From 3d8bca17493090df88bd79565846a26fd316815b Mon Sep 17 00:00:00 2001 From: archive Date: Thu, 12 Aug 2010 00:00:00 +0000 Subject: [PATCH] as released 2010-08-12 --- COPYING.txt | 643 ++ README.txt | 133 + main/ui/menudef.h | 394 ++ src/Makefile | 3 + src/botai/ai_chat.c | 1313 ++++ src/botai/ai_chat.h | 66 + src/botai/ai_cmd.c | 1645 +++++ src/botai/ai_cmd.h | 40 + src/botai/ai_dmnet.c | 2045 ++++++ src/botai/ai_dmnet.h | 66 + src/botai/ai_dmq3.c | 2899 ++++++++ src/botai/ai_dmq3.h | 156 + src/botai/ai_main.c | 1208 ++++ src/botai/ai_main.h | 242 + src/botai/ai_team.c | 612 ++ src/botai/ai_team.h | 40 + src/botai/botai.h | 110 + src/botai/chars.h | 150 + src/botai/inv.h | 128 + src/botai/match.h | 134 + src/botai/syn.h | 46 + src/botlib/aasfile.h | 276 + src/botlib/be_aas.h | 193 + src/botlib/be_aas_bsp.h | 96 + src/botlib/be_aas_bspq3.c | 525 ++ src/botlib/be_aas_cluster.c | 1601 +++++ src/botlib/be_aas_cluster.h | 43 + src/botlib/be_aas_debug.c | 692 ++ src/botlib/be_aas_debug.h | 68 + src/botlib/be_aas_def.h | 295 + src/botlib/be_aas_entity.c | 499 ++ src/botlib/be_aas_entity.h | 68 + src/botlib/be_aas_file.c | 662 ++ src/botlib/be_aas_file.h | 48 + src/botlib/be_aas_funcs.h | 56 + src/botlib/be_aas_main.c | 486 ++ src/botlib/be_aas_main.h | 69 + src/botlib/be_aas_move.c | 912 +++ src/botlib/be_aas_move.h | 69 + src/botlib/be_aas_optimize.c | 337 + src/botlib/be_aas_optimize.h | 39 + src/botlib/be_aas_reach.c | 4480 ++++++++++++ src/botlib/be_aas_reach.h | 74 + src/botlib/be_aas_route.c | 2640 ++++++++ src/botlib/be_aas_route.h | 68 + src/botlib/be_aas_routealt.c | 271 + src/botlib/be_aas_routealt.h | 46 + src/botlib/be_aas_routetable.c | 1449 ++++ src/botlib/be_aas_routetable.h | 171 + src/botlib/be_aas_sample.c | 1274 ++++ src/botlib/be_aas_sample.h | 70 + src/botlib/be_ai_char.c | 765 +++ src/botlib/be_ai_chat.c | 2883 ++++++++ src/botlib/be_ai_gen.c | 151 + src/botlib/be_ai_goal.c | 1630 +++++ src/botlib/be_ai_move.c | 3697 ++++++++++ src/botlib/be_ai_weap.c | 547 ++ src/botlib/be_ai_weight.c | 972 +++ src/botlib/be_ai_weight.h | 89 + src/botlib/be_ea.c | 480 ++ src/botlib/be_interface.c | 936 +++ src/botlib/be_interface.h | 93 + src/botlib/botlib.h | 512 ++ src/botlib/botlib.vcproj | 911 +++ src/botlib/l_crc.c | 155 + src/botlib/l_crc.h | 44 + src/botlib/l_libvar.c | 282 + src/botlib/l_libvar.h | 69 + src/botlib/l_log.c | 186 + src/botlib/l_log.h | 54 + src/botlib/l_memory.c | 446 ++ src/botlib/l_memory.h | 84 + src/botlib/l_precomp.c | 3229 +++++++++ src/botlib/l_precomp.h | 182 + src/botlib/l_script.c | 1452 ++++ src/botlib/l_script.h | 268 + src/botlib/l_struct.c | 507 ++ src/botlib/l_struct.h | 81 + src/botlib/l_utils.h | 41 + src/bspc/_files.c | 91 + src/bspc/aas_areamerging.c | 424 ++ src/bspc/aas_areamerging.h | 40 + src/bspc/aas_cfg.c | 317 + src/bspc/aas_cfg.h | 89 + src/bspc/aas_create.c | 1180 ++++ src/bspc/aas_create.h | 153 + src/bspc/aas_edgemelting.c | 125 + src/bspc/aas_edgemelting.h | 40 + src/bspc/aas_facemerging.c | 312 + src/bspc/aas_facemerging.h | 39 + src/bspc/aas_file.c | 603 ++ src/bspc/aas_file.h | 40 + src/bspc/aas_gsubdiv.c | 688 ++ src/bspc/aas_gsubdiv.h | 40 + src/bspc/aas_map.c | 840 +++ src/bspc/aas_map.h | 38 + src/bspc/aas_prunenodes.c | 103 + src/bspc/aas_prunenodes.h | 39 + src/bspc/aas_store.c | 1124 +++ src/bspc/aas_store.h | 126 + src/bspc/be_aas_bspc.c | 314 + src/bspc/be_aas_bspc.h | 44 + src/bspc/brushbsp.c | 1924 ++++++ src/bspc/bspc.c | 1129 +++ src/bspc/csg.c | 1053 +++ src/bspc/faces.c | 1002 +++ src/bspc/glfile.c | 157 + src/bspc/l_bsp_ent.c | 195 + src/bspc/l_bsp_ent.h | 65 + src/bspc/l_bsp_hl.c | 886 +++ src/bspc/l_bsp_hl.h | 332 + src/bspc/l_bsp_q1.c | 608 ++ src/bspc/l_bsp_q1.h | 282 + src/bspc/l_bsp_q2.c | 1139 ++++ src/bspc/l_bsp_q2.h | 104 + src/bspc/l_bsp_q3.c | 857 +++ src/bspc/l_bsp_q3.h | 88 + src/bspc/l_bsp_sin.c | 1186 ++++ src/bspc/l_bsp_sin.h | 113 + src/bspc/l_cmd.c | 1181 ++++ src/bspc/l_cmd.h | 164 + src/bspc/l_log.c | 220 + src/bspc/l_log.h | 57 + src/bspc/l_math.c | 279 + src/bspc/l_math.h | 100 + src/bspc/l_mem.c | 468 ++ src/bspc/l_mem.h | 58 + src/bspc/l_poly.c | 1434 ++++ src/bspc/l_poly.h | 136 + src/bspc/l_qfiles.c | 702 ++ src/bspc/l_qfiles.h | 106 + src/bspc/l_threads.c | 1528 +++++ src/bspc/l_threads.h | 52 + src/bspc/l_utils.c | 267 + src/bspc/l_utils.h | 94 + src/bspc/leakfile.c | 108 + src/bspc/map.c | 1360 ++++ src/bspc/map_hl.c | 1147 ++++ src/bspc/map_q1.c | 1205 ++++ src/bspc/map_q2.c | 1147 ++++ src/bspc/map_q3.c | 729 ++ src/bspc/map_sin.c | 1201 ++++ src/bspc/nodraw.c | 50 + src/bspc/portals.c | 1308 ++++ src/bspc/prtfile.c | 287 + src/bspc/q2files.h | 494 ++ src/bspc/q3files.h | 379 ++ src/bspc/qbsp.h | 498 ++ src/bspc/qfiles.h | 495 ++ src/bspc/sinfiles.h | 372 + src/bspc/textures.c | 249 + src/bspc/tree.c | 298 + src/bspc/writebsp.c | 632 ++ src/cgame/cg_consolecmds.c | 548 ++ src/cgame/cg_draw.c | 3620 ++++++++++ src/cgame/cg_drawtools.c | 1297 ++++ src/cgame/cg_effects.c | 1708 +++++ src/cgame/cg_ents.c | 2680 ++++++++ src/cgame/cg_event.c | 2882 ++++++++ src/cgame/cg_flamethrower.c | 1696 +++++ src/cgame/cg_info.c | 667 ++ src/cgame/cg_local.h | 2460 +++++++ src/cgame/cg_localents.c | 1775 +++++ src/cgame/cg_main.c | 2427 +++++++ src/cgame/cg_marks.c | 352 + src/cgame/cg_newDraw.c | 2598 +++++++ src/cgame/cg_particles.c | 2626 +++++++ src/cgame/cg_players.c | 5589 +++++++++++++++ src/cgame/cg_playerstate.c | 555 ++ src/cgame/cg_predict.c | 765 +++ src/cgame/cg_public.h | 278 + src/cgame/cg_scoreboard.c | 698 ++ src/cgame/cg_servercmds.c | 1038 +++ src/cgame/cg_snapshot.c | 526 ++ src/cgame/cg_sound.c | 461 ++ src/cgame/cg_syscalls.c | 542 ++ src/cgame/cg_trails.c | 785 +++ src/cgame/cg_view.c | 1655 +++++ src/cgame/cg_weapons.c | 6303 +++++++++++++++++ src/cgame/cgame.def | 3 + src/cgame/cgame.vcproj | 914 +++ src/cgame/tr_types.h | 343 + src/client/cl_cgame.c | 1289 ++++ src/client/cl_cin.c | 1878 +++++ src/client/cl_console.c | 866 +++ src/client/cl_input.c | 1037 +++ src/client/cl_keys.c | 1906 ++++++ src/client/cl_main.c | 3621 ++++++++++ src/client/cl_net_chan.c | 197 + src/client/cl_parse.c | 654 ++ src/client/cl_scrn.c | 555 ++ src/client/cl_ui.c | 1251 ++++ src/client/client.h | 590 ++ src/client/keys.h | 66 + src/client/snd_adpcm.c | 142 + src/client/snd_dma.c | 2574 +++++++ src/client/snd_local.h | 315 + src/client/snd_mem.c | 446 ++ src/client/snd_mix.c | 952 +++ src/client/snd_public.h | 105 + src/client/snd_wavelet.c | 280 + src/extractfuncs/ChangeLog | 17 + src/extractfuncs/Conscript | 9 + src/extractfuncs/extractfuncs.bat | 3 + src/extractfuncs/extractfuncs.c | 653 ++ src/extractfuncs/extractfuncs.vcproj | 428 ++ src/extractfuncs/l_log.c | 199 + src/extractfuncs/l_log.h | 59 + src/extractfuncs/l_memory.c | 444 ++ src/extractfuncs/l_memory.h | 80 + src/extractfuncs/l_precomp.c | 3167 +++++++++ src/extractfuncs/l_precomp.h | 158 + src/extractfuncs/l_script.c | 1419 ++++ src/extractfuncs/l_script.h | 266 + src/ft2/ahangles.c | 128 + src/ft2/ahangles.h | 55 + src/ft2/ahglobal.c | 397 ++ src/ft2/ahglobal.h | 42 + src/ft2/ahglyph.c | 1275 ++++ src/ft2/ahglyph.h | 86 + src/ft2/ahhint.c | 1362 ++++ src/ft2/ahhint.h | 65 + src/ft2/ahloader.h | 123 + src/ft2/ahmodule.c | 114 + src/ft2/ahmodule.h | 32 + src/ft2/ahoptim.c | 875 +++ src/ft2/ahoptim.h | 127 + src/ft2/ahtypes.h | 484 ++ src/ft2/autohint.h | 195 + src/ft2/freetype.h | 2286 +++++++ src/ft2/ftcalc.c | 777 +++ src/ft2/ftcalc.h | 123 + src/ft2/ftconfig.h | 187 + src/ft2/ftdebug.c | 124 + src/ft2/ftdebug.h | 225 + src/ft2/ftdriver.h | 182 + src/ft2/fterrors.h | 166 + src/ft2/ftextend.c | 332 + src/ft2/ftextend.h | 178 + src/ft2/ftglyph.c | 1185 ++++ src/ft2/ftglyph.h | 422 ++ src/ft2/ftgrays.c | 1996 ++++++ src/ft2/ftgrays.h | 48 + src/ft2/ftimage.h | 1003 +++ src/ft2/ftinit.c | 161 + src/ft2/ftlist.c | 310 + src/ft2/ftlist.h | 113 + src/ft2/ftmemory.h | 127 + src/ft2/ftmm.c | 179 + src/ft2/ftmm.h | 175 + src/ft2/ftmodule.h | 274 + src/ft2/ftnames.c | 68 + src/ft2/ftnames.h | 52 + src/ft2/ftobjs.c | 3280 +++++++++ src/ft2/ftobjs.h | 532 ++ src/ft2/ftoption.h | 395 ++ src/ft2/ftoutln.c | 861 +++ src/ft2/ftoutln.h | 344 + src/ft2/ftraster.c | 3251 +++++++++ src/ft2/ftraster.h | 50 + src/ft2/ftrend1.c | 264 + src/ft2/ftrend1.h | 37 + src/ft2/ftrender.h | 191 + src/ft2/ftsmooth.c | 219 + src/ft2/ftsmooth.h | 35 + src/ft2/ftstream.c | 807 +++ src/ft2/ftstream.h | 361 + src/ft2/ftsystem.c | 293 + src/ft2/ftsystem.h | 101 + src/ft2/fttypes.h | 400 ++ src/ft2/psnames.h | 220 + src/ft2/sfdriver.c | 211 + src/ft2/sfdriver.h | 29 + src/ft2/sfnt.h | 492 ++ src/ft2/sfobjs.c | 555 ++ src/ft2/sfobjs.h | 57 + src/ft2/t1tables.h | 235 + src/ft2/ttcmap.c | 550 ++ src/ft2/ttcmap.h | 45 + src/ft2/ttdriver.c | 501 ++ src/ft2/ttdriver.h | 31 + src/ft2/tterrors.h | 121 + src/ft2/ttgload.c | 1432 ++++ src/ft2/ttgload.h | 57 + src/ft2/ttinterp.c | 7349 ++++++++++++++++++++ src/ft2/ttinterp.h | 270 + src/ft2/ttload.c | 1673 +++++ src/ft2/ttload.h | 132 + src/ft2/ttnameid.h | 698 ++ src/ft2/ttobjs.c | 727 ++ src/ft2/ttobjs.h | 412 ++ src/ft2/ttpload.c | 266 + src/ft2/ttpload.h | 51 + src/ft2/ttpost.c | 528 ++ src/ft2/ttpost.h | 52 + src/ft2/ttsbit.c | 1449 ++++ src/ft2/ttsbit.h | 57 + src/ft2/tttables.h | 583 ++ src/ft2/tttags.h | 66 + src/ft2/tttypes.h | 1582 +++++ src/game/ai_cast.c | 1003 +++ src/game/ai_cast.h | 746 ++ src/game/ai_cast_characters.c | 1822 +++++ src/game/ai_cast_debug.c | 237 + src/game/ai_cast_events.c | 594 ++ src/game/ai_cast_fight.c | 2371 +++++++ src/game/ai_cast_fight.h | 42 + src/game/ai_cast_func_attack.c | 1259 ++++ src/game/ai_cast_func_boss1.c | 1034 +++ src/game/ai_cast_funcs.c | 5137 ++++++++++++++ src/game/ai_cast_global.h | 85 + src/game/ai_cast_script.c | 807 +++ src/game/ai_cast_script_actions.c | 3108 +++++++++ src/game/ai_cast_script_ents.c | 229 + src/game/ai_cast_sight.c | 790 +++ src/game/ai_cast_think.c | 1755 +++++ src/game/be_aas.h | 194 + src/game/be_ai_char.h | 53 + src/game/be_ai_chat.h | 118 + src/game/be_ai_gen.h | 38 + src/game/be_ai_goal.h | 117 + src/game/be_ai_move.h | 133 + src/game/be_ai_weap.h | 109 + src/game/be_ea.h | 72 + src/game/bg_animation.c | 2146 ++++++ src/game/bg_lib.c | 1158 ++++ src/game/bg_local.h | 99 + src/game/bg_misc.c | 4561 +++++++++++++ src/game/bg_pmove.c | 4388 ++++++++++++ src/game/bg_public.h | 1750 +++++ src/game/bg_slidemove.c | 329 + src/game/botlib.h | 521 ++ src/game/g_active.c | 1677 +++++ src/game/g_alarm.c | 286 + src/game/g_bot.c | 850 +++ src/game/g_client.c | 2041 ++++++ src/game/g_cmds.c | 2147 ++++++ src/game/g_combat.c | 1549 +++++ src/game/g_func_decs.h | 1547 +++++ src/game/g_funcs.h | 1548 +++++ src/game/g_items.c | 1348 ++++ src/game/g_local.h | 1392 ++++ src/game/g_main.c | 2588 +++++++ src/game/g_mem.c | 69 + src/game/g_misc.c | 2690 ++++++++ src/game/g_missile.c | 1473 ++++ src/game/g_mover.c | 4752 +++++++++++++ src/game/g_props.c | 4180 ++++++++++++ src/game/g_public.h | 476 ++ src/game/g_save.c | 1909 ++++++ src/game/g_script.c | 877 +++ src/game/g_script_actions.c | 1510 +++++ src/game/g_session.c | 200 + src/game/g_spawn.c | 1055 +++ src/game/g_svcmds.c | 537 ++ src/game/g_syscalls.c | 811 +++ src/game/g_target.c | 1124 +++ src/game/g_team.c | 1186 ++++ src/game/g_team.h | 72 + src/game/g_tramcar.c | 1620 +++++ src/game/g_trigger.c | 950 +++ src/game/g_utils.c | 827 +++ src/game/g_weapon.c | 1910 ++++++ src/game/game.def | 3 + src/game/game.vcproj | 1404 ++++ src/game/q_math.c | 1384 ++++ src/game/q_shared.c | 1393 ++++ src/game/q_shared.h | 1542 +++++ src/game/surfaceflags.h | 130 + src/idLib/idAudio.h | 109 + src/idLib/idAudioHardware.h | 249 + src/idLib/idLib.h | 509 ++ src/idLib/idQ3Asnd.cpp | 496 ++ src/idLib/idSound.cpp | 122 + src/idLib/idSound.h | 95 + src/idLib/idSpeaker.cpp | 271 + src/idLib/idSpeaker.h | 181 + src/idLib/sys/win32/eax.h | 561 ++ src/idLib/sys/win32/eaxguid.lib | Bin 0 -> 1888 bytes src/idLib/sys/win32/win_snd.cpp | 1520 +++++ src/jpeg-6/jcapimin.c | 229 + src/jpeg-6/jccoefct.c | 449 ++ src/jpeg-6/jccolor.c | 467 ++ src/jpeg-6/jcdctmgr.c | 383 ++ src/jpeg-6/jchuff.c | 860 +++ src/jpeg-6/jchuff.h | 34 + src/jpeg-6/jcinit.c | 72 + src/jpeg-6/jcmainct.c | 299 + src/jpeg-6/jcmarker.c | 637 ++ src/jpeg-6/jcmaster.c | 604 ++ src/jpeg-6/jcomapi.c | 91 + src/jpeg-6/jconfig.h | 41 + src/jpeg-6/jcparam.c | 582 ++ src/jpeg-6/jcphuff.c | 844 +++ src/jpeg-6/jcprepct.c | 370 + src/jpeg-6/jcsample.c | 512 ++ src/jpeg-6/jctrans.c | 373 + src/jpeg-6/jdapimin.c | 395 ++ src/jpeg-6/jdapistd.c | 282 + src/jpeg-6/jdatadst.c | 150 + src/jpeg-6/jdatasrc.c | 199 + src/jpeg-6/jdcoefct.c | 747 ++ src/jpeg-6/jdcolor.c | 369 + src/jpeg-6/jdct.h | 176 + src/jpeg-6/jddctmgr.c | 270 + src/jpeg-6/jdhuff.c | 581 ++ src/jpeg-6/jdhuff.h | 202 + src/jpeg-6/jdinput.c | 392 ++ src/jpeg-6/jdmainct.c | 521 ++ src/jpeg-6/jdmarker.c | 1080 +++ src/jpeg-6/jdmaster.c | 564 ++ src/jpeg-6/jdpostct.c | 290 + src/jpeg-6/jdsample.c | 476 ++ src/jpeg-6/jdtrans.c | 125 + src/jpeg-6/jerror.c | 231 + src/jpeg-6/jerror.h | 273 + src/jpeg-6/jfdctflt.c | 167 + src/jpeg-6/jidctflt.c | 240 + src/jpeg-6/jinclude.h | 116 + src/jpeg-6/jmemmgr.c | 1143 ++++ src/jpeg-6/jmemnobs.c | 97 + src/jpeg-6/jmemsys.h | 182 + src/jpeg-6/jmorecfg.h | 348 + src/jpeg-6/jpegint.h | 388 ++ src/jpeg-6/jpeglib.h | 1051 +++ src/jpeg-6/jutils.c | 170 + src/jpeg-6/jversion.h | 14 + src/macosx/BuildRelease | 34 + src/macosx/CGMouseDeltaFix.h | 34 + src/macosx/CGMouseDeltaFix.m | 139 + src/macosx/CGPrivateAPI.h | 192 + src/macosx/GenerateQGL.pl | 292 + src/macosx/Performance.rtf | 228 + src/macosx/Q3Controller.h | 47 + src/macosx/Q3Controller.m | 421 ++ src/macosx/Quake3.icns | Bin 0 -> 47878 bytes src/macosx/Quake3.nib/classes.nib | 35 + src/macosx/Quake3.nib/info.nib | 16 + src/macosx/Quake3.nib/objects.nib | Bin 0 -> 768 bytes src/macosx/RecordDemo.zsh | 18 + src/macosx/WolfSP.pbproj/bungi.pbxuser | 148 + src/macosx/WolfSP.pbproj/duane.pbxuser | 148 + src/macosx/WolfSP.pbproj/johnson.pbxuser | 134 + src/macosx/WolfSP.pbproj/project.pbxproj | 6523 ++++++++++++++++++ src/macosx/WolfSP.pbproj/zaphod.pbxuser | 303 + src/macosx/banner.jpg | Bin 0 -> 17137 bytes src/macosx/macosx_display.h | 45 + src/macosx/macosx_display.m | 393 ++ src/macosx/macosx_glimp.h | 44 + src/macosx/macosx_glimp.m | 1073 +++ src/macosx/macosx_glsmp_mutex.m | 178 + src/macosx/macosx_glsmp_null.m | 49 + src/macosx/macosx_glsmp_ports.m | 425 ++ src/macosx/macosx_input.m | 935 +++ src/macosx/macosx_local.h | 136 + src/macosx/macosx_qgl.h | 5448 +++++++++++++++ src/macosx/macosx_sndcore.m | 313 + src/macosx/macosx_snddma.m | 210 + src/macosx/macosx_sys.m | 509 ++ src/macosx/macosx_timers.h | 63 + src/macosx/macosx_timers.m | 89 + src/macosx/timedemo.zsh | 52 + src/qcommon/cm_load.c | 865 +++ src/qcommon/cm_local.h | 206 + src/qcommon/cm_patch.c | 1783 +++++ src/qcommon/cm_patch.h | 110 + src/qcommon/cm_polylib.c | 743 ++ src/qcommon/cm_polylib.h | 76 + src/qcommon/cm_public.h | 82 + src/qcommon/cm_test.c | 486 ++ src/qcommon/cm_trace.c | 1462 ++++ src/qcommon/cmd.c | 698 ++ src/qcommon/common.c | 2943 ++++++++ src/qcommon/cvar.c | 908 +++ src/qcommon/files.c | 3536 ++++++++++ src/qcommon/huffman.c | 432 ++ src/qcommon/md4.c | 314 + src/qcommon/msg.c | 2284 +++++++ src/qcommon/net_chan.c | 719 ++ src/qcommon/qcommon.h | 1072 +++ src/qcommon/qfiles.h | 731 ++ src/qcommon/unzip.c | 4329 ++++++++++++ src/qcommon/unzip.h | 341 + src/qcommon/vm.c | 861 +++ src/qcommon/vm_interpreted.c | 896 +++ src/qcommon/vm_local.h | 184 + src/qcommon/vm_x86.c | 826 +++ src/renderer/anorms256.h | 284 + src/renderer/qgl.h | 617 ++ src/renderer/qgl_linked.h | 364 + src/renderer/ref_trin.def | 2 + src/renderer/renderer.vcproj | 2440 +++++++ src/renderer/tr_animation.c | 1488 ++++ src/renderer/tr_backend.c | 1644 +++++ src/renderer/tr_bsp.c | 2260 ++++++ src/renderer/tr_cmds.c | 556 ++ src/renderer/tr_cmesh.c | 446 ++ src/renderer/tr_curve.c | 634 ++ src/renderer/tr_flares.c | 543 ++ src/renderer/tr_font.c | 550 ++ src/renderer/tr_image.c | 3718 ++++++++++ src/renderer/tr_init.c | 1417 ++++ src/renderer/tr_light.c | 424 ++ src/renderer/tr_local.h | 1856 +++++ src/renderer/tr_main.c | 1881 +++++ src/renderer/tr_marks.c | 822 +++ src/renderer/tr_mesh.c | 450 ++ src/renderer/tr_model.c | 2135 ++++++ src/renderer/tr_noise.c | 99 + src/renderer/tr_public.h | 183 + src/renderer/tr_scene.c | 542 ++ src/renderer/tr_shade.c | 1692 +++++ src/renderer/tr_shade_calc.c | 1232 ++++ src/renderer/tr_shader.c | 3307 +++++++++ src/renderer/tr_shadows.c | 348 + src/renderer/tr_sky.c | 1030 +++ src/renderer/tr_surface.c | 1502 ++++ src/renderer/tr_world.c | 710 ++ src/server/server.h | 421 ++ src/server/sv_bot.c | 694 ++ src/server/sv_ccmds.c | 965 +++ src/server/sv_client.c | 1481 ++++ src/server/sv_game.c | 1054 +++ src/server/sv_init.c | 1036 +++ src/server/sv_main.c | 862 +++ src/server/sv_net_chan.c | 191 + src/server/sv_snapshot.c | 812 +++ src/server/sv_world.c | 732 ++ src/splines/Splines.vcproj | 379 ++ src/splines/math_angles.cpp | 157 + src/splines/math_angles.h | 203 + src/splines/math_matrix.cpp | 141 + src/splines/math_matrix.h | 230 + src/splines/math_quaternion.cpp | 85 + src/splines/math_quaternion.h | 197 + src/splines/math_vector.cpp | 151 + src/splines/math_vector.h | 587 ++ src/splines/q_parse.cpp | 541 ++ src/splines/q_shared.cpp | 994 +++ src/splines/q_splineshared.h | 816 +++ src/splines/splines.cpp | 1418 ++++ src/splines/splines.h | 1100 +++ src/splines/util_list.h | 353 + src/splines/util_str.cpp | 571 ++ src/splines/util_str.h | 728 ++ src/ui/keycodes.h | 169 + src/ui/ui.def | 3 + src/ui/ui.vcproj | 496 ++ src/ui/ui_atoms.c | 531 ++ src/ui/ui_gameinfo.c | 331 + src/ui/ui_local.h | 1172 ++++ src/ui/ui_main.c | 7544 +++++++++++++++++++++ src/ui/ui_players.c | 1692 +++++ src/ui/ui_public.h | 229 + src/ui/ui_shared.c | 6329 +++++++++++++++++ src/ui/ui_shared.h | 504 ++ src/ui/ui_syscalls.c | 452 ++ src/ui/ui_util.c | 36 + src/unix/ChangeLog | 2564 +++++++ src/unix/Conscript-bspc | 78 + src/unix/Conscript-cgame | 50 + src/unix/Conscript-client | 264 + src/unix/Conscript-game | 107 + src/unix/Conscript-setup | 28 + src/unix/Conscript-ui | 30 + src/unix/Construct | 249 + src/unix/README.EULA | 184 + src/unix/README.Linux | 352 + src/unix/README.Q3Test | 306 + src/unix/bspc.vpj | 124 + src/unix/build_setup.sh | 119 + src/unix/build_tarball.sh | 21 + src/unix/cgame.vpj | 47 + src/unix/client.vpj | 109 + src/unix/cons | 6828 +++++++++++++++++++ src/unix/extractfuncs.vpj | 28 + src/unix/ftol.nasm | 131 + src/unix/game.vpj | 86 + src/unix/linux_common.c | 348 + src/unix/linux_glimp.c | 1642 +++++ src/unix/linux_joystick.c | 213 + src/unix/linux_local.h | 57 + src/unix/linux_qgl.c | 3819 +++++++++++ src/unix/linux_snd.c | 309 + src/unix/matha.s | 404 ++ src/unix/pcons-2.3.1 | 7911 ++++++++++++++++++++++ src/unix/qasm.h | 487 ++ src/unix/quake3.xpm | 161 + src/unix/snapvector.nasm | 75 + src/unix/snd_mixa.s | 197 + src/unix/sys_dosa.s | 94 + src/unix/ui.vpj | 31 + src/unix/unix_glw.h | 45 + src/unix/unix_main.c | 1333 ++++ src/unix/unix_net.c | 601 ++ src/unix/unix_shared.c | 420 ++ src/unix/vm_x86.c | 36 + src/unix/vm_x86a.s | 441 ++ src/unix/wolf.vpw | 408 ++ src/win32/background.bmp | Bin 0 -> 197688 bytes src/win32/clear.bmp | Bin 0 -> 5174 bytes src/win32/glw_win.h | 58 + src/win32/qe3.ico | Bin 0 -> 28198 bytes src/win32/resource.h | 49 + src/win32/save/win_snd.c | 681 ++ src/win32/win_gamma.c | 208 + src/win32/win_glimp.c | 1607 +++++ src/win32/win_input.c | 874 +++ src/win32/win_local.h | 120 + src/win32/win_main.c | 1354 ++++ src/win32/win_net.c | 1019 +++ src/win32/win_qgl.c | 4040 +++++++++++ src/win32/win_shared.c | 395 ++ src/win32/win_snd.c | 392 ++ src/win32/win_syscon.c | 557 ++ src/win32/win_wndproc.c | 496 ++ src/win32/winquake.rc | 79 + src/wolf.sln | 73 + src/wolf.vcproj | 1352 ++++ uncrustify.cfg | 929 +++ 620 files changed, 480657 insertions(+) create mode 100644 COPYING.txt create mode 100644 README.txt create mode 100644 main/ui/menudef.h create mode 100644 src/Makefile create mode 100644 src/botai/ai_chat.c create mode 100644 src/botai/ai_chat.h create mode 100644 src/botai/ai_cmd.c create mode 100644 src/botai/ai_cmd.h create mode 100644 src/botai/ai_dmnet.c create mode 100644 src/botai/ai_dmnet.h create mode 100644 src/botai/ai_dmq3.c create mode 100644 src/botai/ai_dmq3.h create mode 100644 src/botai/ai_main.c create mode 100644 src/botai/ai_main.h create mode 100644 src/botai/ai_team.c create mode 100644 src/botai/ai_team.h create mode 100644 src/botai/botai.h create mode 100644 src/botai/chars.h create mode 100644 src/botai/inv.h create mode 100644 src/botai/match.h create mode 100644 src/botai/syn.h create mode 100644 src/botlib/aasfile.h create mode 100644 src/botlib/be_aas.h create mode 100644 src/botlib/be_aas_bsp.h create mode 100644 src/botlib/be_aas_bspq3.c create mode 100644 src/botlib/be_aas_cluster.c create mode 100644 src/botlib/be_aas_cluster.h create mode 100644 src/botlib/be_aas_debug.c create mode 100644 src/botlib/be_aas_debug.h create mode 100644 src/botlib/be_aas_def.h create mode 100644 src/botlib/be_aas_entity.c create mode 100644 src/botlib/be_aas_entity.h create mode 100644 src/botlib/be_aas_file.c create mode 100644 src/botlib/be_aas_file.h create mode 100644 src/botlib/be_aas_funcs.h create mode 100644 src/botlib/be_aas_main.c create mode 100644 src/botlib/be_aas_main.h create mode 100644 src/botlib/be_aas_move.c create mode 100644 src/botlib/be_aas_move.h create mode 100644 src/botlib/be_aas_optimize.c create mode 100644 src/botlib/be_aas_optimize.h create mode 100644 src/botlib/be_aas_reach.c create mode 100644 src/botlib/be_aas_reach.h create mode 100644 src/botlib/be_aas_route.c create mode 100644 src/botlib/be_aas_route.h create mode 100644 src/botlib/be_aas_routealt.c create mode 100644 src/botlib/be_aas_routealt.h create mode 100644 src/botlib/be_aas_routetable.c create mode 100644 src/botlib/be_aas_routetable.h create mode 100644 src/botlib/be_aas_sample.c create mode 100644 src/botlib/be_aas_sample.h create mode 100644 src/botlib/be_ai_char.c create mode 100644 src/botlib/be_ai_chat.c create mode 100644 src/botlib/be_ai_gen.c create mode 100644 src/botlib/be_ai_goal.c create mode 100644 src/botlib/be_ai_move.c create mode 100644 src/botlib/be_ai_weap.c create mode 100644 src/botlib/be_ai_weight.c create mode 100644 src/botlib/be_ai_weight.h create mode 100644 src/botlib/be_ea.c create mode 100644 src/botlib/be_interface.c create mode 100644 src/botlib/be_interface.h create mode 100644 src/botlib/botlib.h create mode 100644 src/botlib/botlib.vcproj create mode 100644 src/botlib/l_crc.c create mode 100644 src/botlib/l_crc.h create mode 100644 src/botlib/l_libvar.c create mode 100644 src/botlib/l_libvar.h create mode 100644 src/botlib/l_log.c create mode 100644 src/botlib/l_log.h create mode 100644 src/botlib/l_memory.c create mode 100644 src/botlib/l_memory.h create mode 100644 src/botlib/l_precomp.c create mode 100644 src/botlib/l_precomp.h create mode 100644 src/botlib/l_script.c create mode 100644 src/botlib/l_script.h create mode 100644 src/botlib/l_struct.c create mode 100644 src/botlib/l_struct.h create mode 100644 src/botlib/l_utils.h create mode 100644 src/bspc/_files.c create mode 100644 src/bspc/aas_areamerging.c create mode 100644 src/bspc/aas_areamerging.h create mode 100644 src/bspc/aas_cfg.c create mode 100644 src/bspc/aas_cfg.h create mode 100644 src/bspc/aas_create.c create mode 100644 src/bspc/aas_create.h create mode 100644 src/bspc/aas_edgemelting.c create mode 100644 src/bspc/aas_edgemelting.h create mode 100644 src/bspc/aas_facemerging.c create mode 100644 src/bspc/aas_facemerging.h create mode 100644 src/bspc/aas_file.c create mode 100644 src/bspc/aas_file.h create mode 100644 src/bspc/aas_gsubdiv.c create mode 100644 src/bspc/aas_gsubdiv.h create mode 100644 src/bspc/aas_map.c create mode 100644 src/bspc/aas_map.h create mode 100644 src/bspc/aas_prunenodes.c create mode 100644 src/bspc/aas_prunenodes.h create mode 100644 src/bspc/aas_store.c create mode 100644 src/bspc/aas_store.h create mode 100644 src/bspc/be_aas_bspc.c create mode 100644 src/bspc/be_aas_bspc.h create mode 100644 src/bspc/brushbsp.c create mode 100644 src/bspc/bspc.c create mode 100644 src/bspc/csg.c create mode 100644 src/bspc/faces.c create mode 100644 src/bspc/glfile.c create mode 100644 src/bspc/l_bsp_ent.c create mode 100644 src/bspc/l_bsp_ent.h create mode 100644 src/bspc/l_bsp_hl.c create mode 100644 src/bspc/l_bsp_hl.h create mode 100644 src/bspc/l_bsp_q1.c create mode 100644 src/bspc/l_bsp_q1.h create mode 100644 src/bspc/l_bsp_q2.c create mode 100644 src/bspc/l_bsp_q2.h create mode 100644 src/bspc/l_bsp_q3.c create mode 100644 src/bspc/l_bsp_q3.h create mode 100644 src/bspc/l_bsp_sin.c create mode 100644 src/bspc/l_bsp_sin.h create mode 100644 src/bspc/l_cmd.c create mode 100644 src/bspc/l_cmd.h create mode 100644 src/bspc/l_log.c create mode 100644 src/bspc/l_log.h create mode 100644 src/bspc/l_math.c create mode 100644 src/bspc/l_math.h create mode 100644 src/bspc/l_mem.c create mode 100644 src/bspc/l_mem.h create mode 100644 src/bspc/l_poly.c create mode 100644 src/bspc/l_poly.h create mode 100644 src/bspc/l_qfiles.c create mode 100644 src/bspc/l_qfiles.h create mode 100644 src/bspc/l_threads.c create mode 100644 src/bspc/l_threads.h create mode 100644 src/bspc/l_utils.c create mode 100644 src/bspc/l_utils.h create mode 100644 src/bspc/leakfile.c create mode 100644 src/bspc/map.c create mode 100644 src/bspc/map_hl.c create mode 100644 src/bspc/map_q1.c create mode 100644 src/bspc/map_q2.c create mode 100644 src/bspc/map_q3.c create mode 100644 src/bspc/map_sin.c create mode 100644 src/bspc/nodraw.c create mode 100644 src/bspc/portals.c create mode 100644 src/bspc/prtfile.c create mode 100644 src/bspc/q2files.h create mode 100644 src/bspc/q3files.h create mode 100644 src/bspc/qbsp.h create mode 100644 src/bspc/qfiles.h create mode 100644 src/bspc/sinfiles.h create mode 100644 src/bspc/textures.c create mode 100644 src/bspc/tree.c create mode 100644 src/bspc/writebsp.c create mode 100644 src/cgame/cg_consolecmds.c create mode 100644 src/cgame/cg_draw.c create mode 100644 src/cgame/cg_drawtools.c create mode 100644 src/cgame/cg_effects.c create mode 100644 src/cgame/cg_ents.c create mode 100644 src/cgame/cg_event.c create mode 100644 src/cgame/cg_flamethrower.c create mode 100644 src/cgame/cg_info.c create mode 100644 src/cgame/cg_local.h create mode 100644 src/cgame/cg_localents.c create mode 100644 src/cgame/cg_main.c create mode 100644 src/cgame/cg_marks.c create mode 100644 src/cgame/cg_newDraw.c create mode 100644 src/cgame/cg_particles.c create mode 100644 src/cgame/cg_players.c create mode 100644 src/cgame/cg_playerstate.c create mode 100644 src/cgame/cg_predict.c create mode 100644 src/cgame/cg_public.h create mode 100644 src/cgame/cg_scoreboard.c create mode 100644 src/cgame/cg_servercmds.c create mode 100644 src/cgame/cg_snapshot.c create mode 100644 src/cgame/cg_sound.c create mode 100644 src/cgame/cg_syscalls.c create mode 100644 src/cgame/cg_trails.c create mode 100644 src/cgame/cg_view.c create mode 100644 src/cgame/cg_weapons.c create mode 100644 src/cgame/cgame.def create mode 100644 src/cgame/cgame.vcproj create mode 100644 src/cgame/tr_types.h create mode 100644 src/client/cl_cgame.c create mode 100644 src/client/cl_cin.c create mode 100644 src/client/cl_console.c create mode 100644 src/client/cl_input.c create mode 100644 src/client/cl_keys.c create mode 100644 src/client/cl_main.c create mode 100644 src/client/cl_net_chan.c create mode 100644 src/client/cl_parse.c create mode 100644 src/client/cl_scrn.c create mode 100644 src/client/cl_ui.c create mode 100644 src/client/client.h create mode 100644 src/client/keys.h create mode 100644 src/client/snd_adpcm.c create mode 100644 src/client/snd_dma.c create mode 100644 src/client/snd_local.h create mode 100644 src/client/snd_mem.c create mode 100644 src/client/snd_mix.c create mode 100644 src/client/snd_public.h create mode 100644 src/client/snd_wavelet.c create mode 100644 src/extractfuncs/ChangeLog create mode 100644 src/extractfuncs/Conscript create mode 100644 src/extractfuncs/extractfuncs.bat create mode 100644 src/extractfuncs/extractfuncs.c create mode 100644 src/extractfuncs/extractfuncs.vcproj create mode 100644 src/extractfuncs/l_log.c create mode 100644 src/extractfuncs/l_log.h create mode 100644 src/extractfuncs/l_memory.c create mode 100644 src/extractfuncs/l_memory.h create mode 100644 src/extractfuncs/l_precomp.c create mode 100644 src/extractfuncs/l_precomp.h create mode 100644 src/extractfuncs/l_script.c create mode 100644 src/extractfuncs/l_script.h create mode 100644 src/ft2/ahangles.c create mode 100644 src/ft2/ahangles.h create mode 100644 src/ft2/ahglobal.c create mode 100644 src/ft2/ahglobal.h create mode 100644 src/ft2/ahglyph.c create mode 100644 src/ft2/ahglyph.h create mode 100644 src/ft2/ahhint.c create mode 100644 src/ft2/ahhint.h create mode 100644 src/ft2/ahloader.h create mode 100644 src/ft2/ahmodule.c create mode 100644 src/ft2/ahmodule.h create mode 100644 src/ft2/ahoptim.c create mode 100644 src/ft2/ahoptim.h create mode 100644 src/ft2/ahtypes.h create mode 100644 src/ft2/autohint.h create mode 100644 src/ft2/freetype.h create mode 100644 src/ft2/ftcalc.c create mode 100644 src/ft2/ftcalc.h create mode 100644 src/ft2/ftconfig.h create mode 100644 src/ft2/ftdebug.c create mode 100644 src/ft2/ftdebug.h create mode 100644 src/ft2/ftdriver.h create mode 100644 src/ft2/fterrors.h create mode 100644 src/ft2/ftextend.c create mode 100644 src/ft2/ftextend.h create mode 100644 src/ft2/ftglyph.c create mode 100644 src/ft2/ftglyph.h create mode 100644 src/ft2/ftgrays.c create mode 100644 src/ft2/ftgrays.h create mode 100644 src/ft2/ftimage.h create mode 100644 src/ft2/ftinit.c create mode 100644 src/ft2/ftlist.c create mode 100644 src/ft2/ftlist.h create mode 100644 src/ft2/ftmemory.h create mode 100644 src/ft2/ftmm.c create mode 100644 src/ft2/ftmm.h create mode 100644 src/ft2/ftmodule.h create mode 100644 src/ft2/ftnames.c create mode 100644 src/ft2/ftnames.h create mode 100644 src/ft2/ftobjs.c create mode 100644 src/ft2/ftobjs.h create mode 100644 src/ft2/ftoption.h create mode 100644 src/ft2/ftoutln.c create mode 100644 src/ft2/ftoutln.h create mode 100644 src/ft2/ftraster.c create mode 100644 src/ft2/ftraster.h create mode 100644 src/ft2/ftrend1.c create mode 100644 src/ft2/ftrend1.h create mode 100644 src/ft2/ftrender.h create mode 100644 src/ft2/ftsmooth.c create mode 100644 src/ft2/ftsmooth.h create mode 100644 src/ft2/ftstream.c create mode 100644 src/ft2/ftstream.h create mode 100644 src/ft2/ftsystem.c create mode 100644 src/ft2/ftsystem.h create mode 100644 src/ft2/fttypes.h create mode 100644 src/ft2/psnames.h create mode 100644 src/ft2/sfdriver.c create mode 100644 src/ft2/sfdriver.h create mode 100644 src/ft2/sfnt.h create mode 100644 src/ft2/sfobjs.c create mode 100644 src/ft2/sfobjs.h create mode 100644 src/ft2/t1tables.h create mode 100644 src/ft2/ttcmap.c create mode 100644 src/ft2/ttcmap.h create mode 100644 src/ft2/ttdriver.c create mode 100644 src/ft2/ttdriver.h create mode 100644 src/ft2/tterrors.h create mode 100644 src/ft2/ttgload.c create mode 100644 src/ft2/ttgload.h create mode 100644 src/ft2/ttinterp.c create mode 100644 src/ft2/ttinterp.h create mode 100644 src/ft2/ttload.c create mode 100644 src/ft2/ttload.h create mode 100644 src/ft2/ttnameid.h create mode 100644 src/ft2/ttobjs.c create mode 100644 src/ft2/ttobjs.h create mode 100644 src/ft2/ttpload.c create mode 100644 src/ft2/ttpload.h create mode 100644 src/ft2/ttpost.c create mode 100644 src/ft2/ttpost.h create mode 100644 src/ft2/ttsbit.c create mode 100644 src/ft2/ttsbit.h create mode 100644 src/ft2/tttables.h create mode 100644 src/ft2/tttags.h create mode 100644 src/ft2/tttypes.h create mode 100644 src/game/ai_cast.c create mode 100644 src/game/ai_cast.h create mode 100644 src/game/ai_cast_characters.c create mode 100644 src/game/ai_cast_debug.c create mode 100644 src/game/ai_cast_events.c create mode 100644 src/game/ai_cast_fight.c create mode 100644 src/game/ai_cast_fight.h create mode 100644 src/game/ai_cast_func_attack.c create mode 100644 src/game/ai_cast_func_boss1.c create mode 100644 src/game/ai_cast_funcs.c create mode 100644 src/game/ai_cast_global.h create mode 100644 src/game/ai_cast_script.c create mode 100644 src/game/ai_cast_script_actions.c create mode 100644 src/game/ai_cast_script_ents.c create mode 100644 src/game/ai_cast_sight.c create mode 100644 src/game/ai_cast_think.c create mode 100644 src/game/be_aas.h create mode 100644 src/game/be_ai_char.h create mode 100644 src/game/be_ai_chat.h create mode 100644 src/game/be_ai_gen.h create mode 100644 src/game/be_ai_goal.h create mode 100644 src/game/be_ai_move.h create mode 100644 src/game/be_ai_weap.h create mode 100644 src/game/be_ea.h create mode 100644 src/game/bg_animation.c create mode 100644 src/game/bg_lib.c create mode 100644 src/game/bg_local.h create mode 100644 src/game/bg_misc.c create mode 100644 src/game/bg_pmove.c create mode 100644 src/game/bg_public.h create mode 100644 src/game/bg_slidemove.c create mode 100644 src/game/botlib.h create mode 100644 src/game/g_active.c create mode 100644 src/game/g_alarm.c create mode 100644 src/game/g_bot.c create mode 100644 src/game/g_client.c create mode 100644 src/game/g_cmds.c create mode 100644 src/game/g_combat.c create mode 100644 src/game/g_func_decs.h create mode 100644 src/game/g_funcs.h create mode 100644 src/game/g_items.c create mode 100644 src/game/g_local.h create mode 100644 src/game/g_main.c create mode 100644 src/game/g_mem.c create mode 100644 src/game/g_misc.c create mode 100644 src/game/g_missile.c create mode 100644 src/game/g_mover.c create mode 100644 src/game/g_props.c create mode 100644 src/game/g_public.h create mode 100644 src/game/g_save.c create mode 100644 src/game/g_script.c create mode 100644 src/game/g_script_actions.c create mode 100644 src/game/g_session.c create mode 100644 src/game/g_spawn.c create mode 100644 src/game/g_svcmds.c create mode 100644 src/game/g_syscalls.c create mode 100644 src/game/g_target.c create mode 100644 src/game/g_team.c create mode 100644 src/game/g_team.h create mode 100644 src/game/g_tramcar.c create mode 100644 src/game/g_trigger.c create mode 100644 src/game/g_utils.c create mode 100644 src/game/g_weapon.c create mode 100644 src/game/game.def create mode 100644 src/game/game.vcproj create mode 100644 src/game/q_math.c create mode 100644 src/game/q_shared.c create mode 100644 src/game/q_shared.h create mode 100644 src/game/surfaceflags.h create mode 100644 src/idLib/idAudio.h create mode 100644 src/idLib/idAudioHardware.h create mode 100644 src/idLib/idLib.h create mode 100644 src/idLib/idQ3Asnd.cpp create mode 100644 src/idLib/idSound.cpp create mode 100644 src/idLib/idSound.h create mode 100644 src/idLib/idSpeaker.cpp create mode 100644 src/idLib/idSpeaker.h create mode 100644 src/idLib/sys/win32/eax.h create mode 100644 src/idLib/sys/win32/eaxguid.lib create mode 100644 src/idLib/sys/win32/win_snd.cpp create mode 100644 src/jpeg-6/jcapimin.c create mode 100644 src/jpeg-6/jccoefct.c create mode 100644 src/jpeg-6/jccolor.c create mode 100644 src/jpeg-6/jcdctmgr.c create mode 100644 src/jpeg-6/jchuff.c create mode 100644 src/jpeg-6/jchuff.h create mode 100644 src/jpeg-6/jcinit.c create mode 100644 src/jpeg-6/jcmainct.c create mode 100644 src/jpeg-6/jcmarker.c create mode 100644 src/jpeg-6/jcmaster.c create mode 100644 src/jpeg-6/jcomapi.c create mode 100644 src/jpeg-6/jconfig.h create mode 100644 src/jpeg-6/jcparam.c create mode 100644 src/jpeg-6/jcphuff.c create mode 100644 src/jpeg-6/jcprepct.c create mode 100644 src/jpeg-6/jcsample.c create mode 100644 src/jpeg-6/jctrans.c create mode 100644 src/jpeg-6/jdapimin.c create mode 100644 src/jpeg-6/jdapistd.c create mode 100644 src/jpeg-6/jdatadst.c create mode 100644 src/jpeg-6/jdatasrc.c create mode 100644 src/jpeg-6/jdcoefct.c create mode 100644 src/jpeg-6/jdcolor.c create mode 100644 src/jpeg-6/jdct.h create mode 100644 src/jpeg-6/jddctmgr.c create mode 100644 src/jpeg-6/jdhuff.c create mode 100644 src/jpeg-6/jdhuff.h create mode 100644 src/jpeg-6/jdinput.c create mode 100644 src/jpeg-6/jdmainct.c create mode 100644 src/jpeg-6/jdmarker.c create mode 100644 src/jpeg-6/jdmaster.c create mode 100644 src/jpeg-6/jdpostct.c create mode 100644 src/jpeg-6/jdsample.c create mode 100644 src/jpeg-6/jdtrans.c create mode 100644 src/jpeg-6/jerror.c create mode 100644 src/jpeg-6/jerror.h create mode 100644 src/jpeg-6/jfdctflt.c create mode 100644 src/jpeg-6/jidctflt.c create mode 100644 src/jpeg-6/jinclude.h create mode 100644 src/jpeg-6/jmemmgr.c create mode 100644 src/jpeg-6/jmemnobs.c create mode 100644 src/jpeg-6/jmemsys.h create mode 100644 src/jpeg-6/jmorecfg.h create mode 100644 src/jpeg-6/jpegint.h create mode 100644 src/jpeg-6/jpeglib.h create mode 100644 src/jpeg-6/jutils.c create mode 100644 src/jpeg-6/jversion.h create mode 100644 src/macosx/BuildRelease create mode 100644 src/macosx/CGMouseDeltaFix.h create mode 100644 src/macosx/CGMouseDeltaFix.m create mode 100644 src/macosx/CGPrivateAPI.h create mode 100644 src/macosx/GenerateQGL.pl create mode 100644 src/macosx/Performance.rtf create mode 100644 src/macosx/Q3Controller.h create mode 100644 src/macosx/Q3Controller.m create mode 100644 src/macosx/Quake3.icns create mode 100644 src/macosx/Quake3.nib/classes.nib create mode 100644 src/macosx/Quake3.nib/info.nib create mode 100644 src/macosx/Quake3.nib/objects.nib create mode 100644 src/macosx/RecordDemo.zsh create mode 100644 src/macosx/WolfSP.pbproj/bungi.pbxuser create mode 100644 src/macosx/WolfSP.pbproj/duane.pbxuser create mode 100644 src/macosx/WolfSP.pbproj/johnson.pbxuser create mode 100644 src/macosx/WolfSP.pbproj/project.pbxproj create mode 100644 src/macosx/WolfSP.pbproj/zaphod.pbxuser create mode 100644 src/macosx/banner.jpg create mode 100644 src/macosx/macosx_display.h create mode 100644 src/macosx/macosx_display.m create mode 100644 src/macosx/macosx_glimp.h create mode 100644 src/macosx/macosx_glimp.m create mode 100644 src/macosx/macosx_glsmp_mutex.m create mode 100644 src/macosx/macosx_glsmp_null.m create mode 100644 src/macosx/macosx_glsmp_ports.m create mode 100644 src/macosx/macosx_input.m create mode 100644 src/macosx/macosx_local.h create mode 100644 src/macosx/macosx_qgl.h create mode 100644 src/macosx/macosx_sndcore.m create mode 100644 src/macosx/macosx_snddma.m create mode 100644 src/macosx/macosx_sys.m create mode 100644 src/macosx/macosx_timers.h create mode 100644 src/macosx/macosx_timers.m create mode 100644 src/macosx/timedemo.zsh create mode 100644 src/qcommon/cm_load.c create mode 100644 src/qcommon/cm_local.h create mode 100644 src/qcommon/cm_patch.c create mode 100644 src/qcommon/cm_patch.h create mode 100644 src/qcommon/cm_polylib.c create mode 100644 src/qcommon/cm_polylib.h create mode 100644 src/qcommon/cm_public.h create mode 100644 src/qcommon/cm_test.c create mode 100644 src/qcommon/cm_trace.c create mode 100644 src/qcommon/cmd.c create mode 100644 src/qcommon/common.c create mode 100644 src/qcommon/cvar.c create mode 100644 src/qcommon/files.c create mode 100644 src/qcommon/huffman.c create mode 100644 src/qcommon/md4.c create mode 100644 src/qcommon/msg.c create mode 100644 src/qcommon/net_chan.c create mode 100644 src/qcommon/qcommon.h create mode 100644 src/qcommon/qfiles.h create mode 100644 src/qcommon/unzip.c create mode 100644 src/qcommon/unzip.h create mode 100644 src/qcommon/vm.c create mode 100644 src/qcommon/vm_interpreted.c create mode 100644 src/qcommon/vm_local.h create mode 100644 src/qcommon/vm_x86.c create mode 100644 src/renderer/anorms256.h create mode 100644 src/renderer/qgl.h create mode 100644 src/renderer/qgl_linked.h create mode 100644 src/renderer/ref_trin.def create mode 100644 src/renderer/renderer.vcproj create mode 100644 src/renderer/tr_animation.c create mode 100644 src/renderer/tr_backend.c create mode 100644 src/renderer/tr_bsp.c create mode 100644 src/renderer/tr_cmds.c create mode 100644 src/renderer/tr_cmesh.c create mode 100644 src/renderer/tr_curve.c create mode 100644 src/renderer/tr_flares.c create mode 100644 src/renderer/tr_font.c create mode 100644 src/renderer/tr_image.c create mode 100644 src/renderer/tr_init.c create mode 100644 src/renderer/tr_light.c create mode 100644 src/renderer/tr_local.h create mode 100644 src/renderer/tr_main.c create mode 100644 src/renderer/tr_marks.c create mode 100644 src/renderer/tr_mesh.c create mode 100644 src/renderer/tr_model.c create mode 100644 src/renderer/tr_noise.c create mode 100644 src/renderer/tr_public.h create mode 100644 src/renderer/tr_scene.c create mode 100644 src/renderer/tr_shade.c create mode 100644 src/renderer/tr_shade_calc.c create mode 100644 src/renderer/tr_shader.c create mode 100644 src/renderer/tr_shadows.c create mode 100644 src/renderer/tr_sky.c create mode 100644 src/renderer/tr_surface.c create mode 100644 src/renderer/tr_world.c create mode 100644 src/server/server.h create mode 100644 src/server/sv_bot.c create mode 100644 src/server/sv_ccmds.c create mode 100644 src/server/sv_client.c create mode 100644 src/server/sv_game.c create mode 100644 src/server/sv_init.c create mode 100644 src/server/sv_main.c create mode 100644 src/server/sv_net_chan.c create mode 100644 src/server/sv_snapshot.c create mode 100644 src/server/sv_world.c create mode 100644 src/splines/Splines.vcproj create mode 100644 src/splines/math_angles.cpp create mode 100644 src/splines/math_angles.h create mode 100644 src/splines/math_matrix.cpp create mode 100644 src/splines/math_matrix.h create mode 100644 src/splines/math_quaternion.cpp create mode 100644 src/splines/math_quaternion.h create mode 100644 src/splines/math_vector.cpp create mode 100644 src/splines/math_vector.h create mode 100644 src/splines/q_parse.cpp create mode 100644 src/splines/q_shared.cpp create mode 100644 src/splines/q_splineshared.h create mode 100644 src/splines/splines.cpp create mode 100644 src/splines/splines.h create mode 100644 src/splines/util_list.h create mode 100644 src/splines/util_str.cpp create mode 100644 src/splines/util_str.h create mode 100644 src/ui/keycodes.h create mode 100644 src/ui/ui.def create mode 100644 src/ui/ui.vcproj create mode 100644 src/ui/ui_atoms.c create mode 100644 src/ui/ui_gameinfo.c create mode 100644 src/ui/ui_local.h create mode 100644 src/ui/ui_main.c create mode 100644 src/ui/ui_players.c create mode 100644 src/ui/ui_public.h create mode 100644 src/ui/ui_shared.c create mode 100644 src/ui/ui_shared.h create mode 100644 src/ui/ui_syscalls.c create mode 100644 src/ui/ui_util.c create mode 100644 src/unix/ChangeLog create mode 100644 src/unix/Conscript-bspc create mode 100644 src/unix/Conscript-cgame create mode 100644 src/unix/Conscript-client create mode 100644 src/unix/Conscript-game create mode 100644 src/unix/Conscript-setup create mode 100644 src/unix/Conscript-ui create mode 100644 src/unix/Construct create mode 100644 src/unix/README.EULA create mode 100644 src/unix/README.Linux create mode 100644 src/unix/README.Q3Test create mode 100644 src/unix/bspc.vpj create mode 100644 src/unix/build_setup.sh create mode 100755 src/unix/build_tarball.sh create mode 100644 src/unix/cgame.vpj create mode 100644 src/unix/client.vpj create mode 100755 src/unix/cons create mode 100644 src/unix/extractfuncs.vpj create mode 100644 src/unix/ftol.nasm create mode 100644 src/unix/game.vpj create mode 100644 src/unix/linux_common.c create mode 100644 src/unix/linux_glimp.c create mode 100644 src/unix/linux_joystick.c create mode 100644 src/unix/linux_local.h create mode 100644 src/unix/linux_qgl.c create mode 100644 src/unix/linux_snd.c create mode 100644 src/unix/matha.s create mode 100755 src/unix/pcons-2.3.1 create mode 100644 src/unix/qasm.h create mode 100644 src/unix/quake3.xpm create mode 100644 src/unix/snapvector.nasm create mode 100644 src/unix/snd_mixa.s create mode 100644 src/unix/sys_dosa.s create mode 100644 src/unix/ui.vpj create mode 100644 src/unix/unix_glw.h create mode 100644 src/unix/unix_main.c create mode 100644 src/unix/unix_net.c create mode 100644 src/unix/unix_shared.c create mode 100644 src/unix/vm_x86.c create mode 100644 src/unix/vm_x86a.s create mode 100644 src/unix/wolf.vpw create mode 100644 src/win32/background.bmp create mode 100644 src/win32/clear.bmp create mode 100644 src/win32/glw_win.h create mode 100644 src/win32/qe3.ico create mode 100644 src/win32/resource.h create mode 100644 src/win32/save/win_snd.c create mode 100644 src/win32/win_gamma.c create mode 100644 src/win32/win_glimp.c create mode 100644 src/win32/win_input.c create mode 100644 src/win32/win_local.h create mode 100644 src/win32/win_main.c create mode 100644 src/win32/win_net.c create mode 100644 src/win32/win_qgl.c create mode 100644 src/win32/win_shared.c create mode 100644 src/win32/win_snd.c create mode 100644 src/win32/win_syscon.c create mode 100644 src/win32/win_wndproc.c create mode 100644 src/win32/winquake.rc create mode 100644 src/wolf.sln create mode 100644 src/wolf.vcproj create mode 100644 uncrustify.cfg diff --git a/COPYING.txt b/COPYING.txt new file mode 100644 index 0000000..3a82b2a --- /dev/null +++ b/COPYING.txt @@ -0,0 +1,643 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + + +ADDITIONAL TERMS APPLICABLE TO THE RETURN TO CASTLE WOLFENSTEIN SINGLE PLAYER GPL SOURCE CODE. + + The following additional terms (“Additional Terms”) supplement and modify the GNU General Public License, Version 3 (“GPL”) applicable to the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). In addition to the terms and conditions of the GPL, the RTCW SP Source Code is subject to the further restrictions below. + +1. Replacement of Section 15. Section 15 of the GPL shall be deleted in its entirety and replaced with the following: + +“15. Disclaimer of Warranty. + +THE PROGRAM IS PROVIDED WITHOUT ANY WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, TITLE AND MERCHANTABILITY. THE PROGRAM IS BEING DELIVERED OR MADE AVAILABLE “AS IS”, “WITH ALL FAULTS” AND WITHOUT WARRANTY OR REPRESENTATION. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.” + +2. Replacement of Section 16. Section 16 of the GPL shall be deleted in its entirety and replaced with the following: + +“16. LIMITATION OF LIABILITY. + +UNDER NO CIRCUMSTANCES SHALL ANY COPYRIGHT HOLDER OR ITS AFFILIATES, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, FOR ANY DAMAGES OR OTHER LIABILITY, INCLUDING ANY GENERAL, DIRECT, INDIRECT, SPECIAL, INCIDENTAL, CONSEQUENTIAL OR PUNITIVE DAMAGES ARISING FROM, OUT OF OR IN CONNECTION WITH THE USE OR INABILITY TO USE THE PROGRAM OR OTHER DEALINGS WITH THE PROGRAM(INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), WHETHER OR NOT ANY COPYRIGHT HOLDER OR SUCH OTHER PARTY RECEIVES NOTICE OF ANY SUCH DAMAGES AND WHETHER OR NOT SUCH DAMAGES COULD HAVE BEEN FORESEEN.” + +3. LEGAL NOTICES; NO TRADEMARK LICENSE; ORIGIN. You must reproduce faithfully all trademark, copyright and other proprietary and legal notices on any copies of the Program or any other required author attributions. This license does not grant you rights to use any copyright holder or any other party’s name, logo, or trademarks. Neither the name of the copyright holder or its affiliates, or any other party who modifies and/or conveys the Program may be used to endorse or promote products derived from this software without specific prior written permission. The origin of the Program must not be misrepresented; you must not claim that you wrote the original Program. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original Program. + +4. INDEMNIFICATION. IF YOU CONVEY A COVERED WORK AND AGREE WITH ANY RECIPIENT OF THAT COVERED WORK THAT YOU WILL ASSUME ANY LIABILITY FOR THAT COVERED WORK, YOU HEREBY AGREE TO INDEMNIFY, DEFEND AND HOLD HARMLESS THE OTHER LICENSORS AND AUTHORS OF THAT COVERED WORK FOR ANY DAMAEGS, DEMANDS, CLAIMS, LOSSES, CAUSES OF ACTION, LAWSUITS, JUDGMENTS EXPENSES (INCLUDING WITHOUT LIMITATION REASONABLE ATTORNEYS' FEES AND EXPENSES) OR ANY OTHER LIABLITY ARISING FROM, RELATED TO OR IN CONNECTION WITH YOUR ASSUMPTIONS OF LIABILITY. diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..9d27f39 --- /dev/null +++ b/README.txt @@ -0,0 +1,133 @@ +Return to Castle Wolfenstein single player GPL source release +============================================================= + +This file contains the following sections: + +GENERAL NOTES +LICENSE + +GENERAL NOTES +============= + +Game data and patching: +----------------------- + +This source release does not contain any game data, the game data is still +covered by the original EULA and must be obeyed as usual. + +You must patch the game to the latest version. + +Note that RTCW is available from the Steam store at +http://store.steampowered.com/app/9010/ + +Linux note: due to the game CD containing only a Windows version of the game, +you must install and update the game using WINE to get the game data. + +Compiling on win32: +------------------- + +A Visual C++ 2008 project is provided in src\wolf.sln. +The solution file is compatible with the Express release of Visual C++. + +You will need to execute src\extractfuncs\extractfuncs.bat to generate src\game\g_save.c + +You can test your binaries by replacing WolfSP.exe, qagamex86.dll, cgamex86.dll, uix86.dll at the top of the RTCW install + +Compiling on GNU/Linux x86: +--------------------------- + +Go to the src/unix directory, and run the cons script +(cons is a perl based precursor to scons, this is what we were using at the time) + +Run ./cons -h to review build options. Use ./cons -- release to compile in release mode. + +If problems occur, consult the internet. + +Other platforms, updated source code, security issues: +------------------------------------------------------ + +If you have obtained this source code several weeks after the time of release +(August 2010), it is likely that you can find modified and improved +versions of the engine in various open source projects across the internet. +Depending what is your interest with the source code, those may be a better +starting point. + + +LICENSE +======= + +See COPYING.txt for the GNU GENERAL PUBLIC LICENSE + +ADDITIONAL TERMS: The Return to Castle Wolfenstein single player GPL Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU GPL which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +EXCLUDED CODE: The code described below and contained in the Return to Castle Wolfenstein single player GPL Source Code release is not part of the Program covered by the GPL and is expressly excluded from its terms. You are solely responsible for obtaining from the copyright holder a license for such code and complying with the applicable license terms. + +IO on .zip files using portions of zlib +--------------------------------------------------------------------------- +lines file(s) +4301 src/qcommon/unzip.c +Copyright (C) 1998 Gilles Vollant +zlib is Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + +MD4 Message-Digest Algorithm +----------------------------------------------------------------------------- +lines file(s) +289 src/qcommon/md4.c +Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved. + +License to copy and use this software is granted provided that it is identified +as the <93>RSA Data Security, Inc. MD4 Message-Digest Algorithm<94> in all mater +ial mentioning or referencing this software or this function. +License is also granted to make and use derivative works provided that such work +s are identified as <93>derived from the RSA Data Security, Inc. MD4 Message-Dig +est Algorithm<94> in all material mentioning or referencing the derived work. +RSA Data Security, Inc. makes no representations concerning either the merchanta +bility of this software or the suitability of this software for any particular p +urpose. It is provided <93>as is<94> without express or implied warranty of any +kind. + +JPEG library +----------------------------------------------------------------------------- +src/jpeg-6 +Copyright (C) 1991-1995, Thomas G. Lane + +Permission is hereby granted to use, copy, modify, and distribute this +software (or portions thereof) for any purpose, without fee, subject to these +conditions: +(1) If any part of the source code for this software is distributed, then this +README file must be included, with this copyright and no-warranty notice +unaltered; and any additions, deletions, or changes to the original files +must be clearly indicated in accompanying documentation. +(2) If only executable code is distributed, then the accompanying +documentation must state that "this software is based in part on the work of +the Independent JPEG Group". +(3) Permission for use of this software is granted only if the user accepts +full responsibility for any undesirable consequences; the authors accept +NO LIABILITY for damages of any kind. + +These conditions apply to any software derived from or based on the IJG code, +not just to the unmodified library. If you use our work, you ought to +acknowledge us. + +NOTE: unfortunately the README that came with our copy of the library has +been lost, so the one from release 6b is included instead. There are a few +'glue type' modifications to the library to make it easier to use from +the engine, but otherwise the dependency can be easily cleaned up to a +better release of the library. + diff --git a/main/ui/menudef.h b/main/ui/menudef.h new file mode 100644 index 0000000..aab0320 --- /dev/null +++ b/main/ui/menudef.h @@ -0,0 +1,394 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +#define ITEM_TYPE_TEXT 0 // simple text +#define ITEM_TYPE_BUTTON 1 // button, basically text with a border +#define ITEM_TYPE_RADIOBUTTON 2 // toggle button, may be grouped +#define ITEM_TYPE_CHECKBOX 3 // check box +#define ITEM_TYPE_EDITFIELD 4 // editable text, associated with a cvar +#define ITEM_TYPE_COMBO 5 // drop down list +#define ITEM_TYPE_LISTBOX 6 // scrollable list +#define ITEM_TYPE_MODEL 7 // model +#define ITEM_TYPE_OWNERDRAW 8 // owner draw, name specs what it is +#define ITEM_TYPE_NUMERICFIELD 9 // editable text, associated with a cvar +#define ITEM_TYPE_SLIDER 10 // mouse speed, volume, etc. +#define ITEM_TYPE_YESNO 11 // yes no cvar setting +#define ITEM_TYPE_MULTI 12 // multiple list setting, enumerated +#define ITEM_TYPE_BIND 13 // multiple list setting, enumerated +#define ITEM_TYPE_MENUMODEL 14 // special menu model +#define ITEM_TYPE_VALIDFILEFIELD 15 // text must be valid for use in a dos filename + +#define ITEM_ALIGN_LEFT 0 // left alignment +#define ITEM_ALIGN_CENTER 1 // center alignment +#define ITEM_ALIGN_RIGHT 2 // right alignment + +#define ITEM_TEXTSTYLE_NORMAL 0 // normal text +#define ITEM_TEXTSTYLE_BLINK 1 // fast blinking +#define ITEM_TEXTSTYLE_PULSE 2 // slow pulsing +#define ITEM_TEXTSTYLE_SHADOWED 3 // drop shadow ( need a color for this ) +#define ITEM_TEXTSTYLE_OUTLINED 4 // drop shadow ( need a color for this ) +#define ITEM_TEXTSTYLE_OUTLINESHADOWED 5 // drop shadow ( need a color for this ) +#define ITEM_TEXTSTYLE_SHADOWEDMORE 6 // drop shadow ( need a color for this ) + +#define WINDOW_BORDER_NONE 0 // no border +#define WINDOW_BORDER_FULL 1 // full border based on border color ( single pixel ) +#define WINDOW_BORDER_HORZ 2 // horizontal borders only +#define WINDOW_BORDER_VERT 3 // vertical borders only +#define WINDOW_BORDER_KCGRADIENT 4 // horizontal border using the gradient bars + +#define WINDOW_STYLE_EMPTY 0 // no background +#define WINDOW_STYLE_FILLED 1 // filled with background color +#define WINDOW_STYLE_GRADIENT 2 // gradient bar based on background color +#define WINDOW_STYLE_SHADER 3 // gradient bar based on background color +#define WINDOW_STYLE_TEAMCOLOR 4 // team color +#define WINDOW_STYLE_CINEMATIC 5 // cinematic + +#define MENU_TRUE 1 // uh.. true +#define MENU_FALSE 0 // and false + +#define HUD_VERTICAL 0x00 +#define HUD_HORIZONTAL 0x01 + +#define RANGETYPE_ABSOLUTE 0 +#define RANGETYPE_RELATIVE 1 + +// list box element types +#define LISTBOX_TEXT 0x00 +#define LISTBOX_IMAGE 0x01 + +// list feeders +#define FEEDER_HEADS 0x00 // model heads +#define FEEDER_MAPS 0x01 // text maps based on game type +#define FEEDER_SERVERS 0x02 // servers +#define FEEDER_CLANS 0x03 // clan names +#define FEEDER_ALLMAPS 0x04 // all maps available, in graphic format +#define FEEDER_REDTEAM_LIST 0x05 // red team members +#define FEEDER_BLUETEAM_LIST 0x06 // blue team members +#define FEEDER_PLAYER_LIST 0x07 // players +#define FEEDER_TEAM_LIST 0x08 // team members for team voting +#define FEEDER_MODS 0x09 // team members for team voting +#define FEEDER_DEMOS 0x0a // team members for team voting +#define FEEDER_SCOREBOARD 0x0b // team members for team voting +#define FEEDER_Q3HEADS 0x0c // model heads +#define FEEDER_SERVERSTATUS 0x0d // server status +#define FEEDER_FINDPLAYER 0x0e // find player +#define FEEDER_CINEMATICS 0x0f // cinematics +#define FEEDER_SAVEGAMES 0x10 // savegames +#define FEEDER_PICKSPAWN 0x11 // NERVE - SMF - wolf mp pick spawn point + +// display flags +#define CG_SHOW_BLUE_TEAM_HAS_REDFLAG 0x00000001 +#define CG_SHOW_RED_TEAM_HAS_BLUEFLAG 0x00000002 +#define CG_SHOW_ANYTEAMGAME 0x00000004 +#define CG_SHOW_HARVESTER 0x00000008 +#define CG_SHOW_ONEFLAG 0x00000010 +#define CG_SHOW_CTF 0x00000020 +#define CG_SHOW_OBELISK 0x00000040 +#define CG_SHOW_HEALTHCRITICAL 0x00000080 +#define CG_SHOW_SINGLEPLAYER 0x00000100 +#define CG_SHOW_TOURNAMENT 0x00000200 +#define CG_SHOW_DURINGINCOMINGVOICE 0x00000400 +#define CG_SHOW_IF_PLAYER_HAS_FLAG 0x00000800 +#define CG_SHOW_LANPLAYONLY 0x00001000 +#define CG_SHOW_MINED 0x00002000 +#define CG_SHOW_HEALTHOK 0x00004000 +#define CG_SHOW_TEAMINFO 0x00008000 +#define CG_SHOW_NOTEAMINFO 0x00010000 +#define CG_SHOW_OTHERTEAMHASFLAG 0x00020000 +#define CG_SHOW_YOURTEAMHASENEMYFLAG 0x00040000 +#define CG_SHOW_ANYNONTEAMGAME 0x00080000 +//(SA) +#define CG_SHOW_TEXTASINT 0x00200000 +#define CG_SHOW_HIGHLIGHTED 0x00100000 + +#define CG_SHOW_NOT_V_BINOC 0x00200000 //----(SA) added // hide on binoc huds +#define CG_SHOW_NOT_V_SNIPER 0x00400000 //----(SA) added // hide on sniper huds +#define CG_SHOW_NOT_V_SNOOPER 0x00800000 //----(SA) added // hide on snooper huds +#define CG_SHOW_NOT_V_FGSCOPE 0x01000000 //----(SA) added // hide on fg42 scope huds +#define CG_SHOW_NOT_V_CLEAR 0x02000000 //----(SA) added // hide on normal, full-view huds + +#define CG_SHOW_2DONLY 0x10000000 + + +#define UI_SHOW_LEADER 0x00000001 +#define UI_SHOW_NOTLEADER 0x00000002 +#define UI_SHOW_FAVORITESERVERS 0x00000004 +#define UI_SHOW_ANYNONTEAMGAME 0x00000008 +#define UI_SHOW_ANYTEAMGAME 0x00000010 +#define UI_SHOW_NEWHIGHSCORE 0x00000020 +#define UI_SHOW_DEMOAVAILABLE 0x00000040 +#define UI_SHOW_NEWBESTTIME 0x00000080 +#define UI_SHOW_FFA 0x00000100 +#define UI_SHOW_NOTFFA 0x00000200 +#define UI_SHOW_NETANYNONTEAMGAME 0x00000400 +#define UI_SHOW_NETANYTEAMGAME 0x00000800 +#define UI_SHOW_NOTFAVORITESERVERS 0x00001000 + +// font types +#define UI_FONT_DEFAULT 0 // auto-chose betwen big/reg/small +#define UI_FONT_NORMAL 1 +#define UI_FONT_BIG 2 +#define UI_FONT_SMALL 3 +#define UI_FONT_HANDWRITING 4 + +// owner draw types +// ideally these should be done outside of this file but +// this makes it much easier for the macro expansion to +// convert them for the designers ( from the .menu files ) +#define CG_OWNERDRAW_BASE 1 +#define CG_PLAYER_ARMOR_ICON 1 +#define CG_PLAYER_ARMOR_VALUE 2 +#define CG_PLAYER_HEAD 3 +#define CG_PLAYER_HEALTH 4 +#define CG_PLAYER_AMMO_ICON 5 +#define CG_PLAYER_AMMO_VALUE 6 +#define CG_SELECTEDPLAYER_HEAD 7 +#define CG_SELECTEDPLAYER_NAME 8 +#define CG_SELECTEDPLAYER_LOCATION 9 +#define CG_SELECTEDPLAYER_STATUS 10 +#define CG_SELECTEDPLAYER_WEAPON 11 +#define CG_SELECTEDPLAYER_POWERUP 12 + +#define CG_FLAGCARRIER_HEAD 13 +#define CG_FLAGCARRIER_NAME 14 +#define CG_FLAGCARRIER_LOCATION 15 +#define CG_FLAGCARRIER_STATUS 16 +#define CG_FLAGCARRIER_WEAPON 17 +#define CG_FLAGCARRIER_POWERUP 18 + +#define CG_PLAYER_ITEM 19 +#define CG_PLAYER_SCORE 20 + +#define CG_BLUE_FLAGHEAD 21 +#define CG_BLUE_FLAGSTATUS 22 +#define CG_BLUE_FLAGNAME 23 +#define CG_RED_FLAGHEAD 24 +#define CG_RED_FLAGSTATUS 25 +#define CG_RED_FLAGNAME 26 + +#define CG_BLUE_SCORE 27 +#define CG_RED_SCORE 28 +#define CG_RED_NAME 29 +#define CG_BLUE_NAME 30 +#define CG_HARVESTER_SKULLS 31 // only shows in harvester +#define CG_ONEFLAG_STATUS 32 // only shows in one flag +#define CG_PLAYER_LOCATION 33 +#define CG_TEAM_COLOR 34 +#define CG_CTF_POWERUP 35 + +#define CG_AREA_POWERUP 36 +#define CG_AREA_LAGOMETER 37 // painted with old system +#define CG_PLAYER_HASFLAG 38 +#define CG_GAME_TYPE 39 // not done + +#define CG_SELECTEDPLAYER_ARMOR 40 +#define CG_SELECTEDPLAYER_HEALTH 41 +#define CG_PLAYER_STATUS 42 +#define CG_FRAGGED_MSG 43 // painted with old system +#define CG_PROXMINED_MSG 44 // painted with old system +#define CG_AREA_FPSINFO 45 // painted with old system +#define CG_AREA_SYSTEMCHAT 46 // painted with old system +#define CG_AREA_TEAMCHAT 47 // painted with old system +#define CG_AREA_CHAT 48 // painted with old system +#define CG_GAME_STATUS 49 +#define CG_KILLER 50 +#define CG_PLAYER_ARMOR_ICON2D 51 +#define CG_PLAYER_AMMO_ICON2D 52 +#define CG_ACCURACY 53 +#define CG_ASSISTS 54 +#define CG_DEFEND 55 +#define CG_EXCELLENT 56 +#define CG_IMPRESSIVE 57 +#define CG_PERFECT 58 +#define CG_GAUNTLET 59 +#define CG_SPECTATORS 60 +#define CG_TEAMINFO 61 +#define CG_VOICE_HEAD 62 +#define CG_VOICE_NAME 63 +#define CG_PLAYER_HASFLAG2D 64 +#define CG_HARVESTER_SKULLS2D 65 // only shows in harvester +#define CG_CAPFRAGLIMIT 66 +#define CG_1STPLACE 67 +#define CG_2NDPLACE 68 +#define CG_CAPTURES 69 + +// (SA) adding +#define CG_PLAYER_AMMOCLIP_VALUE 70 +#define CG_PLAYER_WEAPON_ICON2D 71 +#define CG_CURSORHINT 72 +#define CG_STAMINA 73 +#define CG_PLAYER_WEAPON_HEAT 74 +#define CG_PLAYER_POWERUP 75 +#define CG_PLAYER_HOLDABLE 76 +#define CG_PLAYER_INVENTORY 77 +#define CG_AREA_WEAPON 78 // draw weapons here +#define CG_AREA_HOLDABLE 79 +#define CG_CURSORHINT_STATUS 80 // like 'health' bar when pointing at a func_explosive +#define CG_PLAYER_WEAPON_STABILITY 81 // shows aimSpreadScale value +#define CG_NEWMESSAGE 82 // 'you got mail!' //----(SA) added + +#define UI_OWNERDRAW_BASE 200 +#define UI_HANDICAP 200 +#define UI_EFFECTS 201 +#define UI_PLAYERMODEL 202 +#define UI_CLANNAME 203 +#define UI_CLANLOGO 204 +#define UI_GAMETYPE 205 +#define UI_MAPPREVIEW 206 +#define UI_SKILL 207 +#define UI_BLUETEAMNAME 208 +#define UI_REDTEAMNAME 209 +#define UI_BLUETEAM1 210 +#define UI_BLUETEAM2 211 +#define UI_BLUETEAM3 212 +#define UI_BLUETEAM4 213 +#define UI_BLUETEAM5 214 +#define UI_REDTEAM1 215 +#define UI_REDTEAM2 216 +#define UI_REDTEAM3 217 +#define UI_REDTEAM4 218 +#define UI_REDTEAM5 219 +#define UI_NETSOURCE 220 +#define UI_NETMAPPREVIEW 221 +#define UI_NETFILTER 222 +#define UI_TIER 223 +#define UI_OPPONENTMODEL 224 +#define UI_TIERMAP1 225 +#define UI_TIERMAP2 226 +#define UI_TIERMAP3 227 +#define UI_PLAYERLOGO 228 +#define UI_OPPONENTLOGO 229 +#define UI_PLAYERLOGO_METAL 230 +#define UI_OPPONENTLOGO_METAL 231 +#define UI_PLAYERLOGO_NAME 232 +#define UI_OPPONENTLOGO_NAME 233 +#define UI_TIER_MAPNAME 234 +#define UI_TIER_GAMETYPE 235 +#define UI_ALLMAPS_SELECTION 236 +#define UI_OPPONENT_NAME 237 +#define UI_VOTE_KICK 238 +#define UI_BOTNAME 239 +#define UI_BOTSKILL 240 +#define UI_REDBLUE 241 +#define UI_CROSSHAIR 242 +#define UI_SELECTEDPLAYER 243 +#define UI_MAPCINEMATIC 244 +#define UI_NETGAMETYPE 245 +#define UI_NETMAPCINEMATIC 246 +#define UI_SERVERREFRESHDATE 247 +#define UI_SERVERMOTD 248 +#define UI_GLINFO 249 +#define UI_KEYBINDSTATUS 250 +#define UI_CLANCINEMATIC 251 +#define UI_MAP_TIMETOBEAT 252 +#define UI_JOINGAMETYPE 253 +#define UI_PREVIEWCINEMATIC 254 +#define UI_STARTMAPCINEMATIC 255 +#define UI_MAPS_SELECTION 256 + +#define UI_MENUMODEL 257 +#define UI_SAVEGAME_SHOT 258 + +// NERVE - SMF +#define UI_LIMBOCHAT 259 +// -NERVE - SMF + +#define UI_LEVELSHOT 260 +#define UI_LOADSTATUSBAR 261 +#define UI_SAVEGAMENAME 262 +#define UI_SAVEGAMEINFO 263 + +#define VOICECHAT_GETFLAG "getflag" // command someone to get the flag +#define VOICECHAT_OFFENSE "offense" // command someone to go on offense +#define VOICECHAT_DEFEND "defend" // command someone to go on defense +#define VOICECHAT_DEFENDFLAG "defendflag" // command someone to defend the flag +#define VOICECHAT_PATROL "patrol" // command someone to go on patrol (roam) +#define VOICECHAT_CAMP "camp" // command someone to camp (we don't have sounds for this one) +#define VOICECHAT_FOLLOWME "followme" // command someone to follow you +#define VOICECHAT_RETURNFLAG "returnflag" // command someone to return our flag +#define VOICECHAT_FOLLOWFLAGCARRIER "followflagcarrier" // command someone to follow the flag carrier +#define VOICECHAT_YES "yes" // yes, affirmative, etc. +#define VOICECHAT_NO "no" // no, negative, etc. +#define VOICECHAT_ONGETFLAG "ongetflag" // I'm getting the flag +#define VOICECHAT_ONOFFENSE "onoffense" // I'm on offense +#define VOICECHAT_ONDEFENSE "ondefense" // I'm on defense +#define VOICECHAT_ONPATROL "onpatrol" // I'm on patrol (roaming) +#define VOICECHAT_ONCAMPING "oncamp" // I'm camping somewhere +#define VOICECHAT_ONFOLLOW "onfollow" // I'm following +#define VOICECHAT_ONFOLLOWCARRIER "onfollowcarrier" // I'm following the flag carrier +#define VOICECHAT_ONRETURNFLAG "onreturnflag" // I'm returning our flag +#define VOICECHAT_INPOSITION "inposition" // I'm in position +#define VOICECHAT_IHAVEFLAG "ihaveflag" // I have the flag +#define VOICECHAT_BASEATTACK "baseattack" // the base is under attack +#define VOICECHAT_ENEMYHASFLAG "enemyhasflag" // the enemy has our flag (CTF) +#define VOICECHAT_STARTLEADER "startleader" // I'm the leader +#define VOICECHAT_STOPLEADER "stopleader" // I resign leadership +#define VOICECHAT_WHOISLEADER "whoisleader" // who is the team leader +#define VOICECHAT_WANTONDEFENSE "wantondefense" // I want to be on defense +#define VOICECHAT_WANTONOFFENSE "wantonoffense" // I want to be on offense +#define VOICECHAT_KILLINSULT "kill_insult" // I just killed you +#define VOICECHAT_TAUNT "taunt" // I want to taunt you +#define VOICECHAT_DEATHINSULT "death_insult" // you just killed me +#define VOICECHAT_KILLGAUNTLET "kill_gauntlet" // I just killed you with the gauntlet +#define VOICECHAT_PRAISE "praise" // you did something good + +// NERVE - SMF - wolf multiplayer class/item selection mechanism +#define WM_START_SELECT 0 + +#define WM_SELECT_TEAM 1 +#define WM_SELECT_CLASS 2 +#define WM_SELECT_WEAPON 3 +#define WM_SELECT_PISTOL 4 +#define WM_SELECT_GRENADE 5 +#define WM_SELECT_ITEM1 6 + +#define WM_AXIS 1 +#define WM_ALLIES 2 +#define WM_SPECTATOR 3 + +#define WM_SOLDIER 1 +#define WM_MEDIC 2 +#define WM_LIEUTENANT 3 +#define WM_ENGINEER 4 + +#define WM_PISTOL_1911 1 +#define WM_PISTOL_LUGER 2 + +#define WM_WEAPON_MP40 3 +#define WM_WEAPON_THOMPSON 4 +#define WM_WEAPON_STEN 5 +#define WM_WEAPON_MAUSER 6 +#define WM_WEAPON_GARAND 7 +#define WM_WEAPON_PANZERFAUST 8 +#define WM_WEAPON_VENOM 9 +#define WM_WEAPON_FLAMETHROWER 10 + +#define WM_PINEAPPLE_GRENADE 11 +#define WM_STICK_GRENADE 12 +// -NERVE - SMF diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..8c2913a --- /dev/null +++ b/src/Makefile @@ -0,0 +1,3 @@ +# nasty ugly to get build system working from Anjuta +all: + cd unix && (if [ `hostname` == antares ] ; then (./pcons-2.3.1 -j4) ; else ./cons ; fi) diff --git a/src/botai/ai_chat.c b/src/botai/ai_chat.c new file mode 100644 index 0000000..58d313e --- /dev/null +++ b/src/botai/ai_chat.c @@ -0,0 +1,1313 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: ai_chat.c + * + * desc: Quake3 bot AI + * + * + *****************************************************************************/ + +#include "../game/g_local.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "../game/be_ea.h" +#include "../game/be_ai_char.h" +#include "../game/be_ai_chat.h" +#include "../game/be_ai_gen.h" +#include "../game/be_ai_goal.h" +#include "../game/be_ai_move.h" +#include "../game/be_ai_weap.h" +#include "../botai/botai.h" +// +#include "ai_main.h" +#include "ai_dmq3.h" +#include "ai_chat.h" +#include "ai_cmd.h" +#include "ai_dmnet.h" +// +#include "chars.h" //characteristics +#include "inv.h" //indexes into the inventory +#include "syn.h" //synonyms +#include "match.h" //string matching types and vars + + +/* +================== +BotNumActivePlayers +================== +*/ +int BotNumActivePlayers( void ) { + int i, num; + char buf[MAX_INFO_STRING]; + static int maxclients; + + if ( !maxclients ) { + maxclients = trap_Cvar_VariableIntegerValue( "sv_maxclients" ); + } + + num = 0; + for ( i = 0; i < maxclients && i < MAX_CLIENTS; i++ ) { + trap_GetConfigstring( CS_PLAYERS + i, buf, sizeof( buf ) ); + //if no config string or no name + if ( !strlen( buf ) || !strlen( Info_ValueForKey( buf, "n" ) ) ) { + continue; + } + //skip spectators + if ( atoi( Info_ValueForKey( buf, "t" ) ) == TEAM_SPECTATOR ) { + continue; + } + // + num++; + } + return num; +} + +/* +================== +BotIsFirstInRankings +================== +*/ +int BotIsFirstInRankings( bot_state_t *bs ) { + int i, score; + char buf[MAX_INFO_STRING]; + static int maxclients; + playerState_t ps; + + if ( !maxclients ) { + maxclients = trap_Cvar_VariableIntegerValue( "sv_maxclients" ); + } + + score = bs->cur_ps.persistant[PERS_SCORE]; + for ( i = 0; i < maxclients && i < MAX_CLIENTS; i++ ) { + trap_GetConfigstring( CS_PLAYERS + i, buf, sizeof( buf ) ); + //if no config string or no name + if ( !strlen( buf ) || !strlen( Info_ValueForKey( buf, "n" ) ) ) { + continue; + } + //skip spectators + if ( atoi( Info_ValueForKey( buf, "t" ) ) == TEAM_SPECTATOR ) { + continue; + } + // + BotAI_GetClientState( i, &ps ); + if ( score < ps.persistant[PERS_SCORE] ) { + return qfalse; + } + } + return qtrue; +} + +/* +================== +BotIsLastInRankings +================== +*/ +int BotIsLastInRankings( bot_state_t *bs ) { + int i, score; + char buf[MAX_INFO_STRING]; + static int maxclients; + playerState_t ps; + + if ( !maxclients ) { + maxclients = trap_Cvar_VariableIntegerValue( "sv_maxclients" ); + } + + score = bs->cur_ps.persistant[PERS_SCORE]; + for ( i = 0; i < maxclients && i < MAX_CLIENTS; i++ ) { + trap_GetConfigstring( CS_PLAYERS + i, buf, sizeof( buf ) ); + //if no config string or no name + if ( !strlen( buf ) || !strlen( Info_ValueForKey( buf, "n" ) ) ) { + continue; + } + //skip spectators + if ( atoi( Info_ValueForKey( buf, "t" ) ) == TEAM_SPECTATOR ) { + continue; + } + // + BotAI_GetClientState( i, &ps ); + if ( score > ps.persistant[PERS_SCORE] ) { + return qfalse; + } + } + return qtrue; +} + +/* +================== +BotFirstClientInRankings +================== +*/ +char *BotFirstClientInRankings( void ) { + int i, bestscore, bestclient; + char buf[MAX_INFO_STRING]; + static char name[32]; + static int maxclients; + playerState_t ps; + + if ( !maxclients ) { + maxclients = trap_Cvar_VariableIntegerValue( "sv_maxclients" ); + } + + bestscore = -999999; + bestclient = 0; + for ( i = 0; i < maxclients && i < MAX_CLIENTS; i++ ) { + trap_GetConfigstring( CS_PLAYERS + i, buf, sizeof( buf ) ); + //if no config string or no name + if ( !strlen( buf ) || !strlen( Info_ValueForKey( buf, "n" ) ) ) { + continue; + } + //skip spectators + if ( atoi( Info_ValueForKey( buf, "t" ) ) == TEAM_SPECTATOR ) { + continue; + } + // + BotAI_GetClientState( i, &ps ); + if ( ps.persistant[PERS_SCORE] > bestscore ) { + bestscore = ps.persistant[PERS_SCORE]; + bestclient = i; + } + } + EasyClientName( bestclient, name, 32 ); + return name; +} + +/* +================== +BotLastClientInRankings +================== +*/ +char *BotLastClientInRankings( void ) { + int i, worstscore, bestclient; + char buf[MAX_INFO_STRING]; + static char name[32]; + static int maxclients; + playerState_t ps; + + if ( !maxclients ) { + maxclients = trap_Cvar_VariableIntegerValue( "sv_maxclients" ); + } + + worstscore = 999999; + bestclient = 0; + for ( i = 0; i < maxclients && i < MAX_CLIENTS; i++ ) { + trap_GetConfigstring( CS_PLAYERS + i, buf, sizeof( buf ) ); + //if no config string or no name + if ( !strlen( buf ) || !strlen( Info_ValueForKey( buf, "n" ) ) ) { + continue; + } + //skip spectators + if ( atoi( Info_ValueForKey( buf, "t" ) ) == TEAM_SPECTATOR ) { + continue; + } + // + BotAI_GetClientState( i, &ps ); + if ( ps.persistant[PERS_SCORE] < worstscore ) { + worstscore = ps.persistant[PERS_SCORE]; + bestclient = i; + } + } + EasyClientName( bestclient, name, 32 ); + return name; +} + +/* +================== +BotRandomOpponentName +================== +*/ +char *BotRandomOpponentName( bot_state_t *bs ) { + int i, count; + char buf[MAX_INFO_STRING]; + int opponents[MAX_CLIENTS], numopponents; + static int maxclients; + static char name[32]; + + if ( !maxclients ) { + maxclients = trap_Cvar_VariableIntegerValue( "sv_maxclients" ); + } + + numopponents = 0; + opponents[0] = 0; + for ( i = 0; i < maxclients && i < MAX_CLIENTS; i++ ) { + if ( i == bs->client ) { + continue; + } + // + trap_GetConfigstring( CS_PLAYERS + i, buf, sizeof( buf ) ); + //if no config string or no name + if ( !strlen( buf ) || !strlen( Info_ValueForKey( buf, "n" ) ) ) { + continue; + } + //skip spectators + if ( atoi( Info_ValueForKey( buf, "t" ) ) == TEAM_SPECTATOR ) { + continue; + } + //skip team mates + if ( BotSameTeam( bs, i ) ) { + continue; + } + // + opponents[numopponents] = i; + numopponents++; + } + count = random() * numopponents; + for ( i = 0; i < numopponents; i++ ) { + count--; + if ( count <= 0 ) { + EasyClientName( opponents[i], name, sizeof( name ) ); + return name; + } + } + EasyClientName( opponents[0], name, sizeof( name ) ); + return name; +} + +/* +================== +BotMapTitle +================== +*/ + +char *BotMapTitle( void ) { + char info[1024]; + static char mapname[128]; + + trap_GetServerinfo( info, sizeof( info ) ); + + strncpy( mapname, Info_ValueForKey( info, "mapname" ), sizeof( mapname ) - 1 ); + mapname[sizeof( mapname ) - 1] = '\0'; + + return mapname; +} + + +/* +================== +BotWeaponNameForMeansOfDeath +================== +*/ + +char *BotWeaponNameForMeansOfDeath( int mod ) { + switch ( mod ) { + case MOD_SHOTGUN: return "Shotgun"; + case MOD_GAUNTLET: return "Gauntlet"; + case MOD_MACHINEGUN: return "Machinegun"; + case MOD_GRENADE: + case MOD_GRENADE_SPLASH: return "Grenade Launcher"; + case MOD_ROCKET: + case MOD_ROCKET_SPLASH: return "Rocket Launcher"; + case MOD_RAILGUN: return "Railgun"; + case MOD_LIGHTNING: return "Lightning Gun"; + case MOD_BFG: + case MOD_BFG_SPLASH: return "BFG10K"; + case MOD_GRAPPLE: return "Grapple"; + default: return "[unknown weapon]"; + } +} + +/* +================== +BotRandomWeaponName +================== +*/ +char *BotRandomWeaponName( void ) { + int rnd; + + rnd = random() * 8.9; + switch ( rnd ) { + case 0: return "Gauntlet"; + case 1: return "Shotgun"; + case 2: return "Machinegun"; + case 3: return "Grenade Launcher"; + case 4: return "Rocket Launcher"; + case 5: return "Plasmagun"; + case 6: return "Railgun"; + case 7: return "Lightning Gun"; + default: return "BFG10K"; + } +} + +/* +================== +BotValidChatPosition +================== +*/ +int BotValidChatPosition( bot_state_t *bs ) { + vec3_t point, start, end, mins, maxs; + bsp_trace_t trace; + + //if the bot is dead all positions are valid + if ( BotIsDead( bs ) ) { + return qtrue; + } + //must be on the ground + //if (bs->cur_ps.groundEntityNum != ENTITYNUM_NONE) return qfalse; + //do not chat if in lava or slime + VectorCopy( bs->origin, point ); + point[2] -= 24; + if ( trap_PointContents( point,bs->entitynum ) & ( CONTENTS_LAVA | CONTENTS_SLIME ) ) { + return qfalse; + } + //do not chat if under water + VectorCopy( bs->origin, point ); + point[2] += 32; + if ( trap_PointContents( point,bs->entitynum ) & MASK_WATER ) { + return qfalse; + } + //must be standing on the world entity + VectorCopy( bs->origin, start ); + VectorCopy( bs->origin, end ); + start[2] += 1; + end[2] -= 10; + trap_AAS_PresenceTypeBoundingBox( PRESENCE_CROUCH, mins, maxs ); + BotAI_Trace( &trace, start, mins, maxs, end, bs->client, MASK_SOLID ); + if ( trace.ent != ENTITYNUM_WORLD ) { + return qfalse; + } + //the bot is in a position where it can chat + return qtrue; +} + +/* +================== +BotChat_EnterGame +================== +*/ +int BotChat_EnterGame( bot_state_t *bs ) { + char name[32]; + float rnd; + + if ( bot_nochat.integer ) { + return qfalse; + } + if ( bs->lastchat_time > trap_AAS_Time() - 3 ) { + return qfalse; + } + //don't chat in teamplay + if ( TeamPlayIsOn() ) { + return qfalse; + } + rnd = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_CHAT_ENTEREXITGAME, 0, 1 ); + if ( !bot_fastchat.integer ) { + if ( random() > rnd ) { + return qfalse; + } + } + if ( BotNumActivePlayers() <= 1 ) { + return qfalse; + } + if ( !BotValidChatPosition( bs ) ) { + return qfalse; + } + BotAI_BotInitialChat( bs, "game_enter", + EasyClientName( bs->client, name, 32 ), // 0 + BotRandomOpponentName( bs ), // 1 + "[invalid var]", // 2 + "[invalid var]", // 3 + BotMapTitle(), // 4 + NULL ); + bs->lastchat_time = trap_AAS_Time(); + bs->chatto = CHAT_ALL; + return qtrue; +} + +/* +================== +BotChat_ExitGame +================== +*/ +int BotChat_ExitGame( bot_state_t *bs ) { + char name[32]; + float rnd; + + if ( bot_nochat.integer ) { + return qfalse; + } + if ( bs->lastchat_time > trap_AAS_Time() - 3 ) { + return qfalse; + } + //don't chat in teamplay + if ( TeamPlayIsOn() ) { + return qfalse; + } + rnd = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_CHAT_ENTEREXITGAME, 0, 1 ); + if ( !bot_fastchat.integer ) { + if ( random() > rnd ) { + return qfalse; + } + } + if ( BotNumActivePlayers() <= 1 ) { + return qfalse; + } + // + BotAI_BotInitialChat( bs, "game_exit", + EasyClientName( bs->client, name, 32 ), // 0 + BotRandomOpponentName( bs ), // 1 + "[invalid var]", // 2 + "[invalid var]", // 3 + BotMapTitle(), // 4 + NULL ); + bs->lastchat_time = trap_AAS_Time(); + bs->chatto = CHAT_ALL; + return qtrue; +} + +/* +================== +BotChat_StartLevel +================== +*/ +int BotChat_StartLevel( bot_state_t *bs ) { + char name[32]; + float rnd; + + if ( bot_nochat.integer ) { + return qfalse; + } + if ( BotIsObserver( bs ) ) { + return qfalse; + } + if ( bs->lastchat_time > trap_AAS_Time() - 3 ) { + return qfalse; + } + //don't chat in teamplay + if ( TeamPlayIsOn() ) { + return qfalse; + } + rnd = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_CHAT_STARTENDLEVEL, 0, 1 ); + if ( !bot_fastchat.integer ) { + if ( random() > rnd ) { + return qfalse; + } + } + if ( BotNumActivePlayers() <= 1 ) { + return qfalse; + } + BotAI_BotInitialChat( bs, "level_start", + EasyClientName( bs->client, name, 32 ), // 0 + NULL ); + bs->lastchat_time = trap_AAS_Time(); + bs->chatto = CHAT_ALL; + return qtrue; +} + +/* +================== +BotChat_EndLevel +================== +*/ +int BotChat_EndLevel( bot_state_t *bs ) { + char name[32]; + float rnd; + + if ( bot_nochat.integer ) { + return qfalse; + } + if ( BotIsObserver( bs ) ) { + return qfalse; + } + if ( bs->lastchat_time > trap_AAS_Time() - 3 ) { + return qfalse; + } + //don't chat in teamplay + if ( TeamPlayIsOn() ) { + return qfalse; + } + rnd = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_CHAT_STARTENDLEVEL, 0, 1 ); + if ( !bot_fastchat.integer ) { + if ( random() > rnd ) { + return qfalse; + } + } + if ( BotNumActivePlayers() <= 1 ) { + return qfalse; + } + // + if ( BotIsFirstInRankings( bs ) ) { + BotAI_BotInitialChat( bs, "level_end_victory", + EasyClientName( bs->client, name, 32 ), // 0 + BotRandomOpponentName( bs ), // 1 + "[invalid var]", // 2 + BotLastClientInRankings(), // 3 + BotMapTitle(), // 4 + NULL ); + } else if ( BotIsLastInRankings( bs ) ) { + BotAI_BotInitialChat( bs, "level_end_lose", + EasyClientName( bs->client, name, 32 ), // 0 + BotRandomOpponentName( bs ), // 1 + BotFirstClientInRankings(), // 2 + "[invalid var]", // 3 + BotMapTitle(), // 4 + NULL ); + } else { + BotAI_BotInitialChat( bs, "level_end", + EasyClientName( bs->client, name, 32 ), // 0 + BotRandomOpponentName( bs ), // 1 + BotFirstClientInRankings(), // 2 + BotLastClientInRankings(), // 3 + BotMapTitle(), // 4 + NULL ); + } + bs->lastchat_time = trap_AAS_Time(); + bs->chatto = CHAT_ALL; + return qtrue; +} + +/* +================== +BotChat_Death +================== +*/ +int BotChat_Death( bot_state_t *bs ) { + char name[32]; + float rnd; + + if ( bot_nochat.integer ) { + return qfalse; + } + if ( bs->lastchat_time > trap_AAS_Time() - 3 ) { + return qfalse; + } + rnd = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_CHAT_DEATH, 0, 1 ); + //if fast chatting is off + if ( !bot_fastchat.integer ) { + if ( random() > rnd ) { + return qfalse; + } + } + if ( BotNumActivePlayers() <= 1 ) { + return qfalse; + } + // + if ( bs->lastkilledby >= 0 && bs->lastkilledby < MAX_CLIENTS ) { + EasyClientName( bs->lastkilledby, name, 32 ); + } else { + strcpy( name, "[world]" ); + } + // + if ( TeamPlayIsOn() && BotSameTeam( bs, bs->lastkilledby ) ) { + if ( bs->lastkilledby == bs->client ) { + return qfalse; + } + BotAI_BotInitialChat( bs, "death_teammate", name, NULL ); + bs->chatto = CHAT_TEAM; + } else + { + //don't chat in teamplay + if ( TeamPlayIsOn() ) { + return qfalse; + } + // + if ( bs->botdeathtype == MOD_WATER ) { + BotAI_BotInitialChat( bs, "death_drown", BotRandomOpponentName( bs ), NULL ); + } else if ( bs->botdeathtype == MOD_SLIME ) { + BotAI_BotInitialChat( bs, "death_slime", BotRandomOpponentName( bs ), NULL ); + } else if ( bs->botdeathtype == MOD_LAVA ) { + BotAI_BotInitialChat( bs, "death_lava", BotRandomOpponentName( bs ), NULL ); + } else if ( bs->botdeathtype == MOD_FALLING ) { + BotAI_BotInitialChat( bs, "death_cratered", BotRandomOpponentName( bs ), NULL ); + } else if ( bs->botsuicide || //all other suicides by own weapon + bs->botdeathtype == MOD_CRUSH || + bs->botdeathtype == MOD_SUICIDE || + bs->botdeathtype == MOD_TARGET_LASER || + bs->botdeathtype == MOD_TRIGGER_HURT || + bs->botdeathtype == MOD_UNKNOWN ) { + BotAI_BotInitialChat( bs, "death_suicide", BotRandomOpponentName( bs ), NULL ); + } else if ( bs->botdeathtype == MOD_TELEFRAG ) { + BotAI_BotInitialChat( bs, "death_telefrag", name, NULL ); + } else { + if ( ( bs->botdeathtype == MOD_GAUNTLET || + bs->botdeathtype == MOD_RAILGUN || + bs->botdeathtype == MOD_BFG || + bs->botdeathtype == MOD_BFG_SPLASH ) && random() < 0.5 ) { + + if ( bs->botdeathtype == MOD_GAUNTLET ) { + BotAI_BotInitialChat( bs, "death_gauntlet", + name, // 0 + BotWeaponNameForMeansOfDeath( bs->botdeathtype ), // 1 + NULL ); + } else if ( bs->botdeathtype == MOD_RAILGUN ) { + BotAI_BotInitialChat( bs, "death_rail", + name, // 0 + BotWeaponNameForMeansOfDeath( bs->botdeathtype ), // 1 + NULL ); + } else { + BotAI_BotInitialChat( bs, "death_bfg", + name, // 0 + BotWeaponNameForMeansOfDeath( bs->botdeathtype ), // 1 + NULL ); + } + } + //choose between insult and praise + else if ( random() < trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_CHAT_INSULT, 0, 1 ) ) { + BotAI_BotInitialChat( bs, "death_insult", + name, // 0 + BotWeaponNameForMeansOfDeath( bs->botdeathtype ), // 1 + NULL ); + } else { + BotAI_BotInitialChat( bs, "death_praise", + name, // 0 + BotWeaponNameForMeansOfDeath( bs->botdeathtype ), // 1 + NULL ); + } + } + bs->chatto = CHAT_ALL; + } + bs->lastchat_time = trap_AAS_Time(); + return qtrue; +} + +/* +================== +BotChat_Kill +================== +*/ +int BotChat_Kill( bot_state_t *bs ) { + char name[32]; + float rnd; + + if ( bot_nochat.integer ) { + return qfalse; + } + if ( bs->lastchat_time > trap_AAS_Time() - 3 ) { + return qfalse; + } + rnd = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_CHAT_KILL, 0, 1 ); + //if fast chat is off + if ( !bot_fastchat.integer ) { + if ( random() > rnd ) { + return qfalse; + } + } + if ( bs->lastkilledplayer == bs->client ) { + return qfalse; + } + if ( BotNumActivePlayers() <= 1 ) { + return qfalse; + } + if ( !BotValidChatPosition( bs ) ) { + return qfalse; + } + // + EasyClientName( bs->lastkilledplayer, name, 32 ); + // + bs->chatto = CHAT_ALL; + if ( TeamPlayIsOn() && BotSameTeam( bs, bs->lastkilledplayer ) ) { + BotAI_BotInitialChat( bs, "kill_teammate", name, NULL ); + bs->chatto = CHAT_TEAM; + } else + { + //don't chat in teamplay + if ( TeamPlayIsOn() ) { + return qfalse; + } + // + if ( bs->enemydeathtype == MOD_GAUNTLET ) { + BotAI_BotInitialChat( bs, "kill_gauntlet", name, NULL ); + } else if ( bs->enemydeathtype == MOD_RAILGUN ) { + BotAI_BotInitialChat( bs, "kill_rail", name, NULL ); + } else if ( bs->enemydeathtype == MOD_TELEFRAG ) { + BotAI_BotInitialChat( bs, "kill_telefrag", name, NULL ); + } + //choose between insult and praise + else if ( random() < trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_CHAT_INSULT, 0, 1 ) ) { + BotAI_BotInitialChat( bs, "kill_insult", name, NULL ); + } else { + BotAI_BotInitialChat( bs, "kill_praise", name, NULL ); + } + } + bs->lastchat_time = trap_AAS_Time(); + return qtrue; +} + +/* +================== +BotChat_EnemySuicide +================== +*/ +int BotChat_EnemySuicide( bot_state_t *bs ) { + char name[32]; + float rnd; + + if ( bot_nochat.integer ) { + return qfalse; + } + if ( bs->lastchat_time > trap_AAS_Time() - 3 ) { + return qfalse; + } + if ( BotNumActivePlayers() <= 1 ) { + return qfalse; + } + // + rnd = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_CHAT_KILL, 0, 1 ); + //don't chat in teamplay + if ( TeamPlayIsOn() ) { + return qfalse; + } + //if fast chat is off + if ( !bot_fastchat.integer ) { + if ( random() > rnd ) { + return qfalse; + } + } + if ( !BotValidChatPosition( bs ) ) { + return qfalse; + } + // + if ( bs->enemy >= 0 ) { + EasyClientName( bs->enemy, name, 32 ); + } else { strcpy( name, "" );} + BotAI_BotInitialChat( bs, "enemy_suicide", name, NULL ); + bs->lastchat_time = trap_AAS_Time(); + bs->chatto = CHAT_ALL; + return qtrue; +} + +/* +================== +BotChat_HitTalking +================== +*/ +int BotChat_HitTalking( bot_state_t *bs ) { + char name[32], *weap; + int lasthurt_client; + float rnd; + + if ( bot_nochat.integer ) { + return qfalse; + } + if ( bs->lastchat_time > trap_AAS_Time() - 3 ) { + return qfalse; + } + if ( BotNumActivePlayers() <= 1 ) { + return qfalse; + } + lasthurt_client = g_entities[bs->client].client->lasthurt_client; + if ( !lasthurt_client ) { + return qfalse; + } + if ( lasthurt_client == bs->client ) { + return qfalse; + } + // + if ( lasthurt_client < 0 || lasthurt_client >= MAX_CLIENTS ) { + return qfalse; + } + // + rnd = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_CHAT_HITTALKING, 0, 1 ); + //don't chat in teamplay + if ( TeamPlayIsOn() ) { + return qfalse; + } + //if fast chat is off + if ( !bot_fastchat.integer ) { + if ( random() > rnd * 0.5 ) { + return qfalse; + } + } + if ( !BotValidChatPosition( bs ) ) { + return qfalse; + } + // + ClientName( g_entities[bs->client].client->lasthurt_client, name, sizeof( name ) ); + weap = BotWeaponNameForMeansOfDeath( g_entities[bs->client].client->lasthurt_client ); + // + BotAI_BotInitialChat( bs, "hit_talking", name, weap, NULL ); + bs->lastchat_time = trap_AAS_Time(); + bs->chatto = CHAT_ALL; + return qtrue; +} + +/* +================== +BotChat_HitNoDeath +================== +*/ +int BotChat_HitNoDeath( bot_state_t *bs ) { + char name[32], *weap; + float rnd; + int lasthurt_client; + aas_entityinfo_t entinfo; + + lasthurt_client = g_entities[bs->client].client->lasthurt_client; + if ( !lasthurt_client ) { + return qfalse; + } + if ( lasthurt_client == bs->client ) { + return qfalse; + } + // + if ( lasthurt_client < 0 || lasthurt_client >= MAX_CLIENTS ) { + return qfalse; + } + // + if ( bot_nochat.integer ) { + return qfalse; + } + if ( bs->lastchat_time > trap_AAS_Time() - 3 ) { + return qfalse; + } + if ( BotNumActivePlayers() <= 1 ) { + return qfalse; + } + rnd = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_CHAT_HITNODEATH, 0, 1 ); + //don't chat in teamplay + if ( TeamPlayIsOn() ) { + return qfalse; + } + //if fast chat is off + if ( !bot_fastchat.integer ) { + if ( random() > rnd * 0.5 ) { + return qfalse; + } + } + if ( !BotValidChatPosition( bs ) ) { + return qfalse; + } + //if the enemy is visible + if ( BotEntityVisible( bs->client, bs->eye, bs->viewangles, 360, bs->enemy ) ) { + return qfalse; + } + // + BotEntityInfo( bs->enemy, &entinfo ); + if ( EntityIsShooting( &entinfo ) ) { + return qfalse; + } + // + ClientName( lasthurt_client, name, sizeof( name ) ); + weap = BotWeaponNameForMeansOfDeath( g_entities[bs->client].client->lasthurt_mod ); + // + BotAI_BotInitialChat( bs, "hit_nodeath", name, weap, NULL ); + bs->lastchat_time = trap_AAS_Time(); + bs->chatto = CHAT_ALL; + return qtrue; +} + +/* +================== +BotChat_HitNoKill +================== +*/ +int BotChat_HitNoKill( bot_state_t *bs ) { + char name[32], *weap; + float rnd; + aas_entityinfo_t entinfo; + + if ( bot_nochat.integer ) { + return qfalse; + } + if ( bs->lastchat_time > trap_AAS_Time() - 3 ) { + return qfalse; + } + if ( BotNumActivePlayers() <= 1 ) { + return qfalse; + } + rnd = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_CHAT_HITNOKILL, 0, 1 ); + //don't chat in teamplay + if ( TeamPlayIsOn() ) { + return qfalse; + } + //if fast chat is off + if ( !bot_fastchat.integer ) { + if ( random() > rnd * 0.5 ) { + return qfalse; + } + } + if ( !BotValidChatPosition( bs ) ) { + return qfalse; + } + //if the enemy is visible + if ( BotEntityVisible( bs->client, bs->eye, bs->viewangles, 360, bs->enemy ) ) { + return qfalse; + } + // + BotEntityInfo( bs->enemy, &entinfo ); + if ( EntityIsShooting( &entinfo ) ) { + return qfalse; + } + // + ClientName( bs->enemy, name, sizeof( name ) ); + weap = BotWeaponNameForMeansOfDeath( g_entities[bs->enemy].client->lasthurt_mod ); + // + BotAI_BotInitialChat( bs, "hit_nokill", name, weap, NULL ); + bs->lastchat_time = trap_AAS_Time(); + bs->chatto = CHAT_ALL; + return qtrue; +} + +/* +================== +BotChat_Random +================== +*/ +int BotChat_Random( bot_state_t *bs ) { + float rnd; + char name[32]; + + if ( bot_nochat.integer ) { + return qfalse; + } + if ( BotIsObserver( bs ) ) { + return qfalse; + } + if ( bs->lastchat_time > trap_AAS_Time() - 3 ) { + return qfalse; + } + //don't chat in teamplay + if ( TeamPlayIsOn() ) { + return qfalse; + } + //don't chat when doing something important :) + if ( bs->ltgtype == LTG_TEAMHELP || + bs->ltgtype == LTG_TEAMACCOMPANY || + bs->ltgtype == LTG_RUSHBASE ) { + return qfalse; + } + // + rnd = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_CHAT_RANDOM, 0, 1 ); + if ( random() > bs->thinktime * 0.1 ) { + return qfalse; + } + if ( !bot_fastchat.integer ) { + if ( random() > rnd ) { + return qfalse; + } + if ( random() > 0.25 ) { + return qfalse; + } + } + if ( BotNumActivePlayers() <= 1 ) { + return qfalse; + } + if ( !BotValidChatPosition( bs ) ) { + return qfalse; + } + // + if ( bs->lastkilledplayer == bs->client ) { + strcpy( name, BotRandomOpponentName( bs ) ); + } else { + EasyClientName( bs->lastkilledplayer, name, sizeof( name ) ); + } + // + if ( random() < trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_CHAT_MISC, 0, 1 ) ) { + BotAI_BotInitialChat( bs, "random_misc", + BotRandomOpponentName( bs ), // 0 + name, // 1 + "[invalid var]", // 2 + "[invalid var]", // 3 + BotMapTitle(), // 4 + BotRandomWeaponName(), // 5 + NULL ); + } else { + BotAI_BotInitialChat( bs, "random_insult", + BotRandomOpponentName( bs ), // 0 + name, // 1 + "[invalid var]", // 2 + "[invalid var]", // 3 + BotMapTitle(), // 4 + BotRandomWeaponName(), // 5 + NULL ); + } + bs->lastchat_time = trap_AAS_Time(); + bs->chatto = CHAT_ALL; + return qtrue; +} + +/* +================== +BotChatTime +================== +*/ +float BotChatTime( bot_state_t *bs ) { + int cpm; + + cpm = trap_Characteristic_BInteger( bs->character, CHARACTERISTIC_CHAT_CPM, 1, 4000 ); + + return 2.0; //(float) trap_BotChatLength(bs->cs) * 30 / cpm; +} + +/* +================== +BotChatTest +================== +*/ +void BotChatTest( bot_state_t *bs ) { + + char name[32]; + char *weap; + int num, i; + + num = trap_BotNumInitialChats( bs->cs, "game_enter" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "game_enter", + EasyClientName( bs->client, name, 32 ), // 0 + BotRandomOpponentName( bs ), // 1 + "[invalid var]", // 2 + "[invalid var]", // 3 + BotMapTitle(), // 4 + NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "game_exit" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "game_exit", + EasyClientName( bs->client, name, 32 ), // 0 + BotRandomOpponentName( bs ), // 1 + "[invalid var]", // 2 + "[invalid var]", // 3 + BotMapTitle(), // 4 + NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "level_start" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "level_start", + EasyClientName( bs->client, name, 32 ), // 0 + NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "level_end_victory" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "level_end_victory", + EasyClientName( bs->client, name, 32 ), // 0 + BotRandomOpponentName( bs ), // 1 + BotFirstClientInRankings(), // 2 + BotLastClientInRankings(), // 3 + BotMapTitle(), // 4 + NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "level_end_lose" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "level_end_lose", + EasyClientName( bs->client, name, 32 ), // 0 + BotRandomOpponentName( bs ), // 1 + BotFirstClientInRankings(), // 2 + BotLastClientInRankings(), // 3 + BotMapTitle(), // 4 + NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "level_end" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "level_end", + EasyClientName( bs->client, name, 32 ), // 0 + BotRandomOpponentName( bs ), // 1 + BotFirstClientInRankings(), // 2 + BotLastClientInRankings(), // 3 + BotMapTitle(), // 4 + NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + EasyClientName( bs->lastkilledby, name, sizeof( name ) ); + num = trap_BotNumInitialChats( bs->cs, "death_drown" ); + for ( i = 0; i < num; i++ ) + { + // + BotAI_BotInitialChat( bs, "death_drown", name, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "death_slime" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "death_slime", name, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "death_lava" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "death_lava", name, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "death_cratered" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "death_cratered", name, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "death_suicide" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "death_suicide", name, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "death_telefrag" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "death_telefrag", name, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "death_gauntlet" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "death_gauntlet", + name, // 0 + BotWeaponNameForMeansOfDeath( bs->botdeathtype ), // 1 + NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "death_rail" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "death_rail", + name, // 0 + BotWeaponNameForMeansOfDeath( bs->botdeathtype ), // 1 + NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "death_bfg" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "death_bfg", + name, // 0 + BotWeaponNameForMeansOfDeath( bs->botdeathtype ), // 1 + NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "death_insult" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "death_insult", + name, // 0 + BotWeaponNameForMeansOfDeath( bs->botdeathtype ), // 1 + NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "death_praise" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "death_praise", + name, // 0 + BotWeaponNameForMeansOfDeath( bs->botdeathtype ), // 1 + NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + // + EasyClientName( bs->lastkilledplayer, name, 32 ); + // + num = trap_BotNumInitialChats( bs->cs, "kill_gauntlet" ); + for ( i = 0; i < num; i++ ) + { + // + BotAI_BotInitialChat( bs, "kill_gauntlet", name, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "kill_rail" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "kill_rail", name, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "kill_telefrag" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "kill_telefrag", name, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "kill_insult" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "kill_insult", name, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "kill_praise" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "kill_praise", name, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "enemy_suicide" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "enemy_suicide", name, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + ClientName( g_entities[bs->client].client->lasthurt_client, name, sizeof( name ) ); + weap = BotWeaponNameForMeansOfDeath( g_entities[bs->client].client->lasthurt_client ); + num = trap_BotNumInitialChats( bs->cs, "hit_talking" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "hit_talking", name, weap, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "hit_nodeath" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "hit_nodeath", name, weap, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "hit_nokill" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "hit_nokill", name, weap, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + // + if ( bs->lastkilledplayer == bs->client ) { + strcpy( name, BotRandomOpponentName( bs ) ); + } else { + EasyClientName( bs->lastkilledplayer, name, sizeof( name ) ); + } + // + num = trap_BotNumInitialChats( bs->cs, "random_misc" ); + for ( i = 0; i < num; i++ ) + { + // + BotAI_BotInitialChat( bs, "random_misc", + BotRandomOpponentName( bs ), // 0 + name, // 1 + "[invalid var]", // 2 + "[invalid var]", // 3 + BotMapTitle(), // 4 + BotRandomWeaponName(), // 5 + NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + num = trap_BotNumInitialChats( bs->cs, "random_insult" ); + for ( i = 0; i < num; i++ ) + { + BotAI_BotInitialChat( bs, "random_insult", + BotRandomOpponentName( bs ), // 0 + name, // 1 + "[invalid var]", // 2 + "[invalid var]", // 3 + BotMapTitle(), // 4 + BotRandomWeaponName(), // 5 + NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } +} diff --git a/src/botai/ai_chat.h b/src/botai/ai_chat.h new file mode 100644 index 0000000..1b97caf --- /dev/null +++ b/src/botai/ai_chat.h @@ -0,0 +1,66 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: ai_chat.h + * + * desc: Quake3 bot AI + * + * + *****************************************************************************/ + +// +int BotChat_EnterGame( bot_state_t *bs ); +// +int BotChat_ExitGame( bot_state_t *bs ); +// +int BotChat_StartLevel( bot_state_t *bs ); +// +int BotChat_EndLevel( bot_state_t *bs ); +// +int BotChat_HitTalking( bot_state_t *bs ); +// +int BotChat_HitNoDeath( bot_state_t *bs ); +// +int BotChat_HitNoKill( bot_state_t *bs ); +// +int BotChat_Death( bot_state_t *bs ); +// +int BotChat_Kill( bot_state_t *bs ); +// +int BotChat_EnemySuicide( bot_state_t *bs ); +// +int BotChat_Random( bot_state_t *bs ); +// time the selected chat takes to type in +float BotChatTime( bot_state_t *bs ); +// returns true if the bot can chat at the current position +int BotValidChatPosition( bot_state_t *bs ); +// test the initial bot chats +void BotChatTest( bot_state_t *bs ); + diff --git a/src/botai/ai_cmd.c b/src/botai/ai_cmd.c new file mode 100644 index 0000000..2ddd5d8 --- /dev/null +++ b/src/botai/ai_cmd.c @@ -0,0 +1,1645 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: ai_cmd.c + * + * desc: Quake3 bot AI + * + * + *****************************************************************************/ + +#include "../game/g_local.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "../game/be_ea.h" +#include "../game/be_ai_char.h" +#include "../game/be_ai_chat.h" +#include "../game/be_ai_gen.h" +#include "../game/be_ai_goal.h" +#include "../game/be_ai_move.h" +#include "../game/be_ai_weap.h" +#include "../botai/botai.h" +// +#include "ai_main.h" +#include "ai_dmq3.h" +#include "ai_chat.h" +#include "ai_cmd.h" +#include "ai_dmnet.h" +// +#include "chars.h" //characteristics +#include "inv.h" //indexes into the inventory +#include "syn.h" //synonyms +#include "match.h" //string matching types and vars + + +#ifdef DEBUG +/* +================== +BotPrintTeamGoal +================== +*/ +void BotPrintTeamGoal( bot_state_t *bs ) { + char netname[MAX_NETNAME]; + float t; + + ClientName( bs->client, netname, sizeof( netname ) ); + t = bs->teamgoal_time - trap_AAS_Time(); + switch ( bs->ltgtype ) { + case LTG_TEAMHELP: + { + BotAI_Print( PRT_MESSAGE, "%s: I'm gonna help a team mate for %1.0f secs\n", netname, t ); + break; + } + case LTG_TEAMACCOMPANY: + { + BotAI_Print( PRT_MESSAGE, "%s: I'm gonna accompany a team mate for %1.0f secs\n", netname, t ); + break; + } + case LTG_GETFLAG: + { + BotAI_Print( PRT_MESSAGE, "%s: I'm gonna get the flag for %1.0f secs\n", netname, t ); + break; + } + case LTG_RUSHBASE: + { + BotAI_Print( PRT_MESSAGE, "%s: I'm gonna rush to the base for %1.0f secs\n", netname, t ); + break; + } + case LTG_RETURNFLAG: + { + BotAI_Print( PRT_MESSAGE, "%s: I'm gonna try to return the flag for %1.0f secs\n", netname, t ); + break; + } + case LTG_DEFENDKEYAREA: + { + BotAI_Print( PRT_MESSAGE, "%s: I'm gonna defend a key area for %1.0f secs\n", netname, t ); + break; + } + case LTG_GETITEM: + { + BotAI_Print( PRT_MESSAGE, "%s: I'm gonna get an item for %1.0f secs\n", netname, t ); + break; + } + case LTG_KILL: + { + BotAI_Print( PRT_MESSAGE, "%s: I'm gonna kill someone for %1.0f secs\n", netname, t ); + break; + } + case LTG_CAMP: + case LTG_CAMPORDER: + { + BotAI_Print( PRT_MESSAGE, "%s: I'm gonna camp for %1.0f secs\n", netname, t ); + break; + } + case LTG_PATROL: + { + BotAI_Print( PRT_MESSAGE, "%s: I'm gonna patrol for %1.0f secs\n", netname, t ); + break; + } + default: + { + if ( bs->ctfroam_time > trap_AAS_Time() ) { + t = bs->ctfroam_time - trap_AAS_Time(); + BotAI_Print( PRT_MESSAGE, "%s: I'm gonna roam for %1.0f secs\n", netname, t ); + } else { + BotAI_Print( PRT_MESSAGE, "%s: I've got a regular goal\n", netname ); + } + } + } +} +#endif //DEBUG + +/* +================== +BotGetItemTeamGoal + +FIXME: add stuff like "upper rocket launcher" +"the rl near the railgun", "lower grenade launcher" etc. +================== +*/ +int BotGetItemTeamGoal( char *goalname, bot_goal_t *goal ) { + int i; + + if ( !strlen( goalname ) ) { + return qfalse; + } + i = -1; + do { + i = trap_BotGetLevelItemGoal( i, goalname, goal ); + if ( i > 0 ) { // && !AvoidGoalTime(&bs->gs, goal.number)) + return qtrue; + } + } while ( i > 0 ); + return qfalse; +} + +/* +================== +BotGetMessageTeamGoal +================== +*/ +int BotGetMessageTeamGoal( bot_state_t *bs, char *goalname, bot_goal_t *goal ) { + bot_waypoint_t *cp; + + if ( BotGetItemTeamGoal( goalname, goal ) ) { + return qtrue; + } + + cp = BotFindWayPoint( bs->checkpoints, goalname ); + if ( cp ) { + memcpy( goal, &cp->goal, sizeof( bot_goal_t ) ); + return qtrue; + } + return qfalse; +} + +/* +================== +BotGetTime +================== +*/ +float BotGetTime( bot_match_t *match ) { + bot_match_t timematch; + char timestring[MAX_MESSAGE_SIZE]; + float t; + + //if the matched string has a time + if ( match->subtype & ST_TIME ) { + //get the time string + trap_BotMatchVariable( match, TIME, timestring, MAX_MESSAGE_SIZE ); + //match it to find out if the time is in seconds or minutes + if ( trap_BotFindMatch( timestring, &timematch, MTCONTEXT_TIME ) ) { + if ( timematch.type == MSG_FOREVER ) { + t = 99999999; + } else { + trap_BotMatchVariable( &timematch, TIME, timestring, MAX_MESSAGE_SIZE ); + if ( timematch.type == MSG_MINUTES ) { + t = atof( timestring ) * 60; + } else if ( timematch.type == MSG_SECONDS ) { + t = atof( timestring ); + } else { t = 0;} + } + //if there's a valid time + if ( t > 0 ) { + return trap_AAS_Time() + t; + } + } + } + return 0; +} + +/* +================== +FindClientByName +================== +*/ +int FindClientByName( char *name ) { + int i; + char buf[MAX_INFO_STRING]; + static int maxclients; + + if ( !maxclients ) { + maxclients = trap_Cvar_VariableIntegerValue( "sv_maxclients" ); + } + for ( i = 0; i < maxclients && i < MAX_CLIENTS; i++ ) { + ClientName( i, buf, sizeof( buf ) ); + if ( !Q_stricmp( buf, name ) ) { + return i; + } + } + for ( i = 0; i < maxclients && i < MAX_CLIENTS; i++ ) { + ClientName( i, buf, sizeof( buf ) ); + if ( stristr( buf, name ) ) { + return i; + } + } + return -1; +} + +/* +================== +FindEnemyByName +================== +*/ +int FindEnemyByName( bot_state_t *bs, char *name ) { + int i; + char buf[MAX_INFO_STRING]; + static int maxclients; + + if ( !maxclients ) { + maxclients = trap_Cvar_VariableIntegerValue( "sv_maxclients" ); + } + for ( i = 0; i < maxclients && i < MAX_CLIENTS; i++ ) { + if ( BotSameTeam( bs, i ) ) { + continue; + } + ClientName( i, buf, sizeof( buf ) ); + if ( !Q_stricmp( buf, name ) ) { + return i; + } + } + for ( i = 0; i < maxclients && i < MAX_CLIENTS; i++ ) { + if ( BotSameTeam( bs, i ) ) { + continue; + } + ClientName( i, buf, sizeof( buf ) ); + if ( stristr( buf, name ) ) { + return i; + } + } + return -1; +} + +/* +================== +NumPlayersOnSameTeam +================== +*/ +int NumPlayersOnSameTeam( bot_state_t *bs ) { + int i, num; + char buf[MAX_INFO_STRING]; + static int maxclients; + + if ( !maxclients ) { + maxclients = trap_Cvar_VariableIntegerValue( "sv_maxclients" ); + } + + num = 0; + for ( i = 0; i < maxclients && i < MAX_CLIENTS; i++ ) { + trap_GetConfigstring( CS_PLAYERS + i, buf, MAX_INFO_STRING ); + if ( strlen( buf ) ) { + if ( BotSameTeam( bs, i + 1 ) ) { + num++; + } + } + } + return num; +} + +/* +================== +TeamPlayIsOn +================== +*/ +int BotGetPatrolWaypoints( bot_state_t *bs, bot_match_t *match ) { + char keyarea[MAX_MESSAGE_SIZE]; + int patrolflags; + bot_waypoint_t *wp, *newwp, *newpatrolpoints; + bot_match_t keyareamatch; + bot_goal_t goal; + + newpatrolpoints = NULL; + patrolflags = 0; + // + trap_BotMatchVariable( match, KEYAREA, keyarea, MAX_MESSAGE_SIZE ); + // + while ( 1 ) { + if ( !trap_BotFindMatch( keyarea, &keyareamatch, MTCONTEXT_PATROLKEYAREA ) ) { + trap_EA_SayTeam( bs->client, "what do you say?" ); + BotFreeWaypoints( newpatrolpoints ); + bs->patrolpoints = NULL; + return qfalse; + } + trap_BotMatchVariable( &keyareamatch, KEYAREA, keyarea, MAX_MESSAGE_SIZE ); + if ( !BotGetMessageTeamGoal( bs, keyarea, &goal ) ) { + //BotAI_BotInitialChat(bs, "cannotfind", keyarea, NULL); + //trap_BotEnterChat(bs->cs, bs->client, CHAT_TEAM); + BotFreeWaypoints( newpatrolpoints ); + bs->patrolpoints = NULL; + return qfalse; + } + //create a new waypoint + newwp = BotCreateWayPoint( keyarea, goal.origin, goal.areanum ); + //add the waypoint to the patrol points + newwp->next = NULL; + for ( wp = newpatrolpoints; wp && wp->next; wp = wp->next ) ; + if ( !wp ) { + newpatrolpoints = newwp; + newwp->prev = NULL; + } else { + wp->next = newwp; + newwp->prev = wp; + } + // + if ( keyareamatch.subtype & ST_BACK ) { + patrolflags = PATROL_LOOP; + break; + } else if ( keyareamatch.subtype & ST_REVERSE ) { + patrolflags = PATROL_REVERSE; + break; + } else if ( keyareamatch.subtype & ST_MORE ) { + trap_BotMatchVariable( &keyareamatch, MORE, keyarea, MAX_MESSAGE_SIZE ); + } else { + break; + } + } + // + if ( !newpatrolpoints || !newpatrolpoints->next ) { + trap_EA_SayTeam( bs->client, "I need more key points to patrol\n" ); + BotFreeWaypoints( newpatrolpoints ); + newpatrolpoints = NULL; + return qfalse; + } + // + BotFreeWaypoints( bs->patrolpoints ); + bs->patrolpoints = newpatrolpoints; + // + bs->curpatrolpoint = bs->patrolpoints; + bs->patrolflags = patrolflags; + // + return qtrue; +} + +/* +================== +BotAddressedToBot +================== +*/ +int BotAddressedToBot( bot_state_t *bs, bot_match_t *match ) { + char addressedto[MAX_MESSAGE_SIZE]; + char netname[MAX_MESSAGE_SIZE]; + char name[MAX_MESSAGE_SIZE]; + char botname[128]; + int client; + bot_match_t addresseematch; + + trap_BotMatchVariable( match, NETNAME, netname, sizeof( netname ) ); + client = ClientFromName( netname ); + if ( client < 0 ) { + return qfalse; + } + if ( !BotSameTeam( bs, client ) ) { + return qfalse; + } + //if the message is addressed to someone + if ( match->subtype & ST_ADDRESSED ) { + trap_BotMatchVariable( match, ADDRESSEE, addressedto, sizeof( addressedto ) ); + //the name of this bot + ClientName( bs->client, botname, 128 ); + // + while ( trap_BotFindMatch( addressedto, &addresseematch, MTCONTEXT_ADDRESSEE ) ) { + if ( addresseematch.type == MSG_EVERYONE ) { + return qtrue; + } else if ( addresseematch.type == MSG_MULTIPLENAMES ) { + trap_BotMatchVariable( &addresseematch, TEAMMATE, name, sizeof( name ) ); + if ( strlen( name ) ) { + if ( stristr( botname, name ) ) { + return qtrue; + } + if ( stristr( bs->subteam, name ) ) { + return qtrue; + } + } + trap_BotMatchVariable( &addresseematch, MORE, addressedto, MAX_MESSAGE_SIZE ); + } else { + trap_BotMatchVariable( &addresseematch, TEAMMATE, name, MAX_MESSAGE_SIZE ); + if ( strlen( name ) ) { + if ( stristr( botname, name ) ) { + return qtrue; + } + if ( stristr( bs->subteam, name ) ) { + return qtrue; + } + } + break; + } + } + //Com_sprintf(buf, sizeof(buf), "not addressed to me but %s", addressedto); + //trap_EA_Say(bs->client, buf); + return qfalse; + } else { + //make sure not everyone reacts to this message + if ( random() > (float ) 1.0 / ( NumPlayersOnSameTeam( bs ) - 1 ) ) { + return qfalse; + } + } + return qtrue; +} + +/* +================== +BotGPSToPosition +================== +*/ +int BotGPSToPosition( char *buf, vec3_t position ) { + int i, j = 0; + int num, sign; + + for ( i = 0; i < 3; i++ ) { + num = 0; + while ( buf[j] == ' ' ) j++; + if ( buf[j] == '-' ) { + j++; + sign = -1; + } else { + sign = 1; + } + while ( buf[j] ) { + if ( buf[j] >= '0' && buf[j] <= '9' ) { + num = num * 10 + buf[j] - '0'; + j++; + } else { + j++; + break; + } + } + BotAI_Print( PRT_MESSAGE, "%d\n", sign * num ); + position[i] = (float) sign * num; + } + return qtrue; +} + +/* +================== +BotMatch_HelpAccompany +================== +*/ +void BotMatch_HelpAccompany( bot_state_t *bs, bot_match_t *match ) { + int client, other, areanum; + char teammate[MAX_MESSAGE_SIZE], netname[MAX_MESSAGE_SIZE]; + char itemname[MAX_MESSAGE_SIZE]; + bot_match_t teammatematch; + aas_entityinfo_t entinfo; + + if ( !TeamPlayIsOn() ) { + return; + } + //if not addressed to this bot + if ( !BotAddressedToBot( bs, match ) ) { + return; + } + //get the team mate name + trap_BotMatchVariable( match, TEAMMATE, teammate, sizeof( teammate ) ); + //get the client to help + if ( trap_BotFindMatch( teammate, &teammatematch, MTCONTEXT_TEAMMATE ) && + //if someone asks for him or herself + teammatematch.type == MSG_ME ) { + //get the netname + trap_BotMatchVariable( match, NETNAME, netname, sizeof( netname ) ); + client = ClientFromName( netname ); + other = qfalse; + } else { + //asked for someone else + client = FindClientByName( teammate ); + //if this is the bot self + if ( client == bs->client ) { + other = qfalse; + } else if ( !BotSameTeam( bs, client ) ) { + //FIXME: say "I don't help the enemy" + return; + } else { + other = qtrue; + } + } + //if the bot doesn't know who to help (FindClientByName returned -1) + if ( client < 0 ) { + if ( other ) { + BotAI_BotInitialChat( bs, "whois", teammate, NULL ); + } else { BotAI_BotInitialChat( bs, "whois", netname, NULL );} + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + return; + } + //don't help or accompany yourself + if ( client == bs->client ) { + return; + } + // + bs->teamgoal.entitynum = -1; + BotEntityInfo( client, &entinfo ); + //if info is valid (in PVS) + if ( entinfo.valid ) { + areanum = BotPointAreaNum( entinfo.origin ); + if ( areanum && trap_AAS_AreaReachability( areanum ) ) { + bs->teamgoal.entitynum = client; + bs->teamgoal.areanum = areanum; + VectorCopy( entinfo.origin, bs->teamgoal.origin ); + VectorSet( bs->teamgoal.mins, -8, -8, -8 ); + VectorSet( bs->teamgoal.maxs, 8, 8, 8 ); + } + } + //if no teamgoal yet + if ( bs->teamgoal.entitynum < 0 ) { + //if near an item + if ( match->subtype & ST_NEARITEM ) { + //get the match variable + trap_BotMatchVariable( match, ITEM, itemname, sizeof( itemname ) ); + // + if ( !BotGetMessageTeamGoal( bs, itemname, &bs->teamgoal ) ) { + //BotAI_BotInitialChat(bs, "cannotfind", itemname, NULL); + //trap_BotEnterChat(bs->cs, bs->client, CHAT_TEAM); + return; + } + } + } + // + if ( bs->teamgoal.entitynum < 0 ) { + if ( other ) { + BotAI_BotInitialChat( bs, "whereis", teammate, NULL ); + } else { BotAI_BotInitialChat( bs, "whereareyou", netname, NULL );} + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + return; + } + //the team mate + bs->teammate = client; + //last time the team mate was assumed visible + bs->teammatevisible_time = trap_AAS_Time(); + //set the time to send a message to the team mates + bs->teammessage_time = trap_AAS_Time() + 2 * random(); + //get the team goal time + bs->teamgoal_time = BotGetTime( match ); + //set the ltg type + if ( match->type == MSG_HELP ) { + bs->ltgtype = LTG_TEAMHELP; + if ( !bs->teamgoal_time ) { + bs->teamgoal_time = trap_AAS_Time() + TEAM_HELP_TIME; + } + } else { + bs->ltgtype = LTG_TEAMACCOMPANY; + if ( !bs->teamgoal_time ) { + bs->teamgoal_time = trap_AAS_Time() + TEAM_ACCOMPANY_TIME; + } + bs->formation_dist = 3.5 * 32; //3.5 meter + bs->arrive_time = 0; + } +#ifdef DEBUG + BotPrintTeamGoal( bs ); +#endif //DEBUG +} + +/* +================== +BotMatch_DefendKeyArea +================== +*/ +void BotMatch_DefendKeyArea( bot_state_t *bs, bot_match_t *match ) { + char itemname[MAX_MESSAGE_SIZE]; + + if ( !TeamPlayIsOn() ) { + return; + } + //if not addressed to this bot + if ( !BotAddressedToBot( bs, match ) ) { + return; + } + //get the match variable + trap_BotMatchVariable( match, KEYAREA, itemname, sizeof( itemname ) ); + // + if ( !BotGetMessageTeamGoal( bs, itemname, &bs->teamgoal ) ) { + //BotAI_BotInitialChat(bs, "cannotfind", itemname, NULL); + //trap_BotEnterChat(bs->cs, bs->client, CHAT_TEAM); + return; + } + //set the time to send a message to the team mates + bs->teammessage_time = trap_AAS_Time() + 2 * random(); + //set the ltg type + bs->ltgtype = LTG_DEFENDKEYAREA; + //get the team goal time + bs->teamgoal_time = BotGetTime( match ); + //set the team goal time + if ( !bs->teamgoal_time ) { + bs->teamgoal_time = trap_AAS_Time() + TEAM_DEFENDKEYAREA_TIME; + } + //away from defending + bs->defendaway_time = 0; +#ifdef DEBUG + BotPrintTeamGoal( bs ); +#endif //DEBUG +} + +/* +================== +BotMatch_GetItem +================== +*/ +void BotMatch_GetItem( bot_state_t *bs, bot_match_t *match ) { + char itemname[MAX_MESSAGE_SIZE]; + + if ( !TeamPlayIsOn() ) { + return; + } + //if not addressed to this bot + if ( !BotAddressedToBot( bs, match ) ) { + return; + } + //get the match variable + trap_BotMatchVariable( match, ITEM, itemname, sizeof( itemname ) ); + // + if ( !BotGetMessageTeamGoal( bs, itemname, &bs->teamgoal ) ) { + //BotAI_BotInitialChat(bs, "cannotfind", itemname, NULL); + //trap_BotEnterChat(bs->cs, bs->client, CHAT_TEAM); + return; + } + //set the time to send a message to the team mates + bs->teammessage_time = trap_AAS_Time() + 2 * random(); + //set the ltg type + bs->ltgtype = LTG_GETITEM; + //set the team goal time + bs->teamgoal_time = trap_AAS_Time() + TEAM_GETITEM_TIME; +#ifdef DEBUG + BotPrintTeamGoal( bs ); +#endif //DEBUG +} + +/* +================== +BotMatch_Camp +================== +*/ +void BotMatch_Camp( bot_state_t *bs, bot_match_t *match ) { + int client, areanum; + char netname[MAX_MESSAGE_SIZE]; + char itemname[MAX_MESSAGE_SIZE]; + aas_entityinfo_t entinfo; + + if ( !TeamPlayIsOn() ) { + return; + } + //if not addressed to this bot + if ( !BotAddressedToBot( bs, match ) ) { + return; + } + // + trap_BotMatchVariable( match, NETNAME, netname, sizeof( netname ) ); + //asked for someone else + client = FindClientByName( netname ); + //if there's no valid client with this name + if ( client < 0 ) { + BotAI_BotInitialChat( bs, "whois", netname, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + return; + } + //get the match variable + trap_BotMatchVariable( match, KEYAREA, itemname, sizeof( itemname ) ); + //in CTF it could be the base + if ( match->subtype & ST_THERE ) { + //camp at the spot the bot is currently standing + bs->teamgoal.entitynum = bs->entitynum; + bs->teamgoal.areanum = bs->areanum; + VectorCopy( bs->origin, bs->teamgoal.origin ); + VectorSet( bs->teamgoal.mins, -8, -8, -8 ); + VectorSet( bs->teamgoal.maxs, 8, 8, 8 ); + } else if ( match->subtype & ST_HERE ) { + //if this is the bot self + if ( client == bs->client ) { + return; + } + // + bs->teamgoal.entitynum = -1; + BotEntityInfo( client, &entinfo ); + //if info is valid (in PVS) + if ( entinfo.valid ) { + areanum = BotPointAreaNum( entinfo.origin ); + if ( areanum && trap_AAS_AreaReachability( areanum ) ) { + //NOTE: just cheat and assume the bot knows where the person is + //if (BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, client)) { + bs->teamgoal.entitynum = client; + bs->teamgoal.areanum = areanum; + VectorCopy( entinfo.origin, bs->teamgoal.origin ); + VectorSet( bs->teamgoal.mins, -8, -8, -8 ); + VectorSet( bs->teamgoal.maxs, 8, 8, 8 ); + //} + } + } + //if the other is not visible + if ( bs->teamgoal.entitynum < 0 ) { + BotAI_BotInitialChat( bs, "whereareyou", netname, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + return; + } + } else if ( !BotGetMessageTeamGoal( bs, itemname, &bs->teamgoal ) ) { + //BotAI_BotInitialChat(bs, "cannotfind", itemname, NULL); + //trap_BotEnterChat(bs->cs, bs->client, CHAT_TEAM); + return; + } + //set the time to send a message to the team mates + bs->teammessage_time = trap_AAS_Time() + 2 * random(); + //set the ltg type + bs->ltgtype = LTG_CAMPORDER; + //get the team goal time + bs->teamgoal_time = BotGetTime( match ); + //set the team goal time + if ( !bs->teamgoal_time ) { + bs->teamgoal_time = trap_AAS_Time() + TEAM_CAMP_TIME; + } + //the teammate that requested the camping + bs->teammate = client; + //not arrived yet + bs->arrive_time = 0; + // +#ifdef DEBUG + BotPrintTeamGoal( bs ); +#endif //DEBUG +} + +/* +================== +BotMatch_Patrol +================== +*/ +void BotMatch_Patrol( bot_state_t *bs, bot_match_t *match ) { + if ( !TeamPlayIsOn() ) { + return; + } + //if not addressed to this bot + if ( !BotAddressedToBot( bs, match ) ) { + return; + } + //get the patrol waypoints + if ( !BotGetPatrolWaypoints( bs, match ) ) { + return; + } + //set the time to send a message to the team mates + bs->teammessage_time = trap_AAS_Time() + 2 * random(); + //set the ltg type + bs->ltgtype = LTG_PATROL; + //get the team goal time + bs->teamgoal_time = BotGetTime( match ); + //set the team goal time if not set already + if ( !bs->teamgoal_time ) { + bs->teamgoal_time = trap_AAS_Time() + TEAM_PATROL_TIME; + } + // +#ifdef DEBUG + BotPrintTeamGoal( bs ); +#endif //DEBUG +} + +/* +================== +BotMatch_GetFlag +================== +*/ +void BotMatch_GetFlag( bot_state_t *bs, bot_match_t *match ) { + //if not in CTF mode + if ( gametype != GT_CTF || !ctf_redflag.areanum || !ctf_blueflag.areanum ) { + return; + } + //if not addressed to this bot + if ( !BotAddressedToBot( bs, match ) ) { + return; + } + //set the time to send a message to the team mates + bs->teammessage_time = trap_AAS_Time() + 2 * random(); + //set the ltg type + bs->ltgtype = LTG_GETFLAG; + //set the team goal time + bs->teamgoal_time = trap_AAS_Time() + CTF_GETFLAG_TIME; +#ifdef DEBUG + BotPrintTeamGoal( bs ); +#endif //DEBUG +} + +/* +================== +BotMatch_RushBase +================== +*/ +void BotMatch_RushBase( bot_state_t *bs, bot_match_t *match ) { + //if not in CTF mode + if ( gametype != GT_CTF || !ctf_redflag.areanum || !ctf_blueflag.areanum ) { + return; + } + //if not addressed to this bot + if ( !BotAddressedToBot( bs, match ) ) { + return; + } + //set the time to send a message to the team mates + bs->teammessage_time = trap_AAS_Time() + 2 * random(); + //set the ltg type + bs->ltgtype = LTG_RUSHBASE; + //set the team goal time + bs->teamgoal_time = trap_AAS_Time() + CTF_RUSHBASE_TIME; + bs->rushbaseaway_time = 0; +#ifdef DEBUG + BotPrintTeamGoal( bs ); +#endif //DEBUG +} + + +/* +================== +BotMatch_ReturnFlag +================== +*/ +void BotMatch_ReturnFlag( bot_state_t *bs, bot_match_t *match ) { + //if not in CTF mode + if ( gametype != GT_CTF ) { + return; + } + //if not addressed to this bot + if ( !BotAddressedToBot( bs, match ) ) { + return; + } + //set the time to send a message to the team mates + bs->teammessage_time = trap_AAS_Time() + 2 * random(); + //set the ltg type + bs->ltgtype = LTG_RETURNFLAG; + //set the team goal time + bs->teamgoal_time = trap_AAS_Time() + CTF_RETURNFLAG_TIME; + bs->rushbaseaway_time = 0; +#ifdef DEBUG + BotPrintTeamGoal( bs ); +#endif //DEBUG +} + +/* +================== +BotMatch_JoinSubteam +================== +*/ +void BotMatch_JoinSubteam( bot_state_t *bs, bot_match_t *match ) { + char teammate[MAX_MESSAGE_SIZE]; + + if ( !TeamPlayIsOn() ) { + return; + } + //if not addressed to this bot + if ( !BotAddressedToBot( bs, match ) ) { + return; + } + //get the sub team name + trap_BotMatchVariable( match, TEAMNAME, teammate, MAX_MESSAGE_SIZE ); + //set the sub team name + strncpy( bs->subteam, teammate, 32 ); + bs->subteam[31] = '\0'; + // + BotAI_BotInitialChat( bs, "joinedteam", teammate, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); +} + +/* +================== +BotMatch_LeaveSubteam +================== +*/ +void BotMatch_LeaveSubteam( bot_state_t *bs, bot_match_t *match ) { + if ( !TeamPlayIsOn() ) { + return; + } + //if not addressed to this bot + if ( !BotAddressedToBot( bs, match ) ) { + return; + } + // + if ( strlen( bs->subteam ) ) { + BotAI_BotInitialChat( bs, "leftteam", bs->subteam, NULL ); + } //end if + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + strcpy( bs->subteam, "" ); +} + +/* +================== +BotMatch_LeaveSubteam +================== +*/ +void BotMatch_WhichTeam( bot_state_t *bs, bot_match_t *match ) { + if ( !TeamPlayIsOn() ) { + return; + } + //if not addressed to this bot + if ( !BotAddressedToBot( bs, match ) ) { + return; + } + // + if ( strlen( bs->subteam ) ) { + BotAI_BotInitialChat( bs, "inteam", bs->subteam, NULL ); + } else { + BotAI_BotInitialChat( bs, "noteam", NULL ); + } + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); +} + +/* +================== +BotMatch_CheckPoint +================== +*/ +void BotMatch_CheckPoint( bot_state_t *bs, bot_match_t *match ) { + int areanum; + char buf[MAX_MESSAGE_SIZE]; + vec3_t position; + bot_waypoint_t *cp; + + if ( !TeamPlayIsOn() ) { + return; + } + // + trap_BotMatchVariable( match, POSITION, buf, MAX_MESSAGE_SIZE ); + VectorClear( position ); + //BotGPSToPosition(buf, position); + sscanf( buf, "%f %f %f", &position[0], &position[1], &position[2] ); + position[2] += 0.5; + areanum = BotPointAreaNum( position ); + if ( !areanum ) { + if ( BotAddressedToBot( bs, match ) ) { + BotAI_BotInitialChat( bs, "checkpoint_invalid", NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + } + return; + } + // + trap_BotMatchVariable( match, NAME, buf, MAX_MESSAGE_SIZE ); + //check if there already exists a checkpoint with this name + cp = BotFindWayPoint( bs->checkpoints, buf ); + if ( cp ) { + if ( cp->next ) { + cp->next->prev = cp->prev; + } + if ( cp->prev ) { + cp->prev->next = cp->next; + } else { bs->checkpoints = cp->next;} + cp->inuse = qfalse; + } + //create a new check point + cp = BotCreateWayPoint( buf, position, areanum ); + //add the check point to the bot's known chech points + cp->next = bs->checkpoints; + if ( bs->checkpoints ) { + bs->checkpoints->prev = cp; + } + bs->checkpoints = cp; + // + if ( BotAddressedToBot( bs, match ) ) { + Com_sprintf( buf, sizeof( buf ), "%1.0f %1.0f %1.0f", cp->goal.origin[0], + cp->goal.origin[1], + cp->goal.origin[2] ); + + BotAI_BotInitialChat( bs, "checkpoint_confirm", cp->name, buf, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + } +} + +/* +================== +BotMatch_FormationSpace +================== +*/ +void BotMatch_FormationSpace( bot_state_t *bs, bot_match_t *match ) { + char buf[MAX_MESSAGE_SIZE]; + float space; + + if ( !TeamPlayIsOn() ) { + return; + } + //if not addressed to this bot + if ( !BotAddressedToBot( bs, match ) ) { + return; + } + // + trap_BotMatchVariable( match, NUMBER, buf, MAX_MESSAGE_SIZE ); + //if it's the distance in feet + if ( match->subtype & ST_FEET ) { + space = 0.3048 * 32 * atof( buf ); + } + //else it's in meters + else {space = 32 * atof( buf );} + //check if the formation intervening space is valid + if ( space < 48 || space > 500 ) { + space = 100; + } + bs->formation_dist = space; +} + +/* +================== +BotMatch_Dismiss +================== +*/ +void BotMatch_Dismiss( bot_state_t *bs, bot_match_t *match ) { + if ( !TeamPlayIsOn() ) { + return; + } + //if not addressed to this bot + if ( !BotAddressedToBot( bs, match ) ) { + return; + } + // + bs->ltgtype = 0; + bs->lead_time = 0; + // + BotAI_BotInitialChat( bs, "dismissed", NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); +} + +/* +================== +BotMatch_StartTeamLeaderShip +================== +*/ +void BotMatch_StartTeamLeaderShip( bot_state_t *bs, bot_match_t *match ) { + int client; + char teammate[MAX_MESSAGE_SIZE]; + + if ( !TeamPlayIsOn() ) { + return; + } + //if chats for him or herself + if ( match->subtype & ST_I ) { + //get the team mate that will be the team leader + trap_BotMatchVariable( match, NETNAME, teammate, sizeof( teammate ) ); + strncpy( bs->teamleader, teammate, sizeof( bs->teamleader ) ); + bs->teamleader[sizeof( bs->teamleader )] = '\0'; + } + //chats for someone else + else { + //get the team mate that will be the team leader + trap_BotMatchVariable( match, TEAMMATE, teammate, sizeof( teammate ) ); + client = FindClientByName( teammate ); + if ( client >= 0 ) { + ClientName( client, bs->teamleader, sizeof( bs->teamleader ) ); + } + } +} + +/* +================== +BotMatch_StopTeamLeaderShip +================== +*/ +void BotMatch_StopTeamLeaderShip( bot_state_t *bs, bot_match_t *match ) { + int client; + char teammate[MAX_MESSAGE_SIZE]; + char netname[MAX_MESSAGE_SIZE]; + + if ( !TeamPlayIsOn() ) { + return; + } + //get the team mate that stops being the team leader + trap_BotMatchVariable( match, TEAMMATE, teammate, sizeof( teammate ) ); + //if chats for him or herself + if ( match->subtype & ST_I ) { + trap_BotMatchVariable( match, NETNAME, netname, sizeof( netname ) ); + client = FindClientByName( netname ); + } + //chats for someone else + else { + client = FindClientByName( teammate ); + } //end else + if ( client >= 0 ) { + if ( !Q_stricmp( bs->teamleader, ClientName( client, netname, sizeof( netname ) ) ) ) { + bs->teamleader[0] = '\0'; + } + } +} + +/* +================== +BotMatch_WhoIsTeamLeader +================== +*/ +void BotMatch_WhoIsTeamLeader( bot_state_t *bs, bot_match_t *match ) { + char netname[MAX_MESSAGE_SIZE]; + + if ( !TeamPlayIsOn() ) { + return; + } + + ClientName( bs->client, netname, sizeof( netname ) ); + //if this bot IS the team leader + if ( !Q_stricmp( netname, bs->teamleader ) ) { + trap_EA_SayTeam( bs->client, "I'm the team leader\n" ); + } +} + +/* +================== +BotMatch_WhatAreYouDoing +================== +*/ +void BotMatch_WhatAreYouDoing( bot_state_t *bs, bot_match_t *match ) { + char netname[MAX_MESSAGE_SIZE]; + char goalname[MAX_MESSAGE_SIZE]; + + //if not addressed to this bot + if ( !BotAddressedToBot( bs, match ) ) { + return; + } + // + switch ( bs->ltgtype ) { + case LTG_TEAMHELP: + { + trap_BotMatchVariable( match, NETNAME, netname, sizeof( netname ) ); + EasyClientName( bs->teammate, netname, MAX_MESSAGE_SIZE ); + BotAI_BotInitialChat( bs, "helping", netname, NULL ); + break; + } + case LTG_TEAMACCOMPANY: + { + trap_BotMatchVariable( match, NETNAME, netname, sizeof( netname ) ); + EasyClientName( bs->teammate, netname, MAX_MESSAGE_SIZE ); + BotAI_BotInitialChat( bs, "accompanying", netname, NULL ); + break; + } + case LTG_DEFENDKEYAREA: + { + trap_BotGoalName( bs->teamgoal.number, goalname, sizeof( goalname ) ); + BotAI_BotInitialChat( bs, "defending", goalname, NULL ); + break; + } + case LTG_GETITEM: + { + trap_BotGoalName( bs->teamgoal.number, goalname, sizeof( goalname ) ); + BotAI_BotInitialChat( bs, "gettingitem", goalname, NULL ); + break; + } + case LTG_KILL: + { + ClientName( bs->teamgoal.entitynum, netname, sizeof( netname ) ); + BotAI_BotInitialChat( bs, "killing", netname, NULL ); + break; + } + case LTG_CAMP: + case LTG_CAMPORDER: + { + BotAI_BotInitialChat( bs, "camping", NULL ); + break; + } + case LTG_PATROL: + { + BotAI_BotInitialChat( bs, "patrolling", NULL ); + break; + } + case LTG_GETFLAG: + { + BotAI_BotInitialChat( bs, "capturingflag", NULL ); + break; + } + case LTG_RUSHBASE: + { + BotAI_BotInitialChat( bs, "rushingbase", NULL ); + break; + } + case LTG_RETURNFLAG: + { + BotAI_BotInitialChat( bs, "returningflag", NULL ); + break; + } + default: + { + BotAI_BotInitialChat( bs, "roaming", NULL ); + break; + } + } + //chat what the bot is doing + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); +} + +/* +================== +BotMatch_WhatIsMyCommand +================== +*/ +void BotMatch_WhatIsMyCommand( bot_state_t *bs, bot_match_t *match ) { + char netname[MAX_NETNAME]; + + ClientName( bs->client, netname, sizeof( netname ) ); + if ( Q_stricmp( netname, bs->teamleader ) != 0 ) { + return; + } + bs->forceorders = qtrue; +} + +/* +================== +BotNearestVisibleItem +================== +*/ +float BotNearestVisibleItem( bot_state_t *bs, char *itemname, bot_goal_t *goal ) { + int i; + char name[64]; + bot_goal_t tmpgoal; + float dist, bestdist; + vec3_t dir; + bsp_trace_t trace; + + bestdist = 999999; + i = -1; + do { + i = trap_BotGetLevelItemGoal( i, itemname, &tmpgoal ); + trap_BotGoalName( tmpgoal.number, name, sizeof( name ) ); + if ( Q_stricmp( itemname, name ) != 0 ) { + continue; + } + VectorSubtract( tmpgoal.origin, bs->origin, dir ); + dist = VectorLength( dir ); + if ( dist < bestdist ) { + //trace from start to end + BotAI_Trace( &trace, bs->eye, NULL, NULL, tmpgoal.origin, bs->client, CONTENTS_SOLID | CONTENTS_PLAYERCLIP ); + if ( trace.fraction >= 1.0 ) { + bestdist = dist; + memcpy( goal, &tmpgoal, sizeof( bot_goal_t ) ); + } + } + } while ( i > 0 ); + return bestdist; +} + +/* +================== +BotMatch_WhereAreYou +================== +*/ +void BotMatch_WhereAreYou( bot_state_t *bs, bot_match_t *match ) { + float dist, bestdist; + int i, bestitem, redflagtt, blueflagtt, redtobluett; + bot_goal_t goal; + char *nearbyitems[] = { + "Shotgun", + "Grenade Launcher", + "Rocket Launcher", + "Plasmagun", + "Railgun", + "Lightning Gun", + "BFG10K", + "Quad Damage", + "Regeneration", + "Battle Suit", + "Speed", + "Invisibility", + "Flight", + "Armor", + "Heavy Armor", + "Red Flag", + "Blue Flag", + NULL + }; + // + if ( !TeamPlayIsOn() ) { + return; + } + //if not addressed to this bot + if ( !BotAddressedToBot( bs, match ) ) { + return; + } + + bestitem = -1; + bestdist = 999999; + for ( i = 0; nearbyitems[i]; i++ ) { + dist = BotNearestVisibleItem( bs, nearbyitems[i], &goal ); + if ( dist < bestdist ) { + bestdist = dist; + bestitem = i; + } + } + if ( bestitem != -1 ) { + if ( gametype == GT_CTF ) { + redflagtt = trap_AAS_AreaTravelTimeToGoalArea( bs->areanum, bs->origin, ctf_redflag.areanum, TFL_DEFAULT ); + blueflagtt = trap_AAS_AreaTravelTimeToGoalArea( bs->areanum, bs->origin, ctf_blueflag.areanum, TFL_DEFAULT ); + redtobluett = trap_AAS_AreaTravelTimeToGoalArea( ctf_redflag.areanum, ctf_redflag.origin, ctf_blueflag.areanum, TFL_DEFAULT ); + if ( redflagtt < ( redflagtt + blueflagtt ) * 0.4 ) { + BotAI_BotInitialChat( bs, "ctflocation", nearbyitems[bestitem], "red", NULL ); + } else if ( blueflagtt < ( redflagtt + blueflagtt ) * 0.4 ) { + BotAI_BotInitialChat( bs, "ctflocation", nearbyitems[bestitem], "blue", NULL ); + } else { + BotAI_BotInitialChat( bs, "location", nearbyitems[bestitem], NULL ); + } + } else { + BotAI_BotInitialChat( bs, "location", nearbyitems[bestitem], NULL ); + } + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + } +} + +/* +================== +BotMatch_LeadTheWay +================== +*/ +void BotMatch_LeadTheWay( bot_state_t *bs, bot_match_t *match ) { + aas_entityinfo_t entinfo; + char netname[MAX_MESSAGE_SIZE], teammate[MAX_MESSAGE_SIZE]; + int client, areanum, other; + + if ( !TeamPlayIsOn() ) { + return; + } + //if not addressed to this bot + if ( !BotAddressedToBot( bs, match ) ) { + return; + } + //if someone asks for someone else + if ( match->subtype & ST_SOMEONE ) { + //get the team mate name + trap_BotMatchVariable( match, TEAMMATE, teammate, sizeof( teammate ) ); + client = FindClientByName( teammate ); + //if this is the bot self + if ( client == bs->client ) { + other = qfalse; + } else if ( !BotSameTeam( bs, client ) ) { + //FIXME: say "I don't help the enemy" + return; + } else { + other = qtrue; + } + } else { + //get the netname + trap_BotMatchVariable( match, NETNAME, netname, sizeof( netname ) ); + client = ClientFromName( netname ); + other = qfalse; + } + //if the bot doesn't know who to help (FindClientByName returned -1) + if ( client < 0 ) { + BotAI_BotInitialChat( bs, "whois", netname, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + return; + } + // + bs->lead_teamgoal.entitynum = -1; + BotEntityInfo( client, &entinfo ); + //if info is valid (in PVS) + if ( entinfo.valid ) { + areanum = BotPointAreaNum( entinfo.origin ); + if ( areanum && trap_AAS_AreaReachability( areanum ) ) { + bs->lead_teamgoal.entitynum = client; + bs->lead_teamgoal.areanum = areanum; + VectorCopy( entinfo.origin, bs->lead_teamgoal.origin ); + VectorSet( bs->lead_teamgoal.mins, -8, -8, -8 ); + VectorSet( bs->lead_teamgoal.maxs, 8, 8, 8 ); + } + } + + if ( bs->teamgoal.entitynum < 0 ) { + if ( other ) { + BotAI_BotInitialChat( bs, "whereis", teammate, NULL ); + } else { BotAI_BotInitialChat( bs, "whereareyou", netname, NULL );} + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + return; + } + bs->lead_teammate = client; + bs->lead_time = trap_AAS_Time() + TEAM_LEAD_TIME; + bs->leadvisible_time = 0; + bs->leadmessage_time = -( trap_AAS_Time() + 2 * random() ); +} + +/* +================== +BotMatch_Kill +================== +*/ +void BotMatch_Kill( bot_state_t *bs, bot_match_t *match ) { + char enemy[MAX_MESSAGE_SIZE]; + int client; + + if ( !TeamPlayIsOn() ) { + return; + } + //if not addressed to this bot + if ( !BotAddressedToBot( bs, match ) ) { + return; + } + + trap_BotMatchVariable( match, ENEMY, enemy, sizeof( enemy ) ); + // + client = FindEnemyByName( bs, enemy ); + if ( client < 0 ) { + BotAI_BotInitialChat( bs, "whois", enemy, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + return; + } + bs->teamgoal.entitynum = client; + //set the time to send a message to the team mates + bs->teammessage_time = trap_AAS_Time() + 2 * random(); + //set the ltg type + bs->ltgtype = LTG_KILL; + //set the team goal time + bs->teamgoal_time = trap_AAS_Time() + TEAM_KILL_SOMEONE; +#ifdef DEBUG + BotPrintTeamGoal( bs ); +#endif //DEBUG +} + +/* +================== +BotMatch_CTF +================== +*/ +void BotMatch_CTF( bot_state_t *bs, bot_match_t *match ) { + + char flag[128], netname[MAX_NETNAME]; + + trap_BotMatchVariable( match, FLAG, flag, sizeof( flag ) ); + if ( match->subtype & ST_GOTFLAG ) { + if ( !Q_stricmp( flag, "red" ) ) { + bs->redflagstatus = 1; + if ( BotCTFTeam( bs ) == CTF_TEAM_BLUE ) { + trap_BotMatchVariable( match, NETNAME, netname, sizeof( netname ) ); + bs->flagcarrier = ClientFromName( netname ); + } + } else { + bs->blueflagstatus = 1; + if ( BotCTFTeam( bs ) == CTF_TEAM_RED ) { + trap_BotMatchVariable( match, NETNAME, netname, sizeof( netname ) ); + bs->flagcarrier = ClientFromName( netname ); + } + } + bs->flagstatuschanged = 1; + } else if ( match->subtype & ST_CAPTUREDFLAG ) { + bs->redflagstatus = 0; + bs->blueflagstatus = 0; + bs->flagcarrier = 0; + bs->flagstatuschanged = 1; + } else if ( match->subtype & ST_RETURNEDFLAG ) { + if ( !Q_stricmp( flag, "red" ) ) { + bs->redflagstatus = 0; + } else { bs->blueflagstatus = 0;} + bs->flagstatuschanged = 1; + } +} + +/* +================== +BotMatchMessage +================== +*/ +int BotMatchMessage( bot_state_t *bs, char *message ) { + bot_match_t match; + + match.type = 0; + //if it is an unknown message + if ( !trap_BotFindMatch( message, &match, MTCONTEXT_ENTERGAME + | MTCONTEXT_INITIALTEAMCHAT + | MTCONTEXT_CTF ) ) { + return qfalse; + } + //react to the found message + switch ( match.type ) { + case MSG_HELP: //someone calling for help + case MSG_ACCOMPANY: //someone calling for company + { + BotMatch_HelpAccompany( bs, &match ); + break; + } + case MSG_DEFENDKEYAREA: //teamplay defend a key area + { + BotMatch_DefendKeyArea( bs, &match ); + break; + } + case MSG_CAMP: //camp somewhere + { + BotMatch_Camp( bs, &match ); + break; + } + case MSG_PATROL: //patrol between several key areas + { + BotMatch_Patrol( bs, &match ); + break; + } +#ifdef CTF + case MSG_GETFLAG: //ctf get the enemy flag + { + BotMatch_GetFlag( bs, &match ); + break; + } + case MSG_RUSHBASE: //ctf rush to the base + { + BotMatch_RushBase( bs, &match ); + break; + } + case MSG_RETURNFLAG: + { + BotMatch_ReturnFlag( bs, &match ); + break; + } +#endif //CTF + case MSG_GETITEM: + { + BotMatch_GetItem( bs, &match ); + break; + } + case MSG_JOINSUBTEAM: //join a sub team + { + BotMatch_JoinSubteam( bs, &match ); + break; + } + case MSG_LEAVESUBTEAM: //leave a sub team + { + BotMatch_LeaveSubteam( bs, &match ); + break; + } + case MSG_WHICHTEAM: + { + BotMatch_WhichTeam( bs, &match ); + break; + } + case MSG_CHECKPOINT: //remember a check point + { + BotMatch_CheckPoint( bs, &match ); + break; + } + case MSG_CREATENEWFORMATION: //start the creation of a new formation + { + trap_EA_SayTeam( bs->client, "the part of my brain to create formations has been damaged" ); + break; + } + case MSG_FORMATIONPOSITION: //tell someone his/her position in the formation + { + trap_EA_SayTeam( bs->client, "the part of my brain to create formations has been damaged" ); + break; + } + case MSG_FORMATIONSPACE: //set the formation space + { + BotMatch_FormationSpace( bs, &match ); + break; + } + case MSG_DOFORMATION: //form a certain formation + { + break; + } + case MSG_DISMISS: //dismiss someone + { + BotMatch_Dismiss( bs, &match ); + break; + } + case MSG_STARTTEAMLEADERSHIP: //someone will become the team leader + { + BotMatch_StartTeamLeaderShip( bs, &match ); + break; + } + case MSG_STOPTEAMLEADERSHIP: //someone will stop being the team leader + { + BotMatch_StopTeamLeaderShip( bs, &match ); + break; + } + case MSG_WHOISTEAMLAEDER: + { + BotMatch_WhoIsTeamLeader( bs, &match ); + break; + } + case MSG_WHATAREYOUDOING: //ask a bot what he/she is doing + { + BotMatch_WhatAreYouDoing( bs, &match ); + break; + } + case MSG_WHATISMYCOMMAND: + { + BotMatch_WhatIsMyCommand( bs, &match ); + break; + } + case MSG_WHEREAREYOU: + { + BotMatch_WhereAreYou( bs, &match ); + break; + } + case MSG_LEADTHEWAY: + { + BotMatch_LeadTheWay( bs, &match ); + break; + } + case MSG_KILL: + { + BotMatch_Kill( bs, &match ); + break; + } + case MSG_ENTERGAME: //someone entered the game + { + //NOTE: eliza chats will catch this + //BotMatchVariable(&match, NETNAME, netname); + //Com_sprintf(buf, sizeof(buf), "heya %s", netname); + //EA_Say(bs->client, buf); + break; + } + case MSG_CTF: + { + BotMatch_CTF( bs, &match ); + break; + } + case MSG_WAIT: + { + break; + } + default: + { + BotAI_Print( PRT_MESSAGE, "unknown match type\n" ); + break; + } + } + return qtrue; +} diff --git a/src/botai/ai_cmd.h b/src/botai/ai_cmd.h new file mode 100644 index 0000000..eb91c36 --- /dev/null +++ b/src/botai/ai_cmd.h @@ -0,0 +1,40 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: ai_cmd.h + * + * desc: Quake3 bot AI + * + * + *****************************************************************************/ + +int BotMatchMessage( bot_state_t *bs, char *message ); +void BotPrintTeamGoal( bot_state_t *bs ); + diff --git a/src/botai/ai_dmnet.c b/src/botai/ai_dmnet.c new file mode 100644 index 0000000..1b19160 --- /dev/null +++ b/src/botai/ai_dmnet.c @@ -0,0 +1,2045 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: ai_dmnet.c + * + * desc: Quake3 bot AI + * + * + *****************************************************************************/ + +#include "../game/g_local.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "../game/be_ea.h" +#include "../game/be_ai_char.h" +#include "../game/be_ai_chat.h" +#include "../game/be_ai_gen.h" +#include "../game/be_ai_goal.h" +#include "../game/be_ai_move.h" +#include "../game/be_ai_weap.h" +#include "../botai/botai.h" +// +#include "ai_main.h" +#include "ai_dmq3.h" +#include "ai_chat.h" +#include "ai_cmd.h" +#include "ai_dmnet.h" +//data file headers +#include "chars.h" //characteristics +#include "inv.h" //indexes into the inventory +#include "syn.h" //synonyms +#include "match.h" //string matching types and vars + +//goal flag, see be_ai_goal.h for the other GFL_* +#define GFL_AIR 16 + +int numnodeswitches; +char nodeswitch[MAX_NODESWITCHES + 1][144]; + +#define LOOKAHEAD_DISTANCE 300 + +/* +================== +BotResetNodeSwitches +================== +*/ +void BotResetNodeSwitches( void ) { + numnodeswitches = 0; +} + +/* +================== +BotDumpNodeSwitches +================== +*/ +void BotDumpNodeSwitches( bot_state_t *bs ) { + int i; + char netname[MAX_NETNAME]; + + ClientName( bs->client, netname, sizeof( netname ) ); + BotAI_Print( PRT_MESSAGE, "%s at %1.1f switched more than %d AI nodes\n", netname, trap_AAS_Time(), MAX_NODESWITCHES ); + for ( i = 0; i < numnodeswitches; i++ ) { + BotAI_Print( PRT_MESSAGE, nodeswitch[i] ); + } + BotAI_Print( PRT_FATAL, "" ); +} + +/* +================== +BotRecordNodeSwitch +================== +*/ +void BotRecordNodeSwitch( bot_state_t *bs, char *node, char *str ) { + char netname[MAX_NETNAME]; + + ClientName( bs->client, netname, sizeof( netname ) ); + Com_sprintf( nodeswitch[numnodeswitches], 144, "%s at %2.1f entered %s: %s\n", netname, trap_AAS_Time(), node, str ); +#ifdef DEBUG + if ( 0 ) { + BotAI_Print( PRT_MESSAGE, nodeswitch[numnodeswitches] ); + } +#endif //DEBUG + numnodeswitches++; +} + +/* +================== +BotGetAirGoal +================== +*/ +int BotGetAirGoal( bot_state_t *bs, bot_goal_t *goal ) { + bsp_trace_t bsptrace; + vec3_t end, mins = {-15, -15, -2}, maxs = {15, 15, 2}; + int areanum; + + //trace up until we hit solid + VectorCopy( bs->origin, end ); + end[2] += 1000; + BotAI_Trace( &bsptrace, bs->origin, mins, maxs, end, bs->entitynum, CONTENTS_SOLID | CONTENTS_PLAYERCLIP ); + //trace down until we hit water + VectorCopy( bsptrace.endpos, end ); + BotAI_Trace( &bsptrace, end, mins, maxs, bs->origin, bs->entitynum, CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ); + //if we found the water surface + if ( bsptrace.fraction > 0 ) { + areanum = BotPointAreaNum( bsptrace.endpos ); + if ( areanum ) { + VectorCopy( bsptrace.endpos, goal->origin ); + goal->origin[2] -= 2; + goal->areanum = areanum; + goal->mins[0] = -15; + goal->mins[1] = -15; + goal->mins[2] = -1; + goal->maxs[0] = 15; + goal->maxs[1] = 15; + goal->maxs[2] = 1; + goal->flags = GFL_AIR; + goal->number = 0; + goal->iteminfo = 0; + goal->entitynum = 0; + return qtrue; + } + } + return qfalse; +} + +/* +================== +BotGoForAir +================== +*/ +int BotGoForAir( bot_state_t *bs, int tfl, bot_goal_t *ltg, float range ) { + bot_goal_t goal; + + //if the bot needs air + if ( bs->lastair_time < trap_AAS_Time() - 6 ) { + // +#ifdef DEBUG + //BotAI_Print(PRT_MESSAGE, "going for air\n"); +#endif //DEBUG + //if we can find an air goal + if ( BotGetAirGoal( bs, &goal ) ) { + trap_BotPushGoal( bs->gs, &goal ); + return qtrue; + } else { + //get a nearby goal outside the water + while ( trap_BotChooseNBGItem( bs->gs, bs->origin, bs->inventory, tfl, ltg, range ) ) { + trap_BotGetTopGoal( bs->gs, &goal ); + //if the goal is not in water + if ( !( trap_AAS_PointContents( goal.origin ) & ( CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ) ) ) { + return qtrue; + } + trap_BotPopGoal( bs->gs ); + } + trap_BotResetAvoidGoals( bs->gs ); + } + } + return qfalse; +} + +/* +================== +BotNearbyGoal +================== +*/ +int BotNearbyGoal( bot_state_t *bs, int tfl, bot_goal_t *ltg, float range ) { + int ret; + + if ( BotGoForAir( bs, tfl, ltg, range ) ) { + return qtrue; + } + // + ret = trap_BotChooseNBGItem( bs->gs, bs->origin, bs->inventory, tfl, ltg, range ); + /* + if (ret) + { + char buf[128]; + //get the goal at the top of the stack + trap_BotGetTopGoal(bs->gs, &goal); + trap_BotGoalName(goal.number, buf, sizeof(buf)); + BotAI_Print(PRT_MESSAGE, "%1.1f: new nearby goal %s\n", trap_AAS_Time(), buf); + } + */ + return ret; +} + +/* +================== +BotReachedGoal +================== +*/ +int BotReachedGoal( bot_state_t *bs, bot_goal_t *goal ) { + if ( goal->flags & GFL_ITEM ) { + //if touching the goal + if ( trap_BotTouchingGoal( bs->origin, goal ) ) { + return qtrue; + } + //if the goal isn't there + if ( trap_BotItemGoalInVisButNotVisible( bs->entitynum, bs->eye, bs->viewangles, goal ) ) { + return qtrue; + } + //if in the goal area and below or above the goal and not swimming + if ( bs->areanum == goal->areanum ) { + if ( bs->origin[0] > goal->origin[0] + goal->mins[0] && bs->origin[0] < goal->origin[0] + goal->maxs[0] ) { + if ( bs->origin[1] > goal->origin[1] + goal->mins[1] && bs->origin[1] < goal->origin[1] + goal->maxs[1] ) { + if ( !trap_AAS_Swimming( bs->origin ) ) { + return qtrue; + } + } + } + } + } else if ( goal->flags & GFL_AIR ) { + //if touching the goal + if ( trap_BotTouchingGoal( bs->origin, goal ) ) { + return qtrue; + } + //if the bot got air + if ( bs->lastair_time > trap_AAS_Time() - 1 ) { + return qtrue; + } + } else { + //if touching the goal + if ( trap_BotTouchingGoal( bs->origin, goal ) ) { + return qtrue; + } + } + return qfalse; +} + +/* +================== +BotGetItemLongTermGoal +================== +*/ +int BotGetItemLongTermGoal( bot_state_t *bs, int tfl, bot_goal_t *goal ) { + //if the bot has no goal + if ( !trap_BotGetTopGoal( bs->gs, goal ) ) { + //BotAI_Print(PRT_MESSAGE, "no ltg on stack\n"); + bs->ltg_time = 0; + } + //if the bot touches the current goal + else if ( BotReachedGoal( bs, goal ) ) { + BotChooseWeapon( bs ); + bs->ltg_time = 0; + } + //if it is time to find a new long term goal + if ( bs->ltg_time < trap_AAS_Time() ) { + //pop the current goal from the stack + trap_BotPopGoal( bs->gs ); + //BotAI_Print(PRT_MESSAGE, "%s: choosing new ltg\n", ClientName(bs->client, netname, sizeof(netname))); + //choose a new goal + //BotAI_Print(PRT_MESSAGE, "%6.1f client %d: BotChooseLTGItem\n", trap_AAS_Time(), bs->client); + if ( trap_BotChooseLTGItem( bs->gs, bs->origin, bs->inventory, tfl ) ) { + /* + char buf[128]; + //get the goal at the top of the stack + trap_BotGetTopGoal(bs->gs, goal); + trap_BotGoalName(goal->number, buf, sizeof(buf)); + BotAI_Print(PRT_MESSAGE, "%1.1f: new long term goal %s\n", trap_AAS_Time(), buf); + */ + bs->ltg_time = trap_AAS_Time() + 20; + } else { //the bot gets sorta stuck with all the avoid timings, shouldn't happen though + // +#ifdef DEBUG + char netname[128]; + + BotAI_Print( PRT_MESSAGE, "%s: no valid ltg (probably stuck)\n", ClientName( bs->client, netname, sizeof( netname ) ) ); +#endif + //trap_BotDumpAvoidGoals(bs->gs); + //reset the avoid goals and the avoid reach + trap_BotResetAvoidGoals( bs->gs ); + trap_BotResetAvoidReach( bs->ms ); + } + //get the goal at the top of the stack + return trap_BotGetTopGoal( bs->gs, goal ); + } + return qtrue; +} + +/* +================== +BotGetLongTermGoal + +we could also create a seperate AI node for every long term goal type +however this saves us a lot of code +================== +*/ +int BotGetLongTermGoal( bot_state_t *bs, int tfl, int retreat, bot_goal_t *goal ) { + vec3_t target, dir; + char netname[MAX_NETNAME]; + char buf[MAX_MESSAGE_SIZE]; + int areanum; + float croucher; + aas_entityinfo_t entinfo; + bot_waypoint_t *wp; + + if ( bs->ltgtype == LTG_TEAMHELP && !retreat ) { + //check for bot typing status message + if ( bs->teammessage_time && bs->teammessage_time < trap_AAS_Time() ) { + BotAI_BotInitialChat( bs, "help_start", EasyClientName( bs->teammate, netname, sizeof( netname ) ), NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + bs->teammessage_time = 0; + } + //if trying to help the team mate for more than a minute + if ( bs->teamgoal_time < trap_AAS_Time() ) { + bs->ltgtype = 0; + } + //if the team mate IS visible for quite some time + if ( bs->teammatevisible_time < trap_AAS_Time() - 10 ) { + bs->ltgtype = 0; + } + //get entity information of the companion + BotEntityInfo( bs->teammate, &entinfo ); + //if the team mate is visible + if ( BotEntityVisible( bs->entitynum, bs->eye, bs->viewangles, 360, bs->teammate ) ) { + //if close just stand still there + VectorSubtract( entinfo.origin, bs->origin, dir ); + if ( VectorLength( dir ) < 100 ) { + trap_BotResetAvoidReach( bs->ms ); + return qfalse; + } + } else { + //last time the bot was NOT visible + bs->teammatevisible_time = trap_AAS_Time(); + } + //if the entity information is valid (entity in PVS) + if ( entinfo.valid ) { + areanum = BotPointAreaNum( entinfo.origin ); + if ( areanum && trap_AAS_AreaReachability( areanum ) ) { + //update team goal + bs->teamgoal.entitynum = bs->teammate; + bs->teamgoal.areanum = areanum; + VectorCopy( entinfo.origin, bs->teamgoal.origin ); + VectorSet( bs->teamgoal.mins, -8, -8, -8 ); + VectorSet( bs->teamgoal.maxs, 8, 8, 8 ); + } + } + memcpy( goal, &bs->teamgoal, sizeof( bot_goal_t ) ); + return qtrue; + } + //if the bot accompanies someone + if ( bs->ltgtype == LTG_TEAMACCOMPANY && !retreat ) { + //check for bot typing status message + if ( bs->teammessage_time && bs->teammessage_time < trap_AAS_Time() ) { + BotAI_BotInitialChat( bs, "accompany_start", EasyClientName( bs->teammate, netname, sizeof( netname ) ), NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + bs->teammessage_time = 0; + } + //if accompanying the companion for 3 minutes + if ( bs->teamgoal_time < trap_AAS_Time() ) { + BotAI_BotInitialChat( bs, "accompany_stop", EasyClientName( bs->teammate, netname, sizeof( netname ) ), NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + bs->ltgtype = 0; + } + //get entity information of the companion + BotEntityInfo( bs->teammate, &entinfo ); + //if the companion is visible + if ( BotEntityVisible( bs->entitynum, bs->eye, bs->viewangles, 360, bs->teammate ) ) { + //update visible time + bs->teammatevisible_time = trap_AAS_Time(); + VectorSubtract( entinfo.origin, bs->origin, dir ); + if ( VectorLength( dir ) < bs->formation_dist ) { + //check if the bot wants to crouch + //don't crouch if crouched less than 5 seconds ago + if ( bs->attackcrouch_time < trap_AAS_Time() - 5 ) { + croucher = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_CROUCHER, 0, 1 ); + if ( random() < bs->thinktime * croucher ) { + bs->attackcrouch_time = trap_AAS_Time() + 5 + croucher * 15; + } + } + //don't crouch when swimming + if ( trap_AAS_Swimming( bs->origin ) ) { + bs->attackcrouch_time = trap_AAS_Time() - 1; + } + //if not arrived yet or arived some time ago + if ( bs->arrive_time < trap_AAS_Time() - 2 ) { + //if not arrived yet + if ( !bs->arrive_time ) { + trap_EA_Gesture( bs->client ); + BotAI_BotInitialChat( bs, "accompany_arrive", EasyClientName( bs->teammate, netname, sizeof( netname ) ), NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + bs->arrive_time = trap_AAS_Time(); + } + //if the bot wants to crouch + else if ( bs->attackcrouch_time > trap_AAS_Time() ) { + trap_EA_Crouch( bs->client ); + } + //else do some model taunts + else if ( random() < bs->thinktime * 0.3 ) { + //do a gesture :) + trap_EA_Gesture( bs->client ); + } + } + //if just arrived look at the companion + if ( bs->arrive_time > trap_AAS_Time() - 2 ) { + VectorSubtract( entinfo.origin, bs->origin, dir ); + vectoangles( dir, bs->ideal_viewangles ); + bs->ideal_viewangles[2] *= 0.5; + } + //else look strategically around for enemies + else if ( random() < bs->thinktime * 0.8 ) { + BotRoamGoal( bs, target ); + VectorSubtract( target, bs->origin, dir ); + vectoangles( dir, bs->ideal_viewangles ); + bs->ideal_viewangles[2] *= 0.5; + } + //check if the bot wants to go for air + if ( BotGoForAir( bs, bs->tfl, &bs->teamgoal, 400 ) ) { + trap_BotResetLastAvoidReach( bs->ms ); + //get the goal at the top of the stack + //trap_BotGetTopGoal(bs->gs, &tmpgoal); + //trap_BotGoalName(tmpgoal.number, buf, 144); + //BotAI_Print(PRT_MESSAGE, "new nearby goal %s\n", buf); + //time the bot gets to pick up the nearby goal item + bs->nbg_time = trap_AAS_Time() + 8; + AIEnter_Seek_NBG( bs ); + return qfalse; + } + // + trap_BotResetAvoidReach( bs->ms ); + return qfalse; + } + } + //if the entity information is valid (entity in PVS) + if ( entinfo.valid ) { + areanum = BotPointAreaNum( entinfo.origin ); + if ( areanum && trap_AAS_AreaReachability( areanum ) ) { + //update team goal so bot will accompany + bs->teamgoal.entitynum = bs->teammate; + bs->teamgoal.areanum = areanum; + VectorCopy( entinfo.origin, bs->teamgoal.origin ); + VectorSet( bs->teamgoal.mins, -8, -8, -8 ); + VectorSet( bs->teamgoal.maxs, 8, 8, 8 ); + } + } + //the goal the bot should go for + memcpy( goal, &bs->teamgoal, sizeof( bot_goal_t ) ); + //if the companion is NOT visible for too long + if ( bs->teammatevisible_time < trap_AAS_Time() - 60 ) { + BotAI_BotInitialChat( bs, "accompany_cannotfind", EasyClientName( bs->teammate, netname, sizeof( netname ) ), NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + bs->ltgtype = 0; + } + return qtrue; + } + // + if ( bs->ltgtype == LTG_DEFENDKEYAREA ) { + if ( trap_AAS_AreaTravelTimeToGoalArea( bs->areanum, bs->origin, + bs->teamgoal.areanum, TFL_DEFAULT ) > bs->defendaway_range ) { + bs->defendaway_time = 0; + } + } + //if defending a key area + if ( bs->ltgtype == LTG_DEFENDKEYAREA && !retreat && + bs->defendaway_time < trap_AAS_Time() ) { + //check for bot typing status message + if ( bs->teammessage_time && bs->teammessage_time < trap_AAS_Time() ) { + trap_BotGoalName( bs->teamgoal.number, buf, sizeof( buf ) ); + BotAI_BotInitialChat( bs, "defend_start", buf, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + bs->teammessage_time = 0; + } + //set the bot goal + memcpy( goal, &bs->teamgoal, sizeof( bot_goal_t ) ); + //stop after 2 minutes + if ( bs->teamgoal_time < trap_AAS_Time() ) { + trap_BotGoalName( bs->teamgoal.number, buf, sizeof( buf ) ); + BotAI_BotInitialChat( bs, "defend_stop", buf, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + bs->ltgtype = 0; + } + //if very close... go away for some time + VectorSubtract( goal->origin, bs->origin, dir ); + if ( VectorLength( dir ) < 70 ) { + trap_BotResetAvoidReach( bs->ms ); + bs->defendaway_time = trap_AAS_Time() + 2 + 5 * random(); + bs->defendaway_range = 300; + } + return qtrue; + } + //going to kill someone + if ( bs->ltgtype == LTG_KILL && !retreat ) { + //check for bot typing status message + if ( bs->teammessage_time && bs->teammessage_time < trap_AAS_Time() ) { + EasyClientName( bs->teamgoal.entitynum, buf, sizeof( buf ) ); + BotAI_BotInitialChat( bs, "kill_start", buf, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + bs->teammessage_time = 0; + } + // + if ( bs->lastkilledplayer == bs->teamgoal.entitynum ) { + EasyClientName( bs->teamgoal.entitynum, buf, sizeof( buf ) ); + BotAI_BotInitialChat( bs, "kill_done", buf, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + bs->lastkilledplayer = -1; + bs->ltgtype = 0; + } + // + if ( bs->teamgoal_time < trap_AAS_Time() ) { + bs->ltgtype = 0; + } + //just roam around + return BotGetItemLongTermGoal( bs, tfl, goal ); + } + //get an item + if ( bs->ltgtype == LTG_GETITEM && !retreat ) { + //check for bot typing status message + if ( bs->teammessage_time && bs->teammessage_time < trap_AAS_Time() ) { + trap_BotGoalName( bs->teamgoal.number, buf, sizeof( buf ) ); + BotAI_BotInitialChat( bs, "getitem_start", buf, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + bs->teammessage_time = 0; + } + //set the bot goal + memcpy( goal, &bs->teamgoal, sizeof( bot_goal_t ) ); + //stop after some time + if ( bs->teamgoal_time < trap_AAS_Time() ) { + bs->ltgtype = 0; + } + // + if ( trap_BotItemGoalInVisButNotVisible( bs->entitynum, bs->eye, bs->viewangles, goal ) ) { + trap_BotGoalName( bs->teamgoal.number, buf, sizeof( buf ) ); + BotAI_BotInitialChat( bs, "getitem_notthere", buf, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + bs->ltgtype = 0; + } else if ( BotReachedGoal( bs, goal ) ) { + trap_BotGoalName( bs->teamgoal.number, buf, sizeof( buf ) ); + BotAI_BotInitialChat( bs, "getitem_gotit", buf, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + bs->ltgtype = 0; + } + return qtrue; + } + //if camping somewhere + if ( ( bs->ltgtype == LTG_CAMP || bs->ltgtype == LTG_CAMPORDER ) && !retreat ) { + //check for bot typing status message + if ( bs->teammessage_time && bs->teammessage_time < trap_AAS_Time() ) { + if ( bs->ltgtype == LTG_CAMPORDER ) { + BotAI_BotInitialChat( bs, "camp_start", EasyClientName( bs->teammate, netname, sizeof( netname ) ), NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + } + bs->teammessage_time = 0; + } + //set the bot goal + memcpy( goal, &bs->teamgoal, sizeof( bot_goal_t ) ); + // + if ( bs->teamgoal_time < trap_AAS_Time() ) { + if ( bs->ltgtype == LTG_CAMPORDER ) { + BotAI_BotInitialChat( bs, "camp_stop", NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + } + bs->ltgtype = 0; + } + //if really near the camp spot + VectorSubtract( goal->origin, bs->origin, dir ); + if ( VectorLength( dir ) < 60 ) { + //if not arrived yet + if ( !bs->arrive_time ) { + if ( bs->ltgtype == LTG_CAMPORDER ) { + BotAI_BotInitialChat( bs, "camp_arrive", EasyClientName( bs->teammate, netname, sizeof( netname ) ), NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + } + bs->arrive_time = trap_AAS_Time(); + } + //look strategically around for enemies + if ( random() < bs->thinktime * 0.8 ) { + BotRoamGoal( bs, target ); + VectorSubtract( target, bs->origin, dir ); + vectoangles( dir, bs->ideal_viewangles ); + bs->ideal_viewangles[2] *= 0.5; + } + //check if the bot wants to crouch + //don't crouch if crouched less than 5 seconds ago + if ( bs->attackcrouch_time < trap_AAS_Time() - 5 ) { + croucher = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_CROUCHER, 0, 1 ); + if ( random() < bs->thinktime * croucher ) { + bs->attackcrouch_time = trap_AAS_Time() + 5 + croucher * 15; + } + } + //if the bot wants to crouch + if ( bs->attackcrouch_time > trap_AAS_Time() ) { + trap_EA_Crouch( bs->client ); + } + //don't crouch when swimming + if ( trap_AAS_Swimming( bs->origin ) ) { + bs->attackcrouch_time = trap_AAS_Time() - 1; + } + //make sure the bot is not gonna drown + if ( trap_PointContents( bs->eye,bs->entitynum ) & ( CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ) ) { + if ( bs->ltgtype == LTG_CAMPORDER ) { + BotAI_BotInitialChat( bs, "camp_stop", NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + } + bs->ltgtype = 0; + } + // + if ( bs->camp_range > 0 ) { + //FIXME: move around a bit + } + // + trap_BotResetAvoidReach( bs->ms ); + return qfalse; + } + return qtrue; + } + //patrolling along several waypoints + if ( bs->ltgtype == LTG_PATROL && !retreat ) { + //check for bot typing status message + if ( bs->teammessage_time && bs->teammessage_time < trap_AAS_Time() ) { + strcpy( buf, "" ); + for ( wp = bs->patrolpoints; wp; wp = wp->next ) { + strcat( buf, wp->name ); + if ( wp->next ) { + strcat( buf, " to " ); + } + } + BotAI_BotInitialChat( bs, "patrol_start", buf, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + bs->teammessage_time = 0; + } + // + if ( !bs->curpatrolpoint ) { + bs->ltgtype = 0; + return qfalse; + } + //if the bot touches the current goal + if ( trap_BotTouchingGoal( bs->origin, &bs->curpatrolpoint->goal ) ) { + if ( bs->patrolflags & PATROL_BACK ) { + if ( bs->curpatrolpoint->prev ) { + bs->curpatrolpoint = bs->curpatrolpoint->prev; + } else { + bs->curpatrolpoint = bs->curpatrolpoint->next; + bs->patrolflags &= ~PATROL_BACK; + } + } else { + if ( bs->curpatrolpoint->next ) { + bs->curpatrolpoint = bs->curpatrolpoint->next; + } else { + bs->curpatrolpoint = bs->curpatrolpoint->prev; + bs->patrolflags |= PATROL_BACK; + } + } + } + //stop after 5 minutes + if ( bs->teamgoal_time < trap_AAS_Time() ) { + BotAI_BotInitialChat( bs, "patrol_stop", NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + bs->ltgtype = 0; + } + if ( !bs->curpatrolpoint ) { + bs->ltgtype = 0; + return qfalse; + } + memcpy( goal, &bs->curpatrolpoint->goal, sizeof( bot_goal_t ) ); + return qtrue; + } +#ifdef CTF + //if going for enemy flag + if ( bs->ltgtype == LTG_GETFLAG ) { + //check for bot typing status message + if ( bs->teammessage_time && bs->teammessage_time < trap_AAS_Time() ) { + BotAI_BotInitialChat( bs, "captureflag_start", NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + bs->teammessage_time = 0; + } + // + switch ( BotCTFTeam( bs ) ) { + case CTF_TEAM_RED: *goal = ctf_blueflag; break; + case CTF_TEAM_BLUE: *goal = ctf_redflag; break; + default: bs->ltgtype = 0; return qfalse; + } + //if touching the flag + if ( trap_BotTouchingGoal( bs->origin, goal ) ) { + bs->ltgtype = 0; + } + //stop after 3 minutes + if ( bs->teamgoal_time < trap_AAS_Time() ) { +#ifdef DEBUG + BotAI_Print( PRT_MESSAGE, "%s: I quit getting the flag\n", ClientName( bs->client, netname, sizeof( netname ) ) ); +#endif //DEBUG + bs->ltgtype = 0; + } + return qtrue; + } + //if rushing to the base + if ( bs->ltgtype == LTG_RUSHBASE && bs->rushbaseaway_time < trap_AAS_Time() ) { + switch ( BotCTFTeam( bs ) ) { + case CTF_TEAM_RED: *goal = ctf_redflag; break; + case CTF_TEAM_BLUE: *goal = ctf_blueflag; break; + default: bs->ltgtype = 0; return qfalse; + } + //quit rushing after 2 minutes + if ( bs->teamgoal_time < trap_AAS_Time() ) { + bs->ltgtype = 0; + } + //if touching the base flag the bot should loose the enemy flag + if ( trap_BotTouchingGoal( bs->origin, goal ) ) { + //if the bot is still carrying the enemy flag then the + //base flag is gone, now just walk near the base a bit + if ( BotCTFCarryingFlag( bs ) ) { + trap_BotResetAvoidReach( bs->ms ); + bs->rushbaseaway_time = trap_AAS_Time() + 5 + 10 * random(); + //FIXME: add chat to tell the others to get back the flag + } else { + bs->ltgtype = 0; + } + } + return qtrue; + } + //returning flag + if ( bs->ltgtype == LTG_RETURNFLAG ) { + //check for bot typing status message + if ( bs->teammessage_time && bs->teammessage_time < trap_AAS_Time() ) { + EasyClientName( bs->teamgoal.entitynum, buf, sizeof( buf ) ); + BotAI_BotInitialChat( bs, "returnflag_start", buf, NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + bs->teammessage_time = 0; + } + // + if ( bs->teamgoal_time < trap_AAS_Time() ) { + bs->ltgtype = 0; + } + //just roam around + return BotGetItemLongTermGoal( bs, tfl, goal ); + } +#endif //CTF + //normal goal stuff + return BotGetItemLongTermGoal( bs, tfl, goal ); +} + +/* +================== +BotLongTermGoal +================== +*/ +int BotLongTermGoal( bot_state_t *bs, int tfl, int retreat, bot_goal_t *goal ) { + aas_entityinfo_t entinfo; + char teammate[MAX_MESSAGE_SIZE]; + float dist; + int areanum; + vec3_t dir; + + //FIXME: also have air long term goals? + // + //if the bot is leading someone and not retreating + if ( bs->lead_time > 0 && !retreat ) { + if ( bs->lead_time < trap_AAS_Time() ) { + //FIXME: add chat to tell the team mate that he/she's on his/her own + bs->lead_time = 0; + return BotGetLongTermGoal( bs, tfl, retreat, goal ); + } + // + if ( bs->leadmessage_time < 0 && -bs->leadmessage_time < trap_AAS_Time() ) { + BotAI_BotInitialChat( bs, "followme", EasyClientName( bs->lead_teammate, teammate, sizeof( teammate ) ), NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + bs->leadmessage_time = trap_AAS_Time(); + } + //get entity information of the companion + BotEntityInfo( bs->lead_teammate, &entinfo ); + // + if ( entinfo.valid ) { + areanum = BotPointAreaNum( entinfo.origin ); + if ( areanum && trap_AAS_AreaReachability( areanum ) ) { + //update team goal + bs->lead_teamgoal.entitynum = bs->lead_teammate; + bs->lead_teamgoal.areanum = areanum; + VectorCopy( entinfo.origin, bs->lead_teamgoal.origin ); + VectorSet( bs->lead_teamgoal.mins, -8, -8, -8 ); + VectorSet( bs->lead_teamgoal.maxs, 8, 8, 8 ); + } + } + //if the team mate is visible + if ( BotEntityVisible( bs->entitynum, bs->eye, bs->viewangles, 360, bs->lead_teammate ) ) { + bs->leadvisible_time = trap_AAS_Time(); + } + //if the team mate is not visible for 1 seconds + if ( bs->leadvisible_time < trap_AAS_Time() - 1 ) { + bs->leadbackup_time = trap_AAS_Time() + 2; + } + //distance towards the team mate + VectorSubtract( bs->origin, bs->lead_teamgoal.origin, dir ); + dist = VectorLength( dir ); + //if backing up towards the team mate + if ( bs->leadbackup_time > trap_AAS_Time() ) { + if ( bs->leadmessage_time < trap_AAS_Time() - 20 ) { + BotAI_BotInitialChat( bs, "followme", EasyClientName( bs->lead_teammate, teammate, sizeof( teammate ) ), NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + bs->leadmessage_time = trap_AAS_Time(); + } + //if very close to the team mate + if ( dist < 100 ) { + bs->leadbackup_time = 0; + } + //the bot should go back to the team mate + memcpy( goal, &bs->lead_teamgoal, sizeof( bot_goal_t ) ); + return qtrue; + } else { + //if quite distant from the team mate + if ( dist > 500 ) { + if ( bs->leadmessage_time < trap_AAS_Time() - 20 ) { + BotAI_BotInitialChat( bs, "followme", EasyClientName( bs->lead_teammate, teammate, sizeof( teammate ) ), NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + bs->leadmessage_time = trap_AAS_Time(); + } + //look at the team mate + VectorSubtract( entinfo.origin, bs->origin, dir ); + vectoangles( dir, bs->ideal_viewangles ); + bs->ideal_viewangles[2] *= 0.5; + //just wait for the team mate + return qfalse; + } + } + } + return BotGetLongTermGoal( bs, tfl, retreat, goal ); +} + +/* +================== +AIEnter_Intermission +================== +*/ +void AIEnter_Intermission( bot_state_t *bs ) { + BotRecordNodeSwitch( bs, "intermission", "" ); + //reset the bot state + BotResetState( bs ); + //check for end level chat + if ( BotChat_EndLevel( bs ) ) { + trap_BotEnterChat( bs->cs, bs->client, bs->chatto ); + } + bs->ainode = AINode_Intermission; +} + +/* +================== +AINode_Intermission +================== +*/ +int AINode_Intermission( bot_state_t *bs ) { + //if the intermission ended + if ( !BotIntermission( bs ) ) { + if ( BotChat_StartLevel( bs ) ) { + bs->stand_time = trap_AAS_Time() + BotChatTime( bs ); + } else { + bs->stand_time = trap_AAS_Time() + 2; + } + AIEnter_Stand( bs ); + } + return qtrue; +} + +/* +================== +AIEnter_Observer +================== +*/ +void AIEnter_Observer( bot_state_t *bs ) { + BotRecordNodeSwitch( bs, "observer", "" ); + //reset the bot state + BotResetState( bs ); + bs->ainode = AINode_Observer; +} + +/* +================== +AINode_Observer +================== +*/ +int AINode_Observer( bot_state_t *bs ) { + //if the bot left observer mode + if ( !BotIsObserver( bs ) ) { + AIEnter_Stand( bs ); + } + return qtrue; +} + +/* +================== +AIEnter_Stand +================== +*/ +void AIEnter_Stand( bot_state_t *bs ) { + BotRecordNodeSwitch( bs, "stand", "" ); + bs->standfindenemy_time = trap_AAS_Time() + 1; + bs->ainode = AINode_Stand; +} + +/* +================== +AINode_Stand +================== +*/ +int AINode_Stand( bot_state_t *bs ) { + + //if the bot's health decreased + if ( bs->lastframe_health > bs->inventory[INVENTORY_HEALTH] ) { + if ( BotChat_HitTalking( bs ) ) { + bs->standfindenemy_time = trap_AAS_Time() + BotChatTime( bs ) + 0.1; + bs->stand_time = trap_AAS_Time() + BotChatTime( bs ) + 0.1; + } + } + if ( bs->standfindenemy_time < trap_AAS_Time() ) { + if ( BotFindEnemy( bs, -1 ) ) { + AIEnter_Battle_Fight( bs ); + return qfalse; + } + bs->standfindenemy_time = trap_AAS_Time() + 1; + } + trap_EA_Talk( bs->client ); + if ( bs->stand_time < trap_AAS_Time() ) { + trap_BotEnterChat( bs->cs, bs->client, bs->chatto ); + AIEnter_Seek_LTG( bs ); + return qfalse; + } + // + return qtrue; +} + +/* +================== +AIEnter_Respawn +================== +*/ +void AIEnter_Respawn( bot_state_t *bs ) { + BotRecordNodeSwitch( bs, "respawn", "" ); + //reset some states + trap_BotResetMoveState( bs->ms ); + trap_BotResetGoalState( bs->gs ); + trap_BotResetAvoidGoals( bs->gs ); + trap_BotResetAvoidReach( bs->ms ); + //if the bot wants to chat + if ( BotChat_Death( bs ) ) { + bs->respawn_time = trap_AAS_Time() + BotChatTime( bs ); + bs->respawnchat_time = trap_AAS_Time(); + } else { + bs->respawn_time = trap_AAS_Time() + 1 + random(); + bs->respawnchat_time = 0; + } + //set respawn state + bs->respawn_wait = qfalse; + bs->ainode = AINode_Respawn; +} + +/* +================== +AINode_Respawn +================== +*/ +int AINode_Respawn( bot_state_t *bs ) { + if ( bs->respawn_wait ) { + if ( !BotIsDead( bs ) ) { + AIEnter_Seek_LTG( bs ); + } else { + trap_EA_Respawn( bs->client ); + } + } else if ( bs->respawn_time < trap_AAS_Time() ) { + //wait until respawned + bs->respawn_wait = qtrue; + //elementary action respawn + trap_EA_Respawn( bs->client ); + // + if ( bs->respawnchat_time ) { + trap_BotEnterChat( bs->cs, bs->client, bs->chatto ); + bs->enemy = -1; + } + } + if ( bs->respawnchat_time && bs->respawnchat_time < trap_AAS_Time() - 0.5 ) { + trap_EA_Talk( bs->client ); + } + // + return qtrue; +} + +/* +================== +AIEnter_Seek_ActivateEntity +================== +*/ +void AIEnter_Seek_ActivateEntity( bot_state_t *bs ) { + BotRecordNodeSwitch( bs, "activate entity", "" ); + bs->ainode = AINode_Seek_ActivateEntity; +} + +/* +================== +AINode_Seek_Activate_Entity +================== +*/ +int AINode_Seek_ActivateEntity( bot_state_t *bs ) { + bot_goal_t *goal; + vec3_t target, dir; + bot_moveresult_t moveresult; + + if ( BotIsObserver( bs ) ) { + AIEnter_Observer( bs ); + return qfalse; + } + //if in the intermission + if ( BotIntermission( bs ) ) { + AIEnter_Intermission( bs ); + return qfalse; + } + //respawn if dead + if ( BotIsDead( bs ) ) { + AIEnter_Respawn( bs ); + return qfalse; + } + // + bs->tfl = TFL_DEFAULT; + if ( bot_grapple.integer ) { + bs->tfl |= TFL_GRAPPLEHOOK; + } + //if in lava or slime the bot should be able to get out + if ( BotInLava( bs ) ) { + bs->tfl |= TFL_LAVA; + } + if ( BotInSlime( bs ) ) { + bs->tfl |= TFL_SLIME; + } + //map specific code + BotMapScripts( bs ); + //no enemy + bs->enemy = -1; + // + goal = &bs->activategoal; + //if the bot has no goal + if ( !goal ) { + bs->activate_time = 0; + } + //if the bot touches the current goal + else if ( trap_BotTouchingGoal( bs->origin, goal ) ) { + BotChooseWeapon( bs ); +#ifdef DEBUG + BotAI_Print( PRT_MESSAGE, "touched button or trigger\n" ); +#endif //DEBUG + bs->activate_time = 0; + } + // + if ( bs->activate_time < trap_AAS_Time() ) { + AIEnter_Seek_NBG( bs ); + return qfalse; + } + //initialize the movement state + BotSetupForMovement( bs ); + //move towards the goal + trap_BotMoveToGoal( &moveresult, bs->ms, goal, bs->tfl ); + //if the movement failed + if ( moveresult.failure ) { + //reset the avoid reach, otherwise bot is stuck in current area + trap_BotResetAvoidReach( bs->ms ); + bs->nbg_time = 0; + } + //check if the bot is blocked + BotAIBlocked( bs, &moveresult, qtrue ); + // + if ( moveresult.flags & ( MOVERESULT_MOVEMENTVIEWSET | MOVERESULT_MOVEMENTVIEW | MOVERESULT_SWIMVIEW ) ) { + VectorCopy( moveresult.ideal_viewangles, bs->ideal_viewangles ); + } + //if waiting for something + else if ( moveresult.flags & MOVERESULT_WAITING ) { + if ( random() < bs->thinktime * 0.8 ) { + BotRoamGoal( bs, target ); + VectorSubtract( target, bs->origin, dir ); + vectoangles( dir, bs->ideal_viewangles ); + bs->ideal_viewangles[2] *= 0.5; + } + } else if ( !( bs->flags & BFL_IDEALVIEWSET ) ) { + if ( trap_BotMovementViewTarget( bs->ms, goal, bs->tfl, 300, target ) ) { + VectorSubtract( target, bs->origin, dir ); + vectoangles( dir, bs->ideal_viewangles ); + } else { + //vectoangles(moveresult.movedir, bs->ideal_viewangles); + } + bs->ideal_viewangles[2] *= 0.5; + } + //if the weapon is used for the bot movement + if ( moveresult.flags & MOVERESULT_MOVEMENTWEAPON ) { + bs->weaponnum = moveresult.weapon; + } + //if there is an enemy + if ( BotFindEnemy( bs, -1 ) ) { + if ( BotWantsToRetreat( bs ) ) { + //keep the current long term goal and retreat + AIEnter_Battle_NBG( bs ); + } else { + trap_BotResetLastAvoidReach( bs->ms ); + //empty the goal stack + trap_BotEmptyGoalStack( bs->gs ); + //go fight + AIEnter_Battle_Fight( bs ); + } + } + return qtrue; +} + +/* +================== +AIEnter_Seek_NBG +================== +*/ +void AIEnter_Seek_NBG( bot_state_t *bs ) { + bot_goal_t goal; + char buf[144]; + + if ( trap_BotGetTopGoal( bs->gs, &goal ) ) { + trap_BotGoalName( goal.number, buf, 144 ); + BotRecordNodeSwitch( bs, "seek NBG", buf ); + } else { + BotRecordNodeSwitch( bs, "seek NBG", "no goal" ); + } + bs->ainode = AINode_Seek_NBG; +} + +/* +================== +AINode_Seek_NBG +================== +*/ +int AINode_Seek_NBG( bot_state_t *bs ) { + bot_goal_t goal; + vec3_t target, dir; + bot_moveresult_t moveresult; + + if ( BotIsObserver( bs ) ) { + AIEnter_Observer( bs ); + return qfalse; + } + //if in the intermission + if ( BotIntermission( bs ) ) { + AIEnter_Intermission( bs ); + return qfalse; + } + //respawn if dead + if ( BotIsDead( bs ) ) { + AIEnter_Respawn( bs ); + return qfalse; + } + // + bs->tfl = TFL_DEFAULT; + if ( bot_grapple.integer ) { + bs->tfl |= TFL_GRAPPLEHOOK; + } + //if in lava or slime the bot should be able to get out + if ( BotInLava( bs ) ) { + bs->tfl |= TFL_LAVA; + } + if ( BotInSlime( bs ) ) { + bs->tfl |= TFL_SLIME; + } + // + if ( BotCanAndWantsToRocketJump( bs ) ) { + bs->tfl |= TFL_ROCKETJUMP; + } + //map specific code + BotMapScripts( bs ); + //no enemy + bs->enemy = -1; + //if the bot has no goal + if ( !trap_BotGetTopGoal( bs->gs, &goal ) ) { + bs->nbg_time = 0; + } + //if the bot touches the current goal + else if ( BotReachedGoal( bs, &goal ) ) { + BotChooseWeapon( bs ); + bs->nbg_time = 0; + } + // + if ( bs->nbg_time < trap_AAS_Time() ) { + //pop the current goal from the stack + trap_BotPopGoal( bs->gs ); + //check for new nearby items right away + //NOTE: we canNOT reset the check_time to zero because it would create an endless loop of node switches + bs->check_time = trap_AAS_Time() + 0.05; + //go back to seek ltg + AIEnter_Seek_LTG( bs ); + return qfalse; + } + //initialize the movement state + BotSetupForMovement( bs ); + //move towards the goal + trap_BotMoveToGoal( &moveresult, bs->ms, &goal, bs->tfl ); + //if the movement failed + if ( moveresult.failure ) { + //reset the avoid reach, otherwise bot is stuck in current area + trap_BotResetAvoidReach( bs->ms ); + bs->nbg_time = 0; + } + //check if the bot is blocked + BotAIBlocked( bs, &moveresult, qtrue ); + //if the viewangles are used for the movement + if ( moveresult.flags & ( MOVERESULT_MOVEMENTVIEWSET | MOVERESULT_MOVEMENTVIEW | MOVERESULT_SWIMVIEW ) ) { + VectorCopy( moveresult.ideal_viewangles, bs->ideal_viewangles ); + } + //if waiting for something + else if ( moveresult.flags & MOVERESULT_WAITING ) { + if ( random() < bs->thinktime * 0.8 ) { + BotRoamGoal( bs, target ); + VectorSubtract( target, bs->origin, dir ); + vectoangles( dir, bs->ideal_viewangles ); + bs->ideal_viewangles[2] *= 0.5; + } + } else if ( !( bs->flags & BFL_IDEALVIEWSET ) ) { + if ( !trap_BotGetSecondGoal( bs->gs, &goal ) ) { + trap_BotGetTopGoal( bs->gs, &goal ); + } + if ( trap_BotMovementViewTarget( bs->ms, &goal, bs->tfl, 300, target ) ) { + VectorSubtract( target, bs->origin, dir ); + vectoangles( dir, bs->ideal_viewangles ); + } + //FIXME: look at cluster portals? + else {vectoangles( moveresult.movedir, bs->ideal_viewangles );} + bs->ideal_viewangles[2] *= 0.5; + } + //if the weapon is used for the bot movement + if ( moveresult.flags & MOVERESULT_MOVEMENTWEAPON ) { + bs->weaponnum = moveresult.weapon; + } + //if there is an enemy + if ( BotFindEnemy( bs, -1 ) ) { + if ( BotWantsToRetreat( bs ) ) { + //keep the current long term goal and retreat + AIEnter_Battle_NBG( bs ); + } else { + trap_BotResetLastAvoidReach( bs->ms ); + //empty the goal stack + trap_BotEmptyGoalStack( bs->gs ); + //go fight + AIEnter_Battle_Fight( bs ); + } + } + return qtrue; +} + +/* +================== +AIEnter_Seek_LTG +================== +*/ +void AIEnter_Seek_LTG( bot_state_t *bs ) { + bot_goal_t goal; + char buf[144]; + + if ( trap_BotGetTopGoal( bs->gs, &goal ) ) { + trap_BotGoalName( goal.number, buf, 144 ); + BotRecordNodeSwitch( bs, "seek LTG", buf ); + } else { + BotRecordNodeSwitch( bs, "seek LTG", "no goal" ); + } + bs->ainode = AINode_Seek_LTG; +} + +/* +================== +AINode_Seek_LTG +================== +*/ +int AINode_Seek_LTG( bot_state_t *bs ) { + bot_goal_t goal; + vec3_t target, dir; + bot_moveresult_t moveresult; + int range; + //char buf[128]; + //bot_goal_t tmpgoal; + + if ( BotIsObserver( bs ) ) { + AIEnter_Observer( bs ); + return qfalse; + } + //if in the intermission + if ( BotIntermission( bs ) ) { + AIEnter_Intermission( bs ); + return qfalse; + } + //respawn if dead + if ( BotIsDead( bs ) ) { + AIEnter_Respawn( bs ); + return qfalse; + } + // + if ( BotChat_Random( bs ) ) { + bs->stand_time = trap_AAS_Time() + BotChatTime( bs ); + AIEnter_Stand( bs ); + return qfalse; + } + // + bs->tfl = TFL_DEFAULT; + if ( bot_grapple.integer ) { + bs->tfl |= TFL_GRAPPLEHOOK; + } + //if in lava or slime the bot should be able to get out + if ( BotInLava( bs ) ) { + bs->tfl |= TFL_LAVA; + } + if ( BotInSlime( bs ) ) { + bs->tfl |= TFL_SLIME; + } + // + if ( BotCanAndWantsToRocketJump( bs ) ) { + bs->tfl |= TFL_ROCKETJUMP; + } + //map specific code + BotMapScripts( bs ); + //no enemy + bs->enemy = -1; + // + if ( bs->killedenemy_time > trap_AAS_Time() - 2 ) { + if ( random() < bs->thinktime * 1 ) { + trap_EA_Gesture( bs->client ); + } + } + //if there is an enemy + if ( BotFindEnemy( bs, -1 ) ) { + if ( BotWantsToRetreat( bs ) ) { + //keep the current long term goal and retreat + AIEnter_Battle_Retreat( bs ); + return qfalse; + } else { + trap_BotResetLastAvoidReach( bs->ms ); + //empty the goal stack + trap_BotEmptyGoalStack( bs->gs ); + //go fight + AIEnter_Battle_Fight( bs ); + return qfalse; + } + } +#ifdef CTF + if ( gametype == GT_CTF ) { + //decide what to do in CTF mode + BotCTFSeekGoals( bs ); + } +#endif //CTF + //get the current long term goal + if ( !BotLongTermGoal( bs, bs->tfl, qfalse, &goal ) ) { + return qtrue; + } + //check for nearby goals periodicly + if ( bs->check_time < trap_AAS_Time() ) { + bs->check_time = trap_AAS_Time() + 0.5; + //check if the bot wants to camp + BotWantsToCamp( bs ); + // + if ( bs->ltgtype == LTG_DEFENDKEYAREA ) { + range = 400; + } else { range = 150;} + // +#ifdef CTF + //if carrying a flag the bot shouldn't be distracted too much + if ( BotCTFCarryingFlag( bs ) ) { + range = 50; + } +#endif //CTF + // + if ( BotNearbyGoal( bs, bs->tfl, &goal, range ) ) { + trap_BotResetLastAvoidReach( bs->ms ); + //get the goal at the top of the stack + //trap_BotGetTopGoal(bs->gs, &tmpgoal); + //trap_BotGoalName(tmpgoal.number, buf, 144); + //BotAI_Print(PRT_MESSAGE, "new nearby goal %s\n", buf); + //time the bot gets to pick up the nearby goal item + bs->nbg_time = trap_AAS_Time() + 4 + range * 0.01; + AIEnter_Seek_NBG( bs ); + return qfalse; + } + } + //initialize the movement state + BotSetupForMovement( bs ); + //move towards the goal + trap_BotMoveToGoal( &moveresult, bs->ms, &goal, bs->tfl ); + //if the movement failed + if ( moveresult.failure ) { + //reset the avoid reach, otherwise bot is stuck in current area + trap_BotResetAvoidReach( bs->ms ); + //BotAI_Print(PRT_MESSAGE, "movement failure %d\n", moveresult.traveltype); + bs->ltg_time = 0; + } + // + BotAIBlocked( bs, &moveresult, qtrue ); + //if the viewangles are used for the movement + if ( moveresult.flags & ( MOVERESULT_MOVEMENTVIEWSET | MOVERESULT_MOVEMENTVIEW | MOVERESULT_SWIMVIEW ) ) { + VectorCopy( moveresult.ideal_viewangles, bs->ideal_viewangles ); + } + //if waiting for something + else if ( moveresult.flags & MOVERESULT_WAITING ) { + if ( random() < bs->thinktime * 0.8 ) { + BotRoamGoal( bs, target ); + VectorSubtract( target, bs->origin, dir ); + vectoangles( dir, bs->ideal_viewangles ); + bs->ideal_viewangles[2] *= 0.5; + } + } else if ( !( bs->flags & BFL_IDEALVIEWSET ) ) { + if ( trap_BotMovementViewTarget( bs->ms, &goal, bs->tfl, 300, target ) ) { + VectorSubtract( target, bs->origin, dir ); + vectoangles( dir, bs->ideal_viewangles ); + } + //FIXME: look at cluster portals? + else if ( VectorLength( moveresult.movedir ) ) { + vectoangles( moveresult.movedir, bs->ideal_viewangles ); + } else if ( random() < bs->thinktime * 0.8 ) { + BotRoamGoal( bs, target ); + VectorSubtract( target, bs->origin, dir ); + vectoangles( dir, bs->ideal_viewangles ); + bs->ideal_viewangles[2] *= 0.5; + } + bs->ideal_viewangles[2] *= 0.5; + } + //if the weapon is used for the bot movement + if ( moveresult.flags & MOVERESULT_MOVEMENTWEAPON ) { + bs->weaponnum = moveresult.weapon; + } + // + return qtrue; +} + +/* +================== +AIEnter_Battle_Fight +================== +*/ +void AIEnter_Battle_Fight( bot_state_t *bs ) { + BotRecordNodeSwitch( bs, "battle fight", "" ); + trap_BotResetLastAvoidReach( bs->ms ); + bs->ainode = AINode_Battle_Fight; +} + +/* +================== +AINode_Battle_Fight +================== +*/ +int AINode_Battle_Fight( bot_state_t *bs ) { + int areanum; + aas_entityinfo_t entinfo; + bot_moveresult_t moveresult; + + if ( BotIsObserver( bs ) ) { + AIEnter_Observer( bs ); + return qfalse; + } + //if in the intermission + if ( BotIntermission( bs ) ) { + AIEnter_Intermission( bs ); + return qfalse; + } + //respawn if dead + if ( BotIsDead( bs ) ) { + AIEnter_Respawn( bs ); + return qfalse; + } + //if no enemy + if ( bs->enemy < 0 ) { + AIEnter_Seek_LTG( bs ); + return qfalse; + } + // + BotEntityInfo( bs->enemy, &entinfo ); + //if the enemy is dead + if ( bs->enemydeath_time ) { + if ( bs->enemydeath_time < trap_AAS_Time() - 1.5 ) { + bs->enemydeath_time = 0; + if ( bs->enemysuicide ) { + BotChat_EnemySuicide( bs ); + } + if ( bs->lastkilledplayer == bs->enemy && BotChat_Kill( bs ) ) { + bs->stand_time = trap_AAS_Time() + BotChatTime( bs ); + AIEnter_Stand( bs ); + } else { + bs->ltg_time = 0; + AIEnter_Seek_LTG( bs ); + } + return qfalse; + } + } else { + if ( EntityIsDead( &entinfo ) ) { + bs->enemydeath_time = trap_AAS_Time(); + } + } + //if the enemy is invisible and not shooting the bot looses track easily + if ( EntityIsInvisible( &entinfo ) && !EntityIsShooting( &entinfo ) ) { + if ( random() < 0.2 ) { + AIEnter_Seek_LTG( bs ); + return qfalse; + } + } + //update the reachability area and origin if possible + areanum = BotPointAreaNum( entinfo.origin ); + if ( areanum && trap_AAS_AreaReachability( areanum ) ) { + VectorCopy( entinfo.origin, bs->lastenemyorigin ); + bs->lastenemyareanum = areanum; + } + //update the attack inventory values + BotUpdateBattleInventory( bs, bs->enemy ); + //if the bot's health decreased + if ( bs->lastframe_health > bs->inventory[INVENTORY_HEALTH] ) { + if ( BotChat_HitNoDeath( bs ) ) { + bs->stand_time = trap_AAS_Time() + BotChatTime( bs ); + AIEnter_Stand( bs ); + return qfalse; + } + } + //if the bot hit someone + if ( bs->cur_ps.persistant[PERS_HITS] > bs->lasthitcount ) { + if ( BotChat_HitNoKill( bs ) ) { + bs->stand_time = trap_AAS_Time() + BotChatTime( bs ); + AIEnter_Stand( bs ); + return qfalse; + } + } + //if the enemy is not visible + if ( !BotEntityVisible( bs->entitynum, bs->eye, bs->viewangles, 360, bs->enemy ) ) { + if ( BotWantsToChase( bs ) ) { + AIEnter_Battle_Chase( bs ); + return qfalse; + } else { + AIEnter_Seek_LTG( bs ); + return qfalse; + } + } + //use holdable items + BotBattleUseItems( bs ); + // + bs->tfl = TFL_DEFAULT; + if ( bot_grapple.integer ) { + bs->tfl |= TFL_GRAPPLEHOOK; + } + //if in lava or slime the bot should be able to get out + if ( BotInLava( bs ) ) { + bs->tfl |= TFL_LAVA; + } + if ( BotInSlime( bs ) ) { + bs->tfl |= TFL_SLIME; + } + // + if ( BotCanAndWantsToRocketJump( bs ) ) { + bs->tfl |= TFL_ROCKETJUMP; + } + //choose the best weapon to fight with + BotChooseWeapon( bs ); + //do attack movements + moveresult = BotAttackMove( bs, bs->tfl ); + //if the movement failed + if ( moveresult.failure ) { + //reset the avoid reach, otherwise bot is stuck in current area + trap_BotResetAvoidReach( bs->ms ); + //BotAI_Print(PRT_MESSAGE, "movement failure %d\n", moveresult.traveltype); + bs->ltg_time = 0; + } + // + BotAIBlocked( bs, &moveresult, qfalse ); + //aim at the enemy + BotAimAtEnemy( bs ); + //attack the enemy if possible + BotCheckAttack( bs ); + //if the bot wants to retreat + if ( BotWantsToRetreat( bs ) ) { + AIEnter_Battle_Retreat( bs ); + return qtrue; + } + return qtrue; +} + +/* +================== +AIEnter_Battle_Chase +================== +*/ +void AIEnter_Battle_Chase( bot_state_t *bs ) { + BotRecordNodeSwitch( bs, "battle chase", "" ); + bs->chase_time = trap_AAS_Time(); + bs->ainode = AINode_Battle_Chase; +} + +/* +================== +AINode_Battle_Chase +================== +*/ +int AINode_Battle_Chase( bot_state_t *bs ) { + bot_goal_t goal; + vec3_t target, dir; + bot_moveresult_t moveresult; + float range; + + if ( BotIsObserver( bs ) ) { + AIEnter_Observer( bs ); + return qfalse; + } + //if in the intermission + if ( BotIntermission( bs ) ) { + AIEnter_Intermission( bs ); + return qfalse; + } + //respawn if dead + if ( BotIsDead( bs ) ) { + AIEnter_Respawn( bs ); + return qfalse; + } + //if no enemy + if ( bs->enemy < 0 ) { + AIEnter_Seek_LTG( bs ); + return qfalse; + } + //if the enemy is visible + if ( BotEntityVisible( bs->entitynum, bs->eye, bs->viewangles, 360, bs->enemy ) ) { + AIEnter_Battle_Fight( bs ); + return qfalse; + } + //if there is another enemy + if ( BotFindEnemy( bs, -1 ) ) { + AIEnter_Battle_Fight( bs ); + return qfalse; + } + //there is no last enemy area + if ( !bs->lastenemyareanum ) { + AIEnter_Seek_LTG( bs ); + return qfalse; + } + // + bs->tfl = TFL_DEFAULT; + if ( bot_grapple.integer ) { + bs->tfl |= TFL_GRAPPLEHOOK; + } + //if in lava or slime the bot should be able to get out + if ( BotInLava( bs ) ) { + bs->tfl |= TFL_LAVA; + } + if ( BotInSlime( bs ) ) { + bs->tfl |= TFL_SLIME; + } + // + if ( BotCanAndWantsToRocketJump( bs ) ) { + bs->tfl |= TFL_ROCKETJUMP; + } + //map specific code + BotMapScripts( bs ); + //create the chase goal + goal.entitynum = bs->enemy; + goal.areanum = bs->lastenemyareanum; + VectorCopy( bs->lastenemyorigin, goal.origin ); + VectorSet( goal.mins, -8, -8, -8 ); + VectorSet( goal.maxs, 8, 8, 8 ); + //if the last seen enemy spot is reached the enemy could not be found + if ( trap_BotTouchingGoal( bs->origin, &goal ) ) { + bs->chase_time = 0; + } + //if there's no chase time left + if ( !bs->chase_time || bs->chase_time < trap_AAS_Time() - 10 ) { + AIEnter_Seek_LTG( bs ); + return qfalse; + } + //check for nearby goals periodicly + if ( bs->check_time < trap_AAS_Time() ) { + bs->check_time = trap_AAS_Time() + 1; + range = 150; + // + if ( BotNearbyGoal( bs, bs->tfl, &goal, range ) ) { + //the bot gets 5 seconds to pick up the nearby goal item + bs->nbg_time = trap_AAS_Time() + 0.1 * range + 1; + trap_BotResetLastAvoidReach( bs->ms ); + AIEnter_Battle_NBG( bs ); + return qfalse; + } + } + // + BotUpdateBattleInventory( bs, bs->enemy ); + //initialize the movement state + BotSetupForMovement( bs ); + //move towards the goal + trap_BotMoveToGoal( &moveresult, bs->ms, &goal, bs->tfl ); + //if the movement failed + if ( moveresult.failure ) { + //reset the avoid reach, otherwise bot is stuck in current area + trap_BotResetAvoidReach( bs->ms ); + //BotAI_Print(PRT_MESSAGE, "movement failure %d\n", moveresult.traveltype); + bs->ltg_time = 0; + } + // + BotAIBlocked( bs, &moveresult, qfalse ); + // + if ( moveresult.flags & ( MOVERESULT_MOVEMENTVIEWSET | MOVERESULT_MOVEMENTVIEW | MOVERESULT_SWIMVIEW ) ) { + VectorCopy( moveresult.ideal_viewangles, bs->ideal_viewangles ); + } else if ( !( bs->flags & BFL_IDEALVIEWSET ) ) { + if ( bs->chase_time > trap_AAS_Time() - 2 ) { + BotAimAtEnemy( bs ); + } else { + if ( trap_BotMovementViewTarget( bs->ms, &goal, bs->tfl, 300, target ) ) { + VectorSubtract( target, bs->origin, dir ); + vectoangles( dir, bs->ideal_viewangles ); + } else { + vectoangles( moveresult.movedir, bs->ideal_viewangles ); + } + } + bs->ideal_viewangles[2] *= 0.5; + } + //if the weapon is used for the bot movement + if ( moveresult.flags & MOVERESULT_MOVEMENTWEAPON ) { + bs->weaponnum = moveresult.weapon; + } + //if the bot is in the area the enemy was last seen in + if ( bs->areanum == bs->lastenemyareanum ) { + bs->chase_time = 0; + } + //if the bot wants to retreat (the bot could have been damage during the chase) + if ( BotWantsToRetreat( bs ) ) { + AIEnter_Battle_Retreat( bs ); + return qtrue; + } + return qtrue; +} + +/* +================== +AIEnter_Battle_Retreat +================== +*/ +void AIEnter_Battle_Retreat( bot_state_t *bs ) { + BotRecordNodeSwitch( bs, "battle retreat", "" ); + bs->ainode = AINode_Battle_Retreat; +} + +/* +================== +AINode_Battle_Retreat +================== +*/ +int AINode_Battle_Retreat( bot_state_t *bs ) { + bot_goal_t goal; + aas_entityinfo_t entinfo; + bot_moveresult_t moveresult; + vec3_t target, dir; + float attack_skill, range; + int areanum; + + if ( BotIsObserver( bs ) ) { + AIEnter_Observer( bs ); + return qfalse; + } + //if in the intermission + if ( BotIntermission( bs ) ) { + AIEnter_Intermission( bs ); + return qfalse; + } + //respawn if dead + if ( BotIsDead( bs ) ) { + AIEnter_Respawn( bs ); + return qfalse; + } + //if no enemy + if ( bs->enemy < 0 ) { + AIEnter_Seek_LTG( bs ); + return qfalse; + } + // + BotEntityInfo( bs->enemy, &entinfo ); + if ( EntityIsDead( &entinfo ) ) { + AIEnter_Seek_LTG( bs ); + return qfalse; + } + // + bs->tfl = TFL_DEFAULT; + if ( bot_grapple.integer ) { + bs->tfl |= TFL_GRAPPLEHOOK; + } + //if in lava or slime the bot should be able to get out + if ( BotInLava( bs ) ) { + bs->tfl |= TFL_LAVA; + } + if ( BotInSlime( bs ) ) { + bs->tfl |= TFL_SLIME; + } + //map specific code + BotMapScripts( bs ); + //update the attack inventory values + BotUpdateBattleInventory( bs, bs->enemy ); + //if the bot doesn't want to retreat anymore... probably picked up some nice items + if ( BotWantsToChase( bs ) ) { + //empty the goal stack, when chasing, only the enemy is the goal + trap_BotEmptyGoalStack( bs->gs ); + //go chase the enemy + AIEnter_Battle_Chase( bs ); + return qfalse; + } + //update the last time the enemy was visible + if ( BotEntityVisible( bs->entitynum, bs->eye, bs->viewangles, 360, bs->enemy ) ) { + bs->enemyvisible_time = trap_AAS_Time(); + //update the reachability area and origin if possible + areanum = BotPointAreaNum( entinfo.origin ); + if ( areanum && trap_AAS_AreaReachability( areanum ) ) { + VectorCopy( entinfo.origin, bs->lastenemyorigin ); + bs->lastenemyareanum = areanum; + } + } + //if the enemy is NOT visible for 4 seconds + if ( bs->enemyvisible_time < trap_AAS_Time() - 4 ) { + AIEnter_Seek_LTG( bs ); + return qfalse; + } + //else if the enemy is NOT visible + else if ( bs->enemyvisible_time < trap_AAS_Time() ) { + //if there is another enemy + if ( BotFindEnemy( bs, -1 ) ) { + AIEnter_Battle_Fight( bs ); + return qfalse; + } + } + // +#ifdef CTF + if ( gametype == GT_CTF ) { + BotCTFRetreatGoals( bs ); + } +#endif //CTF + //use holdable items + BotBattleUseItems( bs ); + //get the current long term goal while retreating + if ( !BotLongTermGoal( bs, bs->tfl, qtrue, &goal ) ) { + return qtrue; + } + //check for nearby goals periodicly + if ( bs->check_time < trap_AAS_Time() ) { + bs->check_time = trap_AAS_Time() + 1; + range = 150; +#ifdef CTF + //if carrying a flag the bot shouldn't be distracted too much + if ( BotCTFCarryingFlag( bs ) ) { + range = 100; + } +#endif //CTF + // + if ( BotNearbyGoal( bs, bs->tfl, &goal, range ) ) { + trap_BotResetLastAvoidReach( bs->ms ); + //time the bot gets to pick up the nearby goal item + bs->nbg_time = trap_AAS_Time() + range / 100 + 1; + AIEnter_Battle_NBG( bs ); + return qfalse; + } + } + //initialize the movement state + BotSetupForMovement( bs ); + //move towards the goal + trap_BotMoveToGoal( &moveresult, bs->ms, &goal, bs->tfl ); + //if the movement failed + if ( moveresult.failure ) { + //reset the avoid reach, otherwise bot is stuck in current area + trap_BotResetAvoidReach( bs->ms ); + //BotAI_Print(PRT_MESSAGE, "movement failure %d\n", moveresult.traveltype); + bs->ltg_time = 0; + } + // + BotAIBlocked( bs, &moveresult, qfalse ); + //choose the best weapon to fight with + BotChooseWeapon( bs ); + //if the view is fixed for the movement + if ( moveresult.flags & ( MOVERESULT_MOVEMENTVIEW + //|MOVERESULT_SWIMVIEW + ) ) { + VectorCopy( moveresult.ideal_viewangles, bs->ideal_viewangles ); + } else if ( !( moveresult.flags & MOVERESULT_MOVEMENTVIEWSET ) + && !( bs->flags & BFL_IDEALVIEWSET ) ) { + attack_skill = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_ATTACK_SKILL, 0, 1 ); + //if the bot is skilled anough + if ( attack_skill > 0.3 ) { + BotAimAtEnemy( bs ); + } else { + if ( trap_BotMovementViewTarget( bs->ms, &goal, bs->tfl, 300, target ) ) { + VectorSubtract( target, bs->origin, dir ); + vectoangles( dir, bs->ideal_viewangles ); + } else { + vectoangles( moveresult.movedir, bs->ideal_viewangles ); + } + bs->ideal_viewangles[2] *= 0.5; + } + } + //if the weapon is used for the bot movement + if ( moveresult.flags & MOVERESULT_MOVEMENTWEAPON ) { + bs->weaponnum = moveresult.weapon; + } + //attack the enemy if possible + BotCheckAttack( bs ); + // + return qtrue; +} + +/* +================== +AIEnter_Battle_NBG +================== +*/ +void AIEnter_Battle_NBG( bot_state_t *bs ) { + BotRecordNodeSwitch( bs, "battle NBG", "" ); + bs->ainode = AINode_Battle_NBG; +} + +/* +================== +AINode_Battle_NBG +================== +*/ +int AINode_Battle_NBG( bot_state_t *bs ) { + int areanum; + bot_goal_t goal; + aas_entityinfo_t entinfo; + bot_moveresult_t moveresult; + float attack_skill; + vec3_t target, dir; + + if ( BotIsObserver( bs ) ) { + AIEnter_Observer( bs ); + return qfalse; + } + //if in the intermission + if ( BotIntermission( bs ) ) { + AIEnter_Intermission( bs ); + return qfalse; + } + //respawn if dead + if ( BotIsDead( bs ) ) { + AIEnter_Respawn( bs ); + return qfalse; + } + //if no enemy + if ( bs->enemy < 0 ) { + AIEnter_Seek_NBG( bs ); + return qfalse; + } + // + BotEntityInfo( bs->enemy, &entinfo ); + if ( EntityIsDead( &entinfo ) ) { + AIEnter_Seek_NBG( bs ); + return qfalse; + } + // + bs->tfl = TFL_DEFAULT; + if ( bot_grapple.integer ) { + bs->tfl |= TFL_GRAPPLEHOOK; + } + //if in lava or slime the bot should be able to get out + if ( BotInLava( bs ) ) { + bs->tfl |= TFL_LAVA; + } + if ( BotInSlime( bs ) ) { + bs->tfl |= TFL_SLIME; + } + // + if ( BotCanAndWantsToRocketJump( bs ) ) { + bs->tfl |= TFL_ROCKETJUMP; + } + //map specific code + BotMapScripts( bs ); + //update the last time the enemy was visible + if ( BotEntityVisible( bs->entitynum, bs->eye, bs->viewangles, 360, bs->enemy ) ) { + bs->enemyvisible_time = trap_AAS_Time(); + //update the reachability area and origin if possible + areanum = BotPointAreaNum( entinfo.origin ); + if ( areanum && trap_AAS_AreaReachability( areanum ) ) { + VectorCopy( entinfo.origin, bs->lastenemyorigin ); + bs->lastenemyareanum = areanum; + } + } + //if the bot has no goal or touches the current goal + if ( !trap_BotGetTopGoal( bs->gs, &goal ) ) { + bs->nbg_time = 0; + } else if ( trap_BotTouchingGoal( bs->origin, &goal ) ) { + bs->nbg_time = 0; + } + // + if ( bs->nbg_time < trap_AAS_Time() ) { + //pop the current goal from the stack + trap_BotPopGoal( bs->gs ); + //if the bot still has a goal + if ( trap_BotGetTopGoal( bs->gs, &goal ) ) { + AIEnter_Battle_Retreat( bs ); + } else { AIEnter_Battle_Fight( bs );} + // + return qfalse; + } + //initialize the movement state + BotSetupForMovement( bs ); + //move towards the goal + trap_BotMoveToGoal( &moveresult, bs->ms, &goal, bs->tfl ); + //if the movement failed + if ( moveresult.failure ) { + //reset the avoid reach, otherwise bot is stuck in current area + trap_BotResetAvoidReach( bs->ms ); + //BotAI_Print(PRT_MESSAGE, "movement failure %d\n", moveresult.traveltype); + bs->nbg_time = 0; + } + // + BotAIBlocked( bs, &moveresult, qfalse ); + //update the attack inventory values + BotUpdateBattleInventory( bs, bs->enemy ); + //choose the best weapon to fight with + BotChooseWeapon( bs ); + //if the view is fixed for the movement + if ( moveresult.flags & MOVERESULT_MOVEMENTVIEW ) { + VectorCopy( moveresult.ideal_viewangles, bs->ideal_viewangles ); + } else if ( !( moveresult.flags & MOVERESULT_MOVEMENTVIEWSET ) + && !( bs->flags & BFL_IDEALVIEWSET ) ) { + attack_skill = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_ATTACK_SKILL, 0, 1 ); + //if the bot is skilled anough and the enemy is visible + if ( attack_skill > 0.3 ) { + //&& BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, bs->enemy) + BotAimAtEnemy( bs ); + } else { + if ( trap_BotMovementViewTarget( bs->ms, &goal, bs->tfl, 300, target ) ) { + VectorSubtract( target, bs->origin, dir ); + vectoangles( dir, bs->ideal_viewangles ); + } else { + vectoangles( moveresult.movedir, bs->ideal_viewangles ); + } + bs->ideal_viewangles[2] *= 0.5; + } + } + //if the weapon is used for the bot movement + if ( moveresult.flags & MOVERESULT_MOVEMENTWEAPON ) { + bs->weaponnum = moveresult.weapon; + } + //attack the enemy if possible + BotCheckAttack( bs ); + // + return qtrue; +} diff --git a/src/botai/ai_dmnet.h b/src/botai/ai_dmnet.h new file mode 100644 index 0000000..8b43529 --- /dev/null +++ b/src/botai/ai_dmnet.h @@ -0,0 +1,66 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: ai_dmnet.h + * + * desc: Quake3 bot AI + * + * + *****************************************************************************/ + +#define MAX_NODESWITCHES 50 + +void AIEnter_Intermission( bot_state_t *bs ); +void AIEnter_Observer( bot_state_t *bs ); +void AIEnter_Respawn( bot_state_t *bs ); +void AIEnter_Stand( bot_state_t *bs ); +void AIEnter_Seek_ActivateEntity( bot_state_t *bs ); +void AIEnter_Seek_NBG( bot_state_t *bs ); +void AIEnter_Seek_LTG( bot_state_t *bs ); +void AIEnter_Seek_Camp( bot_state_t *bs ); +void AIEnter_Battle_Fight( bot_state_t *bs ); +void AIEnter_Battle_Chase( bot_state_t *bs ); +void AIEnter_Battle_Retreat( bot_state_t *bs ); +void AIEnter_Battle_NBG( bot_state_t *bs ); +int AINode_Intermission( bot_state_t *bs ); +int AINode_Observer( bot_state_t *bs ); +int AINode_Respawn( bot_state_t *bs ); +int AINode_Stand( bot_state_t *bs ); +int AINode_Seek_ActivateEntity( bot_state_t *bs ); +int AINode_Seek_NBG( bot_state_t *bs ); +int AINode_Seek_LTG( bot_state_t *bs ); +int AINode_Battle_Fight( bot_state_t *bs ); +int AINode_Battle_Chase( bot_state_t *bs ); +int AINode_Battle_Retreat( bot_state_t *bs ); +int AINode_Battle_NBG( bot_state_t *bs ); + +void BotResetNodeSwitches( void ); +void BotDumpNodeSwitches( bot_state_t *bs ); + diff --git a/src/botai/ai_dmq3.c b/src/botai/ai_dmq3.c new file mode 100644 index 0000000..b1eb198 --- /dev/null +++ b/src/botai/ai_dmq3.c @@ -0,0 +1,2899 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: ai_dmq3.c + * + * desc: Quake3 bot AI + * + * + *****************************************************************************/ + + +#include "../game/g_local.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "../game/be_ea.h" +#include "../game/be_ai_char.h" +#include "../game/be_ai_chat.h" +#include "../game/be_ai_gen.h" +#include "../game/be_ai_goal.h" +#include "../game/be_ai_move.h" +#include "../game/be_ai_weap.h" +#include "../botai/botai.h" +// +#include "ai_main.h" +#include "ai_dmq3.h" +#include "ai_chat.h" +#include "ai_cmd.h" +#include "ai_dmnet.h" +#include "ai_team.h" +// +#include "chars.h" //characteristics +#include "inv.h" //indexes into the inventory +#include "syn.h" //synonyms +#include "match.h" //string matching types and vars + +#define IDEAL_ATTACKDIST 140 +#define WEAPONINDEX_MACHINEGUN 2 + +#define MAX_WAYPOINTS 128 + +////////////////// +// from aasfile.h +#define AREACONTENTS_MOVER 1024 +#define AREACONTENTS_MODELNUMSHIFT 24 +#define AREACONTENTS_MAXMODELNUM 0xFF +#define AREACONTENTS_MODELNUM ( AREACONTENTS_MAXMODELNUM << AREACONTENTS_MODELNUMSHIFT ) +////////////////// +// +bot_waypoint_t botai_waypoints[MAX_WAYPOINTS]; +bot_waypoint_t *botai_freewaypoints; + +//NOTE: not using a cvar which can be updated because the game should be reloaded anyway +int gametype; //game type + +// Rafael gameskill +int gameskill; + +vmCvar_t bot_grapple; +vmCvar_t bot_rocketjump; +vmCvar_t bot_fastchat; +vmCvar_t bot_nochat; +vmCvar_t bot_testrchat; + +vec3_t lastteleport_origin; +float lastteleport_time; +//true when the map changed +int max_bspmodelindex; //maximum BSP model index + +//CTF flag goals +bot_goal_t ctf_redflag; +bot_goal_t ctf_blueflag; + +#ifdef CTF +/* +================== +BotCTFCarryingFlag +================== +*/ +int BotCTFCarryingFlag( bot_state_t *bs ) { + if ( gametype != GT_CTF ) { + return CTF_FLAG_NONE; + } + + if ( bs->inventory[INVENTORY_REDFLAG] > 0 ) { + return CTF_FLAG_RED; + } else if ( bs->inventory[INVENTORY_BLUEFLAG] > 0 ) { + return CTF_FLAG_BLUE; + } + return CTF_FLAG_NONE; +} + +/* +================== +BotCTFTeam +================== +*/ +int BotCTFTeam( bot_state_t *bs ) { + char skin[128], *p; + + if ( gametype != GT_CTF ) { + return CTF_TEAM_NONE; + } + ClientSkin( bs->client, skin, sizeof( skin ) ); + p = strchr( skin, '/' ); + if ( !p ) { + p = skin; + } else { p++;} + if ( Q_stricmp( p, CTF_SKIN_REDTEAM ) == 0 ) { + return CTF_TEAM_RED; + } + if ( Q_stricmp( p, CTF_SKIN_BLUETEAM ) == 0 ) { + return CTF_TEAM_BLUE; + } + return CTF_TEAM_NONE; +} + +/* +================== +BotCTFRetreatGoals +================== +*/ +void BotCTFRetreatGoals( bot_state_t *bs ) { + //when carrying a flag in ctf the bot should rush to the base + if ( BotCTFCarryingFlag( bs ) ) { + //if not already rushing to the base + if ( bs->ltgtype != LTG_RUSHBASE ) { + bs->ltgtype = LTG_RUSHBASE; + bs->teamgoal_time = trap_AAS_Time() + CTF_RUSHBASE_TIME; + bs->rushbaseaway_time = 0; + } + } +} + +/* +================== +BotCTFSeekGoals +================== +*/ +void BotCTFSeekGoals( bot_state_t *bs ) { + float rnd; + + //when carrying a flag in ctf the bot should rush to the base + if ( BotCTFCarryingFlag( bs ) ) { + //if not already rushing to the base + if ( bs->ltgtype != LTG_RUSHBASE ) { + bs->ltgtype = LTG_RUSHBASE; + bs->teamgoal_time = trap_AAS_Time() + CTF_RUSHBASE_TIME; + bs->rushbaseaway_time = 0; + } + return; + } + //if the bot is roaming + if ( bs->ctfroam_time > trap_AAS_Time() ) { + return; + } + //if already a CTF or team goal + if ( bs->ltgtype == LTG_TEAMHELP || + bs->ltgtype == LTG_TEAMACCOMPANY || + bs->ltgtype == LTG_DEFENDKEYAREA || + bs->ltgtype == LTG_GETFLAG || + bs->ltgtype == LTG_RUSHBASE || + bs->ltgtype == LTG_CAMPORDER || + bs->ltgtype == LTG_PATROL ) { + return; + } + //if the bot has anough aggression to decide what to do + if ( BotAggression( bs ) < 50 ) { + return; + } + //set the time to send a message to the team mates + bs->teammessage_time = trap_AAS_Time() + 2 * random(); + //get the flag or defend the base + rnd = random(); + if ( rnd < 0.33 && ctf_redflag.areanum && ctf_blueflag.areanum ) { + bs->ltgtype = LTG_GETFLAG; + //set the time the bot will stop getting the flag + bs->teamgoal_time = trap_AAS_Time() + CTF_GETFLAG_TIME; + } else if ( rnd < 0.66 && ctf_redflag.areanum && ctf_blueflag.areanum ) { + //FIXME: do not always use the base flag + if ( BotCTFTeam( bs ) == CTF_TEAM_RED ) { + memcpy( &bs->teamgoal, &ctf_redflag, sizeof( bot_goal_t ) ); + } else { memcpy( &bs->teamgoal, &ctf_blueflag, sizeof( bot_goal_t ) );} + //set the ltg type + bs->ltgtype = LTG_DEFENDKEYAREA; + //set the time the bot stop defending the base + bs->teamgoal_time = trap_AAS_Time() + TEAM_DEFENDKEYAREA_TIME; + bs->defendaway_time = 0; + } else { + bs->ltgtype = 0; + //set the time the bot will stop roaming + bs->ctfroam_time = trap_AAS_Time() + CTF_ROAM_TIME; + } +#ifdef DEBUG + BotPrintTeamGoal( bs ); +#endif //DEBUG +} + +#endif //CTF + +/* +================== +BotPointAreaNum +================== +*/ +int BotPointAreaNum( vec3_t origin ) { + int areanum, numareas, areas[1]; + vec3_t end, ofs; + #define BOTAREA_JIGGLE_DIST 32 + + areanum = trap_AAS_PointAreaNum( origin ); + if ( areanum ) { + return areanum; + } + VectorCopy( origin, end ); + end[2] += 10; + numareas = trap_AAS_TraceAreas( origin, end, areas, NULL, 1 ); + if ( numareas > 0 ) { + return areas[0]; + } + + // Ridah, jiggle them around to look for a fuzzy area, helps LARGE characters reach destinations that are against walls + ofs[2] = 10; + for ( ofs[0] = -BOTAREA_JIGGLE_DIST; ofs[0] <= BOTAREA_JIGGLE_DIST; ofs[0] += BOTAREA_JIGGLE_DIST * 2 ) { + for ( ofs[1] = -BOTAREA_JIGGLE_DIST; ofs[1] <= BOTAREA_JIGGLE_DIST; ofs[1] += BOTAREA_JIGGLE_DIST * 2 ) { + VectorAdd( origin, ofs, end ); + numareas = trap_AAS_TraceAreas( origin, end, areas, NULL, 1 ); + if ( numareas > 0 ) { + return areas[0]; + } + } + } + + return 0; +} + +/* +================== +ClientName +================== +*/ +char *ClientName( int client, char *name, int size ) { + char buf[MAX_INFO_STRING]; + + if ( client < 0 || client >= MAX_CLIENTS ) { + BotAI_Print( PRT_ERROR, "ClientName: client out of range\n" ); + return "[client out of range]"; + } + trap_GetConfigstring( CS_PLAYERS + client, buf, sizeof( buf ) ); + strncpy( name, Info_ValueForKey( buf, "n" ), size - 1 ); + name[size - 1] = '\0'; + Q_CleanStr( name ); + return name; +} + +/* +================== +ClientSkin +================== +*/ +char *ClientSkin( int client, char *skin, int size ) { + char buf[MAX_INFO_STRING]; + + if ( client < 0 || client >= MAX_CLIENTS ) { + BotAI_Print( PRT_ERROR, "ClientSkin: client out of range\n" ); + return "[client out of range]"; + } + trap_GetConfigstring( CS_PLAYERS + client, buf, sizeof( buf ) ); + strncpy( skin, Info_ValueForKey( buf, "model" ), size - 1 ); + skin[size - 1] = '\0'; + return skin; +} + +/* +================== +ClientFromName +================== +*/ +int ClientFromName( char *name ) { + int i; + char buf[MAX_INFO_STRING]; + static int maxclients; + + if ( !maxclients ) { + maxclients = trap_Cvar_VariableIntegerValue( "sv_maxclients" ); + } + for ( i = 0; i < maxclients && i < MAX_CLIENTS; i++ ) { + trap_GetConfigstring( CS_PLAYERS + i, buf, sizeof( buf ) ); + Q_CleanStr( buf ); + if ( !Q_stricmp( Info_ValueForKey( buf, "n" ), name ) ) { + return i; + } + } + return -1; +} + +/* +================== +stristr +================== +*/ +char *stristr( char *str, char *charset ) { + int i; + + while ( *str ) { + for ( i = 0; charset[i] && str[i]; i++ ) { + if ( toupper( charset[i] ) != toupper( str[i] ) ) { + break; + } + } + if ( !charset[i] ) { + return str; + } + str++; + } + return NULL; +} + +/* +================== +EasyClientName +================== +*/ +char *EasyClientName( int client, char *buf, int size ) { + int i; + char *str1, *str2, *ptr, c; + char name[128]; + + strcpy( name, ClientName( client, name, sizeof( name ) ) ); + for ( i = 0; name[i]; i++ ) name[i] &= 127; + //remove all spaces + for ( ptr = strstr( name, " " ); ptr; ptr = strstr( name, " " ) ) { + memmove( ptr, ptr + 1, strlen( ptr + 1 ) + 1 ); + } + //check for [x] and ]x[ clan names + str1 = strstr( name, "[" ); + str2 = strstr( name, "]" ); + if ( str1 && str2 ) { + if ( str2 > str1 ) { + memmove( str1, str2 + 1, strlen( str2 + 1 ) + 1 ); + } else { memmove( str2, str1 + 1, strlen( str1 + 1 ) + 1 );} + } + //remove Mr prefix + if ( ( name[0] == 'm' || name[0] == 'M' ) && + ( name[1] == 'r' || name[1] == 'R' ) ) { + memmove( name, name + 2, strlen( name + 2 ) + 1 ); + } + //only allow lower case alphabet characters + ptr = name; + while ( *ptr ) { + c = *ptr; + if ( ( c >= 'a' && c <= 'z' ) || + ( c >= '0' && c <= '9' ) || c == '_' ) { + ptr++; + } else if ( c >= 'A' && c <= 'Z' ) { + *ptr += 'a' - 'A'; + ptr++; + } else { + memmove( ptr, ptr + 1, strlen( ptr + 1 ) + 1 ); + } + } + strncpy( buf, name, size - 1 ); + buf[size - 1] = '\0'; + return buf; +} + +/* +================== +BotChooseWeapon +================== +*/ +void BotChooseWeapon( bot_state_t *bs ) { + int newweaponnum; + + if ( bs->cur_ps.weaponstate == WEAPON_RAISING || + bs->cur_ps.weaponstate == WEAPON_RAISING_TORELOAD || //----(SA) added + bs->cur_ps.weaponstate == WEAPON_DROPPING || + bs->cur_ps.weaponstate == WEAPON_DROPPING_TORELOAD ) { //----(SA) added + trap_EA_SelectWeapon( bs->client, bs->weaponnum ); + } else { + newweaponnum = trap_BotChooseBestFightWeapon( bs->ws, bs->inventory ); + if ( bs->weaponnum != newweaponnum ) { + bs->weaponchange_time = trap_AAS_Time(); + } + bs->weaponnum = newweaponnum; + //BotAI_Print(PRT_MESSAGE, "bs->weaponnum = %d\n", bs->weaponnum); + trap_EA_SelectWeapon( bs->client, bs->weaponnum ); + } +} + +/* +================== +BotSetupForMovement +================== +*/ +void BotSetupForMovement( bot_state_t *bs ) { + bot_initmove_t initmove; + + memset( &initmove, 0, sizeof( bot_initmove_t ) ); + VectorCopy( bs->cur_ps.origin, initmove.origin ); + VectorCopy( bs->cur_ps.velocity, initmove.velocity ); + VectorCopy( bs->cur_ps.origin, initmove.viewoffset ); + initmove.viewoffset[2] += bs->cur_ps.viewheight; + initmove.entitynum = bs->entitynum; + initmove.client = bs->client; + initmove.thinktime = bs->thinktime; + //set the onground flag + if ( bs->cur_ps.groundEntityNum != ENTITYNUM_NONE ) { + initmove.or_moveflags |= MFL_ONGROUND; + } + //set the teleported flag + if ( ( bs->cur_ps.pm_flags & PMF_TIME_KNOCKBACK ) && ( bs->cur_ps.pm_time > 0 ) ) { + initmove.or_moveflags |= MFL_TELEPORTED; + } + //set the waterjump flag + if ( ( bs->cur_ps.pm_flags & PMF_TIME_WATERJUMP ) && ( bs->cur_ps.pm_time > 0 ) ) { + initmove.or_moveflags |= MFL_WATERJUMP; + } + //set presence type + if ( bs->cur_ps.pm_flags & PMF_DUCKED ) { + initmove.presencetype = PRESENCE_CROUCH; + } else { initmove.presencetype = PRESENCE_NORMAL;} + // + if ( bs->walker > 0.5 ) { + initmove.or_moveflags |= MFL_WALK; + } + // + VectorCopy( bs->viewangles, initmove.viewangles ); + // + trap_BotInitMoveState( bs->ms, &initmove ); +} + +/* +================== +BotUpdateInventory +================== +*/ +void BotUpdateInventory( bot_state_t *bs ) { + //armor + bs->inventory[INVENTORY_ARMOR] = bs->cur_ps.stats[STAT_ARMOR]; + //weapons + bs->inventory[INVENTORY_LUGER] = COM_BitCheck( bs->cur_ps.weapons, ( WP_LUGER ) ); + bs->inventory[INVENTORY_MAUSER] = COM_BitCheck( bs->cur_ps.weapons, ( WP_MAUSER ) ); + bs->inventory[INVENTORY_MP40] = COM_BitCheck( bs->cur_ps.weapons, ( WP_MP40 ) ); + bs->inventory[INVENTORY_GRENADELAUNCHER] = COM_BitCheck( bs->cur_ps.weapons, ( WP_GRENADE_LAUNCHER ) ); + bs->inventory[INVENTORY_VENOM] = COM_BitCheck( bs->cur_ps.weapons, ( WP_VENOM ) ); + bs->inventory[INVENTORY_FLAMETHROWER] = COM_BitCheck( bs->cur_ps.weapons, ( WP_FLAMETHROWER ) ); + bs->inventory[INVENTORY_GAUNTLET] = COM_BitCheck( bs->cur_ps.weapons, ( WP_GAUNTLET ) ); + + // ammo + bs->inventory[INVENTORY_9MM] = bs->cur_ps.ammo[BG_FindAmmoForWeapon( WP_MP40 )]; + bs->inventory[INVENTORY_792MM] = bs->cur_ps.ammo[BG_FindAmmoForWeapon( WP_MAUSER )]; + bs->inventory[INVENTORY_GRENADES] = bs->cur_ps.ammo[BG_FindAmmoForWeapon( WP_GRENADE_LAUNCHER )]; + bs->inventory[INVENTORY_127MM] = bs->cur_ps.ammo[BG_FindAmmoForWeapon( WP_VENOM )]; + bs->inventory[INVENTORY_FUEL] = bs->cur_ps.ammo[BG_FindAmmoForWeapon( WP_FLAMETHROWER )]; + + //powerups + bs->inventory[INVENTORY_HEALTH] = bs->cur_ps.stats[STAT_HEALTH]; + bs->inventory[INVENTORY_TELEPORTER] = bs->cur_ps.stats[STAT_HOLDABLE_ITEM] == MODELINDEX_TELEPORTER; + bs->inventory[INVENTORY_MEDKIT] = bs->cur_ps.stats[STAT_HOLDABLE_ITEM] == MODELINDEX_MEDKIT; + bs->inventory[INVENTORY_QUAD] = bs->cur_ps.powerups[PW_QUAD] != 0; + bs->inventory[INVENTORY_ENVIRONMENTSUIT] = bs->cur_ps.powerups[PW_BATTLESUIT] != 0; + bs->inventory[INVENTORY_HASTE] = bs->cur_ps.powerups[PW_HASTE] != 0; + bs->inventory[INVENTORY_INVISIBILITY] = bs->cur_ps.powerups[PW_INVIS] != 0; + bs->inventory[INVENTORY_REGEN] = bs->cur_ps.powerups[PW_REGEN] != 0; + bs->inventory[INVENTORY_FLIGHT] = bs->cur_ps.powerups[PW_FLIGHT] != 0; + bs->inventory[INVENTORY_REDFLAG] = bs->cur_ps.powerups[PW_REDFLAG] != 0; + bs->inventory[INVENTORY_BLUEFLAG] = bs->cur_ps.powerups[PW_BLUEFLAG] != 0; + // +} + +/* +================== +BotUpdateBattleInventory +================== +*/ +void BotUpdateBattleInventory( bot_state_t *bs, int enemy ) { + vec3_t dir; + aas_entityinfo_t entinfo; + + BotEntityInfo( enemy, &entinfo ); + VectorSubtract( entinfo.origin, bs->origin, dir ); + bs->inventory[ENEMY_HEIGHT] = (int) dir[2]; + dir[2] = 0; + bs->inventory[ENEMY_HORIZONTAL_DIST] = (int) VectorLength( dir ); + //FIXME: add num visible enemies and num visible team mates to the inventory +} + +/* +================== +BotBattleUseItems +================== +*/ +void BotBattleUseItems( bot_state_t *bs ) { + if ( bs->inventory[INVENTORY_HEALTH] < 40 ) { + if ( bs->inventory[INVENTORY_TELEPORTER] > 0 ) { + trap_EA_Use( bs->client ); + } + if ( bs->inventory[INVENTORY_MEDKIT] > 0 ) { + trap_EA_Use( bs->client ); + } + } +} + +/* +================== +BotSetTeleportTime +================== +*/ +void BotSetTeleportTime( bot_state_t *bs ) { + if ( ( bs->cur_ps.eFlags ^ bs->last_eFlags ) & EF_TELEPORT_BIT ) { + bs->teleport_time = trap_AAS_Time(); + } + bs->last_eFlags = bs->cur_ps.eFlags; +} + +/* +================== +BotIsDead +================== +*/ +qboolean BotIsDead( bot_state_t *bs ) { + return ( bs->cur_ps.pm_type == PM_DEAD ); +} + +/* +================== +BotIsObserver +================== +*/ +qboolean BotIsObserver( bot_state_t *bs ) { + char buf[MAX_INFO_STRING]; + if ( bs->cur_ps.pm_type == PM_SPECTATOR ) { + return qtrue; + } + trap_GetConfigstring( CS_PLAYERS + bs->client, buf, sizeof( buf ) ); + if ( atoi( Info_ValueForKey( buf, "t" ) ) == TEAM_SPECTATOR ) { + return qtrue; + } + return qfalse; +} + +/* +================== +BotIntermission +================== +*/ +qboolean BotIntermission( bot_state_t *bs ) { + //NOTE: we shouldn't look at the game code... + if ( level.intermissiontime ) { + return qtrue; + } + return ( bs->cur_ps.pm_type == PM_FREEZE || bs->cur_ps.pm_type == PM_INTERMISSION ); +} + + +/* +============== +BotInLava +============== +*/ +qboolean BotInLava( bot_state_t *bs ) { + vec3_t feet; + + VectorCopy( bs->origin, feet ); + feet[2] -= 23; + return ( trap_AAS_PointContents( feet ) & CONTENTS_LAVA ); +} + +/* +============== +BotInSlime +============== +*/ +qboolean BotInSlime( bot_state_t *bs ) { + vec3_t feet; + + VectorCopy( bs->origin, feet ); + feet[2] -= 23; + return ( trap_AAS_PointContents( feet ) & CONTENTS_SLIME ); +} + +/* +================== +EntityIsDead +================== +*/ +qboolean EntityIsDead( aas_entityinfo_t *entinfo ) { + playerState_t ps; + + if ( entinfo->number >= 0 && entinfo->number < MAX_CLIENTS ) { + //retrieve the current client state + BotAI_GetClientState( entinfo->number, &ps ); + if ( ps.pm_type != PM_NORMAL ) { + return qtrue; + } + } + return qfalse; +} + +/* +================== +EntityIsInvisible +================== +*/ +qboolean EntityIsInvisible( aas_entityinfo_t *entinfo ) { + if ( entinfo->powerups & ( 1 << PW_INVIS ) ) { + return qtrue; + } + return qfalse; +} + +/* +================== +EntityIsShooting +================== +*/ +qboolean EntityIsShooting( aas_entityinfo_t *entinfo ) { + if ( entinfo->flags & EF_FIRING ) { + return qtrue; + } + return qfalse; +} + +/* +================== +EntityIsChatting +================== +*/ +qboolean EntityIsChatting( aas_entityinfo_t *entinfo ) { + if ( entinfo->flags & EF_TALK ) { + return qtrue; + } + return qfalse; +} + +/* +================== +EntityHasQuad +================== +*/ +qboolean EntityHasQuad( aas_entityinfo_t *entinfo ) { + if ( entinfo->powerups & ( 1 << PW_QUAD ) ) { + return qtrue; + } + return qfalse; +} + +/* +================== +BotCreateWayPoint +================== +*/ +bot_waypoint_t *BotCreateWayPoint( char *name, vec3_t origin, int areanum ) { + bot_waypoint_t *wp; + vec3_t waypointmins = {-8, -8, -8}, waypointmaxs = {8, 8, 8}; + + wp = botai_freewaypoints; + if ( !wp ) { + BotAI_Print( PRT_WARNING, "BotCreateWayPoint: Out of waypoints\n" ); + return NULL; + } + botai_freewaypoints = botai_freewaypoints->next; + + Q_strncpyz( wp->name, name, sizeof( wp->name ) ); + VectorCopy( origin, wp->goal.origin ); + VectorCopy( waypointmins, wp->goal.mins ); + VectorCopy( waypointmaxs, wp->goal.maxs ); + wp->goal.areanum = areanum; + wp->next = NULL; + wp->prev = NULL; + return wp; +} + +/* +================== +BotFindWayPoint +================== +*/ +bot_waypoint_t *BotFindWayPoint( bot_waypoint_t *waypoints, char *name ) { + bot_waypoint_t *wp; + + for ( wp = waypoints; wp; wp = wp->next ) { + if ( !Q_stricmp( wp->name, name ) ) { + return wp; + } + } + return NULL; +} + +/* +================== +BotFreeWaypoints +================== +*/ +void BotFreeWaypoints( bot_waypoint_t *wp ) { + bot_waypoint_t *nextwp; + + for (; wp; wp = nextwp ) { + nextwp = wp->next; + wp->next = botai_freewaypoints; + botai_freewaypoints = wp; + } +} + +/* +================== +BotInitWaypoints +================== +*/ +void BotInitWaypoints( void ) { + int i; + + botai_freewaypoints = NULL; + for ( i = 0; i < MAX_WAYPOINTS; i++ ) { + botai_waypoints[i].next = botai_freewaypoints; + botai_freewaypoints = &botai_waypoints[i]; + } +} + +/* +================== +TeamPlayIsOn +================== +*/ +int TeamPlayIsOn( void ) { + return ( gametype == GT_TEAM || gametype == GT_CTF ); +} + +/* +================== +BotAggression + +FIXME: move this to external fuzzy logic + + NOTE!!: I made no changes to this code for wolf weapon awareness. (SA) +================== +*/ +float BotAggression( bot_state_t *bs ) { + //if the bot has quad + if ( bs->inventory[INVENTORY_QUAD] ) { + //if the bot is not holding the gauntlet or the enemy is really nearby + if ( bs->weaponnum != WP_GAUNTLET || + bs->inventory[ENEMY_HORIZONTAL_DIST] < 80 ) { + return 70; + } + } + //if the enemy is located way higher than the bot + if ( bs->inventory[ENEMY_HEIGHT] > 200 ) { + return 0; + } + //if the bot is very low on health + if ( bs->inventory[INVENTORY_HEALTH] < 60 ) { + return 0; + } + //if the bot is low on health + if ( bs->inventory[INVENTORY_HEALTH] < 80 ) { + //if the bot has insufficient armor + if ( bs->inventory[INVENTORY_ARMOR] < 40 ) { + return 0; + } + } +// //if the bot can use the bfg +// if (bs->inventory[INVENTORY_BFG10K] > 0 && +// bs->inventory[INVENTORY_BFGAMMO] > 7) return 100; +// //if the bot can use the railgun +// if (bs->inventory[INVENTORY_RAILGUN] > 0 && +// bs->inventory[INVENTORY_SLUGS] > 5) return 95; + //if the bot can use the lightning gun + if ( bs->inventory[INVENTORY_FLAMETHROWER] > 0 && + bs->inventory[INVENTORY_FUEL] > 50 ) { + return 90; + } + //if the bot can use the rocketlauncher + if ( bs->inventory[INVENTORY_ROCKETLAUNCHER] > 0 && + bs->inventory[INVENTORY_ROCKETS] > 5 ) { + return 90; + } + //if the bot can use the SP5 + if ( bs->inventory[INVENTORY_SP5] > 0 && + bs->inventory[INVENTORY_SP5AMMO] > 40 ) { + return 85; + } + //if the bot can use the grenade launcher + if ( bs->inventory[INVENTORY_GRENADELAUNCHER] > 0 && + bs->inventory[INVENTORY_GRENADES] > 10 ) { + return 80; + } +// //if the bot can use the shotgun +// if (bs->inventory[INVENTORY_SHOTGUN] > 0 && +// bs->inventory[INVENTORY_SHELLS] > 10) return 50; + //otherwise the bot is not feeling too good + return 0; +} + +/* +================== +BotWantsToRetreat +================== +*/ +int BotWantsToRetreat( bot_state_t *bs ) { +#ifdef CTF + //always retreat when carrying a CTF flag + if ( BotCTFCarryingFlag( bs ) ) { + return qtrue; + } + //if the bot is getting the flag + if ( bs->ltgtype == LTG_GETFLAG ) { + return qtrue; + } +#endif //CTF + if ( BotAggression( bs ) < 50 ) { + return qtrue; + } + return qfalse; +} + +/* +================== +BotWantsToChase +================== +*/ +int BotWantsToChase( bot_state_t *bs ) { +#ifdef CTF + //always retreat when carrying a CTF flag + if ( BotCTFCarryingFlag( bs ) ) { + return qfalse; + } + //if the bot is getting the flag + if ( bs->ltgtype == LTG_GETFLAG ) { + return qfalse; + } +#endif //CTF + if ( BotAggression( bs ) > 50 ) { + return qtrue; + } + return qfalse; +} + +/* +================== +BotWantsToHelp +================== +*/ +int BotWantsToHelp( bot_state_t *bs ) { + return qtrue; +} + +/* +================== +BotCanAndWantsToRocketJump +================== +*/ +int BotCanAndWantsToRocketJump( bot_state_t *bs ) { + float rocketjumper; + + //if rocket jumping is disabled + if ( !bot_rocketjump.integer ) { + return qfalse; + } + //if no rocket launcher + if ( bs->inventory[INVENTORY_ROCKETLAUNCHER] <= 0 ) { + return qfalse; + } + //if low on rockets + if ( bs->inventory[INVENTORY_ROCKETS] < 3 ) { + return qfalse; + } + //never rocket jump with the Quad + if ( bs->inventory[INVENTORY_QUAD] ) { + return qfalse; + } + //if low on health + if ( bs->inventory[INVENTORY_HEALTH] < 60 ) { + return qfalse; + } + //if not full health + if ( bs->inventory[INVENTORY_HEALTH] < 90 ) { + //if the bot has insufficient armor + if ( bs->inventory[INVENTORY_ARMOR] < 40 ) { + return qfalse; + } + } + rocketjumper = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_WEAPONJUMPING, 0, 1 ); + if ( rocketjumper < 0.5 ) { + return qfalse; + } + return qtrue; +} + +/* +================== +BotGoCamp +================== +*/ +void BotGoCamp( bot_state_t *bs, bot_goal_t *goal ) { + float camper; + + //set message time to zero so bot will NOT show any message + bs->teammessage_time = 0; + //set the ltg type + bs->ltgtype = LTG_CAMP; + //set the team goal + memcpy( &bs->teamgoal, goal, sizeof( bot_goal_t ) ); + //get the team goal time + camper = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_CAMPER, 0, 1 ); + if ( camper > 0.99 ) { + bs->teamgoal_time = 99999; + } else { bs->teamgoal_time = 120 + 180 * camper + random() * 15;} + //set the last time the bot started camping + bs->camp_time = trap_AAS_Time(); + //the teammate that requested the camping + bs->teammate = 0; + //do NOT type arrive message + bs->arrive_time = 1; +} + +/* +================== +BotWantsToCamp +================== +*/ +int BotWantsToCamp( bot_state_t *bs ) { + float camper; + int cs, traveltime, besttraveltime; + bot_goal_t goal, bestgoal; + + camper = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_CAMPER, 0, 1 ); + if ( camper < 0.1 ) { + return qfalse; + } + //if the bot has a team goal + if ( bs->ltgtype == LTG_TEAMHELP || + bs->ltgtype == LTG_TEAMACCOMPANY || + bs->ltgtype == LTG_DEFENDKEYAREA || + bs->ltgtype == LTG_GETFLAG || + bs->ltgtype == LTG_RUSHBASE || + bs->ltgtype == LTG_CAMP || + bs->ltgtype == LTG_CAMPORDER || + bs->ltgtype == LTG_PATROL ) { + return qfalse; + } + //if camped recently + if ( bs->camp_time > trap_AAS_Time() - 60 + 300 * ( 1 - camper ) ) { + return qfalse; + } + // + if ( random() > camper ) { + bs->camp_time = trap_AAS_Time(); + return qfalse; + } + //if the bot isn't healthy anough + if ( BotAggression( bs ) < 50 ) { + return qfalse; + } + //the bot should have at least have the rocket launcher, the railgun or the bfg10k with some ammo + if ( ( bs->inventory[INVENTORY_ROCKETLAUNCHER] <= 0 || bs->inventory[INVENTORY_ROCKETS < 10] ) +// && (bs->inventory[INVENTORY_RAILGUN] <= 0 || bs->inventory[INVENTORY_SLUGS] < 10) +// && (bs->inventory[INVENTORY_BFG10K] <= 0 || bs->inventory[INVENTORY_BFGAMMO] < 10) + ) { + return qfalse; + } + //find the closest camp spot + besttraveltime = 99999; + for ( cs = trap_BotGetNextCampSpotGoal( 0, &goal ); cs; cs = trap_BotGetNextCampSpotGoal( cs, &goal ) ) { + traveltime = trap_AAS_AreaTravelTimeToGoalArea( bs->areanum, bs->origin, goal.areanum, TFL_DEFAULT ); + if ( traveltime && traveltime < besttraveltime ) { + besttraveltime = traveltime; + memcpy( &bestgoal, &goal, sizeof( bot_goal_t ) ); + } + } + if ( besttraveltime > 150 ) { + return qfalse; + } + //ok found a camp spot, go camp there + BotGoCamp( bs, &bestgoal ); + // + return qtrue; +} + +/* +================== +BotDontAvoid +================== +*/ +void BotDontAvoid( bot_state_t *bs, char *itemname ) { + bot_goal_t goal; + int num; + + num = trap_BotGetLevelItemGoal( -1, itemname, &goal ); + while ( num >= 0 ) { + trap_BotRemoveFromAvoidGoals( bs->gs, goal.number ); + num = trap_BotGetLevelItemGoal( num, itemname, &goal ); + } +} + +/* +================== +BotGoForPowerups +================== +*/ +void BotGoForPowerups( bot_state_t *bs ) { + + //don't avoid any of the powerups anymore + BotDontAvoid( bs, "Quad Damage" ); + BotDontAvoid( bs, "Regeneration" ); + BotDontAvoid( bs, "Battle Suit" ); + BotDontAvoid( bs, "Speed" ); + BotDontAvoid( bs, "Invisibility" ); + //BotDontAvoid(bs, "Flight"); + //reset the long term goal time so the bot will go for the powerup + //NOTE: the long term goal type doesn't change + bs->ltg_time = 0; +} + +/* +================== +BotRoamGoal +================== +*/ +void BotRoamGoal( bot_state_t *bs, vec3_t goal ) { + float len, r1, r2, sign, n; + int pc; + vec3_t dir, bestorg, belowbestorg; + bsp_trace_t trace; + + for ( n = 0; n < 10; n++ ) { + //start at the bot origin + VectorCopy( bs->origin, bestorg ); + r1 = random(); + if ( r1 < 0.8 ) { + //add a random value to the x-coordinate + r2 = random(); + if ( r2 < 0.5 ) { + sign = -1; + } else { sign = 1;} + bestorg[0] += sign * 700 * random() + 50; + } + if ( r1 > 0.2 ) { + //add a random value to the y-coordinate + r2 = random(); + if ( r2 < 0.5 ) { + sign = -1; + } else { sign = 1;} + bestorg[1] += sign * 700 * random() + 50; + } + //add a random value to the z-coordinate (NOTE: 48 = maxjump?) + bestorg[2] += 3 * 48 * random() - 2 * 48 - 1; + //trace a line from the origin to the roam target + BotAI_Trace( &trace, bs->origin, NULL, NULL, bestorg, bs->entitynum, MASK_SOLID ); + //direction and length towards the roam target + VectorSubtract( bestorg, bs->origin, dir ); + len = VectorNormalize( dir ); + //if the roam target is far away anough + if ( len > 200 ) { + //the roam target is in the given direction before walls + VectorScale( dir, len * trace.fraction - 40, dir ); + VectorAdd( bs->origin, dir, bestorg ); + //get the coordinates of the floor below the roam target + belowbestorg[0] = bestorg[0]; + belowbestorg[1] = bestorg[1]; + belowbestorg[2] = bestorg[2] - 800; + BotAI_Trace( &trace, bestorg, NULL, NULL, belowbestorg, bs->entitynum, MASK_SOLID ); + // + if ( !trace.startsolid ) { + trace.endpos[2]++; + pc = trap_PointContents( trace.endpos,bs->entitynum ); + if ( !( pc & CONTENTS_LAVA ) ) { //----(SA) modified since slime is no longer deadly +// if (!(pc & (CONTENTS_LAVA | CONTENTS_SLIME))) { + VectorCopy( bestorg, goal ); + return; + } + } + } + } + VectorCopy( bestorg, goal ); +} + +/* +================== +BotAttackMove +================== +*/ +bot_moveresult_t BotAttackMove( bot_state_t *bs, int tfl ) { + int movetype, i; + float attack_skill, jumper, croucher, dist, strafechange_time; + float attack_dist, attack_range; + vec3_t forward, backward, sideward, hordir, up = {0, 0, 1}; + aas_entityinfo_t entinfo; + bot_moveresult_t moveresult; + bot_goal_t goal; + + if ( bs->attackchase_time > trap_AAS_Time() ) { + //create the chase goal + goal.entitynum = bs->enemy; + goal.areanum = bs->lastenemyareanum; + VectorCopy( bs->lastenemyorigin, goal.origin ); + VectorSet( goal.mins, -8, -8, -8 ); + VectorSet( goal.maxs, 8, 8, 8 ); + //initialize the movement state + BotSetupForMovement( bs ); + //move towards the goal + trap_BotMoveToGoal( &moveresult, bs->ms, &goal, tfl ); + return moveresult; + } + // + memset( &moveresult, 0, sizeof( bot_moveresult_t ) ); + // + attack_skill = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_ATTACK_SKILL, 0, 1 ); + jumper = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_JUMPER, 0, 1 ); + croucher = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_CROUCHER, 0, 1 ); + //if the bot is really stupid + if ( attack_skill < 0.2 ) { + return moveresult; + } + //initialize the movement state + BotSetupForMovement( bs ); + //get the enemy entity info + BotEntityInfo( bs->enemy, &entinfo ); + //direction towards the enemy + VectorSubtract( entinfo.origin, bs->origin, forward ); + //the distance towards the enemy + dist = VectorNormalize( forward ); + VectorNegate( forward, backward ); + //walk, crouch or jump + movetype = MOVE_WALK; + // + if ( bs->attackcrouch_time < trap_AAS_Time() - 1 ) { + if ( random() < jumper ) { + movetype = MOVE_JUMP; + } + //wait at least one second before crouching again + else if ( bs->attackcrouch_time < trap_AAS_Time() - 1 && random() < croucher ) { + bs->attackcrouch_time = trap_AAS_Time() + croucher * 5; + } + } + if ( bs->attackcrouch_time > trap_AAS_Time() ) { + movetype = MOVE_CROUCH; + } + //if the bot should jump + if ( movetype == MOVE_JUMP ) { + //if jumped last frame + if ( bs->attackjump_time > trap_AAS_Time() ) { + movetype = MOVE_WALK; + } else { + bs->attackjump_time = trap_AAS_Time() + 1; + } + } + if ( bs->cur_ps.weapon == WP_GAUNTLET ) { + attack_dist = 0; + attack_range = 0; + } else { + attack_dist = IDEAL_ATTACKDIST; + attack_range = 40; + } + //if the bot is stupid + if ( attack_skill <= 0.4 ) { + //just walk to or away from the enemy + if ( dist > attack_dist + attack_range ) { + if ( trap_BotMoveInDirection( bs->ms, forward, 400, movetype ) ) { + return moveresult; + } + } + if ( dist < attack_dist - attack_range ) { + if ( trap_BotMoveInDirection( bs->ms, backward, 400, movetype ) ) { + return moveresult; + } + } + return moveresult; + } + //increase the strafe time + bs->attackstrafe_time += bs->thinktime; + //get the strafe change time + strafechange_time = 0.4 + ( 1 - attack_skill ) * 0.2; + if ( attack_skill > 0.7 ) { + strafechange_time += crandom() * 0.2; + } + //if the strafe direction should be changed + if ( bs->attackstrafe_time > strafechange_time ) { + //some magic number :) + if ( random() > 0.935 ) { + //flip the strafe direction + bs->flags ^= BFL_STRAFERIGHT; + bs->attackstrafe_time = 0; + } + } + // + for ( i = 0; i < 2; i++ ) { + hordir[0] = forward[0]; + hordir[1] = forward[1]; + hordir[2] = 0; + VectorNormalize( hordir ); + //get the sideward vector + CrossProduct( hordir, up, sideward ); + //reverse the vector depending on the strafe direction + if ( bs->flags & BFL_STRAFERIGHT ) { + VectorNegate( sideward, sideward ); + } + //randomly go back a little + if ( random() > 0.9 ) { + VectorAdd( sideward, backward, sideward ); + } else { + //walk forward or backward to get at the ideal attack distance + if ( dist > attack_dist + attack_range ) { + VectorAdd( sideward, forward, sideward ); + } else if ( dist < attack_dist - attack_range ) { + VectorAdd( sideward, backward, sideward ); + } + } + //perform the movement + if ( trap_BotMoveInDirection( bs->ms, sideward, 400, movetype ) ) { + return moveresult; + } + //movement failed, flip the strafe direction + bs->flags ^= BFL_STRAFERIGHT; + bs->attackstrafe_time = 0; + } + //bot couldn't do any usefull movement +// bs->attackchase_time = AAS_Time() + 6; + return moveresult; +} + +/* +================== +BotSameTeam +================== +*/ +int BotSameTeam( bot_state_t *bs, int entnum ) { + char info1[128], info2[128]; + + if ( bs->client < 0 || bs->client >= MAX_CLIENTS ) { + //BotAI_Print(PRT_ERROR, "BotSameTeam: client out of range\n"); + return qfalse; + } + if ( entnum < 0 || entnum >= MAX_CLIENTS ) { + //BotAI_Print(PRT_ERROR, "BotSameTeam: client out of range\n"); + return qfalse; + } + if ( gametype == GT_TEAM || gametype == GT_CTF ) { + trap_GetConfigstring( CS_PLAYERS + bs->client, info1, sizeof( info1 ) ); + trap_GetConfigstring( CS_PLAYERS + entnum, info2, sizeof( info2 ) ); + // + if ( atoi( Info_ValueForKey( info1, "t" ) ) == atoi( Info_ValueForKey( info2, "t" ) ) ) { + return qtrue; + } + } + return qfalse; +} + +/* +================== +InFieldOfVision +================== +*/ +qboolean InFieldOfVision( vec3_t viewangles, float fov, vec3_t angles ) { + int i; + float diff, angle; + + for ( i = 0; i < 2; i++ ) { + angle = AngleMod( viewangles[i] ); + angles[i] = AngleMod( angles[i] ); + diff = angles[i] - angle; + if ( angles[i] > angle ) { + if ( diff > 180.0 ) { + diff -= 360.0; + } + } else { + if ( diff < -180.0 ) { + diff += 360.0; + } + } + if ( diff > 0 ) { + if ( diff > fov * 0.5 ) { + return qfalse; + } + } else { + if ( diff < -fov * 0.5 ) { + return qfalse; + } + } + } + return qtrue; +} + +/* +================== +BotEntityVisible + +returns visibility in the range [0, 1] taking fog and water surfaces into account +================== +*/ +float BotEntityVisible( int viewer, vec3_t eye, vec3_t viewangles, float fov, int ent ) { + int i, contents_mask, passent, hitent, infog, inwater, otherinfog, pc; + float fogdist, waterfactor, vis, bestvis; + bsp_trace_t trace; + aas_entityinfo_t entinfo; + vec3_t dir, entangles, start, end, middle; + + //calculate middle of bounding box + BotEntityInfo( ent, &entinfo ); + VectorAdd( entinfo.mins, entinfo.maxs, middle ); + VectorScale( middle, 0.5, middle ); + VectorAdd( entinfo.origin, middle, middle ); + //check if entity is within field of vision + VectorSubtract( middle, eye, dir ); + vectoangles( dir, entangles ); + if ( !InFieldOfVision( viewangles, fov, entangles ) ) { + return 0; + } + // + pc = trap_AAS_PointContents( eye ); + infog = ( pc & CONTENTS_SOLID ); + inwater = ( pc & ( CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER ) ); + // + bestvis = 0; + for ( i = 0; i < 3; i++ ) { + //if the point is not in potential visible sight + //if (!AAS_inPVS(eye, middle)) continue; + // + contents_mask = CONTENTS_SOLID | CONTENTS_PLAYERCLIP; + passent = viewer; + hitent = ent; + VectorCopy( eye, start ); + VectorCopy( middle, end ); + //if the entity is in water, lava or slime + if ( trap_AAS_PointContents( middle ) & ( CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER ) ) { + contents_mask |= ( CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER ); + } + //if eye is in water, lava or slime + if ( inwater ) { + if ( !( contents_mask & ( CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER ) ) ) { + passent = ent; + hitent = viewer; + VectorCopy( middle, start ); + VectorCopy( eye, end ); + } + contents_mask ^= ( CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER ); + } + //trace from start to end + BotAI_Trace( &trace, start, NULL, NULL, end, passent, contents_mask ); + //if water was hit + waterfactor = 1.0; + if ( trace.contents & ( CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER ) ) { + //if the water surface is translucent + if ( 1 ) { + //trace through the water + contents_mask &= ~( CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER ); + BotAI_Trace( &trace, trace.endpos, NULL, NULL, end, passent, contents_mask ); + waterfactor = 0.5; + } + } + //if a full trace or the hitent was hit + if ( trace.fraction >= 1 || trace.ent == hitent ) { + //check for fog, assuming there's only one fog brush where + //either the viewer or the entity is in or both are in + otherinfog = ( trap_AAS_PointContents( middle ) & CONTENTS_FOG ); + if ( infog && otherinfog ) { + VectorSubtract( trace.endpos, eye, dir ); + fogdist = VectorLength( dir ); + } else if ( infog ) { + VectorCopy( trace.endpos, start ); + BotAI_Trace( &trace, start, NULL, NULL, eye, viewer, CONTENTS_FOG ); + VectorSubtract( eye, trace.endpos, dir ); + fogdist = VectorLength( dir ); + } else if ( otherinfog ) { + VectorCopy( trace.endpos, end ); + BotAI_Trace( &trace, eye, NULL, NULL, end, viewer, CONTENTS_FOG ); + VectorSubtract( end, trace.endpos, dir ); + fogdist = VectorLength( dir ); + } else { + //if the entity and the viewer are not in fog assume there's no fog in between + fogdist = 0; + } + //decrease visibility with the view distance through fog + vis = 1 / ( ( fogdist * fogdist * 0.001 ) < 1 ? 1 : ( fogdist * fogdist * 0.001 ) ); + //if entering water visibility is reduced + vis *= waterfactor; + // + if ( vis > bestvis ) { + bestvis = vis; + } + //if pretty much no fog + if ( bestvis >= 0.95 ) { + return bestvis; + } + } + //check bottom and top of bounding box as well + if ( i == 0 ) { + middle[2] += entinfo.mins[2]; + } else if ( i == 1 ) { + middle[2] += entinfo.maxs[2] - entinfo.mins[2]; + } + } + return bestvis; +} + +/* +================== +BotFindEnemy +================== +*/ +int BotFindEnemy( bot_state_t *bs, int curenemy ) { + int i, healthdecrease; + float fov, dist, curdist, alertness, easyfragger, vis; + aas_entityinfo_t entinfo, curenemyinfo; + vec3_t dir, angles; + + alertness = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_ALERTNESS, 0, 1 ); + easyfragger = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_EASY_FRAGGER, 0, 1 ); + //check if the health decreased + healthdecrease = bs->lasthealth > bs->inventory[INVENTORY_HEALTH]; + //remember the current health value + bs->lasthealth = bs->inventory[INVENTORY_HEALTH]; + // + if ( curenemy >= 0 ) { + BotEntityInfo( curenemy, &curenemyinfo ); + VectorSubtract( curenemyinfo.origin, bs->origin, dir ); + curdist = VectorLength( dir ); + } else { + curdist = 0; + } + // + for ( i = 0; i < MAX_CLIENTS; i++ ) { + + if ( i == bs->client ) { + continue; + } + //if it's the current enemy + if ( i == curenemy ) { + continue; + } + // + BotEntityInfo( i, &entinfo ); + // + if ( !entinfo.valid ) { + continue; + } + //if the enemy isn't dead and the enemy isn't the bot self + if ( EntityIsDead( &entinfo ) || entinfo.number == bs->entitynum ) { + continue; + } + //if the enemy is invisible and not shooting + if ( EntityIsInvisible( &entinfo ) && !EntityIsShooting( &entinfo ) ) { + continue; + } + //if not an easy fragger don't shoot at chatting players + if ( easyfragger < 0.5 && EntityIsChatting( &entinfo ) ) { + continue; + } + // + if ( lastteleport_time > trap_AAS_Time() - 3 ) { + VectorSubtract( entinfo.origin, lastteleport_origin, dir ); + if ( VectorLength( dir ) < 70 ) { + continue; + } + } + //calculate the distance towards the enemy + VectorSubtract( entinfo.origin, bs->origin, dir ); + dist = VectorLength( dir ); + //if this enemy is further away than the current one + if ( curenemy >= 0 && dist > curdist ) { + continue; + } + //if the bot has no + if ( dist > 900 + alertness * 4000 ) { + continue; + } + //if on the same team + if ( BotSameTeam( bs, i ) ) { + continue; + } + //if the bot's health decreased or the enemy is shooting + if ( curenemy < 0 && ( healthdecrease || EntityIsShooting( &entinfo ) ) ) { + fov = 360; + } else { fov = 90 + 270 - ( 270 - ( dist > 810 ? 810 : dist ) / 3 );} + //check if the enemy visibility + vis = BotEntityVisible( bs->entitynum, bs->eye, bs->viewangles, fov, i ); + if ( vis <= 0 ) { + continue; + } + //if the enemy is quite far away, not shooting and the bot is not damaged + if ( curenemy < 0 && dist > 200 && !healthdecrease && !EntityIsShooting( &entinfo ) ) { + //check if we can avoid this enemy + VectorSubtract( bs->origin, entinfo.origin, dir ); + vectoangles( dir, angles ); + //if the bot isn't in the fov of the enemy + if ( !InFieldOfVision( entinfo.angles, 120, angles ) ) { + //update some stuff for this enemy + BotUpdateBattleInventory( bs, i ); + //if the bot doesn't really want to fight + if ( BotWantsToRetreat( bs ) ) { + continue; + } + } + } + //found an enemy + bs->enemy = entinfo.number; + if ( curenemy >= 0 ) { + bs->enemysight_time = trap_AAS_Time() - 2; + } else { bs->enemysight_time = trap_AAS_Time();} + bs->enemysuicide = qfalse; + bs->enemydeath_time = 0; + return qtrue; + } + return qfalse; +} + +/* +================== +BotAimAtEnemy +================== +*/ +void BotAimAtEnemy( bot_state_t *bs ) { + int i, enemyvisible; + float dist, f, aim_skill, aim_accuracy, speed, reactiontime; + vec3_t dir, bestorigin, end, start, groundtarget, cmdmove, enemyvelocity; + vec3_t mins = {-4,-4,-4}, maxs = {4, 4, 4}; + weaponinfo_t wi; + aas_entityinfo_t entinfo; + bot_goal_t goal; + bsp_trace_t trace; + vec3_t target; + + //if the bot has no enemy + if ( bs->enemy < 0 ) { + return; + } + // + //BotAI_Print(PRT_MESSAGE, "client %d: aiming at client %d\n", bs->entitynum, bs->enemy); + // + aim_skill = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_AIM_SKILL, 0, 1 ); + aim_accuracy = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_AIM_ACCURACY, 0, 1 ); + // + if ( aim_skill > 0.95 ) { + //don't aim too early + reactiontime = 0.5 * trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_REACTIONTIME, 0, 1 ); + if ( bs->enemysight_time > trap_AAS_Time() - reactiontime ) { + return; + } + if ( bs->teleport_time > trap_AAS_Time() - reactiontime ) { + return; + } + } + + //get the weapon information + trap_BotGetWeaponInfo( bs->ws, bs->weaponnum, &wi ); + //get the weapon specific aim accuracy and or aim skill +//----(SA) commented out the weapons that aren't ours. +//----(SA) if we're not using this routine at all and my changes are irrelivant, please let me know. +// if (wi.number == WP_MACHINEGUN) { +// aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY_MACHINEGUN, 0, 1); +// } +// if (wi.number == WP_SHOTGUN) { +// aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY_SHOTGUN, 0, 1); +// } + if ( wi.number == WP_GRENADE_LAUNCHER ) { + aim_accuracy = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_AIM_ACCURACY_GRENADELAUNCHER, 0, 1 ); + aim_skill = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_AIM_SKILL_GRENADELAUNCHER, 0, 1 ); + } + if ( wi.number == WP_FLAMETHROWER ) { + aim_accuracy = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_AIM_ACCURACY_LIGHTNING, 0, 1 ); + } +// if (wi.number == WP_RAILGUN) { +// aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY_RAILGUN, 0, 1); +// } + if ( wi.number == WP_SILENCER ) { + aim_accuracy = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_AIM_ACCURACY_SP5, 0, 1 ); + aim_skill = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_AIM_SKILL_SP5, 0, 1 ); + } +// if (wi.number == WP_BFG) { +// aim_accuracy = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_ACCURACY_BFG10K, 0, 1); +// aim_skill = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_AIM_SKILL_BFG10K, 0, 1); +// } + // + if ( aim_accuracy <= 0 ) { + aim_accuracy = 0.0001; + } + //get the enemy entity information + BotEntityInfo( bs->enemy, &entinfo ); + //if the enemy is invisible then shoot crappy most of the time + if ( EntityIsInvisible( &entinfo ) ) { + if ( random() > 0.1 ) { + aim_accuracy *= 0.4; + } + } + // + VectorSubtract( entinfo.origin, entinfo.lastvisorigin, enemyvelocity ); + VectorScale( enemyvelocity, 1 / entinfo.update_time, enemyvelocity ); + //enemy origin and velocity is remembered every 0.5 seconds + if ( bs->enemyposition_time < trap_AAS_Time() ) { + // + bs->enemyposition_time = trap_AAS_Time() + 0.5; + VectorCopy( enemyvelocity, bs->enemyvelocity ); + VectorCopy( entinfo.origin, bs->enemyorigin ); + } + //if not extremely skilled + if ( aim_skill < 0.9 ) { + VectorSubtract( entinfo.origin, bs->enemyorigin, dir ); + //if the enemy moved a bit + if ( VectorLength( dir ) > 48 ) { + //if the enemy changed direction + if ( DotProduct( bs->enemyvelocity, enemyvelocity ) < 0 ) { + //aim accuracy should be worse now + aim_accuracy *= 0.7; + } + } + } + //check visibility of enemy + enemyvisible = BotEntityVisible( bs->entitynum, bs->eye, bs->viewangles, 360, bs->enemy ); + //if the enemy is visible + if ( enemyvisible ) { + // + VectorCopy( entinfo.origin, bestorigin ); + bestorigin[2] += 8; + //get the start point shooting from + //NOTE: the x and y projectile start offsets are ignored + VectorCopy( bs->origin, start ); + start[2] += bs->cur_ps.viewheight; + start[2] += wi.offset[2]; + // + BotAI_Trace( &trace, start, mins, maxs, bestorigin, bs->entitynum, MASK_SHOT ); + //if the enemy is NOT hit + if ( trace.fraction <= 1 && trace.ent != entinfo.number ) { + bestorigin[2] += 16; + } + //if it is not an instant hit weapon the bot might want to predict the enemy + if ( wi.speed ) { + // + VectorSubtract( bestorigin, bs->origin, dir ); + dist = VectorLength( dir ); + VectorSubtract( entinfo.origin, bs->enemyorigin, dir ); + //if the enemy is NOT pretty far away and strafing just small steps left and right + if ( !( dist > 100 && VectorLength( dir ) < 32 ) ) { + //if skilled anough do exact prediction + if ( aim_skill > 0.8 && + //if the weapon is ready to fire + bs->cur_ps.weaponstate == WEAPON_READY ) { + aas_clientmove_t move; + vec3_t origin; + + VectorSubtract( entinfo.origin, bs->origin, dir ); + //distance towards the enemy + dist = VectorLength( dir ); + //direction the enemy is moving in + VectorSubtract( entinfo.origin, entinfo.lastvisorigin, dir ); + // + VectorScale( dir, 1 / entinfo.update_time, dir ); + // + VectorCopy( entinfo.origin, origin ); + origin[2] += 1; + // + VectorClear( cmdmove ); + //AAS_ClearShownDebugLines(); + trap_AAS_PredictClientMovement( &move, bs->enemy, origin, + PRESENCE_CROUCH, qfalse, + dir, cmdmove, 0, + dist * 10 / wi.speed, 0.1, 0, 0, qfalse ); + VectorCopy( move.endpos, bestorigin ); + //BotAI_Print(PRT_MESSAGE, "%1.1f predicted speed = %f, frames = %f\n", trap_AAS_Time(), VectorLength(dir), dist * 10 / wi.speed); + } + //if not that skilled do linear prediction + else if ( aim_skill > 0.4 ) { + VectorSubtract( entinfo.origin, bs->origin, dir ); + //distance towards the enemy + dist = VectorLength( dir ); + //direction the enemy is moving in + VectorSubtract( entinfo.origin, entinfo.lastvisorigin, dir ); + dir[2] = 0; + // + speed = VectorNormalize( dir ) / entinfo.update_time; + //botimport.Print(PRT_MESSAGE, "speed = %f, wi->speed = %f\n", speed, wi->speed); + //best spot to aim at + VectorMA( entinfo.origin, ( dist / wi.speed ) * speed, dir, bestorigin ); + } + } + } + //if the projectile does radial damage + if ( aim_skill > 0.6 && wi.proj.damagetype & DAMAGETYPE_RADIAL ) { + //if the enemy isn't standing significantly higher than the bot + if ( entinfo.origin[2] < bs->origin[2] + 16 ) { + //try to aim at the ground in front of the enemy + VectorCopy( entinfo.origin, end ); + end[2] -= 64; + BotAI_Trace( &trace, entinfo.origin, NULL, NULL, end, entinfo.number, MASK_SHOT ); + // + VectorCopy( bestorigin, groundtarget ); + if ( trace.startsolid ) { + groundtarget[2] = entinfo.origin[2] - 16; + } else { groundtarget[2] = trace.endpos[2] - 8;} + //trace a line from projectile start to ground target + BotAI_Trace( &trace, start, NULL, NULL, groundtarget, bs->entitynum, MASK_SHOT ); + //if hitpoint is not vertically too far from the ground target + if ( fabs( trace.endpos[2] - groundtarget[2] ) < 50 ) { + VectorSubtract( trace.endpos, groundtarget, dir ); + //if the hitpoint is near anough the ground target + if ( VectorLength( dir ) < 60 ) { + VectorSubtract( trace.endpos, start, dir ); + //if the hitpoint is far anough from the bot + if ( VectorLength( dir ) > 100 ) { + //check if the bot is visible from the ground target + trace.endpos[2] += 1; + BotAI_Trace( &trace, trace.endpos, NULL, NULL, entinfo.origin, entinfo.number, MASK_SHOT ); + if ( trace.fraction >= 1 ) { + //botimport.Print(PRT_MESSAGE, "%1.1f aiming at ground\n", AAS_Time()); + VectorCopy( groundtarget, bestorigin ); + } + } + } + } + } + } + bestorigin[0] += 20 * crandom() * ( 1 - aim_accuracy ); + bestorigin[1] += 20 * crandom() * ( 1 - aim_accuracy ); + bestorigin[2] += 10 * crandom() * ( 1 - aim_accuracy ); + } else { + // + VectorCopy( bs->lastenemyorigin, bestorigin ); + bestorigin[2] += 8; + //if the bot is skilled anough + if ( aim_skill > 0.5 ) { + //do prediction shots around corners +// if (wi.number == WP_BFG || //----(SA) removing old weapon references + if ( wi.number == WP_GRENADE_LAUNCHER ) { + //create the chase goal + goal.entitynum = bs->client; + goal.areanum = bs->areanum; + VectorCopy( bs->eye, goal.origin ); + VectorSet( goal.mins, -8, -8, -8 ); + VectorSet( goal.maxs, 8, 8, 8 ); + // + if ( trap_BotPredictVisiblePosition( bs->lastenemyorigin, bs->lastenemyareanum, &goal, TFL_DEFAULT, target ) ) { + VectorCopy( target, bestorigin ); + bestorigin[2] -= 20; + } + aim_accuracy = 1; + } + } + } + // + if ( enemyvisible ) { + BotAI_Trace( &trace, bs->eye, NULL, NULL, bestorigin, bs->entitynum, MASK_SHOT ); + VectorCopy( trace.endpos, bs->aimtarget ); + } else { + VectorCopy( bestorigin, bs->aimtarget ); + } + //get aim direction + VectorSubtract( bestorigin, bs->eye, dir ); + // + if ( wi.number == WP_FLAMETHROWER ) { +// if (wi.number == WP_MACHINEGUN || //----(SA) removing old weapon references +// wi.number == WP_SHOTGUN || +// wi.number == WP_RAILGUN) { + //distance towards the enemy + dist = VectorLength( dir ); + if ( dist > 150 ) { + dist = 150; + } + f = 0.6 + dist / 150 * 0.4; + aim_accuracy *= f; + } + //add some random stuff to the aim direction depending on the aim accuracy + if ( aim_accuracy < 0.8 ) { + VectorNormalize( dir ); + for ( i = 0; i < 3; i++ ) dir[i] += 0.3 * crandom() * ( 1 - aim_accuracy ); + } + //set the ideal view angles + vectoangles( dir, bs->ideal_viewangles ); + //take the weapon spread into account for lower skilled bots + bs->ideal_viewangles[PITCH] += 6 * wi.vspread * crandom() * ( 1 - aim_accuracy ); + bs->ideal_viewangles[PITCH] = AngleMod( bs->ideal_viewangles[PITCH] ); + bs->ideal_viewangles[YAW] += 6 * wi.hspread * crandom() * ( 1 - aim_accuracy ); + bs->ideal_viewangles[YAW] = AngleMod( bs->ideal_viewangles[YAW] ); + //if the bot is really accurate and has the enemy in view for some time + if ( aim_accuracy > 0.9 && bs->enemysight_time < trap_AAS_Time() - 1 ) { + //set the view angles directly + if ( bs->ideal_viewangles[PITCH] > 180 ) { + bs->ideal_viewangles[PITCH] -= 360; + } + VectorCopy( bs->ideal_viewangles, bs->viewangles ); + trap_EA_View( bs->client, bs->viewangles ); + } +} + +/* +================== +BotCheckAttack +================== +*/ +void BotCheckAttack( bot_state_t *bs ) { + float points, reactiontime, fov, firethrottle; + bsp_trace_t bsptrace; + //float selfpreservation; + vec3_t forward, right, start, end, dir, angles; + weaponinfo_t wi; + bsp_trace_t trace; + aas_entityinfo_t entinfo; + vec3_t mins = {-8, -8, -8}, maxs = {8, 8, 8}; + + if ( bs->enemy < 0 ) { + return; + } + // + reactiontime = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_REACTIONTIME, 0, 1 ); + if ( bs->enemysight_time > trap_AAS_Time() - reactiontime ) { + return; + } + if ( bs->teleport_time > trap_AAS_Time() - reactiontime ) { + return; + } + //if changing weapons + if ( bs->weaponchange_time > trap_AAS_Time() - 0.1 ) { + return; + } + //check fire throttle characteristic + if ( bs->firethrottlewait_time > trap_AAS_Time() ) { + return; + } + firethrottle = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_FIRETHROTTLE, 0, 1 ); + if ( bs->firethrottleshoot_time < trap_AAS_Time() ) { + if ( random() > firethrottle ) { + bs->firethrottlewait_time = trap_AAS_Time() + firethrottle; + bs->firethrottleshoot_time = 0; + } else { + bs->firethrottleshoot_time = trap_AAS_Time() + 1 - firethrottle; + bs->firethrottlewait_time = 0; + } + } + // + BotEntityInfo( bs->enemy, &entinfo ); + VectorSubtract( entinfo.origin, bs->eye, dir ); + // + if ( VectorLength( dir ) < 100 ) { + fov = 120; + } else { fov = 50;} + /* + //if the enemy isn't visible + if (!BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, fov, bs->enemy)) { + //botimport.Print(PRT_MESSAGE, "enemy not visible\n"); + return; + }*/ + vectoangles( dir, angles ); + if ( !InFieldOfVision( bs->viewangles, fov, angles ) ) { + return; + } + BotAI_Trace( &bsptrace, bs->eye, NULL, NULL, bs->aimtarget, bs->client, CONTENTS_SOLID | CONTENTS_PLAYERCLIP ); + if ( bsptrace.fraction < 1 && bsptrace.ent != bs->enemy ) { + return; + } + + //get the weapon info + trap_BotGetWeaponInfo( bs->ws, bs->weaponnum, &wi ); + //get the start point shooting from + VectorCopy( bs->origin, start ); + start[2] += bs->cur_ps.viewheight; + AngleVectors( bs->viewangles, forward, right, NULL ); + start[0] += forward[0] * wi.offset[0] + right[0] * wi.offset[1]; + start[1] += forward[1] * wi.offset[0] + right[1] * wi.offset[1]; + start[2] += forward[2] * wi.offset[0] + right[2] * wi.offset[1] + wi.offset[2]; + //end point aiming at + VectorMA( start, 1000, forward, end ); + //a little back to make sure not inside a very close enemy + VectorMA( start, -12, forward, start ); + BotAI_Trace( &trace, start, mins, maxs, end, bs->entitynum, MASK_SHOT ); //----(SA) should this maybe check the weapon type and adjust the clipflag? it seems like this is probably fine as-is, but I thought I'd note it. + //if won't hit the enemy + if ( trace.ent != bs->enemy ) { + //if the entity is a client + if ( trace.ent > 0 && trace.ent <= MAX_CLIENTS ) { + //if a teammate is hit + if ( BotSameTeam( bs, trace.ent ) ) { + return; + } + } + //if the projectile does a radial damage + if ( wi.proj.damagetype & DAMAGETYPE_RADIAL ) { + if ( trace.fraction * 1000 < wi.proj.radius ) { + points = ( wi.proj.damage - 0.5 * trace.fraction * 1000 ) * 0.5; + if ( points > 0 ) { +// selfpreservation = trap_Characteristic_BFloat(bs->character, CHARACTERISTIC_SELFPRESERVATION, 0, 1); +// if (random() < selfpreservation) return; + return; + } + } + //FIXME: check if a teammate gets radial damage + } + } + //if fire has to be release to activate weapon + if ( wi.flags & WFL_FIRERELEASED ) { + if ( bs->flags & BFL_ATTACKED ) { + trap_EA_Attack( bs->client ); + } + } else { + trap_EA_Attack( bs->client ); + } + bs->flags ^= BFL_ATTACKED; +} + +/* +================== +BotMapScripts +================== +*/ +void BotMapScripts( bot_state_t *bs ) { + char info[1024]; + char mapname[128]; + int i, shootbutton; + float aim_accuracy; + aas_entityinfo_t entinfo; + vec3_t dir; + + trap_GetServerinfo( info, sizeof( info ) ); + + strncpy( mapname, Info_ValueForKey( info, "mapname" ), sizeof( mapname ) - 1 ); + mapname[sizeof( mapname ) - 1] = '\0'; + + if ( !Q_stricmp( mapname, "q3tourney6" ) ) { + vec3_t mins = {700, 204, 672}, maxs = {964, 468, 680}; + vec3_t buttonorg = {304, 352, 920}; + //NOTE: NEVER use the func_bobbing in q3tourney6 + bs->tfl &= ~TFL_FUNCBOB; + //if the bot is below the bounding box + if ( bs->origin[0] > mins[0] && bs->origin[0] < maxs[0] ) { + if ( bs->origin[1] > mins[1] && bs->origin[1] < maxs[1] ) { + if ( bs->origin[2] < mins[2] ) { + return; + } + } + } + shootbutton = qfalse; + //if an enemy is below this bounding box then shoot the button + for ( i = 0; i < MAX_CLIENTS; i++ ) { + + if ( i == bs->client ) { + continue; + } + // + BotEntityInfo( i, &entinfo ); + // + if ( !entinfo.valid ) { + continue; + } + //if the enemy isn't dead and the enemy isn't the bot self + if ( EntityIsDead( &entinfo ) || entinfo.number == bs->entitynum ) { + continue; + } + // + if ( entinfo.origin[0] > mins[0] && entinfo.origin[0] < maxs[0] ) { + if ( entinfo.origin[1] > mins[1] && entinfo.origin[1] < maxs[1] ) { + if ( entinfo.origin[2] < mins[2] ) { + //if there's a team mate below the crusher + if ( BotSameTeam( bs, i ) ) { + shootbutton = qfalse; + break; + } else { + shootbutton = qtrue; + } + } + } + } + } + if ( shootbutton ) { + bs->flags |= BFL_IDEALVIEWSET; + VectorSubtract( buttonorg, bs->eye, dir ); + vectoangles( dir, bs->ideal_viewangles ); + aim_accuracy = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_AIM_ACCURACY, 0, 1 ); + bs->ideal_viewangles[PITCH] += 8 * crandom() * ( 1 - aim_accuracy ); + bs->ideal_viewangles[PITCH] = AngleMod( bs->ideal_viewangles[PITCH] ); + bs->ideal_viewangles[YAW] += 8 * crandom() * ( 1 - aim_accuracy ); + bs->ideal_viewangles[YAW] = AngleMod( bs->ideal_viewangles[YAW] ); + // + if ( InFieldOfVision( bs->viewangles, 20, bs->ideal_viewangles ) ) { + trap_EA_Attack( bs->client ); + } + } + } +} + +/* +================== +BotCheckButtons +================== +*/ +/* +void CheckButtons(void) +{ + int modelindex, i, numbuttons = 0; + char *classname, *model; + float lip, health, dist; + bsp_entity_t *ent; + vec3_t mins, maxs, size, origin, angles, movedir, goalorigin; + vec3_t start, end, bboxmins, bboxmaxs; + aas_trace_t trace; + + for (ent = entities; ent; ent = ent->next) + { + classname = AAS_ValueForBSPEpairKey(ent, "classname"); + if (!strcmp(classname, "func_button")) + { + //create a bot goal towards the button + model = AAS_ValueForBSPEpairKey(ent, "model"); + modelindex = AAS_IndexFromModel(model); + //if the model is not loaded + if (!modelindex) modelindex = atoi(model+1); + VectorClear(angles); + AAS_BSPModelMinsMaxsOrigin(modelindex - 1, angles, mins, maxs, NULL); + //get the lip of the button + lip = AAS_FloatForBSPEpairKey(ent, "lip"); + if (!lip) lip = 4; + //get the move direction from the angle + VectorSet(angles, 0, AAS_FloatForBSPEpairKey(ent, "angle"), 0); + AAS_SetMovedir(angles, movedir); + //button size + VectorSubtract(maxs, mins, size); + //button origin + VectorAdd(mins, maxs, origin); + VectorScale(origin, 0.5, origin); + //touch distance of the button + dist = fabs(movedir[0]) * size[0] + fabs(movedir[1]) * size[1] + fabs(movedir[2]) * size[2];// - lip; + dist *= 0.5; + // + health = AAS_FloatForBSPEpairKey(ent, "health"); + //if the button is shootable + if (health) + { + //calculate the goal origin + VectorMA(origin, -dist, movedir, goalorigin); + AAS_DrawPermanentCross(goalorigin, 4, LINECOLOR_BLUE); + } //end if + else + { + //add bounding box size to the dist + AAS_PresenceTypeBoundingBox(PRESENCE_CROUCH, bboxmins, bboxmaxs); + for (i = 0; i < 3; i++) + { + if (movedir[i] < 0) dist += fabs(movedir[i]) * fabs(bboxmaxs[i]); + else dist += fabs(movedir[i]) * fabs(bboxmins[i]); + } //end for + //calculate the goal origin + VectorMA(origin, -dist, movedir, goalorigin); + // + VectorCopy(goalorigin, start); + start[2] += 24; + VectorSet(end, start[0], start[1], start[2] - 100); + trace = AAS_TraceClientBBox(start, end, PRESENCE_CROUCH, -1); + if (!trace.startsolid) + { + VectorCopy(trace.endpos, goalorigin); + } //end if + // + AAS_DrawPermanentCross(goalorigin, 4, LINECOLOR_YELLOW); + // + VectorSubtract(mins, origin, mins); + VectorSubtract(maxs, origin, maxs); + // + VectorAdd(mins, origin, start); + AAS_DrawPermanentCross(start, 4, LINECOLOR_BLUE); + VectorAdd(maxs, origin, start); + AAS_DrawPermanentCross(start, 4, LINECOLOR_BLUE); + } //end else + if (++numbuttons > 5) return; + } //end if + } //end for +} //end of the function CheckButtons +*/ + +/* +================== +BotEntityToActivate +================== +*/ +//#define OBSTACLEDEBUG + +int BotEntityToActivate( int entitynum ) { + int i, ent, cur_entities[10]; + char model[MAX_INFO_STRING], tmpmodel[128]; + char target[128], classname[128]; + float health; + char targetname[10][128]; + aas_entityinfo_t entinfo; + + BotEntityInfo( entitynum, &entinfo ); + Com_sprintf( model, sizeof( model ), "*%d", entinfo.modelindex ); + for ( ent = trap_AAS_NextBSPEntity( 0 ); ent; ent = trap_AAS_NextBSPEntity( ent ) ) { + if ( !trap_AAS_ValueForBSPEpairKey( ent, "model", tmpmodel, sizeof( tmpmodel ) ) ) { + continue; + } + if ( !strcmp( model, tmpmodel ) ) { + break; + } + } + if ( !ent ) { + BotAI_Print( PRT_ERROR, "BotEntityToActivate: no entity found with model %s\n", model ); + return 0; + } + trap_AAS_ValueForBSPEpairKey( ent, "classname", classname, sizeof( classname ) ); + if ( !classname ) { + BotAI_Print( PRT_ERROR, "BotEntityToActivate: entity with model %s has no classname\n", model ); + return 0; + } + //if it is a door + if ( !strcmp( classname, "func_door" ) ) { + if ( trap_AAS_FloatForBSPEpairKey( ent, "health", &health ) ) { + //if health the door must be shot to open + if ( health ) { + return ent; + } + } + } + //get the targetname so we can find an entity with a matching target + if ( !trap_AAS_ValueForBSPEpairKey( ent, "targetname", targetname[0], sizeof( targetname[0] ) ) ) { +#ifdef OBSTACLEDEBUG + BotAI_Print( PRT_ERROR, "BotEntityToActivate: entity with model \"%s\" has no targetname\n", model ); +#endif //OBSTACLEDEBUG + return 0; + } + cur_entities[0] = trap_AAS_NextBSPEntity( 0 ); + for ( i = 0; i >= 0 && i < 10; ) { + for ( ent = cur_entities[i]; ent; ent = trap_AAS_NextBSPEntity( ent ) ) { + if ( !trap_AAS_ValueForBSPEpairKey( ent, "target", target, sizeof( target ) ) ) { + continue; + } + if ( !strcmp( targetname[i], target ) ) { + cur_entities[i] = trap_AAS_NextBSPEntity( ent ); + break; + } + } + if ( !ent ) { + BotAI_Print( PRT_ERROR, "BotEntityToActivate: no entity with target \"%s\"\n", targetname[i] ); + i--; + continue; + } + if ( !trap_AAS_ValueForBSPEpairKey( ent, "classname", classname, sizeof( classname ) ) ) { + BotAI_Print( PRT_ERROR, "BotEntityToActivate: entity with target \"%s\" has no classname\n", targetname[i] ); + continue; + } + if ( !strcmp( classname, "func_button" ) ) { + //BSP button model + return ent; + } else if ( !strcmp( classname, "trigger_multiple" ) ) { + //invisible trigger multiple box + return ent; + } else { + i--; + } + } + BotAI_Print( PRT_ERROR, "BotEntityToActivate: unknown activator with classname \"%s\"\n", classname ); + return 0; +} + +/* +================== +BotSetMovedir +================== +*/ +vec3_t VEC_UP = {0, -1, 0}; +vec3_t MOVEDIR_UP = {0, 0, 1}; +vec3_t VEC_DOWN = {0, -2, 0}; +vec3_t MOVEDIR_DOWN = {0, 0, -1}; + +void BotSetMovedir( vec3_t angles, vec3_t movedir ) { + if ( VectorCompare( angles, VEC_UP ) ) { + VectorCopy( MOVEDIR_UP, movedir ); + } else if ( VectorCompare( angles, VEC_DOWN ) ) { + VectorCopy( MOVEDIR_DOWN, movedir ); + } else { + AngleVectors( angles, movedir, NULL, NULL ); + } +} + +void BotModelMinsMaxs( int modelindex, vec3_t mins, vec3_t maxs ) { + gentity_t *ent; + int i; + + ent = &g_entities[0]; + for ( i = 0; i < level.num_entities; i++, ent++ ) { + if ( !ent->inuse ) { + continue; + } + if ( ent->s.modelindex == modelindex ) { + VectorCopy( ent->r.mins, mins ); + VectorCopy( ent->r.maxs, maxs ); + return; + } + } + VectorClear( mins ); + VectorClear( maxs ); +} + +/* +================== +BotAIBlocked +================== +*/ +void BotAIBlocked( bot_state_t *bs, bot_moveresult_t *moveresult, int activate ) { + int movetype, ent, i, areas[10], numareas, modelindex; + char classname[128], model[128]; +#ifdef OBSTACLEDEBUG + char buf[128]; +#endif + float lip, dist, health, angle; + vec3_t hordir, size, start, end, mins, maxs, sideward, angles; + vec3_t movedir, origin, goalorigin, bboxmins, bboxmaxs; + vec3_t up = {0, 0, 1}, extramins = {-1, -1, -1}, extramaxs = {1, 1, 1}; + aas_entityinfo_t entinfo; +/* + bsp_trace_t bsptrace; +*/ +#ifdef OBSTACLEDEBUG + char netname[MAX_NETNAME]; +#endif + + if ( !moveresult->blocked ) { + return; + } + // + BotEntityInfo( moveresult->blockentity, &entinfo ); +#ifdef OBSTACLEDEBUG + ClientName( bs->client, netname, sizeof( netname ) ); + BotAI_Print( PRT_MESSAGE, "%s: I'm blocked by model %d\n", netname, entinfo.modelindex ); +#endif //OBSTACLEDEBUG + //if blocked by a bsp model and the bot wants to activate it if possible + if ( entinfo.modelindex > 0 && entinfo.modelindex <= max_bspmodelindex && activate ) { + //find the bsp entity which should be activated in order to remove + //the blocking entity + ent = BotEntityToActivate( entinfo.number ); + if ( !ent ) { + strcpy( classname, "" ); +#ifdef OBSTACLEDEBUG + BotAI_Print( PRT_MESSAGE, "%s: can't find activator for blocking entity\n", ClientName( bs->client, netname, sizeof( netname ) ) ); +#endif //OBSTACLEDEBUG + } else { + trap_AAS_ValueForBSPEpairKey( ent, "classname", classname, sizeof( classname ) ); +#ifdef OBSTACLEDEBUG + ClientName( bs->client, netname, sizeof( netname ) ); + BotAI_Print( PRT_MESSAGE, "%s: I should activate %s\n", netname, classname ); +#endif //OBSTACLEDEBUG + } +#ifdef OBSTACLEDEBUG +// ClientName(bs->client, netname, sizeof(netname)); +// BotAI_Print(PRT_MESSAGE, "%s: I've got no brain cells for activating entities\n", netname); +#endif //OBSTACLEDEBUG + /* + //the bot should now activate one of the following entities + //"func_button", "trigger_multiple", "func_door" + //all these activators use BSP models, so it should be a matter of + //finding where this model is located using AAS and then activating + //by walking against the model it or shooting at it + // + //if it is a door we should shoot at + if (!strcmp(classname, "func_door")) + { + //get the door model + model = AAS_ValueForBSPEpairKey(ent, "model"); + modelindex = AAS_IndexFromModel(model); + //if the model is not loaded + if (!modelindex) return; + VectorClear(angles); + AAS_BSPModelMinsMaxsOrigin(modelindex - 1, angles, mins, maxs, NULL); + //get a goal to shoot at + VectorAdd(maxs, mins, goalorigin); + VectorScale(goalorigin, 0.5, goalorigin); + VectorSubtract(goalorigin, bs->origin, movedir); + // + vectoangles(movedir, moveresult->ideal_viewangles); + moveresult->flags |= MOVERESULT_MOVEMENTVIEW; + //select the blaster + EA_UseItem(bs->client, "Blaster"); + //shoot + EA_Attack(bs->client); + // + return; + } //end if*/ + if ( !strcmp( classname, "func_button" ) ) { + //create a bot goal towards the button + trap_AAS_ValueForBSPEpairKey( ent, "model", model, sizeof( model ) ); + modelindex = atoi( model + 1 ); + //if the model is not loaded + if ( !modelindex ) { + return; + } + VectorClear( angles ); + BotModelMinsMaxs( modelindex, mins, maxs ); + //get the lip of the button + trap_AAS_FloatForBSPEpairKey( ent, "lip", &lip ); + if ( !lip ) { + lip = 4; + } + //get the move direction from the angle + trap_AAS_FloatForBSPEpairKey( ent, "angle", &angle ); + VectorSet( angles, 0, angle, 0 ); + BotSetMovedir( angles, movedir ); + //button size + VectorSubtract( maxs, mins, size ); + //button origin + VectorAdd( mins, maxs, origin ); + VectorScale( origin, 0.5, origin ); + //touch distance of the button + dist = fabs( movedir[0] ) * size[0] + fabs( movedir[1] ) * size[1] + fabs( movedir[2] ) * size[2]; + dist *= 0.5; + // + trap_AAS_FloatForBSPEpairKey( ent, "health", &health ); + //if the button is shootable + if ( health ) { + //calculate the goal origin + VectorMA( origin, -dist, movedir, goalorigin ); + // + //AAS_ClearShownDebugLines(); + //AAS_DrawArrow(bs->origin, goalorigin, LINECOLOR_BLUE, LINECOLOR_YELLOW); + // + VectorSubtract( goalorigin, bs->origin, movedir ); + vectoangles( movedir, moveresult->ideal_viewangles ); + moveresult->flags |= MOVERESULT_MOVEMENTVIEW; + //select the blaster + trap_EA_SelectWeapon( bs->client, WEAPONINDEX_MACHINEGUN ); + //shoot + trap_EA_Attack( bs->client ); + return; + } //end if + else + { + //add bounding box size to the dist + trap_AAS_PresenceTypeBoundingBox( PRESENCE_CROUCH, bboxmins, bboxmaxs ); + for ( i = 0; i < 3; i++ ) + { + if ( movedir[i] < 0 ) { + dist += fabs( movedir[i] ) * fabs( bboxmaxs[i] ); + } else { dist += fabs( movedir[i] ) * fabs( bboxmins[i] );} + } //end for + //calculate the goal origin + VectorMA( origin, -dist, movedir, goalorigin ); + // + VectorCopy( goalorigin, start ); + start[2] += 24; + VectorCopy( start, end ); + end[2] -= 100; + numareas = trap_AAS_TraceAreas( start, end, areas, NULL, 10 ); + // + for ( i = 0; i < numareas; i++ ) { + if ( trap_AAS_AreaReachability( areas[i] ) ) { + break; + } + } + if ( i < numareas ) { + // +#ifdef OBSTACLEDEBUG + if ( bs->activatemessage_time < trap_AAS_Time() ) { + Com_sprintf( buf, sizeof( buf ), "I have to activate a button at %1.1f %1.1f %1.1f in area %d\n", + goalorigin[0], goalorigin[1], goalorigin[2], areas[i] ); + trap_EA_Say( bs->client, buf ); + bs->activatemessage_time = trap_AAS_Time() + 5; + } //end if +#endif //OBSTACLEDEBUG + // + //VectorMA(origin, -dist, movedir, goalorigin); + // + VectorCopy( origin, bs->activategoal.origin ); + bs->activategoal.areanum = areas[i]; + VectorSubtract( mins, origin, bs->activategoal.mins ); + VectorSubtract( maxs, origin, bs->activategoal.maxs ); + // + VectorAdd( bs->activategoal.mins, extramins, bs->activategoal.mins ); + VectorAdd( bs->activategoal.maxs, extramaxs, bs->activategoal.maxs ); + // + bs->activategoal.entitynum = entinfo.number; + bs->activategoal.number = 0; + bs->activategoal.flags = 0; + bs->activate_time = trap_AAS_Time() + 10; + AIEnter_Seek_ActivateEntity( bs ); + } //end if + else + { +#ifdef OBSTACLEDEBUG + BotAI_Print( PRT_MESSAGE, "button area has no reachabilities\n" ); +#endif //OBSTACLEDEBUG + if ( bs->ainode == AINode_Seek_NBG ) { + bs->nbg_time = 0; + } else if ( bs->ainode == AINode_Seek_LTG ) { + bs->ltg_time = 0; + } + } //end else + } //end else + } //end if + /* + if (!strcmp(classname, "trigger_multiple")) + { + //create a bot goal towards the trigger + model = AAS_ValueForBSPEpairKey(ent, "model"); + modelindex = AAS_IndexFromModel(model); + //if the model is not precached (bad thing but happens) assume model is "*X" + if (!modelindex) modelindex = atoi(model+1); + VectorClear(angles); + AAS_BSPModelMinsMaxsOrigin(modelindex - 1, angles, mins, maxs, NULL); + VectorAdd(mins, maxs, mid); + VectorScale(mid, 0.5, mid); + VectorCopy(mid, start); + start[2] = maxs[2] + 24; + VectorSet(end, start[0], start[1], start[2] - 100); + trace = AAS_TraceClientBBox(start, end, PRESENCE_CROUCH, -1); + if (trace.startsolid) return; + //trace.endpos is now the goal origin + VectorCopy(trace.endpos, goalorigin); + // + #ifdef OBSTACLEDEBUG + if (bs->activatemessage_time < AAS_Time()) + { + Com_sprintf(buf, sizeof(buf), "I have to activate a trigger at %1.1f %1.1f %1.1f in area %d\n", + goalorigin[0], goalorigin[1], goalorigin[2], AAS_PointAreaNum(goalorigin)); + EA_Say(bs->client, buf); + bs->activatemessage_time = AAS_Time() + 5; + } //end if* / + #endif //OBSTACLEDEBUG + // + VectorCopy(mid, bs->activategoal.origin); + bs->activategoal.areanum = AAS_PointAreaNum(goalorigin); + VectorSubtract(mins, mid, bs->activategoal.mins); + VectorSubtract(maxs, mid, bs->activategoal.maxs); + bs->activategoal.entitynum = entinfo.number; + bs->activategoal.number = 0; + bs->activategoal.flags = 0; + bs->activate_time = AAS_Time() + 10; + if (!AAS_AreaReachability(bs->activategoal.areanum)) + { + #ifdef OBSTACLEDEBUG + botimport.Print(PRT_MESSAGE, "trigger area has no reachabilities\n"); + #endif //OBSTACLEDEBUG + if (bs->ainode == AINode_Seek_NBG) bs->nbg_time = 0; + else if (bs->ainode == AINode_Seek_LTG) bs->ltg_time = 0; + } //end if + else + { + AIEnter_Seek_ActivateEntity(bs); + } //end else + return; + } //end if*/ + } + //just some basic dynamic obstacle avoidance code + hordir[0] = moveresult->movedir[0]; + hordir[1] = moveresult->movedir[1]; + hordir[2] = 0; + //if no direction just take a random direction + if ( VectorNormalize( hordir ) < 0.1 ) { + VectorSet( angles, 0, 360 * random(), 0 ); + AngleVectors( angles, hordir, NULL, NULL ); + } + // +// if (moveresult->flags & MOVERESULT_ONTOPOFOBSTACLE) movetype = MOVE_JUMP; +// else + movetype = MOVE_WALK; + //if there's an obstacle at the bot's feet and head then + //the bot might be able to crouch through + VectorCopy( bs->origin, start ); + start[2] += 18; + VectorMA( start, 5, hordir, end ); + VectorSet( mins, -16, -16, -24 ); + VectorSet( maxs, 16, 16, 4 ); + // +// bsptrace = AAS_Trace(start, mins, maxs, end, bs->entitynum, MASK_PLAYERSOLID); +// if (bsptrace.fraction >= 1) movetype = MOVE_CROUCH; + //get the sideward vector + CrossProduct( hordir, up, sideward ); + // + if ( bs->flags & BFL_AVOIDRIGHT ) { + VectorNegate( sideward, sideward ); + } + //try to crouch straight forward? + if ( movetype != MOVE_CROUCH || !trap_BotMoveInDirection( bs->ms, hordir, 400, movetype ) ) { + //perform the movement + if ( !trap_BotMoveInDirection( bs->ms, sideward, 400, movetype ) ) { + //flip the avoid direction flag + bs->flags ^= BFL_AVOIDRIGHT; + //flip the direction + VectorNegate( sideward, sideward ); + //move in the other direction + trap_BotMoveInDirection( bs->ms, sideward, 400, movetype ); + } + } + //just reset goals and hope the bot will go into another direction + //still needed?? + if ( bs->ainode == AINode_Seek_NBG ) { + bs->nbg_time = 0; + } else if ( bs->ainode == AINode_Seek_LTG ) { + bs->ltg_time = 0; + } +} + +/* +================== +BotCheckConsoleMessages +================== +*/ +void BotCheckConsoleMessages( bot_state_t *bs ) { + char botname[MAX_NETNAME], message[MAX_MESSAGE_SIZE], netname[MAX_NETNAME]; + float chat_reply; + int context, handle; + bot_consolemessage_t m; + bot_match_t match; + + //the name of this bot + ClientName( bs->client, botname, sizeof( botname ) ); + // + while ( ( handle = trap_BotNextConsoleMessage( bs->cs, &m ) ) != 0 ) { + //if the chat state is flooded with messages the bot will read them quickly + if ( trap_BotNumConsoleMessages( bs->cs ) < 10 ) { + //if it is a chat message the bot needs some time to read it + if ( m.type == CMS_CHAT && m.time > trap_AAS_Time() - ( 1 + random() ) ) { + break; + } + } + //unify the white spaces in the message + trap_UnifyWhiteSpaces( m.message ); + //replace synonyms in the right context + context = CONTEXT_NORMAL | CONTEXT_NEARBYITEM | CONTEXT_NAMES; + if ( BotCTFTeam( bs ) == CTF_TEAM_RED ) { + context |= CONTEXT_CTFREDTEAM; + } else { context |= CONTEXT_CTFBLUETEAM;} + trap_BotReplaceSynonyms( m.message, context ); + //if there's no match + if ( !BotMatchMessage( bs, m.message ) ) { + //if it is a chat message + if ( m.type == CMS_CHAT && !bot_nochat.integer ) { + // + if ( !trap_BotFindMatch( m.message, &match, MTCONTEXT_REPLYCHAT ) ) { + trap_BotRemoveConsoleMessage( bs->cs, handle ); + continue; + } + //don't use eliza chats with team messages + if ( match.subtype & ST_TEAM ) { + trap_BotRemoveConsoleMessage( bs->cs, handle ); + continue; + } + // + trap_BotMatchVariable( &match, NETNAME, netname, sizeof( netname ) ); + trap_BotMatchVariable( &match, MESSAGE, message, sizeof( message ) ); + //if this is a message from the bot self + if ( !Q_stricmp( netname, botname ) ) { + trap_BotRemoveConsoleMessage( bs->cs, handle ); + continue; + } + //unify the message + trap_UnifyWhiteSpaces( message ); + // + trap_Cvar_Update( &bot_testrchat ); + if ( bot_testrchat.integer ) { + // + trap_BotLibVarSet( "bot_testrchat", "1" ); + //if bot replies with a chat message + if ( trap_BotReplyChat( bs->cs, message, context, CONTEXT_REPLY, + NULL, NULL, + NULL, NULL, + NULL, NULL, + botname, netname ) ) { + BotAI_Print( PRT_MESSAGE, "------------------------\n" ); + } else { + BotAI_Print( PRT_MESSAGE, "**** no valid reply ****\n" ); + } + } + //if at a valid chat position and not chatting already + else if ( bs->ainode != AINode_Stand && BotValidChatPosition( bs ) ) { + chat_reply = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_CHAT_REPLY, 0, 1 ); + if ( random() < 1.5 / ( NumBots() + 1 ) && random() < chat_reply ) { + //if bot replies with a chat message + if ( trap_BotReplyChat( bs->cs, message, context, CONTEXT_REPLY, + NULL, NULL, + NULL, NULL, + NULL, NULL, + botname, netname ) ) { + //remove the console message + trap_BotRemoveConsoleMessage( bs->cs, handle ); + bs->stand_time = trap_AAS_Time() + BotChatTime( bs ); + AIEnter_Stand( bs ); + //EA_Say(bs->client, bs->cs.chatmessage); + break; + } + } + } + } + } + //remove the console message + trap_BotRemoveConsoleMessage( bs->cs, handle ); + } +} + +/* +================== +BotCheckEvents +================== +*/ +void BotCheckEvents( bot_state_t *bs, entityState_t *state ) { + int event; + char buf[128]; + // + //this sucks, we're accessing the gentity_t directly but there's no other fast way + //to do it right now + if ( bs->entityeventTime[state->number] == g_entities[state->number].eventTime ) { + return; + } + bs->entityeventTime[state->number] = g_entities[state->number].eventTime; + //if it's an event only entity + if ( state->eType > ET_EVENTS ) { + event = ( state->eType - ET_EVENTS ) & ~EV_EVENT_BITS; + } else { + event = state->event & ~EV_EVENT_BITS; + } + // + switch ( event ) { + //client obituary event + case EV_OBITUARY: + { + int target, attacker, mod; + + target = state->otherEntityNum; + attacker = state->otherEntityNum2; + mod = state->eventParm; + // + if ( target == bs->client ) { + bs->botdeathtype = mod; + bs->lastkilledby = attacker; + // + if ( target == attacker ) { + bs->botsuicide = qtrue; + } else { bs->botsuicide = qfalse;} + // + bs->num_deaths++; + } + //else if this client was killed by the bot + else if ( attacker == bs->client ) { + bs->enemydeathtype = mod; + bs->lastkilledplayer = target; + bs->killedenemy_time = trap_AAS_Time(); + // + bs->num_kills++; + } else if ( attacker == bs->enemy && target == attacker ) { + bs->enemysuicide = qtrue; + } + break; + } + case EV_GLOBAL_SOUND: + { + if ( state->eventParm < 0 || state->eventParm > MAX_SOUNDS ) { + BotAI_Print( PRT_ERROR, "EV_GLOBAL_SOUND: eventParm (%d) out of range\n", state->eventParm ); + break; + } + trap_GetConfigstring( CS_SOUNDS + state->eventParm, buf, sizeof( buf ) ); + if ( !strcmp( buf, "sound/teamplay/flagret_red.wav" ) ) { + //red flag is returned + bs->redflagstatus = 0; + bs->flagstatuschanged = qtrue; + } else if ( !strcmp( buf, "sound/teamplay/flagret_blu.wav" ) ) { + //blue flag is returned + bs->blueflagstatus = 0; + bs->flagstatuschanged = qtrue; + } else if ( !strcmp( buf, "sound/items/poweruprespawn.wav" ) ) { + //powerup respawned... go get it + BotGoForPowerups( bs ); + } + break; + } + case EV_PLAYER_TELEPORT_IN: + { + VectorCopy( state->origin, lastteleport_origin ); + lastteleport_time = trap_AAS_Time(); + break; + } + case EV_GENERAL_SOUND: + { + //if this sound is played on the bot + if ( state->number == bs->client ) { + if ( state->eventParm < 0 || state->eventParm > MAX_SOUNDS ) { + BotAI_Print( PRT_ERROR, "EV_GENERAL_SOUND: eventParm (%d) out of range\n", state->eventParm ); + break; + } + //check out the sound + trap_GetConfigstring( CS_SOUNDS + state->eventParm, buf, sizeof( buf ) ); + //if falling into a death pit + if ( !strcmp( buf, "*falling1.wav" ) ) { + //if the bot has a personal teleporter + if ( bs->inventory[INVENTORY_TELEPORTER] > 0 ) { + //use the holdable item + trap_EA_Use( bs->client ); + } + } + } + break; + } + } +} + +/* +================== +BotCheckSnapshot +================== +*/ +void BotCheckSnapshot( bot_state_t *bs ) { + int ent; + entityState_t state; + + // + ent = 0; + while ( ( ent = BotAI_GetSnapshotEntity( bs->client, ent, &state ) ) != -1 ) { + //check the entity state for events + BotCheckEvents( bs, &state ); + } + //check the player state for events + BotAI_GetEntityState( bs->client, &state ); + //copy the player state events to the entity state + //state.event = bs->cur_ps.externalEvent; + //state.eventParm = bs->cur_ps.externalEventParm; + // + BotCheckEvents( bs, &state ); +} + +/* +================== +BotCheckAir +================== +*/ +void BotCheckAir( bot_state_t *bs ) { + if ( bs->inventory[INVENTORY_ENVIRONMENTSUIT] <= 0 ) { + if ( trap_AAS_PointContents( bs->eye ) & ( CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ) ) { + return; + } + } + bs->lastair_time = trap_AAS_Time(); +} + +/* +================== +BotDeathmatchAI +================== +*/ +void BotDeathmatchAI( bot_state_t *bs, float thinktime ) { + char gender[144], name[144], buf[144]; + char userinfo[MAX_INFO_STRING]; + int i; + + //if the bot has just been setup + if ( bs->setupcount > 0 ) { + bs->setupcount--; + if ( bs->setupcount > 0 ) { + return; + } + //get the gender characteristic + trap_Characteristic_String( bs->character, CHARACTERISTIC_GENDER, gender, sizeof( gender ) ); + //set the bot gender + trap_GetUserinfo( bs->client, userinfo, sizeof( userinfo ) ); + Info_SetValueForKey( userinfo, "sex", gender ); + trap_SetUserinfo( bs->client, userinfo ); + //set the team + if ( g_gametype.integer != GT_TOURNAMENT ) { + Com_sprintf( buf, sizeof( buf ), "team %s", bs->settings.team ); + trap_EA_Command( bs->client, buf ); + } + //set the chat gender + if ( gender[0] == 'm' ) { + trap_BotSetChatGender( bs->cs, CHAT_GENDERMALE ); + } else if ( gender[0] == 'f' ) { + trap_BotSetChatGender( bs->cs, CHAT_GENDERFEMALE ); + } else { trap_BotSetChatGender( bs->cs, CHAT_GENDERLESS );} + //set the chat name + ClientName( bs->client, name, sizeof( name ) ); + trap_BotSetChatName( bs->cs, name ); + // + bs->lastframe_health = bs->inventory[INVENTORY_HEALTH]; + bs->lasthitcount = bs->cur_ps.persistant[PERS_HITS]; + // + bs->setupcount = 0; + } + //no ideal view set + bs->flags &= ~BFL_IDEALVIEWSET; + //set the teleport time + BotSetTeleportTime( bs ); + //update some inventory values + BotUpdateInventory( bs ); + //check the console messages + BotCheckConsoleMessages( bs ); + //check out the snapshot + BotCheckSnapshot( bs ); + //check for air + BotCheckAir( bs ); + //if not in the intermission and not in observer mode + if ( !BotIntermission( bs ) && !BotIsObserver( bs ) ) { + //do team AI + BotTeamAI( bs ); + } + //if the bot has no ai node + if ( !bs->ainode ) { + AIEnter_Seek_LTG( bs ); + } + //if the bot entered the game less than 8 seconds ago + if ( !bs->entergamechat && bs->entergame_time > trap_AAS_Time() - 8 ) { + if ( BotChat_EnterGame( bs ) ) { + bs->stand_time = trap_AAS_Time() + BotChatTime( bs ); + AIEnter_Stand( bs ); + } + bs->entergamechat = qtrue; + } + //reset the node switches from the previous frame + BotResetNodeSwitches(); + //execute AI nodes + for ( i = 0; i < MAX_NODESWITCHES; i++ ) { + if ( bs->ainode( bs ) ) { + break; + } + } + //if the bot removed itself :) + if ( !bs->inuse ) { + return; + } + //if the bot executed too many AI nodes + if ( i >= MAX_NODESWITCHES ) { + trap_BotDumpGoalStack( bs->gs ); + trap_BotDumpAvoidGoals( bs->gs ); + BotDumpNodeSwitches( bs ); + ClientName( bs->client, name, sizeof( name ) ); + BotAI_Print( PRT_ERROR, "%s at %1.1f switched more than %d AI nodes\n", name, trap_AAS_Time(), MAX_NODESWITCHES ); + } + // + bs->lastframe_health = bs->inventory[INVENTORY_HEALTH]; + bs->lasthitcount = bs->cur_ps.persistant[PERS_HITS]; +} + +/* +================== +BotSetupDeathmatchAI +================== +*/ +void BotSetupDeathmatchAI( void ) { + int ent, modelnum; + char model[128]; + + gametype = trap_Cvar_VariableIntegerValue( "g_gametype" ); + + // Rafael gameskill + gameskill = trap_Cvar_VariableIntegerValue( "g_gameskill" ); + // done + + trap_Cvar_Register( &bot_rocketjump, "bot_rocketjump", "1", 0 ); + trap_Cvar_Register( &bot_grapple, "bot_grapple", "0", 0 ); + trap_Cvar_Register( &bot_fastchat, "bot_fastchat", "0", 0 ); + trap_Cvar_Register( &bot_nochat, "bot_nochat", "0", 0 ); + trap_Cvar_Register( &bot_testrchat, "bot_testrchat", "0", 0 ); + // + if ( gametype == GT_CTF ) { + if ( trap_BotGetLevelItemGoal( -1, "Red Flag", &ctf_redflag ) < 0 ) { + BotAI_Print( PRT_WARNING, "CTF without Red Flag\n" ); + } + if ( trap_BotGetLevelItemGoal( -1, "Blue Flag", &ctf_blueflag ) < 0 ) { + BotAI_Print( PRT_WARNING, "CTF without Blue Flag\n" ); + } + } + + max_bspmodelindex = 0; + for ( ent = trap_AAS_NextBSPEntity( 0 ); ent; ent = trap_AAS_NextBSPEntity( ent ) ) { + if ( !trap_AAS_ValueForBSPEpairKey( ent, "model", model, sizeof( model ) ) ) { + continue; + } + if ( model[0] == '*' ) { + modelnum = atoi( model + 1 ); + if ( modelnum > max_bspmodelindex ) { + max_bspmodelindex = modelnum; + } + } + } + //initialize the waypoint heap + BotInitWaypoints(); +} + +/* +================== +BotShutdownDeathmatchAI +================== +*/ +void BotShutdownDeathmatchAI( void ) { +} diff --git a/src/botai/ai_dmq3.h b/src/botai/ai_dmq3.h new file mode 100644 index 0000000..10eaae1 --- /dev/null +++ b/src/botai/ai_dmq3.h @@ -0,0 +1,156 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: ai_dmq3.h + * + * desc: Quake3 bot AI + * + * + *****************************************************************************/ + +//setup the deathmatch AI +void BotSetupDeathmatchAI( void ); +//shutdown the deathmatch AI +void BotShutdownDeathmatchAI( void ); +//let the bot live within it's deathmatch AI net +void BotDeathmatchAI( bot_state_t *bs, float thinktime ); +//free waypoints +void BotFreeWaypoints( bot_waypoint_t *wp ); +//choose a weapon +void BotChooseWeapon( bot_state_t *bs ); +//setup movement stuff +void BotSetupForMovement( bot_state_t *bs ); +//update the inventory +void BotUpdateInventory( bot_state_t *bs ); +//update the inventory during battle +void BotUpdateBattleInventory( bot_state_t *bs, int enemy ); +//use holdable items during battle +void BotBattleUseItems( bot_state_t *bs ); +//return true if the bot is dead +qboolean BotIsDead( bot_state_t *bs ); +//returns true if the bot is in observer mode +qboolean BotIsObserver( bot_state_t *bs ); +//returns true if the bot is in the intermission +qboolean BotIntermission( bot_state_t *bs ); +//returns true if the bot is in lava +qboolean BotInLava( bot_state_t *bs ); +//returns true if the bot is in slime +qboolean BotInSlime( bot_state_t *bs ); +//returns true if the entity is dead +qboolean EntityIsDead( aas_entityinfo_t *entinfo ); +//returns true if the entity is invisible +qboolean EntityIsInvisible( aas_entityinfo_t *entinfo ); +//returns true if the entity is shooting +qboolean EntityIsShooting( aas_entityinfo_t *entinfo ); +//returns the name of the client +char *ClientName( int client, char *name, int size ); +//returns an simplyfied client name +char *EasyClientName( int client, char *name, int size ); +//returns the skin used by the client +char *ClientSkin( int client, char *skin, int size ); +//returns the aggression of the bot in the range [0, 100] +float BotAggression( bot_state_t *bs ); +//returns true if the bot wants to retreat +int BotWantsToRetreat( bot_state_t *bs ); +//returns true if the bot wants to chase +int BotWantsToChase( bot_state_t *bs ); +//returns true if the bot wants to help +int BotWantsToHelp( bot_state_t *bs ); +//returns true if the bot can and wants to rocketjump +int BotCanAndWantsToRocketJump( bot_state_t *bs ); +//returns true if the bot wants to and goes camping +int BotWantsToCamp( bot_state_t *bs ); +//the bot will perform attack movements +bot_moveresult_t BotAttackMove( bot_state_t *bs, int tfl ); +//returns true if the bot and the entity are in the same team +int BotSameTeam( bot_state_t *bs, int entnum ); +//returns true if teamplay is on +int TeamPlayIsOn( void ); +//returns true and sets the .enemy field when an enemy is found +int BotFindEnemy( bot_state_t *bs, int curenemy ); +//returns a roam goal +void BotRoamGoal( bot_state_t *bs, vec3_t goal ); +//returns entity visibility in the range [0, 1] +float BotEntityVisible( int viewer, vec3_t eye, vec3_t viewangles, float fov, int ent ); +//the bot will aim at the current enemy +void BotAimAtEnemy( bot_state_t *bs ); +//check if the bot should attack +void BotCheckAttack( bot_state_t *bs ); +//AI when the bot is blocked +void BotAIBlocked( bot_state_t *bs, bot_moveresult_t *moveresult, int activate ); +//returns the CTF team the bot is in +int BotCTFTeam( bot_state_t *bs ); +//returns the flag the bot is carrying (CTFFLAG_?) +int BotCTFCarryingFlag( bot_state_t *bs ); +//set ctf goals (defend base, get enemy flag) during seek +void BotCTFSeekGoals( bot_state_t *bs ); +//set ctf goals (defend base, get enemy flag) during retreat +void BotCTFRetreatGoals( bot_state_t *bs ); +//create a new waypoint +bot_waypoint_t *BotCreateWayPoint( char *name, vec3_t origin, int areanum ); +//find a waypoint with the given name +bot_waypoint_t *BotFindWayPoint( bot_waypoint_t *waypoints, char *name ); +//strstr but case insensitive +char *stristr( char *str, char *charset ); +//returns the number of the client with the given name +int ClientFromName( char *name ); +// +int BotPointAreaNum( vec3_t origin ); +// +void BotMapScripts( bot_state_t *bs ); + +//ctf flags +#define CTF_FLAG_NONE 0 +#define CTF_FLAG_RED 1 +#define CTF_FLAG_BLUE 2 +//CTF skins +#define CTF_SKIN_REDTEAM "red" +#define CTF_SKIN_BLUETEAM "blue" +//CTF teams +#define CTF_TEAM_NONE 0 +#define CTF_TEAM_RED 1 +#define CTF_TEAM_BLUE 2 + +extern int dmflags; //deathmatch flags +extern int gametype; //game type + +// Rafael gameskill +extern int gameskill; +// done + +extern vmCvar_t bot_grapple; +extern vmCvar_t bot_rocketjump; +extern vmCvar_t bot_fastchat; +extern vmCvar_t bot_nochat; +extern vmCvar_t bot_testrchat; + +extern bot_goal_t ctf_redflag; +extern bot_goal_t ctf_blueflag; + diff --git a/src/botai/ai_main.c b/src/botai/ai_main.c new file mode 100644 index 0000000..1fca71e --- /dev/null +++ b/src/botai/ai_main.c @@ -0,0 +1,1208 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: ai_main.c + * + * desc: Quake3 bot AI + * + * + *****************************************************************************/ + +#include "../game/g_local.h" +#include "../game/q_shared.h" +#include "../game/botlib.h" //bot lib interface +#include "../game/be_aas.h" +#include "../game/be_ea.h" +#include "../game/be_ai_char.h" +#include "../game/be_ai_chat.h" +#include "../game/be_ai_gen.h" +#include "../game/be_ai_goal.h" +#include "../game/be_ai_move.h" +#include "../game/be_ai_weap.h" +#include "../botai/botai.h" //bot ai interface + +#include "ai_main.h" +#include "ai_dmq3.h" +#include "ai_chat.h" +#include "ai_cmd.h" +#include "ai_dmnet.h" +// +#include "chars.h" +#include "inv.h" +#include "syn.h" + +#define MAX_PATH 144 + +//bot states +bot_state_t *botstates[MAX_CLIENTS]; +//number of bots +int numbots; +//time to do a regular update +float regularupdate_time; +// +vmCvar_t bot_thinktime; +vmCvar_t memorydump; + + +/* +================== +BotAI_Print +================== +*/ +void QDECL BotAI_Print( int type, char *fmt, ... ) { + char str[2048]; + va_list ap; + + va_start( ap, fmt ); + vsprintf( str, fmt, ap ); + va_end( ap ); + + switch ( type ) { + case PRT_MESSAGE: { + G_Printf( "%s", str ); + break; + } + case PRT_WARNING: { + G_Printf( S_COLOR_YELLOW "Warning: %s", str ); + break; + } + case PRT_ERROR: { + G_Printf( S_COLOR_RED "Error: %s", str ); + break; + } + case PRT_FATAL: { + G_Printf( S_COLOR_RED "Fatal: %s", str ); + break; + } + case PRT_EXIT: { + G_Error( S_COLOR_RED "Exit: %s", str ); + break; + } + default: { + G_Printf( "unknown print type\n" ); + break; + } + } +} + +/* +================== +BotAI_Trace +================== +*/ +void BotAI_Trace( bsp_trace_t *bsptrace, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int passent, int contentmask ) { + trace_t trace; + + trap_Trace( &trace, start, mins, maxs, end, passent, contentmask ); + //copy the trace information + bsptrace->allsolid = trace.allsolid; + bsptrace->startsolid = trace.startsolid; + bsptrace->fraction = trace.fraction; + VectorCopy( trace.endpos, bsptrace->endpos ); + bsptrace->plane.dist = trace.plane.dist; + VectorCopy( trace.plane.normal, bsptrace->plane.normal ); + bsptrace->plane.signbits = trace.plane.signbits; + bsptrace->plane.type = trace.plane.type; + bsptrace->surface.value = trace.surfaceFlags; + bsptrace->ent = trace.entityNum; + bsptrace->exp_dist = 0; + bsptrace->sidenum = 0; + bsptrace->contents = 0; +} + +/* +================== +BotAI_GetClientState +================== +*/ +int BotAI_GetClientState( int clientNum, playerState_t *state ) { + gentity_t *ent; + + ent = &g_entities[clientNum]; + if ( !ent->inuse ) { + return qfalse; + } + if ( !ent->client ) { + return qfalse; + } + + memcpy( state, &ent->client->ps, sizeof( playerState_t ) ); + return qtrue; +} + +/* +================== +BotAI_GetEntityState +================== +*/ +int BotAI_GetEntityState( int entityNum, entityState_t *state ) { + gentity_t *ent; + + ent = &g_entities[entityNum]; + memset( state, 0, sizeof( entityState_t ) ); + if ( !ent->inuse ) { + return qfalse; + } + if ( !ent->r.linked ) { + return qfalse; + } + if ( ent->r.svFlags & SVF_NOCLIENT ) { + return qfalse; + } + memcpy( state, &ent->s, sizeof( entityState_t ) ); + return qtrue; +} + +/* +================== +BotAI_GetSnapshotEntity +================== +*/ +int BotAI_GetSnapshotEntity( int clientNum, int sequence, entityState_t *state ) { + int entNum; + + entNum = trap_BotGetSnapshotEntity( clientNum, sequence ); + if ( entNum == -1 ) { + memset( state, 0, sizeof( entityState_t ) ); + return -1; + } + + BotAI_GetEntityState( entNum, state ); + + return sequence + 1; +} + +/* +================== +BotAI_BotInitialChat +================== +*/ +void QDECL BotAI_BotInitialChat( bot_state_t *bs, char *type, ... ) { + int i, mcontext; + va_list ap; + char *p; + char *vars[MAX_MATCHVARIABLES]; + + memset( vars, 0, sizeof( vars ) ); + va_start( ap, type ); + p = va_arg( ap, char * ); + for ( i = 0; i < MAX_MATCHVARIABLES; i++ ) { + if ( !p ) { + break; + } + vars[i] = p; + p = va_arg( ap, char * ); + } + va_end( ap ); + + mcontext = CONTEXT_NORMAL | CONTEXT_NEARBYITEM | CONTEXT_NAMES; + if ( BotCTFTeam( bs ) == CTF_TEAM_RED ) { + mcontext |= CONTEXT_CTFREDTEAM; + } else { mcontext |= CONTEXT_CTFBLUETEAM;} + + trap_BotInitialChat( bs->cs, type, mcontext, vars[0], vars[1], vars[2], vars[3], vars[4], vars[5], vars[6], vars[7] ); +} + +/* +============== +BotInterbreeding +============== +*/ +void BotInterbreeding( void ) { + float ranks[MAX_CLIENTS]; + int parent1, parent2, child; + int i; + + // get rankings for all the bots + for ( i = 0; i < MAX_CLIENTS; i++ ) { + if ( botstates[i] && botstates[i]->inuse ) { + ranks[i] = botstates[i]->num_kills * 2 - botstates[i]->num_deaths; + } else { + ranks[i] = -1; + } + } + + if ( trap_GeneticParentsAndChildSelection( MAX_CLIENTS, ranks, &parent1, &parent2, &child ) ) { + trap_BotInterbreedGoalFuzzyLogic( botstates[parent1]->gs, botstates[parent2]->gs, botstates[child]->gs ); + trap_BotMutateGoalFuzzyLogic( botstates[child]->gs, 1 ); + } + // reset the kills and deaths + for ( i = 0; i < MAX_CLIENTS; i++ ) { + if ( botstates[i] && botstates[i]->inuse ) { + botstates[i]->num_kills = 0; + botstates[i]->num_deaths = 0; + } + } +} + +/* +============== +BotEntityInfo +============== +*/ +void BotEntityInfo( int entnum, aas_entityinfo_t *info ) { + trap_AAS_EntityInfo( entnum, info ); +} + +/* +============== +NumBots +============== +*/ +int NumBots( void ) { + return numbots; +} + +/* +============== +AngleDifference +============== +*/ +float AngleDifference( float ang1, float ang2 ) { + float diff; + + diff = ang1 - ang2; + if ( ang1 > ang2 ) { + if ( diff > 180.0 ) { + diff -= 360.0; + } + } else { + if ( diff < -180.0 ) { + diff += 360.0; + } + } + return diff; +} + +/* +============== +BotChangeViewAngle +============== +*/ +float BotChangeViewAngle( float angle, float ideal_angle, float speed ) { + float move; + + angle = AngleMod( angle ); + ideal_angle = AngleMod( ideal_angle ); + if ( angle == ideal_angle ) { + return angle; + } + move = ideal_angle - angle; + if ( ideal_angle > angle ) { + if ( move > 180.0 ) { + move -= 360.0; + } + } else { + if ( move < -180.0 ) { + move += 360.0; + } + } + if ( move > 0 ) { + if ( move > speed ) { + move = speed; + } + } else { + if ( move < -speed ) { + move = -speed; + } + } + return AngleMod( angle + move ); +} + +/* +============== +BotChangeViewAngles +============== +*/ +void BotChangeViewAngles( bot_state_t *bs, float thinktime ) { + float diff, factor, maxchange, anglespeed; + int i; + + if ( bs->ideal_viewangles[PITCH] > 180 ) { + bs->ideal_viewangles[PITCH] -= 360; + } + // + if ( bs->enemy >= 0 ) { + factor = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_VIEW_FACTOR, 0.01, 1 ); + maxchange = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_VIEW_MAXCHANGE, 1, 1800 ); + } else { + factor = 0.25; + maxchange = 300; + } + maxchange *= thinktime; + for ( i = 0; i < 2; i++ ) { + diff = abs( AngleDifference( bs->viewangles[i], bs->ideal_viewangles[i] ) ); + anglespeed = diff * factor; + if ( anglespeed > maxchange ) { + anglespeed = maxchange; + } + bs->viewangles[i] = BotChangeViewAngle( bs->viewangles[i], + bs->ideal_viewangles[i], anglespeed ); + //BotAI_Print(PRT_MESSAGE, "ideal_angles %f %f\n", bs->ideal_viewangles[0], bs->ideal_viewangles[1], bs->ideal_viewangles[2]);` + //bs->viewangles[i] = bs->ideal_viewangles[i]; + } + if ( bs->viewangles[PITCH] > 180 ) { + bs->viewangles[PITCH] -= 360; + } + //elementary action: view + trap_EA_View( bs->client, bs->viewangles ); +} + +/* +============== +BotInputToUserCommand +============== +*/ +void BotInputToUserCommand( bot_input_t *bi, usercmd_t *ucmd, int delta_angles[3], int time ) { + vec3_t angles, forward, right; + short temp; + int j; + + //clear the whole structure + memset( ucmd, 0, sizeof( usercmd_t ) ); + // + //Com_Printf("dir = %f %f %f speed = %f\n", bi->dir[0], bi->dir[1], bi->dir[2], bi->speed); + //the duration for the user command in milli seconds + ucmd->serverTime = time; + // + if ( bi->actionflags & ACTION_DELAYEDJUMP ) { + bi->actionflags |= ACTION_JUMP; + bi->actionflags &= ~ACTION_DELAYEDJUMP; + } + //set the buttons + if ( bi->actionflags & ACTION_RESPAWN ) { + ucmd->buttons = BUTTON_ATTACK; + } + if ( bi->actionflags & ACTION_ATTACK ) { + ucmd->buttons |= BUTTON_ATTACK; + } + if ( bi->actionflags & ACTION_TALK ) { + ucmd->buttons |= BUTTON_TALK; + } + if ( bi->actionflags & ACTION_GESTURE ) { + ucmd->buttons |= BUTTON_GESTURE; + } + if ( bi->actionflags & ACTION_USE ) { + ucmd->buttons |= BUTTON_USE_HOLDABLE; + } + if ( bi->actionflags & ACTION_WALK ) { + ucmd->buttons |= BUTTON_WALKING; + } + ucmd->weapon = bi->weapon; + //set the view angles + //NOTE: the ucmd->angles are the angles WITHOUT the delta angles + ucmd->angles[PITCH] = ANGLE2SHORT( bi->viewangles[PITCH] ); + ucmd->angles[YAW] = ANGLE2SHORT( bi->viewangles[YAW] ); + ucmd->angles[ROLL] = ANGLE2SHORT( bi->viewangles[ROLL] ); + //subtract the delta angles + for ( j = 0; j < 3; j++ ) { + temp = ucmd->angles[j] - delta_angles[j]; + /*NOTE: disabled because temp should be mod first + if ( j == PITCH ) { + // don't let the player look up or down more than 90 degrees + if ( temp > 16000 ) temp = 16000; + else if ( temp < -16000 ) temp = -16000; + } + */ + ucmd->angles[j] = temp; + } + //NOTE: movement is relative to the REAL view angles + //get the horizontal forward and right vector + //get the pitch in the range [-180, 180] + if ( bi->dir[2] ) { + angles[PITCH] = bi->viewangles[PITCH]; + } else { angles[PITCH] = 0;} + angles[YAW] = bi->viewangles[YAW]; + angles[ROLL] = 0; + AngleVectors( angles, forward, right, NULL ); + //bot input speed is in the range [0, 400] + bi->speed = bi->speed * 127 / 400; + //set the view independent movement + ucmd->forwardmove = DotProduct( forward, bi->dir ) * bi->speed; + ucmd->rightmove = DotProduct( right, bi->dir ) * bi->speed; + ucmd->upmove = abs( forward[2] ) * bi->dir[2] * bi->speed; + //normal keyboard movement + if ( bi->actionflags & ACTION_MOVEFORWARD ) { + ucmd->forwardmove += 127; + } + if ( bi->actionflags & ACTION_MOVEBACK ) { + ucmd->forwardmove -= 127; + } + if ( bi->actionflags & ACTION_MOVELEFT ) { + ucmd->rightmove -= 127; + } + if ( bi->actionflags & ACTION_MOVERIGHT ) { + ucmd->rightmove += 127; + } + //jump/moveup + if ( bi->actionflags & ACTION_JUMP ) { + ucmd->upmove += 127; + } + //crouch/movedown + if ( bi->actionflags & ACTION_CROUCH ) { + ucmd->upmove -= 127; + } + // + //Com_Printf("forward = %d right = %d up = %d\n", ucmd.forwardmove, ucmd.rightmove, ucmd.upmove); + //Com_Printf("ucmd->serverTime = %d\n", ucmd->serverTime); +} + +/* +============== +BotUpdateInput +============== +*/ +void BotUpdateInput( bot_state_t *bs, int time ) { + bot_input_t bi; + int j; + + //add the delta angles to the bot's current view angles + for ( j = 0; j < 3; j++ ) { + bs->viewangles[j] = AngleMod( bs->viewangles[j] + SHORT2ANGLE( bs->cur_ps.delta_angles[j] ) ); + } + // + BotChangeViewAngles( bs, (float) time / 1000 ); + trap_EA_GetInput( bs->client, (float) time / 1000, &bi ); + //respawn hack + if ( bi.actionflags & ACTION_RESPAWN ) { + if ( bs->lastucmd.buttons & BUTTON_ATTACK ) { + bi.actionflags &= ~( ACTION_RESPAWN | ACTION_ATTACK ); + } + } + // + BotInputToUserCommand( &bi, &bs->lastucmd, bs->cur_ps.delta_angles, time ); + bs->lastucmd.serverTime = time; + //subtract the delta angles + for ( j = 0; j < 3; j++ ) { + bs->viewangles[j] = AngleMod( bs->viewangles[j] - SHORT2ANGLE( bs->cur_ps.delta_angles[j] ) ); + } +} + +/* +============== +BotAIRegularUpdate +============== +*/ +void BotAIRegularUpdate( void ) { + if ( regularupdate_time < trap_AAS_Time() ) { + trap_BotUpdateEntityItems(); + regularupdate_time = trap_AAS_Time() + 1; + } +} + +/* +============== +BotAI +============== +*/ +int BotAI( int client, float thinktime ) { + bot_state_t *bs; + char buf[1024], *args; + int j; + + trap_EA_ResetInput( client, NULL ); + // + bs = botstates[client]; + if ( !bs || !bs->inuse ) { + BotAI_Print( PRT_FATAL, "client %d hasn't been setup\n", client ); + return BLERR_AICLIENTNOTSETUP; + } + + //retrieve the current client state + BotAI_GetClientState( client, &bs->cur_ps ); + + //retrieve any waiting console messages + while ( trap_BotGetServerCommand( client, buf, sizeof( buf ) ) ) { + //have buf point to the command and args to the command arguments + args = strchr( buf, ' ' ); + if ( !args ) { + continue; + } + *args++ = '\0'; + + //remove color espace sequences from the arguments + Q_CleanStr( args ); + + //botai_import.Print(PRT_MESSAGE, "ConsoleMessage: \"%s\"\n", buf); + if ( !Q_stricmp( buf, "cp " ) ) { /*CenterPrintf*/ + } else if ( !Q_stricmp( buf, "cs" ) ) { /*ConfigStringModified*/ + } else if ( !Q_stricmp( buf, "print" ) ) { + trap_BotQueueConsoleMessage( bs->cs, CMS_NORMAL, args ); + } else if ( !Q_stricmp( buf, "chat" ) ) { + trap_BotQueueConsoleMessage( bs->cs, CMS_CHAT, args ); + } else if ( !Q_stricmp( buf, "tchat" ) ) { + trap_BotQueueConsoleMessage( bs->cs, CMS_CHAT, args ); + } else if ( !Q_stricmp( buf, "scores" ) ) { /*FIXME: parse scores?*/ + } else if ( !Q_stricmp( buf, "clientLevelShot" ) ) { /*ignore*/ + } + } + //add the delta angles to the bot's current view angles + for ( j = 0; j < 3; j++ ) { + bs->viewangles[j] = AngleMod( bs->viewangles[j] + SHORT2ANGLE( bs->cur_ps.delta_angles[j] ) ); + } + //increase the local time of the bot + bs->ltime += thinktime; + // + bs->thinktime = thinktime; + //origin of the bot + VectorCopy( bs->cur_ps.origin, bs->origin ); + //eye coordinates of the bot + VectorCopy( bs->cur_ps.origin, bs->eye ); + bs->eye[2] += bs->cur_ps.viewheight; + //get the area the bot is in + bs->areanum = BotPointAreaNum( bs->origin ); + //the real AI + BotDeathmatchAI( bs, thinktime ); + //set the weapon selection every AI frame + trap_EA_SelectWeapon( bs->client, bs->weaponnum ); + //subtract the delta angles + for ( j = 0; j < 3; j++ ) { + bs->viewangles[j] = AngleMod( bs->viewangles[j] - SHORT2ANGLE( bs->cur_ps.delta_angles[j] ) ); + } + //everything was ok + return BLERR_NOERROR; +} + +/* +================== +BotScheduleBotThink +================== +*/ +void BotScheduleBotThink( void ) { + int i, botnum; + + botnum = 0; + + for ( i = 0; i < MAX_CLIENTS; i++ ) { + if ( !botstates[i] || !botstates[i]->inuse ) { + continue; + } + //initialize the bot think residual time + botstates[i]->botthink_residual = bot_thinktime.integer * botnum / numbots; + botnum++; + } +} + +/* +============== +BotAISetupClient +============== +*/ +int BotAISetupClient( int client, struct bot_settings_s *settings ) { + char filename[MAX_PATH], name[MAX_PATH], gender[MAX_PATH]; + bot_state_t *bs; + int errnum; + + if ( !botstates[client] ) { + botstates[client] = G_Alloc( sizeof( bot_state_t ) ); + } + bs = botstates[client]; + + if ( bs && bs->inuse ) { + BotAI_Print( PRT_FATAL, "client %d already setup\n", client ); + return qfalse; + } + + if ( !trap_AAS_Initialized() ) { + BotAI_Print( PRT_FATAL, "AAS not initialized\n" ); + return qfalse; + } + + //load the bot character + bs->character = trap_BotLoadCharacter( settings->characterfile, settings->skill ); + if ( !bs->character ) { + BotAI_Print( PRT_FATAL, "couldn't load skill %d from %s\n", settings->skill, settings->characterfile ); + return qfalse; + } + //copy the settings + memcpy( &bs->settings, settings, sizeof( bot_settings_t ) ); + //allocate a goal state + bs->gs = trap_BotAllocGoalState( client ); + //load the item weights + trap_Characteristic_String( bs->character, CHARACTERISTIC_ITEMWEIGHTS, filename, MAX_PATH ); + errnum = trap_BotLoadItemWeights( bs->gs, filename ); + if ( errnum != BLERR_NOERROR ) { + trap_BotFreeGoalState( bs->gs ); + return qfalse; + } + //allocate a weapon state + bs->ws = trap_BotAllocWeaponState(); + //load the weapon weights + trap_Characteristic_String( bs->character, CHARACTERISTIC_WEAPONWEIGHTS, filename, MAX_PATH ); + errnum = trap_BotLoadWeaponWeights( bs->ws, filename ); + if ( errnum != BLERR_NOERROR ) { + trap_BotFreeGoalState( bs->gs ); + trap_BotFreeWeaponState( bs->ws ); + return qfalse; + } + //allocate a chat state + bs->cs = trap_BotAllocChatState(); + //load the chat file + trap_Characteristic_String( bs->character, CHARACTERISTIC_CHAT_FILE, filename, MAX_PATH ); + trap_Characteristic_String( bs->character, CHARACTERISTIC_CHAT_NAME, name, MAX_PATH ); + errnum = trap_BotLoadChatFile( bs->cs, filename, name ); + if ( errnum != BLERR_NOERROR ) { + trap_BotFreeChatState( bs->cs ); + trap_BotFreeGoalState( bs->gs ); + trap_BotFreeWeaponState( bs->ws ); + return qfalse; + } + //get the gender characteristic + trap_Characteristic_String( bs->character, CHARACTERISTIC_GENDER, gender, MAX_PATH ); + //set the chat gender + if ( *gender == 'f' || *gender == 'F' ) { + trap_BotSetChatGender( bs->cs, CHAT_GENDERFEMALE ); + } else if ( *gender == 'm' || *gender == 'M' ) { + trap_BotSetChatGender( bs->cs, CHAT_GENDERMALE ); + } else { trap_BotSetChatGender( bs->cs, CHAT_GENDERLESS );} + + bs->inuse = qtrue; + bs->client = client; + bs->entitynum = client; + bs->setupcount = 4; + bs->entergame_time = trap_AAS_Time(); + bs->ms = trap_BotAllocMoveState(); + bs->walker = trap_Characteristic_BFloat( bs->character, CHARACTERISTIC_WALKER, 0, 1 ); + numbots++; + + if ( trap_Cvar_VariableIntegerValue( "bot_testichat" ) ) { + trap_BotLibVarSet( "bot_testichat", "1" ); + BotChatTest( bs ); + } + //NOTE: reschedule the bot thinking + BotScheduleBotThink(); + // + return qtrue; +} + +/* +============== +BotAIShutdownClient +============== +*/ +int BotAIShutdownClient( int client ) { + bot_state_t *bs; + + // Wolfenstein + if ( g_entities[client].r.svFlags & SVF_CASTAI ) { + AICast_ShutdownClient( client ); + return BLERR_NOERROR; + } + // done. + + bs = botstates[client]; + if ( !bs || !bs->inuse ) { + // BotAI_Print(PRT_ERROR, "client %d already shutdown\n", client); + return BLERR_AICLIENTALREADYSHUTDOWN; + } + + if ( BotChat_ExitGame( bs ) ) { + trap_BotEnterChat( bs->cs, bs->client, CHAT_ALL ); + } + + trap_BotFreeMoveState( bs->ms ); + //free the goal state + trap_BotFreeGoalState( bs->gs ); + //free the chat file + trap_BotFreeChatState( bs->cs ); + //free the weapon weights + trap_BotFreeWeaponState( bs->ws ); + //free the bot character + trap_BotFreeCharacter( bs->character ); + // + BotFreeWaypoints( bs->checkpoints ); + BotFreeWaypoints( bs->patrolpoints ); + //clear the bot state + memset( bs, 0, sizeof( bot_state_t ) ); + //set the inuse flag to qfalse + bs->inuse = qfalse; + //there's one bot less + numbots--; + //everything went ok + return BLERR_NOERROR; +} + +/* +============== +BotResetState + +called when a bot enters the intermission or observer mode and +when the level is changed +============== +*/ +void BotResetState( bot_state_t *bs ) { + int client, entitynum, inuse; + int movestate, goalstate, chatstate, weaponstate; + bot_settings_t settings; + int character; + playerState_t ps; //current player state + float entergame_time; + + //save some things that should not be reset here + memcpy( &settings, &bs->settings, sizeof( bot_settings_t ) ); + memcpy( &ps, &bs->cur_ps, sizeof( playerState_t ) ); + inuse = bs->inuse; + client = bs->client; + entitynum = bs->entitynum; + character = bs->character; + movestate = bs->ms; + goalstate = bs->gs; + chatstate = bs->cs; + weaponstate = bs->ws; + entergame_time = bs->entergame_time; + //free checkpoints and patrol points + BotFreeWaypoints( bs->checkpoints ); + BotFreeWaypoints( bs->patrolpoints ); + //reset the whole state + memset( bs, 0, sizeof( bot_state_t ) ); + //copy back some state stuff that should not be reset + bs->ms = movestate; + bs->gs = goalstate; + bs->cs = chatstate; + bs->ws = weaponstate; + memcpy( &bs->cur_ps, &ps, sizeof( playerState_t ) ); + memcpy( &bs->settings, &settings, sizeof( bot_settings_t ) ); + bs->inuse = inuse; + bs->client = client; + bs->entitynum = entitynum; + bs->character = character; + bs->entergame_time = entergame_time; + //reset several states + if ( bs->ms ) { + trap_BotResetMoveState( bs->ms ); + } + if ( bs->gs ) { + trap_BotResetGoalState( bs->gs ); + } + if ( bs->ws ) { + trap_BotResetWeaponState( bs->ws ); + } + if ( bs->gs ) { + trap_BotResetAvoidGoals( bs->gs ); + } + if ( bs->ms ) { + trap_BotResetAvoidReach( bs->ms ); + } +} + +/* +============== +BotAILoadMap +============== +*/ +int BotAILoadMap( int restart ) { + int i; + vmCvar_t mapname; + + if ( !restart ) { + trap_Cvar_Register( &mapname, "mapname", "", CVAR_SERVERINFO | CVAR_ROM ); + trap_BotLibLoadMap( mapname.string ); + } + + for ( i = 0; i < MAX_CLIENTS; i++ ) { + if ( botstates[i] && botstates[i]->inuse ) { + BotResetState( botstates[i] ); + botstates[i]->setupcount = 4; + } + } + + BotSetupDeathmatchAI(); + + return BLERR_NOERROR; +} + +/* +================== +BotAIStartFrame +================== +*/ +int BotAIStartFrame( int time ) { + int i; + gentity_t *ent; + bot_entitystate_t state; + //entityState_t entitystate; + //vec3_t mins = {-15, -15, -24}, maxs = {15, 15, 32}; + int elapsed_time, thinktime; + static int local_time; + static int botlib_residual; + static int lastbotthink_time; + + if ( g_gametype.integer != GT_SINGLE_PLAYER ) { + G_CheckBotSpawn(); + } + + trap_Cvar_Update( &bot_rocketjump ); + trap_Cvar_Update( &bot_grapple ); + trap_Cvar_Update( &bot_fastchat ); + trap_Cvar_Update( &bot_nochat ); + trap_Cvar_Update( &bot_testrchat ); + trap_Cvar_Update( &bot_thinktime ); + // Ridah, set the default AAS world + trap_AAS_SetCurrentWorld( 0 ); + trap_Cvar_Update( &memorydump ); + + if ( memorydump.integer ) { + trap_BotLibVarSet( "memorydump", "1" ); + trap_Cvar_Set( "memorydump", "0" ); + } + + //if the bot think time changed we should reschedule the bots + if ( bot_thinktime.integer != lastbotthink_time ) { + lastbotthink_time = bot_thinktime.integer; + BotScheduleBotThink(); + } + + elapsed_time = time - local_time; + local_time = time; + + botlib_residual += elapsed_time; + + if ( elapsed_time > bot_thinktime.integer ) { + thinktime = elapsed_time; + } else { thinktime = bot_thinktime.integer;} + + // update the bot library + if ( botlib_residual >= thinktime ) { + botlib_residual -= thinktime; + + trap_BotLibStartFrame( (float) time / 1000 ); + + // Ridah, only check the default world + trap_AAS_SetCurrentWorld( 0 ); + + if ( !trap_AAS_Initialized() ) { + return BLERR_NOERROR; + } + + //update entities in the botlib + for ( i = 0; i < MAX_GENTITIES; i++ ) { + + // Ridah, in single player, we only need client entity information + if ( g_gametype.integer == GT_SINGLE_PLAYER && i > level.maxclients ) { + break; + } + + ent = &g_entities[i]; + if ( !ent->inuse ) { + continue; + } + if ( !ent->r.linked ) { + continue; + } + if ( ent->r.svFlags & SVF_NOCLIENT ) { + continue; + } + if ( ent->aiInactive ) { + continue; + } + // + memset( &state, 0, sizeof( bot_entitystate_t ) ); + // + VectorCopy( ent->r.currentOrigin, state.origin ); + VectorCopy( ent->r.currentAngles, state.angles ); + VectorCopy( ent->s.origin2, state.old_origin ); + VectorCopy( ent->r.mins, state.mins ); + VectorCopy( ent->r.maxs, state.maxs ); + state.type = ent->s.eType; + state.flags = ent->s.eFlags; + if ( ent->r.bmodel ) { + state.solid = SOLID_BSP; + } else { state.solid = SOLID_BBOX;} + state.groundent = ent->s.groundEntityNum; + state.modelindex = ent->s.modelindex; + state.modelindex2 = ent->s.modelindex2; + state.frame = ent->s.frame; + //state.event = ent->s.event; + //state.eventParm = ent->s.eventParm; + state.powerups = ent->s.powerups; + state.legsAnim = ent->s.legsAnim; + state.torsoAnim = ent->s.torsoAnim; +// state.weapAnim = ent->s.weapAnim; //----(SA) +//----(SA) didn't want to comment in as I wasn't sure of any implications of changing the aas_entityinfo_t and bot_entitystate_t structures. + state.weapon = ent->s.weapon; + /* + if (!BotAI_GetEntityState(i, &entitystate)) continue; + // + memset(&state, 0, sizeof(bot_entitystate_t)); + // + VectorCopy(entitystate.pos.trBase, state.origin); + VectorCopy(entitystate.angles, state.angles); + VectorCopy(ent->s.origin2, state.old_origin); + //VectorCopy(ent->r.mins, state.mins); + //VectorCopy(ent->r.maxs, state.maxs); + state.type = entitystate.eType; + state.flags = entitystate.eFlags; + if (ent->r.bmodel) state.solid = SOLID_BSP; + else state.solid = SOLID_BBOX; + state.modelindex = entitystate.modelindex; + state.modelindex2 = entitystate.modelindex2; + state.frame = entitystate.frame; + state.event = entitystate.event; + state.eventParm = entitystate.eventParm; + state.powerups = entitystate.powerups; + state.legsAnim = entitystate.legsAnim; + state.torsoAnim = entitystate.torsoAnim; + state.weapon = entitystate.weapon; + */ + // + trap_BotLibUpdateEntity( i, &state ); + } + + BotAIRegularUpdate(); + + } + + // Ridah, in single player, don't need bot's thinking + if ( g_gametype.integer == GT_SINGLE_PLAYER ) { + return BLERR_NOERROR; + } + + // execute scheduled bot AI + for ( i = 0; i < MAX_CLIENTS; i++ ) { + if ( !botstates[i] || !botstates[i]->inuse ) { + continue; + } + // Ridah + if ( g_entities[i].r.svFlags & SVF_CASTAI ) { + continue; + } + // done. + // + botstates[i]->botthink_residual += elapsed_time; + // + if ( botstates[i]->botthink_residual >= thinktime ) { + botstates[i]->botthink_residual -= thinktime; + + if ( !trap_AAS_Initialized() ) { + return BLERR_NOERROR; + } + + if ( g_entities[i].client->pers.connected == CON_CONNECTED ) { + BotAI( i, (float) thinktime / 1000 ); + } + } + } + + + // execute bot user commands every frame + for ( i = 0; i < MAX_CLIENTS; i++ ) { + if ( !botstates[i] || !botstates[i]->inuse ) { + continue; + } + // Ridah + if ( g_entities[i].r.svFlags & SVF_CASTAI ) { + continue; + } + // done. + if ( g_entities[i].client->pers.connected != CON_CONNECTED ) { + continue; + } + + BotUpdateInput( botstates[i], time ); + trap_BotUserCommand( botstates[i]->client, &botstates[i]->lastucmd ); + } + + return BLERR_NOERROR; +} + +/* +============== +BotInitLibrary +============== +*/ +int BotInitLibrary( void ) { + char buf[144]; + + //set the maxclients and maxentities library variables before calling BotSetupLibrary + trap_Cvar_VariableStringBuffer( "sv_maxclients", buf, sizeof( buf ) ); + if ( !strlen( buf ) ) { + strcpy( buf, "8" ); + } + trap_BotLibVarSet( "maxclients", buf ); + Com_sprintf( buf, sizeof( buf ), "%d", MAX_GENTITIES ); + trap_BotLibVarSet( "maxentities", buf ); + //bsp checksum + trap_Cvar_VariableStringBuffer( "sv_mapChecksum", buf, sizeof( buf ) ); + if ( strlen( buf ) ) { + trap_BotLibVarSet( "sv_mapChecksum", buf ); + } + //maximum number of aas links + trap_Cvar_VariableStringBuffer( "max_aaslinks", buf, sizeof( buf ) ); + if ( strlen( buf ) ) { + trap_BotLibVarSet( "max_aaslinks", buf ); + } + //maximum number of items in a level + trap_Cvar_VariableStringBuffer( "max_levelitems", buf, sizeof( buf ) ); + if ( strlen( buf ) ) { + trap_BotLibVarSet( "max_levelitems", buf ); + } + //automatically launch WinBSPC if AAS file not available + trap_Cvar_VariableStringBuffer( "autolaunchbspc", buf, sizeof( buf ) ); + if ( strlen( buf ) ) { + trap_BotLibVarSet( "autolaunchbspc", "1" ); + } + // + trap_Cvar_VariableStringBuffer( "g_gametype", buf, sizeof( buf ) ); + if ( !strlen( buf ) ) { + strcpy( buf, "0" ); + } + trap_BotLibVarSet( "g_gametype", buf ); + // + // Rafael gameskill + trap_Cvar_VariableStringBuffer( "g_gameskill", buf, sizeof( buf ) ); + if ( !strlen( buf ) ) { + strcpy( buf, "0" ); + } + trap_BotLibVarSet( "g_gamekill", buf ); + // done + // + trap_Cvar_VariableStringBuffer( "bot_developer", buf, sizeof( buf ) ); + if ( !strlen( buf ) ) { + strcpy( buf, "0" ); + } + trap_BotLibVarSet( "bot_developer", buf ); + //log file + trap_Cvar_VariableStringBuffer( "bot_developer", buf, sizeof( buf ) ); + if ( !strlen( buf ) ) { + strcpy( buf, "0" ); + } + trap_BotLibVarSet( "log", buf ); + //no chatting + trap_Cvar_VariableStringBuffer( "bot_nochat", buf, sizeof( buf ) ); + if ( strlen( buf ) ) { + trap_BotLibVarSet( "nochat", "0" ); + } + //forced clustering calculations + trap_Cvar_VariableStringBuffer( "forceclustering", buf, sizeof( buf ) ); + if ( strlen( buf ) ) { + trap_BotLibVarSet( "forceclustering", buf ); + } + //forced reachability calculations + trap_Cvar_VariableStringBuffer( "forcereachability", buf, sizeof( buf ) ); + if ( strlen( buf ) ) { + trap_BotLibVarSet( "forcereachability", buf ); + } + //force writing of AAS to file + trap_Cvar_VariableStringBuffer( "forcewrite", buf, sizeof( buf ) ); + if ( strlen( buf ) ) { + trap_BotLibVarSet( "forcewrite", buf ); + } + //no AAS optimization + trap_Cvar_VariableStringBuffer( "nooptimize", buf, sizeof( buf ) ); + if ( strlen( buf ) ) { + trap_BotLibVarSet( "nooptimize", buf ); + } + //number of reachabilities to calculate each frame + trap_Cvar_VariableStringBuffer( "framereachability", buf, sizeof( buf ) ); + if ( !strlen( buf ) ) { + strcpy( buf, "20" ); + } + trap_BotLibVarSet( "framereachability", buf ); + // + trap_Cvar_VariableStringBuffer( "bot_reloadcharacters", buf, sizeof( buf ) ); + if ( !strlen( buf ) ) { + strcpy( buf, "0" ); + } + trap_BotLibVarSet( "bot_reloadcharacters", buf ); + //base directory + trap_Cvar_VariableStringBuffer( "fs_basepath", buf, sizeof( buf ) ); + if ( strlen( buf ) ) { + trap_BotLibVarSet( "basedir", buf ); + } + //game directory + trap_Cvar_VariableStringBuffer( "fs_game", buf, sizeof( buf ) ); + if ( strlen( buf ) ) { + trap_BotLibVarSet( "gamedir", buf ); + } + //cd directory + trap_Cvar_VariableStringBuffer( "fs_cdpath", buf, sizeof( buf ) ); + if ( strlen( buf ) ) { + trap_BotLibVarSet( "cddir", buf ); + } + //setup the bot library + return trap_BotLibSetup(); +} + +/* +============== +BotAISetup +============== +*/ +int BotAISetup( int restart ) { + int errnum; + +#ifdef RANDOMIZE + srand( (unsigned)time( NULL ) ); +#endif //RANDOMIZE + + trap_Cvar_Register( &bot_thinktime, "bot_thinktime", "100", 0 ); + trap_Cvar_Register( &memorydump, "memorydump", "0", 0 ); + + //if the game is restarted for a tournament + if ( restart ) { + return BLERR_NOERROR; + } + + //initialize the bot states + memset( botstates, 0, sizeof( botstates ) ); + + trap_Cvar_Register( &bot_thinktime, "bot_thinktime", "100", 0 ); + + errnum = BotInitLibrary(); + if ( errnum != BLERR_NOERROR ) { + return qfalse; + } + return BLERR_NOERROR; +} + +/* +============== +BotAIShutdown +============== +*/ +int BotAIShutdown( int restart ) { + + int i; + + //if the game is restarted for a tournament + if ( restart ) { + //shutdown all the bots in the botlib + for ( i = 0; i < MAX_CLIENTS; i++ ) { + if ( botstates[i] && botstates[i]->inuse ) { + BotAIShutdownClient( botstates[i]->client ); + } + } + //don't shutdown the bot library + } else { + trap_BotLibShutdown(); + } + return qtrue; +} + diff --git a/src/botai/ai_main.h b/src/botai/ai_main.h new file mode 100644 index 0000000..4ba5177 --- /dev/null +++ b/src/botai/ai_main.h @@ -0,0 +1,242 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: ai_main.h + * + * desc: Quake3 bot AI + * + * + *****************************************************************************/ + +//#define DEBUG +#define CTF + +#define MAX_ITEMS 256 +//bot flags +#define BFL_STRAFERIGHT 1 //strafe to the right +#define BFL_ATTACKED 2 //bot has attacked last ai frame +#define BFL_ATTACKJUMPED 4 //bot jumped during attack last frame +#define BFL_AIMATENEMY 8 //bot aimed at the enemy this frame +#define BFL_AVOIDRIGHT 16 //avoid obstacles by going to the right +#define BFL_IDEALVIEWSET 32 //bot has ideal view angles set +//long term goal types +#define LTG_TEAMHELP 1 //help a team mate +#define LTG_TEAMACCOMPANY 2 //accompany a team mate +#define LTG_DEFENDKEYAREA 3 //defend a key area +#define LTG_GETFLAG 4 //get the enemy flag +#define LTG_RUSHBASE 5 //rush to the base +#define LTG_RETURNFLAG 6 //return the flag +#define LTG_CAMP 7 //camp somewhere +#define LTG_CAMPORDER 8 //ordered to camp somewhere +#define LTG_PATROL 9 //patrol +#define LTG_GETITEM 10 //get an item +#define LTG_KILL 11 //kill someone +//some goal dedication times +#define TEAM_HELP_TIME 60 //1 minute teamplay help time +#define TEAM_ACCOMPANY_TIME 600 //10 minutes teamplay accompany time +#define TEAM_DEFENDKEYAREA_TIME 240 //4 minutes ctf defend base time +#define TEAM_CAMP_TIME 600 //10 minutes camping time +#define TEAM_PATROL_TIME 600 //10 minutes patrolling time +#define TEAM_LEAD_TIME 600 //10 minutes taking the lead +#define TEAM_GETITEM_TIME 60 //1 minute +#define TEAM_KILL_SOMEONE 180 //3 minute to kill someone +#define CTF_GETFLAG_TIME 240 //4 minutes ctf get flag time +#define CTF_RUSHBASE_TIME 120 //2 minutes ctf rush base time +#define CTF_RETURNFLAG_TIME 180 //3 minutes to return the flag +#define CTF_ROAM_TIME 60 //1 minute ctf roam time +//patrol flags +#define PATROL_LOOP 1 +#define PATROL_REVERSE 2 +#define PATROL_BACK 4 +//copied from the aas file header +#define PRESENCE_NONE 1 +#define PRESENCE_NORMAL 2 +#define PRESENCE_CROUCH 4 + +//check points +typedef struct bot_waypoint_s +{ + int inuse; + char name[32]; + bot_goal_t goal; + struct bot_waypoint_s *next, *prev; +} bot_waypoint_t; + +//bot state +typedef struct bot_state_s +{ + int inuse; //true if this state is used by a bot client + int botthink_residual; //residual for the bot thinks + int client; //client number of the bot + int entitynum; //entity number of the bot + playerState_t cur_ps; //current player state + int last_eFlags; //last ps flags + usercmd_t lastucmd; //usercmd from last frame + int entityeventTime[1024]; //last entity event time + // + bot_settings_t settings; //several bot settings + int ( *ainode )( struct bot_state_s *bs ); //current AI node + float thinktime; //time the bot thinks this frame + vec3_t origin; //origin of the bot + vec3_t velocity; //velocity of the bot + int presencetype; //presence type of the bot + vec3_t eye; //eye coordinates of the bot + int areanum; //the number of the area the bot is in + int inventory[MAX_ITEMS]; //string with items amounts the bot has + int tfl; //the travel flags the bot uses + int flags; //several flags + int respawn_wait; //wait until respawned + int lasthealth; //health value previous frame + int lastkilledplayer; //last killed player + int lastkilledby; //player that last killed this bot + int botdeathtype; //the death type of the bot + int enemydeathtype; //the death type of the enemy + int botsuicide; //true when the bot suicides + int enemysuicide; //true when the enemy of the bot suicides + int setupcount; //true when the bot has just been setup + int entergamechat; //true when the bot used an enter game chat + int num_deaths; //number of time this bot died + int num_kills; //number of kills of this bot + int revenge_enemy; //the revenge enemy + int revenge_kills; //number of kills the enemy made + int lastframe_health; //health value the last frame + int lasthitcount; //number of hits last frame + int chatto; //chat to all or team + float walker; //walker charactertic + float ltime; //local bot time + float entergame_time; //time the bot entered the game + float ltg_time; //long term goal time + float nbg_time; //nearby goal time + float respawn_time; //time the bot takes to respawn + float respawnchat_time; //time the bot started a chat during respawn + float chase_time; //time the bot will chase the enemy + float enemyvisible_time; //time the enemy was last visible + float check_time; //time to check for nearby items + float stand_time; //time the bot is standing still + float lastchat_time; //time the bot last selected a chat + float standfindenemy_time; //time to find enemy while standing + float attackstrafe_time; //time the bot is strafing in one dir + float attackcrouch_time; //time the bot will stop crouching + float attackchase_time; //time the bot chases during actual attack + float attackjump_time; //time the bot jumped during attack + float enemysight_time; //time before reacting to enemy + float enemydeath_time; //time the enemy died + float enemyposition_time; //time the position and velocity of the enemy were stored + float activate_time; //time to activate something + float activatemessage_time; //time to show activate message + float defendaway_time; //time away while defending + float defendaway_range; //max travel time away from defend area + float rushbaseaway_time; //time away from rushing to the base + float ctfroam_time; //time the bot is roaming in ctf + float killedenemy_time; //time the bot killed the enemy + float arrive_time; //time arrived (at companion) + float lastair_time; //last time the bot had air + float teleport_time; //last time the bot teleported + float camp_time; //last time camped + float camp_range; //camp range + float weaponchange_time; //time the bot started changing weapons + float firethrottlewait_time; //amount of time to wait + float firethrottleshoot_time; //amount of time to shoot + vec3_t aimtarget; + vec3_t enemyvelocity; //enemy velocity 0.5 secs ago during battle + vec3_t enemyorigin; //enemy origin 0.5 secs ago during battle + // + int character; //the bot character + int ms; //move state of the bot + int gs; //goal state of the bot + int cs; //chat state of the bot + int ws; //weapon state of the bot + // + int enemy; //enemy entity number + int lastenemyareanum; //last reachability area the enemy was in + vec3_t lastenemyorigin; //last origin of the enemy in the reachability area + int weaponnum; //current weapon number + vec3_t viewangles; //current view angles + vec3_t ideal_viewangles; //ideal view angles + // + int ltgtype; //long term goal type + // + int teammate; //team mate + bot_goal_t teamgoal; //the team goal + float teammessage_time; //time to message team mates what the bot is doing + float teamgoal_time; //time to stop helping team mate + float teammatevisible_time; //last time the team mate was NOT visible + // + int lead_teammate; //team mate the bot is leading + bot_goal_t lead_teamgoal; //team goal while leading + float lead_time; //time leading someone + float leadvisible_time; //last time the team mate was visible + float leadmessage_time; //last time a messaged was sent to the team mate + float leadbackup_time; //time backing up towards team mate + // + char teamleader[32]; //netname of the team leader + float askteamleader_time; //time asked for team leader + float becometeamleader_time; //time the bot will become the team leader + float teamgiveorders_time; //time to give team orders + int numteammates; //number of team mates + int redflagstatus; //0 = at base, 1 = not at base + int blueflagstatus; //0 = at base, 1 = not at base + int flagstatuschanged; //flag status changed + int forceorders; //true if forced to give orders + int flagcarrier; //team mate carrying the enemy flag + char subteam[32]; //sub team name + float formation_dist; //formation team mate intervening space + char formation_teammate[16]; //netname of the team mate the bot uses for relative positioning + float formation_angle; //angle relative to the formation team mate + vec3_t formation_dir; //the direction the formation is moving in + vec3_t formation_origin; //origin the bot uses for relative positioning + bot_goal_t formation_goal; //formation goal + bot_goal_t activategoal; //goal to activate (buttons etc.) + bot_waypoint_t *checkpoints; //check points + bot_waypoint_t *patrolpoints; //patrol points + bot_waypoint_t *curpatrolpoint; //current patrol point the bot is going for + int patrolflags; //patrol flags +} bot_state_t; + +//resets the whole bot state +void BotResetState( bot_state_t *bs ); +//returns the number of bots in the game +int NumBots( void ); +//returns info about the entity +void BotEntityInfo( int entnum, aas_entityinfo_t *info ); + +// Ridah, defines for AI Cast system +int AICast_ShutdownClient( int client ); +void AICast_Init( void ); +void AICast_StartFrame( int time ); +// done. + +// from the game source +void QDECL BotAI_Print( int type, char *fmt, ... ); +void QDECL QDECL BotAI_BotInitialChat( bot_state_t *bs, char *type, ... ); +void BotAI_Trace( bsp_trace_t *bsptrace, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int passent, int contentmask ); +int BotAI_GetClientState( int clientNum, playerState_t *state ); +int BotAI_GetEntityState( int entityNum, entityState_t *state ); +int BotAI_GetSnapshotEntity( int clientNum, int sequence, entityState_t *state ); diff --git a/src/botai/ai_team.c b/src/botai/ai_team.c new file mode 100644 index 0000000..80f9993 --- /dev/null +++ b/src/botai/ai_team.c @@ -0,0 +1,612 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: ai_team.c + * + * desc: Quake3 bot AI + * + * + *****************************************************************************/ + +#include "../game/g_local.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "../game/be_ea.h" +#include "../game/be_ai_char.h" +#include "../game/be_ai_chat.h" +#include "../game/be_ai_gen.h" +#include "../game/be_ai_goal.h" +#include "../game/be_ai_move.h" +#include "../game/be_ai_weap.h" +#include "../botai/botai.h" +// +#include "ai_main.h" +#include "ai_dmq3.h" +#include "ai_chat.h" +#include "ai_cmd.h" +#include "ai_dmnet.h" + + +/* +================== +BotValidTeamLeader +================== +*/ +int BotValidTeamLeader( bot_state_t *bs ) { + if ( !strlen( bs->teamleader ) ) { + return qfalse; + } + if ( ClientFromName( bs->teamleader ) == -1 ) { + return qfalse; + } + return qtrue; +} + +/* +================== +BotNumTeamMates +================== +*/ +int BotNumTeamMates( bot_state_t *bs ) { + int i, numplayers; + char buf[MAX_INFO_STRING]; + static int maxclients; + + if ( !maxclients ) { + maxclients = trap_Cvar_VariableIntegerValue( "sv_maxclients" ); + } + + numplayers = 0; + for ( i = 0; i < maxclients && i < MAX_CLIENTS; i++ ) { + trap_GetConfigstring( CS_PLAYERS + i, buf, sizeof( buf ) ); + //if no config string or no name + if ( !strlen( buf ) || !strlen( Info_ValueForKey( buf, "n" ) ) ) { + continue; + } + //skip spectators + if ( atoi( Info_ValueForKey( buf, "t" ) ) == TEAM_SPECTATOR ) { + continue; + } + // + if ( BotSameTeam( bs, i ) ) { + numplayers++; + } + } + return numplayers; +} + +/* +================== +BotClientTravelTimeToGoal +================== +*/ +int BotClientTravelTimeToGoal( int client, bot_goal_t *goal ) { + playerState_t ps; + int areanum; + + BotAI_GetClientState( client, &ps ); + areanum = BotPointAreaNum( ps.origin ); + if ( !areanum ) { + return 1; + } + return trap_AAS_AreaTravelTimeToGoalArea( areanum, ps.origin, goal->areanum, TFL_DEFAULT ); +} + +/* +================== +BotSortTeamMatesByBaseTravelTime +================== +*/ +int BotSortTeamMatesByBaseTravelTime( bot_state_t *bs, int *teammates, int maxteammates ) { + + int i, j, k, numteammates, traveltime; + char buf[MAX_INFO_STRING]; + static int maxclients; + int traveltimes[MAX_CLIENTS]; + bot_goal_t *goal; + + if ( BotCTFTeam( bs ) == CTF_TEAM_RED ) { + goal = &ctf_redflag; + } else { goal = &ctf_blueflag;} + + if ( !maxclients ) { + maxclients = trap_Cvar_VariableIntegerValue( "sv_maxclients" ); + } + + numteammates = 0; + for ( i = 0; i < maxclients && i < MAX_CLIENTS; i++ ) { + trap_GetConfigstring( CS_PLAYERS + i, buf, sizeof( buf ) ); + //if no config string or no name + if ( !strlen( buf ) || !strlen( Info_ValueForKey( buf, "n" ) ) ) { + continue; + } + //skip spectators + if ( atoi( Info_ValueForKey( buf, "t" ) ) == TEAM_SPECTATOR ) { + continue; + } + // + if ( BotSameTeam( bs, i ) ) { + // + traveltime = BotClientTravelTimeToGoal( i, goal ); + // + for ( j = 0; j < numteammates; j++ ) { + if ( traveltime < traveltimes[j] ) { + for ( k = numteammates; k > j; k-- ) { + traveltimes[k] = traveltimes[k - 1]; + teammates[k] = teammates[k - 1]; + } + traveltimes[j] = traveltime; + teammates[j] = i; + break; + } + } + if ( j >= numteammates ) { + traveltimes[j] = traveltime; + teammates[j] = i; + } + numteammates++; + if ( numteammates >= maxteammates ) { + break; + } + } + } + return numteammates; +} + +/* +================== +BotSayTeamOrders +================== +*/ +void BotSayTeamOrder( bot_state_t *bs, int toclient ) { + char teamchat[MAX_MESSAGE_SIZE]; + char buf[MAX_MESSAGE_SIZE]; + char name[MAX_NETNAME]; + + //if the bot is talking to itself + if ( bs->client == toclient ) { + //don't show the message just put it in the console message queue + trap_BotGetChatMessage( bs->cs, buf, sizeof( buf ) ); + ClientName( bs->client, name, sizeof( name ) ); + Com_sprintf( teamchat, sizeof( teamchat ), "(%s): %s", name, buf ); + trap_BotQueueConsoleMessage( bs->cs, CMS_CHAT, teamchat ); + } else { + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + } +} + +/* +================== +BotCTFOrders +================== +*/ +void BotCTFOrders_BothFlagsNotAtBase( bot_state_t *bs ) { + int numteammates, defenders, attackers, i, other; + int teammates[MAX_CLIENTS]; + char name[MAX_NETNAME], carriername[MAX_NETNAME]; + + numteammates = BotSortTeamMatesByBaseTravelTime( bs, teammates, sizeof( teammates ) ); + //different orders based on the number of team mates + switch ( bs->numteammates ) { + case 1: break; + case 2: + { + //tell the one not carrying the flag to attack the enemy base + if ( teammates[0] != bs->flagcarrier ) { + other = teammates[0]; + } else { other = teammates[1];} + ClientName( other, name, sizeof( name ) ); + BotAI_BotInitialChat( bs, "cmd_getflag", name, NULL ); + BotSayTeamOrder( bs, other ); + break; + } + case 3: + { + //tell the one closest to the base not carrying the flag to accompany the flag carrier + if ( teammates[0] != bs->flagcarrier ) { + other = teammates[0]; + } else { other = teammates[1];} + ClientName( other, name, sizeof( name ) ); + ClientName( bs->flagcarrier, carriername, sizeof( carriername ) ); + if ( bs->flagcarrier == bs->client ) { + BotAI_BotInitialChat( bs, "cmd_accompanyme", name, NULL ); + } else { + BotAI_BotInitialChat( bs, "cmd_accompany", name, carriername, NULL ); + } + BotSayTeamOrder( bs, other ); + //tell the one furthest from the the base not carrying the flag to get the enemy flag + if ( teammates[2] != bs->flagcarrier ) { + other = teammates[2]; + } else { other = teammates[1];} + ClientName( other, name, sizeof( name ) ); + BotAI_BotInitialChat( bs, "cmd_getflag", name, NULL ); + BotSayTeamOrder( bs, other ); + break; + } + default: + { + defenders = (int) ( float ) numteammates * 0.4 + 0.5; + attackers = (int) ( float ) numteammates * 0.5 + 0.5; + ClientName( bs->flagcarrier, carriername, sizeof( carriername ) ); + for ( i = 0; i < defenders; i++ ) { + // + if ( teammates[i] == bs->flagcarrier ) { + continue; + } + // + ClientName( teammates[i], name, sizeof( name ) ); + if ( bs->flagcarrier == bs->client ) { + BotAI_BotInitialChat( bs, "cmd_accompanyme", name, NULL ); + } else { + BotAI_BotInitialChat( bs, "cmd_accompany", name, carriername, NULL ); + } + BotSayTeamOrder( bs, teammates[i] ); + } + for ( i = 0; i < attackers; i++ ) { + // + if ( teammates[numteammates - i - 1] == bs->flagcarrier ) { + continue; + } + // + ClientName( teammates[numteammates - i - 1], name, sizeof( name ) ); + BotAI_BotInitialChat( bs, "cmd_getflag", name, NULL ); + BotSayTeamOrder( bs, teammates[numteammates - i - 1] ); + } + // + break; + } + } +} + +/* +================== +BotCTFOrders +================== +*/ +void BotCTFOrders_FlagNotAtBase( bot_state_t *bs ) { + int numteammates, defenders, attackers, i; + int teammates[MAX_CLIENTS]; + char name[MAX_NETNAME]; + + numteammates = BotSortTeamMatesByBaseTravelTime( bs, teammates, sizeof( teammates ) ); + //different orders based on the number of team mates + switch ( bs->numteammates ) { + case 1: break; + case 2: + { + //the one closest to the base will defend the base + ClientName( teammates[0], name, sizeof( name ) ); + BotAI_BotInitialChat( bs, "cmd_defendbase", name, NULL ); + BotSayTeamOrder( bs, teammates[0] ); + //the other will get the flag + ClientName( teammates[1], name, sizeof( name ) ); + BotAI_BotInitialChat( bs, "cmd_getflag", name, NULL ); + BotSayTeamOrder( bs, teammates[1] ); + break; + } + case 3: + { + //the one closest to the base will defend the base + ClientName( teammates[0], name, sizeof( name ) ); + BotAI_BotInitialChat( bs, "cmd_defendbase", name, NULL ); + BotSayTeamOrder( bs, teammates[0] ); + //the other two get the flag + ClientName( teammates[1], name, sizeof( name ) ); + BotAI_BotInitialChat( bs, "cmd_getflag", name, NULL ); + BotSayTeamOrder( bs, teammates[1] ); + // + ClientName( teammates[2], name, sizeof( name ) ); + BotAI_BotInitialChat( bs, "cmd_getflag", name, NULL ); + BotSayTeamOrder( bs, teammates[2] ); + break; + } + default: + { + defenders = (int) ( float ) numteammates * 0.3 + 0.5; + attackers = (int) ( float ) numteammates * 0.5 + 0.5; + for ( i = 0; i < defenders; i++ ) { + // + ClientName( teammates[i], name, sizeof( name ) ); + BotAI_BotInitialChat( bs, "cmd_defendbase", name, NULL ); + BotSayTeamOrder( bs, teammates[i] ); + } + for ( i = 0; i < attackers; i++ ) { + // + ClientName( teammates[numteammates - i - 1], name, sizeof( name ) ); + BotAI_BotInitialChat( bs, "cmd_getflag", name, NULL ); + BotSayTeamOrder( bs, teammates[numteammates - i - 1] ); + } + // + break; + } + } +} + +/* +================== +BotCTFOrders +================== +*/ +void BotCTFOrders_EnemyFlagNotAtBase( bot_state_t *bs ) { + int numteammates, defenders, attackers, i, other; + int teammates[MAX_CLIENTS]; + char name[MAX_NETNAME], carriername[MAX_NETNAME]; + + numteammates = BotSortTeamMatesByBaseTravelTime( bs, teammates, sizeof( teammates ) ); + //different orders based on the number of team mates + switch ( numteammates ) { + case 1: break; + case 2: + { + //tell the one not carrying the flag to defend the base + if ( teammates[0] == bs->flagcarrier ) { + other = teammates[1]; + } else { other = teammates[0];} + ClientName( other, name, sizeof( name ) ); + BotAI_BotInitialChat( bs, "cmd_defendbase", name, NULL ); + BotSayTeamOrder( bs, other ); + break; + } + case 3: + { + //tell the one closest to the base not carrying the flag to defend the base + if ( teammates[0] != bs->flagcarrier ) { + other = teammates[0]; + } else { other = teammates[1];} + ClientName( other, name, sizeof( name ) ); + BotAI_BotInitialChat( bs, "cmd_defendbase", name, NULL ); + BotSayTeamOrder( bs, other ); + //tell the one furthest from the base not carrying the flag to accompany the flag carrier + if ( teammates[2] != bs->flagcarrier ) { + other = teammates[2]; + } else { other = teammates[1];} + ClientName( other, name, sizeof( name ) ); + ClientName( bs->flagcarrier, carriername, sizeof( carriername ) ); + if ( bs->flagcarrier == bs->client ) { + BotAI_BotInitialChat( bs, "cmd_accompanyme", name, NULL ); + } else { + BotAI_BotInitialChat( bs, "cmd_accompany", name, carriername, NULL ); + } + BotSayTeamOrder( bs, other ); + break; + } + default: + { + //40% will defend the base + defenders = (int) ( float ) numteammates * 0.4 + 0.5; + //50% accompanies the flag carrier + attackers = (int) ( float ) numteammates * 0.5 + 0.5; + for ( i = 0; i < defenders; i++ ) { + // + if ( teammates[i] == bs->flagcarrier ) { + continue; + } + ClientName( teammates[i], name, sizeof( name ) ); + BotAI_BotInitialChat( bs, "cmd_defendbase", name, NULL ); + BotSayTeamOrder( bs, teammates[i] ); + } + ClientName( bs->flagcarrier, carriername, sizeof( carriername ) ); + for ( i = 0; i < attackers; i++ ) { + // + if ( teammates[numteammates - i - 1] == bs->flagcarrier ) { + continue; + } + // + ClientName( teammates[numteammates - i - 1], name, sizeof( name ) ); + if ( bs->flagcarrier == bs->client ) { + BotAI_BotInitialChat( bs, "cmd_accompanyme", name, NULL ); + } else { + BotAI_BotInitialChat( bs, "cmd_accompany", name, carriername, NULL ); + } + BotSayTeamOrder( bs, teammates[numteammates - i - 1] ); + } + // + break; + } + } +} + + +/* +================== +BotCTFOrders +================== +*/ +void BotCTFOrders_BothFlagsAtBase( bot_state_t *bs ) { + int numteammates, defenders, attackers, i; + int teammates[MAX_CLIENTS]; + char name[MAX_NETNAME]; +// char buf[MAX_MESSAGE_SIZE]; + + numteammates = BotSortTeamMatesByBaseTravelTime( bs, teammates, sizeof( teammates ) ); + //different orders based on the number of team mates + switch ( numteammates ) { + case 1: break; + case 2: + { + //the one closest to the base will defend the base + ClientName( teammates[0], name, sizeof( name ) ); + BotAI_BotInitialChat( bs, "cmd_defendbase", name, NULL ); + BotSayTeamOrder( bs, teammates[0] ); + //the other will get the flag + ClientName( teammates[1], name, sizeof( name ) ); + BotAI_BotInitialChat( bs, "cmd_getflag", name, NULL ); + BotSayTeamOrder( bs, teammates[1] ); + break; + } + case 3: + { + //the one closest to the base will defend the base + ClientName( teammates[0], name, sizeof( name ) ); + BotAI_BotInitialChat( bs, "cmd_defendbase", name, NULL ); + BotSayTeamOrder( bs, teammates[0] ); + //the second one closest to the base will defend the base + ClientName( teammates[1], name, sizeof( name ) ); + BotAI_BotInitialChat( bs, "cmd_defendbase", name, NULL ); + BotSayTeamOrder( bs, teammates[1] ); + //the other will get the flag + ClientName( teammates[2], name, sizeof( name ) ); + BotAI_BotInitialChat( bs, "cmd_getflag", name, NULL ); + BotSayTeamOrder( bs, teammates[2] ); + break; + } + default: + { + defenders = (int) ( float ) numteammates * 0.5 + 0.5; + attackers = (int) ( float ) numteammates * 0.3 + 0.5; + for ( i = 0; i < defenders; i++ ) { + // + ClientName( teammates[i], name, sizeof( name ) ); + BotAI_BotInitialChat( bs, "cmd_defendbase", name, NULL ); + BotSayTeamOrder( bs, teammates[i] ); + } + for ( i = 0; i < attackers; i++ ) { + // + ClientName( teammates[numteammates - i - 1], name, sizeof( name ) ); + BotAI_BotInitialChat( bs, "cmd_getflag", name, NULL ); + BotSayTeamOrder( bs, teammates[numteammates - i - 1] ); + } + // + break; + } + } +} + + +/* +================== +BotTeamOrders +================== +*/ +void BotTeamOrders( bot_state_t *bs ) { + //no teamplay orders at this time +} + + +/* +================== +BotTeamAI +================== +*/ +void BotTeamAI( bot_state_t *bs ) { + int numteammates, flagstatus; + char netname[MAX_NETNAME]; + + // + if ( gametype != GT_TEAM && gametype != GT_CTF ) { + return; + } + //make sure we've got a valid team leader + if ( !BotValidTeamLeader( bs ) ) { + // + if ( !bs->askteamleader_time && !bs->becometeamleader_time ) { + if ( bs->entergame_time + 10 > trap_AAS_Time() ) { + bs->askteamleader_time = trap_AAS_Time() + 5 + random() * 10; + } else { + bs->becometeamleader_time = trap_AAS_Time() + 5 + random() * 10; + } + } + if ( bs->askteamleader_time && bs->askteamleader_time < trap_AAS_Time() ) { + //if asked for a team leader and no repsonse + BotAI_BotInitialChat( bs, "whoisteamleader", NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + bs->askteamleader_time = 0; + bs->becometeamleader_time = trap_AAS_Time() + 15 + random() * 10; + } + if ( bs->becometeamleader_time && bs->becometeamleader_time < trap_AAS_Time() ) { + BotAI_BotInitialChat( bs, "iamteamleader", NULL ); + trap_BotEnterChat( bs->cs, bs->client, CHAT_TEAM ); + ClientName( bs->client, netname, sizeof( netname ) ); + strncpy( bs->teamleader, netname, sizeof( bs->teamleader ) ); + bs->teamleader[sizeof( bs->teamleader )] = '\0'; + bs->becometeamleader_time = 0; + } + return; + } + bs->askteamleader_time = 0; + bs->becometeamleader_time = 0; + + //return if this bot is NOT the team leader + ClientName( bs->client, netname, sizeof( netname ) ); + if ( Q_stricmp( netname, bs->teamleader ) != 0 ) { + return; + } + // + //if the game starts OR a new player comes onto the team OR a player leaves the team + // + numteammates = BotNumTeamMates( bs ); + //give orders + switch ( gametype ) { + case GT_TEAM: + { + if ( bs->numteammates != numteammates || bs->forceorders ) { + bs->teamgiveorders_time = trap_AAS_Time(); + bs->numteammates = numteammates; + bs->forceorders = qfalse; + } + //if it's time to give orders + if ( bs->teamgiveorders_time < trap_AAS_Time() - 5 ) { + BotTeamOrders( bs ); + // + bs->teamgiveorders_time = 0; + } + break; + } + case GT_CTF: + { + // + if ( bs->numteammates != numteammates || bs->flagstatuschanged || bs->forceorders ) { + bs->teamgiveorders_time = trap_AAS_Time(); + bs->numteammates = numteammates; + bs->flagstatuschanged = qfalse; + bs->forceorders = qfalse; + } + //if it's time to give orders + if ( bs->teamgiveorders_time && bs->teamgiveorders_time < trap_AAS_Time() - 3 ) { + // + if ( BotCTFTeam( bs ) == CTF_TEAM_RED ) { + flagstatus = bs->redflagstatus * 2 + bs->blueflagstatus; + } else { flagstatus = bs->blueflagstatus * 2 + bs->redflagstatus;} + // + switch ( flagstatus ) { + case 0: BotCTFOrders_BothFlagsAtBase( bs ); break; + case 1: BotCTFOrders_EnemyFlagNotAtBase( bs ); break; + case 2: BotCTFOrders_FlagNotAtBase( bs ); break; + case 3: BotCTFOrders_BothFlagsNotAtBase( bs ); break; + } + // + bs->teamgiveorders_time = 0; + } + break; + } + } +} + + diff --git a/src/botai/ai_team.h b/src/botai/ai_team.h new file mode 100644 index 0000000..9c9047f --- /dev/null +++ b/src/botai/ai_team.h @@ -0,0 +1,40 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: ai_team.h + * + * desc: Quake3 bot AI + * + * + *****************************************************************************/ + +void BotTeamAI( bot_state_t *bs ); + + diff --git a/src/botai/botai.h b/src/botai/botai.h new file mode 100644 index 0000000..811e8f4 --- /dev/null +++ b/src/botai/botai.h @@ -0,0 +1,110 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: botai.h +// Function: bot AI +// Programmer: Mr Elusive (MrElusive@idsoftware.com) +// Last update: 1999-08-18 +// Tab Size: 3 +//=========================================================================== + +//debug line colors +#define LINECOLOR_NONE -1 +#define LINECOLOR_RED 1 +#define LINECOLOR_GREEN 2 +#define LINECOLOR_BLUE 3 +#define LINECOLOR_YELLOW 4 +#define LINECOLOR_ORANGE 5 + +//Print types +#define PRT_MESSAGE 1 +#define PRT_WARNING 2 +#define PRT_ERROR 3 +#define PRT_FATAL 4 +#define PRT_EXIT 5 + +//console message types +#define CMS_NORMAL 0 +#define CMS_CHAT 1 + +//some maxs +#define MAX_NETNAME 36 +#define MAX_CLIENTSKINNAME 128 +#define MAX_FILEPATH 144 +#define MAX_CHARACTERNAME 144 + +#ifndef BSPTRACE + +//bsp_trace_t hit surface +typedef struct bsp_surface_s +{ + char name[16]; + int flags; + int value; +} bsp_surface_t; + +//remove the bsp_trace_s structure definition l8r on +//a trace is returned when a box is swept through the world +typedef struct bsp_trace_s +{ + qboolean allsolid; // if true, plane is not valid + qboolean startsolid; // if true, the initial point was in a solid area + float fraction; // time completed, 1.0 = didn't hit anything + vec3_t endpos; // final position + cplane_t plane; // surface normal at impact + float exp_dist; // expanded plane distance + int sidenum; // number of the brush side hit + bsp_surface_t surface; // the hit point surface + int contents; // contents on other side of surface hit + int ent; // number of entity hit +} bsp_trace_t; + +#define BSPTRACE +#endif // BSPTRACE + +// +// imported functions used for the BotAI +// + + +// from the server +/* +void trap_Cvar_Register( vmCvar_t *cvar, const char *var_name, const char *value, int flags ); +void trap_Cvar_Update( vmCvar_t *cvar ); +void trap_Cvar_Set( const char *var_name, const char *value ); +int trap_Cvar_VariableIntegerValue( const char *var_name ); +void trap_Cvar_VariableStringBuffer( const char *var_name, char *buffer, int bufsize ); +void trap_GetConfigstring( int num, char *buffer, int bufferSize ); +void trap_GetServerinfo( char *buffer, int bufferSize ); +int trap_PointContents( const vec3_t point, int passEntityNum ); +qboolean trap_InPVS( const vec3_t p1, const vec3_t p2 ); +int trap_BotAllocateClient( void ); +void trap_BotFreeClient( int clientNum ); +*/ diff --git a/src/botai/chars.h b/src/botai/chars.h new file mode 100644 index 0000000..258adb5 --- /dev/null +++ b/src/botai/chars.h @@ -0,0 +1,150 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: chars.h +// Function: bot characteristics +// Programmer: Mr Elusive (MrElusive@idsoftware.com) +// Last update: 1999-09-08 +// Tab Size: 4 (real tabs) +//=========================================================================== + + +//======================================================== +//======================================================== +//name +#define CHARACTERISTIC_NAME 0 //string +//gender of the bot +#define CHARACTERISTIC_GENDER 1 //string ("male", "female", "it") +//attack skill +// > 0.0 && < 0.2 = don't move +// > 0.3 && < 1.0 = aim at enemy during retreat +// > 0.0 && < 0.4 = only move forward/backward +// >= 0.4 && < 1.0 = circle strafing +// > 0.7 && < 1.0 = random strafe direction change +#define CHARACTERISTIC_ATTACK_SKILL 2 //float [0, 1] +//weapon weight file +#define CHARACTERISTIC_WEAPONWEIGHTS 3 //string +//view angle difference to angle change factor +#define CHARACTERISTIC_VIEW_FACTOR 4 //float <0, 1] +//maximum view angle change +#define CHARACTERISTIC_VIEW_MAXCHANGE 5 //float [1, 360] +//reaction time in seconds +#define CHARACTERISTIC_REACTIONTIME 6 //float [0, 5] +//accuracy when aiming +#define CHARACTERISTIC_AIM_ACCURACY 7 //float [0, 1] +//weapon specific aim accuracy +#define CHARACTERISTIC_AIM_ACCURACY_MACHINEGUN 8 //float [0, 1] +#define CHARACTERISTIC_AIM_ACCURACY_SHOTGUN 9 //float [0, 1] +#define CHARACTERISTIC_AIM_ACCURACY_ROCKETLAUNCHER 10 //float [0, 1] +#define CHARACTERISTIC_AIM_ACCURACY_GRENADELAUNCHER 11 //float [0, 1] +#define CHARACTERISTIC_AIM_ACCURACY_LIGHTNING 12 +#define CHARACTERISTIC_AIM_ACCURACY_SP5 13 //float [0, 1] +#define CHARACTERISTIC_AIM_ACCURACY_RAILGUN 14 +#define CHARACTERISTIC_AIM_ACCURACY_BFG10K 15 //float [0, 1] +//skill when aiming +// > 0.0 && < 0.9 = aim is affected by enemy movement +// > 0.4 && <= 0.8 = enemy linear leading +// > 0.8 && <= 1.0 = enemy exact movement leading +// > 0.5 && <= 1.0 = prediction shots when enemy is not visible +// > 0.6 && <= 1.0 = splash damage by shooting nearby geometry +#define CHARACTERISTIC_AIM_SKILL 16 //float [0, 1] +//weapon specific aim skill +#define CHARACTERISTIC_AIM_SKILL_ROCKETLAUNCHER 17 //float [0, 1] +#define CHARACTERISTIC_AIM_SKILL_GRENADELAUNCHER 18 //float [0, 1] +#define CHARACTERISTIC_AIM_SKILL_SP5 19 //float [0, 1] +#define CHARACTERISTIC_AIM_SKILL_BFG10K 20 //float [0, 1] +//======================================================== +//chat +//======================================================== +//file with chats +#define CHARACTERISTIC_CHAT_FILE 21 //string +//name of the chat character +#define CHARACTERISTIC_CHAT_NAME 22 //string +//characters per minute type speed +#define CHARACTERISTIC_CHAT_CPM 23 //integer [1, 4000] +//tendency to insult/praise +#define CHARACTERISTIC_CHAT_INSULT 24 //float [0, 1] +//tendency to chat misc +#define CHARACTERISTIC_CHAT_MISC 25 //float [0, 1] +//tendency to chat at start or end of level +#define CHARACTERISTIC_CHAT_STARTENDLEVEL 26 //float [0, 1] +//tendency to chat entering or exiting the game +#define CHARACTERISTIC_CHAT_ENTEREXITGAME 27 //float [0, 1] +//tendency to chat when killed someone +#define CHARACTERISTIC_CHAT_KILL 28 //float [0, 1] +//tendency to chat when died +#define CHARACTERISTIC_CHAT_DEATH 29 //float [0, 1] +//tendency to chat when enemy suicides +#define CHARACTERISTIC_CHAT_ENEMYSUICIDE 30 //float [0, 1] +//tendency to chat when hit while talking +#define CHARACTERISTIC_CHAT_HITTALKING 31 //float [0, 1] +//tendency to chat when bot was hit but didn't dye +#define CHARACTERISTIC_CHAT_HITNODEATH 32 //float [0, 1] +//tendency to chat when bot hit the enemy but enemy didn't dye +#define CHARACTERISTIC_CHAT_HITNOKILL 33 //float [0, 1] +//tendency to randomly chat +#define CHARACTERISTIC_CHAT_RANDOM 34 //float [0, 1] +//tendency to reply +#define CHARACTERISTIC_CHAT_REPLY 35 //float [0, 1] +//======================================================== +//movement +//======================================================== +//tendency to crouch +#define CHARACTERISTIC_CROUCHER 36 //float [0, 1] +//tendency to jump +#define CHARACTERISTIC_JUMPER 37 //float [0, 1] +//tendency to walk +#define CHARACTERISTIC_WALKER 48 //float [0, 1] +//tendency to jump using a weapon +#define CHARACTERISTIC_WEAPONJUMPING 38 //float [0, 1] +//tendency to use the grapple hook when available +#define CHARACTERISTIC_GRAPPLE_USER 39 //float [0, 1] //use this!! +//======================================================== +//goal +//======================================================== +//item weight file +#define CHARACTERISTIC_ITEMWEIGHTS 40 //string +//the aggression of the bot +#define CHARACTERISTIC_AGGRESSION 41 //float [0, 1] +//the self preservation of the bot (rockets near walls etc.) +#define CHARACTERISTIC_SELFPRESERVATION 42 //float [0, 1] +//how likely the bot is to take revenge +#define CHARACTERISTIC_VENGEFULNESS 43 //float [0, 1] //use this!! +//tendency to camp +#define CHARACTERISTIC_CAMPER 44 //float [0, 1] +//======================================================== +//======================================================== +//tendency to get easy frags +#define CHARACTERISTIC_EASY_FRAGGER 45 //float [0, 1] +//how alert the bot is (view distance) +#define CHARACTERISTIC_ALERTNESS 46 //float [0, 1] +//how much the bot fires it's weapon +#define CHARACTERISTIC_FIRETHROTTLE 47 //float [0, 1] + diff --git a/src/botai/inv.h b/src/botai/inv.h new file mode 100644 index 0000000..b54e375 --- /dev/null +++ b/src/botai/inv.h @@ -0,0 +1,128 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +/* + * name: inv.h + * + * desc: + * +*/ + + +#define INVENTORY_NONE 0 +//armor +#define INVENTORY_ARMOR 1 +//weapons +#define INVENTORY_LUGER 4 +#define INVENTORY_MAUSER 5 +#define INVENTORY_MP40 6 +#define INVENTORY_SP5 7 +#define INVENTORY_ROCKETLAUNCHER 8 +#define INVENTORY_GRENADELAUNCHER 9 +#define INVENTORY_VENOM 10 +#define INVENTORY_FLAMETHROWER 11 +#define INVENTORY_CROSS 12 + +#define INVENTORY_GAUNTLET 13 + + +// please leave these open up to 27 (INVENTORY_9MM) (and double check defines when merging) +// the inventory max (MAX_ITEMS) is 256, so we aren't too concerned about running out of space + +//ammo +#define INVENTORY_9MM 27 +#define INVENTORY_792MM 28 +#define INVENTORY_SP5AMMO 29 +#define INVENTORY_ROCKETS 30 +#define INVENTORY_GRENADES 31 +#define INVENTORY_127MM 32 +#define INVENTORY_FUEL 33 +#define INVENTORY_CHARGES 34 + +// please leave these open up to 48 (INVENTORY_HEALTH) (and double check defines when merging) +// the inventory max (MAX_ITEMS) is 256, so we aren't too concerned about running out of space + +//powerups +#define INVENTORY_HEALTH 48 +#define INVENTORY_TELEPORTER 49 +#define INVENTORY_MEDKIT 50 +#define INVENTORY_QUAD 51 +#define INVENTORY_ENVIRONMENTSUIT 52 +#define INVENTORY_HASTE 53 +#define INVENTORY_INVISIBILITY 54 +#define INVENTORY_REGEN 55 +#define INVENTORY_FLIGHT 56 +#define INVENTORY_REDFLAG 57 +#define INVENTORY_BLUEFLAG 58 +//enemy stuff +#define ENEMY_HORIZONTAL_DIST 200 +#define ENEMY_HEIGHT 201 +#define NUM_VISIBLE_ENEMIES 202 +#define NUM_VISIBLE_TEAMMATES 203 + +//item numbers (make sure they are in sync with bg_itemlist in bg_misc.c) +#define MODELINDEX_ARMORSHARD 1 +#define MODELINDEX_ARMORCOMBAT 2 +#define MODELINDEX_ARMORBODY 3 +#define MODELINDEX_HEALTHSMALL 4 +#define MODELINDEX_HEALTH 5 +#define MODELINDEX_HEALTHLARGE 6 +#define MODELINDEX_HEALTHMEGA 7 + +#define MODELINDEX_GAUNTLET 8 +#define MODELINDEX_SHOTGUN 9 +#define MODELINDEX_MACHINEGUN 10 +#define MODELINDEX_GRENADELAUNCHER 11 +#define MODELINDEX_ROCKETLAUNCHER 12 +#define MODELINDEX_LIGHTNING 13 +#define MODELINDEX_RAILGUN 14 +#define MODELINDEX_SP5 15 +#define MODELINDEX_BFG10K 16 +#define MODELINDEX_GRAPPLINGHOOK 17 + +#define MODELINDEX_SHELLS 18 +#define MODELINDEX_BULLETS 19 +#define MODELINDEX_GRENADES 20 +#define MODELINDEX_CELLS 21 +#define MODELINDEX_LIGHTNINGAMMO 22 +#define MODELINDEX_ROCKETS 23 +#define MODELINDEX_SLUGS 24 +#define MODELINDEX_BFGAMMO 25 + +#define MODELINDEX_TELEPORTER 26 +#define MODELINDEX_MEDKIT 27 +#define MODELINDEX_QUAD 28 +#define MODELINDEX_ENVIRONMENTSUIT 29 +#define MODELINDEX_HASTE 30 +#define MODELINDEX_INVISIBILITY 31 +#define MODELINDEX_REGEN 32 +#define MODELINDEX_FLIGHT 33 +#define MODELINDEX_REDFLAG 34 +#define MODELINDEX_BLUEFLAG 35 + + diff --git a/src/botai/match.h b/src/botai/match.h new file mode 100644 index 0000000..5e3a79d --- /dev/null +++ b/src/botai/match.h @@ -0,0 +1,134 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: match.h +// Function: match template defines +// Programmer: Mr Elusive (MrElusive@idsoftware.com) +// Last update: 1999-10-01 +// Tab Size: 4 (real tabs) +// +//=========================================================================== + +//match template contexts +#define MTCONTEXT_ENTERGAME 2 +#define MTCONTEXT_INITIALTEAMCHAT 4 +#define MTCONTEXT_TIME 8 +#define MTCONTEXT_TEAMMATE 16 +#define MTCONTEXT_ADDRESSEE 32 +#define MTCONTEXT_PATROLKEYAREA 64 +#define MTCONTEXT_REPLYCHAT 128 +#define MTCONTEXT_CTF 256 + +//message types +#define MSG_ENTERGAME 2 //enter game message +#define MSG_HELP 3 //help someone +#define MSG_ACCOMPANY 4 //accompany someone +#define MSG_DEFENDKEYAREA 5 //defend a key area +#define MSG_RUSHBASE 6 //everyone rush to base +#define MSG_GETFLAG 7 //get the enemy flag +#define MSG_STARTTEAMLEADERSHIP 8 //someone wants to become the team leader +#define MSG_STOPTEAMLEADERSHIP 9 //someone wants to stop being the team leader +#define MSG_WHOISTEAMLAEDER 10 //who is the team leader +#define MSG_WAIT 11 //wait for someone +#define MSG_WHATAREYOUDOING 12 //what are you doing? +#define MSG_JOINSUBTEAM 13 //join a sub-team +#define MSG_LEAVESUBTEAM 14 //leave a sub-team +#define MSG_CREATENEWFORMATION 15 //create a new formation +#define MSG_FORMATIONPOSITION 16 //tell someone his/her position in a formation +#define MSG_FORMATIONSPACE 17 //set the formation intervening space +#define MSG_DOFORMATION 18 //form a known formation +#define MSG_DISMISS 19 //dismiss commanded team mates +#define MSG_CAMP 20 //camp somewhere +#define MSG_CHECKPOINT 21 //remember a check point +#define MSG_PATROL 22 //patrol between certain keypoints +#define MSG_LEADTHEWAY 23 //lead the way +#define MSG_GETITEM 24 //get an item +#define MSG_KILL 25 //kill someone +#define MSG_WHEREAREYOU 26 //where is someone +#define MSG_RETURNFLAG 27 //return the flag +#define MSG_WHATISMYCOMMAND 28 //ask the team leader what to do +#define MSG_WHICHTEAM 29 //ask which team a bot is in +// +#define MSG_ME 100 +#define MSG_EVERYONE 101 +#define MSG_MULTIPLENAMES 102 +#define MSG_NAME 103 +#define MSG_PATROLKEYAREA 104 +#define MSG_MINUTES 105 +#define MSG_SECONDS 106 +#define MSG_FOREVER 107 +// +#define MSG_CHATALL 200 +#define MSG_CHATTEAM 201 +// +#define MSG_CTF 300 //ctf message + +//command sub types +#define ST_SOMEWHERE 0 +#define ST_NEARITEM 1 +#define ST_ADDRESSED 2 +#define ST_METER 4 +#define ST_FEET 8 +#define ST_TIME 16 +#define ST_HERE 32 +#define ST_THERE 64 +#define ST_I 128 +#define ST_MORE 256 +#define ST_BACK 512 +#define ST_REVERSE 1024 +#define ST_SOMEONE 2048 +#define ST_GOTFLAG 4096 +#define ST_CAPTUREDFLAG 8192 +#define ST_RETURNEDFLAG 16384 +#define ST_TEAM 32768 + + +//word replacement variables +#define THE_ENEMY 7 +#define THE_TEAM 7 +//team message variables +#define NETNAME 0 +#define PLACE 1 +#define FLAG 1 +#define MESSAGE 2 +#define ADDRESSEE 2 +#define ITEM 3 +#define TEAMMATE 4 +#define TEAMNAME 4 +#define ENEMY 4 +#define KEYAREA 5 +#define FORMATION 5 +#define POSITION 5 +#define NUMBER 5 +#define TIME 6 +#define NAME 6 +#define MORE 6 + + diff --git a/src/botai/syn.h b/src/botai/syn.h new file mode 100644 index 0000000..4b85612 --- /dev/null +++ b/src/botai/syn.h @@ -0,0 +1,46 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: syn.h +// Function: synonyms +// Programmer: Mr Elusive (MrElusive@idsoftware.com) +// Last update: 1999-09-08 +// Tab Size: 4 (real tabs) +// Notes: - +//=========================================================================== + +#define CONTEXT_ALL 0xFFFFFFFF +#define CONTEXT_NORMAL 1 +#define CONTEXT_NEARBYITEM 2 +#define CONTEXT_CTFREDTEAM 4 +#define CONTEXT_CTFBLUETEAM 8 +#define CONTEXT_REPLY 16 + +#define CONTEXT_NAMES 1024 diff --git a/src/botlib/aasfile.h b/src/botlib/aasfile.h new file mode 100644 index 0000000..5ea0659 --- /dev/null +++ b/src/botlib/aasfile.h @@ -0,0 +1,276 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + + +//NOTE: int = default signed +// default long + +#define AASID ( ( 'S' << 24 ) + ( 'A' << 16 ) + ( 'A' << 8 ) + 'E' ) +#define AASVERSION 8 + +//presence types +#define PRESENCE_NONE 1 +#define PRESENCE_NORMAL 2 +#define PRESENCE_CROUCH 4 + +//travel types +#define MAX_TRAVELTYPES 32 +#define TRAVEL_INVALID 1 //temporary not possible +#define TRAVEL_WALK 2 //walking +#define TRAVEL_CROUCH 3 //crouching +#define TRAVEL_BARRIERJUMP 4 //jumping onto a barrier +#define TRAVEL_JUMP 5 //jumping +#define TRAVEL_LADDER 6 //climbing a ladder +#define TRAVEL_WALKOFFLEDGE 7 //walking of a ledge +#define TRAVEL_SWIM 8 //swimming +#define TRAVEL_WATERJUMP 9 //jump out of the water +#define TRAVEL_TELEPORT 10 //teleportation +#define TRAVEL_ELEVATOR 11 //travel by elevator +#define TRAVEL_ROCKETJUMP 12 //rocket jumping required for travel +#define TRAVEL_BFGJUMP 13 //bfg jumping required for travel +#define TRAVEL_GRAPPLEHOOK 14 //grappling hook required for travel +#define TRAVEL_DOUBLEJUMP 15 //double jump +#define TRAVEL_RAMPJUMP 16 //ramp jump +#define TRAVEL_STRAFEJUMP 17 //strafe jump +#define TRAVEL_JUMPPAD 18 //jump pad +#define TRAVEL_FUNCBOB 19 //func bob + +//additional travel flags +#define TRAVELTYPE_MASK 0xFFFFFF +#define TRAVELFLAG_NOTTEAM1 ( 1 << 24 ) +#define TRAVELFLAG_NOTTEAM2 ( 2 << 24 ) + +//face flags +#define FACE_SOLID 1 //just solid at the other side +#define FACE_LADDER 2 //ladder +#define FACE_GROUND 4 //standing on ground when in this face +#define FACE_GAP 8 //gap in the ground +#define FACE_LIQUID 16 +#define FACE_LIQUIDSURFACE 32 + +//area contents +#define AREACONTENTS_WATER 1 +#define AREACONTENTS_LAVA 2 +#define AREACONTENTS_SLIME 4 +#define AREACONTENTS_CLUSTERPORTAL 8 +#define AREACONTENTS_TELEPORTAL 16 +#define AREACONTENTS_ROUTEPORTAL 32 +#define AREACONTENTS_TELEPORTER 64 +#define AREACONTENTS_JUMPPAD 128 +#define AREACONTENTS_DONOTENTER 256 +#define AREACONTENTS_VIEWPORTAL 512 +// Rafael - nopass +#define AREACONTENTS_DONOTENTER_LARGE 1024 +#define AREACONTENTS_MOVER 2048 + +//number of model of the mover inside this area +#define AREACONTENTS_MODELNUMSHIFT 24 +#define AREACONTENTS_MAXMODELNUM 0xFF +#define AREACONTENTS_MODELNUM ( AREACONTENTS_MAXMODELNUM << AREACONTENTS_MODELNUMSHIFT ) + +//area flags +#define AREA_GROUNDED 1 //bot can stand on the ground +#define AREA_LADDER 2 //area contains one or more ladder faces +#define AREA_LIQUID 4 //area contains a liquid +// Ridah +#define AREA_DISABLED 8 +#define AREA_USEFORROUTING 1024 + +//aas file header lumps +#define AAS_LUMPS 14 +#define AASLUMP_BBOXES 0 +#define AASLUMP_VERTEXES 1 +#define AASLUMP_PLANES 2 +#define AASLUMP_EDGES 3 +#define AASLUMP_EDGEINDEX 4 +#define AASLUMP_FACES 5 +#define AASLUMP_FACEINDEX 6 +#define AASLUMP_AREAS 7 +#define AASLUMP_AREASETTINGS 8 +#define AASLUMP_REACHABILITY 9 +#define AASLUMP_NODES 10 +#define AASLUMP_PORTALS 11 +#define AASLUMP_PORTALINDEX 12 +#define AASLUMP_CLUSTERS 13 + +//========== bounding box ========= + +//bounding box +typedef struct aas_bbox_s +{ + int presencetype; + int flags; + vec3_t mins, maxs; +} aas_bbox_t; + +//============ settings =========== + +//reachability to another area +typedef struct aas_reachability_s +{ + int areanum; //number of the reachable area + int facenum; //number of the face towards the other area + int edgenum; //number of the edge towards the other area + vec3_t start; //start point of inter area movement + vec3_t end; //end point of inter area movement + int traveltype; //type of travel required to get to the area + unsigned short int traveltime; //travel time of the inter area movement +} aas_reachability_t; + +//area settings +typedef struct aas_areasettings_s +{ + //could also add all kind of statistic fields + int contents; //contents of the convex area + int areaflags; //several area flags + int presencetype; //how a bot can be present in this convex area + int cluster; //cluster the area belongs to, if negative it's a portal + int clusterareanum; //number of the area in the cluster + int numreachableareas; //number of reachable areas from this one + int firstreachablearea; //first reachable area in the reachable area index + // Ridah, add a ground steepness stat, so we can avoid terrain when we can take a close-by flat route + float groundsteepness; // 0 = flat, 1 = steep +} aas_areasettings_t; + +//cluster portal +typedef struct aas_portal_s +{ + int areanum; //area that is the actual portal + int frontcluster; //cluster at front of portal + int backcluster; //cluster at back of portal + int clusterareanum[2]; //number of the area in the front and back cluster +} aas_portal_t; + +//cluster portal index +typedef int aas_portalindex_t; + +//cluster +typedef struct aas_cluster_s +{ + int numareas; //number of areas in the cluster + int numreachabilityareas; //number of areas with reachabilities + int numportals; //number of cluster portals + int firstportal; //first cluster portal in the index +} aas_cluster_t; + +//============ 3d definition ============ + +typedef vec3_t aas_vertex_t; + +//just a plane in the third dimension +typedef struct aas_plane_s +{ + vec3_t normal; //normal vector of the plane + float dist; //distance of the plane (normal vector * distance = point in plane) + int type; +} aas_plane_t; + +//edge +typedef struct aas_edge_s +{ + int v[2]; //numbers of the vertexes of this edge +} aas_edge_t; + +//edge index, negative if vertexes are reversed +typedef int aas_edgeindex_t; + +//a face bounds a convex area, often it will also seperate two convex areas +typedef struct aas_face_s +{ + int planenum; //number of the plane this face is in + int faceflags; //face flags (no use to create face settings for just this field) + int numedges; //number of edges in the boundary of the face + int firstedge; //first edge in the edge index + int frontarea; //convex area at the front of this face + int backarea; //convex area at the back of this face +} aas_face_t; + +//face index, stores a negative index if backside of face +typedef int aas_faceindex_t; + +//convex area with a boundary of faces +typedef struct aas_area_s +{ + int areanum; //number of this area + //3d definition + int numfaces; //number of faces used for the boundary of the convex area + int firstface; //first face in the face index used for the boundary of the convex area + vec3_t mins; //mins of the convex area + vec3_t maxs; //maxs of the convex area + vec3_t center; //'center' of the convex area +} aas_area_t; + +//nodes of the bsp tree +typedef struct aas_node_s +{ + int planenum; + int children[2]; //child nodes of this node, or convex areas as leaves when negative + //when a child is zero it's a solid leaf +} aas_node_t; + +//=========== aas file =============== + +//header lump +typedef struct +{ + int fileofs; + int filelen; +} aas_lump_t; + +//aas file header +typedef struct aas_header_s +{ + int ident; + int version; + int bspchecksum; + //data entries + aas_lump_t lumps[AAS_LUMPS]; +} aas_header_t; + + +//====== additional information ====== +/* + +- when a node child is a solid leaf the node child number is zero +- two adjacent areas (sharing a plane at opposite sides) share a face + this face is a portal between the areas +- when an area uses a face from the faceindex with a positive index + then the face plane normal points into the area +- the face edges are stored counter clockwise using the edgeindex +- two adjacent convex areas (sharing a face) only share One face + this is a simple result of the areas being convex +- the convex areas can't have a mixture of ground and gap faces + other mixtures of faces in one area are allowed +- areas with the AREACONTENTS_CLUSTERPORTAL in the settings have + cluster number zero +- edge zero is a dummy +- face zero is a dummy +- area zero is a dummy +- node zero is a dummy +*/ diff --git a/src/botlib/be_aas.h b/src/botlib/be_aas.h new file mode 100644 index 0000000..445983d --- /dev/null +++ b/src/botlib/be_aas.h @@ -0,0 +1,193 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas.h + * + * desc: Area Awareness System, stuff exported to the AI + * + * $Archive: /Wolf4/src/botlib/be_aas.h $ + * + *****************************************************************************/ + +#define MAX_AAS_WORLDS 2 // one for each bounding box type + +#ifndef MAX_STRINGFIELD +#define MAX_STRINGFIELD 80 +#endif + +//travel flags +#define TFL_INVALID 0x0000001 //traveling temporary not possible +#define TFL_WALK 0x0000002 //walking +#define TFL_CROUCH 0x0000004 //crouching +#define TFL_BARRIERJUMP 0x0000008 //jumping onto a barrier +#define TFL_JUMP 0x0000010 //jumping +#define TFL_LADDER 0x0000020 //climbing a ladder +#define TFL_WALKOFFLEDGE 0x0000080 //walking of a ledge +#define TFL_SWIM 0x0000100 //swimming +#define TFL_WATERJUMP 0x0000200 //jumping out of the water +#define TFL_TELEPORT 0x0000400 //teleporting +#define TFL_ELEVATOR 0x0000800 //elevator +#define TFL_ROCKETJUMP 0x0001000 //rocket jumping +#define TFL_BFGJUMP 0x0002000 //bfg jumping +#define TFL_GRAPPLEHOOK 0x0004000 //grappling hook +#define TFL_DOUBLEJUMP 0x0008000 //double jump +#define TFL_RAMPJUMP 0x0010000 //ramp jump +#define TFL_STRAFEJUMP 0x0020000 //strafe jump +#define TFL_JUMPPAD 0x0040000 //jump pad +#define TFL_AIR 0x0080000 //travel through air +#define TFL_WATER 0x0100000 //travel through water +#define TFL_SLIME 0x0200000 //travel through slime +#define TFL_LAVA 0x0400000 //travel through lava +#define TFL_DONOTENTER 0x0800000 //travel through donotenter area +#define TFL_FUNCBOB 0x1000000 //func bobbing +#define TFL_DONOTENTER_LARGE 0x2000000 //travel through donotenter area + +//default travel flags + +//----(SA) modified since slime is no longer deadly +#define TFL_DEFAULT TFL_WALK | TFL_CROUCH | TFL_BARRIERJUMP | \ + TFL_JUMP | TFL_LADDER | \ + TFL_WALKOFFLEDGE | TFL_SWIM | TFL_WATERJUMP | \ + TFL_TELEPORT | TFL_ELEVATOR | TFL_AIR | \ + TFL_WATER | TFL_SLIME | \ + TFL_JUMPPAD | TFL_FUNCBOB + +typedef enum +{ + SOLID_NOT, // no interaction with other objects + SOLID_TRIGGER, // only touch when inside, after moving + SOLID_BBOX, // touch on edge + SOLID_BSP // bsp clip, touch on edge +} solid_t; + +//a trace is returned when a box is swept through the AAS world +typedef struct aas_trace_s +{ + qboolean startsolid; // if true, the initial point was in a solid area + float fraction; // time completed, 1.0 = didn't hit anything + vec3_t endpos; // final position + int ent; // entity blocking the trace + int lastarea; // last area the trace was in (zero if none) + int area; // area blocking the trace (zero if none) + int planenum; // number of the plane that was hit +} aas_trace_t; + +/* Defined in botlib.h + +//bsp_trace_t hit surface +typedef struct bsp_surface_s +{ + char name[16]; + int flags; + int value; +} bsp_surface_t; + +//a trace is returned when a box is swept through the BSP world +typedef struct bsp_trace_s +{ + qboolean allsolid; // if true, plane is not valid + qboolean startsolid; // if true, the initial point was in a solid area + float fraction; // time completed, 1.0 = didn't hit anything + vec3_t endpos; // final position + cplane_t plane; // surface normal at impact + float exp_dist; // expanded plane distance + int sidenum; // number of the brush side hit + bsp_surface_t surface; // hit surface + int contents; // contents on other side of surface hit + int ent; // number of entity hit +} bsp_trace_t; +// +*/ + +//entity info +typedef struct aas_entityinfo_s +{ + int valid; // true if updated this frame + int type; // entity type + int flags; // entity flags + float ltime; // local time + float update_time; // time between last and current update + int number; // number of the entity + vec3_t origin; // origin of the entity + vec3_t angles; // angles of the model + vec3_t old_origin; // for lerping + vec3_t lastvisorigin; // last visible origin + vec3_t mins; // bounding box minimums + vec3_t maxs; // bounding box maximums + int groundent; // ground entity + int solid; // solid type + int modelindex; // model used + int modelindex2; // weapons, CTF flags, etc + int frame; // model frame number + int event; // impulse events -- muzzle flashes, footsteps, etc + int eventParm; // even parameter + int powerups; // bit flags + int weapon; // determines weapon and flash model, etc + int legsAnim; // mask off ANIM_TOGGLEBIT + int torsoAnim; // mask off ANIM_TOGGLEBIT +// int weapAnim; // mask off ANIM_TOGGLEBIT //----(SA) added +//----(SA) didn't want to comment in as I wasn't sure of any implications of changing this structure. +} aas_entityinfo_t; + + +//client movement prediction stop events, stop as soon as: +#define SE_NONE 0 +#define SE_HITGROUND 1 // the ground is hit +#define SE_LEAVEGROUND 2 // there's no ground +#define SE_ENTERWATER 4 // water is entered +#define SE_ENTERSLIME 8 // slime is entered +#define SE_ENTERLAVA 16 // lava is entered +#define SE_HITGROUNDDAMAGE 32 // the ground is hit with damage +#define SE_GAP 64 // there's a gap +#define SE_TOUCHJUMPPAD 128 // touching a jump pad area +#define SE_TOUCHTELEPORTER 256 // touching teleporter +#define SE_ENTERAREA 512 // the given stoparea is entered +#define SE_HITGROUNDAREA 1024 // a ground face in the area is hit + +typedef struct aas_clientmove_s +{ + vec3_t endpos; //position at the end of movement prediction + vec3_t velocity; //velocity at the end of movement prediction + aas_trace_t trace; //last trace + int presencetype; //presence type at end of movement prediction + int stopevent; //event that made the prediction stop + float endcontents; //contents at the end of movement prediction + float time; //time predicted ahead + int frames; //number of frames predicted ahead +} aas_clientmove_t; + +typedef struct aas_altroutegoal_s +{ + vec3_t origin; + int areanum; + unsigned short starttraveltime; + unsigned short goaltraveltime; + unsigned short extratraveltime; +} aas_altroutegoal_t; diff --git a/src/botlib/be_aas_bsp.h b/src/botlib/be_aas_bsp.h new file mode 100644 index 0000000..e3bcfb0 --- /dev/null +++ b/src/botlib/be_aas_bsp.h @@ -0,0 +1,96 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_bsp.h + * + * desc: AAS + * + * + *****************************************************************************/ + +#ifdef AASINTERN +//loads the given BSP file +int AAS_LoadBSPFile( void ); +//dump the loaded BSP data +void AAS_DumpBSPData( void ); +//unlink the given entity from the bsp tree leaves +void AAS_UnlinkFromBSPLeaves( bsp_link_t *leaves ); +//link the given entity to the bsp tree leaves of the given model +bsp_link_t *AAS_BSPLinkEntity( vec3_t absmins, + vec3_t absmaxs, + int entnum, + int modelnum ); + +//calculates collision with given entity +qboolean AAS_EntityCollision( int entnum, + vec3_t start, + vec3_t boxmins, + vec3_t boxmaxs, + vec3_t end, + int contentmask, + bsp_trace_t *trace ); +//for debugging +void AAS_PrintFreeBSPLinks( char *str ); +// + +#endif //AASINTERN + +#define MAX_EPAIRKEY 128 + +//trace through the world +bsp_trace_t AAS_Trace( vec3_t start, + vec3_t mins, + vec3_t maxs, + vec3_t end, + int passent, + int contentmask ); +//returns the contents at the given point +int AAS_PointContents( vec3_t point ); +//returns true when p2 is in the PVS of p1 +qboolean AAS_inPVS( vec3_t p1, vec3_t p2 ); +//returns true when p2 is in the PHS of p1 +qboolean AAS_inPHS( vec3_t p1, vec3_t p2 ); +//returns true if the given areas are connected +qboolean AAS_AreasConnected( int area1, int area2 ); +//creates a list with entities totally or partly within the given box +int AAS_BoxEntities( vec3_t absmins, vec3_t absmaxs, int *list, int maxcount ); +//gets the mins, maxs and origin of a BSP model +void AAS_BSPModelMinsMaxsOrigin( int modelnum, vec3_t angles, vec3_t mins, vec3_t maxs, vec3_t origin ); +//handle to the next bsp entity +int AAS_NextBSPEntity( int ent ); +//return the value of the BSP epair key +int AAS_ValueForBSPEpairKey( int ent, char *key, char *value, int size ); +//get a vector for the BSP epair key +int AAS_VectorForBSPEpairKey( int ent, char *key, vec3_t v ); +//get a float for the BSP epair key +int AAS_FloatForBSPEpairKey( int ent, char *key, float *value ); +//get an integer for the BSP epair key +int AAS_IntForBSPEpairKey( int ent, char *key, int *value ); + diff --git a/src/botlib/be_aas_bspq3.c b/src/botlib/be_aas_bspq3.c new file mode 100644 index 0000000..1f54c15 --- /dev/null +++ b/src/botlib/be_aas_bspq3.c @@ -0,0 +1,525 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_bspq3.c + * + * desc: BSP, Environment Sampling + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "l_memory.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "aasfile.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "be_aas_funcs.h" +#include "be_aas_def.h" + +extern botlib_import_t botimport; + +//#define TRACE_DEBUG + +#define ON_EPSILON 0.005 +//#define DEG2RAD( a ) (( a * M_PI ) / 180.0F) + +#define MAX_BSPENTITIES 2048 + +typedef struct rgb_s +{ + int red; + int green; + int blue; +} rgb_t; + +//bsp entity epair +typedef struct bsp_epair_s +{ + char *key; + char *value; + struct bsp_epair_s *next; +} bsp_epair_t; + +//bsp data entity +typedef struct bsp_entity_s +{ + bsp_epair_t *epairs; +} bsp_entity_t; + +//id Sofware BSP data +typedef struct bsp_s +{ + //true when bsp file is loaded + int loaded; + //entity data + int entdatasize; + char *dentdata; + //bsp entities + int numentities; + bsp_entity_t entities[MAX_BSPENTITIES]; + //memory used for strings and epairs + byte *ebuffer; +} bsp_t; + +//global bsp +bsp_t bspworld; + + +#ifdef BSP_DEBUG +typedef struct cname_s +{ + int value; + char *name; +} cname_t; + +cname_t contentnames[] = +{ + {CONTENTS_SOLID,"CONTENTS_SOLID"}, + {CONTENTS_WINDOW,"CONTENTS_WINDOW"}, + {CONTENTS_AUX,"CONTENTS_AUX"}, + {CONTENTS_LAVA,"CONTENTS_LAVA"}, + {CONTENTS_SLIME,"CONTENTS_SLIME"}, + {CONTENTS_WATER,"CONTENTS_WATER"}, + {CONTENTS_MIST,"CONTENTS_MIST"}, + {LAST_VISIBLE_CONTENTS,"LAST_VISIBLE_CONTENTS"}, + + {CONTENTS_AREAPORTAL,"CONTENTS_AREAPORTAL"}, + {CONTENTS_PLAYERCLIP,"CONTENTS_PLAYERCLIP"}, + {CONTENTS_MONSTERCLIP,"CONTENTS_MONSTERCLIP"}, + {CONTENTS_CURRENT_0,"CONTENTS_CURRENT_0"}, + {CONTENTS_CURRENT_90,"CONTENTS_CURRENT_90"}, + {CONTENTS_CURRENT_180,"CONTENTS_CURRENT_180"}, + {CONTENTS_CURRENT_270,"CONTENTS_CURRENT_270"}, + {CONTENTS_CURRENT_UP,"CONTENTS_CURRENT_UP"}, + {CONTENTS_CURRENT_DOWN,"CONTENTS_CURRENT_DOWN"}, + {CONTENTS_ORIGIN,"CONTENTS_ORIGIN"}, + {CONTENTS_MONSTER,"CONTENTS_MONSTER"}, + {CONTENTS_DEADMONSTER,"CONTENTS_DEADMONSTER"}, + {CONTENTS_DETAIL,"CONTENTS_DETAIL"}, + {CONTENTS_TRANSLUCENT,"CONTENTS_TRANSLUCENT"}, + {CONTENTS_LADDER,"CONTENTS_LADDER"}, + {0, 0} +}; + +void PrintContents( int contents ) { + int i; + + for ( i = 0; contentnames[i].value; i++ ) + { + if ( contents & contentnames[i].value ) { + botimport.Print( PRT_MESSAGE, "%s\n", contentnames[i].name ); + } //end if + } //end for +} //end of the function PrintContents + +#endif //BSP_DEBUG +//=========================================================================== +// traces axial boxes of any size through the world +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bsp_trace_t AAS_Trace( vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int passent, int contentmask ) { + bsp_trace_t bsptrace; + botimport.Trace( &bsptrace, start, mins, maxs, end, passent, contentmask ); + return bsptrace; +} //end of the function AAS_Trace +//=========================================================================== +// returns the contents at the given point +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_PointContents( vec3_t point ) { + return botimport.PointContents( point ); +} //end of the function AAS_PointContents +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_EntityCollision( int entnum, + vec3_t start, vec3_t boxmins, vec3_t boxmaxs, vec3_t end, + int contentmask, bsp_trace_t *trace ) { + bsp_trace_t enttrace; + + botimport.EntityTrace( &enttrace, start, boxmins, boxmaxs, end, entnum, contentmask ); + if ( enttrace.fraction < trace->fraction ) { + memcpy( trace, &enttrace, sizeof( bsp_trace_t ) ); + return qtrue; + } //end if + return qfalse; +} //end of the function AAS_EntityCollision +//=========================================================================== +// returns true if in Potentially Hearable Set +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_inPVS( vec3_t p1, vec3_t p2 ) { + return botimport.inPVS( p1, p2 ); +} //end of the function AAS_InPVS +//=========================================================================== +// returns true if in Potentially Visible Set +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_inPHS( vec3_t p1, vec3_t p2 ) { + return qtrue; +} //end of the function AAS_inPHS +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_BSPModelMinsMaxsOrigin( int modelnum, vec3_t angles, vec3_t mins, vec3_t maxs, vec3_t origin ) { + botimport.BSPModelMinsMaxsOrigin( modelnum, angles, mins, maxs, origin ); +} //end of the function AAS_BSPModelMinsMaxs +//=========================================================================== +// unlinks the entity from all leaves +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_UnlinkFromBSPLeaves( bsp_link_t *leaves ) { +} //end of the function AAS_UnlinkFromBSPLeaves +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bsp_link_t *AAS_BSPLinkEntity( vec3_t absmins, vec3_t absmaxs, int entnum, int modelnum ) { + return NULL; +} //end of the function AAS_BSPLinkEntity +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_BoxEntities( vec3_t absmins, vec3_t absmaxs, int *list, int maxcount ) { + return 0; +} //end of the function AAS_BoxEntities +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_NextBSPEntity( int ent ) { + ent++; + if ( ent >= 1 && ent < bspworld.numentities ) { + return ent; + } + return 0; +} //end of the function AAS_NextBSPEntity +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_BSPEntityInRange( int ent ) { + if ( ent <= 0 || ent >= bspworld.numentities ) { + botimport.Print( PRT_MESSAGE, "bsp entity out of range\n" ); + return qfalse; + } //end if + return qtrue; +} //end of the function AAS_BSPEntityInRange +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_ValueForBSPEpairKey( int ent, char *key, char *value, int size ) { + bsp_epair_t *epair; + + value[0] = '\0'; + if ( !AAS_BSPEntityInRange( ent ) ) { + return qfalse; + } + for ( epair = bspworld.entities[ent].epairs; epair; epair = epair->next ) + { + if ( !strcmp( epair->key, key ) ) { + strncpy( value, epair->value, size - 1 ); + value[size - 1] = '\0'; + return qtrue; + } //end if + } //end for + return qfalse; +} //end of the function AAS_FindBSPEpair +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_VectorForBSPEpairKey( int ent, char *key, vec3_t v ) { + char buf[MAX_EPAIRKEY]; + double v1, v2, v3; + + VectorClear( v ); + if ( !AAS_ValueForBSPEpairKey( ent, key, buf, MAX_EPAIRKEY ) ) { + return qfalse; + } + //scanf into doubles, then assign, so it is vec_t size independent + v1 = v2 = v3 = 0; + sscanf( buf, "%lf %lf %lf", &v1, &v2, &v3 ); + v[0] = v1; + v[1] = v2; + v[2] = v3; + return qtrue; +} //end of the function AAS_VectorForBSPEpairKey +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_FloatForBSPEpairKey( int ent, char *key, float *value ) { + char buf[MAX_EPAIRKEY]; + + *value = 0; + if ( !AAS_ValueForBSPEpairKey( ent, key, buf, MAX_EPAIRKEY ) ) { + return qfalse; + } + *value = atof( buf ); + return qtrue; +} //end of the function AAS_FloatForBSPEpairKey +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_IntForBSPEpairKey( int ent, char *key, int *value ) { + char buf[MAX_EPAIRKEY]; + + *value = 0; + if ( !AAS_ValueForBSPEpairKey( ent, key, buf, MAX_EPAIRKEY ) ) { + return qfalse; + } + *value = atoi( buf ); + return qtrue; +} //end of the function AAS_IntForBSPEpairKey +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FreeBSPEntities( void ) { +// RF, optimized memory allocation +/* + int i; + bsp_entity_t *ent; + bsp_epair_t *epair, *nextepair; + + for (i = 1; i < bspworld.numentities; i++) + { + ent = &bspworld.entities[i]; + for (epair = ent->epairs; epair; epair = nextepair) + { + nextepair = epair->next; + // + if (epair->key) FreeMemory(epair->key); + if (epair->value) FreeMemory(epair->value); + FreeMemory(epair); + } //end for + } //end for +*/ + if ( bspworld.ebuffer ) { + FreeMemory( bspworld.ebuffer ); + } + bspworld.numentities = 0; +} //end of the function AAS_FreeBSPEntities +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ParseBSPEntities( void ) { + script_t *script; + token_t token; + bsp_entity_t *ent; + bsp_epair_t *epair; + byte *buffer, *buftrav; + int bufsize; + + // RF, modified this, so that it first gathers up memory requirements, then allocates a single chunk, + // and places the strings all in there + + bspworld.ebuffer = NULL; + + script = LoadScriptMemory( bspworld.dentdata, bspworld.entdatasize, "entdata" ); + SetScriptFlags( script, SCFL_NOSTRINGWHITESPACES | SCFL_NOSTRINGESCAPECHARS ); //SCFL_PRIMITIVE); + + bufsize = 0; + + while ( PS_ReadToken( script, &token ) ) + { + if ( strcmp( token.string, "{" ) ) { + ScriptError( script, "invalid %s\n", token.string ); + AAS_FreeBSPEntities(); + FreeScript( script ); + return; + } //end if + if ( bspworld.numentities >= MAX_BSPENTITIES ) { + botimport.Print( PRT_MESSAGE, "too many entities in BSP file\n" ); + break; + } //end if + while ( PS_ReadToken( script, &token ) ) + { + if ( !strcmp( token.string, "}" ) ) { + break; + } + bufsize += sizeof( bsp_epair_t ); + if ( token.type != TT_STRING ) { + ScriptError( script, "invalid %s\n", token.string ); + AAS_FreeBSPEntities(); + FreeScript( script ); + return; + } //end if + StripDoubleQuotes( token.string ); + bufsize += strlen( token.string ) + 1; + if ( !PS_ExpectTokenType( script, TT_STRING, 0, &token ) ) { + AAS_FreeBSPEntities(); + FreeScript( script ); + return; + } //end if + StripDoubleQuotes( token.string ); + bufsize += strlen( token.string ) + 1; + } //end while + if ( strcmp( token.string, "}" ) ) { + ScriptError( script, "missing }\n" ); + AAS_FreeBSPEntities(); + FreeScript( script ); + return; + } //end if + } //end while + FreeScript( script ); + + buffer = (byte *)GetClearedHunkMemory( bufsize ); + buftrav = buffer; + bspworld.ebuffer = buffer; + + // RF, now parse the entities into memory + // RF, NOTE: removed error checks for speed, no need to do them twice + + script = LoadScriptMemory( bspworld.dentdata, bspworld.entdatasize, "entdata" ); + SetScriptFlags( script, SCFL_NOSTRINGWHITESPACES | SCFL_NOSTRINGESCAPECHARS ); //SCFL_PRIMITIVE); + + bspworld.numentities = 1; + + while ( PS_ReadToken( script, &token ) ) + { + ent = &bspworld.entities[bspworld.numentities]; + bspworld.numentities++; + ent->epairs = NULL; + while ( PS_ReadToken( script, &token ) ) + { + if ( !strcmp( token.string, "}" ) ) { + break; + } + epair = (bsp_epair_t *) buftrav; buftrav += sizeof( bsp_epair_t ); + epair->next = ent->epairs; + ent->epairs = epair; + StripDoubleQuotes( token.string ); + epair->key = (char *) buftrav; buftrav += ( strlen( token.string ) + 1 ); + strcpy( epair->key, token.string ); + if ( !PS_ExpectTokenType( script, TT_STRING, 0, &token ) ) { + AAS_FreeBSPEntities(); + FreeScript( script ); + return; + } //end if + StripDoubleQuotes( token.string ); + epair->value = (char *) buftrav; buftrav += ( strlen( token.string ) + 1 ); + strcpy( epair->value, token.string ); + } //end while + } //end while + FreeScript( script ); +} //end of the function AAS_ParseBSPEntities +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_BSPTraceLight( vec3_t start, vec3_t end, vec3_t endpos, int *red, int *green, int *blue ) { + return 0; +} //end of the function AAS_BSPTraceLight +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_DumpBSPData( void ) { + AAS_FreeBSPEntities(); + + if ( bspworld.dentdata ) { + FreeMemory( bspworld.dentdata ); + } + bspworld.dentdata = NULL; + bspworld.entdatasize = 0; + // + bspworld.loaded = qfalse; + memset( &bspworld, 0, sizeof( bspworld ) ); +} //end of the function AAS_DumpBSPData +//=========================================================================== +// load an bsp file +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_LoadBSPFile( void ) { + AAS_DumpBSPData(); + bspworld.entdatasize = strlen( botimport.BSPEntityData() ) + 1; + bspworld.dentdata = (char *) GetClearedHunkMemory( bspworld.entdatasize ); + memcpy( bspworld.dentdata, botimport.BSPEntityData(), bspworld.entdatasize ); + AAS_ParseBSPEntities(); + bspworld.loaded = qtrue; + return BLERR_NOERROR; +} //end of the function AAS_LoadBSPFile diff --git a/src/botlib/be_aas_cluster.c b/src/botlib/be_aas_cluster.c new file mode 100644 index 0000000..a971bcd --- /dev/null +++ b/src/botlib/be_aas_cluster.c @@ -0,0 +1,1601 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_cluster.c + * + * desc: area clustering + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "l_memory.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "l_log.h" +#include "l_memory.h" +#include "l_libvar.h" +#include "aasfile.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "be_aas_funcs.h" +#include "be_aas_def.h" + +extern botlib_import_t botimport; + +#define AAS_MAX_PORTALS 65536 +#define AAS_MAX_PORTALINDEXSIZE 65536 +#define AAS_MAX_CLUSTERS 65536 +// +#define MAX_PORTALAREAS 1024 + +// do not flood through area faces, only use reachabilities +int nofaceflood = qtrue; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_RemoveClusterAreas( void ) { + int i; + + for ( i = 1; i < ( *aasworld ).numareas; i++ ) + { + ( *aasworld ).areasettings[i].cluster = 0; + } //end for +} //end of the function AAS_RemoveClusterAreas +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ClearCluster( int clusternum ) { + int i; + + for ( i = 1; i < ( *aasworld ).numareas; i++ ) + { + if ( ( *aasworld ).areasettings[i].cluster == clusternum ) { + ( *aasworld ).areasettings[i].cluster = 0; + } //end if + } //end for +} //end of the function AAS_ClearCluster +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_RemovePortalsClusterReference( int clusternum ) { + int portalnum; + + for ( portalnum = 1; portalnum < ( *aasworld ).numportals; portalnum++ ) + { + if ( ( *aasworld ).portals[portalnum].frontcluster == clusternum ) { + ( *aasworld ).portals[portalnum].frontcluster = 0; + } //end if + if ( ( *aasworld ).portals[portalnum].backcluster == clusternum ) { + ( *aasworld ).portals[portalnum].backcluster = 0; + } //end if + } //end for +} //end of the function AAS_RemovePortalsClusterReference +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_UpdatePortal( int areanum, int clusternum ) { + int portalnum; + aas_portal_t *portal; + aas_cluster_t *cluster; + + //find the portal of the area + for ( portalnum = 1; portalnum < ( *aasworld ).numportals; portalnum++ ) + { + if ( ( *aasworld ).portals[portalnum].areanum == areanum ) { + break; + } + } //end for + // + if ( portalnum == ( *aasworld ).numportals ) { + AAS_Error( "no portal of area %d", areanum ); + return qtrue; + } //end if + // + portal = &( *aasworld ).portals[portalnum]; + //if the portal is already fully updated + if ( portal->frontcluster == clusternum ) { + return qtrue; + } + if ( portal->backcluster == clusternum ) { + return qtrue; + } + //if the portal has no front cluster yet + if ( !portal->frontcluster ) { + portal->frontcluster = clusternum; + } //end if + //if the portal has no back cluster yet + else if ( !portal->backcluster ) { + portal->backcluster = clusternum; + } //end else if + else + { + Log_Write( "portal using area %d is seperating more than two clusters\r\n", areanum ); + //remove the cluster portal flag contents + ( *aasworld ).areasettings[areanum].contents &= ~AREACONTENTS_CLUSTERPORTAL; + return qfalse; + } //end else + if ( ( *aasworld ).portalindexsize >= AAS_MAX_PORTALINDEXSIZE ) { + AAS_Error( "AAS_MAX_PORTALINDEXSIZE" ); + return qtrue; + } //end if + //set the area cluster number to the negative portal number + ( *aasworld ).areasettings[areanum].cluster = -portalnum; + //add the portal to the cluster using the portal index + cluster = &( *aasworld ).clusters[clusternum]; + ( *aasworld ).portalindex[cluster->firstportal + cluster->numportals] = portalnum; + ( *aasworld ).portalindexsize++; + cluster->numportals++; + return qtrue; +} //end of the function AAS_UpdatePortal +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_FloodClusterAreas_r( int areanum, int clusternum ) { + aas_area_t *area; + aas_face_t *face; + int facenum, i; + + // + if ( areanum <= 0 || areanum >= ( *aasworld ).numareas ) { + AAS_Error( "AAS_FloodClusterAreas_r: areanum out of range" ); + return qfalse; + } //end if + //if the area is already part of a cluster + if ( ( *aasworld ).areasettings[areanum].cluster > 0 ) { + if ( ( *aasworld ).areasettings[areanum].cluster == clusternum ) { + return qtrue; + } + // + //there's a reachability going from one cluster to another only in one direction + // + AAS_Error( "cluster %d touched cluster %d at area %d\r\n", + clusternum, ( *aasworld ).areasettings[areanum].cluster, areanum ); + return qfalse; + } //end if + //don't add the cluster portal areas to the clusters + if ( ( *aasworld ).areasettings[areanum].contents & AREACONTENTS_CLUSTERPORTAL ) { + return AAS_UpdatePortal( areanum, clusternum ); + } //end if + //set the area cluster number + ( *aasworld ).areasettings[areanum].cluster = clusternum; + ( *aasworld ).areasettings[areanum].clusterareanum = + ( *aasworld ).clusters[clusternum].numareas; + //the cluster has an extra area + ( *aasworld ).clusters[clusternum].numareas++; + + area = &( *aasworld ).areas[areanum]; + //use area faces to flood into adjacent areas + if ( !nofaceflood ) { + for ( i = 0; i < area->numfaces; i++ ) + { + facenum = abs( ( *aasworld ).faceindex[area->firstface + i] ); + face = &( *aasworld ).faces[facenum]; + if ( face->frontarea == areanum ) { + if ( face->backarea ) { + if ( !AAS_FloodClusterAreas_r( face->backarea, clusternum ) ) { + return qfalse; + } + } + } //end if + else + { + if ( face->frontarea ) { + if ( !AAS_FloodClusterAreas_r( face->frontarea, clusternum ) ) { + return qfalse; + } + } + } //end else + } //end for + } + //use the reachabilities to flood into other areas + for ( i = 0; i < ( *aasworld ).areasettings[areanum].numreachableareas; i++ ) + { + if ( !( *aasworld ).reachability[ + ( *aasworld ).areasettings[areanum].firstreachablearea + i].areanum ) { + continue; + } //end if + if ( !AAS_FloodClusterAreas_r( ( *aasworld ).reachability[ + ( *aasworld ).areasettings[areanum].firstreachablearea + i].areanum, clusternum ) ) { + return qfalse; + } + } //end for + return qtrue; +} //end of the function AAS_FloodClusterAreas_r +//=========================================================================== +// try to flood from all areas without cluster into areas with a cluster set +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_FloodClusterAreasUsingReachabilities( int clusternum ) { + int i, j, areanum; + + for ( i = 1; i < ( *aasworld ).numareas; i++ ) + { + //if this area already has a cluster set + if ( ( *aasworld ).areasettings[i].cluster ) { + continue; + } + //if this area is a cluster portal + if ( ( *aasworld ).areasettings[i].contents & AREACONTENTS_CLUSTERPORTAL ) { + continue; + } + //loop over the reachable areas from this area + for ( j = 0; j < ( *aasworld ).areasettings[i].numreachableareas; j++ ) + { + //the reachable area + areanum = ( *aasworld ).reachability[( *aasworld ).areasettings[i].firstreachablearea + j].areanum; + //if this area is a cluster portal + if ( ( *aasworld ).areasettings[areanum].contents & AREACONTENTS_CLUSTERPORTAL ) { + continue; + } + //if this area has a cluster set + if ( ( *aasworld ).areasettings[areanum].cluster ) { + if ( !AAS_FloodClusterAreas_r( i, clusternum ) ) { + return qfalse; + } + i = 0; + break; + } //end if + } //end for + } //end for + return qtrue; +} //end of the function AAS_FloodClusterAreasUsingReachabilities +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_NumberClusterPortals( int clusternum ) { + int i, portalnum; + aas_cluster_t *cluster; + aas_portal_t *portal; + + cluster = &( *aasworld ).clusters[clusternum]; + for ( i = 0; i < cluster->numportals; i++ ) + { + portalnum = ( *aasworld ).portalindex[cluster->firstportal + i]; + portal = &( *aasworld ).portals[portalnum]; + if ( portal->frontcluster == clusternum ) { + portal->clusterareanum[0] = cluster->numareas++; + } //end if + else + { + portal->clusterareanum[1] = cluster->numareas++; + } //end else + } //end for +} //end of the function AAS_NumberClusterPortals +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_NumberClusterAreas( int clusternum ) { + int i, portalnum; + aas_cluster_t *cluster; + aas_portal_t *portal; + + ( *aasworld ).clusters[clusternum].numareas = 0; + ( *aasworld ).clusters[clusternum].numreachabilityareas = 0; + //number all areas in this cluster WITH reachabilities + for ( i = 1; i < ( *aasworld ).numareas; i++ ) + { + // + if ( ( *aasworld ).areasettings[i].cluster != clusternum ) { + continue; + } + // + if ( !AAS_AreaReachability( i ) ) { + continue; + } + // + ( *aasworld ).areasettings[i].clusterareanum = ( *aasworld ).clusters[clusternum].numareas; + //the cluster has an extra area + ( *aasworld ).clusters[clusternum].numareas++; + ( *aasworld ).clusters[clusternum].numreachabilityareas++; + } //end for + //number all portals in this cluster WITH reachabilities + cluster = &( *aasworld ).clusters[clusternum]; + for ( i = 0; i < cluster->numportals; i++ ) + { + portalnum = ( *aasworld ).portalindex[cluster->firstportal + i]; + portal = &( *aasworld ).portals[portalnum]; + if ( !AAS_AreaReachability( portal->areanum ) ) { + continue; + } + if ( portal->frontcluster == clusternum ) { + portal->clusterareanum[0] = cluster->numareas++; + ( *aasworld ).clusters[clusternum].numreachabilityareas++; + } //end if + else + { + portal->clusterareanum[1] = cluster->numareas++; + ( *aasworld ).clusters[clusternum].numreachabilityareas++; + } //end else + } //end for + //number all areas in this cluster WITHOUT reachabilities + for ( i = 1; i < ( *aasworld ).numareas; i++ ) + { + // + if ( ( *aasworld ).areasettings[i].cluster != clusternum ) { + continue; + } + // + if ( AAS_AreaReachability( i ) ) { + continue; + } + // + ( *aasworld ).areasettings[i].clusterareanum = ( *aasworld ).clusters[clusternum].numareas; + //the cluster has an extra area + ( *aasworld ).clusters[clusternum].numareas++; + } //end for + //number all portals in this cluster WITHOUT reachabilities + cluster = &( *aasworld ).clusters[clusternum]; + for ( i = 0; i < cluster->numportals; i++ ) + { + portalnum = ( *aasworld ).portalindex[cluster->firstportal + i]; + portal = &( *aasworld ).portals[portalnum]; + if ( AAS_AreaReachability( portal->areanum ) ) { + continue; + } + if ( portal->frontcluster == clusternum ) { + portal->clusterareanum[0] = cluster->numareas++; + } //end if + else + { + portal->clusterareanum[1] = cluster->numareas++; + } //end else + } //end for +} //end of the function AAS_NumberClusterAreas +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_FindClusters( void ) { + int i; + aas_cluster_t *cluster; + + AAS_RemoveClusterAreas(); + // + for ( i = 1; i < ( *aasworld ).numareas; i++ ) + { + //if the area is already part of a cluster + if ( ( *aasworld ).areasettings[i].cluster ) { + continue; + } + // if not flooding through faces only use areas that have reachabilities + if ( nofaceflood ) { + if ( !( *aasworld ).areasettings[i].numreachableareas ) { + continue; + } + } //end if + //if the area is a cluster portal + if ( ( *aasworld ).areasettings[i].contents & AREACONTENTS_CLUSTERPORTAL ) { + continue; + } + if ( ( *aasworld ).numclusters >= AAS_MAX_CLUSTERS ) { + AAS_Error( "AAS_MAX_CLUSTERS" ); + return qfalse; + } //end if + cluster = &( *aasworld ).clusters[( *aasworld ).numclusters]; + cluster->numareas = 0; + cluster->numreachabilityareas = 0; + cluster->firstportal = ( *aasworld ).portalindexsize; + cluster->numportals = 0; + //flood the areas in this cluster + if ( !AAS_FloodClusterAreas_r( i, ( *aasworld ).numclusters ) ) { + return qfalse; + } + if ( !AAS_FloodClusterAreasUsingReachabilities( ( *aasworld ).numclusters ) ) { + return qfalse; + } + //number the cluster areas + //AAS_NumberClusterPortals((*aasworld).numclusters); + AAS_NumberClusterAreas( ( *aasworld ).numclusters ); + //Log_Write("cluster %d has %d areas\r\n", (*aasworld).numclusters, cluster->numareas); + ( *aasworld ).numclusters++; + } //end for + return qtrue; +} //end of the function AAS_FindClusters +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_CreatePortals( void ) { + int i; + aas_portal_t *portal; + + for ( i = 1; i < ( *aasworld ).numareas; i++ ) + { + //if the area is a cluster portal + if ( ( *aasworld ).areasettings[i].contents & AREACONTENTS_CLUSTERPORTAL ) { + if ( ( *aasworld ).numportals >= AAS_MAX_PORTALS ) { + AAS_Error( "AAS_MAX_PORTALS" ); + return; + } //end if + portal = &( *aasworld ).portals[( *aasworld ).numportals]; + portal->areanum = i; + portal->frontcluster = 0; + portal->backcluster = 0; + Log_Write( "portal %d: area %d\r\n", ( *aasworld ).numportals, portal->areanum ); + ( *aasworld ).numportals++; + } //end if + } //end for +} //end of the function AAS_CreatePortals +/* +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_MapContainsTeleporters(void) +{ + bsp_entity_t *entities, *ent; + char *classname; + + entities = AAS_ParseBSPEntities(); + + for (ent = entities; ent; ent = ent->next) + { + classname = AAS_ValueForBSPEpairKey(ent, "classname"); + if (classname && !strcmp(classname, "misc_teleporter")) + { + AAS_FreeBSPEntities(entities); + return qtrue; + } //end if + } //end for + return qfalse; +} //end of the function AAS_MapContainsTeleporters +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_NonConvexFaces(aas_face_t *face1, aas_face_t *face2, int side1, int side2) +{ + int i, j, edgenum; + aas_plane_t *plane1, *plane2; + aas_edge_t *edge; + + + plane1 = &(*aasworld).planes[face1->planenum ^ side1]; + plane2 = &(*aasworld).planes[face2->planenum ^ side2]; + + //check if one of the points of face1 is at the back of the plane of face2 + for (i = 0; i < face1->numedges; i++) + { + edgenum = abs((*aasworld).edgeindex[face1->firstedge + i]); + edge = &(*aasworld).edges[edgenum]; + for (j = 0; j < 2; j++) + { + if (DotProduct(plane2->normal, (*aasworld).vertexes[edge->v[j]]) - + plane2->dist < -0.01) return qtrue; + } //end for + } //end for + for (i = 0; i < face2->numedges; i++) + { + edgenum = abs((*aasworld).edgeindex[face2->firstedge + i]); + edge = &(*aasworld).edges[edgenum]; + for (j = 0; j < 2; j++) + { + if (DotProduct(plane1->normal, (*aasworld).vertexes[edge->v[j]]) - + plane1->dist < -0.01) return qtrue; + } //end for + } //end for + + return qfalse; +} //end of the function AAS_NonConvexFaces +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_CanMergeAreas(int *areanums, int numareas) +{ + int i, j, s, face1num, face2num, side1, side2, fn1, fn2; + aas_face_t *face1, *face2; + aas_area_t *area1, *area2; + + for (i = 0; i < numareas; i++) + { + area1 = &(*aasworld).areas[areanums[i]]; + for (fn1 = 0; fn1 < area1->numfaces; fn1++) + { + face1num = abs((*aasworld).faceindex[area1->firstface + fn1]); + face1 = &(*aasworld).faces[face1num]; + side1 = face1->frontarea != areanums[i]; + //check if the face isn't a shared one with one of the other areas + for (s = 0; s < numareas; s++) + { + if (s == i) continue; + if (face1->frontarea == s || face1->backarea == s) break; + } //end for + //if the face was a shared one + if (s != numareas) continue; + // + for (j = 0; j < numareas; j++) + { + if (j == i) continue; + area2 = &(*aasworld).areas[areanums[j]]; + for (fn2 = 0; fn2 < area2->numfaces; fn2++) + { + face2num = abs((*aasworld).faceindex[area2->firstface + fn2]); + face2 = &(*aasworld).faces[face2num]; + side2 = face2->frontarea != areanums[j]; + //check if the face isn't a shared one with one of the other areas + for (s = 0; s < numareas; s++) + { + if (s == j) continue; + if (face2->frontarea == s || face2->backarea == s) break; + } //end for + //if the face was a shared one + if (s != numareas) continue; + // + if (AAS_NonConvexFaces(face1, face2, side1, side2)) return qfalse; + } //end for + } //end for + } //end for + } //end for + return qtrue; +} //end of the function AAS_CanMergeAreas +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_NonConvexEdges(aas_edge_t *edge1, aas_edge_t *edge2, int side1, int side2, int planenum) +{ + int i; + vec3_t edgevec1, edgevec2, normal1, normal2; + float dist1, dist2; + aas_plane_t *plane; + + plane = &(*aasworld).planes[planenum]; + VectorSubtract((*aasworld).vertexes[edge1->v[1]], (*aasworld).vertexes[edge1->v[0]], edgevec1); + VectorSubtract((*aasworld).vertexes[edge2->v[1]], (*aasworld).vertexes[edge2->v[0]], edgevec2); + if (side1) VectorInverse(edgevec1); + if (side2) VectorInverse(edgevec2); + // + CrossProduct(edgevec1, plane->normal, normal1); + dist1 = DotProduct(normal1, (*aasworld).vertexes[edge1->v[0]]); + CrossProduct(edgevec2, plane->normal, normal2); + dist2 = DotProduct(normal2, (*aasworld).vertexes[edge2->v[0]]); + + for (i = 0; i < 2; i++) + { + if (DotProduct((*aasworld).vertexes[edge1->v[i]], normal2) - dist2 < -0.01) return qfalse; + } //end for + for (i = 0; i < 2; i++) + { + if (DotProduct((*aasworld).vertexes[edge2->v[i]], normal1) - dist1 < -0.01) return qfalse; + } //end for + return qtrue; +} //end of the function AAS_NonConvexEdges +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_CanMergeFaces(int *facenums, int numfaces, int planenum) +{ + int i, j, s, edgenum1, edgenum2, side1, side2, en1, en2, ens; + aas_face_t *face1, *face2, *otherface; + aas_edge_t *edge1, *edge2; + + for (i = 0; i < numfaces; i++) + { + face1 = &(*aasworld).faces[facenums[i]]; + for (en1 = 0; en1 < face1->numedges; en1++) + { + edgenum1 = (*aasworld).edgeindex[face1->firstedge + en1]; + side1 = (edgenum1 < 0) ^ (face1->planenum != planenum); + edgenum1 = abs(edgenum1); + edge1 = &(*aasworld).edges[edgenum1]; + //check if the edge is shared with another face + for (s = 0; s < numfaces; s++) + { + if (s == i) continue; + otherface = &(*aasworld).faces[facenums[s]]; + for (ens = 0; ens < otherface->numedges; ens++) + { + if (edgenum1 == abs((*aasworld).edgeindex[otherface->firstedge + ens])) break; + } //end for + if (ens != otherface->numedges) break; + } //end for + //if the edge was shared + if (s != numfaces) continue; + // + for (j = 0; j < numfaces; j++) + { + if (j == i) continue; + face2 = &(*aasworld).faces[facenums[j]]; + for (en2 = 0; en2 < face2->numedges; en2++) + { + edgenum2 = (*aasworld).edgeindex[face2->firstedge + en2]; + side2 = (edgenum2 < 0) ^ (face2->planenum != planenum); + edgenum2 = abs(edgenum2); + edge2 = &(*aasworld).edges[edgenum2]; + //check if the edge is shared with another face + for (s = 0; s < numfaces; s++) + { + if (s == i) continue; + otherface = &(*aasworld).faces[facenums[s]]; + for (ens = 0; ens < otherface->numedges; ens++) + { + if (edgenum2 == abs((*aasworld).edgeindex[otherface->firstedge + ens])) break; + } //end for + if (ens != otherface->numedges) break; + } //end for + //if the edge was shared + if (s != numfaces) continue; + // + if (AAS_NonConvexEdges(edge1, edge2, side1, side2, planenum)) return qfalse; + } //end for + } //end for + } //end for + } //end for + return qtrue; +} //end of the function AAS_CanMergeFaces*/ +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ConnectedAreas_r( int *areanums, int numareas, int *connectedareas, int curarea ) { + int i, j, otherareanum, facenum; + aas_area_t *area; + aas_face_t *face; + + connectedareas[curarea] = qtrue; + area = &( *aasworld ).areas[areanums[curarea]]; + for ( i = 0; i < area->numfaces; i++ ) + { + facenum = abs( ( *aasworld ).faceindex[area->firstface + i] ); + face = &( *aasworld ).faces[facenum]; + //if the face is solid + if ( face->faceflags & FACE_SOLID ) { + continue; + } + //get the area at the other side of the face + if ( face->frontarea != areanums[curarea] ) { + otherareanum = face->frontarea; + } else { otherareanum = face->backarea;} + //check if the face is leading to one of the other areas + for ( j = 0; j < numareas; j++ ) + { + if ( areanums[j] == otherareanum ) { + break; + } + } //end for + //if the face isn't leading to one of the other areas + if ( j == numareas ) { + continue; + } + //if the other area is already connected + if ( connectedareas[j] ) { + continue; + } + //recursively proceed with the other area + AAS_ConnectedAreas_r( areanums, numareas, connectedareas, j ); + } //end for +} //end of the function AAS_ConnectedAreas_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_ConnectedAreas( int *areanums, int numareas ) { + int connectedareas[MAX_PORTALAREAS], i; + + memset( connectedareas, 0, sizeof( connectedareas ) ); + if ( numareas < 1 ) { + return qfalse; + } + if ( numareas == 1 ) { + return qtrue; + } + AAS_ConnectedAreas_r( areanums, numareas, connectedareas, 0 ); + for ( i = 0; i < numareas; i++ ) + { + if ( !connectedareas[i] ) { + return qfalse; + } + } //end for + return qtrue; +} //end of the function AAS_ConnectedAreas +//=========================================================================== +// gets adjacent areas with less presence types recursively +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_GetAdjacentAreasWithLessPresenceTypes_r( int *areanums, int numareas, int curareanum ) { + int i, j, presencetype, otherpresencetype, otherareanum, facenum; + aas_area_t *area; + aas_face_t *face; + + areanums[numareas++] = curareanum; + area = &( *aasworld ).areas[curareanum]; + presencetype = ( *aasworld ).areasettings[curareanum].presencetype; + for ( i = 0; i < area->numfaces; i++ ) + { + facenum = abs( ( *aasworld ).faceindex[area->firstface + i] ); + face = &( *aasworld ).faces[facenum]; + //if the face is solid + if ( face->faceflags & FACE_SOLID ) { + continue; + } + //the area at the other side of the face + if ( face->frontarea != curareanum ) { + otherareanum = face->frontarea; + } else { otherareanum = face->backarea;} + // + otherpresencetype = ( *aasworld ).areasettings[otherareanum].presencetype; + //if the other area has less presence types + if ( ( presencetype & ~otherpresencetype ) && + !( otherpresencetype & ~presencetype ) ) { + //check if the other area isn't already in the list + for ( j = 0; j < numareas; j++ ) + { + if ( otherareanum == areanums[j] ) { + break; + } + } //end for + //if the other area isn't already in the list + if ( j == numareas ) { + if ( numareas >= MAX_PORTALAREAS ) { + AAS_Error( "MAX_PORTALAREAS" ); + return numareas; + } //end if + numareas = AAS_GetAdjacentAreasWithLessPresenceTypes_r( areanums, numareas, otherareanum ); + } //end if + } //end if + } //end for + return numareas; +} //end of the function AAS_GetAdjacentAreasWithLessPresenceTypes_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_CheckAreaForPossiblePortals( int areanum ) { + int i, j, k, fen, ben, frontedgenum, backedgenum, facenum; + int areanums[MAX_PORTALAREAS], numareas, otherareanum; + int numareafrontfaces[MAX_PORTALAREAS], numareabackfaces[MAX_PORTALAREAS]; + int frontfacenums[MAX_PORTALAREAS], backfacenums[MAX_PORTALAREAS]; + int numfrontfaces, numbackfaces; + int frontareanums[MAX_PORTALAREAS], backareanums[MAX_PORTALAREAS]; + int numfrontareas, numbackareas; + int frontplanenum, backplanenum, faceplanenum; + aas_area_t *area; + aas_face_t *frontface, *backface, *face; + + //if it isn't already a portal + if ( ( *aasworld ).areasettings[areanum].contents & AREACONTENTS_CLUSTERPORTAL ) { + return 0; + } + //it must be a grounded area + if ( !( ( *aasworld ).areasettings[areanum].areaflags & AREA_GROUNDED ) ) { + return 0; + } + // + memset( numareafrontfaces, 0, sizeof( numareafrontfaces ) ); + memset( numareabackfaces, 0, sizeof( numareabackfaces ) ); + numareas = numfrontfaces = numbackfaces = 0; + numfrontareas = numbackareas = 0; + frontplanenum = backplanenum = -1; + //add any adjacent areas with less presence types + numareas = AAS_GetAdjacentAreasWithLessPresenceTypes_r( areanums, 0, areanum ); + // + for ( i = 0; i < numareas; i++ ) + { + area = &( *aasworld ).areas[areanums[i]]; + for ( j = 0; j < area->numfaces; j++ ) + { + facenum = abs( ( *aasworld ).faceindex[area->firstface + j] ); + face = &( *aasworld ).faces[facenum]; + //if the face is solid + if ( face->faceflags & FACE_SOLID ) { + continue; + } + //check if the face is shared with one of the other areas + for ( k = 0; k < numareas; k++ ) + { + if ( k == i ) { + continue; + } + if ( face->frontarea == areanums[k] || face->backarea == areanums[k] ) { + break; + } + } //end for + //if the face is shared + if ( k != numareas ) { + continue; + } + //the number of the area at the other side of the face + if ( face->frontarea == areanums[i] ) { + otherareanum = face->backarea; + } else { otherareanum = face->frontarea;} + //if the other area already is a cluter portal + if ( ( *aasworld ).areasettings[otherareanum].contents & AREACONTENTS_CLUSTERPORTAL ) { + return 0; + } + //number of the plane of the area + faceplanenum = face->planenum & ~1; + // + if ( frontplanenum < 0 || faceplanenum == frontplanenum ) { + frontplanenum = faceplanenum; + frontfacenums[numfrontfaces++] = facenum; + for ( k = 0; k < numfrontareas; k++ ) + { + if ( frontareanums[k] == otherareanum ) { + break; + } + } //end for + if ( k == numfrontareas ) { + frontareanums[numfrontareas++] = otherareanum; + } + numareafrontfaces[i]++; + } //end if + else if ( backplanenum < 0 || faceplanenum == backplanenum ) { + backplanenum = faceplanenum; + backfacenums[numbackfaces++] = facenum; + for ( k = 0; k < numbackareas; k++ ) + { + if ( backareanums[k] == otherareanum ) { + break; + } + } //end for + if ( k == numbackareas ) { + backareanums[numbackareas++] = otherareanum; + } + numareabackfaces[i]++; + } //end else + else + { + return 0; + } //end else + } //end for + } //end for + //every area should have at least one front face and one back face + for ( i = 0; i < numareas; i++ ) + { + if ( !numareafrontfaces[i] || !numareabackfaces[i] ) { + return 0; + } + } //end for + //the front areas should all be connected + if ( !AAS_ConnectedAreas( frontareanums, numfrontareas ) ) { + return 0; + } + //the back areas should all be connected + if ( !AAS_ConnectedAreas( backareanums, numbackareas ) ) { + return 0; + } + //none of the front faces should have a shared edge with a back face + for ( i = 0; i < numfrontfaces; i++ ) + { + frontface = &( *aasworld ).faces[frontfacenums[i]]; + for ( fen = 0; fen < frontface->numedges; fen++ ) + { + frontedgenum = abs( ( *aasworld ).edgeindex[frontface->firstedge + fen] ); + for ( j = 0; j < numbackfaces; j++ ) + { + backface = &( *aasworld ).faces[backfacenums[j]]; + for ( ben = 0; ben < backface->numedges; ben++ ) + { + backedgenum = abs( ( *aasworld ).edgeindex[backface->firstedge + ben] ); + if ( frontedgenum == backedgenum ) { + break; + } + } //end for + if ( ben != backface->numedges ) { + break; + } + } //end for + if ( j != numbackfaces ) { + break; + } + } //end for + if ( fen != frontface->numedges ) { + break; + } + } //end for + if ( i != numfrontfaces ) { + return 0; + } + //set the cluster portal contents + for ( i = 0; i < numareas; i++ ) + { + ( *aasworld ).areasettings[areanums[i]].contents |= AREACONTENTS_CLUSTERPORTAL; + //this area can be used as a route portal + ( *aasworld ).areasettings[areanums[i]].contents |= AREACONTENTS_ROUTEPORTAL; + Log_Write( "possible portal: %d\r\n", areanums[i] ); + } //end for + // + return numareas; +} //end of the function AAS_CheckAreaForPossiblePortals +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FindPossiblePortals( void ) { + int i, numpossibleportals; + + numpossibleportals = 0; + for ( i = 1; i < ( *aasworld ).numareas; i++ ) + { + numpossibleportals += AAS_CheckAreaForPossiblePortals( i ); + } //end for + botimport.Print( PRT_MESSAGE, "\r%6d possible portals\n", numpossibleportals ); +} //end of the function AAS_FindPossiblePortals +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_RemoveAllPortals( void ) { + int i; + + for ( i = 1; i < ( *aasworld ).numareas; i++ ) + { + ( *aasworld ).areasettings[i].contents &= ~AREACONTENTS_CLUSTERPORTAL; + } //end for +} //end of the function AAS_RemoveAllPortals +/* +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FloodCluster_r(int areanum, int clusternum) +{ + int i, otherareanum; + aas_face_t *face; + aas_area_t *area; + + //set cluster mark + (*aasworld).areasettings[areanum].cluster = clusternum; + //if the area is a portal + //if ((*aasworld).areasettings[areanum].contents & AREACONTENTS_CLUSTERPORTAL) return; + // + area = &(*aasworld).areas[areanum]; + //use area faces to flood into adjacent areas + for (i = 0; i < area->numfaces; i++) + { + face = &(*aasworld).faces[abs((*aasworld).faceindex[area->firstface + i])]; + // + if (face->frontarea != areanum) otherareanum = face->frontarea; + else otherareanum = face->backarea; + //if there's no area at the other side + if (!otherareanum) continue; + //if the area is a portal + if ((*aasworld).areasettings[otherareanum].contents & AREACONTENTS_CLUSTERPORTAL) continue; + //if the area is already marked + if ((*aasworld).areasettings[otherareanum].cluster) continue; + // + AAS_FloodCluster_r(otherareanum, clusternum); + } //end for + //use the reachabilities to flood into other areas + for (i = 0; i < (*aasworld).areasettings[areanum].numreachableareas; i++) + { + otherareanum = (*aasworld).reachability[ + (*aasworld).areasettings[areanum].firstreachablearea + i].areanum; + if (!otherareanum) + { + continue; + AAS_Error("reachability %d has zero area\n", (*aasworld).areasettings[areanum].firstreachablearea + i); + } //end if + //if the area is a portal + if ((*aasworld).areasettings[otherareanum].contents & AREACONTENTS_CLUSTERPORTAL) continue; + //if the area is already marked + if ((*aasworld).areasettings[otherareanum].cluster) continue; + // + AAS_FloodCluster_r(otherareanum, clusternum); + } //end for +} //end of the function AAS_FloodCluster_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_RemoveTeleporterPortals(void) +{ + int i, j, areanum; + + for (i = 1; i < (*aasworld).numareas; i++) + { + for (j = 0; j < (*aasworld).areasettings[i].numreachableareas; j++) + { + areanum = (*aasworld).reachability[(*aasworld).areasettings[i].firstreachablearea + j].areanum; + if ((*aasworld).reachability[(*aasworld).areasettings[i].firstreachablearea + j].traveltype == TRAVEL_TELEPORT) + { + (*aasworld).areasettings[i].contents &= ~AREACONTENTS_CLUSTERPORTAL; + (*aasworld).areasettings[areanum].contents &= ~AREACONTENTS_CLUSTERPORTAL; + break; + } //end if + } //end for + } //end for +} //end of the function AAS_RemoveTeleporterPortals +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FloodClusterReachabilities(int clusternum) +{ + int i, j, areanum; + + for (i = 1; i < (*aasworld).numareas; i++) + { + //if this area already has a cluster set + if ((*aasworld).areasettings[i].cluster) continue; + //if this area is a cluster portal + if ((*aasworld).areasettings[i].contents & AREACONTENTS_CLUSTERPORTAL) continue; + //loop over the reachable areas from this area + for (j = 0; j < (*aasworld).areasettings[i].numreachableareas; j++) + { + //the reachable area + areanum = (*aasworld).reachability[(*aasworld).areasettings[i].firstreachablearea + j].areanum; + //if this area is a cluster portal + if ((*aasworld).areasettings[areanum].contents & AREACONTENTS_CLUSTERPORTAL) continue; + //if this area has a cluster set + if ((*aasworld).areasettings[areanum].cluster == clusternum) + { + AAS_FloodCluster_r(i, clusternum); + i = 0; + break; + } //end if + } //end for + } //end for +} //end of the function AAS_FloodClusterReachabilities +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +*/ +/* +void AAS_RemoveNotClusterClosingPortals(void) +{ + int i, j, k, facenum, otherareanum, nonclosingportals; + aas_area_t *area; + aas_face_t *face; + + AAS_RemoveTeleporterPortals(); + // + nonclosingportals = 0; + for (i = 1; i < (*aasworld).numareas; i++) + { + if (!((*aasworld).areasettings[i].contents & AREACONTENTS_CLUSTERPORTAL)) continue; + //find a non-portal area adjacent to the portal area and flood + //the cluster from there + area = &(*aasworld).areas[i]; + for (j = 0; j < area->numfaces; j++) + { + facenum = abs((*aasworld).faceindex[area->firstface + j]); + face = &(*aasworld).faces[facenum]; + // + if (face->frontarea != i) otherareanum = face->frontarea; + else otherareanum = face->backarea; + // + if (!otherareanum) continue; + // + if ((*aasworld).areasettings[otherareanum].contents & AREACONTENTS_CLUSTERPORTAL) + { + continue; + } //end if + //reset all cluster fields + AAS_RemoveClusterAreas(); + // + AAS_FloodCluster_r(otherareanum, 1); + AAS_FloodClusterReachabilities(1); + //check if all adjacent non-portal areas have a cluster set + for (k = 0; k < area->numfaces; k++) + { + facenum = abs((*aasworld).faceindex[area->firstface + k]); + face = &(*aasworld).faces[facenum]; + // + if (face->frontarea != i) otherareanum = face->frontarea; + else otherareanum = face->backarea; + // + if (!otherareanum) continue; + // + if ((*aasworld).areasettings[otherareanum].contents & AREACONTENTS_CLUSTERPORTAL) + { + continue; + } //end if + // + if (!(*aasworld).areasettings[otherareanum].cluster) break; + } //end for + //if all adjacent non-portal areas have a cluster set then the portal + //didn't seal a cluster + if (k >= area->numfaces) + { + (*aasworld).areasettings[i].contents &= ~AREACONTENTS_CLUSTERPORTAL; + nonclosingportals++; + //recheck all the other portals again + i = 0; + break; + } //end if + } //end for + } //end for + botimport.Print(PRT_MESSAGE, "\r%6d non closing portals removed\n", nonclosingportals); +} //end of the function AAS_RemoveNotClusterClosingPortals*/ +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +/* +void AAS_RemoveNotClusterClosingPortals(void) +{ + int i, j, facenum, otherareanum, nonclosingportals, numseperatedclusters; + aas_area_t *area; + aas_face_t *face; + + AAS_RemoveTeleporterPortals(); + // + nonclosingportals = 0; + for (i = 1; i < (*aasworld).numareas; i++) + { + if (!((*aasworld).areasettings[i].contents & AREACONTENTS_CLUSTERPORTAL)) continue; + // + numseperatedclusters = 0; + //reset all cluster fields + AAS_RemoveClusterAreas(); + //find a non-portal area adjacent to the portal area and flood + //the cluster from there + area = &(*aasworld).areas[i]; + for (j = 0; j < area->numfaces; j++) + { + facenum = abs((*aasworld).faceindex[area->firstface + j]); + face = &(*aasworld).faces[facenum]; + // + if (face->frontarea != i) otherareanum = face->frontarea; + else otherareanum = face->backarea; + //if not solid at the other side of the face + if (!otherareanum) continue; + //don't flood into other portals + if ((*aasworld).areasettings[otherareanum].contents & AREACONTENTS_CLUSTERPORTAL) continue; + //if the area already has a cluster set + if ((*aasworld).areasettings[otherareanum].cluster) continue; + //another cluster is seperated by this portal + numseperatedclusters++; + //flood the cluster + AAS_FloodCluster_r(otherareanum, numseperatedclusters); + AAS_FloodClusterReachabilities(numseperatedclusters); + } //end for + //use the reachabilities to flood into other areas + for (j = 0; j < (*aasworld).areasettings[i].numreachableareas; j++) + { + otherareanum = (*aasworld).reachability[ + (*aasworld).areasettings[i].firstreachablearea + j].areanum; + //this should never be qtrue but we check anyway + if (!otherareanum) continue; + //don't flood into other portals + if ((*aasworld).areasettings[otherareanum].contents & AREACONTENTS_CLUSTERPORTAL) continue; + //if the area already has a cluster set + if ((*aasworld).areasettings[otherareanum].cluster) continue; + //another cluster is seperated by this portal + numseperatedclusters++; + //flood the cluster + AAS_FloodCluster_r(otherareanum, numseperatedclusters); + AAS_FloodClusterReachabilities(numseperatedclusters); + } //end for + //a portal must seperate no more and no less than 2 clusters + if (numseperatedclusters != 2) + { + (*aasworld).areasettings[i].contents &= ~AREACONTENTS_CLUSTERPORTAL; + nonclosingportals++; + //recheck all the other portals again + i = 0; + } //end if + } //end for + botimport.Print(PRT_MESSAGE, "\r%6d non closing portals removed\n", nonclosingportals); +} //end of the function AAS_RemoveNotClusterClosingPortals +*/ +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +/* +void AAS_AddTeleporterPortals(void) +{ + int j, area2num, facenum, otherareanum; + char *target, *targetname, *classname; + bsp_entity_t *entities, *ent, *dest; + vec3_t origin, destorigin, mins, maxs, end; + vec3_t bbmins, bbmaxs; + aas_area_t *area; + aas_face_t *face; + aas_trace_t trace; + aas_link_t *areas, *link; + + entities = AAS_ParseBSPEntities(); + + for (ent = entities; ent; ent = ent->next) + { + classname = AAS_ValueForBSPEpairKey(ent, "classname"); + if (classname && !strcmp(classname, "misc_teleporter")) + { + if (!AAS_VectorForBSPEpairKey(ent, "origin", origin)) + { + botimport.Print(PRT_ERROR, "teleporter (%s) without origin\n", target); + continue; + } //end if + // + target = AAS_ValueForBSPEpairKey(ent, "target"); + if (!target) + { + botimport.Print(PRT_ERROR, "teleporter (%s) without target\n", target); + continue; + } //end if + for (dest = entities; dest; dest = dest->next) + { + classname = AAS_ValueForBSPEpairKey(dest, "classname"); + if (classname && !strcmp(classname, "misc_teleporter_dest")) + { + targetname = AAS_ValueForBSPEpairKey(dest, "targetname"); + if (targetname && !strcmp(targetname, target)) + { + break; + } //end if + } //end if + } //end for + if (!dest) + { + botimport.Print(PRT_ERROR, "teleporter without destination (%s)\n", target); + continue; + } //end if + if (!AAS_VectorForBSPEpairKey(dest, "origin", destorigin)) + { + botimport.Print(PRT_ERROR, "teleporter destination (%s) without origin\n", target); + continue; + } //end if + destorigin[2] += 24; //just for q2e1m2, the dork has put the telepads in the ground + VectorCopy(destorigin, end); + end[2] -= 100; + trace = AAS_TraceClientBBox(destorigin, end, PRESENCE_CROUCH, -1); + if (trace.startsolid) + { + botimport.Print(PRT_ERROR, "teleporter destination (%s) in solid\n", target); + continue; + } //end if + VectorCopy(trace.endpos, destorigin); + area2num = AAS_PointAreaNum(destorigin); + //reset all cluster fields + for (j = 0; j < (*aasworld).numareas; j++) + { + (*aasworld).areasettings[j].cluster = 0; + } //end for + // + VectorSet(mins, -8, -8, 8); + VectorSet(maxs, 8, 8, 24); + // + AAS_PresenceTypeBoundingBox(PRESENCE_CROUCH, bbmins, bbmaxs); + // + VectorAdd(origin, mins, mins); + VectorAdd(origin, maxs, maxs); + //add bounding box size + VectorSubtract(mins, bbmaxs, mins); + VectorSubtract(maxs, bbmins, maxs); + //link an invalid (-1) entity + areas = AAS_AASLinkEntity(mins, maxs, -1); + // + for (link = areas; link; link = link->next_area) + { + if (!AAS_AreaGrounded(link->areanum)) continue; + //add the teleporter portal mark + (*aasworld).areasettings[link->areanum].contents |= AREACONTENTS_CLUSTERPORTAL | + AREACONTENTS_TELEPORTAL; + } //end for + // + for (link = areas; link; link = link->next_area) + { + if (!AAS_AreaGrounded(link->areanum)) continue; + //find a non-portal area adjacent to the portal area and flood + //the cluster from there + area = &(*aasworld).areas[link->areanum]; + for (j = 0; j < area->numfaces; j++) + { + facenum = abs((*aasworld).faceindex[area->firstface + j]); + face = &(*aasworld).faces[facenum]; + // + if (face->frontarea != link->areanum) otherareanum = face->frontarea; + else otherareanum = face->backarea; + // + if (!otherareanum) continue; + // + if ((*aasworld).areasettings[otherareanum].contents & AREACONTENTS_CLUSTERPORTAL) + { + continue; + } //end if + // + AAS_FloodCluster_r(otherareanum, 1); + } //end for + } //end for + //if the teleport destination IS in the same cluster + if ((*aasworld).areasettings[area2num].cluster) + { + for (link = areas; link; link = link->next_area) + { + if (!AAS_AreaGrounded(link->areanum)) continue; + //add the teleporter portal mark + (*aasworld).areasettings[link->areanum].contents &= ~(AREACONTENTS_CLUSTERPORTAL | + AREACONTENTS_TELEPORTAL); + } //end for + } //end if + } //end if + } //end for + AAS_FreeBSPEntities(entities); +} //end of the function AAS_AddTeleporterPortals*/ +/* +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_AddTeleporterPortals(void) +{ + int i, j, areanum; + + for (i = 1; i < (*aasworld).numareas; i++) + { + for (j = 0; j < (*aasworld).areasettings[i].numreachableareas; j++) + { + if ((*aasworld).reachability[(*aasworld).areasettings[i].firstreachablearea + j].traveltype != TRAVEL_TELEPORT) continue; + areanum = (*aasworld).reachability[(*aasworld).areasettings[i].firstreachablearea + j].areanum; + (*aasworld).areasettings[areanum].contents |= AREACONTENTS_CLUSTERPORTAL; + } //end for + } //end for +} //end of the function AAS_AddTeleporterPortals*/ +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_TestPortals( void ) { + int i; + aas_portal_t *portal; + + for ( i = 1; i < ( *aasworld ).numportals; i++ ) + { + portal = &( *aasworld ).portals[i]; + if ( !portal->frontcluster ) { + ( *aasworld ).areasettings[portal->areanum].contents &= ~AREACONTENTS_CLUSTERPORTAL; + Log_Write( "portal area %d has no front cluster\r\n", portal->areanum ); + return qfalse; + } //end if + if ( !portal->backcluster ) { + ( *aasworld ).areasettings[portal->areanum].contents &= ~AREACONTENTS_CLUSTERPORTAL; + Log_Write( "portal area %d has no back cluster\r\n", portal->areanum ); + return qfalse; + } //end if + } //end for + return qtrue; +} //end of the function +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_CountForcedClusterPortals( void ) { + int num, i; + + num = 0; + for ( i = 1; i < ( *aasworld ).numareas; i++ ) + { + if ( ( *aasworld ).areasettings[i].contents & AREACONTENTS_CLUSTERPORTAL ) { + num++; + } //end if + } //end for + botimport.Print( PRT_MESSAGE, "%6d forced portals\n", num ); +} //end of the function AAS_CountForcedClusterPortals +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_CreateViewPortals( void ) { + int i; + + for ( i = 1; i < ( *aasworld ).numareas; i++ ) + { + if ( ( *aasworld ).areasettings[i].contents & AREACONTENTS_CLUSTERPORTAL ) { + ( *aasworld ).areasettings[i].contents |= AREACONTENTS_VIEWPORTAL; + } //end if + } //end for +} //end of the function AAS_CreateViewPortals +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_SetViewPortalsAsClusterPortals( void ) { + int i; + + for ( i = 1; i < ( *aasworld ).numareas; i++ ) + { + if ( ( *aasworld ).areasettings[i].contents & AREACONTENTS_VIEWPORTAL ) { + ( *aasworld ).areasettings[i].contents |= AREACONTENTS_CLUSTERPORTAL; + } //end if + } //end for +} //end of the function AAS_SetViewPortalsAsClusterPortals +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_InitClustering( void ) { + int i, removedPortalAreas; + int n, total, numreachabilityareas; + + if ( !( *aasworld ).loaded ) { + return; + } + //if there are clusters + if ( ( *aasworld ).numclusters >= 1 ) { +#ifndef BSPC + //if clustering isn't forced + if ( !( (int)LibVarGetValue( "forceclustering" ) ) && + !( (int)LibVarGetValue( "forcereachability" ) ) ) { + return; + } +#else + return; +#endif + } //end if + // + AAS_CountForcedClusterPortals(); + //remove all the existing portals + //AAS_RemoveAllPortals(); + //remove all area cluster marks + AAS_RemoveClusterAreas(); + //find possible cluster portals + AAS_FindPossiblePortals(); + //craete portals to for the bot view + AAS_CreateViewPortals(); + //remove all portals that are not closing a cluster + //AAS_RemoveNotClusterClosingPortals(); + //initialize portal memory + if ( ( *aasworld ).portals ) { + FreeMemory( ( *aasworld ).portals ); + } + ( *aasworld ).portals = (aas_portal_t *) GetClearedMemory( AAS_MAX_PORTALS * sizeof( aas_portal_t ) ); + //initialize portal index memory + if ( ( *aasworld ).portalindex ) { + FreeMemory( ( *aasworld ).portalindex ); + } + ( *aasworld ).portalindex = (aas_portalindex_t *) GetClearedMemory( AAS_MAX_PORTALINDEXSIZE * sizeof( aas_portalindex_t ) ); + //initialize cluster memory + if ( ( *aasworld ).clusters ) { + FreeMemory( ( *aasworld ).clusters ); + } + ( *aasworld ).clusters = (aas_cluster_t *) GetClearedMemory( AAS_MAX_CLUSTERS * sizeof( aas_cluster_t ) ); + // + removedPortalAreas = 0; + botimport.Print( PRT_MESSAGE, "\r%6d removed portal areas", removedPortalAreas ); + while ( 1 ) + { + botimport.Print( PRT_MESSAGE, "\r%6d", removedPortalAreas ); + //initialize the number of portals and clusters + ( *aasworld ).numportals = 1; //portal 0 is a dummy + ( *aasworld ).portalindexsize = 0; + ( *aasworld ).numclusters = 1; //cluster 0 is a dummy + //create the portals from the portal areas + AAS_CreatePortals(); + // + removedPortalAreas++; + //find the clusters + if ( !AAS_FindClusters() ) { + continue; + } + //test the portals + if ( !AAS_TestPortals() ) { + continue; + } + // + break; + } //end while + botimport.Print( PRT_MESSAGE, "\n" ); + //the AAS file should be saved + ( *aasworld ).savefile = qtrue; + // report cluster info + botimport.Print( PRT_MESSAGE, "%6d portals created\n", ( *aasworld ).numportals ); + botimport.Print( PRT_MESSAGE, "%6d clusters created\n", ( *aasworld ).numclusters ); + for ( i = 1; i < ( *aasworld ).numclusters; i++ ) + { + botimport.Print( PRT_MESSAGE, "cluster %d has %d reachability areas\n", i, + ( *aasworld ).clusters[i].numreachabilityareas ); + } //end for + // report AAS file efficiency + numreachabilityareas = 0; + total = 0; + for ( i = 0; i < ( *aasworld ).numclusters; i++ ) { + n = ( *aasworld ).clusters[i].numreachabilityareas; + numreachabilityareas += n; + total += n * n; + } + total += numreachabilityareas * ( *aasworld ).numportals; + // + botimport.Print( PRT_MESSAGE, "%6i total reachability areas\n", numreachabilityareas ); + botimport.Print( PRT_MESSAGE, "%6i AAS memory/CPU usage (the lower the better)\n", total * 3 ); +} //end of the function AAS_InitClustering diff --git a/src/botlib/be_aas_cluster.h b/src/botlib/be_aas_cluster.h new file mode 100644 index 0000000..6121cf3 --- /dev/null +++ b/src/botlib/be_aas_cluster.h @@ -0,0 +1,43 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_cluster.h + * + * desc: AAS + * + * + *****************************************************************************/ + +#ifdef AASINTERN +//initialize the AAS clustering +void AAS_InitClustering( void ); +void AAS_SetViewPortalsAsClusterPortals( void ); +#endif //AASINTERN + diff --git a/src/botlib/be_aas_debug.c b/src/botlib/be_aas_debug.c new file mode 100644 index 0000000..5ecd252 --- /dev/null +++ b/src/botlib/be_aas_debug.c @@ -0,0 +1,692 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_debug.c + * + * desc: AAS debug code + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "l_memory.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "l_libvar.h" +#include "aasfile.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "be_interface.h" +#include "be_aas_funcs.h" +#include "be_aas_def.h" + +#define MAX_DEBUGLINES 1024 +#define MAX_DEBUGPOLYGONS 128 + +int debuglines[MAX_DEBUGLINES]; +int debuglinevisible[MAX_DEBUGLINES]; +int numdebuglines; + +static int debugpolygons[MAX_DEBUGPOLYGONS]; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ClearShownPolygons( void ) { + int i; +//* + for ( i = 0; i < MAX_DEBUGPOLYGONS; i++ ) + { + if ( debugpolygons[i] ) { + botimport.DebugPolygonDelete( debugpolygons[i] ); + } + debugpolygons[i] = 0; + } //end for +//*/ +/* + for (i = 0; i < MAX_DEBUGPOLYGONS; i++) + { + botimport.DebugPolygonDelete(i); + debugpolygons[i] = 0; + } //end for +*/ +} //end of the function AAS_ClearShownPolygons +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ShowPolygon( int color, int numpoints, vec3_t *points ) { + int i; + + for ( i = 0; i < MAX_DEBUGPOLYGONS; i++ ) + { + if ( !debugpolygons[i] ) { + debugpolygons[i] = botimport.DebugPolygonCreate( color, numpoints, points ); + break; + } //end if + } //end for +} //end of the function AAS_ShowPolygon +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ClearShownDebugLines( void ) { + int i; + + //make all lines invisible + for ( i = 0; i < MAX_DEBUGLINES; i++ ) + { + if ( debuglines[i] ) { + //botimport.DebugLineShow(debuglines[i], NULL, NULL, LINECOLOR_NONE); + botimport.DebugLineDelete( debuglines[i] ); + debuglines[i] = 0; + debuglinevisible[i] = qfalse; + } //end if + } //end for +} //end of the function AAS_ClearShownDebugLines +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_DebugLine( vec3_t start, vec3_t end, int color ) { + int line; + + for ( line = 0; line < MAX_DEBUGLINES; line++ ) + { + if ( !debuglines[line] ) { + debuglines[line] = botimport.DebugLineCreate(); + debuglinevisible[line] = qfalse; + numdebuglines++; + } //end if + if ( !debuglinevisible[line] ) { + botimport.DebugLineShow( debuglines[line], start, end, color ); + debuglinevisible[line] = qtrue; + return; + } //end else + } //end for +} //end of the function AAS_DebugLine +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_PermanentLine( vec3_t start, vec3_t end, int color ) { + int line; + + line = botimport.DebugLineCreate(); + botimport.DebugLineShow( line, start, end, color ); +} //end of the function AAS_PermenentLine +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_DrawPermanentCross( vec3_t origin, float size, int color ) { + int i, debugline; + vec3_t start, end; + + for ( i = 0; i < 3; i++ ) + { + VectorCopy( origin, start ); + start[i] += size; + VectorCopy( origin, end ); + end[i] -= size; + AAS_DebugLine( start, end, color ); + debugline = botimport.DebugLineCreate(); + botimport.DebugLineShow( debugline, start, end, color ); + } //end for +} //end of the function AAS_DrawPermanentCross +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_DrawPlaneCross( vec3_t point, vec3_t normal, float dist, int type, int color ) { + int n0, n1, n2, j, line, lines[2]; + vec3_t start1, end1, start2, end2; + + //make a cross in the hit plane at the hit point + VectorCopy( point, start1 ); + VectorCopy( point, end1 ); + VectorCopy( point, start2 ); + VectorCopy( point, end2 ); + + n0 = type % 3; + n1 = ( type + 1 ) % 3; + n2 = ( type + 2 ) % 3; + start1[n1] -= 6; + start1[n2] -= 6; + end1[n1] += 6; + end1[n2] += 6; + start2[n1] += 6; + start2[n2] -= 6; + end2[n1] -= 6; + end2[n2] += 6; + + start1[n0] = ( dist - ( start1[n1] * normal[n1] + + start1[n2] * normal[n2] ) ) / normal[n0]; + end1[n0] = ( dist - ( end1[n1] * normal[n1] + + end1[n2] * normal[n2] ) ) / normal[n0]; + start2[n0] = ( dist - ( start2[n1] * normal[n1] + + start2[n2] * normal[n2] ) ) / normal[n0]; + end2[n0] = ( dist - ( end2[n1] * normal[n1] + + end2[n2] * normal[n2] ) ) / normal[n0]; + + for ( j = 0, line = 0; j < 2 && line < MAX_DEBUGLINES; line++ ) + { + if ( !debuglines[line] ) { + debuglines[line] = botimport.DebugLineCreate(); + lines[j++] = debuglines[line]; + debuglinevisible[line] = qtrue; + numdebuglines++; + } //end if + else if ( !debuglinevisible[line] ) { + lines[j++] = debuglines[line]; + debuglinevisible[line] = qtrue; + } //end else + } //end for + botimport.DebugLineShow( lines[0], start1, end1, color ); + botimport.DebugLineShow( lines[1], start2, end2, color ); +} //end of the function AAS_DrawPlaneCross +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ShowBoundingBox( vec3_t origin, vec3_t mins, vec3_t maxs ) { + vec3_t bboxcorners[8]; + int lines[3]; + int i, j, line; + + //upper corners + bboxcorners[0][0] = origin[0] + maxs[0]; + bboxcorners[0][1] = origin[1] + maxs[1]; + bboxcorners[0][2] = origin[2] + maxs[2]; + // + bboxcorners[1][0] = origin[0] + mins[0]; + bboxcorners[1][1] = origin[1] + maxs[1]; + bboxcorners[1][2] = origin[2] + maxs[2]; + // + bboxcorners[2][0] = origin[0] + mins[0]; + bboxcorners[2][1] = origin[1] + mins[1]; + bboxcorners[2][2] = origin[2] + maxs[2]; + // + bboxcorners[3][0] = origin[0] + maxs[0]; + bboxcorners[3][1] = origin[1] + mins[1]; + bboxcorners[3][2] = origin[2] + maxs[2]; + //lower corners + memcpy( bboxcorners[4], bboxcorners[0], sizeof( vec3_t ) * 4 ); + for ( i = 0; i < 4; i++ ) bboxcorners[4 + i][2] = origin[2] + mins[2]; + //draw bounding box + for ( i = 0; i < 4; i++ ) + { + for ( j = 0, line = 0; j < 3 && line < MAX_DEBUGLINES; line++ ) + { + if ( !debuglines[line] ) { + debuglines[line] = botimport.DebugLineCreate(); + lines[j++] = debuglines[line]; + debuglinevisible[line] = qtrue; + numdebuglines++; + } //end if + else if ( !debuglinevisible[line] ) { + lines[j++] = debuglines[line]; + debuglinevisible[line] = qtrue; + } //end else + } //end for + //top plane + botimport.DebugLineShow( lines[0], bboxcorners[i], + bboxcorners[( i + 1 ) & 3], LINECOLOR_RED ); + //bottom plane + botimport.DebugLineShow( lines[1], bboxcorners[4 + i], + bboxcorners[4 + ( ( i + 1 ) & 3 )], LINECOLOR_RED ); + //vertical lines + botimport.DebugLineShow( lines[2], bboxcorners[i], + bboxcorners[4 + i], LINECOLOR_RED ); + } //end for +} //end of the function AAS_ShowBoundingBox +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ShowFace( int facenum ) { + int i, color, edgenum; + aas_edge_t *edge; + aas_face_t *face; + aas_plane_t *plane; + vec3_t start, end; + + color = LINECOLOR_YELLOW; + //check if face number is in range + if ( facenum >= ( *aasworld ).numfaces ) { + botimport.Print( PRT_ERROR, "facenum %d out of range\n", facenum ); + } //end if + face = &( *aasworld ).faces[facenum]; + //walk through the edges of the face + for ( i = 0; i < face->numedges; i++ ) + { + //edge number + edgenum = abs( ( *aasworld ).edgeindex[face->firstedge + i] ); + //check if edge number is in range + if ( edgenum >= ( *aasworld ).numedges ) { + botimport.Print( PRT_ERROR, "edgenum %d out of range\n", edgenum ); + } //end if + edge = &( *aasworld ).edges[edgenum]; + if ( color == LINECOLOR_RED ) { + color = LINECOLOR_GREEN; + } else if ( color == LINECOLOR_GREEN ) { + color = LINECOLOR_BLUE; + } else if ( color == LINECOLOR_BLUE ) { + color = LINECOLOR_YELLOW; + } else { color = LINECOLOR_RED;} + AAS_DebugLine( ( *aasworld ).vertexes[edge->v[0]], + ( *aasworld ).vertexes[edge->v[1]], + color ); + } //end for + plane = &( *aasworld ).planes[face->planenum]; + edgenum = abs( ( *aasworld ).edgeindex[face->firstedge] ); + edge = &( *aasworld ).edges[edgenum]; + VectorCopy( ( *aasworld ).vertexes[edge->v[0]], start ); + VectorMA( start, 20, plane->normal, end ); + AAS_DebugLine( start, end, LINECOLOR_RED ); +} //end of the function AAS_ShowFace +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ShowFacePolygon( int facenum, int color, int flip ) { + int i, edgenum, numpoints; + vec3_t points[128]; + aas_edge_t *edge; + aas_face_t *face; + + //check if face number is in range + if ( facenum >= ( *aasworld ).numfaces ) { + botimport.Print( PRT_ERROR, "facenum %d out of range\n", facenum ); + } //end if + face = &( *aasworld ).faces[facenum]; + //walk through the edges of the face + numpoints = 0; + if ( flip ) { + for ( i = face->numedges - 1; i >= 0; i-- ) + { + //edge number + edgenum = ( *aasworld ).edgeindex[face->firstedge + i]; + edge = &( *aasworld ).edges[abs( edgenum )]; + VectorCopy( ( *aasworld ).vertexes[edge->v[edgenum < 0]], points[numpoints] ); + numpoints++; + } //end for + } //end if + else + { + for ( i = 0; i < face->numedges; i++ ) + { + //edge number + edgenum = ( *aasworld ).edgeindex[face->firstedge + i]; + edge = &( *aasworld ).edges[abs( edgenum )]; + VectorCopy( ( *aasworld ).vertexes[edge->v[edgenum < 0]], points[numpoints] ); + numpoints++; + } //end for + } //end else + AAS_ShowPolygon( color, numpoints, points ); +} //end of the function AAS_ShowFacePolygon +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ShowArea( int areanum, int groundfacesonly ) { + int areaedges[MAX_DEBUGLINES]; + int numareaedges, i, j, n, color = 0, line; + int facenum, edgenum; + aas_area_t *area; + aas_face_t *face; + aas_edge_t *edge; + + // + numareaedges = 0; + // + if ( areanum < 0 || areanum >= ( *aasworld ).numareas ) { + botimport.Print( PRT_ERROR, "area %d out of range [0, %d]\n", + areanum, ( *aasworld ).numareas ); + return; + } //end if + //pointer to the convex area + area = &( *aasworld ).areas[areanum]; + //walk through the faces of the area + for ( i = 0; i < area->numfaces; i++ ) + { + facenum = abs( ( *aasworld ).faceindex[area->firstface + i] ); + //check if face number is in range + if ( facenum >= ( *aasworld ).numfaces ) { + botimport.Print( PRT_ERROR, "facenum %d out of range\n", facenum ); + } //end if + face = &( *aasworld ).faces[facenum]; + //ground faces only + if ( groundfacesonly ) { + if ( !( face->faceflags & ( FACE_GROUND | FACE_LADDER ) ) ) { + continue; + } + } //end if + //walk through the edges of the face + for ( j = 0; j < face->numedges; j++ ) + { + //edge number + edgenum = abs( ( *aasworld ).edgeindex[face->firstedge + j] ); + //check if edge number is in range + if ( edgenum >= ( *aasworld ).numedges ) { + botimport.Print( PRT_ERROR, "edgenum %d out of range\n", edgenum ); + } //end if + //check if the edge is stored already + for ( n = 0; n < numareaedges; n++ ) + { + if ( areaedges[n] == edgenum ) { + break; + } + } //end for + if ( n == numareaedges && numareaedges < MAX_DEBUGLINES ) { + areaedges[numareaedges++] = edgenum; + } //end if + } //end for + //AAS_ShowFace(facenum); + } //end for + //draw all the edges + for ( n = 0; n < numareaedges; n++ ) + { + for ( line = 0; line < MAX_DEBUGLINES; line++ ) + { + if ( !debuglines[line] ) { + debuglines[line] = botimport.DebugLineCreate(); + debuglinevisible[line] = qfalse; + numdebuglines++; + } //end if + if ( !debuglinevisible[line] ) { + break; + } //end else + } //end for + if ( line >= MAX_DEBUGLINES ) { + return; + } + edge = &( *aasworld ).edges[areaedges[n]]; + if ( color == LINECOLOR_RED ) { + color = LINECOLOR_BLUE; + } else if ( color == LINECOLOR_BLUE ) { + color = LINECOLOR_GREEN; + } else if ( color == LINECOLOR_GREEN ) { + color = LINECOLOR_YELLOW; + } else { color = LINECOLOR_RED;} + botimport.DebugLineShow( debuglines[line], + ( *aasworld ).vertexes[edge->v[0]], + ( *aasworld ).vertexes[edge->v[1]], + color ); + debuglinevisible[line] = qtrue; + } //end for*/ +} //end of the function AAS_ShowArea +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ShowAreaPolygons( int areanum, int color, int groundfacesonly ) { + int i, facenum; + aas_area_t *area; + aas_face_t *face; + + // + if ( areanum < 0 || areanum >= ( *aasworld ).numareas ) { + botimport.Print( PRT_ERROR, "area %d out of range [0, %d]\n", + areanum, ( *aasworld ).numareas ); + return; + } //end if + //pointer to the convex area + area = &( *aasworld ).areas[areanum]; + //walk through the faces of the area + for ( i = 0; i < area->numfaces; i++ ) + { + facenum = abs( ( *aasworld ).faceindex[area->firstface + i] ); + //check if face number is in range + if ( facenum >= ( *aasworld ).numfaces ) { + botimport.Print( PRT_ERROR, "facenum %d out of range\n", facenum ); + } //end if + face = &( *aasworld ).faces[facenum]; + //ground faces only + if ( groundfacesonly ) { + if ( !( face->faceflags & ( FACE_GROUND | FACE_LADDER ) ) ) { + continue; + } + } //end if + AAS_ShowFacePolygon( facenum, color, face->frontarea != areanum ); + } //end for +} //end of the function AAS_ShowAreaPolygons +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_DrawCross( vec3_t origin, float size, int color ) { + int i; + vec3_t start, end; + + for ( i = 0; i < 3; i++ ) + { + VectorCopy( origin, start ); + start[i] += size; + VectorCopy( origin, end ); + end[i] -= size; + AAS_DebugLine( start, end, color ); + } //end for +} //end of the function AAS_DrawCross +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_PrintTravelType( int traveltype ) { +#ifdef DEBUG + char *str; + // + switch ( traveltype ) + { + case TRAVEL_INVALID: str = "TRAVEL_INVALID"; break; + case TRAVEL_WALK: str = "TRAVEL_WALK"; break; + case TRAVEL_CROUCH: str = "TRAVEL_CROUCH"; break; + case TRAVEL_BARRIERJUMP: str = "TRAVEL_BARRIERJUMP"; break; + case TRAVEL_JUMP: str = "TRAVEL_JUMP"; break; + case TRAVEL_LADDER: str = "TRAVEL_LADDER"; break; + case TRAVEL_WALKOFFLEDGE: str = "TRAVEL_WALKOFFLEDGE"; break; + case TRAVEL_SWIM: str = "TRAVEL_SWIM"; break; + case TRAVEL_WATERJUMP: str = "TRAVEL_WATERJUMP"; break; + case TRAVEL_TELEPORT: str = "TRAVEL_TELEPORT"; break; + case TRAVEL_ELEVATOR: str = "TRAVEL_ELEVATOR"; break; + case TRAVEL_ROCKETJUMP: str = "TRAVEL_ROCKETJUMP"; break; + case TRAVEL_BFGJUMP: str = "TRAVEL_BFGJUMP"; break; + case TRAVEL_GRAPPLEHOOK: str = "TRAVEL_GRAPPLEHOOK"; break; + case TRAVEL_JUMPPAD: str = "TRAVEL_JUMPPAD"; break; + case TRAVEL_FUNCBOB: str = "TRAVEL_FUNCBOB"; break; + default: str = "UNKNOWN TRAVEL TYPE"; break; + } //end switch + botimport.Print( PRT_MESSAGE, "%s", str ); +#endif //DEBUG +} //end of the function AAS_PrintTravelType +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_DrawArrow( vec3_t start, vec3_t end, int linecolor, int arrowcolor ) { + vec3_t dir, cross, p1, p2, up = {0, 0, 1}; + float dot; + + VectorSubtract( end, start, dir ); + VectorNormalize( dir ); + dot = DotProduct( dir, up ); + if ( dot > 0.99 || dot < -0.99 ) { + VectorSet( cross, 1, 0, 0 ); + } else { CrossProduct( dir, up, cross );} + + VectorMA( end, -6, dir, p1 ); + VectorCopy( p1, p2 ); + VectorMA( p1, 6, cross, p1 ); + VectorMA( p2, -6, cross, p2 ); + + AAS_DebugLine( start, end, linecolor ); + AAS_DebugLine( p1, end, arrowcolor ); + AAS_DebugLine( p2, end, arrowcolor ); +} //end of the function AAS_DrawArrow +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ShowReachability( aas_reachability_t *reach ) { + vec3_t dir, cmdmove, velocity; + float speed, zvel; + aas_clientmove_t move; + + AAS_ShowAreaPolygons( reach->areanum, 5, qtrue ); + //AAS_ShowArea(reach->areanum, qtrue); + AAS_DrawArrow( reach->start, reach->end, LINECOLOR_BLUE, LINECOLOR_YELLOW ); + // + if ( reach->traveltype == TRAVEL_JUMP || reach->traveltype == TRAVEL_WALKOFFLEDGE ) { + AAS_HorizontalVelocityForJump( aassettings.sv_jumpvel, reach->start, reach->end, &speed ); + // + VectorSubtract( reach->end, reach->start, dir ); + dir[2] = 0; + VectorNormalize( dir ); + //set the velocity + VectorScale( dir, speed, velocity ); + //set the command movement + VectorClear( cmdmove ); + cmdmove[2] = aassettings.sv_jumpvel; + // + AAS_PredictClientMovement( &move, -1, reach->start, PRESENCE_NORMAL, qtrue, + velocity, cmdmove, 3, 30, 0.1, + SE_HITGROUND | SE_ENTERWATER | SE_ENTERSLIME | + SE_ENTERLAVA | SE_HITGROUNDDAMAGE, 0, qtrue ); + // + if ( reach->traveltype == TRAVEL_JUMP ) { + AAS_JumpReachRunStart( reach, dir ); + AAS_DrawCross( dir, 4, LINECOLOR_BLUE ); + } //end if + } //end if + else if ( reach->traveltype == TRAVEL_ROCKETJUMP ) { + zvel = AAS_RocketJumpZVelocity( reach->start ); + AAS_HorizontalVelocityForJump( zvel, reach->start, reach->end, &speed ); + // + VectorSubtract( reach->end, reach->start, dir ); + dir[2] = 0; + VectorNormalize( dir ); + //get command movement + VectorScale( dir, speed, cmdmove ); + VectorSet( velocity, 0, 0, zvel ); + // + AAS_PredictClientMovement( &move, -1, reach->start, PRESENCE_NORMAL, qtrue, + velocity, cmdmove, 30, 30, 0.1, + SE_ENTERWATER | SE_ENTERSLIME | + SE_ENTERLAVA | SE_HITGROUNDDAMAGE | + SE_TOUCHJUMPPAD | SE_HITGROUNDAREA, reach->areanum, qtrue ); + } //end else if + else if ( reach->traveltype == TRAVEL_JUMPPAD ) { + VectorSet( cmdmove, 0, 0, 0 ); + // + VectorSubtract( reach->end, reach->start, dir ); + dir[2] = 0; + VectorNormalize( dir ); + //set the velocity + //NOTE: the edgenum is the horizontal velocity + VectorScale( dir, reach->edgenum, velocity ); + //NOTE: the facenum is the Z velocity + velocity[2] = reach->facenum; + // + AAS_PredictClientMovement( &move, -1, reach->start, PRESENCE_NORMAL, qtrue, + velocity, cmdmove, 30, 30, 0.1, + SE_ENTERWATER | SE_ENTERSLIME | + SE_ENTERLAVA | SE_HITGROUNDDAMAGE | + SE_TOUCHJUMPPAD | SE_HITGROUNDAREA, reach->areanum, qtrue ); + } //end else if +} //end of the function AAS_ShowReachability +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ShowReachableAreas( int areanum ) { + aas_areasettings_t *settings; + static aas_reachability_t reach; + static int index, lastareanum; + static float lasttime; + + if ( areanum != lastareanum ) { + index = 0; + lastareanum = areanum; + } //end if + settings = &( *aasworld ).areasettings[areanum]; + // + if ( !settings->numreachableareas ) { + return; + } + // + if ( index >= settings->numreachableareas ) { + index = 0; + } + // + if ( AAS_Time() - lasttime > 1.5 ) { + memcpy( &reach, &( *aasworld ).reachability[settings->firstreachablearea + index], sizeof( aas_reachability_t ) ); + index++; + lasttime = AAS_Time(); + AAS_PrintTravelType( reach.traveltype ); + botimport.Print( PRT_MESSAGE, "(traveltime: %i)\n", reach.traveltime ); + } //end if + AAS_ShowReachability( &reach ); +} //end of the function ShowReachableAreas diff --git a/src/botlib/be_aas_debug.h b/src/botlib/be_aas_debug.h new file mode 100644 index 0000000..cf23fff --- /dev/null +++ b/src/botlib/be_aas_debug.h @@ -0,0 +1,68 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_debug.h + * + * desc: AAS + * + * + *****************************************************************************/ + +//clear the shown debug lines +void AAS_ClearShownDebugLines( void ); +// +void AAS_ClearShownPolygons( void ); +//show a debug line +void AAS_DebugLine( vec3_t start, vec3_t end, int color ); +//show a permenent line +void AAS_PermanentLine( vec3_t start, vec3_t end, int color ); +//show a permanent cross +void AAS_DrawPermanentCross( vec3_t origin, float size, int color ); +//draw a cross in the plane +void AAS_DrawPlaneCross( vec3_t point, vec3_t normal, float dist, int type, int color ); +//show a bounding box +void AAS_ShowBoundingBox( vec3_t origin, vec3_t mins, vec3_t maxs ); +//show a face +void AAS_ShowFace( int facenum ); +//show an area +void AAS_ShowArea( int areanum, int groundfacesonly ); +// +void AAS_ShowAreaPolygons( int areanum, int color, int groundfacesonly ); +//draw a cros +void AAS_DrawCross( vec3_t origin, float size, int color ); +//print the travel type +void AAS_PrintTravelType( int traveltype ); +//draw an arrow +void AAS_DrawArrow( vec3_t start, vec3_t end, int linecolor, int arrowcolor ); +//visualize the given reachability +void AAS_ShowReachability( struct aas_reachability_s *reach ); +//show the reachable areas from the given area +void AAS_ShowReachableAreas( int areanum ); + diff --git a/src/botlib/be_aas_def.h b/src/botlib/be_aas_def.h new file mode 100644 index 0000000..4404d9b --- /dev/null +++ b/src/botlib/be_aas_def.h @@ -0,0 +1,295 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_def.h + * + * desc: AAS + * + * + *****************************************************************************/ + +//debugging on +#define AAS_DEBUG + +//#define MAX_CLIENTS 128 +//#define MAX_MODELS 256 // these are sent over the net as 8 bits +//#define MAX_SOUNDS 256 // so they cannot be blindly increased +//#define MAX_CONFIGSTRINGS 1024 +#define MAX_CONFIGSTRINGS 2048 //----(SA) upped + +//#define CS_SCORES 32 +//#define CS_MODELS (CS_SCORES+MAX_CLIENTS) +//#define CS_SOUNDS (CS_MODELS+MAX_MODELS) + +#define DF_AASENTNUMBER( x ) ( x - ( *aasworlds ).entities ) +#define DF_NUMBERAASENT( x ) ( &( *aasworlds ).entities[x] ) +#define DF_AASENTCLIENT( x ) ( x - ( *aasworlds ).entities - 1 ) +#define DF_CLIENTAASENT( x ) ( &( *aasworlds ).entities[x + 1] ) + +#ifndef MAX_PATH + #define MAX_PATH MAX_QPATH +#endif + +//string index (for model, sound and image index) +typedef struct aas_stringindex_s +{ + int numindexes; + char **index; +} aas_stringindex_t; + +//structure to link entities to areas and areas to entities +typedef struct aas_link_s +{ + int entnum; + int areanum; + struct aas_link_s *next_ent, *prev_ent; + struct aas_link_s *next_area, *prev_area; +} aas_link_t; + +//structure to link entities to leaves and leaves to entities +typedef struct bsp_link_s +{ + int entnum; + int leafnum; + struct bsp_link_s *next_ent, *prev_ent; + struct bsp_link_s *next_leaf, *prev_leaf; +} bsp_link_t; + +typedef struct bsp_entdata_s +{ + vec3_t origin; + vec3_t angles; + vec3_t absmins; + vec3_t absmaxs; + int solid; + int modelnum; +} bsp_entdata_t; + +//entity +typedef struct aas_entity_s +{ + //entity info + aas_entityinfo_t i; + //links into the AAS areas + aas_link_t *areas; + //links into the BSP leaves + bsp_link_t *leaves; +} aas_entity_t; + +typedef struct aas_settings_s +{ + float sv_friction; + float sv_stopspeed; + float sv_gravity; + float sv_waterfriction; + float sv_watergravity; + float sv_maxvelocity; + float sv_maxwalkvelocity; + float sv_maxcrouchvelocity; + float sv_maxswimvelocity; + float sv_walkaccelerate; + float sv_airaccelerate; + float sv_swimaccelerate; + float sv_maxstep; + float sv_maxsteepness; + float sv_maxwaterjump; + float sv_maxbarrier; + float sv_jumpvel; + qboolean sv_allowladders; +} aas_settings_t; + +//routing cache +typedef struct aas_routingcache_s +{ + int size; //size of the routing cache + float time; //last time accessed or updated + int cluster; //cluster the cache is for + int areanum; //area the cache is created for + vec3_t origin; //origin within the area + float starttraveltime; //travel time to start with + int travelflags; //combinations of the travel flags + struct aas_routingcache_s *prev, *next; + unsigned char *reachabilities; //reachabilities used for routing + unsigned short int traveltimes[1]; //travel time for every area (variable sized) +} aas_routingcache_t; + +//fields for the routing algorithm +typedef struct aas_routingupdate_s +{ + int cluster; + int areanum; //area number of the update + vec3_t start; //start point the area was entered + unsigned short int tmptraveltime; //temporary travel time + unsigned short int *areatraveltimes; //travel times within the area + qboolean inlist; //true if the update is in the list + struct aas_routingupdate_s *next; + struct aas_routingupdate_s *prev; +} aas_routingupdate_t; + +//reversed reachability link +typedef struct aas_reversedlink_s +{ + int linknum; //the aas_areareachability_t + int areanum; //reachable from this area + struct aas_reversedlink_s *next; //next link +} aas_reversedlink_t; + +//reversed area reachability +typedef struct aas_reversedreachability_s +{ + int numlinks; + aas_reversedlink_t *first; +} aas_reversedreachability_t; + +// Ridah, route-tables +#include "be_aas_routetable.h" +// done. + +typedef struct aas_s +{ + int loaded; //true when an AAS file is loaded + int initialized; //true when AAS has been initialized + int savefile; //set true when file should be saved + int bspchecksum; + //current time + float time; + int numframes; + //name of the aas file + char filename[MAX_PATH]; + char mapname[MAX_PATH]; + //bounding boxes + int numbboxes; + aas_bbox_t *bboxes; + //vertexes + int numvertexes; + aas_vertex_t *vertexes; + //planes + int numplanes; + aas_plane_t *planes; + //edges + int numedges; + aas_edge_t *edges; + //edge index + int edgeindexsize; + aas_edgeindex_t *edgeindex; + //faces + int numfaces; + aas_face_t *faces; + //face index + int faceindexsize; + aas_faceindex_t *faceindex; + //convex areas + int numareas; + aas_area_t *areas; + //convex area settings + int numareasettings; + aas_areasettings_t *areasettings; + //reachablity list + int reachabilitysize; + aas_reachability_t *reachability; + //nodes of the bsp tree + int numnodes; + aas_node_t *nodes; + //cluster portals + int numportals; + aas_portal_t *portals; + //cluster portal index + int portalindexsize; + aas_portalindex_t *portalindex; + //clusters + int numclusters; + aas_cluster_t *clusters; + // + int reachabilityareas; + float reachabilitytime; + //enities linked in the areas + aas_link_t *linkheap; //heap with link structures + int linkheapsize; //size of the link heap + aas_link_t *freelinks; //first free link + aas_link_t **arealinkedentities; //entities linked into areas + //entities + int maxentities; + int maxclients; + aas_entity_t *entities; + //string indexes + char *configstrings[MAX_CONFIGSTRINGS]; + int indexessetup; + //index to retrieve travel flag for a travel type + int travelflagfortype[MAX_TRAVELTYPES]; + //routing update + aas_routingupdate_t *areaupdate; + aas_routingupdate_t *portalupdate; + //number of routing updates during a frame (reset every frame) + int frameroutingupdates; + //reversed reachability links + aas_reversedreachability_t *reversedreachability; + //travel times within the areas + unsigned short ***areatraveltimes; + //array of size numclusters with cluster cache + aas_routingcache_t ***clusterareacache; + aas_routingcache_t **portalcache; + //maximum travel time through portals + int *portalmaxtraveltimes; + // Ridah, pointer to Route-Table information + aas_rt_t *routetable; + //hide travel times + unsigned short int *hidetraveltimes; + //vis data + byte *decompressedvis; + int decompressedvisarea; + byte **areavisibility; + // done. + // Ridah, store the area's waypoint for hidepos calculations (center traced downwards) + vec3_t *areawaypoints; + // Ridah, so we can cache the areas that have already been tested for visibility/attackability + byte *visCache; +} aas_t; + +#define AASINTERN + +#ifndef BSPCINCLUDE + +#include "be_aas_main.h" +#include "be_aas_entity.h" +#include "be_aas_sample.h" +#include "be_aas_cluster.h" +#include "be_aas_reach.h" +#include "be_aas_route.h" +#include "be_aas_routealt.h" +#include "be_aas_debug.h" +#include "be_aas_file.h" +#include "be_aas_optimize.h" +#include "be_aas_bsp.h" +#include "be_aas_move.h" + +// Ridah, route-tables +#include "be_aas_routetable.h" + +#endif //BSPCINCLUDE diff --git a/src/botlib/be_aas_entity.c b/src/botlib/be_aas_entity.c new file mode 100644 index 0000000..340cee5 --- /dev/null +++ b/src/botlib/be_aas_entity.c @@ -0,0 +1,499 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_entity.c + * + * desc: AAS entities + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "l_memory.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "l_utils.h" +#include "l_log.h" +#include "aasfile.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "be_aas_funcs.h" +#include "be_interface.h" +#include "be_aas_def.h" + +#define MASK_SOLID CONTENTS_PLAYERCLIP + +// Ridah, always use the default world for entities +extern aas_t aasworlds[MAX_AAS_WORLDS]; + +aas_t *defaultaasworld = aasworlds; + +//FIXME: these might change +enum { + ET_GENERAL, + ET_PLAYER, + ET_ITEM, + ET_MISSILE, + ET_MOVER +}; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_UpdateEntity( int entnum, bot_entitystate_t *state ) { + int relink; + aas_entity_t *ent; + vec3_t absmins, absmaxs; + + if ( !( *defaultaasworld ).loaded ) { + botimport.Print( PRT_MESSAGE, "AAS_UpdateEntity: not loaded\n" ); + return BLERR_NOAASFILE; + } //end if + + ent = &( *defaultaasworld ).entities[entnum]; + + ent->i.update_time = AAS_Time() - ent->i.ltime; + ent->i.type = state->type; + ent->i.flags = state->flags; + ent->i.ltime = AAS_Time(); + VectorCopy( ent->i.origin, ent->i.lastvisorigin ); + VectorCopy( state->old_origin, ent->i.old_origin ); + ent->i.solid = state->solid; + ent->i.groundent = state->groundent; + ent->i.modelindex = state->modelindex; + ent->i.modelindex2 = state->modelindex2; + ent->i.frame = state->frame; + //ent->i.event = state->event; + ent->i.eventParm = state->eventParm; + ent->i.powerups = state->powerups; + ent->i.weapon = state->weapon; + ent->i.legsAnim = state->legsAnim; + ent->i.torsoAnim = state->torsoAnim; + +// ent->i.weapAnim = state->weapAnim; //----(SA) +//----(SA) didn't want to comment in as I wasn't sure of any implications of changing the aas_entityinfo_t and bot_entitystate_t structures. + + //number of the entity + ent->i.number = entnum; + //updated so set valid flag + ent->i.valid = qtrue; + //link everything the first frame + + if ( ( *defaultaasworld ).numframes == 1 ) { + relink = qtrue; + } else { relink = qfalse;} + + // + if ( ent->i.solid == SOLID_BSP ) { + //if the angles of the model changed + if ( !VectorCompare( state->angles, ent->i.angles ) ) { + VectorCopy( state->angles, ent->i.angles ); + relink = qtrue; + } //end if + //get the mins and maxs of the model + //FIXME: rotate mins and maxs + AAS_BSPModelMinsMaxsOrigin( ent->i.modelindex, ent->i.angles, ent->i.mins, ent->i.maxs, NULL ); + } //end if + else if ( ent->i.solid == SOLID_BBOX ) { + //if the bounding box size changed + if ( !VectorCompare( state->mins, ent->i.mins ) || + !VectorCompare( state->maxs, ent->i.maxs ) ) { + VectorCopy( state->mins, ent->i.mins ); + VectorCopy( state->maxs, ent->i.maxs ); + relink = qtrue; + } //end if + } //end if + //if the origin changed + if ( !VectorCompare( state->origin, ent->i.origin ) ) { + VectorCopy( state->origin, ent->i.origin ); + relink = qtrue; + } //end if + //if the entity should be relinked + if ( relink ) { + //don't link the world model + if ( entnum != ENTITYNUM_WORLD ) { + //absolute mins and maxs + VectorAdd( ent->i.mins, ent->i.origin, absmins ); + VectorAdd( ent->i.maxs, ent->i.origin, absmaxs ); + + //unlink the entity + AAS_UnlinkFromAreas( ent->areas ); + //relink the entity to the AAS areas (use the larges bbox) + ent->areas = AAS_LinkEntityClientBBox( absmins, absmaxs, entnum, PRESENCE_NORMAL ); + //unlink the entity from the BSP leaves + AAS_UnlinkFromBSPLeaves( ent->leaves ); + //link the entity to the world BSP tree + ent->leaves = AAS_BSPLinkEntity( absmins, absmaxs, entnum, 0 ); + } //end if + } //end if + return BLERR_NOERROR; +} //end of the function AAS_UpdateEntity +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_EntityInfo( int entnum, aas_entityinfo_t *info ) { + if ( !( *defaultaasworld ).initialized ) { + botimport.Print( PRT_FATAL, "AAS_EntityInfo: (*defaultaasworld) not initialized\n" ); + memset( info, 0, sizeof( aas_entityinfo_t ) ); + return; + } //end if + + if ( entnum < 0 || entnum >= ( *defaultaasworld ).maxentities ) { + botimport.Print( PRT_FATAL, "AAS_EntityInfo: entnum %d out of range\n", entnum ); + memset( info, 0, sizeof( aas_entityinfo_t ) ); + return; + } //end if + + memcpy( info, &( *defaultaasworld ).entities[entnum].i, sizeof( aas_entityinfo_t ) ); +} //end of the function AAS_EntityInfo +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_EntityOrigin( int entnum, vec3_t origin ) { + if ( entnum < 0 || entnum >= ( *defaultaasworld ).maxentities ) { + botimport.Print( PRT_FATAL, "AAS_EntityOrigin: entnum %d out of range\n", entnum ); + VectorClear( origin ); + return; + } //end if + + VectorCopy( ( *defaultaasworld ).entities[entnum].i.origin, origin ); +} //end of the function AAS_EntityOrigin +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_EntityModelindex( int entnum ) { + if ( entnum < 0 || entnum >= ( *defaultaasworld ).maxentities ) { + botimport.Print( PRT_FATAL, "AAS_EntityModelindex: entnum %d out of range\n", entnum ); + return 0; + } //end if + return ( *defaultaasworld ).entities[entnum].i.modelindex; +} //end of the function AAS_EntityModelindex +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_EntityType( int entnum ) { + if ( !( *defaultaasworld ).initialized ) { + return 0; + } + + if ( entnum < 0 || entnum >= ( *defaultaasworld ).maxentities ) { + botimport.Print( PRT_FATAL, "AAS_EntityType: entnum %d out of range\n", entnum ); + return 0; + } //end if + return ( *defaultaasworld ).entities[entnum].i.type; +} //end of the AAS_EntityType +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_EntityModelNum( int entnum ) { + if ( !( *defaultaasworld ).initialized ) { + return 0; + } + + if ( entnum < 0 || entnum >= ( *defaultaasworld ).maxentities ) { + botimport.Print( PRT_FATAL, "AAS_EntityModelNum: entnum %d out of range\n", entnum ); + return 0; + } //end if + return ( *defaultaasworld ).entities[entnum].i.modelindex; +} //end of the function AAS_EntityModelNum +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_OriginOfEntityWithModelNum( int modelnum, vec3_t origin ) { + int i; + aas_entity_t *ent; + + for ( i = 0; i < ( *defaultaasworld ).maxentities; i++ ) + { + ent = &( *defaultaasworld ).entities[i]; + if ( ent->i.type == ET_MOVER ) { + if ( ent->i.modelindex == modelnum ) { + VectorCopy( ent->i.origin, origin ); + return qtrue; + } //end if + } + } //end for + return qfalse; +} //end of the function AAS_OriginOfEntityWithModelNum +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_EntitySize( int entnum, vec3_t mins, vec3_t maxs ) { + aas_entity_t *ent; + + if ( !( *defaultaasworld ).initialized ) { + return; + } + + if ( entnum < 0 || entnum >= ( *defaultaasworld ).maxentities ) { + botimport.Print( PRT_FATAL, "AAS_EntitySize: entnum %d out of range\n", entnum ); + return; + } //end if + + ent = &( *defaultaasworld ).entities[entnum]; + VectorCopy( ent->i.mins, mins ); + VectorCopy( ent->i.maxs, maxs ); +} //end of the function AAS_EntitySize +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_EntityBSPData( int entnum, bsp_entdata_t *entdata ) { + aas_entity_t *ent; + + ent = &( *defaultaasworld ).entities[entnum]; + VectorCopy( ent->i.origin, entdata->origin ); + VectorCopy( ent->i.angles, entdata->angles ); + VectorAdd( ent->i.origin, ent->i.mins, entdata->absmins ); + VectorAdd( ent->i.origin, ent->i.maxs, entdata->absmaxs ); + entdata->solid = ent->i.solid; + entdata->modelnum = ent->i.modelindex - 1; +} //end of the function AAS_EntityBSPData +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ResetEntityLinks( void ) { + int i; + for ( i = 0; i < ( *defaultaasworld ).maxentities; i++ ) + { + ( *defaultaasworld ).entities[i].areas = NULL; + ( *defaultaasworld ).entities[i].leaves = NULL; + } //end for +} //end of the function AAS_ResetEntityLinks +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_InvalidateEntities( void ) { + int i; + for ( i = 0; i < ( *defaultaasworld ).maxentities; i++ ) + { + ( *defaultaasworld ).entities[i].i.valid = qfalse; + ( *defaultaasworld ).entities[i].i.number = i; + } //end for +} //end of the function AAS_InvalidateEntities +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_NearestEntity( vec3_t origin, int modelindex ) { + int i, bestentnum; + float dist, bestdist; + aas_entity_t *ent; + vec3_t dir; + + bestentnum = 0; + bestdist = 99999; + for ( i = 0; i < ( *defaultaasworld ).maxentities; i++ ) + { + ent = &( *defaultaasworld ).entities[i]; + if ( ent->i.modelindex != modelindex ) { + continue; + } + VectorSubtract( ent->i.origin, origin, dir ); + if ( abs( dir[0] ) < 40 ) { + if ( abs( dir[1] ) < 40 ) { + dist = VectorLength( dir ); + if ( dist < bestdist ) { + bestdist = dist; + bestentnum = i; + } //end if + } //end if + } //end if + } //end for + return bestentnum; +} //end of the function AAS_NearestEntity +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_BestReachableEntityArea( int entnum ) { + aas_entity_t *ent; + + ent = &( *defaultaasworld ).entities[entnum]; + return AAS_BestReachableLinkArea( ent->areas ); +} //end of the function AAS_BestReachableEntityArea +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_NextEntity( int entnum ) { + if ( !( *defaultaasworld ).loaded ) { + return 0; + } + + if ( entnum < 0 ) { + entnum = -1; + } + while ( ++entnum < ( *defaultaasworld ).maxentities ) + { + if ( ( *defaultaasworld ).entities[entnum].i.valid ) { + return entnum; + } + } //end while + return 0; +} //end of the function AAS_NextEntity + +// Ridah, used to find out if there is an entity touching the given area, if so, try and avoid it +/* +============ +AAS_EntityInArea +============ +*/ +int AAS_IsEntityInArea( int entnumIgnore, int entnumIgnore2, int areanum ) { + aas_link_t *link; + aas_entity_t *ent; +// int i; + + // RF, not functional (doesnt work with multiple areas) + return qfalse; + + for ( link = ( *aasworld ).arealinkedentities[areanum]; link; link = link->next_ent ) + { + //ignore the pass entity + if ( link->entnum == entnumIgnore ) { + continue; + } + if ( link->entnum == entnumIgnore2 ) { + continue; + } + // + ent = &( *defaultaasworld ).entities[link->entnum]; + if ( !ent->i.valid ) { + continue; + } + if ( !ent->i.solid ) { + continue; + } + return qtrue; + } +/* + ent = (*defaultaasworld).entities; + for (i = 0; i < (*defaultaasworld).maxclients; i++, ent++) + { + if (!ent->i.valid) + continue; + if (!ent->i.solid) + continue; + if (i == entnumIgnore) + continue; + if (i == entnumIgnore2) + continue; + for (link = ent->areas; link; link = link->next_area) + { + if (link->areanum == areanum) + { + return qtrue; + } //end if + } //end for + } +*/ + return qfalse; +} + +/* +============= +AAS_SetAASBlockingEntity +============= +*/ +int AAS_EnableRoutingArea( int areanum, int enable ); +void AAS_SetAASBlockingEntity( vec3_t absmin, vec3_t absmax, qboolean blocking ) { + int areas[128]; + int numareas, i, w; + // + // check for resetting AAS blocking + if ( VectorCompare( absmin, absmax ) && blocking < 0 ) { + for ( w = 0; w < MAX_AAS_WORLDS; w++ ) { + AAS_SetCurrentWorld( w ); + // + if ( !( *aasworld ).loaded ) { + continue; + } + // now clear blocking status + for ( i = 1; i < ( *aasworld ).numareas; i++ ) { + AAS_EnableRoutingArea( i, qtrue ); + } + } + // + return; + } + // + for ( w = 0; w < MAX_AAS_WORLDS; w++ ) { + AAS_SetCurrentWorld( w ); + // + if ( !( *aasworld ).loaded ) { + continue; + } + // grab the list of areas + numareas = AAS_BBoxAreas( absmin, absmax, areas, 128 ); + // now set their blocking status + for ( i = 0; i < numareas; i++ ) { + AAS_EnableRoutingArea( areas[i], !blocking ); + } + } +} diff --git a/src/botlib/be_aas_entity.h b/src/botlib/be_aas_entity.h new file mode 100644 index 0000000..686e4f1 --- /dev/null +++ b/src/botlib/be_aas_entity.h @@ -0,0 +1,68 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_entity.h + * + * desc: AAS + * + * + *****************************************************************************/ + +#ifdef AASINTERN +//invalidates all entity infos +void AAS_InvalidateEntities( void ); +//resets the entity AAS and BSP links (sets areas and leaves pointers to NULL) +void AAS_ResetEntityLinks( void ); +//updates an entity +int AAS_UpdateEntity( int ent, bot_entitystate_t *state ); +//gives the entity data used for collision detection +void AAS_EntityBSPData( int entnum, bsp_entdata_t *entdata ); +#endif //AASINTERN + +//returns the size of the entity bounding box in mins and maxs +void AAS_EntitySize( int entnum, vec3_t mins, vec3_t maxs ); +//returns the BSP model number of the entity +int AAS_EntityModelNum( int entnum ); +//returns the origin of an entity with the given model number +int AAS_OriginOfEntityWithModelNum( int modelnum, vec3_t origin ); +//returns the best reachable area the entity is situated in +int AAS_BestReachableEntityArea( int entnum ); +//returns the info of the given entity +void AAS_EntityInfo( int entnum, aas_entityinfo_t *info ); +//returns the next entity +int AAS_NextEntity( int entnum ); +//returns the origin of the entity +void AAS_EntityOrigin( int entnum, vec3_t origin ); +//returns the entity type +int AAS_EntityType( int entnum ); +//returns the model index of the entity +int AAS_EntityModelindex( int entnum ); +// Ridah +int AAS_IsEntityInArea( int entnumIgnore, int entnumIgnore2, int areanum ); diff --git a/src/botlib/be_aas_file.c b/src/botlib/be_aas_file.c new file mode 100644 index 0000000..cc8d26e --- /dev/null +++ b/src/botlib/be_aas_file.c @@ -0,0 +1,662 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_file.c + * + * desc: AAS file loading/writing + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "l_memory.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "l_libvar.h" +#include "l_utils.h" +#include "aasfile.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "be_aas_funcs.h" +#include "be_interface.h" +#include "be_aas_def.h" + +//#define AASFILEDEBUG + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_SwapAASData( void ) { + int i, j; + + // Ridah, no need to do anything if this OS doesn't need byte swapping + if ( LittleLong( 1 ) == 1 ) { + return; + } + // done. + + //bounding boxes + for ( i = 0; i < ( *aasworld ).numbboxes; i++ ) + { + ( *aasworld ).bboxes[i].presencetype = LittleLong( ( *aasworld ).bboxes[i].presencetype ); + ( *aasworld ).bboxes[i].flags = LittleLong( ( *aasworld ).bboxes[i].flags ); + for ( j = 0; j < 3; j++ ) + { + ( *aasworld ).bboxes[i].mins[j] = LittleFloat( ( *aasworld ).bboxes[i].mins[j] ); + ( *aasworld ).bboxes[i].maxs[j] = LittleFloat( ( *aasworld ).bboxes[i].maxs[j] ); + } //end for + } //end for + //vertexes + for ( i = 0; i < ( *aasworld ).numvertexes; i++ ) + { + for ( j = 0; j < 3; j++ ) + ( *aasworld ).vertexes[i][j] = LittleFloat( ( *aasworld ).vertexes[i][j] ); + } //end for + //planes + for ( i = 0; i < ( *aasworld ).numplanes; i++ ) + { + for ( j = 0; j < 3; j++ ) + ( *aasworld ).planes[i].normal[j] = LittleFloat( ( *aasworld ).planes[i].normal[j] ); + ( *aasworld ).planes[i].dist = LittleFloat( ( *aasworld ).planes[i].dist ); + ( *aasworld ).planes[i].type = LittleLong( ( *aasworld ).planes[i].type ); + } //end for + //edges + for ( i = 0; i < ( *aasworld ).numedges; i++ ) + { + ( *aasworld ).edges[i].v[0] = LittleLong( ( *aasworld ).edges[i].v[0] ); + ( *aasworld ).edges[i].v[1] = LittleLong( ( *aasworld ).edges[i].v[1] ); + } //end for + //edgeindex + for ( i = 0; i < ( *aasworld ).edgeindexsize; i++ ) + { + ( *aasworld ).edgeindex[i] = LittleLong( ( *aasworld ).edgeindex[i] ); + } //end for + //faces + for ( i = 0; i < ( *aasworld ).numfaces; i++ ) + { + ( *aasworld ).faces[i].planenum = LittleLong( ( *aasworld ).faces[i].planenum ); + ( *aasworld ).faces[i].faceflags = LittleLong( ( *aasworld ).faces[i].faceflags ); + ( *aasworld ).faces[i].numedges = LittleLong( ( *aasworld ).faces[i].numedges ); + ( *aasworld ).faces[i].firstedge = LittleLong( ( *aasworld ).faces[i].firstedge ); + ( *aasworld ).faces[i].frontarea = LittleLong( ( *aasworld ).faces[i].frontarea ); + ( *aasworld ).faces[i].backarea = LittleLong( ( *aasworld ).faces[i].backarea ); + } //end for + //face index + for ( i = 0; i < ( *aasworld ).faceindexsize; i++ ) + { + ( *aasworld ).faceindex[i] = LittleLong( ( *aasworld ).faceindex[i] ); + } //end for + //convex areas + for ( i = 0; i < ( *aasworld ).numareas; i++ ) + { + ( *aasworld ).areas[i].areanum = LittleLong( ( *aasworld ).areas[i].areanum ); + ( *aasworld ).areas[i].numfaces = LittleLong( ( *aasworld ).areas[i].numfaces ); + ( *aasworld ).areas[i].firstface = LittleLong( ( *aasworld ).areas[i].firstface ); + for ( j = 0; j < 3; j++ ) + { + ( *aasworld ).areas[i].mins[j] = LittleFloat( ( *aasworld ).areas[i].mins[j] ); + ( *aasworld ).areas[i].maxs[j] = LittleFloat( ( *aasworld ).areas[i].maxs[j] ); + ( *aasworld ).areas[i].center[j] = LittleFloat( ( *aasworld ).areas[i].center[j] ); + } //end for + } //end for + //area settings + for ( i = 0; i < ( *aasworld ).numareasettings; i++ ) + { + ( *aasworld ).areasettings[i].contents = LittleLong( ( *aasworld ).areasettings[i].contents ); + ( *aasworld ).areasettings[i].areaflags = LittleLong( ( *aasworld ).areasettings[i].areaflags ); + ( *aasworld ).areasettings[i].presencetype = LittleLong( ( *aasworld ).areasettings[i].presencetype ); + ( *aasworld ).areasettings[i].cluster = LittleLong( ( *aasworld ).areasettings[i].cluster ); + ( *aasworld ).areasettings[i].clusterareanum = LittleLong( ( *aasworld ).areasettings[i].clusterareanum ); + ( *aasworld ).areasettings[i].numreachableareas = LittleLong( ( *aasworld ).areasettings[i].numreachableareas ); + ( *aasworld ).areasettings[i].firstreachablearea = LittleLong( ( *aasworld ).areasettings[i].firstreachablearea ); + ( *aasworld ).areasettings[i].groundsteepness = LittleLong( ( *aasworld ).areasettings[i].groundsteepness ); + } //end for + //area reachability + for ( i = 0; i < ( *aasworld ).reachabilitysize; i++ ) + { + ( *aasworld ).reachability[i].areanum = LittleLong( ( *aasworld ).reachability[i].areanum ); + ( *aasworld ).reachability[i].facenum = LittleLong( ( *aasworld ).reachability[i].facenum ); + ( *aasworld ).reachability[i].edgenum = LittleLong( ( *aasworld ).reachability[i].edgenum ); + for ( j = 0; j < 3; j++ ) + { + ( *aasworld ).reachability[i].start[j] = LittleFloat( ( *aasworld ).reachability[i].start[j] ); + ( *aasworld ).reachability[i].end[j] = LittleFloat( ( *aasworld ).reachability[i].end[j] ); + } //end for + ( *aasworld ).reachability[i].traveltype = LittleLong( ( *aasworld ).reachability[i].traveltype ); + ( *aasworld ).reachability[i].traveltime = LittleShort( ( *aasworld ).reachability[i].traveltime ); + } //end for + //nodes + for ( i = 0; i < ( *aasworld ).numnodes; i++ ) + { + ( *aasworld ).nodes[i].planenum = LittleLong( ( *aasworld ).nodes[i].planenum ); + ( *aasworld ).nodes[i].children[0] = LittleLong( ( *aasworld ).nodes[i].children[0] ); + ( *aasworld ).nodes[i].children[1] = LittleLong( ( *aasworld ).nodes[i].children[1] ); + } //end for + //cluster portals + for ( i = 0; i < ( *aasworld ).numportals; i++ ) + { + ( *aasworld ).portals[i].areanum = LittleLong( ( *aasworld ).portals[i].areanum ); + ( *aasworld ).portals[i].frontcluster = LittleLong( ( *aasworld ).portals[i].frontcluster ); + ( *aasworld ).portals[i].backcluster = LittleLong( ( *aasworld ).portals[i].backcluster ); + ( *aasworld ).portals[i].clusterareanum[0] = LittleLong( ( *aasworld ).portals[i].clusterareanum[0] ); + ( *aasworld ).portals[i].clusterareanum[1] = LittleLong( ( *aasworld ).portals[i].clusterareanum[1] ); + } //end for + //cluster portal index + for ( i = 0; i < ( *aasworld ).portalindexsize; i++ ) + { + ( *aasworld ).portalindex[i] = LittleLong( ( *aasworld ).portalindex[i] ); + } //end for + //cluster + for ( i = 0; i < ( *aasworld ).numclusters; i++ ) + { + ( *aasworld ).clusters[i].numareas = LittleLong( ( *aasworld ).clusters[i].numareas ); + ( *aasworld ).clusters[i].numreachabilityareas = LittleLong( ( *aasworld ).clusters[i].numreachabilityareas ); + ( *aasworld ).clusters[i].numportals = LittleLong( ( *aasworld ).clusters[i].numportals ); + ( *aasworld ).clusters[i].firstportal = LittleLong( ( *aasworld ).clusters[i].firstportal ); + } //end for +} //end of the function AAS_SwapAASData +//=========================================================================== +// dump the current loaded aas file +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_DumpAASData( void ) { + ( *aasworld ).numbboxes = 0; + if ( ( *aasworld ).bboxes ) { + FreeMemory( ( *aasworld ).bboxes ); + } + ( *aasworld ).bboxes = NULL; + ( *aasworld ).numvertexes = 0; + if ( ( *aasworld ).vertexes ) { + FreeMemory( ( *aasworld ).vertexes ); + } + ( *aasworld ).vertexes = NULL; + ( *aasworld ).numplanes = 0; + if ( ( *aasworld ).planes ) { + FreeMemory( ( *aasworld ).planes ); + } + ( *aasworld ).planes = NULL; + ( *aasworld ).numedges = 0; + if ( ( *aasworld ).edges ) { + FreeMemory( ( *aasworld ).edges ); + } + ( *aasworld ).edges = NULL; + ( *aasworld ).edgeindexsize = 0; + if ( ( *aasworld ).edgeindex ) { + FreeMemory( ( *aasworld ).edgeindex ); + } + ( *aasworld ).edgeindex = NULL; + ( *aasworld ).numfaces = 0; + if ( ( *aasworld ).faces ) { + FreeMemory( ( *aasworld ).faces ); + } + ( *aasworld ).faces = NULL; + ( *aasworld ).faceindexsize = 0; + if ( ( *aasworld ).faceindex ) { + FreeMemory( ( *aasworld ).faceindex ); + } + ( *aasworld ).faceindex = NULL; + ( *aasworld ).numareas = 0; + if ( ( *aasworld ).areas ) { + FreeMemory( ( *aasworld ).areas ); + } + ( *aasworld ).areas = NULL; + ( *aasworld ).numareasettings = 0; + if ( ( *aasworld ).areasettings ) { + FreeMemory( ( *aasworld ).areasettings ); + } + ( *aasworld ).areasettings = NULL; + ( *aasworld ).reachabilitysize = 0; + if ( ( *aasworld ).reachability ) { + FreeMemory( ( *aasworld ).reachability ); + } + ( *aasworld ).reachability = NULL; + ( *aasworld ).numnodes = 0; + if ( ( *aasworld ).nodes ) { + FreeMemory( ( *aasworld ).nodes ); + } + ( *aasworld ).nodes = NULL; + ( *aasworld ).numportals = 0; + if ( ( *aasworld ).portals ) { + FreeMemory( ( *aasworld ).portals ); + } + ( *aasworld ).portals = NULL; + ( *aasworld ).numportals = 0; + if ( ( *aasworld ).portalindex ) { + FreeMemory( ( *aasworld ).portalindex ); + } + ( *aasworld ).portalindex = NULL; + ( *aasworld ).portalindexsize = 0; + if ( ( *aasworld ).clusters ) { + FreeMemory( ( *aasworld ).clusters ); + } + ( *aasworld ).clusters = NULL; + ( *aasworld ).numclusters = 0; + // + ( *aasworld ).loaded = qfalse; + ( *aasworld ).initialized = qfalse; + ( *aasworld ).savefile = qfalse; +} //end of the function AAS_DumpAASData +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef AASFILEDEBUG +void AAS_FileInfo( void ) { + int i, n, optimized; + + botimport.Print( PRT_MESSAGE, "version = %d\n", AASVERSION ); + botimport.Print( PRT_MESSAGE, "numvertexes = %d\n", ( *aasworld ).numvertexes ); + botimport.Print( PRT_MESSAGE, "numplanes = %d\n", ( *aasworld ).numplanes ); + botimport.Print( PRT_MESSAGE, "numedges = %d\n", ( *aasworld ).numedges ); + botimport.Print( PRT_MESSAGE, "edgeindexsize = %d\n", ( *aasworld ).edgeindexsize ); + botimport.Print( PRT_MESSAGE, "numfaces = %d\n", ( *aasworld ).numfaces ); + botimport.Print( PRT_MESSAGE, "faceindexsize = %d\n", ( *aasworld ).faceindexsize ); + botimport.Print( PRT_MESSAGE, "numareas = %d\n", ( *aasworld ).numareas ); + botimport.Print( PRT_MESSAGE, "numareasettings = %d\n", ( *aasworld ).numareasettings ); + botimport.Print( PRT_MESSAGE, "reachabilitysize = %d\n", ( *aasworld ).reachabilitysize ); + botimport.Print( PRT_MESSAGE, "numnodes = %d\n", ( *aasworld ).numnodes ); + botimport.Print( PRT_MESSAGE, "numportals = %d\n", ( *aasworld ).numportals ); + botimport.Print( PRT_MESSAGE, "portalindexsize = %d\n", ( *aasworld ).portalindexsize ); + botimport.Print( PRT_MESSAGE, "numclusters = %d\n", ( *aasworld ).numclusters ); + // + for ( n = 0, i = 0; i < ( *aasworld ).numareasettings; i++ ) + { + if ( ( *aasworld ).areasettings[i].areaflags & AREA_GROUNDED ) { + n++; + } + } //end for + botimport.Print( PRT_MESSAGE, "num grounded areas = %d\n", n ); + // + botimport.Print( PRT_MESSAGE, "planes size %d bytes\n", ( *aasworld ).numplanes * sizeof( aas_plane_t ) ); + botimport.Print( PRT_MESSAGE, "areas size %d bytes\n", ( *aasworld ).numareas * sizeof( aas_area_t ) ); + botimport.Print( PRT_MESSAGE, "areasettings size %d bytes\n", ( *aasworld ).numareasettings * sizeof( aas_areasettings_t ) ); + botimport.Print( PRT_MESSAGE, "nodes size %d bytes\n", ( *aasworld ).numnodes * sizeof( aas_node_t ) ); + botimport.Print( PRT_MESSAGE, "reachability size %d bytes\n", ( *aasworld ).reachabilitysize * sizeof( aas_reachability_t ) ); + botimport.Print( PRT_MESSAGE, "portals size %d bytes\n", ( *aasworld ).numportals * sizeof( aas_portal_t ) ); + botimport.Print( PRT_MESSAGE, "clusters size %d bytes\n", ( *aasworld ).numclusters * sizeof( aas_cluster_t ) ); + + optimized = ( *aasworld ).numplanes * sizeof( aas_plane_t ) + + ( *aasworld ).numareas * sizeof( aas_area_t ) + + ( *aasworld ).numareasettings * sizeof( aas_areasettings_t ) + + ( *aasworld ).numnodes * sizeof( aas_node_t ) + + ( *aasworld ).reachabilitysize * sizeof( aas_reachability_t ) + + ( *aasworld ).numportals * sizeof( aas_portal_t ) + + ( *aasworld ).numclusters * sizeof( aas_cluster_t ); + botimport.Print( PRT_MESSAGE, "optimzed size %d KB\n", optimized >> 10 ); +} //end of the function AAS_FileInfo +#endif //AASFILEDEBUG +//=========================================================================== +// allocate memory and read a lump of a AAS file +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +char *AAS_LoadAASLump( fileHandle_t fp, int offset, int length, int *lastoffset ) { + char *buf; + // + if ( !length ) { + return NULL; + } + //seek to the data + if ( offset != *lastoffset ) { + botimport.Print( PRT_WARNING, "AAS file not sequentially read\n" ); + if ( botimport.FS_Seek( fp, offset, FS_SEEK_SET ) ) { + AAS_Error( "can't seek to aas lump\n" ); + AAS_DumpAASData(); + botimport.FS_FCloseFile( fp ); + return 0; + } //end if + } //end if + //allocate memory + buf = (char *) GetClearedHunkMemory( length + 1 ); + //read the data + if ( length ) { + botimport.FS_Read( buf, length, fp ); + *lastoffset += length; + } //end if + return buf; +} //end of the function AAS_LoadAASLump +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_DData( unsigned char *data, int size ) { + int i; + + for ( i = 0; i < size; i++ ) + { + data[i] ^= (unsigned char) i * 119; + } //end for +} //end of the function AAS_DData +//=========================================================================== +// load an aas file +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_LoadAASFile( char *filename ) { + fileHandle_t fp; + aas_header_t header; + int offset, length, lastoffset; + + botimport.Print( PRT_MESSAGE, "trying to load %s\n", filename ); + //dump current loaded aas file + AAS_DumpAASData(); + //open the file + botimport.FS_FOpenFile( filename, &fp, FS_READ ); + if ( !fp ) { + AAS_Error( "can't open %s\n", filename ); + return BLERR_CANNOTOPENAASFILE; + } //end if + //read the header + botimport.FS_Read( &header, sizeof( aas_header_t ), fp ); + lastoffset = sizeof( aas_header_t ); + //check header identification + header.ident = LittleLong( header.ident ); + if ( header.ident != AASID ) { + AAS_Error( "%s is not an AAS file\n", filename ); + botimport.FS_FCloseFile( fp ); + return BLERR_WRONGAASFILEID; + } //end if + //check the version + header.version = LittleLong( header.version ); + // + if ( header.version != AASVERSION ) { + AAS_Error( "aas file %s is version %i, not %i\n", filename, header.version, AASVERSION ); + botimport.FS_FCloseFile( fp ); + return BLERR_WRONGAASFILEVERSION; + } //end if + // + if ( header.version == AASVERSION ) { + AAS_DData( (unsigned char *) &header + 8, sizeof( aas_header_t ) - 8 ); + } //end if + // + ( *aasworld ).bspchecksum = atoi( LibVarGetString( "sv_mapChecksum" ) ); + if ( LittleLong( header.bspchecksum ) != ( *aasworld ).bspchecksum ) { + AAS_Error( "aas file %s is out of date\n", filename ); + botimport.FS_FCloseFile( fp ); + return BLERR_WRONGAASFILEVERSION; + } //end if + //load the lumps: + //bounding boxes + offset = LittleLong( header.lumps[AASLUMP_BBOXES].fileofs ); + length = LittleLong( header.lumps[AASLUMP_BBOXES].filelen ); + ( *aasworld ).bboxes = (aas_bbox_t *) AAS_LoadAASLump( fp, offset, length, &lastoffset ); + ( *aasworld ).numbboxes = length / sizeof( aas_bbox_t ); + if ( ( *aasworld ).numbboxes && !( *aasworld ).bboxes ) { + return BLERR_CANNOTREADAASLUMP; + } + //vertexes + offset = LittleLong( header.lumps[AASLUMP_VERTEXES].fileofs ); + length = LittleLong( header.lumps[AASLUMP_VERTEXES].filelen ); + ( *aasworld ).vertexes = (aas_vertex_t *) AAS_LoadAASLump( fp, offset, length, &lastoffset ); + ( *aasworld ).numvertexes = length / sizeof( aas_vertex_t ); + if ( ( *aasworld ).numvertexes && !( *aasworld ).vertexes ) { + return BLERR_CANNOTREADAASLUMP; + } + //planes + offset = LittleLong( header.lumps[AASLUMP_PLANES].fileofs ); + length = LittleLong( header.lumps[AASLUMP_PLANES].filelen ); + ( *aasworld ).planes = (aas_plane_t *) AAS_LoadAASLump( fp, offset, length, &lastoffset ); + ( *aasworld ).numplanes = length / sizeof( aas_plane_t ); + if ( ( *aasworld ).numplanes && !( *aasworld ).planes ) { + return BLERR_CANNOTREADAASLUMP; + } + //edges + offset = LittleLong( header.lumps[AASLUMP_EDGES].fileofs ); + length = LittleLong( header.lumps[AASLUMP_EDGES].filelen ); + ( *aasworld ).edges = (aas_edge_t *) AAS_LoadAASLump( fp, offset, length, &lastoffset ); + ( *aasworld ).numedges = length / sizeof( aas_edge_t ); + if ( ( *aasworld ).numedges && !( *aasworld ).edges ) { + return BLERR_CANNOTREADAASLUMP; + } + //edgeindex + offset = LittleLong( header.lumps[AASLUMP_EDGEINDEX].fileofs ); + length = LittleLong( header.lumps[AASLUMP_EDGEINDEX].filelen ); + ( *aasworld ).edgeindex = (aas_edgeindex_t *) AAS_LoadAASLump( fp, offset, length, &lastoffset ); + ( *aasworld ).edgeindexsize = length / sizeof( aas_edgeindex_t ); + if ( ( *aasworld ).edgeindexsize && !( *aasworld ).edgeindex ) { + return BLERR_CANNOTREADAASLUMP; + } + //faces + offset = LittleLong( header.lumps[AASLUMP_FACES].fileofs ); + length = LittleLong( header.lumps[AASLUMP_FACES].filelen ); + ( *aasworld ).faces = (aas_face_t *) AAS_LoadAASLump( fp, offset, length, &lastoffset ); + ( *aasworld ).numfaces = length / sizeof( aas_face_t ); + if ( ( *aasworld ).numfaces && !( *aasworld ).faces ) { + return BLERR_CANNOTREADAASLUMP; + } + //faceindex + offset = LittleLong( header.lumps[AASLUMP_FACEINDEX].fileofs ); + length = LittleLong( header.lumps[AASLUMP_FACEINDEX].filelen ); + ( *aasworld ).faceindex = (aas_faceindex_t *) AAS_LoadAASLump( fp, offset, length, &lastoffset ); + ( *aasworld ).faceindexsize = length / sizeof( int ); + if ( ( *aasworld ).faceindexsize && !( *aasworld ).faceindex ) { + return BLERR_CANNOTREADAASLUMP; + } + //convex areas + offset = LittleLong( header.lumps[AASLUMP_AREAS].fileofs ); + length = LittleLong( header.lumps[AASLUMP_AREAS].filelen ); + ( *aasworld ).areas = (aas_area_t *) AAS_LoadAASLump( fp, offset, length, &lastoffset ); + ( *aasworld ).numareas = length / sizeof( aas_area_t ); + if ( ( *aasworld ).numareas && !( *aasworld ).areas ) { + return BLERR_CANNOTREADAASLUMP; + } + //area settings + offset = LittleLong( header.lumps[AASLUMP_AREASETTINGS].fileofs ); + length = LittleLong( header.lumps[AASLUMP_AREASETTINGS].filelen ); + ( *aasworld ).areasettings = (aas_areasettings_t *) AAS_LoadAASLump( fp, offset, length, &lastoffset ); + ( *aasworld ).numareasettings = length / sizeof( aas_areasettings_t ); + if ( ( *aasworld ).numareasettings && !( *aasworld ).areasettings ) { + return BLERR_CANNOTREADAASLUMP; + } + //reachability list + offset = LittleLong( header.lumps[AASLUMP_REACHABILITY].fileofs ); + length = LittleLong( header.lumps[AASLUMP_REACHABILITY].filelen ); + ( *aasworld ).reachability = (aas_reachability_t *) AAS_LoadAASLump( fp, offset, length, &lastoffset ); + ( *aasworld ).reachabilitysize = length / sizeof( aas_reachability_t ); + if ( ( *aasworld ).reachabilitysize && !( *aasworld ).reachability ) { + return BLERR_CANNOTREADAASLUMP; + } + //nodes + offset = LittleLong( header.lumps[AASLUMP_NODES].fileofs ); + length = LittleLong( header.lumps[AASLUMP_NODES].filelen ); + ( *aasworld ).nodes = (aas_node_t *) AAS_LoadAASLump( fp, offset, length, &lastoffset ); + ( *aasworld ).numnodes = length / sizeof( aas_node_t ); + if ( ( *aasworld ).numnodes && !( *aasworld ).nodes ) { + return BLERR_CANNOTREADAASLUMP; + } + //cluster portals + offset = LittleLong( header.lumps[AASLUMP_PORTALS].fileofs ); + length = LittleLong( header.lumps[AASLUMP_PORTALS].filelen ); + ( *aasworld ).portals = (aas_portal_t *) AAS_LoadAASLump( fp, offset, length, &lastoffset ); + ( *aasworld ).numportals = length / sizeof( aas_portal_t ); + if ( ( *aasworld ).numportals && !( *aasworld ).portals ) { + return BLERR_CANNOTREADAASLUMP; + } + //cluster portal index + offset = LittleLong( header.lumps[AASLUMP_PORTALINDEX].fileofs ); + length = LittleLong( header.lumps[AASLUMP_PORTALINDEX].filelen ); + ( *aasworld ).portalindex = (aas_portalindex_t *) AAS_LoadAASLump( fp, offset, length, &lastoffset ); + ( *aasworld ).portalindexsize = length / sizeof( aas_portalindex_t ); + if ( ( *aasworld ).portalindexsize && !( *aasworld ).portalindex ) { + return BLERR_CANNOTREADAASLUMP; + } + //clusters + offset = LittleLong( header.lumps[AASLUMP_CLUSTERS].fileofs ); + length = LittleLong( header.lumps[AASLUMP_CLUSTERS].filelen ); + ( *aasworld ).clusters = (aas_cluster_t *) AAS_LoadAASLump( fp, offset, length, &lastoffset ); + ( *aasworld ).numclusters = length / sizeof( aas_cluster_t ); + if ( ( *aasworld ).numclusters && !( *aasworld ).clusters ) { + return BLERR_CANNOTREADAASLUMP; + } + //swap everything + AAS_SwapAASData(); + //aas file is loaded + ( *aasworld ).loaded = qtrue; + //close the file + botimport.FS_FCloseFile( fp ); + // +#ifdef AASFILEDEBUG + AAS_FileInfo(); +#endif //AASFILEDEBUG + // + return BLERR_NOERROR; +} //end of the function AAS_LoadAASFile +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +static int AAS_WriteAASLump_offset; + +int AAS_WriteAASLump( fileHandle_t fp, aas_header_t *h, int lumpnum, void *data, int length ) { + aas_lump_t *lump; + + lump = &h->lumps[lumpnum]; + + lump->fileofs = LittleLong( AAS_WriteAASLump_offset ); //LittleLong(ftell(fp)); + lump->filelen = LittleLong( length ); + + if ( length > 0 ) { + botimport.FS_Write( data, length, fp ); + } //end if + + AAS_WriteAASLump_offset += length; + + return qtrue; +} //end of the function AAS_WriteAASLump +//=========================================================================== +// aas data is useless after writing to file because it is byte swapped +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_WriteAASFile( char *filename ) { + aas_header_t header; + fileHandle_t fp; + + botimport.Print( PRT_MESSAGE, "writing %s\n", filename ); + //swap the aas data + AAS_SwapAASData(); + //initialize the file header + memset( &header, 0, sizeof( aas_header_t ) ); + header.ident = LittleLong( AASID ); + header.version = LittleLong( AASVERSION ); + header.bspchecksum = LittleLong( ( *aasworld ).bspchecksum ); + //open a new file + botimport.FS_FOpenFile( filename, &fp, FS_WRITE ); + if ( !fp ) { + botimport.Print( PRT_ERROR, "error opening %s\n", filename ); + return qfalse; + } //end if + //write the header + botimport.FS_Write( &header, sizeof( aas_header_t ), fp ); + AAS_WriteAASLump_offset = sizeof( aas_header_t ); + //add the data lumps to the file + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_BBOXES, ( *aasworld ).bboxes, + ( *aasworld ).numbboxes * sizeof( aas_bbox_t ) ) ) { + return qfalse; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_VERTEXES, ( *aasworld ).vertexes, + ( *aasworld ).numvertexes * sizeof( aas_vertex_t ) ) ) { + return qfalse; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_PLANES, ( *aasworld ).planes, + ( *aasworld ).numplanes * sizeof( aas_plane_t ) ) ) { + return qfalse; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_EDGES, ( *aasworld ).edges, + ( *aasworld ).numedges * sizeof( aas_edge_t ) ) ) { + return qfalse; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_EDGEINDEX, ( *aasworld ).edgeindex, + ( *aasworld ).edgeindexsize * sizeof( aas_edgeindex_t ) ) ) { + return qfalse; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_FACES, ( *aasworld ).faces, + ( *aasworld ).numfaces * sizeof( aas_face_t ) ) ) { + return qfalse; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_FACEINDEX, ( *aasworld ).faceindex, + ( *aasworld ).faceindexsize * sizeof( aas_faceindex_t ) ) ) { + return qfalse; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_AREAS, ( *aasworld ).areas, + ( *aasworld ).numareas * sizeof( aas_area_t ) ) ) { + return qfalse; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_AREASETTINGS, ( *aasworld ).areasettings, + ( *aasworld ).numareasettings * sizeof( aas_areasettings_t ) ) ) { + return qfalse; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_REACHABILITY, ( *aasworld ).reachability, + ( *aasworld ).reachabilitysize * sizeof( aas_reachability_t ) ) ) { + return qfalse; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_NODES, ( *aasworld ).nodes, + ( *aasworld ).numnodes * sizeof( aas_node_t ) ) ) { + return qfalse; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_PORTALS, ( *aasworld ).portals, + ( *aasworld ).numportals * sizeof( aas_portal_t ) ) ) { + return qfalse; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_PORTALINDEX, ( *aasworld ).portalindex, + ( *aasworld ).portalindexsize * sizeof( aas_portalindex_t ) ) ) { + return qfalse; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_CLUSTERS, ( *aasworld ).clusters, + ( *aasworld ).numclusters * sizeof( aas_cluster_t ) ) ) { + return qfalse; + } + //rewrite the header with the added lumps + botimport.FS_Seek( fp, 0, FS_SEEK_SET ); + botimport.FS_Write( &header, sizeof( aas_header_t ), fp ); + //close the file + botimport.FS_FCloseFile( fp ); + return qtrue; +} //end of the function AAS_WriteAASFile + diff --git a/src/botlib/be_aas_file.h b/src/botlib/be_aas_file.h new file mode 100644 index 0000000..4362da5 --- /dev/null +++ b/src/botlib/be_aas_file.h @@ -0,0 +1,48 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_file.h + * + * desc: AAS + * + * + *****************************************************************************/ + +#ifdef AASINTERN +//loads the AAS file with the given name +int AAS_LoadAASFile( char *filename ); +//writes an AAS file with the given name +qboolean AAS_WriteAASFile( char *filename ); +//dumps the loaded AAS data +void AAS_DumpAASData( void ); +//print AAS file information +void AAS_FileInfo( void ); +#endif //AASINTERN + diff --git a/src/botlib/be_aas_funcs.h b/src/botlib/be_aas_funcs.h new file mode 100644 index 0000000..7b54e05 --- /dev/null +++ b/src/botlib/be_aas_funcs.h @@ -0,0 +1,56 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_funcs.h + * + * desc: AAS + * + * + *****************************************************************************/ + +#ifndef BSPCINCLUDE + +#include "be_aas_main.h" +#include "be_aas_entity.h" +#include "be_aas_sample.h" +#include "be_aas_cluster.h" +#include "be_aas_reach.h" +#include "be_aas_route.h" +#include "be_aas_routealt.h" +#include "be_aas_debug.h" +#include "be_aas_file.h" +#include "be_aas_optimize.h" +#include "be_aas_bsp.h" +#include "be_aas_move.h" + +// Ridah, route-tables +#include "be_aas_routetable.h" + +#endif //BSPCINCLUDE diff --git a/src/botlib/be_aas_main.c b/src/botlib/be_aas_main.c new file mode 100644 index 0000000..c5d8870 --- /dev/null +++ b/src/botlib/be_aas_main.c @@ -0,0 +1,486 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_main.c + * + * desc: AAS + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "l_memory.h" +#include "l_libvar.h" +#include "l_utils.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "l_log.h" +#include "aasfile.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "be_aas_funcs.h" +#include "be_interface.h" +#include "be_aas_def.h" + +aas_t aasworlds[MAX_AAS_WORLDS]; + +aas_t *aasworld; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void QDECL AAS_Error( char *fmt, ... ) { + char str[1024]; + va_list arglist; + + va_start( arglist, fmt ); + vsprintf( str, fmt, arglist ); + va_end( arglist ); + botimport.Print( PRT_FATAL, str ); +} //end of the function AAS_Error + +// Ridah, multiple AAS worlds +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_SetCurrentWorld( int index ) { + if ( index >= MAX_AAS_WORLDS || index < 0 ) { + AAS_Error( "AAS_SetCurrentWorld: index out of range\n" ); + return; + } + + // set the current world pointer + aasworld = &aasworlds[index]; +} +// done. + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +char *AAS_StringFromIndex( char *indexname, char *stringindex[], int numindexes, int index ) { + if ( !( *aasworld ).indexessetup ) { + botimport.Print( PRT_ERROR, "%s: index %d not setup\n", indexname, index ); + return ""; + } //end if + if ( index < 0 || index >= numindexes ) { + botimport.Print( PRT_ERROR, "%s: index %d out of range\n", indexname, index ); + return ""; + } //end if + if ( !stringindex[index] ) { + if ( index ) { + botimport.Print( PRT_ERROR, "%s: reference to unused index %d\n", indexname, index ); + } //end if + return ""; + } //end if + return stringindex[index]; +} //end of the function AAS_StringFromIndex +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_IndexFromString( char *indexname, char *stringindex[], int numindexes, char *string ) { + int i; + if ( !( *aasworld ).indexessetup ) { + botimport.Print( PRT_ERROR, "%s: index not setup \"%s\"\n", indexname, string ); + return 0; + } //end if + for ( i = 0; i < numindexes; i++ ) + { + if ( !stringindex[i] ) { + continue; + } + if ( !Q_stricmp( stringindex[i], string ) ) { + return i; + } + } //end for + return 0; +} //end of the function AAS_IndexFromString +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +char *AAS_ModelFromIndex( int index ) { +// return AAS_StringFromIndex("ModelFromIndex", &(*aasworld).configstrings[CS_MODELS], MAX_MODELS, index); + return 0; // removed so the CS_ defines could be removed from be_aas_def.h +} //end of the function AAS_ModelFromIndex +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_IndexFromModel( char *modelname ) { +// return AAS_IndexFromString("IndexFromModel", &(*aasworld).configstrings[CS_MODELS], MAX_MODELS, modelname); + return 0; // removed so the CS_ defines could be removed from be_aas_def.h +} //end of the function AAS_IndexFromModel +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_UpdateStringIndexes( int numconfigstrings, char *configstrings[] ) { + int i; + //set string pointers and copy the strings + for ( i = 0; i < numconfigstrings; i++ ) + { + if ( configstrings[i] ) { + //if ((*aasworld).configstrings[i]) FreeMemory((*aasworld).configstrings[i]); + ( *aasworld ).configstrings[i] = (char *) GetMemory( strlen( configstrings[i] ) + 1 ); + strcpy( ( *aasworld ).configstrings[i], configstrings[i] ); + } //end if + } //end for + ( *aasworld ).indexessetup = qtrue; +} //end of the function AAS_UpdateStringIndexes +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_Loaded( void ) { + return ( *aasworld ).loaded; +} //end of the function AAS_Loaded +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_Initialized( void ) { + return ( *aasworld ).initialized; +} //end of the function AAS_Initialized +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_SetInitialized( void ) { + ( *aasworld ).initialized = qtrue; + botimport.Print( PRT_MESSAGE, "AAS initialized.\n" ); +#ifdef DEBUG + //create all the routing cache + //AAS_CreateAllRoutingCache(); + // + //AAS_RoutingInfo(); +#endif + + // Ridah, build/load the route-table + AAS_RT_BuildRouteTable(); + // done. + +} //end of the function AAS_SetInitialized +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ContinueInit( float time ) { + //if no AAS file loaded + if ( !( *aasworld ).loaded ) { + return; + } + //if AAS is already initialized + if ( ( *aasworld ).initialized ) { + return; + } + //calculate reachability, if not finished return + if ( AAS_ContinueInitReachability( time ) ) { + return; + } + //initialize clustering for the new map + AAS_InitClustering(); + //if reachability has been calculated and an AAS file should be written + //or there is a forced data optimization + if ( ( *aasworld ).savefile || ( (int)LibVarGetValue( "forcewrite" ) ) ) { + //optimize the AAS data + if ( !( (int)LibVarValue( "nooptimize", "1" ) ) ) { + AAS_Optimize(); + } + //save the AAS file + if ( AAS_WriteAASFile( ( *aasworld ).filename ) ) { + botimport.Print( PRT_MESSAGE, "%s written succesfully\n", ( *aasworld ).filename ); + } //end if + else + { + botimport.Print( PRT_ERROR, "couldn't write %s\n", ( *aasworld ).filename ); + } //end else + } //end if + //initialize the routing + AAS_InitRouting(); + //at this point AAS is initialized + AAS_SetInitialized(); +} //end of the function AAS_ContinueInit +//=========================================================================== +// called at the start of every frame +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_StartFrame( float time ) { + // Ridah, do each of the aasworlds + int i; + + for ( i = 0; i < MAX_AAS_WORLDS; i++ ) + { + AAS_SetCurrentWorld( i ); + + ( *aasworld ).time = time; + //invalidate the entities + AAS_InvalidateEntities(); + //initialize AAS + AAS_ContinueInit( time ); + // + ( *aasworld ).frameroutingupdates = 0; + // + /* Ridah, disabled for speed + if (LibVarGetValue("showcacheupdates")) + { + AAS_RoutingInfo(); + LibVarSet("showcacheupdates", "0"); + } //end if + if (LibVarGetValue("showmemoryusage")) + { + PrintUsedMemorySize(); + LibVarSet("showmemoryusage", "0"); + } //end if + if (LibVarGetValue("memorydump")) + { + PrintMemoryLabels(); + LibVarSet("memorydump", "0"); + } //end if + */ + } //end if + ( *aasworld ).numframes++; + return BLERR_NOERROR; +} //end of the function AAS_StartFrame +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float AAS_Time( void ) { + return ( *aasworld ).time; +} //end of the function AAS_Time +//=========================================================================== +// basedir = Quake2 console basedir +// gamedir = Quake2 console gamedir +// mapname = name of the map without extension (.bsp) +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_LoadFiles( const char *mapname ) { + int errnum; + char aasfile[MAX_PATH]; +// char bspfile[MAX_PATH]; + + strcpy( ( *aasworld ).mapname, mapname ); + //NOTE: first reset the entity links into the AAS areas and BSP leaves + // the AAS link heap and BSP link heap are reset after respectively the + // AAS file and BSP file are loaded + AAS_ResetEntityLinks(); + // + + // load bsp info + AAS_LoadBSPFile(); + + //load the aas file + Com_sprintf( aasfile, MAX_PATH, "maps/%s.aas", mapname ); + errnum = AAS_LoadAASFile( aasfile ); + if ( errnum != BLERR_NOERROR ) { + return errnum; + } + + botimport.Print( PRT_MESSAGE, "loaded %s\n", aasfile ); + strncpy( ( *aasworld ).filename, aasfile, MAX_PATH ); + return BLERR_NOERROR; +} //end of the function AAS_LoadFiles +//=========================================================================== +// called everytime a map changes +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== + +// Ridah, modified this for multiple AAS files + +int AAS_LoadMap( const char *mapname ) { + int errnum; + int i; + char this_mapname[256], intstr[4]; + qboolean loaded = qfalse; + int missingErrNum = 0; // TTimo: init + + for ( i = 0; i < MAX_AAS_WORLDS; i++ ) + { + AAS_SetCurrentWorld( i ); + + strncpy( this_mapname, mapname, 256 ); + strncat( this_mapname, "_b", 256 ); + sprintf( intstr, "%i", i ); + strncat( this_mapname, intstr, 256 ); + + //if no mapname is provided then the string indexes are updated + if ( !mapname ) { + return 0; + } //end if + // + ( *aasworld ).initialized = qfalse; + //NOTE: free the routing caches before loading a new map because + // to free the caches the old number of areas, number of clusters + // and number of areas in a clusters must be available + AAS_FreeRoutingCaches(); + //load the map + errnum = AAS_LoadFiles( this_mapname ); + if ( errnum != BLERR_NOERROR ) { + ( *aasworld ).loaded = qfalse; + // RF, we are allowed to skip one of the files, but not both + //return errnum; + missingErrNum = errnum; + continue; + } //end if + // + loaded = qtrue; + // + AAS_InitSettings(); + //initialize the AAS link heap for the new map + AAS_InitAASLinkHeap(); + //initialize the AAS linked entities for the new map + AAS_InitAASLinkedEntities(); + //initialize reachability for the new map + AAS_InitReachability(); + //initialize the alternative routing + AAS_InitAlternativeRouting(); + } + + if ( !loaded ) { + return missingErrNum; + } + + //everything went ok + return 0; +} //end of the function AAS_LoadMap + +// done. + +//=========================================================================== +// called when the library is first loaded +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_Setup( void ) { + // Ridah, just use the default world for entities + AAS_SetCurrentWorld( 0 ); + + ( *aasworlds ).maxclients = (int) LibVarValue( "maxclients", "128" ); + ( *aasworlds ).maxentities = (int) LibVarValue( "maxentities", "1024" ); + //allocate memory for the entities + if ( ( *aasworld ).entities ) { + FreeMemory( ( *aasworld ).entities ); + } + ( *aasworld ).entities = (aas_entity_t *) GetClearedHunkMemory( ( *aasworld ).maxentities * sizeof( aas_entity_t ) ); + //invalidate all the entities + AAS_InvalidateEntities(); + + //force some recalculations + //LibVarSet("forceclustering", "1"); //force clustering calculation + //LibVarSet("forcereachability", "1"); //force reachability calculation + ( *aasworld ).numframes = 0; + return BLERR_NOERROR; +} //end of the function AAS_Setup +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_Shutdown( void ) { + // Ridah, do each of the worlds + int i; + + for ( i = 0; i < MAX_AAS_WORLDS; i++ ) + { + AAS_SetCurrentWorld( i ); + + // Ridah, kill the route-table data + AAS_RT_ShutdownRouteTable(); + + AAS_ShutdownAlternativeRouting(); + AAS_DumpBSPData(); + //free routing caches + AAS_FreeRoutingCaches(); + //free aas link heap + AAS_FreeAASLinkHeap(); + //free aas linked entities + AAS_FreeAASLinkedEntities(); + //free the aas data + AAS_DumpAASData(); + + if ( i == 0 ) { + //free the entities + if ( ( *aasworld ).entities ) { + FreeMemory( ( *aasworld ).entities ); + } + } + + //clear the (*aasworld) structure + memset( &( *aasworld ), 0, sizeof( aas_t ) ); + //aas has not been initialized + ( *aasworld ).initialized = qfalse; + } + + //NOTE: as soon as a new .bsp file is loaded the .bsp file memory is + // freed an reallocated, so there's no need to free that memory here + //print shutdown + botimport.Print( PRT_MESSAGE, "AAS shutdown.\n" ); +} //end of the function AAS_Shutdown diff --git a/src/botlib/be_aas_main.h b/src/botlib/be_aas_main.h new file mode 100644 index 0000000..b0373a8 --- /dev/null +++ b/src/botlib/be_aas_main.h @@ -0,0 +1,69 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_main.h + * + * desc: AAS + * + * + *****************************************************************************/ + +#ifdef AASINTERN + +extern aas_t( *aasworld ); + +//AAS error message +void QDECL AAS_Error( char *fmt, ... ); +//set AAS initialized +void AAS_SetInitialized( void ); +//setup AAS with the given number of entities and clients +int AAS_Setup( void ); +//shutdown AAS +void AAS_Shutdown( void ); +//start a new map +int AAS_LoadMap( const char *mapname ); +//start a new time frame +int AAS_StartFrame( float time ); +#endif //AASINTERN + +//returns true if AAS is initialized +int AAS_Initialized( void ); +//returns true if the AAS file is loaded +int AAS_Loaded( void ); +//returns the model name from the given index +char *AAS_ModelFromIndex( int index ); +//returns the index from the given model name +int AAS_IndexFromModel( char *modelname ); +//returns the current time +float AAS_Time( void ); + +// Ridah +void AAS_SetCurrentWorld( int index ); +// done. diff --git a/src/botlib/be_aas_move.c b/src/botlib/be_aas_move.c new file mode 100644 index 0000000..4f6154c --- /dev/null +++ b/src/botlib/be_aas_move.c @@ -0,0 +1,912 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_move.c + * + * desc: AAS + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "l_memory.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "aasfile.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "be_aas_funcs.h" +#include "be_aas_def.h" + +//#define BSPC + +extern botlib_import_t botimport; + +aas_settings_t aassettings; + +//#define AAS_MOVE_DEBUG + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_DropToFloor( vec3_t origin, vec3_t mins, vec3_t maxs ) { + vec3_t end; + bsp_trace_t trace; + + VectorCopy( origin, end ); + end[2] -= 100; + trace = AAS_Trace( origin, mins, maxs, end, 0, CONTENTS_SOLID ); + if ( trace.startsolid ) { + return qfalse; + } + VectorCopy( trace.endpos, origin ); + return qtrue; +} //end of the function AAS_DropToFloor +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_InitSettings( void ) { + aassettings.sv_friction = 6; + aassettings.sv_stopspeed = 100; + aassettings.sv_gravity = 800; + aassettings.sv_waterfriction = 1; + aassettings.sv_watergravity = 400; + aassettings.sv_maxvelocity = 320; + aassettings.sv_maxwalkvelocity = 300; + aassettings.sv_maxcrouchvelocity = 100; + aassettings.sv_maxswimvelocity = 150; + aassettings.sv_walkaccelerate = 10; + aassettings.sv_airaccelerate = 1; + aassettings.sv_swimaccelerate = 4; + aassettings.sv_maxstep = 18; + aassettings.sv_maxsteepness = 0.7; + aassettings.sv_maxwaterjump = 17; + // Ridah, calculate maxbarrier according to jumpvel and gravity + aassettings.sv_jumpvel = 270; + aassettings.sv_maxbarrier = 49; //-0.8 + (0.5 * aassettings.sv_gravity * (aassettings.sv_jumpvel / aassettings.sv_gravity) * (aassettings.sv_jumpvel / aassettings.sv_gravity)); + // done. + +} //end of the function AAS_InitSettings +//=========================================================================== +// returns qtrue if the bot is against a ladder +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AgainstLadder( vec3_t origin, int ms_areanum ) { + int areanum, i, facenum, side; + vec3_t org; + aas_plane_t *plane; + aas_face_t *face; + aas_area_t *area; + + VectorCopy( origin, org ); + areanum = AAS_PointAreaNum( org ); + if ( !areanum ) { + org[0] += 1; + areanum = AAS_PointAreaNum( org ); + if ( !areanum ) { + org[1] += 1; + areanum = AAS_PointAreaNum( org ); + if ( !areanum ) { + org[0] -= 2; + areanum = AAS_PointAreaNum( org ); + if ( !areanum ) { + org[1] -= 2; + areanum = AAS_PointAreaNum( org ); + } //end if + } //end if + } //end if + } //end if + //if in solid... wrrr shouldn't happen + //if (!areanum) return qfalse; + // RF, it does if they're in a monsterclip brush + if ( !areanum ) { + areanum = ms_areanum; + } + //if not in a ladder area + if ( !( ( *aasworld ).areasettings[areanum].areaflags & AREA_LADDER ) ) { + return qfalse; + } + //if a crouch only area + if ( !( ( *aasworld ).areasettings[areanum].presencetype & PRESENCE_NORMAL ) ) { + return qfalse; + } + // + area = &( *aasworld ).areas[areanum]; + for ( i = 0; i < area->numfaces; i++ ) + { + facenum = ( *aasworld ).faceindex[area->firstface + i]; + side = facenum < 0; + face = &( *aasworld ).faces[abs( facenum )]; + //if the face isn't a ladder face + if ( !( face->faceflags & FACE_LADDER ) ) { + continue; + } + //get the plane the face is in + plane = &( *aasworld ).planes[face->planenum ^ side]; + //if the origin is pretty close to the plane + if ( abs( DotProduct( plane->normal, origin ) - plane->dist ) < 3 ) { + if ( AAS_PointInsideFace( abs( facenum ), origin, 0.1 ) ) { + return qtrue; + } + } //end if + } //end for + return qfalse; +} //end of the function AAS_AgainstLadder +//=========================================================================== +// returns qtrue if the bot is on the ground +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_OnGround( vec3_t origin, int presencetype, int passent ) { + aas_trace_t trace; + vec3_t end, up = {0, 0, 1}; + aas_plane_t *plane; + + VectorCopy( origin, end ); + end[2] -= 10; + + trace = AAS_TraceClientBBox( origin, end, presencetype, passent ); + + //if in solid + if ( trace.startsolid ) { + return qtrue; //qfalse; + } + //if nothing hit at all + if ( trace.fraction >= 1.0 ) { + return qfalse; + } + //if too far from the hit plane + if ( origin[2] - trace.endpos[2] > 10 ) { + return qfalse; + } + //check if the plane isn't too steep + plane = AAS_PlaneFromNum( trace.planenum ); + if ( DotProduct( plane->normal, up ) < aassettings.sv_maxsteepness ) { + return qfalse; + } + //the bot is on the ground + return qtrue; +} //end of the function AAS_OnGround +//=========================================================================== +// returns qtrue if a bot at the given position is swimming +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_Swimming( vec3_t origin ) { + vec3_t testorg; + + VectorCopy( origin, testorg ); + testorg[2] -= 2; + if ( AAS_PointContents( testorg ) & ( CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER ) ) { + return qtrue; + } + return qfalse; +} //end of the function AAS_Swimming +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +vec3_t VEC_UP = {0, -1, 0}; +vec3_t MOVEDIR_UP = {0, 0, 1}; +vec3_t VEC_DOWN = {0, -2, 0}; +vec3_t MOVEDIR_DOWN = {0, 0, -1}; + +void AAS_SetMovedir( vec3_t angles, vec3_t movedir ) { + if ( VectorCompare( angles, VEC_UP ) ) { + VectorCopy( MOVEDIR_UP, movedir ); + } //end if + else if ( VectorCompare( angles, VEC_DOWN ) ) { + VectorCopy( MOVEDIR_DOWN, movedir ); + } //end else if + else + { + AngleVectors( angles, movedir, NULL, NULL ); + } //end else +} //end of the function AAS_SetMovedir +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_JumpReachRunStart( aas_reachability_t *reach, vec3_t runstart ) { + vec3_t hordir, start, cmdmove; + aas_clientmove_t move; + + // + hordir[0] = reach->start[0] - reach->end[0]; + hordir[1] = reach->start[1] - reach->end[1]; + hordir[2] = 0; + VectorNormalize( hordir ); + //start point + VectorCopy( reach->start, start ); + start[2] += 1; + //get command movement + VectorScale( hordir, 400, cmdmove ); + // + AAS_PredictClientMovement( &move, -1, start, PRESENCE_NORMAL, qtrue, + vec3_origin, cmdmove, 1, 2, 0.1, + SE_ENTERWATER | SE_ENTERSLIME | SE_ENTERLAVA | + SE_HITGROUNDDAMAGE | SE_GAP, 0, qfalse ); + VectorCopy( move.endpos, runstart ); + //don't enter slime or lava and don't fall from too high + if ( move.stopevent & ( SE_ENTERLAVA | SE_HITGROUNDDAMAGE ) ) { //----(SA) modified since slime is no longer deadly +// if (move.stopevent & (SE_ENTERSLIME|SE_ENTERLAVA|SE_HITGROUNDDAMAGE)) + VectorCopy( start, runstart ); + } //end if +} //end of the function AAS_JumpReachRunStart +//=========================================================================== +// returns the Z velocity when rocket jumping at the origin +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float AAS_WeaponJumpZVelocity( vec3_t origin, float radiusdamage ) { + vec3_t kvel, v, start, end, forward, right, viewangles, dir; + float mass, knockback, points; + vec3_t rocketoffset = {8, 8, -8}; + vec3_t botmins = {-16, -16, -24}; + vec3_t botmaxs = {16, 16, 32}; + bsp_trace_t bsptrace; + + //look down (90 degrees) + viewangles[PITCH] = 90; + viewangles[YAW] = 0; + viewangles[ROLL] = 0; + //get the start point shooting from + VectorCopy( origin, start ); + start[2] += 8; //view offset Z + AngleVectors( viewangles, forward, right, NULL ); + start[0] += forward[0] * rocketoffset[0] + right[0] * rocketoffset[1]; + start[1] += forward[1] * rocketoffset[0] + right[1] * rocketoffset[1]; + start[2] += forward[2] * rocketoffset[0] + right[2] * rocketoffset[1] + rocketoffset[2]; + //end point of the trace + VectorMA( start, 500, forward, end ); + //trace a line to get the impact point + bsptrace = AAS_Trace( start, NULL, NULL, end, 1, CONTENTS_SOLID ); + //calculate the damage the bot will get from the rocket impact + VectorAdd( botmins, botmaxs, v ); + VectorMA( origin, 0.5, v, v ); + VectorSubtract( bsptrace.endpos, v, v ); + // + points = radiusdamage - 0.5 * VectorLength( v ); + if ( points < 0 ) { + points = 0; + } + //the owner of the rocket gets half the damage + points *= 0.5; + //mass of the bot (p_client.c: PutClientInServer) + mass = 200; + //knockback is the same as the damage points + knockback = points; + //direction of the damage (from trace.endpos to bot origin) + VectorSubtract( origin, bsptrace.endpos, dir ); + VectorNormalize( dir ); + //damage velocity + VectorScale( dir, 1600.0 * (float)knockback / mass, kvel ); //the rocket jump hack... + //rocket impact velocity + jump velocity + return kvel[2] + aassettings.sv_jumpvel; +} //end of the function AAS_WeaponJumpZVelocity +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float AAS_RocketJumpZVelocity( vec3_t origin ) { + //rocket radius damage is 120 (p_weapon.c: Weapon_RocketLauncher_Fire) + return AAS_WeaponJumpZVelocity( origin, 120 ); +} //end of the function AAS_RocketJumpZVelocity +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float AAS_BFGJumpZVelocity( vec3_t origin ) { + //bfg radius damage is 1000 (p_weapon.c: weapon_bfg_fire) + return AAS_WeaponJumpZVelocity( origin, 120 ); +} //end of the function AAS_BFGJumpZVelocity +//=========================================================================== +// applies ground friction to the given velocity +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_Accelerate( vec3_t velocity, float frametime, vec3_t wishdir, float wishspeed, float accel ) { + // q2 style + int i; + float addspeed, accelspeed, currentspeed; + + currentspeed = DotProduct( velocity, wishdir ); + addspeed = wishspeed - currentspeed; + if ( addspeed <= 0 ) { + return; + } + accelspeed = accel * frametime * wishspeed; + if ( accelspeed > addspeed ) { + accelspeed = addspeed; + } + + for ( i = 0 ; i < 3 ; i++ ) { + velocity[i] += accelspeed * wishdir[i]; + } +} //end of the function AAS_Accelerate +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_AirControl( vec3_t start, vec3_t end, vec3_t velocity, vec3_t cmdmove ) { + vec3_t dir; + + VectorSubtract( end, start, dir ); +} //end of the function AAS_AirControl +//=========================================================================== +// applies ground friction to the given velocity +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ApplyFriction( vec3_t vel, float friction, float stopspeed, + float frametime ) { + float speed, control, newspeed; + + //horizontal speed + speed = sqrt( vel[0] * vel[0] + vel[1] * vel[1] ); + if ( speed ) { + control = speed < stopspeed ? stopspeed : speed; + newspeed = speed - frametime * control * friction; + if ( newspeed < 0 ) { + newspeed = 0; + } + newspeed /= speed; + vel[0] *= newspeed; + vel[1] *= newspeed; + } //end if +} //end of the function AAS_ApplyFriction +//=========================================================================== +// predicts the movement +// assumes regular bounding box sizes +// NOTE: out of water jumping is not included +// NOTE: grappling hook is not included +// +// Parameter: origin : origin to start with +// presencetype : presence type to start with +// velocity : velocity to start with +// cmdmove : client command movement +// cmdframes : number of frame cmdmove is valid +// maxframes : maximum number of predicted frames +// frametime : duration of one predicted frame +// stopevent : events that stop the prediction +// stopareanum : stop as soon as entered this area +// Returns: aas_clientmove_t +// Changes Globals: - +//=========================================================================== +int AAS_PredictClientMovement( struct aas_clientmove_s *move, + int entnum, vec3_t origin, + int presencetype, int onground, + vec3_t velocity, vec3_t cmdmove, + int cmdframes, + int maxframes, float frametime, + int stopevent, int stopareanum, int visualize ) { + float sv_friction, sv_stopspeed, sv_gravity, sv_waterfriction; + float sv_watergravity; + float sv_walkaccelerate, sv_airaccelerate, sv_swimaccelerate; + float sv_maxwalkvelocity, sv_maxcrouchvelocity, sv_maxswimvelocity; + float sv_maxstep, sv_maxsteepness, sv_jumpvel, friction; + float gravity, delta, maxvel, wishspeed, accelerate; + //float velchange, newvel; + int n, i, j, pc, step, swimming, ax, crouch, event, jump_frame, areanum; + int areas[20], numareas; + vec3_t points[20]; + vec3_t org, end, feet, start, stepend, lastorg, wishdir; + vec3_t frame_test_vel, old_frame_test_vel, left_test_vel; + vec3_t up = {0, 0, 1}; + aas_plane_t *plane, *plane2; + aas_trace_t trace, steptrace; + + if ( frametime <= 0 ) { + frametime = 0.1; + } + // + sv_friction = aassettings.sv_friction; + sv_stopspeed = aassettings.sv_stopspeed; + sv_gravity = aassettings.sv_gravity; + sv_waterfriction = aassettings.sv_waterfriction; + sv_watergravity = aassettings.sv_watergravity; + sv_maxwalkvelocity = aassettings.sv_maxwalkvelocity; // * frametime; + sv_maxcrouchvelocity = aassettings.sv_maxcrouchvelocity; // * frametime; + sv_maxswimvelocity = aassettings.sv_maxswimvelocity; // * frametime; + sv_walkaccelerate = aassettings.sv_walkaccelerate; + sv_airaccelerate = aassettings.sv_airaccelerate; + sv_swimaccelerate = aassettings.sv_swimaccelerate; + sv_maxstep = aassettings.sv_maxstep; + sv_maxsteepness = aassettings.sv_maxsteepness; + sv_jumpvel = aassettings.sv_jumpvel * frametime; + // + memset( move, 0, sizeof( aas_clientmove_t ) ); + memset( &trace, 0, sizeof( aas_trace_t ) ); + //start at the current origin + VectorCopy( origin, org ); + org[2] += 0.25; + //velocity to test for the first frame + VectorScale( velocity, frametime, frame_test_vel ); + // + jump_frame = -1; + //predict a maximum of 'maxframes' ahead + for ( n = 0; n < maxframes; n++ ) + { + swimming = AAS_Swimming( org ); + //get gravity depending on swimming or not + gravity = swimming ? sv_watergravity : sv_gravity; + //apply gravity at the START of the frame + frame_test_vel[2] = frame_test_vel[2] - ( gravity * 0.1 * frametime ); + //if on the ground or swimming + if ( onground || swimming ) { + friction = swimming ? sv_friction : sv_waterfriction; + //apply friction + VectorScale( frame_test_vel, 1 / frametime, frame_test_vel ); + AAS_ApplyFriction( frame_test_vel, friction, sv_stopspeed, frametime ); + VectorScale( frame_test_vel, frametime, frame_test_vel ); + } //end if + crouch = qfalse; + //apply command movement + if ( n < cmdframes ) { + ax = 0; + maxvel = sv_maxwalkvelocity; + accelerate = sv_airaccelerate; + VectorCopy( cmdmove, wishdir ); + if ( onground ) { + if ( cmdmove[2] < -300 ) { + crouch = qtrue; + maxvel = sv_maxcrouchvelocity; + } //end if + //if not swimming and upmove is positive then jump + if ( !swimming && cmdmove[2] > 1 ) { + //jump velocity minus the gravity for one frame + 5 for safety + frame_test_vel[2] = sv_jumpvel - ( gravity * 0.1 * frametime ) + 5; + jump_frame = n; + //jumping so air accelerate + accelerate = sv_airaccelerate; + } //end if + else + { + accelerate = sv_walkaccelerate; + } //end else + ax = 2; + } //end if + if ( swimming ) { + maxvel = sv_maxswimvelocity; + accelerate = sv_swimaccelerate; + ax = 3; + } //end if + else + { + wishdir[2] = 0; + } //end else + // + wishspeed = VectorNormalize( wishdir ); + if ( wishspeed > maxvel ) { + wishspeed = maxvel; + } + VectorScale( frame_test_vel, 1 / frametime, frame_test_vel ); + AAS_Accelerate( frame_test_vel, frametime, wishdir, wishspeed, accelerate ); + VectorScale( frame_test_vel, frametime, frame_test_vel ); + /* + for (i = 0; i < ax; i++) + { + velchange = (cmdmove[i] * frametime) - frame_test_vel[i]; + if (velchange > sv_maxacceleration) velchange = sv_maxacceleration; + else if (velchange < -sv_maxacceleration) velchange = -sv_maxacceleration; + newvel = frame_test_vel[i] + velchange; + // + if (frame_test_vel[i] <= maxvel && newvel > maxvel) frame_test_vel[i] = maxvel; + else if (frame_test_vel[i] >= -maxvel && newvel < -maxvel) frame_test_vel[i] = -maxvel; + else frame_test_vel[i] = newvel; + } //end for + */ + } //end if + if ( crouch ) { + presencetype = PRESENCE_CROUCH; + } //end if + else if ( presencetype == PRESENCE_CROUCH ) { + if ( AAS_PointPresenceType( org ) & PRESENCE_NORMAL ) { + presencetype = PRESENCE_NORMAL; + } //end if + } //end else + //save the current origin + VectorCopy( org, lastorg ); + //move linear during one frame + VectorCopy( frame_test_vel, left_test_vel ); + j = 0; + do + { + VectorAdd( org, left_test_vel, end ); + //trace a bounding box + trace = AAS_TraceClientBBox( org, end, presencetype, entnum ); + // +//#ifdef AAS_MOVE_DEBUG + if ( visualize ) { + if ( trace.startsolid ) { + botimport.Print( PRT_MESSAGE, "PredictMovement: start solid\n" ); + } + AAS_DebugLine( org, trace.endpos, LINECOLOR_RED ); + } //end if +//#endif //AAS_MOVE_DEBUG + // + if ( stopevent & SE_ENTERAREA ) { + numareas = AAS_TraceAreas( org, trace.endpos, areas, points, 20 ); + for ( i = 0; i < numareas; i++ ) + { + if ( areas[i] == stopareanum ) { + VectorCopy( points[i], move->endpos ); + VectorScale( frame_test_vel, 1 / frametime, move->velocity ); + move->trace = trace; + move->stopevent = SE_ENTERAREA; + move->presencetype = presencetype; + move->endcontents = 0; + move->time = n * frametime; + move->frames = n; + return qtrue; + } //end if + } //end for + } //end if + //move the entity to the trace end point + VectorCopy( trace.endpos, org ); + //if there was a collision + if ( trace.fraction < 1.0 ) { + //get the plane the bounding box collided with + plane = AAS_PlaneFromNum( trace.planenum ); + // + if ( stopevent & SE_HITGROUNDAREA ) { + if ( DotProduct( plane->normal, up ) > sv_maxsteepness ) { + VectorCopy( org, start ); + start[2] += 0.5; + if ( AAS_PointAreaNum( start ) == stopareanum ) { + VectorCopy( start, move->endpos ); + VectorScale( frame_test_vel, 1 / frametime, move->velocity ); + move->trace = trace; + move->stopevent = SE_HITGROUNDAREA; + move->presencetype = presencetype; + move->endcontents = 0; + move->time = n * frametime; + move->frames = n; + return qtrue; + } //end if + } //end if + } //end if + //assume there's no step + step = qfalse; + //if it is a vertical plane and the bot didn't jump recently + if ( plane->normal[2] == 0 && ( jump_frame < 0 || n - jump_frame > 2 ) ) { + //check for a step + VectorMA( org, -0.25, plane->normal, start ); + VectorCopy( start, stepend ); + start[2] += sv_maxstep; + steptrace = AAS_TraceClientBBox( start, stepend, presencetype, entnum ); + // + if ( !steptrace.startsolid ) { + plane2 = AAS_PlaneFromNum( steptrace.planenum ); + if ( DotProduct( plane2->normal, up ) > sv_maxsteepness ) { + VectorSubtract( end, steptrace.endpos, left_test_vel ); + left_test_vel[2] = 0; + frame_test_vel[2] = 0; +//#ifdef AAS_MOVE_DEBUG + if ( visualize ) { + if ( steptrace.endpos[2] - org[2] > 0.125 ) { + VectorCopy( org, start ); + start[2] = steptrace.endpos[2]; + AAS_DebugLine( org, start, LINECOLOR_BLUE ); + } //end if + } //end if +//#endif //AAS_MOVE_DEBUG + org[2] = steptrace.endpos[2]; + step = qtrue; + } //end if + } //end if + } //end if + // + if ( !step ) { + //velocity left to test for this frame is the projection + //of the current test velocity into the hit plane + VectorMA( left_test_vel, -DotProduct( left_test_vel, plane->normal ), + plane->normal, left_test_vel ); + //store the old velocity for landing check + VectorCopy( frame_test_vel, old_frame_test_vel ); + //test velocity for the next frame is the projection + //of the velocity of the current frame into the hit plane + VectorMA( frame_test_vel, -DotProduct( frame_test_vel, plane->normal ), + plane->normal, frame_test_vel ); + //check for a landing on an almost horizontal floor + if ( DotProduct( plane->normal, up ) > sv_maxsteepness ) { + onground = qtrue; + } //end if + if ( stopevent & SE_HITGROUNDDAMAGE ) { + delta = 0; + if ( old_frame_test_vel[2] < 0 && + frame_test_vel[2] > old_frame_test_vel[2] && + !onground ) { + delta = old_frame_test_vel[2]; + } //end if + else if ( onground ) { + delta = frame_test_vel[2] - old_frame_test_vel[2]; + } //end else + if ( delta ) { + delta = delta * 10; + delta = delta * delta * 0.0001; + if ( swimming ) { + delta = 0; + } + // never take falling damage if completely underwater + /* + if (ent->waterlevel == 3) return; + if (ent->waterlevel == 2) delta *= 0.25; + if (ent->waterlevel == 1) delta *= 0.5; + */ + if ( delta > 40 ) { + VectorCopy( org, move->endpos ); + VectorCopy( frame_test_vel, move->velocity ); + move->trace = trace; + move->stopevent = SE_HITGROUNDDAMAGE; + move->presencetype = presencetype; + move->endcontents = 0; + move->time = n * frametime; + move->frames = n; + return qtrue; + } //end if + } //end if + } //end if + } //end if + } //end if + //extra check to prevent endless loop + if ( ++j > 20 ) { + return qfalse; + } + //while there is a plane hit + } while ( trace.fraction < 1.0 ); + //if going down + if ( frame_test_vel[2] <= 10 ) { + //check for a liquid at the feet of the bot + VectorCopy( org, feet ); + feet[2] -= 22; + pc = AAS_PointContents( feet ); + //get event from pc + event = SE_NONE; + if ( pc & CONTENTS_LAVA ) { + event |= SE_ENTERLAVA; + } + if ( pc & CONTENTS_SLIME ) { + event |= SE_ENTERSLIME; + } + if ( pc & CONTENTS_WATER ) { + event |= SE_ENTERWATER; + } + // + areanum = AAS_PointAreaNum( org ); + if ( ( *aasworld ).areasettings[areanum].contents & AREACONTENTS_LAVA ) { + event |= SE_ENTERLAVA; + } + if ( ( *aasworld ).areasettings[areanum].contents & AREACONTENTS_SLIME ) { + event |= SE_ENTERSLIME; + } + if ( ( *aasworld ).areasettings[areanum].contents & AREACONTENTS_WATER ) { + event |= SE_ENTERWATER; + } + //if in lava or slime + if ( event & stopevent ) { + VectorCopy( org, move->endpos ); + VectorScale( frame_test_vel, 1 / frametime, move->velocity ); + move->stopevent = event & stopevent; + move->presencetype = presencetype; + move->endcontents = pc; + move->time = n * frametime; + move->frames = n; + return qtrue; + } //end if + } //end if + // + onground = AAS_OnGround( org, presencetype, entnum ); + //if onground and on the ground for at least one whole frame + if ( onground ) { + if ( stopevent & SE_HITGROUND ) { + VectorCopy( org, move->endpos ); + VectorScale( frame_test_vel, 1 / frametime, move->velocity ); + move->trace = trace; + move->stopevent = SE_HITGROUND; + move->presencetype = presencetype; + move->endcontents = 0; + move->time = n * frametime; + move->frames = n; + return qtrue; + } //end if + } //end if + else if ( stopevent & SE_LEAVEGROUND ) { + VectorCopy( org, move->endpos ); + VectorScale( frame_test_vel, 1 / frametime, move->velocity ); + move->trace = trace; + move->stopevent = SE_LEAVEGROUND; + move->presencetype = presencetype; + move->endcontents = 0; + move->time = n * frametime; + move->frames = n; + return qtrue; + } //end else if + else if ( stopevent & SE_GAP ) { + aas_trace_t gaptrace; + + VectorCopy( org, start ); + VectorCopy( start, end ); + end[2] -= 48 + aassettings.sv_maxbarrier; + gaptrace = AAS_TraceClientBBox( start, end, PRESENCE_CROUCH, -1 ); + //if solid is found the bot cannot walk any further and will not fall into a gap + if ( !gaptrace.startsolid ) { + //if it is a gap (lower than one step height) + if ( gaptrace.endpos[2] < org[2] - aassettings.sv_maxstep - 1 ) { + if ( !( AAS_PointContents( end ) & ( CONTENTS_WATER | CONTENTS_SLIME ) ) ) { //----(SA) modified since slime is no longer deadly +// if (!(AAS_PointContents(end) & CONTENTS_WATER)) + VectorCopy( lastorg, move->endpos ); + VectorScale( frame_test_vel, 1 / frametime, move->velocity ); + move->trace = trace; + move->stopevent = SE_GAP; + move->presencetype = presencetype; + move->endcontents = 0; + move->time = n * frametime; + move->frames = n; + return qtrue; + } //end if + } //end if + } //end if + } //end else if + if ( stopevent & SE_TOUCHJUMPPAD ) { + if ( ( *aasworld ).areasettings[AAS_PointAreaNum( org )].contents & AREACONTENTS_JUMPPAD ) { + VectorCopy( org, move->endpos ); + VectorScale( frame_test_vel, 1 / frametime, move->velocity ); + move->trace = trace; + move->stopevent = SE_TOUCHJUMPPAD; + move->presencetype = presencetype; + move->endcontents = 0; + move->time = n * frametime; + move->frames = n; + return qtrue; + } //end if + } //end if + if ( stopevent & SE_TOUCHTELEPORTER ) { + if ( ( *aasworld ).areasettings[AAS_PointAreaNum( org )].contents & AREACONTENTS_TELEPORTER ) { + VectorCopy( org, move->endpos ); + VectorScale( frame_test_vel, 1 / frametime, move->velocity ); + move->trace = trace; + move->stopevent = SE_TOUCHTELEPORTER; + move->presencetype = presencetype; + move->endcontents = 0; + move->time = n * frametime; + move->frames = n; + return qtrue; + } //end if + } //end if + } //end for + // + VectorCopy( org, move->endpos ); + VectorScale( frame_test_vel, 1 / frametime, move->velocity ); + move->stopevent = SE_NONE; + move->presencetype = presencetype; + move->endcontents = 0; + move->time = n * frametime; + move->frames = n; + // + return qtrue; +} //end of the function AAS_PredictClientMovement +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_TestMovementPrediction( int entnum, vec3_t origin, vec3_t dir ) { + vec3_t velocity, cmdmove; + aas_clientmove_t move; + + VectorClear( velocity ); + if ( !AAS_Swimming( origin ) ) { + dir[2] = 0; + } + VectorNormalize( dir ); + VectorScale( dir, 400, cmdmove ); + cmdmove[2] = 224; + AAS_ClearShownDebugLines(); + AAS_PredictClientMovement( &move, entnum, origin, PRESENCE_NORMAL, qtrue, + velocity, cmdmove, 13, 13, 0.1, SE_HITGROUND, 0, qtrue ); //SE_LEAVEGROUND); + if ( move.stopevent & SE_LEAVEGROUND ) { + botimport.Print( PRT_MESSAGE, "leave ground\n" ); + } //end if +} //end of the function TestMovementPrediction +//=========================================================================== +// calculates the horizontal velocity needed to perform a jump from start +// to end +// +// Parameter: zvel : z velocity for jump +// start : start position of jump +// end : end position of jump +// *speed : returned speed for jump +// Returns: qfalse if too high or too far from start to end +// Changes Globals: - +//=========================================================================== +int AAS_HorizontalVelocityForJump( float zvel, vec3_t start, vec3_t end, float *velocity ) { + float sv_gravity, sv_maxvelocity; + float maxjump, height2fall, t, top; + vec3_t dir; + + sv_gravity = aassettings.sv_gravity; + sv_maxvelocity = aassettings.sv_maxvelocity; + + //maximum height a player can jump with the given initial z velocity + maxjump = 0.5 * sv_gravity * ( zvel / sv_gravity ) * ( zvel / sv_gravity ); + //top of the parabolic jump + top = start[2] + maxjump; + //height the bot will fall from the top + height2fall = top - end[2]; + //if the goal is to high to jump to + if ( height2fall < 0 ) { + *velocity = sv_maxvelocity; + return 0; + } //end if + //time a player takes to fall the height + t = sqrt( height2fall / ( 0.5 * sv_gravity ) ); + //direction from start to end + VectorSubtract( end, start, dir ); + //calculate horizontal speed + *velocity = sqrt( dir[0] * dir[0] + dir[1] * dir[1] ) / ( t + zvel / sv_gravity ); + //the horizontal speed must be lower than the max speed + if ( *velocity > sv_maxvelocity ) { + *velocity = sv_maxvelocity; + return 0; + } //end if + return 1; +} //end of the function AAS_HorizontalVelocityForJump diff --git a/src/botlib/be_aas_move.h b/src/botlib/be_aas_move.h new file mode 100644 index 0000000..4771be0 --- /dev/null +++ b/src/botlib/be_aas_move.h @@ -0,0 +1,69 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_move.h + * + * desc: AAS + * + * + *****************************************************************************/ + +#ifdef AASINTERN +extern aas_settings_t aassettings; +#endif //AASINTERN + +//movement prediction +int AAS_PredictClientMovement( struct aas_clientmove_s *move, + int entnum, vec3_t origin, + int presencetype, int onground, + vec3_t velocity, vec3_t cmdmove, + int cmdframes, + int maxframes, float frametime, + int stopevent, int stopareanum, int visualize ); +//returns true if on the ground at the given origin +int AAS_OnGround( vec3_t origin, int presencetype, int passent ); +//returns true if swimming at the given origin +int AAS_Swimming( vec3_t origin ); +//returns the jump reachability run start point +void AAS_JumpReachRunStart( struct aas_reachability_s *reach, vec3_t runstart ); +//returns true if against a ladder at the given origin +int AAS_AgainstLadder( vec3_t origin, int ms_areanum ); +//rocket jump Z velocity when rocket-jumping at origin +float AAS_RocketJumpZVelocity( vec3_t origin ); +//bfg jump Z velocity when bfg-jumping at origin +float AAS_BFGJumpZVelocity( vec3_t origin ); +//calculates the horizontal velocity needed for a jump and returns true this velocity could be calculated +int AAS_HorizontalVelocityForJump( float zvel, vec3_t start, vec3_t end, float *velocity ); +// +void AAS_SetMovedir( vec3_t angles, vec3_t movedir ); +// +int AAS_DropToFloor( vec3_t origin, vec3_t mins, vec3_t maxs ); +// +void AAS_InitSettings( void ); diff --git a/src/botlib/be_aas_optimize.c b/src/botlib/be_aas_optimize.c new file mode 100644 index 0000000..32bd80e --- /dev/null +++ b/src/botlib/be_aas_optimize.c @@ -0,0 +1,337 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_optimize.c + * + * desc: decreases the .aas file size after the reachabilities have + * been calculated, just dumps all the faces, edges and vertexes + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "l_libvar.h" +//#include "l_utils.h" +#include "l_memory.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "aasfile.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "be_aas_funcs.h" +#include "be_interface.h" +#include "be_aas_def.h" + +typedef struct optimized_s +{ + //vertixes + int numvertexes; + aas_vertex_t *vertexes; + //edges + int numedges; + aas_edge_t *edges; + //edge index + int edgeindexsize; + aas_edgeindex_t *edgeindex; + //faces + int numfaces; + aas_face_t *faces; + //face index + int faceindexsize; + aas_faceindex_t *faceindex; + //convex areas + int numareas; + aas_area_t *areas; + // + int *vertexoptimizeindex; + int *edgeoptimizeindex; + int *faceoptimizeindex; +} optimized_t; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_KeepEdge( aas_edge_t *edge ) { + return 1; +} //end of the function AAS_KeepFace +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_OptimizeEdge( optimized_t *optimized, int edgenum ) { + int i, optedgenum; + aas_edge_t *edge, *optedge; + + edge = &( *aasworld ).edges[abs( edgenum )]; + if ( !AAS_KeepEdge( edge ) ) { + return 0; + } + + optedgenum = optimized->edgeoptimizeindex[abs( edgenum )]; + if ( optedgenum ) { + //keep the edge reversed sign + if ( edgenum > 0 ) { + return optedgenum; + } else { return -optedgenum;} + } //end if + + optedge = &optimized->edges[optimized->numedges]; + + for ( i = 0; i < 2; i++ ) + { + if ( optimized->vertexoptimizeindex[edge->v[i]] ) { + optedge->v[i] = optimized->vertexoptimizeindex[edge->v[i]]; + } //end if + else + { + VectorCopy( ( *aasworld ).vertexes[edge->v[i]], optimized->vertexes[optimized->numvertexes] ); + optedge->v[i] = optimized->numvertexes; + optimized->vertexoptimizeindex[edge->v[i]] = optimized->numvertexes; + optimized->numvertexes++; + } //end else + } //end for + optimized->edgeoptimizeindex[abs( edgenum )] = optimized->numedges; + optedgenum = optimized->numedges; + optimized->numedges++; + //keep the edge reversed sign + if ( edgenum > 0 ) { + return optedgenum; + } else { return -optedgenum;} +} //end of the function AAS_OptimizeEdge +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_KeepFace( aas_face_t *face ) { + if ( !( face->faceflags & FACE_LADDER ) ) { + return 0; + } else { return 1;} +} //end of the function AAS_KeepFace +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_OptimizeFace( optimized_t *optimized, int facenum ) { + int i, edgenum, optedgenum, optfacenum; + aas_face_t *face, *optface; + + face = &( *aasworld ).faces[abs( facenum )]; + if ( !AAS_KeepFace( face ) ) { + return 0; + } + + optfacenum = optimized->faceoptimizeindex[abs( facenum )]; + if ( optfacenum ) { + //keep the face side sign + if ( facenum > 0 ) { + return optfacenum; + } else { return -optfacenum;} + } //end if + + optface = &optimized->faces[optimized->numfaces]; + memcpy( optface, face, sizeof( aas_face_t ) ); + + optface->numedges = 0; + optface->firstedge = optimized->edgeindexsize; + for ( i = 0; i < face->numedges; i++ ) + { + edgenum = ( *aasworld ).edgeindex[face->firstedge + i]; + optedgenum = AAS_OptimizeEdge( optimized, edgenum ); + if ( optedgenum ) { + optimized->edgeindex[optface->firstedge + optface->numedges] = optedgenum; + optface->numedges++; + optimized->edgeindexsize++; + } //end if + } //end for + optimized->faceoptimizeindex[abs( facenum )] = optimized->numfaces; + optfacenum = optimized->numfaces; + optimized->numfaces++; + //keep the face side sign + if ( facenum > 0 ) { + return optfacenum; + } else { return -optfacenum;} +} //end of the function AAS_OptimizeFace +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_OptimizeArea( optimized_t *optimized, int areanum ) { + int i, facenum, optfacenum; + aas_area_t *area, *optarea; + + area = &( *aasworld ).areas[areanum]; + optarea = &optimized->areas[areanum]; + memcpy( optarea, area, sizeof( aas_area_t ) ); + + optarea->numfaces = 0; + optarea->firstface = optimized->faceindexsize; + for ( i = 0; i < area->numfaces; i++ ) + { + facenum = ( *aasworld ).faceindex[area->firstface + i]; + optfacenum = AAS_OptimizeFace( optimized, facenum ); + if ( optfacenum ) { + optimized->faceindex[optarea->firstface + optarea->numfaces] = optfacenum; + optarea->numfaces++; + optimized->faceindexsize++; + } //end if + } //end for +} //end of the function AAS_OptimizeArea +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_OptimizeAlloc( optimized_t *optimized ) { + optimized->vertexes = (aas_vertex_t *) GetClearedMemory( ( *aasworld ).numvertexes * sizeof( aas_vertex_t ) ); + optimized->numvertexes = 0; + optimized->edges = (aas_edge_t *) GetClearedMemory( ( *aasworld ).numedges * sizeof( aas_edge_t ) ); + optimized->numedges = 1; //edge zero is a dummy + optimized->edgeindex = (aas_edgeindex_t *) GetClearedMemory( ( *aasworld ).edgeindexsize * sizeof( aas_edgeindex_t ) ); + optimized->edgeindexsize = 0; + optimized->faces = (aas_face_t *) GetClearedMemory( ( *aasworld ).numfaces * sizeof( aas_face_t ) ); + optimized->numfaces = 1; //face zero is a dummy + optimized->faceindex = (aas_faceindex_t *) GetClearedMemory( ( *aasworld ).faceindexsize * sizeof( aas_faceindex_t ) ); + optimized->faceindexsize = 0; + optimized->areas = (aas_area_t *) GetClearedMemory( ( *aasworld ).numareas * sizeof( aas_area_t ) ); + optimized->numareas = ( *aasworld ).numareas; + // + optimized->vertexoptimizeindex = (int *) GetClearedMemory( ( *aasworld ).numvertexes * sizeof( int ) ); + optimized->edgeoptimizeindex = (int *) GetClearedMemory( ( *aasworld ).numedges * sizeof( int ) ); + optimized->faceoptimizeindex = (int *) GetClearedMemory( ( *aasworld ).numfaces * sizeof( int ) ); +} //end of the function AAS_OptimizeAlloc +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_OptimizeStore( optimized_t *optimized ) { + //store the optimized vertexes + if ( ( *aasworld ).vertexes ) { + FreeMemory( ( *aasworld ).vertexes ); + } + ( *aasworld ).vertexes = optimized->vertexes; + ( *aasworld ).numvertexes = optimized->numvertexes; + //store the optimized edges + if ( ( *aasworld ).edges ) { + FreeMemory( ( *aasworld ).edges ); + } + ( *aasworld ).edges = optimized->edges; + ( *aasworld ).numedges = optimized->numedges; + //store the optimized edge index + if ( ( *aasworld ).edgeindex ) { + FreeMemory( ( *aasworld ).edgeindex ); + } + ( *aasworld ).edgeindex = optimized->edgeindex; + ( *aasworld ).edgeindexsize = optimized->edgeindexsize; + //store the optimized faces + if ( ( *aasworld ).faces ) { + FreeMemory( ( *aasworld ).faces ); + } + ( *aasworld ).faces = optimized->faces; + ( *aasworld ).numfaces = optimized->numfaces; + //store the optimized face index + if ( ( *aasworld ).faceindex ) { + FreeMemory( ( *aasworld ).faceindex ); + } + ( *aasworld ).faceindex = optimized->faceindex; + ( *aasworld ).faceindexsize = optimized->faceindexsize; + //store the optimized areas + if ( ( *aasworld ).areas ) { + FreeMemory( ( *aasworld ).areas ); + } + ( *aasworld ).areas = optimized->areas; + ( *aasworld ).numareas = optimized->numareas; + //free optimize indexes + FreeMemory( optimized->vertexoptimizeindex ); + FreeMemory( optimized->edgeoptimizeindex ); + FreeMemory( optimized->faceoptimizeindex ); +} //end of the function AAS_OptimizeStore +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_Optimize( void ) { + int i, sign; + optimized_t optimized; + + AAS_OptimizeAlloc( &optimized ); + for ( i = 1; i < ( *aasworld ).numareas; i++ ) + { + AAS_OptimizeArea( &optimized, i ); + } //end for + //reset the reachability face pointers + for ( i = 0; i < ( *aasworld ).reachabilitysize; i++ ) + { + //NOTE: for TRAVEL_ELEVATOR the facenum is the model number of + // the elevator + if ( ( *aasworld ).reachability[i].traveltype == TRAVEL_ELEVATOR ) { + continue; + } + //NOTE: for TRAVEL_JUMPPAD the facenum is the Z velocity and the edgenum is the hor velocity + if ( ( *aasworld ).reachability[i].traveltype == TRAVEL_JUMPPAD ) { + continue; + } + //NOTE: for TRAVEL_FUNCBOB the facenum and edgenum contain other coded information + if ( ( *aasworld ).reachability[i].traveltype == TRAVEL_FUNCBOB ) { + continue; + } + // + sign = ( *aasworld ).reachability[i].facenum; + ( *aasworld ).reachability[i].facenum = optimized.faceoptimizeindex[abs( ( *aasworld ).reachability[i].facenum )]; + if ( sign < 0 ) { + ( *aasworld ).reachability[i].facenum = -( *aasworld ).reachability[i].facenum; + } + sign = ( *aasworld ).reachability[i].edgenum; + ( *aasworld ).reachability[i].edgenum = optimized.edgeoptimizeindex[abs( ( *aasworld ).reachability[i].edgenum )]; + if ( sign < 0 ) { + ( *aasworld ).reachability[i].edgenum = -( *aasworld ).reachability[i].edgenum; + } + } //end for + //store the optimized AAS data into (*aasworld) + AAS_OptimizeStore( &optimized ); + //print some nice stuff :) + botimport.Print( PRT_MESSAGE, "AAS data optimized.\n" ); +} //end of the function AAS_Optimize diff --git a/src/botlib/be_aas_optimize.h b/src/botlib/be_aas_optimize.h new file mode 100644 index 0000000..7ef522e --- /dev/null +++ b/src/botlib/be_aas_optimize.h @@ -0,0 +1,39 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_optimize.h + * + * desc: AAS + * + * + *****************************************************************************/ + +void AAS_Optimize( void ); + diff --git a/src/botlib/be_aas_reach.c b/src/botlib/be_aas_reach.c new file mode 100644 index 0000000..0970784 --- /dev/null +++ b/src/botlib/be_aas_reach.c @@ -0,0 +1,4480 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + + +/***************************************************************************** + * name: be_aas_reach.c + * + * desc: reachability calculations + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "l_log.h" +#include "l_memory.h" +#include "l_script.h" +#include "l_libvar.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "aasfile.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "be_aas_funcs.h" +#include "be_aas_def.h" + +extern int Sys_MilliSeconds( void ); + +//#include "../../../gladiator/bspc/aas_store.h" + +extern botlib_import_t botimport; + +//#define REACHDEBUG + +//NOTE: all travel times are in hundreth of a second +//maximum fall delta before getting damaged + +// Ridah, tweaked for Wolf AI +#define FALLDELTA_5DAMAGE 25 //40 +#define FALLDELTA_10DAMAGE 40 //60 +// done. + +//maximum number of reachability links +#define AAS_MAX_REACHABILITYSIZE 65536 +//number of areas reachability is calculated for each frame +#define REACHABILITYAREASPERCYCLE 15 +//number of units reachability points are placed inside the areas +#define INSIDEUNITS 2 + +// Ridah, tweaked this, routing issues around small areas +#define INSIDEUNITS_WALKEND 5 // original +//#define INSIDEUNITS_WALKEND 0.2 // new + +// Ridah, added this for better walking off ledges +#define INSIDEUNITS_WALKOFFLEDGEEND 15 + +#define INSIDEUNITS_WALKSTART 0.1 +#define INSIDEUNITS_WATERJUMP 15 +//travel times in hundreth of a second + +// Ridah, tweaked these for Wolf AI +#define REACH_MIN_TIME 4 // always at least this much time for a reachability +#define WATERJUMP_TIME 700 //7 seconds +#define TELEPORT_TIME 50 //0.5 seconds +#define BARRIERJUMP_TIME 900 //fixed value? +#define STARTCROUCH_TIME 300 //3 sec to start crouching +#define STARTGRAPPLE_TIME 500 //using the grapple costs a lot of time +#define STARTWALKOFFLEDGE_TIME 300 //3 seconds +#define STARTJUMP_TIME 500 //3 seconds for jumping + +#define FALLDAMAGE_5_TIME 400 //extra travel time when falling hurts +#define FALLDAMAGE_10_TIME 900 //extra travel time when falling hurts +// done. + +//maximum height the bot may fall down when jumping +#define MAX_JUMPFALLHEIGHT 450 +//area flag used for weapon jumping +#define AREA_WEAPONJUMP 8192 //valid area to weapon jump to +//number of reachabilities of each type +int reach_swim; //swim +int reach_equalfloor; //walk on floors with equal height +int reach_step; //step up +int reach_walk; //walk of step +int reach_barrier; //jump up to a barrier +int reach_waterjump; //jump out of water +int reach_walkoffledge; //walk of a ledge +int reach_jump; //jump +int reach_ladder; //climb or descent a ladder +int reach_teleport; //teleport +int reach_elevator; //use an elevator +int reach_funcbob; //use a func bob +int reach_grapple; //grapple hook +int reach_doublejump; //double jump +int reach_rampjump; //ramp jump +int reach_strafejump; //strafe jump (just normal jump but further) +int reach_rocketjump; //rocket jump +int reach_bfgjump; //bfg jump +int reach_jumppad; //jump pads +//if true grapple reachabilities are skipped +int calcgrapplereach; +//linked reachability +typedef struct aas_lreachability_s +{ + int areanum; //number of the reachable area + int facenum; //number of the face towards the other area + int edgenum; //number of the edge towards the other area + vec3_t start; //start point of inter area movement + vec3_t end; //end point of inter area movement + int traveltype; //type of travel required to get to the area + unsigned short int traveltime; //travel time of the inter area movement + // + struct aas_lreachability_s *next; +} aas_lreachability_t; +//temporary reachabilities +aas_lreachability_t *reachabilityheap; //heap with reachabilities +aas_lreachability_t *nextreachability; //next free reachability from the heap +aas_lreachability_t **areareachability; //reachability links for every area +int numlreachabilities; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_BestReachableLinkArea( aas_link_t *areas ) { + aas_link_t *link; + + for ( link = areas; link; link = link->next_area ) + { + if ( AAS_AreaGrounded( link->areanum ) || AAS_AreaSwim( link->areanum ) ) { + return link->areanum; + } //end if + } //end for + // + for ( link = areas; link; link = link->next_area ) + { + if ( link->areanum ) { + return link->areanum; + } + //FIXME: cannot enable next line right now because the reachability + // does not have to be calculated when the level items are loaded + //if (AAS_AreaReachability(link->areanum)) return link->areanum; + } //end for + return 0; +} //end of the function AAS_BestReachableLinkArea +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_BestReachableArea( vec3_t origin, vec3_t mins, vec3_t maxs, vec3_t goalorigin ) { + int areanum, i, j, k, l; + aas_link_t *areas; + vec3_t absmins, absmaxs; + //vec3_t bbmins, bbmaxs; + vec3_t start, end; + aas_trace_t trace; + + if ( !( *aasworld ).loaded ) { + botimport.Print( PRT_ERROR, "AAS_BestReachableArea: aas not loaded\n" ); + return 0; + } //end if + //find a point in an area + VectorCopy( origin, start ); + areanum = AAS_PointAreaNum( start ); + //while no area found fudge around a little + for ( i = 0; i < 5 && !areanum; i++ ) + { + for ( j = 0; j < 5 && !areanum; j++ ) + { + for ( k = -1; k <= 1 && !areanum; k++ ) + { + for ( l = -1; l <= 1 && !areanum; l++ ) + { + VectorCopy( origin, start ); + start[0] += (float) j * 4 * k; + start[1] += (float) j * 4 * l; + start[2] += (float) i * 4; + areanum = AAS_PointAreaNum( start ); + } //end for + } //end for + } //end for + } //end for + //if an area was found + if ( areanum ) { + //drop client bbox down and try again + VectorCopy( start, end ); + start[2] += 0.25; + end[2] -= 50; + trace = AAS_TraceClientBBox( start, end, PRESENCE_CROUCH, -1 ); + if ( !trace.startsolid ) { + areanum = AAS_PointAreaNum( trace.endpos ); + VectorCopy( trace.endpos, goalorigin ); + //FIXME: cannot enable next line right now because the reachability + // does not have to be calculated when the level items are loaded + //if the origin is in an area with reachability + //if (AAS_AreaReachability(areanum)) return areanum; + if ( areanum ) { + return areanum; + } + } //end if + else + { + //it can very well happen that the AAS_PointAreaNum function tells that + //a point is in an area and that starting a AAS_TraceClientBBox from that + //point will return trace.startsolid qtrue + /* + if (AAS_PointAreaNum(start)) + { + Log_Write("point %f %f %f in area %d but trace startsolid", start[0], start[1], start[2], areanum); + AAS_DrawPermanentCross(start, 4, LINECOLOR_RED); + } //end if + botimport.Print(PRT_MESSAGE, "AAS_BestReachableArea: start solid\n"); + */ + VectorCopy( start, goalorigin ); + return areanum; + } //end else + } //end if + // + //AAS_PresenceTypeBoundingBox(PRESENCE_CROUCH, bbmins, bbmaxs); + //NOTE: the goal origin does not have to be in the goal area + // because the bot will have to move towards the item origin anyway + VectorCopy( origin, goalorigin ); + // + VectorAdd( origin, mins, absmins ); + VectorAdd( origin, maxs, absmaxs ); + //add bounding box size + //VectorSubtract(absmins, bbmaxs, absmins); + //VectorSubtract(absmaxs, bbmins, absmaxs); + //link an invalid (-1) entity + areas = AAS_AASLinkEntity( absmins, absmaxs, -1 ); + //get the reachable link arae + areanum = AAS_BestReachableLinkArea( areas ); + //unlink the invalid entity + AAS_UnlinkFromAreas( areas ); + // + return areanum; +} //end of the function AAS_BestReachableArea +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_SetupReachabilityHeap( void ) { + int i; + + reachabilityheap = (aas_lreachability_t *) GetClearedMemory( + AAS_MAX_REACHABILITYSIZE * sizeof( aas_lreachability_t ) ); + for ( i = 0; i < AAS_MAX_REACHABILITYSIZE - 1; i++ ) + { + reachabilityheap[i].next = &reachabilityheap[i + 1]; + } //end for + reachabilityheap[AAS_MAX_REACHABILITYSIZE - 1].next = NULL; + nextreachability = reachabilityheap; + numlreachabilities = 0; +} //end of the function AAS_InitReachabilityHeap +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ShutDownReachabilityHeap( void ) { + FreeMemory( reachabilityheap ); + numlreachabilities = 0; +} //end of the function AAS_ShutDownReachabilityHeap +//=========================================================================== +// returns a reachability link +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +aas_lreachability_t *AAS_AllocReachability( void ) { + aas_lreachability_t *r; + + if ( !nextreachability ) { + return NULL; + } + //make sure the error message only shows up once + if ( !nextreachability->next ) { + AAS_Error( "AAS_MAX_REACHABILITYSIZE" ); + } + // + r = nextreachability; + nextreachability = nextreachability->next; + numlreachabilities++; + return r; +} //end of the function AAS_AllocReachability +//=========================================================================== +// frees a reachability link +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FreeReachability( aas_lreachability_t *lreach ) { + memset( lreach, 0, sizeof( aas_lreachability_t ) ); + + lreach->next = nextreachability; + nextreachability = lreach; + numlreachabilities--; +} //end of the function AAS_FreeReachability +//=========================================================================== +// returns qtrue if the area has reachability links +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaReachability( int areanum ) { + if ( areanum < 0 || areanum >= ( *aasworld ).numareas ) { + AAS_Error( "AAS_AreaReachability: areanum %d out of range", areanum ); + return 0; + } //end if + // RF, if this area is disabled, then fail + if ( ( *aasworld ).areasettings[areanum].areaflags & AREA_DISABLED ) { + return 0; + } + return ( *aasworld ).areasettings[areanum].numreachableareas; +} //end of the function AAS_AreaReachability +//=========================================================================== +// returns the surface area of the given face +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float AAS_FaceArea( aas_face_t *face ) { + int i, edgenum, side; + float total; + vec_t *v; + vec3_t d1, d2, cross; + aas_edge_t *edge; + + edgenum = ( *aasworld ).edgeindex[face->firstedge]; + side = edgenum < 0; + edge = &( *aasworld ).edges[abs( edgenum )]; + v = ( *aasworld ).vertexes[edge->v[side]]; + + total = 0; + for ( i = 1; i < face->numedges - 1; i++ ) + { + edgenum = ( *aasworld ).edgeindex[face->firstedge + i]; + side = edgenum < 0; + edge = &( *aasworld ).edges[abs( edgenum )]; + VectorSubtract( ( *aasworld ).vertexes[edge->v[side]], v, d1 ); + VectorSubtract( ( *aasworld ).vertexes[edge->v[!side]], v, d2 ); + CrossProduct( d1, d2, cross ); + total += 0.5 * VectorLength( cross ); + } //end for + return total; +} //end of the function AAS_FaceArea +//=========================================================================== +// returns the volume of an area +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float AAS_AreaVolume( int areanum ) { + int i, edgenum, facenum; + vec_t d, a, volume; + vec3_t corner; + aas_plane_t *plane; + aas_edge_t *edge; + aas_face_t *face; + aas_area_t *area; + + area = &( *aasworld ).areas[areanum]; + facenum = ( *aasworld ).faceindex[area->firstface]; + face = &( *aasworld ).faces[abs( facenum )]; + edgenum = ( *aasworld ).edgeindex[face->firstedge]; + edge = &( *aasworld ).edges[abs( edgenum )]; + // + VectorCopy( ( *aasworld ).vertexes[edge->v[0]], corner ); + + //make tetrahedrons to all other faces + volume = 0; + for ( i = 0; i < area->numfaces; i++ ) + { + facenum = abs( ( *aasworld ).faceindex[area->firstface + i] ); + face = &( *aasworld ).faces[facenum]; + plane = &( *aasworld ).planes[face->planenum]; + d = -( DotProduct( corner, plane->normal ) - plane->dist ); + a = AAS_FaceArea( face ); + volume += d * a; + } //end for + + volume /= 3; + return volume; +} //end of the function AAS_AreaVolume +//=========================================================================== +// returns the surface area of all ground faces together of the area +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float AAS_AreaGroundFaceArea( int areanum ) { + int i; + float total; + aas_area_t *area; + aas_face_t *face; + + total = 0; + area = &( *aasworld ).areas[areanum]; + for ( i = 0; i < area->numfaces; i++ ) + { + face = &( *aasworld ).faces[abs( ( *aasworld ).faceindex[area->firstface + i] )]; + if ( !( face->faceflags & FACE_GROUND ) ) { + continue; + } + // + total += AAS_FaceArea( face ); + } //end for + return total; +} //end of the function AAS_AreaGroundFaceArea +//=========================================================================== +// returns the center of a face +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FaceCenter( int facenum, vec3_t center ) { + int i; + float scale; + aas_face_t *face; + aas_edge_t *edge; + + face = &( *aasworld ).faces[facenum]; + + VectorClear( center ); + for ( i = 0; i < face->numedges; i++ ) + { + edge = &( *aasworld ).edges[abs( ( *aasworld ).edgeindex[face->firstedge + i] )]; + VectorAdd( center, ( *aasworld ).vertexes[edge->v[0]], center ); + VectorAdd( center, ( *aasworld ).vertexes[edge->v[1]], center ); + } //end for + scale = 0.5 / face->numedges; + VectorScale( center, scale, center ); +} //end of the function AAS_FaceCenter +//=========================================================================== +// returns the maximum distance a player can fall before being damaged +// damage = deltavelocity*deltavelocity * 0.0001 +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_FallDamageDistance( void ) { + float maxzvelocity, gravity, t; + + maxzvelocity = sqrt( 30 * 10000 ); + gravity = aassettings.sv_gravity; + t = maxzvelocity / gravity; + return 0.5 * gravity * t * t; +} //end of the function AAS_FallDamageDistance +//=========================================================================== +// distance = 0.5 * gravity * t * t +// vel = t * gravity +// damage = vel * vel * 0.0001 +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float AAS_FallDelta( float distance ) { + float t, delta, gravity; + + gravity = aassettings.sv_gravity; + t = sqrt( fabs( distance ) * 2 / gravity ); + delta = t * gravity; + return delta * delta * 0.0001; +} //end of the function AAS_FallDelta +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float AAS_MaxJumpHeight( float sv_jumpvel ) { + float sv_gravity; + + sv_gravity = aassettings.sv_gravity; + //maximum height a player can jump with the given initial z velocity + return 0.5 * sv_gravity * ( sv_jumpvel / sv_gravity ) * ( sv_jumpvel / sv_gravity ); +} //end of the function MaxJumpHeight +//=========================================================================== +// returns true if a player can only crouch in the area +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float AAS_MaxJumpDistance( float sv_jumpvel ) { + float sv_gravity, sv_maxvelocity, t; + + sv_gravity = aassettings.sv_gravity; + sv_maxvelocity = aassettings.sv_maxvelocity; + //time a player takes to fall the height + t = sqrt( MAX_JUMPFALLHEIGHT / ( 0.5 * sv_gravity ) ); + //maximum distance + return sv_maxvelocity * ( t + sv_jumpvel / sv_gravity ); +} //end of the function AAS_MaxJumpDistance +//=========================================================================== +// returns true if a player can only crouch in the area +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaCrouch( int areanum ) { + if ( !( ( *aasworld ).areasettings[areanum].presencetype & PRESENCE_NORMAL ) ) { + return qtrue; + } else { return qfalse;} +} //end of the function AAS_AreaCrouch +//=========================================================================== +// returns qtrue if it is possible to swim in the area +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaSwim( int areanum ) { + if ( ( *aasworld ).areasettings[areanum].areaflags & AREA_LIQUID ) { + return qtrue; + } else { return qfalse;} +} //end of the function AAS_AreaSwim +//=========================================================================== +// returns qtrue if the area contains a liquid +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaLiquid( int areanum ) { + if ( ( *aasworld ).areasettings[areanum].areaflags & AREA_LIQUID ) { + return qtrue; + } else { return qfalse;} +} //end of the function AAS_AreaLiquid +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaLava( int areanum ) { + return ( ( *aasworld ).areasettings[areanum].contents & AREACONTENTS_LAVA ); +} //end of the function AAS_AreaLava +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaSlime( int areanum ) { + return ( ( *aasworld ).areasettings[areanum].contents & AREACONTENTS_SLIME ); +} //end of the function AAS_AreaSlime +//=========================================================================== +// returns qtrue if the area contains ground faces +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaGrounded( int areanum ) { + return ( ( *aasworld ).areasettings[areanum].areaflags & AREA_GROUNDED ); +} //end of the function AAS_AreaGround +//=========================================================================== +// returns true if the area contains ladder faces +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaLadder( int areanum ) { + return ( ( *aasworld ).areasettings[areanum].areaflags & AREA_LADDER ); +} //end of the function AAS_AreaLadder +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaJumpPad( int areanum ) { + return ( ( *aasworld ).areasettings[areanum].contents & AREACONTENTS_JUMPPAD ); +} //end of the function AAS_AreaJumpPad +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaTeleporter( int areanum ) { + return ( ( *aasworld ).areasettings[areanum].contents & AREACONTENTS_TELEPORTER ); +} //end of the function AAS_AreaTeleporter +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaDoNotEnter( int areanum ) { + return ( ( *aasworld ).areasettings[areanum].contents & AREACONTENTS_DONOTENTER ); +} //end of the function AAS_AreaDoNotEnter +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaDoNotEnterLarge( int areanum ) { + return ( ( *aasworld ).areasettings[areanum].contents & AREACONTENTS_DONOTENTER_LARGE ); +} //end of the function AAS_AreaDoNotEnter +//=========================================================================== +// returns the time it takes perform a barrier jump +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +unsigned short int AAS_BarrierJumpTravelTime( void ) { + return aassettings.sv_jumpvel / ( aassettings.sv_gravity * 0.1 ); +} //end op the function AAS_BarrierJumpTravelTime +//=========================================================================== +// returns true if there already exists a reachability from area1 to area2 +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_ReachabilityExists( int area1num, int area2num ) { + aas_lreachability_t *r; + + for ( r = areareachability[area1num]; r; r = r->next ) + { + if ( r->areanum == area2num ) { + return qtrue; + } + } //end for + return qfalse; +} //end of the function AAS_ReachabilityExists +//=========================================================================== +// returns true if there is a solid just after the end point when going +// from start to end +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_NearbySolidOrGap( vec3_t start, vec3_t end ) { + vec3_t dir, testpoint; + int areanum; + + VectorSubtract( end, start, dir ); + dir[2] = 0; + VectorNormalize( dir ); + VectorMA( end, 48, dir, testpoint ); + + areanum = AAS_PointAreaNum( testpoint ); + if ( !areanum ) { + testpoint[2] += 16; + areanum = AAS_PointAreaNum( testpoint ); + if ( !areanum ) { + return qtrue; + } + } //end if + VectorMA( end, 64, dir, testpoint ); + areanum = AAS_PointAreaNum( testpoint ); + if ( areanum ) { + if ( !AAS_AreaSwim( areanum ) && !AAS_AreaGrounded( areanum ) ) { + return qtrue; + } + } //end if + return qfalse; +} //end of the function AAS_SolidGapTime +//=========================================================================== +// searches for swim reachabilities between adjacent areas +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_Reachability_Swim( int area1num, int area2num ) { + int i, j, face1num, face2num, side1; + aas_area_t *area1, *area2; + aas_areasettings_t *areasettings; + aas_lreachability_t *lreach; + aas_face_t *face1; + aas_plane_t *plane; + vec3_t start; + + if ( !AAS_AreaSwim( area1num ) || !AAS_AreaSwim( area2num ) ) { + return qfalse; + } + //if the second area is crouch only + if ( !( ( *aasworld ).areasettings[area2num].presencetype & PRESENCE_NORMAL ) ) { + return qfalse; + } + + area1 = &( *aasworld ).areas[area1num]; + area2 = &( *aasworld ).areas[area2num]; + + //if the areas are not near anough + for ( i = 0; i < 3; i++ ) + { + if ( area1->mins[i] > area2->maxs[i] + 10 ) { + return qfalse; + } + if ( area1->maxs[i] < area2->mins[i] - 10 ) { + return qfalse; + } + } //end for + //find a shared face and create a reachability link + for ( i = 0; i < area1->numfaces; i++ ) + { + face1num = ( *aasworld ).faceindex[area1->firstface + i]; + side1 = face1num < 0; + face1num = abs( face1num ); + // + for ( j = 0; j < area2->numfaces; j++ ) + { + face2num = abs( ( *aasworld ).faceindex[area2->firstface + j] ); + // + if ( face1num == face2num ) { + AAS_FaceCenter( face1num, start ); + // + if ( AAS_PointContents( start ) & ( CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER ) ) { + // + face1 = &( *aasworld ).faces[face1num]; + areasettings = &( *aasworld ).areasettings[area1num]; + //create a new reachability link + lreach = AAS_AllocReachability(); + if ( !lreach ) { + return qfalse; + } + lreach->areanum = area2num; + lreach->facenum = face1num; + lreach->edgenum = 0; + VectorCopy( start, lreach->start ); + plane = &( *aasworld ).planes[face1->planenum ^ side1]; + VectorMA( lreach->start, INSIDEUNITS, plane->normal, lreach->end ); + lreach->traveltype = TRAVEL_SWIM; + lreach->traveltime = 1; + //if the volume of the area is rather small + if ( AAS_AreaVolume( area2num ) < 800 ) { + lreach->traveltime += 200; + } + //if (!(AAS_PointContents(start) & MASK_WATER)) lreach->traveltime += 500; + //link the reachability + lreach->next = areareachability[area1num]; + areareachability[area1num] = lreach; + reach_swim++; + return qtrue; + } //end if + } //end if + } //end for + } //end for + return qfalse; +} //end of the function AAS_Reachability_Swim +//=========================================================================== +// searches for reachabilities between adjacent areas with equal floor +// heights +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_Reachability_EqualFloorHeight( int area1num, int area2num ) { + int i, j, edgenum, edgenum1, edgenum2, foundreach, side; + float height, bestheight, length, bestlength; + vec3_t dir, start, end, normal, invgravity, gravitydirection = {0, 0, -1}; + vec3_t edgevec; + aas_area_t *area1, *area2; + aas_face_t *face1, *face2; + aas_edge_t *edge; + aas_plane_t *plane2; + aas_lreachability_t lr, *lreach; + + if ( !AAS_AreaGrounded( area1num ) || !AAS_AreaGrounded( area2num ) ) { + return qfalse; + } + + area1 = &( *aasworld ).areas[area1num]; + area2 = &( *aasworld ).areas[area2num]; + //if the areas are not near anough in the x-y direction + for ( i = 0; i < 2; i++ ) + { + if ( area1->mins[i] > area2->maxs[i] + 10 ) { + return qfalse; + } + if ( area1->maxs[i] < area2->mins[i] - 10 ) { + return qfalse; + } + } //end for + //if area 2 is too high above area 1 + if ( area2->mins[2] > area1->maxs[2] ) { + return qfalse; + } + // + VectorCopy( gravitydirection, invgravity ); + VectorInverse( invgravity ); + // + bestheight = 99999; + bestlength = 0; + foundreach = qfalse; + memset( &lr, 0, sizeof( aas_lreachability_t ) ); //make the compiler happy + // + //check if the areas have ground faces with a common edge + //if existing use the lowest common edge for a reachability link + for ( i = 0; i < area1->numfaces; i++ ) + { + face1 = &( *aasworld ).faces[abs( ( *aasworld ).faceindex[area1->firstface + i] )]; + if ( !( face1->faceflags & FACE_GROUND ) ) { + continue; + } + // + for ( j = 0; j < area2->numfaces; j++ ) + { + face2 = &( *aasworld ).faces[abs( ( *aasworld ).faceindex[area2->firstface + j] )]; + if ( !( face2->faceflags & FACE_GROUND ) ) { + continue; + } + //if there is a common edge + for ( edgenum1 = 0; edgenum1 < face1->numedges; edgenum1++ ) + { + for ( edgenum2 = 0; edgenum2 < face2->numedges; edgenum2++ ) + { + if ( abs( ( *aasworld ).edgeindex[face1->firstedge + edgenum1] ) != + abs( ( *aasworld ).edgeindex[face2->firstedge + edgenum2] ) ) { + continue; + } + edgenum = ( *aasworld ).edgeindex[face1->firstedge + edgenum1]; + side = edgenum < 0; + edge = &( *aasworld ).edges[abs( edgenum )]; + //get the length of the edge + VectorSubtract( ( *aasworld ).vertexes[edge->v[1]], + ( *aasworld ).vertexes[edge->v[0]], dir ); + length = VectorLength( dir ); + //get the start point + VectorAdd( ( *aasworld ).vertexes[edge->v[0]], + ( *aasworld ).vertexes[edge->v[1]], start ); + VectorScale( start, 0.5, start ); + VectorCopy( start, end ); + //get the end point several units inside area2 + //and the start point several units inside area1 + //NOTE: normal is pointing into area2 because the + //face edges are stored counter clockwise + VectorSubtract( ( *aasworld ).vertexes[edge->v[side]], + ( *aasworld ).vertexes[edge->v[!side]], edgevec ); + plane2 = &( *aasworld ).planes[face2->planenum]; + CrossProduct( edgevec, plane2->normal, normal ); + VectorNormalize( normal ); + // + //VectorMA(start, -1, normal, start); + VectorMA( end, INSIDEUNITS_WALKEND, normal, end ); + VectorMA( start, INSIDEUNITS_WALKSTART, normal, start ); + end[2] += 0.125; + // + height = DotProduct( invgravity, start ); + //NOTE: if there's nearby solid or a gap area after this area + //disabled this crap + //if (AAS_NearbySolidOrGap(start, end)) height += 200; + //NOTE: disabled because it disables reachabilities to very small areas + //if (AAS_PointAreaNum(end) != area2num) continue; + //get the longest lowest edge + if ( height < bestheight || + ( height < bestheight + 1 && length > bestlength ) ) { + bestheight = height; + bestlength = length; + //create a new reachability link + lr.areanum = area2num; + lr.facenum = 0; + lr.edgenum = edgenum; + VectorCopy( start, lr.start ); + VectorCopy( end, lr.end ); + lr.traveltype = TRAVEL_WALK; + lr.traveltime = 1; + foundreach = qtrue; + } //end if + } //end for + } //end for + } //end for + } //end for + if ( foundreach ) { + //create a new reachability link + lreach = AAS_AllocReachability(); + if ( !lreach ) { + return qfalse; + } + lreach->areanum = lr.areanum; + lreach->facenum = lr.facenum; + lreach->edgenum = lr.edgenum; + VectorCopy( lr.start, lreach->start ); + VectorCopy( lr.end, lreach->end ); + lreach->traveltype = lr.traveltype; + lreach->traveltime = lr.traveltime; + lreach->next = areareachability[area1num]; + areareachability[area1num] = lreach; + //if going into a crouch area + if ( !AAS_AreaCrouch( area1num ) && AAS_AreaCrouch( area2num ) ) { + lreach->traveltime += STARTCROUCH_TIME; + } //end if + /* + //NOTE: if there's nearby solid or a gap area after this area + if (!AAS_NearbySolidOrGap(lreach->start, lreach->end)) + { + lreach->traveltime += 100; + } //end if + */ + //avoid rather small areas + //if (AAS_AreaGroundFaceArea(lreach->areanum) < 500) lreach->traveltime += 100; + // + reach_equalfloor++; + return qtrue; + } //end if + return qfalse; +} //end of the function AAS_Reachability_EqualFloorHeight +//=========================================================================== +// searches step, barrier, waterjump and walk off ledge reachabilities +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_Reachability_Step_Barrier_WaterJump_WalkOffLedge( int area1num, int area2num ) { + int i, j, k, l, edge1num, edge2num; + int ground_bestarea2groundedgenum, ground_foundreach; + int water_bestarea2groundedgenum, water_foundreach; + int side1, area1swim, faceside1, groundface1num; + float dist, dist1, dist2, diff, invgravitydot, ortdot; + float x1, x2, x3, x4, y1, y2, y3, y4, tmp, y; + float length, ground_bestlength, water_bestlength, ground_bestdist, water_bestdist; + vec3_t v1, v2, v3, v4, tmpv, p1area1, p1area2, p2area1, p2area2; + vec3_t normal, ort, edgevec, start, end, dir; + vec3_t ground_beststart, ground_bestend, ground_bestnormal; + vec3_t water_beststart, water_bestend, water_bestnormal; + vec3_t invgravity = {0, 0, 1}; + vec3_t testpoint; + aas_plane_t *plane; + aas_area_t *area1, *area2; + aas_face_t *groundface1, *groundface2, *ground_bestface1, *water_bestface1; + aas_edge_t *edge1, *edge2; + aas_lreachability_t *lreach; + aas_trace_t trace; + + //must be able to walk or swim in the first area + if ( !AAS_AreaGrounded( area1num ) && !AAS_AreaSwim( area1num ) ) { + return qfalse; + } + // + if ( !AAS_AreaGrounded( area2num ) && !AAS_AreaSwim( area2num ) ) { + return qfalse; + } + // + area1 = &( *aasworld ).areas[area1num]; + area2 = &( *aasworld ).areas[area2num]; + //if the first area contains a liquid + area1swim = AAS_AreaSwim( area1num ); + //if the areas are not near anough in the x-y direction + for ( i = 0; i < 2; i++ ) + { + if ( area1->mins[i] > area2->maxs[i] + 10 ) { + return qfalse; + } + if ( area1->maxs[i] < area2->mins[i] - 10 ) { + return qfalse; + } + } //end for + // + ground_foundreach = qfalse; + ground_bestdist = 99999; + ground_bestlength = 0; + ground_bestarea2groundedgenum = 0; + // + water_foundreach = qfalse; + water_bestdist = 99999; + water_bestlength = 0; + water_bestarea2groundedgenum = 0; + // + for ( i = 0; i < area1->numfaces; i++ ) + { + groundface1num = ( *aasworld ).faceindex[area1->firstface + i]; + faceside1 = groundface1num < 0; + groundface1 = &( *aasworld ).faces[abs( groundface1num )]; + //if this isn't a ground face + if ( !( groundface1->faceflags & FACE_GROUND ) ) { + //if we can swim in the first area + if ( area1swim ) { + //face plane must be more or less horizontal + plane = &( *aasworld ).planes[groundface1->planenum ^ ( !faceside1 )]; + if ( DotProduct( plane->normal, invgravity ) < 0.7 ) { + continue; + } + } //end if + else + { + //if we can't swim in the area it must be a ground face + continue; + } //end else + } //end if + // + for ( k = 0; k < groundface1->numedges; k++ ) + { + edge1num = ( *aasworld ).edgeindex[groundface1->firstedge + k]; + side1 = ( edge1num < 0 ); + //NOTE: for water faces we must take the side area 1 is + // on into account because the face is shared and doesn't + // have to be oriented correctly + if ( !( groundface1->faceflags & FACE_GROUND ) ) { + side1 = ( side1 == faceside1 ); + } + edge1num = abs( edge1num ); + edge1 = &( *aasworld ).edges[edge1num]; + //vertexes of the edge + VectorCopy( ( *aasworld ).vertexes[edge1->v[!side1]], v1 ); + VectorCopy( ( *aasworld ).vertexes[edge1->v[side1]], v2 ); + //get a vertical plane through the edge + //NOTE: normal is pointing into area 2 because the + //face edges are stored counter clockwise + VectorSubtract( v2, v1, edgevec ); + CrossProduct( edgevec, invgravity, normal ); + VectorNormalize( normal ); + dist = DotProduct( normal, v1 ); + //check the faces from the second area + for ( j = 0; j < area2->numfaces; j++ ) + { + groundface2 = &( *aasworld ).faces[abs( ( *aasworld ).faceindex[area2->firstface + j] )]; + //must be a ground face + if ( !( groundface2->faceflags & FACE_GROUND ) ) { + continue; + } + //check the edges of this ground face + for ( l = 0; l < groundface2->numedges; l++ ) + { + edge2num = abs( ( *aasworld ).edgeindex[groundface2->firstedge + l] ); + edge2 = &( *aasworld ).edges[edge2num]; + //vertexes of the edge + VectorCopy( ( *aasworld ).vertexes[edge2->v[0]], v3 ); + VectorCopy( ( *aasworld ).vertexes[edge2->v[1]], v4 ); + //check the distance between the two points and the vertical plane + //through the edge of area1 + diff = DotProduct( normal, v3 ) - dist; + if ( diff < -0.1 || diff > 0.1 ) { + continue; + } + diff = DotProduct( normal, v4 ) - dist; + if ( diff < -0.1 || diff > 0.1 ) { + continue; + } + // + //project the two ground edges into the step side plane + //and calculate the shortest distance between the two + //edges if they overlap in the direction orthogonal to + //the gravity direction + CrossProduct( invgravity, normal, ort ); + invgravitydot = DotProduct( invgravity, invgravity ); + ortdot = DotProduct( ort, ort ); + //projection into the step plane + //NOTE: since gravity is vertical this is just the z coordinate + y1 = v1[2]; //DotProduct(v1, invgravity) / invgravitydot; + y2 = v2[2]; //DotProduct(v2, invgravity) / invgravitydot; + y3 = v3[2]; //DotProduct(v3, invgravity) / invgravitydot; + y4 = v4[2]; //DotProduct(v4, invgravity) / invgravitydot; + // + x1 = DotProduct( v1, ort ) / ortdot; + x2 = DotProduct( v2, ort ) / ortdot; + x3 = DotProduct( v3, ort ) / ortdot; + x4 = DotProduct( v4, ort ) / ortdot; + // + if ( x1 > x2 ) { + tmp = x1; x1 = x2; x2 = tmp; + tmp = y1; y1 = y2; y2 = tmp; + VectorCopy( v1, tmpv ); VectorCopy( v2, v1 ); VectorCopy( tmpv, v2 ); + } //end if + if ( x3 > x4 ) { + tmp = x3; x3 = x4; x4 = tmp; + tmp = y3; y3 = y4; y4 = tmp; + VectorCopy( v3, tmpv ); VectorCopy( v4, v3 ); VectorCopy( tmpv, v4 ); + } //end if + //if the two projected edge lines have no overlap + if ( x2 <= x3 || x4 <= x1 ) { +// Log_Write("lines no overlap: from area %d to %d\r\n", area1num, area2num); + continue; + } //end if + //if the two lines fully overlap + if ( ( x1 - 0.5 < x3 && x4 < x2 + 0.5 ) && + ( x3 - 0.5 < x1 && x2 < x4 + 0.5 ) ) { + dist1 = y3 - y1; + dist2 = y4 - y2; + VectorCopy( v1, p1area1 ); + VectorCopy( v2, p2area1 ); + VectorCopy( v3, p1area2 ); + VectorCopy( v4, p2area2 ); + } //end if + else + { + //if the points are equal + if ( x1 > x3 - 0.1 && x1 < x3 + 0.1 ) { + dist1 = y3 - y1; + VectorCopy( v1, p1area1 ); + VectorCopy( v3, p1area2 ); + } //end if + else if ( x1 < x3 ) { + y = y1 + ( x3 - x1 ) * ( y2 - y1 ) / ( x2 - x1 ); + dist1 = y3 - y; + VectorCopy( v3, p1area1 ); + p1area1[2] = y; + VectorCopy( v3, p1area2 ); + } //end if + else + { + y = y3 + ( x1 - x3 ) * ( y4 - y3 ) / ( x4 - x3 ); + dist1 = y - y1; + VectorCopy( v1, p1area1 ); + VectorCopy( v1, p1area2 ); + p1area2[2] = y; + } //end if + //if the points are equal + if ( x2 > x4 - 0.1 && x2 < x4 + 0.1 ) { + dist2 = y4 - y2; + VectorCopy( v2, p2area1 ); + VectorCopy( v4, p2area2 ); + } //end if + else if ( x2 < x4 ) { + y = y3 + ( x2 - x3 ) * ( y4 - y3 ) / ( x4 - x3 ); + dist2 = y - y2; + VectorCopy( v2, p2area1 ); + VectorCopy( v2, p2area2 ); + p2area2[2] = y; + } //end if + else + { + y = y1 + ( x4 - x1 ) * ( y2 - y1 ) / ( x2 - x1 ); + dist2 = y4 - y; + VectorCopy( v4, p2area1 ); + p2area1[2] = y; + VectorCopy( v4, p2area2 ); + } //end else + } //end else + //if both distances are pretty much equal + //then we take the middle of the points + if ( dist1 > dist2 - 1 && dist1 < dist2 + 1 ) { + dist = dist1; + VectorAdd( p1area1, p2area1, start ); + VectorScale( start, 0.5, start ); + VectorAdd( p1area2, p2area2, end ); + VectorScale( end, 0.5, end ); + } //end if + else if ( dist1 < dist2 ) { + dist = dist1; + VectorCopy( p1area1, start ); + VectorCopy( p1area2, end ); + } //end else if + else + { + dist = dist2; + VectorCopy( p2area1, start ); + VectorCopy( p2area2, end ); + } //end else + //get the length of the overlapping part of the edges of the two areas + VectorSubtract( p2area2, p1area2, dir ); + length = VectorLength( dir ); + // + if ( groundface1->faceflags & FACE_GROUND ) { + //if the vertical distance is smaller + if ( dist < ground_bestdist || + //or the vertical distance is pretty much the same + //but the overlapping part of the edges is longer + ( dist < ground_bestdist + 1 && length > ground_bestlength ) ) { + ground_bestdist = dist; + ground_bestlength = length; + ground_foundreach = qtrue; + ground_bestarea2groundedgenum = edge1num; + ground_bestface1 = groundface1; + //best point towards area1 + VectorCopy( start, ground_beststart ); + //normal is pointing into area2 + VectorCopy( normal, ground_bestnormal ); + //best point towards area2 + VectorCopy( end, ground_bestend ); + } //end if + } //end if + else + { + //if the vertical distance is smaller + if ( dist < water_bestdist || + //or the vertical distance is pretty much the same + //but the overlapping part of the edges is longer + ( dist < water_bestdist + 1 && length > water_bestlength ) ) { + water_bestdist = dist; + water_bestlength = length; + water_foundreach = qtrue; + water_bestarea2groundedgenum = edge1num; + water_bestface1 = groundface1; + //best point towards area1 + VectorCopy( start, water_beststart ); + //normal is pointing into area2 + VectorCopy( normal, water_bestnormal ); + //best point towards area2 + VectorCopy( end, water_bestend ); + } //end if + } //end else + } //end for + } //end for + } //end for + } //end for + // + // NOTE: swim reachabilities are already filtered out + // + // Steps + // + // --------- + // | step height -> TRAVEL_WALK + //--------| + // + // --------- + //~~~~~~~~| step height and low water -> TRAVEL_WALK + //--------| + // + //~~~~~~~~~~~~~~~~~~ + // --------- + // | step height and low water up to the step -> TRAVEL_WALK + //--------| + // + //check for a step reachability + if ( ground_foundreach ) { + //if area2 is higher but lower than the maximum step height + //NOTE: ground_bestdist >= 0 also catches equal floor reachabilities + if ( ground_bestdist >= 0 && ground_bestdist < aassettings.sv_maxstep ) { + //create walk reachability from area1 to area2 + lreach = AAS_AllocReachability(); + if ( !lreach ) { + return qfalse; + } + lreach->areanum = area2num; + lreach->facenum = 0; + lreach->edgenum = ground_bestarea2groundedgenum; + VectorMA( ground_beststart, INSIDEUNITS_WALKSTART, ground_bestnormal, lreach->start ); + VectorMA( ground_bestend, INSIDEUNITS_WALKEND, ground_bestnormal, lreach->end ); + lreach->traveltype = TRAVEL_WALK; + lreach->traveltime = 0; //1; + //if going into a crouch area + if ( !AAS_AreaCrouch( area1num ) && AAS_AreaCrouch( area2num ) ) { + lreach->traveltime += STARTCROUCH_TIME; + } //end if + lreach->next = areareachability[area1num]; + areareachability[area1num] = lreach; + //NOTE: if there's nearby solid or a gap area after this area + /* + if (!AAS_NearbySolidOrGap(lreach->start, lreach->end)) + { + lreach->traveltime += 100; + } //end if + */ + //avoid rather small areas + //if (AAS_AreaGroundFaceArea(lreach->areanum) < 500) lreach->traveltime += 100; + // + reach_step++; + return qtrue; + } //end if + } //end if + // + // Water Jumps + // + // --------- + // | + //~~~~~~~~| + // | + // | higher than step height and water up to waterjump height -> TRAVEL_WATERJUMP + //--------| + // + //~~~~~~~~~~~~~~~~~~ + // --------- + // | + // | + // | + // | higher than step height and low water up to the step -> TRAVEL_WATERJUMP + //--------| + // + //check for a waterjump reachability + if ( water_foundreach ) { + //get a test point a little bit towards area1 + VectorMA( water_bestend, -INSIDEUNITS, water_bestnormal, testpoint ); + //go down the maximum waterjump height + testpoint[2] -= aassettings.sv_maxwaterjump; + //if there IS water the sv_maxwaterjump height below the bestend point + if ( ( *aasworld ).areasettings[AAS_PointAreaNum( testpoint )].areaflags & AREA_LIQUID ) { + //don't create rediculous water jump reachabilities from areas very far below + //the water surface + if ( water_bestdist < aassettings.sv_maxwaterjump + 24 ) { + //waterjumping from or towards a crouch only area is not possible in Quake2 + if ( ( ( *aasworld ).areasettings[area1num].presencetype & PRESENCE_NORMAL ) && + ( ( *aasworld ).areasettings[area2num].presencetype & PRESENCE_NORMAL ) ) { + //create water jump reachability from area1 to area2 + lreach = AAS_AllocReachability(); + if ( !lreach ) { + return qfalse; + } + lreach->areanum = area2num; + lreach->facenum = 0; + lreach->edgenum = water_bestarea2groundedgenum; + VectorCopy( water_beststart, lreach->start ); + VectorMA( water_bestend, INSIDEUNITS_WATERJUMP, water_bestnormal, lreach->end ); + lreach->traveltype = TRAVEL_WATERJUMP; + lreach->traveltime = WATERJUMP_TIME; + lreach->next = areareachability[area1num]; + areareachability[area1num] = lreach; + //we've got another waterjump reachability + reach_waterjump++; + return qtrue; + } //end if + } //end if + } //end if + } //end if + // + // Barrier Jumps + // + // --------- + // | + // | + // | + // | higher than step height lower than barrier height -> TRAVEL_BARRIERJUMP + //--------| + // + // --------- + // | + // | + // | + //~~~~~~~~| higher than step height lower than barrier height + //--------| and a thin layer of water in the area to jump from -> TRAVEL_BARRIERJUMP + // + //check for a barrier jump reachability + if ( ground_foundreach ) { + //if area2 is higher but lower than the maximum barrier jump height + if ( ground_bestdist > 0 && ground_bestdist < aassettings.sv_maxbarrier ) { + //if no water in area1 or a very thin layer of water on the ground + if ( !water_foundreach || ( ground_bestdist - water_bestdist < 16 ) ) { + //cannot perform a barrier jump towards or from a crouch area in Quake2 + if ( !AAS_AreaCrouch( area1num ) && !AAS_AreaCrouch( area2num ) ) { + //create barrier jump reachability from area1 to area2 + lreach = AAS_AllocReachability(); + if ( !lreach ) { + return qfalse; + } + lreach->areanum = area2num; + lreach->facenum = 0; + lreach->edgenum = ground_bestarea2groundedgenum; + VectorMA( ground_beststart, INSIDEUNITS_WALKSTART, ground_bestnormal, lreach->start ); + VectorMA( ground_bestend, INSIDEUNITS_WALKEND, ground_bestnormal, lreach->end ); + lreach->traveltype = TRAVEL_BARRIERJUMP; + lreach->traveltime = BARRIERJUMP_TIME; //AAS_BarrierJumpTravelTime(); + lreach->next = areareachability[area1num]; + areareachability[area1num] = lreach; + //we've got another barrierjump reachability + reach_barrier++; + return qtrue; + } //end if + } //end if + } //end if + } //end if + // + // Walk and Walk Off Ledge + // + //--------| + // | can walk or step back -> TRAVEL_WALK + // --------- + // + //--------| + // | + // | + // | + // | cannot walk/step back -> TRAVEL_WALKOFFLEDGE + // --------- + // + //--------| + // | + // |~~~~~~~~ + // | + // | cannot step back but can waterjump back -> TRAVEL_WALKOFFLEDGE + // --------- FIXME: create TRAVEL_WALK reach?? + // + //check for a walk or walk off ledge reachability + if ( ground_foundreach ) { + if ( ground_bestdist < 0 ) { + if ( ground_bestdist > -aassettings.sv_maxstep ) { + //create walk reachability from area1 to area2 + lreach = AAS_AllocReachability(); + if ( !lreach ) { + return qfalse; + } + lreach->areanum = area2num; + lreach->facenum = 0; + lreach->edgenum = ground_bestarea2groundedgenum; + VectorMA( ground_beststart, INSIDEUNITS_WALKSTART, ground_bestnormal, lreach->start ); + + // Ridah +// VectorMA(ground_bestend, INSIDEUNITS_WALKEND, ground_bestnormal, lreach->end); + VectorMA( ground_bestend, INSIDEUNITS_WALKOFFLEDGEEND, ground_bestnormal, lreach->end ); + + lreach->traveltype = TRAVEL_WALK; + lreach->traveltime = 1; + lreach->next = areareachability[area1num]; + areareachability[area1num] = lreach; + //we've got another walk reachability + reach_walk++; + return qtrue; + } //end if + //trace a bounding box vertically to check for solids + VectorMA( ground_bestend, INSIDEUNITS, ground_bestnormal, ground_bestend ); + VectorCopy( ground_bestend, start ); + start[2] = ground_beststart[2]; + VectorCopy( ground_bestend, end ); + end[2] += 4; + trace = AAS_TraceClientBBox( start, end, PRESENCE_NORMAL, -1 ); + //if no solids were found + if ( !trace.startsolid && trace.fraction >= 1.0 ) { + //the trace end point must be in the goal area + trace.endpos[2] += 1; + if ( AAS_PointAreaNum( trace.endpos ) == area2num ) { + //create a walk off ledge reachability from area1 to area2 + lreach = AAS_AllocReachability(); + if ( !lreach ) { + return qfalse; + } + lreach->areanum = area2num; + lreach->facenum = 0; + lreach->edgenum = ground_bestarea2groundedgenum; + VectorCopy( ground_beststart, lreach->start ); + VectorCopy( ground_bestend, lreach->end ); + lreach->traveltype = TRAVEL_WALKOFFLEDGE; + lreach->traveltime = STARTWALKOFFLEDGE_TIME + fabs( ground_bestdist ) * 50 / aassettings.sv_gravity; + //if falling from too high and not falling into water + if ( !AAS_AreaSwim( area2num ) && !AAS_AreaJumpPad( area2num ) ) { + if ( AAS_FallDelta( ground_bestdist ) > FALLDELTA_5DAMAGE ) { + lreach->traveltime += FALLDAMAGE_5_TIME; + } //end if + if ( AAS_FallDelta( ground_bestdist ) > FALLDELTA_10DAMAGE ) { + lreach->traveltime += FALLDAMAGE_10_TIME; + } //end if + } //end if + lreach->next = areareachability[area1num]; + areareachability[area1num] = lreach; + // + reach_walkoffledge++; + //NOTE: don't create a weapon (rl, bfg) jump reachability here + //because it interferes with other reachabilities + //like the ladder reachability + return qtrue; + } //end if + } //end if + } //end else + } //end if + return qfalse; +} //end of the function AAS_Reachability_Step_Barrier_WaterJump_WalkOffLedge +//=========================================================================== +// returns the distance between the two vectors +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +/* Ridah, moved to q_math.c +float VectorDistance(vec3_t v1, vec3_t v2) +{ + vec3_t dir; + + VectorSubtract(v2, v1, dir); + return VectorLength(dir); +} //end of the function VectorDistance +*/ +//=========================================================================== +// returns true if the first vector is between the last two vectors +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int VectorBetweenVectors( vec3_t v, vec3_t v1, vec3_t v2 ) { + vec3_t dir1, dir2; + + VectorSubtract( v, v1, dir1 ); + VectorSubtract( v, v2, dir2 ); + return ( DotProduct( dir1, dir2 ) <= 0 ); +} //end of the function VectorBetweenVectors +//=========================================================================== +// returns the mid point between the two vectors +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void VectorMiddle( vec3_t v1, vec3_t v2, vec3_t middle ) { + VectorAdd( v1, v2, middle ); + VectorScale( middle, 0.5, middle ); +} //end of the function VectorMiddle +//=========================================================================== +// calculate a range of points closest to each other on both edges +// +// Parameter: beststart1 start of the range of points on edge v1-v2 +// beststart2 end of the range of points on edge v1-v2 +// bestend1 start of the range of points on edge v3-v4 +// bestend2 end of the range of points on edge v3-v4 +// bestdist best distance so far +// Returns: - +// Changes Globals: - +//=========================================================================== +/* +float AAS_ClosestEdgePoints(vec3_t v1, vec3_t v2, vec3_t v3, vec3_t v4, + aas_plane_t *plane1, aas_plane_t *plane2, + vec3_t beststart, vec3_t bestend, float bestdist) +{ + vec3_t dir1, dir2, p1, p2, p3, p4; + float a1, a2, b1, b2, dist; + int founddist; + + //edge vectors + VectorSubtract(v2, v1, dir1); + VectorSubtract(v4, v3, dir2); + //get the horizontal directions + dir1[2] = 0; + dir2[2] = 0; + // + // p1 = point on an edge vector of area2 closest to v1 + // p2 = point on an edge vector of area2 closest to v2 + // p3 = point on an edge vector of area1 closest to v3 + // p4 = point on an edge vector of area1 closest to v4 + // + if (dir2[0]) + { + a2 = dir2[1] / dir2[0]; + b2 = v3[1] - a2 * v3[0]; + //point on the edge vector of area2 closest to v1 + p1[0] = (DotProduct(v1, dir2) - (a2 * dir2[0] + b2 * dir2[1])) / dir2[0]; + p1[1] = a2 * p1[0] + b2; + //point on the edge vector of area2 closest to v2 + p2[0] = (DotProduct(v2, dir2) - (a2 * dir2[0] + b2 * dir2[1])) / dir2[0]; + p2[1] = a2 * p2[0] + b2; + } //end if + else + { + //point on the edge vector of area2 closest to v1 + p1[0] = v3[0]; + p1[1] = v1[1]; + //point on the edge vector of area2 closest to v2 + p2[0] = v3[0]; + p2[1] = v2[1]; + } //end else + // + if (dir1[0]) + { + // + a1 = dir1[1] / dir1[0]; + b1 = v1[1] - a1 * v1[0]; + //point on the edge vector of area1 closest to v3 + p3[0] = (DotProduct(v3, dir1) - (a1 * dir1[0] + b1 * dir1[1])) / dir1[0]; + p3[1] = a1 * p3[0] + b1; + //point on the edge vector of area1 closest to v4 + p4[0] = (DotProduct(v4, dir1) - (a1 * dir1[0] + b1 * dir1[1])) / dir1[0]; + p4[1] = a1 * p4[0] + b1; + } //end if + else + { + //point on the edge vector of area1 closest to v3 + p3[0] = v1[0]; + p3[1] = v3[1]; + //point on the edge vector of area1 closest to v4 + p4[0] = v1[0]; + p4[1] = v4[1]; + } //end else + //start with zero z-coordinates + p1[2] = 0; + p2[2] = 0; + p3[2] = 0; + p4[2] = 0; + //calculate the z-coordinates from the ground planes + p1[2] = (plane2->dist - DotProduct(plane2->normal, p1)) / plane2->normal[2]; + p2[2] = (plane2->dist - DotProduct(plane2->normal, p2)) / plane2->normal[2]; + p3[2] = (plane1->dist - DotProduct(plane1->normal, p3)) / plane1->normal[2]; + p4[2] = (plane1->dist - DotProduct(plane1->normal, p4)) / plane1->normal[2]; + // + founddist = qfalse; + // + if (VectorBetweenVectors(p1, v3, v4)) + { + dist = VectorDistance(v1, p1); + if (dist > bestdist - 0.5 && dist < bestdist + 0.5) + { + VectorMiddle(beststart, v1, beststart); + VectorMiddle(bestend, p1, bestend); + } //end if + else if (dist < bestdist) + { + bestdist = dist; + VectorCopy(v1, beststart); + VectorCopy(p1, bestend); + } //end if + founddist = qtrue; + } //end if + if (VectorBetweenVectors(p2, v3, v4)) + { + dist = VectorDistance(v2, p2); + if (dist > bestdist - 0.5 && dist < bestdist + 0.5) + { + VectorMiddle(beststart, v2, beststart); + VectorMiddle(bestend, p2, bestend); + } //end if + else if (dist < bestdist) + { + bestdist = dist; + VectorCopy(v2, beststart); + VectorCopy(p2, bestend); + } //end if + founddist = qtrue; + } //end else if + if (VectorBetweenVectors(p3, v1, v2)) + { + dist = VectorDistance(v3, p3); + if (dist > bestdist - 0.5 && dist < bestdist + 0.5) + { + VectorMiddle(beststart, p3, beststart); + VectorMiddle(bestend, v3, bestend); + } //end if + else if (dist < bestdist) + { + bestdist = dist; + VectorCopy(p3, beststart); + VectorCopy(v3, bestend); + } //end if + founddist = qtrue; + } //end else if + if (VectorBetweenVectors(p4, v1, v2)) + { + dist = VectorDistance(v4, p4); + if (dist > bestdist - 0.5 && dist < bestdist + 0.5) + { + VectorMiddle(beststart, p4, beststart); + VectorMiddle(bestend, v4, bestend); + } //end if + else if (dist < bestdist) + { + bestdist = dist; + VectorCopy(p4, beststart); + VectorCopy(v4, bestend); + } //end if + founddist = qtrue; + } //end else if + //if no shortest distance was found the shortest distance + //is between one of the vertexes of edge1 and one of edge2 + if (!founddist) + { + dist = VectorDistance(v1, v3); + if (dist < bestdist) + { + bestdist = dist; + VectorCopy(v1, beststart); + VectorCopy(v3, bestend); + } //end if + dist = VectorDistance(v1, v4); + if (dist < bestdist) + { + bestdist = dist; + VectorCopy(v1, beststart); + VectorCopy(v4, bestend); + } //end if + dist = VectorDistance(v2, v3); + if (dist < bestdist) + { + bestdist = dist; + VectorCopy(v2, beststart); + VectorCopy(v3, bestend); + } //end if + dist = VectorDistance(v2, v4); + if (dist < bestdist) + { + bestdist = dist; + VectorCopy(v2, beststart); + VectorCopy(v4, bestend); + } //end if + } //end if + return bestdist; +} //end of the function AAS_ClosestEdgePoints*/ + +float AAS_ClosestEdgePoints( vec3_t v1, vec3_t v2, vec3_t v3, vec3_t v4, + aas_plane_t *plane1, aas_plane_t *plane2, + vec3_t beststart1, vec3_t bestend1, + vec3_t beststart2, vec3_t bestend2, float bestdist ) { + vec3_t dir1, dir2, p1, p2, p3, p4; + float a1, a2, b1, b2, dist, dist1, dist2; + int founddist; + + //edge vectors + VectorSubtract( v2, v1, dir1 ); + VectorSubtract( v4, v3, dir2 ); + //get the horizontal directions + dir1[2] = 0; + dir2[2] = 0; + // + // p1 = point on an edge vector of area2 closest to v1 + // p2 = point on an edge vector of area2 closest to v2 + // p3 = point on an edge vector of area1 closest to v3 + // p4 = point on an edge vector of area1 closest to v4 + // + if ( dir2[0] ) { + a2 = dir2[1] / dir2[0]; + b2 = v3[1] - a2 * v3[0]; + //point on the edge vector of area2 closest to v1 + p1[0] = ( DotProduct( v1, dir2 ) - ( a2 * dir2[0] + b2 * dir2[1] ) ) / dir2[0]; + p1[1] = a2 * p1[0] + b2; + //point on the edge vector of area2 closest to v2 + p2[0] = ( DotProduct( v2, dir2 ) - ( a2 * dir2[0] + b2 * dir2[1] ) ) / dir2[0]; + p2[1] = a2 * p2[0] + b2; + } //end if + else + { + //point on the edge vector of area2 closest to v1 + p1[0] = v3[0]; + p1[1] = v1[1]; + //point on the edge vector of area2 closest to v2 + p2[0] = v3[0]; + p2[1] = v2[1]; + } //end else + // + if ( dir1[0] ) { + // + a1 = dir1[1] / dir1[0]; + b1 = v1[1] - a1 * v1[0]; + //point on the edge vector of area1 closest to v3 + p3[0] = ( DotProduct( v3, dir1 ) - ( a1 * dir1[0] + b1 * dir1[1] ) ) / dir1[0]; + p3[1] = a1 * p3[0] + b1; + //point on the edge vector of area1 closest to v4 + p4[0] = ( DotProduct( v4, dir1 ) - ( a1 * dir1[0] + b1 * dir1[1] ) ) / dir1[0]; + p4[1] = a1 * p4[0] + b1; + } //end if + else + { + //point on the edge vector of area1 closest to v3 + p3[0] = v1[0]; + p3[1] = v3[1]; + //point on the edge vector of area1 closest to v4 + p4[0] = v1[0]; + p4[1] = v4[1]; + } //end else + //start with zero z-coordinates + p1[2] = 0; + p2[2] = 0; + p3[2] = 0; + p4[2] = 0; + //calculate the z-coordinates from the ground planes + p1[2] = ( plane2->dist - DotProduct( plane2->normal, p1 ) ) / plane2->normal[2]; + p2[2] = ( plane2->dist - DotProduct( plane2->normal, p2 ) ) / plane2->normal[2]; + p3[2] = ( plane1->dist - DotProduct( plane1->normal, p3 ) ) / plane1->normal[2]; + p4[2] = ( plane1->dist - DotProduct( plane1->normal, p4 ) ) / plane1->normal[2]; + // + founddist = qfalse; + // + if ( VectorBetweenVectors( p1, v3, v4 ) ) { + dist = VectorDistance( v1, p1 ); + if ( dist > bestdist - 0.5 && dist < bestdist + 0.5 ) { + dist1 = VectorDistance( beststart1, v1 ); + dist2 = VectorDistance( beststart2, v1 ); + if ( dist1 > dist2 ) { + if ( dist1 > VectorDistance( beststart1, beststart2 ) ) { + VectorCopy( v1, beststart2 ); + } + } //end if + else + { + if ( dist2 > VectorDistance( beststart1, beststart2 ) ) { + VectorCopy( v1, beststart1 ); + } + } //end else + dist1 = VectorDistance( bestend1, p1 ); + dist2 = VectorDistance( bestend2, p1 ); + if ( dist1 > dist2 ) { + if ( dist1 > VectorDistance( bestend1, bestend2 ) ) { + VectorCopy( p1, bestend2 ); + } + } //end if + else + { + if ( dist2 > VectorDistance( bestend1, bestend2 ) ) { + VectorCopy( p1, bestend1 ); + } + } //end else + } //end if + else if ( dist < bestdist ) { + bestdist = dist; + VectorCopy( v1, beststart1 ); + VectorCopy( v1, beststart2 ); + VectorCopy( p1, bestend1 ); + VectorCopy( p1, bestend2 ); + } //end if + founddist = qtrue; + } //end if + if ( VectorBetweenVectors( p2, v3, v4 ) ) { + dist = VectorDistance( v2, p2 ); + if ( dist > bestdist - 0.5 && dist < bestdist + 0.5 ) { + dist1 = VectorDistance( beststart1, v2 ); + dist2 = VectorDistance( beststart2, v2 ); + if ( dist1 > dist2 ) { + if ( dist1 > VectorDistance( beststart1, beststart2 ) ) { + VectorCopy( v2, beststart2 ); + } + } //end if + else + { + if ( dist2 > VectorDistance( beststart1, beststart2 ) ) { + VectorCopy( v2, beststart1 ); + } + } //end else + dist1 = VectorDistance( bestend1, p2 ); + dist2 = VectorDistance( bestend2, p2 ); + if ( dist1 > dist2 ) { + if ( dist1 > VectorDistance( bestend1, bestend2 ) ) { + VectorCopy( p2, bestend2 ); + } + } //end if + else + { + if ( dist2 > VectorDistance( bestend1, bestend2 ) ) { + VectorCopy( p2, bestend1 ); + } + } //end else + } //end if + else if ( dist < bestdist ) { + bestdist = dist; + VectorCopy( v2, beststart1 ); + VectorCopy( v2, beststart2 ); + VectorCopy( p2, bestend1 ); + VectorCopy( p2, bestend2 ); + } //end if + founddist = qtrue; + } //end else if + if ( VectorBetweenVectors( p3, v1, v2 ) ) { + dist = VectorDistance( v3, p3 ); + if ( dist > bestdist - 0.5 && dist < bestdist + 0.5 ) { + dist1 = VectorDistance( beststart1, p3 ); + dist2 = VectorDistance( beststart2, p3 ); + if ( dist1 > dist2 ) { + if ( dist1 > VectorDistance( beststart1, beststart2 ) ) { + VectorCopy( p3, beststart2 ); + } + } //end if + else + { + if ( dist2 > VectorDistance( beststart1, beststart2 ) ) { + VectorCopy( p3, beststart1 ); + } + } //end else + dist1 = VectorDistance( bestend1, v3 ); + dist2 = VectorDistance( bestend2, v3 ); + if ( dist1 > dist2 ) { + if ( dist1 > VectorDistance( bestend1, bestend2 ) ) { + VectorCopy( v3, bestend2 ); + } + } //end if + else + { + if ( dist2 > VectorDistance( bestend1, bestend2 ) ) { + VectorCopy( v3, bestend1 ); + } + } //end else + } //end if + else if ( dist < bestdist ) { + bestdist = dist; + VectorCopy( p3, beststart1 ); + VectorCopy( p3, beststart2 ); + VectorCopy( v3, bestend1 ); + VectorCopy( v3, bestend2 ); + } //end if + founddist = qtrue; + } //end else if + if ( VectorBetweenVectors( p4, v1, v2 ) ) { + dist = VectorDistance( v4, p4 ); + if ( dist > bestdist - 0.5 && dist < bestdist + 0.5 ) { + dist1 = VectorDistance( beststart1, p4 ); + dist2 = VectorDistance( beststart2, p4 ); + if ( dist1 > dist2 ) { + if ( dist1 > VectorDistance( beststart1, beststart2 ) ) { + VectorCopy( p4, beststart2 ); + } + } //end if + else + { + if ( dist2 > VectorDistance( beststart1, beststart2 ) ) { + VectorCopy( p4, beststart1 ); + } + } //end else + dist1 = VectorDistance( bestend1, v4 ); + dist2 = VectorDistance( bestend2, v4 ); + if ( dist1 > dist2 ) { + if ( dist1 > VectorDistance( bestend1, bestend2 ) ) { + VectorCopy( v4, bestend2 ); + } + } //end if + else + { + if ( dist2 > VectorDistance( bestend1, bestend2 ) ) { + VectorCopy( v4, bestend1 ); + } + } //end else + } //end if + else if ( dist < bestdist ) { + bestdist = dist; + VectorCopy( p4, beststart1 ); + VectorCopy( p4, beststart2 ); + VectorCopy( v4, bestend1 ); + VectorCopy( v4, bestend2 ); + } //end if + founddist = qtrue; + } //end else if + //if no shortest distance was found the shortest distance + //is between one of the vertexes of edge1 and one of edge2 + if ( !founddist ) { + dist = VectorDistance( v1, v3 ); + if ( dist < bestdist ) { + bestdist = dist; + VectorCopy( v1, beststart1 ); + VectorCopy( v1, beststart2 ); + VectorCopy( v3, bestend1 ); + VectorCopy( v3, bestend2 ); + } //end if + dist = VectorDistance( v1, v4 ); + if ( dist < bestdist ) { + bestdist = dist; + VectorCopy( v1, beststart1 ); + VectorCopy( v1, beststart2 ); + VectorCopy( v4, bestend1 ); + VectorCopy( v4, bestend2 ); + } //end if + dist = VectorDistance( v2, v3 ); + if ( dist < bestdist ) { + bestdist = dist; + VectorCopy( v2, beststart1 ); + VectorCopy( v2, beststart2 ); + VectorCopy( v3, bestend1 ); + VectorCopy( v3, bestend2 ); + } //end if + dist = VectorDistance( v2, v4 ); + if ( dist < bestdist ) { + bestdist = dist; + VectorCopy( v2, beststart1 ); + VectorCopy( v2, beststart2 ); + VectorCopy( v4, bestend1 ); + VectorCopy( v4, bestend2 ); + } //end if + } //end if + return bestdist; +} //end of the function AAS_ClosestEdgePoints +//=========================================================================== +// creates possible jump reachabilities between the areas +// +// The two closest points on the ground of the areas are calculated +// One of the points will be on an edge of a ground face of area1 and +// one on an edge of a ground face of area2. +// If there is a range of closest points the point in the middle of this range +// is selected. +// Between these two points there must be one or more gaps. +// If the gaps exist a potential jump is predicted. +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_Reachability_Jump( int area1num, int area2num ) { + int i, j, k, l, face1num, face2num, edge1num, edge2num, traveltype; + float sv_jumpvel, maxjumpdistance, maxjumpheight, height, bestdist, speed; + vec_t *v1, *v2, *v3, *v4; + vec3_t beststart, beststart2, bestend, bestend2; + vec3_t teststart, testend, dir, velocity, cmdmove, up = {0, 0, 1}; + aas_area_t *area1, *area2; + aas_face_t *face1, *face2; + aas_edge_t *edge1, *edge2; + aas_plane_t *plane1, *plane2, *plane; + aas_trace_t trace; + aas_clientmove_t move; + aas_lreachability_t *lreach; + + if ( !AAS_AreaGrounded( area1num ) || !AAS_AreaGrounded( area2num ) ) { + return qfalse; + } + //cannot jump from or to a crouch area + if ( AAS_AreaCrouch( area1num ) || AAS_AreaCrouch( area2num ) ) { + return qfalse; + } + // + area1 = &( *aasworld ).areas[area1num]; + area2 = &( *aasworld ).areas[area2num]; + // + sv_jumpvel = aassettings.sv_jumpvel; + //maximum distance a player can jump + maxjumpdistance = 2 * AAS_MaxJumpDistance( sv_jumpvel ); + //maximum height a player can jump with the given initial z velocity + maxjumpheight = AAS_MaxJumpHeight( sv_jumpvel ); + + //if the areas are not near anough in the x-y direction + for ( i = 0; i < 2; i++ ) + { + if ( area1->mins[i] > area2->maxs[i] + maxjumpdistance ) { + return qfalse; + } + if ( area1->maxs[i] < area2->mins[i] - maxjumpdistance ) { + return qfalse; + } + } //end for + //if area2 is way to high to jump up to + if ( area2->mins[2] > area1->maxs[2] + maxjumpheight ) { + return qfalse; + } + // + bestdist = 999999; + // + for ( i = 0; i < area1->numfaces; i++ ) + { + face1num = ( *aasworld ).faceindex[area1->firstface + i]; + face1 = &( *aasworld ).faces[abs( face1num )]; + //if not a ground face + if ( !( face1->faceflags & FACE_GROUND ) ) { + continue; + } + // + for ( j = 0; j < area2->numfaces; j++ ) + { + face2num = ( *aasworld ).faceindex[area2->firstface + j]; + face2 = &( *aasworld ).faces[abs( face2num )]; + //if not a ground face + if ( !( face2->faceflags & FACE_GROUND ) ) { + continue; + } + // + for ( k = 0; k < face1->numedges; k++ ) + { + edge1num = abs( ( *aasworld ).edgeindex[face1->firstedge + k] ); + edge1 = &( *aasworld ).edges[edge1num]; + for ( l = 0; l < face2->numedges; l++ ) + { + edge2num = abs( ( *aasworld ).edgeindex[face2->firstedge + l] ); + edge2 = &( *aasworld ).edges[edge2num]; + //calculate the minimum distance between the two edges + v1 = ( *aasworld ).vertexes[edge1->v[0]]; + v2 = ( *aasworld ).vertexes[edge1->v[1]]; + v3 = ( *aasworld ).vertexes[edge2->v[0]]; + v4 = ( *aasworld ).vertexes[edge2->v[1]]; + //get the ground planes + plane1 = &( *aasworld ).planes[face1->planenum]; + plane2 = &( *aasworld ).planes[face2->planenum]; + // + bestdist = AAS_ClosestEdgePoints( v1, v2, v3, v4, plane1, plane2, + beststart, bestend, + beststart2, bestend2, bestdist ); + } //end for + } //end for + } //end for + } //end for + VectorMiddle( beststart, beststart2, beststart ); + VectorMiddle( bestend, bestend2, bestend ); + if ( bestdist > 4 && bestdist < maxjumpdistance ) { +// Log_Write("shortest distance between %d and %d is %f\r\n", area1num, area2num, bestdist); + //if the fall would damage the bot + // + if ( AAS_HorizontalVelocityForJump( 0, beststart, bestend, &speed ) ) { + //FIXME: why multiply with 1.2??? + speed *= 1.2; + traveltype = TRAVEL_WALKOFFLEDGE; + } //end if + else if ( bestdist <= 48 && fabs( beststart[2] - bestend[2] ) < 8 ) { + speed = 400; + traveltype = TRAVEL_WALKOFFLEDGE; + } //end else if + else + { + //get the horizontal speed for the jump, if it isn't possible to calculate this + //speed (the jump is not possible) then there's no jump reachability created + if ( !AAS_HorizontalVelocityForJump( sv_jumpvel, beststart, bestend, &speed ) ) { + return qfalse; + } + traveltype = TRAVEL_JUMP; + // + //NOTE: test if the horizontal distance isn't too small + VectorSubtract( bestend, beststart, dir ); + dir[2] = 0; + if ( VectorLength( dir ) < 10 ) { + return qfalse; + } + } //end if + // + VectorSubtract( bestend, beststart, dir ); + VectorNormalize( dir ); + VectorMA( beststart, 1, dir, teststart ); + // + VectorCopy( teststart, testend ); + testend[2] -= 100; + trace = AAS_TraceClientBBox( teststart, testend, PRESENCE_NORMAL, -1 ); + // + if ( trace.startsolid ) { + return qfalse; + } + if ( trace.fraction < 1 ) { + plane = &( *aasworld ).planes[trace.planenum]; + if ( DotProduct( plane->normal, up ) >= 0.7 ) { + if ( !( AAS_PointContents( trace.endpos ) & CONTENTS_LAVA ) ) { //----(SA) modified since slime is no longer deadly +// if (!(AAS_PointContents(trace.endpos) & (CONTENTS_LAVA|CONTENTS_SLIME))) + if ( teststart[2] - trace.endpos[2] <= aassettings.sv_maxbarrier ) { + return qfalse; + } + } //end if + } //end if + } //end if + // + VectorMA( bestend, -1, dir, teststart ); + // + VectorCopy( teststart, testend ); + testend[2] -= 100; + trace = AAS_TraceClientBBox( teststart, testend, PRESENCE_NORMAL, -1 ); + // + if ( trace.startsolid ) { + return qfalse; + } + if ( trace.fraction < 1 ) { + plane = &( *aasworld ).planes[trace.planenum]; + if ( DotProduct( plane->normal, up ) >= 0.7 ) { + if ( !( AAS_PointContents( trace.endpos ) & ( CONTENTS_LAVA | CONTENTS_SLIME ) ) ) { + if ( teststart[2] - trace.endpos[2] <= aassettings.sv_maxbarrier ) { + return qfalse; + } + } //end if + } //end if + } //end if + // + VectorSubtract( bestend, beststart, dir ); + dir[2] = 0; + VectorNormalize( dir ); + // + VectorScale( dir, speed, velocity ); + //get command movement + VectorClear( cmdmove ); + if ( traveltype == TRAVEL_JUMP ) { + cmdmove[2] = aassettings.sv_jumpvel; + } else { cmdmove[2] = 0;} + // + AAS_PredictClientMovement( &move, -1, beststart, PRESENCE_NORMAL, qtrue, + velocity, cmdmove, 3, 30, 0.1, + SE_HITGROUND | SE_ENTERWATER | SE_ENTERSLIME | + SE_ENTERLAVA | SE_HITGROUNDDAMAGE, 0, qfalse ); + //if prediction time wasn't enough to fully predict the movement + if ( move.frames >= 30 ) { + return qfalse; + } + //don't enter slime or lava and don't fall from too high + if ( move.stopevent & SE_ENTERLAVA ) { + return qfalse; //----(SA) modified since slime is no longer deadly + } +// if (move.stopevent & (SE_ENTERSLIME|SE_ENTERLAVA)) return qfalse; + //the end position should be in area2, also test a little bit back + //because the predicted jump could have rushed through the area + for ( i = 0; i <= 32; i += 8 ) + { + VectorMA( move.endpos, -i, dir, teststart ); + teststart[2] += 0.125; + if ( AAS_PointAreaNum( teststart ) == area2num ) { + break; + } + } //end for + if ( i > 32 ) { + return qfalse; + } + // +#ifdef REACHDEBUG + //create the reachability + Log_Write( "jump reachability between %d and %d\r\n", area1num, area2num ); +#endif //REACHDEBUG + //create a new reachability link + lreach = AAS_AllocReachability(); + if ( !lreach ) { + return qfalse; + } + lreach->areanum = area2num; + lreach->facenum = 0; + lreach->edgenum = 0; + VectorCopy( beststart, lreach->start ); + VectorCopy( bestend, lreach->end ); + lreach->traveltype = traveltype; + + VectorSubtract( bestend, beststart, dir ); + height = dir[2]; + dir[2] = 0; + if ( traveltype == TRAVEL_WALKOFFLEDGE && height > VectorLength( dir ) ) { + lreach->traveltime = STARTWALKOFFLEDGE_TIME + height * 50 / aassettings.sv_gravity; + } else + { + lreach->traveltime = STARTJUMP_TIME + VectorDistance( bestend, beststart ) * 240 / aassettings.sv_maxwalkvelocity; + } //end if + // + if ( !AAS_AreaJumpPad( area2num ) ) { + if ( AAS_FallDelta( beststart[2] - bestend[2] ) > FALLDELTA_5DAMAGE ) { + lreach->traveltime += FALLDAMAGE_5_TIME; + } //end if + else if ( AAS_FallDelta( beststart[2] - bestend[2] ) > FALLDELTA_10DAMAGE ) { + lreach->traveltime += FALLDAMAGE_10_TIME; + } //end if + } //end if + lreach->next = areareachability[area1num]; + areareachability[area1num] = lreach; + // + if ( traveltype == TRAVEL_JUMP ) { + reach_jump++; + } else { reach_walkoffledge++;} + } //end if + return qfalse; +} //end of the function AAS_Reachability_Jump +//=========================================================================== +// create a possible ladder reachability from area1 to area2 +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_Reachability_Ladder( int area1num, int area2num ) { + int i, j, k, l, edge1num, edge2num, sharededgenum, lowestedgenum; + int face1num, face2num, ladderface1num, ladderface2num; + int ladderface1vertical, ladderface2vertical, firstv; + float face1area, face2area, bestface1area, bestface2area; + float sv_jumpvel, maxjumpheight; + vec3_t area1point, area2point, v1, v2, up = {0, 0, 1}; + vec3_t mid, lowestpoint, start, end, sharededgevec, dir; + aas_area_t *area1, *area2; + aas_face_t *face1, *face2, *ladderface1, *ladderface2; + aas_plane_t *plane1, *plane2; + aas_edge_t *sharededge, *edge1; + aas_lreachability_t *lreach; + aas_trace_t trace; + + if ( !AAS_AreaLadder( area1num ) || !AAS_AreaLadder( area2num ) ) { + return qfalse; + } + // + sv_jumpvel = aassettings.sv_jumpvel; + //maximum height a player can jump with the given initial z velocity + maxjumpheight = AAS_MaxJumpHeight( sv_jumpvel ); + + area1 = &( *aasworld ).areas[area1num]; + area2 = &( *aasworld ).areas[area2num]; + // + ladderface1 = NULL; + ladderface2 = NULL; + ladderface1num = 0; //make compiler happy + ladderface2num = 0; //make compiler happy + bestface1area = -9999; + bestface2area = -9999; + sharededgenum = 0; //make compiler happy + lowestedgenum = 0; //make compiler happy + // + for ( i = 0; i < area1->numfaces; i++ ) + { + face1num = ( *aasworld ).faceindex[area1->firstface + i]; + face1 = &( *aasworld ).faces[abs( face1num )]; + //if not a ladder face + if ( !( face1->faceflags & FACE_LADDER ) ) { + continue; + } + // + for ( j = 0; j < area2->numfaces; j++ ) + { + face2num = ( *aasworld ).faceindex[area2->firstface + j]; + face2 = &( *aasworld ).faces[abs( face2num )]; + //if not a ladder face + if ( !( face2->faceflags & FACE_LADDER ) ) { + continue; + } + //check if the faces share an edge + for ( k = 0; k < face1->numedges; k++ ) + { + edge1num = ( *aasworld ).edgeindex[face1->firstedge + k]; + for ( l = 0; l < face2->numedges; l++ ) + { + edge2num = ( *aasworld ).edgeindex[face2->firstedge + l]; + if ( abs( edge1num ) == abs( edge2num ) ) { + //get the face with the largest area + face1area = AAS_FaceArea( face1 ); + face2area = AAS_FaceArea( face2 ); + if ( face1area > bestface1area && face2area > bestface2area ) { + bestface1area = face1area; + bestface2area = face2area; + ladderface1 = face1; + ladderface2 = face2; + ladderface1num = face1num; + ladderface2num = face2num; + sharededgenum = edge1num; + } //end if + break; + } //end if + } //end for + if ( l != face2->numedges ) { + break; + } + } //end for + } //end for + } //end for + // + if ( ladderface1 && ladderface2 ) { + //get the middle of the shared edge + sharededge = &( *aasworld ).edges[abs( sharededgenum )]; + firstv = sharededgenum < 0; + // + VectorCopy( ( *aasworld ).vertexes[sharededge->v[firstv]], v1 ); + VectorCopy( ( *aasworld ).vertexes[sharededge->v[!firstv]], v2 ); + VectorAdd( v1, v2, area1point ); + VectorScale( area1point, 0.5, area1point ); + VectorCopy( area1point, area2point ); + // + //if the face plane in area 1 is pretty much vertical + plane1 = &( *aasworld ).planes[ladderface1->planenum ^ ( ladderface1num < 0 )]; + plane2 = &( *aasworld ).planes[ladderface2->planenum ^ ( ladderface2num < 0 )]; + // + //get the points really into the areas + VectorSubtract( v2, v1, sharededgevec ); + CrossProduct( plane1->normal, sharededgevec, dir ); + VectorNormalize( dir ); + //NOTE: 32 because that's larger than 16 (bot bbox x,y) + VectorMA( area1point, -32, dir, area1point ); + VectorMA( area2point, 32, dir, area2point ); + // + ladderface1vertical = abs( DotProduct( plane1->normal, up ) ) < 0.1; + ladderface2vertical = abs( DotProduct( plane2->normal, up ) ) < 0.1; + //there's only reachability between vertical ladder faces + if ( !ladderface1vertical && !ladderface2vertical ) { + return qfalse; + } + //if both vertical ladder faces + if ( ladderface1vertical && ladderface2vertical + //and the ladder faces do not make a sharp corner + && DotProduct( plane1->normal, plane2->normal ) > 0.7 + //and the shared edge is not too vertical + && abs( DotProduct( sharededgevec, up ) ) < 0.7 ) { + //create a new reachability link + lreach = AAS_AllocReachability(); + if ( !lreach ) { + return qfalse; + } + lreach->areanum = area2num; + lreach->facenum = ladderface1num; + lreach->edgenum = abs( sharededgenum ); + VectorCopy( area1point, lreach->start ); + //VectorCopy(area2point, lreach->end); + VectorMA( area2point, -3, plane1->normal, lreach->end ); + lreach->traveltype = TRAVEL_LADDER; + lreach->traveltime = 10; + lreach->next = areareachability[area1num]; + areareachability[area1num] = lreach; + // + reach_ladder++; + //create a new reachability link + lreach = AAS_AllocReachability(); + if ( !lreach ) { + return qfalse; + } + lreach->areanum = area1num; + lreach->facenum = ladderface2num; + lreach->edgenum = abs( sharededgenum ); + VectorCopy( area2point, lreach->start ); + //VectorCopy(area1point, lreach->end); + VectorMA( area1point, -3, plane2->normal, lreach->end ); + lreach->traveltype = TRAVEL_LADDER; + lreach->traveltime = 10; + lreach->next = areareachability[area2num]; + areareachability[area2num] = lreach; + // + reach_ladder++; + // + return qtrue; + } //end if + //if the second ladder face is also a ground face + //create ladder end (just ladder) reachability and + //walk off a ladder (ledge) reachability + if ( ladderface1vertical && ( ladderface2->faceflags & FACE_GROUND ) ) { + //create a new reachability link + lreach = AAS_AllocReachability(); + if ( !lreach ) { + return qfalse; + } + lreach->areanum = area2num; + lreach->facenum = ladderface1num; + lreach->edgenum = abs( sharededgenum ); + VectorCopy( area1point, lreach->start ); + VectorCopy( area2point, lreach->end ); + lreach->end[2] += 16; + VectorMA( lreach->end, -15, plane1->normal, lreach->end ); + lreach->traveltype = TRAVEL_LADDER; + lreach->traveltime = 10; + lreach->next = areareachability[area1num]; + areareachability[area1num] = lreach; + // + reach_ladder++; + //create a new reachability link + lreach = AAS_AllocReachability(); + if ( !lreach ) { + return qfalse; + } + lreach->areanum = area1num; + lreach->facenum = ladderface2num; + lreach->edgenum = abs( sharededgenum ); + VectorCopy( area2point, lreach->start ); + VectorCopy( area1point, lreach->end ); + lreach->traveltype = TRAVEL_WALKOFFLEDGE; + lreach->traveltime = 10; + lreach->next = areareachability[area2num]; + areareachability[area2num] = lreach; + // + reach_walkoffledge++; + // + return qtrue; + } //end if + // + if ( ladderface1vertical ) { + //find lowest edge of the ladder face + lowestpoint[2] = 99999; + for ( i = 0; i < ladderface1->numedges; i++ ) + { + edge1num = abs( ( *aasworld ).edgeindex[ladderface1->firstedge + i] ); + edge1 = &( *aasworld ).edges[edge1num]; + // + VectorCopy( ( *aasworld ).vertexes[edge1->v[0]], v1 ); + VectorCopy( ( *aasworld ).vertexes[edge1->v[1]], v2 ); + // + VectorAdd( v1, v2, mid ); + VectorScale( mid, 0.5, mid ); + // + if ( mid[2] < lowestpoint[2] ) { + VectorCopy( mid, lowestpoint ); + lowestedgenum = edge1num; + } //end if + } //end for + // + plane1 = &( *aasworld ).planes[ladderface1->planenum]; + //trace down in the middle of this edge + VectorMA( lowestpoint, 5, plane1->normal, start ); + VectorCopy( start, end ); + start[2] += 5; + end[2] -= 100; + //trace without entity collision + trace = AAS_TraceClientBBox( start, end, PRESENCE_NORMAL, -1 ); + // + // +#ifdef REACHDEBUG + if ( trace.startsolid ) { + Log_Write( "trace from area %d started in solid\r\n", area1num ); + } //end if +#endif //REACHDEBUG + // + trace.endpos[2] += 1; + area2num = AAS_PointAreaNum( trace.endpos ); + // + area2 = &( *aasworld ).areas[area2num]; + for ( i = 0; i < area2->numfaces; i++ ) + { + face2num = ( *aasworld ).faceindex[area2->firstface + i]; + face2 = &( *aasworld ).faces[abs( face2num )]; + // + if ( face2->faceflags & FACE_LADDER ) { + plane2 = &( *aasworld ).planes[face2->planenum]; + if ( abs( DotProduct( plane2->normal, up ) ) < 0.1 ) { + break; + } + } //end if + } //end for + //if from another area without vertical ladder faces + if ( i >= area2->numfaces && area2num != area1num && + //the reachabilities shouldn't exist already + !AAS_ReachabilityExists( area1num, area2num ) && + !AAS_ReachabilityExists( area2num, area1num ) ) { + //if the height is jumpable + if ( start[2] - trace.endpos[2] < maxjumpheight ) { + //create a new reachability link + lreach = AAS_AllocReachability(); + if ( !lreach ) { + return qfalse; + } + lreach->areanum = area2num; + lreach->facenum = ladderface1num; + lreach->edgenum = lowestedgenum; + VectorCopy( lowestpoint, lreach->start ); + VectorCopy( trace.endpos, lreach->end ); + lreach->traveltype = TRAVEL_LADDER; + lreach->traveltime = 10; + lreach->next = areareachability[area1num]; + areareachability[area1num] = lreach; + // + reach_ladder++; + //create a new reachability link + lreach = AAS_AllocReachability(); + if ( !lreach ) { + return qfalse; + } + lreach->areanum = area1num; + lreach->facenum = ladderface1num; + lreach->edgenum = lowestedgenum; + VectorCopy( trace.endpos, lreach->start ); + //get the end point a little bit into the ladder + VectorMA( lowestpoint, -5, plane1->normal, lreach->end ); + //get the end point a little higher + lreach->end[2] += 10; + lreach->traveltype = TRAVEL_JUMP; + lreach->traveltime = 10; + lreach->next = areareachability[area2num]; + areareachability[area2num] = lreach; + // + reach_jump++; + // + return qtrue; +#ifdef REACHDEBUG + Log_Write( "jump up to ladder reach between %d and %d\r\n", area2num, area1num ); +#endif //REACHDEBUG + } //end if +#ifdef REACHDEBUG + else {Log_Write( "jump too high between area %d and %d\r\n", area2num, area1num );} +#endif //REACHDEBUG + } //end if + /*//if slime or lava below the ladder + //try jump reachability from far towards the ladder + if ((*aasworld).areasettings[area2num].contents & (AREACONTENTS_SLIME + | AREACONTENTS_LAVA)) + { + for (i = 20; i <= 120; i += 20) + { + //trace down in the middle of this edge + VectorMA(lowestpoint, i, plane1->normal, start); + VectorCopy(start, end); + start[2] += 5; + end[2] -= 100; + //trace without entity collision + trace = AAS_TraceClientBBox(start, end, PRESENCE_NORMAL, -1); + // + if (trace.startsolid) break; + trace.endpos[2] += 1; + area2num = AAS_PointAreaNum(trace.endpos); + if (area2num == area1num) continue; + // + if (start[2] - trace.endpos[2] > maxjumpheight) continue; + if ((*aasworld).areasettings[area2num].contents & (AREACONTENTS_SLIME + | AREACONTENTS_LAVA)) continue; + // + //create a new reachability link + lreach = AAS_AllocReachability(); + if (!lreach) return qfalse; + lreach->areanum = area1num; + lreach->facenum = ladderface1num; + lreach->edgenum = lowestedgenum; + VectorCopy(trace.endpos, lreach->start); + VectorCopy(lowestpoint, lreach->end); + lreach->end[2] += 5; + lreach->traveltype = TRAVEL_JUMP; + lreach->traveltime = 10; + lreach->next = areareachability[area2num]; + areareachability[area2num] = lreach; + // + reach_jump++; + // + Log_Write("jump far to ladder reach between %d and %d\r\n", area2num, area1num); + // + break; + } //end for + } //end if*/ + } //end if + } //end if + return qfalse; +} //end of the function AAS_Reachability_Ladder +//=========================================================================== +// create possible teleporter reachabilities +// this is very game dependent.... :( +// +// classname = trigger_multiple or trigger_teleport +// target = "t1" +// +// classname = target_teleporter +// targetname = "t1" +// target = "t2" +// +// classname = misc_teleporter_dest +// targetname = "t2" +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_Reachability_Teleport( void ) { + int area1num, area2num; + char target[MAX_EPAIRKEY], targetname[MAX_EPAIRKEY]; + char classname[MAX_EPAIRKEY], model[MAX_EPAIRKEY]; + int ent, dest; + vec3_t origin, destorigin, mins, maxs, end, angles = {0, 0, 0}; + vec3_t mid; + aas_lreachability_t *lreach; + aas_trace_t trace; + aas_link_t *areas, *link; + + for ( ent = AAS_NextBSPEntity( 0 ); ent; ent = AAS_NextBSPEntity( ent ) ) + { + if ( !AAS_ValueForBSPEpairKey( ent, "classname", classname, MAX_EPAIRKEY ) ) { + continue; + } + if ( !strcmp( classname, "trigger_multiple" ) ) { + AAS_ValueForBSPEpairKey( ent, "model", model, MAX_EPAIRKEY ); +//#ifdef REACHDEBUG + botimport.Print( PRT_MESSAGE, "trigger_multiple model = \"%s\"\n", model ); +//#endif REACHDEBUG + AAS_BSPModelMinsMaxsOrigin( atoi( model + 1 ), angles, mins, maxs, origin ); + // + if ( !AAS_ValueForBSPEpairKey( ent, "target", target, MAX_EPAIRKEY ) ) { + botimport.Print( PRT_ERROR, "trigger_multiple at %1.0f %1.0f %1.0f without target\n", + origin[0], origin[1], origin[2] ); + continue; + } //end if + for ( dest = AAS_NextBSPEntity( 0 ); dest; dest = AAS_NextBSPEntity( dest ) ) + { + if ( !AAS_ValueForBSPEpairKey( dest, "classname", classname, MAX_EPAIRKEY ) ) { + continue; + } + if ( !strcmp( classname, "target_teleporter" ) ) { + if ( !AAS_ValueForBSPEpairKey( dest, "targetname", targetname, MAX_EPAIRKEY ) ) { + continue; + } + if ( !strcmp( targetname, target ) ) { + break; + } //end if + } //end if + } //end for + if ( !dest ) { + continue; + } //end if + if ( !AAS_ValueForBSPEpairKey( dest, "target", target, MAX_EPAIRKEY ) ) { + botimport.Print( PRT_ERROR, "target_teleporter without target\n" ); + continue; + } //end if + } //end else + else if ( !strcmp( classname, "trigger_teleport" ) ) { + AAS_ValueForBSPEpairKey( ent, "model", model, MAX_EPAIRKEY ); +//#ifdef REACHDEBUG + botimport.Print( PRT_MESSAGE, "trigger_teleport model = \"%s\"\n", model ); +//#endif REACHDEBUG + AAS_BSPModelMinsMaxsOrigin( atoi( model + 1 ), angles, mins, maxs, origin ); + // + if ( !AAS_ValueForBSPEpairKey( ent, "target", target, MAX_EPAIRKEY ) ) { + botimport.Print( PRT_ERROR, "trigger_teleport at %1.0f %1.0f %1.0f without target\n", + origin[0], origin[1], origin[2] ); + continue; + } //end if + } //end if + else + { + continue; + } //end else + // + for ( dest = AAS_NextBSPEntity( 0 ); dest; dest = AAS_NextBSPEntity( dest ) ) + { + //classname should be misc_teleporter_dest + //but I've also seen target_position and actually any + //entity could be used... burp + if ( AAS_ValueForBSPEpairKey( dest, "targetname", targetname, MAX_EPAIRKEY ) ) { + if ( !strcmp( targetname, target ) ) { + break; + } //end if + } //end if + } //end for + if ( !dest ) { + botimport.Print( PRT_ERROR, "teleporter without misc_teleporter_dest (%s)\n", target ); + continue; + } //end if + if ( !AAS_VectorForBSPEpairKey( dest, "origin", destorigin ) ) { + botimport.Print( PRT_ERROR, "teleporter destination (%s) without origin\n", target ); + continue; + } //end if + // + area2num = AAS_PointAreaNum( destorigin ); + //if not teleported into a teleporter or into a jumppad + if ( !AAS_AreaTeleporter( area2num ) && !AAS_AreaJumpPad( area2num ) ) { + destorigin[2] += 24; //just for q2e1m2, the dork has put the telepads in the ground + VectorCopy( destorigin, end ); + end[2] -= 100; + trace = AAS_TraceClientBBox( destorigin, end, PRESENCE_CROUCH, -1 ); + if ( trace.startsolid ) { + botimport.Print( PRT_ERROR, "teleporter destination (%s) in solid\n", target ); + continue; + } //end if + VectorCopy( trace.endpos, destorigin ); + area2num = AAS_PointAreaNum( destorigin ); + } //end if + // + //botimport.Print(PRT_MESSAGE, "teleporter brush origin at %f %f %f\n", origin[0], origin[1], origin[2]); + //botimport.Print(PRT_MESSAGE, "teleporter brush mins = %f %f %f\n", mins[0], mins[1], mins[2]); + //botimport.Print(PRT_MESSAGE, "teleporter brush maxs = %f %f %f\n", maxs[0], maxs[1], maxs[2]); + VectorAdd( origin, mins, mins ); + VectorAdd( origin, maxs, maxs ); + // + VectorAdd( mins, maxs, mid ); + VectorScale( mid, 0.5, mid ); + //link an invalid (-1) entity + areas = AAS_LinkEntityClientBBox( mins, maxs, -1, PRESENCE_CROUCH ); + if ( !areas ) { + botimport.Print( PRT_MESSAGE, "trigger_multiple not in any area\n" ); + } + // + for ( link = areas; link; link = link->next_area ) + { + //if (!AAS_AreaGrounded(link->areanum)) continue; + if ( !AAS_AreaTeleporter( link->areanum ) ) { + continue; + } + // + area1num = link->areanum; + //create a new reachability link + lreach = AAS_AllocReachability(); + if ( !lreach ) { + break; + } + lreach->areanum = area2num; + lreach->facenum = 0; + lreach->edgenum = 0; + VectorCopy( mid, lreach->start ); + VectorCopy( destorigin, lreach->end ); + lreach->traveltype = TRAVEL_TELEPORT; + lreach->traveltime = TELEPORT_TIME; + lreach->next = areareachability[area1num]; + areareachability[area1num] = lreach; + // + reach_teleport++; + } //end for + //unlink the invalid entity + AAS_UnlinkFromAreas( areas ); + } //end for +} //end of the function AAS_Reachability_Teleport +//=========================================================================== +// create possible elevator (func_plat) reachabilities +// this is very game dependent.... :( +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#define REACHDEBUG +void AAS_Reachability_Elevator( void ) { + int area1num, area2num, modelnum, i, j, k, l, n, p; + float lip, height, speed; + char model[MAX_EPAIRKEY], classname[MAX_EPAIRKEY]; + int ent; + vec3_t mins, maxs, origin, angles = {0, 0, 0}; + vec3_t pos1, pos2, mids, platbottom, plattop; + vec3_t bottomorg, toporg, start, end, dir; + vec_t xvals[8], yvals[8], xvals_top[8], yvals_top[8]; + aas_lreachability_t *lreach; + aas_trace_t trace; + +#ifdef REACHDEBUG + Log_Write( "AAS_Reachability_Elevator\r\n" ); +#endif //REACHDEBUG + for ( ent = AAS_NextBSPEntity( 0 ); ent; ent = AAS_NextBSPEntity( ent ) ) + { + if ( !AAS_ValueForBSPEpairKey( ent, "classname", classname, MAX_EPAIRKEY ) ) { + continue; + } + if ( !strcmp( classname, "func_plat" ) ) { +#ifdef REACHDEBUG + Log_Write( "found func plat\r\n" ); +#endif //REACHDEBUG + if ( !AAS_ValueForBSPEpairKey( ent, "model", model, MAX_EPAIRKEY ) ) { + botimport.Print( PRT_ERROR, "func_plat without model\n" ); + continue; + } //end if + //get the model number, and skip the leading * + modelnum = atoi( model + 1 ); + if ( modelnum <= 0 ) { + botimport.Print( PRT_ERROR, "func_plat with invalid model number\n" ); + continue; + } //end if + //get the mins, maxs and origin of the model + //NOTE: the origin is usually (0,0,0) and the mins and maxs + // are the absolute mins and maxs + AAS_BSPModelMinsMaxsOrigin( modelnum, angles, mins, maxs, origin ); + // + AAS_VectorForBSPEpairKey( ent, "origin", origin ); + //pos1 is the top position, pos2 is the bottom + VectorCopy( origin, pos1 ); + VectorCopy( origin, pos2 ); + //get the lip of the plat + AAS_FloatForBSPEpairKey( ent, "lip", &lip ); + if ( !lip ) { + lip = 8; + } + //get the movement height of the plat + AAS_FloatForBSPEpairKey( ent, "height", &height ); + if ( !height ) { + height = ( maxs[2] - mins[2] ) - lip; + } + //get the speed of the plat + AAS_FloatForBSPEpairKey( ent, "speed", &speed ); + if ( !speed ) { + speed = 200; + } + //get bottom position below pos1 + pos2[2] -= height; + // + botimport.Print( PRT_MESSAGE, "pos2[2] = %1.1f pos1[2] = %1.1f\n", pos2[2], pos1[2] ); + //get a point just above the plat in the bottom position + VectorAdd( mins, maxs, mids ); + VectorMA( pos2, 0.5, mids, platbottom ); + platbottom[2] = maxs[2] - ( pos1[2] - pos2[2] ) + 2; + //get a point just above the plat in the top position + VectorAdd( mins, maxs, mids ); + VectorMA( pos2, 0.5, mids, plattop ); + plattop[2] = maxs[2] + 2; + // + /*if (!area1num) + { + Log_Write("no grounded area near plat bottom\r\n"); + continue; + } //end if*/ + //get the mins and maxs a little larger + for ( i = 0; i < 3; i++ ) + { + mins[i] -= 1; + maxs[i] += 1; + } //end for + // + botimport.Print( PRT_MESSAGE, "platbottom[2] = %1.1f plattop[2] = %1.1f\n", platbottom[2], plattop[2] ); + // + VectorAdd( mins, maxs, mids ); + VectorScale( mids, 0.5, mids ); + // + xvals[0] = mins[0]; xvals[1] = mids[0]; xvals[2] = maxs[0]; xvals[3] = mids[0]; + yvals[0] = mids[1]; yvals[1] = maxs[1]; yvals[2] = mids[1]; yvals[3] = mins[1]; + // + xvals[4] = mins[0]; xvals[5] = maxs[0]; xvals[6] = maxs[0]; xvals[7] = mins[0]; + yvals[4] = maxs[1]; yvals[5] = maxs[1]; yvals[6] = mins[1]; yvals[7] = mins[1]; + //find adjacent areas around the bottom of the plat + for ( i = 0; i < 9; i++ ) + { + if ( i < 8 ) { //check at the sides of the plat + bottomorg[0] = origin[0] + xvals[i]; + bottomorg[1] = origin[1] + yvals[i]; + bottomorg[2] = platbottom[2] + 16; + //get a grounded or swim area near the plat in the bottom position + area1num = AAS_PointAreaNum( bottomorg ); + for ( k = 0; k < 16; k++ ) + { + if ( area1num ) { + if ( AAS_AreaGrounded( area1num ) || AAS_AreaSwim( area1num ) ) { + break; + } + } //end if + bottomorg[2] += 4; + area1num = AAS_PointAreaNum( bottomorg ); + } //end if + //if in solid + if ( k >= 16 ) { + continue; + } //end if + } //end if + else //at the middle of the plat + { + VectorCopy( plattop, bottomorg ); + bottomorg[2] += 24; + area1num = AAS_PointAreaNum( bottomorg ); + if ( !area1num ) { + continue; + } + VectorCopy( platbottom, bottomorg ); + bottomorg[2] += 24; + } //end else + //look at adjacent areas around the top of the plat + //make larger steps to outside the plat everytime + for ( n = 0; n < 3; n++ ) + { + for ( k = 0; k < 3; k++ ) + { + mins[k] -= 4; + maxs[k] += 4; + } //end for + xvals_top[0] = mins[0]; xvals_top[1] = mids[0]; xvals_top[2] = maxs[0]; xvals_top[3] = mids[0]; + yvals_top[0] = mids[1]; yvals_top[1] = maxs[1]; yvals_top[2] = mids[1]; yvals_top[3] = mins[1]; + // + xvals_top[4] = mins[0]; xvals_top[5] = maxs[0]; xvals_top[6] = maxs[0]; xvals_top[7] = mins[0]; + yvals_top[4] = maxs[1]; yvals_top[5] = maxs[1]; yvals_top[6] = mins[1]; yvals_top[7] = mins[1]; + // + for ( j = 0; j < 8; j++ ) + { + toporg[0] = origin[0] + xvals_top[j]; + toporg[1] = origin[1] + yvals_top[j]; + toporg[2] = plattop[2] + 16; + //get a grounded or swim area near the plat in the top position + area2num = AAS_PointAreaNum( toporg ); + for ( l = 0; l < 16; l++ ) + { + if ( area2num ) { + if ( AAS_AreaGrounded( area2num ) || AAS_AreaSwim( area2num ) ) { + VectorCopy( plattop, start ); + start[2] += 32; + VectorCopy( toporg, end ); + end[2] += 1; + trace = AAS_TraceClientBBox( start, end, PRESENCE_CROUCH, -1 ); + if ( trace.fraction >= 1 ) { + break; + } + } //end if + } //end if + toporg[2] += 4; + area2num = AAS_PointAreaNum( toporg ); + } //end if + //if in solid + if ( l >= 16 ) { + continue; + } + //never create a reachability in the same area + if ( area2num == area1num ) { + continue; + } + //if the area isn't grounded + if ( !AAS_AreaGrounded( area2num ) ) { + continue; + } + //if there already exists reachability between the areas + if ( AAS_ReachabilityExists( area1num, area2num ) ) { + continue; + } + //if the reachability start is within the elevator bounding box + VectorSubtract( bottomorg, platbottom, dir ); + VectorNormalize( dir ); + dir[0] = bottomorg[0] + 24 * dir[0]; + dir[1] = bottomorg[1] + 24 * dir[1]; + dir[2] = bottomorg[2]; + // + for ( p = 0; p < 3; p++ ) + if ( dir[p] < origin[p] + mins[p] || dir[p] > origin[p] + maxs[p] ) { + break; + } + if ( p >= 3 ) { + continue; + } + //create a new reachability link + lreach = AAS_AllocReachability(); + if ( !lreach ) { + continue; + } + lreach->areanum = area2num; + //the facenum is the model number + lreach->facenum = modelnum; + //the edgenum is the height + lreach->edgenum = (int) height; + // + VectorCopy( dir, lreach->start ); + VectorCopy( toporg, lreach->end ); + lreach->traveltype = TRAVEL_ELEVATOR; + lreach->traveltime = height * 100 / speed; + if ( !lreach->traveltime ) { + lreach->traveltime = 50; + } + lreach->next = areareachability[area1num]; + areareachability[area1num] = lreach; + //don't go any further to the outside + n = 9999; + // +#ifdef REACHDEBUG + Log_Write( "elevator reach from %d to %d\r\n", area1num, area2num ); +#endif //REACHDEBUG + // + reach_elevator++; + } //end for + } //end for + } //end for + } //end if + } //end for +} //end of the function AAS_Reachability_Elevator +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +aas_lreachability_t *AAS_FindFaceReachabilities( vec3_t *facepoints, int numpoints, aas_plane_t *plane, int towardsface ) { + int i, j, k, l; + int facenum, edgenum, bestfacenum; + float *v1, *v2, *v3, *v4; + float bestdist, speed, hordist, dist; + vec3_t beststart, beststart2, bestend, bestend2, tmp, hordir, testpoint; + aas_lreachability_t *lreach, *lreachabilities; + aas_area_t *area; + aas_face_t *face; + aas_edge_t *edge; + aas_plane_t *faceplane, *bestfaceplane; + + // + lreachabilities = NULL; + bestfacenum = 0; + bestfaceplane = NULL; + // + for ( i = 1; i < ( *aasworld ).numareas; i++ ) + { + area = &( *aasworld ).areas[i]; + // get the shortest distance between one of the func_bob start edges and + // one of the face edges of area1 + bestdist = 999999; + for ( j = 0; j < area->numfaces; j++ ) + { + facenum = ( *aasworld ).faceindex[area->firstface + j]; + face = &( *aasworld ).faces[abs( facenum )]; + //if not a ground face + if ( !( face->faceflags & FACE_GROUND ) ) { + continue; + } + //get the ground planes + faceplane = &( *aasworld ).planes[face->planenum]; + // + for ( k = 0; k < face->numedges; k++ ) + { + edgenum = abs( ( *aasworld ).edgeindex[face->firstedge + k] ); + edge = &( *aasworld ).edges[edgenum]; + //calculate the minimum distance between the two edges + v1 = ( *aasworld ).vertexes[edge->v[0]]; + v2 = ( *aasworld ).vertexes[edge->v[1]]; + // + for ( l = 0; l < numpoints; l++ ) + { + v3 = facepoints[l]; + v4 = facepoints[( l + 1 ) % numpoints]; + dist = AAS_ClosestEdgePoints( v1, v2, v3, v4, faceplane, plane, + beststart, bestend, + beststart2, bestend2, bestdist ); + if ( dist < bestdist ) { + bestfacenum = facenum; + bestfaceplane = faceplane; + bestdist = dist; + } //end if + } //end for + } //end for + } //end for + // + if ( bestdist > 192 ) { + continue; + } + // + VectorMiddle( beststart, beststart2, beststart ); + VectorMiddle( bestend, bestend2, bestend ); + // + if ( !towardsface ) { + VectorCopy( beststart, tmp ); + VectorCopy( bestend, beststart ); + VectorCopy( tmp, bestend ); + } //end if + // + VectorSubtract( bestend, beststart, hordir ); + hordir[2] = 0; + hordist = VectorLength( hordir ); + // + if ( hordist > 2 * AAS_MaxJumpDistance( aassettings.sv_jumpvel ) ) { + continue; + } + //the end point should not be significantly higher than the start point + if ( bestend[2] - 32 > beststart[2] ) { + continue; + } + //don't fall down too far + if ( bestend[2] < beststart[2] - 128 ) { + continue; + } + //the distance should not be too far + if ( hordist > 32 ) { + //check for walk off ledge + if ( !AAS_HorizontalVelocityForJump( 0, beststart, bestend, &speed ) ) { + continue; + } + } //end if + // + beststart[2] += 1; + bestend[2] += 1; + // + if ( towardsface ) { + VectorCopy( bestend, testpoint ); + } else { VectorCopy( beststart, testpoint );} + testpoint[2] = 0; + testpoint[2] = ( bestfaceplane->dist - DotProduct( bestfaceplane->normal, testpoint ) ) / bestfaceplane->normal[2]; + // + if ( !AAS_PointInsideFace( bestfacenum, testpoint, 0.1 ) ) { + //if the faces are not overlapping then only go down + if ( bestend[2] - 16 > beststart[2] ) { + continue; + } + } //end if + lreach = AAS_AllocReachability(); + if ( !lreach ) { + return lreachabilities; + } + lreach->areanum = i; + lreach->facenum = 0; + lreach->edgenum = 0; + VectorCopy( beststart, lreach->start ); + VectorCopy( bestend, lreach->end ); + lreach->traveltype = 0; + lreach->traveltime = 0; + lreach->next = lreachabilities; + lreachabilities = lreach; +#ifndef BSPC + if ( towardsface ) { + AAS_PermanentLine( lreach->start, lreach->end, 1 ); + } else { AAS_PermanentLine( lreach->start, lreach->end, 2 );} +#endif + } //end for + return lreachabilities; +} //end of the function AAS_FindFaceReachabilities +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_Reachability_FuncBobbing( void ) { + int ent, spawnflags, modelnum, axis; + int i, numareas, areas[10]; + char classname[MAX_EPAIRKEY], model[MAX_EPAIRKEY]; + vec3_t origin, move_end, move_start, move_start_top, move_end_top; + vec3_t mins, maxs, angles = {0, 0, 0}; + vec3_t start_edgeverts[4], end_edgeverts[4], mid; + vec3_t org, start, end, dir, points[10]; + float height; + aas_plane_t start_plane, end_plane; + aas_lreachability_t *startreach, *endreach, *nextstartreach, *nextendreach, *lreach; + aas_lreachability_t *firststartreach, *firstendreach; + + for ( ent = AAS_NextBSPEntity( 0 ); ent; ent = AAS_NextBSPEntity( ent ) ) + { + if ( !AAS_ValueForBSPEpairKey( ent, "classname", classname, MAX_EPAIRKEY ) ) { + continue; + } + if ( strcmp( classname, "func_bobbing" ) ) { + continue; + } + AAS_FloatForBSPEpairKey( ent, "height", &height ); + if ( !height ) { + height = 32; + } + // + if ( !AAS_ValueForBSPEpairKey( ent, "model", model, MAX_EPAIRKEY ) ) { + botimport.Print( PRT_ERROR, "func_bobbing without model\n" ); + continue; + } //end if + //get the model number, and skip the leading * + modelnum = atoi( model + 1 ); + if ( modelnum <= 0 ) { + botimport.Print( PRT_ERROR, "func_bobbing with invalid model number\n" ); + continue; + } //end if + // + AAS_BSPModelMinsMaxsOrigin( modelnum, angles, mins, maxs, NULL ); + // + VectorAdd( mins, maxs, mid ); + VectorScale( mid, 0.5, mid ); + //VectorAdd(mid, origin, mid); + VectorCopy( mid, origin ); + // + VectorCopy( origin, move_end ); + VectorCopy( origin, move_start ); + // + AAS_IntForBSPEpairKey( ent, "spawnflags", &spawnflags ); + // set the axis of bobbing + if ( spawnflags & 1 ) { + axis = 0; + } else if ( spawnflags & 2 ) { + axis = 1; + } else { axis = 2;} + // + move_start[axis] -= height; + move_end[axis] += height; + // + Log_Write( "funcbob model %d, start = {%1.1f, %1.1f, %1.1f} end = {%1.1f, %1.1f, %1.1f}\n", + modelnum, move_start[0], move_start[1], move_start[2], move_end[0], move_end[1], move_end[2] ); + // +#ifndef BSPC + /* + AAS_DrawPermanentCross(move_start, 4, 1); + AAS_DrawPermanentCross(move_end, 4, 2); + */ +#endif + // + for ( i = 0; i < 4; i++ ) + { + VectorCopy( move_start, start_edgeverts[i] ); + start_edgeverts[i][2] += maxs[2] - mid[2]; //+ bbox maxs z + start_edgeverts[i][2] += 24; //+ player origin to ground dist + } //end for + start_edgeverts[0][0] += maxs[0] - mid[0]; + start_edgeverts[0][1] += maxs[1] - mid[1]; + start_edgeverts[1][0] += maxs[0] - mid[0]; + start_edgeverts[1][1] += mins[1] - mid[1]; + start_edgeverts[2][0] += mins[0] - mid[0]; + start_edgeverts[2][1] += mins[1] - mid[1]; + start_edgeverts[3][0] += mins[0] - mid[0]; + start_edgeverts[3][1] += maxs[1] - mid[1]; + // + start_plane.dist = start_edgeverts[0][2]; + VectorSet( start_plane.normal, 0, 0, 1 ); + // + for ( i = 0; i < 4; i++ ) + { + VectorCopy( move_end, end_edgeverts[i] ); + end_edgeverts[i][2] += maxs[2] - mid[2]; //+ bbox maxs z + end_edgeverts[i][2] += 24; //+ player origin to ground dist + } //end for + end_edgeverts[0][0] += maxs[0] - mid[0]; + end_edgeverts[0][1] += maxs[1] - mid[1]; + end_edgeverts[1][0] += maxs[0] - mid[0]; + end_edgeverts[1][1] += mins[1] - mid[1]; + end_edgeverts[2][0] += mins[0] - mid[0]; + end_edgeverts[2][1] += mins[1] - mid[1]; + end_edgeverts[3][0] += mins[0] - mid[0]; + end_edgeverts[3][1] += maxs[1] - mid[1]; + // + end_plane.dist = end_edgeverts[0][2]; + VectorSet( end_plane.normal, 0, 0, 1 ); + // +#ifndef BSPC + /* + for (i = 0; i < 4; i++) + { + AAS_PermanentLine(start_edgeverts[i], start_edgeverts[(i+1)%4], 1); + AAS_PermanentLine(end_edgeverts[i], end_edgeverts[(i+1)%4], 1); + } //end for + */ +#endif + VectorCopy( move_start, move_start_top ); + move_start_top[2] += maxs[2] - mid[2] + 24; //+ bbox maxs z + VectorCopy( move_end, move_end_top ); + move_end_top[2] += maxs[2] - mid[2] + 24; //+ bbox maxs z + // + if ( !AAS_PointAreaNum( move_start_top ) ) { + continue; + } + if ( !AAS_PointAreaNum( move_end_top ) ) { + continue; + } + // + for ( i = 0; i < 2; i++ ) + { + firststartreach = firstendreach = NULL; + // + if ( i == 0 ) { + firststartreach = AAS_FindFaceReachabilities( start_edgeverts, 4, &start_plane, qtrue ); + firstendreach = AAS_FindFaceReachabilities( end_edgeverts, 4, &end_plane, qfalse ); + } //end if + else + { + firststartreach = AAS_FindFaceReachabilities( end_edgeverts, 4, &end_plane, qtrue ); + firstendreach = AAS_FindFaceReachabilities( start_edgeverts, 4, &start_plane, qfalse ); + } //end else + // + //create reachabilities from start to end + for ( startreach = firststartreach; startreach; startreach = nextstartreach ) + { + nextstartreach = startreach->next; + // + //trace = AAS_TraceClientBBox(startreach->start, move_start_top, PRESENCE_NORMAL, -1); + //if (trace.fraction < 1) continue; + // + for ( endreach = firstendreach; endreach; endreach = nextendreach ) + { + nextendreach = endreach->next; + // + //trace = AAS_TraceClientBBox(endreach->end, move_end_top, PRESENCE_NORMAL, -1); + //if (trace.fraction < 1) continue; + // + Log_Write( "funcbob reach from area %d to %d\n", startreach->areanum, endreach->areanum ); + // + // + if ( i == 0 ) { + VectorCopy( move_start_top, org ); + } else { VectorCopy( move_end_top, org );} + VectorSubtract( startreach->start, org, dir ); + dir[2] = 0; + VectorNormalize( dir ); + VectorCopy( startreach->start, start ); + VectorMA( startreach->start, 1, dir, start ); + start[2] += 1; + VectorMA( startreach->start, 16, dir, end ); + end[2] += 1; + // + numareas = AAS_TraceAreas( start, end, areas, points, 10 ); + if ( numareas <= 0 ) { + continue; + } + if ( numareas > 1 ) { + VectorCopy( points[1], startreach->start ); + } else { VectorCopy( end, startreach->start );} + // + if ( !AAS_PointAreaNum( startreach->start ) ) { + continue; + } + if ( !AAS_PointAreaNum( endreach->end ) ) { + continue; + } + // + lreach = AAS_AllocReachability(); + lreach->areanum = endreach->areanum; + if ( i == 0 ) { + lreach->edgenum = ( (int)move_start[axis] << 16 ) | ( (int) move_end[axis] & 0x0000ffff ); + } else { lreach->edgenum = ( (int)move_end[axis] << 16 ) | ( (int) move_start[axis] & 0x0000ffff );} + lreach->facenum = ( spawnflags << 16 ) | modelnum; + VectorCopy( startreach->start, lreach->start ); + VectorCopy( endreach->end, lreach->end ); +#ifndef BSPC +// AAS_DrawArrow(lreach->start, lreach->end, LINECOLOR_BLUE, LINECOLOR_YELLOW); +// AAS_PermanentLine(lreach->start, lreach->end, 1); +#endif + lreach->traveltype = TRAVEL_FUNCBOB; + lreach->traveltime = 300; + reach_funcbob++; + lreach->next = areareachability[startreach->areanum]; + areareachability[startreach->areanum] = lreach; + // + } //end for + } //end for + for ( startreach = firststartreach; startreach; startreach = nextstartreach ) + { + nextstartreach = startreach->next; + AAS_FreeReachability( startreach ); + } //end for + for ( endreach = firstendreach; endreach; endreach = nextendreach ) + { + nextendreach = endreach->next; + AAS_FreeReachability( endreach ); + } //end for + //only go up with func_bobbing entities that go up and down + if ( !( spawnflags & 1 ) && !( spawnflags & 2 ) ) { + break; + } + } //end for + } //end for +} //end of the function AAS_Reachability_FuncBobbing +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_Reachability_JumpPad( void ) { + int face2num, i, ret, modelnum, area2num, visualize; + float speed, zvel, hordist, dist, time, height, gravity, forward; + aas_face_t *face2; + aas_area_t *area2; + aas_lreachability_t *lreach; + vec3_t areastart, facecenter, dir, cmdmove, teststart; + vec3_t velocity, origin, ent2origin, angles, absmins, absmaxs; + aas_clientmove_t move; + aas_trace_t trace; + int ent, ent2; + aas_link_t *areas, *link; + char target[MAX_EPAIRKEY], targetname[MAX_EPAIRKEY]; + char classname[MAX_EPAIRKEY], model[MAX_EPAIRKEY]; + + for ( ent = AAS_NextBSPEntity( 0 ); ent; ent = AAS_NextBSPEntity( ent ) ) + { + if ( !AAS_ValueForBSPEpairKey( ent, "classname", classname, MAX_EPAIRKEY ) ) { + continue; + } + if ( strcmp( classname, "trigger_push" ) ) { + continue; + } + // + AAS_FloatForBSPEpairKey( ent, "speed", &speed ); + if ( !speed ) { + speed = 1000; + } +// AAS_VectorForBSPEpairKey(ent, "angles", angles); +// AAS_SetMovedir(angles, velocity); +// VectorScale(velocity, speed, velocity); + VectorClear( angles ); + //get the mins, maxs and origin of the model + AAS_ValueForBSPEpairKey( ent, "model", model, MAX_EPAIRKEY ); + if ( model[0] ) { + modelnum = atoi( model + 1 ); + } else { modelnum = 0;} + AAS_BSPModelMinsMaxsOrigin( modelnum, angles, absmins, absmaxs, origin ); + VectorAdd( origin, absmins, absmins ); + VectorAdd( origin, absmaxs, absmaxs ); + // +#ifdef REACHDEBUG + botimport.Print( PRT_MESSAGE, "absmins = %f %f %f\n", absmins[0], absmins[1], absmins[2] ); + botimport.Print( PRT_MESSAGE, "absmaxs = %f %f %f\n", absmaxs[0], absmaxs[1], absmaxs[2] ); +#endif //REACHDEBUG + VectorAdd( absmins, absmaxs, origin ); + VectorScale( origin, 0.5, origin ); + + //get the start areas + VectorCopy( origin, teststart ); + teststart[2] += 64; + trace = AAS_TraceClientBBox( teststart, origin, PRESENCE_CROUCH, -1 ); + if ( trace.startsolid ) { + botimport.Print( PRT_MESSAGE, "trigger_push start solid\n" ); + VectorCopy( origin, areastart ); + } //end if + else + { + VectorCopy( trace.endpos, areastart ); + } //end else + areastart[2] += 0.125; + // + //AAS_DrawPermanentCross(origin, 4, 4); + //get the target entity + AAS_ValueForBSPEpairKey( ent, "target", target, MAX_EPAIRKEY ); + for ( ent2 = AAS_NextBSPEntity( 0 ); ent2; ent2 = AAS_NextBSPEntity( ent2 ) ) + { + if ( !AAS_ValueForBSPEpairKey( ent2, "targetname", targetname, MAX_EPAIRKEY ) ) { + continue; + } + if ( !strcmp( targetname, target ) ) { + break; + } + } //end for + if ( !ent2 ) { + botimport.Print( PRT_MESSAGE, "trigger_push without target entity %s\n", target ); + continue; + } //end if + AAS_VectorForBSPEpairKey( ent2, "origin", ent2origin ); + // + height = ent2origin[2] - origin[2]; + gravity = aassettings.sv_gravity; + time = sqrt( height / ( 0.5 * gravity ) ); + if ( !time ) { + botimport.Print( PRT_MESSAGE, "trigger_push without time\n" ); + continue; + } //end if + // set s.origin2 to the push velocity + VectorSubtract( ent2origin, origin, velocity ); + dist = VectorNormalize( velocity ); + forward = dist / time; + //FIXME: why multiply by 1.1 + forward *= 1.1; + VectorScale( velocity, forward, velocity ); + velocity[2] = time * gravity; + //get the areas the jump pad brush is in + areas = AAS_LinkEntityClientBBox( absmins, absmaxs, -1, PRESENCE_CROUCH ); + //* + for ( link = areas; link; link = link->next_area ) + { + if ( link->areanum == 5772 ) { + ret = qfalse; + } + } //*/ + for ( link = areas; link; link = link->next_area ) + { + if ( AAS_AreaJumpPad( link->areanum ) ) { + break; + } + } //end for + if ( !link ) { + botimport.Print( PRT_MESSAGE, "trigger_multiple not in any jump pad area\n" ); + AAS_UnlinkFromAreas( areas ); + continue; + } //end if + // + botimport.Print( PRT_MESSAGE, "found a trigger_push with velocity %f %f %f\n", velocity[0], velocity[1], velocity[2] ); + //if there is a horizontal velocity check for a reachability without air control + if ( velocity[0] || velocity[1] ) { + VectorSet( cmdmove, 0, 0, 0 ); + //VectorCopy(velocity, cmdmove); + //cmdmove[2] = 0; + memset( &move, 0, sizeof( aas_clientmove_t ) ); + area2num = 0; + for ( i = 0; i < 20; i++ ) + { + AAS_PredictClientMovement( &move, -1, areastart, PRESENCE_NORMAL, qfalse, + velocity, cmdmove, 0, 30, 0.1, + SE_HITGROUND | SE_ENTERWATER | SE_ENTERSLIME | + SE_ENTERLAVA | SE_HITGROUNDDAMAGE | SE_TOUCHJUMPPAD | SE_TOUCHTELEPORTER, 0, qfalse ); //qtrue); + area2num = AAS_PointAreaNum( move.endpos ); + for ( link = areas; link; link = link->next_area ) + { + if ( !AAS_AreaJumpPad( link->areanum ) ) { + continue; + } + if ( link->areanum == area2num ) { + break; + } + } //end if + if ( !link ) { + break; + } + VectorCopy( move.endpos, areastart ); + VectorCopy( move.velocity, velocity ); + } //end for + if ( area2num && i < 20 ) { + for ( link = areas; link; link = link->next_area ) + { + if ( !AAS_AreaJumpPad( link->areanum ) ) { + continue; + } + if ( AAS_ReachabilityExists( link->areanum, area2num ) ) { + continue; + } + //create a rocket or bfg jump reachability from area1 to area2 + lreach = AAS_AllocReachability(); + if ( !lreach ) { + AAS_UnlinkFromAreas( areas ); + return; + } //end if + lreach->areanum = area2num; + //NOTE: the facenum is the Z velocity + lreach->facenum = velocity[2]; + //NOTE: the edgenum is the horizontal velocity + lreach->edgenum = sqrt( velocity[0] * velocity[0] + velocity[1] * velocity[1] ); + VectorCopy( areastart, lreach->start ); + VectorCopy( move.endpos, lreach->end ); + lreach->traveltype = TRAVEL_JUMPPAD; + lreach->traveltime = 200; + lreach->next = areareachability[link->areanum]; + areareachability[link->areanum] = lreach; + // + reach_jumppad++; + } //end for + } //end if + } //end if + // + if ( fabs( velocity[0] ) > 100 || fabs( velocity[1] ) > 100 ) { + continue; + } + //check for areas we can reach with air control + for ( area2num = 1; area2num < ( *aasworld ).numareas; area2num++ ) + { + visualize = qfalse; + /* + if (area2num == 3568) + { + for (link = areas; link; link = link->next_area) + { + if (link->areanum == 3380) + { + visualize = qtrue; + botimport.Print(PRT_MESSAGE, "bah\n"); + } //end if + } //end for + } //end if*/ + //never try to go back to one of the original jumppad areas + //and don't create reachabilities if they already exist + for ( link = areas; link; link = link->next_area ) + { + if ( AAS_ReachabilityExists( link->areanum, area2num ) ) { + break; + } + if ( AAS_AreaJumpPad( link->areanum ) ) { + if ( link->areanum == area2num ) { + break; + } + } //end if + } //end if + if ( link ) { + continue; + } + // + area2 = &( *aasworld ).areas[area2num]; + for ( i = 0; i < area2->numfaces; i++ ) + { + face2num = ( *aasworld ).faceindex[area2->firstface + i]; + face2 = &( *aasworld ).faces[abs( face2num )]; + //if it is not a ground face + if ( !( face2->faceflags & FACE_GROUND ) ) { + continue; + } + //get the center of the face + AAS_FaceCenter( face2num, facecenter ); + //only go higher up + if ( facecenter[2] < areastart[2] ) { + continue; + } + //get the jumppad jump z velocity + zvel = velocity[2]; + //get the horizontal speed for the jump, if it isn't possible to calculate this + //speed (the jump is not possible) then there's no jump reachability created + ret = AAS_HorizontalVelocityForJump( zvel, areastart, facecenter, &speed ); + if ( ret && speed < 150 ) { + //direction towards the face center + VectorSubtract( facecenter, areastart, dir ); + dir[2] = 0; + hordist = VectorNormalize( dir ); + //if (hordist < 1.6 * facecenter[2] - areastart[2]) + { + //get command movement + VectorScale( dir, speed, cmdmove ); + // + AAS_PredictClientMovement( &move, -1, areastart, PRESENCE_NORMAL, qfalse, + velocity, cmdmove, 30, 30, 0.1, + SE_ENTERWATER | SE_ENTERSLIME | + SE_ENTERLAVA | SE_HITGROUNDDAMAGE | + SE_TOUCHJUMPPAD | SE_TOUCHTELEPORTER | SE_HITGROUNDAREA, area2num, visualize ); + //if prediction time wasn't enough to fully predict the movement + //don't enter slime or lava and don't fall from too high + if ( move.frames < 30 && + !( move.stopevent & ( SE_ENTERSLIME | SE_ENTERLAVA | SE_HITGROUNDDAMAGE ) ) + && ( move.stopevent & ( SE_HITGROUNDAREA | SE_TOUCHJUMPPAD | SE_TOUCHTELEPORTER ) ) ) { + for ( link = areas; link; link = link->next_area ) + { + if ( !AAS_AreaJumpPad( link->areanum ) ) { + continue; + } + if ( AAS_ReachabilityExists( link->areanum, area2num ) ) { + continue; + } + //create a jumppad reachability from area1 to area2 + lreach = AAS_AllocReachability(); + if ( !lreach ) { + AAS_UnlinkFromAreas( areas ); + return; + } //end if + lreach->areanum = area2num; + //NOTE: the facenum is the Z velocity + lreach->facenum = velocity[2]; + //NOTE: the edgenum is the horizontal velocity + lreach->edgenum = sqrt( cmdmove[0] * cmdmove[0] + cmdmove[1] * cmdmove[1] ); + VectorCopy( areastart, lreach->start ); + VectorCopy( facecenter, lreach->end ); + lreach->traveltype = TRAVEL_JUMPPAD; + lreach->traveltime = 250; + lreach->next = areareachability[link->areanum]; + areareachability[link->areanum] = lreach; + // + reach_jumppad++; + } //end for + } //end if + } //end if + } //end for + } //end for + } //end for + AAS_UnlinkFromAreas( areas ); + } //end for +} //end of the function AAS_Reachability_JumpPad +//=========================================================================== +// never point at ground faces +// always a higher and pretty far area +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_Reachability_Grapple( int area1num, int area2num ) { + int face2num, i, j, areanum, numareas, areas[20]; + float mingrappleangle, z, hordist; + bsp_trace_t bsptrace; + aas_trace_t trace; + aas_face_t *face2; + aas_area_t *area1, *area2; + aas_lreachability_t *lreach; + vec3_t areastart, facecenter, start, end, dir, down = {0, 0, -1}; + vec_t *v; + + //only grapple when on the ground or swimming + if ( !AAS_AreaGrounded( area1num ) && !AAS_AreaSwim( area1num ) ) { + return qfalse; + } + //don't grapple from a crouch area + if ( !( AAS_AreaPresenceType( area1num ) & PRESENCE_NORMAL ) ) { + return qfalse; + } + //NOTE: disabled area swim it doesn't work right + if ( AAS_AreaSwim( area1num ) ) { + return qfalse; + } + // + area1 = &( *aasworld ).areas[area1num]; + area2 = &( *aasworld ).areas[area2num]; + //don't grapple towards way lower areas + if ( area2->maxs[2] < area1->mins[2] ) { + return qfalse; + } + // + VectorCopy( ( *aasworld ).areas[area1num].center, start ); + //if not a swim area + if ( !AAS_AreaSwim( area1num ) ) { + if ( !AAS_PointAreaNum( start ) ) { + Log_Write( "area %d center %f %f %f in solid?\r\n", area1num, + start[0], start[1], start[2] ); + } + VectorCopy( start, end ); + end[2] -= 1000; + trace = AAS_TraceClientBBox( start, end, PRESENCE_CROUCH, -1 ); + if ( trace.startsolid ) { + return qfalse; + } + VectorCopy( trace.endpos, areastart ); + } //end if + else + { + if ( !( AAS_PointContents( start ) & ( CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER ) ) ) { + return qfalse; + } + } //end else + // + //start is now the start point + // + for ( i = 0; i < area2->numfaces; i++ ) + { + face2num = ( *aasworld ).faceindex[area2->firstface + i]; + face2 = &( *aasworld ).faces[abs( face2num )]; + //if it is not a solid face + if ( !( face2->faceflags & FACE_SOLID ) ) { + continue; + } + //direction towards the first vertex of the face + v = ( *aasworld ).vertexes[( *aasworld ).edges[abs( ( *aasworld ).edgeindex[face2->firstedge] )].v[0]]; + VectorSubtract( v, areastart, dir ); + //if the face plane is facing away + if ( DotProduct( ( *aasworld ).planes[face2->planenum].normal, dir ) > 0 ) { + continue; + } + //get the center of the face + AAS_FaceCenter( face2num, facecenter ); + //only go higher up with the grapple + if ( facecenter[2] < areastart[2] + 64 ) { + continue; + } + //only use vertical faces or downward facing faces + if ( DotProduct( ( *aasworld ).planes[face2->planenum].normal, down ) < 0 ) { + continue; + } + //direction towards the face center + VectorSubtract( facecenter, areastart, dir ); + // + z = dir[2]; + dir[2] = 0; + hordist = VectorLength( dir ); + if ( !hordist ) { + continue; + } + //if too far + if ( hordist > 2000 ) { + continue; + } + //check the minimal angle of the movement + mingrappleangle = 15; //15 degrees + if ( z / hordist < tan( 2 * M_PI * mingrappleangle / 360 ) ) { + continue; + } + // + VectorCopy( facecenter, start ); + VectorMA( facecenter, -500, ( *aasworld ).planes[face2->planenum].normal, end ); + // + bsptrace = AAS_Trace( start, NULL, NULL, end, 0, CONTENTS_SOLID ); + //the grapple won't stick to the sky and the grapple point should be near the AAS wall + if ( ( bsptrace.surface.flags & SURF_SKY ) || ( bsptrace.fraction * 500 > 32 ) ) { + continue; + } + //trace a full bounding box from the area center on the ground to + //the center of the face + VectorSubtract( facecenter, areastart, dir ); + VectorNormalize( dir ); + VectorMA( areastart, 4, dir, start ); + VectorCopy( bsptrace.endpos, end ); + trace = AAS_TraceClientBBox( start, end, PRESENCE_NORMAL, -1 ); + VectorSubtract( trace.endpos, facecenter, dir ); + if ( VectorLength( dir ) > 24 ) { + continue; + } + // + VectorCopy( trace.endpos, start ); + VectorCopy( trace.endpos, end ); + end[2] -= AAS_FallDamageDistance(); + trace = AAS_TraceClientBBox( start, end, PRESENCE_NORMAL, -1 ); + if ( trace.fraction >= 1 ) { + continue; + } + //area to end in + areanum = AAS_PointAreaNum( trace.endpos ); + //if not in lava or slime + if ( ( *aasworld ).areasettings[areanum].contents & AREACONTENTS_LAVA ) { //----(SA) modified since slime is no longer deadly +// if ((*aasworld).areasettings[areanum].contents & (AREACONTENTS_SLIME|AREACONTENTS_LAVA)) + continue; + } //end if + //do not go the the source area + if ( areanum == area1num ) { + continue; + } + //don't create reachabilities if they already exist + if ( AAS_ReachabilityExists( area1num, areanum ) ) { + continue; + } + //only end in areas we can stand + if ( !AAS_AreaGrounded( areanum ) ) { + continue; + } + //never go through cluster portals!! + numareas = AAS_TraceAreas( areastart, bsptrace.endpos, areas, NULL, 20 ); + if ( numareas >= 20 ) { + continue; + } + for ( j = 0; j < numareas; j++ ) + { + if ( ( *aasworld ).areasettings[areas[j]].contents & AREACONTENTS_CLUSTERPORTAL ) { + break; + } + } //end for + if ( j < numareas ) { + continue; + } + //create a new reachability link + lreach = AAS_AllocReachability(); + if ( !lreach ) { + return qfalse; + } + lreach->areanum = areanum; + lreach->facenum = face2num; + lreach->edgenum = 0; + VectorCopy( areastart, lreach->start ); + //VectorCopy(facecenter, lreach->end); + VectorCopy( bsptrace.endpos, lreach->end ); + lreach->traveltype = TRAVEL_GRAPPLEHOOK; + VectorSubtract( lreach->end, lreach->start, dir ); + lreach->traveltime = STARTGRAPPLE_TIME + VectorLength( dir ) * 0.25; + lreach->next = areareachability[area1num]; + areareachability[area1num] = lreach; + // + reach_grapple++; + } //end for + // + return qfalse; +} //end of the function AAS_Reachability_Grapple +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_SetWeaponJumpAreaFlags( void ) { + int ent, i; + vec3_t mins = {-15, -15, -15}, maxs = {15, 15, 15}; + vec3_t origin; + int areanum, weaponjumpareas, spawnflags; + char classname[MAX_EPAIRKEY]; + + weaponjumpareas = 0; + for ( ent = AAS_NextBSPEntity( 0 ); ent; ent = AAS_NextBSPEntity( ent ) ) + { + if ( !AAS_ValueForBSPEpairKey( ent, "classname", classname, MAX_EPAIRKEY ) ) { + continue; + } + if ( + !strcmp( classname, "item_armor_body" ) || + !strcmp( classname, "item_armor_combat" ) || + !strcmp( classname, "item_health_mega" ) || + !strcmp( classname, "weapon_grenadelauncher" ) || + !strcmp( classname, "weapon_rocketlauncher" ) || + !strcmp( classname, "weapon_lightning" ) || + !strcmp( classname, "weapon_sp5" ) || + !strcmp( classname, "weapon_railgun" ) || + !strcmp( classname, "weapon_bfg" ) || + !strcmp( classname, "item_quad" ) || + !strcmp( classname, "item_regen" ) || + !strcmp( classname, "item_invulnerability" ) ) { + if ( AAS_VectorForBSPEpairKey( ent, "origin", origin ) ) { + spawnflags = 0; + AAS_IntForBSPEpairKey( ent, "spawnflags", &spawnflags ); + //if not a stationary item + if ( !( spawnflags & 1 ) ) { + if ( !AAS_DropToFloor( origin, mins, maxs ) ) { + botimport.Print( PRT_MESSAGE, "%s in solid at (%1.1f %1.1f %1.1f)\n", + classname, origin[0], origin[1], origin[2] ); + } //end if + } //end if + //areanum = AAS_PointAreaNum(origin); + areanum = AAS_BestReachableArea( origin, mins, maxs, origin ); + //the bot may rocket jump towards this area + ( *aasworld ).areasettings[areanum].areaflags |= AREA_WEAPONJUMP; + // + if ( !AAS_AreaGrounded( areanum ) ) { + botimport.Print( PRT_MESSAGE, "area not grounded\n" ); + } + // + weaponjumpareas++; + } //end if + } //end if + } //end for + for ( i = 1; i < ( *aasworld ).numareas; i++ ) + { + if ( ( *aasworld ).areasettings[i].contents & AREACONTENTS_JUMPPAD ) { + ( *aasworld ).areasettings[i].areaflags |= AREA_WEAPONJUMP; + weaponjumpareas++; + } //end if + } //end for + botimport.Print( PRT_MESSAGE, "%d weapon jump areas\n", weaponjumpareas ); +} //end of the function AAS_SetWeaponJumpAreaFlags +//=========================================================================== +// create a possible weapon jump reachability from area1 to area2 +// +// check if there's a cool item in the second area +// check if area1 is lower than area2 +// check if the bot can rocketjump from area1 to area2 +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_Reachability_WeaponJump( int area1num, int area2num ) { + int face2num, i, n, ret; + float speed, zvel, hordist; + aas_face_t *face2; + aas_area_t *area1, *area2; + aas_lreachability_t *lreach; + vec3_t areastart, facecenter, start, end, dir, cmdmove; // teststart; + vec3_t velocity; + aas_clientmove_t move; + aas_trace_t trace; + + if ( !AAS_AreaGrounded( area1num ) || AAS_AreaSwim( area1num ) ) { + return qfalse; + } + if ( !AAS_AreaGrounded( area2num ) ) { + return qfalse; + } + //NOTE: only weapon jump towards areas with an interesting item in it?? + if ( !( ( *aasworld ).areasettings[area2num].areaflags & AREA_WEAPONJUMP ) ) { + return qfalse; + } + // + area1 = &( *aasworld ).areas[area1num]; + area2 = &( *aasworld ).areas[area2num]; + //don't weapon jump towards way lower areas + if ( area2->maxs[2] < area1->mins[2] ) { + return qfalse; + } + // + VectorCopy( ( *aasworld ).areas[area1num].center, start ); + //if not a swim area + if ( !AAS_PointAreaNum( start ) ) { + Log_Write( "area %d center %f %f %f in solid?\r\n", area1num, + start[0], start[1], start[2] ); + } + VectorCopy( start, end ); + end[2] -= 1000; + trace = AAS_TraceClientBBox( start, end, PRESENCE_CROUCH, -1 ); + if ( trace.startsolid ) { + return qfalse; + } + VectorCopy( trace.endpos, areastart ); + // + //areastart is now the start point + // + for ( i = 0; i < area2->numfaces; i++ ) + { + face2num = ( *aasworld ).faceindex[area2->firstface + i]; + face2 = &( *aasworld ).faces[abs( face2num )]; + //if it is not a solid face + if ( !( face2->faceflags & FACE_GROUND ) ) { + continue; + } + //get the center of the face + AAS_FaceCenter( face2num, facecenter ); + //only go higher up with weapon jumps + if ( facecenter[2] < areastart[2] + 64 ) { + continue; + } + //NOTE: set to 2 to allow bfg jump reachabilities + for ( n = 0; n < 1 /*2*/; n++ ) + { + //get the rocket jump z velocity + if ( n ) { + zvel = AAS_BFGJumpZVelocity( areastart ); + } else { zvel = AAS_RocketJumpZVelocity( areastart );} + //get the horizontal speed for the jump, if it isn't possible to calculate this + //speed (the jump is not possible) then there's no jump reachability created + ret = AAS_HorizontalVelocityForJump( zvel, areastart, facecenter, &speed ); + if ( ret && speed < 270 ) { + //direction towards the face center + VectorSubtract( facecenter, areastart, dir ); + dir[2] = 0; + hordist = VectorNormalize( dir ); + //if (hordist < 1.6 * (facecenter[2] - areastart[2])) + { + //get command movement + VectorScale( dir, speed, cmdmove ); + VectorSet( velocity, 0, 0, zvel ); + /* + //get command movement + VectorScale(dir, speed, velocity); + velocity[2] = zvel; + VectorSet(cmdmove, 0, 0, 0); + */ + // + AAS_PredictClientMovement( &move, -1, areastart, PRESENCE_NORMAL, qtrue, + velocity, cmdmove, 30, 30, 0.1, + SE_ENTERWATER | SE_ENTERSLIME | + SE_ENTERLAVA | SE_HITGROUNDDAMAGE | + SE_TOUCHJUMPPAD | SE_HITGROUNDAREA, area2num, qfalse ); + //if prediction time wasn't enough to fully predict the movement + //don't enter slime or lava and don't fall from too high + if ( move.frames < 30 && + !( move.stopevent & ( SE_ENTERSLIME | SE_ENTERLAVA | SE_HITGROUNDDAMAGE ) ) + && ( move.stopevent & ( SE_HITGROUNDAREA | SE_TOUCHJUMPPAD ) ) ) { + //create a rocket or bfg jump reachability from area1 to area2 + lreach = AAS_AllocReachability(); + if ( !lreach ) { + return qfalse; + } + lreach->areanum = area2num; + lreach->facenum = 0; + lreach->edgenum = 0; + VectorCopy( areastart, lreach->start ); + VectorCopy( facecenter, lreach->end ); + if ( n ) { + lreach->traveltype = TRAVEL_BFGJUMP; + } else { lreach->traveltype = TRAVEL_ROCKETJUMP;} + lreach->traveltime = 300; + lreach->next = areareachability[area1num]; + areareachability[area1num] = lreach; + // + reach_rocketjump++; + return qtrue; + } //end if + } //end if + } //end if + } //end for + } //end for + // + return qfalse; +} //end of the function AAS_Reachability_WeaponJump +//=========================================================================== +// calculates additional walk off ledge reachabilities for the given area +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_Reachability_WalkOffLedge( int areanum ) { + int i, j, k, l, m, n; + int face1num, face2num, face3num, edge1num, edge2num, edge3num; + int otherareanum, gap, reachareanum, side; + aas_area_t *area, *area2; + aas_face_t *face1, *face2, *face3; + aas_edge_t *edge; + aas_plane_t *plane; + vec_t *v1, *v2; + vec3_t sharededgevec, mid, dir, testend; + aas_lreachability_t *lreach; + aas_trace_t trace; + + if ( !AAS_AreaGrounded( areanum ) || AAS_AreaSwim( areanum ) ) { + return; + } + // + area = &( *aasworld ).areas[areanum]; + // + for ( i = 0; i < area->numfaces; i++ ) + { + face1num = ( *aasworld ).faceindex[area->firstface + i]; + face1 = &( *aasworld ).faces[abs( face1num )]; + //face 1 must be a ground face + if ( !( face1->faceflags & FACE_GROUND ) ) { + continue; + } + //go through all the edges of this ground face + for ( k = 0; k < face1->numedges; k++ ) + { + edge1num = ( *aasworld ).edgeindex[face1->firstedge + k]; + //find another not ground face using this same edge + for ( j = 0; j < area->numfaces; j++ ) + { + face2num = ( *aasworld ).faceindex[area->firstface + j]; + face2 = &( *aasworld ).faces[abs( face2num )]; + //face 2 may not be a ground face + if ( face2->faceflags & FACE_GROUND ) { + continue; + } + //compare all the edges + for ( l = 0; l < face2->numedges; l++ ) + { + edge2num = ( *aasworld ).edgeindex[face2->firstedge + l]; + if ( abs( edge1num ) == abs( edge2num ) ) { + //get the area at the other side of the face + if ( face2->frontarea == areanum ) { + otherareanum = face2->backarea; + } else { otherareanum = face2->frontarea;} + // + area2 = &( *aasworld ).areas[otherareanum]; + //if the other area is grounded! + if ( ( *aasworld ).areasettings[otherareanum].areaflags & AREA_GROUNDED ) { + //check for a possible gap + gap = qfalse; + for ( n = 0; n < area2->numfaces; n++ ) + { + face3num = ( *aasworld ).faceindex[area2->firstface + n]; + //may not be the shared face of the two areas + if ( abs( face3num ) == abs( face2num ) ) { + continue; + } + // + face3 = &( *aasworld ).faces[abs( face3num )]; + //find an edge shared by all three faces + for ( m = 0; m < face3->numedges; m++ ) + { + edge3num = ( *aasworld ).edgeindex[face3->firstedge + m]; + //but the edge should be shared by all three faces + if ( abs( edge3num ) == abs( edge1num ) ) { + if ( !( face3->faceflags & FACE_SOLID ) ) { + gap = qtrue; + break; + } //end if + // + if ( face3->faceflags & FACE_GROUND ) { + gap = qfalse; + break; + } //end if + //FIXME: there are more situations to be handled + gap = qtrue; + break; + } //end if + } //end for + if ( m < face3->numedges ) { + break; + } + } //end for + if ( !gap ) { + break; + } + } //end if + //check for a walk off ledge reachability + edge = &( *aasworld ).edges[abs( edge1num )]; + side = edge1num < 0; + // + v1 = ( *aasworld ).vertexes[edge->v[side]]; + v2 = ( *aasworld ).vertexes[edge->v[!side]]; + // + plane = &( *aasworld ).planes[face1->planenum]; + //get the points really into the areas + VectorSubtract( v2, v1, sharededgevec ); + CrossProduct( plane->normal, sharededgevec, dir ); + VectorNormalize( dir ); + // + VectorAdd( v1, v2, mid ); + VectorScale( mid, 0.5, mid ); + VectorMA( mid, 8, dir, mid ); + // + VectorCopy( mid, testend ); + testend[2] -= 1000; + trace = AAS_TraceClientBBox( mid, testend, PRESENCE_CROUCH, -1 ); + // + if ( trace.startsolid ) { + //Log_Write("area %d: trace.startsolid\r\n", areanum); + break; + } //end if + reachareanum = AAS_PointAreaNum( trace.endpos ); + if ( reachareanum == areanum ) { + //Log_Write("area %d: same area\r\n", areanum); + break; + } //end if + if ( AAS_ReachabilityExists( areanum, reachareanum ) ) { + //Log_Write("area %d: reachability already exists\r\n", areanum); + break; + } //end if + if ( !AAS_AreaGrounded( reachareanum ) && !AAS_AreaSwim( reachareanum ) ) { + //Log_Write("area %d, reach area %d: not grounded and not swim\r\n", areanum, reachareanum); + break; + } //end if + // + if ( ( *aasworld ).areasettings[reachareanum].contents & AREACONTENTS_LAVA ) { //----(SA) modified since slime is no longer deadly +// if ((*aasworld).areasettings[reachareanum].contents & (AREACONTENTS_SLIME | AREACONTENTS_LAVA)) + //Log_Write("area %d, reach area %d: lava or slime\r\n", areanum, reachareanum); + break; + } //end if + lreach = AAS_AllocReachability(); + if ( !lreach ) { + break; + } + lreach->areanum = reachareanum; + lreach->facenum = 0; + lreach->edgenum = edge1num; + VectorCopy( mid, lreach->start ); + VectorCopy( trace.endpos, lreach->end ); + lreach->traveltype = TRAVEL_WALKOFFLEDGE; + lreach->traveltime = STARTWALKOFFLEDGE_TIME + fabs( mid[2] - trace.endpos[2] ) * 50 / aassettings.sv_gravity; + if ( !AAS_AreaSwim( reachareanum ) && !AAS_AreaJumpPad( reachareanum ) ) { + if ( AAS_FallDelta( mid[2] - trace.endpos[2] ) > FALLDELTA_5DAMAGE ) { + lreach->traveltime += FALLDAMAGE_5_TIME; + } //end if + else if ( AAS_FallDelta( mid[2] - trace.endpos[2] ) > FALLDELTA_10DAMAGE ) { + lreach->traveltime += FALLDAMAGE_10_TIME; + } //end if + } //end if + lreach->next = areareachability[areanum]; + areareachability[areanum] = lreach; + //we've got another walk off ledge reachability + reach_walkoffledge++; + } //end if + } //end for + } //end for + } //end for + } //end for +} //end of the function AAS_Reachability_WalkOffLedge +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_StoreReachability( void ) { + int i; + aas_areasettings_t *areasettings; + aas_lreachability_t *lreach; + aas_reachability_t *reach; + + if ( ( *aasworld ).reachability ) { + FreeMemory( ( *aasworld ).reachability ); + } + ( *aasworld ).reachability = (aas_reachability_t *) GetClearedMemory( ( numlreachabilities + 10 ) * sizeof( aas_reachability_t ) ); + ( *aasworld ).reachabilitysize = 1; + for ( i = 0; i < ( *aasworld ).numareas; i++ ) + { + areasettings = &( *aasworld ).areasettings[i]; + areasettings->firstreachablearea = ( *aasworld ).reachabilitysize; + areasettings->numreachableareas = 0; + for ( lreach = areareachability[i]; lreach; lreach = lreach->next ) + { + reach = &( *aasworld ).reachability[areasettings->firstreachablearea + + areasettings->numreachableareas]; + reach->areanum = lreach->areanum; + reach->facenum = lreach->facenum; + reach->edgenum = lreach->edgenum; + VectorCopy( lreach->start, reach->start ); + VectorCopy( lreach->end, reach->end ); + reach->traveltype = lreach->traveltype; + reach->traveltime = lreach->traveltime; + // RF, enforce the min reach time + if ( reach->traveltime < REACH_MIN_TIME ) { + reach->traveltime = REACH_MIN_TIME; + } + // + areasettings->numreachableareas++; + } //end for + ( *aasworld ).reachabilitysize += areasettings->numreachableareas; + } //end for +} //end of the function AAS_StoreReachability +//=========================================================================== +// +// TRAVEL_WALK 100% equal floor height + steps +// TRAVEL_CROUCH 100% +// TRAVEL_BARRIERJUMP 100% +// TRAVEL_JUMP 80% +// TRAVEL_LADDER 100% + fall down from ladder + jump up to ladder +// TRAVEL_WALKOFFLEDGE 90% walk off very steep walls? +// TRAVEL_SWIM 100% +// TRAVEL_WATERJUMP 100% +// TRAVEL_TELEPORT 100% +// TRAVEL_ELEVATOR 100% +// TRAVEL_GRAPPLEHOOK 100% +// TRAVEL_DOUBLEJUMP 0% +// TRAVEL_RAMPJUMP 0% +// TRAVEL_STRAFEJUMP 0% +// TRAVEL_ROCKETJUMP 100% (currently limited towards areas with items) +// TRAVEL_BFGJUMP 0% (currently disabled) +// TRAVEL_JUMPPAD 100% +// TRAVEL_FUNCBOB 100% +// +// Parameter: - +// Returns: true if NOT finished +// Changes Globals: - +//=========================================================================== +int AAS_ContinueInitReachability( float time ) { + int i, j, todo, start_time; + static float framereachability, reachability_delay; + static int lastpercentage; + + if ( !( *aasworld ).loaded ) { + return qfalse; + } + //if reachability is calculated for all areas + if ( ( *aasworld ).reachabilityareas >= ( *aasworld ).numareas + 2 ) { + return qfalse; + } + //if starting with area 1 (area 0 is a dummy) + if ( ( *aasworld ).reachabilityareas == 1 ) { + botimport.Print( PRT_MESSAGE, "calculating reachability...\n" ); + lastpercentage = 0; + framereachability = 2000; + reachability_delay = 1000; + } //end if + //number of areas to calculate reachability for this cycle + todo = ( *aasworld ).reachabilityareas + (int) framereachability; + start_time = Sys_MilliSeconds(); + //loop over the areas + for ( i = ( *aasworld ).reachabilityareas; i < ( *aasworld ).numareas && i < todo; i++ ) + { + ( *aasworld ).reachabilityareas++; + //only create jumppad reachabilities from jumppad areas + if ( ( *aasworld ).areasettings[i].contents & AREACONTENTS_JUMPPAD ) { + continue; + } //end if + //loop over the areas + for ( j = 1; j < ( *aasworld ).numareas; j++ ) + { + if ( i == j ) { + continue; + } + //never create reachabilities from teleporter or jumppad areas to regular areas + if ( ( *aasworld ).areasettings[i].contents & ( AREACONTENTS_TELEPORTER | AREACONTENTS_JUMPPAD ) ) { + if ( !( ( *aasworld ).areasettings[j].contents & ( AREACONTENTS_TELEPORTER | AREACONTENTS_JUMPPAD ) ) ) { + continue; + } //end if + } //end if + //if there already is a reachability link from area i to j + if ( AAS_ReachabilityExists( i, j ) ) { + continue; + } + //check for a swim reachability + if ( AAS_Reachability_Swim( i, j ) ) { + continue; + } + //check for a simple walk on equal floor height reachability + if ( AAS_Reachability_EqualFloorHeight( i, j ) ) { + continue; + } + //check for step, barrier, waterjump and walk off ledge reachabilities + if ( AAS_Reachability_Step_Barrier_WaterJump_WalkOffLedge( i, j ) ) { + continue; + } + //check for ladder reachabilities + if ( AAS_Reachability_Ladder( i, j ) ) { + continue; + } + //check for a jump reachability + if ( AAS_Reachability_Jump( i, j ) ) { + continue; + } + } //end for + //never create these reachabilities from teleporter or jumppad areas + if ( ( *aasworld ).areasettings[i].contents & ( AREACONTENTS_TELEPORTER | AREACONTENTS_JUMPPAD ) ) { + continue; + } //end if + //loop over the areas + for ( j = 1; j < ( *aasworld ).numareas; j++ ) + { + if ( i == j ) { + continue; + } + // + if ( AAS_ReachabilityExists( i, j ) ) { + continue; + } + //check for a grapple hook reachability +// Ridah, no grapple +// AAS_Reachability_Grapple(i, j); + //check for a weapon jump reachability +// Ridah, no weapon jumping +// AAS_Reachability_WeaponJump(i, j); + } //end for + //if the calculation took more time than the max reachability delay + if ( Sys_MilliSeconds() - start_time > (int) reachability_delay ) { + break; + } + // + if ( ( *aasworld ).reachabilityareas * 1000 / ( *aasworld ).numareas > lastpercentage ) { + break; + } + } //end for + // + if ( ( *aasworld ).reachabilityareas == ( *aasworld ).numareas ) { + botimport.Print( PRT_MESSAGE, "\r%6.1f%%", (float) 100.0 ); + botimport.Print( PRT_MESSAGE, "\nplease wait while storing reachability...\n" ); + ( *aasworld ).reachabilityareas++; + } //end if + //if this is the last step in the reachability calculations + else if ( ( *aasworld ).reachabilityareas == ( *aasworld ).numareas + 1 ) { + //create additional walk off ledge reachabilities for every area + for ( i = 1; i < ( *aasworld ).numareas; i++ ) + { + //only create jumppad reachabilities from jumppad areas + if ( ( *aasworld ).areasettings[i].contents & AREACONTENTS_JUMPPAD ) { + continue; + } //end if + AAS_Reachability_WalkOffLedge( i ); + } //end for + //create jump pad reachabilities + AAS_Reachability_JumpPad(); + //create teleporter reachabilities + AAS_Reachability_Teleport(); + //create elevator (func_plat) reachabilities + AAS_Reachability_Elevator(); + //create func_bobbing reachabilities + AAS_Reachability_FuncBobbing(); + // +//#ifdef DEBUG + botimport.Print( PRT_MESSAGE, "%6d reach swim\n", reach_swim ); + botimport.Print( PRT_MESSAGE, "%6d reach equal floor\n", reach_equalfloor ); + botimport.Print( PRT_MESSAGE, "%6d reach step\n", reach_step ); + botimport.Print( PRT_MESSAGE, "%6d reach barrier\n", reach_barrier ); + botimport.Print( PRT_MESSAGE, "%6d reach waterjump\n", reach_waterjump ); + botimport.Print( PRT_MESSAGE, "%6d reach walkoffledge\n", reach_walkoffledge ); + botimport.Print( PRT_MESSAGE, "%6d reach jump\n", reach_jump ); + botimport.Print( PRT_MESSAGE, "%6d reach ladder\n", reach_ladder ); + botimport.Print( PRT_MESSAGE, "%6d reach walk\n", reach_walk ); + botimport.Print( PRT_MESSAGE, "%6d reach teleport\n", reach_teleport ); + botimport.Print( PRT_MESSAGE, "%6d reach funcbob\n", reach_funcbob ); + botimport.Print( PRT_MESSAGE, "%6d reach elevator\n", reach_elevator ); + botimport.Print( PRT_MESSAGE, "%6d reach grapple\n", reach_grapple ); + botimport.Print( PRT_MESSAGE, "%6d reach rocketjump\n", reach_rocketjump ); + botimport.Print( PRT_MESSAGE, "%6d reach jumppad\n", reach_jumppad ); +//#endif + //*/ + //store all the reachabilities + AAS_StoreReachability(); + //free the reachability link heap + AAS_ShutDownReachabilityHeap(); + // + FreeMemory( areareachability ); + // + ( *aasworld ).reachabilityareas++; + // + botimport.Print( PRT_MESSAGE, "calculating clusters...\n" ); + } //end if + else + { + lastpercentage = ( *aasworld ).reachabilityareas * 1000 / ( *aasworld ).numareas; + botimport.Print( PRT_MESSAGE, "\r%6.1f%%", (float) lastpercentage / 10 ); + } //end else + //not yet finished + return qtrue; +} //end of the function AAS_ContinueInitReachability +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_InitReachability( void ) { + if ( !( *aasworld ).loaded ) { + return; + } + + if ( ( *aasworld ).reachabilitysize ) { +#ifndef BSPC + if ( !( (int)LibVarGetValue( "forcereachability" ) ) ) { + ( *aasworld ).reachabilityareas = ( *aasworld ).numareas + 2; + return; + } //end if +#else + ( *aasworld ).reachabilityareas = ( *aasworld ).numareas + 2; + return; +#endif //BSPC + } //end if + ( *aasworld ).savefile = qtrue; + //start with area 1 because area zero is a dummy + ( *aasworld ).reachabilityareas = 1; + //setup the heap with reachability links + AAS_SetupReachabilityHeap(); + //allocate area reachability link array + areareachability = (aas_lreachability_t **) GetClearedMemory( + ( *aasworld ).numareas * sizeof( aas_lreachability_t * ) ); + // + AAS_SetWeaponJumpAreaFlags(); +} //end of the function AAS_InitReachable diff --git a/src/botlib/be_aas_reach.h b/src/botlib/be_aas_reach.h new file mode 100644 index 0000000..8c5f615 --- /dev/null +++ b/src/botlib/be_aas_reach.h @@ -0,0 +1,74 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_reach.h + * + * desc: AAS + * + * + *****************************************************************************/ + +#ifdef AASINTERN +//initialize calculating the reachabilities +void AAS_InitReachability( void ); +//continue calculating the reachabilities +int AAS_ContinueInitReachability( float time ); +// +int AAS_BestReachableLinkArea( aas_link_t *areas ); +#endif //AASINTERN + +//returns true if the are has reachabilities to other areas +int AAS_AreaReachability( int areanum ); +//returns the best reachable area and goal origin for a bounding box at the given origin +int AAS_BestReachableArea( vec3_t origin, vec3_t mins, vec3_t maxs, vec3_t goalorigin ); +//returns the next reachability using the given model +int AAS_NextModelReachability( int num, int modelnum ); +//returns the total area of the ground faces of the given area +float AAS_AreaGroundFaceArea( int areanum ); +//returns true if the area is crouch only +int AAS_AreaCrouch( int areanum ); +//returns true if a player can swim in this area +int AAS_AreaSwim( int areanum ); +//returns true if the area is filled with a liquid +int AAS_AreaLiquid( int areanum ); +//returns true if the area contains lava +int AAS_AreaLava( int areanum ); +//returns true if the area contains slime +int AAS_AreaSlime( int areanum ); +//returns true if the area has one or more ground faces +int AAS_AreaGrounded( int areanum ); +//returns true if the area has one or more ladder faces +int AAS_AreaLadder( int areanum ); +//returns true if the area is a jump pad +int AAS_AreaJumpPad( int areanum ); +//returns true if the area is donotenter +int AAS_AreaDoNotEnter( int areanum ); +//returns true if the area is donotenterlarge +int AAS_AreaDoNotEnterLarge( int areanum ); diff --git a/src/botlib/be_aas_route.c b/src/botlib/be_aas_route.c new file mode 100644 index 0000000..7cec1f3 --- /dev/null +++ b/src/botlib/be_aas_route.c @@ -0,0 +1,2640 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_route.c + * + * desc: AAS + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "l_utils.h" +#include "l_memory.h" +#include "l_log.h" +#include "l_crc.h" +#include "l_libvar.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "aasfile.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "be_aas_funcs.h" +#include "be_interface.h" +#include "be_aas_def.h" + +#define ROUTING_DEBUG + +//travel time in hundreths of a second = distance * 100 / speed +#define DISTANCEFACTOR_CROUCH 1.3 //crouch speed = 100 +#define DISTANCEFACTOR_SWIM 1 //should be 0.66, swim speed = 150 +#define DISTANCEFACTOR_WALK 0.33 //walk speed = 300 + +// Ridah, scale traveltimes with ground steepness of area +#define GROUNDSTEEPNESS_TIMESCALE 20 // this is the maximum scale, 1 being the usual for a flat ground + +//cache refresh time +#define CACHE_REFRESHTIME 15.0 //15 seconds refresh time + +//maximum number of routing updates each frame +#define MAX_FRAMEROUTINGUPDATES 100 + +extern aas_t aasworlds[MAX_AAS_WORLDS]; + + +/* + + area routing cache: + stores the distances within one cluster to a specific goal area + this goal area is in this same cluster and could be a cluster portal + for every cluster there's a list with routing cache for every area + in that cluster (including the portals of that cluster) + area cache stores (*aasworld).clusters[?].numreachabilityareas travel times + + portal routing cache: + stores the distances of all portals to a specific goal area + this goal area could be in any cluster and could also be a cluster portal + for every area ((*aasworld).numareas) the portal cache stores + (*aasworld).numportals travel times + +*/ + +#ifdef ROUTING_DEBUG +int numareacacheupdates; +int numportalcacheupdates; +#endif //ROUTING_DEBUG + +int routingcachesize; +int max_routingcachesize; + +// Ridah, routing memory calls go here, so we can change between Hunk/Zone easily +void *AAS_RoutingGetMemory( int size ) { + return GetClearedMemory( size ); +} + +void AAS_RoutingFreeMemory( void *ptr ) { + FreeMemory( ptr ); +} +// done. + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef ROUTING_DEBUG +void AAS_RoutingInfo( void ) { + botimport.Print( PRT_MESSAGE, "%d area cache updates\n", numareacacheupdates ); + botimport.Print( PRT_MESSAGE, "%d portal cache updates\n", numportalcacheupdates ); + botimport.Print( PRT_MESSAGE, "%d bytes routing cache\n", routingcachesize ); +} //end of the function AAS_RoutingInfo +#endif //ROUTING_DEBUG +//=========================================================================== +// returns the number of the area in the cluster +// assumes the given area is in the given cluster or a portal of the cluster +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +__inline int AAS_ClusterAreaNum( int cluster, int areanum ) { + int side, areacluster; + + areacluster = ( *aasworld ).areasettings[areanum].cluster; + if ( areacluster > 0 ) { + return ( *aasworld ).areasettings[areanum].clusterareanum; + } else + { +/*#ifdef ROUTING_DEBUG + if ((*aasworld).portals[-areacluster].frontcluster != cluster && + (*aasworld).portals[-areacluster].backcluster != cluster) + { + botimport.Print(PRT_ERROR, "portal %d: does not belong to cluster %d\n" + , -areacluster, cluster); + } //end if +#endif //ROUTING_DEBUG*/ + side = ( *aasworld ).portals[-areacluster].frontcluster != cluster; + return ( *aasworld ).portals[-areacluster].clusterareanum[side]; + } //end else +} //end of the function AAS_ClusterAreaNum +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_InitTravelFlagFromType( void ) { + int i; + + for ( i = 0; i < MAX_TRAVELTYPES; i++ ) + { + ( *aasworld ).travelflagfortype[i] = TFL_INVALID; + } //end for + ( *aasworld ).travelflagfortype[TRAVEL_INVALID] = TFL_INVALID; + ( *aasworld ).travelflagfortype[TRAVEL_WALK] = TFL_WALK; + ( *aasworld ).travelflagfortype[TRAVEL_CROUCH] = TFL_CROUCH; + ( *aasworld ).travelflagfortype[TRAVEL_BARRIERJUMP] = TFL_BARRIERJUMP; + ( *aasworld ).travelflagfortype[TRAVEL_JUMP] = TFL_JUMP; + ( *aasworld ).travelflagfortype[TRAVEL_LADDER] = TFL_LADDER; + ( *aasworld ).travelflagfortype[TRAVEL_WALKOFFLEDGE] = TFL_WALKOFFLEDGE; + ( *aasworld ).travelflagfortype[TRAVEL_SWIM] = TFL_SWIM; + ( *aasworld ).travelflagfortype[TRAVEL_WATERJUMP] = TFL_WATERJUMP; + ( *aasworld ).travelflagfortype[TRAVEL_TELEPORT] = TFL_TELEPORT; + ( *aasworld ).travelflagfortype[TRAVEL_ELEVATOR] = TFL_ELEVATOR; + ( *aasworld ).travelflagfortype[TRAVEL_ROCKETJUMP] = TFL_ROCKETJUMP; + ( *aasworld ).travelflagfortype[TRAVEL_BFGJUMP] = TFL_BFGJUMP; + ( *aasworld ).travelflagfortype[TRAVEL_GRAPPLEHOOK] = TFL_GRAPPLEHOOK; + ( *aasworld ).travelflagfortype[TRAVEL_DOUBLEJUMP] = TFL_DOUBLEJUMP; + ( *aasworld ).travelflagfortype[TRAVEL_RAMPJUMP] = TFL_RAMPJUMP; + ( *aasworld ).travelflagfortype[TRAVEL_STRAFEJUMP] = TFL_STRAFEJUMP; + ( *aasworld ).travelflagfortype[TRAVEL_JUMPPAD] = TFL_JUMPPAD; + ( *aasworld ).travelflagfortype[TRAVEL_FUNCBOB] = TFL_FUNCBOB; +} //end of the function AAS_InitTravelFlagFromType +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_TravelFlagForType( int traveltype ) { + if ( traveltype < 0 || traveltype >= MAX_TRAVELTYPES ) { + return TFL_INVALID; + } + return ( *aasworld ).travelflagfortype[traveltype]; +} //end of the function AAS_TravelFlagForType +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +__inline float AAS_RoutingTime( void ) { + return AAS_Time(); +} //end of the function AAS_RoutingTime +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FreeRoutingCache( aas_routingcache_t *cache ) { + routingcachesize -= cache->size; + AAS_RoutingFreeMemory( cache ); +} //end of the function AAS_FreeRoutingCache +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_RemoveRoutingCacheInCluster( int clusternum ) { + int i; + aas_routingcache_t *cache, *nextcache; + aas_cluster_t *cluster; + + if ( !( *aasworld ).clusterareacache ) { + return; + } + cluster = &( *aasworld ).clusters[clusternum]; + for ( i = 0; i < cluster->numareas; i++ ) + { + for ( cache = ( *aasworld ).clusterareacache[clusternum][i]; cache; cache = nextcache ) + { + nextcache = cache->next; + AAS_FreeRoutingCache( cache ); + } //end for + ( *aasworld ).clusterareacache[clusternum][i] = NULL; + } //end for +} //end of the function AAS_RemoveRoutingCacheInCluster +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_RemoveRoutingCacheUsingArea( int areanum ) { + int i, clusternum; + aas_routingcache_t *cache, *nextcache; + + clusternum = ( *aasworld ).areasettings[areanum].cluster; + if ( clusternum > 0 ) { + //remove all the cache in the cluster the area is in + AAS_RemoveRoutingCacheInCluster( clusternum ); + } //end if + else + { + // if this is a portal remove all cache in both the front and back cluster + AAS_RemoveRoutingCacheInCluster( ( *aasworld ).portals[-clusternum].frontcluster ); + AAS_RemoveRoutingCacheInCluster( ( *aasworld ).portals[-clusternum].backcluster ); + } //end else + // remove all portal cache + if ( ( *aasworld ).portalcache ) { + for ( i = 0; i < ( *aasworld ).numareas; i++ ) + { + //refresh portal cache + for ( cache = ( *aasworld ).portalcache[i]; cache; cache = nextcache ) + { + nextcache = cache->next; + AAS_FreeRoutingCache( cache ); + } //end for + ( *aasworld ).portalcache[i] = NULL; + } //end for + } +} //end of the function AAS_RemoveRoutingCacheUsingArea +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_EnableRoutingArea( int areanum, int enable ) { + int flags; + + if ( areanum <= 0 || areanum >= ( *aasworld ).numareas ) { + if ( bot_developer ) { + botimport.Print( PRT_ERROR, "AAS_EnableRoutingArea: areanum %d out of range\n", areanum ); + } //end if + return 0; + } //end if + flags = ( *aasworld ).areasettings[areanum].areaflags & AREA_DISABLED; + if ( enable < 0 ) { + return !flags; + } + + if ( enable ) { + ( *aasworld ).areasettings[areanum].areaflags &= ~AREA_DISABLED; + } else { + ( *aasworld ).areasettings[areanum].areaflags |= AREA_DISABLED; + } + // if the status of the area changed + if ( ( flags & AREA_DISABLED ) != ( ( *aasworld ).areasettings[areanum].areaflags & AREA_DISABLED ) ) { + //remove all routing cache involving this area + AAS_RemoveRoutingCacheUsingArea( areanum ); + } //end if + return !flags; +} //end of the function AAS_EnableRoutingArea +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_CreateReversedReachability( void ) { + int i, n; + aas_reversedlink_t *revlink; + aas_reachability_t *reach; + aas_areasettings_t *settings; + char *ptr; +#ifdef DEBUG + int starttime; + + starttime = Sys_MilliSeconds(); +#endif + //free reversed links that have already been created + if ( ( *aasworld ).reversedreachability ) { + AAS_RoutingFreeMemory( ( *aasworld ).reversedreachability ); + } + //allocate memory for the reversed reachability links + ptr = (char *) AAS_RoutingGetMemory( ( *aasworld ).numareas * sizeof( aas_reversedreachability_t ) + + ( *aasworld ).reachabilitysize * sizeof( aas_reversedlink_t ) ); + // + ( *aasworld ).reversedreachability = (aas_reversedreachability_t *) ptr; + //pointer to the memory for the reversed links + ptr += ( *aasworld ).numareas * sizeof( aas_reversedreachability_t ); + //check all other areas for reachability links to the area + for ( i = 1; i < ( *aasworld ).numareas; i++ ) + { + //settings of the area + settings = &( *aasworld ).areasettings[i]; + //check the reachability links + for ( n = 0; n < settings->numreachableareas; n++ ) + { + //reachability link + reach = &( *aasworld ).reachability[settings->firstreachablearea + n]; + // + revlink = (aas_reversedlink_t *) ptr; + ptr += sizeof( aas_reversedlink_t ); + // + revlink->areanum = i; + revlink->linknum = settings->firstreachablearea + n; + revlink->next = ( *aasworld ).reversedreachability[reach->areanum].first; + ( *aasworld ).reversedreachability[reach->areanum].first = revlink; + ( *aasworld ).reversedreachability[reach->areanum].numlinks++; + } //end for + } //end for +#ifdef DEBUG + botimport.Print( PRT_MESSAGE, "reversed reachability %d msec\n", Sys_MilliSeconds() - starttime ); +#endif //DEBUG +} //end of the function AAS_CreateReversedReachability +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float AAS_AreaGroundSteepnessScale( int areanum ) { + return ( 1.0 + ( *aasworld ).areasettings[areanum].groundsteepness * (float)( GROUNDSTEEPNESS_TIMESCALE - 1 ) ); +} +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +unsigned short int AAS_AreaTravelTime( int areanum, vec3_t start, vec3_t end ) { + int intdist; + float dist; + vec3_t dir; + + VectorSubtract( start, end, dir ); + dist = VectorLength( dir ); + // Ridah, factor in the groundsteepness now + dist *= AAS_AreaGroundSteepnessScale( areanum ); + + //if crouch only area + if ( AAS_AreaCrouch( areanum ) ) { + dist *= DISTANCEFACTOR_CROUCH; + } + //if swim area + else if ( AAS_AreaSwim( areanum ) ) { + dist *= DISTANCEFACTOR_SWIM; + } + //normal walk area + else {dist *= DISTANCEFACTOR_WALK;} + // + intdist = (int) ceil( dist ); + //make sure the distance isn't zero + if ( intdist <= 0 ) { + intdist = 1; + } + return intdist; +} //end of the function AAS_AreaTravelTime +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_CalculateAreaTravelTimes( void ) { + int i, l, n, size; + char *ptr; + vec3_t end; + aas_reversedreachability_t *revreach; + aas_reversedlink_t *revlink; + aas_reachability_t *reach; + aas_areasettings_t *settings; + int starttime; + + starttime = Sys_MilliSeconds(); + //if there are still area travel times, free the memory + if ( ( *aasworld ).areatraveltimes ) { + AAS_RoutingFreeMemory( ( *aasworld ).areatraveltimes ); + } + //get the total size of all the area travel times + size = ( *aasworld ).numareas * sizeof( unsigned short ** ); + for ( i = 0; i < ( *aasworld ).numareas; i++ ) + { + revreach = &( *aasworld ).reversedreachability[i]; + //settings of the area + settings = &( *aasworld ).areasettings[i]; + // + size += settings->numreachableareas * sizeof( unsigned short * ); + // + size += settings->numreachableareas * revreach->numlinks * sizeof( unsigned short ); + } //end for + //allocate memory for the area travel times + ptr = (char *) AAS_RoutingGetMemory( size ); + ( *aasworld ).areatraveltimes = (unsigned short ***) ptr; + ptr += ( *aasworld ).numareas * sizeof( unsigned short ** ); + //calcluate the travel times for all the areas + for ( i = 0; i < ( *aasworld ).numareas; i++ ) + { + //reversed reachabilities of this area + revreach = &( *aasworld ).reversedreachability[i]; + //settings of the area + settings = &( *aasworld ).areasettings[i]; + // + ( *aasworld ).areatraveltimes[i] = (unsigned short **) ptr; + ptr += settings->numreachableareas * sizeof( unsigned short * ); + // + reach = &( *aasworld ).reachability[settings->firstreachablearea]; + for ( l = 0; l < settings->numreachableareas; l++, reach++ ) + { + ( *aasworld ).areatraveltimes[i][l] = (unsigned short *) ptr; + ptr += revreach->numlinks * sizeof( unsigned short ); + //reachability link + // + for ( n = 0, revlink = revreach->first; revlink; revlink = revlink->next, n++ ) + { + VectorCopy( ( *aasworld ).reachability[revlink->linknum].end, end ); + // + ( *aasworld ).areatraveltimes[i][l][n] = AAS_AreaTravelTime( i, end, reach->start ); + } //end for + } //end for + } //end for +#ifdef DEBUG + botimport.Print( PRT_MESSAGE, "area travel times %d msec\n", Sys_MilliSeconds() - starttime ); +#endif //DEBUG +} //end of the function AAS_CalculateAreaTravelTimes +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_PortalMaxTravelTime( int portalnum ) { + int l, n, t, maxt; + aas_portal_t *portal; + aas_reversedreachability_t *revreach; + aas_reversedlink_t *revlink; + aas_areasettings_t *settings; + + portal = &( *aasworld ).portals[portalnum]; + //reversed reachabilities of this portal area + revreach = &( *aasworld ).reversedreachability[portal->areanum]; + //settings of the portal area + settings = &( *aasworld ).areasettings[portal->areanum]; + // + maxt = 0; + for ( l = 0; l < settings->numreachableareas; l++ ) + { + for ( n = 0, revlink = revreach->first; revlink; revlink = revlink->next, n++ ) + { + t = ( *aasworld ).areatraveltimes[portal->areanum][l][n]; + if ( t > maxt ) { + maxt = t; + } //end if + } //end for + } //end for + return maxt; +} //end of the function AAS_PortalMaxTravelTime +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_InitPortalMaxTravelTimes( void ) { + int i; + + if ( ( *aasworld ).portalmaxtraveltimes ) { + AAS_RoutingFreeMemory( ( *aasworld ).portalmaxtraveltimes ); + } + + ( *aasworld ).portalmaxtraveltimes = (int *) AAS_RoutingGetMemory( ( *aasworld ).numportals * sizeof( int ) ); + + for ( i = 0; i < ( *aasworld ).numportals; i++ ) + { + ( *aasworld ).portalmaxtraveltimes[i] = AAS_PortalMaxTravelTime( i ); + //botimport.Print(PRT_MESSAGE, "portal %d max tt = %d\n", i, (*aasworld).portalmaxtraveltimes[i]); + } //end for +} //end of the function AAS_InitPortalMaxTravelTimes +/* +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_UnlinkCache(aas_routingcache_t *cache) +{ + if (cache->time_next) cache->time_next->time_prev = cache->time_prev; + else newestcache = cache->time_prev; + if (cache->time_prev) cache->time_prev->time_next = cache->time_next; + else oldestcache = cache->time_next; + cache->time_next = NULL; + cache->time_prev = NULL; +} //end of the function AAS_UnlinkCache +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_LinkCache(aas_routingcache_t *cache) +{ + if (newestcache) + { + newestcache->time_next = cache; + cache->time_prev = cache; + } //end if + else + { + oldestcache = cache; + cache->time_prev = NULL; + } //end else + cache->time_next = NULL; + newestcache = cache; +} //end of the function AAS_LinkCache*/ +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_FreeOldestCache( void ) { + int i, j, bestcluster, bestarea, freed; + float besttime; + aas_routingcache_t *cache, *bestcache; + + freed = qfalse; + besttime = 999999999; + bestcache = NULL; + bestcluster = 0; + bestarea = 0; + //refresh cluster cache + for ( i = 0; i < ( *aasworld ).numclusters; i++ ) + { + for ( j = 0; j < ( *aasworld ).clusters[i].numareas; j++ ) + { + for ( cache = ( *aasworld ).clusterareacache[i][j]; cache; cache = cache->next ) + { + //never remove cache leading towards a portal + if ( ( *aasworld ).areasettings[cache->areanum].cluster < 0 ) { + continue; + } + //if this cache is older than the cache we found so far + if ( cache->time < besttime ) { + bestcache = cache; + bestcluster = i; + bestarea = j; + besttime = cache->time; + } //end if + } //end for + } //end for + } //end for + if ( bestcache ) { + cache = bestcache; + if ( cache->prev ) { + cache->prev->next = cache->next; + } else { ( *aasworld ).clusterareacache[bestcluster][bestarea] = cache->next;} + if ( cache->next ) { + cache->next->prev = cache->prev; + } + AAS_FreeRoutingCache( cache ); + freed = qtrue; + } //end if + besttime = 999999999; + bestcache = NULL; + bestarea = 0; + for ( i = 0; i < ( *aasworld ).numareas; i++ ) + { + //refresh portal cache + for ( cache = ( *aasworld ).portalcache[i]; cache; cache = cache->next ) + { + if ( cache->time < besttime ) { + bestcache = cache; + bestarea = i; + besttime = cache->time; + } //end if + } //end for + } //end for + if ( bestcache ) { + cache = bestcache; + if ( cache->prev ) { + cache->prev->next = cache->next; + } else { ( *aasworld ).portalcache[bestarea] = cache->next;} + if ( cache->next ) { + cache->next->prev = cache->prev; + } + AAS_FreeRoutingCache( cache ); + freed = qtrue; + } //end if + return freed; +} //end of the function AAS_FreeOldestCache +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +aas_routingcache_t *AAS_AllocRoutingCache( int numtraveltimes ) { + aas_routingcache_t *cache; + int size; + + // + size = sizeof( aas_routingcache_t ) + + numtraveltimes * sizeof( unsigned short int ) + + numtraveltimes * sizeof( unsigned char ); + // + routingcachesize += size; + // + cache = (aas_routingcache_t *) AAS_RoutingGetMemory( size ); + cache->reachabilities = (unsigned char *) cache + sizeof( aas_routingcache_t ) + + numtraveltimes * sizeof( unsigned short int ); + cache->size = size; + return cache; +} //end of the function AAS_AllocRoutingCache +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FreeAllClusterAreaCache( void ) { + int i, j; + aas_routingcache_t *cache, *nextcache; + aas_cluster_t *cluster; + + //free all cluster cache if existing + if ( !( *aasworld ).clusterareacache ) { + return; + } + //free caches + for ( i = 0; i < ( *aasworld ).numclusters; i++ ) + { + cluster = &( *aasworld ).clusters[i]; + for ( j = 0; j < cluster->numareas; j++ ) + { + for ( cache = ( *aasworld ).clusterareacache[i][j]; cache; cache = nextcache ) + { + nextcache = cache->next; + AAS_FreeRoutingCache( cache ); + } //end for + ( *aasworld ).clusterareacache[i][j] = NULL; + } //end for + } //end for + //free the cluster cache array + AAS_RoutingFreeMemory( ( *aasworld ).clusterareacache ); + ( *aasworld ).clusterareacache = NULL; +} //end of the function AAS_FreeAllClusterAreaCache +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_InitClusterAreaCache( void ) { + int i, size; + char *ptr; + + // + for ( size = 0, i = 0; i < ( *aasworld ).numclusters; i++ ) + { + size += ( *aasworld ).clusters[i].numareas; + } //end for + //two dimensional array with pointers for every cluster to routing cache + //for every area in that cluster + ptr = (char *) AAS_RoutingGetMemory( + ( *aasworld ).numclusters * sizeof( aas_routingcache_t * * ) + + size * sizeof( aas_routingcache_t * ) ); + ( *aasworld ).clusterareacache = (aas_routingcache_t ***) ptr; + ptr += ( *aasworld ).numclusters * sizeof( aas_routingcache_t * * ); + for ( i = 0; i < ( *aasworld ).numclusters; i++ ) + { + ( *aasworld ).clusterareacache[i] = (aas_routingcache_t **) ptr; + ptr += ( *aasworld ).clusters[i].numareas * sizeof( aas_routingcache_t * ); + } //end for +} //end of the function AAS_InitClusterAreaCache +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FreeAllPortalCache( void ) { + int i; + aas_routingcache_t *cache, *nextcache; + + //free all portal cache if existing + if ( !( *aasworld ).portalcache ) { + return; + } + //free portal caches + for ( i = 0; i < ( *aasworld ).numareas; i++ ) + { + for ( cache = ( *aasworld ).portalcache[i]; cache; cache = nextcache ) + { + nextcache = cache->next; + AAS_FreeRoutingCache( cache ); + } //end for + ( *aasworld ).portalcache[i] = NULL; + } //end for + AAS_RoutingFreeMemory( ( *aasworld ).portalcache ); + ( *aasworld ).portalcache = NULL; +} //end of the function AAS_FreeAllPortalCache +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_InitPortalCache( void ) { + // + ( *aasworld ).portalcache = (aas_routingcache_t **) AAS_RoutingGetMemory( + ( *aasworld ).numareas * sizeof( aas_routingcache_t * ) ); +} //end of the function AAS_InitPortalCache +// +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FreeAreaVisibility( void ) { + int i; + + if ( ( *aasworld ).areavisibility ) { + for ( i = 0; i < ( *aasworld ).numareas; i++ ) + { + if ( ( *aasworld ).areavisibility[i] ) { + FreeMemory( ( *aasworld ).areavisibility[i] ); + } + } + } + if ( ( *aasworld ).areavisibility ) { + FreeMemory( ( *aasworld ).areavisibility ); + } + ( *aasworld ).areavisibility = NULL; + if ( ( *aasworld ).decompressedvis ) { + FreeMemory( ( *aasworld ).decompressedvis ); + } + ( *aasworld ).decompressedvis = NULL; +} +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_InitRoutingUpdate( void ) { +// int i, maxreachabilityareas; + + //free routing update fields if already existing + if ( ( *aasworld ).areaupdate ) { + AAS_RoutingFreeMemory( ( *aasworld ).areaupdate ); + } + // +// Ridah, had to change it to numareas for hidepos checking +/* + maxreachabilityareas = 0; + for (i = 0; i < (*aasworld).numclusters; i++) + { + if ((*aasworld).clusters[i].numreachabilityareas > maxreachabilityareas) + { + maxreachabilityareas = (*aasworld).clusters[i].numreachabilityareas; + } //end if + } //end for + //allocate memory for the routing update fields + (*aasworld).areaupdate = (aas_routingupdate_t *) AAS_RoutingGetMemory( + maxreachabilityareas * sizeof(aas_routingupdate_t)); +*/ + ( *aasworld ).areaupdate = (aas_routingupdate_t *) AAS_RoutingGetMemory( + ( *aasworld ).numareas * sizeof( aas_routingupdate_t ) ); + // + if ( ( *aasworld ).portalupdate ) { + AAS_RoutingFreeMemory( ( *aasworld ).portalupdate ); + } + //allocate memory for the portal update fields + ( *aasworld ).portalupdate = (aas_routingupdate_t *) AAS_RoutingGetMemory( + ( ( *aasworld ).numportals + 1 ) * sizeof( aas_routingupdate_t ) ); +} //end of the function AAS_InitRoutingUpdate +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== + +void AAS_CreateAllRoutingCache( void ) { + int i, j, k, t, tfl, numroutingareas; + aas_areasettings_t *areasettings; + aas_reachability_t *reach; + + numroutingareas = 0; + tfl = TFL_DEFAULT & ~( TFL_JUMPPAD | TFL_ROCKETJUMP | TFL_BFGJUMP | TFL_GRAPPLEHOOK | TFL_DOUBLEJUMP | TFL_RAMPJUMP | TFL_STRAFEJUMP | TFL_LAVA ); //----(SA) modified since slime is no longer deadly +// tfl = TFL_DEFAULT & ~(TFL_JUMPPAD|TFL_ROCKETJUMP|TFL_BFGJUMP|TFL_GRAPPLEHOOK|TFL_DOUBLEJUMP|TFL_RAMPJUMP|TFL_STRAFEJUMP|TFL_SLIME|TFL_LAVA); + botimport.Print( PRT_MESSAGE, "AAS_CreateAllRoutingCache\n" ); + // + for ( i = 1; i < ( *aasworld ).numareas; i++ ) + { + if ( !AAS_AreaReachability( i ) ) { + continue; + } + areasettings = &( *aasworld ).areasettings[i]; + for ( k = 0; k < areasettings->numreachableareas; k++ ) + { + reach = &( *aasworld ).reachability[areasettings->firstreachablearea + k]; + if ( ( *aasworld ).travelflagfortype[reach->traveltype] & tfl ) { + break; + } + } + if ( k >= areasettings->numreachableareas ) { + continue; + } + ( *aasworld ).areasettings[i].areaflags |= AREA_USEFORROUTING; + numroutingareas++; + } + for ( i = 1; i < ( *aasworld ).numareas; i++ ) + { + if ( !( ( *aasworld ).areasettings[i].areaflags & AREA_USEFORROUTING ) ) { + continue; + } + for ( j = 1; j < ( *aasworld ).numareas; j++ ) + { + if ( i == j ) { + continue; + } + if ( !( ( *aasworld ).areasettings[j].areaflags & AREA_USEFORROUTING ) ) { + continue; + } + t = AAS_AreaTravelTimeToGoalArea( j, ( *aasworld ).areawaypoints[j], i, tfl ); + ( *aasworld ).frameroutingupdates = 0; + //if (t) break; + //Log_Write("traveltime from %d to %d is %d", i, j, t); + } //end for + } //end for +} //end of the function AAS_CreateAllRoutingCache +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +unsigned short CRC_ProcessString( unsigned char *data, int length ); + +//the route cache header +//this header is followed by numportalcache + numareacache aas_routingcache_t +//structures that store routing cache +typedef struct routecacheheader_s +{ + int ident; + int version; + int numareas; + int numclusters; + int areacrc; + int clustercrc; + int reachcrc; + int numportalcache; + int numareacache; +} routecacheheader_t; + +#define RCID ( ( 'C' << 24 ) + ( 'R' << 16 ) + ( 'E' << 8 ) + 'M' ) +#define RCVERSION 15 + +void AAS_DecompressVis( byte *in, int numareas, byte *decompressed ); +int AAS_CompressVis( byte *vis, int numareas, byte *dest ); + +void AAS_WriteRouteCache( void ) { + int i, j, numportalcache, numareacache, size; + aas_routingcache_t *cache; + aas_cluster_t *cluster; + fileHandle_t fp; + char filename[MAX_QPATH]; + routecacheheader_t routecacheheader; + byte *buf; + + buf = (byte *) GetClearedMemory( ( *aasworld ).numareas * 2 * sizeof( byte ) ); // in case it ends up bigger than the decompressedvis, which is rare but possible + + numportalcache = 0; + for ( i = 0; i < ( *aasworld ).numareas; i++ ) + { + for ( cache = ( *aasworld ).portalcache[i]; cache; cache = cache->next ) + { + numportalcache++; + } //end for + } //end for + numareacache = 0; + for ( i = 0; i < ( *aasworld ).numclusters; i++ ) + { + cluster = &( *aasworld ).clusters[i]; + for ( j = 0; j < cluster->numareas; j++ ) + { + for ( cache = ( *aasworld ).clusterareacache[i][j]; cache; cache = cache->next ) + { + numareacache++; + } //end for + } //end for + } //end for + // open the file for writing + Com_sprintf( filename, MAX_QPATH, "maps/%s.rcd", ( *aasworld ).mapname ); + botimport.FS_FOpenFile( filename, &fp, FS_WRITE ); + if ( !fp ) { + AAS_Error( "Unable to open file: %s\n", filename ); + return; + } //end if + //create the header + routecacheheader.ident = RCID; + routecacheheader.version = RCVERSION; + routecacheheader.numareas = ( *aasworld ).numareas; + routecacheheader.numclusters = ( *aasworld ).numclusters; + routecacheheader.areacrc = CRC_ProcessString( (unsigned char *)( *aasworld ).areas, sizeof( aas_area_t ) * ( *aasworld ).numareas ); + routecacheheader.clustercrc = CRC_ProcessString( (unsigned char *)( *aasworld ).clusters, sizeof( aas_cluster_t ) * ( *aasworld ).numclusters ); + routecacheheader.reachcrc = CRC_ProcessString( (unsigned char *)( *aasworld ).reachability, sizeof( aas_reachability_t ) * ( *aasworld ).reachabilitysize ); + routecacheheader.numportalcache = numportalcache; + routecacheheader.numareacache = numareacache; + //write the header + botimport.FS_Write( &routecacheheader, sizeof( routecacheheader_t ), fp ); + //write all the cache + for ( i = 0; i < ( *aasworld ).numareas; i++ ) + { + for ( cache = ( *aasworld ).portalcache[i]; cache; cache = cache->next ) + { + botimport.FS_Write( cache, cache->size, fp ); + } //end for + } //end for + for ( i = 0; i < ( *aasworld ).numclusters; i++ ) + { + cluster = &( *aasworld ).clusters[i]; + for ( j = 0; j < cluster->numareas; j++ ) + { + for ( cache = ( *aasworld ).clusterareacache[i][j]; cache; cache = cache->next ) + { + botimport.FS_Write( cache, cache->size, fp ); + } //end for + } //end for + } //end for + // write the visareas + for ( i = 0; i < ( *aasworld ).numareas; i++ ) + { + if ( !( *aasworld ).areavisibility[i] ) { + size = 0; + botimport.FS_Write( &size, sizeof( int ), fp ); + continue; + } + AAS_DecompressVis( ( *aasworld ).areavisibility[i], ( *aasworld ).numareas, ( *aasworld ).decompressedvis ); + size = AAS_CompressVis( ( *aasworld ).decompressedvis, ( *aasworld ).numareas, buf ); + botimport.FS_Write( &size, sizeof( int ), fp ); + botimport.FS_Write( buf, size, fp ); + } + // write the waypoints + botimport.FS_Write( ( *aasworld ).areawaypoints, sizeof( vec3_t ) * ( *aasworld ).numareas, fp ); + // + botimport.FS_FCloseFile( fp ); + botimport.Print( PRT_MESSAGE, "\nroute cache written to %s\n", filename ); +} //end of the function AAS_WriteRouteCache +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +aas_routingcache_t *AAS_ReadCache( fileHandle_t fp ) { + int i, size; + aas_routingcache_t *cache; + + botimport.FS_Read( &size, sizeof( size ), fp ); + size = LittleLong( size ); + cache = (aas_routingcache_t *) AAS_RoutingGetMemory( size ); + cache->size = size; + botimport.FS_Read( (unsigned char *)cache + sizeof( size ), size - sizeof( size ), fp ); + + if ( 1 != LittleLong( 1 ) ) { + cache->time = LittleFloat( cache->time ); + cache->cluster = LittleLong( cache->cluster ); + cache->areanum = LittleLong( cache->areanum ); + cache->origin[0] = LittleFloat( cache->origin[0] ); + cache->origin[1] = LittleFloat( cache->origin[1] ); + cache->origin[2] = LittleFloat( cache->origin[2] ); + cache->starttraveltime = LittleFloat( cache->starttraveltime ); + cache->travelflags = LittleLong( cache->travelflags ); + } + +// cache->reachabilities = (unsigned char *) cache + sizeof(aas_routingcache_t) - sizeof(unsigned short) + +// (size - sizeof(aas_routingcache_t) + sizeof(unsigned short)) / 3 * 2; + cache->reachabilities = (unsigned char *) cache + sizeof( aas_routingcache_t ) + + ( ( size - sizeof( aas_routingcache_t ) ) / 3 ) * 2; + + //DAJ BUGFIX for missing byteswaps for traveltimes + size = ( size - sizeof( aas_routingcache_t ) ) / 3 + 1; + for ( i = 0; i < size; i++ ) { + cache->traveltimes[i] = LittleShort( cache->traveltimes[i] ); + } + return cache; +} //end of the function AAS_ReadCache +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_ReadRouteCache( void ) { + int i, clusterareanum, size; + fileHandle_t fp = 0; + char filename[MAX_QPATH]; + routecacheheader_t routecacheheader; + aas_routingcache_t *cache; + + Com_sprintf( filename, MAX_QPATH, "maps/%s.rcd", ( *aasworld ).mapname ); + botimport.FS_FOpenFile( filename, &fp, FS_READ ); + if ( !fp ) { + return qfalse; + } //end if + botimport.FS_Read( &routecacheheader, sizeof( routecacheheader_t ), fp ); + + // GJD: route cache data MUST be written on a PC because I've not altered the writing code. + + routecacheheader.areacrc = LittleLong( routecacheheader.areacrc ); + routecacheheader.clustercrc = LittleLong( routecacheheader.clustercrc ); + routecacheheader.ident = LittleLong( routecacheheader.ident ); + routecacheheader.numareacache = LittleLong( routecacheheader.numareacache ); + routecacheheader.numareas = LittleLong( routecacheheader.numareas ); + routecacheheader.numclusters = LittleLong( routecacheheader.numclusters ); + routecacheheader.numportalcache = LittleLong( routecacheheader.numportalcache ); + routecacheheader.reachcrc = LittleLong( routecacheheader.reachcrc ); + routecacheheader.version = LittleLong( routecacheheader.version ); + + if ( routecacheheader.ident != RCID ) { + botimport.FS_FCloseFile( fp ); + Com_Printf( "%s is not a route cache dump\n", filename ); // not an aas_error because we want to continue + return qfalse; // and remake them by returning false here + } //end if + + if ( routecacheheader.version != RCVERSION ) { + botimport.FS_FCloseFile( fp ); + Com_Printf( "route cache dump has wrong version %d, should be %d", routecacheheader.version, RCVERSION ); + return qfalse; + } //end if + if ( routecacheheader.numareas != ( *aasworld ).numareas ) { + botimport.FS_FCloseFile( fp ); + //AAS_Error("route cache dump has wrong number of areas\n"); + return qfalse; + } //end if + if ( routecacheheader.numclusters != ( *aasworld ).numclusters ) { + botimport.FS_FCloseFile( fp ); + //AAS_Error("route cache dump has wrong number of clusters\n"); + return qfalse; + } //end if +#ifdef _WIN32 // crc code is only good on intel machines + if ( routecacheheader.areacrc != + CRC_ProcessString( (unsigned char *)( *aasworld ).areas, sizeof( aas_area_t ) * ( *aasworld ).numareas ) ) { + botimport.FS_FCloseFile( fp ); + //AAS_Error("route cache dump area CRC incorrect\n"); + return qfalse; + } //end if + if ( routecacheheader.clustercrc != + CRC_ProcessString( (unsigned char *)( *aasworld ).clusters, sizeof( aas_cluster_t ) * ( *aasworld ).numclusters ) ) { + botimport.FS_FCloseFile( fp ); + //AAS_Error("route cache dump cluster CRC incorrect\n"); + return qfalse; + } //end if + if ( routecacheheader.reachcrc != + CRC_ProcessString( (unsigned char *)( *aasworld ).reachability, sizeof( aas_reachability_t ) * ( *aasworld ).reachabilitysize ) ) { + botimport.FS_FCloseFile( fp ); + //AAS_Error("route cache dump reachability CRC incorrect\n"); + return qfalse; + } //end if +#endif + //read all the portal cache + for ( i = 0; i < routecacheheader.numportalcache; i++ ) + { + cache = AAS_ReadCache( fp ); + cache->next = ( *aasworld ).portalcache[cache->areanum]; + cache->prev = NULL; + if ( ( *aasworld ).portalcache[cache->areanum] ) { + ( *aasworld ).portalcache[cache->areanum]->prev = cache; + } + ( *aasworld ).portalcache[cache->areanum] = cache; + } //end for + //read all the cluster area cache + for ( i = 0; i < routecacheheader.numareacache; i++ ) + { + cache = AAS_ReadCache( fp ); + clusterareanum = AAS_ClusterAreaNum( cache->cluster, cache->areanum ); + cache->next = ( *aasworld ).clusterareacache[cache->cluster][clusterareanum]; + cache->prev = NULL; + if ( ( *aasworld ).clusterareacache[cache->cluster][clusterareanum] ) { + ( *aasworld ).clusterareacache[cache->cluster][clusterareanum]->prev = cache; + } + ( *aasworld ).clusterareacache[cache->cluster][clusterareanum] = cache; + } //end for + // read the visareas + ( *aasworld ).areavisibility = (byte **) GetClearedMemory( ( *aasworld ).numareas * sizeof( byte * ) ); + ( *aasworld ).decompressedvis = (byte *) GetClearedMemory( ( *aasworld ).numareas * sizeof( byte ) ); + for ( i = 0; i < ( *aasworld ).numareas; i++ ) + { + botimport.FS_Read( &size, sizeof( size ), fp ); + size = LittleLong( size ); + if ( size ) { + ( *aasworld ).areavisibility[i] = (byte *) GetMemory( size ); + botimport.FS_Read( ( *aasworld ).areavisibility[i], size, fp ); + } + } + // read the area waypoints + ( *aasworld ).areawaypoints = (vec3_t *) GetClearedMemory( ( *aasworld ).numareas * sizeof( vec3_t ) ); + botimport.FS_Read( ( *aasworld ).areawaypoints, ( *aasworld ).numareas * sizeof( vec3_t ), fp ); + if ( 1 != LittleLong( 1 ) ) { + for ( i = 0; i < ( *aasworld ).numareas; i++ ) { + ( *aasworld ).areawaypoints[i][0] = LittleFloat( ( *aasworld ).areawaypoints[i][0] ); + ( *aasworld ).areawaypoints[i][1] = LittleFloat( ( *aasworld ).areawaypoints[i][1] ); + ( *aasworld ).areawaypoints[i][2] = LittleFloat( ( *aasworld ).areawaypoints[i][2] ); + } + } + // + botimport.FS_FCloseFile( fp ); + return qtrue; +} //end of the function AAS_ReadRouteCache +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_CreateVisibility( void ); +void AAS_InitRouting( void ) { + AAS_InitTravelFlagFromType(); + //initialize the routing update fields + AAS_InitRoutingUpdate(); + //create reversed reachability links used by the routing update algorithm + AAS_CreateReversedReachability(); + //initialize the cluster cache + AAS_InitClusterAreaCache(); + //initialize portal cache + AAS_InitPortalCache(); + //initialize the area travel times + AAS_CalculateAreaTravelTimes(); + //calculate the maximum travel times through portals + AAS_InitPortalMaxTravelTimes(); + // +#ifdef ROUTING_DEBUG + numareacacheupdates = 0; + numportalcacheupdates = 0; +#endif //ROUTING_DEBUG + // + routingcachesize = 0; + max_routingcachesize = 1024 * (int) LibVarValue( "max_routingcache", "4096" ); + // + // Ridah, load or create the routing cache + if ( !AAS_ReadRouteCache() ) { + ( *aasworld ).initialized = qtrue; // Hack, so routing can compute traveltimes + AAS_CreateVisibility(); + AAS_CreateAllRoutingCache(); + ( *aasworld ).initialized = qfalse; + + AAS_WriteRouteCache(); // save it so we don't have to create it again + } + // done. +} //end of the function AAS_InitRouting +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FreeRoutingCaches( void ) { + // free all the existing cluster area cache + AAS_FreeAllClusterAreaCache(); + // free all the existing portal cache + AAS_FreeAllPortalCache(); + // free all the existing area visibility data + AAS_FreeAreaVisibility(); + // free cached travel times within areas + if ( ( *aasworld ).areatraveltimes ) { + AAS_RoutingFreeMemory( ( *aasworld ).areatraveltimes ); + } + ( *aasworld ).areatraveltimes = NULL; + // free cached maximum travel time through cluster portals + if ( ( *aasworld ).portalmaxtraveltimes ) { + AAS_RoutingFreeMemory( ( *aasworld ).portalmaxtraveltimes ); + } + ( *aasworld ).portalmaxtraveltimes = NULL; + // free reversed reachability links + if ( ( *aasworld ).reversedreachability ) { + AAS_RoutingFreeMemory( ( *aasworld ).reversedreachability ); + } + ( *aasworld ).reversedreachability = NULL; + // free routing algorithm memory + if ( ( *aasworld ).areaupdate ) { + AAS_RoutingFreeMemory( ( *aasworld ).areaupdate ); + } + ( *aasworld ).areaupdate = NULL; + if ( ( *aasworld ).portalupdate ) { + AAS_RoutingFreeMemory( ( *aasworld ).portalupdate ); + } + ( *aasworld ).portalupdate = NULL; + // free area waypoints + if ( ( *aasworld ).areawaypoints ) { + FreeMemory( ( *aasworld ).areawaypoints ); + } + ( *aasworld ).areawaypoints = NULL; +} //end of the function AAS_FreeRoutingCaches +//=========================================================================== +// this function could be replaced by a bubble sort or for even faster +// routing by a B+ tree +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +__inline void AAS_AddUpdateToList( aas_routingupdate_t **updateliststart, + aas_routingupdate_t **updatelistend, + aas_routingupdate_t *update ) { + if ( !update->inlist ) { + if ( *updatelistend ) { + ( *updatelistend )->next = update; + } else { *updateliststart = update;} + update->prev = *updatelistend; + update->next = NULL; + *updatelistend = update; + update->inlist = qtrue; + } //end if +} //end of the function AAS_AddUpdateToList +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaContentsTravelFlag( int areanum ) { + int contents, tfl; + + contents = ( *aasworld ).areasettings[areanum].contents; + tfl = 0; + if ( contents & AREACONTENTS_WATER ) { + return tfl |= TFL_WATER; + } else if ( contents & AREACONTENTS_SLIME ) { + return tfl |= TFL_SLIME; + } else if ( contents & AREACONTENTS_LAVA ) { + return tfl |= TFL_LAVA; + } else { tfl |= TFL_AIR;} + if ( contents & AREACONTENTS_DONOTENTER_LARGE ) { + tfl |= TFL_DONOTENTER_LARGE; + } + if ( contents & AREACONTENTS_DONOTENTER ) { + return tfl |= TFL_DONOTENTER; + } + return tfl; +} //end of the function AAS_AreaContentsTravelFlag +//=========================================================================== +// update the given routing cache +// +// Parameter: areacache : routing cache to update +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_UpdateAreaRoutingCache( aas_routingcache_t *areacache ) { + int i, nextareanum, cluster, badtravelflags, clusterareanum, linknum; + int numreachabilityareas; + unsigned short int t, startareatraveltimes[128]; + aas_routingupdate_t *updateliststart, *updatelistend, *curupdate, *nextupdate; + aas_reachability_t *reach; + aas_reversedreachability_t *revreach; + aas_reversedlink_t *revlink; + +#ifdef ROUTING_DEBUG + numareacacheupdates++; +#endif //ROUTING_DEBUG + //number of reachability areas within this cluster + numreachabilityareas = ( *aasworld ).clusters[areacache->cluster].numreachabilityareas; + // + //clear the routing update fields +// memset((*aasworld).areaupdate, 0, (*aasworld).numareas * sizeof(aas_routingupdate_t)); + // + badtravelflags = ~areacache->travelflags; + // + clusterareanum = AAS_ClusterAreaNum( areacache->cluster, areacache->areanum ); + if ( clusterareanum >= numreachabilityareas ) { + return; + } + // + memset( startareatraveltimes, 0, sizeof( startareatraveltimes ) ); + // + curupdate = &( *aasworld ).areaupdate[clusterareanum]; + curupdate->areanum = areacache->areanum; + //VectorCopy(areacache->origin, curupdate->start); + curupdate->areatraveltimes = ( *aasworld ).areatraveltimes[areacache->areanum][0]; + curupdate->tmptraveltime = areacache->starttraveltime; + // + areacache->traveltimes[clusterareanum] = areacache->starttraveltime; + //put the area to start with in the current read list + curupdate->next = NULL; + curupdate->prev = NULL; + updateliststart = curupdate; + updatelistend = curupdate; + //while there are updates in the current list, flip the lists + while ( updateliststart ) + { + curupdate = updateliststart; + // + if ( curupdate->next ) { + curupdate->next->prev = NULL; + } else { updatelistend = NULL;} + updateliststart = curupdate->next; + // + curupdate->inlist = qfalse; + //check all reversed reachability links + revreach = &( *aasworld ).reversedreachability[curupdate->areanum]; + // + for ( i = 0, revlink = revreach->first; revlink; revlink = revlink->next, i++ ) + { + linknum = revlink->linknum; + reach = &( *aasworld ).reachability[linknum]; + //if there is used an undesired travel type + if ( ( *aasworld ).travelflagfortype[reach->traveltype] & badtravelflags ) { + continue; + } + //if not allowed to enter the next area + if ( ( *aasworld ).areasettings[reach->areanum].areaflags & AREA_DISABLED ) { + continue; + } + //if the next area has a not allowed travel flag + if ( AAS_AreaContentsTravelFlag( reach->areanum ) & badtravelflags ) { + continue; + } + //number of the area the reversed reachability leads to + nextareanum = revlink->areanum; + //get the cluster number of the area + cluster = ( *aasworld ).areasettings[nextareanum].cluster; + //don't leave the cluster + if ( cluster > 0 && cluster != areacache->cluster ) { + continue; + } + //get the number of the area in the cluster + clusterareanum = AAS_ClusterAreaNum( areacache->cluster, nextareanum ); + if ( clusterareanum >= numreachabilityareas ) { + continue; + } + //time already travelled plus the traveltime through + //the current area plus the travel time from the reachability + t = curupdate->tmptraveltime + + //AAS_AreaTravelTime(curupdate->areanum, curupdate->start, reach->end) + + curupdate->areatraveltimes[i] + + reach->traveltime; + // + ( *aasworld ).frameroutingupdates++; + // + if ( !areacache->traveltimes[clusterareanum] || + areacache->traveltimes[clusterareanum] > t ) { + areacache->traveltimes[clusterareanum] = t; + areacache->reachabilities[clusterareanum] = linknum - ( *aasworld ).areasettings[nextareanum].firstreachablearea; + nextupdate = &( *aasworld ).areaupdate[clusterareanum]; + nextupdate->areanum = nextareanum; + nextupdate->tmptraveltime = t; + //VectorCopy(reach->start, nextupdate->start); + nextupdate->areatraveltimes = ( *aasworld ).areatraveltimes[nextareanum][linknum - + ( *aasworld ).areasettings[nextareanum].firstreachablearea]; + if ( !nextupdate->inlist ) { + nextupdate->next = NULL; + nextupdate->prev = updatelistend; + if ( updatelistend ) { + updatelistend->next = nextupdate; + } else { updateliststart = nextupdate;} + updatelistend = nextupdate; + nextupdate->inlist = qtrue; + } //end if + } //end if + } //end for + } //end while +} //end of the function AAS_UpdateAreaRoutingCache +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +aas_routingcache_t *AAS_GetAreaRoutingCache( int clusternum, int areanum, int travelflags, qboolean forceUpdate ) { + int clusterareanum; + aas_routingcache_t *cache, *clustercache; + + //number of the area in the cluster + clusterareanum = AAS_ClusterAreaNum( clusternum, areanum ); + //pointer to the cache for the area in the cluster + clustercache = ( *aasworld ).clusterareacache[clusternum][clusterareanum]; + //find the cache without undesired travel flags + for ( cache = clustercache; cache; cache = cache->next ) + { + //if there aren't used any undesired travel types for the cache + if ( cache->travelflags == travelflags ) { + break; + } + } //end for + //if there was no cache + if ( !cache ) { + //NOTE: the number of routing updates is limited per frame + if ( !forceUpdate && ( ( *aasworld ).frameroutingupdates > MAX_FRAMEROUTINGUPDATES ) ) { + return NULL; + } //end if + + cache = AAS_AllocRoutingCache( ( *aasworld ).clusters[clusternum].numreachabilityareas ); + cache->cluster = clusternum; + cache->areanum = areanum; + VectorCopy( ( *aasworld ).areas[areanum].center, cache->origin ); + cache->starttraveltime = 1; + cache->travelflags = travelflags; + cache->prev = NULL; + cache->next = clustercache; + if ( clustercache ) { + clustercache->prev = cache; + } + ( *aasworld ).clusterareacache[clusternum][clusterareanum] = cache; + AAS_UpdateAreaRoutingCache( cache ); + } //end if + //the cache has been accessed + cache->time = AAS_RoutingTime(); + return cache; +} //end of the function AAS_GetAreaRoutingCache +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_UpdatePortalRoutingCache( aas_routingcache_t *portalcache ) { + int i, portalnum, clusterareanum, clusternum; + unsigned short int t; + aas_portal_t *portal; + aas_cluster_t *cluster; + aas_routingcache_t *cache; + aas_routingupdate_t *updateliststart, *updatelistend, *curupdate, *nextupdate; + +#ifdef ROUTING_DEBUG + numportalcacheupdates++; +#endif //ROUTING_DEBUG + //clear the routing update fields +// memset((*aasworld).portalupdate, 0, ((*aasworld).numportals+1) * sizeof(aas_routingupdate_t)); + // + curupdate = &( *aasworld ).portalupdate[( *aasworld ).numportals]; + curupdate->cluster = portalcache->cluster; + curupdate->areanum = portalcache->areanum; + curupdate->tmptraveltime = portalcache->starttraveltime; + //if the start area is a cluster portal, store the travel time for that portal + clusternum = ( *aasworld ).areasettings[portalcache->areanum].cluster; + if ( clusternum < 0 ) { + portalcache->traveltimes[-clusternum] = portalcache->starttraveltime; + } //end if + //put the area to start with in the current read list + curupdate->next = NULL; + curupdate->prev = NULL; + updateliststart = curupdate; + updatelistend = curupdate; + //while there are updates in the current list, flip the lists + while ( updateliststart ) + { + curupdate = updateliststart; + //remove the current update from the list + if ( curupdate->next ) { + curupdate->next->prev = NULL; + } else { updatelistend = NULL;} + updateliststart = curupdate->next; + //current update is removed from the list + curupdate->inlist = qfalse; + // + cluster = &( *aasworld ).clusters[curupdate->cluster]; + // + cache = AAS_GetAreaRoutingCache( curupdate->cluster, + curupdate->areanum, portalcache->travelflags, qtrue ); + //take all portals of the cluster + for ( i = 0; i < cluster->numportals; i++ ) + { + portalnum = ( *aasworld ).portalindex[cluster->firstportal + i]; + portal = &( *aasworld ).portals[portalnum]; + //if this is the portal of the current update continue + if ( portal->areanum == curupdate->areanum ) { + continue; + } + // + clusterareanum = AAS_ClusterAreaNum( curupdate->cluster, portal->areanum ); + if ( clusterareanum >= cluster->numreachabilityareas ) { + continue; + } + // + t = cache->traveltimes[clusterareanum]; + if ( !t ) { + continue; + } + t += curupdate->tmptraveltime; + // + if ( !portalcache->traveltimes[portalnum] || + portalcache->traveltimes[portalnum] > t ) { + portalcache->traveltimes[portalnum] = t; + portalcache->reachabilities[portalnum] = cache->reachabilities[clusterareanum]; + nextupdate = &( *aasworld ).portalupdate[portalnum]; + if ( portal->frontcluster == curupdate->cluster ) { + nextupdate->cluster = portal->backcluster; + } //end if + else + { + nextupdate->cluster = portal->frontcluster; + } //end else + nextupdate->areanum = portal->areanum; + //add travel time through actual portal area for the next update + nextupdate->tmptraveltime = t + ( *aasworld ).portalmaxtraveltimes[portalnum]; + if ( !nextupdate->inlist ) { + nextupdate->next = NULL; + nextupdate->prev = updatelistend; + if ( updatelistend ) { + updatelistend->next = nextupdate; + } else { updateliststart = nextupdate;} + updatelistend = nextupdate; + nextupdate->inlist = qtrue; + } //end if + } //end if + } //end for + } //end while +} //end of the function AAS_UpdatePortalRoutingCache +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +aas_routingcache_t *AAS_GetPortalRoutingCache( int clusternum, int areanum, int travelflags ) { + aas_routingcache_t *cache; + + //find the cached portal routing if existing + for ( cache = ( *aasworld ).portalcache[areanum]; cache; cache = cache->next ) + { + if ( cache->travelflags == travelflags ) { + break; + } + } //end for + //if the portal routing isn't cached + if ( !cache ) { + cache = AAS_AllocRoutingCache( ( *aasworld ).numportals ); + cache->cluster = clusternum; + cache->areanum = areanum; + VectorCopy( ( *aasworld ).areas[areanum].center, cache->origin ); + cache->starttraveltime = 1; + cache->travelflags = travelflags; + //add the cache to the cache list + cache->prev = NULL; + cache->next = ( *aasworld ).portalcache[areanum]; + if ( ( *aasworld ).portalcache[areanum] ) { + ( *aasworld ).portalcache[areanum]->prev = cache; + } + ( *aasworld ).portalcache[areanum] = cache; + //update the cache + AAS_UpdatePortalRoutingCache( cache ); + } //end if + //the cache has been accessed + cache->time = AAS_RoutingTime(); + return cache; +} //end of the function AAS_GetPortalRoutingCache +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaRouteToGoalArea( int areanum, vec3_t origin, int goalareanum, int travelflags, int *traveltime, int *reachnum ) { + int clusternum, goalclusternum, portalnum, i, clusterareanum, bestreachnum; + unsigned short int t, besttime; + aas_portal_t *portal; + aas_cluster_t *cluster; + aas_routingcache_t *areacache, *portalcache; + aas_reachability_t *reach; + aas_portalindex_t *pPortalnum; + + if ( !( *aasworld ).initialized ) { + return qfalse; + } + + if ( areanum == goalareanum ) { + *traveltime = 1; + *reachnum = 0; + return qtrue; + } //end if + // + if ( areanum <= 0 || areanum >= ( *aasworld ).numareas ) { + if ( bot_developer ) { + botimport.Print( PRT_ERROR, "AAS_AreaTravelTimeToGoalArea: areanum %d out of range\n", areanum ); + } //end if + return qfalse; + } //end if + if ( goalareanum <= 0 || goalareanum >= ( *aasworld ).numareas ) { + if ( bot_developer ) { + botimport.Print( PRT_ERROR, "AAS_AreaTravelTimeToGoalArea: goalareanum %d out of range\n", goalareanum ); + } //end if + return qfalse; + } //end if + + //make sure the routing cache doesn't grow to large + while ( routingcachesize > max_routingcachesize ) { + if ( !AAS_FreeOldestCache() ) { + break; + } + } + // + if ( AAS_AreaDoNotEnter( areanum ) || AAS_AreaDoNotEnter( goalareanum ) ) { + travelflags |= TFL_DONOTENTER; + } //end if + if ( AAS_AreaDoNotEnterLarge( areanum ) || AAS_AreaDoNotEnterLarge( goalareanum ) ) { + travelflags |= TFL_DONOTENTER_LARGE; + } //end if + //NOTE: the number of routing updates is limited per frame + /* + if ((*aasworld).frameroutingupdates > MAX_FRAMEROUTINGUPDATES) + { + #ifdef DEBUG + //Log_Write("WARNING: AAS_AreaTravelTimeToGoalArea: frame routing updates overflowed"); + #endif + return 0; + } //end if + */ + // + clusternum = ( *aasworld ).areasettings[areanum].cluster; + goalclusternum = ( *aasworld ).areasettings[goalareanum].cluster; + //check if the area is a portal of the goal area cluster + if ( clusternum < 0 && goalclusternum > 0 ) { + portal = &( *aasworld ).portals[-clusternum]; + if ( portal->frontcluster == goalclusternum || + portal->backcluster == goalclusternum ) { + clusternum = goalclusternum; + } //end if + } //end if + //check if the goalarea is a portal of the area cluster + else if ( clusternum > 0 && goalclusternum < 0 ) { + portal = &( *aasworld ).portals[-goalclusternum]; + if ( portal->frontcluster == clusternum || + portal->backcluster == clusternum ) { + goalclusternum = clusternum; + } //end if + } //end if + //if both areas are in the same cluster + //NOTE: there might be a shorter route via another cluster!!! but we don't care + if ( clusternum > 0 && goalclusternum > 0 && clusternum == goalclusternum ) { + // + areacache = AAS_GetAreaRoutingCache( clusternum, goalareanum, travelflags, qfalse ); + // RF, note that the routing cache might be NULL now since we are restricting + // the updates per frame, hopefully rejected cache's will be requested again + // when things have settled down + if ( !areacache ) { + return qfalse; + } + //the number of the area in the cluster + clusterareanum = AAS_ClusterAreaNum( clusternum, areanum ); + //the cluster the area is in + cluster = &( *aasworld ).clusters[clusternum]; + //if the area is NOT a reachability area + if ( clusterareanum >= cluster->numreachabilityareas ) { + return qfalse; + } + //if it is possible to travel to the goal area through this cluster + if ( areacache->traveltimes[clusterareanum] != 0 ) { + *reachnum = ( *aasworld ).areasettings[areanum].firstreachablearea + + areacache->reachabilities[clusterareanum]; + // + if ( !origin ) { + *traveltime = areacache->traveltimes[clusterareanum]; + return qtrue; + } + // + reach = &( *aasworld ).reachability[*reachnum]; + *traveltime = areacache->traveltimes[clusterareanum] + + AAS_AreaTravelTime( areanum, origin, reach->start ); + return qtrue; + } //end if + } //end if + // + clusternum = ( *aasworld ).areasettings[areanum].cluster; + goalclusternum = ( *aasworld ).areasettings[goalareanum].cluster; + //if the goal area is a portal + if ( goalclusternum < 0 ) { + //just assume the goal area is part of the front cluster + portal = &( *aasworld ).portals[-goalclusternum]; + goalclusternum = portal->frontcluster; + } //end if + //get the portal routing cache + portalcache = AAS_GetPortalRoutingCache( goalclusternum, goalareanum, travelflags ); + //if the area is a cluster portal, read directly from the portal cache + if ( clusternum < 0 ) { + *traveltime = portalcache->traveltimes[-clusternum]; + *reachnum = ( *aasworld ).areasettings[areanum].firstreachablearea + + portalcache->reachabilities[-clusternum]; + return qtrue; + } + // + besttime = 0; + bestreachnum = -1; + //the cluster the area is in + cluster = &( *aasworld ).clusters[clusternum]; + //current area inside the current cluster + clusterareanum = AAS_ClusterAreaNum( clusternum, areanum ); + //if the area is NOT a reachability area + if ( clusterareanum >= cluster->numreachabilityareas ) { + return qfalse; + } + // + pPortalnum = ( *aasworld ).portalindex + cluster->firstportal; + //find the portal of the area cluster leading towards the goal area + for ( i = 0; i < cluster->numportals; i++, pPortalnum++ ) + { + portalnum = *pPortalnum; + //if the goal area isn't reachable from the portal + if ( !portalcache->traveltimes[portalnum] ) { + continue; + } + // + portal = ( *aasworld ).portals + portalnum; + // if the area in disabled + if ( ( *aasworld ).areasettings[portal->areanum].areaflags & AREA_DISABLED ) { + continue; + } + //get the cache of the portal area + areacache = AAS_GetAreaRoutingCache( clusternum, portal->areanum, travelflags, qfalse ); + // RF, this may be NULL if we were unable to calculate the cache this frame + if ( !areacache ) { + return qfalse; + } + //if the portal is NOT reachable from this area + if ( !areacache->traveltimes[clusterareanum] ) { + continue; + } + //total travel time is the travel time the portal area is from + //the goal area plus the travel time towards the portal area + t = portalcache->traveltimes[portalnum] + areacache->traveltimes[clusterareanum]; + //FIXME: add the exact travel time through the actual portal area + //NOTE: for now we just add the largest travel time through the area portal + // because we can't directly calculate the exact travel time + // to be more specific we don't know which reachability is used to travel + // into the portal area when coming from the current area + t += ( *aasworld ).portalmaxtraveltimes[portalnum]; + // + // Ridah, needs to be up here + *reachnum = ( *aasworld ).areasettings[areanum].firstreachablearea + + areacache->reachabilities[clusterareanum]; + +//botimport.Print(PRT_MESSAGE, "portal reachability: %i\n", (int)areacache->reachabilities[clusterareanum] ); + + if ( origin ) { + reach = ( *aasworld ).reachability + *reachnum; + t += AAS_AreaTravelTime( areanum, origin, reach->start ); + } //end if + //if the time is better than the one already found + if ( !besttime || t < besttime ) { + bestreachnum = *reachnum; + besttime = t; + } //end if + } //end for + // Ridah, check a route was found + if ( bestreachnum < 0 ) { + return qfalse; + } + *reachnum = bestreachnum; + *traveltime = besttime; + return qtrue; +} //end of the function AAS_AreaRouteToGoalArea +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaTravelTimeToGoalArea( int areanum, vec3_t origin, int goalareanum, int travelflags ) { + int traveltime, reachnum; + + if ( AAS_AreaRouteToGoalArea( areanum, origin, goalareanum, travelflags, &traveltime, &reachnum ) ) { + return traveltime; + } + return 0; +} //end of the function AAS_AreaTravelTimeToGoalArea +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaTravelTimeToGoalAreaCheckLoop( int areanum, vec3_t origin, int goalareanum, int travelflags, int loopareanum ) { + int traveltime, reachnum; + aas_reachability_t *reach; + + if ( AAS_AreaRouteToGoalArea( areanum, origin, goalareanum, travelflags, &traveltime, &reachnum ) ) { + reach = &( *aasworld ).reachability[reachnum]; + if ( loopareanum && reach->areanum == loopareanum ) { + return 0; // going here will cause a looped route + } + return traveltime; + } + return 0; +} //end of the function AAS_AreaTravelTimeToGoalArea +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaReachabilityToGoalArea( int areanum, vec3_t origin, int goalareanum, int travelflags ) { + int traveltime, reachnum; + + if ( AAS_AreaRouteToGoalArea( areanum, origin, goalareanum, travelflags, &traveltime, &reachnum ) ) { + return reachnum; + } + return 0; +} //end of the function AAS_AreaReachabilityToGoalArea +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ReachabilityFromNum( int num, struct aas_reachability_s *reach ) { + if ( !( *aasworld ).initialized ) { + memset( reach, 0, sizeof( aas_reachability_t ) ); + return; + } //end if + if ( num < 0 || num >= ( *aasworld ).reachabilitysize ) { + memset( reach, 0, sizeof( aas_reachability_t ) ); + return; + } //end if + memcpy( reach, &( *aasworld ).reachability[num], sizeof( aas_reachability_t ) );; +} //end of the function AAS_ReachabilityFromNum +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_NextAreaReachability( int areanum, int reachnum ) { + aas_areasettings_t *settings; + + if ( !( *aasworld ).initialized ) { + return 0; + } + + if ( areanum <= 0 || areanum >= ( *aasworld ).numareas ) { + botimport.Print( PRT_ERROR, "AAS_NextAreaReachability: areanum %d out of range\n", areanum ); + return 0; + } //end if + + settings = &( *aasworld ).areasettings[areanum]; + if ( !reachnum ) { + return settings->firstreachablearea; + } //end if + if ( reachnum < settings->firstreachablearea ) { + botimport.Print( PRT_FATAL, "AAS_NextAreaReachability: reachnum < settings->firstreachableara" ); + return 0; + } //end if + reachnum++; + if ( reachnum >= settings->firstreachablearea + settings->numreachableareas ) { + return 0; + } //end if + return reachnum; +} //end of the function AAS_NextAreaReachability +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_NextModelReachability( int num, int modelnum ) { + int i; + + if ( num <= 0 ) { + num = 1; + } else if ( num >= ( *aasworld ).reachabilitysize ) { + return 0; + } else { num++;} + // + for ( i = num; i < ( *aasworld ).reachabilitysize; i++ ) + { + if ( ( *aasworld ).reachability[i].traveltype == TRAVEL_ELEVATOR ) { + if ( ( *aasworld ).reachability[i].facenum == modelnum ) { + return i; + } + } //end if + else if ( ( *aasworld ).reachability[i].traveltype == TRAVEL_FUNCBOB ) { + if ( ( ( *aasworld ).reachability[i].facenum & 0x0000FFFF ) == modelnum ) { + return i; + } + } //end if + } //end for + return 0; +} //end of the function AAS_NextModelReachability +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_RandomGoalArea( int areanum, int travelflags, int *goalareanum, vec3_t goalorigin ) { + int i, n, t; + vec3_t start, end; + aas_trace_t trace; + + //if the area has no reachabilities + if ( !AAS_AreaReachability( areanum ) ) { + return qfalse; + } + // + n = ( *aasworld ).numareas * random(); + for ( i = 0; i < ( *aasworld ).numareas; i++ ) + { + if ( n <= 0 ) { + n = 1; + } + if ( n >= ( *aasworld ).numareas ) { + n = 1; + } + if ( AAS_AreaReachability( n ) ) { + t = AAS_AreaTravelTimeToGoalArea( areanum, ( *aasworld ).areas[areanum].center, n, travelflags ); + //if the goal is reachable + if ( t > 0 ) { + if ( AAS_AreaSwim( n ) ) { + *goalareanum = n; + VectorCopy( ( *aasworld ).areas[n].center, goalorigin ); + //botimport.Print(PRT_MESSAGE, "found random goal area %d\n", *goalareanum); + return qtrue; + } //end if + VectorCopy( ( *aasworld ).areas[n].center, start ); + if ( !AAS_PointAreaNum( start ) ) { + Log_Write( "area %d center %f %f %f in solid?", n, + start[0], start[1], start[2] ); + } + VectorCopy( start, end ); + end[2] -= 300; + trace = AAS_TraceClientBBox( start, end, PRESENCE_CROUCH, -1 ); + if ( !trace.startsolid && AAS_PointAreaNum( trace.endpos ) == n ) { + if ( AAS_AreaGroundFaceArea( n ) > 300 ) { + *goalareanum = n; + VectorCopy( trace.endpos, goalorigin ); + //botimport.Print(PRT_MESSAGE, "found random goal area %d\n", *goalareanum); + return qtrue; + } //end if + } //end if + } //end if + } //end if + n++; + } //end for + return qfalse; +} //end of the function AAS_RandomGoalArea +//=========================================================================== +// run-length compression on zeros +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_CompressVis( byte *vis, int numareas, byte *dest ) { + int j; + int rep; + //int visrow; + byte *dest_p; + byte check; + + // + dest_p = dest; + //visrow = (numareas + 7)>>3; + + for ( j = 0 ; j < numareas /*visrow*/ ; j++ ) + { + *dest_p++ = vis[j]; + check = vis[j]; + //if (vis[j]) + // continue; + + rep = 1; + for ( j++; j < numareas /*visrow*/ ; j++ ) + if ( vis[j] != check || rep == 255 ) { + break; + } else { + rep++; + } + *dest_p++ = rep; + j--; + } + return dest_p - dest; +} //end of the function AAS_CompressVis +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_DecompressVis( byte *in, int numareas, byte *decompressed ) { + byte c; + byte *out; + //int row; + byte *end; + + // initialize the vis data, only set those that are visible + memset( decompressed, 0, numareas ); + + //row = (numareas+7)>>3; + out = decompressed; + end = ( byte * )( (int)decompressed + numareas ); + + do + { + /* + if (*in) + { + *out++ = *in++; + continue; + } + */ + + c = in[1]; + if ( !c ) { + AAS_Error( "DecompressVis: 0 repeat" ); + } + if ( *in ) { // we need to set these bits + memset( out, 1, c ); + } + in += 2; + /* + while (c) + { + *out++ = 0; + c--; + } + */ + out += c; + } while ( out < end ); +} //end of the function AAS_DecompressVis +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaVisible( int srcarea, int destarea ) { + if ( srcarea != ( *aasworld ).decompressedvisarea ) { + if ( !( *aasworld ).areavisibility[srcarea] ) { + return qfalse; + } + AAS_DecompressVis( ( *aasworld ).areavisibility[srcarea], + ( *aasworld ).numareas, ( *aasworld ).decompressedvis ); + ( *aasworld ).decompressedvisarea = srcarea; + } + return ( *aasworld ).decompressedvis[destarea]; +} //end of the function AAS_AreaVisible +//=========================================================================== +// just center to center visibility checking... +// FIXME: implement a correct full vis +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_CreateVisibility( void ) { + int i, j, size, totalsize; + vec3_t endpos, mins, maxs; + bsp_trace_t trace; + byte *buf; + byte *validareas; + int numvalid = 0; + byte *areaTable = NULL; + int numAreas, numAreaBits; + + numAreas = ( *aasworld ).numareas; + numAreaBits = ( ( numAreas + 8 ) >> 3 ); + areaTable = (byte *) GetClearedMemory( numAreas * numAreaBits * sizeof( byte ) ); + + buf = (byte *) GetClearedMemory( numAreas * 2 * sizeof( byte ) ); // in case it ends up bigger than the decompressedvis, which is rare but possible + validareas = (byte *) GetClearedMemory( numAreas * sizeof( byte ) ); + + ( *aasworld ).areavisibility = (byte **) GetClearedMemory( numAreas * sizeof( byte * ) ); + ( *aasworld ).decompressedvis = (byte *) GetClearedMemory( numAreas * sizeof( byte ) ); + ( *aasworld ).areawaypoints = (vec3_t *) GetClearedMemory( numAreas * sizeof( vec3_t ) ); + totalsize = numAreas * sizeof( byte * ); + for ( i = 1; i < numAreas; i++ ) + { + if ( !AAS_AreaReachability( i ) ) { + continue; + } + + // find the waypoint + VectorCopy( ( *aasworld ).areas[i].center, endpos ); + endpos[2] -= 256; + AAS_PresenceTypeBoundingBox( PRESENCE_NORMAL, mins, maxs ); +// maxs[2] = 0; + trace = AAS_Trace( ( *aasworld ).areas[i].center, mins, maxs, endpos, -1, CONTENTS_SOLID | CONTENTS_PLAYERCLIP | CONTENTS_MONSTERCLIP ); + if ( !trace.startsolid && trace.fraction < 1 && AAS_PointAreaNum( trace.endpos ) == i ) { + VectorCopy( trace.endpos, ( *aasworld ).areawaypoints[i] ); + validareas[i] = 1; + numvalid++; + } else { + continue; + } + } + + for ( i = 1; i < numAreas; i++ ) + { + if ( !validareas[i] ) { + continue; + } + + for ( j = 1; j < numAreas; j++ ) + { + ( *aasworld ).decompressedvis[j] = 0; + if ( i == j ) { + ( *aasworld ).decompressedvis[j] = 1; + if ( areaTable ) { + areaTable[ ( i * numAreaBits ) + ( j >> 3 ) ] |= ( 1 << ( j & 7 ) ); + } + continue; + } + if ( !validareas[j] ) { + continue; + } //end if + // if we have already checked this combination, copy the result + if ( areaTable && ( i > j ) ) { + // use the reverse result stored in the table + if ( areaTable[ ( j * numAreaBits ) + ( i >> 3 ) ] & ( 1 << ( i & 7 ) ) ) { + ( *aasworld ).decompressedvis[j] = 1; + } + // done, move to the next area + continue; + } + + // RF, check PVS first, since it's much faster + if ( !AAS_inPVS( ( *aasworld ).areawaypoints[i], ( *aasworld ).areawaypoints[j] ) ) { + continue; + } + trace = AAS_Trace( ( *aasworld ).areawaypoints[i], NULL, NULL, ( *aasworld ).areawaypoints[j], -1, CONTENTS_SOLID ); + if ( trace.fraction >= 1 ) { + ( *aasworld ).decompressedvis[j] = 1; + } //end if + } //end for + size = AAS_CompressVis( ( *aasworld ).decompressedvis, numAreas, buf ); + ( *aasworld ).areavisibility[i] = (byte *) GetMemory( size ); + memcpy( ( *aasworld ).areavisibility[i], buf, size ); + totalsize += size; + } //end for + if ( areaTable ) { + FreeMemory( areaTable ); + } + botimport.Print( PRT_MESSAGE, "AAS_CreateVisibility: compressed vis size = %i\n", totalsize ); +} //end of the function AAS_CreateVisibility +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float VectorDistance( vec3_t v1, vec3_t v2 ); +extern void ProjectPointOntoVector( vec3_t point, vec3_t vStart, vec3_t vEnd, vec3_t vProj ) ; +int AAS_NearestHideArea( int srcnum, vec3_t origin, int areanum, int enemynum, vec3_t enemyorigin, int enemyareanum, int travelflags ) { + int i, j, nextareanum, badtravelflags, numreach, bestarea; + unsigned short int t, besttraveltime, enemytraveltime; + aas_routingupdate_t *updateliststart, *updatelistend, *curupdate, *nextupdate; + aas_reachability_t *reach; + float dist1, dist2; + float enemytraveldist; + vec3_t enemyVec; + qboolean startVisible; + vec3_t v1, v2, p; + #define MAX_HIDEAREA_LOOPS 3000 + static float lastTime; + static int loopCount; + // + if ( srcnum < 0 ) { // hack to force run this call + srcnum = -srcnum - 1; + lastTime = 0; + } + // don't run this more than once per frame + if ( lastTime == AAS_Time() && loopCount >= MAX_HIDEAREA_LOOPS ) { + return 0; + } + lastTime = AAS_Time(); + loopCount = 0; + // + if ( !( *aasworld ).hidetraveltimes ) { + ( *aasworld ).hidetraveltimes = (unsigned short int *) GetClearedMemory( ( *aasworld ).numareas * sizeof( unsigned short int ) ); + } else { + memset( ( *aasworld ).hidetraveltimes, 0, ( *aasworld ).numareas * sizeof( unsigned short int ) ); + } //end else + // + if ( !( *aasworld ).visCache ) { + ( *aasworld ).visCache = (byte *) GetClearedMemory( ( *aasworld ).numareas * sizeof( byte ) ); + } else { + memset( ( *aasworld ).visCache, 0, ( *aasworld ).numareas * sizeof( byte ) ); + } //end else + besttraveltime = 0; + bestarea = 0; + if ( enemyareanum ) { + enemytraveltime = AAS_AreaTravelTimeToGoalArea( areanum, origin, enemyareanum, travelflags ); + } + VectorSubtract( enemyorigin, origin, enemyVec ); + enemytraveldist = VectorNormalize( enemyVec ); + startVisible = botimport.AICast_VisibleFromPos( enemyorigin, enemynum, origin, srcnum, qfalse ); + // + badtravelflags = ~travelflags; + // + curupdate = &( *aasworld ).areaupdate[areanum]; + curupdate->areanum = areanum; + VectorCopy( origin, curupdate->start ); + curupdate->areatraveltimes = ( *aasworld ).areatraveltimes[areanum][0]; + curupdate->tmptraveltime = 0; + //put the area to start with in the current read list + curupdate->next = NULL; + curupdate->prev = NULL; + updateliststart = curupdate; + updatelistend = curupdate; + //while there are updates in the current list, flip the lists + while ( updateliststart ) + { + curupdate = updateliststart; + // + if ( curupdate->next ) { + curupdate->next->prev = NULL; + } else { updatelistend = NULL;} + updateliststart = curupdate->next; + // + curupdate->inlist = qfalse; + //check all reversed reachability links + numreach = ( *aasworld ).areasettings[curupdate->areanum].numreachableareas; + reach = &( *aasworld ).reachability[( *aasworld ).areasettings[curupdate->areanum].firstreachablearea]; + // + for ( i = 0; i < numreach; i++, reach++ ) + { + //if an undesired travel type is used + if ( ( *aasworld ).travelflagfortype[reach->traveltype] & badtravelflags ) { + continue; + } + // + if ( AAS_AreaContentsTravelFlag( reach->areanum ) & badtravelflags ) { + continue; + } + // dont pass through ladder areas + if ( ( *aasworld ).areasettings[reach->areanum].areaflags & AREA_LADDER ) { + continue; + } + // + if ( ( *aasworld ).areasettings[reach->areanum].areaflags & AREA_DISABLED ) { + continue; + } + //number of the area the reachability leads to + nextareanum = reach->areanum; + // if this moves us into the enemies area, skip it + if ( nextareanum == enemyareanum ) { + continue; + } + //time already travelled plus the traveltime through + //the current area plus the travel time from the reachability + t = curupdate->tmptraveltime + + AAS_AreaTravelTime( curupdate->areanum, curupdate->start, reach->start ) + + reach->traveltime; + // inc the loopCount, we are starting to use a bit of cpu time + loopCount++; + // if this isn't the fastest route to this area, ignore + if ( ( *aasworld ).hidetraveltimes[nextareanum] && ( *aasworld ).hidetraveltimes[nextareanum] < t ) { + continue; + } + ( *aasworld ).hidetraveltimes[nextareanum] = t; + // if the bestarea is this area, then it must be a longer route, so ignore it + if ( bestarea == nextareanum ) { + bestarea = 0; + besttraveltime = 0; + } + // do this test now, so we can reject the route if it starts out too long + if ( besttraveltime && t >= besttraveltime ) { + continue; + } + // + //avoid going near the enemy + ProjectPointOntoVector( enemyorigin, curupdate->start, reach->end, p ); + for ( j = 0; j < 3; j++ ) { + if ( ( p[j] > curupdate->start[j] + 0.1 && p[j] > reach->end[j] + 0.1 ) || + ( p[j] < curupdate->start[j] - 0.1 && p[j] < reach->end[j] - 0.1 ) ) { + break; + } + } + if ( j < 3 ) { + VectorSubtract( enemyorigin, reach->end, v2 ); + } //end if + else + { + VectorSubtract( enemyorigin, p, v2 ); + } //end else + dist2 = VectorLength( v2 ); + //never go through the enemy + if ( enemytraveldist > 32 && dist2 < enemytraveldist && dist2 < 256 ) { + continue; + } + // + VectorSubtract( reach->end, origin, v2 ); + if ( enemytraveldist > 32 && DotProduct( v2, enemyVec ) > enemytraveldist / 2 ) { + continue; + } + // + VectorSubtract( enemyorigin, curupdate->start, v1 ); + dist1 = VectorLength( v1 ); + // + if ( enemytraveldist > 32 && dist2 < dist1 ) { + t += ( dist1 - dist2 ) * 10; + // test it again after modifying it + if ( besttraveltime && t >= besttraveltime ) { + continue; + } + } + // make sure the hide area doesn't have anyone else in it + if ( AAS_IsEntityInArea( srcnum, -1, nextareanum ) ) { + t += 1000; // avoid this path/area + //continue; + } + // + // if we weren't visible when starting, make sure we don't move into their view + if ( enemyareanum && !startVisible && AAS_AreaVisible( enemyareanum, nextareanum ) ) { + continue; + //t += 1000; + } + // + if ( !besttraveltime || besttraveltime > t ) { + // + // if this area doesn't have a vis list, ignore it + if ( ( *aasworld ).areavisibility[nextareanum] ) { + //if the nextarea is not visible from the enemy area + if ( !AAS_AreaVisible( enemyareanum, nextareanum ) ) { // now last of all, check that this area is a safe hiding spot + if ( ( ( *aasworld ).visCache[nextareanum] == 2 ) || + ( !( *aasworld ).visCache[nextareanum] && !botimport.AICast_VisibleFromPos( enemyorigin, enemynum, ( *aasworld ).areawaypoints[nextareanum], srcnum, qfalse ) ) ) { + ( *aasworld ).visCache[nextareanum] = 2; + besttraveltime = t; + bestarea = nextareanum; + } else { + ( *aasworld ).visCache[nextareanum] = 1; + } + } //end if + } + // + // getting down to here is bad for cpu usage + if ( loopCount++ > MAX_HIDEAREA_LOOPS ) { + //botimport.Print(PRT_MESSAGE, "AAS_NearestHideArea: exceeded max loops, aborting\n" ); + continue; + } + // + // otherwise, add this to the list so we check is reachables + // disabled, this should only store the raw traveltime, not the adjusted time + //(*aasworld).hidetraveltimes[nextareanum] = t; + nextupdate = &( *aasworld ).areaupdate[nextareanum]; + nextupdate->areanum = nextareanum; + nextupdate->tmptraveltime = t; + //remember where we entered this area + VectorCopy( reach->end, nextupdate->start ); + //if this update is not in the list yet + if ( !nextupdate->inlist ) { + //add the new update to the end of the list + nextupdate->next = NULL; + nextupdate->prev = updatelistend; + if ( updatelistend ) { + updatelistend->next = nextupdate; + } else { updateliststart = nextupdate;} + updatelistend = nextupdate; + nextupdate->inlist = qtrue; + } //end if + } //end if + } //end for + } //end while + //botimport.Print(PRT_MESSAGE, "AAS_NearestHideArea: hidearea: %i, %i loops\n", bestarea, count ); + return bestarea; +} //end of the function AAS_NearestHideArea + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_FindAttackSpotWithinRange( int srcnum, int rangenum, int enemynum, float rangedist, int travelflags, float *outpos ) { + int i, nextareanum, badtravelflags, numreach, bestarea; + unsigned short int t, besttraveltime, enemytraveltime; + aas_routingupdate_t *updateliststart, *updatelistend, *curupdate, *nextupdate; + aas_reachability_t *reach; + vec3_t srcorg, rangeorg, enemyorg; + int srcarea, rangearea, enemyarea; + unsigned short int srctraveltime; + int count = 0; + #define MAX_ATTACKAREA_LOOPS 200 + static float lastTime; + // + // RF, currently doesn't work with multiple AAS worlds, so only enable for the default world + //if (aasworld != aasworlds) return 0; + // + // don't run this more than once per frame + if ( lastTime == AAS_Time() ) { + return 0; + } + lastTime = AAS_Time(); + // + if ( !( *aasworld ).hidetraveltimes ) { + ( *aasworld ).hidetraveltimes = (unsigned short int *) GetClearedMemory( ( *aasworld ).numareas * sizeof( unsigned short int ) ); + } else { + memset( ( *aasworld ).hidetraveltimes, 0, ( *aasworld ).numareas * sizeof( unsigned short int ) ); + } //end else + // + if ( !( *aasworld ).visCache ) { + ( *aasworld ).visCache = (byte *) GetClearedMemory( ( *aasworld ).numareas * sizeof( byte ) ); + } else { + memset( ( *aasworld ).visCache, 0, ( *aasworld ).numareas * sizeof( byte ) ); + } //end else + // + AAS_EntityOrigin( srcnum, srcorg ); + AAS_EntityOrigin( rangenum, rangeorg ); + AAS_EntityOrigin( enemynum, enemyorg ); + // + srcarea = BotFuzzyPointReachabilityArea( srcorg ); + rangearea = BotFuzzyPointReachabilityArea( rangeorg ); + enemyarea = BotFuzzyPointReachabilityArea( enemyorg ); + // + besttraveltime = 0; + bestarea = 0; + enemytraveltime = AAS_AreaTravelTimeToGoalArea( srcarea, srcorg, enemyarea, travelflags ); + // + badtravelflags = ~travelflags; + // + curupdate = &( *aasworld ).areaupdate[rangearea]; + curupdate->areanum = rangearea; + VectorCopy( rangeorg, curupdate->start ); + curupdate->areatraveltimes = ( *aasworld ).areatraveltimes[srcarea][0]; + curupdate->tmptraveltime = 0; + //put the area to start with in the current read list + curupdate->next = NULL; + curupdate->prev = NULL; + updateliststart = curupdate; + updatelistend = curupdate; + //while there are updates in the current list, flip the lists + while ( updateliststart ) + { + curupdate = updateliststart; + // + if ( curupdate->next ) { + curupdate->next->prev = NULL; + } else { updatelistend = NULL;} + updateliststart = curupdate->next; + // + curupdate->inlist = qfalse; + //check all reversed reachability links + numreach = ( *aasworld ).areasettings[curupdate->areanum].numreachableareas; + reach = &( *aasworld ).reachability[( *aasworld ).areasettings[curupdate->areanum].firstreachablearea]; + // + for ( i = 0; i < numreach; i++, reach++ ) + { + //if an undesired travel type is used + if ( ( *aasworld ).travelflagfortype[reach->traveltype] & badtravelflags ) { + continue; + } + // + if ( AAS_AreaContentsTravelFlag( reach->areanum ) & badtravelflags ) { + continue; + } + // dont pass through ladder areas + if ( ( *aasworld ).areasettings[reach->areanum].areaflags & AREA_LADDER ) { + continue; + } + // + if ( ( *aasworld ).areasettings[reach->areanum].areaflags & AREA_DISABLED ) { + continue; + } + //number of the area the reachability leads to + nextareanum = reach->areanum; + // if this moves us into the enemies area, skip it + if ( nextareanum == enemyarea ) { + continue; + } + // if we've already been to this area + if ( ( *aasworld ).hidetraveltimes[nextareanum] ) { + continue; + } + //time already travelled plus the traveltime through + //the current area plus the travel time from the reachability + if ( count++ > MAX_ATTACKAREA_LOOPS ) { + //botimport.Print(PRT_MESSAGE, "AAS_FindAttackSpotWithinRange: exceeded max loops, aborting\n" ); + if ( bestarea ) { + VectorCopy( ( *aasworld ).areawaypoints[bestarea], outpos ); + } + return bestarea; + } + t = curupdate->tmptraveltime + + AAS_AreaTravelTime( curupdate->areanum, curupdate->start, reach->start ) + + reach->traveltime; + // + // if it's too far from rangenum, ignore + if ( Distance( rangeorg, ( *aasworld ).areawaypoints[nextareanum] ) > rangedist ) { + continue; + } + // + // find the traveltime from srcnum + srctraveltime = AAS_AreaTravelTimeToGoalArea( srcarea, srcorg, nextareanum, travelflags ); + // do this test now, so we can reject the route if it starts out too long + if ( besttraveltime && srctraveltime >= besttraveltime ) { + continue; + } + // + // if this area doesn't have a vis list, ignore it + if ( ( *aasworld ).areavisibility[nextareanum] ) { + //if the nextarea can see the enemy area + if ( AAS_AreaVisible( enemyarea, nextareanum ) ) { // now last of all, check that this area is a good attacking spot + if ( ( ( *aasworld ).visCache[nextareanum] == 2 ) || + ( !( *aasworld ).visCache[nextareanum] && + ( count += 10 ) && // we are about to use lots of CPU time + botimport.AICast_CheckAttackAtPos( srcnum, enemynum, ( *aasworld ).areawaypoints[nextareanum], qfalse, qfalse ) ) ) { + ( *aasworld ).visCache[nextareanum] = 2; + besttraveltime = srctraveltime; + bestarea = nextareanum; + } else { + ( *aasworld ).visCache[nextareanum] = 1; + } + } //end if + } + ( *aasworld ).hidetraveltimes[nextareanum] = t; + nextupdate = &( *aasworld ).areaupdate[nextareanum]; + nextupdate->areanum = nextareanum; + nextupdate->tmptraveltime = t; + //remember where we entered this area + VectorCopy( reach->end, nextupdate->start ); + //if this update is not in the list yet + if ( !nextupdate->inlist ) { + //add the new update to the end of the list + nextupdate->next = NULL; + nextupdate->prev = updatelistend; + if ( updatelistend ) { + updatelistend->next = nextupdate; + } else { updateliststart = nextupdate;} + updatelistend = nextupdate; + nextupdate->inlist = qtrue; + } //end if + } //end for + } //end while +//botimport.Print(PRT_MESSAGE, "AAS_NearestHideArea: hidearea: %i, %i loops\n", bestarea, count ); + if ( bestarea ) { + VectorCopy( ( *aasworld ).areawaypoints[bestarea], outpos ); + } + return bestarea; +} //end of the function AAS_NearestHideArea + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_GetRouteFirstVisPos( vec3_t srcpos, vec3_t destpos, int travelflags, vec3_t retpos ) { + int srcarea, destarea, travarea; + vec3_t travpos; + int ftraveltime, freachnum, lasttraveltime; + aas_reachability_t reach; + int loops = 0; +#define MAX_GETROUTE_VISPOS_LOOPS 200 + // + // SRCPOS: enemy + // DESTPOS: self + // RETPOS: first area that is visible from destpos, in route from srcpos to destpos + srcarea = BotFuzzyPointReachabilityArea( srcpos ); + if ( !srcarea ) { + return qfalse; + } + destarea = BotFuzzyPointReachabilityArea( destpos ); + if ( !destarea ) { + return qfalse; + } + if ( destarea == srcarea ) { + VectorCopy( srcpos, retpos ); + return qtrue; + } + // + //if the srcarea can see the destarea + if ( AAS_AreaVisible( srcarea, destarea ) ) { + VectorCopy( srcpos, retpos ); + return qtrue; + } + // if this area doesn't have a vis list, ignore it + if ( !( *aasworld ).areavisibility[destarea] ) { + return qfalse; + } + // + travarea = srcarea; + VectorCopy( srcpos, travpos ); + lasttraveltime = -1; + while ( ( loops++ < MAX_GETROUTE_VISPOS_LOOPS ) && AAS_AreaRouteToGoalArea( travarea, travpos, destarea, travelflags, &ftraveltime, &freachnum ) ) { + if ( lasttraveltime >= 0 && ftraveltime >= lasttraveltime ) { + return qfalse; // we may be in a loop + } + lasttraveltime = ftraveltime; + // + AAS_ReachabilityFromNum( freachnum, &reach ); + if ( reach.areanum == destarea ) { + VectorCopy( travpos, retpos ); + return qtrue; + } + //if the reach area can see the destarea + if ( AAS_AreaVisible( reach.areanum, destarea ) ) { + VectorCopy( reach.end, retpos ); + return qtrue; + } + // + travarea = reach.areanum; + VectorCopy( reach.end, travpos ); + } + // + // unsuccessful + return qfalse; +} \ No newline at end of file diff --git a/src/botlib/be_aas_route.h b/src/botlib/be_aas_route.h new file mode 100644 index 0000000..bc2e134 --- /dev/null +++ b/src/botlib/be_aas_route.h @@ -0,0 +1,68 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_route.h + * + * desc: AAS + * + * + *****************************************************************************/ + +#ifdef AASINTERN +//initialize the AAS routing +void AAS_InitRouting( void ); +//free the AAS routing caches +void AAS_FreeRoutingCaches( void ); +//returns the travel time from start to end in the given area +unsigned short int AAS_AreaTravelTime( int areanum, vec3_t start, vec3_t end ); +// +void AAS_CreateAllRoutingCache( void ); +// +void AAS_RoutingInfo( void ); +#endif //AASINTERN + +//returns the travel flag for the given travel type +int AAS_TravelFlagForType( int traveltype ); +// +int AAS_AreaContentsTravelFlag( int areanum ); +//returns the index of the next reachability for the given area +int AAS_NextAreaReachability( int areanum, int reachnum ); +//returns the reachability with the given index +void AAS_ReachabilityFromNum( int num, struct aas_reachability_s *reach ); +//returns a random goal area and goal origin +int AAS_RandomGoalArea( int areanum, int travelflags, int *goalareanum, vec3_t goalorigin ); +//returns the travel time within the given area from start to end +unsigned short int AAS_AreaTravelTime( int areanum, vec3_t start, vec3_t end ); +//returns the travel time from the area to the goal area using the given travel flags +int AAS_AreaTravelTimeToGoalArea( int areanum, vec3_t origin, int goalareanum, int travelflags ); +//returns the travel time from the area to the goal area using the given travel flags +int AAS_AreaTravelTimeToGoalAreaCheckLoop( int areanum, vec3_t origin, int goalareanum, int travelflags, int loopareanum ); + +extern int BotFuzzyPointReachabilityArea( vec3_t origin ); diff --git a/src/botlib/be_aas_routealt.c b/src/botlib/be_aas_routealt.c new file mode 100644 index 0000000..ee6828e --- /dev/null +++ b/src/botlib/be_aas_routealt.c @@ -0,0 +1,271 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_routealt.c + * + * desc: AAS + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "l_utils.h" +#include "l_memory.h" +#include "l_log.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "aasfile.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "be_aas_funcs.h" +#include "be_interface.h" +#include "be_aas_def.h" + +//#define ENABLE_ALTROUTING + +typedef struct midrangearea_s +{ + int valid; + unsigned short starttime; + unsigned short goaltime; +} midrangearea_t; + +midrangearea_t *midrangeareas; +int *clusterareas; +int numclusterareas; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_AltRoutingFloodCluster_r( int areanum ) { + int i, otherareanum; + aas_area_t *area; + aas_face_t *face; + + //add the current area to the areas of the current cluster + clusterareas[numclusterareas] = areanum; + numclusterareas++; + //remove the area from the mid range areas + midrangeareas[areanum].valid = qfalse; + //flood to other areas through the faces of this area + area = &( *aasworld ).areas[areanum]; + for ( i = 0; i < area->numfaces; i++ ) + { + face = &( *aasworld ).faces[abs( ( *aasworld ).faceindex[area->firstface + i] )]; + //get the area at the other side of the face + if ( face->frontarea == areanum ) { + otherareanum = face->backarea; + } else { otherareanum = face->frontarea;} + //if there is an area at the other side of this face + if ( !otherareanum ) { + continue; + } + //if the other area is not a midrange area + if ( !midrangeareas[otherareanum].valid ) { + continue; + } + // + AAS_AltRoutingFloodCluster_r( otherareanum ); + } //end for +} //end of the function AAS_AltRoutingFloodCluster_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AlternativeRouteGoals( vec3_t start, vec3_t goal, int travelflags, + aas_altroutegoal_t *altroutegoals, int maxaltroutegoals, + int color ) { +#ifndef ENABLE_ALTROUTING + return 0; +#else + int i, j, startareanum, goalareanum, bestareanum; + int numaltroutegoals, nummidrangeareas; + int starttime, goaltime, goaltraveltime; + float dist, bestdist; + vec3_t mid, dir; +#ifdef DEBUG + int startmillisecs; + + startmillisecs = Sys_MilliSeconds(); +#endif + + startareanum = AAS_PointAreaNum( start ); + if ( !startareanum ) { + return 0; + } + goalareanum = AAS_PointAreaNum( goal ); + if ( !goalareanum ) { + return 0; + } + //travel time towards the goal area + goaltraveltime = AAS_AreaTravelTimeToGoalArea( startareanum, start, goalareanum, travelflags ); + //clear the midrange areas + memset( midrangeareas, 0, ( *aasworld ).numareas * sizeof( midrangearea_t ) ); + numaltroutegoals = 0; + // + nummidrangeareas = 0; + // + for ( i = 1; i < ( *aasworld ).numareas; i++ ) + { + // + if ( !( ( *aasworld ).areasettings[i].contents & AREACONTENTS_ROUTEPORTAL ) ) { + continue; + } + //if the area has no reachabilities + if ( !AAS_AreaReachability( i ) ) { + continue; + } + //tavel time from the area to the start area + starttime = AAS_AreaTravelTimeToGoalArea( startareanum, start, i, travelflags ); + if ( !starttime ) { + continue; + } + //if the travel time from the start to the area is greater than the shortest goal travel time + if ( starttime > 1.5 * goaltraveltime ) { + continue; + } + //travel time from the area to the goal area + goaltime = AAS_AreaTravelTimeToGoalArea( i, NULL, goalareanum, travelflags ); + if ( !goaltime ) { + continue; + } + //if the travel time from the area to the goal is greater than the shortest goal travel time + if ( goaltime > 1.5 * goaltraveltime ) { + continue; + } + //this is a mid range area + midrangeareas[i].valid = qtrue; + midrangeareas[i].starttime = starttime; + midrangeareas[i].goaltime = goaltime; + Log_Write( "%d midrange area %d", nummidrangeareas, i ); + nummidrangeareas++; + } //end for + // + for ( i = 1; i < ( *aasworld ).numareas; i++ ) + { + if ( !midrangeareas[i].valid ) { + continue; + } + //get the areas in one cluster + numclusterareas = 0; + AAS_AltRoutingFloodCluster_r( i ); + //now we've got a cluster with areas through which an alternative route could go + //get the 'center' of the cluster + VectorClear( mid ); + for ( j = 0; j < numclusterareas; j++ ) + { + VectorAdd( mid, ( *aasworld ).areas[clusterareas[j]].center, mid ); + } //end for + VectorScale( mid, 1.0 / numclusterareas, mid ); + //get the area closest to the center of the cluster + bestdist = 999999; + bestareanum = 0; + for ( j = 0; j < numclusterareas; j++ ) + { + VectorSubtract( mid, ( *aasworld ).areas[clusterareas[j]].center, dir ); + dist = VectorLength( dir ); + if ( dist < bestdist ) { + bestdist = dist; + bestareanum = clusterareas[j]; + } //end if + } //end for + //now we've got an area for an alternative route + //FIXME: add alternative goal origin + VectorCopy( ( *aasworld ).areas[bestareanum].center, altroutegoals[numaltroutegoals].origin ); + altroutegoals[numaltroutegoals].areanum = bestareanum; + altroutegoals[numaltroutegoals].starttraveltime = midrangeareas[bestareanum].starttime; + altroutegoals[numaltroutegoals].goaltraveltime = midrangeareas[bestareanum].goaltime; + altroutegoals[numaltroutegoals].extratraveltime = + ( midrangeareas[bestareanum].starttime + midrangeareas[bestareanum].goaltime ) - + goaltraveltime; + numaltroutegoals++; + // +#ifdef DEBUG + botimport.Print( PRT_MESSAGE, "alternative route goal area %d, numclusterareas = %d\n", bestareanum, numclusterareas ); + if ( color ) { + AAS_DrawPermanentCross( ( *aasworld ).areas[bestareanum].center, 10, color ); + } //end if + //AAS_ShowArea(bestarea, qtrue); +#endif + //don't return more than the maximum alternative route goals + if ( numaltroutegoals >= maxaltroutegoals ) { + break; + } + } //end for + botimport.Print( PRT_MESSAGE, "%d alternative route goals\n", numaltroutegoals ); +#ifdef DEBUG + botimport.Print( PRT_MESSAGE, "alternative route goals in %d msec\n", Sys_MilliSeconds() - startmillisecs ); +#endif + return numaltroutegoals; +#endif +} //end of the function AAS_AlternativeRouteGoals +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_InitAlternativeRouting( void ) { +#ifdef ENABLE_ALTROUTING + if ( midrangeareas ) { + FreeMemory( midrangeareas ); + } + midrangeareas = (midrangearea_t *) GetMemory( ( *aasworld ).numareas * sizeof( midrangearea_t ) ); + if ( clusterareas ) { + FreeMemory( clusterareas ); + } + clusterareas = (int *) GetMemory( aasworld.numareas * sizeof( int ) ); +#endif +} //end of the function AAS_InitAlternativeRouting +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ShutdownAlternativeRouting( void ) { +#ifdef ENABLE_ALTROUTING + if ( midrangeareas ) { + FreeMemory( midrangeareas ); + } + midrangeareas = NULL; + if ( clusterareas ) { + FreeMemory( clusterareas ); + } + clusterareas = NULL; + numclusterareas = 0; +#endif +} diff --git a/src/botlib/be_aas_routealt.h b/src/botlib/be_aas_routealt.h new file mode 100644 index 0000000..be40784 --- /dev/null +++ b/src/botlib/be_aas_routealt.h @@ -0,0 +1,46 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_routealt.h + * + * desc: AAS + * + * + *****************************************************************************/ + +#ifdef AASINTERN +void AAS_InitAlternativeRouting( void ); +void AAS_ShutdownAlternativeRouting( void ); +#endif //AASINTERN + + +int AAS_AlternativeRouteGoals( vec3_t start, vec3_t goal, int travelflags, + aas_altroutegoal_t *altroutegoals, int maxaltroutegoals, + int color ); diff --git a/src/botlib/be_aas_routetable.c b/src/botlib/be_aas_routetable.c new file mode 100644 index 0000000..70dbb63 --- /dev/null +++ b/src/botlib/be_aas_routetable.c @@ -0,0 +1,1449 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: be_aas_routetable.c +// Function: Area Awareness System, Route-table defines +// Programmer: Ridah +// Tab Size: 3 +//=========================================================================== + +#include "../game/q_shared.h" +#include "l_memory.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "l_libvar.h" +#include "l_utils.h" +#include "aasfile.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "be_interface.h" +#include "be_aas_def.h" + +// ugly hack to turn off route-tables, can't find a way to check cvar's +int disable_routetable = 0; + +// this must be enabled for the route-tables to work, but it's not fully operational yet +#define CHECK_TRAVEL_TIMES +//#define DEBUG_ROUTETABLE +#define FILTERAREAS + +// enable this to use the built-in route-cache system to find the routes +#define USE_ROUTECACHE + +// enable this to disable Rocket/BFG Jumping, Grapple Hook +#define FILTER_TRAVEL + +// hmm, is there a cleaner way of finding out memory usage? +extern int totalmemorysize; +static int memorycount, cachememory; + +// globals to reduce function parameters +static unsigned short int *filtered_areas, childcount, num_parents; +static unsigned short int *rev_filtered_areas; + +// misc defines +unsigned short CRC_ProcessString( unsigned char *data, int length ); + + +//=========================================================================== +// Memory debugging/optimization + +void *AAS_RT_GetClearedMemory( unsigned long size ) { + void *ptr; + + memorycount += size; + + // ptr = GetClearedMemory(size); + //ptr = GetClearedHunkMemory(size); + // Ryan - 01102k, need to use this, since the routetable calculations use up a lot of memory + // this will be a non-issue once we transfer the remnants of the routetable over to the aasworld + ptr = malloc( size ); + memset( ptr, 0, size ); + + return ptr; +} + +void AAS_RT_FreeMemory( void *ptr ) { + int before; + + before = totalmemorysize; + + // FreeMemory( ptr ); + // Ryan - 01102k + free( ptr ); + + memorycount -= before - totalmemorysize; +} + +void AAS_RT_PrintMemoryUsage() { +#ifdef AAS_RT_MEMORY_USAGE + + botimport.Print( PRT_MESSAGE, "\n" ); + + // TODO: print the usage from each of the aas_rt_t lumps + +#endif +} +//=========================================================================== + + +//=========================================================================== +// return the number of unassigned areas that are in the given area's visible list +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_RT_GetValidVisibleAreasCount( aas_area_buildlocalinfo_t *localinfo, aas_area_childlocaldata_t **childlocaldata ) { + int i, cnt; + + cnt = 1; // assume it can reach itself + + for ( i = 0; i < localinfo->numvisible; i++ ) + { + if ( childlocaldata[localinfo->visible[i]] ) { + continue; + } + + cnt++; + } + + return cnt; +} +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +static aas_rt_route_t **routetable; + +int AAS_AreaRouteToGoalArea( int areanum, vec3_t origin, int goalareanum, int travelflags, int *traveltime, int *reachnum ); + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== + +void AAS_RT_CalcTravelTimesToGoalArea( int goalarea ) { + int i; + // TTimo: unused +// static int tfl = TFL_DEFAULT & ~(TFL_JUMPPAD|TFL_ROCKETJUMP|TFL_BFGJUMP|TFL_GRAPPLEHOOK|TFL_DOUBLEJUMP|TFL_RAMPJUMP|TFL_STRAFEJUMP|TFL_LAVA); //----(SA) modified since slime is no longer deadly +// static int tfl = TFL_DEFAULT & ~(TFL_JUMPPAD|TFL_ROCKETJUMP|TFL_BFGJUMP|TFL_GRAPPLEHOOK|TFL_DOUBLEJUMP|TFL_RAMPJUMP|TFL_STRAFEJUMP|TFL_SLIME|TFL_LAVA); + aas_rt_route_t *rt; + int reach, travel; + + for ( i = 0; i < childcount; i++ ) { + rt = &routetable[i][-1 + rev_filtered_areas[goalarea]]; + if ( AAS_AreaRouteToGoalArea( filtered_areas[i], ( *aasworld ).areas[filtered_areas[i]].center, goalarea, ~RTB_BADTRAVELFLAGS, &travel, &reach ) ) { + rt->reachable_index = reach; + rt->travel_time = travel; + } else { + rt->reachable_index = -1; + rt->travel_time = 0; + } + } +} +//=========================================================================== +// calculate the initial route-table for each filtered area to all other areas +// +// FIXME: this isn't fully operational yet, for some reason not all routes are found +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_RT_CalculateRouteTable( aas_rt_route_t **parmroutetable ) { + int i; + + routetable = parmroutetable; + + for ( i = 0; i < childcount; i++ ) + { + AAS_RT_CalcTravelTimesToGoalArea( filtered_areas[i] ); + } +} + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_RT_AddParentLink( aas_area_childlocaldata_t *child, int parentindex, int childindex ) { + aas_parent_link_t *oldparentlink; + + oldparentlink = child->parentlink; + + child->parentlink = (aas_parent_link_t *) AAS_RT_GetClearedMemory( sizeof( aas_parent_link_t ) ); + + child->parentlink->childindex = (unsigned short int)childindex; + child->parentlink->parent = (unsigned short int)parentindex; + child->parentlink->next = oldparentlink; +} + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_RT_WriteShort( unsigned short int si, fileHandle_t fp ) { + unsigned short int lsi; + + lsi = LittleShort( si ); + botimport.FS_Write( &lsi, sizeof( lsi ), fp ); +} + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_RT_WriteByte( int si, fileHandle_t fp ) { + unsigned char uc; + + uc = si; + botimport.FS_Write( &uc, sizeof( uc ), fp ); +} + +//=========================================================================== +// writes the current route-table data to a .rtb file in tne maps folder +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_RT_WriteRouteTable() { + int ident, version; + unsigned short crc_aas; + fileHandle_t fp; + char filename[MAX_QPATH]; + + // open the file for writing + Com_sprintf( filename, MAX_QPATH, "maps/%s.rtb", ( *aasworld ).mapname ); + botimport.Print( PRT_MESSAGE, "\nsaving route-table to %s\n", filename ); + botimport.FS_FOpenFile( filename, &fp, FS_WRITE ); + if ( !fp ) { + AAS_Error( "Unable to open file: %s\n", filename ); + return; + } + + // ident + ident = LittleLong( RTBID ); + botimport.FS_Write( &ident, sizeof( ident ), fp ); + + // version + version = LittleLong( RTBVERSION ); + botimport.FS_Write( &version, sizeof( version ), fp ); + + // crc + crc_aas = CRC_ProcessString( (unsigned char *)( *aasworld ).areas, sizeof( aas_area_t ) * ( *aasworld ).numareas ); + botimport.FS_Write( &crc_aas, sizeof( crc_aas ), fp ); + + // save the table data + + // children + botimport.FS_Write( &( *aasworld ).routetable->numChildren, sizeof( int ), fp ); + botimport.FS_Write( ( *aasworld ).routetable->children, ( *aasworld ).routetable->numChildren * sizeof( aas_rt_child_t ), fp ); + + // parents + botimport.FS_Write( &( *aasworld ).routetable->numParents, sizeof( int ), fp ); + botimport.FS_Write( ( *aasworld ).routetable->parents, ( *aasworld ).routetable->numParents * sizeof( aas_rt_parent_t ), fp ); + + // parentChildren + botimport.FS_Write( &( *aasworld ).routetable->numParentChildren, sizeof( int ), fp ); + botimport.FS_Write( ( *aasworld ).routetable->parentChildren, ( *aasworld ).routetable->numParentChildren * sizeof( unsigned short int ), fp ); + + // visibleParents + botimport.FS_Write( &( *aasworld ).routetable->numVisibleParents, sizeof( int ), fp ); + botimport.FS_Write( ( *aasworld ).routetable->visibleParents, ( *aasworld ).routetable->numVisibleParents * sizeof( unsigned short int ), fp ); + + // parentLinks + botimport.FS_Write( &( *aasworld ).routetable->numParentLinks, sizeof( int ), fp ); + botimport.FS_Write( ( *aasworld ).routetable->parentLinks, ( *aasworld ).routetable->numParentLinks * sizeof( aas_rt_parent_link_t ), fp ); + + botimport.FS_FCloseFile( fp ); + return; +} + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_RT_DBG_Read( void *buf, int size, int fp ) { + botimport.FS_Read( buf, size, fp ); +} + +//=========================================================================== +// reads the given file, and creates the structures required for the route-table system +// +// Parameter: - +// Returns: qtrue if succesful, qfalse if not +// Changes Globals: - +//=========================================================================== +#define DEBUG_READING_TIME +qboolean AAS_RT_ReadRouteTable( fileHandle_t fp ) { + int ident, version, i; + unsigned short int crc, crc_aas; + aas_rt_t *routetable; + aas_rt_child_t *child; + aas_rt_parent_t *parent; + aas_rt_parent_link_t *plink; + unsigned short int *psi; + + qboolean doswap; + +#ifdef DEBUG_READING_TIME + int pretime; + + pretime = Sys_MilliSeconds(); +#endif + + routetable = ( *aasworld ).routetable; + + doswap = ( LittleLong( 1 ) != 1 ); + + // check ident + AAS_RT_DBG_Read( &ident, sizeof( ident ), fp ); + ident = LittleLong( ident ); + + if ( ident != RTBID ) { + AAS_Error( "File is not an RTB file\n" ); + botimport.FS_FCloseFile( fp ); + return qfalse; + } + + // check version + AAS_RT_DBG_Read( &version, sizeof( version ), fp ); + version = LittleLong( version ); + + if ( version != RTBVERSION ) { + AAS_Error( "File is version %i not %i\n", version, RTBVERSION ); + botimport.FS_FCloseFile( fp ); + return qfalse; + } + + // read the CRC check on the AAS data + AAS_RT_DBG_Read( &crc, sizeof( crc ), fp ); + crc = LittleShort( crc ); + + // calculate a CRC on the AAS areas + crc_aas = CRC_ProcessString( (unsigned char *)( *aasworld ).areas, sizeof( aas_area_t ) * ( *aasworld ).numareas ); + + if ( crc != crc_aas ) { + AAS_Error( "Route-table is from different AAS file, ignoring.\n" ); + botimport.FS_FCloseFile( fp ); + return qfalse; + } + + // read the route-table + + // children + botimport.FS_Read( &routetable->numChildren, sizeof( int ), fp ); + routetable->numChildren = LittleLong( routetable->numChildren ); + routetable->children = (aas_rt_child_t *) AAS_RT_GetClearedMemory( routetable->numChildren * sizeof( aas_rt_child_t ) ); + botimport.FS_Read( routetable->children, routetable->numChildren * sizeof( aas_rt_child_t ), fp ); + child = &routetable->children[0]; + if ( doswap ) { + for ( i = 0; i < routetable->numChildren; i++, child++ ) { + child->areanum = LittleShort( child->areanum ); + child->numParentLinks = LittleLong( child->numParentLinks ); + child->startParentLinks = LittleLong( child->startParentLinks ); + } + } + + // parents + botimport.FS_Read( &routetable->numParents, sizeof( int ), fp ); + routetable->numParents = LittleLong( routetable->numParents ); + routetable->parents = (aas_rt_parent_t *) AAS_RT_GetClearedMemory( routetable->numParents * sizeof( aas_rt_parent_t ) ); + botimport.FS_Read( routetable->parents, routetable->numParents * sizeof( aas_rt_parent_t ), fp ); + parent = &routetable->parents[0]; + if ( doswap ) { + for ( i = 0; i < routetable->numParents; i++, parent++ ) { + parent->areanum = LittleShort( parent->areanum ); + parent->numParentChildren = LittleLong( parent->numParentChildren ); + parent->startParentChildren = LittleLong( parent->startParentChildren ); + parent->numVisibleParents = LittleLong( parent->numVisibleParents ); + parent->startVisibleParents = LittleLong( parent->startVisibleParents ); + } + } + + // parentChildren + botimport.FS_Read( &routetable->numParentChildren, sizeof( int ), fp ); + routetable->numParentChildren = LittleLong( routetable->numParentChildren ); + routetable->parentChildren = (unsigned short int *) AAS_RT_GetClearedMemory( routetable->numParentChildren * sizeof( unsigned short int ) ); + botimport.FS_Read( routetable->parentChildren, routetable->numParentChildren * sizeof( unsigned short int ), fp ); + psi = &routetable->parentChildren[0]; + if ( doswap ) { + for ( i = 0; i < routetable->numParentChildren; i++, psi++ ) { + *psi = LittleShort( *psi ); + } + } + + // visibleParents + botimport.FS_Read( &routetable->numVisibleParents, sizeof( int ), fp ); + routetable->numVisibleParents = LittleLong( routetable->numVisibleParents ); + routetable->visibleParents = (unsigned short int *) AAS_RT_GetClearedMemory( routetable->numVisibleParents * sizeof( unsigned short int ) ); + botimport.FS_Read( routetable->visibleParents, routetable->numVisibleParents * sizeof( unsigned short int ), fp ); + psi = &routetable->visibleParents[0]; + if ( doswap ) { + for ( i = 0; i < routetable->numVisibleParents; i++, psi++ ) { + *psi = LittleShort( *psi ); + } + } + + // parentLinks + botimport.FS_Read( &routetable->numParentLinks, sizeof( int ), fp ); + routetable->numParentLinks = LittleLong( routetable->numParentLinks ); + routetable->parentLinks = (aas_rt_parent_link_t *) AAS_RT_GetClearedMemory( routetable->numParentLinks * sizeof( aas_rt_parent_link_t ) ); + botimport.FS_Read( routetable->parentLinks, routetable->numParentLinks * sizeof( aas_parent_link_t ), fp ); + plink = &routetable->parentLinks[0]; + if ( doswap ) { + for ( i = 0; i < routetable->numParentLinks; i++, plink++ ) { + plink->childIndex = LittleShort( plink->childIndex ); + plink->parent = LittleShort( plink->parent ); + } + } + + // build the areaChildIndexes + routetable->areaChildIndexes = (unsigned short int *) AAS_RT_GetClearedMemory( ( *aasworld ).numareas * sizeof( unsigned short int ) ); + child = routetable->children; + for ( i = 0; i < routetable->numChildren; i++, child++ ) { + routetable->areaChildIndexes[child->areanum] = i + 1; + } + + botimport.Print( PRT_MESSAGE, "Total Parents: %d\n", routetable->numParents ); + botimport.Print( PRT_MESSAGE, "Total Children: %d\n", routetable->numChildren ); + botimport.Print( PRT_MESSAGE, "Total Memory Used: %d\n", memorycount ); + +#ifdef DEBUG_READING_TIME + botimport.Print( PRT_MESSAGE, "Route-Table read time: %i\n", Sys_MilliSeconds() - pretime ); +#endif + + botimport.FS_FCloseFile( fp ); + return qtrue; +} + +int AAS_RT_NumParentLinks( aas_area_childlocaldata_t *child ) { + aas_parent_link_t *plink; + int i; + + i = 0; + plink = child->parentlink; + while ( plink ) + { + i++; + plink = plink->next; + } + + return i; +} + +//=========================================================================== +// main routine to build the route-table +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_CreateAllRoutingCache( void ); + +void AAS_RT_BuildRouteTable( void ) { + int i,j,k; + aas_area_t *srcarea; + aas_areasettings_t *srcsettings; +// vec3_t vec; + unsigned int totalcount; + unsigned int noroutecount; + + aas_area_buildlocalinfo_t **area_localinfos; + aas_area_buildlocalinfo_t *localinfo; + + aas_area_childlocaldata_t **area_childlocaldata; + aas_area_childlocaldata_t *child; + + aas_area_parent_t *area_parents[MAX_PARENTS]; + aas_area_parent_t *thisparent; + + int bestchild, bestcount, bestparent, cnt; + + int memoryend; + + unsigned short int *visibleParents; + +#ifdef CHECK_TRAVEL_TIMES + aas_rt_route_t **filteredroutetable; + unsigned short int traveltime; +#endif + + fileHandle_t fp; + char filename[MAX_QPATH]; + +// not used anymore + return; + + // create the routetable in this aasworld + aasworld->routetable = (aas_rt_t *) AAS_RT_GetClearedMemory( sizeof( aas_rt_t ) ); + + // Try to load in a prepared route-table + Com_sprintf( filename, MAX_QPATH, "maps/%s.rtb", ( *aasworld ).mapname ); + botimport.Print( PRT_MESSAGE, "\n---------------------------------\n" ); + botimport.Print( PRT_MESSAGE, "\ntrying to load %s\n", filename ); + botimport.FS_FOpenFile( filename, &fp, FS_READ ); + if ( fp ) { + // read in the table.. + if ( AAS_RT_ReadRouteTable( fp ) ) { + AAS_RT_PrintMemoryUsage(); + + botimport.Print( PRT_MESSAGE, "\nAAS Route-Table loaded.\n" ); + botimport.Print( PRT_MESSAGE, "---------------------------------\n\n" ); + return; + } else + { + botimport.Print( PRT_MESSAGE, "\nUnable to load %s, building route-table..\n", filename ); + } + } else + { + botimport.Print( PRT_MESSAGE, "file not found, building route-table\n\n" ); + } + + + botimport.Print( PRT_MESSAGE, "\n-------------------------------------\nRoute-table memory usage figures..\n\n" ); + + totalcount = 0; + childcount = 0; + noroutecount = 0; + childcount = 0; + num_parents = 0; + + memorycount = 0; + cachememory = 0; + + filtered_areas = (unsigned short int *) AAS_RT_GetClearedMemory( ( *aasworld ).numareas * sizeof( unsigned short int ) ); + rev_filtered_areas = (unsigned short int *) AAS_RT_GetClearedMemory( ( *aasworld ).numareas * sizeof( unsigned short int ) ); + + // to speed things up, build a list of FILTERED areas first + // do this so we can check for filtered areas + AAS_CreateAllRoutingCache(); + for ( i = 0; i < ( *aasworld ).numareas; i++ ) + { + srcarea = &( *aasworld ).areas[i]; + srcsettings = &( *aasworld ).areasettings[i]; + +#ifdef FILTERAREAS + if ( !( srcsettings->areaflags & ( AREA_USEFORROUTING ) ) ) { + continue; + } + if ( !( srcsettings->areaflags & ( AREA_GROUNDED | AREA_LIQUID | AREA_LADDER ) ) ) { + continue; + } +#endif + + rev_filtered_areas[i] = childcount + 1; + filtered_areas[childcount++] = (unsigned short int)i; + } + +#ifdef CHECK_TRAVEL_TIMES + // allocate and calculate the travel times + filteredroutetable = (aas_rt_route_t **) AAS_RT_GetClearedMemory( childcount * sizeof( aas_rt_route_t * ) ); + for ( i = 0; i < childcount; i++ ) + filteredroutetable[i] = (aas_rt_route_t *) AAS_RT_GetClearedMemory( childcount * sizeof( aas_rt_route_t ) ); + + AAS_RT_CalculateRouteTable( filteredroutetable ); + +#endif // CHECK_TRAVEL_TIMES + + // allocate for the temporary build local data + area_localinfos = (aas_area_buildlocalinfo_t **) AAS_RT_GetClearedMemory( childcount * sizeof( aas_area_buildlocalinfo_t * ) ); + + for ( i = 0; i < childcount; i++ ) + { + srcarea = &( *aasworld ).areas[filtered_areas[i]]; + srcsettings = &( *aasworld ).areasettings[filtered_areas[i]]; + + // allocate memory for this area + area_localinfos[i] = (aas_area_buildlocalinfo_t *) AAS_RT_GetClearedMemory( sizeof( aas_area_buildlocalinfo_t ) ); + localinfo = area_localinfos[i]; + + for ( j = 0; j < childcount; j++ ) + { + if ( i == j ) { + continue; + } + +#ifdef CHECK_TRAVEL_TIMES + + // make sure travel time is reasonable + // Get the travel time from i to j + traveltime = (int)filteredroutetable[i][j].travel_time; + + if ( !traveltime ) { + noroutecount++; + continue; + } + if ( traveltime > MAX_LOCALTRAVELTIME ) { + continue; + } + +#endif // CHECK_TRAVEL_TIMES + + // Add it to the list + localinfo->visible[localinfo->numvisible++] = j; + totalcount++; + + if ( localinfo->numvisible >= MAX_VISIBLE_AREAS ) { + botimport.Print( PRT_MESSAGE, "MAX_VISIBLE_AREAS exceeded, lower MAX_VISIBLE_RANGE\n" ); + break; + } + } + } + + // now calculate the best list of locale's + + // allocate for the long-term child data + area_childlocaldata = (aas_area_childlocaldata_t **) AAS_RT_GetClearedMemory( childcount * sizeof( aas_area_childlocaldata_t * ) ); + + for ( i = 0; i < childcount; i++ ) + { + area_childlocaldata[i] = (aas_area_childlocaldata_t *) AAS_RT_GetClearedMemory( sizeof( aas_area_childlocaldata_t ) ); + area_childlocaldata[i]->areanum = filtered_areas[i]; + } + + while ( 1 ) + { + bestchild = -1; + bestcount = 99999; + + // find the area with the least number of visible areas + for ( i = 0; i < childcount; i++ ) + { + if ( area_childlocaldata[i]->parentlink ) { + continue; // already has been allocated to a parent + + } + cnt = AAS_RT_GetValidVisibleAreasCount( area_localinfos[i], area_childlocaldata ); + + if ( cnt < bestcount ) { + bestcount = area_localinfos[i]->numvisible; + bestchild = i; + } + } + + if ( bestchild < 0 ) { + break; // our job is done + + + } + localinfo = area_localinfos[bestchild]; + + + // look through this area's list of visible areas, and pick the one with the most VALID visible areas + bestparent = bestchild; + + for ( i = 0; i < localinfo->numvisible; i++ ) + { + if ( area_childlocaldata[localinfo->visible[i]]->parentlink ) { + continue; // already has been allocated to a parent + + } + // calculate how many of children are valid + cnt = AAS_RT_GetValidVisibleAreasCount( area_localinfos[localinfo->visible[i]], area_childlocaldata ); + + if ( cnt > bestcount ) { + bestcount = cnt; + bestparent = localinfo->visible[i]; + } + } + + // now setup this parent, and assign all it's children + localinfo = area_localinfos[bestparent]; + + // we use all children now, not just valid ones + bestcount = localinfo->numvisible; + + area_parents[num_parents] = (aas_area_parent_t *) AAS_RT_GetClearedMemory( sizeof( aas_area_parent_t ) ); + thisparent = area_parents[num_parents]; + + thisparent->areanum = filtered_areas[bestparent]; + thisparent->children = (unsigned short int *) AAS_RT_GetClearedMemory( ( localinfo->numvisible + 1 ) * sizeof( unsigned short int ) ); + + // first, add itself to the list (yes, a parent is a child of itself) + child = area_childlocaldata[bestparent]; + AAS_RT_AddParentLink( child, num_parents, thisparent->numchildren ); + thisparent->children[thisparent->numchildren++] = filtered_areas[bestparent]; + + // loop around all the parent's visible list, and make them children if they're aren't already assigned to a parent + for ( i = 0; i < localinfo->numvisible; i++ ) + { + // create the childlocaldata + child = area_childlocaldata[localinfo->visible[i]]; + + // Ridah, only one parent per child in the new system + if ( child->parentlink ) { + continue; // already has been allocated to a parent + + } + if ( child->areanum != thisparent->areanum ) { + AAS_RT_AddParentLink( child, num_parents, thisparent->numchildren ); + thisparent->children[thisparent->numchildren++] = filtered_areas[localinfo->visible[i]]; + } + } + + // now setup the list of children and the route-tables + for ( i = 0; i < thisparent->numchildren; i++ ) + { + child = area_childlocaldata[-1 + rev_filtered_areas[thisparent->children[i]]]; + localinfo = area_localinfos[-1 + rev_filtered_areas[thisparent->children[i]]]; + + child->parentlink->routeindexes = (unsigned short int *) AAS_RT_GetClearedMemory( thisparent->numchildren * sizeof( unsigned short int ) ); + + // now setup the indexes + for ( j = 0; j < thisparent->numchildren; j++ ) + { + // find this child in our list of visibles + if ( j == child->parentlink->childindex ) { + continue; + } + + for ( k = 0; k < localinfo->numvisible; k++ ) + { + if ( thisparent->children[j] == filtered_areas[localinfo->visible[k]] ) { // found a match + child->parentlink->routeindexes[j] = (unsigned short int)k; + break; + } + } + + if ( k == localinfo->numvisible ) { // didn't find it, so add it to our list + if ( localinfo->numvisible >= MAX_VISIBLE_AREAS ) { + botimport.Print( PRT_MESSAGE, "MAX_VISIBLE_AREAS exceeded, lower MAX_VISIBLE_RANGE\n" ); + } else + { + localinfo->visible[localinfo->numvisible] = -1 + rev_filtered_areas[thisparent->children[j]]; + child->parentlink->routeindexes[j] = (unsigned short int)localinfo->numvisible; + localinfo->numvisible++; + } + } + } + } + + num_parents++; + } + + // place all the visible areas from each child, into their childlocaldata route-table + for ( i = 0; i < childcount; i++ ) + { + localinfo = area_localinfos[i]; + child = area_childlocaldata[i]; + + child->numlocal = localinfo->numvisible; + child->localroutes = (aas_rt_route_t *) AAS_RT_GetClearedMemory( localinfo->numvisible * sizeof( aas_rt_route_t ) ); + + for ( j = 0; j < localinfo->numvisible; j++ ) + { + child->localroutes[j] = filteredroutetable[i][localinfo->visible[j]]; + } + + child->parentroutes = (aas_rt_route_t *) AAS_RT_GetClearedMemory( num_parents * sizeof( aas_rt_route_t ) ); + + for ( j = 0; j < num_parents; j++ ) + { + child->parentroutes[j] = filteredroutetable[i][-1 + rev_filtered_areas[area_parents[j]->areanum]]; + } + } + + // build the visibleParents lists + visibleParents = (unsigned short int *) AAS_RT_GetClearedMemory( num_parents * sizeof( unsigned short int ) ); + for ( i = 0; i < num_parents; i++ ) + { + area_parents[i]->numVisibleParents = 0; + + for ( j = 0; j < num_parents; j++ ) + { + if ( i == j ) { + continue; + } + + if ( !AAS_inPVS( ( *aasworld ).areas[area_parents[i]->areanum].center, ( *aasworld ).areas[area_parents[j]->areanum].center ) ) { + continue; + } + + visibleParents[area_parents[i]->numVisibleParents] = j; + area_parents[i]->numVisibleParents++; + } + + // now copy the list over to the current src area + area_parents[i]->visibleParents = (unsigned short int *) AAS_RT_GetClearedMemory( area_parents[i]->numVisibleParents * sizeof( unsigned short int ) ); + memcpy( area_parents[i]->visibleParents, visibleParents, area_parents[i]->numVisibleParents * sizeof( unsigned short int ) ); + + } + AAS_RT_FreeMemory( visibleParents ); + + // before we free the main childlocaldata, go through and assign the aas_area's to their appropriate childlocaldata + // this would require modification of the aas_area_t structure, so for now, we'll just place them in a global array, for external reference + +// aasworld->routetable->area_childlocaldata_list = (aas_area_childlocaldata_t **) AAS_RT_GetClearedMemory( (*aasworld).numareas * sizeof(aas_area_childlocaldata_t *) ); +// for (i=0; iroutetable->area_childlocaldata_list[filtered_areas[i]] = area_childlocaldata[i]; +// } + + // copy the list of parents to a global structure for now (should eventually go into the (*aasworld) structure +// aasworld->routetable->area_parents_global = (aas_area_parent_t **) AAS_RT_GetClearedMemory( num_parents * sizeof(aas_area_parent_t *) ); +// memcpy( aasworld->routetable->area_parents_global, area_parents, num_parents * sizeof(aas_area_parent_t *) ); + + // ................................................ + // Convert the data into the correct format + { + aas_rt_t *rt; + aas_rt_child_t *child; + aas_rt_parent_t *parent; + aas_rt_parent_link_t *plink; + unsigned short int *psi; + + aas_area_childlocaldata_t *chloc; + aas_area_parent_t *apar; + aas_parent_link_t *oplink; + + int localRoutesCount, parentRoutesCount, parentChildrenCount, visibleParentsCount, parentLinkCount, routeIndexesCount; + + rt = ( *aasworld ).routetable; + localRoutesCount = 0; + parentRoutesCount = 0; + parentChildrenCount = 0; + visibleParentsCount = 0; + parentLinkCount = 0; + routeIndexesCount = 0; + + // areaChildIndexes + rt->areaChildIndexes = (unsigned short int *) AAS_RT_GetClearedMemory( ( *aasworld ).numareas * sizeof( unsigned short int ) ); + for ( i = 0; i < childcount; i++ ) + { + rt->areaChildIndexes[filtered_areas[i]] = i + 1; + } + + // children + rt->numChildren = childcount; + rt->children = (aas_rt_child_t *) AAS_RT_GetClearedMemory( rt->numChildren * sizeof( aas_rt_child_t ) ); + child = rt->children; + for ( i = 0; i < childcount; i++, child++ ) + { + chloc = area_childlocaldata[i]; + + child->areanum = chloc->areanum; + child->numParentLinks = AAS_RT_NumParentLinks( chloc ); + + child->startParentLinks = parentLinkCount; + + parentLinkCount += child->numParentLinks; + } + + // parents + rt->numParents = num_parents; + rt->parents = (aas_rt_parent_t *) AAS_RT_GetClearedMemory( rt->numParents * sizeof( aas_rt_parent_t ) ); + parent = rt->parents; + for ( i = 0; i < num_parents; i++, parent++ ) + { + apar = area_parents[i]; + + parent->areanum = apar->areanum; + parent->numParentChildren = apar->numchildren; + parent->numVisibleParents = apar->numVisibleParents; + + parent->startParentChildren = parentChildrenCount; + parent->startVisibleParents = visibleParentsCount; + + parentChildrenCount += parent->numParentChildren; + visibleParentsCount += parent->numVisibleParents; + } + + // parentChildren + rt->numParentChildren = parentChildrenCount; + rt->parentChildren = (unsigned short int *) AAS_RT_GetClearedMemory( parentChildrenCount * sizeof( unsigned short int ) ); + psi = rt->parentChildren; + for ( i = 0; i < num_parents; i++ ) + { + apar = area_parents[i]; + for ( j = 0; j < apar->numchildren; j++, psi++ ) + { + *psi = apar->children[j]; + } + } + + // visibleParents + rt->numVisibleParents = visibleParentsCount; + rt->visibleParents = (unsigned short int *) AAS_RT_GetClearedMemory( rt->numVisibleParents * sizeof( unsigned short int ) ); + psi = rt->visibleParents; + for ( i = 0; i < num_parents; i++ ) + { + apar = area_parents[i]; + for ( j = 0; j < apar->numVisibleParents; j++, psi++ ) + { + *psi = apar->visibleParents[j]; + } + } + + // parentLinks + rt->numParentLinks = parentLinkCount; + rt->parentLinks = (aas_rt_parent_link_t *) AAS_RT_GetClearedMemory( parentLinkCount * sizeof( aas_rt_parent_link_t ) ); + plink = rt->parentLinks; + for ( i = 0; i < childcount; i++ ) + { + chloc = area_childlocaldata[i]; + for ( oplink = chloc->parentlink; oplink; plink++, oplink = oplink->next ) + { + plink->childIndex = oplink->childindex; + plink->parent = oplink->parent; + } + } + + } + // ................................................ + + // write the newly created table + AAS_RT_WriteRouteTable(); + + + botimport.Print( PRT_MESSAGE, "Child Areas: %i\nTotal Parents: %i\nAverage VisAreas: %i\n", (int)childcount, num_parents, (int)( childcount / num_parents ) ); + botimport.Print( PRT_MESSAGE, "NoRoute Ratio: %i%%\n", (int)( ( 100.0 * noroutecount ) / ( 1.0 * childcount * childcount ) ) ); + + memoryend = memorycount; + + // clear allocated memory + +// causes crashes in route-caching +//#ifdef USE_ROUTECACHE +// AAS_FreeRoutingCaches(); +//#endif + + for ( i = 0; i < childcount; i++ ) + { + AAS_RT_FreeMemory( area_localinfos[i] ); +#ifdef CHECK_TRAVEL_TIMES + AAS_RT_FreeMemory( filteredroutetable[i] ); +#endif + } + + { + aas_parent_link_t *next, *trav; + + // kill the client areas + for ( i = 0; i < childcount; i++ ) + { + // kill the parent links + next = area_childlocaldata[i]->parentlink; + // TTimo: suggest () around assignment used as truth value + while ( ( trav = next ) ) + { + next = next->next; + + AAS_RT_FreeMemory( trav->routeindexes ); + AAS_RT_FreeMemory( trav ); + } + + AAS_RT_FreeMemory( area_childlocaldata[i]->localroutes ); + AAS_RT_FreeMemory( area_childlocaldata[i]->parentroutes ); + AAS_RT_FreeMemory( area_childlocaldata[i] ); + } + + // kill the parents + for ( i = 0; i < num_parents; i++ ) + { + AAS_RT_FreeMemory( area_parents[i]->children ); + AAS_RT_FreeMemory( area_parents[i]->visibleParents ); + AAS_RT_FreeMemory( area_parents[i] ); + } + } + + AAS_RT_FreeMemory( area_localinfos ); + AAS_RT_FreeMemory( area_childlocaldata ); + AAS_RT_FreeMemory( filtered_areas ); + AAS_RT_FreeMemory( rev_filtered_areas ); +#ifdef CHECK_TRAVEL_TIMES + AAS_RT_FreeMemory( filteredroutetable ); +#endif + + // check how much memory we've used, and intend to keep + AAS_RT_PrintMemoryUsage(); + + botimport.Print( PRT_MESSAGE, "Route-Table Permanent Memory Usage: %i\n", memorycount ); + botimport.Print( PRT_MESSAGE, "Route-Table Calculation Usage: %i\n", memoryend + cachememory ); + botimport.Print( PRT_MESSAGE, "---------------------------------\n" ); +} + +//=========================================================================== +// free permanent memory used by route-table system +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_RT_ShutdownRouteTable( void ) { + if ( !aasworld->routetable ) { + return; + } + + // free the dynamic lists + AAS_RT_FreeMemory( aasworld->routetable->areaChildIndexes ); + AAS_RT_FreeMemory( aasworld->routetable->children ); + AAS_RT_FreeMemory( aasworld->routetable->parents ); + AAS_RT_FreeMemory( aasworld->routetable->parentChildren ); + AAS_RT_FreeMemory( aasworld->routetable->visibleParents ); +// AAS_RT_FreeMemory( aasworld->routetable->localRoutes ); +// AAS_RT_FreeMemory( aasworld->routetable->parentRoutes ); + AAS_RT_FreeMemory( aasworld->routetable->parentLinks ); +// AAS_RT_FreeMemory( aasworld->routetable->routeIndexes ); +// AAS_RT_FreeMemory( aasworld->routetable->parentTravelTimes ); + + // kill the table + AAS_RT_FreeMemory( aasworld->routetable ); + aasworld->routetable = NULL; +} + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +aas_rt_parent_link_t *AAS_RT_GetFirstParentLink( aas_rt_child_t *child ) { + return &aasworld->routetable->parentLinks[child->startParentLinks]; +} + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +aas_rt_child_t *AAS_RT_GetChild( int areanum ) { + int i; + + i = (int)aasworld->routetable->areaChildIndexes[areanum] - 1; + + if ( i >= 0 ) { + return &aasworld->routetable->children[i]; + } else { + return NULL; + } +} + +//=========================================================================== +// returns a route between the areas +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaRouteToGoalArea( int areanum, vec3_t origin, int goalareanum, int travelflags, int *traveltime, int *reachnum ); +aas_rt_route_t *AAS_RT_GetRoute( int srcnum, vec3_t origin, int destnum ) { + #define GETROUTE_NUMROUTES 64 + static aas_rt_route_t routes[GETROUTE_NUMROUTES]; // cycle through these, so we don't overlap + static int routeIndex = 0; + aas_rt_route_t *thisroute; + int reach, traveltime; + aas_rt_t *rt; + static int tfl = TFL_DEFAULT & ~( TFL_JUMPPAD | TFL_ROCKETJUMP | TFL_BFGJUMP | TFL_GRAPPLEHOOK | TFL_DOUBLEJUMP | TFL_RAMPJUMP | TFL_STRAFEJUMP | TFL_LAVA ); //----(SA) modified since slime is no longer deadly +// static int tfl = TFL_DEFAULT & ~(TFL_JUMPPAD|TFL_ROCKETJUMP|TFL_BFGJUMP|TFL_GRAPPLEHOOK|TFL_DOUBLEJUMP|TFL_RAMPJUMP|TFL_STRAFEJUMP|TFL_SLIME|TFL_LAVA); + + if ( !( rt = aasworld->routetable ) ) { // no route table present + return NULL; + } + + if ( disable_routetable ) { + return NULL; + } + + if ( ++routeIndex >= GETROUTE_NUMROUTES ) { + routeIndex = 0; + } + + thisroute = &routes[routeIndex]; + + if ( AAS_AreaRouteToGoalArea( srcnum, origin, destnum, tfl, &traveltime, &reach ) ) { + thisroute->reachable_index = reach; + thisroute->travel_time = traveltime; + return thisroute; + } else { + return NULL; + } +} + +//=========================================================================== +// draws the route-table from src to dest +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#include "../game/be_ai_goal.h" +int BotGetReachabilityToGoal( vec3_t origin, int areanum, int entnum, + int lastgoalareanum, int lastareanum, + int *avoidreach, float *avoidreachtimes, int *avoidreachtries, + bot_goal_t *goal, int travelflags, int movetravelflags ); + +void AAS_RT_ShowRoute( vec3_t srcpos, int srcnum, int destnum ) { +#ifdef DEBUG +#define MAX_RT_AVOID_REACH 1 + AAS_ClearShownPolygons(); + AAS_ClearShownDebugLines(); + AAS_ShowAreaPolygons( srcnum, 1, qtrue ); + AAS_ShowAreaPolygons( destnum, 4, qtrue ); + { + static int lastgoalareanum, lastareanum; + static int avoidreach[MAX_RT_AVOID_REACH]; + static float avoidreachtimes[MAX_RT_AVOID_REACH]; + static int avoidreachtries[MAX_RT_AVOID_REACH]; + int reachnum; + bot_goal_t goal; + aas_reachability_t reach; + + goal.areanum = destnum; + VectorCopy( botlibglobals.goalorigin, goal.origin ); + reachnum = BotGetReachabilityToGoal( srcpos, srcnum, -1, + lastgoalareanum, lastareanum, + avoidreach, avoidreachtimes, avoidreachtries, + &goal, TFL_DEFAULT | TFL_FUNCBOB, TFL_DEFAULT | TFL_FUNCBOB ); + AAS_ReachabilityFromNum( reachnum, &reach ); + AAS_ShowReachability( &reach ); + } +#endif +} + +/* +================= +AAS_RT_GetHidePos + + "src" is hiding ent, "dest" is the enemy +================= +*/ +int AAS_NearestHideArea( int srcnum, vec3_t origin, int areanum, int enemynum, vec3_t enemyorigin, int enemyareanum, int travelflags ); +qboolean AAS_RT_GetHidePos( vec3_t srcpos, int srcnum, int srcarea, vec3_t destpos, int destnum, int destarea, vec3_t returnPos ) { + static int tfl = TFL_DEFAULT & ~( TFL_JUMPPAD | TFL_ROCKETJUMP | TFL_BFGJUMP | TFL_GRAPPLEHOOK | TFL_DOUBLEJUMP | TFL_RAMPJUMP | TFL_STRAFEJUMP | TFL_LAVA ); //----(SA) modified since slime is no longer deadly +// static int tfl = TFL_DEFAULT & ~(TFL_JUMPPAD|TFL_ROCKETJUMP|TFL_BFGJUMP|TFL_GRAPPLEHOOK|TFL_DOUBLEJUMP|TFL_RAMPJUMP|TFL_STRAFEJUMP|TFL_SLIME|TFL_LAVA); + +#if 1 + // use MrE's breadth first method + int hideareanum; +// int pretime; + + // disabled this so grenade hiding works + //if (!srcarea || !destarea) + // return qfalse; + +// pretime = -Sys_MilliSeconds(); + + hideareanum = AAS_NearestHideArea( srcnum, srcpos, srcarea, destnum, destpos, destarea, tfl ); + if ( !hideareanum ) { +// botimport.Print(PRT_MESSAGE, "Breadth First HidePos FAILED: %i ms\n", pretime + Sys_MilliSeconds()); + return qfalse; + } + // we found a valid hiding area + VectorCopy( ( *aasworld ).areawaypoints[hideareanum], returnPos ); + +// botimport.Print(PRT_MESSAGE, "Breadth First HidePos: %i ms\n", pretime + Sys_MilliSeconds()); + + return qtrue; + +#else + // look around at random parent areas, if any of them have a center point + // that isn't visible from "destpos", then return it's position in returnPos + + int i, j, pathArea, dir; + unsigned short int destTravelTime; + aas_rt_parent_t *srcParent, *travParent, *destParent; + aas_rt_child_t *srcChild, *destChild, *travChild; + aas_rt_route_t *route; + vec3_t destVec; + float destTravelDist; + static float lastTime; + static int frameCount, maxPerFrame = 2; + int firstreach; + aas_reachability_t *reachability, *reach; + qboolean startVisible; + unsigned short int bestTravelTime, thisTravelTime, elapsedTravelTime; + #define MAX_HIDE_TRAVELTIME 1000 // 10 seconds is a very long way away + unsigned char destVisLookup[MAX_PARENTS]; + unsigned short int *destVisTrav; + + aas_rt_t *rt; + + const int MAX_CHECK_VISPARENTS = 100; + int visparents_count, total_parents_checked; + int thisParentIndex; + int pretime; + + if ( !( rt = aasworld->routetable ) ) { // no route table present + return qfalse; + } +/* + if (lastTime > (AAS_Time() - 0.1)) { + if (frameCount++ > maxPerFrame) { + return qfalse; + } + } else { + frameCount = 0; + lastTime = AAS_Time(); + } +*/ + pretime = -Sys_MilliSeconds(); + + // is the src area grounded? + if ( !( srcChild = AAS_RT_GetChild( srcarea ) ) ) { + return qfalse; + } + // does it have a parent? +// all valid areas have a parent +// if (!srcChild->numParentLinks) { +// return qfalse; +// } + // get the dest (enemy) area + if ( !( destChild = AAS_RT_GetChild( destarea ) ) ) { + return qfalse; + } + destParent = &rt->parents[ rt->parentLinks[destChild->startParentLinks].parent ]; + // + // populate the destVisAreas + memset( destVisLookup, 0, sizeof( destVisLookup ) ); + destVisTrav = rt->visibleParents + destParent->startVisibleParents; + for ( i = 0; i < destParent->numVisibleParents; i++, destVisTrav++ ) { + destVisLookup[*destVisTrav] = 1; + } + // + // use the first parent to source the vis areas from + srcParent = &rt->parents[ rt->parentLinks[srcChild->startParentLinks].parent ]; + // + // set the destTravelTime + if ( route = AAS_RT_GetRoute( srcarea, srcpos, destarea ) ) { + destTravelTime = route->travel_time; + } else { + destTravelTime = 0; + } + bestTravelTime = MAX_HIDE_TRAVELTIME; // ignore any routes longer than 10 seconds away + // set the destVec + VectorSubtract( destpos, srcpos, destVec ); + destTravelDist = VectorNormalize( destVec ); + // + // randomize the direction we traverse the list, so the hiding spot isn't always the same + if ( rand() % 2 ) { + dir = 1; // forward + } else { + dir = -1; // reverse + } + // randomize the starting area + if ( srcParent->numVisibleParents ) { + i = rand() % srcParent->numVisibleParents; + } else { // prevent divide by zero + i = 0; + } + // + // setup misc stuff + reachability = ( *aasworld ).reachability; + startVisible = botimport.AICast_VisibleFromPos( destpos, destnum, srcpos, srcnum, qfalse ); + // + // set the firstreach to prevent having to do an array and pointer lookup for each destination + firstreach = ( *aasworld ).areasettings[srcarea].firstreachablearea; + // + // just check random parent areas, traversing the route until we find an area that can't be seen from the dest area + for ( visparents_count = 0, total_parents_checked = 0; visparents_count < MAX_CHECK_VISPARENTS && total_parents_checked < rt->numParents; total_parents_checked++ ) { + thisParentIndex = rand() % rt->numParents; + travParent = &rt->parents[ thisParentIndex ]; + // + // never go to the enemy's areas + if ( travParent->areanum == destarea ) { + continue; + } + // + // if it's visible from dest, ignore it + if ( destVisLookup[thisParentIndex] ) { + continue; + } + // + visparents_count++; + // they might be visible, check to see if the path to the area, takes us towards the + // enemy we are trying to hide from + { + qboolean invalidRoute; + vec3_t curPos, lastVec; + #define GETHIDE_MAX_CHECK_PATHS 15 + // + invalidRoute = qfalse; + // initialize the pathArea + pathArea = srcarea; + VectorCopy( srcpos, curPos ); + // now evaluate the path + for ( j = 0; j < GETHIDE_MAX_CHECK_PATHS; j++ ) { + // get the reachability to the travParent + if ( !( route = AAS_RT_GetRoute( pathArea, curPos, travParent->areanum ) ) ) { + // we can't get to the travParent, so don't bother checking it + invalidRoute = qtrue; + break; + } + // set the pathArea + reach = &reachability[route->reachable_index]; + // how far have we travelled so far? + elapsedTravelTime = AAS_AreaTravelTimeToGoalArea( pathArea, curPos, reach->areanum, tfl ); + // add the travel to the center of the area + elapsedTravelTime += AAS_AreaTravelTime( reach->areanum, reach->end, ( *aasworld ).areas[reach->areanum].center ); + // have we gone too far already? + if ( elapsedTravelTime > bestTravelTime ) { + invalidRoute = qtrue; + break; + } else { + thisTravelTime = route->travel_time; + } + // + // if this travel would have us do something wierd + if ( ( reach->traveltype == TRAVEL_WALKOFFLEDGE ) && ( reach->traveltime > 500 ) ) { + invalidRoute = qtrue; + break; + } + // + pathArea = reach->areanum; + VectorCopy( reach->end, curPos ); + // + // if this moves us into the enemies area, skip it + if ( pathArea == destarea ) { + invalidRoute = qtrue; + break; + } + // if we are very close, don't get any closer under any circumstances + { + vec3_t vec; + float dist; + // + VectorSubtract( destpos, reachability[firstreach + route->reachable_index].end, vec ); + dist = VectorNormalize( vec ); + // + if ( destTravelTime < 400 ) { + if ( dist < destTravelDist ) { + invalidRoute = qtrue; + break; + } + if ( DotProduct( destVec, vec ) < 0.2 ) { + invalidRoute = qtrue; + break; + } + } else { + if ( dist < destTravelDist * 0.7 ) { + invalidRoute = qtrue; + break; + } + } + // + // check the directions to make sure we're not trying to run through them + if ( j > 0 ) { + if ( DotProduct( vec, lastVec ) < 0.2 ) { + invalidRoute = qtrue; + break; + } + } else if ( DotProduct( destVec, vec ) < 0.2 ) { + invalidRoute = qtrue; + break; + } + // + VectorCopy( vec, lastVec ); + } + // + // if this area isn't in the visible list for the enemy's area, it's a good hiding spot + if ( !( travChild = AAS_RT_GetChild( pathArea ) ) ) { + invalidRoute = qtrue; + break; + } + if ( !destVisLookup[rt->parentLinks[travChild->startParentLinks].parent] ) { + // success ? + if ( !botimport.AICast_VisibleFromPos( destpos, destnum, ( *aasworld ).areas[pathArea].center, srcnum, qfalse ) ) { + // SUCESS !! + travParent = &rt->parents[rt->parentLinks[travChild->startParentLinks].parent]; + break; + } + } else { + // if we weren't visible when starting, make sure we don't move into their view + if ( !startVisible ) { //botimport.AICast_VisibleFromPos( destpos, destnum, reachability[firstreach + route->reachable_index].end, srcnum, qfalse )) { + invalidRoute = qtrue; + break; + } + } + // + // if this is the travParent, then stop checking + if ( pathArea == travParent->areanum ) { + invalidRoute = qtrue; // we didn't find a hiding spot + break; + } + } // end for areas in route + // + // if the route is invalid, skip this travParent + if ( invalidRoute ) { + continue; + } + } + // + // now last of all, check that this area is a safe hiding spot +// if (botimport.AICast_VisibleFromPos( destpos, destnum, (*aasworld).areas[travParent->areanum].center, srcnum, qfalse )) { +// continue; +// } + // + // we've found a good hiding spot, so use it + VectorCopy( ( *aasworld ).areas[travParent->areanum].center, returnPos ); + bestTravelTime = elapsedTravelTime; + // + if ( thisTravelTime < 300 ) { + botimport.Print( PRT_MESSAGE, "Fuzzy RT HidePos: %i ms\n", pretime + Sys_MilliSeconds() ); + return qtrue; + } + } + // + // did we find something? + if ( bestTravelTime < MAX_HIDE_TRAVELTIME ) { + botimport.Print( PRT_MESSAGE, "Fuzzy RT HidePos: %i ms\n", pretime + Sys_MilliSeconds() ); + return qtrue; + } + // + // couldn't find anything + botimport.Print( PRT_MESSAGE, "Fuzzy RT HidePos FAILED: %i ms\n", pretime + Sys_MilliSeconds() ); + return qfalse; +#endif +} + +/* +================= +AAS_RT_GetReachabilityIndex +================= +*/ +int AAS_RT_GetReachabilityIndex( int areanum, int reachIndex ) { +// return (*aasworld).areasettings[areanum].firstreachablearea + reachIndex; + return reachIndex; +} diff --git a/src/botlib/be_aas_routetable.h b/src/botlib/be_aas_routetable.h new file mode 100644 index 0000000..dd69ac1 --- /dev/null +++ b/src/botlib/be_aas_routetable.h @@ -0,0 +1,171 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: be_aas_routetable.h +// Function: Area Awareness System, Route-table defines +// Programmer: Ridah +// Tab Size: 3 +//=========================================================================== + +#ifndef RT_DEFINED + +#define RT_DEFINED + +#define RTBID ( ( 'B' << 24 ) + ( 'T' << 16 ) + ( 'R' << 8 ) + 'X' ) +#define RTBVERSION 17 + +#define RTB_BADTRAVELFLAGS ( TFL_JUMPPAD | TFL_ROCKETJUMP | TFL_BFGJUMP | TFL_GRAPPLEHOOK | TFL_DOUBLEJUMP | TFL_RAMPJUMP | TFL_STRAFEJUMP | TFL_LAVA ) //----(SA) modified since slime is no longer deadly +//#define RTB_BADTRAVELFLAGS (TFL_JUMPPAD|TFL_ROCKETJUMP|TFL_BFGJUMP|TFL_GRAPPLEHOOK|TFL_DOUBLEJUMP|TFL_RAMPJUMP|TFL_STRAFEJUMP|TFL_SLIME|TFL_LAVA) + +#define MAX_VISIBLE_AREAS 1024 // going over this limit will result in excessive memory usage, try and keep RANGE low enough so this limit won't be reached +#define MAX_LOCALTRAVELTIME 60 // use this to tweak memory usage (reduces parent count, increases local count (and cpu usage) - find a balance) +#define MAX_PARENTS 8192 + +extern int disable_routetable; + +//.................................................................... +// Permanent structures (in order of highest to lowest count) +typedef struct +{ + unsigned short int reachable_index; // reachability index (from this area's first reachability link in the world) to head for to get to the destination + unsigned short int travel_time; // travel time (!) +} aas_rt_route_t; + +typedef struct +{ + unsigned short int parent; // parent we belong to + unsigned short int childIndex; // our index in the parent's list of children +// unsigned short int numRouteIndexes; +// int startRouteIndexes; +} aas_rt_parent_link_t; + +typedef struct +{ + unsigned short int areanum; +// int numLocalRoutes; +// int startLocalRoutes; +// int numParentRoutes; +// int startParentRoutes; + int numParentLinks; + int startParentLinks; +} aas_rt_child_t; + +typedef struct +{ + unsigned short int areanum; // out area number in the global list + int numParentChildren; + int startParentChildren; + int numVisibleParents; + int startVisibleParents; // list of other parents that we can see (used for fast hide/retreat checks) +// int startParentTravelTimes; +} aas_rt_parent_t; + +// this is what each aasworld attaches itself to +typedef struct +{ + unsigned short int *areaChildIndexes; // each aas area that is part of the Route-Table has a pointer here to their position in the list of children + + int numChildren; + aas_rt_child_t *children; + + int numParents; + aas_rt_parent_t *parents; + + int numParentChildren; + unsigned short int *parentChildren; + + int numVisibleParents; + unsigned short int *visibleParents; + +// int numLocalRoutes; +// aas_rt_route_t *localRoutes; // the list of routes to all other local areas + +// int numParentRoutes; +// unsigned char *parentRoutes; // reachability to each other parent, as an offset from our first reachability + + int numParentLinks; + aas_rt_parent_link_t *parentLinks; // links from each child to the parent's it belongs to + +// int numParentTravelTimes; +// unsigned short int *parentTravelTimes; // travel times between all parent areas + +// int numRouteIndexes; +// unsigned short int *routeIndexes; // each parentLink has a list within here, which + // contains the local indexes of each child that + // belongs to the parent, within the source child's + // localroutes +} aas_rt_t; + +//.................................................................... +// Temp structures used only during route-table contruction +typedef struct +{ + unsigned short int numvisible; // number of areas that are visible and within range + unsigned short int + visible[MAX_VISIBLE_AREAS]; // list of area indexes of visible and within range areas +} aas_area_buildlocalinfo_t; + +typedef struct aas_parent_link_s +{ + unsigned short int parent; // parent we belong to + unsigned short int childindex; // our index in the parent's list of children + unsigned short int *routeindexes; // for this parent link, list the children that fall under that parent, and their associated indexes in our localroutes table + struct aas_parent_link_s *next; +} aas_parent_link_t; + +typedef struct +{ + unsigned short int areanum; + unsigned short int numlocal; + aas_parent_link_t *parentlink; // linked list of parents that we belong to + aas_rt_route_t *localroutes; // the list of routes to all other local areas + aas_rt_route_t *parentroutes; // the list of routes to all other parent areas +} aas_area_childlocaldata_t; + +typedef struct +{ + unsigned short int areanum; // out area number in the global list + unsigned short int numchildren; + unsigned short int *children; + unsigned short int numVisibleParents; + unsigned short int *visibleParents; // list of other parents that we can see (used for fast hide/retreat checks) +} aas_area_parent_t; + +#endif // RT_DEFINED + +//.................................................................... + +void AAS_RT_BuildRouteTable( void ); +void AAS_RT_ShowRoute( vec3_t srcpos, int srcnum, int destnum ); +aas_rt_route_t *AAS_RT_GetRoute( int srcnum, vec3_t origin, int destnum ); +void AAS_RT_ShutdownRouteTable( void ); +qboolean AAS_RT_GetHidePos( vec3_t srcpos, int srcnum, int srcarea, vec3_t destpos, int destnum, int destarea, vec3_t returnPos ); +int AAS_RT_GetReachabilityIndex( int areanum, int reachIndex ); + diff --git a/src/botlib/be_aas_sample.c b/src/botlib/be_aas_sample.c new file mode 100644 index 0000000..faa3b4b --- /dev/null +++ b/src/botlib/be_aas_sample.c @@ -0,0 +1,1274 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_sample.c + * + * desc: AAS environment sampling + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "l_memory.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "aasfile.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "be_aas_funcs.h" +#include "be_aas_def.h" + +extern botlib_import_t botimport; + +//#define AAS_SAMPLE_DEBUG + +#define BBOX_NORMAL_EPSILON 0.001 + +#define ON_EPSILON 0 //0.0005 + +#define TRACEPLANE_EPSILON 0.125 + +typedef struct aas_tracestack_s +{ + vec3_t start; //start point of the piece of line to trace + vec3_t end; //end point of the piece of line to trace + int planenum; //last plane used as splitter + int nodenum; //node found after splitting with planenum +} aas_tracestack_t; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_PresenceTypeBoundingBox( int presencetype, vec3_t mins, vec3_t maxs ) { + int index; + //bounding box size for each presence type + vec3_t boxmins[3] = {{0, 0, 0}, {-15, -15, -24}, {-15, -15, -24}}; + vec3_t boxmaxs[3] = {{0, 0, 0}, { 15, 15, 32}, { 15, 15, 8}}; + + if ( presencetype == PRESENCE_NORMAL ) { + index = 1; + } else if ( presencetype == PRESENCE_CROUCH ) { + index = 2; + } else + { + botimport.Print( PRT_FATAL, "AAS_PresenceTypeBoundingBox: unknown presence type\n" ); + index = 2; + } //end if + VectorCopy( boxmins[index], mins ); + VectorCopy( boxmaxs[index], maxs ); +} //end of the function AAS_PresenceTypeBoundingBox +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_InitAASLinkHeap( void ) { + int i, max_aaslinks; + + max_aaslinks = ( *aasworld ).linkheapsize; + //if there's no link heap present + if ( !( *aasworld ).linkheap ) { + max_aaslinks = (int) 4096; //LibVarValue("max_aaslinks", "4096"); + if ( max_aaslinks < 0 ) { + max_aaslinks = 0; + } + ( *aasworld ).linkheapsize = max_aaslinks; + ( *aasworld ).linkheap = (aas_link_t *) GetHunkMemory( max_aaslinks * sizeof( aas_link_t ) ); + } //end if + //link the links on the heap + ( *aasworld ).linkheap[0].prev_ent = NULL; + ( *aasworld ).linkheap[0].next_ent = &( *aasworld ).linkheap[1]; + for ( i = 1; i < max_aaslinks - 1; i++ ) + { + ( *aasworld ).linkheap[i].prev_ent = &( *aasworld ).linkheap[i - 1]; + ( *aasworld ).linkheap[i].next_ent = &( *aasworld ).linkheap[i + 1]; + } //end for + ( *aasworld ).linkheap[max_aaslinks - 1].prev_ent = &( *aasworld ).linkheap[max_aaslinks - 2]; + ( *aasworld ).linkheap[max_aaslinks - 1].next_ent = NULL; + //pointer to the first free link + ( *aasworld ).freelinks = &( *aasworld ).linkheap[0]; +} //end of the function AAS_InitAASLinkHeap +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FreeAASLinkHeap( void ) { + if ( ( *aasworld ).linkheap ) { + FreeMemory( ( *aasworld ).linkheap ); + } + ( *aasworld ).linkheap = NULL; + ( *aasworld ).linkheapsize = 0; +} //end of the function AAS_FreeAASLinkHeap +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +aas_link_t *AAS_AllocAASLink( void ) { + aas_link_t *link; + + link = ( *aasworld ).freelinks; + if ( !link ) { + botimport.Print( PRT_FATAL, "empty aas link heap\n" ); + return NULL; + } //end if + if ( ( *aasworld ).freelinks ) { + ( *aasworld ).freelinks = ( *aasworld ).freelinks->next_ent; + } + if ( ( *aasworld ).freelinks ) { + ( *aasworld ).freelinks->prev_ent = NULL; + } + return link; +} //end of the function AAS_AllocAASLink +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_DeAllocAASLink( aas_link_t *link ) { + if ( ( *aasworld ).freelinks ) { + ( *aasworld ).freelinks->prev_ent = link; + } + link->prev_ent = NULL; + link->next_ent = ( *aasworld ).freelinks; + link->prev_area = NULL; + link->next_area = NULL; + ( *aasworld ).freelinks = link; +} //end of the function AAS_DeAllocAASLink +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_InitAASLinkedEntities( void ) { + if ( !( *aasworld ).loaded ) { + return; + } + if ( ( *aasworld ).arealinkedentities ) { + FreeMemory( ( *aasworld ).arealinkedentities ); + } + ( *aasworld ).arealinkedentities = (aas_link_t **) GetClearedHunkMemory( + ( *aasworld ).numareas * sizeof( aas_link_t * ) ); +} //end of the function AAS_InitAASLinkedEntities +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FreeAASLinkedEntities( void ) { + if ( ( *aasworld ).arealinkedentities ) { + FreeMemory( ( *aasworld ).arealinkedentities ); + } + ( *aasworld ).arealinkedentities = NULL; +} //end of the function AAS_InitAASLinkedEntities +//=========================================================================== +// returns the AAS area the point is in +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_PointAreaNum( vec3_t point ) { + int nodenum; + vec_t dist; + aas_node_t *node; + aas_plane_t *plane; + + if ( !( *aasworld ).loaded ) { + botimport.Print( PRT_ERROR, "AAS_PointAreaNum: aas not loaded\n" ); + return 0; + } //end if + + //start with node 1 because node zero is a dummy used for solid leafs + nodenum = 1; + while ( nodenum > 0 ) + { +// botimport.Print(PRT_MESSAGE, "[%d]", nodenum); +#ifdef AAS_SAMPLE_DEBUG + if ( nodenum >= ( *aasworld ).numnodes ) { + botimport.Print( PRT_ERROR, "nodenum = %d >= (*aasworld).numnodes = %d\n", nodenum, ( *aasworld ).numnodes ); + return 0; + } //end if +#endif //AAS_SAMPLE_DEBUG + node = &( *aasworld ).nodes[nodenum]; +#ifdef AAS_SAMPLE_DEBUG + if ( node->planenum < 0 || node->planenum >= ( *aasworld ).numplanes ) { + botimport.Print( PRT_ERROR, "node->planenum = %d >= (*aasworld).numplanes = %d\n", node->planenum, ( *aasworld ).numplanes ); + return 0; + } //end if +#endif //AAS_SAMPLE_DEBUG + plane = &( *aasworld ).planes[node->planenum]; + dist = DotProduct( point, plane->normal ) - plane->dist; + if ( dist > 0 ) { + nodenum = node->children[0]; + } else { nodenum = node->children[1];} + } //end while + if ( !nodenum ) { +#ifdef AAS_SAMPLE_DEBUG + botimport.Print( PRT_MESSAGE, "in solid\n" ); +#endif //AAS_SAMPLE_DEBUG + return 0; + } //end if + return -nodenum; +} //end of the function AAS_PointAreaNum +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaCluster( int areanum ) { + if ( areanum <= 0 || areanum >= ( *aasworld ).numareas ) { + botimport.Print( PRT_ERROR, "AAS_AreaCluster: invalid area number\n" ); + return 0; + } //end if + return ( *aasworld ).areasettings[areanum].cluster; +} //end of the function AAS_AreaCluster +//=========================================================================== +// returns the presence types of the given area +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaPresenceType( int areanum ) { + if ( !( *aasworld ).loaded ) { + return 0; + } + if ( areanum <= 0 || areanum >= ( *aasworld ).numareas ) { + botimport.Print( PRT_ERROR, "AAS_AreaPresenceType: invalid area number\n" ); + return 0; + } //end if + return ( *aasworld ).areasettings[areanum].presencetype; +} //end of the function AAS_AreaPresenceType +//=========================================================================== +// returns the presence type at the given point +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_PointPresenceType( vec3_t point ) { + int areanum; + + if ( !( *aasworld ).loaded ) { + return 0; + } + + areanum = AAS_PointAreaNum( point ); + if ( !areanum ) { + return PRESENCE_NONE; + } + return ( *aasworld ).areasettings[areanum].presencetype; +} //end of the function AAS_PointPresenceType +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_AreaEntityCollision( int areanum, vec3_t start, vec3_t end, + int presencetype, int passent, aas_trace_t *trace ) { + int collision; + vec3_t boxmins, boxmaxs; + aas_link_t *link; + bsp_trace_t bsptrace; + + AAS_PresenceTypeBoundingBox( presencetype, boxmins, boxmaxs ); + + memset( &bsptrace, 0, sizeof( bsp_trace_t ) ); //make compiler happy + //assume no collision + bsptrace.fraction = 1; + collision = qfalse; + for ( link = ( *aasworld ).arealinkedentities[areanum]; link; link = link->next_ent ) + { + //ignore the pass entity + if ( link->entnum == passent ) { + continue; + } + // + if ( AAS_EntityCollision( link->entnum, start, boxmins, boxmaxs, end, + CONTENTS_SOLID | CONTENTS_PLAYERCLIP, &bsptrace ) ) { + collision = qtrue; + } //end if + } //end for + if ( collision ) { + trace->startsolid = bsptrace.startsolid; + trace->ent = bsptrace.ent; + VectorCopy( bsptrace.endpos, trace->endpos ); + trace->area = 0; + trace->planenum = 0; + return qtrue; + } //end if + return qfalse; +} //end of the function AAS_AreaEntityCollision +//=========================================================================== +// recursive subdivision of the line by the BSP tree. +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +aas_trace_t AAS_TraceClientBBox( vec3_t start, vec3_t end, int presencetype, + int passent ) { + int side, nodenum, tmpplanenum; + float front, back, frac; + vec3_t cur_start, cur_end, cur_mid, v1, v2; + aas_tracestack_t tracestack[127]; + aas_tracestack_t *tstack_p; + aas_node_t *aasnode; + aas_plane_t *plane; + aas_trace_t trace; + + //clear the trace structure + memset( &trace, 0, sizeof( aas_trace_t ) ); + + if ( !( *aasworld ).loaded ) { + return trace; + } + + tstack_p = tracestack; + //we start with the whole line on the stack + VectorCopy( start, tstack_p->start ); + VectorCopy( end, tstack_p->end ); + tstack_p->planenum = 0; + //start with node 1 because node zero is a dummy for a solid leaf + tstack_p->nodenum = 1; //starting at the root of the tree + tstack_p++; + + while ( 1 ) + { + //pop up the stack + tstack_p--; + //if the trace stack is empty (ended up with a piece of the + //line to be traced in an area) + if ( tstack_p < tracestack ) { + tstack_p++; + //nothing was hit + trace.startsolid = qfalse; + trace.fraction = 1.0; + //endpos is the end of the line + VectorCopy( end, trace.endpos ); + //nothing hit + trace.ent = 0; + trace.area = 0; + trace.planenum = 0; + return trace; + } //end if + //number of the current node to test the line against + nodenum = tstack_p->nodenum; + //if it is an area + if ( nodenum < 0 ) { +#ifdef AAS_SAMPLE_DEBUG + if ( -nodenum > ( *aasworld ).numareasettings ) { + botimport.Print( PRT_ERROR, "AAS_TraceBoundingBox: -nodenum out of range\n" ); + return trace; + } //end if +#endif //AAS_SAMPLE_DEBUG + //botimport.Print(PRT_MESSAGE, "areanum = %d, must be %d\n", -nodenum, AAS_PointAreaNum(start)); + //if can't enter the area because it hasn't got the right presence type + if ( !( ( *aasworld ).areasettings[-nodenum].presencetype & presencetype ) ) { + //if the start point is still the initial start point + //NOTE: no need for epsilons because the points will be + //exactly the same when they're both the start point + if ( tstack_p->start[0] == start[0] && + tstack_p->start[1] == start[1] && + tstack_p->start[2] == start[2] ) { + trace.startsolid = qtrue; + trace.fraction = 0.0; + } //end if + else + { + trace.startsolid = qfalse; + VectorSubtract( end, start, v1 ); + VectorSubtract( tstack_p->start, start, v2 ); + trace.fraction = VectorLength( v2 ) / VectorNormalize( v1 ); + VectorMA( tstack_p->start, -0.125, v1, tstack_p->start ); + } //end else + VectorCopy( tstack_p->start, trace.endpos ); + trace.ent = 0; + trace.area = -nodenum; +// VectorSubtract(end, start, v1); + trace.planenum = tstack_p->planenum; + //always take the plane with normal facing towards the trace start + plane = &( *aasworld ).planes[trace.planenum]; + if ( DotProduct( v1, plane->normal ) > 0 ) { + trace.planenum ^= 1; + } + return trace; + } //end if + else + { + if ( passent >= 0 ) { + if ( AAS_AreaEntityCollision( -nodenum, tstack_p->start, + tstack_p->end, presencetype, passent, + &trace ) ) { + if ( !trace.startsolid ) { + VectorSubtract( end, start, v1 ); + VectorSubtract( trace.endpos, start, v2 ); + trace.fraction = VectorLength( v2 ) / VectorLength( v1 ); + } //end if + return trace; + } //end if + } //end if + } //end else + trace.lastarea = -nodenum; + continue; + } //end if + //if it is a solid leaf + if ( !nodenum ) { + //if the start point is still the initial start point + //NOTE: no need for epsilons because the points will be + //exactly the same when they're both the start point + if ( tstack_p->start[0] == start[0] && + tstack_p->start[1] == start[1] && + tstack_p->start[2] == start[2] ) { + trace.startsolid = qtrue; + trace.fraction = 0.0; + } //end if + else + { + trace.startsolid = qfalse; + VectorSubtract( end, start, v1 ); + VectorSubtract( tstack_p->start, start, v2 ); + trace.fraction = VectorLength( v2 ) / VectorNormalize( v1 ); + VectorMA( tstack_p->start, -0.125, v1, tstack_p->start ); + } //end else + VectorCopy( tstack_p->start, trace.endpos ); + trace.ent = 0; + trace.area = 0; //hit solid leaf +// VectorSubtract(end, start, v1); + trace.planenum = tstack_p->planenum; + //always take the plane with normal facing towards the trace start + plane = &( *aasworld ).planes[trace.planenum]; + if ( DotProduct( v1, plane->normal ) > 0 ) { + trace.planenum ^= 1; + } + return trace; + } //end if +#ifdef AAS_SAMPLE_DEBUG + if ( nodenum > ( *aasworld ).numnodes ) { + botimport.Print( PRT_ERROR, "AAS_TraceBoundingBox: nodenum out of range\n" ); + return trace; + } //end if +#endif //AAS_SAMPLE_DEBUG + //the node to test against + aasnode = &( *aasworld ).nodes[nodenum]; + //start point of current line to test against node + VectorCopy( tstack_p->start, cur_start ); + //end point of the current line to test against node + VectorCopy( tstack_p->end, cur_end ); + //the current node plane + plane = &( *aasworld ).planes[aasnode->planenum]; + + switch ( plane->type ) + {/*FIXME: wtf doesn't this work? obviously the axial node planes aren't always facing positive!!! + //check for axial planes + case PLANE_X: + { + front = cur_start[0] - plane->dist; + back = cur_end[0] - plane->dist; + break; + } //end case + case PLANE_Y: + { + front = cur_start[1] - plane->dist; + back = cur_end[1] - plane->dist; + break; + } //end case + case PLANE_Z: + { + front = cur_start[2] - plane->dist; + back = cur_end[2] - plane->dist; + break; + } //end case*/ + default: //gee it's not an axial plane + { + front = DotProduct( cur_start, plane->normal ) - plane->dist; + back = DotProduct( cur_end, plane->normal ) - plane->dist; + break; + } //end default + } //end switch + + //calculate the hitpoint with the node (split point of the line) + //put the crosspoint TRACEPLANE_EPSILON pixels on the near side + if ( front < 0 ) { + frac = ( front + TRACEPLANE_EPSILON ) / ( front - back ); + } else { frac = ( front - TRACEPLANE_EPSILON ) / ( front - back );} + //if the whole to be traced line is totally at the front of this node + //only go down the tree with the front child + if ( ( front >= -ON_EPSILON && back >= -ON_EPSILON ) ) { + //keep the current start and end point on the stack + //and go down the tree with the front child + tstack_p->nodenum = aasnode->children[0]; + tstack_p++; + if ( tstack_p >= &tracestack[127] ) { + botimport.Print( PRT_ERROR, "AAS_TraceBoundingBox: stack overflow\n" ); + return trace; + } //end if + } //end if + //if the whole to be traced line is totally at the back of this node + //only go down the tree with the back child + else if ( ( front < ON_EPSILON && back < ON_EPSILON ) ) { + //keep the current start and end point on the stack + //and go down the tree with the back child + tstack_p->nodenum = aasnode->children[1]; + tstack_p++; + if ( tstack_p >= &tracestack[127] ) { + botimport.Print( PRT_ERROR, "AAS_TraceBoundingBox: stack overflow\n" ); + return trace; + } //end if + } //end if + //go down the tree both at the front and back of the node + else + { + tmpplanenum = tstack_p->planenum; + // + if ( frac < 0 ) { + frac = 0.001; //0 + } else if ( frac > 1 ) { + frac = 0.999; //1 + } + //frac = front / (front-back); + // + cur_mid[0] = cur_start[0] + ( cur_end[0] - cur_start[0] ) * frac; + cur_mid[1] = cur_start[1] + ( cur_end[1] - cur_start[1] ) * frac; + cur_mid[2] = cur_start[2] + ( cur_end[2] - cur_start[2] ) * frac; + +// AAS_DrawPlaneCross(cur_mid, plane->normal, plane->dist, plane->type, LINECOLOR_RED); + //side the front part of the line is on + side = front < 0; + //first put the end part of the line on the stack (back side) + VectorCopy( cur_mid, tstack_p->start ); + //not necesary to store because still on stack + //VectorCopy(cur_end, tstack_p->end); + tstack_p->planenum = aasnode->planenum; + tstack_p->nodenum = aasnode->children[!side]; + tstack_p++; + if ( tstack_p >= &tracestack[127] ) { + botimport.Print( PRT_ERROR, "AAS_TraceBoundingBox: stack overflow\n" ); + return trace; + } //end if + //now put the part near the start of the line on the stack so we will + //continue with thats part first. This way we'll find the first + //hit of the bbox + VectorCopy( cur_start, tstack_p->start ); + VectorCopy( cur_mid, tstack_p->end ); + tstack_p->planenum = tmpplanenum; + tstack_p->nodenum = aasnode->children[side]; + tstack_p++; + if ( tstack_p >= &tracestack[127] ) { + botimport.Print( PRT_ERROR, "AAS_TraceBoundingBox: stack overflow\n" ); + return trace; + } //end if + } //end else + } //end while +// return trace; +} //end of the function AAS_TraceClientBBox +//=========================================================================== +// recursive subdivision of the line by the BSP tree. +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_TraceAreas( vec3_t start, vec3_t end, int *areas, vec3_t *points, int maxareas ) { + int side, nodenum, tmpplanenum; + int numareas; + float front, back, frac; + vec3_t cur_start, cur_end, cur_mid; + aas_tracestack_t tracestack[127]; + aas_tracestack_t *tstack_p; + aas_node_t *aasnode; + aas_plane_t *plane; + + numareas = 0; + areas[0] = 0; + if ( !( *aasworld ).loaded ) { + return numareas; + } + + tstack_p = tracestack; + //we start with the whole line on the stack + VectorCopy( start, tstack_p->start ); + VectorCopy( end, tstack_p->end ); + tstack_p->planenum = 0; + //start with node 1 because node zero is a dummy for a solid leaf + tstack_p->nodenum = 1; //starting at the root of the tree + tstack_p++; + + while ( 1 ) + { + //pop up the stack + tstack_p--; + //if the trace stack is empty (ended up with a piece of the + //line to be traced in an area) + if ( tstack_p < tracestack ) { + return numareas; + } //end if + //number of the current node to test the line against + nodenum = tstack_p->nodenum; + //if it is an area + if ( nodenum < 0 ) { +#ifdef AAS_SAMPLE_DEBUG + if ( -nodenum > ( *aasworld ).numareasettings ) { + botimport.Print( PRT_ERROR, "AAS_TraceAreas: -nodenum = %d out of range\n", -nodenum ); + return numareas; + } //end if +#endif //AAS_SAMPLE_DEBUG + //botimport.Print(PRT_MESSAGE, "areanum = %d, must be %d\n", -nodenum, AAS_PointAreaNum(start)); + areas[numareas] = -nodenum; + if ( points ) { + VectorCopy( tstack_p->start, points[numareas] ); + } + numareas++; + if ( numareas >= maxareas ) { + return numareas; + } + continue; + } //end if + //if it is a solid leaf + if ( !nodenum ) { + continue; + } //end if +#ifdef AAS_SAMPLE_DEBUG + if ( nodenum > ( *aasworld ).numnodes ) { + botimport.Print( PRT_ERROR, "AAS_TraceAreas: nodenum out of range\n" ); + return numareas; + } //end if +#endif //AAS_SAMPLE_DEBUG + //the node to test against + aasnode = &( *aasworld ).nodes[nodenum]; + //start point of current line to test against node + VectorCopy( tstack_p->start, cur_start ); + //end point of the current line to test against node + VectorCopy( tstack_p->end, cur_end ); + //the current node plane + plane = &( *aasworld ).planes[aasnode->planenum]; + + switch ( plane->type ) + {/*FIXME: wtf doesn't this work? obviously the node planes aren't always facing positive!!! + //check for axial planes + case PLANE_X: + { + front = cur_start[0] - plane->dist; + back = cur_end[0] - plane->dist; + break; + } //end case + case PLANE_Y: + { + front = cur_start[1] - plane->dist; + back = cur_end[1] - plane->dist; + break; + } //end case + case PLANE_Z: + { + front = cur_start[2] - plane->dist; + back = cur_end[2] - plane->dist; + break; + } //end case*/ + default: //gee it's not an axial plane + { + front = DotProduct( cur_start, plane->normal ) - plane->dist; + back = DotProduct( cur_end, plane->normal ) - plane->dist; + break; + } //end default + } //end switch + + //if the whole to be traced line is totally at the front of this node + //only go down the tree with the front child + if ( front > 0 && back > 0 ) { + //keep the current start and end point on the stack + //and go down the tree with the front child + tstack_p->nodenum = aasnode->children[0]; + tstack_p++; + if ( tstack_p >= &tracestack[127] ) { + botimport.Print( PRT_ERROR, "AAS_TraceAreas: stack overflow\n" ); + return numareas; + } //end if + } //end if + //if the whole to be traced line is totally at the back of this node + //only go down the tree with the back child + else if ( front <= 0 && back <= 0 ) { + //keep the current start and end point on the stack + //and go down the tree with the back child + tstack_p->nodenum = aasnode->children[1]; + tstack_p++; + if ( tstack_p >= &tracestack[127] ) { + botimport.Print( PRT_ERROR, "AAS_TraceAreas: stack overflow\n" ); + return numareas; + } //end if + } //end if + //go down the tree both at the front and back of the node + else + { + tmpplanenum = tstack_p->planenum; + //calculate the hitpoint with the node (split point of the line) + //put the crosspoint TRACEPLANE_EPSILON pixels on the near side + if ( front < 0 ) { + frac = ( front ) / ( front - back ); + } else { frac = ( front ) / ( front - back );} + if ( frac < 0 ) { + frac = 0; + } else if ( frac > 1 ) { + frac = 1; + } + //frac = front / (front-back); + // + cur_mid[0] = cur_start[0] + ( cur_end[0] - cur_start[0] ) * frac; + cur_mid[1] = cur_start[1] + ( cur_end[1] - cur_start[1] ) * frac; + cur_mid[2] = cur_start[2] + ( cur_end[2] - cur_start[2] ) * frac; + +// AAS_DrawPlaneCross(cur_mid, plane->normal, plane->dist, plane->type, LINECOLOR_RED); + //side the front part of the line is on + side = front < 0; + //first put the end part of the line on the stack (back side) + VectorCopy( cur_mid, tstack_p->start ); + //not necesary to store because still on stack + //VectorCopy(cur_end, tstack_p->end); + tstack_p->planenum = aasnode->planenum; + tstack_p->nodenum = aasnode->children[!side]; + tstack_p++; + if ( tstack_p >= &tracestack[127] ) { + botimport.Print( PRT_ERROR, "AAS_TraceAreas: stack overflow\n" ); + return numareas; + } //end if + //now put the part near the start of the line on the stack so we will + //continue with thats part first. This way we'll find the first + //hit of the bbox + VectorCopy( cur_start, tstack_p->start ); + VectorCopy( cur_mid, tstack_p->end ); + tstack_p->planenum = tmpplanenum; + tstack_p->nodenum = aasnode->children[side]; + tstack_p++; + if ( tstack_p >= &tracestack[127] ) { + botimport.Print( PRT_ERROR, "AAS_TraceAreas: stack overflow\n" ); + return numareas; + } //end if + } //end else + } //end while +// return numareas; +} //end of the function AAS_TraceAreas +//=========================================================================== +// a simple cross product +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +// void AAS_OrthogonalToVectors(vec3_t v1, vec3_t v2, vec3_t res) +#define AAS_OrthogonalToVectors( v1, v2, res ) \ + ( res )[0] = ( ( v1 )[1] * ( v2 )[2] ) - ( ( v1 )[2] * ( v2 )[1] ); \ + ( res )[1] = ( ( v1 )[2] * ( v2 )[0] ) - ( ( v1 )[0] * ( v2 )[2] ); \ + ( res )[2] = ( ( v1 )[0] * ( v2 )[1] ) - ( ( v1 )[1] * ( v2 )[0] ); +//=========================================================================== +// tests if the given point is within the face boundaries +// +// Parameter: face : face to test if the point is in it +// pnormal : normal of the plane to use for the face +// point : point to test if inside face boundaries +// Returns: qtrue if the point is within the face boundaries +// Changes Globals: - +//=========================================================================== +qboolean AAS_InsideFace( aas_face_t *face, vec3_t pnormal, vec3_t point, float epsilon ) { + int i, firstvertex, edgenum; + vec3_t v0; + vec3_t edgevec, pointvec, sepnormal; + aas_edge_t *edge; +#ifdef AAS_SAMPLE_DEBUG + int lastvertex = 0; +#endif //AAS_SAMPLE_DEBUG + + if ( !( *aasworld ).loaded ) { + return qfalse; + } + + for ( i = 0; i < face->numedges; i++ ) + { + edgenum = ( *aasworld ).edgeindex[face->firstedge + i]; + edge = &( *aasworld ).edges[abs( edgenum )]; + //get the first vertex of the edge + firstvertex = edgenum < 0; + VectorCopy( ( *aasworld ).vertexes[edge->v[firstvertex]], v0 ); + //edge vector + VectorSubtract( ( *aasworld ).vertexes[edge->v[!firstvertex]], v0, edgevec ); + // +#ifdef AAS_SAMPLE_DEBUG + if ( lastvertex && lastvertex != edge->v[firstvertex] ) { + botimport.Print( PRT_MESSAGE, "winding not counter clockwise\n" ); + } //end if + lastvertex = edge->v[!firstvertex]; +#endif //AAS_SAMPLE_DEBUG + //vector from first edge point to point possible in face + VectorSubtract( point, v0, pointvec ); + //get a vector pointing inside the face orthogonal to both the + //edge vector and the normal vector of the plane the face is in + //this vector defines a plane through the origin (first vertex of + //edge) and through both the edge vector and the normal vector + //of the plane + AAS_OrthogonalToVectors( edgevec, pnormal, sepnormal ); + //check on wich side of the above plane the point is + //this is done by checking the sign of the dot product of the + //vector orthogonal vector from above and the vector from the + //origin (first vertex of edge) to the point + //if the dotproduct is smaller than zero the point is outside the face + if ( DotProduct( pointvec, sepnormal ) < -epsilon ) { + return qfalse; + } + } //end for + return qtrue; +} //end of the function AAS_InsideFace +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_PointInsideFace( int facenum, vec3_t point, float epsilon ) { + int i, firstvertex, edgenum; + vec_t *v1, *v2; + vec3_t edgevec, pointvec, sepnormal; + aas_edge_t *edge; + aas_plane_t *plane; + aas_face_t *face; + + if ( !( *aasworld ).loaded ) { + return qfalse; + } + + face = &( *aasworld ).faces[facenum]; + plane = &( *aasworld ).planes[face->planenum]; + // + for ( i = 0; i < face->numedges; i++ ) + { + edgenum = ( *aasworld ).edgeindex[face->firstedge + i]; + edge = &( *aasworld ).edges[abs( edgenum )]; + //get the first vertex of the edge + firstvertex = edgenum < 0; + v1 = ( *aasworld ).vertexes[edge->v[firstvertex]]; + v2 = ( *aasworld ).vertexes[edge->v[!firstvertex]]; + //edge vector + VectorSubtract( v2, v1, edgevec ); + //vector from first edge point to point possible in face + VectorSubtract( point, v1, pointvec ); + // + CrossProduct( edgevec, plane->normal, sepnormal ); + // + if ( DotProduct( pointvec, sepnormal ) < -epsilon ) { + return qfalse; + } + } //end for + return qtrue; +} //end of the function AAS_PointInsideFace +//=========================================================================== +// returns the ground face the given point is above in the given area +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +aas_face_t *AAS_AreaGroundFace( int areanum, vec3_t point ) { + int i, facenum; + vec3_t up = {0, 0, 1}; + vec3_t normal; + aas_area_t *area; + aas_face_t *face; + + if ( !( *aasworld ).loaded ) { + return NULL; + } + + area = &( *aasworld ).areas[areanum]; + for ( i = 0; i < area->numfaces; i++ ) + { + facenum = ( *aasworld ).faceindex[area->firstface + i]; + face = &( *aasworld ).faces[abs( facenum )]; + //if this is a ground face + if ( face->faceflags & FACE_GROUND ) { + //get the up or down normal + if ( ( *aasworld ).planes[face->planenum].normal[2] < 0 ) { + VectorNegate( up, normal ); + } else { VectorCopy( up, normal );} + //check if the point is in the face + if ( AAS_InsideFace( face, normal, point, 0.01 ) ) { + return face; + } + } //end if + } //end for + return NULL; +} //end of the function AAS_AreaGroundFace +//=========================================================================== +// returns the face the trace end position is situated in +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FacePlane( int facenum, vec3_t normal, float *dist ) { + aas_plane_t *plane; + + plane = &( *aasworld ).planes[( *aasworld ).faces[facenum].planenum]; + VectorCopy( plane->normal, normal ); + *dist = plane->dist; +} //end of the function AAS_FacePlane +//=========================================================================== +// returns the face the trace end position is situated in +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +aas_face_t *AAS_TraceEndFace( aas_trace_t *trace ) { + int i, facenum; + aas_area_t *area; + aas_face_t *face, *firstface = NULL; + + if ( !( *aasworld ).loaded ) { + return NULL; + } + + //if started in solid no face was hit + if ( trace->startsolid ) { + return NULL; + } + //trace->lastarea is the last area the trace was in + area = &( *aasworld ).areas[trace->lastarea]; + //check which face the trace.endpos was in + for ( i = 0; i < area->numfaces; i++ ) + { + facenum = ( *aasworld ).faceindex[area->firstface + i]; + face = &( *aasworld ).faces[abs( facenum )]; + //if the face is in the same plane as the trace end point + if ( ( face->planenum & ~1 ) == ( trace->planenum & ~1 ) ) { + //firstface is used for optimization, if theres only one + //face in the plane then it has to be the good one + //if there are more faces in the same plane then always + //check the one with the fewest edges first +/* if (firstface) + { + if (firstface->numedges < face->numedges) + { + if (AAS_InsideFace(firstface, + (*aasworld).planes[face->planenum].normal, trace->endpos)) + { + return firstface; + } //end if + firstface = face; + } //end if + else + { + if (AAS_InsideFace(face, + (*aasworld).planes[face->planenum].normal, trace->endpos)) + { + return face; + } //end if + } //end else + } //end if + else + { + firstface = face; + } //end else*/ + if ( AAS_InsideFace( face, + ( *aasworld ).planes[face->planenum].normal, trace->endpos, 0.01 ) ) { + return face; + } + } //end if + } //end for + return firstface; +} //end of the function AAS_TraceEndFace +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_BoxOnPlaneSide2( vec3_t absmins, vec3_t absmaxs, aas_plane_t *p ) { + int i, sides; + float dist1, dist2; + vec3_t corners[2]; + + for ( i = 0; i < 3; i++ ) + { + if ( p->normal[i] < 0 ) { + corners[0][i] = absmins[i]; + corners[1][i] = absmaxs[i]; + } //end if + else + { + corners[1][i] = absmins[i]; + corners[0][i] = absmaxs[i]; + } //end else + } //end for + dist1 = DotProduct( p->normal, corners[0] ) - p->dist; + dist2 = DotProduct( p->normal, corners[1] ) - p->dist; + sides = 0; + if ( dist1 >= 0 ) { + sides = 1; + } + if ( dist2 < 0 ) { + sides |= 2; + } + + return sides; +} //end of the function AAS_BoxOnPlaneSide2 +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +//int AAS_BoxOnPlaneSide(vec3_t absmins, vec3_t absmaxs, aas_plane_t *p) +#define AAS_BoxOnPlaneSide( absmins, absmaxs, p ) ( \ + ( ( p )->type < 3 ) ? \ + ( \ + ( ( p )->dist <= ( absmins )[( p )->type] ) ? \ + ( \ + 1 \ + ) \ + : \ + ( \ + ( ( p )->dist >= ( absmaxs )[( p )->type] ) ? \ + ( \ + 2 \ + ) \ + : \ + ( \ + 3 \ + ) \ + ) \ + ) \ + : \ + ( \ + AAS_BoxOnPlaneSide2( ( absmins ), ( absmaxs ), ( p ) ) \ + ) \ + ) //end of the function AAS_BoxOnPlaneSide +//=========================================================================== +// remove the links to this entity from all areas +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_UnlinkFromAreas( aas_link_t *areas ) { + aas_link_t *link, *nextlink; + + for ( link = areas; link; link = nextlink ) + { + //next area the entity is linked in + nextlink = link->next_area; + //remove the entity from the linked list of this area + if ( link->prev_ent ) { + link->prev_ent->next_ent = link->next_ent; + } else { ( *aasworld ).arealinkedentities[link->areanum] = link->next_ent;} + if ( link->next_ent ) { + link->next_ent->prev_ent = link->prev_ent; + } + //deallocate the link structure + AAS_DeAllocAASLink( link ); + } //end for +} //end of the function AAS_UnlinkFromAreas +//=========================================================================== +// link the entity to the areas the bounding box is totally or partly +// situated in. This is done with recursion down the tree using the +// bounding box to test for plane sides +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== + +typedef struct +{ + int nodenum; //node found after splitting +} aas_linkstack_t; + +aas_link_t *AAS_AASLinkEntity( vec3_t absmins, vec3_t absmaxs, int entnum ) { + int side, nodenum; + aas_linkstack_t linkstack[128]; + aas_linkstack_t *lstack_p; + aas_node_t *aasnode; + aas_plane_t *plane; + aas_link_t *link, *areas; + + if ( !( *aasworld ).loaded ) { + botimport.Print( PRT_ERROR, "AAS_LinkEntity: aas not loaded\n" ); + return NULL; + } //end if + + areas = NULL; + // + lstack_p = linkstack; + //we start with the whole line on the stack + //start with node 1 because node zero is a dummy used for solid leafs + lstack_p->nodenum = 1; //starting at the root of the tree + lstack_p++; + + while ( 1 ) + { + //pop up the stack + lstack_p--; + //if the trace stack is empty (ended up with a piece of the + //line to be traced in an area) + if ( lstack_p < linkstack ) { + break; + } + //number of the current node to test the line against + nodenum = lstack_p->nodenum; + //if it is an area + if ( nodenum < 0 ) { + //NOTE: the entity might have already been linked into this area + // because several node children can point to the same area + for ( link = ( *aasworld ).arealinkedentities[-nodenum]; link; link = link->next_ent ) + { + if ( link->entnum == entnum ) { + break; + } + } //end for + if ( link ) { + continue; + } + // + link = AAS_AllocAASLink(); + if ( !link ) { + return areas; + } + link->entnum = entnum; + link->areanum = -nodenum; + //put the link into the double linked area list of the entity + link->prev_area = NULL; + link->next_area = areas; + if ( areas ) { + areas->prev_area = link; + } + areas = link; + //put the link into the double linked entity list of the area + link->prev_ent = NULL; + link->next_ent = ( *aasworld ).arealinkedentities[-nodenum]; + if ( ( *aasworld ).arealinkedentities[-nodenum] ) { + ( *aasworld ).arealinkedentities[-nodenum]->prev_ent = link; + } + ( *aasworld ).arealinkedentities[-nodenum] = link; + // + continue; + } //end if + //if solid leaf + if ( !nodenum ) { + continue; + } + //the node to test against + aasnode = &( *aasworld ).nodes[nodenum]; + //the current node plane + plane = &( *aasworld ).planes[aasnode->planenum]; + //get the side(s) the box is situated relative to the plane + side = AAS_BoxOnPlaneSide2( absmins, absmaxs, plane ); + //if on the front side of the node + if ( side & 1 ) { + lstack_p->nodenum = aasnode->children[0]; + lstack_p++; + } //end if + if ( lstack_p >= &linkstack[127] ) { + botimport.Print( PRT_ERROR, "AAS_LinkEntity: stack overflow\n" ); + break; + } //end if + //if on the back side of the node + if ( side & 2 ) { + lstack_p->nodenum = aasnode->children[1]; + lstack_p++; + } //end if + if ( lstack_p >= &linkstack[127] ) { + botimport.Print( PRT_ERROR, "AAS_LinkEntity: stack overflow\n" ); + break; + } //end if + } //end while + return areas; +} //end of the function AAS_AASLinkEntity +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +aas_link_t *AAS_LinkEntityClientBBox( vec3_t absmins, vec3_t absmaxs, int entnum, int presencetype ) { + vec3_t mins, maxs; + vec3_t newabsmins, newabsmaxs; + + AAS_PresenceTypeBoundingBox( presencetype, mins, maxs ); + VectorSubtract( absmins, maxs, newabsmins ); + VectorSubtract( absmaxs, mins, newabsmaxs ); + //relink the entity + return AAS_AASLinkEntity( newabsmins, newabsmaxs, entnum ); +} //end of the function AAS_LinkEntityClientBBox +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +aas_plane_t *AAS_PlaneFromNum( int planenum ) { + if ( !( *aasworld ).loaded ) { + return 0; + } + + return &( *aasworld ).planes[planenum]; +} //end of the function AAS_PlaneFromNum + +/* +============= +AAS_BBoxAreas +============= +*/ +int AAS_BBoxAreas( vec3_t absmins, vec3_t absmaxs, int *areas, int maxareas ) { + aas_link_t *linkedareas, *link; + int num; + + linkedareas = AAS_AASLinkEntity( absmins, absmaxs, -1 ); + num = 0; + for ( link = linkedareas; link; link = link->next_area ) + { + areas[num] = link->areanum; + num++; + if ( num >= maxareas ) { + break; + } + } //end for + AAS_UnlinkFromAreas( linkedareas ); + return num; +} //end of the function AAS_BBoxAreas diff --git a/src/botlib/be_aas_sample.h b/src/botlib/be_aas_sample.h new file mode 100644 index 0000000..2a3b764 --- /dev/null +++ b/src/botlib/be_aas_sample.h @@ -0,0 +1,70 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_aas_sample.h + * + * desc: AAS + * + * + *****************************************************************************/ + +#ifdef AASINTERN +void AAS_InitAASLinkHeap( void ); +void AAS_InitAASLinkedEntities( void ); +void AAS_FreeAASLinkHeap( void ); +void AAS_FreeAASLinkedEntities( void ); +aas_face_t *AAS_AreaGroundFace( int areanum, vec3_t point ); +aas_face_t *AAS_TraceEndFace( aas_trace_t *trace ); +aas_plane_t *AAS_PlaneFromNum( int planenum ); +aas_link_t *AAS_AASLinkEntity( vec3_t absmins, vec3_t absmaxs, int entnum ); +aas_link_t *AAS_LinkEntityClientBBox( vec3_t absmins, vec3_t absmaxs, int entnum, int presencetype ); +qboolean AAS_PointInsideFace( int facenum, vec3_t point, float epsilon ); +qboolean AAS_InsideFace( aas_face_t *face, vec3_t pnormal, vec3_t point, float epsilon ); +void AAS_UnlinkFromAreas( aas_link_t *areas ); +#endif //AASINTERN + +//returns the mins and maxs of the bounding box for the given presence type +void AAS_PresenceTypeBoundingBox( int presencetype, vec3_t mins, vec3_t maxs ); +//returns the cluster the area is in (negative portal number if the area is a portal) +int AAS_AreaCluster( int areanum ); +//returns the presence type(s) of the area +int AAS_AreaPresenceType( int areanum ); +//returns the presence type(s) at the given point +int AAS_PointPresenceType( vec3_t point ); +//returns the result of the trace of a client bbox +aas_trace_t AAS_TraceClientBBox( vec3_t start, vec3_t end, int presencetype, int passent ); +//stores the areas the trace went through and returns the number of passed areas +int AAS_TraceAreas( vec3_t start, vec3_t end, int *areas, vec3_t *points, int maxareas ); +//returns the area the point is in +int AAS_PointAreaNum( vec3_t point ); +//returns the plane the given face is in +void AAS_FacePlane( int facenum, vec3_t normal, float *dist ); + +int AAS_BBoxAreas( vec3_t absmins, vec3_t absmaxs, int *areas, int maxareas ); diff --git a/src/botlib/be_ai_char.c b/src/botlib/be_ai_char.c new file mode 100644 index 0000000..6fcf53b --- /dev/null +++ b/src/botlib/be_ai_char.c @@ -0,0 +1,765 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_ai_char.c + * + * desc: bot characters + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "l_log.h" +#include "l_memory.h" +#include "l_utils.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "l_libvar.h" +#include "aasfile.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "be_aas_funcs.h" +#include "be_interface.h" +#include "../game/be_ai_char.h" + +#define MAX_CHARACTERISTICS 80 + +#define CT_INTEGER 1 +#define CT_FLOAT 2 +#define CT_STRING 3 + +#define DEFAULT_CHARACTER "bots/default_c.c" + +//characteristic value +union cvalue +{ + int integer; + float _float; + char *string; +}; +//a characteristic +typedef struct bot_characteristic_s +{ + char type; //characteristic type + union cvalue value; //characteristic value +} bot_characteristic_t; + +//a bot character +typedef struct bot_character_s +{ + char filename[MAX_QPATH]; + int skill; + bot_characteristic_t c[1]; //variable sized +} bot_character_t; + +bot_character_t *botcharacters[MAX_CLIENTS + 1]; + +//======================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//======================================================================== +bot_character_t *BotCharacterFromHandle( int handle ) { + if ( handle <= 0 || handle > MAX_CLIENTS ) { + botimport.Print( PRT_FATAL, "character handle %d out of range\n", handle ); + return NULL; + } //end if + if ( !botcharacters[handle] ) { + botimport.Print( PRT_FATAL, "invalid character %d\n", handle ); + return NULL; + } //end if + return botcharacters[handle]; +} //end of the function BotCharacterFromHandle +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotDumpCharacter( bot_character_t *ch ) { + int i; + + Log_Write( "%s", ch->filename ); + Log_Write( "skill %d\n", ch->skill ); + Log_Write( "{\n" ); + for ( i = 0; i < MAX_CHARACTERISTICS; i++ ) + { + switch ( ch->c[i].type ) + { + case CT_INTEGER: Log_Write( " %4d %d\n", i, ch->c[i].value.integer ); break; + case CT_FLOAT: Log_Write( " %4d %f\n", i, ch->c[i].value._float ); break; + case CT_STRING: Log_Write( " %4d %s\n", i, ch->c[i].value.string ); break; + } //end case + } //end for + Log_Write( "}\n" ); +} //end of the function BotDumpCharacter +//======================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//======================================================================== +void BotFreeCharacterStrings( bot_character_t *ch ) { + int i; + + for ( i = 0; i < MAX_CHARACTERISTICS; i++ ) + { + if ( ch->c[i].type == CT_STRING ) { + FreeMemory( ch->c[i].value.string ); + } //end if + } //end for +} //end of the function BotFreeCharacterStrings +//======================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//======================================================================== +void BotFreeCharacter2( int handle ) { + if ( handle <= 0 || handle > MAX_CLIENTS ) { + botimport.Print( PRT_FATAL, "character handle %d out of range\n", handle ); + return; + } //end if + if ( !botcharacters[handle] ) { + botimport.Print( PRT_FATAL, "invalid character %d\n", handle ); + return; + } //end if + BotFreeCharacterStrings( botcharacters[handle] ); + FreeMemory( botcharacters[handle] ); + botcharacters[handle] = NULL; +} //end of the function BotFreeCharacter2 +//======================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//======================================================================== +void BotFreeCharacter( int handle ) { + if ( !LibVarGetValue( "bot_reloadcharacters" ) ) { + return; + } + BotFreeCharacter2( handle ); +} //end of the function BotFreeCharacter +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotDefaultCharacteristics( bot_character_t *ch, bot_character_t *defaultch ) { + int i; + + for ( i = 0; i < MAX_CHARACTERISTICS; i++ ) + { + if ( ch->c[i].type ) { + continue; + } + // + if ( defaultch->c[i].type == CT_FLOAT ) { + ch->c[i].type = CT_FLOAT; + ch->c[i].value._float = defaultch->c[i].value._float; + } //end if + else if ( defaultch->c[i].type == CT_INTEGER ) { + ch->c[i].type = CT_INTEGER; + ch->c[i].value.integer = defaultch->c[i].value.integer; + } //end else if + else if ( defaultch->c[i].type == CT_STRING ) { + ch->c[i].type = CT_STRING; + ch->c[i].value.string = (char *) GetMemory( strlen( defaultch->c[i].value.string ) + 1 ); + strcpy( ch->c[i].value.string, defaultch->c[i].value.string ); + } //end else if + } //end for +} //end of the function BotDefaultCharacteristics +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_character_t *BotLoadCharacterFromFile( char *charfile, int skill ) { + int indent, index, foundcharacter; + bot_character_t *ch; + source_t *source; + token_t token; + + foundcharacter = qfalse; + //a bot character is parsed in two phases + source = LoadSourceFile( charfile ); + if ( !source ) { + botimport.Print( PRT_ERROR, "counldn't load %s\n", charfile ); + return NULL; + } //end if + ch = (bot_character_t *) GetClearedMemory( sizeof( bot_character_t ) + + MAX_CHARACTERISTICS * sizeof( bot_characteristic_t ) ); + strcpy( ch->filename, charfile ); + while ( PC_ReadToken( source, &token ) ) + { + if ( !strcmp( token.string, "skill" ) ) { + if ( !PC_ExpectTokenType( source, TT_NUMBER, 0, &token ) ) { + FreeSource( source ); + BotFreeCharacterStrings( ch ); + FreeMemory( ch ); + return NULL; + } //end if + if ( !PC_ExpectTokenString( source, "{" ) ) { + FreeSource( source ); + BotFreeCharacterStrings( ch ); + FreeMemory( ch ); + return NULL; + } //end if + //if it's the correct skill + if ( skill < 0 || token.intvalue == skill ) { + foundcharacter = qtrue; + ch->skill = token.intvalue; + while ( PC_ExpectAnyToken( source, &token ) ) + { + if ( !strcmp( token.string, "}" ) ) { + break; + } + if ( token.type != TT_NUMBER || !( token.subtype & TT_INTEGER ) ) { + SourceError( source, "expected integer index, found %s\n", token.string ); + FreeSource( source ); + BotFreeCharacterStrings( ch ); + FreeMemory( ch ); + return NULL; + } //end if + index = token.intvalue; + if ( index < 0 || index > MAX_CHARACTERISTICS ) { + SourceError( source, "characteristic index out of range [0, %d]\n", MAX_CHARACTERISTICS ); + FreeSource( source ); + BotFreeCharacterStrings( ch ); + FreeMemory( ch ); + return NULL; + } //end if + if ( ch->c[index].type ) { + SourceError( source, "characteristic %d already initialized\n", index ); + FreeSource( source ); + BotFreeCharacterStrings( ch ); + FreeMemory( ch ); + return NULL; + } //end if + if ( !PC_ExpectAnyToken( source, &token ) ) { + FreeSource( source ); + BotFreeCharacterStrings( ch ); + FreeMemory( ch ); + return NULL; + } //end if + if ( token.type == TT_NUMBER ) { + if ( token.subtype & TT_FLOAT ) { + ch->c[index].value._float = token.floatvalue; + ch->c[index].type = CT_FLOAT; + } //end if + else + { + ch->c[index].value.integer = token.intvalue; + ch->c[index].type = CT_INTEGER; + } //end else + } //end if + else if ( token.type == TT_STRING ) { + StripDoubleQuotes( token.string ); + ch->c[index].value.string = GetMemory( strlen( token.string ) + 1 ); + strcpy( ch->c[index].value.string, token.string ); + ch->c[index].type = CT_STRING; + } //end else if + else + { + SourceError( source, "expected integer, float or string, found %s\n", token.string ); + FreeSource( source ); + BotFreeCharacterStrings( ch ); + FreeMemory( ch ); + return NULL; + } //end else + } //end if + break; + } //end if + else + { + indent = 1; + while ( indent ) + { + if ( !PC_ExpectAnyToken( source, &token ) ) { + FreeSource( source ); + BotFreeCharacterStrings( ch ); + FreeMemory( ch ); + return NULL; + } //end if + if ( !strcmp( token.string, "{" ) ) { + indent++; + } else if ( !strcmp( token.string, "}" ) ) { + indent--; + } + } //end while + } //end else + } //end if + else + { + SourceError( source, "unknown definition %s\n", token.string ); + FreeSource( source ); + BotFreeCharacterStrings( ch ); + FreeMemory( ch ); + return NULL; + } //end else + } //end while + FreeSource( source ); + // + if ( !foundcharacter ) { + BotFreeCharacterStrings( ch ); + FreeMemory( ch ); + return NULL; + } //end if + return ch; +} //end of the function BotLoadCharacterFromFile +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotFindCachedCharacter( char *charfile, int skill ) { + int handle; + + for ( handle = 1; handle <= MAX_CLIENTS; handle++ ) + { + if ( !botcharacters[handle] ) { + continue; + } + if ( strcmp( botcharacters[handle]->filename, charfile ) == 0 && + ( skill < 0 || botcharacters[handle]->skill == skill ) ) { + return handle; + } //end if + } //end for + return 0; +} //end of the function BotFindCachedCharacter +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotLoadCachedCharacter( char *charfile, int skill, int reload ) { + int handle, cachedhandle; + bot_character_t *ch = NULL; +#ifdef DEBUG + int starttime; + + starttime = Sys_MilliSeconds(); +#endif //DEBUG + + //find a free spot for a character + for ( handle = 1; handle <= MAX_CLIENTS; handle++ ) + { + if ( !botcharacters[handle] ) { + break; + } + } //end for + if ( handle > MAX_CLIENTS ) { + return 0; + } + //try to load a cached character with the given skill + if ( !reload ) { + cachedhandle = BotFindCachedCharacter( charfile, skill ); + if ( cachedhandle ) { + botimport.Print( PRT_MESSAGE, "loaded cached skill %d from %s\n", skill, charfile ); + return cachedhandle; + } //end if + } //end else + //try to load the character with the given skill + ch = BotLoadCharacterFromFile( charfile, skill ); + if ( ch ) { + botcharacters[handle] = ch; + // + botimport.Print( PRT_MESSAGE, "loaded skill %d from %s\n", skill, charfile ); +#ifdef DEBUG + if ( bot_developer ) { + botimport.Print( PRT_MESSAGE, "skill %d loaded in %d msec from %s\n", skill, Sys_MilliSeconds() - starttime, charfile ); + } //end if +#endif //DEBUG + return handle; + } //end if + // + botimport.Print( PRT_WARNING, "couldn't find skill %d in %s\n", skill, charfile ); + // + if ( !reload ) { + //try to load a cached default character with the given skill + cachedhandle = BotFindCachedCharacter( "bots/default_c.c", skill ); + if ( cachedhandle ) { + botimport.Print( PRT_MESSAGE, "loaded cached default skill %d from %s\n", skill, charfile ); + return cachedhandle; + } //end if + } //end if + //try to load the default character with the given skill + ch = BotLoadCharacterFromFile( DEFAULT_CHARACTER, skill ); + if ( ch ) { + botcharacters[handle] = ch; + botimport.Print( PRT_MESSAGE, "loaded default skill %d from %s\n", skill, charfile ); + return handle; + } //end if + // + if ( !reload ) { + //try to load a cached character with any skill + cachedhandle = BotFindCachedCharacter( charfile, -1 ); + if ( cachedhandle ) { + botimport.Print( PRT_MESSAGE, "loaded cached skill %d from %s\n", botcharacters[cachedhandle]->skill, charfile ); + return cachedhandle; + } //end if + } //end if + //try to load a character with any skill + ch = BotLoadCharacterFromFile( charfile, -1 ); + if ( ch ) { + botcharacters[handle] = ch; + botimport.Print( PRT_MESSAGE, "loaded skill %d from %s\n", ch->skill, charfile ); + return handle; + } //end if + // + if ( !reload ) { + //try to load a cached character with any skill + cachedhandle = BotFindCachedCharacter( DEFAULT_CHARACTER, -1 ); + if ( cachedhandle ) { + botimport.Print( PRT_MESSAGE, "loaded cached default skill %d from %s\n", botcharacters[cachedhandle]->skill, charfile ); + return cachedhandle; + } //end if + } //end if + //try to load a character with any skill + ch = BotLoadCharacterFromFile( DEFAULT_CHARACTER, -1 ); + if ( ch ) { + botcharacters[handle] = ch; + botimport.Print( PRT_MESSAGE, "loaded default skill %d from %s\n", ch->skill, charfile ); + return handle; + } //end if + // + botimport.Print( PRT_WARNING, "couldn't load any skill from %s\n", charfile ); + //couldn't load any character + return 0; +} //end of the function BotLoadCachedCharacter +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotLoadCharacterSkill( char *charfile, int skill ) { + int ch, defaultch; + + defaultch = BotLoadCachedCharacter( DEFAULT_CHARACTER, skill, qfalse ); + ch = BotLoadCachedCharacter( charfile, skill, LibVarGetValue( "bot_reloadcharacters" ) ); + + if ( defaultch && ch ) { + BotDefaultCharacteristics( botcharacters[ch], botcharacters[defaultch] ); + } //end if + + return ch; +} //end of the function BotLoadCharacterSkill +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotInterpolateCharacters( int handle1, int handle2, int desiredskill ) { + bot_character_t *ch1, *ch2, *out; + int i, handle; + float scale; + + ch1 = BotCharacterFromHandle( handle1 ); + ch2 = BotCharacterFromHandle( handle2 ); + if ( !ch1 || !ch2 ) { + return 0; + } + //find a free spot for a character + for ( handle = 1; handle <= MAX_CLIENTS; handle++ ) + { + if ( !botcharacters[handle] ) { + break; + } + } //end for + if ( handle > MAX_CLIENTS ) { + return 0; + } + out = (bot_character_t *) GetClearedMemory( sizeof( bot_character_t ) + + MAX_CHARACTERISTICS * sizeof( bot_characteristic_t ) ); + out->skill = desiredskill; + strcpy( out->filename, ch1->filename ); + botcharacters[handle] = out; + + scale = (float) ( desiredskill - 1 ) / ( ch2->skill - ch1->skill ); + for ( i = 0; i < MAX_CHARACTERISTICS; i++ ) + { + // + if ( ch1->c[i].type == CT_FLOAT && ch2->c[i].type == CT_FLOAT ) { + out->c[i].type = CT_FLOAT; + out->c[i].value._float = ch1->c[i].value._float + + ( ch2->c[i].value._float - ch1->c[i].value._float ) * scale; + } //end if + else if ( ch1->c[i].type == CT_INTEGER ) { + out->c[i].type = CT_INTEGER; + out->c[i].value.integer = ch1->c[i].value.integer; + } //end else if + else if ( ch1->c[i].type == CT_STRING ) { + out->c[i].type = CT_STRING; + out->c[i].value.string = (char *) GetMemory( strlen( ch1->c[i].value.string ) + 1 ); + strcpy( out->c[i].value.string, ch1->c[i].value.string ); + } //end else if + } //end for + return handle; +} //end of the function BotInterpolateCharacters +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotLoadCharacter( char *charfile, int skill ) { + int skill1, skill4, handle; + + //make sure the skill is in the valid range + if ( skill < 1 ) { + skill = 1; + } else if ( skill > 5 ) { + skill = 5; + } + //skill 1, 4 and 5 should be available in the character files + if ( skill == 1 || skill == 4 || skill == 5 ) { + return BotLoadCharacterSkill( charfile, skill ); + } //end if + //check if there's a cached skill 2 or 3 + handle = BotFindCachedCharacter( charfile, skill ); + if ( handle ) { + botimport.Print( PRT_MESSAGE, "loaded cached skill %d from %s\n", skill, charfile ); + return handle; + } //end if + //load skill 1 and 4 + skill1 = BotLoadCharacterSkill( charfile, 1 ); + if ( !skill1 ) { + return 0; + } + skill4 = BotLoadCharacterSkill( charfile, 4 ); + if ( !skill4 ) { + return skill1; + } + //interpolate between 1 and 4 to create skill 2 or 3 + handle = BotInterpolateCharacters( skill1, skill4, skill ); + if ( !handle ) { + return 0; + } + //write the character to the log file + BotDumpCharacter( botcharacters[handle] ); + // + return handle; +} //end of the function BotLoadCharacter +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int CheckCharacteristicIndex( int character, int index ) { + bot_character_t *ch; + + ch = BotCharacterFromHandle( character ); + if ( !ch ) { + return qfalse; + } + if ( index < 0 || index >= MAX_CHARACTERISTICS ) { + botimport.Print( PRT_ERROR, "characteristic %d does not exist\n", index ); + return qfalse; + } //end if + if ( !ch->c[index].type ) { + botimport.Print( PRT_ERROR, "characteristic %d is not initialized\n", index ); + return qfalse; + } //end if + return qtrue; +} //end of the function CheckCharacteristicIndex +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float Characteristic_Float( int character, int index ) { + bot_character_t *ch; + + ch = BotCharacterFromHandle( character ); + if ( !ch ) { + return 0; + } + //check if the index is in range + if ( !CheckCharacteristicIndex( character, index ) ) { + return 0; + } + //an integer will be converted to a float + if ( ch->c[index].type == CT_INTEGER ) { + return (float) ch->c[index].value.integer; + } //end if + //floats are just returned + else if ( ch->c[index].type == CT_FLOAT ) { + return ch->c[index].value._float; + } //end else if + //cannot convert a string pointer to a float + else + { + botimport.Print( PRT_ERROR, "characteristic %d is not a float\n", index ); + return 0; + } //end else if +// return 0; +} //end of the function Characteristic_Float +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float Characteristic_BFloat( int character, int index, float min, float max ) { + float value; + bot_character_t *ch; + + ch = BotCharacterFromHandle( character ); + if ( !ch ) { + return 0; + } + if ( min > max ) { + botimport.Print( PRT_ERROR, "cannot bound characteristic %d between %f and %f\n", index, min, max ); + return 0; + } //end if + value = Characteristic_Float( character, index ); + if ( value < min ) { + return min; + } + if ( value > max ) { + return max; + } + return value; +} //end of the function Characteristic_BFloat +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int Characteristic_Integer( int character, int index ) { + bot_character_t *ch; + + ch = BotCharacterFromHandle( character ); + if ( !ch ) { + return 0; + } + //check if the index is in range + if ( !CheckCharacteristicIndex( character, index ) ) { + return 0; + } + //an integer will just be returned + if ( ch->c[index].type == CT_INTEGER ) { + return ch->c[index].value.integer; + } //end if + //floats are casted to integers + else if ( ch->c[index].type == CT_FLOAT ) { + return (int) ch->c[index].value._float; + } //end else if + else + { + botimport.Print( PRT_ERROR, "characteristic %d is not a integer\n", index ); + return 0; + } //end else if +// return 0; +} //end of the function Characteristic_Integer +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int Characteristic_BInteger( int character, int index, int min, int max ) { + int value; + bot_character_t *ch; + + ch = BotCharacterFromHandle( character ); + if ( !ch ) { + return 0; + } + if ( min > max ) { + botimport.Print( PRT_ERROR, "cannot bound characteristic %d between %d and %d\n", index, min, max ); + return 0; + } //end if + value = Characteristic_Integer( character, index ); + if ( value < min ) { + return min; + } + if ( value > max ) { + return max; + } + return value; +} //end of the function Characteristic_BInteger +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Characteristic_String( int character, int index, char *buf, int size ) { + bot_character_t *ch; + + ch = BotCharacterFromHandle( character ); + if ( !ch ) { + return; + } + //check if the index is in range + if ( !CheckCharacteristicIndex( character, index ) ) { + return; + } + //an integer will be converted to a float + if ( ch->c[index].type == CT_STRING ) { + strncpy( buf, ch->c[index].value.string, size - 1 ); + buf[size - 1] = '\0'; + return; + } //end if + else + { + botimport.Print( PRT_ERROR, "characteristic %d is not a string\n", index ); + return; + } //end else if + return; +} //end of the function Characteristic_String +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotShutdownCharacters( void ) { + int handle; + + for ( handle = 1; handle <= MAX_CLIENTS; handle++ ) + { + if ( botcharacters[handle] ) { + BotFreeCharacter2( handle ); + } //end if + } //end for +} //end of the function BotShutdownCharacters diff --git a/src/botlib/be_ai_chat.c b/src/botlib/be_ai_chat.c new file mode 100644 index 0000000..73c5900 --- /dev/null +++ b/src/botlib/be_ai_chat.c @@ -0,0 +1,2883 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_ai_chat.c + * + * desc: bot chat AI + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +//#include "../server/server.h" +#include "l_memory.h" +#include "l_libvar.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "l_utils.h" +#include "l_log.h" +#include "aasfile.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "be_aas_funcs.h" +#include "be_interface.h" +#include "../game/be_ea.h" +#include "../game/be_ai_chat.h" + + +//escape character +#define ESCAPE_CHAR 0x01 //'_' +// +// "hi ", people, " ", 0, " entered the game" +//becomes: +// "hi _rpeople_ _v0_ entered the game" +// + +//match piece types +#define MT_VARIABLE 1 //variable match piece +#define MT_STRING 2 //string match piece +//reply chat key flags +#define RCKFL_AND 1 //key must be present +#define RCKFL_NOT 2 //key must be absent +#define RCKFL_NAME 4 //name of bot must be present +#define RCKFL_STRING 8 //key is a string +#define RCKFL_VARIABLES 16 //key is a match template +#define RCKFL_BOTNAMES 32 //key is a series of botnames +#define RCKFL_GENDERFEMALE 64 //bot must be female +#define RCKFL_GENDERMALE 128 //bot must be male +#define RCKFL_GENDERLESS 256 //bot must be genderless +//time to ignore a chat message after using it +#define CHATMESSAGE_RECENTTIME 20 + +//the actuall chat messages +typedef struct bot_chatmessage_s +{ + char *chatmessage; //chat message string + float time; //last time used + struct bot_chatmessage_s *next; //next chat message in a list +} bot_chatmessage_t; +//bot chat type with chat lines +typedef struct bot_chattype_s +{ + char name[MAX_CHATTYPE_NAME]; + int numchatmessages; + bot_chatmessage_t *firstchatmessage; + struct bot_chattype_s *next; +} bot_chattype_t; +//bot chat lines +typedef struct bot_chat_s +{ + bot_chattype_t *types; +} bot_chat_t; + +//random string +typedef struct bot_randomstring_s +{ + char *string; + struct bot_randomstring_s *next; +} bot_randomstring_t; +//list with random strings +typedef struct bot_randomlist_s +{ + char *string; + int numstrings; + bot_randomstring_t *firstrandomstring; + struct bot_randomlist_s *next; +} bot_randomlist_t; + +//synonym +typedef struct bot_synonym_s +{ + char *string; + float weight; + struct bot_synonym_s *next; +} bot_synonym_t; +//list with synonyms +typedef struct bot_synonymlist_s +{ + unsigned long int context; + float totalweight; + bot_synonym_t *firstsynonym; + struct bot_synonymlist_s *next; +} bot_synonymlist_t; + +//fixed match string +typedef struct bot_matchstring_s +{ + char *string; + struct bot_matchstring_s *next; +} bot_matchstring_t; + +//piece of a match template +typedef struct bot_matchpiece_s +{ + int type; + bot_matchstring_t *firststring; + int variable; + struct bot_matchpiece_s *next; +} bot_matchpiece_t; +//match template +typedef struct bot_matchtemplate_s +{ + unsigned long int context; + int type; + int subtype; + bot_matchpiece_t *first; + struct bot_matchtemplate_s *next; +} bot_matchtemplate_t; + +//reply chat key +typedef struct bot_replychatkey_s +{ + int flags; + char *string; + bot_matchpiece_t *match; + struct bot_replychatkey_s *next; +} bot_replychatkey_t; +//reply chat +typedef struct bot_replychat_s +{ + bot_replychatkey_t *keys; + float priority; + int numchatmessages; + bot_chatmessage_t *firstchatmessage; + struct bot_replychat_s *next; +} bot_replychat_t; + +//string list +typedef struct bot_stringlist_s +{ + char *string; + struct bot_stringlist_s *next; +} bot_stringlist_t; + +//chat state of a bot +typedef struct bot_chatstate_s +{ + int gender; //0=it, 1=female, 2=male + char name[32]; //name of the bot + char chatmessage[MAX_MESSAGE_SIZE]; + int handle; + //the console messages visible to the bot + bot_consolemessage_t *firstmessage; //first message is the first typed message + bot_consolemessage_t *lastmessage; //last message is the last typed message, bottom of console + //number of console messages stored in the state + int numconsolemessages; + //the bot chat lines + bot_chat_t *chat; +} bot_chatstate_t; + +typedef struct { + bot_chat_t *chat; + int inuse; + char filename[MAX_QPATH]; + char chatname[MAX_QPATH]; +} bot_ichatdata_t; + +bot_ichatdata_t ichatdata[MAX_CLIENTS]; + +bot_chatstate_t *botchatstates[MAX_CLIENTS + 1]; +//console message heap +bot_consolemessage_t *consolemessageheap = NULL; +bot_consolemessage_t *freeconsolemessages = NULL; +//list with match strings +bot_matchtemplate_t *matchtemplates = NULL; +//list with synonyms +bot_synonymlist_t *synonyms = NULL; +//list with random strings +bot_randomlist_t *randomstrings = NULL; +//reply chats +bot_replychat_t *replychats = NULL; + +//======================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//======================================================================== +bot_chatstate_t *BotChatStateFromHandle( int handle ) { + if ( handle <= 0 || handle > MAX_CLIENTS ) { + botimport.Print( PRT_FATAL, "chat state handle %d out of range\n", handle ); + return NULL; + } //end if + if ( !botchatstates[handle] ) { + botimport.Print( PRT_FATAL, "invalid chat state %d\n", handle ); + return NULL; + } //end if + return botchatstates[handle]; +} //end of the function BotChatStateFromHandle +//=========================================================================== +// initialize the heap with unused console messages +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void InitConsoleMessageHeap( void ) { + int i, max_messages; + + if ( consolemessageheap ) { + FreeMemory( consolemessageheap ); + } + // + max_messages = (int) LibVarValue( "max_messages", "1024" ); + consolemessageheap = (bot_consolemessage_t *) GetClearedHunkMemory( max_messages * + sizeof( bot_consolemessage_t ) ); + consolemessageheap[0].prev = NULL; + consolemessageheap[0].next = &consolemessageheap[1]; + for ( i = 1; i < max_messages - 1; i++ ) + { + consolemessageheap[i].prev = &consolemessageheap[i - 1]; + consolemessageheap[i].next = &consolemessageheap[i + 1]; + } //end for + consolemessageheap[max_messages - 1].prev = &consolemessageheap[max_messages - 2]; + consolemessageheap[max_messages - 1].next = NULL; + //pointer to the free console messages + freeconsolemessages = consolemessageheap; +} //end of the function InitConsoleMessageHeap +//=========================================================================== +// allocate one console message from the heap +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_consolemessage_t *AllocConsoleMessage( void ) { + bot_consolemessage_t *message; + message = freeconsolemessages; + if ( freeconsolemessages ) { + freeconsolemessages = freeconsolemessages->next; + } + if ( freeconsolemessages ) { + freeconsolemessages->prev = NULL; + } + return message; +} //end of the function AllocConsoleMessage +//=========================================================================== +// deallocate one console message from the heap +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FreeConsoleMessage( bot_consolemessage_t *message ) { + if ( freeconsolemessages ) { + freeconsolemessages->prev = message; + } + message->prev = NULL; + message->next = freeconsolemessages; + freeconsolemessages = message; +} //end of the function FreeConsoleMessage +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotRemoveConsoleMessage( int chatstate, int handle ) { + bot_consolemessage_t *m, *nextm; + bot_chatstate_t *cs; + + cs = BotChatStateFromHandle( chatstate ); + if ( !cs ) { + return; + } + + for ( m = cs->firstmessage; m; m = nextm ) + { + nextm = m->next; + if ( m->handle == handle ) { + if ( m->next ) { + m->next->prev = m->prev; + } else { cs->lastmessage = m->prev;} + if ( m->prev ) { + m->prev->next = m->next; + } else { cs->firstmessage = m->next;} + + FreeConsoleMessage( m ); + cs->numconsolemessages--; + break; + } //end if + } //end for +} //end of the function BotRemoveConsoleMessage +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotQueueConsoleMessage( int chatstate, int type, char *message ) { + bot_consolemessage_t *m; + bot_chatstate_t *cs; + + cs = BotChatStateFromHandle( chatstate ); + if ( !cs ) { + return; + } + + m = AllocConsoleMessage(); + if ( !m ) { + botimport.Print( PRT_ERROR, "empty console message heap\n" ); + return; + } //end if + cs->handle++; + if ( cs->handle <= 0 || cs->handle > 8192 ) { + cs->handle = 1; + } + m->handle = cs->handle; + m->time = AAS_Time(); + m->type = type; + strncpy( m->message, message, MAX_MESSAGE_SIZE ); + m->next = NULL; + if ( cs->lastmessage ) { + cs->lastmessage->next = m; + m->prev = cs->lastmessage; + cs->lastmessage = m; + } //end if + else + { + cs->lastmessage = m; + cs->firstmessage = m; + m->prev = NULL; + } //end if + cs->numconsolemessages++; +} //end of the function BotQueueConsoleMessage +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotNextConsoleMessage( int chatstate, bot_consolemessage_t *cm ) { + bot_chatstate_t *cs; + + cs = BotChatStateFromHandle( chatstate ); + if ( !cs ) { + return 0; + } + if ( cs->firstmessage ) { + memcpy( cm, cs->firstmessage, sizeof( bot_consolemessage_t ) ); + cm->next = cm->prev = NULL; + return cm->handle; + } //end if + return 0; +} //end of the function BotConsoleMessage +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotNumConsoleMessages( int chatstate ) { + bot_chatstate_t *cs; + + cs = BotChatStateFromHandle( chatstate ); + if ( !cs ) { + return 0; + } + return cs->numconsolemessages; +} //end of the function BotNumConsoleMessages +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int IsWhiteSpace( char c ) { + if ( ( c >= 'a' && c <= 'z' ) + || ( c >= 'A' && c <= 'Z' ) + || ( c >= '0' && c <= '9' ) + || c == '(' || c == ')' + || c == '?' || c == '\'' + || c == ':' || c == ',' + || c == '[' || c == ']' + || c == '-' || c == '_' + || c == '+' || c == '=' ) { + return qfalse; + } + return qtrue; +} //end of the function IsWhiteSpace +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotRemoveTildes( char *message ) { + int i; + + //remove all tildes from the chat message + for ( i = 0; message[i]; i++ ) + { + if ( message[i] == '~' ) { + memmove( &message[i], &message[i + 1], strlen( &message[i + 1] ) + 1 ); + } //end if + } //end for +} //end of the function BotRemoveTildes +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void UnifyWhiteSpaces( char *string ) { + char *ptr, *oldptr; + + for ( ptr = oldptr = string; *ptr; oldptr = ptr ) + { + while ( *ptr && IsWhiteSpace( *ptr ) ) ptr++; + if ( ptr > oldptr ) { + //if not at the start and not at the end of the string + //write only one space + if ( oldptr > string && *ptr ) { + *oldptr++ = ' '; + } + //remove all other white spaces + if ( ptr > oldptr ) { + memmove( oldptr, ptr, strlen( ptr ) + 1 ); + } + } //end if + while ( *ptr && !IsWhiteSpace( *ptr ) ) ptr++; + } //end while +} //end of the function UnifyWhiteSpaces +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int StringContains( char *str1, char *str2, int casesensitive ) { + int len, i, j, index; + + if ( str1 == NULL || str2 == NULL ) { + return -1; + } + + len = strlen( str1 ) - strlen( str2 ); + index = 0; + for ( i = 0; i <= len; i++, str1++, index++ ) + { + for ( j = 0; str2[j]; j++ ) + { + if ( casesensitive ) { + if ( str1[j] != str2[j] ) { + break; + } + } //end if + else + { + if ( toupper( str1[j] ) != toupper( str2[j] ) ) { + break; + } + } //end else + } //end for + if ( !str2[j] ) { + return index; + } + } //end for + return -1; +} //end of the function StringContains +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +char *StringContainsWord( char *str1, char *str2, int casesensitive ) { + int len, i, j; + + len = strlen( str1 ) - strlen( str2 ); + for ( i = 0; i <= len; i++, str1++ ) + { + //if not at the start of the string + if ( i ) { + //skip to the start of the next word + while ( *str1 && *str1 != ' ' ) str1++; + if ( !*str1 ) { + break; + } + str1++; + } //end for + //compare the word + for ( j = 0; str2[j]; j++ ) + { + if ( casesensitive ) { + if ( str1[j] != str2[j] ) { + break; + } + } //end if + else + { + if ( toupper( str1[j] ) != toupper( str2[j] ) ) { + break; + } + } //end else + } //end for + //if there was a word match + if ( !str2[j] ) { + //if the first string has an end of word + if ( !str1[j] || str1[j] == ' ' ) { + return str1; + } + } //end if + } //end for + return NULL; +} //end of the function StringContainsWord +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void StringReplaceWords( char *string, char *synonym, char *replacement ) { + char *str, *str2; + + //find the synonym in the string + str = StringContainsWord( string, synonym, qfalse ); + //if the synonym occured in the string + while ( str ) + { + //if the synonym isn't part of the replacement which is already in the string + //usefull for abreviations + str2 = StringContainsWord( string, replacement, qfalse ); + while ( str2 ) + { + if ( str2 <= str && str < str2 + strlen( replacement ) ) { + break; + } + str2 = StringContainsWord( str2 + 1, replacement, qfalse ); + } //end while + if ( !str2 ) { + memmove( str + strlen( replacement ), str + strlen( synonym ), strlen( str + strlen( synonym ) ) + 1 ); + //append the synonum replacement + memcpy( str, replacement, strlen( replacement ) ); + } //end if + //find the next synonym in the string + str = StringContainsWord( str + strlen( replacement ), synonym, qfalse ); + } //end if +} //end of the function StringReplaceWords +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotDumpSynonymList( bot_synonymlist_t *synlist ) { + FILE *fp; + bot_synonymlist_t *syn; + bot_synonym_t *synonym; + + fp = Log_FilePointer(); + if ( !fp ) { + return; + } + for ( syn = synlist; syn; syn = syn->next ) + { + fprintf( fp, "%d : [", (int)syn->context ); + for ( synonym = syn->firstsynonym; synonym; synonym = synonym->next ) + { + fprintf( fp, "(\"%s\", %1.2f)", synonym->string, synonym->weight ); + if ( synonym->next ) { + fprintf( fp, ", " ); + } + } //end for + fprintf( fp, "]\n" ); + } //end for +} //end of the function BotDumpSynonymList +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_synonymlist_t *BotLoadSynonyms( char *filename ) { + int pass, size, contextlevel, numsynonyms; + unsigned long int context, contextstack[32]; + char *ptr = NULL; + source_t *source; + token_t token; + bot_synonymlist_t *synlist, *lastsyn, *syn; + bot_synonym_t *synonym, *lastsynonym; + + size = 0; + synlist = NULL; //make compiler happy + syn = NULL; //make compiler happy + synonym = NULL; //make compiler happy + //the synonyms are parsed in two phases + for ( pass = 0; pass < 2; pass++ ) + { + // + if ( pass && size ) { + ptr = (char *) GetClearedHunkMemory( size ); + } + // + source = LoadSourceFile( filename ); + if ( !source ) { + botimport.Print( PRT_ERROR, "counldn't load %s\n", filename ); + return NULL; + } //end if + // + context = 0; + contextlevel = 0; + synlist = NULL; //list synonyms + lastsyn = NULL; //last synonym in the list + // + while ( PC_ReadToken( source, &token ) ) + { + if ( token.type == TT_NUMBER ) { + context |= token.intvalue; + contextstack[contextlevel] = token.intvalue; + contextlevel++; + if ( contextlevel >= 32 ) { + SourceError( source, "more than 32 context levels" ); + FreeSource( source ); + return NULL; + } //end if + if ( !PC_ExpectTokenString( source, "{" ) ) { + FreeSource( source ); + return NULL; + } //end if + } //end if + else if ( token.type == TT_PUNCTUATION ) { + if ( !strcmp( token.string, "}" ) ) { + contextlevel--; + if ( contextlevel < 0 ) { + SourceError( source, "too many }" ); + FreeSource( source ); + return NULL; + } //end if + context &= ~contextstack[contextlevel]; + } //end if + else if ( !strcmp( token.string, "[" ) ) { + size += sizeof( bot_synonymlist_t ); + if ( pass ) { + syn = (bot_synonymlist_t *) ptr; + ptr += sizeof( bot_synonymlist_t ); + syn->context = context; + syn->firstsynonym = NULL; + syn->next = NULL; + if ( lastsyn ) { + lastsyn->next = syn; + } else { synlist = syn;} + lastsyn = syn; + } //end if + numsynonyms = 0; + lastsynonym = NULL; + while ( 1 ) + { + if ( !PC_ExpectTokenString( source, "(" ) || + !PC_ExpectTokenType( source, TT_STRING, 0, &token ) ) { + FreeSource( source ); + return NULL; + } //end if + StripDoubleQuotes( token.string ); + if ( strlen( token.string ) <= 0 ) { + SourceError( source, "empty string", token.string ); + FreeSource( source ); + return NULL; + } //end if + size += sizeof( bot_synonym_t ) + strlen( token.string ) + 1; + if ( pass ) { + synonym = (bot_synonym_t *) ptr; + ptr += sizeof( bot_synonym_t ); + synonym->string = ptr; + ptr += strlen( token.string ) + 1; + strcpy( synonym->string, token.string ); + // + if ( lastsynonym ) { + lastsynonym->next = synonym; + } else { syn->firstsynonym = synonym;} + lastsynonym = synonym; + } //end if + numsynonyms++; + if ( !PC_ExpectTokenString( source, "," ) || + !PC_ExpectTokenType( source, TT_NUMBER, 0, &token ) || + !PC_ExpectTokenString( source, ")" ) ) { + FreeSource( source ); + return NULL; + } //end if + if ( pass ) { + synonym->weight = token.floatvalue; + syn->totalweight += synonym->weight; + } //end if + if ( PC_CheckTokenString( source, "]" ) ) { + break; + } + if ( !PC_ExpectTokenString( source, "," ) ) { + FreeSource( source ); + return NULL; + } //end if + } //end while + if ( numsynonyms < 2 ) { + SourceError( source, "synonym must have at least two entries\n" ); + FreeSource( source ); + return NULL; + } //end if + } //end else + else + { + SourceError( source, "unexpected %s", token.string ); + FreeSource( source ); + return NULL; + } //end if + } //end else if + } //end while + // + FreeSource( source ); + // + if ( contextlevel > 0 ) { + SourceError( source, "missing }" ); + return NULL; + } //end if + } //end for + botimport.Print( PRT_MESSAGE, "loaded %s\n", filename ); + // + //BotDumpSynonymList(synlist); + // + return synlist; +} //end of the function BotLoadSynonyms +//=========================================================================== +// replace all the synonyms in the string +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotReplaceSynonyms( char *string, unsigned long int context ) { + bot_synonymlist_t *syn; + bot_synonym_t *synonym; + + for ( syn = synonyms; syn; syn = syn->next ) + { + if ( !( syn->context & context ) ) { + continue; + } + for ( synonym = syn->firstsynonym->next; synonym; synonym = synonym->next ) + { + StringReplaceWords( string, synonym->string, syn->firstsynonym->string ); + } //end for + } //end for +} //end of the function BotReplaceSynonyms +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotReplaceWeightedSynonyms( char *string, unsigned long int context ) { + bot_synonymlist_t *syn; + bot_synonym_t *synonym, *replacement; + float weight, curweight; + + for ( syn = synonyms; syn; syn = syn->next ) + { + if ( !( syn->context & context ) ) { + continue; + } + //choose a weighted random replacement synonym + weight = random() * syn->totalweight; + if ( !weight ) { + continue; + } + curweight = 0; + for ( replacement = syn->firstsynonym; replacement; replacement = replacement->next ) + { + curweight += replacement->weight; + if ( weight < curweight ) { + break; + } + } //end for + if ( !replacement ) { + continue; + } + //replace all synonyms with the replacement + for ( synonym = syn->firstsynonym; synonym; synonym = synonym->next ) + { + if ( synonym == replacement ) { + continue; + } + StringReplaceWords( string, synonym->string, replacement->string ); + } //end for + } //end for +} //end of the function BotReplaceWeightedSynonyms +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotReplaceReplySynonyms( char *string, unsigned long int context ) { + char *str1, *str2, *replacement; + bot_synonymlist_t *syn; + bot_synonym_t *synonym; + + for ( str1 = string; *str1; ) + { + //go to the start of the next word + while ( *str1 && *str1 <= ' ' ) str1++; + if ( !*str1 ) { + break; + } + // + for ( syn = synonyms; syn; syn = syn->next ) + { + if ( !( syn->context & context ) ) { + continue; + } + for ( synonym = syn->firstsynonym->next; synonym; synonym = synonym->next ) + { + str2 = synonym->string; + //if the synonym is not at the front of the string continue + str2 = StringContainsWord( str1, synonym->string, qfalse ); + if ( !str2 || str2 != str1 ) { + continue; + } + // + replacement = syn->firstsynonym->string; + //if the replacement IS in front of the string continue + str2 = StringContainsWord( str1, replacement, qfalse ); + if ( str2 && str2 == str1 ) { + continue; + } + // + memmove( str1 + strlen( replacement ), str1 + strlen( synonym->string ), + strlen( str1 + strlen( synonym->string ) ) + 1 ); + //append the synonum replacement + memcpy( str1, replacement, strlen( replacement ) ); + // + break; + } //end for + //if a synonym has been replaced + if ( synonym ) { + break; + } + } //end for + //skip over this word + while ( *str1 && *str1 > ' ' ) str1++; + if ( !*str1 ) { + break; + } + } //end while +} //end of the function BotReplaceReplySynonyms +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotLoadChatMessage( source_t *source, char *chatmessagestring ) { + char *ptr; + token_t token; + + ptr = chatmessagestring; + *ptr = 0; + // + while ( 1 ) + { + if ( !PC_ExpectAnyToken( source, &token ) ) { + return qfalse; + } + //fixed string + if ( token.type == TT_STRING ) { + StripDoubleQuotes( token.string ); + if ( strlen( ptr ) + strlen( token.string ) + 1 > MAX_MESSAGE_SIZE ) { + SourceError( source, "chat message too long\n" ); + return qfalse; + } //end if + strcat( ptr, token.string ); + } //end else if + //variable string + else if ( token.type == TT_NUMBER && ( token.subtype & TT_INTEGER ) ) { + if ( strlen( ptr ) + 7 > MAX_MESSAGE_SIZE ) { + SourceError( source, "chat message too long\n" ); + return qfalse; + } //end if + sprintf( &ptr[strlen( ptr )], "%cv%d%c", ESCAPE_CHAR, (int)token.intvalue, ESCAPE_CHAR ); + } //end if + //random string + else if ( token.type == TT_NAME ) { + if ( strlen( ptr ) + 7 > MAX_MESSAGE_SIZE ) { + SourceError( source, "chat message too long\n" ); + return qfalse; + } //end if + sprintf( &ptr[strlen( ptr )], "%cr%s%c", ESCAPE_CHAR, token.string, ESCAPE_CHAR ); + } //end else if + else + { + SourceError( source, "unknown message component %s\n", token.string ); + return qfalse; + } //end else + if ( PC_CheckTokenString( source, ";" ) ) { + break; + } + if ( !PC_ExpectTokenString( source, "," ) ) { + return qfalse; + } + } //end while + // + return qtrue; +} //end of the function BotLoadChatMessage +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotDumpRandomStringList( bot_randomlist_t *randomlist ) { + FILE *fp; + bot_randomlist_t *random; + bot_randomstring_t *rs; + + fp = Log_FilePointer(); + if ( !fp ) { + return; + } + for ( random = randomlist; random; random = random->next ) + { + fprintf( fp, "%s = {", random->string ); + for ( rs = random->firstrandomstring; rs; rs = rs->next ) + { + fprintf( fp, "\"%s\"", rs->string ); + if ( rs->next ) { + fprintf( fp, ", " ); + } else { fprintf( fp, "}\n" );} + } //end for + } //end for +} //end of the function BotDumpRandomStringList +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_randomlist_t *BotLoadRandomStrings( char *filename ) { + int pass, size; + char *ptr = NULL, chatmessagestring[MAX_MESSAGE_SIZE]; + source_t *source; + token_t token; + bot_randomlist_t *randomlist, *lastrandom, *random; + bot_randomstring_t *randomstring; + +#ifdef DEBUG + int starttime = Sys_MilliSeconds(); +#endif //DEBUG + + size = 0; + randomlist = NULL; + random = NULL; + //the synonyms are parsed in two phases + for ( pass = 0; pass < 2; pass++ ) + { + // + if ( pass && size ) { + ptr = (char *) GetClearedHunkMemory( size ); + } + // + source = LoadSourceFile( filename ); + if ( !source ) { + botimport.Print( PRT_ERROR, "counldn't load %s\n", filename ); + return NULL; + } //end if + // + randomlist = NULL; //list + lastrandom = NULL; //last + // + while ( PC_ReadToken( source, &token ) ) + { + if ( token.type != TT_NAME ) { + SourceError( source, "unknown random %s", token.string ); + FreeSource( source ); + return NULL; + } //end if + size += sizeof( bot_randomlist_t ) + strlen( token.string ) + 1; + if ( pass ) { + random = (bot_randomlist_t *) ptr; + ptr += sizeof( bot_randomlist_t ); + random->string = ptr; + ptr += strlen( token.string ) + 1; + strcpy( random->string, token.string ); + random->firstrandomstring = NULL; + random->numstrings = 0; + // + if ( lastrandom ) { + lastrandom->next = random; + } else { randomlist = random;} + lastrandom = random; + } //end if + if ( !PC_ExpectTokenString( source, "=" ) || + !PC_ExpectTokenString( source, "{" ) ) { + FreeSource( source ); + return NULL; + } //end if + while ( !PC_CheckTokenString( source, "}" ) ) + { + if ( !BotLoadChatMessage( source, chatmessagestring ) ) { + FreeSource( source ); + return NULL; + } //end if + size += sizeof( bot_randomstring_t ) + strlen( chatmessagestring ) + 1; + if ( pass ) { + randomstring = (bot_randomstring_t *) ptr; + ptr += sizeof( bot_randomstring_t ); + randomstring->string = ptr; + ptr += strlen( chatmessagestring ) + 1; + strcpy( randomstring->string, chatmessagestring ); + // + random->numstrings++; + randomstring->next = random->firstrandomstring; + random->firstrandomstring = randomstring; + } //end if + } //end while + } //end while + //free the source after one pass + FreeSource( source ); + } //end for + botimport.Print( PRT_MESSAGE, "loaded %s\n", filename ); + // +#ifdef DEBUG + botimport.Print( PRT_MESSAGE, "random strings %d msec\n", Sys_MilliSeconds() - starttime ); + //BotDumpRandomStringList(randomlist); +#endif //DEBUG + // + return randomlist; +} //end of the function BotLoadRandomStrings +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +char *RandomString( char *name ) { + bot_randomlist_t *random; + bot_randomstring_t *rs; + int i; + + for ( random = randomstrings; random; random = random->next ) + { + if ( !strcmp( random->string, name ) ) { + i = random() * random->numstrings; + for ( rs = random->firstrandomstring; rs; rs = rs->next ) + { + if ( --i < 0 ) { + break; + } + } //end for + if ( rs ) { + return rs->string; + } //end if + } //end for + } //end for + return NULL; +} //end of the function RandomString +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotDumpMatchTemplates( bot_matchtemplate_t *matches ) { + FILE *fp; + bot_matchtemplate_t *mt; + bot_matchpiece_t *mp; + bot_matchstring_t *ms; + + fp = Log_FilePointer(); + if ( !fp ) { + return; + } + for ( mt = matches; mt; mt = mt->next ) + { + //fprintf(fp, "%8d { "); + for ( mp = mt->first; mp; mp = mp->next ) + { + if ( mp->type == MT_STRING ) { + for ( ms = mp->firststring; ms; ms = ms->next ) + { + fprintf( fp, "\"%s\"", ms->string ); + if ( ms->next ) { + fprintf( fp, "|" ); + } + } //end for + } //end if + else if ( mp->type == MT_VARIABLE ) { + fprintf( fp, "%d", mp->variable ); + } //end else if + if ( mp->next ) { + fprintf( fp, ", " ); + } + } //end for + fprintf( fp, " = (%d, %d);}\n", mt->type, mt->subtype ); + } //end for +} //end of the function BotDumpMatchTemplates +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotFreeMatchPieces( bot_matchpiece_t *matchpieces ) { + bot_matchpiece_t *mp, *nextmp; + bot_matchstring_t *ms, *nextms; + + for ( mp = matchpieces; mp; mp = nextmp ) + { + nextmp = mp->next; + if ( mp->type == MT_STRING ) { + for ( ms = mp->firststring; ms; ms = nextms ) + { + nextms = ms->next; + FreeMemory( ms ); + } //end for + } //end if + FreeMemory( mp ); + } //end for +} //end of the function BotFreeMatchPieces +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_matchpiece_t *BotLoadMatchPieces( source_t *source, char *endtoken ) { + int lastwasvariable, emptystring; + token_t token; + bot_matchpiece_t *matchpiece, *firstpiece, *lastpiece; + bot_matchstring_t *matchstring, *lastmatchstring; + + firstpiece = NULL; + lastpiece = NULL; + // + lastwasvariable = qfalse; + // + while ( PC_ReadToken( source, &token ) ) + { + if ( token.type == TT_NUMBER && ( token.subtype & TT_INTEGER ) ) { + if ( token.intvalue < 0 || token.intvalue >= MAX_MATCHVARIABLES ) { + SourceError( source, "can't have more than %d match variables\n", MAX_MATCHVARIABLES ); + FreeSource( source ); + BotFreeMatchPieces( firstpiece ); + return NULL; + } //end if + if ( lastwasvariable ) { + SourceError( source, "not allowed to have adjacent variables\n" ); + FreeSource( source ); + BotFreeMatchPieces( firstpiece ); + return NULL; + } //end if + lastwasvariable = qtrue; + // + matchpiece = (bot_matchpiece_t *) GetClearedHunkMemory( sizeof( bot_matchpiece_t ) ); + matchpiece->type = MT_VARIABLE; + matchpiece->variable = token.intvalue; + matchpiece->next = NULL; + if ( lastpiece ) { + lastpiece->next = matchpiece; + } else { firstpiece = matchpiece;} + lastpiece = matchpiece; + } //end if + else if ( token.type == TT_STRING ) { + // + matchpiece = (bot_matchpiece_t *) GetClearedHunkMemory( sizeof( bot_matchpiece_t ) ); + matchpiece->firststring = NULL; + matchpiece->type = MT_STRING; + matchpiece->variable = 0; + matchpiece->next = NULL; + if ( lastpiece ) { + lastpiece->next = matchpiece; + } else { firstpiece = matchpiece;} + lastpiece = matchpiece; + // + lastmatchstring = NULL; + emptystring = qfalse; + // + do + { + if ( matchpiece->firststring ) { + if ( !PC_ExpectTokenType( source, TT_STRING, 0, &token ) ) { + FreeSource( source ); + BotFreeMatchPieces( firstpiece ); + return NULL; + } //end if + } //end if + StripDoubleQuotes( token.string ); + matchstring = (bot_matchstring_t *) GetClearedHunkMemory( sizeof( bot_matchstring_t ) + strlen( token.string ) + 1 ); + matchstring->string = (char *) matchstring + sizeof( bot_matchstring_t ); + strcpy( matchstring->string, token.string ); + if ( !strlen( token.string ) ) { + emptystring = qtrue; + } + matchstring->next = NULL; + if ( lastmatchstring ) { + lastmatchstring->next = matchstring; + } else { matchpiece->firststring = matchstring;} + lastmatchstring = matchstring; + } while ( PC_CheckTokenString( source, "|" ) ); + //if there was no empty string found + if ( !emptystring ) { + lastwasvariable = qfalse; + } + } //end if + else + { + SourceError( source, "invalid token %s\n", token.string ); + FreeSource( source ); + BotFreeMatchPieces( firstpiece ); + return NULL; + } //end else + if ( PC_CheckTokenString( source, endtoken ) ) { + break; + } + if ( !PC_ExpectTokenString( source, "," ) ) { + FreeSource( source ); + BotFreeMatchPieces( firstpiece ); + return NULL; + } //end if + } //end while + return firstpiece; +} //end of the function BotLoadMatchPieces +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotFreeMatchTemplates( bot_matchtemplate_t *mt ) { + bot_matchtemplate_t *nextmt; + + for (; mt; mt = nextmt ) + { + nextmt = mt->next; + BotFreeMatchPieces( mt->first ); + FreeMemory( mt ); + } //end for +} //end of the function BotFreeMatchTemplates +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_matchtemplate_t *BotLoadMatchTemplates( char *matchfile ) { + source_t *source; + token_t token; + bot_matchtemplate_t *matchtemplate, *matches, *lastmatch; + unsigned long int context; + + source = LoadSourceFile( matchfile ); + if ( !source ) { + botimport.Print( PRT_ERROR, "counldn't load %s\n", matchfile ); + return NULL; + } //end if + // + matches = NULL; //list with matches + lastmatch = NULL; //last match in the list + + while ( PC_ReadToken( source, &token ) ) + { + if ( token.type != TT_NUMBER || !( token.subtype & TT_INTEGER ) ) { + SourceError( source, "expected integer, found %s\n", token.string ); + BotFreeMatchTemplates( matches ); + FreeSource( source ); + return NULL; + } //end if + //the context + context = token.intvalue; + // + if ( !PC_ExpectTokenString( source, "{" ) ) { + BotFreeMatchTemplates( matches ); + FreeSource( source ); + return NULL; + } //end if + // + while ( PC_ReadToken( source, &token ) ) + { + if ( !strcmp( token.string, "}" ) ) { + break; + } + // + PC_UnreadLastToken( source ); + // + matchtemplate = (bot_matchtemplate_t *) GetClearedHunkMemory( sizeof( bot_matchtemplate_t ) ); + matchtemplate->context = context; + matchtemplate->next = NULL; + //add the match template to the list + if ( lastmatch ) { + lastmatch->next = matchtemplate; + } else { matches = matchtemplate;} + lastmatch = matchtemplate; + //load the match template + matchtemplate->first = BotLoadMatchPieces( source, "=" ); + if ( !matchtemplate->first ) { + BotFreeMatchTemplates( matches ); + return NULL; + } //end if + //read the match type + if ( !PC_ExpectTokenString( source, "(" ) || + !PC_ExpectTokenType( source, TT_NUMBER, TT_INTEGER, &token ) ) { + BotFreeMatchTemplates( matches ); + FreeSource( source ); + return NULL; + } //end if + matchtemplate->type = token.intvalue; + //read the match subtype + if ( !PC_ExpectTokenString( source, "," ) || + !PC_ExpectTokenType( source, TT_NUMBER, TT_INTEGER, &token ) ) { + BotFreeMatchTemplates( matches ); + FreeSource( source ); + return NULL; + } //end if + matchtemplate->subtype = token.intvalue; + //read trailing punctuations + if ( !PC_ExpectTokenString( source, ")" ) || + !PC_ExpectTokenString( source, ";" ) ) { + BotFreeMatchTemplates( matches ); + FreeSource( source ); + return NULL; + } //end if + } //end while + } //end while + //free the source + FreeSource( source ); + botimport.Print( PRT_MESSAGE, "loaded %s\n", matchfile ); + // + //BotDumpMatchTemplates(matches); + // + return matches; +} //end of the function BotLoadMatchTemplates +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int StringsMatch( bot_matchpiece_t *pieces, bot_match_t *match ) { + int lastvariable, index; + char *strptr, *newstrptr; + bot_matchpiece_t *mp; + bot_matchstring_t *ms; + + //no last variable + lastvariable = -1; + //pointer to the string to compare the match string with + strptr = match->string; + //Log_Write("match: %s", strptr); + //compare the string with the current match string + for ( mp = pieces; mp; mp = mp->next ) + { + //if it is a piece of string + if ( mp->type == MT_STRING ) { + newstrptr = NULL; + for ( ms = mp->firststring; ms; ms = ms->next ) + { + if ( !strlen( ms->string ) ) { + newstrptr = strptr; + break; + } //end if + //Log_Write("MT_STRING: %s", mp->string); + index = StringContains( strptr, ms->string, qfalse ); + if ( index >= 0 ) { + newstrptr = strptr + index; + if ( lastvariable >= 0 ) { + match->variables[lastvariable].length = + newstrptr - match->variables[lastvariable].ptr; + lastvariable = -1; + break; + } //end if + else if ( index == 0 ) { + break; + } //end else + newstrptr = NULL; + } //end if + } //end for + if ( !newstrptr ) { + return qfalse; + } + strptr = newstrptr + strlen( ms->string ); + } //end if + //if it is a variable piece of string + else if ( mp->type == MT_VARIABLE ) { + //Log_Write("MT_VARIABLE"); + match->variables[mp->variable].ptr = strptr; + lastvariable = mp->variable; + } //end else if + } //end for + //if a match was found + if ( !mp && ( lastvariable >= 0 || !strlen( strptr ) ) ) { + //if the last piece was a variable string + if ( lastvariable >= 0 ) { + match->variables[lastvariable].length = strlen( match->variables[lastvariable].ptr ); + } //end if + return qtrue; + } //end if + return qfalse; +} //end of the function StringsMatch +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotFindMatch( char *str, bot_match_t *match, unsigned long int context ) { + int i; + bot_matchtemplate_t *ms; + + strncpy( match->string, str, MAX_MESSAGE_SIZE ); + //remove any trailing enters + while ( strlen( match->string ) && + match->string[strlen( match->string ) - 1] == '\n' ) + { + match->string[strlen( match->string ) - 1] = '\0'; + } //end while + //compare the string with all the match strings + for ( ms = matchtemplates; ms; ms = ms->next ) + { + if ( !( ms->context & context ) ) { + continue; + } + //reset the match variable pointers + for ( i = 0; i < MAX_MATCHVARIABLES; i++ ) match->variables[i].ptr = NULL; + // + if ( StringsMatch( ms->first, match ) ) { + match->type = ms->type; + match->subtype = ms->subtype; + return qtrue; + } //end if + } //end for + return qfalse; +} //end of the function BotFindMatch +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotMatchVariable( bot_match_t *match, int variable, char *buf, int size ) { + if ( variable < 0 || variable >= MAX_MATCHVARIABLES ) { + botimport.Print( PRT_FATAL, "BotMatchVariable: variable out of range\n" ); + strcpy( buf, "" ); + return; + } //end if + + if ( match->variables[variable].ptr ) { + if ( match->variables[variable].length < size ) { + size = match->variables[variable].length + 1; + } + strncpy( buf, match->variables[variable].ptr, size - 1 ); + buf[size - 1] = '\0'; + } //end if + else + { + strcpy( buf, "" ); + } //end else + return; +} //end of the function BotMatchVariable +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_stringlist_t *BotFindStringInList( bot_stringlist_t *list, char *string ) { + bot_stringlist_t *s; + + for ( s = list; s; s = s->next ) + { + if ( !strcmp( s->string, string ) ) { + return s; + } + } //end for + return NULL; +} //end of the function BotFindStringInList +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_stringlist_t *BotCheckChatMessageIntegrety( char *message, bot_stringlist_t *stringlist ) { + int i; + char *msgptr; + char temp[MAX_MESSAGE_SIZE]; + bot_stringlist_t *s; + + msgptr = message; + // + while ( *msgptr ) + { + if ( *msgptr == ESCAPE_CHAR ) { + msgptr++; + switch ( *msgptr ) + { + case 'v': //variable + { + //step over the 'v' + msgptr++; + while ( *msgptr && *msgptr != ESCAPE_CHAR ) msgptr++; + //step over the trailing escape char + if ( *msgptr ) { + msgptr++; + } + break; + } //end case + case 'r': //random + { + //step over the 'r' + msgptr++; + for ( i = 0; ( *msgptr && *msgptr != ESCAPE_CHAR ); i++ ) + { + temp[i] = *msgptr++; + } //end while + temp[i] = '\0'; + //step over the trailing escape char + if ( *msgptr ) { + msgptr++; + } + //find the random keyword + if ( !RandomString( temp ) ) { + if ( !BotFindStringInList( stringlist, temp ) ) { + Log_Write( "%s = {\"%s\"} //MISSING RANDOM\r\n", temp, temp ); + s = GetClearedMemory( sizeof( bot_stringlist_t ) + strlen( temp ) + 1 ); + s->string = (char *) s + sizeof( bot_stringlist_t ); + strcpy( s->string, temp ); + s->next = stringlist; + stringlist = s; + } //end if + } //end if + break; + } //end case + default: + { + botimport.Print( PRT_FATAL, "BotCheckChatMessageIntegrety: message \"%s\" invalid escape char\n", message ); + break; + } //end default + } //end switch + } //end if + else + { + msgptr++; + } //end else + } //end while + return stringlist; +} //end of the function BotCheckChatMessageIntegrety +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotCheckReplyChatIntegrety( bot_replychat_t *replychat ) { + bot_replychat_t *rp; + bot_chatmessage_t *cm; + bot_stringlist_t *stringlist, *s, *nexts; + + stringlist = NULL; + for ( rp = replychat; rp; rp = rp->next ) + { + for ( cm = rp->firstchatmessage; cm; cm = cm->next ) + { + stringlist = BotCheckChatMessageIntegrety( cm->chatmessage, stringlist ); + } //end for + } //end for + for ( s = stringlist; s; s = nexts ) + { + nexts = s->next; + FreeMemory( s ); + } //end for +} //end of the function BotCheckReplyChatIntegrety +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotCheckInitialChatIntegrety( bot_chat_t *chat ) { + bot_chattype_t *t; + bot_chatmessage_t *cm; + bot_stringlist_t *stringlist, *s, *nexts; + + stringlist = NULL; + for ( t = chat->types; t; t = t->next ) + { + for ( cm = t->firstchatmessage; cm; cm = cm->next ) + { + stringlist = BotCheckChatMessageIntegrety( cm->chatmessage, stringlist ); + } //end for + } //end for + for ( s = stringlist; s; s = nexts ) + { + nexts = s->next; + FreeMemory( s ); + } //end for +} //end of the function BotCheckInitialChatIntegrety +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotDumpReplyChat( bot_replychat_t *replychat ) { + FILE *fp; + bot_replychat_t *rp; + bot_replychatkey_t *key; + bot_chatmessage_t *cm; + bot_matchpiece_t *mp; + + fp = Log_FilePointer(); + if ( !fp ) { + return; + } + fprintf( fp, "BotDumpReplyChat:\n" ); + for ( rp = replychat; rp; rp = rp->next ) + { + fprintf( fp, "[" ); + for ( key = rp->keys; key; key = key->next ) + { + if ( key->flags & RCKFL_AND ) { + fprintf( fp, "&" ); + } else if ( key->flags & RCKFL_NOT ) { + fprintf( fp, "!" ); + } + // + if ( key->flags & RCKFL_NAME ) { + fprintf( fp, "name" ); + } else if ( key->flags & RCKFL_GENDERFEMALE ) { + fprintf( fp, "female" ); + } else if ( key->flags & RCKFL_GENDERMALE ) { + fprintf( fp, "male" ); + } else if ( key->flags & RCKFL_GENDERLESS ) { + fprintf( fp, "it" ); + } else if ( key->flags & RCKFL_VARIABLES ) { + fprintf( fp, "(" ); + for ( mp = key->match; mp; mp = mp->next ) + { + if ( mp->type == MT_STRING ) { + fprintf( fp, "\"%s\"", mp->firststring->string ); + } else { fprintf( fp, "%d", mp->variable );} + if ( mp->next ) { + fprintf( fp, ", " ); + } + } //end for + fprintf( fp, ")" ); + } //end if + else if ( key->flags & RCKFL_STRING ) { + fprintf( fp, "\"%s\"", key->string ); + } //end if + if ( key->next ) { + fprintf( fp, ", " ); + } else { fprintf( fp, "] = %1.0f\n", rp->priority );} + } //end for + fprintf( fp, "{\n" ); + for ( cm = rp->firstchatmessage; cm; cm = cm->next ) + { + fprintf( fp, "\t\"%s\";\n", cm->chatmessage ); + } //end for + fprintf( fp, "}\n" ); + } //end for +} //end of the function BotDumpReplyChat +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotFreeReplyChat( bot_replychat_t *replychat ) { + bot_replychat_t *rp, *nextrp; + bot_replychatkey_t *key, *nextkey; + bot_chatmessage_t *cm, *nextcm; + + for ( rp = replychat; rp; rp = nextrp ) + { + nextrp = rp->next; + for ( key = rp->keys; key; key = nextkey ) + { + nextkey = key->next; + if ( key->match ) { + BotFreeMatchPieces( key->match ); + } + if ( key->string ) { + FreeMemory( key->string ); + } + FreeMemory( key ); + } //end for + for ( cm = rp->firstchatmessage; cm; cm = nextcm ) + { + nextcm = cm->next; + FreeMemory( cm ); + } //end for + FreeMemory( rp ); + } //end for +} //end of the function BotFreeReplyChat +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_replychat_t *BotLoadReplyChat( char *filename ) { + char chatmessagestring[MAX_MESSAGE_SIZE]; + char namebuffer[MAX_MESSAGE_SIZE]; + source_t *source; + token_t token; + bot_chatmessage_t *chatmessage = NULL; + bot_replychat_t *replychat, *replychatlist; + bot_replychatkey_t *key; + + source = LoadSourceFile( filename ); + if ( !source ) { + botimport.Print( PRT_ERROR, "counldn't load %s\n", filename ); + return NULL; + } //end if + // + replychatlist = NULL; + // + while ( PC_ReadToken( source, &token ) ) + { + if ( strcmp( token.string, "[" ) ) { + SourceError( source, "expected [, found %s", token.string ); + BotFreeReplyChat( replychatlist ); + FreeSource( source ); + return NULL; + } //end if + // + replychat = GetClearedHunkMemory( sizeof( bot_replychat_t ) ); + replychat->keys = NULL; + replychat->next = replychatlist; + replychatlist = replychat; + //read the keys, there must be at least one key + do + { + //allocate a key + key = (bot_replychatkey_t *) GetClearedHunkMemory( sizeof( bot_replychatkey_t ) ); + key->flags = 0; + key->string = NULL; + key->match = NULL; + key->next = replychat->keys; + replychat->keys = key; + //check for MUST BE PRESENT and MUST BE ABSENT keys + if ( PC_CheckTokenString( source, "&" ) ) { + key->flags |= RCKFL_AND; + } else if ( PC_CheckTokenString( source, "!" ) ) { + key->flags |= RCKFL_NOT; + } + //special keys + if ( PC_CheckTokenString( source, "name" ) ) { + key->flags |= RCKFL_NAME; + } else if ( PC_CheckTokenString( source, "female" ) ) { + key->flags |= RCKFL_GENDERFEMALE; + } else if ( PC_CheckTokenString( source, "male" ) ) { + key->flags |= RCKFL_GENDERMALE; + } else if ( PC_CheckTokenString( source, "it" ) ) { + key->flags |= RCKFL_GENDERLESS; + } else if ( PC_CheckTokenString( source, "(" ) ) { //match key + key->flags |= RCKFL_VARIABLES; + key->match = BotLoadMatchPieces( source, ")" ); + if ( !key->match ) { + BotFreeReplyChat( replychatlist ); + return NULL; + } //end if + } //end else if + else if ( PC_CheckTokenString( source, "<" ) ) { //bot names + key->flags |= RCKFL_BOTNAMES; + strcpy( namebuffer, "" ); + do + { + if ( !PC_ExpectTokenType( source, TT_STRING, 0, &token ) ) { + BotFreeReplyChat( replychatlist ); + FreeSource( source ); + return NULL; + } //end if + StripDoubleQuotes( token.string ); + if ( strlen( namebuffer ) ) { + strcat( namebuffer, "\\" ); + } + strcat( namebuffer, token.string ); + } while ( PC_CheckTokenString( source, "," ) ); + if ( !PC_ExpectTokenString( source, ">" ) ) { + BotFreeReplyChat( replychatlist ); + FreeSource( source ); + return NULL; + } //end if + key->string = (char *) GetClearedHunkMemory( strlen( namebuffer ) + 1 ); + strcpy( key->string, namebuffer ); + } //end else if + else //normal string key + { + key->flags |= RCKFL_STRING; + if ( !PC_ExpectTokenType( source, TT_STRING, 0, &token ) ) { + BotFreeReplyChat( replychatlist ); + FreeSource( source ); + return NULL; + } //end if + StripDoubleQuotes( token.string ); + key->string = (char *) GetClearedHunkMemory( strlen( token.string ) + 1 ); + strcpy( key->string, token.string ); + } //end else + // + PC_CheckTokenString( source, "," ); + } while ( !PC_CheckTokenString( source, "]" ) ); + //read the = sign and the priority + if ( !PC_ExpectTokenString( source, "=" ) || + !PC_ExpectTokenType( source, TT_NUMBER, 0, &token ) ) { + BotFreeReplyChat( replychatlist ); + FreeSource( source ); + return NULL; + } //end if + replychat->priority = token.floatvalue; + //read the leading { + if ( !PC_ExpectTokenString( source, "{" ) ) { + BotFreeReplyChat( replychatlist ); + FreeSource( source ); + return NULL; + } //end if + replychat->numchatmessages = 0; + //while the trailing } is not found + while ( !PC_CheckTokenString( source, "}" ) ) + { + if ( !BotLoadChatMessage( source, chatmessagestring ) ) { + BotFreeReplyChat( replychatlist ); + FreeSource( source ); + return NULL; + } //end if + chatmessage = (bot_chatmessage_t *) GetClearedHunkMemory( sizeof( bot_chatmessage_t ) + strlen( chatmessagestring ) + 1 ); + chatmessage->chatmessage = (char *) chatmessage + sizeof( bot_chatmessage_t ); + strcpy( chatmessage->chatmessage, chatmessagestring ); + chatmessage->time = -2 * CHATMESSAGE_RECENTTIME; + chatmessage->next = replychat->firstchatmessage; + //add the chat message to the reply chat + replychat->firstchatmessage = chatmessage; + replychat->numchatmessages++; + } //end while + } //end while + FreeSource( source ); + botimport.Print( PRT_MESSAGE, "loaded %s\n", filename ); + // + //BotDumpReplyChat(replychatlist); + if ( bot_developer ) { + BotCheckReplyChatIntegrety( replychatlist ); + } //end if + // + if ( !replychatlist ) { + botimport.Print( PRT_MESSAGE, "no rchats\n" ); + } + // + return replychatlist; +} //end of the function BotLoadReplyChat +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotDumpInitialChat( bot_chat_t *chat ) { + bot_chattype_t *t; + bot_chatmessage_t *m; + + Log_Write( "{" ); + for ( t = chat->types; t; t = t->next ) + { + Log_Write( " type \"%s\"", t->name ); + Log_Write( " {" ); + Log_Write( " numchatmessages = %d", t->numchatmessages ); + for ( m = t->firstchatmessage; m; m = m->next ) + { + Log_Write( " \"%s\"", m->chatmessage ); + } //end for + Log_Write( " }" ); + } //end for + Log_Write( "}" ); +} //end of the function BotDumpInitialChat +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_chat_t *BotLoadInitialChat( char *chatfile, char *chatname ) { + int pass, foundchat, indent, size; + char *ptr = NULL; + char chatmessagestring[MAX_MESSAGE_SIZE]; + source_t *source; + token_t token; + bot_chat_t *chat = NULL; + bot_chattype_t *chattype = NULL; + bot_chatmessage_t *chatmessage = NULL; +#ifdef DEBUG + int starttime; + + starttime = Sys_MilliSeconds(); +#endif //DEBUG + // + size = 0; + foundchat = qfalse; + //a bot chat is parsed in two phases + for ( pass = 0; pass < 2; pass++ ) + { + //allocate memory + if ( pass && size ) { + ptr = (char *) GetClearedMemory( size ); + } + //load the source file + source = LoadSourceFile( chatfile ); + if ( !source ) { + botimport.Print( PRT_ERROR, "counldn't load %s\n", chatfile ); + return NULL; + } //end if + //chat structure + if ( pass ) { + chat = (bot_chat_t *) ptr; + ptr += sizeof( bot_chat_t ); + } //end if + size = sizeof( bot_chat_t ); + // + while ( PC_ReadToken( source, &token ) ) + { + if ( !strcmp( token.string, "chat" ) ) { + if ( !PC_ExpectTokenType( source, TT_STRING, 0, &token ) ) { + FreeSource( source ); + return NULL; + } //end if + StripDoubleQuotes( token.string ); + //after the chat name we expect a opening brace + if ( !PC_ExpectTokenString( source, "{" ) ) { + FreeSource( source ); + return NULL; + } //end if + //if the chat name is found + if ( !Q_stricmp( token.string, chatname ) ) { + foundchat = qtrue; + //read the chat types + while ( 1 ) + { + if ( !PC_ExpectAnyToken( source, &token ) ) { + FreeSource( source ); + return NULL; + } //end if + if ( !strcmp( token.string, "}" ) ) { + break; + } + if ( strcmp( token.string, "type" ) ) { + SourceError( source, "expected type found %s\n", token.string ); + FreeSource( source ); + return NULL; + } //end if + //expect the chat type name + if ( !PC_ExpectTokenType( source, TT_STRING, 0, &token ) || + !PC_ExpectTokenString( source, "{" ) ) { + FreeSource( source ); + return NULL; + } //end if + StripDoubleQuotes( token.string ); + if ( pass ) { + chattype = (bot_chattype_t *) ptr; + strncpy( chattype->name, token.string, MAX_CHATTYPE_NAME ); + chattype->firstchatmessage = NULL; + //add the chat type to the chat + chattype->next = chat->types; + chat->types = chattype; + // + ptr += sizeof( bot_chattype_t ); + } //end if + size += sizeof( bot_chattype_t ); + //read the chat messages + while ( !PC_CheckTokenString( source, "}" ) ) + { + if ( !BotLoadChatMessage( source, chatmessagestring ) ) { + FreeSource( source ); + return NULL; + } //end if + if ( pass ) { + chatmessage = (bot_chatmessage_t *) ptr; + chatmessage->time = -2 * CHATMESSAGE_RECENTTIME; + //put the chat message in the list + chatmessage->next = chattype->firstchatmessage; + chattype->firstchatmessage = chatmessage; + //store the chat message + ptr += sizeof( bot_chatmessage_t ); + chatmessage->chatmessage = ptr; + strcpy( chatmessage->chatmessage, chatmessagestring ); + ptr += strlen( chatmessagestring ) + 1; + //the number of chat messages increased + chattype->numchatmessages++; + } //end if + size += sizeof( bot_chatmessage_t ) + strlen( chatmessagestring ) + 1; + } //end if + } //end while + } //end if + else //skip the bot chat + { + indent = 1; + while ( indent ) + { + if ( !PC_ExpectAnyToken( source, &token ) ) { + FreeSource( source ); + return NULL; + } //end if + if ( !strcmp( token.string, "{" ) ) { + indent++; + } else if ( !strcmp( token.string, "}" ) ) { + indent--; + } + } //end while + } //end else + } //end if + else + { + SourceError( source, "unknown definition %s\n", token.string ); + FreeSource( source ); + return NULL; + } //end else + } //end while + //free the source + FreeSource( source ); + //if the requested character is not found + if ( !foundchat ) { + botimport.Print( PRT_ERROR, "couldn't find chat %s in %s\n", chatname, chatfile ); + return NULL; + } //end if + } //end for + // + botimport.Print( PRT_MESSAGE, "loaded %s from %s\n", chatname, chatfile ); + // + //BotDumpInitialChat(chat); + if ( bot_developer ) { + BotCheckInitialChatIntegrety( chat ); + } //end if +#ifdef DEBUG + botimport.Print( PRT_MESSAGE, "initial chats loaded in %d msec\n", Sys_MilliSeconds() - starttime ); +#endif //DEBUG + //character was read succesfully + return chat; +} //end of the function BotLoadInitialChat +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotFreeChatFile( int chatstate ) { + bot_chatstate_t *cs; + + cs = BotChatStateFromHandle( chatstate ); + if ( !cs ) { + return; + } + if ( cs->chat ) { + FreeMemory( cs->chat ); + } + cs->chat = NULL; +} //end of the function BotFreeChatFile +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotLoadChatFile( int chatstate, char *chatfile, char *chatname ) { + bot_chatstate_t *cs; + int n, avail = 0; + + cs = BotChatStateFromHandle( chatstate ); + if ( !cs ) { + return BLERR_CANNOTLOADICHAT; + } + BotFreeChatFile( chatstate ); + + if ( !LibVarGetValue( "bot_reloadcharacters" ) ) { + avail = -1; + for ( n = 0; n < MAX_CLIENTS; n++ ) { + if ( !ichatdata[n].inuse ) { + if ( avail == -1 ) { + avail = n; + } + continue; + } + if ( strcmp( chatfile, ichatdata[n].filename ) != 0 ) { + continue; + } + if ( strcmp( chatname, ichatdata[n].chatname ) != 0 ) { + continue; + } + cs->chat = ichatdata[n].chat; + // botimport.Print( PRT_MESSAGE, "retained %s from %s\n", chatname, chatfile ); + return BLERR_NOERROR; + } + + if ( avail == -1 ) { + botimport.Print( PRT_FATAL, "ichatdata table full; couldn't load chat %s from %s\n", chatname, chatfile ); + return BLERR_CANNOTLOADICHAT; + } + } + + cs->chat = BotLoadInitialChat( chatfile, chatname ); + if ( !cs->chat ) { + botimport.Print( PRT_FATAL, "couldn't load chat %s from %s\n", chatname, chatfile ); + return BLERR_CANNOTLOADICHAT; + } //end if + if ( !LibVarGetValue( "bot_reloadcharacters" ) ) { + ichatdata[avail].chat = cs->chat; + Q_strncpyz( ichatdata[avail].chatname, chatname, sizeof( ichatdata[avail].chatname ) ); + Q_strncpyz( ichatdata[avail].filename, chatfile, sizeof( ichatdata[avail].filename ) ); + ichatdata[avail].inuse = qtrue; + } //end if + + return BLERR_NOERROR; +} //end of the function BotLoadChatFile +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotExpandChatMessage( char *outmessage, char *message, unsigned long mcontext, + bot_matchvariable_t *variables, unsigned long vcontext, int reply ) { + int num, len, i, expansion; + char *outputbuf, *ptr, *msgptr; + char temp[MAX_MESSAGE_SIZE]; + + expansion = qfalse; + msgptr = message; + outputbuf = outmessage; + len = 0; + // + while ( *msgptr ) + { + if ( *msgptr == ESCAPE_CHAR ) { + msgptr++; + switch ( *msgptr ) + { + case 'v': //variable + { + msgptr++; + num = 0; + while ( *msgptr && *msgptr != ESCAPE_CHAR ) + { + num = num * 10 + ( *msgptr++ ) - '0'; + } //end while + //step over the trailing escape char + if ( *msgptr ) { + msgptr++; + } + if ( num > MAX_MATCHVARIABLES ) { + botimport.Print( PRT_ERROR, "BotConstructChat: message %s variable %d out of range\n", message, num ); + return qfalse; + } //end if + ptr = variables[num].ptr; + if ( ptr ) { + for ( i = 0; i < variables[num].length; i++ ) + { + temp[i] = ptr[i]; + } //end for + temp[i] = 0; + //if it's a reply message + if ( reply ) { + //replace the reply synonyms in the variables + BotReplaceReplySynonyms( temp, vcontext ); + } //end if + else + { + //replace synonyms in the variable context + BotReplaceSynonyms( temp, vcontext ); + } //end else + // + if ( len + strlen( temp ) >= MAX_MESSAGE_SIZE ) { + botimport.Print( PRT_ERROR, "BotConstructChat: message %s too long\n", message ); + return qfalse; + } //end if + strcpy( &outputbuf[len], temp ); + len += strlen( temp ); + } //end if + break; + } //end case + case 'r': //random + { + msgptr++; + for ( i = 0; ( *msgptr && *msgptr != ESCAPE_CHAR ); i++ ) + { + temp[i] = *msgptr++; + } //end while + temp[i] = '\0'; + //step over the trailing escape char + if ( *msgptr ) { + msgptr++; + } + //find the random keyword + ptr = RandomString( temp ); + if ( !ptr ) { + botimport.Print( PRT_ERROR, "BotConstructChat: unknown random string %s\n", temp ); + return qfalse; + } //end if + if ( len + strlen( ptr ) >= MAX_MESSAGE_SIZE ) { + botimport.Print( PRT_ERROR, "BotConstructChat: message \"%s\" too long\n", message ); + return qfalse; + } //end if + strcpy( &outputbuf[len], ptr ); + len += strlen( ptr ); + expansion = qtrue; + break; + } //end case + default: + { + botimport.Print( PRT_FATAL, "BotConstructChat: message \"%s\" invalid escape char\n", message ); + break; + } //end default + } //end switch + } //end if + else + { + outputbuf[len++] = *msgptr++; + if ( len >= MAX_MESSAGE_SIZE ) { + botimport.Print( PRT_ERROR, "BotConstructChat: message \"%s\" too long\n", message ); + break; + } //end if + } //end else + } //end while + outputbuf[len] = '\0'; + //replace synonyms weighted in the message context + BotReplaceWeightedSynonyms( outputbuf, mcontext ); + //return true if a random was expanded + return expansion; +} //end of the function BotExpandChatMessage +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotConstructChatMessage( bot_chatstate_t *chatstate, char *message, unsigned long mcontext, + bot_matchvariable_t *variables, unsigned long vcontext, int reply ) { + int i; + char srcmessage[MAX_MESSAGE_SIZE]; + + strcpy( srcmessage, message ); + for ( i = 0; i < 10; i++ ) + { + if ( !BotExpandChatMessage( chatstate->chatmessage, srcmessage, mcontext, variables, vcontext, reply ) ) { + break; + } //end if + strcpy( srcmessage, chatstate->chatmessage ); + } //end for + if ( i >= 10 ) { + botimport.Print( PRT_WARNING, "too many expansions in chat message\n" ); + botimport.Print( PRT_WARNING, "%s\n", chatstate->chatmessage ); + } //end if +} //end of the function BotConstructChatMessage +//=========================================================================== +// randomly chooses one of the chat message of the given type +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +char *BotChooseInitialChatMessage( bot_chatstate_t *cs, char *type ) { + int n, numchatmessages; + float besttime; + bot_chattype_t *t; + bot_chatmessage_t *m, *bestchatmessage; + bot_chat_t *chat; + + chat = cs->chat; + for ( t = chat->types; t; t = t->next ) + { + if ( !Q_stricmp( t->name, type ) ) { + numchatmessages = 0; + for ( m = t->firstchatmessage; m; m = m->next ) + { + if ( m->time > AAS_Time() ) { + continue; + } + numchatmessages++; + } //end if + //if all chat messages have been used recently + if ( numchatmessages <= 0 ) { + besttime = 0; + bestchatmessage = NULL; + for ( m = t->firstchatmessage; m; m = m->next ) + { + if ( !besttime || m->time < besttime ) { + bestchatmessage = m; + besttime = m->time; + } //end if + } //end for + if ( bestchatmessage ) { + return bestchatmessage->chatmessage; + } + } //end if + else //choose a chat message randomly + { + n = random() * numchatmessages; + for ( m = t->firstchatmessage; m; m = m->next ) + { + if ( m->time > AAS_Time() ) { + continue; + } + if ( --n < 0 ) { + m->time = AAS_Time() + CHATMESSAGE_RECENTTIME; + return m->chatmessage; + } //end if + } //end for + } //end else + return NULL; + } //end if + } //end for + return NULL; +} //end of the function BotChooseInitialChatMessage +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotNumInitialChats( int chatstate, char *type ) { + bot_chatstate_t *cs; + bot_chattype_t *t; + + cs = BotChatStateFromHandle( chatstate ); + if ( !cs ) { + return 0; + } + + for ( t = cs->chat->types; t; t = t->next ) + { + if ( !Q_stricmp( t->name, type ) ) { + if ( LibVarGetValue( "bot_testichat" ) ) { + botimport.Print( PRT_MESSAGE, "%s has %d chat lines\n", type, t->numchatmessages ); + botimport.Print( PRT_MESSAGE, "-------------------\n" ); + } + return t->numchatmessages; + } //end if + } //end for + return 0; +} //end of the function BotNumInitialChats +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotInitialChat( int chatstate, char *type, int mcontext, char *var0, char *var1, char *var2, char *var3, char *var4, char *var5, char *var6, char *var7 ) { + char *message; + bot_matchvariable_t variables[MAX_MATCHVARIABLES]; + bot_chatstate_t *cs; + + cs = BotChatStateFromHandle( chatstate ); + if ( !cs ) { + return; + } + //if no chat file is loaded + if ( !cs->chat ) { + return; + } + //choose a chat message randomly of the given type + message = BotChooseInitialChatMessage( cs, type ); + //if there's no message of the given type + if ( !message ) { +#ifdef DEBUG + botimport.Print( PRT_MESSAGE, "no chat messages of type %s\n", type ); +#endif //DEBUG + return; + } //end if + // + memset( variables, 0, sizeof( variables ) ); + if ( var0 ) { + variables[0].ptr = var0; + variables[0].length = strlen( var0 ); + } + if ( var1 ) { + variables[1].ptr = var1; + variables[1].length = strlen( var1 ); + } + if ( var2 ) { + variables[2].ptr = var2; + variables[2].length = strlen( var2 ); + } + if ( var3 ) { + variables[3].ptr = var3; + variables[3].length = strlen( var3 ); + } + if ( var4 ) { + variables[4].ptr = var4; + variables[4].length = strlen( var4 ); + } + if ( var5 ) { + variables[5].ptr = var5; + variables[5].length = strlen( var5 ); + } + if ( var6 ) { + variables[6].ptr = var6; + variables[6].length = strlen( var6 ); + } + if ( var7 ) { + variables[7].ptr = var7; + variables[7].length = strlen( var7 ); + } + // + BotConstructChatMessage( cs, message, mcontext, variables, 0, qfalse ); +} //end of the function BotInitialChat +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotPrintReplyChatKeys( bot_replychat_t *replychat ) { + bot_replychatkey_t *key; + bot_matchpiece_t *mp; + + botimport.Print( PRT_MESSAGE, "[" ); + for ( key = replychat->keys; key; key = key->next ) + { + if ( key->flags & RCKFL_AND ) { + botimport.Print( PRT_MESSAGE, "&" ); + } else if ( key->flags & RCKFL_NOT ) { + botimport.Print( PRT_MESSAGE, "!" ); + } + // + if ( key->flags & RCKFL_NAME ) { + botimport.Print( PRT_MESSAGE, "name" ); + } else if ( key->flags & RCKFL_GENDERFEMALE ) { + botimport.Print( PRT_MESSAGE, "female" ); + } else if ( key->flags & RCKFL_GENDERMALE ) { + botimport.Print( PRT_MESSAGE, "male" ); + } else if ( key->flags & RCKFL_GENDERLESS ) { + botimport.Print( PRT_MESSAGE, "it" ); + } else if ( key->flags & RCKFL_VARIABLES ) { + botimport.Print( PRT_MESSAGE, "(" ); + for ( mp = key->match; mp; mp = mp->next ) + { + if ( mp->type == MT_STRING ) { + botimport.Print( PRT_MESSAGE, "\"%s\"", mp->firststring->string ); + } else { botimport.Print( PRT_MESSAGE, "%d", mp->variable );} + if ( mp->next ) { + botimport.Print( PRT_MESSAGE, ", " ); + } + } //end for + botimport.Print( PRT_MESSAGE, ")" ); + } //end if + else if ( key->flags & RCKFL_STRING ) { + botimport.Print( PRT_MESSAGE, "\"%s\"", key->string ); + } //end if + if ( key->next ) { + botimport.Print( PRT_MESSAGE, ", " ); + } else { botimport.Print( PRT_MESSAGE, "] = %1.0f\n", replychat->priority );} + } //end for + botimport.Print( PRT_MESSAGE, "{\n" ); +} //end of the function BotPrintReplyChatKeys +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotReplyChat( int chatstate, char *message, int mcontext, int vcontext, char *var0, char *var1, char *var2, char *var3, char *var4, char *var5, char *var6, char *var7 ) { + bot_replychat_t *rchat, *bestrchat; + bot_replychatkey_t *key; + bot_chatmessage_t *m, *bestchatmessage; + bot_match_t match, bestmatch; + int bestpriority, num, found, res, numchatmessages; + bot_chatstate_t *cs; + + cs = BotChatStateFromHandle( chatstate ); + if ( !cs ) { + return qfalse; + } + memset( &match, 0, sizeof( bot_match_t ) ); + strcpy( match.string, message ); + bestpriority = -1; + bestchatmessage = NULL; + bestrchat = NULL; + //go through all the reply chats + for ( rchat = replychats; rchat; rchat = rchat->next ) + { + found = qfalse; + for ( key = rchat->keys; key; key = key->next ) + { + res = qfalse; + //get the match result + if ( key->flags & RCKFL_NAME ) { + res = ( StringContains( message, cs->name, qfalse ) != -1 ); + } else if ( key->flags & RCKFL_BOTNAMES ) { + res = ( StringContains( key->string, cs->name, qfalse ) != -1 ); + } else if ( key->flags & RCKFL_GENDERFEMALE ) { + res = ( cs->gender == CHAT_GENDERFEMALE ); + } else if ( key->flags & RCKFL_GENDERMALE ) { + res = ( cs->gender == CHAT_GENDERMALE ); + } else if ( key->flags & RCKFL_GENDERLESS ) { + res = ( cs->gender == CHAT_GENDERLESS ); + } else if ( key->flags & RCKFL_VARIABLES ) { + res = StringsMatch( key->match, &match ); + } else if ( key->flags & RCKFL_STRING ) { + res = ( StringContainsWord( message, key->string, qfalse ) != NULL ); + } + //if the key must be present + if ( key->flags & RCKFL_AND ) { + if ( !res ) { + found = qfalse; + break; + } //end if + //botnames is an exception + //if (!(key->flags & RCKFL_BOTNAMES)) found = qtrue; + } //end else if + //if the key must be absent + else if ( key->flags & RCKFL_NOT ) { + if ( res ) { + found = qfalse; + break; + } //end if + } //end if + else if ( res ) { + found = qtrue; + } //end else + } //end for + // + if ( found ) { + if ( rchat->priority > bestpriority ) { + numchatmessages = 0; + for ( m = rchat->firstchatmessage; m; m = m->next ) + { + if ( m->time > AAS_Time() ) { + continue; + } + numchatmessages++; + } //end if + num = random() * numchatmessages; + for ( m = rchat->firstchatmessage; m; m = m->next ) + { + if ( --num < 0 ) { + break; + } + if ( m->time > AAS_Time() ) { + continue; + } + } //end for + //if the reply chat has a message + if ( m ) { + memcpy( &bestmatch, &match, sizeof( bot_match_t ) ); + bestchatmessage = m; + bestrchat = rchat; + bestpriority = rchat->priority; + } //end if + } //end if + } //end if + } //end for + if ( bestchatmessage ) { + if ( var0 ) { + bestmatch.variables[0].ptr = var0; + bestmatch.variables[0].length = strlen( var0 ); + } + if ( var1 ) { + bestmatch.variables[1].ptr = var1; + bestmatch.variables[1].length = strlen( var1 ); + } + if ( var2 ) { + bestmatch.variables[2].ptr = var2; + bestmatch.variables[2].length = strlen( var2 ); + } + if ( var3 ) { + bestmatch.variables[3].ptr = var3; + bestmatch.variables[3].length = strlen( var3 ); + } + if ( var4 ) { + bestmatch.variables[4].ptr = var4; + bestmatch.variables[4].length = strlen( var4 ); + } + if ( var5 ) { + bestmatch.variables[5].ptr = var5; + bestmatch.variables[5].length = strlen( var5 ); + } + if ( var6 ) { + bestmatch.variables[6].ptr = var6; + bestmatch.variables[6].length = strlen( var6 ); + } + if ( var7 ) { + bestmatch.variables[7].ptr = var7; + bestmatch.variables[7].length = strlen( var7 ); + } + if ( LibVarGetValue( "bot_testrchat" ) ) { + for ( m = bestrchat->firstchatmessage; m; m = m->next ) + { + BotConstructChatMessage( cs, m->chatmessage, mcontext, bestmatch.variables, vcontext, qtrue ); + BotRemoveTildes( cs->chatmessage ); + botimport.Print( PRT_MESSAGE, "%s\n", cs->chatmessage ); + } //end if + } //end if + else + { + bestchatmessage->time = AAS_Time() + CHATMESSAGE_RECENTTIME; + BotConstructChatMessage( cs, bestchatmessage->chatmessage, mcontext, bestmatch.variables, vcontext, qtrue ); + } //end else + return qtrue; + } //end if + return qfalse; +} //end of the function BotReplyChat +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotChatLength( int chatstate ) { + bot_chatstate_t *cs; + + cs = BotChatStateFromHandle( chatstate ); + if ( !cs ) { + return 0; + } + return strlen( cs->chatmessage ); +} //end of the function BotChatLength +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotEnterChat( int chatstate, int client, int sendto ) { + bot_chatstate_t *cs; + + cs = BotChatStateFromHandle( chatstate ); + if ( !cs ) { + return; + } + + if ( strlen( cs->chatmessage ) ) { + BotRemoveTildes( cs->chatmessage ); + if ( LibVarGetValue( "bot_testichat" ) ) { + botimport.Print( PRT_MESSAGE, "%s\n", cs->chatmessage ); + } else { + if ( sendto == CHAT_TEAM ) { + EA_SayTeam( client, cs->chatmessage ); + } else { EA_Say( client, cs->chatmessage );} + } + //clear the chat message from the state + strcpy( cs->chatmessage, "" ); + } //end if +} //end of the function BotEnterChat +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotGetChatMessage( int chatstate, char *buf, int size ) { + bot_chatstate_t *cs; + + cs = BotChatStateFromHandle( chatstate ); + if ( !cs ) { + return; + } + + BotRemoveTildes( cs->chatmessage ); + strncpy( buf, cs->chatmessage, size - 1 ); + buf[size - 1] = '\0'; + //clear the chat message from the state + strcpy( cs->chatmessage, "" ); +} //end of the function BotGetChatMessage +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotSetChatGender( int chatstate, int gender ) { + bot_chatstate_t *cs; + + cs = BotChatStateFromHandle( chatstate ); + if ( !cs ) { + return; + } + switch ( gender ) + { + case CHAT_GENDERFEMALE: cs->gender = CHAT_GENDERFEMALE; break; + case CHAT_GENDERMALE: cs->gender = CHAT_GENDERMALE; break; + default: cs->gender = CHAT_GENDERLESS; break; + } //end switch +} //end of the function BotSetChatGender +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotSetChatName( int chatstate, char *name ) { + bot_chatstate_t *cs; + + cs = BotChatStateFromHandle( chatstate ); + if ( !cs ) { + return; + } + memset( cs->name, 0, sizeof( cs->name ) ); + strncpy( cs->name, name, sizeof( cs->name ) ); + cs->name[sizeof( cs->name ) - 1] = '\0'; +} //end of the function BotSetChatName +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotResetChatAI( void ) { + bot_replychat_t *rchat; + bot_chatmessage_t *m; + + for ( rchat = replychats; rchat; rchat = rchat->next ) + { + for ( m = rchat->firstchatmessage; m; m = m->next ) + { + m->time = 0; + } //end for + } //end for +} //end of the function BotResetChatAI +//======================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//======================================================================== +int BotAllocChatState( void ) { + int i; + + for ( i = 1; i <= MAX_CLIENTS; i++ ) + { + if ( !botchatstates[i] ) { + botchatstates[i] = GetClearedMemory( sizeof( bot_chatstate_t ) ); + return i; + } //end if + } //end for + return 0; +} //end of the function BotAllocChatState +//======================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//======================================================================== +void BotFreeChatState( int handle ) { + bot_chatstate_t *cs; + bot_consolemessage_t m; + int h; + + if ( handle <= 0 || handle > MAX_CLIENTS ) { + botimport.Print( PRT_FATAL, "chat state handle %d out of range\n", handle ); + return; + } //end if + if ( !botchatstates[handle] ) { + botimport.Print( PRT_FATAL, "invalid chat state %d\n", handle ); + return; + } //end if + cs = botchatstates[handle]; + if ( LibVarGetValue( "bot_reloadcharacters" ) ) { + BotFreeChatFile( handle ); + } //end if + //free all the console messages left in the chat state + for ( h = BotNextConsoleMessage( handle, &m ); h; h = BotNextConsoleMessage( handle, &m ) ) + { + //remove the console message + BotRemoveConsoleMessage( handle, h ); + } //end for + FreeMemory( botchatstates[handle] ); + botchatstates[handle] = NULL; +} //end of the function BotFreeChatState +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotSetupChatAI( void ) { + char *file; + +#ifdef DEBUG + int starttime = Sys_MilliSeconds(); +#endif //DEBUG + + file = LibVarString( "synfile", "syn.c" ); + synonyms = BotLoadSynonyms( file ); + file = LibVarString( "rndfile", "rnd.c" ); + randomstrings = BotLoadRandomStrings( file ); + file = LibVarString( "matchfile", "match.c" ); + matchtemplates = BotLoadMatchTemplates( file ); + // + if ( !LibVarValue( "nochat", "0" ) ) { + file = LibVarString( "rchatfile", "rchat.c" ); + replychats = BotLoadReplyChat( file ); + } //end if + + InitConsoleMessageHeap(); + +#ifdef DEBUG + botimport.Print( PRT_MESSAGE, "setup chat AI %d msec\n", Sys_MilliSeconds() - starttime ); +#endif //DEBUG + return BLERR_NOERROR; +} //end of the function BotSetupChatAI +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotShutdownChatAI( void ) { + int i; + + //free all remaining chat states + for ( i = 0; i < MAX_CLIENTS; i++ ) + { + if ( botchatstates[i] ) { + BotFreeChatState( i ); + } //end if + } //end for + //free all cached chats + for ( i = 0; i < MAX_CLIENTS; i++ ) + { + if ( ichatdata[i].inuse ) { + FreeMemory( ichatdata[i].chat ); + ichatdata[i].inuse = qfalse; + } //end if + } //end for + if ( consolemessageheap ) { + FreeMemory( consolemessageheap ); + } + consolemessageheap = NULL; + if ( matchtemplates ) { + BotFreeMatchTemplates( matchtemplates ); + } + matchtemplates = NULL; + if ( randomstrings ) { + FreeMemory( randomstrings ); + } + randomstrings = NULL; + if ( synonyms ) { + FreeMemory( synonyms ); + } + synonyms = NULL; + if ( replychats ) { + BotFreeReplyChat( replychats ); + } + replychats = NULL; +} //end of the function BotShutdownChatAI diff --git a/src/botlib/be_ai_gen.c b/src/botlib/be_ai_gen.c new file mode 100644 index 0000000..697f030 --- /dev/null +++ b/src/botlib/be_ai_gen.c @@ -0,0 +1,151 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_ai_gen.c + * + * desc: genetic selection + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "l_memory.h" +#include "l_log.h" +#include "l_utils.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "aasfile.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "be_aas_funcs.h" +#include "be_interface.h" +#include "../game/be_ai_gen.h" + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int GeneticSelection( int numranks, float *rankings ) { + float sum, select; + int i, index; + + sum = 0; + for ( i = 0; i < numranks; i++ ) + { + if ( rankings[i] < 0 ) { + continue; + } + sum += rankings[i]; + } //end for + if ( sum > 0 ) { + //select a bot where the ones with the higest rankings have + //the highest chance of being selected + select = random() * sum; + for ( i = 0; i < numranks; i++ ) + { + if ( rankings[i] < 0 ) { + continue; + } + sum -= rankings[i]; + if ( sum <= 0 ) { + return i; + } + } //end for + } //end if + //select a bot randomly + index = random() * numranks; + for ( i = 0; i < numranks; i++ ) + { + if ( rankings[index] >= 0 ) { + return index; + } + index = ( index + 1 ) % numranks; + } //end for + return 0; +} //end of the function GeneticSelection +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int GeneticParentsAndChildSelection( int numranks, float *ranks, int *parent1, int *parent2, int *child ) { + float rankings[256], max; + int i; + + if ( numranks > 256 ) { + botimport.Print( PRT_WARNING, "GeneticParentsAndChildSelection: too many bots\n" ); + *parent1 = *parent2 = *child = 0; + return qfalse; + } //end if + for ( max = 0, i = 0; i < numranks; i++ ) + { + if ( ranks[i] < 0 ) { + continue; + } + max++; + } //end for + if ( max < 3 ) { + botimport.Print( PRT_WARNING, "GeneticParentsAndChildSelection: too few valid bots\n" ); + *parent1 = *parent2 = *child = 0; + return qfalse; + } //end if + memcpy( rankings, ranks, sizeof( float ) * numranks ); + //select first parent + *parent1 = GeneticSelection( numranks, rankings ); + rankings[*parent1] = -1; + //select second parent + *parent2 = GeneticSelection( numranks, rankings ); + rankings[*parent2] = -1; + //reverse the rankings + max = 0; + for ( i = 0; i < numranks; i++ ) + { + if ( rankings[i] < 0 ) { + continue; + } + if ( rankings[i] > max ) { + max = rankings[i]; + } + } //end for + for ( i = 0; i < numranks; i++ ) + { + if ( rankings[i] < 0 ) { + continue; + } + rankings[i] = max - rankings[i]; + } //end for + //select child + *child = GeneticSelection( numranks, rankings ); + return qtrue; +} //end of the function GeneticParentsAndChildSelection diff --git a/src/botlib/be_ai_goal.c b/src/botlib/be_ai_goal.c new file mode 100644 index 0000000..6b24cd1 --- /dev/null +++ b/src/botlib/be_ai_goal.c @@ -0,0 +1,1630 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_ai_goal.c + * + * desc: goal AI + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "l_utils.h" +#include "l_libvar.h" +#include "l_memory.h" +#include "l_log.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "aasfile.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "be_aas_funcs.h" +#include "be_interface.h" +#include "be_ai_weight.h" +#include "../game/be_ai_goal.h" +#include "../game/be_ai_move.h" + +//#define DEBUG_AI_GOAL +#ifdef RANDOMIZE +#define UNDECIDEDFUZZY +#endif //RANDOMIZE +#define DROPPEDWEIGHT +//avoid goal time +#define AVOID_TIME 30 +//avoid dropped goal time +#define AVOIDDROPPED_TIME 5 +// +#define TRAVELTIME_SCALE 0.01 + +//location in the map "target_location" +typedef struct maplocation_s +{ + vec3_t origin; + int areanum; + char name[MAX_EPAIRKEY]; + struct maplocation_s *next; +} maplocation_t; + +//camp spots "info_camp" +typedef struct campspot_s +{ + vec3_t origin; + int areanum; + char name[MAX_EPAIRKEY]; + float range; + float weight; + float wait; + float random; + struct campspot_s *next; +} campspot_t; + +//FIXME: these are game specific +typedef enum { + GT_FFA, // free for all + GT_TOURNAMENT, // one on one tournament + GT_SINGLE_PLAYER, // single player tournament + + //-- team games go after this -- + + GT_TEAM, // team deathmatch + GT_CTF, // capture the flag + + GT_MAX_GAME_TYPE +} gametype_t; + +// Rafael gameskill +typedef enum { + GSKILL_EASY, + GSKILL_MEDIUM, + GSKILL_HARD, // normal default level + GSKILL_MAX +} gameskill_t; + +typedef struct levelitem_s +{ + int number; //number of the level item + int iteminfo; //index into the item info + int notteam; //true if not in teamplay + int notfree; //true if not in ffa + int notsingle; //true if not in single + vec3_t origin; //origin of the item + int goalareanum; //area the item is in + vec3_t goalorigin; //goal origin within the area + int entitynum; //entity number + float timeout; //item is removed after this time + struct levelitem_s *prev, *next; +} levelitem_t; + +typedef struct iteminfo_s +{ + char classname[32]; //classname of the item + char name[MAX_STRINGFIELD]; //name of the item + char model[MAX_STRINGFIELD]; //model of the item + int modelindex; //model index + int type; //item type + int index; //index in the inventory + float respawntime; //respawn time + vec3_t mins; //mins of the item + vec3_t maxs; //maxs of the item + int number; //number of the item info +} iteminfo_t; + +#define ITEMINFO_OFS( x ) (int)&( ( (iteminfo_t *)0 )->x ) + +fielddef_t iteminfo_fields[] = +{ + {"name", ITEMINFO_OFS( name ), FT_STRING}, + {"model", ITEMINFO_OFS( model ), FT_STRING}, + {"modelindex", ITEMINFO_OFS( modelindex ), FT_INT}, + {"type", ITEMINFO_OFS( type ), FT_INT}, + {"index", ITEMINFO_OFS( index ), FT_INT}, + {"respawntime", ITEMINFO_OFS( respawntime ), FT_FLOAT}, + {"mins", ITEMINFO_OFS( mins ), FT_FLOAT | FT_ARRAY, 3}, + {"maxs", ITEMINFO_OFS( maxs ), FT_FLOAT | FT_ARRAY, 3}, + {0, 0, 0} +}; + +structdef_t iteminfo_struct = +{ + sizeof( iteminfo_t ), iteminfo_fields +}; + +typedef struct itemconfig_s +{ + int numiteminfo; + iteminfo_t *iteminfo; +} itemconfig_t; + +//goal state +typedef struct bot_goalstate_s +{ + struct weightconfig_s *itemweightconfig; //weight config + int *itemweightindex; //index from item to weight + // + int client; //client using this goal state + int lastreachabilityarea; //last area with reachabilities the bot was in + // + bot_goal_t goalstack[MAX_GOALSTACK]; //goal stack + int goalstacktop; //the top of the goal stack + // + int avoidgoals[MAX_AVOIDGOALS]; //goals to avoid + float avoidgoaltimes[MAX_AVOIDGOALS]; //times to avoid the goals +} bot_goalstate_t; + +bot_goalstate_t *botgoalstates[MAX_CLIENTS + 1]; +//item configuration +itemconfig_t *itemconfig = NULL; +//level items +levelitem_t *levelitemheap = NULL; +levelitem_t *freelevelitems = NULL; +levelitem_t *levelitems = NULL; +int numlevelitems = 0; +//map locations +maplocation_t *maplocations = NULL; +//camp spots +campspot_t *campspots = NULL; +//the game type +int g_gametype; + +// Rafael gameskill +int g_gameskill; +// done + +//======================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//======================================================================== +bot_goalstate_t *BotGoalStateFromHandle( int handle ) { + if ( handle <= 0 || handle > MAX_CLIENTS ) { + botimport.Print( PRT_FATAL, "goal state handle %d out of range\n", handle ); + return NULL; + } //end if + if ( !botgoalstates[handle] ) { + botimport.Print( PRT_FATAL, "invalid goal state %d\n", handle ); + return NULL; + } //end if + return botgoalstates[handle]; +} //end of the function BotGoalStateFromHandle +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotInterbreedGoalFuzzyLogic( int parent1, int parent2, int child ) { + bot_goalstate_t *p1, *p2, *c; + + p1 = BotGoalStateFromHandle( parent1 ); + p2 = BotGoalStateFromHandle( parent2 ); + c = BotGoalStateFromHandle( child ); + + InterbreedWeightConfigs( p1->itemweightconfig, p2->itemweightconfig, + c->itemweightconfig ); +} //end of the function BotInterbreedingGoalFuzzyLogic +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotSaveGoalFuzzyLogic( int goalstate, char *filename ) { + bot_goalstate_t *gs; + + gs = BotGoalStateFromHandle( goalstate ); + + //WriteWeightConfig(filename, gs->itemweightconfig); +} //end of the function BotSaveGoalFuzzyLogic +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotMutateGoalFuzzyLogic( int goalstate, float range ) { + bot_goalstate_t *gs; + + gs = BotGoalStateFromHandle( goalstate ); + + EvolveWeightConfig( gs->itemweightconfig ); +} //end of the function BotMutateGoalFuzzyLogic +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +itemconfig_t *LoadItemConfig( char *filename ) { + int max_iteminfo; + token_t token; + char path[MAX_PATH]; + source_t *source; + itemconfig_t *ic; + iteminfo_t *ii; + + max_iteminfo = (int) LibVarValue( "max_iteminfo", "256" ); + if ( max_iteminfo < 0 ) { + botimport.Print( PRT_ERROR, "max_iteminfo = %d\n", max_iteminfo ); + max_iteminfo = 128; + LibVarSet( "max_iteminfo", "128" ); + } + + strncpy( path, filename, MAX_PATH ); + source = LoadSourceFile( path ); + if ( !source ) { + botimport.Print( PRT_ERROR, "counldn't load %s\n", path ); + return NULL; + } //end if + //initialize item config + ic = (itemconfig_t *) GetClearedHunkMemory( sizeof( itemconfig_t ) + + max_iteminfo * sizeof( iteminfo_t ) ); + ic->iteminfo = ( iteminfo_t * )( (char *) ic + sizeof( itemconfig_t ) ); + ic->numiteminfo = 0; + //parse the item config file + while ( PC_ReadToken( source, &token ) ) + { + if ( !strcmp( token.string, "iteminfo" ) ) { + if ( ic->numiteminfo >= max_iteminfo ) { + SourceError( source, "more than %d item info defined\n", max_iteminfo ); + FreeMemory( ic ); + FreeSource( source ); + return NULL; + } //end if + ii = &ic->iteminfo[ic->numiteminfo]; + memset( ii, 0, sizeof( iteminfo_t ) ); + if ( !PC_ExpectTokenType( source, TT_STRING, 0, &token ) ) { + FreeMemory( ic ); + FreeMemory( source ); + return NULL; + } //end if + StripDoubleQuotes( token.string ); + strncpy( ii->classname, token.string, sizeof( ii->classname ) - 1 ); + if ( !ReadStructure( source, &iteminfo_struct, (char *) ii ) ) { + FreeMemory( ic ); + FreeSource( source ); + return NULL; + } //end if + ii->number = ic->numiteminfo; + ic->numiteminfo++; + } //end if + else + { + SourceError( source, "unknown definition %s\n", token.string ); + FreeMemory( ic ); + FreeSource( source ); + return NULL; + } //end else + } //end while + FreeSource( source ); + // + if ( !ic->numiteminfo ) { + botimport.Print( PRT_WARNING, "no item info loaded\n" ); + } + botimport.Print( PRT_MESSAGE, "loaded %s\n", path ); + return ic; +} //end of the function LoadItemConfig +//=========================================================================== +// index to find the weight function of an iteminfo +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int *ItemWeightIndex( weightconfig_t *iwc, itemconfig_t *ic ) { + int *index, i; + + //initialize item weight index + index = (int *) GetClearedMemory( sizeof( int ) * ic->numiteminfo ); + + for ( i = 0; i < ic->numiteminfo; i++ ) + { + index[i] = FindFuzzyWeight( iwc, ic->iteminfo[i].classname ); + if ( index[i] < 0 ) { + Log_Write( "item info %d \"%s\" has no fuzzy weight\r\n", i, ic->iteminfo[i].classname ); + } //end if + } //end for + return index; +} //end of the function ItemWeightIndex +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void InitLevelItemHeap( void ) { + int i, max_levelitems; + + if ( levelitemheap ) { + FreeMemory( levelitemheap ); + } + + max_levelitems = (int) LibVarValue( "max_levelitems", "256" ); + levelitemheap = (levelitem_t *) GetMemory( max_levelitems * sizeof( levelitem_t ) ); + + for ( i = 0; i < max_levelitems - 2; i++ ) + { + levelitemheap[i].next = &levelitemheap[i + 1]; + } //end for + levelitemheap[max_levelitems - 1].next = NULL; + // + freelevelitems = levelitemheap; +} //end of the function InitLevelItemHeap +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +levelitem_t *AllocLevelItem( void ) { + levelitem_t *li; + + li = freelevelitems; + if ( !li ) { + botimport.Print( PRT_FATAL, "out of level items\n" ); + return NULL; + } //end if + // + freelevelitems = freelevelitems->next; + memset( li, 0, sizeof( levelitem_t ) ); + return li; +} //end of the function AllocLevelItem +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FreeLevelItem( levelitem_t *li ) { + li->next = freelevelitems; + freelevelitems = li; +} //end of the function FreeLevelItem +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AddLevelItemToList( levelitem_t *li ) { + if ( levelitems ) { + levelitems->prev = li; + } + li->prev = NULL; + li->next = levelitems; + levelitems = li; +} //end of the function AddLevelItemToList +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void RemoveLevelItemFromList( levelitem_t *li ) { + if ( li->prev ) { + li->prev->next = li->next; + } else { levelitems = li->next;} + if ( li->next ) { + li->next->prev = li->prev; + } +} //end of the function RemoveLevelItemFromList +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotFreeInfoEntities( void ) { + maplocation_t *ml, *nextml; + campspot_t *cs, *nextcs; + + for ( ml = maplocations; ml; ml = nextml ) + { + nextml = ml->next; + FreeMemory( ml ); + } //end for + maplocations = NULL; + for ( cs = campspots; cs; cs = nextcs ) + { + nextcs = cs->next; + FreeMemory( cs ); + } //end for + campspots = NULL; +} //end of the function BotFreeInfoEntities +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotInitInfoEntities( void ) { + char classname[MAX_EPAIRKEY]; + maplocation_t *ml; + campspot_t *cs; + int ent, numlocations, numcampspots; + + BotFreeInfoEntities(); + // + numlocations = 0; + numcampspots = 0; + for ( ent = AAS_NextBSPEntity( 0 ); ent; ent = AAS_NextBSPEntity( ent ) ) + { + if ( !AAS_ValueForBSPEpairKey( ent, "classname", classname, MAX_EPAIRKEY ) ) { + continue; + } + + //map locations + if ( !strcmp( classname, "target_location" ) ) { + ml = (maplocation_t *) GetClearedMemory( sizeof( maplocation_t ) ); + AAS_VectorForBSPEpairKey( ent, "origin", ml->origin ); + AAS_ValueForBSPEpairKey( ent, "message", ml->name, sizeof( ml->name ) ); + ml->areanum = AAS_PointAreaNum( ml->origin ); + ml->next = maplocations; + maplocations = ml; + numlocations++; + } //end if + //camp spots + else if ( !strcmp( classname, "info_camp" ) ) { + cs = (campspot_t *) GetClearedMemory( sizeof( campspot_t ) ); + AAS_VectorForBSPEpairKey( ent, "origin", cs->origin ); + //cs->origin[2] += 16; + AAS_ValueForBSPEpairKey( ent, "message", cs->name, sizeof( cs->name ) ); + AAS_FloatForBSPEpairKey( ent, "range", &cs->range ); + AAS_FloatForBSPEpairKey( ent, "weight", &cs->weight ); + AAS_FloatForBSPEpairKey( ent, "wait", &cs->wait ); + AAS_FloatForBSPEpairKey( ent, "random", &cs->random ); + cs->areanum = AAS_PointAreaNum( cs->origin ); + if ( !cs->areanum ) { + botimport.Print( PRT_MESSAGE, "camp spot at %1.1f %1.1f %1.1f in solid\n", cs->origin[0], cs->origin[1], cs->origin[2] ); + FreeMemory( cs ); + continue; + } //end if + cs->next = campspots; + campspots = cs; + //AAS_DrawPermanentCross(cs->origin, 4, LINECOLOR_YELLOW); + numcampspots++; + } //end else if + } //end for + if ( bot_developer ) { + botimport.Print( PRT_MESSAGE, "%d map locations\n", numlocations ); + botimport.Print( PRT_MESSAGE, "%d camp spots\n", numcampspots ); + } //end if +} //end of the function BotInitInfoEntities +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotInitLevelItems( void ) { + int i, spawnflags; + char classname[MAX_EPAIRKEY]; + vec3_t origin; + int ent; + itemconfig_t *ic; + levelitem_t *li; + + //initialize the map locations and camp spots + BotInitInfoEntities(); + + //initialize the level item heap + InitLevelItemHeap(); + levelitems = NULL; + numlevelitems = 0; + // + ic = itemconfig; + if ( !ic ) { + return; + } + + //if there's no AAS file loaded + if ( !AAS_Loaded() ) { + return; + } + + //update the modelindexes of the item info + for ( i = 0; i < ic->numiteminfo; i++ ) + { + //ic->iteminfo[i].modelindex = AAS_IndexFromModel(ic->iteminfo[i].model); + if ( !ic->iteminfo[i].modelindex ) { + Log_Write( "item %s has modelindex 0", ic->iteminfo[i].classname ); + } //end if + } //end for + + for ( ent = AAS_NextBSPEntity( 0 ); ent; ent = AAS_NextBSPEntity( ent ) ) + { + if ( !AAS_ValueForBSPEpairKey( ent, "classname", classname, MAX_EPAIRKEY ) ) { + continue; + } + // + spawnflags = 0; + AAS_IntForBSPEpairKey( ent, "spawnflags", &spawnflags ); + //FIXME: don't do this + // for now skip all floating entities + if ( spawnflags & 1 ) { + continue; + } + // + for ( i = 0; i < ic->numiteminfo; i++ ) + { + if ( !strcmp( classname, ic->iteminfo[i].classname ) ) { + //get the origin of the item + if ( AAS_VectorForBSPEpairKey( ent, "origin", origin ) ) { + li = AllocLevelItem(); + if ( !li ) { + return; + } + // + li->number = ++numlevelitems; + li->timeout = 0; + li->entitynum = 0; + // + AAS_IntForBSPEpairKey( ent, "notfree", &li->notfree ); + AAS_IntForBSPEpairKey( ent, "notteam", &li->notteam ); + AAS_IntForBSPEpairKey( ent, "notsingle", &li->notsingle ); + //if not a stationary item + if ( !( spawnflags & 1 ) ) { + if ( !AAS_DropToFloor( origin, ic->iteminfo[i].mins, ic->iteminfo[i].maxs ) ) { + botimport.Print( PRT_MESSAGE, "%s in solid at (%1.1f %1.1f %1.1f)\n", + classname, origin[0], origin[1], origin[2] ); + } //end if + } //end if + //item info of the level item + li->iteminfo = i; + //origin of the item + VectorCopy( origin, li->origin ); + //get the item goal area and goal origin + li->goalareanum = AAS_BestReachableArea( origin, + ic->iteminfo[i].mins, ic->iteminfo[i].maxs, + li->goalorigin ); + // + AddLevelItemToList( li ); + } //end if + else + { + botimport.Print( PRT_ERROR, "item %s without origin\n", classname ); + } //end else + break; + } //end if + } //end for + if ( i >= ic->numiteminfo ) { + Log_Write( "entity %s unknown item\r\n", classname ); + } //end if + } //end for + botimport.Print( PRT_MESSAGE, "found %d level items\n", numlevelitems ); +} //end of the function BotInitLevelItems +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotGoalName( int number, char *name, int size ) { + levelitem_t *li; + + if ( !itemconfig ) { + return; + } + // + for ( li = levelitems; li; li = li->next ) + { + if ( li->number == number ) { + strncpy( name, itemconfig->iteminfo[li->iteminfo].name, size - 1 ); + name[size - 1] = '\0'; + return; + } //end for + } //end for + strcpy( name, "" ); + return; +} //end of the function BotGoalName +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotResetAvoidGoals( int goalstate ) { + bot_goalstate_t *gs; + + gs = BotGoalStateFromHandle( goalstate ); + if ( !gs ) { + return; + } + memset( gs->avoidgoals, 0, MAX_AVOIDGOALS * sizeof( int ) ); + memset( gs->avoidgoaltimes, 0, MAX_AVOIDGOALS * sizeof( float ) ); +} //end of the function BotResetAvoidGoals +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotDumpAvoidGoals( int goalstate ) { + int i; + bot_goalstate_t *gs; + char name[32]; + + gs = BotGoalStateFromHandle( goalstate ); + if ( !gs ) { + return; + } + for ( i = 0; i < MAX_AVOIDGOALS; i++ ) + { + if ( gs->avoidgoaltimes[i] >= AAS_Time() ) { + BotGoalName( gs->avoidgoals[i], name, 32 ); + Log_Write( "avoid goal %s, number %d for %f seconds", name, + gs->avoidgoals[i], gs->avoidgoaltimes[i] - AAS_Time() ); + } //end if + } //end for +} //end of the function BotDumpAvoidGoals +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotAddToAvoidGoals( bot_goalstate_t *gs, int number, float avoidtime ) { + int i; + + for ( i = 0; i < MAX_AVOIDGOALS; i++ ) + { + //if this avoid goal has expired + if ( gs->avoidgoaltimes[i] < AAS_Time() ) { + gs->avoidgoals[i] = number; + gs->avoidgoaltimes[i] = AAS_Time() + avoidtime; + return; + } //end if + } //end for +} //end of the function BotAddToAvoidGoals +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotRemoveFromAvoidGoals( int goalstate, int number ) { + int i; + bot_goalstate_t *gs; + + gs = BotGoalStateFromHandle( goalstate ); + if ( !gs ) { + return; + } + //don't use the goals the bot wants to avoid + for ( i = 0; i < MAX_AVOIDGOALS; i++ ) + { + if ( gs->avoidgoals[i] == number && gs->avoidgoaltimes[i] >= AAS_Time() ) { + gs->avoidgoaltimes[i] = 0; + return; + } //end if + } //end for +} //end of the function BotRemoveFromAvoidGoals +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float BotAvoidGoalTime( int goalstate, int number ) { + int i; + bot_goalstate_t *gs; + + gs = BotGoalStateFromHandle( goalstate ); + if ( !gs ) { + return 0; + } + //don't use the goals the bot wants to avoid + for ( i = 0; i < MAX_AVOIDGOALS; i++ ) + { + if ( gs->avoidgoals[i] == number && gs->avoidgoaltimes[i] >= AAS_Time() ) { + return gs->avoidgoaltimes[i] - AAS_Time(); + } //end if + } //end for + return 0; +} //end of the function BotAvoidGoalTime +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotGetLevelItemGoal( int index, char *name, bot_goal_t *goal ) { + levelitem_t *li; + + if ( !itemconfig ) { + return -1; + } + for ( li = levelitems; li; li = li->next ) + { + if ( li->number <= index ) { + continue; + } + // + if ( g_gametype == GT_SINGLE_PLAYER ) { + if ( li->notsingle ) { + continue; + } + } else if ( g_gametype >= GT_TEAM ) { + if ( li->notteam ) { + continue; + } + } else { + if ( li->notfree ) { + continue; + } + } + // + if ( !Q_stricmp( name, itemconfig->iteminfo[li->iteminfo].name ) ) { + goal->areanum = li->goalareanum; + VectorCopy( li->goalorigin, goal->origin ); + goal->entitynum = li->entitynum; + VectorCopy( itemconfig->iteminfo[li->iteminfo].mins, goal->mins ); + VectorCopy( itemconfig->iteminfo[li->iteminfo].maxs, goal->maxs ); + goal->number = li->number; + //botimport.Print(PRT_MESSAGE, "found li %s\n", itemconfig->iteminfo[li->iteminfo].name); + return li->number; + } //end if + } //end for + return -1; +} //end of the function BotGetLevelItemGoal +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotGetMapLocationGoal( char *name, bot_goal_t *goal ) { + maplocation_t *ml; + vec3_t mins = {-8, -8, -8}, maxs = {8, 8, 8}; + + for ( ml = maplocations; ml; ml = ml->next ) + { + if ( !Q_stricmp( ml->name, name ) ) { + goal->areanum = ml->areanum; + VectorCopy( ml->origin, goal->origin ); + goal->entitynum = 0; + VectorCopy( mins, goal->mins ); + VectorCopy( maxs, goal->maxs ); + return qtrue; + } //end if + } //end for + return qfalse; +} //end of the function BotGetMapLocationGoal +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotGetNextCampSpotGoal( int num, bot_goal_t *goal ) { + int i; + campspot_t *cs; + vec3_t mins = {-8, -8, -8}, maxs = {8, 8, 8}; + + if ( num < 0 ) { + num = 0; + } + i = num; + for ( cs = campspots; cs; cs = cs->next ) + { + if ( --i < 0 ) { + goal->areanum = cs->areanum; + VectorCopy( cs->origin, goal->origin ); + goal->entitynum = 0; + VectorCopy( mins, goal->mins ); + VectorCopy( maxs, goal->maxs ); + return num + 1; + } //end if + } //end for + return 0; +} //end of the function BotGetNextCampSpotGoal +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== + +//NOTE: enum entityType_t in bg_public.h +#define ET_ITEM 2 + +void BotUpdateEntityItems( void ) { + int ent, i, modelindex; + vec3_t dir; + levelitem_t *li, *nextli; + aas_entityinfo_t entinfo; + itemconfig_t *ic; + + //timeout current entity items if necessary + for ( li = levelitems; li; li = nextli ) + { + nextli = li->next; + //if it is a item that will time out + if ( li->timeout ) { + //timeout the item + if ( li->timeout < AAS_Time() ) { + RemoveLevelItemFromList( li ); + FreeLevelItem( li ); + } //end if + } //end if + } //end for + //find new entity items + ic = itemconfig; + if ( !itemconfig ) { + return; + } + // + for ( ent = AAS_NextEntity( 0 ); ent; ent = AAS_NextEntity( ent ) ) + { + if ( AAS_EntityType( ent ) != ET_ITEM ) { + continue; + } + //get the model index of the entity + modelindex = AAS_EntityModelindex( ent ); + // + if ( !modelindex ) { + continue; + } + //get info about the entity + AAS_EntityInfo( ent, &entinfo ); + //FIXME: don't do this + //skip all floating items for now + if ( entinfo.groundent != ENTITYNUM_WORLD ) { + continue; + } + //if the entity is still moving + if ( entinfo.origin[0] != entinfo.lastvisorigin[0] || + entinfo.origin[1] != entinfo.lastvisorigin[1] || + entinfo.origin[2] != entinfo.lastvisorigin[2] ) { + continue; + } + //check if the level item isn't already stored + for ( li = levelitems; li; li = li->next ) + { + //if the model of the level item and the entity are different + if ( ic->iteminfo[li->iteminfo].modelindex != modelindex ) { + continue; + } + //if the level item is linked to an entity + if ( li->entitynum ) { + if ( li->entitynum == ent ) { + VectorCopy( entinfo.origin, li->origin ); + break; + } //end if + } //end if + else + { + //check if the entity is very close + VectorSubtract( li->origin, entinfo.origin, dir ); + if ( VectorLength( dir ) < 30 ) { + //found an entity for this level item + li->entitynum = ent; + //keep updating the entity origin + VectorCopy( entinfo.origin, li->origin ); + //also update the goal area number + li->goalareanum = AAS_BestReachableArea( li->origin, + ic->iteminfo[li->iteminfo].mins, ic->iteminfo[li->iteminfo].maxs, + li->goalorigin ); + //Log_Write("found item %s entity", ic->iteminfo[li->iteminfo].classname); + break; + } //end if + //else botimport.Print(PRT_MESSAGE, "item %s has no attached entity\n", + // ic->iteminfo[li->iteminfo].name); + } //end else + } //end for + if ( li ) { + continue; + } + //check if the model is from a known item + for ( i = 0; i < ic->numiteminfo; i++ ) + { + if ( ic->iteminfo[i].modelindex == modelindex ) { + break; + } //end if + } //end for + //if the model is not from a known item + if ( i >= ic->numiteminfo ) { + continue; + } + //allocate a new level item + li = AllocLevelItem(); + // + if ( !li ) { + continue; + } + //entity number of the level item + li->entitynum = ent; + //number for the level item + li->number = numlevelitems + ent; + //set the item info index for the level item + li->iteminfo = i; + //origin of the item + VectorCopy( entinfo.origin, li->origin ); + //get the item goal area and goal origin + li->goalareanum = AAS_BestReachableArea( li->origin, + ic->iteminfo[i].mins, ic->iteminfo[i].maxs, + li->goalorigin ); + // + if ( AAS_AreaJumpPad( li->goalareanum ) ) { + FreeLevelItem( li ); + continue; + } //end if + //time this item out after 30 seconds + //dropped items disappear after 30 seconds + li->timeout = AAS_Time() + 30; + //add the level item to the list + AddLevelItemToList( li ); + //botimport.Print(PRT_MESSAGE, "found new level item %s\n", ic->iteminfo[i].classname); + } //end for +} //end of the function BotUpdateEntityItems +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotDumpGoalStack( int goalstate ) { + int i; + bot_goalstate_t *gs; + char name[32]; + + gs = BotGoalStateFromHandle( goalstate ); + if ( !gs ) { + return; + } + for ( i = 1; i <= gs->goalstacktop; i++ ) + { + BotGoalName( gs->goalstack[i].number, name, 32 ); + Log_Write( "%d: %s", i, name ); + } //end for +} //end of the function BotDumpGoalStack +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotPushGoal( int goalstate, bot_goal_t *goal ) { + bot_goalstate_t *gs; + + gs = BotGoalStateFromHandle( goalstate ); + if ( !gs ) { + return; + } + if ( gs->goalstacktop >= MAX_GOALSTACK - 1 ) { + botimport.Print( PRT_ERROR, "goal heap overflow\n" ); + BotDumpGoalStack( goalstate ); + return; + } //end if + gs->goalstacktop++; + memcpy( &gs->goalstack[gs->goalstacktop], goal, sizeof( bot_goal_t ) ); +} //end of the function BotPushGoal +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotPopGoal( int goalstate ) { + bot_goalstate_t *gs; + + gs = BotGoalStateFromHandle( goalstate ); + if ( !gs ) { + return; + } + if ( gs->goalstacktop > 0 ) { + gs->goalstacktop--; + } +} //end of the function BotPopGoal +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotEmptyGoalStack( int goalstate ) { + bot_goalstate_t *gs; + + gs = BotGoalStateFromHandle( goalstate ); + if ( !gs ) { + return; + } + gs->goalstacktop = 0; +} //end of the function BotEmptyGoalStack +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotGetTopGoal( int goalstate, bot_goal_t *goal ) { + bot_goalstate_t *gs; + + gs = BotGoalStateFromHandle( goalstate ); + if ( !gs ) { + return qfalse; + } + if ( !gs->goalstacktop ) { + return qfalse; + } + memcpy( goal, &gs->goalstack[gs->goalstacktop], sizeof( bot_goal_t ) ); + return qtrue; +} //end of the function BotGetTopGoal +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotGetSecondGoal( int goalstate, bot_goal_t *goal ) { + bot_goalstate_t *gs; + + gs = BotGoalStateFromHandle( goalstate ); + if ( !gs ) { + return qfalse; + } + if ( gs->goalstacktop <= 1 ) { + return qfalse; + } + memcpy( goal, &gs->goalstack[gs->goalstacktop - 1], sizeof( bot_goal_t ) ); + return qtrue; +} //end of the function BotGetSecondGoal +//=========================================================================== +// pops a new long term goal on the goal stack in the goalstate +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotChooseLTGItem( int goalstate, vec3_t origin, int *inventory, int travelflags ) { + int areanum, t, weightnum; + float weight, bestweight, avoidtime; + iteminfo_t *iteminfo; + itemconfig_t *ic; + levelitem_t *li, *bestitem; + bot_goal_t goal; + bot_goalstate_t *gs; + + gs = BotGoalStateFromHandle( goalstate ); + if ( !gs ) { + return qfalse; + } + if ( !gs->itemweightconfig ) { + return qfalse; + } + //get the area the bot is in + areanum = BotReachabilityArea( origin, gs->client ); + //if the bot is in solid or if the area the bot is in has no reachability links + if ( !areanum || !AAS_AreaReachability( areanum ) ) { + //use the last valid area the bot was in + areanum = gs->lastreachabilityarea; + } //end if + //remember the last area with reachabilities the bot was in + gs->lastreachabilityarea = areanum; + //if still in solid + if ( !areanum ) { + return qfalse; + } + //the item configuration + ic = itemconfig; + if ( !itemconfig ) { + return qfalse; + } + //best weight and item so far + bestweight = 0; + bestitem = NULL; + memset( &goal, 0, sizeof( bot_goal_t ) ); + //go through the items in the level + for ( li = levelitems; li; li = li->next ) + { + if ( g_gametype == GT_SINGLE_PLAYER ) { + if ( li->notsingle ) { + continue; + } + } else if ( g_gametype >= GT_TEAM ) { + if ( li->notteam ) { + continue; + } + } else { + if ( li->notfree ) { + continue; + } + } + //if the item is not in a possible goal area + if ( !li->goalareanum ) { + continue; + } + //get the fuzzy weight function for this item + iteminfo = &ic->iteminfo[li->iteminfo]; + weightnum = gs->itemweightindex[iteminfo->number]; + if ( weightnum < 0 ) { + continue; + } + //if this goal is in the avoid goals + if ( BotAvoidGoalTime( goalstate, li->number ) > 0 ) { + continue; + } + +#ifdef UNDECIDEDFUZZY + weight = FuzzyWeightUndecided( inventory, gs->itemweightconfig, weightnum ); +#else + weight = FuzzyWeight( inventory, gs->itemweightconfig, weightnum ); +#endif //UNDECIDEDFUZZY +#ifdef DROPPEDWEIGHT + //HACK: to make dropped items more attractive + if ( li->timeout ) { + weight += 1000; + } +#endif //DROPPEDWEIGHT + if ( weight > 0 ) { + //get the travel time towards the goal area + t = AAS_AreaTravelTimeToGoalArea( areanum, origin, li->goalareanum, travelflags ); + //if the goal is reachable + if ( t > 0 ) { + weight /= (float) t * TRAVELTIME_SCALE; + // + if ( weight > bestweight ) { + bestweight = weight; + bestitem = li; + } //end if + } //end if + } //end if + } //end for + //if no goal item found + if ( !bestitem ) { + /* + //if not in lava or slime + if (!AAS_AreaLava(areanum) && !AAS_AreaSlime(areanum)) + { + if (AAS_RandomGoalArea(areanum, travelflags, &goal.areanum, goal.origin)) + { + VectorSet(goal.mins, -15, -15, -15); + VectorSet(goal.maxs, 15, 15, 15); + goal.entitynum = 0; + goal.number = 0; + goal.flags = GFL_ROAM; + goal.iteminfo = 0; + //push the goal on the stack + BotPushGoal(goalstate, &goal); + // +#ifdef DEBUG + botimport.Print(PRT_MESSAGE, "chosen roam goal area %d\n", goal.areanum); +#endif //DEBUG + return qtrue; + } //end if + } //end if + */ + return qfalse; + } //end if + //create a bot goal for this item + iteminfo = &ic->iteminfo[bestitem->iteminfo]; + VectorCopy( bestitem->goalorigin, goal.origin ); + VectorCopy( iteminfo->mins, goal.mins ); + VectorCopy( iteminfo->maxs, goal.maxs ); + goal.areanum = bestitem->goalareanum; + goal.entitynum = bestitem->entitynum; + goal.number = bestitem->number; + goal.flags = GFL_ITEM; + goal.iteminfo = bestitem->iteminfo; + //add the chosen goal to the goals to avoid for a while + avoidtime = iteminfo->respawntime * 0.5; + if ( avoidtime < 10 ) { + avoidtime = AVOID_TIME; + } + //if it's a dropped item + if ( bestitem->timeout ) { + avoidtime = AVOIDDROPPED_TIME; + } + BotAddToAvoidGoals( gs, bestitem->number, avoidtime ); + //push the goal on the stack + BotPushGoal( goalstate, &goal ); + // +#ifdef DEBUG_AI_GOAL + if ( bestitem->timeout ) { + botimport.Print( PRT_MESSAGE, "new ltg dropped item %s\n", ic->iteminfo[bestitem->iteminfo].classname ); + } //end if + iteminfo = &ic->iteminfo[bestitem->iteminfo]; + botimport.Print( PRT_MESSAGE, "new ltg \"%s\"\n", iteminfo->classname ); +#endif //DEBUG_AI_GOAL + return qtrue; +} //end of the function BotChooseLTGItem +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotChooseNBGItem( int goalstate, vec3_t origin, int *inventory, int travelflags, + bot_goal_t *ltg, float maxtime ) { + int areanum, t, weightnum, ltg_time; + float weight, bestweight, avoidtime; + iteminfo_t *iteminfo; + itemconfig_t *ic; + levelitem_t *li, *bestitem; + bot_goal_t goal; + bot_goalstate_t *gs; + + gs = BotGoalStateFromHandle( goalstate ); + if ( !gs ) { + return qfalse; + } + if ( !gs->itemweightconfig ) { + return qfalse; + } + //get the area the bot is in + areanum = BotReachabilityArea( origin, gs->client ); + //if the bot is in solid or if the area the bot is in has no reachability links + if ( !areanum || !AAS_AreaReachability( areanum ) ) { + //use the last valid area the bot was in + areanum = gs->lastreachabilityarea; + } //end if + //remember the last area with reachabilities the bot was in + gs->lastreachabilityarea = areanum; + //if still in solid + if ( !areanum ) { + return qfalse; + } + // + if ( ltg ) { + ltg_time = AAS_AreaTravelTimeToGoalArea( areanum, origin, ltg->areanum, travelflags ); + } else { ltg_time = 99999;} + //the item configuration + ic = itemconfig; + if ( !itemconfig ) { + return qfalse; + } + //best weight and item so far + bestweight = 0; + bestitem = NULL; + memset( &goal, 0, sizeof( bot_goal_t ) ); + //go through the items in the level + for ( li = levelitems; li; li = li->next ) + { + if ( g_gametype == GT_SINGLE_PLAYER ) { + if ( li->notsingle ) { + continue; + } + } else if ( g_gametype >= GT_TEAM ) { + if ( li->notteam ) { + continue; + } + } else { + if ( li->notfree ) { + continue; + } + } + //if the item is in a possible goal area + if ( !li->goalareanum ) { + continue; + } + //get the fuzzy weight function for this item + iteminfo = &ic->iteminfo[li->iteminfo]; + weightnum = gs->itemweightindex[iteminfo->number]; + if ( weightnum < 0 ) { + continue; + } + //if this goal is in the avoid goals + if ( BotAvoidGoalTime( goalstate, li->number ) > 0 ) { + continue; + } + // +#ifdef UNDECIDEDFUZZY + weight = FuzzyWeightUndecided( inventory, gs->itemweightconfig, weightnum ); +#else + weight = FuzzyWeight( inventory, gs->itemweightconfig, weightnum ); +#endif //UNDECIDEDFUZZY +#ifdef DROPPEDWEIGHT + //HACK: to make dropped items more attractive + if ( li->timeout ) { + weight += 1000; + } +#endif //DROPPEDWEIGHT + if ( weight > 0 ) { + //get the travel time towards the goal area + t = AAS_AreaTravelTimeToGoalArea( areanum, origin, li->goalareanum, travelflags ); + //if the goal is reachable + if ( t > 0 && t < maxtime ) { + weight /= (float) t * TRAVELTIME_SCALE; + // + if ( weight > bestweight ) { + t = 0; + if ( ltg && !li->timeout ) { + //get the travel time from the goal to the long term goal + t = AAS_AreaTravelTimeToGoalArea( li->goalareanum, li->goalorigin, ltg->areanum, travelflags ); + } //end if + //if the travel back is possible and doesn't take too long + if ( t <= ltg_time ) { + bestweight = weight; + bestitem = li; + } //end if + } //end if + } //end if + } //end if + } //end for + //if no goal item found + if ( !bestitem ) { + return qfalse; + } + //create a bot goal for this item + iteminfo = &ic->iteminfo[bestitem->iteminfo]; + VectorCopy( bestitem->goalorigin, goal.origin ); + VectorCopy( iteminfo->mins, goal.mins ); + VectorCopy( iteminfo->maxs, goal.maxs ); + goal.areanum = bestitem->goalareanum; + goal.entitynum = bestitem->entitynum; + goal.number = bestitem->number; + goal.flags = GFL_ITEM; + goal.iteminfo = bestitem->iteminfo; + //add the chosen goal to the goals to avoid for a while + avoidtime = iteminfo->respawntime * 0.5; + if ( avoidtime < 10 ) { + avoidtime = AVOID_TIME; + } + //if it's a dropped item + if ( bestitem->timeout ) { + avoidtime = AVOIDDROPPED_TIME; + } + BotAddToAvoidGoals( gs, bestitem->number, avoidtime ); + //push the goal on the stack + BotPushGoal( goalstate, &goal ); + // +#ifdef DEBUG_AI_GOAL + if ( bestitem->timeout ) { + botimport.Print( PRT_MESSAGE, "new nbg dropped item %s\n", ic->iteminfo[bestitem->iteminfo].classname ); + } //end if + iteminfo = &ic->iteminfo[bestitem->iteminfo]; + botimport.Print( PRT_MESSAGE, "new nbg \"%s\"\n", iteminfo->classname ); +#endif //DEBUG_AI_GOAL + return qtrue; +} //end of the function BotChooseNBGItem +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotTouchingGoal( vec3_t origin, bot_goal_t *goal ) { + int i; + vec3_t boxmins, boxmaxs; + vec3_t absmins, absmaxs; + vec3_t safety_maxs = {0, 0, 0}; //{4, 4, 10}; + vec3_t safety_mins = {0, 0, 0}; //{-4, -4, 0}; + + AAS_PresenceTypeBoundingBox( PRESENCE_NORMAL, boxmins, boxmaxs ); + VectorSubtract( goal->mins, boxmaxs, absmins ); + VectorSubtract( goal->maxs, boxmins, absmaxs ); + VectorAdd( absmins, goal->origin, absmins ); + VectorAdd( absmaxs, goal->origin, absmaxs ); + //make the box a little smaller for safety + VectorSubtract( absmaxs, safety_maxs, absmaxs ); + VectorSubtract( absmins, safety_mins, absmins ); + + for ( i = 0; i < 3; i++ ) + { + if ( origin[i] < absmins[i] || origin[i] > absmaxs[i] ) { + return qfalse; + } + } //end for + return qtrue; +} //end of the function BotTouchingGoal +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotItemGoalInVisButNotVisible( int viewer, vec3_t eye, vec3_t viewangles, bot_goal_t *goal ) { + aas_entityinfo_t entinfo; + bsp_trace_t trace; + vec3_t middle; + + if ( !( goal->flags & GFL_ITEM ) ) { + return qfalse; + } + // + VectorAdd( goal->mins, goal->mins, middle ); + VectorScale( middle, 0.5, middle ); + VectorAdd( goal->origin, middle, middle ); + // + trace = AAS_Trace( eye, NULL, NULL, middle, viewer, CONTENTS_SOLID ); + //if the goal middle point is visible + if ( trace.fraction >= 1 ) { + //the goal entity number doesn't have to be valid + //just assume it's valid + if ( goal->entitynum <= 0 ) { + return qfalse; + } + // + //if the entity data isn't valid + AAS_EntityInfo( goal->entitynum, &entinfo ); + //NOTE: for some wacko reason entities are sometimes + // not updated + //if (!entinfo.valid) return qtrue; + if ( entinfo.ltime < AAS_Time() - 0.5 ) { + return qtrue; + } + } //end if + return qfalse; +} //end of the function BotItemGoalInVisButNotVisible +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotResetGoalState( int goalstate ) { + bot_goalstate_t *gs; + + gs = BotGoalStateFromHandle( goalstate ); + if ( !gs ) { + return; + } + memset( gs->goalstack, 0, MAX_GOALSTACK * sizeof( bot_goal_t ) ); + gs->goalstacktop = 0; + BotResetAvoidGoals( goalstate ); +} //end of the function BotResetGoalState +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotLoadItemWeights( int goalstate, char *filename ) { + bot_goalstate_t *gs; + + gs = BotGoalStateFromHandle( goalstate ); + if ( !gs ) { + return BLERR_CANNOTLOADITEMWEIGHTS; + } + //load the weight configuration + gs->itemweightconfig = ReadWeightConfig( filename ); + if ( !gs->itemweightconfig ) { + botimport.Print( PRT_FATAL, "couldn't load weights\n" ); + return BLERR_CANNOTLOADITEMWEIGHTS; + } //end if + //if there's no item configuration + if ( !itemconfig ) { + return BLERR_CANNOTLOADITEMWEIGHTS; + } + //create the item weight index + gs->itemweightindex = ItemWeightIndex( gs->itemweightconfig, itemconfig ); + //everything went ok + return BLERR_NOERROR; +} //end of the function BotLoadItemWeights +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotFreeItemWeights( int goalstate ) { + bot_goalstate_t *gs; + + gs = BotGoalStateFromHandle( goalstate ); + if ( !gs ) { + return; + } + if ( gs->itemweightconfig ) { + FreeWeightConfig( gs->itemweightconfig ); + } + if ( gs->itemweightindex ) { + FreeMemory( gs->itemweightindex ); + } +} //end of the function BotFreeItemWeights +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotAllocGoalState( int client ) { + int i; + + for ( i = 1; i <= MAX_CLIENTS; i++ ) + { + if ( !botgoalstates[i] ) { + botgoalstates[i] = GetClearedMemory( sizeof( bot_goalstate_t ) ); + botgoalstates[i]->client = client; + return i; + } //end if + } //end for + return 0; +} //end of the function BotAllocGoalState +//======================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//======================================================================== +void BotFreeGoalState( int handle ) { + if ( handle <= 0 || handle > MAX_CLIENTS ) { + botimport.Print( PRT_FATAL, "goal state handle %d out of range\n", handle ); + return; + } //end if + if ( !botgoalstates[handle] ) { + botimport.Print( PRT_FATAL, "invalid goal state handle %d\n", handle ); + return; + } //end if + BotFreeItemWeights( handle ); + FreeMemory( botgoalstates[handle] ); + botgoalstates[handle] = NULL; +} //end of the function BotFreeGoalState +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotSetupGoalAI( void ) { + char *filename; + + //check if teamplay is on + g_gametype = LibVarValue( "g_gametype", "0" ); + //item configuration file + filename = LibVarString( "itemconfig", "items.c" ); + //load the item configuration + itemconfig = LoadItemConfig( filename ); + if ( !itemconfig ) { + botimport.Print( PRT_FATAL, "couldn't load item config\n" ); + return BLERR_CANNOTLOADITEMCONFIG; + } //end if + //everything went ok + return BLERR_NOERROR; +} //end of the function BotSetupGoalAI +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotShutdownGoalAI( void ) { + int i; + + if ( itemconfig ) { + FreeMemory( itemconfig ); + } + itemconfig = NULL; + if ( levelitemheap ) { + FreeMemory( levelitemheap ); + } + levelitemheap = NULL; + freelevelitems = NULL; + levelitems = NULL; + numlevelitems = 0; + + BotFreeInfoEntities(); + + for ( i = 1; i <= MAX_CLIENTS; i++ ) + { + if ( botgoalstates[i] ) { + BotFreeGoalState( i ); + } //end if + } //end for +} //end of the function BotShutdownGoalAI diff --git a/src/botlib/be_ai_move.c b/src/botlib/be_ai_move.c new file mode 100644 index 0000000..0795ed6 --- /dev/null +++ b/src/botlib/be_ai_move.c @@ -0,0 +1,3697 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_ai_move.c + * + * desc: bot movement AI + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "l_memory.h" +#include "l_libvar.h" +#include "l_utils.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "aasfile.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "be_aas_funcs.h" +#include "be_interface.h" + +#include "../game/be_ea.h" +#include "../game/be_ai_goal.h" +#include "../game/be_ai_move.h" + + +//#define DEBUG_AI_MOVE +//#define DEBUG_ELEVATOR +//#define DEBUG_GRAPPLE +//movement state + +//NOTE: the moveflags MFL_ONGROUND, MFL_TELEPORTED and MFL_WATERJUMP must be set outside the movement code +typedef struct bot_movestate_s +{ + //input vars (all set outside the movement code) + vec3_t origin; //origin of the bot + vec3_t velocity; //velocity of the bot + vec3_t viewoffset; //view offset + int entitynum; //entity number of the bot + int client; //client number of the bot + float thinktime; //time the bot thinks + int presencetype; //presencetype of the bot + vec3_t viewangles; //view angles of the bot + //state vars + int areanum; //area the bot is in + int lastareanum; //last area the bot was in + int lastgoalareanum; //last goal area number + int lastreachnum; //last reachability number + vec3_t lastorigin; //origin previous cycle + float lasttime; + int reachareanum; //area number of the reachabilty + int moveflags; //movement flags + int jumpreach; //set when jumped + float grapplevisible_time; //last time the grapple was visible + float lastgrappledist; //last distance to the grapple end + float reachability_time; //time to use current reachability + int avoidreach[MAX_AVOIDREACH]; //reachabilities to avoid + float avoidreachtimes[MAX_AVOIDREACH]; //times to avoid the reachabilities + int avoidreachtries[MAX_AVOIDREACH]; //number of tries before avoiding +} bot_movestate_t; + +//used to avoid reachability links for some time after being used +// Ridah, disabled this to prevent wierd navigational behaviour (mostly by Zombie, since it's so slow) +//#define AVOIDREACH +#define AVOIDREACH_TIME 6 //avoid links for 6 seconds after use +#define AVOIDREACH_TRIES 4 +//prediction times +#define PREDICTIONTIME_JUMP 3 //in seconds +#define PREDICTIONTIME_MOVE 2 //in seconds +//hook commands +#define CMD_HOOKOFF "hookoff" +#define CMD_HOOKON "hookon" +//weapon indexes for weapon jumping +#define WEAPONINDEX_ROCKET_LAUNCHER 5 +#define WEAPONINDEX_BFG 9 + +#define MODELTYPE_FUNC_PLAT 1 +#define MODELTYPE_FUNC_BOB 2 + +float sv_maxstep; +float sv_maxbarrier; +float sv_gravity; +//type of model, func_plat or func_bobbing +int modeltypes[MAX_MODELS]; + +bot_movestate_t *botmovestates[MAX_CLIENTS + 1]; + +//======================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//======================================================================== +int BotAllocMoveState( void ) { + int i; + + for ( i = 1; i <= MAX_CLIENTS; i++ ) + { + if ( !botmovestates[i] ) { + botmovestates[i] = GetClearedMemory( sizeof( bot_movestate_t ) ); + return i; + } //end if + } //end for + return 0; +} //end of the function BotAllocMoveState +//======================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//======================================================================== +void BotFreeMoveState( int handle ) { + if ( handle <= 0 || handle > MAX_CLIENTS ) { + botimport.Print( PRT_FATAL, "move state handle %d out of range\n", handle ); + return; + } //end if + if ( !botmovestates[handle] ) { + botimport.Print( PRT_FATAL, "invalid move state %d\n", handle ); + return; + } //end if + FreeMemory( botmovestates[handle] ); + botmovestates[handle] = NULL; +} //end of the function BotFreeMoveState +//======================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//======================================================================== +bot_movestate_t *BotMoveStateFromHandle( int handle ) { + if ( handle <= 0 || handle > MAX_CLIENTS ) { + botimport.Print( PRT_FATAL, "move state handle %d out of range\n", handle ); + return NULL; + } //end if + if ( !botmovestates[handle] ) { + botimport.Print( PRT_FATAL, "invalid move state %d\n", handle ); + return NULL; + } //end if + return botmovestates[handle]; +} //end of the function BotMoveStateFromHandle + +// Ridah, provide a means of resetting the avoidreach, so if a bot stops moving, they don't avoid the area they were heading for +//======================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//======================================================================== +void BotInitAvoidReach( int handle ) { + bot_movestate_t *ms; + + ms = BotMoveStateFromHandle( handle ); + if ( !ms ) { + return; + } + + memset( ms->avoidreach, 0, sizeof( ms->avoidreach ) ); + memset( ms->avoidreachtries, 0, sizeof( ms->avoidreachtries ) ); + memset( ms->avoidreachtimes, 0, sizeof( ms->avoidreachtimes ) ); +} +// done. + +//======================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//======================================================================== +void BotInitMoveState( int handle, bot_initmove_t *initmove ) { + bot_movestate_t *ms; + + ms = BotMoveStateFromHandle( handle ); + if ( !ms ) { + return; + } + VectorCopy( initmove->origin, ms->origin ); + VectorCopy( initmove->velocity, ms->velocity ); + VectorCopy( initmove->viewoffset, ms->viewoffset ); + ms->entitynum = initmove->entitynum; + ms->client = initmove->client; + ms->thinktime = initmove->thinktime; + ms->presencetype = initmove->presencetype; + VectorCopy( initmove->viewangles, ms->viewangles ); + // + ms->moveflags &= ~MFL_ONGROUND; + if ( initmove->or_moveflags & MFL_ONGROUND ) { + ms->moveflags |= MFL_ONGROUND; + } + ms->moveflags &= ~MFL_TELEPORTED; + if ( initmove->or_moveflags & MFL_TELEPORTED ) { + ms->moveflags |= MFL_TELEPORTED; + } + ms->moveflags &= ~MFL_WATERJUMP; + if ( initmove->or_moveflags & MFL_WATERJUMP ) { + ms->moveflags |= MFL_WATERJUMP; + } + ms->moveflags &= ~MFL_WALK; + if ( initmove->or_moveflags & MFL_WALK ) { + ms->moveflags |= MFL_WALK; + } +} //end of the function BotInitMoveState +//======================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//======================================================================== +float AngleDiff( float ang1, float ang2 ) { + float diff; + + diff = ang1 - ang2; + if ( ang1 > ang2 ) { + if ( diff > 180.0 ) { + diff -= 360.0; + } + } //end if + else + { + if ( diff < -180.0 ) { + diff += 360.0; + } + } //end else + return diff; +} //end of the function AngleDiff +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotFuzzyPointReachabilityArea( vec3_t origin ) { + int firstareanum, j, x, y, z; + int areas[10], numareas, areanum, bestareanum; + float dist, bestdist; + vec3_t points[10], v, end; + + firstareanum = 0; + areanum = AAS_PointAreaNum( origin ); + if ( areanum ) { + firstareanum = areanum; + if ( AAS_AreaReachability( areanum ) ) { + return areanum; + } + } //end if + VectorCopy( origin, end ); + end[2] += 4; + numareas = AAS_TraceAreas( origin, end, areas, points, 10 ); + for ( j = 0; j < numareas; j++ ) + { + if ( AAS_AreaReachability( areas[j] ) ) { + return areas[j]; + } + } //end for + bestdist = 999999; + bestareanum = 0; + //z = 0; + for ( z = 1; z >= -1; z -= 1 ) + { + for ( x = 1; x >= -1; x -= 2 ) + { + for ( y = 1; y >= -1; y -= 2 ) + { + VectorCopy( origin, end ); + // Ridah, increased this for Wolf larger bounding boxes + end[0] += x * 256; //8; + end[1] += y * 256; //8; + end[2] += z * 200; //12; + numareas = AAS_TraceAreas( origin, end, areas, points, 10 ); + for ( j = 0; j < numareas; j++ ) + { + if ( AAS_AreaReachability( areas[j] ) ) { + VectorSubtract( points[j], origin, v ); + dist = VectorLength( v ); + if ( dist < bestdist ) { + bestareanum = areas[j]; + bestdist = dist; + } //end if + } //end if + if ( !firstareanum ) { + firstareanum = areas[j]; + } + } //end for + } //end for + } //end for + if ( bestareanum ) { + return bestareanum; + } + } //end for + return firstareanum; +} //end of the function BotFuzzyPointReachabilityArea +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotReachabilityArea( vec3_t origin, int client ) { + int modelnum, modeltype, reachnum, areanum; + aas_reachability_t reach; + vec3_t org, end, mins, maxs, up = {0, 0, 1}; + bsp_trace_t bsptrace; + aas_trace_t trace; + + //check if the bot is standing on something + AAS_PresenceTypeBoundingBox( PRESENCE_CROUCH, mins, maxs ); + VectorMA( origin, -3, up, end ); + bsptrace = AAS_Trace( origin, mins, maxs, end, client, CONTENTS_SOLID | CONTENTS_PLAYERCLIP ); + if ( !bsptrace.startsolid && bsptrace.fraction < 1 && bsptrace.ent != ENTITYNUM_NONE ) { + //if standing on the world the bot should be in a valid area + if ( bsptrace.ent == ENTITYNUM_WORLD ) { + return BotFuzzyPointReachabilityArea( origin ); + } //end if + + modelnum = AAS_EntityModelindex( bsptrace.ent ); + modeltype = modeltypes[modelnum]; + + //if standing on a func_plat or func_bobbing then the bot is assumed to be + //in the area the reachability points to + if ( modeltype == MODELTYPE_FUNC_PLAT || modeltype == MODELTYPE_FUNC_BOB ) { + reachnum = AAS_NextModelReachability( 0, modelnum ); + if ( reachnum ) { + AAS_ReachabilityFromNum( reachnum, &reach ); + return reach.areanum; + } //end if + } //end else if + + //if the bot is swimming the bot should be in a valid area + if ( AAS_Swimming( origin ) ) { + return BotFuzzyPointReachabilityArea( origin ); + } //end if + // + areanum = BotFuzzyPointReachabilityArea( origin ); + //if the bot is in an area with reachabilities + if ( areanum && AAS_AreaReachability( areanum ) ) { + return areanum; + } + //trace down till the ground is hit because the bot is standing on some other entity + VectorCopy( origin, org ); + VectorCopy( org, end ); + end[2] -= 800; + trace = AAS_TraceClientBBox( org, end, PRESENCE_CROUCH, -1 ); + if ( !trace.startsolid ) { + VectorCopy( trace.endpos, org ); + } //end if + // + return BotFuzzyPointReachabilityArea( org ); + } //end if + // + return BotFuzzyPointReachabilityArea( origin ); +} //end of the function BotReachabilityArea +//=========================================================================== +// returns the reachability area the bot is in +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +/* +int BotReachabilityArea(vec3_t origin, int testground) +{ + int firstareanum, i, j, x, y, z; + int areas[10], numareas, areanum, bestareanum; + float dist, bestdist; + vec3_t org, end, points[10], v; + aas_trace_t trace; + + firstareanum = 0; + for (i = 0; i < 2; i++) + { + VectorCopy(origin, org); + //if test at the ground (used when bot is standing on an entity) + if (i > 0) + { + VectorCopy(origin, end); + end[2] -= 800; + trace = AAS_TraceClientBBox(origin, end, PRESENCE_CROUCH, -1); + if (!trace.startsolid) + { + VectorCopy(trace.endpos, org); + } //end if + } //end if + + firstareanum = 0; + areanum = AAS_PointAreaNum(org); + if (areanum) + { + firstareanum = areanum; + if (AAS_AreaReachability(areanum)) return areanum; + } //end if + bestdist = 999999; + bestareanum = 0; + for (z = 1; z >= -1; z -= 1) + { + for (x = 1; x >= -1; x -= 1) + { + for (y = 1; y >= -1; y -= 1) + { + VectorCopy(org, end); + end[0] += x * 8; + end[1] += y * 8; + end[2] += z * 12; + numareas = AAS_TraceAreas(org, end, areas, points, 10); + for (j = 0; j < numareas; j++) + { + if (AAS_AreaReachability(areas[j])) + { + VectorSubtract(points[j], org, v); + dist = VectorLength(v); + if (dist < bestdist) + { + bestareanum = areas[j]; + bestdist = dist; + } //end if + } //end if + } //end for + } //end for + } //end for + if (bestareanum) return bestareanum; + } //end for + if (!testground) break; + } //end for +//#ifdef DEBUG + //botimport.Print(PRT_MESSAGE, "no reachability area\n"); +//#endif //DEBUG + return firstareanum; +} //end of the function BotReachabilityArea*/ +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotOnMover( vec3_t origin, int entnum, aas_reachability_t *reach ) { + int i, modelnum; + vec3_t mins, maxs, modelorigin, org, end; + vec3_t angles = {0, 0, 0}; + vec3_t boxmins = {-16, -16, -8}, boxmaxs = {16, 16, 8}; + bsp_trace_t trace; + + modelnum = reach->facenum & 0x0000FFFF; + //get some bsp model info + AAS_BSPModelMinsMaxsOrigin( modelnum, angles, mins, maxs, NULL ); + // + if ( !AAS_OriginOfEntityWithModelNum( modelnum, modelorigin ) ) { + botimport.Print( PRT_MESSAGE, "no entity with model %d\n", modelnum ); + return qfalse; + } //end if + // + for ( i = 0; i < 2; i++ ) + { + if ( origin[i] > modelorigin[i] + maxs[i] + 16 ) { + return qfalse; + } + if ( origin[i] < modelorigin[i] + mins[i] - 16 ) { + return qfalse; + } + } //end for + // + VectorCopy( origin, org ); + org[2] += 24; + VectorCopy( origin, end ); + end[2] -= 48; + // + trace = AAS_Trace( org, boxmins, boxmaxs, end, entnum, CONTENTS_SOLID | CONTENTS_PLAYERCLIP ); + if ( !trace.startsolid && !trace.allsolid ) { + //NOTE: the reachability face number is the model number of the elevator + if ( trace.ent != ENTITYNUM_NONE && AAS_EntityModelNum( trace.ent ) == modelnum ) { + return qtrue; + } //end if + } //end if + return qfalse; +} //end of the function BotOnMover +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int MoverDown( aas_reachability_t *reach ) { + int modelnum; + vec3_t mins, maxs, origin; + vec3_t angles = {0, 0, 0}; + + modelnum = reach->facenum & 0x0000FFFF; + //get some bsp model info + AAS_BSPModelMinsMaxsOrigin( modelnum, angles, mins, maxs, origin ); + // + if ( !AAS_OriginOfEntityWithModelNum( modelnum, origin ) ) { + botimport.Print( PRT_MESSAGE, "no entity with model %d\n", modelnum ); + return qfalse; + } //end if + //if the top of the plat is below the reachability start point + if ( origin[2] + maxs[2] < reach->start[2] ) { + return qtrue; + } + return qfalse; +} //end of the function MoverDown +//======================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//======================================================================== +void BotSetBrushModelTypes( void ) { + int ent, modelnum; + char classname[MAX_EPAIRKEY], model[MAX_EPAIRKEY]; + + memset( modeltypes, 0, MAX_MODELS * sizeof( int ) ); + // + for ( ent = AAS_NextBSPEntity( 0 ); ent; ent = AAS_NextBSPEntity( ent ) ) + { + if ( !AAS_ValueForBSPEpairKey( ent, "classname", classname, MAX_EPAIRKEY ) ) { + continue; + } + if ( !AAS_ValueForBSPEpairKey( ent, "model", model, MAX_EPAIRKEY ) ) { + continue; + } + if ( model[0] ) { + modelnum = atoi( model + 1 ); + } else { modelnum = 0;} + + if ( modelnum < 0 || modelnum > MAX_MODELS ) { + botimport.Print( PRT_MESSAGE, "entity %s model number out of range\n", classname ); + continue; + } //end if + + if ( !strcmp( classname, "func_bobbing" ) ) { + modeltypes[modelnum] = MODELTYPE_FUNC_BOB; + } else if ( !strcmp( classname, "func_plat" ) ) { + modeltypes[modelnum] = MODELTYPE_FUNC_PLAT; + } + } //end for +} //end of the function BotSetBrushModelTypes +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotOnTopOfEntity( bot_movestate_t *ms ) { + vec3_t mins, maxs, end, up = {0, 0, 1}; + bsp_trace_t trace; + + AAS_PresenceTypeBoundingBox( ms->presencetype, mins, maxs ); + VectorMA( ms->origin, -3, up, end ); + trace = AAS_Trace( ms->origin, mins, maxs, end, ms->entitynum, CONTENTS_SOLID | CONTENTS_PLAYERCLIP ); + if ( !trace.startsolid && ( trace.ent != ENTITYNUM_WORLD && trace.ent != ENTITYNUM_NONE ) ) { + return trace.ent; + } //end if + return -1; +} //end of the function BotOnTopOfEntity +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotValidTravel( vec3_t origin, aas_reachability_t *reach, int travelflags ) { + //if the reachability uses an unwanted travel type + if ( AAS_TravelFlagForType( reach->traveltype ) & ~travelflags ) { + return qfalse; + } + //don't go into areas with bad travel types + if ( AAS_AreaContentsTravelFlag( reach->areanum ) & ~travelflags ) { + return qfalse; + } + return qtrue; +} //end of the function BotValidTravel +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotAddToAvoidReach( bot_movestate_t *ms, int number, float avoidtime ) { + int i; + + for ( i = 0; i < MAX_AVOIDREACH; i++ ) + { + if ( ms->avoidreach[i] == number ) { + if ( ms->avoidreachtimes[i] > AAS_Time() ) { + ms->avoidreachtries[i]++; + } else { ms->avoidreachtries[i] = 1;} + ms->avoidreachtimes[i] = AAS_Time() + avoidtime; + return; + } //end if + } //end for + //add the reachability to the reachabilities to avoid for a while + for ( i = 0; i < MAX_AVOIDREACH; i++ ) + { + if ( ms->avoidreachtimes[i] < AAS_Time() ) { + ms->avoidreach[i] = number; + ms->avoidreachtimes[i] = AAS_Time() + avoidtime; + ms->avoidreachtries[i] = 1; + return; + } //end if + } //end for +} //end of the function BotAddToAvoidReach +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +//__inline int AAS_AreaContentsTravelFlag(int areanum); + +int BotGetReachabilityToGoal( vec3_t origin, int areanum, int entnum, + int lastgoalareanum, int lastareanum, + int *avoidreach, float *avoidreachtimes, int *avoidreachtries, + bot_goal_t *goal, int travelflags, int movetravelflags ) { + int t, besttime, bestreachnum, reachnum; + aas_reachability_t reach; + + //if not in a valid area + if ( !areanum ) { + return 0; + } + // + if ( AAS_AreaDoNotEnter( areanum ) || AAS_AreaDoNotEnter( goal->areanum ) ) { + travelflags |= TFL_DONOTENTER; + movetravelflags |= TFL_DONOTENTER; + } //end if + if ( AAS_AreaDoNotEnterLarge( areanum ) || AAS_AreaDoNotEnterLarge( goal->areanum ) ) { + travelflags |= TFL_DONOTENTER_LARGE; + movetravelflags |= TFL_DONOTENTER_LARGE; + } //end if + //use the routing to find the next area to go to + besttime = 0; + bestreachnum = 0; + // + for ( reachnum = AAS_NextAreaReachability( areanum, 0 ); reachnum; + reachnum = AAS_NextAreaReachability( areanum, reachnum ) ) + { +#ifdef AVOIDREACH + int i; + //check if it isn't an reachability to avoid + for ( i = 0; i < MAX_AVOIDREACH; i++ ) + { + if ( avoidreach[i] == reachnum && avoidreachtimes[i] >= AAS_Time() ) { + break; + } + } //end for + if ( i != MAX_AVOIDREACH && avoidreachtries[i] > AVOIDREACH_TRIES ) { +#ifdef DEBUG + if ( bot_developer ) { + botimport.Print( PRT_MESSAGE, "avoiding reachability %d\n", avoidreach[i] ); + } //end if +#endif //DEBUG + continue; + } //end if +#endif //AVOIDREACH + //get the reachability from the number + AAS_ReachabilityFromNum( reachnum, &reach ); + //NOTE: do not go back to the previous area if the goal didn't change + //NOTE: is this actually avoidance of local routing minima between two areas??? + if ( lastgoalareanum == goal->areanum && reach.areanum == lastareanum ) { + continue; + } + //if (AAS_AreaContentsTravelFlag(reach.areanum) & ~travelflags) continue; + //if the travel isn't valid + if ( !BotValidTravel( origin, &reach, movetravelflags ) ) { + continue; + } + //RF, ignore disabled areas + if ( !AAS_AreaReachability( reach.areanum ) ) { + continue; + } + //get the travel time (ignore routes that leads us back our current area) + t = AAS_AreaTravelTimeToGoalAreaCheckLoop( reach.areanum, reach.end, goal->areanum, travelflags, areanum ); + //if the goal area isn't reachable from the reachable area + if ( !t ) { + continue; + } + + // Ridah, if this sends us to a looped route, ignore it + //if (AAS_AreaTravelTimeToGoalArea(areanum, reach.start, goal->areanum, travelflags) + reach.traveltime < t) + // continue; + + //add the travel time towards the area + // Ridah, not sure why this was disabled, but it causes looped links in the route-cache + // RF, update.. seems to work better like this.... + t += reach.traveltime; // + AAS_AreaTravelTime(areanum, origin, reach.start); + //t += reach.traveltime + AAS_AreaTravelTime(areanum, origin, reach.start); + + // Ridah, if there exists other entities in this area, avoid it +// if (reach.areanum != goal->areanum && AAS_IsEntityInArea( entnum, goal->entitynum, reach.areanum )) { +// t += 50; +// } + + //if the travel time is better than the ones already found + if ( !besttime || t < besttime ) { + besttime = t; + bestreachnum = reachnum; + } //end if + } //end for + // + return bestreachnum; +} //end of the function BotGetReachabilityToGoal +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotAddToTarget( vec3_t start, vec3_t end, float maxdist, float *dist, vec3_t target ) { + vec3_t dir; + float curdist; + + VectorSubtract( end, start, dir ); + curdist = VectorNormalize( dir ); + if ( *dist + curdist < maxdist ) { + VectorCopy( end, target ); + *dist += curdist; + return qfalse; + } //end if + else + { + VectorMA( start, maxdist - *dist, dir, target ); + *dist = maxdist; + return qtrue; + } //end else +} //end of the function BotAddToTarget + +int BotMovementViewTarget( int movestate, bot_goal_t *goal, int travelflags, float lookahead, vec3_t target ) { + aas_reachability_t reach; + int reachnum, lastareanum; + bot_movestate_t *ms; + vec3_t end; + float dist; + + ms = BotMoveStateFromHandle( movestate ); + if ( !ms ) { + return qfalse; + } + reachnum = 0; + //if the bot has no goal or no last reachability + if ( !ms->lastreachnum || !goal ) { + return qfalse; + } + + reachnum = ms->lastreachnum; + VectorCopy( ms->origin, end ); + lastareanum = ms->lastareanum; + dist = 0; + while ( reachnum && dist < lookahead ) + { + AAS_ReachabilityFromNum( reachnum, &reach ); + if ( BotAddToTarget( end, reach.start, lookahead, &dist, target ) ) { + return qtrue; + } + //never look beyond teleporters + if ( reach.traveltype == TRAVEL_TELEPORT ) { + return qtrue; + } + //don't add jump pad distances + if ( reach.traveltype != TRAVEL_JUMPPAD && + reach.traveltype != TRAVEL_ELEVATOR && + reach.traveltype != TRAVEL_FUNCBOB ) { + if ( BotAddToTarget( reach.start, reach.end, lookahead, &dist, target ) ) { + return qtrue; + } + } //end if + reachnum = BotGetReachabilityToGoal( reach.end, reach.areanum, -1, + ms->lastgoalareanum, lastareanum, + ms->avoidreach, ms->avoidreachtimes, ms->avoidreachtries, + goal, travelflags, travelflags ); + VectorCopy( reach.end, end ); + lastareanum = reach.areanum; + if ( lastareanum == goal->areanum ) { + BotAddToTarget( reach.end, goal->origin, lookahead, &dist, target ); + return qtrue; + } //end if + } //end while + // + return qfalse; +} //end of the function BotMovementViewTarget +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotVisible( int ent, vec3_t eye, vec3_t target ) { + bsp_trace_t trace; + + trace = AAS_Trace( eye, NULL, NULL, target, ent, CONTENTS_SOLID | CONTENTS_PLAYERCLIP ); + if ( trace.fraction >= 1 ) { + return qtrue; + } + return qfalse; +} //end of the function BotVisible +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotPredictVisiblePosition( vec3_t origin, int areanum, bot_goal_t *goal, int travelflags, vec3_t target ) { + aas_reachability_t reach; + int reachnum, lastgoalareanum, lastareanum, i; + int avoidreach[MAX_AVOIDREACH]; + float avoidreachtimes[MAX_AVOIDREACH]; + int avoidreachtries[MAX_AVOIDREACH]; + vec3_t end; + + //if the bot has no goal or no last reachability + if ( !goal ) { + return qfalse; + } + //if the areanum is not valid + if ( !areanum ) { + return qfalse; + } + //if the goal areanum is not valid + if ( !goal->areanum ) { + return qfalse; + } + + memset( avoidreach, 0, MAX_AVOIDREACH * sizeof( int ) ); + lastgoalareanum = goal->areanum; + lastareanum = areanum; + VectorCopy( origin, end ); + //only do 20 hops + for ( i = 0; i < 20 && ( areanum != goal->areanum ); i++ ) + { + // + reachnum = BotGetReachabilityToGoal( end, areanum, -1, + lastgoalareanum, lastareanum, + avoidreach, avoidreachtimes, avoidreachtries, + goal, travelflags, travelflags ); + if ( !reachnum ) { + return qfalse; + } + AAS_ReachabilityFromNum( reachnum, &reach ); + // + if ( BotVisible( goal->entitynum, goal->origin, reach.start ) ) { + VectorCopy( reach.start, target ); + return qtrue; + } //end if + // + if ( BotVisible( goal->entitynum, goal->origin, reach.end ) ) { + VectorCopy( reach.end, target ); + return qtrue; + } //end if + // + if ( reach.areanum == goal->areanum ) { + VectorCopy( reach.end, target ); + return qtrue; + } //end if + // + lastareanum = areanum; + areanum = reach.areanum; + VectorCopy( reach.end, end ); + // + } //end while + // + return qfalse; +} //end of the function BotPredictVisiblePosition +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void MoverBottomCenter( aas_reachability_t *reach, vec3_t bottomcenter ) { + int modelnum; + vec3_t mins, maxs, origin, mids; + vec3_t angles = {0, 0, 0}; + + modelnum = reach->facenum & 0x0000FFFF; + //get some bsp model info + AAS_BSPModelMinsMaxsOrigin( modelnum, angles, mins, maxs, origin ); + // + if ( !AAS_OriginOfEntityWithModelNum( modelnum, origin ) ) { + botimport.Print( PRT_MESSAGE, "no entity with model %d\n", modelnum ); + } //end if + //get a point just above the plat in the bottom position + VectorAdd( mins, maxs, mids ); + VectorMA( origin, 0.5, mids, bottomcenter ); + bottomcenter[2] = reach->start[2]; +} //end of the function MoverBottomCenter +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float BotGapDistance( vec3_t origin, vec3_t hordir, int entnum ) { + float dist, startz; + vec3_t start, end; + aas_trace_t trace; + + //do gap checking + startz = origin[2]; + //this enables walking down stairs more fluidly + { + VectorCopy( origin, start ); + VectorCopy( origin, end ); + end[2] -= 60; + trace = AAS_TraceClientBBox( start, end, PRESENCE_CROUCH, entnum ); + if ( trace.fraction >= 1 ) { + return 1; + } + startz = trace.endpos[2] + 1; + } + // + for ( dist = 8; dist <= 100; dist += 8 ) + { + VectorMA( origin, dist, hordir, start ); + start[2] = startz + 24; + VectorCopy( start, end ); + end[2] -= 48 + sv_maxbarrier; + trace = AAS_TraceClientBBox( start, end, PRESENCE_CROUCH, entnum ); + //if solid is found the bot can't walk any further and fall into a gap + if ( !trace.startsolid ) { + //if it is a gap + if ( trace.endpos[2] < startz - sv_maxstep - 8 ) { + VectorCopy( trace.endpos, end ); + end[2] -= 20; + if ( AAS_PointContents( end ) & ( CONTENTS_WATER | CONTENTS_SLIME ) ) { + break; //----(SA) modified since slime is no longer deadly + } +// if (AAS_PointContents(end) & CONTENTS_WATER) break; + //if a gap is found slow down + //botimport.Print(PRT_MESSAGE, "gap at %f\n", dist); + return dist; + } //end if + startz = trace.endpos[2]; + } //end if + } //end for + return 0; +} //end of the function BotGapDistance +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotCheckBarrierJump( bot_movestate_t *ms, vec3_t dir, float speed ) { + vec3_t start, hordir, end; + aas_trace_t trace; + + VectorCopy( ms->origin, end ); + end[2] += sv_maxbarrier; + //trace right up + trace = AAS_TraceClientBBox( ms->origin, end, PRESENCE_NORMAL, ms->entitynum ); + //this shouldn't happen... but we check anyway + if ( trace.startsolid ) { + return qfalse; + } + //if very low ceiling it isn't possible to jump up to a barrier + if ( trace.endpos[2] - ms->origin[2] < sv_maxstep ) { + return qfalse; + } + // + hordir[0] = dir[0]; + hordir[1] = dir[1]; + hordir[2] = 0; + VectorNormalize( hordir ); + VectorMA( ms->origin, ms->thinktime * speed * 0.5, hordir, end ); + VectorCopy( trace.endpos, start ); + end[2] = trace.endpos[2]; + //trace from previous trace end pos horizontally in the move direction + trace = AAS_TraceClientBBox( start, end, PRESENCE_NORMAL, ms->entitynum ); + //again this shouldn't happen + if ( trace.startsolid ) { + return qfalse; + } + // + VectorCopy( trace.endpos, start ); + VectorCopy( trace.endpos, end ); + end[2] = ms->origin[2]; + //trace down from the previous trace end pos + trace = AAS_TraceClientBBox( start, end, PRESENCE_NORMAL, ms->entitynum ); + //if solid + if ( trace.startsolid ) { + return qfalse; + } + //if no obstacle at all + if ( trace.fraction >= 1.0 ) { + return qfalse; + } + //if less than the maximum step height + if ( trace.endpos[2] - ms->origin[2] < sv_maxstep ) { + return qfalse; + } + // + EA_Jump( ms->client ); + EA_Move( ms->client, hordir, speed ); + ms->moveflags |= MFL_BARRIERJUMP; + //there is a barrier + return qtrue; +} //end of the function BotCheckBarrierJump +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotSwimInDirection( bot_movestate_t *ms, vec3_t dir, float speed, int type ) { + vec3_t normdir; + + VectorCopy( dir, normdir ); + VectorNormalize( normdir ); + EA_Move( ms->client, normdir, speed ); + return qtrue; +} //end of the function BotSwimInDirection +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotWalkInDirection( bot_movestate_t *ms, vec3_t dir, float speed, int type ) { + vec3_t hordir, cmdmove, velocity, tmpdir, origin; + int presencetype, maxframes, cmdframes, stopevent; + aas_clientmove_t move; + float dist; + + //if the bot is on the ground + if ( ms->moveflags & MFL_ONGROUND ) { + //if there is a barrier the bot can jump on + if ( BotCheckBarrierJump( ms, dir, speed ) ) { + return qtrue; + } + //remove barrier jump flag + ms->moveflags &= ~MFL_BARRIERJUMP; + //get the presence type for the movement + if ( ( type & MOVE_CROUCH ) && !( type & MOVE_JUMP ) ) { + presencetype = PRESENCE_CROUCH; + } else { presencetype = PRESENCE_NORMAL;} + //horizontal direction + hordir[0] = dir[0]; + hordir[1] = dir[1]; + hordir[2] = 0; + VectorNormalize( hordir ); + //if the bot is not supposed to jump + if ( !( type & MOVE_JUMP ) ) { + //if there is a gap, try to jump over it + if ( BotGapDistance( ms->origin, hordir, ms->entitynum ) > 0 ) { + type |= MOVE_JUMP; + } + } //end if + //get command movement + VectorScale( hordir, speed, cmdmove ); + VectorCopy( ms->velocity, velocity ); + // + if ( type & MOVE_JUMP ) { + //botimport.Print(PRT_MESSAGE, "trying jump\n"); + cmdmove[2] = 400; + maxframes = PREDICTIONTIME_JUMP / 0.1; + cmdframes = 1; + stopevent = SE_HITGROUND | SE_HITGROUNDDAMAGE | + SE_ENTERWATER | SE_ENTERSLIME | SE_ENTERLAVA; + } //end if + else + { + maxframes = 2; + cmdframes = 2; + stopevent = SE_HITGROUNDDAMAGE | + SE_ENTERWATER | SE_ENTERSLIME | SE_ENTERLAVA; + } //end else + //AAS_ClearShownDebugLines(); + // + VectorCopy( ms->origin, origin ); + origin[2] += 0.5; + AAS_PredictClientMovement( &move, ms->entitynum, origin, presencetype, qtrue, + velocity, cmdmove, cmdframes, maxframes, 0.1, + stopevent, 0, qfalse ); //qtrue); + //if prediction time wasn't enough to fully predict the movement + if ( move.frames >= maxframes && ( type & MOVE_JUMP ) ) { + //botimport.Print(PRT_MESSAGE, "client %d: max prediction frames\n", ms->client); + return qfalse; + } //end if + //don't enter slime or lava and don't fall from too high + if ( move.stopevent & ( SE_ENTERLAVA | SE_HITGROUNDDAMAGE ) ) { //----(SA) modified since slime is no longer deadly +// if (move.stopevent & (SE_ENTERSLIME|SE_ENTERLAVA|SE_HITGROUNDDAMAGE)) + //botimport.Print(PRT_MESSAGE, "client %d: would be hurt ", ms->client); + //if (move.stopevent & SE_ENTERSLIME) botimport.Print(PRT_MESSAGE, "slime\n"); + //if (move.stopevent & SE_ENTERLAVA) botimport.Print(PRT_MESSAGE, "lava\n"); + //if (move.stopevent & SE_HITGROUNDDAMAGE) botimport.Print(PRT_MESSAGE, "hitground\n"); + return qfalse; + } //end if + //if ground was hit + if ( move.stopevent & SE_HITGROUND ) { + //check for nearby gap + VectorNormalize2( move.velocity, tmpdir ); + dist = BotGapDistance( move.endpos, tmpdir, ms->entitynum ); + if ( dist > 0 ) { + return qfalse; + } + // + dist = BotGapDistance( move.endpos, hordir, ms->entitynum ); + if ( dist > 0 ) { + return qfalse; + } + } //end if + //get horizontal movement + tmpdir[0] = move.endpos[0] - ms->origin[0]; + tmpdir[1] = move.endpos[1] - ms->origin[1]; + tmpdir[2] = 0; + // + //AAS_DrawCross(move.endpos, 4, LINECOLOR_BLUE); + //the bot is blocked by something + if ( VectorLength( tmpdir ) < speed * ms->thinktime * 0.5 ) { + return qfalse; + } + //perform the movement + if ( type & MOVE_JUMP ) { + EA_Jump( ms->client ); + } + if ( type & MOVE_CROUCH ) { + EA_Crouch( ms->client ); + } + EA_Move( ms->client, hordir, speed ); + //movement was succesfull + return qtrue; + } //end if + else + { + if ( ms->moveflags & MFL_BARRIERJUMP ) { + //if near the top or going down + if ( ms->velocity[2] < 50 ) { + EA_Move( ms->client, dir, speed ); + } //end if + } //end if + //FIXME: do air control to avoid hazards + return qtrue; + } //end else +} //end of the function BotWalkInDirection +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotMoveInDirection( int movestate, vec3_t dir, float speed, int type ) { + bot_movestate_t *ms; + + ms = BotMoveStateFromHandle( movestate ); + if ( !ms ) { + return qfalse; + } + //if swimming + if ( AAS_Swimming( ms->origin ) ) { + return BotSwimInDirection( ms, dir, speed, type ); + } //end if + else + { + return BotWalkInDirection( ms, dir, speed, type ); + } //end else +} //end of the function BotMoveInDirection +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int Intersection( vec2_t p1, vec2_t p2, vec2_t p3, vec2_t p4, vec2_t out ) { + float x1, dx1, dy1, x2, dx2, dy2, d; + + dx1 = p2[0] - p1[0]; + dy1 = p2[1] - p1[1]; + dx2 = p4[0] - p3[0]; + dy2 = p4[1] - p3[1]; + + d = dy1 * dx2 - dx1 * dy2; + if ( d != 0 ) { + x1 = p1[1] * dx1 - p1[0] * dy1; + x2 = p3[1] * dx2 - p3[0] * dy2; + out[0] = (int) ( ( dx1 * x2 - dx2 * x1 ) / d ); + out[1] = (int) ( ( dy1 * x2 - dy2 * x1 ) / d ); + return qtrue; + } //end if + else + { + return qfalse; + } //end else +} //end of the function Intersection +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotCheckBlocked( bot_movestate_t *ms, vec3_t dir, int checkbottom, bot_moveresult_t *result ) { + vec3_t mins, maxs, end, up = {0, 0, 1}; + bsp_trace_t trace; + + // RF, not required for Wolf AI + return; + + //test for entities obstructing the bot's path + AAS_PresenceTypeBoundingBox( ms->presencetype, mins, maxs ); + // + if ( fabs( DotProduct( dir, up ) ) < 0.7 ) { + mins[2] += sv_maxstep; //if the bot can step on + maxs[2] -= 10; //a little lower to avoid low ceiling + } //end if + VectorMA( ms->origin, 3, dir, end ); + trace = AAS_Trace( ms->origin, mins, maxs, end, ms->entitynum, CONTENTS_SOLID | CONTENTS_PLAYERCLIP | CONTENTS_BODY ); + //if not started in solid and not hitting the world entity + if ( !trace.startsolid && ( trace.ent != ENTITYNUM_WORLD && trace.ent != ENTITYNUM_NONE ) ) { + result->blocked = qtrue; + result->blockentity = trace.ent; +#ifdef DEBUG + //botimport.Print(PRT_MESSAGE, "%d: BotCheckBlocked: I'm blocked\n", ms->client); +#endif //DEBUG + } //end if + //if not in an area with reachability + else if ( checkbottom && !AAS_AreaReachability( ms->areanum ) ) { + //check if the bot is standing on something + AAS_PresenceTypeBoundingBox( ms->presencetype, mins, maxs ); + VectorMA( ms->origin, -3, up, end ); + trace = AAS_Trace( ms->origin, mins, maxs, end, ms->entitynum, CONTENTS_SOLID | CONTENTS_PLAYERCLIP ); + if ( !trace.startsolid && ( trace.ent != ENTITYNUM_WORLD && trace.ent != ENTITYNUM_NONE ) ) { + result->blocked = qtrue; + result->blockentity = trace.ent; + result->flags |= MOVERESULT_ONTOPOFOBSTACLE; +#ifdef DEBUG + //botimport.Print(PRT_MESSAGE, "%d: BotCheckBlocked: I'm blocked\n", ms->client); +#endif //DEBUG + } //end if + } //end else +} //end of the function BotCheckBlocked +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotClearMoveResult( bot_moveresult_t *moveresult ) { + moveresult->failure = qfalse; + moveresult->type = 0; + moveresult->blocked = qfalse; + moveresult->blockentity = 0; + moveresult->traveltype = 0; + moveresult->flags = 0; +} //end of the function BotClearMoveResult +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotTravel_Walk( bot_movestate_t *ms, aas_reachability_t *reach ) { + float dist, speed; + vec3_t hordir; + bot_moveresult_t result; + + BotClearMoveResult( &result ); + //first walk straight to the reachability start + hordir[0] = reach->start[0] - ms->origin[0]; + hordir[1] = reach->start[1] - ms->origin[1]; + hordir[2] = 0; + dist = VectorNormalize( hordir ); + // + BotCheckBlocked( ms, hordir, qtrue, &result ); + // + // Ridah, tweaked this +// if (dist < 10) + if ( dist < 32 ) { + //walk straight to the reachability end + hordir[0] = reach->end[0] - ms->origin[0]; + hordir[1] = reach->end[1] - ms->origin[1]; + hordir[2] = 0; + dist = VectorNormalize( hordir ); + } //end if + //if going towards a crouch area + + // Ridah, some areas have a 0 presence (!?!) +// if (!(AAS_AreaPresenceType(reach->areanum) & PRESENCE_NORMAL)) + if ( ( AAS_AreaPresenceType( reach->areanum ) & PRESENCE_CROUCH ) && + !( AAS_AreaPresenceType( reach->areanum ) & PRESENCE_NORMAL ) ) { + //if pretty close to the reachable area + if ( dist < 20 ) { + EA_Crouch( ms->client ); + } + } //end if + // + dist = BotGapDistance( ms->origin, hordir, ms->entitynum ); + // + if ( ms->moveflags & MFL_WALK ) { + if ( dist > 0 ) { + speed = 200 - ( 180 - 1 * dist ); + } else { speed = 200;} + EA_Walk( ms->client ); + } //end if + else + { + if ( dist > 0 ) { + speed = 400 - ( 360 - 2 * dist ); + } else { speed = 400;} + } //end else + //elemantary action move in direction + EA_Move( ms->client, hordir, speed ); + VectorCopy( hordir, result.movedir ); + // + return result; +} //end of the function BotTravel_Walk +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotFinishTravel_Walk( bot_movestate_t *ms, aas_reachability_t *reach ) { + vec3_t hordir; + float dist, speed; + bot_moveresult_t result; + + BotClearMoveResult( &result ); + //if not on the ground and changed areas... don't walk back!! + //(doesn't seem to help) + /* + ms->areanum = BotFuzzyPointReachabilityArea(ms->origin); + if (ms->areanum == reach->areanum) + { +#ifdef DEBUG + botimport.Print(PRT_MESSAGE, "BotFinishTravel_Walk: already in reach area\n"); +#endif //DEBUG + return result; + } //end if*/ + //go straight to the reachability end + hordir[0] = reach->end[0] - ms->origin[0]; + hordir[1] = reach->end[1] - ms->origin[1]; + hordir[2] = 0; + dist = VectorNormalize( hordir ); + // + if ( dist > 100 ) { + dist = 100; + } + speed = 400 - ( 400 - 3 * dist ); + // + EA_Move( ms->client, hordir, speed ); + VectorCopy( hordir, result.movedir ); + // + return result; +} //end of the function BotFinishTravel_Walk +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotTravel_Crouch( bot_movestate_t *ms, aas_reachability_t *reach ) { + float speed; + vec3_t hordir; + bot_moveresult_t result; + + BotClearMoveResult( &result ); + // + speed = 400; + //walk straight to reachability end + hordir[0] = reach->end[0] - ms->origin[0]; + hordir[1] = reach->end[1] - ms->origin[1]; + hordir[2] = 0; + VectorNormalize( hordir ); + // + BotCheckBlocked( ms, hordir, qtrue, &result ); + //elemantary actions + EA_Crouch( ms->client ); + EA_Move( ms->client, hordir, speed ); + // + VectorCopy( hordir, result.movedir ); + // + return result; +} //end of the function BotTravel_Crouch +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotTravel_BarrierJump( bot_movestate_t *ms, aas_reachability_t *reach ) { + float dist, speed; + vec3_t hordir; + bot_moveresult_t result; + + BotClearMoveResult( &result ); + //walk straight to reachability start + hordir[0] = reach->start[0] - ms->origin[0]; + hordir[1] = reach->start[1] - ms->origin[1]; + hordir[2] = 0; + dist = VectorNormalize( hordir ); + // + BotCheckBlocked( ms, hordir, qtrue, &result ); + //if pretty close to the barrier + if ( dist < 9 ) { + EA_Jump( ms->client ); + + // Ridah, do the movement also, so we have momentum to get onto the barrier + hordir[0] = reach->end[0] - reach->start[0]; + hordir[1] = reach->end[1] - reach->start[1]; + hordir[2] = 0; + VectorNormalize( hordir ); + + dist = 60; + speed = 360 - ( 360 - 6 * dist ); + EA_Move( ms->client, hordir, speed ); + // done. + } //end if + else + { + if ( dist > 60 ) { + dist = 60; + } + speed = 360 - ( 360 - 6 * dist ); + EA_Move( ms->client, hordir, speed ); + } //end else + VectorCopy( hordir, result.movedir ); + // + return result; +} //end of the function BotTravel_BarrierJump +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotFinishTravel_BarrierJump( bot_movestate_t *ms, aas_reachability_t *reach ) { + float dist, speed; + vec3_t hordir; + bot_moveresult_t result; + + BotClearMoveResult( &result ); + //if near the top or going down + if ( ms->velocity[2] < 250 ) { + // Ridah, extend the end point a bit, so we strive to get over the ledge more + vec3_t end; + + VectorSubtract( reach->end, reach->start, end ); + end[2] = 0; + VectorNormalize( end ); + VectorMA( reach->end, 32, end, end ); + hordir[0] = end[0] - ms->origin[0]; + hordir[1] = end[1] - ms->origin[1]; +// hordir[0] = reach->end[0] - ms->origin[0]; +// hordir[1] = reach->end[1] - ms->origin[1]; + // done. + hordir[2] = 0; + dist = VectorNormalize( hordir ); + // + BotCheckBlocked( ms, hordir, qtrue, &result ); + // + if ( dist > 60 ) { + dist = 60; + } + speed = 400 - ( 400 - 6 * dist ); + EA_Move( ms->client, hordir, speed ); + VectorCopy( hordir, result.movedir ); + } //end if + // + return result; +} //end of the function BotFinishTravel_BarrierJump +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotTravel_Swim( bot_movestate_t *ms, aas_reachability_t *reach ) { + vec3_t dir; + bot_moveresult_t result; + + BotClearMoveResult( &result ); + //swim straight to reachability end + VectorSubtract( reach->start, ms->origin, dir ); + VectorNormalize( dir ); + // + BotCheckBlocked( ms, dir, qtrue, &result ); + //elemantary actions + EA_Move( ms->client, dir, 400 ); + // + VectorCopy( dir, result.movedir ); + Vector2Angles( dir, result.ideal_viewangles ); + result.flags |= MOVERESULT_SWIMVIEW; + // + return result; +} //end of the function BotTravel_Swim +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotTravel_WaterJump( bot_movestate_t *ms, aas_reachability_t *reach ) { + vec3_t dir, hordir; + float dist; + bot_moveresult_t result; + + BotClearMoveResult( &result ); + //swim straight to reachability end + VectorSubtract( reach->end, ms->origin, dir ); + VectorCopy( dir, hordir ); + hordir[2] = 0; + dir[2] += 15 + crandom() * 40; + //botimport.Print(PRT_MESSAGE, "BotTravel_WaterJump: dir[2] = %f\n", dir[2]); + VectorNormalize( dir ); + dist = VectorNormalize( hordir ); + //elemantary actions + //EA_Move(ms->client, dir, 400); + EA_MoveForward( ms->client ); + //move up if close to the actual out of water jump spot + if ( dist < 40 ) { + EA_MoveUp( ms->client ); + } + //set the ideal view angles + Vector2Angles( dir, result.ideal_viewangles ); + result.flags |= MOVERESULT_MOVEMENTVIEW; + // + VectorCopy( dir, result.movedir ); + // + return result; +} //end of the function BotTravel_WaterJump +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotFinishTravel_WaterJump( bot_movestate_t *ms, aas_reachability_t *reach ) { + vec3_t dir, pnt; + float dist; + bot_moveresult_t result; + + //botimport.Print(PRT_MESSAGE, "BotFinishTravel_WaterJump\n"); + BotClearMoveResult( &result ); + //if waterjumping there's nothing to do + if ( ms->moveflags & MFL_WATERJUMP ) { + return result; + } + //if not touching any water anymore don't do anything + //otherwise the bot sometimes keeps jumping? + VectorCopy( ms->origin, pnt ); + pnt[2] -= 32; //extra for q2dm4 near red armor/mega health + if ( !( AAS_PointContents( pnt ) & ( CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER ) ) ) { + return result; + } + //swim straight to reachability end + VectorSubtract( reach->end, ms->origin, dir ); + dir[0] += crandom() * 10; + dir[1] += crandom() * 10; + dir[2] += 70 + crandom() * 10; + dist = VectorNormalize( dir ); + //elemantary actions + EA_Move( ms->client, dir, 400 ); + //set the ideal view angles + Vector2Angles( dir, result.ideal_viewangles ); + result.flags |= MOVERESULT_MOVEMENTVIEW; + // + VectorCopy( dir, result.movedir ); + // + return result; +} //end of the function BotFinishTravel_WaterJump +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotTravel_WalkOffLedge( bot_movestate_t *ms, aas_reachability_t *reach ) { + vec3_t hordir, dir; + float dist, speed, reachhordist; + bot_moveresult_t result; + + BotClearMoveResult( &result ); + //check if the bot is blocked by anything + VectorSubtract( reach->start, ms->origin, dir ); + VectorNormalize( dir ); + BotCheckBlocked( ms, dir, qtrue, &result ); + //if the reachability start and end are practially above each other + VectorSubtract( reach->end, reach->start, dir ); + dir[2] = 0; + reachhordist = VectorLength( dir ); + //walk straight to the reachability start + hordir[0] = reach->start[0] - ms->origin[0]; + hordir[1] = reach->start[1] - ms->origin[1]; + hordir[2] = 0; + dist = VectorNormalize( hordir ); + //if pretty close to the start focus on the reachability end + + // Ridah, tweaked this +#if 0 + if ( dist < 48 ) { + hordir[0] = reach->end[0] - ms->origin[0]; + hordir[1] = reach->end[1] - ms->origin[1]; + hordir[2] = 0; +#else + if ( ( dist < 72 ) && ( DotProduct( dir, hordir ) < 0 ) ) { // walk in the direction of start -> end + //hordir[0] = reach->end[0] - ms->origin[0]; + //hordir[1] = reach->end[1] - ms->origin[1]; + //VectorNormalize( dir ); + //VectorMA( hordir, 48, dir, hordir ); + //hordir[2] = 0; + + VectorCopy( dir, hordir ); +#endif + VectorNormalize( hordir ); + // + if ( reachhordist < 20 ) { + //speed = 100; + speed = 200; // RF, increased this to speed up travel speed down steps + } //end if + else if ( !AAS_HorizontalVelocityForJump( 0, reach->start, reach->end, &speed ) ) { + speed = 400; + } //end if + // looks better crouching off a ledge + EA_Crouch( ms->client ); + } //end if + else + { + if ( reachhordist < 20 ) { + if ( dist > 64 ) { + dist = 64; + } + //speed = 400 - (256 - 4 * dist); + speed = 400 - ( 256 - 4 * dist ) * 0.5; // RF changed this for steps + } //end if + else + { + speed = 400; + // Ridah, tweaked this + if ( dist < 128 ) { + speed *= 0.5 + 0.5 * ( dist / 128 ); + } + } //end else + } //end else + // + BotCheckBlocked( ms, hordir, qtrue, &result ); + //elemantary action + EA_Move( ms->client, hordir, speed ); + VectorCopy( hordir, result.movedir ); + // + return result; +} //end of the function BotTravel_WalkOffLedge +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotAirControl( vec3_t origin, vec3_t velocity, vec3_t goal, vec3_t dir, float *speed ) { + vec3_t org, vel; + float dist; + int i; + + VectorCopy( origin, org ); + VectorScale( velocity, 0.1, vel ); + for ( i = 0; i < 50; i++ ) + { + vel[2] -= sv_gravity * 0.01; + //if going down and next position would be below the goal + if ( vel[2] < 0 && org[2] + vel[2] < goal[2] ) { + VectorScale( vel, ( goal[2] - org[2] ) / vel[2], vel ); + VectorAdd( org, vel, org ); + VectorSubtract( goal, org, dir ); + dist = VectorNormalize( dir ); + if ( dist > 32 ) { + dist = 32; + } + *speed = 400 - ( 400 - 13 * dist ); + return qtrue; + } //end if + else + { + VectorAdd( org, vel, org ); + } //end else + } //end for + VectorSet( dir, 0, 0, 0 ); + *speed = 400; + return qfalse; +} //end of the function BotAirControl +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotFinishTravel_WalkOffLedge( bot_movestate_t *ms, aas_reachability_t *reach ) { + vec3_t dir, hordir, end, v; + float dist, speed; + bot_moveresult_t result; + + BotClearMoveResult( &result ); + // + VectorSubtract( reach->end, ms->origin, dir ); + BotCheckBlocked( ms, dir, qtrue, &result ); + // + VectorSubtract( reach->end, ms->origin, v ); + v[2] = 0; + dist = VectorNormalize( v ); + if ( dist > 16 ) { + VectorMA( reach->end, 16, v, end ); + } else { VectorCopy( reach->end, end );} + // + if ( !BotAirControl( ms->origin, ms->velocity, end, hordir, &speed ) ) { + //go straight to the reachability end + VectorCopy( dir, hordir ); + hordir[2] = 0; + // + dist = VectorNormalize( hordir ); + speed = 400; + } //end if + // + // looks better crouching off a ledge + EA_Crouch( ms->client ); + EA_Move( ms->client, hordir, speed ); + VectorCopy( hordir, result.movedir ); + // + return result; +} //end of the function BotFinishTravel_WalkOffLedge +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +/* +bot_moveresult_t BotTravel_Jump(bot_movestate_t *ms, aas_reachability_t *reach) +{ + vec3_t hordir; + float dist, gapdist, speed, horspeed, sv_jumpvel; + bot_moveresult_t result; + + BotClearMoveResult(&result); + // + sv_jumpvel = botlibglobals.sv_jumpvel->value; + //walk straight to the reachability start + hordir[0] = reach->start[0] - ms->origin[0]; + hordir[1] = reach->start[1] - ms->origin[1]; + hordir[2] = 0; + dist = VectorNormalize(hordir); + // + speed = 350; + // + gapdist = BotGapDistance(ms, hordir, ms->entitynum); + //if pretty close to the start focus on the reachability end + if (dist < 50 || (gapdist && gapdist < 50)) + { + //NOTE: using max speed (400) works best + //if (AAS_HorizontalVelocityForJump(sv_jumpvel, ms->origin, reach->end, &horspeed)) + //{ + // speed = horspeed * 400 / botlibglobals.sv_maxwalkvelocity->value; + //} //end if + hordir[0] = reach->end[0] - ms->origin[0]; + hordir[1] = reach->end[1] - ms->origin[1]; + VectorNormalize(hordir); + //elemantary action jump + EA_Jump(ms->client); + // + ms->jumpreach = ms->lastreachnum; + speed = 600; + } //end if + else + { + if (AAS_HorizontalVelocityForJump(sv_jumpvel, reach->start, reach->end, &horspeed)) + { + speed = horspeed * 400 / botlibglobals.sv_maxwalkvelocity->value; + } //end if + } //end else + //elemantary action + EA_Move(ms->client, hordir, speed); + VectorCopy(hordir, result.movedir); + // + return result; +} //end of the function BotTravel_Jump*/ +/* +bot_moveresult_t BotTravel_Jump(bot_movestate_t *ms, aas_reachability_t *reach) +{ + vec3_t hordir, dir1, dir2, mins, maxs, start, end; + float dist1, dist2, speed; + bot_moveresult_t result; + bsp_trace_t trace; + + BotClearMoveResult(&result); + // + hordir[0] = reach->start[0] - reach->end[0]; + hordir[1] = reach->start[1] - reach->end[1]; + hordir[2] = 0; + VectorNormalize(hordir); + // + VectorCopy(reach->start, start); + start[2] += 1; + //minus back the bouding box size plus 16 + VectorMA(reach->start, 80, hordir, end); + // + AAS_PresenceTypeBoundingBox(PRESENCE_NORMAL, mins, maxs); + //check for solids + trace = AAS_Trace(start, mins, maxs, end, ms->entitynum, MASK_PLAYERSOLID); + if (trace.startsolid) VectorCopy(start, trace.endpos); + //check for a gap + for (dist1 = 0; dist1 < 80; dist1 += 10) + { + VectorMA(start, dist1+10, hordir, end); + end[2] += 1; + if (AAS_PointAreaNum(end) != ms->reachareanum) break; + } //end for + if (dist1 < 80) VectorMA(reach->start, dist1, hordir, trace.endpos); +// dist1 = BotGapDistance(start, hordir, ms->entitynum); +// if (dist1 && dist1 <= trace.fraction * 80) VectorMA(reach->start, dist1-20, hordir, trace.endpos); + // + VectorSubtract(ms->origin, reach->start, dir1); + dir1[2] = 0; + dist1 = VectorNormalize(dir1); + VectorSubtract(ms->origin, trace.endpos, dir2); + dir2[2] = 0; + dist2 = VectorNormalize(dir2); + //if just before the reachability start + if (DotProduct(dir1, dir2) < -0.8 || dist2 < 5) + { + //botimport.Print(PRT_MESSAGE, "between jump start and run to point\n"); + hordir[0] = reach->end[0] - ms->origin[0]; + hordir[1] = reach->end[1] - ms->origin[1]; + hordir[2] = 0; + VectorNormalize(hordir); + //elemantary action jump + if (dist1 < 24) EA_Jump(ms->client); + else if (dist1 < 32) EA_DelayedJump(ms->client); + EA_Move(ms->client, hordir, 600); + // + ms->jumpreach = ms->lastreachnum; + } //end if + else + { + //botimport.Print(PRT_MESSAGE, "going towards run to point\n"); + hordir[0] = trace.endpos[0] - ms->origin[0]; + hordir[1] = trace.endpos[1] - ms->origin[1]; + hordir[2] = 0; + VectorNormalize(hordir); + // + if (dist2 > 80) dist2 = 80; + speed = 400 - (400 - 5 * dist2); + EA_Move(ms->client, hordir, speed); + } //end else + VectorCopy(hordir, result.movedir); + // + return result; +} //end of the function BotTravel_Jump*/ +//* +bot_moveresult_t BotTravel_Jump( bot_movestate_t *ms, aas_reachability_t *reach ) { + vec3_t hordir, dir1, dir2, start, end, runstart; +// vec3_t runstart, dir1, dir2, hordir; + float dist1, dist2, speed; + bot_moveresult_t result; + + BotClearMoveResult( &result ); + // + AAS_JumpReachRunStart( reach, runstart ); + //* + hordir[0] = runstart[0] - reach->start[0]; + hordir[1] = runstart[1] - reach->start[1]; + hordir[2] = 0; + VectorNormalize( hordir ); + // + VectorCopy( reach->start, start ); + start[2] += 1; + VectorMA( reach->start, 80, hordir, runstart ); + //check for a gap + for ( dist1 = 0; dist1 < 80; dist1 += 10 ) + { + VectorMA( start, dist1 + 10, hordir, end ); + end[2] += 1; + if ( AAS_PointAreaNum( end ) != ms->reachareanum ) { + break; + } + } //end for + if ( dist1 < 80 ) { + VectorMA( reach->start, dist1, hordir, runstart ); + } + // + VectorSubtract( ms->origin, reach->start, dir1 ); + dir1[2] = 0; + dist1 = VectorNormalize( dir1 ); + VectorSubtract( ms->origin, runstart, dir2 ); + dir2[2] = 0; + dist2 = VectorNormalize( dir2 ); + //if just before the reachability start + if ( DotProduct( dir1, dir2 ) < -0.8 || dist2 < 5 ) { +// botimport.Print(PRT_MESSAGE, "between jump start and run start point\n"); + hordir[0] = reach->end[0] - ms->origin[0]; + hordir[1] = reach->end[1] - ms->origin[1]; + hordir[2] = 0; + VectorNormalize( hordir ); + //elemantary action jump + if ( dist1 < 24 ) { + EA_Jump( ms->client ); + } else if ( dist1 < 32 ) { + EA_DelayedJump( ms->client ); + } + EA_Move( ms->client, hordir, 600 ); + // + ms->jumpreach = ms->lastreachnum; + } //end if + else + { +// botimport.Print(PRT_MESSAGE, "going towards run start point\n"); + hordir[0] = runstart[0] - ms->origin[0]; + hordir[1] = runstart[1] - ms->origin[1]; + hordir[2] = 0; + VectorNormalize( hordir ); + // + if ( dist2 > 80 ) { + dist2 = 80; + } + speed = 400 - ( 400 - 5 * dist2 ); + EA_Move( ms->client, hordir, speed ); + } //end else + VectorCopy( hordir, result.movedir ); + // + return result; +} //end of the function BotTravel_Jump*/ +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotFinishTravel_Jump( bot_movestate_t *ms, aas_reachability_t *reach ) { + vec3_t hordir, hordir2; + float speed, dist; + bot_moveresult_t result; + + BotClearMoveResult( &result ); + //if not jumped yet + if ( !ms->jumpreach ) { + return result; + } + //go straight to the reachability end + hordir[0] = reach->end[0] - ms->origin[0]; + hordir[1] = reach->end[1] - ms->origin[1]; + hordir[2] = 0; + dist = VectorNormalize( hordir ); + // + hordir2[0] = reach->end[0] - reach->start[0]; + hordir2[1] = reach->end[1] - reach->start[1]; + hordir2[2] = 0; + VectorNormalize( hordir2 ); + // + if ( DotProduct( hordir, hordir2 ) < -0.5 && dist < 24 ) { + return result; + } + //always use max speed when traveling through the air + speed = 800; + // + EA_Move( ms->client, hordir, speed ); + VectorCopy( hordir, result.movedir ); + // + return result; +} //end of the function BotFinishTravel_Jump +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotTravel_Ladder( bot_movestate_t *ms, aas_reachability_t *reach ) { + //float dist, speed; + vec3_t dir, viewdir, hordir, pos, p, v1, v2, vec, right; + vec3_t origin = {0, 0, 0}; +// vec3_t up = {0, 0, 1}; + bot_moveresult_t result; + float dist, speed; + + // RF, heavily modified, wolf has different ladder movement + + BotClearMoveResult( &result ); + // + if ( ( ms->moveflags & MFL_AGAINSTLADDER ) + //NOTE: not a good idea for ladders starting in water + || !( ms->moveflags & MFL_ONGROUND ) ) { + //botimport.Print(PRT_MESSAGE, "against ladder or not on ground\n"); + // RF, wolf has different ladder movement + VectorSubtract( reach->end, reach->start, dir ); + VectorNormalize( dir ); + //set the ideal view angles, facing the ladder up or down + viewdir[0] = dir[0]; + viewdir[1] = dir[1]; + viewdir[2] = dir[2]; + if ( dir[2] < 0 ) { // going down, so face the other way (towards ladder) + VectorInverse( viewdir ); + } + viewdir[2] = 0; // straight forward goes up + VectorNormalize( viewdir ); + Vector2Angles( viewdir, result.ideal_viewangles ); + //elemantary action + EA_Move( ms->client, origin, 0 ); + if ( dir[2] < 0 ) { // going down, so face the other way + EA_MoveBack( ms->client ); + } else { + EA_MoveForward( ms->client ); + } + // check for sideways adjustments to stay on the center of the ladder + VectorMA( ms->origin, 18, viewdir, p ); + VectorCopy( reach->start, v1 ); + v1[2] = ms->origin[2]; + VectorCopy( reach->end, v2 ); + v2[2] = ms->origin[2]; + VectorSubtract( v2, v1, vec ); + VectorNormalize( vec ); + VectorMA( v1, -32, vec, v1 ); + VectorMA( v2, 32, vec, v2 ); + ProjectPointOntoVector( p, v1, v2, pos ); + VectorSubtract( pos, p, vec ); + if ( VectorLength( vec ) > 2 ) { + AngleVectors( result.ideal_viewangles, NULL, right, NULL ); + if ( DotProduct( vec, right ) > 0 ) { + EA_MoveRight( ms->client ); + } else { + EA_MoveLeft( ms->client ); + } + } + //set movement view flag so the AI can see the view is focussed + result.flags |= MOVERESULT_MOVEMENTVIEW; + } //end if + else + { + //botimport.Print(PRT_MESSAGE, "moving towards ladder base\n"); + // find a postion back away from the base of the ladder + VectorSubtract( reach->end, reach->start, hordir ); + hordir[2] = 0; + VectorNormalize( hordir ); + VectorMA( reach->start, -24, hordir, pos ); + // project our position onto the vector + ProjectPointOntoVector( ms->origin, pos, reach->start, p ); + VectorSubtract( p, ms->origin, dir ); + //make sure the horizontal movement is large anough + VectorCopy( dir, hordir ); + hordir[2] = 0; + dist = VectorNormalize( hordir ); + if ( dist < 8 ) { // within range, go for the end + //botimport.Print(PRT_MESSAGE, "found base, moving towards ladder top\n"); + VectorSubtract( reach->end, ms->origin, dir ); + //make sure the horizontal movement is large anough + VectorCopy( dir, hordir ); + hordir[2] = 0; + dist = VectorNormalize( hordir ); + //set the ideal view angles, facing the ladder up or down + viewdir[0] = dir[0]; + viewdir[1] = dir[1]; + viewdir[2] = dir[2]; + if ( dir[2] < 0 ) { // going down, so face the other way + VectorInverse( viewdir ); + } + viewdir[2] = 0; + VectorNormalize( viewdir ); + Vector2Angles( viewdir, result.ideal_viewangles ); + result.flags |= MOVERESULT_MOVEMENTVIEW; + } + // + dir[0] = hordir[0]; + dir[1] = hordir[1]; +// if (dist < 48) { +// if (dir[2] > 0) dir[2] = 1; +// else dir[2] = -1; +// } else { + dir[2] = 0; +// } + if ( dist > 50 ) { + dist = 50; + } + speed = 400 - ( 300 - 6 * dist ); + EA_Move( ms->client, dir, speed ); + } //end else + //save the movement direction + VectorCopy( dir, result.movedir ); + // + return result; +} //end of the function BotTravel_Ladder +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotTravel_Teleport( bot_movestate_t *ms, aas_reachability_t *reach ) { + vec3_t hordir; + float dist; + bot_moveresult_t result; + + BotClearMoveResult( &result ); + //if the bot is being teleported + if ( ms->moveflags & MFL_TELEPORTED ) { + return result; + } + + //walk straight to center of the teleporter + VectorSubtract( reach->start, ms->origin, hordir ); + if ( !( ms->moveflags & MFL_SWIMMING ) ) { + hordir[2] = 0; + } + dist = VectorNormalize( hordir ); + // + BotCheckBlocked( ms, hordir, qtrue, &result ); + + if ( dist < 30 ) { + EA_Move( ms->client, hordir, 200 ); + } else { EA_Move( ms->client, hordir, 400 );} + + if ( ms->moveflags & MFL_SWIMMING ) { + result.flags |= MOVERESULT_SWIMVIEW; + } + + VectorCopy( hordir, result.movedir ); + return result; +} //end of the function BotTravel_Teleport +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotTravel_Elevator( bot_movestate_t *ms, aas_reachability_t *reach ) { + vec3_t dir, dir1, dir2, hordir, bottomcenter; + float dist, dist1, dist2, speed; + bot_moveresult_t result; + + BotClearMoveResult( &result ); + //if standing on the plat + if ( BotOnMover( ms->origin, ms->entitynum, reach ) ) { +#ifdef DEBUG_ELEVATOR + botimport.Print( PRT_MESSAGE, "bot on elevator\n" ); +#endif //DEBUG_ELEVATOR + //if vertically not too far from the end point + if ( abs( ms->origin[2] - reach->end[2] ) < sv_maxbarrier ) { +#ifdef DEBUG_ELEVATOR + botimport.Print( PRT_MESSAGE, "bot moving to end\n" ); +#endif //DEBUG_ELEVATOR + //move to the end point + VectorSubtract( reach->end, ms->origin, hordir ); + hordir[2] = 0; + VectorNormalize( hordir ); + if ( !BotCheckBarrierJump( ms, hordir, 100 ) ) { + EA_Move( ms->client, hordir, 400 ); + } //end if + VectorCopy( hordir, result.movedir ); + } //end else + //if not really close to the center of the elevator + else + { + MoverBottomCenter( reach, bottomcenter ); + VectorSubtract( bottomcenter, ms->origin, hordir ); + hordir[2] = 0; + dist = VectorNormalize( hordir ); + // + if ( dist > 10 ) { +#ifdef DEBUG_ELEVATOR + botimport.Print( PRT_MESSAGE, "bot moving to center\n" ); +#endif //DEBUG_ELEVATOR + //move to the center of the plat + if ( dist > 100 ) { + dist = 100; + } + speed = 400 - ( 400 - 4 * dist ); + // + EA_Move( ms->client, hordir, speed ); + VectorCopy( hordir, result.movedir ); + } //end if + } //end else + } //end if + else + { +#ifdef DEBUG_ELEVATOR + botimport.Print( PRT_MESSAGE, "bot not on elevator\n" ); +#endif //DEBUG_ELEVATOR + //if very near the reachability end + VectorSubtract( reach->end, ms->origin, dir ); + dist = VectorLength( dir ); + if ( dist < 64 ) { + if ( dist > 60 ) { + dist = 60; + } + speed = 360 - ( 360 - 6 * dist ); + // + if ( ( ms->moveflags & MFL_SWIMMING ) || !BotCheckBarrierJump( ms, dir, 50 ) ) { + if ( speed > 5 ) { + EA_Move( ms->client, dir, speed ); + } + } //end if + VectorCopy( dir, result.movedir ); + // + if ( ms->moveflags & MFL_SWIMMING ) { + result.flags |= MOVERESULT_SWIMVIEW; + } + //stop using this reachability + ms->reachability_time = 0; + return result; + } //end if + //get direction and distance to reachability start + VectorSubtract( reach->start, ms->origin, dir1 ); + if ( !( ms->moveflags & MFL_SWIMMING ) ) { + dir1[2] = 0; + } + dist1 = VectorNormalize( dir1 ); + //if the elevator isn't down + if ( !MoverDown( reach ) ) { +#ifdef DEBUG_ELEVATOR + botimport.Print( PRT_MESSAGE, "elevator not down\n" ); +#endif //DEBUG_ELEVATOR + dist = dist1; + VectorCopy( dir1, dir ); + // + BotCheckBlocked( ms, dir, qfalse, &result ); + // + if ( dist > 60 ) { + dist = 60; + } + speed = 360 - ( 360 - 6 * dist ); + // + if ( !( ms->moveflags & MFL_SWIMMING ) && !BotCheckBarrierJump( ms, dir, 50 ) ) { + if ( speed > 5 ) { + EA_Move( ms->client, dir, speed ); + } + } //end if + VectorCopy( dir, result.movedir ); + // + if ( ms->moveflags & MFL_SWIMMING ) { + result.flags |= MOVERESULT_SWIMVIEW; + } + //this isn't a failure... just wait till the elevator comes down + //result.failure = qtrue; + result.type = RESULTTYPE_ELEVATORUP; + result.flags |= MOVERESULT_WAITING; + return result; + } //end if + //get direction and distance to elevator bottom center + MoverBottomCenter( reach, bottomcenter ); + VectorSubtract( bottomcenter, ms->origin, dir2 ); + if ( !( ms->moveflags & MFL_SWIMMING ) ) { + dir2[2] = 0; + } + dist2 = VectorNormalize( dir2 ); + //if very close to the reachability start or + //closer to the elevator center or + //between reachability start and elevator center + if ( dist1 < 20 || dist2 < dist1 || DotProduct( dir1, dir2 ) < 0 ) { +#ifdef DEBUG_ELEVATOR + botimport.Print( PRT_MESSAGE, "bot moving to center\n" ); +#endif //DEBUG_ELEVATOR + dist = dist2; + VectorCopy( dir2, dir ); + } //end if + else //closer to the reachability start + { +#ifdef DEBUG_ELEVATOR + botimport.Print( PRT_MESSAGE, "bot moving to start\n" ); +#endif //DEBUG_ELEVATOR + dist = dist1; + VectorCopy( dir1, dir ); + } //end else + // + BotCheckBlocked( ms, dir, qfalse, &result ); + // + if ( dist > 60 ) { + dist = 60; + } + speed = 400 - ( 400 - 6 * dist ); + // + if ( !( ms->moveflags & MFL_SWIMMING ) && !BotCheckBarrierJump( ms, dir, 50 ) ) { + EA_Move( ms->client, dir, speed ); + } //end if + VectorCopy( dir, result.movedir ); + // + if ( ms->moveflags & MFL_SWIMMING ) { + result.flags |= MOVERESULT_SWIMVIEW; + } + } //end else + return result; +} //end of the function BotTravel_Elevator +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotFinishTravel_Elevator( bot_movestate_t *ms, aas_reachability_t *reach ) { + vec3_t bottomcenter, bottomdir, topdir; + bot_moveresult_t result; + + BotClearMoveResult( &result ); + // + MoverBottomCenter( reach, bottomcenter ); + VectorSubtract( bottomcenter, ms->origin, bottomdir ); + // + VectorSubtract( reach->end, ms->origin, topdir ); + // + if ( fabs( bottomdir[2] ) < fabs( topdir[2] ) ) { + VectorNormalize( bottomdir ); + EA_Move( ms->client, bottomdir, 300 ); + } //end if + else + { + VectorNormalize( topdir ); + EA_Move( ms->client, topdir, 300 ); + } //end else + return result; +} //end of the function BotFinishTravel_Elevator +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotFuncBobStartEnd( aas_reachability_t *reach, vec3_t start, vec3_t end, vec3_t origin ) { + int spawnflags, modelnum; + vec3_t mins, maxs, mid, angles = {0, 0, 0}; + int num0, num1; + + modelnum = reach->facenum & 0x0000FFFF; + if ( !AAS_OriginOfEntityWithModelNum( modelnum, origin ) ) { + botimport.Print( PRT_MESSAGE, "BotFuncBobStartEnd: no entity with model %d\n", modelnum ); + VectorSet( start, 0, 0, 0 ); + VectorSet( end, 0, 0, 0 ); + return; + } //end if + AAS_BSPModelMinsMaxsOrigin( modelnum, angles, mins, maxs, NULL ); + VectorAdd( mins, maxs, mid ); + VectorScale( mid, 0.5, mid ); + VectorCopy( mid, start ); + VectorCopy( mid, end ); + spawnflags = reach->facenum >> 16; + num0 = reach->edgenum >> 16; + if ( num0 > 0x00007FFF ) { + num0 |= 0xFFFF0000; + } + num1 = reach->edgenum & 0x0000FFFF; + if ( num1 > 0x00007FFF ) { + num1 |= 0xFFFF0000; + } + if ( spawnflags & 1 ) { + start[0] = num0; + end[0] = num1; + // + origin[0] += mid[0]; + origin[1] = mid[1]; + origin[2] = mid[2]; + } //end if + else if ( spawnflags & 2 ) { + start[1] = num0; + end[1] = num1; + // + origin[0] = mid[0]; + origin[1] += mid[1]; + origin[2] = mid[2]; + } //end else if + else + { + start[2] = num0; + end[2] = num1; + // + origin[0] = mid[0]; + origin[1] = mid[1]; + origin[2] += mid[2]; + } //end else +} //end of the function BotFuncBobStartEnd +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotTravel_FuncBobbing( bot_movestate_t *ms, aas_reachability_t *reach ) { + vec3_t dir, dir1, dir2, hordir, bottomcenter, bob_start, bob_end, bob_origin; + float dist, dist1, dist2, speed; + bot_moveresult_t result; + + BotClearMoveResult( &result ); + // + BotFuncBobStartEnd( reach, bob_start, bob_end, bob_origin ); + //if standing ontop of the func_bobbing + if ( BotOnMover( ms->origin, ms->entitynum, reach ) ) { +#ifdef DEBUG_FUNCBOB + botimport.Print( PRT_MESSAGE, "bot on func_bobbing\n" ); +#endif + //if near end point of reachability + VectorSubtract( bob_origin, bob_end, dir ); + if ( VectorLength( dir ) < 24 ) { +#ifdef DEBUG_FUNCBOB + botimport.Print( PRT_MESSAGE, "bot moving to reachability end\n" ); +#endif + //move to the end point + VectorSubtract( reach->end, ms->origin, hordir ); + hordir[2] = 0; + VectorNormalize( hordir ); + if ( !BotCheckBarrierJump( ms, hordir, 100 ) ) { + EA_Move( ms->client, hordir, 400 ); + } //end if + VectorCopy( hordir, result.movedir ); + } //end else + //if not really close to the center of the elevator + else + { + MoverBottomCenter( reach, bottomcenter ); + VectorSubtract( bottomcenter, ms->origin, hordir ); + hordir[2] = 0; + dist = VectorNormalize( hordir ); + // + if ( dist > 10 ) { +#ifdef DEBUG_FUNCBOB + botimport.Print( PRT_MESSAGE, "bot moving to func_bobbing center\n" ); +#endif + //move to the center of the plat + if ( dist > 100 ) { + dist = 100; + } + speed = 400 - ( 400 - 4 * dist ); + // + EA_Move( ms->client, hordir, speed ); + VectorCopy( hordir, result.movedir ); + } //end if + } //end else + } //end if + else + { +#ifdef DEBUG_FUNCBOB + botimport.Print( PRT_MESSAGE, "bot not ontop of func_bobbing\n" ); +#endif + //if very near the reachability end + VectorSubtract( reach->end, ms->origin, dir ); + dist = VectorLength( dir ); + if ( dist < 64 ) { +#ifdef DEBUG_FUNCBOB + botimport.Print( PRT_MESSAGE, "bot moving to end\n" ); +#endif + if ( dist > 60 ) { + dist = 60; + } + speed = 360 - ( 360 - 6 * dist ); + //if swimming or no barrier jump + if ( ( ms->moveflags & MFL_SWIMMING ) || !BotCheckBarrierJump( ms, dir, 50 ) ) { + if ( speed > 5 ) { + EA_Move( ms->client, dir, speed ); + } + } //end if + VectorCopy( dir, result.movedir ); + // + if ( ms->moveflags & MFL_SWIMMING ) { + result.flags |= MOVERESULT_SWIMVIEW; + } + //stop using this reachability + ms->reachability_time = 0; + return result; + } //end if + //get direction and distance to reachability start + VectorSubtract( reach->start, ms->origin, dir1 ); + if ( !( ms->moveflags & MFL_SWIMMING ) ) { + dir1[2] = 0; + } + dist1 = VectorNormalize( dir1 ); + //if func_bobbing is Not it's start position + VectorSubtract( bob_origin, bob_start, dir ); + if ( VectorLength( dir ) > 16 ) { +#ifdef DEBUG_FUNCBOB + botimport.Print( PRT_MESSAGE, "func_bobbing not at start\n" ); +#endif + dist = dist1; + VectorCopy( dir1, dir ); + // + BotCheckBlocked( ms, dir, qfalse, &result ); + // + if ( dist > 60 ) { + dist = 60; + } + speed = 360 - ( 360 - 6 * dist ); + // + if ( !( ms->moveflags & MFL_SWIMMING ) && !BotCheckBarrierJump( ms, dir, 50 ) ) { + if ( speed > 5 ) { + EA_Move( ms->client, dir, speed ); + } + } //end if + VectorCopy( dir, result.movedir ); + // + if ( ms->moveflags & MFL_SWIMMING ) { + result.flags |= MOVERESULT_SWIMVIEW; + } + //this isn't a failure... just wait till the func_bobbing arrives + result.type = RESULTTYPE_ELEVATORUP; + result.flags |= MOVERESULT_WAITING; + return result; + } //end if + //get direction and distance to func_bob bottom center + MoverBottomCenter( reach, bottomcenter ); + VectorSubtract( bottomcenter, ms->origin, dir2 ); + if ( !( ms->moveflags & MFL_SWIMMING ) ) { + dir2[2] = 0; + } + dist2 = VectorNormalize( dir2 ); + //if very close to the reachability start or + //closer to the elevator center or + //between reachability start and func_bobbing center + if ( dist1 < 20 || dist2 < dist1 || DotProduct( dir1, dir2 ) < 0 ) { +#ifdef DEBUG_FUNCBOB + botimport.Print( PRT_MESSAGE, "bot moving to func_bobbing center\n" ); +#endif + dist = dist2; + VectorCopy( dir2, dir ); + } //end if + else //closer to the reachability start + { +#ifdef DEBUG_FUNCBOB + botimport.Print( PRT_MESSAGE, "bot moving to reachability start\n" ); +#endif + dist = dist1; + VectorCopy( dir1, dir ); + } //end else + // + BotCheckBlocked( ms, dir, qfalse, &result ); + // + if ( dist > 60 ) { + dist = 60; + } + speed = 400 - ( 400 - 6 * dist ); + // + if ( !( ms->moveflags & MFL_SWIMMING ) && !BotCheckBarrierJump( ms, dir, 50 ) ) { + EA_Move( ms->client, dir, speed ); + } //end if + VectorCopy( dir, result.movedir ); + // + if ( ms->moveflags & MFL_SWIMMING ) { + result.flags |= MOVERESULT_SWIMVIEW; + } + } //end else + return result; +} //end of the function BotTravel_FuncBobbing +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotFinishTravel_FuncBobbing( bot_movestate_t *ms, aas_reachability_t *reach ) { + vec3_t bob_origin, bob_start, bob_end, dir, hordir, bottomcenter; + bot_moveresult_t result; + float dist, speed; + + BotClearMoveResult( &result ); + // + BotFuncBobStartEnd( reach, bob_start, bob_end, bob_origin ); + // + VectorSubtract( bob_origin, bob_end, dir ); + dist = VectorLength( dir ); + //if the func_bobbing is near the end + if ( dist < 16 ) { + VectorSubtract( reach->end, ms->origin, hordir ); + if ( !( ms->moveflags & MFL_SWIMMING ) ) { + hordir[2] = 0; + } + dist = VectorNormalize( hordir ); + // + if ( dist > 60 ) { + dist = 60; + } + speed = 360 - ( 360 - 6 * dist ); + // + if ( speed > 5 ) { + EA_Move( ms->client, dir, speed ); + } + VectorCopy( dir, result.movedir ); + // + if ( ms->moveflags & MFL_SWIMMING ) { + result.flags |= MOVERESULT_SWIMVIEW; + } + } //end if + else + { + MoverBottomCenter( reach, bottomcenter ); + VectorSubtract( bottomcenter, ms->origin, hordir ); + if ( !( ms->moveflags & MFL_SWIMMING ) ) { + hordir[2] = 0; + } + dist = VectorNormalize( hordir ); + // + if ( dist > 5 ) { + //move to the center of the plat + if ( dist > 100 ) { + dist = 100; + } + speed = 400 - ( 400 - 4 * dist ); + // + EA_Move( ms->client, hordir, speed ); + VectorCopy( hordir, result.movedir ); + } //end if + } //end else + return result; +} //end of the function BotFinishTravel_FuncBobbing +//=========================================================================== +// 0 no valid grapple hook visible +// 1 the grapple hook is still flying +// 2 the grapple hooked into a wall +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +/* +int GrappleState(bot_movestate_t *ms, aas_reachability_t *reach) +{ + static int grapplemodelindex; + int i; + vec3_t dir; + aas_entityinfo_t entinfo; + + if (!grapplemodelindex) + { + grapplemodelindex = AAS_IndexFromModel("models/weapons/grapple/hook/tris.md2"); + } //end if + for (i = AAS_NextEntity(0); i; i = AAS_NextEntity(i)) + { + if (AAS_EntityModelindex(i) == grapplemodelindex) + { + AAS_EntityInfo(i, &entinfo); + if (VectorCompare(entinfo.origin, entinfo.old_origin)) + { + VectorSubtract(entinfo.origin, reach->end, dir); + //if hooked near the reachability end + if (VectorLength(dir) < 32) return 2; + } //end if + else + { + //still shooting hook + return 1; + } //end else + } //end if + } //end if + //no valid grapple at all + return 0; +} //end of the function GrappleState*/ +//=========================================================================== +// 0 no valid grapple hook visible +// 1 the grapple hook is still flying +// 2 the grapple hooked into a wall +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int GrappleState( bot_movestate_t *ms, aas_reachability_t *reach ) { + static int grapplemodelindex; + static libvar_t *laserhook; + int i; + vec3_t dir; + aas_entityinfo_t entinfo; + + if ( !laserhook ) { + laserhook = LibVar( "laserhook", "0" ); + } + if ( !laserhook->value && !grapplemodelindex ) { + grapplemodelindex = AAS_IndexFromModel( "models/weapons/grapple/hook/tris.md2" ); + } //end if + for ( i = AAS_NextEntity( 0 ); i; i = AAS_NextEntity( i ) ) + { + if ( ( !laserhook->value && AAS_EntityModelindex( i ) == grapplemodelindex ) +// || (laserhook->value && (AAS_EntityRenderFX(i) & RF_BEAM)) + ) { + AAS_EntityInfo( i, &entinfo ); + //if the origin is equal to the last visible origin + if ( VectorCompare( entinfo.origin, entinfo.lastvisorigin ) ) { + VectorSubtract( entinfo.origin, reach->end, dir ); + //if hooked near the reachability end + if ( VectorLength( dir ) < 32 ) { + return 2; + } + } //end if + else + { + //still shooting hook + return 1; + } //end else + } //end if + } //end for + //no valid grapple at all + return 0; +} //end of the function GrappleState +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotResetGrapple( bot_movestate_t *ms ) { + aas_reachability_t reach; + + AAS_ReachabilityFromNum( ms->lastreachnum, &reach ); + //if not using the grapple hook reachability anymore + if ( reach.traveltype != TRAVEL_GRAPPLEHOOK ) { + if ( ( ms->moveflags & MFL_ACTIVEGRAPPLE ) || ms->grapplevisible_time ) { + EA_Command( ms->client, CMD_HOOKOFF ); + ms->moveflags &= ~MFL_ACTIVEGRAPPLE; + ms->grapplevisible_time = 0; +#ifdef DEBUG_GRAPPLE + botimport.Print( PRT_MESSAGE, "reset grapple\n" ); +#endif //DEBUG_GRAPPLE + } //end if + } //end if +} //end of the function BotResetGrapple +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotTravel_Grapple( bot_movestate_t *ms, aas_reachability_t *reach ) { + bot_moveresult_t result; + float dist, speed; + vec3_t dir, viewdir, org; + int state, areanum; + +#ifdef DEBUG_GRAPPLE + static int debugline; + if ( !debugline ) { + debugline = botimport.DebugLineCreate(); + } + botimport.DebugLineShow( debugline, reach->start, reach->end, LINECOLOR_BLUE ); +#endif //DEBUG_GRAPPLE + + BotClearMoveResult( &result ); + // + if ( ms->moveflags & MFL_GRAPPLERESET ) { + EA_Command( ms->client, CMD_HOOKOFF ); + ms->moveflags &= ~MFL_ACTIVEGRAPPLE; + return result; + } //end if + // + if ( ms->moveflags & MFL_ACTIVEGRAPPLE ) { +#ifdef DEBUG_GRAPPLE + botimport.Print( PRT_MESSAGE, "BotTravel_Grapple: active grapple\n" ); +#endif //DEBUG_GRAPPLE + // + state = GrappleState( ms, reach ); + // + VectorSubtract( reach->end, ms->origin, dir ); + dir[2] = 0; + dist = VectorLength( dir ); + //if very close to the grapple end or + //the grappled is hooked and the bot doesn't get any closer + if ( state && dist < 48 ) { + if ( ms->lastgrappledist - dist < 1 ) { + EA_Command( ms->client, CMD_HOOKOFF ); + ms->moveflags &= ~MFL_ACTIVEGRAPPLE; + ms->moveflags |= MFL_GRAPPLERESET; + ms->reachability_time = 0; //end the reachability +#ifdef DEBUG_GRAPPLE + botimport.Print( PRT_ERROR, "grapple normal end\n" ); +#endif //DEBUG_GRAPPLE + } //end if + } //end if + //if no valid grapple at all, or the grapple hooked and the bot + //isn't moving anymore + else if ( !state || ( state == 2 && dist > ms->lastgrappledist - 2 ) ) { + if ( ms->grapplevisible_time < AAS_Time() - 0.4 ) { +#ifdef DEBUG_GRAPPLE + botimport.Print( PRT_ERROR, "grapple not visible\n" ); +#endif //DEBUG_GRAPPLE + EA_Command( ms->client, CMD_HOOKOFF ); + ms->moveflags &= ~MFL_ACTIVEGRAPPLE; + ms->moveflags |= MFL_GRAPPLERESET; + ms->reachability_time = 0; //end the reachability + //result.failure = qtrue; + //result.type = RESULTTYPE_INVISIBLEGRAPPLE; + return result; + } //end if + } //end if + else + { + ms->grapplevisible_time = AAS_Time(); + } //end else + //remember the current grapple distance + ms->lastgrappledist = dist; + } //end if + else + { +#ifdef DEBUG_GRAPPLE + botimport.Print( PRT_MESSAGE, "BotTravel_Grapple: inactive grapple\n" ); +#endif //DEBUG_GRAPPLE + // + ms->grapplevisible_time = AAS_Time(); + // + VectorSubtract( reach->start, ms->origin, dir ); + if ( !( ms->moveflags & MFL_SWIMMING ) ) { + dir[2] = 0; + } + VectorAdd( ms->origin, ms->viewoffset, org ); + VectorSubtract( reach->end, org, viewdir ); + // + dist = VectorNormalize( dir ); + Vector2Angles( viewdir, result.ideal_viewangles ); + result.flags |= MOVERESULT_MOVEMENTVIEW; + // + if ( dist < 5 && + fabs( AngleDiff( result.ideal_viewangles[0], ms->viewangles[0] ) ) < 2 && + fabs( AngleDiff( result.ideal_viewangles[1], ms->viewangles[1] ) ) < 2 ) { +#ifdef DEBUG_GRAPPLE + botimport.Print( PRT_MESSAGE, "BotTravel_Grapple: activating grapple\n" ); +#endif //DEBUG_GRAPPLE + EA_Command( ms->client, CMD_HOOKON ); + ms->moveflags |= MFL_ACTIVEGRAPPLE; + ms->lastgrappledist = 999999; + } //end if + else + { + if ( dist < 70 ) { + speed = 300 - ( 300 - 4 * dist ); + } else { speed = 400;} + // + BotCheckBlocked( ms, dir, qtrue, &result ); + //elemantary action move in direction + EA_Move( ms->client, dir, speed ); + VectorCopy( dir, result.movedir ); + } //end else + //if in another area before actually grappling + areanum = AAS_PointAreaNum( ms->origin ); + if ( areanum && areanum != ms->reachareanum ) { + ms->reachability_time = 0; + } + } //end else + return result; +} //end of the function BotTravel_Grapple +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotTravel_RocketJump( bot_movestate_t *ms, aas_reachability_t *reach ) { + vec3_t hordir; + float dist, speed; + bot_moveresult_t result; + + //botimport.Print(PRT_MESSAGE, "BotTravel_RocketJump: bah\n"); + BotClearMoveResult( &result ); + // + hordir[0] = reach->start[0] - ms->origin[0]; + hordir[1] = reach->start[1] - ms->origin[1]; + hordir[2] = 0; + // + dist = VectorNormalize( hordir ); + // + if ( dist < 5 ) { +// botimport.Print(PRT_MESSAGE, "between jump start and run start point\n"); + hordir[0] = reach->end[0] - ms->origin[0]; + hordir[1] = reach->end[1] - ms->origin[1]; + hordir[2] = 0; + VectorNormalize( hordir ); + //elemantary action jump + EA_Jump( ms->client ); + EA_Attack( ms->client ); + EA_Move( ms->client, hordir, 800 ); + // + ms->jumpreach = ms->lastreachnum; + } //end if + else + { + if ( dist > 80 ) { + dist = 80; + } + speed = 400 - ( 400 - 5 * dist ); + EA_Move( ms->client, hordir, speed ); + } //end else + // +/* + vec3_t hordir, dir1, dir2, start, end, runstart; + float dist1, dist2, speed; + bot_moveresult_t result; + + botimport.Print(PRT_MESSAGE, "BotTravel_RocketJump: bah\n"); + BotClearMoveResult(&result); + AAS_JumpReachRunStart(reach, runstart); + // + hordir[0] = runstart[0] - reach->start[0]; + hordir[1] = runstart[1] - reach->start[1]; + hordir[2] = 0; + VectorNormalize(hordir); + // + VectorCopy(reach->start, start); + start[2] += 1; + VectorMA(reach->start, 80, hordir, runstart); + //check for a gap + for (dist1 = 0; dist1 < 80; dist1 += 10) + { + VectorMA(start, dist1+10, hordir, end); + end[2] += 1; + if (AAS_PointAreaNum(end) != ms->reachareanum) break; + } //end for + if (dist1 < 80) VectorMA(reach->start, dist1, hordir, runstart); + // + VectorSubtract(ms->origin, reach->start, dir1); + dir1[2] = 0; + dist1 = VectorNormalize(dir1); + VectorSubtract(ms->origin, runstart, dir2); + dir2[2] = 0; + dist2 = VectorNormalize(dir2); + //if just before the reachability start + if (DotProduct(dir1, dir2) < -0.8 || dist2 < 5) + { +// botimport.Print(PRT_MESSAGE, "between jump start and run start point\n"); + hordir[0] = reach->end[0] - ms->origin[0]; + hordir[1] = reach->end[1] - ms->origin[1]; + hordir[2] = 0; + VectorNormalize(hordir); + //elemantary action jump + if (dist1 < 24) EA_Jump(ms->client); + else if (dist1 < 32) EA_DelayedJump(ms->client); + EA_Attack(ms->client); + EA_Move(ms->client, hordir, 800); + // + ms->jumpreach = ms->lastreachnum; + } //end if + else + { +// botimport.Print(PRT_MESSAGE, "going towards run start point\n"); + hordir[0] = runstart[0] - ms->origin[0]; + hordir[1] = runstart[1] - ms->origin[1]; + hordir[2] = 0; + VectorNormalize(hordir); + // + if (dist2 > 80) dist2 = 80; + speed = 400 - (400 - 5 * dist2); + EA_Move(ms->client, hordir, speed); + } //end else*/ + //look in the movement direction + Vector2Angles( hordir, result.ideal_viewangles ); + //look straight down + result.ideal_viewangles[PITCH] = 90; + //set the view angles directly + EA_View( ms->client, result.ideal_viewangles ); + //view is important for the movment + result.flags |= MOVERESULT_MOVEMENTVIEWSET; + //select the rocket launcher + EA_SelectWeapon( ms->client, WEAPONINDEX_ROCKET_LAUNCHER ); + //weapon is used for movement + result.weapon = WEAPONINDEX_ROCKET_LAUNCHER; + result.flags |= MOVERESULT_MOVEMENTWEAPON; + // + VectorCopy( hordir, result.movedir ); + // + return result; +} //end of the function BotTravel_RocketJump +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotTravel_BFGJump( bot_movestate_t *ms, aas_reachability_t *reach ) { + bot_moveresult_t result; + + BotClearMoveResult( &result ); + // + return result; +} //end of the function BotTravel_BFGJump +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotFinishTravel_WeaponJump( bot_movestate_t *ms, aas_reachability_t *reach ) { + vec3_t hordir; + bot_moveresult_t result; + + BotClearMoveResult( &result ); + //if not jumped yet + if ( !ms->jumpreach ) { + return result; + } + //go straight to the reachability end + hordir[0] = reach->end[0] - ms->origin[0]; + hordir[1] = reach->end[1] - ms->origin[1]; + hordir[2] = 0; + VectorNormalize( hordir ); + //always use max speed when traveling through the air + EA_Move( ms->client, hordir, 800 ); + VectorCopy( hordir, result.movedir ); + // + return result; +} //end of the function BotFinishTravel_WeaponJump +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotTravel_JumpPad( bot_movestate_t *ms, aas_reachability_t *reach ) { + float dist, speed; + vec3_t hordir; + bot_moveresult_t result; + + BotClearMoveResult( &result ); + //first walk straight to the reachability start + hordir[0] = reach->start[0] - ms->origin[0]; + hordir[1] = reach->start[1] - ms->origin[1]; + hordir[2] = 0; + dist = VectorNormalize( hordir ); + // + BotCheckBlocked( ms, hordir, qtrue, &result ); + speed = 400; + //elemantary action move in direction + EA_Move( ms->client, hordir, speed ); + VectorCopy( hordir, result.movedir ); + // + return result; +} //end of the function BotTravel_JumpPad +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotFinishTravel_JumpPad( bot_movestate_t *ms, aas_reachability_t *reach ) { + float speed; + vec3_t hordir; + bot_moveresult_t result; + + BotClearMoveResult( &result ); + if ( !BotAirControl( ms->origin, ms->velocity, reach->end, hordir, &speed ) ) { + hordir[0] = reach->end[0] - ms->origin[0]; + hordir[1] = reach->end[1] - ms->origin[1]; + hordir[2] = 0; + VectorNormalize( hordir ); + speed = 400; + } //end if + BotCheckBlocked( ms, hordir, qtrue, &result ); + //elemantary action move in direction + EA_Move( ms->client, hordir, speed ); + VectorCopy( hordir, result.movedir ); + // + return result; +} //end of the function BotFinishTravel_JumpPad +//=========================================================================== +// time before the reachability times out +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotReachabilityTime( aas_reachability_t *reach ) { + switch ( reach->traveltype ) + { + case TRAVEL_WALK: return 5; + case TRAVEL_CROUCH: return 5; + case TRAVEL_BARRIERJUMP: return 5; + case TRAVEL_LADDER: return 6; + case TRAVEL_WALKOFFLEDGE: return 5; + case TRAVEL_JUMP: return 5; + case TRAVEL_SWIM: return 5; + case TRAVEL_WATERJUMP: return 5; + case TRAVEL_TELEPORT: return 5; + case TRAVEL_ELEVATOR: return 10; + case TRAVEL_GRAPPLEHOOK: return 8; + case TRAVEL_ROCKETJUMP: return 6; + //case TRAVEL_BFGJUMP: return 6; + case TRAVEL_JUMPPAD: return 10; + case TRAVEL_FUNCBOB: return 10; + default: + { + botimport.Print( PRT_ERROR, "travel type %d not implemented yet\n", reach->traveltype ); + return 8; + } //end case + } //end switch +} //end of the function BotReachabilityTime +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bot_moveresult_t BotMoveInGoalArea( bot_movestate_t *ms, bot_goal_t *goal ) { + bot_moveresult_t result; + vec3_t dir; + float dist, speed; + +#ifdef DEBUG + //botimport.Print(PRT_MESSAGE, "%s: moving straight to goal\n", ClientName(ms->entitynum-1)); + //AAS_ClearShownDebugLines(); + //AAS_DebugLine(ms->origin, goal->origin, LINECOLOR_RED); +#endif //DEBUG + BotClearMoveResult( &result ); + //walk straight to the goal origin + dir[0] = goal->origin[0] - ms->origin[0]; + dir[1] = goal->origin[1] - ms->origin[1]; + if ( ms->moveflags & MFL_SWIMMING ) { + dir[2] = goal->origin[2] - ms->origin[2]; + result.traveltype = TRAVEL_SWIM; + } //end if + else + { + dir[2] = 0; + result.traveltype = TRAVEL_WALK; + } //endif + // + dist = VectorNormalize( dir ); + if ( dist > 100 || ( goal->flags & GFL_NOSLOWAPPROACH ) ) { + dist = 100; + } + speed = 400 - ( 400 - 4 * dist ); + if ( speed < 10 ) { + speed = 0; + } + // + BotCheckBlocked( ms, dir, qtrue, &result ); + //elemantary action move in direction + EA_Move( ms->client, dir, speed ); + VectorCopy( dir, result.movedir ); + // + if ( ms->moveflags & MFL_SWIMMING ) { + Vector2Angles( dir, result.ideal_viewangles ); + result.flags |= MOVERESULT_SWIMVIEW; + } //end if + //if (!debugline) debugline = botimport.DebugLineCreate(); + //botimport.DebugLineShow(debugline, ms->origin, goal->origin, LINECOLOR_BLUE); + // + ms->lastreachnum = 0; + ms->lastareanum = 0; + ms->lastgoalareanum = goal->areanum; + VectorCopy( ms->origin, ms->lastorigin ); + ms->lasttime = AAS_Time(); + // + return result; +} //end of the function BotMoveInGoalArea +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AreaRouteToGoalArea( int areanum, vec3_t origin, int goalareanum, int travelflags, int *traveltime, int *reachnum ); +extern float VectorDistance( vec3_t v1, vec3_t v2 ); +void BotMoveToGoal( bot_moveresult_t *result, int movestate, bot_goal_t *goal, int travelflags ) { + int reachnum = 0, lastreachnum, foundjumppad, ent; // TTimo: init + aas_reachability_t reach, lastreach; + bot_movestate_t *ms; + //vec3_t mins, maxs, up = {0, 0, 1}; + //bsp_trace_t trace; + //static int debugline; + + BotClearMoveResult( result ); + // + ms = BotMoveStateFromHandle( movestate ); + if ( !ms ) { + return; + } + //reset the grapple before testing if the bot has a valid goal + //because the bot could loose all it's goals when stuck to a wall + BotResetGrapple( ms ); + // + if ( !goal ) { +#ifdef DEBUG + botimport.Print( PRT_MESSAGE, "client %d: movetogoal -> no goal\n", ms->client ); +#endif //DEBUG + result->failure = qtrue; + return; + } //end if + //botimport.Print(PRT_MESSAGE, "numavoidreach = %d\n", ms->numavoidreach); + //remove some of the move flags + ms->moveflags &= ~( MFL_SWIMMING | MFL_AGAINSTLADDER ); + //set some of the move flags + //NOTE: the MFL_ONGROUND flag is also set in the higher AI + if ( AAS_OnGround( ms->origin, ms->presencetype, ms->entitynum ) ) { + ms->moveflags |= MFL_ONGROUND; + } + // + + if ( ms->moveflags & MFL_ONGROUND ) { + int modeltype, modelnum; + + ent = BotOnTopOfEntity( ms ); + + if ( ent != -1 ) { + modelnum = AAS_EntityModelindex( ent ); + if ( modelnum >= 0 && modelnum < MAX_MODELS ) { + modeltype = modeltypes[modelnum]; + + if ( modeltype == MODELTYPE_FUNC_PLAT ) { + AAS_ReachabilityFromNum( ms->lastreachnum, &reach ); + //if the bot is Not using the elevator + if ( reach.traveltype != TRAVEL_ELEVATOR || + //NOTE: the face number is the plat model number + ( reach.facenum & 0x0000FFFF ) != modelnum ) { + reachnum = AAS_NextModelReachability( 0, modelnum ); + if ( reachnum ) { + //botimport.Print(PRT_MESSAGE, "client %d: accidentally ended up on func_plat\n", ms->client); + AAS_ReachabilityFromNum( reachnum, &reach ); + ms->lastreachnum = reachnum; + ms->reachability_time = AAS_Time() + BotReachabilityTime( &reach ); + } //end if + else + { + if ( bot_developer ) { + botimport.Print( PRT_MESSAGE, "client %d: on func_plat without reachability\n", ms->client ); + } //end if + result->blocked = qtrue; + result->blockentity = ent; + result->flags |= MOVERESULT_ONTOPOFOBSTACLE; + return; + } //end else + } //end if + result->flags |= MOVERESULT_ONTOPOF_ELEVATOR; + } //end if + else if ( modeltype == MODELTYPE_FUNC_BOB ) { + AAS_ReachabilityFromNum( ms->lastreachnum, &reach ); + //if the bot is Not using the func bobbing + if ( reach.traveltype != TRAVEL_FUNCBOB || + //NOTE: the face number is the func_bobbing model number + ( reach.facenum & 0x0000FFFF ) != modelnum ) { + reachnum = AAS_NextModelReachability( 0, modelnum ); + if ( reachnum ) { + //botimport.Print(PRT_MESSAGE, "client %d: accidentally ended up on func_bobbing\n", ms->client); + AAS_ReachabilityFromNum( reachnum, &reach ); + ms->lastreachnum = reachnum; + ms->reachability_time = AAS_Time() + BotReachabilityTime( &reach ); + } //end if + else + { + if ( bot_developer ) { + botimport.Print( PRT_MESSAGE, "client %d: on func_bobbing without reachability\n", ms->client ); + } //end if + result->blocked = qtrue; + result->blockentity = ent; + result->flags |= MOVERESULT_ONTOPOFOBSTACLE; + return; + } //end else + } //end if + result->flags |= MOVERESULT_ONTOPOF_FUNCBOB; + } //end if + /* Ridah, disabled this, or standing on little fragments causes problems + else + { + result->blocked = qtrue; + result->blockentity = ent; + result->flags |= MOVERESULT_ONTOPOFOBSTACLE; + return; + } //end else + */ + } //end if + } //end if + } //end if + //if swimming + if ( AAS_Swimming( ms->origin ) ) { + ms->moveflags |= MFL_SWIMMING; + } + //if against a ladder + if ( AAS_AgainstLadder( ms->origin, ms->areanum ) ) { + ms->moveflags |= MFL_AGAINSTLADDER; + } + //if the bot is on the ground, swimming or against a ladder + if ( ms->moveflags & ( MFL_ONGROUND | MFL_SWIMMING | MFL_AGAINSTLADDER ) ) { + //botimport.Print(PRT_MESSAGE, "%s: onground, swimming or against ladder\n", ClientName(ms->entitynum-1)); + // + AAS_ReachabilityFromNum( ms->lastreachnum, &lastreach ); + //reachability area the bot is in + //ms->areanum = BotReachabilityArea(ms->origin, (lastreach.traveltype != TRAVEL_ELEVATOR)); + ms->areanum = BotFuzzyPointReachabilityArea( ms->origin ); + //if the bot is in the goal area + if ( ms->areanum == goal->areanum ) { + *result = BotMoveInGoalArea( ms, goal ); + return; + } //end if + //assume we can use the reachability from the last frame + reachnum = ms->lastreachnum; + //if there is a last reachability + +// Ridah, best calc it each frame, especially for Zombie, which moves so slow that we can't afford to take a long route +// reachnum = 0; +// done. + if ( reachnum ) { + AAS_ReachabilityFromNum( reachnum, &reach ); + //check if the reachability is still valid + if ( !( AAS_TravelFlagForType( reach.traveltype ) & travelflags ) ) { + reachnum = 0; + } //end if + //special grapple hook case + else if ( reach.traveltype == TRAVEL_GRAPPLEHOOK ) { + if ( ms->reachability_time < AAS_Time() || + ( ms->moveflags & MFL_GRAPPLERESET ) ) { + reachnum = 0; + } //end if + } //end if + //special elevator case + else if ( reach.traveltype == TRAVEL_ELEVATOR || reach.traveltype == TRAVEL_FUNCBOB ) { + if ( ( result->flags & MOVERESULT_ONTOPOF_FUNCBOB ) || + ( result->flags & MOVERESULT_ONTOPOF_FUNCBOB ) ) { + ms->reachability_time = AAS_Time() + 5; + } //end if + //if the bot was going for an elevator and reached the reachability area + if ( ms->areanum == reach.areanum || + ms->reachability_time < AAS_Time() ) { + reachnum = 0; + } //end if + } //end if + else + { +#ifdef DEBUG + if ( bot_developer ) { + if ( ms->reachability_time < AAS_Time() ) { + botimport.Print( PRT_MESSAGE, "client %d: reachability timeout in ", ms->client ); + AAS_PrintTravelType( reach.traveltype ); + botimport.Print( PRT_MESSAGE, "\n" ); + } //end if + /* + if (ms->lastareanum != ms->areanum) + { + botimport.Print(PRT_MESSAGE, "changed from area %d to %d\n", ms->lastareanum, ms->areanum); + } //end if*/ + } //end if +#endif //DEBUG + //if the goal area changed or the reachability timed out + //or the area changed + if ( ms->lastgoalareanum != goal->areanum || + ms->reachability_time < AAS_Time() || + ms->lastareanum != ms->areanum || + ( ( ms->lasttime > ( AAS_Time() - 0.5 ) ) && ( VectorDistance( ms->origin, ms->lastorigin ) < 20 * ( AAS_Time() - ms->lasttime ) ) ) ) { + reachnum = 0; + //botimport.Print(PRT_MESSAGE, "area change or timeout\n"); + } //end else if + } //end else + } //end if + //if the bot needs a new reachability + if ( !reachnum ) { + //if the area has no reachability links + if ( !AAS_AreaReachability( ms->areanum ) ) { +#ifdef DEBUG + if ( bot_developer ) { + botimport.Print( PRT_MESSAGE, "area %d no reachability\n", ms->areanum ); + } //end if +#endif //DEBUG + } //end if + //get a new reachability leading towards the goal + reachnum = BotGetReachabilityToGoal( ms->origin, ms->areanum, ms->entitynum, + ms->lastgoalareanum, ms->lastareanum, + ms->avoidreach, ms->avoidreachtimes, ms->avoidreachtries, + goal, travelflags, travelflags ); + //the area number the reachability starts in + ms->reachareanum = ms->areanum; + //reset some state variables + ms->jumpreach = 0; //for TRAVEL_JUMP + ms->moveflags &= ~MFL_GRAPPLERESET; //for TRAVEL_GRAPPLEHOOK + //if there is a reachability to the goal + if ( reachnum ) { + AAS_ReachabilityFromNum( reachnum, &reach ); + //set a timeout for this reachability + ms->reachability_time = AAS_Time() + BotReachabilityTime( &reach ); + // +#ifdef AVOIDREACH + //add the reachability to the reachabilities to avoid for a while + BotAddToAvoidReach( ms, reachnum, AVOIDREACH_TIME ); +#endif //AVOIDREACH + } //end if +#ifdef DEBUG + + else if ( bot_developer ) { + botimport.Print( PRT_MESSAGE, "goal not reachable\n" ); + memset( &reach, 0, sizeof( aas_reachability_t ) ); //make compiler happy + } //end else + if ( bot_developer ) { + //if still going for the same goal + if ( ms->lastgoalareanum == goal->areanum ) { + if ( ms->lastareanum == reach.areanum ) { + botimport.Print( PRT_MESSAGE, "same goal, going back to previous area\n" ); + } //end if + } //end if + } //end if +#endif //DEBUG + } //end else + // + ms->lastreachnum = reachnum; + ms->lastgoalareanum = goal->areanum; + ms->lastareanum = ms->areanum; + //if the bot has a reachability + if ( reachnum ) { + //get the reachability from the number + AAS_ReachabilityFromNum( reachnum, &reach ); + result->traveltype = reach.traveltype; + // +#ifdef DEBUG_AI_MOVE + AAS_ClearShownDebugLines(); + AAS_PrintTravelType( reach.traveltype ); + AAS_ShowReachability( &reach ); +#endif //DEBUG_AI_MOVE + // +#ifdef DEBUG + //botimport.Print(PRT_MESSAGE, "client %d: ", ms->client); + //AAS_PrintTravelType(reach.traveltype); + //botimport.Print(PRT_MESSAGE, "\n"); +#endif //DEBUG + switch ( reach.traveltype ) + { + case TRAVEL_WALK: *result = BotTravel_Walk( ms, &reach ); break; + case TRAVEL_CROUCH: *result = BotTravel_Crouch( ms, &reach ); break; + case TRAVEL_BARRIERJUMP: *result = BotTravel_BarrierJump( ms, &reach ); break; + case TRAVEL_LADDER: *result = BotTravel_Ladder( ms, &reach ); break; + case TRAVEL_WALKOFFLEDGE: *result = BotTravel_WalkOffLedge( ms, &reach ); break; + case TRAVEL_JUMP: *result = BotTravel_Jump( ms, &reach ); break; + case TRAVEL_SWIM: *result = BotTravel_Swim( ms, &reach ); break; + case TRAVEL_WATERJUMP: *result = BotTravel_WaterJump( ms, &reach ); break; + case TRAVEL_TELEPORT: *result = BotTravel_Teleport( ms, &reach ); break; + case TRAVEL_ELEVATOR: *result = BotTravel_Elevator( ms, &reach ); break; + case TRAVEL_GRAPPLEHOOK: *result = BotTravel_Grapple( ms, &reach ); break; + case TRAVEL_ROCKETJUMP: *result = BotTravel_RocketJump( ms, &reach ); break; + //case TRAVEL_BFGJUMP: + case TRAVEL_JUMPPAD: *result = BotTravel_JumpPad( ms, &reach ); break; + case TRAVEL_FUNCBOB: *result = BotTravel_FuncBobbing( ms, &reach ); break; + default: + { + botimport.Print( PRT_FATAL, "travel type %d not implemented yet\n", reach.traveltype ); + break; + } //end case + } //end switch + } //end if + else + { + result->failure = qtrue; + memset( &reach, 0, sizeof( aas_reachability_t ) ); + } //end else +#ifdef DEBUG + if ( bot_developer ) { + if ( result->failure ) { + botimport.Print( PRT_MESSAGE, "client %d: movement failure in ", ms->client ); + AAS_PrintTravelType( reach.traveltype ); + botimport.Print( PRT_MESSAGE, "\n" ); + } //end if + } //end if +#endif //DEBUG + } //end if + else + { + int i, numareas, areas[16]; + vec3_t end; + + //special handling of jump pads when the bot uses a jump pad without knowing it + foundjumppad = qfalse; + VectorMA( ms->origin, -2 * ms->thinktime, ms->velocity, end ); + numareas = AAS_TraceAreas( ms->origin, end, areas, NULL, 16 ); + for ( i = numareas - 1; i >= 0; i-- ) + { + if ( AAS_AreaJumpPad( areas[i] ) ) { + //botimport.Print(PRT_MESSAGE, "client %d used a jumppad without knowing, area %d\n", ms->client, areas[i]); + foundjumppad = qtrue; + lastreachnum = BotGetReachabilityToGoal( end, areas[i], ms->entitynum, + ms->lastgoalareanum, ms->lastareanum, + ms->avoidreach, ms->avoidreachtimes, ms->avoidreachtries, + goal, travelflags, TFL_JUMPPAD ); + if ( lastreachnum ) { + ms->lastreachnum = lastreachnum; + ms->lastareanum = areas[i]; + //botimport.Print(PRT_MESSAGE, "found jumppad reachability\n"); + break; + } //end if + else + { + for ( lastreachnum = AAS_NextAreaReachability( areas[i], 0 ); lastreachnum; + lastreachnum = AAS_NextAreaReachability( areas[i], lastreachnum ) ) + { + //get the reachability from the number + AAS_ReachabilityFromNum( lastreachnum, &reach ); + if ( reach.traveltype == TRAVEL_JUMPPAD ) { + ms->lastreachnum = lastreachnum; + ms->lastareanum = areas[i]; + //botimport.Print(PRT_MESSAGE, "found jumppad reachability hard!!\n"); + break; + } //end if + } //end for + if ( lastreachnum ) { + break; + } + } //end else + } //end if + } //end for + if ( bot_developer ) { + //if a jumppad is found with the trace but no reachability is found + if ( foundjumppad && !ms->lastreachnum ) { + botimport.Print( PRT_MESSAGE, "client %d didn't find jumppad reachability\n", ms->client ); + } //end if + } //end if + // + if ( ms->lastreachnum ) { + //botimport.Print(PRT_MESSAGE, "%s: NOT onground, swimming or against ladder\n", ClientName(ms->entitynum-1)); + AAS_ReachabilityFromNum( ms->lastreachnum, &reach ); + result->traveltype = reach.traveltype; +#ifdef DEBUG + //botimport.Print(PRT_MESSAGE, "client %d finish: ", ms->client); + //AAS_PrintTravelType(reach.traveltype); + //botimport.Print(PRT_MESSAGE, "\n"); +#endif //DEBUG + // + switch ( reach.traveltype ) + { + case TRAVEL_WALK: *result = BotTravel_Walk( ms, &reach ); break; //BotFinishTravel_Walk(ms, &reach); break; + case TRAVEL_CROUCH: /*do nothing*/ break; + case TRAVEL_BARRIERJUMP: *result = BotFinishTravel_BarrierJump( ms, &reach ); break; + case TRAVEL_LADDER: *result = BotTravel_Ladder( ms, &reach ); break; + case TRAVEL_WALKOFFLEDGE: *result = BotFinishTravel_WalkOffLedge( ms, &reach ); break; + case TRAVEL_JUMP: *result = BotFinishTravel_Jump( ms, &reach ); break; + case TRAVEL_SWIM: *result = BotTravel_Swim( ms, &reach ); break; + case TRAVEL_WATERJUMP: *result = BotFinishTravel_WaterJump( ms, &reach ); break; + case TRAVEL_TELEPORT: /*do nothing*/ break; + case TRAVEL_ELEVATOR: *result = BotFinishTravel_Elevator( ms, &reach ); break; + case TRAVEL_GRAPPLEHOOK: *result = BotTravel_Grapple( ms, &reach ); break; + case TRAVEL_ROCKETJUMP: *result = BotFinishTravel_WeaponJump( ms, &reach ); break; + //case TRAVEL_BFGJUMP: + case TRAVEL_JUMPPAD: *result = BotFinishTravel_JumpPad( ms, &reach ); break; + case TRAVEL_FUNCBOB: *result = BotFinishTravel_FuncBobbing( ms, &reach ); break; + default: + { + botimport.Print( PRT_FATAL, "(last) travel type %d not implemented yet\n", reach.traveltype ); + break; + } //end case + } //end switch +#ifdef DEBUG + if ( bot_developer ) { + if ( result->failure ) { + botimport.Print( PRT_MESSAGE, "client %d: movement failure in finish ", ms->client ); + AAS_PrintTravelType( reach.traveltype ); + botimport.Print( PRT_MESSAGE, "\n" ); + } //end if + } //end if +#endif //DEBUG + } //end if + } //end else + //FIXME: is it right to do this here? + if ( result->blocked ) { + ms->reachability_time -= 10 * ms->thinktime; + } + //copy the last origin + VectorCopy( ms->origin, ms->lastorigin ); + ms->lasttime = AAS_Time(); + + // RF, try to look in the direction we will be moving ahead of time + if ( reachnum > 0 && !( result->flags & ( MOVERESULT_MOVEMENTVIEW | MOVERESULT_SWIMVIEW ) ) ) { + vec3_t dir; + int ftraveltime, freachnum, straveltime, ltraveltime; + + AAS_ReachabilityFromNum( reachnum, &reach ); + if ( reach.areanum != goal->areanum ) { + if ( AAS_AreaRouteToGoalArea( reach.areanum, reach.end, goal->areanum, travelflags, &straveltime, &freachnum ) ) { + ltraveltime = 999999; + while ( AAS_AreaRouteToGoalArea( reach.areanum, reach.end, goal->areanum, travelflags, &ftraveltime, &freachnum ) ) { + // make sure we are not in a loop + if ( ftraveltime > ltraveltime ) { + break; + } + ltraveltime = ftraveltime; + // + AAS_ReachabilityFromNum( freachnum, &reach ); + if ( reach.areanum == goal->areanum ) { + VectorSubtract( goal->origin, ms->origin, dir ); + VectorNormalize( dir ); + vectoangles( dir, result->ideal_viewangles ); + result->flags |= MOVERESULT_FUTUREVIEW; + break; + } + if ( straveltime - ftraveltime > 120 ) { + VectorSubtract( reach.end, ms->origin, dir ); + VectorNormalize( dir ); + vectoangles( dir, result->ideal_viewangles ); + result->flags |= MOVERESULT_FUTUREVIEW; + break; + } + } + } + } else { + VectorSubtract( goal->origin, ms->origin, dir ); + VectorNormalize( dir ); + vectoangles( dir, result->ideal_viewangles ); + result->flags |= MOVERESULT_FUTUREVIEW; + } + } + + //return the movement result + return; +} //end of the function BotMoveToGoal +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotResetAvoidReach( int movestate ) { + bot_movestate_t *ms; + + ms = BotMoveStateFromHandle( movestate ); + if ( !ms ) { + return; + } + memset( ms->avoidreach, 0, MAX_AVOIDREACH * sizeof( int ) ); + memset( ms->avoidreachtimes, 0, MAX_AVOIDREACH * sizeof( float ) ); + memset( ms->avoidreachtries, 0, MAX_AVOIDREACH * sizeof( int ) ); + + // RF, also clear movestate stuff + ms->lastareanum = 0; + ms->lastgoalareanum = 0; + ms->lastreachnum = 0; +} //end of the function BotResetAvoidReach +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotResetLastAvoidReach( int movestate ) { + int i, latest; + float latesttime; + bot_movestate_t *ms; + + ms = BotMoveStateFromHandle( movestate ); + if ( !ms ) { + return; + } + latesttime = 0; + latest = 0; + for ( i = 0; i < MAX_AVOIDREACH; i++ ) + { + if ( ms->avoidreachtimes[i] > latesttime ) { + latesttime = ms->avoidreachtimes[i]; + latest = i; + } //end if + } //end for + if ( latesttime ) { + ms->avoidreachtimes[latest] = 0; + if ( ms->avoidreachtries[i] > 0 ) { + ms->avoidreachtries[latest]--; + } + } //end if +} //end of the function BotResetLastAvoidReach +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotResetMoveState( int movestate ) { + bot_movestate_t *ms; + + ms = BotMoveStateFromHandle( movestate ); + if ( !ms ) { + return; + } + memset( ms, 0, sizeof( bot_movestate_t ) ); +} //end of the function BotResetMoveState +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotSetupMoveAI( void ) { + BotSetBrushModelTypes(); + sv_maxstep = LibVarValue( "sv_step", "18" ); + sv_maxbarrier = LibVarValue( "sv_maxbarrier", "32" ); + sv_gravity = LibVarValue( "sv_gravity", "800" ); + return BLERR_NOERROR; +} //end of the function BotSetupMoveAI +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotShutdownMoveAI( void ) { + int i; + + for ( i = 1; i <= MAX_CLIENTS; i++ ) + { + if ( botmovestates[i] ) { + FreeMemory( botmovestates[i] ); + botmovestates[i] = NULL; + } //end if + } //end for +} //end of the function BotShutdownMoveAI diff --git a/src/botlib/be_ai_weap.c b/src/botlib/be_ai_weap.c new file mode 100644 index 0000000..72f84e6 --- /dev/null +++ b/src/botlib/be_ai_weap.c @@ -0,0 +1,547 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_ai_weap.c + * + * desc: weapon AI + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "l_libvar.h" +#include "l_log.h" +#include "l_memory.h" +#include "l_utils.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "aasfile.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "be_aas_funcs.h" +#include "be_interface.h" +#include "be_ai_weight.h" //fuzzy weights +#include "../game/be_ai_weap.h" + +//#define DEBUG_AI_WEAP + +//structure field offsets +#define WEAPON_OFS( x ) (int)&( ( (weaponinfo_t *)0 )->x ) +#define PROJECTILE_OFS( x ) (int)&( ( (projectileinfo_t *)0 )->x ) + +//weapon definition +fielddef_t weaponinfo_fields[] = +{ + {"number", WEAPON_OFS( number ), FT_INT}, //weapon number + {"name", WEAPON_OFS( name ), FT_STRING}, //name of the weapon + {"level", WEAPON_OFS( level ), FT_INT}, + {"model", WEAPON_OFS( model ), FT_STRING}, //model of the weapon + {"weaponindex", WEAPON_OFS( weaponindex ), FT_INT}, //index of weapon in inventory + {"flags", WEAPON_OFS( flags ), FT_INT}, //special flags + {"projectile", WEAPON_OFS( projectile ), FT_STRING}, //projectile used by the weapon + {"numprojectiles", WEAPON_OFS( numprojectiles ), FT_INT}, //number of projectiles + {"hspread", WEAPON_OFS( hspread ), FT_FLOAT}, //horizontal spread of projectiles (degrees from middle) + {"vspread", WEAPON_OFS( vspread ), FT_FLOAT}, //vertical spread of projectiles (degrees from middle) + {"speed", WEAPON_OFS( speed ), FT_FLOAT}, //speed of the projectile (0 = instant hit) + {"acceleration", WEAPON_OFS( acceleration ), FT_FLOAT}, //"acceleration" * time (in seconds) + "speed" = projectile speed + {"recoil", WEAPON_OFS( recoil ), FT_FLOAT | FT_ARRAY, 3}, //amount of recoil the player gets from the weapon + {"offset", WEAPON_OFS( offset ), FT_FLOAT | FT_ARRAY, 3}, //projectile start offset relative to eye and view angles + {"angleoffset", WEAPON_OFS( angleoffset ), FT_FLOAT | FT_ARRAY, 3}, //offset of the shoot angles relative to the view angles + {"extrazvelocity", WEAPON_OFS( extrazvelocity ), FT_FLOAT}, //extra z velocity the projectile gets + {"ammoamount", WEAPON_OFS( ammoamount ), FT_INT}, //ammo amount used per shot + {"ammoindex", WEAPON_OFS( ammoindex ), FT_INT}, //index of ammo in inventory + {"activate", WEAPON_OFS( activate ), FT_FLOAT}, //time it takes to select the weapon + {"reload", WEAPON_OFS( reload ), FT_FLOAT}, //time it takes to reload the weapon + {"spinup", WEAPON_OFS( spinup ), FT_FLOAT}, //time it takes before first shot + {"spindown", WEAPON_OFS( spindown ), FT_FLOAT}, //time it takes before weapon stops firing + {NULL, 0, 0, 0} +}; + +//projectile definition +fielddef_t projectileinfo_fields[] = +{ + {"name", PROJECTILE_OFS( name ), FT_STRING}, //name of the projectile + {"model", WEAPON_OFS( model ), FT_STRING}, //model of the projectile + {"flags", PROJECTILE_OFS( flags ), FT_INT}, //special flags + {"gravity", PROJECTILE_OFS( gravity ), FT_FLOAT}, //amount of gravity applied to the projectile [0,1] + {"damage", PROJECTILE_OFS( damage ), FT_INT}, //damage of the projectile + {"radius", PROJECTILE_OFS( radius ), FT_FLOAT}, //radius of damage + {"visdamage", PROJECTILE_OFS( visdamage ), FT_INT}, //damage of the projectile to visible entities + {"damagetype", PROJECTILE_OFS( damagetype ), FT_INT}, //type of damage (combination of the DAMAGETYPE_? flags) + {"healthinc", PROJECTILE_OFS( healthinc ), FT_INT}, //health increase the owner gets + {"push", PROJECTILE_OFS( push ), FT_FLOAT}, //amount a player is pushed away from the projectile impact + {"detonation", PROJECTILE_OFS( detonation ), FT_FLOAT}, //time before projectile explodes after fire pressed + {"bounce", PROJECTILE_OFS( bounce ), FT_FLOAT}, //amount the projectile bounces + {"bouncefric", PROJECTILE_OFS( bouncefric ), FT_FLOAT}, //amount the bounce decreases per bounce + {"bouncestop", PROJECTILE_OFS( bouncestop ), FT_FLOAT}, //minimum bounce value before bouncing stops +//recurive projectile definition?? + {NULL, 0, 0, 0} +}; + +structdef_t weaponinfo_struct = +{ + sizeof( weaponinfo_t ), weaponinfo_fields +}; +structdef_t projectileinfo_struct = +{ + sizeof( projectileinfo_t ), projectileinfo_fields +}; + +//weapon configuration: set of weapons with projectiles +typedef struct weaponconfig_s +{ + int numweapons; + int numprojectiles; + projectileinfo_t *projectileinfo; + weaponinfo_t *weaponinfo; +} weaponconfig_t; + +//the bot weapon state +typedef struct bot_weaponstate_s +{ + struct weightconfig_s *weaponweightconfig; //weapon weight configuration + int *weaponweightindex; //weapon weight index +} bot_weaponstate_t; + +bot_weaponstate_t *botweaponstates[MAX_CLIENTS + 1]; +weaponconfig_t *weaponconfig; + +//======================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//======================================================================== +int BotValidWeaponNumber( int weaponnum ) { + if ( weaponnum <= 0 || weaponnum > weaponconfig->numweapons ) { + botimport.Print( PRT_ERROR, "weapon number out of range\n" ); + return qfalse; + } //end if + return qtrue; +} //end of the function BotValidWeaponNumber +//======================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//======================================================================== +bot_weaponstate_t *BotWeaponStateFromHandle( int handle ) { + if ( handle <= 0 || handle > MAX_CLIENTS ) { + botimport.Print( PRT_FATAL, "move state handle %d out of range\n", handle ); + return NULL; + } //end if + if ( !botweaponstates[handle] ) { + botimport.Print( PRT_FATAL, "invalid move state %d\n", handle ); + return NULL; + } //end if + return botweaponstates[handle]; +} //end of the function BotWeaponStateFromHandle +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef DEBUG_AI_WEAP +void DumpWeaponConfig( weaponconfig_t *wc ) { + FILE *fp; + int i; + + fp = Log_FileStruct(); + if ( !fp ) { + return; + } + for ( i = 0; i < wc->numprojectiles; i++ ) + { + WriteStructure( fp, &projectileinfo_struct, (char *) &wc->projectileinfo[i] ); + Log_Flush(); + } //end for + for ( i = 0; i < wc->numweapons; i++ ) + { + WriteStructure( fp, &weaponinfo_struct, (char *) &wc->weaponinfo[i] ); + Log_Flush(); + } //end for +} //end of the function DumpWeaponConfig +#endif //DEBUG_AI_WEAP +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +weaponconfig_t *LoadWeaponConfig( char *filename ) { + int max_weaponinfo, max_projectileinfo; + token_t token; + char path[MAX_PATH]; + int i, j; + source_t *source; + weaponconfig_t *wc; + weaponinfo_t weaponinfo; + + max_weaponinfo = (int) LibVarValue( "max_weaponinfo", "32" ); + if ( max_weaponinfo < 0 ) { + botimport.Print( PRT_ERROR, "max_weaponinfo = %d\n", max_weaponinfo ); + max_weaponinfo = 32; + LibVarSet( "max_weaponinfo", "32" ); + } //end if + max_projectileinfo = (int) LibVarValue( "max_projectileinfo", "32" ); + if ( max_projectileinfo < 0 ) { + botimport.Print( PRT_ERROR, "max_projectileinfo = %d\n", max_projectileinfo ); + max_projectileinfo = 32; + LibVarSet( "max_projectileinfo", "32" ); + } //end if + strncpy( path, filename, MAX_PATH ); + source = LoadSourceFile( path ); + if ( !source ) { + botimport.Print( PRT_ERROR, "counldn't load %s\n", path ); + return NULL; + } //end if + //initialize weapon config + wc = (weaponconfig_t *) GetClearedHunkMemory( sizeof( weaponconfig_t ) + + max_weaponinfo * sizeof( weaponinfo_t ) + + max_projectileinfo * sizeof( projectileinfo_t ) ); + wc->weaponinfo = ( weaponinfo_t * )( (char *) wc + sizeof( weaponconfig_t ) ); + wc->projectileinfo = ( projectileinfo_t * )( (char *) wc->weaponinfo + + max_weaponinfo * sizeof( weaponinfo_t ) ); + wc->numweapons = max_weaponinfo; + wc->numprojectiles = 0; + //parse the source file + while ( PC_ReadToken( source, &token ) ) + { + if ( !strcmp( token.string, "weaponinfo" ) ) { + memset( &weaponinfo, 0, sizeof( weaponinfo_t ) ); + if ( !ReadStructure( source, &weaponinfo_struct, (char *) &weaponinfo ) ) { + FreeMemory( wc ); + FreeSource( source ); + return NULL; + } //end if + if ( weaponinfo.number < 0 || weaponinfo.number >= max_weaponinfo ) { + botimport.Print( PRT_ERROR, "weapon info number %d out of range in %s\n", weaponinfo.number, path ); + FreeMemory( wc ); + FreeSource( source ); + return NULL; + } //end if + memcpy( &wc->weaponinfo[weaponinfo.number], &weaponinfo, sizeof( weaponinfo_t ) ); + wc->weaponinfo[weaponinfo.number].valid = qtrue; + } //end if + else if ( !strcmp( token.string, "projectileinfo" ) ) { + if ( wc->numprojectiles >= max_projectileinfo ) { + botimport.Print( PRT_ERROR, "more than %d projectiles defined in %s\n", max_projectileinfo, path ); + FreeMemory( wc ); + FreeSource( source ); + return NULL; + } //end if + memset( &wc->projectileinfo[wc->numprojectiles], 0, sizeof( projectileinfo_t ) ); + if ( !ReadStructure( source, &projectileinfo_struct, (char *) &wc->projectileinfo[wc->numprojectiles] ) ) { + FreeMemory( wc ); + FreeSource( source ); + return NULL; + } //end if + wc->numprojectiles++; + } //end if + else + { + botimport.Print( PRT_ERROR, "unknown definition %s in %s\n", token.string, path ); + FreeMemory( wc ); + FreeSource( source ); + return NULL; + } //end else + } //end while + FreeSource( source ); + //fix up weapons + for ( i = 0; i < wc->numweapons; i++ ) + { + if ( !wc->weaponinfo[i].valid ) { + continue; + } + if ( !wc->weaponinfo[i].name[0] ) { + botimport.Print( PRT_ERROR, "weapon %d has no name in %s\n", i, path ); + FreeMemory( wc ); + return NULL; + } //end if + if ( !wc->weaponinfo[i].projectile[0] ) { + botimport.Print( PRT_ERROR, "weapon %s has no projectile in %s\n", wc->weaponinfo[i].name, path ); + FreeMemory( wc ); + return NULL; + } //end if + //find the projectile info and copy it to the weapon info + for ( j = 0; j < wc->numprojectiles; j++ ) + { + if ( !strcmp( wc->projectileinfo[j].name, wc->weaponinfo[i].projectile ) ) { + memcpy( &wc->weaponinfo[i].proj, &wc->projectileinfo[j], sizeof( projectileinfo_t ) ); + break; + } //end if + } //end for + if ( j == wc->numprojectiles ) { + botimport.Print( PRT_ERROR, "weapon %s uses undefined projectile in %s\n", wc->weaponinfo[i].name, path ); + FreeMemory( wc ); + return NULL; + } //end if + } //end for + if ( !wc->numweapons ) { + botimport.Print( PRT_WARNING, "no weapon info loaded\n" ); + } + botimport.Print( PRT_MESSAGE, "loaded %s\n", path ); + return wc; +} //end of the function LoadWeaponConfig +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int *WeaponWeightIndex( weightconfig_t *wwc, weaponconfig_t *wc ) { + int *index, i; + + //initialize item weight index + index = (int *) GetClearedMemory( sizeof( int ) * wc->numweapons ); + + for ( i = 0; i < wc->numweapons; i++ ) + { + index[i] = FindFuzzyWeight( wwc, wc->weaponinfo[i].name ); + } //end for + return index; +} //end of the function WeaponWeightIndex +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotFreeWeaponWeights( int weaponstate ) { + bot_weaponstate_t *ws; + + ws = BotWeaponStateFromHandle( weaponstate ); + if ( !ws ) { + return; + } + if ( ws->weaponweightconfig ) { + FreeWeightConfig( ws->weaponweightconfig ); + } + if ( ws->weaponweightindex ) { + FreeMemory( ws->weaponweightindex ); + } +} //end of the function BotFreeWeaponWeights +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotLoadWeaponWeights( int weaponstate, char *filename ) { + bot_weaponstate_t *ws; + + ws = BotWeaponStateFromHandle( weaponstate ); + if ( !ws ) { + return BLERR_CANNOTLOADWEAPONWEIGHTS; + } + BotFreeWeaponWeights( weaponstate ); + // + ws->weaponweightconfig = ReadWeightConfig( filename ); + if ( !ws->weaponweightconfig ) { + botimport.Print( PRT_FATAL, "couldn't load weapon config %s\n", filename ); + return BLERR_CANNOTLOADWEAPONWEIGHTS; + } //end if + if ( !weaponconfig ) { + return BLERR_CANNOTLOADWEAPONCONFIG; + } + ws->weaponweightindex = WeaponWeightIndex( ws->weaponweightconfig, weaponconfig ); + return BLERR_NOERROR; +} //end of the function BotLoadWeaponWeights +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotGetWeaponInfo( int weaponstate, int weapon, weaponinfo_t *weaponinfo ) { + bot_weaponstate_t *ws; + + if ( !BotValidWeaponNumber( weapon ) ) { + return; + } + ws = BotWeaponStateFromHandle( weaponstate ); + if ( !ws ) { + return; + } + if ( !weaponconfig ) { + return; + } + memcpy( weaponinfo, &weaponconfig->weaponinfo[weapon], sizeof( weaponinfo_t ) ); +} //end of the function BotGetWeaponInfo +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotChooseBestFightWeapon( int weaponstate, int *inventory ) { + int i, index, bestweapon; + float weight, bestweight; + weaponconfig_t *wc; + bot_weaponstate_t *ws; + + ws = BotWeaponStateFromHandle( weaponstate ); + if ( !ws ) { + return 0; + } + wc = weaponconfig; + if ( !weaponconfig ) { + return 0; + } + + //if the bot has no weapon weight configuration + if ( !ws->weaponweightconfig ) { + return 0; + } + + bestweight = 0; + bestweapon = 0; + for ( i = 0; i < wc->numweapons; i++ ) + { + if ( !wc->weaponinfo[i].valid ) { + continue; + } + index = ws->weaponweightindex[i]; + if ( index < 0 ) { + continue; + } + weight = FuzzyWeight( inventory, ws->weaponweightconfig, index ); + if ( weight > bestweight ) { + bestweight = weight; + bestweapon = i; + } //end if + } //end for + return bestweapon; +} //end of the function BotChooseBestFightWeapon +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotResetWeaponState( int weaponstate ) { + struct weightconfig_s *weaponweightconfig; + int *weaponweightindex; + bot_weaponstate_t *ws; + + ws = BotWeaponStateFromHandle( weaponstate ); + if ( !ws ) { + return; + } + weaponweightconfig = ws->weaponweightconfig; + weaponweightindex = ws->weaponweightindex; + + //memset(ws, 0, sizeof(bot_weaponstate_t)); + ws->weaponweightconfig = weaponweightconfig; + ws->weaponweightindex = weaponweightindex; +} //end of the function BotResetWeaponState +//======================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//======================================================================== +int BotAllocWeaponState( void ) { + int i; + + for ( i = 1; i <= MAX_CLIENTS; i++ ) + { + if ( !botweaponstates[i] ) { + botweaponstates[i] = GetClearedMemory( sizeof( bot_weaponstate_t ) ); + return i; + } //end if + } //end for + return 0; +} //end of the function BotAllocWeaponState +//======================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//======================================================================== +void BotFreeWeaponState( int handle ) { + if ( handle <= 0 || handle > MAX_CLIENTS ) { + botimport.Print( PRT_FATAL, "move state handle %d out of range\n", handle ); + return; + } //end if + if ( !botweaponstates[handle] ) { + botimport.Print( PRT_FATAL, "invalid move state %d\n", handle ); + return; + } //end if + BotFreeWeaponWeights( handle ); + FreeMemory( botweaponstates[handle] ); + botweaponstates[handle] = NULL; +} //end of the function BotFreeWeaponState +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotSetupWeaponAI( void ) { + char *file; + + file = LibVarString( "weaponconfig", "weapons.c" ); + weaponconfig = LoadWeaponConfig( file ); + if ( !weaponconfig ) { + botimport.Print( PRT_FATAL, "couldn't load the weapon config\n" ); + return BLERR_CANNOTLOADWEAPONCONFIG; + } //end if + +#ifdef DEBUG_AI_WEAP + DumpWeaponConfig( weaponconfig ); +#endif //DEBUG_AI_WEAP + // + return BLERR_NOERROR; +} //end of the function BotSetupWeaponAI +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotShutdownWeaponAI( void ) { + int i; + + if ( weaponconfig ) { + FreeMemory( weaponconfig ); + } + weaponconfig = NULL; + + for ( i = 1; i <= MAX_CLIENTS; i++ ) + { + if ( botweaponstates[i] ) { + BotFreeWeaponState( i ); + } //end if + } //end for +} //end of the function BotShutdownWeaponAI + diff --git a/src/botlib/be_ai_weight.c b/src/botlib/be_ai_weight.c new file mode 100644 index 0000000..f99c29e --- /dev/null +++ b/src/botlib/be_ai_weight.c @@ -0,0 +1,972 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_ai_weight.c + * + * desc: fuzzy logic + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "l_memory.h" +#include "l_log.h" +#include "l_utils.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "l_libvar.h" +#include "aasfile.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "be_aas_funcs.h" +#include "be_interface.h" +#include "be_ai_weight.h" + +#define MAX_INVENTORYVALUE 999999 +#define EVALUATERECURSIVELY + +#define MAX_WEIGHT_FILES 128 +weightconfig_t *weightFileList[MAX_WEIGHT_FILES]; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int ReadValue( source_t *source, float *value ) { + token_t token; + + if ( !PC_ExpectAnyToken( source, &token ) ) { + return qfalse; + } + if ( !strcmp( token.string, "-" ) ) { + SourceWarning( source, "negative value set to zero\n" ); + if ( !PC_ExpectTokenType( source, TT_NUMBER, 0, &token ) ) { + return qfalse; + } + } //end if + if ( token.type != TT_NUMBER ) { + SourceError( source, "invalid return value %s\n", token.string ); + return qfalse; + } //end if + *value = token.floatvalue; + return qtrue; +} //end of the function ReadValue +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int ReadFuzzyWeight( source_t *source, fuzzyseperator_t *fs ) { + if ( PC_CheckTokenString( source, "balance" ) ) { + fs->type = WT_BALANCE; + if ( !PC_ExpectTokenString( source, "(" ) ) { + return qfalse; + } + if ( !ReadValue( source, &fs->weight ) ) { + return qfalse; + } + if ( !PC_ExpectTokenString( source, "," ) ) { + return qfalse; + } + if ( !ReadValue( source, &fs->minweight ) ) { + return qfalse; + } + if ( !PC_ExpectTokenString( source, "," ) ) { + return qfalse; + } + if ( !ReadValue( source, &fs->maxweight ) ) { + return qfalse; + } + if ( !PC_ExpectTokenString( source, ")" ) ) { + return qfalse; + } + } //end if + else + { + fs->type = 0; + if ( !ReadValue( source, &fs->weight ) ) { + return qfalse; + } + fs->minweight = fs->weight; + fs->maxweight = fs->weight; + } //end if + if ( !PC_ExpectTokenString( source, ";" ) ) { + return qfalse; + } + return qtrue; +} //end of the function ReadFuzzyWeight +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FreeFuzzySeperators_r( fuzzyseperator_t *fs ) { + if ( !fs ) { + return; + } + if ( fs->child ) { + FreeFuzzySeperators_r( fs->child ); + } + if ( fs->next ) { + FreeFuzzySeperators_r( fs->next ); + } + FreeMemory( fs ); +} //end of the function FreeFuzzySeperators +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FreeWeightConfig2( weightconfig_t *config ) { + int i; + + for ( i = 0; i < config->numweights; i++ ) + { + FreeFuzzySeperators_r( config->weights[i].firstseperator ); + if ( config->weights[i].name ) { + FreeMemory( config->weights[i].name ); + } + } //end for + FreeMemory( config ); +} //end of the function FreeWeightConfig2 +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FreeWeightConfig( weightconfig_t *config ) { + if ( !LibVarGetValue( "bot_reloadcharacters" ) ) { + return; + } + FreeWeightConfig2( config ); +} //end of the function FreeWeightConfig +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +fuzzyseperator_t *ReadFuzzySeperators_r( source_t *source ) { + int newindent, index, def, founddefault; + token_t token; + fuzzyseperator_t *fs, *lastfs, *firstfs; + + founddefault = qfalse; + firstfs = NULL; + lastfs = NULL; + if ( !PC_ExpectTokenString( source, "(" ) ) { + return NULL; + } + if ( !PC_ExpectTokenType( source, TT_NUMBER, TT_INTEGER, &token ) ) { + return NULL; + } + index = token.intvalue; + if ( !PC_ExpectTokenString( source, ")" ) ) { + return NULL; + } + if ( !PC_ExpectTokenString( source, "{" ) ) { + return NULL; + } + if ( !PC_ExpectAnyToken( source, &token ) ) { + return NULL; + } + do + { + def = !strcmp( token.string, "default" ); + if ( def || !strcmp( token.string, "case" ) ) { + fs = (fuzzyseperator_t *) GetClearedMemory( sizeof( fuzzyseperator_t ) ); + fs->index = index; + if ( lastfs ) { + lastfs->next = fs; + } else { firstfs = fs;} + lastfs = fs; + if ( def ) { + if ( founddefault ) { + SourceError( source, "switch already has a default\n" ); + FreeFuzzySeperators_r( firstfs ); + return NULL; + } //end if + fs->value = MAX_INVENTORYVALUE; + founddefault = qtrue; + } //end if + else + { + if ( !PC_ExpectTokenType( source, TT_NUMBER, TT_INTEGER, &token ) ) { + FreeFuzzySeperators_r( firstfs ); + return NULL; + } //end if + fs->value = token.intvalue; + } //end else + if ( !PC_ExpectTokenString( source, ":" ) || !PC_ExpectAnyToken( source, &token ) ) { + FreeFuzzySeperators_r( firstfs ); + return NULL; + } //end if + newindent = qfalse; + if ( !strcmp( token.string, "{" ) ) { + newindent = qtrue; + if ( !PC_ExpectAnyToken( source, &token ) ) { + FreeFuzzySeperators_r( firstfs ); + return NULL; + } //end if + } //end if + if ( !strcmp( token.string, "return" ) ) { + if ( !ReadFuzzyWeight( source, fs ) ) { + FreeFuzzySeperators_r( firstfs ); + return NULL; + } //end if + } //end if + else if ( !strcmp( token.string, "switch" ) ) { + fs->child = ReadFuzzySeperators_r( source ); + if ( !fs->child ) { + FreeFuzzySeperators_r( firstfs ); + return NULL; + } //end if + } //end else if + else + { + SourceError( source, "invalid name %s\n", token.string ); + return NULL; + } //end else + if ( newindent ) { + if ( !PC_ExpectTokenString( source, "}" ) ) { + FreeFuzzySeperators_r( firstfs ); + return NULL; + } //end if + } //end if + } //end if + else + { + FreeFuzzySeperators_r( firstfs ); + SourceError( source, "invalid name %s\n", token.string ); + return NULL; + } //end else + if ( !PC_ExpectAnyToken( source, &token ) ) { + FreeFuzzySeperators_r( firstfs ); + return NULL; + } //end if + } while ( strcmp( token.string, "}" ) ); + // + if ( !founddefault ) { + SourceWarning( source, "switch without default\n" ); + fs = (fuzzyseperator_t *) GetClearedMemory( sizeof( fuzzyseperator_t ) ); + fs->index = index; + fs->value = MAX_INVENTORYVALUE; + fs->weight = 0; + fs->next = NULL; + fs->child = NULL; + if ( lastfs ) { + lastfs->next = fs; + } else { firstfs = fs;} + lastfs = fs; + } //end if + // + return firstfs; +} //end of the function ReadFuzzySeperators_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +weightconfig_t *ReadWeightConfig( char *filename ) { + int newindent, avail = 0, n; + token_t token; + source_t *source; + fuzzyseperator_t *fs; + weightconfig_t *config = NULL; +#ifdef DEBUG + int starttime; + + starttime = Sys_MilliSeconds(); +#endif //DEBUG + + if ( !LibVarGetValue( "bot_reloadcharacters" ) ) { + avail = -1; + for ( n = 0; n < MAX_WEIGHT_FILES; n++ ) + { + config = weightFileList[n]; + if ( !config ) { + if ( avail == -1 ) { + avail = n; + } //end if + continue; + } //end if + if ( strcmp( filename, config->filename ) == 0 ) { + //botimport.Print( PRT_MESSAGE, "retained %s\n", filename ); + return config; + } //end if + } //end for + + if ( avail == -1 ) { + botimport.Print( PRT_ERROR, "weightFileList was full trying to load %s\n", filename ); + return NULL; + } //end if + } //end if + + source = LoadSourceFile( filename ); + if ( !source ) { + botimport.Print( PRT_ERROR, "counldn't load %s\n", filename ); + return NULL; + } //end if + // + config = (weightconfig_t *) GetClearedMemory( sizeof( weightconfig_t ) ); + config->numweights = 0; + Q_strncpyz( config->filename, filename, sizeof( config->filename ) ); + //parse the item config file + while ( PC_ReadToken( source, &token ) ) + { + if ( !strcmp( token.string, "weight" ) ) { + if ( config->numweights >= MAX_WEIGHTS ) { + SourceWarning( source, "too many fuzzy weights\n" ); + break; + } //end if + if ( !PC_ExpectTokenType( source, TT_STRING, 0, &token ) ) { + FreeWeightConfig( config ); + FreeSource( source ); + return NULL; + } //end if + StripDoubleQuotes( token.string ); + config->weights[config->numweights].name = (char *) GetClearedMemory( strlen( token.string ) + 1 ); + strcpy( config->weights[config->numweights].name, token.string ); + if ( !PC_ExpectAnyToken( source, &token ) ) { + FreeWeightConfig( config ); + FreeSource( source ); + return NULL; + } //end if + newindent = qfalse; + if ( !strcmp( token.string, "{" ) ) { + newindent = qtrue; + if ( !PC_ExpectAnyToken( source, &token ) ) { + FreeWeightConfig( config ); + FreeSource( source ); + return NULL; + } //end if + } //end if + if ( !strcmp( token.string, "switch" ) ) { + fs = ReadFuzzySeperators_r( source ); + if ( !fs ) { + FreeWeightConfig( config ); + FreeSource( source ); + return NULL; + } //end if + config->weights[config->numweights].firstseperator = fs; + } //end if + else if ( !strcmp( token.string, "return" ) ) { + fs = (fuzzyseperator_t *) GetClearedMemory( sizeof( fuzzyseperator_t ) ); + fs->index = 0; + fs->value = MAX_INVENTORYVALUE; + fs->next = NULL; + fs->child = NULL; + if ( !ReadFuzzyWeight( source, fs ) ) { + FreeMemory( fs ); + FreeWeightConfig( config ); + FreeSource( source ); + return NULL; + } //end if + config->weights[config->numweights].firstseperator = fs; + } //end else if + else + { + SourceError( source, "invalid name %s\n", token.string ); + FreeWeightConfig( config ); + FreeSource( source ); + return NULL; + } //end else + if ( newindent ) { + if ( !PC_ExpectTokenString( source, "}" ) ) { + FreeWeightConfig( config ); + FreeSource( source ); + return NULL; + } //end if + } //end if + config->numweights++; + } //end if + else + { + SourceError( source, "invalid name %s\n", token.string ); + FreeWeightConfig( config ); + FreeSource( source ); + return NULL; + } //end else + } //end while + //free the source at the end of a pass + FreeSource( source ); + //if the file was located in a pak file + botimport.Print( PRT_MESSAGE, "loaded %s\n", filename ); +#ifdef DEBUG + if ( bot_developer ) { + botimport.Print( PRT_MESSAGE, "weights loaded in %d msec\n", Sys_MilliSeconds() - starttime ); + } //end if +#endif //DEBUG + // + if ( !LibVarGetValue( "bot_reloadcharacters" ) ) { + weightFileList[avail] = config; + } //end if + // + return config; +} //end of the function ReadWeightConfig +#if 0 +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean WriteFuzzyWeight( FILE *fp, fuzzyseperator_t *fs ) { + if ( fs->type == WT_BALANCE ) { + if ( fprintf( fp, " return balance(" ) < 0 ) { + return qfalse; + } + if ( !WriteFloat( fp, fs->weight ) ) { + return qfalse; + } + if ( fprintf( fp, "," ) < 0 ) { + return qfalse; + } + if ( !WriteFloat( fp, fs->minweight ) ) { + return qfalse; + } + if ( fprintf( fp, "," ) < 0 ) { + return qfalse; + } + if ( !WriteFloat( fp, fs->maxweight ) ) { + return qfalse; + } + if ( fprintf( fp, ");\n" ) < 0 ) { + return qfalse; + } + } //end if + else + { + if ( fprintf( fp, " return " ) < 0 ) { + return qfalse; + } + if ( !WriteFloat( fp, fs->weight ) ) { + return qfalse; + } + if ( fprintf( fp, ";\n" ) < 0 ) { + return qfalse; + } + } //end else + return qtrue; +} //end of the function WriteFuzzyWeight +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean WriteFuzzySeperators_r( FILE *fp, fuzzyseperator_t *fs, int indent ) { + if ( !WriteIndent( fp, indent ) ) { + return qfalse; + } + if ( fprintf( fp, "switch(%d)\n", fs->index ) < 0 ) { + return qfalse; + } + if ( !WriteIndent( fp, indent ) ) { + return qfalse; + } + if ( fprintf( fp, "{\n" ) < 0 ) { + return qfalse; + } + indent++; + do + { + if ( !WriteIndent( fp, indent ) ) { + return qfalse; + } + if ( fs->next ) { + if ( fprintf( fp, "case %d:", fs->value ) < 0 ) { + return qfalse; + } + } //end if + else + { + if ( fprintf( fp, "default:" ) < 0 ) { + return qfalse; + } + } //end else + if ( fs->child ) { + if ( fprintf( fp, "\n" ) < 0 ) { + return qfalse; + } + if ( !WriteIndent( fp, indent ) ) { + return qfalse; + } + if ( fprintf( fp, "{\n" ) < 0 ) { + return qfalse; + } + if ( !WriteFuzzySeperators_r( fp, fs->child, indent + 1 ) ) { + return qfalse; + } + if ( !WriteIndent( fp, indent ) ) { + return qfalse; + } + if ( fs->next ) { + if ( fprintf( fp, "} //end case\n" ) < 0 ) { + return qfalse; + } + } //end if + else + { + if ( fprintf( fp, "} //end default\n" ) < 0 ) { + return qfalse; + } + } //end else + } //end if + else + { + if ( !WriteFuzzyWeight( fp, fs ) ) { + return qfalse; + } + } //end else + fs = fs->next; + } while ( fs ); + indent--; + if ( !WriteIndent( fp, indent ) ) { + return qfalse; + } + if ( fprintf( fp, "} //end switch\n" ) < 0 ) { + return qfalse; + } + return qtrue; +} //end of the function WriteItemFuzzyWeights_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean WriteWeightConfig( char *filename, weightconfig_t *config ) { + int i; + FILE *fp; + weight_t *ifw; + + fp = fopen( filename, "wb" ); + if ( !fp ) { + return qfalse; + } + + for ( i = 0; i < config->numweights; i++ ) + { + ifw = &config->weights[i]; + if ( fprintf( fp, "\nweight \"%s\"\n", ifw->name ) < 0 ) { + return qfalse; + } + if ( fprintf( fp, "{\n" ) < 0 ) { + return qfalse; + } + if ( ifw->firstseperator->index > 0 ) { + if ( !WriteFuzzySeperators_r( fp, ifw->firstseperator, 1 ) ) { + return qfalse; + } + } //end if + else + { + if ( !WriteIndent( fp, 1 ) ) { + return qfalse; + } + if ( !WriteFuzzyWeight( fp, ifw->firstseperator ) ) { + return qfalse; + } + } //end else + if ( fprintf( fp, "} //end weight\n" ) < 0 ) { + return qfalse; + } + } //end for + fclose( fp ); + return qtrue; +} //end of the function WriteWeightConfig +#endif +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int FindFuzzyWeight( weightconfig_t *wc, char *name ) { + int i; + + for ( i = 0; i < wc->numweights; i++ ) + { + if ( !strcmp( wc->weights[i].name, name ) ) { + return i; + } //end if + } //end if + return -1; +} //end of the function FindFuzzyWeight +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float FuzzyWeight_r( int *inventory, fuzzyseperator_t *fs ) { + float scale, w1, w2; + + if ( inventory[fs->index] < fs->value ) { + if ( fs->child ) { + return FuzzyWeight_r( inventory, fs->child ); + } else { return fs->weight;} + } //end if + else if ( fs->next ) { + if ( inventory[fs->index] < fs->next->value ) { + //first weight + if ( fs->child ) { + w1 = FuzzyWeight_r( inventory, fs->child ); + } else { w1 = fs->weight;} + //second weight + if ( fs->next->child ) { + w2 = FuzzyWeight_r( inventory, fs->next->child ); + } else { w2 = fs->next->weight;} + //the scale factor + scale = ( inventory[fs->index] - fs->value ) / ( fs->next->value - fs->value ); + //scale between the two weights + return scale * w1 + ( 1 - scale ) * w2; + } //end if + return FuzzyWeight_r( inventory, fs->next ); + } //end else if + return fs->weight; +} //end of the function FuzzyWeight_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float FuzzyWeightUndecided_r( int *inventory, fuzzyseperator_t *fs ) { + float scale, w1, w2; + + if ( inventory[fs->index] < fs->value ) { + if ( fs->child ) { + return FuzzyWeightUndecided_r( inventory, fs->child ); + } else { return fs->minweight + random() * ( fs->maxweight - fs->minweight );} + } //end if + else if ( fs->next ) { + if ( inventory[fs->index] < fs->next->value ) { + //first weight + if ( fs->child ) { + w1 = FuzzyWeightUndecided_r( inventory, fs->child ); + } else { w1 = fs->minweight + random() * ( fs->maxweight - fs->minweight );} + //second weight + if ( fs->next->child ) { + w2 = FuzzyWeight_r( inventory, fs->next->child ); + } else { w2 = fs->next->minweight + random() * ( fs->next->maxweight - fs->next->minweight );} + //the scale factor + scale = ( inventory[fs->index] - fs->value ) / ( fs->next->value - fs->value ); + //scale between the two weights + return scale * w1 + ( 1 - scale ) * w2; + } //end if + return FuzzyWeightUndecided_r( inventory, fs->next ); + } //end else if + return fs->weight; +} //end of the function FuzzyWeightUndecided_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float FuzzyWeight( int *inventory, weightconfig_t *wc, int weightnum ) { +#ifdef EVALUATERECURSIVELY + return FuzzyWeight_r( inventory, wc->weights[weightnum].firstseperator ); +#else + fuzzyseperator_t *s; + + s = wc->weights[weightnum].firstseperator; + if ( !s ) { + return 0; + } + while ( 1 ) + { + if ( inventory[s->index] < s->value ) { + if ( s->child ) { + s = s->child; + } else { return s->weight;} + } //end if + else + { + if ( s->next ) { + s = s->next; + } else { return s->weight;} + } //end else + } //end if + return 0; +#endif +} //end of the function FuzzyWeight +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float FuzzyWeightUndecided( int *inventory, weightconfig_t *wc, int weightnum ) { +#ifdef EVALUATERECURSIVELY + return FuzzyWeightUndecided_r( inventory, wc->weights[weightnum].firstseperator ); +#else + fuzzyseperator_t *s; + + s = wc->weights[weightnum].firstseperator; + if ( !s ) { + return 0; + } + while ( 1 ) + { + if ( inventory[s->index] < s->value ) { + if ( s->child ) { + s = s->child; + } else { return s->minweight + random() * ( s->maxweight - s->minweight );} + } //end if + else + { + if ( s->next ) { + s = s->next; + } else { return s->minweight + random() * ( s->maxweight - s->minweight );} + } //end else + } //end if + return 0; +#endif +} //end of the function FuzzyWeightUndecided +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EvolveFuzzySeperator_r( fuzzyseperator_t *fs ) { + if ( fs->child ) { + EvolveFuzzySeperator_r( fs->child ); + } //end if + else if ( fs->type == WT_BALANCE ) { + //every once in a while an evolution leap occurs, mutation + if ( random() < 0.01 ) { + fs->weight += crandom() * ( fs->maxweight - fs->minweight ); + } else { fs->weight += crandom() * ( fs->maxweight - fs->minweight ) * 0.5;} + //modify bounds if necesary because of mutation + if ( fs->weight < fs->minweight ) { + fs->minweight = fs->weight; + } else if ( fs->weight > fs->maxweight ) { + fs->maxweight = fs->weight; + } + } //end else if + if ( fs->next ) { + EvolveFuzzySeperator_r( fs->next ); + } +} //end of the function EvolveFuzzySeperator_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EvolveWeightConfig( weightconfig_t *config ) { + int i; + + for ( i = 0; i < config->numweights; i++ ) + { + EvolveFuzzySeperator_r( config->weights[i].firstseperator ); + } //end for +} //end of the function EvolveWeightConfig +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ScaleFuzzySeperator_r( fuzzyseperator_t *fs, float scale ) { + if ( fs->child ) { + ScaleFuzzySeperator_r( fs->child, scale ); + } //end if + else if ( fs->type == WT_BALANCE ) { + // + fs->weight = ( fs->maxweight + fs->minweight ) * scale; + //get the weight between bounds + if ( fs->weight < fs->minweight ) { + fs->weight = fs->minweight; + } else if ( fs->weight > fs->maxweight ) { + fs->weight = fs->maxweight; + } + } //end else if + if ( fs->next ) { + ScaleFuzzySeperator_r( fs->next, scale ); + } +} //end of the function ScaleFuzzySeperator_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ScaleWeight( weightconfig_t *config, char *name, float scale ) { + int i; + + if ( scale < 0 ) { + scale = 0; + } else if ( scale > 1 ) { + scale = 1; + } + for ( i = 0; i < config->numweights; i++ ) + { + if ( !strcmp( name, config->weights[i].name ) ) { + ScaleFuzzySeperator_r( config->weights[i].firstseperator, scale ); + break; + } //end if + } //end for +} //end of the function ScaleWeight +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ScaleFuzzySeperatorBalanceRange_r( fuzzyseperator_t *fs, float scale ) { + if ( fs->child ) { + ScaleFuzzySeperatorBalanceRange_r( fs->child, scale ); + } //end if + else if ( fs->type == WT_BALANCE ) { + float mid = ( fs->minweight + fs->maxweight ) * 0.5; + //get the weight between bounds + fs->maxweight = mid + ( fs->maxweight - mid ) * scale; + fs->minweight = mid + ( fs->minweight - mid ) * scale; + if ( fs->maxweight < fs->minweight ) { + fs->maxweight = fs->minweight; + } //end if + } //end else if + if ( fs->next ) { + ScaleFuzzySeperatorBalanceRange_r( fs->next, scale ); + } +} //end of the function ScaleFuzzySeperatorBalanceRange_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ScaleFuzzyBalanceRange( weightconfig_t *config, float scale ) { + int i; + + if ( scale < 0 ) { + scale = 0; + } else if ( scale > 100 ) { + scale = 100; + } + for ( i = 0; i < config->numweights; i++ ) + { + ScaleFuzzySeperatorBalanceRange_r( config->weights[i].firstseperator, scale ); + } //end for +} //end of the function ScaleFuzzyBalanceRange +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int InterbreedFuzzySeperator_r( fuzzyseperator_t *fs1, fuzzyseperator_t *fs2, + fuzzyseperator_t *fsout ) { + if ( fs1->child ) { + if ( !fs2->child || !fsout->child ) { + botimport.Print( PRT_ERROR, "cannot interbreed weight configs, unequal child\n" ); + return qfalse; + } //end if + if ( !InterbreedFuzzySeperator_r( fs2->child, fs2->child, fsout->child ) ) { + return qfalse; + } //end if + } //end if + else if ( fs1->type == WT_BALANCE ) { + if ( fs2->type != WT_BALANCE || fsout->type != WT_BALANCE ) { + botimport.Print( PRT_ERROR, "cannot interbreed weight configs, unequal balance\n" ); + return qfalse; + } //end if + fsout->weight = ( fs1->weight + fs2->weight ) / 2; + if ( fsout->weight > fsout->maxweight ) { + fsout->maxweight = fsout->weight; + } + if ( fsout->weight > fsout->minweight ) { + fsout->minweight = fsout->weight; + } + } //end else if + if ( fs1->next ) { + if ( !fs2->next || !fsout->next ) { + botimport.Print( PRT_ERROR, "cannot interbreed weight configs, unequal next\n" ); + return qfalse; + } //end if + if ( !InterbreedFuzzySeperator_r( fs1->next, fs2->next, fsout->next ) ) { + return qfalse; + } //end if + } //end if + return qtrue; +} //end of the function InterbreedFuzzySeperator_r +//=========================================================================== +// config1 and config2 are interbreeded and stored in configout +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void InterbreedWeightConfigs( weightconfig_t *config1, weightconfig_t *config2, + weightconfig_t *configout ) { + int i; + + if ( config1->numweights != config2->numweights || + config1->numweights != configout->numweights ) { + botimport.Print( PRT_ERROR, "cannot interbreed weight configs, unequal numweights\n" ); + return; + } //end if + for ( i = 0; i < config1->numweights; i++ ) + { + InterbreedFuzzySeperator_r( config1->weights[i].firstseperator, + config2->weights[i].firstseperator, + configout->weights[i].firstseperator ); + } //end for +} //end of the function InterbreedWeightConfigs +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotShutdownWeights( void ) { + int i; + + for ( i = 0; i < MAX_WEIGHT_FILES; i++ ) + { + if ( weightFileList[i] ) { + FreeWeightConfig2( weightFileList[i] ); + weightFileList[i] = NULL; + } //end if + } //end for +} //end of the function BotShutdownWeights diff --git a/src/botlib/be_ai_weight.h b/src/botlib/be_ai_weight.h new file mode 100644 index 0000000..b26e83b --- /dev/null +++ b/src/botlib/be_ai_weight.h @@ -0,0 +1,89 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_ai_weight.h + * + * desc: fuzzy weights + * + * + *****************************************************************************/ + +#define WT_BALANCE 1 +#define MAX_WEIGHTS 128 + +//fuzzy seperator +typedef struct fuzzyseperator_s +{ + int index; + int value; + int type; + float weight; + float minweight; + float maxweight; + struct fuzzyseperator_s *child; + struct fuzzyseperator_s *next; +} fuzzyseperator_t; + +//fuzzy weight +typedef struct weight_s +{ + char *name; + struct fuzzyseperator_s *firstseperator; +} weight_t; + +//weight configuration +typedef struct weightconfig_s +{ + int numweights; + weight_t weights[MAX_WEIGHTS]; + char filename[MAX_QPATH]; +} weightconfig_t; + +//reads a weight configuration +weightconfig_t *ReadWeightConfig( char *filename ); +//free a weight configuration +void FreeWeightConfig( weightconfig_t *config ); +//writes a weight configuration, returns true if successfull +qboolean WriteWeightConfig( char *filename, weightconfig_t *config ); +//find the fuzzy weight with the given name +int FindFuzzyWeight( weightconfig_t *wc, char *name ); +//returns the fuzzy weight for the given inventory and weight +float FuzzyWeight( int *inventory, weightconfig_t *wc, int weightnum ); +float FuzzyWeightUndecided( int *inventory, weightconfig_t *wc, int weightnum ); +//scales the weight with the given name +void ScaleWeight( weightconfig_t *config, char *name, float scale ); +//scale the balance range +void ScaleBalanceRange( weightconfig_t *config, float scale ); +//evolves the weight configuration +void EvolveWeightConfig( weightconfig_t *config ); +//interbreed the weight configurations and stores the interbreeded one in configout +void InterbreedWeightConfigs( weightconfig_t *config1, weightconfig_t *config2, weightconfig_t *configout ); +//frees cached weight configurations +void BotShutdownWeights( void ); diff --git a/src/botlib/be_ea.c b/src/botlib/be_ea.c new file mode 100644 index 0000000..9648e5b --- /dev/null +++ b/src/botlib/be_ea.c @@ -0,0 +1,480 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_ea.c + * + * desc: elementary actions + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "l_memory.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "../game/botlib.h" +#include "be_interface.h" + +#define MAX_USERMOVE 400 +#define MAX_COMMANDARGUMENTS 10 +#define ACTION_JUMPEDLASTFRAME 128 + +bot_input_t *botinputs; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_Say( int client, char *str ) { + botimport.BotClientCommand( client, va( "say %s", str ) ); +} //end of the function EA_Say +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_SayTeam( int client, char *str ) { + botimport.BotClientCommand( client, va( "say_team %s", str ) ); +} //end of the function EA_SayTeam +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_UseItem( int client, char *it ) { + botimport.BotClientCommand( client, va( "use %s", it ) ); +} //end of the function EA_UseItem +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_DropItem( int client, char *it ) { + botimport.BotClientCommand( client, va( "drop %s", it ) ); +} //end of the function EA_DropItem +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_UseInv( int client, char *inv ) { + botimport.BotClientCommand( client, va( "invuse %s", inv ) ); +} //end of the function EA_UseInv +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_DropInv( int client, char *inv ) { + botimport.BotClientCommand( client, va( "invdrop %s", inv ) ); +} //end of the function EA_DropInv +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_Gesture( int client ) { + bot_input_t *bi; + + bi = &botinputs[client]; + + bi->actionflags |= ACTION_GESTURE; +} //end of the function EA_Gesture +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_Command( int client, char *command ) { + botimport.BotClientCommand( client, command ); +} //end of the function EA_Command +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_SelectWeapon( int client, int weapon ) { + bot_input_t *bi; + + bi = &botinputs[client]; + + bi->weapon = weapon; +} //end of the function EA_SelectWeapon +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_Attack( int client ) { + bot_input_t *bi; + + bi = &botinputs[client]; + + bi->actionflags |= ACTION_ATTACK; +} //end of the function EA_Attack +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_Reload( int client ) { + bot_input_t *bi; + + bi = &botinputs[client]; + + bi->actionflags |= ACTION_RELOAD; +} //end of the function EA_Attack +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_Talk( int client ) { + bot_input_t *bi; + + bi = &botinputs[client]; + + bi->actionflags |= ACTION_TALK; +} //end of the function EA_Talk +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_Use( int client ) { + bot_input_t *bi; + + bi = &botinputs[client]; + + bi->actionflags |= ACTION_USE; +} //end of the function EA_Use +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_Respawn( int client ) { + bot_input_t *bi; + + bi = &botinputs[client]; + + bi->actionflags |= ACTION_RESPAWN; +} //end of the function EA_Respawn +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_Jump( int client ) { + bot_input_t *bi; + + bi = &botinputs[client]; + + if ( bi->actionflags & ACTION_JUMPEDLASTFRAME ) { + bi->actionflags &= ~ACTION_JUMP; + } //end if + else + { + bi->actionflags |= ACTION_JUMP; + } //end if +} //end of the function EA_Jump +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_DelayedJump( int client ) { + bot_input_t *bi; + + bi = &botinputs[client]; + + if ( bi->actionflags & ACTION_JUMPEDLASTFRAME ) { + bi->actionflags &= ~ACTION_DELAYEDJUMP; + } //end if + else + { + bi->actionflags |= ACTION_DELAYEDJUMP; + } //end if +} //end of the function EA_DelayedJump +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_Crouch( int client ) { + bot_input_t *bi; + + bi = &botinputs[client]; + + bi->actionflags |= ACTION_CROUCH; +} //end of the function EA_Crouch +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_Walk( int client ) { + bot_input_t *bi; + + bi = &botinputs[client]; + + bi->actionflags |= ACTION_WALK; +} //end of the function EA_Walk +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_MoveUp( int client ) { + bot_input_t *bi; + + bi = &botinputs[client]; + + bi->actionflags |= ACTION_MOVEUP; +} //end of the function EA_MoveUp +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_MoveDown( int client ) { + bot_input_t *bi; + + bi = &botinputs[client]; + + bi->actionflags |= ACTION_MOVEDOWN; +} //end of the function EA_MoveDown +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_MoveForward( int client ) { + bot_input_t *bi; + + bi = &botinputs[client]; + + bi->actionflags |= ACTION_MOVEFORWARD; +} //end of the function EA_MoveForward +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_MoveBack( int client ) { + bot_input_t *bi; + + bi = &botinputs[client]; + + bi->actionflags |= ACTION_MOVEBACK; +} //end of the function EA_MoveBack +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_MoveLeft( int client ) { + bot_input_t *bi; + + bi = &botinputs[client]; + + bi->actionflags |= ACTION_MOVELEFT; +} //end of the function EA_MoveLeft +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_MoveRight( int client ) { + bot_input_t *bi; + + bi = &botinputs[client]; + + bi->actionflags |= ACTION_MOVERIGHT; +} //end of the function EA_MoveRight +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_Move( int client, vec3_t dir, float speed ) { + bot_input_t *bi; + + bi = &botinputs[client]; + + VectorCopy( dir, bi->dir ); + //cap speed + if ( speed > MAX_USERMOVE ) { + speed = MAX_USERMOVE; + } else if ( speed < -MAX_USERMOVE ) { + speed = -MAX_USERMOVE; + } + bi->speed = speed; +} //end of the function EA_Move +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_View( int client, vec3_t viewangles ) { + bot_input_t *bi; + + bi = &botinputs[client]; + + VectorCopy( viewangles, bi->viewangles ); +} //end of the function EA_View +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_EndRegular( int client, float thinktime ) { +/* + bot_input_t *bi; + int jumped = qfalse; + + bi = &botinputs[client]; + + bi->actionflags &= ~ACTION_JUMPEDLASTFRAME; + + bi->thinktime = thinktime; + botimport.BotInput(client, bi); + + bi->thinktime = 0; + VectorClear(bi->dir); + bi->speed = 0; + jumped = bi->actionflags & ACTION_JUMP; + bi->actionflags = 0; + if (jumped) bi->actionflags |= ACTION_JUMPEDLASTFRAME; +*/ +} //end of the function EA_EndRegular +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_GetInput( int client, float thinktime, bot_input_t *input ) { + bot_input_t *bi; +// int jumped = qfalse; + + bi = &botinputs[client]; + +// bi->actionflags &= ~ACTION_JUMPEDLASTFRAME; + + bi->thinktime = thinktime; + memcpy( input, bi, sizeof( bot_input_t ) ); + + /* + bi->thinktime = 0; + VectorClear(bi->dir); + bi->speed = 0; + jumped = bi->actionflags & ACTION_JUMP; + bi->actionflags = 0; + if (jumped) bi->actionflags |= ACTION_JUMPEDLASTFRAME; + */ +} //end of the function EA_GetInput +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_ResetInput( int client, bot_input_t *init ) { + bot_input_t *bi; + int jumped = qfalse; + + bi = &botinputs[client]; + bi->actionflags &= ~ACTION_JUMPEDLASTFRAME; + + bi->thinktime = 0; + VectorClear( bi->dir ); + bi->speed = 0; + jumped = bi->actionflags & ACTION_JUMP; + bi->actionflags = 0; + if ( jumped ) { + bi->actionflags |= ACTION_JUMPEDLASTFRAME; + } + + if ( init ) { + memcpy( bi, init, sizeof( bot_input_t ) ); + } +} //end of the function EA_ResetInput +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int EA_Setup( void ) { + //initialize the bot inputs + botinputs = (bot_input_t *) GetClearedHunkMemory( + botlibglobals.maxclients * sizeof( bot_input_t ) ); + return BLERR_NOERROR; +} //end of the function EA_Setup +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void EA_Shutdown( void ) { + FreeMemory( botinputs ); + botinputs = NULL; +} //end of the function EA_Shutdown diff --git a/src/botlib/be_interface.c b/src/botlib/be_interface.c new file mode 100644 index 0000000..1bee884 --- /dev/null +++ b/src/botlib/be_interface.c @@ -0,0 +1,936 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_interface.c + * + * desc: bot library interface + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "l_memory.h" +#include "l_log.h" +#include "l_libvar.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "aasfile.h" +#include "../game/botlib.h" +#include "../game/be_aas.h" +#include "be_aas_funcs.h" +#include "be_aas_def.h" +#include "be_interface.h" + +#include "../game/be_ea.h" +#include "be_ai_weight.h" +#include "../game/be_ai_goal.h" +#include "../game/be_ai_move.h" +#include "../game/be_ai_weap.h" +#include "../game/be_ai_chat.h" +#include "../game/be_ai_char.h" +#include "../game/be_ai_gen.h" + +//library globals in a structure +botlib_globals_t botlibglobals; + +botlib_export_t be_botlib_export; +botlib_import_t botimport; +// +int bot_developer; +//qtrue if the library is setup +int botlibsetup = qfalse; + +//=========================================================================== +// +// several functions used by the exported functions +// +//=========================================================================== + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +// Ridah, faster Win32 code +#ifdef _WIN32 +#undef MAX_PATH // this is an ugly hack, to temporarily ignore the current definition, since it's also defined in windows.h +#include +#undef MAX_PATH +#define MAX_PATH MAX_QPATH +#endif + +int Sys_MilliSeconds( void ) { +// Ridah, faster Win32 code +#ifdef _WIN32 + int sys_curtime; + static qboolean initialized = qfalse; + static int sys_timeBase; + + if ( !initialized ) { + sys_timeBase = timeGetTime(); + initialized = qtrue; + } + sys_curtime = timeGetTime() - sys_timeBase; + + return sys_curtime; +#else + return clock() * 1000 / CLOCKS_PER_SEC; +#endif +} //end of the function Sys_MilliSeconds +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean ValidClientNumber( int num, char *str ) { + if ( num < 0 || num > botlibglobals.maxclients ) { + //weird: the disabled stuff results in a crash + botimport.Print( PRT_ERROR, "%s: invalid client number %d, [0, %d]\n", + str, num, botlibglobals.maxclients ); + return qfalse; + } //end if + return qtrue; +} //end of the function BotValidateClientNumber +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean ValidEntityNumber( int num, char *str ) { + if ( num < 0 || num > botlibglobals.maxentities ) { + botimport.Print( PRT_ERROR, "%s: invalid entity number %d, [0, %d]\n", + str, num, botlibglobals.maxentities ); + return qfalse; + } //end if + return qtrue; +} //end of the function BotValidateClientNumber +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean BotLibSetup( char *str ) { +// return qtrue; + + if ( !botlibglobals.botlibsetup ) { + botimport.Print( PRT_ERROR, "%s: bot library used before being setup\n", str ); + return qfalse; + } //end if + return qtrue; +} //end of the function BotLibSetup +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int Export_BotLibSetup( void ) { + int errnum; + + bot_developer = LibVarGetValue( "bot_developer" ); + //initialize byte swapping (litte endian etc.) + Swap_Init(); + Log_Open( "botlib.log" ); + // + botimport.Print( PRT_MESSAGE, "------- BotLib Initialization -------\n" ); + // + botlibglobals.maxclients = (int) LibVarValue( "maxclients", "128" ); + botlibglobals.maxentities = (int) LibVarValue( "maxentities", "2048" ); + + errnum = AAS_Setup(); //be_aas_main.c + if ( errnum != BLERR_NOERROR ) { + return errnum; + } + errnum = EA_Setup(); //be_ea.c + if ( errnum != BLERR_NOERROR ) { + return errnum; + } +// errnum = BotSetupWeaponAI(); //be_ai_weap.c +// if (errnum != BLERR_NOERROR)return errnum; +// errnum = BotSetupGoalAI(); //be_ai_goal.c +// if (errnum != BLERR_NOERROR) return errnum; +// errnum = BotSetupChatAI(); //be_ai_chat.c +// if (errnum != BLERR_NOERROR) return errnum; + errnum = BotSetupMoveAI(); //be_ai_move.c + if ( errnum != BLERR_NOERROR ) { + return errnum; + } + + botlibsetup = qtrue; + botlibglobals.botlibsetup = qtrue; + + return BLERR_NOERROR; +} //end of the function Export_BotLibSetup +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int Export_BotLibShutdown( void ) { + static int recursive = 0; + + if ( !BotLibSetup( "BotLibShutdown" ) ) { + return BLERR_LIBRARYNOTSETUP; + } + // + if ( recursive ) { + return BLERR_NOERROR; + } + recursive = 1; + // shutdown all AI subsystems + BotShutdownChatAI(); //be_ai_chat.c + BotShutdownMoveAI(); //be_ai_move.c + BotShutdownGoalAI(); //be_ai_goal.c + BotShutdownWeaponAI(); //be_ai_weap.c + BotShutdownWeights(); //be_ai_weight.c + BotShutdownCharacters(); //be_ai_char.c + // shutdown AAS + AAS_Shutdown(); + // shutdown bot elemantary actions + EA_Shutdown(); + // free all libvars + LibVarDeAllocAll(); + // remove all global defines from the pre compiler + PC_RemoveAllGlobalDefines(); + // shut down library log file + Log_Shutdown(); + // + botlibsetup = qfalse; + botlibglobals.botlibsetup = qfalse; + recursive = 0; + // print any files still open + PC_CheckOpenSourceHandles(); + // +#ifdef _DEBUG + Log_AlwaysOpen( "memory.log" ); + PrintMemoryLabels(); + Log_Shutdown(); +#endif + return BLERR_NOERROR; +} //end of the function Export_BotLibShutdown +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int Export_BotLibVarSet( char *var_name, char *value ) { + LibVarSet( var_name, value ); + return BLERR_NOERROR; +} //end of the function Export_BotLibVarSet +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int Export_BotLibVarGet( char *var_name, char *value, int size ) { + char *varvalue; + + varvalue = LibVarGetString( var_name ); + strncpy( value, varvalue, size - 1 ); + value[size - 1] = '\0'; + return BLERR_NOERROR; +} //end of the function Export_BotLibVarGet +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int Export_BotLibStartFrame( float time ) { + if ( !BotLibSetup( "BotStartFrame" ) ) { + return BLERR_LIBRARYNOTSETUP; + } + return AAS_StartFrame( time ); +} //end of the function Export_BotLibStartFrame +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int Export_BotLibLoadMap( const char *mapname ) { +#ifdef DEBUG + int starttime = Sys_MilliSeconds(); +#endif + int errnum; + + if ( !BotLibSetup( "BotLoadMap" ) ) { + return BLERR_LIBRARYNOTSETUP; + } + // + botimport.Print( PRT_MESSAGE, "------------ Map Loading ------------\n" ); + //startup AAS for the current map, model and sound index + errnum = AAS_LoadMap( mapname ); + if ( errnum != BLERR_NOERROR ) { + return errnum; + } + //initialize the items in the level + BotInitLevelItems(); //be_ai_goal.h + BotSetBrushModelTypes(); //be_ai_move.h + // + botimport.Print( PRT_MESSAGE, "-------------------------------------\n" ); +#ifdef DEBUG + botimport.Print( PRT_MESSAGE, "map loaded in %d msec\n", Sys_MilliSeconds() - starttime ); +#endif + // + return BLERR_NOERROR; +} //end of the function Export_BotLibLoadMap +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int Export_BotLibUpdateEntity( int ent, bot_entitystate_t *state ) { + if ( !BotLibSetup( "BotUpdateEntity" ) ) { + return BLERR_LIBRARYNOTSETUP; + } + if ( !ValidEntityNumber( ent, "BotUpdateEntity" ) ) { + return BLERR_INVALIDENTITYNUMBER; + } + + return AAS_UpdateEntity( ent, state ); +} //end of the function Export_BotLibUpdateEntity +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_TestMovementPrediction( int entnum, vec3_t origin, vec3_t dir ); +void ElevatorBottomCenter( aas_reachability_t *reach, vec3_t bottomcenter ); +int BotGetReachabilityToGoal( vec3_t origin, int areanum, int entnum, + int lastgoalareanum, int lastareanum, + int *avoidreach, float *avoidreachtimes, int *avoidreachtries, + bot_goal_t *goal, int travelflags, int movetravelflags ); + +int AAS_PointLight( vec3_t origin, int *red, int *green, int *blue ); + +int AAS_TraceAreas( vec3_t start, vec3_t end, int *areas, vec3_t *points, int maxareas ); + +int AAS_Reachability_WeaponJump( int area1num, int area2num ); + +int BotFuzzyPointReachabilityArea( vec3_t origin ); + +float BotGapDistance( vec3_t origin, vec3_t hordir, int entnum ); + +int AAS_NearestHideArea( int srcnum, vec3_t origin, int areanum, int enemynum, vec3_t enemyorigin, int enemyareanum, int travelflags ); + +int AAS_FindAttackSpotWithinRange( int srcnum, int rangenum, int enemynum, float rangedist, int travelflags, float *outpos ); + +qboolean AAS_GetRouteFirstVisPos( vec3_t srcpos, vec3_t destpos, int travelflags, vec3_t retpos ); + +void AAS_SetAASBlockingEntity( vec3_t absmin, vec3_t absmax, qboolean blocking ); + +int BotExportTest( int parm0, char *parm1, vec3_t parm2, vec3_t parm3 ) { + +// return AAS_PointLight(parm2, NULL, NULL, NULL); + +#ifdef DEBUG + static int area = -1; + static int line[2]; + int newarea, i, highlightarea, bot_testhidepos, hideposarea, bot_testroutevispos; +// int reachnum; + vec3_t eye, forward, right, end, origin; +// vec3_t bottomcenter; +// aas_trace_t trace; +// aas_face_t *face; +// aas_entity_t *ent; +// bsp_trace_t bsptrace; +// aas_reachability_t reach; +// bot_goal_t goal; + +// clock_t start_time, end_time; + vec3_t mins = {-16, -16, -24}; + vec3_t maxs = {16, 16, 32}; +// int areas[10], numareas; + + + //return 0; + + if ( !( *aasworld ).loaded ) { + return 0; + } + AAS_SetCurrentWorld( 0 ); + + for ( i = 0; i < 2; i++ ) if ( !line[i] ) { + line[i] = botimport.DebugLineCreate(); + } + +// AAS_ClearShownDebugLines(); + bot_testhidepos = LibVarGetValue( "bot_testhidepos" ); + if ( bot_testhidepos ) { + VectorCopy( parm2, origin ); + newarea = BotFuzzyPointReachabilityArea( origin ); + if ( parm0 & 1 ) { + botlibglobals.goalareanum = newarea; + VectorCopy( origin, botlibglobals.goalorigin ); + botimport.Print( PRT_MESSAGE, "new enemy position %2.1f %2.1f %2.1f area %d\n", + origin[0], origin[1], origin[2], newarea ); + } //end if + AAS_ClearShownPolygons(); + AAS_ClearShownDebugLines(); + hideposarea = AAS_NearestHideArea( -1, origin, AAS_PointAreaNum( origin ), 0, + botlibglobals.goalorigin, botlibglobals.goalareanum, TFL_DEFAULT ); + + if ( bot_testhidepos > 1 ) { + if ( hideposarea ) { + botimport.Print( PRT_MESSAGE, "hidepos (%i) %2.1f %2.1f %2.1f\n", + hideposarea, + ( *aasworld ).areawaypoints[hideposarea][0], + ( *aasworld ).areawaypoints[hideposarea][1], + ( *aasworld ).areawaypoints[hideposarea][2] ); + } else { + botimport.Print( PRT_MESSAGE, "no hidepos found\n" ); + } + } + + //area we are currently in + AAS_ShowAreaPolygons( newarea, 1, qtrue ); + //enemy position + AAS_ShowAreaPolygons( botlibglobals.goalareanum, 2, qtrue ); + //area we should go hide + AAS_ShowAreaPolygons( hideposarea, 4, qtrue ); + return 0; + } + + bot_testroutevispos = LibVarGetValue( "bot_testroutevispos" ); + if ( bot_testroutevispos ) { + VectorCopy( parm2, origin ); + newarea = BotFuzzyPointReachabilityArea( origin ); + if ( parm0 & 1 ) { + botlibglobals.goalareanum = newarea; + VectorCopy( origin, botlibglobals.goalorigin ); + botimport.Print( PRT_MESSAGE, "new enemy position %2.1f %2.1f %2.1f area %d\n", + origin[0], origin[1], origin[2], newarea ); + } //end if + AAS_ClearShownPolygons(); + AAS_ClearShownDebugLines(); + AAS_GetRouteFirstVisPos( botlibglobals.goalorigin, origin, TFL_DEFAULT, eye ); + //area we are currently in + AAS_ShowAreaPolygons( newarea, 1, qtrue ); + //enemy position + AAS_ShowAreaPolygons( botlibglobals.goalareanum, 2, qtrue ); + //area that is visible in path from enemy pos + hideposarea = BotFuzzyPointReachabilityArea( eye ); + AAS_ShowAreaPolygons( hideposarea, 4, qtrue ); + return 0; + } + + //if (AAS_AgainstLadder(parm2)) botimport.Print(PRT_MESSAGE, "against ladder\n"); + //BotOnGround(parm2, PRESENCE_NORMAL, 1, &newarea, &newarea); + //botimport.Print(PRT_MESSAGE, "%f %f %f\n", parm2[0], parm2[1], parm2[2]); + //* + highlightarea = LibVarGetValue( "bot_highlightarea" ); + if ( highlightarea > 0 ) { + newarea = highlightarea; + } //end if + else + { + VectorCopy( parm2, origin ); + origin[2] += 0.5; + //newarea = AAS_PointAreaNum(origin); + newarea = BotFuzzyPointReachabilityArea( origin ); + } //end else + + botimport.Print( PRT_MESSAGE, "\rtravel time to goal (%d) = %d ", botlibglobals.goalareanum, + AAS_AreaTravelTimeToGoalArea( newarea, origin, botlibglobals.goalareanum, TFL_DEFAULT ) ); + //newarea = BotReachabilityArea(origin, qtrue); + if ( newarea != area ) { + botimport.Print( PRT_MESSAGE, "origin = %f, %f, %f\n", origin[0], origin[1], origin[2] ); + area = newarea; + botimport.Print( PRT_MESSAGE, "new area %d, cluster %d, presence type %d\n", + area, AAS_AreaCluster( area ), AAS_PointPresenceType( origin ) ); + if ( ( *aasworld ).areasettings[area].areaflags & AREA_LIQUID ) { + botimport.Print( PRT_MESSAGE, "liquid area\n" ); + } //end if + botimport.Print( PRT_MESSAGE, "area contents: " ); + if ( ( *aasworld ).areasettings[area].contents & AREACONTENTS_WATER ) { + botimport.Print( PRT_MESSAGE, "water " ); + } //end if + if ( ( *aasworld ).areasettings[area].contents & AREACONTENTS_LAVA ) { + botimport.Print( PRT_MESSAGE, "lava " ); + } //end if + if ( ( *aasworld ).areasettings[area].contents & AREACONTENTS_SLIME ) { +// botimport.Print(PRT_MESSAGE, "slime "); + botimport.Print( PRT_MESSAGE, "slag " ); + } //end if + if ( ( *aasworld ).areasettings[area].contents & AREACONTENTS_JUMPPAD ) { + botimport.Print( PRT_MESSAGE, "jump pad " ); + } //end if + if ( ( *aasworld ).areasettings[area].contents & AREACONTENTS_CLUSTERPORTAL ) { + botimport.Print( PRT_MESSAGE, "cluster portal " ); + } //end if + if ( ( *aasworld ).areasettings[area].contents & AREACONTENTS_DONOTENTER ) { + botimport.Print( PRT_MESSAGE, "do not enter " ); + } //end if + if ( ( *aasworld ).areasettings[area].contents & AREACONTENTS_DONOTENTER_LARGE ) { + botimport.Print( PRT_MESSAGE, "do not enter large " ); + } //end if + if ( !( *aasworld ).areasettings[area].contents ) { + botimport.Print( PRT_MESSAGE, "empty " ); + } //end if + if ( ( *aasworld ).areasettings[area].areaflags & AREA_DISABLED ) { + botimport.Print( PRT_MESSAGE, "DISABLED" ); + } //end if + botimport.Print( PRT_MESSAGE, "\n" ); + botimport.Print( PRT_MESSAGE, "travel time to goal (%d) = %d\n", botlibglobals.goalareanum, + AAS_AreaTravelTimeToGoalArea( newarea, origin, botlibglobals.goalareanum, TFL_DEFAULT | TFL_ROCKETJUMP ) ); + /* + VectorCopy(origin, end); + end[2] += 5; + numareas = AAS_TraceAreas(origin, end, areas, NULL, 10); + AAS_TraceClientBBox(origin, end, PRESENCE_CROUCH, -1); + botimport.Print(PRT_MESSAGE, "num areas = %d, area = %d\n", numareas, areas[0]); + */ + /* + botlibglobals.goalareanum = newarea; + VectorCopy(parm2, botlibglobals.goalorigin); + botimport.Print(PRT_MESSAGE, "new goal %2.1f %2.1f %2.1f area %d\n", + origin[0], origin[1], origin[2], newarea); + */ + } //end if + //* + if ( parm0 & 1 ) { + botlibglobals.goalareanum = newarea; + VectorCopy( parm2, botlibglobals.goalorigin ); + botimport.Print( PRT_MESSAGE, "new goal %2.1f %2.1f %2.1f area %d\n", + origin[0], origin[1], origin[2], newarea ); + } //end if*/ +// if (parm0 & BUTTON_USE) +// { +// botlibglobals.runai = !botlibglobals.runai; +// if (botlibglobals.runai) botimport.Print(PRT_MESSAGE, "started AI\n"); +// else botimport.Print(PRT_MESSAGE, "stopped AI\n"); + //* / + /* + goal.areanum = botlibglobals.goalareanum; + reachnum = BotGetReachabilityToGoal(parm2, newarea, 1, + ms.avoidreach, ms.avoidreachtimes, + &goal, TFL_DEFAULT); + if (!reachnum) + { + botimport.Print(PRT_MESSAGE, "goal not reachable\n"); + } //end if + else + { + AAS_ReachabilityFromNum(reachnum, &reach); + AAS_ClearShownDebugLines(); + AAS_ShowArea(area, qtrue); + AAS_ShowArea(reach.areanum, qtrue); + AAS_DrawCross(reach.start, 6, LINECOLOR_BLUE); + AAS_DrawCross(reach.end, 6, LINECOLOR_RED); + // + if (reach.traveltype == TRAVEL_ELEVATOR) + { + ElevatorBottomCenter(&reach, bottomcenter); + AAS_DrawCross(bottomcenter, 10, LINECOLOR_GREEN); + } //end if + } //end else*/ +// botimport.Print(PRT_MESSAGE, "travel time to goal = %d\n", +// AAS_AreaTravelTimeToGoalArea(area, origin, botlibglobals.goalareanum, TFL_DEFAULT)); +// botimport.Print(PRT_MESSAGE, "test rj from 703 to 716\n"); +// AAS_Reachability_WeaponJump(703, 716); +// } //end if*/ + +/* face = AAS_AreaGroundFace(newarea, parm2); + if (face) + { + AAS_ShowFace(face - (*aasworld).faces); + } //end if*/ + /* + AAS_ClearShownDebugLines(); + AAS_ShowArea(newarea, parm0 & BUTTON_USE); + AAS_ShowReachableAreas(area); + */ + AAS_ClearShownPolygons(); + AAS_ClearShownDebugLines(); + AAS_ShowAreaPolygons( newarea, 1, parm0 & 4 ); + if ( parm0 & 2 ) { + AAS_ShowReachableAreas( area ); + } else + { + static int lastgoalareanum, lastareanum; + static int avoidreach[MAX_AVOIDREACH]; + static float avoidreachtimes[MAX_AVOIDREACH]; + static int avoidreachtries[MAX_AVOIDREACH]; + int reachnum; + bot_goal_t goal; + aas_reachability_t reach; + + goal.areanum = botlibglobals.goalareanum; + VectorCopy( botlibglobals.goalorigin, goal.origin ); + reachnum = BotGetReachabilityToGoal( origin, newarea, -1, + lastgoalareanum, lastareanum, + avoidreach, avoidreachtimes, avoidreachtries, + &goal, TFL_DEFAULT | TFL_FUNCBOB, TFL_DEFAULT | TFL_FUNCBOB ); + AAS_ReachabilityFromNum( reachnum, &reach ); + AAS_ShowReachability( &reach ); + } //end else + VectorClear( forward ); + //BotGapDistance(origin, forward, 0); + /* + if (parm0 & BUTTON_USE) + { + botimport.Print(PRT_MESSAGE, "test rj from 703 to 716\n"); + AAS_Reachability_WeaponJump(703, 716); + } //end if*/ + + AngleVectors( parm3, forward, right, NULL ); + //get the eye 16 units to the right of the origin + VectorMA( parm2, 8, right, eye ); + //get the eye 24 units up + eye[2] += 24; + //get the end point for the line to be traced + VectorMA( eye, 800, forward, end ); + +// AAS_TestMovementPrediction(1, parm2, forward); +/* //trace the line to find the hit point + trace = AAS_TraceClientBBox(eye, end, PRESENCE_NORMAL, 1); + if (!line[0]) line[0] = botimport.DebugLineCreate(); + botimport.DebugLineShow(line[0], eye, trace.endpos, LINECOLOR_BLUE); + // + AAS_ClearShownDebugLines(); + if (trace.ent) + { + ent = &(*aasworld).entities[trace.ent]; + AAS_ShowBoundingBox(ent->origin, ent->mins, ent->maxs); + } //end if*/ + +/* + start_time = clock(); + for (i = 0; i < 2000; i++) + { + AAS_Trace2(eye, mins, maxs, end, 1, MASK_PLAYERSOLID); +// AAS_TraceClientBBox(eye, end, PRESENCE_NORMAL, 1); + } //end for + end_time = clock(); + botimport.Print(PRT_MESSAGE, "me %lu clocks, %lu CLOCKS_PER_SEC\n", end_time - start_time, CLOCKS_PER_SEC); + start_time = clock(); + for (i = 0; i < 2000; i++) + { + AAS_Trace(eye, mins, maxs, end, 1, MASK_PLAYERSOLID); + } //end for + end_time = clock(); + botimport.Print(PRT_MESSAGE, "id %lu clocks, %lu CLOCKS_PER_SEC\n", end_time - start_time, CLOCKS_PER_SEC); +*/ + + /* + AAS_ClearShownDebugLines(); + //bsptrace = AAS_Trace(eye, NULL, NULL, end, 1, MASK_PLAYERSOLID); + bsptrace = AAS_Trace(eye, mins, maxs, end, 1, MASK_PLAYERSOLID); + if (!line[0]) line[0] = botimport.DebugLineCreate(); + botimport.DebugLineShow(line[0], eye, bsptrace.endpos, LINECOLOR_YELLOW); + if (bsptrace.fraction < 1.0) + { + face = AAS_TraceEndFace(&trace); + if (face) + { + AAS_ShowFace(face - (*aasworld).faces); + } //end if + AAS_DrawPlaneCross(bsptrace.endpos, + bsptrace.plane.normal, + bsptrace.plane.dist + bsptrace.exp_dist, + bsptrace.plane.type, LINECOLOR_GREEN); + if (trace.ent) + { + ent = &(*aasworld).entities[trace.ent]; + AAS_ShowBoundingBox(ent->origin, ent->mins, ent->maxs); + } //end if + } //end if*/ + /*/ + //bsptrace = AAS_Trace2(eye, NULL, NULL, end, 1, MASK_PLAYERSOLID); + bsptrace = AAS_Trace2(eye, mins, maxs, end, 1, MASK_PLAYERSOLID); + botimport.DebugLineShow(line[1], eye, bsptrace.endpos, LINECOLOR_BLUE); + if (bsptrace.fraction < 1.0) + { + AAS_DrawPlaneCross(bsptrace.endpos, + bsptrace.plane.normal, + bsptrace.plane.dist,// + bsptrace.exp_dist, + bsptrace.plane.type, LINECOLOR_RED); + if (bsptrace.ent) + { + ent = &(*aasworld).entities[bsptrace.ent]; + AAS_ShowBoundingBox(ent->origin, ent->mins, ent->maxs); + } //end if + } //end if + */ +#endif + return 0; +} //end of the function BotExportTest + + +/* +============ +Init_AAS_Export +============ +*/ +static void Init_AAS_Export( aas_export_t *aas ) { + //-------------------------------------------- + // be_aas_entity.c + //-------------------------------------------- + aas->AAS_EntityInfo = AAS_EntityInfo; + //-------------------------------------------- + // be_aas_main.c + //-------------------------------------------- + aas->AAS_Initialized = AAS_Initialized; + aas->AAS_PresenceTypeBoundingBox = AAS_PresenceTypeBoundingBox; + aas->AAS_Time = AAS_Time; + //-------------------------------------------- + // be_aas_sample.c + //-------------------------------------------- + aas->AAS_PointAreaNum = AAS_PointAreaNum; + aas->AAS_TraceAreas = AAS_TraceAreas; + //-------------------------------------------- + // be_aas_bspq3.c + //-------------------------------------------- + aas->AAS_PointContents = AAS_PointContents; + aas->AAS_NextBSPEntity = AAS_NextBSPEntity; + aas->AAS_ValueForBSPEpairKey = AAS_ValueForBSPEpairKey; + aas->AAS_VectorForBSPEpairKey = AAS_VectorForBSPEpairKey; + aas->AAS_FloatForBSPEpairKey = AAS_FloatForBSPEpairKey; + aas->AAS_IntForBSPEpairKey = AAS_IntForBSPEpairKey; + //-------------------------------------------- + // be_aas_reach.c + //-------------------------------------------- + aas->AAS_AreaReachability = AAS_AreaReachability; + //-------------------------------------------- + // be_aas_route.c + //-------------------------------------------- + aas->AAS_AreaTravelTimeToGoalArea = AAS_AreaTravelTimeToGoalArea; + //-------------------------------------------- + // be_aas_move.c + //-------------------------------------------- + aas->AAS_Swimming = AAS_Swimming; + aas->AAS_PredictClientMovement = AAS_PredictClientMovement; + + // Ridah, route-tables + //-------------------------------------------- + // be_aas_routetable.c + //-------------------------------------------- + aas->AAS_RT_ShowRoute = AAS_RT_ShowRoute; + aas->AAS_RT_GetHidePos = AAS_RT_GetHidePos; + aas->AAS_FindAttackSpotWithinRange = AAS_FindAttackSpotWithinRange; + aas->AAS_GetRouteFirstVisPos = AAS_GetRouteFirstVisPos; + aas->AAS_SetAASBlockingEntity = AAS_SetAASBlockingEntity; + // done. + + // Ridah, multiple AAS files + aas->AAS_SetCurrentWorld = AAS_SetCurrentWorld; + // done. + +} + + +/* +============ +Init_EA_Export +============ +*/ +static void Init_EA_Export( ea_export_t *ea ) { + //ClientCommand elementary actions + ea->EA_Say = EA_Say; + ea->EA_SayTeam = EA_SayTeam; + ea->EA_UseItem = EA_UseItem; + ea->EA_DropItem = EA_DropItem; + ea->EA_UseInv = EA_UseInv; + ea->EA_DropInv = EA_DropInv; + ea->EA_Gesture = EA_Gesture; + ea->EA_Command = EA_Command; + ea->EA_SelectWeapon = EA_SelectWeapon; + ea->EA_Talk = EA_Talk; + ea->EA_Attack = EA_Attack; + ea->EA_Reload = EA_Reload; + ea->EA_Use = EA_Use; + ea->EA_Respawn = EA_Respawn; + ea->EA_Jump = EA_Jump; + ea->EA_DelayedJump = EA_DelayedJump; + ea->EA_Crouch = EA_Crouch; + ea->EA_MoveUp = EA_MoveUp; + ea->EA_MoveDown = EA_MoveDown; + ea->EA_MoveForward = EA_MoveForward; + ea->EA_MoveBack = EA_MoveBack; + ea->EA_MoveLeft = EA_MoveLeft; + ea->EA_MoveRight = EA_MoveRight; + ea->EA_Move = EA_Move; + ea->EA_View = EA_View; + ea->EA_GetInput = EA_GetInput; + ea->EA_EndRegular = EA_EndRegular; + ea->EA_ResetInput = EA_ResetInput; +} + + +/* +============ +Init_AI_Export +============ +*/ +static void Init_AI_Export( ai_export_t *ai ) { + //----------------------------------- + // be_ai_char.h + //----------------------------------- + ai->BotLoadCharacter = BotLoadCharacter; + ai->BotFreeCharacter = BotFreeCharacter; + ai->Characteristic_Float = Characteristic_Float; + ai->Characteristic_BFloat = Characteristic_BFloat; + ai->Characteristic_Integer = Characteristic_Integer; + ai->Characteristic_BInteger = Characteristic_BInteger; + ai->Characteristic_String = Characteristic_String; + //----------------------------------- + // be_ai_chat.h + //----------------------------------- + ai->BotAllocChatState = BotAllocChatState; + ai->BotFreeChatState = BotFreeChatState; + ai->BotQueueConsoleMessage = BotQueueConsoleMessage; + ai->BotRemoveConsoleMessage = BotRemoveConsoleMessage; + ai->BotNextConsoleMessage = BotNextConsoleMessage; + ai->BotNumConsoleMessages = BotNumConsoleMessages; + ai->BotInitialChat = BotInitialChat; + ai->BotNumInitialChats = BotNumInitialChats; + ai->BotReplyChat = BotReplyChat; + ai->BotChatLength = BotChatLength; + ai->BotEnterChat = BotEnterChat; + ai->BotGetChatMessage = BotGetChatMessage; + ai->StringContains = StringContains; + ai->BotFindMatch = BotFindMatch; + ai->BotMatchVariable = BotMatchVariable; + ai->UnifyWhiteSpaces = UnifyWhiteSpaces; + ai->BotReplaceSynonyms = BotReplaceSynonyms; + ai->BotLoadChatFile = BotLoadChatFile; + ai->BotSetChatGender = BotSetChatGender; + ai->BotSetChatName = BotSetChatName; + //----------------------------------- + // be_ai_goal.h + //----------------------------------- + ai->BotResetGoalState = BotResetGoalState; + ai->BotResetAvoidGoals = BotResetAvoidGoals; + ai->BotRemoveFromAvoidGoals = BotRemoveFromAvoidGoals; + ai->BotPushGoal = BotPushGoal; + ai->BotPopGoal = BotPopGoal; + ai->BotEmptyGoalStack = BotEmptyGoalStack; + ai->BotDumpAvoidGoals = BotDumpAvoidGoals; + ai->BotDumpGoalStack = BotDumpGoalStack; + ai->BotGoalName = BotGoalName; + ai->BotGetTopGoal = BotGetTopGoal; + ai->BotGetSecondGoal = BotGetSecondGoal; + ai->BotChooseLTGItem = BotChooseLTGItem; + ai->BotChooseNBGItem = BotChooseNBGItem; + ai->BotTouchingGoal = BotTouchingGoal; + ai->BotItemGoalInVisButNotVisible = BotItemGoalInVisButNotVisible; + ai->BotGetLevelItemGoal = BotGetLevelItemGoal; + ai->BotGetNextCampSpotGoal = BotGetNextCampSpotGoal; + ai->BotGetMapLocationGoal = BotGetMapLocationGoal; + ai->BotAvoidGoalTime = BotAvoidGoalTime; + ai->BotInitLevelItems = BotInitLevelItems; + ai->BotUpdateEntityItems = BotUpdateEntityItems; + ai->BotLoadItemWeights = BotLoadItemWeights; + ai->BotFreeItemWeights = BotFreeItemWeights; + ai->BotInterbreedGoalFuzzyLogic = BotInterbreedGoalFuzzyLogic; + ai->BotSaveGoalFuzzyLogic = BotSaveGoalFuzzyLogic; + ai->BotMutateGoalFuzzyLogic = BotMutateGoalFuzzyLogic; + ai->BotAllocGoalState = BotAllocGoalState; + ai->BotFreeGoalState = BotFreeGoalState; + //----------------------------------- + // be_ai_move.h + //----------------------------------- + ai->BotResetMoveState = BotResetMoveState; + ai->BotMoveToGoal = BotMoveToGoal; + ai->BotMoveInDirection = BotMoveInDirection; + ai->BotResetAvoidReach = BotResetAvoidReach; + ai->BotResetLastAvoidReach = BotResetLastAvoidReach; + ai->BotReachabilityArea = BotReachabilityArea; + ai->BotMovementViewTarget = BotMovementViewTarget; + ai->BotPredictVisiblePosition = BotPredictVisiblePosition; + ai->BotAllocMoveState = BotAllocMoveState; + ai->BotFreeMoveState = BotFreeMoveState; + ai->BotInitMoveState = BotInitMoveState; + // Ridah + ai->BotInitAvoidReach = BotInitAvoidReach; + // done. + //----------------------------------- + // be_ai_weap.h + //----------------------------------- + ai->BotChooseBestFightWeapon = BotChooseBestFightWeapon; + ai->BotGetWeaponInfo = BotGetWeaponInfo; + ai->BotLoadWeaponWeights = BotLoadWeaponWeights; + ai->BotAllocWeaponState = BotAllocWeaponState; + ai->BotFreeWeaponState = BotFreeWeaponState; + ai->BotResetWeaponState = BotResetWeaponState; + //----------------------------------- + // be_ai_gen.h + //----------------------------------- + ai->GeneticParentsAndChildSelection = GeneticParentsAndChildSelection; +} + + +/* +============ +GetBotLibAPI +============ +*/ +botlib_export_t *GetBotLibAPI( int apiVersion, botlib_import_t *import ) { + botimport = *import; + + memset( &be_botlib_export, 0, sizeof( be_botlib_export ) ); + + if ( apiVersion != BOTLIB_API_VERSION ) { + botimport.Print( PRT_ERROR, "Mismatched BOTLIB_API_VERSION: expected %i, got %i\n", BOTLIB_API_VERSION, apiVersion ); + return NULL; + } + + Init_AAS_Export( &be_botlib_export.aas ); + Init_EA_Export( &be_botlib_export.ea ); + Init_AI_Export( &be_botlib_export.ai ); + + be_botlib_export.BotLibSetup = Export_BotLibSetup; + be_botlib_export.BotLibShutdown = Export_BotLibShutdown; + be_botlib_export.BotLibVarSet = Export_BotLibVarSet; + be_botlib_export.BotLibVarGet = Export_BotLibVarGet; + be_botlib_export.PC_AddGlobalDefine = PC_AddGlobalDefine; + be_botlib_export.PC_LoadSourceHandle = PC_LoadSourceHandle; + be_botlib_export.PC_FreeSourceHandle = PC_FreeSourceHandle; + be_botlib_export.PC_ReadTokenHandle = PC_ReadTokenHandle; + be_botlib_export.PC_SourceFileAndLine = PC_SourceFileAndLine; + + be_botlib_export.BotLibStartFrame = Export_BotLibStartFrame; + be_botlib_export.BotLibLoadMap = Export_BotLibLoadMap; + be_botlib_export.BotLibUpdateEntity = Export_BotLibUpdateEntity; + be_botlib_export.Test = BotExportTest; + + return &be_botlib_export; +} diff --git a/src/botlib/be_interface.h b/src/botlib/be_interface.h new file mode 100644 index 0000000..e2355ce --- /dev/null +++ b/src/botlib/be_interface.h @@ -0,0 +1,93 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: be_interface.h + * + * desc: botlib interface + * + * + *****************************************************************************/ + +/* +"Do not go where the path leads, rather go where there's no track and leave a trail." + +"AAS (Area Awareness System)" + +"Part of the Gladiator is BoGuS (Bot Guidance System)" + +"ANSI (Advanced Navigational System Interface)" + +"to make things work the only thing you really have to do is think things work." + +"a madman is just someone living in another reality which isn't shared among many people" +*/ + +//#define DEBUG //debug code +#define RANDOMIZE //randomize bot behaviour +#if defined( WIN32 ) || defined( _WIN32 ) +#define AASZIP //allow reading directly from aasX.zip files +#endif +#define QUAKE2 //bot for Quake2 +//#define HALFLIFE //bot for Half-Life + +//========================================================== +// +// global variable structures +// +//========================================================== + +//FIXME: get rid of this global structure +typedef struct botlib_globals_s +{ + int botlibsetup; //true when the bot library has been setup + int maxentities; //maximum number of entities + int maxclients; //maximum number of clients + float time; //the global time +#ifdef DEBUG + qboolean debug; //true if debug is on + int goalareanum; + vec3_t goalorigin; + int runai; +#endif +} botlib_globals_t; + +//========================================================== +// +// global variables +// +//========================================================== + +extern botlib_globals_t botlibglobals; +extern botlib_import_t botimport; +extern int bot_developer; //true if developer is on + +// +int Sys_MilliSeconds( void ); + diff --git a/src/botlib/botlib.h b/src/botlib/botlib.h new file mode 100644 index 0000000..ea802a5 --- /dev/null +++ b/src/botlib/botlib.h @@ -0,0 +1,512 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: botlib.h +// Function: bot AI Library +// Programmer: Mr Elusive (MrElusive@idsoftware.com) +// Last update: 1999-08-18 +// Tab Size: 3 +//=========================================================================== + +#define BOTLIB_API_VERSION 2 + +struct aas_clientmove_s; +struct aas_entityinfo_s; +struct bot_consolemessage_s; +struct bot_match_s; +struct bot_goal_s; +struct bot_moveresult_s; +struct bot_initmove_s; +struct weaponinfo_s; + + +//debug line colors +#define LINECOLOR_NONE -1 +#define LINECOLOR_RED 1 //0xf2f2f0f0L +#define LINECOLOR_GREEN 2 //0xd0d1d2d3L +#define LINECOLOR_BLUE 3 //0xf3f3f1f1L +#define LINECOLOR_YELLOW 4 //0xdcdddedfL +#define LINECOLOR_ORANGE 5 //0xe0e1e2e3L + +//Print types +#define PRT_MESSAGE 1 +#define PRT_WARNING 2 +#define PRT_ERROR 3 +#define PRT_FATAL 4 +#define PRT_EXIT 5 + +//console message types +#define CMS_NORMAL 0 +#define CMS_CHAT 1 + +//botlib error codes +#define BLERR_NOERROR 0 //no error +#define BLERR_LIBRARYNOTSETUP 1 //library not setup +#define BLERR_LIBRARYALREADYSETUP 2 //BotSetupLibrary: library already setup +#define BLERR_INVALIDCLIENTNUMBER 3 //invalid client number +#define BLERR_INVALIDENTITYNUMBER 4 //invalid entity number +#define BLERR_NOAASFILE 5 //BotLoadMap: no AAS file available +#define BLERR_CANNOTOPENAASFILE 6 //BotLoadMap: cannot open AAS file +#define BLERR_CANNOTSEEKTOAASFILE 7 //BotLoadMap: cannot seek to AAS file +#define BLERR_CANNOTREADAASHEADER 8 //BotLoadMap: cannot read AAS header +#define BLERR_WRONGAASFILEID 9 //BotLoadMap: incorrect AAS file id +#define BLERR_WRONGAASFILEVERSION 10 //BotLoadMap: incorrect AAS file version +#define BLERR_CANNOTREADAASLUMP 11 //BotLoadMap: cannot read AAS file lump +#define BLERR_NOBSPFILE 12 //BotLoadMap: no BSP file available +#define BLERR_CANNOTOPENBSPFILE 13 //BotLoadMap: cannot open BSP file +#define BLERR_CANNOTSEEKTOBSPFILE 14 //BotLoadMap: cannot seek to BSP file +#define BLERR_CANNOTREADBSPHEADER 15 //BotLoadMap: cannot read BSP header +#define BLERR_WRONGBSPFILEID 16 //BotLoadMap: incorrect BSP file id +#define BLERR_WRONGBSPFILEVERSION 17 //BotLoadMap: incorrect BSP file version +#define BLERR_CANNOTREADBSPLUMP 18 //BotLoadMap: cannot read BSP file lump +#define BLERR_AICLIENTNOTSETUP 19 //BotAI: client not setup +#define BLERR_AICLIENTALREADYSETUP 20 //BotSetupClient: client already setup +#define BLERR_AIMOVEINACTIVECLIENT 21 //BotMoveClient: cannot move inactive client +#define BLERR_AIMOVETOACTIVECLIENT 22 //BotMoveClient: cannot move to active client +#define BLERR_AICLIENTALREADYSHUTDOWN 23 //BotShutdownClient: client not setup +#define BLERR_AIUPDATEINACTIVECLIENT 24 //BotUpdateClient: called for inactive client +#define BLERR_AICMFORINACTIVECLIENT 25 //BotConsoleMessage: called for inactive client +#define BLERR_SETTINGSINACTIVECLIENT 26 //BotClientSettings: called for inactive client +#define BLERR_CANNOTLOADICHAT 27 //BotSetupClient: cannot load initial chats +#define BLERR_CANNOTLOADITEMWEIGHTS 28 //BotSetupClient: cannot load item weights +#define BLERR_CANNOTLOADITEMCONFIG 29 //BotSetupLibrary: cannot load item config +#define BLERR_CANNOTLOADWEAPONWEIGHTS 30 //BotSetupClient: cannot load weapon weights +#define BLERR_CANNOTLOADWEAPONCONFIG 31 //BotSetupLibrary: cannot load weapon config +#define BLERR_INVALIDSOUNDINDEX 32 //BotAddSound: invalid sound index value + +//action flags +#define ACTION_ATTACK 1 +#define ACTION_USE 2 +#define ACTION_RESPAWN 4 +#define ACTION_JUMP 8 +#define ACTION_MOVEUP 8 +#define ACTION_CROUCH 16 +#define ACTION_MOVEDOWN 16 +#define ACTION_MOVEFORWARD 32 +#define ACTION_MOVEBACK 64 +#define ACTION_MOVELEFT 128 +#define ACTION_MOVERIGHT 256 +#define ACTION_DELAYEDJUMP 512 +#define ACTION_TALK 1024 +#define ACTION_GESTURE 2048 +#define ACTION_WALK 4096 + +//the bot input, will be converted to an usercmd_t +typedef struct bot_input_s +{ + float thinktime; //time since last output (in seconds) + vec3_t dir; //movement direction + float speed; //speed in the range [0, 400] + vec3_t viewangles; //the view angles + int actionflags; //one of the ACTION_? flags + int weapon; //weapon to use +} bot_input_t; + +#ifndef BSPTRACE + +//bsp_trace_t hit surface +typedef struct bsp_surface_s +{ + char name[16]; + int flags; + int value; +} bsp_surface_t; + +//remove the bsp_trace_s structure definition l8r on +//a trace is returned when a box is swept through the world +typedef struct bsp_trace_s +{ + qboolean allsolid; // if true, plane is not valid + qboolean startsolid; // if true, the initial point was in a solid area + float fraction; // time completed, 1.0 = didn't hit anything + vec3_t endpos; // final position + cplane_t plane; // surface normal at impact + float exp_dist; // expanded plane distance + int sidenum; // number of the brush side hit + bsp_surface_t surface; // the hit point surface + int contents; // contents on other side of surface hit + int ent; // number of entity hit +} bsp_trace_t; + +#define BSPTRACE +#endif // BSPTRACE + +//entity state +typedef struct bot_entitystate_s +{ + int type; // entity type + int flags; // entity flags + vec3_t origin; // origin of the entity + vec3_t angles; // angles of the model + vec3_t old_origin; // for lerping + vec3_t mins; // bounding box minimums + vec3_t maxs; // bounding box maximums + int groundent; // ground entity + int solid; // solid type + int modelindex; // model used + int modelindex2; // weapons, CTF flags, etc + int frame; // model frame number + int event; // impulse events -- muzzle flashes, footsteps, etc + int eventParm; // even parameter + int powerups; // bit flags + int weapon; // determines weapon and flash model, etc + int legsAnim; // mask off ANIM_TOGGLEBIT + int torsoAnim; // mask off ANIM_TOGGLEBIT +// int weapAnim; // mask off ANIM_TOGGLEBIT //----(SA) added +//----(SA) didn't want to comment in as I wasn't sure of any implications of changing this structure. +} bot_entitystate_t; + +//bot AI library exported functions +typedef struct botlib_import_s +{ + //print messages from the bot library + void ( QDECL * Print )( int type, char *fmt, ... ); + //trace a bbox through the world + void ( *Trace )( bsp_trace_t *trace, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int passent, int contentmask ); + //trace a bbox against a specific entity + void ( *EntityTrace )( bsp_trace_t *trace, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int entnum, int contentmask ); + //retrieve the contents at the given point + int ( *PointContents )( vec3_t point ); + //check if the point is in potential visible sight + int ( *inPVS )( vec3_t p1, vec3_t p2 ); + //retrieve the BSP entity data lump + char *( *BSPEntityData )( void ); + // + void ( *BSPModelMinsMaxsOrigin )( int modelnum, vec3_t angles, vec3_t mins, vec3_t maxs, vec3_t origin ); + //send a bot client command + void ( *BotClientCommand )( int client, char *command ); + //memory allocation + void *( *GetMemory )( int size ); + void ( *FreeMemory )( void *ptr ); + void ( *FreeZoneMemory )( void ); + void *( *HunkAlloc )( int size ); + //file system access + int ( *FS_FOpenFile )( const char *qpath, fileHandle_t *file, fsMode_t mode ); + int ( *FS_Read )( void *buffer, int len, fileHandle_t f ); + int ( *FS_Write )( const void *buffer, int len, fileHandle_t f ); + void ( *FS_FCloseFile )( fileHandle_t f ); + int ( *FS_Seek )( fileHandle_t f, long offset, int origin ); + //debug visualisation stuff + int ( *DebugLineCreate )( void ); + void ( *DebugLineDelete )( int line ); + void ( *DebugLineShow )( int line, vec3_t start, vec3_t end, int color ); + // + int ( *DebugPolygonCreate )( int color, int numPoints, vec3_t *points ); + void ( *DebugPolygonDelete )( int id ); + // + // Ridah, Cast AI stuff + qboolean ( *AICast_VisibleFromPos )( vec3_t srcpos, int srcnum, + vec3_t destpos, int destnum, qboolean updateVisPos ); + qboolean ( *AICast_CheckAttackAtPos )( int entnum, int enemy, vec3_t pos, qboolean ducking, qboolean allowHitWorld ); + // done. +} botlib_import_t; + +typedef struct aas_export_s +{ + //----------------------------------- + // be_aas_entity.h + //----------------------------------- + void ( *AAS_EntityInfo )( int entnum, struct aas_entityinfo_s *info ); + //----------------------------------- + // be_aas_main.h + //----------------------------------- + int ( *AAS_Initialized )( void ); + void ( *AAS_PresenceTypeBoundingBox )( int presencetype, vec3_t mins, vec3_t maxs ); + float ( *AAS_Time )( void ); + //-------------------------------------------- + // be_aas_sample.c + //-------------------------------------------- + int ( *AAS_PointAreaNum )( vec3_t point ); + int ( *AAS_TraceAreas )( vec3_t start, vec3_t end, int *areas, vec3_t *points, int maxareas ); + //-------------------------------------------- + // be_aas_bspq3.c + //-------------------------------------------- + int ( *AAS_PointContents )( vec3_t point ); + int ( *AAS_NextBSPEntity )( int ent ); + int ( *AAS_ValueForBSPEpairKey )( int ent, char *key, char *value, int size ); + int ( *AAS_VectorForBSPEpairKey )( int ent, char *key, vec3_t v ); + int ( *AAS_FloatForBSPEpairKey )( int ent, char *key, float *value ); + int ( *AAS_IntForBSPEpairKey )( int ent, char *key, int *value ); + //-------------------------------------------- + // be_aas_reach.c + //-------------------------------------------- + int ( *AAS_AreaReachability )( int areanum ); + //-------------------------------------------- + // be_aas_route.c + //-------------------------------------------- + int ( *AAS_AreaTravelTimeToGoalArea )( int areanum, vec3_t origin, int goalareanum, int travelflags ); + //-------------------------------------------- + // be_aas_move.c + //-------------------------------------------- + int ( *AAS_Swimming )( vec3_t origin ); + int ( *AAS_PredictClientMovement )( struct aas_clientmove_s *move, + int entnum, vec3_t origin, + int presencetype, int onground, + vec3_t velocity, vec3_t cmdmove, + int cmdframes, + int maxframes, float frametime, + int stopevent, int stopareanum, int visualize ); + + // Ridah, route-tables + //-------------------------------------------- + // be_aas_routetable.c + //-------------------------------------------- + void ( *AAS_RT_ShowRoute )( vec3_t srcpos, int srcnum, int destnum ); + qboolean ( *AAS_RT_GetHidePos )( vec3_t srcpos, int srcnum, int srcarea, vec3_t destpos, int destnum, int destarea, vec3_t returnPos ); + int ( *AAS_FindAttackSpotWithinRange )( int srcnum, int rangenum, int enemynum, float rangedist, int travelflags, float *outpos ); + void ( *AAS_SetAASBlockingEntity )( vec3_t absmin, vec3_t absmax, qboolean blocking ); + // done. + + // Ridah + void ( *AAS_SetCurrentWorld )( int index ); + // done. + +} aas_export_t; + +typedef struct ea_export_s +{ + //ClientCommand elementary actions + void ( *EA_Say )( int client, char *str ); + void ( *EA_SayTeam )( int client, char *str ); + void ( *EA_UseItem )( int client, char *it ); + void ( *EA_DropItem )( int client, char *it ); + void ( *EA_UseInv )( int client, char *inv ); + void ( *EA_DropInv )( int client, char *inv ); + void ( *EA_Gesture )( int client ); + void ( *EA_Command )( int client, char *command ); + //regular elementary actions + void ( *EA_SelectWeapon )( int client, int weapon ); + void ( *EA_Talk )( int client ); + void ( *EA_Attack )( int client ); + void ( *EA_Use )( int client ); + void ( *EA_Respawn )( int client ); + void ( *EA_Jump )( int client ); + void ( *EA_DelayedJump )( int client ); + void ( *EA_Crouch )( int client ); + void ( *EA_MoveUp )( int client ); + void ( *EA_MoveDown )( int client ); + void ( *EA_MoveForward )( int client ); + void ( *EA_MoveBack )( int client ); + void ( *EA_MoveLeft )( int client ); + void ( *EA_MoveRight )( int client ); + void ( *EA_Move )( int client, vec3_t dir, float speed ); + void ( *EA_View )( int client, vec3_t viewangles ); + //send regular input to the server + void ( *EA_EndRegular )( int client, float thinktime ); + void ( *EA_GetInput )( int client, float thinktime, bot_input_t *input ); + void ( *EA_ResetInput )( int client, bot_input_t *init ); +} ea_export_t; + +typedef struct ai_export_s +{ + //----------------------------------- + // be_ai_char.h + //----------------------------------- + int ( *BotLoadCharacter )( char *charfile, int skill ); + void ( *BotFreeCharacter )( int character ); + float ( *Characteristic_Float )( int character, int index ); + float ( *Characteristic_BFloat )( int character, int index, float min, float max ); + int ( *Characteristic_Integer )( int character, int index ); + int ( *Characteristic_BInteger )( int character, int index, int min, int max ); + void ( *Characteristic_String )( int character, int index, char *buf, int size ); + //----------------------------------- + // be_ai_chat.h + //----------------------------------- + int ( *BotAllocChatState )( void ); + void ( *BotFreeChatState )( int handle ); + void ( *BotQueueConsoleMessage )( int chatstate, int type, char *message ); + void ( *BotRemoveConsoleMessage )( int chatstate, int handle ); + int ( *BotNextConsoleMessage )( int chatstate, struct bot_consolemessage_s *cm ); + int ( *BotNumConsoleMessages )( int chatstate ); + void ( *BotInitialChat )( int chatstate, char *type, int mcontext, char *var0, char *var1, char *var2, char *var3, char *var4, char *var5, char *var6, char *var7 ); + int ( *BotNumInitialChats )( int chatstate, char *type ); + int ( *BotReplyChat )( int chatstate, char *message, int mcontext, int vcontext, char *var0, char *var1, char *var2, char *var3, char *var4, char *var5, char *var6, char *var7 ); + int ( *BotChatLength )( int chatstate ); + void ( *BotEnterChat )( int chatstate, int client, int sendto ); + void ( *BotGetChatMessage )( int chatstate, char *buf, int size ); + int ( *StringContains )( char *str1, char *str2, int casesensitive ); + int ( *BotFindMatch )( char *str, struct bot_match_s *match, unsigned long int context ); + void ( *BotMatchVariable )( struct bot_match_s *match, int variable, char *buf, int size ); + void ( *UnifyWhiteSpaces )( char *string ); + void ( *BotReplaceSynonyms )( char *string, unsigned long int context ); + int ( *BotLoadChatFile )( int chatstate, char *chatfile, char *chatname ); + void ( *BotSetChatGender )( int chatstate, int gender ); + void ( *BotSetChatName )( int chatstate, char *name ); + //----------------------------------- + // be_ai_goal.h + //----------------------------------- + void ( *BotResetGoalState )( int goalstate ); + void ( *BotResetAvoidGoals )( int goalstate ); + void ( *BotRemoveFromAvoidGoals )( int goalstate, int number ); + void ( *BotPushGoal )( int goalstate, struct bot_goal_s *goal ); + void ( *BotPopGoal )( int goalstate ); + void ( *BotEmptyGoalStack )( int goalstate ); + void ( *BotDumpAvoidGoals )( int goalstate ); + void ( *BotDumpGoalStack )( int goalstate ); + void ( *BotGoalName )( int number, char *name, int size ); + int ( *BotGetTopGoal )( int goalstate, struct bot_goal_s *goal ); + int ( *BotGetSecondGoal )( int goalstate, struct bot_goal_s *goal ); + int ( *BotChooseLTGItem )( int goalstate, vec3_t origin, int *inventory, int travelflags ); + int ( *BotChooseNBGItem )( int goalstate, vec3_t origin, int *inventory, int travelflags, + struct bot_goal_s *ltg, float maxtime ); + int ( *BotTouchingGoal )( vec3_t origin, struct bot_goal_s *goal ); + int ( *BotItemGoalInVisButNotVisible )( int viewer, vec3_t eye, vec3_t viewangles, struct bot_goal_s *goal ); + int ( *BotGetLevelItemGoal )( int index, char *classname, struct bot_goal_s *goal ); + int ( *BotGetNextCampSpotGoal )( int num, struct bot_goal_s *goal ); + int ( *BotGetMapLocationGoal )( char *name, struct bot_goal_s *goal ); + float ( *BotAvoidGoalTime )( int goalstate, int number ); + void ( *BotInitLevelItems )( void ); + void ( *BotUpdateEntityItems )( void ); + int ( *BotLoadItemWeights )( int goalstate, char *filename ); + void ( *BotFreeItemWeights )( int goalstate ); + void ( *BotInterbreedGoalFuzzyLogic )( int parent1, int parent2, int child ); + void ( *BotSaveGoalFuzzyLogic )( int goalstate, char *filename ); + void ( *BotMutateGoalFuzzyLogic )( int goalstate, float range ); + int ( *BotAllocGoalState )( int client ); + void ( *BotFreeGoalState )( int handle ); + //----------------------------------- + // be_ai_move.h + //----------------------------------- + void ( *BotResetMoveState )( int movestate ); + void ( *BotMoveToGoal )( struct bot_moveresult_s *result, int movestate, struct bot_goal_s *goal, int travelflags ); + int ( *BotMoveInDirection )( int movestate, vec3_t dir, float speed, int type ); + void ( *BotResetAvoidReach )( int movestate ); + void ( *BotResetLastAvoidReach )( int movestate ); + int ( *BotReachabilityArea )( vec3_t origin, int testground ); + int ( *BotMovementViewTarget )( int movestate, struct bot_goal_s *goal, int travelflags, float lookahead, vec3_t target ); + int ( *BotPredictVisiblePosition )( vec3_t origin, int areanum, struct bot_goal_s *goal, int travelflags, vec3_t target ); + int ( *BotAllocMoveState )( void ); + void ( *BotFreeMoveState )( int handle ); + void ( *BotInitMoveState )( int handle, struct bot_initmove_s *initmove ); + // Ridah + void ( *BotInitAvoidReach )( int handle ); + // done. + //----------------------------------- + // be_ai_weap.h + //----------------------------------- + int ( *BotChooseBestFightWeapon )( int weaponstate, int *inventory ); + void ( *BotGetWeaponInfo )( int weaponstate, int weapon, struct weaponinfo_s *weaponinfo ); + int ( *BotLoadWeaponWeights )( int weaponstate, char *filename ); + int ( *BotAllocWeaponState )( void ); + void ( *BotFreeWeaponState )( int weaponstate ); + void ( *BotResetWeaponState )( int weaponstate ); + //----------------------------------- + // be_ai_gen.h + //----------------------------------- + int ( *GeneticParentsAndChildSelection )( int numranks, float *ranks, int *parent1, int *parent2, int *child ); +} ai_export_t; + +//bot AI library imported functions +typedef struct botlib_export_s +{ + //Area Awareness System functions + aas_export_t aas; + //Elementary Action functions + ea_export_t ea; + //AI functions + ai_export_t ai; + //setup the bot library, returns BLERR_ + int ( *BotLibSetup )( void ); + //shutdown the bot library, returns BLERR_ + int ( *BotLibShutdown )( void ); + //sets a library variable returns BLERR_ + int ( *BotLibVarSet )( char *var_name, char *value ); + //gets a library variable returns BLERR_ + int ( *BotLibVarGet )( char *var_name, char *value, int size ); + //sets a C-like define returns BLERR_ + int ( *BotLibDefine )( char *string ); + //start a frame in the bot library + int ( *BotLibStartFrame )( float time ); + //load a new map in the bot library + int ( *BotLibLoadMap )( const char *mapname ); + //entity updates + int ( *BotLibUpdateEntity )( int ent, bot_entitystate_t *state ); + //just for testing + int ( *Test )( int parm0, char *parm1, vec3_t parm2, vec3_t parm3 ); +} botlib_export_t; + +//linking of bot library +botlib_export_t *GetBotLibAPI( int apiVersion, botlib_import_t *import ); + +/* Library variables: + +name: default: module(s): description: + +"basedir" "" l_utils.c Quake2 base directory +"gamedir" "" l_utils.c Quake2 game directory +"cddir" "" l_utils.c Quake2 CD directory + +"autolaunchbspc" "0" be_aas_load.c automatically launch (Win)BSPC +"log" "0" l_log.c enable/disable creating a log file +"maxclients" "4" be_interface.c maximum number of clients +"maxentities" "1024" be_interface.c maximum number of entities + +"sv_friction" "6" be_aas_move.c ground friction +"sv_stopspeed" "100" be_aas_move.c stop speed +"sv_gravity" "800" be_aas_move.c gravity value +"sv_waterfriction" "1" be_aas_move.c water friction +"sv_watergravity" "400" be_aas_move.c gravity in water +"sv_maxvelocity" "300" be_aas_move.c maximum velocity +"sv_maxwalkvelocity" "300" be_aas_move.c maximum walk velocity +"sv_maxcrouchvelocity" "100" be_aas_move.c maximum crouch velocity +"sv_maxswimvelocity" "150" be_aas_move.c maximum swim velocity +"sv_walkaccelerate" "10" be_aas_move.c walk acceleration +"sv_airaccelerate" "1" be_aas_move.c air acceleration +"sv_swimaccelerate" "4" be_aas_move.c swim acceleration +"sv_maxstep" "18" be_aas_move.c maximum step height +"sv_maxbarrier" "32" be_aas_move.c maximum barrier height +"sv_maxsteepness" "0.7" be_aas_move.c maximum floor steepness +"sv_jumpvel" "270" be_aas_move.c jump z velocity +"sv_maxwaterjump" "20" be_aas_move.c maximum waterjump height + +"max_aaslinks" "4096" be_aas_sample.c maximum links in the AAS +"max_bsplinks" "4096" be_aas_bsp.c maximum links in the BSP + +"notspawnflags" "2048" be_ai_goal.c entities with these spawnflags will be removed +"itemconfig" "items.c" be_ai_goal.c item configuration file +"weaponconfig" "weapons.c" be_ai_weap.c weapon configuration file +"synfile" "syn.c" be_ai_chat.c file with synonyms +"rndfile" "rnd.c" be_ai_chat.c file with random strings +"matchfile" "match.c" be_ai_chat.c file with match strings +"max_messages" "1024" be_ai_chat.c console message heap size +"max_weaponinfo" "32" be_ai_weap.c maximum number of weapon info +"max_projectileinfo" "32" be_ai_weap.c maximum number of projectile info +"max_iteminfo" "256" be_ai_goal.c maximum number of item info +"max_levelitems" "256" be_ai_goal.c maximum number of level items +"framereachability" "" be_aas_reach.c number of reachabilities to calucate per frame +"forceclustering" "0" be_aas_main.c force recalculation of clusters +"forcereachability" "0" be_aas_main.c force recalculation of reachabilities +"forcewrite" "0" be_aas_main.c force writing of aas file +"nooptimize" "0" be_aas_main.c no aas optimization + +"laserhook" "0" be_ai_move.c 0 = CTF hook, 1 = laser hook + +*/ + diff --git a/src/botlib/botlib.vcproj b/src/botlib/botlib.vcproj new file mode 100644 index 0000000..5add92a --- /dev/null +++ b/src/botlib/botlib.vcproj @@ -0,0 +1,911 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/botlib/l_crc.c b/src/botlib/l_crc.c new file mode 100644 index 0000000..d76f461 --- /dev/null +++ b/src/botlib/l_crc.c @@ -0,0 +1,155 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: l_crc.c + * + * desc: CRC calculation + * + * + *****************************************************************************/ + +#include +#include +#include + +#include "../game/q_shared.h" +#include "../game/botlib.h" +#include "be_interface.h" //for botimport.Print + + +// FIXME: byte swap? + +// this is a 16 bit, non-reflected CRC using the polynomial 0x1021 +// and the initial and final xor values shown below... in other words, the +// CCITT standard CRC used by XMODEM + +#define CRC_INIT_VALUE 0xffff +#define CRC_XOR_VALUE 0x0000 + +unsigned short crctable[257] = +{ + 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, + 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, + 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, + 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, + 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, + 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, + 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, + 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, + 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, + 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, + 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, + 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, + 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, + 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, + 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, + 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, + 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, + 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, + 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, + 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, + 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, + 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, + 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, + 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, + 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, + 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, + 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, + 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, + 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, + 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, + 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, + 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0, + 0x0000 // because process string allows value 256 through and it was undefined +}; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void CRC_Init( unsigned short *crcvalue ) { + *crcvalue = CRC_INIT_VALUE; +} //end of the function CRC_Init +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void CRC_ProcessByte( unsigned short *crcvalue, byte data ) { + *crcvalue = ( *crcvalue << 8 ) ^ crctable[( *crcvalue >> 8 ) ^ data]; +} //end of the function CRC_ProcessByte +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +unsigned short CRC_Value( unsigned short crcvalue ) { + return crcvalue ^ CRC_XOR_VALUE; +} //end of the function CRC_Value +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +unsigned short CRC_ProcessString( unsigned char *data, int length ) { + unsigned short crcvalue; + int i, ind; + + CRC_Init( &crcvalue ); + + for ( i = 0; i < length; i++ ) + { + ind = ( crcvalue >> 8 ) ^ data[i]; + if ( ind < 0 || ind > 256 ) { + ind = 0; + } + crcvalue = ( crcvalue << 8 ) ^ crctable[ind]; + } //end for + return CRC_Value( crcvalue ); +} //end of the function CRC_ProcessString +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void CRC_ContinueProcessString( unsigned short *crc, char *data, int length ) { + int i; + + for ( i = 0; i < length; i++ ) + { + *crc = ( *crc << 8 ) ^ crctable[( *crc >> 8 ) ^ data[i]]; + } //end for +} //end of the function CRC_ProcessString diff --git a/src/botlib/l_crc.h b/src/botlib/l_crc.h new file mode 100644 index 0000000..287289a --- /dev/null +++ b/src/botlib/l_crc.h @@ -0,0 +1,44 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: l_crc.h +// Function: for CRC checks +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-31 +// Tab Size: 3 +//=========================================================================== + +typedef unsigned short crc_t; + +void CRC_Init( unsigned short *crcvalue ); +void CRC_ProcessByte( unsigned short *crcvalue, byte data ); +unsigned short CRC_Value( unsigned short crcvalue ); +unsigned short CRC_ProcessString( unsigned char *data, int length ); +void CRC_ContinueProcessString( unsigned short *crc, char *data, int length ); diff --git a/src/botlib/l_libvar.c b/src/botlib/l_libvar.c new file mode 100644 index 0000000..99c0fe7 --- /dev/null +++ b/src/botlib/l_libvar.c @@ -0,0 +1,282 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: l_libvar.c + * + * desc: bot library variables + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "l_memory.h" +#include "l_libvar.h" + +//list with library variables +libvar_t *libvarlist; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float LibVarStringValue( char *string ) { + int dotfound = 0; + float value = 0; + + while ( *string ) + { + if ( *string < '0' || *string > '9' ) { + if ( dotfound || *string != '.' ) { + return 0; + } //end if + else + { + dotfound = 10; + string++; + } //end if + } //end if + if ( dotfound ) { + value = value + (float) ( *string - '0' ) / (float) dotfound; + dotfound *= 10; + } //end if + else + { + value = value * 10.0 + (float) ( *string - '0' ); + } //end else + string++; + } //end while + return value; +} //end of the function LibVarStringValue +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +libvar_t *LibVarAlloc( char *var_name ) { + libvar_t *v; + + v = (libvar_t *) GetMemory( sizeof( libvar_t ) + strlen( var_name ) + 1 ); + memset( v, 0, sizeof( libvar_t ) ); + v->name = (char *) v + sizeof( libvar_t ); + strcpy( v->name, var_name ); + //add the variable in the list + v->next = libvarlist; + libvarlist = v; + return v; +} //end of the function LibVarAlloc +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void LibVarDeAlloc( libvar_t *v ) { + if ( v->string ) { + FreeMemory( v->string ); + } + FreeMemory( v ); +} //end of the function LibVarDeAlloc +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void LibVarDeAllocAll( void ) { + libvar_t *v; + + for ( v = libvarlist; v; v = libvarlist ) + { + libvarlist = libvarlist->next; + LibVarDeAlloc( v ); + } //end for + libvarlist = NULL; +} //end of the function LibVarDeAllocAll +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +libvar_t *LibVarGet( char *var_name ) { + libvar_t *v; + + for ( v = libvarlist; v; v = v->next ) + { + if ( !Q_stricmp( v->name, var_name ) ) { + return v; + } //end if + } //end for + return NULL; +} //end of the function LibVarGet +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +char *LibVarGetString( char *var_name ) { + libvar_t *v; + + v = LibVarGet( var_name ); + if ( v ) { + return v->string; + } //end if + else + { + return ""; + } //end else +} //end of the function LibVarGetString +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float LibVarGetValue( char *var_name ) { + libvar_t *v; + + v = LibVarGet( var_name ); + if ( v ) { + return v->value; + } //end if + else + { + return 0; + } //end else +} //end of the function LibVarGetValue +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +libvar_t *LibVar( char *var_name, char *value ) { + libvar_t *v; + v = LibVarGet( var_name ); + if ( v ) { + return v; + } + //create new variable + v = LibVarAlloc( var_name ); + //variable string + v->string = (char *) GetMemory( strlen( value ) + 1 ); + strcpy( v->string, value ); + //the value + v->value = LibVarStringValue( v->string ); + //variable is modified + v->modified = qtrue; + // + return v; +} //end of the function LibVar +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +char *LibVarString( char *var_name, char *value ) { + libvar_t *v; + + v = LibVar( var_name, value ); + return v->string; +} //end of the function LibVarString +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float LibVarValue( char *var_name, char *value ) { + libvar_t *v; + + v = LibVar( var_name, value ); + return v->value; +} //end of the function LibVarValue +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void LibVarSet( char *var_name, char *value ) { + libvar_t *v; + + v = LibVarGet( var_name ); + if ( v ) { + FreeMemory( v->string ); + } //end if + else + { + v = LibVarAlloc( var_name ); + } //end else + //variable string + v->string = (char *) GetMemory( strlen( value ) + 1 ); + strcpy( v->string, value ); + //the value + v->value = LibVarStringValue( v->string ); + //variable is modified + v->modified = qtrue; +} //end of the function LibVarSet +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean LibVarChanged( char *var_name ) { + libvar_t *v; + + v = LibVarGet( var_name ); + if ( v ) { + return v->modified; + } //end if + else + { + return qfalse; + } //end else +} //end of the function LibVarChanged +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void LibVarSetNotModified( char *var_name ) { + libvar_t *v; + + v = LibVarGet( var_name ); + if ( v ) { + v->modified = qfalse; + } //end if +} //end of the function LibVarSetNotModified diff --git a/src/botlib/l_libvar.h b/src/botlib/l_libvar.h new file mode 100644 index 0000000..cd04894 --- /dev/null +++ b/src/botlib/l_libvar.h @@ -0,0 +1,69 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: l_libvar.h + * + * desc: botlib vars + * + * + *****************************************************************************/ + +//library variable +typedef struct libvar_s +{ + char *name; + char *string; + int flags; + qboolean modified; // set each time the cvar is changed + float value; + struct libvar_s *next; +} libvar_t; + +//removes all library variables +void LibVarDeAllocAll( void ); +//gets the library variable with the given name +libvar_t *LibVarGet( char *var_name ); +//gets the string of the library variable with the given name +char *LibVarGetString( char *var_name ); +//gets the value of the library variable with the given name +float LibVarGetValue( char *var_name ); +//creates the library variable if not existing already and returns it +libvar_t *LibVar( char *var_name, char *value ); +//creates the library variable if not existing already and returns the value +float LibVarValue( char *var_name, char *value ); +//creates the library variable if not existing already and returns the value string +char *LibVarString( char *var_name, char *value ); +//sets the library variable +void LibVarSet( char *var_name, char *value ); +//returns true if the library variable has been modified +qboolean LibVarChanged( char *var_name ); +//sets the library variable to unmodified +void LibVarSetNotModified( char *var_name ); + diff --git a/src/botlib/l_log.c b/src/botlib/l_log.c new file mode 100644 index 0000000..3ad98ad --- /dev/null +++ b/src/botlib/l_log.c @@ -0,0 +1,186 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: l_log.c + * + * desc: log file + * + * + *****************************************************************************/ + +#include +#include +#include + +#include "../game/q_shared.h" +#include "../game/botlib.h" +#include "be_interface.h" //for botimport.Print +#include "l_libvar.h" + +#define MAX_LOGFILENAMESIZE 1024 + +typedef struct logfile_s +{ + char filename[MAX_LOGFILENAMESIZE]; + FILE *fp; + int numwrites; +} logfile_t; + +static logfile_t logfile; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Log_AlwaysOpen( char *filename ) { + if ( !filename || !strlen( filename ) ) { + botimport.Print( PRT_MESSAGE, "openlog \n" ); + return; + } //end if + if ( logfile.fp ) { + botimport.Print( PRT_ERROR, "log file %s is already opened\n", logfile.filename ); + return; + } //end if + logfile.fp = fopen( filename, "wb" ); + if ( !logfile.fp ) { + botimport.Print( PRT_ERROR, "can't open the log file %s\n", filename ); + return; + } //end if + strncpy( logfile.filename, filename, MAX_LOGFILENAMESIZE ); + botimport.Print( PRT_MESSAGE, "Opened log %s\n", logfile.filename ); +} //end of the function Log_Create +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Log_Open( char *filename ) { + if ( !LibVarValue( "log", "0" ) ) { + return; + } + Log_AlwaysOpen( filename ); + +} +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Log_Close( void ) { + if ( !logfile.fp ) { + return; + } + if ( fclose( logfile.fp ) ) { + botimport.Print( PRT_ERROR, "can't close log file %s\n", logfile.filename ); + return; + } //end if + logfile.fp = NULL; + botimport.Print( PRT_MESSAGE, "Closed log %s\n", logfile.filename ); +} //end of the function Log_Close +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Log_Shutdown( void ) { + if ( logfile.fp ) { + Log_Close(); + } +} //end of the function Log_Shutdown +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void QDECL Log_Write( char *fmt, ... ) { + va_list ap; + + if ( !logfile.fp ) { + return; + } + va_start( ap, fmt ); + vfprintf( logfile.fp, fmt, ap ); + va_end( ap ); + //fprintf(logfile.fp, "\r\n"); + fflush( logfile.fp ); +} //end of the function Log_Write +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void QDECL Log_WriteTimeStamped( char *fmt, ... ) { + va_list ap; + + if ( !logfile.fp ) { + return; + } + fprintf( logfile.fp, "%d %02d:%02d:%02d:%02d ", + logfile.numwrites, + (int) ( botlibglobals.time / 60 / 60 ), + (int) ( botlibglobals.time / 60 ), + (int) ( botlibglobals.time ), + (int) ( (int) ( botlibglobals.time * 100 ) ) - + ( (int) botlibglobals.time ) * 100 ); + va_start( ap, fmt ); + vfprintf( logfile.fp, fmt, ap ); + va_end( ap ); + fprintf( logfile.fp, "\r\n" ); + logfile.numwrites++; + fflush( logfile.fp ); +} //end of the function Log_Write +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +FILE *Log_FilePointer( void ) { + return logfile.fp; +} //end of the function Log_FilePointer +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Log_Flush( void ) { + if ( logfile.fp ) { + fflush( logfile.fp ); + } +} //end of the function Log_Flush + diff --git a/src/botlib/l_log.h b/src/botlib/l_log.h new file mode 100644 index 0000000..fa32c9f --- /dev/null +++ b/src/botlib/l_log.h @@ -0,0 +1,54 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: l_log.h + * + * desc: log file + * + * + *****************************************************************************/ + +//open a log file +void Log_Open( char *filename ); +// +void Log_AlwaysOpen( char *filename ); +//close the current log file +void Log_Close( void ); +//close log file if present +void Log_Shutdown( void ); +//write to the current opened log file +void QDECL Log_Write( char *fmt, ... ); +//write to the current opened log file with a time stamp +void QDECL Log_WriteTimeStamped( char *fmt, ... ); +//returns a pointer to the log file +FILE *Log_FilePointer( void ); +//flush log file +void Log_Flush( void ); + diff --git a/src/botlib/l_memory.c b/src/botlib/l_memory.c new file mode 100644 index 0000000..6905afc --- /dev/null +++ b/src/botlib/l_memory.c @@ -0,0 +1,446 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: l_memory.c + * + * desc: memory allocation + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "../game/botlib.h" +#include "l_log.h" +#include "be_interface.h" + +#ifdef _DEBUG + #define MEMDEBUG + #define MEMORYMANEGER +#endif + +#define MEM_ID 0x12345678l +#define HUNK_ID 0x87654321l + +int allocatedmemory; +int totalmemorysize; +int numblocks; + +#ifdef MEMORYMANEGER + +typedef struct memoryblock_s +{ + unsigned long int id; + void *ptr; + int size; +#ifdef MEMDEBUG + char *label; + char *file; + int line; +#endif //MEMDEBUG + struct memoryblock_s *prev, *next; +} memoryblock_t; + +memoryblock_t *memory; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void LinkMemoryBlock( memoryblock_t *block ) { + block->prev = NULL; + block->next = memory; + if ( memory ) { + memory->prev = block; + } + memory = block; +} //end of the function LinkMemoryBlock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void UnlinkMemoryBlock( memoryblock_t *block ) { + if ( block->prev ) { + block->prev->next = block->next; + } else { memory = block->next;} + if ( block->next ) { + block->next->prev = block->prev; + } +} //end of the function UnlinkMemoryBlock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef MEMDEBUG +void *GetMemoryDebug( unsigned long size, char *label, char *file, int line ) +#else +void *GetMemory( unsigned long size ) +#endif //MEMDEBUG +{ + void *ptr; + memoryblock_t *block; + + ptr = botimport.GetMemory( size + sizeof( memoryblock_t ) ); + block = (memoryblock_t *) ptr; + block->id = MEM_ID; + block->ptr = (char *) ptr + sizeof( memoryblock_t ); + block->size = size + sizeof( memoryblock_t ); +#ifdef MEMDEBUG + block->label = label; + block->file = file; + block->line = line; +#endif //MEMDEBUG + LinkMemoryBlock( block ); + allocatedmemory += block->size; + totalmemorysize += block->size + sizeof( memoryblock_t ); + numblocks++; + return block->ptr; +} //end of the function GetMemoryDebug +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef MEMDEBUG +void *GetClearedMemoryDebug( unsigned long size, char *label, char *file, int line ) +#else +void *GetClearedMemory( unsigned long size ) +#endif //MEMDEBUG +{ + void *ptr; +#ifdef MEMDEBUG + ptr = GetMemoryDebug( size, label, file, line ); +#else + ptr = GetMemory( size ); +#endif //MEMDEBUG + memset( ptr, 0, size ); + return ptr; +} //end of the function GetClearedMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef MEMDEBUG +void *GetHunkMemoryDebug( unsigned long size, char *label, char *file, int line ) +#else +void *GetHunkMemory( unsigned long size ) +#endif //MEMDEBUG +{ + void *ptr; + memoryblock_t *block; + + ptr = botimport.HunkAlloc( size + sizeof( memoryblock_t ) ); + block = (memoryblock_t *) ptr; + block->id = HUNK_ID; + block->ptr = (char *) ptr + sizeof( memoryblock_t ); + block->size = size + sizeof( memoryblock_t ); +#ifdef MEMDEBUG + block->label = label; + block->file = file; + block->line = line; +#endif //MEMDEBUG + LinkMemoryBlock( block ); + allocatedmemory += block->size; + totalmemorysize += block->size + sizeof( memoryblock_t ); + numblocks++; + return block->ptr; +} //end of the function GetHunkMemoryDebug +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef MEMDEBUG +void *GetClearedHunkMemoryDebug( unsigned long size, char *label, char *file, int line ) +#else +void *GetClearedHunkMemory( unsigned long size ) +#endif //MEMDEBUG +{ + void *ptr; +#ifdef MEMDEBUG + ptr = GetHunkMemoryDebug( size, label, file, line ); +#else + ptr = GetHunkMemory( size ); +#endif //MEMDEBUG + memset( ptr, 0, size ); + return ptr; +} //end of the function GetClearedHunkMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +memoryblock_t *BlockFromPointer( void *ptr, char *str ) { + memoryblock_t *block; + + if ( !ptr ) { +#ifdef MEMDEBUG + //char *crash = (char *) NULL; + //crash[0] = 1; + botimport.Print( PRT_FATAL, "%s: NULL pointer\n", str ); +#endif //MEMDEBUG + return NULL; + } //end if + block = ( memoryblock_t * )( (char *) ptr - sizeof( memoryblock_t ) ); + if ( block->id != MEM_ID && block->id != HUNK_ID ) { + botimport.Print( PRT_FATAL, "%s: invalid memory block\n", str ); + return NULL; + } //end if + if ( block->ptr != ptr ) { + botimport.Print( PRT_FATAL, "%s: memory block pointer invalid\n", str ); + return NULL; + } //end if + return block; +} //end of the function BlockFromPointer +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FreeMemory( void *ptr ) { + memoryblock_t *block; + + block = BlockFromPointer( ptr, "FreeMemory" ); + if ( !block ) { + return; + } + UnlinkMemoryBlock( block ); + allocatedmemory -= block->size; + totalmemorysize -= block->size + sizeof( memoryblock_t ); + numblocks--; + // + if ( block->id == MEM_ID ) { + botimport.FreeMemory( block ); + } //end if +} //end of the function FreeMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int MemoryByteSize( void *ptr ) { + memoryblock_t *block; + + block = BlockFromPointer( ptr, "MemoryByteSize" ); + if ( !block ) { + return 0; + } + return block->size; +} //end of the function MemoryByteSize +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void PrintUsedMemorySize( void ) { + botimport.Print( PRT_MESSAGE, "total allocated memory: %d KB\n", allocatedmemory >> 10 ); + botimport.Print( PRT_MESSAGE, "total botlib memory: %d KB\n", totalmemorysize >> 10 ); + botimport.Print( PRT_MESSAGE, "total memory blocks: %d\n", numblocks ); +} //end of the function PrintUsedMemorySize +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void PrintMemoryLabels( void ) { + memoryblock_t *block; + int i; + + PrintUsedMemorySize(); + i = 0; + Log_Write( "\r\n" ); + for ( block = memory; block; block = block->next ) + { +#ifdef MEMDEBUG + if ( block->id == HUNK_ID ) { + Log_Write( "%6d, hunk %p, %8d: %24s line %6d: %s\r\n", i, block->ptr, block->size, block->file, block->line, block->label ); + } //end if + else + { + Log_Write( "%6d, %p, %8d: %24s line %6d: %s\r\n", i, block->ptr, block->size, block->file, block->line, block->label ); + } //end else +#endif //MEMDEBUG + i++; + } //end for +} //end of the function PrintMemoryLabels +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void DumpMemory( void ) { + memoryblock_t *block; + + for ( block = memory; block; block = memory ) + { + FreeMemory( block->ptr ); + } //end for + totalmemorysize = 0; + allocatedmemory = 0; +} //end of the function DumpMemory + +#else + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef MEMDEBUG +void *GetMemoryDebug( unsigned long size, char *label, char *file, int line ) +#else +void *GetMemory( unsigned long size ) +#endif //MEMDEBUG +{ + void *ptr; + unsigned long int *memid; + + ptr = botimport.GetMemory( size + sizeof( unsigned long int ) ); + if ( !ptr ) { + return NULL; + } + memid = (unsigned long int *) ptr; + *memid = MEM_ID; + return (unsigned long int *) ( (char *) ptr + sizeof( unsigned long int ) ); +} //end of the function GetMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef MEMDEBUG +void *GetClearedMemoryDebug( unsigned long size, char *label, char *file, int line ) +#else +void *GetClearedMemory( unsigned long size ) +#endif //MEMDEBUG +{ + void *ptr; +#ifdef MEMDEBUG + ptr = GetMemoryDebug( size, label, file, line ); +#else +ptr = GetMemory( size ); +#endif //MEMDEBUG +memset( ptr, 0, size ); +return ptr; +} //end of the function GetClearedMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef MEMDEBUG +void *GetHunkMemoryDebug( unsigned long size, char *label, char *file, int line ) +#else +void *GetHunkMemory( unsigned long size ) +#endif //MEMDEBUG +{ + void *ptr; + unsigned long int *memid; + + ptr = botimport.HunkAlloc( size + sizeof( unsigned long int ) ); + if ( !ptr ) { + return NULL; + } + memid = (unsigned long int *) ptr; + *memid = HUNK_ID; + return (unsigned long int *) ( (char *) ptr + sizeof( unsigned long int ) ); +} //end of the function GetHunkMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef MEMDEBUG +void *GetClearedHunkMemoryDebug( unsigned long size, char *label, char *file, int line ) +#else +void *GetClearedHunkMemory( unsigned long size ) +#endif //MEMDEBUG +{ + void *ptr; +#ifdef MEMDEBUG + ptr = GetHunkMemoryDebug( size, label, file, line ); +#else +ptr = GetHunkMemory( size ); +#endif //MEMDEBUG +memset( ptr, 0, size ); +return ptr; +} //end of the function GetClearedHunkMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FreeMemory( void *ptr ) { + unsigned long int *memid; + + memid = (unsigned long int *) ( (char *) ptr - sizeof( unsigned long int ) ); + + if ( *memid == MEM_ID ) { + botimport.FreeMemory( memid ); + } //end if +} //end of the function FreeMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void PrintUsedMemorySize( void ) { +} //end of the function PrintUsedMemorySize +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void PrintMemoryLabels( void ) { +} //end of the function PrintMemoryLabels + +#endif diff --git a/src/botlib/l_memory.h b/src/botlib/l_memory.h new file mode 100644 index 0000000..e730c1b --- /dev/null +++ b/src/botlib/l_memory.h @@ -0,0 +1,84 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: l_memory.h + * + * desc: memory management + * + * + *****************************************************************************/ + +#ifdef _DEBUG +#ifndef BSPC + #define MEMDEBUG +#endif +#endif + +#ifdef MEMDEBUG +#define GetMemory( size ) GetMemoryDebug( size, # size, __FILE__, __LINE__ ); +#define GetClearedMemory( size ) GetClearedMemoryDebug( size, # size, __FILE__, __LINE__ ); +//allocate a memory block of the given size +void *GetMemoryDebug( unsigned long size, char *label, char *file, int line ); +//allocate a memory block of the given size and clear it +void *GetClearedMemoryDebug( unsigned long size, char *label, char *file, int line ); +// +#define GetHunkMemory( size ) GetHunkMemoryDebug( size, # size, __FILE__, __LINE__ ); +#define GetClearedHunkMemory( size ) GetClearedHunkMemoryDebug( size, # size, __FILE__, __LINE__ ); +//allocate a memory block of the given size +void *GetHunkMemoryDebug( unsigned long size, char *label, char *file, int line ); +//allocate a memory block of the given size and clear it +void *GetClearedHunkMemoryDebug( unsigned long size, char *label, char *file, int line ); +#else +//allocate a memory block of the given size +void *GetMemory( unsigned long size ); +//allocate a memory block of the given size and clear it +void *GetClearedMemory( unsigned long size ); +// +#ifdef BSPC +#define GetHunkMemory GetMemory +#define GetClearedHunkMemory GetClearedMemory +#else +//allocate a memory block of the given size +void *GetHunkMemory( unsigned long size ); +//allocate a memory block of the given size and clear it +void *GetClearedHunkMemory( unsigned long size ); +#endif +#endif + +//free the given memory block +void FreeMemory( void *ptr ); +//prints the total used memory size +void PrintUsedMemorySize( void ); +//print all memory blocks with label +void PrintMemoryLabels( void ); +//returns the size of the memory block in bytes +int MemoryByteSize( void *ptr ); +//free all allocated memory +void DumpMemory( void ); diff --git a/src/botlib/l_precomp.c b/src/botlib/l_precomp.c new file mode 100644 index 0000000..2a77426 --- /dev/null +++ b/src/botlib/l_precomp.c @@ -0,0 +1,3229 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// Copyright (C) 1999-2000 Id Software, Inc. +// + +/***************************************************************************** + * name: l_precomp.c + * + * desc: pre compiler + * + * + *****************************************************************************/ + +//Notes: fix: PC_StringizeTokens + +//#define SCREWUP +//#define BOTLIB +//#define QUAKE +//#define QUAKEC +//#define MEQCC + +#ifdef SCREWUP +#include +#include +#include +#include +#include +#include +#include "l_memory.h" +#include "l_script.h" +#include "l_precomp.h" + +typedef enum {qfalse, qtrue} qboolean; +#endif //SCREWUP + +#ifdef BOTLIB +#include "../game/q_shared.h" +#include "../game/botlib.h" +#include "be_interface.h" +#include "l_memory.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_log.h" +#endif //BOTLIB + +#ifdef MEQCC +#include "qcc.h" +#include "time.h" //time & ctime +#include "math.h" //fabs +#include "l_memory.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_log.h" + +#define qtrue true +#define qfalse false +#endif //MEQCC + +#ifdef BSPC +//include files for usage in the BSP Converter +#include "../bspc/qbsp.h" +#include "../bspc/l_log.h" +#include "../bspc/l_mem.h" +#include "l_precomp.h" + +#define qtrue true +#define qfalse false +#define Q_stricmp strcasecmp + +#define MAX_TOKENLENGTH 1024 + +#endif //BSPC + +#if defined( QUAKE ) && !defined( BSPC ) +#include "l_utils.h" +#endif //QUAKE + +//#define DEBUG_EVAL + +#define MAX_DEFINEPARMS 128 + +#define DEFINEHASHING 1 + +//directive name with parse function +typedef struct directive_s +{ + char *name; + int ( *func )( source_t *source ); +} directive_t; + +#define DEFINEHASHSIZE 1024 + +#define TOKEN_HEAP_SIZE 4096 + +int numtokens; +/* +int tokenheapinitialized; //true when the token heap is initialized +token_t token_heap[TOKEN_HEAP_SIZE]; //heap with tokens +token_t *freetokens; //free tokens from the heap +*/ + +//list with global defines added to every source loaded +define_t *globaldefines; + +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void QDECL SourceError( source_t *source, char *str, ... ) { + char text[1024]; + va_list ap; + + va_start( ap, str ); + vsprintf( text, str, ap ); + va_end( ap ); +#ifdef BOTLIB + botimport.Print( PRT_ERROR, "file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text ); +#endif //BOTLIB +#ifdef MEQCC + printf( "error: file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text ); +#endif //MEQCC +#ifdef BSPC + Log_Print( "error: file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text ); +#endif //BSPC +} //end of the function SourceError +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void QDECL SourceWarning( source_t *source, char *str, ... ) { + char text[1024]; + va_list ap; + + va_start( ap, str ); + vsprintf( text, str, ap ); + va_end( ap ); +#ifdef BOTLIB + botimport.Print( PRT_WARNING, "file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text ); +#endif //BOTLIB +#ifdef MEQCC + printf( "warning: file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text ); +#endif //MEQCC +#ifdef BSPC + Log_Print( "warning: file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text ); +#endif //BSPC +} //end of the function ScriptWarning +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_PushIndent( source_t *source, int type, int skip ) { + indent_t *indent; + + indent = (indent_t *) GetMemory( sizeof( indent_t ) ); + indent->type = type; + indent->script = source->scriptstack; + indent->skip = ( skip != 0 ); + source->skip += indent->skip; + indent->next = source->indentstack; + source->indentstack = indent; +} //end of the function PC_PushIndent +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_PopIndent( source_t *source, int *type, int *skip ) { + indent_t *indent; + + *type = 0; + *skip = 0; + + indent = source->indentstack; + if ( !indent ) { + return; + } + + //must be an indent from the current script + if ( source->indentstack->script != source->scriptstack ) { + return; + } + + *type = indent->type; + *skip = indent->skip; + source->indentstack = source->indentstack->next; + source->skip -= indent->skip; + FreeMemory( indent ); +} //end of the function PC_PopIndent +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_PushScript( source_t *source, script_t *script ) { + script_t *s; + + for ( s = source->scriptstack; s; s = s->next ) + { + if ( !Q_stricmp( s->filename, script->filename ) ) { + SourceError( source, "%s recursively included", script->filename ); + return; + } //end if + } //end for + //push the script on the script stack + script->next = source->scriptstack; + source->scriptstack = script; +} //end of the function PC_PushScript +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_InitTokenHeap( void ) { + /* + int i; + + if (tokenheapinitialized) return; + freetokens = NULL; + for (i = 0; i < TOKEN_HEAP_SIZE; i++) + { + token_heap[i].next = freetokens; + freetokens = &token_heap[i]; + } //end for + tokenheapinitialized = qtrue; + */ +} //end of the function PC_InitTokenHeap +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +token_t *PC_CopyToken( token_t *token ) { + token_t *t; + +// t = (token_t *) malloc(sizeof(token_t)); + t = (token_t *) GetMemory( sizeof( token_t ) ); +// t = freetokens; + if ( !t ) { +#ifdef BSPC + Error( "out of token space\n" ); +#else + Com_Error( ERR_FATAL, "out of token space\n" ); +#endif + return NULL; + } //end if +// freetokens = freetokens->next; + memcpy( t, token, sizeof( token_t ) ); + t->next = NULL; + numtokens++; + return t; +} //end of the function PC_CopyToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_FreeToken( token_t *token ) { + //free(token); + FreeMemory( token ); +// token->next = freetokens; +// freetokens = token; + numtokens--; +} //end of the function PC_FreeToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_ReadSourceToken( source_t *source, token_t *token ) { + token_t *t; + script_t *script; + int type, skip; + + //if there's no token already available + while ( !source->tokens ) + { + //if there's a token to read from the script + if ( PS_ReadToken( source->scriptstack, token ) ) { + return qtrue; + } + //if at the end of the script + if ( EndOfScript( source->scriptstack ) ) { + //remove all indents of the script + while ( source->indentstack && + source->indentstack->script == source->scriptstack ) + { + SourceWarning( source, "missing #endif" ); + PC_PopIndent( source, &type, &skip ); + } //end if + } //end if + //if this was the initial script + if ( !source->scriptstack->next ) { + return qfalse; + } + //remove the script and return to the last one + script = source->scriptstack; + source->scriptstack = source->scriptstack->next; + FreeScript( script ); + } //end while + //copy the already available token + memcpy( token, source->tokens, sizeof( token_t ) ); + //free the read token + t = source->tokens; + source->tokens = source->tokens->next; + PC_FreeToken( t ); + return qtrue; +} //end of the function PC_ReadSourceToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_UnreadSourceToken( source_t *source, token_t *token ) { + token_t *t; + + t = PC_CopyToken( token ); + t->next = source->tokens; + source->tokens = t; + return qtrue; +} //end of the function PC_UnreadSourceToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_ReadDefineParms( source_t *source, define_t *define, token_t **parms, int maxparms ) { + token_t token, *t, *last; + int i, done, lastcomma, numparms, indent; + + if ( !PC_ReadSourceToken( source, &token ) ) { + SourceError( source, "define %s missing parms", define->name ); + return qfalse; + } //end if + // + if ( define->numparms > maxparms ) { + SourceError( source, "define with more than %d parameters", maxparms ); + return qfalse; + } //end if + // + for ( i = 0; i < define->numparms; i++ ) parms[i] = NULL; + //if no leading "(" + if ( strcmp( token.string, "(" ) ) { + PC_UnreadSourceToken( source, &token ); + SourceError( source, "define %s missing parms", define->name ); + return qfalse; + } //end if + //read the define parameters + for ( done = 0, numparms = 0, indent = 0; !done; ) + { + if ( numparms >= maxparms ) { + SourceError( source, "define %s with too many parms", define->name ); + return qfalse; + } //end if + if ( numparms >= define->numparms ) { + SourceWarning( source, "define %s has too many parms", define->name ); + return qfalse; + } //end if + parms[numparms] = NULL; + lastcomma = 1; + last = NULL; + while ( !done ) + { + // + if ( !PC_ReadSourceToken( source, &token ) ) { + SourceError( source, "define %s incomplete", define->name ); + return qfalse; + } //end if + // + if ( !strcmp( token.string, "," ) ) { + if ( indent <= 0 ) { + if ( lastcomma ) { + SourceWarning( source, "too many comma's" ); + } + lastcomma = 1; + break; + } //end if + } //end if + lastcomma = 0; + // + if ( !strcmp( token.string, "(" ) ) { + indent++; + continue; + } //end if + else if ( !strcmp( token.string, ")" ) ) { + if ( --indent <= 0 ) { + if ( !parms[define->numparms - 1] ) { + SourceWarning( source, "too few define parms" ); + } //end if + done = 1; + break; + } //end if + } //end if + // + if ( numparms < define->numparms ) { + // + t = PC_CopyToken( &token ); + t->next = NULL; + if ( last ) { + last->next = t; + } else { parms[numparms] = t;} + last = t; + } //end if + } //end while + numparms++; + } //end for + return qtrue; +} //end of the function PC_ReadDefineParms +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_StringizeTokens( token_t *tokens, token_t *token ) { + token_t *t; + + token->type = TT_STRING; + token->whitespace_p = NULL; + token->endwhitespace_p = NULL; + token->string[0] = '\0'; + strcat( token->string, "\"" ); + for ( t = tokens; t; t = t->next ) + { + strncat( token->string, t->string, MAX_TOKEN - strlen( token->string ) ); + } //end for + strncat( token->string, "\"", MAX_TOKEN - strlen( token->string ) ); + return qtrue; +} //end of the function PC_StringizeTokens +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_MergeTokens( token_t *t1, token_t *t2 ) { + //merging of a name with a name or number + if ( t1->type == TT_NAME && ( t2->type == TT_NAME || t2->type == TT_NUMBER ) ) { + strcat( t1->string, t2->string ); + return qtrue; + } //end if + //merging of two strings + if ( t1->type == TT_STRING && t2->type == TT_STRING ) { + //remove trailing double quote + t1->string[strlen( t1->string ) - 1] = '\0'; + //concat without leading double quote + strcat( t1->string, &t2->string[1] ); + return qtrue; + } //end if + //FIXME: merging of two number of the same sub type + return qfalse; +} //end of the function PC_MergeTokens +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +/* +void PC_PrintDefine(define_t *define) +{ + printf("define->name = %s\n", define->name); + printf("define->flags = %d\n", define->flags); + printf("define->builtin = %d\n", define->builtin); + printf("define->numparms = %d\n", define->numparms); +// token_t *parms; //define parameters +// token_t *tokens; //macro tokens (possibly containing parm tokens) +// struct define_s *next; //next defined macro in a list +} //end of the function PC_PrintDefine*/ +#if DEFINEHASHING +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_PrintDefineHashTable( define_t **definehash ) { + int i; + define_t *d; + + for ( i = 0; i < DEFINEHASHSIZE; i++ ) + { + Log_Write( "%4d:", i ); + for ( d = definehash[i]; d; d = d->hashnext ) + { + Log_Write( " %s", d->name ); + } //end for + Log_Write( "\n" ); + } //end for +} //end of the function PC_PrintDefineHashTable +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +//char primes[16] = {1, 3, 5, 7, 11, 13, 17, 19, 23, 27, 29, 31, 37, 41, 43, 47}; + +int PC_NameHash( char *name ) { + int register hash, i; + + hash = 0; + for ( i = 0; name[i] != '\0'; i++ ) + { + hash += name[i] * ( 119 + i ); + //hash += (name[i] << 7) + i; + //hash += (name[i] << (i&15)); + } //end while + hash = ( hash ^ ( hash >> 10 ) ^ ( hash >> 20 ) ) & ( DEFINEHASHSIZE - 1 ); + return hash; +} //end of the function PC_NameHash +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_AddDefineToHash( define_t *define, define_t **definehash ) { + int hash; + + hash = PC_NameHash( define->name ); + define->hashnext = definehash[hash]; + definehash[hash] = define; +} //end of the function PC_AddDefineToHash +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +define_t *PC_FindHashedDefine( define_t **definehash, char *name ) { + define_t *d; + int hash; + + hash = PC_NameHash( name ); + for ( d = definehash[hash]; d; d = d->hashnext ) + { + if ( !strcmp( d->name, name ) ) { + return d; + } + } //end for + return NULL; +} //end of the function PC_FindHashedDefine +#endif //DEFINEHASHING +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +define_t *PC_FindDefine( define_t *defines, char *name ) { + define_t *d; + + for ( d = defines; d; d = d->next ) + { + if ( !strcmp( d->name, name ) ) { + return d; + } + } //end for + return NULL; +} //end of the function PC_FindDefine +//============================================================================ +// +// Parameter: - +// Returns: number of the parm +// if no parm found with the given name -1 is returned +// Changes Globals: - +//============================================================================ +int PC_FindDefineParm( define_t *define, char *name ) { + token_t *p; + int i; + + i = 0; + for ( p = define->parms; p; p = p->next ) + { + if ( !strcmp( p->string, name ) ) { + return i; + } + i++; + } //end for + return -1; +} //end of the function PC_FindDefineParm +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_FreeDefine( define_t *define ) { + token_t *t, *next; + + //free the define parameters + for ( t = define->parms; t; t = next ) + { + next = t->next; + PC_FreeToken( t ); + } //end for + //free the define tokens + for ( t = define->tokens; t; t = next ) + { + next = t->next; + PC_FreeToken( t ); + } //end for + //free the define + FreeMemory( define ); +} //end of the function PC_FreeDefine +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_AddBuiltinDefines( source_t *source ) { + int i; + define_t *define; + struct builtin + { + char *string; + int builtin; + } builtin[] = { + { "__LINE__", BUILTIN_LINE }, + { "__FILE__", BUILTIN_FILE }, + { "__DATE__", BUILTIN_DATE }, + { "__TIME__", BUILTIN_TIME }, +// "__STDC__", BUILTIN_STDC, + { NULL, 0 } + }; + + for ( i = 0; builtin[i].string; i++ ) + { + define = (define_t *) GetMemory( sizeof( define_t ) + strlen( builtin[i].string ) + 1 ); + memset( define, 0, sizeof( define_t ) ); + define->name = (char *) define + sizeof( define_t ); + strcpy( define->name, builtin[i].string ); + define->flags |= DEFINE_FIXED; + define->builtin = builtin[i].builtin; + //add the define to the source +#if DEFINEHASHING + PC_AddDefineToHash( define, source->definehash ); +#else + define->next = source->defines; + source->defines = define; +#endif //DEFINEHASHING + } //end for +} //end of the function PC_AddBuiltinDefines +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_ExpandBuiltinDefine( source_t *source, token_t *deftoken, define_t *define, + token_t **firsttoken, token_t **lasttoken ) { + token_t *token; + unsigned long t; // time_t t; //to prevent LCC warning + char *curtime; + + token = PC_CopyToken( deftoken ); + switch ( define->builtin ) + { + case BUILTIN_LINE: + { + sprintf( token->string, "%d", deftoken->line ); +#ifdef NUMBERVALUE + token->intvalue = deftoken->line; + token->floatvalue = deftoken->line; +#endif //NUMBERVALUE + token->type = TT_NUMBER; + token->subtype = TT_DECIMAL | TT_INTEGER; + *firsttoken = token; + *lasttoken = token; + break; + } //end case + case BUILTIN_FILE: + { + strcpy( token->string, source->scriptstack->filename ); + token->type = TT_NAME; + token->subtype = strlen( token->string ); + *firsttoken = token; + *lasttoken = token; + break; + } //end case + case BUILTIN_DATE: + { + t = time( NULL ); + curtime = ctime( &t ); + strcpy( token->string, "\"" ); + strncat( token->string, curtime + 4, 7 ); + strncat( token->string + 7, curtime + 20, 4 ); + strcat( token->string, "\"" ); + free( curtime ); + token->type = TT_NAME; + token->subtype = strlen( token->string ); + *firsttoken = token; + *lasttoken = token; + break; + } //end case + case BUILTIN_TIME: + { + t = time( NULL ); + curtime = ctime( &t ); + strcpy( token->string, "\"" ); + strncat( token->string, curtime + 11, 8 ); + strcat( token->string, "\"" ); + free( curtime ); + token->type = TT_NAME; + token->subtype = strlen( token->string ); + *firsttoken = token; + *lasttoken = token; + break; + } //end case + case BUILTIN_STDC: + default: + { + *firsttoken = NULL; + *lasttoken = NULL; + break; + } //end case + } //end switch + return qtrue; +} //end of the function PC_ExpandBuiltinDefine +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_ExpandDefine( source_t *source, token_t *deftoken, define_t *define, + token_t **firsttoken, token_t **lasttoken ) { + token_t *parms[MAX_DEFINEPARMS], *dt, *pt, *t; + token_t *t1, *t2, *first, *last, *nextpt, token; + int parmnum, i; + + //if it is a builtin define + if ( define->builtin ) { + return PC_ExpandBuiltinDefine( source, deftoken, define, firsttoken, lasttoken ); + } //end if + //if the define has parameters + if ( define->numparms ) { + if ( !PC_ReadDefineParms( source, define, parms, MAX_DEFINEPARMS ) ) { + return qfalse; + } +#ifdef DEBUG_EVAL + for ( i = 0; i < define->numparms; i++ ) + { + Log_Write( "define parms %d:", i ); + for ( pt = parms[i]; pt; pt = pt->next ) + { + Log_Write( "%s", pt->string ); + } //end for + } //end for +#endif //DEBUG_EVAL + } //end if + //empty list at first + first = NULL; + last = NULL; + //create a list with tokens of the expanded define + for ( dt = define->tokens; dt; dt = dt->next ) + { + parmnum = -1; + //if the token is a name, it could be a define parameter + if ( dt->type == TT_NAME ) { + parmnum = PC_FindDefineParm( define, dt->string ); + } //end if + //if it is a define parameter + if ( parmnum >= 0 ) { + for ( pt = parms[parmnum]; pt; pt = pt->next ) + { + t = PC_CopyToken( pt ); + //add the token to the list + t->next = NULL; + if ( last ) { + last->next = t; + } else { first = t;} + last = t; + } //end for + } //end if + else + { + //if stringizing operator + if ( dt->string[0] == '#' && dt->string[1] == '\0' ) { + //the stringizing operator must be followed by a define parameter + if ( dt->next ) { + parmnum = PC_FindDefineParm( define, dt->next->string ); + } else { parmnum = -1;} + // + if ( parmnum >= 0 ) { + //step over the stringizing operator + dt = dt->next; + //stringize the define parameter tokens + if ( !PC_StringizeTokens( parms[parmnum], &token ) ) { + SourceError( source, "can't stringize tokens" ); + return qfalse; + } //end if + t = PC_CopyToken( &token ); + } //end if + else + { + SourceWarning( source, "stringizing operator without define parameter" ); + continue; + } //end if + } //end if + else + { + t = PC_CopyToken( dt ); + } //end else + //add the token to the list + t->next = NULL; + if ( last ) { + last->next = t; + } else { first = t;} + last = t; + } //end else + } //end for + //check for the merging operator + for ( t = first; t; ) + { + if ( t->next ) { + //if the merging operator + if ( t->next->string[0] == '#' && t->next->string[1] == '#' ) { + t1 = t; + t2 = t->next->next; + if ( t2 ) { + if ( !PC_MergeTokens( t1, t2 ) ) { + SourceError( source, "can't merge %s with %s", t1->string, t2->string ); + return qfalse; + } //end if + PC_FreeToken( t1->next ); + t1->next = t2->next; + if ( t2 == last ) { + last = t1; + } + PC_FreeToken( t2 ); + continue; + } //end if + } //end if + } //end if + t = t->next; + } //end for + //store the first and last token of the list + *firsttoken = first; + *lasttoken = last; + //free all the parameter tokens + for ( i = 0; i < define->numparms; i++ ) + { + for ( pt = parms[i]; pt; pt = nextpt ) + { + nextpt = pt->next; + PC_FreeToken( pt ); + } //end for + } //end for + // + return qtrue; +} //end of the function PC_ExpandDefine +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_ExpandDefineIntoSource( source_t *source, token_t *deftoken, define_t *define ) { + token_t *firsttoken, *lasttoken; + + if ( !PC_ExpandDefine( source, deftoken, define, &firsttoken, &lasttoken ) ) { + return qfalse; + } + + if ( firsttoken && lasttoken ) { + lasttoken->next = source->tokens; + source->tokens = firsttoken; + return qtrue; + } //end if + return qfalse; +} //end of the function PC_ExpandDefineIntoSource +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_ConvertPath( char *path ) { + char *ptr; + + //remove double path seperators + for ( ptr = path; *ptr; ) + { + if ( ( *ptr == '\\' || *ptr == '/' ) && + ( *( ptr + 1 ) == '\\' || *( ptr + 1 ) == '/' ) ) { + strcpy( ptr, ptr + 1 ); + } //end if + else + { + ptr++; + } //end else + } //end while + //set OS dependent path seperators + for ( ptr = path; *ptr; ) + { + if ( *ptr == '/' || *ptr == '\\' ) { + *ptr = PATHSEPERATOR_CHAR; + } + ptr++; + } //end while +} //end of the function PC_ConvertPath +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_include( source_t *source ) { + script_t *script; + token_t token; + char path[_MAX_PATH]; +#ifdef QUAKE + foundfile_t file; +#endif //QUAKE + + if ( source->skip > 0 ) { + return qtrue; + } + // + if ( !PC_ReadSourceToken( source, &token ) ) { + SourceError( source, "#include without file name" ); + return qfalse; + } //end if + if ( token.linescrossed > 0 ) { + SourceError( source, "#include without file name" ); + return qfalse; + } //end if + if ( token.type == TT_STRING ) { + StripDoubleQuotes( token.string ); + PC_ConvertPath( token.string ); + script = LoadScriptFile( token.string ); + if ( !script ) { + strcpy( path, source->includepath ); + strcat( path, token.string ); + script = LoadScriptFile( path ); + } //end if + } //end if + else if ( token.type == TT_PUNCTUATION && *token.string == '<' ) { + strcpy( path, source->includepath ); + while ( PC_ReadSourceToken( source, &token ) ) + { + if ( token.linescrossed > 0 ) { + PC_UnreadSourceToken( source, &token ); + break; + } //end if + if ( token.type == TT_PUNCTUATION && *token.string == '>' ) { + break; + } + strncat( path, token.string, _MAX_PATH ); + } //end while + if ( *token.string != '>' ) { + SourceWarning( source, "#include missing trailing >" ); + } //end if + if ( !strlen( path ) ) { + SourceError( source, "#include without file name between < >" ); + return qfalse; + } //end if + PC_ConvertPath( path ); + script = LoadScriptFile( path ); + } //end if + else + { + SourceError( source, "#include without file name" ); + return qfalse; + } //end else +#ifdef QUAKE + if ( !script ) { + memset( &file, 0, sizeof( foundfile_t ) ); + script = LoadScriptFile( path ); + if ( script ) { + strncpy( script->filename, path, _MAX_PATH ); + } + } //end if +#endif //QUAKE + if ( !script ) { +#ifdef SCREWUP + SourceWarning( source, "file %s not found", path ); + return qtrue; +#else + SourceError( source, "file %s not found", path ); + return qfalse; +#endif //SCREWUP + } //end if + PC_PushScript( source, script ); + return qtrue; +} //end of the function PC_Directive_include +//============================================================================ +// reads a token from the current line, continues reading on the next +// line only if a backslash '\' is encountered. +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_ReadLine( source_t *source, token_t *token ) { + int crossline; + + crossline = 0; + do + { + if ( !PC_ReadSourceToken( source, token ) ) { + return qfalse; + } + + if ( token->linescrossed > crossline ) { + PC_UnreadSourceToken( source, token ); + return qfalse; + } //end if + crossline = 1; + } while ( !strcmp( token->string, "\\" ) ); + return qtrue; +} //end of the function PC_ReadLine +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_WhiteSpaceBeforeToken( token_t *token ) { + return token->endwhitespace_p - token->whitespace_p > 0; +} //end of the function PC_WhiteSpaceBeforeToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_ClearTokenWhiteSpace( token_t *token ) { + token->whitespace_p = NULL; + token->endwhitespace_p = NULL; + token->linescrossed = 0; +} //end of the function PC_ClearTokenWhiteSpace +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_undef( source_t *source ) { + token_t token; + define_t *define, *lastdefine; + int hash; + + if ( source->skip > 0 ) { + return qtrue; + } + // + if ( !PC_ReadLine( source, &token ) ) { + SourceError( source, "undef without name" ); + return qfalse; + } //end if + if ( token.type != TT_NAME ) { + PC_UnreadSourceToken( source, &token ); + SourceError( source, "expected name, found %s", token.string ); + return qfalse; + } //end if +#if DEFINEHASHING + + hash = PC_NameHash( token.string ); + for ( lastdefine = NULL, define = source->definehash[hash]; define; define = define->hashnext ) + { + if ( !strcmp( define->name, token.string ) ) { + if ( define->flags & DEFINE_FIXED ) { + SourceWarning( source, "can't undef %s", token.string ); + } //end if + else + { + if ( lastdefine ) { + lastdefine->hashnext = define->hashnext; + } else { source->definehash[hash] = define->hashnext;} + PC_FreeDefine( define ); + } //end else + break; + } //end if + lastdefine = define; + } //end for +#else //DEFINEHASHING + for ( lastdefine = NULL, define = source->defines; define; define = define->next ) + { + if ( !strcmp( define->name, token.string ) ) { + if ( define->flags & DEFINE_FIXED ) { + SourceWarning( source, "can't undef %s", token.string ); + } //end if + else + { + if ( lastdefine ) { + lastdefine->next = define->next; + } else { source->defines = define->next;} + PC_FreeDefine( define ); + } //end else + break; + } //end if + lastdefine = define; + } //end for +#endif //DEFINEHASHING + return qtrue; +} //end of the function PC_Directive_undef +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_define( source_t *source ) { + token_t token, *t, *last; + define_t *define; + + if ( source->skip > 0 ) { + return qtrue; + } + // + if ( !PC_ReadLine( source, &token ) ) { + SourceError( source, "#define without name" ); + return qfalse; + } //end if + if ( token.type != TT_NAME ) { + PC_UnreadSourceToken( source, &token ); + SourceError( source, "expected name after #define, found %s", token.string ); + return qfalse; + } //end if + //check if the define already exists +#if DEFINEHASHING + define = PC_FindHashedDefine( source->definehash, token.string ); +#else + define = PC_FindDefine( source->defines, token.string ); +#endif //DEFINEHASHING + if ( define ) { + if ( define->flags & DEFINE_FIXED ) { + SourceError( source, "can't redefine %s", token.string ); + return qfalse; + } //end if + SourceWarning( source, "redefinition of %s", token.string ); + //unread the define name before executing the #undef directive + PC_UnreadSourceToken( source, &token ); + if ( !PC_Directive_undef( source ) ) { + return qfalse; + } + //if the define was not removed (define->flags & DEFINE_FIXED) +#if DEFINEHASHING + define = PC_FindHashedDefine( source->definehash, token.string ); +#else + define = PC_FindDefine( source->defines, token.string ); +#endif //DEFINEHASHING + } //end if + //allocate define + define = (define_t *) GetMemory( sizeof( define_t ) + strlen( token.string ) + 1 ); + memset( define, 0, sizeof( define_t ) ); + define->name = (char *) define + sizeof( define_t ); + strcpy( define->name, token.string ); + //add the define to the source +#if DEFINEHASHING + PC_AddDefineToHash( define, source->definehash ); +#else //DEFINEHASHING + define->next = source->defines; + source->defines = define; +#endif //DEFINEHASHING + //if nothing is defined, just return + if ( !PC_ReadLine( source, &token ) ) { + return qtrue; + } + //if it is a define with parameters + if ( !PC_WhiteSpaceBeforeToken( &token ) && !strcmp( token.string, "(" ) ) { + //read the define parameters + last = NULL; + if ( !PC_CheckTokenString( source, ")" ) ) { + while ( 1 ) + { + if ( !PC_ReadLine( source, &token ) ) { + SourceError( source, "expected define parameter" ); + return qfalse; + } //end if + //if it isn't a name + if ( token.type != TT_NAME ) { + SourceError( source, "invalid define parameter" ); + return qfalse; + } //end if + // + if ( PC_FindDefineParm( define, token.string ) >= 0 ) { + SourceError( source, "two the same define parameters" ); + return qfalse; + } //end if + //add the define parm + t = PC_CopyToken( &token ); + PC_ClearTokenWhiteSpace( t ); + t->next = NULL; + if ( last ) { + last->next = t; + } else { define->parms = t;} + last = t; + define->numparms++; + //read next token + if ( !PC_ReadLine( source, &token ) ) { + SourceError( source, "define parameters not terminated" ); + return qfalse; + } //end if + // + if ( !strcmp( token.string, ")" ) ) { + break; + } + //then it must be a comma + if ( strcmp( token.string, "," ) ) { + SourceError( source, "define not terminated" ); + return qfalse; + } //end if + } //end while + } //end if + if ( !PC_ReadLine( source, &token ) ) { + return qtrue; + } + } //end if + //read the defined stuff + last = NULL; + do + { + t = PC_CopyToken( &token ); + if ( t->type == TT_NAME && !strcmp( t->string, define->name ) ) { + SourceError( source, "recursive define (removed recursion)" ); + continue; + } //end if + PC_ClearTokenWhiteSpace( t ); + t->next = NULL; + if ( last ) { + last->next = t; + } else { define->tokens = t;} + last = t; + } while ( PC_ReadLine( source, &token ) ); + // + if ( last ) { + //check for merge operators at the beginning or end + if ( !strcmp( define->tokens->string, "##" ) || + !strcmp( last->string, "##" ) ) { + SourceError( source, "define with misplaced ##" ); + return qfalse; + } //end if + } //end if + return qtrue; +} //end of the function PC_Directive_define +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +define_t *PC_DefineFromString( char *string ) { + script_t *script; + source_t src; + token_t *t; + int res, i; + define_t *def; + + PC_InitTokenHeap(); + + script = LoadScriptMemory( string, strlen( string ), "*extern" ); + //create a new source + memset( &src, 0, sizeof( source_t ) ); + strncpy( src.filename, "*extern", _MAX_PATH ); + src.scriptstack = script; +#if DEFINEHASHING + src.definehash = GetClearedMemory( DEFINEHASHSIZE * sizeof( define_t * ) ); +#endif //DEFINEHASHING + //create a define from the source + res = PC_Directive_define( &src ); + //free any tokens if left + for ( t = src.tokens; t; t = src.tokens ) + { + src.tokens = src.tokens->next; + PC_FreeToken( t ); + } //end for +#ifdef DEFINEHASHING + def = NULL; + for ( i = 0; i < DEFINEHASHSIZE; i++ ) + { + if ( src.definehash[i] ) { + def = src.definehash[i]; + break; + } //end if + } //end for +#else + def = src.defines; +#endif //DEFINEHASHING + // +#if DEFINEHASHING + FreeMemory( src.definehash ); +#endif //DEFINEHASHING + // + FreeScript( script ); + //if the define was created succesfully + if ( res > 0 ) { + return def; + } + //free the define if created + if ( src.defines ) { + PC_FreeDefine( def ); + } + // + return NULL; +} //end of the function PC_DefineFromString +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_AddDefine( source_t *source, char *string ) { + define_t *define; + + define = PC_DefineFromString( string ); + if ( !define ) { + return qfalse; + } +#if DEFINEHASHING + PC_AddDefineToHash( define, source->definehash ); +#else //DEFINEHASHING + define->next = source->defines; + source->defines = define; +#endif //DEFINEHASHING + return qtrue; +} //end of the function PC_AddDefine +//============================================================================ +// add a globals define that will be added to all opened sources +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_AddGlobalDefine( char *string ) { + define_t *define; + + define = PC_DefineFromString( string ); + if ( !define ) { + return qfalse; + } + define->next = globaldefines; + globaldefines = define; + return qtrue; +} //end of the function PC_AddGlobalDefine +//============================================================================ +// remove the given global define +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_RemoveGlobalDefine( char *name ) { + define_t *define; + + define = PC_FindDefine( globaldefines, name ); + if ( define ) { + PC_FreeDefine( define ); + return qtrue; + } //end if + return qfalse; +} //end of the function PC_RemoveGlobalDefine +//============================================================================ +// remove all globals defines +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_RemoveAllGlobalDefines( void ) { + define_t *define; + + for ( define = globaldefines; define; define = globaldefines ) + { + globaldefines = globaldefines->next; + PC_FreeDefine( define ); + } //end for +} //end of the function PC_RemoveAllGlobalDefines +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +define_t *PC_CopyDefine( source_t *source, define_t *define ) { + define_t *newdefine; + token_t *token, *newtoken, *lasttoken; + + newdefine = (define_t *) GetMemory( sizeof( define_t ) + strlen( define->name ) + 1 ); + //copy the define name + newdefine->name = (char *) newdefine + sizeof( define_t ); + strcpy( newdefine->name, define->name ); + newdefine->flags = define->flags; + newdefine->builtin = define->builtin; + newdefine->numparms = define->numparms; + //the define is not linked + newdefine->next = NULL; + newdefine->hashnext = NULL; + //copy the define tokens + newdefine->tokens = NULL; + for ( lasttoken = NULL, token = define->tokens; token; token = token->next ) + { + newtoken = PC_CopyToken( token ); + newtoken->next = NULL; + if ( lasttoken ) { + lasttoken->next = newtoken; + } else { newdefine->tokens = newtoken;} + lasttoken = newtoken; + } //end for + //copy the define parameters + newdefine->parms = NULL; + for ( lasttoken = NULL, token = define->parms; token; token = token->next ) + { + newtoken = PC_CopyToken( token ); + newtoken->next = NULL; + if ( lasttoken ) { + lasttoken->next = newtoken; + } else { newdefine->parms = newtoken;} + lasttoken = newtoken; + } //end for + return newdefine; +} //end of the function PC_CopyDefine +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_AddGlobalDefinesToSource( source_t *source ) { + define_t *define, *newdefine; + + for ( define = globaldefines; define; define = define->next ) + { + newdefine = PC_CopyDefine( source, define ); +#if DEFINEHASHING + PC_AddDefineToHash( newdefine, source->definehash ); +#else //DEFINEHASHING + newdefine->next = source->defines; + source->defines = newdefine; +#endif //DEFINEHASHING + } //end for +} //end of the function PC_AddGlobalDefinesToSource +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_if_def( source_t *source, int type ) { + token_t token; + define_t *d; + int skip; + + if ( !PC_ReadLine( source, &token ) ) { + SourceError( source, "#ifdef without name" ); + return qfalse; + } //end if + if ( token.type != TT_NAME ) { + PC_UnreadSourceToken( source, &token ); + SourceError( source, "expected name after #ifdef, found %s", token.string ); + return qfalse; + } //end if +#if DEFINEHASHING + d = PC_FindHashedDefine( source->definehash, token.string ); +#else + d = PC_FindDefine( source->defines, token.string ); +#endif //DEFINEHASHING + skip = ( type == INDENT_IFDEF ) == ( d == NULL ); + PC_PushIndent( source, type, skip ); + return qtrue; +} //end of the function PC_Directiveif_def +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_ifdef( source_t *source ) { + return PC_Directive_if_def( source, INDENT_IFDEF ); +} //end of the function PC_Directive_ifdef +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_ifndef( source_t *source ) { + return PC_Directive_if_def( source, INDENT_IFNDEF ); +} //end of the function PC_Directive_ifndef +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_else( source_t *source ) { + int type, skip; + + PC_PopIndent( source, &type, &skip ); + if ( !type ) { + SourceError( source, "misplaced #else" ); + return qfalse; + } //end if + if ( type == INDENT_ELSE ) { + SourceError( source, "#else after #else" ); + return qfalse; + } //end if + PC_PushIndent( source, INDENT_ELSE, !skip ); + return qtrue; +} //end of the function PC_Directive_else +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_endif( source_t *source ) { + int type, skip; + + PC_PopIndent( source, &type, &skip ); + if ( !type ) { + SourceError( source, "misplaced #endif" ); + return qfalse; + } //end if + return qtrue; +} //end of the function PC_Directive_endif +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +typedef struct operator_s +{ + int operator; + int priority; + int parentheses; + struct operator_s *prev, *next; +} operator_t; + +typedef struct value_s +{ + signed long int intvalue; + double floatvalue; + int parentheses; + struct value_s *prev, *next; +} value_t; + +int PC_OperatorPriority( int op ) { + switch ( op ) + { + case P_MUL: return 15; + case P_DIV: return 15; + case P_MOD: return 15; + case P_ADD: return 14; + case P_SUB: return 14; + + case P_LOGIC_AND: return 7; + case P_LOGIC_OR: return 6; + case P_LOGIC_GEQ: return 12; + case P_LOGIC_LEQ: return 12; + case P_LOGIC_EQ: return 11; + case P_LOGIC_UNEQ: return 11; + + case P_LOGIC_NOT: return 16; + case P_LOGIC_GREATER: return 12; + case P_LOGIC_LESS: return 12; + + case P_RSHIFT: return 13; + case P_LSHIFT: return 13; + + case P_BIN_AND: return 10; + case P_BIN_OR: return 8; + case P_BIN_XOR: return 9; + case P_BIN_NOT: return 16; + + case P_COLON: return 5; + case P_QUESTIONMARK: return 5; + } //end switch + return qfalse; +} //end of the function PC_OperatorPriority + +//#define AllocValue() GetClearedMemory(sizeof(value_t)); +//#define FreeValue(val) FreeMemory(val) +//#define AllocOperator(op) op = (operator_t *) GetClearedMemory(sizeof(operator_t)); +//#define FreeOperator(op) FreeMemory(op); + +#define MAX_VALUES 64 +#define MAX_OPERATORS 64 +#define AllocValue( val ) \ + if ( numvalues >= MAX_VALUES ) { \ + SourceError( source, "out of value space\n" ); \ + error = 1; \ + break; \ + } \ + else { \ + val = &value_heap[numvalues++];} +#define FreeValue( val ) +// +#define AllocOperator( op ) \ + if ( numoperators >= MAX_OPERATORS ) { \ + SourceError( source, "out of operator space\n" ); \ + error = 1; \ + break; \ + } \ + else { \ + op = &operator_heap[numoperators++];} +#define FreeOperator( op ) + +int PC_EvaluateTokens( source_t *source, token_t *tokens, signed long int *intvalue, + double *floatvalue, int integer ) { + operator_t *o, *firstoperator, *lastoperator; + value_t *v, *firstvalue, *lastvalue, *v1, *v2; + token_t *t; + int brace = 0; + int parentheses = 0; + int error = 0; + int lastwasvalue = 0; + int negativevalue = 0; + int questmarkintvalue = 0; + double questmarkfloatvalue = 0; + int gotquestmarkvalue = qfalse; + int lastoperatortype = 0; + // + operator_t operator_heap[MAX_OPERATORS]; + int numoperators = 0; + value_t value_heap[MAX_VALUES]; + int numvalues = 0; + + firstoperator = lastoperator = NULL; + firstvalue = lastvalue = NULL; + if ( intvalue ) { + *intvalue = 0; + } + if ( floatvalue ) { + *floatvalue = 0; + } + for ( t = tokens; t; t = t->next ) + { + switch ( t->type ) + { + case TT_NAME: + { + if ( lastwasvalue || negativevalue ) { + SourceError( source, "syntax error in #if/#elif" ); + error = 1; + break; + } //end if + if ( strcmp( t->string, "defined" ) ) { + SourceError( source, "undefined name %s in #if/#elif", t->string ); + error = 1; + break; + } //end if + t = t->next; + if ( !strcmp( t->string, "(" ) ) { + brace = qtrue; + t = t->next; + } //end if + if ( !t || t->type != TT_NAME ) { + SourceError( source, "defined without name in #if/#elif" ); + error = 1; + break; + } //end if + //v = (value_t *) GetClearedMemory(sizeof(value_t)); + AllocValue( v ); +#if DEFINEHASHING + if ( PC_FindHashedDefine( source->definehash, t->string ) ) +#else + if ( PC_FindDefine( source->defines, t->string ) ) +#endif //DEFINEHASHING + { + v->intvalue = 1; + v->floatvalue = 1; + } //end if + else + { + v->intvalue = 0; + v->floatvalue = 0; + } //end else + v->parentheses = parentheses; + v->next = NULL; + v->prev = lastvalue; + if ( lastvalue ) { + lastvalue->next = v; + } else { firstvalue = v;} + lastvalue = v; + if ( brace ) { + t = t->next; + if ( !t || strcmp( t->string, ")" ) ) { + SourceError( source, "defined without ) in #if/#elif" ); + error = 1; + break; + } //end if + } //end if + brace = qfalse; + // defined() creates a value + lastwasvalue = 1; + break; + } //end case + case TT_NUMBER: + { + if ( lastwasvalue ) { + SourceError( source, "syntax error in #if/#elif" ); + error = 1; + break; + } //end if + //v = (value_t *) GetClearedMemory(sizeof(value_t)); + AllocValue( v ); + if ( negativevalue ) { + v->intvalue = -(signed int) t->intvalue; + v->floatvalue = -t->floatvalue; + } //end if + else + { + v->intvalue = t->intvalue; + v->floatvalue = t->floatvalue; + } //end else + v->parentheses = parentheses; + v->next = NULL; + v->prev = lastvalue; + if ( lastvalue ) { + lastvalue->next = v; + } else { firstvalue = v;} + lastvalue = v; + //last token was a value + lastwasvalue = 1; + // + negativevalue = 0; + break; + } //end case + case TT_PUNCTUATION: + { + if ( negativevalue ) { + SourceError( source, "misplaced minus sign in #if/#elif" ); + error = 1; + break; + } //end if + if ( t->subtype == P_PARENTHESESOPEN ) { + parentheses++; + break; + } //end if + else if ( t->subtype == P_PARENTHESESCLOSE ) { + parentheses--; + if ( parentheses < 0 ) { + SourceError( source, "too many ) in #if/#elsif" ); + error = 1; + } //end if + break; + } //end else if + //check for invalid operators on floating point values + if ( !integer ) { + if ( t->subtype == P_BIN_NOT || t->subtype == P_MOD || + t->subtype == P_RSHIFT || t->subtype == P_LSHIFT || + t->subtype == P_BIN_AND || t->subtype == P_BIN_OR || + t->subtype == P_BIN_XOR ) { + SourceError( source, "illigal operator %s on floating point operands\n", t->string ); + error = 1; + break; + } //end if + } //end if + switch ( t->subtype ) + { + case P_LOGIC_NOT: + case P_BIN_NOT: + { + if ( lastwasvalue ) { + SourceError( source, "! or ~ after value in #if/#elif" ); + error = 1; + break; + } //end if + break; + } //end case + case P_INC: + case P_DEC: + { + SourceError( source, "++ or -- used in #if/#elif" ); + break; + } //end case + case P_SUB: + { + if ( !lastwasvalue ) { + negativevalue = 1; + break; + } //end if + } //end case + + case P_MUL: + case P_DIV: + case P_MOD: + case P_ADD: + + case P_LOGIC_AND: + case P_LOGIC_OR: + case P_LOGIC_GEQ: + case P_LOGIC_LEQ: + case P_LOGIC_EQ: + case P_LOGIC_UNEQ: + + case P_LOGIC_GREATER: + case P_LOGIC_LESS: + + case P_RSHIFT: + case P_LSHIFT: + + case P_BIN_AND: + case P_BIN_OR: + case P_BIN_XOR: + + case P_COLON: + case P_QUESTIONMARK: + { + if ( !lastwasvalue ) { + SourceError( source, "operator %s after operator in #if/#elif", t->string ); + error = 1; + break; + } //end if + break; + } //end case + default: + { + SourceError( source, "invalid operator %s in #if/#elif", t->string ); + error = 1; + break; + } //end default + } //end switch + if ( !error && !negativevalue ) { + //o = (operator_t *) GetClearedMemory(sizeof(operator_t)); + AllocOperator( o ); + o->operator = t->subtype; + o->priority = PC_OperatorPriority( t->subtype ); + o->parentheses = parentheses; + o->next = NULL; + o->prev = lastoperator; + if ( lastoperator ) { + lastoperator->next = o; + } else { firstoperator = o;} + lastoperator = o; + lastwasvalue = 0; + } //end if + break; + } //end case + default: + { + SourceError( source, "unknown %s in #if/#elif", t->string ); + error = 1; + break; + } //end default + } //end switch + if ( error ) { + break; + } + } //end for + if ( !error ) { + if ( !lastwasvalue ) { + SourceError( source, "trailing operator in #if/#elif" ); + error = 1; + } //end if + else if ( parentheses ) { + SourceError( source, "too many ( in #if/#elif" ); + error = 1; + } //end else if + } //end if + // + gotquestmarkvalue = qfalse; + questmarkintvalue = 0; + questmarkfloatvalue = 0; + //while there are operators + while ( !error && firstoperator ) + { + v = firstvalue; + for ( o = firstoperator; o->next; o = o->next ) + { + //if the current operator is nested deeper in parentheses + //than the next operator + if ( o->parentheses > o->next->parentheses ) { + break; + } + //if the current and next operator are nested equally deep in parentheses + if ( o->parentheses == o->next->parentheses ) { + //if the priority of the current operator is equal or higher + //than the priority of the next operator + if ( o->priority >= o->next->priority ) { + break; + } + } //end if + //if the arity of the operator isn't equal to 1 + if ( o->operator != P_LOGIC_NOT + && o->operator != P_BIN_NOT ) { + v = v->next; + } + //if there's no value or no next value + if ( !v ) { + SourceError( source, "mising values in #if/#elif" ); + error = 1; + break; + } //end if + } //end for + if ( error ) { + break; + } + v1 = v; + v2 = v->next; +#ifdef DEBUG_EVAL + if ( integer ) { + Log_Write( "operator %s, value1 = %d", PunctuationFromNum( source->scriptstack, o->operator ), v1->intvalue ); + if ( v2 ) { + Log_Write( "value2 = %d", v2->intvalue ); + } + } //end if + else + { + Log_Write( "operator %s, value1 = %f", PunctuationFromNum( source->scriptstack, o->operator ), v1->floatvalue ); + if ( v2 ) { + Log_Write( "value2 = %f", v2->floatvalue ); + } + } //end else +#endif //DEBUG_EVAL + switch ( o->operator ) + { + case P_LOGIC_NOT: v1->intvalue = !v1->intvalue; + v1->floatvalue = !v1->floatvalue; break; + case P_BIN_NOT: v1->intvalue = ~v1->intvalue; + break; + case P_MUL: v1->intvalue *= v2->intvalue; + v1->floatvalue *= v2->floatvalue; break; + case P_DIV: if ( !v2->intvalue || !v2->floatvalue ) { + SourceError( source, "divide by zero in #if/#elif\n" ); + error = 1; + break; + } + v1->intvalue /= v2->intvalue; + v1->floatvalue /= v2->floatvalue; break; + case P_MOD: if ( !v2->intvalue ) { + SourceError( source, "divide by zero in #if/#elif\n" ); + error = 1; + break; + } + v1->intvalue %= v2->intvalue; break; + case P_ADD: v1->intvalue += v2->intvalue; + v1->floatvalue += v2->floatvalue; break; + case P_SUB: v1->intvalue -= v2->intvalue; + v1->floatvalue -= v2->floatvalue; break; + case P_LOGIC_AND: v1->intvalue = v1->intvalue && v2->intvalue; + v1->floatvalue = v1->floatvalue && v2->floatvalue; break; + case P_LOGIC_OR: v1->intvalue = v1->intvalue || v2->intvalue; + v1->floatvalue = v1->floatvalue || v2->floatvalue; break; + case P_LOGIC_GEQ: v1->intvalue = v1->intvalue >= v2->intvalue; + v1->floatvalue = v1->floatvalue >= v2->floatvalue; break; + case P_LOGIC_LEQ: v1->intvalue = v1->intvalue <= v2->intvalue; + v1->floatvalue = v1->floatvalue <= v2->floatvalue; break; + case P_LOGIC_EQ: v1->intvalue = v1->intvalue == v2->intvalue; + v1->floatvalue = v1->floatvalue == v2->floatvalue; break; + case P_LOGIC_UNEQ: v1->intvalue = v1->intvalue != v2->intvalue; + v1->floatvalue = v1->floatvalue != v2->floatvalue; break; + case P_LOGIC_GREATER: v1->intvalue = v1->intvalue > v2->intvalue; + v1->floatvalue = v1->floatvalue > v2->floatvalue; break; + case P_LOGIC_LESS: v1->intvalue = v1->intvalue < v2->intvalue; + v1->floatvalue = v1->floatvalue < v2->floatvalue; break; + case P_RSHIFT: v1->intvalue >>= v2->intvalue; + break; + case P_LSHIFT: v1->intvalue <<= v2->intvalue; + break; + case P_BIN_AND: v1->intvalue &= v2->intvalue; + break; + case P_BIN_OR: v1->intvalue |= v2->intvalue; + break; + case P_BIN_XOR: v1->intvalue ^= v2->intvalue; + break; + case P_COLON: + { + if ( !gotquestmarkvalue ) { + SourceError( source, ": without ? in #if/#elif" ); + error = 1; + break; + } //end if + if ( integer ) { + if ( !questmarkintvalue ) { + v1->intvalue = v2->intvalue; + } + } //end if + else + { + if ( !questmarkfloatvalue ) { + v1->floatvalue = v2->floatvalue; + } + } //end else + gotquestmarkvalue = qfalse; + break; + } //end case + case P_QUESTIONMARK: + { + if ( gotquestmarkvalue ) { + SourceError( source, "? after ? in #if/#elif" ); + error = 1; + break; + } //end if + questmarkintvalue = v1->intvalue; + questmarkfloatvalue = v1->floatvalue; + gotquestmarkvalue = qtrue; + break; + } //end if + } //end switch +#ifdef DEBUG_EVAL + if ( integer ) { + Log_Write( "result value = %d", v1->intvalue ); + } else { Log_Write( "result value = %f", v1->floatvalue );} +#endif //DEBUG_EVAL + if ( error ) { + break; + } + lastoperatortype = o->operator; + //if not an operator with arity 1 + if ( o->operator != P_LOGIC_NOT + && o->operator != P_BIN_NOT ) { + //remove the second value if not question mark operator + if ( o->operator != P_QUESTIONMARK ) { + v = v->next; + } + // + if ( v->prev ) { + v->prev->next = v->next; + } else { firstvalue = v->next;} + if ( v->next ) { + v->next->prev = v->prev; + } else { lastvalue = v->prev;} + //FreeMemory(v); + FreeValue( v ); + } //end if + //remove the operator + if ( o->prev ) { + o->prev->next = o->next; + } else { firstoperator = o->next;} + if ( o->next ) { + o->next->prev = o->prev; + } else { lastoperator = o->prev;} + //FreeMemory(o); + FreeOperator( o ); + } //end while + if ( firstvalue ) { + if ( intvalue ) { + *intvalue = firstvalue->intvalue; + } + if ( floatvalue ) { + *floatvalue = firstvalue->floatvalue; + } + } //end if + for ( o = firstoperator; o; o = lastoperator ) + { + lastoperator = o->next; + //FreeMemory(o); + FreeOperator( o ); + } //end for + for ( v = firstvalue; v; v = lastvalue ) + { + lastvalue = v->next; + //FreeMemory(v); + FreeValue( v ); + } //end for + if ( !error ) { + return qtrue; + } + if ( intvalue ) { + *intvalue = 0; + } + if ( floatvalue ) { + *floatvalue = 0; + } + return qfalse; +} //end of the function PC_EvaluateTokens +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Evaluate( source_t *source, signed long int *intvalue, + double *floatvalue, int integer ) { + token_t token, *firsttoken, *lasttoken; + token_t *t, *nexttoken; + define_t *define; + int defined = qfalse; + + if ( intvalue ) { + *intvalue = 0; + } + if ( floatvalue ) { + *floatvalue = 0; + } + // + if ( !PC_ReadLine( source, &token ) ) { + SourceError( source, "no value after #if/#elif" ); + return qfalse; + } //end if + firsttoken = NULL; + lasttoken = NULL; + do + { + //if the token is a name + if ( token.type == TT_NAME ) { + if ( defined ) { + defined = qfalse; + t = PC_CopyToken( &token ); + t->next = NULL; + if ( lasttoken ) { + lasttoken->next = t; + } else { firsttoken = t;} + lasttoken = t; + } //end if + else if ( !strcmp( token.string, "defined" ) ) { + defined = qtrue; + t = PC_CopyToken( &token ); + t->next = NULL; + if ( lasttoken ) { + lasttoken->next = t; + } else { firsttoken = t;} + lasttoken = t; + } //end if + else + { + //then it must be a define +#if DEFINEHASHING + define = PC_FindHashedDefine( source->definehash, token.string ); +#else + define = PC_FindDefine( source->defines, token.string ); +#endif //DEFINEHASHING + if ( !define ) { + SourceError( source, "can't evaluate %s, not defined", token.string ); + return qfalse; + } //end if + if ( !PC_ExpandDefineIntoSource( source, &token, define ) ) { + return qfalse; + } + } //end else + } //end if + //if the token is a number or a punctuation + else if ( token.type == TT_NUMBER || token.type == TT_PUNCTUATION ) { + t = PC_CopyToken( &token ); + t->next = NULL; + if ( lasttoken ) { + lasttoken->next = t; + } else { firsttoken = t;} + lasttoken = t; + } //end else + else //can't evaluate the token + { + SourceError( source, "can't evaluate %s", token.string ); + return qfalse; + } //end else + } while ( PC_ReadLine( source, &token ) ); + // + if ( !PC_EvaluateTokens( source, firsttoken, intvalue, floatvalue, integer ) ) { + return qfalse; + } + // +#ifdef DEBUG_EVAL + Log_Write( "eval:" ); +#endif //DEBUG_EVAL + for ( t = firsttoken; t; t = nexttoken ) + { +#ifdef DEBUG_EVAL + Log_Write( " %s", t->string ); +#endif //DEBUG_EVAL + nexttoken = t->next; + PC_FreeToken( t ); + } //end for +#ifdef DEBUG_EVAL + if ( integer ) { + Log_Write( "eval result: %d", *intvalue ); + } else { Log_Write( "eval result: %f", *floatvalue );} +#endif //DEBUG_EVAL + // + return qtrue; +} //end of the function PC_Evaluate +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_DollarEvaluate( source_t *source, signed long int *intvalue, + double *floatvalue, int integer ) { + int indent, defined = qfalse; + token_t token, *firsttoken, *lasttoken; + token_t *t, *nexttoken; + define_t *define; + + if ( intvalue ) { + *intvalue = 0; + } + if ( floatvalue ) { + *floatvalue = 0; + } + // + if ( !PC_ReadSourceToken( source, &token ) ) { + SourceError( source, "no leading ( after $evalint/$evalfloat" ); + return qfalse; + } //end if + if ( !PC_ReadSourceToken( source, &token ) ) { + SourceError( source, "nothing to evaluate" ); + return qfalse; + } //end if + indent = 1; + firsttoken = NULL; + lasttoken = NULL; + do + { + //if the token is a name + if ( token.type == TT_NAME ) { + if ( defined ) { + defined = qfalse; + t = PC_CopyToken( &token ); + t->next = NULL; + if ( lasttoken ) { + lasttoken->next = t; + } else { firsttoken = t;} + lasttoken = t; + } //end if + else if ( !strcmp( token.string, "defined" ) ) { + defined = qtrue; + t = PC_CopyToken( &token ); + t->next = NULL; + if ( lasttoken ) { + lasttoken->next = t; + } else { firsttoken = t;} + lasttoken = t; + } //end if + else + { + //then it must be a define +#if DEFINEHASHING + define = PC_FindHashedDefine( source->definehash, token.string ); +#else + define = PC_FindDefine( source->defines, token.string ); +#endif //DEFINEHASHING + if ( !define ) { + SourceError( source, "can't evaluate %s, not defined", token.string ); + return qfalse; + } //end if + if ( !PC_ExpandDefineIntoSource( source, &token, define ) ) { + return qfalse; + } + } //end else + } //end if + //if the token is a number or a punctuation + else if ( token.type == TT_NUMBER || token.type == TT_PUNCTUATION ) { + if ( *token.string == '(' ) { + indent++; + } else if ( *token.string == ')' ) { + indent--; + } + if ( indent <= 0 ) { + break; + } + t = PC_CopyToken( &token ); + t->next = NULL; + if ( lasttoken ) { + lasttoken->next = t; + } else { firsttoken = t;} + lasttoken = t; + } //end else + else //can't evaluate the token + { + SourceError( source, "can't evaluate %s", token.string ); + return qfalse; + } //end else + } while ( PC_ReadSourceToken( source, &token ) ); + // + if ( !PC_EvaluateTokens( source, firsttoken, intvalue, floatvalue, integer ) ) { + return qfalse; + } + // +#ifdef DEBUG_EVAL + Log_Write( "$eval:" ); +#endif //DEBUG_EVAL + for ( t = firsttoken; t; t = nexttoken ) + { +#ifdef DEBUG_EVAL + Log_Write( " %s", t->string ); +#endif //DEBUG_EVAL + nexttoken = t->next; + PC_FreeToken( t ); + } //end for +#ifdef DEBUG_EVAL + if ( integer ) { + Log_Write( "$eval result: %d", *intvalue ); + } else { Log_Write( "$eval result: %f", *floatvalue );} +#endif //DEBUG_EVAL + // + return qtrue; +} //end of the function PC_DollarEvaluate +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_elif( source_t *source ) { + signed long int value; + int type, skip; + + PC_PopIndent( source, &type, &skip ); + if ( !type || type == INDENT_ELSE ) { + SourceError( source, "misplaced #elif" ); + return qfalse; + } //end if + if ( !PC_Evaluate( source, &value, NULL, qtrue ) ) { + return qfalse; + } + skip = ( value == 0 ); + PC_PushIndent( source, INDENT_ELIF, skip ); + return qtrue; +} //end of the function PC_Directive_elif +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_if( source_t *source ) { + signed long int value; + int skip; + + if ( !PC_Evaluate( source, &value, NULL, qtrue ) ) { + return qfalse; + } + skip = ( value == 0 ); + PC_PushIndent( source, INDENT_IF, skip ); + return qtrue; +} //end of the function PC_Directive +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_line( source_t *source ) { + SourceError( source, "#line directive not supported" ); + return qfalse; +} //end of the function PC_Directive_line +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_error( source_t *source ) { + token_t token; + + strcpy( token.string, "" ); + PC_ReadSourceToken( source, &token ); + SourceError( source, "#error directive: %s", token.string ); + return qfalse; +} //end of the function PC_Directive_error +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_pragma( source_t *source ) { + token_t token; + + SourceWarning( source, "#pragma directive not supported" ); + while ( PC_ReadLine( source, &token ) ) ; + return qtrue; +} //end of the function PC_Directive_pragma +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void UnreadSignToken( source_t *source ) { + token_t token; + + token.line = source->scriptstack->line; + token.whitespace_p = source->scriptstack->script_p; + token.endwhitespace_p = source->scriptstack->script_p; + token.linescrossed = 0; + strcpy( token.string, "-" ); + token.type = TT_PUNCTUATION; + token.subtype = P_SUB; + PC_UnreadSourceToken( source, &token ); +} //end of the function UnreadSignToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_eval( source_t *source ) { + signed long int value; + token_t token; + + if ( !PC_Evaluate( source, &value, NULL, qtrue ) ) { + return qfalse; + } + // + token.line = source->scriptstack->line; + token.whitespace_p = source->scriptstack->script_p; + token.endwhitespace_p = source->scriptstack->script_p; + token.linescrossed = 0; + sprintf( token.string, "%d", abs( value ) ); + token.type = TT_NUMBER; + token.subtype = TT_INTEGER | TT_LONG | TT_DECIMAL; + PC_UnreadSourceToken( source, &token ); + if ( value < 0 ) { + UnreadSignToken( source ); + } + return qtrue; +} //end of the function PC_Directive_eval +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_evalfloat( source_t *source ) { + double value; + token_t token; + + if ( !PC_Evaluate( source, NULL, &value, qfalse ) ) { + return qfalse; + } + token.line = source->scriptstack->line; + token.whitespace_p = source->scriptstack->script_p; + token.endwhitespace_p = source->scriptstack->script_p; + token.linescrossed = 0; + sprintf( token.string, "%1.2f", fabs( value ) ); + token.type = TT_NUMBER; + token.subtype = TT_FLOAT | TT_LONG | TT_DECIMAL; + PC_UnreadSourceToken( source, &token ); + if ( value < 0 ) { + UnreadSignToken( source ); + } + return qtrue; +} //end of the function PC_Directive_evalfloat +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +directive_t directives[20] = +{ + {"if", PC_Directive_if}, + {"ifdef", PC_Directive_ifdef}, + {"ifndef", PC_Directive_ifndef}, + {"elif", PC_Directive_elif}, + {"else", PC_Directive_else}, + {"endif", PC_Directive_endif}, + {"include", PC_Directive_include}, + {"define", PC_Directive_define}, + {"undef", PC_Directive_undef}, + {"line", PC_Directive_line}, + {"error", PC_Directive_error}, + {"pragma", PC_Directive_pragma}, + {"eval", PC_Directive_eval}, + {"evalfloat", PC_Directive_evalfloat}, + {NULL, NULL} +}; + +int PC_ReadDirective( source_t *source ) { + token_t token; + int i; + + //read the directive name + if ( !PC_ReadSourceToken( source, &token ) ) { + SourceError( source, "found # without name" ); + return qfalse; + } //end if + //directive name must be on the same line + if ( token.linescrossed > 0 ) { + PC_UnreadSourceToken( source, &token ); + SourceError( source, "found # at end of line" ); + return qfalse; + } //end if + //if if is a name + if ( token.type == TT_NAME ) { + //find the precompiler directive + for ( i = 0; directives[i].name; i++ ) + { + if ( !strcmp( directives[i].name, token.string ) ) { + return directives[i].func( source ); + } //end if + } //end for + } //end if + SourceError( source, "unknown precompiler directive %s", token.string ); + return qfalse; +} //end of the function PC_ReadDirective +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_DollarDirective_evalint( source_t *source ) { + signed long int value; + token_t token; + + if ( !PC_DollarEvaluate( source, &value, NULL, qtrue ) ) { + return qfalse; + } + // + token.line = source->scriptstack->line; + token.whitespace_p = source->scriptstack->script_p; + token.endwhitespace_p = source->scriptstack->script_p; + token.linescrossed = 0; + sprintf( token.string, "%d", abs( value ) ); + token.type = TT_NUMBER; + token.subtype = TT_INTEGER | TT_LONG | TT_DECIMAL; +#ifdef NUMBERVALUE + token.intvalue = value; + token.floatvalue = value; +#endif //NUMBERVALUE + PC_UnreadSourceToken( source, &token ); + if ( value < 0 ) { + UnreadSignToken( source ); + } + return qtrue; +} //end of the function PC_DollarDirective_evalint +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_DollarDirective_evalfloat( source_t *source ) { + double value; + token_t token; + + if ( !PC_DollarEvaluate( source, NULL, &value, qfalse ) ) { + return qfalse; + } + token.line = source->scriptstack->line; + token.whitespace_p = source->scriptstack->script_p; + token.endwhitespace_p = source->scriptstack->script_p; + token.linescrossed = 0; + sprintf( token.string, "%1.2f", fabs( value ) ); + token.type = TT_NUMBER; + token.subtype = TT_FLOAT | TT_LONG | TT_DECIMAL; +#ifdef NUMBERVALUE + token.intvalue = (unsigned long) value; + token.floatvalue = value; +#endif //NUMBERVALUE + PC_UnreadSourceToken( source, &token ); + if ( value < 0 ) { + UnreadSignToken( source ); + } + return qtrue; +} //end of the function PC_DollarDirective_evalfloat +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +directive_t dollardirectives[20] = +{ + {"evalint", PC_DollarDirective_evalint}, + {"evalfloat", PC_DollarDirective_evalfloat}, + {NULL, NULL} +}; + +int PC_ReadDollarDirective( source_t *source ) { + token_t token; + int i; + + //read the directive name + if ( !PC_ReadSourceToken( source, &token ) ) { + SourceError( source, "found $ without name" ); + return qfalse; + } //end if + //directive name must be on the same line + if ( token.linescrossed > 0 ) { + PC_UnreadSourceToken( source, &token ); + SourceError( source, "found $ at end of line" ); + return qfalse; + } //end if + //if if is a name + if ( token.type == TT_NAME ) { + //find the precompiler directive + for ( i = 0; dollardirectives[i].name; i++ ) + { + if ( !strcmp( dollardirectives[i].name, token.string ) ) { + return dollardirectives[i].func( source ); + } //end if + } //end for + } //end if + PC_UnreadSourceToken( source, &token ); + SourceError( source, "unknown precompiler directive %s", token.string ); + return qfalse; +} //end of the function PC_ReadDirective + +#ifdef QUAKEC +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int BuiltinFunction( source_t *source ) { + token_t token; + + if ( !PC_ReadSourceToken( source, &token ) ) { + return qfalse; + } + if ( token.type == TT_NUMBER ) { + PC_UnreadSourceToken( source, &token ); + return qtrue; + } //end if + else + { + PC_UnreadSourceToken( source, &token ); + return qfalse; + } //end else +} //end of the function BuiltinFunction +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int QuakeCMacro( source_t *source ) { + int i; + token_t token; + + if ( !PC_ReadSourceToken( source, &token ) ) { + return qtrue; + } + if ( token.type != TT_NAME ) { + PC_UnreadSourceToken( source, &token ); + return qtrue; + } //end if + //find the precompiler directive + for ( i = 0; dollardirectives[i].name; i++ ) + { + if ( !strcmp( dollardirectives[i].name, token.string ) ) { + PC_UnreadSourceToken( source, &token ); + return qfalse; + } //end if + } //end for + PC_UnreadSourceToken( source, &token ); + return qtrue; +} //end of the function QuakeCMacro +#endif //QUAKEC +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_ReadToken( source_t *source, token_t *token ) { + define_t *define; + + while ( 1 ) + { + if ( !PC_ReadSourceToken( source, token ) ) { + return qfalse; + } + //check for precompiler directives + if ( token->type == TT_PUNCTUATION && *token->string == '#' ) { +#ifdef QUAKEC + if ( !BuiltinFunction( source ) ) +#endif //QUAKC + { + //read the precompiler directive + if ( !PC_ReadDirective( source ) ) { + return qfalse; + } + continue; + } //end if + } //end if + if ( token->type == TT_PUNCTUATION && *token->string == '$' ) { +#ifdef QUAKEC + if ( !QuakeCMacro( source ) ) +#endif //QUAKEC + { + //read the precompiler directive + if ( !PC_ReadDollarDirective( source ) ) { + return qfalse; + } + continue; + } //end if + } //end if + // recursively concatenate strings that are behind each other still resolving defines + if ( token->type == TT_STRING ) { + token_t newtoken; + if ( PC_ReadToken( source, &newtoken ) ) { + if ( newtoken.type == TT_STRING ) { + token->string[strlen( token->string ) - 1] = '\0'; + if ( strlen( token->string ) + strlen( newtoken.string + 1 ) + 1 >= MAX_TOKEN ) { + SourceError( source, "string longer than MAX_TOKEN %d\n", MAX_TOKEN ); + return qfalse; + } + strcat( token->string, newtoken.string + 1 ); + } else + { + PC_UnreadToken( source, &newtoken ); + } + } + } //end if + //if skipping source because of conditional compilation + if ( source->skip ) { + continue; + } + //if the token is a name + if ( token->type == TT_NAME ) { + //check if the name is a define macro +#if DEFINEHASHING + define = PC_FindHashedDefine( source->definehash, token->string ); +#else + define = PC_FindDefine( source->defines, token->string ); +#endif //DEFINEHASHING + //if it is a define macro + if ( define ) { + //expand the defined macro + if ( !PC_ExpandDefineIntoSource( source, token, define ) ) { + return qfalse; + } + continue; + } //end if + } //end if + //copy token for unreading + memcpy( &source->token, token, sizeof( token_t ) ); + //found a token + return qtrue; + } //end while +} //end of the function PC_ReadToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_ExpectTokenString( source_t *source, char *string ) { + token_t token; + + if ( !PC_ReadToken( source, &token ) ) { + SourceError( source, "couldn't find expected %s", string ); + return qfalse; + } //end if + + if ( strcmp( token.string, string ) ) { + SourceError( source, "expected %s, found %s", string, token.string ); + return qfalse; + } //end if + return qtrue; +} //end of the function PC_ExpectTokenString +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_ExpectTokenType( source_t *source, int type, int subtype, token_t *token ) { + char str[MAX_TOKEN]; + + if ( !PC_ReadToken( source, token ) ) { + SourceError( source, "couldn't read expected token" ); + return qfalse; + } //end if + + if ( token->type != type ) { + strcpy( str, "" ); + if ( type == TT_STRING ) { + strcpy( str, "string" ); + } + if ( type == TT_LITERAL ) { + strcpy( str, "literal" ); + } + if ( type == TT_NUMBER ) { + strcpy( str, "number" ); + } + if ( type == TT_NAME ) { + strcpy( str, "name" ); + } + if ( type == TT_PUNCTUATION ) { + strcpy( str, "punctuation" ); + } + SourceError( source, "expected a %s, found %s", str, token->string ); + return qfalse; + } //end if + if ( token->type == TT_NUMBER ) { + if ( ( token->subtype & subtype ) != subtype ) { + if ( subtype & TT_DECIMAL ) { + strcpy( str, "decimal" ); + } + if ( subtype & TT_HEX ) { + strcpy( str, "hex" ); + } + if ( subtype & TT_OCTAL ) { + strcpy( str, "octal" ); + } + if ( subtype & TT_BINARY ) { + strcpy( str, "binary" ); + } + if ( subtype & TT_LONG ) { + strcat( str, " long" ); + } + if ( subtype & TT_UNSIGNED ) { + strcat( str, " unsigned" ); + } + if ( subtype & TT_FLOAT ) { + strcat( str, " float" ); + } + if ( subtype & TT_INTEGER ) { + strcat( str, " integer" ); + } + SourceError( source, "expected %s, found %s", str, token->string ); + return qfalse; + } //end if + } //end if + else if ( token->type == TT_PUNCTUATION ) { + if ( token->subtype != subtype ) { + SourceError( source, "found %s", token->string ); + return qfalse; + } //end if + } //end else if + return qtrue; +} //end of the function PC_ExpectTokenType +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_ExpectAnyToken( source_t *source, token_t *token ) { + if ( !PC_ReadToken( source, token ) ) { + SourceError( source, "couldn't read expected token" ); + return qfalse; + } //end if + else + { + return qtrue; + } //end else +} //end of the function PC_ExpectAnyToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_CheckTokenString( source_t *source, char *string ) { + token_t tok; + + if ( !PC_ReadToken( source, &tok ) ) { + return qfalse; + } + //if the token is available + if ( !strcmp( tok.string, string ) ) { + return qtrue; + } + // + PC_UnreadSourceToken( source, &tok ); + return qfalse; +} //end of the function PC_CheckTokenString +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_CheckTokenType( source_t *source, int type, int subtype, token_t *token ) { + token_t tok; + + if ( !PC_ReadToken( source, &tok ) ) { + return qfalse; + } + //if the type matches + if ( tok.type == type && + ( tok.subtype & subtype ) == subtype ) { + memcpy( token, &tok, sizeof( token_t ) ); + return qtrue; + } //end if + // + PC_UnreadSourceToken( source, &tok ); + return qfalse; +} //end of the function PC_CheckTokenType +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_SkipUntilString( source_t *source, char *string ) { + token_t token; + + while ( PC_ReadToken( source, &token ) ) + { + if ( !strcmp( token.string, string ) ) { + return qtrue; + } + } //end while + return qfalse; +} //end of the function PC_SkipUntilString +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_UnreadLastToken( source_t *source ) { + PC_UnreadSourceToken( source, &source->token ); +} //end of the function PC_UnreadLastToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_UnreadToken( source_t *source, token_t *token ) { + PC_UnreadSourceToken( source, token ); +} //end of the function PC_UnreadToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_SetIncludePath( source_t *source, char *path ) { + strncpy( source->includepath, path, _MAX_PATH ); + //add trailing path seperator + if ( source->includepath[strlen( source->includepath ) - 1] != '\\' && + source->includepath[strlen( source->includepath ) - 1] != '/' ) { + strcat( source->includepath, PATHSEPERATOR_STR ); + } //end if +} //end of the function PC_SetIncludePath +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_SetPunctuations( source_t *source, punctuation_t *p ) { + source->punctuations = p; +} //end of the function PC_SetPunctuations +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +source_t *LoadSourceFile( const char *filename ) { + source_t *source; + script_t *script; + + PC_InitTokenHeap(); + + script = LoadScriptFile( filename ); + if ( !script ) { + return NULL; + } + + script->next = NULL; + + source = (source_t *) GetMemory( sizeof( source_t ) ); + memset( source, 0, sizeof( source_t ) ); + + strncpy( source->filename, filename, _MAX_PATH ); + source->scriptstack = script; + source->tokens = NULL; + source->defines = NULL; + source->indentstack = NULL; + source->skip = 0; + +#if DEFINEHASHING + source->definehash = GetClearedMemory( DEFINEHASHSIZE * sizeof( define_t * ) ); +#endif //DEFINEHASHING + PC_AddGlobalDefinesToSource( source ); + return source; +} //end of the function LoadSourceFile +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +source_t *LoadSourceMemory( char *ptr, int length, char *name ) { + source_t *source; + script_t *script; + + PC_InitTokenHeap(); + + script = LoadScriptMemory( ptr, length, name ); + if ( !script ) { + return NULL; + } + script->next = NULL; + + source = (source_t *) GetMemory( sizeof( source_t ) ); + memset( source, 0, sizeof( source_t ) ); + + strncpy( source->filename, name, _MAX_PATH ); + source->scriptstack = script; + source->tokens = NULL; + source->defines = NULL; + source->indentstack = NULL; + source->skip = 0; + +#if DEFINEHASHING + source->definehash = GetClearedMemory( DEFINEHASHSIZE * sizeof( define_t * ) ); +#endif //DEFINEHASHING + PC_AddGlobalDefinesToSource( source ); + return source; +} //end of the function LoadSourceMemory +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void FreeSource( source_t *source ) { + script_t *script; + token_t *token; + define_t *define; + indent_t *indent; + int i; + + //PC_PrintDefineHashTable(source->definehash); + //free all the scripts + while ( source->scriptstack ) + { + script = source->scriptstack; + source->scriptstack = source->scriptstack->next; + FreeScript( script ); + } //end for + //free all the tokens + while ( source->tokens ) + { + token = source->tokens; + source->tokens = source->tokens->next; + PC_FreeToken( token ); + } //end for +#if DEFINEHASHING + for ( i = 0; i < DEFINEHASHSIZE; i++ ) + { + while ( source->definehash[i] ) + { + define = source->definehash[i]; + source->definehash[i] = source->definehash[i]->hashnext; + PC_FreeDefine( define ); + } //end while + } //end for +#else //DEFINEHASHING + //free all defines + while ( source->defines ) + { + define = source->defines; + source->defines = source->defines->next; + PC_FreeDefine( define ); + } //end for +#endif //DEFINEHASHING + //free all indents + while ( source->indentstack ) + { + indent = source->indentstack; + source->indentstack = source->indentstack->next; + FreeMemory( indent ); + } //end for +#if DEFINEHASHING + // + if ( source->definehash ) { + FreeMemory( source->definehash ); + } +#endif //DEFINEHASHING + //free the source itself + FreeMemory( source ); +} //end of the function FreeSource +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ + +#define MAX_SOURCEFILES 64 + +source_t *sourceFiles[MAX_SOURCEFILES]; + +int PC_LoadSourceHandle( const char *filename ) { + source_t *source; + int i; + + for ( i = 1; i < MAX_SOURCEFILES; i++ ) + { + if ( !sourceFiles[i] ) { + break; + } + } //end for + if ( i >= MAX_SOURCEFILES ) { + return 0; + } + PS_SetBaseFolder( "" ); + source = LoadSourceFile( filename ); + if ( !source ) { + return 0; + } + sourceFiles[i] = source; + return i; +} //end of the function PC_LoadSourceHandle +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_FreeSourceHandle( int handle ) { + if ( handle < 1 || handle >= MAX_SOURCEFILES ) { + return qfalse; + } + if ( !sourceFiles[handle] ) { + return qfalse; + } + + FreeSource( sourceFiles[handle] ); + sourceFiles[handle] = NULL; + return qtrue; +} //end of the function PC_FreeSourceHandle +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_ReadTokenHandle( int handle, pc_token_t *pc_token ) { + token_t token; + int ret; + + if ( handle < 1 || handle >= MAX_SOURCEFILES ) { + return 0; + } + if ( !sourceFiles[handle] ) { + return 0; + } + + ret = PC_ReadToken( sourceFiles[handle], &token ); + strcpy( pc_token->string, token.string ); + pc_token->type = token.type; + pc_token->subtype = token.subtype; + pc_token->intvalue = token.intvalue; + pc_token->floatvalue = token.floatvalue; + if ( pc_token->type == TT_STRING ) { + StripDoubleQuotes( pc_token->string ); + } + return ret; +} //end of the function PC_ReadTokenHandle +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_SourceFileAndLine( int handle, char *filename, int *line ) { + if ( handle < 1 || handle >= MAX_SOURCEFILES ) { + return qfalse; + } + if ( !sourceFiles[handle] ) { + return qfalse; + } + + strcpy( filename, sourceFiles[handle]->filename ); + if ( sourceFiles[handle]->scriptstack ) { + *line = sourceFiles[handle]->scriptstack->line; + } else { + *line = 0; + } + return qtrue; +} //end of the function PC_SourceFileAndLine +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_SetBaseFolder( char *path ) { + PS_SetBaseFolder( path ); +} //end of the function PC_SetBaseFolder +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_CheckOpenSourceHandles( void ) { + int i; + + for ( i = 1; i < MAX_SOURCEFILES; i++ ) + { + if ( sourceFiles[i] ) { +#ifdef BOTLIB + botimport.Print( PRT_ERROR, "file %s still open in precompiler\n", sourceFiles[i]->scriptstack->filename ); +#endif //BOTLIB + } //end if + } //end for +} //end of the function PC_CheckOpenSourceHandles diff --git a/src/botlib/l_precomp.h b/src/botlib/l_precomp.h new file mode 100644 index 0000000..e1e504f --- /dev/null +++ b/src/botlib/l_precomp.h @@ -0,0 +1,182 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: l_precomp.h + * + * desc: pre compiler + * + * + *****************************************************************************/ + +#ifndef _MAX_PATH + #define MAX_PATH MAX_QPATH +#endif + +#ifndef PATH_SEPERATORSTR + #if defined( WIN32 ) | defined( _WIN32 ) | defined( __NT__ ) | defined( __WINDOWS__ ) | defined( __WINDOWS_386__ ) + #define PATHSEPERATOR_STR "\\" + #else + #define PATHSEPERATOR_STR "/" + #endif +#endif +#ifndef PATH_SEPERATORCHAR + #if defined( WIN32 ) | defined( _WIN32 ) | defined( __NT__ ) | defined( __WINDOWS__ ) | defined( __WINDOWS_386__ ) + #define PATHSEPERATOR_CHAR '\\' + #else + #define PATHSEPERATOR_CHAR '/' + #endif +#endif + + +#define DEFINE_FIXED 0x0001 + +#define BUILTIN_LINE 1 +#define BUILTIN_FILE 2 +#define BUILTIN_DATE 3 +#define BUILTIN_TIME 4 +#define BUILTIN_STDC 5 + +#define INDENT_IF 0x0001 +#define INDENT_ELSE 0x0002 +#define INDENT_ELIF 0x0004 +#define INDENT_IFDEF 0x0008 +#define INDENT_IFNDEF 0x0010 + +//macro definitions +typedef struct define_s +{ + char *name; //define name + int flags; //define flags + int builtin; // > 0 if builtin define + int numparms; //number of define parameters + token_t *parms; //define parameters + token_t *tokens; //macro tokens (possibly containing parm tokens) + struct define_s *next; //next defined macro in a list + struct define_s *hashnext; //next define in the hash chain +} define_t; + +//indents +//used for conditional compilation directives: +//#if, #else, #elif, #ifdef, #ifndef +typedef struct indent_s +{ + int type; //indent type + int skip; //true if skipping current indent + script_t *script; //script the indent was in + struct indent_s *next; //next indent on the indent stack +} indent_t; + +//source file +typedef struct source_s +{ + char filename[_MAX_PATH]; //file name of the script + char includepath[_MAX_PATH]; //path to include files + punctuation_t *punctuations; //punctuations to use + script_t *scriptstack; //stack with scripts of the source + token_t *tokens; //tokens to read first + define_t *defines; //list with macro definitions + define_t **definehash; //hash chain with defines + indent_t *indentstack; //stack with indents + int skip; // > 0 if skipping conditional code + token_t token; //last read token +} source_t; + + +//read a token from the source +int PC_ReadToken( source_t *source, token_t *token ); +//expect a certain token +int PC_ExpectTokenString( source_t *source, char *string ); +//expect a certain token type +int PC_ExpectTokenType( source_t *source, int type, int subtype, token_t *token ); +//expect a token +int PC_ExpectAnyToken( source_t *source, token_t *token ); +//returns true when the token is available +int PC_CheckTokenString( source_t *source, char *string ); +//returns true an reads the token when a token with the given type is available +int PC_CheckTokenType( source_t *source, int type, int subtype, token_t *token ); +//skip tokens until the given token string is read +int PC_SkipUntilString( source_t *source, char *string ); +//unread the last token read from the script +void PC_UnreadLastToken( source_t *source ); +//unread the given token +void PC_UnreadToken( source_t *source, token_t *token ); +//read a token only if on the same line, lines are concatenated with a slash +int PC_ReadLine( source_t *source, token_t *token ); +//returns true if there was a white space in front of the token +int PC_WhiteSpaceBeforeToken( token_t *token ); +//add a define to the source +int PC_AddDefine( source_t *source, char *string ); +//add a globals define that will be added to all opened sources +int PC_AddGlobalDefine( char *string ); +//remove the given global define +int PC_RemoveGlobalDefine( char *name ); +//remove all globals defines +void PC_RemoveAllGlobalDefines( void ); +//add builtin defines +void PC_AddBuiltinDefines( source_t *source ); +//set the source include path +void PC_SetIncludePath( source_t *source, char *path ); +//set the punction set +void PC_SetPunctuations( source_t *source, punctuation_t *p ); +//set the base folder to load files from +void PC_SetBaseFolder( char *path ); +//load a source file +source_t *LoadSourceFile( const char *filename ); +//load a source from memory +source_t *LoadSourceMemory( char *ptr, int length, char *name ); +//free the given source +void FreeSource( source_t *source ); +//print a source error +void QDECL SourceError( source_t *source, char *str, ... ); +//print a source warning +void QDECL SourceWarning( source_t *source, char *str, ... ); + +#ifdef BSPC +// some of BSPC source does include game/q_shared.h and some does not +// we define pc_token_s pc_token_t if needed (yes, it's ugly) +#ifndef __Q_SHARED_H +#define MAX_TOKENLENGTH 1024 +typedef struct pc_token_s +{ + int type; + int subtype; + int intvalue; + float floatvalue; + char string[MAX_TOKENLENGTH]; +} pc_token_t; +#endif //!_Q_SHARED_H +#endif //BSPC + +// +int PC_LoadSourceHandle( const char *filename ); +int PC_FreeSourceHandle( int handle ); +int PC_ReadTokenHandle( int handle, struct pc_token_s *pc_token ); +int PC_SourceFileAndLine( int handle, char *filename, int *line ); +void PC_CheckOpenSourceHandles( void ); diff --git a/src/botlib/l_script.c b/src/botlib/l_script.c new file mode 100644 index 0000000..68d305c --- /dev/null +++ b/src/botlib/l_script.c @@ -0,0 +1,1452 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: l_script.c + * + * desc: lexicographical parser + * + * + *****************************************************************************/ + +//#define SCREWUP +//#define BOTLIB +//#define MEQCC +//#define BSPC + +#ifdef SCREWUP +#include +#include +#include +#include +#include +#include "../botlib/l_memory.h" +#include "../botlib/l_script.h" + +typedef enum {qfalse, qtrue} qboolean; + +#endif //SCREWUP + +#ifdef BOTLIB +//include files for usage in the bot library +#include "../game/q_shared.h" +#include "../game/botlib.h" +#include "be_interface.h" +#include "l_script.h" +#include "l_memory.h" +#include "l_log.h" +#include "l_libvar.h" +#endif //BOTLIB + +#ifdef MEQCC +//include files for usage in MrElusive's QuakeC Compiler +#include "qcc.h" +#include "l_script.h" +#include "l_memory.h" +#include "l_log.h" + +#define qtrue true +#define qfalse false +#endif //MEQCC + +#ifdef BSPC +//include files for usage in the BSP Converter +#include "../bspc/qbsp.h" +#include "../bspc/l_log.h" +#include "../bspc/l_mem.h" +int COM_Compress( char *data_p ); + +#define qtrue true +#define qfalse false +#endif //BSPC + + +#define PUNCTABLE + +//longer punctuations first +punctuation_t default_punctuations[] = +{ + //binary operators + {">>=",P_RSHIFT_ASSIGN, NULL}, + {"<<=",P_LSHIFT_ASSIGN, NULL}, + // + {"...",P_PARMS, NULL}, + //define merge operator + {"##",P_PRECOMPMERGE, NULL}, + //logic operators + {"&&",P_LOGIC_AND, NULL}, + {"||",P_LOGIC_OR, NULL}, + {">=",P_LOGIC_GEQ, NULL}, + {"<=",P_LOGIC_LEQ, NULL}, + {"==",P_LOGIC_EQ, NULL}, + {"!=",P_LOGIC_UNEQ, NULL}, + //arithmatic operators + {"*=",P_MUL_ASSIGN, NULL}, + {"/=",P_DIV_ASSIGN, NULL}, + {"%=",P_MOD_ASSIGN, NULL}, + {"+=",P_ADD_ASSIGN, NULL}, + {"-=",P_SUB_ASSIGN, NULL}, + {"++",P_INC, NULL}, + {"--",P_DEC, NULL}, + //binary operators + {"&=",P_BIN_AND_ASSIGN, NULL}, + {"|=",P_BIN_OR_ASSIGN, NULL}, + {"^=",P_BIN_XOR_ASSIGN, NULL}, + {">>",P_RSHIFT, NULL}, + {"<<",P_LSHIFT, NULL}, + //reference operators + {"->",P_POINTERREF, NULL}, + //C++ + {"::",P_CPP1, NULL}, + {".*",P_CPP2, NULL}, + //arithmatic operators + {"*",P_MUL, NULL}, + {"/",P_DIV, NULL}, + {"%",P_MOD, NULL}, + {"+",P_ADD, NULL}, + {"-",P_SUB, NULL}, + {"=",P_ASSIGN, NULL}, + //binary operators + {"&",P_BIN_AND, NULL}, + {"|",P_BIN_OR, NULL}, + {"^",P_BIN_XOR, NULL}, + {"~",P_BIN_NOT, NULL}, + //logic operators + {"!",P_LOGIC_NOT, NULL}, + {">",P_LOGIC_GREATER, NULL}, + {"<",P_LOGIC_LESS, NULL}, + //reference operator + {".",P_REF, NULL}, + //seperators + {",",P_COMMA, NULL}, + {";",P_SEMICOLON, NULL}, + //label indication + {":",P_COLON, NULL}, + //if statement + {"?",P_QUESTIONMARK, NULL}, + //embracements + {"(",P_PARENTHESESOPEN, NULL}, + {")",P_PARENTHESESCLOSE, NULL}, + {"{",P_BRACEOPEN, NULL}, + {"}",P_BRACECLOSE, NULL}, + {"[",P_SQBRACKETOPEN, NULL}, + {"]",P_SQBRACKETCLOSE, NULL}, + // + {"\\",P_BACKSLASH, NULL}, + //precompiler operator + {"#",P_PRECOMP, NULL}, +#ifdef DOLLAR + {"$",P_DOLLAR, NULL}, +#endif //DOLLAR + {NULL, 0} +}; + +#ifdef BSPC +char basefolder[MAX_PATH]; +#else +char basefolder[MAX_QPATH]; +#endif + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void PS_CreatePunctuationTable( script_t *script, punctuation_t *punctuations ) { + int i; + punctuation_t *p, *lastp, *newp; + + //get memory for the table + if ( !script->punctuationtable ) { + script->punctuationtable = (punctuation_t **) + GetMemory( 256 * sizeof( punctuation_t * ) ); + } + memset( script->punctuationtable, 0, 256 * sizeof( punctuation_t * ) ); + //add the punctuations in the list to the punctuation table + for ( i = 0; punctuations[i].p; i++ ) + { + newp = &punctuations[i]; + lastp = NULL; + //sort the punctuations in this table entry on length (longer punctuations first) + for ( p = script->punctuationtable[(unsigned int) newp->p[0]]; p; p = p->next ) + { + if ( strlen( p->p ) < strlen( newp->p ) ) { + newp->next = p; + if ( lastp ) { + lastp->next = newp; + } else { script->punctuationtable[(unsigned int) newp->p[0]] = newp;} + break; + } //end if + lastp = p; + } //end for + if ( !p ) { + newp->next = NULL; + if ( lastp ) { + lastp->next = newp; + } else { script->punctuationtable[(unsigned int) newp->p[0]] = newp;} + } //end if + } //end for +} //end of the function PS_CreatePunctuationTable +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +char *PunctuationFromNum( script_t *script, int num ) { + int i; + + for ( i = 0; script->punctuations[i].p; i++ ) + { + if ( script->punctuations[i].n == num ) { + return script->punctuations[i].p; + } + } //end for + return "unkown punctuation"; +} //end of the function PunctuationFromNum +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void QDECL ScriptError( script_t *script, char *str, ... ) { + char text[1024]; + va_list ap; + + if ( script->flags & SCFL_NOERRORS ) { + return; + } + + va_start( ap, str ); + vsprintf( text, str, ap ); + va_end( ap ); +#ifdef BOTLIB + botimport.Print( PRT_ERROR, "file %s, line %d: %s\n", script->filename, script->line, text ); +#endif //BOTLIB +#ifdef MEQCC + printf( "error: file %s, line %d: %s\n", script->filename, script->line, text ); +#endif //MEQCC +#ifdef BSPC + Log_Print( "error: file %s, line %d: %s\n", script->filename, script->line, text ); +#endif //BSPC +} //end of the function ScriptError +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void QDECL ScriptWarning( script_t *script, char *str, ... ) { + char text[1024]; + va_list ap; + + if ( script->flags & SCFL_NOWARNINGS ) { + return; + } + + va_start( ap, str ); + vsprintf( text, str, ap ); + va_end( ap ); +#ifdef BOTLIB + botimport.Print( PRT_WARNING, "file %s, line %d: %s\n", script->filename, script->line, text ); +#endif //BOTLIB +#ifdef MEQCC + printf( "warning: file %s, line %d: %s\n", script->filename, script->line, text ); +#endif //MEQCC +#ifdef BSPC + Log_Print( "warning: file %s, line %d: %s\n", script->filename, script->line, text ); +#endif //BSPC +} //end of the function ScriptWarning +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void SetScriptPunctuations( script_t *script, punctuation_t *p ) { +#ifdef PUNCTABLE + if ( p ) { + PS_CreatePunctuationTable( script, p ); + } else { PS_CreatePunctuationTable( script, default_punctuations );} +#endif //PUNCTABLE + if ( p ) { + script->punctuations = p; + } else { script->punctuations = default_punctuations;} +} //end of the function SetScriptPunctuations +//============================================================================ +// Reads spaces, tabs, C-like comments etc. +// When a newline character is found the scripts line counter is increased. +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ReadWhiteSpace( script_t *script ) { + while ( 1 ) + { + //skip white space + while ( *script->script_p <= ' ' ) + { + if ( !*script->script_p ) { + return 0; + } + if ( *script->script_p == '\n' ) { + script->line++; + } + script->script_p++; + } //end while + //skip comments + if ( *script->script_p == '/' ) { + //comments // + if ( *( script->script_p + 1 ) == '/' ) { + script->script_p++; + do + { + script->script_p++; + if ( !*script->script_p ) { + return 0; + } + } //end do + while ( *script->script_p != '\n' ); + script->line++; + script->script_p++; + if ( !*script->script_p ) { + return 0; + } + continue; + } //end if + //comments /* */ + else if ( *( script->script_p + 1 ) == '*' ) { + script->script_p++; + do + { + script->script_p++; + if ( !*script->script_p ) { + return 0; + } + if ( *script->script_p == '\n' ) { + script->line++; + } + } //end do + while ( !( *script->script_p == '*' && *( script->script_p + 1 ) == '/' ) ); + script->script_p++; + if ( !*script->script_p ) { + return 0; + } + script->script_p++; + if ( !*script->script_p ) { + return 0; + } + continue; + } //end if + } //end if + break; + } //end while + return 1; +} //end of the function PS_ReadWhiteSpace +//============================================================================ +// Reads an escape character. +// +// Parameter: script : script to read from +// ch : place to store the read escape character +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ReadEscapeCharacter( script_t *script, char *ch ) { + int c, val, i; + + //step over the leading '\\' + script->script_p++; + //determine the escape character + switch ( *script->script_p ) + { + case '\\': c = '\\'; break; + case 'n': c = '\n'; break; + case 'r': c = '\r'; break; + case 't': c = '\t'; break; + case 'v': c = '\v'; break; + case 'b': c = '\b'; break; + case 'f': c = '\f'; break; + case 'a': c = '\a'; break; + case '\'': c = '\''; break; + case '\"': c = '\"'; break; + case '\?': c = '\?'; break; + case 'x': + { + script->script_p++; + for ( i = 0, val = 0; ; i++, script->script_p++ ) + { + c = *script->script_p; + if ( c >= '0' && c <= '9' ) { + c = c - '0'; + } else if ( c >= 'A' && c <= 'Z' ) { + c = c - 'A' + 10; + } else if ( c >= 'a' && c <= 'z' ) { + c = c - 'a' + 10; + } else { break;} + val = ( val << 4 ) + c; + } //end for + script->script_p--; + if ( val > 0xFF ) { + ScriptWarning( script, "too large value in escape character" ); + val = 0xFF; + } //end if + c = val; + break; + } //end case + default: //NOTE: decimal ASCII code, NOT octal + { + if ( *script->script_p < '0' || *script->script_p > '9' ) { + ScriptError( script, "unknown escape char" ); + } + for ( i = 0, val = 0; ; i++, script->script_p++ ) + { + c = *script->script_p; + if ( c >= '0' && c <= '9' ) { + c = c - '0'; + } else { break;} + val = val * 10 + c; + } //end for + script->script_p--; + if ( val > 0xFF ) { + ScriptWarning( script, "too large value in escape character" ); + val = 0xFF; + } //end if + c = val; + break; + } //end default + } //end switch + //step over the escape character or the last digit of the number + script->script_p++; + //store the escape character + *ch = c; + //succesfully read escape character + return 1; +} //end of the function PS_ReadEscapeCharacter +//============================================================================ +// Reads C-like string. Escape characters are interpretted. +// Quotes are included with the string. +// Reads two strings with a white space between them as one string. +// +// Parameter: script : script to read from +// token : buffer to store the string +// Returns: qtrue when a string was read succesfully +// Changes Globals: - +//============================================================================ +int PS_ReadString( script_t *script, token_t *token, int quote ) { + int len, tmpline; + char *tmpscript_p; + + if ( quote == '\"' ) { + token->type = TT_STRING; + } else { token->type = TT_LITERAL;} + + len = 0; + //leading quote + token->string[len++] = *script->script_p++; + // + while ( 1 ) + { + //minus 2 because trailing double quote and zero have to be appended + if ( len >= MAX_TOKEN - 2 ) { + ScriptError( script, "string longer than MAX_TOKEN = %d", MAX_TOKEN ); + return 0; + } //end if + //if there is an escape character and + //if escape characters inside a string are allowed + if ( *script->script_p == '\\' && !( script->flags & SCFL_NOSTRINGESCAPECHARS ) ) { + if ( !PS_ReadEscapeCharacter( script, &token->string[len] ) ) { + token->string[len] = 0; + return 0; + } //end if + len++; + } //end if + //if a trailing quote + else if ( *script->script_p == quote ) { + //step over the double quote + script->script_p++; + //if white spaces in a string are not allowed + if ( script->flags & SCFL_NOSTRINGWHITESPACES ) { + break; + } + // + tmpscript_p = script->script_p; + tmpline = script->line; + //read unusefull stuff between possible two following strings + if ( !PS_ReadWhiteSpace( script ) ) { + script->script_p = tmpscript_p; + script->line = tmpline; + break; + } //end if + //if there's no leading double qoute + if ( *script->script_p != quote ) { + script->script_p = tmpscript_p; + script->line = tmpline; + break; + } //end if + //step over the new leading double quote + script->script_p++; + } //end if + else + { + if ( *script->script_p == '\0' ) { + token->string[len] = 0; + ScriptError( script, "missing trailing quote" ); + return 0; + } //end if + if ( *script->script_p == '\n' ) { + token->string[len] = 0; + ScriptError( script, "newline inside string %s", token->string ); + return 0; + } //end if + token->string[len++] = *script->script_p++; + } //end else + } //end while + //trailing quote + token->string[len++] = quote; + //end string with a zero + token->string[len] = '\0'; + //the sub type is the length of the string + token->subtype = len; + return 1; +} //end of the function PS_ReadString +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ReadName( script_t *script, token_t *token ) { + int len = 0; + char c; + + token->type = TT_NAME; + do + { + token->string[len++] = *script->script_p++; + if ( len >= MAX_TOKEN ) { + ScriptError( script, "name longer than MAX_TOKEN = %d", MAX_TOKEN ); + return 0; + } //end if + c = *script->script_p; + } while ( ( c >= 'a' && c <= 'z' ) || + ( c >= 'A' && c <= 'Z' ) || + ( c >= '0' && c <= '9' ) || + c == '_' ); + token->string[len] = '\0'; + //the sub type is the length of the name + token->subtype = len; + return 1; +} //end of the function PS_ReadName +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void NumberValue( char *string, int subtype, unsigned long int *intvalue, + long double *floatvalue ) { + unsigned long int dotfound = 0; + + *intvalue = 0; + *floatvalue = 0; + //floating point number + if ( subtype & TT_FLOAT ) { + while ( *string ) + { + if ( *string == '.' ) { + if ( dotfound ) { + return; + } + dotfound = 10; + string++; + } //end if + if ( dotfound ) { + *floatvalue = *floatvalue + ( long double )( *string - '0' ) / + (long double) dotfound; + dotfound *= 10; + } //end if + else + { + *floatvalue = *floatvalue * 10.0 + ( long double )( *string - '0' ); + } //end else + string++; + } //end while + *intvalue = (unsigned long) *floatvalue; + } //end if + else if ( subtype & TT_DECIMAL ) { + while ( *string ) *intvalue = *intvalue * 10 + ( *string++ - '0' ); + *floatvalue = *intvalue; + } //end else if + else if ( subtype & TT_HEX ) { + //step over the leading 0x or 0X + string += 2; + while ( *string ) + { + *intvalue <<= 4; + if ( *string >= 'a' && *string <= 'f' ) { + *intvalue += *string - 'a' + 10; + } else if ( *string >= 'A' && *string <= 'F' ) { + *intvalue += *string - 'A' + 10; + } else { *intvalue += *string - '0';} + string++; + } //end while + *floatvalue = *intvalue; + } //end else if + else if ( subtype & TT_OCTAL ) { + //step over the first zero + string += 1; + while ( *string ) *intvalue = ( *intvalue << 3 ) + ( *string++ - '0' ); + *floatvalue = *intvalue; + } //end else if + else if ( subtype & TT_BINARY ) { + //step over the leading 0b or 0B + string += 2; + while ( *string ) *intvalue = ( *intvalue << 1 ) + ( *string++ - '0' ); + *floatvalue = *intvalue; + } //end else if +} //end of the function NumberValue +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ReadNumber( script_t *script, token_t *token ) { + int len = 0, i; + int octal, dot; + char c; +// unsigned long int intvalue = 0; +// long double floatvalue = 0; + + token->type = TT_NUMBER; + //check for a hexadecimal number + if ( *script->script_p == '0' && + ( *( script->script_p + 1 ) == 'x' || + *( script->script_p + 1 ) == 'X' ) ) { + token->string[len++] = *script->script_p++; + token->string[len++] = *script->script_p++; + c = *script->script_p; + //hexadecimal + while ( ( c >= '0' && c <= '9' ) || + ( c >= 'a' && c <= 'f' ) || + ( c >= 'A' && c <= 'A' ) ) + { + token->string[len++] = *script->script_p++; + if ( len >= MAX_TOKEN ) { + ScriptError( script, "hexadecimal number longer than MAX_TOKEN = %d", MAX_TOKEN ); + return 0; + } //end if + c = *script->script_p; + } //end while + token->subtype |= TT_HEX; + } //end if +#ifdef BINARYNUMBERS + //check for a binary number + else if ( *script->script_p == '0' && + ( *( script->script_p + 1 ) == 'b' || + *( script->script_p + 1 ) == 'B' ) ) { + token->string[len++] = *script->script_p++; + token->string[len++] = *script->script_p++; + c = *script->script_p; + //hexadecimal + while ( c == '0' || c == '1' ) + { + token->string[len++] = *script->script_p++; + if ( len >= MAX_TOKEN ) { + ScriptError( script, "binary number longer than MAX_TOKEN = %d", MAX_TOKEN ); + return 0; + } //end if + c = *script->script_p; + } //end while + token->subtype |= TT_BINARY; + } //end if +#endif //BINARYNUMBERS + else //decimal or octal integer or floating point number + { + octal = qfalse; + dot = qfalse; + if ( *script->script_p == '0' ) { + octal = qtrue; + } + while ( 1 ) + { + c = *script->script_p; + if ( c == '.' ) { + dot = qtrue; + } else if ( c == '8' || c == '9' ) { + octal = qfalse; + } else if ( c < '0' || c > '9' ) { + break; + } + token->string[len++] = *script->script_p++; + if ( len >= MAX_TOKEN - 1 ) { + ScriptError( script, "number longer than MAX_TOKEN = %d", MAX_TOKEN ); + return 0; + } //end if + } //end while + if ( octal ) { + token->subtype |= TT_OCTAL; + } else { token->subtype |= TT_DECIMAL;} + if ( dot ) { + token->subtype |= TT_FLOAT; + } + } //end else + for ( i = 0; i < 2; i++ ) + { + c = *script->script_p; + //check for a LONG number + // TTimo: gcc: suggest parentheses around && within || + // initial line: + // if (c == 'l' || c == 'L' && !(token->subtype & TT_LONG)) + // changed behaviour + if ( ( c == 'l' || c == 'L' ) && + !( token->subtype & TT_LONG ) ) { + script->script_p++; + token->subtype |= TT_LONG; + } //end if + //check for an UNSIGNED number + // TTimo: gcc: suggest parentheses around && within || + // initial line: + // else if (c == 'u' || c == 'U' && !(token->subtype & (TT_UNSIGNED | TT_FLOAT))) + // changed behaviour + else if ( ( c == 'u' || c == 'U' ) && + !( token->subtype & ( TT_UNSIGNED | TT_FLOAT ) ) ) { + script->script_p++; + token->subtype |= TT_UNSIGNED; + } //end if + } //end for + token->string[len] = '\0'; +#ifdef NUMBERVALUE + NumberValue( token->string, token->subtype, &token->intvalue, &token->floatvalue ); +#endif //NUMBERVALUE + if ( !( token->subtype & TT_FLOAT ) ) { + token->subtype |= TT_INTEGER; + } + return 1; +} //end of the function PS_ReadNumber +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ReadLiteral( script_t *script, token_t *token ) { + token->type = TT_LITERAL; + //first quote + token->string[0] = *script->script_p++; + //check for end of file + if ( !*script->script_p ) { + ScriptError( script, "end of file before trailing \'" ); + return 0; + } //end if + //if it is an escape character + if ( *script->script_p == '\\' ) { + if ( !PS_ReadEscapeCharacter( script, &token->string[1] ) ) { + return 0; + } + } //end if + else + { + token->string[1] = *script->script_p++; + } //end else + //check for trailing quote + if ( *script->script_p != '\'' ) { + ScriptWarning( script, "too many characters in literal, ignored" ); + while ( *script->script_p && + *script->script_p != '\'' && + *script->script_p != '\n' ) + { + script->script_p++; + } //end while + if ( *script->script_p == '\'' ) { + script->script_p++; + } + } //end if + //store the trailing quote + token->string[2] = *script->script_p++; + //store trailing zero to end the string + token->string[3] = '\0'; + //the sub type is the integer literal value + token->subtype = token->string[1]; + // + return 1; +} //end of the function PS_ReadLiteral +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ReadPunctuation( script_t *script, token_t *token ) { + int len; + char *p; + punctuation_t *punc; + +#ifdef PUNCTABLE + for ( punc = script->punctuationtable[(unsigned int)*script->script_p]; punc; punc = punc->next ) + { +#else + int i; + + for ( i = 0; script->punctuations[i].p; i++ ) + { + punc = &script->punctuations[i]; +#endif //PUNCTABLE + p = punc->p; + len = strlen( p ); + //if the script contains at least as much characters as the punctuation + if ( script->script_p + len <= script->end_p ) { + //if the script contains the punctuation + if ( !strncmp( script->script_p, p, len ) ) { + strncpy( token->string, p, MAX_TOKEN ); + script->script_p += len; + token->type = TT_PUNCTUATION; + //sub type is the number of the punctuation + token->subtype = punc->n; + return 1; + } //end if + } //end if + } //end for + return 0; +} //end of the function PS_ReadPunctuation +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ReadPrimitive( script_t *script, token_t *token ) { + int len; + + len = 0; + while ( *script->script_p > ' ' && *script->script_p != ';' ) + { + if ( len >= MAX_TOKEN ) { + ScriptError( script, "primitive token longer than MAX_TOKEN = %d", MAX_TOKEN ); + return 0; + } //end if + token->string[len++] = *script->script_p++; + } //end while + token->string[len] = 0; + //copy the token into the script structure + memcpy( &script->token, token, sizeof( token_t ) ); + //primitive reading successfull + return 1; +} //end of the function PS_ReadPrimitive +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ReadToken( script_t *script, token_t *token ) { + //if there is a token available (from UnreadToken) + if ( script->tokenavailable ) { + script->tokenavailable = 0; + memcpy( token, &script->token, sizeof( token_t ) ); + return 1; + } //end if + //save script pointer + script->lastscript_p = script->script_p; + //save line counter + script->lastline = script->line; + //clear the token stuff + memset( token, 0, sizeof( token_t ) ); + //start of the white space + script->whitespace_p = script->script_p; + token->whitespace_p = script->script_p; + //read unusefull stuff + if ( !PS_ReadWhiteSpace( script ) ) { + return 0; + } + //end of the white space + script->endwhitespace_p = script->script_p; + token->endwhitespace_p = script->script_p; + //line the token is on + token->line = script->line; + //number of lines crossed before token + token->linescrossed = script->line - script->lastline; + //if there is a leading double quote + if ( *script->script_p == '\"' ) { + if ( !PS_ReadString( script, token, '\"' ) ) { + return 0; + } + } //end if + //if an literal + else if ( *script->script_p == '\'' ) { + //if (!PS_ReadLiteral(script, token)) return 0; + if ( !PS_ReadString( script, token, '\'' ) ) { + return 0; + } + } //end if + //if there is a number + else if ( ( *script->script_p >= '0' && *script->script_p <= '9' ) || + ( *script->script_p == '.' && + ( *( script->script_p + 1 ) >= '0' && *( script->script_p + 1 ) <= '9' ) ) ) { + if ( !PS_ReadNumber( script, token ) ) { + return 0; + } + } //end if + //if this is a primitive script + else if ( script->flags & SCFL_PRIMITIVE ) { + return PS_ReadPrimitive( script, token ); + } //end else if + //if there is a name + else if ( ( *script->script_p >= 'a' && *script->script_p <= 'z' ) || + ( *script->script_p >= 'A' && *script->script_p <= 'Z' ) || + *script->script_p == '_' ) { + if ( !PS_ReadName( script, token ) ) { + return 0; + } + } //end if + //check for punctuations + else if ( !PS_ReadPunctuation( script, token ) ) { + ScriptError( script, "can't read token" ); + return 0; + } //end if + //copy the token into the script structure + memcpy( &script->token, token, sizeof( token_t ) ); + //succesfully read a token + return 1; +} //end of the function PS_ReadToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ExpectTokenString( script_t *script, char *string ) { + token_t token; + + if ( !PS_ReadToken( script, &token ) ) { + ScriptError( script, "couldn't find expected %s", string ); + return 0; + } //end if + + if ( strcmp( token.string, string ) ) { + ScriptError( script, "expected %s, found %s", string, token.string ); + return 0; + } //end if + return 1; +} //end of the function PS_ExpectToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ExpectTokenType( script_t *script, int type, int subtype, token_t *token ) { + char str[MAX_TOKEN]; + + if ( !PS_ReadToken( script, token ) ) { + ScriptError( script, "couldn't read expected token" ); + return 0; + } //end if + + if ( token->type != type ) { + if ( type == TT_STRING ) { + strcpy( str, "string" ); + } + if ( type == TT_LITERAL ) { + strcpy( str, "literal" ); + } + if ( type == TT_NUMBER ) { + strcpy( str, "number" ); + } + if ( type == TT_NAME ) { + strcpy( str, "name" ); + } + if ( type == TT_PUNCTUATION ) { + strcpy( str, "punctuation" ); + } + ScriptError( script, "expected a %s, found %s", str, token->string ); + return 0; + } //end if + if ( token->type == TT_NUMBER ) { + if ( ( token->subtype & subtype ) != subtype ) { + if ( subtype & TT_DECIMAL ) { + strcpy( str, "decimal" ); + } + if ( subtype & TT_HEX ) { + strcpy( str, "hex" ); + } + if ( subtype & TT_OCTAL ) { + strcpy( str, "octal" ); + } + if ( subtype & TT_BINARY ) { + strcpy( str, "binary" ); + } + if ( subtype & TT_LONG ) { + strcat( str, " long" ); + } + if ( subtype & TT_UNSIGNED ) { + strcat( str, " unsigned" ); + } + if ( subtype & TT_FLOAT ) { + strcat( str, " float" ); + } + if ( subtype & TT_INTEGER ) { + strcat( str, " integer" ); + } + ScriptError( script, "expected %s, found %s", str, token->string ); + return 0; + } //end if + } //end if + else if ( token->type == TT_PUNCTUATION ) { + if ( subtype < 0 ) { + ScriptError( script, "BUG: wrong punctuation subtype" ); + return 0; + } //end if + if ( token->subtype != subtype ) { + ScriptError( script, "expected %s, found %s", + script->punctuations[subtype], token->string ); + return 0; + } //end if + } //end else if + return 1; +} //end of the function PS_ExpectTokenType +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ExpectAnyToken( script_t *script, token_t *token ) { + if ( !PS_ReadToken( script, token ) ) { + ScriptError( script, "couldn't read expected token" ); + return 0; + } //end if + else + { + return 1; + } //end else +} //end of the function PS_ExpectAnyToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_CheckTokenString( script_t *script, char *string ) { + token_t tok; + + if ( !PS_ReadToken( script, &tok ) ) { + return 0; + } + //if the token is available + if ( !strcmp( tok.string, string ) ) { + return 1; + } + //token not available + script->script_p = script->lastscript_p; + return 0; +} //end of the function PS_CheckTokenString +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_CheckTokenType( script_t *script, int type, int subtype, token_t *token ) { + token_t tok; + + if ( !PS_ReadToken( script, &tok ) ) { + return 0; + } + //if the type matches + if ( tok.type == type && + ( tok.subtype & subtype ) == subtype ) { + memcpy( token, &tok, sizeof( token_t ) ); + return 1; + } //end if + //token is not available + script->script_p = script->lastscript_p; + return 0; +} //end of the function PS_CheckTokenType +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_SkipUntilString( script_t *script, char *string ) { + token_t token; + + while ( PS_ReadToken( script, &token ) ) + { + if ( !strcmp( token.string, string ) ) { + return 1; + } + } //end while + return 0; +} //end of the function PS_SkipUntilString +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PS_UnreadLastToken( script_t *script ) { + script->tokenavailable = 1; +} //end of the function UnreadLastToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PS_UnreadToken( script_t *script, token_t *token ) { + memcpy( &script->token, token, sizeof( token_t ) ); + script->tokenavailable = 1; +} //end of the function UnreadToken +//============================================================================ +// returns the next character of the read white space, returns NULL if none +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +char PS_NextWhiteSpaceChar( script_t *script ) { + if ( script->whitespace_p != script->endwhitespace_p ) { + return *script->whitespace_p++; + } //end if + else + { + return 0; + } //end else +} //end of the function PS_NextWhiteSpaceChar +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void StripDoubleQuotes( char *string ) { + if ( *string == '\"' ) { + strcpy( string, string + 1 ); + } //end if + if ( string[strlen( string ) - 1] == '\"' ) { + string[strlen( string ) - 1] = '\0'; + } //end if +} //end of the function StripDoubleQuotes +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void StripSingleQuotes( char *string ) { + if ( *string == '\'' ) { + strcpy( string, string + 1 ); + } //end if + if ( string[strlen( string ) - 1] == '\'' ) { + string[strlen( string ) - 1] = '\0'; + } //end if +} //end of the function StripSingleQuotes +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +long double ReadSignedFloat( script_t *script ) { + token_t token; + long double sign = 1; + + PS_ExpectAnyToken( script, &token ); + if ( !strcmp( token.string, "-" ) ) { + sign = -1; + PS_ExpectTokenType( script, TT_NUMBER, 0, &token ); + } //end if + else if ( token.type != TT_NUMBER ) { + ScriptError( script, "expected float value, found %s\n", token.string ); + } //end else if + return sign * token.floatvalue; +} //end of the function ReadSignedFloat +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +signed long int ReadSignedInt( script_t *script ) { + token_t token; + signed long int sign = 1; + + PS_ExpectAnyToken( script, &token ); + if ( !strcmp( token.string, "-" ) ) { + sign = -1; + PS_ExpectTokenType( script, TT_NUMBER, TT_INTEGER, &token ); + } //end if + else if ( token.type != TT_NUMBER || token.subtype == TT_FLOAT ) { + ScriptError( script, "expected integer value, found %s\n", token.string ); + } //end else if + return sign * token.intvalue; +} //end of the function ReadSignedInt +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void SetScriptFlags( script_t *script, int flags ) { + script->flags = flags; +} //end of the function SetScriptFlags +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int GetScriptFlags( script_t *script ) { + return script->flags; +} //end of the function GetScriptFlags +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void ResetScript( script_t *script ) { + //pointer in script buffer + script->script_p = script->buffer; + //pointer in script buffer before reading token + script->lastscript_p = script->buffer; + //begin of white space + script->whitespace_p = NULL; + //end of white space + script->endwhitespace_p = NULL; + //set if there's a token available in script->token + script->tokenavailable = 0; + // + script->line = 1; + script->lastline = 1; + //clear the saved token + memset( &script->token, 0, sizeof( token_t ) ); +} //end of the function ResetScript +//============================================================================ +// returns true if at the end of the script +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int EndOfScript( script_t *script ) { + return script->script_p >= script->end_p; +} //end of the function EndOfScript +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int NumLinesCrossed( script_t *script ) { + return script->line - script->lastline; +} //end of the function NumLinesCrossed +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int ScriptSkipTo( script_t *script, char *value ) { + int len; + char firstchar; + + firstchar = *value; + len = strlen( value ); + do + { + if ( !PS_ReadWhiteSpace( script ) ) { + return 0; + } + if ( *script->script_p == firstchar ) { + if ( !strncmp( script->script_p, value, len ) ) { + return 1; + } //end if + } //end if + script->script_p++; + } while ( 1 ); +} //end of the function ScriptSkipTo +#ifndef BOTLIB +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int FileLength( FILE *fp ) { + int pos; + int end; + + pos = ftell( fp ); + fseek( fp, 0, SEEK_END ); + end = ftell( fp ); + fseek( fp, pos, SEEK_SET ); + + return end; +} //end of the function FileLength +#endif +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +script_t *LoadScriptFile( const char *filename ) { +#ifdef BOTLIB + fileHandle_t fp; + char pathname[MAX_QPATH]; +#else + FILE *fp; +#endif + int length; + void *buffer; + script_t *script; + +#ifdef BOTLIB + if ( strlen( basefolder ) ) { + Com_sprintf( pathname, sizeof( pathname ), "%s/%s", basefolder, filename ); + } else { + Com_sprintf( pathname, sizeof( pathname ), "%s", filename ); + } + length = botimport.FS_FOpenFile( pathname, &fp, FS_READ ); + if ( !fp ) { + return NULL; + } +#else + fp = fopen( filename, "rb" ); + if ( !fp ) { + return NULL; + } + + length = FileLength( fp ); +#endif + + buffer = GetClearedMemory( sizeof( script_t ) + length + 1 ); + script = (script_t *) buffer; + memset( script, 0, sizeof( script_t ) ); + strcpy( script->filename, filename ); + script->buffer = (char *) buffer + sizeof( script_t ); + script->buffer[length] = 0; + script->length = length; + //pointer in script buffer + script->script_p = script->buffer; + //pointer in script buffer before reading token + script->lastscript_p = script->buffer; + //pointer to end of script buffer + script->end_p = &script->buffer[length]; + //set if there's a token available in script->token + script->tokenavailable = 0; + // + script->line = 1; + script->lastline = 1; + // + SetScriptPunctuations( script, NULL ); + // +#ifdef BOTLIB + botimport.FS_Read( script->buffer, length, fp ); + botimport.FS_FCloseFile( fp ); +#else + if ( fread( script->buffer, length, 1, fp ) != 1 ) { + FreeMemory( buffer ); + script = NULL; + } //end if + fclose( fp ); +#endif + // + script->length = COM_Compress( script->buffer ); + + return script; +} //end of the function LoadScriptFile +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +script_t *LoadScriptMemory( char *ptr, int length, char *name ) { + void *buffer; + script_t *script; + + buffer = GetClearedMemory( sizeof( script_t ) + length + 1 ); + script = (script_t *) buffer; + memset( script, 0, sizeof( script_t ) ); + strcpy( script->filename, name ); + script->buffer = (char *) buffer + sizeof( script_t ); + script->buffer[length] = 0; + script->length = length; + //pointer in script buffer + script->script_p = script->buffer; + //pointer in script buffer before reading token + script->lastscript_p = script->buffer; + //pointer to end of script buffer + script->end_p = &script->buffer[length]; + //set if there's a token available in script->token + script->tokenavailable = 0; + // + script->line = 1; + script->lastline = 1; + // + SetScriptPunctuations( script, NULL ); + // + memcpy( script->buffer, ptr, length ); + // + return script; +} //end of the function LoadScriptMemory +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void FreeScript( script_t *script ) { +#ifdef PUNCTABLE + if ( script->punctuationtable ) { + FreeMemory( script->punctuationtable ); + } +#endif //PUNCTABLE + FreeMemory( script ); +} //end of the function FreeScript +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PS_SetBaseFolder( char *path ) { +#ifdef BSPC + sprintf( basefolder, path ); +#else + Com_sprintf( basefolder, sizeof( basefolder ), path ); +#endif +} //end of the function PS_SetBaseFolder diff --git a/src/botlib/l_script.h b/src/botlib/l_script.h new file mode 100644 index 0000000..461276e --- /dev/null +++ b/src/botlib/l_script.h @@ -0,0 +1,268 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: l_script.h + * + * desc: lexicographical parser + * + * + *****************************************************************************/ + +// Ridah, can't get it to compile without this +#ifndef QDECL + +// for windows fastcall option +#define QDECL +//======================= WIN32 DEFINES ================================= +#ifdef WIN32 +#undef QDECL +#define QDECL __cdecl +#endif +#endif +// done. + +//undef if binary numbers of the form 0b... or 0B... are not allowed +#define BINARYNUMBERS +//undef if not using the token.intvalue and token.floatvalue +#define NUMBERVALUE +//use dollar sign also as punctuation +#define DOLLAR + +//maximum token length +#define MAX_TOKEN 1024 +//maximum path length +#ifndef _MAX_PATH +// TTimo: used to be MAX_QPATH, which is the game filesystem max len, and not the OS max len + #define _MAX_PATH 1024 +#endif + + +//script flags +#define SCFL_NOERRORS 0x0001 +#define SCFL_NOWARNINGS 0x0002 +#define SCFL_NOSTRINGWHITESPACES 0x0004 +#define SCFL_NOSTRINGESCAPECHARS 0x0008 +#define SCFL_PRIMITIVE 0x0010 +#define SCFL_NOBINARYNUMBERS 0x0020 +#define SCFL_NONUMBERVALUES 0x0040 + +//token types +#define TT_STRING 1 // string +#define TT_LITERAL 2 // literal +#define TT_NUMBER 3 // number +#define TT_NAME 4 // name +#define TT_PUNCTUATION 5 // punctuation + +//string sub type +//--------------- +// the length of the string +//literal sub type +//---------------- +// the ASCII code of the literal +//number sub type +//--------------- +#define TT_DECIMAL 0x0008 // decimal number +#define TT_HEX 0x0100 // hexadecimal number +#define TT_OCTAL 0x0200 // octal number +#ifdef BINARYNUMBERS +#define TT_BINARY 0x0400 // binary number +#endif //BINARYNUMBERS +#define TT_FLOAT 0x0800 // floating point number +#define TT_INTEGER 0x1000 // integer number +#define TT_LONG 0x2000 // long number +#define TT_UNSIGNED 0x4000 // unsigned number +//punctuation sub type +//-------------------- +#define P_RSHIFT_ASSIGN 1 +#define P_LSHIFT_ASSIGN 2 +#define P_PARMS 3 +#define P_PRECOMPMERGE 4 + +#define P_LOGIC_AND 5 +#define P_LOGIC_OR 6 +#define P_LOGIC_GEQ 7 +#define P_LOGIC_LEQ 8 +#define P_LOGIC_EQ 9 +#define P_LOGIC_UNEQ 10 + +#define P_MUL_ASSIGN 11 +#define P_DIV_ASSIGN 12 +#define P_MOD_ASSIGN 13 +#define P_ADD_ASSIGN 14 +#define P_SUB_ASSIGN 15 +#define P_INC 16 +#define P_DEC 17 + +#define P_BIN_AND_ASSIGN 18 +#define P_BIN_OR_ASSIGN 19 +#define P_BIN_XOR_ASSIGN 20 +#define P_RSHIFT 21 +#define P_LSHIFT 22 + +#define P_POINTERREF 23 +#define P_CPP1 24 +#define P_CPP2 25 +#define P_MUL 26 +#define P_DIV 27 +#define P_MOD 28 +#define P_ADD 29 +#define P_SUB 30 +#define P_ASSIGN 31 + +#define P_BIN_AND 32 +#define P_BIN_OR 33 +#define P_BIN_XOR 34 +#define P_BIN_NOT 35 + +#define P_LOGIC_NOT 36 +#define P_LOGIC_GREATER 37 +#define P_LOGIC_LESS 38 + +#define P_REF 39 +#define P_COMMA 40 +#define P_SEMICOLON 41 +#define P_COLON 42 +#define P_QUESTIONMARK 43 + +#define P_PARENTHESESOPEN 44 +#define P_PARENTHESESCLOSE 45 +#define P_BRACEOPEN 46 +#define P_BRACECLOSE 47 +#define P_SQBRACKETOPEN 48 +#define P_SQBRACKETCLOSE 49 +#define P_BACKSLASH 50 + +#define P_PRECOMP 51 +#define P_DOLLAR 52 +//name sub type +//------------- +// the length of the name + +//punctuation +typedef struct punctuation_s +{ + char *p; //punctuation character(s) + int n; //punctuation indication + struct punctuation_s *next; //next punctuation +} punctuation_t; + +//token +typedef struct token_s +{ + char string[MAX_TOKEN]; //available token + int type; //last read token type + int subtype; //last read token sub type +#ifdef NUMBERVALUE + unsigned long int intvalue; //integer value + long double floatvalue; //floating point value +#endif //NUMBERVALUE + char *whitespace_p; //start of white space before token + char *endwhitespace_p; //start of white space before token + int line; //line the token was on + int linescrossed; //lines crossed in white space + struct token_s *next; //next token in chain +} token_t; + +//script file +typedef struct script_s +{ + char filename[_MAX_PATH]; //file name of the script + char *buffer; //buffer containing the script + char *script_p; //current pointer in the script + char *end_p; //pointer to the end of the script + char *lastscript_p; //script pointer before reading token + char *whitespace_p; //begin of the white space + char *endwhitespace_p; //end of the white space + int length; //length of the script in bytes + int line; //current line in script + int lastline; //line before reading token + int tokenavailable; //set by UnreadLastToken + int flags; //several script flags + punctuation_t *punctuations; //the punctuations used in the script + punctuation_t **punctuationtable; + token_t token; //available token + struct script_s *next; //next script in a chain +} script_t; + +//read a token from the script +int PS_ReadToken( script_t *script, token_t *token ); +//expect a certain token +int PS_ExpectTokenString( script_t *script, char *string ); +//expect a certain token type +int PS_ExpectTokenType( script_t *script, int type, int subtype, token_t *token ); +//expect a token +int PS_ExpectAnyToken( script_t *script, token_t *token ); +//returns true when the token is available +int PS_CheckTokenString( script_t *script, char *string ); +//returns true an reads the token when a token with the given type is available +int PS_CheckTokenType( script_t *script, int type, int subtype, token_t *token ); +//skip tokens until the given token string is read +int PS_SkipUntilString( script_t *script, char *string ); +//unread the last token read from the script +void PS_UnreadLastToken( script_t *script ); +//unread the given token +void PS_UnreadToken( script_t *script, token_t *token ); +//returns the next character of the read white space, returns NULL if none +char PS_NextWhiteSpaceChar( script_t *script ); +//remove any leading and trailing double quotes from the token +void StripDoubleQuotes( char *string ); +//remove any leading and trailing single quotes from the token +void StripSingleQuotes( char *string ); +//read a possible signed integer +signed long int ReadSignedInt( script_t *script ); +//read a possible signed floating point number +long double ReadSignedFloat( script_t *script ); +//set an array with punctuations, NULL restores default C/C++ set +void SetScriptPunctuations( script_t *script, punctuation_t *p ); +//set script flags +void SetScriptFlags( script_t *script, int flags ); +//get script flags +int GetScriptFlags( script_t *script ); +//reset a script +void ResetScript( script_t *script ); +//returns true if at the end of the script +int EndOfScript( script_t *script ); +//returns a pointer to the punctuation with the given number +char *PunctuationFromNum( script_t *script, int num ); +//load a script from the given file at the given offset with the given length +script_t *LoadScriptFile( const char *filename ); +//load a script from the given memory with the given length +script_t *LoadScriptMemory( char *ptr, int length, char *name ); +//free a script +void FreeScript( script_t *script ); +//set the base folder to load files from +void PS_SetBaseFolder( char *path ); +//print a script error with filename and line number +void QDECL ScriptError( script_t *script, char *str, ... ); +//print a script warning with filename and line number +void QDECL ScriptWarning( script_t *script, char *str, ... ); + + + diff --git a/src/botlib/l_struct.c b/src/botlib/l_struct.c new file mode 100644 index 0000000..a698f36 --- /dev/null +++ b/src/botlib/l_struct.c @@ -0,0 +1,507 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: l_struct.c + * + * desc: structure reading / writing + * + * + *****************************************************************************/ + +#ifdef BOTLIB +#include "../game/q_shared.h" +#include "../game/botlib.h" //for the include of be_interface.h +#include "l_script.h" +#include "l_precomp.h" +#include "l_struct.h" +#include "l_utils.h" +#include "be_interface.h" +#endif //BOTLIB + +#ifdef BSPC +//include files for usage in the BSP Converter +#include "../bspc/qbsp.h" +#include "../bspc/l_log.h" +#include "../bspc/l_mem.h" +#include "l_precomp.h" +#include "l_struct.h" + +#define qtrue true +#define qfalse false +#endif //BSPC + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +fielddef_t *FindField( fielddef_t *defs, char *name ) { + int i; + + for ( i = 0; defs[i].name; i++ ) + { + if ( !strcmp( defs[i].name, name ) ) { + return &defs[i]; + } + } //end for + return NULL; +} //end of the function FindField +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean ReadNumber( source_t *source, fielddef_t *fd, void *p ) { + token_t token; + int negative = qfalse; + long int intval, intmin = 0, intmax = 0; + double floatval; + + if ( !PC_ExpectAnyToken( source, &token ) ) { + return 0; + } + + //check for minus sign + if ( token.type == TT_PUNCTUATION ) { + if ( fd->type & FT_UNSIGNED ) { + SourceError( source, "expected unsigned value, found %s", token.string ); + return 0; + } //end if + //if not a minus sign + if ( strcmp( token.string, "-" ) ) { + SourceError( source, "unexpected punctuation %s", token.string ); + return 0; + } //end if + negative = qtrue; + //read the number + if ( !PC_ExpectAnyToken( source, &token ) ) { + return 0; + } + } //end if + //check if it is a number + if ( token.type != TT_NUMBER ) { + SourceError( source, "expected number, found %s", token.string ); + return 0; + } //end if + //check for a float value + if ( token.subtype & TT_FLOAT ) { + if ( ( fd->type & FT_TYPE ) != FT_FLOAT ) { + SourceError( source, "unexpected float" ); + return 0; + } //end if + floatval = token.floatvalue; + if ( negative ) { + floatval = -floatval; + } + if ( fd->type & FT_BOUNDED ) { + if ( floatval < fd->floatmin || floatval > fd->floatmax ) { + SourceError( source, "float out of range [%f, %f]", fd->floatmin, fd->floatmax ); + return 0; + } //end if + } //end if + *(float *) p = (float) floatval; + return 1; + } //end if + // + intval = token.intvalue; + if ( negative ) { + intval = -intval; + } + //check bounds + if ( ( fd->type & FT_TYPE ) == FT_CHAR ) { + if ( fd->type & FT_UNSIGNED ) { + intmin = 0; intmax = 255; + } else {intmin = -128; intmax = 127;} + } //end if + if ( ( fd->type & FT_TYPE ) == FT_INT ) { + if ( fd->type & FT_UNSIGNED ) { + intmin = 0; intmax = 65535; + } else {intmin = -32768; intmax = 32767;} + } //end else if + if ( ( fd->type & FT_TYPE ) == FT_CHAR || ( fd->type & FT_TYPE ) == FT_INT ) { + if ( fd->type & FT_BOUNDED ) { + intmin = Maximum( intmin, fd->floatmin ); + intmax = Minimum( intmax, fd->floatmax ); + } //end if + if ( intval < intmin || intval > intmax ) { + SourceError( source, "value %d out of range [%d, %d]", intval, intmin, intmax ); + return 0; + } //end if + } //end if + else if ( ( fd->type & FT_TYPE ) == FT_FLOAT ) { + if ( fd->type & FT_BOUNDED ) { + if ( intval < fd->floatmin || intval > fd->floatmax ) { + SourceError( source, "value %d out of range [%f, %f]", intval, fd->floatmin, fd->floatmax ); + return 0; + } //end if + } //end if + } //end else if + //store the value + if ( ( fd->type & FT_TYPE ) == FT_CHAR ) { + if ( fd->type & FT_UNSIGNED ) { + *(unsigned char *) p = (unsigned char) intval; + } else { *(char *) p = (char) intval;} + } //end if + else if ( ( fd->type & FT_TYPE ) == FT_INT ) { + if ( fd->type & FT_UNSIGNED ) { + *(unsigned int *) p = (unsigned int) intval; + } else { *(int *) p = (int) intval;} + } //end else + else if ( ( fd->type & FT_TYPE ) == FT_FLOAT ) { + *(float *) p = (float) intval; + } //end else + return 1; +} //end of the function ReadNumber +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean ReadChar( source_t *source, fielddef_t *fd, void *p ) { + token_t token; + + if ( !PC_ExpectAnyToken( source, &token ) ) { + return 0; + } + + //take literals into account + if ( token.type == TT_LITERAL ) { + StripSingleQuotes( token.string ); + *(char *) p = token.string[0]; + } //end if + else + { + PC_UnreadLastToken( source ); + if ( !ReadNumber( source, fd, p ) ) { + return 0; + } + } //end if + return 1; +} //end of the function ReadChar +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int ReadString( source_t *source, fielddef_t *fd, void *p ) { + token_t token; + + if ( !PC_ExpectTokenType( source, TT_STRING, 0, &token ) ) { + return 0; + } + //remove the double quotes + StripDoubleQuotes( token.string ); + //copy the string + strncpy( (char *) p, token.string, MAX_STRINGFIELD ); + //make sure the string is closed with a zero + ( (char *)p )[MAX_STRINGFIELD - 1] = '\0'; + // + return 1; +} //end of the function ReadString +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int ReadStructure( source_t *source, structdef_t *def, char *structure ) { + token_t token; + fielddef_t *fd; + void *p; + int num; + + if ( !PC_ExpectTokenString( source, "{" ) ) { + return 0; + } + while ( 1 ) + { + if ( !PC_ExpectAnyToken( source, &token ) ) { + return qfalse; + } + //if end of structure + if ( !strcmp( token.string, "}" ) ) { + break; + } + //find the field with the name + fd = FindField( def->fields, token.string ); + if ( !fd ) { + SourceError( source, "unknown structure field %s", token.string ); + return qfalse; + } //end if + if ( fd->type & FT_ARRAY ) { + num = fd->maxarray; + if ( !PC_ExpectTokenString( source, "{" ) ) { + return qfalse; + } + } //end if + else + { + num = 1; + } //end else + p = ( void * )( structure + fd->offset ); + while ( num-- > 0 ) + { + if ( fd->type & FT_ARRAY ) { + if ( PC_CheckTokenString( source, "}" ) ) { + break; + } + } //end if + switch ( fd->type & FT_TYPE ) + { + case FT_CHAR: + { + if ( !ReadChar( source, fd, p ) ) { + return qfalse; + } + p = (char *) p + sizeof( char ); + break; + } //end case + case FT_INT: + { + if ( !ReadNumber( source, fd, p ) ) { + return qfalse; + } + p = (char *) p + sizeof( int ); + break; + } //end case + case FT_FLOAT: + { + if ( !ReadNumber( source, fd, p ) ) { + return qfalse; + } + p = (char *) p + sizeof( float ); + break; + } //end case + case FT_STRING: + { + if ( !ReadString( source, fd, p ) ) { + return qfalse; + } + p = (char *) p + MAX_STRINGFIELD; + break; + } //end case + case FT_STRUCT: + { + if ( !fd->substruct ) { + SourceError( source, "BUG: no sub structure defined" ); + return qfalse; + } //end if + ReadStructure( source, fd->substruct, (char *) p ); + p = (char *) p + fd->substruct->size; + break; + } //end case + } //end switch + if ( fd->type & FT_ARRAY ) { + if ( !PC_ExpectAnyToken( source, &token ) ) { + return qfalse; + } + if ( !strcmp( token.string, "}" ) ) { + break; + } + if ( strcmp( token.string, "," ) ) { + SourceError( source, "expected a comma, found %s", token.string ); + return qfalse; + } //end if + } //end if + } //end while + } //end while + return qtrue; +} //end of the function ReadStructure +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int WriteIndent( FILE *fp, int indent ) { + while ( indent-- > 0 ) + { + if ( fprintf( fp, "\t" ) < 0 ) { + return qfalse; + } + } //end while + return qtrue; +} //end of the function WriteIndent +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int WriteFloat( FILE *fp, float value ) { + char buf[128]; + int l; + + sprintf( buf, "%f", value ); + l = strlen( buf ); + //strip any trailing zeros + while ( l-- > 1 ) + { + if ( buf[l] != '0' && buf[l] != '.' ) { + break; + } + if ( buf[l] == '.' ) { + buf[l] = 0; + break; + } //end if + buf[l] = 0; + } //end while + //write the float to file + if ( fprintf( fp, "%s", buf ) < 0 ) { + return 0; + } + return 1; +} //end of the function WriteFloat +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int WriteStructWithIndent( FILE *fp, structdef_t *def, char *structure, int indent ) { + int i, num; + void *p; + fielddef_t *fd; + + if ( !WriteIndent( fp, indent ) ) { + return qfalse; + } + if ( fprintf( fp, "{\r\n" ) < 0 ) { + return qfalse; + } + + indent++; + for ( i = 0; def->fields[i].name; i++ ) + { + fd = &def->fields[i]; + if ( !WriteIndent( fp, indent ) ) { + return qfalse; + } + if ( fprintf( fp, "%s\t", fd->name ) < 0 ) { + return qfalse; + } + p = ( void * )( structure + fd->offset ); + if ( fd->type & FT_ARRAY ) { + num = fd->maxarray; + if ( fprintf( fp, "{" ) < 0 ) { + return qfalse; + } + } //end if + else + { + num = 1; + } //end else + while ( num-- > 0 ) + { + switch ( fd->type & FT_TYPE ) + { + case FT_CHAR: + { + if ( fprintf( fp, "%d", *(char *) p ) < 0 ) { + return qfalse; + } + p = (char *) p + sizeof( char ); + break; + } //end case + case FT_INT: + { + if ( fprintf( fp, "%d", *(int *) p ) < 0 ) { + return qfalse; + } + p = (char *) p + sizeof( int ); + break; + } //end case + case FT_FLOAT: + { + if ( !WriteFloat( fp, *(float *)p ) ) { + return qfalse; + } + p = (char *) p + sizeof( float ); + break; + } //end case + case FT_STRING: + { + if ( fprintf( fp, "\"%s\"", (char *) p ) < 0 ) { + return qfalse; + } + p = (char *) p + MAX_STRINGFIELD; + break; + } //end case + case FT_STRUCT: + { + if ( !WriteStructWithIndent( fp, fd->substruct, structure, indent ) ) { + return qfalse; + } + p = (char *) p + fd->substruct->size; + break; + } //end case + } //end switch + if ( fd->type & FT_ARRAY ) { + if ( num > 0 ) { + if ( fprintf( fp, "," ) < 0 ) { + return qfalse; + } + } //end if + else + { + if ( fprintf( fp, "}" ) < 0 ) { + return qfalse; + } + } //end else + } //end if + } //end while + if ( fprintf( fp, "\r\n" ) < 0 ) { + return qfalse; + } + } //end for + indent--; + + if ( !WriteIndent( fp, indent ) ) { + return qfalse; + } + if ( fprintf( fp, "}\r\n" ) < 0 ) { + return qfalse; + } + return qtrue; +} //end of the function WriteStructWithIndent +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int WriteStructure( FILE *fp, structdef_t *def, char *structure ) { + return WriteStructWithIndent( fp, def, structure, 0 ); +} //end of the function WriteStructure + diff --git a/src/botlib/l_struct.h b/src/botlib/l_struct.h new file mode 100644 index 0000000..7b7f441 --- /dev/null +++ b/src/botlib/l_struct.h @@ -0,0 +1,81 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: l_struct.h + * + * desc: structure reading/writing + * + * + *****************************************************************************/ + + +#define MAX_STRINGFIELD 80 +//field types +#define FT_CHAR 1 // char +#define FT_INT 2 // int +#define FT_FLOAT 3 // float +#define FT_STRING 4 // char [MAX_STRINGFIELD] +#define FT_STRUCT 6 // struct (sub structure) +//type only mask +#define FT_TYPE 0x00FF // only type, clear subtype +//sub types +#define FT_ARRAY 0x0100 // array of type +#define FT_BOUNDED 0x0200 // bounded value +#define FT_UNSIGNED 0x0400 + +//structure field definition +typedef struct fielddef_s +{ + char *name; //name of the field + int offset; //offset in the structure + int type; //type of the field + //type specific fields + int maxarray; //maximum array size + float floatmin, floatmax; //float min and max + struct structdef_s *substruct; //sub structure +} fielddef_t; + +//structure definition +typedef struct structdef_s +{ + int size; + fielddef_t *fields; +} structdef_t; + +//read a structure from a script +int ReadStructure( source_t *source, structdef_t *def, char *structure ); +//write a structure to a file +int WriteStructure( FILE *fp, structdef_t *def, char *structure ); +//writes indents +int WriteIndent( FILE *fp, int indent ); +//writes a float without traling zeros +int WriteFloat( FILE *fp, float value ); + + diff --git a/src/botlib/l_utils.h b/src/botlib/l_utils.h new file mode 100644 index 0000000..c419299 --- /dev/null +++ b/src/botlib/l_utils.h @@ -0,0 +1,41 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: l_util.h + * + * desc: utils + * + * + *****************************************************************************/ + +#define Vector2Angles( v,a ) vectoangles( v,a ) +#define MAX_PATH MAX_QPATH +#define Maximum( x,y ) ( x > y ? x : y ) +#define Minimum( x,y ) ( x < y ? x : y ) diff --git a/src/bspc/_files.c b/src/bspc/_files.c new file mode 100644 index 0000000..d34525a --- /dev/null +++ b/src/bspc/_files.c @@ -0,0 +1,91 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: _files.c +// Function: +// Programmer: Mr Elusive +// Last update: 1999-12-02 +// Tab Size: 4 +//=========================================================================== + +/* + +aas_areamerging.c //AAS area merging +aas_cfg.c //AAS configuration for different games +aas_create.c //AAS creating +aas_edgemelting.c //AAS edge melting +aas_facemerging.c //AAS face merging +aas_file.c //AAS file writing +aas_gsubdiv.c //AAS gravitational and ladder subdivision +aas_map.c //AAS map brush creation +aas_prunenodes.c //AAS node pruning +aas_store.c //AAS file storing + +map.c //map file loading and writing +map_hl.c //Half-Life map loading +map_q1.c //Quake1 map loading +map_q2.c //Quake2 map loading +map_q3.c //Quake3 map loading +map_sin.c //Sin map loading +tree.c //BSP tree management + node pruning (*) +brushbsp.c //brush bsp creation (*) +portals.c //BSP portal creation and leaf filling (*) +csg.c //Constructive Solid Geometry brush chopping (*) +leakfile.c //leak file writing (*) +textures.c //Quake2 BSP textures (*) + +l_bsp_ent.c //BSP entity parsing +l_bsp_hl.c //Half-Life BSP loading and writing +l_bsp_q1.c //Quake1 BSP loading and writing +l_bsp_q2.c //Quake2 BSP loading and writing +l_bsp_q3.c //Quake2 BSP loading and writing +l_bsp_sin.c //Sin BSP loading and writing +l_cmd.c //cmd library +l_log.c //log file library +l_math.c //math library +l_mem.c //memory management library +l_poly.c //polygon (winding) library +l_script.c //script file parsing library +l_threads.c //multi-threading library +l_utils.c //utility library +l_qfiles.c //loading of quake files + +gldraw.c //GL drawing (*) +glfile.c //GL file writing (*) +nodraw.c //no draw module (*) + +bspc.c //BSPC Win32 console version +winbspc.c //WinBSPC Win32 GUI version +win32_terminal.c //Win32 terminal output +win32_qfiles.c //Win32 game file management (also .pak .sin) +win32_font.c //Win32 fonts +win32_folder.c //Win32 folder dialogs + +*/ diff --git a/src/bspc/aas_areamerging.c b/src/bspc/aas_areamerging.c new file mode 100644 index 0000000..f3e3c9f --- /dev/null +++ b/src/bspc/aas_areamerging.c @@ -0,0 +1,424 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: aas_areamerging.c +// Function: Merging of Areas +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + +#include "qbsp.h" +#include "../botlib/aasfile.h" +#include "aas_create.h" +#include "aas_store.h" + +#define CONVEX_EPSILON 0.3 + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +tmp_node_t *AAS_RefreshMergedTree_r( tmp_node_t *tmpnode ) { + tmp_area_t *tmparea; + + //if this is a solid leaf + if ( !tmpnode ) { + return NULL; + } + //if this is an area leaf + if ( tmpnode->tmparea ) { + tmparea = tmpnode->tmparea; + while ( tmparea->mergedarea ) tmparea = tmparea->mergedarea; + tmpnode->tmparea = tmparea; + return tmpnode; + } //end if + //do the children recursively + tmpnode->children[0] = AAS_RefreshMergedTree_r( tmpnode->children[0] ); + tmpnode->children[1] = AAS_RefreshMergedTree_r( tmpnode->children[1] ); + return tmpnode; +} //end of the function AAS_RefreshMergedTree_r +//=========================================================================== +// returns true if the two given faces would create a non-convex area at +// the given sides, otherwise false is returned +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int NonConvex( tmp_face_t *face1, tmp_face_t *face2, int side1, int side2 ) { + int i; + winding_t *w1, *w2; + plane_t *plane1, *plane2; + + w1 = face1->winding; + w2 = face2->winding; + + plane1 = &mapplanes[face1->planenum ^ side1]; + plane2 = &mapplanes[face2->planenum ^ side2]; + + //check if one of the points of face1 is at the back of the plane of face2 + for ( i = 0; i < w1->numpoints; i++ ) + { + if ( DotProduct( plane2->normal, w1->p[i] ) - plane2->dist < -CONVEX_EPSILON ) { + return true; + } + } //end for + //check if one of the points of face2 is at the back of the plane of face1 + for ( i = 0; i < w2->numpoints; i++ ) + { + if ( DotProduct( plane1->normal, w2->p[i] ) - plane1->dist < -CONVEX_EPSILON ) { + return true; + } + } //end for + + return false; +} //end of the function NonConvex +//=========================================================================== +// try to merge the areas at both sides of the given face +// +// Parameter: seperatingface : face that seperates two areas +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_TryMergeFaceAreas( tmp_face_t *seperatingface ) { + int side1, side2, area1faceflags, area2faceflags; + tmp_area_t *tmparea1, *tmparea2, *newarea; + tmp_face_t *face1, *face2, *nextface1, *nextface2; + + tmparea1 = seperatingface->frontarea; + tmparea2 = seperatingface->backarea; + + //areas must have the same presence type + if ( tmparea1->presencetype != tmparea2->presencetype ) { + return false; + } + //areas must have the same area contents + if ( tmparea1->contents != tmparea2->contents ) { + return false; + } + //areas must have the same bsp model inside (or both none) + if ( tmparea1->modelnum != tmparea2->modelnum ) { + return false; + } + + area1faceflags = 0; + area2faceflags = 0; + for ( face1 = tmparea1->tmpfaces; face1; face1 = face1->next[side1] ) + { + side1 = ( face1->frontarea != tmparea1 ); + //debug: check if the area belongs to the area + if ( face1->frontarea != tmparea1 && + face1->backarea != tmparea1 ) { + Error( "face does not belong to area1" ); + } + //just continue if the face is seperating the two areas + //NOTE: a result of this is that ground and gap areas can + // be merged if the seperating face is the gap + if ( ( face1->frontarea == tmparea1 && + face1->backarea == tmparea2 ) || + ( face1->frontarea == tmparea2 && + face1->backarea == tmparea1 ) ) { + continue; + } + //get area1 face flags + area1faceflags |= face1->faceflags; + if ( AAS_GapFace( face1, side1 ) ) { + area1faceflags |= FACE_GAP; + } + // + for ( face2 = tmparea2->tmpfaces; face2; face2 = face2->next[side2] ) + { + side2 = ( face2->frontarea != tmparea2 ); + //debug: check if the area belongs to the area + if ( face2->frontarea != tmparea2 && + face2->backarea != tmparea2 ) { + Error( "face does not belong to area2" ); + } + //just continue if the face is seperating the two areas + //NOTE: a result of this is that ground and gap areas can + // be merged if the seperating face is the gap + if ( ( face2->frontarea == tmparea1 && + face2->backarea == tmparea2 ) || + ( face2->frontarea == tmparea2 && + face2->backarea == tmparea1 ) ) { + continue; + } + //get area2 face flags + area2faceflags |= face2->faceflags; + if ( AAS_GapFace( face2, side2 ) ) { + area2faceflags |= FACE_GAP; + } + //if the two faces would create a non-convex area + if ( NonConvex( face1, face2, side1, side2 ) ) { + return false; + } + } //end for + } //end for + //if one area has gap faces (that aren't seperating the two areas) + //and the other has ground faces (that aren't seperating the two areas), + //the areas can't be merged + if ( ( ( area1faceflags & FACE_GROUND ) && ( area2faceflags & FACE_GAP ) ) || + ( ( area2faceflags & FACE_GROUND ) && ( area1faceflags & FACE_GAP ) ) ) { +// Log_Print(" can't merge: ground/gap\n"); + return false; + } //end if + +// Log_Print("merged area %d & %d to %d with %d faces\n", tmparea1->areanum, tmparea2->areanum, newarea->areanum, numfaces); +// return false; + // + //AAS_CheckArea(tmparea1); + //AAS_CheckArea(tmparea2); + //create the new area + newarea = AAS_AllocTmpArea(); + newarea->presencetype = tmparea1->presencetype; + newarea->contents = tmparea1->contents; + newarea->modelnum = tmparea1->modelnum; + newarea->tmpfaces = NULL; + + //add all the faces (except the seperating ones) from the first area + //to the new area + for ( face1 = tmparea1->tmpfaces; face1; face1 = nextface1 ) + { + side1 = ( face1->frontarea != tmparea1 ); + nextface1 = face1->next[side1]; + //don't add seperating faces + if ( ( face1->frontarea == tmparea1 && + face1->backarea == tmparea2 ) || + ( face1->frontarea == tmparea2 && + face1->backarea == tmparea1 ) ) { + continue; + } //end if + // + AAS_RemoveFaceFromArea( face1, tmparea1 ); + AAS_AddFaceSideToArea( face1, side1, newarea ); + } //end for + //add all the faces (except the seperating ones) from the second area + //to the new area + for ( face2 = tmparea2->tmpfaces; face2; face2 = nextface2 ) + { + side2 = ( face2->frontarea != tmparea2 ); + nextface2 = face2->next[side2]; + //don't add seperating faces + if ( ( face2->frontarea == tmparea1 && + face2->backarea == tmparea2 ) || + ( face2->frontarea == tmparea2 && + face2->backarea == tmparea1 ) ) { + continue; + } //end if + // + AAS_RemoveFaceFromArea( face2, tmparea2 ); + AAS_AddFaceSideToArea( face2, side2, newarea ); + } //end for + //free all shared faces + for ( face1 = tmparea1->tmpfaces; face1; face1 = nextface1 ) + { + side1 = ( face1->frontarea != tmparea1 ); + nextface1 = face1->next[side1]; + // + AAS_RemoveFaceFromArea( face1, face1->frontarea ); + AAS_RemoveFaceFromArea( face1, face1->backarea ); + AAS_FreeTmpFace( face1 ); + } //end for + // + tmparea1->mergedarea = newarea; + tmparea1->invalid = true; + tmparea2->mergedarea = newarea; + tmparea2->invalid = true; + // + AAS_CheckArea( newarea ); + AAS_FlipAreaFaces( newarea ); +// Log_Print("merged area %d & %d to %d with %d faces\n", tmparea1->areanum, tmparea2->areanum, newarea->areanum); + return true; +} //end of the function AAS_TryMergeFaceAreas +//=========================================================================== +// try to merge areas +// merged areas are added to the end of the convex area list so merging +// will be tried for those areas as well +// +// Parameter: - +// Returns: - +// Changes Globals: tmpaasworld +//=========================================================================== +/* +void AAS_MergeAreas(void) +{ + int side, nummerges; + tmp_area_t *tmparea, *othertmparea; + tmp_face_t *face; + + nummerges = 0; + Log_Write("AAS_MergeAreas\r\n"); + qprintf("%6d areas merged", 1); + //first merge grounded areas only + //NOTE: this is useless because the area settings aren't available yet + for (tmparea = tmpaasworld.areas; tmparea; tmparea = tmparea->l_next) + { +// Log_Print("checking area %d\n", i); + //if the area is invalid + if (tmparea->invalid) + { +// Log_Print(" area invalid\n"); + continue; + } //end if + // +// if (!(tmparea->settings->areaflags & AREA_GROUNDED)) continue; + // + for (face = tmparea->tmpfaces; face; face = face->next[side]) + { + side = (face->frontarea != tmparea); + //if the face has both a front and back area + if (face->frontarea && face->backarea) + { + // + if (face->frontarea == tmparea) othertmparea = face->backarea; + else othertmparea = face->frontarea; +// if (!(othertmparea->settings->areaflags & AREA_GROUNDED)) continue; +// Log_Print(" checking area %d with %d\n", face->frontarea, face->backarea); + if (AAS_TryMergeFaceAreas(face)) + { + qprintf("\r%6d", ++nummerges); + break; + } //end if + } //end if + } //end for + } //end for + //merge all areas + for (tmparea = tmpaasworld.areas; tmparea; tmparea = tmparea->l_next) + { +// Log_Print("checking area %d\n", i); + //if the area is invalid + if (tmparea->invalid) + { +// Log_Print(" area invalid\n"); + continue; + } //end if + // + for (face = tmparea->tmpfaces; face; face = face->next[side]) + { + side = (face->frontarea != tmparea); + //if the face has both a front and back area + if (face->frontarea && face->backarea) + { +// Log_Print(" checking area %d with %d\n", face->frontarea, face->backarea); + if (AAS_TryMergeFaceAreas(face)) + { + qprintf("\r%6d", ++nummerges); + break; + } //end if + } //end if + } //end for + } //end for + Log_Print("\r%6d areas merged\n", nummerges); + //refresh the merged tree + AAS_RefreshMergedTree_r(tmpaasworld.nodes); +} //end of the function AAS_MergeAreas*/ + +int AAS_GroundArea( tmp_area_t *tmparea ) { + tmp_face_t *face; + int side; + + for ( face = tmparea->tmpfaces; face; face = face->next[side] ) + { + side = ( face->frontarea != tmparea ); + if ( face->faceflags & FACE_GROUND ) { + return true; + } + } //end for + return false; +} //end of the function AAS_GroundArea + +void AAS_MergeAreas( void ) { + int side, nummerges, merges, groundfirst; + tmp_area_t *tmparea, *othertmparea; + tmp_face_t *face; + + nummerges = 0; + Log_Write( "AAS_MergeAreas\r\n" ); + qprintf( "%6d areas merged", 1 ); + // + groundfirst = true; + //for (i = 0; i < 4 || merges; i++) + while ( 1 ) + { + //if (i < 2) groundfirst = true; + //else groundfirst = false; + // + merges = 0; + //first merge grounded areas only + for ( tmparea = tmpaasworld.areas; tmparea; tmparea = tmparea->l_next ) + { + //if the area is invalid + if ( tmparea->invalid ) { + continue; + } //end if + // + if ( groundfirst ) { + if ( !AAS_GroundArea( tmparea ) ) { + continue; + } + } //end if + // + for ( face = tmparea->tmpfaces; face; face = face->next[side] ) + { + side = ( face->frontarea != tmparea ); + //if the face has both a front and back area + if ( face->frontarea && face->backarea ) { + // + if ( face->frontarea == tmparea ) { + othertmparea = face->backarea; + } else { othertmparea = face->frontarea;} + // + if ( groundfirst ) { + if ( !AAS_GroundArea( othertmparea ) ) { + continue; + } + } //end if + if ( AAS_TryMergeFaceAreas( face ) ) { + qprintf( "\r%6d", ++nummerges ); + merges++; + break; + } //end if + } //end if + } //end for + } //end for + if ( !merges ) { + if ( groundfirst ) { + groundfirst = false; + } else { break;} + } //end if + } //end for + qprintf( "\n" ); + Log_Write( "%6d areas merged\r\n", nummerges ); + //refresh the merged tree + AAS_RefreshMergedTree_r( tmpaasworld.nodes ); +} //end of the function AAS_MergeAreas diff --git a/src/bspc/aas_areamerging.h b/src/bspc/aas_areamerging.h new file mode 100644 index 0000000..4221942 --- /dev/null +++ b/src/bspc/aas_areamerging.h @@ -0,0 +1,40 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: aas_areamerging.h +// Function: Merging of Areas +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + + +void AAS_MergeAreas( void ); + diff --git a/src/bspc/aas_cfg.c b/src/bspc/aas_cfg.c new file mode 100644 index 0000000..2388787 --- /dev/null +++ b/src/bspc/aas_cfg.c @@ -0,0 +1,317 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: cfg.c +// Function: +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + +#include "qbsp.h" +#include "float.h" +#include "../botlib/aasfile.h" +#include "aas_store.h" +#include "aas_cfg.h" +#include "../botlib/l_precomp.h" +#include "../botlib/l_struct.h" +#include "../botlib/l_libvar.h" + +// TTimo: this is a f*g mess +// I got rid of all occurences except for bspc it seems +// all code *should* be using Q_stricmp +#ifdef BSPC +#define stricmp strcasecmp +#endif + +/////////////////////////////////// +extern void LibVarSet( char *var_name, char *value ); +/////////////////////////////////// + +//structure field offsets +#define BBOX_OFS( x ) (int)&( ( (aas_bbox_t *)0 )->x ) +#define CFG_OFS( x ) (int)&( ( (cfg_t *)0 )->x ) + +//bounding box definition +fielddef_t bbox_fields[] = +{ + {"presencetype", BBOX_OFS( presencetype ), FT_INT}, + {"flags", BBOX_OFS( flags ), FT_INT}, + {"mins", BBOX_OFS( mins ), FT_FLOAT | FT_ARRAY, 3}, + {"maxs", BBOX_OFS( maxs ), FT_FLOAT | FT_ARRAY, 3}, + {NULL, 0, 0, 0} +}; + +fielddef_t cfg_fields[] = +{ + {"phys_gravitydirection", CFG_OFS( phys_gravitydirection ), FT_FLOAT | FT_ARRAY, 3}, + {"phys_friction", CFG_OFS( phys_friction ), FT_FLOAT}, + {"phys_stopspeed", CFG_OFS( phys_stopspeed ), FT_FLOAT}, + {"phys_gravity", CFG_OFS( phys_gravity ), FT_FLOAT}, + {"phys_waterfriction", CFG_OFS( phys_waterfriction ), FT_FLOAT}, + {"phys_watergravity", CFG_OFS( phys_watergravity ), FT_FLOAT}, + {"phys_maxvelocity", CFG_OFS( phys_maxvelocity ), FT_FLOAT}, + {"phys_maxwalkvelocity", CFG_OFS( phys_maxwalkvelocity ), FT_FLOAT}, + {"phys_maxcrouchvelocity", CFG_OFS( phys_maxcrouchvelocity ), FT_FLOAT}, + {"phys_maxswimvelocity", CFG_OFS( phys_maxswimvelocity ), FT_FLOAT}, + {"phys_walkaccelerate", CFG_OFS( phys_walkaccelerate ), FT_FLOAT}, + {"phys_airaccelerate", CFG_OFS( phys_airaccelerate ), FT_FLOAT}, + {"phys_swimaccelerate", CFG_OFS( phys_swimaccelerate ), FT_FLOAT}, + {"phys_maxstep", CFG_OFS( phys_maxstep ), FT_FLOAT}, + {"phys_maxsteepness", CFG_OFS( phys_maxsteepness ), FT_FLOAT}, + {"phys_maxwaterjump", CFG_OFS( phys_maxwaterjump ), FT_FLOAT}, + {"phys_maxbarrier", CFG_OFS( phys_maxbarrier ), FT_FLOAT}, + {"phys_jumpvel", CFG_OFS( phys_jumpvel ), FT_FLOAT}, + {"phys_falldelta5", CFG_OFS( phys_falldelta5 ), FT_FLOAT}, + {"phys_falldelta10", CFG_OFS( phys_falldelta10 ), FT_FLOAT}, + {"rs_waterjump", CFG_OFS( rs_waterjump ), FT_FLOAT}, + {"rs_teleport", CFG_OFS( rs_teleport ), FT_FLOAT}, + {"rs_barrierjump", CFG_OFS( rs_barrierjump ), FT_FLOAT}, + {"rs_startcrouch", CFG_OFS( rs_startcrouch ), FT_FLOAT}, + {"rs_startgrapple", CFG_OFS( rs_startgrapple ), FT_FLOAT}, + {"rs_startwalkoffledge", CFG_OFS( rs_startwalkoffledge ), FT_FLOAT}, + {"rs_startjump", CFG_OFS( rs_startjump ), FT_FLOAT}, + {"rs_rocketjump", CFG_OFS( rs_rocketjump ), FT_FLOAT}, + {"rs_bfgjump", CFG_OFS( rs_bfgjump ), FT_FLOAT}, + {"rs_jumppad", CFG_OFS( rs_jumppad ), FT_FLOAT}, + {"rs_aircontrolledjumppad", CFG_OFS( rs_aircontrolledjumppad ), FT_FLOAT}, + {"rs_funcbob", CFG_OFS( rs_funcbob ), FT_FLOAT}, + {"rs_startelevator", CFG_OFS( rs_startelevator ), FT_FLOAT}, + {"rs_falldamage5", CFG_OFS( rs_falldamage5 ), FT_FLOAT}, + {"rs_falldamage10", CFG_OFS( rs_falldamage10 ), FT_FLOAT}, + {"rs_maxjumpfallheight", CFG_OFS( rs_maxjumpfallheight ), FT_FLOAT}, + {"rs_allowladders", CFG_OFS( rs_allowladders ), FT_INT}, + {NULL, 0, 0, 0} +}; + +structdef_t bbox_struct = +{ + sizeof( aas_bbox_t ), bbox_fields +}; +structdef_t cfg_struct = +{ + sizeof( cfg_t ), cfg_fields +}; + +//global cfg +cfg_t cfg; + +#if 0 +//=========================================================================== +// the default Q3A configuration +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void DefaultCfg( void ) { + int i; + + // default all float values to infinite + for ( i = 0; cfg_fields[i].name; i++ ) + { + if ( ( cfg_fields[i].type & FT_TYPE ) == FT_FLOAT ) { + *( float * )( ( (char*)&cfg ) + cfg_fields[i].offset ) = FLT_MAX; + } + } //end for + // + cfg.numbboxes = 2; + //bbox 0 + cfg.bboxes[0].presencetype = PRESENCE_NORMAL; + cfg.bboxes[0].flags = 0; + cfg.bboxes[0].mins[0] = -18; + cfg.bboxes[0].mins[1] = -18; + cfg.bboxes[0].mins[2] = -24; + cfg.bboxes[0].maxs[0] = 18; + cfg.bboxes[0].maxs[1] = 18; + cfg.bboxes[0].maxs[2] = 48; + //bbox 1 + cfg.bboxes[1].presencetype = PRESENCE_CROUCH; + cfg.bboxes[1].flags = 1; + cfg.bboxes[1].mins[0] = -18; + cfg.bboxes[1].mins[1] = -18; + cfg.bboxes[1].mins[2] = -24; + cfg.bboxes[1].maxs[0] = 18; + cfg.bboxes[1].maxs[1] = 18; + cfg.bboxes[1].maxs[2] = 24; + // + cfg.allpresencetypes = PRESENCE_NORMAL | PRESENCE_CROUCH; + cfg.phys_gravitydirection[0] = 0; + cfg.phys_gravitydirection[1] = 0; + cfg.phys_gravitydirection[2] = -1; + cfg.phys_maxsteepness = 0.7; + +// cfg.phys_maxbarrier = -999;//32; // RIDAH: this is calculated at run-time now, from the gravity and jump velocity settings +} //end of the function DefaultCfg +#else +//=========================================================================== +// the default Q3A configuration +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void DefaultCfg( void ) { + int i; + + // default all float values to infinite + for ( i = 0; cfg_fields[i].name; i++ ) + { + if ( ( cfg_fields[i].type & FT_TYPE ) == FT_FLOAT ) { + *( float * )( ( (char*)&cfg ) + cfg_fields[i].offset ) = FLT_MAX; + } + } //end for + // + cfg.numbboxes = 2; + //bbox 0 + cfg.bboxes[0].presencetype = PRESENCE_NORMAL; + cfg.bboxes[0].flags = 0; + cfg.bboxes[0].mins[0] = -15; + cfg.bboxes[0].mins[1] = -15; + cfg.bboxes[0].mins[2] = -24; + cfg.bboxes[0].maxs[0] = 15; + cfg.bboxes[0].maxs[1] = 15; + cfg.bboxes[0].maxs[2] = 32; + //bbox 1 + cfg.bboxes[1].presencetype = PRESENCE_CROUCH; + cfg.bboxes[1].flags = 1; + cfg.bboxes[1].mins[0] = -15; + cfg.bboxes[1].mins[1] = -15; + cfg.bboxes[1].mins[2] = -24; + cfg.bboxes[1].maxs[0] = 15; + cfg.bboxes[1].maxs[1] = 15; + cfg.bboxes[1].maxs[2] = 16; + // + cfg.allpresencetypes = PRESENCE_NORMAL | PRESENCE_CROUCH; + cfg.phys_gravitydirection[0] = 0; + cfg.phys_gravitydirection[1] = 0; + cfg.phys_gravitydirection[2] = -1; + cfg.phys_maxsteepness = 0.7; + +// cfg.phys_maxbarrier = -999;//32; // RIDAH: this is calculated at run-time now, from the gravity and jump velocity settings +} //end of the function DefaultCfg +#endif + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +char * QDECL va( char *format, ... ) { + va_list argptr; + static char string[2][32000]; // in case va is called by nested functions + static int index = 0; + char *buf; + + buf = string[index & 1]; + index++; + + va_start( argptr, format ); + vsprintf( buf, format,argptr ); + va_end( argptr ); + + return buf; +} //end of the function va +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void SetCfgLibVars( void ) { + int i; + float value; + + for ( i = 0; cfg_fields[i].name; i++ ) + { + if ( ( cfg_fields[i].type & FT_TYPE ) == FT_FLOAT ) { + value = *( float * )( ( (char*)&cfg ) + cfg_fields[i].offset ); + if ( value != FLT_MAX ) { + LibVarSet( cfg_fields[i].name, va( "%f", value ) ); + } //end if + } //end if + } //end for +} //end of the function SetCfgLibVars +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int LoadCfgFile( char *filename ) { + source_t *source; + token_t token; + int settingsdefined; + + source = LoadSourceFile( filename ); + if ( !source ) { + Log_Print( "couldn't open cfg file %s\n", filename ); + return false; + } //end if + + settingsdefined = false; + memset( &cfg, 0, sizeof( cfg_t ) ); + + while ( PC_ReadToken( source, &token ) ) + { + if ( !stricmp( token.string, "bbox" ) ) { + if ( cfg.numbboxes >= AAS_MAX_BBOXES ) { + SourceError( source, "too many bounding box volumes defined" ); + } //end if + if ( !ReadStructure( source, &bbox_struct, (char *) &cfg.bboxes[cfg.numbboxes] ) ) { + FreeSource( source ); + return false; + } //end if + cfg.allpresencetypes |= cfg.bboxes[cfg.numbboxes].presencetype; + cfg.numbboxes++; + } //end if + else if ( !stricmp( token.string, "settings" ) ) { + if ( settingsdefined ) { + SourceWarning( source, "settings already defined\n" ); + } //end if + settingsdefined = true; + if ( !ReadStructure( source, &cfg_struct, (char *) &cfg ) ) { + FreeSource( source ); + return false; + } //end if + } //end else if + } //end while + if ( VectorLength( cfg.phys_gravitydirection ) < 0.9 || VectorLength( cfg.phys_gravitydirection ) > 1.1 ) { + SourceError( source, "invalid gravity direction specified" ); + } //end if + if ( cfg.numbboxes <= 0 ) { + SourceError( source, "no bounding volumes specified" ); + } //end if + FreeSource( source ); + SetCfgLibVars(); + Log_Print( "using cfg file %s\n", filename ); + return true; +} //end of the function LoadCfgFile diff --git a/src/bspc/aas_cfg.h b/src/bspc/aas_cfg.h new file mode 100644 index 0000000..e14c419 --- /dev/null +++ b/src/bspc/aas_cfg.h @@ -0,0 +1,89 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: cfg.h +// Function: +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + +#define BBOXFL_GROUNDED 1 //bounding box only valid when on ground +#define BBOXFL_NOTGROUNDED 2 //bounding box only valid when NOT on ground + +typedef struct cfg_s +{ + int numbboxes; //number of bounding boxes + aas_bbox_t bboxes[AAS_MAX_BBOXES]; //all the bounding boxes + int allpresencetypes; //or of all presence types + // aas settings + vec3_t phys_gravitydirection; + float phys_friction; + float phys_stopspeed; + float phys_gravity; + float phys_waterfriction; + float phys_watergravity; + float phys_maxvelocity; + float phys_maxwalkvelocity; + float phys_maxcrouchvelocity; + float phys_maxswimvelocity; + float phys_walkaccelerate; + float phys_airaccelerate; + float phys_swimaccelerate; + float phys_maxstep; + float phys_maxsteepness; + float phys_maxwaterjump; + float phys_maxbarrier; + float phys_jumpvel; + float phys_falldelta5; + float phys_falldelta10; + float rs_waterjump; + float rs_teleport; + float rs_barrierjump; + float rs_startcrouch; + float rs_startgrapple; + float rs_startwalkoffledge; + float rs_startjump; + float rs_rocketjump; + float rs_bfgjump; + float rs_jumppad; + float rs_aircontrolledjumppad; + float rs_funcbob; + float rs_startelevator; + float rs_falldamage5; + float rs_falldamage10; + float rs_maxjumpfallheight; + qboolean rs_allowladders; +} cfg_t; + +extern cfg_t cfg; + +void DefaultCfg( void ); +int LoadCfgFile( char *filename ); diff --git a/src/bspc/aas_create.c b/src/bspc/aas_create.c new file mode 100644 index 0000000..bdf6bd0 --- /dev/null +++ b/src/bspc/aas_create.c @@ -0,0 +1,1180 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: aas_create.c +// Function: Creation of AAS +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + +#include "qbsp.h" +#include "../botlib/aasfile.h" +#include "aas_create.h" +#include "aas_store.h" +#include "aas_gsubdiv.h" +#include "aas_facemerging.h" +#include "aas_areamerging.h" +#include "aas_edgemelting.h" +#include "aas_prunenodes.h" +#include "aas_cfg.h" +#include "../game/surfaceflags.h" + +#define AREAONFACESIDE( face, area ) ( face->frontarea != area ) + +tmp_aas_t tmpaasworld; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_InitTmpAAS( void ) { + //tmp faces + tmpaasworld.numfaces = 0; + tmpaasworld.facenum = 0; + tmpaasworld.faces = NULL; + //tmp convex areas + tmpaasworld.numareas = 0; + tmpaasworld.areanum = 0; + tmpaasworld.areas = NULL; + //tmp nodes + tmpaasworld.numnodes = 0; + tmpaasworld.nodes = NULL; + // + tmpaasworld.nodebuffer = NULL; +} //end of the function AAS_InitTmpAAS +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FreeTmpAAS( void ) { + tmp_face_t *f, *nextf; + tmp_area_t *a, *nexta; + tmp_nodebuf_t *nb, *nextnb; + + //free all the faces + for ( f = tmpaasworld.faces; f; f = nextf ) + { + nextf = f->l_next; + if ( f->winding ) { + FreeWinding( f->winding ); + } + FreeMemory( f ); + } //end if + //free all tmp areas + for ( a = tmpaasworld.areas; a; a = nexta ) + { + nexta = a->l_next; + if ( a->settings ) { + FreeMemory( a->settings ); + } + FreeMemory( a ); + } //end for + //free all the tmp nodes + for ( nb = tmpaasworld.nodebuffer; nb; nb = nextnb ) + { + nextnb = nb->next; + FreeMemory( nb ); + } //end for +} //end of the function AAS_FreeTmpAAS +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +tmp_face_t *AAS_AllocTmpFace( void ) { + tmp_face_t *tmpface; + + tmpface = (tmp_face_t *) GetClearedMemory( sizeof( tmp_face_t ) ); + tmpface->num = tmpaasworld.facenum++; + tmpface->l_prev = NULL; + tmpface->l_next = tmpaasworld.faces; + if ( tmpaasworld.faces ) { + tmpaasworld.faces->l_prev = tmpface; + } + tmpaasworld.faces = tmpface; + tmpaasworld.numfaces++; + return tmpface; +} //end of the function AAS_AllocTmpFace +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FreeTmpFace( tmp_face_t *tmpface ) { + if ( tmpface->l_next ) { + tmpface->l_next->l_prev = tmpface->l_prev; + } + if ( tmpface->l_prev ) { + tmpface->l_prev->l_next = tmpface->l_next; + } else { tmpaasworld.faces = tmpface->l_next;} + //free the winding + if ( tmpface->winding ) { + FreeWinding( tmpface->winding ); + } + //free the face + FreeMemory( tmpface ); + tmpaasworld.numfaces--; +} //end of the function AAS_FreeTmpFace +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +tmp_area_t *AAS_AllocTmpArea( void ) { + tmp_area_t *tmparea; + + tmparea = (tmp_area_t *) GetClearedMemory( sizeof( tmp_area_t ) ); + tmparea->areanum = tmpaasworld.areanum++; + tmparea->l_prev = NULL; + tmparea->l_next = tmpaasworld.areas; + if ( tmpaasworld.areas ) { + tmpaasworld.areas->l_prev = tmparea; + } + tmpaasworld.areas = tmparea; + tmpaasworld.numareas++; + return tmparea; +} //end of the function AAS_AllocTmpArea +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FreeTmpArea( tmp_area_t *tmparea ) { + if ( tmparea->l_next ) { + tmparea->l_next->l_prev = tmparea->l_prev; + } + if ( tmparea->l_prev ) { + tmparea->l_prev->l_next = tmparea->l_next; + } else { tmpaasworld.areas = tmparea->l_next;} + if ( tmparea->settings ) { + FreeMemory( tmparea->settings ); + } + FreeMemory( tmparea ); + tmpaasworld.numareas--; +} //end of the function AAS_FreeTmpArea +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +tmp_node_t *AAS_AllocTmpNode( void ) { + tmp_nodebuf_t *nodebuf; + + if ( !tmpaasworld.nodebuffer || + tmpaasworld.nodebuffer->numnodes >= NODEBUF_SIZE ) { + nodebuf = (tmp_nodebuf_t *) GetClearedMemory( sizeof( tmp_nodebuf_t ) ); + nodebuf->next = tmpaasworld.nodebuffer; + nodebuf->numnodes = 0; + tmpaasworld.nodebuffer = nodebuf; + } //end if + tmpaasworld.numnodes++; + return &tmpaasworld.nodebuffer->nodes[tmpaasworld.nodebuffer->numnodes++]; +} //end of the function AAS_AllocTmpNode +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FreeTmpNode( tmp_node_t *tmpnode ) { + tmpaasworld.numnodes--; +} //end of the function AAS_FreeTmpNode +//=========================================================================== +// returns true if the face is a gap from the given side +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_GapFace( tmp_face_t *tmpface, int side ) { + vec3_t invgravity; + + //if the face is a solid or ground face it can't be a gap + if ( tmpface->faceflags & ( FACE_GROUND | FACE_SOLID ) ) { + return 0; + } + + VectorCopy( cfg.phys_gravitydirection, invgravity ); + VectorInverse( invgravity ); + + return ( DotProduct( invgravity, mapplanes[tmpface->planenum ^ side].normal ) > cfg.phys_maxsteepness ); +} //end of the function AAS_GapFace +//=========================================================================== +// returns true if the face is a ground face +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_GroundFace( tmp_face_t *tmpface ) { + vec3_t invgravity; + + //must be a solid face + if ( !( tmpface->faceflags & FACE_SOLID ) ) { + return 0; + } + + VectorCopy( cfg.phys_gravitydirection, invgravity ); + VectorInverse( invgravity ); + + return ( DotProduct( invgravity, mapplanes[tmpface->planenum].normal ) > cfg.phys_maxsteepness ); +} //end of the function AAS_GroundFace +//=========================================================================== +// adds the side of a face to an area +// +// side : 0 = front side +// 1 = back side +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_AddFaceSideToArea( tmp_face_t *tmpface, int side, tmp_area_t *tmparea ) { + int tmpfaceside; + + if ( side ) { + if ( tmpface->backarea ) { + Error( "AAS_AddFaceSideToArea: already a back area\n" ); + } + } //end if + else + { + if ( tmpface->frontarea ) { + Error( "AAS_AddFaceSideToArea: already a front area\n" ); + } + } //end else + + if ( side ) { + tmpface->backarea = tmparea; + } else { tmpface->frontarea = tmparea;} + + if ( tmparea->tmpfaces ) { + tmpfaceside = tmparea->tmpfaces->frontarea != tmparea; + tmparea->tmpfaces->prev[tmpfaceside] = tmpface; + } //end if + tmpface->next[side] = tmparea->tmpfaces; + tmpface->prev[side] = NULL; + tmparea->tmpfaces = tmpface; +} //end of the function AAS_AddFaceSideToArea +//=========================================================================== +// remove (a side of) a face from an area +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_RemoveFaceFromArea( tmp_face_t *tmpface, tmp_area_t *tmparea ) { + int side, prevside, nextside; + + if ( tmpface->frontarea != tmparea && + tmpface->backarea != tmparea ) { + Error( "AAS_RemoveFaceFromArea: face not part of the area" ); + } //end if + side = tmpface->frontarea != tmparea; + if ( tmpface->prev[side] ) { + prevside = tmpface->prev[side]->frontarea != tmparea; + tmpface->prev[side]->next[prevside] = tmpface->next[side]; + } //end if + else + { + tmparea->tmpfaces = tmpface->next[side]; + } //end else + if ( tmpface->next[side] ) { + nextside = tmpface->next[side]->frontarea != tmparea; + tmpface->next[side]->prev[nextside] = tmpface->prev[side]; + } //end if + //remove the area number from the face depending on the side + if ( side ) { + tmpface->backarea = NULL; + } else { tmpface->frontarea = NULL;} + tmpface->prev[side] = NULL; + tmpface->next[side] = NULL; +} //end of the function AAS_RemoveFaceFromArea +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_CheckArea( tmp_area_t *tmparea ) { + int side; + tmp_face_t *face; + plane_t *plane; + vec3_t wcenter, acenter = {0, 0, 0}; + vec3_t normal; + float n, dist; + + if ( tmparea->invalid ) { + Log_Print( "AAS_CheckArea: invalid area\n" ); + } + for ( n = 0, face = tmparea->tmpfaces; face; face = face->next[side] ) + { + //side of the face the area is on + side = face->frontarea != tmparea; + WindingCenter( face->winding, wcenter ); + VectorAdd( acenter, wcenter, acenter ); + n++; + } //end for + n = 1 / n; + VectorScale( acenter, n, acenter ); + for ( face = tmparea->tmpfaces; face; face = face->next[side] ) + { + //side of the face the area is on + side = face->frontarea != tmparea; + +#ifdef L_DEBUG + if ( WindingError( face->winding ) ) { + Log_Write( "AAS_CheckArea: area %d face %d: %s\r\n", tmparea->areanum, + face->num, WindingErrorString() ); + } //end if +#endif L_DEBUG + + plane = &mapplanes[face->planenum ^ side]; + + if ( DotProduct( plane->normal, acenter ) - plane->dist < 0 ) { + Log_Print( "AAS_CheckArea: area %d face %d is flipped\n", tmparea->areanum, face->num ); + Log_Print( "AAS_CheckArea: area %d center is %f %f %f\n", tmparea->areanum, acenter[0], acenter[1], acenter[2] ); + } //end if + //check if the winding plane is the same as the face plane + WindingPlane( face->winding, normal, &dist ); + plane = &mapplanes[face->planenum]; +#ifdef L_DEBUG + if ( fabs( dist - plane->dist ) > 0.4 || + fabs( normal[0] - plane->normal[0] ) > 0.0001 || + fabs( normal[1] - plane->normal[1] ) > 0.0001 || + fabs( normal[2] - plane->normal[2] ) > 0.0001 ) { + Log_Write( "AAS_CheckArea: area %d face %d winding plane unequal to face plane\r\n", + tmparea->areanum, face->num ); + } //end if +#endif L_DEBUG + } //end for +} //end of the function AAS_CheckArea +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_CheckFaceWindingPlane( tmp_face_t *face ) { + float dist, sign1, sign2; + vec3_t normal; + plane_t *plane; + winding_t *w; + + //check if the winding plane is the same as the face plane + WindingPlane( face->winding, normal, &dist ); + plane = &mapplanes[face->planenum]; + // + sign1 = DotProduct( plane->normal, normal ); + // + if ( fabs( dist - plane->dist ) > 0.4 || + fabs( normal[0] - plane->normal[0] ) > 0.0001 || + fabs( normal[1] - plane->normal[1] ) > 0.0001 || + fabs( normal[2] - plane->normal[2] ) > 0.0001 ) { + VectorInverse( normal ); + dist = -dist; + if ( fabs( dist - plane->dist ) > 0.4 || + fabs( normal[0] - plane->normal[0] ) > 0.0001 || + fabs( normal[1] - plane->normal[1] ) > 0.0001 || + fabs( normal[2] - plane->normal[2] ) > 0.0001 ) { + Log_Write( "AAS_CheckFaceWindingPlane: face %d winding plane unequal to face plane\r\n", + face->num ); + // + sign2 = DotProduct( plane->normal, normal ); + if ( ( sign1 < 0 && sign2 > 0 ) || + ( sign1 > 0 && sign2 < 0 ) ) { + Log_Write( "AAS_CheckFaceWindingPlane: face %d winding reversed\r\n", + face->num ); + w = face->winding; + face->winding = ReverseWinding( w ); + FreeWinding( w ); + } //end if + } //end if + else + { + Log_Write( "AAS_CheckFaceWindingPlane: face %d winding reversed\r\n", + face->num ); + w = face->winding; + face->winding = ReverseWinding( w ); + FreeWinding( w ); + } //end else + } //end if +} //end of the function AAS_CheckFaceWindingPlane +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_CheckAreaWindingPlanes( void ) { + int side; + tmp_area_t *tmparea; + tmp_face_t *face; + + Log_Write( "AAS_CheckAreaWindingPlanes:\r\n" ); + for ( tmparea = tmpaasworld.areas; tmparea; tmparea = tmparea->l_next ) + { + if ( tmparea->invalid ) { + continue; + } + for ( face = tmparea->tmpfaces; face; face = face->next[side] ) + { + side = face->frontarea != tmparea; + AAS_CheckFaceWindingPlane( face ); + } //end for + } //end for +} //end of the function AAS_CheckAreaWindingPlanes +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FlipAreaFaces( tmp_area_t *tmparea ) { + int side; + tmp_face_t *face; + plane_t *plane; + vec3_t wcenter, acenter = {0, 0, 0}; + //winding_t *w; + float n; + + for ( n = 0, face = tmparea->tmpfaces; face; face = face->next[side] ) + { + if ( !face->frontarea ) { + Error( "face %d has no front area\n", face->num ); + } + //side of the face the area is on + side = face->frontarea != tmparea; + WindingCenter( face->winding, wcenter ); + VectorAdd( acenter, wcenter, acenter ); + n++; + } //end for + n = 1 / n; + VectorScale( acenter, n, acenter ); + for ( face = tmparea->tmpfaces; face; face = face->next[side] ) + { + //side of the face the area is on + side = face->frontarea != tmparea; + + plane = &mapplanes[face->planenum ^ side]; + + if ( DotProduct( plane->normal, acenter ) - plane->dist < 0 ) { + Log_Print( "area %d face %d flipped: front area %d, back area %d\n", tmparea->areanum, face->num, + face->frontarea ? face->frontarea->areanum : 0, + face->backarea ? face->backarea->areanum : 0 ); + /* + face->planenum = face->planenum ^ 1; + w = face->winding; + face->winding = ReverseWinding(w); + FreeWinding(w); + */ + } //end if +#ifdef L_DEBUG + { + float dist; + vec3_t normal; + + //check if the winding plane is the same as the face plane + WindingPlane( face->winding, normal, &dist ); + plane = &mapplanes[face->planenum]; + if ( fabs( dist - plane->dist ) > 0.4 || + fabs( normal[0] - plane->normal[0] ) > 0.0001 || + fabs( normal[1] - plane->normal[1] ) > 0.0001 || + fabs( normal[2] - plane->normal[2] ) > 0.0001 ) { + Log_Write( "area %d face %d winding plane unequal to face plane\r\n", + tmparea->areanum, face->num ); + } //end if + } +#endif + } //end for +} //end of the function AAS_FlipAreaFaces +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_RemoveAreaFaceColinearPoints( void ) { + int side; + tmp_face_t *face; + tmp_area_t *tmparea; + + //FIXME: loop over the faces instead of area->faces + for ( tmparea = tmpaasworld.areas; tmparea; tmparea = tmparea->l_next ) + { + for ( face = tmparea->tmpfaces; face; face = face->next[side] ) + { + side = face->frontarea != tmparea; + RemoveColinearPoints( face->winding ); +// RemoveEqualPoints(face->winding, 0.1); + } //end for + } //end for +} //end of the function AAS_RemoveAreaFaceColinearPoints +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_RemoveTinyFaces( void ) { + int side, num; + tmp_face_t *face, *nextface; + tmp_area_t *tmparea; + + //FIXME: loop over the faces instead of area->faces + Log_Write( "AAS_RemoveTinyFaces\r\n" ); + num = 0; + for ( tmparea = tmpaasworld.areas; tmparea; tmparea = tmparea->l_next ) + { + for ( face = tmparea->tmpfaces; face; face = nextface ) + { + side = face->frontarea != tmparea; + nextface = face->next[side]; + // + if ( WindingArea( face->winding ) < 1 ) { + if ( face->frontarea ) { + AAS_RemoveFaceFromArea( face, face->frontarea ); + } + if ( face->backarea ) { + AAS_RemoveFaceFromArea( face, face->backarea ); + } + AAS_FreeTmpFace( face ); + //Log_Write("area %d face %d is tiny\r\n", tmparea->areanum, face->num); + num++; + } //end if + } //end for + } //end for + Log_Write( "%d tiny faces removed\r\n", num ); +} //end of the function AAS_RemoveTinyFaces +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_CreateAreaSettings( void ) { + int i, flags, side, numgrounded, numladderareas, numliquidareas; + tmp_face_t *face; + tmp_area_t *tmparea; + int count; + + numgrounded = 0; + numladderareas = 0; + numliquidareas = 0; + Log_Write( "AAS_CreateAreaSettings\r\n" ); + i = 0; + qprintf( "%6d areas provided with settings", i ); + for ( tmparea = tmpaasworld.areas; tmparea; tmparea = tmparea->l_next ) + { + //if the area is invalid there no need to create settings for it + if ( tmparea->invalid ) { + continue; + } + + tmparea->settings = (tmp_areasettings_t *) GetClearedMemory( sizeof( tmp_areasettings_t ) ); + tmparea->settings->contents = tmparea->contents; + tmparea->settings->modelnum = tmparea->modelnum; + flags = 0; + count = 0; + tmparea->settings->groundsteepness = 0.0; + for ( face = tmparea->tmpfaces; face; face = face->next[side] ) + { + side = face->frontarea != tmparea; + flags |= face->faceflags; + // Ridah, add this face's steepness + if ( face->faceflags & FACE_GROUND ) { + tmparea->settings->groundsteepness += ( 1.0 - mapplanes[face->planenum ^ side].normal[2] ); + count++; + } + } //end for + tmparea->settings->groundsteepness /= (float)count; + if ( tmparea->settings->groundsteepness > 1.0 ) { + tmparea->settings->groundsteepness = 1.0; + } + if ( tmparea->settings->groundsteepness < 0.0 ) { + tmparea->settings->groundsteepness = 0.0; + } + tmparea->settings->areaflags = 0; + if ( flags & FACE_GROUND ) { + tmparea->settings->areaflags |= AREA_GROUNDED; + numgrounded++; + } //end if + if ( flags & FACE_LADDER ) { + tmparea->settings->areaflags |= AREA_LADDER; + numladderareas++; + } //end if + if ( tmparea->contents & ( AREACONTENTS_WATER | + AREACONTENTS_SLIME | + AREACONTENTS_LAVA ) ) { + tmparea->settings->areaflags |= AREA_LIQUID; + numliquidareas++; + } //end if + //presence type of the area + tmparea->settings->presencetype = tmparea->presencetype; + // + qprintf( "\r%6d", ++i ); + } //end for + qprintf( "\n" ); +#ifdef AASINFO + Log_Print( "%6d grounded areas\n", numgrounded ); + Log_Print( "%6d ladder areas\n", numladderareas ); + Log_Print( "%6d liquid areas\n", numliquidareas ); +#endif //AASINFO +} //end of the function AAS_CreateAreaSettings +//=========================================================================== +// create a tmp AAS area from a leaf node +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +//portal_t *globalPortalDebug; +tmp_node_t *AAS_CreateArea( node_t *node ) { + int pside; + int areafaceflags; + portal_t *p; + tmp_face_t *tmpface; + tmp_area_t *tmparea; + tmp_node_t *tmpnode; + //vec3_t up = {0, 0, 1}; // TTimo: unused + + //create an area from this leaf + tmparea = AAS_AllocTmpArea(); + tmparea->tmpfaces = NULL; + //clear the area face flags + areafaceflags = 0; + //make aas faces from the portals + for ( p = node->portals; p; p = p->next[pside] ) + { + pside = ( p->nodes[1] == node ); + //don't create faces from very small portals +// if (WindingArea(p->winding) < 1) continue; + //if there's already a face created for this portal + if ( p->tmpface ) { + //add the back side of the face to the area + AAS_AddFaceSideToArea( p->tmpface, 1, tmparea ); + } //end if + else + { + tmpface = AAS_AllocTmpFace(); + //set the face pointer at the portal so we can see from + //the portal there's a face created for it + p->tmpface = tmpface; + //FIXME: test this change + //tmpface->planenum = (p->planenum & ~1) | pside; + tmpface->planenum = p->planenum ^ pside; + if ( pside ) { + tmpface->winding = ReverseWinding( p->winding ); + } else { tmpface->winding = CopyWinding( p->winding );} +#ifdef L_DEBUG + // + AAS_CheckFaceWindingPlane( tmpface ); +#endif //L_DEBUG + //if there's solid at the other side of the portal + if ( p->nodes[!pside]->contents & ( CONTENTS_SOLID | CONTENTS_PLAYERCLIP ) ) { + tmpface->faceflags |= FACE_SOLID; + } //end if + //else there is no solid at the other side and if there + //is a liquid at this side + else if ( node->contents & ( CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ) ) { + tmpface->faceflags |= FACE_LIQUID; + //if there's no liquid at the other side + if ( !( p->nodes[!pside]->contents & ( CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ) ) ) { + tmpface->faceflags |= FACE_LIQUIDSURFACE; + } //end if + } //end else + //if there's ladder contents at other side of the portal + if ( ( p->nodes[pside]->contents & CONTENTS_LADDER ) || + ( p->nodes[!pside]->contents & CONTENTS_LADDER ) ) { + + //NOTE: doesn't have to be solid at the other side because + // when standing one can use a crouch area (which is not solid) + // as a ladder + // imagine a ladder one can walk underthrough, + // under the ladder against the ladder is a crouch area + // the (vertical) sides of this crouch area area also used as + // ladder sides when standing (not crouched) + tmpface->faceflags |= FACE_LADDER; + } //end if + //if it is possible to stand on the face + if ( AAS_GroundFace( tmpface ) ) { + tmpface->faceflags |= FACE_GROUND; + } //end if + // + areafaceflags |= tmpface->faceflags; + //no aas face number yet (zero is a dummy in the aasworld faces) + tmpface->aasfacenum = 0; + //add the front side of the face to the area + AAS_AddFaceSideToArea( tmpface, 0, tmparea ); + } //end else + } //end for + qprintf( "\r%6d", tmparea->areanum ); + //presence type in the area + tmparea->presencetype = ~node->expansionbboxes & cfg.allpresencetypes; + // + tmparea->contents = 0; + if ( node->contents & CONTENTS_CLUSTERPORTAL ) { + tmparea->contents |= AREACONTENTS_CLUSTERPORTAL; + } + if ( node->contents & CONTENTS_MOVER ) { + tmparea->contents |= AREACONTENTS_MOVER; + } + if ( node->contents & CONTENTS_TELEPORTER ) { + tmparea->contents |= AREACONTENTS_TELEPORTER; + } + if ( node->contents & CONTENTS_JUMPPAD ) { + tmparea->contents |= AREACONTENTS_JUMPPAD; + } + if ( node->contents & CONTENTS_DONOTENTER ) { + tmparea->contents |= AREACONTENTS_DONOTENTER; + } + if ( node->contents & CONTENTS_DONOTENTER_LARGE ) { + tmparea->contents |= AREACONTENTS_DONOTENTER_LARGE; + } + if ( node->contents & CONTENTS_WATER ) { + tmparea->contents |= AREACONTENTS_WATER; + } + if ( node->contents & CONTENTS_LAVA ) { + tmparea->contents |= AREACONTENTS_LAVA; + } + if ( node->contents & CONTENTS_SLIME ) { + tmparea->contents |= AREACONTENTS_SLIME; + } + + //store the bsp model that's inside this node + tmparea->modelnum = node->modelnum; + //sorta check for flipped area faces (remove??) + AAS_FlipAreaFaces( tmparea ); + //check if the area is ok (remove??) + AAS_CheckArea( tmparea ); + // + tmpnode = AAS_AllocTmpNode(); + tmpnode->planenum = 0; + tmpnode->children[0] = 0; + tmpnode->children[1] = 0; + tmpnode->tmparea = tmparea; + // + return tmpnode; +} //end of the function AAS_CreateArea +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +tmp_node_t *AAS_CreateAreas_r( node_t *node ) { + tmp_node_t *tmpnode; + + //recurse down to leafs + if ( node->planenum != PLANENUM_LEAF ) { + //the first tmp node is a dummy + tmpnode = AAS_AllocTmpNode(); + tmpnode->planenum = node->planenum; + tmpnode->children[0] = AAS_CreateAreas_r( node->children[0] ); + tmpnode->children[1] = AAS_CreateAreas_r( node->children[1] ); + return tmpnode; + } //end if + //areas won't be created for solid leafs + if ( node->contents & CONTENTS_SOLID ) { + //just return zero for a solid leaf (in tmp AAS NULL is a solid leaf) + return NULL; + } //end if + + return AAS_CreateArea( node ); +} //end of the function AAS_CreateAreas_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_CreateAreas( node_t *node ) { + Log_Write( "AAS_CreateAreas\r\n" ); + qprintf( "%6d areas created", 0 ); + tmpaasworld.nodes = AAS_CreateAreas_r( node ); + qprintf( "\n" ); + Log_Write( "%6d areas created\r\n", tmpaasworld.numareas ); +} //end of the function AAS_CreateAreas +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_PrintNumGroundFaces( void ) { + tmp_face_t *tmpface; + int numgroundfaces = 0; + + for ( tmpface = tmpaasworld.faces; tmpface; tmpface = tmpface->l_next ) + { + if ( tmpface->faceflags & FACE_GROUND ) { + numgroundfaces++; + } //end if + } //end for + qprintf( "%6d ground faces\n", numgroundfaces ); +} //end of the function AAS_PrintNumGroundFaces +//=========================================================================== +// checks the number of shared faces between the given two areas +// since areas are convex they should only have ONE shared face +// however due to crappy face merging there are sometimes several +// shared faces +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_CheckAreaSharedFaces( tmp_area_t *tmparea1, tmp_area_t *tmparea2 ) { + int numsharedfaces, side; + tmp_face_t *face1, *sharedface; + + if ( tmparea1->invalid || tmparea2->invalid ) { + return; + } + + sharedface = NULL; + numsharedfaces = 0; + for ( face1 = tmparea1->tmpfaces; face1; face1 = face1->next[side] ) + { + side = face1->frontarea != tmparea1; + if ( face1->backarea == tmparea2 || face1->frontarea == tmparea2 ) { + sharedface = face1; + numsharedfaces++; + } //end if + } //end if + if ( !sharedface ) { + return; + } + //the areas should only have one shared face + if ( numsharedfaces > 1 ) { + Log_Write( "---- tmp area %d and %d have %d shared faces\r\n", + tmparea1->areanum, tmparea2->areanum, numsharedfaces ); + for ( face1 = tmparea1->tmpfaces; face1; face1 = face1->next[side] ) + { + side = face1->frontarea != tmparea1; + if ( face1->backarea == tmparea2 || face1->frontarea == tmparea2 ) { + Log_Write( "face %d, planenum = %d, face->frontarea = %d face->backarea = %d\r\n", + face1->num, face1->planenum, face1->frontarea->areanum, face1->backarea->areanum ); + } //end if + } //end if + } //end if +} //end of the function AAS_CheckAreaSharedFaces +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_CheckSharedFaces( void ) { + tmp_area_t *tmparea1, *tmparea2; + + for ( tmparea1 = tmpaasworld.areas; tmparea1; tmparea1 = tmparea1->l_next ) + { + for ( tmparea2 = tmpaasworld.areas; tmparea2; tmparea2 = tmparea2->l_next ) + { + if ( tmparea1 == tmparea2 ) { + continue; + } + AAS_CheckAreaSharedFaces( tmparea1, tmparea2 ); + } //end for + } //end for +} //end of the function AAS_CheckSharedFaces +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FlipFace( tmp_face_t *face ) { + tmp_area_t *frontarea, *backarea; + winding_t *w; + + frontarea = face->frontarea; + backarea = face->backarea; + //must have an area at both sides before flipping is allowed + if ( !frontarea || !backarea ) { + return; + } + //flip the face winding + w = face->winding; + face->winding = ReverseWinding( w ); + FreeWinding( w ); + //flip the face plane + face->planenum ^= 1; + //flip the face areas + AAS_RemoveFaceFromArea( face, frontarea ); + AAS_RemoveFaceFromArea( face, backarea ); + AAS_AddFaceSideToArea( face, 1, frontarea ); + AAS_AddFaceSideToArea( face, 0, backarea ); +} //end of the function AAS_FlipFace +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +/* +void AAS_FlipAreaSharedFaces(tmp_area_t *tmparea1, tmp_area_t *tmparea2) +{ + int numsharedfaces, side, area1facing, area2facing; + tmp_face_t *face1, *sharedface; + + if (tmparea1->invalid || tmparea2->invalid) return; + + sharedface = NULL; + numsharedfaces = 0; + area1facing = 0; //number of shared faces facing towards area 1 + area2facing = 0; //number of shared faces facing towards area 2 + for (face1 = tmparea1->tmpfaces; face1; face1 = face1->next[side]) + { + side = face1->frontarea != tmparea1; + if (face1->backarea == tmparea2 || face1->frontarea == tmparea2) + { + sharedface = face1; + numsharedfaces++; + if (face1->frontarea == tmparea1) area1facing++; + else area2facing++; + } //end if + } //end if + if (!sharedface) return; + //if there's only one shared face + if (numsharedfaces <= 1) return; + //if all the shared faces are facing to the same area + if (numsharedfaces == area1facing || numsharedfaces == area2facing) return; + // + do + { + for (face1 = tmparea1->tmpfaces; face1; face1 = face1->next[side]) + { + side = face1->frontarea != tmparea1; + if (face1->backarea == tmparea2 || face1->frontarea == tmparea2) + { + if (face1->frontarea != tmparea1) + { + AAS_FlipFace(face1); + break; + } //end if + } //end if + } //end for + } while(face1); +} //end of the function AAS_FlipAreaSharedFaces +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FlipSharedFaces(void) +{ + int i; + tmp_area_t *tmparea1, *tmparea2; + + i = 0; + qprintf("%6d areas checked for shared face flipping", i); + for (tmparea1 = tmpaasworld.areas; tmparea1; tmparea1 = tmparea1->l_next) + { + if (tmparea1->invalid) continue; + for (tmparea2 = tmpaasworld.areas; tmparea2; tmparea2 = tmparea2->l_next) + { + if (tmparea2->invalid) continue; + if (tmparea1 == tmparea2) continue; + AAS_FlipAreaSharedFaces(tmparea1, tmparea2); + } //end for + qprintf("\r%6d", ++i); + } //end for + Log_Print("\r%6d areas checked for shared face flipping\n", i); +} //end of the function AAS_FlipSharedFaces +*/ +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FlipSharedFaces( void ) { + int i, side1, side2; + tmp_area_t *tmparea1; + tmp_face_t *face1, *face2; + + i = 0; + qprintf( "%6d areas checked for shared face flipping", i ); + for ( tmparea1 = tmpaasworld.areas; tmparea1; tmparea1 = tmparea1->l_next ) + { + if ( tmparea1->invalid ) { + continue; + } + for ( face1 = tmparea1->tmpfaces; face1; face1 = face1->next[side1] ) + { + side1 = face1->frontarea != tmparea1; + if ( !face1->frontarea || !face1->backarea ) { + continue; + } + // + for ( face2 = face1->next[side1]; face2; face2 = face2->next[side2] ) + { + side2 = face2->frontarea != tmparea1; + if ( !face2->frontarea || !face2->backarea ) { + continue; + } + // + if ( face1->frontarea == face2->backarea && + face1->backarea == face2->frontarea ) { + AAS_FlipFace( face2 ); + } //end if + //recheck side + side2 = face2->frontarea != tmparea1; + } //end for + } //end for + qprintf( "\r%6d", ++i ); + } //end for + qprintf( "\n" ); + Log_Print( "%6d areas checked for shared face flipping\r\n", i ); +} //end of the function AAS_FlipSharedFaces +//=========================================================================== +// creates an .AAS file with the given name +// a MAP should be loaded before calling this +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_Create( char *aasfile ) { + entity_t *e; + tree_t *tree; + double start_time; + + //for a possible leak file + strcpy( source, aasfile ); + StripExtension( source ); + //the time started + start_time = I_FloatTime(); + //set the default number of threads (depends on number of processors) + ThreadSetDefault(); + //set the global entity number to the world model + entity_num = 0; + //the world entity + e = &entities[entity_num]; + //process the whole world + tree = ProcessWorldBrushes( e->firstbrush, e->firstbrush + e->numbrushes ); + //if the conversion is cancelled + if ( cancelconversion ) { + Tree_Free( tree ); + return; + } //end if + //display BSP tree creation time + Log_Print( "BSP tree created in %5.0f seconds\n", I_FloatTime() - start_time ); + //prune the bsp tree + Tree_PruneNodes( tree->headnode ); + //if the conversion is cancelled + if ( cancelconversion ) { + Tree_Free( tree ); + return; + } //end if + //create the tree portals + MakeTreePortals( tree ); + //if the conversion is cancelled + if ( cancelconversion ) { + Tree_Free( tree ); + return; + } //end if + //Marks all nodes that can be reached by entites + if ( FloodEntities( tree ) ) { + //fill out nodes that can't be reached + FillOutside( tree->headnode ); + } //end if + else + { + LeakFile( tree ); + Error( "**** leaked ****\n" ); + return; + } //end else + //create AAS from the BSP tree + //========================================== + //initialize tmp aas + AAS_InitTmpAAS(); + //create the convex areas from the leaves + AAS_CreateAreas( tree->headnode ); + //free the BSP tree because it isn't used anymore + if ( freetree ) { + Tree_Free( tree ); + } + //try to merge area faces + AAS_MergeAreaFaces(); + //do gravitational subdivision + AAS_GravitationalSubdivision(); + //merge faces if possible + AAS_MergeAreaFaces(); + AAS_RemoveAreaFaceColinearPoints(); + //merge areas if possible + AAS_MergeAreas(); + //NOTE: prune nodes directly after area merging + AAS_PruneNodes(); + //flip shared faces so they are all facing to the same area + AAS_FlipSharedFaces(); + AAS_RemoveAreaFaceColinearPoints(); + //merge faces if possible + AAS_MergeAreaFaces(); + //merge area faces in the same plane + AAS_MergeAreaPlaneFaces(); + //do ladder subdivision + AAS_LadderSubdivision(); + //FIXME: melting is buggy + AAS_MeltAreaFaceWindings(); + //remove tiny faces + AAS_RemoveTinyFaces(); + //create area settings + AAS_CreateAreaSettings(); + //check if the winding plane is equal to the face plane + //AAS_CheckAreaWindingPlanes(); + // + //AAS_CheckSharedFaces(); + //========================================== + //if the conversion is cancelled + if ( cancelconversion ) { + Tree_Free( tree ); + AAS_FreeTmpAAS(); + return; + } //end if + //store the created AAS stuff in the AAS file format and write the file + AAS_StoreFile( aasfile ); + //free the temporary AAS memory + AAS_FreeTmpAAS(); + //display creation time + Log_Print( "\nAAS created in %5.0f seconds\n", I_FloatTime() - start_time ); +} //end of the function AAS_Create diff --git a/src/bspc/aas_create.h b/src/bspc/aas_create.h new file mode 100644 index 0000000..596fe33 --- /dev/null +++ b/src/bspc/aas_create.h @@ -0,0 +1,153 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: aas_create.h +// Function: +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + +#define AREA_PORTAL 1 + +//temporary AAS face +typedef struct tmp_face_s +{ + int num; //face number + int planenum; //number of the plane the face is in + winding_t *winding; //winding of the face + struct tmp_area_s *frontarea; //area at the front of the face + struct tmp_area_s *backarea; //area at the back of the face + int faceflags; //flags of this face + int aasfacenum; //the number of the aas face used for this face + //double link list pointers for front and back area + struct tmp_face_s *prev[2], *next[2]; + //links in the list with faces + struct tmp_face_s *l_prev, *l_next; +} tmp_face_t; + +//temporary AAS area settings +typedef struct tmp_areasettings_s +{ + //could also add all kind of statistic fields + int contents; //contents of the area + int modelnum; //bsp model inside this area + int areaflags; //area flags + int presencetype; //how a bot can be present in this area + int numreachableareas; //number of reachable areas from this one + int firstreachablearea; //first reachable area in the reachable area index + // Ridah, steepness + float groundsteepness; +} tmp_areasettings_t; + +//temporary AAS area +typedef struct tmp_area_s +{ + int areanum; //number of the area + struct tmp_face_s *tmpfaces; //the faces of the area + int presencetype; //presence type of the area + int contents; //area contents + int modelnum; //bsp model inside this area + int invalid; //true if the area is invalid + tmp_areasettings_t *settings; //area settings + struct tmp_area_s *mergedarea; //points to the new area after merging + //when mergedarea != 0 the area has only the + //seperating face of the merged areas + int aasareanum; //number of the aas area created for this tmp area + //links in the list with areas + struct tmp_area_s *l_prev, *l_next; +} tmp_area_t; + +//temporary AAS node +typedef struct tmp_node_s +{ + int planenum; //node plane number + struct tmp_area_s *tmparea; //points to an area if this node is an area + struct tmp_node_s *children[2]; //child nodes of this node +} tmp_node_t; + +#define NODEBUF_SIZE 128 +//node buffer +typedef struct tmp_nodebuf_s +{ + int numnodes; + struct tmp_nodebuf_s *next; + tmp_node_t nodes[NODEBUF_SIZE]; +} tmp_nodebuf_t; + +//the whole temorary AAS +typedef struct tmp_aas_s +{ + //faces + int numfaces; + int facenum; + tmp_face_t *faces; + //areas + int numareas; + int areanum; + tmp_area_t *areas; + //area settings + int numareasettings; + tmp_areasettings_t *areasettings; + //nodes + int numnodes; + tmp_node_t *nodes; + //node buffer + tmp_nodebuf_t *nodebuffer; +} tmp_aas_t; + +extern tmp_aas_t tmpaasworld; + +//creates a .AAS file with the given name from an already loaded map +void AAS_Create( char *aasfile ); +//adds a face side to an area +void AAS_AddFaceSideToArea( tmp_face_t *tmpface, int side, tmp_area_t *tmparea ); +//remvoes a face from an area +void AAS_RemoveFaceFromArea( tmp_face_t *tmpface, tmp_area_t *tmparea ); +//allocate a tmp face +tmp_face_t *AAS_AllocTmpFace( void ); +//free the tmp face +void AAS_FreeTmpFace( tmp_face_t *tmpface ); +//allocate a tmp area +tmp_area_t *AAS_AllocTmpArea( void ); +//free a tmp area +void AAS_FreeTmpArea( tmp_area_t *tmparea ); +//allocate a tmp node +tmp_node_t *AAS_AllocTmpNode( void ); +//free a tmp node +void AAS_FreeTmpNode( tmp_node_t *node ); +//checks if an area is ok +void AAS_CheckArea( tmp_area_t *tmparea ); +//flips the area faces where needed +void AAS_FlipAreaFaces( tmp_area_t *tmparea ); +//returns true if the face is a gap seen from the given side +int AAS_GapFace( tmp_face_t *tmpface, int side ); +//returns true if the face is a ground face +int AAS_GroundFace( tmp_face_t *tmpface ); diff --git a/src/bspc/aas_edgemelting.c b/src/bspc/aas_edgemelting.c new file mode 100644 index 0000000..803937a --- /dev/null +++ b/src/bspc/aas_edgemelting.c @@ -0,0 +1,125 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: aas_edgemelting.c +// Function: Melting of Edges +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + +#include "qbsp.h" +#include "../botlib/aasfile.h" +#include "aas_create.h" + +//=========================================================================== +// try to melt the windings of the two faces +// FIXME: this is buggy +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_MeltFaceWinding( tmp_face_t *face1, tmp_face_t *face2 ) { + int i, n; + int splits = 0; + winding_t *w2, *neww; + plane_t *plane1; + +#ifdef DEBUG + if ( !face1->winding ) { + Error( "face1 %d without winding", face1->num ); + } + if ( !face2->winding ) { + Error( "face2 %d without winding", face2->num ); + } +#endif //DEBUG + w2 = face2->winding; + plane1 = &mapplanes[face1->planenum]; + for ( i = 0; i < w2->numpoints; i++ ) + { + if ( PointOnWinding( face1->winding, plane1->normal, plane1->dist, w2->p[i], &n ) ) { + neww = AddWindingPoint( face1->winding, w2->p[i], n ); + FreeWinding( face1->winding ); + face1->winding = neww; + + splits++; + } //end if + } //end for + return splits; +} //end of the function AAS_MeltFaceWinding +//=========================================================================== +// melt the windings of the area faces +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_MeltFaceWindingsOfArea( tmp_area_t *tmparea ) { + int side1, side2, num_windingsplits = 0; + tmp_face_t *face1, *face2; + + for ( face1 = tmparea->tmpfaces; face1; face1 = face1->next[side1] ) + { + side1 = face1->frontarea != tmparea; + for ( face2 = tmparea->tmpfaces; face2; face2 = face2->next[side2] ) + { + side2 = face2->frontarea != tmparea; + if ( face1 == face2 ) { + continue; + } + num_windingsplits += AAS_MeltFaceWinding( face1, face2 ); + } //end for + } //end for + return num_windingsplits; +} //end of the function AAS_MeltFaceWindingsOfArea +//=========================================================================== +// melt the windings of the faces of all areas +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_MeltAreaFaceWindings( void ) { + tmp_area_t *tmparea; + int num_windingsplits = 0; + + Log_Write( "AAS_MeltAreaFaceWindings\r\n" ); + qprintf( "%6d edges melted", num_windingsplits ); + //NOTE: first convex area (zero) is a dummy + for ( tmparea = tmpaasworld.areas; tmparea; tmparea = tmparea->l_next ) + { + num_windingsplits += AAS_MeltFaceWindingsOfArea( tmparea ); + qprintf( "\r%6d", num_windingsplits ); + } //end for + qprintf( "\n" ); + Log_Write( "%6d edges melted\r\n", num_windingsplits ); +} //end of the function AAS_MeltAreaFaceWindings + diff --git a/src/bspc/aas_edgemelting.h b/src/bspc/aas_edgemelting.h new file mode 100644 index 0000000..20dbf86 --- /dev/null +++ b/src/bspc/aas_edgemelting.h @@ -0,0 +1,40 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: aas_edgemelting.h +// Function: Melting of Edges +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + + +void AAS_MeltAreaFaceWindings( void ); + diff --git a/src/bspc/aas_facemerging.c b/src/bspc/aas_facemerging.c new file mode 100644 index 0000000..2f89ddf --- /dev/null +++ b/src/bspc/aas_facemerging.c @@ -0,0 +1,312 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: aas_facemerging.c +// Function: Merging of Faces +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + +#include "qbsp.h" +#include "../botlib/aasfile.h" +#include "aas_create.h" + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_TryMergeFaces( tmp_face_t *face1, tmp_face_t *face2 ) { + winding_t *neww; + +#ifdef DEBUG + if ( !face1->winding ) { + Error( "face1 %d without winding", face1->num ); + } + if ( !face2->winding ) { + Error( "face2 %d without winding", face2->num ); + } +#endif //DEBUG + // + if ( face1->faceflags != face2->faceflags ) { + return false; + } + //NOTE: if the front or back area is zero this doesn't mean there's + //a real area. It means there's solid at that side of the face + //if both faces have the same front area + if ( face1->frontarea == face2->frontarea ) { + //if both faces have the same back area + if ( face1->backarea == face2->backarea ) { + //if the faces are in the same plane + if ( face1->planenum == face2->planenum ) { + //if they have both a front and a back area (no solid on either side) + if ( face1->frontarea && face1->backarea ) { + neww = MergeWindings( face1->winding, face2->winding, + mapplanes[face1->planenum].normal ); + } //end if + else + { + //this function is to be found in l_poly.c + neww = TryMergeWinding( face1->winding, face2->winding, + mapplanes[face1->planenum].normal ); + } //end else + if ( neww ) { + FreeWinding( face1->winding ); + face1->winding = neww; + if ( face2->frontarea ) { + AAS_RemoveFaceFromArea( face2, face2->frontarea ); + } + if ( face2->backarea ) { + AAS_RemoveFaceFromArea( face2, face2->backarea ); + } + AAS_FreeTmpFace( face2 ); + return true; + } //end if + } //end if + else if ( ( face1->planenum & ~1 ) == ( face2->planenum & ~1 ) ) { + Log_Write( "face %d and %d, same front and back area but flipped planes\r\n", + face1->num, face2->num ); + } //end if + } //end if + } //end if + return false; +} //end of the function AAS_TryMergeFaces +/* +int AAS_TryMergeFaces(tmp_face_t *face1, tmp_face_t *face2) +{ + winding_t *neww; + +#ifdef DEBUG + if (!face1->winding) Error("face1 %d without winding", face1->num); + if (!face2->winding) Error("face2 %d without winding", face2->num); +#endif //DEBUG + //if the faces are in the same plane + if ((face1->planenum & ~1) != (face2->planenum & ~1)) return false; +// if (face1->planenum != face2->planenum) return false; + //NOTE: if the front or back area is zero this doesn't mean there's + //a real area. It means there's solid at that side of the face + //if both faces have the same front area + if (face1->frontarea != face2->frontarea || + face1->backarea != face2->backarea) + { + if (!face1->frontarea || !face1->backarea || + !face2->frontarea || !face2->backarea) return false; + else if (face1->frontarea != face2->backarea || + face1->backarea != face2->frontarea) return false; +// return false; + } //end if + //this function is to be found in l_poly.c + neww = TryMergeWinding(face1->winding, face2->winding, + mapplanes[face1->planenum].normal); + if (!neww) return false; + // + FreeWinding(face1->winding); + face1->winding = neww; + //remove face2 + if (face2->frontarea) + AAS_RemoveFaceFromArea(face2, &tmpaasworld.areas[face2->frontarea]); + if (face2->backarea) + AAS_RemoveFaceFromArea(face2, &tmpaasworld.areas[face2->backarea]); + return true; +} //end of the function AAS_TryMergeFaces*/ +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_MergeAreaFaces( void ) { + int num_facemerges = 0; + int side1, side2, restart; + tmp_area_t *tmparea, *lasttmparea; + tmp_face_t *face1, *face2; + + Log_Write( "AAS_MergeAreaFaces\r\n" ); + qprintf( "%6d face merges", num_facemerges ); + //NOTE: first convex area is a dummy + lasttmparea = tmpaasworld.areas; + for ( tmparea = tmpaasworld.areas; tmparea; tmparea = tmparea->l_next ) + { + restart = false; + // + if ( tmparea->invalid ) { + continue; + } + // + for ( face1 = tmparea->tmpfaces; face1; face1 = face1->next[side1] ) + { + side1 = face1->frontarea != tmparea; + for ( face2 = face1->next[side1]; face2; face2 = face2->next[side2] ) + { + side2 = face2->frontarea != tmparea; + //if succesfully merged + if ( AAS_TryMergeFaces( face1, face2 ) ) { + //start over again after merging two faces + restart = true; + num_facemerges++; + qprintf( "\r%6d", num_facemerges ); + AAS_CheckArea( tmparea ); + break; + } //end if + } //end for + if ( restart ) { + tmparea = lasttmparea; + break; + } //end if + } //end for + lasttmparea = tmparea; + } //end for + qprintf( "\n" ); + Log_Write( "%6d face merges\r\n", num_facemerges ); +} //end of the function AAS_MergeAreaFaces +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_MergePlaneFaces( tmp_area_t *tmparea, int planenum ) { + tmp_face_t *face1, *face2, *nextface2; + winding_t *neww; + int side1, side2; + + for ( face1 = tmparea->tmpfaces; face1; face1 = face1->next[side1] ) + { + side1 = face1->frontarea != tmparea; + if ( face1->planenum != planenum ) { + continue; + } + // + for ( face2 = face1->next[side1]; face2; face2 = nextface2 ) + { + side2 = face2->frontarea != tmparea; + nextface2 = face2->next[side2]; + // + if ( ( face2->planenum & ~1 ) != ( planenum & ~1 ) ) { + continue; + } + // + neww = MergeWindings( face1->winding, face2->winding, + mapplanes[face1->planenum].normal ); + FreeWinding( face1->winding ); + face1->winding = neww; + if ( face2->frontarea ) { + AAS_RemoveFaceFromArea( face2, face2->frontarea ); + } + if ( face2->backarea ) { + AAS_RemoveFaceFromArea( face2, face2->backarea ); + } + AAS_FreeTmpFace( face2 ); + // + nextface2 = face1->next[side1]; + } //end for + } //end for +} //end of the function AAS_MergePlaneFaces +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_CanMergePlaneFaces( tmp_area_t *tmparea, int planenum ) { + tmp_area_t *frontarea, *backarea; + tmp_face_t *face1; + int side1, merge, faceflags = 0; // TTimo: init + + frontarea = backarea = NULL; + merge = false; + for ( face1 = tmparea->tmpfaces; face1; face1 = face1->next[side1] ) + { + side1 = face1->frontarea != tmparea; + if ( ( face1->planenum & ~1 ) != ( planenum & ~1 ) ) { + continue; + } + if ( !frontarea && !backarea ) { + frontarea = face1->frontarea; + backarea = face1->backarea; + faceflags = face1->faceflags; + } //end if + else + { + if ( frontarea != face1->frontarea ) { + return false; + } + if ( backarea != face1->backarea ) { + return false; + } + if ( faceflags != face1->faceflags ) { + return false; + } + merge = true; + } //end else + } //end for + return merge; +} //end of the function AAS_CanMergePlaneFaces +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_MergeAreaPlaneFaces( void ) { + int num_facemerges = 0; + int side1; + tmp_area_t *tmparea, *nexttmparea; + tmp_face_t *face1; + + Log_Write( "AAS_MergePlaneFaces\r\n" ); + qprintf( "%6d plane face merges", num_facemerges ); + //NOTE: first convex area is a dummy + for ( tmparea = tmpaasworld.areas; tmparea; tmparea = nexttmparea ) + { + nexttmparea = tmparea->l_next; + // + if ( tmparea->invalid ) { + continue; + } + // + for ( face1 = tmparea->tmpfaces; face1; face1 = face1->next[side1] ) + { + side1 = face1->frontarea != tmparea; + // + if ( AAS_CanMergePlaneFaces( tmparea, face1->planenum ) ) { + AAS_MergePlaneFaces( tmparea, face1->planenum ); + nexttmparea = tmparea; + num_facemerges++; + qprintf( "\r%6d", num_facemerges ); + break; + } //end if + } //end for + } //end for + qprintf( "\n" ); + Log_Write( "%6d plane face merges\r\n", num_facemerges ); +} //end of the function AAS_MergeAreaPlaneFaces diff --git a/src/bspc/aas_facemerging.h b/src/bspc/aas_facemerging.h new file mode 100644 index 0000000..79e1c12 --- /dev/null +++ b/src/bspc/aas_facemerging.h @@ -0,0 +1,39 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: aas_facemerging.h +// Function: Merging of Faces +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + +void AAS_MergeAreaFaces( void ); +void AAS_MergeAreaPlaneFaces( void ); diff --git a/src/bspc/aas_file.c b/src/bspc/aas_file.c new file mode 100644 index 0000000..29b5c5b --- /dev/null +++ b/src/bspc/aas_file.c @@ -0,0 +1,603 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: aas_file.c +// Function: AAS file loading and writing +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + +#include "qbsp.h" +#include "../botlib/aasfile.h" +#include "aas_file.h" +#include "aas_store.h" +#include "aas_create.h" + +#define AAS_Error Error + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_SwapAASData( void ) { + int i, j; + //bounding boxes + for ( i = 0; i < ( *aasworld ).numbboxes; i++ ) + { + ( *aasworld ).bboxes[i].presencetype = LittleLong( ( *aasworld ).bboxes[i].presencetype ); + ( *aasworld ).bboxes[i].flags = LittleLong( ( *aasworld ).bboxes[i].flags ); + for ( j = 0; j < 3; j++ ) + { + ( *aasworld ).bboxes[i].mins[j] = LittleLong( ( *aasworld ).bboxes[i].mins[j] ); + ( *aasworld ).bboxes[i].maxs[j] = LittleLong( ( *aasworld ).bboxes[i].maxs[j] ); + } //end for + } //end for + //vertexes + for ( i = 0; i < ( *aasworld ).numvertexes; i++ ) + { + for ( j = 0; j < 3; j++ ) + ( *aasworld ).vertexes[i][j] = LittleFloat( ( *aasworld ).vertexes[i][j] ); + } //end for + //planes + for ( i = 0; i < ( *aasworld ).numplanes; i++ ) + { + for ( j = 0; j < 3; j++ ) + ( *aasworld ).planes[i].normal[j] = LittleFloat( ( *aasworld ).planes[i].normal[j] ); + ( *aasworld ).planes[i].dist = LittleFloat( ( *aasworld ).planes[i].dist ); + ( *aasworld ).planes[i].type = LittleLong( ( *aasworld ).planes[i].type ); + } //end for + //edges + for ( i = 0; i < ( *aasworld ).numedges; i++ ) + { + ( *aasworld ).edges[i].v[0] = LittleLong( ( *aasworld ).edges[i].v[0] ); + ( *aasworld ).edges[i].v[1] = LittleLong( ( *aasworld ).edges[i].v[1] ); + } //end for + //edgeindex + for ( i = 0; i < ( *aasworld ).edgeindexsize; i++ ) + { + ( *aasworld ).edgeindex[i] = LittleLong( ( *aasworld ).edgeindex[i] ); + } //end for + //faces + for ( i = 0; i < ( *aasworld ).numfaces; i++ ) + { + ( *aasworld ).faces[i].planenum = LittleLong( ( *aasworld ).faces[i].planenum ); + ( *aasworld ).faces[i].faceflags = LittleLong( ( *aasworld ).faces[i].faceflags ); + ( *aasworld ).faces[i].numedges = LittleLong( ( *aasworld ).faces[i].numedges ); + ( *aasworld ).faces[i].firstedge = LittleLong( ( *aasworld ).faces[i].firstedge ); + ( *aasworld ).faces[i].frontarea = LittleLong( ( *aasworld ).faces[i].frontarea ); + ( *aasworld ).faces[i].backarea = LittleLong( ( *aasworld ).faces[i].backarea ); + } //end for + //face index + for ( i = 0; i < ( *aasworld ).faceindexsize; i++ ) + { + ( *aasworld ).faceindex[i] = LittleLong( ( *aasworld ).faceindex[i] ); + } //end for + //convex areas + for ( i = 0; i < ( *aasworld ).numareas; i++ ) + { + ( *aasworld ).areas[i].areanum = LittleLong( ( *aasworld ).areas[i].areanum ); + ( *aasworld ).areas[i].numfaces = LittleLong( ( *aasworld ).areas[i].numfaces ); + ( *aasworld ).areas[i].firstface = LittleLong( ( *aasworld ).areas[i].firstface ); + for ( j = 0; j < 3; j++ ) + { + ( *aasworld ).areas[i].mins[j] = LittleFloat( ( *aasworld ).areas[i].mins[j] ); + ( *aasworld ).areas[i].maxs[j] = LittleFloat( ( *aasworld ).areas[i].maxs[j] ); + ( *aasworld ).areas[i].center[j] = LittleFloat( ( *aasworld ).areas[i].center[j] ); + } //end for + } //end for + //area settings + for ( i = 0; i < ( *aasworld ).numareasettings; i++ ) + { + ( *aasworld ).areasettings[i].contents = LittleLong( ( *aasworld ).areasettings[i].contents ); + ( *aasworld ).areasettings[i].areaflags = LittleLong( ( *aasworld ).areasettings[i].areaflags ); + ( *aasworld ).areasettings[i].presencetype = LittleLong( ( *aasworld ).areasettings[i].presencetype ); + ( *aasworld ).areasettings[i].cluster = LittleLong( ( *aasworld ).areasettings[i].cluster ); + ( *aasworld ).areasettings[i].clusterareanum = LittleLong( ( *aasworld ).areasettings[i].clusterareanum ); + ( *aasworld ).areasettings[i].numreachableareas = LittleLong( ( *aasworld ).areasettings[i].numreachableareas ); + ( *aasworld ).areasettings[i].firstreachablearea = LittleLong( ( *aasworld ).areasettings[i].firstreachablearea ); + // Ridah + ( *aasworld ).areasettings[i].groundsteepness = LittleFloat( ( *aasworld ).areasettings[i].groundsteepness ); + } //end for + //area reachability + for ( i = 0; i < ( *aasworld ).reachabilitysize; i++ ) + { + ( *aasworld ).reachability[i].areanum = LittleLong( ( *aasworld ).reachability[i].areanum ); + ( *aasworld ).reachability[i].facenum = LittleLong( ( *aasworld ).reachability[i].facenum ); + ( *aasworld ).reachability[i].edgenum = LittleLong( ( *aasworld ).reachability[i].edgenum ); + for ( j = 0; j < 3; j++ ) + { + ( *aasworld ).reachability[i].start[j] = LittleFloat( ( *aasworld ).reachability[i].start[j] ); + ( *aasworld ).reachability[i].end[j] = LittleFloat( ( *aasworld ).reachability[i].end[j] ); + } //end for + ( *aasworld ).reachability[i].traveltype = LittleLong( ( *aasworld ).reachability[i].traveltype ); + ( *aasworld ).reachability[i].traveltime = LittleShort( ( *aasworld ).reachability[i].traveltime ); + } //end for + //nodes + for ( i = 0; i < ( *aasworld ).numnodes; i++ ) + { + ( *aasworld ).nodes[i].planenum = LittleLong( ( *aasworld ).nodes[i].planenum ); + ( *aasworld ).nodes[i].children[0] = LittleLong( ( *aasworld ).nodes[i].children[0] ); + ( *aasworld ).nodes[i].children[1] = LittleLong( ( *aasworld ).nodes[i].children[1] ); + } //end for + //cluster portals + for ( i = 0; i < ( *aasworld ).numportals; i++ ) + { + ( *aasworld ).portals[i].areanum = LittleLong( ( *aasworld ).portals[i].areanum ); + ( *aasworld ).portals[i].frontcluster = LittleLong( ( *aasworld ).portals[i].frontcluster ); + ( *aasworld ).portals[i].backcluster = LittleLong( ( *aasworld ).portals[i].backcluster ); + ( *aasworld ).portals[i].clusterareanum[0] = LittleLong( ( *aasworld ).portals[i].clusterareanum[0] ); + ( *aasworld ).portals[i].clusterareanum[1] = LittleLong( ( *aasworld ).portals[i].clusterareanum[1] ); + } //end for + //cluster portal index + for ( i = 0; i < ( *aasworld ).portalindexsize; i++ ) + { + ( *aasworld ).portalindex[i] = LittleLong( ( *aasworld ).portalindex[i] ); + } //end for + //cluster + for ( i = 0; i < ( *aasworld ).numclusters; i++ ) + { + ( *aasworld ).clusters[i].numareas = LittleLong( ( *aasworld ).clusters[i].numareas ); + ( *aasworld ).clusters[i].numportals = LittleLong( ( *aasworld ).clusters[i].numportals ); + ( *aasworld ).clusters[i].firstportal = LittleLong( ( *aasworld ).clusters[i].firstportal ); + } //end for +} //end of the function AAS_SwapAASData +//=========================================================================== +// dump the current loaded aas file +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_DumpAASData( void ) { + /* + if ((*aasworld).vertexes) FreeMemory((*aasworld).vertexes); + (*aasworld).vertexes = NULL; + if ((*aasworld).planes) FreeMemory((*aasworld).planes); + (*aasworld).planes = NULL; + if ((*aasworld).edges) FreeMemory((*aasworld).edges); + (*aasworld).edges = NULL; + if ((*aasworld).edgeindex) FreeMemory((*aasworld).edgeindex); + (*aasworld).edgeindex = NULL; + if ((*aasworld).faces) FreeMemory((*aasworld).faces); + (*aasworld).faces = NULL; + if ((*aasworld).faceindex) FreeMemory((*aasworld).faceindex); + (*aasworld).faceindex = NULL; + if ((*aasworld).areas) FreeMemory((*aasworld).areas); + (*aasworld).areas = NULL; + if ((*aasworld).areasettings) FreeMemory((*aasworld).areasettings); + (*aasworld).areasettings = NULL; + if ((*aasworld).reachability) FreeMemory((*aasworld).reachability); + (*aasworld).reachability = NULL; + */ + ( *aasworld ).loaded = false; +} //end of the function AAS_DumpAASData +//=========================================================================== +// allocate memory and read a lump of a AAS file +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +char *AAS_LoadAASLump( FILE *fp, int offset, int length, void *buf ) { + if ( !length ) { + printf( "lump size 0\n" ); + return buf; + } //end if + //seek to the data + if ( fseek( fp, offset, SEEK_SET ) ) { + AAS_Error( "can't seek to lump\n" ); + AAS_DumpAASData(); + fclose( fp ); + return 0; + } //end if + //allocate memory + if ( !buf ) { + buf = (void *) GetClearedMemory( length ); + } + //read the data + if ( fread( (char *) buf, 1, length, fp ) != length ) { + AAS_Error( "can't read lump\n" ); + FreeMemory( buf ); + AAS_DumpAASData(); + fclose( fp ); + return NULL; + } //end if + return buf; +} //end of the function AAS_LoadAASLump +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_DData( unsigned char *data, int size ) { + int i; + + for ( i = 0; i < size; i++ ) + { + data[i] ^= (unsigned char) i * 119; + } //end for +} //end of the function AAS_DData +//=========================================================================== +// load an aas file +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_LoadAASFile( char *filename, int fpoffset, int fplength ) { + FILE *fp; + aas_header_t header; + int offset, length; + + //dump current loaded aas file + AAS_DumpAASData(); + //open the file + fp = fopen( filename, "rb" ); + if ( !fp ) { + AAS_Error( "can't open %s\n", filename ); + return false; + } //end if + //seek to the correct position (in the pak file) + if ( fseek( fp, fpoffset, SEEK_SET ) ) { + AAS_Error( "can't seek to file %s\n" ); + fclose( fp ); + return false; + } //end if + //read the header + if ( fread( &header, sizeof( aas_header_t ), 1, fp ) != 1 ) { + AAS_Error( "can't read header of file %s\n", filename ); + fclose( fp ); + return false; + } //end if + //check header identification + header.ident = LittleLong( header.ident ); + if ( header.ident != AASID ) { + AAS_Error( "%s is not an AAS file\n", filename ); + fclose( fp ); + return false; + } //end if + //check the version + header.version = LittleLong( header.version ); + if ( header.version != AASVERSION ) { + AAS_Error( "%s is version %i, not %i\n", filename, header.version, AASVERSION ); + fclose( fp ); + return false; + } //end if + // + if ( header.version == AASVERSION ) { + AAS_DData( (unsigned char *) &header + 8, sizeof( aas_header_t ) - 8 ); + } //end if + ( *aasworld ).bspchecksum = LittleLong( header.bspchecksum ); + //load the lumps: + //bounding boxes + offset = fpoffset + LittleLong( header.lumps[AASLUMP_BBOXES].fileofs ); + length = LittleLong( header.lumps[AASLUMP_BBOXES].filelen ); + ( *aasworld ).bboxes = (aas_bbox_t *) AAS_LoadAASLump( fp, offset, length, ( *aasworld ).bboxes ); + if ( !( *aasworld ).bboxes ) { + return false; + } + ( *aasworld ).numbboxes = length / sizeof( aas_bbox_t ); + //vertexes + offset = fpoffset + LittleLong( header.lumps[AASLUMP_VERTEXES].fileofs ); + length = LittleLong( header.lumps[AASLUMP_VERTEXES].filelen ); + ( *aasworld ).vertexes = (aas_vertex_t *) AAS_LoadAASLump( fp, offset, length, ( *aasworld ).vertexes ); + if ( !( *aasworld ).vertexes ) { + return false; + } + ( *aasworld ).numvertexes = length / sizeof( aas_vertex_t ); + //planes + offset = fpoffset + LittleLong( header.lumps[AASLUMP_PLANES].fileofs ); + length = LittleLong( header.lumps[AASLUMP_PLANES].filelen ); + ( *aasworld ).planes = (aas_plane_t *) AAS_LoadAASLump( fp, offset, length, ( *aasworld ).planes ); + if ( !( *aasworld ).planes ) { + return false; + } + ( *aasworld ).numplanes = length / sizeof( aas_plane_t ); + //edges + offset = fpoffset + LittleLong( header.lumps[AASLUMP_EDGES].fileofs ); + length = LittleLong( header.lumps[AASLUMP_EDGES].filelen ); + ( *aasworld ).edges = (aas_edge_t *) AAS_LoadAASLump( fp, offset, length, ( *aasworld ).edges ); + if ( !( *aasworld ).edges ) { + return false; + } + ( *aasworld ).numedges = length / sizeof( aas_edge_t ); + //edgeindex + offset = fpoffset + LittleLong( header.lumps[AASLUMP_EDGEINDEX].fileofs ); + length = LittleLong( header.lumps[AASLUMP_EDGEINDEX].filelen ); + ( *aasworld ).edgeindex = (aas_edgeindex_t *) AAS_LoadAASLump( fp, offset, length, ( *aasworld ).edgeindex ); + if ( !( *aasworld ).edgeindex ) { + return false; + } + ( *aasworld ).edgeindexsize = length / sizeof( aas_edgeindex_t ); + //faces + offset = fpoffset + LittleLong( header.lumps[AASLUMP_FACES].fileofs ); + length = LittleLong( header.lumps[AASLUMP_FACES].filelen ); + ( *aasworld ).faces = (aas_face_t *) AAS_LoadAASLump( fp, offset, length, ( *aasworld ).faces ); + if ( !( *aasworld ).faces ) { + return false; + } + ( *aasworld ).numfaces = length / sizeof( aas_face_t ); + //faceindex + offset = fpoffset + LittleLong( header.lumps[AASLUMP_FACEINDEX].fileofs ); + length = LittleLong( header.lumps[AASLUMP_FACEINDEX].filelen ); + ( *aasworld ).faceindex = (aas_faceindex_t *) AAS_LoadAASLump( fp, offset, length, ( *aasworld ).faceindex ); + if ( !( *aasworld ).faceindex ) { + return false; + } + ( *aasworld ).faceindexsize = length / sizeof( int ); + //convex areas + offset = fpoffset + LittleLong( header.lumps[AASLUMP_AREAS].fileofs ); + length = LittleLong( header.lumps[AASLUMP_AREAS].filelen ); + ( *aasworld ).areas = (aas_area_t *) AAS_LoadAASLump( fp, offset, length, ( *aasworld ).areas ); + if ( !( *aasworld ).areas ) { + return false; + } + ( *aasworld ).numareas = length / sizeof( aas_area_t ); + //area settings + offset = fpoffset + LittleLong( header.lumps[AASLUMP_AREASETTINGS].fileofs ); + length = LittleLong( header.lumps[AASLUMP_AREASETTINGS].filelen ); + ( *aasworld ).areasettings = (aas_areasettings_t *) AAS_LoadAASLump( fp, offset, length, ( *aasworld ).areasettings ); + if ( !( *aasworld ).areasettings ) { + return false; + } + ( *aasworld ).numareasettings = length / sizeof( aas_areasettings_t ); + //reachability list + offset = fpoffset + LittleLong( header.lumps[AASLUMP_REACHABILITY].fileofs ); + length = LittleLong( header.lumps[AASLUMP_REACHABILITY].filelen ); + ( *aasworld ).reachability = (aas_reachability_t *) AAS_LoadAASLump( fp, offset, length, ( *aasworld ).reachability ); + if ( length && !( *aasworld ).reachability ) { + return false; + } + ( *aasworld ).reachabilitysize = length / sizeof( aas_reachability_t ); + //nodes + offset = fpoffset + LittleLong( header.lumps[AASLUMP_NODES].fileofs ); + length = LittleLong( header.lumps[AASLUMP_NODES].filelen ); + ( *aasworld ).nodes = (aas_node_t *) AAS_LoadAASLump( fp, offset, length, ( *aasworld ).nodes ); + if ( !( *aasworld ).nodes ) { + return false; + } + ( *aasworld ).numnodes = length / sizeof( aas_node_t ); + //cluster portals + offset = fpoffset + LittleLong( header.lumps[AASLUMP_PORTALS].fileofs ); + length = LittleLong( header.lumps[AASLUMP_PORTALS].filelen ); + ( *aasworld ).portals = (aas_portal_t *) AAS_LoadAASLump( fp, offset, length, ( *aasworld ).portals ); + if ( length && !( *aasworld ).portals ) { + return false; + } + ( *aasworld ).numportals = length / sizeof( aas_portal_t ); + //cluster portal index + offset = fpoffset + LittleLong( header.lumps[AASLUMP_PORTALINDEX].fileofs ); + length = LittleLong( header.lumps[AASLUMP_PORTALINDEX].filelen ); + ( *aasworld ).portalindex = (aas_portalindex_t *) AAS_LoadAASLump( fp, offset, length, ( *aasworld ).portalindex ); + if ( length && !( *aasworld ).portalindex ) { + return false; + } + ( *aasworld ).portalindexsize = length / sizeof( aas_portalindex_t ); + //clusters + offset = fpoffset + LittleLong( header.lumps[AASLUMP_CLUSTERS].fileofs ); + length = LittleLong( header.lumps[AASLUMP_CLUSTERS].filelen ); + ( *aasworld ).clusters = (aas_cluster_t *) AAS_LoadAASLump( fp, offset, length, ( *aasworld ).clusters ); + if ( length && !( *aasworld ).clusters ) { + return false; + } + ( *aasworld ).numclusters = length / sizeof( aas_cluster_t ); + //swap everything + AAS_SwapAASData(); + //aas file is loaded + ( *aasworld ).loaded = true; + //close the file + fclose( fp ); + return true; +} //end of the function AAS_LoadAASFile +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_WriteAASLump( FILE *fp, aas_header_t *h, int lumpnum, void *data, int length ) { + aas_lump_t *lump; + + lump = &h->lumps[lumpnum]; + + lump->fileofs = LittleLong( ftell( fp ) ); + lump->filelen = LittleLong( length ); + + if ( length > 0 ) { + if ( fwrite( data, length, 1, fp ) < 1 ) { + Log_Print( "error writing lump %s\n", lumpnum ); + fclose( fp ); + return false; + } //end if + } //end if + return true; +} //end of the function AAS_WriteAASLump +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ShowNumReachabilities( int tt, char *name ) { + int i, num; + + num = 0; + for ( i = 0; i < ( *aasworld ).reachabilitysize; i++ ) + { + if ( ( ( *aasworld ).reachability[i].traveltype & TRAVELTYPE_MASK ) == tt ) { + num++; + } + } //end for + Log_Print( "%6d %s\n", num, name ); +} //end of the function AAS_ShowNumReachabilities +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ShowTotals( void ) { + Log_Print( "numvertexes = %d\r\n", ( *aasworld ).numvertexes ); + Log_Print( "numplanes = %d\r\n", ( *aasworld ).numplanes ); + Log_Print( "numedges = %d\r\n", ( *aasworld ).numedges ); + Log_Print( "edgeindexsize = %d\r\n", ( *aasworld ).edgeindexsize ); + Log_Print( "numfaces = %d\r\n", ( *aasworld ).numfaces ); + Log_Print( "faceindexsize = %d\r\n", ( *aasworld ).faceindexsize ); + Log_Print( "numareas = %d\r\n", ( *aasworld ).numareas ); + Log_Print( "numareasettings = %d\r\n", ( *aasworld ).numareasettings ); + Log_Print( "reachabilitysize = %d\r\n", ( *aasworld ).reachabilitysize ); + Log_Print( "numnodes = %d\r\n", ( *aasworld ).numnodes ); + Log_Print( "numportals = %d\r\n", ( *aasworld ).numportals ); + Log_Print( "portalindexsize = %d\r\n", ( *aasworld ).portalindexsize ); + Log_Print( "numclusters = %d\r\n", ( *aasworld ).numclusters ); + AAS_ShowNumReachabilities( TRAVEL_WALK, "walk" ); + AAS_ShowNumReachabilities( TRAVEL_CROUCH, "crouch" ); + AAS_ShowNumReachabilities( TRAVEL_BARRIERJUMP, "barrier jump" ); + AAS_ShowNumReachabilities( TRAVEL_JUMP, "jump" ); + AAS_ShowNumReachabilities( TRAVEL_LADDER, "ladder" ); + AAS_ShowNumReachabilities( TRAVEL_WALKOFFLEDGE, "walk off ledge" ); + AAS_ShowNumReachabilities( TRAVEL_SWIM, "swim" ); + AAS_ShowNumReachabilities( TRAVEL_WATERJUMP, "water jump" ); + AAS_ShowNumReachabilities( TRAVEL_TELEPORT, "teleport" ); + AAS_ShowNumReachabilities( TRAVEL_ELEVATOR, "elevator" ); + AAS_ShowNumReachabilities( TRAVEL_ROCKETJUMP, "rocket jump" ); + AAS_ShowNumReachabilities( TRAVEL_BFGJUMP, "bfg jump" ); + AAS_ShowNumReachabilities( TRAVEL_GRAPPLEHOOK, "grapple hook" ); + AAS_ShowNumReachabilities( TRAVEL_DOUBLEJUMP, "double jump" ); + AAS_ShowNumReachabilities( TRAVEL_RAMPJUMP, "ramp jump" ); + AAS_ShowNumReachabilities( TRAVEL_STRAFEJUMP, "strafe jump" ); + AAS_ShowNumReachabilities( TRAVEL_JUMPPAD, "jump pad" ); + AAS_ShowNumReachabilities( TRAVEL_FUNCBOB, "func bob" ); + +} //end of the function AAS_ShowTotals +//=========================================================================== +// aas data is useless after writing to file because it is byte swapped +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_WriteAASFile( char *filename ) { + aas_header_t header; + FILE *fp; + + Log_Print( "writing %s\n", filename ); + AAS_ShowTotals(); + //swap the aas data + AAS_SwapAASData(); + //initialize the file header + memset( &header, 0, sizeof( aas_header_t ) ); + header.ident = LittleLong( AASID ); + header.version = LittleLong( AASVERSION ); + header.bspchecksum = LittleLong( ( *aasworld ).bspchecksum ); + //open a new file + fp = fopen( filename, "wb" ); + if ( !fp ) { + Log_Print( "error opening %s\n", filename ); + return false; + } //end if + //write the header + if ( fwrite( &header, sizeof( aas_header_t ), 1, fp ) < 1 ) { + fclose( fp ); + return false; + } //end if + //add the data lumps to the file + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_BBOXES, ( *aasworld ).bboxes, + ( *aasworld ).numbboxes * sizeof( aas_bbox_t ) ) ) { + return false; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_VERTEXES, ( *aasworld ).vertexes, + ( *aasworld ).numvertexes * sizeof( aas_vertex_t ) ) ) { + return false; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_PLANES, ( *aasworld ).planes, + ( *aasworld ).numplanes * sizeof( aas_plane_t ) ) ) { + return false; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_EDGES, ( *aasworld ).edges, + ( *aasworld ).numedges * sizeof( aas_edge_t ) ) ) { + return false; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_EDGEINDEX, ( *aasworld ).edgeindex, + ( *aasworld ).edgeindexsize * sizeof( aas_edgeindex_t ) ) ) { + return false; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_FACES, ( *aasworld ).faces, + ( *aasworld ).numfaces * sizeof( aas_face_t ) ) ) { + return false; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_FACEINDEX, ( *aasworld ).faceindex, + ( *aasworld ).faceindexsize * sizeof( aas_faceindex_t ) ) ) { + return false; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_AREAS, ( *aasworld ).areas, + ( *aasworld ).numareas * sizeof( aas_area_t ) ) ) { + return false; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_AREASETTINGS, ( *aasworld ).areasettings, + ( *aasworld ).numareasettings * sizeof( aas_areasettings_t ) ) ) { + return false; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_REACHABILITY, ( *aasworld ).reachability, + ( *aasworld ).reachabilitysize * sizeof( aas_reachability_t ) ) ) { + return false; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_NODES, ( *aasworld ).nodes, + ( *aasworld ).numnodes * sizeof( aas_node_t ) ) ) { + return false; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_PORTALS, ( *aasworld ).portals, + ( *aasworld ).numportals * sizeof( aas_portal_t ) ) ) { + return false; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_PORTALINDEX, ( *aasworld ).portalindex, + ( *aasworld ).portalindexsize * sizeof( aas_portalindex_t ) ) ) { + return false; + } + if ( !AAS_WriteAASLump( fp, &header, AASLUMP_CLUSTERS, ( *aasworld ).clusters, + ( *aasworld ).numclusters * sizeof( aas_cluster_t ) ) ) { + return false; + } + //rewrite the header with the added lumps + fseek( fp, 0, SEEK_SET ); + AAS_DData( (unsigned char *) &header + 8, sizeof( aas_header_t ) - 8 ); + if ( fwrite( &header, sizeof( aas_header_t ), 1, fp ) < 1 ) { + fclose( fp ); + return false; + } //end if + //close the file + fclose( fp ); + return true; +} //end of the function AAS_WriteAASFile + diff --git a/src/bspc/aas_file.h b/src/bspc/aas_file.h new file mode 100644 index 0000000..dbe6032 --- /dev/null +++ b/src/bspc/aas_file.h @@ -0,0 +1,40 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: aas_write.h +// Function: +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + +qboolean AAS_WriteAASFile( char *filename ); +qboolean AAS_LoadAASFile( char *filename, int fpoffset, int fplength ); + diff --git a/src/bspc/aas_gsubdiv.c b/src/bspc/aas_gsubdiv.c new file mode 100644 index 0000000..184f569 --- /dev/null +++ b/src/bspc/aas_gsubdiv.c @@ -0,0 +1,688 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: aas_gsubdiv.c +// Function: Gravitational Subdivision +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + +#include "qbsp.h" +#include "../botlib/aasfile.h" +#include "aas_create.h" +#include "aas_store.h" +#include "aas_cfg.h" + +#define FACECLIP_EPSILON 0.2 +#define FACE_EPSILON 1.0 + +int numgravitationalsubdivisions = 0; +int numladdersubdivisions = 0; + +//NOTE: only do gravitational subdivision BEFORE area merging!!!!!!! +// because the bsp tree isn't refreshes like with ladder subdivision + +//=========================================================================== +// NOTE: the original face is invalid after splitting +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_SplitFace( tmp_face_t *face, vec3_t normal, float dist, + tmp_face_t **frontface, tmp_face_t **backface ) { + winding_t *frontw, *backw; + + // + *frontface = *backface = NULL; + + ClipWindingEpsilon( face->winding, normal, dist, FACECLIP_EPSILON, &frontw, &backw ); + +#ifdef DEBUG + // + if ( frontw ) { + if ( WindingIsTiny( frontw ) ) { + Log_Write( "AAS_SplitFace: tiny back face\r\n" ); + FreeWinding( frontw ); + frontw = NULL; + } //end if + } //end if + if ( backw ) { + if ( WindingIsTiny( backw ) ) { + Log_Write( "AAS_SplitFace: tiny back face\r\n" ); + FreeWinding( backw ); + backw = NULL; + } //end if + } //end if +#endif //DEBUG + //if the winding was split + if ( frontw ) { + //check bounds + ( *frontface ) = AAS_AllocTmpFace(); + ( *frontface )->planenum = face->planenum; + ( *frontface )->winding = frontw; + ( *frontface )->faceflags = face->faceflags; + } //end if + if ( backw ) { + //check bounds + ( *backface ) = AAS_AllocTmpFace(); + ( *backface )->planenum = face->planenum; + ( *backface )->winding = backw; + ( *backface )->faceflags = face->faceflags; + } //end if +} //end of the function AAS_SplitFace +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +winding_t *AAS_SplitWinding( tmp_area_t *tmparea, int planenum ) { + tmp_face_t *face; + plane_t *plane; + int side; + winding_t *splitwinding; + + // + plane = &mapplanes[planenum]; + //create a split winding, first base winding for plane + splitwinding = BaseWindingForPlane( plane->normal, plane->dist ); + //chop with all the faces of the area + for ( face = tmparea->tmpfaces; face && splitwinding; face = face->next[side] ) + { + //side of the face the original area was on + side = face->frontarea != tmparea; + plane = &mapplanes[face->planenum ^ side]; + ChopWindingInPlace( &splitwinding, plane->normal, plane->dist, 0 ); // PLANESIDE_EPSILON); + } //end for + return splitwinding; +} //end of the function AAS_SplitWinding +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_TestSplitPlane( tmp_area_t *tmparea, vec3_t normal, float dist, + int *facesplits, int *groundsplits, int *epsilonfaces ) { + int j, side, front, back, planenum; + float d, d_front, d_back; + tmp_face_t *face; + winding_t *w; + + *facesplits = *groundsplits = *epsilonfaces = 0; + + planenum = FindFloatPlane( normal, dist ); + + w = AAS_SplitWinding( tmparea, planenum ); + if ( !w ) { + return false; + } + FreeWinding( w ); + // + for ( face = tmparea->tmpfaces; face; face = face->next[side] ) + { + //side of the face the area is on + side = face->frontarea != tmparea; + + if ( ( face->planenum & ~1 ) == ( planenum & ~1 ) ) { + Log_Print( "AAS_TestSplitPlane: tried face plane as splitter\n" ); + return false; + } //end if + w = face->winding; + //reset distance at front and back side of plane + d_front = d_back = 0; + //reset front and back flags + front = back = 0; + for ( j = 0; j < w->numpoints; j++ ) + { + d = DotProduct( w->p[j], normal ) - dist; + if ( d > d_front ) { + d_front = d; + } + if ( d < d_back ) { + d_back = d; + } + + if ( d > 0.4 ) { // PLANESIDE_EPSILON) + front = 1; + } + if ( d < -0.4 ) { // PLANESIDE_EPSILON) + back = 1; + } + } //end for + //check for an epsilon face + if ( ( d_front > FACECLIP_EPSILON && d_front < FACE_EPSILON ) + || ( d_back < -FACECLIP_EPSILON && d_back > -FACE_EPSILON ) ) { + ( *epsilonfaces )++; + } //end if + //if the face has points at both sides of the plane + if ( front && back ) { + ( *facesplits )++; + if ( face->faceflags & FACE_GROUND ) { + ( *groundsplits )++; + } //end if + } //end if + } //end for + return true; +} //end of the function AAS_TestSplitPlane +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_SplitArea( tmp_area_t *tmparea, int planenum, tmp_area_t **frontarea, tmp_area_t **backarea ) { + int side; + tmp_area_t *facefrontarea, *facebackarea, *faceotherarea; + tmp_face_t *face, *frontface, *backface, *splitface, *nextface; + winding_t *splitwinding; + plane_t *splitplane; + +/* +#ifdef AW_DEBUG + int facesplits, groundsplits, epsilonface; + Log_Print("\n----------------------\n"); + Log_Print("splitting area %d\n", areanum); + Log_Print("with normal = \'%f %f %f\', dist = %f\n", normal[0], normal[1], normal[2], dist); + AAS_TestSplitPlane(areanum, normal, dist, + &facesplits, &groundsplits, &epsilonface); + Log_Print("face splits = %d\nground splits = %d\n", facesplits, groundsplits); + if (epsilonface) Log_Print("aaahh epsilon face\n"); +#endif //AW_DEBUG*/ + //the original area + + AAS_FlipAreaFaces( tmparea ); + AAS_CheckArea( tmparea ); + // + splitplane = &mapplanes[planenum]; +/* //create a split winding, first base winding for plane + splitwinding = BaseWindingForPlane(splitplane->normal, splitplane->dist); + //chop with all the faces of the area + for (face = tmparea->tmpfaces; face && splitwinding; face = face->next[side]) + { + //side of the face the original area was on + side = face->frontarea != tmparea->areanum; + plane = &mapplanes[face->planenum ^ side]; + ChopWindingInPlace(&splitwinding, plane->normal, plane->dist, 0); // PLANESIDE_EPSILON); + } //end for*/ + splitwinding = AAS_SplitWinding( tmparea, planenum ); + if ( !splitwinding ) { +/* +#ifdef DEBUG + AAS_TestSplitPlane(areanum, normal, dist, + &facesplits, &groundsplits, &epsilonface); + Log_Print("\nface splits = %d\nground splits = %d\n", facesplits, groundsplits); + if (epsilonface) Log_Print("aaahh epsilon face\n"); +#endif //DEBUG*/ + Error( "AAS_SplitArea: no split winding when splitting area %d\n", tmparea->areanum ); + } //end if + //create a split face + splitface = AAS_AllocTmpFace(); + //get the map plane + splitface->planenum = planenum; + //store the split winding + splitface->winding = splitwinding; + //the new front area + ( *frontarea ) = AAS_AllocTmpArea(); + ( *frontarea )->presencetype = tmparea->presencetype; + ( *frontarea )->contents = tmparea->contents; + ( *frontarea )->modelnum = tmparea->modelnum; + ( *frontarea )->tmpfaces = NULL; + //the new back area + ( *backarea ) = AAS_AllocTmpArea(); + ( *backarea )->presencetype = tmparea->presencetype; + ( *backarea )->contents = tmparea->contents; + ( *backarea )->modelnum = tmparea->modelnum; + ( *backarea )->tmpfaces = NULL; + //add the split face to the new areas + AAS_AddFaceSideToArea( splitface, 0, ( *frontarea ) ); + AAS_AddFaceSideToArea( splitface, 1, ( *backarea ) ); + + //split all the faces of the original area + for ( face = tmparea->tmpfaces; face; face = nextface ) + { + //side of the face the original area was on + side = face->frontarea != tmparea; + //next face of the original area + nextface = face->next[side]; + //front area of the face + facefrontarea = face->frontarea; + //back area of the face + facebackarea = face->backarea; + //remove the face from both the front and back areas + if ( facefrontarea ) { + AAS_RemoveFaceFromArea( face, facefrontarea ); + } + if ( facebackarea ) { + AAS_RemoveFaceFromArea( face, facebackarea ); + } + //split the face + AAS_SplitFace( face, splitplane->normal, splitplane->dist, &frontface, &backface ); + //free the original face + AAS_FreeTmpFace( face ); + //get the number of the area at the other side of the face + if ( side ) { + faceotherarea = facefrontarea; + } else { faceotherarea = facebackarea;} + //if there is an area at the other side of the original face + if ( faceotherarea ) { + if ( frontface ) { + AAS_AddFaceSideToArea( frontface, !side, faceotherarea ); + } + if ( backface ) { + AAS_AddFaceSideToArea( backface, !side, faceotherarea ); + } + } //end if + //add the front and back part left after splitting the original face to the new areas + if ( frontface ) { + AAS_AddFaceSideToArea( frontface, side, ( *frontarea ) ); + } + if ( backface ) { + AAS_AddFaceSideToArea( backface, side, ( *backarea ) ); + } + } //end for + + if ( !( *frontarea )->tmpfaces ) { + Log_Print( "AAS_SplitArea: front area without faces\n" ); + } + if ( !( *backarea )->tmpfaces ) { + Log_Print( "AAS_SplitArea: back area without faces\n" ); + } + + tmparea->invalid = true; +/* +#ifdef AW_DEBUG + for (i = 0, face = frontarea->tmpfaces; face; face = face->next[side]) + { + side = face->frontarea != frontarea->areanum; + i++; + } //end for + Log_Print("created front area %d with %d faces\n", frontarea->areanum, i); + + for (i = 0, face = backarea->tmpfaces; face; face = face->next[side]) + { + side = face->frontarea != backarea->areanum; + i++; + } //end for + Log_Print("created back area %d with %d faces\n", backarea->areanum, i); +#endif //AW_DEBUG*/ + + AAS_FlipAreaFaces( ( *frontarea ) ); + AAS_FlipAreaFaces( ( *backarea ) ); + // + AAS_CheckArea( ( *frontarea ) ); + AAS_CheckArea( ( *backarea ) ); +} //end of the function AAS_SplitArea +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_FindBestAreaSplitPlane( tmp_area_t *tmparea, vec3_t normal, float *dist ) { + int side1, side2; + int foundsplitter, facesplits, groundsplits, epsilonfaces, bestepsilonfaces; + float bestvalue, value; + tmp_face_t *face1, *face2; + vec3_t tmpnormal, invgravity; + float tmpdist; + + //get inverse of gravity direction + VectorCopy( cfg.phys_gravitydirection, invgravity ); + VectorInverse( invgravity ); + + foundsplitter = false; + bestvalue = -999999; + bestepsilonfaces = 0; + // +#ifdef AW_DEBUG + Log_Print( "finding split plane for area %d\n", tmparea->areanum ); +#endif //AW_DEBUG + for ( face1 = tmparea->tmpfaces; face1; face1 = face1->next[side1] ) + { + //side of the face the area is on + side1 = face1->frontarea != tmparea; + // + if ( WindingIsTiny( face1->winding ) ) { + Log_Write( "gsubdiv: area %d has a tiny winding\r\n", tmparea->areanum ); + continue; + } //end if + //if the face isn't a gap or ground there's no split edge + if ( !( face1->faceflags & FACE_GROUND ) && !AAS_GapFace( face1, side1 ) ) { + continue; + } + // + for ( face2 = face1->next[side1]; face2; face2 = face2->next[side2] ) + { + //side of the face the area is on + side2 = face2->frontarea != tmparea; + // + if ( WindingIsTiny( face1->winding ) ) { + Log_Write( "gsubdiv: area %d has a tiny winding\r\n", tmparea->areanum ); + continue; + } //end if + //if the face isn't a gap or ground there's no split edge + if ( !( face2->faceflags & FACE_GROUND ) && !AAS_GapFace( face2, side2 ) ) { + continue; + } + //only split between gaps and ground + if ( !( ( ( face1->faceflags & FACE_GROUND ) && AAS_GapFace( face2, side2 ) ) || + ( ( face2->faceflags & FACE_GROUND ) && AAS_GapFace( face1, side1 ) ) ) ) { + continue; + } + //find a plane seperating the windings of the faces + if ( !FindPlaneSeperatingWindings( face1->winding, face2->winding, invgravity, + tmpnormal, &tmpdist ) ) { + continue; + } +#ifdef AW_DEBUG + Log_Print( "normal = \'%f %f %f\', dist = %f\n", + tmpnormal[0], tmpnormal[1], tmpnormal[2], tmpdist ); +#endif //AW_DEBUG + //get metrics for this vertical plane + if ( !AAS_TestSplitPlane( tmparea, tmpnormal, tmpdist, + &facesplits, &groundsplits, &epsilonfaces ) ) { + continue; + } //end if +#ifdef AW_DEBUG + Log_Print( "face splits = %d\nground splits = %d\n", + facesplits, groundsplits ); +#endif //AW_DEBUG + value = 100 - facesplits - 2 * groundsplits; + //avoid epsilon faces + value += epsilonfaces * -1000; + if ( value > bestvalue ) { + VectorCopy( tmpnormal, normal ); + *dist = tmpdist; + bestvalue = value; + bestepsilonfaces = epsilonfaces; + foundsplitter = true; + } //end if + } //end for + } //end for + if ( bestepsilonfaces ) { + Log_Write( "found %d epsilon faces trying to split area %d\r\n", + epsilonfaces, tmparea->areanum ); + } //end else + return foundsplitter; +} //end of the function AAS_FindBestAreaSplitPlane +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +tmp_node_t *AAS_SubdivideArea_r( tmp_node_t *tmpnode ) { + int planenum; + tmp_area_t *frontarea, *backarea; + tmp_node_t *tmpnode1, *tmpnode2; + vec3_t normal; + float dist; + + if ( AAS_FindBestAreaSplitPlane( tmpnode->tmparea, normal, &dist ) ) { + qprintf( "\r%6d", ++numgravitationalsubdivisions ); + // + planenum = FindFloatPlane( normal, dist ); + //split the area + AAS_SplitArea( tmpnode->tmparea, planenum, &frontarea, &backarea ); + // + tmpnode->tmparea = NULL; + tmpnode->planenum = FindFloatPlane( normal, dist ); + // + tmpnode1 = AAS_AllocTmpNode(); + tmpnode1->planenum = 0; + tmpnode1->tmparea = frontarea; + // + tmpnode2 = AAS_AllocTmpNode(); + tmpnode2->planenum = 0; + tmpnode2->tmparea = backarea; + //subdivide the areas created by splitting recursively + tmpnode->children[0] = AAS_SubdivideArea_r( tmpnode1 ); + tmpnode->children[1] = AAS_SubdivideArea_r( tmpnode2 ); + } //end if + return tmpnode; +} //end of the function AAS_SubdivideArea_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +tmp_node_t *AAS_GravitationalSubdivision_r( tmp_node_t *tmpnode ) { + //if this is a solid leaf + if ( !tmpnode ) { + return NULL; + } + //negative so it's an area + if ( tmpnode->tmparea ) { + return AAS_SubdivideArea_r( tmpnode ); + } + //do the children recursively + tmpnode->children[0] = AAS_GravitationalSubdivision_r( tmpnode->children[0] ); + tmpnode->children[1] = AAS_GravitationalSubdivision_r( tmpnode->children[1] ); + return tmpnode; +} //end of the function AAS_GravitationalSubdivision_r +//=========================================================================== +// NOTE: merge faces and melt edges first +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_GravitationalSubdivision( void ) { + Log_Write( "AAS_GravitationalSubdivision\r\n" ); + numgravitationalsubdivisions = 0; + qprintf( "%6i gravitational subdivisions", numgravitationalsubdivisions ); + //start with the head node + AAS_GravitationalSubdivision_r( tmpaasworld.nodes ); + qprintf( "\n" ); + Log_Write( "%6i gravitational subdivisions\r\n", numgravitationalsubdivisions ); +} //end of the function AAS_GravitationalSubdivision +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +tmp_node_t *AAS_RefreshLadderSubdividedTree_r( tmp_node_t *tmpnode, tmp_area_t *tmparea, + tmp_node_t *tmpnode1, tmp_node_t *tmpnode2, int planenum ) { + //if this is a solid leaf + if ( !tmpnode ) { + return NULL; + } + //negative so it's an area + if ( tmpnode->tmparea ) { + if ( tmpnode->tmparea == tmparea ) { + tmpnode->tmparea = NULL; + tmpnode->planenum = planenum; + tmpnode->children[0] = tmpnode1; + tmpnode->children[1] = tmpnode2; + } //end if + return tmpnode; + } //end if + //do the children recursively + tmpnode->children[0] = AAS_RefreshLadderSubdividedTree_r( tmpnode->children[0], + tmparea, tmpnode1, tmpnode2, planenum ); + tmpnode->children[1] = AAS_RefreshLadderSubdividedTree_r( tmpnode->children[1], + tmparea, tmpnode1, tmpnode2, planenum ); + return tmpnode; +} //end of the function AAS_RefreshLadderSubdividedTree_r +//=========================================================================== +// find an area with ladder faces and ground faces that are not connected +// split the area with a horizontal plane at the lowest vertex of all +// ladder faces in the area +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +tmp_node_t *AAS_LadderSubdivideArea_r( tmp_node_t *tmpnode ) { + int side1, i, planenum; + int foundladderface, foundgroundface; + float dist; + tmp_area_t *tmparea, *frontarea, *backarea; + tmp_face_t *face1; + tmp_node_t *tmpnode1, *tmpnode2; + vec3_t lowestpoint, normal = {0, 0, 1}; + plane_t *plane; + winding_t *w; + + tmparea = tmpnode->tmparea; + //skip areas with a liquid + if ( tmparea->contents & ( AREACONTENTS_WATER + | AREACONTENTS_LAVA + | AREACONTENTS_SLIME ) ) { + return tmpnode; + } + //must be possible to stand in the area + if ( !( tmparea->presencetype & PRESENCE_NORMAL ) ) { + return tmpnode; + } + // + foundladderface = false; + foundgroundface = false; + lowestpoint[2] = 99999; + // + for ( face1 = tmparea->tmpfaces; face1; face1 = face1->next[side1] ) + { + //side of the face the area is on + side1 = face1->frontarea != tmparea; + //if the face is a ladder face + if ( face1->faceflags & FACE_LADDER ) { + plane = &mapplanes[face1->planenum]; + //the ladder face plane should be pretty much vertical + if ( DotProduct( plane->normal, normal ) > -0.1 ) { + foundladderface = true; + //find lowest point + for ( i = 0; i < face1->winding->numpoints; i++ ) + { + if ( face1->winding->p[i][2] < lowestpoint[2] ) { + VectorCopy( face1->winding->p[i], lowestpoint ); + } //end if + } //end for + } //end if + } //end if + else if ( face1->faceflags & FACE_GROUND ) { + foundgroundface = true; + } //end else if + } //end for + // + if ( ( !foundladderface ) || ( !foundgroundface ) ) { + return tmpnode; + } + // + for ( face1 = tmparea->tmpfaces; face1; face1 = face1->next[side1] ) + { + //side of the face the area is on + side1 = face1->frontarea != tmparea; + //if the face isn't a ground face + if ( !( face1->faceflags & FACE_GROUND ) ) { + continue; + } + //the ground plane + plane = &mapplanes[face1->planenum]; + //get the difference between the ground plane and the lowest point + dist = DotProduct( plane->normal, lowestpoint ) - plane->dist; + //if the lowest point is very near one of the ground planes + if ( dist > -1 && dist < 1 ) { + return tmpnode; + } //end if + } //end for + // + dist = DotProduct( normal, lowestpoint ); + planenum = FindFloatPlane( normal, dist ); + // + w = AAS_SplitWinding( tmparea, planenum ); + if ( !w ) { + return tmpnode; + } + FreeWinding( w ); + //split the area with a horizontal plane through the lowest point + qprintf( "\r%6d", ++numladdersubdivisions ); + // + AAS_SplitArea( tmparea, planenum, &frontarea, &backarea ); + // + tmpnode->tmparea = NULL; + tmpnode->planenum = planenum; + // + tmpnode1 = AAS_AllocTmpNode(); + tmpnode1->planenum = 0; + tmpnode1->tmparea = frontarea; + // + tmpnode2 = AAS_AllocTmpNode(); + tmpnode2->planenum = 0; + tmpnode2->tmparea = backarea; + //subdivide the areas created by splitting recursively + tmpnode->children[0] = AAS_LadderSubdivideArea_r( tmpnode1 ); + tmpnode->children[1] = AAS_LadderSubdivideArea_r( tmpnode2 ); + //refresh the tree + AAS_RefreshLadderSubdividedTree_r( tmpaasworld.nodes, tmparea, tmpnode1, tmpnode2, planenum ); + // + return tmpnode; +} //end of the function AAS_LadderSubdivideArea_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +tmp_node_t *AAS_LadderSubdivision_r( tmp_node_t *tmpnode ) { + //if this is a solid leaf + if ( !tmpnode ) { + return 0; + } + //negative so it's an area + if ( tmpnode->tmparea ) { + return AAS_LadderSubdivideArea_r( tmpnode ); + } + //do the children recursively + tmpnode->children[0] = AAS_LadderSubdivision_r( tmpnode->children[0] ); + tmpnode->children[1] = AAS_LadderSubdivision_r( tmpnode->children[1] ); + return tmpnode; +} //end of the function AAS_LadderSubdivision_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_LadderSubdivision( void ) { + Log_Write( "AAS_LadderSubdivision\r\n" ); + numladdersubdivisions = 0; + qprintf( "%6i ladder subdivisions", numladdersubdivisions ); + //start with the head node + AAS_LadderSubdivision_r( tmpaasworld.nodes ); + // + qprintf( "\n" ); + Log_Write( "%6i ladder subdivisions\r\n", numladdersubdivisions ); +} //end of the function AAS_LadderSubdivision + diff --git a/src/bspc/aas_gsubdiv.h b/src/bspc/aas_gsubdiv.h new file mode 100644 index 0000000..4964a40 --- /dev/null +++ b/src/bspc/aas_gsubdiv.h @@ -0,0 +1,40 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: aas_gsubdiv.h +// Function: Gravitational subdivision +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + +//works with the global tmpaasworld +void AAS_GravitationalSubdivision( void ); +void AAS_LadderSubdivision( void ); diff --git a/src/bspc/aas_map.c b/src/bspc/aas_map.c new file mode 100644 index 0000000..3256d27 --- /dev/null +++ b/src/bspc/aas_map.c @@ -0,0 +1,840 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: aas_map.c +// Function: +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-03 +// Tab Size: 3 +//=========================================================================== + +#include "qbsp.h" +#include "l_mem.h" +#include "../botlib/aasfile.h" //aas_bbox_t +#include "aas_store.h" //AAS_MAX_BBOXES +#include "aas_cfg.h" +#include "../game/surfaceflags.h" + +#define SPAWNFLAG_NOT_EASY 0x00000100 +#define SPAWNFLAG_NOT_MEDIUM 0x00000200 +#define SPAWNFLAG_NOT_HARD 0x00000400 +#define SPAWNFLAG_NOT_DEATHMATCH 0x00000800 +#define SPAWNFLAG_NOT_COOP 0x00001000 + +#define STATE_TOP 0 +#define STATE_BOTTOM 1 +#define STATE_UP 2 +#define STATE_DOWN 3 + +#define DOOR_START_OPEN 1 +#define DOOR_REVERSE 2 +#define DOOR_CRUSHER 4 +#define DOOR_NOMONSTER 8 +#define DOOR_TOGGLE 32 +#define DOOR_X_AXIS 64 +#define DOOR_Y_AXIS 128 + +#define BBOX_NORMAL_EPSILON 0.00001 + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +vec_t BoxOriginDistanceFromPlane( vec3_t normal, vec3_t mins, vec3_t maxs, int side ) { + vec3_t v1, v2; + int i; + + if ( side ) { + for ( i = 0; i < 3; i++ ) + { + if ( normal[i] > BBOX_NORMAL_EPSILON ) { + v1[i] = maxs[i]; + } else if ( normal[i] < -BBOX_NORMAL_EPSILON ) { + v1[i] = mins[i]; + } else { v1[i] = 0;} + } //end for + } //end if + else + { + for ( i = 0; i < 3; i++ ) + { + if ( normal[i] > BBOX_NORMAL_EPSILON ) { + v1[i] = mins[i]; + } else if ( normal[i] < -BBOX_NORMAL_EPSILON ) { + v1[i] = maxs[i]; + } else { v1[i] = 0;} + } //end for + } //end else + VectorCopy( normal, v2 ); + VectorInverse( v2 ); + return DotProduct( v1, v2 ); +} //end of the function BoxOriginDistanceFromPlane +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +vec_t CapsuleOriginDistanceFromPlane( vec3_t normal, vec3_t mins, vec3_t maxs ) { + float offset_up, offset_down, width, radius; + + width = maxs[0] - mins[0]; + // if the box is less high then it is wide + if ( maxs[2] - mins[2] < width ) { + width = maxs[2] - mins[2]; + } + radius = width * 0.5; + // offset to upper and lower sphere + offset_up = maxs[2] - radius; + offset_down = -mins[2] - radius; + + // if normal points upward + if ( normal[2] > 0 ) { + // touches lower sphere first + return normal[2] * offset_down + radius; + } else { + // touched upper sphere first + return -normal[2] * offset_up + radius; + } +} +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ExpandMapBrush( mapbrush_t *brush, vec3_t mins, vec3_t maxs ) { + int sn; + float dist; + side_t *s; + plane_t *plane; + + for ( sn = 0; sn < brush->numsides; sn++ ) + { + s = brush->original_sides + sn; + plane = &mapplanes[s->planenum]; + dist = plane->dist; + if ( capsule_collision ) { + dist += CapsuleOriginDistanceFromPlane( plane->normal, mins, maxs ); + } else { + dist += BoxOriginDistanceFromPlane( plane->normal, mins, maxs, 0 ); + } + s->planenum = FindFloatPlane( plane->normal, dist ); + //the side isn't a bevel after expanding + s->flags &= ~SFL_BEVEL; + //don't skip the surface + s->surf &= ~SURF_SKIP; + //make sure the texinfo is not TEXINFO_NODE + //when player clip contents brushes are read from the bsp tree + //they have the texinfo field set to TEXINFO_NODE + //s->texinfo = 0; + } //end for +} //end of the function AAS_ExpandMapBrush +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_SetTexinfo( mapbrush_t *brush ) { + int n; + side_t *side; + + if ( brush->contents & ( CONTENTS_LADDER + | CONTENTS_AREAPORTAL + | CONTENTS_CLUSTERPORTAL + | CONTENTS_TELEPORTER + | CONTENTS_JUMPPAD + | CONTENTS_DONOTENTER + | CONTENTS_DONOTENTER_LARGE + | CONTENTS_WATER + | CONTENTS_LAVA + | CONTENTS_SLIME + | CONTENTS_WINDOW + | CONTENTS_PLAYERCLIP ) ) { + //we just set texinfo to 0 because these brush sides MUST be used as + //bsp splitters textured or not textured + for ( n = 0; n < brush->numsides; n++ ) + { + side = brush->original_sides + n; + //side->flags |= SFL_TEXTURED|SFL_VISIBLE; + side->texinfo = 0; + } //end for + } //end if + else + { + //only use brush sides as splitters if they are textured + //texinfo of non-textured sides will be set to TEXINFO_NODE + for ( n = 0; n < brush->numsides; n++ ) + { + side = brush->original_sides + n; + //don't use side as splitter (set texinfo to TEXINFO_NODE) if not textured + if ( side->flags & ( SFL_TEXTURED | SFL_BEVEL ) ) { + side->texinfo = 0; + } else { side->texinfo = TEXINFO_NODE;} + } //end for + } //end else +} //end of the function AAS_SetTexinfo +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FreeBrushWindings( mapbrush_t *brush ) { + int n; + side_t *side; + // + for ( n = 0; n < brush->numsides; n++ ) + { + side = brush->original_sides + n; + // + if ( side->winding ) { + FreeWinding( side->winding ); + } + } //end for +} //end of the function FreeBrushWindings +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_AddMapBrushSide( mapbrush_t *brush, int planenum ) { + side_t *side; + // + if ( nummapbrushsides >= MAX_MAPFILE_BRUSHSIDES ) { + Error( "MAX_MAPFILE_BRUSHSIDES" ); + } + // + side = brush->original_sides + brush->numsides; + side->original = NULL; + side->winding = NULL; + side->contents = brush->contents; + side->flags &= ~( SFL_BEVEL | SFL_VISIBLE ); + side->surf = 0; + side->planenum = planenum; + side->texinfo = 0; + // + nummapbrushsides++; + brush->numsides++; +} //end of the function AAS_AddMapBrushSide +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FixMapBrush( mapbrush_t *brush ) { + int i, j, planenum; + float dist; + winding_t *w; + plane_t *plane, *plane1, *plane2; + side_t *side; + vec3_t normal; + + //calculate the brush bounds + ClearBounds( brush->mins, brush->maxs ); + for ( i = 0; i < brush->numsides; i++ ) + { + plane = &mapplanes[brush->original_sides[i].planenum]; + w = BaseWindingForPlane( plane->normal, plane->dist ); + for ( j = 0; j < brush->numsides && w; j++ ) + { + if ( i == j ) { + continue; + } + //there are no brush bevels marked but who cares :) + if ( brush->original_sides[j].flags & SFL_BEVEL ) { + continue; + } + plane = &mapplanes[brush->original_sides[j].planenum ^ 1]; + ChopWindingInPlace( &w, plane->normal, plane->dist, 0 ); //CLIP_EPSILON); + } //end for + + side = &brush->original_sides[i]; + side->winding = w; + if ( w ) { + for ( j = 0; j < w->numpoints; j++ ) + { + AddPointToBounds( w->p[j], brush->mins, brush->maxs ); + } //end for + } //end if + } //end for + // + for ( i = 0; i < brush->numsides; i++ ) + { + for ( j = 0; j < brush->numsides; j++ ) + { + if ( i == j ) { + continue; + } + plane1 = &mapplanes[brush->original_sides[i].planenum]; + plane2 = &mapplanes[brush->original_sides[j].planenum]; + if ( WindingsNonConvex( brush->original_sides[i].winding, + brush->original_sides[j].winding, + plane1->normal, plane2->normal, + plane1->dist, plane2->dist ) ) { + Log_Print( "non convex brush" ); + } //end if + } //end for + } //end for + + //NOW close the fucking brush!! + for ( i = 0; i < 3; i++ ) + { + if ( brush->mins[i] < -MAX_MAP_BOUNDS ) { + VectorClear( normal ); + normal[i] = -1; + dist = MAX_MAP_BOUNDS - 10; + planenum = FindFloatPlane( normal, dist ); + // + Log_Print( "mins out of range: added extra brush side\n" ); + AAS_AddMapBrushSide( brush, planenum ); + } //end if + if ( brush->maxs[i] > MAX_MAP_BOUNDS ) { + VectorClear( normal ); + normal[i] = 1; + dist = MAX_MAP_BOUNDS - 10; + planenum = FindFloatPlane( normal, dist ); + // + Log_Print( "maxs out of range: added extra brush side\n" ); + AAS_AddMapBrushSide( brush, planenum ); + } //end if + if ( brush->mins[i] > MAX_MAP_BOUNDS || brush->maxs[i] < -MAX_MAP_BOUNDS ) { + Log_Print( "entity %i, brush %i: no visible sides on brush\n", brush->entitynum, brush->brushnum ); + } //end if + } //end for + //free all the windings + FreeBrushWindings( brush ); +} //end of the function AAS_FixMapBrush +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_MakeBrushWindings( mapbrush_t *ob ) { + int i, j; + winding_t *w; + side_t *side; + plane_t *plane, *plane1, *plane2; + + ClearBounds( ob->mins, ob->maxs ); + + for ( i = 0; i < ob->numsides; i++ ) + { + plane = &mapplanes[ob->original_sides[i].planenum]; + w = BaseWindingForPlane( plane->normal, plane->dist ); + for ( j = 0; j < ob->numsides && w; j++ ) + { + if ( i == j ) { + continue; + } + if ( ob->original_sides[j].flags & SFL_BEVEL ) { + continue; + } + plane = &mapplanes[ob->original_sides[j].planenum ^ 1]; + ChopWindingInPlace( &w, plane->normal, plane->dist, 0 ); //CLIP_EPSILON); + } + + side = &ob->original_sides[i]; + side->winding = w; + if ( w ) { + side->flags |= SFL_VISIBLE; + for ( j = 0; j < w->numpoints; j++ ) + AddPointToBounds( w->p[j], ob->mins, ob->maxs ); + } + } + //check if the brush is convex + for ( i = 0; i < ob->numsides; i++ ) + { + for ( j = 0; j < ob->numsides; j++ ) + { + if ( i == j ) { + continue; + } + plane1 = &mapplanes[ob->original_sides[i].planenum]; + plane2 = &mapplanes[ob->original_sides[j].planenum]; + if ( WindingsNonConvex( ob->original_sides[i].winding, + ob->original_sides[j].winding, + plane1->normal, plane2->normal, + plane1->dist, plane2->dist ) ) { + Log_Print( "non convex brush" ); + } //end if + } //end for + } //end for + //check for out of bound brushes + for ( i = 0; i < 3; i++ ) + { + //IDBUG: all the indexes into the mins and maxs were zero (not using i) + if ( ob->mins[i] < -MAX_MAP_BOUNDS || ob->maxs[i] > MAX_MAP_BOUNDS ) { + Log_Print( "entity %i, brush %i: bounds out of range\n", ob->entitynum, ob->brushnum ); + Log_Print( "ob->mins[%d] = %f, ob->maxs[%d] = %f\n", i, ob->mins[i], i, ob->maxs[i] ); + ob->numsides = 0; //remove the brush + break; + } //end if + if ( ob->mins[i] > MAX_MAP_BOUNDS || ob->maxs[i] < -MAX_MAP_BOUNDS ) { + Log_Print( "entity %i, brush %i: no visible sides on brush\n", ob->entitynum, ob->brushnum ); + Log_Print( "ob->mins[%d] = %f, ob->maxs[%d] = %f\n", i, ob->mins[i], i, ob->maxs[i] ); + ob->numsides = 0; //remove the brush + break; + } //end if + } //end for + return true; +} //end of the function AAS_MakeBrushWindings +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +mapbrush_t *AAS_CopyMapBrush( mapbrush_t *brush, entity_t *mapent ) { + int n; + mapbrush_t *newbrush; + side_t *side, *newside; + + if ( nummapbrushes >= MAX_MAPFILE_BRUSHES ) { + Error( "MAX_MAPFILE_BRUSHES" ); + } + + newbrush = &mapbrushes[nummapbrushes]; + newbrush->original_sides = &brushsides[nummapbrushsides]; + newbrush->entitynum = brush->entitynum; + newbrush->brushnum = nummapbrushes - mapent->firstbrush; + newbrush->numsides = brush->numsides; + newbrush->contents = brush->contents; + + //copy the sides + for ( n = 0; n < brush->numsides; n++ ) + { + if ( nummapbrushsides >= MAX_MAPFILE_BRUSHSIDES ) { + Error( "MAX_MAPFILE_BRUSHSIDES" ); + } + side = brush->original_sides + n; + + newside = newbrush->original_sides + n; + newside->original = NULL; + newside->winding = NULL; + newside->contents = side->contents; + newside->flags = side->flags; + newside->surf = side->surf; + newside->planenum = side->planenum; + newside->texinfo = side->texinfo; + nummapbrushsides++; + } //end for + // + nummapbrushes++; + mapent->numbrushes++; + return newbrush; +} //end of the function AAS_CopyMapBrush +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_AlwaysTriggered( char *targetname ) { + int i; + + if ( !strlen( targetname ) ) { + return false; + } + // + for ( i = 0; i < num_entities; i++ ) + { + //if the entity will activate the given targetname + if ( !strcmp( targetname, ValueForKey( &entities[i], "target" ) ) ) { + //if this activator is present in deathmatch + if ( !( atoi( ValueForKey( &entities[i], "spawnflags" ) ) & SPAWNFLAG_NOT_DEATHMATCH ) ) { + //if it is a trigger_always entity + if ( !strcmp( "trigger_always", ValueForKey( &entities[i], "classname" ) ) ) { + return true; + } //end if + //check for possible trigger_always entities activating this entity + if ( AAS_AlwaysTriggered( ValueForKey( &entities[i], "targetname" ) ) ) { + return true; + } //end if + } //end if + } //end if + } //end for + return false; +} //end of the function AAS_AlwaysTriggered +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_ValidEntity( entity_t *mapent ) { + int i; + char target[1024]; + + //all world brushes are used for AAS + if ( mapent == &entities[0] ) { + return true; + } //end if + //some of the func_wall brushes are also used for AAS + else if ( !strcmp( "func_wall", ValueForKey( mapent, "classname" ) ) ) { + //Log_Print("found func_wall entity %d\n", mapent - entities); + //if the func wall is used in deathmatch + //if (!(atoi(ValueForKey(mapent, "spawnflags")) & SPAWNFLAG_NOT_DEATHMATCH)) + { + //Log_Print("func_wall USED in deathmatch mode %d\n", atoi(ValueForKey(mapent, "spawnflags"))); + return true; + } //end if + } //end else if + else if ( !strcmp( "func_door_rotating", ValueForKey( mapent, "classname" ) ) || + !strcmp( "func_door", ValueForKey( mapent, "classname" ) ) || + !strcmp( "func_invisible_user", ValueForKey( mapent, "classname" ) ) ) { + //if the func_door_rotating is present in deathmatch + //if (!(atoi(ValueForKey(mapent, "spawnflags")) & SPAWNFLAG_NOT_DEATHMATCH)) + { + //if the func_door_rotating is always activated in deathmatch + if ( AAS_AlwaysTriggered( ValueForKey( mapent, "targetname" ) ) ) { + //Log_Print("found func_door_rotating in deathmatch\ntargetname %s\n", ValueForKey(mapent, "targetname")); + return true; + } //end if + } //end if + } //end else if + else if ( !strcmp( "trigger_hurt", ValueForKey( mapent, "classname" ) ) ) { + // RF, spawnflag & 1 is for delayed spawn, so ignore it + if ( atoi( ValueForKey( mapent, "spawnflags" ) ) & 1 ) { + return false; + } + + //"dmg" is the damage, for instance: "dmg" "666" + return true; + } //end else if + else if ( !strcmp( "trigger_push", ValueForKey( mapent, "classname" ) ) ) { + return true; + } //end else if + else if ( !strcmp( "trigger_multiple", ValueForKey( mapent, "classname" ) ) ) { + //find out if the trigger_multiple is pointing to a target_teleporter + strcpy( target, ValueForKey( mapent, "target" ) ); + for ( i = 0; i < num_entities; i++ ) + { + //if the entity will activate the given targetname + if ( !strcmp( target, ValueForKey( &entities[i], "targetname" ) ) ) { + if ( !strcmp( "target_teleporter", ValueForKey( &entities[i], "classname" ) ) ) { + return true; + } //end if + } //end if + } //end for + } //end else if + else if ( !strcmp( "trigger_teleport", ValueForKey( mapent, "classname" ) ) ) { + return true; + } //end else if + else if ( !strcmp( "func_tramcar", ValueForKey( mapent, "classname" ) ) ) { + return true; + } //end else if + else if ( !strcmp( "func_invisible_user", ValueForKey( mapent, "classname" ) ) ) { + return true; + } + /* + else if (!strcmp("func_static", ValueForKey(mapent, "classname"))) + { + //FIXME: easy/medium/hard/deathmatch specific? + return true; + } //end else if + */ + return false; +} //end of the function AAS_ValidEntity +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_TransformPlane( int planenum, vec3_t origin, vec3_t angles ) { + float newdist, matrix[3][3]; + vec3_t normal; + + //rotate the node plane + VectorCopy( mapplanes[planenum].normal, normal ); + CreateRotationMatrix( angles, matrix ); + RotatePoint( normal, matrix ); + newdist = mapplanes[planenum].dist + DotProduct( normal, origin ); + return FindFloatPlane( normal, newdist ); +} //end of the function AAS_TransformPlane +//=========================================================================== +// this function sets the func_rotating_door in it's final position +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_PositionFuncRotatingBrush( entity_t *mapent, mapbrush_t *brush ) { + int spawnflags, i; + float distance; + vec3_t movedir, angles, pos1, pos2; + side_t *s; + + spawnflags = FloatForKey( mapent, "spawnflags" ); + VectorClear( movedir ); + if ( spawnflags & DOOR_X_AXIS ) { + movedir[2] = 1.0; //roll + } else if ( spawnflags & DOOR_Y_AXIS ) { + movedir[0] = 1.0; //pitch + } else { // Z_AXIS + movedir[1] = 1.0; //yaw + + } + // check for reverse rotation + if ( spawnflags & DOOR_REVERSE ) { + VectorInverse( movedir ); + } + + distance = FloatForKey( mapent, "distance" ); + if ( !distance ) { + distance = 90; + } + + GetVectorForKey( mapent, "angles", angles ); + VectorCopy( angles, pos1 ); + VectorMA( angles, -distance, movedir, pos2 ); + // if it starts open, switch the positions + if ( spawnflags & DOOR_START_OPEN ) { + VectorCopy( pos2, angles ); + VectorCopy( pos1, pos2 ); + VectorCopy( angles, pos1 ); + VectorInverse( movedir ); + } //end if + // + for ( i = 0; i < brush->numsides; i++ ) + { + s = &brush->original_sides[i]; + s->planenum = AAS_TransformPlane( s->planenum, mapent->origin, pos2 ); + } //end for + // + FreeBrushWindings( brush ); + AAS_MakeBrushWindings( brush ); + AddBrushBevels( brush ); + FreeBrushWindings( brush ); +} //end of the function AAS_PositionFuncRotatingBrush +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_PositionBrush( entity_t *mapent, mapbrush_t *brush ) { + side_t *s; + float newdist; + int i; + char *model; + + if ( !strcmp( ValueForKey( mapent, "classname" ), "func_door_rotating" ) ) { + AAS_PositionFuncRotatingBrush( mapent, brush ); + } //end if + else + { + if ( mapent->origin[0] || mapent->origin[1] || mapent->origin[2] ) { + for ( i = 0; i < brush->numsides; i++ ) + { + s = &brush->original_sides[i]; + newdist = mapplanes[s->planenum].dist + + DotProduct( mapplanes[s->planenum].normal, mapent->origin ); + s->planenum = FindFloatPlane( mapplanes[s->planenum].normal, newdist ); + } //end for + } //end if + // RF, disabled for Wolf, we dont use trigger_hurt for lava + //if it's a trigger hurt + //if (!strcmp("trigger_hurt", ValueForKey(mapent, "classname"))) + //{ + // //set the lava contents + // brush->contents |= CONTENTS_LAVA; + // //Log_Print("found trigger_hurt brush\n"); + //} //end if + // + else if ( !strcmp( "trigger_push", ValueForKey( mapent, "classname" ) ) ) { + //set the jumppad contents + brush->contents = CONTENTS_JUMPPAD; + //Log_Print("found trigger_push brush\n"); + } //end if + // + else if ( !strcmp( "trigger_multiple", ValueForKey( mapent, "classname" ) ) ) { + //set teleporter contents + brush->contents = CONTENTS_TELEPORTER; + //Log_Print("found trigger_multiple teleporter brush\n"); + } //end if + // + else if ( !strcmp( "trigger_teleport", ValueForKey( mapent, "classname" ) ) ) { + //set teleporter contents + brush->contents = CONTENTS_TELEPORTER; + //Log_Print("found trigger_teleport teleporter brush\n"); + } //end if + else if ( !strcmp( "func_door", ValueForKey( mapent, "classname" ) ) ) { + //set mover contents + brush->contents = CONTENTS_MOVER; + //get the model number + model = ValueForKey( mapent, "model" ); + brush->modelnum = atoi( model + 1 ); + } //end if + else if ( !strcmp( "func_invisible_user", ValueForKey( mapent, "classname" ) ) ) { + //set mover contents + brush->contents = CONTENTS_TRIGGER; + } //end if + + } //end else +} //end of the function AAS_PositionBrush +//=========================================================================== +// uses the global cfg_t cfg +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_CreateMapBrushes( mapbrush_t *brush, entity_t *mapent, int addbevels ) { + int i; + //side_t *s; + mapbrush_t *bboxbrushes[16]; + + //if the brushes are not from an entity used for AAS + if ( !AAS_ValidEntity( mapent ) ) { + nummapbrushsides -= brush->numsides; + brush->numsides = 0; + return; + } //end if + // + AAS_PositionBrush( mapent, brush ); + //from all normal solid brushes only the textured brush sides will + //be used as bsp splitters, so set the right texinfo reference here + AAS_SetTexinfo( brush ); + //remove contents detail flag, otherwise player clip contents won't be + //bsped correctly for AAS! + brush->contents &= ~CONTENTS_DETAIL; + //if the brush has contents area portal it should be the only contents + if ( brush->contents & ( CONTENTS_AREAPORTAL | CONTENTS_CLUSTERPORTAL ) ) { + brush->contents = CONTENTS_CLUSTERPORTAL; + brush->leafnum = -1; + } //end if + //window and playerclip are used for player clipping, make them solid + if ( brush->contents & ( CONTENTS_WINDOW | CONTENTS_PLAYERCLIP ) ) { + // + brush->contents &= ~( CONTENTS_WINDOW | CONTENTS_PLAYERCLIP ); + brush->contents |= CONTENTS_SOLID; + brush->leafnum = -1; + } //end if + // +// Rafael TBD: no flag to support CONTENTS_BOTCLIP +/* + if (brush->contents & CONTENTS_BOTCLIP) + { + brush->contents = CONTENTS_SOLID; + brush->leafnum = -1; + } // end if +*/ + // + //Log_Write("brush %d contents = ", brush->brushnum); + //PrintContents(brush->contents); + //Log_Write("\r\n"); + //if not one of the following brushes then the brush is NOT used for AAS + if ( !( brush->contents & ( CONTENTS_SOLID + | CONTENTS_LADDER + | CONTENTS_CLUSTERPORTAL + | CONTENTS_DONOTENTER + | CONTENTS_DONOTENTER_LARGE + | CONTENTS_TELEPORTER + | CONTENTS_JUMPPAD + | CONTENTS_WATER + | CONTENTS_LAVA + | CONTENTS_SLIME + | CONTENTS_MOVER + ) ) ) { + nummapbrushsides -= brush->numsides; + brush->numsides = 0; + return; + } //end if + //fix the map brush + //AAS_FixMapBrush(brush); + //if brush bevels should be added (for real map brushes, not bsp map brushes) + if ( addbevels ) { + //NOTE: we first have to get the mins and maxs of the brush before + // creating the brush bevels... the mins and maxs are used to + // create them. so we call MakeBrushWindings to get the mins + // and maxs and then after creating the bevels we free the + // windings because they are created for all sides (including + // bevels) a little later + AAS_MakeBrushWindings( brush ); + AddBrushBevels( brush ); + FreeBrushWindings( brush ); + } //end if + //NOTE: add the brush to the WORLD entity!!! + mapent = &entities[0]; + //there's at least one new brush for now + nummapbrushes++; + mapent->numbrushes++; + //liquid brushes are expanded for the maximum possible bounding box + if ( brush->contents & ( CONTENTS_WATER + | CONTENTS_LAVA + | CONTENTS_SLIME + | CONTENTS_TELEPORTER + | CONTENTS_JUMPPAD + | CONTENTS_DONOTENTER + | CONTENTS_DONOTENTER_LARGE + | CONTENTS_MOVER + ) ) { + brush->expansionbbox = 0; + //NOTE: the first bounding box is the max + //FIXME: use max bounding box created from all bboxes + AAS_ExpandMapBrush( brush, cfg.bboxes[0].mins, cfg.bboxes[0].maxs ); + AAS_MakeBrushWindings( brush ); + } //end if + //area portal brushes are NOT expanded + else if ( brush->contents & CONTENTS_CLUSTERPORTAL ) { + brush->expansionbbox = 0; + //NOTE: the first bounding box is the max + //FIXME: use max bounding box created from all bboxes + AAS_ExpandMapBrush( brush, cfg.bboxes[0].mins, cfg.bboxes[0].maxs ); + AAS_MakeBrushWindings( brush ); + } //end if + //all solid brushes are expanded for all bounding boxes + else if ( brush->contents & ( CONTENTS_SOLID + | CONTENTS_LADDER + ) ) { + //brush for the first bounding box + bboxbrushes[0] = brush; + //make a copy for the other bounding boxes + for ( i = 1; i < cfg.numbboxes; i++ ) + { + bboxbrushes[i] = AAS_CopyMapBrush( brush, mapent ); + } //end for + //expand every brush for it's bounding box and create windings + for ( i = 0; i < cfg.numbboxes; i++ ) + { + AAS_ExpandMapBrush( bboxbrushes[i], cfg.bboxes[i].mins, cfg.bboxes[i].maxs ); + bboxbrushes[i]->expansionbbox = cfg.bboxes[i].presencetype; + AAS_MakeBrushWindings( bboxbrushes[i] ); + } //end for + } //end else +} //end of the function AAS_CreateMapBrushes diff --git a/src/bspc/aas_map.h b/src/bspc/aas_map.h new file mode 100644 index 0000000..d41611e --- /dev/null +++ b/src/bspc/aas_map.h @@ -0,0 +1,38 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: aas_map.h +// Function: +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + +void AAS_CreateMapBrushes( mapbrush_t *brush, entity_t *mapent, int addbevels ); diff --git a/src/bspc/aas_prunenodes.c b/src/bspc/aas_prunenodes.c new file mode 100644 index 0000000..1dc6e38 --- /dev/null +++ b/src/bspc/aas_prunenodes.c @@ -0,0 +1,103 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: aas_prunenodes.c +// Function: Prune Nodes +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + +#include "qbsp.h" +#include "../botlib/aasfile.h" +#include "aas_create.h" + +int c_numprunes; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +tmp_node_t *AAS_PruneNodes_r( tmp_node_t *tmpnode ) { + tmp_area_t *tmparea1, *tmparea2; + + //if it is a solid leaf + if ( !tmpnode ) { + return NULL; + } + // + if ( tmpnode->tmparea ) { + return tmpnode; + } + //process the children first + tmpnode->children[0] = AAS_PruneNodes_r( tmpnode->children[0] ); + tmpnode->children[1] = AAS_PruneNodes_r( tmpnode->children[1] ); + //if both children are areas + if ( tmpnode->children[0] && tmpnode->children[1] && + tmpnode->children[0]->tmparea && tmpnode->children[1]->tmparea ) { + tmparea1 = tmpnode->children[0]->tmparea; + while ( tmparea1->mergedarea ) tmparea1 = tmparea1->mergedarea; + + tmparea2 = tmpnode->children[1]->tmparea; + while ( tmparea2->mergedarea ) tmparea2 = tmparea2->mergedarea; + + if ( tmparea1 == tmparea2 ) { + c_numprunes++; + tmpnode->tmparea = tmparea1; + tmpnode->planenum = 0; + AAS_FreeTmpNode( tmpnode->children[0] ); + AAS_FreeTmpNode( tmpnode->children[1] ); + tmpnode->children[0] = NULL; + tmpnode->children[1] = NULL; + return tmpnode; + } //end if + } //end if + //if both solid leafs + if ( !tmpnode->children[0] && !tmpnode->children[1] ) { + c_numprunes++; + AAS_FreeTmpNode( tmpnode ); + return NULL; + } //end if + // + return tmpnode; +} //end of the function AAS_PruneNodes_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_PruneNodes( void ) { + Log_Write( "AAS_PruneNodes\r\n" ); + AAS_PruneNodes_r( tmpaasworld.nodes ); + Log_Print( "%6d nodes pruned\r\n", c_numprunes ); +} //end of the function AAS_PruneNodes diff --git a/src/bspc/aas_prunenodes.h b/src/bspc/aas_prunenodes.h new file mode 100644 index 0000000..cacc59c --- /dev/null +++ b/src/bspc/aas_prunenodes.h @@ -0,0 +1,39 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: aas_prunenodes.h +// Function: Prune Nodes +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + +void AAS_PruneNodes( void ); + diff --git a/src/bspc/aas_store.c b/src/bspc/aas_store.c new file mode 100644 index 0000000..70ffc4f --- /dev/null +++ b/src/bspc/aas_store.c @@ -0,0 +1,1124 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: aas_store.c +// Function: +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + +#include "qbsp.h" +#include "../botlib/aasfile.h" +#include "aas_file.h" +#include "aas_store.h" +#include "aas_create.h" +#include "aas_cfg.h" + + +//#define NOTHREEVERTEXFACES + +#define STOREPLANESDOUBLE + +#define VERTEX_EPSILON 0.1 //NOTE: changed from 0.5 + +// MrE: use same epsilons as q3map! + +//NOTE: changed from 0.0001 +#define NORMAL_EPSILON 0.00001 +//NOTE: changed from 0.05 +#define DIST_EPSILON 0.01 + +#define INTEGRAL_EPSILON 0.01 +#define VEREX_EPSILON 0.1 //NOTE: changed from 0.5 +#define VERTEX_HASHING +#define VERTEX_HASH_SHIFT 7 +#define VERTEX_HASH_SIZE ( ( MAX_MAP_BOUNDS >> ( VERTEX_HASH_SHIFT - 1 ) ) + 1 ) //was 64 +// +#define PLANE_HASHING +#define PLANE_HASH_SIZE 1024 //must be power of 2 +// +#define EDGE_HASHING +#define EDGE_HASH_SIZE 1024 //must be power of 2 + +// Ridah +aas_t aasworlds[1]; +aas_t( *aasworld ); +// done. + +//vertex hash +int *aas_vertexchain; // the next vertex in a hash chain +int aas_hashverts[VERTEX_HASH_SIZE * VERTEX_HASH_SIZE]; // a vertex number, or 0 for no verts +//plane hash +int *aas_planechain; +int aas_hashplanes[PLANE_HASH_SIZE]; +//edge hash +int *aas_edgechain; +int aas_hashedges[EDGE_HASH_SIZE]; + +int allocatedaasmem = 0; + +int groundfacesonly = false; //true; +// +typedef struct max_aas_s +{ + int max_bboxes; + int max_vertexes; + int max_planes; + int max_edges; + int max_edgeindexsize; + int max_faces; + int max_faceindexsize; + int max_areas; + int max_areasettings; + int max_reachabilitysize; + int max_nodes; + int max_portals; + int max_portalindexsize; + int max_clusters; +} max_aas_t; +//maximums of everything +max_aas_t max_aas; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_CountTmpNodes( tmp_node_t *tmpnode ) { + if ( !tmpnode ) { + return 0; + } + return AAS_CountTmpNodes( tmpnode->children[0] ) + + AAS_CountTmpNodes( tmpnode->children[1] ) + 1; +} //end of the function AAS_CountTmpNodes +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_InitMaxAAS( void ) { + int numfaces, numpoints, numareas; + tmp_face_t *f; + tmp_area_t *a; + + numpoints = 0; + numfaces = 0; + for ( f = tmpaasworld.faces; f; f = f->l_next ) + { + numfaces++; + if ( f->winding ) { + numpoints += f->winding->numpoints; + } + } //end for + // + numareas = 0; + for ( a = tmpaasworld.areas; a; a = a->l_next ) + { + numareas++; + } //end for + max_aas.max_bboxes = AAS_MAX_BBOXES; + max_aas.max_vertexes = numpoints + 1; + max_aas.max_planes = nummapplanes; + max_aas.max_edges = numpoints + 1; + max_aas.max_edgeindexsize = ( numpoints + 1 ) * 3; + max_aas.max_faces = numfaces + 10; + max_aas.max_faceindexsize = ( numfaces + 10 ) * 2; + max_aas.max_areas = numareas + 10; + max_aas.max_areasettings = numareas + 10; + max_aas.max_reachabilitysize = 0; + max_aas.max_nodes = AAS_CountTmpNodes( tmpaasworld.nodes ) + 10; + max_aas.max_portals = 0; + max_aas.max_portalindexsize = 0; + max_aas.max_clusters = 0; +} //end of the function AAS_InitMaxAAS +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_AllocMaxAAS( void ) { + int i; + + AAS_InitMaxAAS(); + //bounding boxes + ( *aasworld ).numbboxes = 0; + ( *aasworld ).bboxes = (aas_bbox_t *) GetClearedMemory( max_aas.max_bboxes * sizeof( aas_bbox_t ) ); + allocatedaasmem += max_aas.max_bboxes * sizeof( aas_bbox_t ); + //vertexes + ( *aasworld ).numvertexes = 0; + ( *aasworld ).vertexes = (aas_vertex_t *) GetClearedMemory( max_aas.max_vertexes * sizeof( aas_vertex_t ) ); + allocatedaasmem += max_aas.max_vertexes * sizeof( aas_vertex_t ); + //planes + ( *aasworld ).numplanes = 0; + ( *aasworld ).planes = (aas_plane_t *) GetClearedMemory( max_aas.max_planes * sizeof( aas_plane_t ) ); + allocatedaasmem += max_aas.max_planes * sizeof( aas_plane_t ); + //edges + ( *aasworld ).numedges = 0; + ( *aasworld ).edges = (aas_edge_t *) GetClearedMemory( max_aas.max_edges * sizeof( aas_edge_t ) ); + allocatedaasmem += max_aas.max_edges * sizeof( aas_edge_t ); + //edge index + ( *aasworld ).edgeindexsize = 0; + ( *aasworld ).edgeindex = (aas_edgeindex_t *) GetClearedMemory( max_aas.max_edgeindexsize * sizeof( aas_edgeindex_t ) ); + allocatedaasmem += max_aas.max_edgeindexsize * sizeof( aas_edgeindex_t ); + //faces + ( *aasworld ).numfaces = 0; + ( *aasworld ).faces = (aas_face_t *) GetClearedMemory( max_aas.max_faces * sizeof( aas_face_t ) ); + allocatedaasmem += max_aas.max_faces * sizeof( aas_face_t ); + //face index + ( *aasworld ).faceindexsize = 0; + ( *aasworld ).faceindex = (aas_faceindex_t *) GetClearedMemory( max_aas.max_faceindexsize * sizeof( aas_faceindex_t ) ); + allocatedaasmem += max_aas.max_faceindexsize * sizeof( aas_faceindex_t ); + //convex areas + ( *aasworld ).numareas = 0; + ( *aasworld ).areas = (aas_area_t *) GetClearedMemory( max_aas.max_areas * sizeof( aas_area_t ) ); + allocatedaasmem += max_aas.max_areas * sizeof( aas_area_t ); + //convex area settings + ( *aasworld ).numareasettings = 0; + ( *aasworld ).areasettings = (aas_areasettings_t *) GetClearedMemory( max_aas.max_areasettings * sizeof( aas_areasettings_t ) ); + allocatedaasmem += max_aas.max_areasettings * sizeof( aas_areasettings_t ); + //reachablity list + ( *aasworld ).reachabilitysize = 0; + ( *aasworld ).reachability = (aas_reachability_t *) GetClearedMemory( max_aas.max_reachabilitysize * sizeof( aas_reachability_t ) ); + allocatedaasmem += max_aas.max_reachabilitysize * sizeof( aas_reachability_t ); + //nodes of the bsp tree + ( *aasworld ).numnodes = 0; + ( *aasworld ).nodes = (aas_node_t *) GetClearedMemory( max_aas.max_nodes * sizeof( aas_node_t ) ); + allocatedaasmem += max_aas.max_nodes * sizeof( aas_node_t ); + //cluster portals + ( *aasworld ).numportals = 0; + ( *aasworld ).portals = (aas_portal_t *) GetClearedMemory( max_aas.max_portals * sizeof( aas_portal_t ) ); + allocatedaasmem += max_aas.max_portals * sizeof( aas_portal_t ); + //cluster portal index + ( *aasworld ).portalindexsize = 0; + ( *aasworld ).portalindex = (aas_portalindex_t *) GetClearedMemory( max_aas.max_portalindexsize * sizeof( aas_portalindex_t ) ); + allocatedaasmem += max_aas.max_portalindexsize * sizeof( aas_portalindex_t ); + //cluster + ( *aasworld ).numclusters = 0; + ( *aasworld ).clusters = (aas_cluster_t *) GetClearedMemory( max_aas.max_clusters * sizeof( aas_cluster_t ) ); + allocatedaasmem += max_aas.max_clusters * sizeof( aas_cluster_t ); + // + Log_Print( "allocated " ); + PrintMemorySize( allocatedaasmem ); + Log_Print( " of AAS memory\n" ); + //reset the has stuff + aas_vertexchain = (int *) GetClearedMemory( max_aas.max_vertexes * sizeof( int ) ); + aas_planechain = (int *) GetClearedMemory( max_aas.max_planes * sizeof( int ) ); + aas_edgechain = (int *) GetClearedMemory( max_aas.max_edges * sizeof( int ) ); + // + for ( i = 0; i < max_aas.max_vertexes; i++ ) aas_vertexchain[i] = -1; + for ( i = 0; i < VERTEX_HASH_SIZE * VERTEX_HASH_SIZE; i++ ) aas_hashverts[i] = -1; + // + for ( i = 0; i < max_aas.max_planes; i++ ) aas_planechain[i] = -1; + for ( i = 0; i < PLANE_HASH_SIZE; i++ ) aas_hashplanes[i] = -1; + // + for ( i = 0; i < max_aas.max_edges; i++ ) aas_edgechain[i] = -1; + for ( i = 0; i < EDGE_HASH_SIZE; i++ ) aas_hashedges[i] = -1; +} //end of the function AAS_AllocMaxAAS +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_FreeMaxAAS( void ) { + //bounding boxes + if ( ( *aasworld ).bboxes ) { + FreeMemory( ( *aasworld ).bboxes ); + } + ( *aasworld ).bboxes = NULL; + ( *aasworld ).numbboxes = 0; + //vertexes + if ( ( *aasworld ).vertexes ) { + FreeMemory( ( *aasworld ).vertexes ); + } + ( *aasworld ).vertexes = NULL; + ( *aasworld ).numvertexes = 0; + //planes + if ( ( *aasworld ).planes ) { + FreeMemory( ( *aasworld ).planes ); + } + ( *aasworld ).planes = NULL; + ( *aasworld ).numplanes = 0; + //edges + if ( ( *aasworld ).edges ) { + FreeMemory( ( *aasworld ).edges ); + } + ( *aasworld ).edges = NULL; + ( *aasworld ).numedges = 0; + //edge index + if ( ( *aasworld ).edgeindex ) { + FreeMemory( ( *aasworld ).edgeindex ); + } + ( *aasworld ).edgeindex = NULL; + ( *aasworld ).edgeindexsize = 0; + //faces + if ( ( *aasworld ).faces ) { + FreeMemory( ( *aasworld ).faces ); + } + ( *aasworld ).faces = NULL; + ( *aasworld ).numfaces = 0; + //face index + if ( ( *aasworld ).faceindex ) { + FreeMemory( ( *aasworld ).faceindex ); + } + ( *aasworld ).faceindex = NULL; + ( *aasworld ).faceindexsize = 0; + //convex areas + if ( ( *aasworld ).areas ) { + FreeMemory( ( *aasworld ).areas ); + } + ( *aasworld ).areas = NULL; + ( *aasworld ).numareas = 0; + //convex area settings + if ( ( *aasworld ).areasettings ) { + FreeMemory( ( *aasworld ).areasettings ); + } + ( *aasworld ).areasettings = NULL; + ( *aasworld ).numareasettings = 0; + //reachablity list + if ( ( *aasworld ).reachability ) { + FreeMemory( ( *aasworld ).reachability ); + } + ( *aasworld ).reachability = NULL; + ( *aasworld ).reachabilitysize = 0; + //nodes of the bsp tree + if ( ( *aasworld ).nodes ) { + FreeMemory( ( *aasworld ).nodes ); + } + ( *aasworld ).nodes = NULL; + ( *aasworld ).numnodes = 0; + //cluster portals + if ( ( *aasworld ).portals ) { + FreeMemory( ( *aasworld ).portals ); + } + ( *aasworld ).portals = NULL; + ( *aasworld ).numportals = 0; + //cluster portal index + if ( ( *aasworld ).portalindex ) { + FreeMemory( ( *aasworld ).portalindex ); + } + ( *aasworld ).portalindex = NULL; + ( *aasworld ).portalindexsize = 0; + //clusters + if ( ( *aasworld ).clusters ) { + FreeMemory( ( *aasworld ).clusters ); + } + ( *aasworld ).clusters = NULL; + ( *aasworld ).numclusters = 0; + + Log_Print( "freed " ); + PrintMemorySize( allocatedaasmem ); + Log_Print( " of AAS memory\n" ); + allocatedaasmem = 0; + // + if ( aas_vertexchain ) { + FreeMemory( aas_vertexchain ); + } + aas_vertexchain = NULL; + if ( aas_planechain ) { + FreeMemory( aas_planechain ); + } + aas_planechain = NULL; + if ( aas_edgechain ) { + FreeMemory( aas_edgechain ); + } + aas_edgechain = NULL; +} //end of the function AAS_FreeMaxAAS +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +unsigned AAS_HashVec( vec3_t vec ) { + int x, y; + + x = ( MAX_MAP_BOUNDS + (int)( vec[0] + 0.5 ) ) >> VERTEX_HASH_SHIFT; + y = ( MAX_MAP_BOUNDS + (int)( vec[1] + 0.5 ) ) >> VERTEX_HASH_SHIFT; + + if ( x < 0 || x >= VERTEX_HASH_SIZE || y < 0 || y >= VERTEX_HASH_SIZE ) { + Log_Print( "WARNING! HashVec: point %f %f %f outside valid range\n", vec[0], vec[1], vec[2] ); + Log_Print( "This should never happen!\n" ); + return -1; + } //end if + + return y * VERTEX_HASH_SIZE + x; +} //end of the function AAS_HashVec +//=========================================================================== +// returns true if the vertex was found in the list +// stores the vertex number in *vnum +// stores a new vertex if not stored already +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_GetVertex( vec3_t v, int *vnum ) { + int i; +#ifndef VERTEX_HASHING + float diff; +#endif //VERTEX_HASHING + +#ifdef VERTEX_HASHING + int h, vn; + vec3_t vert; + + for ( i = 0; i < 3; i++ ) + { + if ( fabs( v[i] - Q_rint( v[i] ) ) < INTEGRAL_EPSILON ) { + vert[i] = Q_rint( v[i] ); + } else { + vert[i] = v[i]; + } + } //end for + + h = AAS_HashVec( vert ); + //if the vertex was outside the valid range + if ( h == -1 ) { + *vnum = -1; + return true; + } //end if + + for ( vn = aas_hashverts[h]; vn >= 0; vn = aas_vertexchain[vn] ) + { + if ( fabs( ( *aasworld ).vertexes[vn][0] - vert[0] ) < VEREX_EPSILON + && fabs( ( *aasworld ).vertexes[vn][1] - vert[1] ) < VEREX_EPSILON + && fabs( ( *aasworld ).vertexes[vn][2] - vert[2] ) < VEREX_EPSILON ) { + *vnum = vn; + return true; + } //end if + } //end for +#else //VERTEX_HASHING + //check if the vertex is already stored + //stupid linear search + for ( i = 0; i < ( *aasworld ).numvertexes; i++ ) + { + diff = vert[0] - ( *aasworld ).vertexes[i][0]; + if ( diff < VERTEX_EPSILON && diff > -VERTEX_EPSILON ) { + diff = vert[1] - ( *aasworld ).vertexes[i][1]; + if ( diff < VERTEX_EPSILON && diff > -VERTEX_EPSILON ) { + diff = vert[2] - ( *aasworld ).vertexes[i][2]; + if ( diff < VERTEX_EPSILON && diff > -VERTEX_EPSILON ) { + *vnum = i; + return true; + } //end if + } //end if + } //end if + } //end for +#endif //VERTEX_HASHING + + if ( ( *aasworld ).numvertexes >= max_aas.max_vertexes ) { + Error( "AAS_MAX_VERTEXES = %d", max_aas.max_vertexes ); + } //end if + VectorCopy( vert, ( *aasworld ).vertexes[( *aasworld ).numvertexes] ); + *vnum = ( *aasworld ).numvertexes; + +#ifdef VERTEX_HASHING + aas_vertexchain[( *aasworld ).numvertexes] = aas_hashverts[h]; + aas_hashverts[h] = ( *aasworld ).numvertexes; +#endif //VERTEX_HASHING + + ( *aasworld ).numvertexes++; + return false; +} //end of the function AAS_GetVertex +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +unsigned AAS_HashEdge( int v1, int v2 ) { + int vnum1, vnum2; + // + if ( v1 < v2 ) { + vnum1 = v1; + vnum2 = v2; + } //end if + else + { + vnum1 = v2; + vnum2 = v1; + } //end else + return ( vnum1 + vnum2 ) & ( EDGE_HASH_SIZE - 1 ); +} //end of the function AAS_HashVec +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_AddEdgeToHash( int edgenum ) { + int hash; + aas_edge_t *edge; + + edge = &( *aasworld ).edges[edgenum]; + + hash = AAS_HashEdge( edge->v[0], edge->v[1] ); + + aas_edgechain[edgenum] = aas_hashedges[hash]; + aas_hashedges[hash] = edgenum; +} //end of the function AAS_AddEdgeToHash +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_FindHashedEdge( int v1num, int v2num, int *edgenum ) { + int e, hash; + aas_edge_t *edge; + + hash = AAS_HashEdge( v1num, v2num ); + for ( e = aas_hashedges[hash]; e >= 0; e = aas_edgechain[e] ) + { + edge = &( *aasworld ).edges[e]; + if ( edge->v[0] == v1num ) { + if ( edge->v[1] == v2num ) { + *edgenum = e; + return true; + } //end if + } //end if + else if ( edge->v[1] == v1num ) { + if ( edge->v[0] == v2num ) { + //negative for a reversed edge + *edgenum = -e; + return true; + } //end if + } //end else + } //end for + return false; +} //end of the function AAS_FindHashedPlane +//=========================================================================== +// returns true if the edge was found +// stores the edge number in *edgenum (negative if reversed edge) +// stores new edge if not stored already +// returns zero when the edge is degenerate +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_GetEdge( vec3_t v1, vec3_t v2, int *edgenum ) { + int v1num, v2num; + qboolean found; + + //the first edge is a dummy + if ( ( *aasworld ).numedges == 0 ) { + ( *aasworld ).numedges = 1; + } + + found = AAS_GetVertex( v1, &v1num ); + found &= AAS_GetVertex( v2, &v2num ); + //if one of the vertexes was outside the valid range + if ( v1num == -1 || v2num == -1 ) { + *edgenum = 0; + return true; + } //end if + //if both vertexes are the same or snapped onto each other + if ( v1num == v2num ) { + *edgenum = 0; + return true; + } //end if + //if both vertexes where already stored + if ( found ) { +#ifdef EDGE_HASHING + if ( AAS_FindHashedEdge( v1num, v2num, edgenum ) ) { + return true; + } +#else + int i; + for ( i = 1; i < ( *aasworld ).numedges; i++ ) + { + if ( ( *aasworld ).edges[i].v[0] == v1num ) { + if ( ( *aasworld ).edges[i].v[1] == v2num ) { + *edgenum = i; + return true; + } //end if + } //end if + else if ( ( *aasworld ).edges[i].v[1] == v1num ) { + if ( ( *aasworld ).edges[i].v[0] == v2num ) { + //negative for a reversed edge + *edgenum = -i; + return true; + } //end if + } //end else + } //end for +#endif //EDGE_HASHING + } //end if + if ( ( *aasworld ).numedges >= max_aas.max_edges ) { + Error( "AAS_MAX_EDGES = %d", max_aas.max_edges ); + } //end if + ( *aasworld ).edges[( *aasworld ).numedges].v[0] = v1num; + ( *aasworld ).edges[( *aasworld ).numedges].v[1] = v2num; + *edgenum = ( *aasworld ).numedges; +#ifdef EDGE_HASHING + AAS_AddEdgeToHash( *edgenum ); +#endif //EDGE_HASHING + ( *aasworld ).numedges++; + return false; +} //end of the function AAS_GetEdge +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_PlaneTypeForNormal( vec3_t normal ) { + vec_t ax, ay, az; + + //NOTE: epsilon used + if ( ( normal[0] >= 1.0 - NORMAL_EPSILON ) || + ( normal[0] <= -1.0 + NORMAL_EPSILON ) ) { + return PLANE_X; + } + if ( ( normal[1] >= 1.0 - NORMAL_EPSILON ) || + ( normal[1] <= -1.0 + NORMAL_EPSILON ) ) { + return PLANE_Y; + } + if ( ( normal[2] >= 1.0 - NORMAL_EPSILON ) || + ( normal[2] <= -1.0 + NORMAL_EPSILON ) ) { + return PLANE_Z; + } + + ax = fabs( normal[0] ); + ay = fabs( normal[1] ); + az = fabs( normal[2] ); + + if ( ax >= ay && ax >= az ) { + return PLANE_ANYX; + } + if ( ay >= ax && ay >= az ) { + return PLANE_ANYY; + } + return PLANE_ANYZ; +} //end of the function AAS_PlaneTypeForNormal +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_AddPlaneToHash( int planenum ) { + int hash; + aas_plane_t *plane; + + plane = &( *aasworld ).planes[planenum]; + + hash = (int)fabs( plane->dist ) / 8; + hash &= ( PLANE_HASH_SIZE - 1 ); + + aas_planechain[planenum] = aas_hashplanes[hash]; + aas_hashplanes[hash] = planenum; +} //end of the function AAS_AddPlaneToHash +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_PlaneEqual( vec3_t normal, float dist, int planenum ) { + float diff; + + diff = dist - ( *aasworld ).planes[planenum].dist; + if ( diff > -DIST_EPSILON && diff < DIST_EPSILON ) { + diff = normal[0] - ( *aasworld ).planes[planenum].normal[0]; + if ( diff > -NORMAL_EPSILON && diff < NORMAL_EPSILON ) { + diff = normal[1] - ( *aasworld ).planes[planenum].normal[1]; + if ( diff > -NORMAL_EPSILON && diff < NORMAL_EPSILON ) { + diff = normal[2] - ( *aasworld ).planes[planenum].normal[2]; + if ( diff > -NORMAL_EPSILON && diff < NORMAL_EPSILON ) { + return true; + } //end if + } //end if + } //end if + } //end if + return false; +} //end of the function AAS_PlaneEqual +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_FindPlane( vec3_t normal, float dist, int *planenum ) { + int i; + + for ( i = 0; i < ( *aasworld ).numplanes; i++ ) + { + if ( AAS_PlaneEqual( normal, dist, i ) ) { + *planenum = i; + return true; + } //end if + } //end for + return false; +} //end of the function AAS_FindPlane +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_FindHashedPlane( vec3_t normal, float dist, int *planenum ) { + int i, p; + aas_plane_t *plane; + int hash, h; + + hash = (int)fabs( dist ) / 8; + hash &= ( PLANE_HASH_SIZE - 1 ); + + //search the border bins as well + for ( i = -1; i <= 1; i++ ) + { + h = ( hash + i ) & ( PLANE_HASH_SIZE - 1 ); + for ( p = aas_hashplanes[h]; p >= 0; p = aas_planechain[p] ) + { + plane = &( *aasworld ).planes[p]; + if ( AAS_PlaneEqual( normal, dist, p ) ) { + *planenum = p; + return true; + } //end if + } //end for + } //end for + return false; +} //end of the function AAS_FindHashedPlane +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_GetPlane( vec3_t normal, vec_t dist, int *planenum ) { + aas_plane_t *plane, temp; + + //if (AAS_FindPlane(normal, dist, planenum)) return true; + if ( AAS_FindHashedPlane( normal, dist, planenum ) ) { + return true; + } + + if ( ( *aasworld ).numplanes >= max_aas.max_planes - 1 ) { + Error( "AAS_MAX_PLANES = %d", max_aas.max_planes ); + } //end if + +#ifdef STOREPLANESDOUBLE + plane = &( *aasworld ).planes[( *aasworld ).numplanes]; + VectorCopy( normal, plane->normal ); + plane->dist = dist; + plane->type = ( plane + 1 )->type = PlaneTypeForNormal( plane->normal ); + + VectorCopy( normal, ( plane + 1 )->normal ); + VectorNegate( ( plane + 1 )->normal, ( plane + 1 )->normal ); + ( plane + 1 )->dist = -dist; + + ( *aasworld ).numplanes += 2; + + //allways put axial planes facing positive first + if ( plane->type < 3 ) { + if ( plane->normal[0] < 0 || plane->normal[1] < 0 || plane->normal[2] < 0 ) { + // flip order + temp = *plane; + *plane = *( plane + 1 ); + *( plane + 1 ) = temp; + *planenum = ( *aasworld ).numplanes - 1; + return false; + } //end if + } //end if + *planenum = ( *aasworld ).numplanes - 2; + //add the planes to the hash + AAS_AddPlaneToHash( ( *aasworld ).numplanes - 1 ); + AAS_AddPlaneToHash( ( *aasworld ).numplanes - 2 ); + return false; +#else + plane = &( *aasworld ).planes[( *aasworld ).numplanes]; + VectorCopy( normal, plane->normal ); + plane->dist = dist; + plane->type = AAS_PlaneTypeForNormal( normal ); + + *planenum = ( *aasworld ).numplanes; + ( *aasworld ).numplanes++; + //add the plane to the hash + AAS_AddPlaneToHash( ( *aasworld ).numplanes - 1 ); + return false; +#endif //STOREPLANESDOUBLE +} //end of the function AAS_GetPlane +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean AAS_GetFace( winding_t *w, plane_t *p, int side, int *facenum ) { + int edgenum, i, j; + aas_face_t *face; + + //face zero is a dummy, because of the face index with negative numbers + if ( ( *aasworld ).numfaces == 0 ) { + ( *aasworld ).numfaces = 1; + } + + if ( ( *aasworld ).numfaces >= max_aas.max_faces ) { + Error( "AAS_MAX_FACES = %d", max_aas.max_faces ); + } //end if + face = &( *aasworld ).faces[( *aasworld ).numfaces]; + AAS_GetPlane( p->normal, p->dist, &face->planenum ); + face->faceflags = 0; + face->firstedge = ( *aasworld ).edgeindexsize; + face->frontarea = 0; + face->backarea = 0; + face->numedges = 0; + for ( i = 0; i < w->numpoints; i++ ) + { + if ( ( *aasworld ).edgeindexsize >= max_aas.max_edgeindexsize ) { + Error( "AAS_MAX_EDGEINDEXSIZE = %d", max_aas.max_edgeindexsize ); + } //end if + j = ( i + 1 ) % w->numpoints; + AAS_GetEdge( w->p[i], w->p[j], &edgenum ); + //if the edge wasn't degenerate + if ( edgenum ) { + ( *aasworld ).edgeindex[( *aasworld ).edgeindexsize++] = edgenum; + face->numedges++; + } //end if + else if ( verbose ) { + Log_Write( "AAS_GetFace: face %d had degenerate edge %d-%d\r\n", + ( *aasworld ).numfaces, i, j ); + } //end else + } //end for + if ( face->numedges < 1 +#ifdef NOTHREEVERTEXFACES + || face->numedges < 3 +#endif //NOTHREEVERTEXFACES + ) { + memset( &( *aasworld ).faces[( *aasworld ).numfaces], 0, sizeof( aas_face_t ) ); + Log_Write( "AAS_GetFace: face %d was tiny\r\n", ( *aasworld ).numfaces ); + return false; + } //end if + *facenum = ( *aasworld ).numfaces; + ( *aasworld ).numfaces++; + return true; +} //end of the function AAS_GetFace +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +/* +qboolean AAS_GetFace(winding_t *w, plane_t *p, int side, int *facenum) +{ + aas_edgeindex_t edges[1024]; + int planenum, numedges, i; + int j, edgenum; + qboolean foundplane, foundedges; + aas_face_t *face; + + //face zero is a dummy, because of the face index with negative numbers + if ((*aasworld).numfaces == 0) (*aasworld).numfaces = 1; + + foundplane = AAS_GetPlane(p->normal, p->dist, &planenum); + + foundedges = true; + numedges = w->numpoints; + for (i = 0; i < w->numpoints; i++) + { + if (i >= 1024) Error("AAS_GetFace: more than %d edges\n", 1024); + foundedges &= AAS_GetEdge(w->p[i], w->p[(i+1 >= w->numpoints ? 0 : i+1)], &edges[i]); + } //end for + + //FIXME: use portal number instead of a search + //if the plane and all edges already existed + if (foundplane && foundedges) + { + for (i = 0; i < (*aasworld).numfaces; i++) + { + face = &(*aasworld).faces[i]; + if (planenum == face->planenum) + { + if (numedges == face->numedges) + { + for (j = 0; j < numedges; j++) + { + edgenum = abs((*aasworld).edgeindex[face->firstedge + j]); + if (abs(edges[i]) != edgenum) break; + } //end for + if (j == numedges) + { + //jippy found the face + *facenum = -i; + return true; + } //end if + } //end if + } //end if + } //end for + } //end if + if ((*aasworld).numfaces >= max_aas.max_faces) + { + Error("AAS_MAX_FACES = %d", max_aas.max_faces); + } //end if + face = &(*aasworld).faces[(*aasworld).numfaces]; + face->planenum = planenum; + face->faceflags = 0; + face->numedges = numedges; + face->firstedge = (*aasworld).edgeindexsize; + face->frontarea = 0; + face->backarea = 0; + for (i = 0; i < numedges; i++) + { + if ((*aasworld).edgeindexsize >= max_aas.max_edgeindexsize) + { + Error("AAS_MAX_EDGEINDEXSIZE = %d", max_aas.max_edgeindexsize); + } //end if + (*aasworld).edgeindex[(*aasworld).edgeindexsize++] = edges[i]; + } //end for + *facenum = (*aasworld).numfaces; + (*aasworld).numfaces++; + return false; +} //end of the function AAS_GetFace*/ +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_StoreAreaSettings( tmp_areasettings_t *tmpareasettings ) { + aas_areasettings_t *areasettings; + + if ( ( *aasworld ).numareasettings == 0 ) { + ( *aasworld ).numareasettings = 1; + } + areasettings = &( *aasworld ).areasettings[( *aasworld ).numareasettings++]; + areasettings->areaflags = tmpareasettings->areaflags; + areasettings->presencetype = tmpareasettings->presencetype; + areasettings->contents = tmpareasettings->contents; + areasettings->groundsteepness = tmpareasettings->groundsteepness; // Ridah + if ( tmpareasettings->modelnum > AREACONTENTS_MAXMODELNUM ) { + Log_Print( "WARNING: more than %d mover models\n", AREACONTENTS_MAXMODELNUM ); + } + areasettings->contents |= ( tmpareasettings->modelnum & AREACONTENTS_MAXMODELNUM ) << AREACONTENTS_MODELNUMSHIFT; +} //end of the function AAS_StoreAreaSettings +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_StoreArea( tmp_area_t *tmparea ) { + int side, edgenum, i; + plane_t *plane; + tmp_face_t *tmpface; + aas_area_t *aasarea; + aas_edge_t *edge; + aas_face_t *aasface; + aas_faceindex_t aasfacenum; + vec3_t facecenter; + winding_t *w = NULL; // TTimo: init + + //when the area is merged go to the merged area + //FIXME: this isn't necessary anymore because the tree + // is refreshed after area merging + while ( tmparea->mergedarea ) tmparea = tmparea->mergedarea; + // + if ( tmparea->invalid ) { + Error( "AAS_StoreArea: tried to store invalid area" ); + } + //if there is an aas area already stored for this tmp area + if ( tmparea->aasareanum ) { + return -tmparea->aasareanum; + } + // + if ( ( *aasworld ).numareas >= max_aas.max_areas ) { + Error( "AAS_MAX_AREAS = %d", max_aas.max_areas ); + } //end if + //area zero is a dummy + if ( ( *aasworld ).numareas == 0 ) { + ( *aasworld ).numareas = 1; + } + //create an area from this leaf + aasarea = &( *aasworld ).areas[( *aasworld ).numareas]; + aasarea->areanum = ( *aasworld ).numareas; + aasarea->numfaces = 0; + aasarea->firstface = ( *aasworld ).faceindexsize; + ClearBounds( aasarea->mins, aasarea->maxs ); + VectorClear( aasarea->center ); + // +// Log_Write("tmparea %d became aasarea %d\r\n", tmparea->areanum, aasarea->areanum); + //store the aas area number at the tmp area + tmparea->aasareanum = aasarea->areanum; + // + for ( tmpface = tmparea->tmpfaces; tmpface; tmpface = tmpface->next[side] ) + { + side = tmpface->frontarea != tmparea; + //if there's an aas face created for the tmp face already + if ( tmpface->aasfacenum ) { + //we're at the back of the face so use a negative index + aasfacenum = -tmpface->aasfacenum; +#ifdef DEBUG + if ( tmpface->aasfacenum < 0 || tmpface->aasfacenum > max_aas.max_faces ) { + Error( "AAS_CreateTree_r: face number out of range" ); + } //end if +#endif //DEBUG + aasface = &( *aasworld ).faces[tmpface->aasfacenum]; + aasface->backarea = aasarea->areanum; + } //end if + else + { + plane = &mapplanes[tmpface->planenum ^ side]; + if ( side ) { + w = tmpface->winding; + tmpface->winding = ReverseWinding( tmpface->winding ); + } //end if + if ( !AAS_GetFace( tmpface->winding, plane, 0, &aasfacenum ) ) { + continue; + } + if ( side ) { + FreeWinding( tmpface->winding ); + tmpface->winding = w; + } //end if + aasface = &( *aasworld ).faces[aasfacenum]; + aasface->frontarea = aasarea->areanum; + aasface->backarea = 0; + aasface->faceflags = tmpface->faceflags; + //set the face number at the tmp face + tmpface->aasfacenum = aasfacenum; + } //end else + //add face points to the area bounds and + //calculate the face 'center' + VectorClear( facecenter ); + for ( edgenum = 0; edgenum < aasface->numedges; edgenum++ ) + { + edge = &( *aasworld ).edges[abs( ( *aasworld ).edgeindex[aasface->firstedge + edgenum] )]; + for ( i = 0; i < 2; i++ ) + { + AddPointToBounds( ( *aasworld ).vertexes[edge->v[i]], aasarea->mins, aasarea->maxs ); + VectorAdd( ( *aasworld ).vertexes[edge->v[i]], facecenter, facecenter ); + } //end for + } //end for + VectorScale( facecenter, 1.0 / ( aasface->numedges * 2.0 ), facecenter ); + //add the face 'center' to the area 'center' + VectorAdd( aasarea->center, facecenter, aasarea->center ); + // + if ( ( *aasworld ).faceindexsize >= max_aas.max_faceindexsize ) { + Error( "AAS_MAX_FACEINDEXSIZE = %d", max_aas.max_faceindexsize ); + } //end if + ( *aasworld ).faceindex[( *aasworld ).faceindexsize++] = aasfacenum; + aasarea->numfaces++; + } //end for + //if the area has no faces at all (return 0, = solid leaf) + if ( !aasarea->numfaces ) { + return 0; + } + // + VectorScale( aasarea->center, 1.0 / aasarea->numfaces, aasarea->center ); + //Log_Write("area %d center %f %f %f\r\n", (*aasworld).numareas, + // aasarea->center[0], aasarea->center[1], aasarea->center[2]); + //store the area settings + AAS_StoreAreaSettings( tmparea->settings ); + // + //Log_Write("tmp area %d became aas area %d\r\n", tmpareanum, aasarea->areanum); + qprintf( "\r%6d", aasarea->areanum ); + // + if ( ( *aasworld ).areasettings[aasarea->areanum].contents & AREACONTENTS_CLUSTERPORTAL ) { + static int num; + Log_Write( "***** area %d is a cluster portal %d\n", aasarea->areanum, num++ ); + } //end if + // + ( *aasworld ).numareas++; + return -( ( *aasworld ).numareas - 1 ); +} //end of the function AAS_StoreArea +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int AAS_StoreTree_r( tmp_node_t *tmpnode ) { + int aasnodenum; + plane_t *plane; + aas_node_t *aasnode; + + //if it is a solid leaf + if ( !tmpnode ) { + return 0; + } + //negative so it's an area + if ( tmpnode->tmparea ) { + return AAS_StoreArea( tmpnode->tmparea ); + } + //it's another node + //the first node is a dummy + if ( ( *aasworld ).numnodes == 0 ) { + ( *aasworld ).numnodes = 1; + } + if ( ( *aasworld ).numnodes >= max_aas.max_nodes ) { + Error( "AAS_MAX_NODES = %d", max_aas.max_nodes ); + } //end if + aasnodenum = ( *aasworld ).numnodes; + aasnode = &( *aasworld ).nodes[( *aasworld ).numnodes++]; + plane = &mapplanes[tmpnode->planenum]; + AAS_GetPlane( plane->normal, plane->dist, &aasnode->planenum ); + aasnode->children[0] = AAS_StoreTree_r( tmpnode->children[0] ); + aasnode->children[1] = AAS_StoreTree_r( tmpnode->children[1] ); + return aasnodenum; +} //end of the function AAS_StoreTree_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_StoreBoundingBoxes( void ) { + if ( cfg.numbboxes > max_aas.max_bboxes ) { + Error( "more than %d bounding boxes", max_aas.max_bboxes ); + } //end if + ( *aasworld ).numbboxes = cfg.numbboxes; + memcpy( ( *aasworld ).bboxes, cfg.bboxes, cfg.numbboxes * sizeof( aas_bbox_t ) ); +} //end of the function AAS_StoreBoundingBoxes +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_StoreFile( char *filename ) { + AAS_AllocMaxAAS(); + + Log_Write( "AAS_StoreFile\r\n" ); + // + AAS_StoreBoundingBoxes(); + // + qprintf( "%6d areas stored", 0 ); + //start with node 1 because node zero is a dummy + AAS_StoreTree_r( tmpaasworld.nodes ); + qprintf( "\n" ); + Log_Write( "%6d areas stored\r\n", ( *aasworld ).numareas ); + ( *aasworld ).loaded = true; +} //end of the function AAS_StoreFile diff --git a/src/bspc/aas_store.h b/src/bspc/aas_store.h new file mode 100644 index 0000000..5e0cae4 --- /dev/null +++ b/src/bspc/aas_store.h @@ -0,0 +1,126 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: aas_store.h +// Function: +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + +#define AAS_MAX_BBOXES 5 +#define AAS_MAX_VERTEXES 512000 +#define AAS_MAX_PLANES 65536 +#define AAS_MAX_EDGES 512000 +#define AAS_MAX_EDGEINDEXSIZE 512000 +#define AAS_MAX_FACES 512000 +#define AAS_MAX_FACEINDEXSIZE 512000 +#define AAS_MAX_AREAS 65536 +#define AAS_MAX_AREASETTINGS 65536 +#define AAS_MAX_REACHABILITYSIZE 65536 +#define AAS_MAX_NODES 256000 +#define AAS_MAX_PORTALS 65536 +#define AAS_MAX_PORTALINDEXSIZE 65536 +#define AAS_MAX_CLUSTERS 65536 + +#define BSPCINCLUDE +#include "../botlib/be_aas.h" +#include "../botlib/be_aas_def.h" + +/* +typedef struct bspc_aas_s +{ + int loaded; + int initialized; //true when AAS has been initialized + int savefile; //set true when file should be saved + //bounding boxes + int numbboxes; + aas_bbox_t *bboxes; + //vertexes + int numvertexes; + aas_vertex_t *vertexes; + //planes + int numplanes; + aas_plane_t *planes; + //edges + int numedges; + aas_edge_t *edges; + //edge index + int edgeindexsize; + aas_edgeindex_t *edgeindex; + //faces + int numfaces; + aas_face_t *faces; + //face index + int faceindexsize; + aas_faceindex_t *faceindex; + //convex areas + int numareas; + aas_area_t *areas; + //convex area settings + int numareasettings; + aas_areasettings_t *areasettings; + //reachablity list + int reachabilitysize; + aas_reachability_t *reachability; + //nodes of the bsp tree + int numnodes; + aas_node_t *nodes; + //cluster portals + int numportals; + aas_portal_t *portals; + //cluster portal index + int portalindexsize; + aas_portalindex_t *portalindex; + //clusters + int numclusters; + aas_cluster_t *clusters; + // + int numreachabilityareas; + float reachabilitytime; +} bspc_aas_t; + +extern bspc_aas_t aasworld; +// +*/ + +// Ridah +extern aas_t aasworlds[1]; +extern aas_t *aasworld; +// done. + +//stores the AAS file from the temporary AAS +void AAS_StoreFile( char *filename ); +//returns a number of the given plane +qboolean AAS_FindPlane( vec3_t normal, float dist, int *planenum ); +//allocates the maximum AAS memory for storage +void AAS_AllocMaxAAS( void ); +//frees the maximum AAS memory for storage +void AAS_FreeMaxAAS( void ); diff --git a/src/bspc/be_aas_bspc.c b/src/bspc/be_aas_bspc.c new file mode 100644 index 0000000..01567f5 --- /dev/null +++ b/src/bspc/be_aas_bspc.c @@ -0,0 +1,314 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: b_aas_bspc.c +// Function: Area Awareness System +// Programmer: Mr Elusive (MrElusive@idsoftware.com) +// Last update: 1999-09-14 +// Tab Size: 3 +//=========================================================================== + +#include "../game/q_shared.h" +#include "../bspc/l_log.h" +#include "../bspc/l_qfiles.h" +#include "../botlib/l_memory.h" +#include "../botlib/l_script.h" +#include "../botlib/l_precomp.h" +#include "../botlib/l_struct.h" +#include "../botlib/aasfile.h" +#include "../botlib/botlib.h" +#include "../botlib/be_aas.h" +#include "../botlib/be_aas_def.h" +#include "../qcommon/cm_public.h" + +//#define BSPC + +extern botlib_import_t botimport; +extern qboolean capsule_collision; + +//#define AAS_MOVE_DEBUG + +botlib_import_t botimport; +clipHandle_t worldmodel; + +void Error( char *error, ... ); + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_Error( char *fmt, ... ) { + va_list argptr; + char text[1024]; + + va_start( argptr, fmt ); + vsprintf( text, fmt, argptr ); + va_end( argptr ); + + Error( text ); +} //end of the function AAS_Error +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int Sys_MilliSeconds( void ) { + return clock() * 1000 / CLOCKS_PER_SEC; +} //end of the function Sys_MilliSeconds +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_DebugLine( vec3_t start, vec3_t end, int color ) { +} //end of the function AAS_DebugLine +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ClearShownDebugLines( void ) { +} //end of the function AAS_ClearShownDebugLines +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +char *BotImport_BSPEntityData( void ) { + return CM_EntityString(); +} //end of the function AAS_GetEntityData +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotImport_Trace( bsp_trace_t *bsptrace, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int passent, int contentmask ) { + trace_t result; + + CM_BoxTrace( &result, start, end, mins, maxs, worldmodel, contentmask, capsule_collision ); + + bsptrace->allsolid = result.allsolid; + bsptrace->contents = result.contents; + VectorCopy( result.endpos, bsptrace->endpos ); + bsptrace->ent = result.entityNum; + bsptrace->fraction = result.fraction; + bsptrace->exp_dist = 0; + bsptrace->plane.dist = result.plane.dist; + VectorCopy( result.plane.normal, bsptrace->plane.normal ); + bsptrace->plane.signbits = result.plane.signbits; + bsptrace->plane.type = result.plane.type; + bsptrace->sidenum = 0; + bsptrace->startsolid = result.startsolid; + bsptrace->surface.flags = result.surfaceFlags; +} //end of the function BotImport_Trace +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BotImport_PointContents( vec3_t p ) { + return CM_PointContents( p, worldmodel ); +} //end of the function BotImport_PointContents +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void *BotImport_GetMemory( int size ) { + return GetMemory( size ); +} //end of the function BotImport_GetMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotImport_Print( int type, char *fmt, ... ) { + va_list argptr; + char buf[1024]; + + va_start( argptr, fmt ); + vsprintf( buf, fmt, argptr ); + printf( buf ); + if ( buf[0] != '\r' ) { + Log_Write( buf ); + } + va_end( argptr ); +} //end of the function BotImport_Print +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BotImport_BSPModelMinsMaxsOrigin( int modelnum, vec3_t angles, vec3_t outmins, vec3_t outmaxs, vec3_t origin ) { + clipHandle_t h; + vec3_t mins, maxs; + float max; + int i; + + h = CM_InlineModel( modelnum ); + CM_ModelBounds( h, mins, maxs ); + //if the model is rotated + if ( ( angles[0] || angles[1] || angles[2] ) ) { // expand for rotation + + max = RadiusFromBounds( mins, maxs ); + for ( i = 0; i < 3; i++ ) + { + mins[i] = ( mins[i] + maxs[i] ) * 0.5 - max; + maxs[i] = ( mins[i] + maxs[i] ) * 0.5 + max; + } //end for + } //end if + if ( outmins ) { + VectorCopy( mins, outmins ); + } + if ( outmaxs ) { + VectorCopy( maxs, outmaxs ); + } + if ( origin ) { + VectorClear( origin ); + } +} //end of the function BotImport_BSPModelMinsMaxsOrigin +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Com_DPrintf( char *fmt, ... ) { + va_list argptr; + char buf[1024]; + + va_start( argptr, fmt ); + vsprintf( buf, fmt, argptr ); + printf( buf ); + if ( buf[0] != '\r' ) { + Log_Write( buf ); + } + va_end( argptr ); +} //end of the function Com_DPrintf +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int COM_Compress( char *data_p ) { + return strlen( data_p ); +} +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Com_Memset( void* dest, const int val, const size_t count ) { + memset( dest, val, count ); +} +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Com_Memcpy( void* dest, const void* src, const size_t count ) { + memcpy( dest, src, count ); +} +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_InitBotImport( void ) { + botimport.BSPEntityData = BotImport_BSPEntityData; + botimport.GetMemory = BotImport_GetMemory; + botimport.FreeMemory = FreeMemory; + botimport.Trace = BotImport_Trace; + botimport.PointContents = BotImport_PointContents; + botimport.Print = BotImport_Print; + botimport.BSPModelMinsMaxsOrigin = BotImport_BSPModelMinsMaxsOrigin; +} //end of the function AAS_InitBotImport +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== + +void AAS_CalcReachAndClusters( struct quakefile_s *qf ) { + float time; + + Log_Print( "loading collision map...\n" ); + // + if ( !qf->pakfile[0] ) { + strcpy( qf->pakfile, qf->filename ); + } + //load the map + CM_LoadMap( (char *) qf, qfalse, &( *aasworld ).bspchecksum ); + //get a handle to the world model + worldmodel = CM_InlineModel( 0 ); // 0 = world, 1 + are bmodels + //initialize bot import structure + AAS_InitBotImport(); + //load the BSP entity string + AAS_LoadBSPFile(); + //init physics settings + AAS_InitSettings(); + //initialize AAS link heap + AAS_InitAASLinkHeap(); + //initialize the AAS linked entities for the new map + AAS_InitAASLinkedEntities(); + //reset all reachabilities and clusters + ( *aasworld ).reachabilitysize = 0; + ( *aasworld ).numclusters = 0; + //set all view portals as cluster portals in case we re-calculate the reachabilities and clusters (with -reach) + AAS_SetViewPortalsAsClusterPortals(); + //calculate reachabilities + AAS_InitReachability(); + time = 0; + while ( AAS_ContinueInitReachability( time ) ) time++; + //calculate clusters + AAS_InitClustering(); +} //end of the function AAS_CalcReachAndClusters + +// Ridah +void AAS_SetWorldPointer( aas_t *newaasworld ) { + aasworld = newaasworld; +} +// done. diff --git a/src/bspc/be_aas_bspc.h b/src/bspc/be_aas_bspc.h new file mode 100644 index 0000000..a950474 --- /dev/null +++ b/src/bspc/be_aas_bspc.h @@ -0,0 +1,44 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: b_aas_bspc.h +// Function: Area Awareness System +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1999-02-28 +// Tab Size: 3 +//=========================================================================== + +void AAS_CalcReachAndClusters( struct quakefile_s *qf ); + +// Ridah +void AAS_SetWorldPointer( aas_t *newaasworld ); +// done. + + diff --git a/src/bspc/brushbsp.c b/src/bspc/brushbsp.c new file mode 100644 index 0000000..b88bbe5 --- /dev/null +++ b/src/bspc/brushbsp.c @@ -0,0 +1,1924 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: BrushBSP +// Function: Build a BSP tree from a set of brushes +// Programmer: id Software & Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + +#include "qbsp.h" +#include "l_mem.h" +#include "../botlib/aasfile.h" +#include "aas_store.h" +#include "aas_cfg.h" + +#include + +/* +each side has a count of the other sides it splits + +the best split will be the one that minimizes the total split counts +of all remaining sides + +precalc side on plane table + +evaluate split side +{ +cost = 0 +for all sides + for all sides + get + if side splits side and splitside is on same child + cost++; +} +*/ + +int c_nodes; +int c_nonvis; +int c_active_brushes; +int c_solidleafnodes; +int c_totalsides; +int c_brushmemory; +int c_peak_brushmemory; +int c_nodememory; +int c_peak_totalbspmemory; + +// if a brush just barely pokes onto the other side, +// let it slide by without chopping +#define PLANESIDE_EPSILON 0.001 +//0.1 + +//#ifdef DEBUG +typedef struct cname_s +{ + int value; + char *name; +} cname_t; + +cname_t contentnames[] = +{ + {CONTENTS_SOLID,"CONTENTS_SOLID"}, + {CONTENTS_WINDOW,"CONTENTS_WINDOW"}, + {CONTENTS_AUX,"CONTENTS_AUX"}, + {CONTENTS_LAVA,"CONTENTS_LAVA"}, + {CONTENTS_SLIME,"CONTENTS_SLIME"}, + {CONTENTS_WATER,"CONTENTS_WATER"}, + {CONTENTS_MIST,"CONTENTS_MIST"}, + {LAST_VISIBLE_CONTENTS,"LAST_VISIBLE_CONTENTS"}, + + {CONTENTS_AREAPORTAL,"CONTENTS_AREAPORTAL"}, + {CONTENTS_PLAYERCLIP,"CONTENTS_PLAYERCLIP"}, + {CONTENTS_MONSTERCLIP,"CONTENTS_MONSTERCLIP"}, + {CONTENTS_CURRENT_0,"CONTENTS_CURRENT_0"}, + {CONTENTS_CURRENT_90,"CONTENTS_CURRENT_90"}, + {CONTENTS_CURRENT_180,"CONTENTS_CURRENT_180"}, + {CONTENTS_CURRENT_270,"CONTENTS_CURRENT_270"}, + {CONTENTS_CURRENT_UP,"CONTENTS_CURRENT_UP"}, + {CONTENTS_CURRENT_DOWN,"CONTENTS_CURRENT_DOWN"}, + {CONTENTS_ORIGIN,"CONTENTS_ORIGIN"}, + {CONTENTS_MONSTER,"CONTENTS_MONSTER"}, + {CONTENTS_DEADMONSTER,"CONTENTS_DEADMONSTER"}, + {CONTENTS_DETAIL,"CONTENTS_DETAIL"}, + {CONTENTS_Q2TRANSLUCENT,"CONTENTS_TRANSLUCENT"}, + {CONTENTS_LADDER,"CONTENTS_LADDER"}, + {0, 0} +}; + +void PrintContents( int contents ) { + int i; + + for ( i = 0; contentnames[i].value; i++ ) + { + if ( contents & contentnames[i].value ) { + Log_Write( "%s,", contentnames[i].name ); + } //end if + } //end for +} //end of the function PrintContents + +//#endif DEBUG + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ResetBrushBSP( void ) { + c_nodes = 0; + c_nonvis = 0; + c_active_brushes = 0; + c_solidleafnodes = 0; + c_totalsides = 0; + c_brushmemory = 0; + c_peak_brushmemory = 0; + c_nodememory = 0; + c_peak_totalbspmemory = 0; +} //end of the function ResetBrushBSP +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FindBrushInTree( node_t *node, int brushnum ) { + bspbrush_t *b; + + if ( node->planenum == PLANENUM_LEAF ) { + for ( b = node->brushlist ; b ; b = b->next ) + if ( b->original->brushnum == brushnum ) { + Log_Print( "here\n" ); + } + return; + } + FindBrushInTree( node->children[0], brushnum ); + FindBrushInTree( node->children[1], brushnum ); +} //end of the function FindBrushInTree +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void DrawBrushList( bspbrush_t *brush, node_t *node ) { + int i; + side_t *s; + + GLS_BeginScene(); + for ( ; brush ; brush = brush->next ) + { + for ( i = 0 ; i < brush->numsides ; i++ ) + { + s = &brush->sides[i]; + if ( !s->winding ) { + continue; + } + if ( s->texinfo == TEXINFO_NODE ) { + GLS_Winding( s->winding, 1 ); + } else if ( !( s->flags & SFL_VISIBLE ) ) { + GLS_Winding( s->winding, 2 ); + } else { + GLS_Winding( s->winding, 0 ); + } + } + } + GLS_EndScene(); +} //end of the function DrawBrushList +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void WriteBrushList( char *name, bspbrush_t *brush, qboolean onlyvis ) { + int i; + side_t *s; + FILE *f; + + qprintf( "writing %s\n", name ); + f = SafeOpenWrite( name ); + + for ( ; brush ; brush = brush->next ) + { + for ( i = 0 ; i < brush->numsides ; i++ ) + { + s = &brush->sides[i]; + if ( !s->winding ) { + continue; + } + if ( onlyvis && !( s->flags & SFL_VISIBLE ) ) { + continue; + } + OutputWinding( brush->sides[i].winding, f ); + } + } + + fclose( f ); +} //end of the function WriteBrushList +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void PrintBrush( bspbrush_t *brush ) { + int i; + + printf( "brush: %p\n", brush ); + for ( i = 0; i < brush->numsides ; i++ ) + { + pw( brush->sides[i].winding ); + printf( "\n" ); + } //end for +} //end of the function PrintBrush +//=========================================================================== +// Sets the mins/maxs based on the windings +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BoundBrush( bspbrush_t *brush ) { + int i, j; + winding_t *w; + + ClearBounds( brush->mins, brush->maxs ); + for ( i = 0 ; i < brush->numsides ; i++ ) + { + w = brush->sides[i].winding; + if ( !w ) { + continue; + } + for ( j = 0 ; j < w->numpoints ; j++ ) + AddPointToBounds( w->p[j], brush->mins, brush->maxs ); + } +} //end of the function BoundBrush +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void CreateBrushWindings( bspbrush_t *brush ) { + int i, j; + winding_t *w; + side_t *side; + plane_t *plane; + + for ( i = 0 ; i < brush->numsides ; i++ ) + { + side = &brush->sides[i]; + plane = &mapplanes[side->planenum]; + w = BaseWindingForPlane( plane->normal, plane->dist ); + for ( j = 0 ; j < brush->numsides && w; j++ ) + { + if ( i == j ) { + continue; + } + if ( brush->sides[j].flags & SFL_BEVEL ) { + continue; + } + plane = &mapplanes[brush->sides[j].planenum ^ 1]; + ChopWindingInPlace( &w, plane->normal, plane->dist, 0 ); //CLIP_EPSILON); + } + + side->winding = w; + } + + BoundBrush( brush ); +} //end of the function CreateBrushWindings +//=========================================================================== +// Creates a new axial brush +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *BrushFromBounds( vec3_t mins, vec3_t maxs ) { + bspbrush_t *b; + int i; + vec3_t normal; + vec_t dist; + + b = AllocBrush( 6 ); + b->numsides = 6; + for ( i = 0 ; i < 3 ; i++ ) + { + VectorClear( normal ); + normal[i] = 1; + dist = maxs[i]; + b->sides[i].planenum = FindFloatPlane( normal, dist ); + + normal[i] = -1; + dist = -mins[i]; + b->sides[3 + i].planenum = FindFloatPlane( normal, dist ); + } + + CreateBrushWindings( b ); + + return b; +} //end of the function BrushFromBounds +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BrushOutOfBounds( bspbrush_t *brush, vec3_t mins, vec3_t maxs, float epsilon ) { + int i, j, n; + winding_t *w; + side_t *side; + + for ( i = 0; i < brush->numsides; i++ ) + { + side = &brush->sides[i]; + w = side->winding; + for ( j = 0; j < w->numpoints; j++ ) + { + for ( n = 0; n < 3; n++ ) + { + if ( w->p[j][n] < ( mins[n] + epsilon ) || w->p[j][n] > ( maxs[n] - epsilon ) ) { + return true; + } + } //end for + } //end for + } //end for + return false; +} //end of the function BrushOutOfBounds +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +vec_t BrushVolume( bspbrush_t *brush ) { + int i; + winding_t *w; + vec3_t corner; + vec_t d, area, volume; + plane_t *plane; + + if ( !brush ) { + return 0; + } + + // grab the first valid point as the corner + w = NULL; + for ( i = 0; i < brush->numsides; i++ ) + { + w = brush->sides[i].winding; + if ( w ) { + break; + } + } //end for + if ( !w ) { + return 0; + } + VectorCopy( w->p[0], corner ); + + // make tetrahedrons to all other faces + volume = 0; + for ( ; i < brush->numsides; i++ ) + { + w = brush->sides[i].winding; + if ( !w ) { + continue; + } + plane = &mapplanes[brush->sides[i].planenum]; + d = -( DotProduct( corner, plane->normal ) - plane->dist ); + area = WindingArea( w ); + volume += d * area; + } //end for + + volume /= 3; + return volume; +} //end of the function BrushVolume +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int CountBrushList( bspbrush_t *brushes ) { + int c; + + c = 0; + for ( ; brushes; brushes = brushes->next ) c++; + return c; +} //end of the function CountBrushList +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +node_t *AllocNode( void ) { + node_t *node; + + node = GetMemory( sizeof( *node ) ); + memset( node, 0, sizeof( *node ) ); + if ( numthreads == 1 ) { + c_nodememory += MemorySize( node ); + } //end if + return node; +} //end of the function AllocNode +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *AllocBrush( int numsides ) { + bspbrush_t *bb; + int c; + + c = (int)&( ( (bspbrush_t *)0 )->sides[numsides] ); + bb = GetMemory( c ); + memset( bb, 0, c ); + if ( numthreads == 1 ) { + c_active_brushes++; + c_brushmemory += MemorySize( bb ); + if ( c_brushmemory > c_peak_brushmemory ) { + c_peak_brushmemory = c_brushmemory; + } + } //end if + return bb; +} //end of the function AllocBrush +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FreeBrush( bspbrush_t *brushes ) { + int i; + + for ( i = 0 ; i < brushes->numsides ; i++ ) + if ( brushes->sides[i].winding ) { + FreeWinding( brushes->sides[i].winding ); + } + if ( numthreads == 1 ) { + c_active_brushes--; + c_brushmemory -= MemorySize( brushes ); + if ( c_brushmemory < 0 ) { + c_brushmemory = 0; + } + } //end if + FreeMemory( brushes ); +} //end of the function FreeBrush +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FreeBrushList( bspbrush_t *brushes ) { + bspbrush_t *next; + + for ( ; brushes; brushes = next ) + { + next = brushes->next; + + FreeBrush( brushes ); + } //end for +} //end of the function FreeBrushList +//=========================================================================== +// Duplicates the brush, the sides, and the windings +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *CopyBrush( bspbrush_t *brush ) { + bspbrush_t *newbrush; + int size; + int i; + + size = (int)&( ( (bspbrush_t *)0 )->sides[brush->numsides] ); + + newbrush = AllocBrush( brush->numsides ); + memcpy( newbrush, brush, size ); + + for ( i = 0 ; i < brush->numsides ; i++ ) + { + if ( brush->sides[i].winding ) { + newbrush->sides[i].winding = CopyWinding( brush->sides[i].winding ); + } + } + + return newbrush; +} //end of the function CopyBrush +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +node_t *PointInLeaf( node_t *node, vec3_t point ) { + vec_t d; + plane_t *plane; + + while ( node->planenum != PLANENUM_LEAF ) + { + plane = &mapplanes[node->planenum]; + d = DotProduct( point, plane->normal ) - plane->dist; + if ( d > 0 ) { + node = node->children[0]; + } else { + node = node->children[1]; + } + } + + return node; +} //end of the function PointInLeaf +//=========================================================================== +// Returns PSIDE_FRONT, PSIDE_BACK, or PSIDE_BOTH +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#if 0 +int BoxOnPlaneSide( vec3_t mins, vec3_t maxs, plane_t *plane ) { + int side; + int i; + vec3_t corners[2]; + vec_t dist1, dist2; + + // axial planes are easy + if ( plane->type < 3 ) { + side = 0; + if ( maxs[plane->type] > plane->dist + PLANESIDE_EPSILON ) { + side |= PSIDE_FRONT; + } + if ( mins[plane->type] < plane->dist - PLANESIDE_EPSILON ) { + side |= PSIDE_BACK; + } + return side; + } + + // create the proper leading and trailing verts for the box + + for ( i = 0 ; i < 3 ; i++ ) + { + if ( plane->normal[i] < 0 ) { + corners[0][i] = mins[i]; + corners[1][i] = maxs[i]; + } else + { + corners[1][i] = mins[i]; + corners[0][i] = maxs[i]; + } + } + + dist1 = DotProduct( plane->normal, corners[0] ) - plane->dist; + dist2 = DotProduct( plane->normal, corners[1] ) - plane->dist; + side = 0; + if ( dist1 >= PLANESIDE_EPSILON ) { + side = PSIDE_FRONT; + } + if ( dist2 < PLANESIDE_EPSILON ) { + side |= PSIDE_BACK; + } + + return side; +} +#else +int BoxOnPlaneSide( vec3_t emins, vec3_t emaxs, plane_t *p ) { + float dist1, dist2; + int sides; + + // axial planes are easy + if ( p->type < 3 ) { + sides = 0; + if ( emaxs[p->type] > p->dist + PLANESIDE_EPSILON ) { + sides |= PSIDE_FRONT; + } + if ( emins[p->type] < p->dist - PLANESIDE_EPSILON ) { + sides |= PSIDE_BACK; + } + return sides; + } //end if + +// general case + switch ( p->signbits ) + { + case 0: + dist1 = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]; + dist2 = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]; + break; + case 1: + dist1 = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]; + dist2 = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]; + break; + case 2: + dist1 = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]; + dist2 = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]; + break; + case 3: + dist1 = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]; + dist2 = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]; + break; + case 4: + dist1 = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]; + dist2 = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]; + break; + case 5: + dist1 = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]; + dist2 = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]; + break; + case 6: + dist1 = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]; + dist2 = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]; + break; + case 7: + dist1 = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]; + dist2 = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]; + break; + default: + dist1 = dist2 = 0; // shut up compiler +// assert( 0 ); + break; + } + + sides = 0; + if ( dist1 - p->dist >= PLANESIDE_EPSILON ) { + sides = PSIDE_FRONT; + } + if ( dist2 - p->dist < PLANESIDE_EPSILON ) { + sides |= PSIDE_BACK; + } + +// assert(sides != 0); + + return sides; +} +#endif +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int QuickTestBrushToPlanenum( bspbrush_t *brush, int planenum, int *numsplits ) { + int i, num; + plane_t *plane; + int s; + + *numsplits = 0; + + plane = &mapplanes[planenum]; + +#ifdef ME + //fast axial cases + if ( plane->type < 3 ) { + if ( plane->dist + PLANESIDE_EPSILON < brush->mins[plane->type] ) { + return PSIDE_FRONT; + } + if ( plane->dist - PLANESIDE_EPSILON > brush->maxs[plane->type] ) { + return PSIDE_BACK; + } + } //end if +#endif //ME*/ + + // if the brush actually uses the planenum, + // we can tell the side for sure + for ( i = 0; i < brush->numsides; i++ ) + { + num = brush->sides[i].planenum; + if ( num >= MAX_MAPFILE_PLANES ) { + Error( "bad planenum" ); + } + if ( num == planenum ) { + return PSIDE_BACK | PSIDE_FACING; + } + if ( num == ( planenum ^ 1 ) ) { + return PSIDE_FRONT | PSIDE_FACING; + } + + } + + // box on plane side + s = BoxOnPlaneSide( brush->mins, brush->maxs, plane ); + + // if both sides, count the visible faces split + if ( s == PSIDE_BOTH ) { + *numsplits += 3; + } + + return s; +} //end of the function QuickTestBrushToPlanenum +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int TestBrushToPlanenum( bspbrush_t *brush, int planenum, + int *numsplits, qboolean *hintsplit, int *epsilonbrush ) { + int i, j, num; + plane_t *plane; + int s = 0; + winding_t *w; + vec_t d, d_front, d_back; + int front, back; + int type; + float dist; + + *numsplits = 0; + *hintsplit = false; + + plane = &mapplanes[planenum]; + +#ifdef ME +// fast axial cases + type = plane->type; + if ( type < 3 ) { + dist = plane->dist; + if ( dist + PLANESIDE_EPSILON < brush->mins[type] ) { + return PSIDE_FRONT; + } + if ( dist - PLANESIDE_EPSILON > brush->maxs[type] ) { + return PSIDE_BACK; + } + if ( brush->mins[type] < dist - PLANESIDE_EPSILON && + brush->maxs[type] > dist + PLANESIDE_EPSILON ) { + s = PSIDE_BOTH; + } + } //end if + + if ( s != PSIDE_BOTH ) +#endif //ME + { + // if the brush actually uses the planenum, + // we can tell the side for sure + for ( i = 0; i < brush->numsides; i++ ) + { + num = brush->sides[i].planenum; + if ( num >= MAX_MAPFILE_PLANES ) { + Error( "bad planenum" ); + } + if ( num == planenum ) { + //we don't need to test this side plane again + brush->sides[i].flags |= SFL_TESTED; + return PSIDE_BACK | PSIDE_FACING; + } //end if + if ( num == ( planenum ^ 1 ) ) { + //we don't need to test this side plane again + brush->sides[i].flags |= SFL_TESTED; + return PSIDE_FRONT | PSIDE_FACING; + } //end if + } //end for + + // box on plane side + s = BoxOnPlaneSide( brush->mins, brush->maxs, plane ); + + if ( s != PSIDE_BOTH ) { + return s; + } + } //end if + +// if both sides, count the visible faces split + d_front = d_back = 0; + + for ( i = 0; i < brush->numsides; i++ ) + { + if ( brush->sides[i].texinfo == TEXINFO_NODE ) { + continue; // on node, don't worry about splits + } + if ( !( brush->sides[i].flags & SFL_VISIBLE ) ) { + continue; // we don't care about non-visible + } + w = brush->sides[i].winding; + if ( !w ) { + continue; + } + front = back = 0; + for ( j = 0; j < w->numpoints; j++ ) + { + d = DotProduct( w->p[j], plane->normal ) - plane->dist; + if ( d > d_front ) { + d_front = d; + } + if ( d < d_back ) { + d_back = d; + } + if ( d > 0.1 ) { // PLANESIDE_EPSILON) + front = 1; + } + if ( d < -0.1 ) { // PLANESIDE_EPSILON) + back = 1; + } + } //end for + if ( front && back ) { + if ( !( brush->sides[i].surf & SURF_SKIP ) ) { + ( *numsplits )++; + if ( brush->sides[i].surf & SURF_HINT ) { + *hintsplit = true; + } //end if + } //end if + } //end if + } //end for + + if ( ( d_front > 0.0 && d_front < 1.0 ) + || ( d_back < 0.0 && d_back > -1.0 ) ) { + ( *epsilonbrush )++; + } + +#if 0 + if ( *numsplits == 0 ) { // didn't really need to be split + if ( front ) { + s = PSIDE_FRONT; + } else if ( back ) { + s = PSIDE_BACK; + } else { s = 0;} + } +#endif + + return s; +} //end of the function TestBrushToPlanenum +//=========================================================================== +// Returns true if the winding would be crunched out of +// existance by the vertex snapping. +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#define EDGE_LENGTH 0.2 +qboolean WindingIsTiny( winding_t *w ) { +#if 0 + if ( WindingArea( w ) < 1 ) { + return true; + } + return false; +#else + int i, j; + vec_t len; + vec3_t delta; + int edges; + + edges = 0; + for ( i = 0 ; i < w->numpoints ; i++ ) + { + j = i == w->numpoints - 1 ? 0 : i + 1; + VectorSubtract( w->p[j], w->p[i], delta ); + len = VectorLength( delta ); + if ( len > EDGE_LENGTH ) { + if ( ++edges == 3 ) { + return false; + } + } + } + return true; +#endif +} +//=========================================================================== +// Returns true if the winding still has one of the points +// from basewinding for plane +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean WindingIsHuge( winding_t *w ) { + int i, j; + + for ( i = 0 ; i < w->numpoints ; i++ ) + { + for ( j = 0 ; j < 3 ; j++ ) + if ( w->p[i][j] < -BOGUS_RANGE + 1 || w->p[i][j] > BOGUS_RANGE - 1 ) { + return true; + } + } + return false; +} //end of the function WindingIsHuge +//=========================================================================== +// creates a leaf out of the given nodes with the given brushes +// +// FIXME: use the origin->expansionbbox to find areas with different +// presence types +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void LeafNode( node_t *node, bspbrush_t *brushes ) { + bspbrush_t *b; + int i; + + node->side = NULL; + node->planenum = PLANENUM_LEAF; + node->contents = 0; + + for ( b = brushes; b; b = b->next ) + { + // if the brush is solid and all of its sides are on nodes, + // it eats everything + if ( b->original->contents & CONTENTS_SOLID ) { + for ( i = 0 ; i < b->numsides ; i++ ) + if ( b->sides[i].texinfo != TEXINFO_NODE ) { + break; + } + if ( i == b->numsides ) { + node->contents = CONTENTS_SOLID; + break; + } //end if + } //end if + node->contents |= b->original->contents; + } //end for + + if ( create_aas ) { + node->expansionbboxes = 0; + node->contents = 0; + for ( b = brushes; b; b = b->next ) + { + node->expansionbboxes |= b->original->expansionbbox; + node->contents |= b->original->contents; + if ( b->original->modelnum ) { + node->modelnum = b->original->modelnum; + } + } //end for + if ( node->contents & CONTENTS_SOLID ) { + if ( node->expansionbboxes != cfg.allpresencetypes ) { + node->contents &= ~CONTENTS_SOLID; + } //end if + } //end if + } //end if + + node->brushlist = brushes; +} //end of the function LeafNode +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void CheckPlaneAgainstParents( int pnum, node_t *node ) { + node_t *p; + + for ( p = node->parent; p; p = p->parent ) + { + if ( p->planenum == pnum ) { + Error( "Tried parent" ); + } + } //end for +} //end of the function CheckPlaneAgainstParants +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean CheckPlaneAgainstVolume( int pnum, node_t *node ) { + bspbrush_t *front, *back; + qboolean good; + + SplitBrush( node->volume, pnum, &front, &back ); + + good = ( front && back ); + + if ( front ) { + FreeBrush( front ); + } + if ( back ) { + FreeBrush( back ); + } + + return good; +} //end of the function CheckPlaneAgaintsVolume +//=========================================================================== +// Using a hueristic, choses one of the sides out of the brushlist +// to partition the brushes with. +// Returns NULL if there are no valid planes to split with.. +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== + +/* + + //perfect for fact2: + + value = 5*facing - 7*splits - abs(front-back); + if (mapplanes[pnum].type < 3) + value+=3; // axial is better +*/ + +side_t *SelectSplitSide( bspbrush_t *brushes, node_t *node ) { + int value, bestvalue; + bspbrush_t *brush, *test; + side_t *side, *bestside; + int i, pass, numpasses; + //int j; + int pnum; + int s; + int front, back, both, facing, splits; + int bsplits; + int bestsplits; + int epsilonbrush; + qboolean hintsplit = false; + + bestside = NULL; + bestvalue = -99999; + bestsplits = 0; + + // the search order goes: visible-structural, visible-detail, + // nonvisible-structural, nonvisible-detail. + // If any valid plane is available in a pass, no further + // passes will be tried. + numpasses = 2; + for ( pass = 0; pass < numpasses; pass++ ) + { + for ( brush = brushes; brush; brush = brush->next ) + { + // only check detail the second pass +// if ( (pass & 1) && !(brush->original->contents & CONTENTS_DETAIL) ) +// continue; +// if ( !(pass & 1) && (brush->original->contents & CONTENTS_DETAIL) ) +// continue; + for ( i = 0; i < brush->numsides; i++ ) + { + side = brush->sides + i; +// if (side->flags & SFL_BEVEL) +// continue; // never use a bevel as a spliter + if ( !side->winding ) { + continue; // nothing visible, so it can't split + } + if ( side->texinfo == TEXINFO_NODE ) { + continue; // allready a node splitter + } + if ( side->flags & SFL_TESTED ) { + continue; // we allready have metrics for this plane + } +// if (side->surf & SURF_SKIP) +// continue; // skip surfaces are never chosen +// if (!(side->flags & SFL_VISIBLE) && (pass < 2)) +// continue; // only check visible faces on first pass + if ( ( side->flags & SFL_CURVE ) && ( pass < 1 ) ) { + continue; // only check curves on the second pass + + } + pnum = side->planenum; + pnum &= ~1; // allways use positive facing plane + + CheckPlaneAgainstParents( pnum, node ); + + if ( !CheckPlaneAgainstVolume( pnum, node ) ) { + continue; // would produce a tiny volume + + } + front = 0; + back = 0; + both = 0; + facing = 0; + splits = 0; + epsilonbrush = 0; + + //inner loop: optimize + for ( test = brushes; test; test = test->next ) + { + s = TestBrushToPlanenum( test, pnum, &bsplits, &hintsplit, &epsilonbrush ); + + splits += bsplits; +// if (bsplits && (s&PSIDE_FACING) ) +// Error ("PSIDE_FACING with splits"); + + test->testside = s; + // + if ( s & PSIDE_FACING ) { + facing++; + } + if ( s & PSIDE_FRONT ) { + front++; + } + if ( s & PSIDE_BACK ) { + back++; + } + if ( s == PSIDE_BOTH ) { + both++; + } + } //end for + + // give a value estimate for using this plane + value = 5 * facing - 5 * splits - abs( front - back ); +// value = -5*splits; +// value = 5*facing - 5*splits; + if ( mapplanes[pnum].type < 3 ) { + value += 5; // axial is better + + } + value -= epsilonbrush * 1000; // avoid! + + // never split a hint side except with another hint + if ( hintsplit && !( side->surf & SURF_HINT ) ) { + value = -9999999; + } + + // save off the side test so we don't need + // to recalculate it when we actually seperate + // the brushes + if ( value > bestvalue ) { + bestvalue = value; + bestside = side; + bestsplits = splits; + for ( test = brushes; test ; test = test->next ) + test->side = test->testside; + } //end if + } //end for + } //end for (brush = brushes; + + // if we found a good plane, don't bother trying any + // other passes + if ( bestside ) { + if ( pass > 1 ) { + if ( numthreads == 1 ) { + c_nonvis++; + } + } + if ( pass > 0 ) { + node->detail_seperator = true; // not needed for vis + } + break; + } //end if + } //end for (pass = 0; + + // + // clear all the tested flags we set + // + for ( brush = brushes ; brush ; brush = brush->next ) + { + for ( i = 0; i < brush->numsides; i++ ) + { + brush->sides[i].flags &= ~SFL_TESTED; + } //end for + } //end for + + return bestside; +} //end of the function SelectSplitSide +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BrushMostlyOnSide( bspbrush_t *brush, plane_t *plane ) { + int i, j; + winding_t *w; + vec_t d, max; + int side; + + max = 0; + side = PSIDE_FRONT; + for ( i = 0 ; i < brush->numsides ; i++ ) + { + w = brush->sides[i].winding; + if ( !w ) { + continue; + } + for ( j = 0 ; j < w->numpoints ; j++ ) + { + d = DotProduct( w->p[j], plane->normal ) - plane->dist; + if ( d > max ) { + max = d; + side = PSIDE_FRONT; + } + if ( -d > max ) { + max = -d; + side = PSIDE_BACK; + } + } + } + return side; +} //end of the function BrushMostlyOnSide +//=========================================================================== +// Generates two new brushes, leaving the original +// unchanged +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void SplitBrush( bspbrush_t *brush, int planenum, + bspbrush_t **front, bspbrush_t **back ) { + bspbrush_t *b[2]; + int i, j; + winding_t *w, *cw[2], *midwinding; + plane_t *plane, *plane2; + side_t *s, *cs; + float d, d_front, d_back; + + *front = *back = NULL; + plane = &mapplanes[planenum]; + + // check all points + d_front = d_back = 0; + for ( i = 0 ; i < brush->numsides ; i++ ) + { + w = brush->sides[i].winding; + if ( !w ) { + continue; + } + for ( j = 0 ; j < w->numpoints ; j++ ) + { + d = DotProduct( w->p[j], plane->normal ) - plane->dist; + if ( d > 0 && d > d_front ) { + d_front = d; + } + if ( d < 0 && d < d_back ) { + d_back = d; + } + } + } + + if ( d_front < 0.2 ) { // PLANESIDE_EPSILON) + // only on back + *back = CopyBrush( brush ); + return; + } + if ( d_back > -0.2 ) { // PLANESIDE_EPSILON) + // only on front + *front = CopyBrush( brush ); + return; + } + + // create a new winding from the split plane + + w = BaseWindingForPlane( plane->normal, plane->dist ); + for ( i = 0 ; i < brush->numsides && w ; i++ ) + { + plane2 = &mapplanes[brush->sides[i].planenum ^ 1]; + ChopWindingInPlace( &w, plane2->normal, plane2->dist, 0 ); // PLANESIDE_EPSILON); + } + + if ( !w || WindingIsTiny( w ) ) { // the brush isn't really split + int side; + + side = BrushMostlyOnSide( brush, plane ); + if ( side == PSIDE_FRONT ) { + *front = CopyBrush( brush ); + } + if ( side == PSIDE_BACK ) { + *back = CopyBrush( brush ); + } + //free a possible winding + if ( w ) { + FreeWinding( w ); + } + return; + } + + if ( WindingIsHuge( w ) ) { + Log_Write( "WARNING: huge winding\n" ); + } + + midwinding = w; + + // split it for real + + for ( i = 0 ; i < 2 ; i++ ) + { + b[i] = AllocBrush( brush->numsides + 1 ); + b[i]->original = brush->original; + } + + // split all the current windings + + for ( i = 0 ; i < brush->numsides ; i++ ) + { + s = &brush->sides[i]; + w = s->winding; + if ( !w ) { + continue; + } + ClipWindingEpsilon( w, plane->normal, plane->dist, + 0 /*PLANESIDE_EPSILON*/, &cw[0], &cw[1] ); + for ( j = 0 ; j < 2 ; j++ ) + { + if ( !cw[j] ) { + continue; + } +#if 0 + if ( WindingIsTiny( cw[j] ) ) { + FreeWinding( cw[j] ); + continue; + } +#endif + cs = &b[j]->sides[b[j]->numsides]; + b[j]->numsides++; + *cs = *s; +// cs->planenum = s->planenum; +// cs->texinfo = s->texinfo; +// cs->original = s->original; + cs->winding = cw[j]; + cs->flags &= ~SFL_TESTED; + } + } + + + // see if we have valid polygons on both sides + + for ( i = 0 ; i < 2 ; i++ ) + { + BoundBrush( b[i] ); + for ( j = 0 ; j < 3 ; j++ ) + { + if ( b[i]->mins[j] < -MAX_MAP_BOUNDS || b[i]->maxs[j] > MAX_MAP_BOUNDS ) { + Log_Write( "bogus brush after clip" ); + break; + } + } + + if ( b[i]->numsides < 3 || j < 3 ) { + FreeBrush( b[i] ); + b[i] = NULL; + } + } + + if ( !( b[0] && b[1] ) ) { + if ( !b[0] && !b[1] ) { + Log_Write( "split removed brush\r\n" ); + } else { + Log_Write( "split not on both sides\r\n" ); + } + if ( b[0] ) { + FreeBrush( b[0] ); + *front = CopyBrush( brush ); + } + if ( b[1] ) { + FreeBrush( b[1] ); + *back = CopyBrush( brush ); + } + return; + } + + // add the midwinding to both sides + for ( i = 0 ; i < 2 ; i++ ) + { + cs = &b[i]->sides[b[i]->numsides]; + b[i]->numsides++; + + cs->planenum = planenum ^ i ^ 1; + cs->texinfo = TEXINFO_NODE; //never use these sides as splitters + cs->flags &= ~SFL_VISIBLE; + cs->flags &= ~SFL_TESTED; + if ( i == 0 ) { + cs->winding = CopyWinding( midwinding ); + } else { + cs->winding = midwinding; + } + } + + { + vec_t v1; + int i; + + for ( i = 0; i < 2; i++ ) + { + v1 = BrushVolume( b[i] ); + if ( v1 < 1.0 ) { + FreeBrush( b[i] ); + b[i] = NULL; + //Log_Write("tiny volume after clip"); + } + } + if ( !b[0] && !b[1] ) { + Log_Write( "two tiny brushes\r\n" ); + } //end if + } + + *front = b[0]; + *back = b[1]; +} //end of the function SplitBrush +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void SplitBrushList( bspbrush_t *brushes, + node_t *node, bspbrush_t **front, bspbrush_t **back ) { + bspbrush_t *brush, *newbrush, *newbrush2; + side_t *side; + int sides; + int i; + + *front = *back = NULL; + + for ( brush = brushes ; brush ; brush = brush->next ) + { + sides = brush->side; + + if ( sides == PSIDE_BOTH ) { // split into two brushes + SplitBrush( brush, node->planenum, &newbrush, &newbrush2 ); + if ( newbrush ) { + newbrush->next = *front; + *front = newbrush; + } //end if + if ( newbrush2 ) { + newbrush2->next = *back; + *back = newbrush2; + } //end if + continue; + } //end if + + newbrush = CopyBrush( brush ); + + // if the planenum is actualy a part of the brush + // find the plane and flag it as used so it won't be tried + // as a splitter again + if ( sides & PSIDE_FACING ) { + for ( i = 0 ; i < newbrush->numsides ; i++ ) + { + side = newbrush->sides + i; + if ( ( side->planenum & ~1 ) == node->planenum ) { + side->texinfo = TEXINFO_NODE; + } + } //end for + } //end if + + if ( sides & PSIDE_FRONT ) { + newbrush->next = *front; + *front = newbrush; + continue; + } //end if + if ( sides & PSIDE_BACK ) { + newbrush->next = *back; + *back = newbrush; + continue; + } //end if + } //end for +} //end of the function SplitBrushList +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void CheckBrushLists( bspbrush_t *brushlist1, bspbrush_t *brushlist2 ) { + bspbrush_t *brush1, *brush2; + + for ( brush1 = brushlist1; brush1; brush1 = brush1->next ) + { + for ( brush2 = brushlist2; brush2; brush2 = brush2->next ) + { + assert( brush1 != brush2 ); + } //end for + } //end for +} //end of the function CheckBrushLists +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int numrecurse = 0; + +node_t *BuildTree_r( node_t *node, bspbrush_t *brushes ) { + node_t *newnode; + side_t *bestside; + int i, totalmem; + bspbrush_t *children[2]; + + qprintf( "\r%6d", numrecurse ); + numrecurse++; + + if ( numthreads == 1 ) { + totalmem = WindingMemory() + c_nodememory + c_brushmemory; + if ( totalmem > c_peak_totalbspmemory ) { + c_peak_totalbspmemory = totalmem; + } + c_nodes++; + } //endif + + if ( drawflag ) { + DrawBrushList( brushes, node ); + } + + // find the best plane to use as a splitter + bestside = SelectSplitSide( brushes, node ); + if ( !bestside ) { + // leaf node + node->side = NULL; + node->planenum = -1; + LeafNode( node, brushes ); + if ( node->contents & CONTENTS_SOLID ) { + c_solidleafnodes++; + } + if ( create_aas ) { + //free up memory!!! + FreeBrushList( node->brushlist ); + node->brushlist = NULL; + //free the node volume brush + if ( node->volume ) { + FreeBrush( node->volume ); + node->volume = NULL; + } //end if + } //end if + return node; + } //end if + + // this is a splitplane node + node->side = bestside; + node->planenum = bestside->planenum & ~1; // always use front facing + + //split the brush list in two for both children + SplitBrushList( brushes, node, &children[0], &children[1] ); + //free the old brush list + FreeBrushList( brushes ); + + // allocate children before recursing + for ( i = 0; i < 2; i++ ) + { + newnode = AllocNode(); + newnode->parent = node; + node->children[i] = newnode; + } //end for + + //split the volume brush of the node for the children + SplitBrush( node->volume, node->planenum, &node->children[0]->volume, + &node->children[1]->volume ); + + if ( create_aas ) { + //free the volume brush + if ( node->volume ) { + FreeBrush( node->volume ); + node->volume = NULL; + } //end if + } //end if + // recursively process children + for ( i = 0; i < 2; i++ ) + { + node->children[i] = BuildTree_r( node->children[i], children[i] ); + } //end for + + return node; +} //end of the function BuildTree_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +node_t *firstnode; //first node in the list +node_t *lastnode; //last node in the list +int nodelistsize; //number of nodes in the list +int use_nodequeue = 0; //use nodequeue, otherwise a node stack is used +int numwaiting = 0; + +void ( *AddNodeToList )( node_t *node ); + +//add the node to the front of the node list +//(effectively using a node stack) +void AddNodeToStack( node_t *node ) { + ThreadLock(); + + node->next = firstnode; + firstnode = node; + if ( !lastnode ) { + lastnode = node; + } + nodelistsize++; + + ThreadUnlock(); + // + ThreadSemaphoreIncrease( 1 ); +} //end of the function AddNodeToStack +//add the node to the end of the node list +//(effectively using a node queue) +void AddNodeToQueue( node_t *node ) { + ThreadLock(); + + node->next = NULL; + if ( lastnode ) { + lastnode->next = node; + } else { firstnode = node;} + lastnode = node; + nodelistsize++; + + ThreadUnlock(); + // + ThreadSemaphoreIncrease( 1 ); +} //end of the function AddNodeToQueue +//get the first node from the front of the node list +node_t *NextNodeFromList( void ) { + node_t *node; + + ThreadLock(); + numwaiting++; + if ( !firstnode ) { + if ( numwaiting >= GetNumThreads() ) { + ThreadSemaphoreIncrease( GetNumThreads() ); + } + } //end if + ThreadUnlock(); + + ThreadSemaphoreWait(); + + ThreadLock(); + + numwaiting--; + + node = firstnode; + if ( firstnode ) { + firstnode = firstnode->next; + nodelistsize--; + } //end if + if ( !firstnode ) { + lastnode = NULL; + } + + ThreadUnlock(); + + return node; +} //end of the function NextNodeFromList +//returns the size of the node list +int NodeListSize( void ) { + int size; + + ThreadLock(); + size = nodelistsize; + ThreadUnlock(); + + return size; +} //end of the function NodeListSize +// +void IncreaseNodeCounter( void ) { + ThreadLock(); + //if (verbose) printf("\r%6d", numrecurse++); + qprintf( "\r%6d", numrecurse++ ); + //qprintf("\r%6d %d, %5d ", numrecurse++, GetNumThreads(), nodelistsize); + ThreadUnlock(); +} //end of the function IncreaseNodeCounter +//thread function, gets nodes from the nodelist and processes them +void BuildTreeThread( int threadid ) { + node_t *newnode, *node; + side_t *bestside; + int i, totalmem; + bspbrush_t *brushes; + + for ( node = NextNodeFromList(); node; ) + { + //if the nodelist isn't empty try to add another thread + //if (NodeListSize() > 10) AddThread(BuildTreeThread); + //display the number of nodes processed so far + if ( numthreads == 1 ) { + IncreaseNodeCounter(); + } + + brushes = node->brushlist; + + if ( numthreads == 1 ) { + totalmem = WindingMemory() + c_nodememory + c_brushmemory; + if ( totalmem > c_peak_totalbspmemory ) { + c_peak_totalbspmemory = totalmem; + } //end if + c_nodes++; + } //endif + + if ( drawflag ) { + DrawBrushList( brushes, node ); + } //end if + + if ( cancelconversion ) { + bestside = NULL; + } //end if + else + { + // find the best plane to use as a splitter + bestside = SelectSplitSide( brushes, node ); + } //end else + //if there's no split side left + if ( !bestside ) { + //create a leaf out of the node + LeafNode( node, brushes ); + if ( node->contents & CONTENTS_SOLID ) { + c_solidleafnodes++; + } + + if ( create_aas ) { + //free up memory!!! + FreeBrushList( node->brushlist ); + node->brushlist = NULL; + } //end if + //free the node volume brush (it is not used anymore) + if ( node->volume ) { + FreeBrush( node->volume ); + node->volume = NULL; + } //end if + node = NextNodeFromList(); + continue; + } //end if + + // this is a splitplane node + node->side = bestside; + node->planenum = bestside->planenum & ~1; //always use front facing + + //allocate children + for ( i = 0; i < 2; i++ ) + { + newnode = AllocNode(); + newnode->parent = node; + node->children[i] = newnode; + } //end for + + //split the brush list in two for both children + SplitBrushList( brushes, node, &node->children[0]->brushlist, &node->children[1]->brushlist ); + + CheckBrushLists( node->children[0]->brushlist, node->children[1]->brushlist ); + //free the old brush list + FreeBrushList( brushes ); + node->brushlist = NULL; + + //split the volume brush of the node for the children + SplitBrush( node->volume, node->planenum, &node->children[0]->volume, + &node->children[1]->volume ); + + if ( !node->children[0]->volume || !node->children[1]->volume ) { + Error( "child without volume brush" ); + } //end if + + //free the volume brush + if ( node->volume ) { + FreeBrush( node->volume ); + node->volume = NULL; + } //end if + + //add both children to the node list + //AddNodeToList(node->children[0]); + AddNodeToList( node->children[1] ); + node = node->children[0]; + } //end while + RemoveThread( threadid ); +} //end of the function BuildTreeThread +//=========================================================================== +// build the bsp tree using a node list +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BuildTree( tree_t *tree ) { + int i; + + firstnode = NULL; + lastnode = NULL; + //use a node queue or node stack + if ( use_nodequeue ) { + AddNodeToList = AddNodeToQueue; + } else { AddNodeToList = AddNodeToStack;} + //setup thread locking + ThreadSetupLock(); + ThreadSetupSemaphore(); + numwaiting = 0; + // + Log_Print( "%6d threads max\n", numthreads ); + if ( use_nodequeue ) { + Log_Print( "breadth first bsp building\n" ); + } else { Log_Print( "depth first bsp building\n" );} + qprintf( "%6d splits", 0 ); + //add the first node to the list + AddNodeToList( tree->headnode ); + //start the threads + for ( i = 0; i < numthreads; i++ ) + AddThread( BuildTreeThread ); + //wait for all added threads to be finished + WaitForAllThreadsFinished(); + //shutdown the thread locking + ThreadShutdownLock(); + ThreadShutdownSemaphore(); +} //end of the function BuildTree +//=========================================================================== +// The incoming brush list will be freed before exiting +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +tree_t *BrushBSP( bspbrush_t *brushlist, vec3_t mins, vec3_t maxs ) { + int i, c_faces, c_nonvisfaces, c_brushes; + bspbrush_t *b; + node_t *node; + tree_t *tree; + vec_t volume; +// vec3_t point; + + Log_Print( "-------- Brush BSP ---------\n" ); + + tree = Tree_Alloc(); + + c_faces = 0; + c_nonvisfaces = 0; + c_brushes = 0; + c_totalsides = 0; + for ( b = brushlist; b; b = b->next ) + { + c_brushes++; + + volume = BrushVolume( b ); + if ( volume < microvolume ) { + Log_Print( "WARNING: entity %i, brush %i: microbrush\n", + b->original->entitynum, b->original->brushnum ); + } //end if + + for ( i = 0 ; i < b->numsides ; i++ ) + { + if ( b->sides[i].flags & SFL_BEVEL ) { + continue; + } + if ( !b->sides[i].winding ) { + continue; + } + if ( b->sides[i].texinfo == TEXINFO_NODE ) { + continue; + } + if ( b->sides[i].flags & SFL_VISIBLE ) { + c_faces++; + } //end if + else + { + c_nonvisfaces++; + //if (create_aas) b->sides[i].texinfo = TEXINFO_NODE; + } //end if + } //end for + c_totalsides += b->numsides; + + AddPointToBounds( b->mins, tree->mins, tree->maxs ); + AddPointToBounds( b->maxs, tree->mins, tree->maxs ); + } //end for + + Log_Print( "%6i brushes\n", c_brushes ); + Log_Print( "%6i visible faces\n", c_faces ); + Log_Print( "%6i nonvisible faces\n", c_nonvisfaces ); + Log_Print( "%6i total sides\n", c_totalsides ); + + c_active_brushes = c_brushes; + c_nodememory = 0; + c_brushmemory = 0; + c_peak_brushmemory = 0; + + c_nodes = 0; + c_nonvis = 0; + node = AllocNode(); + + //volume of first node (head node) + node->volume = BrushFromBounds( mins, maxs ); + // + tree->headnode = node; + //just get some statistics and the mins/maxs of the node + numrecurse = 0; +// qprintf("%6d splits", numrecurse); + + tree->headnode->brushlist = brushlist; + BuildTree( tree ); + + //build the bsp tree with the start node from the brushlist +// node = BuildTree_r(node, brushlist); + + //if the conversion is cancelled + if ( cancelconversion ) { + return tree; + } + + qprintf( "\n" ); + Log_Write( "%6d splits\r\n", numrecurse ); +// Log_Print("%6i visible nodes\n", c_nodes/2 - c_nonvis); +// Log_Print("%6i nonvis nodes\n", c_nonvis); +// Log_Print("%6i leaves\n", (c_nodes+1)/2); +// Log_Print("%6i solid leaf nodes\n", c_solidleafnodes); +// Log_Print("%6i active brushes\n", c_active_brushes); + if ( numthreads == 1 ) { +// Log_Print("%6i KB of node memory\n", c_nodememory >> 10); +// Log_Print("%6i KB of brush memory\n", c_brushmemory >> 10); +// Log_Print("%6i KB of peak brush memory\n", c_peak_brushmemory >> 10); +// Log_Print("%6i KB of winding memory\n", WindingMemory() >> 10); +// Log_Print("%6i KB of peak winding memory\n", WindingPeakMemory() >> 10); + Log_Print( "%6i KB of peak total bsp memory\n", c_peak_totalbspmemory >> 10 ); + } //end if + + /* + point[0] = 1485; + point[1] = 956.125; + point[2] = 352.125; + node = PointInLeaf(tree->headnode, point); + if (node->planenum != PLANENUM_LEAF) + { + Log_Print("node not a leaf\n"); + } //end if + Log_Print("at %f %f %f:\n", point[0], point[1], point[2]); + PrintContents(node->contents); + Log_Print("node->expansionbboxes = %d\n", node->expansionbboxes); + // + */ + return tree; +} //end of the function BrushBSP + diff --git a/src/bspc/bspc.c b/src/bspc/bspc.c new file mode 100644 index 0000000..59f97bf --- /dev/null +++ b/src/bspc/bspc.c @@ -0,0 +1,1129 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: BSP tool +// Function: +// Programmer: id Software & Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +// Notes: Microsoft Visual C++ optimizations: +// "global optimization" or "full optimization" results +// in micro brushes?? +//=========================================================================== + +#if defined( WIN32 ) || defined( _WIN32 ) +#include +#include +#include +#include +#else +#include +#include +#include +#include +#endif +#include "qbsp.h" +#include "l_mem.h" +//#include "l_qfiles.h" +#include "../botlib/aasfile.h" +#include "../botlib/be_aas_cluster.h" +#include "../botlib/be_aas_optimize.h" +#include "aas_create.h" +#include "aas_store.h" +#include "aas_file.h" +#include "aas_cfg.h" +#include "be_aas_bspc.h" + +#define BSPC_VERSION "2.1c-wolf" + +// TTimo: some messy config things +#ifndef BSPC + #define BSPC // RF +#endif +#define stricmp strcasecmp + +void AAS_InitBotImport(); +void AAS_InitClustering(); +void AAS_ShowTotals(); + + +extern int use_nodequeue; //brushbsp.c +extern int calcgrapplereach; //be_aas_reach.c + +float subdivide_size = 240; +char source[1024]; +char name[1024]; +vec_t microvolume = 1.0; +char outbase[32]; +int entity_num; +aas_settings_t aassettings; + +qboolean noprune; //don't prune nodes (bspc.c) +qboolean glview; //create a gl view +qboolean nodetail; //don't use detail brushes (map.c) +qboolean fulldetail; //use but don't mark detail brushes (map.c) +qboolean onlyents; //only process the entities (bspc.c) +qboolean nomerge; //don't merge bsp node faces (faces.c) +qboolean nowater; //don't use the water brushes (map.c) +qboolean nocsg; //don't carve intersecting brushes (bspc.c) +qboolean noweld; //use unique face vertexes (faces.c) +qboolean noshare; //don't share bsp edges (faces.c) +qboolean nosubdiv; //don't subdivide bsp node faces (faces.c) +qboolean notjunc; //don't create tjunctions (edge melting) (faces.c) +qboolean optimize; //enable optimisation +qboolean leaktest; //perform a leak test +qboolean verboseentities; +qboolean freetree; //free the bsp tree when not needed anymore +qboolean create_aas; //create an .AAS file +qboolean nobrushmerge; //don't merge brushes +qboolean lessbrushes; //create less brushes instead of correct texture placement +qboolean cancelconversion; //true if the conversion is being cancelled +qboolean noliquids; //no liquids when writing map file +qboolean forcesidesvisible; //force all brush sides to be visible when loaded from bsp +qboolean writeaasmap; // +// qboolean capsule_collision = true; //use capsule collision +qboolean capsule_collision = false; // capsule collision// Ridah, allow to specify an extension for multiple AAS files per map + +char aas_extension[64]; +// done. + +float VectorDistance( vec3_t v1, vec3_t v2 ) { + vec3_t dir; + + VectorSubtract( v2, v1, dir ); + return VectorLength( dir ); +} + +/* +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ProcessWorldModel (void) +{ + entity_t *e; + tree_t *tree; + qboolean leaked; + int brush_start, brush_end; + + e = &entities[entity_num]; + + brush_start = e->firstbrush; + brush_end = brush_start + e->numbrushes; + leaked = false; + + //process the whole world in one time + tree = ProcessWorldBrushes(brush_start, brush_end); + //create the bsp tree portals + MakeTreePortals(tree); + //mark all leafs that can be reached by entities + if (FloodEntities(tree)) + { + FillOutside(tree->headnode); + } //end if + else + { + Log_Print("**** leaked ****\n"); + leaked = true; + LeakFile(tree); + if (leaktest) + { + Log_Print("--- MAP LEAKED ---\n"); + exit(0); + } //end if + } //end else + + MarkVisibleSides (tree, brush_start, brush_end); + + FloodAreas (tree); + +#ifndef ME + if (glview) WriteGLView(tree, source); +#endif + MakeFaces(tree->headnode); + FixTjuncs(tree->headnode); + + //NOTE: Never prune the nodes because the portals + // are screwed when prunning is done and as + // a result portal writing will crash + //if (!noprune) PruneNodes(tree->headnode); + + WriteBSP(tree->headnode); + + if (!leaked) WritePortalFile(tree); + + Tree_Free(tree); +} //end of the function ProcessWorldModel +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ProcessSubModel (void) +{ + entity_t *e; + int start, end; + tree_t *tree; + bspbrush_t *list; + vec3_t mins, maxs; + + e = &entities[entity_num]; + + start = e->firstbrush; + end = start + e->numbrushes; + + mins[0] = mins[1] = mins[2] = -4096; + maxs[0] = maxs[1] = maxs[2] = 4096; + list = MakeBspBrushList(start, end, mins, maxs); + if (!nocsg) list = ChopBrushes (list); + tree = BrushBSP (list, mins, maxs); + MakeTreePortals (tree); + MarkVisibleSides (tree, start, end); + MakeFaces (tree->headnode); + FixTjuncs (tree->headnode); + WriteBSP (tree->headnode); + Tree_Free (tree); +} //end of the function ProcessSubModel +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ProcessModels (void) +{ + BeginBSPFile(); + + for (entity_num = 0; entity_num < num_entities; entity_num++) + { + if (!entities[entity_num].numbrushes) + continue; + + Log_Print("############### model %i ###############\n", nummodels); + BeginModel(); + if (entity_num == 0) ProcessWorldModel(); + else ProcessSubModel(); + EndModel(); + + if (!verboseentities) + verbose = false; // don't bother printing submodels + } //end for + EndBSPFile(); +} //end of the function ProcessModels +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Win_Map2Bsp(char *bspfilename) +{ + double start, end; + char path[1024]; + + start = I_FloatTime(); + + ThreadSetDefault(); + //yeah sure Carmack + //numthreads = 1; // multiple threads aren't helping... + + strcpy(source, ExpandArg(bspfilename)); + StripExtension(source); + + //delete portal and line files + sprintf(path, "%s.prt", source); + remove(path); + sprintf(path, "%s.lin", source); + remove(path); + + strcpy(name, ExpandArg(bspfilename)); + DefaultExtension(name, ".map"); // might be .reg + + Q2_AllocMaxBSP(); + // + SetModelNumbers(); + SetLightStyles(); + ProcessModels(); + //write the BSP + Q2_WriteBSPFile(bspfilename); + + Q2_FreeMaxBSP(); + + end = I_FloatTime(); + Log_Print("%5.0f seconds elapsed\n", end-start); +} //end of the function Win_Map2Bsp +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Map2Bsp(char *mapfilename, char *outputfilename) +{ + double start, end; + char path[1024]; + + start = I_FloatTime (); + + ThreadSetDefault (); + //yeah sure Carmack + //numthreads = 1; //multiple threads aren't helping... + //SetQdirFromPath(bspfilename); + + strcpy(source, ExpandArg(mapfilename)); + StripExtension(source); + + // delete portal and line files + sprintf(path, "%s.prt", source); + remove(path); + sprintf(path, "%s.lin", source); + remove(path); + + strcpy(name, ExpandArg(mapfilename)); + DefaultExtension(name, ".map"); // might be .reg + + // + // if onlyents, just grab the entites and resave + // + if (onlyents) + { + char out[1024]; + + Q2_AllocMaxBSP(); + sprintf (out, "%s.bsp", source); + Q2_LoadBSPFile(out, 0, 0); + num_entities = 0; + + Q2_LoadMapFile(name); + SetModelNumbers(); + SetLightStyles(); + + Q2_UnparseEntities(); + + Q2_WriteBSPFile(out); + // + Q2_FreeMaxBSP(); + } //end if + else + { + // + // start from scratch + // + Q2_AllocMaxBSP(); + //load the map + Q2_LoadMapFile(name); + //create the .bsp file + SetModelNumbers(); + SetLightStyles(); + ProcessModels(); + //write the BSP + Q2_WriteBSPFile(outputfilename); + // + Q2_FreeMaxBSP(); + } //end else + + end = I_FloatTime(); + Log_Print("%5.0f seconds elapsed\n", end-start); +} //end of the function Map2Bsp +*/ +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AASOuputFile( quakefile_t *qf, char *outputpath, char *filename ) { + char ext[MAX_PATH]; + + // + if ( strlen( outputpath ) ) { + strcpy( filename, outputpath ); + //append the bsp file base + AppendPathSeperator( filename, MAX_PATH ); + ExtractFileBase( qf->origname, &filename[strlen( filename )] ); + + // Ridah, add extension + strcat( filename, aas_extension ); + // done. + + //append .aas + strcat( filename, ".aas" ); + return; + } //end if + // + ExtractFileExtension( qf->filename, ext ); + if ( !stricmp( ext, "pk3" ) || !stricmp( ext, "pak" ) || !stricmp( ext, "sin" ) ) { + strcpy( filename, qf->filename ); + while ( strlen( filename ) && + filename[strlen( filename ) - 1] != '\\' && + filename[strlen( filename ) - 1] != '/' ) + { + filename[strlen( filename ) - 1] = '\0'; + } //end while + strcat( filename, "maps" ); + if ( access( filename, 0x04 ) ) { + CreatePath( filename ); + } + //append the bsp file base + AppendPathSeperator( filename, MAX_PATH ); + ExtractFileBase( qf->origname, &filename[strlen( filename )] ); + + // Ridah, add extension + strcat( filename, aas_extension ); + // done. + + //append .aas + strcat( filename, ".aas" ); + } //end if + else + { + strcpy( filename, qf->filename ); + while ( strlen( filename ) && + filename[strlen( filename ) - 1] != '.' ) + { + filename[strlen( filename ) - 1] = '\0'; + } //end while + + // Ridah, add extension + strcat( filename, aas_extension ); + // done. + + strcat( filename, "aas" ); + } //end else +} //end of the function AASOutputFile +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void CreateAASFilesForAllBSPFiles( char *quakepath ) { +#if defined( WIN32 ) | defined( _WIN32 ) + WIN32_FIND_DATA filedata; + HWND handle; + struct _stat statbuf; +#else + glob_t globbuf; + struct stat statbuf; + int j; +#endif + //int done; // TTimo: unused + char filter[_MAX_PATH], bspfilter[_MAX_PATH], aasfilter[_MAX_PATH]; + char aasfile[_MAX_PATH], buf[_MAX_PATH], foldername[_MAX_PATH]; + quakefile_t *qf, *qf2, *files, *bspfiles, *aasfiles; + + strcpy( filter, quakepath ); + AppendPathSeperator( filter, sizeof( filter ) ); + strcat( filter, "*" ); + +#if defined( WIN32 ) | defined( _WIN32 ) + handle = FindFirstFile( filter, &filedata ); + done = ( handle == INVALID_HANDLE_VALUE ); + while ( !done ) + { + _splitpath( filter, foldername, NULL, NULL, NULL ); + _splitpath( filter, NULL, &foldername[strlen( foldername )], NULL, NULL ); + AppendPathSeperator( foldername, _MAX_PATH ); + strcat( foldername, filedata.cFileName ); + _stat( foldername, &statbuf ); +#else + glob( filter, 0, NULL, &globbuf ); + for ( j = 0; j < globbuf.gl_pathc; j++ ) + { + strcpy( foldername, globbuf.gl_pathv[j] ); + stat( foldername, &statbuf ); +#endif + //if it is a folder + if ( statbuf.st_mode & S_IFDIR ) { + // + AppendPathSeperator( foldername, sizeof( foldername ) ); + //get all the bsp files + strcpy( bspfilter, foldername ); + strcat( bspfilter, "maps/*.bsp" ); + files = FindQuakeFiles( bspfilter ); + strcpy( bspfilter, foldername ); + strcat( bspfilter, "*.pk3/maps/*.bsp" ); + bspfiles = FindQuakeFiles( bspfilter ); + for ( qf = bspfiles; qf; qf = qf->next ) if ( !qf->next ) { + break; + } + if ( qf ) { + qf->next = files; + } else { bspfiles = files;} + //get all the aas files + strcpy( aasfilter, foldername ); + strcat( aasfilter, "maps/*.aas" ); + files = FindQuakeFiles( aasfilter ); + strcpy( aasfilter, foldername ); + strcat( aasfilter, "*.pk3/maps/*.aas" ); + aasfiles = FindQuakeFiles( aasfilter ); + for ( qf = aasfiles; qf; qf = qf->next ) if ( !qf->next ) { + break; + } + if ( qf ) { + qf->next = files; + } else { aasfiles = files;} + // + for ( qf = bspfiles; qf; qf = qf->next ) + { + sprintf( aasfile, "%s/%s", qf->pakfile, qf->origname ); + Log_Print( "found %s\n", aasfile ); + strcpy( &aasfile[strlen( aasfile ) - strlen( ".bsp" )], ".aas" ); + for ( qf2 = aasfiles; qf2; qf2 = qf2->next ) + { + sprintf( buf, "%s/%s", qf2->pakfile, qf2->origname ); + if ( !stricmp( aasfile, buf ) ) { + Log_Print( "found %s\n", buf ); + break; + } //end if + } //end for + } //end for + } //end if +#if defined( WIN32 ) | defined( _WIN32 ) + //find the next file + done = !FindNextFile( handle, &filedata ); + } //end while +#else + } //end for + globfree( &globbuf ); +#endif +} //end of the function CreateAASFilesForAllBSPFiles +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +quakefile_t *GetArgumentFiles( int argc, char *argv[], int *i, char *ext ) { + quakefile_t *qfiles, *lastqf, *qf; + int j; + char buf[1024]; + + qfiles = NULL; + lastqf = NULL; + for (; ( *i ) + 1 < argc && argv[( *i ) + 1][0] != '-'; ( *i )++ ) + { + strcpy( buf, argv[( *i ) + 1] ); + for ( j = strlen( buf ) - 1; j >= strlen( buf ) - 4; j-- ) + if ( buf[j] == '.' ) { + break; + } + if ( j >= strlen( buf ) - 4 ) { + strcpy( &buf[j + 1], ext ); + } + qf = FindQuakeFiles( buf ); + if ( !qf ) { + continue; + } + if ( lastqf ) { + lastqf->next = qf; + } else { qfiles = qf;} + lastqf = qf; + while ( lastqf->next ) lastqf = lastqf->next; + } //end for + return qfiles; +} //end of the function GetArgumentFiles +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== + +#define COMP_BSP2MAP 1 +#define COMP_BSP2AAS 2 +#define COMP_REACH 3 +#define COMP_CLUSTER 4 +#define COMP_AASOPTIMIZE 5 +#define COMP_AASINFO 6 +#define COMP_TETRA 7 + +//#define GTKRADIANT + +#ifdef GTKRADIANT +// TTimo: comment this out and use a bogus RADIANT_VERSION if necessary +#include "include/version.h" +#endif + +int main( int argc, char **argv ) { + int i, comp = 0; + char outputpath[MAX_PATH] = ""; + char filename[MAX_PATH] = "unknown"; + quakefile_t *qfiles = NULL, *qf; // TTimo: init + + // Ridah, allow to specify an extension for multiple AAS files per map + int has_ext = 0; + // done. + + // Ridah, set the world pointer up for reachabilities + aasworld = aasworlds; + AAS_SetWorldPointer( &( *aasworld ) ); + // done. + + myargc = argc; + myargv = argv; + + Log_Open( "bspc.log" ); //open a log file + #ifdef GTKRADIANT + Log_Print( "BSPC version " BSPC_VERSION " by Mr Elusive\n" ); + Log_Print( "GtkRadiant " RADIANT_VERSION " %s\n", __DATE__ ); + #else + Log_Print( "BSPC version " BSPC_VERSION ", %s %s by Mr Elusive\n", __DATE__, __TIME__ ); + #endif + + DefaultCfg(); + for ( i = 1; i < argc; i++ ) + { + if ( !stricmp( argv[i],"-threads" ) ) { + if ( i + 1 >= argc ) { + i = 0; break; + } + numthreads = atoi( argv[++i] ); + Log_Print( "threads = %d\n", numthreads ); + } //end if + else if ( !stricmp( argv[i], "-noverbose" ) ) { + Log_Print( "verbose = false\n" ); + verbose = false; + } //end else if + else if ( !stricmp( argv[i], "-nocsg" ) ) { + Log_Print( "nocsg = true\n" ); + nocsg = true; + } //end else if + else if ( !stricmp( argv[i], "-optimize" ) ) { + Log_Print( "optimize = true\n" ); + optimize = true; + } //end else if + /* + else if (!stricmp(argv[i],"-glview")) + { + glview = true; + } //end else if + else if (!stricmp(argv[i], "-draw")) + { + Log_Print("drawflag = true\n"); + drawflag = true; + } //end else if + else if (!stricmp(argv[i], "-noweld")) + { + Log_Print("noweld = true\n"); + noweld = true; + } //end else if + else if (!stricmp(argv[i], "-noshare")) + { + Log_Print("noshare = true\n"); + noshare = true; + } //end else if + else if (!stricmp(argv[i], "-notjunc")) + { + Log_Print("notjunc = true\n"); + notjunc = true; + } //end else if + else if (!stricmp(argv[i], "-nowater")) + { + Log_Print("nowater = true\n"); + nowater = true; + } //end else if + else if (!stricmp(argv[i], "-noprune")) + { + Log_Print("noprune = true\n"); + noprune = true; + } //end else if + else if (!stricmp(argv[i], "-nomerge")) + { + Log_Print("nomerge = true\n"); + nomerge = true; + } //end else if + else if (!stricmp(argv[i], "-nosubdiv")) + { + Log_Print("nosubdiv = true\n"); + nosubdiv = true; + } //end else if + else if (!stricmp(argv[i], "-nodetail")) + { + Log_Print("nodetail = true\n"); + nodetail = true; + } //end else if + else if (!stricmp(argv[i], "-fulldetail")) + { + Log_Print("fulldetail = true\n"); + fulldetail = true; + } //end else if + else if (!stricmp(argv[i], "-onlyents")) + { + Log_Print("onlyents = true\n"); + onlyents = true; + } //end else if + else if (!stricmp(argv[i], "-micro")) + { + if (i + 1 >= argc) {i = 0; break;} + microvolume = atof(argv[++i]); + Log_Print("microvolume = %f\n", microvolume); + } //end else if + else if (!stricmp(argv[i], "-leaktest")) + { + Log_Print("leaktest = true\n"); + leaktest = true; + } //end else if + else if (!stricmp(argv[i], "-verboseentities")) + { + Log_Print("verboseentities = true\n"); + verboseentities = true; + } //end else if + else if (!stricmp(argv[i], "-chop")) + { + if (i + 1 >= argc) {i = 0; break;} + subdivide_size = atof(argv[++i]); + Log_Print("subdivide_size = %f\n", subdivide_size); + } //end else if + else if (!stricmp (argv[i], "-tmpout")) + { + strcpy (outbase, "/tmp"); + Log_Print("temp output\n"); + } //end else if + */ +#ifdef ME + else if ( !stricmp( argv[i], "-freetree" ) ) { + freetree = true; + Log_Print( "freetree = true\n" ); + } //end else if + else if ( !stricmp( argv[i], "-grapplereach" ) ) { + calcgrapplereach = true; + Log_Print( "grapplereach = true\n" ); + } //end else if + else if ( !stricmp( argv[i], "-nobrushmerge" ) ) { + nobrushmerge = true; + Log_Print( "nobrushmerge = true\n" ); + } //end else if + else if ( !stricmp( argv[i], "-noliquids" ) ) { + noliquids = true; + Log_Print( "noliquids = true\n" ); + } //end else if + else if ( !stricmp( argv[i], "-forcesidesvisible" ) ) { + forcesidesvisible = true; + Log_Print( "forcesidesvisible = true\n" ); + } //end else if + else if ( !stricmp( argv[i], "-output" ) ) { + if ( i + 1 >= argc ) { + i = 0; break; + } + if ( access( argv[i + 1], 0x04 ) ) { + Warning( "the folder %s does not exist", argv[i + 1] ); + } + strcpy( outputpath, argv[++i] ); + } //end else if + else if ( !stricmp( argv[i], "-breadthfirst" ) ) { + use_nodequeue = true; + Log_Print( "breadthfirst = true\n" ); + } //end else if + else if ( !stricmp( argv[i], "-cfg" ) ) { + if ( i + 1 >= argc ) { + i = 0; break; + } + if ( !LoadCfgFile( argv[++i] ) ) { + exit( 0 ); + } + } //end else if + else if ( !stricmp( argv[i], "-bsp2map" ) ) { + if ( i + 1 >= argc ) { + i = 0; break; + } + comp = COMP_BSP2MAP; + qfiles = GetArgumentFiles( argc, argv, &i, "bsp" ); + } //end else if + else if ( !stricmp( argv[i], "-bsp2aas" ) ) { + if ( i + 1 >= argc ) { + i = 0; break; + } + comp = COMP_BSP2AAS; + qfiles = GetArgumentFiles( argc, argv, &i, "bsp" ); + } //end else if + else if ( !stricmp( argv[i], "-aasall" ) ) { + if ( i + 1 >= argc ) { + i = 0; break; + } + CreateAASFilesForAllBSPFiles( argv[++i] ); + } //end else if + else if ( !stricmp( argv[i], "-reach" ) ) { + if ( i + 1 >= argc ) { + i = 0; break; + } + comp = COMP_REACH; + qfiles = GetArgumentFiles( argc, argv, &i, "bsp" ); + } //end else if + else if ( !stricmp( argv[i], "-cluster" ) ) { + if ( i + 1 >= argc ) { + i = 0; break; + } + comp = COMP_CLUSTER; + qfiles = GetArgumentFiles( argc, argv, &i, "bsp" ); + } //end else if + else if ( !stricmp( argv[i], "-aasinfo" ) ) { + if ( i + 1 >= argc ) { + i = 0; break; + } + comp = COMP_AASINFO; + qfiles = GetArgumentFiles( argc, argv, &i, "aas" ); + } //end else if + else if ( !stricmp( argv[i], "-aasopt" ) ) { + if ( i + 1 >= argc ) { + i = 0; break; + } + comp = COMP_AASOPTIMIZE; + qfiles = GetArgumentFiles( argc, argv, &i, "aas" ); + } //end else if + else if ( !stricmp( argv[i], "-tetra" ) ) { + if ( i + 1 >= argc ) { + i = 0; break; + } + comp = COMP_TETRA; + qfiles = GetArgumentFiles( argc, argv, &i, "aas" ); + } //end else if + else if ( !stricmp( argv[i], "-writeaasmap" ) ) { + writeaasmap = true; + Log_Print( "writeaasmap = true\n" ); + } + // Ridah, allow to specify an extension for multiple AAS files per map + else if ( !stricmp( argv[i], "-ext" ) ) { + if ( i + 1 >= argc ) { + i = 0; break; + } + strcpy( aas_extension, argv[++i] ); + has_ext = 1; + + } //end else if + // done. + +#endif //ME + else + { + Log_Print( "unknown parameter %s\n", argv[i] ); + break; + } //end else + } //end for + + //if there are parameters and there's no mismatch in one of the parameters + if ( argc > 1 && i == argc ) { + switch ( comp ) + { + case COMP_BSP2MAP: + { + if ( !qfiles ) { + Log_Print( "no files found\n" ); + } + for ( qf = qfiles; qf; qf = qf->next ) + { + //copy the output path + strcpy( filename, outputpath ); + //append the bsp file base + AppendPathSeperator( filename, MAX_PATH ); + ExtractFileBase( qf->origname, &filename[strlen( filename )] ); + //append .map + strcat( filename, ".map" ); + // + Log_Print( "bsp2map: %s to %s\n", qf->origname, filename ); + if ( qf->type != QFILETYPE_BSP ) { + Warning( "%s is probably not a BSP file\n", qf->origname ); + } + // + LoadMapFromBSP( qf ); + //write the map file + WriteMapFile( filename ); + } //end for + break; + } //end case + case COMP_BSP2AAS: + { + if ( !qfiles ) { + Log_Print( "no files found\n" ); + } + for ( qf = qfiles; qf; qf = qf->next ) + { + AASOuputFile( qf, outputpath, filename ); + // + Log_Print( "bsp2aas: %s to %s\n", qf->origname, filename ); + if ( qf->type != QFILETYPE_BSP ) { + Warning( "%s is probably not a BSP file\n", qf->origname ); + } + //set before map loading + create_aas = 1; + LoadMapFromBSP( qf ); + //create the AAS file + AAS_Create( filename ); + //if it's a Quake3 map calculate the reachabilities and clusters + if ( loadedmaptype == MAPTYPE_QUAKE3 ) { + AAS_CalcReachAndClusters( qf ); + } + // + if ( optimize ) { + AAS_Optimize(); + } + // + //write out the stored AAS file + if ( !AAS_WriteAASFile( filename ) ) { + Error( "error writing %s\n", filename ); + } //end if + //deallocate memory + AAS_FreeMaxAAS(); + } //end for + break; + } //end case + case COMP_REACH: + { + if ( !qfiles ) { + Log_Print( "no files found\n" ); + } + for ( qf = qfiles; qf; qf = qf->next ) + { + AASOuputFile( qf, outputpath, filename ); + // + Log_Print( "reach: %s to %s\n", qf->origname, filename ); + if ( qf->type != QFILETYPE_BSP ) { + Warning( "%s is probably not a BSP file\n", qf->origname ); + } + //if the AAS file exists in the output directory + if ( !access( filename, 0x04 ) ) { + if ( !AAS_LoadAASFile( filename, 0, 0 ) ) { + Error( "error loading aas file %s\n", filename ); + } //end if + //assume it's a Quake3 BSP file + loadedmaptype = MAPTYPE_QUAKE3; + } //end if + else + { + Warning( "AAS file %s not found in output folder\n", filename ); + Log_Print( "creating %s...\n", filename ); + //set before map loading + create_aas = 1; + LoadMapFromBSP( qf ); + //create the AAS file + AAS_Create( filename ); + } //end else + //if it's a Quake3 map calculate the reachabilities and clusters + if ( loadedmaptype == MAPTYPE_QUAKE3 ) { + AAS_CalcReachAndClusters( qf ); + } //end if + // + if ( optimize ) { + AAS_Optimize(); + } + //write out the stored AAS file + if ( !AAS_WriteAASFile( filename ) ) { + Error( "error writing %s\n", filename ); + } //end if + //deallocate memory + AAS_FreeMaxAAS(); + } //end for + break; + } //end case + case COMP_CLUSTER: + { + if ( !qfiles ) { + Log_Print( "no files found\n" ); + } + for ( qf = qfiles; qf; qf = qf->next ) + { + AASOuputFile( qf, outputpath, filename ); + // + Log_Print( "cluster: %s to %s\n", qf->origname, filename ); + if ( qf->type != QFILETYPE_BSP ) { + Warning( "%s is probably not a BSP file\n", qf->origname ); + } + //if the AAS file exists in the output directory + if ( !access( filename, 0x04 ) ) { + if ( !AAS_LoadAASFile( filename, 0, 0 ) ) { + Error( "error loading aas file %s\n", filename ); + } //end if + //assume it's a Quake3 BSP file + loadedmaptype = MAPTYPE_QUAKE3; + //if it's a Quake3 map calculate the clusters + if ( loadedmaptype == MAPTYPE_QUAKE3 ) { + ( *aasworld ).numclusters = 0; + AAS_InitBotImport(); + AAS_InitClustering(); + } //end if + } //end if + else + { + Warning( "AAS file %s not found in output folder\n", filename ); + Log_Print( "creating %s...\n", filename ); + //set before map loading + create_aas = 1; + LoadMapFromBSP( qf ); + //create the AAS file + AAS_Create( filename ); + //if it's a Quake3 map calculate the reachabilities and clusters + if ( loadedmaptype == MAPTYPE_QUAKE3 ) { + AAS_CalcReachAndClusters( qf ); + } + } //end else + // + if ( optimize ) { + AAS_Optimize(); + } + //write out the stored AAS file + if ( !AAS_WriteAASFile( filename ) ) { + Error( "error writing %s\n", filename ); + } //end if + //deallocate memory + AAS_FreeMaxAAS(); + } //end for + break; + } //end case + case COMP_AASOPTIMIZE: + { + if ( !qfiles ) { + Log_Print( "no files found\n" ); + } + for ( qf = qfiles; qf; qf = qf->next ) + { + AASOuputFile( qf, outputpath, filename ); + // + Log_Print( "optimizing: %s to %s\n", qf->origname, filename ); + if ( qf->type != QFILETYPE_AAS ) { + Warning( "%s is probably not a AAS file\n", qf->origname ); + } + // + AAS_InitBotImport(); + // + if ( !AAS_LoadAASFile( qf->filename, qf->offset, qf->length ) ) { + Error( "error loading aas file %s\n", qf->filename ); + } //end if + AAS_Optimize(); + //write out the stored AAS file + if ( !AAS_WriteAASFile( filename ) ) { + Error( "error writing %s\n", filename ); + } //end if + //deallocate memory + AAS_FreeMaxAAS(); + } //end for + break; + } //end case + case COMP_AASINFO: + { + if ( !qfiles ) { + Log_Print( "no files found\n" ); + } + for ( qf = qfiles; qf; qf = qf->next ) + { + AASOuputFile( qf, outputpath, filename ); + // + Log_Print( "aas info for: %s\n", filename ); + if ( qf->type != QFILETYPE_AAS ) { + Warning( "%s is probably not a AAS file\n", qf->origname ); + } + // + AAS_InitBotImport(); + // + if ( !AAS_LoadAASFile( qf->filename, qf->offset, qf->length ) ) { + Error( "error loading aas file %s\n", qf->filename ); + } //end if + AAS_ShowTotals(); + } //end for + } //end case + case COMP_TETRA: + { + if ( !qfiles ) { + Log_Print( "no files found\n" ); + } + for ( qf = qfiles; qf; qf = qf->next ) + { + //TH_AASToTetrahedrons(qf->filename); + } //end for + break; + } //end case + default: + { + Log_Print( "don't know what to do\n" ); + break; + } //end default + } //end switch + } //end if + else + { + Log_Print( "Usage: bspc [- [- ...]]\n" +#if defined( WIN32 ) || defined( _WIN32 ) + "Example 1: bspc -bsp2aas d:\\quake3\\baseq3\\maps\\mymap?.bsp\n" + "Example 2: bspc -bsp2aas d:\\quake3\\baseq3\\pak0.pk3\\maps/q3dm*.bsp\n" +#else + "Example 1: bspc -bsp2aas /quake3/baseq3/maps/mymap?.bsp\n" + "Example 2: bspc -bsp2aas /quake3/baseq3/pak0.pk3/maps/q3dm*.bsp\n" +#endif + "\n" + "Switches:\n" + //" bsp2map <[pakfilter/]filter.bsp> = convert BSP to MAP\n" + " bsp2aas <[pakfilter/]filter.bsp> = convert BSP to AAS\n" + //" aasall = create AAS files for all BSPs\n" + " reach = compute reachability & clusters\n" + " cluster = compute clusters\n" + " aasopt = optimize aas file\n" + //" tetra = tetrahedral decomposition\n" + " output = set output path\n" + " threads = set number of threads to X\n" + " cfg = use this cfg file\n" + " optimize = enable optimization\n" + " noverbose = disable verbose output\n" + " breadthfirst = breadth first bsp building\n" + " nobrushmerge = don't merge brushes\n" + " noliquids = don't write liquids to map\n" + " freetree = free the bsp tree\n" + " nocsg = disables brush chopping\n" + " forcesidesvisible = force all sides to be visible\n" + " grapplereach = calculate grapple reachabilities\n" + " writeaasmap = write the map the AI sees\n" + +/* " glview = output a GL view\n" + " draw = enables drawing\n" + " noweld = disables weld\n" + " noshare = disables sharing\n" + " notjunc = disables juncs\n" + " nowater = disables water brushes\n" + " noprune = disables node prunes\n" + " nomerge = disables face merging\n" + " nosubdiv = disables subdeviding\n" + " nodetail = disables detail brushes\n" + " fulldetail = enables full detail\n" + " onlyents = only compile entities with bsp\n" + " micro \n" + " = sets the micro volume to the given float\n" + " leaktest = perform a leak test\n" + " verboseentities\n" + " = enable entity verbose mode\n" + " chop \n" + " = sets the subdivide size to the given float\n"*/ + "\n" ); + } //end else + Log_Close(); //close the log file + return 0; +} //end of the function main + + diff --git a/src/bspc/csg.c b/src/bspc/csg.c new file mode 100644 index 0000000..c3256cc --- /dev/null +++ b/src/bspc/csg.c @@ -0,0 +1,1053 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: Brush CSG +// Function: +// Programmer(s): id Software & Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +// Notes: Microsoft Visual C++ optimizations: +// "global optimization" or "full optimization" results +// in micro brushes?? +//=========================================================================== + +#include "qbsp.h" + +/* + +tag all brushes with original contents +brushes may contain multiple contents +there will be no brush overlap after csg phase + +*/ + +int minplanenums[3]; +int maxplanenums[3]; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void CheckBSPBrush( bspbrush_t *brush ) { + int i, j; + plane_t *plane1, *plane2; + + //check if the brush is convex... flipped planes make a brush non-convex + for ( i = 0; i < brush->numsides; i++ ) + { + for ( j = 0; j < brush->numsides; j++ ) + { + if ( i == j ) { + continue; + } + plane1 = &mapplanes[brush->sides[i].planenum]; + plane2 = &mapplanes[brush->sides[j].planenum]; + // + if ( WindingsNonConvex( brush->sides[i].winding, + brush->sides[j].winding, + plane1->normal, plane2->normal, + plane1->dist, plane2->dist ) ) { + Log_Print( "non convex brush" ); + break; + } //end if + } //end for + } //end for + BoundBrush( brush ); + //check for out of bound brushes + for ( i = 0; i < 3; i++ ) + { + if ( brush->mins[i] < -MAX_MAP_BOUNDS || brush->maxs[i] > MAX_MAP_BOUNDS ) { + Log_Print( "brush: bounds out of range\n" ); + Log_Print( "ob->mins[%d] = %f, ob->maxs[%d] = %f\n", i, brush->mins[i], i, brush->maxs[i] ); + break; + } //end if + if ( brush->mins[i] > MAX_MAP_BOUNDS || brush->maxs[i] < -MAX_MAP_BOUNDS ) { + Log_Print( "brush: no visible sides on brush\n" ); + Log_Print( "ob->mins[%d] = %f, ob->maxs[%d] = %f\n", i, brush->mins[i], i, brush->maxs[i] ); + break; + } //end if + } //end for +} //end of the function CheckBSPBrush +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void BSPBrushWindings( bspbrush_t *brush ) { + int i, j; + winding_t *w; + plane_t *plane; + + for ( i = 0; i < brush->numsides; i++ ) + { + plane = &mapplanes[brush->sides[i].planenum]; + w = BaseWindingForPlane( plane->normal, plane->dist ); + for ( j = 0; j < brush->numsides && w; j++ ) + { + if ( i == j ) { + continue; + } + plane = &mapplanes[brush->sides[j].planenum ^ 1]; + ChopWindingInPlace( &w, plane->normal, plane->dist, 0 ); //CLIP_EPSILON); + } //end for + brush->sides[i].winding = w; + } //end for +} //end of the function BSPBrushWindings +//=========================================================================== +// NOTE: can't keep brush->original intact +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *TryMergeBrushes( bspbrush_t *brush1, bspbrush_t *brush2 ) { + int i, j, k, n, shared; + side_t *side1, *side2, *cs; + plane_t *plane1, *plane2; + bspbrush_t *newbrush; + + //check for bounding box overlapp + for ( i = 0; i < 3; i++ ) + { + if ( brush1->mins[i] > brush2->maxs[i] + 2 + || brush1->maxs[i] < brush2->mins[i] - 2 ) { + return NULL; + } //end if + } //end for + // + shared = 0; + //check if the brush is convex... flipped planes make a brush non-convex + for ( i = 0; i < brush1->numsides; i++ ) + { + side1 = &brush1->sides[i]; + //don't check the "shared" sides + for ( k = 0; k < brush2->numsides; k++ ) + { + side2 = &brush2->sides[k]; + if ( side1->planenum == ( side2->planenum ^ 1 ) ) { + shared++; + //there may only be ONE shared side + if ( shared > 1 ) { + return NULL; + } + break; + } //end if + } //end for + if ( k < brush2->numsides ) { + continue; + } + // + for ( j = 0; j < brush2->numsides; j++ ) + { + side2 = &brush2->sides[j]; + //don't check the "shared" sides + for ( n = 0; n < brush1->numsides; n++ ) + { + side1 = &brush1->sides[n]; + if ( side1->planenum == ( side2->planenum ^ 1 ) ) { + break; + } + } //end for + if ( n < brush1->numsides ) { + continue; + } + // + side1 = &brush1->sides[i]; + //if the side is in the same plane + //* + if ( side1->planenum == side2->planenum ) { + if ( side1->texinfo != TEXINFO_NODE && + side2->texinfo != TEXINFO_NODE && + side1->texinfo != side2->texinfo ) { + return NULL; + } + continue; + } //end if + // + plane1 = &mapplanes[side1->planenum]; + plane2 = &mapplanes[side2->planenum]; + // + if ( WindingsNonConvex( side1->winding, side2->winding, + plane1->normal, plane2->normal, + plane1->dist, plane2->dist ) ) { + return NULL; + } //end if + } //end for + } //end for + newbrush = AllocBrush( brush1->numsides + brush2->numsides ); + newbrush->original = brush1->original; + newbrush->numsides = 0; + //newbrush->side = brush1->side; //brush contents + //fix texinfos for sides lying in the same plane + for ( i = 0; i < brush1->numsides; i++ ) + { + side1 = &brush1->sides[i]; + // + for ( n = 0; n < brush2->numsides; n++ ) + { + side2 = &brush2->sides[n]; + //if both sides are in the same plane get the texinfo right + if ( side1->planenum == side2->planenum ) { + if ( side1->texinfo == TEXINFO_NODE ) { + side1->texinfo = side2->texinfo; + } + if ( side2->texinfo == TEXINFO_NODE ) { + side2->texinfo = side1->texinfo; + } + } //end if + } //end for + } //end for + // + for ( i = 0; i < brush1->numsides; i++ ) + { + side1 = &brush1->sides[i]; + //don't add the "shared" sides + for ( n = 0; n < brush2->numsides; n++ ) + { + side2 = &brush2->sides[n]; + if ( side1->planenum == ( side2->planenum ^ 1 ) ) { + break; + } + } //end for + if ( n < brush2->numsides ) { + continue; + } + // + for ( n = 0; n < newbrush->numsides; n++ ) + { + cs = &newbrush->sides[n]; + if ( cs->planenum == side1->planenum ) { + Log_Print( "brush duplicate plane\n" ); + break; + } //end if + } //end if + if ( n < newbrush->numsides ) { + continue; + } + //add this side + cs = &newbrush->sides[newbrush->numsides]; + newbrush->numsides++; + *cs = *side1; + } //end for + for ( j = 0; j < brush2->numsides; j++ ) + { + side2 = &brush2->sides[j]; + for ( n = 0; n < brush1->numsides; n++ ) + { + side1 = &brush1->sides[n]; + //if the side is in the same plane + if ( side2->planenum == side1->planenum ) { + break; + } + //don't add the "shared" sides + if ( side2->planenum == ( side1->planenum ^ 1 ) ) { + break; + } + } //end for + if ( n < brush1->numsides ) { + continue; + } + // + for ( n = 0; n < newbrush->numsides; n++ ) + { + cs = &newbrush->sides[n]; + if ( cs->planenum == side2->planenum ) { + Log_Print( "brush duplicate plane\n" ); + break; + } //end if + } //end if + if ( n < newbrush->numsides ) { + continue; + } + //add this side + cs = &newbrush->sides[newbrush->numsides]; + newbrush->numsides++; + *cs = *side2; + } //end for + BSPBrushWindings( newbrush ); + BoundBrush( newbrush ); + CheckBSPBrush( newbrush ); + return newbrush; +} //end of the function TryMergeBrushes +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *MergeBrushes( bspbrush_t *brushlist ) { + int nummerges, merged; + bspbrush_t *b1, *b2, *tail, *newbrush, *newbrushlist; + bspbrush_t *lastb2; + + if ( !brushlist ) { + return NULL; + } + + qprintf( "%5d brushes merged", nummerges = 0 ); + do + { + for ( tail = brushlist; tail; tail = tail->next ) + { + if ( !tail->next ) { + break; + } + } //end for + merged = 0; + newbrushlist = NULL; + for ( b1 = brushlist; b1; b1 = brushlist ) + { + lastb2 = b1; + for ( b2 = b1->next; b2; b2 = b2->next ) + { + //if the brushes don't have the same contents + if ( b1->original->contents != b2->original->contents || + b1->original->expansionbbox != b2->original->expansionbbox ) { + newbrush = NULL; + } else { newbrush = TryMergeBrushes( b1, b2 );} + if ( newbrush ) { + tail->next = newbrush; + lastb2->next = b2->next; + brushlist = brushlist->next; + FreeBrush( b1 ); + FreeBrush( b2 ); + for ( tail = brushlist; tail; tail = tail->next ) + { + if ( !tail->next ) { + break; + } + } //end for + merged++; + qprintf( "\r%5d", nummerges++ ); + break; + } //end if + lastb2 = b2; + } //end for + //if b1 can't be merged with any of the other brushes + if ( !b2 ) { + brushlist = brushlist->next; + //keep b1 + b1->next = newbrushlist; + newbrushlist = b1; + } //end else + } //end for + brushlist = newbrushlist; + } while ( merged ); + qprintf( "\n" ); + return newbrushlist; +} //end of the function MergeBrushes +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void SplitBrush2( bspbrush_t *brush, int planenum, + bspbrush_t **front, bspbrush_t **back ) { + SplitBrush( brush, planenum, front, back ); +#if 0 + if ( *front && ( *front )->sides[( *front )->numsides - 1].texinfo == -1 ) { + ( *front )->sides[( *front )->numsides - 1].texinfo = ( *front )->sides[0].texinfo; // not -1 + } + if ( *back && ( *back )->sides[( *back )->numsides - 1].texinfo == -1 ) { + ( *back )->sides[( *back )->numsides - 1].texinfo = ( *back )->sides[0].texinfo; // not -1 + } +#endif +} //end of the function SplitBrush2 +//=========================================================================== +// Returns a list of brushes that remain after B is subtracted from A. +// May by empty if A is contained inside B. +// The originals are undisturbed. +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *SubtractBrush( bspbrush_t *a, bspbrush_t *b ) { // a - b = out (list) + int i; + bspbrush_t *front, *back; + bspbrush_t *out, *in; + + in = a; + out = NULL; + for ( i = 0; i < b->numsides && in; i++ ) + { + SplitBrush2( in, b->sides[i].planenum, &front, &back ); + if ( in != a ) { + FreeBrush( in ); + } + if ( front ) { // add to list + front->next = out; + out = front; + } //end if + in = back; + } //end for + if ( in ) { + FreeBrush( in ); + } //end if + else + { // didn't really intersect + FreeBrushList( out ); + return a; + } //end else + return out; +} //end of the function SubtractBrush +//=========================================================================== +// Returns a single brush made up by the intersection of the +// two provided brushes, or NULL if they are disjoint. +// +// The originals are undisturbed. +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *IntersectBrush( bspbrush_t *a, bspbrush_t *b ) { + int i; + bspbrush_t *front, *back; + bspbrush_t *in; + + in = a; + for ( i = 0 ; i < b->numsides && in ; i++ ) + { + SplitBrush2( in, b->sides[i].planenum, &front, &back ); + if ( in != a ) { + FreeBrush( in ); + } + if ( front ) { + FreeBrush( front ); + } + in = back; + } //end for + + if ( in == a ) { + return NULL; + } + + in->next = NULL; + return in; +} //end of the function IntersectBrush +//=========================================================================== +// Returns true if the two brushes definately do not intersect. +// There will be false negatives for some non-axial combinations. +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean BrushesDisjoint( bspbrush_t *a, bspbrush_t *b ) { + int i, j; + + // check bounding boxes + for ( i = 0 ; i < 3 ; i++ ) + if ( a->mins[i] >= b->maxs[i] + || a->maxs[i] <= b->mins[i] ) { + return true; + } // bounding boxes don't overlap + + // check for opposing planes + for ( i = 0 ; i < a->numsides ; i++ ) + { + for ( j = 0 ; j < b->numsides ; j++ ) + { + if ( a->sides[i].planenum == + ( b->sides[j].planenum ^ 1 ) ) { + return true; // opposite planes, so not touching + } + } + } + + return false; // might intersect +} //end of the function BrushesDisjoint +//=========================================================================== +// Returns a content word for the intersection of two brushes. +// Some combinations will generate a combination (water + clip), +// but most will be the stronger of the two contents. +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int IntersectionContents( int c1, int c2 ) { + int out; + + out = c1 | c2; + + if ( out & CONTENTS_SOLID ) { + out = CONTENTS_SOLID; + } + + return out; +} //end of the function IntersectionContents +//=========================================================================== +// Any planes shared with the box edge will be set to no texinfo +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *ClipBrushToBox( bspbrush_t *brush, vec3_t clipmins, vec3_t clipmaxs ) { + int i, j; + bspbrush_t *front, *back; + int p; + + for ( j = 0 ; j < 2 ; j++ ) + { + if ( brush->maxs[j] > clipmaxs[j] ) { + SplitBrush( brush, maxplanenums[j], &front, &back ); + if ( front ) { + FreeBrush( front ); + } + brush = back; + if ( !brush ) { + return NULL; + } + } + if ( brush->mins[j] < clipmins[j] ) { + SplitBrush( brush, minplanenums[j], &front, &back ); + if ( back ) { + FreeBrush( back ); + } + brush = front; + if ( !brush ) { + return NULL; + } + } + } + + // remove any colinear faces + + for ( i = 0 ; i < brush->numsides ; i++ ) + { + p = brush->sides[i].planenum & ~1; + if ( p == maxplanenums[0] || p == maxplanenums[1] + || p == minplanenums[0] || p == minplanenums[1] ) { + brush->sides[i].texinfo = TEXINFO_NODE; + brush->sides[i].flags &= ~SFL_VISIBLE; + } + } + return brush; +} //end of the function ClipBrushToBox +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *MakeBspBrushList( int startbrush, int endbrush, + vec3_t clipmins, vec3_t clipmaxs ) { + mapbrush_t *mb; + bspbrush_t *brushlist, *newbrush; + int i, j; + int c_faces; + int c_brushes; + int numsides; + int vis; + vec3_t normal; + float dist; + + for ( i = 0 ; i < 2 ; i++ ) + { + VectorClear( normal ); + normal[i] = 1; + dist = clipmaxs[i]; + maxplanenums[i] = FindFloatPlane( normal, dist ); + dist = clipmins[i]; + minplanenums[i] = FindFloatPlane( normal, dist ); + } + + brushlist = NULL; + c_faces = 0; + c_brushes = 0; + + for ( i = startbrush ; i < endbrush ; i++ ) + { + mb = &mapbrushes[i]; + + numsides = mb->numsides; + if ( !numsides ) { + continue; + } + + // make sure the brush has at least one face showing + vis = 0; + for ( j = 0 ; j < numsides ; j++ ) + if ( ( mb->original_sides[j].flags & SFL_VISIBLE ) && mb->original_sides[j].winding ) { + vis++; + } +#if 0 + if ( !vis ) { + continue; // no faces at all + } +#endif + // if the brush is outside the clip area, skip it + for ( j = 0 ; j < 3 ; j++ ) + if ( mb->mins[j] >= clipmaxs[j] + || mb->maxs[j] <= clipmins[j] ) { + break; + } + if ( j != 3 ) { + continue; + } + + // + // make a copy of the brush + // + newbrush = AllocBrush( mb->numsides ); + newbrush->original = mb; + newbrush->numsides = mb->numsides; + memcpy( newbrush->sides, mb->original_sides, numsides * sizeof( side_t ) ); + for ( j = 0 ; j < numsides ; j++ ) + { + if ( newbrush->sides[j].winding ) { + newbrush->sides[j].winding = CopyWinding( newbrush->sides[j].winding ); + } + if ( newbrush->sides[j].surf & SURF_HINT ) { + newbrush->sides[j].flags |= SFL_VISIBLE; // hints are always visible + } + } + VectorCopy( mb->mins, newbrush->mins ); + VectorCopy( mb->maxs, newbrush->maxs ); + + // + // carve off anything outside the clip box + // + newbrush = ClipBrushToBox( newbrush, clipmins, clipmaxs ); + if ( !newbrush ) { + continue; + } + + c_faces += vis; + c_brushes++; + + newbrush->next = brushlist; + brushlist = newbrush; + } + + return brushlist; +} //end of the function MakeBspBrushList +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *AddBrushListToTail( bspbrush_t *list, bspbrush_t *tail ) { + bspbrush_t *walk, *next; + + for ( walk = list ; walk ; walk = next ) + { // add to end of list + next = walk->next; + walk->next = NULL; + tail->next = walk; + tail = walk; + } //end for + return tail; +} //end of the function AddBrushListToTail +//=========================================================================== +// Builds a new list that doesn't hold the given brush +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *CullList( bspbrush_t *list, bspbrush_t *skip1 ) { + bspbrush_t *newlist; + bspbrush_t *next; + + newlist = NULL; + + for ( ; list ; list = next ) + { + next = list->next; + if ( list == skip1 ) { + FreeBrush( list ); + continue; + } + list->next = newlist; + newlist = list; + } + return newlist; +} //end of the function CullList +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +/* +void WriteBrushMap(char *name, bspbrush_t *list) +{ + FILE *f; + side_t *s; + int i; + winding_t *w; + + Log_Print("writing %s\n", name); + f = fopen (name, "wb"); + if (!f) + Error ("Can't write %s\b", name); + + fprintf (f, "{\n\"classname\" \"worldspawn\"\n"); + + for ( ; list ; list=list->next ) + { + fprintf (f, "{\n"); + for (i=0,s=list->sides ; inumsides ; i++,s++) + { + w = BaseWindingForPlane (mapplanes[s->planenum].normal, mapplanes[s->planenum].dist); + + fprintf (f,"( %i %i %i ) ", (int)w->p[0][0], (int)w->p[0][1], (int)w->p[0][2]); + fprintf (f,"( %i %i %i ) ", (int)w->p[1][0], (int)w->p[1][1], (int)w->p[1][2]); + fprintf (f,"( %i %i %i ) ", (int)w->p[2][0], (int)w->p[2][1], (int)w->p[2][2]); + + fprintf (f, "%s 0 0 0 1 1\n", texinfo[s->texinfo].texture); + FreeWinding (w); + } + fprintf (f, "}\n"); + } + fprintf (f, "}\n"); + + fclose (f); +} //end of the function WriteBrushMap +*/ +//=========================================================================== +// Returns true if b1 is allowed to bite b2 +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean BrushGE( bspbrush_t *b1, bspbrush_t *b2 ) { +#ifdef ME + if ( create_aas ) { + if ( b1->original->expansionbbox != b2->original->expansionbbox ) { + return false; + } //end if + //never have something else bite a ladder brush + //never have a ladder brush bite something else + if ( ( b1->original->contents & CONTENTS_LADDER ) + && !( b2->original->contents & CONTENTS_LADDER ) ) { + return false; + } //end if + } //end if +#endif //ME + // detail brushes never bite structural brushes + if ( ( b1->original->contents & CONTENTS_DETAIL ) + && !( b2->original->contents & CONTENTS_DETAIL ) ) { + return false; + } //end if + if ( b1->original->contents & CONTENTS_SOLID ) { + return true; + } //end if + return false; +} //end of the function BrushGE +//=========================================================================== +// Carves any intersecting solid brushes into the minimum number +// of non-intersecting brushes. +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *ChopBrushes( bspbrush_t *head ) { + bspbrush_t *b1, *b2, *next; + bspbrush_t *tail; + bspbrush_t *keep; + bspbrush_t *sub, *sub2; + int c1, c2; + int num_csg_iterations; + + Log_Print( "-------- Brush CSG ---------\n" ); + Log_Print( "%6d original brushes\n", CountBrushList( head ) ); + + num_csg_iterations = 0; + qprintf( "%6d output brushes", num_csg_iterations ); + +#if 0 + if ( startbrush == 0 ) { + WriteBrushList( "before.gl", head, false ); + } +#endif + keep = NULL; + +newlist: + // find tail + if ( !head ) { + return NULL; + } + + for ( tail = head; tail->next; tail = tail->next ) + ; + + for ( b1 = head ; b1 ; b1 = next ) + { + next = b1->next; + + //if the conversion is cancelled + if ( cancelconversion ) { + b1->next = keep; + keep = b1; + continue; + } //end if + + for ( b2 = b1->next; b2; b2 = b2->next ) + { + if ( BrushesDisjoint( b1, b2 ) ) { + continue; + } + + sub = NULL; + sub2 = NULL; + c1 = 999999; + c2 = 999999; + + if ( BrushGE( b2, b1 ) ) { + sub = SubtractBrush( b1, b2 ); + if ( sub == b1 ) { + continue; // didn't really intersect + } //end if + if ( !sub ) { // b1 is swallowed by b2 + head = CullList( b1, b1 ); + goto newlist; + } + c1 = CountBrushList( sub ); + } + + if ( BrushGE( b1, b2 ) ) { + sub2 = SubtractBrush( b2, b1 ); + if ( sub2 == b2 ) { + continue; // didn't really intersect + } + if ( !sub2 ) { // b2 is swallowed by b1 + FreeBrushList( sub ); + head = CullList( b1, b2 ); + goto newlist; + } + c2 = CountBrushList( sub2 ); + } + + if ( !sub && !sub2 ) { + continue; // neither one can bite + + } + // only accept if it didn't fragment + // (commenting this out allows full fragmentation) + if ( c1 > 1 && c2 > 1 ) { + if ( sub2 ) { + FreeBrushList( sub2 ); + } + if ( sub ) { + FreeBrushList( sub ); + } + continue; + } + + if ( c1 < c2 ) { + if ( sub2 ) { + FreeBrushList( sub2 ); + } + tail = AddBrushListToTail( sub, tail ); + head = CullList( b1, b1 ); + goto newlist; + } //end if + else + { + if ( sub ) { + FreeBrushList( sub ); + } + tail = AddBrushListToTail( sub2, tail ); + head = CullList( b1, b2 ); + goto newlist; + } //end else + } //end for + + if ( !b2 ) { // b1 is no longer intersecting anything, so keep it + b1->next = keep; + keep = b1; + } //end if + num_csg_iterations++; + qprintf( "\r%6d", num_csg_iterations ); + } //end for + + if ( cancelconversion ) { + return keep; + } + // + qprintf( "\n" ); + Log_Write( "%6d output brushes\r\n", num_csg_iterations ); + +#if 0 + { + WriteBrushList( "after.gl", keep, false ); + WriteBrushMap( "after.map", keep ); + } +#endif + + return keep; +} //end of the function ChopBrushes +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *InitialBrushList( bspbrush_t *list ) { + bspbrush_t *b; + bspbrush_t *out, *newb; + int i; + + // only return brushes that have visible faces + out = NULL; + for ( b = list ; b ; b = b->next ) + { +#if 0 + for ( i = 0 ; i < b->numsides ; i++ ) + if ( b->sides[i].flags & SFL_VISIBLE ) { + break; + } + if ( i == b->numsides ) { + continue; + } +#endif + newb = CopyBrush( b ); + newb->next = out; + out = newb; + + // clear visible, so it must be set by MarkVisibleFaces_r + // to be used in the optimized list + for ( i = 0 ; i < b->numsides ; i++ ) + { + newb->sides[i].original = &b->sides[i]; +// newb->sides[i].visible = true; + b->sides[i].flags &= ~SFL_VISIBLE; + } + } + + return out; +} //end of the function InitialBrushList +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *OptimizedBrushList( bspbrush_t *list ) { + bspbrush_t *b; + bspbrush_t *out, *newb; + int i; + + // only return brushes that have visible faces + out = NULL; + for ( b = list ; b ; b = b->next ) + { + for ( i = 0 ; i < b->numsides ; i++ ) + if ( b->sides[i].flags & SFL_VISIBLE ) { + break; + } + if ( i == b->numsides ) { + continue; + } + newb = CopyBrush( b ); + newb->next = out; + out = newb; + } //end for + +// WriteBrushList ("vis.gl", out, true); + return out; +} //end of the function OptimizeBrushList +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +tree_t *ProcessWorldBrushes( int brush_start, int brush_end ) { + bspbrush_t *brushes; + tree_t *tree; + node_t *node; + vec3_t mins, maxs; + + //take the whole world + mins[0] = map_mins[0] - 8; + mins[1] = map_mins[1] - 8; + mins[2] = map_mins[2] - 8; + + maxs[0] = map_maxs[0] + 8; + maxs[1] = map_maxs[1] + 8; + maxs[2] = map_maxs[2] + 8; + + //reset the brush bsp + ResetBrushBSP(); + + // the makelist and chopbrushes could be cached between the passes... + + //create a list with brushes that are within the given mins/maxs + //some brushes will be cut and only the part that falls within the + //mins/maxs will be in the bush list + brushes = MakeBspBrushList( brush_start, brush_end, mins, maxs ); + // + + if ( !brushes ) { + node = AllocNode(); + node->planenum = PLANENUM_LEAF; + node->contents = CONTENTS_SOLID; + + tree = Tree_Alloc(); + tree->headnode = node; + VectorCopy( mins, tree->mins ); + VectorCopy( maxs, tree->maxs ); + } //end if + else + { + //Carves any intersecting solid brushes into the minimum number + //of non-intersecting brushes. + if ( !nocsg ) { + brushes = ChopBrushes( brushes ); + /* + if (create_aas) + { + brushes = MergeBrushes(brushes); + } //end if*/ + } //end if + //if the conversion is cancelled + if ( cancelconversion ) { + FreeBrushList( brushes ); + return NULL; + } //end if + //create the actual bsp tree + tree = BrushBSP( brushes, mins, maxs ); + } //end else + //return the tree + return tree; +} //end of the function ProcessWorldBrushes diff --git a/src/bspc/faces.c b/src/bspc/faces.c new file mode 100644 index 0000000..279e10f --- /dev/null +++ b/src/bspc/faces.c @@ -0,0 +1,1002 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// NO LONGER USED +// faces.c +#if 0 +#include "qbsp.h" +#include "l_mem.h" + + +extern dvertex_t *dvertexes; +extern int numvertexes; +extern int numedges; +extern dedge_t *dedges; +extern texinfo_t texinfo[MAX_MAP_TEXINFO]; + +/* + + some faces will be removed before saving, but still form nodes: + + the insides of sky volumes + meeting planes of different water current volumes + +*/ + +// undefine for dumb linear searches +#define USE_HASHING + +#define INTEGRAL_EPSILON 0.01 +#define POINT_EPSILON 0.5 +#define OFF_EPSILON 0.5 + +int c_merge; +int c_subdivide; + +int c_totalverts; +int c_uniqueverts; +int c_degenerate; +int c_tjunctions; +int c_faceoverflows; +int c_facecollapse; +int c_badstartverts; + +#define MAX_SUPERVERTS 512 +int superverts[MAX_SUPERVERTS]; +int numsuperverts; + +face_t *edgefaces[MAX_MAP_EDGES][2]; +int firstmodeledge = 1; +int firstmodelface; + +int c_tryedges; + +vec3_t edge_dir; +vec3_t edge_start; +vec_t edge_len; + +int num_edge_verts; +int edge_verts[MAX_MAP_VERTS]; + + +face_t *NewFaceFromFace( face_t *f ); + +//=========================================================================== + +typedef struct hashvert_s +{ + struct hashvert_s *next; + int num; +} hashvert_t; + + +#define HASH_SIZE 64 + + +int vertexchain[MAX_MAP_VERTS]; // the next vertex in a hash chain +int hashverts[HASH_SIZE * HASH_SIZE]; // a vertex number, or 0 for no verts + +face_t *edgefaces[MAX_MAP_EDGES][2]; + +//============================================================================ + + +unsigned HashVec( vec3_t vec ) { + int x, y; + + x = ( 4096 + (int)( vec[0] + 0.5 ) ) >> 7; + y = ( 4096 + (int)( vec[1] + 0.5 ) ) >> 7; + + if ( x < 0 || x >= HASH_SIZE || y < 0 || y >= HASH_SIZE ) { + Error( "HashVec: point outside valid range" ); + } + + return y * HASH_SIZE + x; +} + +#ifdef USE_HASHING +/* +============= +GetVertex + +Uses hashing +============= +*/ +int GetVertexnum( vec3_t in ) { + int h; + int i; + float *p; + vec3_t vert; + int vnum; + + c_totalverts++; + + for ( i = 0 ; i < 3 ; i++ ) + { + if ( fabs( in[i] - Q_rint( in[i] ) ) < INTEGRAL_EPSILON ) { + vert[i] = Q_rint( in[i] ); + } else { + vert[i] = in[i]; + } + } + + h = HashVec( vert ); + + for ( vnum = hashverts[h] ; vnum ; vnum = vertexchain[vnum] ) + { + p = dvertexes[vnum].point; + if ( fabs( p[0] - vert[0] ) < POINT_EPSILON + && fabs( p[1] - vert[1] ) < POINT_EPSILON + && fabs( p[2] - vert[2] ) < POINT_EPSILON ) { + return vnum; + } + } + +// emit a vertex + if ( numvertexes == MAX_MAP_VERTS ) { + Error( "numvertexes == MAX_MAP_VERTS" ); + } + + dvertexes[numvertexes].point[0] = vert[0]; + dvertexes[numvertexes].point[1] = vert[1]; + dvertexes[numvertexes].point[2] = vert[2]; + + vertexchain[numvertexes] = hashverts[h]; + hashverts[h] = numvertexes; + + c_uniqueverts++; + + numvertexes++; + + return numvertexes - 1; +} +#else +/* +================== +GetVertexnum + +Dumb linear search +================== +*/ +int GetVertexnum( vec3_t v ) { + int i, j; + dvertex_t *dv; + vec_t d; + + c_totalverts++; + + // make really close values exactly integral + for ( i = 0 ; i < 3 ; i++ ) + { + if ( fabs( v[i] - (int)( v[i] + 0.5 ) ) < INTEGRAL_EPSILON ) { + v[i] = (int)( v[i] + 0.5 ); + } + if ( v[i] < -4096 || v[i] > 4096 ) { + Error( "GetVertexnum: outside +/- 4096" ); + } + } + + // search for an existing vertex match + for ( i = 0, dv = dvertexes ; i < numvertexes ; i++, dv++ ) + { + for ( j = 0 ; j < 3 ; j++ ) + { + d = v[j] - dv->point[j]; + if ( d > POINT_EPSILON || d < -POINT_EPSILON ) { + break; + } + } + if ( j == 3 ) { + return i; // a match + } + } + + // new point + if ( numvertexes == MAX_MAP_VERTS ) { + Error( "MAX_MAP_VERTS" ); + } + VectorCopy( v, dv->point ); + numvertexes++; + c_uniqueverts++; + + return numvertexes - 1; +} +#endif + + +/* +================== +FaceFromSuperverts + +The faces vertexes have been added to the superverts[] array, +and there may be more there than can be held in a face (MAXEDGES). + +If less, the faces vertexnums[] will be filled in, otherwise +face will reference a tree of split[] faces until all of the +vertexnums can be added. + +superverts[base] will become face->vertexnums[0], and the others +will be circularly filled in. +================== +*/ +void FaceFromSuperverts( node_t *node, face_t *f, int base ) { + face_t *newf; + int remaining; + int i; + + remaining = numsuperverts; + while ( remaining > MAXEDGES ) + { // must split into two faces, because of vertex overload + c_faceoverflows++; + + newf = f->split[0] = NewFaceFromFace( f ); + newf = f->split[0]; + newf->next = node->faces; + node->faces = newf; + + newf->numpoints = MAXEDGES; + for ( i = 0 ; i < MAXEDGES ; i++ ) + newf->vertexnums[i] = superverts[( i + base ) % numsuperverts]; + + f->split[1] = NewFaceFromFace( f ); + f = f->split[1]; + f->next = node->faces; + node->faces = f; + + remaining -= ( MAXEDGES - 2 ); + base = ( base + MAXEDGES - 1 ) % numsuperverts; + } + + // copy the vertexes back to the face + f->numpoints = remaining; + for ( i = 0 ; i < remaining ; i++ ) + f->vertexnums[i] = superverts[( i + base ) % numsuperverts]; +} + + +/* +================== +EmitFaceVertexes +================== +*/ +void EmitFaceVertexes( node_t *node, face_t *f ) { + winding_t *w; + int i; + + if ( f->merged || f->split[0] || f->split[1] ) { + return; + } + + w = f->w; + for ( i = 0 ; i < w->numpoints ; i++ ) + { + if ( noweld ) { // make every point unique + if ( numvertexes == MAX_MAP_VERTS ) { + Error( "MAX_MAP_VERTS" ); + } + superverts[i] = numvertexes; + VectorCopy( w->p[i], dvertexes[numvertexes].point ); + numvertexes++; + c_uniqueverts++; + c_totalverts++; + } else { + superverts[i] = GetVertexnum( w->p[i] ); + } + } + numsuperverts = w->numpoints; + + // this may fragment the face if > MAXEDGES + FaceFromSuperverts( node, f, 0 ); +} + +/* +================== +EmitVertexes_r +================== +*/ +void EmitVertexes_r( node_t *node ) { + int i; + face_t *f; + + if ( node->planenum == PLANENUM_LEAF ) { + return; + } + + for ( f = node->faces ; f ; f = f->next ) + { + EmitFaceVertexes( node, f ); + } + + for ( i = 0 ; i < 2 ; i++ ) + EmitVertexes_r( node->children[i] ); +} + + +#ifdef USE_HASHING +/* +========== +FindEdgeVerts + +Uses the hash tables to cut down to a small number +========== +*/ +void FindEdgeVerts( vec3_t v1, vec3_t v2 ) { + int x1, x2, y1, y2, t; + int x, y; + int vnum; + +#if 0 + { + int i; + num_edge_verts = numvertexes - 1; + for ( i = 0 ; i < numvertexes - 1 ; i++ ) + edge_verts[i] = i + 1; + } +#endif + + x1 = ( 4096 + (int)( v1[0] + 0.5 ) ) >> 7; + y1 = ( 4096 + (int)( v1[1] + 0.5 ) ) >> 7; + x2 = ( 4096 + (int)( v2[0] + 0.5 ) ) >> 7; + y2 = ( 4096 + (int)( v2[1] + 0.5 ) ) >> 7; + + if ( x1 > x2 ) { + t = x1; + x1 = x2; + x2 = t; + } + if ( y1 > y2 ) { + t = y1; + y1 = y2; + y2 = t; + } +#if 0 + x1--; + x2++; + y1--; + y2++; + if ( x1 < 0 ) { + x1 = 0; + } + if ( x2 >= HASH_SIZE ) { + x2 = HASH_SIZE; + } + if ( y1 < 0 ) { + y1 = 0; + } + if ( y2 >= HASH_SIZE ) { + y2 = HASH_SIZE; + } +#endif + num_edge_verts = 0; + for ( x = x1 ; x <= x2 ; x++ ) + { + for ( y = y1 ; y <= y2 ; y++ ) + { + for ( vnum = hashverts[y * HASH_SIZE + x] ; vnum ; vnum = vertexchain[vnum] ) + { + edge_verts[num_edge_verts++] = vnum; + } + } + } +} + +#else +/* +========== +FindEdgeVerts + +Forced a dumb check of everything +========== +*/ +void FindEdgeVerts( vec3_t v1, vec3_t v2 ) { + int i; + + num_edge_verts = numvertexes - 1; + for ( i = 0 ; i < num_edge_verts ; i++ ) + edge_verts[i] = i + 1; +} +#endif + +/* +========== +TestEdge + +Can be recursively reentered +========== +*/ +void TestEdge( vec_t start, vec_t end, int p1, int p2, int startvert ) { + int j, k; + vec_t dist; + vec3_t delta; + vec3_t exact; + vec3_t off; + vec_t error; + vec3_t p; + + if ( p1 == p2 ) { + c_degenerate++; + return; // degenerate edge + } + + for ( k = startvert ; k < num_edge_verts ; k++ ) + { + j = edge_verts[k]; + if ( j == p1 || j == p2 ) { + continue; + } + + VectorCopy( dvertexes[j].point, p ); + + VectorSubtract( p, edge_start, delta ); + dist = DotProduct( delta, edge_dir ); + if ( dist <= start || dist >= end ) { + continue; // off an end + } + VectorMA( edge_start, dist, edge_dir, exact ); + VectorSubtract( p, exact, off ); + error = VectorLength( off ); + + if ( fabs( error ) > OFF_EPSILON ) { + continue; // not on the edge + + } + // break the edge + c_tjunctions++; + TestEdge( start, dist, p1, j, k + 1 ); + TestEdge( dist, end, j, p2, k + 1 ); + return; + } + + // the edge p1 to p2 is now free of tjunctions + if ( numsuperverts >= MAX_SUPERVERTS ) { + Error( "MAX_SUPERVERTS" ); + } + superverts[numsuperverts] = p1; + numsuperverts++; +} + +/* +================== +FixFaceEdges + +================== +*/ +void FixFaceEdges( node_t *node, face_t *f ) { + int p1, p2; + int i; + vec3_t e2; + vec_t len; + int count[MAX_SUPERVERTS], start[MAX_SUPERVERTS]; + int base; + + if ( f->merged || f->split[0] || f->split[1] ) { + return; + } + + numsuperverts = 0; + + for ( i = 0 ; i < f->numpoints ; i++ ) + { + p1 = f->vertexnums[i]; + p2 = f->vertexnums[( i + 1 ) % f->numpoints]; + + VectorCopy( dvertexes[p1].point, edge_start ); + VectorCopy( dvertexes[p2].point, e2 ); + + FindEdgeVerts( edge_start, e2 ); + + VectorSubtract( e2, edge_start, edge_dir ); + len = VectorNormalize( edge_dir ); + + start[i] = numsuperverts; + TestEdge( 0, len, p1, p2, 0 ); + + count[i] = numsuperverts - start[i]; + } + + if ( numsuperverts < 3 ) { // entire face collapsed + f->numpoints = 0; + c_facecollapse++; + return; + } + + // we want to pick a vertex that doesn't have tjunctions + // on either side, which can cause artifacts on trifans, + // especially underwater + for ( i = 0 ; i < f->numpoints ; i++ ) + { + if ( count[i] == 1 && count[( i + f->numpoints - 1 ) % f->numpoints] == 1 ) { + break; + } + } + if ( i == f->numpoints ) { + f->badstartvert = true; + c_badstartverts++; + base = 0; + } else + { // rotate the vertex order + base = start[i]; + } + + // this may fragment the face if > MAXEDGES + FaceFromSuperverts( node, f, base ); +} + +/* +================== +FixEdges_r +================== +*/ +void FixEdges_r( node_t *node ) { + int i; + face_t *f; + + if ( node->planenum == PLANENUM_LEAF ) { + return; + } + + for ( f = node->faces ; f ; f = f->next ) + FixFaceEdges( node, f ); + + for ( i = 0 ; i < 2 ; i++ ) + FixEdges_r( node->children[i] ); +} + +/* +=========== +FixTjuncs + +=========== +*/ +void FixTjuncs( node_t *headnode ) { + // snap and merge all vertexes + qprintf( "---- snap verts ----\n" ); + memset( hashverts, 0, sizeof( hashverts ) ); + c_totalverts = 0; + c_uniqueverts = 0; + c_faceoverflows = 0; + EmitVertexes_r( headnode ); + qprintf( "%i unique from %i\n", c_uniqueverts, c_totalverts ); + + // break edges on tjunctions + qprintf( "---- tjunc ----\n" ); + c_tryedges = 0; + c_degenerate = 0; + c_facecollapse = 0; + c_tjunctions = 0; + if ( !notjunc ) { + FixEdges_r( headnode ); + } + qprintf( "%5i edges degenerated\n", c_degenerate ); + qprintf( "%5i faces degenerated\n", c_facecollapse ); + qprintf( "%5i edges added by tjunctions\n", c_tjunctions ); + qprintf( "%5i faces added by tjunctions\n", c_faceoverflows ); + qprintf( "%5i bad start verts\n", c_badstartverts ); +} + + +//======================================================== + +int c_faces; + +face_t *AllocFace( void ) { + face_t *f; + + f = GetMemory( sizeof( *f ) ); + memset( f, 0, sizeof( *f ) ); + c_faces++; + + return f; +} + +face_t *NewFaceFromFace( face_t *f ) { + face_t *newf; + + newf = AllocFace(); + *newf = *f; + newf->merged = NULL; + newf->split[0] = newf->split[1] = NULL; + newf->w = NULL; + return newf; +} + +void FreeFace( face_t *f ) { + if ( f->w ) { + FreeWinding( f->w ); + } + FreeMemory( f ); + c_faces--; +} + +//======================================================== + +/* +================== +GetEdge + +Called by writebsp. +Don't allow four way edges +================== +*/ +int GetEdge2( int v1, int v2, face_t *f ) { + dedge_t *edge; + int i; + + c_tryedges++; + + if ( !noshare ) { + for ( i = firstmodeledge ; i < numedges ; i++ ) + { + edge = &dedges[i]; + if ( v1 == edge->v[1] && v2 == edge->v[0] + && edgefaces[i][0]->contents == f->contents ) { + if ( edgefaces[i][1] ) { + // printf ("WARNING: multiple backward edge\n"); + continue; + } + edgefaces[i][1] = f; + return -i; + } + #if 0 + if ( v1 == edge->v[0] && v2 == edge->v[1] ) { + printf( "WARNING: multiple forward edge\n" ); + return i; + } + #endif + } + } + +// emit an edge + if ( numedges >= MAX_MAP_EDGES ) { + Error( "numedges == MAX_MAP_EDGES" ); + } + edge = &dedges[numedges]; + numedges++; + edge->v[0] = v1; + edge->v[1] = v2; + edgefaces[numedges - 1][0] = f; + + return numedges - 1; +} + +/* +=========================================================================== + +FACE MERGING + +=========================================================================== +*/ + +/* +============= +TryMerge + +If two polygons share a common edge and the edges that meet at the +common points are both inside the other polygons, merge them + +Returns NULL if the faces couldn't be merged, or the new face. +The originals will NOT be freed. +============= +*/ +face_t *TryMerge( face_t *f1, face_t *f2, vec3_t planenormal ) { + face_t *newf; + winding_t *nw; + + if ( !f1->w || !f2->w ) { + return NULL; + } + if ( f1->texinfo != f2->texinfo ) { + return NULL; + } + if ( f1->planenum != f2->planenum ) { // on front and back sides + return NULL; + } + if ( f1->contents != f2->contents ) { + return NULL; + } + + + nw = TryMergeWinding( f1->w, f2->w, planenormal ); + if ( !nw ) { + return NULL; + } + + c_merge++; + newf = NewFaceFromFace( f1 ); + newf->w = nw; + + f1->merged = newf; + f2->merged = newf; + + return newf; +} + +/* +=============== +MergeNodeFaces +=============== +*/ +void MergeNodeFaces( node_t *node ) { + face_t *f1, *f2, *end; + face_t *merged; + plane_t *plane; + + plane = &mapplanes[node->planenum]; + merged = NULL; + + for ( f1 = node->faces ; f1 ; f1 = f1->next ) + { + if ( f1->merged || f1->split[0] || f1->split[1] ) { + continue; + } + + for ( f2 = node->faces ; f2 != f1 ; f2 = f2->next ) + { + if ( f2->merged || f2->split[0] || f2->split[1] ) { + continue; + } + + //IDBUG: always passes the face's node's normal to TryMerge() + //regardless of which side the face is on. Approximately 50% of + //the time the face will be on the other side of node, and thus + //the result of the convex/concave test in TryMergeWinding(), + //which depends on the normal, is flipped. This causes faces + //that shouldn't be merged to be merged and faces that + //should be merged to not be merged. + //the following added line fixes this bug + //thanks to: Alexander Malmberg + plane = &mapplanes[f1->planenum]; + // + merged = TryMerge( f1, f2, plane->normal ); + if ( !merged ) { + continue; + } + + // add merged to the end of the node face list + // so it will be checked against all the faces again + for ( end = node->faces ; end->next ; end = end->next ) + ; + merged->next = NULL; + end->next = merged; + break; + } + } +} + +//===================================================================== + +/* +=============== +SubdivideFace + +Chop up faces that are larger than we want in the surface cache +=============== +*/ +void SubdivideFace( node_t *node, face_t *f ) { + float mins, maxs; + vec_t v; + int axis, i; + texinfo_t *tex; + vec3_t temp; + vec_t dist; + winding_t *w, *frontw, *backw; + + if ( f->merged ) { + return; + } + +// special (non-surface cached) faces don't need subdivision + tex = &texinfo[f->texinfo]; + + if ( tex->flags & ( SURF_WARP | SURF_SKY ) ) { + return; + } + + for ( axis = 0 ; axis < 2 ; axis++ ) + { + while ( 1 ) + { + mins = 999999; + maxs = -999999; + + VectorCopy( tex->vecs[axis], temp ); + w = f->w; + for ( i = 0 ; i < w->numpoints ; i++ ) + { + v = DotProduct( w->p[i], temp ); + if ( v < mins ) { + mins = v; + } + if ( v > maxs ) { + maxs = v; + } + } +#if 0 + if ( maxs - mins <= 0 ) { + Error( "zero extents" ); + } +#endif + if ( axis == 2 ) { // allow double high walls + if ( maxs - mins <= subdivide_size /* *2 */ ) { + break; + } + } else if ( maxs - mins <= subdivide_size ) { + break; + } + + // split it + c_subdivide++; + + v = VectorNormalize( temp ); + + dist = ( mins + subdivide_size - 16 ) / v; + + ClipWindingEpsilon( w, temp, dist, ON_EPSILON, &frontw, &backw ); + if ( !frontw || !backw ) { + Error( "SubdivideFace: didn't split the polygon" ); + } + + f->split[0] = NewFaceFromFace( f ); + f->split[0]->w = frontw; + f->split[0]->next = node->faces; + node->faces = f->split[0]; + + f->split[1] = NewFaceFromFace( f ); + f->split[1]->w = backw; + f->split[1]->next = node->faces; + node->faces = f->split[1]; + + SubdivideFace( node, f->split[0] ); + SubdivideFace( node, f->split[1] ); + return; + } + } +} + +void SubdivideNodeFaces( node_t *node ) { + face_t *f; + + for ( f = node->faces ; f ; f = f->next ) + { + SubdivideFace( node, f ); + } +} + +//=========================================================================== + +int c_nodefaces; + + +/* +============ +FaceFromPortal + +============ +*/ +face_t *FaceFromPortal( portal_t *p, int pside ) { + face_t *f; + side_t *side; + + side = p->side; + if ( !side ) { + return NULL; // portal does not bridge different visible contents + + } + f = AllocFace(); + + f->texinfo = side->texinfo; + f->planenum = ( side->planenum & ~1 ) | pside; + f->portal = p; + + if ( ( p->nodes[pside]->contents & CONTENTS_WINDOW ) + && VisibleContents( p->nodes[!pside]->contents ^ p->nodes[pside]->contents ) == CONTENTS_WINDOW ) { + return NULL; // don't show insides of windows + + } + if ( pside ) { + f->w = ReverseWinding( p->winding ); + f->contents = p->nodes[1]->contents; + } else + { + f->w = CopyWinding( p->winding ); + f->contents = p->nodes[0]->contents; + } + return f; +} + + +/* +=============== +MakeFaces_r + +If a portal will make a visible face, +mark the side that originally created it + + solid / empty : solid + solid / water : solid + water / empty : water + water / water : none +=============== +*/ +void MakeFaces_r( node_t *node ) { + portal_t *p; + int s; + + // recurse down to leafs + if ( node->planenum != PLANENUM_LEAF ) { + MakeFaces_r( node->children[0] ); + MakeFaces_r( node->children[1] ); + + // merge together all visible faces on the node + if ( !nomerge ) { + MergeNodeFaces( node ); + } + if ( !nosubdiv ) { + SubdivideNodeFaces( node ); + } + + return; + } + + // solid leafs never have visible faces + if ( node->contents & CONTENTS_SOLID ) { + return; + } + + // see which portals are valid + for ( p = node->portals ; p ; p = p->next[s] ) + { + s = ( p->nodes[1] == node ); + + p->face[s] = FaceFromPortal( p, s ); + if ( p->face[s] ) { + c_nodefaces++; + p->face[s]->next = p->onnode->faces; + p->onnode->faces = p->face[s]; + } + } +} + +/* +============ +MakeFaces +============ +*/ +void MakeFaces( node_t *node ) { + qprintf( "--- MakeFaces ---\n" ); + c_merge = 0; + c_subdivide = 0; + c_nodefaces = 0; + + MakeFaces_r( node ); + + qprintf( "%5i makefaces\n", c_nodefaces ); + qprintf( "%5i merged\n", c_merge ); + qprintf( "%5i subdivided\n", c_subdivide ); +} +#endif \ No newline at end of file diff --git a/src/bspc/glfile.c b/src/bspc/glfile.c new file mode 100644 index 0000000..faf4717 --- /dev/null +++ b/src/bspc/glfile.c @@ -0,0 +1,157 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +#include "qbsp.h" + +int c_glfaces; + +int PortalVisibleSides( portal_t *p ) { + int fcon, bcon; + + if ( !p->onnode ) { + return 0; // outside + + } + fcon = p->nodes[0]->contents; + bcon = p->nodes[1]->contents; + + // same contents never create a face + if ( fcon == bcon ) { + return 0; + } + + // FIXME: is this correct now? + if ( !fcon ) { + return 1; + } + if ( !bcon ) { + return 2; + } + return 0; +} + +void OutputWinding( winding_t *w, FILE *glview ) { + static int level = 128; + vec_t light; + int i; + + fprintf( glview, "%i\n", w->numpoints ); + level += 28; + light = ( level & 255 ) / 255.0; + for ( i = 0 ; i < w->numpoints ; i++ ) + { + fprintf( glview, "%6.3f %6.3f %6.3f %6.3f %6.3f %6.3f\n", + w->p[i][0], + w->p[i][1], + w->p[i][2], + light, + light, + light ); + } + fprintf( glview, "\n" ); +} + +/* +============= +OutputPortal +============= +*/ +void OutputPortal( portal_t *p, FILE *glview ) { + winding_t *w; + int sides; + + sides = PortalVisibleSides( p ); + if ( !sides ) { + return; + } + + c_glfaces++; + + w = p->winding; + + if ( sides == 2 ) { // back side + w = ReverseWinding( w ); + } + + OutputWinding( w, glview ); + + if ( sides == 2 ) { + FreeWinding( w ); + } +} + +/* +============= +WriteGLView_r +============= +*/ +void WriteGLView_r( node_t *node, FILE *glview ) { + portal_t *p, *nextp; + + if ( node->planenum != PLANENUM_LEAF ) { + WriteGLView_r( node->children[0], glview ); + WriteGLView_r( node->children[1], glview ); + return; + } + + // write all the portals + for ( p = node->portals ; p ; p = nextp ) + { + if ( p->nodes[0] == node ) { + OutputPortal( p, glview ); + nextp = p->next[0]; + } else { + nextp = p->next[1]; + } + } +} + +/* +============= +WriteGLView +============= +*/ +void WriteGLView( tree_t *tree, char *source ) { + char name[1024]; + FILE *glview; + + c_glfaces = 0; + sprintf( name, "%s%s.gl",outbase, source ); + printf( "Writing %s\n", name ); + + glview = fopen( name, "w" ); + if ( !glview ) { + Error( "Couldn't open %s", name ); + } + WriteGLView_r( tree->headnode, glview ); + fclose( glview ); + + printf( "%5i c_glfaces\n", c_glfaces ); +} + diff --git a/src/bspc/l_bsp_ent.c b/src/bspc/l_bsp_ent.c new file mode 100644 index 0000000..2671cf5 --- /dev/null +++ b/src/bspc/l_bsp_ent.c @@ -0,0 +1,195 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: l_bsp_ent.c +// Function: bsp entity parsing +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1999-04-05 +// Tab Size: 3 +// Notes: - +//=========================================================================== + +#include "l_cmd.h" +#include "l_math.h" +#include "l_mem.h" +#include "l_log.h" +#include "../botlib/l_script.h" +#include "l_bsp_ent.h" + +#define MAX_KEY 32 +#define MAX_VALUE 1024 + +int num_entities; +entity_t entities[MAX_MAP_ENTITIES]; + +void StripTrailing( char *e ) { + char *s; + + s = e + strlen( e ) - 1; + while ( s >= e && *s <= 32 ) + { + *s = 0; + s--; + } +} + +/* +================= +ParseEpair +================= +*/ +epair_t *ParseEpair( script_t *script ) { + epair_t *e; + token_t token; + + e = GetMemory( sizeof( epair_t ) ); + memset( e, 0, sizeof( epair_t ) ); + + PS_ExpectAnyToken( script, &token ); + StripDoubleQuotes( token.string ); + if ( strlen( token.string ) >= MAX_KEY - 1 ) { + Error( "ParseEpair: token %s too long", token.string ); + } + e->key = copystring( token.string ); + PS_ExpectAnyToken( script, &token ); + StripDoubleQuotes( token.string ); + if ( strlen( token.string ) >= MAX_VALUE - 1 ) { + Error( "ParseEpair: token %s too long", token.string ); + } + e->value = copystring( token.string ); + + // strip trailing spaces + StripTrailing( e->key ); + StripTrailing( e->value ); + + return e; +} //end of the function ParseEpair + + +/* +================ +ParseEntity +================ +*/ +qboolean ParseEntity( script_t *script ) { + epair_t *e; + entity_t *mapent; + token_t token; + + if ( !PS_ReadToken( script, &token ) ) { + return false; + } + + if ( strcmp( token.string, "{" ) ) { + Error( "ParseEntity: { not found" ); + } + + if ( num_entities == MAX_MAP_ENTITIES ) { + Error( "num_entities == MAX_MAP_ENTITIES" ); + } + + mapent = &entities[num_entities]; + num_entities++; + + do + { + if ( !PS_ReadToken( script, &token ) ) { + Error( "ParseEntity: EOF without closing brace" ); + } + if ( !strcmp( token.string, "}" ) ) { + break; + } + PS_UnreadLastToken( script ); + e = ParseEpair( script ); + e->next = mapent->epairs; + mapent->epairs = e; + } while ( 1 ); + + return true; +} //end of the function ParseEntity + +void PrintEntity( entity_t *ent ) { + epair_t *ep; + + printf( "------- entity %p -------\n", ent ); + for ( ep = ent->epairs ; ep ; ep = ep->next ) + { + printf( "%s = %s\n", ep->key, ep->value ); + } + +} + +void SetKeyValue( entity_t *ent, char *key, char *value ) { + epair_t *ep; + + for ( ep = ent->epairs ; ep ; ep = ep->next ) + if ( !strcmp( ep->key, key ) ) { + FreeMemory( ep->value ); + ep->value = copystring( value ); + return; + } + ep = GetMemory( sizeof( *ep ) ); + ep->next = ent->epairs; + ent->epairs = ep; + ep->key = copystring( key ); + ep->value = copystring( value ); +} + +char *ValueForKey( entity_t *ent, char *key ) { + epair_t *ep; + + for ( ep = ent->epairs ; ep ; ep = ep->next ) + if ( !strcmp( ep->key, key ) ) { + return ep->value; + } + return ""; +} + +vec_t FloatForKey( entity_t *ent, char *key ) { + char *k; + + k = ValueForKey( ent, key ); + return atof( k ); +} + +void GetVectorForKey( entity_t *ent, char *key, vec3_t vec ) { + char *k; + double v1, v2, v3; + + k = ValueForKey( ent, key ); +// scanf into doubles, then assign, so it is vec_t size independent + v1 = v2 = v3 = 0; + sscanf( k, "%lf %lf %lf", &v1, &v2, &v3 ); + vec[0] = v1; + vec[1] = v2; + vec[2] = v3; +} + + diff --git a/src/bspc/l_bsp_ent.h b/src/bspc/l_bsp_ent.h new file mode 100644 index 0000000..42257a4 --- /dev/null +++ b/src/bspc/l_bsp_ent.h @@ -0,0 +1,65 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +#ifndef MAX_MAP_ENTITIES +#define MAX_MAP_ENTITIES 2048 +#endif + +typedef struct epair_s +{ + struct epair_s *next; + char *key; + char *value; +} epair_t; + +typedef struct +{ + vec3_t origin; + int firstbrush; + int numbrushes; + epair_t *epairs; + // only valid for func_areaportals + int areaportalnum; + int portalareas[2]; + int modelnum; //for bsp 2 map conversion + qboolean wasdetail; //for SIN +} entity_t; + +extern int num_entities; +extern entity_t entities[MAX_MAP_ENTITIES]; + +void StripTrailing( char *e ); +void SetKeyValue( entity_t *ent, char *key, char *value ); +char *ValueForKey( entity_t *ent, char *key ); // will return "" if not present +vec_t FloatForKey( entity_t *ent, char *key ); +void GetVectorForKey( entity_t *ent, char *key, vec3_t vec ); +qboolean ParseEntity( script_t *script ); +epair_t *ParseEpair( script_t *script ); +void PrintEntity( entity_t *ent ); + diff --git a/src/bspc/l_bsp_hl.c b/src/bspc/l_bsp_hl.c new file mode 100644 index 0000000..87eb171 --- /dev/null +++ b/src/bspc/l_bsp_hl.c @@ -0,0 +1,886 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +/*** +* +* Copyright (c) 1998, 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. +* +****/ + +#include "l_cmd.h" +#include "l_math.h" +#include "l_mem.h" +#include "l_log.h" +#include "../botlib/l_script.h" +#include "l_bsp_hl.h" +#include "l_bsp_ent.h" + +//============================================================================= + +int hl_nummodels; +hl_dmodel_t *hl_dmodels; //[HL_MAX_MAP_MODELS]; +int hl_dmodels_checksum; + +int hl_visdatasize; +byte *hl_dvisdata; //[HL_MAX_MAP_VISIBILITY]; +int hl_dvisdata_checksum; + +int hl_lightdatasize; +byte *hl_dlightdata; //[HL_MAX_MAP_LIGHTING]; +int hl_dlightdata_checksum; + +int hl_texdatasize; +byte *hl_dtexdata; //[HL_MAX_MAP_MIPTEX]; // (dmiptexlump_t) +int hl_dtexdata_checksum; + +int hl_entdatasize; +char *hl_dentdata; //[HL_MAX_MAP_ENTSTRING]; +int hl_dentdata_checksum; + +int hl_numleafs; +hl_dleaf_t *hl_dleafs; //[HL_MAX_MAP_LEAFS]; +int hl_dleafs_checksum; + +int hl_numplanes; +hl_dplane_t *hl_dplanes; //[HL_MAX_MAP_PLANES]; +int hl_dplanes_checksum; + +int hl_numvertexes; +hl_dvertex_t *hl_dvertexes; //[HL_MAX_MAP_VERTS]; +int hl_dvertexes_checksum; + +int hl_numnodes; +hl_dnode_t *hl_dnodes; //[HL_MAX_MAP_NODES]; +int hl_dnodes_checksum; + +int hl_numtexinfo; +hl_texinfo_t *hl_texinfo; //[HL_MAX_MAP_TEXINFO]; +int hl_texinfo_checksum; + +int hl_numfaces; +hl_dface_t *hl_dfaces; //[HL_MAX_MAP_FACES]; +int hl_dfaces_checksum; + +int hl_numclipnodes; +hl_dclipnode_t *hl_dclipnodes; //[HL_MAX_MAP_CLIPNODES]; +int hl_dclipnodes_checksum; + +int hl_numedges; +hl_dedge_t *hl_dedges; //[HL_MAX_MAP_EDGES]; +int hl_dedges_checksum; + +int hl_nummarksurfaces; +unsigned short *hl_dmarksurfaces; //[HL_MAX_MAP_MARKSURFACES]; +int hl_dmarksurfaces_checksum; + +int hl_numsurfedges; +int *hl_dsurfedges; //[HL_MAX_MAP_SURFEDGES]; +int hl_dsurfedges_checksum; + +//int num_entities; +//entity_t entities[HL_MAX_MAP_ENTITIES]; + + +//#ifdef //ME + +int hl_bspallocated = false; +int hl_allocatedbspmem = 0; + +void HL_AllocMaxBSP( void ) { + //models + hl_nummodels = 0; + hl_dmodels = (hl_dmodel_t *) GetMemory( HL_MAX_MAP_MODELS * sizeof( hl_dmodel_t ) ); + hl_allocatedbspmem = HL_MAX_MAP_MODELS * sizeof( hl_dmodel_t ); + //visibility + hl_visdatasize = 0; + hl_dvisdata = (byte *) GetMemory( HL_MAX_MAP_VISIBILITY * sizeof( byte ) ); + hl_allocatedbspmem += HL_MAX_MAP_VISIBILITY * sizeof( byte ); + //light data + hl_lightdatasize = 0; + hl_dlightdata = (byte *) GetMemory( HL_MAX_MAP_LIGHTING * sizeof( byte ) ); + hl_allocatedbspmem += HL_MAX_MAP_LIGHTING * sizeof( byte ); + //texture data + hl_texdatasize = 0; + hl_dtexdata = (byte *) GetMemory( HL_MAX_MAP_MIPTEX * sizeof( byte ) ); // (dmiptexlump_t) + hl_allocatedbspmem += HL_MAX_MAP_MIPTEX * sizeof( byte ); + //entities + hl_entdatasize = 0; + hl_dentdata = (char *) GetMemory( HL_MAX_MAP_ENTSTRING * sizeof( char ) ); + hl_allocatedbspmem += HL_MAX_MAP_ENTSTRING * sizeof( char ); + //leaves + hl_numleafs = 0; + hl_dleafs = (hl_dleaf_t *) GetMemory( HL_MAX_MAP_LEAFS * sizeof( hl_dleaf_t ) ); + hl_allocatedbspmem += HL_MAX_MAP_LEAFS * sizeof( hl_dleaf_t ); + //planes + hl_numplanes = 0; + hl_dplanes = (hl_dplane_t *) GetMemory( HL_MAX_MAP_PLANES * sizeof( hl_dplane_t ) ); + hl_allocatedbspmem += HL_MAX_MAP_PLANES * sizeof( hl_dplane_t ); + //vertexes + hl_numvertexes = 0; + hl_dvertexes = (hl_dvertex_t *) GetMemory( HL_MAX_MAP_VERTS * sizeof( hl_dvertex_t ) ); + hl_allocatedbspmem += HL_MAX_MAP_VERTS * sizeof( hl_dvertex_t ); + //nodes + hl_numnodes = 0; + hl_dnodes = (hl_dnode_t *) GetMemory( HL_MAX_MAP_NODES * sizeof( hl_dnode_t ) ); + hl_allocatedbspmem += HL_MAX_MAP_NODES * sizeof( hl_dnode_t ); + //texture info + hl_numtexinfo = 0; + hl_texinfo = (hl_texinfo_t *) GetMemory( HL_MAX_MAP_TEXINFO * sizeof( hl_texinfo_t ) ); + hl_allocatedbspmem += HL_MAX_MAP_TEXINFO * sizeof( hl_texinfo_t ); + //faces + hl_numfaces = 0; + hl_dfaces = (hl_dface_t *) GetMemory( HL_MAX_MAP_FACES * sizeof( hl_dface_t ) ); + hl_allocatedbspmem += HL_MAX_MAP_FACES * sizeof( hl_dface_t ); + //clip nodes + hl_numclipnodes = 0; + hl_dclipnodes = (hl_dclipnode_t *) GetMemory( HL_MAX_MAP_CLIPNODES * sizeof( hl_dclipnode_t ) ); + hl_allocatedbspmem += HL_MAX_MAP_CLIPNODES * sizeof( hl_dclipnode_t ); + //edges + hl_numedges = 0; + hl_dedges = (hl_dedge_t *) GetMemory( HL_MAX_MAP_EDGES * sizeof( hl_dedge_t ) ); + hl_allocatedbspmem += HL_MAX_MAP_EDGES, sizeof( hl_dedge_t ); + //mark surfaces + hl_nummarksurfaces = 0; + hl_dmarksurfaces = (unsigned short *) GetMemory( HL_MAX_MAP_MARKSURFACES * sizeof( unsigned short ) ); + hl_allocatedbspmem += HL_MAX_MAP_MARKSURFACES * sizeof( unsigned short ); + //surface edges + hl_numsurfedges = 0; + hl_dsurfedges = (int *) GetMemory( HL_MAX_MAP_SURFEDGES * sizeof( int ) ); + hl_allocatedbspmem += HL_MAX_MAP_SURFEDGES * sizeof( int ); + //print allocated memory + Log_Print( "allocated " ); + PrintMemorySize( hl_allocatedbspmem ); + Log_Print( " of BSP memory\n" ); +} //end of the function HL_AllocMaxBSP + +void HL_FreeMaxBSP( void ) { + //models + hl_nummodels = 0; + FreeMemory( hl_dmodels ); + hl_dmodels = NULL; + //visibility + hl_visdatasize = 0; + FreeMemory( hl_dvisdata ); + hl_dvisdata = NULL; + //light data + hl_lightdatasize = 0; + FreeMemory( hl_dlightdata ); + hl_dlightdata = NULL; + //texture data + hl_texdatasize = 0; + FreeMemory( hl_dtexdata ); + hl_dtexdata = NULL; + //entities + hl_entdatasize = 0; + FreeMemory( hl_dentdata ); + hl_dentdata = NULL; + //leaves + hl_numleafs = 0; + FreeMemory( hl_dleafs ); + hl_dleafs = NULL; + //planes + hl_numplanes = 0; + FreeMemory( hl_dplanes ); + hl_dplanes = NULL; + //vertexes + hl_numvertexes = 0; + FreeMemory( hl_dvertexes ); + hl_dvertexes = NULL; + //nodes + hl_numnodes = 0; + FreeMemory( hl_dnodes ); + hl_dnodes = NULL; + //texture info + hl_numtexinfo = 0; + FreeMemory( hl_texinfo ); + hl_texinfo = NULL; + //faces + hl_numfaces = 0; + FreeMemory( hl_dfaces ); + hl_dfaces = NULL; + //clip nodes + hl_numclipnodes = 0; + FreeMemory( hl_dclipnodes ); + hl_dclipnodes = NULL; + //edges + hl_numedges = 0; + FreeMemory( hl_dedges ); + hl_dedges = NULL; + //mark surfaces + hl_nummarksurfaces = 0; + FreeMemory( hl_dmarksurfaces ); + hl_dmarksurfaces = NULL; + //surface edges + hl_numsurfedges = 0; + FreeMemory( hl_dsurfedges ); + hl_dsurfedges = NULL; + // + Log_Print( "freed " ); + PrintMemorySize( hl_allocatedbspmem ); + Log_Print( " of BSP memory\n" ); + hl_allocatedbspmem = 0; +} //end of the function HL_FreeMaxBSP +//#endif //ME + +/* +=============== +FastChecksum +=============== +*/ + +int FastChecksum( void *buffer, int bytes ) { + int checksum = 0; + + while ( bytes-- ) + checksum = ( checksum << 4 ) ^ *( (char *)buffer )++; + + return checksum; +} + +/* +=============== +HL_CompressVis +=============== +*/ +int HL_CompressVis( byte *vis, byte *dest ) { + int j; + int rep; + int visrow; + byte *dest_p; + + dest_p = dest; + visrow = ( hl_numleafs + 7 ) >> 3; + + for ( j = 0 ; j < visrow ; j++ ) + { + *dest_p++ = vis[j]; + if ( vis[j] ) { + continue; + } + + rep = 1; + for ( j++; j < visrow ; j++ ) + if ( vis[j] || rep == 255 ) { + break; + } else { + rep++; + } + *dest_p++ = rep; + j--; + } + + return dest_p - dest; +} + + +/* +=================== +HL_DecompressVis +=================== +*/ +void HL_DecompressVis( byte *in, byte *decompressed ) { + int c; + byte *out; + int row; + + row = ( hl_numleafs + 7 ) >> 3; + out = decompressed; + + do + { + if ( *in ) { + *out++ = *in++; + continue; + } + + c = in[1]; + in += 2; + while ( c ) + { + *out++ = 0; + c--; + } + } while ( out - decompressed < row ); +} + +//============================================================================= + +/* +============= +HL_SwapBSPFile + +Byte swaps all data in a bsp file. +============= +*/ +void HL_SwapBSPFile( qboolean todisk ) { + int i, j, c; + hl_dmodel_t *d; + hl_dmiptexlump_t *mtl; + + +// models + for ( i = 0; i < hl_nummodels; i++ ) + { + d = &hl_dmodels[i]; + + for ( j = 0; j < HL_MAX_MAP_HULLS; j++ ) + d->headnode[j] = LittleLong( d->headnode[j] ); + + d->visleafs = LittleLong( d->visleafs ); + d->firstface = LittleLong( d->firstface ); + d->numfaces = LittleLong( d->numfaces ); + + for ( j = 0; j < 3; j++ ) + { + d->mins[j] = LittleFloat( d->mins[j] ); + d->maxs[j] = LittleFloat( d->maxs[j] ); + d->origin[j] = LittleFloat( d->origin[j] ); + } + } + +// +// vertexes +// + for ( i = 0; i < hl_numvertexes; i++ ) + { + for ( j = 0; j < 3; j++ ) + hl_dvertexes[i].point[j] = LittleFloat( hl_dvertexes[i].point[j] ); + } + +// +// planes +// + for ( i = 0 ; i < hl_numplanes ; i++ ) + { + for ( j = 0 ; j < 3 ; j++ ) + hl_dplanes[i].normal[j] = LittleFloat( hl_dplanes[i].normal[j] ); + hl_dplanes[i].dist = LittleFloat( hl_dplanes[i].dist ); + hl_dplanes[i].type = LittleLong( hl_dplanes[i].type ); + } + +// +// texinfos +// + for ( i = 0 ; i < hl_numtexinfo ; i++ ) + { + for ( j = 0 ; j < 8 ; j++ ) + hl_texinfo[i].vecs[0][j] = LittleFloat( hl_texinfo[i].vecs[0][j] ); + hl_texinfo[i].miptex = LittleLong( hl_texinfo[i].miptex ); + hl_texinfo[i].flags = LittleLong( hl_texinfo[i].flags ); + } + +// +// faces +// + for ( i = 0 ; i < hl_numfaces ; i++ ) + { + hl_dfaces[i].texinfo = LittleShort( hl_dfaces[i].texinfo ); + hl_dfaces[i].planenum = LittleShort( hl_dfaces[i].planenum ); + hl_dfaces[i].side = LittleShort( hl_dfaces[i].side ); + hl_dfaces[i].lightofs = LittleLong( hl_dfaces[i].lightofs ); + hl_dfaces[i].firstedge = LittleLong( hl_dfaces[i].firstedge ); + hl_dfaces[i].numedges = LittleShort( hl_dfaces[i].numedges ); + } + +// +// nodes +// + for ( i = 0 ; i < hl_numnodes ; i++ ) + { + hl_dnodes[i].planenum = LittleLong( hl_dnodes[i].planenum ); + for ( j = 0 ; j < 3 ; j++ ) + { + hl_dnodes[i].mins[j] = LittleShort( hl_dnodes[i].mins[j] ); + hl_dnodes[i].maxs[j] = LittleShort( hl_dnodes[i].maxs[j] ); + } + hl_dnodes[i].children[0] = LittleShort( hl_dnodes[i].children[0] ); + hl_dnodes[i].children[1] = LittleShort( hl_dnodes[i].children[1] ); + hl_dnodes[i].firstface = LittleShort( hl_dnodes[i].firstface ); + hl_dnodes[i].numfaces = LittleShort( hl_dnodes[i].numfaces ); + } + +// +// leafs +// + for ( i = 0 ; i < hl_numleafs ; i++ ) + { + hl_dleafs[i].contents = LittleLong( hl_dleafs[i].contents ); + for ( j = 0 ; j < 3 ; j++ ) + { + hl_dleafs[i].mins[j] = LittleShort( hl_dleafs[i].mins[j] ); + hl_dleafs[i].maxs[j] = LittleShort( hl_dleafs[i].maxs[j] ); + } + + hl_dleafs[i].firstmarksurface = LittleShort( hl_dleafs[i].firstmarksurface ); + hl_dleafs[i].nummarksurfaces = LittleShort( hl_dleafs[i].nummarksurfaces ); + hl_dleafs[i].visofs = LittleLong( hl_dleafs[i].visofs ); + } + +// +// clipnodes +// + for ( i = 0 ; i < hl_numclipnodes ; i++ ) + { + hl_dclipnodes[i].planenum = LittleLong( hl_dclipnodes[i].planenum ); + hl_dclipnodes[i].children[0] = LittleShort( hl_dclipnodes[i].children[0] ); + hl_dclipnodes[i].children[1] = LittleShort( hl_dclipnodes[i].children[1] ); + } + +// +// miptex +// + if ( hl_texdatasize ) { + mtl = (hl_dmiptexlump_t *)hl_dtexdata; + if ( todisk ) { + c = mtl->nummiptex; + } else { + c = LittleLong( mtl->nummiptex ); + } + mtl->nummiptex = LittleLong( mtl->nummiptex ); + for ( i = 0 ; i < c ; i++ ) + mtl->dataofs[i] = LittleLong( mtl->dataofs[i] ); + } + +// +// marksurfaces +// + for ( i = 0 ; i < hl_nummarksurfaces ; i++ ) + hl_dmarksurfaces[i] = LittleShort( hl_dmarksurfaces[i] ); + +// +// surfedges +// + for ( i = 0 ; i < hl_numsurfedges ; i++ ) + hl_dsurfedges[i] = LittleLong( hl_dsurfedges[i] ); + +// +// edges +// + for ( i = 0 ; i < hl_numedges ; i++ ) + { + hl_dedges[i].v[0] = LittleShort( hl_dedges[i].v[0] ); + hl_dedges[i].v[1] = LittleShort( hl_dedges[i].v[1] ); + } +} //end of the function HL_SwapBSPFile + + +hl_dheader_t *hl_header; + +int HL_CopyLump( int lump, void *dest, int size ) { + int length, ofs; + + length = hl_header->lumps[lump].filelen; + ofs = hl_header->lumps[lump].fileofs; + + if ( length % size ) { + Error( "HL_LoadBSPFile: odd lump size" ); + } + + memcpy( dest, (byte *)hl_header + ofs, length ); + + return length / size; +} + +/* +============= +HL_LoadBSPFile +============= +*/ +void HL_LoadBSPFile( char *filename, int offset, int length ) { + int i; + +// +// load the file header +// + LoadFile( filename, (void **)&hl_header, offset, length ); + +// swap the header + for ( i = 0 ; i < sizeof( hl_dheader_t ) / 4 ; i++ ) + ( (int *)hl_header )[i] = LittleLong( ( (int *)hl_header )[i] ); + + if ( hl_header->version != HL_BSPVERSION ) { + Error( "%s is version %i, not %i", filename, hl_header->version, HL_BSPVERSION ); + } + + hl_nummodels = HL_CopyLump( HL_LUMP_MODELS, hl_dmodels, sizeof( hl_dmodel_t ) ); + hl_numvertexes = HL_CopyLump( HL_LUMP_VERTEXES, hl_dvertexes, sizeof( hl_dvertex_t ) ); + hl_numplanes = HL_CopyLump( HL_LUMP_PLANES, hl_dplanes, sizeof( hl_dplane_t ) ); + hl_numleafs = HL_CopyLump( HL_LUMP_LEAFS, hl_dleafs, sizeof( hl_dleaf_t ) ); + hl_numnodes = HL_CopyLump( HL_LUMP_NODES, hl_dnodes, sizeof( hl_dnode_t ) ); + hl_numtexinfo = HL_CopyLump( HL_LUMP_TEXINFO, hl_texinfo, sizeof( hl_texinfo_t ) ); + hl_numclipnodes = HL_CopyLump( HL_LUMP_CLIPNODES, hl_dclipnodes, sizeof( hl_dclipnode_t ) ); + hl_numfaces = HL_CopyLump( HL_LUMP_FACES, hl_dfaces, sizeof( hl_dface_t ) ); + hl_nummarksurfaces = HL_CopyLump( HL_LUMP_MARKSURFACES, hl_dmarksurfaces, sizeof( hl_dmarksurfaces[0] ) ); + hl_numsurfedges = HL_CopyLump( HL_LUMP_SURFEDGES, hl_dsurfedges, sizeof( hl_dsurfedges[0] ) ); + hl_numedges = HL_CopyLump( HL_LUMP_EDGES, hl_dedges, sizeof( hl_dedge_t ) ); + + hl_texdatasize = HL_CopyLump( HL_LUMP_TEXTURES, hl_dtexdata, 1 ); + hl_visdatasize = HL_CopyLump( HL_LUMP_VISIBILITY, hl_dvisdata, 1 ); + hl_lightdatasize = HL_CopyLump( HL_LUMP_LIGHTING, hl_dlightdata, 1 ); + hl_entdatasize = HL_CopyLump( HL_LUMP_ENTITIES, hl_dentdata, 1 ); + + FreeMemory( hl_header ); // everything has been copied out + +// +// swap everything +// + HL_SwapBSPFile( false ); + + hl_dmodels_checksum = FastChecksum( hl_dmodels, hl_nummodels * sizeof( hl_dmodels[0] ) ); + hl_dvertexes_checksum = FastChecksum( hl_dvertexes, hl_numvertexes * sizeof( hl_dvertexes[0] ) ); + hl_dplanes_checksum = FastChecksum( hl_dplanes, hl_numplanes * sizeof( hl_dplanes[0] ) ); + hl_dleafs_checksum = FastChecksum( hl_dleafs, hl_numleafs * sizeof( hl_dleafs[0] ) ); + hl_dnodes_checksum = FastChecksum( hl_dnodes, hl_numnodes * sizeof( hl_dnodes[0] ) ); + hl_texinfo_checksum = FastChecksum( hl_texinfo, hl_numtexinfo * sizeof( hl_texinfo[0] ) ); + hl_dclipnodes_checksum = FastChecksum( hl_dclipnodes, hl_numclipnodes * sizeof( hl_dclipnodes[0] ) ); + hl_dfaces_checksum = FastChecksum( hl_dfaces, hl_numfaces * sizeof( hl_dfaces[0] ) ); + hl_dmarksurfaces_checksum = FastChecksum( hl_dmarksurfaces, hl_nummarksurfaces * sizeof( hl_dmarksurfaces[0] ) ); + hl_dsurfedges_checksum = FastChecksum( hl_dsurfedges, hl_numsurfedges * sizeof( hl_dsurfedges[0] ) ); + hl_dedges_checksum = FastChecksum( hl_dedges, hl_numedges * sizeof( hl_dedges[0] ) ); + hl_dtexdata_checksum = FastChecksum( hl_dtexdata, hl_numedges * sizeof( hl_dtexdata[0] ) ); + hl_dvisdata_checksum = FastChecksum( hl_dvisdata, hl_visdatasize * sizeof( hl_dvisdata[0] ) ); + hl_dlightdata_checksum = FastChecksum( hl_dlightdata, hl_lightdatasize * sizeof( hl_dlightdata[0] ) ); + hl_dentdata_checksum = FastChecksum( hl_dentdata, hl_entdatasize * sizeof( hl_dentdata[0] ) ); + +} + +//============================================================================ + +FILE *wadfile; +hl_dheader_t outheader; + +void HL_AddLump( int lumpnum, void *data, int len ) { + hl_lump_t *lump; + + lump = &hl_header->lumps[lumpnum]; + + lump->fileofs = LittleLong( ftell( wadfile ) ); + lump->filelen = LittleLong( len ); + SafeWrite( wadfile, data, ( len + 3 ) & ~3 ); +} + +/* +============= +HL_WriteBSPFile + +Swaps the bsp file in place, so it should not be referenced again +============= +*/ +void HL_WriteBSPFile( char *filename ) { + hl_header = &outheader; + memset( hl_header, 0, sizeof( hl_dheader_t ) ); + + HL_SwapBSPFile( true ); + + hl_header->version = LittleLong( HL_BSPVERSION ); + + wadfile = SafeOpenWrite( filename ); + SafeWrite( wadfile, hl_header, sizeof( hl_dheader_t ) ); // overwritten later + + HL_AddLump( HL_LUMP_PLANES, hl_dplanes, hl_numplanes * sizeof( hl_dplane_t ) ); + HL_AddLump( HL_LUMP_LEAFS, hl_dleafs, hl_numleafs * sizeof( hl_dleaf_t ) ); + HL_AddLump( HL_LUMP_VERTEXES, hl_dvertexes, hl_numvertexes * sizeof( hl_dvertex_t ) ); + HL_AddLump( HL_LUMP_NODES, hl_dnodes, hl_numnodes * sizeof( hl_dnode_t ) ); + HL_AddLump( HL_LUMP_TEXINFO, hl_texinfo, hl_numtexinfo * sizeof( hl_texinfo_t ) ); + HL_AddLump( HL_LUMP_FACES, hl_dfaces, hl_numfaces * sizeof( hl_dface_t ) ); + HL_AddLump( HL_LUMP_CLIPNODES, hl_dclipnodes, hl_numclipnodes * sizeof( hl_dclipnode_t ) ); + HL_AddLump( HL_LUMP_MARKSURFACES, hl_dmarksurfaces, hl_nummarksurfaces * sizeof( hl_dmarksurfaces[0] ) ); + HL_AddLump( HL_LUMP_SURFEDGES, hl_dsurfedges, hl_numsurfedges * sizeof( hl_dsurfedges[0] ) ); + HL_AddLump( HL_LUMP_EDGES, hl_dedges, hl_numedges * sizeof( hl_dedge_t ) ); + HL_AddLump( HL_LUMP_MODELS, hl_dmodels, hl_nummodels * sizeof( hl_dmodel_t ) ); + + HL_AddLump( HL_LUMP_LIGHTING, hl_dlightdata, hl_lightdatasize ); + HL_AddLump( HL_LUMP_VISIBILITY, hl_dvisdata, hl_visdatasize ); + HL_AddLump( HL_LUMP_ENTITIES, hl_dentdata, hl_entdatasize ); + HL_AddLump( HL_LUMP_TEXTURES, hl_dtexdata, hl_texdatasize ); + + fseek( wadfile, 0, SEEK_SET ); + SafeWrite( wadfile, hl_header, sizeof( hl_dheader_t ) ); + fclose( wadfile ); +} + +//============================================================================ + +#define ENTRIES( a ) ( sizeof( a ) / sizeof( *( a ) ) ) +#define ENTRYSIZE( a ) ( sizeof( *( a ) ) ) + +int ArrayUsage( char *szItem, int items, int maxitems, int itemsize ) { + float percentage = maxitems ? items * 100.0 / maxitems : 0.0; + + qprintf( "%-12s %7i/%-7i %7i/%-7i (%4.1f%%)", + szItem, items, maxitems, items * itemsize, maxitems * itemsize, percentage ); + if ( percentage > 80.0 ) { + qprintf( "VERY FULL!\n" ); + } else if ( percentage > 95.0 ) { + qprintf( "SIZE DANGER!\n" ); + } else if ( percentage > 99.9 ) { + qprintf( "SIZE OVERFLOW!!!\n" ); + } else { + qprintf( "\n" ); + } + return items * itemsize; +} + +int GlobUsage( char *szItem, int itemstorage, int maxstorage ) { + float percentage = maxstorage ? itemstorage * 100.0 / maxstorage : 0.0; + + qprintf( "%-12s [variable] %7i/%-7i (%4.1f%%)", + szItem, itemstorage, maxstorage, percentage ); + if ( percentage > 80.0 ) { + qprintf( "VERY FULL!\n" ); + } else if ( percentage > 95.0 ) { + qprintf( "SIZE DANGER!\n" ); + } else if ( percentage > 99.9 ) { + qprintf( "SIZE OVERFLOW!!!\n" ); + } else { + qprintf( "\n" ); + } + return itemstorage; +} + +/* +============= +HL_PrintBSPFileSizes + +Dumps info about current file +============= +*/ +void HL_PrintBSPFileSizes( void ) { + //int numtextures = hl_texdatasize ? ((hl_dmiptexlump_t*)hl_dtexdata)->nummiptex : 0; // TTimo: unused + int totalmemory = 0; + + qprintf( "\n" ); + qprintf( "Object names Objects/Maxobjs Memory / Maxmem Fullness\n" ); + qprintf( "------------ --------------- --------------- --------\n" ); + + totalmemory += ArrayUsage( "models", hl_nummodels, ENTRIES( hl_dmodels ), ENTRYSIZE( hl_dmodels ) ); + totalmemory += ArrayUsage( "planes", hl_numplanes, ENTRIES( hl_dplanes ), ENTRYSIZE( hl_dplanes ) ); + totalmemory += ArrayUsage( "vertexes", hl_numvertexes, ENTRIES( hl_dvertexes ), ENTRYSIZE( hl_dvertexes ) ); + totalmemory += ArrayUsage( "nodes", hl_numnodes, ENTRIES( hl_dnodes ), ENTRYSIZE( hl_dnodes ) ); + totalmemory += ArrayUsage( "texinfos", hl_numtexinfo, ENTRIES( hl_texinfo ), ENTRYSIZE( hl_texinfo ) ); + totalmemory += ArrayUsage( "faces", hl_numfaces, ENTRIES( hl_dfaces ), ENTRYSIZE( hl_dfaces ) ); + totalmemory += ArrayUsage( "clipnodes", hl_numclipnodes, ENTRIES( hl_dclipnodes ), ENTRYSIZE( hl_dclipnodes ) ); + totalmemory += ArrayUsage( "leaves", hl_numleafs, ENTRIES( hl_dleafs ), ENTRYSIZE( hl_dleafs ) ); + totalmemory += ArrayUsage( "marksurfaces",hl_nummarksurfaces,ENTRIES( hl_dmarksurfaces ),ENTRYSIZE( hl_dmarksurfaces ) ); + totalmemory += ArrayUsage( "surfedges", hl_numsurfedges, ENTRIES( hl_dsurfedges ), ENTRYSIZE( hl_dsurfedges ) ); + totalmemory += ArrayUsage( "edges", hl_numedges, ENTRIES( hl_dedges ), ENTRYSIZE( hl_dedges ) ); + + totalmemory += GlobUsage( "texdata", hl_texdatasize, sizeof( hl_dtexdata ) ); + totalmemory += GlobUsage( "lightdata", hl_lightdatasize, sizeof( hl_dlightdata ) ); + totalmemory += GlobUsage( "visdata", hl_visdatasize, sizeof( hl_dvisdata ) ); + totalmemory += GlobUsage( "entdata", hl_entdatasize, sizeof( hl_dentdata ) ); + + qprintf( "=== Total BSP file data space used: %d bytes ===\n\n", totalmemory ); +} + + + +/* +================= +ParseEpair +================= +* / +epair_t *ParseEpair (void) +{ + epair_t *e; + + e = malloc (sizeof(epair_t)); + memset (e, 0, sizeof(epair_t)); + + if (strlen(token) >= MAX_KEY-1) + Error ("ParseEpar: token too long"); + e->key = copystring(token); + GetToken (false); + if (strlen(token) >= MAX_VALUE-1) + Error ("ParseEpar: token too long"); + e->value = copystring(token); + + return e; +} // +*/ + + +/* +================ +ParseEntity +================ +* / +qboolean ParseEntity (void) +{ + epair_t *e; + entity_t *mapent; + + if (!GetToken (true)) + return false; + + if (strcmp (token, "{") ) + Error ("ParseEntity: { not found"); + + if (num_entities == HL_MAX_MAP_ENTITIES) + Error ("num_entities == HL_MAX_MAP_ENTITIES"); + + mapent = &entities[num_entities]; + num_entities++; + + do + { + if (!GetToken (true)) + Error ("ParseEntity: EOF without closing brace"); + if (!strcmp (token, "}") ) + break; + e = ParseEpair (); + e->next = mapent->epairs; + mapent->epairs = e; + } while (1); + + return true; +} // +*/ + +/* +================ +ParseEntities + +Parses the dentdata string into entities +================ +*/ +void HL_ParseEntities( void ) { + script_t *script; + + num_entities = 0; + script = LoadScriptMemory( hl_dentdata, hl_entdatasize, "*Half-Life bsp file" ); + SetScriptFlags( script, SCFL_NOSTRINGWHITESPACES | + SCFL_NOSTRINGESCAPECHARS ); + + while ( ParseEntity( script ) ) + { + } //end while + + FreeScript( script ); +} //end of the function HL_ParseEntities + + +/* +================ +UnparseEntities + +Generates the dentdata string from all the entities +================ +*/ +void HL_UnparseEntities( void ) { + char *buf, *end; + epair_t *ep; + char line[2048]; + int i; + + buf = hl_dentdata; + end = buf; + *end = 0; + + for ( i = 0 ; i < num_entities ; i++ ) + { + ep = entities[i].epairs; + if ( !ep ) { + continue; // ent got removed + + } + strcat( end,"{\n" ); + end += 2; + + for ( ep = entities[i].epairs ; ep ; ep = ep->next ) + { + sprintf( line, "\"%s\" \"%s\"\n", ep->key, ep->value ); + strcat( end, line ); + end += strlen( line ); + } + strcat( end,"}\n" ); + end += 2; + + if ( end > buf + HL_MAX_MAP_ENTSTRING ) { + Error( "Entity text too long" ); + } + } + hl_entdatasize = end - buf + 1; +} //end of the function HL_UnparseEntities + + +/* +void SetKeyValue (entity_t *ent, char *key, char *value) +{ + epair_t *ep; + + for (ep=ent->epairs ; ep ; ep=ep->next) + if (!strcmp (ep->key, key) ) + { + free (ep->value); + ep->value = copystring(value); + return; + } + ep = malloc (sizeof(*ep)); + ep->next = ent->epairs; + ent->epairs = ep; + ep->key = copystring(key); + ep->value = copystring(value); +} + +char *ValueForKey (entity_t *ent, char *key) +{ + epair_t *ep; + + for (ep=ent->epairs ; ep ; ep=ep->next) + if (!strcmp (ep->key, key) ) + return ep->value; + return ""; +} + +vec_t FloatForKey (entity_t *ent, char *key) +{ + char *k; + + k = ValueForKey (ent, key); + return atof(k); +} + +void GetVectorForKey (entity_t *ent, char *key, vec3_t vec) +{ + char *k; + double v1, v2, v3; + + k = ValueForKey (ent, key); +// scanf into doubles, then assign, so it is vec_t size independent + v1 = v2 = v3 = 0; + sscanf (k, "%lf %lf %lf", &v1, &v2, &v3); + vec[0] = v1; + vec[1] = v2; + vec[2] = v3; +} // +*/ diff --git a/src/bspc/l_bsp_hl.h b/src/bspc/l_bsp_hl.h new file mode 100644 index 0000000..c1131cf --- /dev/null +++ b/src/bspc/l_bsp_hl.h @@ -0,0 +1,332 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +/*** +* +* Copyright (c) 1998, 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. +* +****/ + + +// upper design bounds + +#define HL_MAX_MAP_HULLS 4 + +#define HL_MAX_MAP_MODELS 400 +#define HL_MAX_MAP_BRUSHES 4096 +#define HL_MAX_MAP_ENTITIES 1024 +#define HL_MAX_MAP_ENTSTRING ( 128 * 1024 ) + +#define HL_MAX_MAP_PLANES 32767 +#define HL_MAX_MAP_NODES 32767 // because negative shorts are contents +#define HL_MAX_MAP_CLIPNODES 32767 // +#define HL_MAX_MAP_LEAFS 8192 +#define HL_MAX_MAP_VERTS 65535 +#define HL_MAX_MAP_FACES 65535 +#define HL_MAX_MAP_MARKSURFACES 65535 +#define HL_MAX_MAP_TEXINFO 8192 +#define HL_MAX_MAP_EDGES 256000 +#define HL_MAX_MAP_SURFEDGES 512000 +#define HL_MAX_MAP_TEXTURES 512 +#define HL_MAX_MAP_MIPTEX 0x200000 +#define HL_MAX_MAP_LIGHTING 0x200000 +#define HL_MAX_MAP_VISIBILITY 0x200000 + +#define HL_MAX_MAP_PORTALS 65536 + +// key / value pair sizes + +#define MAX_KEY 32 +#define MAX_VALUE 1024 + +//============================================================================= + + +#define HL_BSPVERSION 30 +#define HL_TOOLVERSION 2 + + +typedef struct +{ + int fileofs, filelen; +} hl_lump_t; + +#define HL_LUMP_ENTITIES 0 +#define HL_LUMP_PLANES 1 +#define HL_LUMP_TEXTURES 2 +#define HL_LUMP_VERTEXES 3 +#define HL_LUMP_VISIBILITY 4 +#define HL_LUMP_NODES 5 +#define HL_LUMP_TEXINFO 6 +#define HL_LUMP_FACES 7 +#define HL_LUMP_LIGHTING 8 +#define HL_LUMP_CLIPNODES 9 +#define HL_LUMP_LEAFS 10 +#define HL_LUMP_MARKSURFACES 11 +#define HL_LUMP_EDGES 12 +#define HL_LUMP_SURFEDGES 13 +#define HL_LUMP_MODELS 14 + +#define HL_HEADER_LUMPS 15 + +typedef struct +{ + float mins[3], maxs[3]; + float origin[3]; + int headnode[HL_MAX_MAP_HULLS]; + int visleafs; // not including the solid leaf 0 + int firstface, numfaces; +} hl_dmodel_t; + +typedef struct +{ + int version; + hl_lump_t lumps[HL_HEADER_LUMPS]; +} hl_dheader_t; + +typedef struct +{ + int nummiptex; + int dataofs[4]; // [nummiptex] +} hl_dmiptexlump_t; + +#define MIPLEVELS 4 +typedef struct hl_miptex_s +{ + char name[16]; + unsigned width, height; + unsigned offsets[MIPLEVELS]; // four mip maps stored +} hl_miptex_t; + + +typedef struct +{ + float point[3]; +} hl_dvertex_t; + + +// 0-2 are axial planes +#define PLANE_X 0 +#define PLANE_Y 1 +#define PLANE_Z 2 + +// 3-5 are non-axial planes snapped to the nearest +#define PLANE_ANYX 3 +#define PLANE_ANYY 4 +#define PLANE_ANYZ 5 + +typedef struct +{ + float normal[3]; + float dist; + int type; // PLANE_X - PLANE_ANYZ ?remove? trivial to regenerate +} hl_dplane_t; + + + +#define HL_CONTENTS_EMPTY -1 +#define HL_CONTENTS_SOLID -2 +#define HL_CONTENTS_WATER -3 +#define HL_CONTENTS_SLIME -4 +#define HL_CONTENTS_LAVA -5 +#define HL_CONTENTS_SKY -6 +#define HL_CONTENTS_ORIGIN -7 // removed at csg time +#define HL_CONTENTS_CLIP -8 // changed to contents_solid + +#define HL_CONTENTS_CURRENT_0 -9 +#define HL_CONTENTS_CURRENT_90 -10 +#define HL_CONTENTS_CURRENT_180 -11 +#define HL_CONTENTS_CURRENT_270 -12 +#define HL_CONTENTS_CURRENT_UP -13 +#define HL_CONTENTS_CURRENT_DOWN -14 + +#define HL_CONTENTS_TRANSLUCENT -15 + +// !!! if this is changed, it must be changed in asm_i386.h too !!! +typedef struct +{ + int planenum; + short children[2]; // negative numbers are -(leafs+1), not nodes + short mins[3]; // for sphere culling + short maxs[3]; + unsigned short firstface; + unsigned short numfaces; // counting both sides +} hl_dnode_t; + +typedef struct +{ + int planenum; + short children[2]; // negative numbers are contents +} hl_dclipnode_t; + + +typedef struct hl_texinfo_s +{ + float vecs[2][4]; // [s/t][xyz offset] + int miptex; + int flags; +} hl_texinfo_t; +#define TEX_SPECIAL 1 // sky or slime, no lightmap or 256 subdivision + +// note that edge 0 is never used, because negative edge nums are used for +// counterclockwise use of the edge in a face +typedef struct +{ + unsigned short v[2]; // vertex numbers +} hl_dedge_t; + +#define MAXLIGHTMAPS 4 +typedef struct +{ + short planenum; + short side; + + int firstedge; // we must support > 64k edges + short numedges; + short texinfo; + +// lighting info + byte styles[MAXLIGHTMAPS]; + int lightofs; // start of [numstyles*surfsize] samples +} hl_dface_t; + + +#define AMBIENT_WATER 0 +#define AMBIENT_SKY 1 +#define AMBIENT_SLIME 2 +#define AMBIENT_LAVA 3 + +#define NUM_AMBIENTS 4 // automatic ambient sounds + +// leaf 0 is the generic HL_CONTENTS_SOLID leaf, used for all solid areas +// all other leafs need visibility info +typedef struct +{ + int contents; + int visofs; // -1 = no visibility info + + short mins[3]; // for frustum culling + short maxs[3]; + + unsigned short firstmarksurface; + unsigned short nummarksurfaces; + + byte ambient_level[NUM_AMBIENTS]; +} hl_dleaf_t; + + +//============================================================================ + +#ifndef QUAKE_GAME + +#define ANGLE_UP -1 +#define ANGLE_DOWN -2 + + +// the utilities get to be lazy and just use large static arrays + +extern int hl_nummodels; +extern hl_dmodel_t *hl_dmodels; //[MAX_MAP_MODELS]; +extern int hl_dmodels_checksum; + +extern int hl_visdatasize; +extern byte *hl_dvisdata; //[MAX_MAP_VISIBILITY]; +extern int hl_dvisdata_checksum; + +extern int hl_lightdatasize; +extern byte *hl_dlightdata; //[MAX_MAP_LIGHTING]; +extern int hl_dlightdata_checksum; + +extern int hl_texdatasize; +extern byte *hl_dtexdata; //[MAX_MAP_MIPTEX]; // (dmiptexlump_t) +extern int hl_dtexdata_checksum; + +extern int hl_entdatasize; +extern char *hl_dentdata; //[MAX_MAP_ENTSTRING]; +extern int hl_dentdata_checksum; + +extern int hl_numleafs; +extern hl_dleaf_t *hl_dleafs; //[MAX_MAP_LEAFS]; +extern int hl_dleafs_checksum; + +extern int hl_numplanes; +extern hl_dplane_t *hl_dplanes; //[MAX_MAP_PLANES]; +extern int hl_dplanes_checksum; + +extern int hl_numvertexes; +extern hl_dvertex_t *hl_dvertexes; //[MAX_MAP_VERTS]; +extern int hl_dvertexes_checksum; + +extern int hl_numnodes; +extern hl_dnode_t *hl_dnodes; //[MAX_MAP_NODES]; +extern int hl_dnodes_checksum; + +extern int hl_numtexinfo; +extern hl_texinfo_t *hl_texinfo; //[MAX_MAP_TEXINFO]; +extern int hl_texinfo_checksum; + +extern int hl_numfaces; +extern hl_dface_t *hl_dfaces; //[MAX_MAP_FACES]; +extern int hl_dfaces_checksum; + +extern int hl_numclipnodes; +extern hl_dclipnode_t *hl_dclipnodes; //[MAX_MAP_CLIPNODES]; +extern int hl_dclipnodes_checksum; + +extern int hl_numedges; +extern hl_dedge_t *hl_dedges; //[MAX_MAP_EDGES]; +extern int hl_dedges_checksum; + +extern int hl_nummarksurfaces; +extern unsigned short *hl_dmarksurfaces; //[MAX_MAP_MARKSURFACES]; +extern int hl_dmarksurfaces_checksum; + +extern int hl_numsurfedges; +extern int *hl_dsurfedges; //[MAX_MAP_SURFEDGES]; +extern int hl_dsurfedges_checksum; + +int FastChecksum( void *buffer, int bytes ); + +void HL_AllocMaxBSP( void ); +void HL_FreeMaxBSP( void ); + +void HL_DecompressVis( byte *in, byte *decompressed ); +int HL_CompressVis( byte *vis, byte *dest ); + +void HL_LoadBSPFile( char *filename, int offset, int length ); +void HL_WriteBSPFile( char *filename ); +void HL_PrintBSPFileSizes( void ); +void HL_PrintBSPFileSizes( void ); +void HL_ParseEntities( void ); +void HL_UnparseEntities( void ); + +#endif diff --git a/src/bspc/l_bsp_q1.c b/src/bspc/l_bsp_q1.c new file mode 100644 index 0000000..c9c966e --- /dev/null +++ b/src/bspc/l_bsp_q1.c @@ -0,0 +1,608 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +#include "l_cmd.h" +#include "l_math.h" +#include "l_mem.h" +#include "l_log.h" +#include "../botlib/l_script.h" +#include "l_bsp_q1.h" +#include "l_bsp_ent.h" + +//============================================================================= + +int q1_nummodels; +q1_dmodel_t *q1_dmodels; //[MAX_MAP_MODELS]; + +int q1_visdatasize; +byte *q1_dvisdata; //[MAX_MAP_VISIBILITY]; + +int q1_lightdatasize; +byte *q1_dlightdata; //[MAX_MAP_LIGHTING]; + +int q1_texdatasize; +byte *q1_dtexdata; //[MAX_MAP_MIPTEX]; // (dmiptexlump_t) + +int q1_entdatasize; +char *q1_dentdata; //[MAX_MAP_ENTSTRING]; + +int q1_numleafs; +q1_dleaf_t *q1_dleafs; //[MAX_MAP_LEAFS]; + +int q1_numplanes; +q1_dplane_t *q1_dplanes; //[MAX_MAP_PLANES]; + +int q1_numvertexes; +q1_dvertex_t *q1_dvertexes; //[MAX_MAP_VERTS]; + +int q1_numnodes; +q1_dnode_t *q1_dnodes; //[MAX_MAP_NODES]; + +int q1_numtexinfo; +q1_texinfo_t *q1_texinfo; //[MAX_MAP_TEXINFO]; + +int q1_numfaces; +q1_dface_t *q1_dfaces; //[MAX_MAP_FACES]; + +int q1_numclipnodes; +q1_dclipnode_t *q1_dclipnodes; //[MAX_MAP_CLIPNODES]; + +int q1_numedges; +q1_dedge_t *q1_dedges; //[MAX_MAP_EDGES]; + +int q1_nummarksurfaces; +unsigned short *q1_dmarksurfaces; //[MAX_MAP_MARKSURFACES]; + +int q1_numsurfedges; +int *q1_dsurfedges; //[MAX_MAP_SURFEDGES]; + +//============================================================================= + +int q1_bspallocated = false; +int q1_allocatedbspmem = 0; + +void Q1_AllocMaxBSP( void ) { + //models + q1_nummodels = 0; + q1_dmodels = (q1_dmodel_t *) GetMemory( Q1_MAX_MAP_MODELS * sizeof( q1_dmodel_t ) ); + q1_allocatedbspmem = Q1_MAX_MAP_MODELS * sizeof( q1_dmodel_t ); + //visibility + q1_visdatasize = 0; + q1_dvisdata = (byte *) GetMemory( Q1_MAX_MAP_VISIBILITY * sizeof( byte ) ); + q1_allocatedbspmem += Q1_MAX_MAP_VISIBILITY * sizeof( byte ); + //light data + q1_lightdatasize = 0; + q1_dlightdata = (byte *) GetMemory( Q1_MAX_MAP_LIGHTING * sizeof( byte ) ); + q1_allocatedbspmem += Q1_MAX_MAP_LIGHTING * sizeof( byte ); + //texture data + q1_texdatasize = 0; + q1_dtexdata = (byte *) GetMemory( Q1_MAX_MAP_MIPTEX * sizeof( byte ) ); // (dmiptexlump_t) + q1_allocatedbspmem += Q1_MAX_MAP_MIPTEX * sizeof( byte ); + //entities + q1_entdatasize = 0; + q1_dentdata = (char *) GetMemory( Q1_MAX_MAP_ENTSTRING * sizeof( char ) ); + q1_allocatedbspmem += Q1_MAX_MAP_ENTSTRING * sizeof( char ); + //leaves + q1_numleafs = 0; + q1_dleafs = (q1_dleaf_t *) GetMemory( Q1_MAX_MAP_LEAFS * sizeof( q1_dleaf_t ) ); + q1_allocatedbspmem += Q1_MAX_MAP_LEAFS * sizeof( q1_dleaf_t ); + //planes + q1_numplanes = 0; + q1_dplanes = (q1_dplane_t *) GetMemory( Q1_MAX_MAP_PLANES * sizeof( q1_dplane_t ) ); + q1_allocatedbspmem += Q1_MAX_MAP_PLANES * sizeof( q1_dplane_t ); + //vertexes + q1_numvertexes = 0; + q1_dvertexes = (q1_dvertex_t *) GetMemory( Q1_MAX_MAP_VERTS * sizeof( q1_dvertex_t ) ); + q1_allocatedbspmem += Q1_MAX_MAP_VERTS * sizeof( q1_dvertex_t ); + //nodes + q1_numnodes = 0; + q1_dnodes = (q1_dnode_t *) GetMemory( Q1_MAX_MAP_NODES * sizeof( q1_dnode_t ) ); + q1_allocatedbspmem += Q1_MAX_MAP_NODES * sizeof( q1_dnode_t ); + //texture info + q1_numtexinfo = 0; + q1_texinfo = (q1_texinfo_t *) GetMemory( Q1_MAX_MAP_TEXINFO * sizeof( q1_texinfo_t ) ); + q1_allocatedbspmem += Q1_MAX_MAP_TEXINFO * sizeof( q1_texinfo_t ); + //faces + q1_numfaces = 0; + q1_dfaces = (q1_dface_t *) GetMemory( Q1_MAX_MAP_FACES * sizeof( q1_dface_t ) ); + q1_allocatedbspmem += Q1_MAX_MAP_FACES * sizeof( q1_dface_t ); + //clip nodes + q1_numclipnodes = 0; + q1_dclipnodes = (q1_dclipnode_t *) GetMemory( Q1_MAX_MAP_CLIPNODES * sizeof( q1_dclipnode_t ) ); + q1_allocatedbspmem += Q1_MAX_MAP_CLIPNODES * sizeof( q1_dclipnode_t ); + //edges + q1_numedges = 0; + q1_dedges = (q1_dedge_t *) GetMemory( Q1_MAX_MAP_EDGES * sizeof( q1_dedge_t ) ); + q1_allocatedbspmem += Q1_MAX_MAP_EDGES, sizeof( q1_dedge_t ); + //mark surfaces + q1_nummarksurfaces = 0; + q1_dmarksurfaces = (unsigned short *) GetMemory( Q1_MAX_MAP_MARKSURFACES * sizeof( unsigned short ) ); + q1_allocatedbspmem += Q1_MAX_MAP_MARKSURFACES * sizeof( unsigned short ); + //surface edges + q1_numsurfedges = 0; + q1_dsurfedges = (int *) GetMemory( Q1_MAX_MAP_SURFEDGES * sizeof( int ) ); + q1_allocatedbspmem += Q1_MAX_MAP_SURFEDGES * sizeof( int ); + //print allocated memory + Log_Print( "allocated " ); + PrintMemorySize( q1_allocatedbspmem ); + Log_Print( " of BSP memory\n" ); +} //end of the function Q1_AllocMaxBSP + +void Q1_FreeMaxBSP( void ) { + //models + q1_nummodels = 0; + FreeMemory( q1_dmodels ); + q1_dmodels = NULL; + //visibility + q1_visdatasize = 0; + FreeMemory( q1_dvisdata ); + q1_dvisdata = NULL; + //light data + q1_lightdatasize = 0; + FreeMemory( q1_dlightdata ); + q1_dlightdata = NULL; + //texture data + q1_texdatasize = 0; + FreeMemory( q1_dtexdata ); + q1_dtexdata = NULL; + //entities + q1_entdatasize = 0; + FreeMemory( q1_dentdata ); + q1_dentdata = NULL; + //leaves + q1_numleafs = 0; + FreeMemory( q1_dleafs ); + q1_dleafs = NULL; + //planes + q1_numplanes = 0; + FreeMemory( q1_dplanes ); + q1_dplanes = NULL; + //vertexes + q1_numvertexes = 0; + FreeMemory( q1_dvertexes ); + q1_dvertexes = NULL; + //nodes + q1_numnodes = 0; + FreeMemory( q1_dnodes ); + q1_dnodes = NULL; + //texture info + q1_numtexinfo = 0; + FreeMemory( q1_texinfo ); + q1_texinfo = NULL; + //faces + q1_numfaces = 0; + FreeMemory( q1_dfaces ); + q1_dfaces = NULL; + //clip nodes + q1_numclipnodes = 0; + FreeMemory( q1_dclipnodes ); + q1_dclipnodes = NULL; + //edges + q1_numedges = 0; + FreeMemory( q1_dedges ); + q1_dedges = NULL; + //mark surfaces + q1_nummarksurfaces = 0; + FreeMemory( q1_dmarksurfaces ); + q1_dmarksurfaces = NULL; + //surface edges + q1_numsurfedges = 0; + FreeMemory( q1_dsurfedges ); + q1_dsurfedges = NULL; + // + Log_Print( "freed " ); + PrintMemorySize( q1_allocatedbspmem ); + Log_Print( " of BSP memory\n" ); + q1_allocatedbspmem = 0; +} //end of the function Q1_FreeMaxBSP +//#endif //ME + +/* +============= +Q1_SwapBSPFile + +Byte swaps all data in a bsp file. +============= +*/ +void Q1_SwapBSPFile( qboolean todisk ) { + int i, j, c; + q1_dmodel_t *d; + q1_dmiptexlump_t *mtl; + + +// models + for ( i = 0 ; i < q1_nummodels ; i++ ) + { + d = &q1_dmodels[i]; + + for ( j = 0 ; j < Q1_MAX_MAP_HULLS ; j++ ) + d->headnode[j] = LittleLong( d->headnode[j] ); + + d->visleafs = LittleLong( d->visleafs ); + d->firstface = LittleLong( d->firstface ); + d->numfaces = LittleLong( d->numfaces ); + + for ( j = 0 ; j < 3 ; j++ ) + { + d->mins[j] = LittleFloat( d->mins[j] ); + d->maxs[j] = LittleFloat( d->maxs[j] ); + d->origin[j] = LittleFloat( d->origin[j] ); + } + } + +// +// vertexes +// + for ( i = 0 ; i < q1_numvertexes ; i++ ) + { + for ( j = 0 ; j < 3 ; j++ ) + q1_dvertexes[i].point[j] = LittleFloat( q1_dvertexes[i].point[j] ); + } + +// +// planes +// + for ( i = 0 ; i < q1_numplanes ; i++ ) + { + for ( j = 0 ; j < 3 ; j++ ) + q1_dplanes[i].normal[j] = LittleFloat( q1_dplanes[i].normal[j] ); + q1_dplanes[i].dist = LittleFloat( q1_dplanes[i].dist ); + q1_dplanes[i].type = LittleLong( q1_dplanes[i].type ); + } + +// +// texinfos +// + for ( i = 0 ; i < q1_numtexinfo ; i++ ) + { + for ( j = 0 ; j < 8 ; j++ ) + q1_texinfo[i].vecs[0][j] = LittleFloat( q1_texinfo[i].vecs[0][j] ); + q1_texinfo[i].miptex = LittleLong( q1_texinfo[i].miptex ); + q1_texinfo[i].flags = LittleLong( q1_texinfo[i].flags ); + } + +// +// faces +// + for ( i = 0 ; i < q1_numfaces ; i++ ) + { + q1_dfaces[i].texinfo = LittleShort( q1_dfaces[i].texinfo ); + q1_dfaces[i].planenum = LittleShort( q1_dfaces[i].planenum ); + q1_dfaces[i].side = LittleShort( q1_dfaces[i].side ); + q1_dfaces[i].lightofs = LittleLong( q1_dfaces[i].lightofs ); + q1_dfaces[i].firstedge = LittleLong( q1_dfaces[i].firstedge ); + q1_dfaces[i].numedges = LittleShort( q1_dfaces[i].numedges ); + } + +// +// nodes +// + for ( i = 0 ; i < q1_numnodes ; i++ ) + { + q1_dnodes[i].planenum = LittleLong( q1_dnodes[i].planenum ); + for ( j = 0 ; j < 3 ; j++ ) + { + q1_dnodes[i].mins[j] = LittleShort( q1_dnodes[i].mins[j] ); + q1_dnodes[i].maxs[j] = LittleShort( q1_dnodes[i].maxs[j] ); + } + q1_dnodes[i].children[0] = LittleShort( q1_dnodes[i].children[0] ); + q1_dnodes[i].children[1] = LittleShort( q1_dnodes[i].children[1] ); + q1_dnodes[i].firstface = LittleShort( q1_dnodes[i].firstface ); + q1_dnodes[i].numfaces = LittleShort( q1_dnodes[i].numfaces ); + } + +// +// leafs +// + for ( i = 0 ; i < q1_numleafs ; i++ ) + { + q1_dleafs[i].contents = LittleLong( q1_dleafs[i].contents ); + for ( j = 0 ; j < 3 ; j++ ) + { + q1_dleafs[i].mins[j] = LittleShort( q1_dleafs[i].mins[j] ); + q1_dleafs[i].maxs[j] = LittleShort( q1_dleafs[i].maxs[j] ); + } + + q1_dleafs[i].firstmarksurface = LittleShort( q1_dleafs[i].firstmarksurface ); + q1_dleafs[i].nummarksurfaces = LittleShort( q1_dleafs[i].nummarksurfaces ); + q1_dleafs[i].visofs = LittleLong( q1_dleafs[i].visofs ); + } + +// +// clipnodes +// + for ( i = 0 ; i < q1_numclipnodes ; i++ ) + { + q1_dclipnodes[i].planenum = LittleLong( q1_dclipnodes[i].planenum ); + q1_dclipnodes[i].children[0] = LittleShort( q1_dclipnodes[i].children[0] ); + q1_dclipnodes[i].children[1] = LittleShort( q1_dclipnodes[i].children[1] ); + } + +// +// miptex +// + if ( q1_texdatasize ) { + mtl = (q1_dmiptexlump_t *)q1_dtexdata; + if ( todisk ) { + c = mtl->nummiptex; + } else { + c = LittleLong( mtl->nummiptex ); + } + mtl->nummiptex = LittleLong( mtl->nummiptex ); + for ( i = 0 ; i < c ; i++ ) + mtl->dataofs[i] = LittleLong( mtl->dataofs[i] ); + } + +// +// marksurfaces +// + for ( i = 0 ; i < q1_nummarksurfaces ; i++ ) + q1_dmarksurfaces[i] = LittleShort( q1_dmarksurfaces[i] ); + +// +// surfedges +// + for ( i = 0 ; i < q1_numsurfedges ; i++ ) + q1_dsurfedges[i] = LittleLong( q1_dsurfedges[i] ); + +// +// edges +// + for ( i = 0 ; i < q1_numedges ; i++ ) + { + q1_dedges[i].v[0] = LittleShort( q1_dedges[i].v[0] ); + q1_dedges[i].v[1] = LittleShort( q1_dedges[i].v[1] ); + } +} + + +q1_dheader_t *q1_header; + +int Q1_CopyLump( int lump, void *dest, int size ) { + int length, ofs; + + length = q1_header->lumps[lump].filelen; + ofs = q1_header->lumps[lump].fileofs; + + if ( length % size ) { + Error( "Q1_LoadBSPFile: odd lump size" ); + } + + memcpy( dest, (byte *)q1_header + ofs, length ); + + return length / size; +} + +/* +============= +Q1_LoadBSPFile +============= +*/ +void Q1_LoadBSPFile( char *filename, int offset, int length ) { + int i; + +// +// load the file header +// + LoadFile( filename, (void **)&q1_header, offset, length ); + +// swap the header + for ( i = 0 ; i < sizeof( q1_dheader_t ) / 4 ; i++ ) + ( (int *)q1_header )[i] = LittleLong( ( (int *)q1_header )[i] ); + + if ( q1_header->version != Q1_BSPVERSION ) { + Error( "%s is version %i, not %i", filename, i, Q1_BSPVERSION ); + } + + q1_nummodels = Q1_CopyLump( Q1_LUMP_MODELS, q1_dmodels, sizeof( q1_dmodel_t ) ); + q1_numvertexes = Q1_CopyLump( Q1_LUMP_VERTEXES, q1_dvertexes, sizeof( q1_dvertex_t ) ); + q1_numplanes = Q1_CopyLump( Q1_LUMP_PLANES, q1_dplanes, sizeof( q1_dplane_t ) ); + q1_numleafs = Q1_CopyLump( Q1_LUMP_LEAFS, q1_dleafs, sizeof( q1_dleaf_t ) ); + q1_numnodes = Q1_CopyLump( Q1_LUMP_NODES, q1_dnodes, sizeof( q1_dnode_t ) ); + q1_numtexinfo = Q1_CopyLump( Q1_LUMP_TEXINFO, q1_texinfo, sizeof( q1_texinfo_t ) ); + q1_numclipnodes = Q1_CopyLump( Q1_LUMP_CLIPNODES, q1_dclipnodes, sizeof( q1_dclipnode_t ) ); + q1_numfaces = Q1_CopyLump( Q1_LUMP_FACES, q1_dfaces, sizeof( q1_dface_t ) ); + q1_nummarksurfaces = Q1_CopyLump( Q1_LUMP_MARKSURFACES, q1_dmarksurfaces, sizeof( q1_dmarksurfaces[0] ) ); + q1_numsurfedges = Q1_CopyLump( Q1_LUMP_SURFEDGES, q1_dsurfedges, sizeof( q1_dsurfedges[0] ) ); + q1_numedges = Q1_CopyLump( Q1_LUMP_EDGES, q1_dedges, sizeof( q1_dedge_t ) ); + + q1_texdatasize = Q1_CopyLump( Q1_LUMP_TEXTURES, q1_dtexdata, 1 ); + q1_visdatasize = Q1_CopyLump( Q1_LUMP_VISIBILITY, q1_dvisdata, 1 ); + q1_lightdatasize = Q1_CopyLump( Q1_LUMP_LIGHTING, q1_dlightdata, 1 ); + q1_entdatasize = Q1_CopyLump( Q1_LUMP_ENTITIES, q1_dentdata, 1 ); + + FreeMemory( q1_header ); // everything has been copied out + +// +// swap everything +// + Q1_SwapBSPFile( false ); +} + +//============================================================================ + +FILE *q1_wadfile; +q1_dheader_t q1_outheader; + +void Q1_AddLump( int lumpnum, void *data, int len ) { + q1_lump_t *lump; + + lump = &q1_header->lumps[lumpnum]; + + lump->fileofs = LittleLong( ftell( q1_wadfile ) ); + lump->filelen = LittleLong( len ); + SafeWrite( q1_wadfile, data, ( len + 3 ) & ~3 ); +} + +/* +============= +Q1_WriteBSPFile + +Swaps the bsp file in place, so it should not be referenced again +============= +*/ +void Q1_WriteBSPFile( char *filename ) { + q1_header = &q1_outheader; + memset( q1_header, 0, sizeof( q1_dheader_t ) ); + + Q1_SwapBSPFile( true ); + + q1_header->version = LittleLong( Q1_BSPVERSION ); + + q1_wadfile = SafeOpenWrite( filename ); + SafeWrite( q1_wadfile, q1_header, sizeof( q1_dheader_t ) ); // overwritten later + + Q1_AddLump( Q1_LUMP_PLANES, q1_dplanes, q1_numplanes * sizeof( q1_dplane_t ) ); + Q1_AddLump( Q1_LUMP_LEAFS, q1_dleafs, q1_numleafs * sizeof( q1_dleaf_t ) ); + Q1_AddLump( Q1_LUMP_VERTEXES, q1_dvertexes, q1_numvertexes * sizeof( q1_dvertex_t ) ); + Q1_AddLump( Q1_LUMP_NODES, q1_dnodes, q1_numnodes * sizeof( q1_dnode_t ) ); + Q1_AddLump( Q1_LUMP_TEXINFO, q1_texinfo, q1_numtexinfo * sizeof( q1_texinfo_t ) ); + Q1_AddLump( Q1_LUMP_FACES, q1_dfaces, q1_numfaces * sizeof( q1_dface_t ) ); + Q1_AddLump( Q1_LUMP_CLIPNODES, q1_dclipnodes, q1_numclipnodes * sizeof( q1_dclipnode_t ) ); + Q1_AddLump( Q1_LUMP_MARKSURFACES, q1_dmarksurfaces, q1_nummarksurfaces * sizeof( q1_dmarksurfaces[0] ) ); + Q1_AddLump( Q1_LUMP_SURFEDGES, q1_dsurfedges, q1_numsurfedges * sizeof( q1_dsurfedges[0] ) ); + Q1_AddLump( Q1_LUMP_EDGES, q1_dedges, q1_numedges * sizeof( q1_dedge_t ) ); + Q1_AddLump( Q1_LUMP_MODELS, q1_dmodels, q1_nummodels * sizeof( q1_dmodel_t ) ); + + Q1_AddLump( Q1_LUMP_LIGHTING, q1_dlightdata, q1_lightdatasize ); + Q1_AddLump( Q1_LUMP_VISIBILITY, q1_dvisdata, q1_visdatasize ); + Q1_AddLump( Q1_LUMP_ENTITIES, q1_dentdata, q1_entdatasize ); + Q1_AddLump( Q1_LUMP_TEXTURES, q1_dtexdata, q1_texdatasize ); + + fseek( q1_wadfile, 0, SEEK_SET ); + SafeWrite( q1_wadfile, q1_header, sizeof( q1_dheader_t ) ); + fclose( q1_wadfile ); +} + +//============================================================================ + +/* +============= +Q1_PrintBSPFileSizes + +Dumps info about current file +============= +*/ +void Q1_PrintBSPFileSizes( void ) { + printf( "%5i planes %6i\n" + ,q1_numplanes, (int)( q1_numplanes * sizeof( q1_dplane_t ) ) ); + printf( "%5i vertexes %6i\n" + ,q1_numvertexes, (int)( q1_numvertexes * sizeof( q1_dvertex_t ) ) ); + printf( "%5i nodes %6i\n" + ,q1_numnodes, (int)( q1_numnodes * sizeof( q1_dnode_t ) ) ); + printf( "%5i texinfo %6i\n" + ,q1_numtexinfo, (int)( q1_numtexinfo * sizeof( q1_texinfo_t ) ) ); + printf( "%5i faces %6i\n" + ,q1_numfaces, (int)( q1_numfaces * sizeof( q1_dface_t ) ) ); + printf( "%5i clipnodes %6i\n" + ,q1_numclipnodes, (int)( q1_numclipnodes * sizeof( q1_dclipnode_t ) ) ); + printf( "%5i leafs %6i\n" + ,q1_numleafs, (int)( q1_numleafs * sizeof( q1_dleaf_t ) ) ); + printf( "%5i marksurfaces %6i\n" + ,q1_nummarksurfaces, (int)( q1_nummarksurfaces * sizeof( q1_dmarksurfaces[0] ) ) ); + printf( "%5i surfedges %6i\n" + ,q1_numsurfedges, (int)( q1_numsurfedges * sizeof( q1_dmarksurfaces[0] ) ) ); + printf( "%5i edges %6i\n" + ,q1_numedges, (int)( q1_numedges * sizeof( q1_dedge_t ) ) ); + if ( !q1_texdatasize ) { + printf( " 0 textures 0\n" ); + } else { + printf( "%5i textures %6i\n",( (q1_dmiptexlump_t*)q1_dtexdata )->nummiptex, q1_texdatasize ); + } + printf( " lightdata %6i\n", q1_lightdatasize ); + printf( " visdata %6i\n", q1_visdatasize ); + printf( " entdata %6i\n", q1_entdatasize ); +} //end of the function Q1_PrintBSPFileSizes + + +/* +================ +Q1_ParseEntities + +Parses the dentdata string into entities +================ +*/ +void Q1_ParseEntities( void ) { + script_t *script; + + num_entities = 0; + script = LoadScriptMemory( q1_dentdata, q1_entdatasize, "*Quake1 bsp file" ); + SetScriptFlags( script, SCFL_NOSTRINGWHITESPACES | + SCFL_NOSTRINGESCAPECHARS ); + + while ( ParseEntity( script ) ) + { + } //end while + + FreeScript( script ); +} //end of the function Q1_ParseEntities + + +/* +================ +Q1_UnparseEntities + +Generates the dentdata string from all the entities +================ +*/ +void Q1_UnparseEntities( void ) { + char *buf, *end; + epair_t *ep; + char line[2048]; + int i; + + buf = q1_dentdata; + end = buf; + *end = 0; + + for ( i = 0 ; i < num_entities ; i++ ) + { + ep = entities[i].epairs; + if ( !ep ) { + continue; // ent got removed + + } + strcat( end,"{\n" ); + end += 2; + + for ( ep = entities[i].epairs ; ep ; ep = ep->next ) + { + sprintf( line, "\"%s\" \"%s\"\n", ep->key, ep->value ); + strcat( end, line ); + end += strlen( line ); + } + strcat( end,"}\n" ); + end += 2; + + if ( end > buf + Q1_MAX_MAP_ENTSTRING ) { + Error( "Entity text too long" ); + } + } + q1_entdatasize = end - buf + 1; +} //end of the function Q1_UnparseEntities diff --git a/src/bspc/l_bsp_q1.h b/src/bspc/l_bsp_q1.h new file mode 100644 index 0000000..dcd8b8e --- /dev/null +++ b/src/bspc/l_bsp_q1.h @@ -0,0 +1,282 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + + +// upper design bounds + +#define Q1_MAX_MAP_HULLS 4 + +#define Q1_MAX_MAP_MODELS 256 +#define Q1_MAX_MAP_BRUSHES 4096 +#define Q1_MAX_MAP_ENTITIES 1024 +#define Q1_MAX_MAP_ENTSTRING 65536 + +#define Q1_MAX_MAP_PLANES 8192 +#define Q1_MAX_MAP_NODES 32767 // because negative shorts are contents +#define Q1_MAX_MAP_CLIPNODES 32767 // +#define Q1_MAX_MAP_LEAFS 32767 // +#define Q1_MAX_MAP_VERTS 65535 +#define Q1_MAX_MAP_FACES 65535 +#define Q1_MAX_MAP_MARKSURFACES 65535 +#define Q1_MAX_MAP_TEXINFO 4096 +#define Q1_MAX_MAP_EDGES 256000 +#define Q1_MAX_MAP_SURFEDGES 512000 +#define Q1_MAX_MAP_MIPTEX 0x200000 +#define Q1_MAX_MAP_LIGHTING 0x100000 +#define Q1_MAX_MAP_VISIBILITY 0x100000 + +// key / value pair sizes + +#define MAX_KEY 32 +#define MAX_VALUE 1024 + +//============================================================================= + + +#define Q1_BSPVERSION 29 + +typedef struct +{ + int fileofs, filelen; +} q1_lump_t; + +#define Q1_LUMP_ENTITIES 0 +#define Q1_LUMP_PLANES 1 +#define Q1_LUMP_TEXTURES 2 +#define Q1_LUMP_VERTEXES 3 +#define Q1_LUMP_VISIBILITY 4 +#define Q1_LUMP_NODES 5 +#define Q1_LUMP_TEXINFO 6 +#define Q1_LUMP_FACES 7 +#define Q1_LUMP_LIGHTING 8 +#define Q1_LUMP_CLIPNODES 9 +#define Q1_LUMP_LEAFS 10 +#define Q1_LUMP_MARKSURFACES 11 +#define Q1_LUMP_EDGES 12 +#define Q1_LUMP_SURFEDGES 13 +#define Q1_LUMP_MODELS 14 + +#define Q1_HEADER_LUMPS 15 + +typedef struct +{ + float mins[3], maxs[3]; + float origin[3]; + int headnode[Q1_MAX_MAP_HULLS]; + int visleafs; // not including the solid leaf 0 + int firstface, numfaces; +} q1_dmodel_t; + +typedef struct +{ + int version; + q1_lump_t lumps[Q1_HEADER_LUMPS]; +} q1_dheader_t; + +typedef struct +{ + int nummiptex; + int dataofs[4]; // [nummiptex] +} q1_dmiptexlump_t; + +#define MIPLEVELS 4 +typedef struct q1_miptex_s +{ + char name[16]; + unsigned width, height; + unsigned offsets[MIPLEVELS]; // four mip maps stored +} q1_miptex_t; + + +typedef struct +{ + float point[3]; +} q1_dvertex_t; + + +// 0-2 are axial planes +#define PLANE_X 0 +#define PLANE_Y 1 +#define PLANE_Z 2 + +// 3-5 are non-axial planes snapped to the nearest +#define PLANE_ANYX 3 +#define PLANE_ANYY 4 +#define PLANE_ANYZ 5 + +typedef struct +{ + float normal[3]; + float dist; + int type; // PLANE_X - PLANE_ANYZ ?remove? trivial to regenerate +} q1_dplane_t; + + + +#define Q1_CONTENTS_EMPTY -1 +#define Q1_CONTENTS_SOLID -2 +#define Q1_CONTENTS_WATER -3 +#define Q1_CONTENTS_SLIME -4 +#define Q1_CONTENTS_LAVA -5 +#define Q1_CONTENTS_SKY -6 + +// !!! if this is changed, it must be changed in asm_i386.h too !!! +typedef struct +{ + int planenum; + short children[2]; // negative numbers are -(leafs+1), not nodes + short mins[3]; // for sphere culling + short maxs[3]; + unsigned short firstface; + unsigned short numfaces; // counting both sides +} q1_dnode_t; + +typedef struct +{ + int planenum; + short children[2]; // negative numbers are contents +} q1_dclipnode_t; + + +typedef struct q1_texinfo_s +{ + float vecs[2][4]; // [s/t][xyz offset] + int miptex; + int flags; +} q1_texinfo_t; +#define TEX_SPECIAL 1 // sky or slime, no lightmap or 256 subdivision + +// note that edge 0 is never used, because negative edge nums are used for +// counterclockwise use of the edge in a face +typedef struct +{ + unsigned short v[2]; // vertex numbers +} q1_dedge_t; + +#define MAXLIGHTMAPS 4 +typedef struct +{ + short planenum; + short side; + + int firstedge; // we must support > 64k edges + short numedges; + short texinfo; + +// lighting info + byte styles[MAXLIGHTMAPS]; + int lightofs; // start of [numstyles*surfsize] samples +} q1_dface_t; + + + +#define AMBIENT_WATER 0 +#define AMBIENT_SKY 1 +#define AMBIENT_SLIME 2 +#define AMBIENT_LAVA 3 + +#define NUM_AMBIENTS 4 // automatic ambient sounds + +// leaf 0 is the generic Q1_CONTENTS_SOLID leaf, used for all solid areas +// all other leafs need visibility info +typedef struct +{ + int contents; + int visofs; // -1 = no visibility info + + short mins[3]; // for frustum culling + short maxs[3]; + + unsigned short firstmarksurface; + unsigned short nummarksurfaces; + + byte ambient_level[NUM_AMBIENTS]; +} q1_dleaf_t; + +//============================================================================ + +#ifndef QUAKE_GAME + +// the utilities get to be lazy and just use large static arrays + +extern int q1_nummodels; +extern q1_dmodel_t *q1_dmodels; //[MAX_MAP_MODELS]; + +extern int q1_visdatasize; +extern byte *q1_dvisdata; //[MAX_MAP_VISIBILITY]; + +extern int q1_lightdatasize; +extern byte *q1_dlightdata; //[MAX_MAP_LIGHTING]; + +extern int q1_texdatasize; +extern byte *q1_dtexdata; //[MAX_MAP_MIPTEX]; // (dmiptexlump_t) + +extern int q1_entdatasize; +extern char *q1_dentdata; //[MAX_MAP_ENTSTRING]; + +extern int q1_numleafs; +extern q1_dleaf_t *q1_dleafs; //[MAX_MAP_LEAFS]; + +extern int q1_numplanes; +extern q1_dplane_t *q1_dplanes; //[MAX_MAP_PLANES]; + +extern int q1_numvertexes; +extern q1_dvertex_t *q1_dvertexes; //[MAX_MAP_VERTS]; + +extern int q1_numnodes; +extern q1_dnode_t *q1_dnodes; //[MAX_MAP_NODES]; + +extern int q1_numtexinfo; +extern q1_texinfo_t *q1_texinfo; //[MAX_MAP_TEXINFO]; + +extern int q1_numfaces; +extern q1_dface_t *q1_dfaces; //[MAX_MAP_FACES]; + +extern int q1_numclipnodes; +extern q1_dclipnode_t *q1_dclipnodes; //[MAX_MAP_CLIPNODES]; + +extern int q1_numedges; +extern q1_dedge_t *q1_dedges; //[MAX_MAP_EDGES]; + +extern int q1_nummarksurfaces; +extern unsigned short *q1_dmarksurfaces; //[MAX_MAP_MARKSURFACES]; + +extern int q1_numsurfedges; +extern int *q1_dsurfedges; //[MAX_MAP_SURFEDGES]; + + +void Q1_AllocMaxBSP( void ); +void Q1_FreeMaxBSP( void ); +void Q1_LoadBSPFile( char *filename, int offset, int length ); +void Q1_WriteBSPFile( char *filename ); +void Q1_PrintBSPFileSizes( void ); +void Q1_ParseEntities( void ); +void Q1_UnparseEntities( void ); + +#endif diff --git a/src/bspc/l_bsp_q2.c b/src/bspc/l_bsp_q2.c new file mode 100644 index 0000000..f3c0d20 --- /dev/null +++ b/src/bspc/l_bsp_q2.c @@ -0,0 +1,1139 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +#include "l_cmd.h" +#include "l_math.h" +#include "l_mem.h" +#include "l_log.h" +#include "l_poly.h" +#include "../botlib/l_script.h" +#include "q2files.h" +#include "l_bsp_q2.h" +#include "l_bsp_ent.h" + +#define q2_dmodel_t dmodel_t +#define q2_lump_t lump_t +#define q2_dheader_t dheader_t +#define q2_dmodel_t dmodel_t +#define q2_dvertex_t dvertex_t +#define q2_dplane_t dplane_t +#define q2_dnode_t dnode_t +#define q2_texinfo_t texinfo_t +#define q2_dedge_t dedge_t +#define q2_dface_t dface_t +#define q2_dleaf_t dleaf_t +#define q2_dbrushside_t dbrushside_t +#define q2_dbrush_t dbrush_t +#define q2_dvis_t dvis_t +#define q2_dareaportal_t dareaportal_t +#define q2_darea_t darea_t + +#define q2_nummodels nummodels +#define q2_dmodels dmodels +#define q2_numleafs numleafs +#define q2_dleafs dleafs +#define q2_numplanes numplanes +#define q2_dplanes dplanes +#define q2_numvertexes numvertexes +#define q2_dvertexes dvertexes +#define q2_numnodes numnodes +#define q2_dnodes dnodes +#define q2_numtexinfo numtexinfo +#define q2_texinfo texinfo +#define q2_numfaces numfaces +#define q2_dfaces dfaces +#define q2_numedges numedges +#define q2_dedges dedges +#define q2_numleaffaces numleaffaces +#define q2_dleaffaces dleaffaces +#define q2_numleafbrushes numleafbrushes +#define q2_dleafbrushes dleafbrushes +#define q2_dsurfedges dsurfedges +#define q2_numbrushes numbrushes +#define q2_dbrushes dbrushes +#define q2_numbrushsides numbrushsides +#define q2_dbrushsides dbrushsides +#define q2_numareas numareas +#define q2_dareas dareas +#define q2_numareaportals numareaportals +#define q2_dareaportals dareaportals + +void GetLeafNums( void ); + +//============================================================================= + +int nummodels; +dmodel_t *dmodels; //[MAX_MAP_MODELS]; + +int visdatasize; +byte *dvisdata; //[MAX_MAP_VISIBILITY]; +dvis_t *dvis; // = (dvis_t *)dvisdata; + +int lightdatasize; +byte *dlightdata; //[MAX_MAP_LIGHTING]; + +int entdatasize; +char *dentdata; //[MAX_MAP_ENTSTRING]; + +int numleafs; +dleaf_t *dleafs; //[MAX_MAP_LEAFS]; + +int numplanes; +dplane_t *dplanes; //[MAX_MAP_PLANES]; + +int numvertexes; +dvertex_t *dvertexes; //[MAX_MAP_VERTS]; + +int numnodes; +dnode_t *dnodes; //[MAX_MAP_NODES]; + +//NOTE: must be static for q2 .map to q2 .bsp +int numtexinfo; +texinfo_t texinfo[MAX_MAP_TEXINFO]; + +int numfaces; +dface_t *dfaces; //[MAX_MAP_FACES]; + +int numedges; +dedge_t *dedges; //[MAX_MAP_EDGES]; + +int numleaffaces; +unsigned short *dleaffaces; //[MAX_MAP_LEAFFACES]; + +int numleafbrushes; +unsigned short *dleafbrushes; //[MAX_MAP_LEAFBRUSHES]; + +int numsurfedges; +int *dsurfedges; //[MAX_MAP_SURFEDGES]; + +int numbrushes; +dbrush_t *dbrushes; //[MAX_MAP_BRUSHES]; + +int numbrushsides; +dbrushside_t *dbrushsides; //[MAX_MAP_BRUSHSIDES]; + +int numareas; +darea_t *dareas; //[MAX_MAP_AREAS]; + +int numareaportals; +dareaportal_t *dareaportals; //[MAX_MAP_AREAPORTALS]; + +#define MAX_MAP_DPOP 256 +byte dpop[MAX_MAP_DPOP]; + +// +char brushsidetextured[MAX_MAP_BRUSHSIDES]; + +//#ifdef ME + +int bspallocated = false; +int allocatedbspmem = 0; + +void Q2_AllocMaxBSP( void ) { + //models + nummodels = 0; + dmodels = (dmodel_t *) GetClearedMemory( MAX_MAP_MODELS * sizeof( dmodel_t ) ); + allocatedbspmem += MAX_MAP_MODELS * sizeof( dmodel_t ); + //vis data + visdatasize = 0; + dvisdata = (byte *) GetClearedMemory( MAX_MAP_VISIBILITY * sizeof( byte ) ); + dvis = (dvis_t *) dvisdata; + allocatedbspmem += MAX_MAP_VISIBILITY * sizeof( byte ); + //light data + lightdatasize = 0; + dlightdata = (byte *) GetClearedMemory( MAX_MAP_LIGHTING * sizeof( byte ) ); + allocatedbspmem += MAX_MAP_LIGHTING * sizeof( byte ); + //entity data + entdatasize = 0; + dentdata = (char *) GetClearedMemory( MAX_MAP_ENTSTRING * sizeof( char ) ); + allocatedbspmem += MAX_MAP_ENTSTRING * sizeof( char ); + //leafs + numleafs = 0; + dleafs = (dleaf_t *) GetClearedMemory( MAX_MAP_LEAFS * sizeof( dleaf_t ) ); + allocatedbspmem += MAX_MAP_LEAFS * sizeof( dleaf_t ); + //planes + numplanes = 0; + dplanes = (dplane_t *) GetClearedMemory( MAX_MAP_PLANES * sizeof( dplane_t ) ); + allocatedbspmem += MAX_MAP_PLANES * sizeof( dplane_t ); + //vertexes + numvertexes = 0; + dvertexes = (dvertex_t *) GetClearedMemory( MAX_MAP_VERTS * sizeof( dvertex_t ) ); + allocatedbspmem += MAX_MAP_VERTS * sizeof( dvertex_t ); + //nodes + numnodes = 0; + dnodes = (dnode_t *) GetClearedMemory( MAX_MAP_NODES * sizeof( dnode_t ) ); + allocatedbspmem += MAX_MAP_NODES * sizeof( dnode_t ); + /* + //texture info + numtexinfo = 0; + texinfo = (texinfo_t *) GetClearedMemory(MAX_MAP_TEXINFO * sizeof(texinfo_t)); + allocatedbspmem += MAX_MAP_TEXINFO * sizeof(texinfo_t); + // + */ + //faces + numfaces = 0; + dfaces = (dface_t *) GetClearedMemory( MAX_MAP_FACES * sizeof( dface_t ) ); + allocatedbspmem += MAX_MAP_FACES * sizeof( dface_t ); + //edges + numedges = 0; + dedges = (dedge_t *) GetClearedMemory( MAX_MAP_EDGES * sizeof( dedge_t ) ); + allocatedbspmem += MAX_MAP_EDGES * sizeof( dedge_t ); + //leaf faces + numleaffaces = 0; + dleaffaces = (unsigned short *) GetClearedMemory( MAX_MAP_LEAFFACES * sizeof( unsigned short ) ); + allocatedbspmem += MAX_MAP_LEAFFACES * sizeof( unsigned short ); + //leaf brushes + numleafbrushes = 0; + dleafbrushes = (unsigned short *) GetClearedMemory( MAX_MAP_LEAFBRUSHES * sizeof( unsigned short ) ); + allocatedbspmem += MAX_MAP_LEAFBRUSHES * sizeof( unsigned short ); + //surface edges + numsurfedges = 0; + dsurfedges = (int *) GetClearedMemory( MAX_MAP_SURFEDGES * sizeof( int ) ); + allocatedbspmem += MAX_MAP_SURFEDGES * sizeof( int ); + //brushes + numbrushes = 0; + dbrushes = (dbrush_t *) GetClearedMemory( MAX_MAP_BRUSHES * sizeof( dbrush_t ) ); + allocatedbspmem += MAX_MAP_BRUSHES * sizeof( dbrush_t ); + //brushsides + numbrushsides = 0; + dbrushsides = (dbrushside_t *) GetClearedMemory( MAX_MAP_BRUSHSIDES * sizeof( dbrushside_t ) ); + allocatedbspmem += MAX_MAP_BRUSHSIDES * sizeof( dbrushside_t ); + //areas + numareas = 0; + dareas = (darea_t *) GetClearedMemory( MAX_MAP_AREAS * sizeof( darea_t ) ); + allocatedbspmem += MAX_MAP_AREAS * sizeof( darea_t ); + //area portals + numareaportals = 0; + dareaportals = (dareaportal_t *) GetClearedMemory( MAX_MAP_AREAPORTALS * sizeof( dareaportal_t ) ); + allocatedbspmem += MAX_MAP_AREAPORTALS * sizeof( dareaportal_t ); + //print allocated memory + Log_Print( "allocated " ); + PrintMemorySize( allocatedbspmem ); + Log_Print( " of BSP memory\n" ); +} //end of the function Q2_AllocMaxBSP + +void Q2_FreeMaxBSP( void ) { + //models + nummodels = 0; + FreeMemory( dmodels ); + dmodels = NULL; + //vis data + visdatasize = 0; + FreeMemory( dvisdata ); + dvisdata = NULL; + dvis = NULL; + //light data + lightdatasize = 0; + FreeMemory( dlightdata ); + dlightdata = NULL; + //entity data + entdatasize = 0; + FreeMemory( dentdata ); + dentdata = NULL; + //leafs + numleafs = 0; + FreeMemory( dleafs ); + dleafs = NULL; + //planes + numplanes = 0; + FreeMemory( dplanes ); + dplanes = NULL; + //vertexes + numvertexes = 0; + FreeMemory( dvertexes ); + dvertexes = NULL; + //nodes + numnodes = 0; + FreeMemory( dnodes ); + dnodes = NULL; + /* + //texture info + numtexinfo = 0; + FreeMemory(texinfo); + texinfo = NULL; + // + */ + //faces + numfaces = 0; + FreeMemory( dfaces ); + dfaces = NULL; + //edges + numedges = 0; + FreeMemory( dedges ); + dedges = NULL; + //leaf faces + numleaffaces = 0; + FreeMemory( dleaffaces ); + dleaffaces = NULL; + //leaf brushes + numleafbrushes = 0; + FreeMemory( dleafbrushes ); + dleafbrushes = NULL; + //surface edges + numsurfedges = 0; + FreeMemory( dsurfedges ); + dsurfedges = NULL; + //brushes + numbrushes = 0; + FreeMemory( dbrushes ); + dbrushes = NULL; + //brushsides + numbrushsides = 0; + FreeMemory( dbrushsides ); + dbrushsides = NULL; + //areas + numareas = 0; + FreeMemory( dareas ); + dareas = NULL; + //area portals + numareaportals = 0; + FreeMemory( dareaportals ); + dareaportals = NULL; + // + Log_Print( "freed " ); + PrintMemorySize( allocatedbspmem ); + Log_Print( " of BSP memory\n" ); + allocatedbspmem = 0; +} //end of the function Q2_FreeMaxBSP + +#define WCONVEX_EPSILON 0.5 + +int InsideWinding( winding_t *w, vec3_t point, int planenum ) { + int i; + float dist; + vec_t *v1, *v2; + vec3_t normal, edgevec; + dplane_t *plane; + + for ( i = 1; i <= w->numpoints; i++ ) + { + v1 = w->p[i % w->numpoints]; + v2 = w->p[( i + 1 ) % w->numpoints]; + + VectorSubtract( v2, v1, edgevec ); + plane = &dplanes[planenum]; + CrossProduct( plane->normal, edgevec, normal ); + VectorNormalize( normal ); + dist = DotProduct( normal, v1 ); + // + if ( DotProduct( normal, point ) - dist > WCONVEX_EPSILON ) { + return false; + } + } //end for + return true; +} //end of the function InsideWinding + +int InsideFace( dface_t *face, vec3_t point ) { + int i, edgenum, side; + float dist; + vec_t *v1, *v2; + vec3_t normal, edgevec; + dplane_t *plane; + + for ( i = 0; i < face->numedges; i++ ) + { + //get the first and second vertex of the edge + edgenum = dsurfedges[face->firstedge + i]; + side = edgenum < 0; + v1 = dvertexes[dedges[abs( edgenum )].v[side]].point; + v2 = dvertexes[dedges[abs( edgenum )].v[!side]].point; + //create a plane through the edge vector, orthogonal to the face plane + //and with the normal vector pointing out of the face + VectorSubtract( v1, v2, edgevec ); + plane = &dplanes[face->planenum]; + CrossProduct( plane->normal, edgevec, normal ); + VectorNormalize( normal ); + dist = DotProduct( normal, v1 ); + // + if ( DotProduct( normal, point ) - dist > WCONVEX_EPSILON ) { + return false; + } + } //end for + return true; +} //end of the function InsideFace +//=========================================================================== +// returns the amount the face and the winding overlap +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float Q2_FaceOnWinding( q2_dface_t *face, winding_t *winding ) { + int i, edgenum, side; + float dist, area; + q2_dplane_t plane; + vec_t *v1, *v2; + vec3_t normal, edgevec; + winding_t *w; + + // + w = CopyWinding( winding ); + memcpy( &plane, &q2_dplanes[face->planenum], sizeof( q2_dplane_t ) ); + //check on which side of the plane the face is + if ( face->side ) { + VectorNegate( plane.normal, plane.normal ); + plane.dist = -plane.dist; + } //end if + for ( i = 0; i < face->numedges && w; i++ ) + { + //get the first and second vertex of the edge + edgenum = q2_dsurfedges[face->firstedge + i]; + side = edgenum > 0; + //if the face plane is flipped + v1 = q2_dvertexes[q2_dedges[abs( edgenum )].v[side]].point; + v2 = q2_dvertexes[q2_dedges[abs( edgenum )].v[!side]].point; + //create a plane through the edge vector, orthogonal to the face plane + //and with the normal vector pointing inward + VectorSubtract( v1, v2, edgevec ); + CrossProduct( edgevec, plane.normal, normal ); + VectorNormalize( normal ); + dist = DotProduct( normal, v1 ); + // + ChopWindingInPlace( &w, normal, dist, -0.1 ); //CLIP_EPSILON + } //end for + if ( w ) { + area = WindingArea( w ); + FreeWinding( w ); + return area; + } //end if + return 0; +} //end of the function Q2_FaceOnWinding +//=========================================================================== +// creates a winding for the given brush side on the given brush +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +winding_t *Q2_BrushSideWinding( dbrush_t *brush, dbrushside_t *baseside ) { + int i; + dplane_t *baseplane, *plane; + winding_t *w; + dbrushside_t *side; + + //create a winding for the brush side with the given planenumber + baseplane = &dplanes[baseside->planenum]; + w = BaseWindingForPlane( baseplane->normal, baseplane->dist ); + for ( i = 0; i < brush->numsides && w; i++ ) + { + side = &dbrushsides[brush->firstside + i]; + //don't chop with the base plane + if ( side->planenum == baseside->planenum ) { + continue; + } + //also don't use planes that are almost equal + plane = &dplanes[side->planenum]; + if ( DotProduct( baseplane->normal, plane->normal ) > 0.999 + && fabs( baseplane->dist - plane->dist ) < 0.01 ) { + continue; + } + // + plane = &dplanes[side->planenum ^ 1]; + ChopWindingInPlace( &w, plane->normal, plane->dist, -0.1 ); //CLIP_EPSILON); + } //end for + return w; +} //end of the function Q2_BrushSideWinding +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int Q2_HintSkipBrush( dbrush_t *brush ) { + int j; + dbrushside_t *brushside; + + for ( j = 0; j < brush->numsides; j++ ) + { + brushside = &dbrushsides[brush->firstside + j]; + if ( brushside->texinfo > 0 ) { + if ( texinfo[brushside->texinfo].flags & ( SURF_SKIP | SURF_HINT ) ) { + return true; + } //end if + } //end if + } //end for + return false; +} //end of the function Q2_HintSkipBrush +//=========================================================================== +// fix screwed brush texture references +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean WindingIsTiny( winding_t *w ); + +void Q2_FixTextureReferences( void ) { + int i, j, k, we; + dbrushside_t *brushside; + dbrush_t *brush; + dface_t *face; + winding_t *w; + + memset( brushsidetextured, false, MAX_MAP_BRUSHSIDES ); + //go over all the brushes + for ( i = 0; i < numbrushes; i++ ) + { + brush = &dbrushes[i]; + //hint brushes are not textured + if ( Q2_HintSkipBrush( brush ) ) { + continue; + } + //go over all the sides of the brush + for ( j = 0; j < brush->numsides; j++ ) + { + brushside = &dbrushsides[brush->firstside + j]; + // + w = Q2_BrushSideWinding( brush, brushside ); + if ( !w ) { + brushsidetextured[brush->firstside + j] = true; + continue; + } //end if + else + { + //RemoveEqualPoints(w, 0.2); + if ( WindingIsTiny( w ) ) { + FreeWinding( w ); + brushsidetextured[brush->firstside + j] = true; + continue; + } //end if + else + { + we = WindingError( w ); + if ( we == WE_NOTENOUGHPOINTS + || we == WE_SMALLAREA + || we == WE_POINTBOGUSRANGE +// || we == WE_NONCONVEX + ) { + FreeWinding( w ); + brushsidetextured[brush->firstside + j] = true; + continue; + } //end if + } //end else + } //end else + if ( WindingArea( w ) < 20 ) { + brushsidetextured[brush->firstside + j] = true; + } //end if + //find a face for texturing this brush + for ( k = 0; k < numfaces; k++ ) + { + face = &dfaces[k]; + //if the face is in the same plane as the brush side + if ( ( face->planenum & ~1 ) != ( brushside->planenum & ~1 ) ) { + continue; + } + //if the face is partly or totally on the brush side + if ( Q2_FaceOnWinding( face, w ) ) { + brushside->texinfo = face->texinfo; + brushsidetextured[brush->firstside + j] = true; + break; + } //end if + } //end for + FreeWinding( w ); + } //end for + } //end for +} //end of the function Q2_FixTextureReferences*/ + +//#endif //ME + + +/* +=============== +CompressVis + +=============== +*/ +int Q2_CompressVis( byte *vis, byte *dest ) { + int j; + int rep; + int visrow; + byte *dest_p; + + dest_p = dest; +// visrow = (r_numvisleafs + 7)>>3; + visrow = ( dvis->numclusters + 7 ) >> 3; + + for ( j = 0 ; j < visrow ; j++ ) + { + *dest_p++ = vis[j]; + if ( vis[j] ) { + continue; + } + + rep = 1; + for ( j++; j < visrow ; j++ ) + if ( vis[j] || rep == 255 ) { + break; + } else { + rep++; + } + *dest_p++ = rep; + j--; + } + + return dest_p - dest; +} + + +/* +=================== +DecompressVis +=================== +*/ +void Q2_DecompressVis( byte *in, byte *decompressed ) { + int c; + byte *out; + int row; + +// row = (r_numvisleafs+7)>>3; + row = ( dvis->numclusters + 7 ) >> 3; + out = decompressed; + + do + { + if ( *in ) { + *out++ = *in++; + continue; + } + + c = in[1]; + if ( !c ) { + Error( "DecompressVis: 0 repeat" ); + } + in += 2; + while ( c ) + { + *out++ = 0; + c--; + } + } while ( out - decompressed < row ); +} + +//============================================================================= + +/* +============= +SwapBSPFile + +Byte swaps all data in a bsp file. +============= +*/ +void Q2_SwapBSPFile( qboolean todisk ) { + int i, j; + dmodel_t *d; + + +// models + for ( i = 0 ; i < nummodels ; i++ ) + { + d = &dmodels[i]; + + d->firstface = LittleLong( d->firstface ); + d->numfaces = LittleLong( d->numfaces ); + d->headnode = LittleLong( d->headnode ); + + for ( j = 0 ; j < 3 ; j++ ) + { + d->mins[j] = LittleFloat( d->mins[j] ); + d->maxs[j] = LittleFloat( d->maxs[j] ); + d->origin[j] = LittleFloat( d->origin[j] ); + } + } + +// +// vertexes +// + for ( i = 0 ; i < numvertexes ; i++ ) + { + for ( j = 0 ; j < 3 ; j++ ) + dvertexes[i].point[j] = LittleFloat( dvertexes[i].point[j] ); + } + +// +// planes +// + for ( i = 0 ; i < numplanes ; i++ ) + { + for ( j = 0 ; j < 3 ; j++ ) + dplanes[i].normal[j] = LittleFloat( dplanes[i].normal[j] ); + dplanes[i].dist = LittleFloat( dplanes[i].dist ); + dplanes[i].type = LittleLong( dplanes[i].type ); + } + +// +// texinfos +// + for ( i = 0 ; i < numtexinfo ; i++ ) + { + for ( j = 0 ; j < 8 ; j++ ) + texinfo[i].vecs[0][j] = LittleFloat( texinfo[i].vecs[0][j] ); + texinfo[i].flags = LittleLong( texinfo[i].flags ); + texinfo[i].value = LittleLong( texinfo[i].value ); + texinfo[i].nexttexinfo = LittleLong( texinfo[i].nexttexinfo ); + } + +// +// faces +// + for ( i = 0 ; i < numfaces ; i++ ) + { + dfaces[i].texinfo = LittleShort( dfaces[i].texinfo ); + dfaces[i].planenum = LittleShort( dfaces[i].planenum ); + dfaces[i].side = LittleShort( dfaces[i].side ); + dfaces[i].lightofs = LittleLong( dfaces[i].lightofs ); + dfaces[i].firstedge = LittleLong( dfaces[i].firstedge ); + dfaces[i].numedges = LittleShort( dfaces[i].numedges ); + } + +// +// nodes +// + for ( i = 0 ; i < numnodes ; i++ ) + { + dnodes[i].planenum = LittleLong( dnodes[i].planenum ); + for ( j = 0 ; j < 3 ; j++ ) + { + dnodes[i].mins[j] = LittleShort( dnodes[i].mins[j] ); + dnodes[i].maxs[j] = LittleShort( dnodes[i].maxs[j] ); + } + dnodes[i].children[0] = LittleLong( dnodes[i].children[0] ); + dnodes[i].children[1] = LittleLong( dnodes[i].children[1] ); + dnodes[i].firstface = LittleShort( dnodes[i].firstface ); + dnodes[i].numfaces = LittleShort( dnodes[i].numfaces ); + } + +// +// leafs +// + for ( i = 0 ; i < numleafs ; i++ ) + { + dleafs[i].contents = LittleLong( dleafs[i].contents ); + dleafs[i].cluster = LittleShort( dleafs[i].cluster ); + dleafs[i].area = LittleShort( dleafs[i].area ); + for ( j = 0 ; j < 3 ; j++ ) + { + dleafs[i].mins[j] = LittleShort( dleafs[i].mins[j] ); + dleafs[i].maxs[j] = LittleShort( dleafs[i].maxs[j] ); + } + + dleafs[i].firstleafface = LittleShort( dleafs[i].firstleafface ); + dleafs[i].numleaffaces = LittleShort( dleafs[i].numleaffaces ); + dleafs[i].firstleafbrush = LittleShort( dleafs[i].firstleafbrush ); + dleafs[i].numleafbrushes = LittleShort( dleafs[i].numleafbrushes ); + } + +// +// leaffaces +// + for ( i = 0 ; i < numleaffaces ; i++ ) + dleaffaces[i] = LittleShort( dleaffaces[i] ); + +// +// leafbrushes +// + for ( i = 0 ; i < numleafbrushes ; i++ ) + dleafbrushes[i] = LittleShort( dleafbrushes[i] ); + +// +// surfedges +// + for ( i = 0 ; i < numsurfedges ; i++ ) + dsurfedges[i] = LittleLong( dsurfedges[i] ); + +// +// edges +// + for ( i = 0 ; i < numedges ; i++ ) + { + dedges[i].v[0] = LittleShort( dedges[i].v[0] ); + dedges[i].v[1] = LittleShort( dedges[i].v[1] ); + } + +// +// brushes +// + for ( i = 0 ; i < numbrushes ; i++ ) + { + dbrushes[i].firstside = LittleLong( dbrushes[i].firstside ); + dbrushes[i].numsides = LittleLong( dbrushes[i].numsides ); + dbrushes[i].contents = LittleLong( dbrushes[i].contents ); + } + +// +// areas +// + for ( i = 0 ; i < numareas ; i++ ) + { + dareas[i].numareaportals = LittleLong( dareas[i].numareaportals ); + dareas[i].firstareaportal = LittleLong( dareas[i].firstareaportal ); + } + +// +// areasportals +// + for ( i = 0 ; i < numareaportals ; i++ ) + { + dareaportals[i].portalnum = LittleLong( dareaportals[i].portalnum ); + dareaportals[i].otherarea = LittleLong( dareaportals[i].otherarea ); + } + +// +// brushsides +// + for ( i = 0 ; i < numbrushsides ; i++ ) + { + dbrushsides[i].planenum = LittleShort( dbrushsides[i].planenum ); + dbrushsides[i].texinfo = LittleShort( dbrushsides[i].texinfo ); + } + +// +// visibility +// + if ( todisk ) { + j = dvis->numclusters; + } else { + j = LittleLong( dvis->numclusters ); + } + dvis->numclusters = LittleLong( dvis->numclusters ); + for ( i = 0 ; i < j ; i++ ) + { + dvis->bitofs[i][0] = LittleLong( dvis->bitofs[i][0] ); + dvis->bitofs[i][1] = LittleLong( dvis->bitofs[i][1] ); + } +} //end of the function Q2_SwapBSPFile + + +dheader_t *header; + +int Q2_CopyLump( int lump, void *dest, int size, int maxsize ) { + int length, ofs; + + length = header->lumps[lump].filelen; + ofs = header->lumps[lump].fileofs; + + if ( length % size ) { + Error( "LoadBSPFile: odd lump size" ); + } + + if ( ( length / size ) > maxsize ) { + Error( "Q2_LoadBSPFile: exceeded max size for lump %d size %d > maxsize %d\n", lump, ( length / size ), maxsize ); + } + + memcpy( dest, (byte *)header + ofs, length ); + + return length / size; +} //end of the function Q2_CopyLump + +/* +============= +LoadBSPFile +============= +*/ +void Q2_LoadBSPFile( char *filename, int offset, int length ) { + int i; + +// +// load the file header +// + LoadFile( filename, (void **)&header, offset, length ); + +// swap the header + for ( i = 0 ; i < sizeof( dheader_t ) / 4 ; i++ ) + ( (int *)header )[i] = LittleLong( ( (int *)header )[i] ); + + if ( header->ident != IDBSPHEADER ) { + Error( "%s is not a IBSP file", filename ); + } + if ( header->version != BSPVERSION ) { + Error( "%s is version %i, not %i", filename, header->version, BSPVERSION ); + } + + nummodels = Q2_CopyLump( LUMP_MODELS, dmodels, sizeof( dmodel_t ), MAX_MAP_MODELS ); + numvertexes = Q2_CopyLump( LUMP_VERTEXES, dvertexes, sizeof( dvertex_t ), MAX_MAP_VERTS ); + numplanes = Q2_CopyLump( LUMP_PLANES, dplanes, sizeof( dplane_t ), MAX_MAP_PLANES ); + numleafs = Q2_CopyLump( LUMP_LEAFS, dleafs, sizeof( dleaf_t ), MAX_MAP_LEAFS ); + numnodes = Q2_CopyLump( LUMP_NODES, dnodes, sizeof( dnode_t ), MAX_MAP_NODES ); + numtexinfo = Q2_CopyLump( LUMP_TEXINFO, texinfo, sizeof( texinfo_t ), MAX_MAP_TEXINFO ); + numfaces = Q2_CopyLump( LUMP_FACES, dfaces, sizeof( dface_t ), MAX_MAP_FACES ); + numleaffaces = Q2_CopyLump( LUMP_LEAFFACES, dleaffaces, sizeof( dleaffaces[0] ), MAX_MAP_LEAFFACES ); + numleafbrushes = Q2_CopyLump( LUMP_LEAFBRUSHES, dleafbrushes, sizeof( dleafbrushes[0] ), MAX_MAP_LEAFBRUSHES ); + numsurfedges = Q2_CopyLump( LUMP_SURFEDGES, dsurfedges, sizeof( dsurfedges[0] ), MAX_MAP_SURFEDGES ); + numedges = Q2_CopyLump( LUMP_EDGES, dedges, sizeof( dedge_t ), MAX_MAP_EDGES ); + numbrushes = Q2_CopyLump( LUMP_BRUSHES, dbrushes, sizeof( dbrush_t ), MAX_MAP_BRUSHES ); + numbrushsides = Q2_CopyLump( LUMP_BRUSHSIDES, dbrushsides, sizeof( dbrushside_t ), MAX_MAP_BRUSHSIDES ); + numareas = Q2_CopyLump( LUMP_AREAS, dareas, sizeof( darea_t ), MAX_MAP_AREAS ); + numareaportals = Q2_CopyLump( LUMP_AREAPORTALS, dareaportals, sizeof( dareaportal_t ), MAX_MAP_AREAPORTALS ); + + visdatasize = Q2_CopyLump( LUMP_VISIBILITY, dvisdata, 1, MAX_MAP_VISIBILITY ); + lightdatasize = Q2_CopyLump( LUMP_LIGHTING, dlightdata, 1, MAX_MAP_LIGHTING ); + entdatasize = Q2_CopyLump( LUMP_ENTITIES, dentdata, 1, MAX_MAP_ENTSTRING ); + + Q2_CopyLump( LUMP_POP, dpop, 1, MAX_MAP_DPOP ); + + FreeMemory( header ); // everything has been copied out + +// +// swap everything +// + Q2_SwapBSPFile( false ); + + Q2_FixTextureReferences(); +} //end of the function Q2_LoadBSPFile + + +/* +============= +LoadBSPFileTexinfo + +Only loads the texinfo lump, so qdata can scan for textures +============= +*/ +void Q2_LoadBSPFileTexinfo( char *filename ) { + int i; + FILE *f; + int length, ofs; + + header = GetMemory( sizeof( dheader_t ) ); + + f = fopen( filename, "rb" ); + fread( header, sizeof( dheader_t ), 1, f ); + +// swap the header + for ( i = 0 ; i < sizeof( dheader_t ) / 4 ; i++ ) + ( (int *)header )[i] = LittleLong( ( (int *)header )[i] ); + + if ( header->ident != IDBSPHEADER ) { + Error( "%s is not a IBSP file", filename ); + } + if ( header->version != BSPVERSION ) { + Error( "%s is version %i, not %i", filename, header->version, BSPVERSION ); + } + + + length = header->lumps[LUMP_TEXINFO].filelen; + ofs = header->lumps[LUMP_TEXINFO].fileofs; + + fseek( f, ofs, SEEK_SET ); + fread( texinfo, length, 1, f ); + fclose( f ); + + numtexinfo = length / sizeof( texinfo_t ); + + FreeMemory( header ); // everything has been copied out + + Q2_SwapBSPFile( false ); +} //end of the function Q2_LoadBSPFileTexinfo + + +//============================================================================ + +FILE *wadfile; +dheader_t outheader; + +void Q2_AddLump( int lumpnum, void *data, int len ) { + lump_t *lump; + + lump = &header->lumps[lumpnum]; + + lump->fileofs = LittleLong( ftell( wadfile ) ); + lump->filelen = LittleLong( len ); + SafeWrite( wadfile, data, ( len + 3 ) & ~3 ); +} //end of the function Q2_AddLump + +/* +============= +WriteBSPFile + +Swaps the bsp file in place, so it should not be referenced again +============= +*/ +void Q2_WriteBSPFile( char *filename ) { + header = &outheader; + memset( header, 0, sizeof( dheader_t ) ); + + Q2_SwapBSPFile( true ); + + header->ident = LittleLong( IDBSPHEADER ); + header->version = LittleLong( BSPVERSION ); + + wadfile = SafeOpenWrite( filename ); + SafeWrite( wadfile, header, sizeof( dheader_t ) ); // overwritten later + + Q2_AddLump( LUMP_PLANES, dplanes, numplanes * sizeof( dplane_t ) ); + Q2_AddLump( LUMP_LEAFS, dleafs, numleafs * sizeof( dleaf_t ) ); + Q2_AddLump( LUMP_VERTEXES, dvertexes, numvertexes * sizeof( dvertex_t ) ); + Q2_AddLump( LUMP_NODES, dnodes, numnodes * sizeof( dnode_t ) ); + Q2_AddLump( LUMP_TEXINFO, texinfo, numtexinfo * sizeof( texinfo_t ) ); + Q2_AddLump( LUMP_FACES, dfaces, numfaces * sizeof( dface_t ) ); + Q2_AddLump( LUMP_BRUSHES, dbrushes, numbrushes * sizeof( dbrush_t ) ); + Q2_AddLump( LUMP_BRUSHSIDES, dbrushsides, numbrushsides * sizeof( dbrushside_t ) ); + Q2_AddLump( LUMP_LEAFFACES, dleaffaces, numleaffaces * sizeof( dleaffaces[0] ) ); + Q2_AddLump( LUMP_LEAFBRUSHES, dleafbrushes, numleafbrushes * sizeof( dleafbrushes[0] ) ); + Q2_AddLump( LUMP_SURFEDGES, dsurfedges, numsurfedges * sizeof( dsurfedges[0] ) ); + Q2_AddLump( LUMP_EDGES, dedges, numedges * sizeof( dedge_t ) ); + Q2_AddLump( LUMP_MODELS, dmodels, nummodels * sizeof( dmodel_t ) ); + Q2_AddLump( LUMP_AREAS, dareas, numareas * sizeof( darea_t ) ); + Q2_AddLump( LUMP_AREAPORTALS, dareaportals, numareaportals * sizeof( dareaportal_t ) ); + + Q2_AddLump( LUMP_LIGHTING, dlightdata, lightdatasize ); + Q2_AddLump( LUMP_VISIBILITY, dvisdata, visdatasize ); + Q2_AddLump( LUMP_ENTITIES, dentdata, entdatasize ); + Q2_AddLump( LUMP_POP, dpop, sizeof( dpop ) ); + + fseek( wadfile, 0, SEEK_SET ); + SafeWrite( wadfile, header, sizeof( dheader_t ) ); + fclose( wadfile ); +} //end of the function Q2_WriteBSPFile + +//============================================================================ + +/* +============= +PrintBSPFileSizes + +Dumps info about current file +============= +*/ +void Q2_PrintBSPFileSizes( void ) { + if ( !num_entities ) { + Q2_ParseEntities(); + } + + printf( "%6i models %7i\n" + ,nummodels, (int)( nummodels * sizeof( dmodel_t ) ) ); + printf( "%6i brushes %7i\n" + ,numbrushes, (int)( numbrushes * sizeof( dbrush_t ) ) ); + printf( "%6i brushsides %7i\n" + ,numbrushsides, (int)( numbrushsides * sizeof( dbrushside_t ) ) ); + printf( "%6i planes %7i\n" + ,numplanes, (int)( numplanes * sizeof( dplane_t ) ) ); + printf( "%6i texinfo %7i\n" + ,numtexinfo, (int)( numtexinfo * sizeof( texinfo_t ) ) ); + printf( "%6i entdata %7i\n", num_entities, entdatasize ); + + printf( "\n" ); + + printf( "%6i vertexes %7i\n" + ,numvertexes, (int)( numvertexes * sizeof( dvertex_t ) ) ); + printf( "%6i nodes %7i\n" + ,numnodes, (int)( numnodes * sizeof( dnode_t ) ) ); + printf( "%6i faces %7i\n" + ,numfaces, (int)( numfaces * sizeof( dface_t ) ) ); + printf( "%6i leafs %7i\n" + ,numleafs, (int)( numleafs * sizeof( dleaf_t ) ) ); + printf( "%6i leaffaces %7i\n" + ,numleaffaces, (int)( numleaffaces * sizeof( dleaffaces[0] ) ) ); + printf( "%6i leafbrushes %7i\n" + ,numleafbrushes, (int)( numleafbrushes * sizeof( dleafbrushes[0] ) ) ); + printf( "%6i surfedges %7i\n" + ,numsurfedges, (int)( numsurfedges * sizeof( dsurfedges[0] ) ) ); + printf( "%6i edges %7i\n" + ,numedges, (int)( numedges * sizeof( dedge_t ) ) ); +//NEW + printf( "%6i areas %7i\n" + ,numareas, (int)( numareas * sizeof( darea_t ) ) ); + printf( "%6i areaportals %7i\n" + ,numareaportals, (int)( numareaportals * sizeof( dareaportal_t ) ) ); +//ENDNEW + printf( " lightdata %7i\n", lightdatasize ); + printf( " visdata %7i\n", visdatasize ); +} //end of the function Q2_PrintBSPFileSizes + +/* +================ +ParseEntities + +Parses the dentdata string into entities +================ +*/ +void Q2_ParseEntities( void ) { + script_t *script; + + num_entities = 0; + script = LoadScriptMemory( dentdata, entdatasize, "*Quake2 bsp file" ); + SetScriptFlags( script, SCFL_NOSTRINGWHITESPACES | + SCFL_NOSTRINGESCAPECHARS ); + + while ( ParseEntity( script ) ) + { + } //end while + + FreeScript( script ); +} //end of the function Q2_ParseEntities + + +/* +================ +UnparseEntities + +Generates the dentdata string from all the entities +================ +*/ +void Q2_UnparseEntities( void ) { + char *buf, *end; + epair_t *ep; + char line[2048]; + int i; + char key[1024], value[1024]; + + buf = dentdata; + end = buf; + *end = 0; + + for ( i = 0 ; i < num_entities ; i++ ) + { + ep = entities[i].epairs; + if ( !ep ) { + continue; // ent got removed + + } + strcat( end,"{\n" ); + end += 2; + + for ( ep = entities[i].epairs ; ep ; ep = ep->next ) + { + strcpy( key, ep->key ); + StripTrailing( key ); + strcpy( value, ep->value ); + StripTrailing( value ); + + sprintf( line, "\"%s\" \"%s\"\n", key, value ); + strcat( end, line ); + end += strlen( line ); + } + strcat( end,"}\n" ); + end += 2; + + if ( end > buf + MAX_MAP_ENTSTRING ) { + Error( "Entity text too long" ); + } + } + entdatasize = end - buf + 1; +} //end of the function Q2_UnparseEntities + diff --git a/src/bspc/l_bsp_q2.h b/src/bspc/l_bsp_q2.h new file mode 100644 index 0000000..9267949 --- /dev/null +++ b/src/bspc/l_bsp_q2.h @@ -0,0 +1,104 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +#ifndef ME +#define ME +#endif //ME + +extern int nummodels; +extern dmodel_t *dmodels; //[MAX_MAP_MODELS]; + +extern int visdatasize; +extern byte *dvisdata; //[MAX_MAP_VISIBILITY]; +extern dvis_t *dvis; + +extern int lightdatasize; +extern byte *dlightdata; //[MAX_MAP_LIGHTING]; + +extern int entdatasize; +extern char *dentdata; //[MAX_MAP_ENTSTRING]; + +extern int numleafs; +extern dleaf_t *dleafs; //[MAX_MAP_LEAFS]; + +extern int numplanes; +extern dplane_t *dplanes; //[MAX_MAP_PLANES]; + +extern int numvertexes; +extern dvertex_t *dvertexes; //[MAX_MAP_VERTS]; + +extern int numnodes; +extern dnode_t *dnodes; //[MAX_MAP_NODES]; + +extern int numtexinfo; +extern texinfo_t texinfo[MAX_MAP_TEXINFO]; + +extern int numfaces; +extern dface_t *dfaces; //[MAX_MAP_FACES]; + +extern int numedges; +extern dedge_t *dedges; //[MAX_MAP_EDGES]; + +extern int numleaffaces; +extern unsigned short *dleaffaces; //[MAX_MAP_LEAFFACES]; + +extern int numleafbrushes; +extern unsigned short *dleafbrushes; //[MAX_MAP_LEAFBRUSHES]; + +extern int numsurfedges; +extern int *dsurfedges; //[MAX_MAP_SURFEDGES]; + +extern int numareas; +extern darea_t *dareas; //[MAX_MAP_AREAS]; + +extern int numareaportals; +extern dareaportal_t *dareaportals; //[MAX_MAP_AREAPORTALS]; + +extern int numbrushes; +extern dbrush_t *dbrushes; //[MAX_MAP_BRUSHES]; + +extern int numbrushsides; +extern dbrushside_t *dbrushsides; //[MAX_MAP_BRUSHSIDES]; + +extern byte dpop[256]; + +extern char brushsidetextured[MAX_MAP_BRUSHSIDES]; + +void Q2_AllocMaxBSP( void ); +void Q2_FreeMaxBSP( void ); + +void Q2_DecompressVis( byte *in, byte *decompressed ); +int Q2_CompressVis( byte *vis, byte *dest ); + +void Q2_LoadBSPFile( char *filename, int offset, int length ); +void Q2_LoadBSPFileTexinfo( char *filename ); // just for qdata +void Q2_WriteBSPFile( char *filename ); +void Q2_PrintBSPFileSizes( void ); +void Q2_ParseEntities( void ); +void Q2_UnparseEntities( void ); + diff --git a/src/bspc/l_bsp_q3.c b/src/bspc/l_bsp_q3.c new file mode 100644 index 0000000..71743b2 --- /dev/null +++ b/src/bspc/l_bsp_q3.c @@ -0,0 +1,857 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +#include "l_cmd.h" +#include "l_math.h" +#include "l_mem.h" +#include "l_log.h" +#include "l_poly.h" +#include "../botlib/l_script.h" +#include "l_qfiles.h" +#include "l_bsp_q3.h" +#include "l_bsp_ent.h" +//#include "qfiles.h" +#include "q2files.h" + +void Q3_ParseEntities( void ); +void Q3_PrintBSPFileSizes( void ); + +void GetLeafNums( void ); + +//============================================================================= + +#define WCONVEX_EPSILON 0.5 + +int q3_nummodels; +q3_dmodel_t *q3_dmodels; //[MAX_MAP_MODELS]; + +int q3_numShaders; +q3_dshader_t *q3_dshaders; //[Q3_MAX_MAP_SHADERS]; + +int q3_entdatasize; +char *q3_dentdata; //[Q3_MAX_MAP_ENTSTRING]; + +int q3_numleafs; +q3_dleaf_t *q3_dleafs; //[Q3_MAX_MAP_LEAFS]; + +int q3_numplanes; +q3_dplane_t *q3_dplanes; //[Q3_MAX_MAP_PLANES]; + +int q3_numnodes; +q3_dnode_t *q3_dnodes; //[Q3_MAX_MAP_NODES]; + +int q3_numleafsurfaces; +int *q3_dleafsurfaces; //[Q3_MAX_MAP_LEAFFACES]; + +int q3_numleafbrushes; +int *q3_dleafbrushes; //[Q3_MAX_MAP_LEAFBRUSHES]; + +int q3_numbrushes; +q3_dbrush_t *q3_dbrushes; //[Q3_MAX_MAP_BRUSHES]; + +int q3_numbrushsides; +q3_dbrushside_t *q3_dbrushsides; //[Q3_MAX_MAP_BRUSHSIDES]; + +int q3_numLightBytes; +byte *q3_lightBytes; //[Q3_MAX_MAP_LIGHTING]; + +int q3_numGridPoints; +byte *q3_gridData; //[Q3_MAX_MAP_LIGHTGRID]; + +int q3_numVisBytes; +byte *q3_visBytes; //[Q3_MAX_MAP_VISIBILITY]; + +int q3_numDrawVerts; +q3_drawVert_t *q3_drawVerts; //[Q3_MAX_MAP_DRAW_VERTS]; + +int q3_numDrawIndexes; +int *q3_drawIndexes; //[Q3_MAX_MAP_DRAW_INDEXES]; + +int q3_numDrawSurfaces; +q3_dsurface_t *q3_drawSurfaces; //[Q3_MAX_MAP_DRAW_SURFS]; + +int q3_numFogs; +q3_dfog_t *q3_dfogs; //[Q3_MAX_MAP_FOGS]; + +char q3_dbrushsidetextured[Q3_MAX_MAP_BRUSHSIDES]; + +extern qboolean forcesidesvisible; + + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Q3_FreeMaxBSP( void ) { + if ( q3_dmodels ) { + FreeMemory( q3_dmodels ); + } + q3_dmodels = NULL; + q3_nummodels = 0; + if ( q3_dshaders ) { + FreeMemory( q3_dshaders ); + } + q3_dshaders = NULL; + q3_numShaders = 0; + if ( q3_dentdata ) { + FreeMemory( q3_dentdata ); + } + q3_dentdata = NULL; + q3_entdatasize = 0; + if ( q3_dleafs ) { + FreeMemory( q3_dleafs ); + } + q3_dleafs = NULL; + q3_numleafs = 0; + if ( q3_dplanes ) { + FreeMemory( q3_dplanes ); + } + q3_dplanes = NULL; + q3_numplanes = 0; + if ( q3_dnodes ) { + FreeMemory( q3_dnodes ); + } + q3_dnodes = NULL; + q3_numnodes = 0; + if ( q3_dleafsurfaces ) { + FreeMemory( q3_dleafsurfaces ); + } + q3_dleafsurfaces = NULL; + q3_numleafsurfaces = 0; + if ( q3_dleafbrushes ) { + FreeMemory( q3_dleafbrushes ); + } + q3_dleafbrushes = NULL; + q3_numleafbrushes = 0; + if ( q3_dbrushes ) { + FreeMemory( q3_dbrushes ); + } + q3_dbrushes = NULL; + q3_numbrushes = 0; + if ( q3_dbrushsides ) { + FreeMemory( q3_dbrushsides ); + } + q3_dbrushsides = NULL; + q3_numbrushsides = 0; + if ( q3_lightBytes ) { + FreeMemory( q3_lightBytes ); + } + q3_lightBytes = NULL; + q3_numLightBytes = 0; + if ( q3_gridData ) { + FreeMemory( q3_gridData ); + } + q3_gridData = NULL; + q3_numGridPoints = 0; + if ( q3_visBytes ) { + FreeMemory( q3_visBytes ); + } + q3_visBytes = NULL; + q3_numVisBytes = 0; + if ( q3_drawVerts ) { + FreeMemory( q3_drawVerts ); + } + q3_drawVerts = NULL; + q3_numDrawVerts = 0; + if ( q3_drawIndexes ) { + FreeMemory( q3_drawIndexes ); + } + q3_drawIndexes = NULL; + q3_numDrawIndexes = 0; + if ( q3_drawSurfaces ) { + FreeMemory( q3_drawSurfaces ); + } + q3_drawSurfaces = NULL; + q3_numDrawSurfaces = 0; + if ( q3_dfogs ) { + FreeMemory( q3_dfogs ); + } + q3_dfogs = NULL; + q3_numFogs = 0; +} //end of the function Q3_FreeMaxBSP + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Q3_PlaneFromPoints( vec3_t p0, vec3_t p1, vec3_t p2, vec3_t normal, float *dist ) { + vec3_t t1, t2; + + VectorSubtract( p0, p1, t1 ); + VectorSubtract( p2, p1, t2 ); + CrossProduct( t1, t2, normal ); + VectorNormalize( normal ); + + *dist = DotProduct( p0, normal ); +} //end of the function PlaneFromPoints +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Q3_SurfacePlane( q3_dsurface_t *surface, vec3_t normal, float *dist ) { + int i; + float *p0, *p1, *p2; + vec3_t t1, t2; + + p0 = q3_drawVerts[surface->firstVert].xyz; + for ( i = 1; i < surface->numVerts - 1; i++ ) + { + p1 = q3_drawVerts[surface->firstVert + ( ( i ) % surface->numVerts )].xyz; + p2 = q3_drawVerts[surface->firstVert + ( ( i + 1 ) % surface->numVerts )].xyz; + VectorSubtract( p0, p1, t1 ); + VectorSubtract( p2, p1, t2 ); + CrossProduct( t1, t2, normal ); + VectorNormalize( normal ); + if ( VectorLength( normal ) ) { + break; + } + } //end for*/ +/* + float dot; + for (i = 0; i < surface->numVerts; i++) + { + p0 = q3_drawVerts[surface->firstVert + ((i) % surface->numVerts)].xyz; + p1 = q3_drawVerts[surface->firstVert + ((i+1) % surface->numVerts)].xyz; + p2 = q3_drawVerts[surface->firstVert + ((i+2) % surface->numVerts)].xyz; + VectorSubtract(p0, p1, t1); + VectorSubtract(p2, p1, t2); + VectorNormalize(t1); + VectorNormalize(t2); + dot = DotProduct(t1, t2); + if (dot > -0.9 && dot < 0.9 && + VectorLength(t1) > 0.1 && VectorLength(t2) > 0.1) break; + } //end for + CrossProduct(t1, t2, normal); + VectorNormalize(normal); +*/ + if ( VectorLength( normal ) < 0.9 ) { + printf( "surface %d bogus normal vector %f %f %f\n", surface - q3_drawSurfaces, normal[0], normal[1], normal[2] ); + printf( "t1 = %f %f %f, t2 = %f %f %f\n", t1[0], t1[1], t1[2], t2[0], t2[1], t2[2] ); + for ( i = 0; i < surface->numVerts; i++ ) + { + p1 = q3_drawVerts[surface->firstVert + ( ( i ) % surface->numVerts )].xyz; + Log_Print( "p%d = %f %f %f\n", i, p1[0], p1[1], p1[2] ); + } //end for + } //end if + *dist = DotProduct( p0, normal ); +} //end of the function Q3_SurfacePlane +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +q3_dplane_t *q3_surfaceplanes; + +void Q3_CreatePlanarSurfacePlanes( void ) { + int i; + q3_dsurface_t *surface; + + Log_Print( "creating planar surface planes...\n" ); + q3_surfaceplanes = (q3_dplane_t *) GetClearedMemory( q3_numDrawSurfaces * sizeof( q3_dplane_t ) ); + + for ( i = 0; i < q3_numDrawSurfaces; i++ ) + { + surface = &q3_drawSurfaces[i]; + if ( surface->surfaceType != MST_PLANAR ) { + continue; + } + Q3_SurfacePlane( surface, q3_surfaceplanes[i].normal, &q3_surfaceplanes[i].dist ); + //Log_Print("normal = %f %f %f, dist = %f\n", q3_surfaceplanes[i].normal[0], + // q3_surfaceplanes[i].normal[1], + // q3_surfaceplanes[i].normal[2], q3_surfaceplanes[i].dist); + } //end for +} //end of the function Q3_CreatePlanarSurfacePlanes +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +/* +void Q3_SurfacePlane(q3_dsurface_t *surface, vec3_t normal, float *dist) +{ + //take the plane information from the lightmap vector + //VectorCopy(surface->lightmapVecs[2], normal); + //calculate plane dist with first surface vertex + // *dist = DotProduct(q3_drawVerts[surface->firstVert].xyz, normal); + Q3_PlaneFromPoints(q3_drawVerts[surface->firstVert].xyz, + q3_drawVerts[surface->firstVert+1].xyz, + q3_drawVerts[surface->firstVert+2].xyz, normal, dist); +} //end of the function Q3_SurfacePlane*/ +//=========================================================================== +// returns the amount the face and the winding overlap +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float Q3_FaceOnWinding( q3_dsurface_t *surface, winding_t *winding ) { + int i; + float dist, area; + q3_dplane_t plane; + vec_t *v1, *v2; + vec3_t normal, edgevec; + winding_t *w; + + //copy the winding before chopping + w = CopyWinding( winding ); + //retrieve the surface plane + Q3_SurfacePlane( surface, plane.normal, &plane.dist ); + //chop the winding with the surface edge planes + for ( i = 0; i < surface->numVerts && w; i++ ) + { + v1 = q3_drawVerts[surface->firstVert + ( ( i ) % surface->numVerts )].xyz; + v2 = q3_drawVerts[surface->firstVert + ( ( i + 1 ) % surface->numVerts )].xyz; + //create a plane through the edge from v1 to v2, orthogonal to the + //surface plane and with the normal vector pointing inward + VectorSubtract( v2, v1, edgevec ); + CrossProduct( edgevec, plane.normal, normal ); + VectorNormalize( normal ); + dist = DotProduct( normal, v1 ); + // + ChopWindingInPlace( &w, normal, dist, -0.1 ); //CLIP_EPSILON + } //end for + if ( w ) { + area = WindingArea( w ); + FreeWinding( w ); + return area; + } //end if + return 0; +} //end of the function Q3_FaceOnWinding +//=========================================================================== +// creates a winding for the given brush side on the given brush +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +winding_t *Q3_BrushSideWinding( q3_dbrush_t *brush, q3_dbrushside_t *baseside ) { + int i; + q3_dplane_t *baseplane, *plane; + winding_t *w; + q3_dbrushside_t *side; + + //create a winding for the brush side with the given planenumber + baseplane = &q3_dplanes[baseside->planeNum]; + w = BaseWindingForPlane( baseplane->normal, baseplane->dist ); + for ( i = 0; i < brush->numSides && w; i++ ) + { + side = &q3_dbrushsides[brush->firstSide + i]; + //don't chop with the base plane + if ( side->planeNum == baseside->planeNum ) { + continue; + } + //also don't use planes that are almost equal + plane = &q3_dplanes[side->planeNum]; + if ( DotProduct( baseplane->normal, plane->normal ) > 0.999 + && fabs( baseplane->dist - plane->dist ) < 0.01 ) { + continue; + } + // + plane = &q3_dplanes[side->planeNum ^ 1]; + ChopWindingInPlace( &w, plane->normal, plane->dist, -0.1 ); //CLIP_EPSILON); + } //end for + return w; +} //end of the function Q3_BrushSideWinding +//=========================================================================== +// fix screwed brush texture references +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean WindingIsTiny( winding_t *w ); + +void Q3_FindVisibleBrushSides( void ) { + int i, j, k, we, numtextured, numsides; + float dot; + q3_dplane_t *plane; + q3_dbrushside_t *brushside; + q3_dbrush_t *brush; + q3_dsurface_t *surface; + winding_t *w; + + memset( q3_dbrushsidetextured, false, Q3_MAX_MAP_BRUSHSIDES ); + // + numsides = 0; + //create planes for the planar surfaces + Q3_CreatePlanarSurfacePlanes(); + Log_Print( "searching visible brush sides...\n" ); + Log_Print( "%6d brush sides", numsides ); + //go over all the brushes + for ( i = 0; i < q3_numbrushes; i++ ) + { + brush = &q3_dbrushes[i]; + // if one of the brush sides uses the terrain shader mark all sides as visible + for ( j = 0; j < brush->numSides; j++ ) { + brushside = &q3_dbrushsides[brush->firstSide + j]; + // NOTE: using "terrain" just like in q3map/terrain.c + if ( strstr( q3_dshaders[brushside->shaderNum].shader, "terrain" ) ) { + for ( j = 0; j < brush->numSides; j++ ) { + q3_dbrushsidetextured[brush->firstSide + j] = true; + } + break; + } + } + if ( j < brush->numSides ) { + continue; + } + //go over all the sides of the brush + for ( j = 0; j < brush->numSides; j++ ) + { + qprintf( "\r%6d", numsides++ ); + brushside = &q3_dbrushsides[brush->firstSide + j]; + // + w = Q3_BrushSideWinding( brush, brushside ); + if ( !w ) { + q3_dbrushsidetextured[brush->firstSide + j] = true; + continue; + } //end if + else + { + //RemoveEqualPoints(w, 0.2); + if ( WindingIsTiny( w ) ) { + FreeWinding( w ); + q3_dbrushsidetextured[brush->firstSide + j] = true; + continue; + } //end if + else + { + we = WindingError( w ); + if ( we == WE_NOTENOUGHPOINTS + || we == WE_SMALLAREA + || we == WE_POINTBOGUSRANGE +// || we == WE_NONCONVEX + ) { + FreeWinding( w ); + q3_dbrushsidetextured[brush->firstSide + j] = true; + continue; + } //end if + } //end else + } //end else + if ( WindingArea( w ) < 20 ) { + q3_dbrushsidetextured[brush->firstSide + j] = true; + continue; + } //end if + //find a face for texturing this brush + for ( k = 0; k < q3_numDrawSurfaces; k++ ) + { + surface = &q3_drawSurfaces[k]; + if ( surface->surfaceType != MST_PLANAR ) { + continue; + } + // + //Q3_SurfacePlane(surface, plane.normal, &plane.dist); + plane = &q3_surfaceplanes[k]; + //the surface plane and the brush side plane should be pretty much the same + if ( fabs( fabs( plane->dist ) - fabs( q3_dplanes[brushside->planeNum].dist ) ) > 5 ) { + continue; + } + dot = DotProduct( plane->normal, q3_dplanes[brushside->planeNum].normal ); + if ( dot > -0.9 && dot < 0.9 ) { + continue; + } + //if the face is partly or totally on the brush side + if ( Q3_FaceOnWinding( surface, w ) ) { + q3_dbrushsidetextured[brush->firstSide + j] = true; + //Log_Write("Q3_FaceOnWinding"); + break; + } //end if + } //end for + FreeWinding( w ); + } //end for + } //end for + qprintf( "\r%6d brush sides\n", numsides ); + numtextured = 0; + for ( i = 0; i < q3_numbrushsides; i++ ) + { + if ( forcesidesvisible ) { + q3_dbrushsidetextured[i] = true; + } + if ( q3_dbrushsidetextured[i] ) { + numtextured++; + } + } //end for + Log_Print( "%d brush sides textured out of %d\n", numtextured, q3_numbrushsides ); +} //end of the function Q3_FindVisibleBrushSides + +/* +============= +Q3_SwapBlock + +If all values are 32 bits, this can be used to swap everything +============= +*/ +void Q3_SwapBlock( int *block, int sizeOfBlock ) { + int i; + + sizeOfBlock >>= 2; + for ( i = 0 ; i < sizeOfBlock ; i++ ) { + block[i] = LittleLong( block[i] ); + } +} //end of the function Q3_SwapBlock + +/* +============= +Q3_SwapBSPFile + +Byte swaps all data in a bsp file. +============= +*/ +void Q3_SwapBSPFile( void ) { + int i; + + // models + Q3_SwapBlock( (int *)q3_dmodels, q3_nummodels * sizeof( q3_dmodels[0] ) ); + + // shaders (don't swap the name) + for ( i = 0 ; i < q3_numShaders ; i++ ) { + q3_dshaders[i].contentFlags = LittleLong( q3_dshaders[i].contentFlags ); + q3_dshaders[i].surfaceFlags = LittleLong( q3_dshaders[i].surfaceFlags ); + // + // RF, only keep content flags that are consistent with q3map + q3_dshaders[i].contentFlags &= ( CONTENTS_SOLID | CONTENTS_WINDOW | CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER | CONTENTS_MIST | CONTENTS_AREAPORTAL | CONTENTS_PLAYERCLIP | CONTENTS_MONSTERCLIP | CONTENTS_ORIGIN | CONTENTS_DETAIL ); + } + + // planes + Q3_SwapBlock( (int *)q3_dplanes, q3_numplanes * sizeof( q3_dplanes[0] ) ); + + // nodes + Q3_SwapBlock( (int *)q3_dnodes, q3_numnodes * sizeof( q3_dnodes[0] ) ); + + // leafs + Q3_SwapBlock( (int *)q3_dleafs, q3_numleafs * sizeof( q3_dleafs[0] ) ); + + // leaffaces + Q3_SwapBlock( (int *)q3_dleafsurfaces, q3_numleafsurfaces * sizeof( q3_dleafsurfaces[0] ) ); + + // leafbrushes + Q3_SwapBlock( (int *)q3_dleafbrushes, q3_numleafbrushes * sizeof( q3_dleafbrushes[0] ) ); + + // brushes + Q3_SwapBlock( (int *)q3_dbrushes, q3_numbrushes * sizeof( q3_dbrushes[0] ) ); + + // brushsides + Q3_SwapBlock( (int *)q3_dbrushsides, q3_numbrushsides * sizeof( q3_dbrushsides[0] ) ); + + // vis + ( (int *)&q3_visBytes )[0] = LittleLong( ( (int *)&q3_visBytes )[0] ); + ( (int *)&q3_visBytes )[1] = LittleLong( ( (int *)&q3_visBytes )[1] ); + + // drawverts (don't swap colors ) + for ( i = 0 ; i < q3_numDrawVerts ; i++ ) { + q3_drawVerts[i].lightmap[0] = LittleFloat( q3_drawVerts[i].lightmap[0] ); + q3_drawVerts[i].lightmap[1] = LittleFloat( q3_drawVerts[i].lightmap[1] ); + q3_drawVerts[i].st[0] = LittleFloat( q3_drawVerts[i].st[0] ); + q3_drawVerts[i].st[1] = LittleFloat( q3_drawVerts[i].st[1] ); + q3_drawVerts[i].xyz[0] = LittleFloat( q3_drawVerts[i].xyz[0] ); + q3_drawVerts[i].xyz[1] = LittleFloat( q3_drawVerts[i].xyz[1] ); + q3_drawVerts[i].xyz[2] = LittleFloat( q3_drawVerts[i].xyz[2] ); + q3_drawVerts[i].normal[0] = LittleFloat( q3_drawVerts[i].normal[0] ); + q3_drawVerts[i].normal[1] = LittleFloat( q3_drawVerts[i].normal[1] ); + q3_drawVerts[i].normal[2] = LittleFloat( q3_drawVerts[i].normal[2] ); + } + + // drawindexes + Q3_SwapBlock( (int *)q3_drawIndexes, q3_numDrawIndexes * sizeof( q3_drawIndexes[0] ) ); + + // drawsurfs + Q3_SwapBlock( (int *)q3_drawSurfaces, q3_numDrawSurfaces * sizeof( q3_drawSurfaces[0] ) ); + + // fogs + for ( i = 0 ; i < q3_numFogs ; i++ ) { + q3_dfogs[i].brushNum = LittleLong( q3_dfogs[i].brushNum ); + } +} + + + +/* +============= +Q3_CopyLump +============= +*/ +int Q3_CopyLump( q3_dheader_t *header, int lump, void **dest, int size ) { + int length, ofs; + + length = header->lumps[lump].filelen; + ofs = header->lumps[lump].fileofs; + + if ( length % size ) { + Error( "Q3_LoadBSPFile: odd lump size" ); + } + + *dest = GetMemory( length ); + + memcpy( *dest, (byte *)header + ofs, length ); + + return length / size; +} + +/* +============= +Q3_LoadBSPFile +============= +*/ +void Q3_LoadBSPFile( struct quakefile_s *qf ) { + q3_dheader_t *header; + + // load the file header + //LoadFile(filename, (void **)&header, offset, length); + // + LoadQuakeFile( qf, (void **)&header ); + + // swap the header + Q3_SwapBlock( (int *)header, sizeof( *header ) ); + + if ( header->ident != Q3_BSP_IDENT ) { + Error( "%s is not a IBSP file", qf->filename ); + } + if ( header->version != Q3_BSP_VERSION ) { + Error( "%s is version %i, not %i", qf->filename, header->version, Q3_BSP_VERSION ); + } + + q3_numShaders = Q3_CopyLump( header, Q3_LUMP_SHADERS, (void *) &q3_dshaders, sizeof( q3_dshader_t ) ); + q3_nummodels = Q3_CopyLump( header, Q3_LUMP_MODELS, (void *) &q3_dmodels, sizeof( q3_dmodel_t ) ); + q3_numplanes = Q3_CopyLump( header, Q3_LUMP_PLANES, (void *) &q3_dplanes, sizeof( q3_dplane_t ) ); + q3_numleafs = Q3_CopyLump( header, Q3_LUMP_LEAFS, (void *) &q3_dleafs, sizeof( q3_dleaf_t ) ); + q3_numnodes = Q3_CopyLump( header, Q3_LUMP_NODES, (void *) &q3_dnodes, sizeof( q3_dnode_t ) ); + q3_numleafsurfaces = Q3_CopyLump( header, Q3_LUMP_LEAFSURFACES, (void *) &q3_dleafsurfaces, sizeof( q3_dleafsurfaces[0] ) ); + q3_numleafbrushes = Q3_CopyLump( header, Q3_LUMP_LEAFBRUSHES, (void *) &q3_dleafbrushes, sizeof( q3_dleafbrushes[0] ) ); + q3_numbrushes = Q3_CopyLump( header, Q3_LUMP_BRUSHES, (void *) &q3_dbrushes, sizeof( q3_dbrush_t ) ); + q3_numbrushsides = Q3_CopyLump( header, Q3_LUMP_BRUSHSIDES, (void *) &q3_dbrushsides, sizeof( q3_dbrushside_t ) ); + q3_numDrawVerts = Q3_CopyLump( header, Q3_LUMP_DRAWVERTS, (void *) &q3_drawVerts, sizeof( q3_drawVert_t ) ); + q3_numDrawSurfaces = Q3_CopyLump( header, Q3_LUMP_SURFACES, (void *) &q3_drawSurfaces, sizeof( q3_dsurface_t ) ); + q3_numFogs = Q3_CopyLump( header, Q3_LUMP_FOGS, (void *) &q3_dfogs, sizeof( q3_dfog_t ) ); + q3_numDrawIndexes = Q3_CopyLump( header, Q3_LUMP_DRAWINDEXES, (void *) &q3_drawIndexes, sizeof( q3_drawIndexes[0] ) ); + + q3_numVisBytes = Q3_CopyLump( header, Q3_LUMP_VISIBILITY, (void *) &q3_visBytes, 1 ); + q3_numLightBytes = Q3_CopyLump( header, Q3_LUMP_LIGHTMAPS, (void *) &q3_lightBytes, 1 ); + q3_entdatasize = Q3_CopyLump( header, Q3_LUMP_ENTITIES, (void *) &q3_dentdata, 1 ); + + q3_numGridPoints = Q3_CopyLump( header, Q3_LUMP_LIGHTGRID, (void *) &q3_gridData, 8 ); + + + FreeMemory( header ); // everything has been copied out + + // swap everything + Q3_SwapBSPFile(); + + Q3_FindVisibleBrushSides(); + + //Q3_PrintBSPFileSizes(); +} + + +//============================================================================ + +/* +============= +Q3_AddLump +============= +*/ +void Q3_AddLump( FILE *bspfile, q3_dheader_t *header, int lumpnum, void *data, int len ) { + q3_lump_t *lump; + + lump = &header->lumps[lumpnum]; + + lump->fileofs = LittleLong( ftell( bspfile ) ); + lump->filelen = LittleLong( len ); + SafeWrite( bspfile, data, ( len + 3 ) & ~3 ); +} + +/* +============= +Q3_WriteBSPFile + +Swaps the bsp file in place, so it should not be referenced again +============= +*/ +void Q3_WriteBSPFile( char *filename ) { + q3_dheader_t outheader, *header; + FILE *bspfile; + + header = &outheader; + memset( header, 0, sizeof( q3_dheader_t ) ); + + Q3_SwapBSPFile(); + + header->ident = LittleLong( Q3_BSP_IDENT ); + header->version = LittleLong( Q3_BSP_VERSION ); + + bspfile = SafeOpenWrite( filename ); + SafeWrite( bspfile, header, sizeof( q3_dheader_t ) ); // overwritten later + + Q3_AddLump( bspfile, header, Q3_LUMP_SHADERS, q3_dshaders, q3_numShaders * sizeof( q3_dshader_t ) ); + Q3_AddLump( bspfile, header, Q3_LUMP_PLANES, q3_dplanes, q3_numplanes * sizeof( q3_dplane_t ) ); + Q3_AddLump( bspfile, header, Q3_LUMP_LEAFS, q3_dleafs, q3_numleafs * sizeof( q3_dleaf_t ) ); + Q3_AddLump( bspfile, header, Q3_LUMP_NODES, q3_dnodes, q3_numnodes * sizeof( q3_dnode_t ) ); + Q3_AddLump( bspfile, header, Q3_LUMP_BRUSHES, q3_dbrushes, q3_numbrushes * sizeof( q3_dbrush_t ) ); + Q3_AddLump( bspfile, header, Q3_LUMP_BRUSHSIDES, q3_dbrushsides, q3_numbrushsides * sizeof( q3_dbrushside_t ) ); + Q3_AddLump( bspfile, header, Q3_LUMP_LEAFSURFACES, q3_dleafsurfaces, q3_numleafsurfaces * sizeof( q3_dleafsurfaces[0] ) ); + Q3_AddLump( bspfile, header, Q3_LUMP_LEAFBRUSHES, q3_dleafbrushes, q3_numleafbrushes * sizeof( q3_dleafbrushes[0] ) ); + Q3_AddLump( bspfile, header, Q3_LUMP_MODELS, q3_dmodels, q3_nummodels * sizeof( q3_dmodel_t ) ); + Q3_AddLump( bspfile, header, Q3_LUMP_DRAWVERTS, q3_drawVerts, q3_numDrawVerts * sizeof( q3_drawVert_t ) ); + Q3_AddLump( bspfile, header, Q3_LUMP_SURFACES, q3_drawSurfaces, q3_numDrawSurfaces * sizeof( q3_dsurface_t ) ); + Q3_AddLump( bspfile, header, Q3_LUMP_VISIBILITY, q3_visBytes, q3_numVisBytes ); + Q3_AddLump( bspfile, header, Q3_LUMP_LIGHTMAPS, q3_lightBytes, q3_numLightBytes ); + Q3_AddLump( bspfile, header, Q3_LUMP_LIGHTGRID, q3_gridData, 8 * q3_numGridPoints ); + Q3_AddLump( bspfile, header, Q3_LUMP_ENTITIES, q3_dentdata, q3_entdatasize ); + Q3_AddLump( bspfile, header, Q3_LUMP_FOGS, q3_dfogs, q3_numFogs * sizeof( q3_dfog_t ) ); + Q3_AddLump( bspfile, header, Q3_LUMP_DRAWINDEXES, q3_drawIndexes, q3_numDrawIndexes * sizeof( q3_drawIndexes[0] ) ); + + fseek( bspfile, 0, SEEK_SET ); + SafeWrite( bspfile, header, sizeof( q3_dheader_t ) ); + fclose( bspfile ); +} + +//============================================================================ + +/* +============= +Q3_PrintBSPFileSizes + +Dumps info about current file +============= +*/ +void Q3_PrintBSPFileSizes( void ) { + if ( !num_entities ) { + Q3_ParseEntities(); + } + + Log_Print( "%6i models %7i\n" + ,q3_nummodels, (int)( q3_nummodels * sizeof( q3_dmodel_t ) ) ); + Log_Print( "%6i shaders %7i\n" + ,q3_numShaders, (int)( q3_numShaders * sizeof( q3_dshader_t ) ) ); + Log_Print( "%6i brushes %7i\n" + ,q3_numbrushes, (int)( q3_numbrushes * sizeof( q3_dbrush_t ) ) ); + Log_Print( "%6i brushsides %7i\n" + ,q3_numbrushsides, (int)( q3_numbrushsides * sizeof( q3_dbrushside_t ) ) ); + Log_Print( "%6i fogs %7i\n" + ,q3_numFogs, (int)( q3_numFogs * sizeof( q3_dfog_t ) ) ); + Log_Print( "%6i planes %7i\n" + ,q3_numplanes, (int)( q3_numplanes * sizeof( q3_dplane_t ) ) ); + Log_Print( "%6i entdata %7i\n", num_entities, q3_entdatasize ); + + Log_Print( "\n" ); + + Log_Print( "%6i nodes %7i\n" + ,q3_numnodes, (int)( q3_numnodes * sizeof( q3_dnode_t ) ) ); + Log_Print( "%6i leafs %7i\n" + ,q3_numleafs, (int)( q3_numleafs * sizeof( q3_dleaf_t ) ) ); + Log_Print( "%6i leafsurfaces %7i\n" + ,q3_numleafsurfaces, (int)( q3_numleafsurfaces * sizeof( q3_dleafsurfaces[0] ) ) ); + Log_Print( "%6i leafbrushes %7i\n" + ,q3_numleafbrushes, (int)( q3_numleafbrushes * sizeof( q3_dleafbrushes[0] ) ) ); + Log_Print( "%6i drawverts %7i\n" + ,q3_numDrawVerts, (int)( q3_numDrawVerts * sizeof( q3_drawVerts[0] ) ) ); + Log_Print( "%6i drawindexes %7i\n" + ,q3_numDrawIndexes, (int)( q3_numDrawIndexes * sizeof( q3_drawIndexes[0] ) ) ); + Log_Print( "%6i drawsurfaces %7i\n" + ,q3_numDrawSurfaces, (int)( q3_numDrawSurfaces * sizeof( q3_drawSurfaces[0] ) ) ); + + Log_Print( "%6i lightmaps %7i\n" + ,q3_numLightBytes / ( LIGHTMAP_WIDTH * LIGHTMAP_HEIGHT * 3 ), q3_numLightBytes ); + Log_Print( " visibility %7i\n" + , q3_numVisBytes ); +} + +/* +================ +Q3_ParseEntities + +Parses the q3_dentdata string into entities +================ +*/ +void Q3_ParseEntities( void ) { + script_t *script; + + num_entities = 0; + script = LoadScriptMemory( q3_dentdata, q3_entdatasize, "*Quake3 bsp file" ); + SetScriptFlags( script, SCFL_NOSTRINGWHITESPACES | + SCFL_NOSTRINGESCAPECHARS ); + + while ( ParseEntity( script ) ) + { + } //end while + + FreeScript( script ); +} //end of the function Q3_ParseEntities + + +/* +================ +Q3_UnparseEntities + +Generates the q3_dentdata string from all the entities +================ +*/ +void Q3_UnparseEntities( void ) { + char *buf, *end; + epair_t *ep; + char line[2048]; + int i; + + buf = q3_dentdata; + end = buf; + *end = 0; + + for ( i = 0 ; i < num_entities ; i++ ) + { + ep = entities[i].epairs; + if ( !ep ) { + continue; // ent got removed + + } + strcat( end,"{\n" ); + end += 2; + + for ( ep = entities[i].epairs ; ep ; ep = ep->next ) + { + sprintf( line, "\"%s\" \"%s\"\n", ep->key, ep->value ); + strcat( end, line ); + end += strlen( line ); + } + strcat( end,"}\n" ); + end += 2; + + if ( end > buf + Q3_MAX_MAP_ENTSTRING ) { + Error( "Entity text too long" ); + } + } + q3_entdatasize = end - buf + 1; +} //end of the function Q3_UnparseEntities + diff --git a/src/bspc/l_bsp_q3.h b/src/bspc/l_bsp_q3.h new file mode 100644 index 0000000..30a545d --- /dev/null +++ b/src/bspc/l_bsp_q3.h @@ -0,0 +1,88 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +#include "q3files.h" +//#include "surfaceflags.h" + +extern int q3_nummodels; +extern q3_dmodel_t *q3_dmodels; //[MAX_MAP_MODELS]; + +extern int q3_numShaders; +extern q3_dshader_t *q3_dshaders; //[Q3_MAX_MAP_SHADERS]; + +extern int q3_entdatasize; +extern char *q3_dentdata; //[Q3_MAX_MAP_ENTSTRING]; + +extern int q3_numleafs; +extern q3_dleaf_t *q3_dleafs; //[Q3_MAX_MAP_LEAFS]; + +extern int q3_numplanes; +extern q3_dplane_t *q3_dplanes; //[Q3_MAX_MAP_PLANES]; + +extern int q3_numnodes; +extern q3_dnode_t *q3_dnodes; //[Q3_MAX_MAP_NODES]; + +extern int q3_numleafsurfaces; +extern int *q3_dleafsurfaces; //[Q3_MAX_MAP_LEAFFACES]; + +extern int q3_numleafbrushes; +extern int *q3_dleafbrushes; //[Q3_MAX_MAP_LEAFBRUSHES]; + +extern int q3_numbrushes; +extern q3_dbrush_t *q3_dbrushes; //[Q3_MAX_MAP_BRUSHES]; + +extern int q3_numbrushsides; +extern q3_dbrushside_t *q3_dbrushsides; //[Q3_MAX_MAP_BRUSHSIDES]; + +extern int q3_numLightBytes; +extern byte *q3_lightBytes; //[Q3_MAX_MAP_LIGHTING]; + +extern int q3_numGridPoints; +extern byte *q3_gridData; //[Q3_MAX_MAP_LIGHTGRID]; + +extern int q3_numVisBytes; +extern byte *q3_visBytes; //[Q3_MAX_MAP_VISIBILITY]; + +extern int q3_numDrawVerts; +extern q3_drawVert_t *q3_drawVerts; //[Q3_MAX_MAP_DRAW_VERTS]; + +extern int q3_numDrawIndexes; +extern int *q3_drawIndexes; //[Q3_MAX_MAP_DRAW_INDEXES]; + +extern int q3_numDrawSurfaces; +extern q3_dsurface_t *q3_drawSurfaces; //[Q3_MAX_MAP_DRAW_SURFS]; + +extern int q3_numFogs; +extern q3_dfog_t *q3_dfogs; //[Q3_MAX_MAP_FOGS]; + +extern char q3_dbrushsidetextured[Q3_MAX_MAP_BRUSHSIDES]; + +void Q3_LoadBSPFile( struct quakefile_s *qf ); +void Q3_FreeMaxBSP( void ); +void Q3_ParseEntities( void ); diff --git a/src/bspc/l_bsp_sin.c b/src/bspc/l_bsp_sin.c new file mode 100644 index 0000000..745f1da --- /dev/null +++ b/src/bspc/l_bsp_sin.c @@ -0,0 +1,1186 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +#include "l_cmd.h" +#include "l_math.h" +#include "l_mem.h" +#include "l_log.h" +#include "l_poly.h" +#include "../botlib/l_script.h" +#include "l_bsp_ent.h" +#include "l_bsp_sin.h" + +void GetLeafNums( void ); + +//============================================================================= + +int sin_nummodels; +sin_dmodel_t *sin_dmodels; //[SIN_MAX_MAP_MODELS]; + +int sin_visdatasize; +byte *sin_dvisdata; //[SIN_MAX_MAP_VISIBILITY]; +sin_dvis_t *sin_dvis; // = (sin_dvis_t *)sin_sin_dvisdata; + +int sin_lightdatasize; +byte *sin_dlightdata; //[SIN_MAX_MAP_LIGHTING]; + +int sin_entdatasize; +char *sin_dentdata; //[SIN_MAX_MAP_ENTSTRING]; + +int sin_numleafs; +sin_dleaf_t *sin_dleafs; //[SIN_MAX_MAP_LEAFS]; + +int sin_numplanes; +sin_dplane_t *sin_dplanes; //[SIN_MAX_MAP_PLANES]; + +int sin_numvertexes; +sin_dvertex_t *sin_dvertexes; //[SIN_MAX_MAP_VERTS]; + +int sin_numnodes; +sin_dnode_t *sin_dnodes; //[SIN_MAX_MAP_NODES]; + +int sin_numtexinfo; +sin_texinfo_t *sin_texinfo; //[SIN_MAX_MAP_sin_texinfo]; + +int sin_numfaces; +sin_dface_t *sin_dfaces; //[SIN_MAX_MAP_FACES]; + +int sin_numedges; +sin_dedge_t *sin_dedges; //[SIN_MAX_MAP_EDGES]; + +int sin_numleaffaces; +unsigned short *sin_dleaffaces; //[SIN_MAX_MAP_LEAFFACES]; + +int sin_numleafbrushes; +unsigned short *sin_dleafbrushes; //[SIN_MAX_MAP_LEAFBRUSHES]; + +int sin_numsurfedges; +int *sin_dsurfedges; //[SIN_MAX_MAP_SURFEDGES]; + +int sin_numbrushes; +sin_dbrush_t *sin_dbrushes; //[SIN_MAX_MAP_BRUSHES]; + +int sin_numbrushsides; +sin_dbrushside_t *sin_dbrushsides; //[SIN_MAX_MAP_BRUSHSIDES]; + +int sin_numareas; +sin_darea_t *sin_dareas; //[SIN_MAX_MAP_AREAS]; + +int sin_numareaportals; +sin_dareaportal_t *sin_dareaportals; //[SIN_MAX_MAP_AREAPORTALS]; + +int sin_numlightinfo; +sin_lightvalue_t *sin_lightinfo; //[SIN_MAX_MAP_LIGHTINFO]; + +byte sin_dpop[256]; + +char sin_dbrushsidetextured[SIN_MAX_MAP_BRUSHSIDES]; + +int sin_bspallocated = false; +int sin_allocatedbspmem = 0; + +void Sin_AllocMaxBSP( void ) { + //models + sin_nummodels = 0; + sin_dmodels = (sin_dmodel_t *) GetClearedMemory( SIN_MAX_MAP_MODELS * sizeof( sin_dmodel_t ) ); + sin_allocatedbspmem += SIN_MAX_MAP_MODELS * sizeof( sin_dmodel_t ); + //vis data + sin_visdatasize = 0; + sin_dvisdata = (byte *) GetClearedMemory( SIN_MAX_MAP_VISIBILITY * sizeof( byte ) ); + sin_dvis = (sin_dvis_t *) sin_dvisdata; + sin_allocatedbspmem += SIN_MAX_MAP_VISIBILITY * sizeof( byte ); + //light data + sin_lightdatasize = 0; + sin_dlightdata = (byte *) GetClearedMemory( SIN_MAX_MAP_LIGHTING * sizeof( byte ) ); + sin_allocatedbspmem += SIN_MAX_MAP_LIGHTING * sizeof( byte ); + //entity data + sin_entdatasize = 0; + sin_dentdata = (char *) GetClearedMemory( SIN_MAX_MAP_ENTSTRING * sizeof( char ) ); + sin_allocatedbspmem += SIN_MAX_MAP_ENTSTRING * sizeof( char ); + //leafs + sin_numleafs = 0; + sin_dleafs = (sin_dleaf_t *) GetClearedMemory( SIN_MAX_MAP_LEAFS * sizeof( sin_dleaf_t ) ); + sin_allocatedbspmem += SIN_MAX_MAP_LEAFS * sizeof( sin_dleaf_t ); + //planes + sin_numplanes = 0; + sin_dplanes = (sin_dplane_t *) GetClearedMemory( SIN_MAX_MAP_PLANES * sizeof( sin_dplane_t ) ); + sin_allocatedbspmem += SIN_MAX_MAP_PLANES * sizeof( sin_dplane_t ); + //vertexes + sin_numvertexes = 0; + sin_dvertexes = (sin_dvertex_t *) GetClearedMemory( SIN_MAX_MAP_VERTS * sizeof( sin_dvertex_t ) ); + sin_allocatedbspmem += SIN_MAX_MAP_VERTS * sizeof( sin_dvertex_t ); + //nodes + sin_numnodes = 0; + sin_dnodes = (sin_dnode_t *) GetClearedMemory( SIN_MAX_MAP_NODES * sizeof( sin_dnode_t ) ); + sin_allocatedbspmem += SIN_MAX_MAP_NODES * sizeof( sin_dnode_t ); + //texture info + sin_numtexinfo = 0; + sin_texinfo = (sin_texinfo_t *) GetClearedMemory( SIN_MAX_MAP_TEXINFO * sizeof( sin_texinfo_t ) ); + sin_allocatedbspmem += SIN_MAX_MAP_TEXINFO * sizeof( sin_texinfo_t ); + //faces + sin_numfaces = 0; + sin_dfaces = (sin_dface_t *) GetClearedMemory( SIN_MAX_MAP_FACES * sizeof( sin_dface_t ) ); + sin_allocatedbspmem += SIN_MAX_MAP_FACES * sizeof( sin_dface_t ); + //edges + sin_numedges = 0; + sin_dedges = (sin_dedge_t *) GetClearedMemory( SIN_MAX_MAP_EDGES * sizeof( sin_dedge_t ) ); + sin_allocatedbspmem += SIN_MAX_MAP_EDGES * sizeof( sin_dedge_t ); + //leaf faces + sin_numleaffaces = 0; + sin_dleaffaces = (unsigned short *) GetClearedMemory( SIN_MAX_MAP_LEAFFACES * sizeof( unsigned short ) ); + sin_allocatedbspmem += SIN_MAX_MAP_LEAFFACES * sizeof( unsigned short ); + //leaf brushes + sin_numleafbrushes = 0; + sin_dleafbrushes = (unsigned short *) GetClearedMemory( SIN_MAX_MAP_LEAFBRUSHES * sizeof( unsigned short ) ); + sin_allocatedbspmem += SIN_MAX_MAP_LEAFBRUSHES * sizeof( unsigned short ); + //surface edges + sin_numsurfedges = 0; + sin_dsurfedges = (int *) GetClearedMemory( SIN_MAX_MAP_SURFEDGES * sizeof( int ) ); + sin_allocatedbspmem += SIN_MAX_MAP_SURFEDGES * sizeof( int ); + //brushes + sin_numbrushes = 0; + sin_dbrushes = (sin_dbrush_t *) GetClearedMemory( SIN_MAX_MAP_BRUSHES * sizeof( sin_dbrush_t ) ); + sin_allocatedbspmem += SIN_MAX_MAP_BRUSHES * sizeof( sin_dbrush_t ); + //brushsides + sin_numbrushsides = 0; + sin_dbrushsides = (sin_dbrushside_t *) GetClearedMemory( SIN_MAX_MAP_BRUSHSIDES * sizeof( sin_dbrushside_t ) ); + sin_allocatedbspmem += SIN_MAX_MAP_BRUSHSIDES * sizeof( sin_dbrushside_t ); + //areas + sin_numareas = 0; + sin_dareas = (sin_darea_t *) GetClearedMemory( SIN_MAX_MAP_AREAS * sizeof( sin_darea_t ) ); + sin_allocatedbspmem += SIN_MAX_MAP_AREAS * sizeof( sin_darea_t ); + //area portals + sin_numareaportals = 0; + sin_dareaportals = (sin_dareaportal_t *) GetClearedMemory( SIN_MAX_MAP_AREAPORTALS * sizeof( sin_dareaportal_t ) ); + sin_allocatedbspmem += SIN_MAX_MAP_AREAPORTALS * sizeof( sin_dareaportal_t ); + //light info + sin_numlightinfo = 0; + sin_lightinfo = (sin_lightvalue_t *) GetClearedMemory( SIN_MAX_MAP_LIGHTINFO * sizeof( sin_lightvalue_t ) ); + sin_allocatedbspmem += SIN_MAX_MAP_LIGHTINFO * sizeof( sin_lightvalue_t ); + //print allocated memory + Log_Print( "allocated " ); + PrintMemorySize( sin_allocatedbspmem ); + Log_Print( " of BSP memory\n" ); +} //end of the function Sin_AllocMaxBSP + +void Sin_FreeMaxBSP( void ) { + //models + sin_nummodels = 0; + FreeMemory( sin_dmodels ); + sin_dmodels = NULL; + //vis data + sin_visdatasize = 0; + FreeMemory( sin_dvisdata ); + sin_dvisdata = NULL; + sin_dvis = NULL; + //light data + sin_lightdatasize = 0; + FreeMemory( sin_dlightdata ); + sin_dlightdata = NULL; + //entity data + sin_entdatasize = 0; + FreeMemory( sin_dentdata ); + sin_dentdata = NULL; + //leafs + sin_numleafs = 0; + FreeMemory( sin_dleafs ); + sin_dleafs = NULL; + //planes + sin_numplanes = 0; + FreeMemory( sin_dplanes ); + sin_dplanes = NULL; + //vertexes + sin_numvertexes = 0; + FreeMemory( sin_dvertexes ); + sin_dvertexes = NULL; + //nodes + sin_numnodes = 0; + FreeMemory( sin_dnodes ); + sin_dnodes = NULL; + //texture info + sin_numtexinfo = 0; + FreeMemory( sin_texinfo ); + sin_texinfo = NULL; + //faces + sin_numfaces = 0; + FreeMemory( sin_dfaces ); + sin_dfaces = NULL; + //edges + sin_numedges = 0; + FreeMemory( sin_dedges ); + sin_dedges = NULL; + //leaf faces + sin_numleaffaces = 0; + FreeMemory( sin_dleaffaces ); + sin_dleaffaces = NULL; + //leaf brushes + sin_numleafbrushes = 0; + FreeMemory( sin_dleafbrushes ); + sin_dleafbrushes = NULL; + //surface edges + sin_numsurfedges = 0; + FreeMemory( sin_dsurfedges ); + sin_dsurfedges = NULL; + //brushes + sin_numbrushes = 0; + FreeMemory( sin_dbrushes ); + sin_dbrushes = NULL; + //brushsides + sin_numbrushsides = 0; + FreeMemory( sin_dbrushsides ); + sin_dbrushsides = NULL; + //areas + sin_numareas = 0; + FreeMemory( sin_dareas ); + sin_dareas = NULL; + //area portals + sin_numareaportals = 0; + FreeMemory( sin_dareaportals ); + sin_dareaportals = NULL; + //light info + sin_numlightinfo = 0; + FreeMemory( sin_lightinfo ); + sin_lightinfo = NULL; + // + Log_Print( "freed " ); + PrintMemorySize( sin_allocatedbspmem ); + Log_Print( " of BSP memory\n" ); + sin_allocatedbspmem = 0; +} //end of the function Sin_FreeMaxBSP + +#define WCONVEX_EPSILON 0.5 + +//=========================================================================== +// returns the amount the face and the winding overlap +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float Sin_FaceOnWinding( sin_dface_t *face, winding_t *winding ) { + int i, edgenum, side; + float dist, area; + sin_dplane_t plane; + vec_t *v1, *v2; + vec3_t normal, edgevec; + winding_t *w; + + // + w = CopyWinding( winding ); + memcpy( &plane, &sin_dplanes[face->planenum], sizeof( sin_dplane_t ) ); + //check on which side of the plane the face is + if ( face->side ) { + VectorNegate( plane.normal, plane.normal ); + plane.dist = -plane.dist; + } //end if + for ( i = 0; i < face->numedges && w; i++ ) + { + //get the first and second vertex of the edge + edgenum = sin_dsurfedges[face->firstedge + i]; + side = edgenum > 0; + //if the face plane is flipped + v1 = sin_dvertexes[sin_dedges[abs( edgenum )].v[side]].point; + v2 = sin_dvertexes[sin_dedges[abs( edgenum )].v[!side]].point; + //create a plane through the edge vector, orthogonal to the face plane + //and with the normal vector pointing out of the face + VectorSubtract( v1, v2, edgevec ); + CrossProduct( edgevec, plane.normal, normal ); + VectorNormalize( normal ); + dist = DotProduct( normal, v1 ); + // + ChopWindingInPlace( &w, normal, dist, 0.9 ); //CLIP_EPSILON + } //end for + if ( w ) { + area = WindingArea( w ); + FreeWinding( w ); + return area; + } //end if + return 0; +} //end of the function Sin_FaceOnWinding +//=========================================================================== +// creates a winding for the given brush side on the given brush +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +winding_t *Sin_BrushSideWinding( sin_dbrush_t *brush, sin_dbrushside_t *baseside ) { + int i; + sin_dplane_t *baseplane, *plane; + sin_dbrushside_t *side; + winding_t *w; + + //create a winding for the brush side with the given planenumber + baseplane = &sin_dplanes[baseside->planenum]; + w = BaseWindingForPlane( baseplane->normal, baseplane->dist ); + for ( i = 0; i < brush->numsides && w; i++ ) + { + side = &sin_dbrushsides[brush->firstside + i]; + //don't chop with the base plane + if ( side->planenum == baseside->planenum ) { + continue; + } + //also don't use planes that are almost equal + plane = &sin_dplanes[side->planenum]; + if ( DotProduct( baseplane->normal, plane->normal ) > 0.999 + && fabs( baseplane->dist - plane->dist ) < 0.01 ) { + continue; + } + // + plane = &sin_dplanes[side->planenum ^ 1]; + ChopWindingInPlace( &w, plane->normal, plane->dist, 0 ); //CLIP_EPSILON); + } //end for + return w; +} //end of the function Sin_BrushSideWinding +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int Sin_HintSkipBrush( sin_dbrush_t *brush ) { + int j; + sin_dbrushside_t *brushside; + + for ( j = 0; j < brush->numsides; j++ ) + { + brushside = &sin_dbrushsides[brush->firstside + j]; + if ( brushside->texinfo > 0 ) { + if ( sin_texinfo[brushside->texinfo].flags & ( SURF_SKIP | SURF_HINT ) ) { + return true; + } //end if + } //end if + } //end for + return false; +} //end of the function Sin_HintSkipBrush +//=========================================================================== +// fix screwed brush texture references +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean WindingIsTiny( winding_t *w ); + +void Sin_FixTextureReferences( void ) { + int i, j, k, we; + sin_dbrushside_t *brushside; + sin_dbrush_t *brush; + sin_dface_t *face; + winding_t *w; + + memset( sin_dbrushsidetextured, false, SIN_MAX_MAP_BRUSHSIDES ); + //go over all the brushes + for ( i = 0; i < sin_numbrushes; i++ ) + { + brush = &sin_dbrushes[i]; + //hint brushes are not textured + if ( Sin_HintSkipBrush( brush ) ) { + continue; + } + //go over all the sides of the brush + for ( j = 0; j < brush->numsides; j++ ) + { + brushside = &sin_dbrushsides[brush->firstside + j]; + // + w = Sin_BrushSideWinding( brush, brushside ); + if ( !w ) { + sin_dbrushsidetextured[brush->firstside + j] = true; + continue; + } //end if + else + { + //RemoveEqualPoints(w, 0.2); + if ( WindingIsTiny( w ) ) { + FreeWinding( w ); + sin_dbrushsidetextured[brush->firstside + j] = true; + continue; + } //end if + else + { + we = WindingError( w ); + if ( we == WE_NOTENOUGHPOINTS + || we == WE_SMALLAREA + || we == WE_POINTBOGUSRANGE +// || we == WE_NONCONVEX + ) { + FreeWinding( w ); + sin_dbrushsidetextured[brush->firstside + j] = true; + continue; + } //end if + } //end else + } //end else + if ( WindingArea( w ) < 20 ) { + sin_dbrushsidetextured[brush->firstside + j] = true; + } //end if + //find a face for texturing this brush + for ( k = 0; k < sin_numfaces; k++ ) + { + face = &sin_dfaces[k]; + //if the face is in the same plane as the brush side + if ( ( face->planenum & ~1 ) != ( brushside->planenum & ~1 ) ) { + continue; + } + //if the face is partly or totally on the brush side + if ( Sin_FaceOnWinding( face, w ) ) { + brushside->texinfo = face->texinfo; + sin_dbrushsidetextured[brush->firstside + j] = true; + break; + } //end if + } //end for + FreeWinding( w ); + } //end for + } //end for +} //end of the function Sin_FixTextureReferences*/ + +/* +=============== +CompressVis + +=============== +*/ +int Sin_CompressVis( byte *vis, byte *dest ) { + int j; + int rep; + int visrow; + byte *dest_p; + + dest_p = dest; +// visrow = (r_numvisleafs + 7)>>3; + visrow = ( sin_dvis->numclusters + 7 ) >> 3; + + for ( j = 0 ; j < visrow ; j++ ) + { + *dest_p++ = vis[j]; + if ( vis[j] ) { + continue; + } + + rep = 1; + for ( j++; j < visrow ; j++ ) + if ( vis[j] || rep == 255 ) { + break; + } else { + rep++; + } + *dest_p++ = rep; + j--; + } + + return dest_p - dest; +} //end of the function Sin_CompressVis + + +/* +=================== +DecompressVis +=================== +*/ +void Sin_DecompressVis( byte *in, byte *decompressed ) { + int c; + byte *out; + int row; + +// row = (r_numvisleafs+7)>>3; + row = ( sin_dvis->numclusters + 7 ) >> 3; + out = decompressed; + + do + { + if ( *in ) { + *out++ = *in++; + continue; + } + + c = in[1]; + if ( !c ) { + Error( "DecompressVis: 0 repeat" ); + } + in += 2; + while ( c ) + { + *out++ = 0; + c--; + } + } while ( out - decompressed < row ); +} //end of the function Sin_DecompressVis + +//============================================================================= + +/* +============= +Sin_SwapBSPFile + +Byte swaps all data in a bsp file. +============= +*/ +void Sin_SwapBSPFile( qboolean todisk ) { + int i, j; + sin_dmodel_t *d; + + +// models + for ( i = 0 ; i < sin_nummodels ; i++ ) + { + d = &sin_dmodels[i]; + + d->firstface = LittleLong( d->firstface ); + d->numfaces = LittleLong( d->numfaces ); + d->headnode = LittleLong( d->headnode ); + + for ( j = 0 ; j < 3 ; j++ ) + { + d->mins[j] = LittleFloat( d->mins[j] ); + d->maxs[j] = LittleFloat( d->maxs[j] ); + d->origin[j] = LittleFloat( d->origin[j] ); + } + } + +// +// vertexes +// + for ( i = 0 ; i < sin_numvertexes ; i++ ) + { + for ( j = 0 ; j < 3 ; j++ ) + sin_dvertexes[i].point[j] = LittleFloat( sin_dvertexes[i].point[j] ); + } + +// +// planes +// + for ( i = 0 ; i < sin_numplanes ; i++ ) + { + for ( j = 0 ; j < 3 ; j++ ) + sin_dplanes[i].normal[j] = LittleFloat( sin_dplanes[i].normal[j] ); + sin_dplanes[i].dist = LittleFloat( sin_dplanes[i].dist ); + sin_dplanes[i].type = LittleLong( sin_dplanes[i].type ); + } + +// +// sin_texinfos +// + for ( i = 0; i < sin_numtexinfo; i++ ) + { + for ( j = 0 ; j < 8 ; j++ ) + sin_texinfo[i].vecs[0][j] = LittleFloat( sin_texinfo[i].vecs[0][j] ); +#ifdef SIN + sin_texinfo[i].trans_mag = LittleFloat( sin_texinfo[i].trans_mag ); + sin_texinfo[i].trans_angle = LittleLong( sin_texinfo[i].trans_angle ); + sin_texinfo[i].animtime = LittleFloat( sin_texinfo[i].animtime ); + sin_texinfo[i].nonlit = LittleFloat( sin_texinfo[i].nonlit ); + sin_texinfo[i].translucence = LittleFloat( sin_texinfo[i].translucence ); + sin_texinfo[i].friction = LittleFloat( sin_texinfo[i].friction ); + sin_texinfo[i].restitution = LittleFloat( sin_texinfo[i].restitution ); + sin_texinfo[i].flags = LittleUnsigned( sin_texinfo[i].flags ); +#else + sin_texinfo[i].value = LittleLong( sin_texinfo[i].value ); + sin_texinfo[i].flags = LittleLong( sin_texinfo[i].flags ); +#endif + sin_texinfo[i].nexttexinfo = LittleLong( sin_texinfo[i].nexttexinfo ); + } + +#ifdef SIN +// +// lightinfos +// + for ( i = 0; i < sin_numlightinfo; i++ ) + { + for ( j = 0 ; j < 3 ; j++ ) + { + sin_lightinfo[i].color[j] = LittleFloat( sin_lightinfo[i].color[j] ); + } + sin_lightinfo[i].value = LittleLong( sin_lightinfo[i].value ); + sin_lightinfo[i].direct = LittleFloat( sin_lightinfo[i].direct ); + sin_lightinfo[i].directangle = LittleFloat( sin_lightinfo[i].directangle ); + sin_lightinfo[i].directstyle = LittleFloat( sin_lightinfo[i].directstyle ); + } +#endif + +// +// faces +// + for ( i = 0 ; i < sin_numfaces ; i++ ) + { + sin_dfaces[i].texinfo = LittleShort( sin_dfaces[i].texinfo ); +#ifdef SIN + sin_dfaces[i].lightinfo = LittleLong( sin_dfaces[i].lightinfo ); + sin_dfaces[i].planenum = LittleUnsignedShort( sin_dfaces[i].planenum ); +#else + sin_dfaces[i].planenum = LittleShort( sin_dfaces[i].planenum ); +#endif + sin_dfaces[i].side = LittleShort( sin_dfaces[i].side ); + sin_dfaces[i].lightofs = LittleLong( sin_dfaces[i].lightofs ); + sin_dfaces[i].firstedge = LittleLong( sin_dfaces[i].firstedge ); + sin_dfaces[i].numedges = LittleShort( sin_dfaces[i].numedges ); + } + +// +// nodes +// + for ( i = 0 ; i < sin_numnodes ; i++ ) + { + sin_dnodes[i].planenum = LittleLong( sin_dnodes[i].planenum ); + for ( j = 0 ; j < 3 ; j++ ) + { + sin_dnodes[i].mins[j] = LittleShort( sin_dnodes[i].mins[j] ); + sin_dnodes[i].maxs[j] = LittleShort( sin_dnodes[i].maxs[j] ); + } + sin_dnodes[i].children[0] = LittleLong( sin_dnodes[i].children[0] ); + sin_dnodes[i].children[1] = LittleLong( sin_dnodes[i].children[1] ); +#ifdef SIN + sin_dnodes[i].firstface = LittleUnsignedShort( sin_dnodes[i].firstface ); + sin_dnodes[i].numfaces = LittleUnsignedShort( sin_dnodes[i].numfaces ); +#else + sin_dnodes[i].firstface = LittleShort( sin_dnodes[i].firstface ); + sin_dnodes[i].numfaces = LittleShort( sin_dnodes[i].numfaces ); +#endif + } + +// +// leafs +// + for ( i = 0 ; i < sin_numleafs ; i++ ) + { + sin_dleafs[i].contents = LittleLong( sin_dleafs[i].contents ); + sin_dleafs[i].cluster = LittleShort( sin_dleafs[i].cluster ); + sin_dleafs[i].area = LittleShort( sin_dleafs[i].area ); + for ( j = 0 ; j < 3 ; j++ ) + { + sin_dleafs[i].mins[j] = LittleShort( sin_dleafs[i].mins[j] ); + sin_dleafs[i].maxs[j] = LittleShort( sin_dleafs[i].maxs[j] ); + } +#ifdef SIN + sin_dleafs[i].firstleafface = LittleUnsignedShort( sin_dleafs[i].firstleafface ); + sin_dleafs[i].numleaffaces = LittleUnsignedShort( sin_dleafs[i].numleaffaces ); + sin_dleafs[i].firstleafbrush = LittleUnsignedShort( sin_dleafs[i].firstleafbrush ); + sin_dleafs[i].numleafbrushes = LittleUnsignedShort( sin_dleafs[i].numleafbrushes ); +#else + sin_dleafs[i].firstleafface = LittleShort( sin_dleafs[i].firstleafface ); + sin_dleafs[i].numleaffaces = LittleShort( sin_dleafs[i].numleaffaces ); + sin_dleafs[i].firstleafbrush = LittleShort( sin_dleafs[i].firstleafbrush ); + sin_dleafs[i].numleafbrushes = LittleShort( sin_dleafs[i].numleafbrushes ); +#endif + } + +// +// leaffaces +// + for ( i = 0 ; i < sin_numleaffaces ; i++ ) + sin_dleaffaces[i] = LittleShort( sin_dleaffaces[i] ); + +// +// leafbrushes +// + for ( i = 0 ; i < sin_numleafbrushes ; i++ ) + sin_dleafbrushes[i] = LittleShort( sin_dleafbrushes[i] ); + +// +// surfedges +// + for ( i = 0 ; i < sin_numsurfedges ; i++ ) + sin_dsurfedges[i] = LittleLong( sin_dsurfedges[i] ); + +// +// edges +// + for ( i = 0 ; i < sin_numedges ; i++ ) + { +#ifdef SIN + sin_dedges[i].v[0] = LittleUnsignedShort( sin_dedges[i].v[0] ); + sin_dedges[i].v[1] = LittleUnsignedShort( sin_dedges[i].v[1] ); +#else + sin_dedges[i].v[0] = LittleShort( sin_dedges[i].v[0] ); + sin_dedges[i].v[1] = LittleShort( sin_dedges[i].v[1] ); +#endif + } + +// +// brushes +// + for ( i = 0 ; i < sin_numbrushes ; i++ ) + { + sin_dbrushes[i].firstside = LittleLong( sin_dbrushes[i].firstside ); + sin_dbrushes[i].numsides = LittleLong( sin_dbrushes[i].numsides ); + sin_dbrushes[i].contents = LittleLong( sin_dbrushes[i].contents ); + } + +// +// areas +// + for ( i = 0 ; i < sin_numareas ; i++ ) + { + sin_dareas[i].numareaportals = LittleLong( sin_dareas[i].numareaportals ); + sin_dareas[i].firstareaportal = LittleLong( sin_dareas[i].firstareaportal ); + } + +// +// areasportals +// + for ( i = 0 ; i < sin_numareaportals ; i++ ) + { + sin_dareaportals[i].portalnum = LittleLong( sin_dareaportals[i].portalnum ); + sin_dareaportals[i].otherarea = LittleLong( sin_dareaportals[i].otherarea ); + } + +// +// brushsides +// + for ( i = 0 ; i < sin_numbrushsides ; i++ ) + { +#ifdef SIN + sin_dbrushsides[i].planenum = LittleUnsignedShort( sin_dbrushsides[i].planenum ); +#else + sin_dbrushsides[i].planenum = LittleShort( sin_dbrushsides[i].planenum ); +#endif + sin_dbrushsides[i].texinfo = LittleShort( sin_dbrushsides[i].texinfo ); +#ifdef SIN + sin_dbrushsides[i].lightinfo = LittleLong( sin_dbrushsides[i].lightinfo ); +#endif + } + +// +// visibility +// + if ( todisk ) { + j = sin_dvis->numclusters; + } else { + j = LittleLong( sin_dvis->numclusters ); + } + sin_dvis->numclusters = LittleLong( sin_dvis->numclusters ); + for ( i = 0 ; i < j ; i++ ) + { + sin_dvis->bitofs[i][0] = LittleLong( sin_dvis->bitofs[i][0] ); + sin_dvis->bitofs[i][1] = LittleLong( sin_dvis->bitofs[i][1] ); + } +} //end of the function Sin_SwapBSPFile + + +sin_dheader_t *header; +#ifdef SIN +int Sin_CopyLump( int lump, void *dest, int size, int maxsize ) { + int length, ofs; + + length = header->lumps[lump].filelen; + ofs = header->lumps[lump].fileofs; + + if ( length % size ) { + Error( "Sin_LoadBSPFile: odd lump size" ); + } + + if ( ( length / size ) > maxsize ) { + Error( "Sin_LoadBSPFile: exceeded max size for lump %d size %d > maxsize %d\n", lump, ( length / size ), maxsize ); + } + + memcpy( dest, (byte *)header + ofs, length ); + + return length / size; +} +#else +int Sin_CopyLump( int lump, void *dest, int size ) { + int length, ofs; + + length = header->lumps[lump].filelen; + ofs = header->lumps[lump].fileofs; + + if ( length % size ) { + Error( "Sin_LoadBSPFile: odd lump size" ); + } + + memcpy( dest, (byte *)header + ofs, length ); + + return length / size; +} +#endif + +/* +============= +Sin_LoadBSPFile +============= +*/ +void Sin_LoadBSPFile( char *filename, int offset, int length ) { + int i; + +// +// load the file header +// + LoadFile( filename, (void **)&header, offset, length ); + +// swap the header + for ( i = 0 ; i < sizeof( sin_dheader_t ) / 4 ; i++ ) + ( (int *)header )[i] = LittleLong( ( (int *)header )[i] ); + + if ( header->ident != SIN_BSPHEADER && header->ident != SINGAME_BSPHEADER ) { + Error( "%s is not a IBSP file", filename ); + } + if ( header->version != SIN_BSPVERSION && header->version != SINGAME_BSPVERSION ) { + Error( "%s is version %i, not %i", filename, header->version, SIN_BSPVERSION ); + } + +#ifdef SIN + sin_nummodels = Sin_CopyLump( SIN_LUMP_MODELS, sin_dmodels, sizeof( sin_dmodel_t ), SIN_MAX_MAP_MODELS ); + sin_numvertexes = Sin_CopyLump( SIN_LUMP_VERTEXES, sin_dvertexes, sizeof( sin_dvertex_t ), SIN_MAX_MAP_VERTS ); + sin_numplanes = Sin_CopyLump( SIN_LUMP_PLANES, sin_dplanes, sizeof( sin_dplane_t ), SIN_MAX_MAP_PLANES ); + sin_numleafs = Sin_CopyLump( SIN_LUMP_LEAFS, sin_dleafs, sizeof( sin_dleaf_t ), SIN_MAX_MAP_LEAFS ); + sin_numnodes = Sin_CopyLump( SIN_LUMP_NODES, sin_dnodes, sizeof( sin_dnode_t ), SIN_MAX_MAP_NODES ); + sin_numtexinfo = Sin_CopyLump( SIN_LUMP_TEXINFO, sin_texinfo, sizeof( sin_texinfo_t ), SIN_MAX_MAP_TEXINFO ); + sin_numfaces = Sin_CopyLump( SIN_LUMP_FACES, sin_dfaces, sizeof( sin_dface_t ), SIN_MAX_MAP_FACES ); + sin_numleaffaces = Sin_CopyLump( SIN_LUMP_LEAFFACES, sin_dleaffaces, sizeof( sin_dleaffaces[0] ), SIN_MAX_MAP_LEAFFACES ); + sin_numleafbrushes = Sin_CopyLump( SIN_LUMP_LEAFBRUSHES, sin_dleafbrushes, sizeof( sin_dleafbrushes[0] ), SIN_MAX_MAP_LEAFBRUSHES ); + sin_numsurfedges = Sin_CopyLump( SIN_LUMP_SURFEDGES, sin_dsurfedges, sizeof( sin_dsurfedges[0] ), SIN_MAX_MAP_SURFEDGES ); + sin_numedges = Sin_CopyLump( SIN_LUMP_EDGES, sin_dedges, sizeof( sin_dedge_t ), SIN_MAX_MAP_EDGES ); + sin_numbrushes = Sin_CopyLump( SIN_LUMP_BRUSHES, sin_dbrushes, sizeof( sin_dbrush_t ), SIN_MAX_MAP_BRUSHES ); + sin_numbrushsides = Sin_CopyLump( SIN_LUMP_BRUSHSIDES, sin_dbrushsides, sizeof( sin_dbrushside_t ), SIN_MAX_MAP_BRUSHSIDES ); + sin_numareas = Sin_CopyLump( SIN_LUMP_AREAS, sin_dareas, sizeof( sin_darea_t ), SIN_MAX_MAP_AREAS ); + sin_numareaportals = Sin_CopyLump( SIN_LUMP_AREAPORTALS, sin_dareaportals, sizeof( sin_dareaportal_t ), SIN_MAX_MAP_AREAPORTALS ); + sin_numlightinfo = Sin_CopyLump( SIN_LUMP_LIGHTINFO, sin_lightinfo, sizeof( sin_lightvalue_t ), SIN_MAX_MAP_LIGHTINFO ); + + sin_visdatasize = Sin_CopyLump( SIN_LUMP_VISIBILITY, sin_dvisdata, 1, SIN_MAX_MAP_VISIBILITY ); + sin_lightdatasize = Sin_CopyLump( SIN_LUMP_LIGHTING, sin_dlightdata, 1, SIN_MAX_MAP_LIGHTING ); + sin_entdatasize = Sin_CopyLump( SIN_LUMP_ENTITIES, sin_dentdata, 1, SIN_MAX_MAP_ENTSTRING ); + + Sin_CopyLump( SIN_LUMP_POP, sin_dpop, 1, sizeof( sin_dpop ) ); +#else + sin_nummodels = Sin_CopyLump( SIN_LUMP_MODELS, sin_dmodels, sizeof( sin_dmodel_t ) ); + sin_numvertexes = Sin_CopyLump( SIN_LUMP_VERTEXES, sin_dvertexes, sizeof( sin_dvertex_t ) ); + sin_numplanes = Sin_CopyLump( SIN_LUMP_PLANES, sin_dplanes, sizeof( sin_dplane_t ) ); + sin_numleafs = Sin_CopyLump( SIN_LUMP_LEAFS, sin_dleafs, sizeof( sin_dleaf_t ) ); + sin_numnodes = Sin_CopyLump( SIN_LUMP_NODES, sin_dnodes, sizeof( sin_dnode_t ) ); + sin_numtexinfo = Sin_CopyLump( SIN_LUMP_TEXINFO, sin_texinfo, sizeof( sin_texinfo_t ) ); + sin_numfaces = Sin_CopyLump( SIN_LUMP_FACES, sin_dfaces, sizeof( sin_dface_t ) ); + sin_numleaffaces = Sin_CopyLump( SIN_LUMP_LEAFFACES, sin_dleaffaces, sizeof( sin_dleaffaces[0] ) ); + sin_numleafbrushes = Sin_CopyLump( SIN_LUMP_LEAFBRUSHES, sin_dleafbrushes, sizeof( sin_dleafbrushes[0] ) ); + sin_numsurfedges = Sin_CopyLump( SIN_LUMP_SURFEDGES, sin_dsurfedges, sizeof( sin_dsurfedges[0] ) ); + sin_numedges = Sin_CopyLump( SIN_LUMP_EDGES, sin_dedges, sizeof( sin_dedge_t ) ); + sin_numbrushes = Sin_CopyLump( SIN_LUMP_BRUSHES, sin_dbrushes, sizeof( sin_dbrush_t ) ); + sin_numbrushsides = Sin_CopyLump( SIN_LUMP_BRUSHSIDES, sin_dbrushsides, sizeof( sin_dbrushside_t ) ); + sin_numareas = Sin_CopyLump( SIN_LUMP_AREAS, sin_dareas, sizeof( sin_darea_t ) ); + sin_numareaportals = Sin_CopyLump( SIN_LUMP_AREAPORTALS, sin_dareaportals, sizeof( sin_dareaportal_t ) ); + + sin_visdatasize = Sin_CopyLump( SIN_LUMP_VISIBILITY, sin_dvisdata, 1 ); + sin_lightdatasize = Sin_CopyLump( SIN_LUMP_LIGHTING, sin_dlightdata, 1 ); + sin_entdatasize = Sin_CopyLump( SIN_LUMP_ENTITIES, sin_dentdata, 1 ); + + Sin_CopyLump( SIN_LUMP_POP, sin_dpop, 1 ); +#endif + + FreeMemory( header ); // everything has been copied out + +// +// swap everything +// + Sin_SwapBSPFile( false ); +} //end of the function Sin_LoadBSPFile + +/* +============= +Sin_LoadBSPFilesTexinfo + +Only loads the sin_texinfo lump, so qdata can scan for textures +============= +*/ +void Sin_LoadBSPFileTexinfo( char *filename ) { + int i; + FILE *f; + int length, ofs; + + header = GetMemory( sizeof( sin_dheader_t ) ); + + f = fopen( filename, "rb" ); + fread( header, sizeof( sin_dheader_t ), 1, f ); + +// swap the header + for ( i = 0 ; i < sizeof( sin_dheader_t ) / 4 ; i++ ) + ( (int *)header )[i] = LittleLong( ( (int *)header )[i] ); + + if ( header->ident != SIN_BSPHEADER && header->ident != SINGAME_BSPHEADER ) { + Error( "%s is not a IBSP file", filename ); + } + if ( header->version != SIN_BSPVERSION && header->version != SINGAME_BSPVERSION ) { + Error( "%s is version %i, not %i", filename, header->version, SIN_BSPVERSION ); + } + + + length = header->lumps[SIN_LUMP_TEXINFO].filelen; + ofs = header->lumps[SIN_LUMP_TEXINFO].fileofs; + + fseek( f, ofs, SEEK_SET ); + fread( sin_texinfo, length, 1, f ); + fclose( f ); + + sin_numtexinfo = length / sizeof( sin_texinfo_t ); + + FreeMemory( header ); // everything has been copied out + + Sin_SwapBSPFile( false ); +} //end of the function Sin_LoadBSPFilesTexinfo + + +//============================================================================ + +FILE *wadfile; +sin_dheader_t outheader; + +#ifdef SIN +void Sin_AddLump( int lumpnum, void *data, int len, int size, int maxsize ) { + sin_lump_t *lump; + int totallength; + + totallength = len * size; + + if ( len > maxsize ) { + Error( "Sin_WriteBSPFile: exceeded max size for lump %d size %d > maxsize %d\n", lumpnum, len, maxsize ); + } + + lump = &header->lumps[lumpnum]; + + lump->fileofs = LittleLong( ftell( wadfile ) ); + lump->filelen = LittleLong( totallength ); + SafeWrite( wadfile, data, ( totallength + 3 ) & ~3 ); +} +#else +void Sin_AddLump( int lumpnum, void *data, int len ) { + sin_lump_t *lump; + + lump = &header->lumps[lumpnum]; + + lump->fileofs = LittleLong( ftell( wadfile ) ); + lump->filelen = LittleLong( len ); + SafeWrite( wadfile, data, ( len + 3 ) & ~3 ); +} +#endif +/* +============= +Sin_WriteBSPFile + +Swaps the bsp file in place, so it should not be referenced again +============= +*/ +void Sin_WriteBSPFile( char *filename ) { + header = &outheader; + memset( header, 0, sizeof( sin_dheader_t ) ); + + Sin_SwapBSPFile( true ); + + header->ident = LittleLong( SIN_BSPHEADER ); + header->version = LittleLong( SIN_BSPVERSION ); + + wadfile = SafeOpenWrite( filename ); + SafeWrite( wadfile, header, sizeof( sin_dheader_t ) ); // overwritten later + +#ifdef SIN + Sin_AddLump( SIN_LUMP_PLANES, sin_dplanes, sin_numplanes, sizeof( sin_dplane_t ), SIN_MAX_MAP_PLANES ); + Sin_AddLump( SIN_LUMP_LEAFS, sin_dleafs, sin_numleafs, sizeof( sin_dleaf_t ), SIN_MAX_MAP_LEAFS ); + Sin_AddLump( SIN_LUMP_VERTEXES, sin_dvertexes, sin_numvertexes, sizeof( sin_dvertex_t ), SIN_MAX_MAP_VERTS ); + Sin_AddLump( SIN_LUMP_NODES, sin_dnodes, sin_numnodes, sizeof( sin_dnode_t ), SIN_MAX_MAP_NODES ); + Sin_AddLump( SIN_LUMP_TEXINFO, sin_texinfo, sin_numtexinfo, sizeof( sin_texinfo_t ), SIN_MAX_MAP_TEXINFO ); + Sin_AddLump( SIN_LUMP_FACES, sin_dfaces, sin_numfaces, sizeof( sin_dface_t ), SIN_MAX_MAP_FACES ); + Sin_AddLump( SIN_LUMP_BRUSHES, sin_dbrushes, sin_numbrushes, sizeof( sin_dbrush_t ), SIN_MAX_MAP_BRUSHES ); + Sin_AddLump( SIN_LUMP_BRUSHSIDES, sin_dbrushsides, sin_numbrushsides, sizeof( sin_dbrushside_t ), SIN_MAX_MAP_BRUSHSIDES ); + Sin_AddLump( SIN_LUMP_LEAFFACES, sin_dleaffaces, sin_numleaffaces, sizeof( sin_dleaffaces[0] ), SIN_MAX_MAP_LEAFFACES ); + Sin_AddLump( SIN_LUMP_LEAFBRUSHES, sin_dleafbrushes, sin_numleafbrushes, sizeof( sin_dleafbrushes[0] ), SIN_MAX_MAP_LEAFBRUSHES ); + Sin_AddLump( SIN_LUMP_SURFEDGES, sin_dsurfedges, sin_numsurfedges, sizeof( sin_dsurfedges[0] ), SIN_MAX_MAP_SURFEDGES ); + Sin_AddLump( SIN_LUMP_EDGES, sin_dedges, sin_numedges, sizeof( sin_dedge_t ), SIN_MAX_MAP_EDGES ); + Sin_AddLump( SIN_LUMP_MODELS, sin_dmodels, sin_nummodels, sizeof( sin_dmodel_t ), SIN_MAX_MAP_MODELS ); + Sin_AddLump( SIN_LUMP_AREAS, sin_dareas, sin_numareas, sizeof( sin_darea_t ), SIN_MAX_MAP_AREAS ); + Sin_AddLump( SIN_LUMP_AREAPORTALS, sin_dareaportals, sin_numareaportals, sizeof( sin_dareaportal_t ), SIN_MAX_MAP_AREAPORTALS ); + Sin_AddLump( SIN_LUMP_LIGHTINFO, sin_lightinfo, sin_numlightinfo, sizeof( sin_lightvalue_t ), SIN_MAX_MAP_LIGHTINFO ); + + Sin_AddLump( SIN_LUMP_LIGHTING, sin_dlightdata, sin_lightdatasize, 1, SIN_MAX_MAP_LIGHTING ); + Sin_AddLump( SIN_LUMP_VISIBILITY, sin_dvisdata, sin_visdatasize, 1, SIN_MAX_MAP_VISIBILITY ); + Sin_AddLump( SIN_LUMP_ENTITIES, sin_dentdata, sin_entdatasize, 1, SIN_MAX_MAP_ENTSTRING ); + Sin_AddLump( SIN_LUMP_POP, sin_dpop, sizeof( sin_dpop ), 1, sizeof( sin_dpop ) ); +#else + Sin_AddLump( SIN_LUMP_PLANES, sin_dplanes, sin_numplanes * sizeof( sin_dplane_t ) ); + Sin_AddLump( SIN_LUMP_LEAFS, sin_dleafs, sin_numleafs * sizeof( sin_dleaf_t ) ); + Sin_AddLump( SIN_LUMP_VERTEXES, sin_dvertexes, sin_numvertexes * sizeof( sin_dvertex_t ) ); + Sin_AddLump( SIN_LUMP_NODES, sin_dnodes, sin_numnodes * sizeof( sin_dnode_t ) ); + Sin_AddLump( SIN_LUMP_TEXINFO, sin_texinfo, sin_numtexinfo * sizeof( sin_texinfo_t ) ); + Sin_AddLump( SIN_LUMP_FACES, sin_dfaces, sin_numfaces * sizeof( sin_dface_t ) ); + Sin_AddLump( SIN_LUMP_BRUSHES, sin_dbrushes, sin_numbrushes * sizeof( sin_dbrush_t ) ); + Sin_AddLump( SIN_LUMP_BRUSHSIDES, sin_dbrushsides, sin_numbrushsides * sizeof( sin_dbrushside_t ) ); + Sin_AddLump( SIN_LUMP_LEAFFACES, sin_dleaffaces, sin_numleaffaces * sizeof( sin_dleaffaces[0] ) ); + Sin_AddLump( SIN_LUMP_LEAFBRUSHES, sin_dleafbrushes, sin_numleafbrushes * sizeof( sin_dleafbrushes[0] ) ); + Sin_AddLump( SIN_LUMP_SURFEDGES, sin_dsurfedges, sin_numsurfedges * sizeof( sin_dsurfedges[0] ) ); + Sin_AddLump( SIN_LUMP_EDGES, sin_dedges, sin_numedges * sizeof( sin_dedge_t ) ); + Sin_AddLump( SIN_LUMP_MODELS, sin_dmodels, sin_nummodels * sizeof( sin_dmodel_t ) ); + Sin_AddLump( SIN_LUMP_AREAS, sin_dareas, sin_numareas * sizeof( sin_darea_t ) ); + Sin_AddLump( SIN_LUMP_AREAPORTALS, sin_dareaportals, sin_numareaportals * sizeof( sin_dareaportal_t ) ); + + Sin_AddLump( SIN_LUMP_LIGHTING, sin_dlightdata, sin_lightdatasize ); + Sin_AddLump( SIN_LUMP_VISIBILITY, sin_dvisdata, sin_visdatasize ); + Sin_AddLump( SIN_LUMP_ENTITIES, sin_dentdata, sin_entdatasize ); + Sin_AddLump( SIN_LUMP_POP, sin_dpop, sizeof( sin_dpop ) ); +#endif + + fseek( wadfile, 0, SEEK_SET ); + SafeWrite( wadfile, header, sizeof( sin_dheader_t ) ); + fclose( wadfile ); +} + +//============================================================================ + + +//============================================ + +/* +================ +ParseEntities + +Parses the sin_dentdata string into entities +================ +*/ +void Sin_ParseEntities( void ) { + script_t *script; + + num_entities = 0; + script = LoadScriptMemory( sin_dentdata, sin_entdatasize, "*sin bsp file" ); + SetScriptFlags( script, SCFL_NOSTRINGWHITESPACES | + SCFL_NOSTRINGESCAPECHARS ); + + while ( ParseEntity( script ) ) + { + } //end while + + FreeScript( script ); +} //end of the function Sin_ParseEntities + + +/* +================ +UnparseEntities + +Generates the sin_dentdata string from all the entities +================ +*/ +void Sin_UnparseEntities( void ) { + char *buf, *end; + epair_t *ep; + char line[2048]; + int i; + char key[1024], value[1024]; + + buf = sin_dentdata; + end = buf; + *end = 0; + + for ( i = 0 ; i < num_entities ; i++ ) + { + ep = entities[i].epairs; + if ( !ep ) { + continue; // ent got removed + + } + strcat( end,"{\n" ); + end += 2; + + for ( ep = entities[i].epairs ; ep ; ep = ep->next ) + { + strcpy( key, ep->key ); + StripTrailing( key ); + strcpy( value, ep->value ); + StripTrailing( value ); + + sprintf( line, "\"%s\" \"%s\"\n", key, value ); + strcat( end, line ); + end += strlen( line ); + } + strcat( end,"}\n" ); + end += 2; + + if ( end > buf + SIN_MAX_MAP_ENTSTRING ) { + Error( "Entity text too long" ); + } + } + sin_entdatasize = end - buf + 1; +} //end of the function Sin_UnparseEntities + +#ifdef SIN +void FreeValueKeys( entity_t *ent ) { + epair_t *ep,*next; + + for ( ep = ent->epairs ; ep ; ep = next ) + { + next = ep->next; + FreeMemory( ep->value ); + FreeMemory( ep->key ); + FreeMemory( ep ); + } + ent->epairs = NULL; +} +#endif + +/* +============= +Sin_PrintBSPFileSizes + +Dumps info about current file +============= +*/ +void Sin_PrintBSPFileSizes( void ) { + if ( !num_entities ) { + Sin_ParseEntities(); + } + + Log_Print( "%6i models %7i\n" + ,sin_nummodels, (int)( sin_nummodels * sizeof( sin_dmodel_t ) ) ); + Log_Print( "%6i brushes %7i\n" + ,sin_numbrushes, (int)( sin_numbrushes * sizeof( sin_dbrush_t ) ) ); + Log_Print( "%6i brushsides %7i\n" + ,sin_numbrushsides, (int)( sin_numbrushsides * sizeof( sin_dbrushside_t ) ) ); + Log_Print( "%6i planes %7i\n" + ,sin_numplanes, (int)( sin_numplanes * sizeof( sin_dplane_t ) ) ); + Log_Print( "%6i texinfo %7i\n" + ,sin_numtexinfo, (int)( sin_numtexinfo * sizeof( sin_texinfo_t ) ) ); +#ifdef SIN + Log_Print( "%6i lightinfo %7i\n" + ,sin_numlightinfo, (int)( sin_numlightinfo * sizeof( sin_lightvalue_t ) ) ); +#endif + Log_Print( "%6i entdata %7i\n", num_entities, sin_entdatasize ); + + Log_Print( "\n" ); + + Log_Print( "%6i vertexes %7i\n" + ,sin_numvertexes, (int)( sin_numvertexes * sizeof( sin_dvertex_t ) ) ); + Log_Print( "%6i nodes %7i\n" + ,sin_numnodes, (int)( sin_numnodes * sizeof( sin_dnode_t ) ) ); + Log_Print( "%6i faces %7i\n" + ,sin_numfaces, (int)( sin_numfaces * sizeof( sin_dface_t ) ) ); + Log_Print( "%6i leafs %7i\n" + ,sin_numleafs, (int)( sin_numleafs * sizeof( sin_dleaf_t ) ) ); + Log_Print( "%6i leaffaces %7i\n" + ,sin_numleaffaces, (int)( sin_numleaffaces * sizeof( sin_dleaffaces[0] ) ) ); + Log_Print( "%6i leafbrushes %7i\n" + ,sin_numleafbrushes, (int)( sin_numleafbrushes * sizeof( sin_dleafbrushes[0] ) ) ); + Log_Print( "%6i surfedges %7i\n" + ,sin_numsurfedges, (int)( sin_numsurfedges * sizeof( sin_dsurfedges[0] ) ) ); + Log_Print( "%6i edges %7i\n" + ,sin_numedges, (int)( sin_numedges * sizeof( sin_dedge_t ) ) ); + Log_Print( " lightdata %7i\n", sin_lightdatasize ); + Log_Print( " visdata %7i\n", sin_visdatasize ); +} diff --git a/src/bspc/l_bsp_sin.h b/src/bspc/l_bsp_sin.h new file mode 100644 index 0000000..2ba1843 --- /dev/null +++ b/src/bspc/l_bsp_sin.h @@ -0,0 +1,113 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +#include "sinfiles.h" + +#define SINGAME_BSPHEADER ( ( 'P' << 24 ) + ( 'S' << 16 ) + ( 'B' << 8 ) + 'R' ) //RBSP +#define SINGAME_BSPVERSION 1 + +#define SIN_BSPHEADER ( ( 'P' << 24 ) + ( 'S' << 16 ) + ( 'B' << 8 ) + 'I' ) //IBSP +#define SIN_BSPVERSION 41 + + +extern int sin_nummodels; +extern sin_dmodel_t *sin_dmodels; //[MAX_MAP_MODELS]; + +extern int sin_visdatasize; +extern byte *sin_dvisdata; //[MAX_MAP_VISIBILITY]; +extern sin_dvis_t *sin_dvis; // = (dvis_t *)sin_sin_dvisdata; + +extern int sin_lightdatasize; +extern byte *sin_dlightdata; //[MAX_MAP_LIGHTING]; + +extern int sin_entdatasize; +extern char *sin_dentdata; //[MAX_MAP_ENTSTRING]; + +extern int sin_numleafs; +extern sin_dleaf_t *sin_dleafs; //[MAX_MAP_LEAFS]; + +extern int sin_numplanes; +extern sin_dplane_t *sin_dplanes; //[MAX_MAP_PLANES]; + +extern int sin_numvertexes; +extern sin_dvertex_t *sin_dvertexes; //[MAX_MAP_VERTS]; + +extern int sin_numnodes; +extern sin_dnode_t *sin_dnodes; //[MAX_MAP_NODES]; + +extern int sin_numtexinfo; +extern sin_texinfo_t *sin_texinfo; //[MAX_MAP_sin_texinfo]; + +extern int sin_numfaces; +extern sin_dface_t *sin_dfaces; //[MAX_MAP_FACES]; + +extern int sin_numedges; +extern sin_dedge_t *sin_dedges; //[MAX_MAP_EDGES]; + +extern int sin_numleaffaces; +extern unsigned short *sin_dleaffaces; //[MAX_MAP_LEAFFACES]; + +extern int sin_numleafbrushes; +extern unsigned short *sin_dleafbrushes; //[MAX_MAP_LEAFBRUSHES]; + +extern int sin_numsurfedges; +extern int *sin_dsurfedges; //[MAX_MAP_SURFEDGES]; + +extern int sin_numbrushes; +extern sin_dbrush_t *sin_dbrushes; //[MAX_MAP_BRUSHES]; + +extern int sin_numbrushsides; +extern sin_dbrushside_t *sin_dbrushsides; //[MAX_MAP_BRUSHSIDES]; + +extern int sin_numareas; +extern sin_darea_t *sin_dareas; //[MAX_MAP_AREAS]; + +extern int sin_numareaportals; +extern sin_dareaportal_t *sin_dareaportals; //[MAX_MAP_AREAPORTALS]; + +extern int sin_numlightinfo; +extern sin_lightvalue_t *sin_lightinfo; //[MAX_MAP_LIGHTINFO]; + +extern byte sin_dpop[256]; + +extern char sin_dbrushsidetextured[SIN_MAX_MAP_BRUSHSIDES]; + +void Sin_AllocMaxBSP( void ); +void Sin_FreeMaxBSP( void ); + +void Sin_DecompressVis( byte *in, byte *decompressed ); +int Sin_CompressVis( byte *vis, byte *dest ); + +void Sin_LoadBSPFile( char *filename, int offset, int length ); +void Sin_LoadBSPFileTexinfo( char *filename ); // just for qdata +void Sin_WriteBSPFile( char *filename ); +void Sin_PrintBSPFileSizes( void ); +void Sin_ParseEntities( void ); +void Sin_UnparseEntities( void ); + diff --git a/src/bspc/l_cmd.c b/src/bspc/l_cmd.c new file mode 100644 index 0000000..6ca23f1 --- /dev/null +++ b/src/bspc/l_cmd.c @@ -0,0 +1,1181 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +// cmdlib.c + +#include "l_cmd.h" +#include "l_log.h" +#include "l_mem.h" +#include +#include + +#ifndef SIN +#define SIN +#endif //SIN + +#if defined( WIN32 ) || defined( _WIN32 ) +#include +#else +#include +#endif + +#ifdef NeXT +#include +#endif + +#define BASEDIRNAME "wolf" +#define PATHSEPERATOR '/' + +// set these before calling CheckParm +int myargc; +char **myargv; + +char com_token[1024]; +qboolean com_eof; + +qboolean archive; +char archivedir[1024]; + + +/* +=================== +ExpandWildcards + +Mimic unix command line expansion +=================== +*/ +#define MAX_EX_ARGC 1024 +int ex_argc; +char *ex_argv[MAX_EX_ARGC]; +#ifdef _WIN32 +#include "io.h" +void ExpandWildcards( int *argc, char ***argv ) { + struct _finddata_t fileinfo; + int handle; + int i; + char filename[1024]; + char filebase[1024]; + char *path; + + ex_argc = 0; + for ( i = 0 ; i < *argc ; i++ ) + { + path = ( *argv )[i]; + if ( path[0] == '-' + || ( !strstr( path, "*" ) && !strstr( path, "?" ) ) ) { + ex_argv[ex_argc++] = path; + continue; + } + + handle = _findfirst( path, &fileinfo ); + if ( handle == -1 ) { + return; + } + + ExtractFilePath( path, filebase ); + + do + { + sprintf( filename, "%s%s", filebase, fileinfo.name ); + ex_argv[ex_argc++] = copystring( filename ); + } while ( _findnext( handle, &fileinfo ) != -1 ); + + _findclose( handle ); + } + + *argc = ex_argc; + *argv = ex_argv; +} +#else +void ExpandWildcards( int *argc, char ***argv ) { +} +#endif + +#ifdef WINBSPC + +#include + +HWND program_hwnd; + +void SetProgramHandle( HWND hwnd ) { + program_hwnd = hwnd; +} //end of the function SetProgramHandle + +/* +================= +Error + +For abnormal program terminations in windowed apps +================= +*/ +void Error( char *error, ... ) { + va_list argptr; + char text[1024]; + char text2[1024]; + int err; + + err = GetLastError(); + + va_start( argptr, error ); + vsprintf( text, error, argptr ); + va_end( argptr ); + + sprintf( text2, "%s\nGetLastError() = %i", text, err ); + MessageBox( program_hwnd, text2, "Error", 0 /* MB_OK */ ); + + Log_Write( text ); + Log_Close(); + + exit( 1 ); +} //end of the function Error + +void Warning( char *szFormat, ... ) { + char szBuffer[256]; + va_list argptr; + + va_start( argptr, szFormat ); + vsprintf( szBuffer, szFormat, argptr ); + va_end( argptr ); + + MessageBox( program_hwnd, szBuffer, "Warning", MB_OK ); + + Log_Write( szBuffer ); +} //end of the function Warning + + +#else +/* +================= +Error + +For abnormal program terminations in console apps +================= +*/ +void Error( char *error, ... ) { + va_list argptr; + char text[1024]; + + va_start( argptr, error ); + vsprintf( text, error, argptr ); + va_end( argptr ); + printf( "ERROR: %s\n", text ); + + Log_Write( text ); + Log_Close(); + + exit( 1 ); +} //end of the function Error + +void Warning( char *warning, ... ) { + va_list argptr; + char text[1024]; + + va_start( argptr, warning ); + vsprintf( text, warning, argptr ); + va_end( argptr ); + printf( "WARNING: %s\n", text ); + + Log_Write( text ); +} //end of the function Warning + +#endif + +//only printf if in verbose mode +qboolean verbose = true; + +void qprintf( char *format, ... ) { + va_list argptr; +#ifdef WINBSPC + char buf[2048]; +#endif //WINBSPC + + if ( !verbose ) { + return; + } + + va_start( argptr,format ); +#ifdef WINBSPC + vsprintf( buf, format, argptr ); + WinBSPCPrint( buf ); +#else + vprintf( format, argptr ); +#endif //WINBSPC + va_end( argptr ); +} //end of the function qprintf + +void Com_Error( int level, char *error, ... ) { + va_list argptr; + char text[1024]; + + va_start( argptr, error ); + vsprintf( text, error, argptr ); + va_end( argptr ); + Error( text ); +} //end of the funcion Com_Error + +void Com_Printf( const char *fmt, ... ) { + va_list argptr; + char text[1024]; + + va_start( argptr, fmt ); + vsprintf( text, fmt, argptr ); + va_end( argptr ); + Log_Print( text ); +} //end of the funcion Com_Printf + +/* + +qdir will hold the path up to the quake directory, including the slash + + f:\quake \ + /raid/quake/ + +gamedir will hold qdir + the game directory (id1, id2, etc) + + */ + +char qdir[1024]; +char gamedir[1024]; + +void SetQdirFromPath( char *path ) { + char temp[1024]; + char *c; + int len; + + if ( !( path[0] == '/' || path[0] == '\\' || path[1] == ':' ) ) { // path is partial + Q_getwd( temp ); + strcat( temp, path ); + path = temp; + } + + // search for "quake2" in path + + len = strlen( BASEDIRNAME ); + for ( c = path + strlen( path ) - 1 ; c != path ; c-- ) + if ( !Q_strncasecmp( c, BASEDIRNAME, len ) ) { + strncpy( qdir, path, c + len + 1 - path ); + qprintf( "qdir: %s\n", qdir ); + c += len + 1; + while ( *c ) + { + if ( *c == '/' || *c == '\\' ) { + strncpy( gamedir, path, c + 1 - path ); + qprintf( "gamedir: %s\n", gamedir ); + return; + } + c++; + } + Error( "No gamedir in %s", path ); + return; + } + Error( "SetQdirFromPath: no '%s' in %s", BASEDIRNAME, path ); +} + +char *ExpandArg( char *path ) { + static char full[1024]; + + if ( path[0] != '/' && path[0] != '\\' && path[1] != ':' ) { + Q_getwd( full ); + strcat( full, path ); + } else { + strcpy( full, path ); + } + return full; +} + +char *ExpandPath( char *path ) { + static char full[1024]; + if ( !qdir ) { + Error( "ExpandPath called without qdir set" ); + } + if ( path[0] == '/' || path[0] == '\\' || path[1] == ':' ) { + return path; + } + sprintf( full, "%s%s", qdir, path ); + return full; +} + +char *ExpandPathAndArchive( char *path ) { + char *expanded; + char archivename[1024]; + + expanded = ExpandPath( path ); + + if ( archive ) { + sprintf( archivename, "%s/%s", archivedir, path ); + QCopyFile( expanded, archivename ); + } + return expanded; +} + + +char *copystring( char *s ) { + char *b; + b = GetMemory( strlen( s ) + 1 ); + strcpy( b, s ); + return b; +} + + + +/* +================ +I_FloatTime +================ +*/ +double I_FloatTime( void ) { + time_t t; + + time( &t ); + + return t; +#if 0 +// more precise, less portable + struct timeval tp; + struct timezone tzp; + static int secbase; + + gettimeofday( &tp, &tzp ); + + if ( !secbase ) { + secbase = tp.tv_sec; + return tp.tv_usec / 1000000.0; + } + + return ( tp.tv_sec - secbase ) + tp.tv_usec / 1000000.0; +#endif +} + +void Q_getwd( char *out ) { +#if defined( WIN32 ) || defined( _WIN32 ) + getcwd( out, 256 ); + strcat( out, "\\" ); +#else + getwd( out ); + strcat( out, "/" ); +#endif +} + + +void Q_mkdir( char *path ) { +#ifdef WIN32 + if ( _mkdir( path ) != -1 ) { + return; + } +#else + if ( mkdir( path, 0777 ) != -1 ) { + return; + } +#endif + if ( errno != EEXIST ) { + Error( "mkdir %s: %s",path, strerror( errno ) ); + } +} + +/* +============ +FileTime + +returns -1 if not present +============ +*/ +int FileTime( char *path ) { + struct stat buf; + + if ( stat( path,&buf ) == -1 ) { + return -1; + } + + return buf.st_mtime; +} + + + +/* +============== +COM_Parse + +Parse a token out of a string +============== +*/ +char *COM_Parse( char *data ) { + int c; + int len; + + len = 0; + com_token[0] = 0; + + if ( !data ) { + return NULL; + } + +// skip whitespace +skipwhite: + while ( ( c = *data ) <= ' ' ) + { + if ( c == 0 ) { + com_eof = true; + return NULL; // end of file; + } + data++; + } + +// skip // comments + if ( c == '/' && data[1] == '/' ) { + while ( *data && *data != '\n' ) + data++; + goto skipwhite; + } + + +// handle quoted strings specially + if ( c == '\"' ) { + data++; + do + { + c = *data++; + if ( c == '\"' ) { + com_token[len] = 0; + return data; + } + com_token[len] = c; + len++; + } while ( 1 ); + } + +// parse single characters + if ( c == '{' || c == '}' || c == ')' || c == '(' || c == '\'' || c == ':' ) { + com_token[len] = c; + len++; + com_token[len] = 0; + return data + 1; + } + +// parse a regular word + do + { + com_token[len] = c; + data++; + len++; + c = *data; + if ( c == '{' || c == '}' || c == ')' || c == '(' || c == '\'' || c == ':' ) { + break; + } + } while ( c > 32 ); + + com_token[len] = 0; + return data; +} + + +int Q_strncasecmp( char *s1, char *s2, int n ) { + int c1, c2; + + do + { + c1 = *s1++; + c2 = *s2++; + + if ( !n-- ) { + return 0; // strings are equal until end point + + } + if ( c1 != c2 ) { + if ( c1 >= 'a' && c1 <= 'z' ) { + c1 -= ( 'a' - 'A' ); + } + if ( c2 >= 'a' && c2 <= 'z' ) { + c2 -= ( 'a' - 'A' ); + } + if ( c1 != c2 ) { + return -1; // strings not equal + } + } + } while ( c1 ); + + return 0; // strings are equal +} + +int Q_strcasecmp( char *s1, char *s2 ) { + return Q_strncasecmp( s1, s2, 99999 ); +} + +int Q_stricmp( char *s1, char *s2 ) { + return Q_strncasecmp( s1, s2, 99999 ); +} + +void Q_strncpyz( char *dest, const char *src, int destsize ) { + strncpy( dest, src, destsize - 1 ); + dest[destsize - 1] = 0; +} + +char *strupr( char *start ) { + char *in; + in = start; + while ( *in ) + { + *in = toupper( *in ); + in++; + } + return start; +} + +char *strlower( char *start ) { + char *in; + in = start; + while ( *in ) + { + *in = tolower( *in ); + in++; + } + return start; +} + + +/* +============================================================================= + + MISC FUNCTIONS + +============================================================================= +*/ + + +/* +================= +CheckParm + +Checks for the given parameter in the program's command line arguments +Returns the argument number (1 to argc-1) or 0 if not present +================= +*/ +int CheckParm( char *check ) { + int i; + + for ( i = 1; i < myargc; i++ ) + { + if ( !Q_strcasecmp( check, myargv[i] ) ) { + return i; + } + } + + return 0; +} + + + +/* +================ +Q_filelength +================ +*/ +int Q_filelength( FILE *f ) { + int pos; + int end; + + pos = ftell( f ); + fseek( f, 0, SEEK_END ); + end = ftell( f ); + fseek( f, pos, SEEK_SET ); + + return end; +} + + +FILE *SafeOpenWrite( char *filename ) { + FILE *f; + + f = fopen( filename, "wb" ); + + if ( !f ) { + Error( "Error opening %s: %s",filename,strerror( errno ) ); + } + + return f; +} + +FILE *SafeOpenRead( char *filename ) { + FILE *f; + + f = fopen( filename, "rb" ); + + if ( !f ) { + Error( "Error opening %s: %s",filename,strerror( errno ) ); + } + + return f; +} + + +void SafeRead( FILE *f, void *buffer, int count ) { + if ( fread( buffer, 1, count, f ) != (size_t)count ) { + Error( "File read failure" ); + } +} + + +void SafeWrite( FILE *f, void *buffer, int count ) { + if ( fwrite( buffer, 1, count, f ) != (size_t)count ) { + Error( "File write failure" ); + } +} + + +/* +============== +FileExists +============== +*/ +qboolean FileExists( char *filename ) { + FILE *f; + + f = fopen( filename, "r" ); + if ( !f ) { + return false; + } + fclose( f ); + return true; +} + +/* +============== +LoadFile +============== +*/ +int LoadFile( char *filename, void **bufferptr, int offset, int length ) { + FILE *f; + void *buffer; + + f = SafeOpenRead( filename ); + fseek( f, offset, SEEK_SET ); + if ( !length ) { + length = Q_filelength( f ); + } + buffer = GetMemory( length + 1 ); + ( (char *)buffer )[length] = 0; + SafeRead( f, buffer, length ); + fclose( f ); + + *bufferptr = buffer; + return length; +} + + +/* +============== +TryLoadFile + +Allows failure +============== +*/ +int TryLoadFile( char *filename, void **bufferptr ) { + FILE *f; + int length; + void *buffer; + + *bufferptr = NULL; + + f = fopen( filename, "rb" ); + if ( !f ) { + return -1; + } + length = Q_filelength( f ); + buffer = GetMemory( length + 1 ); + ( (char *)buffer )[length] = 0; + SafeRead( f, buffer, length ); + fclose( f ); + + *bufferptr = buffer; + return length; +} + + +/* +============== +SaveFile +============== +*/ +void SaveFile( char *filename, void *buffer, int count ) { + FILE *f; + + f = SafeOpenWrite( filename ); + SafeWrite( f, buffer, count ); + fclose( f ); +} + + + +void DefaultExtension( char *path, char *extension ) { + char *src; +// +// if path doesnt have a .EXT, append extension +// (extension should include the .) +// + src = path + strlen( path ) - 1; + + while ( *src != PATHSEPERATOR && src != path ) + { + if ( *src == '.' ) { + return; // it has an extension + } + src--; + } + + strcat( path, extension ); +} + + +void DefaultPath( char *path, char *basepath ) { + char temp[128]; + + if ( path[0] == PATHSEPERATOR ) { + return; // absolute path location + } + strcpy( temp,path ); + strcpy( path,basepath ); + strcat( path,temp ); +} + + +void StripFilename( char *path ) { + int length; + + length = strlen( path ) - 1; + while ( length > 0 && path[length] != PATHSEPERATOR ) + length--; + path[length] = 0; +} + +void StripExtension( char *path ) { + int length; + + length = strlen( path ) - 1; + while ( length > 0 && path[length] != '.' ) + { + length--; + if ( path[length] == '/' ) { + return; // no extension + } + } + if ( length ) { + path[length] = 0; + } +} + + +/* +==================== +Extract file parts +==================== +*/ +// FIXME: should include the slash, otherwise +// backing to an empty path will be wrong when appending a slash +void ExtractFilePath( char *path, char *dest ) { + char *src; + + src = path + strlen( path ) - 1; + +// +// back up until a \ or the start +// + while ( src != path && *( src - 1 ) != '\\' && *( src - 1 ) != '/' ) + src--; + + memcpy( dest, path, src - path ); + dest[src - path] = 0; +} + +void ExtractFileBase( char *path, char *dest ) { + char *src; + + src = path + strlen( path ) - 1; + +// +// back up until a \ or the start +// + while ( src != path && *( src - 1 ) != '\\' && *( src - 1 ) != '/' ) + src--; + + while ( *src && *src != '.' ) + { + *dest++ = *src++; + } + *dest = 0; +} + +void ExtractFileExtension( char *path, char *dest ) { + char *src; + + src = path + strlen( path ) - 1; + +// +// back up until a . or the start +// + while ( src != path && *( src - 1 ) != '.' ) + src--; + if ( src == path ) { + *dest = 0; // no extension + return; + } + + strcpy( dest,src ); +} + + +/* +============== +ParseNum / ParseHex +============== +*/ +int ParseHex( char *hex ) { + char *str; + int num; + + num = 0; + str = hex; + + while ( *str ) + { + num <<= 4; + if ( *str >= '0' && *str <= '9' ) { + num += *str - '0'; + } else if ( *str >= 'a' && *str <= 'f' ) { + num += 10 + *str - 'a'; + } else if ( *str >= 'A' && *str <= 'F' ) { + num += 10 + *str - 'A'; + } else { + Error( "Bad hex number: %s",hex ); + } + str++; + } + + return num; +} + + +int ParseNum( char *str ) { + if ( str[0] == '$' ) { + return ParseHex( str + 1 ); + } + if ( str[0] == '0' && str[1] == 'x' ) { + return ParseHex( str + 2 ); + } + return atol( str ); +} + + + +/* +============================================================================ + + BYTE ORDER FUNCTIONS + +============================================================================ +*/ + +#ifdef _SGI_SOURCE +#define __BIG_ENDIAN__ +#endif + +#ifdef __BIG_ENDIAN__ + +short LittleShort( short l ) { + byte b1,b2; + + b1 = l & 255; + b2 = ( l >> 8 ) & 255; + + return ( b1 << 8 ) + b2; +} + +short BigShort( short l ) { + return l; +} + + +int LittleLong( int l ) { + byte b1,b2,b3,b4; + + b1 = l & 255; + b2 = ( l >> 8 ) & 255; + b3 = ( l >> 16 ) & 255; + b4 = ( l >> 24 ) & 255; + + return ( (int)b1 << 24 ) + ( (int)b2 << 16 ) + ( (int)b3 << 8 ) + b4; +} + +int BigLong( int l ) { + return l; +} + + +float LittleFloat( float l ) { + union {byte b[4]; float f;} in, out; + + in.f = l; + out.b[0] = in.b[3]; + out.b[1] = in.b[2]; + out.b[2] = in.b[1]; + out.b[3] = in.b[0]; + + return out.f; +} + +float BigFloat( float l ) { + return l; +} + +#ifdef SIN +unsigned short LittleUnsignedShort( unsigned short l ) { + byte b1,b2; + + b1 = l & 255; + b2 = ( l >> 8 ) & 255; + + return ( b1 << 8 ) + b2; +} + +unsigned short BigUnsignedShort( unsigned short l ) { + return l; +} + +unsigned LittleUnsigned( unsigned l ) { + byte b1,b2,b3,b4; + + b1 = l & 255; + b2 = ( l >> 8 ) & 255; + b3 = ( l >> 16 ) & 255; + b4 = ( l >> 24 ) & 255; + + return ( (unsigned)b1 << 24 ) + ( (unsigned)b2 << 16 ) + ( (unsigned)b3 << 8 ) + b4; +} + +unsigned BigUnsigned( unsigned l ) { + return l; +} +#endif + + +#else + + +short BigShort( short l ) { + byte b1,b2; + + b1 = l & 255; + b2 = ( l >> 8 ) & 255; + + return ( b1 << 8 ) + b2; +} + +short LittleShort( short l ) { + return l; +} + + +int BigLong( int l ) { + byte b1,b2,b3,b4; + + b1 = l & 255; + b2 = ( l >> 8 ) & 255; + b3 = ( l >> 16 ) & 255; + b4 = ( l >> 24 ) & 255; + + return ( (int)b1 << 24 ) + ( (int)b2 << 16 ) + ( (int)b3 << 8 ) + b4; +} + +int LittleLong( int l ) { + return l; +} + +float BigFloat( float l ) { + union {byte b[4]; float f;} in, out; + + in.f = l; + out.b[0] = in.b[3]; + out.b[1] = in.b[2]; + out.b[2] = in.b[1]; + out.b[3] = in.b[0]; + + return out.f; +} + +float LittleFloat( float l ) { + return l; +} + +#ifdef SIN +unsigned short BigUnsignedShort( unsigned short l ) { + byte b1,b2; + + b1 = l & 255; + b2 = ( l >> 8 ) & 255; + + return ( b1 << 8 ) + b2; +} + +unsigned short LittleUnsignedShort( unsigned short l ) { + return l; +} + + +unsigned BigUnsigned( unsigned l ) { + byte b1,b2,b3,b4; + + b1 = l & 255; + b2 = ( l >> 8 ) & 255; + b3 = ( l >> 16 ) & 255; + b4 = ( l >> 24 ) & 255; + + return ( (unsigned)b1 << 24 ) + ( (unsigned)b2 << 16 ) + ( (unsigned)b3 << 8 ) + b4; +} + +unsigned LittleUnsigned( unsigned l ) { + return l; +} +#endif + + +#endif + + +//======================================================= + + +// FIXME: byte swap? + +// this is a 16 bit, non-reflected CRC using the polynomial 0x1021 +// and the initial and final xor values shown below... in other words, the +// CCITT standard CRC used by XMODEM + +#define CRC_INIT_VALUE 0xffff +#define CRC_XOR_VALUE 0x0000 + +static unsigned short crctable[256] = +{ + 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, + 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, + 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, + 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, + 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, + 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, + 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, + 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, + 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, + 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, + 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, + 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, + 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, + 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, + 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, + 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, + 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, + 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, + 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, + 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, + 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, + 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, + 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, + 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, + 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, + 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, + 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, + 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, + 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, + 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, + 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, + 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 +}; + +void CRC_Init( unsigned short *crcvalue ) { + *crcvalue = CRC_INIT_VALUE; +} + +void CRC_ProcessByte( unsigned short *crcvalue, byte data ) { + *crcvalue = ( *crcvalue << 8 ) ^ crctable[( *crcvalue >> 8 ) ^ data]; +} + +unsigned short CRC_Value( unsigned short crcvalue ) { + return crcvalue ^ CRC_XOR_VALUE; +} +//============================================================================= + +/* +============ +CreatePath +============ +*/ +void CreatePath( char *path ) { + char *ofs, c; + + if ( path[1] == ':' ) { + path += 2; + } + + for ( ofs = path + 1 ; *ofs ; ofs++ ) + { + c = *ofs; + if ( c == '/' || c == '\\' ) { // create the directory + *ofs = 0; + Q_mkdir( path ); + *ofs = c; + } + } +} + + +/* +============ +QCopyFile + + Used to archive source files +============ +*/ +void QCopyFile( char *from, char *to ) { + void *buffer; + int length; + + length = LoadFile( from, &buffer, 0, 0 ); + CreatePath( to ); + SaveFile( to, buffer, length ); + FreeMemory( buffer ); +} + +void FS_FreeFile( void *buf ) { + FreeMemory( buf ); +} //end of the function FS_FreeFile + +int FS_ReadFileAndCache( const char *qpath, void **buffer ) { + return LoadFile( (char *) qpath, buffer, 0, 0 ); +} //end of the function FS_ReadFileAndCache + +int FS_FOpenFileRead( const char *filename, FILE **file, qboolean uniqueFILE ) { + *file = fopen( filename, "rb" ); + return ( *file != NULL ); +} //end of the function FS_FOpenFileRead diff --git a/src/bspc/l_cmd.h b/src/bspc/l_cmd.h new file mode 100644 index 0000000..6d0e1fe --- /dev/null +++ b/src/bspc/l_cmd.h @@ -0,0 +1,164 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// cmdlib.h + +#ifndef SIN +#define SIN +#endif //SIN + +#ifndef __CMDLIB__ +#define __CMDLIB__ + +#ifdef _WIN32 +#pragma warning(disable : 4244) // MIPS +#pragma warning(disable : 4136) // X86 +#pragma warning(disable : 4051) // ALPHA + +#pragma warning(disable : 4018) // signed/unsigned mismatch +#pragma warning(disable : 4305) // truncate from double to float +#endif + +#include +#include +#include +#include +#include +#include +#include + +#ifndef __BYTEBOOL__ +#define __BYTEBOOL__ +typedef enum {false, true} qboolean; +typedef unsigned char byte; +#endif + +// the dec offsetof macro doesnt work very well... +#define myoffsetof( type,identifier ) ( (size_t)&( (type *)0 )->identifier ) + + +// set these before calling CheckParm +extern int myargc; +extern char **myargv; + +char *strupr( char *in ); +char *strlower( char *in ); +int Q_strncasecmp( char *s1, char *s2, int n ); +int Q_strcasecmp( char *s1, char *s2 ); +void Q_getwd( char *out ); + +int Q_filelength( FILE *f ); +int FileTime( char *path ); + +void Q_mkdir( char *path ); + +extern char qdir[1024]; +extern char gamedir[1024]; +void SetQdirFromPath( char *path ); +char *ExpandArg( char *path ); // from cmd line +char *ExpandPath( char *path ); // from scripts +char *ExpandPathAndArchive( char *path ); + + +double I_FloatTime( void ); + +void Error( char *error, ... ); +void Warning( char *warning, ... ); + +int CheckParm( char *check ); + +FILE *SafeOpenWrite( char *filename ); +FILE *SafeOpenRead( char *filename ); +void SafeRead( FILE *f, void *buffer, int count ); +void SafeWrite( FILE *f, void *buffer, int count ); + +int LoadFile( char *filename, void **bufferptr, int offset, int length ); +int TryLoadFile( char *filename, void **bufferptr ); +void SaveFile( char *filename, void *buffer, int count ); +qboolean FileExists( char *filename ); + +void DefaultExtension( char *path, char *extension ); +void DefaultPath( char *path, char *basepath ); +void StripFilename( char *path ); +void StripExtension( char *path ); + +void ExtractFilePath( char *path, char *dest ); +void ExtractFileBase( char *path, char *dest ); +void ExtractFileExtension( char *path, char *dest ); + +int ParseNum( char *str ); + +short BigShort( short l ); +short LittleShort( short l ); +int BigLong( int l ); +int LittleLong( int l ); +float BigFloat( float l ); +float LittleFloat( float l ); + +#ifdef SIN +unsigned short BigUnsignedShort( unsigned short l ); +unsigned short LittleUnsignedShort( unsigned short l ); +unsigned BigUnsigned( unsigned l ); +unsigned LittleUnsigned( unsigned l ); +#endif + + +char *COM_Parse( char *data ); + +extern char com_token[1024]; +extern qboolean com_eof; + +char *copystring( char *s ); + + +void CRC_Init( unsigned short *crcvalue ); +void CRC_ProcessByte( unsigned short *crcvalue, byte data ); +unsigned short CRC_Value( unsigned short crcvalue ); + +void CreatePath( char *path ); +void QCopyFile( char *from, char *to ); + +extern qboolean archive; +extern char archivedir[1024]; + + +extern qboolean verbose; +void qprintf( char *format, ... ); + +void ExpandWildcards( int *argc, char ***argv ); + + +// for compression routines +typedef struct +{ + byte *data; + int count; +} cblock_t; + +#endif + diff --git a/src/bspc/l_log.c b/src/bspc/l_log.c new file mode 100644 index 0000000..49ac74b --- /dev/null +++ b/src/bspc/l_log.c @@ -0,0 +1,220 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: l_log.c +// Function: log file stuff +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-31 +// Tab Size: 3 +//=========================================================================== + +#include +#include +#include + +#include "qbsp.h" + +#define MAX_LOGFILENAMESIZE 1024 + +typedef struct logfile_s +{ + char filename[MAX_LOGFILENAMESIZE]; + FILE *fp; + int numwrites; +} logfile_t; + +logfile_t logfile; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Log_Open( char *filename ) { + if ( !filename || !strlen( filename ) ) { + printf( "openlog \n" ); + return; + } //end if + if ( logfile.fp ) { + printf( "log file %s is already opened\n", logfile.filename ); + return; + } //end if + logfile.fp = fopen( filename, "wb" ); + if ( !logfile.fp ) { + printf( "can't open the log file %s\n", filename ); + return; + } //end if + strncpy( logfile.filename, filename, MAX_LOGFILENAMESIZE ); + printf( "Opened log %s\n", logfile.filename ); +} //end of the function Log_Create +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Log_Close( void ) { + if ( !logfile.fp ) { + printf( "no log file to close\n" ); + return; + } //end if + if ( fclose( logfile.fp ) ) { + printf( "can't close log file %s\n", logfile.filename ); + return; + } //end if + logfile.fp = NULL; + printf( "Closed log %s\n", logfile.filename ); +} //end of the function Log_Close +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Log_Shutdown( void ) { + if ( logfile.fp ) { + Log_Close(); + } +} //end of the function Log_Shutdown +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Log_UnifyEndOfLine( char *buf ) { + int i; + + for ( i = 0; buf[i]; i++ ) + { + if ( buf[i] == '\n' ) { + if ( i <= 0 || buf[i - 1] != '\r' ) { + memmove( &buf[i + 1], &buf[i], strlen( &buf[i] ) + 1 ); + buf[i] = '\r'; + i++; + } //end if + } //end if + } //end for +} //end of the function Log_UnifyEndOfLine +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Log_Print( char *fmt, ... ) { + va_list ap; + char buf[2048]; + + va_start( ap, fmt ); + vsprintf( buf, fmt, ap ); + va_end( ap ); + + if ( verbose ) { +#ifdef WINBSPC + WinBSPCPrint( buf ); +#else + printf( "%s", buf ); +#endif //WINBSPS + } //end if + + if ( logfile.fp ) { + Log_UnifyEndOfLine( buf ); + fprintf( logfile.fp, "%s", buf ); + fflush( logfile.fp ); + } //end if +} //end of the function Log_Print +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Log_Write( char *fmt, ... ) { + va_list ap; + char buf[2048]; + + if ( !logfile.fp ) { + return; + } + va_start( ap, fmt ); + vsprintf( buf, fmt, ap ); + va_end( ap ); + Log_UnifyEndOfLine( buf ); + fprintf( logfile.fp, "%s", buf ); + fflush( logfile.fp ); +} //end of the function Log_Write +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Log_WriteTimeStamped( char *fmt, ... ) { + va_list ap; + + if ( !logfile.fp ) { + return; + } +/* fprintf(logfile.fp, "%d %02d:%02d:%02d:%02d ", + logfile.numwrites, + (int) (botlibglobals.time / 60 / 60), + (int) (botlibglobals.time / 60), + (int) (botlibglobals.time), + (int) ((int) (botlibglobals.time * 100)) - + ((int) botlibglobals.time) * 100);*/ + va_start( ap, fmt ); + vfprintf( logfile.fp, fmt, ap ); + va_end( ap ); + logfile.numwrites++; + fflush( logfile.fp ); +} //end of the function Log_Write +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +FILE *Log_FileStruct( void ) { + return logfile.fp; +} //end of the function Log_FileStruct +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Log_Flush( void ) { + if ( logfile.fp ) { + fflush( logfile.fp ); + } +} //end of the function Log_Flush + diff --git a/src/bspc/l_log.h b/src/bspc/l_log.h new file mode 100644 index 0000000..978b19d --- /dev/null +++ b/src/bspc/l_log.h @@ -0,0 +1,57 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: l_log.h +// Function: log file stuff +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-31 +// Tab Size: 3 +//=========================================================================== + +//open a log file +void Log_Open( char *filename ); +//close the current log file +void Log_Close( void ); +//close log file if present +void Log_Shutdown( void ); +//print on stdout and write to the current opened log file +void Log_Print( char *fmt, ... ); +//write to the current opened log file +void Log_Write( char *fmt, ... ); +//write to the current opened log file with a time stamp +void Log_WriteTimeStamped( char *fmt, ... ); +//returns the log file structure +FILE *Log_FileStruct( void ); +//flush log file +void Log_Flush( void ); + +#ifdef WINBSPC +void WinBSPCPrint( char *str ); +#endif //WINBSPC diff --git a/src/bspc/l_math.c b/src/bspc/l_math.c new file mode 100644 index 0000000..52f15fc --- /dev/null +++ b/src/bspc/l_math.c @@ -0,0 +1,279 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// mathlib.c -- math primitives + +#include "l_cmd.h" +#include "l_math.h" + +vec3_t vec3_origin = {0,0,0}; + +void AngleVectors( const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up ) { + float angle; + static float sr, sp, sy, cr, cp, cy; + // static to help MS compiler fp bugs + + 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 ); + + if ( forward ) { + forward[0] = cp * cy; + forward[1] = cp * sy; + forward[2] = -sp; + } + if ( right ) { + right[0] = ( -1 * sr * sp * cy + - 1 * cr * -sy ); + right[1] = ( -1 * sr * sp * sy + - 1 * cr * cy ); + right[2] = -1 * sr * cp; + } + if ( up ) { + up[0] = ( cr * sp * cy + - sr * -sy ); + up[1] = ( cr * sp * sy + - sr * cy ); + up[2] = cr * cp; + } +} + +/* +================= +RadiusFromBounds +================= +*/ +float RadiusFromBounds( const vec3_t mins, const vec3_t maxs ) { + int i; + vec3_t corner; + float a, b; + + for ( i = 0 ; i < 3 ; i++ ) { + a = fabs( mins[i] ); + b = fabs( maxs[i] ); + corner[i] = a > b ? a : b; + } + + return VectorLength( corner ); +} + +/* +================ +R_ConcatRotations +================ +*/ +void R_ConcatRotations( float in1[3][3], float in2[3][3], float out[3][3] ) { + 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[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[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]; +} + +void AxisClear( vec3_t axis[3] ) { + axis[0][0] = 1; + axis[0][1] = 0; + axis[0][2] = 0; + axis[1][0] = 0; + axis[1][1] = 1; + axis[1][2] = 0; + axis[2][0] = 0; + axis[2][1] = 0; + axis[2][2] = 1; +} + +double VectorLength( vec3_t v ) { + int i; + double length; + + length = 0; + for ( i = 0 ; i < 3 ; i++ ) + length += v[i] * v[i]; + length = sqrt( length ); // FIXME + + return length; +} + +float VectorLengthSquared( vec3_t v ) { + return DotProduct( v, v ); +} + +qboolean VectorCompare( vec3_t v1, vec3_t v2 ) { + int i; + + for ( i = 0 ; i < 3 ; i++ ) + if ( fabs( v1[i] - v2[i] ) > EQUAL_EPSILON ) { + return false; + } + + return true; +} + +vec_t Q_rint( vec_t in ) { + return floor( in + 0.5 ); +} + +void CrossProduct( const vec3_t v1, const vec3_t v2, vec3_t 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]; +} + +void _VectorMA( vec3_t va, double scale, vec3_t vb, vec3_t vc ) { + vc[0] = va[0] + scale * vb[0]; + vc[1] = va[1] + scale * vb[1]; + vc[2] = va[2] + scale * vb[2]; +} + +vec_t _DotProduct( vec3_t v1, vec3_t v2 ) { + return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2]; +} + +void _VectorSubtract( vec3_t va, vec3_t vb, vec3_t out ) { + out[0] = va[0] - vb[0]; + out[1] = va[1] - vb[1]; + out[2] = va[2] - vb[2]; +} + +void _VectorAdd( vec3_t va, vec3_t vb, vec3_t out ) { + out[0] = va[0] + vb[0]; + out[1] = va[1] + vb[1]; + out[2] = va[2] + vb[2]; +} + +void _VectorCopy( vec3_t in, vec3_t out ) { + out[0] = in[0]; + out[1] = in[1]; + out[2] = in[2]; +} + +void _VectorScale( vec3_t v, vec_t scale, vec3_t out ) { + out[0] = v[0] * scale; + out[1] = v[1] * scale; + out[2] = v[2] * scale; +} + +vec_t VectorNormalize( vec3_t inout ) { + vec_t length, ilength; + + length = sqrt( inout[0] * inout[0] + inout[1] * inout[1] + inout[2] * inout[2] ); + if ( length == 0 ) { + VectorClear( inout ); + return 0; + } + + ilength = 1.0 / length; + inout[0] = inout[0] * ilength; + inout[1] = inout[1] * ilength; + inout[2] = inout[2] * ilength; + + return length; +} + +vec_t VectorNormalize2( const vec3_t in, vec3_t out ) { + vec_t length, ilength; + + length = sqrt( in[0] * in[0] + in[1] * in[1] + in[2] * in[2] ); + if ( length == 0 ) { + VectorClear( out ); + return 0; + } + + ilength = 1.0 / length; + out[0] = in[0] * ilength; + out[1] = in[1] * ilength; + out[2] = in[2] * ilength; + + return length; +} + +vec_t ColorNormalize( vec3_t in, vec3_t out ) { + float max, scale; + + max = in[0]; + if ( in[1] > max ) { + max = in[1]; + } + if ( in[2] > max ) { + max = in[2]; + } + + if ( max == 0 ) { + return 0; + } + + scale = 1.0 / max; + + VectorScale( in, scale, out ); + + return max; +} + + + +void VectorInverse( vec3_t v ) { + v[0] = -v[0]; + v[1] = -v[1]; + v[2] = -v[2]; +} + +void ClearBounds( vec3_t mins, vec3_t maxs ) { + mins[0] = mins[1] = mins[2] = 99999; + maxs[0] = maxs[1] = maxs[2] = -99999; +} + +void AddPointToBounds( const vec3_t v, vec3_t mins, vec3_t maxs ) { + int i; + vec_t val; + + for ( i = 0 ; i < 3 ; i++ ) + { + val = v[i]; + if ( val < mins[i] ) { + mins[i] = val; + } + if ( val > maxs[i] ) { + maxs[i] = val; + } + } +} diff --git a/src/bspc/l_math.h b/src/bspc/l_math.h new file mode 100644 index 0000000..872c4c5 --- /dev/null +++ b/src/bspc/l_math.h @@ -0,0 +1,100 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +#ifndef __MATHLIB__ +#define __MATHLIB__ + +// mathlib.h + +#include + +#ifdef DOUBLEVEC_T +typedef double vec_t; +#else +typedef float vec_t; +#endif +typedef vec_t vec3_t[3]; +typedef vec_t vec4_t[4]; + +#define SIDE_FRONT 0 +#define SIDE_ON 2 +#define SIDE_BACK 1 +#define SIDE_CROSS -2 + +#define PITCH 0 +#define YAW 1 +#define ROLL 2 + +#define Q_PI 3.14159265358979323846 + +#define DEG2RAD( a ) ( a * M_PI ) / 180.0F + +#ifndef M_PI +#define M_PI 3.14159265358979323846 // matches value in gcc v2 math.h +#endif + +extern vec3_t vec3_origin; + +#define EQUAL_EPSILON 0.001 + +qboolean VectorCompare( vec3_t v1, vec3_t v2 ); + +#define DotProduct( x,y ) ( x[0] * y[0] + x[1] * y[1] + x[2] * y[2] ) +#define VectorSubtract( a,b,c ) {c[0] = a[0] - b[0]; c[1] = a[1] - b[1]; c[2] = a[2] - b[2];} +#define VectorAdd( a,b,c ) {c[0] = a[0] + b[0]; c[1] = a[1] + b[1]; c[2] = a[2] + b[2];} +#define VectorCopy( a,b ) {b[0] = a[0]; b[1] = a[1]; b[2] = a[2];} +#define Vector4Copy( a,b ) {b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; b[3] = a[3];} +#define VectorScale( v, s, o ) ( ( o )[0] = ( v )[0] * ( s ),( o )[1] = ( v )[1] * ( s ),( o )[2] = ( v )[2] * ( s ) ) +#define VectorClear( x ) {x[0] = x[1] = x[2] = 0;} +#define VectorNegate( x, y ) {y[0] = -x[0]; y[1] = -x[1]; y[2] = -x[2];} +#define VectorMA( v, s, b, o ) ( ( o )[0] = ( v )[0] + ( b )[0] * ( s ),( o )[1] = ( v )[1] + ( b )[1] * ( s ),( o )[2] = ( v )[2] + ( b )[2] * ( s ) ) + +vec_t Q_rint( vec_t in ); +vec_t _DotProduct( vec3_t v1, vec3_t v2 ); +void _VectorSubtract( vec3_t va, vec3_t vb, vec3_t out ); +void _VectorAdd( vec3_t va, vec3_t vb, vec3_t out ); +void _VectorCopy( vec3_t in, vec3_t out ); +void _VectorScale( vec3_t v, vec_t scale, vec3_t out ); +void _VectorMA( vec3_t va, double scale, vec3_t vb, vec3_t vc ); + +double VectorLength( vec3_t v ); +void CrossProduct( const vec3_t v1, const vec3_t v2, vec3_t cross ); +vec_t VectorNormalize( vec3_t inout ); +vec_t ColorNormalize( vec3_t in, vec3_t out ); +vec_t VectorNormalize2( const vec3_t v, vec3_t out ); +void VectorInverse( vec3_t v ); + +void ClearBounds( vec3_t mins, vec3_t maxs ); +void AddPointToBounds( const vec3_t v, vec3_t mins, vec3_t maxs ); + +void AngleVectors( const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up ); +void R_ConcatRotations( float in1[3][3], float in2[3][3], float out[3][3] ); +void RotatePoint( vec3_t point, float matrix[3][3] ); +void CreateRotationMatrix( vec3_t angles, float matrix[3][3] ); + +#endif diff --git a/src/bspc/l_mem.c b/src/bspc/l_mem.c new file mode 100644 index 0000000..c81375a --- /dev/null +++ b/src/bspc/l_mem.c @@ -0,0 +1,468 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: l_mem.c +// Function: +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1999-06-02 +// Tab Size: 3 +//=========================================================================== + +#include "qbsp.h" +#include "l_log.h" + +int allocedmemory; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void PrintMemorySize( unsigned long size ) { + unsigned long number1, number2, number3; + number1 = size >> 20; + number2 = ( size & 0xFFFFF ) >> 10; + number3 = ( size & 0x3FF ); + if ( number1 ) { + Log_Print( "%ld MB", number1 ); + } + if ( number1 && number2 ) { + Log_Print( " and " ); + } + if ( number2 ) { + Log_Print( "%ld KB", number2 ); + } + if ( number2 && number3 ) { + Log_Print( " and " ); + } + if ( number3 ) { + Log_Print( "%ld bytes", number3 ); + } +} //end of the function PrintFileSize + +#ifndef MEMDEBUG +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int MemorySize( void *ptr ) { +#if defined( WIN32 ) || defined( _WIN32 ) + #ifdef __WATCOMC__ + //Intel 32 bits memory addressing, 16 bytes aligned + return ( _msize( ptr ) + 15 ) >> 4 << 4; + #else + return _msize( ptr ); + #endif +#else + return 0; +#endif +} //end of the function MemorySize +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void *GetClearedMemory( int size ) { + void *ptr; + + ptr = (void *) malloc( size ); + if ( !ptr ) { + Error( "out of memory" ); + } + memset( ptr, 0, size ); + allocedmemory += MemorySize( ptr ); + return ptr; +} //end of the function GetClearedMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void *GetMemory( unsigned long size ) { + void *ptr; + ptr = malloc( size ); + if ( !ptr ) { + Error( "out of memory" ); + } + allocedmemory += MemorySize( ptr ); + return ptr; +} //end of the function GetMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int fmemsize; +void FreeMemory( void *ptr ) { + // RF, modified this for better memory trash testing + fmemsize = MemorySize( ptr ); + allocedmemory -= fmemsize; + + // RF, somehow this crashes windows if size is less than or equal 8 + if ( fmemsize <= 8 ) { + return; + } + + // RF, set this memory to something that will cause problems if accessed again + memset( ptr, 0xAA, fmemsize ); + + free( ptr ); +} //end of the function FreeMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int TotalAllocatedMemory( void ) { + return allocedmemory; +} //end of the function TotalAllocatedMemory + +#else + +#define MEM_ID 0x12345678l + +int totalmemorysize; +int numblocks; + +typedef struct memoryblock_s +{ + unsigned long int id; + void *ptr; + int size; +#ifdef MEMDEBUG + char *label; + char *file; + int line; +#endif //MEMDEBUG + struct memoryblock_s *prev, *next; +} memoryblock_t; + +memoryblock_t *memory; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void LinkMemoryBlock( memoryblock_t *block ) { + block->prev = NULL; + block->next = memory; + if ( memory ) { + memory->prev = block; + } + memory = block; +} //end of the function LinkMemoryBlock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void UnlinkMemoryBlock( memoryblock_t *block ) { + if ( block->prev ) { + block->prev->next = block->next; + } else { memory = block->next;} + if ( block->next ) { + block->next->prev = block->prev; + } +} //end of the function UnlinkMemoryBlock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef MEMDEBUG +void *GetMemoryDebug( unsigned long size, char *label, char *file, int line ) +#else +void *GetMemory( unsigned long size ) +#endif //MEMDEBUG +{ + void *ptr; + memoryblock_t *block; + + ptr = malloc( size + sizeof( memoryblock_t ) ); + block = (memoryblock_t *) ptr; + block->id = MEM_ID; + block->ptr = (char *) ptr + sizeof( memoryblock_t ); + block->size = size + sizeof( memoryblock_t ); +#ifdef MEMDEBUG + block->label = label; + block->file = file; + block->line = line; +#endif //MEMDEBUG + LinkMemoryBlock( block ); + totalmemorysize += block->size; + numblocks++; + return block->ptr; +} //end of the function GetMemoryDebug +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef MEMDEBUG +void *GetClearedMemoryDebug( unsigned long size, char *label, char *file, int line ) +#else +void *GetClearedMemory( unsigned long size ) +#endif //MEMDEBUG +{ + void *ptr; +#ifdef MEMDEBUG + ptr = GetMemoryDebug( size, label, file, line ); +#else + ptr = GetMemory( size ); +#endif //MEMDEBUG + memset( ptr, 0, size ); + return ptr; +} //end of the function GetClearedMemoryLabelled +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void *GetClearedHunkMemory( unsigned long size ) { + return GetClearedMemory( size ); +} //end of the function GetClearedHunkMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void *GetHunkMemory( unsigned long size ) { + return GetMemory( size ); +} //end of the function GetHunkMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +memoryblock_t *BlockFromPointer( void *ptr, char *str ) { + memoryblock_t *block; + + if ( !ptr ) { +#ifdef MEMDEBUG + //char *crash = (char *) NULL; + //crash[0] = 1; + Error( "%s: NULL pointer\n", str ); +#endif MEMDEBUG + return NULL; + } //end if + block = ( memoryblock_t * )( (char *) ptr - sizeof( memoryblock_t ) ); + if ( block->id != MEM_ID ) { + Error( "%s: invalid memory block\n", str ); + } //end if + if ( block->ptr != ptr ) { + + Error( "%s: memory block pointer invalid\n", str ); + } //end if + return block; +} //end of the function BlockFromPointer +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FreeMemory( void *ptr ) { + memoryblock_t *block; + + block = BlockFromPointer( ptr, "FreeMemory" ); + if ( !block ) { + return; + } + UnlinkMemoryBlock( block ); + totalmemorysize -= block->size; + numblocks--; + // + free( block ); +} //end of the function FreeMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int MemoryByteSize( void *ptr ) { + memoryblock_t *block; + + block = BlockFromPointer( ptr, "MemoryByteSize" ); + if ( !block ) { + return 0; + } + return block->size; +} //end of the function MemoryByteSize +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int MemorySize( void *ptr ) { + return MemoryByteSize( ptr ); +} //end of the function MemorySize +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void PrintUsedMemorySize( void ) { + printf( "total botlib memory: %d KB\n", totalmemorysize >> 10 ); + printf( "total memory blocks: %d\n", numblocks ); +} //end of the function PrintUsedMemorySize +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void PrintMemoryLabels( void ) { + memoryblock_t *block; + int i; + + PrintUsedMemorySize(); + i = 0; + for ( block = memory; block; block = block->next ) + { +#ifdef MEMDEBUG + Log_Write( "%6d, %p, %8d: %24s line %6d: %s", i, block->ptr, block->size, block->file, block->line, block->label ); +#endif //MEMDEBUG + i++; + } //end for +} //end of the function PrintMemoryLabels +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void DumpMemory( void ) { + memoryblock_t *block; + + for ( block = memory; block; block = memory ) + { + FreeMemory( block->ptr ); + } //end for + totalmemorysize = 0; +} //end of the function DumpMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int TotalAllocatedMemory( void ) { + return totalmemorysize; +} //end of the function TotalAllocatedMemory +#endif + +//=========================================================================== +// Q3 Hunk and Z_ memory management +//=========================================================================== + +typedef struct memhunk_s +{ + void *ptr; + struct memhunk_s *next; +} memhunk_t; + +memhunk_t *memhunk_high; +memhunk_t *memhunk_low; +int memhunk_high_size = 16 * 1024 * 1024; +int memhunk_low_size = 0; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Hunk_ClearHigh( void ) { + memhunk_t *h, *nexth; + + for ( h = memhunk_high; h; h = nexth ) + { + nexth = h->next; + FreeMemory( h ); + } //end for + memhunk_high = NULL; + memhunk_high_size = 16 * 1024 * 1024; +} //end of the function Hunk_ClearHigh +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void *Hunk_Alloc( int size ) { + memhunk_t *h; + + if ( !size ) { + return (void *) memhunk_high_size; + } + // + h = GetClearedMemory( size + sizeof( memhunk_t ) ); + h->ptr = (char *) h + sizeof( memhunk_t ); + h->next = memhunk_high; + memhunk_high = h; + memhunk_high_size -= size; + return h->ptr; +} //end of the function Hunk_Alloc +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void *Z_Malloc( int size ) { + return GetClearedMemory( size ); +} //end of the function Z_Malloc +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Z_Free( void *ptr ) { + FreeMemory( ptr ); +} //end of the function Z_Free diff --git a/src/bspc/l_mem.h b/src/bspc/l_mem.h new file mode 100644 index 0000000..5b29f81 --- /dev/null +++ b/src/bspc/l_mem.h @@ -0,0 +1,58 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + + +//============================================================================= + +// memory.h +//#define MEMDEBUG +#undef MEMDEBUG + +#ifndef MEMDEBUG + +void *GetClearedMemory( int size ); +void *GetMemory( unsigned long size ); + +#else + +#define GetMemory( size ) GetMemoryDebug( size, # size, __FILE__, __LINE__ ); +#define GetClearedMemory( size ) GetClearedMemoryDebug( size, # size, __FILE__, __LINE__ ); +//allocate a memory block of the given size +void *GetMemoryDebug( unsigned long size, char *label, char *file, int line ); +//allocate a memory block of the given size and clear it +void *GetClearedMemoryDebug( unsigned long size, char *label, char *file, int line ); +// +void PrintMemoryLabels( void ); +#endif //MEMDEBUG + +void FreeMemory( void *ptr ); +int MemorySize( void *ptr ); +void PrintMemorySize( unsigned long size ); +int TotalAllocatedMemory( void ); + diff --git a/src/bspc/l_poly.c b/src/bspc/l_poly.c new file mode 100644 index 0000000..761cd9c --- /dev/null +++ b/src/bspc/l_poly.c @@ -0,0 +1,1434 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: l_poly.c +// Function: +// Programmer: id Sofware +// Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + +#include +#include "l_cmd.h" +#include "l_math.h" +#include "l_poly.h" +#include "l_log.h" +#include "l_mem.h" + +//#define BOGUS_RANGE 8192 +#define BOGUS_RANGE ( 128 * 1024 ) + +extern int numthreads; + +// counters are only bumped when running single threaded, +// because they are an awefull coherence problem +int c_active_windings; +int c_peak_windings; +int c_winding_allocs; +int c_winding_points; +int c_windingmemory; +int c_peak_windingmemory; + +char windingerror[1024]; + +void pw( winding_t *w ) { + int i; + for ( i = 0 ; i < w->numpoints ; i++ ) + printf( "(%5.3f, %5.3f, %5.3f)\n",w->p[i][0], w->p[i][1],w->p[i][2] ); +} + + +void ResetWindings( void ) { + c_active_windings = 0; + c_peak_windings = 0; + c_winding_allocs = 0; + c_winding_points = 0; + c_windingmemory = 0; + c_peak_windingmemory = 0; + + strcpy( windingerror, "" ); +} //end of the function ResetWindings +/* +============= +AllocWinding +============= +*/ +winding_t *AllocWinding( int points ) { + winding_t *w; + int s; + + s = sizeof( vec_t ) * 3 * points + sizeof( int ); + w = GetMemory( s ); + memset( w, 0, s ); + + if ( numthreads == 1 ) { + c_winding_allocs++; + c_winding_points += points; + c_active_windings++; + if ( c_active_windings > c_peak_windings ) { + c_peak_windings = c_active_windings; + } + c_windingmemory += MemorySize( w ); + if ( c_windingmemory > c_peak_windingmemory ) { + c_peak_windingmemory = c_windingmemory; + } + } //end if + return w; +} //end of the function AllocWinding + +void FreeWinding( winding_t *w ) { + if ( *(unsigned *)w == 0xdeaddead ) { + Error( "FreeWinding: freed a freed winding" ); + } + + if ( numthreads == 1 ) { + c_active_windings--; + c_windingmemory -= MemorySize( w ); + } //end if + + *(unsigned *)w = 0xdeaddead; + + FreeMemory( w ); +} //end of the function FreeWinding + +int WindingMemory( void ) { + return c_windingmemory; +} //end of the function WindingMemory + +int WindingPeakMemory( void ) { + return c_peak_windingmemory; +} //end of the function WindingPeakMemory + +int ActiveWindings( void ) { + return c_active_windings; +} //end of the function ActiveWindings +/* +============ +RemoveColinearPoints +============ +*/ +int c_removed; + +void RemoveColinearPoints( winding_t *w ) { + int i, j, k; + vec3_t v1, v2; + int nump; + vec3_t p[MAX_POINTS_ON_WINDING]; + + nump = 0; + for ( i = 0 ; i < w->numpoints ; i++ ) + { + j = ( i + 1 ) % w->numpoints; + k = ( i + w->numpoints - 1 ) % w->numpoints; + VectorSubtract( w->p[j], w->p[i], v1 ); + VectorSubtract( w->p[i], w->p[k], v2 ); + VectorNormalize( v1 ); + VectorNormalize( v2 ); + if ( DotProduct( v1, v2 ) < 0.999 ) { + if ( nump >= MAX_POINTS_ON_WINDING ) { + Error( "RemoveColinearPoints: MAX_POINTS_ON_WINDING" ); + } + VectorCopy( w->p[i], p[nump] ); + nump++; + } + } + + if ( nump == w->numpoints ) { + return; + } + + if ( numthreads == 1 ) { + c_removed += w->numpoints - nump; + } + w->numpoints = nump; + memcpy( w->p, p, nump * sizeof( p[0] ) ); +} + +/* +============ +WindingPlane +============ +*/ +void WindingPlane( winding_t *w, vec3_t normal, vec_t *dist ) { + vec3_t v1, v2; + int i; + + //find two vectors each longer than 0.5 units + for ( i = 0; i < w->numpoints; i++ ) + { + VectorSubtract( w->p[( i + 1 ) % w->numpoints], w->p[i], v1 ); + VectorSubtract( w->p[( i + 2 ) % w->numpoints], w->p[i], v2 ); + if ( VectorLength( v1 ) > 0.5 && VectorLength( v2 ) > 0.5 ) { + break; + } + } //end for + CrossProduct( v2, v1, normal ); + VectorNormalize( normal ); + *dist = DotProduct( w->p[0], normal ); +} //end of the function WindingPlane + +/* +============= +WindingArea +============= +*/ +vec_t WindingArea( winding_t *w ) { + int i; + vec3_t d1, d2, cross; + vec_t total; + + total = 0; + for ( i = 2 ; i < w->numpoints ; i++ ) + { + VectorSubtract( w->p[i - 1], w->p[0], d1 ); + VectorSubtract( w->p[i], w->p[0], d2 ); + CrossProduct( d1, d2, cross ); + total += 0.5 * VectorLength( cross ); + } + return total; +} + +void WindingBounds( winding_t *w, vec3_t mins, vec3_t maxs ) { + vec_t v; + int i,j; + + mins[0] = mins[1] = mins[2] = 99999; + maxs[0] = maxs[1] = maxs[2] = -99999; + + for ( i = 0 ; i < w->numpoints ; i++ ) + { + for ( j = 0 ; j < 3 ; j++ ) + { + v = w->p[i][j]; + if ( v < mins[j] ) { + mins[j] = v; + } + if ( v > maxs[j] ) { + maxs[j] = v; + } + } + } +} + +/* +============= +WindingCenter +============= +*/ +void WindingCenter( winding_t *w, vec3_t center ) { + int i; + float scale; + + VectorCopy( vec3_origin, center ); + for ( i = 0 ; i < w->numpoints ; i++ ) + VectorAdd( w->p[i], center, center ); + + scale = 1.0 / w->numpoints; + VectorScale( center, scale, center ); +} + +/* +================= +BaseWindingForPlane +================= +*/ +winding_t *BaseWindingForPlane( vec3_t normal, vec_t dist ) { + int i, x; + vec_t max, v; + vec3_t org, vright, vup; + winding_t *w; + +// find the major axis + + max = -BOGUS_RANGE; + x = -1; + for ( i = 0 ; i < 3; i++ ) + { + v = fabs( normal[i] ); + if ( v > max ) { + x = i; + max = v; + } + } + if ( x == -1 ) { + Error( "BaseWindingForPlane: no axis found" ); + } + + VectorCopy( vec3_origin, vup ); + switch ( x ) + { + case 0: + case 1: + vup[2] = 1; + break; + case 2: + vup[0] = 1; + break; + } + + v = DotProduct( vup, normal ); + VectorMA( vup, -v, normal, vup ); + VectorNormalize( vup ); + + VectorScale( normal, dist, org ); + + CrossProduct( vup, normal, vright ); + + VectorScale( vup, BOGUS_RANGE, vup ); + VectorScale( vright, BOGUS_RANGE, vright ); + +// project a really big axis aligned box onto the plane + w = AllocWinding( 4 ); + + VectorSubtract( org, vright, w->p[0] ); + VectorAdd( w->p[0], vup, w->p[0] ); + + VectorAdd( org, vright, w->p[1] ); + VectorAdd( w->p[1], vup, w->p[1] ); + + VectorAdd( org, vright, w->p[2] ); + VectorSubtract( w->p[2], vup, w->p[2] ); + + VectorSubtract( org, vright, w->p[3] ); + VectorSubtract( w->p[3], vup, w->p[3] ); + + w->numpoints = 4; + + return w; +} + +/* +================== +CopyWinding +================== +*/ +winding_t *CopyWinding( winding_t *w ) { + int size; + winding_t *c; + + c = AllocWinding( w->numpoints ); + size = (int)( (winding_t *)0 )->p[w->numpoints]; + memcpy( c, w, size ); + return c; +} + +/* +================== +ReverseWinding +================== +*/ +winding_t *ReverseWinding( winding_t *w ) { + int i; + winding_t *c; + + c = AllocWinding( w->numpoints ); + for ( i = 0 ; i < w->numpoints ; i++ ) + { + VectorCopy( w->p[w->numpoints - 1 - i], c->p[i] ); + } + c->numpoints = w->numpoints; + return c; +} + + +/* +============= +ClipWindingEpsilon +============= +*/ +void ClipWindingEpsilon( winding_t *in, vec3_t normal, vec_t dist, + vec_t epsilon, winding_t **front, winding_t **back ) { + vec_t dists[MAX_POINTS_ON_WINDING + 4]; + int sides[MAX_POINTS_ON_WINDING + 4]; + int counts[3]; + //MrElusive: DOH can't use statics when unsing multithreading!!! + vec_t dot; // VC 4.2 optimizer bug if not static + int i, j; + vec_t *p1, *p2; + vec3_t mid; + winding_t *f, *b; + int maxpts; + + counts[0] = counts[1] = counts[2] = 0; + +// determine sides for each point + for ( i = 0 ; i < in->numpoints ; i++ ) + { + dot = DotProduct( in->p[i], normal ); + dot -= dist; + dists[i] = dot; + if ( dot > epsilon ) { + sides[i] = SIDE_FRONT; + } else if ( dot < -epsilon ) { + sides[i] = SIDE_BACK; + } else + { + sides[i] = SIDE_ON; + } + counts[sides[i]]++; + } + sides[i] = sides[0]; + dists[i] = dists[0]; + + *front = *back = NULL; + + if ( !counts[0] ) { + *back = CopyWinding( in ); + return; + } + if ( !counts[1] ) { + *front = CopyWinding( in ); + return; + } + + maxpts = in->numpoints + 4; // cant use counts[0]+2 because + // of fp grouping errors + + *front = f = AllocWinding( maxpts ); + *back = b = AllocWinding( maxpts ); + + for ( i = 0 ; i < in->numpoints ; i++ ) + { + p1 = in->p[i]; + + if ( sides[i] == SIDE_ON ) { + VectorCopy( p1, f->p[f->numpoints] ); + f->numpoints++; + VectorCopy( p1, b->p[b->numpoints] ); + b->numpoints++; + continue; + } + + if ( sides[i] == SIDE_FRONT ) { + VectorCopy( p1, f->p[f->numpoints] ); + f->numpoints++; + } + if ( sides[i] == SIDE_BACK ) { + VectorCopy( p1, b->p[b->numpoints] ); + b->numpoints++; + } + + if ( sides[i + 1] == SIDE_ON || sides[i + 1] == sides[i] ) { + continue; + } + + // generate a split point + p2 = in->p[( i + 1 ) % in->numpoints]; + + dot = dists[i] / ( dists[i] - dists[i + 1] ); + for ( j = 0 ; j < 3 ; j++ ) + { // avoid round off error when possible + if ( normal[j] == 1 ) { + mid[j] = dist; + } else if ( normal[j] == -1 ) { + mid[j] = -dist; + } else { + mid[j] = p1[j] + dot * ( p2[j] - p1[j] ); + } + } + + VectorCopy( mid, f->p[f->numpoints] ); + f->numpoints++; + VectorCopy( mid, b->p[b->numpoints] ); + b->numpoints++; + } + + if ( f->numpoints > maxpts || b->numpoints > maxpts ) { + Error( "ClipWinding: points exceeded estimate" ); + } + if ( f->numpoints > MAX_POINTS_ON_WINDING || b->numpoints > MAX_POINTS_ON_WINDING ) { + Error( "ClipWinding: MAX_POINTS_ON_WINDING" ); + } +} + + +/* +============= +ChopWindingInPlace +============= +*/ +void ChopWindingInPlace( winding_t **inout, vec3_t normal, vec_t dist, vec_t epsilon ) { + winding_t *in; + vec_t dists[MAX_POINTS_ON_WINDING + 4]; + int sides[MAX_POINTS_ON_WINDING + 4]; + int counts[3]; + //MrElusive: DOH can't use statics when unsing multithreading!!! + vec_t dot; // VC 4.2 optimizer bug if not static + int i, j; + vec_t *p1, *p2; + vec3_t mid; + winding_t *f; + int maxpts; + + in = *inout; + counts[0] = counts[1] = counts[2] = 0; + +// determine sides for each point + for ( i = 0 ; i < in->numpoints ; i++ ) + { + dot = DotProduct( in->p[i], normal ); + dot -= dist; + dists[i] = dot; + if ( dot > epsilon ) { + sides[i] = SIDE_FRONT; + } else if ( dot < -epsilon ) { + sides[i] = SIDE_BACK; + } else + { + sides[i] = SIDE_ON; + } + counts[sides[i]]++; + } + sides[i] = sides[0]; + dists[i] = dists[0]; + + if ( !counts[0] ) { + FreeWinding( in ); + *inout = NULL; + return; + } + if ( !counts[1] ) { + return; // inout stays the same + + } + maxpts = in->numpoints + 4; // cant use counts[0]+2 because + // of fp grouping errors + + f = AllocWinding( maxpts ); + + for ( i = 0 ; i < in->numpoints ; i++ ) + { + p1 = in->p[i]; + + if ( sides[i] == SIDE_ON ) { + VectorCopy( p1, f->p[f->numpoints] ); + f->numpoints++; + continue; + } + + if ( sides[i] == SIDE_FRONT ) { + VectorCopy( p1, f->p[f->numpoints] ); + f->numpoints++; + } + + if ( sides[i + 1] == SIDE_ON || sides[i + 1] == sides[i] ) { + continue; + } + + // generate a split point + p2 = in->p[( i + 1 ) % in->numpoints]; + + dot = dists[i] / ( dists[i] - dists[i + 1] ); + for ( j = 0 ; j < 3 ; j++ ) + { // avoid round off error when possible + if ( normal[j] == 1 ) { + mid[j] = dist; + } else if ( normal[j] == -1 ) { + mid[j] = -dist; + } else { + mid[j] = p1[j] + dot * ( p2[j] - p1[j] ); + } + } + + VectorCopy( mid, f->p[f->numpoints] ); + f->numpoints++; + } + + if ( f->numpoints > maxpts ) { + Error( "ClipWinding: points exceeded estimate" ); + } + if ( f->numpoints > MAX_POINTS_ON_WINDING ) { + Error( "ClipWinding: MAX_POINTS_ON_WINDING" ); + } + + FreeWinding( in ); + *inout = f; +} + + +/* +================= +ChopWinding + +Returns the fragment of in that is on the front side +of the cliping plane. The original is freed. +================= +*/ +winding_t *ChopWinding( winding_t *in, vec3_t normal, vec_t dist ) { + winding_t *f, *b; + + ClipWindingEpsilon( in, normal, dist, ON_EPSILON, &f, &b ); + FreeWinding( in ); + if ( b ) { + FreeWinding( b ); + } + return f; +} + + +/* +================= +CheckWinding + +================= +*/ +void CheckWinding( winding_t *w ) { + int i, j; + vec_t *p1, *p2; + vec_t d, edgedist; + vec3_t dir, edgenormal, facenormal; + vec_t area; + vec_t facedist; + + if ( w->numpoints < 3 ) { + Error( "CheckWinding: %i points",w->numpoints ); + } + + area = WindingArea( w ); + if ( area < 1 ) { + Error( "CheckWinding: %f area", area ); + } + + WindingPlane( w, facenormal, &facedist ); + + for ( i = 0 ; i < w->numpoints ; i++ ) + { + p1 = w->p[i]; + + for ( j = 0 ; j < 3 ; j++ ) + if ( p1[j] > BOGUS_RANGE || p1[j] < -BOGUS_RANGE ) { + Error( "CheckWinding: BUGUS_RANGE: %f",p1[j] ); + } + + j = i + 1 == w->numpoints ? 0 : i + 1; + + // check the point is on the face plane + d = DotProduct( p1, facenormal ) - facedist; + if ( d < -ON_EPSILON || d > ON_EPSILON ) { + Error( "CheckWinding: point off plane" ); + } + + // check the edge isnt degenerate + p2 = w->p[j]; + VectorSubtract( p2, p1, dir ); + + if ( VectorLength( dir ) < ON_EPSILON ) { + Error( "CheckWinding: degenerate edge" ); + } + + CrossProduct( facenormal, dir, edgenormal ); + VectorNormalize( edgenormal ); + edgedist = DotProduct( p1, edgenormal ); + edgedist += ON_EPSILON; + + // all other points must be on front side + for ( j = 0 ; j < w->numpoints ; j++ ) + { + if ( j == i ) { + continue; + } + d = DotProduct( w->p[j], edgenormal ); + if ( d > edgedist ) { + Error( "CheckWinding: non-convex" ); + } + } + } +} + + +/* +============ +WindingOnPlaneSide +============ +*/ +int WindingOnPlaneSide( winding_t *w, vec3_t normal, vec_t dist ) { + qboolean front, back; + int i; + vec_t d; + + front = false; + back = false; + for ( i = 0 ; i < w->numpoints ; i++ ) + { + d = DotProduct( w->p[i], normal ) - dist; + if ( d < -ON_EPSILON ) { + if ( front ) { + return SIDE_CROSS; + } + back = true; + continue; + } + if ( d > ON_EPSILON ) { + if ( back ) { + return SIDE_CROSS; + } + front = true; + continue; + } + } + + if ( back ) { + return SIDE_BACK; + } + if ( front ) { + return SIDE_FRONT; + } + return SIDE_ON; +} + +//#ifdef ME + #define CONTINUOUS_EPSILON 0.005 +//#else +// #define CONTINUOUS_EPSILON 0.001 +//#endif + +/* +============= +TryMergeWinding + +If two polygons share a common edge and the edges that meet at the +common points are both inside the other polygons, merge them + +Returns NULL if the faces couldn't be merged, or the new face. +The originals will NOT be freed. +============= +*/ + +winding_t *TryMergeWinding( winding_t *f1, winding_t *f2, vec3_t planenormal ) { + vec_t *p1, *p2, *p3, *p4, *back; + winding_t *newf; + int i, j, k, l; + vec3_t normal, delta; + vec_t dot; + qboolean keep1, keep2; + + + // + // find a common edge + // + p1 = p2 = NULL; // stop compiler warning + j = 0; // + + for ( i = 0; i < f1->numpoints; i++ ) + { + p1 = f1->p[i]; + p2 = f1->p[( i + 1 ) % f1->numpoints]; + for ( j = 0; j < f2->numpoints; j++ ) + { + p3 = f2->p[j]; + p4 = f2->p[( j + 1 ) % f2->numpoints]; + for ( k = 0; k < 3; k++ ) + { + if ( fabs( p1[k] - p4[k] ) > 0.1 ) { //EQUAL_EPSILON) //ME + break; + } + if ( fabs( p2[k] - p3[k] ) > 0.1 ) { //EQUAL_EPSILON) //ME + break; + } + } //end for + if ( k == 3 ) { + break; + } + } //end for + if ( j < f2->numpoints ) { + break; + } + } //end for + + if ( i == f1->numpoints ) { + return NULL; // no matching edges + + } + // + // check slope of connected lines + // if the slopes are colinear, the point can be removed + // + back = f1->p[( i + f1->numpoints - 1 ) % f1->numpoints]; + VectorSubtract( p1, back, delta ); + CrossProduct( planenormal, delta, normal ); + VectorNormalize( normal ); + + back = f2->p[( j + 2 ) % f2->numpoints]; + VectorSubtract( back, p1, delta ); + dot = DotProduct( delta, normal ); + if ( dot > CONTINUOUS_EPSILON ) { + return NULL; // not a convex polygon + } + keep1 = (qboolean)( dot < -CONTINUOUS_EPSILON ); + + back = f1->p[( i + 2 ) % f1->numpoints]; + VectorSubtract( back, p2, delta ); + CrossProduct( planenormal, delta, normal ); + VectorNormalize( normal ); + + back = f2->p[( j + f2->numpoints - 1 ) % f2->numpoints]; + VectorSubtract( back, p2, delta ); + dot = DotProduct( delta, normal ); + if ( dot > CONTINUOUS_EPSILON ) { + return NULL; // not a convex polygon + } + keep2 = (qboolean)( dot < -CONTINUOUS_EPSILON ); + + // + // build the new polygon + // + newf = AllocWinding( f1->numpoints + f2->numpoints ); + + // copy first polygon + for ( k = ( i + 1 ) % f1->numpoints ; k != i ; k = ( k + 1 ) % f1->numpoints ) + { + if ( k == ( i + 1 ) % f1->numpoints && !keep2 ) { + continue; + } + + VectorCopy( f1->p[k], newf->p[newf->numpoints] ); + newf->numpoints++; + } + + // copy second polygon + for ( l = ( j + 1 ) % f2->numpoints ; l != j ; l = ( l + 1 ) % f2->numpoints ) + { + if ( l == ( j + 1 ) % f2->numpoints && !keep1 ) { + continue; + } + VectorCopy( f2->p[l], newf->p[newf->numpoints] ); + newf->numpoints++; + } + + return newf; +} + +//#ifdef ME +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +winding_t *MergeWindings( winding_t *w1, winding_t *w2, vec3_t planenormal ) { + winding_t *neww; + float dist; + int i, j, n, found, insertafter; + int sides[MAX_POINTS_ON_WINDING + 4]; + vec3_t newp[MAX_POINTS_ON_WINDING + 4]; + int numpoints; + vec3_t edgevec, sepnormal, v; + + RemoveEqualPoints( w1, 0.2 ); + numpoints = w1->numpoints; + memcpy( newp, w1->p, w1->numpoints * sizeof( vec3_t ) ); + // + for ( i = 0; i < w2->numpoints; i++ ) + { + VectorCopy( w2->p[i], v ); + for ( j = 0; j < numpoints; j++ ) + { + VectorSubtract( newp[( j + 1 ) % numpoints], + newp[( j ) % numpoints], edgevec ); + CrossProduct( edgevec, planenormal, sepnormal ); + VectorNormalize( sepnormal ); + if ( VectorLength( sepnormal ) < 0.9 ) { + //remove the point from the new winding + for ( n = j; n < numpoints - 1; n++ ) + { + VectorCopy( newp[n + 1], newp[n] ); + sides[n] = sides[n + 1]; + } //end for + numpoints--; + j--; + Log_Print( "MergeWindings: degenerate edge on winding %f %f %f\n", sepnormal[0], + sepnormal[1], + sepnormal[2] ); + continue; + } //end if + dist = DotProduct( newp[( j ) % numpoints], sepnormal ); + if ( DotProduct( v, sepnormal ) - dist < -0.1 ) { + sides[j] = SIDE_BACK; + } else { sides[j] = SIDE_FRONT;} + } //end for + //remove all unnecesary points + for ( j = 0; j < numpoints; ) + { + if ( sides[j] == SIDE_BACK + && sides[( j + 1 ) % numpoints] == SIDE_BACK ) { + //remove the point from the new winding + for ( n = ( j + 1 ) % numpoints; n < numpoints - 1; n++ ) + { + VectorCopy( newp[n + 1], newp[n] ); + sides[n] = sides[n + 1]; + } //end for + numpoints--; + } //end if + else + { + j++; + } //end else + } //end for + // + found = false; + for ( j = 0; j < numpoints; j++ ) + { + if ( sides[j] == SIDE_FRONT + && sides[( j + 1 ) % numpoints] == SIDE_BACK ) { + if ( found ) { + Log_Print( "Warning: MergeWindings: front to back found twice\n" ); + } + found = true; + } //end if + } //end for + // + for ( j = 0; j < numpoints; j++ ) + { + if ( sides[j] == SIDE_FRONT + && sides[( j + 1 ) % numpoints] == SIDE_BACK ) { + insertafter = ( j + 1 ) % numpoints; + //insert the new point after j+1 + for ( n = numpoints - 1; n > insertafter; n-- ) + { + VectorCopy( newp[n], newp[n + 1] ); + } //end for + numpoints++; + VectorCopy( v, newp[( insertafter + 1 ) % numpoints] ); + break; + } //end if + } //end for + } //end for + neww = AllocWinding( numpoints ); + neww->numpoints = numpoints; + memcpy( neww->p, newp, numpoints * sizeof( vec3_t ) ); + RemoveColinearPoints( neww ); + return neww; +} //end of the function MergeWindings +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +char *WindingErrorString( void ) { + return windingerror; +} //end of the function WindingErrorString +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int WindingError( winding_t *w ) { + int i, j; + vec_t *p1, *p2; + vec_t d, edgedist; + vec3_t dir, edgenormal, facenormal; + vec_t area; + vec_t facedist; + + if ( w->numpoints < 3 ) { + sprintf( windingerror, "winding %i points", w->numpoints ); + return WE_NOTENOUGHPOINTS; + } //end if + + area = WindingArea( w ); + if ( area < 1 ) { + sprintf( windingerror, "winding %f area", area ); + return WE_SMALLAREA; + } //end if + + WindingPlane( w, facenormal, &facedist ); + + for ( i = 0 ; i < w->numpoints ; i++ ) + { + p1 = w->p[i]; + + for ( j = 0 ; j < 3 ; j++ ) + { + if ( p1[j] > BOGUS_RANGE || p1[j] < -BOGUS_RANGE ) { + sprintf( windingerror, "winding point %d BUGUS_RANGE \'%f %f %f\'", j, p1[0], p1[1], p1[2] ); + return WE_POINTBOGUSRANGE; + } //end if + } //end for + + j = i + 1 == w->numpoints ? 0 : i + 1; + + // check the point is on the face plane + d = DotProduct( p1, facenormal ) - facedist; + if ( d < -ON_EPSILON || d > ON_EPSILON ) { + sprintf( windingerror, "winding point %d off plane", i ); + return WE_POINTOFFPLANE; + } //end if + + // check the edge isnt degenerate + p2 = w->p[j]; + VectorSubtract( p2, p1, dir ); + + if ( VectorLength( dir ) < ON_EPSILON ) { + sprintf( windingerror, "winding degenerate edge %d-%d", i, j ); + return WE_DEGENERATEEDGE; + } //end if + + CrossProduct( facenormal, dir, edgenormal ); + VectorNormalize( edgenormal ); + edgedist = DotProduct( p1, edgenormal ); + edgedist += ON_EPSILON; + + // all other points must be on front side + for ( j = 0 ; j < w->numpoints ; j++ ) + { + if ( j == i ) { + continue; + } + d = DotProduct( w->p[j], edgenormal ); + if ( d > edgedist ) { + sprintf( windingerror, "winding non-convex" ); + return WE_NONCONVEX; + } //end if + } //end for + } //end for + return WE_NONE; +} //end of the function WindingError +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void RemoveEqualPoints( winding_t *w, float epsilon ) { + int i, nump; + vec3_t v; + vec3_t p[MAX_POINTS_ON_WINDING]; + + VectorCopy( w->p[0], p[0] ); + nump = 1; + for ( i = 1; i < w->numpoints; i++ ) + { + VectorSubtract( w->p[i], p[nump - 1], v ); + if ( VectorLength( v ) > epsilon ) { + if ( nump >= MAX_POINTS_ON_WINDING ) { + Error( "RemoveColinearPoints: MAX_POINTS_ON_WINDING" ); + } + VectorCopy( w->p[i], p[nump] ); + nump++; + } //end if + } //end for + + if ( nump == w->numpoints ) { + return; + } + + w->numpoints = nump; + memcpy( w->p, p, nump * sizeof( p[0] ) ); +} //end of the function RemoveEqualPoints +//=========================================================================== +// adds the given point to a winding at the given spot +// (for instance when spot is zero then the point is added at position zero) +// the original winding is NOT freed +// +// Parameter: - +// Returns: the new winding with the added point +// Changes Globals: - +//=========================================================================== +winding_t *AddWindingPoint( winding_t *w, vec3_t point, int spot ) { + int i, j; + winding_t *neww; + + if ( spot > w->numpoints ) { + Error( "AddWindingPoint: num > w->numpoints" ); + } //end if + if ( spot < 0 ) { + Error( "AddWindingPoint: num < 0" ); + } //end if + neww = AllocWinding( w->numpoints + 1 ); + neww->numpoints = w->numpoints + 1; + for ( i = 0, j = 0; i < neww->numpoints; i++ ) + { + if ( i == spot ) { + VectorCopy( point, neww->p[i] ); + } //end if + else + { + VectorCopy( w->p[j], neww->p[i] ); + j++; + } //end else + } //end for + return neww; +} //end of the function AddWindingPoint +//=========================================================================== +// the position where the new point should be added in the winding is +// stored in *spot +// +// Parameter: - +// Returns: true if the point is on the winding +// Changes Globals: - +//=========================================================================== +#define MELT_ON_EPSILON 0.2 + +int PointOnWinding( winding_t *w, vec3_t normal, float dist, vec3_t point, int *spot ) { + int i, j; + vec3_t v1, v2; + vec3_t edgenormal, edgevec; + float edgedist, dot; + + *spot = 0; + //the point must be on the winding plane + dot = DotProduct( point, normal ) - dist; + if ( dot < -MELT_ON_EPSILON || dot > MELT_ON_EPSILON ) { + return false; + } + // + for ( i = 0; i < w->numpoints; i++ ) + { + j = ( i + 1 ) % w->numpoints; + //get a plane orthogonal to the winding plane through the edge + VectorSubtract( w->p[j], w->p[i], edgevec ); + CrossProduct( normal, edgevec, edgenormal ); + VectorNormalize( edgenormal ); + edgedist = DotProduct( edgenormal, w->p[i] ); + //point must be not too far from the plane + dot = DotProduct( point, edgenormal ) - edgedist; + if ( dot < -MELT_ON_EPSILON || dot > MELT_ON_EPSILON ) { + continue; + } + //vector from first point of winding to the point to test + VectorSubtract( point, w->p[i], v1 ); + //vector from second point of winding to the point to test + VectorSubtract( point, w->p[j], v2 ); + //if the length of the vector is not larger than 0.5 units then + //the point is assumend to be the same as one of the winding points + if ( VectorNormalize( v1 ) < 0.5 ) { + return false; + } + if ( VectorNormalize( v2 ) < 0.5 ) { + return false; + } + //point must be between the two winding points + //(the two vectors must be directed towards each other, and on the + //same straight line) + if ( DotProduct( v1, v2 ) < -0.99 ) { + *spot = i + 1; + return true; + } //end if + } //end for + return false; +} //end of the function PointOnWinding +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int FindPlaneSeperatingWindings( winding_t *w1, winding_t *w2, vec3_t dir, + vec3_t normal, float *dist ) { + int i, i2, j, j2, n; + int sides1[3], sides2[3]; + float dist1, dist2, dot, diff; + vec3_t normal1, normal2; + vec3_t v1, v2; + + for ( i = 0; i < w1->numpoints; i++ ) + { + i2 = ( i + 1 ) % w1->numpoints; + // + VectorSubtract( w1->p[i2], w1->p[i], v1 ); + if ( VectorLength( v1 ) < 0.1 ) { + //Log_Write("FindPlaneSeperatingWindings: winding1 with degenerate edge\r\n"); + continue; + } //end if + CrossProduct( v1, dir, normal1 ); + VectorNormalize( normal1 ); + dist1 = DotProduct( normal1, w1->p[i] ); + // + for ( j = 0; j < w2->numpoints; j++ ) + { + j2 = ( j + 1 ) % w2->numpoints; + // + VectorSubtract( w2->p[j2], w2->p[j], v2 ); + if ( VectorLength( v2 ) < 0.1 ) { + //Log_Write("FindPlaneSeperatingWindings: winding2 with degenerate edge\r\n"); + continue; + } //end if + CrossProduct( v2, dir, normal2 ); + VectorNormalize( normal2 ); + dist2 = DotProduct( normal2, w2->p[j] ); + // + diff = dist1 - dist2; + if ( diff < -0.1 || diff > 0.1 ) { + dist2 = -dist2; + VectorNegate( normal2, normal2 ); + diff = dist1 - dist2; + if ( diff < -0.1 || diff > 0.1 ) { + continue; + } + } //end if + //check if the normal vectors are equal + for ( n = 0; n < 3; n++ ) + { + diff = normal1[n] - normal2[n]; + if ( diff < -0.0001 || diff > 0.0001 ) { + break; + } + } //end for + if ( n != 3 ) { + continue; + } + //check on which side of the seperating plane the points of + //the first winding are + sides1[0] = sides1[1] = sides1[2] = 0; + for ( n = 0; n < w1->numpoints; n++ ) + { + dot = DotProduct( w1->p[n], normal1 ) - dist1; + if ( dot > 0.1 ) { + sides1[0]++; + } else if ( dot < -0.1 ) { + sides1[1]++; + } else { sides1[2]++;} + } //end for + //check on which side of the seperating plane the points of + //the second winding are + sides2[0] = sides2[1] = sides2[2] = 0; + for ( n = 0; n < w2->numpoints; n++ ) + { + //used normal1 and dist1 (they are equal to normal2 and dist2) + dot = DotProduct( w2->p[n], normal1 ) - dist1; + if ( dot > 0.1 ) { + sides2[0]++; + } else if ( dot < -0.1 ) { + sides2[1]++; + } else { sides2[2]++;} + } //end for + //if the first winding has points at both sides + if ( sides1[0] && sides1[1] ) { + Log_Write( "FindPlaneSeperatingWindings: winding1 non-convex\r\n" ); + continue; + } //end if + //if the second winding has points at both sides + if ( sides2[0] && sides2[1] ) { + Log_Write( "FindPlaneSeperatingWindings: winding2 non-convex\r\n" ); + continue; + } //end if + // + if ( ( !sides1[0] && !sides1[1] ) || ( !sides2[0] && !sides2[1] ) ) { + //don't use one of the winding planes as the seperating plane + continue; + } //end if + //the windings must be at different sides of the seperating plane + if ( ( !sides1[0] && !sides2[1] ) || ( !sides1[1] && !sides2[0] ) ) { + VectorCopy( normal1, normal ); + *dist = dist1; + return true; + } //end if + } //end for + } //end for + return false; +} //end of the function FindPlaneSeperatingWindings +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#define WCONVEX_EPSILON 0.2 + +int WindingsNonConvex( winding_t *w1, winding_t *w2, + vec3_t normal1, vec3_t normal2, + float dist1, float dist2 ) { + int i; + + if ( !w1 || !w2 ) { + return false; + } + + //check if one of the points of face1 is at the back of the plane of face2 + for ( i = 0; i < w1->numpoints; i++ ) + { + if ( DotProduct( normal2, w1->p[i] ) - dist2 > WCONVEX_EPSILON ) { + return true; + } + } //end for + //check if one of the points of face2 is at the back of the plane of face1 + for ( i = 0; i < w2->numpoints; i++ ) + { + if ( DotProduct( normal1, w2->p[i] ) - dist1 > WCONVEX_EPSILON ) { + return true; + } + } //end for + + return false; +} //end of the function WindingsNonConvex +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +/* +#define VERTEX_EPSILON 0.5 + +qboolean EqualVertexes(vec3_t v1, vec3_t v2) +{ + float diff; + + diff = v1[0] - v2[0]; + if (diff > -VERTEX_EPSILON && diff < VERTEX_EPSILON) + { + diff = v1[1] - v2[1]; + if (diff > -VERTEX_EPSILON && diff < VERTEX_EPSILON) + { + diff = v1[2] - v2[2]; + if (diff > -VERTEX_EPSILON && diff < VERTEX_EPSILON) + { + return true; + } //end if + } //end if + } //end if + return false; +} //end of the function EqualVertexes + +#define CONTINUOUS_EPSILON 0.001 + +winding_t *AAS_MergeWindings(winding_t *w1, winding_t *w2, vec3_t windingnormal) +{ + int n, i, k; + vec3_t normal, delta; + winding_t *winding, *neww; + float dist, dot; + int p1, p2; + int points[2][64]; + int numpoints[2] = {0, 0}; + int newnumpoints; + int keep[2]; + + if (!FindPlaneSeperatingWindings(w1, w2, windingnormal, normal, &dist)) return NULL; + + //for both windings + for (n = 0; n < 2; n++) + { + if (n == 0) winding = w1; + else winding = w2; + //get the points of the winding which are on the seperating plane + for (i = 0; i < winding->numpoints; i++) + { + dot = DotProduct(winding->p[i], normal) - dist; + if (dot > -ON_EPSILON && dot < ON_EPSILON) + { + //don't allow more than 64 points on the seperating plane + if (numpoints[n] >= 64) Error("AAS_MergeWindings: more than 64 points on seperating plane\n"); + points[n][numpoints[n]++] = i; + } //end if + } //end for + //there must be at least two points of each winding on the seperating plane + if (numpoints[n] < 2) return NULL; + } //end for + + //if the first point of winding1 (which is on the seperating plane) is unequal + //to the last point of winding2 (which is on the seperating plane) + if (!EqualVertexes(w1->p[points[0][0]], w2->p[points[1][numpoints[1]-1]])) + { + return NULL; + } //end if + //if the last point of winding1 (which is on the seperating plane) is unequal + //to the first point of winding2 (which is on the seperating plane) + if (!EqualVertexes(w1->p[points[0][numpoints[0]-1]], w2->p[points[1][0]])) + { + return NULL; + } //end if + // + // check slope of connected lines + // if the slopes are colinear, the point can be removed + // + //first point of winding1 which is on the seperating plane + p1 = points[0][0]; + //point before p1 + p2 = (p1 + w1->numpoints - 1) % w1->numpoints; + VectorSubtract(w1->p[p1], w1->p[p2], delta); + CrossProduct(windingnormal, delta, normal); + VectorNormalize(normal, normal); + + //last point of winding2 which is on the seperating plane + p1 = points[1][numpoints[1]-1]; + //point after p1 + p2 = (p1 + 1) % w2->numpoints; + VectorSubtract(w2->p[p2], w2->p[p1], delta); + dot = DotProduct(delta, normal); + if (dot > CONTINUOUS_EPSILON) return NULL; //merging would create a non-convex polygon + keep[0] = (qboolean)(dot < -CONTINUOUS_EPSILON); + + //first point of winding2 which is on the seperating plane + p1 = points[1][0]; + //point before p1 + p2 = (p1 + w2->numpoints - 1) % w2->numpoints; + VectorSubtract(w2->p[p1], w2->p[p2], delta); + CrossProduct(windingnormal, delta, normal); + VectorNormalize(normal, normal); + + //last point of winding1 which is on the seperating plane + p1 = points[0][numpoints[0]-1]; + //point after p1 + p2 = (p1 + 1) % w1->numpoints; + VectorSubtract(w1->p[p2], w1->p[p1], delta); + dot = DotProduct(delta, normal); + if (dot > CONTINUOUS_EPSILON) return NULL; //merging would create a non-convex polygon + keep[1] = (qboolean)(dot < -CONTINUOUS_EPSILON); + + //number of points on the new winding + newnumpoints = w1->numpoints - numpoints[0] + w2->numpoints - numpoints[1] + 2; + //allocate the winding + neww = AllocWinding(newnumpoints); + neww->numpoints = newnumpoints; + //copy all the points + k = 0; + //for both windings + for (n = 0; n < 2; n++) + { + if (n == 0) winding = w1; + else winding = w2; + //copy the points of the winding starting with the last point on the + //seperating plane and ending before the first point on the seperating plane + for (i = points[n][numpoints[n]-1]; i != points[n][0]; i = (i+1)%winding->numpoints) + { + if (k >= newnumpoints) + { + Log_Print("numpoints[0] = %d\n", numpoints[0]); + Log_Print("numpoints[1] = %d\n", numpoints[1]); + Error("AAS_MergeWindings: k = %d >= newnumpoints = %d\n", k, newnumpoints); + } //end if + VectorCopy(winding->p[i], neww->p[k]); + k++; + } //end for + } //end for + RemoveEqualPoints(neww); + if (!WindingIsOk(neww, 1)) + { + Log_Print("AAS_MergeWindings: winding not ok after merging\n"); + FreeWinding(neww); + return NULL; + } //end if + return neww; +} //end of the function AAS_MergeWindings*/ +//#endif //ME diff --git a/src/bspc/l_poly.h b/src/bspc/l_poly.h new file mode 100644 index 0000000..028fc22 --- /dev/null +++ b/src/bspc/l_poly.h @@ -0,0 +1,136 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: l_poly.h +// Function: +// Programmer: id Sofware +// Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-04 +// Tab Size: 3 +//=========================================================================== + +//a winding gives the bounding points of a convex polygon +typedef struct +{ + int numpoints; + vec3_t p[4]; //variable sized +} winding_t; + +#define MAX_POINTS_ON_WINDING 96 + +//you can define on_epsilon in the makefile as tighter +#ifndef ON_EPSILON +#define ON_EPSILON 0.1 +#endif +//winding errors +#define WE_NONE 0 +#define WE_NOTENOUGHPOINTS 1 +#define WE_SMALLAREA 2 +#define WE_POINTBOGUSRANGE 3 +#define WE_POINTOFFPLANE 4 +#define WE_DEGENERATEEDGE 5 +#define WE_NONCONVEX 6 + +//allocates a winding +winding_t *AllocWinding( int points ); +//returns the area of the winding +vec_t WindingArea( winding_t *w ); +//gives the center of the winding +void WindingCenter( winding_t *w, vec3_t center ); +//clips the given winding to the given plane and gives the front +//and back part of the clipped winding +void ClipWindingEpsilon( winding_t *in, vec3_t normal, vec_t dist, + vec_t epsilon, winding_t **front, winding_t **back ); +//returns the fragment of the given winding that is on the front +//side of the cliping plane. The original is freed. +winding_t *ChopWinding( winding_t *in, vec3_t normal, vec_t dist ); +//returns a copy of the given winding +winding_t *CopyWinding( winding_t *w ); +//returns the reversed winding of the given one +winding_t *ReverseWinding( winding_t *w ); +//returns a base winding for the given plane +winding_t *BaseWindingForPlane( vec3_t normal, vec_t dist ); +//checks the winding for errors +void CheckWinding( winding_t *w ); +//returns the plane normal and dist the winding is in +void WindingPlane( winding_t *w, vec3_t normal, vec_t *dist ); +//removes colinear points from the winding +void RemoveColinearPoints( winding_t *w ); +//returns on which side of the plane the winding is situated +int WindingOnPlaneSide( winding_t *w, vec3_t normal, vec_t dist ); +//frees the winding +void FreeWinding( winding_t *w ); +//gets the bounds of the winding +void WindingBounds( winding_t *w, vec3_t mins, vec3_t maxs ); +//chops the winding with the given plane, the original winding is freed if clipped +void ChopWindingInPlace( winding_t **w, vec3_t normal, vec_t dist, vec_t epsilon ); +//prints the winding points on STDOUT +void pw( winding_t *w ); +//try to merge the two windings which are in the given plane +//the original windings are undisturbed +//the merged winding is returned when merging was possible +//NULL is returned otherwise +winding_t *TryMergeWinding( winding_t *f1, winding_t *f2, vec3_t planenormal ); +//brute force winding merging... creates a convex winding out of +//the two whatsoever +winding_t *MergeWindings( winding_t *w1, winding_t *w2, vec3_t planenormal ); + +//#ifdef ME +void ResetWindings( void ); +//returns the amount of winding memory +int WindingMemory( void ); +int WindingPeakMemory( void ); +int ActiveWindings( void ); +//returns the winding error string +char *WindingErrorString( void ); +//returns one of the WE_ flags when the winding has errors +int WindingError( winding_t *w ); +//removes equal points from the winding +void RemoveEqualPoints( winding_t *w, float epsilon ); +//returns a winding with a point added at the given spot to the +//given winding, original winding is NOT freed +winding_t *AddWindingPoint( winding_t *w, vec3_t point, int spot ); +//returns true if the point is on one of the winding 'edges' +//when the point is on one of the edged the number of the first +//point of the edge is stored in 'spot' +int PointOnWinding( winding_t *w, vec3_t normal, float dist, vec3_t point, int *spot ); +//find a plane seperating the two windings +//true is returned when the windings area adjacent +//the seperating plane normal and distance area stored in 'normal' and 'dist' +//this plane will contain both the piece of common edge of the two windings +//and the vector 'dir' +int FindPlaneSeperatingWindings( winding_t *w1, winding_t *w2, vec3_t dir, + vec3_t normal, float *dist ); +// +int WindingsNonConvex( winding_t *w1, winding_t *w2, + vec3_t normal1, vec3_t normal2, + float dist1, float dist2 ); +//#endif //ME + diff --git a/src/bspc/l_qfiles.c b/src/bspc/l_qfiles.c new file mode 100644 index 0000000..6d75e40 --- /dev/null +++ b/src/bspc/l_qfiles.c @@ -0,0 +1,702 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: l_qfiles.h +// Function: - +// Programmer: Mr Elusive +// Last update: 1999-11-29 +// Tab Size: 3 +//=========================================================================== + +#if defined( WIN32 ) | defined( _WIN32 ) +#include +#include +#include +#else +#include +#include +#include +#endif + +#include "qbsp.h" +#define stricmp strcasecmp + +//file extensions with their type +typedef struct qfile_exttype_s +{ + char *extension; + int type; +} qfile_exttyp_t; + +qfile_exttyp_t quakefiletypes[] = +{ + {QFILEEXT_UNKNOWN, QFILETYPE_UNKNOWN}, + {QFILEEXT_PAK, QFILETYPE_PAK}, + {QFILEEXT_PK3, QFILETYPE_PK3}, + {QFILEEXT_SIN, QFILETYPE_PAK}, + {QFILEEXT_BSP, QFILETYPE_BSP}, + {QFILEEXT_MAP, QFILETYPE_MAP}, + {QFILEEXT_MDL, QFILETYPE_MDL}, + {QFILEEXT_MD2, QFILETYPE_MD2}, + {QFILEEXT_MD3, QFILETYPE_MD3}, + {QFILEEXT_WAL, QFILETYPE_WAL}, + {QFILEEXT_WAV, QFILETYPE_WAV}, + {QFILEEXT_AAS, QFILETYPE_AAS}, + {NULL, 0} +}; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int QuakeFileExtensionType( char *extension ) { + int i; + + for ( i = 0; quakefiletypes[i].extension; i++ ) + { + if ( !stricmp( extension, quakefiletypes[i].extension ) ) { + return quakefiletypes[i].type; + } //end if + } //end for + return QFILETYPE_UNKNOWN; +} //end of the function QuakeFileExtensionType +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +char *QuakeFileTypeExtension( int type ) { + int i; + + for ( i = 0; quakefiletypes[i].extension; i++ ) + { + if ( quakefiletypes[i].type == type ) { + return quakefiletypes[i].extension; + } //end if + } //end for + return QFILEEXT_UNKNOWN; +} //end of the function QuakeFileExtension +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int QuakeFileType( char *filename ) { + char ext[_MAX_PATH] = "."; + + ExtractFileExtension( filename, ext + 1 ); + return QuakeFileExtensionType( ext ); +} //end of the function QuakeFileTypeFromFileName +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +char *StringContains( char *str1, char *str2, int casesensitive ) { + int len, i, j; + + len = strlen( str1 ) - strlen( str2 ); + for ( i = 0; i <= len; i++, str1++ ) + { + for ( j = 0; str2[j]; j++ ) + { + if ( casesensitive ) { + if ( str1[j] != str2[j] ) { + break; + } + } //end if + else + { + if ( toupper( str1[j] ) != toupper( str2[j] ) ) { + break; + } + } //end else + } //end for + if ( !str2[j] ) { + return str1; + } + } //end for + return NULL; +} //end of the function StringContains +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int FileFilter( char *filter, char *filename, int casesensitive ) { + char buf[1024]; + char *ptr; + int i, found; + + while ( *filter ) + { + if ( *filter == '*' ) { + filter++; + for ( i = 0; *filter; i++ ) + { + if ( *filter == '*' || *filter == '?' ) { + break; + } + buf[i] = *filter; + filter++; + } //end for + buf[i] = '\0'; + if ( strlen( buf ) ) { + ptr = StringContains( filename, buf, casesensitive ); + if ( !ptr ) { + return false; + } + filename = ptr + strlen( buf ); + } //end if + } //end if + else if ( *filter == '?' ) { + filter++; + filename++; + } //end else if + else if ( *filter == '[' && *( filter + 1 ) == '[' ) { + filter++; + } //end if + else if ( *filter == '[' ) { + filter++; + found = false; + while ( *filter && !found ) + { + if ( *filter == ']' && *( filter + 1 ) != ']' ) { + break; + } + if ( *( filter + 1 ) == '-' && *( filter + 2 ) && ( *( filter + 2 ) != ']' || *( filter + 3 ) == ']' ) ) { + if ( casesensitive ) { + if ( *filename >= *filter && *filename <= *( filter + 2 ) ) { + found = true; + } + } //end if + else + { + if ( toupper( *filename ) >= toupper( *filter ) && + toupper( *filename ) <= toupper( *( filter + 2 ) ) ) { + found = true; + } + } //end else + filter += 3; + } //end if + else + { + if ( casesensitive ) { + if ( *filter == *filename ) { + found = true; + } + } //end if + else + { + if ( toupper( *filter ) == toupper( *filename ) ) { + found = true; + } + } //end else + filter++; + } //end else + } //end while + if ( !found ) { + return false; + } + while ( *filter ) + { + if ( *filter == ']' && *( filter + 1 ) != ']' ) { + break; + } + filter++; + } //end while + filter++; + filename++; + } //end else if + else + { + if ( casesensitive ) { + if ( *filter != *filename ) { + return false; + } + } //end if + else + { + if ( toupper( *filter ) != toupper( *filename ) ) { + return false; + } + } //end else + filter++; + filename++; + } //end else + } //end while + return true; +} //end of the function FileFilter +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +quakefile_t *FindQuakeFilesInZip( char *zipfile, char *filter ) { + unzFile uf; + int err; + unz_global_info gi; + char filename_inzip[MAX_PATH]; + unz_file_info file_info; + int i; + quakefile_t *qfiles, *lastqf, *qf; + + uf = unzOpen( zipfile ); + err = unzGetGlobalInfo( uf, &gi ); + + if ( err != UNZ_OK ) { + return NULL; + } + + unzGoToFirstFile( uf ); + + qfiles = NULL; + lastqf = NULL; + for ( i = 0; i < gi.number_entry; i++ ) + { + err = unzGetCurrentFileInfo( uf, &file_info, filename_inzip, sizeof( filename_inzip ), NULL,0,NULL,0 ); + if ( err != UNZ_OK ) { + break; + } + + ConvertPath( filename_inzip ); + if ( FileFilter( filter, filename_inzip, false ) ) { + qf = malloc( sizeof( quakefile_t ) ); + if ( !qf ) { + Error( "out of memory" ); + } + memset( qf, 0, sizeof( quakefile_t ) ); + strcpy( qf->pakfile, zipfile ); + strcpy( qf->filename, zipfile ); + strcpy( qf->origname, filename_inzip ); + qf->zipfile = true; + //memcpy( &buildBuffer[i].zipfileinfo, (unz_s*)uf, sizeof(unz_s)); + memcpy( &qf->zipinfo, (unz_s*)uf, sizeof( unz_s ) ); + qf->offset = 0; + qf->length = file_info.uncompressed_size; + qf->type = QuakeFileType( filename_inzip ); + //add the file ot the list + qf->next = NULL; + if ( lastqf ) { + lastqf->next = qf; + } else { qfiles = qf;} + lastqf = qf; + } //end if + unzGoToNextFile( uf ); + } //end for + + unzClose( uf ); + + return qfiles; +} //end of the function FindQuakeFilesInZip +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +quakefile_t *FindQuakeFilesInPak( char *pakfile, char *filter ) { + FILE *fp; + dpackheader_t packheader; + dsinpackfile_t *packfiles; + dpackfile_t *idpackfiles; + quakefile_t *qfiles, *lastqf, *qf; + int numpackdirs, i; + + qfiles = NULL; + lastqf = NULL; + //open the pak file + fp = fopen( pakfile, "rb" ); + if ( !fp ) { + Warning( "can't open pak file %s", pakfile ); + return NULL; + } //end if + //read pak header, check for valid pak id and seek to the dir entries + if ( ( fread( &packheader, 1, sizeof( dpackheader_t ), fp ) != sizeof( dpackheader_t ) ) + || ( packheader.ident != IDPAKHEADER && packheader.ident != SINPAKHEADER ) + || ( fseek( fp, LittleLong( packheader.dirofs ), SEEK_SET ) ) + ) { + fclose( fp ); + Warning( "invalid pak file %s", pakfile ); + return NULL; + } //end if + //if it is a pak file from id software + if ( packheader.ident == IDPAKHEADER ) { + //number of dir entries in the pak file + numpackdirs = LittleLong( packheader.dirlen ) / sizeof( dpackfile_t ); + idpackfiles = (dpackfile_t *) malloc( numpackdirs * sizeof( dpackfile_t ) ); + if ( !idpackfiles ) { + Error( "out of memory" ); + } + //read the dir entry + if ( fread( idpackfiles, sizeof( dpackfile_t ), numpackdirs, fp ) != numpackdirs ) { + fclose( fp ); + free( idpackfiles ); + Warning( "can't read the Quake pak file dir entries from %s", pakfile ); + return NULL; + } //end if + fclose( fp ); + //convert to sin pack files + packfiles = (dsinpackfile_t *) malloc( numpackdirs * sizeof( dsinpackfile_t ) ); + if ( !packfiles ) { + Error( "out of memory" ); + } + for ( i = 0; i < numpackdirs; i++ ) + { + strcpy( packfiles[i].name, idpackfiles[i].name ); + packfiles[i].filepos = LittleLong( idpackfiles[i].filepos ); + packfiles[i].filelen = LittleLong( idpackfiles[i].filelen ); + } //end for + free( idpackfiles ); + } //end if + else //its a Sin pack file + { + //number of dir entries in the pak file + numpackdirs = LittleLong( packheader.dirlen ) / sizeof( dsinpackfile_t ); + packfiles = (dsinpackfile_t *) malloc( numpackdirs * sizeof( dsinpackfile_t ) ); + if ( !packfiles ) { + Error( "out of memory" ); + } + //read the dir entry + if ( fread( packfiles, sizeof( dsinpackfile_t ), numpackdirs, fp ) != numpackdirs ) { + fclose( fp ); + free( packfiles ); + Warning( "can't read the Sin pak file dir entries from %s", pakfile ); + return NULL; + } //end if + fclose( fp ); + for ( i = 0; i < numpackdirs; i++ ) + { + packfiles[i].filepos = LittleLong( packfiles[i].filepos ); + packfiles[i].filelen = LittleLong( packfiles[i].filelen ); + } //end for + } //end else + // + for ( i = 0; i < numpackdirs; i++ ) + { + ConvertPath( packfiles[i].name ); + if ( FileFilter( filter, packfiles[i].name, false ) ) { + qf = malloc( sizeof( quakefile_t ) ); + if ( !qf ) { + Error( "out of memory" ); + } + memset( qf, 0, sizeof( quakefile_t ) ); + strcpy( qf->pakfile, pakfile ); + strcpy( qf->filename, pakfile ); + strcpy( qf->origname, packfiles[i].name ); + qf->zipfile = false; + qf->offset = packfiles[i].filepos; + qf->length = packfiles[i].filelen; + qf->type = QuakeFileType( packfiles[i].name ); + //add the file ot the list + qf->next = NULL; + if ( lastqf ) { + lastqf->next = qf; + } else { qfiles = qf;} + lastqf = qf; + } //end if + } //end for + free( packfiles ); + return qfiles; +} //end of the function FindQuakeFilesInPak +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +quakefile_t *FindQuakeFilesWithPakFilter( char *pakfilter, char *filter ) { +#if defined( WIN32 ) | defined( _WIN32 ) + WIN32_FIND_DATA filedata; + HWND handle; + struct _stat statbuf; +#else + glob_t globbuf; + struct stat statbuf; + int j; +#endif + quakefile_t *qfiles, *lastqf, *qf; + char pakfile[_MAX_PATH], filename[_MAX_PATH], *str; + //int done; //TTimo: unused + + qfiles = NULL; + lastqf = NULL; + if ( pakfilter && strlen( pakfilter ) ) { +#if defined( WIN32 ) | defined( _WIN32 ) + handle = FindFirstFile( pakfilter, &filedata ); + done = ( handle == INVALID_HANDLE_VALUE ); + while ( !done ) + { + _splitpath( pakfilter, pakfile, NULL, NULL, NULL ); + _splitpath( pakfilter, NULL, &pakfile[strlen( pakfile )], NULL, NULL ); + AppendPathSeperator( pakfile, _MAX_PATH ); + strcat( pakfile, filedata.cFileName ); + _stat( pakfile, &statbuf ); +#else + glob( pakfilter, 0, NULL, &globbuf ); + for ( j = 0; j < globbuf.gl_pathc; j++ ) + { + strcpy( pakfile, globbuf.gl_pathv[j] ); + stat( pakfile, &statbuf ); +#endif + //if the file with .pak or .pk3 is a folder + if ( statbuf.st_mode & S_IFDIR ) { + strcpy( filename, pakfilter ); + AppendPathSeperator( filename, _MAX_PATH ); + strcat( filename, filter ); + qf = FindQuakeFilesWithPakFilter( NULL, filename ); + if ( lastqf ) { + lastqf->next = qf; + } else { qfiles = qf;} + lastqf = qf; + while ( lastqf->next ) lastqf = lastqf->next; + } //end if + else + { +#if defined( WIN32 ) | defined( _WIN32 ) + str = StringContains( pakfile, ".pk3", false ); +#else + str = StringContains( pakfile, ".pk3", true ); +#endif + if ( str && str == pakfile + strlen( pakfile ) - strlen( ".pk3" ) ) { + qf = FindQuakeFilesInZip( pakfile, filter ); + } //end if + else + { + qf = FindQuakeFilesInPak( pakfile, filter ); + } //end else + // + if ( qf ) { + if ( lastqf ) { + lastqf->next = qf; + } else { qfiles = qf;} + lastqf = qf; + while ( lastqf->next ) lastqf = lastqf->next; + } //end if + } //end else + // +#if defined( WIN32 ) | defined( _WIN32 ) + //find the next file + done = !FindNextFile( handle, &filedata ); + } //end while +#else + } //end for + globfree( &globbuf ); +#endif + } //end if + else + { +#if defined( WIN32 ) | defined( _WIN32 ) + handle = FindFirstFile( filter, &filedata ); + done = ( handle == INVALID_HANDLE_VALUE ); + while ( !done ) + { + _splitpath( filter, filename, NULL, NULL, NULL ); + _splitpath( filter, NULL, &filename[strlen( filename )], NULL, NULL ); + AppendPathSeperator( filename, _MAX_PATH ); + strcat( filename, filedata.cFileName ); +#else + glob( filter, 0, NULL, &globbuf ); + for ( j = 0; j < globbuf.gl_pathc; j++ ) + { + strcpy( filename, globbuf.gl_pathv[j] ); +#endif + // + qf = malloc( sizeof( quakefile_t ) ); + if ( !qf ) { + Error( "out of memory" ); + } + memset( qf, 0, sizeof( quakefile_t ) ); + strcpy( qf->pakfile, "" ); + strcpy( qf->filename, filename ); + strcpy( qf->origname, filename ); + qf->offset = 0; + qf->length = 0; + qf->type = QuakeFileType( filename ); + //add the file ot the list + qf->next = NULL; + if ( lastqf ) { + lastqf->next = qf; + } else { qfiles = qf;} + lastqf = qf; +#if defined( WIN32 ) | defined( _WIN32 ) + //find the next file + done = !FindNextFile( handle, &filedata ); + } //end while +#else + } //end for + globfree( &globbuf ); +#endif + } //end else + return qfiles; +} //end of the function FindQuakeFilesWithPakFilter +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +quakefile_t *FindQuakeFiles( char *filter ) { + char *str; + char newfilter[_MAX_PATH]; + char pakfilter[_MAX_PATH]; + char filefilter[_MAX_PATH]; + + strcpy( newfilter, filter ); + ConvertPath( newfilter ); + strcpy( pakfilter, newfilter ); + + str = StringContains( pakfilter, ".pak", false ); + if ( !str ) { + str = StringContains( pakfilter, ".pk3", false ); + } + + if ( str ) { + str += strlen( ".pak" ); + if ( *str ) { + *str++ = '\0'; + while ( *str == '\\' || *str == '/' ) str++; + strcpy( filefilter, str ); + return FindQuakeFilesWithPakFilter( pakfilter, filefilter ); + } //end if + } //end else + return FindQuakeFilesWithPakFilter( NULL, newfilter ); +} //end of the function FindQuakeFiles +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int LoadQuakeFile( quakefile_t *qf, void **bufferptr ) { + FILE *fp; + void *buffer; + int length; + unzFile zf; + + if ( qf->zipfile ) { + //open the zip file + zf = unzOpen( qf->pakfile ); + //set the file pointer + qf->zipinfo.file = ( (unz_s *) zf )->file; + //open the Quake file in the zip file + unzOpenCurrentFile( &qf->zipinfo ); + //allocate memory for the buffer + length = qf->length; + buffer = GetMemory( length + 1 ); + //read the Quake file from the zip file + length = unzReadCurrentFile( &qf->zipinfo, buffer, length ); + //close the Quake file in the zip file + unzCloseCurrentFile( &qf->zipinfo ); + //close the zip file + unzClose( zf ); + + *bufferptr = buffer; + return length; + } //end if + else + { + fp = SafeOpenRead( qf->filename ); + if ( qf->offset ) { + fseek( fp, qf->offset, SEEK_SET ); + } + length = qf->length; + if ( !length ) { + length = Q_filelength( fp ); + } + buffer = GetMemory( length + 1 ); + ( (char *)buffer )[length] = 0; + SafeRead( fp, buffer, length ); + fclose( fp ); + + *bufferptr = buffer; + return length; + } //end else +} //end of the function LoadQuakeFile +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int ReadQuakeFile( quakefile_t *qf, void *buffer, int offset, int length ) { + FILE *fp; + int read; + unzFile zf; + char tmpbuf[1024]; + + if ( qf->zipfile ) { + //open the zip file + zf = unzOpen( qf->pakfile ); + //set the file pointer + qf->zipinfo.file = ( (unz_s *) zf )->file; + //open the Quake file in the zip file + unzOpenCurrentFile( &qf->zipinfo ); + // + while ( offset > 0 ) + { + read = offset; + if ( read > sizeof( tmpbuf ) ) { + read = sizeof( tmpbuf ); + } + unzReadCurrentFile( &qf->zipinfo, tmpbuf, read ); + offset -= read; + } //end while + //read the Quake file from the zip file + length = unzReadCurrentFile( &qf->zipinfo, buffer, length ); + //close the Quake file in the zip file + unzCloseCurrentFile( &qf->zipinfo ); + //close the zip file + unzClose( zf ); + + return length; + } //end if + else + { + fp = SafeOpenRead( qf->filename ); + if ( qf->offset ) { + fseek( fp, qf->offset, SEEK_SET ); + } + if ( offset ) { + fseek( fp, offset, SEEK_CUR ); + } + SafeRead( fp, buffer, length ); + fclose( fp ); + + return length; + } //end else +} //end of the function ReadQuakeFile diff --git a/src/bspc/l_qfiles.h b/src/bspc/l_qfiles.h new file mode 100644 index 0000000..7b331ad --- /dev/null +++ b/src/bspc/l_qfiles.h @@ -0,0 +1,106 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: l_qfiles.h +// Function: - +// Programmer: Mr Elusive +// Last update: 1999-12-01 +// Tab Size: 3 +//=========================================================================== + +#include "../qcommon/unzip.h" + +#define QFILETYPE_UNKNOWN 0x8000 +#define QFILETYPE_PAK 0x0001 +#define QFILETYPE_PK3 0x0002 +#define QFILETYPE_BSP 0x0004 +#define QFILETYPE_MAP 0x0008 +#define QFILETYPE_MDL 0x0010 +#define QFILETYPE_MD2 0x0020 +#define QFILETYPE_MD3 0x0040 +#define QFILETYPE_WAL 0x0080 +#define QFILETYPE_WAV 0x0100 +#define QFILETYPE_AAS 0x4000 + +#define QFILEEXT_UNKNOWN "" +#define QFILEEXT_PAK ".PAK" +#define QFILEEXT_PK3 ".PK3" +#define QFILEEXT_SIN ".SIN" +#define QFILEEXT_BSP ".BSP" +#define QFILEEXT_MAP ".MAP" +#define QFILEEXT_MDL ".MDL" +#define QFILEEXT_MD2 ".MD2" +#define QFILEEXT_MD3 ".MD3" +#define QFILEEXT_WAL ".WAL" +#define QFILEEXT_WAV ".WAV" +#define QFILEEXT_AAS ".AAS" + +//maximum path length +#ifndef _MAX_PATH + #define _MAX_PATH 1024 +#endif + +//for Sin packs +#define MAX_PAK_FILENAME_LENGTH 120 +#define SINPAKHEADER ( ( 'K' << 24 ) + ( 'A' << 16 ) + ( 'P' << 8 ) + 'S' ) + +typedef struct +{ + char name[MAX_PAK_FILENAME_LENGTH]; + int filepos, filelen; +} dsinpackfile_t; + +typedef struct quakefile_s +{ + char pakfile[_MAX_PATH]; + char filename[_MAX_PATH]; + char origname[_MAX_PATH]; + int zipfile; + int type; + int offset; + int length; + unz_s zipinfo; + struct quakefile_s *next; +} quakefile_t; + +//returns the file extension for the given type +char *QuakeFileTypeExtension( int type ); +//returns the file type for the given extension +int QuakeFileExtensionType( char *extension ); +//return the Quake file type for the given file +int QuakeFileType( char *filename ); +//returns true if the filename complies to the filter +int FileFilter( char *filter, char *filename, int casesensitive ); +//find Quake files using the given filter +quakefile_t *FindQuakeFiles( char *filter ); +//load the given Quake file, returns the length of the file +int LoadQuakeFile( quakefile_t *qf, void **bufferptr ); +//read part of a Quake file into the buffer +int ReadQuakeFile( quakefile_t *qf, void *buffer, int offset, int length ); diff --git a/src/bspc/l_threads.c b/src/bspc/l_threads.c new file mode 100644 index 0000000..f36b5cc --- /dev/null +++ b/src/bspc/l_threads.c @@ -0,0 +1,1528 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: l_threads.c +// Function: multi-threading +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1999-05-14 +// Tab Size: 3 +//=========================================================================== + +#include "l_cmd.h" +#include "l_threads.h" +#include "l_log.h" +#include "l_mem.h" + +#define MAX_THREADS 64 + +//#define THREAD_DEBUG + +int dispatch; +int workcount; +int oldf; +qboolean pacifier; +qboolean threaded; +void ( *workfunction )( int ); + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int GetThreadWork( void ) { + int r; + int f; + + ThreadLock(); + + if ( dispatch == workcount ) { + ThreadUnlock(); + return -1; + } + + f = 10 * dispatch / workcount; + if ( f != oldf ) { + oldf = f; + if ( pacifier ) { + printf( "%i...", f ); + } + } //end if + + r = dispatch; + dispatch++; + ThreadUnlock(); + + return r; +} //end of the function GetThreadWork +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadWorkerFunction( int threadnum ) { + int work; + + while ( 1 ) + { + work = GetThreadWork(); + if ( work == -1 ) { + break; + } +//printf ("thread %i, work %i\n", threadnum, work); + workfunction( work ); + } //end while +} //end of the function ThreadWorkerFunction +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void RunThreadsOnIndividual( int workcnt, qboolean showpacifier, void ( *func )(int) ) { + if ( numthreads == -1 ) { + ThreadSetDefault(); + } + workfunction = func; + RunThreadsOn( workcnt, showpacifier, ThreadWorkerFunction ); +} //end of the function RunThreadsOnIndividual + + +//=================================================================== +// +// WIN32 +// +//=================================================================== + +#if defined( WIN32 ) || defined( _WIN32 ) + +#define USED + +#include + +typedef struct thread_s +{ + HANDLE handle; + int threadid; + int id; + struct thread_s *next; +} thread_t; + +thread_t *firstthread; +thread_t *lastthread; +int currentnumthreads; +int currentthreadid; + +int numthreads = 1; +CRITICAL_SECTION crit; +HANDLE semaphore; +static int enter; +static int numwaitingthreads = 0; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadSetDefault( void ) { + SYSTEM_INFO info; + + if ( numthreads == -1 ) { // not set manually + GetSystemInfo( &info ); + numthreads = info.dwNumberOfProcessors; + if ( numthreads < 1 || numthreads > 32 ) { + numthreads = 1; + } + } //end if + qprintf( "%i threads\n", numthreads ); +} //end of the function ThreadSetDefault +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadLock( void ) { + if ( !threaded ) { + Error( "ThreadLock: !threaded" ); + return; + } //end if + EnterCriticalSection( &crit ); + if ( enter ) { + Error( "Recursive ThreadLock\n" ); + } + enter = 1; +} //end of the function ThreadLock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadUnlock( void ) { + if ( !threaded ) { + Error( "ThreadUnlock: !threaded" ); + return; + } //end if + if ( !enter ) { + Error( "ThreadUnlock without lock\n" ); + } + enter = 0; + LeaveCriticalSection( &crit ); +} //end of the function ThreadUnlock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadSetupLock( void ) { + Log_Print( "Win32 multi-threading\n" ); + InitializeCriticalSection( &crit ); + threaded = true; //Stupid me... forgot this!!! + currentnumthreads = 0; + currentthreadid = 0; +} //end of the function ThreadInitLock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadShutdownLock( void ) { + DeleteCriticalSection( &crit ); + threaded = false; //Stupid me... forgot this!!! +} //end of the function ThreadShutdownLock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadSetupSemaphore( void ) { + semaphore = CreateSemaphore( NULL, 0, 99999999, "bspc" ); +} //end of the function ThreadSetupSemaphore +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadShutdownSemaphore( void ) { +} //end of the function ThreadShutdownSemaphore +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadSemaphoreWait( void ) { + WaitForSingleObject( semaphore, INFINITE ); +} //end of the function ThreadSemaphoreWait +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadSemaphoreIncrease( int count ) { + ReleaseSemaphore( semaphore, count, NULL ); +} //end of the function ThreadSemaphoreIncrease +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )(int) ) { + int threadid[MAX_THREADS]; + HANDLE threadhandle[MAX_THREADS]; + int i; + int start, end; + + Log_Print( "Win32 multi-threading\n" ); + start = I_FloatTime(); + dispatch = 0; + workcount = workcnt; + oldf = -1; + pacifier = showpacifier; + threaded = true; + + if ( numthreads == -1 ) { + ThreadSetDefault(); + } + + if ( numthreads < 1 || numthreads > MAX_THREADS ) { + numthreads = 1; + } + // + // run threads in parallel + // + InitializeCriticalSection( &crit ); + + numwaitingthreads = 0; + + if ( numthreads == 1 ) { // use same thread + func( 0 ); + } //end if + else + { +// printf("starting %d threads\n", numthreads); + for ( i = 0; i < numthreads; i++ ) + { + threadhandle[i] = CreateThread( + NULL, // LPSECURITY_ATTRIBUTES lpsa, + 0, // DWORD cbStack, + (LPTHREAD_START_ROUTINE)func, // LPTHREAD_START_ROUTINE lpStartAddr, + (LPVOID)i, // LPVOID lpvThreadParm, + 0, // DWORD fdwCreate, + &threadid[i] ); +// printf("started thread %d\n", i); + } //end for + + for ( i = 0; i < numthreads; i++ ) + WaitForSingleObject( threadhandle[i], INFINITE ); + } //end else + DeleteCriticalSection( &crit ); + + threaded = false; + end = I_FloatTime(); + if ( pacifier ) { + printf( " (%i)\n", end - start ); + } +} //end of the function RunThreadsOn +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AddThread( void ( *func )(int) ) { + thread_t *thread; + + if ( numthreads == 1 ) { + if ( currentnumthreads >= numthreads ) { + return; + } + currentnumthreads++; + func( -1 ); + currentnumthreads--; + } //end if + else + { + ThreadLock(); + if ( currentnumthreads >= numthreads ) { + ThreadUnlock(); + return; + } //end if + //allocate new thread + thread = GetMemory( sizeof( thread_t ) ); + if ( !thread ) { + Error( "can't allocate memory for thread\n" ); + } + + // + thread->threadid = currentthreadid; + thread->handle = CreateThread( + NULL, // LPSECURITY_ATTRIBUTES lpsa, + 0, // DWORD cbStack, + (LPTHREAD_START_ROUTINE)func, // LPTHREAD_START_ROUTINE lpStartAddr, + (LPVOID) thread->threadid, // LPVOID lpvThreadParm, + 0, // DWORD fdwCreate, + &thread->id ); + + //add the thread to the end of the list + thread->next = NULL; + if ( lastthread ) { + lastthread->next = thread; + } else { firstthread = thread;} + lastthread = thread; + // +#ifdef THREAD_DEBUG + qprintf( "added thread with id %d\n", thread->threadid ); +#endif //THREAD_DEBUG + // + currentnumthreads++; + currentthreadid++; + // + ThreadUnlock(); + } //end else +} //end of the function AddThread +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void RemoveThread( int threadid ) { + thread_t *thread, *last; + + //if a single thread + if ( threadid == -1 ) { + return; + } + // + ThreadLock(); + last = NULL; + for ( thread = firstthread; thread; thread = thread->next ) + { + if ( thread->threadid == threadid ) { + if ( last ) { + last->next = thread->next; + } else { firstthread = thread->next;} + if ( !thread->next ) { + lastthread = last; + } + // + FreeMemory( thread ); + currentnumthreads--; +#ifdef THREAD_DEBUG + qprintf( "removed thread with id %d\n", threadid ); +#endif //THREAD_DEBUG + break; + } //end if + last = thread; + } //end if + if ( !thread ) { + Error( "couldn't find thread with id %d", threadid ); + } + ThreadUnlock(); +} //end of the function RemoveThread +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void WaitForAllThreadsFinished( void ) { + HANDLE handle; + + ThreadLock(); + while ( firstthread ) + { + handle = firstthread->handle; + ThreadUnlock(); + + WaitForSingleObject( handle, INFINITE ); + + ThreadLock(); + } //end while + ThreadUnlock(); +} //end of the function WaitForAllThreadsFinished +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int GetNumThreads( void ) { + return currentnumthreads; +} //end of the function GetNumThreads + +#endif + + +//=================================================================== +// +// OSF1 +// +//=================================================================== + +#if defined( __osf__ ) + +#define USED + +#include + +typedef struct thread_s +{ + pthread_t thread; + int threadid; + int id; + struct thread_s *next; +} thread_t; + +thread_t *firstthread; +thread_t *lastthread; +int currentnumthreads; +int currentthreadid; + +int numthreads = 1; +pthread_mutex_t my_mutex; +pthread_attr_t attrib; +static int enter; +static int numwaitingthreads = 0; + + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadSetDefault( void ) { + if ( numthreads == -1 ) { // not set manually + numthreads = 1; + } //end if + qprintf( "%i threads\n", numthreads ); +} //end of the function ThreadSetDefault +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadLock( void ) { + if ( !threaded ) { + Error( "ThreadLock: !threaded" ); + return; + } //end if + if ( my_mutex ) { + pthread_mutex_lock( my_mutex ); + } //end if + if ( enter ) { + Error( "Recursive ThreadLock\n" ); + } + enter = 1; +} //end of the function ThreadLock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadUnlock( void ) { + if ( !threaded ) { + Error( "ThreadUnlock: !threaded" ); + return; + } //end if + if ( !enter ) { + Error( "ThreadUnlock without lock\n" ); + } + enter = 0; + if ( my_mutex ) { + pthread_mutex_unlock( my_mutex ); + } //end if +} //end of the function ThreadUnlock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadSetupLock( void ) { + pthread_mutexattr_t mattrib; + + Log_Print( "pthread multi-threading\n" ); + + if ( !my_mutex ) { + my_mutex = GetMemory( sizeof( *my_mutex ) ); + if ( pthread_mutexattr_create( &mattrib ) == -1 ) { + Error( "pthread_mutex_attr_create failed" ); + } + if ( pthread_mutexattr_setkind_np( &mattrib, MUTEX_FAST_NP ) == -1 ) { + Error( "pthread_mutexattr_setkind_np failed" ); + } + if ( pthread_mutex_init( my_mutex, mattrib ) == -1 ) { + Error( "pthread_mutex_init failed" ); + } + } + + if ( pthread_attr_create( &attrib ) == -1 ) { + Error( "pthread_attr_create failed" ); + } + if ( pthread_attr_setstacksize( &attrib, 0x100000 ) == -1 ) { + Error( "pthread_attr_setstacksize failed" ); + } + + threaded = true; + currentnumthreads = 0; + currentthreadid = 0; +} //end of the function ThreadInitLock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadShutdownLock( void ) { + threaded = false; +} //end of the function ThreadShutdownLock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )(int) ) { + int i; + pthread_t work_threads[MAX_THREADS]; + pthread_addr_t status; + pthread_attr_t attrib; + pthread_mutexattr_t mattrib; + int start, end; + + Log_Print( "pthread multi-threading\n" ); + + start = I_FloatTime(); + dispatch = 0; + workcount = workcnt; + oldf = -1; + pacifier = showpacifier; + threaded = true; + + if ( numthreads < 1 || numthreads > MAX_THREADS ) { + numthreads = 1; + } + + if ( pacifier ) { + setbuf( stdout, NULL ); + } + + if ( !my_mutex ) { + my_mutex = GetMemory( sizeof( *my_mutex ) ); + if ( pthread_mutexattr_create( &mattrib ) == -1 ) { + Error( "pthread_mutex_attr_create failed" ); + } + if ( pthread_mutexattr_setkind_np( &mattrib, MUTEX_FAST_NP ) == -1 ) { + Error( "pthread_mutexattr_setkind_np failed" ); + } + if ( pthread_mutex_init( my_mutex, mattrib ) == -1 ) { + Error( "pthread_mutex_init failed" ); + } + } + + if ( pthread_attr_create( &attrib ) == -1 ) { + Error( "pthread_attr_create failed" ); + } + if ( pthread_attr_setstacksize( &attrib, 0x100000 ) == -1 ) { + Error( "pthread_attr_setstacksize failed" ); + } + + for ( i = 0 ; i < numthreads ; i++ ) + { + if ( pthread_create( &work_threads[i], attrib + , (pthread_startroutine_t)func, (pthread_addr_t)i ) == -1 ) { + Error( "pthread_create failed" ); + } + } + + for ( i = 0 ; i < numthreads ; i++ ) + { + if ( pthread_join( work_threads[i], &status ) == -1 ) { + Error( "pthread_join failed" ); + } + } + + threaded = false; + + end = I_FloatTime(); + if ( pacifier ) { + printf( " (%i)\n", end - start ); + } +} //end of the function RunThreadsOn +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AddThread( void ( *func )(int) ) { + thread_t *thread; + + if ( numthreads == 1 ) { + if ( currentnumthreads >= numthreads ) { + return; + } + currentnumthreads++; + func( -1 ); + currentnumthreads--; + } //end if + else + { + ThreadLock(); + if ( currentnumthreads >= numthreads ) { + ThreadUnlock(); + return; + } //end if + //allocate new thread + thread = GetMemory( sizeof( thread_t ) ); + if ( !thread ) { + Error( "can't allocate memory for thread\n" ); + } + // + thread->threadid = currentthreadid; + + if ( pthread_create( &thread->thread, attrib, (pthread_startroutine_t)func, (pthread_addr_t)thread->threadid ) == -1 ) { + Error( "pthread_create failed" ); + } + + //add the thread to the end of the list + thread->next = NULL; + if ( lastthread ) { + lastthread->next = thread; + } else { firstthread = thread;} + lastthread = thread; + // +#ifdef THREAD_DEBUG + qprintf( "added thread with id %d\n", thread->threadid ); +#endif //THREAD_DEBUG + // + currentnumthreads++; + currentthreadid++; + // + ThreadUnlock(); + } //end else +} //end of the function AddThread +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void RemoveThread( int threadid ) { + thread_t *thread, *last; + + //if a single thread + if ( threadid == -1 ) { + return; + } + // + ThreadLock(); + last = NULL; + for ( thread = firstthread; thread; thread = thread->next ) + { + if ( thread->threadid == threadid ) { + if ( last ) { + last->next = thread->next; + } else { firstthread = thread->next;} + if ( !thread->next ) { + lastthread = last; + } + // + FreeMemory( thread ); + currentnumthreads--; +#ifdef THREAD_DEBUG + qprintf( "removed thread with id %d\n", threadid ); +#endif //THREAD_DEBUG + break; + } //end if + last = thread; + } //end if + if ( !thread ) { + Error( "couldn't find thread with id %d", threadid ); + } + ThreadUnlock(); +} //end of the function RemoveThread +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void WaitForAllThreadsFinished( void ) { + pthread_t *thread; + pthread_addr_t status; + + ThreadLock(); + while ( firstthread ) + { + thread = &firstthread->thread; + ThreadUnlock(); + + if ( pthread_join( *thread, &status ) == -1 ) { + Error( "pthread_join failed" ); + } + + ThreadLock(); + } //end while + ThreadUnlock(); +} //end of the function WaitForAllThreadsFinished +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int GetNumThreads( void ) { + return currentnumthreads; +} //end of the function GetNumThreads + +#endif + +//=================================================================== +// +// LINUX +// +//=================================================================== + +#if defined( LINUX ) + +#define USED + +#include +#include + +typedef struct thread_s +{ + pthread_t thread; + int threadid; + int id; + struct thread_s *next; +} thread_t; + +thread_t *firstthread; +thread_t *lastthread; +int currentnumthreads; +int currentthreadid; + +int numthreads = 1; +pthread_mutex_t my_mutex = PTHREAD_MUTEX_INITIALIZER; +pthread_attr_t attrib; +sem_t semaphore; +static int enter; +static int numwaitingthreads = 0; + + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadSetDefault( void ) { + if ( numthreads == -1 ) { // not set manually + numthreads = 1; + } //end if + qprintf( "%i threads\n", numthreads ); +} //end of the function ThreadSetDefault +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadLock( void ) { + if ( !threaded ) { + Error( "ThreadLock: !threaded" ); + return; + } //end if + pthread_mutex_lock( &my_mutex ); + if ( enter ) { + Error( "Recursive ThreadLock\n" ); + } + enter = 1; +} //end of the function ThreadLock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadUnlock( void ) { + if ( !threaded ) { + Error( "ThreadUnlock: !threaded" ); + return; + } //end if + if ( !enter ) { + Error( "ThreadUnlock without lock\n" ); + } + enter = 0; + pthread_mutex_unlock( &my_mutex ); +} //end of the function ThreadUnlock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadSetupLock( void ) { + pthread_mutexattr_t mattrib; + + Log_Print( "pthread multi-threading\n" ); + + threaded = true; + currentnumthreads = 0; + currentthreadid = 0; +} //end of the function ThreadInitLock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadShutdownLock( void ) { + threaded = false; +} //end of the function ThreadShutdownLock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadSetupSemaphore( void ) { + sem_init( &semaphore, 0, 0 ); +} //end of the function ThreadSetupSemaphore +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadShutdownSemaphore( void ) { + sem_destroy( &semaphore ); +} //end of the function ThreadShutdownSemaphore +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadSemaphoreWait( void ) { + sem_wait( &semaphore ); +} //end of the function ThreadSemaphoreWait +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadSemaphoreIncrease( int count ) { + int i; + + for ( i = 0; i < count; i++ ) + { + sem_post( &semaphore ); + } //end for +} //end of the function ThreadSemaphoreIncrease +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )(int) ) { + int i; + pthread_t work_threads[MAX_THREADS]; + void *pthread_return; + pthread_attr_t attrib; + pthread_mutexattr_t mattrib; + int start, end; + + Log_Print( "pthread multi-threading\n" ); + + start = I_FloatTime(); + dispatch = 0; + workcount = workcnt; + oldf = -1; + pacifier = showpacifier; + threaded = true; + + if ( numthreads < 1 || numthreads > MAX_THREADS ) { + numthreads = 1; + } + + if ( pacifier ) { + setbuf( stdout, NULL ); + } + + for ( i = 0 ; i < numthreads ; i++ ) + { + if ( pthread_create( &work_threads[i], NULL, (void *)func, (void *)i ) == -1 ) { + Error( "pthread_create failed" ); + } + } + + for ( i = 0 ; i < numthreads ; i++ ) + { + if ( pthread_join( work_threads[i], &pthread_return ) == -1 ) { + Error( "pthread_join failed" ); + } + } + + threaded = false; + + end = I_FloatTime(); + if ( pacifier ) { + printf( " (%i)\n", end - start ); + } +} //end of the function RunThreadsOn +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AddThread( void ( *func )(int) ) { + thread_t *thread; + + if ( numthreads == 1 ) { + if ( currentnumthreads >= numthreads ) { + return; + } + currentnumthreads++; + func( -1 ); + currentnumthreads--; + } //end if + else + { + ThreadLock(); + if ( currentnumthreads >= numthreads ) { + ThreadUnlock(); + return; + } //end if + //allocate new thread + thread = GetMemory( sizeof( thread_t ) ); + if ( !thread ) { + Error( "can't allocate memory for thread\n" ); + } + // + thread->threadid = currentthreadid; + + if ( pthread_create( &thread->thread, NULL, (void *)func, (void *)thread->threadid ) == -1 ) { + Error( "pthread_create failed" ); + } + + //add the thread to the end of the list + thread->next = NULL; + if ( lastthread ) { + lastthread->next = thread; + } else { firstthread = thread;} + lastthread = thread; + // +#ifdef THREAD_DEBUG + qprintf( "added thread with id %d\n", thread->threadid ); +#endif //THREAD_DEBUG + // + currentnumthreads++; + currentthreadid++; + // + ThreadUnlock(); + } //end else +} //end of the function AddThread +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void RemoveThread( int threadid ) { + thread_t *thread, *last; + + //if a single thread + if ( threadid == -1 ) { + return; + } + // + ThreadLock(); + last = NULL; + for ( thread = firstthread; thread; thread = thread->next ) + { + if ( thread->threadid == threadid ) { + if ( last ) { + last->next = thread->next; + } else { firstthread = thread->next;} + if ( !thread->next ) { + lastthread = last; + } + // + FreeMemory( thread ); + currentnumthreads--; +#ifdef THREAD_DEBUG + qprintf( "removed thread with id %d\n", threadid ); +#endif //THREAD_DEBUG + break; + } //end if + last = thread; + } //end if + if ( !thread ) { + Error( "couldn't find thread with id %d", threadid ); + } + ThreadUnlock(); +} //end of the function RemoveThread +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void WaitForAllThreadsFinished( void ) { + pthread_t *thread; + void *pthread_return; + + ThreadLock(); + while ( firstthread ) + { + thread = &firstthread->thread; + ThreadUnlock(); + + if ( pthread_join( *thread, &pthread_return ) == -1 ) { + Error( "pthread_join failed" ); + } + + ThreadLock(); + } //end while + ThreadUnlock(); +} //end of the function WaitForAllThreadsFinished +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int GetNumThreads( void ) { + return currentnumthreads; +} //end of the function GetNumThreads + +#endif //LINUX + + +//=================================================================== +// +// IRIX +// +//=================================================================== + +#ifdef _MIPS_ISA + +#define USED + +#include +#include +#include +#include + +typedef struct thread_s +{ + int threadid; + int id; + struct thread_s *next; +} thread_t; + +thread_t *firstthread; +thread_t *lastthread; +int currentnumthreads; +int currentthreadid; + +int numthreads = 1; +static int enter; +static int numwaitingthreads = 0; + +abilock_t lck; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadSetDefault( void ) { + if ( numthreads == -1 ) { + numthreads = prctl( PR_MAXPPROCS ); + } + printf( "%i threads\n", numthreads ); +//@@ + usconfig( CONF_INITUSERS, numthreads ); +} //end of the function ThreadSetDefault +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadLock( void ) { + spin_lock( &lck ); +} //end of the function ThreadLock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadUnlock( void ) { + release_lock( &lck ); +} //end of the function ThreadUnlock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadSetupLock( void ) { + init_lock( &lck ); + + Log_Print( "IRIX multi-threading\n" ); + + threaded = true; + currentnumthreads = 0; + currentthreadid = 0; +} //end of the function ThreadInitLock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadShutdownLock( void ) { + threaded = false; +} //end of the function ThreadShutdownLock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )(int) ) { + int i; + int pid[MAX_THREADS]; + int start, end; + + start = I_FloatTime(); + dispatch = 0; + workcount = workcnt; + oldf = -1; + pacifier = showpacifier; + threaded = true; + + if ( numthreads < 1 || numthreads > MAX_THREADS ) { + numthreads = 1; + } + + if ( pacifier ) { + setbuf( stdout, NULL ); + } + + init_lock( &lck ); + + for ( i = 0 ; i < numthreads - 1 ; i++ ) + { + pid[i] = sprocsp( ( void( * ) ( void *, size_t ) )func, PR_SALL, (void *)i + , NULL, 0x100000 ); +// pid[i] = sprocsp ( (void (*)(void *, size_t))func, PR_SALL, (void *)i +// , NULL, 0x80000); + if ( pid[i] == -1 ) { + perror( "sproc" ); + Error( "sproc failed" ); + } + } + + func( i ); + + for ( i = 0 ; i < numthreads - 1 ; i++ ) + wait( NULL ); + + threaded = false; + + end = I_FloatTime(); + if ( pacifier ) { + printf( " (%i)\n", end - start ); + } +} //end of the function RunThreadsOn +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AddThread( void ( *func )(int) ) { + thread_t *thread; + + if ( numthreads == 1 ) { + if ( currentnumthreads >= numthreads ) { + return; + } + currentnumthreads++; + func( -1 ); + currentnumthreads--; + } //end if + else + { + ThreadLock(); + if ( currentnumthreads >= numthreads ) { + ThreadUnlock(); + return; + } //end if + //allocate new thread + thread = GetMemory( sizeof( thread_t ) ); + if ( !thread ) { + Error( "can't allocate memory for thread\n" ); + } + // + thread->threadid = currentthreadid; + + thread->id = sprocsp( ( void( * ) ( void *, size_t ) )func, PR_SALL, (void *)thread->threadid, NULL, 0x100000 ); + if ( thread->id == -1 ) { + perror( "sproc" ); + Error( "sproc failed" ); + } + + //add the thread to the end of the list + thread->next = NULL; + if ( lastthread ) { + lastthread->next = thread; + } else { firstthread = thread;} + lastthread = thread; + // +#ifdef THREAD_DEBUG + qprintf( "added thread with id %d\n", thread->threadid ); +#endif //THREAD_DEBUG + // + currentnumthreads++; + currentthreadid++; + // + ThreadUnlock(); + } //end else +} //end of the function AddThread +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void RemoveThread( int threadid ) { + thread_t *thread, *last; + + //if a single thread + if ( threadid == -1 ) { + return; + } + // + ThreadLock(); + last = NULL; + for ( thread = firstthread; thread; thread = thread->next ) + { + if ( thread->threadid == threadid ) { + if ( last ) { + last->next = thread->next; + } else { firstthread = thread->next;} + if ( !thread->next ) { + lastthread = last; + } + // + FreeMemory( thread ); + currentnumthreads--; +#ifdef THREAD_DEBUG + qprintf( "removed thread with id %d\n", threadid ); +#endif //THREAD_DEBUG + break; + } //end if + last = thread; + } //end if + if ( !thread ) { + Error( "couldn't find thread with id %d", threadid ); + } + ThreadUnlock(); +} //end of the function RemoveThread +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void WaitForAllThreadsFinished( void ) { + ThreadLock(); + while ( firstthread ) + { + ThreadUnlock(); + + //wait (NULL); + + ThreadLock(); + } //end while + ThreadUnlock(); +} //end of the function WaitForAllThreadsFinished +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int GetNumThreads( void ) { + return currentnumthreads; +} //end of the function GetNumThreads + +#endif //_MIPS_ISA + + +//======================================================================= +// +// SINGLE THREAD +// +//======================================================================= + +#ifndef USED + +int numthreads = 1; +int currentnumthreads = 0; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadSetDefault( void ) { + numthreads = 1; +} //end of the function ThreadSetDefault +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadLock( void ) { +} //end of the function ThreadLock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadUnlock( void ) { +} //end of the function ThreadUnlock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadSetupLock( void ) { + Log_Print( "no multi-threading\n" ); +} //end of the function ThreadInitLock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadShutdownLock( void ) { +} //end of the function ThreadShutdownLock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadSetupSemaphore( void ) { +} //end of the function ThreadSetupSemaphore +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadShutdownSemaphore( void ) { +} //end of the function ThreadShutdownSemaphore +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadSemaphoreWait( void ) { +} //end of the function ThreadSemaphoreWait +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ThreadSemaphoreIncrease( int count ) { +} //end of the function ThreadSemaphoreIncrease +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )(int) ) { + int start, end; + + Log_Print( "no multi-threading\n" ); + dispatch = 0; + workcount = workcnt; + oldf = -1; + pacifier = showpacifier; + start = I_FloatTime(); +#ifdef NeXT + if ( pacifier ) { + setbuf( stdout, NULL ); + } +#endif + func( 0 ); + + end = I_FloatTime(); + if ( pacifier ) { + printf( " (%i)\n", end - start ); + } +} //end of the function RunThreadsOn +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AddThread( void ( *func )(int) ) { + if ( currentnumthreads >= numthreads ) { + return; + } + currentnumthreads++; + func( -1 ); + currentnumthreads--; +} //end of the function AddThread +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void RemoveThread( int threadid ) { +} //end of the function RemoveThread +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void WaitForAllThreadsFinished( void ) { +} //end of the function WaitForAllThreadsFinished +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int GetNumThreads( void ) { + return currentnumthreads; +} //end of the function GetNumThreads + +#endif //USED diff --git a/src/bspc/l_threads.h b/src/bspc/l_threads.h new file mode 100644 index 0000000..70ad0fa --- /dev/null +++ b/src/bspc/l_threads.h @@ -0,0 +1,52 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +extern int numthreads; + +void ThreadSetDefault( void ); +int GetThreadWork( void ); +void RunThreadsOnIndividual( int workcnt, qboolean showpacifier, void ( *func )( int ) ); +void RunThreadsOn( int workcnt, qboolean showpacifier, void ( *func )( int ) ); + +//mutex +void ThreadSetupLock( void ); +void ThreadShutdownLock( void ); +void ThreadLock( void ); +void ThreadUnlock( void ); +//semaphore +void ThreadSetupSemaphore( void ); +void ThreadShutdownSemaphore( void ); +void ThreadSemaphoreWait( void ); +void ThreadSemaphoreIncrease( int count ); +//add/remove threads +void AddThread( void ( *func )( int ) ); +void RemoveThread( int threadid ); +void WaitForAllThreadsFinished( void ); +int GetNumThreads( void ); + diff --git a/src/bspc/l_utils.c b/src/bspc/l_utils.c new file mode 100644 index 0000000..596529d --- /dev/null +++ b/src/bspc/l_utils.c @@ -0,0 +1,267 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: l_utils.c +// Function: several utils +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-31 +// Tab Size: 3 +//=========================================================================== + +#include +//#ifndef BOTLIB +//#define BOTLIB +//#endif //BOTLIB + +#ifdef BOTLIB +#include "q_shared.h" +#include "qfiles.h" +#include "botlib.h" +#include "l_log.h" +#include "l_libvar.h" +#include "l_memory.h" +//#include "l_utils.h" +#include "be_interface.h" +#else //BOTLIB +#include "qbsp.h" +#include "l_mem.h" +#endif //BOTLIB + +#ifdef BOTLIB +//======================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//======================================================================== +void Vector2Angles( vec3_t value1, vec3_t angles ) { + float forward; + float yaw, pitch; + + if ( value1[1] == 0 && value1[0] == 0 ) { + yaw = 0; + if ( value1[2] > 0 ) { + pitch = 90; + } else { pitch = 270;} + } //end if + else + { + yaw = (int) ( atan2( value1[1], value1[0] ) * 180 / M_PI ); + if ( yaw < 0 ) { + yaw += 360; + } + + forward = sqrt( value1[0] * value1[0] + value1[1] * value1[1] ); + pitch = (int) ( atan2( value1[2], forward ) * 180 / M_PI ); + if ( pitch < 0 ) { + pitch += 360; + } + } //end else + + angles[PITCH] = -pitch; + angles[YAW] = yaw; + angles[ROLL] = 0; +} //end of the function Vector2Angles +#endif //BOTLIB +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ConvertPath( char *path ) { + while ( *path ) + { + if ( *path == '/' || *path == '\\' ) { + *path = PATHSEPERATOR_CHAR; + } + path++; + } //end while +} //end of the function ConvertPath +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AppendPathSeperator( char *path, int length ) { + int pathlen = strlen( path ); + + if ( strlen( path ) && length - pathlen > 1 && path[pathlen - 1] != '/' && path[pathlen - 1] != '\\' ) { + path[pathlen] = PATHSEPERATOR_CHAR; + path[pathlen + 1] = '\0'; + } //end if +} //end of the function AppenPathSeperator +//=========================================================================== +// returns pointer to file handle +// sets offset to and length of 'filename' in the pak file +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean FindFileInPak( char *pakfile, char *filename, foundfile_t *file ) { + FILE *fp; + dpackheader_t packheader; + dpackfile_t *packfiles; + int numdirs, i; + char path[MAX_PATH]; + + //open the pak file + fp = fopen( pakfile, "rb" ); + if ( !fp ) { + return false; + } //end if + //read pak header, check for valid pak id and seek to the dir entries + if ( ( fread( &packheader, 1, sizeof( dpackheader_t ), fp ) != sizeof( dpackheader_t ) ) + || ( packheader.ident != IDPAKHEADER ) + || ( fseek( fp, LittleLong( packheader.dirofs ), SEEK_SET ) ) + ) { + fclose( fp ); + return false; + } //end if + //number of dir entries in the pak file + numdirs = LittleLong( packheader.dirlen ) / sizeof( dpackfile_t ); + packfiles = (dpackfile_t *) GetMemory( numdirs * sizeof( dpackfile_t ) ); + //read the dir entry + if ( fread( packfiles, sizeof( dpackfile_t ), numdirs, fp ) != numdirs ) { + fclose( fp ); + FreeMemory( packfiles ); + return false; + } //end if + fclose( fp ); + // + strcpy( path, filename ); + ConvertPath( path ); + //find the dir entry in the pak file + for ( i = 0; i < numdirs; i++ ) + { + //convert the dir entry name + ConvertPath( packfiles[i].name ); + //compare the dir entry name with the filename + if ( Q_strcasecmp( packfiles[i].name, path ) == 0 ) { + strcpy( file->filename, pakfile ); + file->offset = LittleLong( packfiles[i].filepos ); + file->length = LittleLong( packfiles[i].filelen ); + FreeMemory( packfiles ); + return true; + } //end if + } //end for + FreeMemory( packfiles ); + return false; +} //end of the function FindFileInPak +//=========================================================================== +// find a Quake2 file +// returns full path in 'filename' +// sets offset and length of the file +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean FindQuakeFile2( char *basedir, char *gamedir, char *filename, foundfile_t *file ) { + int dir, i; + //NOTE: 3 is necessary (LCC bug???) + char gamedirs[3][MAX_PATH] = {"","",""}; + char filedir[MAX_PATH] = ""; + + // + if ( gamedir ) { + strncpy( gamedirs[0], gamedir, MAX_PATH ); + } + strncpy( gamedirs[1], "baseq2", MAX_PATH ); + // + //find the file in the two game directories + for ( dir = 0; dir < 2; dir++ ) + { + //check if the file is in a directory + filedir[0] = 0; + if ( basedir && strlen( basedir ) ) { + strncpy( filedir, basedir, MAX_PATH ); + AppendPathSeperator( filedir, MAX_PATH ); + } //end if + if ( strlen( gamedirs[dir] ) ) { + strncat( filedir, gamedirs[dir], MAX_PATH - strlen( filedir ) ); + AppendPathSeperator( filedir, MAX_PATH ); + } //end if + strncat( filedir, filename, MAX_PATH - strlen( filedir ) ); + ConvertPath( filedir ); + Log_Write( "accessing %s", filedir ); + if ( !access( filedir, 0x04 ) ) { + strcpy( file->filename, filedir ); + file->length = 0; + file->offset = 0; + return true; + } //end if + //check if the file is in a pak?.pak + for ( i = 0; i < 10; i++ ) + { + filedir[0] = 0; + if ( basedir && strlen( basedir ) ) { + strncpy( filedir, basedir, MAX_PATH ); + AppendPathSeperator( filedir, MAX_PATH ); + } //end if + if ( strlen( gamedirs[dir] ) ) { + strncat( filedir, gamedirs[dir], MAX_PATH - strlen( filedir ) ); + AppendPathSeperator( filedir, MAX_PATH ); + } //end if + // TTimo: huuuuh .. I suppose this means there needs to be two \0\0 at the end? + //sprintf(&filedir[strlen(filedir)], "pak%d.pak\0", i); + // doing it more 'clean' + sprintf( &filedir[strlen( filedir )], "pak%d.pak ", i ); + filedir[strlen( filedir ) - 1] = '\0'; + if ( !access( filedir, 0x04 ) ) { + Log_Write( "searching %s in %s", filename, filedir ); + if ( FindFileInPak( filedir, filename, file ) ) { + return true; + } + } //end if + } //end for + } //end for + file->offset = 0; + file->length = 0; + return false; +} //end of the function FindQuakeFile2 +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef BOTLIB +qboolean FindQuakeFile( char *filename, foundfile_t *file ) { + return FindQuakeFile2( LibVarGetString( "basedir" ), + LibVarGetString( "gamedir" ), filename, file ); +} //end of the function FindQuakeFile +#else //BOTLIB +qboolean FindQuakeFile( char *basedir, char *gamedir, char *filename, foundfile_t *file ) { + return FindQuakeFile2( basedir, gamedir, filename, file ); +} //end of the function FindQuakeFile +#endif //BOTLIB diff --git a/src/bspc/l_utils.h b/src/bspc/l_utils.h new file mode 100644 index 0000000..4b9949e --- /dev/null +++ b/src/bspc/l_utils.h @@ -0,0 +1,94 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: l_utils.h +// Function: several utils +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-31 +// Tab Size: 3 +//=========================================================================== + +#ifndef MAX_PATH + #define MAX_PATH 64 +#endif + +#ifndef PATH_SEPERATORSTR + #if defined( WIN32 ) | defined( _WIN32 ) | defined( __NT__ ) | defined( __WINDOWS__ ) | defined( __WINDOWS_386__ ) + #define PATHSEPERATOR_STR "\\" + #else + #define PATHSEPERATOR_STR "/" + #endif +#endif +#ifndef PATH_SEPERATORCHAR + #if defined( WIN32 ) | defined( _WIN32 ) | defined( __NT__ ) | defined( __WINDOWS__ ) | defined( __WINDOWS_386__ ) + #define PATHSEPERATOR_CHAR '\\' + #else + #define PATHSEPERATOR_CHAR '/' + #endif +#endif + +//random in the range [0, 1] +#define random() ( ( rand() & 0x7fff ) / ( (float)0x7fff ) ) +//random in the range [-1, 1] +#define crandom() ( 2.0 * ( random() - 0.5 ) ) +//min and max +#define Maximum( x,y ) ( x > y ? x : y ) +#define Minimum( x,y ) ( x < y ? x : y ) +//absolute value +#define FloatAbs( x ) ( *(float *) &( ( *(int *) &( x ) ) & 0x7FFFFFFF ) ) +#define IntAbs( x ) ( ~( x ) ) +//coordinates +#define _X 0 +#define _Y 1 +#define _Z 2 + +typedef struct foundfile_s +{ + int offset; + int length; + char filename[MAX_PATH]; //screw LCC, array must be at end of struct +} foundfile_t; + +void Vector2Angles( vec3_t value1, vec3_t angles ); +//set the correct path seperators +void ConvertPath( char *path ); +//append a path seperator to the given path not exceeding the length +void AppendPathSeperator( char *path, int length ); +//find a file in a pak file +qboolean FindFileInPak( char *pakfile, char *filename, foundfile_t *file ); +//find a quake file +#ifdef BOTLIB +qboolean FindQuakeFile( char *filename, foundfile_t *file ); +#else //BOTLIB +qboolean FindQuakeFile( char *basedir, char *gamedir, char *filename, foundfile_t *file ); +#endif //BOTLIB + + + diff --git a/src/bspc/leakfile.c b/src/bspc/leakfile.c new file mode 100644 index 0000000..4d500ba --- /dev/null +++ b/src/bspc/leakfile.c @@ -0,0 +1,108 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +#include "qbsp.h" + +/* +============================================================================== + +LEAF FILE GENERATION + +Save out name.line for qe3 to read +============================================================================== +*/ + + +/* +============= +LeakFile + +Finds the shortest possible chain of portals +that leads from the outside leaf to a specifically +occupied leaf +============= +*/ +void LeakFile( tree_t *tree ) { + vec3_t mid; + FILE *linefile; + char filename[1024]; + node_t *node; + int count; + + if ( !tree->outside_node.occupied ) { + return; + } + + qprintf( "--- LeakFile ---\n" ); + + // + // write the points to the file + // + sprintf( filename, "%s.lin", source ); + qprintf( "%s\n", filename ); + linefile = fopen( filename, "w" ); + if ( !linefile ) { + Error( "Couldn't open %s\n", filename ); + } + + count = 0; + node = &tree->outside_node; + while ( node->occupied > 1 ) + { + int next; + portal_t *p, *nextportal = NULL; // TTimo: init + node_t *nextnode = NULL; // TTimo: init + int s; + + // find the best portal exit + next = node->occupied; + for ( p = node->portals ; p ; p = p->next[!s] ) + { + s = ( p->nodes[0] == node ); + if ( p->nodes[s]->occupied + && p->nodes[s]->occupied < next ) { + nextportal = p; + nextnode = p->nodes[s]; + next = nextnode->occupied; + } + } + node = nextnode; + WindingCenter( nextportal->winding, mid ); + fprintf( linefile, "%f %f %f\n", mid[0], mid[1], mid[2] ); + count++; + } + // add the occupant center + GetVectorForKey( node->occupant, "origin", mid ); + + fprintf( linefile, "%f %f %f\n", mid[0], mid[1], mid[2] ); + qprintf( "%5i point linefile\n", count + 1 ); + + fclose( linefile ); +} + diff --git a/src/bspc/map.c b/src/bspc/map.c new file mode 100644 index 0000000..8e11d31 --- /dev/null +++ b/src/bspc/map.c @@ -0,0 +1,1360 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: map.c +// Function: map loading and writing +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-03 +// Tab Size: 3 +//=========================================================================== + +#include "qbsp.h" +#include "l_bsp_hl.h" +#include "l_bsp_q1.h" +#include "l_bsp_q2.h" +#include "l_bsp_q3.h" +#include "l_bsp_sin.h" +#include "l_mem.h" +#include "../botlib/aasfile.h" //aas_bbox_t +#include "aas_store.h" //AAS_MAX_BBOXES +#include "aas_cfg.h" + +// TTimo: messy... +#define stricmp strcasecmp + +#define Sign( x ) ( x < 0 ? 1 : 0 ) + +int nummapbrushes; +mapbrush_t mapbrushes[MAX_MAPFILE_BRUSHES]; + +int nummapbrushsides; +side_t brushsides[MAX_MAPFILE_BRUSHSIDES]; +brush_texture_t side_brushtextures[MAX_MAPFILE_BRUSHSIDES]; + +int nummapplanes; +plane_t mapplanes[MAX_MAPFILE_PLANES]; +int mapplaneusers[MAX_MAPFILE_PLANES]; + +#define PLANE_HASHES 1024 +plane_t *planehash[PLANE_HASHES]; +vec3_t map_mins, map_maxs; + +#ifdef SIN +textureref_t side_newrefs[MAX_MAPFILE_BRUSHSIDES]; +#endif + +map_texinfo_t map_texinfo[MAX_MAPFILE_TEXINFO]; +int map_numtexinfo; +int loadedmaptype; //loaded map type + +// undefine to make plane finding use linear sort +#define USE_HASHING + +int c_boxbevels; +int c_edgebevels; +int c_areaportals; +int c_clipbrushes; +int c_squattbrushes; +int c_writtenbrushes; + +/* +============================================================================= + +PLANE FINDING + +============================================================================= +*/ + + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int PlaneSignBits( vec3_t normal ) { + int i, signbits; + + signbits = 0; + for ( i = 2; i >= 0; i-- ) + { + signbits = ( signbits << 1 ) + Sign( normal[i] ); + } //end for + return signbits; +} //end of the function PlaneSignBits +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int PlaneTypeForNormal( vec3_t normal ) { + vec_t ax, ay, az; + +// NOTE: should these have an epsilon around 1.0? + if ( normal[0] == 1.0 || normal[0] == -1.0 ) { + return PLANE_X; + } + if ( normal[1] == 1.0 || normal[1] == -1.0 ) { + return PLANE_Y; + } + if ( normal[2] == 1.0 || normal[2] == -1.0 ) { + return PLANE_Z; + } + + ax = fabs( normal[0] ); + ay = fabs( normal[1] ); + az = fabs( normal[2] ); + + if ( ax >= ay && ax >= az ) { + return PLANE_ANYX; + } + if ( ay >= ax && ay >= az ) { + return PLANE_ANYY; + } + return PLANE_ANYZ; +} //end of the function PlaneTypeForNormal +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== + +// MrE: use the same epsilons as q3map! + +//ME NOTE: changed from 0.00001 +#define NORMAL_EPSILON 0.00001 +//ME NOTE: changed from 0.01 +#define DIST_EPSILON 0.01 + +qboolean PlaneEqual( plane_t *p, vec3_t normal, vec_t dist ) { +#if 1 + if ( + fabs( p->normal[0] - normal[0] ) < NORMAL_EPSILON + && fabs( p->normal[1] - normal[1] ) < NORMAL_EPSILON + && fabs( p->normal[2] - normal[2] ) < NORMAL_EPSILON + && fabs( p->dist - dist ) < DIST_EPSILON ) { + return true; + } +#else + if ( p->normal[0] == normal[0] + && p->normal[1] == normal[1] + && p->normal[2] == normal[2] + && p->dist == dist ) { + return true; + } +#endif + return false; +} //end of the function PlaneEqual +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AddPlaneToHash( plane_t *p ) { + int hash; + + hash = (int)fabs( p->dist ) / 8; + hash &= ( PLANE_HASHES - 1 ); + + p->hash_chain = planehash[hash]; + planehash[hash] = p; +} //end of the function AddPlaneToHash +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int CreateNewFloatPlane( vec3_t normal, vec_t dist ) { + plane_t *p, temp; + + if ( VectorLength( normal ) < 0.5 ) { + Error( "FloatPlane: bad normal" ); + } + // create a new plane + if ( nummapplanes + 2 > MAX_MAPFILE_PLANES ) { + Error( "MAX_MAPFILE_PLANES" ); + } + + p = &mapplanes[nummapplanes]; + VectorCopy( normal, p->normal ); + p->dist = dist; + p->type = ( p + 1 )->type = PlaneTypeForNormal( p->normal ); + p->signbits = PlaneSignBits( p->normal ); + + VectorSubtract( vec3_origin, normal, ( p + 1 )->normal ); + ( p + 1 )->dist = -dist; + ( p + 1 )->signbits = PlaneSignBits( ( p + 1 )->normal ); + + nummapplanes += 2; + + // allways put axial planes facing positive first + if ( p->type < 3 ) { + if ( p->normal[0] < 0 || p->normal[1] < 0 || p->normal[2] < 0 ) { + // flip order + temp = *p; + *p = *( p + 1 ); + *( p + 1 ) = temp; + + AddPlaneToHash( p ); + AddPlaneToHash( p + 1 ); + return nummapplanes - 1; + } + } + + AddPlaneToHash( p ); + AddPlaneToHash( p + 1 ); + return nummapplanes - 2; +} //end of the function CreateNewFloatPlane +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void SnapVector( vec3_t normal ) { + int i; + + for ( i = 0 ; i < 3 ; i++ ) + { + if ( fabs( normal[i] - 1 ) < NORMAL_EPSILON ) { + VectorClear( normal ); + normal[i] = 1; + break; + } + if ( fabs( normal[i] - -1 ) < NORMAL_EPSILON ) { + VectorClear( normal ); + normal[i] = -1; + break; + } + } +} //end of the function SnapVector +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void SnapPlane( vec3_t normal, vec_t *dist ) { + SnapVector( normal ); + + if ( fabs( *dist - Q_rint( *dist ) ) < DIST_EPSILON ) { + *dist = Q_rint( *dist ); + } +} //end of the function SnapPlane +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifndef USE_HASHING +int FindFloatPlane( vec3_t normal, vec_t dist ) { + int i; + plane_t *p; + + SnapPlane( normal, &dist ); + for ( i = 0, p = mapplanes; i < nummapplanes; i++, p++ ) + { + if ( PlaneEqual( p, normal, dist ) ) { + mapplaneusers[i]++; + return i; + } //end if + } //end for + i = CreateNewFloatPlane( normal, dist ); + mapplaneusers[i]++; + return i; +} //end of the function FindFloatPlane +#else +int FindFloatPlane( vec3_t normal, vec_t dist ) { + int i; + plane_t *p; + int hash, h; + + SnapPlane( normal, &dist ); + hash = (int)fabs( dist ) / 8; + hash &= ( PLANE_HASHES - 1 ); + + // search the border bins as well + for ( i = -1; i <= 1; i++ ) + { + h = ( hash + i ) & ( PLANE_HASHES - 1 ); + for ( p = planehash[h]; p; p = p->hash_chain ) + { + if ( PlaneEqual( p, normal, dist ) ) { + mapplaneusers[p - mapplanes]++; + return p - mapplanes; + } //end if + } //end for + } //end for + i = CreateNewFloatPlane( normal, dist ); + mapplaneusers[i]++; + return i; +} //end of the function FindFloatPlane +#endif +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int PlaneFromPoints( int *p0, int *p1, int *p2 ) { + vec3_t t1, t2, normal; + vec_t dist; + + VectorSubtract( p0, p1, t1 ); + VectorSubtract( p2, p1, t2 ); + CrossProduct( t1, t2, normal ); + VectorNormalize( normal ); + + dist = DotProduct( p0, normal ); + + return FindFloatPlane( normal, dist ); +} //end of the function PlaneFromPoints +//=========================================================================== +// Adds any additional planes necessary to allow the brush to be expanded +// against axial bounding boxes +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AddBrushBevels( mapbrush_t *b ) { + int axis, dir; + int i, j, k, l, order; + side_t sidetemp; + brush_texture_t tdtemp; +#ifdef SIN + textureref_t trtemp; +#endif + side_t *s, *s2; + vec3_t normal; + float dist; + winding_t *w, *w2; + vec3_t vec, vec2; + float d; + + // + // add the axial planes + // + order = 0; + for ( axis = 0 ; axis < 3 ; axis++ ) + { + for ( dir = -1 ; dir <= 1 ; dir += 2, order++ ) + { + // see if the plane is allready present + for ( i = 0, s = b->original_sides ; i < b->numsides ; i++,s++ ) + { + if ( mapplanes[s->planenum].normal[axis] == dir ) { + break; + } + } + + if ( i == b->numsides ) { // add a new side + if ( nummapbrushsides == MAX_MAP_BRUSHSIDES ) { + Error( "MAX_MAP_BRUSHSIDES" ); + } + nummapbrushsides++; + b->numsides++; + VectorClear( normal ); + normal[axis] = dir; + if ( dir == 1 ) { + dist = b->maxs[axis]; + } else { + dist = -b->mins[axis]; + } + s->planenum = FindFloatPlane( normal, dist ); + s->texinfo = b->original_sides[0].texinfo; +#ifdef SIN + s->lightinfo = b->original_sides[0].lightinfo; +#endif + s->contents = b->original_sides[0].contents; + s->flags |= SFL_BEVEL; + c_boxbevels++; + } + + // if the plane is not in it canonical order, swap it + if ( i != order ) { + sidetemp = b->original_sides[order]; + b->original_sides[order] = b->original_sides[i]; + b->original_sides[i] = sidetemp; + + j = b->original_sides - brushsides; + tdtemp = side_brushtextures[j + order]; + side_brushtextures[j + order] = side_brushtextures[j + i]; + side_brushtextures[j + i] = tdtemp; + +#ifdef SIN + trtemp = side_newrefs[j + order]; + side_newrefs[j + order] = side_newrefs[j + i]; + side_newrefs[j + i] = trtemp; +#endif + } + } + } + + // + // add the edge bevels + // + if ( b->numsides == 6 ) { + return; // pure axial + + } + // test the non-axial plane edges + for ( i = 6 ; i < b->numsides ; i++ ) + { + s = b->original_sides + i; + w = s->winding; + if ( !w ) { + continue; + } + for ( j = 0 ; j < w->numpoints ; j++ ) + { + k = ( j + 1 ) % w->numpoints; + VectorSubtract( w->p[j], w->p[k], vec ); + if ( VectorNormalize( vec ) < 0.5 ) { + continue; + } + SnapVector( vec ); + for ( k = 0 ; k < 3 ; k++ ) + if ( vec[k] == -1 || vec[k] == 1 ) { + break; + } // axial + if ( k != 3 ) { + continue; // only test non-axial edges + + } + // try the six possible slanted axials from this edge + for ( axis = 0 ; axis < 3 ; axis++ ) + { + for ( dir = -1 ; dir <= 1 ; dir += 2 ) + { + // construct a plane + VectorClear( vec2 ); + vec2[axis] = dir; + CrossProduct( vec, vec2, normal ); + if ( VectorNormalize( normal ) < 0.5 ) { + continue; + } + dist = DotProduct( w->p[j], normal ); + + // if all the points on all the sides are + // behind this plane, it is a proper edge bevel + for ( k = 0 ; k < b->numsides ; k++ ) + { + // if this plane has allready been used, skip it + if ( PlaneEqual( &mapplanes[b->original_sides[k].planenum] + , normal, dist ) ) { + break; + } + + w2 = b->original_sides[k].winding; + if ( !w2 ) { + continue; + } + for ( l = 0 ; l < w2->numpoints ; l++ ) + { + d = DotProduct( w2->p[l], normal ) - dist; + if ( d > 0.1 ) { + break; // point in front + } + } + if ( l != w2->numpoints ) { + break; + } + } + + if ( k != b->numsides ) { + continue; // wasn't part of the outer hull + } + // add this plane + if ( nummapbrushsides == MAX_MAP_BRUSHSIDES ) { + Error( "MAX_MAP_BRUSHSIDES" ); + } + nummapbrushsides++; + s2 = &b->original_sides[b->numsides]; + s2->planenum = FindFloatPlane( normal, dist ); + s2->texinfo = b->original_sides[0].texinfo; +#ifdef SIN + s2->lightinfo = b->original_sides[0].lightinfo; +#endif + s2->contents = b->original_sides[0].contents; + s2->flags |= SFL_BEVEL; + c_edgebevels++; + b->numsides++; + } + } + } + } +} //end of the function AddBrushBevels +//=========================================================================== +// creates windigs for sides and mins / maxs for the brush +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean MakeBrushWindings( mapbrush_t *ob ) { + int i, j; + winding_t *w; + side_t *side; + plane_t *plane; + + ClearBounds( ob->mins, ob->maxs ); + + for ( i = 0; i < ob->numsides; i++ ) + { + plane = &mapplanes[ob->original_sides[i].planenum]; + w = BaseWindingForPlane( plane->normal, plane->dist ); + for ( j = 0; j < ob->numsides && w; j++ ) + { + if ( i == j ) { + continue; + } + if ( ob->original_sides[j].flags & SFL_BEVEL ) { + continue; + } + plane = &mapplanes[ob->original_sides[j].planenum ^ 1]; + ChopWindingInPlace( &w, plane->normal, plane->dist, 0 ); //CLIP_EPSILON); + } + + side = &ob->original_sides[i]; + side->winding = w; + if ( w ) { + side->flags |= SFL_VISIBLE; + for ( j = 0; j < w->numpoints; j++ ) + AddPointToBounds( w->p[j], ob->mins, ob->maxs ); + } + } + + for ( i = 0; i < 3; i++ ) + { + //IDBUG: all the indexes into the mins and maxs were zero (not using i) + if ( ob->mins[i] < -MAX_MAP_BOUNDS || ob->maxs[i] > MAX_MAP_BOUNDS ) { + Log_Print( "entity %i, brush %i: bounds out of range\n", ob->entitynum, ob->brushnum ); + ob->numsides = 0; //remove the brush + break; + } //end if + if ( ob->mins[i] > MAX_MAP_BOUNDS || ob->maxs[i] < -MAX_MAP_BOUNDS ) { + Log_Print( "entity %i, brush %i: no visible sides on brush\n", ob->entitynum, ob->brushnum ); + ob->numsides = 0; //remove the brush + break; + } //end if + } //end for + return true; +} //end of the function MakeBrushWindings +//=========================================================================== +// FIXME: currently doesn't mark all bevels +// NOTE: when one brush bevel is found the remaining sides of the brush +// are bevels as well (when the brush isn't expanded for AAS :)) +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void MarkBrushBevels( mapbrush_t *brush ) { + int i; + int we; + side_t *s; + + //check all the sides of the brush + for ( i = 0; i < brush->numsides; i++ ) + { + s = brush->original_sides + i; + //if the side has no winding + if ( !s->winding ) { + Log_Write( "MarkBrushBevels: brush %d no winding", brush->brushnum ); + s->flags |= SFL_BEVEL; + } //end if + //if the winding is tiny + else if ( WindingIsTiny( s->winding ) ) { + s->flags |= SFL_BEVEL; + Log_Write( "MarkBrushBevels: brush %d tiny winding", brush->brushnum ); + } //end else if + //if the winding has errors + else + { + we = WindingError( s->winding ); + if ( we == WE_NOTENOUGHPOINTS + || we == WE_SMALLAREA + || we == WE_POINTBOGUSRANGE +// || we == WE_NONCONVEX + ) { + Log_Write( "MarkBrushBevels: brush %d %s", brush->brushnum, WindingErrorString() ); + s->flags |= SFL_BEVEL; + } //end else if + } //end else + if ( s->flags & SFL_BEVEL ) { + s->flags &= ~SFL_VISIBLE; + //if the side has a valid plane + if ( s->planenum > 0 && s->planenum < nummapplanes ) { + //if it is an axial plane + if ( mapplanes[s->planenum].type < 3 ) { + c_boxbevels++; + } else { c_edgebevels++;} + } //end if + } //end if + } //end for +} //end of the function MarkBrushBevels +//=========================================================================== +// returns true if the map brush already exists +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int BrushExists( mapbrush_t *brush ) { + int i, s1, s2; + side_t *side1, *side2; + mapbrush_t *brush1, *brush2; + + for ( i = 0; i < nummapbrushes; i++ ) + { + brush1 = brush; + brush2 = &mapbrushes[i]; + //compare the brushes + if ( brush1->entitynum != brush2->entitynum ) { + continue; + } + //if (brush1->contents != brush2->contents) continue; + if ( brush1->numsides != brush2->numsides ) { + continue; + } + for ( s1 = 0; s1 < brush1->numsides; s1++ ) + { + side1 = brush1->original_sides + s1; + // + for ( s2 = 0; s2 < brush2->numsides; s2++ ) + { + side2 = brush2->original_sides + s2; + // + if ( ( side1->planenum & ~1 ) == ( side2->planenum & ~1 ) +// && side1->texinfo == side2->texinfo +// && side1->contents == side2->contents +// && side1->surf == side2->surf + ) { + break; + } + } //end if + if ( s2 >= brush2->numsides ) { + break; + } + } //end for + if ( s1 >= brush1->numsides ) { + return true; + } + } //end for + return false; +} //end of the function BrushExists +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean WriteMapBrush( FILE *fp, mapbrush_t *brush, vec3_t origin ) { + int sn, rotate, shift[2], sv, tv, planenum, p1, i, j; + float scale[2], originshift[2], ang1, ang2, newdist; + vec3_t vecs[2], axis[2]; + map_texinfo_t *ti; + winding_t *w; + side_t *s; + plane_t *plane; + + if ( noliquids ) { + if ( brush->contents & ( CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ) ) { + return true; + } //end if + } //end if + //if the brush has no contents + if ( !brush->contents ) { + return true; + } + //print the leading { + if ( fprintf( fp, " { //brush %d\n", brush->brushnum ) < 0 ) { + return false; + } + //write brush sides + for ( sn = 0; sn < brush->numsides; sn++ ) + { + s = brush->original_sides + sn; + //don't write out bevels + if ( !( s->flags & SFL_BEVEL ) ) { + //if the entity has an origin set + if ( origin[0] || origin[1] || origin[2] ) { + newdist = mapplanes[s->planenum].dist + + DotProduct( mapplanes[s->planenum].normal, origin ); + planenum = FindFloatPlane( mapplanes[s->planenum].normal, newdist ); + } //end if + else + { + planenum = s->planenum; + } //end else + //always take the first plane, then flip the points if necesary + plane = &mapplanes[planenum & ~1]; + w = BaseWindingForPlane( plane->normal, plane->dist ); + // + for ( i = 0; i < 3; i++ ) + { + for ( j = 0; j < 3; j++ ) + { + if ( fabs( w->p[i][j] ) < 0.2 ) { + w->p[i][j] = 0; + } else if ( fabs( (int)w->p[i][j] - w->p[i][j] ) < 0.3 ) { + w->p[i][j] = (int) w->p[i][j]; + } + //w->p[i][j] = (int) (w->p[i][j] + 0.2); + } //end for + } //end for + //three non-colinear points to define the plane + if ( planenum & 1 ) { + p1 = 1; + } else { p1 = 0;} + if ( fprintf( fp," ( %5i %5i %5i ) ", (int)w->p[p1][0], (int)w->p[p1][1], (int)w->p[p1][2] ) < 0 ) { + return false; + } + if ( fprintf( fp,"( %5i %5i %5i ) ", (int)w->p[!p1][0], (int)w->p[!p1][1], (int)w->p[!p1][2] ) < 0 ) { + return false; + } + if ( fprintf( fp,"( %5i %5i %5i ) ", (int)w->p[2][0], (int)w->p[2][1], (int)w->p[2][2] ) < 0 ) { + return false; + } + //free the winding + FreeWinding( w ); + // + if ( s->texinfo == TEXINFO_NODE ) { + if ( brush->contents & CONTENTS_PLAYERCLIP ) { + //player clip + if ( loadedmaptype == MAPTYPE_SIN ) { + if ( fprintf( fp, "generic/misc/clip 0 0 0 1 1" ) < 0 ) { + return false; + } + } //end if + else if ( loadedmaptype == MAPTYPE_QUAKE2 ) { //FIXME: don't always use e1u1 + if ( fprintf( fp, "e1u1/clip 0 0 0 1 1" ) < 0 ) { + return false; + } + } //end else + else if ( loadedmaptype == MAPTYPE_QUAKE3 ) { + if ( fprintf( fp, "e1u1/clip 0 0 0 1 1" ) < 0 ) { + return false; + } + } //end else if + else + { + if ( fprintf( fp, "clip 0 0 0 1 1" ) < 0 ) { + return false; + } + } //end else + } //end if + else if ( brush->contents == CONTENTS_MONSTERCLIP ) { + //monster clip + if ( loadedmaptype == MAPTYPE_SIN ) { + if ( fprintf( fp, "generic/misc/monster 0 0 0 1 1" ) < 0 ) { + return false; + } + } //end if + else if ( loadedmaptype == MAPTYPE_QUAKE2 ) { + if ( fprintf( fp, "e1u1/clip_mon 0 0 0 1 1" ) < 0 ) { + return false; + } + } //end else + else + { + if ( fprintf( fp, "clip 0 0 0 1 1" ) < 0 ) { + return false; + } + } //end else + } //end else + else + { + if ( fprintf( fp, "clip 0 0 0 1 1" ) < 0 ) { + return false; + } + Log_Write( "brush->contents = %d\n", brush->contents ); + } //end else + } //end if + else if ( loadedmaptype == MAPTYPE_SIN && s->texinfo == 0 ) { + if ( brush->contents & CONTENTS_DUMMYFENCE ) { + if ( fprintf( fp, "generic/misc/fence 0 0 0 1 1" ) < 0 ) { + return false; + } + } //end if + else if ( brush->contents & CONTENTS_MIST ) { + if ( fprintf( fp, "generic/misc/volumetric_base 0 0 0 1 1" ) < 0 ) { + return false; + } + } //end if + else //unknown so far + { + if ( fprintf( fp, "generic/misc/red 0 0 0 1 1" ) < 0 ) { + return false; + } + } //end else + } //end if + else if ( loadedmaptype == MAPTYPE_QUAKE3 ) { + //always use the same texture + if ( fprintf( fp, "e2u3/floor1_2 0 0 0 1 1 1 0 0" ) < 0 ) { + return false; + } + } //end else if + else + { + //* + ti = &map_texinfo[s->texinfo]; + //the scaling of the texture + scale[0] = 1 / VectorNormalize2( ti->vecs[0], vecs[0] ); + scale[1] = 1 / VectorNormalize2( ti->vecs[1], vecs[1] ); + // + TextureAxisFromPlane( plane, axis[0], axis[1] ); + //calculate texture shift done by entity origin + originshift[0] = DotProduct( origin, axis[0] ); + originshift[1] = DotProduct( origin, axis[1] ); + //the texture shift without origin shift + shift[0] = ti->vecs[0][3] - originshift[0]; + shift[1] = ti->vecs[1][3] - originshift[1]; + // + if ( axis[0][0] ) { + sv = 0; + } else if ( axis[0][1] ) { + sv = 1; + } else { sv = 2;} + if ( axis[1][0] ) { + tv = 0; + } else if ( axis[1][1] ) { + tv = 1; + } else { tv = 2;} + //calculate rotation of texture + if ( vecs[0][tv] == 0 ) { + ang1 = vecs[0][sv] > 0 ? 90.0 : -90.0; + } else { ang1 = atan2( vecs[0][sv], vecs[0][tv] ) * 180 / Q_PI;} + if ( ang1 < 0 ) { + ang1 += 360; + } + if ( ang1 >= 360 ) { + ang1 -= 360; + } + if ( axis[0][tv] == 0 ) { + ang2 = axis[0][sv] > 0 ? 90.0 : -90.0; + } else { ang2 = atan2( axis[0][sv], axis[0][tv] ) * 180 / Q_PI;} + if ( ang2 < 0 ) { + ang2 += 360; + } + if ( ang2 >= 360 ) { + ang2 -= 360; + } + rotate = ang2 - ang1; + if ( rotate < 0 ) { + rotate += 360; + } + if ( rotate >= 360 ) { + rotate -= 360; + } + //write the texture info + if ( fprintf( fp, "%s %d %d %d", ti->texture, shift[0], shift[1], rotate ) < 0 ) { + return false; + } + if ( fabs( scale[0] - ( (int) scale[0] ) ) < 0.001 ) { + if ( fprintf( fp, " %d", (int) scale[0] ) < 0 ) { + return false; + } + } //end if + else + { + if ( fprintf( fp, " %4f", scale[0] ) < 0 ) { + return false; + } + } //end if + if ( fabs( scale[1] - ( (int) scale[1] ) ) < 0.001 ) { + if ( fprintf( fp, " %d", (int) scale[1] ) < 0 ) { + return false; + } + } //end if + else + { + if ( fprintf( fp, " %4f", scale[1] ) < 0 ) { + return false; + } + } //end else + //write the extra brush side info + if ( loadedmaptype == MAPTYPE_QUAKE2 ) { + if ( fprintf( fp, " %d %d %d", s->contents, ti->flags, ti->value ) < 0 ) { + return false; + } + } //end if + //*/ + } //end else + if ( fprintf( fp, "\n" ) < 0 ) { + return false; + } + } //end if + } //end if + if ( fprintf( fp, " }\n" ) < 0 ) { + return false; + } + c_writtenbrushes++; + return true; +} //end of the function WriteMapBrush +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean WriteOriginBrush( FILE *fp, vec3_t origin ) { + vec3_t normal; + float dist; + int i, s; + winding_t *w; + + if ( fprintf( fp, " {\n" ) < 0 ) { + return false; + } + // + for ( i = 0; i < 3; i++ ) + { + for ( s = -1; s <= 1; s += 2 ) + { + // + VectorClear( normal ); + normal[i] = s; + dist = origin[i] * s + 16; + // + w = BaseWindingForPlane( normal, dist ); + //three non-colinear points to define the plane + if ( fprintf( fp," ( %5i %5i %5i ) ", (int)w->p[0][0], (int)w->p[0][1], (int)w->p[0][2] ) < 0 ) { + return false; + } + if ( fprintf( fp,"( %5i %5i %5i ) ", (int)w->p[1][0], (int)w->p[1][1], (int)w->p[1][2] ) < 0 ) { + return false; + } + if ( fprintf( fp,"( %5i %5i %5i ) ", (int)w->p[2][0], (int)w->p[2][1], (int)w->p[2][2] ) < 0 ) { + return false; + } + //free the winding + FreeWinding( w ); + //write origin texture: + // CONTENTS_ORIGIN = 16777216 + // SURF_NODRAW = 128 + if ( loadedmaptype == MAPTYPE_SIN ) { + if ( fprintf( fp, "generic/misc/origin 0 0 0 1 1" ) < 0 ) { + return false; + } + } //end if + else if ( loadedmaptype == MAPTYPE_HALFLIFE ) { + if ( fprintf( fp, "origin 0 0 0 1 1" ) < 0 ) { + return false; + } + } //end if + else + { + if ( fprintf( fp, "e1u1/origin 0 0 0 1 1" ) < 0 ) { + return false; + } + } //end else + //Quake2 extra brush side info + if ( loadedmaptype == MAPTYPE_QUAKE2 ) { + //if (fprintf(fp, " 16777216 128 0") < 0) return false; + } //end if + if ( fprintf( fp, "\n" ) < 0 ) { + return false; + } + } //end for + } //end for + if ( fprintf( fp, " }\n" ) < 0 ) { + return false; + } + c_writtenbrushes++; + return true; +} //end of the function WriteOriginBrush +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +mapbrush_t *GetAreaPortalBrush( entity_t *mapent ) { + int portalnum, bn; + mapbrush_t *brush = NULL; // TTimo: init + + //the area portal number + portalnum = mapent->areaportalnum; + //find the area portal brush in the world brushes + for ( bn = 0; bn < nummapbrushes && portalnum; bn++ ) + { + brush = &mapbrushes[bn]; + //must be in world entity + if ( brush->entitynum == 0 ) { + if ( brush->contents & CONTENTS_AREAPORTAL ) { + portalnum--; + } //end if + } //end if + } //end for + if ( bn < nummapbrushes ) { + return brush; + } //end if + else + { + Log_Print( "area portal %d brush not found\n", mapent->areaportalnum ); + return NULL; + } //end else +} //end of the function GetAreaPortalBrush +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean WriteMapFileSafe( FILE *fp ) { + char key[1024], value[1024]; + int i, bn, entitybrushes; + epair_t *ep; + mapbrush_t *brush; + entity_t *mapent; + //vec3_t vec_origin = {0, 0, 0}; + + // + if ( fprintf( fp,"//=====================================================\n" + "//\n" + "// map file created with BSPC v1.6\n" + "//\n" + "// BSPC is created by Mr Elusive\n" + "//\n" ) < 0 ) { + return false; + } + if ( loadedmaptype == MAPTYPE_SIN ) { + if ( fprintf( fp, + "// generic/misc/red is used for unknown textures\n" ) < 0 ) { + return false; + } + } //end if + if ( fprintf( fp,"//\n" + "//=====================================================\n" ) < 0 ) { + return false; + } + //write out all the entities + for ( i = 0; i < num_entities; i++ ) + { + mapent = &entities[i]; + if ( !mapent->epairs ) { + continue; + } //end if + if ( fprintf( fp, "{\n" ) < 0 ) { + return false; + } + // + if ( loadedmaptype == MAPTYPE_QUAKE3 ) { + if ( !stricmp( ValueForKey( mapent, "classname" ), "light" ) ) { + SetKeyValue( mapent, "light", "10000" ); + } //end if + } //end if + //write epairs + for ( ep = mapent->epairs; ep; ep = ep->next ) + { + strcpy( key, ep->key ); + StripTrailing( key ); + strcpy( value, ep->value ); + StripTrailing( value ); + // + if ( loadedmaptype == MAPTYPE_QUAKE2 || + loadedmaptype == MAPTYPE_SIN ) { + //don't write an origin for BSP models + if ( mapent->modelnum >= 0 && !strcmp( key, "origin" ) ) { + continue; + } + } //end if + //don't write BSP model numbers + if ( mapent->modelnum >= 0 && !strcmp( key, "model" ) && value[0] == '*' ) { + continue; + } + // + if ( fprintf( fp, " \"%s\" \"%s\"\n", key, value ) < 0 ) { + return false; + } + } //end for + // + if ( ValueForKey( mapent, "origin" ) ) { + GetVectorForKey( mapent, "origin", mapent->origin ); + } else { mapent->origin[0] = mapent->origin[1] = mapent->origin[2] = 0;} + //if this is an area portal entity + if ( !strcmp( "func_areaportal", ValueForKey( mapent, "classname" ) ) ) { + brush = GetAreaPortalBrush( mapent ); + if ( !brush ) { + return false; + } + if ( !WriteMapBrush( fp, brush, mapent->origin ) ) { + return false; + } + } //end if + else + { + entitybrushes = false; + //write brushes + for ( bn = 0; bn < nummapbrushes; bn++ ) + { + brush = &mapbrushes[bn]; + //if the brush is part of this entity + if ( brush->entitynum == i ) { + //don't write out area portal brushes in the world + if ( !( ( brush->contents & CONTENTS_AREAPORTAL ) && brush->entitynum == 0 ) ) { + /* + if (!strcmp("func_door_rotating", ValueForKey(mapent, "classname"))) + { + AAS_PositionFuncRotatingBrush(mapent, brush); + if (!WriteMapBrush(fp, brush, vec_origin)) return false; + } //end if + else // + */ + { + if ( !WriteMapBrush( fp, brush, mapent->origin ) ) { + return false; + } + } //end else + entitybrushes = true; + } //end if + } //end if + } //end for + //if the entity had brushes + if ( entitybrushes ) { + //if the entity has an origin set + if ( mapent->origin[0] || mapent->origin[1] || mapent->origin[2] ) { + if ( !WriteOriginBrush( fp, mapent->origin ) ) { + return false; + } + } //end if + } //end if + } //end else + if ( fprintf( fp, "}\n" ) < 0 ) { + return false; + } + } //end for + if ( fprintf( fp, "//total of %d brushes\n", c_writtenbrushes ) < 0 ) { + return false; + } + return true; +} //end of the function WriteMapFileSafe +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void WriteMapFile( char *filename ) { + FILE *fp; + double start_time; + + c_writtenbrushes = 0; + //the time started + start_time = I_FloatTime(); + // + Log_Print( "writing %s\n", filename ); + fp = fopen( filename, "wb" ); + if ( !fp ) { + Log_Print( "can't open %s\n", filename ); + return; + } //end if + if ( !WriteMapFileSafe( fp ) ) { + fclose( fp ); + Log_Print( "error writing map file %s\n", filename ); + return; + } //end if + fclose( fp ); + //display creation time + Log_Print( "written %d brushes\n", c_writtenbrushes ); + Log_Print( "map file written in %5.0f seconds\n", I_FloatTime() - start_time ); +} //end of the function WriteMapFile +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void PrintMapInfo( void ) { + Log_Print( "\n" ); + Log_Print( "%6i brushes\n", nummapbrushes ); + Log_Print( "%6i brush sides\n", nummapbrushsides ); +// Log_Print("%6i clipbrushes\n", c_clipbrushes); +// Log_Print("%6i total sides\n", nummapbrushsides); +// Log_Print("%6i boxbevels\n", c_boxbevels); +// Log_Print("%6i edgebevels\n", c_edgebevels); +// Log_Print("%6i entities\n", num_entities); +// Log_Print("%6i planes\n", nummapplanes); +// Log_Print("%6i areaportals\n", c_areaportals); +// Log_Print("%6i squatt brushes\n", c_squattbrushes); +// Log_Print("size: %5.0f,%5.0f,%5.0f to %5.0f,%5.0f,%5.0f\n", map_mins[0],map_mins[1],map_mins[2], +// map_maxs[0],map_maxs[1],map_maxs[2]); +} //end of the function PrintMapInfo +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void ResetMapLoading( void ) { + int i; + epair_t *ep, *nextep; + + Q2_ResetMapLoading(); + Sin_ResetMapLoading(); + + //free all map brush side windings + for ( i = 0; i < nummapbrushsides; i++ ) + { + if ( brushsides[i].winding ) { + FreeWinding( brushsides[i].winding ); + } //end for + } //end for + + //reset regular stuff + nummapbrushes = 0; + memset( mapbrushes, 0, MAX_MAPFILE_BRUSHES * sizeof( mapbrush_t ) ); + // + nummapbrushsides = 0; + memset( brushsides, 0, MAX_MAPFILE_BRUSHSIDES * sizeof( side_t ) ); + memset( side_brushtextures, 0, MAX_MAPFILE_BRUSHSIDES * sizeof( brush_texture_t ) ); + // + nummapplanes = 0; + memset( mapplanes, 0, MAX_MAPFILE_PLANES * sizeof( plane_t ) ); + // + memset( planehash, 0, PLANE_HASHES * sizeof( plane_t * ) ); + // + memset( map_texinfo, 0, MAX_MAPFILE_TEXINFO * sizeof( map_texinfo_t ) ); + map_numtexinfo = 0; + // + VectorClear( map_mins ); + VectorClear( map_maxs ); + // + c_boxbevels = 0; + c_edgebevels = 0; + c_areaportals = 0; + c_clipbrushes = 0; + c_writtenbrushes = 0; + //clear the entities + for ( i = 0; i < num_entities; i++ ) + { + for ( ep = entities[i].epairs; ep; ep = nextep ) + { + nextep = ep->next; + FreeMemory( ep->key ); + FreeMemory( ep->value ); + FreeMemory( ep ); + } //end for + } //end for + num_entities = 0; + memset( entities, 0, MAX_MAP_ENTITIES * sizeof( entity_t ) ); +} //end of the function ResetMapLoading +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifndef Q1_BSPVERSION +#define Q1_BSPVERSION 29 +#endif +#ifndef HL_BSPVERSION +#define HL_BSPVERSION 30 +#endif + +#define Q2_BSPHEADER ( ( 'P' << 24 ) + ( 'S' << 16 ) + ( 'B' << 8 ) + 'I' ) //IBSP +#define Q2_BSPVERSION 38 + +#define SINGAME_BSPHEADER ( ( 'P' << 24 ) + ( 'S' << 16 ) + ( 'B' << 8 ) + 'R' ) //RBSP +#define SINGAME_BSPVERSION 1 + +#define SIN_BSPHEADER ( ( 'P' << 24 ) + ( 'S' << 16 ) + ( 'B' << 8 ) + 'I' ) //IBSP +#define SIN_BSPVERSION 41 + +typedef struct +{ + int ident; + int version; +} idheader_t; + +int LoadMapFromBSP( struct quakefile_s *qf ) { + idheader_t idheader; + + if ( ReadQuakeFile( qf, &idheader, 0, sizeof( idheader_t ) ) != sizeof( idheader_t ) ) { + return false; + } //end if + + idheader.ident = LittleLong( idheader.ident ); + idheader.version = LittleLong( idheader.version ); + //Quake3 BSP file + if ( idheader.ident == Q3_BSP_IDENT && idheader.version == Q3_BSP_VERSION ) { + ResetMapLoading(); + Q3_LoadMapFromBSP( qf ); + Q3_FreeMaxBSP(); + } //end if + //Quake2 BSP file + else if ( idheader.ident == Q2_BSPHEADER && idheader.version == Q2_BSPVERSION ) { + ResetMapLoading(); + Q2_AllocMaxBSP(); + Q2_LoadMapFromBSP( qf->filename, qf->offset, qf->length ); + Q2_FreeMaxBSP(); + } //endif + //Sin BSP file + else if ( ( idheader.ident == SIN_BSPHEADER && idheader.version == SIN_BSPVERSION ) || + //the dorks gave the same format another ident and verions + ( idheader.ident == SINGAME_BSPHEADER && idheader.version == SINGAME_BSPVERSION ) ) { + ResetMapLoading(); + Sin_AllocMaxBSP(); + Sin_LoadMapFromBSP( qf->filename, qf->offset, qf->length ); + Sin_FreeMaxBSP(); + } //end if + //the Quake1 bsp files don't have a ident only a version + else if ( idheader.ident == Q1_BSPVERSION ) { + ResetMapLoading(); + Q1_AllocMaxBSP(); + Q1_LoadMapFromBSP( qf->filename, qf->offset, qf->length ); + Q1_FreeMaxBSP(); + } //end if + //Half-Life also only uses a version number + else if ( idheader.ident == HL_BSPVERSION ) { + ResetMapLoading(); + HL_AllocMaxBSP(); + HL_LoadMapFromBSP( qf->filename, qf->offset, qf->length ); + HL_FreeMaxBSP(); + } //end if + else + { + Error( "unknown BSP format %c%c%c%c, version %d\n", + ( idheader.ident & 0xFF ), + ( ( idheader.ident >> 8 ) & 0xFF ), + ( ( idheader.ident >> 16 ) & 0xFF ), + ( ( idheader.ident >> 24 ) & 0xFF ), idheader.version ); + return false; + } //end if + // + return true; +} //end of the function LoadMapFromBSP diff --git a/src/bspc/map_hl.c b/src/bspc/map_hl.c new file mode 100644 index 0000000..ad7906a --- /dev/null +++ b/src/bspc/map_hl.c @@ -0,0 +1,1147 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: map_hl.c +// Function: map loading and writing +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-03 +// Tab Size: 3 +//=========================================================================== + +#include "qbsp.h" +#include "l_bsp_hl.h" +#include "aas_map.h" //AAS_CreateMapBrushes + +int hl_numbrushes; +int hl_numclipbrushes; + +//#define HL_PRINT +#define HLCONTENTS + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int HL_TextureContents( char *name ) { + if ( !Q_strncasecmp( name, "sky",3 ) ) { + return CONTENTS_SOLID; + } + + if ( !Q_strncasecmp( name + 1,"!lava",5 ) ) { + return CONTENTS_LAVA; + } + + if ( !Q_strncasecmp( name + 1,"!slime",6 ) ) { + return CONTENTS_SLIME; + } + + /* + if (!Q_strncasecmp (name, "!cur_90",7)) + return CONTENTS_CURRENT_90; + if (!Q_strncasecmp (name, "!cur_0",6)) + return CONTENTS_CURRENT_0; + if (!Q_strncasecmp (name, "!cur_270",8)) + return CONTENTS_CURRENT_270; + if (!Q_strncasecmp (name, "!cur_180",8)) + return CONTENTS_CURRENT_180; + if (!Q_strncasecmp (name, "!cur_up",7)) + return CONTENTS_CURRENT_UP; + if (!Q_strncasecmp (name, "!cur_dwn",8)) + return CONTENTS_CURRENT_DOWN; + // + */ + if ( name[0] == '!' ) { + return CONTENTS_WATER; + } + /* + if (!Q_strncasecmp (name, "origin",6)) + return CONTENTS_ORIGIN; + if (!Q_strncasecmp (name, "clip",4)) + return CONTENTS_CLIP; + if( !Q_strncasecmp( name, "translucent", 11 ) ) + return CONTENTS_TRANSLUCENT; + if( name[0] == '@' ) + return CONTENTS_TRANSLUCENT; + // + */ + + return CONTENTS_SOLID; +} //end of the function HL_TextureContents +//=========================================================================== +// Generates two new brushes, leaving the original +// unchanged +// +// modified for Half-Life because there are quite a lot of tiny node leaves +// in the Half-Life bsps +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void HL_SplitBrush( bspbrush_t *brush, int planenum, int nodenum, + bspbrush_t **front, bspbrush_t **back ) { + bspbrush_t *b[2]; + int i, j; + winding_t *w, *cw[2], *midwinding; + plane_t *plane, *plane2; + side_t *s, *cs; + float d, d_front, d_back; + + *front = *back = NULL; + plane = &mapplanes[planenum]; + + // check all points + d_front = d_back = 0; + for ( i = 0 ; i < brush->numsides ; i++ ) + { + w = brush->sides[i].winding; + if ( !w ) { + continue; + } + for ( j = 0 ; j < w->numpoints ; j++ ) + { + d = DotProduct( w->p[j], plane->normal ) - plane->dist; + if ( d > 0 && d > d_front ) { + d_front = d; + } + if ( d < 0 && d < d_back ) { + d_back = d; + } + } //end for + } //end for + + if ( d_front < 0.1 ) { // PLANESIDE_EPSILON) + // only on back + *back = CopyBrush( brush ); + Log_Print( "HL_SplitBrush: only on back\n" ); + return; + } //end if + if ( d_back > -0.1 ) { // PLANESIDE_EPSILON) + // only on front + *front = CopyBrush( brush ); + Log_Print( "HL_SplitBrush: only on front\n" ); + return; + } //end if + + // create a new winding from the split plane + + w = BaseWindingForPlane( plane->normal, plane->dist ); + for ( i = 0; i < brush->numsides && w; i++ ) + { + plane2 = &mapplanes[brush->sides[i].planenum ^ 1]; + ChopWindingInPlace( &w, plane2->normal, plane2->dist, 0 ); // PLANESIDE_EPSILON); + } //end for + + if ( !w || WindingIsTiny( w ) ) { // the brush isn't really split + int side; + + Log_Print( "HL_SplitBrush: no split winding\n" ); + side = BrushMostlyOnSide( brush, plane ); + if ( side == PSIDE_FRONT ) { + *front = CopyBrush( brush ); + } + if ( side == PSIDE_BACK ) { + *back = CopyBrush( brush ); + } + return; + } + + if ( WindingIsHuge( w ) ) { + Log_Print( "HL_SplitBrush: WARNING huge split winding\n" ); + } //end of + + midwinding = w; + + // split it for real + + for ( i = 0; i < 2; i++ ) + { + b[i] = AllocBrush( brush->numsides + 1 ); + b[i]->original = brush->original; + } //end for + + // split all the current windings + + for ( i = 0 ; i < brush->numsides ; i++ ) + { + s = &brush->sides[i]; + w = s->winding; + if ( !w ) { + continue; + } + ClipWindingEpsilon( w, plane->normal, plane->dist, + 0 /*PLANESIDE_EPSILON*/, &cw[0], &cw[1] ); + for ( j = 0 ; j < 2 ; j++ ) + { + if ( !cw[j] ) { + continue; + } +#if 0 + if ( WindingIsTiny( cw[j] ) ) { + FreeWinding( cw[j] ); + continue; + } +#endif + cs = &b[j]->sides[b[j]->numsides]; + b[j]->numsides++; + *cs = *s; +// cs->planenum = s->planenum; +// cs->texinfo = s->texinfo; +// cs->visible = s->visible; +// cs->original = s->original; + cs->winding = cw[j]; + cs->flags &= ~SFL_TESTED; + } //end for + } //end for + + + // see if we have valid polygons on both sides + + for ( i = 0 ; i < 2 ; i++ ) + { + BoundBrush( b[i] ); + for ( j = 0 ; j < 3 ; j++ ) + { + if ( b[i]->mins[j] < -4096 || b[i]->maxs[j] > 4096 ) { + Log_Print( "HL_SplitBrush: bogus brush after clip\n" ); + break; + } //end if + } //end for + + if ( b[i]->numsides < 3 || j < 3 ) { + FreeBrush( b[i] ); + b[i] = NULL; + Log_Print( "HL_SplitBrush: numsides < 3\n" ); + } //end if + } //end for + + if ( !( b[0] && b[1] ) ) { + if ( !b[0] && !b[1] ) { + Log_Print( "HL_SplitBrush: split removed brush\n" ); + } else { + Log_Print( "HL_SplitBrush: split not on both sides\n" ); + } + if ( b[0] ) { + FreeBrush( b[0] ); + *front = CopyBrush( brush ); + } //end if + if ( b[1] ) { + FreeBrush( b[1] ); + *back = CopyBrush( brush ); + } //end if + return; + } //end if + + // add the midwinding to both sides + for ( i = 0; i < 2; i++ ) + { + cs = &b[i]->sides[b[i]->numsides]; + b[i]->numsides++; + + cs->planenum = planenum ^ i ^ 1; + cs->texinfo = 0; + //store the node number in the surf to find the texinfo later on + cs->surf = nodenum; + // + cs->flags &= ~SFL_VISIBLE; + cs->flags &= ~SFL_TESTED; + if ( i == 0 ) { + cs->winding = CopyWinding( midwinding ); + } else { + cs->winding = midwinding; + } + } //end for + + + { + vec_t v1; + int i; + + for ( i = 0 ; i < 2 ; i++ ) + { + v1 = BrushVolume( b[i] ); + if ( v1 < 1 ) { + FreeBrush( b[i] ); + b[i] = NULL; + Log_Print( "HL_SplitBrush: tiny volume after clip\n" ); + } //end if + } //end for + } //*/ + + *front = b[0]; + *back = b[1]; +} //end of the function HL_SplitBrush +//=========================================================================== +// returns true if the tree starting at nodenum has only solid leaves +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int HL_SolidTree_r( int nodenum ) { + if ( nodenum < 0 ) { + switch ( hl_dleafs[( -nodenum ) - 1].contents ) + { + case HL_CONTENTS_EMPTY: + { + return false; + } //end case + case HL_CONTENTS_SOLID: +#ifdef HLCONTENTS + case HL_CONTENTS_CLIP: +#endif //HLCONTENTS + case HL_CONTENTS_SKY: +#ifdef HLCONTENTS + case HL_CONTENTS_TRANSLUCENT: +#endif //HLCONTENTS + { + return true; + } //end case + case HL_CONTENTS_WATER: + case HL_CONTENTS_SLIME: + case HL_CONTENTS_LAVA: +#ifdef HLCONTENTS + //these contents should not be found in the BSP + case HL_CONTENTS_ORIGIN: + case HL_CONTENTS_CURRENT_0: + case HL_CONTENTS_CURRENT_90: + case HL_CONTENTS_CURRENT_180: + case HL_CONTENTS_CURRENT_270: + case HL_CONTENTS_CURRENT_UP: + case HL_CONTENTS_CURRENT_DOWN: +#endif //HLCONTENTS + default: + { + return false; + } //end default + } //end switch + return false; + } //end if + if ( !HL_SolidTree_r( hl_dnodes[nodenum].children[0] ) ) { + return false; + } + if ( !HL_SolidTree_r( hl_dnodes[nodenum].children[1] ) ) { + return false; + } + return true; +} //end of the function HL_SolidTree_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *HL_CreateBrushes_r( bspbrush_t *brush, int nodenum ) { + int planenum; + bspbrush_t *front, *back; + hl_dleaf_t *leaf; + + //if it is a leaf + if ( nodenum < 0 ) { + leaf = &hl_dleafs[( -nodenum ) - 1]; + if ( leaf->contents != HL_CONTENTS_EMPTY ) { +#ifdef HL_PRINT + qprintf( "\r%5i", ++hl_numbrushes ); +#endif //HL_PRINT + } //end if + switch ( leaf->contents ) + { + case HL_CONTENTS_EMPTY: + { + FreeBrush( brush ); + return NULL; + } //end case + case HL_CONTENTS_SOLID: +#ifdef HLCONTENTS + case HL_CONTENTS_CLIP: +#endif //HLCONTENTS + case HL_CONTENTS_SKY: +#ifdef HLCONTENTS + case HL_CONTENTS_TRANSLUCENT: +#endif //HLCONTENTS + { + brush->side = CONTENTS_SOLID; + return brush; + } //end case + case HL_CONTENTS_WATER: + { + brush->side = CONTENTS_WATER; + return brush; + } //end case + case HL_CONTENTS_SLIME: + { + brush->side = CONTENTS_SLIME; + return brush; + } //end case + case HL_CONTENTS_LAVA: + { + brush->side = CONTENTS_LAVA; + return brush; + } //end case +#ifdef HLCONTENTS + //these contents should not be found in the BSP + case HL_CONTENTS_ORIGIN: + case HL_CONTENTS_CURRENT_0: + case HL_CONTENTS_CURRENT_90: + case HL_CONTENTS_CURRENT_180: + case HL_CONTENTS_CURRENT_270: + case HL_CONTENTS_CURRENT_UP: + case HL_CONTENTS_CURRENT_DOWN: + { + Error( "HL_CreateBrushes_r: found contents %d in Half-Life BSP", leaf->contents ); + return NULL; + } //end case +#endif //HLCONTENTS + default: + { + Error( "HL_CreateBrushes_r: unknown contents %d in Half-Life BSP", leaf->contents ); + return NULL; + } //end default + } //end switch + return NULL; + } //end if + //if the rest of the tree is solid + /*if (HL_SolidTree_r(nodenum)) + { + brush->side = CONTENTS_SOLID; + return brush; + } //end if*/ + // + planenum = hl_dnodes[nodenum].planenum; + planenum = FindFloatPlane( hl_dplanes[planenum].normal, hl_dplanes[planenum].dist ); + //split the brush with the node plane + HL_SplitBrush( brush, planenum, nodenum, &front, &back ); + //free the original brush + FreeBrush( brush ); + //every node must split the brush in two + if ( !front || !back ) { + Log_Print( "HL_CreateBrushes_r: WARNING node not splitting brush\n" ); + //return NULL; + } //end if + //create brushes recursively + if ( front ) { + front = HL_CreateBrushes_r( front, hl_dnodes[nodenum].children[0] ); + } + if ( back ) { + back = HL_CreateBrushes_r( back, hl_dnodes[nodenum].children[1] ); + } + //link the brushes if possible and return them + if ( front ) { + for ( brush = front; brush->next; brush = brush->next ) ; + brush->next = back; + return front; + } //end if + else + { + return back; + } //end else +} //end of the function HL_CreateBrushes_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *HL_CreateBrushesFromBSP( int modelnum ) { + bspbrush_t *brushlist; + bspbrush_t *brush; + hl_dnode_t *headnode; + vec3_t mins, maxs; + int i; + + // + headnode = &hl_dnodes[hl_dmodels[modelnum].headnode[0]]; + //get the mins and maxs of the world + VectorCopy( headnode->mins, mins ); + VectorCopy( headnode->maxs, maxs ); + //enlarge these mins and maxs + for ( i = 0; i < 3; i++ ) + { + mins[i] -= 8; + maxs[i] += 8; + } //end for + //NOTE: have to add the BSP tree mins and maxs to the MAP mins and maxs + AddPointToBounds( mins, map_mins, map_maxs ); + AddPointToBounds( maxs, map_mins, map_maxs ); + // + if ( !modelnum ) { + Log_Print( "brush size: %5.0f,%5.0f,%5.0f to %5.0f,%5.0f,%5.0f\n", + map_mins[0], map_mins[1], map_mins[2], + map_maxs[0], map_maxs[1], map_maxs[2] ); + } //end if + //create one huge brush containing the whole world + brush = BrushFromBounds( mins, maxs ); + VectorCopy( mins, brush->mins ); + VectorCopy( maxs, brush->maxs ); + // +#ifdef HL_PRINT + qprintf( "creating Half-Life brushes\n" ); + qprintf( "%5d brushes", hl_numbrushes = 0 ); +#endif //HL_PRINT + //create the brushes + brushlist = HL_CreateBrushes_r( brush, hl_dmodels[modelnum].headnode[0] ); + // +#ifdef HL_PRINT + qprintf( "\n" ); +#endif //HL_PRINT + //now we've got a list with brushes! + return brushlist; +} //end of the function HL_CreateBrushesFromBSP +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *HL_MergeBrushes( bspbrush_t *brushlist, int modelnum ) { + int nummerges = 0, merged; // TTimo: init + bspbrush_t *b1, *b2, *tail, *newbrush, *newbrushlist; + bspbrush_t *lastb2; + + if ( !brushlist ) { + return NULL; + } + + if ( !modelnum ) { + qprintf( "%5d brushes merged", nummerges = 0 ); + } + do + { + for ( tail = brushlist; tail; tail = tail->next ) + { + if ( !tail->next ) { + break; + } + } //end for + merged = 0; + newbrushlist = NULL; + for ( b1 = brushlist; b1; b1 = brushlist ) + { + lastb2 = b1; + for ( b2 = b1->next; b2; b2 = b2->next ) + { + //can't merge brushes with different contents + if ( b1->side != b2->side ) { + newbrush = NULL; + } else { newbrush = TryMergeBrushes( b1, b2 );} + //if a merged brush is created + if ( newbrush ) { + //copy the brush contents + newbrush->side = b1->side; + //add the new brush to the end of the list + tail->next = newbrush; + //remove the second brush from the list + lastb2->next = b2->next; + //remove the first brush from the list + brushlist = brushlist->next; + //free the merged brushes + FreeBrush( b1 ); + FreeBrush( b2 ); + //get a new tail brush + for ( tail = brushlist; tail; tail = tail->next ) + { + if ( !tail->next ) { + break; + } + } //end for + merged++; + if ( !modelnum ) { + qprintf( "\r%5d", nummerges++ ); + } + break; + } //end if + lastb2 = b2; + } //end for + //if b1 can't be merged with any of the other brushes + if ( !b2 ) { + brushlist = brushlist->next; + //keep b1 + b1->next = newbrushlist; + newbrushlist = b1; + } //end else + } //end for + brushlist = newbrushlist; + } while ( merged ); + if ( !modelnum ) { + qprintf( "\n" ); + } + return newbrushlist; +} //end of the function HL_MergeBrushes +//=========================================================================== +// returns the amount the face and the winding have overlap +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float HL_FaceOnWinding( hl_dface_t *face, winding_t *winding ) { + int i, edgenum, side; + float dist, area; + hl_dplane_t plane; + vec_t *v1, *v2; + vec3_t normal, edgevec; + winding_t *w; + + // + w = CopyWinding( winding ); + memcpy( &plane, &hl_dplanes[face->planenum], sizeof( hl_dplane_t ) ); + //check on which side of the plane the face is + if ( face->side ) { + VectorNegate( plane.normal, plane.normal ); + plane.dist = -plane.dist; + } //end if + for ( i = 0; i < face->numedges && w; i++ ) + { + //get the first and second vertex of the edge + edgenum = hl_dsurfedges[face->firstedge + i]; + side = edgenum > 0; + //if the face plane is flipped + v1 = hl_dvertexes[hl_dedges[abs( edgenum )].v[side]].point; + v2 = hl_dvertexes[hl_dedges[abs( edgenum )].v[!side]].point; + //create a plane through the edge vector, orthogonal to the face plane + //and with the normal vector pointing out of the face + VectorSubtract( v1, v2, edgevec ); + CrossProduct( edgevec, plane.normal, normal ); + VectorNormalize( normal ); + dist = DotProduct( normal, v1 ); + // + ChopWindingInPlace( &w, normal, dist, 0.9 ); //CLIP_EPSILON + } //end for + if ( w ) { + area = WindingArea( w ); + FreeWinding( w ); + return area; + } //end if + return 0; +} //end of the function HL_FaceOnWinding +//=========================================================================== +// returns a list with brushes created by splitting the given brush with +// planes that go through the face edges and are orthogonal to the face plane +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *HL_SplitBrushWithFace( bspbrush_t *brush, hl_dface_t *face ) { + int i, edgenum, side, planenum, splits; + float dist; + hl_dplane_t plane; + vec_t *v1, *v2; + vec3_t normal, edgevec; + bspbrush_t *front, *back, *brushlist; + + memcpy( &plane, &hl_dplanes[face->planenum], sizeof( hl_dplane_t ) ); + //check on which side of the plane the face is + if ( face->side ) { + VectorNegate( plane.normal, plane.normal ); + plane.dist = -plane.dist; + } //end if + splits = 0; + brushlist = NULL; + for ( i = 0; i < face->numedges; i++ ) + { + //get the first and second vertex of the edge + edgenum = hl_dsurfedges[face->firstedge + i]; + side = edgenum > 0; + //if the face plane is flipped + v1 = hl_dvertexes[hl_dedges[abs( edgenum )].v[side]].point; + v2 = hl_dvertexes[hl_dedges[abs( edgenum )].v[!side]].point; + //create a plane through the edge vector, orthogonal to the face plane + //and with the normal vector pointing out of the face + VectorSubtract( v1, v2, edgevec ); + CrossProduct( edgevec, plane.normal, normal ); + VectorNormalize( normal ); + dist = DotProduct( normal, v1 ); + // + planenum = FindFloatPlane( normal, dist ); + //split the current brush + SplitBrush( brush, planenum, &front, &back ); + //if there is a back brush just put it in the list + if ( back ) { + //copy the brush contents + back->side = brush->side; + // + back->next = brushlist; + brushlist = back; + splits++; + } //end if + if ( !front ) { + Log_Print( "HL_SplitBrushWithFace: no new brush\n" ); + FreeBrushList( brushlist ); + return NULL; + } //end if + //copy the brush contents + front->side = brush->side; + //continue splitting the front brush + brush = front; + } //end for + if ( !splits ) { + FreeBrush( front ); + return NULL; + } //end if + front->next = brushlist; + brushlist = front; + return brushlist; +} //end of the function HL_SplitBrushWithFace +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *HL_TextureBrushes( bspbrush_t *brushlist, int modelnum ) { + float area, largestarea; + int i, n, texinfonum, sn, numbrushes = 0; // TTimo: init + int bestfacenum, sidenodenum; + side_t *side; + hl_dmiptexlump_t *miptexlump; + hl_miptex_t *miptex; + bspbrush_t *brush, *nextbrush, *prevbrush, *newbrushes, *brushlistend; + vec_t defaultvec[4] = {1, 0, 0, 0}; + + if ( !modelnum ) { + qprintf( "texturing brushes\n" ); + } + if ( !modelnum ) { + qprintf( "%5d brushes", numbrushes = 0 ); + } + //get a pointer to the last brush in the list + for ( brushlistend = brushlist; brushlistend; brushlistend = brushlistend->next ) + { + if ( !brushlistend->next ) { + break; + } + } //end for + //there's no previous brush when at the start of the list + prevbrush = NULL; + //go over the brush list + for ( brush = brushlist; brush; brush = nextbrush ) + { + nextbrush = brush->next; + //find a texinfo for every brush side + for ( sn = 0; sn < brush->numsides; sn++ ) + { + side = &brush->sides[sn]; + // + if ( side->flags & SFL_TEXTURED ) { + continue; + } + //number of the node that created this brush side + sidenodenum = side->surf; //see midwinding in HL_SplitBrush + //no face found yet + bestfacenum = -1; + //minimum face size + largestarea = 1; + //if optimizing the texture placement and not going for the + //least number of brushes + if ( !lessbrushes ) { + for ( i = 0; i < hl_numfaces; i++ ) + { + //the face must be in the same plane as the node plane that created + //this brush side + if ( hl_dfaces[i].planenum == hl_dnodes[sidenodenum].planenum ) { + //get the area the face and the brush side overlap + area = HL_FaceOnWinding( &hl_dfaces[i], side->winding ); + //if this face overlaps the brush side winding more than previous faces + if ( area > largestarea ) { + //if there already was a face for texturing this brush side with + //a different texture + if ( bestfacenum >= 0 && + ( hl_dfaces[bestfacenum].texinfo != hl_dfaces[i].texinfo ) ) { + //split the brush to fit the texture + newbrushes = HL_SplitBrushWithFace( brush, &hl_dfaces[i] ); + //if new brushes where created + if ( newbrushes ) { + //remove the current brush from the list + if ( prevbrush ) { + prevbrush->next = brush->next; + } else { brushlist = brush->next;} + if ( brushlistend == brush ) { + brushlistend = prevbrush; + nextbrush = newbrushes; + } //end if + //add the new brushes to the end of the list + if ( brushlistend ) { + brushlistend->next = newbrushes; + } else { brushlist = newbrushes;} + //free the current brush + FreeBrush( brush ); + //don't forget about the prevbrush pointer at the bottom of + //the outer loop + brush = prevbrush; + //find the end of the list + for ( brushlistend = brushlist; brushlistend; brushlistend = brushlistend->next ) + { + if ( !brushlistend->next ) { + break; + } + } //end for + break; + } //end if + else + { + Log_Write( "brush %d: no real texture split", numbrushes ); + } //end else + } //end if + else + { + //best face for texturing this brush side + bestfacenum = i; + } //end else + } //end if + } //end if + } //end for + //if the brush was split the original brush is removed + //and we just continue with the next one in the list + if ( i < hl_numfaces ) { + break; + } + } //end if + else + { + //find the face with the largest overlap with this brush side + //for texturing the brush side + for ( i = 0; i < hl_numfaces; i++ ) + { + //the face must be in the same plane as the node plane that created + //this brush side + if ( hl_dfaces[i].planenum == hl_dnodes[sidenodenum].planenum ) { + //get the area the face and the brush side overlap + area = HL_FaceOnWinding( &hl_dfaces[i], side->winding ); + //if this face overlaps the brush side winding more than previous faces + if ( area > largestarea ) { + largestarea = area; + bestfacenum = i; + } //end if + } //end if + } //end for + } //end else + //if a face was found for texturing this brush side + if ( bestfacenum >= 0 ) { + //set the MAP texinfo values + texinfonum = hl_dfaces[bestfacenum].texinfo; + for ( n = 0; n < 4; n++ ) + { + map_texinfo[texinfonum].vecs[0][n] = hl_texinfo[texinfonum].vecs[0][n]; + map_texinfo[texinfonum].vecs[1][n] = hl_texinfo[texinfonum].vecs[1][n]; + } //end for + //make sure the two vectors aren't of zero length otherwise use the default + //vector to prevent a divide by zero in the map writing + if ( VectorLength( map_texinfo[texinfonum].vecs[0] ) < 0.01 ) { + memcpy( map_texinfo[texinfonum].vecs[0], defaultvec, sizeof( defaultvec ) ); + } + if ( VectorLength( map_texinfo[texinfonum].vecs[1] ) < 0.01 ) { + memcpy( map_texinfo[texinfonum].vecs[1], defaultvec, sizeof( defaultvec ) ); + } + // + map_texinfo[texinfonum].flags = hl_texinfo[texinfonum].flags; + map_texinfo[texinfonum].value = 0; //HL_ and HL texinfos don't have a value + //the mip texture + miptexlump = (hl_dmiptexlump_t *) hl_dtexdata; + miptex = ( hl_miptex_t * )( (byte *)miptexlump + miptexlump->dataofs[hl_texinfo[texinfonum].miptex] ); + //get the mip texture name + strcpy( map_texinfo[texinfonum].texture, miptex->name ); + //no animations in Quake1 and Half-Life mip textures + map_texinfo[texinfonum].nexttexinfo = -1; + //store the texinfo number + side->texinfo = texinfonum; + // + if ( texinfonum > map_numtexinfo ) { + map_numtexinfo = texinfonum; + } + //this side is textured + side->flags |= SFL_TEXTURED; + } //end if + else + { + //no texture for this side + side->texinfo = TEXINFO_NODE; + //this side is textured + side->flags |= SFL_TEXTURED; + } //end if + } //end for + // + if ( !modelnum && prevbrush != brush ) { + qprintf( "\r%5d", ++numbrushes ); + } + //previous brush in the list + prevbrush = brush; + } //end for + if ( !modelnum ) { + qprintf( "\n" ); + } + //return the new list with brushes + return brushlist; +} //end of the function HL_TextureBrushes +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void HL_FixContentsTextures( bspbrush_t *brushlist ) { + int i, texinfonum; + bspbrush_t *brush; + + for ( brush = brushlist; brush; brush = brush->next ) + { + //only fix the textures of water, slime and lava brushes + if ( brush->side != CONTENTS_WATER && + brush->side != CONTENTS_SLIME && + brush->side != CONTENTS_LAVA ) { + continue; + } + // + for ( i = 0; i < brush->numsides; i++ ) + { + texinfonum = brush->sides[i].texinfo; + if ( HL_TextureContents( map_texinfo[texinfonum].texture ) == brush->side ) { + break; + } + } //end for + //if no specific contents texture was found + if ( i >= brush->numsides ) { + texinfonum = -1; + for ( i = 0; i < map_numtexinfo; i++ ) + { + if ( HL_TextureContents( map_texinfo[i].texture ) == brush->side ) { + texinfonum = i; + break; + } //end if + } //end for + } //end if + // + if ( texinfonum >= 0 ) { + //give all the brush sides this contents texture + for ( i = 0; i < brush->numsides; i++ ) + { + brush->sides[i].texinfo = texinfonum; + } //end for + } //end if + else {Log_Print( "brush contents %d with wrong textures\n", brush->side );} + // + } //end for + /* + for (brush = brushlist; brush; brush = brush->next) + { + //give all the brush sides this contents texture + for (i = 0; i < brush->numsides; i++) + { + if (HL_TextureContents(map_texinfo[texinfonum].texture) != brush->side) + { + Error("brush contents %d with wrong contents textures %s\n", brush->side, + HL_TextureContents(map_texinfo[texinfonum].texture)); + } //end if + } //end for + } //end for*/ +} //end of the function HL_FixContentsTextures +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void HL_BSPBrushToMapBrush( bspbrush_t *bspbrush, entity_t *mapent ) { + mapbrush_t *mapbrush; + side_t *side; + int i, besttexinfo; + + if ( nummapbrushes >= MAX_MAPFILE_BRUSHES ) { + Error( "nummapbrushes == MAX_MAPFILE_BRUSHES" ); + } + + mapbrush = &mapbrushes[nummapbrushes]; + mapbrush->original_sides = &brushsides[nummapbrushsides]; + mapbrush->entitynum = mapent - entities; + mapbrush->brushnum = nummapbrushes - mapent->firstbrush; + mapbrush->leafnum = -1; + mapbrush->numsides = 0; + + besttexinfo = TEXINFO_NODE; + for ( i = 0; i < bspbrush->numsides; i++ ) + { + if ( !bspbrush->sides[i].winding ) { + continue; + } + // + if ( nummapbrushsides >= MAX_MAPFILE_BRUSHSIDES ) { + Error( "MAX_MAPFILE_BRUSHSIDES" ); + } + side = &brushsides[nummapbrushsides]; + //the contents of the bsp brush is stored in the side variable + side->contents = bspbrush->side; + side->surf = 0; + side->planenum = bspbrush->sides[i].planenum; + side->texinfo = bspbrush->sides[i].texinfo; + if ( side->texinfo != TEXINFO_NODE ) { + //this brush side is textured + side->flags |= SFL_TEXTURED; + besttexinfo = side->texinfo; + } //end if + // + nummapbrushsides++; + mapbrush->numsides++; + } //end for + // + if ( besttexinfo == TEXINFO_NODE ) { + mapbrush->numsides = 0; + hl_numclipbrushes++; + return; + } //end if + //set the texinfo for all the brush sides without texture + for ( i = 0; i < mapbrush->numsides; i++ ) + { + if ( mapbrush->original_sides[i].texinfo == TEXINFO_NODE ) { + mapbrush->original_sides[i].texinfo = besttexinfo; + } //end if + } //end for + //contents of the brush + mapbrush->contents = bspbrush->side; + // + if ( create_aas ) { + //create the AAS brushes from this brush, add brush bevels + AAS_CreateMapBrushes( mapbrush, mapent, true ); + return; + } //end if + //create windings for sides and bounds for brush + MakeBrushWindings( mapbrush ); + //add brush bevels + AddBrushBevels( mapbrush ); + //a new brush has been created + nummapbrushes++; + mapent->numbrushes++; +} //end of the function HL_BSPBrushToMapBrush +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void HL_CreateMapBrushes( entity_t *mapent, int modelnum ) { + bspbrush_t *brushlist, *brush, *nextbrush; + int i = 0; //TTimo: init + + //create brushes from the model BSP tree + brushlist = HL_CreateBrushesFromBSP( modelnum ); + //texture the brushes and split them when necesary + brushlist = HL_TextureBrushes( brushlist, modelnum ); + //fix the contents textures of all brushes + HL_FixContentsTextures( brushlist ); + // + if ( !nobrushmerge ) { + brushlist = HL_MergeBrushes( brushlist, modelnum ); + //brushlist = HL_MergeBrushes(brushlist, modelnum); + } //end if + // + if ( !modelnum ) { + qprintf( "converting brushes to map brushes\n" ); + } + if ( !modelnum ) { + qprintf( "%5d brushes", i = 0 ); + } + for ( brush = brushlist; brush; brush = nextbrush ) + { + nextbrush = brush->next; + HL_BSPBrushToMapBrush( brush, mapent ); + brush->next = NULL; + FreeBrush( brush ); + if ( !modelnum ) { + qprintf( "\r%5d", ++i ); + } + } //end for + if ( !modelnum ) { + qprintf( "\n" ); + } +} //end of the function HL_CreateMapBrushes +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void HL_ResetMapLoading( void ) { +} //end of the function HL_ResetMapLoading +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void HL_LoadMapFromBSP( char *filename, int offset, int length ) { + int i, modelnum; + char *model, *classname; + + Log_Print( "-- HL_LoadMapFromBSP --\n" ); + //loaded map type + loadedmaptype = MAPTYPE_HALFLIFE; + // + qprintf( "loading map from %s at %d\n", filename, offset ); + //load the Half-Life BSP file + HL_LoadBSPFile( filename, offset, length ); + // + hl_numclipbrushes = 0; + //parse the entities from the BSP + HL_ParseEntities(); + //clear the map mins and maxs + ClearBounds( map_mins, map_maxs ); + // + qprintf( "creating Half-Life brushes\n" ); + if ( lessbrushes ) { + qprintf( "creating minimum number of brushes\n" ); + } else { qprintf( "placing textures correctly\n" );} + // + for ( i = 0; i < num_entities; i++ ) + { + entities[i].firstbrush = nummapbrushes; + entities[i].numbrushes = 0; + // + classname = ValueForKey( &entities[i], "classname" ); + if ( classname && !strcmp( classname, "worldspawn" ) ) { + modelnum = 0; + } //end if + else + { + // + model = ValueForKey( &entities[i], "model" ); + if ( !model || *model != '*' ) { + continue; + } + model++; + modelnum = atoi( model ); + } //end else + //create map brushes for the entity + HL_CreateMapBrushes( &entities[i], modelnum ); + } //end for + // + qprintf( "%5d map brushes\n", nummapbrushes ); + qprintf( "%5d clip brushes\n", hl_numclipbrushes ); +} //end of the function HL_LoadMapFromBSP diff --git a/src/bspc/map_q1.c b/src/bspc/map_q1.c new file mode 100644 index 0000000..4756915 --- /dev/null +++ b/src/bspc/map_q1.c @@ -0,0 +1,1205 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: map_q1.c +// Function: map loading and writing +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-03 +// Tab Size: 3 +// NOTES: the recursive splitting of the huge brush sometimes still +// creates bad brushes +//=========================================================================== + +#include "qbsp.h" +#include "l_bsp_q1.h" +#include "aas_map.h" //AAS_CreateMapBrushes + +int q1_numbrushes; +int q1_numclipbrushes; + +//#define Q1_PRINT + +//=========================================================================== +// water, slime and lava brush textures names always start with a * +// followed by the type: "slime", "lava" or otherwise water +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int Q1_TextureContents( char *name ) { + if ( !Q_strcasecmp( name, "clip" ) ) { + return CONTENTS_SOLID; + } + if ( name[0] == '*' ) { + if ( !Q_strncasecmp( name + 1,"lava",4 ) ) { + return CONTENTS_LAVA; + } else if ( !Q_strncasecmp( name + 1,"slime",5 ) ) { + return CONTENTS_SLIME; + } else { return CONTENTS_WATER;} + } //end if + else if ( !Q_strncasecmp( name, "sky", 3 ) ) { + return CONTENTS_SOLID; + } else { return CONTENTS_SOLID;} +} //end of the function Q1_TextureContents +//=========================================================================== +// Generates two new brushes, leaving the original +// unchanged +// +// modified for Half-Life because there are quite a lot of tiny node leaves +// in the Half-Life bsps +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Q1_SplitBrush( bspbrush_t *brush, int planenum, int nodenum, + bspbrush_t **front, bspbrush_t **back ) { + bspbrush_t *b[2]; + int i, j; + winding_t *w, *cw[2], *midwinding; + plane_t *plane, *plane2; + side_t *s, *cs; + float d, d_front, d_back; + + *front = *back = NULL; + plane = &mapplanes[planenum]; + + // check all points + d_front = d_back = 0; + for ( i = 0 ; i < brush->numsides ; i++ ) + { + w = brush->sides[i].winding; + if ( !w ) { + continue; + } + for ( j = 0 ; j < w->numpoints ; j++ ) + { + d = DotProduct( w->p[j], plane->normal ) - plane->dist; + if ( d > 0 && d > d_front ) { + d_front = d; + } + if ( d < 0 && d < d_back ) { + d_back = d; + } + } //end for + } //end for + + if ( d_front < 0.1 ) { // PLANESIDE_EPSILON) + // only on back + *back = CopyBrush( brush ); + Log_Print( "Q1_SplitBrush: only on back\n" ); + return; + } //end if + if ( d_back > -0.1 ) { // PLANESIDE_EPSILON) + // only on front + *front = CopyBrush( brush ); + Log_Print( "Q1_SplitBrush: only on front\n" ); + return; + } //end if + + // create a new winding from the split plane + + w = BaseWindingForPlane( plane->normal, plane->dist ); + for ( i = 0; i < brush->numsides && w; i++ ) + { + plane2 = &mapplanes[brush->sides[i].planenum ^ 1]; + ChopWindingInPlace( &w, plane2->normal, plane2->dist, 0 ); // PLANESIDE_EPSILON); + } //end for + + if ( !w || WindingIsTiny( w ) ) { // the brush isn't really split + int side; + + Log_Print( "Q1_SplitBrush: no split winding\n" ); + side = BrushMostlyOnSide( brush, plane ); + if ( side == PSIDE_FRONT ) { + *front = CopyBrush( brush ); + } + if ( side == PSIDE_BACK ) { + *back = CopyBrush( brush ); + } + return; + } + + if ( WindingIsHuge( w ) ) { + Log_Print( "Q1_SplitBrush: WARNING huge split winding\n" ); + } //end of + + midwinding = w; + + // split it for real + + for ( i = 0; i < 2; i++ ) + { + b[i] = AllocBrush( brush->numsides + 1 ); + b[i]->original = brush->original; + } //end for + + // split all the current windings + + for ( i = 0 ; i < brush->numsides ; i++ ) + { + s = &brush->sides[i]; + w = s->winding; + if ( !w ) { + continue; + } + ClipWindingEpsilon( w, plane->normal, plane->dist, + 0 /*PLANESIDE_EPSILON*/, &cw[0], &cw[1] ); + for ( j = 0 ; j < 2 ; j++ ) + { + if ( !cw[j] ) { + continue; + } +#if 0 + if ( WindingIsTiny( cw[j] ) ) { + FreeWinding( cw[j] ); + continue; + } +#endif + cs = &b[j]->sides[b[j]->numsides]; + b[j]->numsides++; + *cs = *s; +// cs->planenum = s->planenum; +// cs->texinfo = s->texinfo; +// cs->visible = s->visible; +// cs->original = s->original; + cs->winding = cw[j]; + cs->flags &= ~SFL_TESTED; + } //end for + } //end for + + + // see if we have valid polygons on both sides + + for ( i = 0 ; i < 2 ; i++ ) + { + BoundBrush( b[i] ); + for ( j = 0 ; j < 3 ; j++ ) + { + if ( b[i]->mins[j] < -4096 || b[i]->maxs[j] > 4096 ) { + Log_Print( "Q1_SplitBrush: bogus brush after clip\n" ); + break; + } //end if + } //end for + + if ( b[i]->numsides < 3 || j < 3 ) { + FreeBrush( b[i] ); + b[i] = NULL; + Log_Print( "Q1_SplitBrush: numsides < 3\n" ); + } //end if + } //end for + + if ( !( b[0] && b[1] ) ) { + if ( !b[0] && !b[1] ) { + Log_Print( "Q1_SplitBrush: split removed brush\n" ); + } else { + Log_Print( "Q1_SplitBrush: split not on both sides\n" ); + } + if ( b[0] ) { + FreeBrush( b[0] ); + *front = CopyBrush( brush ); + } //end if + if ( b[1] ) { + FreeBrush( b[1] ); + *back = CopyBrush( brush ); + } //end if + return; + } //end if + + // add the midwinding to both sides + for ( i = 0; i < 2; i++ ) + { + cs = &b[i]->sides[b[i]->numsides]; + b[i]->numsides++; + + cs->planenum = planenum ^ i ^ 1; + cs->texinfo = 0; + //store the node number in the surf to find the texinfo later on + cs->surf = nodenum; + // + cs->flags &= ~SFL_VISIBLE; + cs->flags &= ~SFL_TESTED; + cs->flags &= ~SFL_TEXTURED; + if ( i == 0 ) { + cs->winding = CopyWinding( midwinding ); + } else { + cs->winding = midwinding; + } + } //end for + + + { + vec_t v1; + int i; + + for ( i = 0 ; i < 2 ; i++ ) + { + v1 = BrushVolume( b[i] ); + if ( v1 < 1 ) { + FreeBrush( b[i] ); + b[i] = NULL; + Log_Print( "Q1_SplitBrush: tiny volume after clip\n" ); + } //end if + } //end for + } //*/ + + *front = b[0]; + *back = b[1]; +} //end of the function Q1_SplitBrush +//=========================================================================== +// returns true if the tree starting at nodenum has only solid leaves +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int Q1_SolidTree_r( int nodenum ) { + if ( nodenum < 0 ) { + switch ( q1_dleafs[( -nodenum ) - 1].contents ) + { + case Q1_CONTENTS_EMPTY: + { + return false; + } //end case + case Q1_CONTENTS_SOLID: +#ifdef HLCONTENTS + case Q1_CONTENTS_CLIP: +#endif HLCONTENTS + case Q1_CONTENTS_SKY: +#ifdef HLCONTENTS + case Q1_CONTENTS_TRANSLUCENT: +#endif HLCONTENTS + { + return true; + } //end case + case Q1_CONTENTS_WATER: + case Q1_CONTENTS_SLIME: + case Q1_CONTENTS_LAVA: +#ifdef HLCONTENTS + //these contents should not be found in the BSP + case Q1_CONTENTS_ORIGIN: + case Q1_CONTENTS_CURRENT_0: + case Q1_CONTENTS_CURRENT_90: + case Q1_CONTENTS_CURRENT_180: + case Q1_CONTENTS_CURRENT_270: + case Q1_CONTENTS_CURRENT_UP: + case Q1_CONTENTS_CURRENT_DOWN: +#endif HLCONTENTS + default: + { + return false; + } //end default + } //end switch + return false; + } //end if + if ( !Q1_SolidTree_r( q1_dnodes[nodenum].children[0] ) ) { + return false; + } + if ( !Q1_SolidTree_r( q1_dnodes[nodenum].children[1] ) ) { + return false; + } + return true; +} //end of the function Q1_SolidTree_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *Q1_CreateBrushes_r( bspbrush_t *brush, int nodenum ) { + int planenum; + bspbrush_t *front, *back; + q1_dleaf_t *leaf; + + //if it is a leaf + if ( nodenum < 0 ) { + leaf = &q1_dleafs[( -nodenum ) - 1]; + if ( leaf->contents != Q1_CONTENTS_EMPTY ) { +#ifdef Q1_PRINT + qprintf( "\r%5i", ++q1_numbrushes ); +#endif //Q1_PRINT + } //end if + switch ( leaf->contents ) + { + case Q1_CONTENTS_EMPTY: + { + FreeBrush( brush ); + return NULL; + } //end case + case Q1_CONTENTS_SOLID: +#ifdef HLCONTENTS + case Q1_CONTENTS_CLIP: +#endif HLCONTENTS + case Q1_CONTENTS_SKY: +#ifdef HLCONTENTS + case Q1_CONTENTS_TRANSLUCENT: +#endif HLCONTENTS + { + brush->side = CONTENTS_SOLID; + return brush; + } //end case + case Q1_CONTENTS_WATER: + { + brush->side = CONTENTS_WATER; + return brush; + } //end case + case Q1_CONTENTS_SLIME: + { + brush->side = CONTENTS_SLIME; + return brush; + } //end case + case Q1_CONTENTS_LAVA: + { + brush->side = CONTENTS_LAVA; + return brush; + } //end case +#ifdef HLCONTENTS + //these contents should not be found in the BSP + case Q1_CONTENTS_ORIGIN: + case Q1_CONTENTS_CURRENT_0: + case Q1_CONTENTS_CURRENT_90: + case Q1_CONTENTS_CURRENT_180: + case Q1_CONTENTS_CURRENT_270: + case Q1_CONTENTS_CURRENT_UP: + case Q1_CONTENTS_CURRENT_DOWN: + { + Error( "Q1_CreateBrushes_r: found contents %d in Half-Life BSP", leaf->contents ); + return NULL; + } //end case +#endif HLCONTENTS + default: + { + Error( "Q1_CreateBrushes_r: unknown contents %d in Half-Life BSP", leaf->contents ); + return NULL; + } //end default + } //end switch + return NULL; + } //end if + //if the rest of the tree is solid + /*if (Q1_SolidTree_r(nodenum)) + { + brush->side = CONTENTS_SOLID; + return brush; + } //end if*/ + // + planenum = q1_dnodes[nodenum].planenum; + planenum = FindFloatPlane( q1_dplanes[planenum].normal, q1_dplanes[planenum].dist ); + //split the brush with the node plane + Q1_SplitBrush( brush, planenum, nodenum, &front, &back ); + //free the original brush + FreeBrush( brush ); + //every node must split the brush in two + if ( !front || !back ) { + Log_Print( "Q1_CreateBrushes_r: WARNING node not splitting brush\n" ); + //return NULL; + } //end if + //create brushes recursively + if ( front ) { + front = Q1_CreateBrushes_r( front, q1_dnodes[nodenum].children[0] ); + } + if ( back ) { + back = Q1_CreateBrushes_r( back, q1_dnodes[nodenum].children[1] ); + } + //link the brushes if possible and return them + if ( front ) { + for ( brush = front; brush->next; brush = brush->next ) ; + brush->next = back; + return front; + } //end if + else + { + return back; + } //end else +} //end of the function Q1_CreateBrushes_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *Q1_CreateBrushesFromBSP( int modelnum ) { + bspbrush_t *brushlist; + bspbrush_t *brush; + q1_dnode_t *headnode; + vec3_t mins, maxs; + int i; + + // + headnode = &q1_dnodes[q1_dmodels[modelnum].headnode[0]]; + //get the mins and maxs of the world + VectorCopy( headnode->mins, mins ); + VectorCopy( headnode->maxs, maxs ); + //enlarge these mins and maxs + for ( i = 0; i < 3; i++ ) + { + mins[i] -= 8; + maxs[i] += 8; + } //end for + //NOTE: have to add the BSP tree mins and maxs to the MAP mins and maxs + AddPointToBounds( mins, map_mins, map_maxs ); + AddPointToBounds( maxs, map_mins, map_maxs ); + // + if ( !modelnum ) { + Log_Print( "brush size: %5.0f,%5.0f,%5.0f to %5.0f,%5.0f,%5.0f\n", + map_mins[0], map_mins[1], map_mins[2], + map_maxs[0], map_maxs[1], map_maxs[2] ); + } //end if + //create one huge brush containing the whole world + brush = BrushFromBounds( mins, maxs ); + VectorCopy( mins, brush->mins ); + VectorCopy( maxs, brush->maxs ); + // +#ifdef Q1_PRINT + qprintf( "creating Quake brushes\n" ); + qprintf( "%5d brushes", q1_numbrushes = 0 ); +#endif //Q1_PRINT + //create the brushes + brushlist = Q1_CreateBrushes_r( brush, q1_dmodels[modelnum].headnode[0] ); + // +#ifdef Q1_PRINT + qprintf( "\n" ); +#endif //Q1_PRINT + //now we've got a list with brushes! + return brushlist; +} //end of the function Q1_CreateBrushesFromBSP +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +q1_dleaf_t *Q1_PointInLeaf( int startnode, vec3_t point ) { + int nodenum; + vec_t dist; + q1_dnode_t *node; + q1_dplane_t *plane; + + nodenum = startnode; + while ( nodenum >= 0 ) + { + node = &q1_dnodes[nodenum]; + plane = &q1_dplanes[node->planenum]; + dist = DotProduct( point, plane->normal ) - plane->dist; + if ( dist > 0 ) { + nodenum = node->children[0]; + } else { + nodenum = node->children[1]; + } + } //end while + + return &q1_dleafs[-nodenum - 1]; +} //end of the function Q1_PointInLeaf +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float Q1_FaceArea( q1_dface_t *face ) { + int i; + float total; + vec_t *v; + vec3_t d1, d2, cross; + q1_dedge_t *edge; + + edge = &q1_dedges[face->firstedge]; + v = q1_dvertexes[edge->v[0]].point; + + total = 0; + for ( i = 1; i < face->numedges - 1; i++ ) + { + edge = &q1_dedges[face->firstedge + i]; + VectorSubtract( q1_dvertexes[edge->v[0]].point, v, d1 ); + VectorSubtract( q1_dvertexes[edge->v[1]].point, v, d2 ); + CrossProduct( d1, d2, cross ); + total += 0.5 * VectorLength( cross ); + } //end for + return total; +} //end of the function AAS_FaceArea +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Q1_FacePlane( q1_dface_t *face, vec3_t normal, float *dist ) { + vec_t *v1, *v2, *v3; + vec3_t vec1, vec2; + int side, edgenum; + + edgenum = q1_dsurfedges[face->firstedge]; + side = edgenum < 0; + v1 = q1_dvertexes[q1_dedges[abs( edgenum )].v[side]].point; + v2 = q1_dvertexes[q1_dedges[abs( edgenum )].v[!side]].point; + edgenum = q1_dsurfedges[face->firstedge + 1]; + side = edgenum < 0; + v3 = q1_dvertexes[q1_dedges[abs( edgenum )].v[!side]].point; + // + VectorSubtract( v2, v1, vec1 ); + VectorSubtract( v3, v1, vec2 ); + + CrossProduct( vec1, vec2, normal ); + VectorNormalize( normal ); + *dist = DotProduct( v1, normal ); +} //end of the function Q1_FacePlane +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *Q1_MergeBrushes( bspbrush_t *brushlist, int modelnum ) { + int nummerges = 0, merged; // TTimo: init + bspbrush_t *b1, *b2, *tail, *newbrush, *newbrushlist; + bspbrush_t *lastb2; + + if ( !brushlist ) { + return NULL; + } + + if ( !modelnum ) { + qprintf( "%5d brushes merged", nummerges = 0 ); + } + do + { + for ( tail = brushlist; tail; tail = tail->next ) + { + if ( !tail->next ) { + break; + } + } //end for + merged = 0; + newbrushlist = NULL; + for ( b1 = brushlist; b1; b1 = brushlist ) + { + lastb2 = b1; + for ( b2 = b1->next; b2; b2 = b2->next ) + { + //can't merge brushes with different contents + if ( b1->side != b2->side ) { + newbrush = NULL; + } else { newbrush = TryMergeBrushes( b1, b2 );} + //if a merged brush is created + if ( newbrush ) { + //copy the brush contents + newbrush->side = b1->side; + //add the new brush to the end of the list + tail->next = newbrush; + //remove the second brush from the list + lastb2->next = b2->next; + //remove the first brush from the list + brushlist = brushlist->next; + //free the merged brushes + FreeBrush( b1 ); + FreeBrush( b2 ); + //get a new tail brush + for ( tail = brushlist; tail; tail = tail->next ) + { + if ( !tail->next ) { + break; + } + } //end for + merged++; + if ( !modelnum ) { + qprintf( "\r%5d", nummerges++ ); + } + break; + } //end if + lastb2 = b2; + } //end for + //if b1 can't be merged with any of the other brushes + if ( !b2 ) { + brushlist = brushlist->next; + //keep b1 + b1->next = newbrushlist; + newbrushlist = b1; + } //end else + } //end for + brushlist = newbrushlist; + } while ( merged ); + if ( !modelnum ) { + qprintf( "\n" ); + } + return newbrushlist; +} //end of the function Q1_MergeBrushes +//=========================================================================== +// returns the amount the face and the winding overlap +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +float Q1_FaceOnWinding( q1_dface_t *face, winding_t *winding ) { + int i, edgenum, side; + float dist, area; + q1_dplane_t plane; + vec_t *v1, *v2; + vec3_t normal, edgevec; + winding_t *w; + + // + w = CopyWinding( winding ); + memcpy( &plane, &q1_dplanes[face->planenum], sizeof( q1_dplane_t ) ); + //check on which side of the plane the face is + if ( face->side ) { + VectorNegate( plane.normal, plane.normal ); + plane.dist = -plane.dist; + } //end if + for ( i = 0; i < face->numedges && w; i++ ) + { + //get the first and second vertex of the edge + edgenum = q1_dsurfedges[face->firstedge + i]; + side = edgenum > 0; + //if the face plane is flipped + v1 = q1_dvertexes[q1_dedges[abs( edgenum )].v[side]].point; + v2 = q1_dvertexes[q1_dedges[abs( edgenum )].v[!side]].point; + //create a plane through the edge vector, orthogonal to the face plane + //and with the normal vector pointing out of the face + VectorSubtract( v1, v2, edgevec ); + CrossProduct( edgevec, plane.normal, normal ); + VectorNormalize( normal ); + dist = DotProduct( normal, v1 ); + // + ChopWindingInPlace( &w, normal, dist, 0.9 ); //CLIP_EPSILON + } //end for + if ( w ) { + area = WindingArea( w ); + FreeWinding( w ); + return area; + } //end if + return 0; +} //end of the function Q1_FaceOnWinding +//=========================================================================== +// returns a list with brushes created by splitting the given brush with +// planes that go through the face edges and are orthogonal to the face plane +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *Q1_SplitBrushWithFace( bspbrush_t *brush, q1_dface_t *face ) { + int i, edgenum, side, planenum, splits; + float dist; + q1_dplane_t plane; + vec_t *v1, *v2; + vec3_t normal, edgevec; + bspbrush_t *front, *back, *brushlist; + + memcpy( &plane, &q1_dplanes[face->planenum], sizeof( q1_dplane_t ) ); + //check on which side of the plane the face is + if ( face->side ) { + VectorNegate( plane.normal, plane.normal ); + plane.dist = -plane.dist; + } //end if + splits = 0; + brushlist = NULL; + for ( i = 0; i < face->numedges; i++ ) + { + //get the first and second vertex of the edge + edgenum = q1_dsurfedges[face->firstedge + i]; + side = edgenum > 0; + //if the face plane is flipped + v1 = q1_dvertexes[q1_dedges[abs( edgenum )].v[side]].point; + v2 = q1_dvertexes[q1_dedges[abs( edgenum )].v[!side]].point; + //create a plane through the edge vector, orthogonal to the face plane + //and with the normal vector pointing out of the face + VectorSubtract( v1, v2, edgevec ); + CrossProduct( edgevec, plane.normal, normal ); + VectorNormalize( normal ); + dist = DotProduct( normal, v1 ); + // + planenum = FindFloatPlane( normal, dist ); + //split the current brush + SplitBrush( brush, planenum, &front, &back ); + //if there is a back brush just put it in the list + if ( back ) { + //copy the brush contents + back->side = brush->side; + // + back->next = brushlist; + brushlist = back; + splits++; + } //end if + if ( !front ) { + Log_Print( "Q1_SplitBrushWithFace: no new brush\n" ); + FreeBrushList( brushlist ); + return NULL; + } //end if + //copy the brush contents + front->side = brush->side; + //continue splitting the front brush + brush = front; + } //end for + if ( !splits ) { + FreeBrush( front ); + return NULL; + } //end if + front->next = brushlist; + brushlist = front; + return brushlist; +} //end of the function Q1_SplitBrushWithFace +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +bspbrush_t *Q1_TextureBrushes( bspbrush_t *brushlist, int modelnum ) { + float area, largestarea; + int i, n, texinfonum, sn, numbrushes = 0; // TTimo: init + int bestfacenum, sidenodenum; + side_t *side; + q1_dmiptexlump_t *miptexlump; + q1_miptex_t *miptex; + bspbrush_t *brush, *nextbrush, *prevbrush, *newbrushes, *brushlistend; + vec_t defaultvec[4] = {1, 0, 0, 0}; + + if ( !modelnum ) { + qprintf( "texturing brushes\n" ); + } + if ( !modelnum ) { + qprintf( "%5d brushes", numbrushes = 0 ); + } + //get a pointer to the last brush in the list + for ( brushlistend = brushlist; brushlistend; brushlistend = brushlistend->next ) + { + if ( !brushlistend->next ) { + break; + } + } //end for + //there's no previous brush when at the start of the list + prevbrush = NULL; + //go over the brush list + for ( brush = brushlist; brush; brush = nextbrush ) + { + nextbrush = brush->next; + //find a texinfo for every brush side + for ( sn = 0; sn < brush->numsides; sn++ ) + { + side = &brush->sides[sn]; + // + if ( side->flags & SFL_TEXTURED ) { + continue; + } + //number of the node that created this brush side + sidenodenum = side->surf; //see midwinding in Q1_SplitBrush + //no face found yet + bestfacenum = -1; + //minimum face size + largestarea = 1; + //if optimizing the texture placement and not going for the + //least number of brushes + if ( !lessbrushes ) { + for ( i = 0; i < q1_numfaces; i++ ) + { + //the face must be in the same plane as the node plane that created + //this brush side + if ( q1_dfaces[i].planenum == q1_dnodes[sidenodenum].planenum ) { + //get the area the face and the brush side overlap + area = Q1_FaceOnWinding( &q1_dfaces[i], side->winding ); + //if this face overlaps the brush side winding more than previous faces + if ( area > largestarea ) { + //if there already was a face for texturing this brush side with + //a different texture + if ( bestfacenum >= 0 && + ( q1_dfaces[bestfacenum].texinfo != q1_dfaces[i].texinfo ) ) { + //split the brush to fit the texture + newbrushes = Q1_SplitBrushWithFace( brush, &q1_dfaces[i] ); + //if new brushes where created + if ( newbrushes ) { + //remove the current brush from the list + if ( prevbrush ) { + prevbrush->next = brush->next; + } else { brushlist = brush->next;} + if ( brushlistend == brush ) { + brushlistend = prevbrush; + nextbrush = newbrushes; + } //end if + //add the new brushes to the end of the list + if ( brushlistend ) { + brushlistend->next = newbrushes; + } else { brushlist = newbrushes;} + //free the current brush + FreeBrush( brush ); + //don't forget about the prevbrush pointer at the bottom of + //the outer loop + brush = prevbrush; + //find the end of the list + for ( brushlistend = brushlist; brushlistend; brushlistend = brushlistend->next ) + { + if ( !brushlistend->next ) { + break; + } + } //end for + break; + } //end if + else + { + Log_Write( "brush %d: no real texture split", numbrushes ); + } //end else + } //end if + else + { + //best face for texturing this brush side + bestfacenum = i; + } //end else + } //end if + } //end if + } //end for + //if the brush was split the original brush is removed + //and we just continue with the next one in the list + if ( i < q1_numfaces ) { + break; + } + } //end if + else + { + //find the face with the largest overlap with this brush side + //for texturing the brush side + for ( i = 0; i < q1_numfaces; i++ ) + { + //the face must be in the same plane as the node plane that created + //this brush side + if ( q1_dfaces[i].planenum == q1_dnodes[sidenodenum].planenum ) { + //get the area the face and the brush side overlap + area = Q1_FaceOnWinding( &q1_dfaces[i], side->winding ); + //if this face overlaps the brush side winding more than previous faces + if ( area > largestarea ) { + largestarea = area; + bestfacenum = i; + } //end if + } //end if + } //end for + } //end else + //if a face was found for texturing this brush side + if ( bestfacenum >= 0 ) { + //set the MAP texinfo values + texinfonum = q1_dfaces[bestfacenum].texinfo; + for ( n = 0; n < 4; n++ ) + { + map_texinfo[texinfonum].vecs[0][n] = q1_texinfo[texinfonum].vecs[0][n]; + map_texinfo[texinfonum].vecs[1][n] = q1_texinfo[texinfonum].vecs[1][n]; + } //end for + //make sure the two vectors aren't of zero length otherwise use the default + //vector to prevent a divide by zero in the map writing + if ( VectorLength( map_texinfo[texinfonum].vecs[0] ) < 0.01 ) { + memcpy( map_texinfo[texinfonum].vecs[0], defaultvec, sizeof( defaultvec ) ); + } + if ( VectorLength( map_texinfo[texinfonum].vecs[1] ) < 0.01 ) { + memcpy( map_texinfo[texinfonum].vecs[1], defaultvec, sizeof( defaultvec ) ); + } + // + map_texinfo[texinfonum].flags = q1_texinfo[texinfonum].flags; + map_texinfo[texinfonum].value = 0; //Q1 and HL texinfos don't have a value + //the mip texture + miptexlump = (q1_dmiptexlump_t *) q1_dtexdata; + miptex = ( q1_miptex_t * )( (byte *)miptexlump + miptexlump->dataofs[q1_texinfo[texinfonum].miptex] ); + //get the mip texture name + strcpy( map_texinfo[texinfonum].texture, miptex->name ); + //no animations in Quake1 and Half-Life mip textures + map_texinfo[texinfonum].nexttexinfo = -1; + //store the texinfo number + side->texinfo = texinfonum; + // + if ( texinfonum > map_numtexinfo ) { + map_numtexinfo = texinfonum; + } + //this side is textured + side->flags |= SFL_TEXTURED; + } //end if + else + { + //no texture for this side + side->texinfo = TEXINFO_NODE; + //this side is textured + side->flags |= SFL_TEXTURED; + } //end if + } //end for + // + if ( !modelnum && prevbrush != brush ) { + qprintf( "\r%5d", ++numbrushes ); + } + //previous brush in the list + prevbrush = brush; + } //end for + if ( !modelnum ) { + qprintf( "\n" ); + } + //return the new list with brushes + return brushlist; +} //end of the function Q1_TextureBrushes +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Q1_FixContentsTextures( bspbrush_t *brushlist ) { + int i, texinfonum; + bspbrush_t *brush; + + for ( brush = brushlist; brush; brush = brush->next ) + { + //only fix the textures of water, slime and lava brushes + if ( brush->side != CONTENTS_WATER && + brush->side != CONTENTS_SLIME && + brush->side != CONTENTS_LAVA ) { + continue; + } + // + for ( i = 0; i < brush->numsides; i++ ) + { + texinfonum = brush->sides[i].texinfo; + if ( Q1_TextureContents( map_texinfo[texinfonum].texture ) == brush->side ) { + break; + } + } //end for + //if no specific contents texture was found + if ( i >= brush->numsides ) { + texinfonum = -1; + for ( i = 0; i < map_numtexinfo; i++ ) + { + if ( Q1_TextureContents( map_texinfo[i].texture ) == brush->side ) { + texinfonum = i; + break; + } //end if + } //end for + } //end if + // + if ( texinfonum >= 0 ) { + //give all the brush sides this contents texture + for ( i = 0; i < brush->numsides; i++ ) + { + brush->sides[i].texinfo = texinfonum; + } //end for + } //end if + else {Log_Print( "brush contents %d with wrong textures\n", brush->side );} + // + } //end for + /* + for (brush = brushlist; brush; brush = brush->next) + { + //give all the brush sides this contents texture + for (i = 0; i < brush->numsides; i++) + { + if (Q1_TextureContents(map_texinfo[texinfonum].texture) != brush->side) + { + Error("brush contents %d with wrong contents textures %s\n", brush->side, + Q1_TextureContents(map_texinfo[texinfonum].texture)); + } //end if + } //end for + } //end for*/ +} //end of the function Q1_FixContentsTextures +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Q1_BSPBrushToMapBrush( bspbrush_t *bspbrush, entity_t *mapent ) { + mapbrush_t *mapbrush; + side_t *side; + int i, besttexinfo; + + CheckBSPBrush( bspbrush ); + + if ( nummapbrushes >= MAX_MAPFILE_BRUSHES ) { + Error( "nummapbrushes == MAX_MAPFILE_BRUSHES" ); + } + + mapbrush = &mapbrushes[nummapbrushes]; + mapbrush->original_sides = &brushsides[nummapbrushsides]; + mapbrush->entitynum = mapent - entities; + mapbrush->brushnum = nummapbrushes - mapent->firstbrush; + mapbrush->leafnum = -1; + mapbrush->numsides = 0; + + besttexinfo = TEXINFO_NODE; + for ( i = 0; i < bspbrush->numsides; i++ ) + { + if ( !bspbrush->sides[i].winding ) { + continue; + } + // + if ( nummapbrushsides >= MAX_MAPFILE_BRUSHSIDES ) { + Error( "MAX_MAPFILE_BRUSHSIDES" ); + } + side = &brushsides[nummapbrushsides]; + //the contents of the bsp brush is stored in the side variable + side->contents = bspbrush->side; + side->surf = 0; + side->planenum = bspbrush->sides[i].planenum; + side->texinfo = bspbrush->sides[i].texinfo; + if ( side->texinfo != TEXINFO_NODE ) { + //this brush side is textured + side->flags |= SFL_TEXTURED; + besttexinfo = side->texinfo; + } //end if + // + nummapbrushsides++; + mapbrush->numsides++; + } //end for + // + if ( besttexinfo == TEXINFO_NODE ) { + mapbrush->numsides = 0; + q1_numclipbrushes++; + return; + } //end if + //set the texinfo for all the brush sides without texture + for ( i = 0; i < mapbrush->numsides; i++ ) + { + if ( mapbrush->original_sides[i].texinfo == TEXINFO_NODE ) { + mapbrush->original_sides[i].texinfo = besttexinfo; + } //end if + } //end for + //contents of the brush + mapbrush->contents = bspbrush->side; + // + if ( create_aas ) { + //create the AAS brushes from this brush, add brush bevels + AAS_CreateMapBrushes( mapbrush, mapent, true ); + return; + } //end if + //create windings for sides and bounds for brush + MakeBrushWindings( mapbrush ); + //add brush bevels + AddBrushBevels( mapbrush ); + //a new brush has been created + nummapbrushes++; + mapent->numbrushes++; +} //end of the function Q1_BSPBrushToMapBrush +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Q1_CreateMapBrushes( entity_t *mapent, int modelnum ) { + bspbrush_t *brushlist, *brush, *nextbrush; + int i = 0; // TTimo: init + + //create brushes from the model BSP tree + brushlist = Q1_CreateBrushesFromBSP( modelnum ); + //texture the brushes and split them when necesary + brushlist = Q1_TextureBrushes( brushlist, modelnum ); + //fix the contents textures of all brushes + Q1_FixContentsTextures( brushlist ); + // + if ( !nobrushmerge ) { + brushlist = Q1_MergeBrushes( brushlist, modelnum ); + //brushlist = Q1_MergeBrushes(brushlist, modelnum); + } //end if + // + if ( !modelnum ) { + qprintf( "converting brushes to map brushes\n" ); + } + if ( !modelnum ) { + qprintf( "%5d brushes", i = 0 ); + } + for ( brush = brushlist; brush; brush = nextbrush ) + { + nextbrush = brush->next; + Q1_BSPBrushToMapBrush( brush, mapent ); + brush->next = NULL; + FreeBrush( brush ); + if ( !modelnum ) { + qprintf( "\r%5d", ++i ); + } + } //end for + if ( !modelnum ) { + qprintf( "\n" ); + } +} //end of the function Q1_CreateMapBrushes +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Q1_ResetMapLoading( void ) { +} //end of the function Q1_ResetMapLoading +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Q1_LoadMapFromBSP( char *filename, int offset, int length ) { + int i, modelnum; + char *model, *classname; + + Log_Print( "-- Q1_LoadMapFromBSP --\n" ); + //the loaded map type + loadedmaptype = MAPTYPE_QUAKE1; + // + qprintf( "loading map from %s at %d\n", filename, offset ); + //load the Half-Life BSP file + Q1_LoadBSPFile( filename, offset, length ); + // + q1_numclipbrushes = 0; + //CreatePath(path); + //Q1_CreateQ2WALFiles(path); + //parse the entities from the BSP + Q1_ParseEntities(); + //clear the map mins and maxs + ClearBounds( map_mins, map_maxs ); + // + qprintf( "creating Quake1 brushes\n" ); + if ( lessbrushes ) { + qprintf( "creating minimum number of brushes\n" ); + } else { qprintf( "placing textures correctly\n" );} + // + for ( i = 0; i < num_entities; i++ ) + { + entities[i].firstbrush = nummapbrushes; + entities[i].numbrushes = 0; + // + classname = ValueForKey( &entities[i], "classname" ); + if ( classname && !strcmp( classname, "worldspawn" ) ) { + modelnum = 0; + } //end if + else + { + // + model = ValueForKey( &entities[i], "model" ); + if ( !model || *model != '*' ) { + continue; + } + model++; + modelnum = atoi( model ); + } //end else + //create map brushes for the entity + Q1_CreateMapBrushes( &entities[i], modelnum ); + } //end for + // + qprintf( "%5d map brushes\n", nummapbrushes ); + qprintf( "%5d clip brushes\n", q1_numclipbrushes ); +} //end of the function Q1_LoadMapFromBSP diff --git a/src/bspc/map_q2.c b/src/bspc/map_q2.c new file mode 100644 index 0000000..1603579 --- /dev/null +++ b/src/bspc/map_q2.c @@ -0,0 +1,1147 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: map_q2.c +// Function: map loading and writing +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-03 +// Tab Size: 3 +//=========================================================================== + +//=========================================================================== +// ANSI, Area Navigational System Interface +// AAS, Area Awareness System +//=========================================================================== + +#include "qbsp.h" +#include "l_mem.h" +#include "../botlib/aasfile.h" //aas_bbox_t +#include "aas_store.h" //AAS_MAX_BBOXES +#include "aas_cfg.h" +#include "aas_map.h" //AAS_CreateMapBrushes +#include "l_bsp_q2.h" + +#ifdef ME + +#define NODESTACKSIZE 1024 + +int nodestack[NODESTACKSIZE]; +int *nodestackptr; +int nodestacksize = 0; +int brushmodelnumbers[MAX_MAPFILE_BRUSHES]; +int dbrushleafnums[MAX_MAPFILE_BRUSHES]; +int dplanes2mapplanes[MAX_MAPFILE_PLANES]; + +#endif //ME + +//==================================================================== + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Q2_CreateMapTexinfo( void ) { + int i; + + for ( i = 0; i < numtexinfo; i++ ) + { + memcpy( map_texinfo[i].vecs, texinfo[i].vecs, sizeof( float ) * 2 * 4 ); + map_texinfo[i].flags = texinfo[i].flags; + map_texinfo[i].value = texinfo[i].value; + strcpy( map_texinfo[i].texture, texinfo[i].texture ); + map_texinfo[i].nexttexinfo = 0; + } //end for +} //end of the function Q2_CreateMapTexinfo + +/* +=========== +Q2_BrushContents +=========== +*/ +int Q2_BrushContents( mapbrush_t *b ) { + int contents; + side_t *s; + int i; + int trans; + + s = &b->original_sides[0]; + contents = s->contents; + trans = texinfo[s->texinfo].flags; + for ( i = 1; i < b->numsides; i++, s++ ) + { + s = &b->original_sides[i]; + trans |= texinfo[s->texinfo].flags; + if ( s->contents != contents ) { + Log_Print( "Entity %i, Brush %i: mixed face contents\n" + , b->entitynum, b->brushnum ); + Log_Print( "texture name = %s\n", texinfo[s->texinfo].texture ); + break; + } + } + + // if any side is translucent, mark the contents + // and change solid to window + if ( trans & ( SURF_TRANS33 | SURF_TRANS66 ) ) { + contents |= CONTENTS_Q2TRANSLUCENT; + if ( contents & CONTENTS_SOLID ) { + contents &= ~CONTENTS_SOLID; + contents |= CONTENTS_WINDOW; + } + } + + return contents; +} + +#ifdef ME + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void MakeAreaPortalBrush( mapbrush_t *brush ) { + int sn; + side_t *s; + + brush->contents = CONTENTS_AREAPORTAL; + + for ( sn = 0; sn < brush->numsides; sn++ ) + { + s = brush->original_sides + sn; + //make sure the surfaces are not hint or skip + s->surf &= ~( SURF_HINT | SURF_SKIP ); + // + s->texinfo = 0; + s->contents = CONTENTS_AREAPORTAL; + } //end for +} //end of the function MakeAreaPortalBrush +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void DPlanes2MapPlanes( void ) { + int i; + + for ( i = 0; i < numplanes; i++ ) + { + dplanes2mapplanes[i] = FindFloatPlane( dplanes[i].normal, dplanes[i].dist ); + } //end for +} //end of the function DPlanes2MapPlanes +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void MarkVisibleBrushSides( mapbrush_t *brush ) { + int n, i, planenum; + side_t *side; + dface_t *face; + // + for ( n = 0; n < brush->numsides; n++ ) + { + side = brush->original_sides + n; + //if this side is a bevel or the leaf number of the brush is unknown + if ( ( side->flags & SFL_BEVEL ) || brush->leafnum < 0 ) { + //this side is a valid splitter + side->flags |= SFL_VISIBLE; + continue; + } //end if + //assum this side will not be used as a splitter + side->flags &= ~SFL_VISIBLE; + //check if the side plane is used by a visible face + for ( i = 0; i < numfaces; i++ ) + { + face = &dfaces[i]; + planenum = dplanes2mapplanes[face->planenum]; + if ( ( planenum & ~1 ) == ( side->planenum & ~1 ) ) { + //this side is a valid splitter + side->flags |= SFL_VISIBLE; + } //end if + } //end for + } //end for +} //end of the function MarkVisibleBrushSides + +#endif //ME + +/* +================= +Q2_ParseBrush +================= +*/ +void Q2_ParseBrush( script_t *script, entity_t *mapent ) { + mapbrush_t *b; + int i, j, k; + int mt; + side_t *side, *s2; + int planenum; + brush_texture_t td; + int planepts[3][3]; + token_t token; + + if ( nummapbrushes >= MAX_MAPFILE_BRUSHES ) { + Error( "nummapbrushes == MAX_MAPFILE_BRUSHES" ); + } + + b = &mapbrushes[nummapbrushes]; + b->original_sides = &brushsides[nummapbrushsides]; + b->entitynum = num_entities - 1; + b->brushnum = nummapbrushes - mapent->firstbrush; + b->leafnum = -1; + + do + { + if ( !PS_ReadToken( script, &token ) ) { + break; + } + if ( !strcmp( token.string, "}" ) ) { + break; + } + + //IDBUG: mixed use of MAX_MAPFILE_? and MAX_MAP_? this could + // lead to out of bound indexing of the arrays + if ( nummapbrushsides >= MAX_MAPFILE_BRUSHSIDES ) { + Error( "MAX_MAPFILE_BRUSHSIDES" ); + } + side = &brushsides[nummapbrushsides]; + + //read the three point plane definition + for ( i = 0; i < 3; i++ ) + { + if ( i != 0 ) { + PS_ExpectTokenString( script, "(" ); + } + for ( j = 0; j < 3; j++ ) + { + PS_ExpectAnyToken( script, &token ); + planepts[i][j] = atof( token.string ); + } //end for + PS_ExpectTokenString( script, ")" ); + } //end for + + // + //read the texturedef + // + PS_ExpectAnyToken( script, &token ); + strcpy( td.name, token.string ); + + PS_ExpectAnyToken( script, &token ); + td.shift[0] = atol( token.string ); + PS_ExpectAnyToken( script, &token ); + td.shift[1] = atol( token.string ); + PS_ExpectAnyToken( script, &token ); + td.rotate = atol( token.string ); + PS_ExpectAnyToken( script, &token ); + td.scale[0] = atof( token.string ); + PS_ExpectAnyToken( script, &token ); + td.scale[1] = atof( token.string ); + + //find default flags and values + mt = FindMiptex( td.name ); + td.flags = textureref[mt].flags; + td.value = textureref[mt].value; + side->contents = textureref[mt].contents; + side->surf = td.flags = textureref[mt].flags; + + //check if there's a number available + if ( PS_CheckTokenType( script, TT_NUMBER, 0, &token ) ) { + side->contents = token.intvalue; + PS_ExpectTokenType( script, TT_NUMBER, 0, &token ); + side->surf = td.flags = token.intvalue; + PS_ExpectTokenType( script, TT_NUMBER, 0, &token ); + td.value = token.intvalue; + } + + // translucent objects are automatically classified as detail + if ( side->surf & ( SURF_TRANS33 | SURF_TRANS66 ) ) { + side->contents |= CONTENTS_DETAIL; + } + if ( side->contents & ( CONTENTS_PLAYERCLIP | CONTENTS_MONSTERCLIP ) ) { + side->contents |= CONTENTS_DETAIL; + } + if ( fulldetail ) { + side->contents &= ~CONTENTS_DETAIL; + } + if ( !( side->contents & ( ( LAST_VISIBLE_CONTENTS - 1 ) + | CONTENTS_PLAYERCLIP | CONTENTS_MONSTERCLIP | CONTENTS_MIST ) ) ) { + side->contents |= CONTENTS_SOLID; + } + + // hints and skips are never detail, and have no content + if ( side->surf & ( SURF_HINT | SURF_SKIP ) ) { + side->contents = 0; + side->surf &= ~CONTENTS_DETAIL; + } + +#ifdef ME + //for creating AAS... this side is textured + side->flags |= SFL_TEXTURED; +#endif //ME + // + // find the plane number + // + planenum = PlaneFromPoints( planepts[0], planepts[1], planepts[2] ); + if ( planenum == -1 ) { + Log_Print( "Entity %i, Brush %i: plane with no normal\n" + , b->entitynum, b->brushnum ); + continue; + } + + // + // see if the plane has been used already + // + for ( k = 0 ; k < b->numsides ; k++ ) + { + s2 = b->original_sides + k; + if ( s2->planenum == planenum ) { + Log_Print( "Entity %i, Brush %i: duplicate plane\n" + , b->entitynum, b->brushnum ); + break; + } + if ( s2->planenum == ( planenum ^ 1 ) ) { + Log_Print( "Entity %i, Brush %i: mirrored plane\n" + , b->entitynum, b->brushnum ); + break; + } + } + if ( k != b->numsides ) { + continue; // duplicated + + } + // + // keep this side + // + + side = b->original_sides + b->numsides; + side->planenum = planenum; + side->texinfo = TexinfoForBrushTexture( &mapplanes[planenum], + &td, vec3_origin ); + + // save the td off in case there is an origin brush and we + // have to recalculate the texinfo + side_brushtextures[nummapbrushsides] = td; + + nummapbrushsides++; + b->numsides++; + } while ( 1 ); + + // get the content for the entire brush + b->contents = Q2_BrushContents( b ); + +#ifdef ME + if ( BrushExists( b ) ) { + c_squattbrushes++; + b->numsides = 0; + return; + } //end if + + if ( create_aas ) { + //create AAS brushes, and add brush bevels + AAS_CreateMapBrushes( b, mapent, true ); + //NOTE: if we return here then duplicate plane errors occur for the non world entities + return; + } //end if +#endif //ME + + // allow detail brushes to be removed + if ( nodetail && ( b->contents & CONTENTS_DETAIL ) ) { + b->numsides = 0; + return; + } + + // allow water brushes to be removed + if ( nowater && ( b->contents & ( CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER ) ) ) { + b->numsides = 0; + return; + } + + // create windings for sides and bounds for brush + MakeBrushWindings( b ); + + // brushes that will not be visible at all will never be + // used as bsp splitters + if ( b->contents & ( CONTENTS_PLAYERCLIP | CONTENTS_MONSTERCLIP ) ) { + c_clipbrushes++; + for ( i = 0 ; i < b->numsides ; i++ ) + b->original_sides[i].texinfo = TEXINFO_NODE; + } + + // + // origin brushes are removed, but they set + // the rotation origin for the rest of the brushes + // in the entity. After the entire entity is parsed, + // the planenums and texinfos will be adjusted for + // the origin brush + // + if ( b->contents & CONTENTS_ORIGIN ) { + char string[32]; + vec3_t origin; + + if ( num_entities == 1 ) { + Error( "Entity %i, Brush %i: origin brushes not allowed in world" + , b->entitynum, b->brushnum ); + return; + } + + VectorAdd( b->mins, b->maxs, origin ); + VectorScale( origin, 0.5, origin ); + + sprintf( string, "%i %i %i", (int)origin[0], (int)origin[1], (int)origin[2] ); + SetKeyValue( &entities[b->entitynum], "origin", string ); + + VectorCopy( origin, entities[b->entitynum].origin ); + + // don't keep this brush + b->numsides = 0; + + return; + } + + AddBrushBevels( b ); + + nummapbrushes++; + mapent->numbrushes++; +} + +/* +================ +Q2_MoveBrushesToWorld + +Takes all of the brushes from the current entity and +adds them to the world's brush list. + +Used by func_group and func_areaportal +================ +*/ +void Q2_MoveBrushesToWorld( entity_t *mapent ) { + int newbrushes; + int worldbrushes; + mapbrush_t *temp; + int i; + + // this is pretty gross, because the brushes are expected to be + // in linear order for each entity + + newbrushes = mapent->numbrushes; + worldbrushes = entities[0].numbrushes; + + temp = GetMemory( newbrushes * sizeof( mapbrush_t ) ); + memcpy( temp, mapbrushes + mapent->firstbrush, newbrushes * sizeof( mapbrush_t ) ); + +#if 0 // let them keep their original brush numbers + for ( i = 0 ; i < newbrushes ; i++ ) + temp[i].entitynum = 0; +#endif + + // make space to move the brushes (overlapped copy) + memmove( mapbrushes + worldbrushes + newbrushes, + mapbrushes + worldbrushes, + sizeof( mapbrush_t ) * ( nummapbrushes - worldbrushes - newbrushes ) ); + + // copy the new brushes down + memcpy( mapbrushes + worldbrushes, temp, sizeof( mapbrush_t ) * newbrushes ); + + // fix up indexes + entities[0].numbrushes += newbrushes; + for ( i = 1 ; i < num_entities ; i++ ) + entities[i].firstbrush += newbrushes; + FreeMemory( temp ); + + mapent->numbrushes = 0; +} + +/* +================ +Q2_ParseMapEntity +================ +*/ +qboolean Q2_ParseMapEntity( script_t *script ) { + entity_t *mapent; + epair_t *e; + side_t *s; + int i, j; + int startbrush, startsides; + vec_t newdist; + mapbrush_t *b; + token_t token; + + if ( !PS_ReadToken( script, &token ) ) { + return false; + } + + if ( strcmp( token.string, "{" ) ) { + Error( "ParseEntity: { not found" ); + } + + if ( num_entities == MAX_MAP_ENTITIES ) { + Error( "num_entities == MAX_MAP_ENTITIES" ); + } + + startbrush = nummapbrushes; + startsides = nummapbrushsides; + + mapent = &entities[num_entities]; + num_entities++; + memset( mapent, 0, sizeof( *mapent ) ); + mapent->firstbrush = nummapbrushes; + mapent->numbrushes = 0; +// mapent->portalareas[0] = -1; +// mapent->portalareas[1] = -1; + + do + { + if ( !PS_ReadToken( script, &token ) ) { + Error( "ParseEntity: EOF without closing brace" ); + } //end if + if ( !strcmp( token.string, "}" ) ) { + break; + } + if ( !strcmp( token.string, "{" ) ) { + Q2_ParseBrush( script, mapent ); + } //end if + else + { + PS_UnreadLastToken( script ); + e = ParseEpair( script ); + e->next = mapent->epairs; + mapent->epairs = e; + } //end else + } while ( 1 ); + + GetVectorForKey( mapent, "origin", mapent->origin ); + + // + // if there was an origin brush, offset all of the planes and texinfo + // + if ( mapent->origin[0] || mapent->origin[1] || mapent->origin[2] ) { + for ( i = 0 ; i < mapent->numbrushes ; i++ ) + { + b = &mapbrushes[mapent->firstbrush + i]; + for ( j = 0 ; j < b->numsides ; j++ ) + { + s = &b->original_sides[j]; + newdist = mapplanes[s->planenum].dist - + DotProduct( mapplanes[s->planenum].normal, mapent->origin ); + s->planenum = FindFloatPlane( mapplanes[s->planenum].normal, newdist ); + s->texinfo = TexinfoForBrushTexture( &mapplanes[s->planenum], + &side_brushtextures[s - brushsides], mapent->origin ); + } + MakeBrushWindings( b ); + } + } + + // group entities are just for editor convenience + // toss all brushes into the world entity + if ( !strcmp( "func_group", ValueForKey( mapent, "classname" ) ) ) { + Q2_MoveBrushesToWorld( mapent ); + mapent->numbrushes = 0; + return true; + } + + // areaportal entities move their brushes, but don't eliminate + // the entity + if ( !strcmp( "func_areaportal", ValueForKey( mapent, "classname" ) ) ) { + char str[128]; + + if ( mapent->numbrushes != 1 ) { + Error( "Entity %i: func_areaportal can only be a single brush", num_entities - 1 ); + } + + b = &mapbrushes[nummapbrushes - 1]; + b->contents = CONTENTS_AREAPORTAL; + c_areaportals++; + mapent->areaportalnum = c_areaportals; + // set the portal number as "style" + sprintf( str, "%i", c_areaportals ); + SetKeyValue( mapent, "style", str ); + Q2_MoveBrushesToWorld( mapent ); + return true; + } + + return true; +} + +//=================================================================== + +/* +================ +LoadMapFile +================ +*/ +void Q2_LoadMapFile( char *filename ) { + int i; + script_t *script; + + Log_Print( "-- Q2_LoadMapFile --\n" ); +#ifdef ME + //loaded map type + loadedmaptype = MAPTYPE_QUAKE2; + //reset the map loading + ResetMapLoading(); +#endif //ME + + script = LoadScriptFile( filename ); + if ( !script ) { + Log_Print( "couldn't open %s\n", filename ); + return; + } //end if + //white spaces and escape characters inside a string are not allowed + SetScriptFlags( script, SCFL_NOSTRINGWHITESPACES | + SCFL_NOSTRINGESCAPECHARS | + SCFL_PRIMITIVE ); + + nummapbrushsides = 0; + num_entities = 0; + + while ( Q2_ParseMapEntity( script ) ) + { + } + + ClearBounds( map_mins, map_maxs ); + for ( i = 0 ; i < entities[0].numbrushes ; i++ ) + { + if ( mapbrushes[i].mins[0] > 4096 ) { + continue; // no valid points + } + AddPointToBounds( mapbrushes[i].mins, map_mins, map_maxs ); + AddPointToBounds( mapbrushes[i].maxs, map_mins, map_maxs ); + } //end for + + PrintMapInfo(); + + //free the script + FreeScript( script ); +// TestExpandBrushes (); + // + Q2_CreateMapTexinfo(); +} //end of the function Q2_LoadMapFile + +#ifdef ME //Begin MAP loading from BSP file +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Q2_SetLeafBrushesModelNumbers( int leafnum, int modelnum ) { + int i, brushnum; + dleaf_t *leaf; + + leaf = &dleafs[leafnum]; + for ( i = 0; i < leaf->numleafbrushes; i++ ) + { + brushnum = dleafbrushes[leaf->firstleafbrush + i]; + brushmodelnumbers[brushnum] = modelnum; + dbrushleafnums[brushnum] = leafnum; + } //end for +} //end of the function Q2_SetLeafBrushesModelNumbers +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Q2_InitNodeStack( void ) { + nodestackptr = nodestack; + nodestacksize = 0; +} //end of the function Q2_InitNodeStack +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Q2_PushNodeStack( int num ) { + *nodestackptr = num; + nodestackptr++; + nodestacksize++; + // + if ( nodestackptr >= &nodestack[NODESTACKSIZE] ) { + Error( "Q2_PushNodeStack: stack overflow\n" ); + } //end if +} //end of the function Q2_PushNodeStack +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int Q2_PopNodeStack( void ) { + //if the stack is empty + if ( nodestackptr <= nodestack ) { + return -1; + } + //decrease stack pointer + nodestackptr--; + nodestacksize--; + //return the top value from the stack + return *nodestackptr; +} //end of the function Q2_PopNodeStack +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Q2_SetBrushModelNumbers( entity_t *mapent ) { + int n, pn; + int leafnum; + + // + Q2_InitNodeStack(); + //head node (root) of the bsp tree + n = dmodels[mapent->modelnum].headnode; + pn = 0; + + do + { + //if we are in a leaf (negative node number) + if ( n < 0 ) { + //number of the leaf + leafnum = ( -n ) - 1; + //set the brush numbers + Q2_SetLeafBrushesModelNumbers( leafnum, mapent->modelnum ); + //walk back into the tree to find a second child to continue with + for ( pn = Q2_PopNodeStack(); pn >= 0; n = pn, pn = Q2_PopNodeStack() ) + { + //if we took the first child at the parent node + if ( dnodes[pn].children[0] == n ) { + break; + } + } //end for + //if the stack wasn't empty (if not processed whole tree) + if ( pn >= 0 ) { + //push the parent node again + Q2_PushNodeStack( pn ); + //we proceed with the second child of the parent node + n = dnodes[pn].children[1]; + } //end if + } //end if + else + { + //push the current node onto the stack + Q2_PushNodeStack( n ); + //walk forward into the tree to the first child + n = dnodes[n].children[0]; + } //end else + } while ( pn >= 0 ); +} //end of the function Q2_SetBrushModelNumbers +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Q2_BSPBrushToMapBrush( dbrush_t *bspbrush, entity_t *mapent ) { + mapbrush_t *b; + int i, k, n; + side_t *side, *s2; + int planenum; + dbrushside_t *bspbrushside; + dplane_t *bspplane; + + if ( nummapbrushes >= MAX_MAPFILE_BRUSHES ) { + Error( "nummapbrushes >= MAX_MAPFILE_BRUSHES" ); + } + + b = &mapbrushes[nummapbrushes]; + b->original_sides = &brushsides[nummapbrushsides]; + b->entitynum = mapent - entities; + b->brushnum = nummapbrushes - mapent->firstbrush; + b->leafnum = dbrushleafnums[bspbrush - dbrushes]; + + for ( n = 0; n < bspbrush->numsides; n++ ) + { + //pointer to the bsp brush side + bspbrushside = &dbrushsides[bspbrush->firstside + n]; + + if ( nummapbrushsides >= MAX_MAPFILE_BRUSHSIDES ) { + Error( "MAX_MAPFILE_BRUSHSIDES" ); + } //end if + //pointer to the map brush side + side = &brushsides[nummapbrushsides]; + //if the BSP brush side is textured + if ( brushsidetextured[bspbrush->firstside + n] ) { + side->flags |= SFL_TEXTURED; + } else { side->flags &= ~SFL_TEXTURED;} + //ME: can get side contents and surf directly from BSP file + side->contents = bspbrush->contents; + //if the texinfo is TEXINFO_NODE + if ( bspbrushside->texinfo < 0 ) { + side->surf = 0; + } else { side->surf = texinfo[bspbrushside->texinfo].flags;} + + // translucent objects are automatically classified as detail + if ( side->surf & ( SURF_TRANS33 | SURF_TRANS66 ) ) { + side->contents |= CONTENTS_DETAIL; + } + if ( side->contents & ( CONTENTS_PLAYERCLIP | CONTENTS_MONSTERCLIP ) ) { + side->contents |= CONTENTS_DETAIL; + } + if ( fulldetail ) { + side->contents &= ~CONTENTS_DETAIL; + } + if ( !( side->contents & ( ( LAST_VISIBLE_CONTENTS - 1 ) + | CONTENTS_PLAYERCLIP | CONTENTS_MONSTERCLIP | CONTENTS_MIST ) ) ) { + side->contents |= CONTENTS_SOLID; + } + + // hints and skips are never detail, and have no content + if ( side->surf & ( SURF_HINT | SURF_SKIP ) ) { + side->contents = 0; + side->surf &= ~CONTENTS_DETAIL; + } + + //ME: get a plane for this side + bspplane = &dplanes[bspbrushside->planenum]; + planenum = FindFloatPlane( bspplane->normal, bspplane->dist ); + // + // see if the plane has been used already + // + //ME: this really shouldn't happen!!! + //ME: otherwise the bsp file is corrupted?? + //ME: still it seems to happen, maybe Johny Boy's + //ME: brush bevel adding is crappy ? + for ( k = 0; k < b->numsides; k++ ) + { + s2 = b->original_sides + k; +// if (DotProduct (mapplanes[s2->planenum].normal, mapplanes[planenum].normal) > 0.999 +// && fabs(mapplanes[s2->planenum].dist - mapplanes[planenum].dist) < 0.01 ) + + if ( s2->planenum == planenum ) { + Log_Print( "Entity %i, Brush %i: duplicate plane\n" + , b->entitynum, b->brushnum ); + break; + } + if ( s2->planenum == ( planenum ^ 1 ) ) { + Log_Print( "Entity %i, Brush %i: mirrored plane\n" + , b->entitynum, b->brushnum ); + break; + } + } + if ( k != b->numsides ) { + continue; // duplicated + + } + // + // keep this side + // + //ME: reset pointer to side, why? hell I dunno (pointer is set above already) + side = b->original_sides + b->numsides; + //ME: store the plane number + side->planenum = planenum; + //ME: texinfo is already stored when bsp is loaded + //NOTE: check for TEXINFO_NODE, otherwise crash in Q2_BrushContents + if ( bspbrushside->texinfo < 0 ) { + side->texinfo = 0; + } else { side->texinfo = bspbrushside->texinfo;} + + // save the td off in case there is an origin brush and we + // have to recalculate the texinfo + // ME: don't need to recalculate because it's already done + // (for non-world entities) in the BSP file +// side_brushtextures[nummapbrushsides] = td; + + nummapbrushsides++; + b->numsides++; + } //end for + + // get the content for the entire brush + b->contents = bspbrush->contents; + Q2_BrushContents( b ); + + if ( BrushExists( b ) ) { + c_squattbrushes++; + b->numsides = 0; + return; + } //end if + + //if we're creating AAS + if ( create_aas ) { + //create the AAS brushes from this brush, don't add brush bevels + AAS_CreateMapBrushes( b, mapent, false ); + return; + } //end if + + // allow detail brushes to be removed + if ( nodetail && ( b->contents & CONTENTS_DETAIL ) ) { + b->numsides = 0; + return; + } //end if + + // allow water brushes to be removed + if ( nowater && ( b->contents & ( CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER ) ) ) { + b->numsides = 0; + return; + } //end if + + // create windings for sides and bounds for brush + MakeBrushWindings( b ); + + //mark brushes without winding or with a tiny window as bevels + MarkBrushBevels( b ); + + // brushes that will not be visible at all will never be + // used as bsp splitters + if ( b->contents & ( CONTENTS_PLAYERCLIP | CONTENTS_MONSTERCLIP ) ) { + c_clipbrushes++; + for ( i = 0; i < b->numsides; i++ ) + b->original_sides[i].texinfo = TEXINFO_NODE; + } //end for + + // + // origin brushes are removed, but they set + // the rotation origin for the rest of the brushes + // in the entity. After the entire entity is parsed, + // the planenums and texinfos will be adjusted for + // the origin brush + // + //ME: not needed because the entities in the BSP file already + // have an origin set +// if (b->contents & CONTENTS_ORIGIN) +// { +// char string[32]; +// vec3_t origin; +// +// if (num_entities == 1) +// { +// Error ("Entity %i, Brush %i: origin brushes not allowed in world" +// , b->entitynum, b->brushnum); +// return; +// } +// +// VectorAdd (b->mins, b->maxs, origin); +// VectorScale (origin, 0.5, origin); +// +// sprintf (string, "%i %i %i", (int)origin[0], (int)origin[1], (int)origin[2]); +// SetKeyValue (&entities[b->entitynum], "origin", string); +// +// VectorCopy (origin, entities[b->entitynum].origin); +// +// // don't keep this brush +// b->numsides = 0; +// +// return; +// } + + //ME: the bsp brushes already have bevels, so we won't try to + // add them again (especially since Johny Boy's bevel adding might + // be crappy) +// AddBrushBevels(b); + + nummapbrushes++; + mapent->numbrushes++; +} //end of the function Q2_BSPBrushToMapBrush +//=========================================================================== +//=========================================================================== +void Q2_ParseBSPBrushes( entity_t *mapent ) { + int i; + + //give all the brushes that belong to this entity the number of the + //BSP model used by this entity + Q2_SetBrushModelNumbers( mapent ); + //now parse all the brushes with the correct mapent->modelnum + for ( i = 0; i < numbrushes; i++ ) + { + if ( brushmodelnumbers[i] == mapent->modelnum ) { + Q2_BSPBrushToMapBrush( &dbrushes[i], mapent ); + } //end if + } //end for +} //end of the function Q2_ParseBSPBrushes +//=========================================================================== +//=========================================================================== +qboolean Q2_ParseBSPEntity( int entnum ) { + entity_t *mapent; + char *model; + int startbrush, startsides; + + startbrush = nummapbrushes; + startsides = nummapbrushsides; + + mapent = &entities[entnum]; //num_entities]; + mapent->firstbrush = nummapbrushes; + mapent->numbrushes = 0; + mapent->modelnum = -1; //-1 = no model + + model = ValueForKey( mapent, "model" ); + if ( model && strlen( model ) ) { + if ( *model != '*' ) { + Error( "Q2_ParseBSPEntity: model number without leading *" ); + } //end if + //get the model number of this entity (skip the leading *) + mapent->modelnum = atoi( &model[1] ); + } //end if + + GetVectorForKey( mapent, "origin", mapent->origin ); + + //if this is the world entity it has model number zero + //the world entity has no model key + if ( !strcmp( "worldspawn", ValueForKey( mapent, "classname" ) ) ) { + mapent->modelnum = 0; + } //end if + //if the map entity has a BSP model (a modelnum of -1 is used for + //entities that aren't using a BSP model) + if ( mapent->modelnum >= 0 ) { + //parse the bsp brushes + Q2_ParseBSPBrushes( mapent ); + } //end if + // + //the origin of the entity is already taken into account + // + //func_group entities can't be in the bsp file + // + //check out the func_areaportal entities + if ( !strcmp( "func_areaportal", ValueForKey( mapent, "classname" ) ) ) { + c_areaportals++; + mapent->areaportalnum = c_areaportals; + return true; + } //end if + return true; +} //end of the function Q2_ParseBSPEntity +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Q2_LoadMapFromBSP( char *filename, int offset, int length ) { + int i; + + Log_Print( "-- Q2_LoadMapFromBSP --\n" ); + //loaded map type + loadedmaptype = MAPTYPE_QUAKE2; + + Log_Print( "Loading map from %s...\n", filename ); + //load the bsp file + Q2_LoadBSPFile( filename, offset, length ); + + //create an index from bsp planes to map planes + //DPlanes2MapPlanes(); + //clear brush model numbers + for ( i = 0; i < MAX_MAPFILE_BRUSHES; i++ ) + brushmodelnumbers[i] = -1; + + nummapbrushsides = 0; + num_entities = 0; + + Q2_ParseEntities(); + // + for ( i = 0; i < num_entities; i++ ) + { + Q2_ParseBSPEntity( i ); + } //end for + + //get the map mins and maxs from the world model + ClearBounds( map_mins, map_maxs ); + for ( i = 0; i < entities[0].numbrushes; i++ ) + { + if ( mapbrushes[i].mins[0] > 4096 ) { + continue; //no valid points + } + AddPointToBounds( mapbrushes[i].mins, map_mins, map_maxs ); + AddPointToBounds( mapbrushes[i].maxs, map_mins, map_maxs ); + } //end for + + PrintMapInfo(); + // + Q2_CreateMapTexinfo(); +} //end of the function Q2_LoadMapFromBSP + +void Q2_ResetMapLoading( void ) { + //reset for map loading from bsp + memset( nodestack, 0, NODESTACKSIZE * sizeof( int ) ); + nodestackptr = NULL; + nodestacksize = 0; + memset( brushmodelnumbers, 0, MAX_MAPFILE_BRUSHES * sizeof( int ) ); +} //end of the function Q2_ResetMapLoading + +//End MAP loading from BSP file +#endif //ME + +//==================================================================== + +/* +================ +TestExpandBrushes + +Expands all the brush planes and saves a new map out +================ +*/ +void TestExpandBrushes( void ) { + FILE *f; + side_t *s; + int i, j, bn; + winding_t *w; + char *name = "expanded.map"; + mapbrush_t *brush; + vec_t dist; + + Log_Print( "writing %s\n", name ); + f = fopen( name, "wb" ); + if ( !f ) { + Error( "Can't write %s\n", name ); + } + + fprintf( f, "{\n\"classname\" \"worldspawn\"\n" ); + + for ( bn = 0 ; bn < nummapbrushes ; bn++ ) + { + brush = &mapbrushes[bn]; + fprintf( f, "{\n" ); + for ( i = 0 ; i < brush->numsides ; i++ ) + { + s = brush->original_sides + i; + dist = mapplanes[s->planenum].dist; + for ( j = 0 ; j < 3 ; j++ ) + dist += fabs( 16 * mapplanes[s->planenum].normal[j] ); + + w = BaseWindingForPlane( mapplanes[s->planenum].normal, dist ); + + fprintf( f,"( %i %i %i ) ", (int)w->p[0][0], (int)w->p[0][1], (int)w->p[0][2] ); + fprintf( f,"( %i %i %i ) ", (int)w->p[1][0], (int)w->p[1][1], (int)w->p[1][2] ); + fprintf( f,"( %i %i %i ) ", (int)w->p[2][0], (int)w->p[2][1], (int)w->p[2][2] ); + + fprintf( f, "%s 0 0 0 1 1\n", texinfo[s->texinfo].texture ); + FreeWinding( w ); + } + fprintf( f, "}\n" ); + } + fprintf( f, "}\n" ); + + fclose( f ); + + Error( "can't proceed after expanding brushes" ); +} //end of the function TestExpandBrushes + diff --git a/src/bspc/map_q3.c b/src/bspc/map_q3.c new file mode 100644 index 0000000..5cc64a6 --- /dev/null +++ b/src/bspc/map_q3.c @@ -0,0 +1,729 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: map_q3.c +// Function: map loading +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1999-07-02 +// Tab Size: 3 +//=========================================================================== + +#include "qbsp.h" +#include "l_mem.h" +#include "../botlib/aasfile.h" //aas_bbox_t +#include "aas_store.h" //AAS_MAX_BBOXES +#include "aas_cfg.h" +#include "aas_map.h" //AAS_CreateMapBrushes +#include "l_bsp_q3.h" +#include "../qcommon/cm_patch.h" +#include "../game/surfaceflags.h" + +#define NODESTACKSIZE 1024 +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void PrintContents( int contents ); + +int Q3_BrushContents( mapbrush_t *b ) { + int contents, i, mixed, hint; + side_t *s; + + s = &b->original_sides[0]; + contents = s->contents; + // + mixed = false; + hint = false; + for ( i = 1; i < b->numsides; i++ ) + { + s = &b->original_sides[i]; + if ( s->contents != contents ) { + mixed = true; + } + if ( s->surf & ( SURF_HINT | SURF_SKIP ) ) { + hint = true; + } + contents |= s->contents; + } //end for + // + if ( hint ) { + if ( contents ) { + Log_Write( "WARNING: hint brush with contents: " ); + PrintContents( contents ); + Log_Write( "\r\n" ); + // + Log_Write( "brush contents is: " ); + PrintContents( b->contents ); + Log_Write( "\r\n" ); + } //end if + return 0; + } //end if + //Log_Write("brush %d contents ", nummapbrushes); + //PrintContents(contents); + //Log_Write("\r\n"); + //remove ladder and fog contents + contents &= ~( CONTENTS_LADDER | CONTENTS_FOG ); + // + if ( mixed ) { + Log_Write( "Entity %i, Brush %i: mixed face contents " + , b->entitynum, b->brushnum ); + PrintContents( contents ); + Log_Write( "\r\n" ); + // + Log_Write( "brush contents is: " ); + PrintContents( b->contents ); + Log_Write( "\r\n" ); + // + if ( contents & CONTENTS_DONOTENTER ) { + return CONTENTS_DONOTENTER; //Log_Print("mixed contents with donotenter\n"); + } + /* + Log_Print("contents:"); PrintContents(contents); + Log_Print("\ncontents:"); PrintContents(s->contents); + Log_Print("\n"); + Log_Print("texture name = %s\n", texinfo[s->texinfo].texture); + */ + //if liquid brush + if ( contents & ( CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER ) ) { + return ( contents & ( CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER ) ); + } //end if + if ( contents & CONTENTS_PLAYERCLIP ) { + return ( contents & CONTENTS_PLAYERCLIP ); + } + return ( contents & CONTENTS_SOLID ); + } //end if + /* + if (contents & CONTENTS_AREAPORTAL) + { + static int num; + Log_Write("Entity %i, Brush %i: area portal %d\r\n", b->entitynum, b->brushnum, num++); + } //end if*/ + if ( contents == ( contents & CONTENTS_STRUCTURAL ) ) { + //Log_Print("brush %i is only structural\n", b->brushnum); + contents = 0; + } //end if + if ( contents & CONTENTS_DONOTENTER ) { + Log_Print( "brush %i is a donotenter brush, c = %X\n", b->brushnum, contents ); + } //end if + return contents; +} //end of the function Q3_BrushContents +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Q3_DPlanes2MapPlanes( void ) { + int i; + + for ( i = 0; i < q3_numplanes; i++ ) + { + dplanes2mapplanes[i] = FindFloatPlane( q3_dplanes[i].normal, q3_dplanes[i].dist ); + } //end for +} //end of the function Q3_DPlanes2MapPlanes +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Q3_BSPBrushToMapBrush( q3_dbrush_t *bspbrush, entity_t *mapent ) { + mapbrush_t *b; + int i, k, n; + side_t *side, *s2; + int planenum; + q3_dbrushside_t *bspbrushside; + q3_dplane_t *bspplane; + int contentFlags = 0; + + if ( nummapbrushes >= MAX_MAPFILE_BRUSHES ) { + Error( "nummapbrushes >= MAX_MAPFILE_BRUSHES" ); + } + + b = &mapbrushes[nummapbrushes]; + b->original_sides = &brushsides[nummapbrushsides]; + b->entitynum = mapent - entities; + b->brushnum = nummapbrushes - mapent->firstbrush; + b->leafnum = dbrushleafnums[bspbrush - q3_dbrushes]; + + for ( n = 0; n < bspbrush->numSides; n++ ) + { + //pointer to the bsp brush side + bspbrushside = &q3_dbrushsides[bspbrush->firstSide + n]; + + if ( nummapbrushsides >= MAX_MAPFILE_BRUSHSIDES ) { + Error( "MAX_MAPFILE_BRUSHSIDES" ); + } //end if + //pointer to the map brush side + side = &brushsides[nummapbrushsides]; + //if the BSP brush side is textured + if ( q3_dbrushsidetextured[bspbrush->firstSide + n] ) { + side->flags |= SFL_TEXTURED | SFL_VISIBLE; + } else { side->flags &= ~SFL_TEXTURED;} + //NOTE: all Quake3 sides are assumed textured + //side->flags |= SFL_TEXTURED|SFL_VISIBLE; + // + if ( bspbrushside->shaderNum < 0 ) { + side->contents = 0; + side->surf = 0; + } //end if + else + { + side->contents = q3_dshaders[bspbrushside->shaderNum].contentFlags; + side->surf = q3_dshaders[bspbrushside->shaderNum].surfaceFlags; + + if ( strstr( q3_dshaders[bspbrushside->shaderNum].shader, "common/hint" ) ) { + //Log_Print("found hint side\n"); + side->surf |= SURF_HINT; + } //end if + + // Ridah, mark ladder brushes + if ( cfg.rs_allowladders && strstr( q3_dshaders[bspbrushside->shaderNum].shader, "common/ladder" ) ) { + //Log_Print("found ladder side\n"); + side->contents |= CONTENTS_LADDER; + contentFlags |= CONTENTS_LADDER; + } //end if + // done. + + } //end else + // + + if ( !( strstr( q3_dshaders[bspbrushside->shaderNum].shader, "common/slip" ) ) ) { + side->flags |= SFL_VISIBLE; + } else if ( side->surf & SURF_NODRAW ) { + side->flags |= SFL_TEXTURED | SFL_VISIBLE; + } //end if + /* + if (side->contents & (CONTENTS_TRANSLUCENT|CONTENTS_STRUCTURAL)) + { + side->flags |= SFL_TEXTURED|SFL_VISIBLE; + } //end if*/ + + // hints and skips are never detail, and have no content + if ( side->surf & ( SURF_HINT | SURF_SKIP ) ) { + side->contents = 0; + //Log_Print("found hint brush side\n"); + } + /* + if ((side->surf & SURF_NODRAW) && (side->surf & SURF_NOIMPACT)) + { + side->contents = 0; + side->surf &= ~CONTENTS_DETAIL; + Log_Print("probably found hint brush in a BSP without hints being used\n"); + } //end if*/ + +/* + // RF, ignore slick brushes (causes ladder issues) + if (strstr(q3_dshaders[bspbrushside->shaderNum].shader, "common/slick")) + { + //Log_Print("found hint side\n"); + b->numsides = 0; + b->contents = 0; + return; // get out of here + } //end if +*/ + + //ME: get a plane for this side + bspplane = &q3_dplanes[bspbrushside->planeNum]; + planenum = FindFloatPlane( bspplane->normal, bspplane->dist ); + // + // see if the plane has been used already + // + //ME: this really shouldn't happen!!! + //ME: otherwise the bsp file is corrupted?? + //ME: still it seems to happen, maybe Johny Boy's + //ME: brush bevel adding is crappy ? + for ( k = 0; k < b->numsides; k++ ) + { + s2 = b->original_sides + k; +// if (DotProduct (mapplanes[s2->planenum].normal, mapplanes[planenum].normal) > 0.999 +// && fabs(mapplanes[s2->planenum].dist - mapplanes[planenum].dist) < 0.01 ) + + if ( s2->planenum == planenum ) { + Log_Print( "Entity %i, Brush %i: duplicate plane\n" + , b->entitynum, b->brushnum ); + break; + } + if ( s2->planenum == ( planenum ^ 1 ) ) { + Log_Print( "Entity %i, Brush %i: mirrored plane\n" + , b->entitynum, b->brushnum ); + break; + } + } + if ( k != b->numsides ) { + continue; // duplicated + + } + // + // keep this side + // + //ME: reset pointer to side, why? hell I dunno (pointer is set above already) + side = b->original_sides + b->numsides; + //ME: store the plane number + side->planenum = planenum; + //ME: texinfo is already stored when bsp is loaded + //NOTE: check for TEXINFO_NODE, otherwise crash in Q3_BrushContents + //if (bspbrushside->texinfo < 0) side->texinfo = 0; + //else side->texinfo = bspbrushside->texinfo; + + // save the td off in case there is an origin brush and we + // have to recalculate the texinfo + // ME: don't need to recalculate because it's already done + // (for non-world entities) in the BSP file +// side_brushtextures[nummapbrushsides] = td; + + nummapbrushsides++; + b->numsides++; + } //end for + + // get the content for the entire brush + //Quake3 bsp brushes don't have a contents + b->contents = q3_dshaders[bspbrush->shaderNum].contentFlags | contentFlags; + // Ridah, Wolf has ladders (if we call Q3_BrushContents(), we'll get the solid area bug + b->contents &= ~( /*CONTENTS_LADDER|*/ CONTENTS_FOG | CONTENTS_STRUCTURAL ); + //b->contents = Q3_BrushContents(b); + // + // Ridah, CONTENTS_MOSTERCLIP should prevent AAS from being created, but not clip players/AI in the game + if ( b->contents & CONTENTS_MONSTERCLIP ) { + b->contents |= CONTENTS_PLAYERCLIP; + } + + // func_explosive's not solid + if ( !strcmp( "func_explosive", ValueForKey( &entities[b->entitynum], "classname" ) ) || + !strcmp( "func_invisible_user", ValueForKey( &entities[b->entitynum], "classname" ) ) || + !strcmp( "script_mover", ValueForKey( &entities[b->entitynum], "classname" ) ) || + !strcmp( "func_static", ValueForKey( &entities[b->entitynum], "classname" ) ) ) { + Log_Print( "Ignoring %s brush..\n", ValueForKey( &entities[b->entitynum], "classname" ) ); + b->numsides = 0; + b->contents = 0; + return; + } + + if ( BrushExists( b ) ) { + c_squattbrushes++; + b->numsides = 0; + return; + } //end if + + //if we're creating AAS + if ( create_aas ) { + //create the AAS brushes from this brush, don't add brush bevels + AAS_CreateMapBrushes( b, mapent, false ); + return; + } //end if + + // allow detail brushes to be removed + if ( nodetail && ( b->contents & CONTENTS_DETAIL ) ) { + b->numsides = 0; + return; + } //end if + + // allow water brushes to be removed + if ( nowater && ( b->contents & ( CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER ) ) ) { + b->numsides = 0; + return; + } //end if + + + // create windings for sides and bounds for brush + MakeBrushWindings( b ); + + //mark brushes without winding or with a tiny window as bevels + MarkBrushBevels( b ); + + // brushes that will not be visible at all will never be + // used as bsp splitters + if ( b->contents & ( CONTENTS_PLAYERCLIP | CONTENTS_MONSTERCLIP ) ) { + c_clipbrushes++; + for ( i = 0; i < b->numsides; i++ ) + b->original_sides[i].texinfo = TEXINFO_NODE; + } //end for + + // + // origin brushes are removed, but they set + // the rotation origin for the rest of the brushes + // in the entity. After the entire entity is parsed, + // the planenums and texinfos will be adjusted for + // the origin brush + // + //ME: not needed because the entities in the BSP file already + // have an origin set +// if (b->contents & CONTENTS_ORIGIN) +// { +// char string[32]; +// vec3_t origin; +// +// if (num_entities == 1) +// { +// Error ("Entity %i, Brush %i: origin brushes not allowed in world" +// , b->entitynum, b->brushnum); +// return; +// } +// +// VectorAdd (b->mins, b->maxs, origin); +// VectorScale (origin, 0.5, origin); +// +// sprintf (string, "%i %i %i", (int)origin[0], (int)origin[1], (int)origin[2]); +// SetKeyValue (&entities[b->entitynum], "origin", string); +// +// VectorCopy (origin, entities[b->entitynum].origin); +// +// // don't keep this brush +// b->numsides = 0; +// +// return; +// } + + //ME: the bsp brushes already have bevels, so we won't try to + // add them again (especially since Johny Boy's bevel adding might + // be crappy) +// AddBrushBevels(b); + + nummapbrushes++; + mapent->numbrushes++; +} //end of the function Q3_BSPBrushToMapBrush +//=========================================================================== +//=========================================================================== +void Q3_ParseBSPBrushes( entity_t *mapent ) { + int i; + + /* + //give all the brushes that belong to this entity the number of the + //BSP model used by this entity + Q3_SetBrushModelNumbers(mapent); + //now parse all the brushes with the correct mapent->modelnum + for (i = 0; i < q3_numbrushes; i++) + { + if (brushmodelnumbers[i] == mapent->modelnum) + { + Q3_BSPBrushToMapBrush(&q3_dbrushes[i], mapent); + } //end if + } //end for + */ + for ( i = 0; i < q3_dmodels[mapent->modelnum].numBrushes; i++ ) + { + Q3_BSPBrushToMapBrush( &q3_dbrushes[q3_dmodels[mapent->modelnum].firstBrush + i], mapent ); + } //end for +} //end of the function Q3_ParseBSPBrushes +//=========================================================================== +//=========================================================================== +qboolean Q3_ParseBSPEntity( int entnum ) { + entity_t *mapent; + char *model; + int startbrush, startsides; + + startbrush = nummapbrushes; + startsides = nummapbrushsides; + + mapent = &entities[entnum]; //num_entities]; + mapent->firstbrush = nummapbrushes; + mapent->numbrushes = 0; + mapent->modelnum = -1; //-1 = no BSP model + + model = ValueForKey( mapent, "model" ); + if ( model && strlen( model ) ) { + if ( *model == '*' ) { + //get the model number of this entity (skip the leading *) + mapent->modelnum = atoi( &model[1] ); + } //end if + } //end if + + GetVectorForKey( mapent, "origin", mapent->origin ); + + //if this is the world entity it has model number zero + //the world entity has no model key + if ( !strcmp( "worldspawn", ValueForKey( mapent, "classname" ) ) ) { + mapent->modelnum = 0; + } //end if + //if the map entity has a BSP model (a modelnum of -1 is used for + //entities that aren't using a BSP model) + if ( mapent->modelnum >= 0 ) { + //parse the bsp brushes + Q3_ParseBSPBrushes( mapent ); + } //end if + // + //the origin of the entity is already taken into account + // + //func_group entities can't be in the bsp file + // + //check out the func_areaportal entities + if ( !strcmp( "func_areaportal", ValueForKey( mapent, "classname" ) ) ) { + c_areaportals++; + mapent->areaportalnum = c_areaportals; + return true; + } //end if + return true; +} //end of the function Q3_ParseBSPEntity +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#define MAX_PATCH_VERTS 1024 + +void AAS_CreateCurveBrushes( void ) { + int i, j, n, planenum, numcurvebrushes = 0; + q3_dsurface_t *surface; + q3_drawVert_t *dv_p; + vec3_t points[MAX_PATCH_VERTS]; + int width, height, c; + patchCollide_t *pc; + facet_t *facet; + mapbrush_t *brush; + side_t *side; + entity_t *mapent; + winding_t *winding; + + qprintf( "nummapbrushsides = %d\n", nummapbrushsides ); + mapent = &entities[0]; + for ( i = 0; i < q3_numDrawSurfaces; i++ ) + { + surface = &q3_drawSurfaces[i]; + if ( !surface->patchWidth ) { + continue; + } + //if the curve is not solid + if ( !( q3_dshaders[surface->shaderNum].contentFlags & ( CONTENTS_SOLID | CONTENTS_PLAYERCLIP ) ) ) { + //Log_Print("skipped non-solid curve\n"); + continue; + } //end if + // + width = surface->patchWidth; + height = surface->patchHeight; + c = width * height; + if ( c > MAX_PATCH_VERTS ) { + Error( "ParseMesh: MAX_PATCH_VERTS" ); + } //end if + + dv_p = q3_drawVerts + surface->firstVert; + for ( j = 0 ; j < c ; j++, dv_p++ ) + { + points[j][0] = dv_p->xyz[0]; + points[j][1] = dv_p->xyz[1]; + points[j][2] = dv_p->xyz[2]; + } //end for + // create the internal facet structure + pc = CM_GeneratePatchCollide( width, height, points ); + // + for ( j = 0; j < pc->numFacets; j++ ) + { + facet = &pc->facets[j]; + // + brush = &mapbrushes[nummapbrushes]; + brush->original_sides = &brushsides[nummapbrushsides]; + brush->entitynum = 0; + brush->brushnum = nummapbrushes - mapent->firstbrush; + // + brush->numsides = facet->numBorders + 2; + nummapbrushsides += brush->numsides; + brush->contents = CONTENTS_SOLID; + // + //qprintf("\r%6d curve brushes", nummapbrushsides);//++numcurvebrushes); + qprintf( "\r%6d curve brushes", ++numcurvebrushes ); + // + planenum = FindFloatPlane( pc->planes[facet->surfacePlane].plane, pc->planes[facet->surfacePlane].plane[3] ); + // + side = &brush->original_sides[0]; + side->planenum = planenum; + side->contents = CONTENTS_SOLID; + side->flags |= SFL_TEXTURED | SFL_VISIBLE | SFL_CURVE; + side->surf = 0; + // + side = &brush->original_sides[1]; + if ( create_aas ) { + //the plane is expanded later so it's not a problem that + //these first two opposite sides are coplanar + side->planenum = planenum ^ 1; + } //end if + else + { + side->planenum = FindFloatPlane( mapplanes[planenum ^ 1].normal, mapplanes[planenum ^ 1].dist + 1 ); + side->flags |= SFL_TEXTURED | SFL_VISIBLE; + } //end else + side->contents = CONTENTS_SOLID; + side->flags |= SFL_CURVE; + side->surf = 0; + // + winding = BaseWindingForPlane( mapplanes[side->planenum].normal, mapplanes[side->planenum].dist ); + for ( n = 0; n < facet->numBorders; n++ ) + { + //never use the surface plane as a border + if ( facet->borderPlanes[n] == facet->surfacePlane ) { + continue; + } + // + side = &brush->original_sides[2 + n]; + side->planenum = FindFloatPlane( pc->planes[facet->borderPlanes[n]].plane, pc->planes[facet->borderPlanes[n]].plane[3] ); + if ( facet->borderInward[n] ) { + side->planenum ^= 1; + } + side->contents = CONTENTS_SOLID; + side->flags |= SFL_TEXTURED | SFL_CURVE; + side->surf = 0; + //chop the winding in place + if ( winding ) { + ChopWindingInPlace( &winding, mapplanes[side->planenum ^ 1].normal, mapplanes[side->planenum ^ 1].dist, 0.1 ); //CLIP_EPSILON); + } + } //end for + //VectorCopy(pc->bounds[0], brush->mins); + //VectorCopy(pc->bounds[1], brush->maxs); + if ( !winding ) { + Log_Print( "WARNING: AAS_CreateCurveBrushes: no winding\n" ); + brush->numsides = 0; + continue; + } //end if + brush->original_sides[0].winding = winding; + WindingBounds( winding, brush->mins, brush->maxs ); + for ( n = 0; n < 3; n++ ) + { + //IDBUG: all the indexes into the mins and maxs were zero (not using i) + if ( brush->mins[n] < -MAX_MAP_BOUNDS || brush->maxs[n] > MAX_MAP_BOUNDS ) { + Log_Print( "entity %i, brush %i: bounds out of range\n", brush->entitynum, brush->brushnum ); + Log_Print( "brush->mins[%d] = %f, brush->maxs[%d] = %f\n", n, brush->mins[n], n, brush->maxs[n] ); + brush->numsides = 0; //remove the brush + break; + } //end if + if ( brush->mins[n] > MAX_MAP_BOUNDS || brush->maxs[n] < -MAX_MAP_BOUNDS ) { + Log_Print( "entity %i, brush %i: no visible sides on brush\n", brush->entitynum, brush->brushnum ); + Log_Print( "brush->mins[%d] = %f, brush->maxs[%d] = %f\n", n, brush->mins[n], n, brush->maxs[n] ); + brush->numsides = 0; //remove the brush + break; + } //end if + } //end for + if ( create_aas ) { + //NOTE: brush bevels now already added + //AddBrushBevels(brush); + AAS_CreateMapBrushes( brush, mapent, false ); + } //end if + else + { + // create windings for sides and bounds for brush + MakeBrushWindings( brush ); + AddBrushBevels( brush ); + nummapbrushes++; + mapent->numbrushes++; + } //end else + } //end for + } //end for + //qprintf("\r%6d curve brushes", nummapbrushsides);//++numcurvebrushes); + qprintf( "\r%6d curve brushes\n", numcurvebrushes ); +} //end of the function AAS_CreateCurveBrushes +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AAS_ExpandMapBrush( mapbrush_t *brush, vec3_t mins, vec3_t maxs ); + +void Q3_LoadMapFromBSP( struct quakefile_s *qf ) { + int i; + //vec3_t mins = {-1,-1,-1}, maxs = {1, 1, 1}; // TTimo: unused + + Log_Print( "-- Q3_LoadMapFromBSP --\n" ); + //loaded map type + loadedmaptype = MAPTYPE_QUAKE3; + + Log_Print( "Loading map from %s...\n", qf->filename ); + //load the bsp file + Q3_LoadBSPFile( qf ); + + //create an index from bsp planes to map planes + //DPlanes2MapPlanes(); + //clear brush model numbers + for ( i = 0; i < MAX_MAPFILE_BRUSHES; i++ ) + brushmodelnumbers[i] = -1; + + nummapbrushsides = 0; + num_entities = 0; + + Q3_ParseEntities(); + // + for ( i = 0; i < num_entities; i++ ) + { + Q3_ParseBSPEntity( i ); + } //end for + + AAS_CreateCurveBrushes(); + //get the map mins and maxs from the world model + ClearBounds( map_mins, map_maxs ); + for ( i = 0; i < entities[0].numbrushes; i++ ) + { + if ( mapbrushes[i].numsides <= 0 ) { + continue; + } + //if (mapbrushes[i].mins[0] > 4096) + // continue; //no valid points + AddPointToBounds( mapbrushes[i].mins, map_mins, map_maxs ); + AddPointToBounds( mapbrushes[i].maxs, map_mins, map_maxs ); + } //end for + /*/ + for (i = 0; i < nummapbrushes; i++) + { + //if (!mapbrushes[i].original_sides) continue; + //AddBrushBevels(&mapbrushes[i]); + //AAS_ExpandMapBrush(&mapbrushes[i], mins, maxs); + } //end for*/ + /* + for (i = 0; i < nummapbrushsides; i++) + { + Log_Write("side %d flags = %d", i, brushsides[i].flags); + } //end for + for (i = 0; i < nummapbrushes; i++) + { + Log_Write("brush contents: "); + PrintContents(mapbrushes[i].contents); + Log_Print("\n"); + } //end for*/ + if ( writeaasmap ) { + char name[MAX_QPATH]; + strncpy( name, qf->filename, sizeof( name ) ); + StripExtension( name ); + strcat( name, "_aas.map" ); + WriteMapFile( name ); + } +} //end of the function Q3_LoadMapFromBSP +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Q3_ResetMapLoading( void ) { + //reset for map loading from bsp + memset( nodestack, 0, NODESTACKSIZE * sizeof( int ) ); + nodestackptr = NULL; + nodestacksize = 0; + memset( brushmodelnumbers, 0, MAX_MAPFILE_BRUSHES * sizeof( int ) ); +} //end of the function Q3_ResetMapLoading + diff --git a/src/bspc/map_sin.c b/src/bspc/map_sin.c new file mode 100644 index 0000000..17145ac --- /dev/null +++ b/src/bspc/map_sin.c @@ -0,0 +1,1201 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//----------------------------------------------------------------------------- +// +// $Logfile:: /Wolf4/src/bspc/map_sin.c $ + +#include "qbsp.h" +#include "l_bsp_sin.h" +#include "aas_map.h" //AAS_CreateMapBrushes + + +//==================================================================== + + +/* +=========== +Sin_BrushContents +=========== +*/ + +int Sin_BrushContents( mapbrush_t *b ) { + int contents; + side_t *s; + int i; +#ifdef SIN + float trans = 0; +#else + int trans; +#endif + + s = &b->original_sides[0]; + contents = s->contents; + +#ifdef SIN + trans = sin_texinfo[s->texinfo].translucence; +#else + trans = texinfo[s->texinfo].flags; +#endif + for ( i = 1 ; i < b->numsides ; i++, s++ ) + { + s = &b->original_sides[i]; +#ifdef SIN + trans += sin_texinfo[s->texinfo].translucence; +#else + trans |= texinfo[s->texinfo].flags; +#endif + if ( s->contents != contents ) { +#ifdef SIN + if ( + ( s->contents & CONTENTS_DETAIL && !( contents & CONTENTS_DETAIL ) ) || + ( !( s->contents & CONTENTS_DETAIL ) && contents & CONTENTS_DETAIL ) + ) { + s->contents |= CONTENTS_DETAIL; + contents |= CONTENTS_DETAIL; + continue; + } +#endif + printf( "Entity %i, Brush %i: mixed face contents\n" + , b->entitynum, b->brushnum ); + break; + } + } + + +#ifdef SIN + if ( contents & CONTENTS_FENCE ) { +// contents |= CONTENTS_TRANSLUCENT; + contents |= CONTENTS_DETAIL; + contents |= CONTENTS_DUMMYFENCE; + contents &= ~CONTENTS_SOLID; + contents &= ~CONTENTS_FENCE; + contents |= CONTENTS_WINDOW; + } +#endif + + // if any side is translucent, mark the contents + // and change solid to window +#ifdef SIN + if ( trans > 0 ) +#else + if ( trans & ( SURF_TRANS33 | SURF_TRANS66 ) ) +#endif + { + contents |= CONTENTS_Q2TRANSLUCENT; + if ( contents & CONTENTS_SOLID ) { + contents &= ~CONTENTS_SOLID; + contents |= CONTENTS_WINDOW; + } + } + + return contents; +} //*/ + + +//============================================================================ + + + +/* +================= +ParseBrush +================= +* / +void ParseBrush (entity_t *mapent) +{ + mapbrush_t *b; + int i,j, k; + int mt; + side_t *side, *s2; + int planenum; + brush_texture_t td; +#ifdef SIN + textureref_t newref; +#endif + int planepts[3][3]; + + if (nummapbrushes == MAX_MAP_BRUSHES) + Error ("nummapbrushes == MAX_MAP_BRUSHES"); + + b = &mapbrushes[nummapbrushes]; + b->original_sides = &brushsides[nummapbrushsides]; + b->entitynum = num_entities-1; + b->brushnum = nummapbrushes - mapent->firstbrush; + + do + { + if (!GetToken (true)) + break; + if (!strcmp (token, "}") ) + break; + + if (nummapbrushsides == MAX_MAP_BRUSHSIDES) + Error ("MAX_MAP_BRUSHSIDES"); + side = &brushsides[nummapbrushsides]; + + // read the three point plane definition + for (i=0 ; i<3 ; i++) + { + if (i != 0) + GetToken (true); + if (strcmp (token, "(") ) + Error ("parsing brush"); + + for (j=0 ; j<3 ; j++) + { + GetToken (false); + planepts[i][j] = atoi(token); + } + + GetToken (false); + if (strcmp (token, ")") ) + Error ("parsing brush"); + + } + + + // + // read the texturedef + // + GetToken (false); + strcpy (td.name, token); + + GetToken (false); + td.shift[0] = atoi(token); + GetToken (false); + td.shift[1] = atoi(token); + GetToken (false); +#ifdef SIN + td.rotate = atof(token); +#else + td.rotate = atoi(token); +#endif + GetToken (false); + td.scale[0] = atof(token); + GetToken (false); + td.scale[1] = atof(token); + + // find default flags and values + mt = FindMiptex (td.name); +#ifdef SIN + // clear out the masks on newref + memset(&newref,0,sizeof(newref)); + // copy over the name + strcpy( newref.name, td.name ); + + ParseSurfaceInfo( &newref ); + MergeRefs( &bsp_textureref[mt], &newref, &td.tref ); + side->contents = td.tref.contents; + side->surf = td.tref.flags; +#else + td.flags = textureref[mt].flags; + td.value = textureref[mt].value; + side->contents = textureref[mt].contents; + side->surf = td.flags = textureref[mt].flags; + + if (TokenAvailable()) + { + GetToken (false); + side->contents = atoi(token); + GetToken (false); + side->surf = td.flags = atoi(token); + GetToken (false); + td.value = atoi(token); + } +#endif + + // translucent objects are automatically classified as detail +#ifdef SIN + if ( td.tref.translucence > 0 ) +#else + if (side->surf & (SURF_TRANS33|SURF_TRANS66) ) +#endif + side->contents |= CONTENTS_DETAIL; + if (side->contents & (CONTENTS_PLAYERCLIP|CONTENTS_MONSTERCLIP) ) + side->contents |= CONTENTS_DETAIL; + if (fulldetail) + side->contents &= ~CONTENTS_DETAIL; + if (!(side->contents & ((LAST_VISIBLE_CONTENTS-1) + | CONTENTS_PLAYERCLIP|CONTENTS_MONSTERCLIP|CONTENTS_MIST) ) ) + side->contents |= CONTENTS_SOLID; + + // hints and skips are never detail, and have no content + if (side->surf & (SURF_HINT|SURF_SKIP) ) + { + side->contents = 0; +#ifndef SIN // I think this is a bug of some kind + side->surf &= ~CONTENTS_DETAIL; +#endif + } + + // + // find the plane number + // + planenum = PlaneFromPoints (planepts[0], planepts[1], planepts[2]); + if (planenum == -1) + { + printf ("Entity %i, Brush %i: plane with no normal\n" + , b->entitynum, b->brushnum); + continue; + } + + // + // see if the plane has been used already + // + for (k=0 ; knumsides ; k++) + { + s2 = b->original_sides + k; + if (s2->planenum == planenum) + { + printf ("Entity %i, Brush %i: duplicate plane\n" + , b->entitynum, b->brushnum); + break; + } + if ( s2->planenum == (planenum^1) ) + { + printf ("Entity %i, Brush %i: mirrored plane\n" + , b->entitynum, b->brushnum); + break; + } + } + if (k != b->numsides) + continue; // duplicated + + // + // keep this side + // + + side = b->original_sides + b->numsides; + side->planenum = planenum; +#ifdef SIN + side->texinfo = TexinfoForBrushTexture (&mapplanes[planenum], + &td, vec3_origin, &newref); + // + // save off lightinfo + // + side->lightinfo = LightinfoForBrushTexture ( &td ); +#else + side->texinfo = TexinfoForBrushTexture (&mapplanes[planenum], + &td, vec3_origin); + +#endif + + // save the td off in case there is an origin brush and we + // have to recalculate the texinfo + side_brushtextures[nummapbrushsides] = td; +#ifdef SIN + // save off the merged tref for animating textures + side_newrefs[nummapbrushsides] = newref; +#endif + + nummapbrushsides++; + b->numsides++; + } while (1); + + // get the content for the entire brush + b->contents = Sin_BrushContents (b); + + // allow detail brushes to be removed + if (nodetail && (b->contents & CONTENTS_DETAIL) ) + { + b->numsides = 0; + return; + } + + // allow water brushes to be removed + if (nowater && (b->contents & (CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER)) ) + { + b->numsides = 0; + return; + } + + // create windings for sides and bounds for brush + MakeBrushWindings (b); + + // brushes that will not be visible at all will never be + // used as bsp splitters + if (b->contents & (CONTENTS_PLAYERCLIP|CONTENTS_MONSTERCLIP) ) + { + c_clipbrushes++; + for (i=0 ; inumsides ; i++) + b->original_sides[i].texinfo = TEXINFO_NODE; + } + + // + // origin brushes are removed, but they set + // the rotation origin for the rest of the brushes + // in the entity. After the entire entity is parsed, + // the planenums and texinfos will be adjusted for + // the origin brush + // + if (b->contents & CONTENTS_ORIGIN) + { + char string[32]; + vec3_t origin; + + if (num_entities == 1) + { + Error ("Entity %i, Brush %i: origin brushes not allowed in world" + , b->entitynum, b->brushnum); + return; + } + + VectorAdd (b->mins, b->maxs, origin); + VectorScale (origin, 0.5, origin); + + sprintf (string, "%i %i %i", (int)origin[0], (int)origin[1], (int)origin[2]); + SetKeyValue (&entities[b->entitynum], "origin", string); + + VectorCopy (origin, entities[b->entitynum].origin); + + // don't keep this brush + b->numsides = 0; + + return; + } + + AddBrushBevels (b); + + nummapbrushes++; + mapent->numbrushes++; +} // +*/ + +/* +================ +MoveBrushesToWorld + +Takes all of the brushes from the current entity and +adds them to the world's brush list. + +Used by func_group and func_areaportal +================ +* / +void MoveBrushesToWorld (entity_t *mapent) +{ + int newbrushes; + int worldbrushes; + mapbrush_t *temp; + int i; + + // this is pretty gross, because the brushes are expected to be + // in linear order for each entity + + newbrushes = mapent->numbrushes; + worldbrushes = entities[0].numbrushes; + + temp = malloc(newbrushes*sizeof(mapbrush_t)); + memcpy (temp, mapbrushes + mapent->firstbrush, newbrushes*sizeof(mapbrush_t)); + +#if 0 // let them keep their original brush numbers + for (i=0 ; inumbrushes = 0; +} // +*/ + +/* +================ +ParseMapEntity +================ +* / +qboolean Sin_ParseMapEntity (void) +{ + entity_t *mapent; + epair_t *e; + side_t *s; + int i, j; + int startbrush, startsides; + vec_t newdist; + mapbrush_t *b; + + if (!GetToken (true)) + return false; + + if (strcmp (token, "{") ) + Error ("ParseEntity: { not found"); + + if (num_entities == MAX_MAP_ENTITIES) + Error ("num_entities == MAX_MAP_ENTITIES"); + + startbrush = nummapbrushes; + startsides = nummapbrushsides; + + mapent = &entities[num_entities]; + num_entities++; + memset (mapent, 0, sizeof(*mapent)); + mapent->firstbrush = nummapbrushes; + mapent->numbrushes = 0; +// mapent->portalareas[0] = -1; +// mapent->portalareas[1] = -1; + + do + { + if (!GetToken (true)) + Error ("ParseEntity: EOF without closing brace"); + if (!strcmp (token, "}") ) + break; + if (!strcmp (token, "{") ) + ParseBrush (mapent); + else + { + e = ParseEpair (); +#ifdef SIN + //HACK HACK HACK + // MED Gotta do this here + if ( !stricmp(e->key, "surfacefile") ) + { + if (!surfacefile[0]) + { + strcpy( surfacefile, e->value ); + } + printf ("--- ParseSurfaceFile ---\n"); + printf ("Surface script: %s\n", surfacefile); + if (!ParseSurfaceFile(surfacefile)) + { + Error ("Script file not found: %s\n", surfacefile); + } + } +#endif + e->next = mapent->epairs; + mapent->epairs = e; + } + } while (1); + +#ifdef SIN + if (!(strlen(ValueForKey(mapent, "origin"))) && ((num_entities-1) != 0)) + { + mapbrush_t *brush; + vec3_t origin; + char string[32]; + vec3_t mins, maxs; + int start, end; + // Calculate bounds + + start = mapent->firstbrush; + end = start + mapent->numbrushes; + ClearBounds (mins, maxs); + + for (j=start ; jnumsides) + continue; // not a real brush (origin brush) - shouldn't happen + AddPointToBounds (brush->mins, mins, maxs); + AddPointToBounds (brush->maxs, mins, maxs); + } + + // Set the origin to be the centroid of the entity. + VectorAdd ( mins, maxs, origin); + VectorScale( origin, 0.5f, origin ); + + sprintf (string, "%i %i %i", (int)origin[0], (int)origin[1], (int)origin[2]); + SetKeyValue ( mapent, "origin", string); +// qprintf("Setting origin to %s\n",string); + } +#endif + + GetVectorForKey (mapent, "origin", mapent->origin); + +#ifdef SIN + if ( + (!strcmp ("func_areaportal", ValueForKey (mapent, "classname"))) || + (!strcmp ("func_group", ValueForKey (mapent, "classname"))) || + (!strcmp ("detail", ValueForKey (mapent, "classname")) && !entitydetails) + ) + { + VectorClear( mapent->origin ); + } +#endif + + // + // if there was an origin brush, offset all of the planes and texinfo + // + if (mapent->origin[0] || mapent->origin[1] || mapent->origin[2]) + { + for (i=0 ; inumbrushes ; i++) + { + b = &mapbrushes[mapent->firstbrush + i]; + for (j=0 ; jnumsides ; j++) + { + s = &b->original_sides[j]; + newdist = mapplanes[s->planenum].dist - + DotProduct (mapplanes[s->planenum].normal, mapent->origin); + s->planenum = FindFloatPlane (mapplanes[s->planenum].normal, newdist); +#ifdef SIN + s->texinfo = TexinfoForBrushTexture (&mapplanes[s->planenum], + &side_brushtextures[s-brushsides], mapent->origin, &side_newrefs[s-brushsides]); + // + // save off lightinfo + // + s->lightinfo = LightinfoForBrushTexture ( &side_brushtextures[s-brushsides] ); +#else + s->texinfo = TexinfoForBrushTexture (&mapplanes[s->planenum], + &side_brushtextures[s-brushsides], mapent->origin); +#endif + } + MakeBrushWindings (b); + } + } + + // group entities are just for editor convenience + // toss all brushes into the world entity + if (!strcmp ("func_group", ValueForKey (mapent, "classname"))) + { + MoveBrushesToWorld (mapent); + mapent->numbrushes = 0; + mapent->wasdetail = true; + FreeValueKeys( mapent ); + return true; + } +#ifdef SIN + // detail entities are just for editor convenience + // toss all brushes into the world entity as detail brushes + if (!strcmp ("detail", ValueForKey (mapent, "classname")) && !entitydetails) + { + for (i=0 ; inumbrushes ; i++) + { + int j; + side_t * s; + b = &mapbrushes[mapent->firstbrush + i]; + if (nodetail) + { + b->numsides = 0; + continue; + } + if (!fulldetail) + { + // set the contents for the entire brush + b->contents |= CONTENTS_DETAIL; + // set the contents in the sides as well + for (j=0, s=b->original_sides ; jnumsides ; j++,s++) + { + s->contents |= CONTENTS_DETAIL; + } + } + else + { + // set the contents for the entire brush + b->contents |= CONTENTS_SOLID; + // set the contents in the sides as well + for (j=0, s=b->original_sides ; jnumsides ; j++,s++) + { + s->contents |= CONTENTS_SOLID; + } + } + } + MoveBrushesToWorld (mapent); + mapent->wasdetail = true; + FreeValueKeys( mapent ); + // kill off the entity + // num_entities--; + return true; + } +#endif + + // areaportal entities move their brushes, but don't eliminate + // the entity + if (!strcmp ("func_areaportal", ValueForKey (mapent, "classname"))) + { + char str[128]; + + if (mapent->numbrushes != 1) + Error ("Entity %i: func_areaportal can only be a single brush", num_entities-1); + + b = &mapbrushes[nummapbrushes-1]; + b->contents = CONTENTS_AREAPORTAL; + c_areaportals++; + mapent->areaportalnum = c_areaportals; + // set the portal number as "style" + sprintf (str, "%i", c_areaportals); + SetKeyValue (mapent, "style", str); + MoveBrushesToWorld (mapent); + return true; + } + + return true; +} //end of the function Sin_ParseMapEntity */ + +//=================================================================== + +/* +================ +LoadMapFile +================ +* / +void Sin_LoadMapFile (char *filename) +{ + int i; +#ifdef SIN + int num_detailsides=0; + int num_detailbrushes=0; + int num_worldsides=0; + int num_worldbrushes=0; + int j,k; +#endif + + qprintf ("--- LoadMapFile ---\n"); + + LoadScriptFile (filename); + + nummapbrushsides = 0; + num_entities = 0; + + while (ParseMapEntity ()) + { + } + + ClearBounds (map_mins, map_maxs); + for (i=0 ; i 4096) + continue; // no valid points + AddPointToBounds (mapbrushes[i].mins, map_mins, map_maxs); + AddPointToBounds (mapbrushes[i].maxs, map_mins, map_maxs); + } +#ifdef SIN + for (j=0; jnumsides && b->contents & CONTENTS_DETAIL) + num_detailbrushes++; + else if (b->numsides) + num_worldbrushes++; + for (k=0, s=b->original_sides ; knumsides ; k++,s++) + { + if (s->contents & CONTENTS_DETAIL) + num_detailsides++; + else + num_worldsides++; + } + } + } +#endif + + qprintf ("%5i brushes\n", nummapbrushes); + qprintf ("%5i clipbrushes\n", c_clipbrushes); + qprintf ("%5i total sides\n", nummapbrushsides); + qprintf ("%5i boxbevels\n", c_boxbevels); + qprintf ("%5i edgebevels\n", c_edgebevels); + qprintf ("%5i entities\n", num_entities); + qprintf ("%5i planes\n", nummapplanes); + qprintf ("%5i areaportals\n", c_areaportals); + qprintf ("size: %5.0f,%5.0f,%5.0f to %5.0f,%5.0f,%5.0f\n", map_mins[0],map_mins[1],map_mins[2], + map_maxs[0],map_maxs[1],map_maxs[2]); +#ifdef SIN + qprintf ("%5i detailbrushes\n", num_detailbrushes); + qprintf ("%5i worldbrushes\n", num_worldbrushes); + qprintf ("%5i detailsides\n", num_detailsides); + qprintf ("%5i worldsides\n", num_worldsides); +#endif + +} //end of the function Sin_LoadMap */ + + +#ifdef ME //Begin MAP loading from BSP file +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Sin_CreateMapTexinfo( void ) { + int i; + vec_t defaultvec[4] = {1, 0, 0, 0}; + + memcpy( map_texinfo[0].vecs[0], defaultvec, sizeof( defaultvec ) ); + memcpy( map_texinfo[0].vecs[1], defaultvec, sizeof( defaultvec ) ); + map_texinfo[0].flags = 0; + map_texinfo[0].value = 0; + strcpy( map_texinfo[0].texture, "generic/misc/red" ); //no texture + map_texinfo[0].nexttexinfo = -1; + for ( i = 1; i < sin_numtexinfo; i++ ) + { + memcpy( map_texinfo[i].vecs, sin_texinfo[i].vecs, sizeof( float ) * 2 * 4 ); + map_texinfo[i].flags = sin_texinfo[i].flags; + map_texinfo[i].value = 0; + strcpy( map_texinfo[i].texture, sin_texinfo[i].texture ); + map_texinfo[i].nexttexinfo = -1; + } //end for +} //end of the function Sin_CreateMapTexinfo +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Sin_SetLeafBrushesModelNumbers( int leafnum, int modelnum ) { + int i, brushnum; + sin_dleaf_t *leaf; + + leaf = &sin_dleafs[leafnum]; + for ( i = 0; i < leaf->numleafbrushes; i++ ) + { + brushnum = sin_dleafbrushes[leaf->firstleafbrush + i]; + brushmodelnumbers[brushnum] = modelnum; + dbrushleafnums[brushnum] = leafnum; + } //end for +} //end of the function Sin_SetLeafBrushesModelNumbers +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Sin_InitNodeStack( void ) { + nodestackptr = nodestack; + nodestacksize = 0; +} //end of the function Sin_InitNodeStack +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Sin_PushNodeStack( int num ) { + *nodestackptr = num; + nodestackptr++; + nodestacksize++; + // + if ( nodestackptr >= &nodestack[NODESTACKSIZE] ) { + Error( "Sin_PushNodeStack: stack overflow\n" ); + } //end if +} //end of the function Sin_PushNodeStack +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int Sin_PopNodeStack( void ) { + //if the stack is empty + if ( nodestackptr <= nodestack ) { + return -1; + } + //decrease stack pointer + nodestackptr--; + nodestacksize--; + //return the top value from the stack + return *nodestackptr; +} //end of the function Sin_PopNodeStack +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Sin_SetBrushModelNumbers( entity_t *mapent ) { + int n, pn; + int leafnum; + + // + Sin_InitNodeStack(); + //head node (root) of the bsp tree + n = sin_dmodels[mapent->modelnum].headnode; + pn = 0; + + do + { + //if we are in a leaf (negative node number) + if ( n < 0 ) { + //number of the leaf + leafnum = ( -n ) - 1; + //set the brush numbers + Sin_SetLeafBrushesModelNumbers( leafnum, mapent->modelnum ); + //walk back into the tree to find a second child to continue with + for ( pn = Sin_PopNodeStack(); pn >= 0; n = pn, pn = Sin_PopNodeStack() ) + { + //if we took the first child at the parent node + if ( sin_dnodes[pn].children[0] == n ) { + break; + } + } //end for + //if the stack wasn't empty (if not processed whole tree) + if ( pn >= 0 ) { + //push the parent node again + Sin_PushNodeStack( pn ); + //we proceed with the second child of the parent node + n = sin_dnodes[pn].children[1]; + } //end if + } //end if + else + { + //push the current node onto the stack + Sin_PushNodeStack( n ); + //walk forward into the tree to the first child + n = sin_dnodes[n].children[0]; + } //end else + } while ( pn >= 0 ); +} //end of the function Sin_SetBrushModelNumbers +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Sin_BSPBrushToMapBrush( sin_dbrush_t *bspbrush, entity_t *mapent ) { + mapbrush_t *b; + int i, k, n; + side_t *side, *s2; + int planenum; + sin_dbrushside_t *bspbrushside; + sin_dplane_t *bspplane; + + if ( nummapbrushes >= MAX_MAPFILE_BRUSHES ) { + Error( "nummapbrushes >= MAX_MAPFILE_BRUSHES" ); + } + + b = &mapbrushes[nummapbrushes]; + b->original_sides = &brushsides[nummapbrushsides]; + b->entitynum = mapent - entities; + b->brushnum = nummapbrushes - mapent->firstbrush; + b->leafnum = dbrushleafnums[bspbrush - sin_dbrushes]; + + for ( n = 0; n < bspbrush->numsides; n++ ) + { + //pointer to the bsp brush side + bspbrushside = &sin_dbrushsides[bspbrush->firstside + n]; + + if ( nummapbrushsides >= MAX_MAPFILE_BRUSHSIDES ) { + Error( "MAX_MAPFILE_BRUSHSIDES" ); + } //end if + //pointer to the map brush side + side = &brushsides[nummapbrushsides]; + //if the BSP brush side is textured + if ( sin_dbrushsidetextured[bspbrush->firstside + n] ) { + side->flags |= SFL_TEXTURED; + } else { side->flags &= ~SFL_TEXTURED;} + //ME: can get side contents and surf directly from BSP file + side->contents = bspbrush->contents; + //if the texinfo is TEXINFO_NODE + if ( bspbrushside->texinfo < 0 ) { + side->surf = 0; + } else { side->surf = sin_texinfo[bspbrushside->texinfo].flags;} + + // translucent objects are automatically classified as detail + if ( side->surf & ( SURF_TRANS33 | SURF_TRANS66 ) ) { + side->contents |= CONTENTS_DETAIL; + } + if ( side->contents & ( CONTENTS_PLAYERCLIP | CONTENTS_MONSTERCLIP ) ) { + side->contents |= CONTENTS_DETAIL; + } + if ( fulldetail ) { + side->contents &= ~CONTENTS_DETAIL; + } + if ( !( side->contents & ( ( LAST_VISIBLE_CONTENTS - 1 ) + | CONTENTS_PLAYERCLIP | CONTENTS_MONSTERCLIP | CONTENTS_MIST ) ) ) { + side->contents |= CONTENTS_SOLID; + } + + // hints and skips are never detail, and have no content + if ( side->surf & ( SURF_HINT | SURF_SKIP ) ) { + side->contents = 0; + side->surf &= ~CONTENTS_DETAIL; + } + + //ME: get a plane for this side + bspplane = &sin_dplanes[bspbrushside->planenum]; + planenum = FindFloatPlane( bspplane->normal, bspplane->dist ); + // + // see if the plane has been used already + // + //ME: this really shouldn't happen!!! + //ME: otherwise the bsp file is corrupted?? + //ME: still it seems to happen, maybe Johny Boy's + //ME: brush bevel adding is crappy ? + for ( k = 0; k < b->numsides; k++ ) + { + s2 = b->original_sides + k; + if ( s2->planenum == planenum ) { + Log_Print( "Entity %i, Brush %i: duplicate plane\n" + , b->entitynum, b->brushnum ); + break; + } + if ( s2->planenum == ( planenum ^ 1 ) ) { + Log_Print( "Entity %i, Brush %i: mirrored plane\n" + , b->entitynum, b->brushnum ); + break; + } + } + if ( k != b->numsides ) { + continue; // duplicated + + } + // + // keep this side + // + //ME: reset pointer to side, why? hell I dunno (pointer is set above already) + side = b->original_sides + b->numsides; + //ME: store the plane number + side->planenum = planenum; + //ME: texinfo is already stored when bsp is loaded + //NOTE: check for TEXINFO_NODE, otherwise crash in Sin_BrushContents + if ( bspbrushside->texinfo < 0 ) { + side->texinfo = 0; + } else { side->texinfo = bspbrushside->texinfo;} + + // save the td off in case there is an origin brush and we + // have to recalculate the texinfo + // ME: don't need to recalculate because it's already done + // (for non-world entities) in the BSP file +// side_brushtextures[nummapbrushsides] = td; + + nummapbrushsides++; + b->numsides++; + } //end for + + // get the content for the entire brush + b->contents = bspbrush->contents; + Sin_BrushContents( b ); + + if ( BrushExists( b ) ) { + c_squattbrushes++; + b->numsides = 0; + return; + } //end if + + //if we're creating AAS + if ( create_aas ) { + //create the AAS brushes from this brush, don't add brush bevels + AAS_CreateMapBrushes( b, mapent, false ); + return; + } //end if + + // allow detail brushes to be removed + if ( nodetail && ( b->contents & CONTENTS_DETAIL ) ) { + b->numsides = 0; + return; + } //end if + + // allow water brushes to be removed + if ( nowater && ( b->contents & ( CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER ) ) ) { + b->numsides = 0; + return; + } //end if + + // create windings for sides and bounds for brush + MakeBrushWindings( b ); + + //mark brushes without winding or with a tiny window as bevels + MarkBrushBevels( b ); + + // brushes that will not be visible at all will never be + // used as bsp splitters + if ( b->contents & ( CONTENTS_PLAYERCLIP | CONTENTS_MONSTERCLIP ) ) { + c_clipbrushes++; + for ( i = 0; i < b->numsides; i++ ) + b->original_sides[i].texinfo = TEXINFO_NODE; + } //end for + + // + // origin brushes are removed, but they set + // the rotation origin for the rest of the brushes + // in the entity. After the entire entity is parsed, + // the planenums and texinfos will be adjusted for + // the origin brush + // + //ME: not needed because the entities in the BSP file already + // have an origin set +// if (b->contents & CONTENTS_ORIGIN) +// { +// char string[32]; +// vec3_t origin; +// +// if (num_entities == 1) +// { +// Error ("Entity %i, Brush %i: origin brushes not allowed in world" +// , b->entitynum, b->brushnum); +// return; +// } +// +// VectorAdd (b->mins, b->maxs, origin); +// VectorScale (origin, 0.5, origin); +// +// sprintf (string, "%i %i %i", (int)origin[0], (int)origin[1], (int)origin[2]); +// SetKeyValue (&entities[b->entitynum], "origin", string); +// +// VectorCopy (origin, entities[b->entitynum].origin); +// +// // don't keep this brush +// b->numsides = 0; +// +// return; +// } + + //ME: the bsp brushes already have bevels, so we won't try to + // add them again (especially since Johny Boy's bevel adding might + // be crappy) +// AddBrushBevels(b); + + nummapbrushes++; + mapent->numbrushes++; +} //end of the function Sin_BSPBrushToMapBrush +//=========================================================================== +//=========================================================================== +void Sin_ParseBSPBrushes( entity_t *mapent ) { + int i, testnum = 0; + + //give all the brushes that belong to this entity the number of the + //BSP model used by this entity + Sin_SetBrushModelNumbers( mapent ); + //now parse all the brushes with the correct mapent->modelnum + for ( i = 0; i < sin_numbrushes; i++ ) + { + if ( brushmodelnumbers[i] == mapent->modelnum ) { + testnum++; + Sin_BSPBrushToMapBrush( &sin_dbrushes[i], mapent ); + } //end if + } //end for +} //end of the function Sin_ParseBSPBrushes +//=========================================================================== +//=========================================================================== +qboolean Sin_ParseBSPEntity( int entnum ) { + entity_t *mapent; + char *model; + int startbrush, startsides; + + startbrush = nummapbrushes; + startsides = nummapbrushsides; + + mapent = &entities[entnum]; //num_entities]; + mapent->firstbrush = nummapbrushes; + mapent->numbrushes = 0; + mapent->modelnum = -1; //-1 = no model + + model = ValueForKey( mapent, "model" ); + if ( model && *model == '*' ) { + mapent->modelnum = atoi( &model[1] ); + //Log_Print("model = %s\n", model); + //Log_Print("mapent->modelnum = %d\n", mapent->modelnum); + } //end if + + GetVectorForKey( mapent, "origin", mapent->origin ); + + //if this is the world entity it has model number zero + //the world entity has no model key + if ( !strcmp( "worldspawn", ValueForKey( mapent, "classname" ) ) ) { + mapent->modelnum = 0; + } //end if + //if the map entity has a BSP model (a modelnum of -1 is used for + //entities that aren't using a BSP model) + if ( mapent->modelnum >= 0 ) { + //parse the bsp brushes + Sin_ParseBSPBrushes( mapent ); + } //end if + // + //the origin of the entity is already taken into account + // + //func_group entities can't be in the bsp file + // + //check out the func_areaportal entities + if ( !strcmp( "func_areaportal", ValueForKey( mapent, "classname" ) ) ) { + c_areaportals++; + mapent->areaportalnum = c_areaportals; + return true; + } //end if + return true; +} //end of the function Sin_ParseBSPEntity +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Sin_LoadMapFromBSP( char *filename, int offset, int length ) { + int i; + + Log_Print( "-- Sin_LoadMapFromBSP --\n" ); + //loaded map type + loadedmaptype = MAPTYPE_SIN; + + Log_Print( "Loading map from %s...\n", filename ); + //load the bsp file + Sin_LoadBSPFile( filename, offset, length ); + + //create an index from bsp planes to map planes + //DPlanes2MapPlanes(); + //clear brush model numbers + for ( i = 0; i < MAX_MAPFILE_BRUSHES; i++ ) + brushmodelnumbers[i] = -1; + + nummapbrushsides = 0; + num_entities = 0; + + Sin_ParseEntities(); + // + for ( i = 0; i < num_entities; i++ ) + { + Sin_ParseBSPEntity( i ); + } //end for + + //get the map mins and maxs from the world model + ClearBounds( map_mins, map_maxs ); + for ( i = 0; i < entities[0].numbrushes; i++ ) + { + if ( mapbrushes[i].mins[0] > 4096 ) { + continue; //no valid points + } + AddPointToBounds( mapbrushes[i].mins, map_mins, map_maxs ); + AddPointToBounds( mapbrushes[i].maxs, map_mins, map_maxs ); + } //end for + // + Sin_CreateMapTexinfo(); +} //end of the function Sin_LoadMapFromBSP + +void Sin_ResetMapLoading( void ) { + //reset for map loading from bsp + memset( nodestack, 0, NODESTACKSIZE * sizeof( int ) ); + nodestackptr = NULL; + nodestacksize = 0; + memset( brushmodelnumbers, 0, MAX_MAPFILE_BRUSHES * sizeof( int ) ); +} //end of the function Sin_ResetMapLoading + +//End MAP loading from BSP file + +#endif //ME diff --git a/src/bspc/nodraw.c b/src/bspc/nodraw.c new file mode 100644 index 0000000..b4846b0 --- /dev/null +++ b/src/bspc/nodraw.c @@ -0,0 +1,50 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +#include "qbsp.h" + +vec3_t draw_mins, draw_maxs; +qboolean drawflag; + +void Draw_ClearWindow( void ) { +} + +//============================================================ + +#define GLSERV_PORT 25001 + + +void GLS_BeginScene( void ) { +} + +void GLS_Winding( winding_t *w, int code ) { +} + +void GLS_EndScene( void ) { +} diff --git a/src/bspc/portals.c b/src/bspc/portals.c new file mode 100644 index 0000000..6b0bf5d --- /dev/null +++ b/src/bspc/portals.c @@ -0,0 +1,1308 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: Portals +// Function: partalizing a bsp tree, flooding through portals +// Programmer: id Software & Mr Elusive (MrElusive@worldentity.com) +// Last update: 1999-08-10 +// Tab Size: 3 +//=========================================================================== + +#include "qbsp.h" +#include "l_mem.h" + +int c_active_portals; +int c_peak_portals; +int c_boundary; +int c_boundary_sides; +int c_portalmemory; + +//portal_t *portallist = NULL; +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +portal_t *AllocPortal( void ) { + portal_t *p; + + p = GetMemory( sizeof( portal_t ) ); + memset( p, 0, sizeof( portal_t ) ); + + if ( numthreads == 1 ) { + c_active_portals++; + if ( c_active_portals > c_peak_portals ) { + c_peak_portals = c_active_portals; + } //end if + c_portalmemory += MemorySize( p ); + } //end if + +// p->nextportal = portallist; +// portallist = p; + + return p; +} //end of the function AllocPortal +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FreePortal( portal_t *p ) { + if ( p->winding ) { + FreeWinding( p->winding ); + } + if ( numthreads == 1 ) { + c_active_portals--; + c_portalmemory -= MemorySize( p ); + } //end if + FreeMemory( p ); +} //end of the function FreePortal +//=========================================================================== +// Returns the single content bit of the +// strongest visible content present +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int VisibleContents( int contents ) { + int i; + + for ( i = 1 ; i <= LAST_VISIBLE_CONTENTS ; i <<= 1 ) + if ( contents & i ) { + return i; + } + + return 0; +} //end of the function VisibleContents +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int ClusterContents( node_t *node ) { + int c1, c2, c; + + if ( node->planenum == PLANENUM_LEAF ) { + return node->contents; + } + + c1 = ClusterContents( node->children[0] ); + c2 = ClusterContents( node->children[1] ); + c = c1 | c2; + + // a cluster may include some solid detail areas, but + // still be seen into + if ( !( c1 & CONTENTS_SOLID ) || !( c2 & CONTENTS_SOLID ) ) { + c &= ~CONTENTS_SOLID; + } + return c; +} //end of the function ClusterContents + +//=========================================================================== +// Returns true if the portal is empty or translucent, allowing +// the PVS calculation to see through it. +// The nodes on either side of the portal may actually be clusters, +// not leaves, so all contents should be ored together +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean Portal_VisFlood( portal_t *p ) { + int c1, c2; + + if ( !p->onnode ) { + return false; // to global outsideleaf + + } + c1 = ClusterContents( p->nodes[0] ); + c2 = ClusterContents( p->nodes[1] ); + + if ( !VisibleContents( c1 ^ c2 ) ) { + return true; + } + + if ( c1 & ( CONTENTS_Q2TRANSLUCENT | CONTENTS_DETAIL ) ) { + c1 = 0; + } + if ( c2 & ( CONTENTS_Q2TRANSLUCENT | CONTENTS_DETAIL ) ) { + c2 = 0; + } + + if ( ( c1 | c2 ) & CONTENTS_SOLID ) { + return false; // can't see through solid + + } + if ( !( c1 ^ c2 ) ) { + return true; // identical on both sides + + } + if ( !VisibleContents( c1 ^ c2 ) ) { + return true; + } + return false; +} //end of the function Portal_VisFlood +//=========================================================================== +// The entity flood determines which areas are +// "outside" on the map, which are then filled in. +// Flowing from side s to side !s +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean Portal_EntityFlood( portal_t *p, int s ) { + if ( p->nodes[0]->planenum != PLANENUM_LEAF + || p->nodes[1]->planenum != PLANENUM_LEAF ) { + Error( "Portal_EntityFlood: not a leaf" ); + } + + // can never cross to a solid + if ( ( p->nodes[0]->contents & CONTENTS_SOLID ) + || ( p->nodes[1]->contents & CONTENTS_SOLID ) ) { + return false; + } + + // can flood through everything else + return true; +} + + +//============================================================================= + +int c_tinyportals; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void AddPortalToNodes( portal_t *p, node_t *front, node_t *back ) { + if ( p->nodes[0] || p->nodes[1] ) { + Error( "AddPortalToNode: allready included" ); + } + + p->nodes[0] = front; + p->next[0] = front->portals; + front->portals = p; + + p->nodes[1] = back; + p->next[1] = back->portals; + back->portals = p; +} //end of the function AddPortalToNodes +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void RemovePortalFromNode( portal_t *portal, node_t *l ) { + portal_t **pp, *t; + + int s, i, n; + portal_t *p; + portal_t *portals[4096]; + +// remove reference to the current portal + pp = &l->portals; + while ( 1 ) + { + t = *pp; + if ( !t ) { + Error( "RemovePortalFromNode: portal not in leaf" ); + } + + if ( t == portal ) { + break; + } + + if ( t->nodes[0] == l ) { + pp = &t->next[0]; + } else if ( t->nodes[1] == l ) { + pp = &t->next[1]; + } else { + Error( "RemovePortalFromNode: portal not bounding leaf" ); + } + } + + if ( portal->nodes[0] == l ) { + *pp = portal->next[0]; + portal->nodes[0] = NULL; + } //end if + else if ( portal->nodes[1] == l ) { + *pp = portal->next[1]; + portal->nodes[1] = NULL; + } //end else if + else + { + Error( "RemovePortalFromNode: mislinked portal" ); + } //end else +//#ifdef ME + n = 0; + for ( p = l->portals; p; p = p->next[s] ) + { + for ( i = 0; i < n; i++ ) + { + if ( p == portals[i] ) { + Error( "RemovePortalFromNode: circular linked\n" ); + } + } //end for + if ( p->nodes[0] != l && p->nodes[1] != l ) { + Error( "RemovePortalFromNodes: portal does not belong to node\n" ); + } //end if + portals[n] = p; + s = ( p->nodes[1] == l ); +// if (++n >= 4096) Error("RemovePortalFromNode: more than 4096 portals\n"); + } //end for +//#endif +} //end of the function RemovePortalFromNode +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void PrintPortal( portal_t *p ) { + int i; + winding_t *w; + + w = p->winding; + for ( i = 0 ; i < w->numpoints ; i++ ) + printf( "(%5.0f,%5.0f,%5.0f)\n",w->p[i][0] + , w->p[i][1], w->p[i][2] ); +} //end of the function PrintPortal +//=========================================================================== +// The created portals will face the global outside_node +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#define SIDESPACE 8 + +void MakeHeadnodePortals( tree_t *tree ) { + vec3_t bounds[2]; + int i, j, n; + portal_t *p, *portals[6]; + plane_t bplanes[6], *pl; + node_t *node; + + node = tree->headnode; + +// pad with some space so there will never be null volume leaves + for ( i = 0 ; i < 3 ; i++ ) + { + bounds[0][i] = tree->mins[i] - SIDESPACE; + bounds[1][i] = tree->maxs[i] + SIDESPACE; + } + + tree->outside_node.planenum = PLANENUM_LEAF; + tree->outside_node.brushlist = NULL; + tree->outside_node.portals = NULL; + tree->outside_node.contents = 0; + + for ( i = 0 ; i < 3 ; i++ ) + for ( j = 0 ; j < 2 ; j++ ) + { + n = j * 3 + i; + + p = AllocPortal(); + portals[n] = p; + + pl = &bplanes[n]; + memset( pl, 0, sizeof( *pl ) ); + if ( j ) { + pl->normal[i] = -1; + pl->dist = -bounds[j][i]; + } else + { + pl->normal[i] = 1; + pl->dist = bounds[j][i]; + } + p->plane = *pl; + p->winding = BaseWindingForPlane( pl->normal, pl->dist ); + AddPortalToNodes( p, node, &tree->outside_node ); + } + +// clip the basewindings by all the other planes + for ( i = 0 ; i < 6 ; i++ ) + { + for ( j = 0 ; j < 6 ; j++ ) + { + if ( j == i ) { + continue; + } + ChopWindingInPlace( &portals[i]->winding, bplanes[j].normal, bplanes[j].dist, ON_EPSILON ); + } //end for + } //end for +} //end of the function MakeHeadNodePortals +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#define BASE_WINDING_EPSILON 0.001 +#define SPLIT_WINDING_EPSILON 0.001 + +winding_t *BaseWindingForNode( node_t *node ) { + winding_t *w; + node_t *n; + plane_t *plane; + vec3_t normal; + vec_t dist; + + w = BaseWindingForPlane( mapplanes[node->planenum].normal + , mapplanes[node->planenum].dist ); + + // clip by all the parents + for ( n = node->parent ; n && w ; ) + { + plane = &mapplanes[n->planenum]; + + if ( n->children[0] == node ) { // take front + ChopWindingInPlace( &w, plane->normal, plane->dist, BASE_WINDING_EPSILON ); + } else + { // take back + VectorSubtract( vec3_origin, plane->normal, normal ); + dist = -plane->dist; + ChopWindingInPlace( &w, normal, dist, BASE_WINDING_EPSILON ); + } + node = n; + n = n->parent; + } + + return w; +} //end of the function BaseWindingForNode +//=========================================================================== +// create the new portal by taking the full plane winding for the cutting +// plane and clipping it by all of parents of this node +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean WindingIsTiny( winding_t *w ); + +void MakeNodePortal( node_t *node ) { + portal_t *new_portal, *p; + winding_t *w; + vec3_t normal; + float dist = 0; // TTimo: init + int side = 0; + + w = BaseWindingForNode( node ); + + // clip the portal by all the other portals in the node + for ( p = node->portals; p && w; p = p->next[side] ) + { + if ( p->nodes[0] == node ) { + side = 0; + VectorCopy( p->plane.normal, normal ); + dist = p->plane.dist; + } //end if + else if ( p->nodes[1] == node ) { + side = 1; + VectorSubtract( vec3_origin, p->plane.normal, normal ); + dist = -p->plane.dist; + } //end else if + else + { + Error( "MakeNodePortal: mislinked portal" ); + } //end else + ChopWindingInPlace( &w, normal, dist, 0.1 ); + } //end for + + if ( !w ) { + return; + } //end if + + if ( WindingIsTiny( w ) ) { + c_tinyportals++; + FreeWinding( w ); + return; + } //end if + +#ifdef DEBUG +/* //NOTE: don't use this winding ok check + // all the invalid windings only have a degenerate edge + if (WindingError(w)) + { + Log_Print("MakeNodePortal: %s\n", WindingErrorString()); + FreeWinding(w); + return; + } //end if*/ +#endif //DEBUG + + + new_portal = AllocPortal(); + new_portal->plane = mapplanes[node->planenum]; + +#ifdef ME + new_portal->planenum = node->planenum; +#endif //ME + + new_portal->onnode = node; + new_portal->winding = w; + AddPortalToNodes( new_portal, node->children[0], node->children[1] ); +} //end of the function MakeNodePortal +//=========================================================================== +// Move or split the portals that bound node so that the node's +// children have portals instead of node. +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void SplitNodePortals( node_t *node ) { + portal_t *p, *next_portal, *new_portal; + node_t *f, *b, *other_node; + int side = 0; // TTimo: init + plane_t *plane; + winding_t *frontwinding, *backwinding; + + plane = &mapplanes[node->planenum]; + f = node->children[0]; + b = node->children[1]; + + for ( p = node->portals ; p ; p = next_portal ) + { + if ( p->nodes[0] == node ) { + side = 0; + } else if ( p->nodes[1] == node ) { + side = 1; + } else { Error( "SplitNodePortals: mislinked portal" );} + next_portal = p->next[side]; + + other_node = p->nodes[!side]; + RemovePortalFromNode( p, p->nodes[0] ); + RemovePortalFromNode( p, p->nodes[1] ); + +// +// cut the portal into two portals, one on each side of the cut plane +// + ClipWindingEpsilon( p->winding, plane->normal, plane->dist, + SPLIT_WINDING_EPSILON, &frontwinding, &backwinding ); + + if ( frontwinding && WindingIsTiny( frontwinding ) ) { + FreeWinding( frontwinding ); + frontwinding = NULL; + c_tinyportals++; + } //end if + + if ( backwinding && WindingIsTiny( backwinding ) ) { + FreeWinding( backwinding ); + backwinding = NULL; + c_tinyportals++; + } //end if + +#ifdef DEBUG +/* //NOTE: don't use this winding ok check + // all the invalid windings only have a degenerate edge + if (frontwinding && WindingError(frontwinding)) + { + Log_Print("SplitNodePortals: front %s\n", WindingErrorString()); + FreeWinding(frontwinding); + frontwinding = NULL; + } //end if + if (backwinding && WindingError(backwinding)) + { + Log_Print("SplitNodePortals: back %s\n", WindingErrorString()); + FreeWinding(backwinding); + backwinding = NULL; + } //end if*/ +#endif //DEBUG + + if ( !frontwinding && !backwinding ) { // tiny windings on both sides + continue; + } + + if ( !frontwinding ) { + FreeWinding( backwinding ); + if ( side == 0 ) { + AddPortalToNodes( p, b, other_node ); + } else { AddPortalToNodes( p, other_node, b );} + continue; + } + if ( !backwinding ) { + FreeWinding( frontwinding ); + if ( side == 0 ) { + AddPortalToNodes( p, f, other_node ); + } else { AddPortalToNodes( p, other_node, f );} + continue; + } + + // the winding is split + new_portal = AllocPortal(); + *new_portal = *p; + new_portal->winding = backwinding; + FreeWinding( p->winding ); + p->winding = frontwinding; + + if ( side == 0 ) { + AddPortalToNodes( p, f, other_node ); + AddPortalToNodes( new_portal, b, other_node ); + } //end if + else + { + AddPortalToNodes( p, other_node, f ); + AddPortalToNodes( new_portal, other_node, b ); + } //end else + } + + node->portals = NULL; +} //end of the function SplitNodePortals +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void CalcNodeBounds( node_t *node ) { + portal_t *p; + int s; + int i; + + // calc mins/maxs for both leaves and nodes + ClearBounds( node->mins, node->maxs ); + for ( p = node->portals ; p ; p = p->next[s] ) + { + s = ( p->nodes[1] == node ); + for ( i = 0 ; i < p->winding->numpoints ; i++ ) + AddPointToBounds( p->winding->p[i], node->mins, node->maxs ); + } +} //end of the function CalcNodeBounds +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int c_numportalizednodes; + +void MakeTreePortals_r( node_t *node ) { + int i; + +#ifdef ME + qprintf( "\r%6d", ++c_numportalizednodes ); + if ( cancelconversion ) { + return; + } +#endif //ME + + CalcNodeBounds( node ); + if ( node->mins[0] >= node->maxs[0] ) { + Log_Print( "WARNING: node without a volume\n" ); + } + + for ( i = 0 ; i < 3 ; i++ ) + { + if ( node->mins[i] < -MAX_MAP_BOUNDS || node->maxs[i] > MAX_MAP_BOUNDS ) { + Log_Print( "WARNING: node with unbounded volume\n" ); + break; + } + } + if ( node->planenum == PLANENUM_LEAF ) { + return; + } + + MakeNodePortal( node ); + SplitNodePortals( node ); + + MakeTreePortals_r( node->children[0] ); + MakeTreePortals_r( node->children[1] ); +} //end of the function MakeTreePortals_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void MakeTreePortals( tree_t *tree ) { + +#ifdef ME + Log_Print( "---- Node Portalization ----\n" ); + c_numportalizednodes = 0; + c_portalmemory = 0; + qprintf( "%6d nodes portalized", c_numportalizednodes ); +#endif //ME + + MakeHeadnodePortals( tree ); + MakeTreePortals_r( tree->headnode ); + +#ifdef ME + qprintf( "\n" ); + Log_Write( "\r%6d nodes portalized\r\n", c_numportalizednodes ); + Log_Print( "%6d tiny portals\r\n", c_tinyportals ); + Log_Print( "%6d KB of portal memory\r\n", c_portalmemory >> 10 ); + Log_Print( "%6i KB of winding memory\r\n", WindingMemory() >> 10 ); +#endif //ME +} //end of the function MakeTreePortals + +/* +========================================================= + +FLOOD ENTITIES + +========================================================= +*/ +//#define P_NODESTACK + +node_t *p_firstnode; +node_t *p_lastnode; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef P_NODESTACK +void P_AddNodeToList( node_t *node ) { + node->next = p_firstnode; + p_firstnode = node; + if ( !p_lastnode ) { + p_lastnode = node; + } +} //end of the function P_AddNodeToList +#else //it's a node queue +//add the node to the end of the node list +void P_AddNodeToList( node_t *node ) { + node->next = NULL; + if ( p_lastnode ) { + p_lastnode->next = node; + } else { p_firstnode = node;} + p_lastnode = node; +} //end of the function P_AddNodeToList +#endif //P_NODESTACK +//=========================================================================== +// get the first node from the front of the node list +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +node_t *P_NextNodeFromList( void ) { + node_t *node; + + node = p_firstnode; + if ( p_firstnode ) { + p_firstnode = p_firstnode->next; + } + if ( !p_firstnode ) { + p_lastnode = NULL; + } + return node; +} //end of the function P_NextNodeFromList +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FloodPortals( node_t *firstnode ) { + node_t *node; + portal_t *p; + int s; + + firstnode->occupied = 1; + P_AddNodeToList( firstnode ); + + for ( node = P_NextNodeFromList(); node; node = P_NextNodeFromList() ) + { + for ( p = node->portals; p; p = p->next[s] ) + { + s = ( p->nodes[1] == node ); + //if the node at the other side of the portal is occupied already + if ( p->nodes[!s]->occupied ) { + continue; + } + //if it isn't possible to flood through this portal + if ( !Portal_EntityFlood( p, s ) ) { + continue; + } + // + p->nodes[!s]->occupied = node->occupied + 1; + // + P_AddNodeToList( p->nodes[!s] ); + } //end for + } //end for +} //end of the function FloodPortals +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int numrec; + +void FloodPortals_r( node_t *node, int dist ) { + portal_t *p; + int s; +// int i; + + Log_Print( "\r%6d", ++numrec ); + + if ( node->occupied ) { + Error( "FloodPortals_r: node already occupied\n" ); + } + if ( !node ) { + Error( "FloodPortals_r: NULL node\n" ); + } //end if*/ + node->occupied = dist; + + for ( p = node->portals; p; p = p->next[s] ) + { + s = ( p->nodes[1] == node ); + //if the node at the other side of the portal is occupied already + if ( p->nodes[!s]->occupied ) { + continue; + } + //if it isn't possible to flood through this portal + if ( !Portal_EntityFlood( p, s ) ) { + continue; + } + //flood recursively through the current portal + FloodPortals_r( p->nodes[!s], dist + 1 ); + } //end for + Log_Print( "\r%6d", --numrec ); +} //end of the function FloodPortals_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean PlaceOccupant( node_t *headnode, vec3_t origin, entity_t *occupant ) { + node_t *node; + vec_t d; + plane_t *plane; + + //find the leaf to start in + node = headnode; + while ( node->planenum != PLANENUM_LEAF ) + { + if ( node->planenum < 0 || node->planenum > nummapplanes ) { + Error( "PlaceOccupant: invalid node->planenum\n" ); + } //end if + plane = &mapplanes[node->planenum]; + d = DotProduct( origin, plane->normal ) - plane->dist; + if ( d >= 0 ) { + node = node->children[0]; + } else { node = node->children[1];} + if ( !node ) { + Error( "PlaceOccupant: invalid child %d\n", d < 0 ); + } //end if + } //end while + //don't start in solid +// if (node->contents == CONTENTS_SOLID) + //ME: replaced because in LeafNode in brushbsp.c + // some nodes have contents solid with other contents + if ( node->contents & CONTENTS_SOLID ) { + return false; + } + //if the node is already occupied + if ( node->occupied ) { + return false; + } + + //place the occupant in the first leaf + node->occupant = occupant; + + numrec = 0; +// Log_Print("%6d recurses", numrec); +// FloodPortals_r(node, 1); +// Log_Print("\n"); + FloodPortals( node ); + + return true; +} //end of the function PlaceOccupant +//=========================================================================== +// Marks all nodes that can be reached by entites +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +qboolean FloodEntities( tree_t *tree ) { + int i; + int x, y; + vec3_t origin; + char *cl; + qboolean inside; + node_t *headnode; + + headnode = tree->headnode; + Log_Print( "------ FloodEntities -------\n" ); + inside = false; + tree->outside_node.occupied = 0; + + //start at entity 1 not the world ( = 0) + for ( i = 1; i < num_entities; i++ ) + { + GetVectorForKey( &entities[i], "origin", origin ); + if ( VectorCompare( origin, vec3_origin ) ) { + continue; + } + + cl = ValueForKey( &entities[i], "classname" ); + origin[2] += 1; //so objects on floor are ok + +// Log_Print("flooding from entity %d: %s\n", i, cl); + //nudge playerstart around if needed so clipping hulls allways + //have a valid point + if ( !strcmp( cl, "info_player_start" ) ) { + for ( x = -16; x <= 16; x += 16 ) + { + for ( y = -16; y <= 16; y += 16 ) + { + origin[0] += x; + origin[1] += y; + if ( PlaceOccupant( headnode, origin, &entities[i] ) ) { + inside = true; + x = 999; //stop for this info_player_start + break; + } //end if + origin[0] -= x; + origin[1] -= y; + } //end for + } //end for + } //end if + else + { + if ( PlaceOccupant( headnode, origin, &entities[i] ) ) { + inside = true; + } //end if + } //end else + } //end for + + if ( !inside ) { + Log_Print( "WARNING: no entities inside\n" ); + } //end if + else if ( tree->outside_node.occupied ) { + Log_Print( "WARNING: entity reached from outside\n" ); + } //end else if + + return (qboolean)( inside && !tree->outside_node.occupied ); +} //end of the function FloodEntities + +/* +========================================================= + +FILL OUTSIDE + +========================================================= +*/ + +int c_outside; +int c_inside; +int c_solid; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FillOutside_r( node_t *node ) { + if ( node->planenum != PLANENUM_LEAF ) { + FillOutside_r( node->children[0] ); + FillOutside_r( node->children[1] ); + return; + } //end if + // anything not reachable by an entity + // can be filled away (by setting solid contents) + if ( !node->occupied ) { + if ( !( node->contents & CONTENTS_SOLID ) ) { + c_outside++; + node->contents |= CONTENTS_SOLID; + } //end if + else + { + c_solid++; + } //end else + } //end if + else + { + c_inside++; + } //end else +} //end of the function FillOutside_r +//=========================================================================== +// Fill all nodes that can't be reached by entities +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FillOutside( node_t *headnode ) { + c_outside = 0; + c_inside = 0; + c_solid = 0; + Log_Print( "------- FillOutside --------\n" ); + FillOutside_r( headnode ); + Log_Print( "%5i solid leaves\n", c_solid ); + Log_Print( "%5i leaves filled\n", c_outside ); + Log_Print( "%5i inside leaves\n", c_inside ); +} //end of the function FillOutside + +/* +========================================================= + +FLOOD AREAS + +========================================================= +*/ + +int c_areas; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FloodAreas_r( node_t *node ) { + portal_t *p; + int s; + bspbrush_t *b; + entity_t *e; + + if ( node->contents == CONTENTS_AREAPORTAL ) { + // this node is part of an area portal + b = node->brushlist; + e = &entities[b->original->entitynum]; + + // if the current area has allready touched this + // portal, we are done + if ( e->portalareas[0] == c_areas || e->portalareas[1] == c_areas ) { + return; + } + + // note the current area as bounding the portal + if ( e->portalareas[1] ) { + Log_Print( "WARNING: areaportal entity %i touches > 2 areas\n", b->original->entitynum ); + return; + } + if ( e->portalareas[0] ) { + e->portalareas[1] = c_areas; + } else { + e->portalareas[0] = c_areas; + } + + return; + } //end if + + if ( node->area ) { + return; // allready got it + } + node->area = c_areas; + + for ( p = node->portals ; p ; p = p->next[s] ) + { + s = ( p->nodes[1] == node ); +#if 0 + if ( p->nodes[!s]->occupied ) { + continue; + } +#endif + if ( !Portal_EntityFlood( p, s ) ) { + continue; + } + + FloodAreas_r( p->nodes[!s] ); + } //end for +} //end of the function FloodAreas_r +//=========================================================================== +// Just decend the tree, and for each node that hasn't had an +// area set, flood fill out from there +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FindAreas_r( node_t *node ) { + if ( node->planenum != PLANENUM_LEAF ) { + FindAreas_r( node->children[0] ); + FindAreas_r( node->children[1] ); + return; + } + + if ( node->area ) { + return; // allready got it + + } + if ( node->contents & CONTENTS_SOLID ) { + return; + } + + if ( !node->occupied ) { + return; // not reachable by entities + + } + // area portals are allways only flooded into, never + // out of + if ( node->contents == CONTENTS_AREAPORTAL ) { + return; + } + + c_areas++; + FloodAreas_r( node ); +} //end of the function FindAreas_r +//=========================================================================== +// Just decend the tree, and for each node that hasn't had an +// area set, flood fill out from there +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void SetAreaPortalAreas_r( node_t *node ) { + bspbrush_t *b; + entity_t *e; + + if ( node->planenum != PLANENUM_LEAF ) { + SetAreaPortalAreas_r( node->children[0] ); + SetAreaPortalAreas_r( node->children[1] ); + return; + } //end if + + if ( node->contents == CONTENTS_AREAPORTAL ) { + if ( node->area ) { + return; // allready set + + } + b = node->brushlist; + e = &entities[b->original->entitynum]; + node->area = e->portalareas[0]; + if ( !e->portalareas[1] ) { + Log_Print( "WARNING: areaportal entity %i doesn't touch two areas\n", b->original->entitynum ); + return; + } //end if + } //end if +} //end of the function SetAreaPortalAreas_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +/* +void EmitAreaPortals(node_t *headnode) +{ + int i, j; + entity_t *e; + dareaportal_t *dp; + + if (c_areas > MAX_MAP_AREAS) + Error ("MAX_MAP_AREAS"); + numareas = c_areas+1; + numareaportals = 1; // leave 0 as an error + + for (i=1 ; i<=c_areas ; i++) + { + dareas[i].firstareaportal = numareaportals; + for (j=0 ; jareaportalnum) + continue; + dp = &dareaportals[numareaportals]; + if (e->portalareas[0] == i) + { + dp->portalnum = e->areaportalnum; + dp->otherarea = e->portalareas[1]; + numareaportals++; + } //end if + else if (e->portalareas[1] == i) + { + dp->portalnum = e->areaportalnum; + dp->otherarea = e->portalareas[0]; + numareaportals++; + } //end else if + } //end for + dareas[i].numareaportals = numareaportals - dareas[i].firstareaportal; + } //end for + + Log_Print("%5i numareas\n", numareas); + Log_Print("%5i numareaportals\n", numareaportals); +} //end of the function EmitAreaPortals +*/ +//=========================================================================== +// Mark each leaf with an area, bounded by CONTENTS_AREAPORTAL +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FloodAreas( tree_t *tree ) { + Log_Print( "--- FloodAreas ---\n" ); + FindAreas_r( tree->headnode ); + SetAreaPortalAreas_r( tree->headnode ); + Log_Print( "%5i areas\n", c_areas ); +} //end of the function FloodAreas +//=========================================================================== +// Finds a brush side to use for texturing the given portal +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FindPortalSide( portal_t *p ) { + int viscontents; + bspbrush_t *bb; + mapbrush_t *brush; + node_t *n; + int i,j; + int planenum; + side_t *side, *bestside; + float dot, bestdot; + plane_t *p1, *p2; + + // decide which content change is strongest + // solid > lava > water, etc + viscontents = VisibleContents( p->nodes[0]->contents ^ p->nodes[1]->contents ); + if ( !viscontents ) { + return; + } + + planenum = p->onnode->planenum; + bestside = NULL; + bestdot = 0; + + for ( j = 0 ; j < 2 ; j++ ) + { + n = p->nodes[j]; + p1 = &mapplanes[p->onnode->planenum]; + for ( bb = n->brushlist ; bb ; bb = bb->next ) + { + brush = bb->original; + if ( !( brush->contents & viscontents ) ) { + continue; + } + for ( i = 0 ; i < brush->numsides ; i++ ) + { + side = &brush->original_sides[i]; + if ( side->flags & SFL_BEVEL ) { + continue; + } + if ( side->texinfo == TEXINFO_NODE ) { + continue; // non-visible + } + if ( ( side->planenum & ~1 ) == planenum ) { // exact match + bestside = &brush->original_sides[i]; + goto gotit; + } //end if + // see how close the match is + p2 = &mapplanes[side->planenum & ~1]; + dot = DotProduct( p1->normal, p2->normal ); + if ( dot > bestdot ) { + bestdot = dot; + bestside = side; + } //end if + } //end for + } //end for + } //end for + +gotit: + if ( !bestside ) { + Log_Print( "WARNING: side not found for portal\n" ); + } + + p->sidefound = true; + p->side = bestside; +} //end of the function FindPortalSide +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void MarkVisibleSides_r( node_t *node ) { + portal_t *p; + int s; + + if ( node->planenum != PLANENUM_LEAF ) { + MarkVisibleSides_r( node->children[0] ); + MarkVisibleSides_r( node->children[1] ); + return; + } //end if + + // empty leaves are never boundary leaves + if ( !node->contents ) { + return; + } + + // see if there is a visible face + for ( p = node->portals ; p ; p = p->next[!s] ) + { + s = ( p->nodes[0] == node ); + if ( !p->onnode ) { + continue; // edge of world + } + if ( !p->sidefound ) { + FindPortalSide( p ); + } + if ( p->side ) { + p->side->flags |= SFL_VISIBLE; + } + } //end for +} //end of the function MarkVisibleSides_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void MarkVisibleSides( tree_t *tree, int startbrush, int endbrush ) { + int i, j; + mapbrush_t *mb; + int numsides; + + Log_Print( "--- MarkVisibleSides ---\n" ); + + // clear all the visible flags + for ( i = startbrush ; i < endbrush ; i++ ) + { + mb = &mapbrushes[i]; + + numsides = mb->numsides; + for ( j = 0 ; j < numsides ; j++ ) + mb->original_sides[j].flags &= ~SFL_VISIBLE; + } + + // set visible flags on the sides that are used by portals + MarkVisibleSides_r( tree->headnode ); +} //end of the function MarkVisibleSides + diff --git a/src/bspc/prtfile.c b/src/bspc/prtfile.c new file mode 100644 index 0000000..f412d5a --- /dev/null +++ b/src/bspc/prtfile.c @@ -0,0 +1,287 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// NO LONGER USED +#if 0 +#include "qbsp.h" + +extern dleaf_t dleafs[MAX_MAP_LEAFS]; +/* +============================================================================== + +PORTAL FILE GENERATION + +Save out name.prt for qvis to read +============================================================================== +*/ + + +#define PORTALFILE "PRT1" + +FILE *pf; +int num_visclusters; // clusters the player can be in +int num_visportals; + +void WriteFloat2( FILE *f, vec_t v ) { + if ( fabs( v - Q_rint( v ) ) < 0.001 ) { + fprintf( f,"%i ",(int)Q_rint( v ) ); + } else { + fprintf( f,"%f ",v ); + } +} + +/* +================= +WritePortalFile_r +================= +*/ +void WritePortalFile_r( node_t *node ) { + int i, s; + portal_t *p; + winding_t *w; + vec3_t normal; + vec_t dist; + + // decision node + if ( node->planenum != PLANENUM_LEAF && !node->detail_seperator ) { + WritePortalFile_r( node->children[0] ); + WritePortalFile_r( node->children[1] ); + return; + } + + if ( node->contents & CONTENTS_SOLID ) { + return; + } + + for ( p = node->portals ; p ; p = p->next[s] ) + { + w = p->winding; + s = ( p->nodes[1] == node ); + if ( w && p->nodes[0] == node ) { + if ( !Portal_VisFlood( p ) ) { + continue; + } + // write out to the file + + // sometimes planes get turned around when they are very near + // the changeover point between different axis. interpret the + // plane the same way vis will, and flip the side orders if needed + // FIXME: is this still relevent? + WindingPlane( w, normal, &dist ); + if ( DotProduct( p->plane.normal, normal ) < 0.99 ) { // backwards... + fprintf( pf,"%i %i %i ",w->numpoints, p->nodes[1]->cluster, p->nodes[0]->cluster ); + } else { + fprintf( pf,"%i %i %i ",w->numpoints, p->nodes[0]->cluster, p->nodes[1]->cluster ); + } + for ( i = 0 ; i < w->numpoints ; i++ ) + { + fprintf( pf,"(" ); + WriteFloat2( pf, w->p[i][0] ); + WriteFloat2( pf, w->p[i][1] ); + WriteFloat2( pf, w->p[i][2] ); + fprintf( pf,") " ); + } + fprintf( pf,"\n" ); + } + } + +} + +/* +================ +FillLeafNumbers_r + +All of the leafs under node will have the same cluster +================ +*/ +void FillLeafNumbers_r( node_t *node, int num ) { + if ( node->planenum == PLANENUM_LEAF ) { + if ( node->contents & CONTENTS_SOLID ) { + node->cluster = -1; + } else { + node->cluster = num; + } + return; + } + node->cluster = num; + FillLeafNumbers_r( node->children[0], num ); + FillLeafNumbers_r( node->children[1], num ); +} + +/* +================ +NumberLeafs_r +================ +*/ +void NumberLeafs_r( node_t *node ) { + portal_t *p; + + if ( node->planenum != PLANENUM_LEAF && !node->detail_seperator ) { // decision node + node->cluster = -99; + NumberLeafs_r( node->children[0] ); + NumberLeafs_r( node->children[1] ); + return; + } + + // either a leaf or a detail cluster + + if ( node->contents & CONTENTS_SOLID ) { // solid block, viewpoint never inside + node->cluster = -1; + return; + } + + FillLeafNumbers_r( node, num_visclusters ); + num_visclusters++; + + // count the portals + for ( p = node->portals ; p ; ) + { + if ( p->nodes[0] == node ) { // only write out from first leaf + if ( Portal_VisFlood( p ) ) { + num_visportals++; + } + p = p->next[0]; + } else { + p = p->next[1]; + } + } + +} + + +/* +================ +CreateVisPortals_r +================ +*/ +void CreateVisPortals_r( node_t *node ) { + // stop as soon as we get to a detail_seperator, which + // means that everything below is in a single cluster + if ( node->planenum == PLANENUM_LEAF || node->detail_seperator ) { + return; + } + + MakeNodePortal( node ); + SplitNodePortals( node ); + + CreateVisPortals_r( node->children[0] ); + CreateVisPortals_r( node->children[1] ); +} + +/* +================ +FinishVisPortals_r +================ +*/ +void FinishVisPortals2_r( node_t *node ) { + if ( node->planenum == PLANENUM_LEAF ) { + return; + } + + MakeNodePortal( node ); + SplitNodePortals( node ); + + FinishVisPortals2_r( node->children[0] ); + FinishVisPortals2_r( node->children[1] ); +} + +void FinishVisPortals_r( node_t *node ) { + if ( node->planenum == PLANENUM_LEAF ) { + return; + } + + if ( node->detail_seperator ) { + FinishVisPortals2_r( node ); + return; + } + + FinishVisPortals_r( node->children[0] ); + FinishVisPortals_r( node->children[1] ); +} + + +int clusterleaf; +void SaveClusters_r( node_t *node ) { + if ( node->planenum == PLANENUM_LEAF ) { + dleafs[clusterleaf++].cluster = node->cluster; + return; + } + SaveClusters_r( node->children[0] ); + SaveClusters_r( node->children[1] ); +} + +/* +================ +WritePortalFile +================ +*/ +void WritePortalFile( tree_t *tree ) { + char filename[1024]; + node_t *headnode; + + qprintf( "--- WritePortalFile ---\n" ); + + headnode = tree->headnode; + num_visclusters = 0; + num_visportals = 0; + + Tree_FreePortals_r( headnode ); + + MakeHeadnodePortals( tree ); + + CreateVisPortals_r( headnode ); + +// set the cluster field in every leaf and count the total number of portals + + NumberLeafs_r( headnode ); + +// write the file + sprintf( filename, "%s.prt", source ); + printf( "writing %s\n", filename ); + pf = fopen( filename, "w" ); + if ( !pf ) { + Error( "Error opening %s", filename ); + } + + fprintf( pf, "%s\n", PORTALFILE ); + fprintf( pf, "%i\n", num_visclusters ); + fprintf( pf, "%i\n", num_visportals ); + + qprintf( "%5i visclusters\n", num_visclusters ); + qprintf( "%5i visportals\n", num_visportals ); + + WritePortalFile_r( headnode ); + + fclose( pf ); + + // we need to store the clusters out now because ordering + // issues made us do this after writebsp... + clusterleaf = 1; + SaveClusters_r( headnode ); +} +#endif diff --git a/src/bspc/q2files.h b/src/bspc/q2files.h new file mode 100644 index 0000000..9c33e06 --- /dev/null +++ b/src/bspc/q2files.h @@ -0,0 +1,494 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +// +// qfiles.h: quake file formats +// This file must be identical in the quake and utils directories +// + +/* +======================================================================== + +The .pak files are just a linear collapse of a directory tree + +======================================================================== +*/ + +#define IDPAKHEADER ( ( 'K' << 24 ) + ( 'C' << 16 ) + ( 'A' << 8 ) + 'P' ) + +typedef struct +{ + char name[56]; + int filepos, filelen; +} dpackfile_t; + +typedef struct +{ + int ident; // == IDPAKHEADER + int dirofs; + int dirlen; +} dpackheader_t; + +#define MAX_FILES_IN_PACK 4096 + + +/* +======================================================================== + +PCX files are used for as many images as possible + +======================================================================== +*/ + +typedef struct +{ + char manufacturer; + char version; + char encoding; + char bits_per_pixel; + unsigned short xmin,ymin,xmax,ymax; + unsigned short hres,vres; + unsigned char palette[48]; + char reserved; + char color_planes; + unsigned short bytes_per_line; + unsigned short palette_type; + char filler[58]; + unsigned char data; // unbounded +} pcx_t; + + +/* +======================================================================== + +.MD2 triangle model file format + +======================================================================== +*/ + +#define IDALIASHEADER ( ( '2' << 24 ) + ( 'P' << 16 ) + ( 'D' << 8 ) + 'I' ) +#define ALIAS_VERSION 8 + +#define MAX_TRIANGLES 4096 +#define MAX_VERTS 2048 +#define MAX_FRAMES 512 +#define MAX_MD2SKINS 32 +#define MAX_SKINNAME 64 + +typedef struct +{ + short s; + short t; +} dstvert_t; + +typedef struct +{ + short index_xyz[3]; + short index_st[3]; +} dtriangle_t; + +typedef struct +{ + byte v[3]; // scaled byte to fit in frame mins/maxs + byte lightnormalindex; +} dtrivertx_t; + +#define DTRIVERTX_V0 0 +#define DTRIVERTX_V1 1 +#define DTRIVERTX_V2 2 +#define DTRIVERTX_LNI 3 +#define DTRIVERTX_SIZE 4 + +typedef struct +{ + float scale[3]; // multiply byte verts by this + float translate[3]; // then add this + char name[16]; // frame name from grabbing + dtrivertx_t verts[1]; // variable sized +} daliasframe_t; + + +// the glcmd format: +// a positive integer starts a tristrip command, followed by that many +// vertex structures. +// a negative integer starts a trifan command, followed by -x vertexes +// a zero indicates the end of the command list. +// a vertex consists of a floating point s, a floating point t, +// and an integer vertex index. + + +typedef struct +{ + int ident; + int version; + + int skinwidth; + int skinheight; + int framesize; // byte size of each frame + + int num_skins; + int num_xyz; + int num_st; // greater than num_xyz for seams + int num_tris; + int num_glcmds; // dwords in strip/fan command list + int num_frames; + + int ofs_skins; // each skin is a MAX_SKINNAME string + int ofs_st; // byte offset from start for stverts + int ofs_tris; // offset for dtriangles + int ofs_frames; // offset for first frame + int ofs_glcmds; + int ofs_end; // end of file + +} dmdl_t; + +/* +======================================================================== + +.SP2 sprite file format + +======================================================================== +*/ + +#define IDSPRITEHEADER ( ( '2' << 24 ) + ( 'S' << 16 ) + ( 'D' << 8 ) + 'I' ) +// little-endian "IDS2" +#define SPRITE_VERSION 2 + +typedef struct +{ + int width, height; + int origin_x, origin_y; // raster coordinates inside pic + char name[MAX_SKINNAME]; // name of pcx file +} dsprframe_t; + +typedef struct { + int ident; + int version; + int numframes; + dsprframe_t frames[1]; // variable sized +} dsprite_t; + +/* +============================================================================== + + .WAL texture file format + +============================================================================== +*/ + + +#define MIPLEVELS 4 +typedef struct miptex_s +{ + char name[32]; + unsigned width, height; + unsigned offsets[MIPLEVELS]; // four mip maps stored + char animname[32]; // next frame in animation chain + int flags; + int contents; + int value; +} miptex_t; + + + +/* +============================================================================== + + .BSP file format + +============================================================================== +*/ + +#define IDBSPHEADER ( ( 'P' << 24 ) + ( 'S' << 16 ) + ( 'B' << 8 ) + 'I' ) +// little-endian "IBSP" + +#define BSPVERSION 38 + + +// upper design bounds +// leaffaces, leafbrushes, planes, and verts are still bounded by +// 16 bit short limits +#define MAX_MAP_MODELS 1024 +#define MAX_MAP_BRUSHES 8192 +#define MAX_MAP_ENTITIES 2048 +#define MAX_MAP_ENTSTRING 0x40000 +#define MAX_MAP_TEXINFO 8192 + +#define MAX_MAP_AREAS 256 +#define MAX_MAP_AREAPORTALS 1024 +#define MAX_MAP_PLANES 65536 +#define MAX_MAP_NODES 65536 +#define MAX_MAP_BRUSHSIDES 65536 +#define MAX_MAP_LEAFS 65536 +#define MAX_MAP_VERTS 65536 +#define MAX_MAP_FACES 65536 +#define MAX_MAP_LEAFFACES 65536 +#define MAX_MAP_LEAFBRUSHES 65536 +#define MAX_MAP_PORTALS 65536 +#define MAX_MAP_EDGES 128000 +#define MAX_MAP_SURFEDGES 256000 +#define MAX_MAP_LIGHTING 0x320000 +#define MAX_MAP_VISIBILITY 0x280000 + +// key / value pair sizes + +#define MAX_KEY 32 +#define MAX_VALUE 1024 + +//============================================================================= + +typedef struct +{ + int fileofs, filelen; +} lump_t; + +#define LUMP_ENTITIES 0 +#define LUMP_PLANES 1 +#define LUMP_VERTEXES 2 +#define LUMP_VISIBILITY 3 +#define LUMP_NODES 4 +#define LUMP_TEXINFO 5 +#define LUMP_FACES 6 +#define LUMP_LIGHTING 7 +#define LUMP_LEAFS 8 +#define LUMP_LEAFFACES 9 +#define LUMP_LEAFBRUSHES 10 +#define LUMP_EDGES 11 +#define LUMP_SURFEDGES 12 +#define LUMP_MODELS 13 +#define LUMP_BRUSHES 14 +#define LUMP_BRUSHSIDES 15 +#define LUMP_POP 16 +#define LUMP_AREAS 17 +#define LUMP_AREAPORTALS 18 +#define HEADER_LUMPS 19 + +typedef struct +{ + int ident; + int version; + lump_t lumps[HEADER_LUMPS]; +} dheader_t; + +typedef struct +{ + float mins[3], maxs[3]; + float origin[3]; // for sounds or lights + int headnode; + int firstface, numfaces; // submodels just draw faces + // without walking the bsp tree +} dmodel_t; + + +typedef struct +{ + float point[3]; +} dvertex_t; + + +// 0-2 are axial planes +#define PLANE_X 0 +#define PLANE_Y 1 +#define PLANE_Z 2 + +// 3-5 are non-axial planes snapped to the nearest +#define PLANE_ANYX 3 +#define PLANE_ANYY 4 +#define PLANE_ANYZ 5 + +// planes (x&~1) and (x&~1)+1 are allways opposites + +typedef struct +{ + float normal[3]; + float dist; + int type; // PLANE_X - PLANE_ANYZ ?remove? trivial to regenerate +} dplane_t; + + +// contents flags are seperate bits +// a given brush can contribute multiple content bits +// multiple brushes can be in a single leaf + +// these definitions also need to be in q_shared.h! + +// lower bits are stronger, and will eat weaker brushes completely +#define CONTENTS_SOLID 1 // an eye is never valid in a solid +#define CONTENTS_WINDOW 2 // translucent, but not watery +#define CONTENTS_AUX 4 +#define CONTENTS_LAVA 8 +#define CONTENTS_SLIME 16 +#define CONTENTS_WATER 32 +#define CONTENTS_MIST 64 +#define LAST_VISIBLE_CONTENTS 64 + +// remaining contents are non-visible, and don't eat brushes + +#define CONTENTS_AREAPORTAL 0x8000 + +#define CONTENTS_PLAYERCLIP 0x10000 +#define CONTENTS_MONSTERCLIP 0x20000 + +// currents can be added to any other contents, and may be mixed +#define CONTENTS_CURRENT_0 0x40000 +#define CONTENTS_CURRENT_90 0x80000 +#define CONTENTS_CURRENT_180 0x100000 +#define CONTENTS_CURRENT_270 0x200000 +#define CONTENTS_CURRENT_UP 0x400000 +#define CONTENTS_CURRENT_DOWN 0x800000 + +#define CONTENTS_ORIGIN 0x1000000 // removed before bsping an entity + +#define CONTENTS_MONSTER 0x2000000 // should never be on a brush, only in game +#define CONTENTS_DEADMONSTER 0x4000000 +#define CONTENTS_DETAIL 0x8000000 // brushes to be added after vis leafs +//renamed because it's in conflict with the Q3A translucent contents +#define CONTENTS_Q2TRANSLUCENT 0x10000000 // auto set if any surface has trans +#define CONTENTS_LADDER 0x20000000 + + + +#define SURF_LIGHT 0x1 // value will hold the light strength + +#define SURF_SLICK 0x2 // effects game physics + +#define SURF_SKY 0x4 // don't draw, but add to skybox +#define SURF_WARP 0x8 // turbulent water warp +#define SURF_TRANS33 0x10 +#define SURF_TRANS66 0x20 +#define SURF_FLOWING 0x40 // scroll towards angle +#define SURF_NODRAW 0x80 // don't bother referencing the texture + +#define SURF_HINT 0x100 // make a primary bsp splitter +#define SURF_SKIP 0x200 // completely ignore, allowing non-closed brushes + + + +typedef struct +{ + int planenum; + int children[2]; // negative numbers are -(leafs+1), not nodes + short mins[3]; // for frustom culling + short maxs[3]; + unsigned short firstface; + unsigned short numfaces; // counting both sides +} dnode_t; + + +typedef struct texinfo_s +{ + float vecs[2][4]; // [s/t][xyz offset] + int flags; // miptex flags + overrides + int value; // light emission, etc + char texture[32]; // texture name (textures/*.wal) + int nexttexinfo; // for animations, -1 = end of chain +} texinfo_t; + + +// note that edge 0 is never used, because negative edge nums are used for +// counterclockwise use of the edge in a face +typedef struct +{ + unsigned short v[2]; // vertex numbers +} dedge_t; + +#define MAXLIGHTMAPS 4 +typedef struct +{ + unsigned short planenum; + short side; + + int firstedge; // we must support > 64k edges + short numedges; + short texinfo; + +// lighting info + byte styles[MAXLIGHTMAPS]; + int lightofs; // start of [numstyles*surfsize] samples +} dface_t; + +typedef struct +{ + int contents; // OR of all brushes (not needed?) + + short cluster; + short area; + + short mins[3]; // for frustum culling + short maxs[3]; + + unsigned short firstleafface; + unsigned short numleaffaces; + + unsigned short firstleafbrush; + unsigned short numleafbrushes; +} dleaf_t; + +typedef struct +{ + unsigned short planenum; // facing out of the leaf + short texinfo; +} dbrushside_t; + +typedef struct +{ + int firstside; + int numsides; + int contents; +} dbrush_t; + +#define ANGLE_UP -1 +#define ANGLE_DOWN -2 + + +// the visibility lump consists of a header with a count, then +// byte offsets for the PVS and PHS of each cluster, then the raw +// compressed bit vectors +#define DVIS_PVS 0 +#define DVIS_PHS 1 +typedef struct +{ + int numclusters; + int bitofs[8][2]; // bitofs[numclusters][2] +} dvis_t; + +// each area has a list of portals that lead into other areas +// when portals are closed, other areas may not be visible or +// hearable even if the vis info says that it should be +typedef struct +{ + int portalnum; + int otherarea; +} dareaportal_t; + +typedef struct +{ + int numareaportals; + int firstareaportal; +} darea_t; diff --git a/src/bspc/q3files.h b/src/bspc/q3files.h new file mode 100644 index 0000000..78d3f79 --- /dev/null +++ b/src/bspc/q3files.h @@ -0,0 +1,379 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +#ifndef __QFILES_H__ +#define __QFILES_H__ + +// +// qfiles.h: quake file formats +// This file must be identical in the quake and utils directories +// + +// surface geometry should not exceed these limits +#define SHADER_MAX_VERTEXES 1000 +#define SHADER_MAX_INDEXES ( 6 * SHADER_MAX_VERTEXES ) + + +// the maximum size of game reletive pathnames +#define MAX_QPATH 64 + + +/* +======================================================================== + +PCX files are used for 8 bit images + +======================================================================== +*/ +/* +typedef struct { + char manufacturer; + char version; + char encoding; + char bits_per_pixel; + unsigned short xmin,ymin,xmax,ymax; + unsigned short hres,vres; + unsigned char palette[48]; + char reserved; + char color_planes; + unsigned short bytes_per_line; + unsigned short palette_type; + char filler[58]; + unsigned char data; // unbounded +} pcx_t; +*/ + +/* +======================================================================== + +TGA files are used for 24/32 bit images + +======================================================================== +*/ +/* +typedef struct _TargaHeader { + unsigned char id_length, colormap_type, image_type; + unsigned short colormap_index, colormap_length; + unsigned char colormap_size; + unsigned short x_origin, y_origin, width, height; + unsigned char pixel_size, attributes; +} TargaHeader; +*/ + +/* +======================================================================== + +.MD3 triangle model file format + +======================================================================== +*/ + +#define MD3_IDENT ( ( '3' << 24 ) + ( 'P' << 16 ) + ( 'D' << 8 ) + 'I' ) +#define MD3_VERSION 15 + +// limits +#define MD3_MAX_LODS 4 +#define MD3_MAX_TRIANGLES 8192 // per surface +#define MD3_MAX_VERTS 4096 // per surface +#define MD3_MAX_SHADERS 256 // per surface +#define MD3_MAX_FRAMES 1024 // per model +#define MD3_MAX_SURFACES 32 // per model +#define MD3_MAX_TAGS 16 // per frame + +// vertex scales +#define MD3_XYZ_SCALE ( 1.0 / 64 ) + +typedef struct md3Frame_s { + vec3_t bounds[2]; + vec3_t localOrigin; + float radius; + char name[16]; +} md3Frame_t; + +typedef struct md3Tag_s { + char name[MAX_QPATH]; // tag name + vec3_t origin; + vec3_t axis[3]; +} md3Tag_t; + +/* +** md3Surface_t +** +** CHUNK SIZE +** header sizeof( md3Surface_t ) +** shaders sizeof( md3Shader_t ) * numShaders +** triangles[0] sizeof( md3Triangle_t ) * numTriangles +** st sizeof( md3St_t ) * numVerts +** XyzNormals sizeof( md3XyzNormal_t ) * numVerts * numFrames +*/ + +typedef struct { + int ident; // + + char name[MAX_QPATH]; // polyset name + + int flags; + int numFrames; // all surfaces in a model should have the same + + int numShaders; // all surfaces in a model should have the same + int numVerts; + + int numTriangles; + int ofsTriangles; + + int ofsShaders; // offset from start of md3Surface_t + int ofsSt; // texture coords are common for all frames + int ofsXyzNormals; // numVerts * numFrames + + int ofsEnd; // next surface follows +} md3Surface_t; + +typedef struct { + char name[MAX_QPATH]; + int shaderIndex; // for in-game use +} md3Shader_t; + +typedef struct { + int indexes[3]; +} md3Triangle_t; + +typedef struct { + float st[2]; +} md3St_t; + +typedef struct { + short xyz[3]; + short normal; +} md3XyzNormal_t; + +typedef struct { + int ident; + int version; + + char name[MAX_QPATH]; // model name + + int flags; + + int numFrames; + int numTags; + int numSurfaces; + + int numSkins; + + int ofsFrames; // offset for first frame + int ofsTags; // numFrames * numTags + int ofsSurfaces; // first surface, others follow + + int ofsEnd; // end of file +} md3Header_t; + + + +/* +============================================================================== + + .BSP file format + +============================================================================== +*/ + + +#define Q3_BSP_IDENT ( ( 'P' << 24 ) + ( 'S' << 16 ) + ( 'B' << 8 ) + 'I' ) +// little-endian "IBSP" + +#define Q3_BSP_VERSION 47 + + +// there shouldn't be any problem with increasing these values at the +// expense of more memory allocation in the utilities +#define Q3_MAX_MAP_MODELS 0x400 +#define Q3_MAX_MAP_BRUSHES 0x8000 +#define Q3_MAX_MAP_ENTITIES 0x800 +#define Q3_MAX_MAP_ENTSTRING 0x10000 +#define Q3_MAX_MAP_SHADERS 0x400 + +#define Q3_MAX_MAP_AREAS 0x100 // MAX_MAP_AREA_BYTES in q_shared must match! +#define Q3_MAX_MAP_FOGS 0x100 +#define Q3_MAX_MAP_PLANES 0x10000 +#define Q3_MAX_MAP_NODES 0x10000 +#define Q3_MAX_MAP_BRUSHSIDES 0x10000 +#define Q3_MAX_MAP_LEAFS 0x10000 +#define Q3_MAX_MAP_LEAFFACES 0x10000 +#define Q3_MAX_MAP_LEAFBRUSHES 0x10000 +#define Q3_MAX_MAP_PORTALS 0x10000 +#define Q3_MAX_MAP_LIGHTING 0x400000 +#define Q3_MAX_MAP_LIGHTGRID 0x400000 +#define Q3_MAX_MAP_VISIBILITY 0x200000 + +#define Q3_MAX_MAP_DRAW_SURFS 0x20000 +#define Q3_MAX_MAP_DRAW_VERTS 0x80000 +#define Q3_MAX_MAP_DRAW_INDEXES 0x80000 + + +// key / value pair sizes in the entities lump +#define Q3_MAX_KEY 32 +#define Q3_MAX_VALUE 1024 + +// the editor uses these predefined yaw angles to orient entities up or down +#define ANGLE_UP -1 +#define ANGLE_DOWN -2 + +#define LIGHTMAP_WIDTH 128 +#define LIGHTMAP_HEIGHT 128 + + +//============================================================================= + + +typedef struct { + int fileofs, filelen; +} q3_lump_t; + +#define Q3_LUMP_ENTITIES 0 +#define Q3_LUMP_SHADERS 1 +#define Q3_LUMP_PLANES 2 +#define Q3_LUMP_NODES 3 +#define Q3_LUMP_LEAFS 4 +#define Q3_LUMP_LEAFSURFACES 5 +#define Q3_LUMP_LEAFBRUSHES 6 +#define Q3_LUMP_MODELS 7 +#define Q3_LUMP_BRUSHES 8 +#define Q3_LUMP_BRUSHSIDES 9 +#define Q3_LUMP_DRAWVERTS 10 +#define Q3_LUMP_DRAWINDEXES 11 +#define Q3_LUMP_FOGS 12 +#define Q3_LUMP_SURFACES 13 +#define Q3_LUMP_LIGHTMAPS 14 +#define Q3_LUMP_LIGHTGRID 15 +#define Q3_LUMP_VISIBILITY 16 +#define Q3_HEADER_LUMPS 17 + +typedef struct { + int ident; + int version; + + q3_lump_t lumps[Q3_HEADER_LUMPS]; +} q3_dheader_t; + +typedef struct { + float mins[3], maxs[3]; + int firstSurface, numSurfaces; + int firstBrush, numBrushes; +} q3_dmodel_t; + +typedef struct { + char shader[MAX_QPATH]; + int surfaceFlags; + int contentFlags; +} q3_dshader_t; + +// planes (x&~1) and (x&~1)+1 are allways opposites + +typedef struct { + float normal[3]; + float dist; +} q3_dplane_t; + +typedef struct { + int planeNum; + int children[2]; // negative numbers are -(leafs+1), not nodes + int mins[3]; // for frustom culling + int maxs[3]; +} q3_dnode_t; + +typedef struct { + int cluster; // -1 = opaque cluster (do I still store these?) + int area; + + int mins[3]; // for frustum culling + int maxs[3]; + + int firstLeafSurface; + int numLeafSurfaces; + + int firstLeafBrush; + int numLeafBrushes; +} q3_dleaf_t; + +typedef struct { + int planeNum; // positive plane side faces out of the leaf + int shaderNum; +} q3_dbrushside_t; + +typedef struct { + int firstSide; + int numSides; + int shaderNum; // the shader that determines the contents flags +} q3_dbrush_t; + +typedef struct { + char shader[MAX_QPATH]; + int brushNum; + int visibleSide; // the brush side that ray tests need to clip against (-1 == none) +} q3_dfog_t; + +typedef struct { + vec3_t xyz; + float st[2]; + float lightmap[2]; + vec3_t normal; + byte color[4]; +} q3_drawVert_t; + +typedef enum { + MST_BAD, + MST_PLANAR, + MST_PATCH, + MST_TRIANGLE_SOUP, + MST_FLARE +} q3_mapSurfaceType_t; + +typedef struct { + int shaderNum; + int fogNum; + int surfaceType; + + int firstVert; + int numVerts; + + int firstIndex; + int numIndexes; + + int lightmapNum; + int lightmapX, lightmapY; + int lightmapWidth, lightmapHeight; + + vec3_t lightmapOrigin; + vec3_t lightmapVecs[3]; // for patches, [0] and [1] are lodbounds + + int patchWidth; + int patchHeight; +} q3_dsurface_t; + + +#endif diff --git a/src/bspc/qbsp.h b/src/bspc/qbsp.h new file mode 100644 index 0000000..ceb257d --- /dev/null +++ b/src/bspc/qbsp.h @@ -0,0 +1,498 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + + +#if defined( WIN32 ) || defined( _WIN32 ) +#include +#endif +#include +#include "l_cmd.h" +#include "l_math.h" +#include "l_poly.h" +#include "l_threads.h" +#include "../botlib/l_script.h" +#include "l_bsp_ent.h" +#include "q2files.h" +#include "l_mem.h" +#include "l_utils.h" +#include "l_log.h" +#include "l_qfiles.h" + +//Mr Elusive shit +#define ME +#define DEBUG +#define NODELIST +#define SIN + +#define MAX_BRUSH_SIDES 128 //maximum number of sides per brush +#define CLIP_EPSILON 0.1 +//#define MAX_MAP_BOUNDS 65535 +#define MAX_MAP_BOUNDS ( 128 * 1024 ) // (SA) (9/17/01) new map dimensions (from Q3TA) +#define BOGUS_RANGE ( MAX_MAP_BOUNDS + 128 ) //somewhere outside the map + +#define TEXINFO_NODE -1 //side is allready on a node +#define PLANENUM_LEAF -1 //used for leaf nodes +#define MAXEDGES 20 //maximum number of face edges +#define MAX_NODE_BRUSHES 8 //maximum brushes in a node +//side flags +#define SFL_TESTED 1 +#define SFL_VISIBLE 2 +#define SFL_BEVEL 4 +#define SFL_TEXTURED 8 +#define SFL_CURVE 16 + +//map plane +typedef struct plane_s +{ + vec3_t normal; + vec_t dist; + int type; + int signbits; + struct plane_s *hash_chain; +} plane_t; +//brush texture +typedef struct +{ + vec_t shift[2]; + vec_t rotate; + vec_t scale[2]; + char name[32]; + int flags; + int value; +} brush_texture_t; +//brush side +typedef struct side_s +{ + int planenum; // map plane this side is in + int texinfo; // texture reference + winding_t *winding; // winding of this side + struct side_s *original; // bspbrush_t sides will reference the mapbrush_t sides + int lightinfo; // for SIN only + int contents; // from miptex + int surf; // from miptex + unsigned short flags; // side flags +} side_t; //sizeof(side_t) = 36 +//map brush +typedef struct mapbrush_s +{ + int entitynum; + int brushnum; + + int contents; +#ifdef ME + int expansionbbox; //bbox used for expansion of the brush + int leafnum; + int modelnum; +#endif + + vec3_t mins, maxs; + + int numsides; + side_t *original_sides; +} mapbrush_t; +//bsp face +typedef struct face_s +{ + struct face_s *next; // on node + + // the chain of faces off of a node can be merged or split, + // but each face_t along the way will remain in the chain + // until the entire tree is freed + struct face_s *merged; // if set, this face isn't valid anymore + struct face_s *split[2]; // if set, this face isn't valid anymore + + struct portal_s *portal; + int texinfo; +#ifdef SIN + int lightinfo; +#endif + int planenum; + int contents; // faces in different contents can't merge + int outputnumber; + winding_t *w; + int numpoints; + qboolean badstartvert; // tjunctions cannot be fixed without a midpoint vertex + int vertexnums[MAXEDGES]; +} face_t; +//bsp brush +typedef struct bspbrush_s +{ + struct bspbrush_s *next; + vec3_t mins, maxs; + int side, testside; // side of node during construction + mapbrush_t *original; + int numsides; + side_t sides[6]; // variably sized +} bspbrush_t; //sizeof(bspbrush_t) = 44 + numsides * sizeof(side_t) +//bsp node +typedef struct node_s +{ + //both leafs and nodes + int planenum; // -1 = leaf node + struct node_s *parent; + vec3_t mins, maxs; // valid after portalization + bspbrush_t *volume; // one for each leaf/node + + // nodes only + qboolean detail_seperator; // a detail brush caused the split + side_t *side; // the side that created the node + struct node_s *children[2]; + face_t *faces; + + // leafs only + bspbrush_t *brushlist; // fragments of all brushes in this leaf + int contents; // OR of all brush contents + int occupied; // 1 or greater can reach entity + entity_t *occupant; // for leak file testing + int cluster; // for portalfile writing + int area; // for areaportals + struct portal_s *portals; // also on nodes during construction +#ifdef NODELIST + struct node_s *next; //next node in the nodelist +#endif +#ifdef ME + int expansionbboxes; //OR of all bboxes used for expansion of the brushes + int modelnum; +#endif +} node_t; //sizeof(node_t) = 80 bytes +//bsp portal +typedef struct portal_s +{ + plane_t plane; + node_t *onnode; // NULL = outside box + node_t *nodes[2]; // [0] = front side of plane + struct portal_s *next[2]; + winding_t *winding; + + qboolean sidefound; // false if ->side hasn't been checked + side_t *side; // NULL = non-visible + face_t *face[2]; // output face in bsp file +#ifdef ME + struct tmp_face_s *tmpface; //pointer to the tmpface created for this portal + int planenum; //number of the map plane used by the portal +#endif +} portal_t; +//bsp tree +typedef struct +{ + node_t *headnode; + node_t outside_node; + vec3_t mins, maxs; +} tree_t; + +//============================================================================= +// bspc.c +//============================================================================= + +extern qboolean noprune; +extern qboolean nodetail; +extern qboolean fulldetail; +extern qboolean nomerge; +extern qboolean nosubdiv; +extern qboolean nowater; +extern qboolean noweld; +extern qboolean noshare; +extern qboolean notjunc; +extern qboolean onlyents; +#ifdef ME +extern qboolean nocsg; +extern qboolean create_aas; +extern qboolean freetree; +extern qboolean lessbrushes; +extern qboolean nobrushmerge; +extern qboolean cancelconversion; +extern qboolean noliquids; +extern qboolean capsule_collision; +extern qboolean writeaasmap; +#endif //ME + +extern float subdivide_size; +extern vec_t microvolume; + +extern char outbase[32]; +extern char source[1024]; + +//============================================================================= +// map.c +//============================================================================= + +#define MAX_MAPFILE_PLANES 128000 +#define MAX_MAPFILE_BRUSHES 65535 //16384 +#define MAX_MAPFILE_BRUSHSIDES ( MAX_MAPFILE_BRUSHES * 8 ) +#define MAX_MAPFILE_TEXINFO 8192 + +extern int entity_num; + +extern plane_t mapplanes[MAX_MAPFILE_PLANES]; +extern int nummapplanes; +extern int mapplaneusers[MAX_MAPFILE_PLANES]; + +extern int nummapbrushes; +extern mapbrush_t mapbrushes[MAX_MAPFILE_BRUSHES]; + +extern vec3_t map_mins, map_maxs; + +extern int nummapbrushsides; +extern side_t brushsides[MAX_MAPFILE_BRUSHSIDES]; +extern brush_texture_t side_brushtextures[MAX_MAPFILE_BRUSHSIDES]; + +#ifdef ME + +typedef struct +{ + float vecs[2][4]; // [s/t][xyz offset] + int flags; // miptex flags + overrides + int value; + char texture[64]; // texture name (textures/*.wal) + int nexttexinfo; // for animations, -1 = end of chain +} map_texinfo_t; + +extern map_texinfo_t map_texinfo[MAX_MAPFILE_TEXINFO]; +extern int map_numtexinfo; +#define NODESTACKSIZE 1024 + +#define MAPTYPE_QUAKE1 1 +#define MAPTYPE_QUAKE2 2 +#define MAPTYPE_QUAKE3 3 +#define MAPTYPE_HALFLIFE 4 +#define MAPTYPE_SIN 5 + +extern int nodestack[NODESTACKSIZE]; +extern int *nodestackptr; +extern int nodestacksize; +extern int brushmodelnumbers[MAX_MAPFILE_BRUSHES]; +extern int dbrushleafnums[MAX_MAPFILE_BRUSHES]; +extern int dplanes2mapplanes[MAX_MAPFILE_PLANES]; + +extern int loadedmaptype; +#endif //ME + +extern int c_boxbevels; +extern int c_edgebevels; +extern int c_areaportals; +extern int c_clipbrushes; +extern int c_squattbrushes; + +//finds a float plane for the given normal and distance +int FindFloatPlane( vec3_t normal, vec_t dist ); +//returns the plane type for the given normal +int PlaneTypeForNormal( vec3_t normal ); +//returns the plane defined by the three given points +int PlaneFromPoints( int *p0, int *p1, int *p2 ); +//add bevels to the map brush +void AddBrushBevels( mapbrush_t *b ); +//makes brush side windings for the brush +qboolean MakeBrushWindings( mapbrush_t *ob ); +//marks brush bevels of the brush as bevel +void MarkBrushBevels( mapbrush_t *brush ); +//returns true if the map brush already exists +int BrushExists( mapbrush_t *brush ); +//loads a map from a bsp file +int LoadMapFromBSP( struct quakefile_s *qf ); +//resets map loading +void ResetMapLoading( void ); +//print some map info +void PrintMapInfo( void ); +//writes a map file (type depending on loaded map type) +void WriteMapFile( char *filename ); + +//============================================================================= +// map_q2.c +//============================================================================= + +void Q2_ResetMapLoading( void ); +//loads a Quake2 map file +void Q2_LoadMapFile( char *filename ); +//loads a map from a Quake2 bsp file +void Q2_LoadMapFromBSP( char *filename, int offset, int length ); + +//============================================================================= +// map_q1.c +//============================================================================= + +void Q1_ResetMapLoading( void ); +//loads a Quake2 map file +void Q1_LoadMapFile( char *filename ); +//loads a map from a Quake1 bsp file +void Q1_LoadMapFromBSP( char *filename, int offset, int length ); + +//============================================================================= +// map_q3.c +//============================================================================= +void Q3_ResetMapLoading( void ); +//loads a map from a Quake3 bsp file +void Q3_LoadMapFromBSP( struct quakefile_s *qf ); + +//============================================================================= +// map_sin.c +//============================================================================= + +void Sin_ResetMapLoading( void ); +//loads a Sin map file +void Sin_LoadMapFile( char *filename ); +//loads a map from a Sin bsp file +void Sin_LoadMapFromBSP( char *filename, int offset, int length ); + +//============================================================================= +// map_hl.c +//============================================================================= + +void HL_ResetMapLoading( void ); +//loads a Half-Life map file +void HL_LoadMapFile( char *filename ); +//loads a map from a Half-Life bsp file +void HL_LoadMapFromBSP( char *filename, int offset, int length ); + +//============================================================================= +// textures.c +//============================================================================= + +typedef struct +{ + char name[64]; + int flags; + int value; + int contents; + char animname[64]; +} textureref_t; + +#define MAX_MAP_TEXTURES 1024 + +extern textureref_t textureref[MAX_MAP_TEXTURES]; + +int FindMiptex( char *name ); +int TexinfoForBrushTexture( plane_t *plane, brush_texture_t *bt, vec3_t origin ); +void TextureAxisFromPlane( plane_t *pln, vec3_t xv, vec3_t yv ); + +//============================================================================= +// csg +//============================================================================= + +bspbrush_t *MakeBspBrushList( int startbrush, int endbrush, vec3_t clipmins, vec3_t clipmaxs ); +bspbrush_t *ChopBrushes( bspbrush_t *head ); +bspbrush_t *InitialBrushList( bspbrush_t *list ); +bspbrush_t *OptimizedBrushList( bspbrush_t *list ); +void WriteBrushMap( char *name, bspbrush_t *list ); +void CheckBSPBrush( bspbrush_t *brush ); +void BSPBrushWindings( bspbrush_t *brush ); +bspbrush_t *TryMergeBrushes( bspbrush_t *brush1, bspbrush_t *brush2 ); +tree_t *ProcessWorldBrushes( int brush_start, int brush_end ); + +//============================================================================= +// brushbsp +//============================================================================= + +#define PSIDE_FRONT 1 +#define PSIDE_BACK 2 +#define PSIDE_BOTH ( PSIDE_FRONT | PSIDE_BACK ) +#define PSIDE_FACING 4 + +void WriteBrushList( char *name, bspbrush_t *brush, qboolean onlyvis ); +bspbrush_t *CopyBrush( bspbrush_t *brush ); +void SplitBrush( bspbrush_t *brush, int planenum, bspbrush_t **front, bspbrush_t **back ); +node_t *AllocNode( void ); +bspbrush_t *AllocBrush( int numsides ); +int CountBrushList( bspbrush_t *brushes ); +void FreeBrush( bspbrush_t *brushes ); +vec_t BrushVolume( bspbrush_t *brush ); +void BoundBrush( bspbrush_t *brush ); +void FreeBrushList( bspbrush_t *brushes ); +tree_t *BrushBSP( bspbrush_t *brushlist, vec3_t mins, vec3_t maxs ); + +bspbrush_t *BrushFromBounds( vec3_t mins, vec3_t maxs ); +int BrushMostlyOnSide( bspbrush_t *brush, plane_t *plane ); +qboolean WindingIsHuge( winding_t *w ); +qboolean WindingIsTiny( winding_t *w ); +void ResetBrushBSP( void ); + +//============================================================================= +// portals.c +//============================================================================= + +int VisibleContents( int contents ); +void MakeHeadnodePortals( tree_t *tree ); +void MakeNodePortal( node_t *node ); +void SplitNodePortals( node_t *node ); +qboolean Portal_VisFlood( portal_t *p ); +qboolean FloodEntities( tree_t *tree ); +void FillOutside( node_t *headnode ); +void FloodAreas( tree_t *tree ); +void MarkVisibleSides( tree_t *tree, int start, int end ); +void FreePortal( portal_t *p ); +void EmitAreaPortals( node_t *headnode ); +void MakeTreePortals( tree_t *tree ); + +//============================================================================= +// glfile.c +//============================================================================= + +void OutputWinding( winding_t *w, FILE *glview ); +void WriteGLView( tree_t *tree, char *source ); + +//============================================================================= +// gldraw.c +//============================================================================= + +extern vec3_t draw_mins, draw_maxs; +extern qboolean drawflag; + +void Draw_ClearWindow( void ); +void DrawWinding( winding_t *w ); +void GLS_BeginScene( void ); +void GLS_Winding( winding_t *w, int code ); +void GLS_EndScene( void ); + +//============================================================================= +// leakfile.c +//============================================================================= + +void LeakFile( tree_t *tree ); + +//============================================================================= +// tree.c +//============================================================================= + +tree_t *Tree_Alloc( void ); +void Tree_Free( tree_t *tree ); +void Tree_Free_r( node_t *node ); +void Tree_Print_r( node_t *node, int depth ); +void Tree_FreePortals_r( node_t *node ); +void Tree_PruneNodes_r( node_t *node ); +void Tree_PruneNodes( node_t *node ); + +//============================================================================= +// faces.c +//============================================================================= + +face_t *AllocFace( void ); +void FreeFace( face_t *f ); +void MakeFaces( node_t *headnode ); +void FixTjuncs( node_t *headnode ); +int GetEdge2( int v1, int v2, face_t *f ); +void MergeNodeFaces( node_t *node ); diff --git a/src/bspc/qfiles.h b/src/bspc/qfiles.h new file mode 100644 index 0000000..1a29769 --- /dev/null +++ b/src/bspc/qfiles.h @@ -0,0 +1,495 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +// +// qfiles.h: quake file formats +// This file must be identical in the quake and utils directories +// + +/* +======================================================================== + +The .pak files are just a linear collapse of a directory tree + +======================================================================== +*/ + +#define IDPAKHEADER ( ( 'K' << 24 ) + ( 'C' << 16 ) + ( 'A' << 8 ) + 'P' ) + +typedef struct +{ + char name[56]; + int filepos, filelen; +} dpackfile_t; + +typedef struct +{ + int ident; // == IDPAKHEADER + int dirofs; + int dirlen; +} dpackheader_t; + +#define MAX_FILES_IN_PACK 4096 + + +/* +======================================================================== + +PCX files are used for as many images as possible + +======================================================================== +*/ + +typedef struct +{ + char manufacturer; + char version; + char encoding; + char bits_per_pixel; + unsigned short xmin,ymin,xmax,ymax; + unsigned short hres,vres; + unsigned char palette[48]; + char reserved; + char color_planes; + unsigned short bytes_per_line; + unsigned short palette_type; + char filler[58]; + unsigned char data; // unbounded +} pcx_t; + + +/* +======================================================================== + +.MD2 triangle model file format + +======================================================================== +*/ + +#define IDALIASHEADER ( ( '2' << 24 ) + ( 'P' << 16 ) + ( 'D' << 8 ) + 'I' ) +#define ALIAS_VERSION 8 + +#define MAX_TRIANGLES 4096 +#define MAX_VERTS 2048 +#define MAX_FRAMES 512 +#define MAX_MD2SKINS 32 +#define MAX_SKINNAME 64 + +typedef struct +{ + short s; + short t; +} dstvert_t; + +typedef struct +{ + short index_xyz[3]; + short index_st[3]; +} dtriangle_t; + +typedef struct +{ + byte v[3]; // scaled byte to fit in frame mins/maxs + byte lightnormalindex; +} dtrivertx_t; + +#define DTRIVERTX_V0 0 +#define DTRIVERTX_V1 1 +#define DTRIVERTX_V2 2 +#define DTRIVERTX_LNI 3 +#define DTRIVERTX_SIZE 4 + +typedef struct +{ + float scale[3]; // multiply byte verts by this + float translate[3]; // then add this + char name[16]; // frame name from grabbing + dtrivertx_t verts[1]; // variable sized +} daliasframe_t; + + +// the glcmd format: +// a positive integer starts a tristrip command, followed by that many +// vertex structures. +// a negative integer starts a trifan command, followed by -x vertexes +// a zero indicates the end of the command list. +// a vertex consists of a floating point s, a floating point t, +// and an integer vertex index. + + +typedef struct +{ + int ident; + int version; + + int skinwidth; + int skinheight; + int framesize; // byte size of each frame + + int num_skins; + int num_xyz; + int num_st; // greater than num_xyz for seams + int num_tris; + int num_glcmds; // dwords in strip/fan command list + int num_frames; + + int ofs_skins; // each skin is a MAX_SKINNAME string + int ofs_st; // byte offset from start for stverts + int ofs_tris; // offset for dtriangles + int ofs_frames; // offset for first frame + int ofs_glcmds; + int ofs_end; // end of file + +} dmdl_t; + +/* +======================================================================== + +.SP2 sprite file format + +======================================================================== +*/ + +#define IDSPRITEHEADER ( ( '2' << 24 ) + ( 'S' << 16 ) + ( 'D' << 8 ) + 'I' ) +// little-endian "IDS2" +#define SPRITE_VERSION 2 + +typedef struct +{ + int width, height; + int origin_x, origin_y; // raster coordinates inside pic + char name[MAX_SKINNAME]; // name of pcx file +} dsprframe_t; + +typedef struct { + int ident; + int version; + int numframes; + dsprframe_t frames[1]; // variable sized +} dsprite_t; + +/* +============================================================================== + + .WAL texture file format + +============================================================================== +*/ + + +#define MIPLEVELS 4 +typedef struct miptex_s +{ + char name[32]; + unsigned width, height; + unsigned offsets[MIPLEVELS]; // four mip maps stored + char animname[32]; // next frame in animation chain + int flags; + int contents; + int value; +} miptex_t; + + + +/* +============================================================================== + + .BSP file format + +============================================================================== +*/ + +#define IDBSPHEADER ( ( 'P' << 24 ) + ( 'S' << 16 ) + ( 'B' << 8 ) + 'I' ) +// little-endian "IBSP" + +#define BSPVERSION 38 + + +// upper design bounds +// leaffaces, leafbrushes, planes, and verts are still bounded by +// 16 bit short limits +#define MAX_MAP_MODELS 1024 +#define MAX_MAP_BRUSHES 8192 +#define MAX_MAP_ENTITIES 2048 +#define MAX_MAP_ENTSTRING 0x40000 +#define MAX_MAP_TEXINFO 8192 + +#define MAX_MAP_AREAS 256 +#define MAX_MAP_AREAPORTALS 1024 +#define MAX_MAP_PLANES 65536 +#define MAX_MAP_NODES 65536 +#define MAX_MAP_BRUSHSIDES 65536 +#define MAX_MAP_LEAFS 65536 +#define MAX_MAP_VERTS 65536 +#define MAX_MAP_FACES 65536 +#define MAX_MAP_LEAFFACES 65536 +#define MAX_MAP_LEAFBRUSHES 65536 +#define MAX_MAP_PORTALS 65536 +#define MAX_MAP_EDGES 128000 +#define MAX_MAP_SURFEDGES 256000 +#define MAX_MAP_LIGHTING 0x320000 +#define MAX_MAP_VISIBILITY 0x280000 + +// key / value pair sizes + +#define MAX_KEY 32 +#define MAX_VALUE 1024 + +//============================================================================= + +typedef struct +{ + int fileofs, filelen; +} lump_t; + +#define LUMP_ENTITIES 0 +#define LUMP_PLANES 1 +#define LUMP_VERTEXES 2 +#define LUMP_VISIBILITY 3 +#define LUMP_NODES 4 +#define LUMP_TEXINFO 5 +#define LUMP_FACES 6 +#define LUMP_LIGHTING 7 +#define LUMP_LEAFS 8 +#define LUMP_LEAFFACES 9 +#define LUMP_LEAFBRUSHES 10 +#define LUMP_EDGES 11 +#define LUMP_SURFEDGES 12 +#define LUMP_MODELS 13 +#define LUMP_BRUSHES 14 +#define LUMP_BRUSHSIDES 15 +#define LUMP_POP 16 +#define LUMP_AREAS 17 +#define LUMP_AREAPORTALS 18 +#define HEADER_LUMPS 19 + +typedef struct +{ + int ident; + int version; + lump_t lumps[HEADER_LUMPS]; +} dheader_t; + +typedef struct +{ + float mins[3], maxs[3]; + float origin[3]; // for sounds or lights + int headnode; + int firstface, numfaces; // submodels just draw faces + // without walking the bsp tree +} dmodel_t; + + +typedef struct +{ + float point[3]; +} dvertex_t; + + +// 0-2 are axial planes +#define PLANE_X 0 +#define PLANE_Y 1 +#define PLANE_Z 2 + +// 3-5 are non-axial planes snapped to the nearest +#define PLANE_ANYX 3 +#define PLANE_ANYY 4 +#define PLANE_ANYZ 5 + +// planes (x&~1) and (x&~1)+1 are allways opposites + +typedef struct +{ + float normal[3]; + float dist; + int type; // PLANE_X - PLANE_ANYZ ?remove? trivial to regenerate +} dplane_t; + + +// contents flags are seperate bits +// a given brush can contribute multiple content bits +// multiple brushes can be in a single leaf + +// these definitions also need to be in q_shared.h! + +// lower bits are stronger, and will eat weaker brushes completely +#define CONTENTS_SOLID 1 // an eye is never valid in a solid +#define CONTENTS_WINDOW 2 // translucent, but not watery +#define CONTENTS_AUX 4 +#define CONTENTS_LAVA 8 +#define CONTENTS_SLIME 16 +#define CONTENTS_WATER 32 +#define CONTENTS_MIST 64 +#define LAST_VISIBLE_CONTENTS 64 + +// remaining contents are non-visible, and don't eat brushes + +#define CONTENTS_AREAPORTAL 0x8000 + +#define CONTENTS_PLAYERCLIP 0x10000 +#define CONTENTS_MONSTERCLIP 0x20000 + +// currents can be added to any other contents, and may be mixed +#define CONTENTS_CURRENT_0 0x40000 +#define CONTENTS_CURRENT_90 0x80000 +#define CONTENTS_CURRENT_180 0x100000 +#define CONTENTS_CURRENT_270 0x200000 +#define CONTENTS_CURRENT_UP 0x400000 +#define CONTENTS_CURRENT_DOWN 0x800000 + +#define CONTENTS_ORIGIN 0x1000000 // removed before bsping an entity + +#define CONTENTS_MONSTER 0x2000000 // should never be on a brush, only in game +#define CONTENTS_DEADMONSTER 0x4000000 +#define CONTENTS_DETAIL 0x8000000 // brushes to be added after vis leafs +//renamed because it's in conflict with the Q3A translucent contents +#define CONTENTS_Q2TRANSLUCENT 0x10000000 // auto set if any surface has trans +#define CONTENTS_LADDER 0x20000000 + + + +#define SURF_LIGHT 0x1 // value will hold the light strength + +#define SURF_SLICK 0x2 // effects game physics + +#define SURF_SKY 0x4 // don't draw, but add to skybox +#define SURF_WARP 0x8 // turbulent water warp +#define SURF_TRANS33 0x10 +#define SURF_TRANS66 0x20 +#define SURF_FLOWING 0x40 // scroll towards angle +#define SURF_NODRAW 0x80 // don't bother referencing the texture + +#define SURF_HINT 0x100 // make a primary bsp splitter +#define SURF_SKIP 0x200 // completely ignore, allowing non-closed brushes + +#define SURF_MONSTERSLICK 0x4000000 // slick surf that only affects ai's + + +typedef struct +{ + int planenum; + int children[2]; // negative numbers are -(leafs+1), not nodes + short mins[3]; // for frustom culling + short maxs[3]; + unsigned short firstface; + unsigned short numfaces; // counting both sides +} dnode_t; + + +typedef struct texinfo_s +{ + float vecs[2][4]; // [s/t][xyz offset] + int flags; // miptex flags + overrides + int value; // light emission, etc + char texture[32]; // texture name (textures/*.wal) + int nexttexinfo; // for animations, -1 = end of chain +} texinfo_t; + + +// note that edge 0 is never used, because negative edge nums are used for +// counterclockwise use of the edge in a face +typedef struct +{ + unsigned short v[2]; // vertex numbers +} dedge_t; + +#define MAXLIGHTMAPS 4 +typedef struct +{ + unsigned short planenum; + short side; + + int firstedge; // we must support > 64k edges + short numedges; + short texinfo; + +// lighting info + byte styles[MAXLIGHTMAPS]; + int lightofs; // start of [numstyles*surfsize] samples +} dface_t; + +typedef struct +{ + int contents; // OR of all brushes (not needed?) + + short cluster; + short area; + + short mins[3]; // for frustum culling + short maxs[3]; + + unsigned short firstleafface; + unsigned short numleaffaces; + + unsigned short firstleafbrush; + unsigned short numleafbrushes; +} dleaf_t; + +typedef struct +{ + unsigned short planenum; // facing out of the leaf + short texinfo; +} dbrushside_t; + +typedef struct +{ + int firstside; + int numsides; + int contents; +} dbrush_t; + +#define ANGLE_UP -1 +#define ANGLE_DOWN -2 + + +// the visibility lump consists of a header with a count, then +// byte offsets for the PVS and PHS of each cluster, then the raw +// compressed bit vectors +#define DVIS_PVS 0 +#define DVIS_PHS 1 +typedef struct +{ + int numclusters; + int bitofs[8][2]; // bitofs[numclusters][2] +} dvis_t; + +// each area has a list of portals that lead into other areas +// when portals are closed, other areas may not be visible or +// hearable even if the vis info says that it should be +typedef struct +{ + int portalnum; + int otherarea; +} dareaportal_t; + +typedef struct +{ + int numareaportals; + int firstareaportal; +} darea_t; diff --git a/src/bspc/sinfiles.h b/src/bspc/sinfiles.h new file mode 100644 index 0000000..ea5f79a --- /dev/null +++ b/src/bspc/sinfiles.h @@ -0,0 +1,372 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/* +============================================================================== + + .BSP file format + +============================================================================== +*/ + +#define SIN + +#define SINBSPVERSION 41 + +// upper design bounds +// leaffaces, leafbrushes, planes, and verts are still bounded by +// 16 bit short limits +#define SIN_MAX_MAP_MODELS 1024 +#define SIN_MAX_MAP_BRUSHES 8192 +#define SIN_MAX_MAP_ENTITIES 2048 +#define SIN_MAX_MAP_ENTSTRING 0x40000 +#define SIN_MAX_MAP_TEXINFO 8192 + +#define SIN_MAX_MAP_AREAS 256 +#define SIN_MAX_MAP_AREAPORTALS 1024 +#define SIN_MAX_MAP_PLANES 65536 +#define SIN_MAX_MAP_NODES 65536 +#define SIN_MAX_MAP_BRUSHSIDES 65536 +#define SIN_MAX_MAP_LEAFS 65536 +#define SIN_MAX_MAP_VERTS 65536 +#define SIN_MAX_MAP_FACES 65536 +#define SIN_MAX_MAP_LEAFFACES 65536 +#define SIN_MAX_MAP_LEAFBRUSHES 65536 +#define SIN_MAX_MAP_PORTALS 65536 +#define SIN_MAX_MAP_EDGES 128000 +#define SIN_MAX_MAP_SURFEDGES 256000 +#define SIN_MAX_MAP_LIGHTING 0x320000 +#define SIN_MAX_MAP_VISIBILITY 0x280000 + +#ifdef SIN +#define SIN_MAX_MAP_LIGHTINFO 8192 +#endif + +#ifdef SIN +#undef SIN_MAX_MAP_LIGHTING //undef the Quake2 bsp version +#define SIN_MAX_MAP_LIGHTING 0x300000 +#endif + +#ifdef SIN +#undef SIN_MAX_MAP_VISIBILITY //undef the Quake2 bsp version +#define SIN_MAX_MAP_VISIBILITY 0x280000 +#endif + +//============================================================================= + +typedef struct +{ + int fileofs, filelen; +} sin_lump_t; + +#define SIN_LUMP_ENTITIES 0 +#define SIN_LUMP_PLANES 1 +#define SIN_LUMP_VERTEXES 2 +#define SIN_LUMP_VISIBILITY 3 +#define SIN_LUMP_NODES 4 +#define SIN_LUMP_TEXINFO 5 +#define SIN_LUMP_FACES 6 +#define SIN_LUMP_LIGHTING 7 +#define SIN_LUMP_LEAFS 8 +#define SIN_LUMP_LEAFFACES 9 +#define SIN_LUMP_LEAFBRUSHES 10 +#define SIN_LUMP_EDGES 11 +#define SIN_LUMP_SURFEDGES 12 +#define SIN_LUMP_MODELS 13 +#define SIN_LUMP_BRUSHES 14 +#define SIN_LUMP_BRUSHSIDES 15 +#define SIN_LUMP_POP 16 +#define SIN_LUMP_AREAS 17 +#define SIN_LUMP_AREAPORTALS 18 + +#ifdef SIN +#define SIN_LUMP_LIGHTINFO 19 +#define SINHEADER_LUMPS 20 +#endif + +typedef struct +{ + int ident; + int version; + sin_lump_t lumps[SINHEADER_LUMPS]; +} sin_dheader_t; + +typedef struct +{ + float mins[3], maxs[3]; + float origin[3]; // for sounds or lights + int headnode; + int firstface, numfaces; // submodels just draw faces + // without walking the bsp tree +} sin_dmodel_t; + +typedef struct +{ + float point[3]; +} sin_dvertex_t; + + +// 0-2 are axial planes +#define PLANE_X 0 +#define PLANE_Y 1 +#define PLANE_Z 2 + +// 3-5 are non-axial planes snapped to the nearest +#define PLANE_ANYX 3 +#define PLANE_ANYY 4 +#define PLANE_ANYZ 5 + +// planes (x&~1) and (x&~1)+1 are allways opposites + +typedef struct +{ + float normal[3]; + float dist; + int type; // PLANE_X - PLANE_ANYZ ?remove? trivial to regenerate +} sin_dplane_t; + + +// contents flags are seperate bits +// a given brush can contribute multiple content bits +// multiple brushes can be in a single leaf + +// these definitions also need to be in q_shared.h! + +// lower bits are stronger, and will eat weaker brushes completely +#ifdef SIN +#define CONTENTS_FENCE 4 +#endif +// remaining contents are non-visible, and don't eat brushes + +#ifdef SIN +#define CONTENTS_DUMMYFENCE 0x1000 +#endif + +#ifdef SIN +#define SURF_MASKED 0x2 // surface texture is masked +#endif + +#define SURF_SKY 0x4 // don't draw, but add to skybox +#define SURF_WARP 0x8 // turbulent water warp + +#ifdef SIN +#define SURF_NONLIT 0x10 // surface is not lit +#define SURF_NOFILTER 0x20 // surface is not bi-linear filtered +#endif + +#define SURF_FLOWING 0x40 // scroll towards angle +#define SURF_NODRAW 0x80 // don't bother referencing the texture + +#define SURF_HINT 0x100 // make a primary bsp splitter +#define SURF_SKIP 0x200 // completely ignore, allowing non-closed brushes + +#ifdef SIN +#define SURF_CONVEYOR 0x40 // surface is not lit +#endif + +#ifdef SIN +#define SURF_WAVY 0x400 // surface has waves +#define SURF_RICOCHET 0x800 // projectiles bounce literally bounce off this surface +#define SURF_PRELIT 0x1000 // surface has intensity information for pre-lighting +#define SURF_MIRROR 0x2000 // surface is a mirror +#define SURF_CONSOLE 0x4000 // surface is a console +#define SURF_USECOLOR 0x8000 // surface is lit with non-lit * color +#define SURF_HARDWAREONLY 0x10000 // surface has been damaged +#define SURF_DAMAGE 0x20000 // surface can be damaged +#define SURF_WEAK 0x40000 // surface has weak hit points +#define SURF_NORMAL 0x80000 // surface has normal hit points +#define SURF_ADD 0x100000 // surface will be additive +#define SURF_ENVMAPPED 0x200000 // surface is envmapped +#define SURF_RANDOMANIMATE 0x400000 // surface start animating on a random frame +#define SURF_ANIMATE 0x800000 // surface animates +#define SURF_RNDTIME 0x1000000 // time between animations is random +#define SURF_TRANSLATE 0x2000000 // surface translates +#define SURF_NOMERGE 0x4000000 // surface is not merged in csg phase +#define SURF_TYPE_BIT0 0x8000000 // 0 bit of surface type +#define SURF_TYPE_BIT1 0x10000000 // 1 bit of surface type +#define SURF_TYPE_BIT2 0x20000000 // 2 bit of surface type +#define SURF_TYPE_BIT3 0x40000000 // 3 bit of surface type + +#define SURF_START_BIT 27 +#define SURFACETYPE_FROM_FLAGS( x ) ( ( x >> ( SURF_START_BIT ) ) & 0xf ) + + +#define SURF_TYPE_SHIFT( x ) ( ( x ) << ( SURF_START_BIT ) ) // macro for getting proper bit mask + +#define SURF_TYPE_NONE SURF_TYPE_SHIFT( 0 ) +#define SURF_TYPE_WOOD SURF_TYPE_SHIFT( 1 ) +#define SURF_TYPE_METAL SURF_TYPE_SHIFT( 2 ) +#define SURF_TYPE_STONE SURF_TYPE_SHIFT( 3 ) +#define SURF_TYPE_CONCRETE SURF_TYPE_SHIFT( 4 ) +#define SURF_TYPE_DIRT SURF_TYPE_SHIFT( 5 ) +#define SURF_TYPE_FLESH SURF_TYPE_SHIFT( 6 ) +#define SURF_TYPE_GRILL SURF_TYPE_SHIFT( 7 ) +#define SURF_TYPE_GLASS SURF_TYPE_SHIFT( 8 ) +#define SURF_TYPE_FABRIC SURF_TYPE_SHIFT( 9 ) +#define SURF_TYPE_MONITOR SURF_TYPE_SHIFT( 10 ) +#define SURF_TYPE_GRAVEL SURF_TYPE_SHIFT( 11 ) +#define SURF_TYPE_VEGETATION SURF_TYPE_SHIFT( 12 ) +#define SURF_TYPE_PAPER SURF_TYPE_SHIFT( 13 ) +#define SURF_TYPE_DUCT SURF_TYPE_SHIFT( 14 ) +#define SURF_TYPE_WATER SURF_TYPE_SHIFT( 15 ) +#endif + + +typedef struct +{ + int planenum; + int children[2]; // negative numbers are -(leafs+1), not nodes + short mins[3]; // for frustom culling + short maxs[3]; + unsigned short firstface; + unsigned short numfaces; // counting both sides +} sin_dnode_t; + +#ifdef SIN + +typedef struct sin_lightvalue_s +{ + int value; // light emission, etc + vec3_t color; + float direct; + float directangle; + float directstyle; + char directstylename[32]; +} sin_lightvalue_t; + +typedef struct sin_texinfo_s +{ + float vecs[2][4]; // [s/t][xyz offset] + int flags; // miptex flags + overrides + char texture[64]; // texture name (textures/*.wal) + int nexttexinfo; // for animations, -1 = end of chain + float trans_mag; + int trans_angle; + int base_angle; + float animtime; + float nonlit; + float translucence; + float friction; + float restitution; + vec3_t color; + char groupname[32]; +} sin_texinfo_t; + +#endif //SIN + +// note that edge 0 is never used, because negative edge nums are used for +// counterclockwise use of the edge in a face +typedef struct +{ + unsigned short v[2]; // vertex numbers +} sin_dedge_t; + +#ifdef MAXLIGHTMAPS +#undef MAXLIGHTMAPS +#endif +#define MAXLIGHTMAPS 16 +typedef struct +{ + unsigned short planenum; + short side; + + int firstedge; // we must support > 64k edges + short numedges; + short texinfo; + +// lighting info + byte styles[MAXLIGHTMAPS]; + int lightofs; // start of [numstyles*surfsize] samples +#ifdef SIN + int lightinfo; +#endif +} sin_dface_t; + +typedef struct +{ + int contents; // OR of all brushes (not needed?) + + short cluster; + short area; + + short mins[3]; // for frustum culling + short maxs[3]; + + unsigned short firstleafface; + unsigned short numleaffaces; + + unsigned short firstleafbrush; + unsigned short numleafbrushes; +} sin_dleaf_t; + +typedef struct +{ + unsigned short planenum; // facing out of the leaf + short texinfo; +#ifdef SIN + int lightinfo; +#endif +} sin_dbrushside_t; + +typedef struct +{ + int firstside; + int numsides; + int contents; +} sin_dbrush_t; + +#define ANGLE_UP -1 +#define ANGLE_DOWN -2 + + +// the visibility lump consists of a header with a count, then +// byte offsets for the PVS and PHS of each cluster, then the raw +// compressed bit vectors +#define DVIS_PVS 0 +#define DVIS_PHS 1 +typedef struct +{ + int numclusters; + int bitofs[8][2]; // bitofs[numclusters][2] +} sin_dvis_t; + +// each area has a list of portals that lead into other areas +// when portals are closed, other areas may not be visible or +// hearable even if the vis info says that it should be +typedef struct +{ + int portalnum; + int otherarea; +} sin_dareaportal_t; + +typedef struct +{ + int numareaportals; + int firstareaportal; +} sin_darea_t; diff --git a/src/bspc/textures.c b/src/bspc/textures.c new file mode 100644 index 0000000..9822b86 --- /dev/null +++ b/src/bspc/textures.c @@ -0,0 +1,249 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: textures.c +// Function: textures +// Programmer: Mr Elusive (MrElusive@worldentity.com) +// Last update: 1999-08-10 +// Tab Size: 3 +//=========================================================================== + +#include "qbsp.h" +#include "l_bsp_q2.h" + + +int nummiptex; +textureref_t textureref[MAX_MAP_TEXTURES]; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int FindMiptex( char *name ) { + int i; + char path[1024]; + miptex_t *mt; + + for ( i = 0; i < nummiptex; i++ ) + { + if ( !strcmp( name, textureref[i].name ) ) { + return i; + } //end if + } //end for + if ( nummiptex == MAX_MAP_TEXTURES ) { + Error( "MAX_MAP_TEXTURES" ); + } + strcpy( textureref[i].name, name ); + + // load the miptex to get the flags and values + sprintf( path, "%stextures/%s.wal", gamedir, name ); + if ( TryLoadFile( path, (void **)&mt ) != -1 ) { + textureref[i].value = LittleLong( mt->value ); + textureref[i].flags = LittleLong( mt->flags ); + textureref[i].contents = LittleLong( mt->contents ); + strcpy( textureref[i].animname, mt->animname ); + FreeMemory( mt ); + } //end if + nummiptex++; + + if ( textureref[i].animname[0] ) { + FindMiptex( textureref[i].animname ); + } + + return i; +} //end of the function FindMipTex +//=========================================================================== +//textureAxisFromPlane +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +vec3_t baseaxis[18] = +{ + {0,0,1}, {1,0,0}, {0,-1,0}, // floor + {0,0,-1}, {1,0,0}, {0,-1,0}, // ceiling + {1,0,0}, {0,1,0}, {0,0,-1}, // west wall + {-1,0,0}, {0,1,0}, {0,0,-1}, // east wall + {0,1,0}, {1,0,0}, {0,0,-1}, // south wall + {0,-1,0}, {1,0,0}, {0,0,-1} // north wall +}; + +void TextureAxisFromPlane( plane_t *pln, vec3_t xv, vec3_t yv ) { + int bestaxis; + vec_t dot,best; + int i; + + best = 0; + bestaxis = 0; + + for ( i = 0 ; i < 6 ; i++ ) + { + dot = DotProduct( pln->normal, baseaxis[i * 3] ); + if ( dot > best ) { + best = dot; + bestaxis = i; + } + } + + VectorCopy( baseaxis[bestaxis * 3 + 1], xv ); + VectorCopy( baseaxis[bestaxis * 3 + 2], yv ); +} //end of the function TextureAxisFromPlane +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int TexinfoForBrushTexture( plane_t *plane, brush_texture_t *bt, vec3_t origin ) { + vec3_t vecs[2]; + int sv, tv; + vec_t ang, sinv, cosv; + vec_t ns, nt; + texinfo_t tx, *tc; + int i, j, k; + float shift[2]; + brush_texture_t anim; + int mt; + + if ( !bt->name[0] ) { + return 0; + } + + memset( &tx, 0, sizeof( tx ) ); + strcpy( tx.texture, bt->name ); + + TextureAxisFromPlane( plane, vecs[0], vecs[1] ); + + shift[0] = DotProduct( origin, vecs[0] ); + shift[1] = DotProduct( origin, vecs[1] ); + + if ( !bt->scale[0] ) { + bt->scale[0] = 1; + } + if ( !bt->scale[1] ) { + bt->scale[1] = 1; + } + + +// rotate axis + if ( bt->rotate == 0 ) { + sinv = 0 ; cosv = 1; + } else if ( bt->rotate == 90 ) { + sinv = 1 ; cosv = 0; + } else if ( bt->rotate == 180 ) { + sinv = 0 ; cosv = -1; + } else if ( bt->rotate == 270 ) { + sinv = -1 ; cosv = 0; + } else + { + ang = bt->rotate / 180 * Q_PI; + sinv = sin( ang ); + cosv = cos( ang ); + } + + if ( vecs[0][0] ) { + sv = 0; + } else if ( vecs[0][1] ) { + sv = 1; + } else { + sv = 2; + } + + if ( vecs[1][0] ) { + tv = 0; + } else if ( vecs[1][1] ) { + tv = 1; + } else { + tv = 2; + } + + for ( i = 0 ; i < 2 ; i++ ) + { + ns = cosv * vecs[i][sv] - sinv * vecs[i][tv]; + nt = sinv * vecs[i][sv] + cosv * vecs[i][tv]; + vecs[i][sv] = ns; + vecs[i][tv] = nt; + } + + for ( i = 0 ; i < 2 ; i++ ) + for ( j = 0 ; j < 3 ; j++ ) + tx.vecs[i][j] = vecs[i][j] / bt->scale[i]; + + tx.vecs[0][3] = bt->shift[0] + shift[0]; + tx.vecs[1][3] = bt->shift[1] + shift[1]; + tx.flags = bt->flags; + tx.value = bt->value; + + // + // find the texinfo + // + tc = texinfo; + for ( i = 0 ; i < numtexinfo ; i++, tc++ ) + { + if ( tc->flags != tx.flags ) { + continue; + } + if ( tc->value != tx.value ) { + continue; + } + for ( j = 0 ; j < 2 ; j++ ) + { + if ( strcmp( tc->texture, tx.texture ) ) { + goto skip; + } + for ( k = 0 ; k < 4 ; k++ ) + { + if ( tc->vecs[j][k] != tx.vecs[j][k] ) { + goto skip; + } + } + } + return i; +skip:; + } + *tc = tx; + numtexinfo++; + + // load the next animation + mt = FindMiptex( bt->name ); + if ( textureref[mt].animname[0] ) { + anim = *bt; + strcpy( anim.name, textureref[mt].animname ); + tc->nexttexinfo = TexinfoForBrushTexture( plane, &anim, origin ); + } else { + tc->nexttexinfo = -1; + } + + + return i; +} //end of the function TexinfoForBrushTexture diff --git a/src/bspc/tree.c b/src/bspc/tree.c new file mode 100644 index 0000000..706ce24 --- /dev/null +++ b/src/bspc/tree.c @@ -0,0 +1,298 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: textures.c +// Function: textures +// Programmer: Mr Elusive (MrElusive@worldentity.com) +// Last update: 1999-08-10 +// Tab Size: 3 +//=========================================================================== + +#include "qbsp.h" + +extern int c_nodes; +int c_pruned; +int freedtreemem = 0; + +void RemovePortalFromNode( portal_t *portal, node_t *l ); + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +node_t *NodeForPoint( node_t *node, vec3_t origin ) { + plane_t *plane; + vec_t d; + + while ( node->planenum != PLANENUM_LEAF ) + { + plane = &mapplanes[node->planenum]; + d = DotProduct( origin, plane->normal ) - plane->dist; + if ( d >= 0 ) { + node = node->children[0]; + } else { + node = node->children[1]; + } + } + return node; +} //end of the function NodeForPoint +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Tree_FreePortals_r( node_t *node ) { + portal_t *p, *nextp; + int s; + + // free children + if ( node->planenum != PLANENUM_LEAF ) { + Tree_FreePortals_r( node->children[0] ); + Tree_FreePortals_r( node->children[1] ); + } + + // free portals + for ( p = node->portals; p; p = nextp ) + { + s = ( p->nodes[1] == node ); + nextp = p->next[s]; + + RemovePortalFromNode( p, p->nodes[!s] ); +#ifdef ME + if ( p->winding ) { + freedtreemem += MemorySize( p->winding ); + } + freedtreemem += MemorySize( p ); +#endif //ME + FreePortal( p ); + } + node->portals = NULL; +} //end of the function Tree_FreePortals_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Tree_Free_r( node_t *node ) { +// face_t *f, *nextf; + bspbrush_t *brush, *nextbrush; + + //free children + if ( node->planenum != PLANENUM_LEAF ) { + Tree_Free_r( node->children[0] ); + Tree_Free_r( node->children[1] ); + } //end if + //free bspbrushes +// FreeBrushList (node->brushlist); + for ( brush = node->brushlist; brush; brush = nextbrush ) + { + nextbrush = brush->next; +#ifdef ME + freedtreemem += MemorySize( brush ); +#endif //ME + FreeBrush( brush ); + } //end for + node->brushlist = NULL; + + /* + NOTE: only used when creating Q2 bsp + // free faces + for (f = node->faces; f; f = nextf) + { + nextf = f->next; +#ifdef ME + if (f->w) freedtreemem += MemorySize(f->w); + freedtreemem += sizeof(face_t); +#endif //ME + FreeFace(f); + } //end for + */ + + // free the node + if ( node->volume ) { +#ifdef ME + freedtreemem += MemorySize( node->volume ); +#endif //ME + FreeBrush( node->volume ); + } //end if + + if ( numthreads == 1 ) { + c_nodes--; + } +#ifdef ME + freedtreemem += MemorySize( node ); +#endif //ME + FreeMemory( node ); +} //end of the function Tree_Free_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Tree_Free( tree_t *tree ) { + //if no tree just return + if ( !tree ) { + return; + } + // + freedtreemem = 0; + // + Tree_FreePortals_r( tree->headnode ); + Tree_Free_r( tree->headnode ); +#ifdef ME + freedtreemem += MemorySize( tree ); +#endif //ME + FreeMemory( tree ); +#ifdef ME + Log_Print( "freed " ); + PrintMemorySize( freedtreemem ); + Log_Print( " of tree memory\n" ); +#endif //ME +} //end of the function Tree_Free +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +tree_t *Tree_Alloc( void ) { + tree_t *tree; + + tree = GetMemory( sizeof( *tree ) ); + memset( tree, 0, sizeof( *tree ) ); + ClearBounds( tree->mins, tree->maxs ); + + return tree; +} //end of the function Tree_Alloc +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Tree_Print_r( node_t *node, int depth ) { + int i; + plane_t *plane; + bspbrush_t *bb; + + for ( i = 0 ; i < depth ; i++ ) + printf( " " ); + if ( node->planenum == PLANENUM_LEAF ) { + if ( !node->brushlist ) { + printf( "NULL\n" ); + } else + { + for ( bb = node->brushlist ; bb ; bb = bb->next ) + printf( "%i ", bb->original->brushnum ); + printf( "\n" ); + } + return; + } + + plane = &mapplanes[node->planenum]; + printf( "#%i (%5.2f %5.2f %5.2f):%5.2f\n", node->planenum, + plane->normal[0], plane->normal[1], plane->normal[2], + plane->dist ); + Tree_Print_r( node->children[0], depth + 1 ); + Tree_Print_r( node->children[1], depth + 1 ); +} //end of the function Tree_Print_r +//=========================================================================== +// NODES THAT DON'T SEPERATE DIFFERENT CONTENTS CAN BE PRUNED +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Tree_PruneNodes_r( node_t *node ) { + bspbrush_t *b, *next; + + if ( node->planenum == PLANENUM_LEAF ) { + return; + } + + Tree_PruneNodes_r( node->children[0] ); + Tree_PruneNodes_r( node->children[1] ); + + if ( create_aas ) { + if ( ( node->children[0]->contents & CONTENTS_LADDER ) || + ( node->children[1]->contents & CONTENTS_LADDER ) ) { + return; + } + } + + if ( ( node->children[0]->contents & CONTENTS_SOLID ) + && ( node->children[1]->contents & CONTENTS_SOLID ) ) { + if ( node->faces ) { + Error( "node->faces seperating CONTENTS_SOLID" ); + } + if ( node->children[0]->faces || node->children[1]->faces ) { + Error( "!node->faces with children" ); + } + // FIXME: free stuff + node->planenum = PLANENUM_LEAF; + node->contents = CONTENTS_SOLID; + node->detail_seperator = false; + + if ( node->brushlist ) { + Error( "PruneNodes: node->brushlist" ); + } + // combine brush lists + node->brushlist = node->children[1]->brushlist; + + for ( b = node->children[0]->brushlist; b; b = next ) + { + next = b->next; + b->next = node->brushlist; + node->brushlist = b; + } //end for + //free the child nodes + FreeMemory( node->children[0] ); + FreeMemory( node->children[1] ); + //two nodes are cut away + c_pruned += 2; + } //end if +} //end of the function Tree_PruneNodes_r +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Tree_PruneNodes( node_t *node ) { + Log_Print( "------- Prune Nodes --------\n" ); + c_pruned = 0; + Tree_PruneNodes_r( node ); + Log_Print( "%5i pruned nodes\n", c_pruned ); +} //end of the function Tree_PruneNodes diff --git a/src/bspc/writebsp.c b/src/bspc/writebsp.c new file mode 100644 index 0000000..10bb718 --- /dev/null +++ b/src/bspc/writebsp.c @@ -0,0 +1,632 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// NO LONGER IN PROJECT +#if 0 +#include "qbsp.h" + +int c_nofaces; +int c_facenodes; + + +extern int numplanes; +extern int numfaces; +extern int numleaffaces; +extern int numleafs; +extern int numleafbrushes; +extern int numsurfedges; +extern int numnodes; + +extern int nummodels; +extern int numbrushsides; +extern int numbrushes; +extern int numvertexes; +extern int numedges; + +extern dplane_t dplanes[MAX_MAP_PLANES]; +extern dleaf_t dleafs[MAX_MAP_LEAFS]; +extern dleaf_t dleafs[MAX_MAP_LEAFS]; +extern dface_t dfaces[MAX_MAP_FACES]; +extern unsigned short dleafbrushes[MAX_MAP_LEAFBRUSHES]; +extern unsigned short dleaffaces[MAX_MAP_LEAFFACES]; +extern int dsurfedges[MAX_MAP_SURFEDGES]; +extern dnode_t dnodes[MAX_MAP_NODES]; +extern dmodel_t dmodels[MAX_MAP_MODELS]; +extern dbrush_t dbrushes[MAX_MAP_BRUSHES]; +extern dbrushside_t dbrushsides[MAX_MAP_BRUSHSIDES]; + +/* +========================================================= + +ONLY SAVE OUT PLANES THAT ARE ACTUALLY USED AS NODES + +========================================================= +*/ + +int planeused[MAX_MAP_PLANES]; + +/* +============ +EmitPlanes + +There is no oportunity to discard planes, because all of the original +brushes will be saved in the map. +============ +*/ +void EmitPlanes( void ) { + int i; + dplane_t *dp; + plane_t *mp; + //ME: this causes a crash?? +// int planetranslate[MAX_MAP_PLANES]; + + mp = mapplanes; + for ( i = 0 ; i < nummapplanes ; i++, mp++ ) + { + dp = &dplanes[numplanes]; +// planetranslate[i] = numplanes; + VectorCopy( mp->normal, dp->normal ); + dp->dist = mp->dist; + dp->type = mp->type; + numplanes++; + if ( numplanes >= MAX_MAP_PLANES ) { + Error( "MAX_MAP_PLANES" ); + } + } +} + + +//======================================================== + +void EmitMarkFace( dleaf_t *leaf_p, face_t *f ) { + int i; + int facenum; + + while ( f->merged ) + f = f->merged; + + if ( f->split[0] ) { + EmitMarkFace( leaf_p, f->split[0] ); + EmitMarkFace( leaf_p, f->split[1] ); + return; + } + + facenum = f->outputnumber; + if ( facenum == -1 ) { + return; // degenerate face + + } + if ( facenum < 0 || facenum >= numfaces ) { + Error( "Bad leafface" ); + } + for ( i = leaf_p->firstleafface ; i < numleaffaces ; i++ ) + if ( dleaffaces[i] == facenum ) { + break; + } // merged out face + if ( i == numleaffaces ) { + if ( numleaffaces >= MAX_MAP_LEAFFACES ) { + Error( "MAX_MAP_LEAFFACES" ); + } + + dleaffaces[numleaffaces] = facenum; + numleaffaces++; + } + +} + + +/* +================== +EmitLeaf +================== +*/ +void EmitLeaf( node_t *node ) { + dleaf_t *leaf_p; + portal_t *p; + int s; + face_t *f; + bspbrush_t *b; + int i; + int brushnum; + + // emit a leaf + if ( numleafs >= MAX_MAP_LEAFS ) { + Error( "MAX_MAP_LEAFS" ); + } + + leaf_p = &dleafs[numleafs]; + numleafs++; + + leaf_p->contents = node->contents; + leaf_p->cluster = node->cluster; + leaf_p->area = node->area; + + // + // write bounding box info + // + VectorCopy( node->mins, leaf_p->mins ); + VectorCopy( node->maxs, leaf_p->maxs ); + + // + // write the leafbrushes + // + leaf_p->firstleafbrush = numleafbrushes; + for ( b = node->brushlist ; b ; b = b->next ) + { + if ( numleafbrushes >= MAX_MAP_LEAFBRUSHES ) { + Error( "MAX_MAP_LEAFBRUSHES" ); + } + + brushnum = b->original - mapbrushes; + for ( i = leaf_p->firstleafbrush ; i < numleafbrushes ; i++ ) + if ( dleafbrushes[i] == brushnum ) { + break; + } + if ( i == numleafbrushes ) { + dleafbrushes[numleafbrushes] = brushnum; + numleafbrushes++; + } + } + leaf_p->numleafbrushes = numleafbrushes - leaf_p->firstleafbrush; + + // + // write the leaffaces + // + if ( leaf_p->contents & CONTENTS_SOLID ) { + return; // no leaffaces in solids + + } + leaf_p->firstleafface = numleaffaces; + + for ( p = node->portals ; p ; p = p->next[s] ) + { + s = ( p->nodes[1] == node ); + f = p->face[s]; + if ( !f ) { + continue; // not a visible portal + + } + EmitMarkFace( leaf_p, f ); + } + + leaf_p->numleaffaces = numleaffaces - leaf_p->firstleafface; +} + + +/* +================== +EmitFace +================== +*/ +void EmitFace( face_t *f ) { + dface_t *df; + int i; + int e; + + f->outputnumber = -1; + + if ( f->numpoints < 3 ) { + return; // degenerated + } + if ( f->merged || f->split[0] || f->split[1] ) { + return; // not a final face + } + + // save output number so leaffaces can use + f->outputnumber = numfaces; + + if ( numfaces >= MAX_MAP_FACES ) { + Error( "numfaces == MAX_MAP_FACES" ); + } + df = &dfaces[numfaces]; + numfaces++; + + // planenum is used by qlight, but not quake + df->planenum = f->planenum & ( ~1 ); + df->side = f->planenum & 1; + + df->firstedge = numsurfedges; + df->numedges = f->numpoints; + df->texinfo = f->texinfo; + for ( i = 0 ; i < f->numpoints ; i++ ) + { +// e = GetEdge (f->pts[i], f->pts[(i+1)%f->numpoints], f); + e = GetEdge2( f->vertexnums[i], f->vertexnums[( i + 1 ) % f->numpoints], f ); + if ( numsurfedges >= MAX_MAP_SURFEDGES ) { + Error( "numsurfedges == MAX_MAP_SURFEDGES" ); + } + dsurfedges[numsurfedges] = e; + numsurfedges++; + } +} + +/* +============ +EmitDrawingNode_r +============ +*/ +int EmitDrawNode_r( node_t *node ) { + dnode_t *n; + face_t *f; + int i; + + if ( node->planenum == PLANENUM_LEAF ) { + EmitLeaf( node ); + return -numleafs; + } + + // emit a node + if ( numnodes == MAX_MAP_NODES ) { + Error( "MAX_MAP_NODES" ); + } + n = &dnodes[numnodes]; + numnodes++; + + VectorCopy( node->mins, n->mins ); + VectorCopy( node->maxs, n->maxs ); + + planeused[node->planenum]++; + planeused[node->planenum ^ 1]++; + + if ( node->planenum & 1 ) { + Error( "WriteDrawNodes_r: odd planenum" ); + } + n->planenum = node->planenum; + n->firstface = numfaces; + + if ( !node->faces ) { + c_nofaces++; + } else { + c_facenodes++; + } + + for ( f = node->faces ; f ; f = f->next ) + EmitFace( f ); + + n->numfaces = numfaces - n->firstface; + + + // + // recursively output the other nodes + // + for ( i = 0 ; i < 2 ; i++ ) + { + if ( node->children[i]->planenum == PLANENUM_LEAF ) { + n->children[i] = -( numleafs + 1 ); + EmitLeaf( node->children[i] ); + } else + { + n->children[i] = numnodes; + EmitDrawNode_r( node->children[i] ); + } + } + + return n - dnodes; +} + +//========================================================= + + +/* +============ +WriteBSP +============ +*/ +void WriteBSP( node_t *headnode ) { + int oldfaces; + + c_nofaces = 0; + c_facenodes = 0; + + qprintf( "--- WriteBSP ---\n" ); + + oldfaces = numfaces; + dmodels[nummodels].headnode = EmitDrawNode_r( headnode ); +// EmitAreaPortals (headnode); + + qprintf( "%5i nodes with faces\n", c_facenodes ); + qprintf( "%5i nodes without faces\n", c_nofaces ); + qprintf( "%5i faces\n", numfaces - oldfaces ); +} + +//=========================================================== + +/* +============ +SetModelNumbers +============ +*/ +void SetModelNumbers( void ) { + int i; + int models; + char value[10]; + + models = 1; + for ( i = 1 ; i < num_entities ; i++ ) + { + if ( entities[i].numbrushes ) { + sprintf( value, "*%i", models ); + models++; + SetKeyValue( &entities[i], "model", value ); + } + } + +} + +/* +============ +SetLightStyles +============ +*/ +#define MAX_SWITCHED_LIGHTS 32 +void SetLightStyles( void ) { + int stylenum; + char *t; + entity_t *e; + int i, j; + char value[10]; + char lighttargets[MAX_SWITCHED_LIGHTS][64]; + + + // any light that is controlled (has a targetname) + // must have a unique style number generated for it + + stylenum = 0; + for ( i = 1 ; i < num_entities ; i++ ) + { + e = &entities[i]; + + t = ValueForKey( e, "classname" ); + if ( Q_strncasecmp( t, "light", 5 ) ) { + continue; + } + t = ValueForKey( e, "targetname" ); + if ( !t[0] ) { + continue; + } + + // find this targetname + for ( j = 0 ; j < stylenum ; j++ ) + if ( !strcmp( lighttargets[j], t ) ) { + break; + } + if ( j == stylenum ) { + if ( stylenum == MAX_SWITCHED_LIGHTS ) { + Error( "stylenum == MAX_SWITCHED_LIGHTS" ); + } + strcpy( lighttargets[j], t ); + stylenum++; + } + sprintf( value, "%i", 32 + j ); + SetKeyValue( e, "style", value ); + } + +} + +//=========================================================== + +/* +============ +EmitBrushes +============ +*/ +void EmitBrushes( void ) { + int i, j, bnum, s, x; + dbrush_t *db; + mapbrush_t *b; + dbrushside_t *cp; + vec3_t normal; + vec_t dist; + int planenum; + + numbrushsides = 0; + numbrushes = nummapbrushes; + + for ( bnum = 0 ; bnum < nummapbrushes ; bnum++ ) + { + b = &mapbrushes[bnum]; + db = &dbrushes[bnum]; + + db->contents = b->contents; + db->firstside = numbrushsides; + db->numsides = b->numsides; + for ( j = 0 ; j < b->numsides ; j++ ) + { + if ( numbrushsides == MAX_MAP_BRUSHSIDES ) { + Error( "MAX_MAP_BRUSHSIDES" ); + } + cp = &dbrushsides[numbrushsides]; + numbrushsides++; + cp->planenum = b->original_sides[j].planenum; + cp->texinfo = b->original_sides[j].texinfo; + } + +#ifdef ME + //for collision detection, bounding boxes are axial :) + //brushes are convex so just add dot or line touching planes on the sides of + //the brush parallell to the axis planes +#endif + // add any axis planes not contained in the brush to bevel off corners + for ( x = 0 ; x < 3 ; x++ ) + for ( s = -1 ; s <= 1 ; s += 2 ) + { + // add the plane + VectorCopy( vec3_origin, normal ); + normal[x] = s; + if ( s == -1 ) { + dist = -b->mins[x]; + } else { + dist = b->maxs[x]; + } + planenum = FindFloatPlane( normal, dist ); + for ( i = 0 ; i < b->numsides ; i++ ) + if ( b->original_sides[i].planenum == planenum ) { + break; + } + if ( i == b->numsides ) { + if ( numbrushsides >= MAX_MAP_BRUSHSIDES ) { + Error( "MAX_MAP_BRUSHSIDES" ); + } + + dbrushsides[numbrushsides].planenum = planenum; + dbrushsides[numbrushsides].texinfo = + dbrushsides[numbrushsides - 1].texinfo; + numbrushsides++; + db->numsides++; + } + } + + } + +} + +//=========================================================== + +/* +================== +BeginBSPFile +================== +*/ +void BeginBSPFile( void ) { + // these values may actually be initialized + // if the file existed when loaded, so clear them explicitly + nummodels = 0; + numfaces = 0; + numnodes = 0; + numbrushsides = 0; + numvertexes = 0; + numleaffaces = 0; + numleafbrushes = 0; + numsurfedges = 0; + + // edge 0 is not used, because 0 can't be negated + numedges = 1; + + // leave vertex 0 as an error + numvertexes = 1; + + // leave leaf 0 as an error + numleafs = 1; + dleafs[0].contents = CONTENTS_SOLID; +} + + +/* +============ +EndBSPFile +============ +*/ +void EndBSPFile( void ) { +#if 0 + char path[1024]; + int len; + byte *buf; +#endif + + + EmitBrushes(); + EmitPlanes(); + Q2_UnparseEntities(); + + // load the pop +#if 0 + sprintf( path, "%s/pics/pop.lmp", gamedir ); + len = LoadFile( path, &buf ); + memcpy( dpop, buf, sizeof( dpop ) ); + FreeMemory( buf ); +#endif +} + + +/* +================== +BeginModel +================== +*/ +int firstmodleaf; +extern int firstmodeledge; +extern int firstmodelface; +void BeginModel( void ) { + dmodel_t *mod; + int start, end; + mapbrush_t *b; + int j; + entity_t *e; + vec3_t mins, maxs; + + if ( nummodels == MAX_MAP_MODELS ) { + Error( "MAX_MAP_MODELS" ); + } + mod = &dmodels[nummodels]; + + mod->firstface = numfaces; + + firstmodleaf = numleafs; + firstmodeledge = numedges; + firstmodelface = numfaces; + + // + // bound the brushes + // + e = &entities[entity_num]; + + start = e->firstbrush; + end = start + e->numbrushes; + ClearBounds( mins, maxs ); + + for ( j = start ; j < end ; j++ ) + { + b = &mapbrushes[j]; + if ( !b->numsides ) { + continue; // not a real brush (origin brush) + } + AddPointToBounds( b->mins, mins, maxs ); + AddPointToBounds( b->maxs, mins, maxs ); + } + + VectorCopy( mins, mod->mins ); + VectorCopy( maxs, mod->maxs ); +} + + +/* +================== +EndModel +================== +*/ +void EndModel( void ) { + dmodel_t *mod; + + mod = &dmodels[nummodels]; + + mod->numfaces = numfaces - mod->firstface; + + nummodels++; +} + +#endif \ No newline at end of file diff --git a/src/cgame/cg_consolecmds.c b/src/cgame/cg_consolecmds.c new file mode 100644 index 0000000..1d94e7d --- /dev/null +++ b/src/cgame/cg_consolecmds.c @@ -0,0 +1,548 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +/* + * name: cg_consolecmds.c + * + * desc: text commands typed in at the local console, or executed by a key binding + * +*/ + + +#include "cg_local.h" +#include "../ui/ui_shared.h" + + + +void CG_TargetCommand_f( void ) { + int targetNum; + char test[4]; + + targetNum = CG_CrosshairPlayer(); + if ( !targetNum ) { + return; + } + + trap_Argv( 1, test, 4 ); + trap_SendConsoleCommand( va( "gc %i %i", targetNum, atoi( test ) ) ); +} + + + +/* +================= +CG_SizeUp_f + +Keybinding command +================= +*/ +static void CG_SizeUp_f( void ) { + trap_Cvar_Set( "cg_viewsize", va( "%i",(int)( cg_viewsize.integer + 10 ) ) ); +} + + +/* +================= +CG_SizeDown_f + +Keybinding command +================= +*/ +static void CG_SizeDown_f( void ) { + trap_Cvar_Set( "cg_viewsize", va( "%i",(int)( cg_viewsize.integer - 10 ) ) ); +} + + +/* +============= +CG_Viewpos_f + +Debugging command to print the current position +============= +*/ +static void CG_Viewpos_f( void ) { + CG_Printf( "(%i %i %i) : %i\n", (int)cg.refdef.vieworg[0], + (int)cg.refdef.vieworg[1], (int)cg.refdef.vieworg[2], + (int)cg.refdefViewAngles[YAW] ); +} + + +static void CG_ScoresDown_f( void ) { + if ( cg.scoresRequestTime + 2000 < cg.time ) { + // the scores are more than two seconds out of data, + // so request new ones + cg.scoresRequestTime = cg.time; + trap_SendClientCommand( "score" ); + + // leave the current scores up if they were already + // displayed, but if this is the first hit, clear them out + if ( !cg.showScores ) { + cg.showScores = qtrue; + cg.numScores = 0; + } + } else { + // show the cached contents even if they just pressed if it + // is within two seconds + cg.showScores = qtrue; + } +} + +static void CG_ScoresUp_f( void ) { + if ( cg.showScores ) { + cg.showScores = qfalse; + cg.scoreFadeTime = cg.time; + } +} + + +extern menuDef_t *menuScoreboard; +void Menu_Reset(); // FIXME: add to right include file + +static void CG_LoadHud_f( void ) { + char buff[1024]; + const char *hudSet; + memset( buff, 0, sizeof( buff ) ); + + String_Init(); + Menu_Reset(); + + trap_Cvar_VariableStringBuffer( "cg_hudFiles", buff, sizeof( buff ) ); + hudSet = buff; + if ( hudSet[0] == '\0' ) { + hudSet = "ui/hud.txt"; + } + + CG_LoadMenus( hudSet ); + menuScoreboard = NULL; +} + +// TTimo: defined but not used +/* +static void CG_scrollScoresDown_f( void) { + if (menuScoreboard && cg.scoreBoardShowing) { + Menu_ScrollFeeder(menuScoreboard, FEEDER_SCOREBOARD, qtrue); + Menu_ScrollFeeder(menuScoreboard, FEEDER_REDTEAM_LIST, qtrue); + Menu_ScrollFeeder(menuScoreboard, FEEDER_BLUETEAM_LIST, qtrue); + } +} + + +static void CG_scrollScoresUp_f( void) { + if (menuScoreboard && cg.scoreBoardShowing) { + Menu_ScrollFeeder(menuScoreboard, FEEDER_SCOREBOARD, qfalse); + Menu_ScrollFeeder(menuScoreboard, FEEDER_REDTEAM_LIST, qfalse); + Menu_ScrollFeeder(menuScoreboard, FEEDER_BLUETEAM_LIST, qfalse); + } +} + + +static void CG_spWin_f( void) { + trap_Cvar_Set("cg_cameraOrbit", "2"); + trap_Cvar_Set("cg_cameraOrbitDelay", "35"); + trap_Cvar_Set("cg_thirdPerson", "1"); + trap_Cvar_Set("cg_thirdPersonAngle", "0"); + trap_Cvar_Set("cg_thirdPersonRange", "100"); +// CG_AddBufferedSound(cgs.media.winnerSound); + //trap_S_StartLocalSound(cgs.media.winnerSound, CHAN_ANNOUNCER); + CG_CenterPrint("YOU WIN!", SCREEN_HEIGHT * .30, 0); +} + +static void CG_spLose_f( void) { + trap_Cvar_Set("cg_cameraOrbit", "2"); + trap_Cvar_Set("cg_cameraOrbitDelay", "35"); + trap_Cvar_Set("cg_thirdPerson", "1"); + trap_Cvar_Set("cg_thirdPersonAngle", "0"); + trap_Cvar_Set("cg_thirdPersonRange", "100"); +// CG_AddBufferedSound(cgs.media.loserSound); + //trap_S_StartLocalSound(cgs.media.loserSound, CHAN_ANNOUNCER); + CG_CenterPrint("YOU LOSE...", SCREEN_HEIGHT * .30, 0); +} +*/ + +//----(SA) item (key/pickup) drawing +static void CG_InventoryDown_f( void ) { + cg.showItems = qtrue; +} + +static void CG_InventoryUp_f( void ) { + cg.showItems = qfalse; + cg.itemFadeTime = cg.time; +} + +//----(SA) end + +static void CG_TellTarget_f( void ) { + int clientNum; + char command[128]; + char message[128]; + + clientNum = CG_CrosshairPlayer(); + if ( clientNum == -1 ) { + return; + } + + trap_Args( message, 128 ); + Com_sprintf( command, 128, "tell %i %s", clientNum, message ); + trap_SendClientCommand( command ); +} + +static void CG_TellAttacker_f( void ) { + int clientNum; + char command[128]; + char message[128]; + + clientNum = CG_LastAttacker(); + if ( clientNum == -1 ) { + return; + } + + trap_Args( message, 128 ); + Com_sprintf( command, 128, "tell %i %s", clientNum, message ); + trap_SendClientCommand( command ); +} + +// TTimo: unused +/* +static void CG_NextTeamMember_f( void ) { + CG_SelectNextPlayer(); +} + +static void CG_PrevTeamMember_f( void ) { + CG_SelectPrevPlayer(); +} +*/ + +/////////// cameras + +#define MAX_CAMERAS 64 // matches define in splines.cpp +qboolean cameraInuse[MAX_CAMERAS]; + +int CG_LoadCamera( const char *name ) { + int i; + for ( i = 1; i < MAX_CAMERAS; i++ ) { // start at '1' since '0' is always taken by the cutscene camera + if ( !cameraInuse[i] ) { + if ( trap_loadCamera( i, name ) ) { + cameraInuse[i] = qtrue; + return i; + } + } + } + return -1; +} + +void CG_FreeCamera( int camNum ) { + cameraInuse[camNum] = qfalse; +} + +/* +============== +CG_StartCamera +============== +*/ +void CG_StartCamera( const char *name, qboolean startBlack ) { + char lname[MAX_QPATH]; + + //if ( cg.predictedPlayerState.stats[STAT_HEALTH] <= 0 ) // don't allow camera to start if you're dead + // return; + + COM_StripExtension( name, lname ); //----(SA) added + strcat( lname, ".camera" ); + + if ( trap_loadCamera( CAM_PRIMARY, va( "cameras/%s", lname ) ) ) { + cg.cameraMode = qtrue; // camera on in cgame + if ( startBlack ) { + CG_Fade( 0, 0, 0, 255, cg.time, 0 ); // go black + } + trap_Cvar_Set( "cg_letterbox", "1" ); // go letterbox + trap_SendClientCommand( "startCamera" ); // camera on in game + trap_startCamera( CAM_PRIMARY, cg.time ); // camera on in client + } else { +//----(SA) removed check for cams in main dir + cg.cameraMode = qfalse; // camera off in cgame + trap_SendClientCommand( "stopCamera" ); // camera off in game + trap_stopCamera( CAM_PRIMARY ); // camera off in client + CG_Fade( 0, 0, 0, 0, cg.time, 0 ); // ensure fadeup + trap_Cvar_Set( "cg_letterbox", "0" ); + CG_Printf( "Unable to load camera %s\n",lname ); + } +} + +/* +============== +CG_SopCamera +============== +*/ +void CG_StopCamera( void ) { + cg.cameraMode = qfalse; // camera off in cgame + trap_SendClientCommand( "stopCamera" ); // camera off in game + trap_stopCamera( CAM_PRIMARY ); // camera off in client + trap_Cvar_Set( "cg_letterbox", "0" ); + + // fade back into world + CG_Fade( 0, 0, 0, 255, 0, 0 ); + CG_Fade( 0, 0, 0, 0, cg.time + 500, 2000 ); + +} + +static void CG_Camera_f( void ) { + char name[MAX_QPATH]; + trap_Argv( 1, name, sizeof( name ) ); + + CG_StartCamera( name, qfalse ); +} + +static void CG_Fade_f( void ) { + int r, g, b, a; + float duration; + + if ( trap_Argc() < 6 ) { + return; + } + + r = atof( CG_Argv( 1 ) ); + g = atof( CG_Argv( 2 ) ); + b = atof( CG_Argv( 3 ) ); + a = atof( CG_Argv( 4 ) ); + + duration = atof( CG_Argv( 5 ) ) * 1000; + + CG_Fade( r, g, b, a, cg.time, duration ); +} + +// TTimo unused +/* +// NERVE - SMF +static void CG_PickTeam_f( void ) { + const char *s; + char buf[144]; + + if(cgs.gametype != GT_WOLF) + return; + + // set map title + trap_Cvar_VariableStringBuffer( "sv_mapname", buf, sizeof( buf ) ); + trap_Cvar_Set( "mp_mapTitle", "MAP" ); //buf ); + + // set map description + s = CG_ConfigString( CS_MULTI_MAPDESC ); + if ( s ) + trap_Cvar_Set( "mp_mapDesc", s ); + + trap_UI_Popup( "UIMENU_WM_PICKTEAM" ); +} + +static void CG_PickPlayer_f( void ) { + const char *s; + char buf[144]; + + if(cgs.gametype != GT_WOLF) + return; + + // set map title + trap_Cvar_VariableStringBuffer( "sv_mapname", buf, sizeof( buf ) ); + trap_Cvar_Set( "mp_mapTitle", "MAP" ); //buf ); + + // set map description + s = CG_ConfigString( CS_MULTI_MAPDESC ); + if ( s ) + trap_Cvar_Set( "mp_mapDesc", s ); + + trap_UI_Popup( "UIMENU_WM_PICKPLAYER" ); +} +*/ + +static void CG_QuickMessage_f( void ) { + if ( cgs.gametype != GT_WOLF ) { + return; + } + trap_UI_Popup( "UIMENU_WM_QUICKMESSAGE" ); +} + +static void CG_OpenLimbo_f( void ) { + if ( cgs.gametype != GT_WOLF ) { + return; + } + trap_UI_Popup( "UIMENU_WM_LIMBO" ); +} + +static void CG_CloseLimbo_f( void ) { + if ( cgs.gametype != GT_WOLF ) { + return; + } + trap_UI_ClosePopup( "UIMENU_WM_LIMBO" ); +} + +static void CG_LimboMessage_f( void ) { + char teamStr[80], classStr[80], weapStr[80]; + + if ( cgs.gametype != GT_WOLF ) { + return; + } + + Q_strncpyz( teamStr, CG_Argv( 1 ), 80 ); + Q_strncpyz( classStr, CG_Argv( 2 ), 80 ); + Q_strncpyz( weapStr, CG_Argv( 3 ), 80 ); + + CG_CenterPrint( va( "You will spawn as a %s \n%s with a %s.", teamStr, classStr, weapStr ), + SCREEN_HEIGHT - ( SCREEN_HEIGHT * 0.25 ), SMALLCHAR_WIDTH ); +} +// -NERVE - SMF + +typedef struct { + char *cmd; + void ( *function )( void ); +} consoleCommand_t; + +static consoleCommand_t commands[] = { + { "testgun", CG_TestGun_f }, + { "testmodel", CG_TestModel_f }, + { "nextframe", CG_TestModelNextFrame_f }, + { "prevframe", CG_TestModelPrevFrame_f }, + { "nextskin", CG_TestModelNextSkin_f }, + { "prevskin", CG_TestModelPrevSkin_f }, + { "viewpos", CG_Viewpos_f }, + { "+scores", CG_ScoresDown_f }, + { "-scores", CG_ScoresUp_f }, + { "+inventory", CG_InventoryDown_f }, + { "-inventory", CG_InventoryUp_f }, +// { "+zoom", CG_ZoomDown_f }, // (SA) zoom moved to a wbutton so server can determine weapon firing based on zoom status +// { "-zoom", CG_ZoomUp_f }, + { "zoomin", CG_ZoomIn_f }, + { "zoomout", CG_ZoomOut_f }, + { "sizeup", CG_SizeUp_f }, + { "sizedown", CG_SizeDown_f }, + { "weaplastused", CG_LastWeaponUsed_f }, + { "weapnextinbank", CG_NextWeaponInBank_f }, + { "weapprevinbank", CG_PrevWeaponInBank_f }, + { "weapnext", CG_NextWeapon_f }, + { "weapprev", CG_PrevWeapon_f }, + { "weapalt", CG_AltWeapon_f }, + { "weapon", CG_Weapon_f }, + { "weaponbank", CG_WeaponBank_f }, + { "itemnext", CG_NextItem_f }, + { "itemprev", CG_PrevItem_f }, + { "item", CG_Item_f }, + { "tell_target", CG_TellTarget_f }, + { "tell_attacker", CG_TellAttacker_f }, + { "tcmd", CG_TargetCommand_f }, + { "loadhud", CG_LoadHud_f }, + { "loaddeferred", CG_LoadDeferredPlayers }, // spelling fixed (SA) + { "camera", CG_Camera_f }, // duffy + { "fade", CG_Fade_f }, // duffy + + // NERVE - SMF + { "mp_QuickMessage", CG_QuickMessage_f }, + { "OpenLimboMenu", CG_OpenLimbo_f }, + { "CloseLimboMenu", CG_CloseLimbo_f }, + { "LimboMessage", CG_LimboMessage_f } + // -NERVE - SMF +}; + + +/* +================= +CG_ConsoleCommand + +The string has been tokenized and can be retrieved with +Cmd_Argc() / Cmd_Argv() +================= +*/ +qboolean CG_ConsoleCommand( void ) { + const char *cmd; + int i; + + cmd = CG_Argv( 0 ); + + for ( i = 0 ; i < sizeof( commands ) / sizeof( commands[0] ) ; i++ ) { + if ( !Q_stricmp( cmd, commands[i].cmd ) ) { + commands[i].function(); + return qtrue; + } + } + + return qfalse; +} + + +/* +================= +CG_InitConsoleCommands + +Let the client system know about all of our commands +so it can perform tab completion +================= +*/ +void CG_InitConsoleCommands( void ) { + int i; + + for ( i = 0 ; i < sizeof( commands ) / sizeof( commands[0] ) ; i++ ) { + trap_AddCommand( commands[i].cmd ); + } + + // + // the game server will interpret these commands, which will be automatically + // forwarded to the server after they are not recognized locally + // + trap_AddCommand( "kill" ); + trap_AddCommand( "say" ); + trap_AddCommand( "say_team" ); + trap_AddCommand( "say_limbo" ); // NERVE - SMF + trap_AddCommand( "tell" ); +// trap_AddCommand ("vsay"); +// trap_AddCommand ("vsay_team"); +// trap_AddCommand ("vtell"); +// trap_AddCommand ("vtaunt"); +// trap_AddCommand ("vosay"); +// trap_AddCommand ("vosay_team"); +// trap_AddCommand ("votell"); + trap_AddCommand( "give" ); + trap_AddCommand( "god" ); + trap_AddCommand( "notarget" ); + trap_AddCommand( "noclip" ); + trap_AddCommand( "team" ); + trap_AddCommand( "follow" ); + trap_AddCommand( "levelshot" ); + trap_AddCommand( "addbot" ); + trap_AddCommand( "setviewpos" ); + trap_AddCommand( "callvote" ); + trap_AddCommand( "vote" ); +// trap_AddCommand ("callteamvote"); +// trap_AddCommand ("teamvote"); + trap_AddCommand( "stats" ); +// trap_AddCommand ("teamtask"); + trap_AddCommand( "loaddeferred" ); // spelling fixed (SA) + + trap_AddCommand( "startCamera" ); + trap_AddCommand( "stopCamera" ); + trap_AddCommand( "setCameraOrigin" ); + + // Rafael + trap_AddCommand( "nofatigue" ); + + trap_AddCommand( "setspawnpt" ); // NERVE - SMF +} diff --git a/src/cgame/cg_draw.c b/src/cgame/cg_draw.c new file mode 100644 index 0000000..b8e1c9b --- /dev/null +++ b/src/cgame/cg_draw.c @@ -0,0 +1,3620 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// cg_draw.c -- draw all of the graphical elements during +// active (after loading) gameplay + +#include "cg_local.h" +#include "../ui/ui_shared.h" + +//----(SA) added to make it easier to raise/lower our statsubar by only changing one thing +#define STATUSBARHEIGHT 452 +//----(SA) end + +extern displayContextDef_t cgDC; +menuDef_t *menuScoreboard = NULL; + +int sortedTeamPlayers[TEAM_MAXOVERLAY]; +int numSortedTeamPlayers; + +char systemChat[256]; +char teamChat1[256]; +char teamChat2[256]; + +//////////////////////// +//////////////////////// +////// new hud stuff +/////////////////////// +/////////////////////// + +int CG_Text_Width( const char *text, int font, float scale, int limit ) { + int count,len; + float out; + glyphInfo_t *glyph; + float useScale; + const char *s = text; + + fontInfo_t *fnt = &cgDC.Assets.textFont; + + if ( font == UI_FONT_DEFAULT ) { + if ( scale <= cg_smallFont.value ) { + fnt = &cgDC.Assets.smallFont; + } else if ( scale > cg_bigFont.value ) { + fnt = &cgDC.Assets.bigFont; + } + } else if ( font == UI_FONT_BIG ) { + fnt = &cgDC.Assets.bigFont; + } else if ( font == UI_FONT_SMALL ) { + fnt = &cgDC.Assets.smallFont; + } else if ( font == UI_FONT_HANDWRITING ) { + fnt = &cgDC.Assets.handwritingFont; + } + + useScale = scale * fnt->glyphScale; + out = 0; + if ( text ) { + len = strlen( text ); + if ( limit > 0 && len > limit ) { + len = limit; + } + count = 0; + while ( s && *s && count < len ) { + if ( Q_IsColorString( s ) ) { + s += 2; + continue; + } else { + glyph = &fnt->glyphs[(int)*s]; + out += glyph->xSkip; + s++; + count++; + } + } + } + return out * useScale; +} + +int CG_Text_Height( const char *text, int font, float scale, int limit ) { + int len, count; + float max; + glyphInfo_t *glyph; + float useScale; + const char *s = text; + + fontInfo_t *fnt = &cgDC.Assets.textFont; + if ( font == UI_FONT_DEFAULT ) { + if ( scale <= cg_smallFont.value ) { + fnt = &cgDC.Assets.smallFont; + } else if ( scale > cg_bigFont.value ) { + fnt = &cgDC.Assets.bigFont; + } + } else if ( font == UI_FONT_BIG ) { + fnt = &cgDC.Assets.bigFont; + } else if ( font == UI_FONT_SMALL ) { + fnt = &cgDC.Assets.smallFont; + } else if ( font == UI_FONT_HANDWRITING ) { + fnt = &cgDC.Assets.handwritingFont; + } + + useScale = scale * fnt->glyphScale; + max = 0; + if ( text ) { + len = strlen( text ); + if ( limit > 0 && len > limit ) { + len = limit; + } + count = 0; + while ( s && *s && count < len ) { + if ( Q_IsColorString( s ) ) { + s += 2; + continue; + } else { + glyph = &fnt->glyphs[(int)*s]; + if ( max < glyph->height ) { + max = glyph->height; + } + s++; + count++; + } + } + } + return max * useScale; +} + +void CG_Text_PaintChar( float x, float y, float width, float height, float scale, float s, float t, float s2, float t2, qhandle_t hShader ) { + float w, h; + w = width * scale; + h = height * scale; + CG_AdjustFrom640( &x, &y, &w, &h ); + trap_R_DrawStretchPic( x, y, w, h, s, t, s2, t2, hShader ); +} + +void CG_Text_Paint( float x, float y, int font, float scale, vec4_t color, const char *text, float adjust, int limit, int style ) { + int len, count; + vec4_t newColor; + glyphInfo_t *glyph; + float useScale; + fontInfo_t *fnt = &cgDC.Assets.textFont; + + if ( font == UI_FONT_DEFAULT ) { + if ( scale <= cg_smallFont.value ) { + fnt = &cgDC.Assets.smallFont; + } else if ( scale > cg_bigFont.value ) { + fnt = &cgDC.Assets.bigFont; + } + } else if ( font == UI_FONT_BIG ) { + fnt = &cgDC.Assets.bigFont; + } else if ( font == UI_FONT_SMALL ) { + fnt = &cgDC.Assets.smallFont; + } else if ( font == UI_FONT_HANDWRITING ) { + fnt = &cgDC.Assets.handwritingFont; + } + + useScale = scale * fnt->glyphScale; + + color[3] *= cg_hudAlpha.value; // (SA) adjust for cg_hudalpha + + if ( text ) { + const char *s = text; + trap_R_SetColor( color ); + memcpy( &newColor[0], &color[0], sizeof( vec4_t ) ); + len = strlen( text ); + if ( limit > 0 && len > limit ) { + len = limit; + } + count = 0; + while ( s && *s && count < len ) { + glyph = &fnt->glyphs[(int)*s]; + //int yadj = Assets.textFont.glyphs[text[i]].bottom + Assets.textFont.glyphs[text[i]].top; + //float yadj = scale * (Assets.textFont.glyphs[text[i]].imageHeight - Assets.textFont.glyphs[text[i]].height); + if ( Q_IsColorString( s ) ) { + memcpy( newColor, g_color_table[ColorIndex( *( s + 1 ) )], sizeof( newColor ) ); + newColor[3] = color[3]; + trap_R_SetColor( newColor ); + s += 2; + continue; + } else { + float yadj = useScale * glyph->top; + if ( style == ITEM_TEXTSTYLE_SHADOWED || style == ITEM_TEXTSTYLE_SHADOWEDMORE ) { + int ofs = style == ITEM_TEXTSTYLE_SHADOWED ? 1 : 2; + colorBlack[3] = newColor[3]; + trap_R_SetColor( colorBlack ); + CG_Text_PaintChar( x + ofs, y - yadj + ofs, + glyph->imageWidth, + glyph->imageHeight, + useScale, + glyph->s, + glyph->t, + glyph->s2, + glyph->t2, + glyph->glyph ); + colorBlack[3] = 1.0; + trap_R_SetColor( newColor ); + } + CG_Text_PaintChar( x, y - yadj, + glyph->imageWidth, + glyph->imageHeight, + useScale, + glyph->s, + glyph->t, + glyph->s2, + glyph->t2, + glyph->glyph ); + // CG_DrawPic(x, y - yadj, scale * cgDC.Assets.textFont.glyphs[text[i]].imageWidth, scale * cgDC.Assets.textFont.glyphs[text[i]].imageHeight, cgDC.Assets.textFont.glyphs[text[i]].glyph); + x += ( glyph->xSkip * useScale ) + adjust; + s++; + count++; + } + } + trap_R_SetColor( NULL ); + } +} + + + + + + + + + + + +#ifdef OLDWOLFUI +static void CG_DrawField( int x, int y, int width, int value ) { + char num[16], *ptr; + int l; + int frame; + + if ( width < 1 ) { + return; + } + + // draw number string + if ( width > 5 ) { + width = 5; + } + + switch ( width ) { + case 1: + value = value > 9 ? 9 : value; + value = value < 0 ? 0 : value; + break; + case 2: + value = value > 99 ? 99 : value; + value = value < -9 ? -9 : value; + break; + case 3: + value = value > 999 ? 999 : value; + value = value < -99 ? -99 : value; + break; + case 4: + value = value > 9999 ? 9999 : value; + value = value < -999 ? -999 : value; + break; + } + + Com_sprintf( num, sizeof( num ), "%i", value ); + l = strlen( num ); + if ( l > width ) { + l = width; + } + x += 2 + CHAR_WIDTH * ( width - l ); + + ptr = num; + while ( *ptr && l ) + { + if ( *ptr == '-' ) { + frame = STAT_MINUS; + } else { + frame = *ptr - '0'; + } + + CG_DrawPic( x,y, CHAR_WIDTH, CHAR_HEIGHT, cgs.media.numberShaders[frame] ); + x += CHAR_WIDTH; + ptr++; + l--; + } +} +#endif // #ifdef OLDWOLFUI + +/* +================ +CG_Draw3DModel + +================ +*/ +void CG_Draw3DModel( float x, float y, float w, float h, qhandle_t model, qhandle_t skin, vec3_t origin, vec3_t angles ) { + refdef_t refdef; + refEntity_t ent; + + if ( !cg_draw3dIcons.integer || !cg_drawIcons.integer ) { + return; + } + + CG_AdjustFrom640( &x, &y, &w, &h ); + + memset( &refdef, 0, sizeof( refdef ) ); + + memset( &ent, 0, sizeof( ent ) ); + AnglesToAxis( angles, ent.axis ); + VectorCopy( origin, ent.origin ); + ent.hModel = model; + ent.customSkin = skin; + ent.renderfx = RF_NOSHADOW; // no stencil shadows + + refdef.rdflags = RDF_NOWORLDMODEL; + + AxisClear( refdef.viewaxis ); + + refdef.fov_x = 30; + refdef.fov_y = 30; + + refdef.x = x; + refdef.y = y; + refdef.width = w; + refdef.height = h; + + refdef.time = cg.time; + + refdef.rdflags |= RDF_DRAWSKYBOX; + if ( !cg_skybox.integer ) { + refdef.rdflags &= ~RDF_DRAWSKYBOX; + } + + trap_R_ClearScene(); + trap_R_AddRefEntityToScene( &ent ); + trap_R_RenderScene( &refdef ); +} + +/* +================ +CG_DrawHead + +Used for both the status bar and the scoreboard +================ +*/ +void CG_DrawHead( float x, float y, float w, float h, int clientNum, vec3_t headAngles ) { + clipHandle_t cm; + clientInfo_t *ci; + float len; + vec3_t origin; + vec3_t mins, maxs; + + ci = &cgs.clientinfo[ clientNum ]; + + if ( cg_draw3dIcons.integer ) { + cm = ci->headModel; + if ( !cm ) { + return; + } + + // offset the origin y and z to center the head + trap_R_ModelBounds( cm, mins, maxs ); + + origin[2] = -0.5 * ( mins[2] + maxs[2] ); + origin[1] = 0.5 * ( mins[1] + maxs[1] ); + + // calculate distance so the head nearly fills the box + // assume heads are taller than wide + len = 0.7 * ( maxs[2] - mins[2] ); + origin[0] = len / 0.268; // len / tan( fov/2 ) + + // allow per-model tweaking + VectorAdd( origin, ci->modelInfo->headOffset, origin ); + + CG_Draw3DModel( x, y, w, h, ci->headModel, ci->headSkin, origin, headAngles ); +// } else if ( cg_drawIcons.integer ) { +// CG_DrawPic( x, y, w, h, ci->modelIcon ); + } + + // if they are deferred, draw a cross out + if ( ci->deferred ) { + CG_DrawPic( x, y, w, h, cgs.media.deferShader ); + } +} + +/* +================ +CG_DrawFlagModel + +Used for both the status bar and the scoreboard +================ +*/ +void CG_DrawFlagModel( float x, float y, float w, float h, int team ) { + qhandle_t cm; + float len; + vec3_t origin, angles; + vec3_t mins, maxs; + + VectorClear( angles ); + + cm = cgs.media.redFlagModel; + + // offset the origin y and z to center the flag + trap_R_ModelBounds( cm, mins, maxs ); + + origin[2] = -0.5 * ( mins[2] + maxs[2] ); + origin[1] = 0.5 * ( mins[1] + maxs[1] ); + + // calculate distance so the flag nearly fills the box + // assume heads are taller than wide + len = 0.5 * ( maxs[2] - mins[2] ); + origin[0] = len / 0.268; // len / tan( fov/2 ) + + angles[YAW] = 60 * sin( cg.time / 2000.0 );; + + CG_Draw3DModel( x, y, w, h, + team == TEAM_RED ? cgs.media.redFlagModel : cgs.media.blueFlagModel, + 0, origin, angles ); +} + + +/* +============== +CG_DrawKeyModel +============== +*/ +void CG_DrawKeyModel( int keynum, float x, float y, float w, float h, int fadetime ) { + qhandle_t cm; + float len; + vec3_t origin, angles; + vec3_t mins, maxs; + + VectorClear( angles ); + + cm = cg_items[keynum].models[0]; + + // offset the origin y and z to center the model + trap_R_ModelBounds( cm, mins, maxs ); + + origin[2] = -0.5 * ( mins[2] + maxs[2] ); + origin[1] = 0.5 * ( mins[1] + maxs[1] ); + +// len = 0.5 * ( maxs[2] - mins[2] ); + len = 0.75 * ( maxs[2] - mins[2] ); + origin[0] = len / 0.268; // len / tan( fov/2 ) + + angles[YAW] = 30 * sin( cg.time / 2000.0 );; + + CG_Draw3DModel( x, y, w, h, cg_items[keynum].models[0], 0, origin, angles ); +} + +/* +================ +CG_DrawStatusBarHead + +================ +*/ +#ifdef OLDWOLFUI +static void CG_DrawStatusBarHead( float x ) { + vec3_t angles; + float size, stretch; + float frac; + + VectorClear( angles ); + + if ( cg.damageTime && cg.time - cg.damageTime < DAMAGE_TIME ) { + frac = (float)( cg.time - cg.damageTime ) / DAMAGE_TIME; + size = ICON_SIZE * 1.25 * ( 1.5 - frac * 0.5 ); + + stretch = size - ICON_SIZE * 1.25; + // kick in the direction of damage + x -= stretch * 0.5 + cg.viewDamage[cg.damageIndex].damageX * stretch * 0.5; + + cg.headStartYaw = 180 + cg.viewDamage[cg.damageIndex].damageX * 45; + + cg.headEndYaw = 180 + 20 * cos( crandom() * M_PI ); + cg.headEndPitch = 5 * cos( crandom() * M_PI ); + + cg.headStartTime = cg.time; + cg.headEndTime = cg.time + 100 + random() * 2000; + } else { + if ( cg.time >= cg.headEndTime ) { + // select a new head angle + cg.headStartYaw = cg.headEndYaw; + cg.headStartPitch = cg.headEndPitch; + cg.headStartTime = cg.headEndTime; + cg.headEndTime = cg.time + 100 + random() * 2000; + + cg.headEndYaw = 180 + 20 * cos( crandom() * M_PI ); + cg.headEndPitch = 5 * cos( crandom() * M_PI ); + } + + size = ICON_SIZE * 1.25; + } + + // if the server was frozen for a while we may have a bad head start time + if ( cg.headStartTime > cg.time ) { + cg.headStartTime = cg.time; + } + + frac = ( cg.time - cg.headStartTime ) / (float)( cg.headEndTime - cg.headStartTime ); + frac = frac * frac * ( 3 - 2 * frac ); + angles[YAW] = cg.headStartYaw + ( cg.headEndYaw - cg.headStartYaw ) * frac; + angles[PITCH] = cg.headStartPitch + ( cg.headEndPitch - cg.headStartPitch ) * frac; + + CG_DrawHead( x, 480 - size, size, size, + cg.snap->ps.clientNum, angles ); +} +#endif // #ifdef OLDWOLFUI + +/* +============== +CG_DrawStatusBarKeys +IT_KEY (this makes this routine easier to find in files...) (SA) +============== +*/ +#ifdef OLDWOLFUI +static void CG_DrawStatusBarKeys() { + int i; + float y = 0; // start height is + gitem_t *gi; + int itemnum; +// int fadetime = 0; + float *fadeColor; + + +//----(SA) added + if ( cg.showItems ) { + fadeColor = colorWhite; + } else { + fadeColor = CG_FadeColor( cg.itemFadeTime, 1000 ); + } + + if ( !fadeColor ) { + return; + } + + + // (SA) just don't draw this stuff for now. It's got fog issues I need to clean up + + return; + + + + for ( i = 1; i < KEY_NUM_KEYS; i++ ) + { + gi = BG_FindItemForKey( i, &itemnum ); + // if i've got the key... + + if ( cg.snap->ps.stats[STAT_KEYS] & ( 1 << gi->giTag ) ) { + y += ICON_SIZE + 5; + CG_DrawKeyModel( itemnum, 640 - ( 1.5 * ICON_SIZE ), y, ICON_SIZE, ICON_SIZE, cg.time + fadeColor[0] * 1000 ); + } + } +} +#endif // #ifdef OLDWOLFUI + +/* +================ +CG_DrawStatusBarFlag + +================ +*/ +#ifdef OLDWOLFUI +static void CG_DrawStatusBarFlag( float x, int team ) { + CG_DrawFlagModel( x, 480 - ICON_SIZE, ICON_SIZE, ICON_SIZE, team ); +} +#endif // #ifdef OLDWOFLUI + +/* +================ +CG_DrawTeamBackground + +================ +*/ +void CG_DrawTeamBackground( int x, int y, int w, int h, float alpha, int team ) { + vec4_t hcolor; + + hcolor[3] = alpha; + if ( team == TEAM_RED ) { + hcolor[0] = 1; + hcolor[1] = 0; + hcolor[2] = 0; + } else if ( team == TEAM_BLUE ) { + hcolor[0] = 0; + hcolor[1] = 0; + hcolor[2] = 1; + } else { + return; + } + trap_R_SetColor( hcolor ); + CG_DrawPic( x, y, w, h, cgs.media.teamStatusBar ); + trap_R_SetColor( NULL ); +} + +////////////////////// +////// end new hud stuff +////////////////////// + + + + + +// JOSEPH 4-25-00 +/* +================ +CG_DrawStatusBar + +================ +*/ +#ifdef OLDWOLFUI +static void CG_DrawStatusBar( void ) { + int color; + centity_t *cent; + playerState_t *ps; + int value, inclip; + vec4_t hcolor; + vec3_t angles; +// vec3_t origin; + static float colors[4][4] = { +// { 0.2, 1.0, 0.2, 1.0 } , { 1.0, 0.2, 0.2, 1.0 }, {0.5, 0.5, 0.5, 1} }; + { 1, 0.69, 0, 1.0 }, // normal + { 1.0, 0.2, 0.2, 1.0 }, // low health + {0.5, 0.5, 0.5, 1}, // weapon firing + { 1, 1, 1, 1 } + }; // health > 100 + + if ( cg_drawStatus.integer == 0 ) { + return; + } + + // draw the team background + if ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_RED ) { + hcolor[0] = 1; + hcolor[1] = 0; + hcolor[2] = 0; + hcolor[3] = 0.33; + trap_R_SetColor( hcolor ); + CG_DrawPic( 0, 420, 640, 60, cgs.media.teamStatusBar ); + trap_R_SetColor( NULL ); + } else if ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_BLUE ) { + hcolor[0] = 0; + hcolor[1] = 0; + hcolor[2] = 1; + hcolor[3] = 0.33; + trap_R_SetColor( hcolor ); + CG_DrawPic( 0, 420, 640, 60, cgs.media.teamStatusBar ); + trap_R_SetColor( NULL ); + } + + cent = &cg_entities[cg.snap->ps.clientNum]; + ps = &cg.snap->ps; + + VectorClear( angles ); + + // draw any 3D icons first, so the changes back to 2D are minimized + + //----(SA) further change... we don't need to draw the ammo 3d model do we? +/* + if ( cent->currentState.weapon && cg_weapons[ cent->currentState.weapon ].ammoModel ) { + origin[0] = 70; + origin[1] = 0; + origin[2] = 0; + angles[YAW] = 90 + 20 * sin( cg.time / 1000.0 );; +//----(SA) Wolf statusbar change + CG_Draw3DModel( CHAR_WIDTH*3 + TEXT_ICON_SPACE, STATUSBARHEIGHT -20, ICON_SIZE, ICON_SIZE, + cg_weapons[ cent->currentState.weapon ].ammoModel, 0, origin, angles ); +//----(SA) end + } +*/ + //CG_DrawStatusBarHead( 185 + CHAR_WIDTH*3 + TEXT_ICON_SPACE ); + + CG_DrawStatusBarKeys(); + + if ( cg.predictedPlayerState.powerups[PW_REDFLAG] ) { + CG_DrawStatusBarFlag( 185 + CHAR_WIDTH * 3 + TEXT_ICON_SPACE + ICON_SIZE, TEAM_RED ); + } else if ( cg.predictedPlayerState.powerups[PW_BLUEFLAG] ) { + CG_DrawStatusBarFlag( 185 + CHAR_WIDTH * 3 + TEXT_ICON_SPACE + ICON_SIZE, TEAM_BLUE ); + } + + //----(SA) further change... we don't need to draw the armor do we? +/* + if ( ps->stats[ STAT_ARMOR ] ) { + origin[0] = 90; + origin[1] = 0; + origin[2] = -10; + angles[YAW] = ( cg.time & 2047 ) * 360 / 2048.0; +//----(SA) Wolf statusbar change +// CG_Draw3DModel( 370 + CHAR_WIDTH*3 + TEXT_ICON_SPACE, STATUSBARHEIGHT -20, ICON_SIZE, ICON_SIZE, +// cgs.media.armorModel, 0, origin, angles ); +//----(SA) end + } +*/ + // + // ammo + // + if ( cent->currentState.weapon ) { + qhandle_t icon; + float scale,halfScale; + float wideOffset; + + value = ps->ammo[BG_FindAmmoForWeapon( cent->currentState.weapon )]; + inclip = ps->ammoclip[BG_FindClipForWeapon( cent->currentState.weapon )]; + + if ( value > -1 ) { + if ( ( cg.predictedPlayerState.weaponstate == WEAPON_FIRING || cg.predictedPlayerState.weaponstate == WEAPON_FIRINGALT ) + && cg.predictedPlayerState.weaponTime > 100 ) { + // draw as dark grey when reloading + color = 2; // dark grey + } else { + if ( value >= 0 ) { + color = 0; // green + } else { + color = 1; // red + } + } + trap_R_SetColor( colors[color] ); + + // pulsing grenade icon to help the player 'count' in their head + if ( ps->grenadeTimeLeft ) { + if ( ps->weapon == WP_DYNAMITE ) { + + } else { + if ( ( ( cg.grenLastTime ) % 1000 ) < ( ( ps->grenadeTimeLeft ) % 1000 ) ) { + switch ( ps->grenadeTimeLeft / 1000 ) { + case 3: + trap_S_StartLocalSound( cgs.media.grenadePulseSound4, CHAN_LOCAL_SOUND ); + break; + case 2: + trap_S_StartLocalSound( cgs.media.grenadePulseSound3, CHAN_LOCAL_SOUND ); + break; + case 1: + trap_S_StartLocalSound( cgs.media.grenadePulseSound2, CHAN_LOCAL_SOUND ); + break; + case 0: + trap_S_StartLocalSound( cgs.media.grenadePulseSound1, CHAN_LOCAL_SOUND ); + break; + } + } + } + + scale = (float)( ( ps->grenadeTimeLeft ) % 1000 ) / 100.0f; + halfScale = scale * 0.5f; + + cg.grenLastTime = ps->grenadeTimeLeft; + } else { + scale = halfScale = 0; + } + + + switch ( cg.predictedPlayerState.weapon ) { + case WP_THOMPSON: + case WP_MP40: + case WP_STEN: + case WP_MAUSER: + case WP_GARAND: + case WP_VENOM: + case WP_TESLA: + case WP_PANZERFAUST: + case WP_FLAMETHROWER: + wideOffset = -38; + break; + default: + wideOffset = 0; + break; + } + + // don't draw ammo value for knife + if ( cg.predictedPlayerState.weapon != WP_KNIFE ) { + if ( cgs.dmflags & DF_NO_WEAPRELOAD ) { + CG_DrawBigString2( ( 580 - 23 + 35 ) + wideOffset, STATUSBARHEIGHT, va( "%d.", value ), cg_hudAlpha.value ); + } else if ( value ) { + CG_DrawBigString2( ( 580 - 23 + 35 ) + wideOffset, STATUSBARHEIGHT, va( "%d/%d", inclip, value ), cg_hudAlpha.value ); + } else { + CG_DrawBigString2( ( 580 - 23 + 35 ) + wideOffset, STATUSBARHEIGHT, va( "%d", inclip ), cg_hudAlpha.value ); + } + } + + icon = cg_weapons[ cg.predictedPlayerState.weapon ].weaponIcon[0]; + if ( icon ) { + CG_DrawPic( ( ( 530 + 68 ) - halfScale ) + wideOffset, ( 446 - 10 ) - halfScale, ( 38 + scale ) - wideOffset, 38 + scale, icon ); + } + + trap_R_SetColor( NULL ); + + // if we didn't draw a 3D icon, draw a 2D icon for ammo + if ( !cg_draw3dIcons.integer ) { + qhandle_t icon; + + icon = cg_weapons[ cg.predictedPlayerState.weapon ].ammoIcon; + if ( icon ) { + CG_DrawPic( CHAR_WIDTH * 3 + TEXT_ICON_SPACE, STATUSBARHEIGHT, ICON_SIZE, ICON_SIZE, icon ); + } + } + } + } + + // + // health + // + value = ps->stats[STAT_HEALTH]; + if ( value > 100 ) { + trap_R_SetColor( colors[3] ); // white + } else if ( value > 25 ) { + trap_R_SetColor( colors[0] ); // green + } else if ( value > 0 ) { + color = ( cg.time >> 8 ) & 1; // flash + trap_R_SetColor( colors[color] ); + } else { + trap_R_SetColor( colors[1] ); // red + } + + // stretch the health up when taking damage +//----(SA) Wolf statusbar change +// CG_DrawField ( 185, STATUSBARHEIGHT, 3, value); + { + char printme[16]; + sprintf( printme, "%d", value ); + //CG_DrawBigString( 185, STATUSBARHEIGHT, printme, cg_hudAlpha.value ); + CG_DrawBigString2( 16 + 23 + 43, STATUSBARHEIGHT, printme, cg_hudAlpha.value ); + } +//----(SA) end + CG_ColorForHealth( hcolor ); + trap_R_SetColor( hcolor ); + + + // + // armor + // + value = ps->stats[STAT_ARMOR]; + if ( value > 0 ) { + trap_R_SetColor( colors[0] ); +//----(SA) Wolf statusbar change +// CG_DrawField (370, STATUSBARHEIGHT, 3, value); + { + char printme[16]; + sprintf( printme, "%d", value ); + //CG_DrawBigString( 370, STATUSBARHEIGHT, printme, cg_hudAlpha.value ); + CG_DrawBigString2( 200, STATUSBARHEIGHT, printme, cg_hudAlpha.value ); + } +//----(SA) end + trap_R_SetColor( NULL ); +//----(SA) Wolf statusbar change +// CG_DrawPic( 370 + CHAR_WIDTH*3 + TEXT_ICON_SPACE, STATUSBARHEIGHT, ICON_SIZE, ICON_SIZE, cgs.media.armorIcon ); +//----(SA) end + } +} +// END JOSEPH +#endif // #ifdef OLDWOLFUI +/* +=========================================================================================== + + UPPER RIGHT CORNER + +=========================================================================================== +*/ + +/* +================ +CG_DrawAttacker + +================ +*/ +#ifdef OLDWOLFUI +static float CG_DrawAttacker( float y ) { + int t; + float size; + vec3_t angles; + const char *info; + const char *name; + int clientNum; + + if ( cg.predictedPlayerState.stats[STAT_HEALTH] <= 0 ) { + return y; + } + + if ( !cg.attackerTime ) { + return y; + } + + clientNum = cg.predictedPlayerState.persistant[PERS_ATTACKER]; + if ( clientNum < 0 || clientNum >= MAX_CLIENTS || clientNum == cg.snap->ps.clientNum ) { + return y; + } + + t = cg.time - cg.attackerTime; + if ( t > ATTACKER_HEAD_TIME ) { + cg.attackerTime = 0; + return y; + } + + size = ICON_SIZE * 1.25; + + angles[PITCH] = 0; + angles[YAW] = 180; + angles[ROLL] = 0; + CG_DrawHead( 640 - size, y, size, size, clientNum, angles ); + + info = CG_ConfigString( CS_PLAYERS + clientNum ); + name = Info_ValueForKey( info, "n" ); + y += size; + CG_DrawBigString( 640 - ( Q_PrintStrlen( name ) * BIGCHAR_WIDTH ), y, name, 0.5 ); + + return y + BIGCHAR_HEIGHT + 2; +} +#endif // #ifdef OLDWOLFUI + +#define UPPERRIGHT_X 500 +/* +================== +CG_DrawSnapshot +================== +*/ +static float CG_DrawSnapshot( float y ) { + char *s; + int w; + + s = va( "time:%i snap:%i cmd:%i", cg.snap->serverTime, + cg.latestSnapshotNum, cgs.serverCommandSequence ); + w = CG_DrawStrlen( s ) * BIGCHAR_WIDTH; + + CG_DrawBigString( UPPERRIGHT_X - w, y + 2, s, 1.0F ); + + return y + BIGCHAR_HEIGHT + 4; +} + +/* +================== +CG_DrawFPS +================== +*/ +#define FPS_FRAMES 4 +static float CG_DrawFPS( float y ) { + char *s; + int w; + static int previousTimes[FPS_FRAMES]; + static int index; + int i, total; + int fps; + static int previous; + int t, frameTime; + + // don't use serverTime, because that will be drifting to + // correct for internet lag changes, timescales, timedemos, etc + t = trap_Milliseconds(); + frameTime = t - previous; + previous = t; + + previousTimes[index % FPS_FRAMES] = frameTime; + index++; + if ( index > FPS_FRAMES ) { + // average multiple frames together to smooth changes out a bit + total = 0; + for ( i = 0 ; i < FPS_FRAMES ; i++ ) { + total += previousTimes[i]; + } + if ( !total ) { + total = 1; + } + fps = 1000 * FPS_FRAMES / total; + + s = va( "%ifps", fps ); + w = CG_DrawStrlen( s ) * BIGCHAR_WIDTH; + + CG_DrawBigString( UPPERRIGHT_X - w, y + 2, s, 1.0F ); + } + + return y + BIGCHAR_HEIGHT + 4; +} + +/* +================= +CG_DrawTimer +================= +*/ +static float CG_DrawTimer( float y ) { + char *s; + int w; + int mins, seconds, tens; + int msec; + + // NERVE - SMF - draw time remaining in multiplayer + if ( cgs.gametype == GT_WOLF ) { + msec = ( cgs.timelimit * 60.f * 1000.f ) - ( cg.time - cgs.levelStartTime ); + } else { + msec = cg.time - cgs.levelStartTime; + } + // -NERVE - SMF + + seconds = msec / 1000; + mins = seconds / 60; + seconds -= mins * 60; + tens = seconds / 10; + seconds -= tens * 10; + + s = va( "%i:%i%i", mins, tens, seconds ); + w = CG_DrawStrlen( s ) * BIGCHAR_WIDTH; + + CG_DrawBigString( UPPERRIGHT_X - w, y + 2, s, 1.0F ); + + return y + BIGCHAR_HEIGHT + 4; +} + + +/* +================= +CG_DrawTeamOverlay +================= +*/ + +// set in CG_ParseTeamInfo +int sortedTeamPlayers[TEAM_MAXOVERLAY]; +int numSortedTeamPlayers; + +#define TEAM_OVERLAY_MAXNAME_WIDTH 16 +#define TEAM_OVERLAY_MAXLOCATION_WIDTH 20 + +static float CG_DrawTeamOverlay( float y ) { + int x, w, h, xx; + int i, j, len; + const char *p; + vec4_t hcolor; + int pwidth, lwidth; + int plyrs; + char st[16]; + clientInfo_t *ci; + + if ( !cg_drawTeamOverlay.integer ) { + return y; + } + + if ( cg.snap->ps.persistant[PERS_TEAM] != TEAM_RED && + cg.snap->ps.persistant[PERS_TEAM] != TEAM_BLUE ) { + return y; // Not on any team + + } + plyrs = 0; + + // max player name width + pwidth = 0; + for ( i = 0; i < numSortedTeamPlayers; i++ ) { + ci = cgs.clientinfo + sortedTeamPlayers[i]; + if ( ci->infoValid && ci->team == cg.snap->ps.persistant[PERS_TEAM] ) { + plyrs++; + len = CG_DrawStrlen( cgs.clientinfo[i].name ); + if ( len > pwidth ) { + pwidth = len; + } + } + } + + if ( !plyrs ) { + return y; + } + + if ( pwidth > TEAM_OVERLAY_MAXNAME_WIDTH ) { + pwidth = TEAM_OVERLAY_MAXNAME_WIDTH; + } + +#if 0 + // max location name width + lwidth = 0; + for ( i = 0; i < numSortedTeamPlayers; i++ ) { + ci = cgs.clientinfo + sortedTeamPlayers[i]; + if ( ci->infoValid && + ci->team == cg.snap->ps.persistant[PERS_TEAM] && + CG_ConfigString( CS_LOCATIONS + ci->location ) ) { + len = CG_DrawStrlen( CG_ConfigString( CS_LOCATIONS + ci->location ) ); + if ( len > lwidth ) { + lwidth = len; + } + } + } +#else + // max location name width + lwidth = 0; + for ( i = 1; i < MAX_LOCATIONS; i++ ) { + p = CG_ConfigString( CS_LOCATIONS + i ); + if ( p && *p ) { + len = CG_DrawStrlen( p ); + if ( len > lwidth ) { + lwidth = len; + } + } + } +#endif + + if ( lwidth > TEAM_OVERLAY_MAXLOCATION_WIDTH ) { + lwidth = TEAM_OVERLAY_MAXLOCATION_WIDTH; + } + + w = ( pwidth + lwidth + 4 + 7 ) * TINYCHAR_WIDTH; + x = 640 - w - 32; + h = plyrs * TINYCHAR_HEIGHT; + + if ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_RED ) { + hcolor[0] = 1; + hcolor[1] = 0; + hcolor[2] = 0; + hcolor[3] = 0.33; + } else { // if ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_BLUE ) + hcolor[0] = 0; + hcolor[1] = 0; + hcolor[2] = 1; + hcolor[3] = 0.33; + } + trap_R_SetColor( hcolor ); + CG_DrawPic( x, y, w, h, cgs.media.teamStatusBar ); + trap_R_SetColor( NULL ); + + + for ( i = 0; i < numSortedTeamPlayers; i++ ) { + ci = cgs.clientinfo + sortedTeamPlayers[i]; + if ( ci->infoValid && ci->team == cg.snap->ps.persistant[PERS_TEAM] ) { + + hcolor[0] = hcolor[1] = hcolor[2] = hcolor[3] = 1.0; + + xx = x + TINYCHAR_WIDTH; + + CG_DrawStringExt( xx, y, + ci->name, hcolor, qfalse, qfalse, + TINYCHAR_WIDTH, TINYCHAR_HEIGHT, TEAM_OVERLAY_MAXNAME_WIDTH ); + + if ( lwidth ) { + p = CG_ConfigString( CS_LOCATIONS + ci->location ); + if ( !p || !*p ) { + p = "unknown"; + } + len = CG_DrawStrlen( p ); + if ( len > lwidth ) { + len = lwidth; + } + + xx = x + TINYCHAR_WIDTH * 2 + TINYCHAR_WIDTH * pwidth + + ( ( lwidth / 2 - len / 2 ) * TINYCHAR_WIDTH ); + CG_DrawStringExt( xx, y, + p, hcolor, qfalse, qfalse, TINYCHAR_WIDTH, TINYCHAR_HEIGHT, + TEAM_OVERLAY_MAXLOCATION_WIDTH ); + } + + CG_ColorForHealth( hcolor ); + + Com_sprintf( st, sizeof( st ), "%3i %3i", ci->health, ci->armor ); + + xx = x + TINYCHAR_WIDTH * 3 + + TINYCHAR_WIDTH * pwidth + TINYCHAR_WIDTH * lwidth; + + CG_DrawStringExt( xx, y, + st, hcolor, qfalse, qfalse, + TINYCHAR_WIDTH, TINYCHAR_HEIGHT, 0 ); + + // draw weapon icon + xx += TINYCHAR_WIDTH * 3; + + CG_DrawPic( xx, y, TINYCHAR_WIDTH, TINYCHAR_HEIGHT, + cg_weapons[ci->curWeapon].weaponIcon[0] ); + + // Draw powerup icons + xx = x; + for ( j = 0; j < PW_NUM_POWERUPS; j++ ) { + if ( ci->powerups & ( 1 << j ) ) { + gitem_t *item; + + item = BG_FindItemForPowerup( j ); + + if ( item != NULL ) { // JPW NERVE added for invulnerability powerup at beginning of map + CG_DrawPic( xx, y, TINYCHAR_WIDTH, TINYCHAR_HEIGHT, + trap_R_RegisterShader( item->icon ) ); + xx -= TINYCHAR_WIDTH; + } // jpw + } + } + + y += TINYCHAR_HEIGHT; + } + } + + return y; +} + + +/* +===================== +CG_DrawUpperRight + +===================== +*/ +static void CG_DrawUpperRight( void ) { + float y; + + y = 0; + + if ( cgs.gametype >= GT_TEAM ) { + y = CG_DrawTeamOverlay( y ); + } + if ( cg_drawSnapshot.integer ) { + y = CG_DrawSnapshot( y ); + } + if ( cg_drawFPS.integer ) { + y = CG_DrawFPS( y ); + } + if ( cg_drawTimer.integer ) { + y = CG_DrawTimer( y ); + } +// (SA) disabling drawattacker for the time being +// if ( cg_drawAttacker.integer ) { +// y = CG_DrawAttacker( y ); +// } +//----(SA) end +} + +/* +=========================================================================================== + + LOWER RIGHT CORNER + +=========================================================================================== +*/ + +/* +================= +CG_DrawScores + +Draw the small two score display +================= +*/ +#ifdef OLDWOLFUI +static float CG_DrawScores( float y ) { + const char *s; + int s1, s2, score; + int x, w; + int v; + vec4_t color; + + s = CG_ConfigString( CS_SCORES1 ); + s1 = cgs.scores1; + s = CG_ConfigString( CS_SCORES2 ); + s2 = cgs.scores2; + + y -= BIGCHAR_HEIGHT + 8; + + // draw from the right side to left + if ( cgs.gametype >= GT_TEAM ) { + x = 640; + + color[0] = 0; + color[1] = 0; + color[2] = 1; + color[3] = 0.33; + s = va( "%2i", s2 ); + w = CG_DrawStrlen( s ) * BIGCHAR_WIDTH + 8; + x -= w; + CG_FillRect( x, y - 4, w, BIGCHAR_HEIGHT + 8, color ); + if ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_BLUE ) { + CG_DrawPic( x, y - 4, w, BIGCHAR_HEIGHT + 8, cgs.media.selectShader ); + } + CG_DrawBigString( x + 4, y, s, 1.0F ); + + + color[0] = 1; + color[1] = 0; + color[2] = 0; + color[3] = 0.33; + s = va( "%2i", s1 ); + w = CG_DrawStrlen( s ) * BIGCHAR_WIDTH + 8; + x -= w; + CG_FillRect( x, y - 4, w, BIGCHAR_HEIGHT + 8, color ); + if ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_RED ) { + CG_DrawPic( x, y - 4, w, BIGCHAR_HEIGHT + 8, cgs.media.selectShader ); + } + CG_DrawBigString( x + 4, y, s, 1.0F ); + + if ( cgs.gametype == GT_CTF ) { + v = cgs.capturelimit; + } else { + v = cgs.fraglimit; + } + if ( v ) { + s = va( "%2i", v ); + w = CG_DrawStrlen( s ) * BIGCHAR_WIDTH + 8; + x -= w; + CG_DrawBigString( x + 4, y, s, 1.0F ); + } + +//----(SA) don't show frag count/limit in sp + } else if ( cgs.gametype != GT_SINGLE_PLAYER && cg_drawFrags.integer ) { +//----(SA) end + qboolean spectator; + + x = 640; + score = cg.snap->ps.persistant[PERS_SCORE]; + spectator = ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_SPECTATOR ); + + // always show your score in the second box if not in first place + if ( s1 != score ) { + s2 = score; + } + if ( s2 != SCORE_NOT_PRESENT ) { + s = va( "%2i", s2 ); + w = CG_DrawStrlen( s ) * BIGCHAR_WIDTH + 8; + x -= w; + if ( !spectator && score == s2 && score != s1 ) { + color[0] = 1; + color[1] = 0; + color[2] = 0; + color[3] = 0.33; + CG_FillRect( x, y - 4, w, BIGCHAR_HEIGHT + 8, color ); + CG_DrawPic( x, y - 4, w, BIGCHAR_HEIGHT + 8, cgs.media.selectShader ); + } else { + color[0] = 0.5; + color[1] = 0.5; + color[2] = 0.5; + color[3] = 0.33; + CG_FillRect( x, y - 4, w, BIGCHAR_HEIGHT + 8, color ); + } + CG_DrawBigString( x + 4, y, s, 1.0F ); + } + + // first place + if ( s1 != SCORE_NOT_PRESENT ) { + s = va( "%2i", s1 ); + w = CG_DrawStrlen( s ) * BIGCHAR_WIDTH + 8; + x -= w; + if ( !spectator && score == s1 ) { + color[0] = 0; + color[1] = 0; + color[2] = 1; + color[3] = 0.33; + CG_FillRect( x, y - 4, w, BIGCHAR_HEIGHT + 8, color ); + CG_DrawPic( x, y - 4, w, BIGCHAR_HEIGHT + 8, cgs.media.selectShader ); + } else { + color[0] = 0.5; + color[1] = 0.5; + color[2] = 0.5; + color[3] = 0.33; + CG_FillRect( x, y - 4, w, BIGCHAR_HEIGHT + 8, color ); + } + CG_DrawBigString( x + 4, y, s, 1.0F ); + } + + if ( cgs.fraglimit ) { + s = va( "%2i", cgs.fraglimit ); + w = CG_DrawStrlen( s ) * BIGCHAR_WIDTH + 8; + x -= w; + CG_DrawBigString( x + 4, y, s, 1.0F ); + } + + } + + return y - 8; +} +#endif // #ifdef OLDWOLFUI + +/* +================ +CG_DrawPowerups +================ +*/ +#ifdef OLDWOLFUI +static float CG_DrawPowerups( float y ) { + int sorted[MAX_POWERUPS]; + int sortedTime[MAX_POWERUPS]; + int i, j, k; + int active; + playerState_t *ps; + int t; + gitem_t *item; + int x; + int color; + float size; + float f; + static float colors[2][4] = { + { 0.2, 1.0, 0.2, 1.0 }, { 1.0, 0.2, 0.2, 1.0 } + }; + + ps = &cg.snap->ps; + + if ( ps->stats[STAT_HEALTH] <= 0 ) { + return y; + } + + // sort the list by time remaining + active = 0; + for ( i = 0 ; i < MAX_POWERUPS ; i++ ) { + if ( !ps->powerups[ i ] ) { + continue; + } + t = ps->powerups[ i ] - cg.time; + // ZOID--don't draw if the power up has unlimited time (999 seconds) + // This is true of the CTF flags + if ( t < 0 || t > 999000 ) { + continue; + } + + // insert into the list + for ( j = 0 ; j < active ; j++ ) { + if ( sortedTime[j] >= t ) { + for ( k = active - 1 ; k >= j ; k-- ) { + sorted[k + 1] = sorted[k]; + sortedTime[k + 1] = sortedTime[k]; + } + break; + } + } + sorted[j] = i; + sortedTime[j] = t; + active++; + } + + // draw the icons and timers + x = 640 - ICON_SIZE - CHAR_WIDTH * 2; + for ( i = 0 ; i < active ; i++ ) { + + continue; // (SA) FIXME: TEMP: as I'm getting powerup business going + + item = BG_FindItemForPowerup( sorted[i] ); + + color = 1; + + y -= ICON_SIZE; + + trap_R_SetColor( colors[color] ); + CG_DrawField( x, y, 2, sortedTime[ i ] / 1000 ); + + t = ps->powerups[ sorted[i] ]; + if ( t - cg.time >= POWERUP_BLINKS * POWERUP_BLINK_TIME ) { + trap_R_SetColor( NULL ); + } else { + vec4_t modulate; + + f = (float)( t - cg.time ) / POWERUP_BLINK_TIME; + f -= (int)f; + modulate[0] = modulate[1] = modulate[2] = modulate[3] = f; + trap_R_SetColor( modulate ); + } + + if ( cg.powerupActive == sorted[i] && + cg.time - cg.powerupTime < PULSE_TIME ) { + f = 1.0 - ( ( (float)cg.time - cg.powerupTime ) / PULSE_TIME ); + size = ICON_SIZE * ( 1.0 + ( PULSE_SCALE - 1.0 ) * f ); + } else { + size = ICON_SIZE; + } + + CG_DrawPic( 640 - size, y + ICON_SIZE / 2 - size / 2, + size, size, trap_R_RegisterShader( item->icon ) ); + } + trap_R_SetColor( NULL ); + + return y; +} +#endif // #ifdef OLDWOLFUI + + +/* +===================== +CG_DrawLowerRight + +===================== +*/ +#ifdef OLDWOLFUI +static void CG_DrawLowerRight( void ) { + float y; + + y = 480 - ICON_SIZE; + + y = CG_DrawScores( y ); + y = CG_DrawPowerups( y ); +} +#endif // #ifdef OLDWOLFUI + +//=========================================================================================== + +/* +================= +CG_DrawTeamInfo +================= +*/ +static void CG_DrawTeamInfo( void ) { + int w, h; + int i, len; + vec4_t hcolor; + int chatHeight; + +#define CHATLOC_Y 420 // bottom end +#define CHATLOC_X 0 + + if ( cg_teamChatHeight.integer < TEAMCHAT_HEIGHT ) { + chatHeight = cg_teamChatHeight.integer; + } else { + chatHeight = TEAMCHAT_HEIGHT; + } + if ( chatHeight <= 0 ) { + return; // disabled + + } + if ( cgs.teamLastChatPos != cgs.teamChatPos ) { + if ( cg.time - cgs.teamChatMsgTimes[cgs.teamLastChatPos % chatHeight] > cg_teamChatTime.integer ) { + cgs.teamLastChatPos++; + } + + h = ( cgs.teamChatPos - cgs.teamLastChatPos ) * TINYCHAR_HEIGHT; + + w = 0; + + for ( i = cgs.teamLastChatPos; i < cgs.teamChatPos; i++ ) { + len = CG_DrawStrlen( cgs.teamChatMsgs[i % chatHeight] ); + if ( len > w ) { + w = len; + } + } + w *= TINYCHAR_WIDTH; + w += TINYCHAR_WIDTH * 2; + + if ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_RED ) { + hcolor[0] = 1; + hcolor[1] = 0; + hcolor[2] = 0; + hcolor[3] = 0.33; + } else if ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_BLUE ) { + hcolor[0] = 0; + hcolor[1] = 0; + hcolor[2] = 1; + hcolor[3] = 0.33; + } else { + hcolor[0] = 0; + hcolor[1] = 1; + hcolor[2] = 0; + hcolor[3] = 0.33; + } + + trap_R_SetColor( hcolor ); + CG_DrawPic( CHATLOC_X, CHATLOC_Y - h, 640, h, cgs.media.teamStatusBar ); + trap_R_SetColor( NULL ); + + hcolor[0] = hcolor[1] = hcolor[2] = 1.0; + hcolor[3] = 1.0; + + for ( i = cgs.teamChatPos - 1; i >= cgs.teamLastChatPos; i-- ) { + CG_DrawStringExt( CHATLOC_X + TINYCHAR_WIDTH, + CHATLOC_Y - ( cgs.teamChatPos - i ) * TINYCHAR_HEIGHT, + cgs.teamChatMsgs[i % chatHeight], hcolor, qfalse, qfalse, + TINYCHAR_WIDTH, TINYCHAR_HEIGHT, 0 ); +// CG_DrawSmallString( CHATLOC_X + SMALLCHAR_WIDTH, +// CHATLOC_Y - (cgs.teamChatPos - i)*SMALLCHAR_HEIGHT, +// cgs.teamChatMsgs[i % TEAMCHAT_HEIGHT], 1.0F ); + } + } +} + +//----(SA) modified +/* +=================== +CG_DrawPickupItem +=================== +*/ +static void CG_DrawPickupItem( void ) { + int value; + float *fadeColor; + char pickupText[256]; + float color[4]; + + value = cg.itemPickup; + if ( value ) { + fadeColor = CG_FadeColor( cg.itemPickupTime, 3000 ); + if ( fadeColor ) { + CG_RegisterItemVisuals( value ); + +//----(SA) commented out +// trap_R_SetColor( fadeColor ); +// CG_DrawPic( 8, 380, ICON_SIZE, ICON_SIZE, cg_items[ value ].icon ); +//----(SA) end + + //----(SA) so we don't pick up all sorts of items and have it print "0 " + if ( bg_itemlist[ value ].giType == IT_AMMO || bg_itemlist[ value ].giType == IT_HEALTH || bg_itemlist[value].giType == IT_POWERUP ) { + if ( bg_itemlist[ value ].world_model[2] ) { // this is a multi-stage item + // FIXME: print the correct amount for multi-stage + Com_sprintf( pickupText, sizeof( pickupText ), "%s", cgs.itemPrintNames[ value ] ); + } else { + if ( bg_itemlist[ value ].gameskillnumber[cg_gameSkill.integer] > 1 ) { + Com_sprintf( pickupText, sizeof( pickupText ), "%i %s", bg_itemlist[ value ].gameskillnumber[cg_gameSkill.integer], cgs.itemPrintNames[ value ] ); + } else { + Com_sprintf( pickupText, sizeof( pickupText ), "%s", cgs.itemPrintNames[value] ); + } + } + } else { + Com_sprintf( pickupText, sizeof( pickupText ), "%s", cgs.itemPrintNames[value] ); + } + + //----(SA) trying smaller text + color[0] = color[1] = color[2] = 1.0; + color[3] = fadeColor[0]; + CG_DrawStringExt2( ICON_SIZE + 16, 398, pickupText, color, qfalse, qtrue, 10, 10, 0 ); +// CG_Text_Paint(ICON_SIZE + 16, 398, 2, 0.3f, color, pickupText, 0, 0, ITEM_TEXTSTYLE_SHADOWEDMORE); + + + trap_R_SetColor( NULL ); + } + } +} +//----(SA) end + + +/* +=================== +CG_DrawHoldableItem +=================== +*/ +void CG_DrawHoldableItem_old( void ) { + int value; + gitem_t *item; + + if ( !cg.holdableSelect ) { + return; + } + + item = BG_FindItemForHoldable( cg.holdableSelect ); + + if ( !item ) { + return; + } + + value = cg.predictedPlayerState.holdable[cg.holdableSelect]; + + if ( value ) { + + trap_R_SetColor( NULL ); + + CG_RegisterItemVisuals( item - bg_itemlist ); + + if ( cg.holdableSelect == HI_WINE ) { + if ( value > 3 ) { + value = 3; // 3 stages to icon, just draw full if beyond 'full' + + } + //----(SA) trying smaller text + //----(SA) and off to the right side of the HUD +// CG_DrawPic( 100, (SCREEN_HEIGHT-ICON_SIZE)-8, ICON_SIZE/2, ICON_SIZE, cg_items[item - bg_itemlist].icons[2-(value-1)] ); + CG_DrawPic( 606, 366, 24, 48, cg_items[item - bg_itemlist].icons[2 - ( value - 1 )] ); + + } else { +// CG_DrawPic( 100, (SCREEN_HEIGHT-ICON_SIZE)-8, ICON_SIZE/2, ICON_SIZE, cg_items[item - bg_itemlist].icons[0] ); + CG_DrawPic( 606, 366, 24, 48, cg_items[item - bg_itemlist].icons[0] ); + + } + + // draw the selection box so it's not just floating in space + CG_DrawPic( 606 - 4, 366 - 4, 32, 56, cgs.media.selectShader ); + } +} +/* + if(cg.holdableSelect == HI_WINE) { + if(value > 3) + value = 3; // 3 stages to icon, just draw full if beyond 'full' + + CG_DrawPic( 598 + 16, 366, 16, 32, cg_items[item - bg_itemlist].icons[2-(value-1)] ); + CG_DrawPic( (598 + 16)-4, 366-4, 24, 40, cgs.media.selectShader ); + + } else { + CG_DrawPic( 598, 366, 32, 32, cg_items[item - bg_itemlist].icons[0] ); + CG_DrawPic( 598-4, 366-4, 40, 40, cgs.media.selectShader ); + } +*/ + +/* +=================== +CG_DrawReward +=================== +*/ +static void CG_DrawReward( void ) { + float *color; + int i; + float x, y; + + if ( !cg_drawRewards.integer ) { + return; + } + color = CG_FadeColor( cg.rewardTime, REWARD_TIME ); + if ( !color ) { + return; + } + + trap_R_SetColor( color ); + y = 56; + x = 320 - cg.rewardCount * ICON_SIZE / 2; + for ( i = 0 ; i < cg.rewardCount ; i++ ) { + CG_DrawPic( x, y, ICON_SIZE - 4, ICON_SIZE - 4, cg.rewardShader ); + x += ICON_SIZE; + } + trap_R_SetColor( NULL ); +} + + +/* +=============================================================================== + +LAGOMETER + +=============================================================================== +*/ + +#define LAG_SAMPLES 128 + + +typedef struct { + int frameSamples[LAG_SAMPLES]; + int frameCount; + int snapshotFlags[LAG_SAMPLES]; + int snapshotSamples[LAG_SAMPLES]; + int snapshotCount; +} lagometer_t; + +lagometer_t lagometer; + +/* +============== +CG_AddLagometerFrameInfo + +Adds the current interpolate / extrapolate bar for this frame +============== +*/ +void CG_AddLagometerFrameInfo( void ) { + int offset; + + offset = cg.time - cg.latestSnapshotTime; + lagometer.frameSamples[ lagometer.frameCount & ( LAG_SAMPLES - 1 ) ] = offset; + lagometer.frameCount++; +} + +/* +============== +CG_AddLagometerSnapshotInfo + +Each time a snapshot is received, log its ping time and +the number of snapshots that were dropped before it. + +Pass NULL for a dropped packet. +============== +*/ +void CG_AddLagometerSnapshotInfo( snapshot_t *snap ) { + // dropped packet + if ( !snap ) { + lagometer.snapshotSamples[ lagometer.snapshotCount & ( LAG_SAMPLES - 1 ) ] = -1; + lagometer.snapshotCount++; + return; + } + + // add this snapshot's info + lagometer.snapshotSamples[ lagometer.snapshotCount & ( LAG_SAMPLES - 1 ) ] = snap->ping; + lagometer.snapshotFlags[ lagometer.snapshotCount & ( LAG_SAMPLES - 1 ) ] = snap->snapFlags; + lagometer.snapshotCount++; +} + +/* +============== +CG_DrawDisconnect + +Should we draw something differnet for long lag vs no packets? +============== +*/ +static void CG_DrawDisconnect( void ) { + float x, y; + int cmdNum; + usercmd_t cmd; + const char *s; + int w; // bk010215 - FIXME char message[1024]; + + // draw the phone jack if we are completely past our buffers + cmdNum = trap_GetCurrentCmdNumber() - CMD_BACKUP + 1; + trap_GetUserCmd( cmdNum, &cmd ); + if ( cmd.serverTime <= cg.snap->ps.commandTime + || cmd.serverTime > cg.time ) { // special check for map_restart // bk 0102165 - FIXME + return; + } + + // also add text in center of screen + s = "Connection Interrupted"; // bk 010215 - FIXME + w = CG_DrawStrlen( s ) * BIGCHAR_WIDTH; + CG_DrawBigString( 320 - w / 2, 100, s, 1.0F ); + + // blink the icon + if ( ( cg.time >> 9 ) & 1 ) { + return; + } + + x = 640 - 48; + y = 480 - 48; + + CG_DrawPic( x, y, 48, 48, trap_R_RegisterShader( "gfx/2d/net.tga" ) ); +} + + +#define MAX_LAGOMETER_PING 900 +#define MAX_LAGOMETER_RANGE 300 + +/* +============== +CG_DrawLagometer +============== +*/ +static void CG_DrawLagometer( void ) { + int a, x, y, i; + float v; + float ax, ay, aw, ah, mid, range; + int color; + float vscale; + + if ( !cg_lagometer.integer || cgs.localServer ) { +// if(0) { + CG_DrawDisconnect(); + return; + } + + // + // draw the graph + // + x = 640 - 48; + y = 480 - 48; + + trap_R_SetColor( NULL ); + CG_DrawPic( x, y, 48, 48, cgs.media.lagometerShader ); + + ax = x; + ay = y; + aw = 48; + ah = 48; + CG_AdjustFrom640( &ax, &ay, &aw, &ah ); + + color = -1; + range = ah / 3; + mid = ay + range; + + vscale = range / MAX_LAGOMETER_RANGE; + + // draw the frame interpoalte / extrapolate graph + for ( a = 0 ; a < aw ; a++ ) { + i = ( lagometer.frameCount - 1 - a ) & ( LAG_SAMPLES - 1 ); + v = lagometer.frameSamples[i]; + v *= vscale; + if ( v > 0 ) { + if ( color != 1 ) { + color = 1; + trap_R_SetColor( g_color_table[ColorIndex( COLOR_YELLOW )] ); + } + if ( v > range ) { + v = range; + } + trap_R_DrawStretchPic( ax + aw - a, mid - v, 1, v, 0, 0, 0, 0, cgs.media.whiteShader ); + } else if ( v < 0 ) { + if ( color != 2 ) { + color = 2; + trap_R_SetColor( g_color_table[ColorIndex( COLOR_BLUE )] ); + } + v = -v; + if ( v > range ) { + v = range; + } + trap_R_DrawStretchPic( ax + aw - a, mid, 1, v, 0, 0, 0, 0, cgs.media.whiteShader ); + } + } + + // draw the snapshot latency / drop graph + range = ah / 2; + vscale = range / MAX_LAGOMETER_PING; + + for ( a = 0 ; a < aw ; a++ ) { + i = ( lagometer.snapshotCount - 1 - a ) & ( LAG_SAMPLES - 1 ); + v = lagometer.snapshotSamples[i]; + if ( v > 0 ) { + if ( lagometer.snapshotFlags[i] & SNAPFLAG_RATE_DELAYED ) { + if ( color != 5 ) { + color = 5; // YELLOW for rate delay + trap_R_SetColor( g_color_table[ColorIndex( COLOR_YELLOW )] ); + } + } else { + if ( color != 3 ) { + color = 3; + trap_R_SetColor( g_color_table[ColorIndex( COLOR_GREEN )] ); + } + } + v = v * vscale; + if ( v > range ) { + v = range; + } + trap_R_DrawStretchPic( ax + aw - a, ay + ah - v, 1, v, 0, 0, 0, 0, cgs.media.whiteShader ); + } else if ( v < 0 ) { + if ( color != 4 ) { + color = 4; // RED for dropped snapshots + trap_R_SetColor( g_color_table[ColorIndex( COLOR_RED )] ); + } + trap_R_DrawStretchPic( ax + aw - a, ay + ah - range, 1, range, 0, 0, 0, 0, cgs.media.whiteShader ); + } + } + + trap_R_SetColor( NULL ); + + if ( cg_nopredict.integer || cg_synchronousClients.integer ) { + CG_DrawBigString( ax, ay, "snc", 1.0 ); + } + + CG_DrawDisconnect(); +} + + +/* +=============================================================================== + +CENTER PRINTING + +=============================================================================== +*/ + +/* +============== +CG_CenterPrint + +Called for important messages that should stay in the center of the screen +for a few moments +============== +*/ +void CG_CenterPrint( const char *str, int y, int charWidth ) { + unsigned char *s; + +//----(SA) added translation lookup + Q_strncpyz( cg.centerPrint, CG_translateString( (char*)str ), sizeof( cg.centerPrint ) ); +//----(SA) end + + + cg.centerPrintTime = cg.time; + cg.centerPrintY = y; + cg.centerPrintCharWidth = charWidth; + + // count the number of lines for centering + cg.centerPrintLines = 1; + s = cg.centerPrint; + while ( *s ) { + if ( *s == '\n' ) { + cg.centerPrintLines++; + } + if ( !Q_strncmp( s, "\\n", 1 ) ) { + cg.centerPrintLines++; + s++; + } + s++; + } +} + + +/* +=================== +CG_DrawCenterString +=================== +*/ +static void CG_DrawCenterString( void ) { + char *start; + int l; + int x, y, w; + float *color; + + if ( !cg.centerPrintTime ) { + return; + } + + color = CG_FadeColor( cg.centerPrintTime, 1000 * cg_centertime.value ); + if ( !color ) { + return; + } + + trap_R_SetColor( color ); + + start = cg.centerPrint; + + y = cg.centerPrintY - cg.centerPrintLines * BIGCHAR_HEIGHT / 2; + + while ( 1 ) { + char linebuffer[1024]; + + for ( l = 0; l < 40; l++ ) { + if ( !start[l] || start[l] == '\n' || !Q_strncmp( &start[l], "\\n", 1 ) ) { + break; + } + linebuffer[l] = start[l]; + } + linebuffer[l] = 0; + + w = cg.centerPrintCharWidth * CG_DrawStrlen( linebuffer ); + + x = ( SCREEN_WIDTH - w ) / 2; + + CG_DrawStringExt( x, y, linebuffer, color, qfalse, qtrue, cg.centerPrintCharWidth, (int)( cg.centerPrintCharWidth * 1.5 ), 0 ); +// CG_Text_Paint(x, y, 2, 0.3f, color, linebuffer, 0, 0, ITEM_TEXTSTYLE_SHADOWEDMORE); + +// y += cg.centerPrintCharWidth * 1.5; + y += cg.centerPrintCharWidth * 2; + +// while ( *start && ( *start != '\n' ) && !Q_strncmp(start, "\\n", 1) ) { + while ( *start && ( *start != '\n' ) ) { + if ( !Q_strncmp( start, "\\n", 1 ) ) { + start++; + break; + } + start++; + } + if ( !*start ) { + break; + } + start++; + } + + trap_R_SetColor( NULL ); +} + + + +/* +================================================================================ + +CROSSHAIRS + +================================================================================ +*/ + + +/* +============== +CG_DrawWeapReticle +============== +*/ +static void CG_DrawWeapReticle( void ) { + int weap; + vec4_t color = {0, 0, 0, 1}; + vec4_t snoopercolor = {0.7, .8, 0.7, 0}; // greenish + float snooperBrightness; + float x = 80, y, w = 240, h = 240; + + CG_AdjustFrom640( &x, &y, &w, &h ); + + weap = cg.weaponSelect; + + // DHM - Nerve :: So that we will draw reticle + if ( cgs.gametype == GT_WOLF && cg.snap->ps.pm_flags & PMF_FOLLOW ) { + weap = cg.snap->ps.weapon; + } + + + if ( weap == WP_SNIPERRIFLE ) { + + + // sides + CG_FillRect( 0, 0, 80, 480, color ); + CG_FillRect( 560, 0, 80, 480, color ); + + // center + if ( cgs.media.reticleShaderSimpleQ ) { + trap_R_DrawStretchPic( x, 0, w, h, 0, 0, 1, 1, cgs.media.reticleShaderSimpleQ ); // tl + trap_R_DrawStretchPic( x + w, 0, w, h, 1, 0, 0, 1, cgs.media.reticleShaderSimpleQ ); // tr + trap_R_DrawStretchPic( x, h, w, h, 0, 1, 1, 0, cgs.media.reticleShaderSimpleQ ); // bl + trap_R_DrawStretchPic( x + w, h, w, h, 1, 1, 0, 0, cgs.media.reticleShaderSimpleQ ); // br + } + + // hairs + CG_FillRect( 84, 239, 177, 2, color ); // left + CG_FillRect( 320, 242, 1, 58, color ); // center top + CG_FillRect( 319, 300, 2, 178, color ); // center bot + CG_FillRect( 380, 239, 177, 2, color ); // right + } else if ( weap == WP_SNOOPERSCOPE ) { + // sides + CG_FillRect( 0, 0, 80, 480, color ); + CG_FillRect( 560, 0, 80, 480, color ); + + // center + +//----(SA) added + // DM didn't like how bright it gets + snooperBrightness = Com_Clamp( 0.0f, 1.0f, cg_reticleBrightness.value ); + snoopercolor[0] *= snooperBrightness; + snoopercolor[1] *= snooperBrightness; + snoopercolor[2] *= snooperBrightness; + trap_R_SetColor( snoopercolor ); +//----(SA) end + + if ( cgs.media.snooperShaderSimple ) { + CG_DrawPic( 80, 0, 480, 480, cgs.media.snooperShaderSimple ); + } + + // hairs + + CG_FillRect( 310, 120, 20, 1, color ); // ----- + CG_FillRect( 300, 160, 40, 1, color ); // ------------- + CG_FillRect( 310, 200, 20, 1, color ); // ----- + + CG_FillRect( 140, 239, 360, 1, color ); // horiz --------------------------- + + CG_FillRect( 310, 280, 20, 1, color ); // ----- + CG_FillRect( 300, 320, 40, 1, color ); // ------------- + CG_FillRect( 310, 360, 20, 1, color ); // ----- + + + + CG_FillRect( 400, 220, 1, 40, color ); // l + + CG_FillRect( 319, 60, 1, 360, color ); // vert + + CG_FillRect( 240, 220, 1, 40, color ); // r + } else if ( weap == WP_FG42SCOPE ) { + // sides + CG_FillRect( 0, 0, 80, 480, color ); + CG_FillRect( 560, 0, 80, 480, color ); + + // center + if ( cgs.media.reticleShaderSimpleQ ) { + trap_R_DrawStretchPic( x, 0, w, h, 0, 0, 1, 1, cgs.media.reticleShaderSimpleQ ); // tl + trap_R_DrawStretchPic( x + w, 0, w, h, 1, 0, 0, 1, cgs.media.reticleShaderSimpleQ ); // tr + trap_R_DrawStretchPic( x, h, w, h, 0, 1, 1, 0, cgs.media.reticleShaderSimpleQ ); // bl + trap_R_DrawStretchPic( x + w, h, w, h, 1, 1, 0, 0, cgs.media.reticleShaderSimpleQ ); // br + } + + // hairs + CG_FillRect( 84, 239, 150, 3, color ); // left + CG_FillRect( 234, 240, 173, 1, color ); // horiz center + CG_FillRect( 407, 239, 150, 3, color ); // right + + + CG_FillRect( 319, 2, 3, 151, color ); // top center top + CG_FillRect( 320, 153, 1, 114, color ); // top center bot + + CG_FillRect( 320, 241, 1, 87, color ); // bot center top + CG_FillRect( 319, 327, 3, 151, color ); // bot center bot + } +} + + +//----(SA) removed (9/8/2001) + +/* +============== +CG_DrawBinocReticle +============== +*/ +static void CG_DrawBinocReticle( void ) { + // an alternative. This gives nice sharp lines at the expense of a few extra polys + vec4_t color = {0, 0, 0, 1}; + float x, y, w = 320, h = 240; + + if ( cgs.media.binocShaderSimpleQ ) { + CG_AdjustFrom640( &x, &y, &w, &h ); + trap_R_DrawStretchPic( 0, 0, w, h, 0, 0, 1, 1, cgs.media.binocShaderSimpleQ ); // tl + trap_R_DrawStretchPic( w, 0, w, h, 1, 0, 0, 1, cgs.media.binocShaderSimpleQ ); // tr + trap_R_DrawStretchPic( 0, h, w, h, 0, 1, 1, 0, cgs.media.binocShaderSimpleQ ); // bl + trap_R_DrawStretchPic( w, h, w, h, 1, 1, 0, 0, cgs.media.binocShaderSimpleQ ); // br + } + + CG_FillRect( 146, 239, 348, 1, color ); + + CG_FillRect( 188, 234, 1, 13, color ); // ll + CG_FillRect( 234, 226, 1, 29, color ); // l + CG_FillRect( 274, 234, 1, 13, color ); // lr + CG_FillRect( 320, 213, 1, 55, color ); // center + CG_FillRect( 360, 234, 1, 13, color ); // rl + CG_FillRect( 406, 226, 1, 29, color ); // r + CG_FillRect( 452, 234, 1, 13, color ); // rr +} + +void CG_FinishWeaponChange( int lastweap, int newweap ); // JPW NERVE + + +/* +================= +CG_DrawCrosshair +================= +*/ +static void CG_DrawCrosshair( void ) { + float w, h; + qhandle_t hShader; + float f; + float x, y; + int weapnum; // DHM - Nerve + vec4_t hcolor = {1, 1, 1, 0}; + qboolean friendInSights = qfalse; + + if ( cg.renderingThirdPerson ) { + return; + } + + if ( cg_crosshairHealth.integer ) { + CG_ColorForHealth( hcolor ); + } + + hcolor[3] = cg_crosshairAlpha.value; //----(SA) added + + + // on mg42 + if ( cg.snap->ps.eFlags & EF_MG42_ACTIVE ) { + hcolor[0] = hcolor[1] = hcolor[2] = 0.0f; + hcolor[3] = 0.6f; + // option 1 +// CG_FillRect (300, 240, 40, 2, hcolor); // horizontal +// CG_FillRect (319, 242, 2, 16, hcolor); // vertical + + // option 2 + CG_FillRect( 305, 240, 30, 2, hcolor ); // horizontal + CG_FillRect( 314, 256, 12, 2, hcolor ); // horizontal2 + CG_FillRect( 319, 242, 2, 32, hcolor ); // vertical + + return; + } + + friendInSights = (qboolean)( cg.snap->ps.serverCursorHint == HINT_PLYR_FRIEND ); //----(SA) added + + // DHM - Nerve :: show reticle in limbo and spectator + if ( cgs.gametype == GT_WOLF && cg.snap->ps.pm_flags & PMF_FOLLOW ) { + weapnum = cg.snap->ps.weapon; + } else { + weapnum = cg.weaponSelect; + } + + switch ( weapnum ) { + + // weapons that get no reticle + case WP_NONE: // no weapon, no crosshair + case WP_GARAND: + if ( cg.zoomedBinoc ) { + CG_DrawBinocReticle(); + } + return; + break; + + // special reticle for weapon + case WP_KNIFE: + if ( cg.zoomedBinoc ) { + CG_DrawBinocReticle(); + return; + } + + // no crosshair when looking at exits + if ( cg.snap->ps.serverCursorHint >= HINT_EXIT && cg.snap->ps.serverCursorHint <= HINT_NOEXIT_FAR ) { + return; + } + + if ( !friendInSights ) { + if ( !cg.snap->ps.leanf ) { // no crosshair while leaning + CG_FillRect( 319, 239, 2, 2, hcolor ); // dot + } + return; + } + + break; + + case WP_SNIPERRIFLE: + case WP_SNOOPERSCOPE: + case WP_FG42SCOPE: + +// JPW NERVE -- don't let players run with rifles -- speed 80 == crouch, 128 == walk, 256 == run + if ( cg_gameType.integer != GT_SINGLE_PLAYER ) { + if ( VectorLength( cg.snap->ps.velocity ) > 127.0f ) { + if ( cg.snap->ps.weapon == WP_SNIPERRIFLE ) { + CG_FinishWeaponChange( WP_SNIPERRIFLE, WP_MAUSER ); + } + if ( cg.snap->ps.weapon == WP_SNOOPERSCOPE ) { + CG_FinishWeaponChange( WP_SNOOPERSCOPE, WP_GARAND ); + } + } + } +// jpw + + CG_DrawWeapReticle(); + return; + + default: + break; + } + + // using binoculars + if ( cg.zoomedBinoc ) { + CG_DrawBinocReticle(); + return; + } + + + // mauser only gets crosshair if you don't have the scope (I don't like this, but it's a test) + if ( cg.weaponSelect == WP_MAUSER ) { + if ( COM_BitCheck( cg.predictedPlayerState.weapons, WP_SNIPERRIFLE ) ) { + return; + } + } + + + if ( !cg_drawCrosshair.integer ) { //----(SA) moved down so it doesn't keep the scoped weaps from drawing reticles + return; + } + + // no crosshair while leaning + if ( cg.snap->ps.leanf ) { + return; + } + + // no crosshair when looking at exits + if ( cg.snap->ps.serverCursorHint >= HINT_EXIT && cg.snap->ps.serverCursorHint <= HINT_NOEXIT_FAR ) { + return; + } + + if ( cg_paused.integer ) { + // no draw if any menu's are up (or otherwise paused) + return; + } + + // set color based on health + if ( cg_crosshairHealth.integer ) { + trap_R_SetColor( hcolor ); + } else { + trap_R_SetColor( NULL ); + } + + w = h = cg_crosshairSize.value; +/* + // pulse the size of the crosshair when picking up items + f = cg.time - cg.itemPickupBlendTime; + if ( f > 0 && f < ITEM_BLOB_TIME ) { + f /= ITEM_BLOB_TIME; + w *= ( 1 + f ); + h *= ( 1 + f ); + } +*/ + // RF, crosshair size represents aim spread + f = (float)cg.snap->ps.aimSpreadScale / 255.0; + w *= ( 1 + f * 2.0 ); + h *= ( 1 + f * 2.0 ); + + x = cg_crosshairX.integer; + y = cg_crosshairY.integer; + CG_AdjustFrom640( &x, &y, &w, &h ); + +//----(SA) modified + if ( friendInSights ) { + hShader = cgs.media.crosshairFriendly; + } else { + hShader = cgs.media.crosshairShader[ cg_drawCrosshair.integer % NUM_CROSSHAIRS ]; + } +//----(SA) end + + // NERVE - SMF - modified, fixes crosshair offset in shifted/scaled 3d views + // (SA) also breaks scaled view... + trap_R_DrawStretchPic( x + cg.refdef.x + 0.5 * ( cg.refdef.width - w ), + y + cg.refdef.y + 0.5 * ( cg.refdef.height - h ), + w, h, 0, 0, 1, 1, hShader ); +} + + + +/* +================= +CG_ScanForCrosshairEntity +================= +*/ +static void CG_ScanForCrosshairEntity( void ) { + trace_t trace; +// gentity_t *traceEnt; + vec3_t start, end; + int content; + + // DHM - Nerve :: We want this in multiplayer + if ( cgs.gametype == GT_SINGLE_PLAYER ) { + return; //----(SA) don't use any scanning at the moment. + + } + VectorCopy( cg.refdef.vieworg, start ); + VectorMA( start, 4096, cg.refdef.viewaxis[0], end ); //----(SA) changed from 8192 + + CG_Trace( &trace, start, vec3_origin, vec3_origin, end, + cg.snap->ps.clientNum, CONTENTS_SOLID | CONTENTS_BODY | CONTENTS_ITEM ); + +//----(SA) allow targets that aren't clients +// if ( trace.entityNum >= MAX_CLIENTS ) { +// return; +// } + +// traceEnt = &g_entities[trace.entityNum]; + + + // if the player is in fog, don't show it + content = trap_CM_PointContents( trace.endpos, 0 ); + if ( content & CONTENTS_FOG ) { + return; + } + + // if the player is invisible, don't show it + if ( cg_entities[ trace.entityNum ].currentState.powerups & ( 1 << PW_INVIS ) ) { + return; + } + + // update the fade timer + cg.crosshairClientNum = trace.entityNum; + cg.crosshairClientTime = cg.time; +} + + + +/* +============== +CG_DrawDynamiteStatus +============== +*/ +static void CG_DrawDynamiteStatus( void ) { + float color[4]; + char *name; + int timeleft; + float w; + + if ( cg.snap->ps.weapon != WP_DYNAMITE ) { + return; + } + + if ( cg.snap->ps.grenadeTimeLeft <= 0 ) { + return; + } + + timeleft = cg.snap->ps.grenadeTimeLeft; + +// color = g_color_table[ColorIndex(COLOR_RED)]; + color[0] = color[3] = 1.0f; + + // fade red as it pulses past seconds + color[1] = color[2] = 1.0f - ( (float)( timeleft % 1000 ) * 0.001f ); + + if ( timeleft < 300 ) { // fade up the text + color[3] = (float)timeleft / 300.0f; + } + + trap_R_SetColor( color ); + + timeleft *= 5; + timeleft -= ( timeleft % 5000 ); + timeleft += 5000; + timeleft /= 1000; + + name = va( "Timer: %d", timeleft ); + w = CG_DrawStrlen( name ) * BIGCHAR_WIDTH; + + color[3] *= cg_hudAlpha.value; + CG_DrawBigStringColor( 320 - w / 2, 170, name, color ); + + trap_R_SetColor( NULL ); +} + + + +/* +============== +CG_CheckForCursorHints +============== +*/ +void CG_CheckForCursorHints( void ) { + + if ( cg.renderingThirdPerson ) { + return; + } + + if ( cg.snap->ps.serverCursorHint != HINT_NONE ) { // let the client remember what was last looked at (for fading out) + cg.cursorHintTime = cg.time; + cg.cursorHintFade = cg_hintFadeTime.integer; // fade out time + cg.cursorHintIcon = cg.snap->ps.serverCursorHint; + cg.cursorHintValue = cg.snap->ps.serverCursorHintVal; + } + + // (SA) (8/14/01) removed all the client-side stuff. don't think it's really necessary anymore +} + + +/* +===================== +CG_DrawCrosshairNames +===================== +*/ +static void CG_DrawCrosshairNames( void ) { + float *color; + vec4_t teamColor; // NERVE - SMF + char *name; + float w; + + if ( !cg_drawCrosshair.integer ) { + return; + } + if ( !cg_drawCrosshairNames.integer ) { + return; + } + if ( cg.renderingThirdPerson ) { + return; + } + + // Ridah + if ( cg_gameType.integer == GT_SINGLE_PLAYER ) { + return; + } + // done. + + // scan the known entities to see if the crosshair is sighted on one + CG_ScanForCrosshairEntity(); + + // draw the name of the player being looked at + color = CG_FadeColor( cg.crosshairClientTime, 1000 ); + + if ( !color ) { + trap_R_SetColor( NULL ); + return; + } + + // NERVE - SMF - use fade alpha but color text according to teams + teamColor[3] = color[3]; + + // NERVE - SMF - no longer identify opposing side, so just use green now +// if ( cgs.clientinfo[ cg.crosshairClientNum ].team != cgs.clientinfo[ cg.clientNum ].team ) +// VectorSet( teamColor, 0.7608, 0.1250, 0.0859 ); // LIGHT-RED +// else + VectorSet( teamColor, 0.1250, 0.7608, 0.0859 ); // LIGHT-GREEN + + trap_R_SetColor( teamColor ); + // -NERVE - SMF + + name = cgs.clientinfo[ cg.crosshairClientNum ].name; + w = CG_DrawStrlen( va( "Axis: %s", name ) ) * BIGCHAR_WIDTH; +// CG_DrawBigString( 320 - w / 2, 170, name, color[3] * 0.5 ); + + // NERVE - SMF + if ( strlen( name ) ) { + if ( ( cgs.clientinfo[ cg.crosshairClientNum ].team == TEAM_RED ) && + ( cgs.clientinfo[cg.snap->ps.clientNum].team == TEAM_RED ) ) { // JPW NERVE -- only show same team info so people can't pan-search + CG_DrawBigStringColor( 320 - w / 2, 170, va( "Axis: %s", name ), teamColor ); + } else if ( ( cgs.clientinfo[ cg.crosshairClientNum ].team == TEAM_BLUE ) && + ( cgs.clientinfo[cg.snap->ps.clientNum].team == TEAM_BLUE ) ) { // JPW NERVE -- so's we can't find snipers for free + CG_DrawBigStringColor( 320 - w / 2, 170, va( "Ally: %s", name ), teamColor ); + } + } + // -NERVE - SMF + + trap_R_SetColor( NULL ); +} + + + +//============================================================================== + +/* +================= +CG_DrawSpectator +================= +*/ +static void CG_DrawSpectator( void ) { + CG_DrawBigString( 320 - 9 * 8, 440, "SPECTATOR", 1.0F ); + if ( cgs.gametype == GT_TOURNAMENT ) { + CG_DrawBigString( 320 - 15 * 8, 460, "waiting to play", 1.0F ); + } + if ( cgs.gametype == GT_TEAM || cgs.gametype == GT_CTF ) { + CG_DrawBigString( 320 - 25 * 8, 460, "use the TEAM menu to play", 1.0F ); + } +} + +/* +================= +CG_DrawVote +================= +*/ +static void CG_DrawVote( void ) { + char *s; + int sec; + + if ( !cgs.voteTime ) { + return; + } + + // play a talk beep whenever it is modified + if ( cgs.voteModified ) { + cgs.voteModified = qfalse; + trap_S_StartLocalSound( cgs.media.talkSound, CHAN_LOCAL_SOUND ); + } + + sec = ( VOTE_TIME - ( cg.time - cgs.voteTime ) ) / 1000; + if ( sec < 0 ) { + sec = 0; + } + s = va( "VOTE(%i):%s yes(F1):%i no(F2):%i", sec, cgs.voteString, cgs.voteYes, cgs.voteNo ); + CG_DrawSmallString( 0, 58, s, 1.0F ); +} + +/* +================= +CG_DrawIntermission +================= +*/ +static void CG_DrawIntermission( void ) { + if ( cgs.gametype == GT_SINGLE_PLAYER ) { + CG_DrawCenterString(); + return; + } + + cg.scoreFadeTime = cg.time; + CG_DrawScoreboard(); +} + +// NERVE - SMF +/* +================= +CG_ActivateLimboMenu +================= +*/ +// TTimo: unused +/* +static void CG_ActivateLimboMenu( void ) { + static qboolean latch = qfalse; + qboolean test; + char buf[32]; + + if ( cgs.gametype != GT_WOLF ) + return; + + // should we open the limbo menu + test = cg.snap->ps.pm_flags & PMF_LIMBO || cg.snap->ps.persistant[PERS_TEAM] == TEAM_SPECTATOR; + + if ( test && !latch ) { + trap_SendConsoleCommand( "startLimboMode\n" ); + trap_SendConsoleCommand( "OpenLimboMenu\n" ); + latch = qtrue; + } + else if ( !test && latch ) { + trap_SendConsoleCommand( "stopLimboMode\n" ); + trap_SendConsoleCommand( "CloseLimboMenu\n" ); + latch = qfalse; + } + + // set the limbo state + trap_Cvar_VariableStringBuffer( "ui_limboMode", buf, sizeof( buf ) ); + + if ( atoi( buf ) ) + cg.limboMenu = qtrue; + else + cg.limboMenu = qfalse; +} +*/ +// -NERVE - SMF + +/* +================= +CG_DrawFollow +================= +*/ +static qboolean CG_DrawFollow( void ) { + float x; + vec4_t color; + const char *name; + char deploytime[128]; // JPW NERVE + + if ( !( cg.snap->ps.pm_flags & PMF_FOLLOW ) ) { + return qfalse; + } + color[0] = 1; + color[1] = 1; + color[2] = 1; + color[3] = 1; + +// JPW NERVE -- if in limbo, show different follow message + if ( cg.snap->ps.pm_flags & PMF_LIMBO ) { +// CG_Printf("following %s\n",cgs.clientinfo[ cg.snap->ps.clientNum ].name); + color[1] = 0; + color[2] = 0; + if ( cgs.clientinfo[cg.snap->ps.clientNum].team == TEAM_RED ) { + sprintf( deploytime,"Deploying in %d seconds", (int)( (float)( cg_redlimbotime.integer - ( cg.time % cg_redlimbotime.integer ) ) * 0.001f ) ); + } else { + sprintf( deploytime,"Deploying in %d seconds", (int)( (float)( cg_bluelimbotime.integer - ( cg.time % cg_bluelimbotime.integer ) ) * 0.001f ) ); + } + + x = 0.5 * ( 640 - BIGCHAR_WIDTH * strlen( deploytime ) ); //CG_DrawStrlen( deploytime ) ); + CG_DrawStringExt( x, 24, deploytime, color, qtrue, qtrue, BIGCHAR_WIDTH, BIGCHAR_HEIGHT, 0 ); + sprintf( deploytime,"(Following %s)",cgs.clientinfo[ cg.snap->ps.clientNum ].name ); + x = 0.5 * ( 640 - BIGCHAR_WIDTH * strlen( deploytime ) ); //CG_DrawStrlen( deploytime ) ); + CG_DrawStringExt( x, 48, deploytime, color, qtrue, qtrue, BIGCHAR_WIDTH, BIGCHAR_HEIGHT, 0 ); + + } else { +// jpw + CG_DrawBigString( 320 - 9 * 8, 24, "following", 1.0F ); + + name = cgs.clientinfo[ cg.snap->ps.clientNum ].name; + + x = 0.5 * ( 640 - GIANT_WIDTH * CG_DrawStrlen( name ) ); + + CG_DrawStringExt( x, 40, name, color, qtrue, qtrue, GIANT_WIDTH, GIANT_HEIGHT, 0 ); + } // JPW NERVE + return qtrue; +} + + + +/* +================= +CG_DrawAmmoWarning +================= +*/ +static void CG_DrawAmmoWarning( void ) { + const char *s; + int w; + +//----(SA) forcing return for now +// if we have messages to show here, comment back in + return; + + + if ( cg_drawAmmoWarning.integer == 0 ) { + return; + } + + if ( !cg.lowAmmoWarning ) { + return; + } + + if ( cg.lowAmmoWarning == 2 ) { + s = "OUT OF AMMO"; + } else { + s = "LOW AMMO WARNING"; + } + w = CG_DrawStrlen( s ) * BIGCHAR_WIDTH; + CG_DrawBigString( 320 - w / 2, 64, s, 1.0F ); +} + +/* +================= +CG_DrawWarmup +================= +*/ +static void CG_DrawWarmup( void ) { + int w; + int sec; + int i; + clientInfo_t *ci1, *ci2; + int cw; + const char *s; + + if ( cgs.gametype == GT_SINGLE_PLAYER ) { + return; // (SA) don't bother with this stuff in sp + } + + sec = cg.warmup; + if ( !sec ) { + return; + } + + if ( sec < 0 ) { + s = "Waiting for players"; + w = CG_DrawStrlen( s ) * BIGCHAR_WIDTH; + CG_DrawBigString( 320 - w / 2, 40, s, 1.0F ); + cg.warmupCount = 0; + return; + } + + + // find the two active players + ci1 = NULL; + ci2 = NULL; + for ( i = 0 ; i < cgs.maxclients ; i++ ) { + if ( cgs.clientinfo[i].infoValid && cgs.clientinfo[i].team == TEAM_FREE ) { + if ( !ci1 ) { + ci1 = &cgs.clientinfo[i]; + } else { + ci2 = &cgs.clientinfo[i]; + } + } + } + + if ( ci1 && ci2 ) { + s = va( "%s vs %s", ci1->name, ci2->name ); + w = CG_DrawStrlen( s ); + if ( w > 640 / GIANT_WIDTH ) { + cw = 640 / w; + } else { + cw = GIANT_WIDTH; + } + CG_DrawStringExt( 320 - w * cw / 2, 20,s, colorWhite, + qfalse, qtrue, cw, (int)( cw * 1.5 ), 0 ); + } + + + sec = ( sec - cg.time ) / 1000; + if ( sec < 0 ) { + sec = 0; + } + s = va( "Starts in: %i", sec + 1 ); + if ( sec != cg.warmupCount ) { + cg.warmupCount = sec; + switch ( sec ) { + case 0: + trap_S_StartLocalSound( cgs.media.count1Sound, CHAN_ANNOUNCER ); + break; + case 1: + trap_S_StartLocalSound( cgs.media.count2Sound, CHAN_ANNOUNCER ); + break; + case 2: + trap_S_StartLocalSound( cgs.media.count3Sound, CHAN_ANNOUNCER ); + break; + default: + break; + } + } + switch ( cg.warmupCount ) { + case 0: + cw = 28; + break; + case 1: + cw = 24; + break; + case 2: + cw = 20; + break; + default: + cw = 16; + break; + } + + w = CG_DrawStrlen( s ); + CG_DrawStringExt( 320 - w * cw / 2, 70, s, colorWhite, + qfalse, qtrue, cw, (int)( cw * 1.5 ), 0 ); +} + +//================================================================================== + +/* +================= +CG_DrawFlashFade +================= +*/ +static void CG_DrawFlashFade( void ) { + static int lastTime; + int elapsed, time; + vec4_t col; + + if ( cgs.scrFadeStartTime + cgs.scrFadeDuration < cg.time ) { + cgs.scrFadeAlphaCurrent = cgs.scrFadeAlpha; + } else if ( cgs.scrFadeAlphaCurrent != cgs.scrFadeAlpha ) { + elapsed = ( time = trap_Milliseconds() ) - lastTime; // we need to use trap_Milliseconds() here since the cg.time gets modified upon reloading + lastTime = time; + if ( elapsed < 500 && elapsed > 0 ) { + if ( cgs.scrFadeAlphaCurrent > cgs.scrFadeAlpha ) { + cgs.scrFadeAlphaCurrent -= ( (float)elapsed / (float)cgs.scrFadeDuration ); + if ( cgs.scrFadeAlphaCurrent < cgs.scrFadeAlpha ) { + cgs.scrFadeAlphaCurrent = cgs.scrFadeAlpha; + } + } else { + cgs.scrFadeAlphaCurrent += ( (float)elapsed / (float)cgs.scrFadeDuration ); + if ( cgs.scrFadeAlphaCurrent > cgs.scrFadeAlpha ) { + cgs.scrFadeAlphaCurrent = cgs.scrFadeAlpha; + } + } + } + } + // now draw the fade + if ( cgs.scrFadeAlphaCurrent > 0.0 ) { +// CG_Printf("fade: %f\n", cgs.scrFadeAlphaCurrent); + VectorClear( col ); + col[3] = cgs.scrFadeAlphaCurrent; +// CG_FillRect( -10, -10, 650, 490, col ); + CG_FillRect( 0, 0, 640, 480, col ); // why do a bunch of these extend outside 640x480? + } +} + + + +/* +============== +CG_DrawFlashZoomTransition + hide the snap transition from regular view to/from zoomed + + FIXME: TODO: use cg_fade? +============== +*/ +static void CG_DrawFlashZoomTransition( void ) { + vec4_t color; + float frac; + int fadeTime; + + if ( !cg.snap ) { + return; + } + + if ( cg.snap->ps.eFlags & EF_MG42_ACTIVE ) { // don't draw when on mg_42 + // keep the timer fresh so when you remove yourself from the mg42, it'll fade + cg.zoomTime = cg.time; + return; + } + + if ( cgs.gametype != GT_SINGLE_PLAYER ) { // JPW NERVE + fadeTime = 400; + } else { + if ( cg.zoomedScope ) { + fadeTime = cg.zoomedScope; //----(SA) + } else { + fadeTime = 300; + } + } + // jpw + + frac = cg.time - cg.zoomTime; + + if ( frac < fadeTime ) { + frac = frac / (float)fadeTime; + + if ( cg.weaponSelect == WP_SNOOPERSCOPE ) { +// Vector4Set( color, 0.7f, 0.3f, 0.7f, 1.0f - frac ); +// Vector4Set( color, 1, 0.5, 1, 1.0f - frac ); +// Vector4Set( color, 0.5f, 0.3f, 0.5f, 1.0f - frac ); + Vector4Set( color, 0.7f, 0.6f, 0.7f, 1.0f - frac ); + } else { + Vector4Set( color, 0, 0, 0, 1.0f - frac ); + } + + CG_FillRect( -10, -10, 650, 490, color ); + } +} + + + +/* +================= +CG_DrawFlashDamage +================= +*/ +static void CG_DrawFlashDamage( void ) { + vec4_t col; + float redFlash; + + if ( !cg.snap ) { + return; + } + + if ( cg.v_dmg_time > cg.time ) { + redFlash = fabs( cg.v_dmg_pitch * ( ( cg.v_dmg_time - cg.time ) / DAMAGE_TIME ) ); + + // blend the entire screen red + if ( redFlash > 5 ) { + redFlash = 5; + } + + VectorSet( col, 0.2, 0, 0 ); + col[3] = 0.7 * ( redFlash / 5.0 ); + + CG_FillRect( -10, -10, 650, 490, col ); + } +} + + +/* +================= +CG_DrawFlashFire +================= +*/ +static void CG_DrawFlashFire( void ) { + vec4_t col = {1,1,1,1}; + float alpha, max, f; + + if ( !cg.snap ) { + return; + } + + if ( cg_thirdPerson.integer ) { + return; + } + + if ( cg.cameraMode ) { // don't draw flames on camera screen. will still do damage though, so not a potential cheat + return; + } + + if ( !cg.snap->ps.onFireStart ) { + cg.v_noFireTime = cg.time; + return; + } + + alpha = (float)( ( FIRE_FLASH_TIME - 1000 ) - ( cg.time - cg.snap->ps.onFireStart ) ) / ( FIRE_FLASH_TIME - 1000 ); + if ( alpha > 0 ) { + if ( alpha >= 1.0 ) { + alpha = 1.0; + } + + // fade in? + f = (float)( cg.time - cg.v_noFireTime ) / FIRE_FLASH_FADEIN_TIME; + if ( f >= 0.0 && f < 1.0 ) { + alpha = f; + } + + max = 0.5 + 0.5 * sin( (float)( ( cg.time / 10 ) % 1000 ) / 1000.0 ); + if ( alpha > max ) { + alpha = max; + } + col[0] = alpha; + col[1] = alpha; + col[2] = alpha; + col[3] = alpha; + trap_R_SetColor( col ); + CG_DrawPic( -10, -10, 650, 490, cgs.media.viewFlashFire[( cg.time / 50 ) % 16] ); + trap_R_SetColor( NULL ); + + trap_S_AddLoopingSound( cg.snap->ps.clientNum, cg.snap->ps.origin, vec3_origin, cgs.media.flameSound, (int)( 255.0 * alpha ) ); + trap_S_AddLoopingSound( cg.snap->ps.clientNum, cg.snap->ps.origin, vec3_origin, cgs.media.flameCrackSound, (int)( 255.0 * alpha ) ); + } else { + cg.v_noFireTime = cg.time; + } +} + +/* +================= +CG_DrawFlashLightning +================= +*/ +static void CG_DrawFlashLightning( void ) { + //vec4_t col={1,1,1,1}; // TTimo: unused + float alpha; + centity_t *cent; + qhandle_t shader; + + if ( !cg.snap ) { + return; + } + + if ( cg_thirdPerson.integer ) { + return; + } + + cent = &cg_entities[cg.snap->ps.clientNum]; + + if ( !cent->pe.teslaDamagedTime || ( cent->pe.teslaDamagedTime > cg.time ) ) { + return; + } + + alpha = 1.0 - (float)( cg.time - cent->pe.teslaDamagedTime ) / LIGHTNING_FLASH_TIME; + if ( alpha > 0 ) { + if ( alpha >= 1.0 ) { + alpha = 1.0; + } + + if ( ( cg.time / 50 ) % ( 2 + ( cg.time % 2 ) ) == 0 ) { + shader = cgs.media.viewTeslaAltDamageEffectShader; + } else { + shader = cgs.media.viewTeslaDamageEffectShader; + } + + CG_DrawPic( -10, -10, 650, 490, shader ); + } +} + + + +/* +============== +CG_DrawFlashBlendBehindHUD + screen flash stuff drawn first (on top of world, behind HUD) +============== +*/ +static void CG_DrawFlashBlendBehindHUD( void ) { + CG_DrawFlashZoomTransition(); +} + + +/* +================= +CG_DrawFlashBlend + screen flash stuff drawn last (on top of everything) +================= +*/ +static void CG_DrawFlashBlend( void ) { + CG_DrawFlashLightning(); + CG_DrawFlashFire(); + CG_DrawFlashDamage(); + CG_DrawFlashFade(); +} + +// NERVE - SMF +/* +================= +CG_DrawObjectiveInfo +================= +*/ +#define OID_LEFT 10 +#define OID_TOP 65 + +void CG_ObjectivePrint( const char *str, int charWidth, int team ) { + char *s; + + Q_strncpyz( cg.oidPrint, str, sizeof( cg.oidPrint ) ); + + cg.oidPrintTime = cg.time; + cg.oidPrintY = OID_TOP; + cg.oidPrintCharWidth = charWidth; + + // count the number of lines for oiding + cg.oidPrintLines = 1; + s = cg.oidPrint; + while ( *s ) { + if ( *s == '\n' ) { + cg.oidPrintLines++; + } + s++; + } +} + +static void CG_DrawObjectiveInfo( void ) { + char *start; + int l; + int x, y, w; + int x1, y1, x2, y2; + float *color; + vec4_t backColor = { 0.2f, 0.2f, 0.2f, 1.f }; + + if ( !cg.oidPrintTime ) { + return; + } + + color = CG_FadeColor( cg.oidPrintTime, 1000 * 5 ); + if ( !color ) { + return; + } + + trap_R_SetColor( color ); + + start = cg.oidPrint; + + y = cg.oidPrintY - cg.oidPrintLines * BIGCHAR_HEIGHT / 2; + + x1 = OID_LEFT - 2; + y1 = y - 2; + x2 = 0; + + // first just find the bounding rect + while ( 1 ) { + char linebuffer[1024]; + + for ( l = 0; l < 40; l++ ) { + if ( !start[l] || start[l] == '\n' ) { + break; + } + linebuffer[l] = start[l]; + } + linebuffer[l] = 0; + + w = cg.oidPrintCharWidth * CG_DrawStrlen( linebuffer ); + if ( x1 + w > x2 ) { + x2 = x1 + w; + } + + x = OID_LEFT; + + y += cg.oidPrintCharWidth * 1.5; + + while ( *start && ( *start != '\n' ) ) { + start++; + } + if ( !*start ) { + break; + } + start++; + } + + x2 = x2 + 4; + y2 = y - cg.oidPrintCharWidth * 1.5 + 4; + + backColor[3] = color[3]; + CG_FillRect( x1, y1, x2 - x1, y2 - y1, backColor ); + + VectorSet( backColor, 0, 0, 0 ); + CG_DrawRect( x1, y1, x2 - x1, y2 - y1, 1, backColor ); + + // do the actual drawing + start = cg.oidPrint; + y = cg.oidPrintY - cg.oidPrintLines * BIGCHAR_HEIGHT / 2; + + while ( 1 ) { + char linebuffer[1024]; + + for ( l = 0; l < 40; l++ ) { + if ( !start[l] || start[l] == '\n' ) { + break; + } + linebuffer[l] = start[l]; + } + linebuffer[l] = 0; + + w = cg.oidPrintCharWidth * CG_DrawStrlen( linebuffer ); + if ( x1 + w > x2 ) { + x2 = x1 + w; + } + + x = OID_LEFT; + + CG_DrawStringExt( x, y, linebuffer, color, qfalse, qtrue, + cg.oidPrintCharWidth, (int)( cg.oidPrintCharWidth * 1.5 ), 0 ); + + y += cg.oidPrintCharWidth * 1.5; + + while ( *start && ( *start != '\n' ) ) { + start++; + } + if ( !*start ) { + break; + } + start++; + } + + trap_R_SetColor( NULL ); +} +// -NERVE - SMF + +//================================================================================== + + +void CG_DrawTimedMenus() { + if ( cg.voiceTime ) { + int t = cg.time - cg.voiceTime; + if ( t > 2500 ) { + Menus_CloseByName( "voiceMenu" ); + trap_Cvar_Set( "cl_conXOffset", "0" ); + cg.voiceTime = 0; + } + } +} + + +/* +================= +CG_Fade +================= +*/ +void CG_Fade( int r, int g, int b, int a, int time, int duration ) { + + // incorporate this into the current fade scheme + + cgs.scrFadeAlpha = (float)a / 255.0f; + cgs.scrFadeStartTime = time; + cgs.scrFadeDuration = duration; + + if ( cgs.scrFadeStartTime + cgs.scrFadeDuration <= cg.time ) { + cgs.scrFadeAlphaCurrent = cgs.scrFadeAlpha; + } + + + return; + + + if ( time <= 0 ) { // do instantly + cg.fadeRate = 1; + cg.fadeTime = cg.time - 1; // set cg.fadeTime behind cg.time so it will start out 'done' + } else { + cg.fadeRate = 1.0f / time; + cg.fadeTime = cg.time + time; + } + + cg.fadeColor2[ 0 ] = ( float )r / 255.0f; + cg.fadeColor2[ 1 ] = ( float )g / 255.0f; + cg.fadeColor2[ 2 ] = ( float )b / 255.0f; + cg.fadeColor2[ 3 ] = ( float )a / 255.0f; +} + + +/* +=============== +CG_DrawGameScreenFade +=============== +*/ +static void CG_DrawGameScreenFade( void ) { + vec4_t col; + + if ( cg.viewFade <= 0.0 ) { + return; + } + + if ( !cg.snap ) { + return; + } + + VectorClear( col ); + col[3] = cg.viewFade; + CG_FillRect( 0, 0, 640, 480, col ); +} + +/* +================= +CG_ScreenFade +================= +*/ +static void CG_ScreenFade( void ) { + int msec; + int i; + float t, invt; + vec4_t color; + + // Ridah, fade the screen (in-game) + CG_DrawGameScreenFade(); + + if ( !cg.fadeRate ) { + return; + } + + msec = cg.fadeTime - cg.time; + if ( msec <= 0 ) { + cg.fadeColor1[ 0 ] = cg.fadeColor2[ 0 ]; + cg.fadeColor1[ 1 ] = cg.fadeColor2[ 1 ]; + cg.fadeColor1[ 2 ] = cg.fadeColor2[ 2 ]; + cg.fadeColor1[ 3 ] = cg.fadeColor2[ 3 ]; + + if ( !cg.fadeColor1[ 3 ] ) { + cg.fadeRate = 0; + return; + } + + CG_FillRect( 0, 0, 640, 480, cg.fadeColor1 ); + + } else { + t = ( float )msec * cg.fadeRate; + invt = 1.0f - t; + + for ( i = 0; i < 4; i++ ) { + color[ i ] = cg.fadeColor1[ i ] * t + cg.fadeColor2[ i ] * invt; + } + + if ( color[ 3 ] ) { + CG_FillRect( 0, 0, 640, 480, color ); + } + } +} + + + +/* +================= +CG_Draw2D +================= +*/ +static void CG_Draw2D( void ) { + + // if we are taking a levelshot for the menu, don't draw anything + if ( cg.levelShot ) { + return; + } + + if ( cg.cameraMode ) { //----(SA) no 2d when in camera view + CG_DrawFlashBlend(); // (for fades) + return; + } + + if ( cg_draw2D.integer == 0 ) { + return; + } + + CG_ScreenFade(); + + + if ( cg.snap->ps.pm_type == PM_INTERMISSION ) { + CG_DrawIntermission(); + return; + } + + CG_DrawFlashBlendBehindHUD(); + + if ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_SPECTATOR ) { + CG_DrawSpectator(); + CG_DrawCrosshair(); + CG_DrawCrosshairNames(); + } else { + // don't draw any status if dead + if ( cg.snap->ps.stats[STAT_HEALTH] > 0 ) { + + CG_DrawCrosshair(); + + if ( cg_drawStatus.integer ) { + Menu_PaintAll(); + CG_DrawTimedMenus(); + } + +// CG_DrawStatusBar(); + CG_DrawAmmoWarning(); + CG_DrawDynamiteStatus(); + CG_DrawCrosshairNames(); + CG_DrawWeaponSelect(); + CG_DrawHoldableSelect(); + CG_DrawPickupItem(); + CG_DrawReward(); + } + if ( cgs.gametype >= GT_TEAM ) { + CG_DrawTeamInfo(); + } + } + + CG_DrawVote(); + + CG_DrawLagometer(); + + if ( !cg_paused.integer ) { + CG_DrawUpperRight(); + } + +// CG_DrawLowerRight(); + if ( !CG_DrawFollow() ) { + CG_DrawWarmup(); + } + + // don't draw center string if scoreboard is up + if ( !CG_DrawScoreboard() ) { + CG_DrawCenterString(); + + CG_DrawObjectiveInfo(); // NERVE - SMF + } + + // Ridah, draw flash blends now + CG_DrawFlashBlend(); +} + +/* +==================== +CG_StartShakeCamera +==================== +*/ +void CG_StartShakeCamera( float p, int duration, vec3_t src, float radius ) { + int i; + + // find a free shake slot + for ( i = 0; i < MAX_CAMERA_SHAKE; i++ ) { + if ( cg.cameraShake[i].time > cg.time || cg.cameraShake[i].time + cg.cameraShake[i].length <= cg.time ) { + break; + } + } + + if ( i == MAX_CAMERA_SHAKE ) { + return; // no free slots + + } + cg.cameraShake[i].scale = p; + + cg.cameraShake[i].length = duration; + cg.cameraShake[i].time = cg.time; + VectorCopy( src, cg.cameraShake[i].src ); + cg.cameraShake[i].radius = radius; +} + +/* +==================== +CG_CalcShakeCamera +==================== +*/ +void CG_CalcShakeCamera() { + float val, scale, dist, x, sx; + float bx = 0.0f; // TTimo: init + int i; + + // build the scale + scale = 0.0f; + sx = (float)cg.time / 600.0; // x * (float)(cg.cameraShake[i].length) / 600.0; + for ( i = 0; i < MAX_CAMERA_SHAKE; i++ ) { + if ( cg.cameraShake[i].time <= cg.time && cg.cameraShake[i].time + cg.cameraShake[i].length > cg.time ) { + dist = Distance( cg.cameraShake[i].src, cg.refdef.vieworg ); + // fade with distance + val = cg.cameraShake[i].scale * ( 1.0f - ( dist / cg.cameraShake[i].radius ) ); + // fade with time + x = 1.0f - ( ( cg.time - cg.cameraShake[i].time ) / cg.cameraShake[i].length ); + val *= x; + // overwrite global scale if larger + if ( val > scale ) { + scale = val; + bx = x; + } + } + } + + // check the current rumble status + if ( cg.rumbleScale > scale ) { + scale = cg.rumbleScale; + bx = cg.rumbleScale; + } + + if ( scale <= 0.0f ) { + cg.cameraShakePhase = crandom() * M_PI; // randomize the phase + return; + } + + if ( scale > 1.0f ) { + scale = 1.0f; + } + + // up/down + val = sin( M_PI * 8 * sx + cg.cameraShakePhase ) * bx * 18.0f * scale; + cg.cameraShakeAngles[0] = val; + //cg.refdefViewAngles[0] += val; + + // left/right + val = sin( M_PI * 15 * sx + cg.cameraShakePhase ) * bx * 16.0f * scale; + cg.cameraShakeAngles[1] = val; + //cg.refdefViewAngles[1] += val; + + // roll + val = sin( M_PI * 12 * sx + cg.cameraShakePhase ) * bx * 10.0f * scale; + cg.cameraShakeAngles[2] = val; + //cg.refdefViewAngles[2] += val; +} + +/* +==================== +CG_ApplyShakeCamera +==================== +*/ +void CG_ApplyShakeCamera() { + VectorAdd( cg.refdefViewAngles, cg.cameraShakeAngles, cg.refdefViewAngles ); + AnglesToAxis( cg.refdefViewAngles, cg.refdef.viewaxis ); +} + +/* +===================== +CG_DrawActive + +Perform all drawing needed to completely fill the screen +===================== +*/ +void CG_DrawActive( stereoFrame_t stereoView ) { + float separation; + vec3_t baseOrg; + + // optionally draw the info screen instead + if ( !cg.snap ) { + CG_DrawInformation(); + return; + } + + // if they are waiting at the mission stats screen, show the stats + if ( cg_gameType.integer == GT_SINGLE_PLAYER ) { + if ( strlen( cg_missionStats.string ) > 1 ) { + trap_Cvar_Set( "com_expectedhunkusage", "-2" ); + CG_DrawInformation(); + return; + } + } + + // optionally draw the tournement scoreboard instead + if ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_SPECTATOR && + ( cg.snap->ps.pm_flags & PMF_SCOREBOARD ) ) { + CG_DrawTourneyScoreboard(); + return; + } + + switch ( stereoView ) { + case STEREO_CENTER: + separation = 0; + break; + case STEREO_LEFT: + separation = -cg_stereoSeparation.value / 2; + break; + case STEREO_RIGHT: + separation = cg_stereoSeparation.value / 2; + break; + default: + separation = 0; + CG_Error( "CG_DrawActive: Undefined stereoView" ); + } + + + // clear around the rendered view if sized down +// CG_TileClear(); // (SA) moved down + + // offset vieworg appropriately if we're doing stereo separation + VectorCopy( cg.refdef.vieworg, baseOrg ); + if ( separation != 0 ) { + VectorMA( cg.refdef.vieworg, -separation, cg.refdef.viewaxis[1], cg.refdef.vieworg ); + } + + cg.refdef.glfog.registered = 0; // make sure it doesn't use fog from another scene +/* + // NERVE - SMF - activate limbo menu and draw small 3d window + CG_ActivateLimboMenu(); + + if ( cg.limboMenu ) { + float x, y, w, h; + x = LIMBO_3D_X; + y = LIMBO_3D_Y; + w = LIMBO_3D_W; + h = LIMBO_3D_H; + + cg.refdef.width = 0; + CG_AdjustFrom640( &x, &y, &w, &h ); + + cg.refdef.x = x; + cg.refdef.y = y; + cg.refdef.width = w; + cg.refdef.height = h; + } + // -NERVE - SMF +*/ + cg.refdef.rdflags |= RDF_DRAWSKYBOX; + if ( !cg_skybox.integer ) { + cg.refdef.rdflags &= ~RDF_DRAWSKYBOX; + } + + trap_R_RenderScene( &cg.refdef ); + + // restore original viewpoint if running stereo + if ( separation != 0 ) { + VectorCopy( baseOrg, cg.refdef.vieworg ); + } + + // clear around the rendered view if sized down + CG_TileClear(); //----(SA) moved to 2d section to avoid 2d/3d fog-state problems + + // draw status bar and other floating elements + CG_Draw2D(); +} + + diff --git a/src/cgame/cg_drawtools.c b/src/cgame/cg_drawtools.c new file mode 100644 index 0000000..cc7ff2d --- /dev/null +++ b/src/cgame/cg_drawtools.c @@ -0,0 +1,1297 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// cg_drawtools.c -- helper functions called by cg_draw, cg_scoreboard, cg_info, etc +#include "cg_local.h" + +/* +================ +CG_AdjustFrom640 + +Adjusted for resolution and screen aspect ratio +================ +*/ +void CG_AdjustFrom640( float *x, float *y, float *w, float *h ) { +#if 0 + // adjust for wide screens + if ( cgs.glconfig.vidWidth * 480 > cgs.glconfig.vidHeight * 640 ) { + *x += 0.5 * ( cgs.glconfig.vidWidth - ( cgs.glconfig.vidHeight * 640 / 480 ) ); + } +#endif + + // NERVE - SMF - hack to make images display properly in small view / limbo mode + if ( cg.limboMenu && cg.refdef.width ) { + float xscale = ( ( cg.refdef.width / cgs.screenXScale ) / 640.f ); + float yscale = ( ( cg.refdef.height / cgs.screenYScale ) / 480.f ); + + ( *x ) = ( *x ) * xscale + ( cg.refdef.x / cgs.screenXScale ); + ( *y ) = ( *y ) * yscale + ( cg.refdef.y / cgs.screenYScale ); + ( *w ) *= xscale; + ( *h ) *= yscale; + } + // -NERVE - SMF + + // scale for screen sizes + *x *= cgs.screenXScale; + *y *= cgs.screenYScale; + *w *= cgs.screenXScale; + *h *= cgs.screenYScale; +} + +/* +================ +CG_FillRect + +Coordinates are 640*480 virtual values +================= +*/ +void CG_FillRect( float x, float y, float width, float height, const float *color ) { + trap_R_SetColor( color ); + + CG_AdjustFrom640( &x, &y, &width, &height ); + trap_R_DrawStretchPic( x, y, width, height, 0, 0, 0, 1, cgs.media.whiteShader ); + + trap_R_SetColor( NULL ); +} + +/* +============== +CG_FillRectGradient +============== +*/ +void CG_FillRectGradient( float x, float y, float width, float height, const float *color, const float *gradcolor, int gradientType ) { + trap_R_SetColor( color ); + + CG_AdjustFrom640( &x, &y, &width, &height ); + trap_R_DrawStretchPicGradient( x, y, width, height, 0, 0, 0, 0, cgs.media.whiteShader, gradcolor, gradientType ); + + trap_R_SetColor( NULL ); +} + + +/* +============== +CG_HorizontalPercentBar + Generic routine for pretty much all status indicators that show a fractional + value to the palyer by virtue of how full a drawn box is. + +flags: + left - 1 + center - 2 // direction is 'right' by default and orientation is 'horizontal' + vert - 4 + nohudalpha - 8 // don't adjust bar's alpha value by the cg_hudalpha value + bg - 16 // background contrast box (bg set with bgColor of 'NULL' means use default bg color (1,1,1,0.25) + spacing - 32 // some bars use different sorts of spacing when drawing both an inner and outer box + + lerp color - 256 // use an average of the start and end colors to set the fill color +============== +*/ + + +// TODO: these flags will be shared, but it was easier to work on stuff if I wasn't changing header files a lot +#define BAR_LEFT 0x0001 +#define BAR_CENTER 0x0002 +#define BAR_VERT 0x0004 +#define BAR_NOHUDALPHA 0x0008 +#define BAR_BG 0x0010 +// different spacing modes for use w/ BAR_BG +#define BAR_BGSPACING_X0Y5 0x0020 +#define BAR_BGSPACING_X0Y0 0x0040 + +#define BAR_LERP_COLOR 0x0100 + +#define BAR_BORDERSIZE 2 + +void CG_FilledBar( float x, float y, float w, float h, const float *startColorIn, float *endColor, const float *bgColor, float frac, int flags ) { + vec4_t backgroundcolor = {1, 1, 1, 0.25f}, colorAtPos; // colorAtPos is the lerped color if necessary + vec4_t startColor; + + int indent = BAR_BORDERSIZE; + + VectorCopy4( startColorIn, startColor ); + + if ( ( flags & BAR_BG ) && bgColor ) { // BAR_BG set, and color specified, use specified bg color + Vector4Copy( bgColor, backgroundcolor ); + } + + // hud alpha + if ( !( flags & BAR_NOHUDALPHA ) ) { + startColor[3] *= cg_hudAlpha.value; + if ( endColor ) { + endColor[3] *= cg_hudAlpha.value; + } + if ( backgroundcolor ) { + backgroundcolor[3] *= cg_hudAlpha.value; + } + } + + if ( flags & BAR_LERP_COLOR ) { + Vector4Average( startColor, endColor, frac, colorAtPos ); + } + + // background + if ( ( flags & BAR_BG ) ) { + // draw background at full size and shrink the remaining box to fit inside with a border. (alternate border may be specified by a BAR_BGSPACING_xx) + CG_FillRect( x, + y, + w, + h, + backgroundcolor ); + + if ( flags & BAR_BGSPACING_X0Y0 ) { // fill the whole box (no border) + + } else if ( flags & BAR_BGSPACING_X0Y5 ) { // spacing created for weapon heat + indent *= 3; + y += indent; + h -= ( 2 * indent ); + + } else { // default spacing of 2 units on each side + x += indent; + y += indent; + w -= ( 2 * indent ); + h -= ( 2 * indent ); + } + } + + + // adjust for horiz/vertical and draw the fractional box + if ( flags & BAR_VERT ) { + if ( flags & BAR_LEFT ) { // TODO: remember to swap colors on the ends here + y += ( h * ( 1 - frac ) ); + } else if ( flags & BAR_CENTER ) { + y += ( h * ( 1 - frac ) / 2 ); + } + + if ( flags & BAR_LERP_COLOR ) { + CG_FillRect( x, y, w, h * frac, colorAtPos ); + } else { +// CG_FillRectGradient ( x, y, w, h * frac, startColor, endColor, 0 ); + CG_FillRect( x, y, w, h * frac, startColor ); + } + + } else { + + if ( flags & BAR_LEFT ) { // TODO: remember to swap colors on the ends here + x += ( w * ( 1 - frac ) ); + } else if ( flags & BAR_CENTER ) { + x += ( w * ( 1 - frac ) / 2 ); + } + + if ( flags & BAR_LERP_COLOR ) { + CG_FillRect( x, y, w * frac, h, colorAtPos ); + } else { +// CG_FillRectGradient ( x, y, w * frac, h, startColor, endColor, 0 ); + CG_FillRect( x, y, w * frac, h, startColor ); + } + } + +} + + +/* +================= +CG_HorizontalPercentBar +================= +*/ +void CG_HorizontalPercentBar( float x, float y, float width, float height, float percent ) { + vec4_t bgcolor = {0.5f, 0.5f, 0.5f, 0.3f}, + color = {1.0f, 1.0f, 1.0f, 0.3f}; + CG_FilledBar( x, y, width, height, color, NULL, bgcolor, percent, BAR_BG | BAR_NOHUDALPHA ); +} + + +/* +================ +CG_DrawSides + +Coords are virtual 640x480 +================ +*/ +void CG_DrawSides( float x, float y, float w, float h, float size ) { + CG_AdjustFrom640( &x, &y, &w, &h ); + size *= cgs.screenXScale; + trap_R_DrawStretchPic( x, y, size, h, 0, 0, 0, 0, cgs.media.whiteShader ); + trap_R_DrawStretchPic( x + w - size, y, size, h, 0, 0, 0, 0, cgs.media.whiteShader ); +} + +void CG_DrawTopBottom( float x, float y, float w, float h, float size ) { + CG_AdjustFrom640( &x, &y, &w, &h ); + size *= cgs.screenYScale; + trap_R_DrawStretchPic( x, y, w, size, 0, 0, 0, 0, cgs.media.whiteShader ); + trap_R_DrawStretchPic( x, y + h - size, w, size, 0, 0, 0, 0, cgs.media.whiteShader ); +} + +/* +================ +UI_DrawRect + +Coordinates are 640*480 virtual values +================= +*/ +void CG_DrawRect( float x, float y, float width, float height, float size, const float *color ) { + vec4_t hudAlphaColor; + + Vector4Copy( color, hudAlphaColor ); + hudAlphaColor[3] *= cg_hudAlpha.value; + + trap_R_SetColor( hudAlphaColor ); + + CG_DrawTopBottom( x, y, width, height, size ); + CG_DrawSides( x, y, width, height, size ); + + trap_R_SetColor( NULL ); +} + + + +/* +================ +CG_DrawPic + +Coordinates are 640*480 virtual values +================= +*/ +void CG_DrawPic( float x, float y, float width, float height, qhandle_t hShader ) { + CG_AdjustFrom640( &x, &y, &width, &height ); + trap_R_DrawStretchPic( x, y, width, height, 0, 0, 1, 1, hShader ); +} + + + +/* +=============== +CG_DrawChar + +Coordinates and size in 640*480 virtual screen size +=============== +*/ +void CG_DrawChar( int x, int y, int width, int height, int ch ) { + int row, col; + float frow, fcol; + float size; + float ax, ay, aw, ah; + + ch &= 255; + + if ( ch == ' ' ) { + return; + } + + ax = x; + ay = y; + aw = width; + ah = height; + CG_AdjustFrom640( &ax, &ay, &aw, &ah ); + + row = ch >> 4; + col = ch & 15; + + frow = row * 0.0625; + fcol = col * 0.0625; + size = 0.0625; + + trap_R_DrawStretchPic( ax, ay, aw, ah, + fcol, frow, + fcol + size, frow + size, + cgs.media.charsetShader ); +} + +/* +=============== +CG_DrawChar2 + +Coordinates and size in 640*480 virtual screen size +=============== +*/ +void CG_DrawChar2( int x, int y, int width, int height, int ch ) { + int row, col; + float frow, fcol; + float size; + float ax, ay, aw, ah; + + ch &= 255; + + if ( ch == ' ' ) { + return; + } + + ax = x; + ay = y; + aw = width; + ah = height; + CG_AdjustFrom640( &ax, &ay, &aw, &ah ); + + row = ch >> 4; + col = ch & 15; + + frow = row * 0.0625; + fcol = col * 0.0625; + size = 0.0625; + + trap_R_DrawStretchPic( ax, ay, aw, ah, + fcol, frow, + fcol + size, frow + size, + cgs.media.menucharsetShader ); +} + +// JOSEPH 4-25-00 +/* +================== +CG_DrawStringExt + +Draws a multi-colored string with a drop shadow, optionally forcing +to a fixed color. + +Coordinates are at 640 by 480 virtual resolution +================== +*/ +void CG_DrawStringExt( int x, int y, const char *string, const float *setColor, + qboolean forceColor, qboolean shadow, int charWidth, int charHeight, int maxChars ) { + vec4_t color; + const unsigned char *s; + int xx; + int cnt; + + if ( maxChars <= 0 ) { + maxChars = 32767; // do them all! + + } + // draw the drop shadow + if ( shadow ) { + color[0] = color[1] = color[2] = 0; + color[3] = setColor[3]; + trap_R_SetColor( color ); + s = string; + xx = x; + cnt = 0; + while ( *s && cnt < maxChars ) { + if ( Q_IsColorString( s ) ) { + s += 2; + continue; + } + CG_DrawChar( xx + 2, y + 2, charWidth, charHeight, *s ); + cnt++; + xx += charWidth; + s++; + } + } + + // draw the colored text + s = string; + xx = x; + cnt = 0; + trap_R_SetColor( setColor ); + while ( *s && cnt < maxChars ) { + if ( Q_IsColorString( s ) ) { + if ( !forceColor ) { + memcpy( color, g_color_table[ColorIndex( *( s + 1 ) )], sizeof( color ) ); + color[3] = setColor[3]; + trap_R_SetColor( color ); + } + s += 2; + continue; + } + CG_DrawChar( xx, y, charWidth, charHeight, *s ); + xx += charWidth; + cnt++; + s++; + } + trap_R_SetColor( NULL ); +} + +/*================== +CG_DrawStringExt2 + +Draws a multi-colored string with a drop shadow, optionally forcing +to a fixed color. + +Coordinates are at 640 by 480 virtual resolution +================== +*/ +void CG_DrawStringExt2( int x, int y, const char *string, const float *setColor, + qboolean forceColor, qboolean shadow, int charWidth, int charHeight, int maxChars ) { + vec4_t color; + const unsigned char *s; + int xx; + int cnt; + + if ( maxChars <= 0 ) { + maxChars = 32767; // do them all! + + } + // draw the drop shadow + if ( shadow ) { + color[0] = color[1] = color[2] = 0; + color[3] = setColor[3]; + trap_R_SetColor( color ); + s = string; + xx = x; + cnt = 0; + while ( *s && cnt < maxChars ) { + if ( Q_IsColorString( s ) ) { + s += 2; + continue; + } + CG_DrawChar2( xx + 2, y + 2, charWidth, charHeight, *s ); + cnt++; + xx += charWidth; + s++; + } + } + + // draw the colored text + s = string; + xx = x; + cnt = 0; + trap_R_SetColor( setColor ); + while ( *s && cnt < maxChars ) { + if ( Q_IsColorString( s ) ) { + if ( !forceColor ) { + memcpy( color, g_color_table[ColorIndex( *( s + 1 ) )], sizeof( color ) ); + color[3] = setColor[3]; + trap_R_SetColor( color ); + } + s += 2; + continue; + } + CG_DrawChar2( xx, y, charWidth, charHeight, *s ); + xx += charWidth; + cnt++; + s++; + } + trap_R_SetColor( NULL ); +} + +/*================== +CG_DrawStringExt3 + +Draws a multi-colored string with a drop shadow, optionally forcing +to a fixed color. + +Coordinates are at 640 by 480 virtual resolution +================== +*/ +void CG_DrawStringExt3( int x, int y, const char *string, const float *setColor, + qboolean forceColor, qboolean shadow, int charWidth, int charHeight, int maxChars ) { + vec4_t color; + const unsigned char *s; + int xx; + int cnt; + + if ( maxChars <= 0 ) { + maxChars = 32767; // do them all! + + } + s = string; + xx = 0; + + while ( *s ) { + xx += charWidth; + s++; + } + + x -= xx; + + s = string; + xx = x; + + // draw the drop shadow + if ( shadow ) { + color[0] = color[1] = color[2] = 0; + color[3] = setColor[3]; + trap_R_SetColor( color ); + s = string; + xx = x; + cnt = 0; + while ( *s && cnt < maxChars ) { + if ( Q_IsColorString( s ) ) { + s += 2; + continue; + } + CG_DrawChar2( xx + 2, y + 2, charWidth, charHeight, *s ); + cnt++; + xx += charWidth; + s++; + } + } + + // draw the colored text + s = string; + xx = x; + cnt = 0; + trap_R_SetColor( setColor ); + while ( *s && cnt < maxChars ) { + if ( Q_IsColorString( s ) ) { + if ( !forceColor ) { + memcpy( color, g_color_table[ColorIndex( *( s + 1 ) )], sizeof( color ) ); + color[3] = setColor[3]; + trap_R_SetColor( color ); + } + s += 2; + continue; + } + CG_DrawChar2( xx, y, charWidth, charHeight, *s ); + xx += charWidth; + cnt++; + s++; + } + trap_R_SetColor( NULL ); +} + +/* +================== +CG_DrawStringExt2 + +Draws a multi-colored string with a drop shadow, optionally forcing +to a fixed color. + +Coordinates are at 640 by 480 virtual resolution +================== +*/ +/*void CG_DrawStringExt2( int x, int y, const char *string, const float *setColor, + qboolean forceColor, qboolean shadow, int charWidth, int charHeight, int maxChars ) { + vec4_t color; + const char *s; + int xx; + int cnt; + + if (maxChars <= 0) + maxChars = 32767; // do them all! + + // draw the drop shadow + if (shadow) { + color[0] = color[1] = color[2] = 0; + color[3] = setColor[3]; + trap_R_SetColor( color ); + s = string; + xx = x; + cnt = 0; + while ( *s && cnt < maxChars) { + if ( Q_IsColorString( s ) ) { + s += 2; + continue; + } + CG_DrawChar2( xx + 2, y + 2, charWidth, charHeight, *s ); + cnt++; + xx += charWidth; + s++; + } + } + + // draw the colored text + s = string; + xx = x; + cnt = 0; + trap_R_SetColor( setColor ); + while ( *s && cnt < maxChars) { + if ( Q_IsColorString( s ) ) { + if ( !forceColor ) { + memcpy( color, g_color_table[ColorIndex(*(s+1))], sizeof( color ) ); + color[3] = setColor[3]; + trap_R_SetColor( color ); + } + s += 2; + continue; + } + CG_DrawChar2( xx, y, charWidth, charHeight, *s ); + xx += charWidth; + cnt++; + s++; + } + trap_R_SetColor( NULL ); +}*/ + +void CG_DrawBigString( int x, int y, const char *s, float alpha ) { + float color[4]; + + color[0] = color[1] = color[2] = 1.0; + color[3] = alpha; + //CG_DrawStringExt( x, y, s, color, qfalse, qtrue, BIGCHAR_WIDTH, BIGCHAR_HEIGHT, 0 ); + CG_DrawStringExt2( x, y, s, color, qfalse, qtrue, BIGCHAR_WIDTH, BIGCHAR_HEIGHT, 0 ); +} + +void CG_DrawBigStringColor( int x, int y, const char *s, vec4_t color ) { + //CG_DrawStringExt( x, y, s, color, qtrue, qtrue, BIGCHAR_WIDTH, BIGCHAR_HEIGHT, 0 ); + CG_DrawStringExt2( x, y, s, color, qfalse, qtrue, BIGCHAR_WIDTH, BIGCHAR_HEIGHT, 0 ); +} +// END JOSEPH + +// JOSEPH 4-25-00 +void CG_DrawBigString2( int x, int y, const char *s, float alpha ) { + float color[4]; + + color[0] = color[1] = color[2] = 1.0; + color[3] = alpha; + CG_DrawStringExt3( x, y, s, color, qfalse, qtrue, BIGCHAR_WIDTH, BIGCHAR_HEIGHT, 0 ); +} + +void CG_DrawBigStringColor2( int x, int y, const char *s, vec4_t color ) { + CG_DrawStringExt3( x, y, s, color, qfalse, qtrue, BIGCHAR_WIDTH, BIGCHAR_HEIGHT, 0 ); +} +// END JOSEPH + +void CG_DrawSmallString( int x, int y, const char *s, float alpha ) { + float color[4]; + + color[0] = color[1] = color[2] = 1.0; + color[3] = alpha; + CG_DrawStringExt( x, y, s, color, qfalse, qfalse, SMALLCHAR_WIDTH, SMALLCHAR_HEIGHT, 0 ); +} + +void CG_DrawSmallStringColor( int x, int y, const char *s, vec4_t color ) { + CG_DrawStringExt( x, y, s, color, qtrue, qfalse, SMALLCHAR_WIDTH, SMALLCHAR_HEIGHT, 0 ); +} + +/* +================= +CG_DrawStrlen + +Returns character count, skiping color escape codes +================= +*/ +int CG_DrawStrlen( const char *str ) { + const char *s = str; + int count = 0; + + while ( *s ) { + if ( Q_IsColorString( s ) ) { + s += 2; + } else { + count++; + s++; + } + } + + return count; +} + +/* +============= +CG_TileClearBox + +This repeats a 64*64 tile graphic to fill the screen around a sized down +refresh window. +============= +*/ +static void CG_TileClearBox( int x, int y, int w, int h, qhandle_t hShader ) { + float s1, t1, s2, t2; + s1 = x / 64.0; + t1 = y / 64.0; + s2 = ( x + w ) / 64.0; + t2 = ( y + h ) / 64.0; + trap_R_DrawStretchPic( x, y, w, h, s1, t1, s2, t2, hShader ); +} + + + +/* +============== +CG_TileClear + +Clear around a sized down screen +============== +*/ +void CG_TileClear( void ) { + int top, bottom, left, right; + int w, h; + + w = cgs.glconfig.vidWidth; + h = cgs.glconfig.vidHeight; + + if ( cg.refdef.x == 0 && cg.refdef.y == 0 && + cg.refdef.width == w && cg.refdef.height == h ) { + return; // full screen rendering + } + + top = cg.refdef.y; + bottom = top + cg.refdef.height - 1; + left = cg.refdef.x; + right = left + cg.refdef.width - 1; + + // clear above view screen + CG_TileClearBox( 0, 0, w, top, cgs.media.backTileShader ); + + // clear below view screen + CG_TileClearBox( 0, bottom, w, h - bottom, cgs.media.backTileShader ); + + // clear left of view screen + CG_TileClearBox( 0, top, left, bottom - top + 1, cgs.media.backTileShader ); + + // clear right of view screen + CG_TileClearBox( right, top, w - right, bottom - top + 1, cgs.media.backTileShader ); +} + + + +/* +================ +CG_FadeColor +================ +*/ +float *CG_FadeColor( int startMsec, int totalMsec ) { + static vec4_t color; + int t; + + if ( startMsec == 0 ) { + return NULL; + } + + t = cg.time - startMsec; + + if ( t >= totalMsec ) { + return NULL; + } + + // fade out + if ( totalMsec - t < FADE_TIME ) { + color[3] = ( totalMsec - t ) * 1.0 / FADE_TIME; + } else { + color[3] = 1.0; + } + color[0] = color[1] = color[2] = 1; + + return color; +} + + +/* +================ +CG_TeamColor +================ +*/ +float *CG_TeamColor( int team ) { + static vec4_t red = {1, 0.2, 0.2, 1}; + static vec4_t blue = {0.2, 0.2, 1, 1}; + static vec4_t other = {1, 1, 1, 1}; + static vec4_t spectator = {0.7, 0.7, 0.7, 1}; + + switch ( team ) { + case TEAM_RED: + return red; + case TEAM_BLUE: + return blue; + case TEAM_SPECTATOR: + return spectator; + default: + return other; + } +} + + +/* +================= +CG_GetColorForHealth +================= +*/ +void CG_GetColorForHealth( int health, int armor, vec4_t hcolor ) { + int count; + int max; + + // calculate the total points of damage that can + // be sustained at the current health / armor level + if ( health <= 0 ) { + VectorClear( hcolor ); // black + hcolor[3] = 1; + return; + } + count = armor; + max = health * ARMOR_PROTECTION / ( 1.0 - ARMOR_PROTECTION ); + if ( max < count ) { + count = max; + } + health += count; + + // set the color based on health + hcolor[0] = 1.0; + hcolor[3] = 1.0; + if ( health >= 100 ) { + hcolor[2] = 1.0; + } else if ( health < 66 ) { + hcolor[2] = 0; + } else { + hcolor[2] = ( health - 66 ) / 33.0; + } + + if ( health > 60 ) { + hcolor[1] = 1.0; + } else if ( health < 30 ) { + hcolor[1] = 0; + } else { + hcolor[1] = ( health - 30 ) / 30.0; + } +} + +/* +================= +CG_ColorForHealth +================= +*/ +void CG_ColorForHealth( vec4_t hcolor ) { + int health; + int count; + int max; + + // calculate the total points of damage that can + // be sustained at the current health / armor level + health = cg.snap->ps.stats[STAT_HEALTH]; + if ( health <= 0 ) { + VectorClear( hcolor ); // black + hcolor[3] = 1; + return; + } + count = cg.snap->ps.stats[STAT_ARMOR]; + max = health * ARMOR_PROTECTION / ( 1.0 - ARMOR_PROTECTION ); + if ( max < count ) { + count = max; + } + health += count; + + + // set the color based on health + hcolor[0] = 1.0; + hcolor[3] = 1.0; + if ( health >= 100 ) { + hcolor[2] = 1.0; + } else if ( health < 66 ) { + hcolor[2] = 0; + } else { + hcolor[2] = ( health - 66 ) / 33.0; + } + + if ( health > 60 ) { + hcolor[1] = 1.0; + } else if ( health < 30 ) { + hcolor[1] = 0; + } else { + hcolor[1] = ( health - 30 ) / 30.0; + } +} + + + + +/* +================= +UI_DrawProportionalString2 +================= +*/ +static int propMap[128][3] = { + {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, + {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, + + {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, + {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, + + {0, 0, PROP_SPACE_WIDTH}, // SPACE + {11, 122, 7}, // ! + {154, 181, 14}, // " + {55, 122, 17}, // # + {79, 122, 18}, // $ + {101, 122, 23}, // % + {153, 122, 18}, // & + {9, 93, 7}, // ' + {207, 122, 8}, // ( + {230, 122, 9}, // ) + {177, 122, 18}, // * + {30, 152, 18}, // + + {85, 181, 7}, // , + {34, 93, 11}, // - + {110, 181, 6}, // . + {130, 152, 14}, // / + + {22, 64, 17}, // 0 + {41, 64, 12}, // 1 + {58, 64, 17}, // 2 + {78, 64, 18}, // 3 + {98, 64, 19}, // 4 + {120, 64, 18}, // 5 + {141, 64, 18}, // 6 + {204, 64, 16}, // 7 + {162, 64, 17}, // 8 + {182, 64, 18}, // 9 + {59, 181, 7}, // : + {35,181, 7}, // ; + {203, 152, 14}, // < + {56, 93, 14}, // = + {228, 152, 14}, // > + {177, 181, 18}, // ? + + {28, 122, 22}, // @ + {5, 4, 18}, // A + {27, 4, 18}, // B + {48, 4, 18}, // C + {69, 4, 17}, // D + {90, 4, 13}, // E + {106, 4, 13}, // F + {121, 4, 18}, // G + {143, 4, 17}, // H + {164, 4, 8}, // I + {175, 4, 16}, // J + {195, 4, 18}, // K + {216, 4, 12}, // L + {230, 4, 23}, // M + {6, 34, 18}, // N + {27, 34, 18}, // O + + {48, 34, 18}, // P + {68, 34, 18}, // Q + {90, 34, 17}, // R + {110, 34, 18}, // S + {130, 34, 14}, // T + {146, 34, 18}, // U + {166, 34, 19}, // V + {185, 34, 29}, // W + {215, 34, 18}, // X + {234, 34, 18}, // Y + {5, 64, 14}, // Z + {60, 152, 7}, // [ + {106, 151, 13}, // '\' + {83, 152, 7}, // ] + {128, 122, 17}, // ^ + {4, 152, 21}, // _ + + {134, 181, 5}, // ' + {5, 4, 18}, // A + {27, 4, 18}, // B + {48, 4, 18}, // C + {69, 4, 17}, // D + {90, 4, 13}, // E + {106, 4, 13}, // F + {121, 4, 18}, // G + {143, 4, 17}, // H + {164, 4, 8}, // I + {175, 4, 16}, // J + {195, 4, 18}, // K + {216, 4, 12}, // L + {230, 4, 23}, // M + {6, 34, 18}, // N + {27, 34, 18}, // O + + {48, 34, 18}, // P + {68, 34, 18}, // Q + {90, 34, 17}, // R + {110, 34, 18}, // S + {130, 34, 14}, // T + {146, 34, 18}, // U + {166, 34, 19}, // V + {185, 34, 29}, // W + {215, 34, 18}, // X + {234, 34, 18}, // Y + {5, 64, 14}, // Z + {153, 152, 13}, // { + {11, 181, 5}, // | + {180, 152, 13}, // } + {79, 93, 17}, // ~ + {0, 0, -1} // DEL +}; + +static int propMapB[26][3] = { + {11, 12, 33}, + {49, 12, 31}, + {85, 12, 31}, + {120, 12, 30}, + {156, 12, 21}, + {183, 12, 21}, + {207, 12, 32}, + + {13, 55, 30}, + {49, 55, 13}, + {66, 55, 29}, + {101, 55, 31}, + {135, 55, 21}, + {158, 55, 40}, + {204, 55, 32}, + + {12, 97, 31}, + {48, 97, 31}, + {82, 97, 30}, + {118, 97, 30}, + {153, 97, 30}, + {185, 97, 25}, + {213, 97, 30}, + + {11, 139, 32}, + {42, 139, 51}, + {93, 139, 32}, + {126, 139, 31}, + {158, 139, 25}, +}; + +#define PROPB_GAP_WIDTH 4 +#define PROPB_SPACE_WIDTH 12 +#define PROPB_HEIGHT 36 + +/* +================= +UI_DrawBannerString +================= +*/ +static void UI_DrawBannerString2( int x, int y, const char* str, vec4_t color ) { + const char* s; + unsigned char ch; + float ax; + float ay; + float aw; + float ah; + float frow; + float fcol; + float fwidth; + float fheight; + + // draw the colored text + trap_R_SetColor( color ); + + ax = x * cgs.screenXScale + cgs.screenXBias; + ay = y * cgs.screenXScale; + + s = str; + while ( *s ) + { + ch = *s & 127; + if ( ch == ' ' ) { + ax += ( (float)PROPB_SPACE_WIDTH + (float)PROPB_GAP_WIDTH ) * cgs.screenXScale; + } else if ( Q_isupper( ch ) ) { + ch -= 'A'; + fcol = (float)propMapB[ch][0] / 256.0f; + frow = (float)propMapB[ch][1] / 256.0f; + fwidth = (float)propMapB[ch][2] / 256.0f; + fheight = (float)PROPB_HEIGHT / 256.0f; + aw = (float)propMapB[ch][2] * cgs.screenXScale; + ah = (float)PROPB_HEIGHT * cgs.screenXScale; + trap_R_DrawStretchPic( ax, ay, aw, ah, fcol, frow, fcol + fwidth, frow + fheight, cgs.media.charsetPropB ); + ax += ( aw + (float)PROPB_GAP_WIDTH * cgs.screenXScale ); + } + s++; + } + + trap_R_SetColor( NULL ); +} + +void UI_DrawBannerString( int x, int y, const char* str, int style, vec4_t color ) { + const char * s; + int ch; + int width; + vec4_t drawcolor; + + // find the width of the drawn text + s = str; + width = 0; + while ( *s ) { + ch = *s; + if ( ch == ' ' ) { + width += PROPB_SPACE_WIDTH; + } else if ( ch >= 'A' && ch <= 'Z' ) { + width += propMapB[ch - 'A'][2] + PROPB_GAP_WIDTH; + } + s++; + } + width -= PROPB_GAP_WIDTH; + + switch ( style & UI_FORMATMASK ) { + case UI_CENTER: + x -= width / 2; + break; + + case UI_RIGHT: + x -= width; + break; + + case UI_LEFT: + default: + break; + } + + if ( style & UI_DROPSHADOW ) { + drawcolor[0] = drawcolor[1] = drawcolor[2] = 0; + drawcolor[3] = color[3]; + UI_DrawBannerString2( x + 2, y + 2, str, drawcolor ); + } + + UI_DrawBannerString2( x, y, str, color ); +} + + +int UI_ProportionalStringWidth( const char* str ) { + const char * s; + int ch; + int charWidth; + int width; + + s = str; + width = 0; + while ( *s ) { + ch = *s & 127; + charWidth = propMap[ch][2]; + if ( charWidth != -1 ) { + width += charWidth; + width += PROP_GAP_WIDTH; + } + s++; + } + + width -= PROP_GAP_WIDTH; + return width; +} + +static void UI_DrawProportionalString2( int x, int y, const char* str, vec4_t color, float sizeScale, qhandle_t charset ) { + const char* s; + unsigned char ch; + float ax; + float ay; + float aw; + float ah; + float frow; + float fcol; + float fwidth; + float fheight; + + // draw the colored text + trap_R_SetColor( color ); + + ax = x * cgs.screenXScale + cgs.screenXBias; + ay = y * cgs.screenXScale; + + s = str; + while ( *s ) + { + ch = *s & 127; + if ( ch == ' ' ) { + aw = (float)PROP_SPACE_WIDTH * cgs.screenXScale * sizeScale; + } else if ( propMap[ch][2] != -1 ) { + fcol = (float)propMap[ch][0] / 256.0f; + frow = (float)propMap[ch][1] / 256.0f; + fwidth = (float)propMap[ch][2] / 256.0f; + fheight = (float)PROP_HEIGHT / 256.0f; + aw = (float)propMap[ch][2] * cgs.screenXScale * sizeScale; + ah = (float)PROP_HEIGHT * cgs.screenXScale * sizeScale; + trap_R_DrawStretchPic( ax, ay, aw, ah, fcol, frow, fcol + fwidth, frow + fheight, charset ); + } else { + aw = 0; + } + + ax += ( aw + (float)PROP_GAP_WIDTH * cgs.screenXScale * sizeScale ); + s++; + } + + trap_R_SetColor( NULL ); +} + +/* +================= +UI_ProportionalSizeScale +================= +*/ +float UI_ProportionalSizeScale( int style ) { + if ( style & UI_SMALLFONT ) { + return 0.75; + } + if ( style & UI_EXSMALLFONT ) { + return 0.4; + } + + return 1.00; +} + + +/* +================= +UI_DrawProportionalString +================= +*/ +void UI_DrawProportionalString( int x, int y, const char* str, int style, vec4_t color ) { + vec4_t drawcolor; + int width; + float sizeScale; + + sizeScale = UI_ProportionalSizeScale( style ); + + switch ( style & UI_FORMATMASK ) { + case UI_CENTER: + width = UI_ProportionalStringWidth( str ) * sizeScale; + x -= width / 2; + break; + + case UI_RIGHT: + width = UI_ProportionalStringWidth( str ) * sizeScale; + x -= width; + break; + + case UI_LEFT: + default: + break; + } + + if ( style & UI_DROPSHADOW ) { + drawcolor[0] = drawcolor[1] = drawcolor[2] = 0; + drawcolor[3] = color[3]; + UI_DrawProportionalString2( x + 2, y + 2, str, drawcolor, sizeScale, cgs.media.charsetProp ); + } + + if ( style & UI_INVERSE ) { + drawcolor[0] = color[0] * 0.8; + drawcolor[1] = color[1] * 0.8; + drawcolor[2] = color[2] * 0.8; + drawcolor[3] = color[3]; + UI_DrawProportionalString2( x, y, str, drawcolor, sizeScale, cgs.media.charsetProp ); + return; + } + + // JOSEPH 12-29-99 + if ( style & UI_PULSE ) { + //drawcolor[0] = color[0] * 0.8; + //drawcolor[1] = color[1] * 0.8; + //drawcolor[2] = color[2] * 0.8; + drawcolor[3] = color[3]; + UI_DrawProportionalString2( x, y, str, color, sizeScale, cgs.media.charsetProp ); + + drawcolor[0] = color[0]; + drawcolor[1] = color[1]; + drawcolor[2] = color[2]; + drawcolor[3] = 0.5 + 0.5 * sin( cg.time / PULSE_DIVISOR ); + UI_DrawProportionalString2( x, y, str, drawcolor, sizeScale, cgs.media.charsetPropGlow ); + return; + } + // END JOSEPH + + UI_DrawProportionalString2( x, y, str, color, sizeScale, cgs.media.charsetProp ); +} + + + + + + + + + + + + + + + diff --git a/src/cgame/cg_effects.c b/src/cgame/cg_effects.c new file mode 100644 index 0000000..65edb62 --- /dev/null +++ b/src/cgame/cg_effects.c @@ -0,0 +1,1708 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// cg_effects.c -- these functions generate localentities, usually as a result +// of event processing + +#include "cg_local.h" + + +/* +================== +CG_BubbleTrail + +Bullets shot underwater +================== +*/ +void CG_BubbleTrail( vec3_t start, vec3_t end, float size, float spacing ) { + vec3_t move; + vec3_t vec; + float len; + int i; + + return; + + VectorCopy( start, move ); + VectorSubtract( end, start, vec ); + len = VectorNormalize( vec ); + + // advance a random amount first + i = rand() % (int)spacing; + VectorMA( move, i, vec, move ); + + VectorScale( vec, spacing, vec ); + + for ( ; i < len; i += spacing ) { + localEntity_t *le; + refEntity_t *re; + + le = CG_AllocLocalEntity(); + le->leFlags = LEF_PUFF_DONT_SCALE; + le->leType = LE_MOVE_SCALE_FADE; + le->startTime = cg.time; + le->endTime = cg.time + 1000 + random() * 250; + le->lifeRate = 1.0 / ( le->endTime - le->startTime ); + + re = &le->refEntity; + re->shaderTime = cg.time / 1000.0f; + + re->reType = RT_SPRITE; + re->rotation = 0; +// re->radius = 3; + re->radius = size; // (SA) + re->customShader = cgs.media.waterBubbleShader; + re->shaderRGBA[0] = 0xff; + re->shaderRGBA[1] = 0xff; + re->shaderRGBA[2] = 0xff; + re->shaderRGBA[3] = 0xff; + + le->color[3] = 1.0; + + le->pos.trType = TR_LINEAR; + le->pos.trTime = cg.time; + VectorCopy( move, le->pos.trBase ); + le->pos.trDelta[0] = crandom() * 3; + le->pos.trDelta[1] = crandom() * 3; +// le->pos.trDelta[2] = crandom()*5 + 6; + le->pos.trDelta[2] = crandom() * 5 + 20; // (SA) + + VectorAdd( move, vec, move ); + } +} + +/* +===================== +CG_SmokePuff + +Adds a smoke puff or blood trail localEntity. + +(SA) boy, it would be nice to have an acceleration vector for this as well. + big velocity vector with a negative acceleration for deceleration, etc. + (breath could then come out of a guys mouth at the rate he's walking/running and it + would slow down once it's created) +===================== +*/ + +//----(SA) modified +localEntity_t *CG_SmokePuff( const vec3_t p, const vec3_t vel, + float radius, + float r, float g, float b, float a, + float duration, + int startTime, + int fadeInTime, + int leFlags, + qhandle_t hShader ) { + static int seed = 0x92; + localEntity_t *le; + refEntity_t *re; + + le = CG_AllocLocalEntity(); + le->leFlags = leFlags; + le->radius = radius; + + re = &le->refEntity; + re->rotation = Q_random( &seed ) * 360; + re->radius = radius; + re->shaderTime = startTime / 1000.0f; + + le->leType = LE_MOVE_SCALE_FADE; + le->startTime = startTime; + le->endTime = startTime + duration; + le->fadeInTime = fadeInTime; + if ( fadeInTime > startTime ) { + le->lifeRate = 1.0 / ( le->endTime - le->fadeInTime ); + } else { + le->lifeRate = 1.0 / ( le->endTime - le->startTime ); + } + le->color[0] = r; + le->color[1] = g; + le->color[2] = b; + le->color[3] = a; + + + le->pos.trType = TR_LINEAR; + le->pos.trTime = startTime; + VectorCopy( vel, le->pos.trDelta ); + VectorCopy( p, le->pos.trBase ); + + VectorCopy( p, re->origin ); + re->customShader = hShader; + + // rage pro can't alpha fade, so use a different shader + if ( cgs.glconfig.hardwareType == GLHW_RAGEPRO ) { + re->customShader = cgs.media.smokePuffRageProShader; + re->shaderRGBA[0] = 0xff; + re->shaderRGBA[1] = 0xff; + re->shaderRGBA[2] = 0xff; + re->shaderRGBA[3] = 0xff; + } else { + re->shaderRGBA[0] = le->color[0] * 0xff; + re->shaderRGBA[1] = le->color[1] * 0xff; + re->shaderRGBA[2] = le->color[2] * 0xff; + re->shaderRGBA[3] = 0xff; + } + + re->reType = RT_SPRITE; + re->radius = le->radius; + + return le; +} + +/* +================== +CG_SpawnEffect + +Player teleporting in or out +================== +*/ +void CG_SpawnEffect( vec3_t org ) { + localEntity_t *le; + refEntity_t *re; + + return; // (SA) don't play spawn in effect right now + + le = CG_AllocLocalEntity(); + le->leFlags = 0; + le->leType = LE_FADE_RGB; + le->startTime = cg.time; + le->endTime = cg.time + 500; + le->lifeRate = 1.0 / ( le->endTime - le->startTime ); + + le->color[0] = le->color[1] = le->color[2] = le->color[3] = 1.0; + + re = &le->refEntity; + + re->reType = RT_MODEL; + re->shaderTime = cg.time / 1000.0f; + + re->customShader = cgs.media.teleportEffectShader; + re->hModel = cgs.media.teleportEffectModel; + AxisClear( re->axis ); + + VectorCopy( org, re->origin ); + re->origin[2] -= 24; +} + + + + +/* +==================== +CG_MakeExplosion +==================== +*/ +localEntity_t *CG_MakeExplosion( vec3_t origin, vec3_t dir, + qhandle_t hModel, qhandle_t shader, + int msec, qboolean isSprite ) { + float ang; + localEntity_t *ex; + int offset; + vec3_t tmpVec, newOrigin; + + if ( msec <= 0 ) { + CG_Error( "CG_MakeExplosion: msec = %i", msec ); + } + + // skew the time a bit so they aren't all in sync + offset = rand() & 63; + + ex = CG_AllocLocalEntity(); + if ( isSprite ) { + ex->leType = LE_SPRITE_EXPLOSION; + + // randomly rotate sprite orientation + ex->refEntity.rotation = rand() % 360; + VectorScale( dir, 16, tmpVec ); + VectorAdd( tmpVec, origin, newOrigin ); + } else { + ex->leType = LE_EXPLOSION; + VectorCopy( origin, newOrigin ); + + // set axis with random rotate + if ( !dir ) { + AxisClear( ex->refEntity.axis ); + } else { + ang = rand() % 360; + VectorCopy( dir, ex->refEntity.axis[0] ); + RotateAroundDirection( ex->refEntity.axis, ang ); + } + } + + ex->startTime = cg.time - offset; + ex->endTime = ex->startTime + msec; + + // bias the time so all shader effects start correctly + ex->refEntity.shaderTime = ex->startTime / 1000.0f; + + ex->refEntity.hModel = hModel; + ex->refEntity.customShader = shader; + + // set origin + VectorCopy( newOrigin, ex->refEntity.origin ); + VectorCopy( newOrigin, ex->refEntity.oldorigin ); + + // Ridah, move away from the wall as the sprite expands + ex->pos.trType = TR_LINEAR; + ex->pos.trTime = cg.time; + VectorCopy( newOrigin, ex->pos.trBase ); + VectorScale( dir, 48, ex->pos.trDelta ); + // done. + + ex->color[0] = ex->color[1] = ex->color[2] = 1.0; + + return ex; +} + +/* +================= +CG_AddBloodTrails +================= +*/ +void CG_AddBloodTrails( vec3_t origin, vec3_t dir, int speed, int duration, int count, float randScale ) { + localEntity_t *le; + refEntity_t *re; + vec3_t velocity; + int i; + + for ( i = 0; i < count; i++ ) { + le = CG_AllocLocalEntity(); + re = &le->refEntity; + + VectorSet( velocity, dir[0] + crandom() * randScale, dir[1] + crandom() * randScale, dir[2] + crandom() * randScale ); + VectorScale( velocity, (float)speed, velocity ); + + le->leType = LE_BLOOD; + le->startTime = cg.time; + le->endTime = le->startTime + duration - (int)( 0.5 * random() * duration ); + le->lastTrailTime = cg.time; + + VectorCopy( origin, re->origin ); + AxisCopy( axisDefault, re->axis ); + + le->pos.trType = TR_GRAVITY_LOW; + VectorCopy( origin, le->pos.trBase ); + VectorMA( le->pos.trBase, 2 + random() * 4, dir, le->pos.trBase ); + VectorCopy( velocity, le->pos.trDelta ); + le->pos.trTime = cg.time; + + le->bounceFactor = 0.9; + } +} + +/* +================= +CG_Bleed + +This is the spurt of blood when a character gets hit +================= +*/ +void CG_Bleed( vec3_t origin, int entityNum ) { +#define BLOOD_SPURT_COUNT 4 + int i,j; +// localEntity_t *ex; + centity_t *cent; +// vec3_t dir; + + if ( !cg_blood.integer ) { + return; + } + + if ( cg_reloading.integer ) { + return; // to dangerous, since we call playerangles() in here, which calls the animation system, which might not be setup yet + } + + cent = &cg_entities[entityNum]; + + if ( cent->currentState.aiChar == AICHAR_ZOMBIE ) { + CG_ParticleBloodCloudZombie( cent, origin, vec3_origin ); + return; + } + +/* + ex = CG_AllocLocalEntity(); + ex->leType = LE_EXPLOSION; + + ex->startTime = cg.time; + ex->endTime = ex->startTime + 500; + + VectorCopy ( origin, ex->refEntity.origin); + ex->refEntity.reType = RT_SPRITE; + ex->refEntity.rotation = rand() % 360; + ex->refEntity.radius = 3; + + ex->refEntity.customShader = cgs.media.bloodExplosionShader; + + // don't show player's own blood in view + if ( entityNum == cg.snap->ps.clientNum ) { + ex->refEntity.renderfx |= RF_THIRD_PERSON; + } +*/ + // Ridah, blood spurts + if ( entityNum != cg.snap->ps.clientNum ) { + vec3_t vhead, vlegs, vtorso, bOrigin, dir, vec, pvec, ndir; + + CG_GetBleedOrigin( vhead, vtorso, vlegs, entityNum ); + + // project the impact point onto the vector defined by torso -> head + ProjectPointOntoVector( origin, vtorso, vhead, bOrigin ); + + // if it's below the waste, or above the head, clamp + VectorSubtract( vhead, vtorso, vec ); + VectorSubtract( bOrigin, vtorso, pvec ); + if ( DotProduct( pvec, vec ) < 0 ) { + VectorCopy( vtorso, bOrigin ); + } else { + VectorSubtract( bOrigin, vhead, pvec ); + if ( DotProduct( pvec, vec ) > 0 ) { + VectorCopy( vhead, bOrigin ); + } + } + + // spawn some blood trails, heading out towards the impact point + VectorSubtract( origin, bOrigin, dir ); + VectorNormalize( dir ); + + { + float len; + vec3_t vec; + + VectorSubtract( bOrigin, vhead, vec ); + len = VectorLength( vec ); + + if ( len > 8 ) { + VectorMA( bOrigin, 8, dir, bOrigin ); + } + } + + + for ( i = 0; i < BLOOD_SPURT_COUNT; i++ ) { + VectorCopy( dir, ndir ); + for ( j = 0; j < 3; j++ ) ndir[j] += crandom() * 0.3; + VectorNormalize( ndir ); + CG_AddBloodTrails( bOrigin, ndir, + 100, // speed + 250 + (int)( crandom() * 50 ), // duration + 3 + rand() % 2, // count + 0.1 ); // rand scale + } + + } + // done. +} + + + +/* +================== +CG_LaunchGib +================== +*/ +void CG_LaunchGib( centity_t *cent, vec3_t origin, vec3_t angles, vec3_t velocity, qhandle_t hModel, float sizeScale, int breakCount ) { + localEntity_t *le; + refEntity_t *re; + int i; + + le = CG_AllocLocalEntity(); + re = &le->refEntity; + + le->leType = LE_FRAGMENT; + le->startTime = cg.time; + // le->endTime = le->startTime + 60000 + random() * 60000; + le->endTime = le->startTime + 20000 + ( crandom() * 5000 ); + le->breakCount = breakCount; + le->sizeScale = sizeScale; + + VectorCopy( angles, le->angles.trBase ); + VectorCopy( origin, re->origin ); + AnglesToAxis( angles, re->axis ); + if ( sizeScale != 1.0 ) { + for ( i = 0; i < 3; i++ ) VectorScale( re->axis[i], sizeScale, re->axis[i] ); + } + re->hModel = hModel; + + switch ( cent->currentState.aiChar ) { + case AICHAR_ZOMBIE: + le->pos.trType = TR_GRAVITY_LOW; + le->angles.trDelta[0] = 400 * crandom(); + le->angles.trDelta[1] = 400 * crandom(); + le->angles.trDelta[2] = 400 * crandom(); + + le->leBounceSoundType = LEBS_BONE; + + le->bounceFactor = 0.5; + break; + case AICHAR_HEINRICH: + case AICHAR_HELGA: + le->endTime = le->startTime + 999000; // stay around for long enough to see the player off + default: + le->leBounceSoundType = LEBS_BLOOD; + le->leMarkType = LEMT_BLOOD; + le->pos.trType = TR_GRAVITY; + + le->angles.trDelta[0] = ( 10 + ( rand() & 50 ) ) - 30; + // le->angles.trDelta[0] = (100 + (rand()&500)) - 300; // pitch + le->angles.trDelta[1] = ( 100 + ( rand() & 500 ) ) - 300; // (SA) this is the safe one right now (yaw) turn the others up when I have tumbling things landing properly + le->angles.trDelta[2] = ( 10 + ( rand() & 50 ) ) - 30; + // le->angles.trDelta[2] = (100 + (rand()&500)) - 300; // roll + + le->bounceFactor = 0.3; + break; + } + + if ( cent->currentState.aiChar == AICHAR_HELGA || cent->currentState.aiChar == AICHAR_HEINRICH ) { + // bit bouncier + //le->pos.trType = TR_GRAVITY_LOW; + le->bounceFactor = 0.4; + //if (VectorLength( velocity ) < 300) { + // VectorScale( velocity, 300.0 / VectorLength( velocity ), velocity ); + //} + } + + VectorCopy( origin, le->pos.trBase ); + VectorCopy( velocity, le->pos.trDelta ); + le->pos.trTime = cg.time; + + re->fadeStartTime = le->endTime - 1000; + re->fadeEndTime = le->endTime; + + le->angles.trType = TR_LINEAR; + + le->angles.trTime = cg.time; + + le->ownerNum = cent->currentState.number; + + // Ridah, if the player is on fire, then spawn some flaming gibs + if ( cent && CG_EntOnFire( cent ) ) { + le->onFireStart = cent->currentState.onFireStart; + le->onFireEnd = re->fadeEndTime + 1000; + } else if ( ( cent->currentState.aiChar == AICHAR_ZOMBIE ) && IS_FLAMING_ZOMBIE( cent->currentState ) ) { + le->onFireStart = cg.time - 1000; + le->onFireEnd = re->fadeEndTime + 1000; + } +} + +//#define GIB_VELOCITY 250 +//#define GIB_JUMP 250 + +#define GIB_VELOCITY 75 +#define GIB_JUMP 250 + + +/* +============== +CG_LoseHat +============== +*/ +void CG_LoseHat( centity_t *cent, vec3_t dir ) { + clientInfo_t *ci; + int clientNum; +// int i, count, tagIndex, gibIndex; + int tagIndex; + vec3_t origin, velocity; + + clientNum = cent->currentState.clientNum; + if ( clientNum < 0 || clientNum >= MAX_CLIENTS ) { + CG_Error( "Bad clientNum on player entity" ); + } + ci = &cgs.clientinfo[ clientNum ]; + + if ( !ci->accModels[ACC_HAT] ) { // don't launch anything if they don't have one + return; + } + + tagIndex = CG_GetOriginForTag( cent, ¢->pe.headRefEnt, "tag_mouth", 0, origin, NULL ); + + velocity[0] = dir[0] * ( 0.75 + random() ) * GIB_VELOCITY; + velocity[1] = dir[1] * ( 0.75 + random() ) * GIB_VELOCITY; + velocity[2] = GIB_JUMP - 50 + dir[2] * ( 0.5 + random() ) * GIB_VELOCITY; + + { + localEntity_t *le; + refEntity_t *re; + + le = CG_AllocLocalEntity(); + re = &le->refEntity; + + le->leType = LE_FRAGMENT; + le->startTime = cg.time; + le->endTime = le->startTime + 20000 + ( crandom() * 5000 ); + + VectorCopy( origin, re->origin ); + AxisCopy( axisDefault, re->axis ); + re->hModel = ci->accModels[ACC_HAT]; + + re->fadeStartTime = le->endTime - 1000; + re->fadeEndTime = le->endTime; + + // (SA) FIXME: origin of hat md3 is offset from center. need to center the origin when you toss it + le->pos.trType = TR_GRAVITY; + VectorCopy( origin, le->pos.trBase ); + VectorCopy( velocity, le->pos.trDelta ); + le->pos.trTime = cg.time; + + // spin it a bit + le->angles.trType = TR_LINEAR; + VectorCopy( tv( 0, 0, 0 ), le->angles.trBase ); + le->angles.trDelta[0] = 0; + le->angles.trDelta[1] = ( 100 + ( rand() & 500 ) ) - 300; +// le->angles.trDelta[2] = 0; + le->angles.trDelta[2] = 400; // (SA) this is set with a very particular value to try to get it + // to flip exactly once before landing (based on player alive + // (standing) and on level ground) and will be unnecessary when + // I have things landing properly on their own + + le->angles.trTime = cg.time; + + le->bounceFactor = 0.2; + + // Ridah, if the player is on fire, then make the hat on fire + if ( cent && CG_EntOnFire( cent ) ) { + le->onFireStart = cent->currentState.onFireStart; + le->onFireEnd = cent->currentState.onFireEnd + 4000; + } + } +} +/* +============== +CG_GibHead + + FIXME: accept the cent as parameter, and use it to grab the head position + from the model TAG's +============== +*/ +void CG_GibHead( vec3_t headOrigin ) { + vec3_t origin, velocity; + + VectorCopy( headOrigin, origin ); + velocity[0] = crandom() * GIB_VELOCITY; + velocity[1] = crandom() * GIB_VELOCITY; + velocity[2] = GIB_JUMP + crandom() * GIB_VELOCITY; + CG_LaunchGib( NULL, origin, vec3_origin, velocity, cgs.media.gibSkull, 1.0, 0 ); + + VectorCopy( headOrigin, origin ); + velocity[0] = crandom() * GIB_VELOCITY; + velocity[1] = crandom() * GIB_VELOCITY; + velocity[2] = GIB_JUMP + crandom() * GIB_VELOCITY; + CG_LaunchGib( NULL, origin, vec3_origin, velocity, cgs.media.gibBrain, 1.0, 0 ); + +} + +/* +====================== +CG_GetOriginForTag + + places the position of the tag into "org" + + returns the index of the tag it used, so we can cycle through tag's with the same name +====================== +*/ +int CG_GetOriginForTag( centity_t *cent, refEntity_t *parent, char *tagName, int startIndex, vec3_t org, vec3_t axis[3] ) { + int i; + orientation_t lerped; + int retval; + + // lerp the tag + retval = trap_R_LerpTag( &lerped, parent, tagName, startIndex ); + + if ( retval < 0 ) { + return retval; + } + + VectorCopy( parent->origin, org ); + + for ( i = 0 ; i < 3 ; i++ ) { + VectorMA( org, lerped.origin[i], parent->axis[i], org ); + } + + if ( axis ) { + // had to cast away the const to avoid compiler problems... + MatrixMultiply( lerped.axis, ( (refEntity_t *)parent )->axis, axis ); + } + + return retval; +} + +/* +=================== +CG_GibPlayer + +Generated a bunch of gibs launching out from the bodies location +=================== +*/ +#define MAXJUNCTIONS 8 + +void CG_GibPlayer( centity_t *cent, vec3_t playerOrigin, vec3_t gdir ) { + vec3_t origin, velocity, dir; + int i, count, tagIndex, gibIndex; + trace_t trace; + qboolean foundtag; + + clientInfo_t *ci; + int clientNum; + + // Rafael + // BloodCloud + qboolean newjunction[MAXJUNCTIONS]; + vec3_t junctionOrigin[MAXJUNCTIONS]; + int junction; + int j; + float size; + vec3_t axis[3], angles; + + char *JunctiongibTags[] = { + // leg tag + "tag_footright", + "tag_footleft", + "tag_legright", + "tag_legleft", + + // torsotags + "tag_armright", + "tag_armleft", + + "tag_torso", + "tag_chest" + }; + + char *ConnectTags[] = { + // legs tags + "tag_legright", + "tag_legleft", + "tag_torso", + "tag_torso", + + // torso tags + "tag_chest", + "tag_chest", + + "tag_chest", + "tag_torso", + }; + + // FIXME: let them specify tag & model in the cfg file? + + // (SA) and perhaps which body part that tag is attached to? + + char *gibTags[] = { + // tags in the legs + "tag_footright", + "tag_footleft", + "tag_legright", + "tag_legleft", + "tag_torso", + + // tags in the torso + "tag_chest", + "tag_armright", + "tag_armleft", + "tag_head", + NULL + }; + +#define TORSOTAGSSTART 5 // where the 'split' happens in the above table + + + // Rafael + for ( i = 0; i < MAXJUNCTIONS; i++ ) + newjunction[i] = qfalse; + + clientNum = cent->currentState.clientNum; + if ( clientNum < 0 || clientNum >= MAX_CLIENTS ) { + CG_Error( "Bad clientNum on player entity" ); + } + ci = &cgs.clientinfo[ clientNum ]; + + // Ridah, fetch the various positions of the tag_gib*'s + // and spawn the gibs from the correct places (especially the head) + for ( gibIndex = 0, count = 0, foundtag = qtrue; foundtag && gibIndex < MAX_GIB_MODELS && gibTags[gibIndex]; gibIndex++ ) { + + refEntity_t *re = 0; + + foundtag = qfalse; + + if ( !ci->gibModels[gibIndex] ) { + continue; + } + + // (SA) split this into torso and legs, since the tags are only attached to the appropriate part +// if(gibIndex >= TORSOTAGSSTART) + re = ¢->pe.torsoRefEnt; +// else +// re = ¢->pe.legsRefEnt; + + for ( tagIndex = 0; ( tagIndex = CG_GetOriginForTag( cent, re, gibTags[gibIndex], tagIndex, origin, axis ) ) >= 0; count++, tagIndex++ ) { + + foundtag = qtrue; + + VectorSubtract( origin, re->origin, dir ); + VectorNormalize( dir ); + + // spawn a gib + velocity[0] = dir[0] * ( 0.5 + random() ) * GIB_VELOCITY * 0.3; + velocity[1] = dir[1] * ( 0.5 + random() ) * GIB_VELOCITY * 0.3; + velocity[2] = GIB_JUMP + dir[2] * ( 0.5 + random() ) * GIB_VELOCITY * 0.5; + + VectorMA( velocity, GIB_VELOCITY, gdir, velocity ); + + AxisToAngles( axis, angles ); + + // RF, Zombies dying by particle effect dont spawn gibs + if ( ( cent->currentState.aiChar == AICHAR_ZOMBIE ) || + ( cent->currentState.aiChar == AICHAR_HELGA ) || + ( cent->currentState.aiChar == AICHAR_HEINRICH ) ) { + //VectorScale( velocity, 4, velocity ); + size = 0.6 + 0.4 * random(); + if ( ( cent->currentState.aiChar == AICHAR_HELGA ) || ( cent->currentState.aiChar == AICHAR_HEINRICH ) ) { + //size *= 3.0; + velocity[0] = crandom() * GIB_VELOCITY * 1.0; + velocity[1] = crandom() * GIB_VELOCITY * 1.0; + velocity[2] = GIB_JUMP + crandom() * GIB_VELOCITY; + // additional gibs + CG_LaunchGib( cent, origin, angles, velocity, ci->gibModels[gibIndex], size, 1 ); + CG_LaunchGib( cent, origin, angles, velocity, ci->gibModels[gibIndex], size, 1 ); + } else { + CG_LaunchGib( cent, origin, angles, velocity, ci->gibModels[gibIndex], size, 1 + (int)( 2.0 * ( size - 0.4 ) ) ); + } + } else { + CG_LaunchGib( cent, origin, angles, velocity, ci->gibModels[gibIndex], 1.0, 0 ); + } + + for ( junction = 0; junction < MAXJUNCTIONS; junction++ ) + { + if ( !Q_stricmp( gibTags[gibIndex], JunctiongibTags[junction] ) ) { + VectorCopy( origin, junctionOrigin[junction] ); + newjunction[junction] = qtrue; + } + } + } + } + + + for ( i = 0; i < MAXJUNCTIONS; i++ ) + { + if ( newjunction[i] == qtrue ) { + for ( j = 0; j < MAXJUNCTIONS; j++ ) + { + if ( !Q_stricmp( JunctiongibTags[j], ConnectTags[i] ) ) { + if ( newjunction[j] == qtrue ) { + // spawn a blood cloud somewhere on the vec from + VectorSubtract( junctionOrigin[i], junctionOrigin[j], dir ); + + // ok now lets spawn a little blood + if ( cent->currentState.aiChar == AICHAR_ZOMBIE ) { + CG_ParticleBloodCloudZombie( cent, junctionOrigin[i], dir ); + } else { + CG_ParticleBloodCloud( cent, junctionOrigin[i], dir ); + } + + // RF, also spawn some blood in this direction + VectorMA( junctionOrigin[i], 2.0, dir, origin ); + if ( cent->currentState.aiChar == AICHAR_ZOMBIE ) { + CG_ParticleBloodCloudZombie( cent, origin, dir ); + } else { + CG_ParticleBloodCloud( cent, origin, dir ); + } + + // Zombies spawn more bones + if ( ( cent->currentState.aiChar == AICHAR_ZOMBIE ) || + ( cent->currentState.aiChar == AICHAR_HEINRICH ) || + ( cent->currentState.aiChar == AICHAR_HELGA ) ) { + // spawn a gib + VectorCopy( junctionOrigin[i], origin ); + + if ( ( cent->currentState.aiChar == AICHAR_HELGA ) || ( cent->currentState.aiChar == AICHAR_HEINRICH ) ) { + size *= 3.0; + velocity[0] = crandom() * GIB_VELOCITY * 2.0; + velocity[1] = crandom() * GIB_VELOCITY * 2.0; + velocity[2] = GIB_JUMP + random() * GIB_VELOCITY; + } else { + velocity[0] = dir[0] * ( 0.5 + random() ) * GIB_VELOCITY * 0.3; + velocity[1] = dir[1] * ( 0.5 + random() ) * GIB_VELOCITY * 0.3; + velocity[2] = GIB_JUMP + dir[2] * ( 0.5 + random() ) * GIB_VELOCITY * 0.5; + VectorMA( velocity, GIB_VELOCITY, gdir, velocity ); + } + + vectoangles( dir, angles ); + + VectorScale( velocity, 3, velocity ); + size = 0.6 + 0.4 * random(); + if ( ( cent->currentState.aiChar == AICHAR_HELGA ) || ( cent->currentState.aiChar == AICHAR_HEINRICH ) ) { + CG_LaunchGib( cent, origin, angles, velocity, ci->gibModels[rand() % 4], size, 1 ); + } else { + CG_LaunchGib( cent, origin, angles, velocity, ci->gibModels[rand() % 4], size, 1 + (int)( 2.0 * ( size - 0.4 ) ) ); + } + } + } + } + } + } + } + + if ( !count ) { + + // CG_Printf("falling back to old-style gibs\n"); + + // old style gibs + + VectorCopy( playerOrigin, origin ); + velocity[0] = crandom() * GIB_VELOCITY; + velocity[1] = crandom() * GIB_VELOCITY; + velocity[2] = GIB_JUMP + crandom() * GIB_VELOCITY; + if ( rand() & 1 ) { + CG_LaunchGib( cent, origin, vec3_origin, velocity, cgs.media.gibSkull, 1.0, 0 ); + } else { + CG_LaunchGib( cent, origin, vec3_origin, velocity, cgs.media.gibBrain, 1.0, 0 ); + } + + // allow gibs to be turned off for speed + if ( !cg_gibs.integer ) { + return; + } + + VectorCopy( playerOrigin, origin ); + velocity[0] = crandom() * GIB_VELOCITY; + velocity[1] = crandom() * GIB_VELOCITY; + velocity[2] = GIB_JUMP + crandom() * GIB_VELOCITY; + CG_LaunchGib( cent, origin, vec3_origin, velocity, cgs.media.gibAbdomen, 1.0, 0 ); + + VectorCopy( playerOrigin, origin ); + velocity[0] = crandom() * GIB_VELOCITY; + velocity[1] = crandom() * GIB_VELOCITY; + velocity[2] = GIB_JUMP + crandom() * GIB_VELOCITY; + CG_LaunchGib( cent, origin, vec3_origin, velocity, cgs.media.gibArm, 1.0, 0 ); + + VectorCopy( playerOrigin, origin ); + velocity[0] = crandom() * GIB_VELOCITY; + velocity[1] = crandom() * GIB_VELOCITY; + velocity[2] = GIB_JUMP + crandom() * GIB_VELOCITY; + CG_LaunchGib( cent, origin, vec3_origin, velocity, cgs.media.gibChest, 1.0, 0 ); + + VectorCopy( playerOrigin, origin ); + velocity[0] = crandom() * GIB_VELOCITY; + velocity[1] = crandom() * GIB_VELOCITY; + velocity[2] = GIB_JUMP + crandom() * GIB_VELOCITY; + CG_LaunchGib( cent, origin, vec3_origin, velocity, cgs.media.gibFist, 1.0, 0 ); + + VectorCopy( playerOrigin, origin ); + velocity[0] = crandom() * GIB_VELOCITY; + velocity[1] = crandom() * GIB_VELOCITY; + velocity[2] = GIB_JUMP + crandom() * GIB_VELOCITY; + CG_LaunchGib( cent, origin, vec3_origin, velocity, cgs.media.gibFoot, 1.0, 0 ); + + VectorCopy( playerOrigin, origin ); + velocity[0] = crandom() * GIB_VELOCITY; + velocity[1] = crandom() * GIB_VELOCITY; + velocity[2] = GIB_JUMP + crandom() * GIB_VELOCITY; + CG_LaunchGib( cent, origin, vec3_origin, velocity, cgs.media.gibForearm, 1.0, 0 ); + + VectorCopy( playerOrigin, origin ); + velocity[0] = crandom() * GIB_VELOCITY; + velocity[1] = crandom() * GIB_VELOCITY; + velocity[2] = GIB_JUMP + crandom() * GIB_VELOCITY; + CG_LaunchGib( cent, origin, vec3_origin, velocity, cgs.media.gibIntestine, 1.0, 0 ); + + VectorCopy( playerOrigin, origin ); + velocity[0] = crandom() * GIB_VELOCITY; + velocity[1] = crandom() * GIB_VELOCITY; + velocity[2] = GIB_JUMP + crandom() * GIB_VELOCITY; + CG_LaunchGib( cent, origin, vec3_origin, velocity, cgs.media.gibLeg, 1.0, 0 ); + } + + +//----(SA) toss the hat + if ( !( cent->currentState.eFlags & EF_HEADSHOT ) ) { // (SA) already lost hat while living + CG_LoseHat( cent, tv( 0, 0, 1 ) ); + } +//----(SA) end + + // Ridah, spawn a bunch of blood dots around the place + #define GIB_BLOOD_DOTS 4 + for ( i = 0, count = 0; i < GIB_BLOOD_DOTS * 2; i++ ) { + //static vec3_t mins = {-10,-10,-10}; // TTimo: unused + //static vec3_t maxs = { 10, 10, 10}; // TTimo: unused + + if ( i > 0 ) { + velocity[0] = ( ( i % 2 ) * 2 - 1 ) * ( 40 + 40 * random() ); + velocity[1] = ( ( ( i / 2 ) % 2 ) * 2 - 1 ) * ( 40 + 40 * random() ); + velocity[2] = ( ( ( i < GIB_BLOOD_DOTS ) * 2 ) - 1 ) * 40; + } else { + VectorClear( velocity ); + velocity[2] = -64; + } + + VectorAdd( playerOrigin, velocity, origin ); + + CG_Trace( &trace, playerOrigin, NULL, NULL, origin, -1, CONTENTS_SOLID ); + if ( trace.fraction < 1.0 ) { + BG_GetMarkDir( velocity, trace.plane.normal, velocity ); + CG_ImpactMark( cgs.media.bloodDotShaders[rand() % 5], trace.endpos, velocity, random() * 360, + 1,1,1,1, qtrue, 30, qfalse, cg_bloodTime.integer * 1000 ); + if ( count++ > GIB_BLOOD_DOTS ) { + break; + } + } + } + +} + + +/* +============== +CG_SparklerSparks +============== +*/ +void CG_SparklerSparks( vec3_t origin, int count ) { +// these effect the look of the, umm, effect + int FUSE_SPARK_LIFE = 100; + int FUSE_SPARK_LENGTH = 30; +// these are calculated from the above + int FUSE_SPARK_SPEED = ( FUSE_SPARK_LENGTH * 1000 / FUSE_SPARK_LIFE ); + + int i; + localEntity_t *le; + refEntity_t *re; + + for ( i = 0; i < count; i++ ) { + + // spawn the spark + le = CG_AllocLocalEntity(); + re = &le->refEntity; + + le->leType = LE_FUSE_SPARK; + le->startTime = cg.time; + le->endTime = cg.time + FUSE_SPARK_LIFE; + le->lastTrailTime = cg.time; + + VectorCopy( origin, re->origin ); + + le->pos.trType = TR_GRAVITY; + VectorCopy( origin, le->pos.trBase ); + VectorSet( le->pos.trDelta, crandom(), crandom(), crandom() ); + VectorNormalize( le->pos.trDelta ); + VectorScale( le->pos.trDelta, FUSE_SPARK_SPEED, le->pos.trDelta ); + le->pos.trTime = cg.time; + + } +} + +// just a bunch of numbers we can use for pseudo-randomizing based on time +#define NUMRANDTABLE 257 +unsigned short randtable[NUMRANDTABLE] = +{ + 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, + 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, + 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, + 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, + 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, + 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, + 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, + 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, + 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, + 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, + 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, + 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, + 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, + 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, + 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, + 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, + 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, + 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, + 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, + 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, + 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, + 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, + 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, + 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, + 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, + 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, + 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, + 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, + 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, + 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, + 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, + 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 +}; + +#define LT_MS 100 // random number will change every LT_MS millseconds +#define LT_RANDMAX ( (unsigned short)0xffff ) + +float lt_random( int thisrandseed, int t ) { + return (float)randtable[abs( ( thisrandseed + t + ( cg.time / LT_MS ) * ( cg.time / LT_MS ) ) ) % NUMRANDTABLE] / (float)LT_RANDMAX; +} + +float lt_crandom( int thisrandseed, int t ) { + return ( ( 2.0 * ( (float)randtable[abs( ( thisrandseed + t + ( cg.time / LT_MS ) * ( cg.time / LT_MS ) ) ) % NUMRANDTABLE] / (float)LT_RANDMAX ) ) - 1.0 ); +} + +/* +=============== +CG_DynamicLightningBolt +=============== +*/ +void CG_DynamicLightningBolt( qhandle_t shader, vec3_t start, vec3_t pend, int numBolts, float maxWidth, qboolean fade, float startAlpha, int recursion, int randseed ) { + int i,j; + float segMin, segMax, length; + float thisSeg, distLeft, thisWidth; + vec3_t pos, vec, end; + int curJunc; + vec3_t c, bc = {0.8,0.9,1}; + const float rndSize = 12.0; + const float maxSTscale = 30.0; + float stScale; + float alpha, viewDist; + const int trailLife = 1; + int forks = 0; + #define STYPE_LIGHTNING STYPE_REPEAT // ST mapping for trail + #define FORK_CHANCE 0.5 + #define VIEW_SCALE_DIST 128 + + VectorCopy( pend, end ); // need this so recursive calls don't override stacked endpoints + + // HACK, updated sprite, so downscale all widths + maxWidth *= 0.6; + + length = Distance( start, end ); + //if (length > 128) { + segMin = length / 10.0; + if ( segMin < 8 ) { + segMin = 8; + } + segMax = segMin * 1.2; + //segMax = length / 30.0; + //} else { + // segMin = length / 3.0; + // segMax = length / 1.5; + //} + + if ( startAlpha > 1.0 ) { + startAlpha = 1.0; + } + alpha = startAlpha; // change only if fading + + for ( i = 0; i < numBolts; i++ ) + { + distLeft = length; + VectorCopy( start, pos ); + // drop a start junction + stScale = maxSTscale * ( 0.5 + lt_random( randseed,i + 1 ) * 0.5 ); + if ( fade ) { + if ( startAlpha == 1.0 ) { + alpha = startAlpha * ( distLeft / length ); + } else { + alpha = 1.0 - 1.0 * fabs( ( 1.0 - ( distLeft / length ) ) - startAlpha ); + if ( alpha < 0 ) { + alpha = 0; + } + if ( alpha > 1 ) { + alpha = 1; + } + } + } + thisWidth = maxWidth * ( 0.5 + 0.5 * alpha ); + if ( ( viewDist = VectorDistance( pos, cg.refdef.vieworg ) ) < VIEW_SCALE_DIST ) { + thisWidth *= 0.5 + ( 0.5 * ( viewDist / VIEW_SCALE_DIST ) ); + if ( thisWidth < 4.0 && thisWidth < maxWidth ) { + thisWidth = 4.0; + } + } else { // scale it wider with distance so it remains visible + thisWidth *= 0.5 + ( 0.5 * ( viewDist / VIEW_SCALE_DIST ) ); + if ( thisWidth > maxWidth * 2 ) { + // thisWidth > maxWidth*2; + thisWidth = maxWidth * 2; + } + } + // + VectorScale( bc, alpha, c ); + c[2] *= 1.0 + ( 1.0 - alpha ); + if ( c[2] > 1.0 ) { + c[2] = 1.0; + } + // + curJunc = CG_AddTrailJunc( 0, shader, cg.time, STYPE_LIGHTNING, pos, trailLife, 1, 1, thisWidth, thisWidth, TJFL_NOCULL, c, c, stScale, 20.0 ); + while ( distLeft > 0 ) + { // create this bolt + thisSeg = segMin + ( segMax - segMin ) * lt_random( randseed,2 ); + thisWidth = maxWidth * ( 0.5 + 0.5 * alpha ); + if ( thisSeg >= distLeft - rndSize ) { // go directly to the end point + VectorCopy( end, pos ); + thisWidth *= 0.6; + } else if ( (float)distLeft / length < 0.3 ) { + VectorSubtract( end, pos, vec ); + VectorNormalize( vec ); + VectorMA( pos, thisSeg, vec, pos ); + // randomize the position a bit + thisSeg *= 0.05; + if ( thisSeg > rndSize ) { + thisSeg = rndSize; + } + for ( j = 0; j < 3; j++ ) { + viewDist = lt_crandom( randseed * randseed,j * j + i * i + 3 ); + if ( fabs( viewDist ) < 0.5 ) { + if ( viewDist > 0 ) { + viewDist = 0.5; + } else { viewDist = -0.5;} + } + pos[j] += viewDist * thisSeg; + } + } else { + VectorSubtract( end, pos, vec ); + VectorNormalize( vec ); + VectorMA( pos, thisSeg, vec, pos ); + // randomize the position a bit + thisSeg *= 0.25; + if ( thisSeg > rndSize ) { + thisSeg = rndSize; + } + for ( j = 0; j < 3; j++ ) { + viewDist = lt_crandom( randseed,j * j + i * i + 3 ); + if ( fabs( viewDist ) < 0.5 ) { + if ( viewDist > 0 ) { + viewDist = 0.5; + } else { viewDist = -0.5;} + } + pos[j] += viewDist * thisSeg; + } + } + + distLeft = Distance( pos, end ); + if ( fade ) { + if ( startAlpha == 1.0 ) { + alpha = startAlpha * ( distLeft / length ); + } else { + alpha = 1.0 - 1.0 * fabs( ( 1.0 - ( distLeft / length ) ) - startAlpha ); + if ( alpha < 0 ) { + alpha = 0; + } + if ( alpha > 1 ) { + alpha = 1; + } + } + } + //thisWidth *= alpha; + if ( ( viewDist = VectorDistance( pos, cg.refdef.vieworg ) ) < VIEW_SCALE_DIST ) { + thisWidth *= 0.5 + ( 0.5 * ( viewDist / VIEW_SCALE_DIST ) ); + if ( thisWidth < 4.0 && thisWidth < maxWidth ) { + thisWidth = 4.0; + } + } else { // scale it wider with distance so it remains visible + thisWidth *= 0.5 + ( 0.5 * ( viewDist / VIEW_SCALE_DIST ) ); + if ( thisWidth > maxWidth * 2 ) { + // thisWidth > maxWidth*2; + thisWidth = maxWidth * 2; + } + + } + // + VectorScale( bc, alpha, c ); + c[2] *= 1.0 + ( 1.0 - alpha ); + if ( c[2] > 1.0 ) { + c[2] = 1.0; + } + // + //stScale = maxSTscale * (0.4 + lt_random(randseed,1)*0.6); + curJunc = CG_AddTrailJunc( curJunc, shader, cg.time, STYPE_LIGHTNING, pos, trailLife, 1, 1, thisWidth, thisWidth, TJFL_NOCULL, c, c, stScale, 20.0 ); + + // fork from here? + if ( thisWidth < 4 && distLeft > 10 && recursion < 3 && forks < 3 && lt_random( randseed,383 + i + forks ) < FORK_CHANCE ) { + vec3_t fend; + forks++; + VectorSet( fend, + distLeft * 0.3 * lt_crandom( randseed,56 + i + forks ), + distLeft * 0.3 * lt_crandom( randseed,160 + i + forks ), + distLeft * 0.3 * lt_crandom( randseed,190 + i + forks ) ); + VectorAdd( fend, end, fend ); + VectorSubtract( fend, pos, fend ); + VectorMA( pos, 0.2 + 0.7 * lt_random( randseed,6 + i + forks ), fend, fend ); + + //if (recursion > 0 && recursion < 2) { + // CG_DynamicLightningBolt( cgs.media.lightningBoltShader, pos, fend, 1, maxWidth, qtrue, alpha, recursion, randseed ); + // return; // divert bolt rather than split + //} else { + CG_DynamicLightningBolt( shader, pos, fend, 1, maxWidth, qtrue, alpha, recursion + 1, randseed + 765 ); + //} + } + + randseed++; + + } + } +} + + + +/* +================ +CG_ProjectedSpotLight +================ +*/ +void CG_ProjectedSpotLight( vec3_t start, vec3_t dir ) { + vec3_t end, proj; + trace_t tr; + float alpha, radius; + + VectorMA( start, 1000, dir, end ); + CG_Trace( &tr, start, NULL, NULL, end, -1, CONTENTS_SOLID ); + if ( tr.fraction == 1.0 ) { + return; + } + // + alpha = ( 1.0 - tr.fraction ); + if ( alpha > 1.0 ) { + alpha = 1.0; + } + // + radius = 32 + 64 * tr.fraction; + VectorNegate( dir, proj ); + CG_ImpactMark( cgs.media.spotLightShader, tr.endpos, proj, 0, alpha, alpha, alpha, 1.0, qfalse, radius, qtrue, -2 ); +} + + +#define MAX_SPOT_SEGS 20 +#define MAX_SPOT_RANGE 2000 +/* +============== +CG_Spotlight + segs: number of sides on tube. - 999 is a valid value and just means, 'cap to max' (MAX_SPOT_SEGS) or use lod scheme + range: effective range of beam + startWidth: will be optimized for '0' as a value (true cone) to not use quads and not cap the start circle + + -- flags -- + SL_NOTRACE - don't do a trace check for shortening the beam, always draw at full 'range' length + SL_TRACEWORLDONLY - go through everything but the world + SL_NODLIGHT - don't put a dlight at the end + SL_NOSTARTCAP - dont' cap the start circle + SL_LOCKTRACETORANGE - only trace out as far as the specified range (rather than to max spot range) + SL_NOFLARE - don't draw a flare when the light is pointing at the camera + SL_NOIMPACT - don't draw the impact mark on hit surfaces + SL_LOCKUV - lock the texture coordinates at the 'true' length of the requested beam. + SL_NOCORE - don't draw the center 'core' beam + + + + + + + I know, this is a bit kooky right now. It evolved big, but now that I know what it should do, it'll get + crunched down to a bunch of table driven stuff. once it works, I'll make it work well... + +============== +*/ + +void CG_Spotlight( centity_t *cent, float *color, vec3_t realstart, vec3_t lightDir, int segs, float range, int startWidth, float coneAngle, int flags ) { + int i, j; + vec3_t start, traceEnd, proj; + vec3_t right, up; + vec3_t v1, v2; + vec3_t startvec, endvec; // the vectors to rotate around lightDir to create the circles + vec3_t conevec; + vec3_t start_points[MAX_SPOT_SEGS], end_points[MAX_SPOT_SEGS]; + vec3_t coreright; + polyVert_t verts[MAX_SPOT_SEGS * 4]; // x4 for 4 verts per poly + polyVert_t plugVerts[MAX_SPOT_SEGS]; + vec3_t endCenter; + polyVert_t coreverts[4]; + trace_t tr; + float /*alpha,*/ radius = 0.0f; // TTimo: init + float coreEndRadius; + qboolean capStart = qtrue; + float hitDist; // the actual distance of the trace impact (0 is no hit) + float beamLen; // actual distance of the drawn beam + float startAlpha, endAlpha; + vec4_t colorNorm; // normalized color vector + refEntity_t ent; + vec3_t angles; + float deadFrac = 0; + + VectorCopy( realstart, start ); + + // normalize color + colorNorm[3] = 0; // store normalize multiplier in alpha index + for ( i = 0; i < 3; i++ ) { + if ( color[i] > colorNorm[3] ) { + colorNorm[3] = color[i]; // find largest color value in RGB + } + } + + if ( colorNorm[3] != 1 ) { // it needs to be boosted + VectorMA( color, 1.0 / colorNorm[3], color, colorNorm ); // FIXME: div by 0 + } else { + VectorCopy( color, colorNorm ); + } + colorNorm[3] = color[3]; + + + if ( flags & SL_NOSTARTCAP ) { + capStart = qfalse; + } + + if ( startWidth == 0 ) { // cone, not cylinder + capStart = qfalse; + } + + + if ( flags & SL_LOCKTRACETORANGE ) { + VectorMA( start, range, lightDir, traceEnd ); // trace out to 'range' + } else { + VectorMA( start, MAX_SPOT_RANGE, lightDir, traceEnd ); // trace all the way out to max dist + } + + // first trace to see if anything is hit + if ( flags & SL_NOTRACE ) { + tr.fraction = 1.0; // force no hit + } else { + if ( flags & SL_TRACEWORLDONLY ) { + CG_Trace( &tr, start, NULL, NULL, traceEnd, -1, CONTENTS_SOLID ); + } else { +// CG_Trace( &tr, start, NULL, NULL, traceEnd, -1, MASK_SHOT); + CG_Trace( &tr, start, NULL, NULL, traceEnd, -1, MASK_SHOT & ~CONTENTS_BODY ); + } +// CG_Trace( &tr, start, NULL, NULL, traceEnd, -1, MASK_ALL &~(CONTENTS_MONSTERCLIP|CONTENTS_AREAPORTAL|CONTENTS_CLUSTERPORTAL)); + } + + + if ( tr.fraction < 1.0 ) { + hitDist = beamLen = MAX_SPOT_RANGE * tr.fraction; + if ( beamLen > range ) { + beamLen = range; + } + } else { + hitDist = 0; + beamLen = range; + } + + + startAlpha = color[3] * 255.0f; + endAlpha = 0.0f; + + + + if ( flags & SL_LOCKUV ) { + if ( beamLen < range ) { + endAlpha = 255.0f * ( color[3] - ( color[3] * beamLen / range ) ); + } + } + + + if ( segs >= MAX_SPOT_SEGS ) { + segs = MAX_SPOT_SEGS - 1; + } + + // TODO: adjust segs based on r_lodbias + // TODO: move much of this to renderer + + +// model at base +// if(cgs.media.spotLightLightModel) { + memset( &ent, 0, sizeof( ent ) ); + ent.frame = 0; + ent.oldframe = 0; + ent.backlerp = 0; + VectorCopy( cent->lerpOrigin, ent.origin ); + VectorCopy( cent->lerpOrigin, ent.oldorigin ); +// ent.hModel = cgs.gameModels[cent->currentState.modelindex]; + if ( cent->currentState.frame == 1 ) { + ent.hModel = cgs.media.spotLightLightModelBroke; + } else { + ent.hModel = cgs.media.spotLightLightModel; + } + + vectoangles( lightDir, angles ); + angles[ROLL] = 0.0f; // clear out roll so it doesn't interfere + AnglesToAxis( angles, ent.axis ); + trap_R_AddRefEntityToScene( &ent ); + + ent.hModel = cgs.media.spotLightBaseModel; + angles[PITCH] = 0.0f; // flatten out pitch so it only yaws + AnglesToAxis( angles, ent.axis ); + trap_R_AddRefEntityToScene( &ent ); + + // push start out a bit so the beam fits to the front of the base model + VectorMA( start, 14, lightDir, start ); +// } + + + + if ( cent->currentState.frame == 1 ) { // dead + deadFrac = (float)( cg.time - cent->currentState.time2 ) / 5000.0f; + + // no fade out, just off +// if(deadFrac > 1) + return; + + startAlpha *= ( 1.0f - deadFrac ); + endAlpha *= ( 1.0f - deadFrac ); + } + + +//// BEAM + + PerpendicularVector( up, lightDir ); + CrossProduct( lightDir, up, right ); + + // find first vert of the start + VectorScale( right, startWidth, startvec ); + // find the first vert of the end + RotatePointAroundVector( conevec, up, lightDir, -coneAngle ); + VectorMA( startvec, beamLen, conevec, endvec ); // this applies the offset of the start diameter before finding the end points + + VectorScale( lightDir, beamLen, endCenter ); + VectorSubtract( endCenter, endvec, coreverts[3].xyz ); // get a vector of the radius out at the end for the core to use + coreEndRadius = VectorLength( coreverts[3].xyz ); +#define CORESCALE 0.6f + +// +// generate the flat beam 'core' +// + if ( !( flags & SL_NOCORE ) ) { + VectorSubtract( start, cg.refdef.vieworg, v1 ); + VectorNormalize( v1 ); + VectorSubtract( traceEnd, cg.refdef.vieworg, v2 ); + VectorNormalize( v2 ); + CrossProduct( v1, v2, coreright ); + VectorNormalize( coreright ); + + memset( &coreverts[0], 0, 4 * sizeof( polyVert_t ) ); + VectorMA( start, startWidth * 0.8f, coreright, coreverts[0].xyz ); + VectorMA( start, -startWidth * 0.8f, coreright, coreverts[1].xyz ); + VectorMA( endCenter, -coreEndRadius * CORESCALE, coreright, coreverts[2].xyz ); + VectorAdd( start, coreverts[2].xyz, coreverts[2].xyz ); + VectorMA( endCenter, coreEndRadius * CORESCALE, coreright, coreverts[3].xyz ); + VectorAdd( start, coreverts[3].xyz, coreverts[3].xyz ); + + for ( i = 0; i < 4; i++ ) { + coreverts[i].modulate[0] = color[0] * 200.0f; + coreverts[i].modulate[1] = color[1] * 200.0f; + coreverts[i].modulate[2] = color[2] * 200.0f; + coreverts[i].modulate[3] = startAlpha; + if ( i > 1 ) { + coreverts[i].modulate[3] = endAlpha; + } + } + + trap_R_AddPolyToScene( cgs.media.spotLightBeamShader, 4, &coreverts[0] ); + } + + +// +// generate the beam cylinder +// + + + + for ( i = 0; i <= segs; i++ ) { + RotatePointAroundVector( start_points[i], lightDir, startvec, ( 360.0f / (float)segs ) * i ); + VectorAdd( start_points[i], start, start_points[i] ); + + RotatePointAroundVector( end_points[i], lightDir, endvec, ( 360.0f / (float)segs ) * i ); + VectorAdd( end_points[i], start, end_points[i] ); + } + + for ( i = 0; i < segs; i++ ) { + + j = ( i * 4 ); + + VectorCopy( start_points[i], verts[( i * 4 )].xyz ); + verts[j].st[0] = 0; + verts[j].st[1] = 1; + verts[j].modulate[0] = color[0] * 255.0f; + verts[j].modulate[1] = color[1] * 255.0f; + verts[j].modulate[2] = color[2] * 255.0f; + verts[j].modulate[3] = startAlpha; + j++; + + VectorCopy( end_points[i], verts[j].xyz ); + verts[j].st[0] = 0; + verts[j].st[1] = 0; + verts[j].modulate[0] = color[0] * 255.0f; + verts[j].modulate[1] = color[1] * 255.0f; + verts[j].modulate[2] = color[2] * 255.0f; + verts[j].modulate[3] = endAlpha; + j++; + + VectorCopy( end_points[i + 1], verts[j].xyz ); + verts[j].st[0] = 1; + verts[j].st[1] = 0; + verts[j].modulate[0] = color[0] * 255.0f; + verts[j].modulate[1] = color[1] * 255.0f; + verts[j].modulate[2] = color[2] * 255.0f; + verts[j].modulate[3] = endAlpha; + j++; + + VectorCopy( start_points[i + 1], verts[j].xyz ); + verts[j].st[0] = 1; + verts[j].st[1] = 1; + verts[j].modulate[0] = color[0] * 255.0f; + verts[j].modulate[1] = color[1] * 255.0f; + verts[j].modulate[2] = color[2] * 255.0f; + verts[j].modulate[3] = startAlpha; + + if ( capStart ) { + VectorCopy( start_points[i], plugVerts[i].xyz ); + plugVerts[i].st[0] = 0; + plugVerts[i].st[1] = 0; + plugVerts[i].modulate[0] = color[0] * 255.0f; + plugVerts[i].modulate[1] = color[1] * 255.0f; + plugVerts[i].modulate[2] = color[2] * 255.0f; + plugVerts[i].modulate[3] = startAlpha; + } + } + + trap_R_AddPolysToScene( cgs.media.spotLightBeamShader, 4, &verts[0], segs ); + + + // plug up the start circle + if ( capStart ) { + trap_R_AddPolyToScene( cgs.media.spotLightBeamShader, segs, &plugVerts[0] ); + } + + + // show the endpoint + + if ( !( flags & SL_NOIMPACT ) ) { + if ( hitDist ) { + vec3_t impactPos; + VectorMA( startvec, hitDist, conevec, endvec ); + + radius = 1.5f * coreEndRadius * ( hitDist / beamLen ); + + VectorNegate( lightDir, proj ); + + VectorMA( tr.endpos, -0.5f * radius, lightDir, impactPos ); // back away a little from the hit + + CG_ImpactMark( cgs.media.spotLightShader, impactPos, proj, 0, colorNorm[0], colorNorm[1], colorNorm[2], 0.3f, qfalse, radius, qtrue, -1 ); +// CG_ImpactMark( cgs.media.spotLightShader, tr.endpos, proj, 0, colorNorm[0], colorNorm[1], colorNorm[2], 1.0f, qfalse, radius, qtrue, -1 ); + } + } + + + + // add d light at end + if ( !( flags & SL_NODLIGHT ) ) { + vec3_t dlightLoc; +// VectorMA(tr.endpos, -60, lightDir, dlightLoc); // back away from the hit + VectorMA( tr.endpos, 0, lightDir, dlightLoc ); // back away from the hit +// trap_R_AddLightToScene(dlightLoc, 200, colorNorm[0], colorNorm[1], colorNorm[2], 0); // ,REF_JUNIOR_DLIGHT); +// trap_R_AddLightToScene(dlightLoc, radius*2, colorNorm[0], colorNorm[1], colorNorm[2], 0); // ,REF_JUNIOR_DLIGHT); + trap_R_AddLightToScene( dlightLoc, radius * 2, 0.3, 0.3, 0.3, 0 ); // ,REF_JUNIOR_DLIGHT); + } + + +#define FLAREANGLE 35 + + // draw flare at source + if ( !( flags & SL_NOFLARE ) ) { + qboolean lightInEyes = qfalse; + vec3_t camloc, dirtolight; + float dot, deg, dist; + float flarescale = 0.0f; // TTimo: init + + // get camera position and direction to lightsource + VectorCopy( cg.snap->ps.origin, camloc ); + camloc[2] += cg.snap->ps.viewheight; + VectorSubtract( start, camloc, dirtolight ); + dist = VectorNormalize( dirtolight ); + + // first use dot to determine if it's facing the camera + dot = DotProduct( lightDir, dirtolight ); + + // it's facing the camera, find out how closely and trace to see if the source can be seen + + deg = RAD2DEG( M_PI - acos( dot ) ); + if ( deg <= FLAREANGLE ) { // start flare a bit before the camera gets inside the cylinder + lightInEyes = qtrue; + flarescale = 1 - ( deg / FLAREANGLE ); + } + + if ( lightInEyes ) { // the dot check succeeded, now do a trace +// CG_Trace( &tr, start, NULL, NULL, camloc, -1, MASK_ALL &~(CONTENTS_MONSTERCLIP|CONTENTS_AREAPORTAL|CONTENTS_CLUSTERPORTAL)); + CG_Trace( &tr, start, NULL, NULL, camloc, -1, MASK_SOLID ); + if ( tr.fraction != 1 ) { + lightInEyes = qfalse; + } + + } + + if ( lightInEyes ) { + float coronasize = flarescale; + if ( dist < 512 ) { // make even bigger if you're close enough + coronasize *= ( 512.0f / dist ); + } + + trap_R_AddCoronaToScene( start, colorNorm[0], colorNorm[1], colorNorm[2], coronasize, cent->currentState.number, 3 ); // 1&2 ('visible' & 'spotlightflare') + } else { + // even though it's off, still need to add it, but turned off so it can fade in/out properly + trap_R_AddCoronaToScene( start, colorNorm[0], colorNorm[1], colorNorm[2], 0, cent->currentState.number, 2 ); // 0&2 ('not visible' & 'spotlightflare') + } + } + +} + + + +/* +============== +CG_RumbleEfx +============== +*/ +void CG_RumbleEfx( float pitch, float yaw ) { + float pitchRecoilAdd, pitchAdd; + float yawRandom; + vec3_t recoil; + + // + pitchRecoilAdd = 0; + pitchAdd = 0; + yawRandom = 0; + // + + if ( pitch < 1 ) { + pitch = 1; + } + + pitchRecoilAdd = pow( random(),8 ) * ( 10 + VectorLength( cg.snap->ps.velocity ) / 5 ); + pitchAdd = ( rand() % (int)pitch ) - ( pitch * 0.5 ); //5 + yawRandom = yaw; //2 + + pitchRecoilAdd *= 0.5; + pitchAdd *= 0.5; + yawRandom *= 0.5; + + // calc the recoil + recoil[YAW] = crandom() * yawRandom; + recoil[ROLL] = -recoil[YAW]; // why not + recoil[PITCH] = -pitchAdd; + // scale it up a bit (easier to modify this while tweaking) + VectorScale( recoil, 30, recoil ); + // set the recoil + VectorCopy( recoil, cg.kickAVel ); + // set the recoil + cg.recoilPitch -= pitchRecoilAdd; +} + + + diff --git a/src/cgame/cg_ents.c b/src/cgame/cg_ents.c new file mode 100644 index 0000000..791e852 --- /dev/null +++ b/src/cgame/cg_ents.c @@ -0,0 +1,2680 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +/* + * name: cg_ents.c + * + * desc: present snapshot entities, happens every single frame + * +*/ + + +#include "cg_local.h" + +/////////////////////// +extern int propellerModel; +/////////////////////// + + + +/* +====================== +CG_PositionEntityOnTag + +Modifies the entities position and axis by the given +tag location +====================== +*/ +void CG_PositionEntityOnTag( refEntity_t *entity, const refEntity_t *parent, + char *tagName, int startIndex, vec3_t *offset ) { + int i; + orientation_t lerped; + + // lerp the tag + trap_R_LerpTag( &lerped, parent, tagName, startIndex ); + + // FIXME: allow origin offsets along tag? //----(SA) Yes! Adding. + + VectorCopy( parent->origin, entity->origin ); + + if ( offset ) { + VectorAdd( lerped.origin, *offset, lerped.origin ); + } +//----(SA) end + + for ( i = 0 ; i < 3 ; i++ ) { + VectorMA( entity->origin, lerped.origin[i], parent->axis[i], entity->origin ); + } + + // had to cast away the const to avoid compiler problems... + MatrixMultiply( lerped.axis, ( (refEntity_t *)parent )->axis, entity->axis ); + // Ridah, not sure why this was here.. causes jittery torso animation, since the torso might have + // different frame/oldFrame + //entity->backlerp = parent->backlerp; +} + + +/* +====================== +CG_PositionRotatedEntityOnTag + +Modifies the entities position and axis by the given +tag location +====================== +*/ +void CG_PositionRotatedEntityOnTag( refEntity_t *entity, const refEntity_t *parent, + char *tagName ) { + int i; + orientation_t lerped; + vec3_t tempAxis[3]; + +//AxisClear( entity->axis ); + // lerp the tag + trap_R_LerpTag( &lerped, parent, tagName, 0 ); + + // FIXME: allow origin offsets along tag? + VectorCopy( parent->origin, entity->origin ); + for ( i = 0 ; i < 3 ; i++ ) { + VectorMA( entity->origin, lerped.origin[i], parent->axis[i], entity->origin ); + } + + // had to cast away the const to avoid compiler problems... + MatrixMultiply( entity->axis, lerped.axis, tempAxis ); + MatrixMultiply( tempAxis, ( (refEntity_t *)parent )->axis, entity->axis ); +} + + +//----(SA) added +/* +============== +CG_LoseArmor + maybe better in cg_localents.c +============== +*/ +void CG_LoseArmor( centity_t *cent, int index ) { + char *protoTags[] = { "tag_chest", + "tag_calfleft", + "tag_armleft", + "tag_back", + "tag_legleft", + "tag_calfright", + "tag_armright", + "tag_back", + "tag_legright"}; + + char *ssTags[] = { "tag_chest", + "tag_calfleft", + "tag_armleft", + "tag_back", + "tag_legleft", + "tag_calfright", + "tag_armright", + "tag_back", + "tag_legright", + + "tag_footleft", + "tag_footright", + "tag_sholeft", + "tag_shoright", + "tag_torso", + "tag_calfleft", + "tag_calfright"}; + + char *heinrichTags[] = {"tag_chest", + "tag_calfleft", + "tag_armleft", + "tag_back", + "tag_legleft", + "tag_calfright", + "tag_armright", + "tag_back", + "tag_legright", + + "tag_footleft", + "tag_footright", + "tag_sholeft", + "tag_shoright", + "tag_torso", + "tag_legleft", + "tag_legright", + + "tag_sholeft", + "tag_shoright", + "tag_legleft", + "tag_legright", + "tag_calfleft", + "tag_calfright"}; + + clientInfo_t *ci; + // TTimo: bunch of inits + int totalparts = 0, dynamicparts = 0, protoParts = 9, superParts = 16, heinrichParts = 22; + char **tags = NULL; + qhandle_t *models = NULL; + qhandle_t sound = 0; //----(SA) added + int dmgbits = 16; // 32/2; + int clientNum; + int tagIndex; + vec3_t origin, velocity, dir; + + + if ( cent->currentState.aiChar == AICHAR_PROTOSOLDIER ) { + tags = &protoTags[0]; + models = &cgs.media.protoArmor[0]; + dynamicparts = totalparts = protoParts; + sound = cgs.media.protoArmorBreak; + } else if ( cent->currentState.aiChar == AICHAR_SUPERSOLDIER ) { + tags = &ssTags[0]; + models = &cgs.media.superArmor[0]; + dynamicparts = 14; // the other two stay permanent + totalparts = superParts; + sound = cgs.media.superArmorBreak; + } else if ( cent->currentState.aiChar == AICHAR_HEINRICH ) { + tags = &heinrichTags[0]; + models = &cgs.media.heinrichArmor[0]; + dynamicparts = 20; // will get kicked down to 16 + totalparts = heinrichParts; + sound = cgs.media.heinrichArmorBreak; + } else { + return; //----(SA) added + } + + if ( dynamicparts > dmgbits ) { + dynamicparts = dmgbits; + } + + if ( index > dynamicparts ) { // shouldn't happen + return; + } + + clientNum = cent->currentState.clientNum; + if ( clientNum < 0 || clientNum >= MAX_CLIENTS ) { + CG_Error( "Bad clientNum on player entity" ); + } + ci = &cgs.clientinfo[ clientNum ]; + + // check if the model for the damaged part to fling is there + if ( cent->currentState.dmgFlags & ( 1 << ( index + dynamicparts ) ) ) { + if ( !models[index + ( 2 * totalparts )] ) { + return; + } + } else if ( !models[index + totalparts] ) { + return; + } + + tagIndex = CG_GetOriginForTag( cent, ¢->pe.torsoRefEnt, tags[index], 0, origin, NULL ); + + // calculate direction vector based on player center->tag position + VectorSubtract( origin, cent->currentState.origin, dir ); + VectorNormalize( dir ); + +//----(SA) added + if ( sound ) { + CG_SoundPlayIndexedScript( sound, NULL, cent->currentState.number ); + } +//----(SA) end + +//#define FLY_VELOCITY 75 +//#define FLY_JUMP 200 +#define FLY_VELOCITY 200 +#define FLY_JUMP 300 + +// velocity[0] = dir[0]*(0.75+random())*FLY_VELOCITY; +// velocity[1] = dir[1]*(0.75+random())*FLY_VELOCITY; + velocity[0] = dir[0] * FLY_VELOCITY; + velocity[1] = dir[1] * FLY_VELOCITY; + velocity[2] = FLY_JUMP - 50 + dir[2] * ( 0.5 + random() ) * FLY_VELOCITY; + + { + localEntity_t *le; + refEntity_t *re; + + le = CG_AllocLocalEntity(); + re = &le->refEntity; + + le->leType = LE_FRAGMENT; + le->startTime = cg.time; + le->endTime = le->startTime + 20000 + ( crandom() * 5000 ); + + VectorCopy( origin, re->origin ); + AxisCopy( axisDefault, re->axis ); + + if ( cent->currentState.dmgFlags & ( 1 << ( index + dynamicparts ) ) ) { + re->hModel = models[index + ( 2 * dynamicparts )]; + } else { + re->hModel = models[index + dynamicparts]; + } + + + re->fadeStartTime = le->endTime - 1000; + re->fadeEndTime = le->endTime; + + // (SA) FIXME: origin of md3 is offset from center. need to center the origin when you toss it + le->pos.trType = TR_GRAVITY; + VectorCopy( origin, le->pos.trBase ); + VectorCopy( velocity, le->pos.trDelta ); + le->pos.trTime = cg.time; + + // spin it a bit + le->angles.trType = TR_LINEAR; + VectorCopy( tv( 0, 0, 0 ), le->angles.trBase ); + le->angles.trDelta[0] = 0; + le->angles.trDelta[1] = ( 100 + ( rand() & 500 ) ) - 300; + le->angles.trDelta[2] = 400; + le->angles.trTime = cg.time; + le->bounceFactor = 0.4; + +// time = le->lastTrailTime+step; + + // want to add a trail, but I need to find out what all these parameters are first + + le->headJuncIndex = CG_AddTrailJunc( le->headJuncIndex, + cgs.media.zombieSpiritTrailShader, +// time, + le->lastTrailTime + 50, + STYPE_STRETCH, + le->refEntity.origin, +// (int)le->effectWidth, // trail life + 1000, + 0.3, + 0.0, +// le->radius, + 50, + 0, + 0, //TJFL_FIXDISTORT, + colorWhite, + colorWhite, + 1.0, 1 ); + + + // Ridah, if the player is on fire, then make the part on fire + if ( cent && CG_EntOnFire( cent ) ) { + le->onFireStart = cent->currentState.onFireStart; + le->onFireEnd = cent->currentState.onFireEnd + 4000; + } + } +} + + +/* +============== +CG_AttachedPartChange +============== +*/ +void CG_AttachedPartChange( centity_t *cent ) { + unsigned int diff, i; + int aiCharNum, numParts = 0; + + aiCharNum = cent->currentState.aiChar; + + diff = (unsigned int)cent->nextState.dmgFlags ^ (unsigned int)cent->currentState.dmgFlags; + + if ( aiCharNum == AICHAR_PROTOSOLDIER || + aiCharNum == AICHAR_SUPERSOLDIER || + aiCharNum == AICHAR_HEINRICH ) { + // TODO: get these from a bloody #define (or something) + if ( aiCharNum == AICHAR_PROTOSOLDIER ) { + numParts = 9; + } else if ( aiCharNum == AICHAR_SUPERSOLDIER ) { + numParts = 14; + } else if ( aiCharNum == AICHAR_HEINRICH ) { + numParts = 20; + } + + for ( i = 0; i < numParts; i++ ) { // FIXME: 7 is how many parts he currently has, this should probably share a define with the server, or be stored in the cent (cent->currentState.dmgParts) + if ( diff & ( 1 << i ) ) { + CG_LoseArmor( cent, i ); + } + } + } +} + +//----(SA) end + + + + +/* +========================================================================== + +FUNCTIONS CALLED EACH FRAME + +========================================================================== +*/ + +/* +====================== +CG_SetEntitySoundPosition + +Also called by event processing code +====================== +*/ +void CG_SetEntitySoundPosition( centity_t *cent ) { + if ( cent->currentState.solid == SOLID_BMODEL ) { + vec3_t origin; + float *v; + + v = cgs.inlineModelMidpoints[ cent->currentState.modelindex ]; + VectorAdd( cent->lerpOrigin, v, origin ); + trap_S_UpdateEntityPosition( cent->currentState.number, origin ); + } else { + trap_S_UpdateEntityPosition( cent->currentState.number, cent->lerpOrigin ); + } +} + + + + +#define LS_FRAMETIME 100 // (ms) cycle through lightstyle characters at 10fps + + +/* +============== +CG_SetDlightIntensity + +============== +*/ +void CG_AddLightstyle( centity_t *cent ) { + float lightval; + int cl; + int r, g, b; + int stringlength; + float offset; + int offsetwhole; + int otime; + int lastch, nextch; + + if ( !cent->dl_stylestring ) { + return; + } + + otime = cg.time - cent->dl_time; + stringlength = strlen( cent->dl_stylestring ); + + // it's been a long time since you were updated, lets assume a reset + if ( otime > 2 * LS_FRAMETIME ) { + otime = 0; + cent->dl_frame = cent->dl_oldframe = 0; + cent->dl_backlerp = 0; + } + + cent->dl_time = cg.time; + + offset = ( (float)otime ) / LS_FRAMETIME; + offsetwhole = (int)offset; + + cent->dl_backlerp += offset; + + + if ( cent->dl_backlerp > 1 ) { // we're moving on to the next frame + cent->dl_oldframe = cent->dl_oldframe + (int)cent->dl_backlerp; + cent->dl_frame = cent->dl_oldframe + 1; + if ( cent->dl_oldframe >= stringlength ) { + cent->dl_oldframe = ( cent->dl_oldframe ) % stringlength; + if ( cent->dl_oldframe < 3 && cent->dl_sound ) { // < 3 so if an alarm comes back into the pvs it will only start a sound if it's going to be closely synced with the light, otherwise wait till the next cycle + trap_S_StartSound( NULL, cent->currentState.number, CHAN_AUTO, cgs.gameSounds[cent->dl_sound] ); + } + } + + if ( cent->dl_frame >= stringlength ) { + cent->dl_frame = ( cent->dl_frame ) % stringlength; + } + + cent->dl_backlerp = cent->dl_backlerp - (int)cent->dl_backlerp; + } + + + lastch = cent->dl_stylestring[cent->dl_oldframe] - 'a'; + nextch = cent->dl_stylestring[cent->dl_frame] - 'a'; + + lightval = ( lastch * ( 1.0 - cent->dl_backlerp ) ) + ( nextch * cent->dl_backlerp ); + + lightval = ( lightval * ( 1000.0f / 24.0f ) ) - 200.0f; // they want 'm' as the "middle" value as 300 + + lightval = max( 0.0f, lightval ); + lightval = min( 1000.0f, lightval ); + + cl = cent->currentState.constantLight; + r = cl & 255; + g = ( cl >> 8 ) & 255; + b = ( cl >> 16 ) & 255; + + trap_R_AddLightToScene( cent->lerpOrigin, lightval, (float)r / 255.0f, (float)g / 255.0f, (float)b / 255.0f, 0 ); // overdraw forced to 0 for now +} + + + +/* +================== +CG_EntityEffects + +Add continuous entity effects, like local entity emission and lighting +================== +*/ +static void CG_EntityEffects( centity_t *cent ) { + + // update sound origins + CG_SetEntitySoundPosition( cent ); + + // add loop sound + if ( cent->currentState.loopSound ) { + //----(SA) hmm, the above (CG_SetEntitySoundPosition()) sets s_entityPosition[entityNum] with a valid + // location, but the looping sound for a bmodel will never get it since that sound is + // started with the lerpOriging right here. \/ \/ How do looping sounds ever work for bmodels? + // Or have they always been broken and we just never used them? + + if ( cent->currentState.eType == ET_SPEAKER ) { + /*if(cent->currentState.density == 1) { // NO_PVS + trap_S_AddRealLoopingSound( cent->currentState.number, cent->lerpOrigin, vec3_origin, cgs.gameSounds[ cent->currentState.loopSound ] ); + } + else*/if ( cent->currentState.dmgFlags ) { // range is set + trap_S_AddRangedLoopingSound( cent->currentState.number, cent->lerpOrigin, vec3_origin, cgs.gameSounds[ cent->currentState.loopSound ], cent->currentState.dmgFlags ); + } else { + trap_S_AddLoopingSound( cent->currentState.number, cent->lerpOrigin, vec3_origin, cgs.gameSounds[ cent->currentState.loopSound ], 255 ); + } + } else if ( cent->currentState.solid == SOLID_BMODEL ) { + vec3_t origin; + float *v; + + v = cgs.inlineModelMidpoints[ cent->currentState.modelindex ]; + VectorAdd( cent->lerpOrigin, v, origin ); + + if ( cgs.gameSoundTypes[ cent->currentState.loopSound ] == 1 ) { // old style + trap_S_AddLoopingSound( cent->currentState.number, origin, vec3_origin, cgs.gameSounds[ cent->currentState.loopSound ], 255 ); + } else { // from script + int soundIndex; + soundIndex = soundScripts[cgs.gameSounds[ cent->currentState.loopSound ] - 1 ].soundList->sfxHandle; + trap_S_AddLoopingSound( cent->currentState.number, origin, vec3_origin, soundIndex, 255 ); + } + } else { + trap_S_AddLoopingSound( cent->currentState.number, cent->lerpOrigin, vec3_origin, cgs.gameSounds[ cent->currentState.loopSound ], 255 ); + } + } /*else { + // stop NO_PVS speakers if they've been turned off + if(cent->currentState.eType == ET_SPEAKER) { + if(cent->currentState.density == 1) { + trap_S_StopLoopingSound(cent->currentState.number); + } + } + }*/ + + + // constant light glow + if ( cent->currentState.constantLight ) { + int cl; + int i, r, g, b; + + + if ( cent->dl_stylestring[0] != 0 ) { // it's probably a dlight + CG_AddLightstyle( cent ); + } else + { + cl = cent->currentState.constantLight; + r = cl & 255; + g = ( cl >> 8 ) & 255; + b = ( cl >> 16 ) & 255; + i = ( ( cl >> 24 ) & 255 ) * 4; + + trap_R_AddLightToScene( cent->lerpOrigin, i, (float)r / 255.0f, (float)g / 255.0f, (float)b / 255.0f, 0 ); + } + } + + // Ridah, flaming sounds + if ( CG_EntOnFire( cent ) ) { + // play a flame blow sound when moving + trap_S_AddLoopingSound( cent->currentState.number, cent->lerpOrigin, vec3_origin, cgs.media.flameBlowSound, (int)( 255.0 * ( 1.0 - fabs( cent->fireRiseDir[2] ) ) ) ); + // play a burning sound when not moving + trap_S_AddLoopingSound( cent->currentState.number, cent->lerpOrigin, vec3_origin, cgs.media.flameSound, (int)( 0.3 * 255.0 * ( pow( cent->fireRiseDir[2],2 ) ) ) ); + } + +} + +/* +================== +CG_General +================== +*/ +static void CG_General( centity_t *cent ) { + refEntity_t ent; + entityState_t *s1; + + s1 = ¢->currentState; + + // if set to invisible, skip + if ( !s1->modelindex ) { + return; + } + + memset( &ent, 0, sizeof( ent ) ); + + // set frame + + ent.frame = s1->frame; + ent.oldframe = ent.frame; + ent.backlerp = 0; + + if ( ent.frame ) { + + ent.oldframe -= 1; + ent.backlerp = 1 - cg.frameInterpolation; + + if ( cent->currentState.time ) { + ent.fadeStartTime = cent->currentState.time; + ent.fadeEndTime = cent->currentState.time2; + } + + } + + VectorCopy( cent->lerpOrigin, ent.origin ); + VectorCopy( cent->lerpOrigin, ent.oldorigin ); + + ent.hModel = cgs.gameModels[s1->modelindex]; + + // player model + if ( s1->number == cg.snap->ps.clientNum ) { + ent.renderfx |= RF_THIRD_PERSON; // only draw from mirrors + } + + // convert angles to axis + AnglesToAxis( cent->lerpAngles, ent.axis ); + + // scale gamemodels + if ( cent->currentState.eType == ET_GAMEMODEL ) { + VectorScale( ent.axis[0], cent->currentState.angles2[0], ent.axis[0] ); + VectorScale( ent.axis[1], cent->currentState.angles2[1], ent.axis[1] ); + VectorScale( ent.axis[2], cent->currentState.angles2[2], ent.axis[2] ); + ent.nonNormalizedAxes = qtrue; + +// if( (cent->currentState.angles2[0] * cent->currentState.angles2[1] * cent->currentState.angles2[2]) != 1) { +// ent.reFlags |= REFLAG_SCALEDSPHERECULL; +// +// // find greatest scaled axis +// ent.radius = cent->currentState.angles2[0]; +// if(cent->currentState.angles2[1] > ent.radius) +// ent.radius = cent->currentState.angles2[1]; +// if(cent->currentState.angles2[2] > ent.radius) +// ent.radius = cent->currentState.angles2[2]; +// } + +//----(SA) testing +// if(cent->currentState.apos.trType) { +// ent.reFlags |= REFLAG_ORIENT_LOD; +// } +//----(SA) end + } + + // add to refresh list + trap_R_AddRefEntityToScene( &ent ); + + memcpy( ¢->refEnt, &ent, sizeof( refEntity_t ) ); +} + +/* +================== +CG_Speaker + +Speaker entities can automatically play sounds +================== +*/ +static void CG_Speaker( centity_t *cent ) { + if ( !cent->currentState.clientNum ) { // FIXME: use something other than clientNum... + return; // not auto triggering + } + + if ( cg.time < cent->miscTime ) { + return; + } + + trap_S_StartSound( NULL, cent->currentState.number, CHAN_ITEM, cgs.gameSounds[cent->currentState.eventParm] ); + + // ent->s.frame = ent->wait * 10; + // ent->s.clientNum = ent->random * 10; + cent->miscTime = cg.time + cent->currentState.frame * 100 + cent->currentState.clientNum * 100 * crandom(); +} + + + +/* +============== +CG_DrawHoldableSelect + + This, of course, will all change when we've got a hud, but for now it makes the holdable items usable and not bad looking +============== +*/ +void CG_DrawHoldableSelect( void ) { + int bits; + int count; + int amount; + int i, x, y, w; + float *color; + char *name; + gitem_t *item; + + // don't display if dead + if ( cg.predictedPlayerState.stats[STAT_HEALTH] <= 0 ) { + return; + } + + color = CG_FadeColor( cg.holdableSelectTime, HOLDABLE_SELECT_TIME ); + if ( !color ) { + return; + } + trap_R_SetColor( color ); + + // showing select clears pickup item display, but not the blend blob + cg.itemPickupTime = 0; + + // count the number of holdables owned + bits = cg.snap->ps.stats[ STAT_HOLDABLE_ITEM ]; + count = 0; + + for ( i = 1 ; i <= HI_BOOK3; i++ ) { + if ( bits & ( 1 << i ) ) { + if ( cg.predictedPlayerState.holdable[i] ) { // don't show ones we're out of + count++; + } + } + } + + + if ( !count ) { + return; + } + + x = 320 - count * 20; + y = 370; + + + for ( i = 1 ; i <= HI_BOOK3 ; i++ ) { + if ( !( bits & ( 1 << i ) ) ) { + continue; + } + + amount = cg.predictedPlayerState.holdable[i]; + + if ( !amount ) { + continue; + } + + item = BG_FindItemForHoldable( i ); + if ( !item ) { + continue; + } + + CG_RegisterItemVisuals( item - bg_itemlist ); + + // draw icon + if ( i == HI_WINE ) { + // wine icons have three stages since each bottle has three uses (as opposed to others so far where there's only 1 use) + int wine = amount; + if ( wine > 3 ) { + wine = 3; + } + CG_DrawPic( x, y, 32, 32, cg_items[item - bg_itemlist].icons[ 2 - ( wine - 1 ) ] ) ; + } else { + CG_DrawPic( x, y, 32, 32, cg_items[item - bg_itemlist].icons[0] ); + } + + // draw remaining uses if there's more than one + if ( amount > 1 ) { + CG_DrawBigStringColor( x + 6, y + 40, va( "%d", amount ), color ); + } + + // draw selection marker + if ( i == cg.holdableSelect ) { + CG_DrawPic( x - 4, y - 4, 40, 40, cgs.media.selectShader ); + } + + x += 40; + } + + // draw the selected name + if ( cg.holdableSelect ) { + item = BG_FindItemForHoldable( cg.holdableSelect ); + if ( item ) { + name = cgs.itemPrintNames[ item - bg_itemlist ]; + if ( name ) { + //----(SA) trying smaller text +// w = CG_DrawStrlen( name ) * BIGCHAR_WIDTH; + w = CG_DrawStrlen( name ) * 10; + x = ( SCREEN_WIDTH - w ) / 2; +// CG_DrawBigStringColor(x, y - 22, name, color); + CG_DrawStringExt2( x, y + 74, name, color, qfalse, qtrue, 10, 10, 0 ); +// CG_Text_Paint(x, y + 74, 2, 0.3f, color, name, 0, 0, 6); // ITEM_TEXTSTYLE_SHADOWEDMORE + } + } + } + + trap_R_SetColor( NULL ); +} + + +/* +============== +CG_NextItem_f +============== +*/ + +void CG_NextItem_f( void ) { + int i; + int original, next; + + if ( !cg.snap ) { + return; + } + + if ( cg.snap->ps.pm_flags & PMF_FOLLOW ) { + return; + } + + cg.holdableSelectTime = cg.time; + cg.weaponSelectTime = 0; // (SA) clear weapon selection drawing + + next = original = cg.holdableSelect; + + for ( i = 0 ; i < HI_NUM_HOLDABLE ; i++ ) { + next++; + + if ( next == HI_NUM_HOLDABLE ) { + next = 0; + } + + if ( cg.predictedPlayerState.holdable[next] ) { //----(SA) + break; + } + } + + if ( i == HI_NUM_HOLDABLE ) { + next = original; + } + + cg.holdableSelect = next; +} + +/* +============== +CG_PrevItem_f +============== +*/ +void CG_PrevItem_f( void ) { + int i; + int original, next; + + if ( !cg.snap ) { + return; + } + + if ( cg.snap->ps.pm_flags & PMF_FOLLOW ) { + return; + } + + cg.weaponSelectTime = 0; // (SA) clear weapon selection drawing + cg.holdableSelectTime = cg.time; + + next = original = cg.holdableSelect; + + for ( i = 0 ; i < HI_NUM_HOLDABLE ; i++ ) { + next--; + + if ( next == -1 ) { + next = HI_NUM_HOLDABLE - 1; + } + + if ( cg.predictedPlayerState.holdable[next] ) { //----(SA) + break; + } + } + + if ( i == HI_NUM_HOLDABLE ) { + next = original; + } + + cg.holdableSelect = next; +} + +/* +============== +CG_Item_f +============== +*/ +void CG_Item_f( void ) { + int num; + num = atoi( CG_Argv( 1 ) ); + + cg.holdableSelectTime = cg.time; + + CG_Printf( "Item set to: d\n", num ); +} + + + +//----(SA) added +/* +============== +CG_HoldableUsedupChange +============== +*/ +void CG_HoldableUsedupChange( void ) { + int holding; + + holding = cg.holdableSelect; + + CG_NextItem_f(); + + if ( cg.holdableSelect == holding ) { // nothing else to go to + cg.holdableSelect = 0; + cg.weaponSelectTime = 0; + return; + } +} +//----(SA) end + + + +qboolean CG_PlayerSeesItem( playerState_t *ps, entityState_t *item, int atTime, int itemType ) { + vec3_t vorigin, eorigin, viewa, dir; + float dot, dist, foo; + trace_t tr; + + BG_EvaluateTrajectory( &item->pos, atTime, eorigin ); + + VectorCopy( ps->origin, vorigin ); + vorigin[2] += ps->viewheight; // get the view loc up to the viewheight +// eorigin[2] += 8; // and subtract the item's offset (that is used to place it on the ground) + + + VectorSubtract( vorigin, eorigin, dir ); + + dist = VectorNormalize( dir ); // dir is now the direction from the item to the player + + if ( dist > 255 ) { + return qfalse; // only run the remaining stuff on items that are close enough + + } + // (SA) FIXME: do this without AngleVectors. + // It'd be nice if the angle vectors for the player + // have already been figured at this point and I can + // just pick them up. (if anybody is storing this somewhere, + // for the current frame please let me know so I don't + // have to do redundant calcs) + AngleVectors( ps->viewangles, viewa, 0, 0 ); + dot = DotProduct( viewa, dir ); + + // give more range based on distance (the hit area is wider when closer) + +// foo = -0.94f - (dist/255.0f) * 0.057f; // (ranging from -0.94 to -0.997) (it happened to be a pretty good range) + foo = -0.94f - ( dist * ( 1.0f / 255.0f ) ) * 0.057f; // (ranging from -0.94 to -0.997) (it happened to be a pretty good range) + +/// Com_Printf("test: if(%f > %f) return qfalse (dot > foo)\n", dot, foo); + if ( dot > foo ) { + return qfalse; + } + + // (SA) okay, everything else is okay, so do a bloody trace. (so coronas on treasure doesn't show through walls) + if ( itemType == IT_TREASURE ) { + CG_Trace( &tr, vorigin, NULL, NULL, eorigin, -1, MASK_SOLID ); + + if ( tr.fraction != 1 ) { + return qfalse; + } + } + + return qtrue; + +} + + +/* +================== +CG_Item +================== +*/ +static void CG_Item( centity_t *cent ) { + refEntity_t ent; + entityState_t *es; + gitem_t *item; + float scale; + qboolean hasStand, highlight; + float highlightFadeScale = 1.0f; + + es = ¢->currentState; + + hasStand = qfalse; + highlight = qfalse; + + // (item index is stored in es->modelindex for item) + + if ( es->modelindex >= bg_numItems ) { + CG_Error( "Bad item index %i on entity", es->modelindex ); + } + + // if set to invisible, skip + if ( !es->modelindex || ( es->eFlags & EF_NODRAW ) ) { + return; + } + + item = &bg_itemlist[ es->modelindex ]; + + if ( cg_simpleItems.integer && item->giType != IT_TEAM ) { + memset( &ent, 0, sizeof( ent ) ); + ent.reType = RT_SPRITE; + VectorCopy( cent->lerpOrigin, ent.origin ); + ent.radius = 14; + ent.customShader = cg_items[es->modelindex].icons[0]; + ent.shaderRGBA[0] = 255; + ent.shaderRGBA[1] = 255; + ent.shaderRGBA[2] = 255; + ent.shaderRGBA[3] = 255; + trap_R_AddRefEntityToScene( &ent ); + return; + } + + scale = 0.005 + cent->currentState.number * 0.00001; + + memset( &ent, 0, sizeof( ent ) ); + + ent.nonNormalizedAxes = qfalse; + + if ( item->giType == IT_WEAPON ) { + weaponInfo_t *weaponInfo = &cg_weapons[item->giTag]; + + if ( weaponInfo->standModel ) { + hasStand = qtrue; + } + + if ( hasStand ) { // first try to put the weapon on it's 'stand' + refEntity_t stand; + + memset( &stand, 0, sizeof( stand ) ); + stand.hModel = weaponInfo->standModel; + + if ( es->eFlags & EF_SPINNING ) { + if ( es->groundEntityNum == -1 || !es->groundEntityNum ) { // (SA) spinning with a stand will spin the stand and the attached weap (only when in the air) + VectorCopy( cg.autoAnglesSlow, cent->lerpAngles ); + VectorCopy( cg.autoAnglesSlow, cent->lastLerpAngles ); + } else { + VectorCopy( cent->lastLerpAngles, cent->lerpAngles ); // make a tossed weapon sit on the ground in a position that matches how it was yawed + } + } + + AnglesToAxis( cent->lerpAngles, stand.axis ); + VectorCopy( cent->lerpOrigin, stand.origin ); + + // scale the stand to match the weapon scale ( the weapon will also be scaled inside CG_PositionEntityOnTag() ) + VectorScale( stand.axis[0], 1.5, stand.axis[0] ); + VectorScale( stand.axis[1], 1.5, stand.axis[1] ); + VectorScale( stand.axis[2], 1.5, stand.axis[2] ); + +//----(SA) modified + if ( cent->currentState.frame ) { + CG_PositionEntityOnTag( &ent, &stand, va( "tag_stand%d", cent->currentState.frame ), 0, NULL ); + } else { + CG_PositionEntityOnTag( &ent, &stand, "tag_stand", 0, NULL ); + } +//----(SA) end + + VectorCopy( ent.origin, ent.oldorigin ); + ent.nonNormalizedAxes = qtrue; + + } else { // then default to laying it on it's side + if ( !cg_items[es->modelindex].models[2] ) { + cent->lerpAngles[2] += 90; + } + + AnglesToAxis( cent->lerpAngles, ent.axis ); + + // increase the size of the weapons when they are presented as items + VectorScale( ent.axis[0], 1.5, ent.axis[0] ); + VectorScale( ent.axis[1], 1.5, ent.axis[1] ); + VectorScale( ent.axis[2], 1.5, ent.axis[2] ); + ent.nonNormalizedAxes = qtrue; + + VectorCopy( cent->lerpOrigin, ent.origin ); + VectorCopy( cent->lerpOrigin, ent.oldorigin ); + + if ( es->eFlags & EF_SPINNING ) { // spinning will override the angles set by a stand + if ( es->groundEntityNum == -1 || !es->groundEntityNum ) { // (SA) spinning with a stand will spin the stand and the attached weap (only when in the air) + VectorCopy( cg.autoAnglesSlow, cent->lerpAngles ); + VectorCopy( cg.autoAnglesSlow, cent->lastLerpAngles ); + } else { + VectorCopy( cent->lastLerpAngles, cent->lerpAngles ); // make a tossed weapon sit on the ground in a position that matches how it was yawed + } + } + } + + } else { + AnglesToAxis( cent->lerpAngles, ent.axis ); + VectorCopy( cent->lerpOrigin, ent.origin ); + VectorCopy( cent->lerpOrigin, ent.oldorigin ); + + if ( es->eFlags & EF_SPINNING ) { // spinning will override the angles set by a stand + VectorCopy( cg.autoAnglesSlow, cent->lerpAngles ); + AxisCopy( cg.autoAxisSlow, ent.axis ); + } + } + + + if ( es->modelindex2 ) { // modelindex2 was specified for the ent, meaning it probably has an alternate model (as opposed to the one in the itemlist) + // try to load it first, and if it fails, default to the itemlist model + ent.hModel = cgs.gameModels[ es->modelindex2 ]; + } else { + if ( item->giType == IT_WEAPON && cg_items[es->modelindex].models[2] ) { // check if there's a specific model for weapon pickup placement + ent.hModel = cg_items[es->modelindex].models[2]; + } else if ( item->giType == IT_HEALTH || item->giType == IT_AMMO || item->giType == IT_POWERUP ) { + if ( es->density < ( 1 << 9 ) ) { // (10 bits of data transmission for density) + ent.hModel = cg_items[es->modelindex].models[es->density]; // multi-state powerups store their state in 'density' + + // add steam to 'hot' meals + if ( !Q_stricmp( item->classname, "item_health_turkey" ) ) { + if ( !cg_paused.integer ) { // don't add while paused + if ( !( rand() % 7 ) ) { + CG_ParticleImpactSmokePuffExtended( cgs.media.smokeParticleShader, cent->lerpOrigin, tv( 0,0,1 ), 6, 2000, 4, 10, 16, 0.2f ); + } + } + } + } else { + ent.hModel = cg_items[es->modelindex].models[0]; + } + } else { + ent.hModel = cg_items[es->modelindex].models[0]; + } + } + + //----(SA) find midpoint for highlight corona. + // Can't do it when item is registered since it wouldn't know about replacement model + if ( !( cent->usehighlightOrigin ) ) { + vec3_t mins, maxs, offset; + int i; + + trap_R_ModelBounds( ent.hModel, mins, maxs ); // get bounds + + for ( i = 0 ; i < 3 ; i++ ) { + offset[i] = mins[i] + 0.5 * ( maxs[i] - mins[i] ); // find object-space center + } + + VectorCopy( cent->lerpOrigin, cent->highlightOrigin ); // set 'midpoint' to origin + + for ( i = 0 ; i < 3 ; i++ ) { // adjust midpoint by offset and orientation + cent->highlightOrigin[i] += offset[0] * ent.axis[0][i] + + offset[1] * ent.axis[1][i] + + offset[2] * ent.axis[2][i]; + } + + cent->usehighlightOrigin = qtrue; + } + + // items without glow textures need to keep a minimum light value so they are always visible +// if ( ( item->giType == IT_WEAPON ) || ( item->giType == IT_ARMOR ) ) { + ent.renderfx |= RF_MINLIGHT; +// } + + // highlighting items the player looks at + if ( cg_drawCrosshairPickups.integer ) { + + + if ( cg_drawCrosshairPickups.integer == 2 ) { // '2' is 'force highlights' + highlight = qtrue; + } + + if ( CG_PlayerSeesItem( &cg.predictedPlayerState, es, cg.time, item->giType ) ) { + highlight = qtrue; + + if ( item->giType == IT_TREASURE ) { + trap_R_AddCoronaToScene( cent->highlightOrigin, 1, 0.85, 0.5, 2, cent->currentState.number, 1 ); //----(SA) add corona to treasure + } + } else { + if ( item->giType == IT_TREASURE ) { + trap_R_AddCoronaToScene( cent->highlightOrigin, 1, 0.85, 0.5, 2, cent->currentState.number, 0 ); //----(SA) "empty corona" for proper fades + } + } + +//----(SA) added fixed item highlight fading + + if ( highlight ) { + if ( !cent->highlighted ) { + cent->highlighted = qtrue; + cent->highlightTime = cg.time; + } + ent.hilightIntensity = ( ( cg.time - cent->highlightTime ) / 250.0f ) * highlightFadeScale; // .25 sec to brighten up + } else { + if ( cent->highlighted ) { + cent->highlighted = qfalse; + cent->highlightTime = cg.time; + } + ent.hilightIntensity = 1.0f - ( ( cg.time - cent->highlightTime ) / 1000.0f ) * highlightFadeScale; // 1 sec to dim down (diff in time causes problems if you quickly flip to/away from looking at the item) + } + + if ( ent.hilightIntensity < 0.25f ) { // leave a minlight + ent.hilightIntensity = 0.25f; + } + if ( ent.hilightIntensity > 1 ) { + ent.hilightIntensity = 1.0; + } + } +//----(SA) end + + + // add to refresh list + trap_R_AddRefEntityToScene( &ent ); +} + +//============================================================================ + +/* +=============== +CG_Missile +=============== +*/ + +extern void CG_RocketTrail( centity_t *ent, const weaponInfo_t *wi ); + +static void CG_Missile( centity_t *cent ) { + refEntity_t ent; + entityState_t *s1; + const weaponInfo_t *weapon; + + s1 = ¢->currentState; + if ( s1->weapon > WP_NUM_WEAPONS ) { + s1->weapon = 0; + } + weapon = &cg_weapons[s1->weapon]; + + // calculate the axis + VectorCopy( s1->angles, cent->lerpAngles ); + + // add trails + if ( cent->currentState.eType == ET_FP_PARTS + || cent->currentState.eType == ET_FIRE_COLUMN + || cent->currentState.eType == ET_FIRE_COLUMN_SMOKE + || cent->currentState.eType == ET_RAMJET ) { + CG_RocketTrail( cent, NULL ); + } else if ( weapon->missileTrailFunc ) { + weapon->missileTrailFunc( cent, weapon ); + } + + // add dynamic light + if ( weapon->missileDlight ) { + trap_R_AddLightToScene( cent->lerpOrigin, weapon->missileDlight, + weapon->missileDlightColor[0], weapon->missileDlightColor[1], weapon->missileDlightColor[2], 0 ); + } + +//----(SA) whoops, didn't mean to check it in with the missile flare + + // add missile sound + if ( weapon->missileSound ) { + vec3_t velocity; + + BG_EvaluateTrajectoryDelta( ¢->currentState.pos, cg.time, velocity ); + trap_S_AddLoopingSound( cent->currentState.number, cent->lerpOrigin, velocity, weapon->missileSound, 255 ); + } + + // create the render entity + memset( &ent, 0, sizeof( ent ) ); + VectorCopy( cent->lerpOrigin, ent.origin ); + VectorCopy( cent->lerpOrigin, ent.oldorigin ); + +//----(SA) removed plasma gun code as sp5 is taking that spot + + // flicker between two skins + ent.skinNum = cg.clientFrame & 1; + + if ( cent->currentState.eType == ET_FP_PARTS ) { + ent.hModel = cgs.gameModels[cent->currentState.modelindex]; + } else if ( cent->currentState.eType == ET_EXPLO_PART ) { + ent.hModel = cgs.gameModels[cent->currentState.modelindex]; + } else if ( cent->currentState.eType == ET_FLAMEBARREL ) { + ent.hModel = cgs.media.flamebarrel; + } else if ( cent->currentState.eType == ET_FIRE_COLUMN || cent->currentState.eType == ET_FIRE_COLUMN_SMOKE ) { + // it may have a model sometime in the future + ent.hModel = 0; + } else if ( cent->currentState.eType == ET_RAMJET ) { + ent.hModel = 0; + } + // ent.hModel = cgs.gameModels[cent->currentState.modelindex]; + else { + ent.hModel = weapon->missileModel; + } + ent.renderfx = weapon->missileRenderfx | RF_NOSHADOW; + + // convert direction of travel into axis + if ( VectorNormalize2( s1->pos.trDelta, ent.axis[0] ) == 0 ) { + ent.axis[0][2] = 1; + } + + // spin as it moves + if ( s1->pos.trType != TR_STATIONARY ) { + RotateAroundDirection( ent.axis, cg.time / 4 ); + } else { + RotateAroundDirection( ent.axis, s1->time ); + } + + // Rafael + // Added this since it may be a propExlosion + if ( ent.hModel ) { + // add to refresh list, possibly with quad glow + CG_AddRefEntityWithPowerups( &ent, s1->powerups, TEAM_FREE, s1, vec3_origin ); + } + +} + +/* +=============== +CG_ZombieSpit +=============== +*/ +static void CG_ZombieSpit( centity_t *cent ) { + refEntity_t ent; + entityState_t *s1; + const weaponInfo_t *weapon; + + s1 = ¢->currentState; + if ( s1->weapon > WP_NUM_WEAPONS ) { + s1->weapon = 0; + } + weapon = &cg_weapons[s1->weapon]; + + // calculate the axis + VectorCopy( s1->angles, cent->lerpAngles ); + + // create the render entity + memset( &ent, 0, sizeof( ent ) ); + VectorCopy( cent->lerpOrigin, ent.origin ); + VectorCopy( cent->lerpOrigin, ent.oldorigin ); + + // flicker between two skins + ent.skinNum = cg.clientFrame & 1; + + ent.hModel = cgs.media.zombieLoogie; + + ent.renderfx = weapon->missileRenderfx | RF_NOSHADOW; + + // convert direction of travel into axis + if ( VectorNormalize2( s1->pos.trDelta, ent.axis[0] ) == 0 ) { + ent.axis[0][2] = 1; + } + + // spin as it moves + if ( s1->pos.trType != TR_STATIONARY ) { + RotateAroundDirection( ent.axis, cg.time / 4 ); + } else { + RotateAroundDirection( ent.axis, s1->time ); + } + + // add to refresh list, possibly with quad glow + CG_AddRefEntityWithPowerups( &ent, s1->powerups, TEAM_FREE, s1, vec3_origin ); + + // smoke trail effect + CG_Particle_Bleed( cgs.media.smokePuffShader, ent.origin, vec3_origin, 1, 180 ); +} + +/* +=============== +CG_Bat + + RF, a bat now is actually a spirit +=============== +*/ +static void CG_Bat( centity_t *cent ) { + refEntity_t refent; + + memset( &refent, 0, sizeof( refent ) ); + +//CG_Printf("remove me!!"); +//cgs.media.zombieSpiritLoopSound = trap_S_RegisterSound( "sound/zombie/attack/spirit_loop.wav" ); +//cgs.media.ssSpiritSkullModel = trap_R_RegisterModel( "models/players/supersoldier/ssghost.md3" ); + + // add the visible entity and trail + memset( &refent, 0, sizeof( refent ) ); + refent.hModel = cgs.media.ssSpiritSkullModel; + refent.backlerp = 0; + refent.renderfx = RF_NOSHADOW | RF_MINLIGHT; //----(SA) + refent.reType = RT_MODEL; + refent.shaderRGBA[3] = 255; + VectorCopy( cent->lerpOrigin, refent.origin ); + AnglesToAxis( cent->currentState.angles, refent.axis ); + // draw it + trap_R_AddRefEntityToScene( &refent ); + // emit a sound + trap_S_AddLoopingSound( 0, refent.origin, vec3_origin, cgs.media.zombieSpiritLoopSound, 255 ); +} + +/* +=============== +CG_Crowbar +=============== +*/ +static void CG_Crowbar( centity_t *cent ) { + refEntity_t ent; + entityState_t *s1; + const weaponInfo_t *weapon; + + s1 = ¢->currentState; + if ( s1->weapon > WP_NUM_WEAPONS ) { + s1->weapon = 0; + } + weapon = &cg_weapons[s1->weapon]; + + // calculate the axis + VectorCopy( s1->angles, cent->lerpAngles ); + + // create the render entity + memset( &ent, 0, sizeof( ent ) ); + VectorCopy( cent->lerpOrigin, ent.origin ); + VectorCopy( cent->lerpOrigin, ent.oldorigin ); + + // flicker between two skins + ent.skinNum = cg.clientFrame & 1; + + ent.hModel = cgs.media.crowbar; + + ent.renderfx = weapon->missileRenderfx | RF_NOSHADOW; + + // convert direction of travel into axis + if ( VectorNormalize2( s1->pos.trDelta, ent.axis[0] ) == 0 ) { + ent.axis[0][2] = 1; + } + + // spin as it moves + if ( s1->pos.trType != TR_STATIONARY ) { + RotateAroundDirection( ent.axis, cg.time / 4 ); + } else { + RotateAroundDirection( ent.axis, s1->time ); + } + + // add to refresh list, possibly with quad glow + CG_AddRefEntityWithPowerups( &ent, s1->powerups, TEAM_FREE, s1, vec3_origin ); + +} + + +//----(SA) animation_t struct changed, so changes are to keep this working +static animation_t grabberAnims[] = { + {"", 0, 6, 6, 1000 / 5, 1000 / 5 }, // (main idle) + {"", 5, 21, 21, 1000 / 7, 1000 / 7 }, // (random idle) + {"", 25, 11, 0, 1000 / 15, 1000 / 15 }, // (attack big swipe) + {"", 35, 16, 0, 1000 / 15, 1000 / 15 }, // (attack small swipe) + {"", 50, 16, 0, 1000 / 15, 1000 / 15 }, // (attack grab) + {"", 66, 1, 0, 1000 / 15, 1000 / 15 } // (starting position) +}; + +//----(SA) added +// TTimo: unused +/* +static animation_t footlockerAnims[] = { + {"", 0, 1, 1, 1000/5, 1000/5 }, // (main idle) + {"", 0, 5, 5, 1000/5, 1000/5 }, // (lock rattle) + {"", 5, 6, 0, 1000/5, 1000/5 } // (break open) +}; +*/ +//----(SA) end + +// DHM - Nerve :: capture and hold flag + +static animation_t multi_flagpoleAnims[] = { + {"", 0, 1, 0, 1000 / 15, 1000 / 15 }, // (no flags, idle) + {"", 0, 15, 0, 1000 / 15, 1000 / 15 }, // (nazi flag rising) + {"", 490, 15, 0, 1000 / 15, 1000 / 15 }, // (american flag rising) + {"", 20, 211, 211, 1000 / 15, 1000 / 15 }, // (nazi flag raised) + {"", 255, 211, 211, 1000 / 15, 1000 / 15 }, // (american flag raised) + {"", 235, 15, 0, 1000 / 15, 1000 / 15 }, // (nazi switching to american) + {"", 470, 15, 0, 1000 / 15, 1000 / 15 } // (american switching to nazi) +}; + +// dhm - end + +extern void CG_RunLerpFrame( clientInfo_t *ci, lerpFrame_t *lf, int newAnimation, float speedScale ); + +///// +//////----(SA) okay, a whole bunch of changes in here. LMK if this breaks anything. I will fix. +///// I'm trying to test any potential breaks, but stuff could slip by. +///// + +/* +============== +CG_SetAnim +============== +*/ +static void CG_SetAnim( centity_t *cent, lerpFrame_t *lf, int newSequence ) { + + // transition animation + lf->animationNumber = newSequence; + lf->animation = &lf->cgAnim[newSequence]; + lf->animationTime = lf->frameTime + lf->animation->initialLerp; + +} + +//----(SA) added +/* +============== +CG_NewAnim +============== +*/ +static int CG_NewAnim( centity_t *cent, lerpFrame_t *lf, animation_t *anim, int initialSequence ) { + + lf->frameTime = + lf->oldFrameTime = cg.time; + + lf->cgAnim = anim; // set the pointer to the base of the animation array for this ent + // TODO: handle out of range? + lf->animation = &anim[initialSequence]; + + CG_SetAnim( cent, lf, initialSequence ); + + lf->frame = + lf->oldFrame = lf->animation->firstFrame; + + return 0; +} +//----(SA) end + +/* +============== +CG_RunAnim +============== +*/ +static void CG_RunAnim( centity_t *cent, int *frame, int *oldframe, float *backlerp ) { + + // transition to new anim if requested (rather than letting it get done in cg_runlerpframe() since that expects a player ent) + if ( ( cent->lerpFrame.animationNumber != cent->currentState.frame ) || !cent->lerpFrame.animation ) { + CG_SetAnim( cent, ¢->lerpFrame, cent->currentState.frame ); + } + + // run it + CG_RunLerpFrame( NULL, ¢->lerpFrame, 0, 1 ); + + *frame = cent->lerpFrame.frame; + *oldframe = cent->lerpFrame.oldFrame; + *backlerp = cent->lerpFrame.backlerp; +} + + +/* +============== +CG_Trap + // TODO: change from 'trap' to something else. 'trap' is a misnomer. it's actually used for other stuff too +============== +*/ +static void CG_Trap( centity_t *cent ) { + refEntity_t ent; + entityState_t *cs; + animation_t *trapAnim; + + trapAnim = &grabberAnims[0]; + + memset( &ent, 0, sizeof( ent ) ); + + cs = ¢->currentState; + + // initial setup. set pointer to animation table and setup anim + if ( !cent->lerpFrame.oldFrameTime ) { + + // DHM - Nerve :: teamNum specifies which set of animations to use (only 1 exists right now) + if ( cgs.gametype == GT_WOLF ) { + switch ( cent->currentState.teamNum ) { + + case 1: + trapAnim = &multi_flagpoleAnims[0]; +// lf->animation = &multi_flagpoleAnims[ cent->currentState.frame ]; + break; + default: + // Keep what was set above + break; + } + } + // dhm - end + + CG_NewAnim( cent, ¢->lerpFrame, trapAnim, cs->frame ); + } + + CG_RunAnim( cent, &ent.frame, &ent.oldframe, &ent.backlerp ); + + VectorCopy( cent->lerpOrigin, ent.origin ); + VectorCopy( cent->lerpOrigin, ent.oldorigin ); + + ent.hModel = cgs.gameModels[cs->modelindex]; + + AnglesToAxis( cent->lerpAngles, ent.axis ); + + trap_R_AddRefEntityToScene( &ent ); + + memcpy( ¢->refEnt, &ent, sizeof( refEntity_t ) ); +} +//----(SA) end + + +/* +============== +CG_Corona +============== +*/ +static void CG_Corona( centity_t *cent ) { + trace_t tr; + int r, g, b; + int dli; + int flags = 0; + qboolean behind = qfalse, + toofar = qfalse; + + float dot, dist; + vec3_t dir; + + if ( cg_coronas.integer == 0 ) { // if set to '0' no coronas + return; + } + + dli = cent->currentState.dl_intensity; + r = dli & 255; + g = ( dli >> 8 ) & 255; + b = ( dli >> 16 ) & 255; + + // only coronas that are in your PVS are being added + + VectorSubtract( cg.refdef.vieworg, cent->lerpOrigin, dir ); + + dist = VectorNormalize2( dir, dir ); + if ( dist > cg_coronafardist.integer ) { // performance variable cg_coronafardist will keep down super long traces + toofar = qtrue; + } + + dot = DotProduct( dir, cg.refdef.viewaxis[0] ); + if ( dot >= -0.6 ) { // assumes ~90 deg fov (SA) changed value to 0.6 (screen corner at 90 fov) + behind = qtrue; // use the dot to at least do trivial removal of those behind you. + } + // yeah, I could calc side planes to clip against, but would that be worth it? (much better than dumb dot>= thing?) + +// CG_Printf("dot: %f\n", dot); + + if ( cg_coronas.integer == 2 ) { // if set to '2' trace everything + behind = qfalse; + toofar = qfalse; + } + + + if ( !behind && !toofar ) { + CG_Trace( &tr, cg.refdef.vieworg, NULL, NULL, cent->lerpOrigin, -1, MASK_SOLID | CONTENTS_BODY ); // added blockage by players. not sure how this is going to be since this is their bb, not their model (too much blockage) + + if ( tr.fraction == 1 ) { + flags = 1; + } + + trap_R_AddCoronaToScene( cent->lerpOrigin, (float)r / 255.0f, (float)g / 255.0f, (float)b / 255.0f, (float)cent->currentState.density / 255.0f, cent->currentState.number, flags ); + } +} + + +/* +============== +CG_Efx +============== +*/ +extern void CG_Explodef( vec3_t origin, vec3_t dir, int mass, int type, qhandle_t sound, int forceLowGrav, qhandle_t shader, int parent, qboolean damage ); + +static void CG_Efx( centity_t *cent ) { + int i; + float rnd; + trace_t trace; + vec3_t perpvec; + vec3_t stickPoint; + float movePerUpdate; + + if ( cent->currentState.eType == ET_TESLA_EF ) { + rnd = cent->currentState.angles2[0]; + + for ( i = 0; i < MAX_TESLA_BOLTS; i++ ) { + if ( cent->boltTimes[i] < cg.time ) { + VectorSet( cent->boltLocs[i], crandom(), crandom(), crandom() ); + VectorNormalize2( cent->boltLocs[i], cent->boltLocs[i] ); + VectorMA( cent->currentState.origin2, rnd, cent->boltLocs[i], cent->boltLocs[i] ); + + cent->boltTimes[i] = cg.time + rand() % cent->currentState.time2; // hold this position for ~1 second ('stickytime' value is stored in .time2) + + // cut the bolt short if it collides w/ something + CG_Trace( &trace, cent->currentState.origin, NULL, NULL, cent->boltLocs[i], -1, MASK_SOLID | CONTENTS_BODY ); + + if ( trace.fraction < 1 ) { + // take damage + if ( trace.entityNum != ENTITYNUM_WORLD ) { +// CG_ClientDamage(trace.entityNum, cent->currentState.number, CLDMG_TESLA); +// cg_entities[trace.entityNum].pe.teslaDamagedTime = cg.time; + } + + VectorCopy( trace.endpos, cent->boltLocs[i] ); + + // store perpendicular vector so end can 'crawl' + PerpendicularVector( perpvec, trace.plane.normal ); + + RotatePointAroundVector( stickPoint, trace.plane.normal, perpvec, crandom() * 360 ); + + // scale it so it won't move too far with bolts that have long boltTimer's + movePerUpdate = 1.0f / (float)( cent->boltTimes[i] - cg.time ); + + // move a max of 64 away from the 'original' target location + VectorScale( stickPoint, movePerUpdate * trace.fraction * 64.0f, cent->boltCrawlDirs[i] ); + + } else { + VectorSet( cent->boltCrawlDirs[i], 0, 0, 0 ); + } + + } + } + + for ( i = 0; i < MAX_TESLA_BOLTS; i++ ) { + + if ( cent->boltCrawlDirs[0] || cent->boltCrawlDirs[1] || cent->boltCrawlDirs[2] ) { + VectorMA( cent->boltLocs[i], cent->boltTimes[i] - cg.time, cent->boltCrawlDirs[i], perpvec ); + } else { + VectorCopy( cent->boltLocs[i], perpvec ); + } + + CG_DynamicLightningBolt( cgs.media.lightningBoltShader, // shader + cent->currentState.origin, // start + perpvec, // end + cent->currentState.density, // numBolts + cent->currentState.frame, // maxWidth + qtrue, // fade + 1.0, // startAlpha + 0, // recursion + i * i * 2 ); // randseed + } + + // add dlight + if ( cent->currentState.dl_intensity ) { + int r, g, b; + int dli; + int randomness = cent->currentState.time2; + + if ( ( cg.time / 50 ) % ( randomness + ( cg.time % randomness ) ) == 0 ) { + dli = cent->currentState.dl_intensity; + r = dli & 255; + g = ( dli >> 8 ) & 255; + b = ( dli >> 16 ) & 255; + trap_R_AddLightToScene( cent->currentState.origin, cent->currentState.time, (float)r / 255.0f, (float)g / 255.0f, (float)b / 255.0f, 0 ); + } + } + } else if ( cent->currentState.eType == ET_SPOTLIGHT_EF ) { + + vec3_t targetpos, normalized_direction, direction; + float dist, fov = 90; + vec4_t color = {1, 1, 1, .1}; + int splinetarget = 0; + char *cs; + int time = 0; + + VectorCopy( cent->currentState.origin2, targetpos ); + + splinetarget = cent->overheatTime; + + time = cg.time; + if ( cent->currentState.frame == 1 ) { // dead, don't move + time = cent->currentState.time2; // 'time2' set when it died, so you have a good position + + + if ( rand() % 50 == 1 ) { + vec3_t angNorm; // normalized angles + VectorNormalize2( cent->lerpAngles, angNorm ); + // (origin, dir, speed, duration, count, 'randscale') + CG_AddBulletParticles( cent->lerpOrigin, angNorm, 2, 800, 4, 16.0f ); + trap_S_StartSound( NULL, cent->currentState.number, CHAN_AUTO, cgs.media.sparkSounds[0] ); + } + + // smoking dead light + if ( !cg_paused.integer ) { // don't add while paused + if ( !( rand() % 3 ) ) { + CG_ParticleImpactSmokePuffExtended( cgs.media.smokeParticleShader, cent->currentState.origin, tv( 0,0,1 ), 8, 1000, 8, 20, 20, 0.25f ); + } + } + } + + if ( !splinetarget ) { + cs = (char *)CG_ConfigString( CS_SPLINES + cent->currentState.density ); + cent->overheatTime = splinetarget = CG_LoadCamera( va( "cameras/%s.camera", cs ) ); + if ( splinetarget != -1 ) { + trap_startCamera( splinetarget, cg.time ); + } + } else { + vec3_t angles; + if ( splinetarget != -1 ) { + if ( trap_getCameraInfo( splinetarget, time, &targetpos, &angles, &fov ) ) { + } else { // loop + trap_startCamera( splinetarget, cg.time ); + trap_getCameraInfo( splinetarget, cg.time, &targetpos, &angles, &fov ); + } + } + } + + + normalized_direction[0] = direction[0] = targetpos[0] - cent->currentState.origin[0]; + normalized_direction[1] = direction[1] = targetpos[1] - cent->currentState.origin[1]; + normalized_direction[2] = direction[2] = targetpos[2] - cent->currentState.origin[2]; + + dist = VectorNormalize( normalized_direction ); + + if ( dist == 0 ) { + return; + } + + if ( cent->currentState.frame == 1 ) { // dead + if ( ( cg.time - cent->currentState.time2 ) < 100 ) { // it just died, throw some glass + CG_Explodef( cent->lerpOrigin, normalized_direction, 50, 1, cgs.media.sfx_bullet_glasshit[0], 1, 0, cent->currentState.number, qfalse ); + } + } + + CG_Spotlight( cent, color, cent->currentState.origin, normalized_direction, 999, 4096, 10, fov, 0 ); + + } else if ( cent->currentState.eType == ET_EFFECT3 ) { + vec3_t forward; + // tag attached emitter + + // messed up tag attachment rotation (forward for a tag is not forward in the world) + forward[0] = cent->lerpAngles[1]; + forward[1] = cent->lerpAngles[0]; + forward[2] = cent->lerpAngles[2]; + +// CG_FireFlameChunks( cent, cent->lerpOrigin, forward, 1.0, qtrue, 1 ); + CG_FireFlameChunks( cent, cent->currentState.pos.trBase, forward, 1.0, qtrue, 1 ); + } +} + + +//----(SA) adding func_explosive + +/* +=============== +CG_Explosive + This is currently almost exactly the same as CG_Mover + It's split out so that any changes or experiments are + unattached to anything else. +=============== +*/ +static void CG_Explosive( centity_t *cent ) { + lerpFrame_t *traplf; + refEntity_t ent; + entityState_t *s1; + + s1 = ¢->currentState; + + + // create the render entity + memset( &ent, 0, sizeof( ent ) ); + + traplf = ¢->lerpFrame; + +//----(SA) added animation stuff + if ( cent->currentState.modelindex2 ) { // there's a 'model2' + +// if(cent->currentState.effect3Time) { // if there's at least 1 frame specified, this is animated + + // first time initialize + if ( !cent->lerpFrame.oldFrameTime ) { + // note: effect3Time and density will be invalidated when this explodes. + // they get re-used for explosion parameters. seems like this + // could be re-arranged a bit better in the future. + // (this is why the above check for currentstate.effect3Time is commented out) + cent->centAnim[0].name[0] = 0; + cent->centAnim[0].firstFrame = 0; + cent->centAnim[0].numFrames = cent->currentState.effect3Time; + cent->centAnim[0].loopFrames = cent->currentState.effect3Time; + cent->centAnim[0].frameLerp = 1000.0f / 15.0f; + cent->centAnim[0].initialLerp = 1000.0f / 15.0f; + + cent->centAnim[1].name[0] = 0; + cent->centAnim[1].firstFrame = cent->currentState.effect3Time; + cent->centAnim[1].numFrames = cent->currentState.density; + cent->centAnim[1].loopFrames = 0; + cent->centAnim[1].frameLerp = 1000.0f / 15.0f; + cent->centAnim[1].initialLerp = 1000.0f / 15.0f; + + CG_NewAnim( cent, ¢->lerpFrame, ¢->centAnim[0], s1->frame ); + } + + CG_RunAnim( cent, &ent.frame, &ent.oldframe, &ent.backlerp ); +// } + } +//----(SA) end + + VectorCopy( cent->lerpOrigin, ent.origin ); + VectorCopy( cent->lerpOrigin, ent.oldorigin ); + + // fade out + if ( cent->currentState.time ) { + ent.fadeStartTime = cent->currentState.time; + ent.fadeEndTime = cent->currentState.time2; + } + + // TODO: need to set lightingOrigin? + +// AnglesToAxis( cent->lerpAngles, ent.axis ); + AnglesToAxis( cent->currentState.angles, ent.axis ); + + ent.renderfx = RF_NOSHADOW; + + // get the model, either as a bmodel or a modelindex + if ( s1->solid == SOLID_BMODEL ) { + ent.hModel = cgs.inlineDrawModel[s1->modelindex]; + } else { + ent.hModel = cgs.gameModels[s1->modelindex]; + } + + // add to refresh list + // trap_R_AddRefEntityToScene(&ent); + + // add the secondary model + if ( s1->modelindex2 ) { + ent.skinNum = 0; + ent.hModel = cgs.gameModels[s1->modelindex2]; + trap_R_AddRefEntityToScene( &ent ); + } else { + trap_R_AddRefEntityToScene( &ent ); + } + +} + +//----(SA) done + +/* +=============== +CG_Mover +=============== +*/ +static void CG_Mover( centity_t *cent ) { + refEntity_t ent; + entityState_t *s1; + + s1 = ¢->currentState; + + // create the render entity + memset( &ent, 0, sizeof( ent ) ); + + VectorCopy( cent->lerpOrigin, ent.origin ); + VectorCopy( cent->lerpOrigin, ent.oldorigin ); + + AnglesToAxis( cent->lerpAngles, ent.axis ); + + ent.renderfx = RF_NOSHADOW; + + // flicker between two skins (FIXME?) + ent.skinNum = 0; +// ent.skinNum = ( cg.time >> 6 ) & 1; + + // get the model, either as a bmodel or a modelindex + if ( s1->solid == SOLID_BMODEL ) { + ent.hModel = cgs.inlineDrawModel[s1->modelindex]; + } else { + ent.hModel = cgs.gameModels[s1->modelindex]; + } + + // add to refresh list + //trap_R_AddRefEntityToScene(&ent); + + + // Rafael + // testing for mike to get movers to scale + if ( cent->currentState.density == ET_MOVERSCALED ) { + VectorScale( ent.axis[0], cent->currentState.angles2[0], ent.axis[0] ); + VectorScale( ent.axis[1], cent->currentState.angles2[1], ent.axis[1] ); + VectorScale( ent.axis[2], cent->currentState.angles2[2], ent.axis[2] ); + ent.nonNormalizedAxes = qtrue; + } + + +//----(SA) added + if ( cent->currentState.eType == ET_ALARMBOX ) { + ent.renderfx |= RF_MINLIGHT; + } +//----(SA) end + + + // add the secondary model + if ( s1->modelindex2 ) { + ent.skinNum = 0; + ent.hModel = cgs.gameModels[s1->modelindex2]; + // set frame + ent.frame = s1->frame; + ent.oldframe = ent.frame; + ent.backlerp = 0; + // RF, add interpolation + if ( ent.frame && ( cent->currentState.eFlags & EF_MOVER_ANIMATE ) ) { + ent.oldframe -= 1; + ent.backlerp = 1 - cg.frameInterpolation; + } + trap_R_AddRefEntityToScene( &ent ); + memcpy( ¢->refEnt, &ent, sizeof( refEntity_t ) ); + } else { + trap_R_AddRefEntityToScene( &ent ); + } + + // add propeller and sfx to me109 + if ( cent->currentState.density == 7 || cent->currentState.density == 8 ) { + refEntity_t propeller; + vec3_t angles; + + memset( &propeller, 0, sizeof( propeller ) ); + VectorCopy( ent.lightingOrigin, propeller.lightingOrigin ); + propeller.shadowPlane = ent.shadowPlane; + propeller.renderfx = ent.renderfx; + + propeller.hModel = propellerModel; + + angles[PITCH] = cg.time % 16; + + AnglesToAxis( angles, propeller.axis ); + + CG_PositionRotatedEntityOnTag( &propeller, &ent, "tag_prop" ); + + trap_R_AddRefEntityToScene( &propeller ); + + if ( cent->currentState.density == 8 ) { + refEntity_t flash; + vec3_t angles; + + angles[YAW] = 90; + angles[ROLL] = random() * 90; + + memset( &flash, 0, sizeof( flash ) ); + flash.renderfx = ent.shadowPlane; + //flash.hModel = cgs.media.mg42muzzleflash; + flash.hModel = cgs.media.planemuzzleflash; + + AnglesToAxis( angles, flash.axis ); + CG_PositionRotatedEntityOnTag( &flash, &ent, "tag_gun1" ); + + trap_R_AddRefEntityToScene( &flash ); + trap_R_AddLightToScene( flash.origin, 200 + ( rand() & 31 ),1.0, 0.6, 0.23, 0 ); + + memset( &flash, 0, sizeof( flash ) ); + flash.renderfx = ent.shadowPlane; + //flash.hModel = cgs.media.mg42muzzleflash; + flash.hModel = cgs.media.planemuzzleflash; + + AnglesToAxis( angles, flash.axis ); + CG_PositionRotatedEntityOnTag( &flash, &ent, "tag_gun02" ); + + trap_R_AddRefEntityToScene( &flash ); + trap_R_AddLightToScene( flash.origin, 200 + ( rand() & 31 ),1.0, 0.6, 0.23, 0 ); + } + } + + // alarm box spark effects + + // (SA) I'd like to do this as an entity flag (EF_SPARKING or something) and have it randomly + // pick a vert on the model and have a few sparks at that point, jumping away from the core (geometric center or lerporigin) + // of the model. Then we could use it generally for sparking ents/characters/character gibs/etc + // (since we've got lots of electric based characters) + + if ( cent->currentState.eType == ET_ALARMBOX ) { + if ( cent->currentState.frame == 2 ) { // i'm dead + if ( rand() % 50 == 1 ) { + vec3_t angNorm; // normalized angles + VectorNormalize2( cent->lerpAngles, angNorm ); + // (origin, dir, speed, duration, count, 'randscale') + CG_AddBulletParticles( cent->lerpOrigin, angNorm, 2, 800, 4, 16.0f ); + trap_S_StartSound( NULL, cent->currentState.number, CHAN_AUTO, cgs.media.sparkSounds[0] ); + } + } + } +} + +/* +=============== +CG_Beam + +Also called as an event +=============== +*/ +void CG_Beam( centity_t *cent ) { + refEntity_t ent; + entityState_t *s1; + + s1 = ¢->currentState; + + // create the render entity + memset( &ent, 0, sizeof( ent ) ); + VectorCopy( s1->pos.trBase, ent.origin ); + VectorCopy( s1->origin2, ent.oldorigin ); + + AxisClear( ent.axis ); + ent.reType = RT_BEAM; + + ent.renderfx = RF_NOSHADOW; + + // add to refresh list + trap_R_AddRefEntityToScene( &ent ); +} + + +/* +=============== +CG_Portal +=============== +*/ +static void CG_Portal( centity_t *cent ) { + refEntity_t ent; + entityState_t *s1; + + s1 = ¢->currentState; + + // create the render entity + memset( &ent, 0, sizeof( ent ) ); + VectorCopy( cent->lerpOrigin, ent.origin ); + VectorCopy( s1->origin2, ent.oldorigin ); + ByteToDir( s1->eventParm, ent.axis[0] ); + PerpendicularVector( ent.axis[1], ent.axis[0] ); + + // negating this tends to get the directions like they want + // we really should have a camera roll value + VectorSubtract( vec3_origin, ent.axis[1], ent.axis[1] ); + + CrossProduct( ent.axis[0], ent.axis[1], ent.axis[2] ); + ent.reType = RT_PORTALSURFACE; + ent.frame = s1->frame; // rotation speed + ent.skinNum = s1->clientNum / 256.0 * 360; // roll offset + + // add to refresh list + trap_R_AddRefEntityToScene( &ent ); +} + +/* +=============== +CG_Prop +=============== +*/ +static void CG_Prop( centity_t *cent ) { + refEntity_t ent; + entityState_t *s1; + vec3_t angles; + float scale; + + s1 = ¢->currentState; + + if ( cg.cameraMode ) { // don't render chair in hands when in cinematic + return; + } + + // create the render entity + memset( &ent, 0, sizeof( ent ) ); + + if ( cg.renderingThirdPerson ) { + VectorCopy( cent->lerpOrigin, ent.origin ); + VectorCopy( cent->lerpOrigin, ent.oldorigin ); + + ent.frame = s1->frame; + ent.oldframe = ent.frame; + ent.backlerp = 0; + } else + { + VectorCopy( cg.refdef.vieworg, ent.origin ); + VectorCopy( cg.refdefViewAngles, angles ); + + if ( cg.bobcycle & 1 ) { + scale = -cg.xyspeed; + } else { + scale = cg.xyspeed; + } + + // modify angles from bobbing + angles[ROLL] += scale * cg.bobfracsin * 0.005; + angles[YAW] += scale * cg.bobfracsin * 0.01; + angles[PITCH] += cg.xyspeed * cg.bobfracsin * 0.005; + + VectorCopy( angles, cent->lerpAngles ); + + ent.frame = s1->frame; + ent.oldframe = ent.frame; + ent.backlerp = 0; + + if ( cent->currentState.density ) { + ent.frame = s1->frame + cent->currentState.density; + ent.oldframe = ent.frame - 1; + ent.backlerp = 1 - cg.frameInterpolation; + ent.renderfx = RF_DEPTHHACK | RF_FIRST_PERSON; + + //CG_Printf ("frame %d oldframe %d\n", ent.frame, ent.oldframe); + } else if ( ent.frame ) { + ent.oldframe -= 1; + ent.backlerp = 1 - cg.frameInterpolation; + } else + { + ent.renderfx = RF_DEPTHHACK | RF_FIRST_PERSON; + } + } + + AnglesToAxis( cent->lerpAngles, ent.axis ); + + ent.renderfx |= RF_NOSHADOW; + + // flicker between two skins (FIXME?) + ent.skinNum = ( cg.time >> 6 ) & 1; + + // get the model, either as a bmodel or a modelindex + if ( s1->solid == SOLID_BMODEL ) { + ent.hModel = cgs.inlineDrawModel[s1->modelindex]; + } else { + ent.hModel = cgs.gameModels[s1->modelindex]; + } + + // add to refresh list + //trap_R_AddRefEntityToScene(&ent); + + // add the secondary model + if ( s1->modelindex2 ) { + ent.skinNum = 0; + ent.hModel = cgs.gameModels[s1->modelindex2]; + ent.frame = s1->frame; + trap_R_AddRefEntityToScene( &ent ); + memcpy( ¢->refEnt, &ent, sizeof( refEntity_t ) ); + } else { + trap_R_AddRefEntityToScene( &ent ); + } + +} + +/* +============== +CG_FlamethrowerProp +============== +*/ +void CG_FlamethrowerProp( centity_t *cent ) { + int old; + int flags; + + old = cent->currentState.aiChar; + cent->currentState.aiChar = AICHAR_ZOMBIE; + + if ( !( cent->currentState.eFlags & EF_FIRING ) ) { + return; + } + + flags = 1; // use angles + + if ( cent->currentState.density ) { + flags |= 2; // silent + + } + // shoot this only in bursts + +// (SA) this first one doesn't seem to do anything. ? + +// if ((cg.time+cent->currentState.number*100)%1000 > 200) { +// CG_FireFlameChunks( cent, cent->currentState.origin, cent->lerpAngles, 0.1, qfalse, flags ); +// CG_FireFlameChunks( cent, cent->currentState.origin, cent->currentState.apos.trBase, 0.1, qfalse, flags ); +// } +// else +// CG_FireFlameChunks( cent, cent->currentState.origin, cent->lerpAngles, 0.6, 2, flags ); + + CG_FireFlameChunks( cent, cent->currentState.origin, cent->currentState.apos.trBase, 0.6, 2, flags ); + + cent->currentState.aiChar = old; + +} + +/* +================ +CG_ProcessRumble +================ +*/ +void CG_ProcessRumble( centity_t *cent ) { + + if ( cent->currentState.angles[2] > cg.rumbleScale ) { + cg.rumbleScale = cent->currentState.angles[2]; + } + +} + +/* +========================= +CG_AdjustPositionForMover + +Also called by client movement prediction code +========================= +*/ +void CG_AdjustPositionForMover( const vec3_t in, int moverNum, int fromTime, int toTime, vec3_t out, vec3_t outDeltaAngles ) { + centity_t *cent; + vec3_t oldOrigin, origin, deltaOrigin; + vec3_t oldAngles, angles, deltaAngles; + + if ( outDeltaAngles ) { + VectorClear( outDeltaAngles ); + } + + if ( moverNum <= 0 || moverNum >= ENTITYNUM_MAX_NORMAL ) { + VectorCopy( in, out ); + return; + } + + cent = &cg_entities[ moverNum ]; + + if ( cent->currentState.eType != ET_MOVER ) { + VectorCopy( in, out ); + return; + } + + BG_EvaluateTrajectory( ¢->currentState.pos, fromTime, oldOrigin ); + BG_EvaluateTrajectory( ¢->currentState.apos, fromTime, oldAngles ); + + BG_EvaluateTrajectory( ¢->currentState.pos, toTime, origin ); + BG_EvaluateTrajectory( ¢->currentState.apos, toTime, angles ); + + VectorSubtract( origin, oldOrigin, deltaOrigin ); + VectorSubtract( angles, oldAngles, deltaAngles ); + + VectorAdd( in, deltaOrigin, out ); + if ( outDeltaAngles ) { + VectorCopy( deltaAngles, outDeltaAngles ); + } + + // FIXME: origin change when on a rotating object +} + + +/* +============================= +CG_InterpolateEntityPosition +============================= +*/ +static void CG_InterpolateEntityPosition( centity_t *cent ) { + vec3_t current, next; + float f; + + // it would be an internal error to find an entity that interpolates without + // a snapshot ahead of the current one + if ( cg.nextSnap == NULL ) { + CG_Error( "CG_InterpoateEntityPosition: cg.nextSnap == NULL" ); + } + + f = cg.frameInterpolation; + + // this will linearize a sine or parabolic curve, but it is important + // to not extrapolate player positions if more recent data is available + BG_EvaluateTrajectory( ¢->currentState.pos, cg.snap->serverTime, current ); + BG_EvaluateTrajectory( ¢->nextState.pos, cg.nextSnap->serverTime, next ); + + cent->lerpOrigin[0] = current[0] + f * ( next[0] - current[0] ); + cent->lerpOrigin[1] = current[1] + f * ( next[1] - current[1] ); + cent->lerpOrigin[2] = current[2] + f * ( next[2] - current[2] ); + + BG_EvaluateTrajectory( ¢->currentState.apos, cg.snap->serverTime, current ); + BG_EvaluateTrajectory( ¢->nextState.apos, cg.nextSnap->serverTime, next ); + + cent->lerpAngles[0] = LerpAngle( current[0], next[0], f ); + cent->lerpAngles[1] = LerpAngle( current[1], next[1], f ); + cent->lerpAngles[2] = LerpAngle( current[2], next[2], f ); + +} + +/* +=============== +CG_CalcEntityLerpPositions + +=============== +*/ +static void CG_CalcEntityLerpPositions( centity_t *cent ) { + if ( cent->interpolate && cent->currentState.pos.trType == TR_INTERPOLATE ) { + CG_InterpolateEntityPosition( cent ); + return; + } + + // just use the current frame and evaluate as best we can + BG_EvaluateTrajectory( ¢->currentState.pos, cg.time, cent->lerpOrigin ); + BG_EvaluateTrajectory( ¢->currentState.apos, cg.time, cent->lerpAngles ); + + // adjust for riding a mover if it wasn't rolled into the predicted + // player state + if ( cent != &cg.predictedPlayerEntity ) { + CG_AdjustPositionForMover( cent->lerpOrigin, cent->currentState.groundEntityNum, + cg.snap->serverTime, cg.time, cent->lerpOrigin, NULL ); + } +} + +/* +=============== +CG_ProcessEntity +=============== +*/ +static void CG_ProcessEntity( centity_t *cent ) { + switch ( cent->currentState.eType ) { + default: + CG_Error( "Bad entity type: %i\n", cent->currentState.eType ); + break; + case ET_CAMERA: + case ET_INVISIBLE: + case ET_PUSH_TRIGGER: + case ET_TELEPORT_TRIGGER: + case ET_AI_EFFECT: + case ET_LEAKY: //----(SA) added + case ET_SPIRIT_SPAWNER: + break; + case ET_RUMBLE: + CG_ProcessRumble( cent ); + break; + case ET_GAMEMODEL: + if ( !cg_drawGamemodels.integer ) { + break; + } + case ET_MG42: + if ( cent->currentState.frame == 2 ) { // dead + if ( !cg_paused.integer ) { // don't add while paused + if ( !( rand() % 7 ) ) { + CG_ParticleImpactSmokePuffExtended( cgs.media.smokeParticleShader, cent->lerpOrigin, tv( 0,0,1 ), 6, 2000, 4, 10, 16, 0.2f ); + } + } + } + + case ET_FOOTLOCKER: + case ET_GENERAL: + CG_General( cent ); + break; + case ET_PLAYER: + CG_Player( cent ); + break; + case ET_ITEM: + CG_Item( cent ); + break; + case ET_MISSILE: + case ET_FLAMEBARREL: + case ET_FP_PARTS: + case ET_FIRE_COLUMN: + case ET_FIRE_COLUMN_SMOKE: + case ET_EXPLO_PART: + case ET_RAMJET: + CG_Missile( cent ); + break; + case ET_CROWBAR: + CG_Crowbar( cent ); + break; + case ET_ZOMBIESPIT: + CG_ZombieSpit( cent ); + break; + case ET_TESLA_EF: + case ET_SPOTLIGHT_EF: + case ET_EFFECT3: + CG_Efx( cent ); + break; + case ET_EXPLOSIVE: + CG_Explosive( cent ); + break; + case ET_TRAP: + CG_Trap( cent ); + break; + case ET_ALARMBOX: + case ET_MOVER: + CG_Mover( cent ); + break; + case ET_PROP: + CG_Prop( cent ); + break; + case ET_BEAM: + CG_Beam( cent ); + break; + case ET_PORTAL: + CG_Portal( cent ); + break; + case ET_SPEAKER: + CG_Speaker( cent ); + break; + case ET_CORONA: + CG_Corona( cent ); + break; + case ET_BAT: + CG_Bat( cent ); + break; + case ET_FLAMETHROWER_PROP: + CG_FlamethrowerProp( cent ); + break; + } +} + +/* +=============== +CG_AddCEntity + +=============== +*/ +static void CG_AddCEntity( centity_t *cent ) { + // event-only entities will have been dealt with already + if ( cent->currentState.eType >= ET_EVENTS ) { + return; + } + + cent->processedFrame = cg.clientFrame; + + // calculate the current origin + CG_CalcEntityLerpPositions( cent ); + + // add automatic effects + CG_EntityEffects( cent ); + + // call the appropriate function which will add this entity to the view accordingly + CG_ProcessEntity( cent ); +} + +/* +================== +CG_AddEntityToTag +================== +*/ +static void CG_AddEntityToTag( centity_t *cent ) { + entityState_t *s1; + centity_t *centParent; + entityState_t *sParent; + refEntity_t ent; + char *cs, *token = NULL; + int i, pi; + vec3_t ang; + + memset( &ent, 0, sizeof( ent ) ); + + // event-only entities will have been dealt with already + if ( cent->currentState.eType >= ET_EVENTS ) { + return; + } + + if ( cent->processedFrame == cg.clientFrame ) { + // already processed this frame + return; + } + + // calculate the current origin + CG_CalcEntityLerpPositions( cent ); + + s1 = ¢->currentState; + + // if set to invisible, skip + //if (!s1->modelindex) { + // return; + //} + + // find us in the list of tagged entities + sParent = NULL; + centParent = NULL; + for ( i = CS_TAGCONNECTS + 1; i < CS_TAGCONNECTS + MAX_TAGCONNECTS; i++ ) { // NOTE: +1 since G_FindConfigStringIndex() starts at index 1 rather than 0 (not sure why) + cs = (char *)CG_ConfigString( i ); + token = COM_Parse( &cs ); + if ( !token[0] ) { + break; + } + if ( atoi( token ) == s1->number ) { + token = COM_Parse( &cs ); + if ( !token[0] ) { + CG_Error( "CG_EntityTagConnected: missing parameter in configstring" ); + } + pi = atoi( token ); + if ( pi < 0 || pi >= MAX_GENTITIES ) { + CG_Error( "CG_EntityTagConnected: parent out of range" ); + } + centParent = &cg_entities[pi]; + sParent = &( cg_entities[pi].currentState ); + token = COM_Parse( &cs ); + if ( !token[0] ) { + CG_Error( "CG_EntityTagConnected: missing parameter in configstring" ); + } + + // NOTE: token is now the tag name to attach to + + break; + } + } + + if ( !sParent ) { + //return; // assume the configstring hasn't arrived yet? + CG_Error( "CG_EntityTagConnected: unable to find configstring to perform connection" ); + } + + // if parent isn't visible, then don't draw us + if ( !centParent->currentValid ) { + return; + } + + // make sure all parents are added first + if ( centParent->processedFrame != cg.clientFrame ) { + if ( sParent->eFlags & EF_TAGCONNECT ) { + CG_AddEntityToTag( centParent ); + } + } + + // if there was a higher ranking parent not added to the scene, then don't add us + if ( centParent->processedFrame != cg.clientFrame ) { + return; + } + + cent->processedFrame = cg.clientFrame; + + // start with default axis + AnglesToAxis( vec3_origin, ent.axis ); + //AnglesToAxis( cent->lerpAngles, ent.axis ); + + // get the tag position from parent + CG_PositionEntityOnTag( &ent, ¢Parent->refEnt, token, 0, NULL ); +// CG_PositionRotatedEntityOnTag( &ent, ¢Parent->refEnt, centParent->refEnt.hModel, token ); + + VectorCopy( ent.origin, cent->lerpOrigin ); + // we need to add the child's angles to the tag angles + //if (!cent->currentState.density) { // this entity should rotate with it's parent, but can turn around using it's own angles + AxisToAngles( ent.axis, ang ); + VectorAdd( cent->lerpAngles, ang, cent->lerpAngles ); + //} else { // face our angles exactly + // BG_EvaluateTrajectory( ¢->currentState.apos, cg.time, cent->lerpAngles ); + //} + + // add automatic effects + CG_EntityEffects( cent ); + + // call the appropriate function which will add this entity to the view accordingly + CG_ProcessEntity( cent ); +} + +/* +=============== +CG_AddPacketEntities + +=============== +*/ +void CG_AddPacketEntities( void ) { + int num; + centity_t *cent; + playerState_t *ps; + int clcount; + + // set cg.frameInterpolation + if ( cg.nextSnap ) { + int delta; + + delta = ( cg.nextSnap->serverTime - cg.snap->serverTime ); + if ( delta == 0 ) { + cg.frameInterpolation = 0; + } else { + cg.frameInterpolation = (float)( cg.time - cg.snap->serverTime ) / delta; + } + } else { + cg.frameInterpolation = 0; // actually, it should never be used, because + // no entities should be marked as interpolating + } + + cg.rumbleScale = 0.0; // RF, default to 0 each frame + + // the auto-rotating items will all have the same axis + cg.autoAnglesSlow[0] = 0; + cg.autoAnglesSlow[1] = ( cg.time & 4095 ) * 360 / 4095.0; + cg.autoAnglesSlow[2] = 0; + + cg.autoAngles[0] = 0; + cg.autoAngles[1] = ( cg.time & 2047 ) * 360 / 2048.0; + cg.autoAngles[2] = 0; + + cg.autoAnglesFast[0] = 0; + cg.autoAnglesFast[1] = ( cg.time & 1023 ) * 360 / 1024.0f; + cg.autoAnglesFast[2] = 0; + + AnglesToAxis( cg.autoAnglesSlow, cg.autoAxisSlow ); + AnglesToAxis( cg.autoAngles, cg.autoAxis ); + AnglesToAxis( cg.autoAnglesFast, cg.autoAxisFast ); + + // generate and add the entity from the playerstate + ps = &cg.predictedPlayerState; + BG_PlayerStateToEntityState( ps, &cg.predictedPlayerEntity.currentState, qfalse ); + CG_AddCEntity( &cg.predictedPlayerEntity ); + + // lerp the non-predicted value for lightning gun origins + CG_CalcEntityLerpPositions( &cg_entities[ cg.snap->ps.clientNum ] ); + + // add each entity sent over by the server + + + // RF, count the number of players in the scene, so we can force low LOD's for dead bodies + for ( num = 0, clcount = 0 ; num < cg.snap->numEntities ; num++ ) { + cent = &cg_entities[ cg.snap->entities[ num ].number ]; + cent->pe.forceLOD = qfalse; + if ( cent->currentState.number < MAX_CLIENTS ) { + clcount++; + } + } + + // NON TAG-CONNECTED ENTITIES + for ( num = 0 ; num < cg.snap->numEntities ; num++ ) { + cent = &cg_entities[ cg.snap->entities[ num ].number ]; + if ( !( cent->currentState.eFlags & EF_TAGCONNECT ) ) { +//----(SA) commented this out for DM and Dom +// if (cent->currentState.number < MAX_CLIENTS) { +// if ((clcount > 2) && (cent->currentState.eFlags & EF_DEAD)) { +// cent->pe.forceLOD = qtrue; +// } +// } +//----(SA) end + CG_AddCEntity( cent ); + } + } + + // TAG-CONNECTED ENTITIES (connected to NON TAG-CONNECTED ENTITIES) + for ( num = 0 ; num < cg.snap->numEntities ; num++ ) { + cent = &cg_entities[ cg.snap->entities[ num ].number ]; + if ( cent->currentState.eFlags & EF_TAGCONNECT ) { + CG_AddEntityToTag( cent ); + } + } + + // Ridah, add the flamethrower sounds + CG_UpdateFlamethrowerSounds(); +} diff --git a/src/cgame/cg_event.c b/src/cgame/cg_event.c new file mode 100644 index 0000000..a26961e --- /dev/null +++ b/src/cgame/cg_event.c @@ -0,0 +1,2882 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + + +// cg_event.c -- handle entity events at snapshot or playerstate transitions + +#include "cg_local.h" +#include "../ui/ui_shared.h" // for Menus_CloseAll() + +extern int hWeaponSnd; + +extern void CG_Tracer( vec3_t source, vec3_t dest, int sparks ); +//========================================================================== + +/* +=================== +CG_PlaceString + +Also called by scoreboard drawing +=================== +*/ +const char *CG_PlaceString( int rank ) { + static char str[64]; + char *s, *t; + + if ( rank & RANK_TIED_FLAG ) { + rank &= ~RANK_TIED_FLAG; + t = "Tied for "; + } else { + t = ""; + } + + if ( rank == 1 ) { + s = S_COLOR_BLUE "1st" S_COLOR_WHITE; // draw in blue + } else if ( rank == 2 ) { + s = S_COLOR_RED "2nd" S_COLOR_WHITE; // draw in red + } else if ( rank == 3 ) { + s = S_COLOR_YELLOW "3rd" S_COLOR_WHITE; // draw in yellow + } else if ( rank == 11 ) { + s = "11th"; + } else if ( rank == 12 ) { + s = "12th"; + } else if ( rank == 13 ) { + s = "13th"; + } else if ( rank % 10 == 1 ) { + s = va( "%ist", rank ); + } else if ( rank % 10 == 2 ) { + s = va( "%ind", rank ); + } else if ( rank % 10 == 3 ) { + s = va( "%ird", rank ); + } else { + s = va( "%ith", rank ); + } + + Com_sprintf( str, sizeof( str ), "%s%s", t, s ); + return str; +} + +/* +============= +CG_Obituary +============= +*/ +static void CG_Obituary( entityState_t *ent ) { + int mod; + int target, attacker; + char *message; + char *message2; + const char *targetInfo; + const char *attackerInfo; + char targetName[32]; + char attackerName[32]; + gender_t gender; + clientInfo_t *ci; + + // Ridah, no obituaries in single player + if ( cgs.gametype == GT_SINGLE_PLAYER ) { + return; + } + + target = ent->otherEntityNum; + attacker = ent->otherEntityNum2; + mod = ent->eventParm; + + if ( target < 0 || target >= MAX_CLIENTS ) { + CG_Error( "CG_Obituary: target out of range" ); + } + ci = &cgs.clientinfo[target]; + + if ( attacker < 0 || attacker >= MAX_CLIENTS ) { + attacker = ENTITYNUM_WORLD; + attackerInfo = NULL; + } else { + attackerInfo = CG_ConfigString( CS_PLAYERS + attacker ); + } + + targetInfo = CG_ConfigString( CS_PLAYERS + target ); + if ( !targetInfo ) { + return; + } + Q_strncpyz( targetName, Info_ValueForKey( targetInfo, "n" ), sizeof( targetName ) - 2 ); + strcat( targetName, S_COLOR_WHITE ); + + message2 = ""; + + // check for single client messages + + switch ( mod ) { + case MOD_SUICIDE: + message = "suicides"; + break; + case MOD_FALLING: + message = "cratered"; + break; + case MOD_CRUSH: + message = "was squished"; + break; + case MOD_WATER: + message = "sank like a rock"; + break; + case MOD_SLIME: + message = "melted"; + break; + case MOD_LAVA: + message = "does a back flip into the lava"; + break; + case MOD_TARGET_LASER: + message = "saw the light"; + break; + case MOD_TRIGGER_HURT: + message = "was in the wrong place"; + break; + default: + message = NULL; + break; + } + + if ( attacker == target ) { + gender = ci->modelInfo->gender; + switch ( mod ) { + case MOD_GRENADE_SPLASH: + if ( gender == GENDER_FEMALE ) { + message = "tripped on her own grenade"; + } else if ( gender == GENDER_NEUTER ) { + message = "tripped on its own grenade"; + } else { + message = "tripped on his own grenade"; + } + break; + case MOD_ROCKET_SPLASH: + if ( gender == GENDER_FEMALE ) { + message = "blew herself up"; + } else if ( gender == GENDER_NEUTER ) { + message = "blew itself up"; + } else { + message = "blew himself up"; + } + break; + case MOD_BFG_SPLASH: + message = "should have used a smaller gun"; + break; + case MOD_EXPLOSIVE: + message = "died in an explosion"; + break; + default: + if ( gender == GENDER_FEMALE ) { + message = "killed herself"; + } else if ( gender == GENDER_NEUTER ) { + message = "killed itself"; + } else { + message = "killed himself"; + } + break; + } + } + + if ( message ) { + CG_Printf( "%s %s.\n", targetName, message ); + return; + } + + // check for kill messages from the current clientNum + if ( attacker == cg.snap->ps.clientNum ) { + char *s; + + if ( cgs.gametype < GT_TEAM ) { + s = va( "You fragged %s\n%s place with %i", targetName, + CG_PlaceString( cg.snap->ps.persistant[PERS_RANK] + 1 ), + cg.snap->ps.persistant[PERS_SCORE] ); + } else { + s = va( "You fragged %s", targetName ); + } + CG_CenterPrint( s, SCREEN_HEIGHT * 0.25, BIGCHAR_WIDTH ); + + // print the text message as well + } + + // check for double client messages + if ( !attackerInfo ) { + attacker = ENTITYNUM_WORLD; + strcpy( attackerName, "noname" ); + } else { + Q_strncpyz( attackerName, Info_ValueForKey( attackerInfo, "n" ), sizeof( attackerName ) - 2 ); + strcat( attackerName, S_COLOR_WHITE ); + // check for kill messages about the current clientNum + if ( target == cg.snap->ps.clientNum ) { + Q_strncpyz( cg.killerName, attackerName, sizeof( cg.killerName ) ); + } + } + + if ( attacker != ENTITYNUM_WORLD ) { + switch ( mod ) { + + // TODO: put real text here. these are just placeholders + + case MOD_KNIFE_STEALTH: + case MOD_KNIFE: + case MOD_KNIFE2: + message = "was knifed by"; + break; + case MOD_LUGER: + message = "was killed (luger) by"; + break; + case MOD_COLT: + message = "was killed (colt) by"; + break; + case MOD_MP40: + message = "was killed (mp40) by"; + break; + case MOD_THOMPSON: + message = "was killed (thompson) by"; + break; + case MOD_STEN: + message = "was killed (sten) by"; + break; + case MOD_MAUSER: + message = "was killed (mauser) by"; + break; + case MOD_SNIPERRIFLE: + message = "was killed (sniper) by"; + break; + case MOD_GARAND: + message = "was killed (garand) by"; + break; + case MOD_SNOOPERSCOPE: + message = "was killed (snooper) by"; + break; + case MOD_AKIMBO: + message = "was killed (dual colts) by"; + break; + case MOD_ROCKET_LAUNCHER: + message = "was killed (rl) by"; + break; + case MOD_GRENADE_LAUNCHER: + message = "was killed (gren - gm) by"; + break; + case MOD_VENOM: + message = "was killed (venom) by"; + break; + case MOD_VENOM_FULL: + message = "was killed (venom shot) by"; + break; + case MOD_FLAMETHROWER: + message = "was killed (flamethrower) by"; + break; + case MOD_TESLA: + message = "was killed (tesla) by"; + break; + case MOD_SPEARGUN: + message = "was killed (spear) by"; + break; + case MOD_SPEARGUN_CO2: + message = "was killed (co2 spear) by"; + break; + case MOD_GRENADE_PINEAPPLE: + message = "was killed (gren - am) by"; + break; + case MOD_CROSS: + message = "was killed (cross) by"; + break; +// JPW NERVE + case MOD_AIRSTRIKE: + message = "stood under"; + message2 = "'s air strike"; + break; +// jpw +// (SA) leaving a sample of two part obit's +// case MOD_ROCKET: +// message = "ate"; +// message2 = "'s rocket"; +// break; +// case MOD_ROCKET_SPLASH: +// message = "almost dodged"; +// message2 = "'s rocket"; +// break; + default: + message = "was killed by"; + break; + } + + if ( message ) { + CG_Printf( "%s %s %s%s\n", + targetName, message, attackerName, message2 ); + return; + } + } + + // we don't know what it was +// JPW NERVE added mod check for machinegun (prolly mortar here too) + switch ( mod ) { + case MOD_MACHINEGUN: + CG_Printf( "%s was riddled by machinegun fire\n",targetName ); + break; + default: + CG_Printf( "%s died.\n", targetName ); + break; + } +// jpw +} + +//========================================================================== + +/* +=============== +CG_UseItem +=============== +*/ +static void CG_UseItem( centity_t *cent ) { + int itemNum; + gitem_t *item; + entityState_t *es; + + es = ¢->currentState; + + // itemNum = es->event - EV_USE_ITEM0; + // JCash bluesnews reported fix + itemNum = ( es->event & ~EV_EVENT_BITS ) - EV_USE_ITEM0; + + if ( itemNum < 0 || itemNum > HI_NUM_HOLDABLE ) { + itemNum = 0; + } + + // print a message if the local player + if ( es->number == cg.snap->ps.clientNum ) { + if ( !itemNum ) { + CG_CenterPrint( "noitem", SCREEN_HEIGHT - ( SCREEN_HEIGHT * 0.25 ), SMALLCHAR_WIDTH ); //----(SA) modified + } else { + item = BG_FindItemForHoldable( itemNum ); + + if ( item ) { + cg.holdableSelectTime = cg.time; // show remaining items + + switch ( itemNum ) { + case HI_BOOK1: + case HI_BOOK2: + case HI_BOOK3: + break; + case HI_WINE: + CG_CenterPrint( "drankwine", SCREEN_HEIGHT - ( SCREEN_HEIGHT * 0.25 ), SMALLCHAR_WIDTH ); + break; + default: + CG_CenterPrint( va( "Use %s", cgs.itemPrintNames[item - bg_itemlist] ), SCREEN_HEIGHT - ( SCREEN_HEIGHT * 0.25 ), SMALLCHAR_WIDTH ); + break; + } + } + } + } + + switch ( itemNum ) { + default: + case HI_NONE: + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.useNothingSound ); + break; + + case HI_BOOK1: + case HI_BOOK2: + case HI_BOOK3: + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.bookSound ); + break; + + case HI_WINE: + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.wineSound ); + break; + + case HI_STAMINA: + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.staminaSound ); + break; + } + +} + +// from cg_weapons.c +extern int CG_WeaponIndex( int weapnum, int *bank, int *cycle ); + + +/* +================ +CG_ItemPickup + +A new item was picked up this frame +================ +*/ +static void CG_ItemPickup( int itemNum ) { + int itemid; + int wpbank_cur, wpbank_pickup; + qboolean selectIt; + + itemid = bg_itemlist[itemNum].giTag; + + cg.itemPickup = itemNum; + cg.itemPickupTime = cg.time; + cg.itemPickupBlendTime = cg.time; + + // see if it should be the grabbed weapon + if ( bg_itemlist[itemNum].giType == IT_WEAPON ) { + int weapon; + + selectIt = qfalse; + + weapon = itemid; + + if ( weapon == WP_COLT ) { + if ( COM_BitCheck( cg.snap->ps.weapons, weapon ) ) { + weapon = WP_AKIMBO; // you have colt, now get akimbo (second) + } + } + + if ( cg_autoswitch.integer && cg.predictedPlayerState.weaponstate != WEAPON_RELOADING ) { + + // 0 - "Off" + // 1 - "Always Switch" + // 2 - "If New" + // 3 - "If Better" + // 4 - "New or Better" + // 5 - "New and Better" + + // don't ever autoswitch to secondary fire weapons + if ( weapon != WP_SNIPERRIFLE && weapon != WP_SNOOPERSCOPE && weapon != WP_FG42SCOPE ) { //----(SA) modified + + // no weap currently selected, always just select the new one + if ( !cg.weaponSelect ) { + selectIt = qtrue; + } + // 1 - "Always Switch" - always switch to new weap (Q3A default) + else if ( cg_autoswitch.integer == 1 ) { + selectIt = qtrue; + } else { + + // 2 - "If New" - switch to weap if it's not already in the player's inventory (Wolf default) + // 4 - either 2 or 3 + // 5 - both 2 and 3 + + // FIXME: this works fine for predicted pickups (when you walk over the weapon), but not for + // manual pickups (activate item) + if ( cg_autoswitch.integer == 2 || cg_autoswitch.integer == 4 || cg_autoswitch.integer == 5 ) { + if ( !COM_BitCheck( cg.snap->ps.weapons, weapon ) ) { + selectIt = qtrue; + } + } // end 2/4/5 + + // 3 - "If Better" - switch to weap if it's in a bank greater than the current weap + // 4 - either 2 or 3 + // 5 - both 2 and 3 + if ( cg_autoswitch.integer == 3 || cg_autoswitch.integer == 4 || cg_autoswitch.integer == 5 ) { + // switch away only if a primary weapon is selected (read: don't switch away if current weap is a secondary mode) + if ( CG_WeaponIndex( cg.weaponSelect, &wpbank_cur, NULL ) ) { + if ( CG_WeaponIndex( weapon, &wpbank_pickup, NULL ) ) { + if ( wpbank_pickup > wpbank_cur ) { + if ( cg_autoswitch.integer == 5 ) { // 'new /and/ better' + if ( !selectIt ) { + selectIt = qfalse; // if it isn't selected because it's new, then this isn't "both" new /and/ better + } + } else { + selectIt = qtrue; + } + } else { // not better + if ( cg_autoswitch.integer == 5 ) { + selectIt = qfalse; + } + } + } + } + } // end 3/4/5 + + } // end cg_autoswitch.integer != 1 + + } // end weapon != WP_SNIPERRIFLE && ... + + } // end cg_autoswitch.integer + + // only select one-handed weaps if you've got a chair + if ( cg.snap->ps.eFlags & EF_MELEE_ACTIVE ) { + if ( !( ( 1 << weapon ) & WEAPS_ONE_HANDED ) ) { + selectIt = qfalse; + } + } + + if ( selectIt ) { + cg.weaponSelectTime = cg.time; + cg.weaponSelect = weapon; + } + + } // end bg_itemlist[itemNum].giType == IT_WEAPON + + + if ( bg_itemlist[itemNum].giType == IT_HOLDABLE ) { + cg.holdableSelectTime = cg.time; // show holdables when a new one is picked up + cg.holdableSelect = itemid; // and select the new one + } + +} + + +/* +================ +CG_PainEvent + +Also called by playerstate transition +================ +*/ +typedef struct { + char *tag; + int refEntOfs; + int anim; +} painAnimForTag_t; + +#define PEFOFS( x ) ( (int)&( ( (playerEntity_t *)0 )->x ) ) + +void CG_PainEvent( centity_t *cent, int health, qboolean crouching ) { + char *snd; + + #define STUNNED_ANIM BOTH_PAIN8 + painAnimForTag_t tagAnims[] = { + {"tag_head", PEFOFS( torsoRefEnt ), BOTH_PAIN1}, + {"tag_chest", PEFOFS( torsoRefEnt ), BOTH_PAIN2}, + {"tag_groin", PEFOFS( legsRefEnt ), BOTH_PAIN3}, + {"tag_armright",PEFOFS( torsoRefEnt ), BOTH_PAIN4}, + {"tag_armleft", PEFOFS( torsoRefEnt ), BOTH_PAIN5}, + {"tag_legright",PEFOFS( legsRefEnt ), BOTH_PAIN6}, + {"tag_legleft", PEFOFS( legsRefEnt ), BOTH_PAIN7}, + {NULL,0,0}, + }; + vec3_t tagOrg; + int tagIndex, bestTag, oldPainAnim; + float bestDist, dist; + + // Rafael + if ( cent->currentState.aiChar && cgs.gametype == GT_SINGLE_PLAYER ) { + + if ( cent->pe.painTime > cg.time - 1000 ) { + oldPainAnim = cent->pe.painAnimTorso; + } else { + oldPainAnim = -1; + } + + // Ridah, health is actually time to spend playing the animation + cent->pe.painTime = cg.time; + cent->pe.painDuration = health << 4; + cent->pe.painDirection ^= 1; + cent->pe.painAnimLegs = -1; + cent->pe.painAnimTorso = -1; + + if ( VectorLength( cent->currentState.origin2 ) > 1 ) { + // find a correct animation to play, based on the body orientation at previous frame + for ( tagIndex = 0, bestDist = 0, bestTag = -1; tagAnims[tagIndex].tag; tagIndex++ ) { + if ( oldPainAnim >= 0 && tagAnims[tagIndex].anim == oldPainAnim ) { + continue; + } + // grab the tag with this name + if ( CG_GetOriginForTag( cent, ( refEntity_t * )( ( (byte *)¢->pe ) + tagAnims[tagIndex].refEntOfs ), tagAnims[tagIndex].tag, 0, tagOrg, NULL ) >= 0 ) { + dist = VectorDistance( tagOrg, cent->currentState.origin2 ); + if ( !bestDist || dist < bestDist ) { + bestTag = tagIndex; + bestDist = dist; + } + } + } + + if ( bestTag >= 0 ) { + if ( !crouching ) { + cent->pe.painAnimLegs = tagAnims[bestTag].anim; + } + cent->pe.painAnimTorso = tagAnims[bestTag].anim; + } + } + + if ( cent->pe.painAnimTorso < 0 && cent->pe.painDuration > 1000 ) { // stunned + if ( !crouching ) { + cent->pe.painAnimLegs = STUNNED_ANIM; + } + cent->pe.painAnimTorso = STUNNED_ANIM; + } + + if ( cent->pe.painAnimTorso < 0 ) { + // pick a random anim + for ( tagIndex = 0; tagAnims[tagIndex].tag; tagIndex++ ) {}; + bestTag = rand() % tagIndex; + if ( !crouching ) { + cent->pe.painAnimLegs = tagAnims[bestTag].anim; + } + cent->pe.painAnimTorso = tagAnims[bestTag].anim; + } + + // adjust the animation speed + { + animation_t *anim; + clientInfo_t *ci; + + ci = &cgs.clientinfo[ cent->currentState.number ]; + anim = &ci->modelInfo->animations[ cent->pe.painAnimTorso ]; + + cent->pe.animSpeed = ( anim->frameLerp * anim->numFrames ) / (float)cent->pe.painDuration; + } + + return; + } + + // don't do more than two pain sounds a second + if ( cg.time - cent->pe.painTime < 500 ) { + return; + } + + if ( health < 25 ) { + snd = "*pain25_1.wav"; + } else if ( health < 50 ) { + snd = "*pain50_1.wav"; + } else if ( health < 75 ) { + snd = "*pain75_1.wav"; + } else { + snd = "*pain100_1.wav"; + } + trap_S_StartSound( NULL, cent->currentState.number, CHAN_VOICE, + CG_CustomSound( cent->currentState.number, snd ) ); + + // save pain time for programitic twitch animation + cent->pe.painTime = cg.time; + cent->pe.painDirection ^= 1; +} + + + + + +/* +============== +CG_Explode + + + if (cent->currentState.angles2[0] || cent->currentState.angles2[1] || cent->currentState.angles2[2]) + +============== +*/ + +#define POSSIBLE_PIECES 6 + + + + +void CG_Explodef( vec3_t origin, vec3_t dir, int mass, int type, qhandle_t sound, int forceLowGrav, qhandle_t shader, int parent, qboolean damage ); + +/* +============== +CG_Explode + the old cent-based explode calls will still work with this pass-through +============== +*/ +void CG_Explode( centity_t *cent, vec3_t origin, vec3_t dir, qhandle_t shader ) { + vec3_t pos; + qhandle_t inheritmodel = 0; + + +// VectorCopy(origin, pos); + VectorCopy( cent->currentState.origin2, pos ); + + // inherit shader + // (SA) FIXME: do this at spawn time rather than explode time so any new necessary shaders are created earlier + if ( cent->currentState.eFlags & EF_INHERITSHADER ) { + if ( !shader ) { +// inheritmodel = cent->currentState.modelindex; + inheritmodel = cgs.inlineDrawModel[cent->currentState.modelindex]; // okay, this should be better. + if ( inheritmodel ) { + shader = trap_R_GetShaderFromModel( inheritmodel, 0, 0 ); + } + } + } + + CG_Explodef( pos, + dir, + cent->currentState.density, // mass +// cent->currentState.time2, // type + cent->currentState.effect3Time, //----(SA) needed .time + cent->currentState.dl_intensity, // sound + cent->currentState.weapon, // forceLowGrav + shader, + cent->currentState.number, + cent->currentState.teamNum + ); + +} + + +/* +============== +CG_Explodef + made this more generic for spawning hits and breaks without needing a *cent +============== +*/ +void CG_Explodef( vec3_t origin, vec3_t dir, int mass, int type, qhandle_t sound, int forceLowGrav, qhandle_t shader, int parent, qboolean damage ) { + int i; + localEntity_t *le; + refEntity_t *re; + int howmany, total; + int pieces[6]; // how many of each piece + qhandle_t modelshader = 0; + float materialmul = 1; // multiplier for different types + + memset( &pieces, 0, sizeof( pieces ) ); + + if ( type == 5 && damage ) { + vec3_t vec, org; + centity_t *boss; + // set the default pos to the viewpos + VectorCopy( cg.refdef.vieworg, org ); + // find the boss + for ( boss = cg_entities; boss < &cg_entities[MAX_CLIENTS]; boss++ ) { + if ( !boss->currentValid ) { + continue; + } + if ( boss->currentState.aiChar == AICHAR_HEINRICH ) { + VectorCopy( boss->lerpOrigin, org ); + break; + } + } + // optimization, ignore if too far away + VectorSubtract( origin, org, vec ); // whoops, that way wouldn't work for cameras... + vec[2] = 0; + if ( VectorLength( vec ) > 800 ) { + return; + } + mass = (int)( 2.0 * mass * ( 1.0 - ( 0.6 + 0.4 * ( VectorLength( vec ) / 800.0 ) ) ) ); + } + + pieces[5] = (int)( mass / 250.0f ); + pieces[4] = (int)( mass / 76.0f ); + pieces[3] = (int)( mass / 37.0f ); // so 2 per 75 + pieces[2] = (int)( mass / 15.0f ); + pieces[1] = (int)( mass / 10.0f ); + pieces[0] = (int)( mass / 5.0f ); + + if ( pieces[0] > 20 ) { + pieces[0] = 20; // cap some of the smaller bits so they don't get out of control + } + if ( pieces[1] > 15 ) { + pieces[1] = 15; + } + if ( pieces[2] > 10 ) { + pieces[2] = 10; + } + + if ( type == 0 ) { // cap wood even more since it's often grouped, and the small splinters can add up + if ( pieces[0] > 10 ) { + pieces[0] = 10; + } + if ( pieces[1] > 10 ) { + pieces[1] = 10; + } + if ( pieces[2] > 10 ) { + pieces[2] = 10; + } + } + + // cap end map debris (optimization) + if ( type == 5 && damage ) { + pieces[0] = 5; + pieces[1] = 5; + pieces[2] = 4; + pieces[3] = 4; + pieces[4] = 4; + } + + total = pieces[5] + pieces[4] + pieces[3] + pieces[2] + pieces[1] + pieces[0]; + + if ( sound ) { + trap_S_StartSound( origin, ENTITYNUM_WORLD, CHAN_AUTO, cgs.gameSounds[sound] ); + } + + if ( shader ) { // shader passed in to use + modelshader = shader; + } + + for ( i = 0; i < POSSIBLE_PIECES; i++ ) { + leBounceSoundType_t snd = LEBS_NONE; + int hmodel = 0; + float scale; + int endtime; + for ( howmany = 0; howmany < pieces[i]; howmany++ ) { + + scale = 1.0f; + endtime = 0; // set endtime offset for faster/slower fadeouts + + switch ( type ) { + case 0: // "wood" + snd = LEBS_WOOD; + hmodel = cgs.media.debWood[i]; + + if ( i == 0 ) { + scale = 0.5f; + } else if ( i == 1 ) { + scale = 0.6f; + } else if ( i == 2 ) { + scale = 0.7f; + } else if ( i == 3 ) { + scale = 0.5f; + } + // else + // scale = cg_forceModel.value; + // else goto pass; + + if ( i < 3 ) { + endtime = -3000; // small bits live 3 sec shorter than normal + } + break; + + case 1: // "glass" + snd = LEBS_NONE; + if ( i == 5 ) { + hmodel = cgs.media.shardGlass1; + } else if ( i == 4 ) { + hmodel = cgs.media.shardGlass2; + } else if ( i == 2 ) { + hmodel = cgs.media.shardGlass2; + } else if ( i == 1 ) { + hmodel = cgs.media.shardGlass2; + scale = 0.5f; + } else {goto pass;} + break; + + case 2: // "metal" + snd = LEBS_BRASS; + if ( i == 5 ) { + hmodel = cgs.media.shardMetal1; + } else if ( i == 4 ) { + hmodel = cgs.media.shardMetal2; + } else if ( i == 2 ) { + hmodel = cgs.media.shardMetal2; + } else if ( i == 1 ) { + hmodel = cgs.media.shardMetal2; + scale = 0.5f; + } else {goto pass;} + break; + + case 3: // "gibs" + snd = LEBS_BLOOD; + if ( i == 5 ) { + hmodel = cgs.media.gibIntestine; + } else if ( i == 4 ) { + hmodel = cgs.media.gibLeg; + } else if ( i == 2 ) { + hmodel = cgs.media.gibChest; + } else { goto pass;} + break; + + case 4: // "brick" + snd = LEBS_ROCK; + hmodel = cgs.media.debBlock[i]; + break; + + case 5: // "rock" + snd = LEBS_ROCK; + if ( i == 5 ) { + hmodel = cgs.media.debRock[2]; // temporarily use the next smallest rock piece + } else if ( i == 4 ) { + hmodel = cgs.media.debRock[2]; + } else if ( i == 3 ) { + hmodel = cgs.media.debRock[1]; + } else if ( i == 2 ) { + hmodel = cgs.media.debRock[0]; + } else if ( i == 1 ) { + hmodel = cgs.media.debBlock[1]; // temporarily use the small block pieces + } else { hmodel = cgs.media.debBlock[0]; // temporarily use the small block pieces + } + if ( i <= 2 ) { + endtime = -2000; // small bits live 2 sec shorter than normal + } + break; + + case 6: // "fabric" + if ( i == 5 ) { + hmodel = cgs.media.debFabric[0]; + } else if ( i == 4 ) { + hmodel = cgs.media.debFabric[1]; + } else if ( i == 2 ) { + hmodel = cgs.media.debFabric[2]; + } else if ( i == 1 ) { + hmodel = cgs.media.debFabric[2]; + scale = 0.5; + } else {goto pass; // (only do 5, 4, 2 and 1) + } + break; + } + + le = CG_AllocLocalEntity(); + re = &le->refEntity; + + le->leType = LE_FRAGMENT; + le->startTime = cg.time; + + le->endTime = ( le->startTime + 5000 + random() * 5000 ) + endtime; + + // RF, debris rocks last longer in the boss map (is thee a better way of doing this at this late stage?) + if ( snd == LEBS_ROCK && damage ) { + snd = 0; + if ( damage ) { + le->leFlags |= LEF_PLAYER_DAMAGE; + } + le->endTime = le->startTime + 7000 + random() * 5000; + } + + if ( parent ) { + le->leFlags |= LEF_NOTOUCHPARENT; //----(SA) added + le->ownerNum = parent; + } + + // as it turns out, i'm not sure if setting the re->axis here will actually do anything + // AxisClear(re->axis); + // re->axis[0][0] = + // re->axis[1][1] = + // re->axis[2][2] = scale; + // + // if(scale != 1.0) + // re->nonNormalizedAxes = qtrue; + + le->sizeScale = scale; + + if ( type == 1 ) { // glass + // Rafael added this because glass looks funky when it fades out + // TBD: need to look into this so that they fade out correctly + re->fadeStartTime = le->endTime; + re->fadeEndTime = le->endTime; + } else { + re->fadeStartTime = le->endTime - 4000; + re->fadeEndTime = le->endTime; + } + + + le->lifeRate = 1.0 / ( le->endTime - le->startTime ); + le->leFlags |= LEF_TUMBLE; + le->leMarkType = 0; + + VectorCopy( origin, re->origin ); + AxisCopy( axisDefault, re->axis ); + + le->leBounceSoundType = snd; + re->hModel = hmodel; + + // inherit shader + if ( modelshader ) { + re->customShader = modelshader; + } + + re->radius = 1000; + + switch ( type ) { + case 6: // fabric + le->pos.trType = TR_GRAVITY_FLOAT; // the fabric stuff will change to use something that looks better + le->bounceFactor = 0.0f; + materialmul = 0.3f; // rotation speed + break; + default: + if ( !forceLowGrav && rand() & 1 ) { // if low gravity is not forced and die roll goes our way use regular grav + le->pos.trType = TR_GRAVITY; + } else { + le->pos.trType = TR_GRAVITY_LOW; + } + le->bounceFactor = 0.2f; // RF, rubble in end is like rubber, also ID requestsed this + break; + } + + + // rotation + le->angles.trType = TR_LINEAR; + le->angles.trTime = cg.time; + le->angles.trBase[0] = rand() & 31; + le->angles.trBase[1] = rand() & 31; + le->angles.trBase[2] = rand() & 31; + le->angles.trDelta[0] = ( ( 100 + ( rand() & 500 ) ) - 300 ) * materialmul; + le->angles.trDelta[1] = ( ( 100 + ( rand() & 500 ) ) - 300 ) * materialmul; + le->angles.trDelta[2] = ( ( 100 + ( rand() & 500 ) ) - 300 ) * materialmul; + + + // if(type == 6) // fabric + // materialmul = 1; // translation speed + + + VectorCopy( origin, le->pos.trBase ); + VectorNormalize( dir ); + le->pos.trTime = cg.time; + + // (SA) hoping that was just intended to represent randomness + // if (cent->currentState.angles2[0] || cent->currentState.angles2[1] || cent->currentState.angles2[2]) + if ( ( le->angles.trBase[0] == 1 || le->angles.trBase[1] == 1 || le->angles.trBase[2] == 1 ) && type != 6 ) { // not for fabric + le->pos.trType = TR_GRAVITY; + VectorScale( dir, 10 * 8, le->pos.trDelta ); + le->pos.trDelta[0] += ( ( random() * 100 ) - 50 ); + le->pos.trDelta[1] += ( ( random() * 100 ) - 50 ); + le->pos.trDelta[2] = ( random() * 200 ) + 200; + + } else { + // location + VectorScale( dir, 200 + mass, le->pos.trDelta ); + le->pos.trDelta[0] += ( ( random() * 100 ) - 50 ); + le->pos.trDelta[1] += ( ( random() * 100 ) - 50 ); + + if ( dir[2] ) { + le->pos.trDelta[2] = random() * 200 * materialmul; // randomize sort of a lot so they don't all land together + } else { + le->pos.trDelta[2] = random() * 20; + } + + if ( type == 5 && damage ) { + VectorScale( le->pos.trDelta, 4.0, le->pos.trDelta ); + // dont let them fly out too fast + while ( VectorLength( le->pos.trDelta ) > 800 ) { + VectorScale( le->pos.trDelta, 0.5, le->pos.trDelta ); + } + } + } + } +pass: + continue; + } + +} + + +/* +============== +CG_Effect + Quake ed -> target_effect (0 .5 .8) (-6 -6 -6) (6 6 6) TNT explode smoke debris gore lowgrav +============== +*/ +void CG_Effect( centity_t *cent, vec3_t origin, vec3_t dir ) { + localEntity_t *le; + refEntity_t *re; +// int howmany; + int mass; +// int large, small; + + VectorSet( dir, 0, 0, 1 ); // straight up. + + mass = cent->currentState.density; + +// 1 large per 100, 1 small per 24 +// large = (int)(mass / 100); +// small = (int)(mass / 24) + 1; + + if ( cent->currentState.eventParm & 1 ) { // fire + vec3_t sprVel, sprOrg; + int i,j; + + // RF, sprVel is used without being set, so to be sure, I'm going to clear out the vector + VectorClear( sprVel ); + + for ( i = 0; i < 5; i++ ) { + for ( j = 0; j < 3; j++ ) + sprOrg[j] = origin[j] + 64 * dir[j] + 24 * crandom(); + sprVel[2] += rand() % 50; + CG_ParticleExplosion( "blacksmokeanimb", sprOrg, sprVel, + 3500 + rand() % 250, // duration + 10, // startsize + 250 + rand() % 60 ); // endsize + } + + VectorMA( origin, 16, dir, sprOrg ); + VectorScale( dir, 100, sprVel ); + + // trying this one just for now just for variety + CG_ParticleExplosion( "explode1", sprOrg, sprVel, + 1200, // duration + 9, // startsize + 300 ); // endsize + + CG_AddDebris( origin, dir, + 280, // speed + 1400, // duration + 7 + rand() % 2 ); // count + + trap_S_StartSound( origin, ENTITYNUM_WORLD, CHAN_AUTO, cgs.media.sfx_dynamiteexp ); + trap_S_StartLocalSound( cgs.media.sfx_dynamiteexpDist, CHAN_AUTO ); + CG_ImpactMark( cgs.media.burnMarkShader, origin, dir, random() * 360, 1,1,1,1, qfalse, 64, qfalse, -1 ); + } + + // (SA) right now force smoke on any explosions +// if(cent->currentState.eventParm & 4) // smoke + if ( cent->currentState.eventParm & 6 ) { + int i, j; + vec3_t sprVel, sprOrg; + // explosion sprite animation + VectorScale( dir, 16, sprVel ); + for ( i = 0; i < 5; i++ ) { + for ( j = 0; j < 3; j++ ) + sprOrg[j] = origin[j] + 64 * dir[j] + 24 * crandom(); + sprVel[2] += rand() % 50; +// CG_ParticleExplosion( 2, sprOrg, sprVel, 1000+rand()%250, 20, 40+rand()%60 ); + CG_ParticleExplosion( "blacksmokeanimb", sprOrg, sprVel, 3500 + rand() % 250, 10, 250 + rand() % 60 ); + } + } + + + if ( cent->currentState.eventParm & 2 ) { // explode + vec3_t sprVel, sprOrg; + trap_S_StartSound( origin, ENTITYNUM_WORLD, CHAN_AUTO, cgs.media.sfx_rockexp ); + + // new explode (from rl) + VectorMA( origin, 16, dir, sprOrg ); + VectorScale( dir, 100, sprVel ); + CG_ParticleExplosion( "expblue", sprOrg, sprVel, 500, 20, 160 ); + //CG_ParticleExplosion( "blueexp", sprOrg, sprVel, 1200, 9, 300 ); + + // (SA) this is done only if the level designer has it marked in the entity. + // (see "cent->currentState.eventParm & 64" below) + + // RF, throw some debris +// CG_AddDebris( origin, dir, +// 280, // speed +// 1400, // duration +// // 15 + rand()%5 ); // count +// 7 + rand()%2 ); // count + + CG_ImpactMark( cgs.media.burnMarkShader, origin, dir, random() * 360, 1,1,1,1, qfalse, 64, qfalse, 0xffffffff ); + } + + + if ( cent->currentState.eventParm & 8 ) { // rubble + // share the cg_explode code with func_explosives + const char *s; + qhandle_t sh = 0; // shader handle + + vec3_t newdir = {0, 0, 0}; + + if ( cent->currentState.angles2[0] || cent->currentState.angles2[1] || cent->currentState.angles2[2] ) { + VectorCopy( cent->currentState.angles2, newdir ); + } + + s = CG_ConfigString( CS_TARGETEFFECT ); // see if ent has a shader specified + if ( s && strlen( s ) > 0 ) { + sh = trap_R_RegisterShader( va( "textures/%s", s ) ); // FIXME: don't do this here. only for testing + + } + cent->currentState.eFlags &= ~EF_INHERITSHADER; // don't try to inherit shader + cent->currentState.dl_intensity = 0; // no sound + CG_Explode( cent, origin, newdir, sh ); + } + + + if ( cent->currentState.eventParm & 16 ) { // gore + le = CG_AllocLocalEntity(); + re = &le->refEntity; + + le->leType = LE_FRAGMENT; + le->startTime = cg.time; + le->endTime = le->startTime + 5000 + random() * 3000; +//----(SA) fading out + re->fadeStartTime = le->endTime - 4000; + re->fadeEndTime = le->endTime; +//----(SA) end + + VectorCopy( origin, re->origin ); + AxisCopy( axisDefault, re->axis ); + // re->hModel = hModel; + re->hModel = cgs.media.gibIntestine; + le->pos.trType = TR_GRAVITY; + VectorCopy( origin, le->pos.trBase ); + + // VectorCopy( velocity, le->pos.trDelta ); + VectorNormalize( dir ); + VectorMA( dir, 200, dir, le->pos.trDelta ); + + le->pos.trTime = cg.time; + + le->bounceFactor = 0.3; + + le->leBounceSoundType = LEBS_BLOOD; + le->leMarkType = LEMT_BLOOD; + } + + + if ( cent->currentState.eventParm & 64 ) { // debris trails (the black strip that Ryan did) + CG_AddDebris( origin, dir, + 280, // speed + 1400, // duration + // 15 + rand()%5 ); // count + 7 + rand() % 2 ); // count + } +} + + + + + + +/* +CG_Shard + + We should keep this separate since there will be considerable differences + in the physical properties of shard vrs debris. not to mention the fact + there is no way we can quantify what type of effects the designers will + potentially desire. If it is still possible to merge the functionality of + cg_shard into cg_explode at a latter time I would have no problem with that + but for now I want to keep it separate +*/ +void CG_Shard( centity_t *cent, vec3_t origin, vec3_t dir ) { + localEntity_t *le; + refEntity_t *re; + int type; + int howmany; + int i; + int rval; + + qboolean isflyingdebris = qfalse; + + type = cent->currentState.density; + howmany = cent->currentState.frame; + + for ( i = 0; i < howmany; i++ ) + { + le = CG_AllocLocalEntity(); + re = &le->refEntity; + + le->leType = LE_FRAGMENT; + le->startTime = cg.time; + le->endTime = le->startTime + 5000 + random() * 5000; + +//----(SA) fading out + re->fadeStartTime = le->endTime - 1000; + re->fadeEndTime = le->endTime; +//----(SA) end + + if ( type == 999 ) { + le->startTime = cg.time; + le->endTime = le->startTime + 100; + re->fadeStartTime = le->endTime - 100; + re->fadeEndTime = le->endTime; + type = 1; + + isflyingdebris = qtrue; + } + + + le->lifeRate = 1.0 / ( le->endTime - le->startTime ); + le->leFlags |= LEF_TUMBLE; + le->bounceFactor = 0.4; + // le->leBounceSoundType = LEBS_WOOD; + le->leMarkType = 0; + + VectorCopy( origin, re->origin ); + AxisCopy( axisDefault, re->axis ); + + rval = rand() % 2; + + if ( type == 0 ) { // glass + if ( rval ) { + re->hModel = cgs.media.shardGlass1; + } else { + re->hModel = cgs.media.shardGlass2; + } + } else if ( type == 1 ) { // wood + if ( rval ) { + re->hModel = cgs.media.shardWood1; + } else { + re->hModel = cgs.media.shardWood2; + } + } else if ( type == 2 ) { // metal + if ( rval ) { + re->hModel = cgs.media.shardMetal1; + } else { + re->hModel = cgs.media.shardMetal2; + } + } else if ( type == 3 ) { // ceramic + if ( rval ) { + re->hModel = cgs.media.shardCeramic1; + } else { + re->hModel = cgs.media.shardCeramic2; + } + } else if ( type == 4 ) { // rubble + rval = rand() % 3; + + if ( rval == 1 ) { + re->hModel = cgs.media.shardRubble1; + } else if ( rval == 2 ) { + re->hModel = cgs.media.shardRubble2; + } else { + re->hModel = cgs.media.shardRubble3; + } + + } else { + CG_Printf( "CG_Debris has an unknown type\n" ); + } + + // location + if ( isflyingdebris ) { + le->pos.trType = TR_GRAVITY_LOW; + } else { + le->pos.trType = TR_GRAVITY; + } + + VectorCopy( origin, le->pos.trBase ); + VectorNormalize( dir ); + VectorScale( dir, 10 * howmany, le->pos.trDelta ); + le->pos.trTime = cg.time; + le->pos.trDelta[0] += ( ( random() * 100 ) - 50 ); + le->pos.trDelta[1] += ( ( random() * 100 ) - 50 ); + if ( type ) { + le->pos.trDelta[2] = ( random() * 200 ) + 100; // randomize sort of a lot so they don't all land together + } else { // glass + le->pos.trDelta[2] = ( random() * 100 ) + 50; // randomize sort of a lot so they don't all land together + + } + // rotation + le->angles.trType = TR_LINEAR; + le->angles.trTime = cg.time; + le->angles.trBase[0] = rand() & 31; + le->angles.trBase[1] = rand() & 31; + le->angles.trBase[2] = rand() & 31; + le->angles.trDelta[0] = ( 100 + ( rand() & 500 ) ) - 300; + le->angles.trDelta[1] = ( 100 + ( rand() & 500 ) ) - 300; + le->angles.trDelta[2] = ( 100 + ( rand() & 500 ) ) - 300; + + } + +} + + +void CG_ShardJunk( centity_t *cent, vec3_t origin, vec3_t dir ) { + localEntity_t *le; + refEntity_t *re; + int type; + + type = cent->currentState.density; + + le = CG_AllocLocalEntity(); + re = &le->refEntity; + + le->leType = LE_FRAGMENT; + le->startTime = cg.time; + le->endTime = le->startTime + 5000 + random() * 5000; + + re->fadeStartTime = le->endTime - 1000; + re->fadeEndTime = le->endTime; + + le->lifeRate = 1.0 / ( le->endTime - le->startTime ); + le->leFlags |= LEF_TUMBLE; + le->bounceFactor = 0.4; + le->leMarkType = 0; + + VectorCopy( origin, re->origin ); + AxisCopy( axisDefault, re->axis ); + + re->hModel = cgs.media.shardJunk[rand() % MAX_LOCKER_DEBRIS]; + + le->pos.trType = TR_GRAVITY; + + VectorCopy( origin, le->pos.trBase ); + VectorNormalize( dir ); + VectorScale( dir, 10 * 8, le->pos.trDelta ); + le->pos.trTime = cg.time; + le->pos.trDelta[0] += ( ( random() * 100 ) - 50 ); + le->pos.trDelta[1] += ( ( random() * 100 ) - 50 ); + + le->pos.trDelta[2] = ( random() * 100 ) + 50; // randomize sort of a lot so they don't all land together + + // rotation + le->angles.trType = TR_LINEAR; + le->angles.trTime = cg.time; + //le->angles.trBase[0] = rand()&31; + //le->angles.trBase[1] = rand()&31; + le->angles.trBase[2] = rand() & 31; + + //le->angles.trDelta[0] = (100 + (rand()&500)) - 300; + //le->angles.trDelta[1] = (100 + (rand()&500)) - 300; + le->angles.trDelta[2] = ( 100 + ( rand() & 500 ) ) - 300; + +} + +void CG_BatDeath( centity_t *cent ) { + CG_ParticleExplosion( "blood", cent->lerpOrigin, vec3_origin, 400, 20, 30 ); +} + +void CG_SpawnSpirit( centity_t *cent ) { + localEntity_t *le; + refEntity_t *re; + vec3_t enemyPos, v, ang; + + le = CG_AllocLocalEntity(); + re = &le->refEntity; + + re->hModel = cgs.media.ssSpiritSkullModel; + re->backlerp = 0; + re->renderfx = RF_NOSHADOW | RF_MINLIGHT; //----(SA) + //re->customShader = cgs.media.ssSpiritSkullModel; + re->reType = RT_MODEL; + + le->leType = LE_HELGA_SPIRIT; + le->startTime = cg.time; + le->endTime = cg.time + 6000; //cent->currentState.time; + + le->pos.trType = TR_LINEAR; + le->pos.trTime = cg.time; + VectorCopy( cent->currentState.origin, le->pos.trBase ); + VectorClear( le->pos.trDelta ); + + le->effectWidth = 600; + le->radius = 30.0; + le->lastTrailTime = cg.time; + le->headJuncIndex = -1; + le->loopingSound = cgs.media.zombieSpiritLoopSound; + + le->ownerNum = cent->currentState.number; + + re->fadeStartTime = le->endTime - 2000; + re->fadeEndTime = le->endTime; + re->shaderTime = cg.time; + + // get direction to enemy + if ( cg_entities[le->ownerNum].currentState.otherEntityNum2 == cg.snap->ps.clientNum ) { + VectorCopy( cg.snap->ps.origin, enemyPos ); + enemyPos[2] += cg.snap->ps.viewheight; + } else { + VectorCopy( cg_entities[le->ownerNum].currentState.origin2, enemyPos ); + } + + // set angles + VectorSubtract( enemyPos, le->pos.trBase, v ); + VectorNormalize( v ); + vectoangles( v, ang ); + AnglesToAxis( ang, re->axis ); + VectorScale( v, 350, le->pos.trDelta ); + +} + +/* +============== +CG_EntityEvent + +An entity has an event value +also called by CG_CheckPlayerstateEvents +============== +*/ +#define DEBUGNAME( x ) if ( cg_debugEvents.integer ) {CG_Printf( x "\n" );} +void CG_EntityEvent( centity_t *cent, vec3_t position ) { + entityState_t *es; + int event; + vec3_t dir; + const char *s; + int clientNum; + clientInfo_t *ci; + //char tempStr[MAX_QPATH]; + + static int footstepcnt = 0; + static int splashfootstepcnt = 0; + + es = ¢->currentState; + event = es->event & ~EV_EVENT_BITS; + + if ( cg_debugEvents.integer ) { + CG_Printf( "ent:%3i event:%3i ", es->number, event ); + } + + if ( !event ) { + DEBUGNAME( "ZEROEVENT" ); + return; + } + + clientNum = es->clientNum; + if ( clientNum < 0 || clientNum >= MAX_CLIENTS ) { + clientNum = 0; + } + ci = &cgs.clientinfo[ clientNum ]; + + if ( !ci->modelInfo ) { // not ready yet? + return; + } + + switch ( event ) { + // + // movement generated events + // + + // TODO: change all this to sound scripts + + case EV_FOOTSTEP: + DEBUGNAME( "EV_FOOTSTEP" ); + if ( cg_footsteps.integer ) { + if ( cent->currentState.aiChar == AICHAR_ELITEGUARD ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_ELITE_STEP ][footstepcnt] ); + } else if ( cent->currentState.aiChar == AICHAR_ZOMBIE ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_ZOMBIE_STEP ][footstepcnt] ); + } else if ( cent->currentState.aiChar == AICHAR_LOPER ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_LOPER_STEP ][footstepcnt] ); + } else if ( cent->currentState.aiChar == AICHAR_PROTOSOLDIER ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_PROTOSOLDIER_STEP ][footstepcnt] ); + CG_StartShakeCamera( 0.05, 400, es->pos.trBase, 512 ); + } else if ( cent->currentState.aiChar == AICHAR_SUPERSOLDIER ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_SUPERSOLDIER_STEP ][footstepcnt] ); + CG_StartShakeCamera( 0.08, 500, es->pos.trBase, 800 ); + } else if ( cent->currentState.aiChar == AICHAR_HEINRICH ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_HEINRICH ][footstepcnt] ); + CG_StartShakeCamera( 0.08, 500, es->pos.trBase, 800 ); + } else if ( cent->currentState.aiChar == AICHAR_HELGA ) { + CG_SoundPlayIndexedScript( cgs.media.footsteps[FOOTSTEP_BEAST][0], NULL, es->number ); + } else { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ ci->modelInfo->footsteps ][footstepcnt] ); + } + } + break; + case EV_FOOTSTEP_METAL: + DEBUGNAME( "EV_FOOTSTEP_METAL" ); + if ( cg_footsteps.integer ) { + if ( cent->currentState.aiChar == AICHAR_ELITEGUARD ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_ELITE_METAL ][footstepcnt] ); + } else if ( cent->currentState.aiChar == AICHAR_LOPER ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_LOPER_METAL ][footstepcnt] ); + } else if ( cent->currentState.aiChar == AICHAR_PROTOSOLDIER ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_PROTOSOLDIER_METAL ][footstepcnt] ); + CG_StartShakeCamera( 0.05, 400, es->pos.trBase, 512 ); + } else if ( cent->currentState.aiChar == AICHAR_SUPERSOLDIER || cent->currentState.aiChar == AICHAR_HEINRICH ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_SUPERSOLDIER_METAL ][0] ); + CG_StartShakeCamera( 0.08, 500, es->pos.trBase, 800 ); + } else if ( cent->currentState.aiChar == AICHAR_HELGA ) { + CG_SoundPlayIndexedScript( cgs.media.footsteps[FOOTSTEP_BEAST][0], NULL, es->number ); + } else { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_METAL ][footstepcnt] ); + } + } + break; + case EV_FOOTSTEP_WOOD: + DEBUGNAME( "EV_FOOTSTEP_WOOD" ); + if ( cg_footsteps.integer ) { + if ( cent->currentState.aiChar == AICHAR_ELITEGUARD ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_ELITE_WOOD ][footstepcnt] ); + } else if ( cent->currentState.aiChar == AICHAR_ZOMBIE ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_ZOMBIE_WOOD ][footstepcnt] ); + } else if ( cent->currentState.aiChar == AICHAR_LOPER ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_LOPER_WOOD ][footstepcnt] ); + } else if ( cent->currentState.aiChar == AICHAR_PROTOSOLDIER ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_PROTOSOLDIER_WOOD ][footstepcnt] ); + CG_StartShakeCamera( 0.05, 400, es->pos.trBase, 512 ); + } else if ( cent->currentState.aiChar == AICHAR_SUPERSOLDIER || cent->currentState.aiChar == AICHAR_HEINRICH ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_SUPERSOLDIER_WOOD ][footstepcnt] ); + CG_StartShakeCamera( 0.08, 500, es->pos.trBase, 800 ); + } else if ( cent->currentState.aiChar == AICHAR_HELGA ) { + CG_SoundPlayIndexedScript( cgs.media.footsteps[FOOTSTEP_BEAST][0], NULL, es->number ); + } else { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_WOOD ][footstepcnt] ); + } + + } + break; + case EV_FOOTSTEP_GRASS: + DEBUGNAME( "EV_FOOTSTEP_GRASS" ); + if ( cg_footsteps.integer ) { + //if (cent->currentState.aiChar == AICHAR_ELITEGUARD) + // trap_S_StartSound (NULL, es->number, CHAN_BODY, + // cgs.media.footsteps[ FOOTSTEP_ELITE_STEP ][footstepcnt] ); + //else + if ( cent->currentState.aiChar == AICHAR_PROTOSOLDIER ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_PROTOSOLDIER_GRASS ][footstepcnt] ); + } else if ( cent->currentState.aiChar == AICHAR_SUPERSOLDIER || cent->currentState.aiChar == AICHAR_HEINRICH ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_SUPERSOLDIER_GRASS ][footstepcnt] ); + } else if ( cent->currentState.aiChar == AICHAR_HELGA ) { + CG_SoundPlayIndexedScript( cgs.media.footsteps[FOOTSTEP_BEAST][0], NULL, es->number ); + } else { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_GRASS ][footstepcnt] ); + } + } + break; + case EV_FOOTSTEP_GRAVEL: + DEBUGNAME( "EV_FOOTSTEP_GRAVEL" ); + if ( cg_footsteps.integer ) { + if ( cent->currentState.aiChar == AICHAR_ELITEGUARD ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_ELITE_GRAVEL ][footstepcnt] ); + } else if ( cent->currentState.aiChar == AICHAR_ZOMBIE ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_ZOMBIE_GRAVEL ][footstepcnt] ); + } else if ( cent->currentState.aiChar == AICHAR_PROTOSOLDIER ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_PROTOSOLDIER_GRAVEL][footstepcnt] ); + } else if ( cent->currentState.aiChar == AICHAR_SUPERSOLDIER || cent->currentState.aiChar == AICHAR_HEINRICH ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_SUPERSOLDIER_GRAVEL][footstepcnt] ); + } else if ( cent->currentState.aiChar == AICHAR_HELGA ) { + CG_SoundPlayIndexedScript( cgs.media.footsteps[FOOTSTEP_BEAST][0], NULL, es->number ); + } else { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_GRAVEL ][footstepcnt] ); + } + } + break; + + case EV_FOOTSTEP_ROOF: // tile sound + DEBUGNAME( "EV_FOOTSTEP_ROOF" ); + if ( cg_footsteps.integer ) { + //if (cent->currentState.aiChar == AICHAR_ELITEGUARD) + // trap_S_StartSound (NULL, es->number, CHAN_BODY, + // cgs.media.footsteps[ FOOTSTEP_ELITE_ROOF ][footstepcnt] ); + //else + trap_S_StartSound( NULL, es->number, CHAN_BODY, + cgs.media.footsteps[ FOOTSTEP_ROOF ][footstepcnt] ); + } + break; + case EV_FOOTSTEP_SNOW: + DEBUGNAME( "EV_FOOTSTEP_SNOW" ); + if ( cg_footsteps.integer ) { + //if (cent->currentState.aiChar == AICHAR_ELITEGUARD) + // trap_S_StartSound (NULL, es->number, CHAN_BODY, + // cgs.media.footsteps[ FOOTSTEP_ELITE_STEP ][footstepcnt] ); + //else + trap_S_StartSound( NULL, es->number, CHAN_BODY, + cgs.media.footsteps[ FOOTSTEP_SNOW ][footstepcnt] ); + } + break; + +//----(SA) added + case EV_FOOTSTEP_CARPET: + DEBUGNAME( "EV_FOOTSTEP_CARPET" ); + if ( cg_footsteps.integer ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.footsteps[ FOOTSTEP_CARPET ][footstepcnt] ); + } + break; + + +//----(SA) end + + case EV_FOOTSPLASH: + DEBUGNAME( "EV_FOOTSPLASH" ); + if ( cg_footsteps.integer ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, + cgs.media.footsteps[ FOOTSTEP_SPLASH ][splashfootstepcnt] ); + } + break; + case EV_FOOTWADE: + DEBUGNAME( "EV_FOOTWADE" ); + if ( cg_footsteps.integer ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, + cgs.media.footsteps[ FOOTSTEP_SPLASH ][splashfootstepcnt] ); + } + break; + case EV_SWIM: + DEBUGNAME( "EV_SWIM" ); + if ( cg_footsteps.integer ) { + trap_S_StartSound( NULL, es->number, CHAN_BODY, + cgs.media.footsteps[ FOOTSTEP_SPLASH ][footstepcnt] ); + } + break; + + + case EV_FALL_SHORT: + DEBUGNAME( "EV_FALL_SHORT" ); + trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.landSound ); + if ( clientNum == cg.predictedPlayerState.clientNum ) { + // smooth landing z changes + cg.landChange = -8; + cg.landTime = cg.time; + } + break; + + case EV_FALL_DMG_10: + DEBUGNAME( "EV_FALL_DMG_10" ); + trap_S_StartSound( NULL, es->number, CHAN_AUTO, CG_CustomSound( es->number, "*fall1.wav" ) ); + // use normal pain sound trap_S_StartSound( NULL, es->number, CHAN_VOICE, CG_CustomSound( es->number, "*pain100_1.wav" ) ); + if ( clientNum == cg.predictedPlayerState.clientNum ) { + // smooth landing z changes + cg.landChange = -16; + cg.landTime = cg.time; + CG_StartShakeCamera( 0.03, 200, cg.predictedPlayerState.origin, 200 ); + } + break; + + case EV_FALL_DMG_15: + DEBUGNAME( "EV_FALL_DMG_15" ); + trap_S_StartSound( NULL, es->number, CHAN_AUTO, CG_CustomSound( es->number, "*fall1.wav" ) ); + // use normal pain sound trap_S_StartSound( NULL, es->number, CHAN_VOICE, CG_CustomSound( es->number, "*pain100_1.wav" ) ); + if ( clientNum == cg.predictedPlayerState.clientNum ) { + // smooth landing z changes + cg.landChange = -16; + cg.landTime = cg.time; + CG_StartShakeCamera( 0.04, 200, cg.predictedPlayerState.origin, 200 ); + } + break; + + case EV_FALL_DMG_25: + DEBUGNAME( "EV_FALL_DMG_25" ); + trap_S_StartSound( NULL, es->number, CHAN_AUTO, CG_CustomSound( es->number, "*fall2.wav" ) ); + cent->pe.painTime = cg.time; // don't play a pain sound right after this + if ( clientNum == cg.predictedPlayerState.clientNum ) { + // smooth landing z changes + cg.landChange = -24; + cg.landTime = cg.time; + CG_StartShakeCamera( 0.05, 200, cg.predictedPlayerState.origin, 200 ); + } + break; + + case EV_FALL_DMG_50: + DEBUGNAME( "EV_FALL_DMG_50" ); + trap_S_StartSound( NULL, es->number, CHAN_AUTO, CG_CustomSound( es->number, "*fall2.wav" ) ); + cent->pe.painTime = cg.time; // don't play a pain sound right after this + if ( clientNum == cg.predictedPlayerState.clientNum ) { + // smooth landing z changes + cg.landChange = -24; + cg.landTime = cg.time; + CG_StartShakeCamera( 0.08, 200, cg.predictedPlayerState.origin, 200 ); + } + break; + + case EV_FALL_NDIE: + DEBUGNAME( "EV_FALL_NDIE" ); + // splat + break; + + case EV_EXERT1: + DEBUGNAME( "EV_EXERT1" ); + trap_S_StartSound( NULL, es->number, CHAN_VOICE, CG_CustomSound( es->number, "*exert1.wav" ) ); + break; + case EV_EXERT2: + DEBUGNAME( "EV_EXERT2" ); + trap_S_StartSound( NULL, es->number, CHAN_VOICE, CG_CustomSound( es->number, "*exert2.wav" ) ); + break; + case EV_EXERT3: + DEBUGNAME( "EV_EXERT3" ); + trap_S_StartSound( NULL, es->number, CHAN_VOICE, CG_CustomSound( es->number, "*exert3.wav" ) ); + break; + + case EV_STEP_4: + case EV_STEP_8: + case EV_STEP_12: + case EV_STEP_16: // smooth out step up transitions + DEBUGNAME( "EV_STEP" ); + { + float oldStep; + int delta; + int step; + + if ( clientNum != cg.predictedPlayerState.clientNum ) { + break; + } + // if we are interpolating, we don't need to smooth steps + if ( cg.demoPlayback || ( cg.snap->ps.pm_flags & PMF_FOLLOW ) || + cg_nopredict.integer || cg_synchronousClients.integer ) { + break; + } + // check for stepping up before a previous step is completed + delta = cg.time - cg.stepTime; + if ( delta < STEP_TIME ) { + oldStep = cg.stepChange * ( STEP_TIME - delta ) / STEP_TIME; + } else { + oldStep = 0; + } + + // add this amount + step = 4 * ( event - EV_STEP_4 + 1 ); + cg.stepChange = oldStep + step; + if ( cg.stepChange > MAX_STEP_CHANGE ) { + cg.stepChange = MAX_STEP_CHANGE; + } + cg.stepTime = cg.time; + break; + } + + case EV_JUMP_PAD: + DEBUGNAME( "EV_JUMP_PAD" ); + // boing sound at origin, jump sound on player + trap_S_StartSound( cent->lerpOrigin, -1, CHAN_VOICE, cgs.media.jumpPadSound ); + trap_S_StartSound( NULL, es->number, CHAN_VOICE, CG_CustomSound( es->number, "*jump1.wav" ) ); + break; + + case EV_JUMP: + DEBUGNAME( "EV_JUMP" ); + trap_S_StartSound( NULL, es->number, CHAN_VOICE, CG_CustomSound( es->number, "*jump1.wav" ) ); + break; + case EV_TAUNT: + DEBUGNAME( "EV_TAUNT" ); + trap_S_StartSound( NULL, es->number, CHAN_VOICE, CG_CustomSound( es->number, "*taunt.wav" ) ); + break; + case EV_WATER_TOUCH: + DEBUGNAME( "EV_WATER_TOUCH" ); + trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.watrInSound ); + break; + case EV_WATER_LEAVE: + DEBUGNAME( "EV_WATER_LEAVE" ); + trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.watrOutSound ); + break; + case EV_WATER_UNDER: + DEBUGNAME( "EV_WATER_UNDER" ); + trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.watrUnSound ); +//----(SA) this fog stuff for underwater is really just a test for feasibility of creating the under-water effect that way. +//----(SA) the related issues of load/savegames, death underwater, etc. are not handled at all. +//----(SA) the actual problem, of course, is doing underwater stuff when the water is very turbulant and you can't simply +//----(SA) do things based on the players head being above/below the water brushes top surface. (since the waves can potentially be /way/ above/below that) + + // DHM - Nerve :: causes problems in multiplayer... + if ( cgs.gametype == GT_SINGLE_PLAYER && clientNum == cg.predictedPlayerState.clientNum ) { +// trap_R_SetFog(FOG_WATER, 0, 400, .1, .1, .1, 111); +// trap_R_SetFog(FOG_CMD_SWITCHFOG, FOG_WATER, 200, 0, 0, 0, 0); + char buff[64]; + trap_Cvar_VariableStringBuffer( "r_waterFogColor", buff, sizeof( buff ) ); + trap_SendClientCommand( va( "fogswitch %s", buff ) ); + } + break; + case EV_WATER_CLEAR: + DEBUGNAME( "EV_WATER_CLEAR" ); + trap_S_StartSound( NULL, es->number, CHAN_AUTO, CG_CustomSound( es->number, "*gasp.wav" ) ); + + // DHM - Nerve :: causes problems in multiplayer... + if ( cgs.gametype == GT_SINGLE_PLAYER && clientNum == cg.predictedPlayerState.clientNum ) { +// trap_R_SetFog(FOG_CMD_SWITCHFOG, FOG_MAP, 400,0,0,0,0); + char buff[64]; + trap_Cvar_VariableStringBuffer( "r_mapFogColor", buff, sizeof( buff ) ); + trap_SendClientCommand( va( "fogswitch %s", buff ) ); + } + break; + + case EV_ITEM_PICKUP: + case EV_ITEM_PICKUP_QUIET: + DEBUGNAME( "EV_ITEM_PICKUP" ); + { + gitem_t *item; + int index; + + index = es->eventParm; // player predicted + + if ( index < 1 || index >= bg_numItems ) { + break; + } + item = &bg_itemlist[ index ]; + + if ( event == EV_ITEM_PICKUP ) { // not quiet + // powerups and team items will have a separate global sound, this one + // will be played at prediction time + if ( item->giType == IT_POWERUP || item->giType == IT_TEAM ) { + trap_S_StartSound( NULL, es->number, CHAN_AUTO, trap_S_RegisterSound( "sound/items/n_health.wav" ) ); + } else { + trap_S_StartSound( NULL, es->number, CHAN_AUTO, trap_S_RegisterSound( item->pickup_sound ) ); + } + } + + // show icon and name on status bar + if ( es->number == cg.snap->ps.clientNum ) { + CG_ItemPickup( index ); + } + +//----(SA) draw the HUD items for a sec since this is a special item + if ( item->giType == IT_KEY ) { + cg.itemFadeTime = cg.time + 1000; + } + + } + break; + + case EV_GLOBAL_ITEM_PICKUP: + DEBUGNAME( "EV_GLOBAL_ITEM_PICKUP" ); + { + gitem_t *item; + int index; + + index = es->eventParm; // player predicted + + if ( index < 1 || index >= bg_numItems ) { + break; + } + item = &bg_itemlist[ index ]; + // powerup pickups are global + trap_S_StartSound( NULL, cg.snap->ps.clientNum, CHAN_AUTO, trap_S_RegisterSound( item->pickup_sound ) ); + + // show icon and name on status bar + if ( es->number == cg.snap->ps.clientNum ) { + CG_ItemPickup( index ); + } + } + break; + + // + // weapon events + // + case EV_VENOM: + DEBUGNAME( "EV_VENOM" ); + CG_VenomFire( es, qfalse ); + break; + case EV_VENOMFULL: + DEBUGNAME( "EV_VENOMFULL" ); + CG_VenomFire( es, qtrue ); + break; + + case EV_NOITEM: + DEBUGNAME( "EV_NOITEM" ); + if ( es->number == cg.snap->ps.clientNum ) { + CG_HoldableUsedupChange(); + } + break; + + case EV_WEAP_OVERHEAT: + DEBUGNAME( "EV_WEAP_OVERHEAT" ); + + // start weapon idle animation + if ( es->number == cg.snap->ps.clientNum ) { + if ( ( cg.predictedPlayerState.weapAnim & ~ANIM_TOGGLEBIT ) == WEAP_ATTACK1 ) { // if attacking, go idle while overheating + cg.predictedPlayerState.weapAnim = ( ( cg.predictedPlayerState.weapAnim & ANIM_TOGGLEBIT ) ^ ANIM_TOGGLEBIT ) | WEAP_IDLE1; + } + cent->overheatTime = cg.time; // used to make the barrels smoke when overheated + } + + if ( cg_weapons[es->weapon].overheatSound ) { + trap_S_StartSound( NULL, es->number, CHAN_AUTO, cg_weapons[es->weapon].overheatSound ); + } + break; + +// JPW NERVE + case EV_SPINUP: + DEBUGNAME( "EV_SPINUP" ); + if ( cg_gameType.integer != GT_SINGLE_PLAYER ) { + trap_S_StartSound( NULL, es->number, CHAN_AUTO, cg_weapons[es->weapon].spinupSound ); + } + break; +// jpw + + case EV_EMPTYCLIP: + DEBUGNAME( "EV_EMPTYCLIP" ); + break; + + case EV_FILL_CLIP: + DEBUGNAME( "EV_FILL_CLIP" ); + if ( cg_weapons[es->weapon].reloadSound ) { + + // hope this does not cause trouble. changed it to chan_weapon so i could kill the sound if the guy dies while reloading + // can re-work if this causes trouble +// trap_S_StartSound (NULL, es->number, CHAN_AUTO, cg_weapons[es->weapon].reloadSound ); + trap_S_StartSound( NULL, es->number, CHAN_WEAPON, cg_weapons[es->weapon].reloadSound ); + } + break; + + + case EV_NOAMMO: + DEBUGNAME( "EV_NOAMMO" ); + if ( ( es->weapon != WP_GRENADE_LAUNCHER ) && ( es->weapon != WP_GRENADE_PINEAPPLE ) && ( es->weapon != WP_DYNAMITE ) ) { + trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.noAmmoSound ); + } + if ( es->number == cg.snap->ps.clientNum ) { + CG_OutOfAmmoChange(); + } + break; + case EV_CHANGE_WEAPON: + { + + int newweap = 0; + + DEBUGNAME( "EV_CHANGE_WEAPON" ); + + // client will get this message if reloading while using an alternate weapon + // client should voluntarily switch back to primary at that point + switch ( es->weapon ) { + case WP_SNOOPERSCOPE: + newweap = WP_GARAND; + break; + case WP_SNIPERRIFLE: + newweap = WP_MAUSER; + break; + case WP_FG42SCOPE: + newweap = WP_FG42; + break; + default: + break; + } + + // TTimo + // show_bug.cgi?id=417 + if ( ( newweap ) && ( cgs.gametype != GT_WOLF ) ) { + CG_FinishWeaponChange( es->weapon, newweap ); + } + + } + break; + +//----(SA) added + case EV_SUGGESTWEAP: + CG_WeaponSuggest( es->eventParm ); + break; +//----(SA) end + + case EV_FIRE_WEAPON_MG42: + // shake the camera a bit + CG_StartShakeCamera( 0.05, 100, cent->lerpOrigin, 100 ); + trap_S_StartSound( NULL, cent->currentState.number, CHAN_WEAPON, hWeaponSnd ); + DEBUGNAME( "EV_FIRE_WEAPON" ); + CG_FireWeapon( cent ); + break; + case EV_FIRE_WEAPON: + case EV_FIRE_WEAPONB: + DEBUGNAME( "EV_FIRE_WEAPON" ); + CG_FireWeapon( cent ); + if ( event == EV_FIRE_WEAPONB ) { // akimbo firing colt + cent->akimboFire = qtrue; + } else { + cent->akimboFire = qfalse; + } + break; + case EV_FIRE_WEAPON_LASTSHOT: + DEBUGNAME( "EV_FIRE_WEAPON_LASTSHOT" ); + CG_FireWeapon( cent ); + break; + +//----(SA) added + case EV_GRENADE_SUICIDE: + DEBUGNAME( "EV_GRENADE_SUICIDE" ); + CG_MissileHitWall( WP_GRENADE_LAUNCHER, 0, position, dir, 0 ); // (SA) modified to send missilehitwall surface parameters + break; +//----(SA) end + +//----(SA) added + case EV_FIRE_QUICKGREN: + // testing. no client side effect yet + break; +//----(SA) end +//----(SA) added + case EV_NOFIRE_UNDERWATER: + DEBUGNAME( "EV_NOFIRE_UNDERWATER" ); + if ( es->number == cg.snap->ps.clientNum ) { // reset client-side weapon animation + cg.predictedPlayerState.weapAnim = ( ( cg.predictedPlayerState.weapAnim & ANIM_TOGGLEBIT ) ^ ANIM_TOGGLEBIT ) | WEAP_IDLE1; + } + if ( cgs.media.noFireUnderwater ) { + trap_S_StartSound( NULL, es->number, CHAN_WEAPON, cgs.media.noFireUnderwater ); + } + break; +//----(SA) end + case EV_USE_ITEM0: + DEBUGNAME( "EV_USE_ITEM0" ); + CG_UseItem( cent ); + break; + case EV_USE_ITEM1: + DEBUGNAME( "EV_USE_ITEM1" ); + CG_UseItem( cent ); + break; + case EV_USE_ITEM2: + DEBUGNAME( "EV_USE_ITEM2" ); + CG_UseItem( cent ); + break; + case EV_USE_ITEM3: + DEBUGNAME( "EV_USE_ITEM3" ); + CG_UseItem( cent ); + break; + case EV_USE_ITEM4: + DEBUGNAME( "EV_USE_ITEM4" ); + CG_UseItem( cent ); + break; + case EV_USE_ITEM5: + DEBUGNAME( "EV_USE_ITEM5" ); + CG_UseItem( cent ); + break; + case EV_USE_ITEM6: + DEBUGNAME( "EV_USE_ITEM6" ); + CG_UseItem( cent ); + break; + case EV_USE_ITEM7: + DEBUGNAME( "EV_USE_ITEM7" ); + CG_UseItem( cent ); + break; + case EV_USE_ITEM8: + DEBUGNAME( "EV_USE_ITEM8" ); + CG_UseItem( cent ); + break; + case EV_USE_ITEM9: + DEBUGNAME( "EV_USE_ITEM9" ); + CG_UseItem( cent ); + break; + case EV_USE_ITEM10: + DEBUGNAME( "EV_USE_ITEM10" ); + CG_UseItem( cent ); + break; + case EV_USE_ITEM11: + DEBUGNAME( "EV_USE_ITEM11" ); + CG_UseItem( cent ); + break; + case EV_USE_ITEM12: + DEBUGNAME( "EV_USE_ITEM12" ); + CG_UseItem( cent ); + break; + case EV_USE_ITEM13: + DEBUGNAME( "EV_USE_ITEM13" ); + CG_UseItem( cent ); + break; + case EV_USE_ITEM14: + DEBUGNAME( "EV_USE_ITEM14" ); + CG_UseItem( cent ); + break; + + //================================================================= + + // + // other events + // + case EV_PLAYER_TELEPORT_IN: + DEBUGNAME( "EV_PLAYER_TELEPORT_IN" ); + trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.teleInSound ); + CG_SpawnEffect( position ); + break; + + case EV_PLAYER_TELEPORT_OUT: + DEBUGNAME( "EV_PLAYER_TELEPORT_OUT" ); + trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.teleOutSound ); + CG_SpawnEffect( position ); + break; + + case EV_ITEM_POP: + DEBUGNAME( "EV_ITEM_POP" ); + trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.respawnSound ); + break; + case EV_ITEM_RESPAWN: + DEBUGNAME( "EV_ITEM_RESPAWN" ); + cent->miscTime = cg.time; // scale up from this + trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.respawnSound ); + break; + + case EV_GRENADE_BOUNCE: + DEBUGNAME( "EV_GRENADE_BOUNCE" ); + +// CG_Printf("bounce on: %d\n", es->eventParm); + + // DYNAMITE + if ( es->weapon == WP_DYNAMITE ) { + trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.dynamitebounce1 ); + } else { + int flags; + // GRENADES + // es->eventParm - surfaceparms +//#define SURF_METAL 0x1000 // clanking footsteps + + flags = es->eventParm; + flags = ( flags << 12 ); + if ( flags & SURF_WOOD ) { // SURF_WOOD + if ( rand() & 1 ) { + trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.grenadebounce[GRENBOUNCE_WOOD][0] ); + } else { trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.grenadebounce[GRENBOUNCE_WOOD][1] );} + } else if ( flags & ( SURF_METAL | SURF_ROOF | SURF_GLASS ) ) { // SURF_METAL | SURF_ROOF | SURF_GLASS + if ( rand() & 1 ) { + trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.grenadebounce[GRENBOUNCE_METAL][0] ); + } else { trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.grenadebounce[GRENBOUNCE_METAL][1] );} + } else if ( flags & ( SURF_GRASS | SURF_GRAVEL | SURF_SNOW | SURF_CARPET ) ) { //SURF_GRASS | SURF_GRAVEL | SURF_SNOW | SURF_CARPET + if ( rand() & 1 ) { + trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.grenadebounce[GRENBOUNCE_DIRT][0] ); + } else { trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.grenadebounce[GRENBOUNCE_DIRT][1] );} + } else { + if ( rand() & 1 ) { + trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.grenadebounce[GRENBOUNCE_DEFAULT][0] ); + } else { trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.grenadebounce[GRENBOUNCE_DEFAULT][1] );} + } + } + break; + + case EV_FLAMEBARREL_BOUNCE: + DEBUGNAME( "EV_FLAMEBARREL_BOUNCE" ); + if ( rand() & 1 ) { + trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.fbarrelexp1 ); + } else { + trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.fbarrelexp2 ); + } + break; + + case EV_RAILTRAIL: + // ev_railtrail is now sent standalone rather than by a player entity + CG_RailTrail( &cgs.clientinfo[ es->otherEntityNum2 ], es->origin2, es->pos.trBase, es->dmgFlags ); //----(SA) added 'type' field + break; + // + // missile impacts + // + case EV_MISSILE_HIT: + DEBUGNAME( "EV_MISSILE_HIT" ); + ByteToDir( es->eventParm, dir ); + CG_MissileHitPlayer( cent, es->weapon, position, dir, es->otherEntityNum ); + break; + + case EV_MISSILE_MISS_SMALL: + DEBUGNAME( "EV_MISSILE_MISS" ); + ByteToDir( es->eventParm, dir ); + CG_MissileHitWallSmall( es->weapon, 0, position, dir ); + break; + + case EV_MISSILE_MISS: + DEBUGNAME( "EV_MISSILE_MISS" ); + ByteToDir( es->eventParm, dir ); + CG_MissileHitWall( es->weapon, 0, position, dir, 0 ); // (SA) modified to send missilehitwall surface parameters + break; + + case EV_MISSILE_MISS_LARGE: + DEBUGNAME( "EV_MISSILE_MISS_LARGE" ); + ByteToDir( es->eventParm, dir ); + CG_MissileHitWall( VERYBIGEXPLOSION, 0, position, dir, 0 ); // (SA) modified to send missilehitwall surface parameters + break; + + case EV_SPIT_MISS: + case EV_SPIT_HIT: + DEBUGNAME( "EV_SPIT_MISS" ); + ByteToDir( es->eventParm, dir ); + CG_MissileHitWall( es->weapon, 0, position, dir, 0 ); // (SA) modified to send missilehitwall surface parameters + break; + + case EV_BULLET_HIT_WALL: + DEBUGNAME( "EV_BULLET_HIT_WALL" ); + ByteToDir( es->eventParm, dir ); + CG_Bullet( es->pos.trBase, es->otherEntityNum, dir, qfalse, ENTITYNUM_WORLD, qfalse, es->otherEntityNum2 ); + break; + + case EV_BULLET_HIT_FLESH: + DEBUGNAME( "EV_BULLET_HIT_FLESH" ); + CG_Bullet( es->pos.trBase, es->otherEntityNum, dir, qtrue, es->eventParm, qfalse, es->otherEntityNum2 ); + break; + + case EV_WOLFKICK_HIT_WALL: + DEBUGNAME( "EV_WOLFKICK_HIT_WALL" ); + ByteToDir( es->eventParm, dir ); + CG_Bullet( es->pos.trBase, es->otherEntityNum, dir, qfalse, ENTITYNUM_WORLD, qtrue, es->otherEntityNum2 ); + trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.fkickwall ); + break; + + case EV_WOLFKICK_HIT_FLESH: + DEBUGNAME( "EV_WOLFKICK_HIT_FLESH" ); + CG_Bullet( es->pos.trBase, es->otherEntityNum, dir, qtrue, es->eventParm, qtrue, es->otherEntityNum2 ); + trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.fkickflesh ); + break; + + case EV_WOLFKICK_MISS: + DEBUGNAME( "EV_WOLFKICK_MISS" ); + trap_S_StartSound( NULL, es->number, CHAN_AUTO, cgs.media.fkickmiss ); + break; + + case EV_POPUPBOOK: + trap_UI_Popup( va( "hbook%d", es->eventParm ) ); + break; + + case EV_POPUP: + s = CG_ConfigString( CS_CLIPBOARDS + es->eventParm ); + // 's' is now the name of the menu script to run + trap_Cvar_Set( "cg_clipboardName", s ); // store new current page name for the ui to pick up + trap_UI_Popup( s ); + break; + + case EV_CLOSEMENU: + Menus_CloseAll(); + break; + + case EV_GIVEPAGE: + { + int havepages = cg_notebookpages.integer; + havepages |= es->eventParm; + trap_Cvar_Set( "cg_notebookpages", va( "%d", havepages ) ); // store new current page name for the ui to pick up + trap_Cvar_Set( "cg_youGotMail", "1" ); //----(SA) added + } + break; + + case EV_GENERAL_SOUND: + DEBUGNAME( "EV_GENERAL_SOUND" ); + // Ridah, check for a sound script + s = CG_ConfigString( CS_SOUNDS + es->eventParm ); + if ( !strstr( s, ".wav" ) ) { + if ( CG_SoundPlaySoundScript( s, NULL, es->number ) ) { + break; + } + // try with .wav + break; // RF, all sounds should have extension + //Q_strncpyz( tempStr, s, sizeof(tempStr) ); + //Q_strcat( tempStr, sizeof(tempStr), ".wav" ); + //s = tempStr; + } + // done. + if ( cgs.gameSounds[ es->eventParm ] ) { + trap_S_StartSound( NULL, es->number, CHAN_VOICE, cgs.gameSounds[ es->eventParm ] ); + } else { + s = CG_ConfigString( CS_SOUNDS + es->eventParm ); + trap_S_StartSound( NULL, es->number, CHAN_VOICE, CG_CustomSound( es->number, s ) ); + } + break; + + case EV_GLOBAL_SOUND: // play from the player's head so it never diminishes + DEBUGNAME( "EV_GLOBAL_SOUND" ); + // Ridah, check for a sound script + s = CG_ConfigString( CS_SOUNDS + es->eventParm ); + if ( !strstr( s, ".wav" ) ) { + if ( CG_SoundPlaySoundScript( s, NULL, es->number ) ) { + break; + } + // try with .wav + break; // RF, all sounds should have extension + //Q_strncpyz( tempStr, s, sizeof(tempStr) ); + //Q_strcat( tempStr, sizeof(tempStr), ".wav" ); + //s = tempStr; + } + // done. + if ( cgs.gameSounds[ es->eventParm ] ) { + trap_S_StartSound( NULL, cg.snap->ps.clientNum, CHAN_AUTO, cgs.gameSounds[ es->eventParm ] ); + } else { + s = CG_ConfigString( CS_SOUNDS + es->eventParm ); + trap_S_StartSound( NULL, cg.snap->ps.clientNum, CHAN_AUTO, CG_CustomSound( es->number, s ) ); + } + break; + + + case EV_PAIN: + // local player sounds are triggered in CG_CheckLocalSounds, + // so ignore events on the player + DEBUGNAME( "EV_PAIN" ); + if ( cent->currentState.number != cg.snap->ps.clientNum ) { + CG_PainEvent( cent, es->eventParm, qfalse ); + } + break; + + case EV_CROUCH_PAIN: + // local player sounds are triggered in CG_CheckLocalSounds, + // so ignore events on the player + DEBUGNAME( "EV_PAIN" ); + if ( cent->currentState.number != cg.snap->ps.clientNum ) { + CG_PainEvent( cent, es->eventParm, qtrue ); + } + break; + + case EV_DEATH1: + case EV_DEATH2: + case EV_DEATH3: + DEBUGNAME( "EV_DEATHx" ); + +// trap_S_StartSoundEx(NULL, cent->currentState.number, CHAN_WEAPON, 0, SND_CUTOFF_ALL); // kill weapon sound (could be reloading) +// trap_S_StopStreamingSound( es->number ); // kill speech + + trap_S_StartSound( NULL, es->number, CHAN_VOICE, + CG_CustomSound( es->number, va( "*death%i.wav", event - EV_DEATH1 + 1 ) ) ); + break; + + case EV_ENTDEATH: + DEBUGNAME( "EV_ENTDEATH" ); + switch ( es->eventParm ) { +// case ET_SPOTLIGHT: +// CG_Explodef(cent->lerpOrigin, normalized_direction, 50, 1, cgs.media.sfx_bullet_glasshit[0], 1, 0, cent->currentState.number, qfalse); +// break; + + case ET_ALARMBOX: + // all this crap shouldn't be in here, but we don't have a generic entry_point into + // explosions that's reasonable. This will be an early thing to get fixed in future work + { + int i, j; + vec3_t sprVel, sprOrg; + + VectorCopy( cent->lerpAngles, dir ); + VectorNormalize( dir ); + + // explosion sprite animation +// VectorScale( dir, 16, sprVel ); + VectorScale( dir, 6, sprVel ); + + for ( i = 0; i < 5; i++ ) { + for ( j = 0; j < 3; j++ ) + sprOrg[j] = cent->lerpOrigin[j] + 2 * dir[j] + 4 * crandom(); + sprVel[2] += rand() % 10; + CG_ParticleExplosion( "blacksmokeanimb", sprOrg, sprVel, 3500 + rand() % 250, 4, 50 + rand() % 20 ); + } + + CG_AddDebris( cent->lerpOrigin, + dir, + 80, // speed + 1000, // duration + 3 + rand() % 2 ); // count + } + break; + } + + break; + + case EV_OBITUARY: + DEBUGNAME( "EV_OBITUARY" ); + CG_Obituary( es ); + break; + + // + // powerup events + // + case EV_POWERUP_QUAD: + DEBUGNAME( "EV_POWERUP_QUAD" ); + if ( es->number == cg.snap->ps.clientNum ) { + cg.powerupActive = PW_QUAD; + cg.powerupTime = cg.time; + } + trap_S_StartSound( NULL, es->number, CHAN_ITEM, cgs.media.quadSound ); + break; + case EV_POWERUP_BATTLESUIT: + DEBUGNAME( "EV_POWERUP_BATTLESUIT" ); + if ( es->number == cg.snap->ps.clientNum ) { + cg.powerupActive = PW_BATTLESUIT; + cg.powerupTime = cg.time; + } + trap_S_StartSound( NULL, es->number, CHAN_ITEM, trap_S_RegisterSound( "sound/items/protect3.wav" ) ); + break; + case EV_POWERUP_REGEN: + DEBUGNAME( "EV_POWERUP_REGEN" ); + if ( es->number == cg.snap->ps.clientNum ) { + cg.powerupActive = PW_REGEN; + cg.powerupTime = cg.time; + } + trap_S_StartSound( NULL, es->number, CHAN_ITEM, trap_S_RegisterSound( "sound/items/regen.wav" ) ); + break; + + case EV_LOSE_HAT: + DEBUGNAME( "EV_LOSE_HAT" ); + ByteToDir( es->eventParm, dir ); + // (SA) okay, some events not getting through, so I'm still testing. Works except for that tho. +// CG_Printf("lose had dir: %2.4f %2.4f %2.4f\n", dir[0], dir[1], dir[2]); + CG_LoseHat( cent, dir ); + break; + + case EV_GIB_HEAD: + DEBUGNAME( "EV_GIB_HEAD" ); + trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.gibSound ); + { + vec3_t vhead, vtorso, vlegs; + CG_GetBleedOrigin( vhead, vtorso, vlegs, cent->currentState.clientNum ); + CG_GibHead( vhead ); + } + break; + + case EV_GIB_PLAYER: + DEBUGNAME( "EV_GIB_PLAYER" ); + if ( es->aiChar == AICHAR_ZOMBIE ) { + trap_S_StartSound( es->pos.trBase, es->number, CHAN_VOICE, cgs.media.zombieDeathSound ); + } else { + trap_S_StartSound( es->pos.trBase, es->number, CHAN_VOICE, cgs.media.gibSound ); + } + ByteToDir( es->eventParm, dir ); + CG_GibPlayer( cent, cent->lerpOrigin, dir ); + break; + +//----(SA) added + case EV_STOPSTREAMINGSOUND: + DEBUGNAME( "EV_STOPLOOPINGSOUND" ); + trap_S_StopStreamingSound( es->number ); + + // hope this does not cause trouble. + // can re-work if this causes trouble + // this is only called on death now, so stop the weapon sound now too + trap_S_StartSoundEx( NULL, es->number, CHAN_WEAPON, 0, SND_CUTOFF_ALL ); // kill weapon sound (could be reloading) + + break; +//----(SA) end + + case EV_STOPLOOPINGSOUND: + DEBUGNAME( "EV_STOPLOOPINGSOUND" ); + trap_S_StopLoopingSound( es->number ); + es->loopSound = 0; + break; + + case EV_DEBUG_LINE: + DEBUGNAME( "EV_DEBUG_LINE" ); + CG_Beam( cent ); + break; + + // Rafael particles + case EV_SMOKE: + DEBUGNAME( "EV_SMOKE" ); + if ( cent->currentState.density == 3 ) { // cannon + CG_ParticleSmoke( cgs.media.smokePuffShaderdirty, cent ); + } else if ( cent->currentState.density == 7 ) { // steam + // steam from panzerfaust casing + CG_ParticleImpactSmokePuffExtended( cgs.media.smokeParticleShader, cent->currentState.origin, tv( 0,0,1 ), 8, 1000, 8, 20, 20, 0.25f ); + } else if ( !( cent->currentState.density ) ) { + CG_ParticleSmoke( cgs.media.smokePuffShader, cent ); + } else { + CG_ParticleSmoke( cgs.media.smokePuffShader, cent ); + } + break; + // done. + + case EV_FLAMETHROWER_EFFECT: + { + int old; + old = cent->currentState.aiChar; + cent->currentState.aiChar = AICHAR_ZOMBIE; + + // shoot this only in bursts + + // (SA) this first one doesn't seem to do anything. ? + +// if ((cg.time+cent->currentState.number*100)%1000 > 200) { +// CG_FireFlameChunks( cent, cent->currentState.origin, cent->lerpAngles, 0.1, qfalse, 1 ); +// CG_FireFlameChunks( cent, cent->currentState.origin, cent->currentState.apos.trBase, 0.1, qfalse, 1 ); +// } +// else +// CG_FireFlameChunks( cent, cent->currentState.origin, cent->lerpAngles, 0.6, 2, 1 ); + CG_FireFlameChunks( cent, cent->currentState.origin, cent->currentState.apos.trBase, 0.6, 2, 1 ); + + cent->currentState.aiChar = old; + } + break; + + case EV_DUST: + CG_ParticleDust( cent, cent->currentState.origin, cent->currentState.angles ); + break; + + case EV_RUMBLE_EFX: + { + float pitch, yaw; + pitch = cent->currentState.angles[0]; + yaw = cent->currentState.angles[1]; + CG_RumbleEfx( pitch, yaw ); + } + break; + + case EV_CONCUSSIVE: + CG_Concussive( cent ); + break; + +// generic particle emitter that uses client-side particle scripts + case EV_EMITTER: + { + localEntity_t *le; + le = CG_AllocLocalEntity(); + le->leType = LE_EMITTER; + le->startTime = cg.time; + le->endTime = le->startTime + cent->currentState.time; // 'time' stores lifetime + le->pos.trType = TR_STATIONARY; + VectorCopy( cent->currentState.origin, le->pos.trBase ); + VectorCopy( cent->currentState.origin2, le->angles.trBase ); + le->ownerNum = 0; + le->radius = cent->currentState.density; // 'density' stores pressure + le->headJuncIndex = cent->currentState.teamNum; // 'type' + } + break; + + case EV_OILPARTICLES: + CG_Particle_OilParticle( cgs.media.oilParticle, cent->currentState.origin, cent->currentState.origin2, cent->currentState.time, cent->currentState.density ); + break; + case EV_OILSLICK: + CG_Particle_OilSlick( cgs.media.oilSlick, cent ); + break; + case EV_OILSLICKREMOVE: + CG_OilSlickRemove( cent ); + break; + + case EV_MG42EFX: + CG_MG42EFX( cent ); + break; + + case EV_FLAKGUN1: + CG_FLAKEFX( cent, 1 ); + break; + case EV_FLAKGUN2: + CG_FLAKEFX( cent, 2 ); + break; + case EV_FLAKGUN3: + CG_FLAKEFX( cent, 3 ); + break; + case EV_FLAKGUN4: + CG_FLAKEFX( cent, 4 ); + break; + + case EV_SPARKS_ELECTRIC: + case EV_SPARKS: + { + int numsparks; + int i; + int duration; + float x,y; + float speed; + vec3_t source, dest; + + if ( !( cent->currentState.density ) ) { + cent->currentState.density = 1; + } + numsparks = rand() % cent->currentState.density; + duration = cent->currentState.frame; + x = cent->currentState.angles2[0]; + y = cent->currentState.angles2[1]; + speed = cent->currentState.angles2[2]; + + if ( !numsparks ) { + numsparks = 1; + } + for ( i = 0; i < numsparks; i++ ) + { + + if ( event == EV_SPARKS_ELECTRIC ) { + VectorCopy( cent->currentState.origin, source ); + + VectorCopy( source, dest ); + dest[0] += ( ( rand() & 31 ) - 16 ); + dest[1] += ( ( rand() & 31 ) - 16 ); + dest[2] += ( ( rand() & 31 ) - 16 ); + + CG_Tracer( source, dest, 1 ); + } else { + CG_ParticleSparks( cent->currentState.origin, cent->currentState.angles, duration, x, y, speed ); + } + + } + + } + break; + + case EV_GUNSPARKS: + { + int numsparks; + int speed; + //int count; + + numsparks = cent->currentState.density; + speed = cent->currentState.angles2[2]; + + CG_AddBulletParticles( cent->currentState.origin, cent->currentState.angles, speed, 800, numsparks, 1.0f ); + + } + break; + + // Rafael bats + case EV_BATS: + { + int i; + for ( i = 0; i < cent->currentState.density; i++ ) + CG_ParticleBats( cgs.media.bats[0], cent ); + } + break; + + case EV_BATS_UPDATEPOSITION: + CG_BatsUpdatePosition( cent ); + break; + + case EV_BATS_DEATH: + CG_BatDeath( cent ); + break; + + // Rafael snow pvs check + case EV_SNOW_ON: + CG_SnowLink( cent, qtrue ); + break; + + case EV_SNOW_OFF: + CG_SnowLink( cent, qfalse ); + break; + + + case EV_SNOWFLURRY: + CG_ParticleSnowFlurry( cgs.media.snowShader, cent ); + break; + + //----(SA) + + // for func_exploding + case EV_EXPLODE: + DEBUGNAME( "EV_EXPLODE" ); + ByteToDir( es->eventParm, dir ); + CG_Explode( cent, position, dir, 0 ); + break; + + // for target_effect + case EV_EFFECT: + DEBUGNAME( "EV_EFFECT" ); +// ByteToDir( es->eventParm, dir ); + CG_Effect( cent, position, dir ); + break; + + //----(SA) done + + case EV_MORTAREFX: // mortar firing + DEBUGNAME( "EV_MORTAREFX" ); + CG_MortarEFX( cent ); + break; + + case EV_SHARD: + ByteToDir( es->eventParm, dir ); + CG_Shard( cent, position, dir ); + break; + + case EV_JUNK: + ByteToDir( es->eventParm, dir ); + { + int i; + int rval; + + rval = rand() % 3 + 3; + + for ( i = 0; i < rval; i++ ) + CG_ShardJunk( cent, position, dir ); + } + break; + + case EV_SNIPER_SOUND: + // trap_S_StartSound( es->pos.trBase, -1, CHAN_AUTO, cgs.media.snipersound ); + // trap_S_StartSound (NULL, es->number, CHAN_AUTO, cgs.media.snipersound ); + trap_S_StartSound( NULL, cent->currentState.number, CHAN_WEAPON, cgs.media.snipersound ); + break; + + case EV_SPAWN_SPIRIT: + CG_SpawnSpirit( cent ); + break; + + default: + DEBUGNAME( "UNKNOWN" ); + CG_Error( "Unknown event: %i", event ); + break; + } + + + { + int rval; + + rval = rand() & 3; + + if ( splashfootstepcnt != rval ) { + splashfootstepcnt = rval; + } else { + splashfootstepcnt++; + } + + if ( splashfootstepcnt > 3 ) { + splashfootstepcnt = 0; + } + + + if ( footstepcnt != rval ) { + footstepcnt = rval; + } else { + footstepcnt++; + } + + if ( footstepcnt > 3 ) { + footstepcnt = 0; + } + } +} + + +/* +============== +CG_CheckEvents + +============== +*/ +void CG_CheckEvents( centity_t *cent ) { + int i, event; + + // calculate the position at exactly the frame time + BG_EvaluateTrajectory( ¢->currentState.pos, cg.snap->serverTime, cent->lerpOrigin ); + CG_SetEntitySoundPosition( cent ); + + // check for event-only entities + if ( cent->currentState.eType > ET_EVENTS ) { + if ( cent->previousEvent ) { + goto skipEvent; + //return; // already fired + } + // if this is a player event set the entity number of the client entity number +//(SA) note: EF_PLAYER_EVENT never set +// if ( cent->currentState.eFlags & EF_PLAYER_EVENT ) { +// cent->currentState.number = cent->currentState.otherEntityNum; +// } + + cent->previousEvent = 1; + + cent->currentState.event = cent->currentState.eType - ET_EVENTS; + } else { + + // DHM - Nerve :: Entities that make it here are Not TempEntities. + // As far as we could tell, for all non-TempEntities, the + // circular 'events' list contains the valid events. So we + // skip processing the single 'event' field and go straight + // to the circular list. + + goto skipEvent; + /* + // check for events riding with another entity + if ( cent->currentState.event == cent->previousEvent ) { + goto skipEvent; + //return; + } + cent->previousEvent = cent->currentState.event; + if ( ( cent->currentState.event & ~EV_EVENT_BITS ) == 0 ) { + goto skipEvent; + //return; + } + */ + // dhm - end + } + + CG_EntityEvent( cent, cent->lerpOrigin ); + +skipEvent: + + // check the sequencial list + // if the eventSequence is zero, then there are no events + if ( !cent->currentState.eventSequence ) { + cent->previousEventSequence = 0; + } + // if we've added more events than can fit into the list, make sure we only add them once + if ( cent->currentState.eventSequence < cent->previousEventSequence ) { + cent->previousEventSequence -= ( 1 << 8 ); // eventSequence is sent as an 8-bit through network stream + } + if ( cent->currentState.eventSequence - cent->previousEventSequence > MAX_EVENTS ) { + cent->previousEventSequence = cent->currentState.eventSequence - MAX_EVENTS; + } + for ( i = cent->previousEventSequence ; i != cent->currentState.eventSequence; i++ ) { + event = cent->currentState.events[ i & ( MAX_EVENTS - 1 ) ]; + + cent->currentState.event = event; + cent->currentState.eventParm = cent->currentState.eventParms[ i & ( MAX_EVENTS - 1 ) ]; + CG_EntityEvent( cent, cent->lerpOrigin ); + } + cent->previousEventSequence = cent->currentState.eventSequence; + + // set the event back so we don't think it's changed next frame (unless it really has) + cent->currentState.event = cent->previousEvent; +} + + +/* +void CG_CheckEvents( centity_t *cent ) { + int i, event; + + // calculate the position at exactly the frame time + BG_EvaluateTrajectory( ¢->currentState.pos, cg.snap->serverTime, cent->lerpOrigin ); + CG_SetEntitySoundPosition( cent ); + + // check for event-only entities + if ( cent->currentState.eType > ET_EVENTS ) { + if ( !cent->previousEvent ) { + cent->previousEvent = 1; + cent->currentState.event = cent->currentState.eType - ET_EVENTS; + CG_EntityEvent( cent, cent->lerpOrigin ); + } + } else { + // check for events riding with another entity + if ( cent->currentState.event != cent->previousEvent ) { + cent->previousEvent = cent->currentState.event; + if ( cent->currentState.event & ~EV_EVENT_BITS ) { + CG_EntityEvent( cent, cent->lerpOrigin ); + } + } + } + + // check the sequencial list + // if we've added more events than can fit into the list, make sure we only add them once + if (cent->currentState.eventSequence < cent->previousEventSequence) { + cent->previousEventSequence -= (1 << 8); // eventSequence is sent as an 8-bit through network stream + } + if (cent->currentState.eventSequence - cent->previousEventSequence > MAX_EVENTS) { + cent->previousEventSequence = cent->currentState.eventSequence - MAX_EVENTS; + } + for ( i = cent->previousEventSequence ; i != cent->currentState.eventSequence; i++ ) { + event = cent->currentState.events[ i & (MAX_EVENTS-1) ]; + + cent->currentState.event = event; + cent->currentState.eventParm = cent->currentState.eventParms[ i & (MAX_EVENTS-1) ]; + CG_EntityEvent( cent, cent->lerpOrigin ); + } + cent->previousEventSequence = cent->currentState.eventSequence; + + // set the event back so we don't think it's changed next frame (unless it really has) + cent->currentState.event = cent->previousEvent; +} +*/ diff --git a/src/cgame/cg_flamethrower.c b/src/cgame/cg_flamethrower.c new file mode 100644 index 0000000..17a39d6 --- /dev/null +++ b/src/cgame/cg_flamethrower.c @@ -0,0 +1,1696 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +// cg_flamethrower.c - special code for the flamethrower effects +// +// the flameChunks behave similarly to the trailJunc's, except they are rendered differently, and +// also interact with the environment +// +// NOTE: some AI's are treated different, mostly for aesthetical reasons. + +#include "cg_local.h" + +// a flameChunk is a ball or section of fuel which goes from fuel->blue ignition->flame ball +// optimization is necessary, since lots of these will be spawned, but as they grow, they can be +// merged so that less overdraw occurs +typedef struct flameChunk_s +{ + struct flameChunk_s *nextGlobal, *prevGlobal; // next junction in the global list it is in (free or used) + struct flameChunk_s *nextFlameChunk; // next junction in the trail + struct flameChunk_s *nextHead, *prevHead; // next head junc in the world + + qboolean inuse; + qboolean dead; // set when a chunk is effectively inactive, but waiting to be freed + int ownerCent; // cent that spawned us + + int timeStart, timeEnd; + float sizeStart, sizeMax; // start small, increase if we slow down + float sizeRand; + float sizeRate; // rate per ms, variable according to speed (larger if moving slower) + int sizeTime; + vec3_t baseOrg; + int baseOrgTime; + vec3_t velDir; + float velSpeed; // flame chunks should start with a fast velocity, then slow down if there is nothing behind them pushing them along + float rollAngle; + qboolean ignitionOnly; + int blueLife; + float gravity; + vec3_t startVelDir; + float speedScale; + + // current variables + vec3_t org; + float size; + float lifeFrac; // 0.0 (baby) -> 1.0 (aged) + + int lastFriction, lastFrictionTake; + vec3_t parentFwd; +} flameChunk_t; + +#define MAX_FLAME_CHUNKS 2048 +static flameChunk_t flameChunks[MAX_FLAME_CHUNKS]; +static flameChunk_t *freeFlameChunks, *activeFlameChunks, *headFlameChunks; + +static qboolean initFlameChunks = qfalse; + +static int numFlameChunksInuse; + +// this structure stores information relevant to each cent in the game, this way we keep +// the flamethrower data seperate to the rest of the code, which helps if we decide against +// using this weapon in the game +typedef struct centFlameInfo_s +{ + int lastClientFrame; // client frame that we last fired the flamethrower + vec3_t lastAngles; // angles at last firing + vec3_t lastOrigin; // origin at last firing + flameChunk_t + *lastFlameChunk; // flame chunk we last spawned + int lastSoundUpdate; + + qboolean lastFiring; + + qboolean silent; //----(SA) added + + int lastDmgUpdate; // time we last told server about this ent's flame damage + int lastDmgCheck; // only check once per 100ms + int lastDmgEnemy; // entity that inflicted the damage +} centFlameInfo_t; + +static centFlameInfo_t centFlameInfo[MAX_GENTITIES]; + +typedef struct +{ +// float fireVolume; // not needed, since we add individual loop sources for the flame, so it gets spacialized + float blowVolume; + float streamVolume; +} flameSoundStatus_t; + +static flameSoundStatus_t centFlameStatus[MAX_GENTITIES]; + +// procedure defs +flameChunk_t *CG_SpawnFlameChunk( flameChunk_t *headFlameChunk ); +void CG_FlameCalcOrg( flameChunk_t *f, int time, vec3_t outOrg ); +void CG_FlameGetMuzzlePoint( vec3_t org, vec3_t fwd, vec3_t right, vec3_t up, vec3_t outPos ); + +// these must be globals, since they cannot expand or contract, since that might result in them getting +// stuck in geometry. therefore when a chunk hits a surface, we should deflect it away from the surface +// slightly, rather than running along it, so that as the chink grows, the sprites don't sink into the +// wall too much. +static vec3_t flameChunkMins = {-4, -4, -4}; +static vec3_t flameChunkMaxs = { 4, 4, 4}; + +// these define how the flame looks +#define FLAME_START_SIZE 1.0 +#define FLAME_START_MAX_SIZE 140.0 // when the flame is spawned, it should endevour to reach this size +#define FLAME_START_MAX_SIZE_RAND 60.0 +#define FLAME_MAX_SIZE 200.0 // flame sprites cannot be larger than this +#define FLAME_MIN_MAXSIZE 40.0 // don't ever let the sizeMax go less than this +#define FLAME_START_SPEED 950.0 // speed of flame as it leaves the nozzle +#define FLAME_MIN_SPEED 60.0 //200.0 +#define FLAME_CHUNK_DIST 4.0 // space in between chunks when fired + +#define FLAME_BLUE_LENGTH 230.0 +#define FLAME_BLUE_MAX_ALPHA 1.0 + +#define FLAME_FUEL_LENGTH 48.0 +#define FLAME_FUEL_MAX_ALPHA 0.35 +#define FLAME_FUEL_MIN_WIDTH 1.0 + +// these are calculated (don't change) +#define FLAME_LENGTH ( FLAMETHROWER_RANGE + 50.0 ) // NOTE: only modify the range, since this should always reflect that range + +#define FLAME_LIFETIME (int)( ( FLAME_LENGTH / FLAME_START_SPEED ) * 1000 ) // life duration in milliseconds +#define FLAME_FRICTION_PER_SEC ( 1.0 * FLAME_START_SPEED ) +#define FLAME_BLUE_LIFE (int)( ( FLAME_BLUE_LENGTH / FLAME_START_SPEED ) * 1000 ) +#define FLAME_FUEL_LIFE (int)( ( FLAME_FUEL_LENGTH / FLAME_START_SPEED ) * 1000 ) +#define FLAME_FUEL_FADEIN_TIME ( 0.2 * FLAME_FUEL_LIFE ) + +#define FLAME_BLUE_FADEIN_TIME( x ) ( 0.2 * x ) +#define FLAME_BLUE_FADEOUT_TIME( x ) ( 0.05 * x ) +#define GET_FLAME_BLUE_SIZE_SPEED( x ) ( ( (float)x / FLAME_LIFETIME ) / 1.0 ) // x is the current sizeMax +#define GET_FLAME_SIZE_SPEED( x ) ( ( (float)x / FLAME_LIFETIME ) / 0.6 ) // x is the current sizeMax + +//#define FLAME_MIN_DRAWSIZE 20 + +// enable this for the fuel stream +//#define FLAME_ENABLE_FUEL_STREAM + +// enable this for dynamic lighting around flames +//#define FLAMETHROW_LIGHTS + +// disable this to stop rotating flames (this is variable so we can change it at run-time) +int rotatingFlames = qtrue; + +/* +=============== +CG_FlameLerpVec +=============== +*/ +void CG_FlameLerpVec( const vec3_t oldV, const vec3_t newV, float backLerp, vec3_t outV ) { + VectorScale( newV, ( 1.0 - backLerp ), outV ); + VectorMA( outV, backLerp, oldV, outV ); +} + +/* +=============== +CG_FlameAdjustSpeed +=============== +*/ +void CG_FlameAdjustSpeed( flameChunk_t *f, float change ) { + if ( !f->velSpeed && !change ) { + return; + } + + f->velSpeed += change; + if ( f->velSpeed < FLAME_MIN_SPEED ) { + f->velSpeed = FLAME_MIN_SPEED; + } +/* + f->sizeMax = FLAME_START_MAX_SIZE + ((1.0 - (f->velSpeed / FLAME_START_SPEED)) * (FLAME_MAX_SIZE - FLAME_START_MAX_SIZE)); + + if (!f->ignitionOnly) { + if ((cg.time - f->timeStart) < f->blueLife) { + f->sizeRate = GET_FLAME_BLUE_SIZE_SPEED(f->sizeMax); + } else { + f->sizeMax += f->sizeRand; + if (f->sizeMax < FLAME_MIN_MAXSIZE) + f->sizeMax = FLAME_MIN_MAXSIZE; + f->sizeRate = GET_FLAME_SIZE_SPEED(f->sizeMax); + } + } +*/ +} + +/* +=============== +CG_FireFlameChunks + + The given entity is firing a flamethrower + + flags: + 1 - use 'angles' + 2 - silent +=============== +*/ +void CG_FireFlameChunks( centity_t *cent, vec3_t origin, vec3_t angles, float speedScale, qboolean firing, int flags ) { + centFlameInfo_t *centInfo; + flameChunk_t *f, *of; + vec3_t lastFwd, thisFwd, fwd; + vec3_t lastUp, thisUp, up; + vec3_t lastRight, thisRight, right; + vec3_t thisOrg, lastOrg, org; + double timeInc, backLerp, fracInc; + int t; + double ft; + trace_t trace; + vec3_t parentFwd; + int flameLife; +#define MAX_CHUNKS_PER_CALL 30 + + + centInfo = ¢FlameInfo[cent->currentState.number]; + +//----(SA) added + if ( flags & 2 ) { // silent + centInfo->silent = qtrue; + } +//----(SA) end + + // only do one ignition flame for zombie + if ( cent->currentState.aiChar == AICHAR_ZOMBIE && !firing ) { + if ( !centInfo->lastFiring ) { + return; + } + } + + // if this point is in water, no flames + if ( CG_PointContents( origin, cent->currentState.number ) & ( CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ) ) { + return; + } + + // for any other character or in 3rd person view, use entity angles for friction + if ( cent->currentState.number != cg.snap->ps.clientNum || cg_thirdPerson.integer ) { + if ( flags & 1 ) { // use 'angles' + AngleVectors( angles, parentFwd, NULL, NULL ); + } else { + AngleVectors( cent->currentState.angles, parentFwd, NULL, NULL ); + } + } else { + AngleVectors( angles, parentFwd, NULL, NULL ); + } + + // tweak the speed of the flame + //if (cent->currentState.aiChar) + // speedScale *= 0.8; + //else + // speedScale *= 1.0; + + AngleVectors( angles, thisFwd, thisRight, thisUp ); + VectorCopy( origin, thisOrg ); + + // if this entity was firing last frame, interpolate the angles as we spawn the chunks that + // fired over the last frame + if ( ( centInfo->lastClientFrame == cent->currentState.frame ) && + ( centInfo->lastFlameChunk && centInfo->lastFiring == firing ) ) { + AngleVectors( centInfo->lastAngles, lastFwd, lastRight, lastUp ); + VectorCopy( centInfo->lastOrigin, lastOrg ); + centInfo->lastFiring = firing; + + flameLife = FLAME_LIFETIME * ( 1.0 / ( 0.5 + 0.5 * speedScale ) ); + of = centInfo->lastFlameChunk; + timeInc = 1000.0 * ( firing ? 1.0 : 0.5 ) * ( FLAME_CHUNK_DIST /**(1.0+crandom()*0.1)*/ / ( FLAME_START_SPEED * speedScale ) ); + ft = ( (double)of->timeStart ); + // never spawn too many chunk in one call (framerate preservation) + if ( ( ( (double)cg.time - ft ) / timeInc ) > MAX_CHUNKS_PER_CALL ) { + timeInc = ( (double)cg.time - ft ) / MAX_CHUNKS_PER_CALL; + } + ft += timeInc; + t = (int)ft; + fracInc = timeInc / (double)( cg.time - of->timeStart ); + backLerp = 1.0 - fracInc; + while ( t <= cg.time ) { + // spawn a new chunk + CG_FlameLerpVec( lastOrg, thisOrg, backLerp, org ); + + CG_Trace( &trace, org, flameChunkMins, flameChunkMaxs, org, cent->currentState.number, MASK_SHOT ); + if ( trace.startsolid && trace.entityNum >= cgs.maxclients ) { + centInfo->lastFlameChunk = NULL; + return; // don't spawn inside a wall + } + + f = CG_SpawnFlameChunk( of ); + + if ( !f ) { + CG_Printf( "Out of flame chunks\n" ); + return; + } + + CG_FlameLerpVec( lastFwd, thisFwd, backLerp, fwd ); + VectorNormalize( fwd ); + CG_FlameLerpVec( lastRight, thisRight, backLerp, right ); + VectorNormalize( right ); + CG_FlameLerpVec( lastUp, thisUp, backLerp, up ); + VectorNormalize( up ); + + f->timeStart = t; + f->timeEnd = t + flameLife; // * (firing ? 1.0 : 0.2); + f->sizeStart = f->size = FLAME_START_SIZE * speedScale; // * (firing ? 1.0 : 0.2); + f->sizeMax = ( 0.3 + 0.7 * speedScale ) * ( FLAME_START_MAX_SIZE + f->sizeRand * ( firing ? 1.0 : 0.0 ) ); + //if (cent->currentState.aiChar == AICHAR_ZOMBIE) { + // f->sizeRand = crandom() * FLAME_START_MAX_SIZE_RAND; + // f->sizeMax = 20; + //} else { + f->sizeRand = 0; + //} + if ( f->sizeMax > FLAME_MAX_SIZE ) { + f->sizeMax = FLAME_MAX_SIZE; + } + f->sizeRate = GET_FLAME_BLUE_SIZE_SPEED( f->sizeMax * speedScale * ( 1.0 + ( 0.5 * (float)!firing ) ) ); + f->sizeTime = t; + VectorCopy( org, f->baseOrg ); + f->baseOrgTime = t; + VectorCopy( fwd, f->velDir ); + VectorCopy( fwd, f->startVelDir ); + f->speedScale = speedScale; + // randomize the direction slightly + /* + if (!firing) { + f->velDir[0] += 0.03*sin((float)cg.time/134); + f->velDir[1] += 0.03*sin((float)cg.time/567); + f->velDir[2] += 0.03*cos((float)cg.time/3435); + } + */ + VectorNormalize( f->velDir ); + f->velSpeed = FLAME_START_SPEED * ( 0.3 + 0.7 * speedScale ); + f->ownerCent = cent->currentState.number; + f->rollAngle = crandom() * 179; + f->ignitionOnly = !firing; + if ( !firing ) { + f->gravity = -150; + f->blueLife = FLAME_BLUE_LIFE * 0.5; +// f->blueLife = (int)(0.3*(1.0/speedScale)*(float)FLAME_BLUE_LIFE); + } else { + f->gravity = 0; + f->blueLife = FLAME_BLUE_LIFE; + } + f->lastFriction = cg.time; + f->lastFrictionTake = cg.time; + VectorCopy( parentFwd, f->parentFwd ); + + ft += timeInc; + // always spawn a chunk right on the current time + if ( (int)ft > cg.time && t < cg.time ) { + ft = (double)cg.time; + t = cg.time; + backLerp = 0; + } else { + t = (int)floor( ft ); + backLerp -= fracInc; + } + centInfo->lastFlameChunk = of = f; + } + } else { + + centInfo->lastFiring = firing; + t = cg.time; + + VectorCopy( thisOrg, org ); + + // if igniting, play start sound + //if (firing) { + // trap_S_StartSound( org, cent->currentState.number, CHAN_AUTO, cgs.media.flameStartSound ); + //} + + CG_Trace( &trace, org, flameChunkMins, flameChunkMaxs, org, cent->currentState.number, MASK_SHOT ); + if ( trace.startsolid && trace.entityNum >= cgs.maxclients ) { + centInfo->lastFlameChunk = NULL; + return; // don't spawn inside a wall + } + + // just fire a single chunk to get us started + f = CG_SpawnFlameChunk( NULL ); + + if ( !f ) { + CG_Printf( "Out of flame chunks\n" ); + return; + } + + VectorCopy( thisFwd, fwd ); + VectorCopy( thisUp, up ); + VectorCopy( thisRight, right ); + + f->timeStart = cg.time; + f->timeEnd = cg.time + FLAME_LIFETIME * ( 1.0 / ( 0.5 + 0.5 * speedScale ) ); // * (firing ? 1.0 : 0.2); + //f->timeEnd = cg.time + FLAME_LIFETIME * (1.0/speedScale); + f->sizeStart = f->size = FLAME_START_SIZE * speedScale; + f->sizeMax = FLAME_START_MAX_SIZE * ( 0.3 + 0.7 * speedScale ); + if ( f->sizeMax > FLAME_MAX_SIZE ) { + f->sizeMax = FLAME_MAX_SIZE; + } + //if (cent->currentState.aiChar == AICHAR_ZOMBIE) { + // f->sizeRand = crandom() * FLAME_START_MAX_SIZE_RAND; + // f->sizeMax = 20; + //} else { + f->sizeRand = 0; + //} + f->sizeRate = GET_FLAME_BLUE_SIZE_SPEED( f->sizeMax * speedScale ); + f->sizeTime = cg.time; + VectorCopy( org, f->baseOrg ); + f->baseOrgTime = cg.time; + VectorCopy( fwd, f->velDir ); + VectorCopy( fwd, f->startVelDir ); + f->velSpeed = FLAME_START_SPEED * ( 0.3 + 0.7 * speedScale ); + f->ownerCent = cent->currentState.number; + f->rollAngle = crandom() * 179; + f->ignitionOnly = !firing; + f->speedScale = speedScale; + if ( !firing ) { + f->gravity = -100; + f->blueLife = (int)( 0.3 * ( 1.0 / speedScale ) * (float)FLAME_BLUE_LIFE ); + } else { + f->gravity = 0; + f->blueLife = FLAME_BLUE_LIFE; + } + f->lastFriction = cg.time; + f->lastFrictionTake = cg.time; + VectorCopy( parentFwd, f->parentFwd ); + + centInfo->lastFlameChunk = f; + } + + /*if (centInfo->lastFlameChunk && !cent->currentState.aiChar)*/ { + // push them along + float frametime, dot; + + f = centInfo->lastFlameChunk; + while ( f ) { + + if ( f->lastFriction < cg.time - 50 ) { + frametime = (float)( cg.time - f->lastFriction ) / 1000.0; + f->lastFriction = cg.time; + dot = DotProduct( parentFwd, f->parentFwd ); + if ( dot >= 0.99 ) { + dot -= 0.99; + dot *= ( 1.0 / ( 1.0 - 0.99 ) ); + CG_FlameAdjustSpeed( f, /*(0.3+0.7*f->speedScale) **/ 0.5 * frametime * FLAME_FRICTION_PER_SEC * pow( dot,4 ) ); + } + } + + f = f->nextFlameChunk; + } + } + + VectorCopy( angles, centInfo->lastAngles ); + VectorCopy( origin, centInfo->lastOrigin ); + centInfo->lastClientFrame = cent->currentState.frame; +} + +/* +=============== +CG_ClearFlameChunks +=============== +*/ +void CG_ClearFlameChunks( void ) { + int i; + + memset( flameChunks, 0, sizeof( flameChunks ) ); + memset( centFlameInfo, 0, sizeof( centFlameInfo ) ); + + freeFlameChunks = flameChunks; + activeFlameChunks = NULL; + headFlameChunks = NULL; + + for ( i = 0 ; i < MAX_FLAME_CHUNKS ; i++ ) + { + flameChunks[i].nextGlobal = &flameChunks[i + 1]; + + if ( i > 0 ) { + flameChunks[i].prevGlobal = &flameChunks[i - 1]; + } else { + flameChunks[i].prevGlobal = NULL; + } + + flameChunks[i].inuse = qfalse; + } + flameChunks[MAX_FLAME_CHUNKS - 1].nextGlobal = NULL; + + initFlameChunks = qtrue; + numFlameChunksInuse = 0; +} + +/* +=============== +CG_SpawnFlameChunk +=============== +*/ +flameChunk_t *CG_SpawnFlameChunk( flameChunk_t *headFlameChunk ) { + flameChunk_t *f; + + if ( !freeFlameChunks ) { + //CG_ClearFlameChunks(); + return NULL; + } + + if ( headFlameChunks && headFlameChunks->dead ) { + headFlameChunks = NULL; + } + + // select the first free trail, and remove it from the list + f = freeFlameChunks; + freeFlameChunks = f->nextGlobal; + if ( freeFlameChunks ) { + freeFlameChunks->prevGlobal = NULL; + } + + f->nextGlobal = activeFlameChunks; + if ( activeFlameChunks ) { + activeFlameChunks->prevGlobal = f; + } + activeFlameChunks = f; + f->prevGlobal = NULL; + f->inuse = qtrue; + f->dead = qfalse; + + // if this owner has a headJunc, add us to the start + if ( headFlameChunk ) { + // remove the headJunc from the list of heads + if ( headFlameChunk == headFlameChunks ) { + headFlameChunks = headFlameChunks->nextHead; + if ( headFlameChunks ) { + headFlameChunks->prevHead = NULL; + } + } else { + if ( headFlameChunk->nextHead ) { + headFlameChunk->nextHead->prevHead = headFlameChunk->prevHead; + } + if ( headFlameChunk->prevHead ) { + headFlameChunk->prevHead->nextHead = headFlameChunk->nextHead; + } + } + headFlameChunk->prevHead = NULL; + headFlameChunk->nextHead = NULL; + } + // make us the headTrail + if ( headFlameChunks ) { + headFlameChunks->prevHead = f; + } + f->nextHead = headFlameChunks; + f->prevHead = NULL; + headFlameChunks = f; + + f->nextFlameChunk = headFlameChunk; // if headJunc is NULL, then we'll just be the end of the list + + numFlameChunksInuse++; + + // debugging + if ( cg_drawRewards.integer > 1 && numFlameChunksInuse > cg_drawRewards.integer ) { + CG_Printf( "NumFlameChunks: %i\n", numFlameChunksInuse ); + } + + return f; +} + +/* +=========== +CG_FreeFlameChunk +=========== +*/ +void CG_FreeFlameChunk( flameChunk_t *f ) { +// if (!f->inuse) +// return; + + // kill any juncs after us, so they aren't left hanging + if ( f->nextFlameChunk ) { + CG_FreeFlameChunk( f->nextFlameChunk ); + f->nextFlameChunk = NULL; + } + + // make it non-active + f->inuse = qfalse; + f->dead = qfalse; + if ( f->nextGlobal ) { + f->nextGlobal->prevGlobal = f->prevGlobal; + } + if ( f->prevGlobal ) { + f->prevGlobal->nextGlobal = f->nextGlobal; + } + if ( f == activeFlameChunks ) { + activeFlameChunks = f->nextGlobal; + } + + // if it's a head, remove it + if ( f == headFlameChunks ) { + headFlameChunks = f->nextHead; + } + if ( f->nextHead ) { + f->nextHead->prevHead = f->prevHead; + } + if ( f->prevHead ) { + f->prevHead->nextHead = f->nextHead; + } + f->nextHead = NULL; + f->prevHead = NULL; + + // stick it in the free list + f->prevGlobal = NULL; + f->nextGlobal = freeFlameChunks; + if ( freeFlameChunks ) { + freeFlameChunks->prevGlobal = f; + } + freeFlameChunks = f; + + numFlameChunksInuse--; +} + +/* +=============== +CG_MergeFlameChunks + + Assumes f1 comes before f2 +=============== +*/ +void CG_MergeFlameChunks( flameChunk_t *f1, flameChunk_t *f2 ) { + if ( f1->nextFlameChunk != f2 ) { + CG_Error( "CG_MergeFlameChunks: f2 doesn't follow f1, cannot merge\n" ); + } + + f1->nextFlameChunk = f2->nextFlameChunk; + f2->nextFlameChunk = NULL; + + //VectorAdd( f1->velDir, f2->velDir, f1->velDir ); + //VectorScale( f1->velDir, 0.5, f1->velDir ); + VectorCopy( f2->velDir, f1->velDir ); + + //VectorAdd( f1->baseOrg, f2->baseOrg, f1->baseOrg ); + //VectorScale( f1->baseOrg, 0.5, f1->baseOrg ); + VectorCopy( f2->baseOrg, f1->baseOrg ); + f1->baseOrgTime = f2->baseOrgTime; + + f1->velSpeed = f2->velSpeed; //(f1->velSpeed + f2->velSpeed) / 2.0; + f1->sizeMax = f2->sizeMax; //(f1->sizeMax + f2->sizeMax) / 2.0; + f1->size = f2->size; //(f1->size + f2->size) / 2.0; + f1->timeStart = f2->timeStart; + f1->timeEnd = f2->timeEnd; + + CG_FreeFlameChunk( f2 ); +} + +/* +=============== +CG_FlameCalcOrg +=============== +*/ +void CG_FlameCalcOrg( flameChunk_t *f, int time, vec3_t outOrg ) { + VectorMA( f->baseOrg, f->velSpeed * ( (float)( time - f->baseOrgTime ) / 1000 ), f->velDir, outOrg ); + outOrg[2] -= f->gravity * ( (float)( time - f->timeStart ) / 1000.0 ) * ( (float)( time - f->timeStart ) / 1000.0 ); +} + +void CG_AdjustFlameSize( flameChunk_t *f, int t ) { + // adjust size + if ( f->size < f->sizeMax ) { + //f->sizeMax = FLAME_START_MAX_SIZE + ((1.0 - (f->velSpeed / FLAME_START_SPEED)) * (FLAME_MAX_SIZE - FLAME_START_MAX_SIZE)); + if ( ( t - f->timeStart ) < f->blueLife ) { + f->sizeRate = GET_FLAME_BLUE_SIZE_SPEED( FLAME_START_MAX_SIZE ); // use a constant so the blue flame doesn't distort + } else { + f->sizeRate = GET_FLAME_SIZE_SPEED( f->sizeMax ); + } + + f->size += f->sizeRate * (float)( t - f->sizeTime ); + if ( f->size > f->sizeMax ) { + f->size = f->sizeMax; + } + } + f->sizeTime = t; +} + +/* +=============== +CG_MoveFlameChunk +=============== +*/ +void CG_MoveFlameChunk( flameChunk_t *f, int t ) { + vec3_t newOrigin, sOrg; + trace_t trace; + int jiggleCount; + float dot; + //static vec3_t umins = {-1,-1,-1}; // TTimo: unused + //static vec3_t umaxs = { 1, 1, 1}; // TTimo: unused + + // subtract friction from speed + if ( f->velSpeed > 1 && f->lastFrictionTake < t - 50 ) { + CG_FlameAdjustSpeed( f, /*(0.3+0.7*f->speedScale) **/ -( (float)( t - f->lastFrictionTake ) / 1000.0 ) * FLAME_FRICTION_PER_SEC ); + f->lastFrictionTake = t; + } +/* + // adjust size + if (f->size < f->sizeMax) { + //f->sizeMax = FLAME_START_MAX_SIZE + ((1.0 - (f->velSpeed / FLAME_START_SPEED)) * (FLAME_MAX_SIZE - FLAME_START_MAX_SIZE)); + if ((t - f->timeStart) < f->blueLife) + f->sizeRate = GET_FLAME_BLUE_SIZE_SPEED(FLAME_START_MAX_SIZE); // use a constant so the blue flame doesn't distort + else + f->sizeRate = GET_FLAME_SIZE_SPEED(f->sizeMax); + + f->size += f->sizeRate * (float)(t - f->baseOrgTime); + if (f->size > f->sizeMax) { + f->size = f->sizeMax; + } + } +*/ + + jiggleCount = 0; + VectorCopy( f->baseOrg, sOrg ); + while ( f->velSpeed > 1 && ( ( t - f->baseOrgTime ) > 20 ) ) { + CG_FlameCalcOrg( f, t, newOrigin ); + + //if (cg_entities[f->ownerCent].currentState.aiChar != AICHAR_ZOMBIE) { + // trace a line from previous position to new position + CG_Trace( &trace, sOrg, flameChunkMins, flameChunkMaxs, newOrigin, f->ownerCent, MASK_SHOT ); + //} else { + // trace.startsolid = 0; + // trace.surfaceFlags = 0; + // trace.fraction = 1.0; + // VectorCopy( newOrigin, trace.endpos ); + //} + + if ( trace.startsolid ) { + // if it's young, let it go through solids briefly + if ( f->lifeFrac < 0.4 ) { + trace.fraction = 1.0; + } else { + f->velSpeed = 0; + break; + } + } + + if ( trace.surfaceFlags & SURF_NOIMPACT ) { + break; + } + + // moved some distance + VectorCopy( trace.endpos, f->baseOrg ); + f->baseOrgTime += (int)( (float)( t - f->baseOrgTime ) * trace.fraction ); + + if ( trace.fraction == 1.0 ) { + // check for hitting client + if ( ( f->ownerCent != cg.snap->ps.clientNum ) && !( cg.snap->ps.eFlags & EF_DEAD ) && VectorDistance( newOrigin, cg.snap->ps.origin ) < 32 ) { + VectorNegate( f->velDir, trace.plane.normal ); + } else { + break; + } + } + + // reflect off surface + dot = DotProduct( f->velDir, trace.plane.normal ); + VectorMA( f->velDir, -1.3 * dot, trace.plane.normal, f->velDir ); + VectorNormalize( f->velDir ); + // subtract some speed + f->velSpeed *= 0.5 * ( 0.25 + 0.75 * ( ( dot + 1.0 ) * 0.5 ) ); + if ( f->velSpeed > 20 ) { + f->velSpeed = 20; + } + // adjust size + //f->sizeMax = FLAME_START_MAX_SIZE + ((/*1.0 -*/ (f->velSpeed / FLAME_START_SPEED)) * (FLAME_MAX_SIZE - FLAME_START_MAX_SIZE)); + //f->sizeRate = GET_FLAME_SIZE_SPEED(f->sizeMax); + VectorCopy( f->velDir, f->parentFwd ); + + VectorCopy( f->baseOrg, sOrg ); + } + + CG_FlameCalcOrg( f, t, f->org ); + //f->baseOrgTime = t; // incase we skipped the movement +} + +/* +=============== +CG_AddFlameSpriteToScene +=============== +*/ +static vec3_t vright, vup; +static vec3_t rright, rup; + +#ifdef _DEBUG // just in case we forget about it, but it should be disabled at all times (only enabled to generate updated shaders) +#ifdef ALLOW_GEN_SHADERS // secondary security measure + +//#define GEN_FLAME_SHADER + +#endif // ALLOW_GEN_SHADERS +#endif // _DEBUG + +#define FLAME_BLEND_SRC "GL_ONE" +#define FLAME_BLEND_DST "GL_ONE_MINUS_SRC_COLOR" + +#define NUM_FLAME_SPRITES 45 +#define FLAME_SPRITE_DIR "twiltb2" + +#define NUM_NOZZLE_SPRITES 8 + +static qhandle_t flameShaders[NUM_FLAME_SPRITES]; +static qhandle_t nozzleShaders[NUM_NOZZLE_SPRITES]; +static qboolean initFlameShaders = qtrue; + +#define MAX_CLIPPED_FLAMES 2 // dont draw more than this many per frame +static int numClippedFlames; + +void CG_FlameDamage( int owner, vec3_t org, float radius ) { + centity_t *cent; + int i; + trace_t tr; + static vec3_t lastPos; + static int lastTime; + + if ( cg.time == lastTime ) { + if ( VectorDistance( org, lastPos ) < radius ) { + return; + } + } + lastTime = cg.time; + VectorCopy( org, lastPos ); + + // check for damage this flame is inflicting on something + if ( radius < 24 ) { + radius = 24; + } + + // check the local playing client + if ( ( centFlameInfo[cg.snap->ps.clientNum].lastDmgCheck < cg.time - 100 ) && + ( Distance( org, cg.snap->ps.origin ) < radius * ( cg.snap->ps.clientNum == owner ? 0.1 : 1.0 ) ) ) { + // trace to make sure it's not going through geometry + CG_Trace( &tr, org, NULL, NULL, cg.snap->ps.origin, -1, MASK_SHOT ); + // + if ( tr.fraction == 1.0 || tr.entityNum == cg.snap->ps.clientNum ) { + centFlameInfo[cg.snap->ps.clientNum].lastDmgCheck = cg.time; + centFlameInfo[cg.snap->ps.clientNum].lastDmgEnemy = owner; + } + } + + if ( cgs.localServer && cgs.gametype == GT_SINGLE_PLAYER ) { + + // flaming zombie's only hurt the player + //if (cg_entities[owner].currentState.aiChar == AICHAR_ZOMBIE) + // return; + + // check for AI's getting hurt (TODO: bot support?) + for ( cent = cg_entities, i = 0; i < cgs.maxclients; cent++, i++ ) { + if ( cent->currentState.aiChar && + cent->currentState.aiChar != AICHAR_VENOM && // Venom has no flame damage + centFlameInfo[i].lastDmgCheck < ( cg.time - 100 ) && + cent->currentValid && // is in the visible frame + ( Distance( org, cent->lerpOrigin ) < radius ) ) { + // trace to make sure it's not going through geometry + CG_Trace( &tr, org, NULL, NULL, cent->lerpOrigin, -1, MASK_SHOT ); + // + if ( tr.fraction == 1.0 || tr.entityNum == cent->currentState.number ) { + centFlameInfo[i].lastDmgCheck = cg.time; + centFlameInfo[i].lastDmgEnemy = owner; + } + } + } + } +} + +void CG_AddFlameSpriteToScene( flameChunk_t *f, float lifeFrac, float alpha ) { + vec3_t point, p2, projVec, sProj; + polyVert_t verts[4]; + float radius, sdist, x; + int frameNum; + vec3_t vec, rotate_ang; + unsigned char alphaChar; + vec2_t fovRadius; + vec2_t rdist, rST; + int rollAngleClamped; + static vec3_t lastPos; + + CG_FlameDamage( f->ownerCent, f->org, f->size ); + + if ( alpha < 0 ) { + return; // we dont want to see this + + } + radius = ( f->size / 2.0 ); + if ( radius < 6 ) { + radius = 6; + } + rST[0] = radius * 1.0; + rST[1] = radius * 1.0 / 1.481; + alphaChar = ( unsigned char )( 255.0 * alpha ); + + verts[0].modulate[0] = alphaChar; + verts[0].modulate[1] = alphaChar; + verts[0].modulate[2] = alphaChar; + verts[0].modulate[3] = alphaChar; + verts[1] = verts[0]; + verts[2] = verts[0]; + verts[3] = verts[0]; + + // find the projected distance from the eye to the projection of the flame origin + // onto the view direction vector + VectorMA( cg.refdef.vieworg, 1024, cg.refdef.viewaxis[0], p2 ); + ProjectPointOntoVector( f->org, cg.refdef.vieworg, p2, sProj ); + + // make sure its infront of us + VectorSubtract( sProj, cg.refdef.vieworg, vec ); + sdist = VectorNormalize( vec ); + if ( !sdist || DotProduct( vec, cg.refdef.viewaxis[0] ) < 0 ) { + return; + } + + // if we are "inside" this sprite, clip it to our view frustum + if ( sdist < f->size * 0.6 ) { + // clip the sprite to the viewport, avoiding rendering off-screen pixels which + // is the main cause of slow-downs + + // if it's close to the previous sprite, ignore + //if (VectorDistance(lastPos, f->org) < 6) + // return; + + if ( ( sdist < f->size * 0.6 ) && ( numClippedFlames++ > MAX_CLIPPED_FLAMES ) ) { + return; + } + + // OPTIMIZATION: clamp to 90 degree rotations + rollAngleClamped = 0; + if ( rotatingFlames ) { + vectoangles( cg.refdef.viewaxis[0], rotate_ang ); + rotate_ang[ROLL] += ( rollAngleClamped = ( (int)( f->rollAngle ) / 90 ) * 90 ); + AngleVectors( rotate_ang, NULL, rright, rup ); + } else { + VectorCopy( vright, rright ); + VectorCopy( vup, rup ); + } + + VectorSubtract( sProj, f->org, projVec ); + + // find the distances from the sprite origin to the projection along the refdef axis + rdist[0] = -1.0 * DotProduct( rright, projVec ); + rdist[1] = -1.0 * DotProduct( rup, projVec ); + + if ( fabs( rdist[0] ) > radius || fabs( rdist[1] ) > radius ) { + return; // completely off-screen + + } + // now set the bounds for clipping + fovRadius[0] = tan( DEG2RAD( ( rollAngleClamped % 2 == 0 ? cg.refdef.fov_x : cg.refdef.fov_x ) * 0.52 ) ) * sdist; + fovRadius[1] = tan( DEG2RAD( ( rollAngleClamped % 2 == 0 ? cg.refdef.fov_x : cg.refdef.fov_x ) * 0.52 ) ) * sdist; + + // BOTTOM LEFT + x = ( -radius + rdist[0] ); + if ( x < -fovRadius[0] ) { // clip + VectorMA( f->org, -radius + ( -fovRadius[0] - x ), rright, point ); + verts[0].st[0] = 0.0 + ( -fovRadius[0] - x ) / ( radius * 2 ); + } else { + VectorMA( f->org, -radius, rright, point ); + verts[0].st[0] = 0.0; + } + x = ( -radius + rdist[1] ); + if ( x < -fovRadius[1] ) { + VectorMA( point, -radius + ( -fovRadius[1] - x ), rup, point ); + verts[0].st[1] = 0.0 + ( -fovRadius[1] - x ) / ( radius * 2 ); + } else { + VectorMA( point, -radius, rup, point ); + verts[0].st[1] = 0.0; + } + VectorCopy( point, verts[0].xyz ); + + // TOP LEFT + x = ( -radius + rdist[0] ); + if ( x < -fovRadius[0] ) { // clip + VectorMA( f->org, -radius + ( -fovRadius[0] - x ), rright, point ); + verts[1].st[0] = 0.0 + ( -fovRadius[0] - x ) / ( radius * 2 ); + } else { + VectorMA( f->org, -radius, rright, point ); + verts[1].st[0] = 0.0; + } + x = ( radius + rdist[1] ); + if ( x > fovRadius[1] ) { + VectorMA( point, radius + ( fovRadius[1] - x ), rup, point ); + verts[1].st[1] = 1.0 + ( fovRadius[1] - x ) / ( radius * 2 ); + } else { + VectorMA( point, radius, rup, point ); + verts[1].st[1] = 1.0; + } + VectorCopy( point, verts[1].xyz ); + + // TOP RIGHT + x = ( radius + rdist[0] ); + if ( x > fovRadius[0] ) { + VectorMA( f->org, radius + ( fovRadius[0] - x ), rright, point ); + verts[2].st[0] = 1.0 + ( fovRadius[0] - x ) / ( radius * 2 ); + } else { + VectorMA( f->org, radius, rright, point ); + verts[2].st[0] = 1.0; + } + x = ( radius + rdist[1] ); + if ( x > fovRadius[1] ) { + VectorMA( point, radius + ( fovRadius[1] - x ), rup, point ); + verts[2].st[1] = 1.0 + ( fovRadius[1] - x ) / ( radius * 2 ); + } else { + VectorMA( point, radius, rup, point ); + verts[2].st[1] = 1.0; + } + VectorCopy( point, verts[2].xyz ); + + // BOTTOM RIGHT + x = ( radius + rdist[0] ); + if ( x > fovRadius[0] ) { + VectorMA( f->org, radius + ( fovRadius[0] - x ), rright, point ); + verts[3].st[0] = 1.0 + ( fovRadius[0] - x ) / ( radius * 2 ); + } else { + VectorMA( f->org, radius, rright, point ); + verts[3].st[0] = 1.0; + } + x = ( -radius + rdist[1] ); + if ( x < -fovRadius[1] ) { + VectorMA( point, -radius + ( -fovRadius[1] - x ), rup, point ); + verts[3].st[1] = 0.0 + ( -fovRadius[1] - x ) / ( radius * 2 ); + } else { + VectorMA( point, -radius, rup, point ); + verts[3].st[1] = 0.0; + } + VectorCopy( point, verts[3].xyz ); + + } else { // set default values for no clipping + + if ( rotatingFlames ) { + vectoangles( cg.refdef.viewaxis[0], rotate_ang ); + rotate_ang[ROLL] += f->rollAngle; + AngleVectors( rotate_ang, NULL, rright, rup ); + } else { + VectorCopy( vright, rright ); + VectorCopy( vup, rup ); + } + + VectorMA( f->org, -rST[1], rup, point ); + VectorMA( point, -rST[0], rright, point ); + VectorCopy( point, verts[0].xyz ); + verts[0].st[0] = 0; + verts[0].st[1] = 0; + + VectorMA( point, rST[1] * 2, rup, point ); + VectorCopy( point, verts[1].xyz ); + verts[1].st[0] = 0; + verts[1].st[1] = 1; + + VectorMA( point, rST[0] * 2, rright, point ); + VectorCopy( point, verts[2].xyz ); + verts[2].st[0] = 1; + verts[2].st[1] = 1; + + VectorMA( point, -rST[1] * 2, rup, point ); + VectorCopy( point, verts[3].xyz ); + verts[3].st[0] = 1; + verts[3].st[1] = 0; + } + + frameNum = (int)floor( lifeFrac * NUM_FLAME_SPRITES ); + if ( frameNum < 0 ) { + frameNum = 0; + } else if ( frameNum > NUM_FLAME_SPRITES - 1 ) { + frameNum = NUM_FLAME_SPRITES - 1; + } + + trap_R_AddPolyToScene( flameShaders[frameNum], 4, verts ); + VectorCopy( f->org, lastPos ); +} + +/* +=============== +CG_AddFlameToScene +=============== +*/ +void CG_AddFlameToScene( flameChunk_t *fHead ) { + flameChunk_t *f, *fNext; + int blueTrailHead = 0, fuelTrailHead = 0; + static vec3_t whiteColor = {1,1,1}; + vec3_t c; + float alpha; + float lived; + int headTimeStart; + //qboolean firstFuel = qtrue; // TTimo + float vdist, bdot; +#define FLAME_SOUND_RANGE 1024.0 + + //flameChunk_t *lastSoundFlameChunk=NULL; // TTimo: unused + flameChunk_t *lastBlowChunk = NULL; + qboolean isClientFlame, firing; + int shader; + + flameChunk_t *lastBlueChunk = NULL; + qboolean skip = qfalse, droppedTrail; + vec3_t v, lastDrawPos; + +#ifdef FLAMETHROW_LIGHTS + vec3_t lastLightPos; + flameChunk_t *lastLightFlameChunk; + float lightSize, lastLightSize, lightAlpha; +#endif + + vec3_t lightOrg; // origin to place light at + float lightSize; + float lightFlameCount; + + float lastFuelAlpha; + + + //if (fHead->ownerCent == cg.snap->ps.clientNum) + isClientFlame = ( fHead == centFlameInfo[fHead->ownerCent].lastFlameChunk ); + //else + // isClientFlame = qfalse; // always optimize enemy flames + + if ( ( cg_entities[fHead->ownerCent].currentState.eFlags & EF_FIRING ) /*(centFlameInfo[fHead->ownerCent].lastClientFrame == cg_entities[fHead->ownerCent].lastWeaponClientFrame)*/ && ( centFlameInfo[fHead->ownerCent].lastFlameChunk == fHead ) ) { + headTimeStart = fHead->timeStart; + firing = qtrue; + } else { + //isClientFlame = qfalse; + headTimeStart = cg.time; + firing = qfalse; + } + + // Zombie ignition is just to make on/off transitions look better + //if (cg_entities[fHead->ownerCent].currentState.aiChar == AICHAR_ZOMBIE && !centFlameInfo[fHead->ownerCent].lastFiring) { + // return; + //} + + VectorClear( lightOrg ); + lightSize = 0; + lightFlameCount = 0; + + lastFuelAlpha = 1.0; + VectorClear( lastDrawPos ); + + f = fHead; + while ( f ) { + + if ( f->nextFlameChunk && f->nextFlameChunk->dead ) { + // kill it + CG_FreeFlameChunk( f->nextFlameChunk ); + f->nextFlameChunk = NULL; + } + + // draw this chunk + + fNext = f->nextFlameChunk; + lived = (float)( headTimeStart - f->timeStart ); + + // update the "blow" sound volume (louder as we sway it) + vdist = Distance( cg.refdef.vieworg, f->org ); // NOTE: this needs to be here or the flameSound code further below won't work + if ( lastBlowChunk && ( centFlameStatus[f->ownerCent].blowVolume < 1.0 ) && + ( ( bdot = DotProduct( lastBlowChunk->startVelDir, f->startVelDir ) ) < 1.0 ) ) { + if ( vdist < FLAME_SOUND_RANGE ) { + centFlameStatus[f->ownerCent].blowVolume += 500.0 * ( 1.0 - bdot ) * ( 1.0 - ( vdist / FLAME_SOUND_RANGE ) ); + if ( centFlameStatus[f->ownerCent].blowVolume > 1.0 ) { + centFlameStatus[f->ownerCent].blowVolume = 1.0; + } + } + } + lastBlowChunk = f; + + VectorMA( lightOrg, f->size / 20.0, f->org, lightOrg ); + lightSize += f->size; + lightFlameCount += f->size / 20.0; + + droppedTrail = qfalse; + + // is it a stream chunk? (no special handling) + if ( !f->ignitionOnly && f->velSpeed < 1 ) { + + CG_AddFlameSpriteToScene( f, f->lifeFrac, 1.0 ); +/* + } else if (cg_entities[f->ownerCent].currentState.aiChar == AICHAR_ZOMBIE) { + + if (isClientFlame && !fHead->ignitionOnly) { + + if (f->lifeFrac < 0.3) { + skip = qfalse; + + // if this is backwards from the last chunk, then skip it + if (!f->ignitionOnly && fNext && f != fHead && lastBlueChunk) { + VectorSubtract( f->org, lastBlueChunk->org, v ); + if (VectorNormalize( v ) < f->size/4 ) + skip = qtrue; + else if (DotProduct( v, f->velDir ) < 0.7) + skip = qtrue; + } + + if (!skip) { + lastBlueChunk = f; + alpha = 1.0; // new nozzle sprite + VectorScale( whiteColor, alpha, c ); + shader = cgs.media.funnelFireShader[(cg.time/10)%NUM_FUNNEL_SPRITES]; + blueTrailHead = CG_AddTrailJunc( blueTrailHead, + shader, + cg.time, + STYPE_STRETCH, + f->org, + 1, + alpha, alpha, + f->size*4.0*f->speedScale, + FLAME_MAX_SIZE, + TJFL_NOCULL|TJFL_FIXDISTORT, + c, c, 1.0, 5.0 ); + } + } + + } else { + lightSize = 0; + } +*/ + // is it in the blue ignition section of the flame? + } else if ( isClientFlame && f->blueLife > ( lived / 2.0 ) ) { + + skip = qfalse; + + // if this is backwards from the last chunk, then skip it + if ( /*!f->ignitionOnly &&*/ fNext && f != fHead && lastBlueChunk ) { + VectorSubtract( f->org, lastBlueChunk->org, v ); + if ( VectorNormalize( v ) < f->size / 2 ) { + skip = qtrue; + } else if ( DotProduct( v, f->velDir ) < 0 ) { + skip = qtrue; + } + } + + // stream sound + if ( !f->ignitionOnly && ( cg_entities[f->ownerCent].currentState.number >= MAX_CLIENTS || cg_entities[f->ownerCent].currentState.weapon == WP_FLAMETHROWER ) ) { + centFlameStatus[f->ownerCent].streamVolume += 0.05; + if ( centFlameStatus[f->ownerCent].streamVolume > 1.0 ) { + centFlameStatus[f->ownerCent].streamVolume = 1.0; + } + } + + + if ( !skip ) { + + // just call this for damage checking + if ( !f->ignitionOnly ) { + CG_AddFlameSpriteToScene( f, f->lifeFrac, -1 ); + } + + lastBlueChunk = f; + + alpha = 1.0; // new nozzle sprite + VectorScale( whiteColor, alpha, c ); + + if ( f->blueLife > lived * ( f->ignitionOnly ? 3.0 : 3.0 ) ) { + + //if (cg_entities[f->ownerCent].currentState.aiChar == AICHAR_ZOMBIE) + // shader = cgs.media.funnelFireShader[(cg.time/50 + (cg.time/50 >> 1))%NUM_FUNNEL_SPRITES]; + //else + shader = nozzleShaders[( cg.time / 50 + ( cg.time / 50 >> 1 ) ) % NUM_NOZZLE_SPRITES]; + + blueTrailHead = CG_AddTrailJunc( blueTrailHead, + shader, + cg.time, + STYPE_STRETCH, + f->org, + 1, + alpha, alpha, + f->size * ( f->ignitionOnly /*&& (cg.snap->ps.clientNum != f->ownerCent || cg_thirdPerson.integer)*/ ? 2.0 : 1.0 ), + FLAME_MAX_SIZE, + TJFL_NOCULL | TJFL_FIXDISTORT, + c, c, 1.0, 5.0 ); + } + + // fire stream +//if (qfalse) + if ( !f->ignitionOnly ) { // && isClientFlame) { + float bscale; + qboolean fskip = qfalse; + + bscale = 1.0; + + if ( !f->nextFlameChunk ) { + alpha = 0; + } else if ( lived / 1.3 < bscale * FLAME_BLUE_FADEIN_TIME( f->blueLife ) ) { + alpha = FLAME_BLUE_MAX_ALPHA * ( ( lived / 1.3 ) / ( bscale * FLAME_BLUE_FADEIN_TIME( f->blueLife ) ) ); + } else if ( lived / 1.3 < ( f->blueLife - FLAME_BLUE_FADEOUT_TIME( f->blueLife ) ) ) { + alpha = FLAME_BLUE_MAX_ALPHA; + } else { + alpha = FLAME_BLUE_MAX_ALPHA * ( 1.0 - ( ( lived / 1.3 - ( f->blueLife - FLAME_BLUE_FADEOUT_TIME( f->blueLife ) ) ) / ( FLAME_BLUE_FADEOUT_TIME( f->blueLife ) ) ) ); + } + if ( alpha <= 0.0 ) { + alpha = 0.0; + if ( lastFuelAlpha <= 0.0 ) { + fskip = qtrue; + } + } + + if ( !fskip ) { + lastFuelAlpha = alpha; + + VectorScale( whiteColor, alpha, c ); + + droppedTrail = qtrue; + + fuelTrailHead = CG_AddTrailJunc( fuelTrailHead, + cgs.media.flamethrowerFireStream, + cg.time, + ( f->ignitionOnly ? STYPE_STRETCH : STYPE_REPEAT ), + f->org, + 1, + alpha, alpha, + ( f->size / 2 < f->sizeMax / 4 ? f->size / 2 : f->sizeMax / 4 ), + FLAME_MAX_SIZE, + TJFL_NOCULL | TJFL_FIXDISTORT | TJFL_CROSSOVER, + c, c, 0.5, 1.5 ); + } + } + + } + + } + +#define FLAME_SPRITE_START_BLUE_SCALE 0.2 + + if ( !f->ignitionOnly && + //!(cg_entities[f->ownerCent].currentState.aiChar == AICHAR_ZOMBIE) && + ( (float)( FLAME_SPRITE_START_BLUE_SCALE * f->blueLife ) < (float)lived ) ) { + + float alpha, lifeFrac; + qboolean skip = qfalse; + vec3_t v1, v2; + float fDist, vDist; + + // should we merge it with the next sprite? + while ( fNext && !droppedTrail ) { + if ( ( Distance( f->org, fNext->org ) < ( ( 0.2 + 0.8 * f->lifeFrac ) * f->size * ( isClientFlame ? 0.2 : 0.1 ) ) ) + && ( fabs( f->size - fNext->size ) < ( 40.0 ) ) + && ( fabs( f->timeStart - fNext->timeStart ) < 50 ) + && ( DotProduct( f->velDir, fNext->velDir ) > 0.999 ) + ) { + if ( !droppedTrail ) { + CG_MergeFlameChunks( f, fNext ); + fNext = f->nextFlameChunk; // it may have changed + } else { + skip = qtrue; + break; + } + } else { + break; + } + } + + lifeFrac = ( lived - FLAME_SPRITE_START_BLUE_SCALE * f->blueLife ) / ( FLAME_LIFETIME - FLAME_SPRITE_START_BLUE_SCALE * f->blueLife ); + + alpha = ( 1.0 - lifeFrac ) * 1.4; + if ( alpha > 1.0 ) { + alpha = 1.0; + } + + // should we draw this sprite? + if ( !skip && VectorLengthSquared( lastDrawPos ) ) { + VectorSubtract( cg.refdef.vieworg, lastDrawPos, v1 ); + VectorSubtract( f->org, lastDrawPos, v2 ); + fDist = VectorNormalize( v2 ); + vDist = VectorNormalize( v1 ); + if ( ( vDist / f->size ) * fDist * ( 0.1 + 0.9 * ( 1.0 - fabs( DotProduct( v1, v2 ) ) ) ) < ( 2.0 * ( f->size / 30.0 < 1.0 ? f->size / 30.0 : 1.0 ) ) ) { + skip = qtrue; + } + } + + if ( !skip ) { + // draw the sprite + CG_AddFlameSpriteToScene( f, lifeFrac, alpha ); + VectorCopy( f->org, lastDrawPos ); + } + // update the sizeRate + f->sizeRate = GET_FLAME_SIZE_SPEED( f->sizeMax ); + +#ifdef FLAMETHROW_LIGHTS + // drop a light? + if ( /*f->size > FLAME_START_MAX_SIZE/1.5 &&*/ ( !lastLightFlameChunk || Distance( lastLightPos, f->org ) > lastLightSize * 0.4 ) ) { + lightSize = f->size * 10; + if ( lightSize > 250 ) { + lightSize = 250; + } + lightAlpha = 1.0; //lightSize / 600; + if ( lightSize < 200 ) { + lightSize = 200; + } + trap_R_AddLightToScene( f->org, lightSize, 1.0 * lightAlpha, 0.7 * lightAlpha, 0.3 * lightAlpha, 0 ); + VectorCopy( f->org, lastLightPos ); + lastLightFlameChunk = f; + lastLightSize = lightSize; + } +#endif + + } + + f = fNext; + } + + if ( lightSize < 80 ) { + lightSize = 80; + } + + if ( lightSize > 500 ) { + lightSize = 500; + } + lightSize *= 1.0 + 0.2 * ( sin( 1.0 * cg.time / 50.0 ) * cos( 1.0 * cg.time / 43.0 ) ); + // set the alpha + alpha = lightSize / 500.0; + if ( alpha > 1.0 ) { + alpha = 1.0; + } + VectorScale( lightOrg, 1.0 / lightFlameCount, lightOrg ); + // if it's only a nozzle, make it blue + if ( fHead->ignitionOnly ) { + if ( lightSize > 80 ) { + lightSize = 80; + } + trap_R_AddLightToScene( lightOrg, 90 + lightSize, 0, 0, alpha * 0.5, 0 ); + } else if ( isClientFlame || ( fHead->ownerCent == cg.snap->ps.clientNum ) ) { + //trap_R_AddLightToScene( lightOrg, 90 + lightSize, 1.000000*alpha, 0.603922*alpha, 0.207843*alpha, 2/*isClientFlame * (fHead->ownerCent == cg.snap->ps.clientNum)*/ ); + } +} + +/* +============= +CG_GenerateShaders + + A util to create a bunch of shaders in a unique shader file, which represent an animation +============= +*/ +void CG_GenerateShaders( char *filename, char *shaderName, char *dir, int numFrames, char *srcBlend, char *dstBlend, char *extras, qboolean compressedVersionAvailable, qboolean nomipmap ) { + fileHandle_t f; + int b, c, d, lastNumber; + char str[512]; + int i; + + trap_FS_FOpenFile( filename, &f, FS_WRITE ); + for ( i = 0; i < numFrames; i++ ) { + lastNumber = i; + b = lastNumber / 100; + lastNumber -= b * 100; + c = lastNumber / 10; + lastNumber -= c * 10; + d = lastNumber; + + if ( compressedVersionAvailable ) { + Com_sprintf( str, sizeof( str ), "%s%i\n{\n\tnofog%s\n\tallowCompress\n\tcull none\n\t{\n\t\tmapcomp sprites/%s_lg/spr%i%i%i.tga\n\t\tmapnocomp sprites/%s/spr%i%i%i.tga\n\t\tblendFunc %s %s\n%s\t}\n}\n", shaderName, i + 1, nomipmap ? "\n\tnomipmaps" : "", dir, b, c, d, dir, b, c, d, srcBlend, dstBlend, extras ); + } else { + Com_sprintf( str, sizeof( str ), "%s%i\n{\n\tnofog%s\n\tallowCompress\n\tcull none\n\t{\n\t\tmap sprites/%s/spr%i%i%i.tga\n\t\tblendFunc %s %s\n%s\t}\n}\n", shaderName, i + 1, nomipmap ? "\n\tnomipmap" : "", dir, b, c, d, srcBlend, dstBlend, extras ); + } + trap_FS_Write( str, strlen( str ), f ); + } + trap_FS_FCloseFile( f ); +} + +/* +=============== +CG_InitFlameChunks +=============== +*/ +void CG_InitFlameChunks( void ) { + int i; + char filename[MAX_QPATH]; + + CG_ClearFlameChunks(); + +#ifdef GEN_FLAME_SHADER + CG_GenerateShaders( "scripts/flamethrower.shader", + "flamethrowerFire", + FLAME_SPRITE_DIR, + NUM_FLAME_SPRITES, + FLAME_BLEND_SRC, + FLAME_BLEND_DST, + "", + qtrue, qtrue ); + + CG_GenerateShaders( "scripts/blacksmokeanim.shader", + "blacksmokeanim", + "explode1", + 23, + "GL_ZERO", + "GL_ONE_MINUS_SRC_ALPHA", + "\t\talphaGen const 0.2\n", + qfalse, qfalse ); + + CG_GenerateShaders( "scripts/viewflames.shader", + "viewFlashFire", + "clnfire", + 16, + "GL_ONE", + "GL_ONE", + "\t\talphaGen vertex\n\t\trgbGen vertex\n", + qtrue, qtrue ); + + CG_GenerateShaders( "scripts/twiltb.shader", + "twiltb", + "twiltb", + 42, + "GL_SRC_ALPHA", + "GL_ONE_MINUS_SRC_COLOR", + "", + qtrue, qfalse ); + + CG_GenerateShaders( "scripts/twiltb2.shader", + "twiltb2", + "twiltb2", + 45, + "GL_ONE", + "GL_ONE_MINUS_SRC_COLOR", + "", + qtrue, qfalse ); + + CG_GenerateShaders( "scripts/expblue.shader", + "expblue", + "expblue", + 25, + "GL_ONE", + "GL_ONE_MINUS_SRC_COLOR", + "", + qfalse, qfalse ); + + CG_GenerateShaders( "scripts/firest.shader", + "firest", + "firest", + 36, + "GL_ONE", + "GL_ONE_MINUS_SRC_COLOR", + "", + qtrue, qfalse ); + + CG_GenerateShaders( "scripts/explode1.shader", + "explode1", + "explode1", + 23, + "GL_ONE", + "GL_ONE_MINUS_SRC_COLOR", + "", + qtrue, qfalse ); + + CG_GenerateShaders( "scripts/funnel.shader", + "funnel", + "funnel", + 21, + "GL_ONE", + "GL_ONE_MINUS_SRC_COLOR", + "", + qfalse, qfalse ); +#endif + + for ( i = 0; i < NUM_FLAME_SPRITES; i++ ) { + Com_sprintf( filename, MAX_QPATH, "flamethrowerFire%i", i + 1 ); + flameShaders[i] = trap_R_RegisterShader( filename ); + } + for ( i = 0; i < NUM_NOZZLE_SPRITES; i++ ) { + Com_sprintf( filename, MAX_QPATH, "nozzleFlame%i", i + 1 ); + nozzleShaders[i] = trap_R_RegisterShader( filename ); + } + initFlameShaders = qfalse; +} + +/* +=============== +CG_AddFlameChunks +=============== +*/ +void CG_AddFlameChunks( void ) { + flameChunk_t *f, *fNext; + //int moveStep = 100; // TTimo: unused + + //AngleVectors( cg.refdef.viewangles, NULL, vright, vup ); + VectorCopy( cg.refdef.viewaxis[1], vright ); + VectorCopy( cg.refdef.viewaxis[2], vup ); + + // clear out the volumes so we can rebuild them + memset( centFlameStatus, 0, sizeof( centFlameStatus ) ); + + numClippedFlames = 0; + + // age them + f = activeFlameChunks; + while ( f ) { + if ( !f->dead ) { + if ( cg.time > f->timeEnd ) { + f->dead = qtrue; + } else if ( f->ignitionOnly && ( f->blueLife < ( cg.time - f->timeStart ) ) ) { + f->dead = qtrue; + } else { + // adjust by a fraction if crossing the blueLife threshold + if ( ( f->sizeTime - f->timeStart < f->blueLife ) != ( cg.time - f->timeStart < f->blueLife ) ) { + CG_AdjustFlameSize( f, f->timeStart + f->blueLife ); + } + // + CG_AdjustFlameSize( f, cg.time ); + //if (cg.time - f->baseOrgTime >= moveStep) + CG_MoveFlameChunk( f, cg.time ); + // + f->lifeFrac = (float)( f->baseOrgTime - f->timeStart ) / (float)( f->timeEnd - f->timeStart ); + } + } + f = f->nextGlobal; + } + + // draw each of the headFlameChunk's + f = headFlameChunks; + while ( f ) { + fNext = f->nextHead; // in case it gets removed + if ( f->dead ) { + if ( centFlameInfo[f->ownerCent].lastFlameChunk == f ) { + centFlameInfo[f->ownerCent].lastFlameChunk = NULL; + centFlameInfo[f->ownerCent].lastClientFrame = 0; + } + CG_FreeFlameChunk( f ); + } else if ( !f->ignitionOnly || ( centFlameInfo[f->ownerCent].lastFlameChunk == f ) ) { // don't draw the ignition flame after we start firing + CG_AddFlameToScene( f ); + } + f = fNext; + } +} + +/* +=============== +CG_UpdateFlamethrowerSounds +=============== +*/ +void CG_UpdateFlamethrowerSounds( void ) { + flameChunk_t *f, *trav; + //flameChunk_t *lastSoundFlameChunk=NULL; // TTimo: unused + int i; + centity_t *cent; + #define MIN_BLOW_VOLUME 30 + + // draw each of the headFlameChunk's + f = headFlameChunks; + while ( f ) { + + if ( centFlameInfo[f->ownerCent].silent ) { + f = f->nextHead; + continue; + } + + // update this entity? + if ( centFlameInfo[f->ownerCent].lastSoundUpdate != cg.time ) { + // blow/ignition sound + if ( centFlameStatus[f->ownerCent].blowVolume * 255.0 > MIN_BLOW_VOLUME ) { + trap_S_AddLoopingSound( f->ownerCent, f->org, vec3_origin, cgs.media.flameBlowSound, (int)( 255.0 * centFlameStatus[f->ownerCent].blowVolume ) ); + } else { + trap_S_AddLoopingSound( f->ownerCent, f->org, vec3_origin, cgs.media.flameBlowSound, MIN_BLOW_VOLUME ); + } + + if ( centFlameStatus[f->ownerCent].streamVolume ) { + if ( cg_entities[f->ownerCent].currentState.aiChar != AICHAR_ZOMBIE ) { + trap_S_AddLoopingSound( f->ownerCent, f->org, vec3_origin, cgs.media.flameStreamSound, (int)( 255.0 /**centFlameStatus[f->ownerCent].streamVolume*/ ) ); + } else { + trap_S_AddLoopingSound( f->ownerCent, f->org, vec3_origin, cgs.media.flameCrackSound, (int)( 255.0 * centFlameStatus[f->ownerCent].streamVolume ) ); + } + } + + centFlameInfo[f->ownerCent].lastSoundUpdate = cg.time; + } + + // traverse the chunks, spawning flame sound sources as we go + for ( trav = f; trav; trav = trav->nextFlameChunk ) { + // update the sound volume + if ( trav->blueLife + 100 < ( cg.time - trav->timeStart ) ) { + //if (!lastSoundFlameChunk || Distance( lastSoundFlameChunk->org, trav->org ) > lastSoundFlameChunk->size) { + trap_S_AddLoopingSound( trav->ownerCent, trav->org, vec3_origin, cgs.media.flameSound, (int)( 255.0 * ( 0.2 * ( trav->size / FLAME_MAX_SIZE ) ) ) ); + // lastSoundFlameChunk = trav; + //} + } + } + + f = f->nextHead; + } + + // send client damage updates if required + for ( cent = cg_entities, i = 0; i < cgs.maxclients; cent++, i++ ) { + if ( centFlameInfo[i].lastDmgCheck > centFlameInfo[i].lastDmgUpdate && + centFlameInfo[i].lastDmgUpdate < cg.time - 50 ) { // JPW NERVE (cgs.gametype == GT_SINGLE_PLAYER ? 50 : 50)) -- sean changed clientdamage so this isn't a saturation issue any longer + if ( ( cg.snap->ps.pm_flags & PMF_LIMBO ) || ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_SPECTATOR ) ) { // JPW NERVE + return; // JPW NERVE don't do flame damage to guys in limbo or spectator, they drop out of the game + + } + CG_ClientDamage( i, centFlameInfo[i].lastDmgEnemy, CLDMG_FLAMETHROWER ); + centFlameInfo[i].lastDmgUpdate = cg.time; + } + } +} diff --git a/src/cgame/cg_info.c b/src/cgame/cg_info.c new file mode 100644 index 0000000..5f45326 --- /dev/null +++ b/src/cgame/cg_info.c @@ -0,0 +1,667 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// cg_info.c -- display information while data is being loading + +#include "cg_local.h" +#include "../ui/ui_shared.h" + +#define MAX_LOADING_PLAYER_ICONS 16 +#define MAX_LOADING_ITEM_ICONS 26 + +static int loadingPlayerIconCount; +static int loadingItemIconCount; +static qhandle_t loadingPlayerIcons[MAX_LOADING_PLAYER_ICONS]; +static qhandle_t loadingItemIcons[MAX_LOADING_ITEM_ICONS]; + + +/* +=================== +CG_DrawLoadingIcons +=================== +*/ +static void CG_DrawLoadingIcons( void ) { + int n; + int x, y; + + // JOSEPH 5-2-00 Per MAXX + return; + + for ( n = 0; n < loadingPlayerIconCount; n++ ) { + x = 16 + n * 78; + y = 324; + CG_DrawPic( x, y, 64, 64, loadingPlayerIcons[n] ); + } + + for ( n = 0; n < loadingItemIconCount; n++ ) { + y = 400; + if ( n >= 13 ) { + y += 40; + } + x = 16 + n % 13 * 48; + CG_DrawPic( x, y, 32, 32, loadingItemIcons[n] ); + } +} + + +/* +====================== +CG_LoadingString + +====================== +*/ +void CG_LoadingString( const char *s ) { + Q_strncpyz( cg.infoScreenText, s, sizeof( cg.infoScreenText ) ); + + if ( s && s[0] != 0 ) { + CG_Printf( va( "LOADING... %s\n",s ) ); //----(SA) added so you can see from the console what's going on + + } + trap_UpdateScreen(); +} + +/* +=================== +CG_LoadingItem +=================== +*/ +void CG_LoadingItem( int itemNum ) { + gitem_t *item; + + item = &bg_itemlist[itemNum]; + + if ( item->giType == IT_KEY ) { // do not show keys at level startup //----(SA) + return; + } + +//----(SA) Max Kaufman request that we don't show any pacifier stuff for items + return; +//----(SA) end + + + if ( item->icon && loadingItemIconCount < MAX_LOADING_ITEM_ICONS ) { + loadingItemIcons[loadingItemIconCount++] = trap_R_RegisterShaderNoMip( item->icon ); + } + + CG_LoadingString( cgs.itemPrintNames[item - bg_itemlist] ); +} + +/* +=================== +CG_LoadingClient +=================== +*/ +void CG_LoadingClient( int clientNum ) { + const char *info; + char *skin; + char personality[MAX_QPATH]; + char model[MAX_QPATH]; + char iconName[MAX_QPATH]; + + if ( cgs.gametype == GT_SINGLE_PLAYER && clientNum > 0 ) { // for now only show the player's icon in SP games + return; + } + + info = CG_ConfigString( CS_PLAYERS + clientNum ); + + Q_strncpyz( model, Info_ValueForKey( info, "model" ), sizeof( model ) ); + skin = Q_strrchr( model, '/' ); + if ( skin ) { + *skin++ = '\0'; + } else { + skin = "default"; + } + + Com_sprintf( iconName, MAX_QPATH, "models/players/%s/icon_%s.tga", model, skin ); + +// (SA) ignore player icons for the moment + if ( !( cg_entities[clientNum].currentState.aiChar ) ) { +// if ( loadingPlayerIconCount < MAX_LOADING_PLAYER_ICONS ) { +// loadingPlayerIcons[loadingPlayerIconCount++] = trap_R_RegisterShaderNoMip( iconName ); +// } + } + + Q_strncpyz( personality, Info_ValueForKey( info, "n" ), sizeof( personality ) ); + Q_CleanStr( personality ); + + if ( cgs.gametype == GT_SINGLE_PLAYER ) { + trap_S_RegisterSound( va( "sound/player/announce/%s.wav", personality ) ); + } + + CG_LoadingString( personality ); +} + +/* +==================== +CG_DrawStats +==================== +*/ + +typedef struct { + char *label; + int YOfs; + int labelX; + int labelFlags; + vec4_t labelColor; + + char *format; + int formatX; + int formatFlags; + vec4_t formatColor; + + int numVars; +} statsItem_t; + +// this defines the layout of the mission stats +// NOTE: these must match the stats sent in AICast_ScriptAction_ChangeLevel() +static statsItem_t statsItems[] = { + { "end_time", 168, 214, ITEM_TEXTSTYLE_SHADOWEDMORE, {1.0f, 1.0f, 1.0f, 1.0f}, "%02i:%02i:%02i", 348, ITEM_TEXTSTYLE_SHADOWEDMORE, {1.0f, 1.0f, 1.0f, 1.0f}, 3 }, + { "end_objectives", 28, 214, ITEM_TEXTSTYLE_SHADOWEDMORE, {1.0f, 1.0f, 1.0f, 1.0f}, "%i/%i", 348, ITEM_TEXTSTYLE_SHADOWEDMORE, {1.0f, 1.0f, 1.0f, 1.0f}, 2 }, + { "end_secrets", 28, 214, ITEM_TEXTSTYLE_SHADOWEDMORE, {1.0f, 1.0f, 1.0f, 1.0f}, "%i/%i", 348, ITEM_TEXTSTYLE_SHADOWEDMORE, {1.0f, 1.0f, 1.0f, 1.0f}, 2 }, + { "end_treasure", 28, 214, ITEM_TEXTSTYLE_SHADOWEDMORE, {0.62f, 0.56f, 0.0f, 1.0f}, "%i/%i", 348, ITEM_TEXTSTYLE_SHADOWEDMORE, {1.0f, 1.0f, 1.0f, 1.0f}, 2 }, + { "end_attempts", 28, 214, ITEM_TEXTSTYLE_SHADOWEDMORE, {1.0f, 1.0f, 1.0f, 1.0f}, "%i", 348, ITEM_TEXTSTYLE_SHADOWEDMORE, {1.0f, 1.0f, 1.0f, 1.0f}, 1 }, + + { NULL } +}; + +// int numSecrets; +// int numTreasure; +// int numTreasureFound; +// int numArtifacts; +// int numObjectives; + + + +/* +============== +CG_DrawStats +============== +*/ +void CG_DrawStats( char *stats ) { + int i, y, v, j; + #define MAX_STATS_VARS 64 + int vars[MAX_STATS_VARS]; + char *str, *token; + char *formatStr; + int varIndex; + char string[MAX_QPATH]; + + UI_DrawProportionalString( 320, 120, "MISSION STATS", + UI_CENTER | UI_SMALLFONT | UI_DROPSHADOW, colorWhite ); + + Q_strncpyz( string, stats, sizeof( string ) ); + str = string; + // convert commas to spaces + for ( i = 0; str[i]; i++ ) { + if ( str[i] == ',' ) { + str[i] = ' '; + } + } + + for ( i = 0, y = 0, v = 0; statsItems[i].label; i++ ) { + y += statsItems[i].YOfs; + +// UI_DrawProportionalString( statsItems[i].labelX, y, statsItems[i].label, +// statsItems[i].labelFlags, *statsItems[i].labelColor ); + + if ( statsItems[i].numVars ) { + varIndex = v; + for ( j = 0; j < statsItems[i].numVars; j++ ) { + token = COM_Parse( &str ); + if ( !token || !token[0] ) { + CG_Error( "error parsing mission stats\n" ); + return; + } + + vars[v++] = atoi( token ); + } + + // build the formatStr + switch ( statsItems[i].numVars ) { + case 1: + formatStr = va( statsItems[i].format, vars[varIndex] ); + break; + case 2: + formatStr = va( statsItems[i].format, vars[varIndex], vars[varIndex + 1] ); + break; + case 3: + formatStr = va( statsItems[i].format, vars[varIndex], vars[varIndex + 1], vars[varIndex + 2] ); + break; + case 4: + formatStr = va( statsItems[i].format, vars[varIndex], vars[varIndex + 1], vars[varIndex + 2], vars[varIndex + 3] ); + break; + } + +// UI_DrawProportionalString( statsItems[i].formatX, y, formatStr, +// statsItems[i].formatFlags, *statsItems[i].formatColor ); + } + } +} + +/* +============== +CG_DrawExitStats + pretty much what the game should draw when you're at the exit + This is not the final deal, but represents the kind of thing + that will be there +============== +*/ + +void CG_DrawExitStats( void ) { + int i, y, v, j; + float *color; // faded color based on cursor hint drawing + float color2[4] = {0, 0, 0, 1}; + const char *str; + char *mstats, *token; + + #define MAX_STATS_VARS 64 + int vars[MAX_STATS_VARS]; + char *formatStr = NULL; // TTimo: init + int varIndex = 0; // TTimo: init + + if ( cg_paused.integer ) { + // no draw if any menu's are up (or otherwise paused) + return; + } + + color = CG_FadeColor( cg.cursorHintTime, cg.cursorHintFade ); + + if ( !color ) { // currently faded out, don't draw + return; + } + + // check for fade up + if ( cg.time < ( cg.exitStatsTime + cg.exitStatsFade ) ) { + color[3] = (float)( cg.time - cg.exitStatsTime ) / (float)cg.exitStatsFade; + } + + color2[3] = color[3]; + + +// parse it + str = CG_ConfigString( CS_MISSIONSTATS ); + + if ( !str || !str[0] ) { + return; + } + + // background + color2[3] *= 0.6f; + CG_FilledBar( 150, 104, 340, 230, color2, NULL, NULL, 1.0f, 0 ); + + color2[0] = color2[1] = color2[2] = 0.3f; + color2[3] *= 0.6f; + + // border + CG_FilledBar( 148, 104, 2, 230, color2, NULL, NULL, 1.0f, 0 ); // left + CG_FilledBar( 490, 104, 2, 230, color2, NULL, NULL, 1.0f, 0 ); // right + CG_FilledBar( 148, 102, 344, 2, color2, NULL, NULL, 1.0f, 0 ); // top + CG_FilledBar( 148, 334, 344, 2, color2, NULL, NULL, 1.0f, 0 ); // bot + + + // text boxes + color2[0] = color2[1] = color2[2] = 0.4f; + for ( i = 0; i < 5; i++ ) { + CG_FilledBar( 170, 154 + ( 28 * i ), 300, 20, color2, NULL, NULL, 1.0f, 0 ); + } + + + // green title + color2[0] = color2[2] = 0; + color2[1] = 0.3f; + CG_FilledBar( 150, 104, 340, 20, color2, NULL, NULL, 1.0f, 0 ); + + color2[0] = color2[1] = color2[2] = 0.2f; + + // title + color2[0] = color2[1] = color2[2] = 1; + color2[3] = color[3]; +// CG_Text_Paint(280, 120, 2, 0.25f, color2, va("%s", CG_translateString("end_title")), 0, 0, ITEM_TEXTSTYLE_SHADOWEDMORE); + //----(SA) scale change per MK + CG_Text_Paint( 270, 120, 2, 0.313f, color2, va( "%s", CG_translateString( "end_title" ) ), 0, 0, ITEM_TEXTSTYLE_SHADOWEDMORE ); + + color2[0] = color2[1] = color2[2] = 1; + if ( cg.cursorHintIcon == HINT_NOEXIT ) { + // "exit not available" +// CG_Text_Paint(250, 320, 2, 0.3f, color2, va("%s", CG_translateString("end_noexit")), 0, 0, ITEM_TEXTSTYLE_SHADOWEDMORE); + //----(SA) scale change per MK + CG_Text_Paint( 260, 320, 2, 0.225f, color2, va( "%s", CG_translateString( "end_noexit" ) ), 0, 0, ITEM_TEXTSTYLE_SHADOWEDMORE ); + } else { + // "forward to proceed" +// CG_Text_Paint(230, 320, 2, 0.3f, color2, va("%s", CG_translateString("end_exit")), 0, 0, ITEM_TEXTSTYLE_SHADOWEDMORE); + //----(SA) scale change per MK + CG_Text_Paint( 250, 320, 2, 0.225f, color2, va( "%s", CG_translateString( "end_exit" ) ), 0, 0, ITEM_TEXTSTYLE_SHADOWEDMORE ); + } + + mstats = (char*)str + 2; // add offset for 's=' + for ( i = 0; mstats[i]; i++ ) { + if ( mstats[i] == ',' ) { + mstats[i] = ' '; + } + } + for ( i = 0, y = 0, v = 0; statsItems[i].label; i++ ) { + y += statsItems[i].YOfs; + + VectorCopy4( statsItems[i].labelColor, color2 ); +// statsItems[i].labelColor[3] = statsItems[i].formatColor[3] = color[3]; // set proper alpha + color2[3] = statsItems[i].formatColor[3] = color[3]; // set proper alpha + + + if ( statsItems[i].numVars ) { + varIndex = v; + for ( j = 0; j < statsItems[i].numVars; j++ ) { + token = COM_Parse( &mstats ); + if ( !token || !token[0] ) { + CG_Error( "error parsing mission stats\n" ); + return; + } + + vars[v++] = atoi( token ); + } + + // build the formatStr + switch ( statsItems[i].numVars ) { + case 1: + formatStr = va( statsItems[i].format, vars[varIndex] ); + break; + case 2: + formatStr = va( statsItems[i].format, vars[varIndex], vars[varIndex + 1] ); + break; + case 3: + formatStr = va( statsItems[i].format, vars[varIndex], vars[varIndex + 1], vars[varIndex + 2] ); + break; + case 4: + formatStr = va( statsItems[i].format, vars[varIndex], vars[varIndex + 1], vars[varIndex + 2], vars[varIndex + 3] ); + break; + } + + CG_Text_Paint( statsItems[i].formatX, y, 2, 0.3, statsItems[i].formatColor, formatStr, 0, 0, statsItems[i].formatFlags ); +// UI_DrawProportionalString( statsItems[i].formatX, y, formatStr, statsItems[i].formatFlags, *statsItems[i].formatColor ); + } + + if ( i == 1 ) { // 'objectives' + if ( vars[varIndex] < vars[varIndex + 1] ) { // missing objectives, draw in red + color2[0] = 1; + color2[1] = color2[2] = 0; + } + } + + if ( i == 3 ) { // 'treasure' + if ( vars[varIndex] < vars[varIndex + 1] || !vars[varIndex + 1] ) { // missing treasure, only draw in white (gold when you got em all) (unless there's no gold available, then 0/0 shows white) + color2[0] = color2[1] = color2[2] = 1; // white + } + } + +// UI_DrawProportionalString( statsItems[i].labelX, y, statsItems[i].label, statsItems[i].labelFlags, *statsItems[i].labelColor ); + +// CG_Text_Paint(statsItems[i].labelX, y, 2, 0.3, statsItems[i].labelColor, va("%s:", CG_translateString(statsItems[i].label)), 0, 0, statsItems[i].labelFlags); + CG_Text_Paint( statsItems[i].labelX, y, 2, 0.3, color2, va( "%s:", CG_translateString( statsItems[i].label ) ), 0, 0, statsItems[i].labelFlags ); + + } + token = COM_Parse( &mstats ); + +// end (parse it) +} + + +/* +==================== +CG_DrawInformation + +Draw all the status / pacifier stuff during level loading +==================== +*/ +void CG_DrawInformation( void ) { + const char *s; + const char *info; + const char *sysInfo; + int y; + int value; + qhandle_t levelshot = 0; // TTimo: init +// qhandle_t detail; + char buf[1024]; + static int lastDraw = 0; // Ridah, so we don't draw the screen more often than we need to + int ms; + static int callCount = 0; + float percentDone; + + int expectedHunk; + char hunkBuf[MAX_QPATH]; + + vec4_t color; + + if ( cg.snap && ( strlen( cg_missionStats.string ) <= 1 ) ) { + return; // we are in the world, no need to draw information + } + + if ( callCount ) { // reject recursive calls + return; + } + + ms = trap_Milliseconds(); + if ( ( lastDraw <= ms ) && ( lastDraw > ms - 100 ) ) { + return; + } + lastDraw = ms; + + callCount++; + + info = CG_ConfigString( CS_SERVERINFO ); + sysInfo = CG_ConfigString( CS_SYSTEMINFO ); + + trap_Cvar_VariableStringBuffer( "com_expectedhunkusage", hunkBuf, MAX_QPATH ); + expectedHunk = atoi( hunkBuf ); + + + s = Info_ValueForKey( info, "mapname" ); + + //----(SA) just the briefing now + + if ( s && s[0] != 0 ) { // there is often no 's' + levelshot = trap_R_RegisterShaderNoMip( va( "levelshots/%s.tga", s ) ); + } + + if ( !levelshot ) { + levelshot = trap_R_RegisterShaderNoMip( "menu/art/unknownmap" ); + } + + trap_R_SetColor( NULL ); +// CG_DrawPic( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, levelshot ); + + // blend a detail texture over it + //detail = trap_R_RegisterShader( "levelShotDetail" ); + //trap_R_DrawStretchPic( 0, 0, cgs.glconfig.vidWidth, cgs.glconfig.vidHeight, 0, 0, 2.5, 2, detail ); + + +// (SA) commented out for Drew +// UI_DrawProportionalString( 320, 16, va( "Loading %s", Info_ValueForKey( info, "mapname" ) ), UI_SMALLFONT|UI_CENTER|UI_DROPSHADOW, colorWhite ); + + // show the loading progress + VectorSet( color, 0.8, 0.8, 0.8 ); + color[3] = 0.8; + + if ( strlen( cg_missionStats.string ) > 1 && cg_missionStats.string[0] == 's' ) { + vec2_t xy = { 190, 470 }; + vec2_t wh = { 260, 10 }; + + // draw the mission stats while loading + + if ( expectedHunk > 0 ) { + percentDone = (float)( cg_hunkUsed.integer + cg_soundAdjust.integer ) / (float)( expectedHunk ); + if ( percentDone > 0.97 ) { // never actually show 100%, since we are not in the game yet + percentDone = 0.97; + } + CG_HorizontalPercentBar( xy[0] + 10, xy[1] + wh[1] - 10, wh[0] - 20, 10, percentDone ); + } else { + UI_DrawProportionalString( 320, xy[1] + wh[1] - 10, "please wait", + UI_CENTER | UI_EXSMALLFONT | UI_DROPSHADOW, color ); + } + + trap_UpdateScreen(); + callCount--; + return; + } + + // Ridah, in single player, cheats disabled, don't show unnecessary information + if ( cgs.gametype == GT_SINGLE_PLAYER ) { + + if ( 0 ) { // bar drawn in menu now + vec2_t xy = { 200, 468 }; + vec2_t wh = { 240, 10 }; + + // show the percent complete bar + if ( expectedHunk > 0 ) { + percentDone = (float)( cg_hunkUsed.integer + cg_soundAdjust.integer ) / (float)( expectedHunk ); + if ( percentDone > 0.97 ) { + percentDone = 0.97; + } + + CG_HorizontalPercentBar( xy[0], xy[1], wh[0], wh[1], percentDone ); + } + } + + trap_UI_Popup( "briefing" ); + + trap_UpdateScreen(); + callCount--; + return; + } + // done. + + + // draw the icons of thiings as they are loaded + CG_DrawLoadingIcons(); + + // the first 150 rows are reserved for the client connection + // screen to write into + if ( cg.infoScreenText[0] ) { + UI_DrawProportionalString( 320, 128, va( "Loading... %s", cg.infoScreenText ), + UI_CENTER | UI_SMALLFONT | UI_DROPSHADOW, colorWhite ); + } else { + UI_DrawProportionalString( 320, 128, "Awaiting snapshot...", + UI_CENTER | UI_SMALLFONT | UI_DROPSHADOW, colorWhite ); + } + + // draw info string information + + y = 180; + + // don't print server lines if playing a local game + trap_Cvar_VariableStringBuffer( "sv_running", buf, sizeof( buf ) ); + if ( !atoi( buf ) ) { + // server hostname + s = Info_ValueForKey( info, "sv_hostname" ); + UI_DrawProportionalString( 320, y, s, + UI_CENTER | UI_SMALLFONT | UI_DROPSHADOW, colorWhite ); + y += PROP_HEIGHT; + + // server-specific message of the day + s = CG_ConfigString( CS_MOTD ); + if ( s[0] ) { + UI_DrawProportionalString( 320, y, s, + UI_CENTER | UI_SMALLFONT | UI_DROPSHADOW, colorWhite ); + y += PROP_HEIGHT; + } + + // some extra space after hostname and motd + y += 10; + } + + // map-specific message (long map name) + s = CG_ConfigString( CS_MESSAGE ); + if ( s[0] ) { + UI_DrawProportionalString( 320, y, s, + UI_CENTER | UI_SMALLFONT | UI_DROPSHADOW, colorWhite ); + y += PROP_HEIGHT; + } + + // cheats warning + s = Info_ValueForKey( sysInfo, "sv_cheats" ); + if ( s[0] == '1' ) { + UI_DrawProportionalString( 320, y, "CHEATS ARE ENABLED", + UI_CENTER | UI_SMALLFONT | UI_DROPSHADOW, colorWhite ); + y += PROP_HEIGHT; + } + + // game type + switch ( cgs.gametype ) { + case GT_FFA: + s = "Free For All"; + break; + case GT_SINGLE_PLAYER: + s = "Single Player"; + break; + case GT_TOURNAMENT: + s = "Tournament"; + break; + case GT_TEAM: + s = "Team Deathmatch"; + break; + case GT_CTF: + s = "Capture The Flag"; + break; +// JPW NERVE + case GT_WOLF: + s = "Wolfenstein Multiplayer"; + break; +// jpw + default: + s = "Unknown Gametype"; + break; + } + UI_DrawProportionalString( 320, y, s, + UI_CENTER | UI_SMALLFONT | UI_DROPSHADOW, colorWhite ); + y += PROP_HEIGHT; + + value = atoi( Info_ValueForKey( info, "timelimit" ) ); + if ( value ) { + UI_DrawProportionalString( 320, y, va( "timelimit %i", value ), + UI_CENTER | UI_SMALLFONT | UI_DROPSHADOW, colorWhite ); + y += PROP_HEIGHT; + } + + if ( cgs.gametype != GT_CTF && cgs.gametype != GT_SINGLE_PLAYER ) { + value = atoi( Info_ValueForKey( info, "fraglimit" ) ); + if ( value ) { + UI_DrawProportionalString( 320, y, va( "fraglimit %i", value ), + UI_CENTER | UI_SMALLFONT | UI_DROPSHADOW, colorWhite ); + y += PROP_HEIGHT; + } + } + + if ( cgs.gametype == GT_CTF ) { + value = atoi( Info_ValueForKey( info, "capturelimit" ) ); + if ( value ) { + UI_DrawProportionalString( 320, y, va( "capturelimit %i", value ), + UI_CENTER | UI_SMALLFONT | UI_DROPSHADOW, colorWhite ); + y += PROP_HEIGHT; + } + } + + callCount--; +} diff --git a/src/cgame/cg_local.h b/src/cgame/cg_local.h new file mode 100644 index 0000000..54d32d8 --- /dev/null +++ b/src/cgame/cg_local.h @@ -0,0 +1,2460 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +/* + * name: cg_local.h + * + * desc: The entire cgame module is unloaded and reloaded on each level change, + * so there is NO persistant data between levels on the client side. + * If you absolutely need something stored, it can either be kept + * by the server in the server stored userinfos, or stashed in a cvar. + + * +*/ + +#include "../game/q_shared.h" +#include "tr_types.h" +#include "../game/bg_public.h" +#include "cg_public.h" + + +#define POWERUP_BLINKS 5 + +#define POWERUP_BLINK_TIME 1000 +#define FADE_TIME 200 +#define PULSE_TIME 200 +#define DAMAGE_DEFLECT_TIME 100 +#define DAMAGE_RETURN_TIME 400 +#define DAMAGE_TIME 500 +#define LAND_DEFLECT_TIME 150 +#define LAND_RETURN_TIME 300 +#define STEP_TIME 200 +#define DUCK_TIME 100 +#define PAIN_TWITCH_TIME 200 +#define WEAPON_SELECT_TIME 1400 +#define HOLDABLE_SELECT_TIME 1400 //----(SA) for drawing holdable icons +#define ITEM_SCALEUP_TIME 1000 +#define ZOOM_TIME 150 +#define ITEM_BLOB_TIME 200 +#define MUZZLE_FLASH_TIME 30 //----(SA) +#define SINK_TIME 1000 // time for fragments to sink into ground before going away +#define ATTACKER_HEAD_TIME 10000 +#define REWARD_TIME 3000 + +#define PULSE_SCALE 1.5 // amount to scale up the icons when activating + +#define MAX_STEP_CHANGE 32 + +#define MAX_VERTS_ON_POLY 10 +#define MAX_MARK_POLYS 1024 + +#define STAT_MINUS 10 // num frame for '-' stats digit + +#define ICON_SIZE 48 +#define CHAR_WIDTH 32 +#define CHAR_HEIGHT 48 +#define TEXT_ICON_SPACE 4 + +#define TEAMCHAT_WIDTH 80 +#define TEAMCHAT_HEIGHT 8 + +// very large characters +#define GIANT_WIDTH 32 +#define GIANT_HEIGHT 48 + +#define NUM_CROSSHAIRS 10 + +// Ridah, trails +#define STYPE_STRETCH 0 +#define STYPE_REPEAT 1 + +#define TJFL_FADEIN ( 1 << 0 ) +#define TJFL_CROSSOVER ( 1 << 1 ) +#define TJFL_NOCULL ( 1 << 2 ) +#define TJFL_FIXDISTORT ( 1 << 3 ) +#define TJFL_SPARKHEADFLARE ( 1 << 4 ) +#define TJFL_NOPOLYMERGE ( 1 << 5 ) +// done. + +// NERVE - SMF - limbo mode 3d view position +#define LIMBO_3D_X 10 +#define LIMBO_3D_Y 120 +#define LIMBO_3D_W 420 +#define LIMBO_3D_H 330 +// -NERVE - SMF + +//================================================= + +// player entities need to track more information +// than any other type of entity. + +// note that not every player entity is a client entity, +// because corpses after respawn are outside the normal +// client numbering range + +// when changing animation, set animationTime to frameTime + lerping time +// The current lerp will finish out, then it will lerp to the new animation +typedef struct { + int oldFrame; + int oldFrameTime; // time when ->oldFrame was exactly on + + int frame; + int frameTime; // time when ->frame will be exactly on + + float backlerp; + + float yawAngle; + qboolean yawing; + float pitchAngle; + qboolean pitching; + + int animationNumber; // may include ANIM_TOGGLEBIT + int oldAnimationNumber; // may include ANIM_TOGGLEBIT + animation_t *animation; + int animationTime; // time when the first frame of the animation will be exact + + // Ridah, variable speed anims + vec3_t oldFramePos; + float animSpeedScale; + int oldFrameSnapshotTime; + headAnimation_t *headAnim; + // done. + + animation_t *cgAnim; // pointer to the root of the animation array to use (*animation above points at the current sequence) //----(SA) added +} lerpFrame_t; + +// Ridah, effect defines +#define MAX_ZOMBIE_SPIRITS 4 +#define MAX_ZOMBIE_DEATH_TRAILS 16 + +#define MAX_LOPER_LIGHTNING_POINTS 24 + +#define MAX_TESLA_BOLTS 4 + +#define MAX_EFFECT_ENTS 20 + +typedef struct { + lerpFrame_t legs, torso; + + // Ridah, talking animations + lerpFrame_t head; + // done. + + lerpFrame_t weap; //----(SA) autonomous weapon animations + + int lastTime; // last time we were processed/ If the time goes backwards, reset. + + int painTime; + int painDuration; + int painDirection; // flip from 0 to 1 + int painAnimTorso; + int painAnimLegs; + int lightningFiring; + + // railgun trail spawning + vec3_t railgunImpact; + qboolean railgunFlash; + + // machinegun spinning + float barrelAngle; + int barrelTime; + qboolean barrelSpinning; + + //----(SA) machinegun bolt sliding + float boltPosition; + int boltTime; + int boltSliding; + //----(SA) end + + //----(SA) 'spinner' spinning (body part) + float spinnerAngle; + int spinnerTime; + qboolean spinnerSpinning; + //----(SA) end + + // Ridah, so we can do fast tag grabbing + refEntity_t legsRefEnt, torsoRefEnt, headRefEnt, gunRefEnt; + int gunRefEntFrame; + + float animSpeed; // for manual adjustment + + // Zombie spirit effect + // !!FIXME: these effects will be restarted by a *_restart command, can we save this data somehow? + qboolean cueZombieSpirit; // if this is qfalse, and the zombie effect flag is set, then we need to start a new attack + int zombieSpiritStartTime; // time the effect was started, so we can fade things in + int zombieSpiritTrailHead[MAX_ZOMBIE_SPIRITS]; + int zombieSpiritRotationTimes[MAX_ZOMBIE_SPIRITS]; + int zombieSpiritRadiusCycleTimes[MAX_ZOMBIE_SPIRITS]; + int lastZombieSpirit; + int nextZombieSpiritSound; + int zombieSpiritEndTime; // time the effect was disabled + vec3_t zombieSpiritPos[MAX_ZOMBIE_SPIRITS]; + vec3_t zombieSpiritDir[MAX_ZOMBIE_SPIRITS]; + float zombieSpiritSpeed[MAX_ZOMBIE_SPIRITS]; + int zombieSpiritStartTimes[MAX_ZOMBIE_SPIRITS]; + + // Zombie death effect + // !!FIXME: these effects will be restarted by a *_restart command, can we save this data somehow? + qboolean cueZombieDeath; // if this is qfalse, and the zombie effect flag is set, then we need to start a new attack + int zombieDeathStartTime; // time the effect was started, so we can fade things in + int zombieDeathEndTime; // time the effect was disabled + int lastZombieDeath; + int zombieDeathFadeStart; + int zombieDeathFadeEnd; + int zombieDeathTrailHead[MAX_ZOMBIE_DEATH_TRAILS]; + int zombieDeathRotationTimes[MAX_ZOMBIE_DEATH_TRAILS]; + int zombieDeathRadiusCycleTimes[MAX_ZOMBIE_DEATH_TRAILS]; + + // loper effects + int loperLastGroundChargeTime; + byte loperGroundChargeToggle; + int loperGroundValidTime; + + vec3_t headLookIdeal; + vec3_t headLookOffset; + float headLookSpeed; + int headLookStopTime; + float headLookSpeedMax; + + // tesla coil effects + vec3_t teslaEndPoints[MAX_TESLA_BOLTS]; + int teslaEndPointTimes[MAX_TESLA_BOLTS]; // time the bolt stays valid + vec3_t teslaOffsetDirs[MAX_TESLA_BOLTS]; // bending direction from center or direct beam + float teslaOffsets[MAX_TESLA_BOLTS]; // amount to offset from center + int teslaOffsetTimes[MAX_TESLA_BOLTS]; // time the offset stays valid + int teslaEnemy[MAX_TESLA_BOLTS]; + int teslaDamageApplyTime; + + int teslaDamagedTime; // time we were last hit by a tesla bolt + + // misc effects + int effectEnts[MAX_EFFECT_ENTS]; + int numEffectEnts; + int effect1EndTime; + vec3_t lightningPoints[MAX_LOPER_LIGHTNING_POINTS]; + int lightningTimes[MAX_LOPER_LIGHTNING_POINTS]; + int lightningSoundTime; + + qboolean forceLOD; + +} playerEntity_t; + +//----(SA) +typedef struct { + char type[MAX_QPATH]; // md3_lower, md3_lbelt, md3_rbelt, etc. + char model[MAX_QPATH]; // lower.md3, belt1.md3, etc. +} skinModel_t; +//----(SA) end + + +//================================================= + + + +// centity_t have a direct corespondence with gentity_t in the game, but +// only the entityState_t is directly communicated to the cgame +typedef struct centity_s { + entityState_t currentState; // from cg.frame + entityState_t nextState; // from cg.nextFrame, if available + qboolean interpolate; // true if next is valid to interpolate to + qboolean currentValid; // true if cg.frame holds this entity + + int muzzleFlashTime; // move to playerEntity? + int overheatTime; + int previousEvent; + int previousEventSequence; // Ridah + int teleportFlag; + + int trailTime; // so missile trails can handle dropped initial packets + int miscTime; + + playerEntity_t pe; + + int errorTime; // decay the error from this time + vec3_t errorOrigin; + vec3_t errorAngles; + + qboolean extrapolated; // false if origin / angles is an interpolation + vec3_t rawOrigin; + vec3_t rawAngles; + + vec3_t beamEnd; + + // exact interpolated position of entity on this frame + vec3_t lerpOrigin; + vec3_t lerpAngles; + + vec3_t lastLerpAngles; // (SA) for remembering the last position when a state changes + + // Ridah, trail effects + int headJuncIndex, headJuncIndex2; + int lastTrailTime; + // done. + + // Ridah + float loopSoundVolume; + vec3_t fireRiseDir; // if standing still this will be up, otherwise it'll point away from movement dir + int lastWeaponClientFrame; + int lastFuseSparkTime; + vec3_t lastFuseSparkOrg; + + // client side dlights + int dl_frame; + int dl_oldframe; + float dl_backlerp; + int dl_time; + char dl_stylestring[64]; + int dl_sound; + int dl_atten; + + lerpFrame_t lerpFrame; //----(SA) added + vec3_t highlightOrigin; // center of the geometry. for things like corona placement on treasure + qboolean usehighlightOrigin; + + refEntity_t refEnt; + int processedFrame; // frame we were last added to the scene + + // client-side lightning + int boltTimes[MAX_TESLA_BOLTS]; + vec3_t boltLocs[MAX_TESLA_BOLTS]; + vec3_t boltCrawlDirs[MAX_TESLA_BOLTS]; + + // item highlighting + + int highlightTime; + qboolean highlighted; + + animation_t centAnim[2]; + + // (SA) added to help akimbo effects attach to the correct model + qboolean akimboFire; +} centity_t; + + +//====================================================================== + +// local entities are created as a result of events or predicted actions, +// and live independantly from all server transmitted entities + +typedef struct markPoly_s { + struct markPoly_s *prevMark, *nextMark; + int time; + qhandle_t markShader; + qboolean alphaFade; // fade alpha instead of rgb + float color[4]; + poly_t poly; + polyVert_t verts[MAX_VERTS_ON_POLY]; + + int duration; // Ridah +} markPoly_t; + +//----(SA) moved in from cg_view.c +typedef enum { + ZOOM_NONE, + ZOOM_BINOC, + ZOOM_SNIPER, + ZOOM_SNOOPER, + ZOOM_FG42SCOPE, + ZOOM_MG42, + ZOOM_MAX_ZOOMS +} EZoom_t; + +typedef enum { + ZOOM_OUT, // widest angle + ZOOM_IN // tightest angle (approaching 0) +} EZoomInOut_t; + +extern float zoomTable[ZOOM_MAX_ZOOMS][2]; + +//----(SA) end + +typedef enum { + LE_MARK, + LE_EXPLOSION, + LE_SPRITE_EXPLOSION, + LE_FRAGMENT, + LE_MOVE_SCALE_FADE, + LE_FALL_SCALE_FADE, + LE_FADE_RGB, + LE_SCALE_FADE, + LE_SPARK, + LE_DEBRIS, + LE_BLOOD, + LE_FUSE_SPARK, + LE_ZOMBIE_SPIRIT, + LE_HELGA_SPIRIT, + LE_ZOMBIE_BAT, + LE_MOVING_TRACER, + LE_EMITTER, + LE_SPIRIT_VIEWFLASH +} leType_t; + +typedef enum { + LEF_PUFF_DONT_SCALE = 0x0001 // do not scale size over time + ,LEF_TUMBLE = 0x0002 // tumble over time, used for ejecting shells + ,LEF_NOFADEALPHA = 0x0004 // Ridah, sparks + ,LEF_SMOKING = 0x0008 // (SA) smoking + ,LEF_NOTOUCHPARENT = 0x0010 // (SA) when tracing to eval trajectory, ignore parent cent + ,LEF_PLAYER_DAMAGE = 0x0020 // hurt the player on impact +} leFlag_t; + +typedef enum { + LEMT_NONE, + LEMT_BLOOD +} leMarkType_t; // fragment local entities can leave marks on walls + +typedef enum { + LEBS_NONE, + LEBS_BLOOD, + LEBS_ROCK, + LEBS_WOOD, + LEBS_BRASS, + LEBS_BONE +} leBounceSoundType_t; // fragment local entities can make sounds on impacts + +#define MAX_OLD_POS 3 + +typedef struct localEntity_s { + struct localEntity_s *prev, *next; + leType_t leType; + int leFlags; + + int startTime; + int endTime; + int fadeInTime; + + float lifeRate; // 1.0 / (endTime - startTime) + + trajectory_t pos; + trajectory_t angles; + + float bounceFactor; // 0.0 = no bounce, 1.0 = perfect + + float color[4]; + + float radius; + + float light; + vec3_t lightColor; + + leMarkType_t leMarkType; // mark to leave on fragment impact + leBounceSoundType_t leBounceSoundType; + + refEntity_t refEntity; + + // Ridah + int lightOverdraw; + int lastTrailTime; + int headJuncIndex, headJuncIndex2; + float effectWidth; + int effectFlags; + struct localEntity_s *chain; // used for grouping entities (like for flamethrower junctions) + int onFireStart, onFireEnd; + int ownerNum; + int lastSpiritDmgTime; + + int loopingSound; + + int breakCount; // break-up this many times before we can break no more + float sizeScale; + + char validOldPos[MAX_OLD_POS]; + vec3_t oldPos[MAX_OLD_POS]; + int oldPosHead; + // done. + +} localEntity_t; + +//====================================================================== + + +typedef struct { + int client; + int score; + int ping; + int time; + int scoreFlags; + int powerUps; + int accuracy; + int impressiveCount; + int excellentCount; + int guantletCount; + int defendCount; + int assistCount; + int captures; + qboolean perfect; + int team; +} score_t; + + +typedef enum { + ACC_BELT_LEFT, // belt left (lower) + ACC_BELT_RIGHT, // belt right (lower) + ACC_BELT, // belt (upper) + ACC_BACK, // back (upper) + ACC_WEAPON, // weapon (upper) + ACC_WEAPON2, // weapon2 (upper) + ACC_HAT, // hat (head) + ACC_MOUTH2, // + ACC_MOUTH3, // + // + ACC_MAX // this is bound by network limits, must change network stream to increase this + // (SA) No, really? that's not true is it? isn't this client-side only? +} accType_t; + +#define ACC_NUM_MOUTH 3 // matches the above count (hat/mouth2/mouth3) + + + + +// each client has an associated clientInfo_t +// that contains media references necessary to present the +// client model and other color coded effects +// this is regenerated each time a client's configstring changes, +// usually as a result of a userinfo (name, model, etc) change +#define MAX_CUSTOM_SOUNDS 32 +#define MAX_GIB_MODELS 16 +typedef struct { + qboolean infoValid; + + int clientNum; + + char name[MAX_QPATH]; + team_t team; + + int botSkill; // 0 = not bot, 1-5 = bot + + vec3_t color; + + int score; // updated by score servercmds + int location; // location index for team mode + int health; // you only get this info about your teammates + int armor; + int curWeapon; + + int handicap; + int wins, losses; // in tourney mode + + int powerups; // so can display quad/flag status + + int breathPuffTime; + + // when clientinfo is changed, the loading of models/skins/sounds + // can be deferred until you are dead, to prevent hitches in + // gameplay + char modelName[MAX_QPATH]; + char skinName[MAX_QPATH]; + char hSkinName[MAX_QPATH]; + qboolean deferred; + + qhandle_t legsModel; + qhandle_t legsSkin; + + qhandle_t torsoModel; + qhandle_t torsoSkin; + + qboolean isSkeletal; + + //----(SA) added accessory models/skins for belts/backpacks/etc. + qhandle_t accModels[ACC_MAX]; // see def of ACC_MAX for index descriptions + qhandle_t accSkins[ACC_MAX]; // FIXME: put the #define for number of accessory models somewhere. (SA) + + //----(SA) additional parts for specialized characters (the loper's spinning trunk for example) + qhandle_t partModels[9]; // [0-7] are optionally called in scripts, [8] is reserved for internal use + qhandle_t partSkins[9]; + //----(SA) end + + qhandle_t headModel; + qhandle_t headSkin; + + qhandle_t modelIcon; + + // RF, may be shared by multiple clients/characters + animModelInfo_t *modelInfo; + + sfxHandle_t sounds[MAX_CUSTOM_SOUNDS]; + + qhandle_t gibModels[MAX_GIB_MODELS]; + + vec3_t playermodelScale; //----(SA) set in the skin. client-side only + + int blinkTime; //----(SA) +} clientInfo_t; + + + +typedef enum { + W_PART_1, + W_PART_2, + W_PART_3, + W_PART_4, + W_PART_5, + W_PART_6, + W_PART_7, + W_MAX_PARTS +} barrelType_t; + +typedef enum { + W_TP_MODEL, // third person model + W_FP_MODEL, // first person model + W_PU_MODEL, // pickup model + W_FP_MODEL_SWAP, // swap out model + W_SKTP_MODEL, // SKELETAL version third person model + W_NUM_TYPES +} modelViewType_t; + +// each WP_* weapon enum has an associated weaponInfo_t +// that contains media references necessary to present the +// weapon and its effects +typedef struct weaponInfo_s { + qboolean registered; + gitem_t *item; + +//----(SA) weapon animation sequences loaded from the weapon.cfg + animation_t weapAnimations[MAX_WP_ANIMATIONS]; +//----(SA) end + + qhandle_t handsModel; // the hands don't actually draw, they just position the weapon + + qhandle_t standModel; // not drawn. tags used for positioning weapons for pickup + +//----(SA) mod for 1st/3rd person weap views + qhandle_t weaponModel[W_NUM_TYPES]; + qhandle_t wpPartModels[W_NUM_TYPES][W_MAX_PARTS]; + qhandle_t flashModel[W_NUM_TYPES]; + qhandle_t modModel[W_NUM_TYPES]; // like the scope for the rifles +//----(SA) end + + pose_t position; // wolf locations (high, low, knife, pistol, shoulder, throw) defines are WPOS_HIGH, WPOS_LOW, WPOS_KNIFE, WPOS_PISTOL, WPOS_SHOULDER, WPOS_THROW + + vec3_t weaponMidpoint; // so it will rotate centered instead of by tag + + float flashDlight; + vec3_t flashDlightColor; + sfxHandle_t flashSound[4]; // fast firing weapons randomly choose + sfxHandle_t flashEchoSound[4]; //----(SA) added - distant gun firing sound + sfxHandle_t lastShotSound[4]; // sound of the last shot can be different (mauser doesn't have bolt action on last shot for example) + + sfxHandle_t switchSound[4]; //----(SA) added + + qhandle_t weaponIcon[2]; //----(SA) [0] is weap icon, [1] is highlight icon + qhandle_t ammoIcon; + + qhandle_t ammoModel; + + qhandle_t missileModel; + sfxHandle_t missileSound; + void ( *missileTrailFunc )( centity_t *, const struct weaponInfo_s *wi ); + float missileDlight; + vec3_t missileDlightColor; + int missileRenderfx; + + void ( *ejectBrassFunc )( centity_t * ); + + float trailRadius; + float wiTrailTime; + + sfxHandle_t readySound; // an amibient sound the weapon makes when it's /not/ firing + sfxHandle_t firingSound; + sfxHandle_t overheatSound; + sfxHandle_t reloadSound; + + sfxHandle_t spinupSound; //----(SA) added // sound started when fire button goes down, and stepped on when the first fire event happens + sfxHandle_t spindownSound; //----(SA) added // sound called if the above is running but player doesn't follow through and fire +} weaponInfo_t; + + +// each IT_* item has an associated itemInfo_t +// that constains media references necessary to present the +// item and its effects +typedef struct { + qboolean registered; + qhandle_t models[MAX_ITEM_MODELS]; + qhandle_t icons[MAX_ITEM_ICONS]; +} itemInfo_t; + + +typedef struct { + int itemNum; +} powerupInfo_t; + +#define MAX_VIEWDAMAGE 8 +typedef struct { + int damageTime, damageDuration; + float damageX, damageY, damageValue; +} viewDamage_t; + +#define MAX_CAMERA_SHAKE 4 +typedef struct { + int time; + float scale; + float length; + float radius; + vec3_t src; +} cameraShake_t; + +//====================================================================== + +// all cg.stepTime, cg.duckTime, cg.landTime, etc are set to cg.time when the action +// occurs, and they will have visible effects for #define STEP_TIME or whatever msec after + +#define MAX_PREDICTED_EVENTS 16 + +typedef struct { + int clientFrame; // incremented each frame + + int clientNum; + + qboolean demoPlayback; + qboolean levelShot; // taking a level menu screenshot + int deferredPlayerLoading; + qboolean loading; // don't defer players at initial startup + qboolean intermissionStarted; // don't play voice rewards, because game will end shortly + + // there are only one or two snapshot_t that are relevent at a time + int latestSnapshotNum; // the number of snapshots the client system has received + int latestSnapshotTime; // the time from latestSnapshotNum, so we don't need to read the snapshot yet + + snapshot_t *snap; // cg.snap->serverTime <= cg.time + snapshot_t *nextSnap; // cg.nextSnap->serverTime > cg.time, or NULL + snapshot_t activeSnapshots[2]; + + float frameInterpolation; // (float)( cg.time - cg.frame->serverTime ) / (cg.nextFrame->serverTime - cg.frame->serverTime) + + qboolean thisFrameTeleport; + qboolean nextFrameTeleport; + + int frametime; // cg.time - cg.oldTime + + int time; // this is the time value that the client + // is rendering at. + int oldTime; // time at last frame, used for missile trails and prediction checking + + int physicsTime; // either cg.snap->time or cg.nextSnap->time + + int timelimitWarnings; // 5 min, 1 min, overtime + int fraglimitWarnings; + + qboolean mapRestart; // set on a map restart to set back the weapon + + qboolean renderingThirdPerson; // during deaths, chasecams, etc + + // prediction state + qboolean hyperspace; // true if prediction has hit a trigger_teleport + playerState_t predictedPlayerState; + centity_t predictedPlayerEntity; + qboolean validPPS; // clear until the first call to CG_PredictPlayerState + int predictedErrorTime; + vec3_t predictedError; + + int eventSequence; + int predictableEvents[MAX_PREDICTED_EVENTS]; + + float stepChange; // for stair up smoothing + int stepTime; + + float duckChange; // for duck viewheight smoothing + int duckTime; + + float landChange; // for landing hard + int landTime; + + // input state sent to server + int weaponSelect; + int holdableSelect; // (SA) which holdable item is currently held ("selected"). When the client is ready to use it, send "use item " + + // auto rotating items + vec3_t autoAnglesSlow; + vec3_t autoAxisSlow[3]; + vec3_t autoAngles; + vec3_t autoAxis[3]; + vec3_t autoAnglesFast; + vec3_t autoAxisFast[3]; + + // view rendering + refdef_t refdef; + vec3_t refdefViewAngles; // will be converted to refdef.viewaxis + + // zoom key + qboolean zoomed; + qboolean zoomedBinoc; + int zoomedScope; //----(SA) changed to int + int zoomTime; + float zoomSensitivity; + float zoomval; + + + // information screen text during loading + unsigned char infoScreenText[MAX_STRING_CHARS]; + + // scoreboard + int scoresRequestTime; + int numScores; + int selectedScore; + int teamScores[2]; + score_t scores[MAX_CLIENTS]; + qboolean showScores; + qboolean scoreBoardShowing; + int scoreFadeTime; + char killerName[MAX_NAME_LENGTH]; + char spectatorList[MAX_STRING_CHARS]; // list of names + int spectatorLen; // length of list + float spectatorWidth; // width in device units + int spectatorTime; // next time to offset + int spectatorPaintX; // current paint x + int spectatorPaintX2; // current paint x + int spectatorOffset; // current offset from start + int spectatorPaintLen; // current offset from start + + qboolean showItems; + int itemFadeTime; + + qboolean lightstylesInited; + + // centerprinting + int centerPrintTime; + int centerPrintCharWidth; + int centerPrintY; + unsigned char centerPrint[1024]; + int centerPrintLines; + + // fade in/out + int fadeTime; + float fadeRate; + vec4_t fadeColor1; + vec4_t fadeColor2; + +//----(SA) added + // game stats + int exitStatsTime; + int exitStatsFade; + // just a copy of what's on the server, updated by configstring. better way to communicate/store this I'm sure + int playTimeH; + int playTimeM; + int playTimeS; + int attempts; + int numObjectives; + int numObjectivesFound; + int numSecrets; + int numSecretsFound; + int numTreasure; + int numTreasureFound; + int numArtifacts; + int numArtifactsFound; +//----(SA) end + + + // low ammo warning state + int lowAmmoWarning; // 1 = low, 2 = empty + + // kill timers for carnage reward + int lastKillTime; + + // crosshair client ID + int crosshairClientNum; + int crosshairClientTime; + + int crosshairPowerupNum; + int crosshairPowerupTime; + +//----(SA) added + // cursorhints + int cursorHintIcon; + int cursorHintTime; + int cursorHintFade; + int cursorHintValue; + +//----(SA) end + + // powerup active flashing + int powerupActive; + int powerupTime; + + // attacking player + int attackerTime; + int voiceTime; + + // reward medals + int rewardTime; + int rewardCount; + qhandle_t rewardShader; + + // warmup countdown + int warmup; + int warmupCount; + + // message icon popup time //----(SA) added + int yougotmailTime; + + //========================== + + int itemPickup; + int itemPickupTime; + int itemPickupBlendTime; // the pulse around the crosshair is timed seperately + + int holdableSelectTime; //----(SA) for holdable item icon drawing + + int weaponSelectTime; + int weaponAnimation; + int weaponAnimationTime; + + // blend blobs + viewDamage_t viewDamage[MAX_VIEWDAMAGE]; + float damageTime; // last time any kind of damage was recieved + int damageIndex; // slot that was filled in + float damageX, damageY, damageValue; + float viewFade; + + int grenLastTime; + + int switchbackWeapon; + int lastFiredWeapon; + int lastWeapSelInBank[MAX_WEAP_BANKS]; // remember which weapon was last selected in a bank for 'weaponbank' commands //----(SA) added +// JPW FIXME NOTE: max_weap_banks > max_weap_banks_mp so this should be OK, but if that changes, change this too + + // status bar head + float headYaw; + float headEndPitch; + float headEndYaw; + int headEndTime; + float headStartPitch; + float headStartYaw; + int headStartTime; + + // view movement + float v_dmg_time; + float v_dmg_pitch; + float v_dmg_roll; + + vec3_t kick_angles; // weapon kicks + vec3_t kick_origin; + + // RF, view flames when getting burnt + int v_fireTime, v_noFireTime; + vec3_t v_fireRiseDir; + + // temp working variables for player view + float bobfracsin; + int bobcycle; + float xyspeed; + int nextOrbitTime; + + // development tool + refEntity_t testModelEntity; + char testModelName[MAX_QPATH]; + qboolean testGun; + + // RF, new kick angles + vec3_t kickAVel; // for damage feedback, weapon recoil, etc + // This is the angular velocity, to give a smooth + // rotational feedback, rather than sudden jerks + vec3_t kickAngles; // for damage feedback, weapon recoil, etc + // NOTE: this is not transmitted through MSG.C stream + // since weapon kicks are client-side, and damage feedback + // is rare enough that we can transmit that as an event + float recoilPitch, recoilPitchAngle; + + // Duffy + qboolean cameraMode; // if rendering from a camera + // Duffy end + + unsigned int cld; // NERVE - SMF + qboolean limboMenu; // NERVE - SMF + + // NERVE - SMF - Objective info display + int oidTeam; + int oidPrintTime; + int oidPrintCharWidth; + int oidPrintY; + char oidPrint[1024]; + int oidPrintLines; + // -NERVE - SMF + + cameraShake_t cameraShake[MAX_CAMERA_SHAKE]; + float cameraShakePhase; + vec3_t cameraShakeAngles; + + float rumbleScale; //RUMBLE FX using new shakeCamera code + +} cg_t; + +#define NUM_FUNNEL_SPRITES 21 +#define MAX_LOCKER_DEBRIS 5 + +// all of the model, shader, and sound references that are +// loaded at gamestate time are stored in cgMedia_t +// Other media that can be tied to clients, weapons, or items are +// stored in the clientInfo_t, itemInfo_t, weaponInfo_t, and powerupInfo_t +typedef struct { + qhandle_t charsetShader; + // JOSEPH 4-17-00 + qhandle_t menucharsetShader; + // END JOSEPH + qhandle_t charsetProp; + qhandle_t charsetPropGlow; + qhandle_t charsetPropB; + qhandle_t whiteShader; + + qhandle_t redFlagModel; + qhandle_t blueFlagModel; + + qhandle_t armorModel; + + qhandle_t teamStatusBar; + + qhandle_t deferShader; + + // gib explosions + qhandle_t gibAbdomen; + qhandle_t gibArm; + qhandle_t gibChest; + qhandle_t gibFist; + qhandle_t gibFoot; + qhandle_t gibForearm; + qhandle_t gibIntestine; + qhandle_t gibLeg; + qhandle_t gibSkull; + qhandle_t gibBrain; + + // debris + qhandle_t debBlock[6]; + qhandle_t debRock[3]; + qhandle_t debFabric[3]; + qhandle_t debWood[6]; + + qhandle_t targetEffectExplosionShader; + + qhandle_t machinegunBrassModel; + qhandle_t panzerfaustBrassModel; //----(SA) added + + // Rafael + qhandle_t smallgunBrassModel; + + qhandle_t shotgunBrassModel; + + qhandle_t railRingsShader; + qhandle_t railCoreShader; + + qhandle_t lightningShader; + + qhandle_t friendShader; + +// qhandle_t medicReviveShader; //----(SA) commented out from MP + qhandle_t balloonShader; + qhandle_t connectionShader; + + qhandle_t aiStateShaders[MAX_AISTATES]; + + qhandle_t selectShader; + qhandle_t viewBloodShader; + qhandle_t tracerShader; + qhandle_t crosshairShader[NUM_CROSSHAIRS]; + qhandle_t crosshairFriendly; //----(SA) added + qhandle_t lagometerShader; + qhandle_t backTileShader; + qhandle_t noammoShader; + + qhandle_t reticleShader; +// qhandle_t reticleShaderSimple; + qhandle_t reticleShaderSimpleQ; +// qhandle_t snooperShader; + qhandle_t snooperShaderSimple; +// qhandle_t binocShaderSimple; + qhandle_t binocShaderSimpleQ; // same as above, but quartered. (trying to save texture space) + + qhandle_t smokePuffShader; + qhandle_t smokePuffRageProShader; + qhandle_t shotgunSmokePuffShader; + qhandle_t waterBubbleShader; + qhandle_t bloodTrailShader; + + qhandle_t nailPuffShader; + +//----(SA) modified + + // cursor hints + // would be nice to specify these in the menu scripts instead of permanent handles... + qhandle_t hintShaders[HINT_NUM_HINTS]; + + qhandle_t youGotMailShader; // '!' - new entry in notebook + qhandle_t youGotObjectiveShader; // ' - you completed objective +//----(SA) end + + // Rafael + qhandle_t snowShader; + qhandle_t oilParticle; + qhandle_t oilSlick; + // done. + + // Rafael - cannon + qhandle_t smokePuffShaderdirty; + qhandle_t smokePuffShaderb1; + qhandle_t smokePuffShaderb2; + qhandle_t smokePuffShaderb3; + qhandle_t smokePuffShaderb4; + qhandle_t smokePuffShaderb5; + // done + + // Rafael - blood pool + qhandle_t bloodPool; + + // Ridah, viewscreen blood animation + qhandle_t viewBloodAni[5]; + qhandle_t viewFlashBlood; + qhandle_t viewFlashFire[16]; + // done + + // Rafael bats + qhandle_t bats[10]; + // done + + // Rafael shards + qhandle_t shardGlass1; + qhandle_t shardGlass2; + qhandle_t shardWood1; + qhandle_t shardWood2; + qhandle_t shardMetal1; + qhandle_t shardMetal2; + qhandle_t shardCeramic1; + qhandle_t shardCeramic2; + // done + + qhandle_t shardRubble1; + qhandle_t shardRubble2; + qhandle_t shardRubble3; + + + qhandle_t shardJunk[MAX_LOCKER_DEBRIS]; + + qhandle_t numberShaders[11]; + + qhandle_t shadowMarkShader; + qhandle_t shadowFootShader; + qhandle_t shadowTorsoShader; + + qhandle_t botSkillShaders[5]; + + // wall mark shaders + qhandle_t wakeMarkShader; + qhandle_t wakeMarkShaderAnim; + qhandle_t bloodMarkShaders[5]; + qhandle_t bloodDotShaders[5]; + qhandle_t bulletMarkShader; + qhandle_t bulletMarkShaderMetal; + qhandle_t bulletMarkShaderWood; + qhandle_t bulletMarkShaderCeramic; + qhandle_t bulletMarkShaderGlass; + qhandle_t burnMarkShader; + qhandle_t holeMarkShader; + qhandle_t energyMarkShader; + + // powerup shaders + qhandle_t quadShader; + qhandle_t redQuadShader; + qhandle_t quadWeaponShader; + qhandle_t invisShader; + qhandle_t regenShader; + qhandle_t battleSuitShader; + qhandle_t battleWeaponShader; + qhandle_t hastePuffShader; + + // weapon effect models + qhandle_t spearModel; //----(SA) + + qhandle_t bulletFlashModel; + qhandle_t ringFlashModel; + qhandle_t dishFlashModel; + qhandle_t lightningExplosionModel; + + qhandle_t zombieLoogie; + qhandle_t flamebarrel; + qhandle_t mg42muzzleflash; + //qhandle_t mg42muzzleflashgg; + qhandle_t planemuzzleflash; + + // Rafael + qhandle_t crowbar; + + qhandle_t waterSplashModel; + qhandle_t waterSplashShader; + + qhandle_t thirdPersonBinocModel; //----(SA) added + qhandle_t cigModel; //----(SA) added + + qhandle_t batModel; + qhandle_t spiritSkullModel; + qhandle_t helgaGhostModel; + + // weapon effect shaders + qhandle_t railExplosionShader; + qhandle_t bulletExplosionShader; + qhandle_t rocketExplosionShader; + qhandle_t grenadeExplosionShader; + qhandle_t bfgExplosionShader; + qhandle_t bloodExplosionShader; + + qhandle_t flameThrowerhitShader; + + // special effects models + qhandle_t teleportEffectModel; + qhandle_t teleportEffectShader; + + // scoreboard headers + qhandle_t scoreboardName; + qhandle_t scoreboardPing; + qhandle_t scoreboardScore; + qhandle_t scoreboardTime; + // Ridah + qhandle_t bloodCloudShader; + qhandle_t sparkParticleShader; + qhandle_t smokeTrailShader; + qhandle_t fireTrailShader; + qhandle_t lightningBoltShader; + qhandle_t lightningBoltShaderGreen; + qhandle_t flamethrowerFireStream; + qhandle_t flamethrowerBlueStream; + qhandle_t flamethrowerFuelStream; + qhandle_t flamethrowerFuelShader; + qhandle_t onFireShader, onFireShader2; + //qhandle_t dripWetShader, dripWetShader2; + qhandle_t viewFadeBlack; + qhandle_t sparkFlareShader; + qhandle_t funnelFireShader[NUM_FUNNEL_SPRITES]; + + qhandle_t spotLightShader; + qhandle_t spotLightBeamShader; + qhandle_t spotLightBaseModel; //----(SA) added + qhandle_t spotLightLightModel; //----(SA) added + qhandle_t spotLightLightModelBroke; //----(SA) added + + qhandle_t lightningHitWallShader; + qhandle_t lightningWaveShader; + qhandle_t bulletParticleTrailShader; + qhandle_t smokeParticleShader; + + // DHM - Nerve :: bullet hitting dirt + qhandle_t dirtParticle1Shader; + qhandle_t dirtParticle2Shader; + qhandle_t dirtParticle3Shader; + + qhandle_t zombieSpiritWallShader; + qhandle_t zombieSpiritTrailShader; + qhandle_t zombieSpiritSkullShader; + qhandle_t zombieDeathDustShader; + qhandle_t zombieBodyFadeShader; + qhandle_t zombieHeadFadeShader; + + qhandle_t helgaSpiritSkullShader; + qhandle_t helgaSpiritTrailShader; + + qhandle_t ssSpiritSkullModel; + + qhandle_t skeletonSkinShader; + qhandle_t skeletonLegsModel; + qhandle_t skeletonTorsoModel; + qhandle_t skeletonHeadModel; + qhandle_t skeletonLegsSkin; + qhandle_t skeletonTorsoSkin; + qhandle_t skeletonHeadSkin; + + qhandle_t loperGroundChargeShader; + + qhandle_t teslaDamageEffectShader; + qhandle_t teslaAltDamageEffectShader; + qhandle_t viewTeslaDamageEffectShader; + qhandle_t viewTeslaAltDamageEffectShader; + // done. + +//----(SA) + // proto/super/heini armor parts + qhandle_t protoArmor[9 * 3]; // 9 parts, 3 sections each (nodam, dam1, dam2) + qhandle_t superArmor[16 * 3]; // 14 parts, 3 sections each + qhandle_t heinrichArmor[22 * 3]; // 20 parts, 3 sections each +//----(SA) end + + // medals shown during gameplay + qhandle_t medalImpressive; + qhandle_t medalExcellent; + qhandle_t medalGauntlet; + + // sounds + sfxHandle_t n_health; + sfxHandle_t noFireUnderwater; + sfxHandle_t snipersound; + sfxHandle_t quadSound; + sfxHandle_t tracerSound; + sfxHandle_t selectSound; + sfxHandle_t useNothingSound; + sfxHandle_t wearOffSound; + sfxHandle_t footsteps[FOOTSTEP_TOTAL][4]; + sfxHandle_t sfx_lghit1; + sfxHandle_t sfx_lghit2; + sfxHandle_t sfx_lghit3; + sfxHandle_t sfx_ric1; + sfxHandle_t sfx_ric2; + sfxHandle_t sfx_ric3; + sfxHandle_t sfx_railg; + sfxHandle_t sfx_rockexp; + sfxHandle_t sfx_dynamiteexp; + sfxHandle_t sfx_dynamiteexpDist; //----(SA) added + sfxHandle_t sfx_spearhit; + sfxHandle_t sfx_knifehit[5]; + sfxHandle_t sfx_bullet_metalhit[3]; + sfxHandle_t sfx_bullet_woodhit[3]; + sfxHandle_t sfx_bullet_roofhit[3]; + sfxHandle_t sfx_bullet_ceramichit[3]; + sfxHandle_t sfx_bullet_glasshit[3]; + sfxHandle_t gibSound; + sfxHandle_t gibBounce1Sound; + sfxHandle_t gibBounce2Sound; + sfxHandle_t gibBounce3Sound; + sfxHandle_t teleInSound; + sfxHandle_t teleOutSound; + sfxHandle_t noAmmoSound; + sfxHandle_t respawnSound; + sfxHandle_t talkSound; + sfxHandle_t landSound; + sfxHandle_t fallSound; + sfxHandle_t jumpPadSound; + + sfxHandle_t oneMinuteSound; + sfxHandle_t fiveMinuteSound; + sfxHandle_t suddenDeathSound; + + sfxHandle_t threeFragSound; + sfxHandle_t twoFragSound; + sfxHandle_t oneFragSound; + + sfxHandle_t hitSound; + sfxHandle_t hitTeamSound; + sfxHandle_t impressiveSound; + sfxHandle_t excellentSound; + sfxHandle_t deniedSound; + sfxHandle_t humiliationSound; + + sfxHandle_t takenLeadSound; + sfxHandle_t tiedLeadSound; + sfxHandle_t lostLeadSound; + + sfxHandle_t watrInSound; + sfxHandle_t watrOutSound; + sfxHandle_t watrUnSound; + +// sfxHandle_t flightSound; + sfxHandle_t underWaterSound; + sfxHandle_t medkitSound; + sfxHandle_t wineSound; + sfxHandle_t bookSound; //----(SA) added + sfxHandle_t staminaSound; //----(SA) added + sfxHandle_t elecSound; + sfxHandle_t fireSound; + sfxHandle_t waterSound; + + // teamplay sounds + sfxHandle_t redLeadsSound; + sfxHandle_t blueLeadsSound; + sfxHandle_t teamsTiedSound; + + // tournament sounds + sfxHandle_t count3Sound; + sfxHandle_t count2Sound; + sfxHandle_t count1Sound; + sfxHandle_t countFightSound; + sfxHandle_t countPrepareSound; + + //----(SA) added + sfxHandle_t debBounce1Sound; + sfxHandle_t debBounce2Sound; + sfxHandle_t debBounce3Sound; + //----(SA) end + + //----(SA) added + sfxHandle_t grenadePulseSound4; + sfxHandle_t grenadePulseSound3; + sfxHandle_t grenadePulseSound2; + sfxHandle_t grenadePulseSound1; + //----(SA) + +//----(SA) added + sfxHandle_t sparkSounds[2]; +//----(SA) + + // Ridah + sfxHandle_t flameSound; + sfxHandle_t flameBlowSound; + sfxHandle_t flameStartSound; + sfxHandle_t flameStreamSound; + sfxHandle_t lightningSounds[3]; + sfxHandle_t lightningZap; + sfxHandle_t flameCrackSound; + sfxHandle_t boneBounceSound; + + sfxHandle_t zombieSpiritSound; + sfxHandle_t zombieSpiritLoopSound; + sfxHandle_t zombieDeathSound; + + sfxHandle_t helgaSpiritLoopSound; + sfxHandle_t helgaSpiritSound; + sfxHandle_t helgaGaspSound; + + sfxHandle_t heinrichArmorBreak; //----(SA) + sfxHandle_t protoArmorBreak; //----(SA) + sfxHandle_t superArmorBreak; //----(SA) + + + sfxHandle_t debrisHitSound; + + sfxHandle_t loperLightningSounds[3]; + sfxHandle_t loperLightningZap; + + sfxHandle_t lightningClap[5]; + + sfxHandle_t batsFlyingLoopSound; + +// sfxHandle_t grenadebounce1; +// sfxHandle_t grenadebounce2; + sfxHandle_t grenadebounce[GRENBOUNCE_TOTAL][2]; //----(SA) modified + + sfxHandle_t dynamitebounce1; //----(SA) added + + sfxHandle_t fbarrelexp1; + sfxHandle_t fbarrelexp2; + + sfxHandle_t fkickwall; + sfxHandle_t fkickflesh; + sfxHandle_t fkickmiss; + + int bulletHitFleshScript; + int bulletHitFleshMetalScript; + + int teslaZapScript; + sfxHandle_t teslaLoopSound; + // done. + + qhandle_t cursor; + qhandle_t selectCursor; + qhandle_t sizeCursor; + +} cgMedia_t; + + +// +// SOUND SCRIPTING +// + +typedef struct soundScriptSound_s +{ + char filename[MAX_QPATH]; + sfxHandle_t sfxHandle; + int lastPlayed; + + struct soundScriptSound_s *next; +} soundScriptSound_t; + + +#define MAX_SOUND_SCRIPT_SOUNDS 8192 +extern soundScriptSound_t soundScriptSounds[MAX_SOUND_SCRIPT_SOUNDS]; +//DAJ defined in cg_sound.c int numSoundScriptSounds; + + +typedef struct soundScript_s +{ + int index; + char name[MAX_QPATH]; + int channel; + int attenuation; + qboolean streaming; + qboolean looping; + float shakeScale; + float shakeRadius; + int shakeDuration; + qboolean random; // TODO + int numSounds; + soundScriptSound_t *soundList; // pointer into the global list of soundScriptSounds (defined below) + + struct soundScript_s *nextHash; // next soundScript in our hashTable list position +} soundScript_t; + +// we have to define these static lists, since we can't alloc memory within the cgame + +#define FILE_HASH_SIZE 1024 +extern soundScript_t* hashTable[FILE_HASH_SIZE]; + +#define MAX_SOUND_SCRIPTS 4096 +extern soundScript_t soundScripts[MAX_SOUND_SCRIPTS]; +//DAJ defined in cg_sound.c int numSoundScripts; + +extern soundScript_t soundScripts[MAX_SOUND_SCRIPTS]; + + + + + +// The client game static (cgs) structure hold everything +// loaded or calculated from the gamestate. It will NOT +// be cleared when a tournement restart is done, allowing +// all clients to begin playing instantly +typedef struct { + gameState_t gameState; // gamestate from server + glconfig_t glconfig; // rendering configuration + float screenXScale; // derived from glconfig + float screenYScale; + float screenXBias; + + int serverCommandSequence; // reliable command stream counter + int processedSnapshotNum; // the number of snapshots cgame has requested + + qboolean localServer; // detected on startup by checking sv_running + + // parsed from serverinfo + gametype_t gametype; + + // Rafael gameskill + gameskill_t gameskill; + // done + + int dmflags; + int teamflags; + int fraglimit; + int capturelimit; + int timelimit; + int maxclients; + char mapname[MAX_QPATH]; + char redTeam[MAX_QPATH]; // A team + char blueTeam[MAX_QPATH]; // B team + + int voteTime; + int voteYes; + int voteNo; + qboolean voteModified; // beep whenever changed + char voteString[MAX_STRING_TOKENS]; + + int teamVoteTime[2]; + int teamVoteYes[2]; + int teamVoteNo[2]; + qboolean teamVoteModified[2]; // beep whenever changed + char teamVoteString[2][MAX_STRING_TOKENS]; + + int levelStartTime; + + int scores1, scores2; // from configstrings + + // + // locally derived information from gamestate + // + qhandle_t gameModels[MAX_MODELS]; + + sfxHandle_t gameSounds[MAX_SOUNDS]; + int gameSoundTypes[MAX_SOUNDS]; //----(SA) added + + int numInlineModels; + qhandle_t inlineDrawModel[MAX_MODELS]; + vec3_t inlineModelMidpoints[MAX_MODELS]; + + clientInfo_t clientinfo[MAX_CLIENTS]; + + // teamchat width is *3 because of embedded color codes + char teamChatMsgs[TEAMCHAT_HEIGHT][TEAMCHAT_WIDTH * 3 + 1]; + int teamChatMsgTimes[TEAMCHAT_HEIGHT]; + int teamChatPos; + int teamLastChatPos; + + char itemPrintNames[MAX_ITEMS][32]; //----(SA) added + + int cursorX; + int cursorY; + qboolean eventHandling; + qboolean mouseCaptured; + qboolean sizingHud; + void *capturedItem; + qhandle_t activeCursor; + + // screen fading + //----(SA) modified just in name so global searching is easier to narrow down (added 'scrF') + float scrFadeAlpha, scrFadeAlphaCurrent; + int scrFadeStartTime; + int scrFadeDuration; + + // media + cgMedia_t media; + + // player/AI model scripting (client repository) + animScriptData_t animScriptData; + +} cgs_t; + +//============================================================================== + +extern cgs_t cgs; +extern cg_t cg; +extern centity_t cg_entities[MAX_GENTITIES]; +extern weaponInfo_t cg_weapons[MAX_WEAPONS]; +extern itemInfo_t cg_items[MAX_ITEMS]; +extern markPoly_t cg_markPolys[MAX_MARK_POLYS]; + +extern vmCvar_t cg_centertime; +extern vmCvar_t cg_runpitch; +extern vmCvar_t cg_runroll; +extern vmCvar_t cg_bobup; +extern vmCvar_t cg_bobpitch; +extern vmCvar_t cg_bobroll; +extern vmCvar_t cg_swingSpeed; +extern vmCvar_t cg_shadows; +extern vmCvar_t cg_gibs; +extern vmCvar_t cg_drawTimer; +extern vmCvar_t cg_drawFPS; +extern vmCvar_t cg_drawSnapshot; +extern vmCvar_t cg_draw3dIcons; +extern vmCvar_t cg_drawIcons; +extern vmCvar_t cg_youGotMail; //----(SA) added +extern vmCvar_t cg_drawAmmoWarning; +extern vmCvar_t cg_drawCrosshair; +extern vmCvar_t cg_drawCrosshairNames; +extern vmCvar_t cg_drawCrosshairPickups; +extern vmCvar_t cg_hudAlpha; +extern vmCvar_t cg_useWeapsForZoom; +extern vmCvar_t cg_weaponCycleDelay; //----(SA) added +extern vmCvar_t cg_cycleAllWeaps; +extern vmCvar_t cg_drawAllWeaps; +extern vmCvar_t cg_drawRewards; +extern vmCvar_t cg_drawTeamOverlay; +extern vmCvar_t cg_crosshairX; +extern vmCvar_t cg_crosshairY; +extern vmCvar_t cg_crosshairSize; +extern vmCvar_t cg_crosshairAlpha; //----(SA) added +extern vmCvar_t cg_crosshairHealth; +extern vmCvar_t cg_drawStatus; +extern vmCvar_t cg_draw2D; +extern vmCvar_t cg_drawFrags; +extern vmCvar_t cg_animSpeed; +extern vmCvar_t cg_debugAnim; +extern vmCvar_t cg_debugPosition; +extern vmCvar_t cg_debugEvents; +extern vmCvar_t cg_drawSpreadScale; +extern vmCvar_t cg_railTrailTime; +extern vmCvar_t cg_errorDecay; +extern vmCvar_t cg_nopredict; +extern vmCvar_t cg_noPlayerAnims; +extern vmCvar_t cg_showmiss; +extern vmCvar_t cg_footsteps; +extern vmCvar_t cg_markTime; +extern vmCvar_t cg_brassTime; +extern vmCvar_t cg_gun_frame; +extern vmCvar_t cg_gun_x; +extern vmCvar_t cg_gun_y; +extern vmCvar_t cg_gun_z; +extern vmCvar_t cg_drawGun; +extern vmCvar_t cg_drawFPGun; +extern vmCvar_t cg_drawGamemodels; +extern vmCvar_t cg_cursorHints; +extern vmCvar_t cg_hintFadeTime; //----(SA) added +extern vmCvar_t cg_viewsize; +extern vmCvar_t cg_letterbox; //----(SA) added +extern vmCvar_t cg_tracerChance; +extern vmCvar_t cg_tracerWidth; +extern vmCvar_t cg_tracerLength; +extern vmCvar_t cg_tracerSpeed; +extern vmCvar_t cg_autoswitch; +extern vmCvar_t cg_ignore; +extern vmCvar_t cg_simpleItems; +extern vmCvar_t cg_fov; +extern vmCvar_t cg_zoomFov; +extern vmCvar_t cg_zoomDefaultBinoc; +extern vmCvar_t cg_zoomDefaultSniper; +extern vmCvar_t cg_zoomDefaultFG; +extern vmCvar_t cg_zoomDefaultSnooper; +extern vmCvar_t cg_zoomStepBinoc; +extern vmCvar_t cg_zoomStepSniper; +extern vmCvar_t cg_zoomStepSnooper; +extern vmCvar_t cg_zoomStepFG; +extern vmCvar_t cg_reticles; +extern vmCvar_t cg_reticleBrightness; +extern vmCvar_t cg_thirdPersonRange; +extern vmCvar_t cg_thirdPersonAngle; +extern vmCvar_t cg_thirdPerson; +extern vmCvar_t cg_stereoSeparation; +extern vmCvar_t cg_lagometer; +extern vmCvar_t cg_drawAttacker; +extern vmCvar_t cg_synchronousClients; +extern vmCvar_t cg_teamChatTime; +extern vmCvar_t cg_teamChatHeight; +extern vmCvar_t cg_stats; +extern vmCvar_t cg_forceModel; +extern vmCvar_t cg_coronafardist; +extern vmCvar_t cg_coronas; +extern vmCvar_t cg_buildScript; +extern vmCvar_t cg_paused; +extern vmCvar_t cg_blood; +extern vmCvar_t cg_predictItems; +extern vmCvar_t cg_deferPlayers; +extern vmCvar_t cg_teamChatsOnly; +extern vmCvar_t cg_enableBreath; +extern vmCvar_t cg_autoactivate; +extern vmCvar_t cg_emptyswitch; +extern vmCvar_t cg_useSuggestedWeapons; //----(SA) added +extern vmCvar_t cg_particleDist; +extern vmCvar_t cg_particleLOD; +extern vmCvar_t cg_smoothClients; +extern vmCvar_t pmove_fixed; +extern vmCvar_t pmove_msec; + +extern vmCvar_t cg_cameraOrbit; +extern vmCvar_t cg_cameraOrbitDelay; +extern vmCvar_t cg_timescaleFadeEnd; +extern vmCvar_t cg_timescaleFadeSpeed; +extern vmCvar_t cg_timescale; +extern vmCvar_t cg_cameraMode; +extern vmCvar_t cg_smallFont; +extern vmCvar_t cg_bigFont; + +extern vmCvar_t cg_blinktime; //----(SA) added + +extern vmCvar_t cg_currentSelectedPlayer; +extern vmCvar_t cg_currentSelectedPlayerName; + +// Rafael - particle switch +extern vmCvar_t cg_wolfparticles; +// done + +// Ridah +extern vmCvar_t cg_gameType; +extern vmCvar_t cg_bloodTime; +extern vmCvar_t cg_norender; +extern vmCvar_t cg_skybox; + +// Rafael gameskill +extern vmCvar_t cg_gameSkill; +// done + +extern vmCvar_t cg_reloading; //----(SA) added + +// JPW NERVE +extern vmCvar_t cg_medicChargeTime; +extern vmCvar_t cg_engineerChargeTime; +extern vmCvar_t cg_LTChargeTime; +extern vmCvar_t cg_soldierChargeTime; +extern vmCvar_t cg_redlimbotime; +extern vmCvar_t cg_bluelimbotime; +// jpw + +extern vmCvar_t cg_hunkUsed; +extern vmCvar_t cg_soundAdjust; +extern vmCvar_t cg_expectedhunkusage; + +extern vmCvar_t cg_showAIState; + +extern vmCvar_t cg_notebook; +extern vmCvar_t cg_notebookpages; // bitflags for the currently accessable pages. if they wanna cheat, let 'em. Most won't, or will wait 'til they actually play it. + +extern vmCvar_t cg_animState; +extern vmCvar_t cg_missionStats; +extern vmCvar_t cg_waitForFire; + +extern vmCvar_t cg_loadWeaponSelect; + +// NERVE - SMF - Wolf multiplayer configuration cvars +extern vmCvar_t mp_playerType; +extern vmCvar_t mp_weapon; +extern vmCvar_t mp_item1; +extern vmCvar_t mp_item2; +extern vmCvar_t mp_mapDesc; +extern vmCvar_t mp_mapTitle; +// -NERVE - SMF + +// +// cg_main.c +// +const char *CG_ConfigString( int index ); +const char *CG_Argv( int arg ); + +void QDECL CG_Printf( const char *msg, ... ); +void QDECL CG_Error( const char *msg, ... ); + +void CG_StartMusic( void ); +void CG_QueueMusic( void ); //----(SA) added + +void CG_UpdateCvars( void ); + +int CG_CrosshairPlayer( void ); +int CG_LastAttacker( void ); +void CG_LoadMenus( const char *menuFile ); +void CG_KeyEvent( int key, qboolean down ); +void CG_MouseEvent( int x, int y ); +void CG_EventHandling( int type ); + +qboolean CG_GetTag( int clientNum, char *tagname, orientation_t * or ); +qboolean CG_GetWeaponTag( int clientNum, char *tagname, orientation_t * or ); + +// +// cg_view.c +// +void CG_TestModel_f( void ); +void CG_TestGun_f( void ); +void CG_TestModelNextFrame_f( void ); +void CG_TestModelPrevFrame_f( void ); +void CG_TestModelNextSkin_f( void ); +void CG_TestModelPrevSkin_f( void ); +void CG_ZoomDown_f( void ); +void CG_ZoomIn_f( void ); +void CG_ZoomOut_f( void ); +void CG_ZoomUp_f( void ); + +void CG_DrawActiveFrame( int serverTime, stereoFrame_t stereoView, qboolean demoPlayback ); + +void CG_Concussive( centity_t *cent ); +// +// cg_drawtools.c +// +void CG_AdjustFrom640( float *x, float *y, float *w, float *h ); +void CG_FillRect( float x, float y, float width, float height, const float *color ); +void CG_HorizontalPercentBar( float x, float y, float width, float height, float percent ); +void CG_DrawPic( float x, float y, float width, float height, qhandle_t hShader ); +void CG_FilledBar( float x, float y, float w, float h, const float *startColorIn, float *endColor, const float *bgColor, float frac, int flags ); +// JOSEPH 10-26-99 +void CG_DrawStretchPic( float x, float y, float width, float height, qhandle_t hShader ); +// END JOSEPH +void CG_DrawString( float x, float y, const char *string, + float charWidth, float charHeight, const float *modulate ); + + +void CG_DrawStringExt( int x, int y, const char *string, const float *setColor, + qboolean forceColor, qboolean shadow, int charWidth, int charHeight, int maxChars ); +// JOSEPH 4-17-00 +void CG_DrawStringExt2( int x, int y, const char *string, const float *setColor, + qboolean forceColor, qboolean shadow, int charWidth, int charHeight, int maxChars ); +// END JOSEPH +void CG_DrawBigString( int x, int y, const char *s, float alpha ); +void CG_DrawBigStringColor( int x, int y, const char *s, vec4_t color ); +void CG_DrawSmallString( int x, int y, const char *s, float alpha ); +void CG_DrawSmallStringColor( int x, int y, const char *s, vec4_t color ); +// JOSEPH 4-25-00 +void CG_DrawBigString2( int x, int y, const char *s, float alpha ); +void CG_DrawBigStringColor2( int x, int y, const char *s, vec4_t color ); +// END JOSEPH +int CG_DrawStrlen( const char *str ); + +float *CG_FadeColor( int startMsec, int totalMsec ); +float *CG_TeamColor( int team ); +void CG_TileClear( void ); +void CG_ColorForHealth( vec4_t hcolor ); +void CG_GetColorForHealth( int health, int armor, vec4_t hcolor ); + +void UI_DrawProportionalString( int x, int y, const char* str, int style, vec4_t color ); + +// new hud stuff +void CG_DrawRect( float x, float y, float width, float height, float size, const float *color ); +void CG_DrawSides( float x, float y, float w, float h, float size ); +void CG_DrawTopBottom( float x, float y, float w, float h, float size ); + + + +// +// cg_draw.c, cg_newDraw.c +// +extern int sortedTeamPlayers[TEAM_MAXOVERLAY]; +extern int numSortedTeamPlayers; +extern int drawTeamOverlayModificationCount; +extern char systemChat[256]; +extern char teamChat1[256]; +extern char teamChat2[256]; + +void CG_AddLagometerFrameInfo( void ); +void CG_AddLagometerSnapshotInfo( snapshot_t *snap ); +void CG_CenterPrint( const char *str, int y, int charWidth ); +void CG_ObjectivePrint( const char *str, int charWidth, int team ); // NERVE - SMF +void CG_DrawHead( float x, float y, float w, float h, int clientNum, vec3_t headAngles ); +void CG_DrawActive( stereoFrame_t stereoView ); +void CG_DrawFlagModel( float x, float y, float w, float h, int team ); + +void CG_DrawTeamBackground( int x, int y, int w, int h, float alpha, int team ); +void CG_OwnerDraw( float x, float y, float w, float h, float text_x, float text_y, int ownerDraw, int ownerDrawFlags, int align, float special, int font, float scale, vec4_t color, qhandle_t shader, int textStyle ); +void CG_Text_Paint( float x, float y, int font, float scale, vec4_t color, const char *text, float adjust, int limit, int style ); //----(SA) modified +int CG_Text_Width( const char *text, int font, float scale, int limit ); +int CG_Text_Height( const char *text, int font, float scale, int limit ); +void CG_SelectPrevPlayer(); +void CG_SelectNextPlayer(); +float CG_GetValue( int ownerDraw, int type ); // 'type' is relative or absolute (fractional-'0.5' or absolute- '50' health) +qboolean CG_OwnerDrawVisible( int flags ); +void CG_RunMenuScript( char **args ); +void CG_ShowResponseHead(); +void CG_SetPrintString( int type, const char *p ); +void CG_InitTeamChat(); +void CG_GetTeamColor( vec4_t *color ); +const char *CG_GetGameStatusText(); +const char *CG_GetKillerText(); +void CG_Draw3DModel( float x, float y, float w, float h, qhandle_t model, qhandle_t skin, vec3_t origin, vec3_t angles ); +void CG_Text_PaintChar( float x, float y, float width, float height, float scale, float s, float t, float s2, float t2, qhandle_t hShader ); +void CG_CheckOrderPending(); +const char *CG_GameTypeString(); +qboolean CG_YourTeamHasFlag(); +qboolean CG_OtherTeamHasFlag(); +qhandle_t CG_StatusHandle( int task ); +void CG_Fade( int r, int g, int b, int a, int time, int duration ); //----(SA) modified + +void CG_CalcShakeCamera(); +void CG_ApplyShakeCamera(); + + + +// +// cg_player.c +// +qboolean CG_EntOnFire( centity_t *cent ); // Ridah +void CG_Player( centity_t *cent ); +void CG_ResetPlayerEntity( centity_t *cent ); +void CG_AddRefEntityWithPowerups( refEntity_t *ent, int powerups, int team, entityState_t *es, const vec3_t fireRiseDir ); +void CG_NewClientInfo( int clientNum ); +sfxHandle_t CG_CustomSound( int clientNum, const char *soundName ); + +// Rafael particles +extern qboolean initparticles; +int CG_NewParticleArea( int num ); + +// +// cg_predict.c +// +void CG_BuildSolidList( void ); +int CG_PointContents( const vec3_t point, int passEntityNum ); +void CG_Trace( trace_t *result, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, + int skipNumber, int mask ); +void CG_PredictPlayerState( void ); +void CG_LoadDeferredPlayers( void ); + + +// +// cg_events.c +// +void CG_CheckEvents( centity_t *cent ); +const char *CG_PlaceString( int rank ); +void CG_EntityEvent( centity_t *cent, vec3_t position ); +void CG_PainEvent( centity_t *cent, int health, qboolean crouching ); + + +// +// cg_ents.c +// +void CG_SetEntitySoundPosition( centity_t *cent ); +void CG_AddPacketEntities( void ); +void CG_Beam( centity_t *cent ); +void CG_AdjustPositionForMover( const vec3_t in, int moverNum, int fromTime, int toTime, vec3_t out, vec3_t outDeltaAngles ); + +void CG_PositionEntityOnTag( refEntity_t *entity, const refEntity_t *parent, + char *tagName, int startIndex, vec3_t *offset ); +void CG_PositionRotatedEntityOnTag( refEntity_t *entity, const refEntity_t *parent, char *tagName ); + + +//----(SA) +void CG_AttachedPartChange( centity_t *cent ); +void CG_NextItem_f( void ); +void CG_PrevItem_f( void ); +void CG_Item_f( void ); +//----(SA) end + + +// +// cg_weapons.c +// +void CG_LastWeaponUsed_f( void ); //----(SA) added +void CG_NextWeaponInBank_f( void ); //----(SA) added +void CG_PrevWeaponInBank_f( void ); //----(SA) added +void CG_AltWeapon_f( void ); +void CG_NextWeapon_f( void ); +void CG_PrevWeapon_f( void ); +void CG_Weapon_f( void ); +void CG_WeaponBank_f( void ); +void CG_WeaponSuggest( int weap ); + +void CG_FinishWeaponChange( int lastweap, int newweap ); + +void CG_RegisterWeapon( int weaponNum ); +void CG_RegisterItemVisuals( int itemNum ); + +void CG_FireWeapon( centity_t *cent ); //----(SA) modified. +//void CG_EndFireWeapon( centity_t *cent, int firemode ); //----(SA) added +void CG_MissileHitWall( int weapon, int clientNum, vec3_t origin, vec3_t dir, int surfaceFlags ); // (SA) modified to send missilehitwall surface parameters + +void CG_MissileHitWallSmall( int weapon, int clientNum, vec3_t origin, vec3_t dir ); +void CG_DrawTracer( vec3_t start, vec3_t finish ); + +// Rafael +void CG_MG42EFX( centity_t *cent ); + +void CG_FLAKEFX( centity_t *cent, int whichgun ); + +void CG_MortarEFX( centity_t *cent ); + +// Ridah +qboolean CG_MonsterUsingWeapon( centity_t *cent, int aiChar, int weaponNum ); + +// Rafael +void CG_MissileHitWall2( int weapon, int clientNum, vec3_t origin, vec3_t dir ); +// done + +void CG_MissileHitPlayer( centity_t *cent, int weapon, vec3_t origin, vec3_t dir, int entityNum ); +//----(SA) +void CG_VenomFire( entityState_t *es, qboolean fullmode ); +//----(SA) +void CG_Bullet( vec3_t origin, int sourceEntityNum, vec3_t normal, qboolean flesh, int fleshEntityNum, qboolean wolfkick, int otherEntNum2 ); + +void CG_RailTrail( clientInfo_t *ci, vec3_t start, vec3_t end, int type ); //----(SA) added 'type' +void CG_GrappleTrail( centity_t *ent, const weaponInfo_t *wi ); +void CG_AddViewWeapon( playerState_t *ps ); +void CG_AddPlayerWeapon( refEntity_t *parent, playerState_t *ps, centity_t *cent ); +void CG_DrawWeaponSelect( void ); +void CG_DrawHoldableSelect( void ); + +void CG_OutOfAmmoChange( void ); +void CG_HoldableUsedupChange( void ); //----(SA) added + +//----(SA) added to header to access from outside cg_weapons.c +void CG_AddDebris( vec3_t origin, vec3_t dir, int speed, int duration, int count ); +//----(SA) done + +void CG_ClientDamage( int entnum, int enemynum, int id ); + +void CG_AddBulletParticles( vec3_t origin, vec3_t dir, int speed, int duration, int count, float randScale ); + +// +// cg_marks.c +// +void CG_InitMarkPolys( void ); +void CG_AddMarks( void ); +void CG_ImpactMark( qhandle_t markShader, + const vec3_t origin, const vec3_t dir, + float orientation, + float r, float g, float b, float a, + qboolean alphaFade, + float radius, qboolean temporary, int duration ); + +// Rafael particles +// +// cg_particles.c +// +void CG_ClearParticles( void ); +void CG_AddParticles( void ); +void CG_ParticleSnow( qhandle_t pshader, vec3_t origin, vec3_t origin2, int turb, float range, int snum ); +void CG_ParticleSmoke( qhandle_t pshader, centity_t *cent ); +void CG_AddParticleShrapnel( localEntity_t *le ); +void CG_ParticleSnowFlurry( qhandle_t pshader, centity_t *cent ); +void CG_ParticleBulletDebris( vec3_t org, vec3_t vel, int duration ); +void CG_ParticleDirtBulletDebris( vec3_t org, vec3_t vel, int duration ); // DHM - Nerve +void CG_ParticleDirtBulletDebris_Core( vec3_t org, vec3_t vel, int duration, float width, float height, float alpha, char *shadername ); // NERVE - SMF // JPW addtnl params +void CG_ParticleSparks( vec3_t org, vec3_t vel, int duration, float x, float y, float speed ); +void CG_ParticleDust( centity_t *cent, vec3_t origin, vec3_t dir ); +void CG_ParticleMisc( qhandle_t pshader, vec3_t origin, int size, int duration, float alpha ); + +// Ridah +void CG_ParticleExplosion( char *animStr, vec3_t origin, vec3_t vel, int duration, int sizeStart, int sizeEnd ); + +// Rafael snow pvs check +void CG_SnowLink( centity_t *cent, qboolean particleOn ); +// done. + +// Rafael bats +void CG_ParticleBat( centity_t *cent ); +void CG_ParticleBats( qhandle_t pshader, centity_t *cent ); +void CG_BatsUpdatePosition( centity_t *cent ); +void CG_ParticleImpactSmokePuff( qhandle_t pshader, vec3_t origin ); +void CG_ParticleImpactSmokePuffExtended( qhandle_t pshader, vec3_t origin, vec3_t dir, int radius, int lifetime, int vel, int acc, int maxroll, float alpha ); // (SA) so I can add more parameters without screwing up the one that's there +void CG_Particle_Bleed( qhandle_t pshader, vec3_t start, vec3_t dir, int fleshEntityNum, int duration ); +void CG_GetBleedOrigin( vec3_t head_origin, vec3_t torso_origin, vec3_t legs_origin, int fleshEntityNum ); +void CG_Particle_OilParticle( qhandle_t pshader, vec3_t origin, vec3_t origin2, int ptime, int snum ); +void CG_Particle_OilSlick( qhandle_t pshader, centity_t *cent ); +void CG_OilSlickRemove( centity_t *cent ); +void CG_BloodPool( localEntity_t *le, qhandle_t pshader, trace_t *tr ); +void CG_ParticleBloodCloudZombie( centity_t *cent, vec3_t origin, vec3_t dir ); +void CG_ParticleBloodCloud( centity_t *cent, vec3_t origin, vec3_t dir ); +// done + +// Ridah, trails +// +// cg_trails.c +// +int CG_AddTrailJunc( int headJuncIndex, qhandle_t shader, int spawnTime, int sType, vec3_t pos, int trailLife, float alphaStart, float alphaEnd, float startWidth, float endWidth, int flags, vec3_t colorStart, vec3_t colorEnd, float sRatio, float animSpeed ); +int CG_AddSparkJunc( int headJuncIndex, qhandle_t shader, vec3_t pos, int trailLife, float alphaStart, float alphaEnd, float startWidth, float endWidth ); +int CG_AddSmokeJunc( int headJuncIndex, qhandle_t shader, vec3_t pos, int trailLife, float alpha, float startWidth, float endWidth ); +int CG_AddFireJunc( int headJuncIndex, qhandle_t shader, vec3_t pos, int trailLife, float alpha, float startWidth, float endWidth ); +void CG_AddTrails( void ); +void CG_ClearTrails( void ); +// done. + +// Ridah, sound scripting +int CG_SoundScriptPrecache( const char *name ); +qboolean CG_SoundPlaySoundScript( const char *name, vec3_t org, int entnum ); +void CG_SoundPlayIndexedScript( int index, vec3_t org, int entnum ); +void CG_SoundInit( void ); +// done. + +// Ridah, flamethrower +void CG_FireFlameChunks( centity_t *cent, vec3_t origin, vec3_t angles, float speedScale, qboolean firing, int flags ); //----(SA) added 'flags' +void CG_InitFlameChunks( void ); +void CG_AddFlameChunks( void ); +void CG_UpdateFlamethrowerSounds( void ); +void CG_FlameDamage( int owner, vec3_t org, float radius ); +// done. + +// +// cg_localents.c +// +void CG_InitLocalEntities( void ); +localEntity_t *CG_AllocLocalEntity( void ); +void CG_AddLocalEntities( void ); + +// +// cg_effects.c +// +int CG_GetOriginForTag( centity_t * cent, refEntity_t * parent, char *tagName, int startIndex, vec3_t org, vec3_t axis[3] ); +localEntity_t *CG_SmokePuff( const vec3_t p, + const vec3_t vel, + float radius, + float r, float g, float b, float a, + float duration, + int startTime, + int fadeInTime, + int leFlags, + qhandle_t hShader ); + +void CG_BubbleTrail( vec3_t start, vec3_t end, float size, float spacing ); +void CG_SpawnEffect( vec3_t org ); +void CG_GibPlayer( centity_t *cent, vec3_t playerOrigin, vec3_t gdir ); +void CG_LoseHat( centity_t *cent, vec3_t dir ); //----(SA) added +void CG_GibHead( vec3_t headOrigin ); + +void CG_Bleed( vec3_t origin, int entityNum ); + +localEntity_t *CG_MakeExplosion( vec3_t origin, vec3_t dir, + qhandle_t hModel, qhandle_t shader, int msec, + qboolean isSprite ); +// Ridah +void CG_DynamicLightningBolt( qhandle_t shader, vec3_t start, vec3_t pend, int numBolts, float maxWidth, qboolean fade, float startAlpha, int recursion, int randseed ); +void CG_SparklerSparks( vec3_t origin, int count ); +void CG_ClearFlameChunks( void ); +void CG_ProjectedSpotLight( vec3_t start, vec3_t dir ); +// done. + +//----(SA) +void CG_Spotlight( centity_t *cent, float *color, vec3_t start, vec3_t dir, int segs, float range, int startWidth, float coneAngle, int flags ); +#define SL_NOTRACE 0x001 // don't do a trace check for shortening the beam, always draw at full 'range' length +#define SL_NODLIGHT 0x002 // don't put a dlight at the end +#define SL_NOSTARTCAP 0x004 // dont' cap the start circle +#define SL_LOCKTRACETORANGE 0x010 // only trace out as far as the specified range (rather than to max spot range) +#define SL_NOFLARE 0x020 // don't draw a flare when the light is pointing at the camera +#define SL_NOIMPACT 0x040 // don't draw the impact mark +#define SL_LOCKUV 0x080 // lock the texture coordinates at the 'true' length of the requested beam. +#define SL_NOCORE 0x100 // don't draw the center 'core' beam +#define SL_TRACEWORLDONLY 0x200 +//----(SA) done + +void CG_RumbleEfx( float pitch, float yaw ); + +// +// cg_snapshot.c +// +void CG_ProcessSnapshots( void ); + +// +// cg_info.c +// +void CG_LoadingString( const char *s ); +void CG_LoadingItem( int itemNum ); +void CG_LoadingClient( int clientNum ); +void CG_DrawInformation( void ); +const char *CG_translateString( const char *str ); + +// +// cg_scoreboard.c +// +qboolean CG_DrawScoreboard( void ); +void CG_DrawTourneyScoreboard( void ); + +// +// cg_consolecmds.c +// +qboolean CG_ConsoleCommand( void ); +void CG_InitConsoleCommands( void ); + +// +// cg_servercmds.c +// +void CG_ExecuteNewServerCommands( int latestSequence ); +void CG_ParseServerinfo( void ); +void CG_SetConfigValues( void ); +void CG_ShaderStateChanged( void ); +void CG_SendMoveSpeed( animation_t *animList, int numAnims, char *modelName ); + +// +// cg_playerstate.c +// +void CG_Respawn( void ); +void CG_TransitionPlayerState( playerState_t *ps, playerState_t *ops ); +void CG_LoadClientInfo( clientInfo_t *ci ); + + +//=============================================== + +// +// system traps +// These functions are how the cgame communicates with the main game system +// + +// print message on the local console +void trap_Print( const char *fmt ); + +// abort the game +void trap_Error( const char *fmt ); + +// exit game to main menu (credits/etc) +void trap_Endgame( void ); //----(SA) added + +// milliseconds should only be used for performance tuning, never +// for anything game related. Get time from the CG_DrawActiveFrame parameter +int trap_Milliseconds( void ); + +// console variable interaction +void trap_Cvar_Register( vmCvar_t *vmCvar, const char *varName, const char *defaultValue, int flags ); +void trap_Cvar_Update( vmCvar_t *vmCvar ); +void trap_Cvar_Set( const char *var_name, const char *value ); +void trap_Cvar_VariableStringBuffer( const char *var_name, char *buffer, int bufsize ); + +// ServerCommand and ConsoleCommand parameter access +int trap_Argc( void ); +void trap_Argv( int n, char *buffer, int bufferLength ); +void trap_Args( char *buffer, int bufferLength ); + +// filesystem access +// returns length of file +int trap_FS_FOpenFile( const char *qpath, fileHandle_t *f, fsMode_t mode ); +void trap_FS_Read( void *buffer, int len, fileHandle_t f ); +void trap_FS_Write( const void *buffer, int len, fileHandle_t f ); +void trap_FS_FCloseFile( fileHandle_t f ); +void trap_FS_CopyFile( char *from, char *to ); //DAJ + +// add commands to the local console as if they were typed in +// for map changing, etc. The command is not executed immediately, +// but will be executed in order the next time console commands +// are processed +void trap_SendConsoleCommand( const char *text ); + +// register a command name so the console can perform command completion. +// FIXME: replace this with a normal console command "defineCommand"? +void trap_AddCommand( const char *cmdName ); + +// send a string to the server over the network +void trap_SendClientCommand( const char *s ); + +// force a screen update, only used during gamestate load +void trap_UpdateScreen( void ); + +// model collision +void trap_CM_LoadMap( const char *mapname ); +int trap_CM_NumInlineModels( void ); +clipHandle_t trap_CM_InlineModel( int index ); // 0 = world, 1+ = bmodels +clipHandle_t trap_CM_TempBoxModel( const vec3_t mins, const vec3_t maxs ); +clipHandle_t trap_CM_TempCapsuleModel( const vec3_t mins, const vec3_t maxs ); +int trap_CM_PointContents( const vec3_t p, clipHandle_t model ); +int trap_CM_TransformedPointContents( const vec3_t p, clipHandle_t model, const vec3_t origin, const vec3_t angles ); +void trap_CM_BoxTrace( trace_t *results, const vec3_t start, const vec3_t end, + const vec3_t mins, const vec3_t maxs, + clipHandle_t model, int brushmask ); +void trap_CM_TransformedBoxTrace( trace_t *results, const vec3_t start, const vec3_t end, + const vec3_t mins, const vec3_t maxs, + clipHandle_t model, int brushmask, + const vec3_t origin, const vec3_t angles ); + +void trap_CM_CapsuleTrace( trace_t *results, const vec3_t start, const vec3_t end, + const vec3_t mins, const vec3_t maxs, + clipHandle_t model, int brushmask ); +void trap_CM_TransformedCapsuleTrace( trace_t *results, const vec3_t start, const vec3_t end, + const vec3_t mins, const vec3_t maxs, + clipHandle_t model, int brushmask, + const vec3_t origin, const vec3_t angles ); + +// Returns the projection of a polygon onto the solid brushes in the world +int trap_CM_MarkFragments( int numPoints, const vec3_t *points, + const vec3_t projection, + int maxPoints, vec3_t pointBuffer, + int maxFragments, markFragment_t *fragmentBuffer ); + +// normal sounds will have their volume dynamically changed as their entity +// moves and the listener moves +void trap_S_StartSound( vec3_t origin, int entityNum, int entchannel, sfxHandle_t sfx ); +void trap_S_StartSoundEx( vec3_t origin, int entityNum, int entchannel, sfxHandle_t sfx, int flags ); +void trap_S_StopLoopingSound( int entnum ); + +void trap_S_StopStreamingSound( int entnum ); // usually AI. character is talking and needs to be shut up /now/ + +// a local sound is always played full volume +void trap_S_StartLocalSound( sfxHandle_t sfx, int channelNum ); +void trap_S_ClearLoopingSounds( qboolean killall ); +void trap_S_AddLoopingSound( int entityNum, const vec3_t origin, const vec3_t velocity, sfxHandle_t sfx, int volume ); +void trap_S_AddRangedLoopingSound( int entityNum, const vec3_t origin, const vec3_t velocity, sfxHandle_t sfx, int range ); +void trap_S_AddRealLoopingSound( int entityNum, const vec3_t origin, const vec3_t velocity, sfxHandle_t sfx ); +void trap_S_UpdateEntityPosition( int entityNum, const vec3_t origin ); + +// Ridah, talking animations +int trap_S_GetVoiceAmplitude( int entityNum ); +// done. + +// repatialize recalculates the volumes of sound as they should be heard by the +// given entityNum and position +void trap_S_Respatialize( int entityNum, const vec3_t origin, vec3_t axis[3], int inwater ); +sfxHandle_t trap_S_RegisterSound( const char *sample ); // returns buzz if not found +void trap_S_StartBackgroundTrack( const char *intro, const char *loop, int fadeupTime ); // empty name stops music +void trap_S_StopBackgroundTrack( void ); +void trap_S_FadeBackgroundTrack( float targetvol, int time, int sound ); //----(SA) added +void trap_S_StartStreamingSound( const char *intro, const char *loop, int entnum, int channel, int attenuation ); +void trap_S_FadeAllSound( float targetvol, int time ); //----(SA) added + +void trap_R_LoadWorldMap( const char *mapname ); + +// all media should be registered during level startup to prevent +// hitches during gameplay +qhandle_t trap_R_RegisterModel( const char *name ); // returns rgb axis if not found +qhandle_t trap_R_RegisterSkin( const char *name ); // returns all white if not found +qhandle_t trap_R_RegisterShader( const char *name ); // returns all white if not found +qhandle_t trap_R_RegisterShaderNoMip( const char *name ); // returns all white if not found + +qboolean trap_R_GetSkinModel( qhandle_t skinid, const char *type, char *name ); //----(SA) added +qhandle_t trap_R_GetShaderFromModel( qhandle_t modelid, int surfnum, int withlightmap ); //----(SA) added + +// a scene is built up by calls to R_ClearScene and the various R_Add functions. +// Nothing is drawn until R_RenderScene is called. +void trap_R_ClearScene( void ); +void trap_R_AddRefEntityToScene( const refEntity_t *re ); + +// polys are intended for simple wall marks, not really for doing +// significant construction +void trap_R_AddPolyToScene( qhandle_t hShader, int numVerts, const polyVert_t *verts ); +// Ridah +void trap_R_AddPolysToScene( qhandle_t hShader, int numVerts, const polyVert_t *verts, int numPolys ); +void trap_RB_ZombieFXAddNewHit( int entityNum, const vec3_t hitPos, const vec3_t hitDir ); +// done. +void trap_R_AddLightToScene( const vec3_t org, float intensity, float r, float g, float b, int overdraw ); +void trap_R_AddCoronaToScene( const vec3_t org, float r, float g, float b, float scale, int id, int flags ); //----(SA) modified +void trap_R_RenderScene( const refdef_t *fd ); +void trap_R_SetColor( const float *rgba ); // NULL = 1,1,1,1 +void trap_R_DrawStretchPic( float x, float y, float w, float h, + float s1, float t1, float s2, float t2, qhandle_t hShader ); +void trap_R_DrawStretchPicGradient( float x, float y, float w, float h, + float s1, float t1, float s2, float t2, qhandle_t hShader, const float *gradientColor, int gradientType ); + +void trap_R_ModelBounds( clipHandle_t model, vec3_t mins, vec3_t maxs ); +int trap_R_LerpTag( orientation_t *tag, const refEntity_t *refent, const char *tagName, int startIndex ); +void trap_R_RemapShader( const char *oldShader, const char *newShader, const char *timeOffset ); + +//----(SA) +void trap_R_SetFog( int fogvar, int var1, int var2, float r, float g, float b, float density ); + +//----(SA) + +// The glconfig_t will not change during the life of a cgame. +// If it needs to change, the entire cgame will be restarted, because +// all the qhandle_t are then invalid. +void trap_GetGlconfig( glconfig_t *glconfig ); + +// the gamestate should be grabbed at startup, and whenever a +// configstring changes +void trap_GetGameState( gameState_t *gamestate ); + +// cgame will poll each frame to see if a newer snapshot has arrived +// that it is interested in. The time is returned seperately so that +// snapshot latency can be calculated. +void trap_GetCurrentSnapshotNumber( int *snapshotNumber, int *serverTime ); + +// a snapshot get can fail if the snapshot (or the entties it holds) is so +// old that it has fallen out of the client system queue +qboolean trap_GetSnapshot( int snapshotNumber, snapshot_t *snapshot ); + +// retrieve a text command from the server stream +// the current snapshot will hold the number of the most recent command +// qfalse can be returned if the client system handled the command +// argc() / argv() can be used to examine the parameters of the command +qboolean trap_GetServerCommand( int serverCommandNumber ); + +// returns the most recent command number that can be passed to GetUserCmd +// this will always be at least one higher than the number in the current +// snapshot, and it may be quite a few higher if it is a fast computer on +// a lagged connection +int trap_GetCurrentCmdNumber( void ); + +qboolean trap_GetUserCmd( int cmdNumber, usercmd_t *ucmd ); + +// used for the weapon/holdable select and zoom +void trap_SetUserCmdValue( int stateValue, int holdValue, float sensitivityScale, int cld ); // NERVE - SMF - added cld + +// aids for VM testing +void testPrintInt( char *string, int i ); +void testPrintFloat( char *string, float f ); + +int trap_MemoryRemaining( void ); +void trap_R_RegisterFont( const char *fontName, int pointSize, fontInfo_t *font ); +qboolean trap_Key_IsDown( int keynum ); +int trap_Key_GetCatcher( void ); +void trap_Key_SetCatcher( int catcher ); +int trap_Key_GetKey( const char *binding ); + +// RF +void trap_SendMoveSpeedsToGame( int entnum, char *movespeeds ); + +typedef enum { + SYSTEM_PRINT, + CHAT_PRINT, + TEAMCHAT_PRINT +} q3print_t; // bk001201 - warning: useless keyword or type name in empty declaration + +void trap_UI_Popup( const char *arg0 ); //----(SA) added +void trap_UI_ClosePopup( const char *arg0 ); // NERVE - SMF +void trap_UI_LimboChat( const char *arg0 ); // NERVE - SMF + +int trap_CIN_PlayCinematic( const char *arg0, int xpos, int ypos, int width, int height, int bits ); +e_status trap_CIN_StopCinematic( int handle ); +e_status trap_CIN_RunCinematic( int handle ); +void trap_CIN_DrawCinematic( int handle ); +void trap_CIN_SetExtents( int handle, int x, int y, int w, int h ); + +void trap_SnapVector( float *v ); + +qboolean trap_GetEntityToken( char *buffer, int bufferSize ); + +// Duffy, camera stuff +#define CAM_PRIMARY 0 // the main camera for cutscenes, etc. +qboolean trap_loadCamera( int camNum, const char *name ); +void trap_startCamera( int camNum, int time ); +void trap_stopCamera( int camNum ); //----(SA) added +qboolean trap_getCameraInfo( int camNum, int time, vec3_t *origin, vec3_t *angles, float *fov ); +void CG_StartCamera( const char *name, qboolean startBlack ); +void CG_StopCamera( void ); + +//----(SA) added +int CG_LoadCamera( const char *name ); +void CG_FreeCamera( int camNum ); +//----(SA) end + +void CG_StartShakeCamera( float p, int duration, vec3_t src, float radius ); + +qboolean trap_GetModelInfo( int clientNum, char *modelName, animModelInfo_t **modelInfo ); \ No newline at end of file diff --git a/src/cgame/cg_localents.c b/src/cgame/cg_localents.c new file mode 100644 index 0000000..77ff886 --- /dev/null +++ b/src/cgame/cg_localents.c @@ -0,0 +1,1775 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +// cg_localents.c -- every frame, generate renderer commands for locally +// processed entities, like smoke puffs, gibs, shells, etc. + +#include "cg_local.h" + +// Ridah, increased this +//#define MAX_LOCAL_ENTITIES 512 +#define MAX_LOCAL_ENTITIES 768 // renderer can only handle 1024 entities max, so we should avoid + // overwriting game entities +// done. + +localEntity_t cg_localEntities[MAX_LOCAL_ENTITIES]; +localEntity_t cg_activeLocalEntities; // double linked list +localEntity_t *cg_freeLocalEntities; // single linked list + +// Ridah, debugging +int localEntCount = 0; + +/* +=================== +CG_InitLocalEntities + +This is called at startup and for tournement restarts +=================== +*/ +void CG_InitLocalEntities( void ) { + int i; + + memset( cg_localEntities, 0, sizeof( cg_localEntities ) ); + cg_activeLocalEntities.next = &cg_activeLocalEntities; + cg_activeLocalEntities.prev = &cg_activeLocalEntities; + cg_freeLocalEntities = cg_localEntities; + for ( i = 0 ; i < MAX_LOCAL_ENTITIES - 1 ; i++ ) { + cg_localEntities[i].next = &cg_localEntities[i + 1]; + } + + // Ridah, debugging + localEntCount = 0; +} + + +/* +================== +CG_FreeLocalEntity +================== +*/ +void CG_FreeLocalEntity( localEntity_t *le ) { + if ( !le->prev ) { + CG_Error( "CG_FreeLocalEntity: not active" ); + } + + // Ridah, debugging + localEntCount--; +// trap_Print( va("FreeLocalEntity: locelEntCount = %d\n", localEntCount) ); + // done. + + // remove from the doubly linked active list + le->prev->next = le->next; + le->next->prev = le->prev; + + // the free list is only singly linked + le->next = cg_freeLocalEntities; + cg_freeLocalEntities = le; +} + +/* +=================== +CG_AllocLocalEntity + +Will allways succeed, even if it requires freeing an old active entity +=================== +*/ +localEntity_t *CG_AllocLocalEntity( void ) { + localEntity_t *le; + + if ( !cg_freeLocalEntities ) { + // no free entities, so free the one at the end of the chain + // remove the oldest active entity + CG_FreeLocalEntity( cg_activeLocalEntities.prev ); + } + + // Ridah, debugging + localEntCount++; +// trap_Print( va("AllocLocalEntity: locelEntCount = %d\n", localEntCount) ); + // done. + + le = cg_freeLocalEntities; + cg_freeLocalEntities = cg_freeLocalEntities->next; + + memset( le, 0, sizeof( *le ) ); + + // link into the active list + le->next = cg_activeLocalEntities.next; + le->prev = &cg_activeLocalEntities; + cg_activeLocalEntities.next->prev = le; + cg_activeLocalEntities.next = le; + return le; +} + + +/* +==================================================================================== + +FRAGMENT PROCESSING + +A fragment localentity interacts with the environment in some way (hitting walls), +or generates more localentities along a trail. + +==================================================================================== +*/ + +/* +================ +CG_BloodTrail + +Leave expanding blood puffs behind gibs +================ +*/ +// use this to change between particle and trail code +//#define BLOOD_PARTICLE_TRAIL +void CG_BloodTrail( localEntity_t *le ) { + int t; + int t2; + int step; + vec3_t newOrigin; + +#ifndef BLOOD_PARTICLE_TRAIL + static vec3_t col = {1,1,1}; +#endif + + centity_t *cent; + cent = &cg_entities[le->ownerNum]; + + if ( !cg_blood.integer ) { + return; + } + + // step = 150; +#ifdef BLOOD_PARTICLE_TRAIL + step = 10; +#else + // time it takes to move 3 units + step = ( 1000 * 3 ) / VectorLength( le->pos.trDelta ); +#endif + + if ( cent && cent->currentState.aiChar == AICHAR_ZOMBIE ) { + step = 30; + } + + t = step * ( ( cg.time - cg.frametime + step ) / step ); + t2 = step * ( cg.time / step ); + + for ( ; t <= t2; t += step ) { + BG_EvaluateTrajectory( &le->pos, t, newOrigin ); + +#ifdef BLOOD_PARTICLE_TRAIL + CG_Particle_Bleed( cgs.media.smokePuffShader, newOrigin, vec3_origin, 0, 500 + rand() % 200 ); +#else + + + if ( cent && cent->currentState.aiChar == AICHAR_ZOMBIE ) { + CG_Particle_Bleed( cgs.media.smokePuffShader, newOrigin, vec3_origin, 1, 500 + rand() % 200 ); + } else { + // Ridah, blood trail using trail code (should be faster since we don't have to spawn as many) + le->headJuncIndex = CG_AddTrailJunc( le->headJuncIndex, + cgs.media.bloodTrailShader, + t, + STYPE_STRETCH, + newOrigin, + 180, + 1.0, // start alpha + 0.0, // end alpha + 12.0, + 12.0, + TJFL_NOCULL, + col, col, + 0, 0 ); + } +#endif + + } +} + + +/* +================ +CG_FragmentBounceMark +================ +*/ +void CG_FragmentBounceMark( localEntity_t *le, trace_t *trace ) { + int radius; + + if ( le->leMarkType == LEMT_BLOOD ) { + static int lastBloodMark; + + // don't drop too many blood marks + if ( !( lastBloodMark > cg.time || lastBloodMark > cg.time - 100 ) ) { + radius = 16 + ( rand() & 31 ); + CG_ImpactMark( cgs.media.bloodDotShaders[rand() % 5], trace->endpos, trace->plane.normal, random() * 360, + 1,1,1,1, qtrue, radius, qfalse, cg_bloodTime.integer * 1000 ); + + lastBloodMark = cg.time; + } + } + + // don't allow a fragment to make multiple marks, or they + // pile up while settling + le->leMarkType = LEMT_NONE; +} + +/* +================ +CG_FragmentBounceSound +================ +*/ +void CG_FragmentBounceSound( localEntity_t *le, trace_t *trace ) { + if ( le->leBounceSoundType == LEBS_BLOOD ) { + // half the gibs will make splat sounds + if ( rand() & 1 ) { + int r = rand() & 3; + sfxHandle_t s; + + if ( r < 2 ) { + s = cgs.media.gibBounce1Sound; + } else if ( r == 2 ) { + s = cgs.media.gibBounce2Sound; + } else { + s = cgs.media.gibBounce3Sound; + } + trap_S_StartSound( trace->endpos, ENTITYNUM_WORLD, CHAN_AUTO, s ); + } + } else if ( le->leBounceSoundType == LEBS_BRASS ) { + +//----(SA) added + } else if ( le->leBounceSoundType == LEBS_ROCK ) { + // half the hits will make thunk sounds (this is just to start since we don't even have the sound yet... (SA)) + if ( rand() & 1 ) { + int r = rand() & 3; + sfxHandle_t s; + + if ( r < 2 ) { + s = cgs.media.debBounce1Sound; + } else if ( r == 2 ) { + s = cgs.media.debBounce2Sound; + } else { + s = cgs.media.debBounce3Sound; + } + trap_S_StartSound( trace->endpos, ENTITYNUM_WORLD, CHAN_AUTO, s ); + } +//----(SA) end + + } else if ( le->leBounceSoundType == LEBS_BONE ) { + + trap_S_StartSound( trace->endpos, ENTITYNUM_WORLD, CHAN_AUTO, cgs.media.boneBounceSound ); + + } + + // don't allow a fragment to make multiple bounce sounds, + // or it gets too noisy as they settle + le->leBounceSoundType = LEBS_NONE; +} + + +/* +================ +CG_ReflectVelocity +================ +*/ +void CG_ReflectVelocity( localEntity_t *le, trace_t *trace ) { + vec3_t velocity; + float dot; + int hitTime; + + // reflect the velocity on the trace plane + hitTime = cg.time - cg.frametime + cg.frametime * trace->fraction; + BG_EvaluateTrajectoryDelta( &le->pos, hitTime, velocity ); + dot = DotProduct( velocity, trace->plane.normal ); + VectorMA( velocity, -2 * dot, trace->plane.normal, le->pos.trDelta ); + + VectorScale( le->pos.trDelta, le->bounceFactor, le->pos.trDelta ); + + VectorCopy( trace->endpos, le->pos.trBase ); + le->pos.trTime = cg.time; + + + // check for stop, making sure that even on low FPS systems it doesn't bobble + + if ( le->leMarkType == LEMT_BLOOD && trace->startsolid ) { + //centity_t *cent; + //cent = &cg_entities[trace->entityNum]; + //if (cent && cent->currentState.apos.trType != TR_STATIONARY) + // le->pos.trType = TR_STATIONARY; + } else if ( trace->allsolid || + ( trace->plane.normal[2] > 0 && + ( le->pos.trDelta[2] < 40 || le->pos.trDelta[2] < -cg.frametime * le->pos.trDelta[2] ) ) ) { + +//----(SA) if it's a fragment and it's not resting on the world... +// if(le->leType == LE_DEBRIS && trace->entityNum < (MAX_ENTITIES - 1)) + if ( le->leType == LE_FRAGMENT && trace->entityNum < ( MAX_ENTITIES - 1 ) ) { + le->pos.trType = TR_GRAVITY_PAUSED; + } else + { + le->pos.trType = TR_STATIONARY; + } + } else { + + } +} + + +//----(SA) added + +/* +============== +CG_AddEmitter +============== +*/ +void CG_AddEmitter( localEntity_t *le ) { + vec3_t dir; + int nextTime = 50; + + if ( le->breakCount > cg.time ) { // using 'breakCount' for 'wait' + return; + } + + if ( cg_paused.integer ) { // don't add while paused + return; + } + + // TODO: look up particle script and use proper effect rather than this check + //if(water){} + //else if(oil) {} + //else if(steam) {} + //else if(wine) {} + + switch ( le->headJuncIndex ) { +// CG_ParticleImpactSmokePuff (cgs.media.oilParticle, le->pos.trBase); +// CG_Particle_Bleed(cgs.media.smokePuffShader, le->pos.trBase, dir, 0, 200); + + case 1: // oil + VectorScale( le->angles.trBase, le->radius, dir ); + CG_Particle_OilParticle( cgs.media.oilParticle, le->pos.trBase, dir, 10000, le->ownerNum ); + break; + case 2: // water + VectorScale( le->angles.trBase, le->radius, dir ); + CG_Particle_OilParticle( cgs.media.oilParticle, le->pos.trBase, dir, 10000, le->ownerNum ); + break; + case 3: // steam + nextTime = 100; +// CG_ParticleImpactSmokePuffExtended(cgs.media.smokeParticleShader, le->pos.trBase, 8, 1000, 8, 20, 20, 0.25f); + CG_ParticleImpactSmokePuffExtended( cgs.media.smokeParticleShader, le->pos.trBase, le->angles.trBase, 8, 1000, 8, le->radius, 20, 0.25f ); + break; + case 4: // wine + VectorScale( le->angles.trBase, le->radius, dir ); + CG_Particle_OilParticle( cgs.media.oilParticle, le->pos.trBase, dir, 10000, le->ownerNum ); + break; + case 5: // smoke + nextTime = 100; + CG_ParticleImpactSmokePuffExtended( cgs.media.smokeParticleShader, le->pos.trBase, dir, 8, 1000, 8, 20, 20, 0.25f ); + break; + case 6: // electrical + nextTime = 100; + CG_AddBulletParticles( le->pos.trBase, dir, 2, 800, 4, 16.0f ); +// CG_AddBulletParticles( le->pos.trBase, dir, 1, 800, 4, 0 ); + break; + + case 0: + default: + nextTime = 100; + CG_ParticleImpactSmokePuffExtended( cgs.media.smokeParticleShader, le->pos.trBase, dir, 8, 1000, 8, 20, 20, 0.25f ); + break; + } + + le->breakCount = cg.time + nextTime; +} + +//----(SA) end + + +/* +================ +CG_AddFragment +================ +*/ +void CG_AddFragment( localEntity_t *le ) { + vec3_t newOrigin; + trace_t trace; + refEntity_t *re; + float flameAlpha = 0.0f; // TTimo: init + vec3_t flameDir; + qboolean hasFlame = qfalse; + int i; + int contents; + + // Ridah + re = &le->refEntity; + if ( !re->fadeStartTime || re->fadeEndTime < le->endTime ) { + if ( le->endTime - cg.time > 5000 ) { + re->fadeStartTime = le->endTime - 5000; + } else { + re->fadeStartTime = le->endTime - 1000; + } + re->fadeEndTime = le->endTime; + } + + // Ridah, flaming gibs + if ( le->onFireStart && ( le->onFireStart < cg.time && le->onFireEnd > cg.time ) ) { + hasFlame = qtrue; + // calc the alpha + flameAlpha = 1.0 - ( (float)( cg.time - le->onFireStart ) / (float)( le->onFireEnd - le->onFireStart ) ); + if ( flameAlpha < 0.0 ) { + flameAlpha = 0.0; + } + if ( flameAlpha > 1.0 ) { + flameAlpha = 1.0; + } + trap_S_AddLoopingSound( -1, le->refEntity.origin, vec3_origin, cgs.media.flameCrackSound, (int)( 5.0 * flameAlpha ) ); + } + +//----(SA) added + if ( le->leFlags & LEF_SMOKING ) { + float alpha; + refEntity_t flash; + + // create a little less smoke + + // TODO: FIXME: this is not quite right, because it'll become fps dependant - in a bad way. + // the slower the fps, the /more/ smoke there'll be, probably driving the fps lower. + if ( !cg_paused.integer ) { // don't add while paused + if ( !( rand() % 5 ) ) { + alpha = 1.0 - ( (float)( cg.time - le->startTime ) / (float)( le->endTime - le->startTime ) ); + alpha *= 0.25f; + memset( &flash, 0, sizeof( flash ) ); + CG_PositionEntityOnTag( &flash, &le->refEntity, "tag_flash", 0, NULL ); + CG_ParticleImpactSmokePuffExtended( cgs.media.smokeParticleShader, flash.origin, tv( 0,0,1 ), 8, 1000, 8, 20, 20, alpha ); + } + } + } +//----(SA) end + + if ( le->pos.trType == TR_STATIONARY ) { + int t; + + // Ridah, add the flame + if ( hasFlame ) { + refEntity_t backupEnt; + + backupEnt = le->refEntity; + + VectorClear( flameDir ); + flameDir[2] = 1; + + le->refEntity.shaderRGBA[3] = ( unsigned char )( 255.0 * flameAlpha ); + VectorCopy( flameDir, le->refEntity.fireRiseDir ); + le->refEntity.customShader = cgs.media.onFireShader; + trap_R_AddRefEntityToScene( &le->refEntity ); + le->refEntity.customShader = cgs.media.onFireShader2; + trap_R_AddRefEntityToScene( &le->refEntity ); + + le->refEntity = backupEnt; + } + + t = le->endTime - cg.time; + trap_R_AddRefEntityToScene( &le->refEntity ); + + return; + + } else if ( le->pos.trType == TR_GRAVITY_PAUSED ) { + int t; + + // Ridah, add the flame + if ( hasFlame ) { + refEntity_t backupEnt; + + backupEnt = le->refEntity; + + VectorClear( flameDir ); + flameDir[2] = 1; + + le->refEntity.shaderRGBA[3] = ( unsigned char )( 255.0 * flameAlpha ); + VectorCopy( flameDir, le->refEntity.fireRiseDir ); + le->refEntity.customShader = cgs.media.onFireShader; + trap_R_AddRefEntityToScene( &le->refEntity ); + le->refEntity.customShader = cgs.media.onFireShader2; + trap_R_AddRefEntityToScene( &le->refEntity ); + + le->refEntity = backupEnt; + } + + t = le->endTime - cg.time; + trap_R_AddRefEntityToScene( &le->refEntity ); + + + // trace a line from previous position down, to see if I should start falling again + + VectorCopy( le->refEntity.origin, newOrigin ); + newOrigin [2] -= 5; + CG_Trace( &trace, le->refEntity.origin, NULL, NULL, newOrigin, -1, CONTENTS_SOLID | CONTENTS_PLAYERCLIP | CONTENTS_MISSILECLIP ); + + if ( trace.fraction == 1.0 ) { // it's clear, start moving again + VectorClear( le->pos.trDelta ); + VectorClear( le->angles.trDelta ); + le->pos.trType = TR_GRAVITY; // nothing below me, start falling again + } else { + return; + } + } + + // calculate new position + BG_EvaluateTrajectory( &le->pos, cg.time, newOrigin ); + + if ( hasFlame ) { + // calc the flame dir + VectorSubtract( le->refEntity.origin, newOrigin, flameDir ); + if ( VectorLength( flameDir ) == 0 ) { + flameDir[2] = 1; + // play a burning sound when not moving + trap_S_AddLoopingSound( 0, newOrigin, vec3_origin, cgs.media.flameSound, (int)( 0.3 * 255.0 * flameAlpha ) ); + } else { + VectorNormalize( flameDir ); + // play a flame blow sound when moving + trap_S_AddLoopingSound( 0, newOrigin, vec3_origin, cgs.media.flameBlowSound, (int)( 0.3 * 255.0 * flameAlpha ) ); + } + } + + contents = CONTENTS_SOLID; + + // trace a line from previous position to new position + if ( ( le->leFlags & LEF_NOTOUCHPARENT ) && le->ownerNum ) { + CG_Trace( &trace, le->refEntity.origin, NULL, NULL, newOrigin, le->ownerNum, contents ); + } else { + CG_Trace( &trace, le->refEntity.origin, NULL, NULL, newOrigin, -1, contents ); + } + + // did we hit someone? + if ( le->leFlags & LEF_PLAYER_DAMAGE ) { + // only do damage if travelling at a fast enough velocity + if ( newOrigin[2] < le->refEntity.origin[2] ) { + vec3_t pmin, pmax, dmin = {-12,-12,-12}, dmax = {12,12,12}; + // debris bounds + VectorAdd( dmin, newOrigin, dmin ); + VectorAdd( dmax, newOrigin, dmax ); + dmax[2] += le->refEntity.origin[2] - newOrigin[2]; // add falling distance to box + // are we touching the player? + VectorAdd( cg.snap->ps.mins, cg.snap->ps.origin, pmin ); + VectorAdd( cg.snap->ps.maxs, cg.snap->ps.origin, pmax ); + pmin[2] = pmax[2] - 32; // only hit on the head + for ( i = 0; i < 3; i++ ) { + if ( ( dmax[i] < pmin[i] ) || ( dmin[i] > pmax[i] ) ) { + break; + } + } + if ( i == 3 ) { + trap_S_StartSound( cg.snap->ps.origin, cg.snap->ps.clientNum, CHAN_VOICE, cgs.media.debrisHitSound ); + CG_ClientDamage( cg.snap->ps.clientNum, ENTITYNUM_WORLD, CLDMG_DEBRIS ); + // disable damage now for this debris + le->leFlags &= ~LEF_PLAYER_DAMAGE; + } + } + } + + if ( trace.fraction == 1.0 ) { + + // still in free fall + VectorCopy( newOrigin, le->refEntity.origin ); + + if ( le->leFlags & LEF_TUMBLE || le->angles.trType == TR_LINEAR ) { + vec3_t angles; + + BG_EvaluateTrajectory( &le->angles, cg.time, angles ); + AnglesToAxis( angles, le->refEntity.axis ); + if ( le->sizeScale && le->sizeScale != 1.0 ) { + for ( i = 0; i < 3; i++ ) VectorScale( le->refEntity.axis[i], le->sizeScale, le->refEntity.axis[i] ); + } + } + + // Ridah, add the flame + if ( hasFlame ) { + refEntity_t backupEnt; + + backupEnt = le->refEntity; + + le->refEntity.shaderRGBA[3] = ( unsigned char )( 255.0 * flameAlpha ); + VectorCopy( flameDir, le->refEntity.fireRiseDir ); + le->refEntity.customShader = cgs.media.onFireShader; + trap_R_AddRefEntityToScene( &le->refEntity ); + le->refEntity.customShader = cgs.media.onFireShader2; + trap_R_AddRefEntityToScene( &le->refEntity ); + + le->refEntity = backupEnt; + } + + trap_R_AddRefEntityToScene( &le->refEntity ); + + // add a blood trail + if ( le->leBounceSoundType == LEBS_BLOOD ) { + CG_BloodTrail( le ); + } + + return; + } + + // if it is in a nodrop zone, remove it + // this keeps gibs from waiting at the bottom of pits of death + // and floating levels + if ( trap_CM_PointContents( trace.endpos, 0 ) & CONTENTS_NODROP ) { + CG_FreeLocalEntity( le ); + return; + } + + // do a bouncy sound + CG_FragmentBounceSound( le, &trace ); + + // reflect the velocity on the trace plane + CG_ReflectVelocity( le, &trace ); + + + // (SA) disabled since it caused debris to get stuck in walls (flags mostly) + // RF, if it has come to a halt, pause velocity +// if (VectorLength( le->pos.trDelta ) < 1) { +// le->pos.trType = TR_STATIONARY; +// VectorCopy( trace.endpos, le->pos.trBase ); +// VectorClear( le->pos.trDelta ); +// } + + // break on contact? + if ( le->breakCount ) { + clientInfo_t *ci; + int clientNum; + localEntity_t *nle; + vec3_t dir; + + clientNum = le->ownerNum; + if ( clientNum < 0 || clientNum >= MAX_CLIENTS ) { + CG_Error( "Bad clientNum on player entity" ); + } + ci = &cgs.clientinfo[ clientNum ]; + + // spawn some new fragments + for ( i = 0; i <= le->breakCount; i++ ) { + nle = CG_AllocLocalEntity(); + memcpy( &( nle->leType ), &( le->leType ), sizeof( localEntity_t ) - 2 * sizeof( localEntity_t * ) ); + if ( nle->breakCount-- < 2 ) { + nle->refEntity.hModel = ci->gibModels[rand() % 2]; + } else { + nle->refEntity.hModel = ci->gibModels[rand() % 4]; + } + // make it smaller + nle->endTime = le->endTime + ( cg.time - le->startTime ); + nle->sizeScale *= 0.8; + if ( nle->sizeScale < 0.7 ) { + nle->sizeScale = 0.7; + nle->leBounceSoundType = 0; + } + // move us a bit + VectorNormalize2( nle->pos.trDelta, dir ); + VectorMA( trace.endpos, 4.0 * le->sizeScale * i, dir, nle->pos.trBase ); + // randomize vel a bit + VectorMA( nle->pos.trDelta, VectorLength( nle->pos.trDelta ) * 0.3, bytedirs[rand() % NUMVERTEXNORMALS], nle->pos.trDelta ); + } + // we're done + CG_FreeLocalEntity( le ); + return; + } + + if ( le->pos.trType == TR_STATIONARY && le->leMarkType == LEMT_BLOOD ) { + // RF, disabled for performance reasons in boss1 + //if (le->leBounceSoundType) + // CG_BloodPool (le, cgs.media.bloodPool, &trace); + + // leave a mark + if ( le->leMarkType ) { + CG_FragmentBounceMark( le, &trace ); + } + } + + // Ridah, add the flame + if ( hasFlame ) { + refEntity_t backupEnt; + + backupEnt = le->refEntity; + + le->refEntity.shaderRGBA[3] = ( unsigned char )( 255.0 * flameAlpha ); + VectorCopy( flameDir, le->refEntity.fireRiseDir ); + le->refEntity.customShader = cgs.media.onFireShader; + trap_R_AddRefEntityToScene( &le->refEntity ); + le->refEntity.customShader = cgs.media.onFireShader2; + trap_R_AddRefEntityToScene( &le->refEntity ); + + le->refEntity = backupEnt; + } + + trap_R_AddRefEntityToScene( &le->refEntity ); +} + +// Ridah +/* +================ +CG_AddMovingTracer +================ +*/ +void CG_AddMovingTracer( localEntity_t *le ) { + vec3_t start, end, dir; + + BG_EvaluateTrajectory( &le->pos, cg.time, start ); + VectorNormalize2( le->pos.trDelta, dir ); + VectorMA( start, cg_tracerLength.value, dir, end ); + + CG_DrawTracer( start, end ); +} + +/* +================ +CG_AddSparkElements +================ +*/ +void CG_AddSparkElements( localEntity_t *le ) { + vec3_t newOrigin; + trace_t trace; + float time; + float lifeFrac; + + time = (float)( cg.time - cg.frametime ); + + while ( 1 ) { + // calculate new position + BG_EvaluateTrajectory( &le->pos, cg.time, newOrigin ); + +// if ((le->endTime - le->startTime) > 500) { + + // trace a line from previous position to new position + CG_Trace( &trace, le->refEntity.origin, NULL, NULL, newOrigin, -1, MASK_SHOT ); + + // if stuck, kill it + if ( trace.startsolid ) { + // HACK, some walls screw up, so just pass through if starting in a solid + VectorCopy( newOrigin, trace.endpos ); + trace.fraction = 1.0; + } + + // moved some distance + VectorCopy( trace.endpos, le->refEntity.origin ); +/* + } else + { // just move it there + + VectorCopy( newOrigin, le->refEntity.origin ); + trace.fraction = 1.0; + + } +*/ + time += cg.frametime * trace.fraction; + + lifeFrac = (float)( cg.time - le->startTime ) / (float)( le->endTime - le->startTime ); + + // add a trail + le->headJuncIndex = CG_AddSparkJunc( le->headJuncIndex, + le->refEntity.customShader, + le->refEntity.origin, + 200, + 1.0 - lifeFrac, // start alpha + 0.0, //1.0 - lifeFrac, // end alpha + lifeFrac * 2.0 * ( ( ( le->endTime - le->startTime ) > 400 ) + 1 ) * 1.5, + lifeFrac * 2.0 * ( ( ( le->endTime - le->startTime ) > 400 ) + 1 ) * 1.5 ); + + // if it is in a nodrop zone, remove it + // this keeps gibs from waiting at the bottom of pits of death + // and floating levels +// for some reason SFM1.BSP is one big NODROP zone +// if ( trap_CM_PointContents( le->refEntity.origin, 0 ) & CONTENTS_NODROP ) { +// CG_FreeLocalEntity( le ); +// return; +// } + + if ( trace.fraction < 1.0 ) { + // just kill it + CG_FreeLocalEntity( le ); + return; +/* + // reflect the velocity on the trace plane + CG_ReflectVelocity( le, &trace ); + // the intersection is a fraction of the frametime + le->pos.trTime = (int)time; +*/ + } + + if ( trace.fraction == 1.0 || time >= (float)cg.time ) { + return; + } + } + +} + +/* +================ +CG_AddFuseSparkElements +================ +*/ +void CG_AddFuseSparkElements( localEntity_t *le ) { + + float FUSE_SPARK_WIDTH = 1.0; + + int step = 10; + float time; + float lifeFrac; + static vec3_t whiteColor = {1,1,1}; + + time = (float)( le->lastTrailTime ); + + while ( time < cg.time ) { + + // calculate new position + BG_EvaluateTrajectory( &le->pos, time, le->refEntity.origin ); + + lifeFrac = (float)( time - le->startTime ) / (float)( le->endTime - le->startTime ); + + //if (lifeFrac > 0.2) { + // add a trail + le->headJuncIndex = CG_AddTrailJunc( le->headJuncIndex, cgs.media.sparkParticleShader, time, STYPE_STRETCH, le->refEntity.origin, (int)( lifeFrac * (float)( le->endTime - le->startTime ) / 2.0 ), + 1.0 /*(1.0 - lifeFrac)*/, 0.0, FUSE_SPARK_WIDTH * ( 1.0 - lifeFrac ), FUSE_SPARK_WIDTH * ( 1.0 - lifeFrac ), TJFL_SPARKHEADFLARE, whiteColor, whiteColor, 0, 0 ); + //} + + time += step; + + le->lastTrailTime = time; + } + +} + +/* +================ +CG_AddBloodElements +================ +*/ +void CG_AddBloodElements( localEntity_t *le ) { + vec3_t newOrigin; + trace_t trace; + float time; + float lifeFrac; + + time = (float)( cg.time - cg.frametime ); + + while ( 1 ) { + // calculate new position + BG_EvaluateTrajectory( &le->pos, cg.time, newOrigin ); + + // trace a line from previous position to new position + CG_Trace( &trace, le->refEntity.origin, NULL, NULL, newOrigin, -1, MASK_SHOT ); + + // if stuck, kill it + if ( trace.startsolid ) { + // HACK, some walls screw up, so just pass through if starting in a solid + VectorCopy( newOrigin, trace.endpos ); + trace.fraction = 1.0; + } + + // moved some distance + VectorCopy( trace.endpos, le->refEntity.origin ); + time += cg.frametime * trace.fraction; + + lifeFrac = (float)( cg.time - le->startTime ) / (float)( le->endTime - le->startTime ); + + // add a trail + le->headJuncIndex = CG_AddSparkJunc( le->headJuncIndex, + cgs.media.bloodTrailShader, + le->refEntity.origin, + 200, + 1.0 - lifeFrac, // start alpha + 1.0 - lifeFrac, // end alpha + 3.0, + 5.0 ); + + if ( trace.fraction < 1.0 ) { + // reflect the velocity on the trace plane + CG_ReflectVelocity( le, &trace ); + + // TODO: spawn a blood decal here? + + // the intersection is a fraction of the frametime + le->pos.trTime = (int)time; + } + + if ( trace.fraction == 1.0 || time >= (float)cg.time ) { + return; + } + } + +} + +/* +=================== +CG_AddSpiritViewflash +=================== +*/ +void CG_AddSpiritViewflash( localEntity_t *le ) { + float alpha; +#define SPIRIT_FLASH_FADEIN 50 +#define SPIRIT_FLASH_DURATION 400 +#define SPIRIT_FLASH_FADEOUT 2000 + + if ( cg.viewFade > 1.0 ) { + return; + } + + if ( cg.time < le->startTime + SPIRIT_FLASH_FADEIN ) { + alpha = (float)( cg.time - le->startTime ) / (float)SPIRIT_FLASH_FADEIN; + } else if ( cg.time < le->startTime + SPIRIT_FLASH_FADEIN + SPIRIT_FLASH_DURATION ) { + alpha = 1.0; + } else if ( cg.time < le->startTime + SPIRIT_FLASH_FADEIN + SPIRIT_FLASH_DURATION + SPIRIT_FLASH_FADEOUT ) { + alpha = 1.0 - (float)( cg.time - ( le->startTime + SPIRIT_FLASH_FADEIN + SPIRIT_FLASH_DURATION ) ) / (float)SPIRIT_FLASH_FADEOUT; + } else { + return; + } + + if ( alpha < 0 ) { + return; + } + + // only ever use the highest fade + if ( cg.viewFade < alpha ) { + cg.viewFade = alpha; + } +} + +/* +================ +CG_AddClientCritter +================ +*/ +void CG_AddClientCritter( localEntity_t *le ) { + vec3_t newOrigin; + trace_t trace; + int time, step = 25, i; + vec3_t v, ang, v2, oDelta; + localEntity_t backup; + float oldSpeed, enemyDist, of; + vec3_t enemyPos; + float alpha = 0.0f, fadeRatio; // TTimo: init + + if ( cg_entities[le->ownerNum].currentState.otherEntityNum2 == cg.snap->ps.clientNum ) { + VectorCopy( cg.snap->ps.origin, enemyPos ); + enemyPos[2] += cg.snap->ps.viewheight; + } else { + VectorCopy( cg_entities[le->ownerNum].currentState.origin2, enemyPos ); + } + + VectorCopy( le->pos.trDelta, oDelta ); + + // vary the enemyPos to create a psuedo-randomness + of = (float)cg.time + le->startTime; + enemyPos[0] += 12 * ( sin( of / 100 ) * cos( of / 78 ) ); + enemyPos[1] += 12 * ( sin( of / 70 ) * cos( of / 82 ) ); + enemyPos[2] += 12 * ( sin( of / 67 ) * cos( of / 98 ) ); + + time = le->lastTrailTime + step; + + fadeRatio = (float)( cg.time - le->startTime ) / 2000.0; + if ( fadeRatio < 0.0 ) { + fadeRatio = 0.0; + } + if ( fadeRatio > 1.0 ) { + fadeRatio = 1.0; + } + + while ( time <= cg.time ) { + if ( time > le->refEntity.fadeStartTime ) { + alpha = (float)( time - le->refEntity.fadeStartTime ) / (float)( le->refEntity.fadeEndTime - le->refEntity.fadeStartTime ); + if ( alpha < 0 ) { + alpha = 0; + } else if ( alpha > 1 ) { + alpha = 1; + } + } else { + alpha = fadeRatio; + } + + // calculate new position + BG_EvaluateTrajectory( &le->pos, time, newOrigin ); + + VectorSubtract( enemyPos, le->refEntity.origin, v ); + enemyDist = VectorNormalize( v ); + + // trace a line from previous position to new position + CG_Trace( &trace, le->refEntity.origin, NULL, NULL, newOrigin, le->ownerNum, MASK_SHOT ); + + // if stuck, kill it + if ( trace.startsolid || ( trace.fraction < 1.0 ) ) { + // let heinrich spirits pass through geometry + if ( !( le->leType == LE_HELGA_SPIRIT && ( le->refEntity.hModel == cgs.media.ssSpiritSkullModel ) ) ) { + // kill it + CG_FreeLocalEntity( le ); + return; + } else { + VectorCopy( newOrigin, trace.endpos ); + } + } + + // moved some distance + VectorCopy( trace.endpos, le->refEntity.origin ); + + // record this pos + le->validOldPos[le->oldPosHead] = 1; + VectorCopy( le->refEntity.origin, le->oldPos[le->oldPosHead] ); + if ( ++le->oldPosHead >= MAX_OLD_POS ) { + le->oldPosHead = 0; + } + + if ( le->leType == LE_ZOMBIE_SPIRIT ) { + le->headJuncIndex = CG_AddTrailJunc( le->headJuncIndex, + cgs.media.zombieSpiritTrailShader, + time, + STYPE_STRETCH, + le->refEntity.origin, + (int)le->effectWidth, // trail life + 0.3 * alpha, + 0.0, + le->radius, + 0, + 0, //TJFL_FIXDISTORT, + colorWhite, + colorWhite, + 1.0, 1 ); + } + + if ( le->leType == LE_HELGA_SPIRIT && le->refEntity.hModel != cgs.media.ssSpiritSkullModel ) { + le->headJuncIndex = CG_AddTrailJunc( le->headJuncIndex, + cgs.media.helgaSpiritTrailShader, + time, + STYPE_STRETCH, + le->refEntity.origin, + (int)le->effectWidth, // trail life + 0.3 * alpha, + 0.0, + le->radius, + 0, + 0, //TJFL_FIXDISTORT, + colorWhite, + colorWhite, + 1.0, 1 ); + } + + // tracking factor + if ( le->leType == LE_ZOMBIE_BAT ) { + le->bounceFactor = 3.0 * (float)step / 1000.0; + } else { + le->bounceFactor = 5.0 * (float)step / 1000.0; + } + oldSpeed = VectorLength( le->pos.trDelta ); + + // track the enemy + backup = *le; + VectorSubtract( enemyPos, le->refEntity.origin, v ); + enemyDist = VectorNormalize( v ); + + if ( alpha > 0.5 && ( le->lastSpiritDmgTime < time - 100 ) && enemyDist < 24 ) { + localEntity_t *fb; + + // if dead, ignore + if ( !( le->ownerNum != cg.snap->ps.clientNum ? cg_entities[le->ownerNum].currentState.eFlags & EF_DEAD : cg.snap->ps.pm_type == PM_DEAD ) ) { + // inflict the damage! + CG_ClientDamage( cg_entities[le->ownerNum].currentState.otherEntityNum2, le->ownerNum, CLDMG_SPIRIT ); + le->lastSpiritDmgTime = time; + + if ( le->leType == LE_HELGA_SPIRIT ) { + // spawn a "flashbang" thinker + fb = CG_AllocLocalEntity(); + fb->leType = LE_SPIRIT_VIEWFLASH; + fb->startTime = cg.time + 50; + fb->endTime = fb->startTime + SPIRIT_FLASH_FADEIN + SPIRIT_FLASH_DURATION + SPIRIT_FLASH_FADEOUT; + // gasp! + CG_SoundPlayIndexedScript( cgs.media.helgaGaspSound, NULL, cg_entities[le->ownerNum].currentState.otherEntityNum2 ); + } + } + } + + VectorMA( le->pos.trDelta, le->bounceFactor * oldSpeed, v, le->pos.trDelta ); + //VectorCopy( v, le->pos.trDelta ); + if ( VectorLength( le->pos.trDelta ) < 1 ) { + CG_FreeLocalEntity( le ); + return; + } + + le->bounceFactor = 5.0 * (float)step / 1000.0; // avoidance factor + + // the intersection is a fraction of the frametime + le->pos.trTime = time; + VectorCopy( le->refEntity.origin, le->pos.trBase ); + VectorNormalize( le->pos.trDelta ); + VectorScale( le->pos.trDelta, oldSpeed, le->pos.trDelta ); + + // now trace ahead of time, if we're going to hit something, then avoid it + // only avoid dangers if we don't have direct sight to the enemy + trap_CM_BoxTrace( &trace, le->refEntity.origin, enemyPos, NULL, NULL, 0, MASK_SOLID ); + if ( trace.fraction < 1.0 ) { + BG_EvaluateTrajectory( &le->pos, time + 1000, newOrigin ); + + // if we would go passed the enemy, don't bother + if ( VectorDistance( le->refEntity.origin, enemyPos ) > VectorDistance( le->refEntity.origin, newOrigin ) ) { + + trap_CM_BoxTrace( &trace, le->refEntity.origin, newOrigin, NULL, NULL, 0, MASK_SOLID ); + + if ( trace.fraction < 1.0 ) { + // make sure we are not heading away from the enemy too much + VectorNormalize2( le->pos.trDelta, v2 ); + if ( DotProduct( v, v2 ) > 0.7 ) { + // avoid world geometry + backup = *le; + le->bounceFactor = ( 1.0 - trace.fraction ) * 10.0 * (float)step / 1000.0; // tracking and avoidance factor + // reflect the velocity on the trace plane + VectorMA( le->pos.trDelta, le->bounceFactor * oldSpeed, trace.plane.normal, le->pos.trDelta ); + if ( VectorLength( le->pos.trDelta ) < 1 ) { + CG_FreeLocalEntity( le ); + return; + } + // the intersection is a fraction of the frametime + le->pos.trTime = time; + VectorCopy( le->refEntity.origin, le->pos.trBase ); + VectorNormalize( le->pos.trDelta ); + VectorScale( le->pos.trDelta, oldSpeed, le->pos.trDelta ); + // + // double check end velocity + VectorNormalize2( le->pos.trDelta, v2 ); + if ( DotProduct( v, v2 ) <= 0.2 ) { + // restore + *le = backup; + } + } + } + } + } + + // set the angles + VectorNormalize2( oDelta, v ); + // HACK!!! skull model is back-to-front, need to fix + if ( le->leType == LE_ZOMBIE_SPIRIT /*|| le->leType == LE_HELGA_SPIRIT*/ ) { + VectorInverse( v ); + } + vectoangles( v, ang ); + AnglesToAxis( ang, le->refEntity.axis ); + // lean when turning + if ( le->leType == LE_ZOMBIE_BAT || le->leType == LE_HELGA_SPIRIT ) { + VectorSubtract( le->pos.trDelta, oDelta, v2 ); + ang[ROLL] = -0.5 * DotProduct( le->refEntity.axis[1], v2 ); + if ( fabs( ang[ROLL] ) < 20 ) { + ang[ROLL] = 0; + } else { + if ( ang[ROLL] < 0 ) { + ang[ROLL] += 20; + } else { + ang[ROLL] -= 20; + } + } + if ( fabs( ang[ROLL] ) > 80 ) { + if ( ang[ROLL] > 80 ) { + ang[ROLL] = 80; + } else { ang[ROLL] = -80;} + } + AnglesToAxis( ang, le->refEntity.axis ); + } + + // HACK: the skull is slightly higher than the origin + if ( le->leType == LE_ZOMBIE_SPIRIT ) { + // set the size scale + for ( i = 0; i < 3; i++ ) + VectorScale( le->refEntity.axis[i], 0.35, le->refEntity.axis[i] ); + VectorMA( le->refEntity.origin, -10, le->refEntity.axis[2], le->refEntity.origin ); + } + + le->lastTrailTime = time; + time += step; + } + + // Bats, set the frame + if ( le->leType == LE_ZOMBIE_BAT ) { + #define BAT_ANIM_FRAMETIME 30 + le->refEntity.frame = ( cg.time / BAT_ANIM_FRAMETIME + 1 ) % 19; + le->refEntity.oldframe = ( cg.time / BAT_ANIM_FRAMETIME ) % 19; + le->refEntity.backlerp = 1.0 - ( (float)( cg.time % BAT_ANIM_FRAMETIME ) / (float)BAT_ANIM_FRAMETIME ); + } + + // add the sound + if ( le->loopingSound ) { + if ( cg.time > le->refEntity.fadeStartTime ) { + trap_S_AddLoopingSound( 0, le->refEntity.origin, vec3_origin, le->loopingSound, 255 - (int)( 255.0 * (float)( cg.time - le->refEntity.fadeStartTime ) / (float)( le->refEntity.fadeEndTime - le->refEntity.fadeStartTime ) ) ); + } else { + trap_S_AddLoopingSound( 0, le->refEntity.origin, vec3_origin, le->loopingSound, 255 - (int)( 255.0 * ( 1.0 - alpha ) ) ); + } + } +/* + if (le->leType == LE_HELGA_SPIRIT) { + int cnt=1; + float alpha; + refEntity_t re; + + // add the "motion blur" ghosts + re = le->refEntity; + i = le->oldPosHead - 1; + if (i < 0) i = MAX_OLD_POS-1; + while (i != le->oldPosHead && le->validOldPos[i]) { + alpha = 1.0 - ((float)cnt / (float)MAX_OLD_POS); + if (alpha > 1.0) alpha = 1.0; + if (alpha < 0.0) alpha = 0.0; + + re.shaderTime = le->refEntity.shaderTime - cnt*100; + VectorCopy( le->oldPos[i], re.origin ); + re.shaderRGBA[3] = (unsigned char)(255.0 * alpha); + trap_R_AddRefEntityToScene( &re ); + + if (--i<0) i=MAX_OLD_POS-1; + cnt++; + } + } else { +*/ trap_R_AddRefEntityToScene( &le->refEntity ); +// } + + // Bats, add the flame + if ( le->leType == LE_ZOMBIE_BAT ) { + // + le->refEntity.shaderRGBA[3] = 255; + VectorNormalize2( le->pos.trDelta, v ); + VectorInverse( v ); + v[2] += 1; + VectorNormalize2( v, le->refEntity.fireRiseDir ); + + le->refEntity.customShader = cgs.media.onFireShader2; + trap_R_AddRefEntityToScene( &le->refEntity ); + le->refEntity.shaderTime = 1434; + trap_R_AddRefEntityToScene( &le->refEntity ); + + le->refEntity.customShader = 0; + le->refEntity.shaderTime = 0; + } +} + +/* +================ +CG_AddDebrisElements +================ +*/ +void CG_AddDebrisElements( localEntity_t *le ) { + vec3_t newOrigin; + trace_t trace; + float lifeFrac; + int t, step = 50; + + for ( t = le->lastTrailTime + step; t < cg.time; t += step ) { + // calculate new position + BG_EvaluateTrajectory( &le->pos, t, newOrigin ); + + // trace a line from previous position to new position + CG_Trace( &trace, le->refEntity.origin, NULL, NULL, newOrigin, -1, MASK_SHOT ); + + // if stuck, kill it + if ( trace.startsolid ) { + // HACK, some walls screw up, so just pass through if starting in a solid + VectorCopy( newOrigin, trace.endpos ); + trace.fraction = 1.0; + } + + // moved some distance + VectorCopy( trace.endpos, le->refEntity.origin ); + + // add a trail + lifeFrac = (float)( t - le->startTime ) / (float)( le->endTime - le->startTime ); + +#if 0 + // fire +#if 1 // flame + if ( le->effectWidth > 0 ) { + le->headJuncIndex = CG_AddSparkJunc( le->headJuncIndex, + cgs.media.fireTrailShader, + le->refEntity.origin, + (int)( 500.0 * ( 0.5 + 0.5 * ( 1.0 - lifeFrac ) ) ), // trail life + 1.0, // alpha + 0.5, // end alpha + 3, // start width + le->effectWidth ); // end width +#else // spark line + if ( le->effectWidth > 0 ) { + le->headJuncIndex = CG_AddSparkJunc( le->headJuncIndex, + cgs.media.sparkParticleShader, + le->refEntity.origin, + (int)( 600.0 * ( 0.5 + 0.5 * ( 0.5 - lifeFrac ) ) ), // trail life + 1.0 - lifeFrac * 2, // alpha + 0.5 * ( 1.0 - lifeFrac ), // end alpha + 5.0 * ( 1.0 - lifeFrac ), // start width + 5.0 * ( 1.0 - lifeFrac ) ); // end width +#endif + } +#endif + + // smoke + if ( le->effectFlags & 1 ) { + le->headJuncIndex2 = CG_AddSmokeJunc( le->headJuncIndex2, + cgs.media.smokeTrailShader, + le->refEntity.origin, + (int)( 2000.0 * ( 0.5 + 0.5 * ( 1.0 - lifeFrac ) ) ), // trail life + 1.0 * ( trace.fraction == 1.0 ) * ( 0.5 + 0.5 * ( 1.0 - lifeFrac ) ), // alpha + 1, // start width + (int)( 60.0 * ( 0.5 + 0.5 * ( 1.0 - lifeFrac ) ) ) ); // end width + } + + // if it is in a nodrop zone, remove it + // this keeps gibs from waiting at the bottom of pits of death + // and floating levels +// if ( trap_CM_PointContents( trace.endpos, 0 ) & CONTENTS_NODROP ) { +// CG_FreeLocalEntity( le ); +// return; +// } + + if ( trace.fraction < 1.0 ) { + // reflect the velocity on the trace plane + CG_ReflectVelocity( le, &trace ); + if ( VectorLength( le->pos.trDelta ) < 1 ) { + CG_FreeLocalEntity( le ); + return; + } + // the intersection is a fraction of the frametime + le->pos.trTime = t; + } + + le->lastTrailTime = t; + } + +} + +// Rafael Shrapnel +/* +=============== +CG_AddShrapnel +=============== +*/ +void CG_AddShrapnel( localEntity_t *le ) { + vec3_t newOrigin; + trace_t trace; + + if ( le->pos.trType == TR_STATIONARY ) { + // sink into the ground if near the removal time + int t; + float oldZ; + + t = le->endTime - cg.time; + if ( t < SINK_TIME ) { + // we must use an explicit lighting origin, otherwise the + // lighting would be lost as soon as the origin went + // into the ground + VectorCopy( le->refEntity.origin, le->refEntity.lightingOrigin ); + le->refEntity.renderfx |= RF_LIGHTING_ORIGIN; + oldZ = le->refEntity.origin[2]; + le->refEntity.origin[2] -= 16 * ( 1.0 - (float)t / SINK_TIME ); + trap_R_AddRefEntityToScene( &le->refEntity ); + le->refEntity.origin[2] = oldZ; + } else { + trap_R_AddRefEntityToScene( &le->refEntity ); + CG_AddParticleShrapnel( le ); + } + + return; + } + + // calculate new position + BG_EvaluateTrajectory( &le->pos, cg.time, newOrigin ); + + // trace a line from previous position to new position + CG_Trace( &trace, le->refEntity.origin, NULL, NULL, newOrigin, -1, CONTENTS_SOLID ); + if ( trace.fraction == 1.0 ) { + // still in free fall + VectorCopy( newOrigin, le->refEntity.origin ); + + if ( le->leFlags & LEF_TUMBLE ) { + vec3_t angles; + + BG_EvaluateTrajectory( &le->angles, cg.time, angles ); + AnglesToAxis( angles, le->refEntity.axis ); + } + + trap_R_AddRefEntityToScene( &le->refEntity ); + CG_AddParticleShrapnel( le ); + return; + } + + // if it is in a nodrop zone, remove it + // this keeps gibs from waiting at the bottom of pits of death + // and floating levels + if ( trap_CM_PointContents( trace.endpos, 0 ) & CONTENTS_NODROP ) { + CG_FreeLocalEntity( le ); + return; + } + + // leave a mark + CG_FragmentBounceMark( le, &trace ); + + // do a bouncy sound + CG_FragmentBounceSound( le, &trace ); + + // reflect the velocity on the trace plane + CG_ReflectVelocity( le, &trace ); + + trap_R_AddRefEntityToScene( &le->refEntity ); + CG_AddParticleShrapnel( le ); +} +// done. + +/* +===================================================================== + +TRIVIAL LOCAL ENTITIES + +These only do simple scaling or modulation before passing to the renderer +===================================================================== +*/ + +/* +==================== +CG_AddFadeRGB +==================== +*/ +void CG_AddFadeRGB( localEntity_t *le ) { + refEntity_t *re; + float c; + + re = &le->refEntity; + + c = ( le->endTime - cg.time ) * le->lifeRate; + c *= 0xff; + + re->shaderRGBA[0] = le->color[0] * c; + re->shaderRGBA[1] = le->color[1] * c; + re->shaderRGBA[2] = le->color[2] * c; + re->shaderRGBA[3] = le->color[3] * c; + + trap_R_AddRefEntityToScene( re ); +} + +/* +================== +CG_AddMoveScaleFade +================== +*/ +static void CG_AddMoveScaleFade( localEntity_t *le ) { + refEntity_t *re; + float c; + vec3_t delta; + float len; + + re = &le->refEntity; + + // fade / grow time +// c = ( le->endTime - cg.time ) * le->lifeRate; + if ( le->fadeInTime > le->startTime && cg.time < le->fadeInTime ) { + // fade / grow time + c = 1.0 - (float) ( le->fadeInTime - cg.time ) / ( le->fadeInTime - le->startTime ); + } else { + // fade / grow time + c = ( le->endTime - cg.time ) * le->lifeRate; + } + + // Ridah, spark + if ( !( le->leFlags & LEF_NOFADEALPHA ) ) { + // done. + re->shaderRGBA[3] = 0xff * c * le->color[3]; + } + + if ( !( le->leFlags & LEF_PUFF_DONT_SCALE ) ) { + c = ( le->endTime - cg.time ) * le->lifeRate; + re->radius = le->radius * ( 1.0 - c ) + 8; + } + + BG_EvaluateTrajectory( &le->pos, cg.time, re->origin ); + + // if the view would be "inside" the sprite, kill the sprite + // so it doesn't add too much overdraw + VectorSubtract( re->origin, cg.refdef.vieworg, delta ); + len = VectorLength( delta ); + if ( len < le->radius ) { + CG_FreeLocalEntity( le ); + return; + } + + trap_R_AddRefEntityToScene( re ); +} + + +/* +=================== +CG_AddScaleFade + +For rocket smokes that hang in place, fade out, and are +removed if the view passes through them. +There are often many of these, so it needs to be simple. +=================== +*/ +static void CG_AddScaleFade( localEntity_t *le ) { + refEntity_t *re; + float c; + vec3_t delta; + float len; + + re = &le->refEntity; + + // fade / grow time + c = ( le->endTime - cg.time ) * le->lifeRate; + + re->shaderRGBA[3] = 0xff * c * le->color[3]; + if ( !( le->leFlags & LEF_PUFF_DONT_SCALE ) ) { + re->radius = le->radius * ( 1.0 - c ) + 8; + } + + // if the view would be "inside" the sprite, kill the sprite + // so it doesn't add too much overdraw + VectorSubtract( re->origin, cg.refdef.vieworg, delta ); + len = VectorLength( delta ); + if ( len < le->radius ) { + CG_FreeLocalEntity( le ); + return; + } + + trap_R_AddRefEntityToScene( re ); +} + + +/* +================= +CG_AddFallScaleFade + +This is just an optimized CG_AddMoveScaleFade +For blood mists that drift down, fade out, and are +removed if the view passes through them. +There are often 100+ of these, so it needs to be simple. +================= +*/ +static void CG_AddFallScaleFade( localEntity_t *le ) { + refEntity_t *re; + float c; + vec3_t delta; + float len; + + re = &le->refEntity; + + // fade time + c = ( le->endTime - cg.time ) * le->lifeRate; + + re->shaderRGBA[3] = 0xff * c * le->color[3]; + + re->origin[2] = le->pos.trBase[2] - ( 1.0 - c ) * le->pos.trDelta[2]; + + re->radius = le->radius * ( 1.0 - c ) + 16; + + // if the view would be "inside" the sprite, kill the sprite + // so it doesn't add too much overdraw + VectorSubtract( re->origin, cg.refdef.vieworg, delta ); + len = VectorLength( delta ); + if ( len < le->radius ) { + CG_FreeLocalEntity( le ); + return; + } + + trap_R_AddRefEntityToScene( re ); +} + + + +/* +================ +CG_AddExplosion +================ +*/ +static void CG_AddExplosion( localEntity_t *ex ) { + refEntity_t *ent; + + ent = &ex->refEntity; + + // add the entity + // RF, don't add if shader is invalid + if ( ent->customShader >= 0 ) { + trap_R_AddRefEntityToScene( ent ); + } + + // add the dlight + if ( ex->light ) { + float light; + + light = (float)( cg.time - ex->startTime ) / ( ex->endTime - ex->startTime ); + if ( light < 0.5 ) { + light = 1.0; + } else { + light = 1.0 - ( light - 0.5 ) * 2; + } + light = ex->light * light; + trap_R_AddLightToScene( ent->origin, light, ex->lightColor[0], ex->lightColor[1], ex->lightColor[2], 0 ); + } +} + +/* +================ +CG_AddSpriteExplosion +================ +*/ +static void CG_AddSpriteExplosion( localEntity_t *le ) { + refEntity_t re; + float c; + + re = le->refEntity; + + c = ( le->endTime - cg.time ) / ( float ) ( le->endTime - le->startTime ); + if ( c > 1 ) { + c = 1.0; // can happen during connection problems + } + + re.shaderRGBA[0] = 0xff; + re.shaderRGBA[1] = 0xff; + re.shaderRGBA[2] = 0xff; + re.shaderRGBA[3] = 0xff * c * 0.33; + + re.reType = RT_SPRITE; + re.radius = 42 * ( 1.0 - c ) + 30; + + // Ridah, move away from surface + VectorMA( le->pos.trBase, ( 1.0 - c ), le->pos.trDelta, re.origin ); + // done. + + // RF, don't add if shader is invalid + if ( re.customShader >= 0 ) { + trap_R_AddRefEntityToScene( &re ); + } + + // add the dlight + if ( le->light ) { + float light; + + // Ridah, modified this so the light fades out rather than shrinking + /* + light = (float)( cg.time - le->startTime ) / ( le->endTime - le->startTime ); + if ( light < 0.5 ) { + light = 1.0; + } else { + light = 1.0 - ( light - 0.5 ) * 2; + } + light = le->light * light; + trap_R_AddLightToScene(re.origin, light, le->lightColor[0], le->lightColor[1], le->lightColor[2], 0 ); + */ + light = (float)( cg.time - le->startTime ) / ( le->endTime - le->startTime ); + if ( light < 0.5 ) { + light = 1.0; + } else { + light = 1.0 - ( light - 0.5 ) * 2; + } + trap_R_AddLightToScene( re.origin, le->light, light * le->lightColor[0], light * le->lightColor[1], light * le->lightColor[2], 0 ); + // done. + } +} + +//============================================================================== + +/* +=================== +CG_AddLocalEntities + +=================== +*/ +void CG_AddLocalEntities( void ) { + localEntity_t *le, *next; + + cg.viewFade = 0.0; + + // walk the list backwards, so any new local entities generated + // (trails, marks, etc) will be present this frame + le = cg_activeLocalEntities.prev; + for ( ; le != &cg_activeLocalEntities ; le = next ) { + // grab next now, so if the local entity is freed we + // still have it + next = le->prev; + + if ( cg.time >= le->endTime ) { + CG_FreeLocalEntity( le ); + continue; + } + switch ( le->leType ) { + default: + CG_Error( "Bad leType: %i", le->leType ); + break; + + // Ridah + case LE_MOVING_TRACER: + CG_AddMovingTracer( le ); + break; + case LE_SPARK: + CG_AddSparkElements( le ); + break; + case LE_FUSE_SPARK: + CG_AddFuseSparkElements( le ); + break; + case LE_DEBRIS: + CG_AddDebrisElements( le ); + break; + case LE_BLOOD: + CG_AddBloodElements( le ); + break; + case LE_HELGA_SPIRIT: + case LE_ZOMBIE_SPIRIT: + case LE_ZOMBIE_BAT: + CG_AddClientCritter( le ); + break; + case LE_SPIRIT_VIEWFLASH: + CG_AddSpiritViewflash( le ); + // done. + + case LE_MARK: + break; + + case LE_SPRITE_EXPLOSION: + CG_AddSpriteExplosion( le ); + break; + + case LE_EXPLOSION: + CG_AddExplosion( le ); + break; + + case LE_FRAGMENT: // gibs and brass + CG_AddFragment( le ); + break; + + case LE_MOVE_SCALE_FADE: // water bubbles + CG_AddMoveScaleFade( le ); + break; + + case LE_FADE_RGB: // teleporters, railtrails + CG_AddFadeRGB( le ); + break; + + case LE_FALL_SCALE_FADE: // gib blood trails + CG_AddFallScaleFade( le ); + break; + + case LE_SCALE_FADE: // rocket trails + CG_AddScaleFade( le ); + break; + + case LE_EMITTER: + CG_AddEmitter( le ); + break; + + } + } +} + diff --git a/src/cgame/cg_main.c b/src/cgame/cg_main.c new file mode 100644 index 0000000..ca808c8 --- /dev/null +++ b/src/cgame/cg_main.c @@ -0,0 +1,2427 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +/* + * name: cg_main.c + * + * desc: initialization and primary entry point for cgame + * +*/ + + +#include "cg_local.h" +#include "../ui/ui_shared.h" + +displayContextDef_t cgDC; + +int forceModelModificationCount = -1; + +void CG_Init( int serverMessageNum, int serverCommandSequence ); +void CG_Shutdown( void ); + + +/* +================ +vmMain + +This is the only way control passes into the module. +This must be the very first function compiled into the .q3vm file +================ +*/ +#if defined( __MACOS__ ) // TTimo: guarding +#pragma export on +#endif +int vmMain( int command, int arg0, int arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7, int arg8, int arg9, int arg10, int arg11 ) { +#if defined( __MACOS__ ) +#pragma export off +#endif + switch ( command ) { + case CG_GET_TAG: + return CG_GetTag( arg0, (char *)arg1, (orientation_t *)arg2 ); + case CG_DRAW_ACTIVE_FRAME: + CG_DrawActiveFrame( arg0, arg1, arg2 ); + return 0; + case CG_EVENT_HANDLING: + CG_EventHandling( arg0 ); + return 0; + case CG_INIT: + CG_Init( arg0, arg1 ); + return 0; + case CG_SHUTDOWN: + CG_Shutdown(); + return 0; + case CG_CONSOLE_COMMAND: + return CG_ConsoleCommand(); + case CG_CROSSHAIR_PLAYER: + return CG_CrosshairPlayer(); + case CG_LAST_ATTACKER: + return CG_LastAttacker(); + case CG_KEY_EVENT: + CG_KeyEvent( arg0, arg1 ); + return 0; + case CG_MOUSE_EVENT: + cgDC.cursorx = cgs.cursorX; + cgDC.cursory = cgs.cursorY; + CG_MouseEvent( arg0, arg1 ); + return 0; + default: + CG_Error( "vmMain: unknown command %i", command ); + break; + } + return -1; +} + + +cg_t cg; +cgs_t cgs; +centity_t cg_entities[MAX_GENTITIES]; +weaponInfo_t cg_weapons[MAX_WEAPONS]; +itemInfo_t cg_items[MAX_ITEMS]; + + +vmCvar_t cg_railTrailTime; +vmCvar_t cg_centertime; +vmCvar_t cg_runpitch; +vmCvar_t cg_runroll; +vmCvar_t cg_bobup; +vmCvar_t cg_bobpitch; +vmCvar_t cg_bobroll; +vmCvar_t cg_swingSpeed; +vmCvar_t cg_shadows; +vmCvar_t cg_gibs; +vmCvar_t cg_drawTimer; +vmCvar_t cg_drawFPS; +vmCvar_t cg_drawSnapshot; +vmCvar_t cg_draw3dIcons; +vmCvar_t cg_drawIcons; +vmCvar_t cg_youGotMail; //----(SA) added +vmCvar_t cg_drawAmmoWarning; +vmCvar_t cg_drawCrosshair; +vmCvar_t cg_drawCrosshairNames; +vmCvar_t cg_drawCrosshairPickups; +vmCvar_t cg_hudAlpha; +vmCvar_t cg_weaponCycleDelay; //----(SA) added +vmCvar_t cg_cycleAllWeaps; +vmCvar_t cg_useWeapsForZoom; +vmCvar_t cg_drawAllWeaps; +vmCvar_t cg_drawRewards; +vmCvar_t cg_crosshairSize; +vmCvar_t cg_crosshairAlpha; //----(SA) added +vmCvar_t cg_crosshairX; +vmCvar_t cg_crosshairY; +vmCvar_t cg_crosshairHealth; +vmCvar_t cg_draw2D; +vmCvar_t cg_drawFrags; +vmCvar_t cg_teamChatsOnly; +vmCvar_t cg_drawStatus; +vmCvar_t cg_animSpeed; +vmCvar_t cg_drawSpreadScale; +vmCvar_t cg_debugAnim; +vmCvar_t cg_debugPosition; +vmCvar_t cg_debugEvents; +vmCvar_t cg_errorDecay; +vmCvar_t cg_nopredict; +vmCvar_t cg_noPlayerAnims; +vmCvar_t cg_showmiss; +vmCvar_t cg_footsteps; +vmCvar_t cg_markTime; +vmCvar_t cg_brassTime; +vmCvar_t cg_viewsize; +vmCvar_t cg_letterbox; +vmCvar_t cg_drawGun; +vmCvar_t cg_drawFPGun; +vmCvar_t cg_drawGamemodels; +vmCvar_t cg_cursorHints; +vmCvar_t cg_hintFadeTime; //----(SA) added +vmCvar_t cg_gun_frame; +vmCvar_t cg_gun_x; +vmCvar_t cg_gun_y; +vmCvar_t cg_gun_z; +vmCvar_t cg_tracerChance; +vmCvar_t cg_tracerWidth; +vmCvar_t cg_tracerLength; +vmCvar_t cg_tracerSpeed; +vmCvar_t cg_autoswitch; +vmCvar_t cg_ignore; +vmCvar_t cg_simpleItems; +vmCvar_t cg_fov; +vmCvar_t cg_zoomFov; +vmCvar_t cg_zoomStepBinoc; +vmCvar_t cg_zoomStepSniper; +vmCvar_t cg_zoomStepSnooper; +vmCvar_t cg_zoomStepFG; //----(SA) added +vmCvar_t cg_zoomDefaultBinoc; +vmCvar_t cg_zoomDefaultSniper; +vmCvar_t cg_zoomDefaultSnooper; +vmCvar_t cg_zoomDefaultFG; //----(SA) added +vmCvar_t cg_reticles; +vmCvar_t cg_reticleBrightness; //----(SA) added +vmCvar_t cg_thirdPerson; +vmCvar_t cg_thirdPersonRange; +vmCvar_t cg_thirdPersonAngle; +vmCvar_t cg_stereoSeparation; +vmCvar_t cg_lagometer; +vmCvar_t cg_drawAttacker; +vmCvar_t cg_synchronousClients; +vmCvar_t cg_teamChatTime; +vmCvar_t cg_teamChatHeight; +vmCvar_t cg_stats; +vmCvar_t cg_buildScript; +vmCvar_t cg_forceModel; +vmCvar_t cg_coronafardist; +vmCvar_t cg_coronas; +vmCvar_t cg_paused; +vmCvar_t cg_blood; +vmCvar_t cg_predictItems; +vmCvar_t cg_deferPlayers; +vmCvar_t cg_drawTeamOverlay; +vmCvar_t cg_enableBreath; +vmCvar_t cg_autoactivate; +vmCvar_t cg_useSuggestedWeapons; //----(SA) added +vmCvar_t cg_emptyswitch; +vmCvar_t cg_particleDist; +vmCvar_t cg_particleLOD; +vmCvar_t cg_blinktime; //----(SA) added + +vmCvar_t cg_smoothClients; +vmCvar_t pmove_fixed; +vmCvar_t pmove_msec; + +// Rafael - particle switch +vmCvar_t cg_wolfparticles; +// done + +// Ridah +vmCvar_t cg_gameType; +vmCvar_t cg_bloodTime; +vmCvar_t cg_norender; +vmCvar_t cg_skybox; + +// Rafael +vmCvar_t cg_gameSkill; +// done + +vmCvar_t cg_reloading; //----(SA) added + +// JPW NERVE +vmCvar_t cg_medicChargeTime; +vmCvar_t cg_engineerChargeTime; +vmCvar_t cg_LTChargeTime; +vmCvar_t cg_soldierChargeTime; +vmCvar_t cg_redlimbotime; +vmCvar_t cg_bluelimbotime; +// jpw + +vmCvar_t cg_hunkUsed; +vmCvar_t cg_soundAdjust; +vmCvar_t cg_expectedhunkusage; + +vmCvar_t cg_showAIState; + +vmCvar_t cg_notebook; +vmCvar_t cg_notebookpages; // bitflags for the currently accessable pages. if they wanna cheat, let 'em. Most won't, or will wait 'til they actually play it. + +vmCvar_t cg_currentSelectedPlayer; +vmCvar_t cg_currentSelectedPlayerName; +vmCvar_t cg_cameraMode; +vmCvar_t cg_cameraOrbit; +vmCvar_t cg_cameraOrbitDelay; +vmCvar_t cg_timescaleFadeEnd; +vmCvar_t cg_timescaleFadeSpeed; +vmCvar_t cg_timescale; +vmCvar_t cg_smallFont; +vmCvar_t cg_bigFont; +vmCvar_t cg_hudFiles; + +vmCvar_t cg_animState; +vmCvar_t cg_missionStats; +vmCvar_t cg_waitForFire; + +vmCvar_t cg_loadWeaponSelect; + +// NERVE - SMF - Wolf multiplayer configuration cvars +vmCvar_t mp_playerType; +vmCvar_t mp_team; +vmCvar_t mp_weapon; +vmCvar_t mp_pistol; +vmCvar_t mp_item1; +vmCvar_t mp_item2; +vmCvar_t mp_mapDesc; +vmCvar_t mp_mapTitle; +vmCvar_t mp_itemDesc; +// -NERVE - SMF + +typedef struct { + vmCvar_t *vmCvar; + char *cvarName; + char *defaultString; + int cvarFlags; +} cvarTable_t; + +cvarTable_t cvarTable[] = { + { &cg_ignore, "cg_ignore", "0", 0 }, // used for debugging + { &cg_autoswitch, "cg_autoswitch", "2", CVAR_ARCHIVE }, + { &cg_drawGun, "cg_drawGun", "1", CVAR_ARCHIVE }, + { &cg_drawGamemodels, "cg_drawGamemodels", "1", CVAR_CHEAT }, + { &cg_drawFPGun, "cg_drawFPGun", "1", CVAR_ARCHIVE }, + { &cg_gun_frame, "cg_gun_frame", "0", CVAR_TEMP }, + { &cg_cursorHints, "cg_cursorHints", "1", CVAR_ARCHIVE }, + { &cg_hintFadeTime, "cg_hintFadeTime", "500", CVAR_ARCHIVE }, //----(SA) added + { &cg_zoomFov, "cg_zoomfov", "22.5", CVAR_ARCHIVE }, + { &cg_zoomDefaultBinoc, "cg_zoomDefaultBinoc", "22.5", CVAR_ARCHIVE }, + { &cg_zoomDefaultSniper, "cg_zoomDefaultSniper", "15", CVAR_ARCHIVE }, + { &cg_zoomDefaultSnooper, "cg_zoomDefaultSnooper", "40", CVAR_ARCHIVE }, + { &cg_zoomDefaultFG, "cg_zoomDefaultFG", "55", CVAR_ARCHIVE }, //----(SA) added + { &cg_zoomStepBinoc, "cg_zoomStepBinoc", "3", CVAR_ARCHIVE }, + { &cg_zoomStepSniper, "cg_zoomStepSniper", "2", CVAR_ARCHIVE }, + { &cg_zoomStepSnooper, "cg_zoomStepSnooper", "5", CVAR_ARCHIVE }, + { &cg_zoomStepFG, "cg_zoomStepFG", "10", CVAR_ARCHIVE }, //----(SA) added + { &cg_fov, "cg_fov", "90", CVAR_ARCHIVE | CVAR_CHEAT }, // JPW NERVE added cheat protect NOTE: there is already a dmflag (DF_FIXED_FOV) to allow server control of this cheat + { &cg_viewsize, "cg_viewsize", "100", CVAR_ARCHIVE }, + { &cg_letterbox, "cg_letterbox", "0", CVAR_TEMP }, //----(SA) added + { &cg_stereoSeparation, "cg_stereoSeparation", "0.4", CVAR_ARCHIVE }, + { &cg_shadows, "cg_shadows", "1", CVAR_ARCHIVE }, + { &cg_gibs, "cg_gibs", "1", CVAR_ARCHIVE }, + { &cg_draw2D, "cg_draw2D", "1", CVAR_ARCHIVE }, + { &cg_drawSpreadScale, "cg_drawSpreadScale", "1", CVAR_ARCHIVE }, + { &cg_drawFrags, "cg_drawFrags", "1", CVAR_ARCHIVE }, + { &cg_drawStatus, "cg_drawStatus", "1", CVAR_ARCHIVE }, + { &cg_drawTimer, "cg_drawTimer", "0", CVAR_ARCHIVE }, + { &cg_drawFPS, "cg_drawFPS", "0", CVAR_ARCHIVE }, + { &cg_drawSnapshot, "cg_drawSnapshot", "0", CVAR_ARCHIVE }, + { &cg_draw3dIcons, "cg_draw3dIcons", "1", CVAR_ARCHIVE }, + { &cg_drawIcons, "cg_drawIcons", "1", CVAR_ARCHIVE }, + { &cg_drawAmmoWarning, "cg_drawAmmoWarning", "1", CVAR_ARCHIVE }, + { &cg_drawAttacker, "cg_drawAttacker", "1", CVAR_ARCHIVE }, + { &cg_drawCrosshair, "cg_drawCrosshair", "4", CVAR_ARCHIVE }, + { &cg_drawCrosshairNames, "cg_drawCrosshairNames", "1", CVAR_ARCHIVE }, + { &cg_drawCrosshairPickups, "cg_drawCrosshairPickups", "1", CVAR_ARCHIVE }, + { &cg_drawRewards, "cg_drawRewards", "1", CVAR_ARCHIVE }, + { &cg_hudAlpha, "cg_hudAlpha", "0.8", CVAR_ARCHIVE }, + { &cg_useWeapsForZoom, "cg_useWeapsForZoom", "1", CVAR_ARCHIVE }, + { &cg_weaponCycleDelay, "cg_weaponCycleDelay", "150", CVAR_ARCHIVE }, //----(SA) added + { &cg_cycleAllWeaps, "cg_cycleAllWeaps", "1", CVAR_ARCHIVE }, + { &cg_drawAllWeaps, "cg_drawAllWeaps", "1", CVAR_ARCHIVE }, + { &cg_crosshairSize, "cg_crosshairSize", "24", CVAR_ARCHIVE }, + { &cg_crosshairAlpha, "cg_crosshairAlpha", "0.5", CVAR_ARCHIVE }, //----(SA) added + { &cg_crosshairHealth, "cg_crosshairHealth", "1", CVAR_ARCHIVE }, + { &cg_crosshairX, "cg_crosshairX", "0", CVAR_ARCHIVE }, + { &cg_crosshairY, "cg_crosshairY", "0", CVAR_ARCHIVE }, + { &cg_brassTime, "cg_brassTime", "1250", CVAR_ARCHIVE }, + { &cg_simpleItems, "cg_simpleItems", "0", CVAR_ARCHIVE }, + { &cg_reticles, "cg_reticles", "1", CVAR_CHEAT }, + { &cg_reticleBrightness, "cg_reticleBrightness", "0.7", CVAR_ARCHIVE }, + { &cg_markTime, "cg_marktime", "20000", CVAR_ARCHIVE }, + { &cg_lagometer, "cg_lagometer", "1", CVAR_ARCHIVE }, + { &cg_railTrailTime, "cg_railTrailTime", "400", CVAR_ARCHIVE }, + { &cg_gun_x, "cg_gunX", "0", CVAR_CHEAT }, + { &cg_gun_y, "cg_gunY", "0", CVAR_CHEAT }, + { &cg_gun_z, "cg_gunZ", "0", CVAR_CHEAT }, + { &cg_centertime, "cg_centertime", "3", CVAR_CHEAT }, + { &cg_runpitch, "cg_runpitch", "0.002", CVAR_ARCHIVE}, + { &cg_runroll, "cg_runroll", "0.005", CVAR_ARCHIVE }, + { &cg_bobup, "cg_bobup", "0.005", CVAR_ARCHIVE }, + { &cg_bobpitch, "cg_bobpitch", "0.002", CVAR_ARCHIVE }, + { &cg_bobroll, "cg_bobroll", "0.002", CVAR_ARCHIVE }, + + // JOSEPH 10-27-99 + { &cg_autoactivate, "cg_autoactivate", "1", CVAR_ARCHIVE }, + { &cg_emptyswitch, "cg_emptyswitch", "0", CVAR_ARCHIVE }, + // END JOSEPH + +//----(SA) added + { &cg_particleDist, "cg_particleDist", "1024", CVAR_ARCHIVE }, + { &cg_particleLOD, "cg_particleLOD", "0", CVAR_ARCHIVE }, + { &cg_useSuggestedWeapons, "cg_useSuggestedWeapons", "1", CVAR_ARCHIVE }, //----(SA) added +//----(SA) end + + // Ridah, more fluid rotations + { &cg_swingSpeed, "cg_swingSpeed", "0.1", CVAR_CHEAT }, // was 0.3 for Q3 + { &cg_bloodTime, "cg_bloodTime", "120", CVAR_ARCHIVE }, + { &cg_hunkUsed, "com_hunkUsed", "0", 0 }, + { &cg_soundAdjust, "hunk_soundadjust", "0", 0 }, + + { &cg_skybox, "cg_skybox", "1", CVAR_CHEAT }, + // done. + + { &cg_animSpeed, "cg_animspeed", "1", CVAR_CHEAT }, + { &cg_debugAnim, "cg_debuganim", "0", CVAR_CHEAT }, + { &cg_debugPosition, "cg_debugposition", "0", CVAR_CHEAT }, + { &cg_debugEvents, "cg_debugevents", "0", CVAR_CHEAT }, + { &cg_errorDecay, "cg_errordecay", "100", 0 }, + { &cg_nopredict, "cg_nopredict", "0", 0 }, + { &cg_noPlayerAnims, "cg_noplayeranims", "0", CVAR_CHEAT }, + { &cg_showmiss, "cg_showmiss", "0", 0 }, + { &cg_footsteps, "cg_footsteps", "1", CVAR_CHEAT }, + { &cg_tracerChance, "cg_tracerchance", "0.4", CVAR_CHEAT }, + { &cg_tracerWidth, "cg_tracerwidth", "0.8", CVAR_CHEAT }, + { &cg_tracerSpeed, "cg_tracerSpeed", "4500", CVAR_CHEAT }, + { &cg_tracerLength, "cg_tracerlength", "160", CVAR_CHEAT }, + { &cg_thirdPersonRange, "cg_thirdPersonRange", "40", 0 }, + { &cg_thirdPersonAngle, "cg_thirdPersonAngle", "0", CVAR_CHEAT }, + { &cg_thirdPerson, "cg_thirdPerson", "0", 0 }, + { &cg_teamChatTime, "cg_teamChatTime", "3000", CVAR_ARCHIVE }, + { &cg_teamChatHeight, "cg_teamChatHeight", "8", CVAR_ARCHIVE }, + { &cg_forceModel, "cg_forceModel", "0", CVAR_ARCHIVE }, + { &cg_coronafardist, "cg_coronafardist", "1536", CVAR_ARCHIVE }, + { &cg_coronas, "cg_coronas", "1", CVAR_ARCHIVE }, + { &cg_predictItems, "cg_predictItems", "1", CVAR_ARCHIVE }, + { &cg_deferPlayers, "cg_deferPlayers", "1", CVAR_ARCHIVE }, + { &cg_drawTeamOverlay, "cg_drawTeamOverlay", "0", CVAR_ARCHIVE }, + { &cg_stats, "cg_stats", "0", 0 }, + { &cg_blinktime, "cg_blinktime", "100", CVAR_ARCHIVE}, //----(SA) added + + { &cg_enableBreath, "g_enableBreath", "1", CVAR_SERVERINFO}, + { &cg_cameraOrbit, "cg_cameraOrbit", "0", CVAR_CHEAT}, + { &cg_cameraOrbitDelay, "cg_cameraOrbitDelay", "50", CVAR_ARCHIVE}, + { &cg_timescaleFadeEnd, "cg_timescaleFadeEnd", "1", 0}, + { &cg_timescaleFadeSpeed, "cg_timescaleFadeSpeed", "0", 0}, + { &cg_timescale, "timescale", "1", 0}, +// { &cg_smoothClients, "cg_smoothClients", "0", CVAR_USERINFO | CVAR_ARCHIVE}, + { &cg_cameraMode, "com_cameraMode", "0", CVAR_CHEAT}, + + { &pmove_fixed, "pmove_fixed", "0", 0}, + { &pmove_msec, "pmove_msec", "8", 0}, + { &cg_smallFont, "ui_smallFont", "0.25", CVAR_ARCHIVE}, + { &cg_bigFont, "ui_bigFont", "0.4", CVAR_ARCHIVE}, + { &cg_hudFiles, "cg_hudFiles", "ui/hud.txt", CVAR_ARCHIVE}, + + { &cg_teamChatsOnly, "cg_teamChatsOnly", "0", CVAR_ARCHIVE }, + // the following variables are created in other parts of the system, + // but we also reference them here + + { &cg_buildScript, "com_buildScript", "0", 0 }, // force loading of all possible data amd error on failures + { &cg_paused, "cl_paused", "0", CVAR_ROM }, + + { &cg_blood, "com_blood", "1", CVAR_ARCHIVE }, + { &cg_synchronousClients, "g_syncronousClients", "0", 0 }, // communicated by systeminfo + { &cg_currentSelectedPlayer, "cg_currentSelectedPlayer", "0", CVAR_ARCHIVE}, + { &cg_currentSelectedPlayerName, "cg_currentSelectedPlayerName", "", CVAR_ARCHIVE}, + + // Rafael - particle switch + { &cg_wolfparticles, "cg_wolfparticles", "1", CVAR_ARCHIVE }, + // done + + // Ridah + { &cg_gameType, "g_gametype", "0", 0 }, // communicated by systeminfo + { &cg_norender, "cg_norender", "0", 0 }, // only used during single player, to suppress rendering until the server is ready + + { &cg_gameSkill, "g_gameskill", "2", 0 }, // communicated by systeminfo // (SA) new default '2' (was '1') + + { &cg_reloading, "g_reloading", "0", 0 }, //----(SA) added + + // JPW NERVE + { &cg_medicChargeTime, "g_medicChargeTime", "10000", 0 }, // communicated by systeminfo + { &cg_LTChargeTime, "g_LTChargeTime", "30000", 0 }, // communicated by systeminfo + { &cg_engineerChargeTime, "g_engineerChargeTime", "30000", 0 }, // communicated by systeminfo + { &cg_soldierChargeTime, "g_soldierChargeTime", "20000", 0 }, // communicated by systeminfo + { &cg_bluelimbotime, "g_bluelimbotime", "30000", 0 }, // communicated by systeminfo + { &cg_redlimbotime, "g_redlimbotime","30000", 0 }, // communicated by systeminfo + // jpw + + { &cg_notebook, "cl_notebook", "0", CVAR_ROM }, + { &cg_notebookpages, "cg_notebookpages", "0", CVAR_ROM}, +// { &cg_youGotMail, "cg_youGotMail", "0", CVAR_ROM}, // used to display notebook new-info icon + { &cg_youGotMail, "cg_youGotMail", "0", 0}, // used to display notebook new-info icon + + { &cg_animState, "cg_animState", "0", CVAR_CHEAT}, + { &cg_missionStats, "g_missionStats", "0", CVAR_ROM}, + { &cg_waitForFire, "cl_waitForFire", "0", CVAR_ROM}, + + { &cg_loadWeaponSelect, "cg_loadWeaponSelect", "0", CVAR_ROM}, + + { &cg_expectedhunkusage, "com_expectedhunkusage", "0", CVAR_ROM}, + + // NERVE - SMF + { &mp_playerType, "mp_playerType", "0", 0 }, + { &mp_team, "mp_team", "0", 0 }, + { &mp_weapon, "mp_weapon", "0", 0 }, + { &mp_pistol, "mp_pistol", "0", 0 }, + { &mp_item1, "mp_item1", "0", 0 }, + { &mp_item2, "mp_item2", "0", 0 }, + { &mp_mapDesc, "mp_mapDesc", "", 0 }, + { &mp_mapTitle, "mp_mapTitle", "", 0 }, + { &mp_itemDesc, "mp_itemDesc", "", 0 }, + // -NERVE - SMF + + { &cg_showAIState, "cg_showAIState", "0", CVAR_CHEAT}, +}; +int cvarTableSize = sizeof( cvarTable ) / sizeof( cvarTable[0] ); + +/* +================= +CG_RegisterCvars +================= +*/ +void CG_RegisterCvars( void ) { + int i; + cvarTable_t *cv; + char var[MAX_TOKEN_CHARS]; + + trap_Cvar_Set( "cg_letterbox", "0" ); // force this for people who might have it in their + + for ( i = 0, cv = cvarTable ; i < cvarTableSize ; i++, cv++ ) { + trap_Cvar_Register( cv->vmCvar, cv->cvarName, + cv->defaultString, cv->cvarFlags ); + } + + // see if we are also running the server on this machine + trap_Cvar_VariableStringBuffer( "sv_running", var, sizeof( var ) ); + cgs.localServer = atoi( var ); + + forceModelModificationCount = cg_forceModel.modificationCount; + + trap_Cvar_Register( NULL, "model", DEFAULT_MODEL, CVAR_USERINFO | CVAR_ARCHIVE ); + trap_Cvar_Register( NULL, "head", DEFAULT_HEAD, CVAR_USERINFO | CVAR_ARCHIVE ); + + +} + +/* +=================== +CG_ForceModelChange +=================== +*/ +// TTimo: unused +/* +static void CG_ForceModelChange( void ) { + int i; + + for (i=0 ; ivmCvar ); + } +/* RF, disabled this, not needed anymore + // if force model changed + if ( forceModelModificationCount != cg_forceModel.modificationCount ) { + forceModelModificationCount = cg_forceModel.modificationCount; + CG_ForceModelChange(); + } +*/ +} + + +int CG_CrosshairPlayer( void ) { + if ( cg.time > ( cg.crosshairClientTime + 1000 ) ) { + return -1; + } + return cg.crosshairClientNum; +} + +int CG_LastAttacker( void ) { + if ( !cg.attackerTime ) { + return -1; + } + return cg.snap->ps.persistant[PERS_ATTACKER]; +} + +void QDECL CG_Printf( const char *msg, ... ) { + va_list argptr; + char text[1024]; + + va_start( argptr, msg ); + vsprintf( text, msg, argptr ); + va_end( argptr ); + + trap_Print( text ); +} + +void QDECL CG_Error( const char *msg, ... ) { + va_list argptr; + char text[1024]; + + va_start( argptr, msg ); + vsprintf( text, msg, argptr ); + va_end( argptr ); + + trap_Error( text ); +} + +// TTimo: was commented out for Mac, guarding +#if !defined( CGAME_HARD_LINKED ) || defined( __MACOS__ ) +// this is only here so the functions in q_shared.c and bg_*.c can link (FIXME) + +void QDECL Com_Error( int level, const char *error, ... ) { + va_list argptr; + char text[1024]; + + va_start( argptr, error ); + vsprintf( text, error, argptr ); + va_end( argptr ); + + CG_Error( "%s", text ); +} + +void QDECL Com_Printf( const char *msg, ... ) { + va_list argptr; + char text[1024]; + + va_start( argptr, msg ); + vsprintf( text, msg, argptr ); + va_end( argptr ); + + CG_Printf( "%s", text ); +} + +#endif + +/* +================ +CG_Argv +================ +*/ +const char *CG_Argv( int arg ) { + static char buffer[MAX_STRING_CHARS]; + + trap_Argv( arg, buffer, sizeof( buffer ) ); + + return buffer; +} + + +//======================================================================== +void CG_SetupDlightstyles( void ) { + int i, j; + char *str; + char *token; + int entnum; + centity_t *cent; + + cg.lightstylesInited = qtrue; + + for ( i = 1; i < MAX_DLIGHT_CONFIGSTRINGS; i++ ) + { + str = (char *) CG_ConfigString( CS_DLIGHTS + i ); + if ( !strlen( str ) ) { + break; + } + + token = COM_Parse( &str ); // ent num + entnum = atoi( token ); + cent = &cg_entities[entnum]; + + token = COM_Parse( &str ); // stylestring + Q_strncpyz( cent->dl_stylestring, token, strlen( token ) ); + + token = COM_Parse( &str ); // offset + cent->dl_frame = atoi( token ); + cent->dl_oldframe = cent->dl_frame - 1; + if ( cent->dl_oldframe < 0 ) { + cent->dl_oldframe = strlen( cent->dl_stylestring ); + } + + token = COM_Parse( &str ); // sound id + cent->dl_sound = atoi( token ); + + token = COM_Parse( &str ); // attenuation + cent->dl_atten = atoi( token ); + + for ( j = 0; j < strlen( cent->dl_stylestring ); j++ ) { + + cent->dl_stylestring[j] += cent->dl_atten; // adjust character for attenuation/amplification + + // clamp result + if ( cent->dl_stylestring[j] < 'a' ) { + cent->dl_stylestring[j] = 'a'; + } + if ( cent->dl_stylestring[j] > 'z' ) { + cent->dl_stylestring[j] = 'z'; + } + } + + cent->dl_backlerp = 0.0; + cent->dl_time = cg.time; + } + +} + +//======================================================================== + +/* +================= +CG_RegisterItemSounds + +The server says this item is used on this level +================= +*/ +static void CG_RegisterItemSounds( int itemNum ) { + gitem_t *item; + char data[MAX_QPATH]; + char *s, *start; + int len; + + item = &bg_itemlist[ itemNum ]; + + if ( item->pickup_sound ) { + trap_S_RegisterSound( item->pickup_sound ); + } + + // parse the space seperated precache string for other media + s = item->sounds; + if ( !s || !s[0] ) { + return; + } + + while ( *s ) { + start = s; + while ( *s && *s != ' ' ) { + s++; + } + + len = s - start; + if ( len >= MAX_QPATH || len < 5 ) { + CG_Error( "PrecacheItem: %s has bad precache string", + item->classname ); + return; + } + memcpy( data, start, len ); + data[len] = 0; + if ( *s ) { + s++; + } + + if ( !strcmp( data + len - 3, "wav" ) ) { + trap_S_RegisterSound( data ); + } + } +} + + +//----(SA) added + +// this is the only thing that sets a cap on # items. would like it to be adaptable. +// (rather than 256 max items with pickup name fixed at 32 chars) + +/* +============== +CG_LoadPickupNames +============== +*/ +#define MAX_BUFFER 20000 +static void CG_LoadPickupNames( void ) { + char buffer[MAX_BUFFER]; + char *text; + char filename[MAX_QPATH]; + fileHandle_t f; + int len, i; + char *token; + + Com_sprintf( filename, MAX_QPATH, "text/pickupnames.txt" ); + len = trap_FS_FOpenFile( filename, &f, FS_READ ); + if ( len <= 0 ) { + CG_Printf( S_COLOR_RED "WARNING: pickup name file (pickupnames.txt not found in main/text)\n" ); + return; + } + if ( len > MAX_BUFFER ) { + CG_Error( "%s is too big, make it smaller (max = %i bytes)\n", filename, MAX_BUFFER ); + } + + // load the file into memory + trap_FS_Read( buffer, len, f ); + buffer[len] = 0; + trap_FS_FCloseFile( f ); + // parse the list + text = buffer; + + for ( i = 0; i < bg_numItems; i++ ) { + token = COM_ParseExt( &text, qtrue ); + if ( !token[0] ) { + break; + } + if ( !Q_stricmp( token, "---" ) ) { // no name. use hardcoded value + if ( bg_itemlist[i].pickup_name && strlen( bg_itemlist[i].pickup_name ) ) { + Com_sprintf( cgs.itemPrintNames[i], MAX_QPATH, bg_itemlist[ i ].pickup_name ); + } else { + cgs.itemPrintNames[i][0] = 0; + } + } else { + Com_sprintf( cgs.itemPrintNames[i], MAX_QPATH, token ); + } + } +} + +// a straight dupe right now so I don't mess anything up while adding this +static void CG_LoadTranslationStrings( void ) { + char buffer[MAX_BUFFER]; + char *text; + char filename[MAX_QPATH]; + fileHandle_t f; + int len, i, numStrings; + char *token; + + Com_sprintf( filename, MAX_QPATH, "text/strings.txt" ); + len = trap_FS_FOpenFile( filename, &f, FS_READ ); + if ( len <= 0 ) { + CG_Printf( S_COLOR_RED "WARNING: string translation file (strings.txt not found in main/text)\n" ); + return; + } + if ( len > MAX_BUFFER ) { + CG_Error( "%s is too big, make it smaller (max = %i bytes)\n", filename, MAX_BUFFER ); + } + + // load the file into memory + trap_FS_Read( buffer, len, f ); + buffer[len] = 0; + trap_FS_FCloseFile( f ); + // parse the list + text = buffer; + + numStrings = sizeof( translateStrings ) / sizeof( translateStrings[0] ) - 1; + + for ( i = 0; i < numStrings; i++ ) { + token = COM_ParseExt( &text, qtrue ); + if ( !token[0] ) { + break; + } + translateStrings[i].localname = malloc( strlen( token ) + 1 ); + strcpy( translateStrings[i].localname, token ); + } +} + + +static void CG_LoadTranslateStrings( void ) { + CG_LoadPickupNames(); + CG_LoadTranslationStrings(); // right now just centerprint +} + +//----(SA) end + + +/* +================= +CG_RegisterSounds + +called during a precache command +================= +*/ +static void CG_RegisterSounds( void ) { + int i; + char items[MAX_ITEMS + 1]; + char name[MAX_QPATH]; + const char *soundName; + + // Ridah, init sound scripts + CG_SoundInit(); + // done. + + cgs.media.n_health = trap_S_RegisterSound( "sound/items/n_health.wav" ); + cgs.media.noFireUnderwater = trap_S_RegisterSound( "sound/weapons/underwaterfire.wav" ); //----(SA) added + + cgs.media.snipersound = trap_S_RegisterSound( "sound/weapons/mauser/mauserf1.wav" ); + cgs.media.tracerSound = trap_S_RegisterSound( "sound/weapons/machinegun/buletby1.wav" ); + cgs.media.selectSound = trap_S_RegisterSound( "sound/weapons/change.wav" ); + cgs.media.wearOffSound = trap_S_RegisterSound( "sound/items/wearoff.wav" ); + cgs.media.useNothingSound = trap_S_RegisterSound( "sound/items/use_nothing.wav" ); + cgs.media.gibSound = trap_S_RegisterSound( "sound/player/gibsplt1.wav" ); + cgs.media.gibBounce1Sound = trap_S_RegisterSound( "sound/player/gibimp1.wav" ); + cgs.media.gibBounce2Sound = trap_S_RegisterSound( "sound/player/gibimp2.wav" ); + cgs.media.gibBounce3Sound = trap_S_RegisterSound( "sound/player/gibimp3.wav" ); + +// cgs.media.teleInSound = trap_S_RegisterSound( "sound/world/telein.wav" ); +// cgs.media.teleOutSound = trap_S_RegisterSound( "sound/world/teleout.wav" ); +// cgs.media.respawnSound = trap_S_RegisterSound( "sound/items/respawn1.wav" ); + + + cgs.media.grenadebounce[GRENBOUNCE_DEFAULT][0] = trap_S_RegisterSound( "sound/weapons/grenade/hgrenb1a.wav" ); + cgs.media.grenadebounce[GRENBOUNCE_DEFAULT][1] = trap_S_RegisterSound( "sound/weapons/grenade/hgrenb2a.wav" ); + cgs.media.grenadebounce[GRENBOUNCE_DIRT][0] = trap_S_RegisterSound( "sound/weapons/grenade/hg_dirt1a.wav" ); + cgs.media.grenadebounce[GRENBOUNCE_DIRT][1] = trap_S_RegisterSound( "sound/weapons/grenade/hg_dirt2a.wav" ); + cgs.media.grenadebounce[GRENBOUNCE_WOOD][0] = trap_S_RegisterSound( "sound/weapons/grenade/hg_wood1a.wav" ); + cgs.media.grenadebounce[GRENBOUNCE_WOOD][1] = trap_S_RegisterSound( "sound/weapons/grenade/hg_wood2a.wav" ); + cgs.media.grenadebounce[GRENBOUNCE_METAL][0] = trap_S_RegisterSound( "sound/weapons/grenade/hg_metal1a.wav" ); + cgs.media.grenadebounce[GRENBOUNCE_METAL][1] = trap_S_RegisterSound( "sound/weapons/grenade/hg_metal2a.wav" ); + + cgs.media.dynamitebounce1 = trap_S_RegisterSound( "sound/weapons/dynamite/dynamite_bounce.wav" ); + + cgs.media.fbarrelexp1 = trap_S_RegisterSound( "sound/weapons/flamebarrel/fbarrela.wav" ); + cgs.media.fbarrelexp2 = trap_S_RegisterSound( "sound/weapons/flamebarrel/fbarrelb.wav" ); + + + cgs.media.fkickwall = trap_S_RegisterSound( "sound/weapons/melee/fstatck.wav" ); + cgs.media.fkickflesh = trap_S_RegisterSound( "sound/weapons/melee/fstatck.wav" ); + cgs.media.fkickmiss = trap_S_RegisterSound( "sound/weapons/melee/fstmiss.wav" ); + + + cgs.media.noAmmoSound = trap_S_RegisterSound( "sound/weapons/noammo.wav" ); + + cgs.media.talkSound = trap_S_RegisterSound( "sound/player/talk.wav" ); + cgs.media.landSound = trap_S_RegisterSound( "sound/player/land1.wav" ); + + cgs.media.watrInSound = trap_S_RegisterSound( "sound/player/watr_in.wav" ); + cgs.media.watrOutSound = trap_S_RegisterSound( "sound/player/watr_out.wav" ); + cgs.media.watrUnSound = trap_S_RegisterSound( "sound/player/watr_un.wav" ); + + cgs.media.underWaterSound = trap_S_RegisterSound( "sound/world/underwater03.wav" ); + + for ( i = 0 ; i < 4 ; i++ ) { + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/step%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_NORMAL][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/boot%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_BOOT][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/flesh%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_FLESH][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/mech%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_MECH][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/energy%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_ENERGY][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/splash%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_SPLASH][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/clank%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_METAL][i] = trap_S_RegisterSound( name ); + + + // (SA) Wolf footstep sound registration + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/wood%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_WOOD][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/grass%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_GRASS][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/gravel%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_GRAVEL][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/roof%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_ROOF][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/snow%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_SNOW][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/carpet%i.wav", i + 1 ); //----(SA) + cgs.media.footsteps[FOOTSTEP_CARPET][i] = trap_S_RegisterSound( name ); + } + + // only register the items that the server says we need + strcpy( items, CG_ConfigString( CS_ITEMS ) ); + + for ( i = 1 ; i < bg_numItems ; i++ ) { + if ( items[ i ] == '1' || cg_buildScript.integer ) { + CG_RegisterItemSounds( i ); + } + } + + for ( i = 1 ; i < MAX_SOUNDS ; i++ ) { + soundName = CG_ConfigString( CS_SOUNDS + i ); + if ( !soundName[0] ) { + break; + } + if ( soundName[0] == '*' ) { + continue; // custom sound + } + + // Ridah, register sound scripts seperately + if ( !strstr( soundName, ".wav" ) ) { + cgs.gameSounds[i] = CG_SoundScriptPrecache( soundName ); //----(SA) shouldn't this be okay? The cs index is reserved anyway, so it can't hurt, right? + cgs.gameSoundTypes[i] = 2; + } else { + cgs.gameSounds[i] = trap_S_RegisterSound( soundName ); + cgs.gameSoundTypes[i] = 1; + } + } + + //----(SA) added + cgs.media.grenadePulseSound4 = trap_S_RegisterSound( "sound/weapons/grenade/grenpulse4.wav" ); + cgs.media.grenadePulseSound3 = trap_S_RegisterSound( "sound/weapons/grenade/grenpulse3.wav" ); + cgs.media.grenadePulseSound2 = trap_S_RegisterSound( "sound/weapons/grenade/grenpulse2.wav" ); + cgs.media.grenadePulseSound1 = trap_S_RegisterSound( "sound/weapons/grenade/grenpulse1.wav" ); + //----(SA) end + + //----(SA) added + cgs.media.debBounce1Sound = trap_S_RegisterSound( "sound/world/block.wav" ); + cgs.media.debBounce2Sound = trap_S_RegisterSound( "sound/world/brick.wav" ); + cgs.media.debBounce3Sound = trap_S_RegisterSound( "sound/world/brick2.wav" ); + + // Ridah + cgs.media.flameSound = trap_S_RegisterSound( "sound/weapons/flamethrower/fl_fire.wav" ); + cgs.media.flameBlowSound = trap_S_RegisterSound( "sound/weapons/flamethrower/fl_blow.wav" ); + cgs.media.flameStartSound = trap_S_RegisterSound( "sound/weapons/flamethrower/fl_start.wav" ); + cgs.media.flameStreamSound = trap_S_RegisterSound( "sound/weapons/flamethrower/fl_stream.wav" ); + cgs.media.flameCrackSound = trap_S_RegisterSound( "sound/world/firecrack1.wav" ); + cgs.media.boneBounceSound = trap_S_RegisterSound( "sound/world/boardbreak.wav" ); // TODO: need a real sound for this + + cgs.media.lightningSounds[0] = trap_S_RegisterSound( "sound/world/electzap1.wav" ); + cgs.media.lightningSounds[1] = trap_S_RegisterSound( "sound/world/electzap2.wav" ); + cgs.media.lightningSounds[2] = trap_S_RegisterSound( "sound/world/electzap3.wav" ); + cgs.media.lightningZap = trap_S_RegisterSound( "sound/world/electrocute.wav" ); + + // precache sound scripts that get called from the cgame + cgs.media.bulletHitFleshScript = CG_SoundScriptPrecache( "bulletHitFlesh" ); + cgs.media.bulletHitFleshMetalScript = CG_SoundScriptPrecache( "bulletHitFleshMetal" ); + + cgs.media.teslaZapScript = CG_SoundScriptPrecache( "teslaZap" ); + cgs.media.teslaLoopSound = trap_S_RegisterSound( "sound/weapons/tesla/loop.wav" ); + + cgs.media.batsFlyingLoopSound = trap_S_RegisterSound( "sound/world/bats_flying.wav" ); + + // FIXME: only needed with item +// cgs.media.flightSound = trap_S_RegisterSound( "sound/items/flight.wav" ); +// cgs.media.medkitSound = trap_S_RegisterSound ("sound/items/use_medkit.wav"); + cgs.media.elecSound = trap_S_RegisterSound( "sound/items/use_elec.wav" ); + cgs.media.fireSound = trap_S_RegisterSound( "sound/items/use_fire.wav" ); + cgs.media.waterSound = trap_S_RegisterSound( "sound/items/use_water.wav" ); + cgs.media.wineSound = trap_S_RegisterSound( "sound/pickup/holdable/use_wine.wav" ); //----(SA) modified + cgs.media.bookSound = trap_S_RegisterSound( "sound/pickup/holdable/use_book.wav" ); //----(SA) added + cgs.media.staminaSound = trap_S_RegisterSound( "sound/pickup/holdable/use_stamina.wav" ); //----(SA) added + cgs.media.quadSound = trap_S_RegisterSound( "sound/items/damage3.wav" ); + cgs.media.sfx_ric1 = trap_S_RegisterSound( "sound/weapons/machinegun/ric1.wav" ); + cgs.media.sfx_ric2 = trap_S_RegisterSound( "sound/weapons/machinegun/ric2.wav" ); + cgs.media.sfx_ric3 = trap_S_RegisterSound( "sound/weapons/machinegun/ric3.wav" ); +// cgs.media.sfx_railg = trap_S_RegisterSound ("sound/weapons/railgun/railgf1a.wav"); + cgs.media.sfx_rockexp = trap_S_RegisterSound( "sound/weapons/rocket/rocklx1a.wav" ); + cgs.media.sfx_dynamiteexp = trap_S_RegisterSound( "sound/weapons/dynamite/dynamite_exp.wav" ); + cgs.media.sfx_dynamiteexpDist = trap_S_RegisterSound( "sound/weapons/dynamite/dynamite_exp_dist.wav" ); //----(SA) added + + + cgs.media.sfx_spearhit = trap_S_RegisterSound( "sound/weapons/speargun/spearhit.wav" ); + + cgs.media.sfx_knifehit[0] = trap_S_RegisterSound( "sound/weapons/knife/knife_hit1.wav" ); // hitting player + cgs.media.sfx_knifehit[1] = trap_S_RegisterSound( "sound/weapons/knife/knife_hit2.wav" ); + cgs.media.sfx_knifehit[2] = trap_S_RegisterSound( "sound/weapons/knife/knife_hit3.wav" ); + cgs.media.sfx_knifehit[3] = trap_S_RegisterSound( "sound/weapons/knife/knife_hit4.wav" ); + + cgs.media.sfx_knifehit[4] = trap_S_RegisterSound( "sound/weapons/knife/knife_hitwall1.wav" ); // hitting wall + + cgs.media.sfx_bullet_metalhit[0] = trap_S_RegisterSound( "sound/weapons/bullethit_metal1.wav" ); + cgs.media.sfx_bullet_metalhit[1] = trap_S_RegisterSound( "sound/weapons/bullethit_metal2.wav" ); + cgs.media.sfx_bullet_metalhit[2] = trap_S_RegisterSound( "sound/weapons/bullethit_metal3.wav" ); + + cgs.media.sfx_bullet_woodhit[0] = trap_S_RegisterSound( "sound/weapons/bullethit_wood1.wav" ); + cgs.media.sfx_bullet_woodhit[1] = trap_S_RegisterSound( "sound/weapons/bullethit_wood2.wav" ); + cgs.media.sfx_bullet_woodhit[2] = trap_S_RegisterSound( "sound/weapons/bullethit_wood3.wav" ); + + cgs.media.sfx_bullet_roofhit[0] = trap_S_RegisterSound( "sound/weapons/bullethit_roof1.wav" ); + cgs.media.sfx_bullet_roofhit[1] = trap_S_RegisterSound( "sound/weapons/bullethit_roof2.wav" ); + cgs.media.sfx_bullet_roofhit[2] = trap_S_RegisterSound( "sound/weapons/bullethit_roof3.wav" ); + + cgs.media.sfx_bullet_ceramichit[0] = trap_S_RegisterSound( "sound/weapons/bullethit_ceramic1.wav" ); + cgs.media.sfx_bullet_ceramichit[1] = trap_S_RegisterSound( "sound/weapons/bullethit_ceramic2.wav" ); + cgs.media.sfx_bullet_ceramichit[2] = trap_S_RegisterSound( "sound/weapons/bullethit_ceramic3.wav" ); + + cgs.media.sfx_bullet_glasshit[0] = trap_S_RegisterSound( "sound/weapons/bullethit_glass1.wav" ); + cgs.media.sfx_bullet_glasshit[1] = trap_S_RegisterSound( "sound/weapons/bullethit_glass2.wav" ); + cgs.media.sfx_bullet_glasshit[2] = trap_S_RegisterSound( "sound/weapons/bullethit_glass3.wav" ); + + + cgs.media.sparkSounds[0] = trap_S_RegisterSound( "sound/world/saarc2.wav" ); + cgs.media.sparkSounds[1] = trap_S_RegisterSound( "sound/world/arc2.wav" ); + + +//----(SA) doors and kick + + //----(SA) removed some unnecessary stuff + + trap_S_RegisterSound( "sound/weapons/melee/fstatck.wav" ); + trap_S_RegisterSound( "sound/weapons/melee/fstmiss.wav" ); + + trap_S_RegisterSound( "sound/Loogie/spit.wav" ); + trap_S_RegisterSound( "sound/Loogie/sizzle.wav" ); + +} + + +//=================================================================================== + + + +/* +================= +CG_RegisterGraphics + +This function may execute for a couple of minutes with a slow disk. +================= +*/ +static void CG_RegisterGraphics( void ) { + char name[1024]; + + int i; + char items[MAX_ITEMS + 1]; + static char *sb_nums[11] = { + "gfx/2d/numbers/zero_32b", + "gfx/2d/numbers/one_32b", + "gfx/2d/numbers/two_32b", + "gfx/2d/numbers/three_32b", + "gfx/2d/numbers/four_32b", + "gfx/2d/numbers/five_32b", + "gfx/2d/numbers/six_32b", + "gfx/2d/numbers/seven_32b", + "gfx/2d/numbers/eight_32b", + "gfx/2d/numbers/nine_32b", + "gfx/2d/numbers/minus_32b", + }; + + + // clear any references to old media + memset( &cg.refdef, 0, sizeof( cg.refdef ) ); + trap_R_ClearScene(); + + CG_LoadingString( cgs.mapname ); + + trap_R_LoadWorldMap( cgs.mapname ); + + // precache status bar pics + CG_LoadingString( "game media" ); + + + CG_LoadingString( " - textures" ); + + for ( i = 0 ; i < 11 ; i++ ) { + cgs.media.numberShaders[i] = trap_R_RegisterShader( sb_nums[i] ); + } + + + cgs.media.smokePuffShader = trap_R_RegisterShader( "smokePuff" ); + + // Rafael - blood pool + //cgs.media.bloodPool = trap_R_RegisterShader ("bloodPool"); + + // RF, blood cloud + cgs.media.bloodCloudShader = trap_R_RegisterShader( "bloodCloud" ); + + // Rafael - cannon + cgs.media.smokePuffShaderdirty = trap_R_RegisterShader( "smokePuffdirty" ); + cgs.media.smokePuffShaderb1 = trap_R_RegisterShader( "smokePuffblack1" ); + cgs.media.smokePuffShaderb2 = trap_R_RegisterShader( "smokePuffblack2" ); + cgs.media.smokePuffShaderb3 = trap_R_RegisterShader( "smokePuffblack3" ); + cgs.media.smokePuffShaderb4 = trap_R_RegisterShader( "smokePuffblack4" ); + cgs.media.smokePuffShaderb5 = trap_R_RegisterShader( "smokePuffblack5" ); + // done + + // Rafael - bleedanim + for ( i = 0; i < 5; i++ ) { + cgs.media.viewBloodAni[i] = trap_R_RegisterShader( va( "viewBloodBlend%i", i + 1 ) ); + } + cgs.media.viewFlashBlood = trap_R_RegisterShader( "viewFlashBlood" ); + for ( i = 0; i < 16; i++ ) { + cgs.media.viewFlashFire[i] = trap_R_RegisterShader( va( "viewFlashFire%i", i + 1 ) ); + } + // done + + // Rafael bats + for ( i = 0; i < 10; i++ ) { + cgs.media.bats[i] = trap_R_RegisterShader( va( "bats%i",i + 1 ) ); + } + // done + + cgs.media.smokePuffRageProShader = trap_R_RegisterShader( "smokePuffRagePro" ); + cgs.media.shotgunSmokePuffShader = trap_R_RegisterShader( "shotgunSmokePuff" ); + + cgs.media.bloodTrailShader = trap_R_RegisterShader( "bloodTrail" ); + cgs.media.lagometerShader = trap_R_RegisterShader( "lagometer" ); + cgs.media.connectionShader = trap_R_RegisterShader( "disconnected" ); + + cgs.media.nailPuffShader = trap_R_RegisterShader( "nailtrail" ); + + +// cgs.media.reticleShaderSimple = trap_R_RegisterShader( "gfx/misc/reticlesimple" ); // TODO: remove + cgs.media.reticleShaderSimpleQ = trap_R_RegisterShader( "gfx/misc/reticlesimple_quarter" ); + + cgs.media.snooperShaderSimple = trap_R_RegisterShader( "gfx/misc/snoopersimple" ); + +// cgs.media.binocShaderSimple = trap_R_RegisterShader( "gfx/misc/binocsimple" ); // TODO: remove + cgs.media.binocShaderSimpleQ = trap_R_RegisterShader( "gfx/misc/binocsimple_quarter" ); //----(SA) added + + + // Rafael + // cgs.media.snowShader = trap_R_RegisterShader ( "snowPuff" ); + cgs.media.snowShader = trap_R_RegisterShader( "snow_tri" ); + + cgs.media.oilParticle = trap_R_RegisterShader( "oilParticle" ); + cgs.media.oilSlick = trap_R_RegisterShader( "oilSlick" ); + + cgs.media.waterBubbleShader = trap_R_RegisterShader( "waterBubble" ); + + cgs.media.tracerShader = trap_R_RegisterShader( "gfx/misc/tracer" ); + cgs.media.selectShader = trap_R_RegisterShader( "gfx/2d/select" ); + + + cgs.media.hintShaders[HINT_ACTIVATE] = trap_R_RegisterShader( "gfx/2d/usableHint" ); + cgs.media.hintShaders[HINT_NOACTIVATE] = trap_R_RegisterShader( "gfx/2d/notUsableHint" ); + cgs.media.hintShaders[HINT_DOOR] = trap_R_RegisterShader( "gfx/2d/doorHint" ); + cgs.media.hintShaders[HINT_DOOR_ROTATING] = trap_R_RegisterShader( "gfx/2d/doorRotateHint" ); + cgs.media.hintShaders[HINT_DOOR_LOCKED] = trap_R_RegisterShader( "gfx/2d/doorLockHint" ); + cgs.media.hintShaders[HINT_DOOR_ROTATING_LOCKED] = trap_R_RegisterShader( "gfx/2d/doorRotateLockHint" ); + cgs.media.hintShaders[HINT_MG42] = trap_R_RegisterShader( "gfx/2d/mg42Hint" ); + cgs.media.hintShaders[HINT_BREAKABLE] = trap_R_RegisterShader( "gfx/2d/breakableHint" ); + cgs.media.hintShaders[HINT_CHAIR] = trap_R_RegisterShader( "gfx/2d/chairHint" ); + cgs.media.hintShaders[HINT_ALARM] = trap_R_RegisterShader( "gfx/2d/alarmHint" ); + cgs.media.hintShaders[HINT_HEALTH] = trap_R_RegisterShader( "gfx/2d/healthHint" ); + cgs.media.hintShaders[HINT_TREASURE] = trap_R_RegisterShader( "gfx/2d/treasureHint" ); + cgs.media.hintShaders[HINT_KNIFE] = trap_R_RegisterShader( "gfx/2d/knifeHint" ); + cgs.media.hintShaders[HINT_LADDER] = trap_R_RegisterShader( "gfx/2d/ladderHint" ); + cgs.media.hintShaders[HINT_BUTTON] = trap_R_RegisterShader( "gfx/2d/buttonHint" ); + cgs.media.hintShaders[HINT_WATER] = trap_R_RegisterShader( "gfx/2d/waterHint" ); + cgs.media.hintShaders[HINT_CAUTION] = trap_R_RegisterShader( "gfx/2d/cautionHint" ); + cgs.media.hintShaders[HINT_DANGER] = trap_R_RegisterShader( "gfx/2d/dangerHint" ); + cgs.media.hintShaders[HINT_SECRET] = trap_R_RegisterShader( "gfx/2d/secretHint" ); + cgs.media.hintShaders[HINT_QUESTION] = trap_R_RegisterShader( "gfx/2d/questionHint" ); + cgs.media.hintShaders[HINT_EXCLAMATION] = trap_R_RegisterShader( "gfx/2d/exclamationHint" ); + cgs.media.hintShaders[HINT_CLIPBOARD] = trap_R_RegisterShader( "gfx/2d/clipboardHint" ); + cgs.media.hintShaders[HINT_WEAPON] = trap_R_RegisterShader( "gfx/2d/weaponHint" ); + cgs.media.hintShaders[HINT_AMMO] = trap_R_RegisterShader( "gfx/2d/ammoHint" ); + cgs.media.hintShaders[HINT_ARMOR] = trap_R_RegisterShader( "gfx/2d/armorHint" ); + cgs.media.hintShaders[HINT_POWERUP] = trap_R_RegisterShader( "gfx/2d/powerupHint" ); + cgs.media.hintShaders[HINT_HOLDABLE] = trap_R_RegisterShader( "gfx/2d/holdableHint" ); + cgs.media.hintShaders[HINT_INVENTORY] = trap_R_RegisterShader( "gfx/2d/inventoryHint" ); + cgs.media.hintShaders[HINT_EXIT] = trap_R_RegisterShader( "gfx/2d/exitHint" ); + cgs.media.hintShaders[HINT_NOEXIT] = cgs.media.hintShaders[HINT_EXIT]; + cgs.media.hintShaders[HINT_EXIT_FAR] = cgs.media.hintShaders[HINT_EXIT]; + cgs.media.hintShaders[HINT_NOEXIT_FAR] = cgs.media.hintShaders[HINT_EXIT]; + +// cgs.media.hintShaders[HINT_PLYR_FRIEND] = trap_R_RegisterShader( "gfx/2d/hintPlrFriend" ); +// cgs.media.hintShaders[HINT_PLYR_NEUTRAL] = trap_R_RegisterShader( "gfx/2d/hintPlrNeutral" ); +// cgs.media.hintShaders[HINT_PLYR_ENEMY] = trap_R_RegisterShader( "gfx/2d/hintPlrEnemy" ); +// cgs.media.hintShaders[HINT_PLYR_UNKNOWN] = trap_R_RegisterShader( "gfx/2d/hintPlrUnknown" ); + +// cgs.media.hintShaders[HINT_BUILD] = trap_R_RegisterShader( "gfx/2d/buildHint" ); // DHM - Nerve + + cgs.media.youGotMailShader = trap_R_RegisterShader( "gfx/2d/yougotmail" ); //----(SA) added + cgs.media.youGotObjectiveShader = trap_R_RegisterShader( "gfx/2d/yougotobjective" ); //----(SA) added + +//----(SA) end + + for ( i = 0 ; i < NUM_CROSSHAIRS ; i++ ) { + cgs.media.crosshairShader[i] = trap_R_RegisterShaderNoMip( va( "gfx/2d/crosshair%c", 'a' + i ) ); + } + + cgs.media.crosshairFriendly = trap_R_RegisterShader( "gfx/2d/friendlycross" ); //----(SA) added + + cgs.media.backTileShader = trap_R_RegisterShader( "gfx/2d/backtile" ); + cgs.media.noammoShader = trap_R_RegisterShader( "icons/noammo" ); + + // powerup shaders +// cgs.media.quadShader = trap_R_RegisterShader("powerups/quad" ); +// cgs.media.quadWeaponShader = trap_R_RegisterShader("powerups/quadWeapon" ); +// cgs.media.battleSuitShader = trap_R_RegisterShader("powerups/battleSuit" ); +// cgs.media.battleWeaponShader = trap_R_RegisterShader("powerups/battleWeapon" ); +// cgs.media.invisShader = trap_R_RegisterShader("powerups/invisibility" ); +// cgs.media.regenShader = trap_R_RegisterShader("powerups/regen" ); +// cgs.media.hastePuffShader = trap_R_RegisterShader("hasteSmokePuff" ); + + // DHM - Nerve :: Allow flags again, will change later to more appropriate models + if ( cgs.gametype == GT_CTF || cgs.gametype == GT_WOLF || cg_buildScript.integer ) { + cgs.media.redFlagModel = trap_R_RegisterModel( "models/flags/r_flag.md3" ); + //cgs.media.redFlagModel = trap_R_RegisterModel( "models/powerups/keys/chalice.md3" ); + cgs.media.blueFlagModel = trap_R_RegisterModel( "models/flags/b_flag.md3" ); + } + +// if ( cgs.gametype >= GT_TEAM || cg_buildScript.integer ) { +// cgs.media.friendShader = trap_R_RegisterShader( "sprites/foe" ); +// cgs.media.redQuadShader = trap_R_RegisterShader("powerups/blueflag" ); +// cgs.media.teamStatusBar = trap_R_RegisterShader( "gfx/2d/colorbar.tga" ); +// } + + CG_LoadingString( " - models" ); + + cgs.media.machinegunBrassModel = trap_R_RegisterModel( "models/weapons2/shells/m_shell.md3" ); + cgs.media.panzerfaustBrassModel = trap_R_RegisterModel( "models/weapons2/shells/pf_shell.md3" ); + cgs.media.smallgunBrassModel = trap_R_RegisterModel( "models/weapons2/shells/sm_shell.md3" ); + + cgs.media.debBlock[0] = trap_R_RegisterModel( "models/mapobjects/debris/brick1.md3" ); + cgs.media.debBlock[1] = trap_R_RegisterModel( "models/mapobjects/debris/brick2.md3" ); + cgs.media.debBlock[2] = trap_R_RegisterModel( "models/mapobjects/debris/brick3.md3" ); + cgs.media.debBlock[3] = trap_R_RegisterModel( "models/mapobjects/debris/brick4.md3" ); + cgs.media.debBlock[4] = trap_R_RegisterModel( "models/mapobjects/debris/brick5.md3" ); + cgs.media.debBlock[5] = trap_R_RegisterModel( "models/mapobjects/debris/brick6.md3" ); + + cgs.media.debRock[0] = trap_R_RegisterModel( "models/mapobjects/debris/rubble1.md3" ); + cgs.media.debRock[1] = trap_R_RegisterModel( "models/mapobjects/debris/rubble2.md3" ); + cgs.media.debRock[2] = trap_R_RegisterModel( "models/mapobjects/debris/rubble3.md3" ); + + + cgs.media.debWood[0] = trap_R_RegisterModel( "models/gibs/wood/wood1.md3" ); + cgs.media.debWood[1] = trap_R_RegisterModel( "models/gibs/wood/wood2.md3" ); + cgs.media.debWood[2] = trap_R_RegisterModel( "models/gibs/wood/wood3.md3" ); + cgs.media.debWood[3] = trap_R_RegisterModel( "models/gibs/wood/wood4.md3" ); + cgs.media.debWood[4] = trap_R_RegisterModel( "models/gibs/wood/wood5.md3" ); + cgs.media.debWood[5] = trap_R_RegisterModel( "models/gibs/wood/wood6.md3" ); +// cgs.media.debWoodl = trap_R_RegisterModel( "models/mapobjects/debris/woodxl.md3" ); +// cgs.media.debWoodm = trap_R_RegisterModel( "models/mapobjects/debris/woodm.md3" ); +// cgs.media.debWoods = trap_R_RegisterModel( "models/mapobjects/debris/woodsm.md3" ); + + cgs.media.debFabric[0] = trap_R_RegisterModel( "models/shards/fabric1.md3" ); + cgs.media.debFabric[1] = trap_R_RegisterModel( "models/shards/fabric2.md3" ); + cgs.media.debFabric[2] = trap_R_RegisterModel( "models/shards/fabric3.md3" ); + + //----(SA) end + +// cgs.media.medicReviveShader = trap_R_RegisterShader( "sprites/medic_revive" ); //----(SA) commented out from MP + cgs.media.balloonShader = trap_R_RegisterShader( "sprites/balloon3" ); + + for ( i = 0; i < MAX_AISTATES; i++ ) { + cgs.media.aiStateShaders[i] = trap_R_RegisterShader( va( "sprites/aistate%i", i + 1 ) ); + } + + cgs.media.bloodExplosionShader = trap_R_RegisterShader( "bloodExplosion" ); + + //cgs.media.bleedExplosionShader = trap_R_RegisterShader( "bleedExplosion" ); + + //----(SA) water splash + //cgs.media.waterSplashModel = trap_R_RegisterModel("models/weaphits/bullet.md3"); + //cgs.media.waterSplashShader = trap_R_RegisterShader( "waterSplash" ); + //----(SA) end + + //cgs.media.spearModel = trap_R_RegisterModel("models/weaphits/spear.md3"); //----(SA) + + //cgs.media.bulletFlashModel = trap_R_RegisterModel("models/weaphits/bullet.md3"); + //cgs.media.ringFlashModel = trap_R_RegisterModel("models/weaphits/ring02.md3"); + //cgs.media.dishFlashModel = trap_R_RegisterModel("models/weaphits/boom01.md3"); +// cgs.media.teleportEffectModel = trap_R_RegisterModel( "models/misc/telep.md3" ); +// cgs.media.teleportEffectShader = trap_R_RegisterShader( "teleportEffect" ); + +// cgs.media.batModel = trap_R_RegisterModel( "models/mapobjects/bat/bat.md3" ); + +// cgs.media.medalImpressive = trap_R_RegisterShaderNoMip( "medal_impressive" ); +// cgs.media.medalExcellent = trap_R_RegisterShaderNoMip( "medal_excellent" ); +// cgs.media.medalGauntlet = trap_R_RegisterShaderNoMip( "medal_gauntlet" ); + + // Ridah, spark particles + cgs.media.sparkParticleShader = trap_R_RegisterShader( "sparkParticle" ); + cgs.media.smokeTrailShader = trap_R_RegisterShader( "smokeTrail" ); +// cgs.media.fireTrailShader = trap_R_RegisterShader( "fireTrail" ); + cgs.media.lightningBoltShader = trap_R_RegisterShader( "lightningBolt" ); + //cgs.media.lightningBoltShaderGreen = trap_R_RegisterShader( "lightningBoltGreen" ); //----(SA) alternate lightning color + cgs.media.flamethrowerFireStream = trap_R_RegisterShader( "flamethrowerFireStream" ); + cgs.media.flamethrowerBlueStream = trap_R_RegisterShader( "flamethrowerBlueStream" ); + //cgs.media.flamethrowerFuelStream = trap_R_RegisterShader( "flamethrowerFuelStream" ); + //cgs.media.flamethrowerFuelShader = trap_R_RegisterShader( "flamethrowerFuel" ); + cgs.media.onFireShader2 = trap_R_RegisterShader( "entityOnFire1" ); + cgs.media.onFireShader = trap_R_RegisterShader( "entityOnFire2" ); + //cgs.media.dripWetShader2 = trap_R_RegisterShader( "dripWet2" ); + //cgs.media.dripWetShader = trap_R_RegisterShader( "dripWet1" ); + cgs.media.viewFadeBlack = trap_R_RegisterShader( "viewFadeBlack" ); + cgs.media.sparkFlareShader = trap_R_RegisterShader( "sparkFlareParticle" ); + + // spotlight + // shaders + cgs.media.spotLightShader = trap_R_RegisterShader( "spotLight" ); + cgs.media.spotLightBeamShader = trap_R_RegisterShader( "lightBeam" ); + + // models + cgs.media.spotLightBaseModel = trap_R_RegisterModel( "models/mapobjects/light/searchlight1_b.md3" ); + cgs.media.spotLightLightModel = trap_R_RegisterModel( "models/mapobjects/light/searchlight1_l.md3" ); + cgs.media.spotLightLightModelBroke = trap_R_RegisterModel( "models/mapobjects/light/searchlight_l_broke.md3" ); + + // end spotlight + + cgs.media.lightningHitWallShader = trap_R_RegisterShader( "lightningHitWall" ); + cgs.media.lightningWaveShader = trap_R_RegisterShader( "lightningWave" ); + cgs.media.bulletParticleTrailShader = trap_R_RegisterShader( "bulletParticleTrail" ); + cgs.media.smokeParticleShader = trap_R_RegisterShader( "smokeParticle" ); + + // DHM - Nerve :: bullet hitting dirt + cgs.media.dirtParticle1Shader = trap_R_RegisterShader( "dirt_splash" ); + cgs.media.dirtParticle2Shader = trap_R_RegisterShader( "water_splash" ); + //cgs.media.dirtParticle3Shader = trap_R_RegisterShader( "dirtParticle3" ); + + cgs.media.teslaDamageEffectShader = trap_R_RegisterShader( "teslaDamageEffect" ); + cgs.media.teslaAltDamageEffectShader = trap_R_RegisterShader( "teslaAltDamageEffect" ); + cgs.media.viewTeslaDamageEffectShader = trap_R_RegisterShader( "viewTeslaDamageEffect" ); + cgs.media.viewTeslaAltDamageEffectShader = trap_R_RegisterShader( "viewTeslaAltDamageEffect" ); + // done. + + cgs.media.railCoreShader = trap_R_RegisterShader( "railCore" ); // (SA) for debugging server traces + + + cgs.media.thirdPersonBinocModel = trap_R_RegisterModel( "models/powerups/holdable/binocs_thirdperson.md3" ); //----(SA) added + cgs.media.cigModel = trap_R_RegisterModel( "models/players/infantryss/acc/cig.md3" ); //----(SA) added + + // RF, not used anymore + //cgs.media.targetEffectExplosionShader = trap_R_RegisterShader( "targetEffectExplode" ); + //cgs.media.rocketExplosionShader = trap_R_RegisterShader( "rocketExplosion" ); + //cgs.media.grenadeExplosionShader = trap_R_RegisterShader( "grenadeExplosion" ); + + // zombie shot + //cgs.media.zombieLoogie = trap_R_RegisterModel( "models/mapobjects/bodyparts/zom_loog.md3" ); + cgs.media.flamebarrel = trap_R_RegisterModel( "models/furniture/barrel/barrel_a.md3" ); + //----(SA) end + + cgs.media.mg42muzzleflash = trap_R_RegisterModel( "models/weapons2/machinegun/mg42_flash.md3" ); + // cgs.media.mg42muzzleflashgg = trap_R_RegisterModel ("models/weapons2/machinegun/mg42_flash_gg.md3" ); + + cgs.media.planemuzzleflash = trap_R_RegisterModel( "models/mapobjects/vehicles/gunflare.md3" ); + + cgs.media.crowbar = trap_R_RegisterModel( "models/weapons2/wrench/wrench.md3" ); + + // Rafael shards + cgs.media.shardGlass1 = trap_R_RegisterModel( "models/shards/glass1.md3" ); + cgs.media.shardGlass2 = trap_R_RegisterModel( "models/shards/glass2.md3" ); + cgs.media.shardWood1 = trap_R_RegisterModel( "models/shards/wood1.md3" ); + cgs.media.shardWood2 = trap_R_RegisterModel( "models/shards/wood2.md3" ); + cgs.media.shardMetal1 = trap_R_RegisterModel( "models/shards/metal1.md3" ); + cgs.media.shardMetal2 = trap_R_RegisterModel( "models/shards/metal2.md3" ); + cgs.media.shardCeramic1 = trap_R_RegisterModel( "models/shards/ceramic1.md3" ); + cgs.media.shardCeramic2 = trap_R_RegisterModel( "models/shards/ceramic2.md3" ); + // done + + cgs.media.shardRubble1 = trap_R_RegisterModel( "models/mapobjects/debris/brick000.md3" ); + cgs.media.shardRubble2 = trap_R_RegisterModel( "models/mapobjects/debris/brick001.md3" ); + cgs.media.shardRubble3 = trap_R_RegisterModel( "models/mapobjects/debris/brick002.md3" ); + + for ( i = 0; i < MAX_LOCKER_DEBRIS; i++ ) + { + Com_sprintf( name, sizeof( name ), "models/mapobjects/debris/personal%i.md3", i + 1 ); + cgs.media.shardJunk[i] = trap_R_RegisterModel( name ); + } + + memset( cg_items, 0, sizeof( cg_items ) ); + memset( cg_weapons, 0, sizeof( cg_weapons ) ); + + CG_LoadTranslateStrings(); //----(SA) added. for localization, read on-screen print names from text file + +// TODO: FIXME: REMOVE REGISTRATION OF EACH MODEL FOR EVERY LEVEL LOAD + + + //----(SA) okay, new stuff to intialize rather than doing it at level load time (or "give all" time) + // (I'm certainly not against being efficient here, but I'm tired of the rocket launcher effect only registering + // sometimes and want it to work for sure for this demo) + +/////////// +// code is almost complete for doing this correctly. will remove when that is complete. + CG_LoadingString( " - weapons" ); + for ( i = WP_KNIFE; i < WP_GAUNTLET; i++ ) { +// CG_LoadingString( va(" - %d", i) ); + CG_RegisterWeapon( i ); + } +/////////// +// END + + + // only register the items that the server says we need + strcpy( items, CG_ConfigString( CS_ITEMS ) ); + + CG_LoadingString( " - items" ); + + for ( i = 1 ; i < bg_numItems ; i++ ) { + if ( items[ i ] == '1' || cg_buildScript.integer ) { + +// TODO: get weapons added to the list that are 'set' from a script + CG_LoadingItem( i ); + CG_RegisterItemVisuals( i ); + } + } + + // wall marks + cgs.media.bulletMarkShader = trap_R_RegisterShader( "gfx/damage/bullet_mrk" ); + cgs.media.burnMarkShader = trap_R_RegisterShader( "gfx/damage/burn_med_mrk" ); + cgs.media.holeMarkShader = trap_R_RegisterShader( "gfx/damage/hole_lg_mrk" ); + cgs.media.shadowMarkShader = trap_R_RegisterShader( "markShadow" ); + cgs.media.shadowFootShader = trap_R_RegisterShader( "markShadowFoot" ); + cgs.media.shadowTorsoShader = trap_R_RegisterShader( "markShadowTorso" ); + cgs.media.wakeMarkShader = trap_R_RegisterShader( "wake" ); + cgs.media.wakeMarkShaderAnim = trap_R_RegisterShader( "wakeAnim" ); // (SA) + + cgs.media.bulletMarkShaderMetal = trap_R_RegisterShader( "gfx/damage/metal_mrk" ); + cgs.media.bulletMarkShaderWood = trap_R_RegisterShader( "gfx/damage/wood_mrk" ); + cgs.media.bulletMarkShaderCeramic = trap_R_RegisterShader( "gfx/damage/ceramic_mrk" ); + cgs.media.bulletMarkShaderGlass = trap_R_RegisterShader( "gfx/damage/glass_mrk" ); + + for ( i = 0 ; i < 5 ; i++ ) { + char name[32]; + //Com_sprintf( name, sizeof(name), "textures/decals/blood%i", i+1 ); + //cgs.media.bloodMarkShaders[i] = trap_R_RegisterShader( name ); + Com_sprintf( name, sizeof( name ), "blood_dot%i", i + 1 ); + cgs.media.bloodDotShaders[i] = trap_R_RegisterShader( name ); + } + + CG_LoadingString( " - inline models" ); + + // register the inline models + cgs.numInlineModels = trap_CM_NumInlineModels(); + for ( i = 1 ; i < cgs.numInlineModels ; i++ ) { + char name[10]; + vec3_t mins, maxs; + int j; + + Com_sprintf( name, sizeof( name ), "*%i", i ); + cgs.inlineDrawModel[i] = trap_R_RegisterModel( name ); + trap_R_ModelBounds( cgs.inlineDrawModel[i], mins, maxs ); + for ( j = 0 ; j < 3 ; j++ ) { + cgs.inlineModelMidpoints[i][j] = mins[j] + 0.5 * ( maxs[j] - mins[j] ); + } + } + + CG_LoadingString( " - server models" ); + + // register all the server specified models + for ( i = 1 ; i < MAX_MODELS ; i++ ) { + const char *modelName; + + modelName = CG_ConfigString( CS_MODELS + i ); + if ( !modelName[0] ) { + break; + } + cgs.gameModels[i] = trap_R_RegisterModel( modelName ); + } + + CG_LoadingString( " - particles" ); + CG_ClearParticles(); + + for ( i = 1; i < MAX_PARTICLES_AREAS; i++ ) + { + { + int rval; + + rval = CG_NewParticleArea( CS_PARTICLES + i ); + if ( !rval ) { + break; + } + } + } + +// cgs.media.cursor = trap_R_RegisterShaderNoMip( "menu/art/3_cursor2" ); + cgs.media.sizeCursor = trap_R_RegisterShaderNoMip( "ui/assets/sizecursor.tga" ); + cgs.media.selectCursor = trap_R_RegisterShaderNoMip( "ui/assets/selectcursor.tga" ); + CG_LoadingString( " - game media done" ); + +} + +/* +=================== +CG_RegisterClients + +=================== +*/ +static void CG_RegisterClients( void ) { + int i; + + for ( i = 0 ; i < MAX_CLIENTS ; i++ ) { + const char *clientInfo; + + clientInfo = CG_ConfigString( CS_PLAYERS + i ); + if ( !clientInfo[0] ) { + continue; + } + CG_LoadingClient( i ); + CG_NewClientInfo( i ); + } +} + +//=========================================================================== + +/* +================= +CG_ConfigString +================= +*/ +const char *CG_ConfigString( int index ) { + if ( index < 0 || index >= MAX_CONFIGSTRINGS ) { + CG_Error( "CG_ConfigString: bad index: %i", index ); + } + return cgs.gameState.stringData + cgs.gameState.stringOffsets[ index ]; +} + +//================================================================== + +/* +====================== +CG_StartMusic + +====================== +*/ +void CG_StartMusic( void ) { + char *s; + char parm1[MAX_QPATH], parm2[MAX_QPATH]; + + // start the background music + s = (char *)CG_ConfigString( CS_MUSIC ); + Q_strncpyz( parm1, COM_Parse( &s ), sizeof( parm1 ) ); + Q_strncpyz( parm2, COM_Parse( &s ), sizeof( parm2 ) ); + + if ( strlen( parm1 ) ) { + trap_S_StartBackgroundTrack( parm1, parm2, 0 ); + } +} + +//----(SA) added +/* +============== +CG_QueueMusic +============== +*/ +void CG_QueueMusic( void ) { + char *s; + char parm[MAX_QPATH]; + + // prepare the next background track + s = (char *)CG_ConfigString( CS_MUSIC_QUEUE ); + Q_strncpyz( parm, COM_Parse( &s ), sizeof( parm ) ); + + // even if no strlen(parm). we want to be able to clear the queue + + // TODO: \/ the values stored in here will be made accessable so + // it doesn't have to go through startbackgroundtrack() (which is stupid) + trap_S_StartBackgroundTrack( parm, "", -2 ); // '-2' for 'queue looping track' (QUEUED_PLAY_LOOPED) +} + +//----(SA) end + + +char *CG_GetMenuBuffer( const char *filename ) { + int len; + fileHandle_t f; + static char buf[MAX_MENUFILE]; + + len = trap_FS_FOpenFile( filename, &f, FS_READ ); + if ( !f ) { + trap_Print( va( S_COLOR_RED "menu file not found: %s, using default\n", filename ) ); + return NULL; + } + if ( len >= MAX_MENUFILE ) { + trap_Print( va( S_COLOR_RED "menu file too large: %s is %i, max allowed is %i", filename, len, MAX_MENUFILE ) ); + trap_FS_FCloseFile( f ); + return NULL; + } + + trap_FS_Read( buf, len, f ); + buf[len] = 0; + trap_FS_FCloseFile( f ); + + return buf; +} + +// +// ============================== +// new hud stuff ( mission pack ) +// ============================== +// +qboolean CG_Asset_Parse( int handle ) { + pc_token_t token; + const char *tempStr; + + if ( !trap_PC_ReadToken( handle, &token ) ) { + return qfalse; + } + if ( Q_stricmp( token.string, "{" ) != 0 ) { + return qfalse; + } + + while ( 1 ) { + if ( !trap_PC_ReadToken( handle, &token ) ) { + return qfalse; + } + + if ( Q_stricmp( token.string, "}" ) == 0 ) { + return qtrue; + } + + // font + if ( Q_stricmp( token.string, "font" ) == 0 ) { + int pointSize; + if ( !PC_String_Parse( handle, &tempStr ) || !PC_Int_Parse( handle, &pointSize ) ) { + return qfalse; + } + cgDC.registerFont( tempStr, pointSize, &cgDC.Assets.textFont ); + continue; + } + + // smallFont + if ( Q_stricmp( token.string, "smallFont" ) == 0 ) { + int pointSize; + if ( !PC_String_Parse( handle, &tempStr ) || !PC_Int_Parse( handle, &pointSize ) ) { + return qfalse; + } + cgDC.registerFont( tempStr, pointSize, &cgDC.Assets.smallFont ); + continue; + } + + // font + if ( Q_stricmp( token.string, "bigfont" ) == 0 ) { + int pointSize; + if ( !PC_String_Parse( handle, &tempStr ) || !PC_Int_Parse( handle, &pointSize ) ) { + return qfalse; + } + cgDC.registerFont( tempStr, pointSize, &cgDC.Assets.bigFont ); + continue; + } + + // handwriting + if ( Q_stricmp( token.string, "handwritingFont" ) == 0 ) { + int pointSize; + if ( !PC_String_Parse( handle, &tempStr ) || !PC_Int_Parse( handle, &pointSize ) ) { + return qfalse; + } + cgDC.registerFont( tempStr, pointSize, &cgDC.Assets.handwritingFont ); + continue; + } + + // gradientbar + if ( Q_stricmp( token.string, "gradientbar" ) == 0 ) { + if ( !PC_String_Parse( handle, &tempStr ) ) { + return qfalse; + } + cgDC.Assets.gradientBar = trap_R_RegisterShaderNoMip( tempStr ); + continue; + } + + // enterMenuSound + if ( Q_stricmp( token.string, "menuEnterSound" ) == 0 ) { + if ( !PC_String_Parse( handle, &tempStr ) ) { + return qfalse; + } + cgDC.Assets.menuEnterSound = trap_S_RegisterSound( tempStr ); + continue; + } + + // exitMenuSound + if ( Q_stricmp( token.string, "menuExitSound" ) == 0 ) { + if ( !PC_String_Parse( handle, &tempStr ) ) { + return qfalse; + } + cgDC.Assets.menuExitSound = trap_S_RegisterSound( tempStr ); + continue; + } + + // itemFocusSound + if ( Q_stricmp( token.string, "itemFocusSound" ) == 0 ) { + if ( !PC_String_Parse( handle, &tempStr ) ) { + return qfalse; + } + cgDC.Assets.itemFocusSound = trap_S_RegisterSound( tempStr ); + continue; + } + + // menuBuzzSound + if ( Q_stricmp( token.string, "menuBuzzSound" ) == 0 ) { + if ( !PC_String_Parse( handle, &tempStr ) ) { + return qfalse; + } + cgDC.Assets.menuBuzzSound = trap_S_RegisterSound( tempStr ); + continue; + } + + if ( Q_stricmp( token.string, "cursor" ) == 0 ) { + if ( !PC_String_Parse( handle, &cgDC.Assets.cursorStr ) ) { + return qfalse; + } + cgDC.Assets.cursor = trap_R_RegisterShaderNoMip( cgDC.Assets.cursorStr ); + continue; + } + + if ( Q_stricmp( token.string, "fadeClamp" ) == 0 ) { + if ( !PC_Float_Parse( handle, &cgDC.Assets.fadeClamp ) ) { + return qfalse; + } + continue; + } + + if ( Q_stricmp( token.string, "fadeCycle" ) == 0 ) { + if ( !PC_Int_Parse( handle, &cgDC.Assets.fadeCycle ) ) { + return qfalse; + } + continue; + } + + if ( Q_stricmp( token.string, "fadeAmount" ) == 0 ) { + if ( !PC_Float_Parse( handle, &cgDC.Assets.fadeAmount ) ) { + return qfalse; + } + continue; + } + + if ( Q_stricmp( token.string, "shadowX" ) == 0 ) { + if ( !PC_Float_Parse( handle, &cgDC.Assets.shadowX ) ) { + return qfalse; + } + continue; + } + + if ( Q_stricmp( token.string, "shadowY" ) == 0 ) { + if ( !PC_Float_Parse( handle, &cgDC.Assets.shadowY ) ) { + return qfalse; + } + continue; + } + + if ( Q_stricmp( token.string, "shadowColor" ) == 0 ) { + if ( !PC_Color_Parse( handle, &cgDC.Assets.shadowColor ) ) { + return qfalse; + } + cgDC.Assets.shadowFadeClamp = cgDC.Assets.shadowColor[3]; + continue; + } + } + //return qfalse; +} + +void CG_ParseMenu( const char *menuFile ) { + pc_token_t token; + int handle; + + handle = trap_PC_LoadSource( menuFile ); + if ( !handle ) { + handle = trap_PC_LoadSource( "ui/testhud.menu" ); + } + if ( !handle ) { + return; + } + + while ( 1 ) { + if ( !trap_PC_ReadToken( handle, &token ) ) { + break; + } + + //if ( Q_stricmp( token, "{" ) ) { + // Com_Printf( "Missing { in menu file\n" ); + // break; + //} + + //if ( menuCount == MAX_MENUS ) { + // Com_Printf( "Too many menus!\n" ); + // break; + //} + + if ( token.string[0] == '}' ) { + break; + } + + if ( Q_stricmp( token.string, "assetGlobalDef" ) == 0 ) { + if ( CG_Asset_Parse( handle ) ) { + continue; + } else { + break; + } + } + + + if ( Q_stricmp( token.string, "menudef" ) == 0 ) { + // start a new menu + Menu_New( handle ); + } + } + trap_PC_FreeSource( handle ); +} + +qboolean CG_Load_Menu( char **p ) { + char *token; + + token = COM_ParseExt( p, qtrue ); + + if ( token[0] != '{' ) { + return qfalse; + } + + while ( 1 ) { + + token = COM_ParseExt( p, qtrue ); + + if ( Q_stricmp( token, "}" ) == 0 ) { + return qtrue; + } + + if ( !token || token[0] == 0 ) { + return qfalse; + } + + CG_ParseMenu( token ); + } + return qfalse; +} + + + +void CG_LoadMenus( const char *menuFile ) { + char *token; + char *p; + int len, start; + fileHandle_t f; + static char buf[MAX_MENUDEFFILE]; + + start = trap_Milliseconds(); + + len = trap_FS_FOpenFile( menuFile, &f, FS_READ ); + if ( !f ) { + trap_Error( va( S_COLOR_YELLOW "menu file not found: %s, using default\n", menuFile ) ); + len = trap_FS_FOpenFile( "ui/hud.txt", &f, FS_READ ); + if ( !f ) { + trap_Error( va( S_COLOR_RED "default menu file not found: ui/hud.txt, unable to continue!\n", menuFile ) ); + } + } + + if ( len >= MAX_MENUDEFFILE ) { + trap_Error( va( S_COLOR_RED "menu file too large: %s is %i, max allowed is %i", menuFile, len, MAX_MENUDEFFILE ) ); + trap_FS_FCloseFile( f ); + return; + } + + trap_FS_Read( buf, len, f ); + buf[len] = 0; + trap_FS_FCloseFile( f ); + + COM_Compress( buf ); + + Menu_Reset(); + + p = buf; + + while ( 1 ) { + token = COM_ParseExt( &p, qtrue ); + if ( !token || token[0] == 0 || token[0] == '}' ) { + break; + } + + //if ( Q_stricmp( token, "{" ) ) { + // Com_Printf( "Missing { in menu file\n" ); + // break; + //} + + //if ( menuCount == MAX_MENUS ) { + // Com_Printf( "Too many menus!\n" ); + // break; + //} + + if ( Q_stricmp( token, "}" ) == 0 ) { + break; + } + + if ( Q_stricmp( token, "loadmenu" ) == 0 ) { + if ( CG_Load_Menu( &p ) ) { + continue; + } else { + break; + } + } + } + + Com_Printf( "UI menu load time = %d milli seconds\n", trap_Milliseconds() - start ); + +} + + + +static qboolean CG_OwnerDrawHandleKey( int ownerDraw, int flags, float *special, int key ) { + return qfalse; +} + + +static int CG_FeederCount( float feederID ) { + int i, count; + count = 0; + if ( feederID == FEEDER_REDTEAM_LIST ) { + for ( i = 0; i < cg.numScores; i++ ) { + if ( cg.scores[i].team == TEAM_RED ) { + count++; + } + } + } else if ( feederID == FEEDER_BLUETEAM_LIST ) { + for ( i = 0; i < cg.numScores; i++ ) { + if ( cg.scores[i].team == TEAM_BLUE ) { + count++; + } + } + } else if ( feederID == FEEDER_SCOREBOARD ) { + return cg.numScores; + } + return count; +} + + + + +/////////////////////////// +/////////////////////////// + +static clientInfo_t * CG_InfoFromScoreIndex( int index, int team, int *scoreIndex ) { + int i, count; + if ( cgs.gametype >= GT_TEAM ) { + count = 0; + for ( i = 0; i < cg.numScores; i++ ) { + if ( cg.scores[i].team == team ) { + if ( count == index ) { + *scoreIndex = i; + return &cgs.clientinfo[cg.scores[i].client]; + } + count++; + } + } + } + *scoreIndex = index; + return &cgs.clientinfo[ cg.scores[index].client ]; +} + +static const char *CG_FeederItemText( float feederID, int index, int column, qhandle_t *handle ) { +#ifdef MISSIONPACK + gitem_t *item; +#endif // #ifdef MISSIONPACK + int scoreIndex = 0; + clientInfo_t *info = NULL; + int team = -1; + score_t *sp = NULL; + + *handle = -1; + + if ( feederID == FEEDER_REDTEAM_LIST ) { + team = TEAM_RED; + } else if ( feederID == FEEDER_BLUETEAM_LIST ) { + team = TEAM_BLUE; + } + + info = CG_InfoFromScoreIndex( index, team, &scoreIndex ); + sp = &cg.scores[scoreIndex]; + + if ( info && info->infoValid ) { + switch ( column ) { + case 0: +#ifdef MISSIONPACK + if ( info->powerups & ( 1 << PW_NEUTRALFLAG ) ) { + item = BG_FindItemForPowerup( PW_NEUTRALFLAG ); + *handle = cg_items[ ITEM_INDEX( item ) ].icon; + } else if ( info->powerups & ( 1 << PW_REDFLAG ) ) { + item = BG_FindItemForPowerup( PW_REDFLAG ); + *handle = cg_items[ ITEM_INDEX( item ) ].icon; + } else if ( info->powerups & ( 1 << PW_BLUEFLAG ) ) { + item = BG_FindItemForPowerup( PW_BLUEFLAG ); + *handle = cg_items[ ITEM_INDEX( item ) ].icon; + } else { + if ( info->botSkill > 0 && info->botSkill <= 5 ) { + *handle = cgs.media.botSkillShaders[ info->botSkill - 1 ]; + } else if ( info->handicap < 100 ) { + return va( "%i", info->handicap ); + } + } + break; + case 1: + if ( team == -1 ) { + return ""; + } else { + *handle = CG_StatusHandle( info->teamTask ); + } + break; + case 2: + if ( cg.snap->ps.stats[ STAT_CLIENTS_READY ] & ( 1 << sp->client ) ) { + return "Ready"; + } + if ( team == -1 ) { + if ( cgs.gametype == GT_TOURNAMENT ) { + return va( "%i/%i", info->wins, info->losses ); + } else if ( info->infoValid && info->team == TEAM_SPECTATOR ) { + return "Spectator"; + } else { + return ""; + } + } else { + if ( info->teamLeader ) { + return "Leader"; + } + } +#endif // #ifdef MISSIONPACK + break; + case 3: + return info->name; + break; + case 4: + return va( "%i", info->score ); + break; + case 5: + return va( "%4i", sp->time ); + break; + case 6: + if ( sp->ping == -1 ) { + return "connecting"; + } + return va( "%4i", sp->ping ); + break; + } + } + + return ""; +} + +static qhandle_t CG_FeederItemImage( float feederID, int index ) { + return 0; +} + +static void CG_FeederSelection( float feederID, int index ) { + if ( cgs.gametype >= GT_TEAM ) { + int i, count; + int team = ( feederID == FEEDER_REDTEAM_LIST ) ? TEAM_RED : TEAM_BLUE; + count = 0; + for ( i = 0; i < cg.numScores; i++ ) { + if ( cg.scores[i].team == team ) { + if ( index == count ) { + cg.selectedScore = i; + } + count++; + } + } + } else { + cg.selectedScore = index; + } +} + +static float CG_Cvar_Get( const char *cvar ) { + char buff[128]; + memset( buff, 0, sizeof( buff ) ); + trap_Cvar_VariableStringBuffer( cvar, buff, sizeof( buff ) ); + return atof( buff ); +} + +void CG_Text_PaintWithCursor( float x, float y, int font, float scale, vec4_t color, const char *text, int cursorPos, char cursor, int limit, int style ) { + CG_Text_Paint( x, y, font, scale, color, text, 0, limit, style ); +} + +static int CG_OwnerDrawWidth( int ownerDraw, int font, float scale ) { + switch ( ownerDraw ) { + case CG_GAME_TYPE: + return CG_Text_Width( CG_GameTypeString(), font, scale, 0 ); + case CG_GAME_STATUS: + return CG_Text_Width( CG_GetGameStatusText(), font, scale, 0 ); + break; + case CG_KILLER: + return CG_Text_Width( CG_GetKillerText(), font, scale, 0 ); + break; +#ifdef MISSIONPACK + case CG_RED_NAME: + return CG_Text_Width( cg_redTeamName.string, font, scale, 0 ); + break; + case CG_BLUE_NAME: + return CG_Text_Width( cg_blueTeamName.string, font, scale, 0 ); + break; +#endif + + } + return 0; +} + +static int CG_PlayCinematic( const char *name, float x, float y, float w, float h ) { + return trap_CIN_PlayCinematic( name, x, y, w, h, CIN_loop ); +} + +static void CG_StopCinematic( int handle ) { + trap_CIN_StopCinematic( handle ); +} + +static void CG_DrawCinematic( int handle, float x, float y, float w, float h ) { + trap_CIN_SetExtents( handle, x, y, w, h ); + trap_CIN_DrawCinematic( handle ); +} + +static void CG_RunCinematicFrame( int handle ) { + trap_CIN_RunCinematic( handle ); +} + + + +/* +============== +CG_translateString + presumably if this gets used more extensively, it'll be modified to a hash table +============== +*/ +const char *CG_translateString( const char *str ) { + int i, numStrings; + + numStrings = sizeof( translateStrings ) / sizeof( translateStrings[0] ) - 1; + + for ( i = 0; i < numStrings; i++ ) { + if ( !translateStrings[i].name || !strlen( translateStrings[i].name ) ) { + return str; + } + + if ( !strcmp( str, translateStrings[i].name ) ) { + if ( translateStrings[i].localname && strlen( translateStrings[i].localname ) ) { + return translateStrings[i].localname; + } + break; + } + } + + return str; +} + +/* +================= +CG_LoadHudMenu(); + +================= +*/ +void CG_LoadHudMenu() { + char buff[1024]; + const char *hudSet; + + cgDC.registerShaderNoMip = &trap_R_RegisterShaderNoMip; + cgDC.setColor = &trap_R_SetColor; + cgDC.drawHandlePic = &CG_DrawPic; + cgDC.drawStretchPic = &trap_R_DrawStretchPic; + cgDC.drawText = &CG_Text_Paint; + cgDC.textWidth = &CG_Text_Width; + cgDC.textHeight = &CG_Text_Height; + cgDC.registerModel = &trap_R_RegisterModel; + cgDC.modelBounds = &trap_R_ModelBounds; + cgDC.fillRect = &CG_FillRect; + cgDC.drawRect = &CG_DrawRect; + cgDC.drawSides = &CG_DrawSides; + cgDC.drawTopBottom = &CG_DrawTopBottom; + cgDC.clearScene = &trap_R_ClearScene; + cgDC.addRefEntityToScene = &trap_R_AddRefEntityToScene; + cgDC.renderScene = &trap_R_RenderScene; + cgDC.registerFont = &trap_R_RegisterFont; + cgDC.ownerDrawItem = &CG_OwnerDraw; + cgDC.getValue = &CG_GetValue; + cgDC.ownerDrawVisible = &CG_OwnerDrawVisible; + cgDC.runScript = &CG_RunMenuScript; + cgDC.getTeamColor = &CG_GetTeamColor; + cgDC.setCVar = trap_Cvar_Set; + cgDC.getCVarString = trap_Cvar_VariableStringBuffer; + cgDC.getCVarValue = CG_Cvar_Get; + cgDC.drawTextWithCursor = &CG_Text_PaintWithCursor; + //cgDC.setOverstrikeMode = &trap_Key_SetOverstrikeMode; + //cgDC.getOverstrikeMode = &trap_Key_GetOverstrikeMode; + cgDC.startLocalSound = &trap_S_StartLocalSound; + cgDC.ownerDrawHandleKey = &CG_OwnerDrawHandleKey; + cgDC.feederCount = &CG_FeederCount; + cgDC.feederItemImage = &CG_FeederItemImage; + cgDC.feederItemText = &CG_FeederItemText; + cgDC.feederSelection = &CG_FeederSelection; + //cgDC.setBinding = &trap_Key_SetBinding; + //cgDC.getBindingBuf = &trap_Key_GetBindingBuf; + //cgDC.keynumToStringBuf = &trap_Key_KeynumToStringBuf; + //cgDC.executeText = &trap_Cmd_ExecuteText; + + cgDC.getTranslatedString = &CG_translateString; //----(SA) added + + cgDC.Error = &Com_Error; + cgDC.Print = &Com_Printf; + cgDC.ownerDrawWidth = &CG_OwnerDrawWidth; + //cgDC.Pause = &CG_Pause; + cgDC.registerSound = &trap_S_RegisterSound; + cgDC.startBackgroundTrack = &trap_S_StartBackgroundTrack; + cgDC.stopBackgroundTrack = &trap_S_StopBackgroundTrack; + cgDC.playCinematic = &CG_PlayCinematic; + cgDC.stopCinematic = &CG_StopCinematic; + cgDC.drawCinematic = &CG_DrawCinematic; + cgDC.runCinematicFrame = &CG_RunCinematicFrame; + + Init_Display( &cgDC ); + + Menu_Reset(); + + trap_Cvar_VariableStringBuffer( "cg_hudFiles", buff, sizeof( buff ) ); + hudSet = buff; + if ( hudSet[0] == '\0' ) { + hudSet = "ui/hud.txt"; + } + + CG_LoadMenus( hudSet ); +} + +void CG_AssetCache() { + //if (Assets.textFont == NULL) { + // trap_R_RegisterFont("fonts/arial.ttf", 72, &Assets.textFont); + //} + //Assets.background = trap_R_RegisterShaderNoMip( ASSET_BACKGROUND ); + //Com_Printf("Menu Size: %i bytes\n", sizeof(Menus)); + cgDC.Assets.gradientBar = trap_R_RegisterShaderNoMip( ASSET_GRADIENTBAR ); + cgDC.Assets.fxBasePic = trap_R_RegisterShaderNoMip( ART_FX_BASE ); + cgDC.Assets.fxPic[0] = trap_R_RegisterShaderNoMip( ART_FX_RED ); + cgDC.Assets.fxPic[1] = trap_R_RegisterShaderNoMip( ART_FX_YELLOW ); + cgDC.Assets.fxPic[2] = trap_R_RegisterShaderNoMip( ART_FX_GREEN ); + cgDC.Assets.fxPic[3] = trap_R_RegisterShaderNoMip( ART_FX_TEAL ); + cgDC.Assets.fxPic[4] = trap_R_RegisterShaderNoMip( ART_FX_BLUE ); + cgDC.Assets.fxPic[5] = trap_R_RegisterShaderNoMip( ART_FX_CYAN ); + cgDC.Assets.fxPic[6] = trap_R_RegisterShaderNoMip( ART_FX_WHITE ); + cgDC.Assets.scrollBar = trap_R_RegisterShaderNoMip( ASSET_SCROLLBAR ); + cgDC.Assets.scrollBarArrowDown = trap_R_RegisterShaderNoMip( ASSET_SCROLLBAR_ARROWDOWN ); + cgDC.Assets.scrollBarArrowUp = trap_R_RegisterShaderNoMip( ASSET_SCROLLBAR_ARROWUP ); + cgDC.Assets.scrollBarArrowLeft = trap_R_RegisterShaderNoMip( ASSET_SCROLLBAR_ARROWLEFT ); + cgDC.Assets.scrollBarArrowRight = trap_R_RegisterShaderNoMip( ASSET_SCROLLBAR_ARROWRIGHT ); + cgDC.Assets.scrollBarThumb = trap_R_RegisterShaderNoMip( ASSET_SCROLL_THUMB ); + cgDC.Assets.sliderBar = trap_R_RegisterShaderNoMip( ASSET_SLIDER_BAR ); + cgDC.Assets.sliderThumb = trap_R_RegisterShaderNoMip( ASSET_SLIDER_THUMB ); +} + + +/* +================= +CG_Init + +Called after every level change or subsystem restart +Will perform callbacks to make the loading info screen update. +================= +*/ +void CG_Init( int serverMessageNum, int serverCommandSequence ) { + const char *s; + + // clear everything + memset( &cgs, 0, sizeof( cgs ) ); + memset( &cg, 0, sizeof( cg ) ); + memset( cg_entities, 0, sizeof( cg_entities ) ); + memset( cg_weapons, 0, sizeof( cg_weapons ) ); + memset( cg_items, 0, sizeof( cg_items ) ); + + // RF, init the anim scripting + cgs.animScriptData.soundIndex = CG_SoundScriptPrecache; + cgs.animScriptData.playSound = CG_SoundPlayIndexedScript; + + cgs.processedSnapshotNum = serverMessageNum; + cgs.serverCommandSequence = serverCommandSequence; + + // load a few needed things before we do any screen updates + // (SA) using Nerve's text since they have foreign characters + cgs.media.charsetShader = trap_R_RegisterShader( "gfx/2d/hudchars" ); //trap_R_RegisterShader( "gfx/2d/bigchars" ); + // JOSEPH 4-17-00 + cgs.media.menucharsetShader = trap_R_RegisterShader( "gfx/2d/hudchars" ); + // END JOSEPH + cgs.media.whiteShader = trap_R_RegisterShader( "white" ); + cgs.media.charsetProp = trap_R_RegisterShaderNoMip( "menu/art/font1_prop.tga" ); + cgs.media.charsetPropGlow = trap_R_RegisterShaderNoMip( "menu/art/font1_prop_glo.tga" ); + cgs.media.charsetPropB = trap_R_RegisterShaderNoMip( "menu/art/font2_prop.tga" ); + + CG_RegisterCvars(); + + CG_InitConsoleCommands(); + +// cg.weaponSelect = WP_MP40; + + // get the rendering configuration from the client system + trap_GetGlconfig( &cgs.glconfig ); + cgs.screenXScale = cgs.glconfig.vidWidth / 640.0; + cgs.screenYScale = cgs.glconfig.vidHeight / 480.0; + + // get the gamestate from the client system + trap_GetGameState( &cgs.gameState ); + + // check version + s = CG_ConfigString( CS_GAME_VERSION ); + if ( strcmp( s, GAME_VERSION ) ) { + CG_Error( "Client/Server game mismatch: %s/%s", GAME_VERSION, s ); + } + + s = CG_ConfigString( CS_LEVEL_START_TIME ); + cgs.levelStartTime = atoi( s ); + + CG_ParseServerinfo(); + + // load the new map + CG_LoadingString( "collision map" ); + + trap_CM_LoadMap( cgs.mapname ); + + String_Init(); + + cg.loading = qtrue; // force players to load instead of defer + + CG_LoadingString( "sounds" ); + + CG_RegisterSounds(); + + CG_LoadingString( "graphics" ); + + CG_RegisterGraphics(); + + CG_LoadingString( "flamechunks" ); + + CG_InitFlameChunks(); // RF, register and clear all flamethrower resources + + CG_LoadingString( "clients" ); + + CG_RegisterClients(); // if low on memory, some clients will be deferred + + CG_AssetCache(); + CG_LoadHudMenu(); // load new hud stuff + + cg.loading = qfalse; // future players will be deferred + + CG_InitLocalEntities(); + + CG_InitMarkPolys(); + + // RF, init ZombieFX + trap_RB_ZombieFXAddNewHit( -1, NULL, NULL ); + + // remove the last loading update + cg.infoScreenText[0] = 0; + + // Make sure we have update values (scores) + CG_SetConfigValues(); + + CG_StartMusic(); + + cg.lightstylesInited = qfalse; + + CG_LoadingString( "" ); + + CG_ShaderStateChanged(); + + // RF, clear all sounds, so we dont hear anything after level load + trap_S_ClearLoopingSounds( 2 ); + + // start level load music + // too late... +// trap_S_StartBackgroundTrack( "sound/music/fla_mp03.wav", "sound/music/fla_mp03.wav", 1 ); + + + // NERVE - SMF +// JPW NERVE -- commented out 'cause this moved + + if ( cgs.gametype == GT_WOLF ) { + trap_Cvar_Set( "cg_drawTimer", "0" ); // jpw + + } + // jpw + // -NERVE - SMF +} + +/* +================= +CG_Shutdown + +Called before every level change or subsystem restart +================= +*/ +void CG_Shutdown( void ) { + + // some mods may need to do cleanup work here, + // like closing files or archiving session data +} + + diff --git a/src/cgame/cg_marks.c b/src/cgame/cg_marks.c new file mode 100644 index 0000000..b7358b6 --- /dev/null +++ b/src/cgame/cg_marks.c @@ -0,0 +1,352 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// cg_marks.c -- wall marks + +#include "cg_local.h" + +/* +=================================================================== + +MARK POLYS + +=================================================================== +*/ + + +markPoly_t cg_activeMarkPolys; // double linked list +markPoly_t *cg_freeMarkPolys; // single linked list +markPoly_t cg_markPolys[MAX_MARK_POLYS]; + +/* +=================== +CG_InitMarkPolys + +This is called at startup and for tournement restarts +=================== +*/ +void CG_InitMarkPolys( void ) { + int i; + markPoly_t *trav, *lasttrav; + + memset( cg_markPolys, 0, sizeof( cg_markPolys ) ); + + cg_activeMarkPolys.nextMark = &cg_activeMarkPolys; + cg_activeMarkPolys.prevMark = &cg_activeMarkPolys; + cg_freeMarkPolys = cg_markPolys; + for ( i = 0, trav = cg_markPolys + 1, lasttrav = cg_markPolys ; i < MAX_MARK_POLYS - 1 ; i++, trav++ ) { + lasttrav->nextMark = trav; + lasttrav = trav; + } +} + + +/* +================== +CG_FreeMarkPoly +================== +*/ +void CG_FreeMarkPoly( markPoly_t *le ) { + if ( !le->prevMark ) { + CG_Error( "CG_FreeLocalEntity: not active" ); + } + + // remove from the doubly linked active list + le->prevMark->nextMark = le->nextMark; + le->nextMark->prevMark = le->prevMark; + + // the free list is only singly linked + le->nextMark = cg_freeMarkPolys; + cg_freeMarkPolys = le; +} + +/* +=================== +CG_AllocMark + +Will allways succeed, even if it requires freeing an old active mark +=================== +*/ +markPoly_t *CG_AllocMark( int endTime ) { + markPoly_t *le; //, *trav, *lastTrav; + int time; + + if ( !cg_freeMarkPolys ) { + // no free entities, so free the one at the end of the chain + // remove the oldest active entity + time = cg_activeMarkPolys.prevMark->time; + while ( cg_activeMarkPolys.prevMark && time == cg_activeMarkPolys.prevMark->time ) { + CG_FreeMarkPoly( cg_activeMarkPolys.prevMark ); + } + } + + le = cg_freeMarkPolys; + cg_freeMarkPolys = cg_freeMarkPolys->nextMark; + + memset( le, 0, sizeof( *le ) ); + + // Ridah, TODO: sort this, so the list is always sorted by longest duration -> shortest duration, + // this way the shortest duration mark will always get overwritten first + //for (trav = cg_activeMarkPolys.nextMark; (trav->duration + trav->time > endTime) && (trav != cg_activeMarkPolys.prevMark) ; lastTrav = trav, trav++ ) { + // Respect the FOR loop + //} + + // link into the active list + le->nextMark = cg_activeMarkPolys.nextMark; + le->prevMark = &cg_activeMarkPolys; + cg_activeMarkPolys.nextMark->prevMark = le; + cg_activeMarkPolys.nextMark = le; + return le; +} + + + +/* +================= +CG_ImpactMark + +origin should be a point within a unit of the plane +dir should be the plane normal + +temporary marks will not be stored or randomly oriented, but immediately +passed to the renderer. +================= +*/ +// Ridah, increased this since we leave them around for longer +#define MAX_MARK_FRAGMENTS 384 +#define MAX_MARK_POINTS 1024 +//#define MAX_MARK_FRAGMENTS 128 +//#define MAX_MARK_POINTS 384 + +// these are ignored now for the most part +//#define MARK_TOTAL_TIME 20000 // (SA) made this a cvar: cg_markTime (we could cap the time or remove marks quicker if too long a time starts to cause new marks to not appear) +#define MARK_FADE_TIME 10000 + +void CG_ImpactMark( qhandle_t markShader, const vec3_t origin, const vec3_t dir, + float orientation, float red, float green, float blue, float alpha, + qboolean alphaFade, float radius, qboolean temporary, int duration ) { + vec3_t axis[3]; + float texCoordScale; + vec3_t originalPoints[4]; + byte colors[4]; + int i, j; + int numFragments; + markFragment_t markFragments[MAX_MARK_FRAGMENTS], *mf; + vec5_t markPoints[MAX_MARK_POINTS]; // Ridah, made it vec5_t so it includes S/T + vec3_t projection; + int multMaxFragments = 1; + + if ( !cg_markTime.integer ) { + return; + } + + if ( radius <= 0 ) { + // just ignore it, don't error out + return; +// CG_Error( "CG_ImpactMark called with <= 0 radius" ); + } + + // Ridah, if no duration, use the default + if ( duration < 0 ) { + if ( duration == -2 ) { + multMaxFragments = -1; // use original mapping + } + +// duration = MARK_TOTAL_TIME; + duration = cg_markTime.integer; + } + + // create the texture axis + VectorNormalize2( dir, axis[0] ); + PerpendicularVector( axis[1], axis[0] ); + RotatePointAroundVector( axis[2], axis[0], axis[1], orientation ); + CrossProduct( axis[0], axis[2], axis[1] ); + + texCoordScale = 0.5 * 1.0 / radius; + + // create the full polygon + for ( i = 0 ; i < 3 ; i++ ) { + originalPoints[0][i] = origin[i] - radius * axis[1][i] - radius * axis[2][i]; + originalPoints[1][i] = origin[i] + radius * axis[1][i] - radius * axis[2][i]; + originalPoints[2][i] = origin[i] + radius * axis[1][i] + radius * axis[2][i]; + originalPoints[3][i] = origin[i] - radius * axis[1][i] + radius * axis[2][i]; + } + + // get the fragments + //VectorScale( dir, -20, projection ); + VectorScale( dir, radius * 2, projection ); + numFragments = trap_CM_MarkFragments( (int)orientation, (void *)originalPoints, + projection, MAX_MARK_POINTS, (float *)&markPoints[0], + MAX_MARK_FRAGMENTS * multMaxFragments, markFragments ); + + colors[0] = red * 255; + colors[1] = green * 255; + colors[2] = blue * 255; + colors[3] = alpha * 255; + + for ( i = 0, mf = markFragments ; i < numFragments ; i++, mf++ ) { + polyVert_t *v; + polyVert_t verts[MAX_VERTS_ON_POLY]; + markPoly_t *mark; + qboolean hasST; + + // we have an upper limit on the complexity of polygons + // that we store persistantly + if ( mf->numPoints > MAX_VERTS_ON_POLY ) { + mf->numPoints = MAX_VERTS_ON_POLY; + } + if ( mf->numPoints < 0 ) { + hasST = qtrue; + mf->numPoints *= -1; + } else { + hasST = qfalse; + } + for ( j = 0, v = verts ; j < mf->numPoints ; j++, v++ ) { + vec3_t delta; + + VectorCopy( markPoints[mf->firstPoint + j], v->xyz ); + + if ( !hasST ) { + VectorSubtract( v->xyz, origin, delta ); + v->st[0] = 0.5 + DotProduct( delta, axis[1] ) * texCoordScale; + v->st[1] = 0.5 + DotProduct( delta, axis[2] ) * texCoordScale; + } else { + v->st[0] = markPoints[mf->firstPoint + j][3]; + v->st[1] = markPoints[mf->firstPoint + j][4]; + } + + *(int *)v->modulate = *(int *)colors; + } + + // if it is a temporary (shadow) mark, add it immediately and forget about it + if ( temporary ) { + trap_R_AddPolyToScene( markShader, mf->numPoints, verts ); + continue; + } + + // otherwise save it persistantly + mark = CG_AllocMark( cg.time + duration ); + mark->time = cg.time; + mark->alphaFade = alphaFade; + mark->markShader = markShader; + mark->poly.numVerts = mf->numPoints; + mark->color[0] = red; + mark->color[1] = green; + mark->color[2] = blue; + mark->color[3] = alpha; + mark->duration = duration; + memcpy( mark->verts, verts, mf->numPoints * sizeof( verts[0] ) ); + } +} + + +/* +=============== +CG_AddMarks +=============== +*/ + +void CG_AddMarks( void ) { + int j; + markPoly_t *mp, *next; + int t; + int fade; + + if ( !cg_markTime.integer ) { + return; + } + + mp = cg_activeMarkPolys.nextMark; + for ( ; mp != &cg_activeMarkPolys ; mp = next ) { + // grab next now, so if the local entity is freed we + // still have it + next = mp->nextMark; + + // see if it is time to completely remove it + if ( cg.time > mp->time + mp->duration ) { + CG_FreeMarkPoly( mp ); + continue; + } + + // fade out the energy bursts + if ( mp->markShader == cgs.media.energyMarkShader ) { + + fade = 450 - 450 * ( ( cg.time - mp->time ) / 3000.0 ); + if ( fade < 255 ) { + if ( fade < 0 ) { + fade = 0; + } + if ( mp->verts[0].modulate[0] != 0 ) { + for ( j = 0 ; j < mp->poly.numVerts ; j++ ) { + mp->verts[j].modulate[0] = mp->color[0] * fade; + mp->verts[j].modulate[1] = mp->color[1] * fade; + mp->verts[j].modulate[2] = mp->color[2] * fade; + } + } + } + } + + // fade in the zombie spirit marks + if ( mp->markShader == cgs.media.zombieSpiritWallShader ) { + + fade = 255 * ( ( cg.time - mp->time ) / 2000.0 ); + if ( fade < 255 ) { + if ( fade < 0 ) { + fade = 0; + } + if ( mp->verts[0].modulate[0] != 0 ) { + for ( j = 0 ; j < mp->poly.numVerts ; j++ ) { + mp->verts[j].modulate[0] = mp->color[0] * fade; + mp->verts[j].modulate[1] = mp->color[1] * fade; + mp->verts[j].modulate[2] = mp->color[2] * fade; + } + } + } + } + + // fade all marks out with time + t = mp->time + mp->duration - cg.time; + if ( t < (float)mp->duration / 2.0 ) { + fade = (int)( 255.0 * (float)t / ( (float)mp->duration / 2.0 ) ); + if ( mp->alphaFade ) { + for ( j = 0 ; j < mp->poly.numVerts ; j++ ) { + mp->verts[j].modulate[3] = fade; + } + } else { + for ( j = 0 ; j < mp->poly.numVerts ; j++ ) { + mp->verts[j].modulate[0] = mp->color[0] * fade; + mp->verts[j].modulate[1] = mp->color[1] * fade; + mp->verts[j].modulate[2] = mp->color[2] * fade; + } + } + } + + trap_R_AddPolyToScene( mp->markShader, mp->poly.numVerts, mp->verts ); + } +} + diff --git a/src/cgame/cg_newDraw.c b/src/cgame/cg_newDraw.c new file mode 100644 index 0000000..e832d84 --- /dev/null +++ b/src/cgame/cg_newDraw.c @@ -0,0 +1,2598 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + + +#include "cg_local.h" +#include "../ui/ui_shared.h" + +extern displayContextDef_t cgDC; + +// set in CG_ParseTeamInfo + +//static int sortedTeamPlayers[TEAM_MAXOVERLAY]; +//static int numSortedTeamPlayers; +int drawTeamOverlayModificationCount = -1; + +//static char systemChat[256]; +//static char teamChat1[256]; +//static char teamChat2[256]; + +void CG_InitTeamChat() { +#ifdef MISSIONPACK + memset( teamChat1, 0, sizeof( teamChat1 ) ); + memset( teamChat2, 0, sizeof( teamChat2 ) ); + memset( systemChat, 0, sizeof( systemChat ) ); +#endif // #ifdef MISSIONPACK +} + +void CG_SetPrintString( int type, const char *p ) { + if ( type == SYSTEM_PRINT ) { + strcpy( systemChat, p ); + } else { + strcpy( teamChat2, teamChat1 ); + strcpy( teamChat1, p ); + } +} + +void CG_CheckOrderPending() { +#ifdef MISSIONPACK + if ( cgs.gametype < GT_CTF ) { + return; + } + if ( cgs.orderPending ) { + //clientInfo_t *ci = cgs.clientinfo + sortedTeamPlayers[cg_currentSelectedPlayer.integer]; + const char *p1, *p2, *b; + p1 = p2 = b = NULL; + switch ( cgs.currentOrder ) { + case TEAMTASK_OFFENSE: + p1 = VOICECHAT_ONOFFENSE; + p2 = VOICECHAT_OFFENSE; + b = "+button7; wait; -button7"; + break; + case TEAMTASK_DEFENSE: + p1 = VOICECHAT_ONDEFENSE; + p2 = VOICECHAT_DEFEND; + b = "+button8; wait; -button8"; + break; + case TEAMTASK_PATROL: + p1 = VOICECHAT_ONPATROL; + p2 = VOICECHAT_PATROL; + b = "+button9; wait; -button9"; + break; + case TEAMTASK_FOLLOW: + p1 = VOICECHAT_ONFOLLOW; + p2 = VOICECHAT_FOLLOWME; + b = "+button10; wait; -button10"; + break; + case TEAMTASK_CAMP: + p1 = VOICECHAT_ONCAMPING; + p2 = VOICECHAT_CAMP; + break; + case TEAMTASK_RETRIEVE: + p1 = VOICECHAT_ONGETFLAG; + p2 = VOICECHAT_RETURNFLAG; + break; + case TEAMTASK_ESCORT: + p1 = VOICECHAT_ONFOLLOWCARRIER; + p2 = VOICECHAT_FOLLOWFLAGCARRIER; + break; + } + + if ( cg_currentSelectedPlayer.integer == numSortedTeamPlayers ) { + // to everyone + trap_SendConsoleCommand( va( "cmd vsay_team %s\n", p2 ) ); + } else { + // for the player self + if ( sortedTeamPlayers[cg_currentSelectedPlayer.integer] == cg.snap->ps.clientNum && p1 ) { + trap_SendConsoleCommand( va( "teamtask %i\n", cgs.currentOrder ) ); + //trap_SendConsoleCommand(va("cmd say_team %s\n", p2)); + trap_SendConsoleCommand( va( "cmd vsay_team %s\n", p1 ) ); + } else if ( p2 ) { + //trap_SendConsoleCommand(va("cmd say_team %s, %s\n", ci->name,p)); + trap_SendConsoleCommand( va( "cmd vtell %d %s\n", sortedTeamPlayers[cg_currentSelectedPlayer.integer], p2 ) ); + } + } + if ( b ) { + trap_SendConsoleCommand( b ); + } + cgs.orderPending = qfalse; + } +#endif // #ifdef MISSIONPACK +} + +static void CG_SetSelectedPlayerName() { + if ( cg_currentSelectedPlayer.integer >= 0 && cg_currentSelectedPlayer.integer < numSortedTeamPlayers ) { + clientInfo_t *ci = cgs.clientinfo + sortedTeamPlayers[cg_currentSelectedPlayer.integer]; + if ( ci ) { + trap_Cvar_Set( "cg_selectedPlayerName", ci->name ); + trap_Cvar_Set( "cg_selectedPlayer", va( "%d", sortedTeamPlayers[cg_currentSelectedPlayer.integer] ) ); +// cgs.currentOrder = ci->teamTask; + } + } else { + trap_Cvar_Set( "cg_selectedPlayerName", "Everyone" ); + } +} +int CG_GetSelectedPlayer() { + if ( cg_currentSelectedPlayer.integer < 0 || cg_currentSelectedPlayer.integer >= numSortedTeamPlayers ) { + cg_currentSelectedPlayer.integer = 0; + } + return cg_currentSelectedPlayer.integer; +} + +void CG_SelectNextPlayer() { + CG_CheckOrderPending(); + if ( cg_currentSelectedPlayer.integer >= 0 && cg_currentSelectedPlayer.integer < numSortedTeamPlayers ) { + cg_currentSelectedPlayer.integer++; + } else { + cg_currentSelectedPlayer.integer = 0; + } + CG_SetSelectedPlayerName(); +} + +void CG_SelectPrevPlayer() { + CG_CheckOrderPending(); + if ( cg_currentSelectedPlayer.integer > 0 && cg_currentSelectedPlayer.integer < numSortedTeamPlayers ) { + cg_currentSelectedPlayer.integer--; + } else { + cg_currentSelectedPlayer.integer = numSortedTeamPlayers; + } + CG_SetSelectedPlayerName(); +} + + +// (SA) not sure what you'd use this for anyway... + +static void CG_DrawPlayerArmorIcon( rectDef_t *rect, qboolean draw2D ) { +#ifdef MISSIONPACK + centity_t *cent; + playerState_t *ps; + vec3_t angles; + vec3_t origin; + + if ( cg_drawStatus.integer == 0 ) { + return; + } + + cent = &cg_entities[cg.snap->ps.clientNum]; + ps = &cg.snap->ps; + + if ( draw2D || !cg_draw3dIcons.integer && cg_drawIcons.integer ) { + CG_DrawPic( rect->x, rect->y + rect->h / 2 + 1, rect->w, rect->h, cgs.media.armorIcon ); + } else if ( cg_draw3dIcons.integer ) { + VectorClear( angles ); + origin[0] = 90; + origin[1] = 0; + origin[2] = -10; + angles[YAW] = ( cg.time & 2047 ) * 360 / 2048.0; + + CG_Draw3DModel( rect->x, rect->y, rect->w, rect->h, cgs.media.armorModel, 0, origin, angles ); + } +#endif +} + +static void CG_DrawPlayerArmorValue( rectDef_t *rect, int font, float scale, vec4_t color, qhandle_t shader, int textStyle ) { + char num[16]; + int value; + centity_t *cent; + playerState_t *ps; + + cent = &cg_entities[cg.snap->ps.clientNum]; + ps = &cg.snap->ps; + + value = ps->stats[STAT_ARMOR]; + + + if ( shader ) { + trap_R_SetColor( color ); + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, shader ); + trap_R_SetColor( NULL ); + } else { + Com_sprintf( num, sizeof( num ), "%i", value ); + value = CG_Text_Width( num, font, scale, 0 ); + CG_Text_Paint( rect->x + ( rect->w - value ) / 2, rect->y + rect->h, font, scale, color, num, 0, 0, textStyle ); + } +} + +// TTimo: unused +/* +static float healthColors[4][4] = { +// { 0.2, 1.0, 0.2, 1.0 } , { 1.0, 0.2, 0.2, 1.0 }, {0.5, 0.5, 0.5, 1} }; + { 1, 0.69f, 0, 1.0f } , // normal + { 1.0f, 0.2f, 0.2f, 1.0f }, // low health + {0.5f, 0.5f, 0.5f, 1}, // weapon firing + { 1, 1, 1, 1 } }; // health > 100 +*/ + +/* +============== +weapIconDrawSize +============== +*/ +static int weapIconDrawSize( int weap ) { + switch ( weap ) { + + // weapons to not draw + case WP_KNIFE: + return 0; + + // weapons with 'wide' icons + case WP_THOMPSON: + case WP_MP40: + case WP_STEN: + case WP_MAUSER: + case WP_GARAND: + case WP_VENOM: + case WP_TESLA: + case WP_PANZERFAUST: + case WP_FLAMETHROWER: + case WP_FG42: + case WP_FG42SCOPE: + case WP_SNOOPERSCOPE: + case WP_SNIPERRIFLE: + return 2; + } + + return 1; +} + + +/* +============== +CG_DrawPlayerWeaponIcon +============== +*/ +static void CG_DrawPlayerWeaponIcon( rectDef_t *rect, qboolean drawHighlighted, int align ) { + int size; + int realweap; // DHM - Nerve + qhandle_t icon; + float scale,halfScale; + + if ( !cg_drawIcons.integer ) { + return; + } + + // DHM - Nerve :: special case for WP_CLASS_SPECIAL + + realweap = cg.predictedPlayerState.weapon; + + if ( cgs.gametype == GT_WOLF && realweap == WP_CLASS_SPECIAL ) { + switch ( cg.predictedPlayerState.stats[ STAT_PLAYER_CLASS ] ) { + case PC_MEDIC: + realweap = WP_MEDIC_HEAL; + break; + case PC_LT: + realweap = WP_GRENADE_SMOKE; + break; + default: + break; + } + } + // dhm + + size = weapIconDrawSize( realweap ); + + if ( !size ) { + return; + } + + if ( drawHighlighted ) { + icon = cg_weapons[ realweap ].weaponIcon[1]; + } else { + icon = cg_weapons[ realweap ].weaponIcon[0]; + } + + + + // pulsing grenade icon to help the player 'count' in their head + if ( cg.predictedPlayerState.grenadeTimeLeft ) { // grenades and dynamite set this + + // these time differently + if ( realweap == WP_DYNAMITE ) { + if ( ( ( cg.grenLastTime ) % 1000 ) > ( ( cg.predictedPlayerState.grenadeTimeLeft ) % 1000 ) ) { + trap_S_StartLocalSound( cgs.media.grenadePulseSound4, CHAN_LOCAL_SOUND ); + } + } else { + if ( ( ( cg.grenLastTime ) % 1000 ) < ( ( cg.predictedPlayerState.grenadeTimeLeft ) % 1000 ) ) { + switch ( cg.predictedPlayerState.grenadeTimeLeft / 1000 ) { + case 3: + trap_S_StartLocalSound( cgs.media.grenadePulseSound4, CHAN_LOCAL_SOUND ); + break; + case 2: + trap_S_StartLocalSound( cgs.media.grenadePulseSound3, CHAN_LOCAL_SOUND ); + break; + case 1: + trap_S_StartLocalSound( cgs.media.grenadePulseSound2, CHAN_LOCAL_SOUND ); + break; + case 0: + trap_S_StartLocalSound( cgs.media.grenadePulseSound1, CHAN_LOCAL_SOUND ); + break; + } + } + } + + scale = (float)( ( cg.predictedPlayerState.grenadeTimeLeft ) % 1000 ) / 100.0f; + halfScale = scale * 0.5f; + + cg.grenLastTime = cg.predictedPlayerState.grenadeTimeLeft; + } else { + scale = halfScale = 0; + } + + + if ( icon ) { + float x, y, w, h; + + if ( size == 1 ) { // draw half width to match the icon asset + + // start at left + x = rect->x - halfScale; + y = rect->y - halfScale; + w = rect->w / 2 + scale; + h = rect->h + scale; + + switch ( align ) { + case ITEM_ALIGN_CENTER: + x += rect->w / 4; + break; + case ITEM_ALIGN_RIGHT: + x += rect->w / 2; + break; + case ITEM_ALIGN_LEFT: + default: + break; + } + + } else { + x = rect->x - halfScale; + y = rect->y - halfScale; + w = rect->w + scale; + h = rect->h + scale; + } + + + CG_DrawPic( x, y, w, h, icon ); + } +} + + +/* +============== +CG_DrawPlayerAmmoIcon +============== +*/ +static void CG_DrawPlayerAmmoIcon( rectDef_t *rect, qboolean draw2D ) { + centity_t *cent; + playerState_t *ps; + vec3_t angles; + vec3_t origin; + + cent = &cg_entities[cg.snap->ps.clientNum]; + ps = &cg.snap->ps; + + // TTimo: gcc: suggests () around && within || + if ( draw2D || ( !cg_draw3dIcons.integer && cg_drawIcons.integer ) ) { + qhandle_t icon; + icon = cg_weapons[ cg.predictedPlayerState.weapon ].ammoIcon; + if ( icon ) { + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, icon ); + } + } else if ( cg_draw3dIcons.integer ) { + if ( cent->currentState.weapon && cg_weapons[ cent->currentState.weapon ].ammoModel ) { + VectorClear( angles ); + origin[0] = 70; + origin[1] = 0; + origin[2] = 0; + angles[YAW] = 90 + 20 * sin( cg.time / 1000.0 ); + CG_Draw3DModel( rect->x, rect->y, rect->w, rect->h, cg_weapons[ cent->currentState.weapon ].ammoModel, 0, origin, angles ); + } + } +} + +extern void CG_CheckForCursorHints( void ); +#define CURSORHINT_SCALE 10 + +/* +============== +CG_DrawCursorHints + + cg_cursorHints.integer == + 0: no hints + 1: sin size pulse + 2: one way size pulse + 3: alpha pulse + 4+: static image + +============== +*/ +extern void CG_DrawExitStats( void ); + +static void CG_DrawCursorhint( rectDef_t *rect ) { + float *color; + qhandle_t icon, icon2 = 0; + float scale, halfscale; + qboolean redbar = qfalse; + + if ( !cg_cursorHints.integer ) { + return; + } + + CG_CheckForCursorHints(); + + icon = cgs.media.hintShaders[cg.cursorHintIcon]; + + switch ( cg.cursorHintIcon ) { + case HINT_NONE: + case HINT_FORCENONE: + icon = 0; + break; + case HINT_BREAKABLE_DYNAMITE: + icon = cgs.media.hintShaders[HINT_BREAKABLE]; + break; + case HINT_CHAIR: + // only show 'pickupable' if you're not armed, or are armed with a single handed weapon + if ( cg.predictedPlayerState.weapon && !( WEAPS_ONE_HANDED & ( 1 << ( cg.predictedPlayerState.weapon ) ) ) ) { // (SA) this was backwards + icon = cgs.media.hintShaders[HINT_NOACTIVATE]; + } + break; + + case HINT_PLAYER: + icon = cgs.media.hintShaders[HINT_NOACTIVATE]; + break; + + case HINT_PLYR_FRIEND: + break; + + case HINT_NOEXIT_FAR: + redbar = qtrue; // draw the status bar in red to show that you can't exit yet + case HINT_EXIT_FAR: + break; + + case HINT_NOEXIT: + redbar = qtrue; // draw the status bar in red to show that you can't exit yet + case HINT_EXIT: + cg.exitStatsFade = 250; // fade /up/ time + if ( !cg.exitStatsTime ) { + cg.exitStatsTime = cg.time; + } + CG_DrawExitStats(); + break; + + default: + break; + } + + if ( !icon ) { + return; + } + + + // color + color = CG_FadeColor( cg.cursorHintTime, cg.cursorHintFade ); + if ( !color ) { + trap_R_SetColor( NULL ); + cg.exitStatsTime = 0; // exit stats will fade up next time they're hit + cg.cursorHintIcon = HINT_NONE; // clear the hint + return; + } + + if ( cg_cursorHints.integer == 3 ) { + color[3] *= 0.5 + 0.5 * sin( (float)cg.time / 150.0 ); + } + + + // size + if ( cg_cursorHints.integer >= 3 ) { // no size pulsing + scale = halfscale = 0; + } else { + if ( cg_cursorHints.integer == 2 ) { + scale = (float)( ( cg.cursorHintTime ) % 1000 ) / 100.0f; // one way size pulse + } else { + scale = CURSORHINT_SCALE * ( 0.5 + 0.5 * sin( (float)cg.time / 150.0 ) ); // sin pulse + + } + halfscale = scale * 0.5f; + } + + // set color and draw the hint + trap_R_SetColor( color ); + CG_DrawPic( rect->x - halfscale, rect->y - halfscale, rect->w + scale, rect->h + scale, icon ); + + if ( icon2 ) { + CG_DrawPic( rect->x - halfscale, rect->y - halfscale, rect->w + scale, rect->h + scale, icon2 ); + } + + trap_R_SetColor( NULL ); + + // draw status bar under the cursor hint + if ( cg.cursorHintValue && ( !( cg.time - cg.cursorHintTime ) ) ) { // don't fade bar out w/ hint icon + if ( redbar ) { + Vector4Set( color, 1, 0, 0, 0.5f ); + } else { + Vector4Set( color, 0, 0, 1, 0.5f ); + } + CG_FilledBar( rect->x, rect->y + rect->h + 4, rect->w, 8, color, NULL, NULL, (float)cg.cursorHintValue / 255.0f, 0 ); + } + +} + + + +/* +============== +CG_DrawMessageIcon +============== +*/ +//----(SA) added + +static void CG_DrawMessageIcon( rectDef_t *rect ) { + int icon; + + if ( !cg_youGotMail.integer ) { + return; + } + + if ( cg_youGotMail.integer == 2 ) { + icon = cgs.media.youGotObjectiveShader; + } else { + icon = cgs.media.youGotMailShader; + } + + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, icon ); +} + + + +/* +============== +CG_DrawPlayerAmmoValue + 0 - ammo + 1 - clip +============== +*/ +static void CG_DrawPlayerAmmoValue( rectDef_t *rect, int font, float scale, vec4_t color, qhandle_t shader, int textStyle, int type ) { + char num[16]; + int value, value2 = 0; + centity_t *cent; + playerState_t *ps; + int weap; + qboolean special = qfalse; + + cent = &cg_entities[cg.snap->ps.clientNum]; + ps = &cg.snap->ps; + + weap = cent->currentState.weapon; + + if ( !weap ) { + return; + } + + if ( ps->weaponstate == WEAPON_RELOADING && type != 0 ) { + return; + } + + switch ( weap ) { // some weapons don't draw ammo count text + case WP_KNIFE: + case WP_CLASS_SPECIAL: // DHM - Nerve + return; + + case WP_AKIMBO: + special = qtrue; + break; + + case WP_GRENADE_LAUNCHER: + case WP_GRENADE_PINEAPPLE: + case WP_DYNAMITE: + case WP_TESLA: + case WP_FLAMETHROWER: + if ( type == 0 ) { // don't draw reserve value, just clip (since these weapons have all their ammo in the clip) + return; + } + break; + + default: + break; + } + + + if ( type == 0 ) { // ammo + value = cg.snap->ps.ammo[BG_FindAmmoForWeapon( weap )]; + } else { // clip + value = ps->ammoclip[BG_FindClipForWeapon( weap )]; + if ( special ) { + value2 = value; + if ( weapAlts[weap] ) { + value = ps->ammoclip[weapAlts[weap]]; + } +// value2 = ps->ammoclip[weapAlts[weap]]; + } + } + + if ( value > -1 ) { + if ( shader ) { + trap_R_SetColor( color ); + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, shader ); + trap_R_SetColor( NULL ); + } else { + Com_sprintf( num, sizeof( num ), "%i", value ); + value = CG_Text_Width( num, font, scale, 0 ); + CG_Text_Paint( rect->x + ( rect->w - value ) / 2, rect->y + rect->h, font, scale, color, num, 0, 0, textStyle ); + +// if(special) { // draw '0' for akimbo guns + if ( value2 || ( special && type == 1 ) ) { + Com_sprintf( num, sizeof( num ), "%i /", value2 ); + value = CG_Text_Width( num, font, scale, 0 ); + CG_Text_Paint( -30 + rect->x + ( rect->w - value ) / 2, rect->y + rect->h, font, scale, color, num, 0, 0, textStyle ); + } + } + } +} + + + +static void CG_DrawPlayerHead( rectDef_t *rect, qboolean draw2D ) { + vec3_t angles; + float size, stretch; + float frac; + float x = rect->x; + + VectorClear( angles ); + + if ( cg.damageTime && cg.time - cg.damageTime < DAMAGE_TIME ) { + frac = (float)( cg.time - cg.damageTime ) / DAMAGE_TIME; + size = rect->w * 1.25 * ( 1.5 - frac * 0.5 ); + + stretch = size - rect->w * 1.25; + // kick in the direction of damage + x -= stretch * 0.5 + cg.damageX * stretch * 0.5; + + cg.headStartYaw = 180 + cg.damageX * 45; + + cg.headEndYaw = 180 + 20 * cos( crandom() * M_PI ); + cg.headEndPitch = 5 * cos( crandom() * M_PI ); + + cg.headStartTime = cg.time; + cg.headEndTime = cg.time + 100 + random() * 2000; + } else { + if ( cg.time >= cg.headEndTime ) { + // select a new head angle + cg.headStartYaw = cg.headEndYaw; + cg.headStartPitch = cg.headEndPitch; + cg.headStartTime = cg.headEndTime; + cg.headEndTime = cg.time + 100 + random() * 2000; + + cg.headEndYaw = 180 + 20 * cos( crandom() * M_PI ); + cg.headEndPitch = 5 * cos( crandom() * M_PI ); + } + + size = rect->w * 1.25; + } + + // if the server was frozen for a while we may have a bad head start time + if ( cg.headStartTime > cg.time ) { + cg.headStartTime = cg.time; + } + + frac = ( cg.time - cg.headStartTime ) / (float)( cg.headEndTime - cg.headStartTime ); + frac = frac * frac * ( 3 - 2 * frac ); + angles[YAW] = cg.headStartYaw + ( cg.headEndYaw - cg.headStartYaw ) * frac; + angles[PITCH] = cg.headStartPitch + ( cg.headEndPitch - cg.headStartPitch ) * frac; + + CG_DrawHead( x, rect->y, rect->w, rect->h, cg.snap->ps.clientNum, angles ); +} + +static void CG_DrawSelectedPlayerHealth( rectDef_t *rect, int font, float scale, vec4_t color, qhandle_t shader, int textStyle ) { + clientInfo_t *ci; + int value; + char num[16]; + + ci = cgs.clientinfo + sortedTeamPlayers[CG_GetSelectedPlayer()]; + if ( ci ) { + if ( shader ) { + trap_R_SetColor( color ); + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, shader ); + trap_R_SetColor( NULL ); + } else { + Com_sprintf( num, sizeof( num ), "%i", ci->health ); + value = CG_Text_Width( num, font, scale, 0 ); + CG_Text_Paint( rect->x + ( rect->w - value ) / 2, rect->y + rect->h, font, scale, color, num, 0, 0, textStyle ); + } + } +} + +static void CG_DrawSelectedPlayerArmor( rectDef_t *rect, int font, float scale, vec4_t color, qhandle_t shader, int textStyle ) { + clientInfo_t *ci; + int value; + char num[16]; + + ci = cgs.clientinfo + sortedTeamPlayers[CG_GetSelectedPlayer()]; + if ( ci ) { + if ( ci->armor > 0 ) { + if ( shader ) { + trap_R_SetColor( color ); + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, shader ); + trap_R_SetColor( NULL ); + } else { + Com_sprintf( num, sizeof( num ), "%i", ci->armor ); + value = CG_Text_Width( num, font, scale, 0 ); + CG_Text_Paint( rect->x + ( rect->w - value ) / 2, rect->y + rect->h, font, scale, color, num, 0, 0, textStyle ); + } + } + } +} + +qhandle_t CG_StatusHandle( int task ) { +#ifdef MISSIONPACK + qhandle_t h = cgs.media.assaultShader; + switch ( task ) { + case TEAMTASK_OFFENSE: + h = cgs.media.assaultShader; + break; + case TEAMTASK_DEFENSE: + h = cgs.media.defendShader; + break; + case TEAMTASK_PATROL: + h = cgs.media.patrolShader; + break; + case TEAMTASK_FOLLOW: + h = cgs.media.followShader; + break; + case TEAMTASK_CAMP: + h = cgs.media.campShader; + break; + case TEAMTASK_RETRIEVE: + h = cgs.media.retrieveShader; + break; + case TEAMTASK_ESCORT: + h = cgs.media.escortShader; + break; + default: + h = cgs.media.assaultShader; + break; + } + return h; +#else + return 0; +#endif // #ifdef MISSIONPACK +} + +static void CG_DrawSelectedPlayerStatus( rectDef_t *rect ) { +#ifdef MISSIONPACK + clientInfo_t *ci = cgs.clientinfo + sortedTeamPlayers[CG_GetSelectedPlayer()]; + if ( ci ) { + qhandle_t h; + if ( cgs.orderPending ) { + // blink the icon + if ( cg.time > cgs.orderTime - 2500 && ( cg.time >> 9 ) & 1 ) { + return; + } + h = CG_StatusHandle( cgs.currentOrder ); + } else { + h = CG_StatusHandle( ci->teamTask ); + } + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, h ); + } +#endif // #ifdef MISSIONPACK +} + + +static void CG_DrawPlayerStatus( rectDef_t *rect ) { +#ifdef MISSIONPACK + clientInfo_t *ci = &cgs.clientinfo[cg.snap->ps.clientNum]; + if ( ci ) { + qhandle_t h = CG_StatusHandle( ci->teamTask ); + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, h ); + } +#endif // #ifdef MISSIONPACK +} + + +static void CG_DrawSelectedPlayerName( rectDef_t *rect, int font, float scale, vec4_t color, qboolean voice, int textStyle ) { + clientInfo_t *ci; + +// ci = cgs.clientinfo + ((voice) ? cgs.currentVoiceClient : sortedTeamPlayers[CG_GetSelectedPlayer()]); + ci = cgs.clientinfo + sortedTeamPlayers[CG_GetSelectedPlayer()]; + if ( ci ) { + CG_Text_Paint( rect->x, rect->y + rect->h, font, scale, color, ci->name, 0, 0, textStyle ); + } +} + +static void CG_DrawSelectedPlayerLocation( rectDef_t *rect, int font, float scale, vec4_t color, int textStyle ) { + clientInfo_t *ci; + ci = cgs.clientinfo + sortedTeamPlayers[CG_GetSelectedPlayer()]; + if ( ci ) { + const char *p = CG_ConfigString( CS_LOCATIONS + ci->location ); + + if ( !p || !*p ) { + p = "unknown"; + } + CG_Text_Paint( rect->x, rect->y + rect->h, font, scale, color, p, 0, 0, textStyle ); + } +} + +static void CG_DrawPlayerLocation( rectDef_t *rect, int font, float scale, vec4_t color, int textStyle ) { + clientInfo_t *ci = &cgs.clientinfo[cg.snap->ps.clientNum]; + if ( ci ) { + const char *p = CG_ConfigString( CS_LOCATIONS + ci->location ); + if ( !p || !*p ) { + p = "unknown"; + } + CG_Text_Paint( rect->x, rect->y + rect->h, font, scale, color, p, 0, 0, textStyle ); + } +} + + + +static void CG_DrawSelectedPlayerWeapon( rectDef_t *rect ) { + clientInfo_t *ci; + + ci = cgs.clientinfo + sortedTeamPlayers[CG_GetSelectedPlayer()]; + if ( ci ) { + if ( cg_weapons[ci->curWeapon].weaponIcon[1] ) { + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, cg_weapons[ci->curWeapon].weaponIcon[1] ); // (SA) using the 'selected' version of the icon + } else { + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, cgs.media.deferShader ); + } + } +} + +static void CG_DrawPlayerScore( rectDef_t *rect, int font, float scale, vec4_t color, qhandle_t shader, int textStyle ) { + char num[16]; + int value = cg.snap->ps.persistant[PERS_SCORE]; + + if ( shader ) { + trap_R_SetColor( color ); + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, shader ); + trap_R_SetColor( NULL ); + } else { + Com_sprintf( num, sizeof( num ), "%i", value ); + value = CG_Text_Width( num, font, scale, 0 ); + CG_Text_Paint( rect->x + ( rect->w - value ) / 2, rect->y + rect->h, font, scale, color, num, 0, 0, textStyle ); + } +} + + +static void CG_DrawHoldableItem( rectDef_t *rect, int font, float scale, qboolean draw2D ) { + int value; + gitem_t *item; + + item = BG_FindItemForHoldable( cg.holdableSelect ); + + if ( !item ) { + return; + } + + value = cg.predictedPlayerState.holdable[cg.holdableSelect]; + + if ( value ) { +// CG_RegisterItemVisuals( value ); + CG_RegisterItemVisuals( item - bg_itemlist ); + + if ( cg.holdableSelect == HI_WINE ) { + if ( value > 3 ) { + value = 3; // 3 stages to icon, just draw full if beyond 'full' + } +// CG_DrawPic( rect->x, rect->y, rect->w, rect->h, cg_items[ value ].icons[2-(value-1)] ); + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, cg_items[item - bg_itemlist].icons[2 - ( value - 1 )] ); + } else { +// CG_DrawPic( rect->x, rect->y, rect->w, rect->h, cg_items[ value ].icons[0] ); + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, cg_items[item - bg_itemlist].icons[0] ); + } + } +} + +void flubfoo() { + int value; + gitem_t *item; + + if ( !cg.holdableSelect ) { + return; + } + + item = BG_FindItemForHoldable( cg.holdableSelect ); + + if ( !item ) { + return; + } + + value = cg.predictedPlayerState.holdable[cg.holdableSelect]; + + if ( value ) { + + trap_R_SetColor( NULL ); + + CG_RegisterItemVisuals( item - bg_itemlist ); + + if ( cg.holdableSelect == HI_WINE ) { + if ( value > 3 ) { + value = 3; // 3 stages to icon, just draw full if beyond 'full' + + } + //----(SA) trying smaller text + //----(SA) and off to the right side of the HUD +// CG_DrawPic( 100, (SCREEN_HEIGHT-ICON_SIZE)-8, ICON_SIZE/2, ICON_SIZE, cg_items[item - bg_itemlist].icons[2-(value-1)] ); + CG_DrawPic( 606, 366, 24, 48, cg_items[item - bg_itemlist].icons[2 - ( value - 1 )] ); + + } else { +// CG_DrawPic( 100, (SCREEN_HEIGHT-ICON_SIZE)-8, ICON_SIZE/2, ICON_SIZE, cg_items[item - bg_itemlist].icons[0] ); + CG_DrawPic( 606, 366, 24, 48, cg_items[item - bg_itemlist].icons[0] ); + + } + + // draw the selection box so it's not just floating in space + CG_DrawPic( 606 - 4, 366 - 4, 32, 56, cgs.media.selectShader ); + } +} + + + +static void CG_DrawPlayerItem( rectDef_t *rect, int font, float scale, qboolean draw2D ) { + int value; + vec3_t origin, angles; + + value = cg.snap->ps.stats[STAT_HOLDABLE_ITEM]; + + if ( value ) { + CG_RegisterItemVisuals( value ); + + if ( qtrue ) { + CG_RegisterItemVisuals( value ); + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, cg_items[ value ].icons[0] ); + } else { + VectorClear( angles ); + origin[0] = 90; + origin[1] = 0; + origin[2] = -10; + angles[YAW] = ( cg.time & 2047 ) * 360 / 2048.0; + CG_Draw3DModel( rect->x, rect->y, rect->w, rect->h, cg_items[ value ].models[0], 0, origin, angles ); + } + } +} + + +static void CG_DrawSelectedPlayerPowerup( rectDef_t *rect, qboolean draw2D ) { + clientInfo_t *ci; + int j; + float x, y; + + ci = cgs.clientinfo + sortedTeamPlayers[CG_GetSelectedPlayer()]; + if ( ci ) { + x = rect->x; + y = rect->y; + + for ( j = 0; j < PW_NUM_POWERUPS; j++ ) { + if ( ci->powerups & ( 1 << j ) ) { + gitem_t *item; + item = BG_FindItemForPowerup( j ); + if ( item ) { + CG_DrawPic( x, y, rect->w, rect->h, trap_R_RegisterShader( item->icon ) ); + x += 3; + y += 3; + return; + } + } + } + } +} + + +static void CG_DrawSelectedPlayerHead( rectDef_t *rect, qboolean draw2D, qboolean voice ) { + clipHandle_t cm; + clientInfo_t *ci; + float len; + vec3_t origin; + vec3_t mins, maxs, angles; + + +// ci = cgs.clientinfo + ((voice) ? cgs.currentVoiceClient : sortedTeamPlayers[CG_GetSelectedPlayer()]); + ci = cgs.clientinfo + sortedTeamPlayers[CG_GetSelectedPlayer()]; + + if ( ci ) { + if ( cg_draw3dIcons.integer ) { + cm = ci->headModel; + if ( !cm ) { + return; + } + + // offset the origin y and z to center the head + trap_R_ModelBounds( cm, mins, maxs ); + + origin[2] = -0.5 * ( mins[2] + maxs[2] ); + origin[1] = 0.5 * ( mins[1] + maxs[1] ); + + // calculate distance so the head nearly fills the box + // assume heads are taller than wide + len = 0.7 * ( maxs[2] - mins[2] ); + origin[0] = len / 0.268; // len / tan( fov/2 ) + + // allow per-model tweaking + VectorAdd( origin, ci->modelInfo->headOffset, origin ); + + angles[PITCH] = 0; + angles[YAW] = 180; + angles[ROLL] = 0; + + CG_Draw3DModel( rect->x, rect->y, rect->w, rect->h, ci->headModel, ci->headSkin, origin, angles ); + + } else if ( cg_drawIcons.integer ) { + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, ci->modelIcon ); + } + + // if they are deferred, draw a cross out + if ( ci->deferred ) { + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, cgs.media.deferShader ); + } + } +} + + +static void CG_DrawPlayerHealth( rectDef_t *rect, int font, float scale, vec4_t color, qhandle_t shader, int textStyle ) { + playerState_t *ps; + int value; + char num[16]; + + ps = &cg.snap->ps; + + value = ps->stats[STAT_HEALTH]; + + if ( shader ) { + trap_R_SetColor( color ); + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, shader ); + trap_R_SetColor( NULL ); + } else { + Com_sprintf( num, sizeof( num ), "%i", value ); + value = CG_Text_Width( num, font, scale, 0 ); + CG_Text_Paint( rect->x + ( rect->w - value ) / 2, rect->y + rect->h, font, scale, color, num, 0, 0, textStyle ); + } +} + + +static void CG_DrawRedScore( rectDef_t *rect, int font, float scale, vec4_t color, qhandle_t shader, int textStyle ) { + int value; + char num[16]; + if ( cgs.scores1 == SCORE_NOT_PRESENT ) { + Com_sprintf( num, sizeof( num ), "-" ); + } else { + Com_sprintf( num, sizeof( num ), "%i", cgs.scores1 ); + } + value = CG_Text_Width( num, font, scale, 0 ); + CG_Text_Paint( rect->x + rect->w - value, rect->y + rect->h, font, scale, color, num, 0, 0, textStyle ); +} + +static void CG_DrawBlueScore( rectDef_t *rect, int font, float scale, vec4_t color, qhandle_t shader, int textStyle ) { + int value; + char num[16]; + + if ( cgs.scores2 == SCORE_NOT_PRESENT ) { + Com_sprintf( num, sizeof( num ), "-" ); + } else { + Com_sprintf( num, sizeof( num ), "%i", cgs.scores2 ); + } + value = CG_Text_Width( num, font, scale, 0 ); + CG_Text_Paint( rect->x + rect->w - value, rect->y + rect->h, font, scale, color, num, 0, 0, textStyle ); +} + +// FIXME: team name support +static void CG_DrawRedName( rectDef_t *rect, int font, float scale, vec4_t color, int textStyle ) { +#ifdef MISSIONPACK + CG_Text_Paint( rect->x, rect->y + rect->h, font, scale, color, cg_redTeamName.string, 0, 0, textStyle ); +#endif // #ifdef MISSIONPACK +} + +static void CG_DrawBlueName( rectDef_t *rect, int font, float scale, vec4_t color, int textStyle ) { +#ifdef MISSIONPACK + CG_Text_Paint( rect->x, rect->y + rect->h, font, scale, color, cg_blueTeamName.string, 0, 0, textStyle ); +#endif // #ifdef MISSIONPACK +} + +static void CG_DrawBlueFlagName( rectDef_t *rect, int font, float scale, vec4_t color, int textStyle ) { +#ifdef MISSIONPACK + int i; + for ( i = 0 ; i < cgs.maxclients ; i++ ) { + if ( cgs.clientinfo[i].infoValid && cgs.clientinfo[i].team == TEAM_RED && cgs.clientinfo[i].powerups & ( 1 << PW_BLUEFLAG ) ) { + CG_Text_Paint( rect->x, rect->y + rect->h, scale, color, cgs.clientinfo[i].name, 0, 0, textStyle ); + return; + } + } +#endif // #ifdef MISSIONPACK +} + +static void CG_DrawBlueFlagStatus( rectDef_t *rect, qhandle_t shader ) { +#ifdef MISSIONPACK + if ( cgs.gametype != GT_CTF && cgs.gametype != GT_1FCTF ) { + if ( cgs.gametype == GT_HARVESTER ) { + vec4_t color = {0, 0, 1, 1}; + trap_R_SetColor( color ); + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, cgs.media.blueCubeIcon ); + trap_R_SetColor( NULL ); + } + return; + } + if ( shader ) { + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, shader ); + } else { + gitem_t *item = BG_FindItemForPowerup( PW_BLUEFLAG ); + if ( item ) { + vec4_t color = {0, 0, 1, 1}; + trap_R_SetColor( color ); + if ( cgs.blueflag >= 0 && cgs.blueflag <= 2 ) { + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, cgs.media.flagShaders[cgs.blueflag] ); + } else { + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, cgs.media.flagShaders[0] ); + } + trap_R_SetColor( NULL ); + } + } +#endif // #ifdef MISSIONPACK +} + +static void CG_DrawBlueFlagHead( rectDef_t *rect ) { +#ifdef MISSIONPACK + int i; + for ( i = 0 ; i < cgs.maxclients ; i++ ) { + if ( cgs.clientinfo[i].infoValid && cgs.clientinfo[i].team == TEAM_RED && cgs.clientinfo[i].powerups & ( 1 << PW_BLUEFLAG ) ) { + vec3_t angles; + VectorClear( angles ); + angles[YAW] = 180 + 20 * sin( cg.time / 650.0 );; + CG_DrawHead( rect->x, rect->y, rect->w, rect->h, 0,angles ); + return; + } + } +#endif // #ifdef MISSIONPACK +} + +static void CG_DrawRedFlagName( rectDef_t *rect, int font, float scale, vec4_t color, int textStyle ) { +#ifdef MISSIONPACK + int i; + for ( i = 0 ; i < cgs.maxclients ; i++ ) { + if ( cgs.clientinfo[i].infoValid && cgs.clientinfo[i].team == TEAM_BLUE && cgs.clientinfo[i].powerups & ( 1 << PW_REDFLAG ) ) { + CG_Text_Paint( rect->x, rect->y + rect->h, scale, color, cgs.clientinfo[i].name, 0, 0, textStyle ); + return; + } + } +#endif // #ifdef MISSIONPACK +} + +static void CG_DrawRedFlagStatus( rectDef_t *rect, qhandle_t shader ) { +#ifdef MISSIONPACK + if ( cgs.gametype != GT_CTF && cgs.gametype != GT_1FCTF ) { + if ( cgs.gametype == GT_HARVESTER ) { + vec4_t color = {1, 0, 0, 1}; + trap_R_SetColor( color ); + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, cgs.media.redCubeIcon ); + trap_R_SetColor( NULL ); + } + return; + } + if ( shader ) { + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, shader ); + } else { + gitem_t *item = BG_FindItemForPowerup( PW_REDFLAG ); + if ( item ) { + vec4_t color = {1, 0, 0, 1}; + trap_R_SetColor( color ); + if ( cgs.redflag >= 0 && cgs.redflag <= 2 ) { + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, cgs.media.flagShaders[cgs.redflag] ); + } else { + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, cgs.media.flagShaders[0] ); + } + trap_R_SetColor( NULL ); + } + } +#endif // #ifdef MISSIONPACK +} + +static void CG_DrawRedFlagHead( rectDef_t *rect ) { +#ifdef MISSIONPACK + int i; + for ( i = 0 ; i < cgs.maxclients ; i++ ) { + if ( cgs.clientinfo[i].infoValid && cgs.clientinfo[i].team == TEAM_BLUE && cgs.clientinfo[i].powerups & ( 1 << PW_REDFLAG ) ) { + vec3_t angles; + VectorClear( angles ); + angles[YAW] = 180 + 20 * sin( cg.time / 650.0 );; + CG_DrawHead( rect->x, rect->y, rect->w, rect->h, 0,angles ); + return; + } + } +#endif // #ifdef MISSIONPACK +} + +static void CG_HarvesterSkulls( rectDef_t *rect, int font, float scale, vec4_t color, qboolean force2D, int textStyle ) { +#ifdef MISSIONPACK + char num[16]; + vec3_t origin, angles; + qhandle_t handle; + int value = cg.snap->ps.generic1; + + if ( cgs.gametype != GT_HARVESTER ) { + return; + } + + if ( value > 99 ) { + value = 99; + } + + Com_sprintf( num, sizeof( num ), "%i", value ); + value = CG_Text_Width( num, font, scale, 0 ); + CG_Text_Paint( rect->x + ( rect->w - value ), rect->y + rect->h, scale, color, num, 0, 0, textStyle ); + + if ( cg_drawIcons.integer ) { + if ( !force2D && cg_draw3dIcons.integer ) { + VectorClear( angles ); + origin[0] = 90; + origin[1] = 0; + origin[2] = -10; + angles[YAW] = ( cg.time & 2047 ) * 360 / 2048.0; + if ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_BLUE ) { + handle = cgs.media.redCubeModel; + } else { + handle = cgs.media.blueCubeModel; + } + CG_Draw3DModel( rect->x, rect->y, 35, 35, handle, 0, origin, angles ); + } else { + if ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_BLUE ) { + handle = cgs.media.redCubeIcon; + } else { + handle = cgs.media.blueCubeIcon; + } + CG_DrawPic( rect->x + 3, rect->y + 16, 20, 20, handle ); + } + } +#endif // #ifdef MISSIONPACK +} + +static void CG_OneFlagStatus( rectDef_t *rect ) { +#ifdef MISSIONPACK + if ( cgs.gametype != GT_1FCTF ) { + return; + } else { + gitem_t *item = BG_FindItemForPowerup( PW_NEUTRALFLAG ); + if ( item ) { + if ( cgs.flagStatus >= 0 && cgs.flagStatus <= 4 ) { + vec4_t color = {1, 1, 1, 1}; + int index = 0; + if ( cgs.flagStatus == FLAG_TAKEN_RED ) { + color[1] = color[2] = 0; + index = 1; + } else if ( cgs.flagStatus == FLAG_TAKEN_BLUE ) { + color[0] = color[1] = 0; + index = 1; + } else if ( cgs.flagStatus == FLAG_DROPPED ) { + index = 2; + } + trap_R_SetColor( color ); + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, cgs.media.flagShaders[index] ); + } + } + } +#endif // #ifdef MISSIONPACK +} + + +static void CG_DrawCTFPowerUp( rectDef_t *rect ) { +#ifdef MISSIONPACK + int value; + value = cg.snap->ps.stats[STAT_PERSISTANT_POWERUP]; + if ( value ) { + CG_RegisterItemVisuals( value ); + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, cg_items[ value ].icon ); + } +#endif // #ifdef MISSIONPACK +} + + + +static void CG_DrawTeamColor( rectDef_t *rect, vec4_t color ) { + CG_DrawTeamBackground( rect->x, rect->y, rect->w, rect->h, color[3], cg.snap->ps.persistant[PERS_TEAM] ); +} + + +static void CG_DrawAreaHoldable( rectDef_t *rect, int align, float spacing, int font, float scale, vec4_t color ) { +} + +static void CG_DrawAreaWeapons( rectDef_t *rect, int align, float spacing, int font, float scale, vec4_t color ) { +} + + +static void CG_DrawAreaPowerUp( rectDef_t *rect, int align, float spacing, int font, float scale, vec4_t color ) { + char num[16]; + int sorted[MAX_POWERUPS]; + int sortedTime[MAX_POWERUPS]; + int i, j, k; + int active; + playerState_t *ps; + int t; + gitem_t *item; + float f; + rectDef_t r2; + float *inc; + r2.x = rect->x; + r2.y = rect->y; + r2.w = rect->w; + r2.h = rect->h; + + inc = ( align == HUD_VERTICAL ) ? &r2.y : &r2.x; + + ps = &cg.snap->ps; + + if ( ps->stats[STAT_HEALTH] <= 0 ) { + return; + } + + // sort the list by time remaining + active = 0; + for ( i = 0 ; i < MAX_POWERUPS ; i++ ) { + if ( !ps->powerups[ i ] ) { + continue; + } + t = ps->powerups[ i ] - cg.time; + // ZOID--don't draw if the power up has unlimited time (999 seconds) + // This is true of the CTF flags + if ( t <= 0 || t >= 999000 ) { + continue; + } + + // insert into the list + for ( j = 0 ; j < active ; j++ ) { + if ( sortedTime[j] >= t ) { + for ( k = active - 1 ; k >= j ; k-- ) { + sorted[k + 1] = sorted[k]; + sortedTime[k + 1] = sortedTime[k]; + } + break; + } + } + sorted[j] = i; + sortedTime[j] = t; + active++; + } + + // draw the icons and timers + for ( i = 0 ; i < active ; i++ ) { + item = BG_FindItemForPowerup( sorted[i] ); + + if ( item ) { + t = ps->powerups[ sorted[i] ]; + if ( t - cg.time >= POWERUP_BLINKS * POWERUP_BLINK_TIME ) { + trap_R_SetColor( NULL ); + } else { + vec4_t modulate; + + f = (float)( t - cg.time ) / POWERUP_BLINK_TIME; + f -= (int)f; + modulate[0] = modulate[1] = modulate[2] = modulate[3] = f; + trap_R_SetColor( modulate ); + } + + CG_DrawPic( r2.x, r2.y, r2.w * .75, r2.h, trap_R_RegisterShader( item->icon ) ); + + Com_sprintf( num, sizeof( num ), "%i", sortedTime[i] / 1000 ); + CG_Text_Paint( r2.x + ( r2.w * .75 ) + 3, r2.y + r2.h, font, scale, color, num, 0, 0, 0 ); + *inc += r2.w + spacing; + } + + } + trap_R_SetColor( NULL ); + +} + +float CG_GetValue( int ownerDraw, int type ) { + centity_t *cent; + clientInfo_t *ci; + playerState_t *ps; + + cent = &cg_entities[cg.snap->ps.clientNum]; + ps = &cg.snap->ps; + + switch ( ownerDraw ) { + + case CG_SELECTEDPLAYER_ARMOR: + ci = cgs.clientinfo + sortedTeamPlayers[CG_GetSelectedPlayer()]; + return ci->armor; + break; + case CG_SELECTEDPLAYER_HEALTH: + ci = cgs.clientinfo + sortedTeamPlayers[CG_GetSelectedPlayer()]; + return ci->health; + break; + + case CG_PLAYER_ARMOR_VALUE: + return ps->stats[STAT_ARMOR]; + break; + case CG_PLAYER_AMMO_VALUE: + if ( cent->currentState.weapon ) { + if ( type == RANGETYPE_RELATIVE ) { + int weap = BG_FindAmmoForWeapon( cent->currentState.weapon ); + return (float)ps->ammo[weap] / (float)ammoTable[weap].maxammo; + } else { + return ps->ammo[BG_FindAmmoForWeapon( cent->currentState.weapon )]; + } + } + break; + case CG_PLAYER_AMMOCLIP_VALUE: + if ( cent->currentState.weapon ) { + if ( type == RANGETYPE_RELATIVE ) { + return (float)ps->ammoclip[BG_FindClipForWeapon( cent->currentState.weapon )] / (float)ammoTable[cent->currentState.weapon].maxclip; + } else { + return ps->ammoclip[BG_FindClipForWeapon( cent->currentState.weapon )]; + } + } + break; + case CG_PLAYER_SCORE: + return cg.snap->ps.persistant[PERS_SCORE]; + break; + case CG_PLAYER_HEALTH: + return ps->stats[STAT_HEALTH]; + break; + case CG_RED_SCORE: + return cgs.scores1; + break; + case CG_BLUE_SCORE: + return cgs.scores2; + break; + case CG_PLAYER_WEAPON_STABILITY: //----(SA) added + return ps->aimSpreadScale; + break; + +#define BONUSTIME 60000.0f +#define SPRINTTIME 20000.0f + + case CG_STAMINA: //----(SA) added + if ( type == RANGETYPE_RELATIVE ) { + return (float)cg.snap->ps.sprintTime / SPRINTTIME; + } else { + return cg.snap->ps.sprintTime; + } + break; + default: + break; + } + + return -1; +} + +qboolean CG_OtherTeamHasFlag() { +#ifdef MISSIONPACK + if ( cgs.gametype == GT_CTF || cgs.gametype == GT_1FCTF ) { + int team = cg.snap->ps.persistant[PERS_TEAM]; + if ( cgs.gametype == GT_1FCTF ) { + if ( team == TEAM_RED && cgs.flagStatus == FLAG_TAKEN_BLUE ) { + return qtrue; + } else if ( team == TEAM_BLUE && cgs.flagStatus == FLAG_TAKEN_RED ) { + return qtrue; + } else { + return qfalse; + } + } else { + if ( team == TEAM_RED && cgs.redflag == FLAG_TAKEN ) { + return qtrue; + } else if ( team == TEAM_BLUE && cgs.blueflag == FLAG_TAKEN ) { + return qtrue; + } else { + return qfalse; + } + } + } +#endif // #ifdef MISSIONPACK + return qfalse; +} + +qboolean CG_YourTeamHasFlag() { +#ifdef MISSIONPACK + if ( cgs.gametype == GT_CTF || cgs.gametype == GT_1FCTF ) { + int team = cg.snap->ps.persistant[PERS_TEAM]; + if ( cgs.gametype == GT_1FCTF ) { + if ( team == TEAM_RED && cgs.flagStatus == FLAG_TAKEN_RED ) { + return qtrue; + } else if ( team == TEAM_BLUE && cgs.flagStatus == FLAG_TAKEN_BLUE ) { + return qtrue; + } else { + return qfalse; + } + } else { + if ( team == TEAM_RED && cgs.blueflag == FLAG_TAKEN ) { + return qtrue; + } else if ( team == TEAM_BLUE && cgs.redflag == FLAG_TAKEN ) { + return qtrue; + } else { + return qfalse; + } + } + } +#endif // #ifdef MISSIONPACK + return qfalse; +} + +// THINKABOUTME: should these be exclusive or inclusive.. +// +qboolean CG_OwnerDrawVisible( int flags ) { + +//----(SA) added + if ( flags & CG_SHOW_NOT_V_BINOC ) { // if looking through binocs + if ( cg.zoomedBinoc ) { + return qfalse; + } + } + + if ( flags & CG_SHOW_NOT_V_SNIPER ) { // if looking through sniper scope + if ( cg.weaponSelect == WP_SNIPERRIFLE ) { + return qfalse; + } + } + + if ( flags & CG_SHOW_NOT_V_SNOOPER ) { // if looking through snooper scope + if ( cg.weaponSelect == WP_SNOOPERSCOPE ) { + return qfalse; + } + } + + if ( flags & CG_SHOW_NOT_V_FGSCOPE ) { // if looking through fg42 scope + if ( cg.weaponSelect == WP_FG42SCOPE ) { + return qfalse; + } + } + +//----(SA) end + + if ( flags & CG_SHOW_TEAMINFO ) { + return ( cg_currentSelectedPlayer.integer == numSortedTeamPlayers ); + } + + if ( flags & CG_SHOW_NOTEAMINFO ) { + return !( cg_currentSelectedPlayer.integer == numSortedTeamPlayers ); + } + + if ( flags & CG_SHOW_OTHERTEAMHASFLAG ) { + return CG_OtherTeamHasFlag(); + } + + if ( flags & CG_SHOW_YOURTEAMHASENEMYFLAG ) { + return CG_YourTeamHasFlag(); + } + +#ifdef MISSIONPACK + if ( flags & ( CG_SHOW_BLUE_TEAM_HAS_REDFLAG | CG_SHOW_RED_TEAM_HAS_BLUEFLAG ) ) { + if ( flags & CG_SHOW_BLUE_TEAM_HAS_REDFLAG && ( cgs.redflag == FLAG_TAKEN || cgs.flagStatus == FLAG_TAKEN_RED ) ) { + return qtrue; + } else if ( flags & CG_SHOW_RED_TEAM_HAS_BLUEFLAG && ( cgs.blueflag == FLAG_TAKEN || cgs.flagStatus == FLAG_TAKEN_BLUE ) ) { + return qtrue; + } + return qfalse; + } +#endif // #ifdef MISSIONPACK + + if ( flags & CG_SHOW_ANYTEAMGAME ) { + if ( cgs.gametype >= GT_TEAM ) { + return qtrue; + } + } + + if ( flags & CG_SHOW_ANYNONTEAMGAME ) { + if ( cgs.gametype < GT_TEAM ) { + return qtrue; + } + } + +#ifdef MISSIONPACK + if ( flags & CG_SHOW_HARVESTER ) { + if ( cgs.gametype == GT_HARVESTER ) { + return qtrue; + } else { + return qfalse; + } + } + + if ( flags & CG_SHOW_ONEFLAG ) { + if ( cgs.gametype == GT_1FCTF ) { + return qtrue; + } else { + return qfalse; + } + } + +#endif // #ifdef MISSIONPACK + if ( flags & CG_SHOW_CTF ) { + if ( cgs.gametype == GT_CTF ) { + return qtrue; + } + } + +#ifdef MISSIONPACK + if ( flags & CG_SHOW_OBELISK ) { + if ( cgs.gametype == GT_OBELISK ) { + return qtrue; + } else { + return qfalse; + } + } +#endif // #ifdef MISSIONPACK + + if ( flags & CG_SHOW_HEALTHCRITICAL ) { + if ( cg.snap->ps.stats[STAT_HEALTH] < 25 ) { + return qtrue; + } + } + + if ( flags & CG_SHOW_HEALTHOK ) { + if ( cg.snap->ps.stats[STAT_HEALTH] > 25 ) { + return qtrue; + } + } + + if ( flags & CG_SHOW_SINGLEPLAYER ) { + if ( cgs.gametype == GT_SINGLE_PLAYER ) { + return qtrue; + } + } + + if ( flags & CG_SHOW_TOURNAMENT ) { + if ( cgs.gametype == GT_TOURNAMENT ) { + return qtrue; + } + } + + if ( flags & CG_SHOW_DURINGINCOMINGVOICE ) { + } + +#ifdef MISSIONPACK + if ( flags & CG_SHOW_IF_PLAYER_HAS_FLAG ) { + if ( cg.snap->ps.powerups[PW_REDFLAG] || cg.snap->ps.powerups[PW_BLUEFLAG] || cg.snap->ps.powerups[PW_NEUTRALFLAG] ) { + return qtrue; + } + } +#endif // #ifdef MISSIONPACK + +//----(SA) added + if ( flags & CG_SHOW_NOT_V_CLEAR ) { + // if /not/ looking through binocs,snooper or sniper + if ( !cg.zoomedBinoc && !( cg.weaponSelect == WP_SNIPERRIFLE ) && !( cg.weaponSelect == WP_SNOOPERSCOPE ) && !( cg.weaponSelect == WP_FG42SCOPE ) ) { + return qfalse; + } + } + + if ( flags & ( CG_SHOW_NOT_V_BINOC | CG_SHOW_NOT_V_SNIPER | CG_SHOW_NOT_V_SNOOPER | CG_SHOW_NOT_V_FGSCOPE ) ) { + // setting any of these does not necessarily disable drawing in regular view + // CG_SHOW_NOT_V_CLEAR must also be set to hide for reg view + if ( !( flags & CG_SHOW_NOT_V_CLEAR ) ) { + return qtrue; + } + } + +//----(SA) end + + + return qfalse; +} + + + +static void CG_DrawPlayerHasFlag( rectDef_t *rect, qboolean force2D ) { +#ifdef MISSIONPACK + int adj = ( force2D ) ? 0 : 2; + if ( cg.predictedPlayerState.powerups[PW_REDFLAG] ) { + CG_DrawFlagModel( rect->x + adj, rect->y + adj, rect->w - adj, rect->h - adj, TEAM_RED, force2D ); + } else if ( cg.predictedPlayerState.powerups[PW_BLUEFLAG] ) { + CG_DrawFlagModel( rect->x + adj, rect->y + adj, rect->w - adj, rect->h - adj, TEAM_BLUE, force2D ); + } else if ( cg.predictedPlayerState.powerups[PW_NEUTRALFLAG] ) { + CG_DrawFlagModel( rect->x + adj, rect->y + adj, rect->w - adj, rect->h - adj, TEAM_FREE, force2D ); + } +#endif // #ifdef MISSIONPACK +} + +static void CG_DrawAreaSystemChat( rectDef_t *rect, int font, float scale, vec4_t color, qhandle_t shader ) { + CG_Text_Paint( rect->x, rect->y + rect->h, font, scale, color, systemChat, 0, 0, 0 ); +} + +static void CG_DrawAreaTeamChat( rectDef_t *rect, int font, float scale, vec4_t color, qhandle_t shader ) { + CG_Text_Paint( rect->x, rect->y + rect->h, font, scale, color,teamChat1, 0, 0, 0 ); +} + +static void CG_DrawAreaChat( rectDef_t *rect, int font, float scale, vec4_t color, qhandle_t shader ) { + CG_Text_Paint( rect->x, rect->y + rect->h, font, scale, color, teamChat2, 0, 0, 0 ); +} + +const char *CG_GetKillerText() { + const unsigned char *s = ""; + if ( cg.killerName[0] ) { + s = va( "Fragged by %s", cg.killerName ); + } + return s; +} + + +static void CG_DrawKiller( rectDef_t *rect, int font, float scale, vec4_t color, qhandle_t shader, int textStyle ) { + // fragged by ... line + if ( cg.killerName[0] ) { + int x = rect->x + rect->w / 2; + CG_Text_Paint( x - CG_Text_Width( CG_GetKillerText(),font, scale, 0 ) / 2, rect->y + rect->h, font, scale, color, CG_GetKillerText(), 0, 0, textStyle ); + } + +} + + +static void CG_DrawCapFragLimit( rectDef_t *rect, int font, float scale, vec4_t color, qhandle_t shader, int textStyle ) { + int limit = ( cgs.gametype >= GT_CTF ) ? cgs.capturelimit : cgs.fraglimit; + CG_Text_Paint( rect->x, rect->y, font, scale, color, va( "%2i", limit ),0, 0, textStyle ); +} + +static void CG_Draw1stPlace( rectDef_t *rect, int font, float scale, vec4_t color, qhandle_t shader, int textStyle ) { + if ( cgs.scores1 != SCORE_NOT_PRESENT ) { + CG_Text_Paint( rect->x, rect->y, font, scale, color, va( "%2i", cgs.scores1 ),0, 0, textStyle ); + } +} + +static void CG_Draw2ndPlace( rectDef_t *rect, int font, float scale, vec4_t color, qhandle_t shader, int textStyle ) { + if ( cgs.scores2 != SCORE_NOT_PRESENT ) { + CG_Text_Paint( rect->x, rect->y, font, scale, color, va( "%2i", cgs.scores2 ),0, 0, textStyle ); + } +} + +const char *CG_GetGameStatusText() { + const unsigned char *s = ""; + if ( cgs.gametype < GT_TEAM ) { + if ( cg.snap->ps.persistant[PERS_TEAM] != TEAM_SPECTATOR ) { + s = va( "%s place with %i",CG_PlaceString( cg.snap->ps.persistant[PERS_RANK] + 1 ),cg.snap->ps.persistant[PERS_SCORE] ); + } + } else { + if ( cg.teamScores[0] == cg.teamScores[1] ) { + s = va( "Teams are tied at %i", cg.teamScores[0] ); + } else if ( cg.teamScores[0] >= cg.teamScores[1] ) { + s = va( "Red leads Blue, %i to %i", cg.teamScores[0], cg.teamScores[1] ); + } else { + s = va( "Blue leads Red, %i to %i", cg.teamScores[1], cg.teamScores[0] ); + } + } + return s; +} + +static void CG_DrawGameStatus( rectDef_t *rect, int font, float scale, vec4_t color, qhandle_t shader, int textStyle ) { + CG_Text_Paint( rect->x, rect->y + rect->h, font, scale, color, CG_GetGameStatusText(), 0, 0, textStyle ); +} + +const char *CG_GameTypeString() { + if ( cgs.gametype == GT_FFA ) { + return "Free For All"; + } else if ( cgs.gametype == GT_TEAM ) { + return "Team Deathmatch"; + } else if ( cgs.gametype == GT_CTF ) { + return "Capture the Flag"; +#ifdef MISSIONPACK + } else if ( cgs.gametype == GT_1FCTF ) { + return "One Flag CTF"; + } else if ( cgs.gametype == GT_OBELISK ) { + return "Overload"; + } else if ( cgs.gametype == GT_HARVESTER ) { + return "Harvester"; +#endif // #ifdef MISSIONPACK + } + return ""; +} +static void CG_DrawGameType( rectDef_t *rect, int font, float scale, vec4_t color, qhandle_t shader, int textStyle ) { + CG_Text_Paint( rect->x, rect->y + rect->h, font, scale, color, CG_GameTypeString(), 0, 0, textStyle ); +} + +static void CG_Text_Paint_Limit( float *maxX, float x, float y, int font, float scale, vec4_t color, const char* text, float adjust, int limit ) { + int len, count; + vec4_t newColor; + glyphInfo_t *glyph; + if ( text ) { + const unsigned char *s = text; + float max = *maxX; + float useScale; + + fontInfo_t *fnt = &cgDC.Assets.textFont; + if ( font == UI_FONT_DEFAULT ) { + if ( scale <= cg_smallFont.value ) { + fnt = &cgDC.Assets.smallFont; + } else if ( scale > cg_bigFont.value ) { + fnt = &cgDC.Assets.bigFont; + } + } else if ( font == UI_FONT_BIG ) { + fnt = &cgDC.Assets.bigFont; + } else if ( font == UI_FONT_SMALL ) { + fnt = &cgDC.Assets.smallFont; + } else if ( font == UI_FONT_HANDWRITING ) { + fnt = &cgDC.Assets.handwritingFont; + } + + useScale = scale * fnt->glyphScale; + trap_R_SetColor( color ); + len = strlen( text ); + if ( limit > 0 && len > limit ) { + len = limit; + } + count = 0; + while ( s && *s && count < len ) { + glyph = &fnt->glyphs[*s]; + if ( Q_IsColorString( s ) ) { + memcpy( newColor, g_color_table[ColorIndex( *( s + 1 ) )], sizeof( newColor ) ); + newColor[3] = color[3]; + trap_R_SetColor( newColor ); + s += 2; + continue; + } else { + float yadj = useScale * glyph->top; + if ( CG_Text_Width( s, font, useScale, 1 ) + x > max ) { + *maxX = 0; + break; + } + CG_Text_PaintChar( x, y - yadj, + glyph->imageWidth, + glyph->imageHeight, + useScale, + glyph->s, + glyph->t, + glyph->s2, + glyph->t2, + glyph->glyph ); + x += ( glyph->xSkip * useScale ) + adjust; + *maxX = x; + count++; + s++; + } + } + trap_R_SetColor( NULL ); + } + +} + + + +#define PIC_WIDTH 12 + +void CG_DrawNewTeamInfo( rectDef_t *rect, float text_x, float text_y, int font, float scale, vec4_t color, qhandle_t shader ) { +#ifdef MISSIONPACK + int xx; + float y; + int i, j, len, count; + const char *p; + vec4_t hcolor; + float pwidth, lwidth, maxx, leftOver; + clientInfo_t *ci; + gitem_t *item; + qhandle_t h; + + // max player name width + pwidth = 0; + count = ( numSortedTeamPlayers > 8 ) ? 8 : numSortedTeamPlayers; + for ( i = 0; i < count; i++ ) { + ci = cgs.clientinfo + sortedTeamPlayers[i]; + if ( ci->infoValid && ci->team == cg.snap->ps.persistant[PERS_TEAM] ) { + len = CG_Text_Width( ci->name, font, scale, 0 ); + if ( len > pwidth ) { + pwidth = len; + } + } + } + + // max location name width + lwidth = 0; + for ( i = 1; i < MAX_LOCATIONS; i++ ) { + p = CG_ConfigString( CS_LOCATIONS + i ); + if ( p && *p ) { + len = CG_Text_Width( p, font, scale, 0 ); + if ( len > lwidth ) { + lwidth = len; + } + } + } + + y = rect->y; + + for ( i = 0; i < count; i++ ) { + ci = cgs.clientinfo + sortedTeamPlayers[i]; + if ( ci->infoValid && ci->team == cg.snap->ps.persistant[PERS_TEAM] ) { + + xx = rect->x + 1; + for ( j = 0; j <= PW_NUM_POWERUPS; j++ ) { + if ( ci->powerups & ( 1 << j ) ) { + + item = BG_FindItemForPowerup( j ); + + if ( item ) { + CG_DrawPic( xx, y, PIC_WIDTH, PIC_WIDTH, trap_R_RegisterShader( item->icon ) ); + xx += PIC_WIDTH; + } + } + } + + // FIXME: max of 3 powerups shown properly + xx = rect->x + ( PIC_WIDTH * 3 ) + 2; + + CG_GetColorForHealth( ci->health, ci->armor, hcolor ); + trap_R_SetColor( hcolor ); + CG_DrawPic( xx, y + 1, PIC_WIDTH - 2, PIC_WIDTH - 2, cgs.media.heartShader ); + + //Com_sprintf (st, sizeof(st), "%3i %3i", ci->health, ci->armor); + //CG_Text_Paint(xx, y + text_y, scale, hcolor, st, 0, 0); + + // draw weapon icon + xx += PIC_WIDTH + 1; + +// weapon used is not that useful, use the space for task +#if 0 + if ( cg_weapons[ci->curWeapon].weaponIcon ) { + CG_DrawPic( xx, y, PIC_WIDTH, PIC_WIDTH, cg_weapons[ci->curWeapon].weaponIcon ); + } else { + CG_DrawPic( xx, y, PIC_WIDTH, PIC_WIDTH, cgs.media.deferShader ); + } +#endif + + trap_R_SetColor( NULL ); + if ( cgs.orderPending ) { + // blink the icon + if ( cg.time > cgs.orderTime - 2500 && ( cg.time >> 9 ) & 1 ) { + h = 0; + } else { + h = CG_StatusHandle( cgs.currentOrder ); + } + } else { + h = CG_StatusHandle( ci->teamTask ); + } + + if ( h ) { + CG_DrawPic( xx, y, PIC_WIDTH, PIC_WIDTH, h ); + } + + xx += PIC_WIDTH + 1; + + leftOver = rect->w - xx; + maxx = xx + leftOver / 3; + + + + CG_Text_Paint_Limit( &maxx, xx, y + text_y, UI_FONT_DEFAULT, scale, color, ci->name, 0, 0 ); + + p = CG_ConfigString( CS_LOCATIONS + ci->location ); + if ( !p || !*p ) { + p = "unknown"; + } + + xx += leftOver / 3 + 2; + maxx = rect->w - 4; + + CG_Text_Paint_Limit( &maxx, xx, y + text_y, UI_FONT_DEFAULT, scale, color, p, 0, 0 ); + y += text_y + 2; + if ( y + text_y + 2 > rect->y + rect->h ) { + break; + } + + } + } +#endif // #ifdef MISSIONPACK +} + + +void CG_DrawTeamSpectators( rectDef_t *rect, int font, float scale, vec4_t color, qhandle_t shader ) { + if ( cg.spectatorLen ) { + float maxX; + + if ( cg.spectatorWidth == -1 ) { + cg.spectatorWidth = 0; + cg.spectatorPaintX = rect->x + 1; + cg.spectatorPaintX2 = -1; + } + + if ( cg.spectatorOffset > cg.spectatorLen ) { + cg.spectatorOffset = 0; + cg.spectatorPaintX = rect->x + 1; + cg.spectatorPaintX2 = -1; + } + + if ( cg.time > cg.spectatorTime ) { + cg.spectatorTime = cg.time + 10; + if ( cg.spectatorPaintX <= rect->x + 2 ) { + if ( cg.spectatorOffset < cg.spectatorLen ) { + cg.spectatorPaintX += CG_Text_Width( &cg.spectatorList[cg.spectatorOffset], font, scale, 1 ) - 1; + cg.spectatorOffset++; + } else { + cg.spectatorOffset = 0; + if ( cg.spectatorPaintX2 >= 0 ) { + cg.spectatorPaintX = cg.spectatorPaintX2; + } else { + cg.spectatorPaintX = rect->x + rect->w - 2; + } + cg.spectatorPaintX2 = -1; + } + } else { + cg.spectatorPaintX--; + if ( cg.spectatorPaintX2 >= 0 ) { + cg.spectatorPaintX2--; + } + } + } + + maxX = rect->x + rect->w - 2; + CG_Text_Paint_Limit( &maxX, cg.spectatorPaintX, rect->y + rect->h - 3, UI_FONT_DEFAULT, scale, color, &cg.spectatorList[cg.spectatorOffset], 0, 0 ); + if ( cg.spectatorPaintX2 >= 0 ) { + float maxX2 = rect->x + rect->w - 2; + CG_Text_Paint_Limit( &maxX2, cg.spectatorPaintX2, rect->y + rect->h - 3, UI_FONT_DEFAULT, scale, color, cg.spectatorList, 0, cg.spectatorOffset ); + } + if ( cg.spectatorOffset && maxX > 0 ) { + // if we have an offset ( we are skipping the first part of the string ) and we fit the string + if ( cg.spectatorPaintX2 == -1 ) { + cg.spectatorPaintX2 = rect->x + rect->w - 2; + } + } else { + cg.spectatorPaintX2 = -1; + } + + } +} + + + +void CG_DrawMedal( int ownerDraw, rectDef_t *rect, int font, float scale, vec4_t color, qhandle_t shader ) { + score_t *score = &cg.scores[cg.selectedScore]; + float value = 0; + char *text = NULL; + color[3] = 0.25; + + switch ( ownerDraw ) { + case CG_ACCURACY: + value = score->accuracy; + break; + case CG_ASSISTS: + value = score->assistCount; + break; + case CG_DEFEND: + value = score->defendCount; + break; + case CG_EXCELLENT: + value = score->excellentCount; + break; + case CG_IMPRESSIVE: + value = score->impressiveCount; + break; + case CG_PERFECT: + value = score->perfect; + break; + case CG_GAUNTLET: + value = score->guantletCount; + break; + case CG_CAPTURES: + value = score->captures; + break; + } + + if ( value > 0 ) { + if ( ownerDraw != CG_PERFECT ) { + if ( ownerDraw == CG_ACCURACY ) { + text = va( "%i%%", (int)value ); + if ( value > 50 ) { + color[3] = 1.0; + } + } else { + text = va( "%i", (int)value ); + color[3] = 1.0; + } + } else { + if ( value ) { + color[3] = 1.0; + } + text = "Wow"; + } + } + + trap_R_SetColor( color ); + CG_DrawPic( rect->x, rect->y, rect->w, rect->h, shader ); + + if ( text ) { + color[3] = 1.0; + value = CG_Text_Width( text, font, scale, 0 ); + CG_Text_Paint( rect->x + ( rect->w - value ) / 2, rect->y + rect->h + 10, font, scale, color, text, 0, 0, 0 ); + } + trap_R_SetColor( NULL ); + +} + + +/* +============== +CG_DrawWeapStability + draw a bar showing current stability level (0-255), max at current weapon/ability, and 'perfect' reference mark + + probably only drawn for scoped weapons +============== +*/ +void CG_DrawWeapStability( rectDef_t *rect, vec4_t color, int align ) { + vec4_t goodColor = {0, 1, 0, 0.5f}, badColor = {1, 0, 0, 0.5f}; + + if ( !cg_drawSpreadScale.integer ) { + return; + } + + if ( cg_drawSpreadScale.integer == 1 && !( cg.weaponSelect == WP_SNOOPERSCOPE || cg.weaponSelect == WP_SNIPERRIFLE || cg.weaponSelect == WP_FG42SCOPE ) ) { + // cg_drawSpreadScale of '1' means only draw for scoped weapons, '2' means draw all the time (for debugging) + return; + } + + if ( !( cg.snap->ps.aimSpreadScale ) ) { + return; + } + + CG_FilledBar( rect->x, rect->y, rect->w, rect->h, goodColor, badColor, NULL, (float)cg.snap->ps.aimSpreadScale / 255.0f, 2 | 4 | 256 ); // flags (BAR_CENTER|BAR_VERT|BAR_LERP_COLOR) +} + + +/* +============== +CG_DrawWeapHeat +============== +*/ +void CG_DrawWeapHeat( rectDef_t *rect, int align ) { + vec4_t color = {1, 0, 0, 0.2f}, color2 = {1, 0, 0, 0.5f}; + int flags = 0; + + if ( !( cg.snap->ps.curWeapHeat ) ) { + return; + } + + if ( align != HUD_HORIZONTAL ) { + flags |= 4; // BAR_VERT + + } + flags |= 1; // BAR_LEFT - this is hardcoded now, but will be decided by the menu script + flags |= 16; // BAR_BG - draw the filled contrast box +// flags|=32; // BAR_BGSPACING_X0Y5 - different style + + flags |= 256; // BAR_COLOR_LERP + CG_FilledBar( rect->x, rect->y, rect->w, rect->h, color, color2, NULL, (float)cg.snap->ps.curWeapHeat / 255.0f, flags ); +} + + +/* +============== +CG_DrawFatigue +============== +*/ + +static void CG_DrawFatigue( rectDef_t *rect, vec4_t color, int align ) { + // vec4_t color = {0, 1, 0, 1}, color2 = {1, 0, 0, 1}; + vec4_t colorBonus = {1, 1, 0, 0.45f}; // yellow (a little more solid for the 'bonus' stamina) + float barFrac; //, omBarFrac; + int flags = 0; + float chargeTime; // DHM - Nerve + + barFrac = (float)cg.snap->ps.sprintTime / SPRINTTIME; +// omBarFrac = 1.0f-barFrac; + + if ( align != HUD_HORIZONTAL ) { + flags |= 4; // BAR_VERT + flags |= 1; // BAR_LEFT (left, when vertical means grow 'up') + } + + CG_FilledBar( rect->x, rect->y, rect->w, rect->h, color, NULL, NULL, (float)cg.snap->ps.sprintTime / SPRINTTIME, flags ); + + // fill in the left side of the bar with the counter for the nofatigue powerup + if ( cg.snap->ps.powerups[PW_NOFATIGUE] ) { + CG_FilledBar( rect->x, rect->y, rect->w / 2, rect->h, colorBonus, NULL, NULL, cg.snap->ps.powerups[PW_NOFATIGUE] / BONUSTIME, flags ); + } +// JPW NERVE -- added drawWeaponPercent in multiplayer, drawn to left + if ( cgs.gametype != GT_SINGLE_PLAYER ) { + + if ( cg.snap->ps.stats[ STAT_PLAYER_CLASS ] == PC_ENGINEER ) { + chargeTime = cg_engineerChargeTime.value; + } else if ( cg.snap->ps.stats[ STAT_PLAYER_CLASS ] == PC_MEDIC ) { + chargeTime = cg_medicChargeTime.value; + } else if ( cg.snap->ps.stats[ STAT_PLAYER_CLASS ] == PC_LT ) { + chargeTime = cg_LTChargeTime.value; + } else { + chargeTime = cg_soldierChargeTime.value; + } + + barFrac = (float)( cg.time - cg.snap->ps.classWeaponTime ) / chargeTime; + + if ( barFrac > 1.0 ) { + barFrac = 1.0; + } + + color[0] = 1.0f; + color[1] = color[2] = barFrac; + color[3] = 0.25 + barFrac * 0.5; + + CG_FilledBar( rect->x - rect->w, rect->y, rect->w / 2, rect->h, color, NULL, NULL, barFrac, flags ); + } +// jpw +} + + +/* +============== +CG_OwnerDraw +============== +*/ +void CG_OwnerDraw( float x, float y, float w, float h, float text_x, float text_y, int ownerDraw, int ownerDrawFlags, int align, float special, int font, float scale, vec4_t color, qhandle_t shader, int textStyle ) { + rectDef_t rect; + + if ( cg_drawStatus.integer == 0 ) { + return; + } + + //if (ownerDrawFlags != 0 && !CG_OwnerDrawVisible(ownerDrawFlags)) { + // return; + //} + + rect.x = x; + rect.y = y; + rect.w = w; + rect.h = h; + + switch ( ownerDraw ) { + case CG_PLAYER_WEAPON_ICON2D: + CG_DrawPlayerWeaponIcon( &rect, ownerDrawFlags & CG_SHOW_HIGHLIGHTED, align ); + break; + case CG_PLAYER_ARMOR_ICON: + CG_DrawPlayerArmorIcon( &rect, ownerDrawFlags & CG_SHOW_2DONLY ); + break; + case CG_PLAYER_ARMOR_ICON2D: + CG_DrawPlayerArmorIcon( &rect, qtrue ); + break; + case CG_PLAYER_ARMOR_VALUE: + CG_DrawPlayerArmorValue( &rect, font, scale, color, shader, textStyle ); + break; + case CG_PLAYER_AMMO_ICON: + CG_DrawPlayerAmmoIcon( &rect, ownerDrawFlags & CG_SHOW_2DONLY ); + break; + case CG_PLAYER_AMMO_ICON2D: + CG_DrawPlayerAmmoIcon( &rect, qtrue ); + break; + case CG_PLAYER_AMMO_VALUE: + CG_DrawPlayerAmmoValue( &rect, font, scale, color, shader, textStyle, 0 ); + break; + case CG_CURSORHINT: + CG_DrawCursorhint( &rect ); + break; +//----(SA) added + case CG_NEWMESSAGE: + CG_DrawMessageIcon( &rect ); + break; +//----(SA) end + case CG_PLAYER_AMMOCLIP_VALUE: + CG_DrawPlayerAmmoValue( &rect, font, scale, color, shader, textStyle, 1 ); + break; + case CG_SELECTEDPLAYER_HEAD: + CG_DrawSelectedPlayerHead( &rect, ownerDrawFlags & CG_SHOW_2DONLY, qfalse ); + break; + case CG_VOICE_HEAD: + CG_DrawSelectedPlayerHead( &rect, ownerDrawFlags & CG_SHOW_2DONLY, qtrue ); + break; + case CG_VOICE_NAME: + CG_DrawSelectedPlayerName( &rect, font, scale, color, qtrue, textStyle ); + break; + case CG_SELECTEDPLAYER_STATUS: + CG_DrawSelectedPlayerStatus( &rect ); + break; + case CG_SELECTEDPLAYER_ARMOR: + CG_DrawSelectedPlayerArmor( &rect, font, scale, color, shader, textStyle ); + break; + case CG_SELECTEDPLAYER_HEALTH: + CG_DrawSelectedPlayerHealth( &rect, font, scale, color, shader, textStyle ); + break; + case CG_SELECTEDPLAYER_NAME: + CG_DrawSelectedPlayerName( &rect, font, scale, color, qfalse, textStyle ); + break; + case CG_SELECTEDPLAYER_LOCATION: + CG_DrawSelectedPlayerLocation( &rect, font, scale, color, textStyle ); + break; + case CG_SELECTEDPLAYER_WEAPON: + CG_DrawSelectedPlayerWeapon( &rect ); + break; + case CG_SELECTEDPLAYER_POWERUP: + CG_DrawSelectedPlayerPowerup( &rect, ownerDrawFlags & CG_SHOW_2DONLY ); + break; + case CG_PLAYER_WEAPON_HEAT: + CG_DrawWeapHeat( &rect, align ); + break; + case CG_PLAYER_WEAPON_STABILITY: + CG_DrawWeapStability( &rect, color, align ); + break; + case CG_STAMINA: + CG_DrawFatigue( &rect, color, align ); + break; + case CG_PLAYER_HEAD: + CG_DrawPlayerHead( &rect, ownerDrawFlags & CG_SHOW_2DONLY ); + break; + case CG_PLAYER_HOLDABLE: + CG_DrawHoldableItem( &rect, font, scale, ownerDrawFlags & CG_SHOW_2DONLY ); + break; + case CG_PLAYER_ITEM: + CG_DrawPlayerItem( &rect, font, scale, ownerDrawFlags & CG_SHOW_2DONLY ); + break; + case CG_PLAYER_SCORE: + CG_DrawPlayerScore( &rect, font, scale, color, shader, textStyle ); + break; + case CG_PLAYER_HEALTH: + CG_DrawPlayerHealth( &rect, font, scale, color, shader, textStyle ); + break; + case CG_RED_SCORE: + CG_DrawRedScore( &rect, font, scale, color, shader, textStyle ); + break; + case CG_BLUE_SCORE: + CG_DrawBlueScore( &rect, font, scale, color, shader, textStyle ); + break; + case CG_RED_NAME: + CG_DrawRedName( &rect, font, scale, color, textStyle ); + break; + case CG_BLUE_NAME: + CG_DrawBlueName( &rect, font, scale, color, textStyle ); + break; + case CG_BLUE_FLAGHEAD: + CG_DrawBlueFlagHead( &rect ); + break; + case CG_BLUE_FLAGSTATUS: + CG_DrawBlueFlagStatus( &rect, shader ); + break; + case CG_BLUE_FLAGNAME: + CG_DrawBlueFlagName( &rect, font, scale, color, textStyle ); + break; + case CG_RED_FLAGHEAD: + CG_DrawRedFlagHead( &rect ); + break; + case CG_RED_FLAGSTATUS: + CG_DrawRedFlagStatus( &rect, shader ); + break; + case CG_RED_FLAGNAME: + CG_DrawRedFlagName( &rect, font, scale, color, textStyle ); + break; + case CG_HARVESTER_SKULLS: + CG_HarvesterSkulls( &rect, font, scale, color, qfalse, textStyle ); + break; + case CG_HARVESTER_SKULLS2D: + CG_HarvesterSkulls( &rect, font, scale, color, qtrue, textStyle ); + break; + case CG_ONEFLAG_STATUS: + CG_OneFlagStatus( &rect ); + break; + case CG_PLAYER_LOCATION: + CG_DrawPlayerLocation( &rect, font, scale, color, textStyle ); + break; + case CG_TEAM_COLOR: + CG_DrawTeamColor( &rect, color ); + break; + case CG_CTF_POWERUP: + CG_DrawCTFPowerUp( &rect ); + break; + case CG_AREA_WEAPON: + CG_DrawAreaWeapons( &rect, align, special, font, scale, color ); + break; + case CG_AREA_HOLDABLE: + CG_DrawAreaHoldable( &rect, align, special, font, scale, color ); + break; + case CG_AREA_POWERUP: + CG_DrawAreaPowerUp( &rect, align, special, font, scale, color ); + break; + case CG_PLAYER_STATUS: + CG_DrawPlayerStatus( &rect ); + break; + case CG_PLAYER_HASFLAG: + CG_DrawPlayerHasFlag( &rect, qfalse ); + break; + case CG_PLAYER_HASFLAG2D: + CG_DrawPlayerHasFlag( &rect, qtrue ); + break; + case CG_AREA_SYSTEMCHAT: + CG_DrawAreaSystemChat( &rect, font, scale, color, shader ); + break; + case CG_AREA_TEAMCHAT: + CG_DrawAreaTeamChat( &rect, font, scale, color, shader ); + break; + case CG_AREA_CHAT: + CG_DrawAreaChat( &rect, font, scale, color, shader ); + break; + case CG_GAME_TYPE: + CG_DrawGameType( &rect, font, scale, color, shader, textStyle ); + break; + case CG_GAME_STATUS: + CG_DrawGameStatus( &rect, font, scale, color, shader, textStyle ); + break; + case CG_KILLER: + CG_DrawKiller( &rect, font, scale, color, shader, textStyle ); + break; + case CG_ACCURACY: + case CG_ASSISTS: + case CG_DEFEND: + case CG_EXCELLENT: + case CG_IMPRESSIVE: + case CG_PERFECT: + case CG_GAUNTLET: + case CG_CAPTURES: + CG_DrawMedal( ownerDraw, &rect, font, scale, color, shader ); + break; + case CG_SPECTATORS: + CG_DrawTeamSpectators( &rect, font, scale, color, shader ); + break; + case CG_TEAMINFO: + if ( cg_currentSelectedPlayer.integer == numSortedTeamPlayers ) { + CG_DrawNewTeamInfo( &rect, text_x, text_y, font, scale, color, shader ); + } + break; + case CG_CAPFRAGLIMIT: + CG_DrawCapFragLimit( &rect, font, scale, color, shader, textStyle ); + break; + case CG_1STPLACE: + CG_Draw1stPlace( &rect, font, scale, color, shader, textStyle ); + break; + case CG_2NDPLACE: + CG_Draw2ndPlace( &rect, font, scale, color, shader, textStyle ); + break; + default: + break; + } +} + +void CG_MouseEvent( int x, int y ) { + int n; + + if ( ( cg.predictedPlayerState.pm_type == PM_NORMAL || cg.predictedPlayerState.pm_type == PM_SPECTATOR ) && cg.showScores == qfalse ) { + trap_Key_SetCatcher( 0 ); + return; + } + + cgs.cursorX += x; + if ( cgs.cursorX < 0 ) { + cgs.cursorX = 0; + } else if ( cgs.cursorX > 640 ) { + cgs.cursorX = 640; + } + + cgs.cursorY += y; + if ( cgs.cursorY < 0 ) { + cgs.cursorY = 0; + } else if ( cgs.cursorY > 480 ) { + cgs.cursorY = 480; + } + + n = Display_CursorType( cgs.cursorX, cgs.cursorY ); + cgs.activeCursor = 0; + if ( n == CURSOR_ARROW ) { + cgs.activeCursor = cgs.media.selectCursor; + } else if ( n == CURSOR_SIZER ) { + cgs.activeCursor = cgs.media.sizeCursor; + } + + if ( cgs.capturedItem ) { + Display_MouseMove( cgs.capturedItem, x, y ); + } else { + Display_MouseMove( NULL, cgs.cursorX, cgs.cursorY ); + } + +} + +/* +================== +CG_HideTeamMenus +================== + +*/ +void CG_HideTeamMenu() { + Menus_CloseByName( "teamMenu" ); + Menus_CloseByName( "getMenu" ); +} + +/* +================== +CG_ShowTeamMenus +================== + +*/ +void CG_ShowTeamMenu() { + Menus_OpenByName( "teamMenu" ); +} + + + + +/* +================== +CG_EventHandling +================== + type 0 - no event handling + 1 - team menu + 2 - hud editor + +*/ +void CG_EventHandling( int type ) { + cgs.eventHandling = type; + if ( type == CGAME_EVENT_NONE ) { + CG_HideTeamMenu(); + } else if ( type == CGAME_EVENT_TEAMMENU ) { + //CG_ShowTeamMenu(); + } else if ( type == CGAME_EVENT_SCOREBOARD ) { + } + +} + + + +void CG_KeyEvent( int key, qboolean down ) { + + if ( !down ) { + return; + } + + if ( cg.predictedPlayerState.pm_type == PM_NORMAL || ( cg.predictedPlayerState.pm_type == PM_SPECTATOR && cg.showScores == qfalse ) ) { + CG_EventHandling( CGAME_EVENT_NONE ); + trap_Key_SetCatcher( 0 ); + return; + } + + //if (key == trap_Key_GetKey("teamMenu") || !Display_CaptureItem(cgs.cursorX, cgs.cursorY)) { + // if we see this then we should always be visible + // CG_EventHandling(CGAME_EVENT_NONE); + // trap_Key_SetCatcher(0); + //} + + + + Display_HandleKey( key, down, cgs.cursorX, cgs.cursorY ); + + if ( cgs.capturedItem ) { + cgs.capturedItem = NULL; + } else { + if ( key == K_MOUSE2 && down ) { + cgs.capturedItem = Display_CaptureItem( cgs.cursorX, cgs.cursorY ); + } + } +} + +int CG_ClientNumFromName( const char *p ) { + int i; + for ( i = 0; i < cgs.maxclients; i++ ) { + if ( cgs.clientinfo[i].infoValid && Q_stricmp( cgs.clientinfo[i].name, p ) == 0 ) { + return i; + } + } + return -1; +} + +void CG_ShowResponseHead() { + Menus_OpenByName( "voiceMenu" ); + trap_Cvar_Set( "cl_conXOffset", "72" ); + cg.voiceTime = cg.time; +} + +void CG_RunMenuScript( char **args ) { +} + + +void CG_GetTeamColor( vec4_t *color ) { + if ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_RED ) { + ( *color )[0] = 1; + ( *color )[3] = .25f; + ( *color )[1] = ( *color )[2] = 0; + } else if ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_BLUE ) { + ( *color )[0] = ( *color )[1] = 0; + ( *color )[2] = 1; + ( *color )[3] = .25f; + } else { + ( *color )[0] = ( *color )[2] = 0; + ( *color )[1] = .17f; + ( *color )[3] = .25f; + } +} + diff --git a/src/cgame/cg_particles.c b/src/cgame/cg_particles.c new file mode 100644 index 0000000..c4f7774 --- /dev/null +++ b/src/cgame/cg_particles.c @@ -0,0 +1,2626 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// Rafael particles +// cg_particles.c + +#include "cg_local.h" + +#define MUSTARD 1 +#define BLOODRED 2 +#define EMISIVEFADE 3 +#define GREY75 4 +#define ZOMBIE 5 + +typedef struct particle_s +{ + struct particle_s *next; + + float time; + float endtime; + + vec3_t org; + vec3_t vel; + vec3_t accel; + int color; + float colorvel; + float alpha; + float alphavel; + int type; + qhandle_t pshader; + + float height; + float width; + + float endheight; + float endwidth; + + float start; + float end; + + float startfade; + qboolean rotate; + int snum; + + qboolean link; + + // Ridah + int shaderAnim; + int roll; + + int accumroll; + +} cparticle_t; + +typedef enum +{ + P_NONE, + P_WEATHER, + P_FLAT, + P_SMOKE, + P_ROTATE, + P_WEATHER_TURBULENT, + P_ANIM, // Ridah + P_BAT, + P_BLEED, + P_FLAT_SCALEUP, + P_FLAT_SCALEUP_FADE, + P_WEATHER_FLURRY, + P_SMOKE_IMPACT, + P_BUBBLE, + P_BUBBLE_TURBULENT, + P_SPRITE +} particle_type_t; + +#define MAX_SHADER_ANIMS 8 +#define MAX_SHADER_ANIM_FRAMES 64 +static char *shaderAnimNames[MAX_SHADER_ANIMS] = { + "explode1", + "blacksmokeanim", + "twiltb2", + "expblue", + "blacksmokeanimb", // uses 'explode1' sequence + "blood", + NULL +}; +static qhandle_t shaderAnims[MAX_SHADER_ANIMS][MAX_SHADER_ANIM_FRAMES]; +static int shaderAnimCounts[MAX_SHADER_ANIMS] = { + 23, + 23, // (SA) removing warning messages from startup + 45, + 25, + 23, + 5, +}; +static float shaderAnimSTRatio[MAX_SHADER_ANIMS] = { + 1.405, + 1, + 1, + 1, + 1, + 1, +}; +static int numShaderAnims; +// done. + +#define PARTICLE_GRAVITY 40 +#define MAX_PARTICLES 1024 * 8 + +cparticle_t *active_particles, *free_particles; +cparticle_t particles[MAX_PARTICLES]; +int cl_numparticles = MAX_PARTICLES; + +qboolean initparticles = qfalse; +vec3_t vforward, vright, vup; +vec3_t rforward, rright, rup; + +float oldtime; + + +/* +============== +CG_ParticleLODCheck +============== +*/ +qboolean CG_ParticleLODCheck( void ) { + if ( cg_particleLOD.integer <= 1 ) { + return qtrue; + } + + + if ( !( rand() % ( cg_particleLOD.integer ) ) ) { // let particle lod thin out particles + return qtrue; + } + + return qfalse; +} + +/* +=============== +CL_ClearParticles +=============== +*/ +void CG_ClearParticles( void ) { + int i; + + memset( particles, 0, sizeof( particles ) ); + + free_particles = &particles[0]; + active_particles = NULL; + + for ( i = 0 ; i < cl_numparticles ; i++ ) + { + particles[i].next = &particles[i + 1]; + particles[i].type = 0; + } + particles[cl_numparticles - 1].next = NULL; + + oldtime = cg.time; + + // Ridah, init the shaderAnims + for ( i = 0; shaderAnimNames[i]; i++ ) { + int j; + + for ( j = 0; j < shaderAnimCounts[i]; j++ ) { + shaderAnims[i][j] = trap_R_RegisterShader( va( "%s%i", shaderAnimNames[i], j + 1 ) ); + } + } + numShaderAnims = i; + // done. + + initparticles = qtrue; +} + + +/* +===================== +CG_AddParticleToScene +===================== +*/ +void CG_AddParticleToScene( cparticle_t *p, vec3_t org, float alpha ) { + + vec3_t point; + polyVert_t verts[4]; + float width; + float height; + float time, time2; + float ratio; + float invratio; + vec3_t color; + polyVert_t TRIverts[3]; + vec3_t rright2, rup2; + + if ( p->type == P_WEATHER || p->type == P_WEATHER_TURBULENT || p->type == P_WEATHER_FLURRY + || p->type == P_BUBBLE || p->type == P_BUBBLE_TURBULENT ) { // create a front facing polygon + + if ( p->type != P_WEATHER_FLURRY ) { + if ( p->type == P_BUBBLE || p->type == P_BUBBLE_TURBULENT ) { + if ( org[2] > p->end ) { + p->time = cg.time; + VectorCopy( org, p->org ); // Ridah, fixes rare snow flakes that flicker on the ground + + p->org[2] = ( p->start + crandom() * 4 ); + + + if ( p->type == P_BUBBLE_TURBULENT ) { + p->vel[0] = crandom() * 4; + p->vel[1] = crandom() * 4; + } + + } + } else + { + if ( org[2] < p->end ) { + p->time = cg.time; + VectorCopy( org, p->org ); // Ridah, fixes rare snow flakes that flicker on the ground + + while ( p->org[2] < p->end ) + { + p->org[2] += ( p->start - p->end ); + } + + + if ( p->type == P_WEATHER_TURBULENT ) { + p->vel[0] = crandom() * 16; + p->vel[1] = crandom() * 16; + } + + } + } + + + // Rafael snow pvs check + if ( !p->link ) { + return; + } + + p->alpha = 1; + } + + // Ridah, had to do this or MAX_POLYS is being exceeded in village1.bsp + //----(SA) made the dist a cvar + + // dot product removal (gets you the dist^2, which you needed anyway, also dot lets you adjust lod when zooming) + if ( 1 ) { + vec3_t dir; + float dot, distSqrd; + + VectorSubtract( cg.refdef.vieworg, org, dir ); + distSqrd = dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]; + + dot = DotProduct( dir, cg.refdef.viewaxis[0] ); + + if ( distSqrd > ( cg_particleDist.value * cg_particleDist.value ) ) { + return; + } + } + + + + // done. + + if ( p->type == P_BUBBLE || p->type == P_BUBBLE_TURBULENT ) { + VectorMA( org, -p->height, vup, point ); + VectorMA( point, -p->width, vright, point ); + VectorCopy( point, verts[0].xyz ); + verts[0].st[0] = 0; + verts[0].st[1] = 0; + verts[0].modulate[0] = 255; + verts[0].modulate[1] = 255; + verts[0].modulate[2] = 255; + verts[0].modulate[3] = 255 * p->alpha; + + VectorMA( org, -p->height, vup, point ); + VectorMA( point, p->width, vright, point ); + VectorCopy( point, verts[1].xyz ); + verts[1].st[0] = 0; + verts[1].st[1] = 1; + verts[1].modulate[0] = 255; + verts[1].modulate[1] = 255; + verts[1].modulate[2] = 255; + verts[1].modulate[3] = 255 * p->alpha; + + VectorMA( org, p->height, vup, point ); + VectorMA( point, p->width, vright, point ); + VectorCopy( point, verts[2].xyz ); + verts[2].st[0] = 1; + verts[2].st[1] = 1; + verts[2].modulate[0] = 255; + verts[2].modulate[1] = 255; + verts[2].modulate[2] = 255; + verts[2].modulate[3] = 255 * p->alpha; + + VectorMA( org, p->height, vup, point ); + VectorMA( point, -p->width, vright, point ); + VectorCopy( point, verts[3].xyz ); + verts[3].st[0] = 1; + verts[3].st[1] = 0; + verts[3].modulate[0] = 255; + verts[3].modulate[1] = 255; + verts[3].modulate[2] = 255; + verts[3].modulate[3] = 255 * p->alpha; + } else + { + VectorMA( org, -p->height, vup, point ); + VectorMA( point, -p->width, vright, point ); + VectorCopy( point, TRIverts[0].xyz ); + TRIverts[0].st[0] = 1; + TRIverts[0].st[1] = 0; + TRIverts[0].modulate[0] = 255; + TRIverts[0].modulate[1] = 255; + TRIverts[0].modulate[2] = 255; + TRIverts[0].modulate[3] = 255 * p->alpha; + + VectorMA( org, p->height, vup, point ); + VectorMA( point, -p->width, vright, point ); + VectorCopy( point, TRIverts[1].xyz ); + TRIverts[1].st[0] = 0; + TRIverts[1].st[1] = 0; + TRIverts[1].modulate[0] = 255; + TRIverts[1].modulate[1] = 255; + TRIverts[1].modulate[2] = 255; + TRIverts[1].modulate[3] = 255 * p->alpha; + + VectorMA( org, p->height, vup, point ); + VectorMA( point, p->width, vright, point ); + VectorCopy( point, TRIverts[2].xyz ); + TRIverts[2].st[0] = 0; + TRIverts[2].st[1] = 1; + TRIverts[2].modulate[0] = 255; + TRIverts[2].modulate[1] = 255; + TRIverts[2].modulate[2] = 255; + TRIverts[2].modulate[3] = 255 * p->alpha; + } + + } else if ( p->type == P_SPRITE ) { + vec3_t rr, ru; + vec3_t rotate_ang; + + VectorSet( color, 1.0, 1.0, 1.0 ); + time = cg.time - p->time; + time2 = p->endtime - p->time; + ratio = time / time2; + + width = p->width + ( ratio * ( p->endwidth - p->width ) ); + height = p->height + ( ratio * ( p->endheight - p->height ) ); + + if ( p->roll ) { + vectoangles( cg.refdef.viewaxis[0], rotate_ang ); + rotate_ang[ROLL] += p->roll; + AngleVectors( rotate_ang, NULL, rr, ru ); + } + + if ( p->roll ) { + VectorMA( org, -height, ru, point ); + VectorMA( point, -width, rr, point ); + } else { + VectorMA( org, -height, vup, point ); + VectorMA( point, -width, vright, point ); + } + VectorCopy( point, verts[0].xyz ); + verts[0].st[0] = 0; + verts[0].st[1] = 0; + verts[0].modulate[0] = 255; + verts[0].modulate[1] = 255; + verts[0].modulate[2] = 255; + verts[0].modulate[3] = 255; + + if ( p->roll ) { + VectorMA( point, 2 * height, ru, point ); + } else { + VectorMA( point, 2 * height, vup, point ); + } + VectorCopy( point, verts[1].xyz ); + verts[1].st[0] = 0; + verts[1].st[1] = 1; + verts[1].modulate[0] = 255; + verts[1].modulate[1] = 255; + verts[1].modulate[2] = 255; + verts[1].modulate[3] = 255; + + if ( p->roll ) { + VectorMA( point, 2 * width, rr, point ); + } else { + VectorMA( point, 2 * width, vright, point ); + } + VectorCopy( point, verts[2].xyz ); + verts[2].st[0] = 1; + verts[2].st[1] = 1; + verts[2].modulate[0] = 255; + verts[2].modulate[1] = 255; + verts[2].modulate[2] = 255; + verts[2].modulate[3] = 255; + + if ( p->roll ) { + VectorMA( point, -2 * height, ru, point ); + } else { + VectorMA( point, -2 * height, vup, point ); + } + VectorCopy( point, verts[3].xyz ); + verts[3].st[0] = 1; + verts[3].st[1] = 0; + verts[3].modulate[0] = 255; + verts[3].modulate[1] = 255; + verts[3].modulate[2] = 255; + verts[3].modulate[3] = 255; + } else if ( p->type == P_SMOKE || p->type == P_SMOKE_IMPACT ) { // create a front rotating facing polygon + +// if ( p->type == P_SMOKE_IMPACT && Distance( cg.snap->ps.origin, org ) > 1024) { +// return; +// } + + // dot product removal (gets you the dist^2, which you needed anyway, also dot lets you adjust lod when zooming) + if ( 1 ) { + vec3_t dir; + float dot, distSqrd, fardist; + + VectorSubtract( org, cg.refdef.vieworg, dir ); + distSqrd = dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]; + + VectorNormalize( dir ); + dot = DotProduct( dir, cg.refdef.viewaxis[0] ); + + if ( dot < 0 ) { // behind camera + return; + } + + fardist = ( cg_particleDist.value * cg_particleDist.value ); + // push distance out when zooming + if ( cg.predictedPlayerState.eFlags & EF_ZOOMING ) { + fardist *= 2; + } + +// if(fabs(dot) < 0.8) +// return; + + if ( distSqrd > fardist ) { + return; + } + } + + + if ( p->color == MUSTARD ) { + VectorSet( color, 0.42, 0.33, 0.19 ); + } else if ( p->color == BLOODRED ) { + VectorSet( color, 0.22, 0, 0 ); + } else if ( p->color == ZOMBIE ) { + VectorSet( color, 0.4, 0.28, 0.23 ); + } else if ( p->color == GREY75 ) { + float len; + float greyit; + float val; + len = Distance( cg.snap->ps.origin, org ); + if ( !len ) { + len = 1; + } + + val = 4096 / len; + greyit = 0.25 * val; + if ( greyit > 0.5 ) { + greyit = 0.5; + } + + VectorSet( color, greyit, greyit, greyit ); + } else { + VectorSet( color, 1.0, 1.0, 1.0 ); + } + + time = cg.time - p->time; + time2 = p->endtime - p->time; + ratio = time / time2; + + if ( cg.time > p->startfade ) { + invratio = 1 - ( ( cg.time - p->startfade ) / ( p->endtime - p->startfade ) ); + + if ( p->color == EMISIVEFADE ) { + float fval; + fval = ( invratio * invratio ); + if ( fval < 0 ) { + fval = 0; + } + VectorSet( color, fval, fval, fval ); + } + invratio *= p->alpha; + } else { + invratio = 1 * p->alpha; + } + + if ( cgs.glconfig.hardwareType == GLHW_RAGEPRO ) { + invratio = 1; + } + + if ( invratio > 1 ) { + invratio = 1; + } + + width = p->width + ( ratio * ( p->endwidth - p->width ) ); + height = p->height + ( ratio * ( p->endheight - p->height ) ); + +// if (p->type != P_SMOKE_IMPACT) + { + vec3_t temp; + + vectoangles( rforward, temp ); + p->accumroll += p->roll; + temp[ROLL] += p->accumroll * 0.1; +// temp[ROLL] += p->roll * 0.1; + AngleVectors( temp, NULL, rright2, rup2 ); + } +// else +// { +// VectorCopy (rright, rright2); +// VectorCopy (rup, rup2); +// } + + if ( p->rotate ) { + VectorMA( org, -height, rup2, point ); + VectorMA( point, -width, rright2, point ); + } else + { + VectorMA( org, -p->height, vup, point ); + VectorMA( point, -p->width, vright, point ); + } + VectorCopy( point, verts[0].xyz ); + verts[0].st[0] = 0; + verts[0].st[1] = 0; + verts[0].modulate[0] = 255 * color[0]; + verts[0].modulate[1] = 255 * color[1]; + verts[0].modulate[2] = 255 * color[2]; + verts[0].modulate[3] = 255 * invratio; + + if ( p->rotate ) { + VectorMA( org, -height, rup2, point ); + VectorMA( point, width, rright2, point ); + } else + { + VectorMA( org, -p->height, vup, point ); + VectorMA( point, p->width, vright, point ); + } + VectorCopy( point, verts[1].xyz ); + verts[1].st[0] = 0; + verts[1].st[1] = 1; + verts[1].modulate[0] = 255 * color[0]; + verts[1].modulate[1] = 255 * color[1]; + verts[1].modulate[2] = 255 * color[2]; + verts[1].modulate[3] = 255 * invratio; + + if ( p->rotate ) { + VectorMA( org, height, rup2, point ); + VectorMA( point, width, rright2, point ); + } else + { + VectorMA( org, p->height, vup, point ); + VectorMA( point, p->width, vright, point ); + } + VectorCopy( point, verts[2].xyz ); + verts[2].st[0] = 1; + verts[2].st[1] = 1; + verts[2].modulate[0] = 255 * color[0]; + verts[2].modulate[1] = 255 * color[1]; + verts[2].modulate[2] = 255 * color[2]; + verts[2].modulate[3] = 255 * invratio; + + if ( p->rotate ) { + VectorMA( org, height, rup2, point ); + VectorMA( point, -width, rright2, point ); + } else + { + VectorMA( org, p->height, vup, point ); + VectorMA( point, -p->width, vright, point ); + } + VectorCopy( point, verts[3].xyz ); + verts[3].st[0] = 1; + verts[3].st[1] = 0; + verts[3].modulate[0] = 255 * color[0]; + verts[3].modulate[1] = 255 * color[1]; + verts[3].modulate[2] = 255 * color[2]; + verts[3].modulate[3] = 255 * invratio; + + } else if ( p->type == P_BAT ) { + p->pshader = cgs.media.bats[( cg.time / 50 + (int)( p - particles ) ) % 10]; + + VectorMA( org, -p->height, vup, point ); + VectorMA( point, -p->width, vright, point ); + VectorCopy( point, verts[0].xyz ); + verts[0].st[0] = 0; + verts[0].st[1] = 0; + verts[0].modulate[0] = 255; + verts[0].modulate[1] = 255; + verts[0].modulate[2] = 255; + verts[0].modulate[3] = 255; + + VectorMA( org, -p->height, vup, point ); + VectorMA( point, p->width, vright, point ); + VectorCopy( point, verts[1].xyz ); + verts[1].st[0] = 0; + verts[1].st[1] = 1; + verts[1].modulate[0] = 255; + verts[1].modulate[1] = 255; + verts[1].modulate[2] = 255; + verts[1].modulate[3] = 255; + + VectorMA( org, p->height, vup, point ); + VectorMA( point, p->width, vright, point ); + VectorCopy( point, verts[2].xyz ); + verts[2].st[0] = 1; + verts[2].st[1] = 1; + verts[2].modulate[0] = 255; + verts[2].modulate[1] = 255; + verts[2].modulate[2] = 255; + verts[2].modulate[3] = 255; + + VectorMA( org, p->height, vup, point ); + VectorMA( point, -p->width, vright, point ); + VectorCopy( point, verts[3].xyz ); + verts[3].st[0] = 1; + verts[3].st[1] = 0; + verts[3].modulate[0] = 255; + verts[3].modulate[1] = 255; + verts[3].modulate[2] = 255; + verts[3].modulate[3] = 255; + + } else if ( p->type == P_BLEED ) { + vec3_t rr, ru; + vec3_t rotate_ang; + float alpha; + + alpha = p->alpha; + + if ( cgs.glconfig.hardwareType == GLHW_RAGEPRO ) { + alpha = 1; + } + + if ( p->roll ) { + vectoangles( cg.refdef.viewaxis[0], rotate_ang ); + rotate_ang[ROLL] += p->roll; + AngleVectors( rotate_ang, NULL, rr, ru ); + } else + { + VectorCopy( vup, ru ); + VectorCopy( vright, rr ); + } + + VectorMA( org, -p->height, ru, point ); + VectorMA( point, -p->width, rr, point ); + VectorCopy( point, verts[0].xyz ); + verts[0].st[0] = 0; + verts[0].st[1] = 0; + verts[0].modulate[0] = 111; + verts[0].modulate[1] = 19; + verts[0].modulate[2] = 9; + verts[0].modulate[3] = 255 * alpha; + + VectorMA( org, -p->height, ru, point ); + VectorMA( point, p->width, rr, point ); + VectorCopy( point, verts[1].xyz ); + verts[1].st[0] = 0; + verts[1].st[1] = 1; + verts[1].modulate[0] = 111; + verts[1].modulate[1] = 19; + verts[1].modulate[2] = 9; + verts[1].modulate[3] = 255 * alpha; + + VectorMA( org, p->height, ru, point ); + VectorMA( point, p->width, rr, point ); + VectorCopy( point, verts[2].xyz ); + verts[2].st[0] = 1; + verts[2].st[1] = 1; + verts[2].modulate[0] = 111; + verts[2].modulate[1] = 19; + verts[2].modulate[2] = 9; + verts[2].modulate[3] = 255 * alpha; + + VectorMA( org, p->height, ru, point ); + VectorMA( point, -p->width, rr, point ); + VectorCopy( point, verts[3].xyz ); + verts[3].st[0] = 1; + verts[3].st[1] = 0; + verts[3].modulate[0] = 111; + verts[3].modulate[1] = 19; + verts[3].modulate[2] = 9; + verts[3].modulate[3] = 255 * alpha; + + } else if ( p->type == P_FLAT_SCALEUP ) { + float width, height; + float sinR, cosR; + + if ( p->color == BLOODRED ) { + VectorSet( color, 1, 1, 1 ); + } else { + VectorSet( color, 0.5, 0.5, 0.5 ); + } + + time = cg.time - p->time; + time2 = p->endtime - p->time; + ratio = time / time2; + + width = p->width + ( ratio * ( p->endwidth - p->width ) ); + height = p->height + ( ratio * ( p->endheight - p->height ) ); + + if ( width > p->endwidth ) { + width = p->endwidth; + } + + if ( height > p->endheight ) { + height = p->endheight; + } + + sinR = height * sin( DEG2RAD( p->roll ) ) * sqrt( 2 ); + cosR = width * cos( DEG2RAD( p->roll ) ) * sqrt( 2 ); + + VectorCopy( org, verts[0].xyz ); + verts[0].xyz[0] -= sinR; + verts[0].xyz[1] -= cosR; + verts[0].st[0] = 0; + verts[0].st[1] = 0; + verts[0].modulate[0] = 255 * color[0]; + verts[0].modulate[1] = 255 * color[1]; + verts[0].modulate[2] = 255 * color[2]; + verts[0].modulate[3] = 255; + + VectorCopy( org, verts[1].xyz ); + verts[1].xyz[0] -= cosR; + verts[1].xyz[1] += sinR; + verts[1].st[0] = 0; + verts[1].st[1] = 1; + verts[1].modulate[0] = 255 * color[0]; + verts[1].modulate[1] = 255 * color[1]; + verts[1].modulate[2] = 255 * color[2]; + verts[1].modulate[3] = 255; + + VectorCopy( org, verts[2].xyz ); + verts[2].xyz[0] += sinR; + verts[2].xyz[1] += cosR; + verts[2].st[0] = 1; + verts[2].st[1] = 1; + verts[2].modulate[0] = 255 * color[0]; + verts[2].modulate[1] = 255 * color[1]; + verts[2].modulate[2] = 255 * color[2]; + verts[2].modulate[3] = 255; + + VectorCopy( org, verts[3].xyz ); + verts[3].xyz[0] += cosR; + verts[3].xyz[1] -= sinR; + verts[3].st[0] = 1; + verts[3].st[1] = 0; + verts[3].modulate[0] = 255 * color[0]; + verts[3].modulate[1] = 255 * color[1]; + verts[3].modulate[2] = 255 * color[2]; + verts[3].modulate[3] = 255; + } else if ( p->type == P_FLAT ) { + + VectorCopy( org, verts[0].xyz ); + verts[0].xyz[0] -= p->height; + verts[0].xyz[1] -= p->width; + verts[0].st[0] = 0; + verts[0].st[1] = 0; + verts[0].modulate[0] = 255; + verts[0].modulate[1] = 255; + verts[0].modulate[2] = 255; + verts[0].modulate[3] = 255; + + VectorCopy( org, verts[1].xyz ); + verts[1].xyz[0] -= p->height; + verts[1].xyz[1] += p->width; + verts[1].st[0] = 0; + verts[1].st[1] = 1; + verts[1].modulate[0] = 255; + verts[1].modulate[1] = 255; + verts[1].modulate[2] = 255; + verts[1].modulate[3] = 255; + + VectorCopy( org, verts[2].xyz ); + verts[2].xyz[0] += p->height; + verts[2].xyz[1] += p->width; + verts[2].st[0] = 1; + verts[2].st[1] = 1; + verts[2].modulate[0] = 255; + verts[2].modulate[1] = 255; + verts[2].modulate[2] = 255; + verts[2].modulate[3] = 255; + + VectorCopy( org, verts[3].xyz ); + verts[3].xyz[0] += p->height; + verts[3].xyz[1] -= p->width; + verts[3].st[0] = 1; + verts[3].st[1] = 0; + verts[3].modulate[0] = 255; + verts[3].modulate[1] = 255; + verts[3].modulate[2] = 255; + verts[3].modulate[3] = 255; + + } + // Ridah + else if ( p->type == P_ANIM ) { + vec3_t rr, ru; + vec3_t rotate_ang; + int i, j; + + time = cg.time - p->time; + time2 = p->endtime - p->time; + ratio = time / time2; + if ( ratio >= 1.0 ) { + ratio = 0.9999; + } + + width = p->width + ( ratio * ( p->endwidth - p->width ) ); + height = p->height + ( ratio * ( p->endheight - p->height ) ); + + // if we are "inside" this sprite, don't draw + if ( Distance( cg.snap->ps.origin, org ) < width / 1.5 ) { + return; + } + + i = p->shaderAnim; + j = (int)floor( ratio * shaderAnimCounts[p->shaderAnim] ); + p->pshader = shaderAnims[i][j]; + + if ( p->roll ) { + vectoangles( cg.refdef.viewaxis[0], rotate_ang ); + rotate_ang[ROLL] += p->roll; + AngleVectors( rotate_ang, NULL, rr, ru ); + } + + if ( p->roll ) { + VectorMA( org, -height, ru, point ); + VectorMA( point, -width, rr, point ); + } else { + VectorMA( org, -height, vup, point ); + VectorMA( point, -width, vright, point ); + } + VectorCopy( point, verts[0].xyz ); + verts[0].st[0] = 0; + verts[0].st[1] = 0; + verts[0].modulate[0] = 255; + verts[0].modulate[1] = 255; + verts[0].modulate[2] = 255; + verts[0].modulate[3] = 255; + + if ( p->roll ) { + VectorMA( point, 2 * height, ru, point ); + } else { + VectorMA( point, 2 * height, vup, point ); + } + VectorCopy( point, verts[1].xyz ); + verts[1].st[0] = 0; + verts[1].st[1] = 1; + verts[1].modulate[0] = 255; + verts[1].modulate[1] = 255; + verts[1].modulate[2] = 255; + verts[1].modulate[3] = 255; + + if ( p->roll ) { + VectorMA( point, 2 * width, rr, point ); + } else { + VectorMA( point, 2 * width, vright, point ); + } + VectorCopy( point, verts[2].xyz ); + verts[2].st[0] = 1; + verts[2].st[1] = 1; + verts[2].modulate[0] = 255; + verts[2].modulate[1] = 255; + verts[2].modulate[2] = 255; + verts[2].modulate[3] = 255; + + if ( p->roll ) { + VectorMA( point, -2 * height, ru, point ); + } else { + VectorMA( point, -2 * height, vup, point ); + } + VectorCopy( point, verts[3].xyz ); + verts[3].st[0] = 1; + verts[3].st[1] = 0; + verts[3].modulate[0] = 255; + verts[3].modulate[1] = 255; + verts[3].modulate[2] = 255; + verts[3].modulate[3] = 255; + } + // done. + + if ( !cg_wolfparticles.integer ) { + return; + } + + if ( !p->pshader ) { +// (SA) temp commented out for DM again. FIXME: TODO: this needs to be addressed +// CG_Printf ("CG_AddParticleToScene type %d p->pshader == ZERO\n", p->type); + return; + } + + if ( p->type == P_WEATHER || p->type == P_WEATHER_TURBULENT || p->type == P_WEATHER_FLURRY ) { + trap_R_AddPolyToScene( p->pshader, 3, TRIverts ); + } else { + trap_R_AddPolyToScene( p->pshader, 4, verts ); + } + +} + +// Ridah, made this static so it doesn't interfere with other files +static float roll = 0.0; + +/* +=============== +CG_AddParticles +=============== +*/ +void CG_AddParticles( void ) { + cparticle_t *p, *next; + float alpha; + float time, time2; + vec3_t org; + int color; + cparticle_t *active, *tail; + int type; + vec3_t rotate_ang; + + if ( !initparticles ) { + CG_ClearParticles(); + } + + VectorCopy( cg.refdef.viewaxis[0], vforward ); + VectorCopy( cg.refdef.viewaxis[1], vright ); + VectorCopy( cg.refdef.viewaxis[2], vup ); + + vectoangles( cg.refdef.viewaxis[0], rotate_ang ); + roll += ( ( cg.time - oldtime ) * 0.1 ) ; + rotate_ang[ROLL] += ( roll * 0.9 ); + AngleVectors( rotate_ang, rforward, rright, rup ); + + oldtime = cg.time; + + active = NULL; + tail = NULL; + + for ( p = active_particles ; p ; p = next ) + { + + next = p->next; + + time = ( cg.time - p->time ) * 0.001; + + alpha = p->alpha + time * p->alphavel; + if ( alpha <= 0 ) { // faded out + p->next = free_particles; + free_particles = p; + p->type = 0; + p->color = 0; + p->alpha = 0; + continue; + } + + if ( p->type == P_SMOKE || p->type == P_ANIM || p->type == P_BLEED || p->type == P_SMOKE_IMPACT ) { + if ( cg.time > p->endtime ) { + p->next = free_particles; + free_particles = p; + p->type = 0; + p->color = 0; + p->alpha = 0; + + continue; + } + + } + + if ( p->type == P_WEATHER_FLURRY ) { + if ( cg.time > p->endtime ) { + p->next = free_particles; + free_particles = p; + p->type = 0; + p->color = 0; + p->alpha = 0; + + continue; + } + } + + + if ( p->type == P_FLAT_SCALEUP_FADE ) { + if ( cg.time > p->endtime ) { + p->next = free_particles; + free_particles = p; + p->type = 0; + p->color = 0; + p->alpha = 0; + continue; + } + + } + + if ( ( p->type == P_BAT || p->type == P_SPRITE ) && p->endtime < 0 ) { + // temporary sprite + CG_AddParticleToScene( p, p->org, alpha ); + p->next = free_particles; + free_particles = p; + p->type = 0; + p->color = 0; + p->alpha = 0; + continue; + } + + p->next = NULL; + if ( !tail ) { + active = tail = p; + } else + { + tail->next = p; + tail = p; + } + + if ( alpha > 1.0 ) { + alpha = 1; + } + + color = p->color; + + time2 = time * time; + + org[0] = p->org[0] + p->vel[0] * time + p->accel[0] * time2; + org[1] = p->org[1] + p->vel[1] * time + p->accel[1] * time2; + org[2] = p->org[2] + p->vel[2] * time + p->accel[2] * time2; + + type = p->type; + + CG_AddParticleToScene( p, org, alpha ); + } + + active_particles = active; +} + +/* +====================== +CG_AddParticles +====================== +*/ +void CG_ParticleSnowFlurry( qhandle_t pshader, centity_t *cent ) { + cparticle_t *p; + qboolean turb = qtrue; + + if ( !pshader ) { + CG_Printf( "CG_ParticleSnowFlurry pshader == ZERO!\n" ); + } + + if ( !free_particles ) { + return; + } + + if ( !CG_ParticleLODCheck() ) { + return; + } + + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; + p->time = cg.time; + p->color = 0; + p->alpha = 0.90; + p->alphavel = 0; + + p->start = cent->currentState.origin2[0]; + p->end = cent->currentState.origin2[1]; + + p->endtime = cg.time + cent->currentState.time; + p->startfade = cg.time + cent->currentState.time2; + + p->pshader = pshader; + + if ( rand() % 100 > 90 ) { + p->height = 32; + p->width = 32; + p->alpha = 0.10; + } else + { + p->height = 1; + p->width = 1; + } + + p->vel[2] = -20; + + p->type = P_WEATHER_FLURRY; + + if ( turb ) { + p->vel[2] = -10; + } + + VectorCopy( cent->currentState.origin, p->org ); + + p->org[0] = p->org[0]; + p->org[1] = p->org[1]; + p->org[2] = p->org[2]; + + p->vel[0] = p->vel[1] = 0; + + p->accel[0] = p->accel[1] = p->accel[2] = 0; + + p->vel[0] += cent->currentState.angles[0] * 32 + ( crandom() * 16 ); + p->vel[1] += cent->currentState.angles[1] * 32 + ( crandom() * 16 ); + p->vel[2] += cent->currentState.angles[2]; + + if ( turb ) { + p->accel[0] = crandom() * 16; + p->accel[1] = crandom() * 16; + } + +} + +void CG_ParticleSnow( qhandle_t pshader, vec3_t origin, vec3_t origin2, int turb, float range, int snum ) { + cparticle_t *p; + + if ( !pshader ) { + CG_Printf( "CG_ParticleSnow pshader == ZERO!\n" ); + } + + if ( !free_particles ) { + return; + } + + if ( !CG_ParticleLODCheck() ) { + return; + } + + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; + p->time = cg.time; + p->color = 0; + p->alpha = 0.40; + p->alphavel = 0; + p->start = origin[2]; + p->end = origin2[2]; + p->pshader = pshader; + p->height = 1; + p->width = 1; + + p->vel[2] = -50; + + if ( turb ) { + p->type = P_WEATHER_TURBULENT; + p->vel[2] = -50 * 1.3; + } else + { + p->type = P_WEATHER; + } + + VectorCopy( origin, p->org ); + + p->org[0] = p->org[0] + ( crandom() * range ); + p->org[1] = p->org[1] + ( crandom() * range ); + p->org[2] = p->org[2] + ( crandom() * ( p->start - p->end ) ); + + p->vel[0] = p->vel[1] = 0; + + p->accel[0] = p->accel[1] = p->accel[2] = 0; + + if ( turb ) { + p->vel[0] = crandom() * 16; + p->vel[1] = crandom() * 16; + } + + // Rafael snow pvs check + p->snum = snum; + p->link = qtrue; + +} + +void CG_ParticleBubble( qhandle_t pshader, vec3_t origin, vec3_t origin2, int turb, float range, int snum ) { + cparticle_t *p; + float randsize; + + if ( !pshader ) { + CG_Printf( "CG_ParticleSnow pshader == ZERO!\n" ); + } + + if ( !free_particles ) { + return; + } + + if ( !CG_ParticleLODCheck() ) { + return; + } + + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; + p->time = cg.time; + p->color = 0; + p->alpha = 0.40; + p->alphavel = 0; + p->start = origin[2]; + p->end = origin2[2]; + p->pshader = pshader; + + randsize = 1 + ( crandom() * 0.5 ); + + p->height = randsize; + p->width = randsize; + + p->vel[2] = 50 + ( crandom() * 10 ); + + if ( turb ) { + p->type = P_BUBBLE_TURBULENT; + p->vel[2] = 50 * 1.3; + } else + { + p->type = P_BUBBLE; + } + + VectorCopy( origin, p->org ); + + p->org[0] = p->org[0] + ( crandom() * range ); + p->org[1] = p->org[1] + ( crandom() * range ); + p->org[2] = p->org[2] + ( crandom() * ( p->start - p->end ) ); + + p->vel[0] = p->vel[1] = 0; + + p->accel[0] = p->accel[1] = p->accel[2] = 0; + + if ( turb ) { + p->vel[0] = crandom() * 4; + p->vel[1] = crandom() * 4; + } + + // Rafael snow pvs check + p->snum = snum; + p->link = qtrue; + +} + +void CG_ParticleSmoke( qhandle_t pshader, centity_t *cent ) { + + // using cent->density = enttime + // cent->frame = startfade + cparticle_t *p; + vec3_t dir; + + if ( !pshader ) { + CG_Printf( "CG_ParticleSmoke == ZERO!\n" ); + } + + if ( !free_particles ) { + return; + } + + if ( !CG_ParticleLODCheck() ) { + return; + } + + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; + p->time = cg.time; + + p->endtime = cg.time + cent->currentState.time; + p->startfade = cg.time + cent->currentState.time2; + + p->color = 0; + p->alpha = 1.0; + p->alphavel = 0; + p->start = cent->currentState.origin[2]; + p->end = cent->currentState.origin2[2]; + p->pshader = pshader; + if ( cent->currentState.density == 1 ) { + p->rotate = qfalse; + p->height = 8; + p->width = 8; + p->endheight = 32; + p->endwidth = 32; + } else if ( cent->currentState.density == 2 ) { + p->rotate = qtrue; + p->height = 4; + p->width = 4; + p->endheight = 8; + p->endwidth = 8; + } else if ( cent->currentState.density == 3 ) { + p->rotate = qfalse; + { + float scale; + + scale = 16 + ( crandom() * 8 ); + p->height = 24 + scale; + p->width = 24 + scale; + p->endheight = 64 + scale; + p->endwidth = 64 + scale; + } + } else if ( cent->currentState.density == 4 ) { // white smoke + p->rotate = qtrue; + p->height = cent->currentState.angles2[0]; + p->width = cent->currentState.angles2[0]; + p->endheight = cent->currentState.angles2[1]; + p->endwidth = cent->currentState.angles2[1]; + p->color = GREY75; + } else if ( cent->currentState.density == 5 ) { // mustard gas + p->rotate = qtrue; + p->height = cent->currentState.angles2[0]; + p->width = cent->currentState.angles2[0]; + p->endheight = cent->currentState.angles2[1]; + p->endwidth = cent->currentState.angles2[1]; + p->color = MUSTARD; + p->alpha = 0.75; + } else // black smoke + { + p->rotate = qtrue; + p->height = cent->currentState.angles2[0]; + p->width = cent->currentState.angles2[0]; + p->endheight = cent->currentState.angles2[1]; + p->endwidth = cent->currentState.angles2[1]; + + { + int rval; + rval = rand() % 6; + if ( rval == 1 ) { + p->pshader = cgs.media.smokePuffShaderb1; + } else if ( rval == 2 ) { + p->pshader = cgs.media.smokePuffShaderb2; + } else if ( rval == 3 ) { + p->pshader = cgs.media.smokePuffShaderb3; + } else if ( rval == 4 ) { + p->pshader = cgs.media.smokePuffShaderb4; + } else { + p->pshader = cgs.media.smokePuffShaderb5; + } + } + } + + + p->type = P_SMOKE; + + VectorCopy( cent->currentState.origin, p->org ); + + p->vel[0] = p->vel[1] = 0; + p->accel[0] = p->accel[1] = p->accel[2] = 0; + + if ( cent->currentState.density == 1 ) { + p->vel[2] = 5; + } else if ( cent->currentState.density == 2 ) { + p->vel[2] = 5; + } else if ( cent->currentState.density == 3 ) { // cannon + VectorCopy( cent->currentState.origin2, dir ); + p->vel[0] = dir[0] * 128 + ( crandom() * 64 ); + p->vel[1] = dir[1] * 128 + ( crandom() * 64 ); + p->vel[2] = 15 + ( crandom() * 16 ); + } else if ( cent->currentState.density == 5 ) { // gas or cover smoke + VectorCopy( cent->currentState.origin2, dir ); + p->vel[0] = dir[0] * 32 + ( crandom() * 16 ); + p->vel[1] = dir[1] * 32 + ( crandom() * 16 ); + p->vel[2] = 4 + ( crandom() * 2 ); + } else // smoke + { + VectorCopy( cent->currentState.origin2, dir ); + p->vel[0] = dir[0] + ( crandom() * p->height ); + p->vel[1] = dir[1] + ( crandom() * p->height ); + p->vel[2] = cent->currentState.angles2[2]; + } + + if ( cent->currentState.frame == 1 ) { // reverse gravity + p->vel[2] *= -1; + } + +// p->roll = 8 + (crandom() * 4); + p->roll = rand() % ( 2 * 8 ); + p->roll -= 8; +} + + +void CG_ParticleBulletDebris( vec3_t org, vec3_t vel, int duration ) { + + cparticle_t *p; + + if ( !free_particles ) { + return; + } + + if ( !CG_ParticleLODCheck() ) { + return; + } + + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; + p->time = cg.time; + + p->endtime = cg.time + duration; + p->startfade = cg.time + duration / 2; + + p->color = EMISIVEFADE; + p->alpha = 1.0; + p->alphavel = 0; + + p->height = 0.5; + p->width = 0.5; + p->endheight = 0.5; + p->endwidth = 0.5; + + p->pshader = cgs.media.tracerShader; + + p->type = P_SMOKE; + + VectorCopy( org, p->org ); + + p->vel[0] = vel[0]; + p->vel[1] = vel[1]; + p->vel[2] = vel[2]; + p->accel[0] = p->accel[1] = p->accel[2] = 0; + + p->accel[2] = -60; + p->vel[2] += -20; + +} + +// DHM - Nerve :: bullets hitting dirt + +void CG_ParticleDirtBulletDebris( vec3_t org, vec3_t vel, int duration ) { + int r = rand() % 3; + cparticle_t *p; + + if ( !free_particles ) { + return; + } + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; + p->time = cg.time; + + p->endtime = cg.time + duration; + p->startfade = cg.time + duration / 2; + + p->color = EMISIVEFADE; + p->alpha = 1.0; + p->alphavel = 0; + + p->height = 1.2; + p->width = 1.2; + p->endheight = 4.5; + p->endwidth = 4.5; + + if ( r == 0 ) { + p->pshader = cgs.media.dirtParticle1Shader; + } else if ( r == 1 ) { + p->pshader = cgs.media.dirtParticle2Shader; + } else { + p->pshader = cgs.media.dirtParticle3Shader; + } + + p->type = P_SMOKE; + + VectorCopy( org, p->org ); + + p->vel[0] = vel[0]; + p->vel[1] = vel[1]; + p->vel[2] = vel[2]; + p->accel[0] = p->accel[1] = p->accel[2] = 0; + + p->accel[2] = -330; + p->vel[2] += -20; +} + +// NERVE - SMF :: the core of the dirt explosion +void CG_ParticleDirtBulletDebris_Core( vec3_t org, vec3_t vel, int duration, + float width, float height, float alpha, char *shadername ) { // JPW NERVE +// int r = rand(); // TTimo: unused + cparticle_t *p; + + if ( !free_particles ) { + return; + } + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; + p->time = cg.time; + + p->endtime = cg.time + duration; + p->startfade = cg.time + duration / 2; + + p->color = EMISIVEFADE; + p->alpha = alpha; + p->alphavel = 0; + + p->height = width; // JPW NERVE was 512/5.f; + p->width = height; // JPW NERVE was 128/5.f; + p->endheight = p->height; + p->endwidth = p->width; + + p->rotate = 0; + + p->pshader = trap_R_RegisterShader( shadername ); // JPW NERVE was "dirt_splash" + + p->type = P_SMOKE; + + VectorCopy( org, p->org ); + VectorCopy( vel, p->vel ); + +// p->vel[0] = vel[0]; +// p->vel[1] = vel[1]; +// p->vel[2] = vel[2]; + p->accel[0] = p->accel[1] = p->accel[2] = 0; + + p->accel[2] = -330; +// p->vel[2] += -20; +} + +/* +====================== +CG_ParticleExplosion +====================== +*/ + +void CG_ParticleExplosion( char *animStr, vec3_t origin, vec3_t vel, int duration, int sizeStart, int sizeEnd ) { + cparticle_t *p; + int anim; + + if ( animStr < (char *)10 ) { + CG_Error( "CG_ParticleExplosion: animStr is probably an index rather than a string" ); + } + + if ( !CG_ParticleLODCheck() ) { + return; + } + + // find the animation string + for ( anim = 0; shaderAnimNames[anim]; anim++ ) { + if ( !Q_strcasecmp( animStr, shaderAnimNames[anim] ) ) { + break; + } + } + if ( !shaderAnimNames[anim] ) { + CG_Error( "CG_ParticleExplosion: unknown animation string: %s\n", animStr ); + return; + } + + if ( !free_particles ) { + return; + } + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; + p->time = cg.time; + p->alpha = 1.0; + p->alphavel = 0; + + if ( duration < 0 ) { + duration *= -1; + p->roll = 0; + } else { + p->roll = crandom() * 179; + } + + p->shaderAnim = anim; + + p->width = sizeStart; + p->height = sizeStart * shaderAnimSTRatio[anim]; // for sprites that are stretch in either direction + + p->endheight = sizeEnd; + p->endwidth = sizeEnd * shaderAnimSTRatio[anim]; + + p->endtime = cg.time + duration; + + p->type = P_ANIM; + + VectorCopy( origin, p->org ); + VectorCopy( vel, p->vel ); + VectorClear( p->accel ); + +} + +// Rafael Shrapnel +void CG_AddParticleShrapnel( localEntity_t *le ) { + return; +} +// done. + +int CG_NewParticleArea( int num ) { + // const char *str; + char *str; + char *token; + int type; + vec3_t origin, origin2; + int i; + float range = 0; + int turb; + int numparticles; + int snum; + + str = (char *) CG_ConfigString( num ); + if ( !str[0] ) { + return ( 0 ); + } + + // returns type 128 64 or 32 + token = COM_Parse( &str ); + type = atoi( token ); + + if ( type == 1 ) { + range = 128; + } else if ( type == 2 ) { + range = 64; + } else if ( type == 3 ) { + range = 32; + } else if ( type == 0 ) { + range = 256; + } else if ( type == 4 ) { + range = 8; + } else if ( type == 5 ) { + range = 16; + } else if ( type == 6 ) { + range = 32; + } else if ( type == 7 ) { + range = 64; + } + + + for ( i = 0; i < 3; i++ ) + { + token = COM_Parse( &str ); + origin[i] = atof( token ); + } + + for ( i = 0; i < 3; i++ ) + { + token = COM_Parse( &str ); + origin2[i] = atof( token ); + } + + token = COM_Parse( &str ); + numparticles = atoi( token ); + + token = COM_Parse( &str ); + turb = atoi( token ); + + token = COM_Parse( &str ); + snum = atoi( token ); + + for ( i = 0; i < numparticles; i++ ) + { + if ( type >= 4 ) { + CG_ParticleBubble( cgs.media.waterBubbleShader, origin, origin2, turb, range, snum ); + } else { + CG_ParticleSnow( cgs.media.snowShader, origin, origin2, turb, range, snum ); + } + } + + return ( 1 ); +} + +void CG_SnowLink( centity_t *cent, qboolean particleOn ) { + cparticle_t *p, *next; + int id; + + id = cent->currentState.frame; + + for ( p = active_particles ; p ; p = next ) + { + next = p->next; + + if ( p->type == P_WEATHER || p->type == P_WEATHER_TURBULENT ) { + if ( p->snum == id ) { + if ( particleOn ) { + p->link = qtrue; + } else { + p->link = qfalse; + } + } + } + + } +} + +void CG_ParticleBat( centity_t *cent ) { + cparticle_t *p; + vec3_t origin; + + if ( !free_particles ) { + return; + } + + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; + p->time = cg.time; + p->color = 0; + p->alpha = 1.0; + p->alphavel = 0; + p->height = 4; + p->width = 4; + + VectorCopy( cent->lerpOrigin, origin ); + VectorCopy( origin, p->org ); + VectorClear( p->vel ); + + p->accel[0] = p->accel[1] = p->accel[2] = 0; + + p->snum = cent->currentState.frame; + + p->type = P_BAT; + p->endtime = -1; // last one frame only +} + + +void CG_ParticleBats( qhandle_t pshader, centity_t *cent ) { + cparticle_t *p; + vec3_t origin; + + if ( !free_particles ) { + return; + } + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; + p->time = cg.time; + p->color = 0; + p->alpha = 0.40; + p->alphavel = 0; + p->pshader = pshader; + p->height = 4; + p->width = 4; + + VectorCopy( cent->currentState.origin, origin ); + VectorCopy( origin, p->org ); + + p->org[0] = p->org[0] + ( crandom() * 32 ); + p->org[1] = p->org[1] + ( crandom() * 32 ); + p->org[2] = p->org[2] + ( crandom() * 32 ); + + p->vel[0] = cent->currentState.angles[0] * cent->currentState.time; + p->vel[1] = cent->currentState.angles[1] * cent->currentState.time; + p->vel[2] = cent->currentState.angles[2] * cent->currentState.time; + + p->accel[0] = p->accel[1] = p->accel[2] = 0; + + p->snum = cent->currentState.frame; + + p->type = P_BAT; +} + +void CG_BatsUpdatePosition( centity_t *cent ) { + cparticle_t *p, *next; + int id; + float time; + + id = cent->currentState.frame; + + for ( p = active_particles ; p ; p = next ) + { + next = p->next; + + if ( p->type == P_BAT ) { + if ( p->snum == id ) { + time = ( cg.time - p->time ) * 0.001; + + p->org[0] = p->org[0] + p->vel[0] * time; + p->org[1] = p->org[1] + p->vel[1] * time; + p->org[2] = p->org[2] + p->vel[2] * time; + + p->time = cg.time; + + p->vel[0] = cent->currentState.angles[0] * cent->currentState.time; + p->vel[1] = cent->currentState.angles[1] * cent->currentState.time; + p->vel[2] = cent->currentState.angles[2] * cent->currentState.time; + + } + } + + } +} + + +void CG_ParticleImpactSmokePuffExtended( qhandle_t pshader, vec3_t origin, vec3_t dir, int radius, int lifetime, int vel, int acc, int maxroll, float alpha ) { + cparticle_t *p; + + if ( !pshader ) { + CG_Printf( "CG_ParticleImpactSmokePuff pshader == ZERO!\n" ); + } + + if ( !free_particles ) { + return; + } + + if ( !CG_ParticleLODCheck() ) { + return; + } + + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; + p->time = cg.time; + p->alpha = alpha; + p->alphavel = 0; + + // (SA) roll either direction + p->roll = rand() % ( 2 * maxroll ); +// p->roll = crandom()*(float)(maxroll*2); + p->roll -= maxroll; + + p->pshader = pshader; + + p->endtime = cg.time + lifetime; + p->startfade = cg.time + 100; + + p->width = rand() % 4 + radius; //----(SA) + p->height = rand() % 4 + radius; //----(SA) + + p->endheight = p->height * 2; + p->endwidth = p->width * 2; + + p->type = P_SMOKE_IMPACT; + + VectorCopy( origin, p->org ); + VectorScale( dir, vel, p->vel ); + VectorScale( dir, acc, p->accel ); +// VectorSet(p->vel, 0, 0, vel); +// VectorSet(p->accel, 0, 0, acc); + + p->rotate = qtrue; +} + +void CG_ParticleImpactSmokePuff( qhandle_t pshader, vec3_t origin ) { + CG_ParticleImpactSmokePuffExtended( pshader, origin, tv( 0,0,1 ), 8, 500, 20, 20, 30, 0.25f ); +} + + +void CG_Particle_Bleed( qhandle_t pshader, vec3_t start, vec3_t dir, int fleshEntityNum, int duration ) { + cparticle_t *p; + + if ( !pshader ) { + CG_Printf( "CG_Particle_Bleed pshader == ZERO!\n" ); + } + + if ( !CG_ParticleLODCheck() ) { + return; + } + + if ( !free_particles ) { + return; + } + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; + p->time = cg.time; + p->alpha = 1.0; + p->alphavel = 0; + p->roll = 0; + + p->pshader = pshader; + + p->endtime = cg.time + duration; + + if ( fleshEntityNum ) { + p->startfade = cg.time; + } else { + p->startfade = cg.time + 100; + } + + p->width = 4; + p->height = 4; + + p->endheight = 4 + rand() % 3; + p->endwidth = p->endheight; + + p->type = P_SMOKE; + + VectorCopy( start, p->org ); + p->vel[0] = 0; + p->vel[1] = 0; + p->vel[2] = -20; + VectorClear( p->accel ); + + p->rotate = qfalse; + + p->roll = rand() % 179; + + if ( fleshEntityNum ) { + p->color = MUSTARD; + } else { + p->color = BLOODRED; + } + p->alpha = 0.75; + +} + +//void CG_Particle_OilParticle (qhandle_t pshader, centity_t *cent) +void CG_Particle_OilParticle( qhandle_t pshader, vec3_t origin, vec3_t dir, int ptime, int snum ) { // snum is parent ent number? + cparticle_t *p; + + int time; + int time2; + float ratio; + +// float duration = 1500; + float duration = 2000; + + time = cg.time; + time2 = cg.time + ptime; + + ratio = (float)1 - ( (float)time / (float)time2 ); + + if ( !pshader ) { + CG_Printf( "CG_Particle_OilParticle == ZERO!\n" ); + } + + if ( !free_particles ) { + return; + } + + if ( !CG_ParticleLODCheck() ) { + return; + } + + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; + p->time = cg.time; + p->alphavel = 0; + p->roll = 0; + + p->pshader = pshader; + + p->endtime = cg.time + duration; + + p->startfade = p->endtime; + + p->width = 2; + p->height = 2; + + p->endwidth = 1; + p->endheight = 1; + + p->type = P_SMOKE; + + VectorCopy( origin, p->org ); + + p->vel[0] = ( dir[0] * ( 16 * ratio ) ); + p->vel[1] = ( dir[1] * ( 16 * ratio ) ); + p->vel[2] = ( dir[2] * ( 16 * ratio ) ); +// p->vel[2] = (dir[2]); + + p->snum = snum; + + VectorClear( p->accel ); + + p->accel[2] = -20; + + p->rotate = qfalse; + + p->roll = rand() % 179; + + p->alpha = 0.5; + + p->color = BLOODRED; + +} + + +void CG_Particle_OilSlick( qhandle_t pshader, centity_t *cent ) { + cparticle_t *p; + + if ( !pshader ) { + CG_Printf( "CG_Particle_OilSlick == ZERO!\n" ); + } + + if ( !free_particles ) { + return; + } + + if ( !CG_ParticleLODCheck() ) { + return; + } + + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; + p->time = cg.time; + + if ( cent->currentState.angles2[2] ) { + p->endtime = cg.time + cent->currentState.angles2[2]; + } else { + p->endtime = cg.time + 60000; + } + + p->startfade = p->endtime; + + p->alpha = 1.0; + p->alphavel = 0; + p->roll = 0; + + p->pshader = pshader; + + if ( cent->currentState.angles2[0] || cent->currentState.angles2[1] ) { + p->width = cent->currentState.angles2[0]; + p->height = cent->currentState.angles2[0]; + + p->endheight = cent->currentState.angles2[1]; + p->endwidth = cent->currentState.angles2[1]; + } else + { + p->width = 8; + p->height = 8; + + p->endheight = 16; + p->endwidth = 16; + } + + p->type = P_FLAT_SCALEUP; + + p->snum = cent->currentState.density; + + VectorCopy( cent->currentState.origin, p->org ); + + p->org[2] += 0.55 + ( crandom() * 0.5 ); + + p->vel[0] = 0; + p->vel[1] = 0; + p->vel[2] = 0; + VectorClear( p->accel ); + + p->rotate = qfalse; + + p->roll = rand() % 179; + + p->alpha = 0.75; + +} + +void CG_OilSlickRemove( centity_t *cent ) { + cparticle_t *p, *next; + int id; + + id = cent->currentState.density; + + if ( !id ) { + CG_Printf( "CG_OilSlickRevove NULL id\n" ); + } + + for ( p = active_particles ; p ; p = next ) + { + next = p->next; + + if ( p->type == P_FLAT_SCALEUP ) { + if ( p->snum == id ) { + p->endtime = cg.time + 100; + p->startfade = p->endtime; + p->type = P_FLAT_SCALEUP_FADE; + + } + } + + } +} + +qboolean ValidBloodPool( vec3_t start ) { +#define EXTRUDE_DIST 0.5 + + vec3_t angles; + vec3_t right, up; + vec3_t this_pos, x_pos, center_pos, end_pos; + float x, y; + float fwidth, fheight; + trace_t trace; + vec3_t normal; + + fwidth = 16; + fheight = 16; + + VectorSet( normal, 0, 0, 1 ); + + vectoangles( normal, angles ); + AngleVectors( angles, NULL, right, up ); + + VectorMA( start, EXTRUDE_DIST, normal, center_pos ); + + for ( x = -fwidth / 2; x < fwidth; x += fwidth ) + { + VectorMA( center_pos, x, right, x_pos ); + + for ( y = -fheight / 2; y < fheight; y += fheight ) + { + VectorMA( x_pos, y, up, this_pos ); + VectorMA( this_pos, -EXTRUDE_DIST * 2, normal, end_pos ); + + CG_Trace( &trace, this_pos, NULL, NULL, end_pos, -1, CONTENTS_SOLID ); + + + if ( trace.entityNum < ( MAX_ENTITIES - 1 ) ) { // may only land on world + return qfalse; + } + + if ( !( !trace.startsolid && trace.fraction < 1 ) ) { + return qfalse; + } + + } + } + + return qtrue; +} + +void CG_BloodPool( localEntity_t *le, qhandle_t pshader, trace_t *tr ) { + cparticle_t *p; + qboolean legit; + vec3_t start; + float rndSize; + + if ( !pshader ) { + CG_Printf( "CG_BloodPool pshader == ZERO!\n" ); + } + + if ( !free_particles ) { + return; + } + + VectorCopy( tr->endpos, start ); + legit = ValidBloodPool( start ); + + if ( !legit ) { + return; + } + + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; + p->time = cg.time; + + p->endtime = cg.time + 3000; + p->startfade = p->endtime; + + p->alpha = 1.0; + p->alphavel = 0; + p->roll = 0; + + p->pshader = pshader; + + rndSize = 0.4 + random() * 0.6; + + p->width = 8 * rndSize; + p->height = 8 * rndSize; + + p->endheight = 16 * rndSize; + p->endwidth = 16 * rndSize; + + p->type = P_FLAT_SCALEUP; + + VectorCopy( start, p->org ); + + p->vel[0] = 0; + p->vel[1] = 0; + p->vel[2] = 0; + VectorClear( p->accel ); + + p->rotate = qfalse; + + p->roll = rand() % 179; + + p->alpha = 0.75; + + p->color = BLOODRED; +} + +#define NORMALSIZE 16 +#define LARGESIZE 32 + +void CG_ParticleBloodCloud( centity_t *cent, vec3_t origin, vec3_t dir ) { + float length; + float dist; + float crittersize; + vec3_t angles, forward; + vec3_t point; + cparticle_t *p; + int i; + + dist = 0; + + length = VectorLength( dir ); + vectoangles( dir, angles ); + AngleVectors( angles, forward, NULL, NULL ); + + if ( cent->currentState.density == 0 ) { // normal ai size + crittersize = NORMALSIZE; + } else { + crittersize = LARGESIZE; + } + + if ( length ) { + dist = length / crittersize; + } + + if ( dist < 1 ) { + dist = 1; + } + + VectorCopy( origin, point ); + + for ( i = 0; i < dist; i++ ) + { + VectorMA( point, crittersize, forward, point ); + + if ( !free_particles ) { + return; + } + + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; + + p->time = cg.time; + p->alpha = 1.0; + p->alphavel = 0; + p->roll = 0; + + p->pshader = cgs.media.smokePuffShader; + + p->endtime = cg.time + 450 + ( crandom() * 100 ); + + if ( cent->currentState.aiChar == AICHAR_HELGA || cent->currentState.aiChar == AICHAR_HEINRICH ) { + // stick around longer + p->endtime += 3000; + } + + p->startfade = cg.time; + + if ( cent->currentState.density == 0 ) { // normal ai size + p->width = NORMALSIZE; + p->height = NORMALSIZE; + + p->endheight = NORMALSIZE; + p->endwidth = NORMALSIZE; + } else // large frame + { + p->width = LARGESIZE; + p->height = LARGESIZE; + + p->endheight = LARGESIZE; + p->endwidth = LARGESIZE; + } + + p->type = P_SMOKE; + + VectorCopy( origin, p->org ); + + p->vel[0] = 0; + p->vel[1] = 0; + p->vel[2] = -1; + + VectorClear( p->accel ); + + p->rotate = qfalse; + + p->roll = rand() % 179; + + if ( cent->currentState.aiChar == AICHAR_ZOMBIE ) { + p->color = MUSTARD; + } else { + p->color = BLOODRED; + } + + p->alpha = 0.75; + + } + + +} + +void CG_ParticleBloodCloudZombie( centity_t *cent, vec3_t origin, vec3_t dir ) { + float length; + float dist; + float crittersize; + vec3_t angles, forward; + vec3_t point; + cparticle_t *p; + int i; + + dist = 0; + + length = VectorLength( dir ); + vectoangles( dir, angles ); + AngleVectors( angles, forward, NULL, NULL ); + + if ( cent->currentState.density == 0 ) { // normal ai size + crittersize = NORMALSIZE / 4; + } else { + crittersize = LARGESIZE / 3; + } + + if ( length ) { + dist = length / crittersize; + } + + if ( dist < 1 ) { + dist = 1; + } + + VectorCopy( origin, point ); + + for ( i = 0; i < dist; i++ ) + { + VectorMA( point, crittersize, forward, point ); + + if ( !free_particles ) { + return; + } + + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; + + p->time = cg.time; + p->alpha = 0.2; + p->alphavel = 0; + p->roll = 0; + + p->pshader = cgs.media.bloodCloudShader; + + // RF, stay around for long enough to expand and dissipate naturally + if ( length ) { + p->endtime = cg.time + 3500 + ( crandom() * 2000 ); + } else { + p->endtime = cg.time + 750 + ( crandom() * 500 ); + } + + p->startfade = cg.time; + + if ( cent->currentState.density == 0 ) { // normal ai size + p->width = NORMALSIZE; + p->height = NORMALSIZE; + + // RF, expand while falling + p->endheight = NORMALSIZE * 4.0; + p->endwidth = NORMALSIZE * 4.0; + } else // large frame + { + p->width = LARGESIZE; + p->height = LARGESIZE; + + // RF, expand while falling + p->endheight = LARGESIZE * 3.0; + p->endwidth = LARGESIZE * 3.0; + } + + if ( !length ) { + p->width *= 0.2; + p->height *= 0.2; + + p->endheight = NORMALSIZE; + p->endwidth = NORMALSIZE; + } + + p->type = P_SMOKE; + + VectorCopy( origin, p->org ); + + p->vel[0] = crandom() * 6; + p->vel[1] = crandom() * 6; + p->vel[2] = random() * 6; + + // RF, add some gravity/randomness + p->accel[0] = crandom() * 3; + p->accel[1] = crandom() * 3; + p->accel[2] = -PARTICLE_GRAVITY * 0.2; + + VectorClear( p->accel ); + + p->rotate = qfalse; + + p->roll = rand() % 179; + + p->color = ZOMBIE; + + } + + +} + +void CG_ParticleSparks( vec3_t org, vec3_t vel, int duration, float x, float y, float speed ) { + cparticle_t *p; + + if ( !free_particles ) { + return; + } + + if ( !CG_ParticleLODCheck() ) { + return; + } + + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; + p->time = cg.time; + + p->endtime = cg.time + duration; + p->startfade = cg.time + duration / 2; + + p->color = EMISIVEFADE; + p->alpha = 0.4; + p->alphavel = 0; + + p->height = 0.5; + p->width = 0.5; + p->endheight = 0.5; + p->endwidth = 0.5; + + p->pshader = cgs.media.tracerShader; + + p->type = P_SMOKE; + + VectorCopy( org, p->org ); + + p->org[0] += ( crandom() * x ); + p->org[1] += ( crandom() * y ); + + p->vel[0] = vel[0]; + p->vel[1] = vel[1]; + p->vel[2] = vel[2]; + + p->accel[0] = p->accel[1] = p->accel[2] = 0; + + p->vel[0] += ( crandom() * 4 ); + p->vel[1] += ( crandom() * 4 ); + p->vel[2] += ( 20 + ( crandom() * 10 ) ) * speed; + + p->accel[0] = crandom() * 4; + p->accel[1] = crandom() * 4; + +} + +void CG_ParticleDust( centity_t *cent, vec3_t origin, vec3_t dir ) { + float length; + float dist; + float crittersize; + vec3_t angles, forward; + vec3_t point; + cparticle_t *p; + int i; + + dist = 0; + + VectorNegate( dir, dir ); + length = VectorLength( dir ); + vectoangles( dir, angles ); + AngleVectors( angles, forward, NULL, NULL ); + + if ( cent->currentState.density == 0 ) { // normal ai size + crittersize = NORMALSIZE; + } else { + crittersize = LARGESIZE; + } + + if ( length ) { + dist = length / crittersize; + } + + if ( dist < 1 ) { + dist = 1; + } + + VectorCopy( origin, point ); + + for ( i = 0; i < dist; i++ ) + { + VectorMA( point, crittersize, forward, point ); + + if ( !free_particles ) { + return; + } + + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; + + p->time = cg.time; + p->alpha = 5.0; + p->alphavel = 0; + p->roll = 0; + + p->pshader = cgs.media.bloodCloudShader; + + // RF, stay around for long enough to expand and dissipate naturally + if ( length ) { + p->endtime = cg.time + 4500 + ( crandom() * 3500 ); + } else { + p->endtime = cg.time + 750 + ( crandom() * 500 ); + } + + p->startfade = cg.time; + + if ( cent->currentState.density == 0 ) { // normal ai size + p->width = NORMALSIZE; + p->height = NORMALSIZE; + + // RF, expand while falling + p->endheight = NORMALSIZE * 4.0; + p->endwidth = NORMALSIZE * 4.0; + } else // large frame + { + p->width = LARGESIZE; + p->height = LARGESIZE; + + // RF, expand while falling + p->endheight = LARGESIZE * 3.0; + p->endwidth = LARGESIZE * 3.0; + } + + if ( !length ) { + p->width *= 0.2; + p->height *= 0.2; + + p->endheight = NORMALSIZE; + p->endwidth = NORMALSIZE; + } + + p->type = P_SMOKE; + + VectorCopy( point, p->org ); + + p->vel[0] = crandom() * 6; + p->vel[1] = crandom() * 6; + p->vel[2] = random() * 20; + + // RF, add some gravity/randomness + p->accel[0] = crandom() * 3; + p->accel[1] = crandom() * 3; + p->accel[2] = -PARTICLE_GRAVITY * 0.4; + + VectorClear( p->accel ); + + p->rotate = qfalse; + + p->roll = rand() % 179; + + if ( cent->currentState.density ) { + p->color = GREY75; + } else { + p->color = MUSTARD; + } + + p->alpha = 0.75; + + } + + +} + +void CG_ParticleMisc( qhandle_t pshader, vec3_t origin, int size, int duration, float alpha ) { + cparticle_t *p; + + if ( !pshader ) { + CG_Printf( "CG_ParticleImpactSmokePuff pshader == ZERO!\n" ); + } + + if ( !free_particles ) { + return; + } + + if ( !CG_ParticleLODCheck() ) { + return; + } + + p = free_particles; + free_particles = p->next; + p->next = active_particles; + active_particles = p; + p->time = cg.time; + p->alpha = 1.0; + p->alphavel = 0; + p->roll = rand() % 179; + + p->pshader = pshader; + + if ( duration > 0 ) { + p->endtime = cg.time + duration; + } else { + p->endtime = duration; + } + + p->startfade = cg.time; + + p->width = size; + p->height = size; + + p->endheight = size; + p->endwidth = size; + + p->type = P_SPRITE; + + VectorCopy( origin, p->org ); + + p->rotate = qfalse; +} diff --git a/src/cgame/cg_players.c b/src/cgame/cg_players.c new file mode 100644 index 0000000..69aec41 --- /dev/null +++ b/src/cgame/cg_players.c @@ -0,0 +1,5589 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +/* + * name: cg_players.c + * + * desc: handle the media and animation for player entities + * +*/ + +#include "cg_local.h" + +#define SWING_RIGHT 1 +#define SWING_LEFT 2 + +char *cg_customSoundNames[MAX_CUSTOM_SOUNDS] = { + "*death1.wav", + "*death2.wav", + "*death3.wav", + "*jump1.wav", + "*pain25_1.wav", + "*pain50_1.wav", + "*pain75_1.wav", + "*pain100_1.wav", + "*falling1.wav", + "*gasp.wav", + "*drown.wav", + "*fall1.wav", + "*fall2.wav", + "*taunt.wav", + "*exert1.wav", + "*exert2.wav", + "*exert3.wav", +}; + +/* +================ +CG_EntOnFire +================ +*/ +qboolean CG_EntOnFire( centity_t *cent ) { + return ( ( cent->currentState.onFireStart < cg.time ) && + ( cent->currentState.onFireEnd > cg.time ) ); +} + +/* +================ +CG_IsCrouchingAnim +================ +*/ +qboolean CG_IsCrouchingAnim( clientInfo_t *ci, int animNum ) { + animation_t *anim; + + // FIXME: make compatible with new scripting + animNum &= ~ANIM_TOGGLEBIT; + // + anim = BG_GetAnimationForIndex( ci->clientNum, animNum ); + // + if ( anim->movetype & ( ( 1 << ANIM_MT_IDLECR ) | ( 1 << ANIM_MT_WALKCR ) | ( 1 << ANIM_MT_WALKCRBK ) ) ) { + return qtrue; + } + // + return qfalse; +} + +/* +================ +CG_CustomSound + +================ +*/ +sfxHandle_t CG_CustomSound( int clientNum, const char *soundName ) { + clientInfo_t *ci; + int i; + + if ( soundName[0] != '*' ) { + return trap_S_RegisterSound( soundName ); + } + + if ( clientNum < 0 || clientNum >= MAX_CLIENTS ) { + clientNum = 0; + } + ci = &cgs.clientinfo[ clientNum ]; + + for ( i = 0 ; i < MAX_CUSTOM_SOUNDS && cg_customSoundNames[i] ; i++ ) { + if ( !Q_stricmp( soundName, cg_customSoundNames[i] ) ) { + return ci->sounds[i]; + } + } + + CG_Error( "Unknown custom sound: %s", soundName ); + return 0; +} + + + +/* +============================================================================= + +CLIENT INFO + +============================================================================= +*/ + +/* +====================== +CG_ParseGibModels + +Read a configuration file containing gib models for use with this character +====================== +*/ +static qboolean CG_ParseGibModels( const char *filename, clientInfo_t *ci ) { + char *text_p; + int len; + int i; + char *token; + char text[20000]; + fileHandle_t f; + + memset( ci->gibModels, 0, sizeof( ci->gibModels ) ); + + // load the file + len = trap_FS_FOpenFile( filename, &f, FS_READ ); + if ( len <= 0 ) { + return qfalse; + } + if ( len >= sizeof( text ) - 1 ) { + CG_Printf( "File %s too long\n", filename ); + return qfalse; + } + trap_FS_Read( text, len, f ); + text[len] = 0; + trap_FS_FCloseFile( f ); + + // parse the text + text_p = text; + + for ( i = 0; i < MAX_GIB_MODELS; i++ ) { + token = COM_Parse( &text_p ); + if ( !token ) { + break; + } + // cache this model + ci->gibModels[i] = trap_R_RegisterModel( token ); + } + + return qtrue; +} + + +/* +================== +CG_CalcMoveSpeeds +================== +*/ +void CG_CalcMoveSpeeds( clientInfo_t *ci ) { + char *tags[2] = {"tag_footleft", "tag_footright"}; + vec3_t oldPos[2]; + refEntity_t refent; + animation_t *anim; + int i, j, k; + float totalSpeed; + int numSpeed; + int lastLow, low, numSteps, lastFirst, thisFirst; + qboolean isStrafe; + orientation_t o[2]; + + refent.hModel = ci->legsModel; + + for ( i = 0, anim = ci->modelInfo->animations; i < ci->modelInfo->numAnimations; i++, anim++ ) { + + if ( anim->moveSpeed == 0 ) { + continue; + } + + totalSpeed = 0; + lastLow = -1; + numSpeed = 0; + numSteps = 0; + isStrafe = qfalse; + if ( strstr( anim->name, "strafe" ) ) { + isStrafe = qtrue; + } + + // first, get the end frame positions, since thats where we loop from + refent.frame = anim->firstFrame + anim->numFrames - 1; + refent.oldframe = refent.frame; + // for each foot + for ( k = 0; k < 2; k++ ) { + if ( trap_R_LerpTag( &o[k], &refent, tags[k], 0 ) < 0 ) { + CG_Error( "CG_CalcMoveSpeeds: unable to find tag %s, cannot calculate movespeed", tags[k] ); + } + } + // save the positions + for ( k = 0; k < 2; k++ ) { + VectorCopy( o[k].origin, oldPos[k] ); + } + // + // set the numSteps + if ( !isStrafe ) { + if ( o[0].origin[0] > o[1].origin[0] ) { + thisFirst = 0; + } else { thisFirst = 1;} + } else { + if ( o[0].origin[1] > o[1].origin[1] ) { + thisFirst = 0; + } else { thisFirst = 1;} + } + lastFirst = thisFirst; + + // for each frame + for ( j = 0; j < anim->numFrames; j++ ) { + + refent.frame = anim->firstFrame + j; + refent.oldframe = refent.frame; + + // for each foot + for ( k = 0; k < 2; k++ ) { + if ( trap_R_LerpTag( &o[k], &refent, tags[k], 0 ) < 0 ) { + CG_Error( "CG_CalcMoveSpeeds: unable to find tag %s, cannot calculate movespeed", tags[k] ); + } + } + + // find the contact foot + if ( anim->flags & ANIMFL_LADDERANIM ) { + if ( o[0].origin[0] > o[1].origin[0] ) { + low = 0; + } else { + low = 1; + } + totalSpeed += fabs( oldPos[low][2] - o[low].origin[2] ); + } else { + if ( o[0].origin[2] < o[1].origin[2] ) { + low = 0; + } else { + low = 1; + } + if ( !isStrafe ) { + totalSpeed += fabs( oldPos[low][0] - o[low].origin[0] ); + } else { + totalSpeed += fabs( oldPos[low][1] - o[low].origin[1] ); + } + // + // set the numSteps + if ( !isStrafe ) { + if ( o[0].origin[0] > o[1].origin[0] ) { + thisFirst = 0; + } else { thisFirst = 1;} + } else { + if ( o[0].origin[1] > o[1].origin[1] ) { + thisFirst = 0; + } else { thisFirst = 1;} + } + // if they have changed, record the step + if ( lastFirst != thisFirst ) { + numSteps++; + lastFirst = thisFirst; + } + } + + numSpeed++; + + // save the positions + for ( k = 0; k < 2; k++ ) { + VectorCopy( o[k].origin, oldPos[k] ); + } + lastLow = low; + } + + // record the speed + if ( anim->moveSpeed < 0 ) { // use the auto calculations + anim->moveSpeed = (int)( ( totalSpeed / numSpeed ) * 1000.0 / anim->frameLerp ); + } + // set the stepGap + if ( !numSteps ) { + numSteps = 2; + } + if ( numSteps % 2 ) { + numSteps++; // round it up if odd number of steps + } + anim->stepGap = ( 0.5 * ( (float)anim->moveSpeed * (float)anim->duration / 1000.0 ) ); + anim->stepGap /= ( numSteps / 2 ); // in case there are more than 2 steps in animation + if ( isStrafe ) { + anim->stepGap *= 1.3; // HACK + } + } + + if ( cgs.localServer ) { + CG_SendMoveSpeed( ci->modelInfo->animations, ci->modelInfo->numAnimations, ci->modelInfo->modelname ); + } +} + +/* +====================== +CG_ParseAnimationFiles + + Read in all the configuration and script files for this model. +====================== +*/ + +#if 0 // RF, this entire function not used anymore, since we now grab all this stuff from the server + +static qboolean CG_ParseAnimationFiles( const char *modelname, clientInfo_t *ci, int client ) { + char text[100000]; + char filename[MAX_QPATH]; + fileHandle_t f; + int len; + + // set the name of the model in the modelinfo structure + Q_strncpyz( ci->modelInfo->modelname, modelname, sizeof( ci->modelInfo->modelname ) ); + + // load the cfg file + Com_sprintf( filename, sizeof( filename ), "models/players/%s/wolfanim.cfg", modelname ); + len = trap_FS_FOpenFile( filename, &f, FS_READ ); + if ( len <= 0 ) { + return qfalse; + } + if ( len >= sizeof( text ) - 1 ) { + CG_Printf( "File %s too long\n", filename ); + return qfalse; + } + trap_FS_Read( text, len, f ); + text[len] = 0; + trap_FS_FCloseFile( f ); + + // parse the text + BG_AnimParseAnimConfig( ci->modelInfo, filename, text ); + + if ( ci->isSkeletal != ci->modelInfo->isSkeletal ) { + CG_Error( "Mis-match in %s, loaded skeletal model, but file does not specify SKELETAL\n", filename ); + } + + // calc movespeed values if required + CG_CalcMoveSpeeds( ci ); + + // load the script file + Com_sprintf( filename, sizeof( filename ), "models/players/%s/wolfanim.script", modelname ); + len = trap_FS_FOpenFile( filename, &f, FS_READ ); + if ( len <= 0 ) { + if ( ci->modelInfo->version > 1 ) { + return qfalse; + } + // try loading the default script for old legacy models + Com_sprintf( filename, sizeof( filename ), "models/players/default.script", modelname ); + len = trap_FS_FOpenFile( filename, &f, FS_READ ); + if ( len <= 0 ) { + return qfalse; + } + } + if ( len >= sizeof( text ) - 1 ) { + CG_Printf( "File %s too long\n", filename ); + return qfalse; + } + trap_FS_Read( text, len, f ); + text[len] = 0; + trap_FS_FCloseFile( f ); + + // parse the text + BG_AnimParseAnimScript( ci->modelInfo, &cgs.animScriptData, ci->clientNum, filename, text ); + return qtrue; +} +#endif + +/* +========================== +CG_RegisterClientSkin +========================== +*/ + +//----(SA) modified this for head separation + +static qboolean CG_RegisterClientSkin( clientInfo_t *ci, const char *modelName, const char *skinName ) { + char filename[MAX_QPATH]; + + // RF, try and register the new "body_*.skin" file for skeletal animation + Com_sprintf( filename, sizeof( filename ), "models/players/%s/body_%s.skin", modelName, skinName ); + ci->legsSkin = trap_R_RegisterSkin( filename ); + if ( ci->legsSkin ) { // skeletal model + ci->torsoSkin = ci->legsSkin; + return qtrue; + } + + Com_sprintf( filename, sizeof( filename ), "models/players/%s/lower_%s.skin", modelName, skinName ); + ci->legsSkin = trap_R_RegisterSkin( filename ); + + Com_sprintf( filename, sizeof( filename ), "models/players/%s/upper_%s.skin", modelName, skinName ); + ci->torsoSkin = trap_R_RegisterSkin( filename ); + + if ( !ci->legsSkin || !ci->torsoSkin ) { + return qfalse; + } + + return qtrue; +} + +/* +============== +CG_RegisterClientHeadSkin +============== +*/ +static qboolean CG_RegisterClientHeadSkin( clientInfo_t *ci, const char *modelName, const char *hSkinName ) { + char filename[MAX_QPATH]; + + Com_sprintf( filename, sizeof( filename ), "models/players/%s/head_%s.skin", modelName, hSkinName ); + ci->headSkin = trap_R_RegisterSkin( filename ); + + if ( !ci->headSkin ) { + return qfalse; + } + + return qtrue; + +} + +//----(SA) end + + + +//----(SA) added +/* +============== +CG_RegisterAcc +============== +*/ +static qboolean CG_RegisterAcc( clientInfo_t *ci, const char *modelName, const char *skinName, int *model, int *skin ) { + char namefromskin[MAX_QPATH]; + char filename[MAX_QPATH]; + + if ( !model || !skin ) { + return qfalse; + } + + // FIXME: have the check the last 4 chars rather than strstr() + if ( !strstr( skinName, ".md3" ) ) { // try to find a skin in the acc folder that matches + *skin = trap_R_RegisterSkin( va( "%s/%s.skin", modelName, skinName ) ); + + if ( *skin ) { + if ( trap_R_GetSkinModel( *skin, "md3_part", &namefromskin[0] ) ) { + Com_sprintf( filename, sizeof( filename ), "%s/acc/%s", modelName, namefromskin ); + // NOTE: FIXME: this will currently only work with accessories in the /acc directory. + // It will have to strip the directory off the end and then use the remaining + // path in order to work for arbitrary sub-directories. +// Com_sprintf( filename, sizeof( filename ), "%s/%s/%s.md3", modelName, , namefromskin ); + + } else { + Com_sprintf( filename, sizeof( filename ), "%s/%s.md3", modelName, skinName ); + } + } else { + Com_sprintf( filename, sizeof( filename ), "%s/%s.md3", modelName, skinName ); + } + } else { // the skin wants a straight model + Com_sprintf( filename, sizeof( filename ), "%s/%s", modelName, skinName ); + } + + + *model = trap_R_RegisterModel( filename ); + + if ( *model ) { + return qtrue; + } + + return qfalse; +} + +//----(SA) end + + +/* +================== +CG_CheckForExistingModelInfo + + If this player model has already been parsed, then use the existing information. + Otherwise, set the modelInfo pointer to the first free slot. + + returns qtrue if existing model found, qfalse otherwise +================== +*/ +extern animScriptData_t *globalScriptData; +qboolean CG_CheckForExistingModelInfo( clientInfo_t *ci, char *modelName, animModelInfo_t **modelInfo ) { + int i; + animModelInfo_t *trav; // *firstFree=NULL; // TTimo: unused + + globalScriptData = &cgs.animScriptData; + + for ( i = 0; i < MAX_ANIMSCRIPT_MODELS; i++ ) { + trav = cgs.animScriptData.modelInfo[i]; + if ( trav && trav->modelname[0] ) { + // this model is used, so check if it's a match + if ( !Q_stricmp( trav->modelname, modelName ) ) { + // found a match, use this modelinfo + *modelInfo = trav; + cgs.animScriptData.clientModels[ci->clientNum] = i + 1; + return qtrue; + } + } else { + // if we fell down to here, then we have found a free slot + + // request it from the server (game module) + if ( trap_GetModelInfo( ci->clientNum, modelName, &cgs.animScriptData.modelInfo[i] ) ) { + + // success + cgs.animScriptData.clientModels[ci->clientNum] = i + 1; + *modelInfo = cgs.animScriptData.modelInfo[i]; + // calc movespeed/footstep values + CG_CalcMoveSpeeds( ci ); + return qfalse; // we need to cache all the assets for this character + + } + + // huh!? + CG_Error( "CG_CheckForExistingModelInfo: unable to optain modelInfo from server" ); + } + + } + + CG_Error( "unable to find a free modelinfo slot, cannot continue\n" ); + // qfalse signifies that we need to parse the information from the script files + return qfalse; +} + + +/* +========================== +CG_RegisterClientModelname +========================== +*/ + +//----(SA) modified this for head separation + +static qboolean CG_RegisterClientModelname( clientInfo_t *ci, const char *modelName, const char *skinName ) { + char namefromskin[MAX_QPATH]; + char filename[MAX_QPATH]; + char name[MAX_QPATH]; + int i; + + // if any skins failed to load, return failure +//----(SA) modified this for head separation + if ( !CG_RegisterClientSkin( ci, modelName, skinName ) ) { + Com_Printf( "Failed to load skin file: %s/%s\n", modelName, skinName ); + return qfalse; + } + + // load cmodels before models so filecache works + + if ( trap_R_GetSkinModel( ci->legsSkin, "md3_part", &namefromskin[0] ) ) { + Com_sprintf( filename, sizeof( filename ), "models/players/%s/%s", modelName, namefromskin ); + ci->legsModel = trap_R_RegisterModel( filename ); + } else { // try skeletal model + Com_sprintf( filename, sizeof( filename ), "models/players/%s/body.mds", modelName ); + ci->legsModel = trap_R_RegisterModel( filename ); + + if ( !ci->legsModel ) { // revert to mesh animation + Com_sprintf( filename, sizeof( filename ), "models/players/%s/lower.md3", modelName ); + ci->legsModel = trap_R_RegisterModel( filename ); + } else { // found skeletal model + ci->isSkeletal = qtrue; + ci->torsoModel = ci->legsModel; + } + } + + if ( !ci->isSkeletal ) { + + if ( !ci->legsModel ) { + Com_Printf( "Failed to load legs model file %s\n", filename ); + return qfalse; + } + + if ( trap_R_GetSkinModel( ci->torsoSkin, "md3_part", &namefromskin[0] ) ) { + Com_sprintf( filename, sizeof( filename ), "models/players/%s/%s", modelName, namefromskin ); + } else { + Com_sprintf( filename, sizeof( filename ), "models/players/%s/upper.md3", modelName ); + } + + ci->torsoModel = trap_R_RegisterModel( filename ); + + if ( !ci->torsoModel ) { + Com_Printf( "Failed to load torso model file %s\n", filename ); + return qfalse; + } + + } + +//----(SA) testing + { + char scaleString[MAX_QPATH]; + char *string_p; + char *scaleToken; + qboolean badscale = qfalse; + + string_p = scaleString; + + if ( trap_R_GetSkinModel( ci->legsSkin, "playerscale", &scaleString[0] ) ) { + scaleToken = COM_Parse( &string_p ); + if ( !scaleToken ) { + badscale = qtrue; // and drop to "if(badscale)" below + } else + { + ci->playermodelScale[0] = atof( scaleToken ); + + scaleToken = COM_Parse( &string_p ); + if ( !scaleToken ) { + badscale = qtrue; // and drop to "if(badscale)" below + } else + { + ci->playermodelScale[1] = atof( scaleToken ); + + scaleToken = COM_Parse( &string_p ); + if ( !scaleToken ) { + badscale = qtrue; // and drop to "if(badscale)" below + } else + { + ci->playermodelScale[2] = atof( scaleToken ); + } + } + } + } + + if ( badscale ) { + ci->playermodelScale[0] = + ci->playermodelScale[1] = + ci->playermodelScale[2] = 0.0f; + } + } +//----(SA) end + + + // try all the accessories + if ( trap_R_GetSkinModel( ci->legsSkin, "md3_beltr", &namefromskin[0] ) ) { + CG_RegisterAcc( ci, va( "models/players/%s", modelName ), namefromskin, &ci->accModels[ACC_BELT_LEFT], &ci->accSkins[ACC_BELT_LEFT] ); + } + if ( trap_R_GetSkinModel( ci->legsSkin, "md3_beltl", &namefromskin[0] ) ) { + CG_RegisterAcc( ci, va( "models/players/%s", modelName ), namefromskin, &ci->accModels[ACC_BELT_RIGHT], &ci->accSkins[ACC_BELT_RIGHT] ); + } + if ( trap_R_GetSkinModel( ci->torsoSkin, "md3_belt", &namefromskin[0] ) ) { + CG_RegisterAcc( ci, va( "models/players/%s", modelName ), namefromskin, &ci->accModels[ACC_BELT], &ci->accSkins[ACC_BELT] ); + } + if ( trap_R_GetSkinModel( ci->torsoSkin, "md3_back", &namefromskin[0] ) ) { + CG_RegisterAcc( ci, va( "models/players/%s", modelName ), namefromskin, &ci->accModels[ACC_BACK], &ci->accSkins[ACC_BACK] ); + } + if ( trap_R_GetSkinModel( ci->torsoSkin, "md3_weapon", &namefromskin[0] ) ) { + CG_RegisterAcc( ci, va( "models/players/%s", modelName ), namefromskin, &ci->accModels[ACC_WEAPON], &ci->accSkins[ACC_WEAPON] ); + } + if ( trap_R_GetSkinModel( ci->torsoSkin, "md3_weapon2", &namefromskin[0] ) ) { + CG_RegisterAcc( ci, va( "models/players/%s", modelName ), namefromskin, &ci->accModels[ACC_WEAPON2], &ci->accSkins[ACC_WEAPON2] ); + } +//----(SA) added + // try anim script parts + for ( i = 0; i < 8; i++ ) { + if ( trap_R_GetSkinModel( ci->torsoSkin, va( "md3_animscript%d", i ), &namefromskin[0] ) ) { + CG_RegisterAcc( ci, va( "models/players/%s", modelName ), namefromskin, &ci->partModels[ACC_WEAPON], &ci->partSkins[ACC_WEAPON] ); + } + } +//----(SA) end + + // look for this model in the list of models already opened + if ( !CG_CheckForExistingModelInfo( ci, (char *)modelName, &ci->modelInfo ) ) { +/* + if ( !CG_ParseAnimationFiles( modelName, ci, ci->clientNum ) ) { + Com_Printf( "Failed to load animation file %s\n", filename ); + return qfalse; + } +*/ + // special case, only cache certain shaders/models for certain characters + if ( !Q_strcasecmp( (char *)modelName, "zombie" ) ) { + cgs.media.zombieSpiritWallShader = trap_R_RegisterShader( "zombieDeathWindTrail" ); + cgs.media.zombieSpiritTrailShader = trap_R_RegisterShader( "zombieSpiritTrail" ); + cgs.media.zombieSpiritSkullShader = trap_R_RegisterShader( "zombieSpiritSkull" ); + //cgs.media.zombieDeathDustShader = trap_R_RegisterShader( "zombieDeathDust" ); + //cgs.media.zombieBodyFadeShader = trap_R_RegisterShader( "zombieBodyFade" ); + //cgs.media.zombieHeadFadeShader = trap_R_RegisterShader( "zombieHeadFade" ); + + cgs.media.skeletonSkinShader = trap_R_RegisterShader( "skeletonSkin" ); + cgs.media.skeletonLegsModel = trap_R_RegisterModel( "models/players/skel/lower.md3" ); + cgs.media.skeletonLegsSkin = trap_R_RegisterSkin( "models/players/skel/lower_default.skin" ); + cgs.media.skeletonTorsoModel = trap_R_RegisterModel( "models/players/skel/upper.md3" ); + cgs.media.skeletonTorsoSkin = trap_R_RegisterSkin( "models/players/skel/upper_default.skin" ); + cgs.media.skeletonHeadModel = trap_R_RegisterModel( "models/players/skel/head.md3" ); + cgs.media.skeletonHeadSkin = trap_R_RegisterSkin( "models/players/skel/head_default.skin" ); + + cgs.media.zombieSpiritSound = trap_S_RegisterSound( "sound/zombie/attack/spirit_start.wav" ); + cgs.media.zombieSpiritLoopSound = trap_S_RegisterSound( "sound/zombie/attack/spirit_loop.wav" ); + cgs.media.zombieDeathSound = trap_S_RegisterSound( "sound/world/ceramicbreak.wav" ); // Zombie Gib + + cgs.media.spiritSkullModel = trap_R_RegisterModel( "models/mapobjects/skull/skul2t.md3" ); + + CG_RegisterWeapon( WP_GAUNTLET ); + } else if ( !Q_strcasecmp( (char *)modelName, "beast" ) ) { + cgs.media.helgaSpiritSkullShader = trap_R_RegisterShader( "helgaSpiritGhost" ); + cgs.media.helgaSpiritTrailShader = trap_R_RegisterShader( "helgaSpiritTrail" ); + cgs.media.helgaGhostModel = trap_R_RegisterModel( "models/players/beast/ghost.md3" ); + cgs.media.helgaSpiritLoopSound = trap_S_RegisterSound( "sound/beast/tortured_souls_loop.wav" ); + cgs.media.helgaSpiritSound = CG_SoundScriptPrecache( "helgaSpiritStartSound" ); + cgs.media.helgaGaspSound = CG_SoundScriptPrecache( "helgaSpiritGasp" ); + } else if ( !Q_strcasecmp( (char *)modelName, "loper" ) ) { + //cgs.media.loperGroundChargeShader = trap_R_RegisterShader( "loperGroundCharge" ); + } else if ( !Q_strcasecmp( (char *)modelName, "protosoldier" ) ) { + cgs.media.protoArmorBreak = CG_SoundScriptPrecache( "Protosoldier_loseArmor" ); + + cgs.media.protoArmor[0] = trap_R_RegisterModel( "models/players/protosoldier/armor/nodam_chest.md3" ); + cgs.media.protoArmor[1] = trap_R_RegisterModel( "models/players/protosoldier/armor/nodam_lftcalf.md3" ); + cgs.media.protoArmor[2] = trap_R_RegisterModel( "models/players/protosoldier/armor/nodam_lftforarm.md3" ); + cgs.media.protoArmor[3] = trap_R_RegisterModel( "models/players/protosoldier/armor/nodam_lftshoulder.md3" ); + cgs.media.protoArmor[4] = trap_R_RegisterModel( "models/players/protosoldier/armor/nodam_lftthigh.md3" ); + cgs.media.protoArmor[5] = trap_R_RegisterModel( "models/players/protosoldier/armor/nodam_rtcalf.md3" ); + cgs.media.protoArmor[6] = trap_R_RegisterModel( "models/players/protosoldier/armor/nodam_rtforarm.md3" ); + cgs.media.protoArmor[7] = trap_R_RegisterModel( "models/players/protosoldier/armor/nodam_rtshoulder.md3" ); + cgs.media.protoArmor[8] = trap_R_RegisterModel( "models/players/protosoldier/armor/nodam_rtthigh.md3" ); + + cgs.media.protoArmor[9] = trap_R_RegisterModel( "models/players/protosoldier/armor/dam_chest1.md3" ); + cgs.media.protoArmor[10] = trap_R_RegisterModel( "models/players/protosoldier/armor/dam_lftcalf1.md3" ); + cgs.media.protoArmor[11] = trap_R_RegisterModel( "models/players/protosoldier/armor/dam_lftforarm1.md3" ); + cgs.media.protoArmor[12] = trap_R_RegisterModel( "models/players/protosoldier/armor/dam_lftshoulder1.md3" ); + cgs.media.protoArmor[13] = trap_R_RegisterModel( "models/players/protosoldier/armor/dam_lftthigh1.md3" ); + cgs.media.protoArmor[14] = trap_R_RegisterModel( "models/players/protosoldier/armor/dam_rtcalf1.md3" ); + cgs.media.protoArmor[15] = trap_R_RegisterModel( "models/players/protosoldier/armor/dam_rtforarm1.md3" ); + cgs.media.protoArmor[16] = trap_R_RegisterModel( "models/players/protosoldier/armor/dam_rtshoulder1.md3" ); + cgs.media.protoArmor[17] = trap_R_RegisterModel( "models/players/protosoldier/armor/dam_rtthigh1.md3" ); + + cgs.media.protoArmor[18] = trap_R_RegisterModel( "models/players/protosoldier/armor/dam_chest2.md3" ); + cgs.media.protoArmor[19] = trap_R_RegisterModel( "models/players/protosoldier/armor/dam_lftcalf2.md3" ); + cgs.media.protoArmor[20] = trap_R_RegisterModel( "models/players/protosoldier/armor/dam_lftforarm2.md3" ); + cgs.media.protoArmor[21] = trap_R_RegisterModel( "models/players/protosoldier/armor/dam_lftshoulder2.md3" ); + cgs.media.protoArmor[22] = trap_R_RegisterModel( "models/players/protosoldier/armor/dam_lftthigh2.md3" ); + cgs.media.protoArmor[23] = trap_R_RegisterModel( "models/players/protosoldier/armor/dam_rtcalf2.md3" ); + cgs.media.protoArmor[24] = trap_R_RegisterModel( "models/players/protosoldier/armor/dam_rtforarm2.md3" ); + cgs.media.protoArmor[25] = trap_R_RegisterModel( "models/players/protosoldier/armor/dam_rtshoulder2.md3" ); + cgs.media.protoArmor[26] = trap_R_RegisterModel( "models/players/protosoldier/armor/dam_rtthigh2.md3" ); + } else if ( !Q_strcasecmp( (char *)modelName, "supersoldier" ) ) { + + cgs.media.superArmorBreak = CG_SoundScriptPrecache( "Supersoldier_loseArmor" ); + + cgs.media.superArmor[0] = trap_R_RegisterModel( "models/players/supersoldier/armor/nodam_chest.md3" ); + cgs.media.superArmor[1] = trap_R_RegisterModel( "models/players/supersoldier/armor/nodam_lftcalf.md3" ); + cgs.media.superArmor[2] = trap_R_RegisterModel( "models/players/supersoldier/armor/nodam_lftforarm.md3" ); + cgs.media.superArmor[3] = trap_R_RegisterModel( "models/players/supersoldier/armor/nodam_lftshoulder.md3" ); + cgs.media.superArmor[4] = trap_R_RegisterModel( "models/players/supersoldier/armor/nodam_lftthigh.md3" ); + cgs.media.superArmor[5] = trap_R_RegisterModel( "models/players/supersoldier/armor/nodam_rtcalf.md3" ); + cgs.media.superArmor[6] = trap_R_RegisterModel( "models/players/supersoldier/armor/nodam_rtforarm.md3" ); + cgs.media.superArmor[7] = trap_R_RegisterModel( "models/players/supersoldier/armor/nodam_rtshoulder.md3" ); + cgs.media.superArmor[8] = trap_R_RegisterModel( "models/players/supersoldier/armor/nodam_rtthigh.md3" ); + + cgs.media.superArmor[9] = trap_R_RegisterModel( "models/players/supersoldier/armor/nodam_lftfoot.md3" ); + cgs.media.superArmor[10] = trap_R_RegisterModel( "models/players/supersoldier/armor/nodam_rtfoot.md3" ); + cgs.media.superArmor[11] = trap_R_RegisterModel( "models/players/supersoldier/armor/nodam_lftuparm.md3" ); + cgs.media.superArmor[12] = trap_R_RegisterModel( "models/players/supersoldier/armor/nodam_rtuparm.md3" ); + cgs.media.superArmor[13] = trap_R_RegisterModel( "models/players/supersoldier/armor/nodam_waist.md3" ); + cgs.media.superArmor[14] = trap_R_RegisterModel( "models/players/supersoldier/armor/nodam_lftknee.md3" ); + cgs.media.superArmor[15] = trap_R_RegisterModel( "models/players/supersoldier/armor/nodam_rtknee.md3" ); + + + + cgs.media.superArmor[16] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_chest1.md3" ); + cgs.media.superArmor[17] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_lftcalf1.md3" ); + cgs.media.superArmor[18] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_lftforarm1.md3" ); + cgs.media.superArmor[19] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_lftshoulder1.md3" ); + cgs.media.superArmor[20] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_lftthigh1.md3" ); + cgs.media.superArmor[21] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_rtcalf1.md3" ); + cgs.media.superArmor[22] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_rtforarm1.md3" ); + cgs.media.superArmor[23] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_rtshoulder1.md3" ); + cgs.media.superArmor[24] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_rtthigh1.md3" ); + + cgs.media.superArmor[25] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_lftfoot1.md3" ); + cgs.media.superArmor[26] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_rtfoot1.md3" ); + cgs.media.superArmor[27] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_lftuparm1.md3" ); + cgs.media.superArmor[28] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_rtuparm1.md3" ); + cgs.media.superArmor[29] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_waist1.md3" ); + + cgs.media.superArmor[30] = 0; + cgs.media.superArmor[31] = 0; + + + cgs.media.superArmor[32] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_chest2.md3" ); + cgs.media.superArmor[33] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_lftcalf2.md3" ); + cgs.media.superArmor[34] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_lftforarm2.md3" ); + cgs.media.superArmor[35] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_lftshoulder2.md3" ); + cgs.media.superArmor[36] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_lftthigh2.md3" ); + cgs.media.superArmor[37] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_rtcalf2.md3" ); + cgs.media.superArmor[38] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_rtforarm2.md3" ); + cgs.media.superArmor[39] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_rtshoulder2.md3" ); + cgs.media.superArmor[30] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_rtthigh2.md3" ); + + cgs.media.superArmor[31] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_lftfoot2.md3" ); + cgs.media.superArmor[32] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_rtfoot2.md3" ); + cgs.media.superArmor[33] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_lftuparm2.md3" ); + cgs.media.superArmor[44] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_rtuparm2.md3" ); + cgs.media.superArmor[45] = trap_R_RegisterModel( "models/players/supersoldier/armor/dam_waist2.md3" ); + + cgs.media.superArmor[46] = 0; + cgs.media.superArmor[47] = 0; +/* +super has that proto doesn't... + +dam_chest3 attached to tag_chest + +dam_lftfoot1 +dam_lftfoot2 +nodam_lftfoot attached to tag_footleft + +dam_rtfoot1 +dam_rtfoot2 +nodam_rtfoot attached to tag_footright + +dam_lftuparm1 +dam_lftuparm2 +nodam_lftuparm attached to tag_sholeft + +dam_rtuparm1 +dam_rtuparm2 +nodam_rtuparm attached to tag_shoright + +dam_waist1 +dam_waist2 +nodam_waist attached to tag_torso + +nodam_lftknee attached to tag_calfleft + +nodam_rtknee attached to tag_calfright +*/ + } else if ( !Q_strcasecmp( (char *)modelName, "dark" ) ) { + + cgs.media.superArmorBreak = CG_SoundScriptPrecache( "Supersoldier_loseArmor" ); + + cgs.media.superArmor[0] = trap_R_RegisterModel( "models/players/dark/armor/nodam_chest.md3" ); + cgs.media.superArmor[1] = trap_R_RegisterModel( "models/players/dark/armor/nodam_lftcalf.md3" ); + cgs.media.superArmor[2] = trap_R_RegisterModel( "models/players/dark/armor/nodam_lftforarm.md3" ); + cgs.media.superArmor[3] = trap_R_RegisterModel( "models/players/dark/armor/nodam_lftshoulder.md3" ); + cgs.media.superArmor[4] = trap_R_RegisterModel( "models/players/dark/armor/nodam_lftthigh.md3" ); + cgs.media.superArmor[5] = trap_R_RegisterModel( "models/players/dark/armor/nodam_rtcalf.md3" ); + cgs.media.superArmor[6] = trap_R_RegisterModel( "models/players/dark/armor/nodam_rtforarm.md3" ); + cgs.media.superArmor[7] = trap_R_RegisterModel( "models/players/dark/armor/nodam_rtshoulder.md3" ); + cgs.media.superArmor[8] = trap_R_RegisterModel( "models/players/dark/armor/nodam_rtthigh.md3" ); + + cgs.media.superArmor[9] = trap_R_RegisterModel( "models/players/dark/armor/nodam_lftfoot.md3" ); + cgs.media.superArmor[10] = trap_R_RegisterModel( "models/players/dark/armor/nodam_rtfoot.md3" ); + cgs.media.superArmor[11] = trap_R_RegisterModel( "models/players/dark/armor/nodam_lftuparm.md3" ); + cgs.media.superArmor[12] = trap_R_RegisterModel( "models/players/dark/armor/nodam_rtuparm.md3" ); + cgs.media.superArmor[13] = trap_R_RegisterModel( "models/players/dark/armor/nodam_waist.md3" ); + cgs.media.superArmor[14] = trap_R_RegisterModel( "models/players/dark/armor/nodam_lftknee.md3" ); + cgs.media.superArmor[15] = trap_R_RegisterModel( "models/players/dark/armor/nodam_rtknee.md3" ); + + + + cgs.media.superArmor[16] = trap_R_RegisterModel( "models/players/dark/armor/dam_chest1.md3" ); + cgs.media.superArmor[17] = trap_R_RegisterModel( "models/players/dark/armor/dam_lftcalf1.md3" ); + cgs.media.superArmor[18] = trap_R_RegisterModel( "models/players/dark/armor/dam_lftforarm1.md3" ); + cgs.media.superArmor[19] = trap_R_RegisterModel( "models/players/dark/armor/dam_lftshoulder1.md3" ); + cgs.media.superArmor[20] = trap_R_RegisterModel( "models/players/dark/armor/dam_lftthigh1.md3" ); + cgs.media.superArmor[21] = trap_R_RegisterModel( "models/players/dark/armor/dam_rtcalf1.md3" ); + cgs.media.superArmor[22] = trap_R_RegisterModel( "models/players/dark/armor/dam_rtforarm1.md3" ); + cgs.media.superArmor[23] = trap_R_RegisterModel( "models/players/dark/armor/dam_rtshoulder1.md3" ); + cgs.media.superArmor[24] = trap_R_RegisterModel( "models/players/dark/armor/dam_rtthigh1.md3" ); + + cgs.media.superArmor[25] = trap_R_RegisterModel( "models/players/dark/armor/dam_lftfoot1.md3" ); + cgs.media.superArmor[26] = trap_R_RegisterModel( "models/players/dark/armor/dam_rtfoot1.md3" ); + cgs.media.superArmor[27] = trap_R_RegisterModel( "models/players/dark/armor/dam_lftuparm1.md3" ); + cgs.media.superArmor[28] = trap_R_RegisterModel( "models/players/dark/armor/dam_rtuparm1.md3" ); + cgs.media.superArmor[29] = trap_R_RegisterModel( "models/players/dark/armor/dam_waist1.md3" ); + + cgs.media.superArmor[30] = 0; + cgs.media.superArmor[31] = 0; + + + cgs.media.superArmor[32] = trap_R_RegisterModel( "models/players/dark/armor/dam_chest2.md3" ); + cgs.media.superArmor[33] = trap_R_RegisterModel( "models/players/dark/armor/dam_lftcalf2.md3" ); + cgs.media.superArmor[34] = trap_R_RegisterModel( "models/players/dark/armor/dam_lftforarm2.md3" ); + cgs.media.superArmor[35] = trap_R_RegisterModel( "models/players/dark/armor/dam_lftshoulder2.md3" ); + cgs.media.superArmor[36] = trap_R_RegisterModel( "models/players/dark/armor/dam_lftthigh2.md3" ); + cgs.media.superArmor[37] = trap_R_RegisterModel( "models/players/dark/armor/dam_rtcalf2.md3" ); + cgs.media.superArmor[38] = trap_R_RegisterModel( "models/players/dark/armor/dam_rtforarm2.md3" ); + cgs.media.superArmor[39] = trap_R_RegisterModel( "models/players/dark/armor/dam_rtshoulder2.md3" ); + cgs.media.superArmor[30] = trap_R_RegisterModel( "models/players/dark/armor/dam_rtthigh2.md3" ); + + cgs.media.superArmor[31] = trap_R_RegisterModel( "models/players/dark/armor/dam_lftfoot2.md3" ); + cgs.media.superArmor[32] = trap_R_RegisterModel( "models/players/dark/armor/dam_rtfoot2.md3" ); + cgs.media.superArmor[33] = trap_R_RegisterModel( "models/players/dark/armor/dam_lftuparm2.md3" ); + cgs.media.superArmor[44] = trap_R_RegisterModel( "models/players/dark/armor/dam_rtuparm2.md3" ); + cgs.media.superArmor[45] = trap_R_RegisterModel( "models/players/dark/armor/dam_waist2.md3" ); + + cgs.media.superArmor[46] = 0; + cgs.media.superArmor[47] = 0; + + } else if ( !Q_strcasecmp( (char *)modelName, "heinrich" ) ) { + + cgs.media.heinrichArmorBreak = CG_SoundScriptPrecache( "Heinrich_loseArmor" ); + + // RF, these are also used but supersoldier "spirits" in end map + cgs.media.zombieSpiritLoopSound = trap_S_RegisterSound( "sound/zombie/attack/spirit_loop.wav" ); + cgs.media.ssSpiritSkullModel = trap_R_RegisterModel( "models/players/supersoldier/ssghost.md3" ); + + cgs.media.zombieSpiritTrailShader = trap_R_RegisterShader( "zombieSpiritTrail" ); + cgs.media.zombieSpiritLoopSound = trap_S_RegisterSound( "sound/zombie/attack/spirit_loop.wav" ); + cgs.media.helgaGaspSound = CG_SoundScriptPrecache( "helgaSpiritGasp" ); + + cgs.media.debrisHitSound = trap_S_RegisterSound( "sound/world/debris_hit.wav" ); + + cgs.media.heinrichArmor[0] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_chest.md3" ); + cgs.media.heinrichArmor[1] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_lftcalf.md3" ); + cgs.media.heinrichArmor[2] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_lftforarm.md3" ); + cgs.media.heinrichArmor[3] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_lftshoulder.md3" ); + cgs.media.heinrichArmor[4] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_lftthigh.md3" ); + cgs.media.heinrichArmor[5] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_rtcalf.md3" ); + cgs.media.heinrichArmor[6] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_rtforarm.md3" ); + cgs.media.heinrichArmor[7] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_rtshoulder.md3" ); + cgs.media.heinrichArmor[8] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_rtthigh.md3" ); + + cgs.media.heinrichArmor[9] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_lftfoot.md3" ); + cgs.media.heinrichArmor[10] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_rtfoot.md3" ); + cgs.media.heinrichArmor[11] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_lftuparm.md3" ); + cgs.media.heinrichArmor[12] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_rtuparm.md3" ); + cgs.media.heinrichArmor[13] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_waist.md3" ); + cgs.media.heinrichArmor[14] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_lftknee.md3" ); + cgs.media.heinrichArmor[15] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_rtknee.md3" ); + + cgs.media.heinrichArmor[16] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_lftelbow.md3" ); + cgs.media.heinrichArmor[17] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_rtelbow.md3" ); + cgs.media.heinrichArmor[18] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_lfthip.md3" ); + cgs.media.heinrichArmor[19] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_rthip.md3" ); + cgs.media.heinrichArmor[20] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_lftshin.md3" ); + cgs.media.heinrichArmor[21] = trap_R_RegisterModel( "models/players/heinrich/armor/nodam_rtshin.md3" ); + + + cgs.media.heinrichArmor[22] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_chest1.md3" ); + cgs.media.heinrichArmor[23] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_lftcalf1.md3" ); + cgs.media.heinrichArmor[24] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_lftforarm1.md3" ); + cgs.media.heinrichArmor[25] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_lftshoulder1.md3" ); + cgs.media.heinrichArmor[26] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_lftthigh1.md3" ); + cgs.media.heinrichArmor[27] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_rtcalf1.md3" ); + cgs.media.heinrichArmor[28] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_rtforarm1.md3" ); + cgs.media.heinrichArmor[29] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_rtshoulder1.md3" ); + cgs.media.heinrichArmor[30] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_rtthigh1.md3" ); + + cgs.media.heinrichArmor[31] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_lftfoot1.md3" ); + cgs.media.heinrichArmor[32] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_rtfoot1.md3" ); + cgs.media.heinrichArmor[33] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_lftuparm1.md3" ); + cgs.media.heinrichArmor[34] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_rtuparm1.md3" ); + cgs.media.heinrichArmor[35] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_waist1.md3" ); + cgs.media.heinrichArmor[36] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_lftknee1.md3" ); + cgs.media.heinrichArmor[37] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_rtknee1.md3" ); + + cgs.media.heinrichArmor[38] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_lftelbow1.md3" ); + cgs.media.heinrichArmor[39] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_rtelbow1.md3" ); + cgs.media.heinrichArmor[40] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_lfthip1.md3" ); + cgs.media.heinrichArmor[41] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_rthip1.md3" ); + cgs.media.heinrichArmor[42] = 0; + cgs.media.heinrichArmor[43] = 0; + + + cgs.media.heinrichArmor[44] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_chest2.md3" ); + cgs.media.heinrichArmor[45] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_lftcalf2.md3" ); + cgs.media.heinrichArmor[46] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_lftforarm2.md3" ); + cgs.media.heinrichArmor[47] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_lftshoulder2.md3" ); + cgs.media.heinrichArmor[48] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_lftthigh2.md3" ); + cgs.media.heinrichArmor[49] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_rtcalf2.md3" ); + cgs.media.heinrichArmor[50] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_rtforarm2.md3" ); + cgs.media.heinrichArmor[51] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_rtshoulder2.md3" ); + cgs.media.heinrichArmor[52] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_rtthigh2.md3" ); + + cgs.media.heinrichArmor[43] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_lftfoot2.md3" ); + cgs.media.heinrichArmor[54] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_rtfoot2.md3" ); + cgs.media.heinrichArmor[55] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_lftuparm2.md3" ); + cgs.media.heinrichArmor[56] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_rtuparm2.md3" ); + cgs.media.heinrichArmor[57] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_waist2.md3" ); + cgs.media.heinrichArmor[58] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_lftknee2.md3" ); + cgs.media.heinrichArmor[59] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_rtknee2.md3" ); + + cgs.media.heinrichArmor[60] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_lftelbow2.md3" ); + cgs.media.heinrichArmor[61] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_rtelbow2.md3" ); + cgs.media.heinrichArmor[62] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_lfthip2.md3" ); + cgs.media.heinrichArmor[63] = trap_R_RegisterModel( "models/players/heinrich/armor/dam_rthip2.md3" ); + cgs.media.heinrichArmor[64] = 0; + cgs.media.heinrichArmor[65] = 0; + +/* +heinrich has that ss doesn't... + +dam_lftelbow +dam_lftelbow1 +dam_lftelbow2 attached to tag_sholeft + +nodam_rtelbow +dam_rtelbow1 +dam_rtelbow2 attached to tag_shoright + +nodam_lfthip +dam_lfthip1 +dam_lfthip2 +dam_lfthip3 attached to tag_legleft + +nodam_rthip +dam_rthip1 +dam_rthip2 +dam_rthip3 attached to tag_legright + +nodam_lftshin attached to tag_calfleft + +nodam_rtshin attached to tag_calfright +*/ + } +//----(SA) end + +// end special AI model loading + + + // -------- FOOTSTEP SOUNDS --------- + // load model specific footsteps + // FIXME: this should be moved over to per model scripts or animation scripting + if ( !Q_strcasecmp( (char *)modelName, "eliteguard" ) ) { + // ELITEGUARD + + for ( i = 0; i < 4; i++ ) { + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/eliteguard/step%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_ELITE_STEP][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/eliteguard/clank%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_ELITE_METAL][i] = trap_S_RegisterSound( name ); + +// Com_sprintf (name, sizeof(name), "sound/player/footsteps/eliteguard/roof%i.wav", i+1); +// cgs.media.footsteps[FOOTSTEP_ELITE_ROOF][i] = trap_S_RegisterSound (name); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/eliteguard/wood%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_ELITE_WOOD][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/eliteguard/gravel%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_ELITE_GRAVEL][i] = trap_S_RegisterSound( name ); + } + } else if ( !Q_strcasecmp( (char *)modelName, "protosoldier" ) ) { + // ProtoSoldier + for ( i = 0; i < 4; i++ ) { + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/protosoldier/step%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_PROTOSOLDIER_STEP][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/protosoldier/clank%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_PROTOSOLDIER_METAL][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/protosoldier/grass%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_PROTOSOLDIER_GRASS][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/protosoldier/gravel%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_PROTOSOLDIER_GRAVEL][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/protosoldier/wood%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_PROTOSOLDIER_WOOD][i] = trap_S_RegisterSound( name ); + } + } else if ( !Q_strcasecmp( (char *)modelName, "supersoldier" ) ) { + // SuperSoldier/HEINRICH + for ( i = 0; i < 4; i++ ) { + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/supersoldier/step%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_SUPERSOLDIER_STEP][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/supersoldier/clank%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_SUPERSOLDIER_METAL][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/supersoldier/grass%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_SUPERSOLDIER_GRASS][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/supersoldier/gravel%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_SUPERSOLDIER_GRAVEL][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/supersoldier/wood%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_SUPERSOLDIER_WOOD][i] = trap_S_RegisterSound( name ); + } + } + // Heinrich special + else if ( !Q_strcasecmp( (char *)modelName, "heinrich" ) ) { + // SuperSoldier/HEINRICH + for ( i = 0; i < 4; i++ ) { + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/heinrich/step%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_HEINRICH][i] = trap_S_RegisterSound( name ); + } + } else if ( !Q_strcasecmp( (char *)modelName, "loper" ) ) { + // Loper + for ( i = 0; i < 4; i++ ) { + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/loper/clank%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_LOPER_METAL][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/loper/step%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_LOPER_STEP][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/loper/wood%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_LOPER_WOOD][i] = trap_S_RegisterSound( name ); + } + } else if ( !Q_strcasecmp( (char *)modelName, "zombie" ) ) { + // Zombie + for ( i = 0; i < 4; i++ ) { + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/zombie/gravel%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_ZOMBIE_GRAVEL][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/zombie/step%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_ZOMBIE_STEP][i] = trap_S_RegisterSound( name ); + + Com_sprintf( name, sizeof( name ), "sound/player/footsteps/zombie/wood%i.wav", i + 1 ); + cgs.media.footsteps[FOOTSTEP_ZOMBIE_WOOD][i] = trap_S_RegisterSound( name ); + } + } else if ( !Q_strcasecmp( (char *)modelName, "beast" ) ) { + // Helga Boss + cgs.media.footsteps[FOOTSTEP_BEAST][0] = CG_SoundScriptPrecache( "beastStep" ); // just precache the sound script + } + } + + // whoops! this stuff would never get set if it found one existing already!! + if ( !Q_strcasecmp( (char *)modelName, "loper" ) ) { + ci->partModels[8] = trap_R_RegisterModel( va( "models/players/%s/spinner.md3", modelName ) ); + } else if ( !Q_strcasecmp( (char *)modelName, "sealoper" ) ) { + ci->partModels[8] = trap_R_RegisterModel( va( "models/players/%s/spinner.md3", modelName ) ); + } + + + return qtrue; +} + +/* +============== +CG_RegisterClientHeadname +============== +*/ +static qboolean CG_RegisterClientHeadname( clientInfo_t *ci, const char *modelName, const char *hSkinName ) { + char namefromskin[MAX_QPATH]; + char filename[MAX_QPATH]; + int i; + + if ( !CG_RegisterClientHeadSkin( ci, modelName, hSkinName ) ) { + Com_Printf( "Failed to load head skin file: %s/head_%s.skin\n", modelName, hSkinName ); //----(SA) + return qfalse; + } + + if ( trap_R_GetSkinModel( ci->headSkin, "md3_part", &namefromskin[0] ) ) { + Com_sprintf( filename, sizeof( filename ), "models/players/%s/%s", modelName, namefromskin ); + } else { + Com_sprintf( filename, sizeof( filename ), "models/players/%s/head.md3", modelName ); + } + + ci->headModel = trap_R_RegisterModel( filename ); + if ( !ci->headModel ) { + Com_Printf( "Failed to load head model file %s\n", filename ); //----(SA) + return qfalse; + } + + if ( trap_R_GetSkinModel( ci->headSkin, "md3_hat", &namefromskin[0] ) ) { + CG_RegisterAcc( ci, va( "models/players/%s", modelName ), namefromskin, &ci->accModels[ACC_HAT], &ci->accSkins[ACC_HAT] ); + } + + for ( i = 0; i < ACC_NUM_MOUTH - 1; i++ ) { + if ( trap_R_GetSkinModel( ci->headSkin, va( "md3_hat%d", 2 + i ), &namefromskin[0] ) ) { + CG_RegisterAcc( ci, va( "models/players/%s", modelName ), namefromskin, &ci->accModels[ACC_MOUTH2 + i], &ci->accSkins[ACC_MOUTH2 + i] ); + } + } + + return qtrue; +} +/* +==================== +CG_ColorFromString +==================== +*/ +static void CG_ColorFromString( const char *v, vec3_t color ) { + int val; + + VectorClear( color ); + + val = atoi( v ); + + if ( val < 1 || val > 7 ) { + VectorSet( color, 1, 1, 1 ); + return; + } + + if ( val & 1 ) { + color[2] = 1.0f; + } + if ( val & 2 ) { + color[1] = 1.0f; + } + if ( val & 4 ) { + color[0] = 1.0f; + } +} + +/* +=================== +CG_LoadClientInfo + +Load it now, taking the disk hits. +This will usually be deferred to a safe time +=================== +*/ +void CG_LoadClientInfo( clientInfo_t *ci ) { + const char *dir, *fallback; + int i; + const char *s; + int clientNum; + int headfail = 0; + char filename[MAX_QPATH]; + +//----(SA) modified this for head separation + + // load the head first (since if the head loads but there is a problem with something in the lower + // body, you will want to default the model back to a default and want the head to match) + // + + if ( !CG_RegisterClientHeadname( ci, ci->modelName, ci->hSkinName ) ) { + if ( cg_buildScript.integer ) { + CG_Error( "CG_RegisterClientHeadname( %s, %s ) failed. setting default", ci->modelName, ci->hSkinName ); + } + + // fall back to default head + if ( !CG_RegisterClientHeadname( ci, ci->modelName, "default" ) ) { + headfail = 1; + if ( cg_buildScript.integer ) { + CG_Error( "head model/skin (%s/default) failed to register", ci->modelName ); //----(SA) + } + } + } + + if ( headfail || !CG_RegisterClientModelname( ci, ci->modelName, ci->skinName ) ) { + if ( cg_buildScript.integer ) { + CG_Error( "CG_RegisterClientModelname( %s, %s ) failed", ci->modelName, ci->skinName ); + } + + // fall back + if ( cgs.gametype >= GT_TEAM ) { + // keep skin name but set default model + if ( !CG_RegisterClientModelname( ci, DEFAULT_MODEL, ci->skinName ) ) { + CG_Error( "DEFAULT_MODEL / skin (%s/%s) failed to register", DEFAULT_MODEL, ci->skinName ); + } + } else if ( cgs.gametype == GT_SINGLE_PLAYER && !headfail ) { + // try to keep the model but default the skin (so you can tell bad guys from good) + if ( !CG_RegisterClientModelname( ci, ci->modelName, "default" ) ) { + CG_Error( "DEFAULT_MODEL (%s/default) failed to register", ci->modelName ); + } + } else { + // go totally default + if ( !CG_RegisterClientModelname( ci, DEFAULT_MODEL, "default" ) ) { + CG_Error( "DEFAULT_MODEL (%s/default) failed to register", DEFAULT_MODEL ); + } + + // fall back to default head + if ( !CG_RegisterClientHeadname( ci, DEFAULT_MODEL, "default" ) ) { + CG_Error( "model/ DEFAULT_HEAD / skin (%s/default) failed to register", DEFAULT_HEAD ); + } + + } + + } + +//----(SA) end + + // sounds + dir = ci->modelName; + fallback = DEFAULT_MODEL; + + for ( i = 0 ; i < MAX_CUSTOM_SOUNDS ; i++ ) { + s = cg_customSoundNames[i]; + if ( !s ) { + break; + } + ci->sounds[i] = trap_S_RegisterSound( va( "sound/player/%s/%s", dir, s + 1 ) ); + if ( !ci->sounds[i] ) { + ci->sounds[i] = trap_S_RegisterSound( va( "sound/player/%s/%s", fallback, s + 1 ) ); + } + } + + // load the gibs + Com_sprintf( filename, sizeof( filename ), "models/players/%s/gibs.cfg", dir ); + if ( !CG_ParseGibModels( filename, ci ) ) { + // n/mind.. gib code will automatically fall back to old gibs + } + + ci->deferred = qfalse; + + // reset any existing players and bodies, because they might be in bad + // frames for this new model + clientNum = ci - cgs.clientinfo; + for ( i = 0 ; i < MAX_GENTITIES ; i++ ) { + if ( cg_entities[i].currentState.clientNum == clientNum + && cg_entities[i].currentState.eType == ET_PLAYER ) { + CG_ResetPlayerEntity( &cg_entities[i] ); + } + } +} + +/* +====================== +CG_CopyClientInfoModel +====================== +*/ +static void CG_CopyClientInfoModel( clientInfo_t *from, clientInfo_t *to ) { + VectorCopy( from->playermodelScale, to->playermodelScale ); + + to->legsModel = from->legsModel; + to->legsSkin = from->legsSkin; + to->torsoModel = from->torsoModel; + to->torsoSkin = from->torsoSkin; + to->headModel = from->headModel; + to->headSkin = from->headSkin; + to->isSkeletal = from->isSkeletal; + to->modelIcon = from->modelIcon; + +//----(SA) + memcpy( to->accModels, from->accModels, sizeof( to->accModels ) ); + memcpy( to->accSkins, from->accSkins, sizeof( to->accSkins ) ); + memcpy( to->partModels, from->partModels, sizeof( to->partModels ) ); +// memcpy( to->partSkins, from->partSkins, sizeof( to->partSkins ) ); +//----(SA) end + + memcpy( to->sounds, from->sounds, sizeof( to->sounds ) ); + + // Ridah + memcpy( to->gibModels, from->gibModels, sizeof( to->gibModels ) ); + // done. + + // copy the modelInfo + to->modelInfo = from->modelInfo; + cgs.animScriptData.clientModels[ to->clientNum ] = cgs.animScriptData.clientModels[ from->clientNum ]; + +} + +/* +====================== +CG_ScanForExistingClientInfo +====================== +*/ +static qboolean CG_ScanForExistingClientInfo( clientInfo_t *ci ) { + int i; + clientInfo_t *match; + + for ( i = 0 ; i < cgs.maxclients ; i++ ) { + match = &cgs.clientinfo[ i ]; + if ( !match->infoValid ) { + continue; + } + if ( match->deferred ) { + continue; + } +//----(SA) added checks for same head. FIXME: soon this will be more efficient than just looking for a complete character (body/head) match. + if ( + !Q_stricmp( ci->modelName, match->modelName ) + && !Q_stricmp( ci->skinName, match->skinName ) + && !Q_stricmp( ci->hSkinName, match->hSkinName ) + ) { + +//----(SA) done + + // this clientinfo is identical, so use it's handles + + ci->deferred = qfalse; + + CG_CopyClientInfoModel( match, ci ); + + return qtrue; + } + } + + // nothing matches, so defer the load + return qfalse; +} + +/* +====================== +CG_SetDeferredClientInfo + +We aren't going to load it now, so grab some other +client's info to use until we have some spare time. +====================== +*/ +static void CG_SetDeferredClientInfo( clientInfo_t *ci ) { + int i; + clientInfo_t *match; + + // if we are in teamplay, only grab a model if the skin is correct + if ( cgs.gametype >= GT_TEAM ) { + for ( i = 0 ; i < cgs.maxclients ; i++ ) { + match = &cgs.clientinfo[ i ]; + if ( !match->infoValid ) { + continue; + } + if ( Q_stricmp( ci->skinName, match->skinName ) ) { + continue; + } + ci->deferred = qtrue; + CG_CopyClientInfoModel( match, ci ); + return; + } + + // load the full model, because we don't ever want to show + // an improper team skin. This will cause a hitch for the first + // player, when the second enters. Combat shouldn't be going on + // yet, so it shouldn't matter + CG_LoadClientInfo( ci ); + return; + } + + // find the first valid clientinfo and grab its stuff + for ( i = 0 ; i < cgs.maxclients ; i++ ) { + match = &cgs.clientinfo[ i ]; + if ( !match->infoValid ) { + continue; + } + + ci->deferred = qtrue; + CG_CopyClientInfoModel( match, ci ); + return; + } + + // we should never get here... + CG_Printf( "CG_SetDeferredClientInfo: no valid clients!\n" ); + + CG_LoadClientInfo( ci ); +} + + +/* +====================== +CG_NewClientInfo +====================== +*/ +void CG_NewClientInfo( int clientNum ) { + clientInfo_t *ci; + clientInfo_t newInfo; + const char *configstring; + const char *v; + char *slash; + + ci = &cgs.clientinfo[clientNum]; + + configstring = CG_ConfigString( clientNum + CS_PLAYERS ); + if ( !configstring[0] ) { + memset( ci, 0, sizeof( *ci ) ); + return; // player just left + } + + // build into a temp buffer so the defer checks can use + // the old value + memset( &newInfo, 0, sizeof( newInfo ) ); + + newInfo.clientNum = clientNum; + + // isolate the player's name + v = Info_ValueForKey( configstring, "n" ); + Q_strncpyz( newInfo.name, v, sizeof( newInfo.name ) ); + + // colors + v = Info_ValueForKey( configstring, "c1" ); + CG_ColorFromString( v, newInfo.color ); + + // bot skill + v = Info_ValueForKey( configstring, "skill" ); + newInfo.botSkill = atoi( v ); + + // handicap + v = Info_ValueForKey( configstring, "hc" ); + newInfo.handicap = atoi( v ); + + // wins + v = Info_ValueForKey( configstring, "w" ); + newInfo.wins = atoi( v ); + + // losses + v = Info_ValueForKey( configstring, "l" ); + newInfo.losses = atoi( v ); + + // team + v = Info_ValueForKey( configstring, "t" ); + newInfo.team = atoi( v ); + +//----(SA) modified this for head separation + +// (SA) note to Ryan: The problem I see with having the model set for cg_forceModel in the game (g_forcemodel) +// is that it was initally there for a performance/fairness thing so you can connect to a +// server and not use other players goofy models or whatever. We should still have some simple +// client-side thing for defaulting all models to one particular player model or something. (did that make sense?) + + // head + v = Info_ValueForKey( configstring, "head" ); +/* RF, disabled this, not needed anymore + if ( cg_forceModel.integer ) + { + char modelStr[MAX_QPATH]; + + // forcemodel makes everyone use a single model + // to prevent load hitches + + trap_Cvar_VariableStringBuffer( "head", modelStr, sizeof( modelStr ) ); + Q_strncpyz( newInfo.hSkinName, modelStr, sizeof( newInfo.hSkinName ) ); + } + else { +*/ + Q_strncpyz( newInfo.hSkinName, v, sizeof( newInfo.hSkinName ) ); +// } + +//----(SA) modified this for head separation + + // model + v = Info_ValueForKey( configstring, "model" ); +/* RF, disabled this, not needed anymore + if ( cg_forceModel.integer ) { + // forcemodel makes everyone use a single model + // to prevent load hitches + char modelStr[MAX_QPATH]; + char *skin; + + trap_Cvar_VariableStringBuffer( "model", modelStr, sizeof( modelStr ) ); + if ( ( skin = strchr( modelStr, '/' ) ) == NULL) { + skin = "default"; + } else { + *skin++ = 0; + } + + Q_strncpyz( newInfo.skinName, skin, sizeof( newInfo.skinName ) ); + Q_strncpyz( newInfo.modelName, modelStr, sizeof( newInfo.modelName ) ); + + if ( cgs.gametype >= GT_TEAM ) { + // keep skin name + slash = strchr( v, '/' ); + if ( slash ) { + Q_strncpyz( newInfo.skinName, slash + 1, sizeof( newInfo.skinName ) ); + } + } + } else { +*/ + Q_strncpyz( newInfo.modelName, v, sizeof( newInfo.modelName ) ); + + slash = strchr( newInfo.modelName, '/' ); + if ( !slash ) { + // modelName did not include a skin name + Q_strncpyz( newInfo.skinName, "default", sizeof( newInfo.skinName ) ); + } else { + Q_strncpyz( newInfo.skinName, slash + 1, sizeof( newInfo.skinName ) ); + // truncate modelName + *slash = 0; + } +// } + + //----(SA) modify \/ to differentiate for head models/skins as well + + + // scan for an existing clientinfo that matches this modelname + // so we can avoid loading checks if possible + if ( !CG_ScanForExistingClientInfo( &newInfo ) ) { + qboolean forceDefer; + + // RF, disabled this, we can't have this happening in Wolf. If there is not enough memory, + // then we have a leak or havent allocated enough hunk + forceDefer = qfalse; + +// forceDefer = trap_MemoryRemaining() < 4000000; + + // if we are defering loads, just have it pick the first valid +// if ( forceDefer || ( cg_deferPlayers.integer && !cg_buildScript.integer && !cg.loading ) ) { + + // very temporary! do not defer any players just yet + // we need to get ai's to be non-deferred players before we can + // do this (SA) + if ( forceDefer ) { + // keep whatever they had if it won't violate team skins + if ( ci->infoValid && + ( cgs.gametype < GT_TEAM || !Q_stricmp( newInfo.skinName, ci->skinName ) ) ) { + CG_CopyClientInfoModel( ci, &newInfo ); + newInfo.deferred = qtrue; + } else { + // use whatever is available + CG_SetDeferredClientInfo( &newInfo ); + } + // if we are low on memory, leave them with this model + if ( forceDefer ) { + CG_Printf( "Memory is low. Using deferred model.\n" ); + newInfo.deferred = qfalse; + } + } else { + CG_LoadClientInfo( &newInfo ); + } + } + + // replace whatever was there with the new one + newInfo.infoValid = qtrue; + *ci = newInfo; +} + + + +/* +====================== +CG_LoadDeferredPlayers + +Called each frame when a player is dead +and the scoreboard is up +so deferred players can be loaded +====================== +*/ +void CG_LoadDeferredPlayers( void ) { + int i; + clientInfo_t *ci; + + // scan for a deferred player to load + for ( i = 0, ci = cgs.clientinfo ; i < cgs.maxclients ; i++, ci++ ) { + if ( ci->infoValid && ci->deferred ) { + // if we are low on memory, leave it deferred + if ( trap_MemoryRemaining() < 4000000 ) { + CG_Printf( "Memory is low. Using deferred model.\n" ); + ci->deferred = qfalse; + continue; + } + CG_LoadClientInfo( ci ); + } + } +} + +/* +============================================================================= + +PLAYER ANIMATION + +============================================================================= +*/ + + +/* +=============== +CG_SetLerpFrameAnimation + +may include ANIM_TOGGLEBIT +=============== +*/ +static void CG_SetLerpFrameAnimation( clientInfo_t *ci, lerpFrame_t *lf, int newAnimation ) { + animation_t *anim; + + if ( !ci->modelInfo ) { + return; + } + + lf->animationNumber = newAnimation; + newAnimation &= ~ANIM_TOGGLEBIT; + + if ( newAnimation < 0 || newAnimation >= ci->modelInfo->numAnimations ) { + CG_Error( "Bad animation number (CG_SLFA): %i", newAnimation ); + } + + anim = &ci->modelInfo->animations[ newAnimation ]; + + lf->animation = anim; + lf->animationTime = lf->frameTime + anim->initialLerp; + + if ( cg_debugAnim.integer == 1 ) { // DHM - Nerve :: extra debug info + CG_Printf( "Anim: %i, %s\n", newAnimation, ci->modelInfo->animations[newAnimation].name ); + } +} + +/* +=============== +CG_RunLerpFrame + +Sets cg.snap, cg.oldFrame, and cg.backlerp +cg.time should be between oldFrameTime and frameTime after exit +=============== +*/ +void CG_RunLerpFrame( clientInfo_t *ci, lerpFrame_t *lf, int newAnimation, float speedScale ) { + int f; + animation_t *anim; + + // debugging tool to get no animations + if ( cg_animSpeed.integer == 0 ) { + lf->oldFrame = lf->frame = lf->backlerp = 0; + return; + } + + // see if the animation sequence is switching + if ( ci && ( newAnimation != lf->animationNumber || !lf->animation ) ) { //----(SA) modified + CG_SetLerpFrameAnimation( ci, lf, newAnimation ); + } + + // if we have passed the current frame, move it to + // oldFrame and calculate a new frame + if ( cg.time >= lf->frameTime ) { + lf->oldFrame = lf->frame; + lf->oldFrameTime = lf->frameTime; + + // get the next frame based on the animation + anim = lf->animation; + if ( !anim->frameLerp ) { + return; // shouldn't happen + } + if ( cg.time < lf->animationTime ) { + lf->frameTime = lf->animationTime; // initial lerp + } else { + lf->frameTime = lf->oldFrameTime + anim->frameLerp; + } + f = ( lf->frameTime - lf->animationTime ) / anim->frameLerp; + f *= speedScale; // adjust for haste, etc + if ( f >= anim->numFrames ) { + f -= anim->numFrames; + if ( anim->loopFrames ) { + f %= anim->loopFrames; + f += anim->numFrames - anim->loopFrames; + } else { + f = anim->numFrames - 1; + // the animation is stuck at the end, so it + // can immediately transition to another sequence + lf->frameTime = cg.time; + } + } + lf->frame = anim->firstFrame + f; + if ( cg.time > lf->frameTime ) { + lf->frameTime = cg.time; + if ( cg_debugAnim.integer ) { + CG_Printf( "Clamp lf->frameTime\n" ); + } + } + } + + if ( lf->frameTime > cg.time + 200 ) { + lf->frameTime = cg.time; + } + + if ( lf->oldFrameTime > cg.time ) { + lf->oldFrameTime = cg.time; + } + // calculate current lerp value + if ( lf->frameTime == lf->oldFrameTime ) { + lf->backlerp = 0; + } else { + lf->backlerp = 1.0 - (float)( cg.time - lf->oldFrameTime ) / ( lf->frameTime - lf->oldFrameTime ); + } +} + + +/* +=============== +CG_ClearLerpFrame +=============== +*/ +static void CG_ClearLerpFrame( clientInfo_t *ci, lerpFrame_t *lf, int animationNumber ) { + lf->frameTime = lf->oldFrameTime = cg.time; + CG_SetLerpFrameAnimation( ci, lf, animationNumber ); + if ( lf->animation ) { + lf->oldFrame = lf->frame = lf->animation->firstFrame; + } +} + +//------------------------------------------------------------------------------ +// Ridah, variable speed animations +/* +=============== +CG_SetLerpFrameAnimationRate + +may include ANIM_TOGGLEBIT +=============== +*/ +void CG_SetLerpFrameAnimationRate( centity_t *cent, clientInfo_t *ci, lerpFrame_t *lf, int newAnimation ) { + animation_t *anim, *oldanim; + int transitionMin = -1, oldAnimTime, oldAnimNum; + qboolean firstAnim = qfalse; + + if ( !ci->modelInfo ) { + return; + } + + oldAnimTime = lf->animationTime; + oldanim = lf->animation; + oldAnimNum = lf->animationNumber; + + if ( !lf->animation ) { + firstAnim = qtrue; + } + + lf->animationNumber = newAnimation; + newAnimation &= ~ANIM_TOGGLEBIT; + + if ( newAnimation < 0 || newAnimation >= ci->modelInfo->numAnimations ) { + CG_Error( "Bad animation number (CG_SLFAR): %i", newAnimation ); + } + + anim = &ci->modelInfo->animations[ newAnimation ]; + + lf->animation = anim; + lf->animationTime = lf->frameTime + anim->initialLerp; + + if ( !( anim->flags & ANIMFL_FIRINGANIM ) || ( lf != ¢->pe.torso ) ) { + if ( ( lf == ¢->pe.legs ) && ( CG_IsCrouchingAnim( ci, newAnimation ) != CG_IsCrouchingAnim( ci, oldAnimNum ) ) ) { + if ( anim->moveSpeed || ( anim->movetype & ( ( 1 << ANIM_MT_TURNLEFT ) | ( 1 << ANIM_MT_TURNRIGHT ) ) ) ) { // if unknown movetype, go there faster + transitionMin = lf->frameTime + 200; // slowly raise/drop + } else { + transitionMin = lf->frameTime + 350; // slowly raise/drop + } + } else if ( anim->moveSpeed ) { + transitionMin = lf->frameTime + 120; // always do some lerping (?) + } else { // not moving, so take your time + transitionMin = lf->frameTime + 170; // always do some lerping (?) + + } + if ( oldanim && oldanim->animBlend ) { //transitionMin < lf->frameTime + oldanim->animBlend) { + transitionMin = lf->frameTime + oldanim->animBlend; + lf->animationTime = transitionMin; + } else { + // slow down transitions according to speed + if ( anim->moveSpeed && lf->animSpeedScale < 1.0 ) { + lf->animationTime += anim->initialLerp; + } + + if ( lf->animationTime < transitionMin ) { + lf->animationTime = transitionMin; + } + } + } + + // if first anim, go immediately + if ( firstAnim ) { + lf->frameTime = cg.time - 1; + lf->animationTime = cg.time - 1; + lf->frame = anim->firstFrame; + } + + // if death frame, move straight to last frame + if ( cent->currentState.eFlags & EF_DEATH_FRAME ) { + lf->frameTime = cg.time - 1; + lf->animationTime = cg.time - 1; + lf->frame = anim->firstFrame + anim->numFrames - 1; + lf->oldFrame = lf->frame; + lf->oldAnimationNumber = lf->animationNumber; + } + + if ( cg_debugAnim.integer == 1 ) { // DHM - Nerve :: extra debug info + CG_Printf( "Anim: %i, %s\n", newAnimation, ci->modelInfo->animations[newAnimation].name ); + } +} + +/* +=============== +CG_RunLerpFrameRate + +Sets cg.snap, cg.oldFrame, and cg.backlerp +cg.time should be between oldFrameTime and frameTime after exit +=============== +*/ +void CG_RunLerpFrameRate( clientInfo_t *ci, lerpFrame_t *lf, int newAnimation, centity_t *cent, int recursion ) { + int f; + animation_t *anim, *oldAnim; + animation_t *otherAnim = NULL; // TTimo: init + qboolean isLadderAnim; + +#define ANIM_SCALEMAX_LOW 1.1 +#define ANIM_SCALEMAX_HIGH 1.6 + +#define ANIM_SPEEDMAX_LOW 100 +#define ANIM_SPEEDMAX_HIGH 20 + + // debugging tool to get no animations + if ( cg_animSpeed.integer == 0 ) { + lf->oldFrame = lf->frame = lf->backlerp = 0; + return; + } + + isLadderAnim = lf->animation && ( lf->animation->flags & ANIMFL_LADDERANIM ); + + oldAnim = lf->animation; + + // see if the animation sequence is switching + if ( newAnimation != lf->animationNumber || !lf->animation ) { +#if 0 +#ifdef _DEBUG // RF, debugging anims +//if ( cent->currentState.number == 3 ) + CG_Printf( "(%i) %s anim change on %s: %s -> %s\n", cg.time, ci->modelInfo->modelname, ( lf == ¢->pe.legs ? "LEGS" : "TORSO" ), lf->animation->name, ci->modelInfo->animations[newAnimation & ~( 1 << 9 )].name ); +#endif +#endif + CG_SetLerpFrameAnimationRate( cent, ci, lf, newAnimation ); + } + + anim = lf->animation; + + // check for forcing last frame + if ( cent->currentState.eFlags & EF_FORCE_END_FRAME ) { + lf->frame = anim->firstFrame + anim->numFrames - 1; + lf->oldFrame = lf->frame; + lf->backlerp = 0; + return; + } + + // Ridah, make sure the animation speed is updated when possible + if ( anim->moveSpeed && lf->oldFrameSnapshotTime ) { + float moveSpeed; + + // calculate the speed at which we moved over the last frame + if ( cg.latestSnapshotTime != lf->oldFrameSnapshotTime && cg.nextSnap ) { + if ( cent->currentState.number == cg.snap->ps.clientNum ) { + if ( isLadderAnim ) { // only use Z axis for speed + lf->oldFramePos[0] = cent->lerpOrigin[0]; + lf->oldFramePos[1] = cent->lerpOrigin[1]; + } else { // only use x/y axis + lf->oldFramePos[2] = cent->lerpOrigin[2]; + } + moveSpeed = Distance( cent->lerpOrigin, lf->oldFramePos ) / ( (float)( cg.time - lf->oldFrameTime ) / 1000.0 ); + } else { + if ( isLadderAnim ) { // only use Z axis for speed + lf->oldFramePos[0] = cent->currentState.pos.trBase[0]; + lf->oldFramePos[1] = cent->currentState.pos.trBase[1]; + } + // TTimo + // show_bug.cgi?id=407 + if ( cg.snap != cg.nextSnap ) { + moveSpeed = Distance( cent->currentState.pos.trBase, cent->nextState.pos.trBase ) / ( (float)( cg.nextSnap->serverTime - cg.snap->serverTime ) / 1000.0 ); + } else + { + moveSpeed = 0; + } + } + // + // convert it to a factor of this animation's movespeed + lf->animSpeedScale = moveSpeed / (float)anim->moveSpeed; + lf->oldFrameSnapshotTime = cg.latestSnapshotTime; + } + } else { + // move at normal speed + lf->animSpeedScale = 1.0; + lf->oldFrameSnapshotTime = cg.latestSnapshotTime; + } + // adjust with manual setting (pain anims) + lf->animSpeedScale *= cent->pe.animSpeed; + + // if we have passed the current frame, move it to + // oldFrame and calculate a new frame + if ( cg.time >= lf->frameTime ) { + + lf->oldFrame = lf->frame; + lf->oldFrameTime = lf->frameTime; + VectorCopy( cent->lerpOrigin, lf->oldFramePos ); + + // restrict the speed range + if ( lf->animSpeedScale < 0.25 ) { // if it's too slow, then a really slow spped, combined with a sudden take-off, can leave them playing a really slow frame while they a moving really fast + if ( lf->animSpeedScale < 0.01 && isLadderAnim ) { + lf->animSpeedScale = 0.0; + } else { + lf->animSpeedScale = 0.25; + } + } else if ( lf->animSpeedScale > ANIM_SCALEMAX_LOW ) { + + if ( !( anim->flags & ANIMFL_LADDERANIM ) ) { + // allow slower anims to speed up more than faster anims + if ( anim->moveSpeed > ANIM_SPEEDMAX_LOW ) { + lf->animSpeedScale = ANIM_SCALEMAX_LOW; + } else if ( anim->moveSpeed < ANIM_SPEEDMAX_HIGH ) { + if ( lf->animSpeedScale > ANIM_SCALEMAX_HIGH ) { + lf->animSpeedScale = ANIM_SCALEMAX_HIGH; + } + } else { + lf->animSpeedScale = ANIM_SCALEMAX_HIGH - ( ANIM_SCALEMAX_HIGH - ANIM_SCALEMAX_LOW ) * (float)( anim->moveSpeed - ANIM_SPEEDMAX_HIGH ) / (float)( ANIM_SPEEDMAX_LOW - ANIM_SPEEDMAX_HIGH ); + } + } else if ( lf->animSpeedScale > 4.0 ) { + lf->animSpeedScale = 4.0; + } + + } + + if ( lf == ¢->pe.legs ) { + otherAnim = cent->pe.torso.animation; + } else if ( lf == ¢->pe.torso ) { + otherAnim = cent->pe.legs.animation; + } + + // get the next frame based on the animation + if ( !lf->animSpeedScale ) { + // stopped on the ladder, so stay on the same frame + f = lf->frame - anim->firstFrame; + lf->frameTime += anim->frameLerp; // don't wait too long before starting to move again + } else if ( lf->oldAnimationNumber != lf->animationNumber && + ( !anim->moveSpeed || lf->oldFrame < anim->firstFrame || lf->oldFrame >= anim->firstFrame + anim->numFrames ) ) { // Ridah, added this so walking frames don't always get reset to 0, which can happen in the middle of a walking anim, which looks wierd + lf->frameTime = lf->animationTime; // initial lerp + if ( oldAnim && anim->moveSpeed ) { // keep locomotions going continuously + f = ( lf->frame - oldAnim->firstFrame ) + 1; + while ( f < 0 ) { + f += anim->numFrames; + } + } else { + f = 0; + } + } else if ( ( lf == ¢->pe.legs ) && otherAnim && !( anim->flags & ANIMFL_FIRINGANIM ) && ( ( lf->animationNumber & ~ANIM_TOGGLEBIT ) == ( cent->pe.torso.animationNumber & ~ANIM_TOGGLEBIT ) ) && ( !anim->moveSpeed ) ) { + // legs should synch with torso + f = cent->pe.torso.frame - otherAnim->firstFrame; + if ( f >= anim->numFrames || f < 0 ) { + f = 0; // wait at the start for the legs to catch up (assuming they are still in an old anim) + } + lf->frameTime = cent->pe.torso.frameTime; + } else if ( ( lf == ¢->pe.torso ) && otherAnim && !( anim->flags & ANIMFL_FIRINGANIM ) && ( ( lf->animationNumber & ~ANIM_TOGGLEBIT ) == ( cent->pe.legs.animationNumber & ~ANIM_TOGGLEBIT ) ) && ( otherAnim->moveSpeed ) ) { + // torso needs to sync with legs + f = cent->pe.legs.frame - otherAnim->firstFrame; + if ( f >= anim->numFrames || f < 0 ) { + f = 0; // wait at the start for the legs to catch up (assuming they are still in an old anim) + } + lf->frameTime = cent->pe.legs.frameTime; + } else { +#if 0 + lf->frameTime = lf->oldFrameTime + (int)( (float)anim->frameLerp * ( 1.0 / lf->animSpeedScale ) ); + if ( lf->frameTime < cg.time ) { + lf->frameTime = cg.time; + } + + // check for skipping frames (eg. death anims play in slo-mo if low framerate) + if ( cg.time > lf->frameTime && !anim->moveSpeed ) { + f = ( lf->frame - anim->firstFrame ) + 1 + ( cg.time - lf->frameTime ) / anim->frameLerp; + } else { + f = ( lf->frame - anim->firstFrame ) + 1; + } +#else + if ( anim->moveSpeed || lf->frameTime + 1000 < cg.time ) { + lf->frameTime = lf->oldFrameTime + (int)( (float)anim->frameLerp * ( 1.0 / lf->animSpeedScale ) ); + f = ( lf->frame - anim->firstFrame ) + 1; +/* if (lf->frameTime < cg.time) { + lf->frameTime = cg.time; + } +*/ + while ( lf->frameTime < cg.time ) { + lf->frameTime += (int)( (float)anim->frameLerp * ( 1.0 / lf->animSpeedScale ) ); + f++; + while ( f >= anim->numFrames ) + f -= anim->numFrames; + } + } else { // skip frames as required + f = lf->frame - anim->firstFrame; + if ( f < 0 ) { + f = 0; + } + while ( lf->frameTime < cg.time ) { + lf->frameTime += (int)anim->frameLerp; + f++; // f is allowed to go over anim->numFrame, since it gets adjusted later + } + } +#endif + + if ( f < 0 ) { + f = 0; + } + } + //f = ( lf->frameTime - lf->animationTime ) / anim->frameLerp; + if ( f >= anim->numFrames ) { + f -= anim->numFrames; + if ( anim->loopFrames ) { + f %= anim->loopFrames; + f += anim->numFrames - anim->loopFrames; + } else { + f = anim->numFrames - 1; + // the animation is stuck at the end, so it + // can immediately transition to another sequence + lf->frameTime = cg.time; + } + } + lf->frame = anim->firstFrame + f; + if ( cg.time > lf->frameTime ) { + // NOTE TTimo + // show_bug.cgi?id=424 + // is that a related problem? +#if 0 +// disabled, causes bad jolting when oldFrame is updated incorrectly + // Ridah, run the frame again until we move ahead of the current time, fixes walking speeds for zombie + if ( recursion > MAX_LERPFRAME_RECURSION ) { + lf->frameTime = cg.time; + } else { + CG_RunLerpFrameRate( ci, lf, newAnimation, cent, recursion + 1 ); + } +#endif + lf->frameTime = cg.time; + +#if 0 + if ( cg_debugAnim.integer ) { + CG_Printf( "Clamp lf->frameTime\n" ); + } +#endif + } + lf->oldAnimationNumber = lf->animationNumber; + } + + if ( lf->oldFrameTime > cg.time ) { + lf->oldFrameTime = cg.time; + } + // calculate current lerp value + if ( lf->frameTime == lf->oldFrameTime ) { + lf->backlerp = 0; + } else { + lf->backlerp = 1.0 - (float)( cg.time - lf->oldFrameTime ) / ( lf->frameTime - lf->oldFrameTime ); + } +} + +/* +=============== +CG_ClearLerpFrameRate +=============== +*/ +void CG_ClearLerpFrameRate( clientInfo_t *ci, lerpFrame_t *lf, int animationNumber, centity_t *cent ) { + lf->frameTime = lf->oldFrameTime = cg.time; + CG_SetLerpFrameAnimationRate( cent, ci, lf, animationNumber ); + if ( lf->animation ) { + lf->oldFrame = lf->frame = lf->animation->firstFrame; + } +} + +// done. +//------------------------------------------------------------------------------ + + + +/* +=============== +CG_PlayerAnimation +=============== +*/ +static void CG_PlayerAnimation( centity_t *cent, int *legsOld, int *legs, float *legsBackLerp, + int *torsoOld, int *torso, float *torsoBackLerp ) { + clientInfo_t *ci; + int clientNum; + int animIndex, tempIndex; + + clientNum = cent->currentState.clientNum; + + if ( cg_noPlayerAnims.integer ) { + *legsOld = *legs = *torsoOld = *torso = 0; + return; + } + + ci = &cgs.clientinfo[ clientNum ]; + + // default to whatever the legs are currently doing + animIndex = cent->currentState.legsAnim; + + // do the shuffle turn frames locally + if ( !( cent->currentState.eFlags & ( EF_DEAD | EF_NO_TURN_ANIM ) ) && cent->pe.legs.yawing ) { +//CG_Printf("turn: %i\n", cg.time ); + tempIndex = BG_GetAnimScriptAnimation( clientNum, cent->currentState.aiState, ( cent->pe.legs.yawing == SWING_RIGHT ? ANIM_MT_TURNRIGHT : ANIM_MT_TURNLEFT ) ); + if ( tempIndex > -1 ) { + animIndex = tempIndex; + } + } + // run the animation + CG_RunLerpFrameRate( ci, ¢->pe.legs, animIndex, cent, 0 ); + + *legsOld = cent->pe.legs.oldFrame; + *legs = cent->pe.legs.frame; + *legsBackLerp = cent->pe.legs.backlerp; + + CG_RunLerpFrameRate( ci, ¢->pe.torso, cent->currentState.torsoAnim, cent, 0 ); + + *torsoOld = cent->pe.torso.oldFrame; + *torso = cent->pe.torso.frame; + *torsoBackLerp = cent->pe.torso.backlerp; +} + +/* +============================================================================= + +PLAYER ANGLES + +============================================================================= +*/ + +/* +================== +CG_SwingAngles +================== +*/ +static void CG_SwingAngles( float destination, float swingTolerance, float clampTolerance, + float speed, float *angle, qboolean *swinging ) { + float swing; + float move; + float scale; + + if ( !*swinging ) { + // see if a swing should be started + swing = AngleSubtract( *angle, destination ); + if ( swing > swingTolerance || swing < -swingTolerance ) { + *swinging = qtrue; + } + } + + if ( !*swinging ) { + return; + } + + // modify the speed depending on the delta + // so it doesn't seem so linear + swing = AngleSubtract( destination, *angle ); + scale = fabs( swing ); + scale *= 0.05; + if ( scale < 0.5 ) { + scale = 0.5; + } + + // swing towards the destination angle + if ( swing >= 0 ) { + move = cg.frametime * scale * speed; + if ( move >= swing ) { + move = swing; + *swinging = qfalse; + } else { + *swinging = SWING_LEFT; // left + } + *angle = AngleMod( *angle + move ); + } else if ( swing < 0 ) { + move = cg.frametime * scale * -speed; + if ( move <= swing ) { + move = swing; + *swinging = qfalse; + } else { + *swinging = SWING_RIGHT; // right + } + *angle = AngleMod( *angle + move ); + } + + // clamp to no more than tolerance + swing = AngleSubtract( destination, *angle ); + if ( swing > clampTolerance ) { + *angle = AngleMod( destination - ( clampTolerance - 1 ) ); + } else if ( swing < -clampTolerance ) { + *angle = AngleMod( destination + ( clampTolerance - 1 ) ); + } +} + +/* +================= +CG_AddPainTwitch +================= +*/ +static void CG_AddPainTwitch( centity_t *cent, vec3_t torsoAngles ) { + int t; + float f; + int duration; + float direction; + + if ( !cent->pe.animSpeed ) { + // we need to inititialize this stuff + cent->pe.painAnimLegs = -1; + cent->pe.painAnimTorso = -1; + cent->pe.animSpeed = 1.0; + } + + if ( cent->currentState.eFlags & EF_DEAD ) { + cent->pe.painAnimLegs = -1; + cent->pe.painAnimTorso = -1; + cent->pe.animSpeed = 1.0; + return; + } + + // special pain anims for AI + if ( cent->currentState.aiChar ) { + if ( cent->pe.painAnimTorso >= 0 ) { + animation_t *anim; + clientInfo_t *ci; + + ci = &cgs.clientinfo[ cent->currentState.number ]; + anim = &ci->modelInfo->animations[ cent->pe.painAnimTorso ]; + + // play the current animation + if ( cent->pe.torso.frame != cent->pe.torso.oldFrame || cent->pe.torso.frame != anim->firstFrame + anim->numFrames - 1 ) { + if ( cent->pe.painAnimLegs >= 0 ) { + cent->currentState.legsAnim = cent->pe.painAnimLegs; + } + cent->currentState.torsoAnim = cent->pe.painAnimTorso; + } else { // end it + cent->pe.painAnimLegs = -1; + cent->pe.painAnimTorso = -1; + cent->pe.animSpeed = 1.0; + } + } + + return; + } + + if ( cent->pe.painDuration ) { + duration = cent->pe.painDuration; + } else { + duration = PAIN_TWITCH_TIME; + } + direction = (float)duration * 0.085; + if ( direction > 30 ) { + direction = 30; + } + if ( direction < 10 ) { + direction = 10; + } + direction *= (float)( cent->pe.painDirection * 2 ) - 1; + + t = cg.time - cent->pe.painTime; + if ( t >= duration ) { + return; + } + + if ( cent->currentState.clientNum && cgs.gametype == GT_SINGLE_PLAYER ) { + #define FADEIN_RATIO 0.25 + #define FADEOUT_RATIO ( 1.0 - FADEIN_RATIO ) + f = (float)t / duration; + if ( f < FADEIN_RATIO ) { + torsoAngles[ROLL] += ( 0.5 * direction * ( f * ( 1.0 / FADEIN_RATIO ) ) ); + torsoAngles[PITCH] -= ( fabs( direction ) * ( f * ( 1.0 / FADEIN_RATIO ) ) ); + torsoAngles[YAW] += ( direction * ( f * ( 1.0 / FADEIN_RATIO ) ) ); + } else { + torsoAngles[ROLL] += ( 0.5 * direction * ( 1.0 - ( f - FADEIN_RATIO ) ) * ( 1.0 / FADEOUT_RATIO ) ); + torsoAngles[PITCH] -= ( fabs( direction ) * ( 1.0 - ( f - FADEIN_RATIO ) ) * ( 1.0 / FADEOUT_RATIO ) ); + torsoAngles[YAW] += ( direction * ( 1.0 - ( f - FADEIN_RATIO ) ) * ( 1.0 / FADEOUT_RATIO ) ); + } + } else { // fast, Q3 style + f = 1.0 - (float)t / duration; + if ( cent->pe.painDirection ) { + torsoAngles[ROLL] += 20 * f; + } else { + torsoAngles[ROLL] -= 20 * f; + } + } +} + +/* +================ +CG_IdleHeadMovement +================ +*/ +static void CG_IdleHeadMovement( centity_t *cent, const vec3_t torsoAngles, vec3_t headAngles ) { + const float angleSpeedMax = 60; + const float angleSpeedAccel = 40; + //const float diffThreshold = 50; // don't yaw from torso beyond this in any axis + const float diffRandMax = 30; + const float stopTime = 3000; + + int i; + + if ( ( cent->currentState.eFlags & EF_DEAD ) || !( cent->currentState.eFlags & EF_HEADLOOK ) ) { + // center + VectorClear( cent->pe.headLookIdeal ); + cent->pe.headLookStopTime = 0; + cent->pe.headLookSpeed = 100; // hurry back to normal + } else if ( !cent->pe.headLookSpeedMax && cent->pe.headLookStopTime < cg.time ) { + // need new ideal angles + VectorSet( cent->pe.headLookIdeal, + diffRandMax * ( -0.25 + crandom() ) * 0.25, // pitch + diffRandMax * crandom(), // yaw + 0 ); // roll + cent->pe.headLookSpeedMax = angleSpeedMax; // * (0.5 + 0.5*random()); + cent->pe.headLookStopTime = cg.time - 1; + } + + // move towards ideal position + if ( cent->pe.headLookStopTime < cg.time && !VectorCompare( cent->pe.headLookIdeal, cent->pe.headLookOffset ) ) { + // slow down as we get closer + if ( fabs( cent->pe.headLookOffset[YAW] - cent->pe.headLookIdeal[YAW] ) < angleSpeedMax * 1.2 ) { + cent->pe.headLookSpeedMax = angleSpeedMax * ( 0.1 + 0.9 * ( fabs( cent->pe.headLookOffset[YAW] - cent->pe.headLookIdeal[YAW] ) / ( angleSpeedMax * 1.2 ) ) ); + } + // accelerate angle speed + if ( cent->pe.headLookSpeed < cent->pe.headLookSpeedMax ) { + cent->pe.headLookSpeed += angleSpeedAccel * ( 0.001 * cg.frametime ); + if ( cent->pe.headLookSpeed > cent->pe.headLookSpeedMax ) { + cent->pe.headLookSpeed = cent->pe.headLookSpeedMax; + } + } else if ( cent->pe.headLookSpeed > cent->pe.headLookSpeedMax ) { + cent->pe.headLookSpeed -= angleSpeedAccel * ( 0.001 * cg.frametime ); + if ( cent->pe.headLookSpeed < cent->pe.headLookSpeedMax ) { + cent->pe.headLookSpeed = cent->pe.headLookSpeedMax; + } + } + // move towards the ideal angles + for ( i = 0; i < 3; i++ ) { + if ( cent->pe.headLookOffset[i] < cent->pe.headLookIdeal[i] ) { + cent->pe.headLookOffset[i] += cent->pe.headLookSpeed * 0.001 * cg.frametime; + if ( cent->pe.headLookOffset[i] > cent->pe.headLookIdeal[i] ) { + cent->pe.headLookOffset[i] = cent->pe.headLookIdeal[i]; + } + } else if ( cent->pe.headLookOffset[i] > cent->pe.headLookIdeal[i] ) { + cent->pe.headLookOffset[i] -= cent->pe.headLookSpeed * 0.001 * cg.frametime; + if ( cent->pe.headLookOffset[i] < cent->pe.headLookIdeal[i] ) { + cent->pe.headLookOffset[i] = cent->pe.headLookIdeal[i]; + } + } + } + // if we made it, stop here for a bit + if ( VectorCompare( cent->pe.headLookIdeal, cent->pe.headLookOffset ) ) { + cent->pe.headLookStopTime = cg.time + (int)( stopTime * ( 0.5 + 0.5 * random() ) ); + } + // randomly choose a new destination before reaching ideal angles + if ( cent->pe.headLookStopTime % 2 && rand() % ( cg.time - cent->pe.headLookStopTime ) > 700 ) { + cent->pe.headLookSpeedMax = 0; + } + } else { + cent->pe.headLookSpeedMax = 0; + cent->pe.headLookSpeed = 0; // accelerate when resuming idle movement + } +/* + // make sure these angles don't place us too far from the torso angles + for (i=0; i<3; i++) { + if (fabs(cent->pe.headLookOffset[i] + headAngles[i] - torsoAngles[i]) > diffThreshold) { + if (cent->pe.headLookOffset[i] + headAngles[i] - torsoAngles[i] > 0) + cent->pe.headLookOffset[i] = diffThreshold - (headAngles[i] - torsoAngles[i]); + else + cent->pe.headLookOffset[i] = -diffThreshold - (headAngles[i] - torsoAngles[i]); + } + } +*/ + // finally, add the headLookOffset to the head angles + VectorAdd( headAngles, cent->pe.headLookOffset, headAngles ); +} + +/* +=============== +CG_PlayerAngles + +Handles seperate torso motion + + legs pivot based on direction of movement + + head always looks exactly at cent->lerpAngles + + if motion < 20 degrees, show in head only + if < 45 degrees, also show in torso +=============== +*/ +static void CG_PlayerAngles( centity_t *cent, vec3_t legs[3], vec3_t torso[3], vec3_t head[3] ) { + vec3_t legsAngles, torsoAngles, headAngles; + float dest; + //static int movementOffsets[8] = { 0, 22, 45, -22, 0, 22, -45, -22 }; // TTimo: unused + vec3_t velocity; + float speed; + float clampTolerance; + int legsSet, torsoSet; + clientInfo_t *ci; + ci = &cgs.clientinfo[ cent->currentState.number ]; + + // special case (female zombie while climbing wall) + if ( cent->currentState.eFlags & EF_FORCED_ANGLES ) { + // torso & legs parts should turn to face the given angles + VectorCopy( cent->lerpAngles, legsAngles ); + AnglesToAxis( legsAngles, legs ); + AnglesToAxis( vec3_origin, torso ); + AnglesToAxis( vec3_origin, head ); + return; + } + + legsSet = cent->currentState.legsAnim & ~ANIM_TOGGLEBIT; + torsoSet = cent->currentState.torsoAnim & ~ANIM_TOGGLEBIT; + + VectorCopy( cent->lerpAngles, headAngles ); + headAngles[YAW] = AngleMod( headAngles[YAW] ); + VectorClear( legsAngles ); + VectorClear( torsoAngles ); + + // --------- yaw ------------- + + // allow yaw to drift a bit, unless these conditions don't allow them + if ( !( BG_GetConditionValue( cent->currentState.number, ANIM_COND_MOVETYPE, qfalse ) & ( ( 1 << ANIM_MT_IDLE ) | ( 1 << ANIM_MT_IDLECR ) ) )/* + || (BG_GetConditionValue( cent->currentState.number, ANIM_COND_MOVETYPE, qfalse ) & ((1<pe.torso.yawing = qtrue; // always center + cent->pe.torso.pitching = qtrue; // always center + cent->pe.legs.yawing = qtrue; // always center + + // if firing, make sure torso and head are always aligned + } else if ( BG_GetConditionValue( cent->currentState.number, ANIM_COND_FIRING, qtrue ) ) { + + cent->pe.torso.yawing = qtrue; // always center + cent->pe.torso.pitching = qtrue; // always center + + } + + // adjust legs for movement dir + if ( cent->currentState.eFlags & EF_DEAD ) { + // don't let dead bodies twitch + legsAngles[YAW] = headAngles[YAW]; + torsoAngles[YAW] = headAngles[YAW]; + } else { + legsAngles[YAW] = headAngles[YAW] + cent->currentState.angles2[YAW]; + + if ( cent->currentState.eFlags & EF_NOSWINGANGLES ) { + legsAngles[YAW] = torsoAngles[YAW] = headAngles[YAW]; // always face firing direction + clampTolerance = 40; + } else if ( !( cent->currentState.eFlags & EF_FIRING ) && !( cent->currentState.eFlags & EF_RECENTLY_FIRING ) ) { + torsoAngles[YAW] = headAngles[YAW] + 0.35 * cent->currentState.angles2[YAW]; + clampTolerance = 60; + } else { // must be firing + torsoAngles[YAW] = headAngles[YAW]; // always face firing direction + //if (fabs(cent->currentState.angles2[YAW]) > 30) + // legsAngles[YAW] = headAngles[YAW]; + clampTolerance = 40; + } + + // torso + if ( !cent->pe.torso.yawing ) { + CG_SwingAngles( torsoAngles[YAW], 30, clampTolerance, 0.5 * cg_swingSpeed.value, ¢->pe.torso.yawAngle, ¢->pe.torso.yawing ); + } else if ( !( cent->currentState.eFlags & EF_FIRING ) && !( cent->currentState.eFlags & EF_RECENTLY_FIRING ) ) { + // not firing + CG_SwingAngles( torsoAngles[YAW], 0, clampTolerance, 0.5 * cg_swingSpeed.value, ¢->pe.torso.yawAngle, ¢->pe.torso.yawing ); + } else { // firing + CG_SwingAngles( torsoAngles[YAW], 0, clampTolerance, 4.0 * cg_swingSpeed.value, ¢->pe.torso.yawAngle, ¢->pe.torso.yawing ); + } + + // if the legs are yawing (facing heading direction), allow them to rotate a bit, so we don't keep calling + // the legs_turn animation while an AI is firing, and therefore his angles will be randomizing according to their accuracy + + clampTolerance = 90; + + if ( BG_GetConditionValue( ci->clientNum, ANIM_COND_MOVETYPE, qfalse ) & ( 1 << ANIM_MT_IDLE ) ) { + if ( cent->pe.legs.yawing ) { + CG_SwingAngles( legsAngles[YAW], 0, clampTolerance, 0.75 * cg_swingSpeed.value, ¢->pe.legs.yawAngle, ¢->pe.legs.yawing ); + } else { + cent->pe.legs.yawing = qfalse; // set it if they really need to swing + CG_SwingAngles( legsAngles[YAW], 50, clampTolerance, 0.75 * cg_swingSpeed.value, ¢->pe.legs.yawAngle, ¢->pe.legs.yawing ); + } + } else + //if ( BG_GetConditionValue( ci->clientNum, ANIM_COND_MOVETYPE, qfalse ) & ((1<clientNum, legsSet ), "strafe" ) ) { + cent->pe.legs.yawing = qfalse; // set it if they really need to swing + legsAngles[YAW] = headAngles[YAW]; + CG_SwingAngles( legsAngles[YAW], 0, clampTolerance, cg_swingSpeed.value, ¢->pe.legs.yawAngle, ¢->pe.legs.yawing ); + } else + if ( cent->pe.legs.yawing ) { + CG_SwingAngles( legsAngles[YAW], 0, clampTolerance, cg_swingSpeed.value, ¢->pe.legs.yawAngle, ¢->pe.legs.yawing ); + } else + { + CG_SwingAngles( legsAngles[YAW], 50, clampTolerance, cg_swingSpeed.value, ¢->pe.legs.yawAngle, ¢->pe.legs.yawing ); + } + + torsoAngles[YAW] = cent->pe.torso.yawAngle; + legsAngles[YAW] = cent->pe.legs.yawAngle; + } + + // --------- pitch ------------- + + // only show a fraction of the pitch angle in the torso + if ( headAngles[PITCH] > 180 ) { + dest = ( -360 + headAngles[PITCH] ) * 0.75; + } else { + dest = headAngles[PITCH] * 0.75; + } + CG_SwingAngles( dest, 15, 30, 0.1, ¢->pe.torso.pitchAngle, ¢->pe.torso.pitching ); + torsoAngles[PITCH] = cent->pe.torso.pitchAngle; + + // --------- roll ------------- + + + // lean towards the direction of travel + VectorCopy( cent->currentState.pos.trDelta, velocity ); + speed = VectorNormalize( velocity ); + if ( speed ) { + vec3_t axis[3]; + float side; + + speed *= 0.05; + + AnglesToAxis( legsAngles, axis ); + side = speed * DotProduct( velocity, axis[1] ); + legsAngles[ROLL] -= side; + + side = speed * DotProduct( velocity, axis[0] ); + legsAngles[PITCH] += side; + } + + // pain twitch + CG_AddPainTwitch( cent, torsoAngles ); + + // RF, add the head movement if flag is set for looking around + CG_IdleHeadMovement( cent, torsoAngles, headAngles ); + + // pull the angles back out of the hierarchial chain + AnglesSubtract( headAngles, torsoAngles, headAngles ); + AnglesSubtract( torsoAngles, legsAngles, torsoAngles ); + AnglesToAxis( legsAngles, legs ); + AnglesToAxis( torsoAngles, torso ); + AnglesToAxis( headAngles, head ); +} + + +//========================================================================== + +/* +=============== +CG_HasteTrail +=============== +*/ +static void CG_HasteTrail( centity_t *cent ) { + localEntity_t *smoke; + vec3_t origin; + int anim; + + if ( cent->trailTime > cg.time ) { + return; + } + anim = cent->pe.legs.animationNumber & ~ANIM_TOGGLEBIT; +// RF, this is all broken by scripting system +// if ( anim != LEGS_RUN && anim != LEGS_BACK ) { +// return; +// } + + cent->trailTime += 100; + if ( cent->trailTime < cg.time ) { + cent->trailTime = cg.time; + } + + VectorCopy( cent->lerpOrigin, origin ); + origin[2] -= 16; + + smoke = CG_SmokePuff( origin, vec3_origin, + 8, + 1, 1, 1, 1, + 500, + cg.time, + 0, + 0, + cgs.media.hastePuffShader ); + + // use the optimized local entity add + smoke->leType = LE_SCALE_FADE; +} + + +//----(SA) added and modified from missionpack +/* +=============== +CG_BreathPuffs +=============== +*/ +static void CG_BreathPuffs( centity_t *cent, refEntity_t *head ) { + clientInfo_t *ci; + vec3_t up, forward; + int contents; + + vec3_t mang, morg, maxis[3]; //, mang; + + ci = &cgs.clientinfo[ cent->currentState.number ]; + + if ( !cg_enableBreath.integer ) { + return; + } + + if ( cent->currentState.number == cg.snap->ps.clientNum && !cg.renderingThirdPerson ) { + return; + } + if ( cent->currentState.eFlags & EF_DEAD ) { + return; + } + + // allow cg_enableBreath to force everyone to have breath + if ( cg_enableBreath.integer == 1 ) { + if ( !( cent->currentState.eFlags & EF_BREATH ) ) { + return; + } + } + + contents = trap_CM_PointContents( head->origin, 0 ); + if ( contents & ( CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ) ) { + return; + } + if ( ci->breathPuffTime > cg.time ) { + return; + } + + CG_GetOriginForTag( cent, head, "tag_mouth", 0, morg, maxis ); + AxisToAngles( maxis, mang ); + AngleVectors( mang, forward, NULL, up ); + + //push the origin out a tad so it's not right in the guys face (tad==4) + VectorMA( morg, 4, forward, morg ); + + forward[0] = up[0] * 8 + forward[0] * 5; + forward[1] = up[1] * 8 + forward[1] * 5; + forward[2] = up[2] * 8 + forward[2] * 5; + + CG_SmokePuff( morg, forward, 4, 1, 1, 1, 0.5f, 2000, cg.time, cg.time + 400, 0, cgs.media.shotgunSmokePuffShader ); + + + ci->breathPuffTime = cg.time + 3000 + random() * 1000; + +} + +//----(SA) end + +/* +=============== +CG_TrailItem +=============== +*/ +static void CG_TrailItem( centity_t *cent, qhandle_t hModel ) { + refEntity_t ent; + vec3_t angles; + vec3_t axis[3]; + + VectorCopy( cent->lerpAngles, angles ); + angles[PITCH] = 0; + angles[ROLL] = 0; + AnglesToAxis( angles, axis ); + + memset( &ent, 0, sizeof( ent ) ); + // DHM - Nerve :: adjusted values + VectorMA( cent->lerpOrigin, -4, axis[0], ent.origin ); + ent.origin[2] += 36; + VectorScale( cg.autoAxis[0], 0.75, ent.axis[0] ); + VectorScale( cg.autoAxis[1], 0.75, ent.axis[1] ); + VectorScale( cg.autoAxis[2], 0.75, ent.axis[2] ); + ent.hModel = hModel; + trap_R_AddRefEntityToScene( &ent ); +} + + +/* +=============== +CG_PlayerPowerups +=============== +*/ +static void CG_PlayerPowerups( centity_t *cent ) { + int powerups; + + if ( cent->pe.teslaDamagedTime > cg.time - 400 ) { + trap_R_AddLightToScene( cent->lerpOrigin, 128 + 128 * sin( cg.time * cg.time ), 0.2, 0.6, 1, 0 ); + } + + // RF, AI don't use these effects, they are generally added manually by the game + if ( cent->currentState.aiChar ) { + return; + } + +//----(SA) test stuff leave in, but comment out +// if(cg_forceModel.integer) { +// vec3_t orig, forward, li; +// trace_t trace; +// +// AngleVectors(cg.refdefViewAngles, forward, NULL, NULL); +// VectorNormalize(forward); // just in case +// VectorCopy(cent->lerpOrigin, orig); +// orig[2]+=cg.predictedPlayerState.viewheight; +// VectorMA(orig, 1000, forward, li); +// CG_Trace(&trace, orig, NULL, NULL, li, -1, MASK_SHOT); +// VectorMA(trace.endpos, -5, forward, li); +// trap_R_AddLightToScene( li, 100 + 100*trace.fraction, 1, 1, 1, 1 ); +// } +//----(SA) end + + powerups = cent->currentState.powerups; + if ( !powerups ) { + return; + } + + // quad gives a dlight + if ( powerups & ( 1 << PW_QUAD ) ) { + trap_R_AddLightToScene( cent->lerpOrigin, 200 + ( rand() & 31 ), 0.2, 0.2, 1, 0 ); + } + + // flight plays a looped sound +// if ( powerups & ( 1 << PW_FLIGHT ) ) { +// trap_S_AddLoopingSound( cent->currentState.number, cent->lerpOrigin, vec3_origin, cgs.media.flightSound, 255 ); +// } + + // redflag + if ( powerups & ( 1 << PW_REDFLAG ) ) { + CG_TrailItem( cent, cgs.media.redFlagModel ); + trap_R_AddLightToScene( cent->lerpOrigin, 200 + ( rand() & 31 ), 1, 0.2, 0.2, 0 ); + } + + // blueflag + if ( powerups & ( 1 << PW_BLUEFLAG ) ) { + CG_TrailItem( cent, cgs.media.blueFlagModel ); + trap_R_AddLightToScene( cent->lerpOrigin, 200 + ( rand() & 31 ), 0.2, 0.2, 1, 0 ); + } + + // haste leaves smoke trails + if ( powerups & ( 1 << PW_HASTE ) ) { + CG_HasteTrail( cent ); + } +} + + +/* +=============== +CG_PlayerFloatSprite + +Float a sprite over the player's head +DHM - Nerve :: added height parameter +=============== +*/ +static void CG_PlayerFloatSprite( centity_t *cent, qhandle_t shader, int height ) { + int rf; + refEntity_t ent; + + if ( cent->currentState.number == cg.snap->ps.clientNum && !cg.renderingThirdPerson ) { + rf = RF_THIRD_PERSON; // only show in mirrors + } else { + rf = 0; + } + + memset( &ent, 0, sizeof( ent ) ); + VectorCopy( cent->lerpOrigin, ent.origin ); + ent.origin[2] += height; // DHM - Nerve :: was '48' + ent.reType = RT_SPRITE; + ent.customShader = shader; + ent.radius = 10; + ent.renderfx = rf; + ent.shaderRGBA[0] = 255; + ent.shaderRGBA[1] = 255; + ent.shaderRGBA[2] = 255; + ent.shaderRGBA[3] = 255; + trap_R_AddRefEntityToScene( &ent ); +} + + + +/* +=============== +CG_PlayerSprites + +Float sprites over the player's head +=============== +*/ +static void CG_PlayerSprites( centity_t *cent ) { + int team; + + if ( cg_showAIState.integer && cent->currentState.aiChar ) { + CG_PlayerFloatSprite( cent, cgs.media.aiStateShaders[cent->currentState.aiState], 48 ); + return; + } + + if ( cent->currentState.eFlags & EF_CONNECTION ) { + CG_PlayerFloatSprite( cent, cgs.media.connectionShader, 48 ); + return; + } + + // DHM - Nerve :: If this client is a medic, draw a 'revive' icon over + // dead players that are not in limbo yet. + team = cgs.clientinfo[ cent->currentState.clientNum ].team; + if ( cgs.gametype == GT_WOLF && ( cent->currentState.eFlags & EF_DEAD ) + && cent->currentState.number == cent->currentState.clientNum + && cg.snap->ps.stats[ STAT_PLAYER_CLASS ] == PC_MEDIC + && cg.snap->ps.persistant[PERS_TEAM] == team ) { + +// CG_PlayerFloatSprite( cent, cgs.media.medicReviveShader, 8 ); //----(SA) commented out from MP + return; + } + + // DHM - Nerve :: not using, gives away position if chatting to coordinate attack +// if ( cent->currentState.eFlags & EF_TALK ) { +// CG_PlayerFloatSprite( cent, cgs.media.balloonShader, 48 ); +// return; +// } + +//----(SA) commented out +// if ( cent->currentState.eFlags & EF_AWARD_IMPRESSIVE ) { +// CG_PlayerFloatSprite( cent, cgs.media.medalImpressive, 48 ); +// return; +// } + +//----(SA) commented out +// if ( cent->currentState.eFlags & EF_AWARD_EXCELLENT ) { +// CG_PlayerFloatSprite( cent, cgs.media.medalExcellent, 48 ); +// return; +// } + +//----(SA) commented out +// if ( cent->currentState.eFlags & EF_AWARD_GAUNTLET ) { +// CG_PlayerFloatSprite( cent, cgs.media.medalGauntlet, 48 ); +// return; +// } +// DHM - Nerve :: Not using friendly sprites in GT_WOLF + if ( cgs.gametype != GT_WOLF && cgs.gametype >= GT_TEAM && + !( cent->currentState.eFlags & EF_DEAD ) && + cg.snap->ps.persistant[PERS_TEAM] == team ) { + + CG_PlayerFloatSprite( cent, cgs.media.friendShader, 48 ); + return; + } +} + +/* +=============== +CG_PlayerShadow + +Returns the Z component of the surface being shadowed + + should it return a full plane instead of a Z? +=============== +*/ +#define SHADOW_DISTANCE 64 +typedef struct { + char *tagname; + float size; + float maxdist; + float maxalpha; + qhandle_t shader; +} shadowPart_t; + +static qboolean CG_PlayerShadow( centity_t *cent, float *shadowPlane ) { + vec3_t end; + trace_t trace; + float alpha, dist, distfade, scale, dot; + int tagIndex, subIndex; + vec3_t origin, angles, axis[3]; + #define ZOFS 6.0 + #define SHADOW_MIN_DIST 250.0 + #define SHADOW_MAX_DIST 512.0 + shadowPart_t shadowParts[] = { + {"tag_footleft", 10, 4, 0.5, 0}, + {"tag_footright", 10, 4, 0.5, 0}, + {"tag_torso", 18, 96, 0.4, 0}, + {NULL, 0} + }; + + shadowParts[0].shader = cgs.media.shadowFootShader; //DAJ pulled out of initliization + shadowParts[1].shader = cgs.media.shadowFootShader; + shadowParts[2].shader = cgs.media.shadowTorsoShader; + *shadowPlane = 0; + + if ( cg_shadows.integer == 0 ) { + return qfalse; + } + + // no shadows when invisible + if ( cent->currentState.powerups & ( 1 << PW_INVIS ) ) { + return qfalse; + } + + // send a trace down from the player to the ground + VectorCopy( cent->lerpOrigin, end ); + end[2] -= SHADOW_DISTANCE; + + trap_CM_BoxTrace( &trace, cent->lerpOrigin, end, NULL, NULL, 0, MASK_PLAYERSOLID ); + + // no shadow if too high + if ( trace.fraction == 1.0 ) { + return qfalse; + } + + *shadowPlane = trace.endpos[2] + 1; + + if ( cg_shadows.integer != 1 ) { // no mark for stencil or projection shadows + return qtrue; + } + + // no shadows when dead + if ( cent->currentState.eFlags & EF_DEAD ) { + return qfalse; + } + + if ( cg.snap && cent->currentState.number != cg.snap->ps.clientNum ) { + // scale shadow according to bounding box size + scale = ( cent->currentState.solid & 255 ); + scale /= 18.0; + } else { + scale = 1.0; + } + + // fade the shadow out with height + alpha = 1.0 - trace.fraction; + + // if zooming, dont draw shadows if we're not looking in their direction + if ( cg.predictedPlayerState.eFlags & EF_ZOOMING ) { + VectorSubtract( cent->lerpOrigin, cg.refdef.vieworg, end ); + VectorNormalize( end ); + dot = DotProduct( cg.refdef.viewaxis[0], end ); + if ( dot < 0.96 ) { + return qfalse; + } else if ( dot < 0.98 ) { + alpha *= ( dot - 0.94 ) / ( 0.97 - 0.94 ); + } + } + + // if the torso is below the ground, dont draw shadows + if ( CG_GetOriginForTag( cent, ¢->pe.legsRefEnt, "tag_torso", 0, origin, axis ) ) { + if ( origin[2] - cent->lerpOrigin[2] < -26 ) { + return qfalse; + } + } + + // add the mark as a temporary, so it goes directly to the renderer + // without taking a spot in the cg_marks array + dist = VectorDistance( cent->lerpOrigin, cg.refdef.vieworg ); + if ( dist > SHADOW_MIN_DIST ) { + if ( dist < SHADOW_MAX_DIST ) { + distfade = ( dist - SHADOW_MIN_DIST ) / ( SHADOW_MAX_DIST - SHADOW_MIN_DIST ); + } else { + if ( dist > SHADOW_MAX_DIST * 2 ) { + return qfalse; + } else { // fade out + distfade = 1.0 - ( ( dist - SHADOW_MAX_DIST ) / SHADOW_MAX_DIST ); + } + } + alpha *= distfade; + CG_ImpactMark( cgs.media.shadowTorsoShader, trace.endpos, trace.plane.normal, + 0, alpha,alpha,alpha,1, qfalse, 18 * scale, qtrue, -1 ); + } else { + distfade = 0.0; + } + + if ( dist < SHADOW_MAX_DIST ) { // show more detail + // now add shadows for the various body parts + for ( tagIndex = 0; shadowParts[tagIndex].tagname; tagIndex++ ) { + // grab each tag with this name + for ( subIndex = 0; ( subIndex = CG_GetOriginForTag( cent, ¢->pe.legsRefEnt, shadowParts[tagIndex].tagname, subIndex, origin, axis ) ) >= 0; subIndex++ ) { + // move the shadow center forward + VectorMA( origin, 4, axis[0], origin ); + // project it onto the shadow plane + if ( origin[2] < *shadowPlane ) { + origin[2] = *shadowPlane; + } + alpha = 1.0 - ( ( origin[2] - ( *shadowPlane + ZOFS ) ) / shadowParts[tagIndex].maxdist ); + if ( alpha < 0 ) { + continue; + } + if ( alpha > shadowParts[tagIndex].maxalpha ) { + alpha = shadowParts[tagIndex].maxalpha; + } + alpha *= ( 1.0 - distfade ); + + origin[2] = *shadowPlane; + + AxisToAngles( axis, angles ); + + CG_ImpactMark( shadowParts[tagIndex].shader, origin, trace.plane.normal, + angles[YAW] /*cent->pe.legs.yawAngle*/, alpha,alpha,alpha,1, qfalse, shadowParts[tagIndex].size * scale, qtrue, -1 ); + } + } + } + + return qtrue; +} + + + +/* +=============== +CG_PlayerSplash + +Draw a mark at the water surface +=============== +*/ +static void CG_PlayerSplash( centity_t *cent ) { + vec3_t start, end; + trace_t trace; + int contents; + polyVert_t verts[4]; + + if ( !cg_shadows.integer ) { + return; + } + + VectorCopy( cent->lerpOrigin, end ); + end[2] -= 24; + + // if the feet aren't in liquid, don't make a mark + // this won't handle moving water brushes, but they wouldn't draw right anyway... + contents = trap_CM_PointContents( end, 0 ); + if ( !( contents & ( CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ) ) ) { + return; + } + + VectorCopy( cent->lerpOrigin, start ); + start[2] += 32; + + // if the head isn't out of liquid, don't make a mark + contents = trap_CM_PointContents( start, 0 ); + if ( contents & ( CONTENTS_SOLID | CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ) ) { + return; + } + + // trace down to find the surface + trap_CM_BoxTrace( &trace, start, end, NULL, NULL, 0, ( CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ) ); + + if ( trace.fraction == 1.0 ) { + return; + } + + // create a mark polygon + VectorCopy( trace.endpos, verts[0].xyz ); + verts[0].xyz[0] -= 32; + verts[0].xyz[1] -= 32; + verts[0].st[0] = 0; + verts[0].st[1] = 0; + verts[0].modulate[0] = 255; + verts[0].modulate[1] = 255; + verts[0].modulate[2] = 255; + verts[0].modulate[3] = 255; + + VectorCopy( trace.endpos, verts[1].xyz ); + verts[1].xyz[0] -= 32; + verts[1].xyz[1] += 32; + verts[1].st[0] = 0; + verts[1].st[1] = 1; + verts[1].modulate[0] = 255; + verts[1].modulate[1] = 255; + verts[1].modulate[2] = 255; + verts[1].modulate[3] = 255; + + VectorCopy( trace.endpos, verts[2].xyz ); + verts[2].xyz[0] += 32; + verts[2].xyz[1] += 32; + verts[2].st[0] = 1; + verts[2].st[1] = 1; + verts[2].modulate[0] = 255; + verts[2].modulate[1] = 255; + verts[2].modulate[2] = 255; + verts[2].modulate[3] = 255; + + VectorCopy( trace.endpos, verts[3].xyz ); + verts[3].xyz[0] += 32; + verts[3].xyz[1] -= 32; + verts[3].st[0] = 1; + verts[3].st[1] = 0; + verts[3].modulate[0] = 255; + verts[3].modulate[1] = 255; + verts[3].modulate[2] = 255; + verts[3].modulate[3] = 255; + + trap_R_AddPolyToScene( cgs.media.wakeMarkShader, 4, verts ); +} + +//========================================================================== +// Zombie Effects + +/* +============== +CG_SpawnZombieSpirit +============== +*/ +void CG_SpawnZombieSpirit( vec3_t origin, vec3_t vel, int trailHead, int ownerNum, refEntity_t *oldrefent, int trailLife, int idealWidth ) { + localEntity_t *le; + refEntity_t *re; + + le = CG_AllocLocalEntity(); + re = &le->refEntity; + + *re = *oldrefent; + + le->leType = LE_ZOMBIE_SPIRIT; + le->startTime = cg.time - 5000; // set it back so we dont fade it in like the "end" spirits + le->endTime = cg.time + 5000; + + le->pos.trType = TR_LINEAR; + le->pos.trTime = cg.time; + VectorCopy( origin, le->pos.trBase ); + VectorCopy( vel, le->pos.trDelta ); + + le->effectWidth = trailLife; + le->radius = idealWidth; + le->lastTrailTime = cg.time; + le->headJuncIndex = trailHead; + le->loopingSound = cgs.media.zombieSpiritLoopSound; + + le->ownerNum = ownerNum; + + re->fadeStartTime = le->endTime - 2000; + re->fadeEndTime = le->endTime; +} + +/* +============== +CG_AddZombieSpiritEffect +============== +*/ +void CG_AddZombieSpiritEffect( centity_t *cent ) { + const int trailLife = 600; + const float idealWidth = 14.0; + const int fadeInTime = 1200; + const int fadeOutTime = 2000; + const int minRotationTime = 3000; + const int maxRotationTime = 4000; + const int minRadiusCycleTime = 2400; + const int maxRadiusCycleTime = 2900; + const int zCycleTime = 3000; + + const float minDist = 16; + const float maxDist = 40; + const float fadeDist = 4; + + const int sndIntervalMin = 300; + const int sndIntervalMax = 1400; + const int sndDelay = 0; + //const int sndDuration = 99999;//1500; // TTimo: init + + const int step = 50; + + int i, t; + vec3_t v,p[MAX_ZOMBIE_SPIRITS], ang; + float fadeRatio, alpha, radius; + int rotationTime, radiusCycleTime; + trace_t trace; + refEntity_t refent; + + qboolean active = qfalse; + + static int lastSpiritRelease; + + if ( cent->currentState.aiChar != AICHAR_ZOMBIE ) { + return; + } + + // sanity check the server time to make sure we don't start the effect + // to early, whilst reloading a savegame or something + if ( cg.time < cent->currentState.effect1Time ) { + return; + } + + if ( cent->currentState.eFlags & EF_MONSTER_EFFECT ) { + + if ( !cent->pe.cueZombieSpirit ) { + // starting a new effect + cent->pe.cueZombieSpirit = qtrue; + cent->pe.zombieSpiritStartTime = cent->currentState.effect1Time; + cent->pe.lastZombieSpirit = cg.time; + cent->pe.nextZombieSpiritSound = cg.time + sndDelay; + for ( i = 0; i < MAX_ZOMBIE_SPIRITS; i++ ) { + cent->pe.zombieSpiritTrailHead[i] = -1; + cent->pe.zombieSpiritRotationTimes[i] = minRotationTime + ( random() * ( maxRotationTime - minRotationTime ) ); + cent->pe.zombieSpiritRadiusCycleTimes[i] = minRadiusCycleTime + ( random() * ( maxRadiusCycleTime - minRadiusCycleTime ) ); + cent->pe.zombieSpiritStartTimes[i] = cent->currentState.effect1Time; + } + } + cent->pe.zombieSpiritEndTime = cg.time; + + } else { + + // if running another effect, dont mess with its variables + if ( cent->currentState.eFlags & EF_MONSTER_EFFECT3 || cent->currentState.effect1Time < cent->currentState.effect3Time ) { + return; + } + + if ( !cent->pe.zombieSpiritEndTime ) { + return; + } + // clear the flag, and let the effect fade itself out + cent->pe.cueZombieSpirit = qfalse; + } + + for ( t = cent->pe.lastZombieSpirit + step; t <= cg.time; t += step ) { + + // add the spirits + for ( i = 0; i < MAX_ZOMBIE_SPIRITS; i++ ) { + + if ( cent->pe.zombieSpiritTrailHead[i] < -1 ) { + // spirit has gone, create a new one + cent->pe.zombieSpiritTrailHead[i] = -1; + cent->pe.zombieSpiritRotationTimes[i] = minRotationTime + ( random() * ( maxRotationTime - minRotationTime ) ); + cent->pe.zombieSpiritRadiusCycleTimes[i] = minRadiusCycleTime + ( random() * ( maxRadiusCycleTime - minRadiusCycleTime ) ); + cent->pe.zombieSpiritStartTimes[i] = cg.time - step; + } + + fadeRatio = (float)( cg.time - cent->pe.zombieSpiritStartTimes[i] ) / (float)fadeInTime; + if ( fadeRatio < 0.0 ) { + fadeRatio = 0.0; + } + if ( fadeRatio > 1.0 ) { + fadeRatio = 1.0; + } + + if ( cent->pe.cueZombieSpirit ) { + alpha = fadeRatio; + } else { + alpha = 1.0 - ( (float)( cg.time - cent->pe.zombieSpiritEndTime ) / (float)fadeOutTime ); + fadeRatio = alpha; + if ( alpha < 0.0 ) { + cent->pe.zombieSpiritTrailHead[i] = -2; // kill it + continue; + } + } + + active = qtrue; // we have an active spirit, so continue effect + + alpha *= 0.3; + + if ( cent->pe.cueZombieSpirit ) { + rotationTime = cent->pe.zombieSpiritRotationTimes[i]; + radiusCycleTime = cent->pe.zombieSpiritRadiusCycleTimes[i]; + + radius = ( minDist + sin( M_PI * 2 * (float)( (float)( (int)( t + ( (float)( radiusCycleTime * i ) / MAX_ZOMBIE_SPIRITS ) ) % radiusCycleTime ) / (float)radiusCycleTime ) ) * ( maxDist - minDist ) ); + + // get the position + v[0] = ( 0.5 + 0.5 * fadeRatio ) * sin( M_PI * 2 * (float)( (float)( (int)( t + ( (float)( rotationTime * i ) / MAX_ZOMBIE_SPIRITS ) ) % rotationTime ) / (float)rotationTime ) ) * radius; + v[1] = ( 0.5 + 0.5 * fadeRatio ) * cos( M_PI * 2 * (float)( (float)( (int)( t + ( (float)( rotationTime * i ) / MAX_ZOMBIE_SPIRITS ) ) % rotationTime ) / (float)rotationTime ) ) * radius; + v[2] = 12 + 36 * ( 0.5 + 0.5 * fadeRatio ) * sin( M_PI * 2 * (float)( (float)( (int)( t + ( (float)( zCycleTime * i ) / MAX_ZOMBIE_SPIRITS ) ) % zCycleTime ) / (float)zCycleTime ) ); + v[2] -= ( 1.0 - fadeRatio ) * 32; + + VectorAdd( cent->lerpOrigin, v, p[i] ); + } else { + // expand the radius, if it enters world geometry, kill it + rotationTime = cent->pe.zombieSpiritRotationTimes[i]; + radiusCycleTime = cent->pe.zombieSpiritRadiusCycleTimes[i]; + + radius = pow( 1.0 - fadeRatio, 2 ) * fadeDist + ( 1.0 - pow( 1.0 - fadeRatio, 2 ) ) * ( minDist + sin( M_PI * 2 * (float)( (float)( (int)( t + ( (float)( radiusCycleTime * i ) / MAX_ZOMBIE_SPIRITS ) ) % radiusCycleTime ) / (float)radiusCycleTime ) ) * ( maxDist - minDist ) ); + + // get the position + v[0] = sin( M_PI * 2 * (float)( (float)( (int)( t + ( (float)( rotationTime * i ) / MAX_ZOMBIE_SPIRITS ) ) % rotationTime ) / (float)rotationTime ) ) * radius; + v[1] = cos( M_PI * 2 * (float)( (float)( (int)( t + ( (float)( rotationTime * i ) / MAX_ZOMBIE_SPIRITS ) ) % rotationTime ) / (float)rotationTime ) ) * radius; + v[2] = 12 + 36 * sin( M_PI * 2 * (float)( (float)( (int)( t + ( (float)( zCycleTime * i ) / MAX_ZOMBIE_SPIRITS ) ) % zCycleTime ) / (float)zCycleTime ) ); + v[2] -= ( 1.0 - fadeRatio ) * 32; + + VectorAdd( cent->lerpOrigin, v, p[i] ); + + // check for sinking into geometry + trap_CM_BoxTrace( &trace, p[i], p[i], NULL, NULL, 0, MASK_SOLID ); + // if we hit something, clip the velocity, but maintain speed + if ( trace.startsolid ) { + cent->pe.zombieSpiritTrailHead[i] = -2; // kill it + continue; + } + } + + VectorSubtract( p[i], cent->pe.zombieSpiritPos[i], cent->pe.zombieSpiritDir[i] ); + cent->pe.zombieSpiritSpeed[i] = 1000.0 / step * VectorNormalize( cent->pe.zombieSpiritDir[i] ); + VectorCopy( p[i], cent->pe.zombieSpiritPos[i] ); + + cent->pe.zombieSpiritTrailHead[i] = CG_AddTrailJunc( cent->pe.zombieSpiritTrailHead[i], + cgs.media.zombieSpiritTrailShader, + t, + STYPE_STRETCH, + p[i], + trailLife * 2, + alpha, + 0.0, + ( 0.5 + 0.5 * fadeRatio ) * idealWidth, + 0, + TJFL_NOCULL, + colorWhite, + colorWhite, + 1.0, 1 ); + + } + + cent->pe.lastZombieSpirit = t; + + if ( !active ) { // effect has gone + cent->pe.zombieSpiritEndTime = 0; + } + } + + // add the skull at the head of the spirits + memset( &refent, 0, sizeof( refent ) ); + refent.hModel = cgs.media.spiritSkullModel; + refent.backlerp = 0; + refent.renderfx = RF_NOSHADOW | RF_MINLIGHT; //----(SA) + refent.customShader = cgs.media.zombieSpiritSkullShader; + refent.reType = RT_MODEL; + for ( i = 0; i < MAX_ZOMBIE_SPIRITS; i++ ) { + if ( cent->pe.zombieSpiritTrailHead[i] < -1 ) { + continue; // spirit has gone + } + fadeRatio = (float)( cg.time - cent->pe.zombieSpiritStartTimes[i] ) / (float)fadeInTime; + if ( fadeRatio < 0.0 ) { + fadeRatio = 0.0; + } + if ( fadeRatio > 1.0 ) { + fadeRatio = 1.0; + } + + if ( cent->pe.cueZombieSpirit ) { + alpha = fadeRatio; + } else { + alpha = 1.0 - ( (float)( cg.time - cent->pe.zombieSpiritEndTime ) / (float)fadeOutTime ); + fadeRatio = alpha; + if ( alpha < 0.0 ) { + cent->pe.zombieSpiritTrailHead[i] = -2; // kill it + continue; + } + } + + refent.shaderRGBA[3] = (byte)( 0.5 * alpha * 255.0 ); + VectorCopy( cent->pe.zombieSpiritPos[i], refent.origin ); + + // HACK!!! skull model is back-to-front, need to fix + VectorInverse( cent->pe.zombieSpiritDir[i] ); + vectoangles( cent->pe.zombieSpiritDir[i], ang ); + VectorInverse( cent->pe.zombieSpiritDir[i] ); + AnglesToAxis( ang, refent.axis ); + // create the non-normalized axis so we can size it + refent.nonNormalizedAxes = qtrue; + for ( t = 0; t < 3; t++ ) { + VectorNormalize( refent.axis[t] ); + VectorScale( refent.axis[t], 0.35, refent.axis[t] ); + } + // + // add the sound + trap_S_AddLoopingSound( cent->currentState.number, cent->lerpOrigin, vec3_origin, cgs.media.zombieSpiritLoopSound, fadeRatio ); + // + // if this spirit is in a good position to be released and head to the enemy, then release it + if ( fadeRatio == 1.0 && ( lastSpiritRelease > cg.time || ( lastSpiritRelease < cg.time - 2000 ) ) ) { + VectorSubtract( cent->currentState.origin2, refent.origin, v ); + VectorNormalize( v ); + if ( DotProduct( cent->pe.zombieSpiritDir[i], v ) > 0.6 || ( cent->currentState.eFlags & EF_DEAD ) ) { + // check for sinking into geometry + trap_CM_BoxTrace( &trace, refent.origin, refent.origin, NULL, NULL, 0, MASK_SOLID ); + // if we hit something, don't release it yet + if ( !trace.startsolid ) { + if ( cent->pe.zombieSpiritSpeed[i] < 300 ) { + cent->pe.zombieSpiritSpeed[i] = 300; + } + VectorScale( cent->pe.zombieSpiritDir[i], cent->pe.zombieSpiritSpeed[i], v ); + CG_SpawnZombieSpirit( refent.origin, v, cent->pe.zombieSpiritTrailHead[i], cent->currentState.number, &refent, trailLife, idealWidth ); + lastSpiritRelease = cg.time; + cent->pe.zombieSpiritTrailHead[i] = -2; // kill this version of it + continue; + } + } + } + // + // if we didn't kill it, draw it + trap_R_AddRefEntityToScene( &refent ); + } + + if ( cg.time > cent->pe.nextZombieSpiritSound && cent->pe.cueZombieSpirit ) { //&& (cg.time < cent->pe.zombieSpiritStartTime + sndDuration)) { + // spawn a new sound + trap_S_StartSound( cent->lerpOrigin, -1, CHAN_AUTO, cgs.media.zombieSpiritSound ); + cent->pe.nextZombieSpiritSound = cg.time + sndIntervalMin + (int)( (float)( sndIntervalMax - sndIntervalMin ) * random() ); + } + + // add a negative light around us + fadeRatio = (float)( cg.time - cent->pe.zombieSpiritStartTime ) / (float)fadeInTime; + if ( fadeRatio < 0.0 ) { + fadeRatio = 0.0; + } + if ( fadeRatio > 1.0 ) { + fadeRatio = 1.0; + } + + if ( cent->pe.cueZombieSpirit ) { + alpha = fadeRatio; + } else { + alpha = 1.0 - ( (float)( cg.time - cent->pe.zombieSpiritEndTime ) / (float)fadeOutTime ); + fadeRatio = alpha; + if ( alpha < 0.0 ) { + cent->pe.zombieSpiritEndTime = 0; // stop the effect + return; + } + } + fadeRatio *= 0.7; + trap_R_AddLightToScene( cent->lerpOrigin, 300.0, 1.0 * fadeRatio, 1.0 * fadeRatio, 1.0 * fadeRatio, 10 ); +} + +/* +============== +CG_SpawnZombieBat +============== +*/ +void CG_SpawnZombieBat( centity_t *cent, refEntity_t *oldrefent ) { + localEntity_t *le; + refEntity_t *re; + + le = CG_AllocLocalEntity(); + re = &le->refEntity; + + *re = *oldrefent; + + le->leType = LE_ZOMBIE_BAT; + le->startTime = cg.time; + le->endTime = le->startTime + 6000; // TODO: some kind of effect when the bat "burns up" + + le->pos.trType = TR_LINEAR; + le->pos.trTime = cg.time; + CG_PositionEntityOnTag( re, ¢->pe.headRefEnt, "tag_mouth", 0, NULL ); + VectorCopy( re->origin, le->pos.trBase ); + VectorScale( re->axis[0], 150 + 50 * random(), le->pos.trDelta ); + + //le->effectWidth = trailLife; + //le->radius = idealWidth; + le->lastTrailTime = cg.time; + //le->headJuncIndex = trailHead; + le->loopingSound = cgs.media.batsFlyingLoopSound; + + le->ownerNum = cent->currentState.number; + + re->fadeStartTime = le->endTime - 2000; + re->fadeEndTime = le->endTime; +} + +/* +============== +CG_AddZombieFlameEffect +============== +*/ +void CG_AddZombieFlameEffect( centity_t *cent ) { + //const float flameRadius = ZOMBIE_FLAME_RADIUS*2; // TTimo: unused + + const int fadeInTime = 500; + const int fadeOutTime = 0; + + // TTimo: unused + /* + const int spawnIntervalMin = 300; + const int spawnIntervalMax = 1200; + + const int sndIntervalMin = 300; + const int sndIntervalMax = 1400; + */ + const int sndDelay = 0; + // const int sndDuration = 99999; // TTimo: unused + + // const int step = 50; // TTimo: unused + + vec3_t morg, maxis[3], mang; + + float alpha, fadeRatio; + + // qboolean active=qfalse; // TTimo: unused + + if ( cent->currentState.aiChar != AICHAR_ZOMBIE ) { + return; + } + + if ( cent->currentState.eFlags & EF_DEAD ) { + return; + } + + if ( !IS_FLAMING_ZOMBIE( cent->currentState ) ) { + return; + } + + if ( cent->currentState.time > cg.time ) { // doing short burst + return; + } + + if ( ( cent->currentState.eFlags & EF_MONSTER_EFFECT3 ) && ( cg.time > cent->currentState.effect3Time ) ) { + + if ( !cent->pe.cueZombieSpirit ) { + // starting a new effect + cent->pe.cueZombieSpirit = qtrue; + cent->pe.zombieSpiritStartTime = cent->currentState.effect3Time; + cent->pe.lastZombieSpirit = cg.time; + cent->pe.nextZombieSpiritSound = cg.time + sndDelay; + } + cent->pe.zombieSpiritEndTime = cg.time + fadeOutTime; + } else { + + // if running another effect, dont mess with its variables + if ( cent->currentState.eFlags & EF_MONSTER_EFFECT || cent->currentState.effect1Time > cent->currentState.effect3Time ) { + CG_FireFlameChunks( cent, morg, mang, 0.05, qfalse, 0 ); + return; + } + + if ( cent->pe.zombieSpiritEndTime < cg.time ) { + CG_FireFlameChunks( cent, morg, mang, 0.05, qfalse, 0 ); + return; + } + + // clear the flag, and let the effect fade itself out + cent->pe.cueZombieSpirit = qfalse; + } + + // expand the flame dlight + fadeRatio = (float)( cg.time - cent->pe.zombieSpiritStartTime ) / (float)fadeInTime; + if ( fadeRatio < 0.0 ) { + fadeRatio = 0.0; + } + if ( fadeRatio > 1.0 ) { + fadeRatio = 1.0; + } + + if ( cent->pe.cueZombieSpirit ) { + alpha = fadeRatio; + } else { + alpha = ( (float)( cent->pe.zombieSpiritEndTime - cg.time ) / (float)fadeOutTime ); + fadeRatio = alpha; + if ( alpha < 0.0 ) { + cent->pe.zombieSpiritEndTime = 0; // stop the effect + CG_FireFlameChunks( cent, morg, mang, 0.1, qfalse, 0 ); + return; + } + } + + if ( fadeRatio >= 1.0 ) { + CG_GetOriginForTag( cent, ¢->pe.headRefEnt, "tag_mouth", 0, morg, maxis ); + AxisToAngles( maxis, mang ); + CG_FireFlameChunks( cent, morg, mang, ZOMBIE_FLAME_SCALE, qtrue, 0 ); + trap_S_AddLoopingSound( cent->currentState.number, cent->lerpOrigin, vec3_origin, cgs.media.flameSound, 50 ); + } +} + + +/* +============== +CG_AddZombieFlameEffect +============== +*/ +void CG_AddZombieFlameShort( centity_t *cent ) { + vec3_t morg, maxis[3], mang; + + if ( cent->currentState.aiChar != AICHAR_ZOMBIE ) { + return; + } + + if ( cent->currentState.eFlags & EF_DEAD ) { + return; + } + + if ( !IS_FLAMING_ZOMBIE( cent->currentState ) ) { + return; + } + + if ( cent->currentState.time < cg.time ) { + return; + } + + CG_GetOriginForTag( cent, ¢->pe.headRefEnt, "tag_mouth", 0, morg, maxis ); + AxisToAngles( maxis, mang ); + + // shoot this only in bursts + if ( ( cg.time + cent->currentState.number * 100 ) % 1000 > 200 ) { + CG_FireFlameChunks( cent, morg, cent->lerpAngles, 0.1, qfalse, 0 ); + return; + } + + CG_FireFlameChunks( cent, morg, cent->lerpAngles, 0.4, 2, 0 ); + trap_S_AddLoopingSound( cent->currentState.number, cent->lerpOrigin, vec3_origin, cgs.media.flameSound, 50 ); +} + +//========================================================================== + +/* +=============== +LoperLightningEffect + + If loper is alive, a constant lightning effect surrounds him +=============== +*/ +void CG_AddLoperLightningEffect( centity_t *cent ) { +#define LOPER_LIGHTNING_POINT_TIMEOUT 200 +#define LOPER_LIGHTNING_MAX_DIST 360 +#define LOPER_LIGHTNING_NORMAL_DIST 80 +#define LOPER_MAX_POINT_TESTS 10 +#define LOPER_MAX_POINT_TESTS_PERFRAME 20 + + int i, j, pointTests = 0; + vec3_t testPos, tagPos, c, v; + trace_t tr; + float maxDist; + int numPoints; + float colTake; + + if ( cent->currentState.aiChar != AICHAR_LOPER ) { + return; + } + + if ( !cent->currentValid ) { + return; + } + + if ( cent->currentState.eFlags & EF_DEAD ) { + return; + } + + if ( !cent->pe.legsRefEnt.hModel ) { + return; + } + + if ( cent->currentState.eFlags & EF_MONSTER_EFFECT3 ) { + maxDist = LOPER_LIGHTNING_MAX_DIST; + numPoints = MAX_LOPER_LIGHTNING_POINTS; + } else { + maxDist = LOPER_LIGHTNING_NORMAL_DIST; + numPoints = MAX_LOPER_LIGHTNING_POINTS / 3; + } + + CG_GetOriginForTag( cent, ¢->pe.legsRefEnt, "tag_spinner", 0, tagPos, NULL ); + + // show a dlight + // color + colTake = 0.8 - fabs( sin( cg.time ) ) * 0.3; + c[0] = 1.0 - colTake; + c[1] = 1.0 - 0.7 * colTake; + c[2] = 1.0; //c[1] + 0.2; + if ( c[2] > 1.0 ) { + c[2] = 1.0; + } + //VectorScale( c, alpha, c ); + // add the light + trap_R_AddLightToScene( tagPos, LOPER_LIGHTNING_NORMAL_DIST * ( 2.5 + ( 1.0 + sin( cg.time ) ) / 4.0 ), c[0], c[1], c[2], 1 ); + + for ( i = 0; i < numPoints; i++ ) { + // if this point has timed out, find a new spot + if ( ( cent->currentState.eFlags & EF_MONSTER_EFFECT ) && + ( ( !cent->pe.lightningTimes[i] ) || + ( cent->pe.lightningTimes[i] > cg.time ) || + ( cent->pe.lightningTimes[i] < cg.time - 50 ) || + ( VectorDistance( cent->lerpOrigin, cent->pe.lightningPoints[i] ) > maxDist ) ) ) { + // attacking the player + VectorSet( testPos, 12 * crandom(), + 12 * crandom(), + 32 * crandom() ); + VectorAdd( testPos, cg.snap->ps.origin, testPos ); + cent->pe.lightningTimes[i] = cg.time - rand() % ( LOPER_LIGHTNING_POINT_TIMEOUT / 2 ); + VectorCopy( testPos, cent->pe.lightningPoints[i] ); + // play a zap sound + if ( cent->pe.lightningSoundTime < cg.time - 100 ) { + trap_S_StartSound( testPos, ENTITYNUM_WORLD, CHAN_AUTO, cgs.media.lightningZap /*cgs.media.lightningSounds[rand()%3]*/ ); + cent->pe.lightningSoundTime = cg.time; + } + } else if ( ( !cent->pe.lightningTimes[i] ) || + ( cent->pe.lightningTimes[i] > cg.time ) || + ( cent->pe.lightningTimes[i] < cg.time - LOPER_LIGHTNING_POINT_TIMEOUT ) || + ( VectorDistance( cent->lerpOrigin, cent->pe.lightningPoints[i] ) > maxDist ) ) { + + if ( cent->currentState.groundEntityNum == ENTITYNUM_NONE ) { + continue; // must be on the ground + + } + // find a new spot + for ( j = 0; j < LOPER_MAX_POINT_TESTS; j++ ) { + VectorSet( testPos, maxDist * crandom(), + maxDist * crandom(), + maxDist * ( crandom() - 0.5 ) ); + VectorAdd( testPos, cent->lerpOrigin, testPos ); + // try a trace to find a world collision + CG_Trace( &tr, cent->lerpOrigin, NULL, NULL, testPos, cent->currentState.number, MASK_SOLID ); + if ( tr.fraction < 1 && tr.entityNum == ENTITYNUM_WORLD ) { + // found a valid spot! + cent->pe.lightningTimes[i] = cg.time - rand() % ( LOPER_LIGHTNING_POINT_TIMEOUT / 2 ); + VectorCopy( tr.endpos, cent->pe.lightningPoints[i] ); + // play a zap sound + if ( cent->pe.lightningSoundTime < cg.time - 100 ) { + // HACK, move ths sound away from the viewpos, to simulate lower volume + VectorSubtract( testPos, cg.refdef.vieworg, v ); + VectorMA( cg.refdef.vieworg, 3.0, v, v ); + trap_S_StartSound( v, ENTITYNUM_WORLD, CHAN_AUTO, cgs.media.lightningSounds[rand() % 3] ); + cent->pe.lightningSoundTime = cg.time; + } + break; + } + if ( pointTests++ > LOPER_MAX_POINT_TESTS_PERFRAME ) { + j = LOPER_MAX_POINT_TESTS; + continue; + } + } + if ( j == LOPER_MAX_POINT_TESTS ) { + continue; // just don't draw this point + } + } + // + // we have a valid lightning point, so draw it + // sanity check though to make sure it's valid + if ( VectorDistance( cent->lerpOrigin, cent->pe.lightningPoints[i] ) <= maxDist ) { + CG_DynamicLightningBolt( cgs.media.lightningBoltShader, tagPos, cent->pe.lightningPoints[i], 1, 25 + 12.0 * random(), ( cent->currentState.eFlags & EF_MONSTER_EFFECT ) == 0, 1.0, 0, i * i * 2 ); + } + } +} + +/* +=============== +LoperGroundEffect + + Electrical charge applied to ground through torso +=============== +*/ +void CG_AddLoperGroundEffect( centity_t *cent ) { +#define LOPER_GROUNDCHARGE_INTERVAL 30 +#define LOPER_GROUNDCHARGE_DURATION 100 +#define LOPER_GROUNDCHARGE_FADEOUT 400 +#define LOPER_GROUNDCHARGE_RADIUS 150 + vec3_t org, c; + //static vec3_t up = {0,0,1}; // TTimo: unused + float colTake; + int duration; + float alpha, lightAlpha = 0.0f; // TTimo: init + + if ( cent->currentState.aiChar != AICHAR_LOPER ) { + return; + } + + if ( !cent->currentValid ) { + return; + } + + if ( cent->currentState.eFlags & EF_DEAD ) { + return; + } + + if ( !cent->pe.legsRefEnt.hModel ) { + return; + } + + if ( !( cent->currentState.eFlags & EF_MONSTER_EFFECT3 ) ) { + if ( !cent->pe.loperGroundValidTime ) { + alpha = 0.0; + } else { + // alpha for lightning mark + duration = LOPER_GROUNDCHARGE_FADEOUT - ( cg.time - cent->pe.loperGroundValidTime ); + if ( duration <= 0 ) { + cent->pe.loperGroundValidTime = 0; + } + alpha = (float)duration / (float)LOPER_GROUNDCHARGE_FADEOUT; + if ( alpha < 0 ) { + alpha = 0; + } + // light lives on a bit after the last lightning mark is spawned + lightAlpha = (float)( duration + LOPER_GROUNDCHARGE_DURATION ) / (float)( LOPER_GROUNDCHARGE_FADEOUT + LOPER_GROUNDCHARGE_DURATION ); + if ( lightAlpha < 0 ) { + lightAlpha = 0; + } + } + } else { + cent->pe.loperGroundValidTime = cg.time; + duration = LOPER_GROUNDCHARGE_DURATION; + alpha = 1.0; + lightAlpha = 1.0; + } + + if ( !lightAlpha ) { + cent->pe.loperGroundValidTime = 0; + return; + } + + // show a dlight + // color + colTake = 0.8 - fabs( sin( cg.time ) ) * 0.3; + c[0] = 1.0 - colTake; + c[1] = 1.0 - 0.8 * colTake; + c[2] = 1.0; //c[1] + 0.2; + if ( c[2] > 1.0 ) { + c[2] = 1.0; + } + VectorScale( c, alpha, c ); + // add the light + trap_R_AddLightToScene( cent->lerpOrigin, LOPER_GROUNDCHARGE_RADIUS * ( 3.0 + 2.0 * ( 1.0 + sin( 0.001 * ( ( cg.time ) % ( 1000 * ( 2 + cent->currentState.number ) ) ) ) ) / 2.0 ), c[0], c[1], c[2], 1 ); + //trap_R_AddLightToScene( cent->lerpOrigin, LOPER_GROUNDCHARGE_RADIUS*(2.0 + 1.0*(1.0+cos(0.001343*((cg.time)%(1000*(2+cent->currentState.number)))))/2.0), c[0], c[1], c[2], 0 ); + + if ( !alpha ) { + return; + } + + // create a fading mark at intervals + if ( cent->pe.loperLastGroundChargeTime < cg.time - LOPER_GROUNDCHARGE_INTERVAL ) { + // org + VectorCopy( cent->lerpOrigin, org ); + org[2] -= 20; + // color + if ( cent->pe.loperGroundChargeToggle ^= 1 ) { + // random blue + colTake = 0.5 + random() * 0.5; + c[0] = 1.0 - colTake; + c[1] = 1.0 - /*(0.5 + 0.5*random())**/ colTake; + c[2] = c[1] + 0.2; + if ( c[2] > 1.0 ) { + c[2] = 1.0; + } + } else { + VectorSet( c, 1,1,1 ); + } + // draw the mark + //CG_ImpactMark( cgs.media.loperGroundChargeShader, org, up, random()*360, c[0], c[1], c[2], alpha, qtrue, (0.5+0.5*random())*LOPER_GROUNDCHARGE_RADIUS, qfalse, LOPER_GROUNDCHARGE_DURATION ); + cent->pe.loperLastGroundChargeTime = cg.time; + // make a new sound + VectorSet( org, org[0] + crandom() * 256, org[1] + crandom() * 256, org[2] + crandom() * 256 ); + trap_S_StartSound( org, ENTITYNUM_WORLD, CHAN_AUTO, cgs.media.lightningZap ); + } + +} + +//========================================================================== + +/* +============== +CG_SpawnHelgaSpirit +============== +*/ +void CG_SpawnHelgaSpirit( vec3_t origin, vec3_t vel, int trailHead, int ownerNum, refEntity_t *oldrefent, int trailLife, int idealWidth ) { + localEntity_t *le; + refEntity_t *re; + + le = CG_AllocLocalEntity(); + re = &le->refEntity; + + *re = *oldrefent; + + le->leType = LE_HELGA_SPIRIT; + le->startTime = cg.time - 5000; // set it back so we dont fade it in like the "end" spirits + le->endTime = cg.time + 5000; + + le->pos.trType = TR_LINEAR; + le->pos.trTime = cg.time; + VectorCopy( origin, le->pos.trBase ); + VectorCopy( vel, le->pos.trDelta ); + + le->effectWidth = trailLife; + le->radius = idealWidth; + le->lastTrailTime = cg.time; + le->headJuncIndex = trailHead; + le->loopingSound = cgs.media.helgaSpiritLoopSound; + + le->ownerNum = ownerNum; + + re->fadeStartTime = le->endTime - 2000; + re->fadeEndTime = le->endTime; + re->shaderTime = cg.time; +} + +/* +============== +CG_AddHelgaSpiritEffect +============== +*/ +void CG_AddHelgaSpiritEffect( centity_t *cent ) { + const int trailLife = 600; + const float idealWidth = 30.0; + const int fadeInTime = 2000; + const int fadeOutTime = 2000; + const int minRotationTime = 6000; + const int maxRotationTime = 10000; + const int minRadiusCycleTime = 2400; + const int maxRadiusCycleTime = 2900; + const int zCycleTime = 3000; + + const float minDist = 50; + const float maxDist = 100; + const float fadeDist = 4; + + const int sndIntervalMin = 300; + const int sndIntervalMax = 1400; + const int sndDelay = 0; + //const int sndDuration = 99999;//1500; // TTimo: unused + + const int step = 25; + + int i, t; + vec3_t v,p[MAX_ZOMBIE_SPIRITS], ang; + float fadeRatio, alpha, radius; + int rotationTime, radiusCycleTime; + trace_t trace; + refEntity_t refent; + + static int lastSpiritRelease; + + qboolean active = qfalse; + + if ( cent->currentState.aiChar != AICHAR_HELGA ) { + return; + } + + // sanity check the server time to make sure we don't start the effect + // to early, whilst reloading a savegame or something + if ( cg.time < cent->currentState.effect1Time ) { + return; + } + + if ( cent->currentState.eFlags & EF_MONSTER_EFFECT ) { + + if ( !cent->pe.cueZombieSpirit ) { + // starting a new effect + cent->pe.cueZombieSpirit = qtrue; + cent->pe.zombieSpiritStartTime = cent->currentState.effect1Time; + cent->pe.lastZombieSpirit = cg.time; + cent->pe.nextZombieSpiritSound = cg.time + sndDelay; + for ( i = 0; i < MAX_ZOMBIE_SPIRITS; i++ ) { + cent->pe.zombieSpiritTrailHead[i] = -1; + cent->pe.zombieSpiritRotationTimes[i] = minRotationTime + ( random() * ( maxRotationTime - minRotationTime ) ); + cent->pe.zombieSpiritRadiusCycleTimes[i] = minRadiusCycleTime + ( random() * ( maxRadiusCycleTime - minRadiusCycleTime ) ); + cent->pe.zombieSpiritStartTimes[i] = cent->currentState.effect1Time; + } + } + cent->pe.zombieSpiritEndTime = cg.time; + + } else { + + // if running another effect, dont mess with its variables + if ( cent->currentState.eFlags & EF_MONSTER_EFFECT3 || cent->currentState.effect1Time < cent->currentState.effect3Time ) { + return; + } + + if ( !cent->pe.zombieSpiritEndTime ) { + return; + } + // clear the flag, and let the effect fade itself out + cent->pe.cueZombieSpirit = qfalse; + } + + for ( t = cent->pe.lastZombieSpirit + step; t <= cg.time; t += step ) { + + // add the spirits + for ( i = 0; i < MAX_ZOMBIE_SPIRITS; i++ ) { + + if ( cent->pe.zombieSpiritTrailHead[i] < -1 ) { + // spirit has gone, create a new one + cent->pe.zombieSpiritTrailHead[i] = -1; + cent->pe.zombieSpiritRotationTimes[i] = minRotationTime + ( random() * ( maxRotationTime - minRotationTime ) ); + cent->pe.zombieSpiritRadiusCycleTimes[i] = minRadiusCycleTime + ( random() * ( maxRadiusCycleTime - minRadiusCycleTime ) ); + cent->pe.zombieSpiritStartTimes[i] = cg.time - step; + } + + fadeRatio = (float)( cg.time - cent->pe.zombieSpiritStartTimes[i] ) / (float)fadeInTime; + if ( fadeRatio < 0.0 ) { + fadeRatio = 0.0; + } + if ( fadeRatio > 1.0 ) { + fadeRatio = 1.0; + } + + if ( cent->pe.cueZombieSpirit ) { + alpha = fadeRatio; + } else { + alpha = 1.0 - ( (float)( cg.time - cent->pe.zombieSpiritEndTime ) / (float)fadeOutTime ); + fadeRatio = alpha; + if ( alpha < 0.0 ) { + cent->pe.zombieSpiritTrailHead[i] = -2; // kill it + continue; + } + } + + active = qtrue; // we have an active spirit, so continue effect + + alpha *= 0.3; + + if ( cent->pe.cueZombieSpirit ) { + rotationTime = cent->pe.zombieSpiritRotationTimes[i]; + radiusCycleTime = cent->pe.zombieSpiritRadiusCycleTimes[i]; + + radius = ( minDist + sin( M_PI * 2 * (float)( (float)( (int)( t + ( (float)( radiusCycleTime * i ) / MAX_ZOMBIE_SPIRITS ) ) % radiusCycleTime ) / (float)radiusCycleTime ) ) * ( maxDist - minDist ) ); + + // get the position + v[0] = ( 0.5 + 0.5 * fadeRatio ) * sin( M_PI * 2 * (float)( (float)( (int)( t + ( (float)( rotationTime * i ) / MAX_ZOMBIE_SPIRITS ) ) % rotationTime ) / (float)rotationTime ) ) * radius; + v[1] = ( 0.5 + 0.5 * fadeRatio ) * cos( M_PI * 2 * (float)( (float)( (int)( t + ( (float)( rotationTime * i ) / MAX_ZOMBIE_SPIRITS ) ) % rotationTime ) / (float)rotationTime ) ) * radius; + v[2] = 12 + 36 * ( 0.5 + 0.5 * fadeRatio ) * sin( M_PI * 2 * (float)( (float)( (int)( t + ( (float)( zCycleTime * i ) / MAX_ZOMBIE_SPIRITS ) ) % zCycleTime ) / (float)zCycleTime ) ); + v[2] -= ( 1.0 - fadeRatio ) * 32; + + VectorAdd( cent->lerpOrigin, v, p[i] ); + } else { + // expand the radius, if it enters world geometry, kill it + rotationTime = cent->pe.zombieSpiritRotationTimes[i]; + radiusCycleTime = cent->pe.zombieSpiritRadiusCycleTimes[i]; + + radius = pow( 1.0 - fadeRatio, 2 ) * fadeDist + ( 1.0 - pow( 1.0 - fadeRatio, 2 ) ) * ( minDist + sin( M_PI * 2 * (float)( (float)( (int)( t + ( (float)( radiusCycleTime * i ) / MAX_ZOMBIE_SPIRITS ) ) % radiusCycleTime ) / (float)radiusCycleTime ) ) * ( maxDist - minDist ) ); + + // get the position + v[0] = sin( M_PI * 2 * (float)( (float)( (int)( t + ( (float)( rotationTime * i ) / MAX_ZOMBIE_SPIRITS ) ) % rotationTime ) / (float)rotationTime ) ) * radius; + v[1] = cos( M_PI * 2 * (float)( (float)( (int)( t + ( (float)( rotationTime * i ) / MAX_ZOMBIE_SPIRITS ) ) % rotationTime ) / (float)rotationTime ) ) * radius; + v[2] = 12 + 36 * sin( M_PI * 2 * (float)( (float)( (int)( t + ( (float)( zCycleTime * i ) / MAX_ZOMBIE_SPIRITS ) ) % zCycleTime ) / (float)zCycleTime ) ); + v[2] -= ( 1.0 - fadeRatio ) * 32; + + VectorAdd( cent->lerpOrigin, v, p[i] ); + + // check for sinking into geometry + trap_CM_BoxTrace( &trace, p[i], p[i], NULL, NULL, 0, MASK_SOLID ); + // if we hit something, clip the velocity, but maintain speed + if ( trace.startsolid ) { + cent->pe.zombieSpiritTrailHead[i] = -2; // kill it + continue; + } + } + + VectorSubtract( p[i], cent->pe.zombieSpiritPos[i], cent->pe.zombieSpiritDir[i] ); + cent->pe.zombieSpiritSpeed[i] = 1000.0 / step * VectorNormalize( cent->pe.zombieSpiritDir[i] ); + VectorCopy( p[i], cent->pe.zombieSpiritPos[i] ); + + cent->pe.zombieSpiritTrailHead[i] = CG_AddTrailJunc( cent->pe.zombieSpiritTrailHead[i], + cgs.media.zombieSpiritTrailShader, + t, + STYPE_STRETCH, + p[i], + trailLife * 2, + alpha, + 0.0, + ( 0.5 + 0.5 * fadeRatio ) * idealWidth, + 0, + TJFL_NOCULL, + colorWhite, + colorWhite, + 1.0, 1 ); + + } + + cent->pe.lastZombieSpirit = t; + + if ( !active ) { // effect has gone + cent->pe.zombieSpiritEndTime = 0; + } + } + + // add the skull at the head of the spirits + memset( &refent, 0, sizeof( refent ) ); + refent.hModel = cgs.media.helgaGhostModel; + refent.backlerp = 0; + refent.renderfx = RF_NOSHADOW | RF_MINLIGHT; //----(SA) + refent.customShader = cgs.media.helgaSpiritSkullShader; + refent.reType = RT_MODEL; + for ( i = 0; i < MAX_ZOMBIE_SPIRITS; i++ ) { + if ( cent->pe.zombieSpiritTrailHead[i] < -1 ) { + continue; // spirit has gone + } + fadeRatio = (float)( cg.time - cent->pe.zombieSpiritStartTimes[i] ) / (float)fadeInTime; + if ( fadeRatio < 0.0 ) { + fadeRatio = 0.0; + } + if ( fadeRatio > 1.0 ) { + fadeRatio = 1.0; + } + + if ( cent->pe.cueZombieSpirit ) { + alpha = fadeRatio; + } else { + alpha = 1.0 - ( (float)( cg.time - cent->pe.zombieSpiritEndTime ) / (float)fadeOutTime ); + fadeRatio = alpha; + if ( alpha < 0.0 ) { + cent->pe.zombieSpiritTrailHead[i] = -2; // kill it + continue; + } + } + + refent.shaderRGBA[3] = (byte)( 0.5 * alpha * 255.0 ); + VectorCopy( cent->pe.zombieSpiritPos[i], refent.origin ); + + // HACK!!! skull model is back-to-front, need to fix + //VectorInverse(cent->pe.zombieSpiritDir[i]); + vectoangles( cent->pe.zombieSpiritDir[i], ang ); + //VectorInverse(cent->pe.zombieSpiritDir[i]); + AnglesToAxis( ang, refent.axis ); +/* // create the non-normalized axis so we can size it + refent.nonNormalizedAxes = qtrue; + for (t=0; t<3; t++) { + VectorNormalize( refent.axis[t] ); + VectorScale( refent.axis[t], 0.35, refent.axis[t] ); + } +*/ // + // add the sound + trap_S_AddLoopingSound( -1, cent->lerpOrigin, vec3_origin, cgs.media.helgaSpiritLoopSound, fadeRatio ); + // + // if this spirit is in a good position to be released and head to the enemy, then release it + if ( fadeRatio == 1.0 && ( lastSpiritRelease > cg.time || ( lastSpiritRelease < cg.time - 1000 ) ) ) { + VectorSubtract( cent->currentState.origin2, refent.origin, v ); + VectorNormalize( v ); + if ( DotProduct( cent->pe.zombieSpiritDir[i], v ) > 0.4 || ( cent->currentState.eFlags & EF_DEAD ) ) { + // check for sinking into geometry + trap_CM_BoxTrace( &trace, refent.origin, refent.origin, NULL, NULL, 0, MASK_SOLID ); + // if we hit something, don't release it yet + if ( !trace.startsolid ) { + if ( cent->pe.zombieSpiritSpeed[i] < 300 ) { + cent->pe.zombieSpiritSpeed[i] = 300; + } + VectorScale( cent->pe.zombieSpiritDir[i], cent->pe.zombieSpiritSpeed[i], v ); + CG_SpawnHelgaSpirit( refent.origin, v, cent->pe.zombieSpiritTrailHead[i], cent->currentState.number, &refent, trailLife, idealWidth ); + lastSpiritRelease = cg.time; + cent->pe.zombieSpiritTrailHead[i] = -2; // kill this version of it + continue; + } + } + } + // + // if we didn't kill it, draw it + trap_R_AddRefEntityToScene( &refent ); + } + + if ( cg.time > cent->pe.nextZombieSpiritSound && cent->pe.cueZombieSpirit ) { //&& (cg.time < cent->pe.zombieSpiritStartTime + sndDuration)) { + // spawn a new sound + //trap_S_StartSound( cent->lerpOrigin, -1, CHAN_AUTO, cgs.media.helgaSpiritSound ); + CG_SoundPlayIndexedScript( cgs.media.helgaSpiritSound, NULL, cent->currentState.number ); + cent->pe.nextZombieSpiritSound = cg.time + sndIntervalMin + (int)( (float)( sndIntervalMax - sndIntervalMin ) * random() ); + } + + // add a negative light around us + fadeRatio = (float)( cg.time - cent->pe.zombieSpiritStartTime ) / (float)fadeInTime; + if ( fadeRatio < 0.0 ) { + fadeRatio = 0.0; + } + if ( fadeRatio > 1.0 ) { + fadeRatio = 1.0; + } + + if ( cent->pe.cueZombieSpirit ) { + alpha = fadeRatio; + } else { + alpha = 1.0 - ( (float)( cg.time - cent->pe.zombieSpiritEndTime ) / (float)fadeOutTime ); + fadeRatio = alpha; + if ( alpha < 0.0 ) { + cent->pe.zombieSpiritEndTime = 0; // stop the effect + return; + } + } + fadeRatio *= 0.7; + trap_R_AddLightToScene( cent->lerpOrigin, 500.0, 1.0 * fadeRatio, 1.0 * fadeRatio, 1.0 * fadeRatio, 10 ); +} + +//========================================================================== + +/* +=============== +CG_AddRefEntityWithPowerups + +Adds a piece with modifications or duplications for powerups +Also called by CG_Missile for quad rockets, but nobody can tell... +=============== +*/ +void CG_AddRefEntityWithPowerups( refEntity_t *ent, int powerups, int team, entityState_t *es, const vec3_t fireRiseDir ) { + centity_t *cent; + refEntity_t backupRefEnt; //, parentEnt; + qboolean onFire = qfalse; + float alpha = 0.0; + + cent = &cg_entities[es->number]; + + ent->entityNum = es->number; + + if ( cent->pe.forceLOD ) { + ent->reFlags |= REFLAG_FORCE_LOD; + } + + // RF, if in camera mode, force a full lod, since we are in a controlled environment + if ( cg.cameraMode ) { + ent->reFlags |= REFLAG_FULL_LOD; + } + +//----(SA) testing + // (SA) disabling +// if(cent->currentState.eFlags & EF_DEAD) { +// ent->reFlags |= REFLAG_DEAD_LOD; +// } +//----(SA) end + + backupRefEnt = *ent; + + if ( powerups & ( 1 << PW_INVIS ) ) { + ent->customShader = cgs.media.invisShader; + trap_R_AddRefEntityToScene( ent ); +#if 0 + // ------------------------------- + // Zombie effects + // + } else if ( es->aiChar == AICHAR_ZOMBIE ) { + + // Zombie needs special processing, to remove the bits of flesh that have been torn away + + if ( ent->hModel == cent->pe.torsoRefEnt.hModel ) { + //ent->reFlags = REFLAG_ZOMBIEFX; + ent->shaderTime = 0; + } else if ( ent->hModel == cent->pe.legsRefEnt.hModel ) { + //ent->reFlags = REFLAG_ZOMBIEFX2; // ref needs to know this is the legs + ent->shaderTime = 0; + } + + // first, check for portal spawning + if ( es->time2 ) { + if ( es->time2 < cg.time ) { + return; // not ready yet + } + // fade in the skeleton, skin should "compose" (reverse decomposition) + alpha = (float)( cg.time - es->time2 ) / PORTAL_ZOMBIE_SPAWNTIME; + if ( alpha > 1 ) { + alpha = 1; + } + // skeleton fades in towards end of effect + if ( alpha < 0.5 ) { + ent->shaderRGBA[3] = 0; + } else { ent->shaderRGBA[3] = ( unsigned char )( ( alpha - 0.5 ) * 2.0 * 255 );} + // + ent->shaderTime = 0.001 * ( 1.0 - alpha ) * ZOMBIEFX_FADEOUT_TIME; + + if ( ent->hModel == cent->pe.headRefEnt.hModel ) { + //ent->reFlags = REFLAG_ZOMBIEFX; + ent->customShader = cgs.media.zombieHeadFadeShader; + } + + trap_R_AddRefEntityToScene( ent ); + + // add flaming effect + onFire = qtrue; + alpha *= 0.5; + + } else { + + // if the Zombie is dead, the skin should decompose + + ent->shaderRGBA[3] = 255; + + if ( es->eFlags & EF_MONSTER_EFFECT2 && + cent->currentState.effect2Time < cg.time ) { // Ridah, Zombie death effect + + if ( ent->hModel == cent->pe.headRefEnt.hModel ) { + //ent->reFlags = REFLAG_ZOMBIEFX; + ent->customShader = cgs.media.zombieHeadFadeShader; + } + + ent->shaderTime = 0.001 * ( cg.time - cent->currentState.effect2Time ); + trap_R_AddRefEntityToScene( ent ); +/* + // skeleton: add legs and head parts + if (ent->hModel == cent->pe.legsRefEnt.hModel) { + ent->skinNum = 0; + ent->reFlags = 0; + ent->customShader = cgs.media.skeletonSkinShader; + + // legs + ent->hModel = cgs.media.skeletonLegsModel; + trap_R_AddRefEntityToScene( ent ); + + // torso (just get this so we can place the head correctly) + parentEnt = *ent; + CG_PositionEntityOnTag( ent, &parentEnt, parentEnt.hModel, "tag_torso", NULL ); + ent->hModel = cgs.media.skeletonTorsoModel; + + // head + parentEnt = *ent; + CG_PositionEntityOnTag( ent, &parentEnt, parentEnt.hModel, "tag_head", NULL ); + ent->hModel = cgs.media.skeletonHeadModel; + trap_R_AddRefEntityToScene( ent ); + } +*/ + } else { // show it normally + trap_R_AddRefEntityToScene( ent ); + } + } + + // restore previous state + *ent = backupRefEnt; +#endif +/* + } else if (es->eFlags & EF_MONSTER_EFFECT2 && es->aiChar == AICHAR_ZOMBIE && + cent->currentState.effect2Time < cg.time) { // Ridah, Zombie death effect + const int fadeRiseTime = 4000; + + if (cent->pe.zombieDeathFadeStart < cg.time && ent->hModel == cent->pe.legsRefEnt.hModel) { + // add the skeleton models starting with the legs + ent->fadeEndTime = 0; + ent->fadeStartTime = 0; + ent->skinNum = 0; + + // legs + ent->hModel = cgs.media.skeletonLegsModel; + ent->customSkin = cgs.media.skeletonLegsSkin; + trap_R_AddRefEntityToScene( ent ); + + // torso + parentEnt = *ent; + CG_PositionEntityOnTag( ent, &parentEnt, parentEnt.hModel, "tag_torso", NULL ); + ent->hModel = cgs.media.skeletonTorsoModel; + ent->customSkin = cgs.media.skeletonTorsoSkin; + trap_R_AddRefEntityToScene( ent ); + + // head + parentEnt = *ent; + CG_PositionEntityOnTag( ent, &parentEnt, parentEnt.hModel, "tag_head", NULL ); + ent->hModel = cgs.media.skeletonHeadModel; + ent->customSkin = cgs.media.skeletonHeadSkin; + trap_R_AddRefEntityToScene( ent ); + + // restore previous state + *ent = backupRefEnt; + } + + if (cent->pe.zombieDeathFadeEnd + fadeRiseTime > cg.time) { + // slowly fade the zombie "skin" out, revealing the skeleton underneath + + //VectorSubtract( ent->origin, cg.snap->ps.origin, ent->fireRiseDir ); + //VectorNegate( ent->axis[0], ent->fireRiseDir ); + VectorNormalize2( ent->axis[0], ent->fireRiseDir ); + + if (cent->pe.zombieDeathFadeEnd > cg.time) { + + // the zombie has hard-edged alpha blending on it's body texture by default + // so we need to override that for smooth fading + if (ent->hModel == cent->pe.legsRefEnt.hModel || + ent->hModel == cent->pe.torsoRefEnt.hModel) { + ent->customShader = cgs.media.zombieBodyFadeShader; + } else if (ent->hModel == cent->pe.headRefEnt.hModel) { + ent->customShader = cgs.media.zombieHeadFadeShader; + } + // fade the alpha from 0 -> 255 as the time goes, which will fade from front to back + if (cent->pe.zombieDeathFadeStart > cg.time) { + ent->shaderRGBA[3] = 128; + } else { + ent->shaderRGBA[3] = 128 - (unsigned char)(128.0*pow(((float)(cg.time - cent->pe.zombieDeathFadeStart) / (float)(cent->pe.zombieDeathFadeEnd - cent->pe.zombieDeathFadeStart)), 2)); + } + ent->shaderTime = 1.0; + + trap_R_AddRefEntityToScene( ent ); + } + } +*/ + // ------------------------------- + } else { + + if ( CG_EntOnFire( &cg_entities[es->number] ) ) { + ent->reFlags |= REFLAG_FORCE_LOD; + } + + trap_R_AddRefEntityToScene( ent ); + + if ( powerups & ( 1 << PW_QUAD ) ) { + if ( team == TEAM_RED ) { + ent->customShader = cgs.media.redQuadShader; + } else { + ent->customShader = cgs.media.quadShader; + } + trap_R_AddRefEntityToScene( ent ); + } + if ( powerups & ( 1 << PW_REGEN ) ) { + if ( ( ( cg.time / 100 ) % 10 ) == 1 ) { + ent->customShader = cgs.media.regenShader; + trap_R_AddRefEntityToScene( ent ); + } + } + if ( powerups & ( 1 << PW_BATTLESUIT ) ) { + ent->customShader = cgs.media.battleSuitShader; + trap_R_AddRefEntityToScene( ent ); + } + } + + if ( !onFire && CG_EntOnFire( &cg_entities[es->number] ) ) { + onFire = qtrue; + // set the alpha + alpha = ( cg.time - es->onFireStart ) / 1500.0; + if ( alpha > 1.0 ) { + alpha = ( es->onFireEnd - cg.time ) / 1500.0; + if ( alpha > 1.0 ) { + alpha = 1.0; + } + } + } + // Flaming zombie always shows a little fire + if ( !es->time2 && alpha < 1.0 && ( cent->currentState.aiChar == AICHAR_ZOMBIE ) && IS_FLAMING_ZOMBIE( cent->currentState ) /*&& !(cent->currentState.eFlags & EF_DEAD)*/ ) { + onFire = qtrue; + // set the alpha + alpha = 1.0; + } + + if ( onFire ) { + if ( alpha < 0.0 ) { + alpha = 0.0; + } + ent->shaderRGBA[3] = ( unsigned char )( 255.0 * alpha ); + VectorCopy( fireRiseDir, ent->fireRiseDir ); + if ( VectorCompare( ent->fireRiseDir, vec3_origin ) ) { + VectorSet( ent->fireRiseDir, 0, 0, 1 ); + } +/* + if (ent->fireRiseDir[2] > 0) + ent->fireRiseDir[2] *= -1; + + ent->customShader = cgs.media.dripWetShader2; + trap_R_AddRefEntityToScene( ent ); + + ent->customShader = cgs.media.dripWetShader; + trap_R_AddRefEntityToScene( ent ); + + VectorCopy( fireRiseDir, ent->fireRiseDir ); +*/ + ent->customShader = cgs.media.onFireShader; + trap_R_AddRefEntityToScene( ent ); + + ent->customShader = cgs.media.onFireShader2; + trap_R_AddRefEntityToScene( ent ); + + if ( ent->hModel == cent->pe.legsRefEnt.hModel ) { + trap_S_AddLoopingSound( es->number, ent->origin, vec3_origin, cgs.media.flameCrackSound, (int)( 40.0 * alpha ) ); + } + } + + // tesla effect + if ( cg_entities[es->number].pe.teslaDamagedTime > cg.time - 400 ) { + float alpha; + + alpha = ( 400.0 - (float)( cg.time - cg_entities[es->number].pe.teslaDamagedTime ) ) / 400.0; + + ent->shaderRGBA[0] = ( unsigned char )( 50.0 * alpha ); + ent->shaderRGBA[1] = ( unsigned char )( 130.0 * alpha ); + ent->shaderRGBA[2] = ( unsigned char )( 255.0 * alpha ); + + if ( ( cg.time / 50 ) % ( 2 + ( cg.time % 2 ) ) == 0 ) { + ent->customShader = cgs.media.teslaAltDamageEffectShader; + } else { + ent->customShader = cgs.media.teslaDamageEffectShader; + } + trap_R_AddRefEntityToScene( ent ); + } + + *ent = backupRefEnt; +} + +char *vtosf( const vec3_t v ) { + static int index; + static char str[8][64]; + char *s; + + // use an array so that multiple vtos won't collide + s = str[index]; + index = ( index + 1 ) & 7; + + Com_sprintf( s, 64, "(%f %f %f)", v[0], v[1], v[2] ); + + return s; +} + + +#define SPINNER_SPEED 0.3f +/* +============== +CG_SpinnerSpinAngle +============== +*/ +static float CG_SpinnerSpinAngle( centity_t *cent ) { + int delta; + + if ( cent->currentState.eFlags & EF_DEAD ) { // don't spin for dead loper (TODO: spindown, or blow off parts rather than just stopping) + return cent->pe.spinnerAngle; + } + + delta = cg.time - cent->pe.spinnerTime; + + return -( cent->pe.spinnerAngle + delta * SPINNER_SPEED ); +} + +/* +============== +CG_AddFireLight +============== +*/ +static void CG_AddFireLight( centity_t *cent ) { + + return; + +/* OPTIMIZATION, TOO MANY DLIGHTS WHEN FIRE IS AROUND + + entityState_t *es; + float alpha; + + if (CG_EntOnFire(&cg_entities[cent->currentState.number])) { + + es = ¢->currentState; + + // set the alpha + alpha = (cg.time - es->onFireStart) / 1500.0; + if (alpha > 1.0) { + alpha = (es->onFireEnd - cg.time) / 1500.0; + if (alpha > 1.0) { + alpha = 1.0; + } + } + if (alpha <= 0.0) return; + + trap_R_AddLightToScene( cent->lerpOrigin, 128 + 128*alpha, 1.000000*alpha, 0.603922*alpha, 0.207843*alpha, 0 ); + } +*/ +} + +/* +=============== +CG_AnimPlayerConditions + + predict, or calculate condition for this entity, if it is not the local client +=============== +*/ +void CG_AnimPlayerConditions( centity_t *cent ) { + entityState_t *es; + clientInfo_t *ci; +// int legsAnim; + + if ( cg.snap && cg.snap->ps.clientNum == cent->currentState.number && !cg.renderingThirdPerson ) { + return; + } + + es = ¢->currentState; + // DHM-Nerve + //ci = &cgs.clientinfo[es->number]; // es->number is not always a valid client num + ci = &cgs.clientinfo[es->clientNum]; + // dhm-Nerve + + // WEAPON + BG_UpdateConditionValue( es->clientNum, ANIM_COND_WEAPON, es->weapon, qtrue ); + + // MOUNTED + if ( es->eFlags & EF_MG42_ACTIVE ) { + BG_UpdateConditionValue( es->clientNum, ANIM_COND_MOUNTED, MOUNTED_MG42, qtrue ); + } else { + BG_UpdateConditionValue( es->clientNum, ANIM_COND_MOUNTED, MOUNTED_UNUSED, qtrue ); + } + + // UNDERHAND + BG_UpdateConditionValue( es->clientNum, ANIM_COND_UNDERHAND, cent->lerpAngles[0] > 0, qtrue ); + + // LEANING + /* TODO??? + if (es->lean > 0) { + BG_UpdateConditionValue( es->clientNum, ANIM_COND_LEANING, LEANING_RIGHT, qtrue ); + } else if (es->lean < 0) { + BG_UpdateConditionValue( es->clientNum, ANIM_COND_LEANING, LEANING_LEFT, qtrue ); + } else { + BG_UpdateConditionValue( es->clientNum, ANIM_COND_LEANING, LEANING_UNUSED, qtrue ); + } + */ + + if ( es->eFlags & EF_CROUCHING ) { + BG_UpdateConditionValue( es->clientNum, ANIM_COND_CROUCHING, qtrue, qtrue ); + } else { + BG_UpdateConditionValue( es->clientNum, ANIM_COND_CROUCHING, qfalse, qtrue ); + } + + if ( es->eFlags & EF_FIRING ) { + BG_UpdateConditionValue( es->clientNum, ANIM_COND_FIRING, qtrue, qtrue ); + } else { + BG_UpdateConditionValue( es->clientNum, ANIM_COND_FIRING, qfalse, qtrue ); + } + + // reverse engineer the legs anim -> movetype (if possible) + //legsAnim = es->legsAnim & ~ANIM_TOGGLEBIT; + //if (ci->modelInfo->animations[legsAnim].movetype) { + // BG_UpdateConditionValue( es->clientNum, ANIM_COND_MOVETYPE, ci->modelInfo->animations[legsAnim].movetype, qfalse ); + //} + // RF, changed this since we dont need to be careful about bandwidth anymore, and this method + // is much more accurate + if ( cent->currentState.animMovetype ) { + BG_UpdateConditionValue( es->clientNum, ANIM_COND_MOVETYPE, cent->currentState.animMovetype, qtrue ); + } +} + +void CG_DeadSink( centity_t *cent ) { + if ( cent->currentState.aiChar != AICHAR_WARZOMBIE ) { + return; + } + if ( !( cent->currentState.eFlags & EF_DEAD ) ) { + return; + } + if ( !cent->currentState.effect3Time ) { + return; + } + if ( cent->currentState.effect3Time >= cg.time ) { + return; + } + // sink + cent->lerpOrigin[2] -= DEAD_SINK_DEPTH * ( (float)( cg.time - cent->currentState.effect3Time ) / DEAD_SINK_DURATION ); +} + +/* +=============== +CG_Player +=============== +*/ +void CG_Player( centity_t *cent ) { + int i; + clientInfo_t *ci; + refEntity_t legs; + refEntity_t torso; + refEntity_t head; + refEntity_t acc; + + vec3_t lightorigin; + + int clientNum; + int renderfx; + qboolean shadow; //, drawweap = qtrue; // TTimo: unused + float shadowPlane; + + float gumsflappin = 0; // talking amplitude + + centity_t *cgsnap; + + cgsnap = &cg_entities[cg.snap->ps.clientNum]; + + shadow = qfalse; // gjd added to make sure it was initialized + shadowPlane = 0.0; // ditto + + // if set to invisible, skip + if ( cent->currentState.eFlags & EF_NODRAW ) { + return; + } + + // the client number is stored in clientNum. It can't be derived + // from the entity number, because a single client may have + // multiple corpses on the level using the same clientinfo + clientNum = cent->currentState.clientNum; + if ( clientNum < 0 || clientNum >= MAX_CLIENTS ) { + CG_Error( "Bad clientNum on player entity" ); + } + ci = &cgs.clientinfo[ clientNum ]; + +//----(SA) we need to not see the player in the camera +// if (cg.cameraMode && clientNum == cg.snap->ps.clientNum) { +// return; +// } +//----(SA) end + + // it is possible to see corpses from disconnected players that may + // not have valid clientinfo + if ( !ci->infoValid ) { + return; + } + + // check time + if ( cent->pe.lastTime > cg.time ) { + CG_ResetPlayerEntity( cent ); + } + cent->pe.lastTime = cg.time; + + memset( &legs, 0, sizeof( legs ) ); + memset( &torso, 0, sizeof( torso ) ); + memset( &head, 0, sizeof( head ) ); + memset( &acc, 0, sizeof( acc ) ); + + // get the rotation information + CG_PlayerAngles( cent, legs.axis, torso.axis, head.axis ); + +//----(SA) added + // setting the legs axis should pass the scale down through the other parts/tags/etc. + // and will need to be 'undone' for the weapon + if ( ci->playermodelScale[0] ) { + VectorScale( legs.axis[0], ci->playermodelScale[0], legs.axis[0] ); + VectorScale( legs.axis[1], ci->playermodelScale[1], legs.axis[1] ); + VectorScale( legs.axis[2], ci->playermodelScale[2], legs.axis[2] ); + legs.nonNormalizedAxes = qtrue; + torso.nonNormalizedAxes = qtrue; + head.nonNormalizedAxes = qtrue; + } +//----(SA) end + + // copy the torso rotation to the accessories + AxisCopy( torso.axis, acc.axis ); + + // calculate client-side conditions + CG_AnimPlayerConditions( cent ); + + // get the animation state (after rotation, to allow feet shuffle) + CG_PlayerAnimation( cent, &legs.oldframe, &legs.frame, &legs.backlerp, + &torso.oldframe, &torso.frame, &torso.backlerp ); + + // add powerups floating behind the player + CG_PlayerPowerups( cent ); + + // add the talk baloon or disconnect icon + CG_PlayerSprites( cent ); + + // add a water splash if partially in and out of water + CG_PlayerSplash( cent ); + + // get the player model information + renderfx = 0; + if ( cent->currentState.number == cg.snap->ps.clientNum && !cg.renderingThirdPerson ) { + renderfx = RF_THIRD_PERSON; // only draw in mirrors + } + + // draw the player in cameras +// if(cg.cameraMode) +// renderfx &= ~RF_THIRD_PERSON; + + if ( cg_shadows.integer == 3 && shadow ) { + renderfx |= RF_SHADOW_PLANE; + } + renderfx |= RF_LIGHTING_ORIGIN; // use the same origin for all + + // set renderfx for accessories + acc.renderfx = renderfx; + +//CG_Printf("%i cl_org: %s\n", clientNum, vtosf(cent->lerpOrigin) ); + + VectorCopy( cent->lerpOrigin, lightorigin ); + lightorigin[2] += 31 + (float)cg_drawFPGun.integer; + + // dead sink? + CG_DeadSink( cent ); + + // + // add special monster effects here + // + CG_AddZombieSpiritEffect( cent ); + CG_AddZombieFlameEffect( cent ); + CG_AddZombieFlameShort( cent ); + CG_AddLoperLightningEffect( cent ); + CG_AddLoperGroundEffect( cent ); + CG_AddHelgaSpiritEffect( cent ); + + // + // add dynamic lights, and other misc effects + // + CG_AddFireLight( cent ); + + // + // add the legs + // + legs.hModel = ci->legsModel; + legs.customSkin = ci->legsSkin; + + VectorCopy( cent->lerpOrigin, legs.origin ); + + if ( ci->playermodelScale[0] != 0 ) { // player scaled, adjust for the (-24) offset of player legs origin to ground + legs.origin[2] -= 24.0f * ( 1.0f - ci->playermodelScale[2] ); + } + + VectorCopy( lightorigin, legs.lightingOrigin ); + legs.shadowPlane = shadowPlane; + legs.renderfx = renderfx; + VectorCopy( legs.origin, legs.oldorigin ); // don't positionally lerp at all + + if ( !ci->isSkeletal ) { + CG_AddRefEntityWithPowerups( &legs, cent->currentState.powerups, ci->team, ¢->currentState, cent->fireRiseDir ); + } + + cent->pe.legsRefEnt = legs; + + // if the model failed, allow the default nullmodel to be displayed + if ( !legs.hModel ) { + return; + } + + // (SA) only need to set this once... + VectorCopy( lightorigin, acc.lightingOrigin ); + + + // + // add the torso + // + torso.hModel = ci->torsoModel; + + if ( !torso.hModel ) { + return; + } + + torso.customSkin = ci->torsoSkin; + + VectorCopy( lightorigin, torso.lightingOrigin ); + + //----(SA) check for ladder and if you're on it, don't allow torso model rotation (so the body climbs aligned with the ladder) + //----(SA) also taking care of the Loper's interesting heirarchy (his upper body is effectively the same as a weapon_hand.md3. it keeps things connected, but has no geometry) + + if ( !ci->isSkeletal ) { + + if ( ( cgsnap == cent && ( cg.snap->ps.pm_flags & PMF_LADDER ) ) + || ( cent->currentState.aiChar == AICHAR_LOPER ) ) { + CG_PositionEntityOnTag( &torso, &legs, "tag_torso", 0, NULL ); + } else { + CG_PositionRotatedEntityOnTag( &torso, &legs, "tag_torso" ); + } + + } else { // just clear out the angles + + if ( ( cgsnap == cent && ( cg.snap->ps.pm_flags & PMF_LADDER ) ) + || ( cent->currentState.aiChar == AICHAR_LOPER ) ) { + memcpy( torso.axis, legs.axis, sizeof( torso.axis ) ); + } + + } + + torso.shadowPlane = shadowPlane; + torso.renderfx = renderfx; + + if ( !ci->isSkeletal ) { + + CG_AddRefEntityWithPowerups( &torso, cent->currentState.powerups, ci->team, ¢->currentState, cent->fireRiseDir ); + + } else { // SKELETAL ANIMATION + + // skeletal models combine the legs and torso, so we must build a compiled refEntity_t + + legs.torsoFrame = torso.frame; + legs.oldTorsoFrame = torso.oldframe; + + memcpy( legs.torsoAxis, torso.axis, sizeof( torso.axis ) ); + legs.torsoBacklerp = torso.backlerp; + + CG_AddRefEntityWithPowerups( &legs, cent->currentState.powerups, ci->team, ¢->currentState, cent->fireRiseDir ); + + cent->pe.legsRefEnt = legs; + torso = legs; // so tag calls use the correct values + } + + cent->pe.torsoRefEnt = torso; + + +// if ( cent->currentState.eFlags & EF_DEAD) { +// if(cent->currentState.eFlags & EF_HEADSHOT) +// // dead guy with no head +// return; +// } + + // + // add the head + // + head.hModel = ci->headModel; + if ( !head.hModel ) { + return; + } + head.customSkin = ci->headSkin; + + VectorCopy( lightorigin, head.lightingOrigin ); + + CG_PositionRotatedEntityOnTag( &head, &torso, "tag_head" ); + + head.shadowPlane = shadowPlane; + head.renderfx = renderfx; + + + // Ridah, talking animations + if ( !( cent->currentState.eFlags & EF_DEAD ) ) { + #define HEAD_EMOTION_SUBTYPES 8 // closed, A, O, I, E + int talk_frame; //, subtype; + qboolean closed; + + #define NUM_EMOTIONS 2 // 0 neutral, 1 happy, 2 angry + int emotion = 0; // this should default to the entity's current emotion + + gumsflappin = (float)trap_S_GetVoiceAmplitude( clientNum ); + talk_frame = (int)floor( ( HEAD_EMOTION_SUBTYPES - 1 ) * ( gumsflappin / 256.0 ) ); + + // add the current frame to the total, so when it comes to pick a new frame, we choose the average + // of those we missed over the last frame + cent->pe.head.animationTime += talk_frame; + cent->pe.head.animationNumber++; + + // if we are starting a talk after idling, hurry up the animation + if ( ( cent->pe.head.frameTime > cg.time + 30 ) && cent->pe.head.animationTime && !( ( cent->pe.head.frame - ci->modelInfo->headAnims[0].firstFrame ) % HEAD_EMOTION_SUBTYPES ) && !( ( cent->pe.head.oldFrame - ci->modelInfo->headAnims[0].firstFrame ) % HEAD_EMOTION_SUBTYPES ) ) { + cent->pe.head.frameTime = cg.time + 30; + } + + if ( cent->pe.head.frameTime < cg.time ) { // set the new frame + cent->pe.head.oldFrame = cent->pe.head.frame; + cent->pe.head.oldFrameTime = cent->pe.head.frameTime; + + if ( cent->pe.head.animationTime ) { + + talk_frame = (int)( (float)cent->pe.head.animationTime / (float)cent->pe.head.animationNumber ); + //subtype = rand()%HEAD_EMOTION_SUBTYPES; + emotion = rand() % NUM_EMOTIONS; // this helps animate the mouth more realistically while talking + + switch ( cent->currentState.aiChar ) { + case AICHAR_ZOMBIE: + case AICHAR_LOPER: + talk_frame = (int)( (float)talk_frame * 1.2 ); + closed = qfalse; + break; + default: + // randomly set it back to 0 amplitude to simulate certain synonyms pronounced with a closed mouth + closed = ( ( rand() % 5 ) == 0 ) && ( talk_frame < ( HEAD_EMOTION_SUBTYPES - 1 ) ); + } + + if ( closed ) { + talk_frame -= rand() % ( HEAD_EMOTION_SUBTYPES / 2 ); + if ( talk_frame < 0 ) { + talk_frame = 0; + } + } + + if ( talk_frame >= HEAD_EMOTION_SUBTYPES ) { + talk_frame = HEAD_EMOTION_SUBTYPES - 1; + } + + cent->pe.head.frame = emotion * HEAD_EMOTION_SUBTYPES + talk_frame; //ci->modelInfo->headAnims[emotion*HEAD_EMOTION_SUBTYPES].firstFrame + talk_frame; + cent->pe.head.frameTime = cg.time + 80 + rand() % 40; // interpolate for smoother animation vs latency + + //CG_Printf("%i head: frame %i, oldframe %i, nextframetime %i\n", cg.time, cent->pe.head.frame, cent->pe.head.oldFrame, cent->pe.head.frameTime ); + + //if (closed) + // cent->pe.head.frameTime += 30; // slow it down a bit + } else { +#if 0 + // debugging, play all the frames and display the frame numbers + if ( ++cent->pe.head.frame > ( ci->headAnims[( NUM_EMOTIONS - 1 ) * HEAD_EMOTION_SUBTYPES + HEAD_EMOTION_SUBTYPES - 1].firstFrame ) ) { + cent->pe.head.frame = ci->headAnims[0].firstFrame; + } + cent->pe.head.frameTime = cg.time + 2000; + CG_Printf( "%d - %d\n", cent->currentState.number, cent->pe.head.oldFrame ); +#else + while ( ( emotion = rand() % NUM_EMOTIONS ) == 1 ) ; // don't use happy emotion + if ( ( cent->pe.head.frame - ci->modelInfo->headAnims[0].firstFrame ) % HEAD_EMOTION_SUBTYPES ) { + cent->pe.head.frame = ci->modelInfo->headAnims[emotion * HEAD_EMOTION_SUBTYPES].firstFrame; + cent->pe.head.frameTime = cg.time + 150; + } else { // mouth is currently closed + cent->pe.head.frame = ci->modelInfo->headAnims[emotion * HEAD_EMOTION_SUBTYPES].firstFrame; + cent->pe.head.frameTime = cent->pe.head.frameTime + 1000 + rand() % 2000; + if ( cent->pe.head.frameTime < cg.time ) { + cent->pe.head.frameTime = cg.time + 1000; + } + } +#endif + } + + cent->pe.head.animationTime = 0; + cent->pe.head.animationNumber = 0; + } + + head.frame = cent->pe.head.frame; + head.oldframe = cent->pe.head.oldFrame; + head.backlerp = 1.0 - (float)( cg.time - cent->pe.head.oldFrameTime ) / ( cent->pe.head.frameTime - cent->pe.head.oldFrameTime ); + + } else { // dead + head.frame = 0; + head.oldframe = 0; + head.backlerp = 0.0; + } + // done. + + + + // set blinking flag + if ( cent->currentState.eFlags & EF_DEAD ) { + // dead guy, eyes closed. + head.renderfx |= RF_BLINK; + + } else if ( ci->blinkTime <= cg.time ) { + + head.renderfx |= RF_BLINK; + + if ( ci->blinkTime <= ( cg.time - cg_blinktime.integer ) ) { + ci->blinkTime = cg.time + 500 + random() * 4000; + + // blink more often when talking + if ( gumsflappin >= 0 ) { + ci->blinkTime = max( cg.time, ci->blinkTime - 1000 ); + } + } + } + + + CG_AddRefEntityWithPowerups( &head, cent->currentState.powerups, ci->team, ¢->currentState, cent->fireRiseDir ); + + cent->pe.headRefEnt = head; + + // add the shadow + shadow = CG_PlayerShadow( cent, &shadowPlane ); + + // set the shadowplane for accessories + acc.shadowPlane = shadowPlane; + + CG_BreathPuffs( cent, &head ); + + // + // add the gun / barrel / flash + // + CG_AddPlayerWeapon( &torso, NULL, cent ); + + cent->lastWeaponClientFrame = cg.clientFrame; + + // + // add binoculars (if it's not the player) + // + if ( ( cent->currentState.clientNum != cg.snap->ps.clientNum ) && + cent->currentState.eFlags & EF_ZOOMING ) { + + acc.hModel = cgs.media.thirdPersonBinocModel; + CG_PositionEntityOnTag( &acc, &torso, "tag_weapon", 0, NULL ); + CG_AddRefEntityWithPowerups( &acc, cent->currentState.powerups, ci->team, ¢->currentState, cent->fireRiseDir ); + } + + if ( ( cent->currentState.clientNum != cg.snap->ps.clientNum ) && + cent->currentState.eFlags & EF_CIG ) { + + acc.hModel = cgs.media.cigModel; + CG_PositionEntityOnTag( &acc, &torso, "tag_weapon2", 0, NULL ); + CG_AddRefEntityWithPowerups( &acc, cent->currentState.powerups, ci->team, ¢->currentState, cent->fireRiseDir ); + + // smoke + if ( !cg_paused.integer ) { // don't add while paused + if ( !( rand() % 3 ) ) { + CG_ParticleImpactSmokePuffExtended( cgs.media.smokeParticleShader, acc.origin, tv( 0,0,1 ), 1, 1000, 6, 4, 10, 0.15f ); + } + } + } + + + + // + // add player specific models + // + + if ( cent->currentState.aiChar == AICHAR_LOPER ) { + if ( ci->partModels[8] ) { + vec3_t angles; + + acc.hModel = ci->partModels[8]; + VectorClear( angles ); + angles[YAW] = CG_SpinnerSpinAngle( cent ); + AnglesToAxis( angles, acc.axis ); + CG_PositionRotatedEntityOnTag( &acc, &legs, "tag_spinner" ); + CG_AddRefEntityWithPowerups( &acc, cent->currentState.powerups, ci->team, ¢->currentState, cent->fireRiseDir ); + } + } +//----(SA) modified + else if ( cent->currentState.aiChar == AICHAR_PROTOSOLDIER || + cent->currentState.aiChar == AICHAR_SUPERSOLDIER || + cent->currentState.aiChar == AICHAR_HEINRICH ) { + + char *protoTags[] = { "tag_chest", + "tag_calfleft", + "tag_armleft", + "tag_back", + "tag_legleft", + "tag_calfright", + "tag_armright", + "tag_back", + "tag_legright"}; + + char *ssTags[] = { "tag_chest", + "tag_calfleft", + "tag_armleft", + "tag_back", + "tag_legleft", + "tag_calfright", + "tag_armright", + "tag_back", + "tag_legright", + + "tag_footleft", + "tag_footright", + "tag_sholeft", + "tag_shoright", + "tag_torso", + "tag_calfleft", + "tag_calfright"}; + + char *heinrichTags[] = {"tag_chest", + "tag_calfleft", + "tag_armleft", + "tag_back", + "tag_legleft", + "tag_calfright", + "tag_armright", + "tag_back", + "tag_legright", + + "tag_footleft", + "tag_footright", + "tag_sholeft", + "tag_shoright", + "tag_torso", + "tag_legleft", + "tag_legright", + + "tag_sholeft", + "tag_shoright", + "tag_legleft", + "tag_legright", + "tag_legleft", + "tag_legright"}; + + // TTimo: init + int totalparts = 0, dynamicparts = 0, protoParts = 9, superParts = 16, heinrichParts = 22; + char **tags = NULL; + qhandle_t *models = NULL; + int dmgbits = 16; // 32/2; + + if ( cent->currentState.aiChar == AICHAR_PROTOSOLDIER ) { + tags = &protoTags[0]; + models = &cgs.media.protoArmor[0]; + dynamicparts = totalparts = protoParts; + } else if ( cent->currentState.aiChar == AICHAR_SUPERSOLDIER ) { + tags = &ssTags[0]; + models = &cgs.media.superArmor[0]; + dynamicparts = 14; // the other two stay permanent + totalparts = superParts; + } else if ( cent->currentState.aiChar == AICHAR_HEINRICH ) { + tags = &heinrichTags[0]; + models = &cgs.media.heinrichArmor[0]; + dynamicparts = 20; // will get kicked down to 16 + totalparts = heinrichParts; + } + + if ( dynamicparts > dmgbits ) { + dynamicparts = dmgbits; + } + + for ( i = 0; i < totalparts; i++ ) { + if ( ( i >= dynamicparts ) || ( !( cent->currentState.dmgFlags & ( 1 << i ) ) ) ) { // ones beyond 16 just draw the good part + acc.hModel = models[i]; + } else { + if ( cent->currentState.dmgFlags & ( 1 << ( i + totalparts ) ) ) { + acc.hModel = models[i + totalparts]; + } else { + acc.hModel = models[i + ( 2 * totalparts )]; + } + } + + if ( !acc.hModel ) { + continue; + } + + CG_PositionEntityOnTag( &acc, &torso, tags[i], 0, NULL ); + + if ( cent->currentState.aiChar == AICHAR_PROTOSOLDIER && !( cent->currentState.eFlags & EF_DEAD ) ) { + if ( acc.hModel != models[i] ) { + vec3_t dir; + int mynum; + + VectorSubtract( acc.origin, cent->currentState.pos.trBase, dir ); + dir[2] += 20; + VectorNormalize( dir ); + + mynum = ( rand() % 100 ); + if ( mynum < 2 ) { + CG_AddBulletParticles( acc.origin, dir, 30, 10 * mynum, 3, 300.0f ); + } + } + } + + CG_AddRefEntityWithPowerups( &acc, cent->currentState.powerups, ci->team, ¢->currentState, cent->fireRiseDir ); + } + } else if ( cent->currentState.aiChar == AICHAR_WARZOMBIE && + ( !Q_strcasecmp( (char *)ci->modelInfo->modelname, "dark" ) ) ) { + // TTimo: unused + /* + char *tags[] = { "tag_armleft", + "tag_armright", + "tag_back", + "tag_back", + "tag_calfleft", + "tag_calfleft", + "tag_calfright", + "tag_calfright", + "tag_chest", + "tag_chest", + "tag_footleft", + "tag_footright", + "tag_legleft", + "tag_sholeft", + "tag_shoright", + "tag_torso" + }; + */ + +// TTimo: unused + /* +int parts[] = { 34, + 38, + 0, + 19, + 0, + 14, + 21, + 15, + 16, + 0, + 0, + 32, + 0, + 33, + 0, + 45 + }; + */ + +// char *parts[] = { "dam_lftforarm2",// 34 +// "dam_rtforarm2",//38 +// "dam_rtshoulder", +// "dam_lftshoulder1",//19 +// "dam_lftcalf", +// "nodam_lftknee",//14 +// "dam_rtcalf1",//21 +// "nodam_rtknee",//15 +// "dam_chest1",//16 +// "dam_chest3", +// "dam_lftfoot", +// "dam_rtfoot2",//32 +// "dam_lftthigh", +// "dam_lftuparm2",//33 +// "dam_rtuparm", +// "dam_waist2"//45 +// }; + + +// do not turn on unless asked for +/* + for(i=0;i<16;i++) { + acc.hModel = cgs.media.superArmor[parts[i]]; + CG_PositionEntityOnTag( &acc, &torso, tags[i], 0, NULL); + CG_AddRefEntityWithPowerups( &acc, cent->currentState.powerups, ci->team, ¢->currentState, cent->fireRiseDir ); + } +*/ +// + } + + + +//#if 1 +#ifdef TEST_HEADLIGHT + // used for testing spotlights. + // this just puts one in the mouth of every other player so you can + // get a good read of how well the various elements of spots are working +// if(cent->currentState.number != cg.predictedPlayerState.clientNum) + { + vec3_t morg, viewDir; +// vec4_t color = {1,1,1,0.1f}; + vec4_t color = {0.7,0.7,0.7,0.1f}; + + CG_GetOriginForTag( cent, &head, "tag_mouth", 0, morg, NULL ); + AngleVectors( cent->lerpAngles, viewDir, NULL, NULL ); + CG_Spotlight( cent, color, morg, viewDir, 12, 512, 2, 5, SL_TRACEWORLDONLY | SL_NOCORE | SL_LOCKUV ); // SL_NOTRACE +// color[0] = 1; +// color[1] = 1; +// color[2] = 1; + color[3] = 0.1f; + CG_Spotlight( cent, color, morg, viewDir, 12, 512, 1.5, 2, SL_TRACEWORLDONLY | SL_NOCORE | SL_NODLIGHT | SL_LOCKUV ); // SL_NOTRACE + } +#endif + + + // + // add accessories + // + + for ( i = ACC_BELT_LEFT; i < ACC_MAX; i++ ) { + if ( !( ci->accModels[i] ) ) { + continue; + } + + // first 8 can be hidden by animation scripts + if ( i < 8 ) { + if ( cg.snap->ps.accHideBits & ( 1 << i ) ) { + continue; + } + } + + acc.hModel = ci->accModels[i]; // set the model + + if ( ci->accSkins[i] ) { + acc.customSkin = ci->accSkins[i]; // and the skin if there is one + + + } + switch ( i ) { + case ACC_BELT_LEFT: + CG_PositionEntityOnTag( &acc, &legs, "tag_bright", 0, NULL ); + break; + case ACC_BELT_RIGHT: + CG_PositionEntityOnTag( &acc, &legs, "tag_bleft", 0, NULL ); + break; + + case ACC_BELT: + CG_PositionEntityOnTag( &acc, &torso, "tag_ubelt", 0, NULL ); + break; + case ACC_BACK: + CG_PositionEntityOnTag( &acc, &torso, "tag_back", 0, NULL ); + break; + + case ACC_HAT: //hat + if ( cent->currentState.eFlags & EF_HEADSHOT ) { + continue; + } + case ACC_MOUTH2: // hat2 + case ACC_MOUTH3: // hat3 + CG_PositionEntityOnTag( &acc, &head, "tag_mouth", 0, NULL ); + break; + + // weapon and weapon2 + // these are used by characters who have permanent weapons attached to their character in the skin + case ACC_WEAPON: // weap + CG_PositionEntityOnTag( &acc, &torso, "tag_weapon", 0, NULL ); + break; + case ACC_WEAPON2: // weap2 + CG_PositionEntityOnTag( &acc, &torso, "tag_weapon2", 0, NULL ); + break; + + default: + continue; + } + + CG_AddRefEntityWithPowerups( &acc, cent->currentState.powerups, ci->team, ¢->currentState, cent->fireRiseDir ); + } + + for ( i = 0; i < 8; i++ ) { + if ( !( ci->partModels[i] ) ) { + continue; + } + + // first 8 can be hidden by animation scripts + if ( !( cg.snap->ps.accShowBits & ( 1 << i ) ) ) { + continue; + } + + acc.hModel = ci->partModels[i]; // set the model + if ( ci->partSkins[i] ) { + acc.customSkin = ci->partSkins[i]; // and the skin if there is one + + } + CG_PositionEntityOnTag( &acc, &legs, va( "tag_animscript%s", i ), 0, NULL ); + CG_AddRefEntityWithPowerups( &acc, cent->currentState.powerups, ci->team, ¢->currentState, cent->fireRiseDir ); + } + + +} + + +//===================================================================== + +extern void CG_ClearWeapLerpFrame( clientInfo_t *ci, lerpFrame_t *lf, int animationNumber ); + +/* +=============== +CG_ResetPlayerEntity + +A player just came into view or teleported, so reset all animation info +=============== +*/ +void CG_ResetPlayerEntity( centity_t *cent ) { + cent->errorTime = -99999; // guarantee no error decay added + cent->extrapolated = qfalse; + + if ( !( cent->currentState.eFlags & EF_DEAD ) ) { + CG_ClearLerpFrameRate( &cgs.clientinfo[ cent->currentState.clientNum ], ¢->pe.legs, cent->currentState.legsAnim, cent ); + CG_ClearLerpFrame( &cgs.clientinfo[ cent->currentState.clientNum ], ¢->pe.torso, cent->currentState.torsoAnim ); + + memset( ¢->pe.legs, 0, sizeof( cent->pe.legs ) ); + cent->pe.legs.yawAngle = cent->nextState.apos.trBase[YAW]; //cent->rawAngles[YAW]; + cent->pe.legs.yawing = qfalse; + cent->pe.legs.pitchAngle = cent->nextState.apos.trBase[PITCH]; + cent->pe.legs.pitching = qfalse; + + memset( ¢->pe.torso, 0, sizeof( cent->pe.legs ) ); + cent->pe.torso.yawAngle = cent->nextState.apos.trBase[YAW]; //cent->rawAngles[YAW]; + cent->pe.torso.yawing = qfalse; + cent->pe.torso.pitchAngle = cent->nextState.apos.trBase[PITCH]; //cent->rawAngles[PITCH]; + cent->pe.torso.pitching = qfalse; + } + + BG_EvaluateTrajectory( ¢->currentState.pos, cg.time, cent->lerpOrigin ); + BG_EvaluateTrajectory( ¢->currentState.apos, cg.time, cent->lerpAngles ); + + VectorCopy( cent->lerpOrigin, cent->rawOrigin ); + VectorCopy( cent->lerpAngles, cent->rawAngles ); + + if ( cg_debugPosition.integer ) { + CG_Printf( "%i ResetPlayerEntity yaw=%i\n", cent->currentState.number, cent->pe.torso.yawAngle ); + } + + cent->pe.painAnimLegs = -1; + cent->pe.painAnimTorso = -1; + cent->pe.animSpeed = 1.0; + +} + +void CG_GetBleedOrigin( vec3_t head_origin, vec3_t torso_origin, vec3_t legs_origin, int fleshEntityNum ) { + clientInfo_t *ci; + refEntity_t legs; + refEntity_t torso; + refEntity_t head; + //int clientNum; + centity_t *cent, backupCent; + + // clientNum = cg.snap->entities[fleshEntityNum].clientNum; + ci = &cgs.clientinfo[ fleshEntityNum ]; + + // cent = &cg_entities[ cg.snap->entities[fleshEntityNum].number ]; + cent = &cg_entities [ fleshEntityNum ]; + backupCent = *cent; + + // cent = &cg_entities [ cg.snap->entities [ fleshEntityNum - 1].clientNum ]; + + if ( !ci->infoValid ) { + return; + } + + memset( &legs, 0, sizeof( legs ) ); + memset( &torso, 0, sizeof( torso ) ); + memset( &head, 0, sizeof( head ) ); + + CG_PlayerAngles( cent, legs.axis, torso.axis, head.axis ); + CG_PlayerAnimation( cent, &legs.oldframe, &legs.frame, &legs.backlerp, + &torso.oldframe, &torso.frame, &torso.backlerp ); + + legs.hModel = ci->legsModel; +// VectorCopy( cg.snap->entities[fleshEntityNum - 1].pos.trBase, legs.origin ); + VectorCopy( cent->lerpOrigin, legs.origin ); + VectorCopy( legs.origin, legs.oldorigin ); + + // Ridah, restore the cent so we don't interfere with animation timings + *cent = backupCent; + + if ( !legs.hModel ) { + return; + } + + torso.hModel = ci->torsoModel; + if ( !torso.hModel ) { + return; + } + + head.hModel = ci->headModel; + if ( !head.hModel ) { + return; + } + + CG_PositionRotatedEntityOnTag( &torso, &legs, "tag_torso" ); + CG_PositionRotatedEntityOnTag( &head, &torso, "tag_head" ); + + VectorCopy( head.origin, head_origin ); + VectorCopy( torso.origin, torso_origin ); + VectorCopy( legs.origin, legs_origin ); +} + +/* +=============== +CG_GetTag +=============== +*/ +qboolean CG_GetTag( int clientNum, char *tagname, orientation_t *or ) { + clientInfo_t *ci; + centity_t *cent; + refEntity_t *refent; + vec3_t tempAxis[3]; + vec3_t org; + int i; + + ci = &cgs.clientinfo[ clientNum ]; + + if ( !ci->isSkeletal ) { + return qfalse; // only skeletal models supported + + } + if ( cg.snap && clientNum == cg.snap->ps.clientNum && cg.renderingThirdPerson ) { + cent = &cg.predictedPlayerEntity; + } else { + cent = &cg_entities[ci->clientNum]; + if ( !cent->currentValid ) { + return qfalse; // not currently in PVS + } + } + + refent = ¢->pe.legsRefEnt; + + if ( trap_R_LerpTag( or, refent, tagname, 0 ) < 0 ) { + return qfalse; + } + + VectorCopy( refent->origin, org ); + + for ( i = 0 ; i < 3 ; i++ ) { + VectorMA( org, or->origin[i], refent->axis[i], org ); + } + + VectorCopy( org, or->origin ); + + // add the origin of the entity + //VectorAdd( refent->origin, or->origin, or->origin ); + + // rotate with entity + MatrixMultiply( refent->axis, or->axis, tempAxis ); + memcpy( or->axis, tempAxis, sizeof( vec3_t ) * 3 ); + + return qtrue; +} + +/* +=============== +CG_GetWeaponTag +=============== +*/ +qboolean CG_GetWeaponTag( int clientNum, char *tagname, orientation_t *or ) { + clientInfo_t *ci; + centity_t *cent; + refEntity_t *refent; + vec3_t tempAxis[3]; + vec3_t org; + int i; + + ci = &cgs.clientinfo[ clientNum ]; + + if ( !ci->isSkeletal ) { + return qfalse; // only skeletal models supported + + } + if ( cg.snap && clientNum == cg.snap->ps.clientNum && cg.renderingThirdPerson ) { + cent = &cg.predictedPlayerEntity; + } else { + cent = &cg_entities[ci->clientNum]; + if ( !cent->currentValid ) { + return qfalse; // not currently in PVS + } + } + + if ( cent->pe.gunRefEntFrame < cg.clientFrame - 1 ) { + return qfalse; + } + + refent = ¢->pe.gunRefEnt; + + if ( trap_R_LerpTag( or, refent, tagname, 0 ) < 0 ) { + return qfalse; + } + + VectorCopy( refent->origin, org ); + + for ( i = 0 ; i < 3 ; i++ ) { + VectorMA( org, or->origin[i], refent->axis[i], org ); + } + + VectorCopy( org, or->origin ); + + // add the origin of the entity + //VectorAdd( refent->origin, or->origin, or->origin ); + + // rotate with entity + MatrixMultiply( refent->axis, or->axis, tempAxis ); + memcpy( or->axis, tempAxis, sizeof( vec3_t ) * 3 ); + + return qtrue; +} diff --git a/src/cgame/cg_playerstate.c b/src/cgame/cg_playerstate.c new file mode 100644 index 0000000..e19b5a4 --- /dev/null +++ b/src/cgame/cg_playerstate.c @@ -0,0 +1,555 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + + +// cg_playerstate.c -- this file acts on changes in a new playerState_t +// With normal play, this will be done after local prediction, but when +// following another player or playing back a demo, it will be checked +// when the snapshot transitions like all the other entities + +#include "cg_local.h" + +/* +============== +CG_CheckAmmo + +If the ammo has gone low enough to generate the warning, play a sound +============== +*/ +void CG_CheckAmmo( void ) { + int i; + int total; + int weapons[MAX_WEAPONS / ( sizeof( int ) * 8 )]; + + // see about how many seconds of ammo we have remaining + memcpy( weapons, cg.snap->ps.weapons, sizeof( weapons ) ); + + if ( !weapons[0] && !weapons[1] ) { // (SA) we start out with no weapons, so don't make a click on startup + return; + } + + total = 0; + + // first weap now WP_LUGER + for ( i = WP_FIRST ; i < WP_NUM_WEAPONS ; i++ ) + { + if ( !( weapons[0] & ( 1 << i ) ) ) { + continue; + } + switch ( i ) + { + case WP_PANZERFAUST: + case WP_GRENADE_LAUNCHER: + case WP_GRENADE_PINEAPPLE: + case WP_LUGER: + case WP_COLT: + case WP_AKIMBO: + case WP_SILENCER: + case WP_FG42: + case WP_FG42SCOPE: + case WP_MP40: + case WP_THOMPSON: + case WP_STEN: + case WP_VENOM: + case WP_TESLA: + case WP_MAUSER: + case WP_GARAND: + default: + total += cg.snap->ps.ammo[BG_FindAmmoForWeapon( i )] * 1000; +// break; +// default: +// total += cg.snap->ps.ammo[BG_FindAmmoForWeapon(i)] * 200; +// break; + } + + if ( total >= 5000 ) { + cg.lowAmmoWarning = 0; + return; + } + } + + if ( !cg.lowAmmoWarning ) { + // play a sound on this transition + trap_S_StartLocalSound( cgs.media.noAmmoSound, CHAN_LOCAL_SOUND ); + } + + if ( total == 0 ) { + cg.lowAmmoWarning = 2; + } else { + cg.lowAmmoWarning = 1; + } +} + +/* +============== +CG_DamageFeedback +============== +*/ +void CG_DamageFeedback( int yawByte, int pitchByte, int damage ) { + float left, front, up; + float kick; + int health; + float scale; + vec3_t dir; + vec3_t angles; + float dist; + float yaw, pitch; + int slot; + viewDamage_t *vd; + + // show the attacking player's head and name in corner + cg.attackerTime = cg.time; + + // the lower on health you are, the greater the view kick will be + health = cg.snap->ps.stats[STAT_HEALTH]; + if ( health < 40 ) { + scale = 1; + } else { + scale = 40.0 / health; + } + kick = damage * scale; + + if ( kick < 5 ) { + kick = 5; + } + if ( kick > 10 ) { + kick = 10; + } + + // find a free slot + for ( slot = 0; slot < MAX_VIEWDAMAGE; slot++ ) { + if ( cg.viewDamage[slot].damageTime + cg.viewDamage[slot].damageDuration < cg.time ) { + break; + } + } + + if ( slot == MAX_VIEWDAMAGE ) { + return; // no free slots, never override or splats will suddenly disappear + + } + vd = &cg.viewDamage[slot]; + + // if yaw and pitch are both 255, make the damage always centered (falling, etc) + if ( yawByte == 255 && pitchByte == 255 ) { + vd->damageX = 0; + vd->damageY = 0; + cg.v_dmg_roll = 0; + cg.v_dmg_pitch = -kick; + } else { + // positional + pitch = pitchByte / 255.0 * 360; + yaw = yawByte / 255.0 * 360; + + angles[PITCH] = pitch; + angles[YAW] = yaw; + angles[ROLL] = 0; + + AngleVectors( angles, dir, NULL, NULL ); + VectorSubtract( vec3_origin, dir, dir ); + + front = DotProduct( dir, cg.refdef.viewaxis[0] ); + left = DotProduct( dir, cg.refdef.viewaxis[1] ); + up = DotProduct( dir, cg.refdef.viewaxis[2] ); + + dir[0] = front; + dir[1] = left; + dir[2] = 0; + dist = VectorLength( dir ); + if ( dist < 0.1 ) { + dist = 0.1; + } + + cg.v_dmg_roll = kick * left; + + cg.v_dmg_pitch = -kick * front; + + if ( front <= 0.1 ) { + front = 0.1; + } + vd->damageX = crandom() * 0.3 + - left / front; + vd->damageY = crandom() * 0.3 + up / dist; + } + + // clamp the position + if ( vd->damageX > 1.0 ) { + vd->damageX = 1.0; + } + if ( vd->damageX < -1.0 ) { + vd->damageX = -1.0; + } + + if ( vd->damageY > 1.0 ) { + vd->damageY = 1.0; + } + if ( vd->damageY < -1.0 ) { + vd->damageY = -1.0; + } + + // don't let the screen flashes vary as much + if ( kick > 10 ) { + kick = 10; + } + vd->damageValue = kick; + cg.v_dmg_time = cg.time + DAMAGE_TIME; + vd->damageTime = cg.snap->serverTime; + vd->damageDuration = kick * 50 * ( 1 + 2 * ( !vd->damageX && !vd->damageY ) ); + cg.damageTime = cg.snap->serverTime; + cg.damageIndex = slot; +} + + + + +/* +================ +CG_Respawn + +A respawn happened this snapshot +================ +*/ +void CG_Respawn( void ) { + // no error decay on player movement + cg.thisFrameTeleport = qtrue; + + // need to reset client-side weapon animations + cg.predictedPlayerState.weapAnim = WEAP_IDLE1; // reset weapon animations + cg.predictedPlayerState.weapAnimTimer = 0; // allow other animations to happen right away + cg.predictedPlayerState.weaponstate = WEAPON_RAISING; // hmm, set this? what to? + + // display weapons available + cg.weaponSelectTime = cg.time; + + cg.holdableSelectTime = 0; //----(SA) reset holdable timer + cg.centerPrintTime = 0; //----(SA) reset centerprint counter so previous messages don't re-appear + cg.cursorHintIcon = 0; + cg.cursorHintTime = 0; + cg.yougotmailTime = 0; + +// cg.cameraMode = 0; //----(SA) get out of camera for sure + + // select the weapon the server says we are using + //cg.weaponSelect = cg.snap->ps.weapon; + // DHM - Nerve :: Clear even more things on respawn + cg.zoomedBinoc = qfalse; + cg.zoomedBinoc = cg.zoomedScope = qfalse; + cg.zoomTime = 0; + cg.zoomval = 0; + + // reset fog to world fog (if present) +// trap_R_SetFog(FOG_CMD_SWITCHFOG, FOG_MAP,20,0,0,0,0); + + // dhm - end + + trap_Cvar_Set( "cg_notebookpages", "3" ); // (SA) TEMP: clear notebook pages on spawn (cept for page 1&2) this is temporary + trap_Cvar_Set( "ui_notebookCurrentPage", "0" ); // (SA) TEMP: clear notebook pages on spawn (cept for page 1) this is temporary + + +// trap_S_FadeAllSound(1.0f, 1000); // make sure sound fades up + +} + +extern char *eventnames[]; + +/* +============== +CG_CheckPlayerstateEvents +============== +*/ +void CG_CheckPlayerstateEvents_wolf( playerState_t *ps, playerState_t *ops ) { + int i; + int event; + centity_t *cent; +/* + if ( ps->externalEvent && ps->externalEvent != ops->externalEvent ) { + cent = &cg_entities[ ps->clientNum ]; + cent->currentState.event = ps->externalEvent; + cent->currentState.eventParm = ps->externalEventParm; + CG_EntityEvent( cent, cent->lerpOrigin ); + } +*/ + cent = &cg.predictedPlayerEntity; // cg_entities[ ps->clientNum ]; + // go through the predictable events buffer + for ( i = ps->eventSequence - MAX_EVENTS ; i < ps->eventSequence ; i++ ) { + if ( ps->events[i & ( MAX_EVENTS - 1 )] != ops->events[i & ( MAX_EVENTS - 1 )] + || i >= ops->eventSequence ) { + event = ps->events[ i & ( MAX_EVENTS - 1 ) ]; + + cent->currentState.event = event; + cent->currentState.eventParm = ps->eventParms[ i & ( MAX_EVENTS - 1 ) ]; + CG_EntityEvent( cent, cent->lerpOrigin ); + } + } +} + +void CG_CheckPlayerstateEvents( playerState_t *ps, playerState_t *ops ) { + int i; + int event; + centity_t *cent; + + if ( ps->externalEvent && ps->externalEvent != ops->externalEvent ) { + cent = &cg_entities[ ps->clientNum ]; + cent->currentState.event = ps->externalEvent; + cent->currentState.eventParm = ps->externalEventParm; + CG_EntityEvent( cent, cent->lerpOrigin ); + } + + cent = &cg.predictedPlayerEntity; // cg_entities[ ps->clientNum ]; + // go through the predictable events buffer + for ( i = ps->eventSequence - MAX_EVENTS ; i < ps->eventSequence ; i++ ) { + // if we have a new predictable event + if ( i >= ops->eventSequence + // or the server told us to play another event instead of a predicted event we already issued + // or something the server told us changed our prediction causing a different event + || ( i > ops->eventSequence - MAX_EVENTS && ps->events[i & ( MAX_EVENTS - 1 )] != ops->events[i & ( MAX_EVENTS - 1 )] ) ) { + + event = ps->events[ i & ( MAX_EVENTS - 1 ) ]; + cent->currentState.event = event; + cent->currentState.eventParm = ps->eventParms[ i & ( MAX_EVENTS - 1 ) ]; + CG_EntityEvent( cent, cent->lerpOrigin ); + + cg.predictableEvents[ i & ( MAX_PREDICTED_EVENTS - 1 ) ] = event; + + cg.eventSequence++; + } + } +} + +/* +================== +CG_CheckChangedPredictableEvents +================== +*/ +void CG_CheckChangedPredictableEvents( playerState_t *ps ) { + int i; + int event; + centity_t *cent; + + cent = &cg.predictedPlayerEntity; + for ( i = ps->eventSequence - MAX_EVENTS ; i < ps->eventSequence ; i++ ) { + // + if ( i >= cg.eventSequence ) { + continue; + } + // if this event is not further back in than the maximum predictable events we remember + if ( i > cg.eventSequence - MAX_PREDICTED_EVENTS ) { + // if the new playerstate event is different from a previously predicted one + if ( ps->events[i & ( MAX_EVENTS - 1 )] != cg.predictableEvents[i & ( MAX_PREDICTED_EVENTS - 1 ) ] ) { + + event = ps->events[ i & ( MAX_EVENTS - 1 ) ]; + cent->currentState.event = event; + cent->currentState.eventParm = ps->eventParms[ i & ( MAX_EVENTS - 1 ) ]; + CG_EntityEvent( cent, cent->lerpOrigin ); + + cg.predictableEvents[ i & ( MAX_PREDICTED_EVENTS - 1 ) ] = event; + + if ( cg_showmiss.integer ) { + CG_Printf( "WARNING: changed predicted event\n" ); + } + } + } + } +} + + +/* +================== +CG_CheckLocalSounds +================== +*/ +void CG_CheckLocalSounds( playerState_t *ps, playerState_t *ops ) { + const char *s; + int highScore; + + // hit changes + if ( ps->persistant[PERS_HITS] > ops->persistant[PERS_HITS] ) { + trap_S_StartLocalSound( cgs.media.hitSound, CHAN_LOCAL_SOUND ); + } else if ( ps->persistant[PERS_HITS] < ops->persistant[PERS_HITS] ) { + trap_S_StartLocalSound( cgs.media.hitTeamSound, CHAN_LOCAL_SOUND ); + } + + // health changes of more than -1 should make pain sounds + if ( ps->stats[STAT_HEALTH] < ops->stats[STAT_HEALTH] - 1 ) { + if ( ps->stats[STAT_HEALTH] > 0 ) { + CG_PainEvent( &cg.predictedPlayerEntity, ps->stats[STAT_HEALTH], qfalse ); + } + } + + + // if we are going into the intermission, don't start any voices + if ( cg.intermissionStarted ) { + return; + } + + // reward sounds + if ( ps->persistant[PERS_REWARD_COUNT] > ops->persistant[PERS_REWARD_COUNT] ) { + switch ( ps->persistant[PERS_REWARD] ) { + case REWARD_IMPRESSIVE: + trap_S_StartLocalSound( cgs.media.impressiveSound, CHAN_ANNOUNCER ); + cg.rewardTime = cg.time; + cg.rewardShader = cgs.media.medalImpressive; + cg.rewardCount = ps->persistant[PERS_IMPRESSIVE_COUNT]; + break; + case REWARD_EXCELLENT: + trap_S_StartLocalSound( cgs.media.excellentSound, CHAN_ANNOUNCER ); + cg.rewardTime = cg.time; + cg.rewardShader = cgs.media.medalExcellent; + cg.rewardCount = ps->persistant[PERS_EXCELLENT_COUNT]; + break; + case REWARD_DENIED: + trap_S_StartLocalSound( cgs.media.deniedSound, CHAN_ANNOUNCER ); + break; + case REWARD_GAUNTLET: + trap_S_StartLocalSound( cgs.media.humiliationSound, CHAN_ANNOUNCER ); + // if we are the killer and not the killee, show the award + if ( ps->stats[STAT_HEALTH] ) { + cg.rewardTime = cg.time; + cg.rewardShader = cgs.media.medalGauntlet; + cg.rewardCount = ps->persistant[PERS_GAUNTLET_FRAG_COUNT]; + } + break; + default: + CG_Error( "Bad reward_t" ); + } + } else { + // lead changes (only if no reward) + s = CG_ConfigString( CS_WARMUP ); + if ( !s[0] ) { + // never play lead changes during warmup + if ( ps->persistant[PERS_RANK] != ops->persistant[PERS_RANK] ) { + if ( cgs.gametype >= GT_TEAM ) { + if ( ps->persistant[PERS_RANK] == 2 ) { + trap_S_StartLocalSound( cgs.media.teamsTiedSound, CHAN_ANNOUNCER ); + } else if ( ps->persistant[PERS_RANK] == 0 ) { + trap_S_StartLocalSound( cgs.media.redLeadsSound, CHAN_ANNOUNCER ); + } else if ( ps->persistant[PERS_RANK] == 1 ) { + trap_S_StartLocalSound( cgs.media.blueLeadsSound, CHAN_ANNOUNCER ); + } + } else { + if ( ps->persistant[PERS_RANK] == 0 ) { + trap_S_StartLocalSound( cgs.media.takenLeadSound, CHAN_ANNOUNCER ); + } else if ( ps->persistant[PERS_RANK] == RANK_TIED_FLAG ) { + trap_S_StartLocalSound( cgs.media.tiedLeadSound, CHAN_ANNOUNCER ); + } else if ( ( ops->persistant[PERS_RANK] & ~RANK_TIED_FLAG ) == 0 ) { + trap_S_StartLocalSound( cgs.media.lostLeadSound, CHAN_ANNOUNCER ); + } + } + } + } + } + + // timelimit warnings + if ( cgs.timelimit > 0 ) { + int msec; + + msec = cg.time - cgs.levelStartTime; + + if ( cgs.timelimit > 5 && !( cg.timelimitWarnings & 1 ) && msec > ( cgs.timelimit - 5 ) * 60 * 1000 ) { + cg.timelimitWarnings |= 1; + trap_S_StartLocalSound( cgs.media.fiveMinuteSound, CHAN_ANNOUNCER ); + } + if ( !( cg.timelimitWarnings & 2 ) && msec > ( cgs.timelimit - 1 ) * 60 * 1000 ) { + cg.timelimitWarnings |= 2; + trap_S_StartLocalSound( cgs.media.oneMinuteSound, CHAN_ANNOUNCER ); + } + if ( !( cg.timelimitWarnings & 4 ) && msec > ( cgs.timelimit * 60 + 2 ) * 1000 ) { + cg.timelimitWarnings |= 4; + trap_S_StartLocalSound( cgs.media.suddenDeathSound, CHAN_ANNOUNCER ); + } + } + + // fraglimit warnings + if ( cgs.fraglimit > 0 && cgs.gametype != GT_CTF ) { + highScore = cgs.scores1; + if ( cgs.fraglimit > 3 && !( cg.fraglimitWarnings & 1 ) && highScore == ( cgs.fraglimit - 3 ) ) { + cg.fraglimitWarnings |= 1; + trap_S_StartLocalSound( cgs.media.threeFragSound, CHAN_ANNOUNCER ); + } + if ( cgs.fraglimit > 2 && !( cg.fraglimitWarnings & 2 ) && highScore == ( cgs.fraglimit - 2 ) ) { + cg.fraglimitWarnings |= 2; + trap_S_StartLocalSound( cgs.media.twoFragSound, CHAN_ANNOUNCER ); + } + if ( !( cg.fraglimitWarnings & 4 ) && highScore == ( cgs.fraglimit - 1 ) ) { + cg.fraglimitWarnings |= 4; + trap_S_StartLocalSound( cgs.media.oneFragSound, CHAN_ANNOUNCER ); + } + } +} + +/* +=============== +CG_TransitionPlayerState + +=============== +*/ +void CG_TransitionPlayerState( playerState_t *ps, playerState_t *ops ) { + // check for changing follow mode + if ( ps->clientNum != ops->clientNum ) { + cg.thisFrameTeleport = qtrue; + // make sure we don't get any unwanted transition effects + *ops = *ps; + + // DHM - Nerve :: After Limbo, make sure and do a CG_Respawn + if ( ps->clientNum == cg.clientNum ) { + ops->persistant[PERS_SPAWN_COUNT]--; + } + } + + // damage events (player is getting wounded) + if ( ps->damageEvent != ops->damageEvent && ps->damageCount ) { + CG_DamageFeedback( ps->damageYaw, ps->damagePitch, ps->damageCount ); + } + + // respawning + if ( ps->persistant[PERS_SPAWN_COUNT] != ops->persistant[PERS_SPAWN_COUNT] ) { + CG_Respawn(); + } + + if ( cg.mapRestart ) { + CG_Respawn(); + cg.mapRestart = qfalse; + } + + if ( cg.snap->ps.pm_type != PM_INTERMISSION + && ps->persistant[PERS_TEAM] != TEAM_SPECTATOR ) { + CG_CheckLocalSounds( ps, ops ); + } + + // check for going low on ammo + CG_CheckAmmo(); + + // run events + CG_CheckPlayerstateEvents( ps, ops ); + + // smooth the ducking viewheight change + if ( ps->viewheight != ops->viewheight ) { + cg.duckChange = ps->viewheight - ops->viewheight; + cg.duckTime = cg.time; + } +} + diff --git a/src/cgame/cg_predict.c b/src/cgame/cg_predict.c new file mode 100644 index 0000000..0a06e47 --- /dev/null +++ b/src/cgame/cg_predict.c @@ -0,0 +1,765 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + + +// cg_predict.c -- this file generates cg.predictedPlayerState by either +// interpolating between snapshots from the server or locally predicting +// ahead the client's movement. +// It also handles local physics interaction, like fragments bouncing off walls + +#include "cg_local.h" + +static pmove_t cg_pmove; + +static int cg_numSolidEntities; +static centity_t *cg_solidEntities[MAX_ENTITIES_IN_SNAPSHOT]; +static int cg_numTriggerEntities; +static centity_t *cg_triggerEntities[MAX_ENTITIES_IN_SNAPSHOT]; + +/* +==================== +CG_BuildSolidList + +When a new cg.snap has been set, this function builds a sublist +of the entities that are actually solid, to make for more +efficient collision detection +==================== +*/ +void CG_BuildSolidList( void ) { + int i; + centity_t *cent; + snapshot_t *snap; + entityState_t *ent; + + cg_numSolidEntities = 0; + cg_numTriggerEntities = 0; + + if ( cg.nextSnap && !cg.nextFrameTeleport && !cg.thisFrameTeleport ) { + snap = cg.nextSnap; + } else { + snap = cg.snap; + } + + for ( i = 0 ; i < snap->numEntities ; i++ ) { + cent = &cg_entities[ snap->entities[ i ].number ]; + ent = ¢->currentState; + + // RF, dont clip again non-solid bmodels + if ( cent->nextState.solid == SOLID_BMODEL && ( cent->nextState.eFlags & EF_NONSOLID_BMODEL ) ) { + continue; + } + + if ( ent->eType == ET_ITEM || ent->eType == ET_PUSH_TRIGGER || ent->eType == ET_TELEPORT_TRIGGER ) { + cg_triggerEntities[cg_numTriggerEntities] = cent; + cg_numTriggerEntities++; + continue; + } + + if ( cent->nextState.solid ) { + cg_solidEntities[cg_numSolidEntities] = cent; + cg_numSolidEntities++; + continue; + } + } +} + +/* +==================== +CG_ClipMoveToEntities + +==================== +*/ +static void CG_ClipMoveToEntities( const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, + int skipNumber, int mask, int capsule, trace_t *tr ) { + int i, x, zd, zu; + trace_t trace; + entityState_t *ent; + clipHandle_t cmodel; + vec3_t bmins, bmaxs; + vec3_t origin, angles; + centity_t *cent; + + for ( i = 0 ; i < cg_numSolidEntities ; i++ ) { + cent = cg_solidEntities[ i ]; + ent = ¢->currentState; + + if ( ent->number == skipNumber ) { + continue; + } + + // RF, special case, ignore chairs if we are carrying them + if ( ent->eType == ET_PROP && ent->otherEntityNum == skipNumber + 1 ) { + continue; + } + + if ( ent->solid == SOLID_BMODEL ) { + // special value for bmodel + cmodel = trap_CM_InlineModel( ent->modelindex ); +// VectorCopy( cent->lerpAngles, angles ); + BG_EvaluateTrajectory( ¢->currentState.apos, cg.physicsTime, angles ); + BG_EvaluateTrajectory( ¢->currentState.pos, cg.physicsTime, origin ); + } else { + // encoded bbox + x = ( ent->solid & 255 ); + zd = ( ( ent->solid >> 8 ) & 255 ); + zu = ( ( ent->solid >> 16 ) & 255 ) - 32; + + bmins[0] = bmins[1] = -x; + bmaxs[0] = bmaxs[1] = x; + bmins[2] = -zd; + bmaxs[2] = zu; + + // MrE: use bbox or capsule + if ( ent->eFlags & EF_CAPSULE ) { + cmodel = trap_CM_TempCapsuleModel( bmins, bmaxs ); + } else { + cmodel = trap_CM_TempBoxModel( bmins, bmaxs ); + } + VectorCopy( vec3_origin, angles ); + VectorCopy( cent->lerpOrigin, origin ); + } + // MrE: use bbox of capsule + if ( capsule ) { + trap_CM_TransformedCapsuleTrace( &trace, start, end, + mins, maxs, cmodel, mask, origin, angles ); + } else { + trap_CM_TransformedBoxTrace( &trace, start, end, + mins, maxs, cmodel, mask, origin, angles ); + } + + if ( trace.allsolid || trace.fraction < tr->fraction ) { + trace.entityNum = ent->number; + *tr = trace; + } else if ( trace.startsolid ) { + tr->startsolid = qtrue; + } + if ( tr->allsolid ) { + return; + } + } +} + +/* +================ +CG_Trace +================ +*/ +void CG_Trace( trace_t *result, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, + int skipNumber, int mask ) { + trace_t t; + + trap_CM_BoxTrace( &t, start, end, mins, maxs, 0, mask ); + t.entityNum = t.fraction != 1.0 ? ENTITYNUM_WORLD : ENTITYNUM_NONE; + // check all other solid models + CG_ClipMoveToEntities( start, mins, maxs, end, skipNumber, mask, qfalse, &t ); + + *result = t; +} + +/* +================ +CG_TraceCapsule +================ +*/ +void CG_TraceCapsule( trace_t *result, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, + int skipNumber, int mask ) { + trace_t t; + + trap_CM_CapsuleTrace( &t, start, end, mins, maxs, 0, mask ); + t.entityNum = t.fraction != 1.0 ? ENTITYNUM_WORLD : ENTITYNUM_NONE; + // check all other solid models + CG_ClipMoveToEntities( start, mins, maxs, end, skipNumber, mask, qtrue, &t ); + + *result = t; +} + +/* +================ +CG_PointContents +================ +*/ +int CG_PointContents( const vec3_t point, int passEntityNum ) { + int i; + entityState_t *ent; + centity_t *cent; + clipHandle_t cmodel; + int contents; + + contents = trap_CM_PointContents( point, 0 ); + + for ( i = 0 ; i < cg_numSolidEntities ; i++ ) { + cent = cg_solidEntities[ i ]; + + ent = ¢->currentState; + + if ( ent->number == passEntityNum ) { + continue; + } + + if ( ent->solid != SOLID_BMODEL ) { // special value for bmodel + continue; + } + + cmodel = trap_CM_InlineModel( ent->modelindex ); + if ( !cmodel ) { + continue; + } + + contents |= trap_CM_TransformedPointContents( point, cmodel, ent->origin, ent->angles ); + } + + return contents; +} + + +/* +======================== +CG_InterpolatePlayerState + +Generates cg.predictedPlayerState by interpolating between +cg.snap->player_state and cg.nextFrame->player_state +======================== +*/ +static void CG_InterpolatePlayerState( qboolean grabAngles ) { + float f; + int i; + playerState_t *out; + snapshot_t *prev, *next; + + out = &cg.predictedPlayerState; + prev = cg.snap; + next = cg.nextSnap; + + *out = cg.snap->ps; + + // if we are still allowing local input, short circuit the view angles + if ( grabAngles ) { + usercmd_t cmd; + int cmdNum; + + cmdNum = trap_GetCurrentCmdNumber(); + trap_GetUserCmd( cmdNum, &cmd ); + + PM_UpdateViewAngles( out, &cmd, CG_Trace ); + } + + // if the next frame is a teleport, we can't lerp to it + if ( cg.nextFrameTeleport ) { + return; + } + + if ( !next || next->serverTime <= prev->serverTime ) { + return; + } + + f = (float)( cg.time - prev->serverTime ) / ( next->serverTime - prev->serverTime ); + + i = next->ps.bobCycle; + if ( i < prev->ps.bobCycle ) { + i += 256; // handle wraparound + } + out->bobCycle = prev->ps.bobCycle + f * ( i - prev->ps.bobCycle ); + + for ( i = 0 ; i < 3 ; i++ ) { + out->origin[i] = prev->ps.origin[i] + f * ( next->ps.origin[i] - prev->ps.origin[i] ); + if ( !grabAngles ) { + out->viewangles[i] = LerpAngle( + prev->ps.viewangles[i], next->ps.viewangles[i], f ); + } + out->velocity[i] = prev->ps.velocity[i] + + f * ( next->ps.velocity[i] - prev->ps.velocity[i] ); + } + +} + +/* +=================== +CG_TouchItem +=================== +*/ +static void CG_TouchItem( centity_t *cent ) { + gitem_t *item; + + if ( !cg_predictItems.integer ) { + return; + } + +//----(SA) wolf -- not allowing this for single player games +// if( cgs.gametype == GT_SINGLE_PLAYER) { +// return; +// } + +//----(SA) autoactivate + if ( !cg_autoactivate.integer ) { + return; + } +//----(SA) end + + + if ( !BG_PlayerTouchesItem( &cg.predictedPlayerState, ¢->currentState, cg.time ) ) { + return; + } + + // never pick an item up twice in a prediction + if ( cent->miscTime == cg.time ) { + return; + } + + if ( !BG_CanItemBeGrabbed( ¢->currentState, &cg.predictedPlayerState ) ) { + return; // can't hold it + } + + item = &bg_itemlist[ cent->currentState.modelindex ]; + + // (SA) no prediction of books/clipboards + if ( item->giType == IT_HOLDABLE ) { + if ( item->giTag >= HI_BOOK1 && item->giTag <= HI_BOOK3 ) { + return; + } + } + if ( item->giType == IT_CLIPBOARD ) { + return; + } + + // Special case for flags. + // We don't predict touching our own flag + if ( cg.predictedPlayerState.persistant[PERS_TEAM] == TEAM_RED && + item->giTag == PW_REDFLAG ) { + return; + } + if ( cg.predictedPlayerState.persistant[PERS_TEAM] == TEAM_BLUE && + item->giTag == PW_BLUEFLAG ) { + return; + } + + + // grab it + BG_AddPredictableEventToPlayerstate( EV_ITEM_PICKUP, cent->currentState.modelindex, &cg.predictedPlayerState ); + + // remove it from the frame so it won't be drawn + cent->currentState.eFlags |= EF_NODRAW; + + // don't touch it again this prediction + cent->miscTime = cg.time; + + // if its a weapon, give them some predicted ammo so the autoswitch will work + if ( item->giType == IT_WEAPON ) { + int weapon; +//----(SA) added + weapon = item->giTag; + + if ( weapon == WP_COLT ) { + if ( COM_BitCheck( cg.predictedPlayerState.weapons, WP_COLT ) ) { + // you got the colt, you gettin' another + weapon = WP_AKIMBO; + } + } +//----(SA) end + + COM_BitSet( cg.predictedPlayerState.weapons, weapon ); + +//----(SA) added + if ( weapon == WP_SNOOPERSCOPE ) { + COM_BitSet( cg.predictedPlayerState.weapons, WP_GARAND ); + } else if ( weapon == WP_GARAND ) { + COM_BitSet( cg.predictedPlayerState.weapons, WP_SNOOPERSCOPE ); + } else if ( weapon == WP_FG42 ) { + COM_BitSet( cg.predictedPlayerState.weapons, WP_FG42SCOPE ); + } else if ( weapon == WP_SNIPERRIFLE ) { + COM_BitSet( cg.predictedPlayerState.weapons, WP_MAUSER ); + } +//----(SA) end + + if ( !cg.predictedPlayerState.ammo[ BG_FindAmmoForWeapon( weapon )] ) { + cg.predictedPlayerState.ammo[ BG_FindAmmoForWeapon( weapon )] = 1; + } + } + +//----(SA) + if ( item->giType == IT_HOLDABLE ) { + cg.predictedPlayerState.stats[ STAT_HOLDABLE_ITEM ] |= 1 << item->giTag; + } +//----(SA) end +} + + +/* +========================= +CG_TouchTriggerPrediction + +Predict push triggers and items +========================= +*/ +static void CG_TouchTriggerPrediction( void ) { + int i; + trace_t trace; + entityState_t *ent; + clipHandle_t cmodel; + centity_t *cent; + qboolean spectator; + + // dead clients don't activate triggers + if ( cg.predictedPlayerState.stats[STAT_HEALTH] <= 0 ) { + return; + } + + spectator = ( ( cg.predictedPlayerState.pm_type == PM_SPECTATOR ) || ( cg.predictedPlayerState.pm_flags & PMF_LIMBO ) ); // JPW NERVE + + if ( cg.predictedPlayerState.pm_type != PM_NORMAL && !spectator ) { + return; + } + + for ( i = 0 ; i < cg_numTriggerEntities ; i++ ) { + cent = cg_triggerEntities[ i ]; + ent = ¢->currentState; + + if ( ent->eType == ET_ITEM && !spectator ) { + CG_TouchItem( cent ); + continue; + } + + if ( ent->solid != SOLID_BMODEL ) { + continue; + } + + cmodel = trap_CM_InlineModel( ent->modelindex ); + if ( !cmodel ) { + continue; + } + + trap_CM_BoxTrace( &trace, cg.predictedPlayerState.origin, cg.predictedPlayerState.origin, + cg_pmove.mins, cg_pmove.maxs, cmodel, -1 ); + + if ( !trace.startsolid ) { + continue; + } + + if ( ent->eType == ET_TELEPORT_TRIGGER ) { + cg.hyperspace = qtrue; + } else { + float s; + vec3_t dir; + + // we hit this push trigger + if ( spectator ) { + continue; + } + + // flying characters don't hit bounce pads + if ( cg.predictedPlayerState.powerups[PW_FLIGHT] ) { + continue; + } + + // if we are already flying along the bounce direction, don't play sound again + VectorNormalize2( ent->origin2, dir ); + s = DotProduct( cg.predictedPlayerState.velocity, dir ); + if ( s < 500 ) { + // don't play the event sound again if we are in a fat trigger + BG_AddPredictableEventToPlayerstate( EV_JUMP_PAD, 0, &cg.predictedPlayerState ); + } + VectorCopy( ent->origin2, cg.predictedPlayerState.velocity ); + } + } +} + + + +/* +================= +CG_PredictPlayerState + +Generates cg.predictedPlayerState for the current cg.time +cg.predictedPlayerState is guaranteed to be valid after exiting. + +For demo playback, this will be an interpolation between two valid +playerState_t. + +For normal gameplay, it will be the result of predicted usercmd_t on +top of the most recent playerState_t received from the server. + +Each new snapshot will usually have one or more new usercmd over the last, +but we simulate all unacknowledged commands each time, not just the new ones. +This means that on an internet connection, quite a few pmoves may be issued +each frame. + +OPTIMIZE: don't re-simulate unless the newly arrived snapshot playerState_t +differs from the predicted one. Would require saving all intermediate +playerState_t during prediction. (this is "dead reckoning" and would definately +be nice to have in there (SA)) + +We detect prediction errors and allow them to be decayed off over several frames +to ease the jerk. +================= +*/ +void CG_PredictPlayerState( void ) { + int cmdNum, current; + playerState_t oldPlayerState; + qboolean moved; + usercmd_t oldestCmd; + usercmd_t latestCmd; + vec3_t deltaAngles; + + cg.hyperspace = qfalse; // will be set if touching a trigger_teleport + + // if this is the first frame we must guarantee + // predictedPlayerState is valid even if there is some + // other error condition + if ( !cg.validPPS ) { + cg.validPPS = qtrue; + cg.predictedPlayerState = cg.snap->ps; + } + + // demo playback just copies the moves + if ( cg.demoPlayback || ( cg.snap->ps.pm_flags & PMF_FOLLOW ) ) { + CG_InterpolatePlayerState( qfalse ); + return; + } + + // non-predicting local movement will grab the latest angles + if ( cg_nopredict.integer || cg_synchronousClients.integer + || ( cg.snap->ps.eFlags & EF_MG42_ACTIVE ) ) { // RF, somewhat of a hack, but just disable prediction if on MG42, since it's just not very prediction friendly + CG_InterpolatePlayerState( qtrue ); + return; + } + + // prepare for pmove + cg_pmove.ps = &cg.predictedPlayerState; + cg_pmove.trace = CG_TraceCapsule; + cg_pmove.pointcontents = CG_PointContents; + if ( cg_pmove.ps->pm_type == PM_DEAD ) { + cg_pmove.tracemask = MASK_PLAYERSOLID & ~CONTENTS_BODY; + // DHM-Nerve added:: EF_DEAD is checked for in Pmove functions, but wasn't being set + // until after Pmove + cg_pmove.ps->eFlags |= EF_DEAD; + // dhm-Nerve end + } else { + cg_pmove.tracemask = MASK_PLAYERSOLID; + } + if ( ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_SPECTATOR ) || ( cg.snap->ps.pm_flags & PMF_LIMBO ) ) { // JPW NERVE limbo + cg_pmove.tracemask &= ~CONTENTS_BODY; // spectators can fly through bodies + } + cg_pmove.noFootsteps = ( cgs.dmflags & DF_NO_FOOTSTEPS ) > 0; + + //----(SA) added + cg_pmove.noWeapClips = ( cgs.dmflags & DF_NO_WEAPRELOAD ) > 0; + if ( cg.predictedPlayerState.aiChar ) { + cg_pmove.noWeapClips = qtrue; // ensure AI characters don't use clips + } +//----(SA) end + + + // save the state before the pmove so we can detect transitions + oldPlayerState = cg.predictedPlayerState; + + current = trap_GetCurrentCmdNumber(); + + // if we don't have the commands right after the snapshot, we + // can't accurately predict a current position, so just freeze at + // the last good position we had + cmdNum = current - CMD_BACKUP + 1; + trap_GetUserCmd( cmdNum, &oldestCmd ); + if ( oldestCmd.serverTime > cg.snap->ps.commandTime + && oldestCmd.serverTime < cg.time ) { // special check for map_restart + if ( cg_showmiss.integer ) { + CG_Printf( "exceeded PACKET_BACKUP on commands\n" ); + } +// return; + } + + // get the latest command so we can know which commands are from previous map_restarts + trap_GetUserCmd( current, &latestCmd ); + + // get the most recent information we have, even if + // the server time is beyond our current cg.time, + // because predicted player positions are going to + // be ahead of everything else anyway + if ( cg.nextSnap && !cg.nextFrameTeleport && !cg.thisFrameTeleport ) { + cg.predictedPlayerState = cg.nextSnap->ps; + cg.physicsTime = cg.nextSnap->serverTime; + } else { + cg.predictedPlayerState = cg.snap->ps; + cg.physicsTime = cg.snap->serverTime; + } + + if ( pmove_msec.integer < 8 ) { + trap_Cvar_Set( "pmove_msec", "8" ); + } else if ( pmove_msec.integer > 33 ) { + trap_Cvar_Set( "pmove_msec", "33" ); + } + + cg_pmove.pmove_fixed = pmove_fixed.integer; // | cg_pmove_fixed.integer; + cg_pmove.pmove_msec = pmove_msec.integer; + +//----(SA) added + // restore persistant client-side playerstate variables before doing the pmove + // this could be done as suggested in qshared.h ~line 991, but right now I copy each variable individually + cg.predictedPlayerState.weapAnim = oldPlayerState.weapAnim; + cg.predictedPlayerState.weapAnimTimer = oldPlayerState.weapAnimTimer; + cg.predictedPlayerState.venomTime = oldPlayerState.venomTime; + // show_bug.cgi?id=416 + // FIXME TTimo this causing a double weapon reload sound if you hit reload at the same time you run out of ammo + // cg.predictedPlayerState.weaponstate = oldPlayerState.weaponstate; // RF, added this, since they can become unsynched on loadgame, leaving incorrect anims + +//----(SA) end + + // RF, anim system + if ( cg_animState.integer ) { + cg.predictedPlayerState.aiState = cg_animState.integer - 1; + } + + // run cmds + moved = qfalse; + for ( cmdNum = current - CMD_BACKUP + 1 ; cmdNum <= current ; cmdNum++ ) { + // get the command + trap_GetUserCmd( cmdNum, &cg_pmove.cmd ); + // get the previous command + trap_GetUserCmd( cmdNum - 1, &cg_pmove.oldcmd ); + + if ( cg_pmove.pmove_fixed ) { + PM_UpdateViewAngles( cg_pmove.ps, &cg_pmove.cmd, CG_Trace ); + } + + // don't do anything if the time is before the snapshot player time + if ( cg_pmove.cmd.serverTime <= cg.predictedPlayerState.commandTime ) { + continue; + } + + // don't do anything if the command was from a previous map_restart + if ( cg_pmove.cmd.serverTime > latestCmd.serverTime ) { + continue; + } + + // check for a prediction error from last frame + // on a lan, this will often be the exact value + // from the snapshot, but on a wan we will have + // to predict several commands to get to the point + // we want to compare + if ( cg.predictedPlayerState.commandTime == oldPlayerState.commandTime ) { + vec3_t delta; + float len; + + if ( cg.thisFrameTeleport ) { + // a teleport will not cause an error decay + VectorClear( cg.predictedError ); + if ( cg_showmiss.integer ) { + CG_Printf( "PredictionTeleport\n" ); + } + cg.thisFrameTeleport = qfalse; + } else { + vec3_t adjusted; + CG_AdjustPositionForMover( cg.predictedPlayerState.origin, + cg.predictedPlayerState.groundEntityNum, cg.physicsTime, cg.oldTime, adjusted, deltaAngles ); + // RF, add the deltaAngles (fixes jittery view while riding trains) + cg.predictedPlayerState.delta_angles[YAW] += ANGLE2SHORT( deltaAngles[YAW] ); + + if ( cg_showmiss.integer ) { + if ( !VectorCompare( oldPlayerState.origin, adjusted ) ) { + CG_Printf( "prediction error\n" ); + } + } + VectorSubtract( oldPlayerState.origin, adjusted, delta ); + len = VectorLength( delta ); + if ( len > 0.1 ) { + if ( cg_showmiss.integer ) { + CG_Printf( "Prediction miss: %f\n", len ); + } + if ( cg_errorDecay.integer ) { + int t; + float f; + + t = cg.time - cg.predictedErrorTime; + f = ( cg_errorDecay.value - t ) / cg_errorDecay.value; + if ( f < 0 ) { + f = 0; + } + if ( f > 0 && cg_showmiss.integer ) { + CG_Printf( "Double prediction decay: %f\n", f ); + } + VectorScale( cg.predictedError, f, cg.predictedError ); + } else { + VectorClear( cg.predictedError ); + } + VectorAdd( delta, cg.predictedError, cg.predictedError ); + cg.predictedErrorTime = cg.oldTime; + } + } + } + + // don't predict gauntlet firing, which is only supposed to happen + // when it actually inflicts damage + cg_pmove.gauntletHit = qfalse; + + if ( cg_pmove.pmove_fixed ) { + cg_pmove.cmd.serverTime = ( ( cg_pmove.cmd.serverTime + pmove_msec.integer - 1 ) / pmove_msec.integer ) * pmove_msec.integer; + } + + // RF, if waiting for mission stats to go, ignore all input + if ( ( cgs.scrFadeAlphaCurrent ) || cg_norender.integer ) { + cg_pmove.cmd.buttons = 0; + cg_pmove.cmd.forwardmove = 0; + cg_pmove.cmd.rightmove = 0; + cg_pmove.cmd.upmove = 0; + cg_pmove.cmd.wbuttons = 0; + cg_pmove.cmd.wolfkick = 0; + cg_pmove.cmd.angles[0] = cg_pmove.oldcmd.angles[0]; + cg_pmove.cmd.angles[1] = cg_pmove.oldcmd.angles[1]; + cg_pmove.cmd.angles[2] = cg_pmove.oldcmd.angles[2]; + if ( cg_pmove.cmd.serverTime - cg.predictedPlayerState.commandTime > 1 ) { + cg_pmove.cmd.serverTime = cg.predictedPlayerState.commandTime + 1; + } + } + + Pmove( &cg_pmove ); + + moved = qtrue; + + // add push trigger movement effects + CG_TouchTriggerPrediction(); + } + + if ( cg_showmiss.integer > 1 ) { + CG_Printf( "[%i : %i] ", cg_pmove.cmd.serverTime, cg.time ); + } + + if ( !moved ) { + if ( cg_showmiss.integer ) { + CG_Printf( "not moved\n" ); + } + return; + } + + // adjust for the movement of the groundentity + CG_AdjustPositionForMover( cg.predictedPlayerState.origin, + cg.predictedPlayerState.groundEntityNum, + cg.physicsTime, cg.time, cg.predictedPlayerState.origin, deltaAngles ); + + // fire events and other transition triggered things + CG_TransitionPlayerState( &cg.predictedPlayerState, &oldPlayerState ); +} + diff --git a/src/cgame/cg_public.h b/src/cgame/cg_public.h new file mode 100644 index 0000000..d951595 --- /dev/null +++ b/src/cgame/cg_public.h @@ -0,0 +1,278 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + + +#define CMD_BACKUP 64 +#define CMD_MASK ( CMD_BACKUP - 1 ) +// allow a lot of command backups for very fast systems +// multiple commands may be combined into a single packet, so this +// needs to be larger than PACKET_BACKUP + + +#define MAX_ENTITIES_IN_SNAPSHOT 256 + +// snapshots are a view of the server at a given time + +// Snapshots are generated at regular time intervals by the server, +// but they may not be sent if a client's rate level is exceeded, or +// they may be dropped by the network. +typedef struct { + int snapFlags; // SNAPFLAG_RATE_DELAYED, etc + int ping; + + int serverTime; // server time the message is valid for (in msec) + + byte areamask[MAX_MAP_AREA_BYTES]; // portalarea visibility bits + + playerState_t ps; // complete information about the current player at this time + + int numEntities; // all of the entities that need to be presented + entityState_t entities[MAX_ENTITIES_IN_SNAPSHOT]; // at the time of this snapshot + + int numServerCommands; // text based server commands to execute when this + int serverCommandSequence; // snapshot becomes current +} snapshot_t; + +enum { + CGAME_EVENT_NONE, + CGAME_EVENT_TEAMMENU, + CGAME_EVENT_SCOREBOARD, + CGAME_EVENT_EDITHUD +}; + + +/* +================================================================== + +functions imported from the main executable + +================================================================== +*/ + +#define CGAME_IMPORT_API_VERSION 3 + +typedef enum { + CG_PRINT, + CG_ERROR, + CG_MILLISECONDS, + CG_CVAR_REGISTER, + CG_CVAR_UPDATE, + CG_CVAR_SET, + CG_CVAR_VARIABLESTRINGBUFFER, + CG_ARGC, + CG_ARGV, + CG_ARGS, + CG_FS_FOPENFILE, + CG_FS_READ, + CG_FS_WRITE, + CG_FS_FCLOSEFILE, + CG_SENDCONSOLECOMMAND, + CG_ADDCOMMAND, + CG_SENDCLIENTCOMMAND, + CG_UPDATESCREEN, + CG_CM_LOADMAP, + CG_CM_NUMINLINEMODELS, + CG_CM_INLINEMODEL, + CG_CM_LOADMODEL, + CG_CM_TEMPBOXMODEL, + CG_CM_POINTCONTENTS, + CG_CM_TRANSFORMEDPOINTCONTENTS, + CG_CM_BOXTRACE, + CG_CM_TRANSFORMEDBOXTRACE, +// MrE: + CG_CM_CAPSULETRACE, + CG_CM_TRANSFORMEDCAPSULETRACE, + CG_CM_TEMPCAPSULEMODEL, +// done. + CG_CM_MARKFRAGMENTS, + CG_S_STARTSOUND, + CG_S_STARTSOUNDEX, //----(SA) added + CG_S_STARTLOCALSOUND, + CG_S_CLEARLOOPINGSOUNDS, + CG_S_ADDLOOPINGSOUND, + CG_S_UPDATEENTITYPOSITION, +// Ridah, talking animations + CG_S_GETVOICEAMPLITUDE, +// done. + CG_S_RESPATIALIZE, + CG_S_REGISTERSOUND, + CG_S_STARTBACKGROUNDTRACK, + CG_S_FADESTREAMINGSOUND, //----(SA) modified + CG_S_FADEALLSOUNDS, //----(SA) added for fading out everything + CG_S_STARTSTREAMINGSOUND, + CG_R_LOADWORLDMAP, + CG_R_REGISTERMODEL, + CG_R_REGISTERSKIN, + CG_R_REGISTERSHADER, + + CG_R_GETSKINMODEL, // client allowed to view what the .skin loaded so they can set their model appropriately + CG_R_GETMODELSHADER, // client allowed the shader handle for given model/surface (for things like debris inheriting shader from explosive) + + CG_R_REGISTERFONT, + CG_R_CLEARSCENE, + CG_R_ADDREFENTITYTOSCENE, + CG_GET_ENTITY_TOKEN, + CG_R_ADDPOLYTOSCENE, +// Ridah + CG_R_ADDPOLYSTOSCENE, + CG_RB_ZOMBIEFXADDNEWHIT, +// done. + CG_R_ADDLIGHTTOSCENE, + + CG_R_ADDCORONATOSCENE, + CG_R_SETFOG, + + CG_R_RENDERSCENE, + CG_R_SETCOLOR, + CG_R_DRAWSTRETCHPIC, + CG_R_DRAWSTRETCHPIC_GRADIENT, //----(SA) added + CG_R_MODELBOUNDS, + CG_R_LERPTAG, + CG_GETGLCONFIG, + CG_GETGAMESTATE, + CG_GETCURRENTSNAPSHOTNUMBER, + CG_GETSNAPSHOT, + CG_GETSERVERCOMMAND, + CG_GETCURRENTCMDNUMBER, + CG_GETUSERCMD, + CG_SETUSERCMDVALUE, + CG_R_REGISTERSHADERNOMIP, + CG_MEMORY_REMAINING, + + CG_KEY_ISDOWN, + CG_KEY_GETCATCHER, + CG_KEY_SETCATCHER, + CG_KEY_GETKEY, + + CG_PC_ADD_GLOBAL_DEFINE, + CG_PC_LOAD_SOURCE, + CG_PC_FREE_SOURCE, + CG_PC_READ_TOKEN, + CG_PC_SOURCE_FILE_AND_LINE, + CG_S_STOPBACKGROUNDTRACK, + CG_REAL_TIME, + CG_SNAPVECTOR, + CG_REMOVECOMMAND, +// CG_R_LIGHTFORPOINT, // not currently used (sorry, trying to keep CG_MEMSET @ 100) + + CG_SENDMOVESPEEDSTOGAME, + + CG_CIN_PLAYCINEMATIC, + CG_CIN_STOPCINEMATIC, + CG_CIN_RUNCINEMATIC, + CG_CIN_DRAWCINEMATIC, + CG_CIN_SETEXTENTS, + CG_R_REMAP_SHADER, +// CG_S_ADDREALLOOPINGSOUND, // not currently used (sorry, trying to keep CG_MEMSET @ 100) + CG_S_STOPLOOPINGSOUND, + CG_S_STOPSTREAMINGSOUND, //----(SA) added + + CG_LOADCAMERA, + CG_STARTCAMERA, + CG_STOPCAMERA, //----(SA) added + CG_GETCAMERAINFO, + + CG_MEMSET = 110, + CG_MEMCPY, + CG_STRNCPY, + CG_SIN, + CG_COS, + CG_ATAN2, + CG_SQRT, + CG_FLOOR, + CG_CEIL, + + CG_TESTPRINTINT, + CG_TESTPRINTFLOAT, + CG_ACOS, + + CG_INGAME_POPUP, //----(SA) added + CG_INGAME_CLOSEPOPUP, // NERVE - SMF + CG_LIMBOCHAT, // NERVE - SMF + + CG_GETMODELINFO +} cgameImport_t; + + +/* +================================================================== + +functions exported to the main executable + +================================================================== +*/ + +typedef enum { + CG_INIT, +// void CG_Init( int serverMessageNum, int serverCommandSequence ) + // called when the level loads or when the renderer is restarted + // all media should be registered at this time + // cgame will display loading status by calling SCR_Update, which + // will call CG_DrawInformation during the loading process + // reliableCommandSequence will be 0 on fresh loads, but higher for + // demos, tourney restarts, or vid_restarts + + CG_SHUTDOWN, +// void (*CG_Shutdown)( void ); + // oportunity to flush and close any open files + + CG_CONSOLE_COMMAND, +// qboolean (*CG_ConsoleCommand)( void ); + // a console command has been issued locally that is not recognized by the + // main game system. + // use Cmd_Argc() / Cmd_Argv() to read the command, return qfalse if the + // command is not known to the game + + CG_DRAW_ACTIVE_FRAME, +// void (*CG_DrawActiveFrame)( int serverTime, stereoFrame_t stereoView, qboolean demoPlayback ); + // Generates and draws a game scene and status information at the given time. + // If demoPlayback is set, local movement prediction will not be enabled + + CG_CROSSHAIR_PLAYER, +// int (*CG_CrosshairPlayer)( void ); + + CG_LAST_ATTACKER, +// int (*CG_LastAttacker)( void ); + + CG_KEY_EVENT, +// void (*CG_KeyEvent)( int key, qboolean down ); + + CG_MOUSE_EVENT, +// void (*CG_MouseEvent)( int dx, int dy ); + CG_EVENT_HANDLING, +// void (*CG_EventHandling)(int type); + + CG_GET_TAG, +// qboolean CG_GetTag( int clientNum, char *tagname, orientation_t *or ); + + MAX_CGAME_EXPORT + +} cgameExport_t; + +//---------------------------------------------- diff --git a/src/cgame/cg_scoreboard.c b/src/cgame/cg_scoreboard.c new file mode 100644 index 0000000..a787a0a --- /dev/null +++ b/src/cgame/cg_scoreboard.c @@ -0,0 +1,698 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// cg_scoreboard -- draw the scoreboard on top of the game screen +#include "cg_local.h" + + +#define SCOREBOARD_WIDTH ( 31 * BIGCHAR_WIDTH ) + +/* +================= +CG_DrawScoreboard +================= +*/ +static void CG_DrawClientScore( int x, int y, score_t *score, float *color, float fade ) { + char string[1024]; + vec3_t headAngles; + clientInfo_t *ci; + + if ( score->client < 0 || score->client >= cgs.maxclients ) { + Com_Printf( "Bad score->client: %i\n", score->client ); + return; + } + + ci = &cgs.clientinfo[score->client]; + + // draw the handicap or bot skill marker + if ( ci->botSkill > 0 && ci->botSkill <= 5 ) { + CG_DrawPic( 0, y - 8, 32, 32, cgs.media.botSkillShaders[ ci->botSkill - 1 ] ); + } else if ( ci->handicap < 100 ) { + Com_sprintf( string, sizeof( string ), "%i", ci->handicap ); + CG_DrawSmallStringColor( 8, y, string, color ); + } + + // draw the wins / losses + if ( cgs.gametype == GT_TOURNAMENT ) { + Com_sprintf( string, sizeof( string ), "%i/%i", ci->wins, ci->losses ); + CG_DrawSmallStringColor( x + SCOREBOARD_WIDTH + 2, y, string, color ); + } + + // draw the face + VectorClear( headAngles ); + headAngles[YAW] = 180; + + CG_DrawHead( x - ICON_SIZE, y - ( ICON_SIZE - BIGCHAR_HEIGHT ) / 2, ICON_SIZE, ICON_SIZE, + score->client, headAngles ); + + if ( ci->powerups & ( 1 << PW_REDFLAG ) ) { + CG_DrawFlagModel( x - ICON_SIZE - ICON_SIZE / 2, y - ( ICON_SIZE - BIGCHAR_HEIGHT ) / 2, ICON_SIZE, ICON_SIZE, + TEAM_RED ); + } else if ( ci->powerups & ( 1 << PW_BLUEFLAG ) ) { + CG_DrawFlagModel( x - ICON_SIZE - ICON_SIZE / 2, y - ( ICON_SIZE - BIGCHAR_HEIGHT ) / 2, ICON_SIZE, ICON_SIZE, + TEAM_BLUE ); + } + + // draw the score line + if ( score->ping == -1 ) { + Com_sprintf( string, sizeof( string ), + "connecting %s", ci->name ); + } else if ( ci->team == TEAM_SPECTATOR ) { + Com_sprintf( string, sizeof( string ), + "SPECT %4i %4i %s", score->ping, score->time, ci->name ); + } else { + Com_sprintf( string, sizeof( string ), + "%5i %4i %4i %s", score->score, score->ping, score->time, ci->name ); + } + + // highlight your position + if ( score->client == cg.snap->ps.clientNum ) { + float hcolor[4]; + int rank; + + if ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_SPECTATOR + || cgs.gametype >= GT_TEAM ) { + rank = -1; + } else { + rank = cg.snap->ps.persistant[PERS_RANK] & ~RANK_TIED_FLAG; + } + + if ( rank == 0 ) { + hcolor[0] = 0; + hcolor[1] = 0; + hcolor[2] = 0.7; + } else if ( rank == 1 ) { + hcolor[0] = 0.7; + hcolor[1] = 0; + hcolor[2] = 0; + } else if ( rank == 2 ) { + hcolor[0] = 0.7; + hcolor[1] = 0.7; + hcolor[2] = 0; + } else { + hcolor[0] = 0.7; + hcolor[1] = 0.7; + hcolor[2] = 0.7; + } + + hcolor[3] = fade * 0.7; + CG_FillRect( x - 2, y, SCOREBOARD_WIDTH, BIGCHAR_HEIGHT + 1, hcolor ); + } + + CG_DrawBigString( x, y, string, fade ); + + // add the "ready" marker for intermission exiting + if ( cg.snap->ps.stats[ STAT_CLIENTS_READY ] & ( 1 << score->client ) ) { + CG_DrawBigStringColor( 0, y, "READY", color ); + } +} + +/* +================= +CG_TeamScoreboard +================= +*/ +static int CG_TeamScoreboard( int x, int y, team_t team, float fade ) { + int i; + score_t *score; + float color[4]; + int count; + int lineHeight; + clientInfo_t *ci; + + color[0] = color[1] = color[2] = 1.0; + color[3] = fade; + + count = 0; + lineHeight = 40; + // don't draw more than 9 rows + for ( i = 0 ; i < cg.numScores && count < 9 ; i++ ) { + score = &cg.scores[i]; + ci = &cgs.clientinfo[ score->client ]; + + if ( team != ci->team ) { + continue; + } + + CG_DrawClientScore( x, y + lineHeight * count, score, color, fade ); + count++; + } + + return y + count * lineHeight + 20; +} + +// NERVE - SMF +/* +================= +WM_DrawClientScore +================= +*/ +static int INFO_PLAYER_WIDTH = 300; +static int INFO_SCORE_WIDTH = 50; +static int INFO_LATENCY_WIDTH = 80; +static int INFO_TEAM_HEIGHT = 24; +static int INFO_BORDER = 2; + +static void WM_DrawClientScore( int x, int y, score_t *score, float *color, float fade ) { + float tempx; + vec4_t hcolor; + clientInfo_t *ci; + + if ( y + SMALLCHAR_HEIGHT >= 440 ) { + return; + } + + if ( score->client == cg.snap->ps.clientNum ) { + tempx = x; + + hcolor[3] = fade * 0.3; + VectorSet( hcolor, 0.4452, 0.1172, 0.0782 ); // DARK-RED + + CG_FillRect( tempx, y + 1, INFO_PLAYER_WIDTH - INFO_BORDER, SMALLCHAR_HEIGHT - 1, hcolor ); + tempx += INFO_PLAYER_WIDTH; + + CG_FillRect( tempx, y + 1, INFO_SCORE_WIDTH - INFO_BORDER, SMALLCHAR_HEIGHT - 1, hcolor ); + tempx += INFO_SCORE_WIDTH; + + CG_FillRect( tempx, y + 1, INFO_LATENCY_WIDTH - INFO_BORDER, SMALLCHAR_HEIGHT - 1, hcolor ); + tempx += INFO_LATENCY_WIDTH; + } + + tempx = x; + ci = &cgs.clientinfo[score->client]; + + CG_DrawSmallString( tempx, y, ci->name, fade ); + tempx += INFO_PLAYER_WIDTH; + + CG_DrawSmallString( tempx, y, va( "%4i", score->score ), fade ); + tempx += INFO_SCORE_WIDTH; + + CG_DrawSmallString( tempx, y, va( "%4i", score->ping ), fade ); + tempx += INFO_LATENCY_WIDTH; +} + +/* +================= +WM_TeamScoreboard +================= +*/ +static int WM_TeamScoreboard( int x, int y, team_t team, float fade ) { + vec4_t hcolor; + float tempx; + int i; + + hcolor[3] = fade; + if ( team == TEAM_RED ) { + VectorSet( hcolor, 0.4452, 0.1172, 0.0782 ); // LIGHT-RED + } else if ( team == TEAM_BLUE ) { + VectorSet( hcolor, 0.1836, 0.2422, 0.1680 ); // LIGHT-GREEN + } else { + VectorSet( hcolor, 0.2, 0.2, 0.2 ); // DARK-GREY + + } + // dont draw spectator if there are none + for ( i = 0; i < cg.numScores; i++ ) { + if ( team == cgs.clientinfo[ cg.scores[i].client ].team ) { + break; + } + } + if ( team == TEAM_SPECTATOR && i == cg.numScores ) { + return y; + } + + // draw team header + if ( y + SMALLCHAR_HEIGHT >= 440 ) { + return y; + } + + tempx = x; + + CG_FillRect( tempx, y, INFO_PLAYER_WIDTH - INFO_BORDER, INFO_TEAM_HEIGHT, hcolor ); + if ( team == TEAM_RED ) { + CG_DrawSmallString( tempx, y, "Axis", fade ); + } else if ( team == TEAM_BLUE ) { + CG_DrawSmallString( tempx, y, "Allies", fade ); + } else { + CG_DrawSmallString( tempx, y, "Spectators", fade ); + } + tempx += INFO_PLAYER_WIDTH; + + CG_FillRect( tempx, y, INFO_SCORE_WIDTH - INFO_BORDER, INFO_TEAM_HEIGHT, hcolor ); + tempx += INFO_SCORE_WIDTH; + + CG_FillRect( tempx, y, INFO_LATENCY_WIDTH - INFO_BORDER, INFO_TEAM_HEIGHT, hcolor ); + tempx += INFO_LATENCY_WIDTH; + + // draw player info + VectorSet( hcolor, 1, 1, 1 ); + hcolor[3] = fade; + + y += INFO_TEAM_HEIGHT + INFO_BORDER; + + for ( i = 0; i < cg.numScores; i++ ) { + if ( team != cgs.clientinfo[ cg.scores[i].client ].team ) { + continue; + } + + WM_DrawClientScore( x, y, &cg.scores[i], hcolor, fade ); + y += SMALLCHAR_HEIGHT; + } + + y += 4; + + return y; +} + +/* +================= +WM_DrawObjectives +================= +*/ +int WM_DrawObjectives( int x, int y, int width, float fade ) { + const char *s, *buf, *str; + char teamstr[32]; + int i, num, strwidth, status; + + y += 32; + + // determine character's team + if ( cg.snap->ps.persistant[PERS_TEAM] == TEAM_RED ) { + strcpy( teamstr, "axis_desc" ); + } else { + strcpy( teamstr, "allied_desc" ); + } + + s = CG_ConfigString( CS_MULTI_INFO ); + buf = Info_ValueForKey( s, "numobjectives" ); + + if ( buf && atoi( buf ) ) { + num = atoi( buf ); + + for ( i = 0; i < num; i++ ) { + s = CG_ConfigString( CS_MULTI_OBJECTIVE1 + i ); + buf = Info_ValueForKey( s, teamstr ); + + // draw text + str = va( "%s", buf ); + strwidth = CG_DrawStrlen( str ) * SMALLCHAR_WIDTH; + CG_DrawSmallString( x + width / 2 - strwidth / 2 - 12, y, str, fade ); + + // draw status flags + status = atoi( Info_ValueForKey( s, "status" ) ); + + if ( status == 0 ) { + CG_DrawPic( x + width / 2 - strwidth / 2 - 16 - 24, y, 24, 16, trap_R_RegisterShaderNoMip( "ui/assets/ger_flag.tga" ) ); + CG_DrawPic( x + width / 2 + strwidth / 2 - 12 + 4, y, 24, 16, trap_R_RegisterShaderNoMip( "ui/assets/ger_flag.tga" ) ); + } else if ( status == 1 ) { + CG_DrawPic( x + width / 2 - strwidth / 2 - 16 - 24, y, 24, 16, trap_R_RegisterShaderNoMip( "ui/assets/usa_flag.tga" ) ); + CG_DrawPic( x + width / 2 + strwidth / 2 - 12 + 4, y, 24, 16, trap_R_RegisterShaderNoMip( "ui/assets/usa_flag.tga" ) ); + } + + y += 16; + } + } + + return y; +} + +/* +================= +WM_ScoreboardOverlay +================= +*/ +int WM_ScoreboardOverlay( int x, int y, float fade ) { + vec4_t hcolor; + int width; + char *s; // JPW NERVE + int msec, mins, seconds, tens; // JPW NERVE + + width = INFO_PLAYER_WIDTH + INFO_LATENCY_WIDTH + INFO_SCORE_WIDTH + 25; + + VectorSet( hcolor, 0, 0, 0 ); + hcolor[3] = 0.7 * fade; + + // draw background + CG_FillRect( x - 12, y, width, 400, hcolor ); + + // draw title frame + VectorSet( hcolor, 0.0039, 0.0039, 0.2461 ); + hcolor[3] = 1 * fade; + CG_FillRect( x - 12, y, width, 30, hcolor ); + CG_DrawRect( x - 12, y, width, 400, 2, hcolor ); + + if ( cg.snap->ps.pm_type == PM_INTERMISSION ) { + const char *s, *buf; + + s = CG_ConfigString( CS_MULTI_INFO ); + buf = Info_ValueForKey( s, "winner" ); + + if ( atoi( buf ) ) { + CG_DrawSmallString( x - 12 + 5, y, "ALLIES WIN!", fade ); + } else { + CG_DrawSmallString( x - 12 + 5, y, "AXIS WIN!", fade ); + } + } +// JPW NERVE -- mission time & reinforce time + else { + msec = ( cgs.timelimit * 60.f * 1000.f ) - ( cg.time - cgs.levelStartTime ); + + seconds = msec / 1000; + mins = seconds / 60; + seconds -= mins * 60; + tens = seconds / 10; + seconds -= tens * 10; + + s = va( "Mission time: %2.0f:%i%i", (float)mins, tens, seconds ); // float cast to line up with reinforce time + CG_DrawSmallString( x - 7,y,s,fade ); + + if ( cgs.clientinfo[cg.snap->ps.clientNum].team == TEAM_RED ) { + msec = cg_redlimbotime.integer - ( cg.time % cg_redlimbotime.integer ); + } else if ( cgs.clientinfo[cg.snap->ps.clientNum].team == TEAM_BLUE ) { + msec = cg_bluelimbotime.integer - ( cg.time % cg_bluelimbotime.integer ); + } else { // no team (spectator mode) + msec = 0; + } + + if ( msec ) { + seconds = msec / 1000; + mins = seconds / 60; + seconds -= mins * 60; + tens = seconds / 10; + seconds -= tens * 10; + + s = va( "Reinforce time: %2.0f:%i%i", (float)mins, tens, seconds ); + CG_DrawSmallString( x - 7,y + 16,s,fade ); + } + } +// jpw +// CG_DrawSmallString( x - 12 + 5, y, "Wolfenstein Multiplayer", fade ); // old one + + y = WM_DrawObjectives( x, y, width, fade ); + y += 5; + + // draw field names + CG_DrawSmallString( x, y, "Players", fade ); + x += INFO_PLAYER_WIDTH; + + CG_DrawSmallString( x, y, "Score", fade ); + x += INFO_SCORE_WIDTH; + + CG_DrawSmallString( x, y, "Latency", fade ); + x += INFO_LATENCY_WIDTH; + + y += 20; + + return y; +} +// -NERVE - SMF + +/* +================= +CG_DrawScoreboard + +Draw the normal in-game scoreboard +================= +*/ +qboolean CG_DrawScoreboard( void ) { + int x = 0, y = 0, w; // TTimo init + float fade; + float *fadeColor; + char *s; + + // don't draw amuthing if the menu or console is up + if ( cg_paused.integer ) { + cg.deferredPlayerLoading = 0; + return qfalse; + } + + // still need to see 'mission failed' message in SP + if ( cgs.gametype == GT_SINGLE_PLAYER && cg.predictedPlayerState.pm_type == PM_DEAD ) { + return qfalse; + } + + if ( cgs.gametype == GT_SINGLE_PLAYER && cg.predictedPlayerState.pm_type == PM_INTERMISSION ) { + cg.deferredPlayerLoading = 0; + return qfalse; + } + + // don't draw scoreboard during death while warmup up + if ( cg.warmup && !cg.showScores ) { + return qfalse; + } + + if ( cg.showScores || cg.predictedPlayerState.pm_type == PM_DEAD || + cg.predictedPlayerState.pm_type == PM_INTERMISSION ) { + fade = 1.0; + fadeColor = colorWhite; + } else { + fadeColor = CG_FadeColor( cg.scoreFadeTime, FADE_TIME ); + + if ( !fadeColor ) { + // next time scoreboard comes up, don't print killer + cg.deferredPlayerLoading = 0; + cg.killerName[0] = 0; + return qfalse; + } + fade = *fadeColor; + } + + + // fragged by ... line + if ( cg.killerName[0] ) { + s = va( "Killed by %s", cg.killerName ); + w = CG_DrawStrlen( s ) * BIGCHAR_WIDTH; + x = ( SCREEN_WIDTH - w ) / 2; + y = 40; + CG_DrawBigString( x, y, s, fade ); + } + + // current rank + + //----(SA) enclosed this so it doesn't draw for SP + if ( cgs.gametype != GT_SINGLE_PLAYER && cgs.gametype != GT_WOLF ) { // NERVE - SMF - added wolf multiplayer check + if ( cg.snap->ps.persistant[PERS_TEAM] != TEAM_SPECTATOR ) { + if ( cgs.gametype < GT_TEAM ) { + s = va( "%s place with %i", + CG_PlaceString( cg.snap->ps.persistant[PERS_RANK] + 1 ), + cg.snap->ps.persistant[PERS_SCORE] ); + w = CG_DrawStrlen( s ) * BIGCHAR_WIDTH; + x = ( SCREEN_WIDTH - w ) / 2; + y = 60; + CG_DrawBigString( x, y, s, fade ); + } else { + if ( cg.teamScores[0] == cg.teamScores[1] ) { + s = va( "Teams are tied at %i", cg.teamScores[0] ); + } else if ( cg.teamScores[0] >= cg.teamScores[1] ) { + s = va( "Red leads %i to %i",cg.teamScores[0], cg.teamScores[1] ); + } else { + s = va( "Blue leads %i to %i",cg.teamScores[1], cg.teamScores[0] ); + } + + w = CG_DrawStrlen( s ) * BIGCHAR_WIDTH; + x = ( SCREEN_WIDTH - w ) / 2; + y = 60; + CG_DrawBigString( x, y, s, fade ); + } + } + + // scoreboard + x = 320 - SCOREBOARD_WIDTH / 2; + y = 86; + + #if 0 + CG_DrawBigStringColor( x, y, "SCORE PING TIME NAME", fadeColor ); + CG_DrawBigStringColor( x, y + 12, "----- ---- ---- ---------------", fadeColor ); + #endif + CG_DrawPic( x + 1 * 16, y, 64, 32, cgs.media.scoreboardScore ); + CG_DrawPic( x + 6 * 16 + 8, y, 64, 32, cgs.media.scoreboardPing ); + CG_DrawPic( x + 11 * 16 + 8, y, 64, 32, cgs.media.scoreboardTime ); + CG_DrawPic( x + 16 * 16, y, 64, 32, cgs.media.scoreboardName ); + + y += 32; + } + + // NERVE - SMF + if ( cgs.gametype == GT_WOLF ) { + // + // teamplay scoreboard + // + x = 320 - SCOREBOARD_WIDTH / 2 + 20 + 20; + y = 40; + + y = WM_ScoreboardOverlay( x, y, fade ); + + if ( cg.teamScores[0] >= cg.teamScores[1] ) { + y = WM_TeamScoreboard( x, y, TEAM_RED, fade ); + y = WM_TeamScoreboard( x, y, TEAM_BLUE, fade ); + } else { + y = WM_TeamScoreboard( x, y, TEAM_BLUE, fade ); + y = WM_TeamScoreboard( x, y, TEAM_RED, fade ); + } + y = WM_TeamScoreboard( x, y, TEAM_SPECTATOR, fade ); + } + // -NERVE - SMF + else if ( cgs.gametype >= GT_TEAM ) { + // + // teamplay scoreboard + // + if ( cg.teamScores[0] >= cg.teamScores[1] ) { + y = CG_TeamScoreboard( x, y, TEAM_RED, fade ); + y = CG_TeamScoreboard( x, y, TEAM_BLUE, fade ); + } else { + y = CG_TeamScoreboard( x, y, TEAM_BLUE, fade ); + y = CG_TeamScoreboard( x, y, TEAM_RED, fade ); + } + y = CG_TeamScoreboard( x, y, TEAM_SPECTATOR, fade ); + + } else if ( cgs.gametype != GT_SINGLE_PLAYER ) { //----(SA) modified + // + // free for all scoreboard + // + y = CG_TeamScoreboard( x, y, TEAM_FREE, fade ); + y = CG_TeamScoreboard( x, y, TEAM_SPECTATOR, fade ); + } + + // load any models that have been deferred + if ( ++cg.deferredPlayerLoading > 1 ) { + CG_LoadDeferredPlayers(); + } + + return qtrue; +} + +//================================================================================ + +/* +================ +CG_CenterGiantLine +================ +*/ +static void CG_CenterGiantLine( float y, const char *string ) { + float x; + vec4_t color; + + color[0] = 1; + color[1] = 1; + color[2] = 1; + color[3] = 1; + + x = 0.5 * ( 640 - GIANT_WIDTH * CG_DrawStrlen( string ) ); + + CG_DrawStringExt( x, y, string, color, qtrue, qtrue, GIANT_WIDTH, GIANT_HEIGHT, 0 ); +} + +/* +================= +CG_DrawTourneyScoreboard + +Draw the oversize scoreboard for tournements +================= +*/ +void CG_DrawTourneyScoreboard( void ) { + const char *s; + vec4_t color; + int min, tens, ones; + clientInfo_t *ci; + int y; + int i; + + // request more scores regularly + if ( cg.scoresRequestTime + 2000 < cg.time ) { + cg.scoresRequestTime = cg.time; + trap_SendClientCommand( "score" ); + } + + color[0] = 1; + color[1] = 1; + color[2] = 1; + color[3] = 1; + + // draw the dialog background + color[0] = color[1] = color[2] = 0; + color[3] = 1; + CG_FillRect( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, color ); + + // print the mesage of the day + s = CG_ConfigString( CS_MOTD ); + if ( !s[0] ) { + s = "Scoreboard"; + } + + // print optional title + CG_CenterGiantLine( 8, s ); + + // print server time + ones = cg.time / 1000; + min = ones / 60; + ones %= 60; + tens = ones / 10; + ones %= 10; + s = va( "%i:%i%i", min, tens, ones ); + + CG_CenterGiantLine( 64, s ); + + + // print the two scores + + y = 160; + if ( cgs.gametype >= GT_TEAM ) { + // + // teamplay scoreboard + // + CG_DrawStringExt( 8, y, "Red Team", color, qtrue, qtrue, GIANT_WIDTH, GIANT_HEIGHT, 0 ); + s = va( "%i", cg.teamScores[0] ); + CG_DrawStringExt( 632 - GIANT_WIDTH * strlen( s ), y, s, color, qtrue, qtrue, GIANT_WIDTH, GIANT_HEIGHT, 0 ); + + y += 64; + + CG_DrawStringExt( 8, y, "Blue Team", color, qtrue, qtrue, GIANT_WIDTH, GIANT_HEIGHT, 0 ); + s = va( "%i", cg.teamScores[1] ); + CG_DrawStringExt( 632 - GIANT_WIDTH * strlen( s ), y, s, color, qtrue, qtrue, GIANT_WIDTH, GIANT_HEIGHT, 0 ); + } else { + // + // free for all scoreboard + // + for ( i = 0 ; i < MAX_CLIENTS ; i++ ) { + ci = &cgs.clientinfo[i]; + if ( !ci->infoValid ) { + continue; + } + if ( ci->team != TEAM_FREE ) { + continue; + } + + CG_DrawStringExt( 8, y, ci->name, color, qtrue, qtrue, GIANT_WIDTH, GIANT_HEIGHT, 0 ); + s = va( "%i", ci->score ); + CG_DrawStringExt( 632 - GIANT_WIDTH * strlen( s ), y, s, color, qtrue, qtrue, GIANT_WIDTH, GIANT_HEIGHT, 0 ); + y += 64; + } + } + + +} + diff --git a/src/cgame/cg_servercmds.c b/src/cgame/cg_servercmds.c new file mode 100644 index 0000000..1f6636b --- /dev/null +++ b/src/cgame/cg_servercmds.c @@ -0,0 +1,1038 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + + +// cg_servercmds.c -- reliably sequenced text commands sent by the server +// these are processed at snapshot transition time, so there will definately +// be a valid snapshot this frame + +#include "cg_local.h" +#include "../ui/ui_shared.h" // bk001205 - for Q3_ui as well + + +/* +================= +CG_ParseScores + +================= +*/ +static void CG_ParseScores( void ) { + int i, powerups; + + cg.numScores = atoi( CG_Argv( 1 ) ); + if ( cg.numScores > MAX_CLIENTS ) { + cg.numScores = MAX_CLIENTS; + } + + cg.teamScores[0] = atoi( CG_Argv( 2 ) ); + cg.teamScores[1] = atoi( CG_Argv( 3 ) ); + + memset( cg.scores, 0, sizeof( cg.scores ) ); + for ( i = 0 ; i < cg.numScores ; i++ ) { + // + cg.scores[i].client = atoi( CG_Argv( i * 6 + 4 ) ); + cg.scores[i].score = atoi( CG_Argv( i * 6 + 5 ) ); + cg.scores[i].ping = atoi( CG_Argv( i * 6 + 6 ) ); + cg.scores[i].time = atoi( CG_Argv( i * 6 + 7 ) ); + cg.scores[i].scoreFlags = atoi( CG_Argv( i * 6 + 8 ) ); + powerups = atoi( CG_Argv( i * 6 + 9 ) ); + // DHM - Nerve :: the following parameters are not sent by server + /* + cg.scores[i].accuracy = atoi(CG_Argv(i * 14 + 10)); + cg.scores[i].impressiveCount = atoi(CG_Argv(i * 14 + 11)); + cg.scores[i].excellentCount = atoi(CG_Argv(i * 14 + 12)); + cg.scores[i].guantletCount = atoi(CG_Argv(i * 14 + 13)); + cg.scores[i].defendCount = atoi(CG_Argv(i * 14 + 14)); + cg.scores[i].assistCount = atoi(CG_Argv(i * 14 + 15)); + cg.scores[i].perfect = atoi(CG_Argv(i * 14 + 16)); + cg.scores[i].captures = atoi(CG_Argv(i * 14 + 17)); + */ + + if ( cg.scores[i].client < 0 || cg.scores[i].client >= MAX_CLIENTS ) { + cg.scores[i].client = 0; + } + cgs.clientinfo[ cg.scores[i].client ].score = cg.scores[i].score; + cgs.clientinfo[ cg.scores[i].client ].powerups = powerups; + + cg.scores[i].team = cgs.clientinfo[cg.scores[i].client].team; + } +#ifdef MISSIONPACK + CG_SetScoreSelection( NULL ); +#endif + +} + +/* +================= +CG_ParseTeamInfo + +================= +*/ +static void CG_ParseTeamInfo( void ) { + int i; + int client; + + numSortedTeamPlayers = atoi( CG_Argv( 1 ) ); + + for ( i = 0 ; i < numSortedTeamPlayers ; i++ ) { + client = atoi( CG_Argv( i * 6 + 2 ) ); + + sortedTeamPlayers[i] = client; + + cgs.clientinfo[ client ].location = atoi( CG_Argv( i * 6 + 3 ) ); + cgs.clientinfo[ client ].health = atoi( CG_Argv( i * 6 + 4 ) ); + cgs.clientinfo[ client ].armor = atoi( CG_Argv( i * 6 + 5 ) ); + cgs.clientinfo[ client ].curWeapon = atoi( CG_Argv( i * 6 + 6 ) ); + cgs.clientinfo[ client ].powerups = atoi( CG_Argv( i * 6 + 7 ) ); + } +} + + +/* +================ +CG_ParseServerinfo + +This is called explicitly when the gamestate is first received, +and whenever the server updates any serverinfo flagged cvars +================ +*/ +void CG_ParseServerinfo( void ) { + const char *info; + char *mapname; + + info = CG_ConfigString( CS_SERVERINFO ); + cgs.gametype = atoi( Info_ValueForKey( info, "g_gametype" ) ); + trap_Cvar_Set( "g_gametype", va( "%i", cgs.gametype ) ); + cgs.dmflags = atoi( Info_ValueForKey( info, "dmflags" ) ); + cgs.teamflags = atoi( Info_ValueForKey( info, "teamflags" ) ); + cgs.fraglimit = atoi( Info_ValueForKey( info, "fraglimit" ) ); + cgs.capturelimit = atoi( Info_ValueForKey( info, "capturelimit" ) ); + cgs.timelimit = atoi( Info_ValueForKey( info, "timelimit" ) ); + cgs.maxclients = atoi( Info_ValueForKey( info, "sv_maxclients" ) ); + mapname = Info_ValueForKey( info, "mapname" ); + Com_sprintf( cgs.mapname, sizeof( cgs.mapname ), "maps/%s.bsp", mapname ); + +// JPW NERVE +// prolly should parse all CS_SERVERINFO keys automagically, but I don't want to break anything that might be improperly set for wolf SP, so I'm just parsing MP relevant stuff here + trap_Cvar_Set( "g_medicChargeTime",Info_ValueForKey( info,"g_medicChargeTime" ) ); + trap_Cvar_Set( "g_engineerChargeTime",Info_ValueForKey( info,"g_engineerChargeTime" ) ); + trap_Cvar_Set( "g_soldierChargeTime",Info_ValueForKey( info,"g_soldierChargeTime" ) ); + trap_Cvar_Set( "g_LTChargeTime",Info_ValueForKey( info,"g_LTChargeTime" ) ); + trap_Cvar_Set( "g_redlimbotime",Info_ValueForKey( info,"g_redlimbotime" ) ); + trap_Cvar_Set( "g_bluelimbotime",Info_ValueForKey( info,"g_bluelimbotime" ) ); +// jpw + + // Q_strncpyz( cgs.redTeam, Info_ValueForKey( info, "g_redTeam" ), sizeof(cgs.redTeam) ); +// trap_Cvar_Set("g_redTeam", cgs.redTeam); +// Q_strncpyz( cgs.blueTeam, Info_ValueForKey( info, "g_blueTeam" ), sizeof(cgs.blueTeam) ); +// trap_Cvar_Set("g_blueTeam", cgs.blueTeam); +} + + +//----(SA) added +/* +============== +CG_ParseMissionStats + time h/m/s + objectives n/n + secrets n/n + treasure n/n + artifacts n/n + attempts num +============== +*/ +static void CG_ParseMissionStats( void ) { + const char *info; + char *token; + + info = CG_ConfigString( CS_MISSIONSTATS ); + +// time + token = COM_Parse( (char **)&info ); + cg.playTimeH = atoi( token ); + token = COM_Parse( (char **)&info ); + cg.playTimeM = atoi( token ); + token = COM_Parse( (char **)&info ); + cg.playTimeS = atoi( token ); + +// objectives + token = COM_Parse( (char **)&info ); + cg.numObjectivesFound = atoi( token ); + token = COM_Parse( (char **)&info ); + cg.numObjectives = atoi( token ); + +// secrets + token = COM_Parse( (char **)&info ); + cg.numSecretsFound = atoi( token ); + token = COM_Parse( (char **)&info ); + cg.numSecrets = atoi( token ); + +// treasure + token = COM_Parse( (char **)&info ); + cg.numTreasureFound = atoi( token ); + token = COM_Parse( (char **)&info ); + cg.numTreasure = atoi( token ); + +// artifacts + token = COM_Parse( (char **)&info ); + cg.numArtifactsFound = atoi( token ); + token = COM_Parse( (char **)&info ); + cg.numArtifacts = atoi( token ); + +// attempts + token = COM_Parse( (char **)&info ); + cg.attempts = atoi( token ); + +} +//----(SA) end + + +/* +================== +CG_ParseWarmup +================== +*/ +static void CG_ParseWarmup( void ) { + const char *info; + int warmup; + + info = CG_ConfigString( CS_WARMUP ); + + warmup = atoi( info ); + cg.warmupCount = -1; + + if ( warmup == 0 && cg.warmup ) { + + } else if ( warmup > 0 && cg.warmup <= 0 ) { + trap_S_StartLocalSound( cgs.media.countPrepareSound, CHAN_ANNOUNCER ); + } + + cg.warmup = warmup; +} + +/* +===================== +CG_ParseScreenFade +===================== +*/ +static void CG_ParseScreenFade( void ) { + const char *info; + char *token; + float fadealpha; + int fadestart, fadeduration; + + info = CG_ConfigString( CS_SCREENFADE ); + token = COM_Parse( (char **)&info ); + fadealpha = atof( token ); + token = COM_Parse( (char **)&info ); + fadestart = atoi( token ); + token = COM_Parse( (char **)&info ); + fadeduration = atoi( token ); + + CG_Fade( 0, 0, 0, (int)( fadealpha * 255.0f ), fadestart, fadeduration ); +} + + +/* +============== +CG_ParseFog + float near dist + float far dist + float density + float[3] r,g,b + int time +============== +*/ +static void CG_ParseFog( void ) { + const char *info; + char *token; + float ne, fa, r, g, b, density; + int time; + + info = CG_ConfigString( CS_FOGVARS ); + token = COM_Parse( (char **)&info ); ne = atof( token ); + token = COM_Parse( (char **)&info ); + + if ( !token || !token[0] ) { + // set to 'no fog' + // 'FOG_MAP' is not registered, so it will always make fog go away + trap_R_SetFog( FOG_CMD_SWITCHFOG, FOG_MAP, (int)ne, 0, 0, 0, 0 ); + return; + } + + fa = atof( token ); + + token = COM_Parse( (char **)&info ); density = atof( token ); + token = COM_Parse( (char **)&info ); r = atof( token ); + token = COM_Parse( (char **)&info ); g = atof( token ); + token = COM_Parse( (char **)&info ); b = atof( token ); + token = COM_Parse( (char **)&info ); time = atoi( token ); + + trap_R_SetFog( FOG_SERVER, (int)ne, (int)fa, r, g, b, density ); + trap_R_SetFog( FOG_CMD_SWITCHFOG, FOG_SERVER, time, 0, 0, 0, 0 ); +} + +/* +================ +CG_SetConfigValues + +Called on load to set the initial values from configure strings +================ +*/ +void CG_SetConfigValues( void ) { +#ifdef MISSIONPACK + const char *s; +#endif + + cgs.scores1 = atoi( CG_ConfigString( CS_SCORES1 ) ); + cgs.scores2 = atoi( CG_ConfigString( CS_SCORES2 ) ); + cgs.levelStartTime = atoi( CG_ConfigString( CS_LEVEL_START_TIME ) ); +#ifdef MISSIONPACK + if ( cgs.gametype == GT_CTF ) { + s = CG_ConfigString( CS_FLAGSTATUS ); + cgs.redflag = s[0] - '0'; + cgs.blueflag = s[1] - '0'; + } else if ( cgs.gametype == GT_1FCTF ) { + s = CG_ConfigString( CS_FLAGSTATUS ); + cgs.flagStatus = s[0] - '0'; + } +#endif + cg.warmup = atoi( CG_ConfigString( CS_WARMUP ) ); +} + +/* +===================== +CG_ShaderStateChanged +===================== +*/ +void CG_ShaderStateChanged( void ) { + char originalShader[MAX_QPATH]; + char newShader[MAX_QPATH]; + char timeOffset[16]; + const char *o; + char *n,*t; + + o = CG_ConfigString( CS_SHADERSTATE ); + while ( o && *o ) { + n = strstr( o, "=" ); + if ( n && *n ) { + strncpy( originalShader, o, n - o ); + originalShader[n - o] = 0; + n++; + t = strstr( n, ":" ); + if ( t && *t ) { + strncpy( newShader, n, t - n ); + newShader[t - n] = 0; + } else { + break; + } + t++; + o = strstr( t, "@" ); + if ( o ) { + strncpy( timeOffset, t, o - t ); + timeOffset[o - t] = 0; + o++; + trap_R_RemapShader( originalShader, newShader, timeOffset ); + } + } else { + break; + } + } +} + +/* +================ +CG_ConfigStringModified + +================ +*/ +static void CG_ConfigStringModified( void ) { + const char *str; + int num; + + num = atoi( CG_Argv( 1 ) ); + + // get the gamestate from the client system, which will have the + // new configstring already integrated + trap_GetGameState( &cgs.gameState ); + + // look up the individual string that was modified + str = CG_ConfigString( num ); + + // do something with it if necessary + if ( num == CS_MUSIC ) { + CG_StartMusic(); + } else if ( num == CS_MUSIC_QUEUE ) { //----(SA) added + CG_QueueMusic(); + } else if ( num == CS_MISSIONSTATS ) { //----(SA) added + CG_ParseMissionStats(); + } else if ( num == CS_SERVERINFO ) { + CG_ParseServerinfo(); + } else if ( num == CS_WARMUP ) { + CG_ParseWarmup(); + } else if ( num == CS_SCORES1 ) { + cgs.scores1 = atoi( str ); + } else if ( num == CS_SCORES2 ) { + cgs.scores2 = atoi( str ); + } else if ( num == CS_LEVEL_START_TIME ) { + cgs.levelStartTime = atoi( str ); + } else if ( num == CS_VOTE_TIME ) { + cgs.voteTime = atoi( str ); + cgs.voteModified = qtrue; + } else if ( num == CS_VOTE_YES ) { + cgs.voteYes = atoi( str ); + cgs.voteModified = qtrue; + } else if ( num == CS_VOTE_NO ) { + cgs.voteNo = atoi( str ); + cgs.voteModified = qtrue; + } else if ( num == CS_VOTE_STRING ) { + Q_strncpyz( cgs.voteString, str, sizeof( cgs.voteString ) ); +#if 0 + trap_S_StartLocalSound( cgs.media.voteNow, CHAN_ANNOUNCER ); + } else if ( num >= CS_TEAMVOTE_TIME && num <= CS_TEAMVOTE_TIME + 1 ) { + cgs.teamVoteTime[num - CS_TEAMVOTE_TIME] = atoi( str ); + cgs.teamVoteModified[num - CS_TEAMVOTE_TIME] = qtrue; + } else if ( num >= CS_TEAMVOTE_YES && num <= CS_TEAMVOTE_YES + 1 ) { + cgs.teamVoteYes[num - CS_TEAMVOTE_YES] = atoi( str ); + cgs.teamVoteModified[num - CS_TEAMVOTE_YES] = qtrue; + } else if ( num >= CS_TEAMVOTE_NO && num <= CS_TEAMVOTE_NO + 1 ) { + cgs.teamVoteNo[num - CS_TEAMVOTE_NO] = atoi( str ); + cgs.teamVoteModified[num - CS_TEAMVOTE_NO] = qtrue; + } else if ( num >= CS_TEAMVOTE_STRING && num <= CS_TEAMVOTE_STRING + 1 ) { + Q_strncpyz( cgs.teamVoteString[num - CS_TEAMVOTE_STRING], str, sizeof( cgs.teamVoteString ) ); + trap_S_StartLocalSound( cgs.media.voteNow, CHAN_ANNOUNCER ); +#endif + } else if ( num == CS_INTERMISSION ) { + cg.intermissionStarted = atoi( str ); + } else if ( num == CS_SCREENFADE ) { + CG_ParseScreenFade(); + } else if ( num == CS_FOGVARS ) { + CG_ParseFog(); + } else if ( num >= CS_MODELS && num < CS_MODELS + MAX_MODELS ) { + cgs.gameModels[ num - CS_MODELS ] = trap_R_RegisterModel( str ); + } else if ( num >= CS_SOUNDS && num < CS_SOUNDS + MAX_MODELS ) { + if ( str[0] != '*' ) { // player specific sounds don't register here + + // Ridah, register sound scripts seperately + if ( !strstr( str, ".wav" ) ) { + CG_SoundScriptPrecache( str ); + } else { + cgs.gameSounds[ num - CS_SOUNDS] = trap_S_RegisterSound( str ); + } + + } + } else if ( num >= CS_PLAYERS && num < CS_PLAYERS + MAX_CLIENTS ) { + CG_NewClientInfo( num - CS_PLAYERS ); + } + // Rafael particle configstring + else if ( num >= CS_PARTICLES && num < CS_PARTICLES + MAX_PARTICLES_AREAS ) { + CG_NewParticleArea( num ); + } +//----(SA) have not reached this code yet so I don't know if I really need this here + else if ( num >= CS_DLIGHTS && num < CS_DLIGHTS + MAX_DLIGHTS ) { + CG_Printf( ">>>>>>>>>>>got configstring for dlight: %d\ntell Sherman!!!!!!!!!!", num - CS_DLIGHTS ); +//----(SA) + } else if ( num == CS_SHADERSTATE ) { + CG_ShaderStateChanged(); + } + +} + + +/* +======================= +CG_AddToTeamChat + +======================= +*/ +static void CG_AddToTeamChat( const char *str ) { + int len; + char *p, *ls; + int lastcolor; + int chatHeight; + + if ( cg_teamChatHeight.integer < TEAMCHAT_HEIGHT ) { + chatHeight = cg_teamChatHeight.integer; + } else { + chatHeight = TEAMCHAT_HEIGHT; + } + + if ( chatHeight <= 0 || cg_teamChatTime.integer <= 0 ) { + // team chat disabled, dump into normal chat + cgs.teamChatPos = cgs.teamLastChatPos = 0; + return; + } + + len = 0; + + p = cgs.teamChatMsgs[cgs.teamChatPos % chatHeight]; + *p = 0; + + lastcolor = '7'; + + ls = NULL; + while ( *str ) { + if ( len > TEAMCHAT_WIDTH - 1 ) { + if ( ls ) { + str -= ( p - ls ); + str++; + p -= ( p - ls ); + } + *p = 0; + + cgs.teamChatMsgTimes[cgs.teamChatPos % chatHeight] = cg.time; + + cgs.teamChatPos++; + p = cgs.teamChatMsgs[cgs.teamChatPos % chatHeight]; + *p = 0; + *p++ = Q_COLOR_ESCAPE; + *p++ = lastcolor; + len = 0; + ls = NULL; + } + + if ( Q_IsColorString( str ) ) { + *p++ = *str++; + lastcolor = *str; + *p++ = *str++; + continue; + } + if ( *str == ' ' ) { + ls = p; + } + *p++ = *str++; + len++; + } + *p = 0; + + cgs.teamChatMsgTimes[cgs.teamChatPos % chatHeight] = cg.time; + cgs.teamChatPos++; + + if ( cgs.teamChatPos - cgs.teamLastChatPos > chatHeight ) { + cgs.teamLastChatPos = cgs.teamChatPos - chatHeight; + } +} + +/* +=============== +CG_SendMoveSpeed +=============== +*/ +void CG_SendMoveSpeed( animation_t *animList, int numAnims, char *modelName ) { + animation_t *anim; + int i; + char text[10000]; + + if ( !cgs.localServer ) { + return; + } + + text[0] = 0; + Q_strcat( text, sizeof( text ), modelName ); + + for ( i = 0, anim = animList; i < numAnims; i++, anim++ ) { + if ( anim->moveSpeed <= 0 ) { + continue; + } + + // add this to the list + Q_strcat( text, sizeof( text ), va( " %s %i %.1f", anim->name, anim->moveSpeed, anim->stepGap ) ); + } + + // send the movespeeds to the server + trap_SendMoveSpeedsToGame( 0, text ); +} + +/* +=============== +CG_SendMoveSpeeds + + send moveSpeeds for all unique models +=============== +*/ +#if 0 +void CG_SendMoveSpeeds( void ) { + int i,j; + animModelInfo_t *modelInfo; + clientInfo_t *ci; + + for ( i = 0; i < MAX_ANIMSCRIPT_MODELS; i++ ) { + + modelInfo = cgs.animScriptData.modelInfo[i]; + + if ( modelInfo == NULL ) { + continue; + } + + if ( !modelInfo->modelname[0] ) { + continue; + } +/* + // recalc them + // find a client that uses this model + for (ci = cgs.clientinfo, j=0; jmodelInfo && ci->modelInfo == modelInfo) { + CG_CalcMoveSpeeds( ci ); + break; + } + } +*/ +// if (j==MAX_CLIENTS) +// CG_Error( "CG_SendMoveSpeeds: cannot find client with modelName \"%s\" for moveSpeed calc", modelInfo->modelname ); + + // send this model + //CG_SendMoveSpeed( modelInfo->animations, modelInfo->numAnimations, modelInfo->modelname ); + } + +} +#endif + +/* +=============== +CG_MapRestart + +The server has issued a map_restart, so the next snapshot +is completely new and should not be interpolated to. + +A tournement restart will clear everything, but doesn't +require a reload of all the media +=============== +*/ +static void CG_MapRestart( void ) { +// char buff[64]; + int i; + if ( cg_showmiss.integer ) { + CG_Printf( "CG_MapRestart\n" ); + } + + memset( &cg.lastWeapSelInBank[0], 0, MAX_WEAP_BANKS * sizeof( int ) ); // clear weapon bank selections + + cg.centerPrintTime = 0; // reset centerprint counter so previous messages don't re-appear + cg.itemPickupTime = 0; // reset item pickup counter so previous messages don't re-appear + cg.cursorHintFade = 0; // reset cursor hint timer + cg.yougotmailTime = 0; // reset + + // (SA) clear zoom (so no warpies) + cg.zoomedBinoc = qfalse; + cg.zoomedBinoc = cg.zoomedScope = qfalse; + cg.zoomTime = 0; + cg.zoomval = 0; + + // reset fog to world fog (if present) +// trap_R_SetFog(FOG_CMD_SWITCHFOG, FOG_MAP,20,0,0,0,0); +// trap_Cvar_VariableStringBuffer("r_mapFogColor", buff, sizeof(buff)); +// trap_SendClientCommand(va("fogswitch %s", buff) ); + + CG_InitLocalEntities(); + CG_InitMarkPolys(); + + //Rafael particles + CG_ClearParticles(); + // done. + + for ( i = 1; i < MAX_PARTICLES_AREAS; i++ ) + { + { + int rval; + + rval = CG_NewParticleArea( CS_PARTICLES + i ); + if ( !rval ) { + break; + } + } + } + + + // Ridah, trails + CG_ClearTrails(); + // done. + + // Ridah + CG_ClearFlameChunks(); + CG_SoundInit(); + // done. + + // RF, init ZombieFX + trap_RB_ZombieFXAddNewHit( -1, NULL, NULL ); + + // make sure the "3 frags left" warnings play again + cg.fraglimitWarnings = 0; + + cg.timelimitWarnings = 0; + + cg.intermissionStarted = qfalse; + + cgs.voteTime = 0; + + cg.lightstylesInited = qfalse; + + cg.mapRestart = qtrue; + + CG_StartMusic(); + + trap_S_ClearLoopingSounds( qtrue ); + + // we really should clear more parts of cg here and stop sounds + cg.v_dmg_time = 0; + cg.v_noFireTime = 0; + cg.v_fireTime = 0; + + // RF, clear out animScriptData so we recalc everything and get new pointers from server + memset( cgs.animScriptData.modelInfo, 0, sizeof( cgs.animScriptData.modelInfo ) ); + for ( i = 0; i < MAX_CLIENTS; i++ ) { + if ( cgs.clientinfo[i].infoValid ) { + CG_LoadClientInfo( &cgs.clientinfo[i] ); // re-register the valid clients + } + } + // always clear the weapon selection + cg.weaponSelect = WP_NONE; + // clear out the player weapon info + memset( &cg_entities[0].pe.weap, 0, sizeof( cg_entities[0].pe.weap ) ); + // check for server set weapons we might not know about + // (FIXME: this is a hack for the time being since a scripted "selectweapon" does + // not hit the first snap, the server weapon set in cg_playerstate.c line 219 doesn't + // do the trick) + if ( !cg.weaponSelect ) { + if ( cg_loadWeaponSelect.integer > 0 ) { + cg.weaponSelect = cg_loadWeaponSelect.integer; + cg.weaponSelectTime = cg.time; + trap_Cvar_Set( "cg_loadWeaponSelect", "0" ); // turn it off + } + } + // clear out rumble effects + memset( cg.cameraShake, 0, sizeof( cg.cameraShake ) ); + memset( cg.cameraShakeAngles, 0, sizeof( cg.cameraShakeAngles ) ); + cg.rumbleScale = 0; + + // play the "fight" sound if this is a restart without warmup +// if ( cg.warmup == 0 /* && cgs.gametype == GT_TOURNAMENT */) { +// trap_S_StartLocalSound( cgs.media.countFightSound, CHAN_ANNOUNCER ); +// CG_CenterPrint( "FIGHT!", 120, GIANTCHAR_WIDTH*2 ); +// } +#ifdef MISSIONPACK + if ( cg_singlePlayerActive.integer ) { + trap_Cvar_Set( "ui_matchStartTime", va( "%i", cg.time ) ); + if ( cg_recordSPDemo.integer && cg_recordSPDemoName.string && *cg_recordSPDemoName.string ) { + trap_SendConsoleCommand( va( "set g_synchronousclients 1 ; record %s \n", cg_recordSPDemoName.string ) ); + } + } +#endif + trap_Cvar_Set( "cg_thirdPerson", "0" ); +} + +/* +================= +CG_RequestMoveSpeed +================= +*/ +void CG_RequestMoveSpeed( const char *modelname ) { + animModelInfo_t *modelInfo; + + modelInfo = BG_ModelInfoForModelname( (char *)modelname ); + + if ( !modelInfo ) { + // ignore it + return; + } + + // send it + CG_SendMoveSpeed( modelInfo->animations, modelInfo->numAnimations, (char *)modelname ); +} + +/* +================= +CG_RemoveChatEscapeChar +================= +*/ +static void CG_RemoveChatEscapeChar( char *text ) { + int i, l; + + l = 0; + for ( i = 0; text[i]; i++ ) { + if ( text[i] == '\x19' ) { + continue; + } + text[l++] = text[i]; + } + text[l] = '\0'; +} + +/* +================= +CG_ServerCommand + +The string has been tokenized and can be retrieved with +Cmd_Argc() / Cmd_Argv() +================= +*/ +void CG_ObjectivePrint( const char *str, int charWidth, int team ); // NERVE - SMF + +static void CG_ServerCommand( void ) { + const char *cmd; + char text[MAX_SAY_TEXT]; + + cmd = CG_Argv( 0 ); + + if ( !cmd[0] ) { + // server claimed the command + return; + } + + if ( !strcmp( cmd, "startCam" ) ) { + CG_StartCamera( CG_Argv( 1 ), atoi( CG_Argv( 2 ) ) ); + return; + } + + if ( !strcmp( cmd, "stopCam" ) ) { + CG_StopCamera(); + return; + } + + if ( !strcmp( cmd, "mvspd" ) ) { + CG_RequestMoveSpeed( CG_Argv( 1 ) ); + return; + } + + if ( !strcmp( cmd, "dp" ) ) { // dynamite print (what a hack :( + + CG_CenterPrint( va( "%s %d %s", CG_translateString( "dynamitetimer" ), atoi( CG_Argv( 1 ) ), CG_translateString( "seconds" ) ), + SCREEN_HEIGHT - ( SCREEN_HEIGHT * 0.25 ), SMALLCHAR_WIDTH ); + return; + } + + if ( !strcmp( cmd, "cp" ) ) { + CG_CenterPrint( CG_Argv( 1 ), SCREEN_HEIGHT - ( SCREEN_HEIGHT * 0.25 ), SMALLCHAR_WIDTH ); + return; + } + + if ( !strcmp( cmd, "cs" ) ) { + CG_ConfigStringModified(); + return; + } + + if ( !strcmp( cmd, "print" ) ) { + CG_Printf( "%s", CG_Argv( 1 ) ); +#ifdef MISSIONPACK + cmd = CG_Argv( 1 ); // yes, this is obviously a hack, but so is the way we hear about + // votes passing or failing + if ( !Q_stricmpn( cmd, "vote failed", 11 ) || !Q_stricmpn( cmd, "team vote failed", 16 ) ) { + trap_S_StartLocalSound( cgs.media.voteFailed, CHAN_ANNOUNCER ); + } else if ( !Q_stricmpn( cmd, "vote passed", 11 ) || !Q_stricmpn( cmd, "team vote passed", 16 ) ) { + trap_S_StartLocalSound( cgs.media.votePassed, CHAN_ANNOUNCER ); + } +#endif + return; + } + + if ( !strcmp( cmd, "chat" ) ) { + if ( !cg_teamChatsOnly.integer ) { + trap_S_StartLocalSound( cgs.media.talkSound, CHAN_LOCAL_SOUND ); + Q_strncpyz( text, CG_Argv( 1 ), MAX_SAY_TEXT ); + CG_RemoveChatEscapeChar( text ); + CG_Printf( "%s\n", text ); + } + return; + } + + if ( !strcmp( cmd, "tchat" ) ) { + trap_S_StartLocalSound( cgs.media.talkSound, CHAN_LOCAL_SOUND ); + Q_strncpyz( text, CG_Argv( 1 ), MAX_SAY_TEXT ); + CG_RemoveChatEscapeChar( text ); + CG_AddToTeamChat( text ); + CG_Printf( "%s\n", text ); + return; + } + + // NERVE - SMF - limbo chat + if ( !strcmp( cmd, "lchat" ) ) { + trap_S_StartLocalSound( cgs.media.talkSound, CHAN_LOCAL_SOUND ); + Q_strncpyz( text, CG_Argv( 1 ), MAX_SAY_TEXT ); + CG_RemoveChatEscapeChar( text ); +// CG_AddToLimboChat( text ); + trap_UI_LimboChat( text ); + CG_Printf( "%s\n", text ); + return; + } + // -NERVE - SMF + + if ( !strcmp( cmd, "vchat" ) ) { +// CG_VoiceChat( SAY_ALL ); + return; + } + + if ( !strcmp( cmd, "vtchat" ) ) { +// CG_VoiceChat( SAY_TEAM ); + return; + } + + if ( !strcmp( cmd, "vtell" ) ) { +// CG_VoiceChat( SAY_TELL ); + return; + } + + if ( !strcmp( cmd, "scores" ) ) { + CG_ParseScores(); + return; + } + + if ( !strcmp( cmd, "tinfo" ) ) { + CG_ParseTeamInfo(); + return; + } + + if ( !strcmp( cmd, "map_restart" ) ) { + CG_MapRestart(); + return; + } + + if ( Q_stricmp( cmd, "remapShader" ) == 0 ) { + if ( trap_Argc() == 4 ) { + trap_R_RemapShader( CG_Argv( 1 ), CG_Argv( 2 ), CG_Argv( 3 ) ); + } + } + + // loaddeferred can be both a servercmd and a consolecmd + if ( !strcmp( cmd, "loaddeferred" ) ) { // spelling fixed (SA) + CG_LoadDeferredPlayers(); + return; + } + + // clientLevelShot is sent before taking a special screenshot for + // the menu system during development + if ( !strcmp( cmd, "clientLevelShot" ) ) { + cg.levelShot = qtrue; + return; + } + + // NERVE - SMF + if ( !Q_stricmp( cmd, "oid" ) ) { + CG_ObjectivePrint( CG_Argv( 2 ), SMALLCHAR_WIDTH, atoi( CG_Argv( 1 ) ) ); + return; + } + // -NERVE - SMF + + + // + // music + // + + // loops \/ + if ( !strcmp( cmd, "mu_start" ) ) { // has optional parameter for fade-up time + int fadeTime = 0; // default to instant start + + Q_strncpyz( text, CG_Argv( 2 ), MAX_SAY_TEXT ); + if ( text && strlen( text ) ) { + fadeTime = atoi( text ); + } + + trap_S_StartBackgroundTrack( CG_Argv( 1 ), CG_Argv( 1 ), fadeTime ); + return; + } + // plays once then back to whatever the loop was \/ + if ( !strcmp( cmd, "mu_play" ) ) { // has optional parameter for fade-up time + int fadeTime = 0; // default to instant start + + Q_strncpyz( text, CG_Argv( 2 ), MAX_SAY_TEXT ); + if ( text && strlen( text ) ) { + fadeTime = atoi( text ); + } + + trap_S_StartBackgroundTrack( CG_Argv( 1 ), "onetimeonly", fadeTime ); + return; + } + + if ( !strcmp( cmd, "mu_stop" ) ) { // has optional parameter for fade-down time + int fadeTime = 0; // default to instant stop + + Q_strncpyz( text, CG_Argv( 1 ), MAX_SAY_TEXT ); + if ( text && strlen( text ) ) { + fadeTime = atoi( text ); + } + trap_S_FadeBackgroundTrack( 0.0f, fadeTime, 0 ); + trap_S_StartBackgroundTrack( "", "", -2 ); // '-2' for 'queue looping track' (QUEUED_PLAY_LOOPED) + return; + } + + if ( !strcmp( cmd, "mu_fade" ) ) { + trap_S_FadeBackgroundTrack( atof( CG_Argv( 1 ) ), atoi( CG_Argv( 2 ) ), 0 ); + return; + } + + if ( !strcmp( cmd, "snd_fade" ) ) { + trap_S_FadeAllSound( atof( CG_Argv( 1 ) ), atoi( CG_Argv( 2 ) ) ); + return; + } + + if ( !strcmp( cmd, "rockandroll" ) ) { // map loaded, game is ready to begin. + CG_Fade( 0, 0, 0, 255, cg.time, 0 ); // go black + trap_UI_Popup( "pregame" ); // start pregame menu + trap_Cvar_Set( "cg_norender", "1" ); // don't render the world until the player clicks in and the 'playerstart' func has been called (g_main in G_UpdateCvars() ~ilne 949) + + trap_S_FadeAllSound( 1.0f, 1000 ); // fade sound up + + return; + } + + + + // ensure a file gets into a build (mainly for scripted music calls) + if ( !strcmp( cmd, "addToBuild" ) ) { + fileHandle_t f; + + if ( !cg_buildScript.integer ) { + return; + } + + // just open the file so it gets copied to the build dir + //CG_FileTouchForBuild(CG_Argv(1)); + trap_FS_FOpenFile( CG_Argv( 1 ), &f, FS_READ ); + trap_FS_FCloseFile( f ); + return; + } + + + CG_Printf( "Unknown client game command: %s\n", cmd ); +} + + +/* +==================== +CG_ExecuteNewServerCommands + +Execute all of the server commands that were received along +with this this snapshot. +==================== +*/ +void CG_ExecuteNewServerCommands( int latestSequence ) { + while ( cgs.serverCommandSequence < latestSequence ) { + if ( trap_GetServerCommand( ++cgs.serverCommandSequence ) ) { + CG_ServerCommand(); + } + } +} diff --git a/src/cgame/cg_snapshot.c b/src/cgame/cg_snapshot.c new file mode 100644 index 0000000..8c164bd --- /dev/null +++ b/src/cgame/cg_snapshot.c @@ -0,0 +1,526 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// cg_snapshot.c -- things that happen on snapshot transition, +// not necessarily every single rendered frame + + + +#include "cg_local.h" + + + +/* +================== +CG_ResetEntity +================== +*/ +static void CG_ResetEntity( centity_t *cent ) { + // if an event is set, assume it is new enough to use + // if the event had timed out, it would have been cleared + // RF, not needed for wolf + // DHM - Nerve :: Wolf is now using this. + cent->previousEvent = 0; + + cent->trailTime = cg.snap->serverTime; + + // Ridah + cent->headJuncIndex = 0; + cent->headJuncIndex2 = 0; + // RF, disabled this, since we clear events out now in g_main.c, this is redundant, and actually + // causes "double-door-sound" syndrome if a door is triggered and heard, and then comes into view before + // it has timed out in g_main.c, therefore playing the sound again, since it thinks it hasn't processed this + // event yet. + //cent->previousEventSequence = 0; + // done. + + VectorCopy( cent->currentState.origin, cent->lerpOrigin ); + VectorCopy( cent->currentState.angles, cent->lerpAngles ); + if ( cent->currentState.eType == ET_PLAYER ) { + CG_ResetPlayerEntity( cent ); + } +} + + + +/* +=============== +CG_TransitionEntity + +cent->nextState is moved to cent->currentState and events are fired +=============== +*/ +static void CG_TransitionEntity( centity_t *cent ) { + + // Ridah, update the fireDir if it's on fire + if ( CG_EntOnFire( cent ) ) { + vec3_t newDir, newPos, oldPos; + float adjust; + // + BG_EvaluateTrajectory( ¢->nextState.pos, cg.snap->serverTime, newPos ); + BG_EvaluateTrajectory( ¢->currentState.pos, cg.snap->serverTime, oldPos ); + // update the fireRiseDir + VectorSubtract( oldPos, newPos, newDir ); + // fire should go upwards if travelling slow + newDir[2] += 2; + if ( VectorNormalize( newDir ) < 1 ) { + VectorClear( newDir ); + newDir[2] = 1; + } + // now move towards the newDir + adjust = 0.3; + VectorMA( cent->fireRiseDir, adjust, newDir, cent->fireRiseDir ); + if ( VectorNormalize( cent->fireRiseDir ) <= 0.1 ) { + VectorCopy( newDir, cent->fireRiseDir ); + } + } + + //----(SA) the ent lost or gained some part(s), do any necessary effects + //TODO: check for ai first + if ( cent->currentState.dmgFlags != cent->nextState.dmgFlags ) { + CG_AttachedPartChange( cent ); + } + + + cent->currentState = cent->nextState; + cent->currentValid = qtrue; + + // reset if the entity wasn't in the last frame or was teleported + if ( !cent->interpolate ) { + CG_ResetEntity( cent ); + } + + // clear the next state. if will be set by the next CG_SetNextSnap + cent->interpolate = qfalse; + + // check for events + CG_CheckEvents( cent ); +} + + +/* +================== +CG_SetInitialSnapshot + +This will only happen on the very first snapshot, or +on tourney restarts. All other times will use +CG_TransitionSnapshot instead. + +FIXME: Also called by map_restart? +================== +*/ +void CG_SetInitialSnapshot( snapshot_t *snap ) { + char buf[64]; + int i; + centity_t *cent; + entityState_t *state; + + cg.snap = snap; + + BG_PlayerStateToEntityState( &snap->ps, &cg_entities[ snap->ps.clientNum ].currentState, qfalse ); + + // sort out solid entities + CG_BuildSolidList(); + + CG_ExecuteNewServerCommands( snap->serverCommandSequence ); + + trap_SendClientCommand( "fogswitch 0" ); // clear it out so the set below will take + + trap_Cvar_VariableStringBuffer( "r_savegameFogColor", buf, sizeof( buf ) ); + trap_Cvar_Set( "r_savegameFogColor", "0" ); + if ( strlen( buf ) > 1 ) { + if ( !Q_stricmp( buf, "none" ) ) { + trap_SendClientCommand( "fogswitch 0" ); // 'off' + } else { + trap_SendClientCommand( va( "fogswitch %s", buf ) ); + } + } else { + trap_Cvar_VariableStringBuffer( "r_mapFogColor", buf, sizeof( buf ) ); + trap_SendClientCommand( va( "fogswitch %s", buf ) ); + } + + // set our local weapon selection pointer to + // what the server has indicated the current weapon is + CG_Respawn(); + + for ( i = 0 ; i < cg.snap->numEntities ; i++ ) { + state = &cg.snap->entities[ i ]; + cent = &cg_entities[ state->number ]; + + memcpy( ¢->currentState, state, sizeof( entityState_t ) ); + //cent->currentState = *state; + cent->interpolate = qfalse; + cent->currentValid = qtrue; + + CG_ResetEntity( cent ); + + // check for events + CG_CheckEvents( cent ); + } + + // DHM - Nerve :: Set cg.clientNum so that it may be used elsewhere + cg.clientNum = snap->ps.clientNum; + + // NERVE - SMF + { + static char prevmap[64] = { 0 }; + char curmap[64]; + + trap_Cvar_VariableStringBuffer( "mapname", curmap, 64 ); + + if ( cgs.gametype == GT_WOLF && Q_stricmp( curmap, prevmap ) ) { + strcpy( prevmap, curmap ); + trap_SendConsoleCommand( "openLimboMenu\n" ); + } + } + // -NERVE - SMF +} + + +/* +=================== +CG_TransitionSnapshot + +The transition point from snap to nextSnap has passed +=================== +*/ +static void CG_TransitionSnapshot( void ) { + centity_t *cent; + snapshot_t *oldFrame; + int i; + + if ( !cg.snap ) { + CG_Error( "CG_TransitionSnapshot: NULL cg.snap" ); + } + if ( !cg.nextSnap ) { + CG_Error( "CG_TransitionSnapshot: NULL cg.nextSnap" ); + } + + // execute any server string commands before transitioning entities + CG_ExecuteNewServerCommands( cg.nextSnap->serverCommandSequence ); + + // if we had a map_restart, set everthing with initial + + if ( !( cg.snap ) || !( cg.nextSnap ) ) { + return; + } + + // clear the currentValid flag for all entities in the existing snapshot + for ( i = 0 ; i < cg.snap->numEntities ; i++ ) { + cent = &cg_entities[ cg.snap->entities[ i ].number ]; + cent->currentValid = qfalse; + } + + // move nextSnap to snap and do the transitions + oldFrame = cg.snap; + cg.snap = cg.nextSnap; + + BG_PlayerStateToEntityState( &cg.snap->ps, &cg_entities[ cg.snap->ps.clientNum ].currentState, qfalse ); + cg_entities[ cg.snap->ps.clientNum ].interpolate = qfalse; + + for ( i = 0 ; i < cg.snap->numEntities ; i++ ) { + cent = &cg_entities[ cg.snap->entities[ i ].number ]; + CG_TransitionEntity( cent ); + } + + cg.nextSnap = NULL; + + // check for playerstate transition events + if ( oldFrame ) { + playerState_t *ops, *ps; + + ops = &oldFrame->ps; + ps = &cg.snap->ps; + // teleporting checks are irrespective of prediction + if ( ( ps->eFlags ^ ops->eFlags ) & EF_TELEPORT_BIT ) { + cg.thisFrameTeleport = qtrue; // will be cleared by prediction code + } + + // if we are not doing client side movement prediction for any + // reason, then the client events and view changes will be issued now + if ( cg.demoPlayback || ( cg.snap->ps.pm_flags & PMF_FOLLOW ) + || cg_nopredict.integer || cg_synchronousClients.integer ) { + CG_TransitionPlayerState( ps, ops ); + } + } + +} + + +/* +=================== +CG_SetNextSnap + +A new snapshot has just been read in from the client system. +=================== +*/ +static void CG_SetNextSnap( snapshot_t *snap ) { + int num; + entityState_t *es; + centity_t *cent; + + cg.nextSnap = snap; + + BG_PlayerStateToEntityState( &snap->ps, &cg_entities[ snap->ps.clientNum ].nextState, qfalse ); + cg_entities[ cg.snap->ps.clientNum ].interpolate = qtrue; + + // check for extrapolation errors + for ( num = 0 ; num < snap->numEntities ; num++ ) { + es = &snap->entities[num]; + cent = &cg_entities[ es->number ]; + + memcpy( ¢->nextState, es, sizeof( entityState_t ) ); + //cent->nextState = *es; + + // if this frame is a teleport, or the entity wasn't in the + // previous frame, don't interpolate + if ( !cent->currentValid || ( ( cent->currentState.eFlags ^ es->eFlags ) & EF_TELEPORT_BIT ) ) { + cent->interpolate = qfalse; + } else { + cent->interpolate = qtrue; + } + } + + // if the next frame is a teleport for the playerstate, we + // can't interpolate during demos + if ( cg.snap && ( ( snap->ps.eFlags ^ cg.snap->ps.eFlags ) & EF_TELEPORT_BIT ) ) { + cg.nextFrameTeleport = qtrue; + } else { + cg.nextFrameTeleport = qfalse; + } + + // if changing follow mode, don't interpolate + if ( cg.nextSnap->ps.clientNum != cg.snap->ps.clientNum ) { + cg.nextFrameTeleport = qtrue; + } + + // if changing server restarts, don't interpolate + if ( ( cg.nextSnap->snapFlags ^ cg.snap->snapFlags ) & SNAPFLAG_SERVERCOUNT ) { + cg.nextFrameTeleport = qtrue; + } + + // sort out solid entities + CG_BuildSolidList(); +} + + +/* +======================== +CG_ReadNextSnapshot + +This is the only place new snapshots are requested +This may increment cgs.processedSnapshotNum multiple +times if the client system fails to return a +valid snapshot. +======================== +*/ +static snapshot_t *CG_ReadNextSnapshot( void ) { + qboolean r; + snapshot_t *dest; + + if ( cg.latestSnapshotNum > cgs.processedSnapshotNum + 1000 ) { + CG_Printf( "WARNING: CG_ReadNextSnapshot: way out of range, %i > %i", + cg.latestSnapshotNum, cgs.processedSnapshotNum ); + } + + while ( cgs.processedSnapshotNum < cg.latestSnapshotNum ) { + // decide which of the two slots to load it into + if ( cg.snap == &cg.activeSnapshots[0] ) { + dest = &cg.activeSnapshots[1]; + } else { + dest = &cg.activeSnapshots[0]; + } + + // try to read the snapshot from the client system + cgs.processedSnapshotNum++; + r = trap_GetSnapshot( cgs.processedSnapshotNum, dest ); + + // FIXME: why would trap_GetSnapshot return a snapshot with the same server time + if ( cg.snap && r && dest->serverTime == cg.snap->serverTime ) { + //continue; + } + + // if it succeeded, return + if ( r ) { + CG_AddLagometerSnapshotInfo( dest ); + + // RF, if we have no weapon selected, and this snapshots says we have a weapon, then switch to that + if ( cg.snap && !cg.weaponSelect && cg.snap->ps.weapon ) { + cg.weaponSelect = cg.snap->ps.weapon; + cg.weaponSelectTime = cg.time; + } + + // Ridah, savegame: we should use this as our new base snapshot + // server has been restarted + if ( cg.snap && ( dest->snapFlags ^ cg.snap->snapFlags ) & SNAPFLAG_SERVERCOUNT ) { + int i; + centity_t backupCent; + CG_SetInitialSnapshot( dest ); + cg.nextFrameTeleport = qtrue; + cg.damageTime = 0; + cg.duckTime = -1; + cg.landTime = -1; + cg.stepTime = -1; + // RF, loadgame hasn't occured yet, so this is likely wrong + //cg.weaponSelect = cg.snap->ps.weapon; + cg.weaponSelectTime = cg.time; + memset( cg.viewDamage, 0, sizeof( cg.viewDamage ) ); + memset( cg.cameraShake, 0, sizeof( cg.cameraShake ) ); + // go through an reset the cent's + for ( i = 0; i < MAX_GENTITIES; i++ ) { + backupCent = cg_entities[i]; + memset( &cg_entities[i], 0, sizeof( centity_t ) ); + cg_entities[i].currentState = backupCent.currentState; + cg_entities[i].nextState = backupCent.nextState; + cg_entities[i].currentValid = backupCent.currentValid; + cg_entities[i].interpolate = backupCent.interpolate; + } + // reset the predicted cent + memset( &cg.predictedPlayerEntity, 0, sizeof( centity_t ) ); + cg.predictedPlayerEntity.currentState = backupCent.currentState; + cg.predictedPlayerEntity.nextState = backupCent.nextState; + cg.predictedPlayerEntity.currentValid = backupCent.currentValid; + cg.predictedPlayerEntity.interpolate = backupCent.interpolate; + + return NULL; + } + + return dest; + } + + // a GetSnapshot will return failure if the snapshot + // never arrived, or is so old that its entities + // have been shoved off the end of the circular + // buffer in the client system. + + // record as a dropped packet + CG_AddLagometerSnapshotInfo( NULL ); + + // If there are additional snapshots, continue trying to + // read them. + } + + // nothing left to read + return NULL; +} + + +/* +============ +CG_ProcessSnapshots + +We are trying to set up a renderable view, so determine +what the simulated time is, and try to get snapshots +both before and after that time if available. + +If we don't have a valid cg.snap after exiting this function, +then a 3D game view cannot be rendered. This should only happen +right after the initial connection. After cg.snap has been valid +once, it will never turn invalid. + +Even if cg.snap is valid, cg.nextSnap may not be, if the snapshot +hasn't arrived yet (it becomes an extrapolating situation instead +of an interpolating one) + +============ +*/ +void CG_ProcessSnapshots( void ) { + snapshot_t *snap; + int n; + + // see what the latest snapshot the client system has is + trap_GetCurrentSnapshotNumber( &n, &cg.latestSnapshotTime ); + if ( n != cg.latestSnapshotNum ) { + if ( n < cg.latestSnapshotNum ) { + // this should never happen + CG_Error( "CG_ProcessSnapshots: n < cg.latestSnapshotNum" ); + } + cg.latestSnapshotNum = n; + } + + // If we have yet to receive a snapshot, check for it. + // Once we have gotten the first snapshot, cg.snap will + // always have valid data for the rest of the game + while ( !cg.snap ) { + snap = CG_ReadNextSnapshot(); + if ( !snap ) { + // we can't continue until we get a snapshot + return; + } + + // set our weapon selection to what + // the playerstate is currently using + if ( !( snap->snapFlags & SNAPFLAG_NOT_ACTIVE ) ) { + CG_SetInitialSnapshot( snap ); + } + } + + // loop until we either have a valid nextSnap with a serverTime + // greater than cg.time to interpolate towards, or we run + // out of available snapshots + do { + // if we don't have a nextframe, try and read a new one in + if ( !cg.nextSnap ) { + snap = CG_ReadNextSnapshot(); + + // if we still don't have a nextframe, we will just have to + // extrapolate + if ( !snap ) { + break; + } + + CG_SetNextSnap( snap ); + + // if time went backwards, we have a level restart + if ( cg.nextSnap->serverTime < cg.snap->serverTime ) { + CG_Error( "CG_ProcessSnapshots: Server time went backwards" ); + } + } + + // if our time is < nextFrame's, we have a nice interpolating state + if ( cg.time >= cg.snap->serverTime && cg.time < cg.nextSnap->serverTime ) { + break; + } + + // we have passed the transition from nextFrame to frame + CG_TransitionSnapshot(); + } while ( 1 ); + + // assert our valid conditions upon exiting + if ( cg.snap == NULL ) { + CG_Error( "CG_ProcessSnapshots: cg.snap == NULL" ); + } + if ( cg.time < cg.snap->serverTime ) { + // this can happen right after a vid_restart + cg.time = cg.snap->serverTime; + } + if ( cg.nextSnap != NULL && cg.nextSnap->serverTime <= cg.time ) { + CG_Error( "CG_ProcessSnapshots: cg.nextSnap->serverTime <= cg.time" ); + } + +} diff --git a/src/cgame/cg_sound.c b/src/cgame/cg_sound.c new file mode 100644 index 0000000..d84da3e --- /dev/null +++ b/src/cgame/cg_sound.c @@ -0,0 +1,461 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// Ridah, cg_sound.c - parsing and use of sound script files + +#include "cg_local.h" + + +// we have to define these static lists, since we can't alloc memory within the cgame + +soundScript_t* hashTable[FILE_HASH_SIZE]; +soundScript_t soundScripts[MAX_SOUND_SCRIPTS]; +int numSoundScripts = 0; +soundScriptSound_t soundScriptSounds[MAX_SOUND_SCRIPT_SOUNDS]; +int numSoundScriptSounds = 0; + +/* +================ +return a hash value for the filename +================ +*/ +static long generateHashValue( const char *fname ) { + int i; + long hash; + char letter; + + hash = 0; + i = 0; + while ( fname[i] != '\0' ) { + letter = tolower( fname[i] ); + if ( letter == '.' ) { + break; // don't include extension + } + if ( letter == '\\' ) { + letter = '/'; // damn path names + } + hash += (long)( letter ) * ( i + 119 ); + i++; + } + hash &= ( FILE_HASH_SIZE - 1 ); + return hash; +} + +/* +============== +CG_SoundScriptPrecache + + returns the index+1 of the script in the global list, for fast calling +============== +*/ +int CG_SoundScriptPrecache( const char *name ) { + soundScriptSound_t *scriptSound; + long hash; + char *s; + soundScript_t *sound; + byte buf[1024]; + + if ( !name || !name[0] ) { + return 0; + } + + hash = generateHashValue( name ); + + s = (char *)name; + sound = hashTable[hash]; + while ( sound ) { + if ( !Q_strcasecmp( s, sound->name ) ) { + // found a match, precache these sounds + scriptSound = sound->soundList; + if ( !sound->streaming ) { + while ( scriptSound ) { + scriptSound->sfxHandle = trap_S_RegisterSound( scriptSound->filename ); + scriptSound = scriptSound->next; + } + } else /*if (cg_buildScript.integer)*/ { // RF, 11/6/01 enabled this permanently so that streaming sounds get touched within file system on startup + while ( scriptSound ) { + // just open the file so it gets copied to the build dir + fileHandle_t f; + trap_FS_FOpenFile( scriptSound->filename, &f, FS_READ ); + // read a few bytes so the operating system does a better job of caching it for us + trap_FS_Read( buf, sizeof( buf ), f ); + trap_FS_FCloseFile( f ); + scriptSound = scriptSound->next; + } + } + return sound->index + 1; + } + sound = sound->nextHash; + } + + return 0; +} + +/* +============== +CG_SoundPickOldestRandomSound +============== +*/ +void CG_SoundPickOldestRandomSound( soundScript_t *sound, vec3_t org, int entnum ) { + int oldestTime = 0; // TTimo: init + soundScriptSound_t *oldestSound; + soundScriptSound_t *scriptSound; + vec3_t eOrg; + + oldestSound = NULL; + scriptSound = sound->soundList; + while ( scriptSound ) { + if ( !oldestSound || ( scriptSound->lastPlayed < oldestTime ) ) { + oldestTime = scriptSound->lastPlayed; + oldestSound = scriptSound; + } + scriptSound = scriptSound->next; + } + + if ( oldestSound ) { + // play this sound + if ( !sound->streaming ) { + if ( !oldestSound->sfxHandle ) { + oldestSound->sfxHandle = trap_S_RegisterSound( oldestSound->filename ); + } + if ( sound->attenuation ) { + trap_S_StartSound( org, entnum, sound->channel, oldestSound->sfxHandle ); + } else { + trap_S_StartLocalSound( oldestSound->sfxHandle, sound->channel ); + } + } else { + trap_S_StartStreamingSound( oldestSound->filename, sound->looping ? oldestSound->filename : NULL, entnum, sound->channel, sound->attenuation ); + } + oldestSound->lastPlayed = cg.time; + // + // shake the view? + if ( sound->shakeScale ) { + // get the origin + if ( org ) { + VectorCopy( org, eOrg ); + } else { + VectorCopy( cg_entities[entnum].lerpOrigin, eOrg ); + } + // + // start the shaker + CG_StartShakeCamera( sound->shakeScale, sound->shakeDuration, eOrg, sound->shakeRadius ); + } + } else { + CG_Error( "Unable to locate a valid sound for soundScript: %s\n", sound->name ); + } +} + +/* +============== +CG_SoundPlaySoundScript + + returns qtrue is a script is found +============== +*/ +qboolean CG_SoundPlaySoundScript( const char *name, vec3_t org, int entnum ) { + long hash; + char *s; + soundScript_t *sound; + + if ( !name || !name[0] ) { + return qfalse; + } + + hash = generateHashValue( name ); + + s = (char *)name; + sound = hashTable[hash]; + while ( sound ) { + if ( !Q_strcasecmp( s, sound->name ) ) { + // found a match, pick the oldest sound + CG_SoundPickOldestRandomSound( sound, org, entnum ); + return qtrue; + } + sound = sound->nextHash; + } + + //CG_Printf( S_COLOR_RED "CG_SoundPlaySoundScript: cannot find sound script '%s'\n", name ); + return qfalse; +} + + + +/* +============== +CG_SoundPlayIndexedScript + + returns qtrue is a script is found +============== +*/ +void CG_SoundPlayIndexedScript( int index, vec3_t org, int entnum ) { + soundScript_t *sound; + + if ( !index ) { + return; + } + + if ( index > numSoundScripts ) { + return; + } + + sound = &soundScripts[index - 1]; + // pick the oldest sound + CG_SoundPickOldestRandomSound( sound, org, entnum ); +} + +/* +=============== +CG_SoundParseSounds +=============== +*/ +static void CG_SoundParseSounds( char *filename, char *buffer ) { + char *token, **text; + int s; + long hash; + soundScript_t sound; // the current sound being read + soundScriptSound_t *scriptSound; + qboolean inSound, wantSoundName; + + s = 0; + inSound = qfalse; + wantSoundName = qtrue; + text = &buffer; + + while ( 1 ) { + token = COM_ParseExt( text, qtrue ); + if ( !token[0] ) { + if ( inSound ) { + CG_Error( "no concluding '}' in sound %s, file %s\n", sound.name, filename ); + } + return; + } + if ( !Q_strcasecmp( token, "{" ) ) { + if ( inSound ) { + CG_Error( "no concluding '}' in sound %s, file %s\n", sound.name, filename ); + } + if ( wantSoundName ) { + CG_Error( "'{' found but not expected, after %s, file %s\n", sound.name, filename ); + } + inSound = qtrue; + continue; + } + if ( !Q_strcasecmp( token, "}" ) ) { + if ( !inSound ) { + CG_Error( "'}' unexpected after sound %s, file %s\n", sound.name, filename ); + } + + // end of a sound, copy it to the global list and stick it in the hashTable + hash = generateHashValue( sound.name ); + sound.nextHash = hashTable[hash]; + soundScripts[numSoundScripts] = sound; + hashTable[hash] = &soundScripts[numSoundScripts++]; + + if ( numSoundScripts == MAX_SOUND_SCRIPTS ) { + CG_Error( "MAX_SOUND_SCRIPTS exceeded.\nReduce number of sound scripts.\n" ); + } + + inSound = qfalse; + wantSoundName = qtrue; + continue; + } + if ( !inSound ) { + // this is the identifier for a new sound + if ( !wantSoundName ) { + CG_Error( "'%s' unexpected after sound %s, file %s\n", token, sound.name, filename ); + } + memset( &sound, 0, sizeof( sound ) ); + Q_strncpyz( sound.name, token, sizeof( sound.name ) ); + wantSoundName = qfalse; + sound.index = numSoundScripts; + // setup the new sound defaults + sound.channel = CHAN_AUTO; + sound.attenuation = 1; // default to fade away with distance (for streaming sounds) + // + continue; + } + + // we are inside a sound script + + if ( !Q_strcasecmp( token, "channel" ) ) { + // ignore this now, just look for the channel identifiers explicitly + continue; + } + if ( !Q_strcasecmp( token, "local" ) ) { + sound.channel = CHAN_LOCAL; + continue; + } else if ( !Q_strcasecmp( token, "announcer" ) ) { + sound.channel = CHAN_ANNOUNCER; + continue; + } else if ( !Q_strcasecmp( token, "body" ) ) { + sound.channel = CHAN_BODY; + continue; + } else if ( !Q_strcasecmp( token, "voice" ) ) { + sound.channel = CHAN_VOICE; + continue; + } else if ( !Q_strcasecmp( token, "weapon" ) ) { + sound.channel = CHAN_WEAPON; + continue; + } else if ( !Q_strcasecmp( token, "item" ) ) { + sound.channel = CHAN_ITEM; + continue; + } else if ( !Q_strcasecmp( token, "auto" ) ) { + sound.channel = CHAN_AUTO; + continue; + } + if ( !Q_strcasecmp( token, "global" ) ) { + sound.attenuation = 0; + continue; + } + if ( !Q_strcasecmp( token, "streaming" ) ) { + sound.streaming = qtrue; + continue; + } + if ( !Q_strcasecmp( token, "looping" ) ) { + sound.looping = qtrue; + continue; + } + if ( !Q_strcasecmp( token, "shake" ) ) { + token = COM_ParseExt( text, qfalse ); + sound.shakeScale = atof( token ); + token = COM_ParseExt( text, qfalse ); + sound.shakeRadius = atof( token ); + token = COM_ParseExt( text, qfalse ); + if ( !token || !token[0] ) { + sound.shakeDuration = 350 + 900 * ( sound.shakeScale * sound.shakeScale ); + } else { + sound.shakeDuration = atoi( token ); + } + } + if ( !Q_strcasecmp( token, "sound" ) ) { + // grab a free scriptSound + scriptSound = &soundScriptSounds[numSoundScriptSounds++]; + + if ( numSoundScripts == MAX_SOUND_SCRIPT_SOUNDS ) { + CG_Error( "MAX_SOUND_SCRIPT_SOUNDS exceeded.\nReduce number of sound scripts.\n" ); + } + + token = COM_ParseExt( text, qtrue ); + Q_strncpyz( scriptSound->filename, token, sizeof( scriptSound->filename ) ); + scriptSound->lastPlayed = 0; + scriptSound->sfxHandle = 0; + scriptSound->next = sound.soundList; + sound.soundList = scriptSound; + continue; + } + } +} + +/* +=============== +CG_SoundLoadSoundFiles +=============== +*/ +#define MAX_SOUND_FILES 128 +#define MAX_BUFFER 20000 +static void CG_SoundLoadSoundFiles( void ) { + char soundFiles[MAX_SOUND_FILES][MAX_QPATH]; + char buffer[MAX_BUFFER]; + char *text; + char filename[MAX_QPATH]; + fileHandle_t f; + int numSounds; + int i, len; + char *token; + + // scan for sound files + Com_sprintf( filename, MAX_QPATH, "sound/scripts/filelist.txt" ); + len = trap_FS_FOpenFile( filename, &f, FS_READ ); + if ( len <= 0 ) { + CG_Printf( S_COLOR_RED "WARNING: no sound files found (filelist.txt not found in sound/scripts)\n" ); + return; + } + if ( len > MAX_BUFFER ) { + CG_Error( "%s is too big, make it smaller (max = %i bytes)\n", filename, MAX_BUFFER ); + } + // load the file into memory + trap_FS_Read( buffer, len, f ); + buffer[len] = 0; + trap_FS_FCloseFile( f ); + // parse the list + text = buffer; + numSounds = 0; + while ( 1 ) { + token = COM_ParseExt( &text, qtrue ); + if ( !token[0] ) { + break; + } + Com_sprintf( soundFiles[numSounds++], MAX_QPATH, token ); + } + + if ( !numSounds ) { + CG_Printf( S_COLOR_RED "WARNING: no sound files found\n" ); + return; + } + + // load and parse sound files + for ( i = 0; i < numSounds; i++ ) + { + Com_sprintf( filename, sizeof( filename ), "sound/scripts/%s", soundFiles[i] ); + CG_Printf( "...loading '%s'\n", filename ); + len = trap_FS_FOpenFile( filename, &f, FS_READ ); + if ( len <= 0 ) { + CG_Error( "Couldn't load %s", filename ); + } + if ( len > MAX_BUFFER ) { + CG_Error( "%s is too big, make it smaller (max = %i bytes)\n", filename, MAX_BUFFER ); + } + memset( buffer, 0, sizeof( buffer ) ); + trap_FS_Read( buffer, len, f ); + trap_FS_FCloseFile( f ); + CG_SoundParseSounds( filename, buffer ); + } +} + +/* +============== +CG_SoundInit +============== +*/ +void CG_SoundInit( void ) { + + if ( numSoundScripts ) { + // keep all the information, just reset the vars + int i; + + for ( i = 0; i < numSoundScriptSounds; i++ ) { + soundScriptSounds[i].lastPlayed = 0; + soundScriptSounds[i].sfxHandle = 0; + } + } else { + CG_Printf( "\n.........................\n" + "Initializing Sound Scripts\n" ); + CG_SoundLoadSoundFiles(); + CG_Printf( "done.\n" ); + } + +} diff --git a/src/cgame/cg_syscalls.c b/src/cgame/cg_syscalls.c new file mode 100644 index 0000000..3fd9c1d --- /dev/null +++ b/src/cgame/cg_syscalls.c @@ -0,0 +1,542 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// cg_syscalls.c -- this file is only included when building a dll +// cg_syscalls.asm is included instead when building a qvm +#include "cg_local.h" + +static int ( QDECL * syscall )( int arg, ... ) = ( int ( QDECL * )( int, ... ) ) - 1; + +// TTimo: guarding +#if defined( __MACOS__ ) +#pragma export on +#endif +void dllEntry( int ( QDECL *syscallptr )( int arg,... ) ) { +#if defined( __MACOS__ ) +#pragma export off +#endif + syscall = syscallptr; +} + + +int PASSFLOAT( float x ) { + float floatTemp; + floatTemp = x; + return *(int *)&floatTemp; +} + +void trap_Print( const char *fmt ) { + syscall( CG_PRINT, fmt ); +} + +void trap_Error( const char *fmt ) { + syscall( CG_ERROR, fmt ); +} + +int trap_Milliseconds( void ) { + return syscall( CG_MILLISECONDS ); +} + +void trap_Cvar_Register( vmCvar_t *vmCvar, const char *varName, const char *defaultValue, int flags ) { + syscall( CG_CVAR_REGISTER, vmCvar, varName, defaultValue, flags ); +} + +void trap_Cvar_Update( vmCvar_t *vmCvar ) { + syscall( CG_CVAR_UPDATE, vmCvar ); +} + +void trap_Cvar_Set( const char *var_name, const char *value ) { + syscall( CG_CVAR_SET, var_name, value ); +} + +void trap_Cvar_VariableStringBuffer( const char *var_name, char *buffer, int bufsize ) { + syscall( CG_CVAR_VARIABLESTRINGBUFFER, var_name, buffer, bufsize ); +} + +int trap_Argc( void ) { + return syscall( CG_ARGC ); +} + +void trap_Argv( int n, char *buffer, int bufferLength ) { + syscall( CG_ARGV, n, buffer, bufferLength ); +} + +void trap_Args( char *buffer, int bufferLength ) { + syscall( CG_ARGS, buffer, bufferLength ); +} + +int trap_FS_FOpenFile( const char *qpath, fileHandle_t *f, fsMode_t mode ) { + return syscall( CG_FS_FOPENFILE, qpath, f, mode ); +} + +void trap_FS_Read( void *buffer, int len, fileHandle_t f ) { + syscall( CG_FS_READ, buffer, len, f ); +} + +void trap_FS_Write( const void *buffer, int len, fileHandle_t f ) { + syscall( CG_FS_WRITE, buffer, len, f ); +} + +void trap_FS_FCloseFile( fileHandle_t f ) { + syscall( CG_FS_FCLOSEFILE, f ); +} + +void trap_SendConsoleCommand( const char *text ) { + syscall( CG_SENDCONSOLECOMMAND, text ); +} + +void trap_AddCommand( const char *cmdName ) { + syscall( CG_ADDCOMMAND, cmdName ); +} + +void trap_SendClientCommand( const char *s ) { + syscall( CG_SENDCLIENTCOMMAND, s ); +} + +void trap_UpdateScreen( void ) { + syscall( CG_UPDATESCREEN ); +} + +void trap_CM_LoadMap( const char *mapname ) { + syscall( CG_CM_LOADMAP, mapname ); +} + +int trap_CM_NumInlineModels( void ) { + return syscall( CG_CM_NUMINLINEMODELS ); +} + +clipHandle_t trap_CM_InlineModel( int index ) { + return syscall( CG_CM_INLINEMODEL, index ); +} + +clipHandle_t trap_CM_TempBoxModel( const vec3_t mins, const vec3_t maxs ) { + return syscall( CG_CM_TEMPBOXMODEL, mins, maxs ); +} + +clipHandle_t trap_CM_TempCapsuleModel( const vec3_t mins, const vec3_t maxs ) { + return syscall( CG_CM_TEMPCAPSULEMODEL, mins, maxs ); +} + +int trap_CM_PointContents( const vec3_t p, clipHandle_t model ) { + return syscall( CG_CM_POINTCONTENTS, p, model ); +} + +int trap_CM_TransformedPointContents( const vec3_t p, clipHandle_t model, const vec3_t origin, const vec3_t angles ) { + return syscall( CG_CM_TRANSFORMEDPOINTCONTENTS, p, model, origin, angles ); +} + +void trap_CM_BoxTrace( trace_t *results, const vec3_t start, const vec3_t end, + const vec3_t mins, const vec3_t maxs, + clipHandle_t model, int brushmask ) { + syscall( CG_CM_BOXTRACE, results, start, end, mins, maxs, model, brushmask ); +} + +void trap_CM_TransformedBoxTrace( trace_t *results, const vec3_t start, const vec3_t end, + const vec3_t mins, const vec3_t maxs, + clipHandle_t model, int brushmask, + const vec3_t origin, const vec3_t angles ) { + syscall( CG_CM_TRANSFORMEDBOXTRACE, results, start, end, mins, maxs, model, brushmask, origin, angles ); +} + +void trap_CM_CapsuleTrace( trace_t *results, const vec3_t start, const vec3_t end, + const vec3_t mins, const vec3_t maxs, + clipHandle_t model, int brushmask ) { + syscall( CG_CM_CAPSULETRACE, results, start, end, mins, maxs, model, brushmask ); +} + +void trap_CM_TransformedCapsuleTrace( trace_t *results, const vec3_t start, const vec3_t end, + const vec3_t mins, const vec3_t maxs, + clipHandle_t model, int brushmask, + const vec3_t origin, const vec3_t angles ) { + syscall( CG_CM_TRANSFORMEDCAPSULETRACE, results, start, end, mins, maxs, model, brushmask, origin, angles ); +} + +int trap_CM_MarkFragments( int numPoints, const vec3_t *points, + const vec3_t projection, + int maxPoints, vec3_t pointBuffer, + int maxFragments, markFragment_t *fragmentBuffer ) { + return syscall( CG_CM_MARKFRAGMENTS, numPoints, points, projection, maxPoints, pointBuffer, maxFragments, fragmentBuffer ); +} + +void trap_S_StartSound( vec3_t origin, int entityNum, int entchannel, sfxHandle_t sfx ) { + syscall( CG_S_STARTSOUND, origin, entityNum, entchannel, sfx ); +} + +//----(SA) added +void trap_S_StartSoundEx( vec3_t origin, int entityNum, int entchannel, sfxHandle_t sfx, int flags ) { + syscall( CG_S_STARTSOUNDEX, origin, entityNum, entchannel, sfx, flags ); +} +//----(SA) end + +void trap_S_StartLocalSound( sfxHandle_t sfx, int channelNum ) { + syscall( CG_S_STARTLOCALSOUND, sfx, channelNum ); +} + +void trap_S_ClearLoopingSounds( qboolean killall ) { + syscall( CG_S_CLEARLOOPINGSOUNDS, killall ); +} + +void trap_S_AddLoopingSound( int entityNum, const vec3_t origin, const vec3_t velocity, sfxHandle_t sfx, int volume ) { + syscall( CG_S_ADDLOOPINGSOUND, entityNum, origin, velocity, 1250, sfx, volume ); // volume was previously removed from CG_S_ADDLOOPINGSOUND. I added 'range' +} + +//----(SA) added +void trap_S_AddRangedLoopingSound( int entityNum, const vec3_t origin, const vec3_t velocity, sfxHandle_t sfx, int range ) { + syscall( CG_S_ADDLOOPINGSOUND, entityNum, origin, velocity, range, sfx, 255 ); // RF, assume full volume, since thats how it worked before +} +//----(SA) end + +void trap_S_AddRealLoopingSound( int entityNum, const vec3_t origin, const vec3_t velocity, sfxHandle_t sfx ) { +// not in use +// syscall( CG_S_ADDREALLOOPINGSOUND, entityNum, origin, velocity, 1250, sfx, 255 ); //----(SA) modified +} + +void trap_S_StopLoopingSound( int entityNum ) { + syscall( CG_S_STOPLOOPINGSOUND, entityNum ); +} + +//----(SA) added +void trap_S_StopStreamingSound( int entityNum ) { + syscall( CG_S_STOPSTREAMINGSOUND, entityNum ); +} +//----(SA) end + +void trap_S_UpdateEntityPosition( int entityNum, const vec3_t origin ) { + syscall( CG_S_UPDATEENTITYPOSITION, entityNum, origin ); +} + +// Ridah, talking animations +int trap_S_GetVoiceAmplitude( int entityNum ) { + return syscall( CG_S_GETVOICEAMPLITUDE, entityNum ); +} +// done. + +void trap_S_Respatialize( int entityNum, const vec3_t origin, vec3_t axis[3], int inwater ) { + syscall( CG_S_RESPATIALIZE, entityNum, origin, axis, inwater ); +} + +sfxHandle_t trap_S_RegisterSound( const char *sample ) { + CG_DrawInformation(); + return syscall( CG_S_REGISTERSOUND, sample ); +} + +void trap_S_StartBackgroundTrack( const char *intro, const char *loop, int fadeupTime ) { + syscall( CG_S_STARTBACKGROUNDTRACK, intro, loop, fadeupTime ); +} + +void trap_S_FadeBackgroundTrack( float targetvol, int time, int num ) { // yes, i know. fadebackground coming in, fadestreaming going out. will have to see where functionality leads... + syscall( CG_S_FADESTREAMINGSOUND, PASSFLOAT( targetvol ), time, num ); // 'num' is '0' if it's music, '1' if it's "all streaming sounds" +} + +void trap_S_FadeAllSound( float targetvol, int time ) { + syscall( CG_S_FADEALLSOUNDS, PASSFLOAT( targetvol ), time ); +} + +//----(SA) end + +void trap_S_StartStreamingSound( const char *intro, const char *loop, int entnum, int channel, int attenuation ) { + syscall( CG_S_STARTSTREAMINGSOUND, intro, loop, entnum, channel, attenuation ); +} + +void trap_R_LoadWorldMap( const char *mapname ) { + syscall( CG_R_LOADWORLDMAP, mapname ); +} + +qhandle_t trap_R_RegisterModel( const char *name ) { + CG_DrawInformation(); + return syscall( CG_R_REGISTERMODEL, name ); +} + +//----(SA) added +qboolean trap_R_GetSkinModel( qhandle_t skinid, const char *type, char *name ) { + return syscall( CG_R_GETSKINMODEL, skinid, type, name ); +} + +qhandle_t trap_R_GetShaderFromModel( qhandle_t modelid, int surfnum, int withlightmap ) { + return syscall( CG_R_GETMODELSHADER, modelid, surfnum, withlightmap ); +} +//----(SA) end + +qhandle_t trap_R_RegisterSkin( const char *name ) { + CG_DrawInformation(); + return syscall( CG_R_REGISTERSKIN, name ); +} + +qhandle_t trap_R_RegisterShader( const char *name ) { + CG_DrawInformation(); + return syscall( CG_R_REGISTERSHADER, name ); +} + +qhandle_t trap_R_RegisterShaderNoMip( const char *name ) { + CG_DrawInformation(); + return syscall( CG_R_REGISTERSHADERNOMIP, name ); +} + +void trap_R_RegisterFont( const char *fontName, int pointSize, fontInfo_t *font ) { + syscall( CG_R_REGISTERFONT, fontName, pointSize, font ); +} + +void trap_R_ClearScene( void ) { + syscall( CG_R_CLEARSCENE ); +} + +void trap_R_AddRefEntityToScene( const refEntity_t *re ) { + syscall( CG_R_ADDREFENTITYTOSCENE, re ); +} + +void trap_R_AddPolyToScene( qhandle_t hShader, int numVerts, const polyVert_t *verts ) { + syscall( CG_R_ADDPOLYTOSCENE, hShader, numVerts, verts ); +} + +// Ridah +void trap_R_AddPolysToScene( qhandle_t hShader, int numVerts, const polyVert_t *verts, int numPolys ) { + syscall( CG_R_ADDPOLYSTOSCENE, hShader, numVerts, verts, numPolys ); +} + +void trap_RB_ZombieFXAddNewHit( int entityNum, const vec3_t hitPos, const vec3_t hitDir ) { + syscall( CG_RB_ZOMBIEFXADDNEWHIT, entityNum, hitPos, hitDir ); +} +// done. + +void trap_R_AddLightToScene( const vec3_t org, float intensity, float r, float g, float b, int overdraw ) { + syscall( CG_R_ADDLIGHTTOSCENE, org, PASSFLOAT( intensity ), PASSFLOAT( r ), PASSFLOAT( g ), PASSFLOAT( b ), overdraw ); +} + +//----(SA) +void trap_R_AddCoronaToScene( const vec3_t org, float r, float g, float b, float scale, int id, int flags ) { + syscall( CG_R_ADDCORONATOSCENE, org, PASSFLOAT( r ), PASSFLOAT( g ), PASSFLOAT( b ), PASSFLOAT( scale ), id, flags ); +} +//----(SA) + +//----(SA) +void trap_R_SetFog( int fogvar, int var1, int var2, float r, float g, float b, float density ) { + syscall( CG_R_SETFOG, fogvar, var1, var2, PASSFLOAT( r ), PASSFLOAT( g ), PASSFLOAT( b ), PASSFLOAT( density ) ); +} +//----(SA) +void trap_R_RenderScene( const refdef_t *fd ) { + syscall( CG_R_RENDERSCENE, fd ); +} + +void trap_R_SetColor( const float *rgba ) { + syscall( CG_R_SETCOLOR, rgba ); +} + +void trap_R_DrawStretchPic( float x, float y, float w, float h, + float s1, float t1, float s2, float t2, qhandle_t hShader ) { + syscall( CG_R_DRAWSTRETCHPIC, PASSFLOAT( x ), PASSFLOAT( y ), PASSFLOAT( w ), PASSFLOAT( h ), PASSFLOAT( s1 ), PASSFLOAT( t1 ), PASSFLOAT( s2 ), PASSFLOAT( t2 ), hShader ); +} + +void trap_R_DrawStretchPicGradient( float x, float y, float w, float h, + float s1, float t1, float s2, float t2, qhandle_t hShader, + const float *gradientColor, int gradientType ) { + syscall( CG_R_DRAWSTRETCHPIC_GRADIENT, PASSFLOAT( x ), PASSFLOAT( y ), PASSFLOAT( w ), PASSFLOAT( h ), PASSFLOAT( s1 ), PASSFLOAT( t1 ), PASSFLOAT( s2 ), PASSFLOAT( t2 ), hShader, gradientColor, gradientType ); +} + +void trap_R_ModelBounds( clipHandle_t model, vec3_t mins, vec3_t maxs ) { + syscall( CG_R_MODELBOUNDS, model, mins, maxs ); +} + +int trap_R_LerpTag( orientation_t *tag, const refEntity_t *refent, const char *tagName, int startIndex ) { + return syscall( CG_R_LERPTAG, tag, refent, tagName, startIndex ); +} + +void trap_R_RemapShader( const char *oldShader, const char *newShader, const char *timeOffset ) { + syscall( CG_R_REMAP_SHADER, oldShader, newShader, timeOffset ); +} + +void trap_GetGlconfig( glconfig_t *glconfig ) { + syscall( CG_GETGLCONFIG, glconfig ); +} + +void trap_GetGameState( gameState_t *gamestate ) { + syscall( CG_GETGAMESTATE, gamestate ); +} + +void trap_GetCurrentSnapshotNumber( int *snapshotNumber, int *serverTime ) { + syscall( CG_GETCURRENTSNAPSHOTNUMBER, snapshotNumber, serverTime ); +} + +qboolean trap_GetSnapshot( int snapshotNumber, snapshot_t *snapshot ) { + return syscall( CG_GETSNAPSHOT, snapshotNumber, snapshot ); +} + +qboolean trap_GetServerCommand( int serverCommandNumber ) { + return syscall( CG_GETSERVERCOMMAND, serverCommandNumber ); +} + +int trap_GetCurrentCmdNumber( void ) { + return syscall( CG_GETCURRENTCMDNUMBER ); +} + +qboolean trap_GetUserCmd( int cmdNumber, usercmd_t *ucmd ) { + return syscall( CG_GETUSERCMD, cmdNumber, ucmd ); +} + +void trap_SetUserCmdValue( int stateValue, int holdableValue, float sensitivityScale, int cld ) { //----(SA) // NERVE - SMF - added cld + syscall( CG_SETUSERCMDVALUE, stateValue, holdableValue, PASSFLOAT( sensitivityScale ), cld ); +} + +void testPrintInt( char *string, int i ) { + syscall( CG_TESTPRINTINT, string, i ); +} + +void testPrintFloat( char *string, float f ) { + syscall( CG_TESTPRINTFLOAT, string, PASSFLOAT( f ) ); +} + +int trap_MemoryRemaining( void ) { + return syscall( CG_MEMORY_REMAINING ); +} + +qboolean trap_loadCamera( int camNum, const char *name ) { + return syscall( CG_LOADCAMERA, camNum, name ); +} + +void trap_startCamera( int camNum, int time ) { + syscall( CG_STARTCAMERA, camNum, time ); +} + +//----(SA) added +void trap_stopCamera( int camNum ) { + syscall( CG_STOPCAMERA, camNum ); +} +//----(SA) end + +qboolean trap_getCameraInfo( int camNum, int time, vec3_t *origin, vec3_t *angles, float *fov ) { + return syscall( CG_GETCAMERAINFO, camNum, time, origin, angles, fov ); +} + + +qboolean trap_Key_IsDown( int keynum ) { + return syscall( CG_KEY_ISDOWN, keynum ); +} + +int trap_Key_GetCatcher( void ) { + return syscall( CG_KEY_GETCATCHER ); +} + +void trap_Key_SetCatcher( int catcher ) { + syscall( CG_KEY_SETCATCHER, catcher ); +} + +int trap_Key_GetKey( const char *binding ) { + return syscall( CG_KEY_GETKEY, binding ); +} + + +int trap_PC_AddGlobalDefine( char *define ) { + return syscall( CG_PC_ADD_GLOBAL_DEFINE, define ); +} + +int trap_PC_LoadSource( const char *filename ) { + return syscall( CG_PC_LOAD_SOURCE, filename ); +} + +int trap_PC_FreeSource( int handle ) { + return syscall( CG_PC_FREE_SOURCE, handle ); +} + +int trap_PC_ReadToken( int handle, pc_token_t *pc_token ) { + return syscall( CG_PC_READ_TOKEN, handle, pc_token ); +} + + +int trap_PC_SourceFileAndLine( int handle, char *filename, int *line ) { + return syscall( CG_PC_SOURCE_FILE_AND_LINE, handle, filename, line ); +} + +void trap_S_StopBackgroundTrack( void ) { + syscall( CG_S_STOPBACKGROUNDTRACK ); +} + +int trap_RealTime( qtime_t *qtime ) { + return syscall( CG_REAL_TIME, qtime ); +} + +void trap_SnapVector( float *v ) { + syscall( CG_SNAPVECTOR, v ); +} + +void trap_SendMoveSpeedsToGame( int entnum, char *movespeeds ) { + syscall( CG_SENDMOVESPEEDSTOGAME, entnum, movespeeds ); +} + +// this returns a handle. arg0 is the name in the format "idlogo.roq", set arg1 to NULL, alteredstates to qfalse (do not alter gamestate) +int trap_CIN_PlayCinematic( const char *arg0, int xpos, int ypos, int width, int height, int bits ) { + return syscall( CG_CIN_PLAYCINEMATIC, arg0, xpos, ypos, width, height, bits ); +} + +// stops playing the cinematic and ends it. should always return FMV_EOF +// cinematics must be stopped in reverse order of when they are started +e_status trap_CIN_StopCinematic( int handle ) { + return syscall( CG_CIN_STOPCINEMATIC, handle ); +} + + +// will run a frame of the cinematic but will not draw it. Will return FMV_EOF if the end of the cinematic has been reached. +e_status trap_CIN_RunCinematic( int handle ) { + return syscall( CG_CIN_RUNCINEMATIC, handle ); +} + + +// draws the current frame +void trap_CIN_DrawCinematic( int handle ) { + syscall( CG_CIN_DRAWCINEMATIC, handle ); +} + + +// allows you to resize the animation dynamically +void trap_CIN_SetExtents( int handle, int x, int y, int w, int h ) { + syscall( CG_CIN_SETEXTENTS, handle, x, y, w, h ); +} + +qboolean trap_GetEntityToken( char *buffer, int bufferSize ) { + return syscall( CG_GET_ENTITY_TOKEN, buffer, bufferSize ); +} + +//----(SA) added +// bring up a popup menu +extern void Menus_OpenByName( const char *p ); + +void trap_UI_Popup( const char *arg0 ) { + syscall( CG_INGAME_POPUP, arg0 ); +} + +// NERVE - SMF +void trap_UI_LimboChat( const char *arg0 ) { + syscall( CG_LIMBOCHAT, arg0 ); +} + +void trap_UI_ClosePopup( const char *arg0 ) { + syscall( CG_INGAME_CLOSEPOPUP, arg0 ); +} +// -NERVE - SMF + +qboolean trap_GetModelInfo( int clientNum, char *modelName, animModelInfo_t **modelInfo ) { + return syscall( CG_GETMODELINFO, clientNum, modelName, modelInfo ); +} \ No newline at end of file diff --git a/src/cgame/cg_trails.c b/src/cgame/cg_trails.c new file mode 100644 index 0000000..4956165 --- /dev/null +++ b/src/cgame/cg_trails.c @@ -0,0 +1,785 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// Ridah, cg_trails.c - draws a trail using multiple junction points + +#include "cg_local.h" + +typedef struct trailJunc_s +{ + struct trailJunc_s *nextGlobal, *prevGlobal; // next junction in the global list it is in (free or used) + struct trailJunc_s *nextJunc; // next junction in the trail + struct trailJunc_s *nextHead, *prevHead; // next head junc in the world + + qboolean inuse, freed; + int ownerIndex; + qhandle_t shader; + + int sType; + int flags; + float sTex; + vec3_t pos; + int spawnTime, endTime; + float alphaStart, alphaEnd; + vec3_t colorStart, colorEnd; + float widthStart, widthEnd; + + // current settings + float alpha; + float width; + vec3_t color; + +} trailJunc_t; + +#define MAX_TRAILJUNCS 4096 + +trailJunc_t trailJuncs[MAX_TRAILJUNCS]; +trailJunc_t *freeTrails, *activeTrails; +trailJunc_t *headTrails; + +qboolean initTrails = qfalse; + +int numTrailsInuse; + +/* +=============== +CG_ClearTrails +=============== +*/ +void CG_ClearTrails( void ) { + int i; + + memset( trailJuncs, 0, sizeof( trailJunc_t ) * MAX_TRAILJUNCS ); + + freeTrails = trailJuncs; + activeTrails = NULL; + headTrails = NULL; + + for ( i = 0 ; i < MAX_TRAILJUNCS ; i++ ) + { + trailJuncs[i].nextGlobal = &trailJuncs[i + 1]; + + if ( i > 0 ) { + trailJuncs[i].prevGlobal = &trailJuncs[i - 1]; + } else { + trailJuncs[i].prevGlobal = NULL; + } + + trailJuncs[i].inuse = qfalse; + } + trailJuncs[MAX_TRAILJUNCS - 1].nextGlobal = NULL; + + initTrails = qtrue; + numTrailsInuse = 0; +} + +/* +=============== +CG_SpawnTrailJunc +=============== +*/ +trailJunc_t *CG_SpawnTrailJunc( trailJunc_t *headJunc ) { + trailJunc_t *j; + + if ( !freeTrails ) { + return NULL; + } + + if ( cg_paused.integer ) { + return NULL; + } + + // select the first free trail, and remove it from the list + j = freeTrails; + freeTrails = j->nextGlobal; + if ( freeTrails ) { + freeTrails->prevGlobal = NULL; + } + + j->nextGlobal = activeTrails; + if ( activeTrails ) { + activeTrails->prevGlobal = j; + } + activeTrails = j; + j->prevGlobal = NULL; + j->inuse = qtrue; + j->freed = qfalse; + + // if this owner has a headJunc, add us to the start + if ( headJunc ) { + // remove the headJunc from the list of heads + if ( headJunc == headTrails ) { + headTrails = headJunc->nextHead; + if ( headTrails ) { + headTrails->prevHead = NULL; + } + } else { + if ( headJunc->nextHead ) { + headJunc->nextHead->prevHead = headJunc->prevHead; + } + if ( headJunc->prevHead ) { + headJunc->prevHead->nextHead = headJunc->nextHead; + } + } + headJunc->prevHead = NULL; + headJunc->nextHead = NULL; + } + // make us the headTrail + if ( headTrails ) { + headTrails->prevHead = j; + } + j->nextHead = headTrails; + j->prevHead = NULL; + headTrails = j; + + j->nextJunc = headJunc; // if headJunc is NULL, then we'll just be the end of the list + + numTrailsInuse++; + + // debugging +// CG_Printf( "NumTrails: %i\n", numTrailsInuse ); + + return j; +} + + +/* +=============== +CG_AddTrailJunc + + returns the index of the trail junction created + + Used for generic trails +=============== +*/ +int CG_AddTrailJunc( int headJuncIndex, qhandle_t shader, int spawnTime, int sType, vec3_t pos, int trailLife, float alphaStart, float alphaEnd, float startWidth, float endWidth, int flags, vec3_t colorStart, vec3_t colorEnd, float sRatio, float animSpeed ) { + trailJunc_t *j, *headJunc; + + if ( headJuncIndex > 0 ) { + headJunc = &trailJuncs[headJuncIndex - 1]; + + if ( !headJunc->inuse ) { + headJunc = NULL; + } + } else { + headJunc = NULL; + } + + j = CG_SpawnTrailJunc( headJunc ); + if ( !j ) { +// CG_Printf("couldnt spawn trail junc\n"); + return 0; + } + + if ( alphaStart > 1.0 ) { + alphaStart = 1.0; + } + if ( alphaStart < 0.0 ) { + alphaStart = 0.0; + } + if ( alphaEnd > 1.0 ) { + alphaEnd = 1.0; + } + if ( alphaEnd < 0.0 ) { + alphaEnd = 0.0; + } + + // setup the trail junction + j->shader = shader; + j->sType = sType; + VectorCopy( pos, j->pos ); + j->flags = flags; + + j->spawnTime = spawnTime; + j->endTime = spawnTime + trailLife; + + VectorCopy( colorStart, j->colorStart ); + VectorCopy( colorEnd, j->colorEnd ); + + j->alphaStart = alphaStart; + j->alphaEnd = alphaEnd; + + j->widthStart = startWidth; + j->widthEnd = endWidth; + + if ( sType == STYPE_REPEAT ) { + if ( headJunc ) { + j->sTex = headJunc->sTex + ( ( Distance( headJunc->pos, pos ) / sRatio ) / j->widthEnd ); + } else { + // FIXME: need a way to specify offset timing + j->sTex = ( animSpeed * ( 1.0 - ( (float)( cg.time % 1000 ) / 1000.0 ) ) ) / ( sRatio ); +// j->sTex = 0; + } + } + + return ( (int)( j - trailJuncs ) + 1 ); +} + +/* +=============== +CG_AddSparkJunc + + returns the index of the trail junction created +=============== +*/ +int CG_AddSparkJunc( int headJuncIndex, qhandle_t shader, vec3_t pos, int trailLife, float alphaStart, float alphaEnd, float startWidth, float endWidth ) { + trailJunc_t *j, *headJunc; + + if ( headJuncIndex > 0 ) { + headJunc = &trailJuncs[headJuncIndex - 1]; + + if ( !headJunc->inuse ) { + headJunc = NULL; + } + } else { + headJunc = NULL; + } + + j = CG_SpawnTrailJunc( headJunc ); + if ( !j ) { + return 0; + } + + // setup the trail junction + j->shader = shader; + j->sType = STYPE_STRETCH; + VectorCopy( pos, j->pos ); + j->flags = TJFL_NOCULL; // don't worry about fading up close + + j->spawnTime = cg.time; + j->endTime = cg.time + trailLife; + + VectorSet( j->colorStart, 1.0, 0.8 + 0.2 * alphaStart, 0.4 + 0.4 * alphaStart ); + VectorSet( j->colorEnd, 1.0, 0.8 + 0.2 * alphaEnd, 0.4 + 0.4 * alphaEnd ); +// VectorScale( j->colorStart, alphaStart, j->colorStart ); +// VectorScale( j->colorEnd, alphaEnd, j->colorEnd ); + + j->alphaStart = alphaStart * 2; + j->alphaEnd = alphaEnd * 2; +// j->alphaStart = 1.0; +// j->alphaEnd = 1.0; + + j->widthStart = startWidth; + j->widthEnd = endWidth; + + return ( (int)( j - trailJuncs ) + 1 ); +} + +/* +=============== +CG_AddSmokeJunc + + returns the index of the trail junction created +=============== +*/ +int CG_AddSmokeJunc( int headJuncIndex, qhandle_t shader, vec3_t pos, int trailLife, float alpha, float startWidth, float endWidth ) { +#define ST_RATIO 4.0 // sprite image: width / height + trailJunc_t *j, *headJunc; + + if ( headJuncIndex > 0 ) { + headJunc = &trailJuncs[headJuncIndex - 1]; + + if ( !headJunc->inuse ) { + headJunc = NULL; + } + } else { + headJunc = NULL; + } + + j = CG_SpawnTrailJunc( headJunc ); + if ( !j ) { + return 0; + } + + // setup the trail junction + j->shader = shader; + j->sType = STYPE_REPEAT; + VectorCopy( pos, j->pos ); + j->flags = TJFL_FADEIN; + + j->spawnTime = cg.time; + j->endTime = cg.time + trailLife; + + // VectorSet(j->colorStart, 0.2, 0.2, 0.2); + VectorSet( j->colorStart, 0.0, 0.0, 0.0 ); + // VectorSet(j->colorEnd, 0.1, 0.1, 0.1); + VectorSet( j->colorEnd, 0.0, 0.0, 0.0 ); + + j->alphaStart = alpha; + j->alphaEnd = 0.0; + + j->widthStart = startWidth; + j->widthEnd = endWidth; + + if ( headJunc ) { + j->sTex = headJunc->sTex + ( ( Distance( headJunc->pos, pos ) / ST_RATIO ) / j->widthEnd ); + } else { + // first junction, so this will become the "tail" very soon, make it fade out + j->sTex = 0; + j->alphaStart = 0.0; + j->alphaEnd = 0.0; + } + + return ( (int)( j - trailJuncs ) + 1 ); +} + +void CG_KillTrail( trailJunc_t *t ); + +/* +=========== +CG_FreeTrailJunc +=========== +*/ +void CG_FreeTrailJunc( trailJunc_t *junc ) { + // kill any juncs after us, so they aren't left hanging + if ( junc->nextJunc ) { + CG_KillTrail( junc ); + } + + // make it non-active + junc->inuse = qfalse; + junc->freed = qtrue; + if ( junc->nextGlobal ) { + junc->nextGlobal->prevGlobal = junc->prevGlobal; + } + if ( junc->prevGlobal ) { + junc->prevGlobal->nextGlobal = junc->nextGlobal; + } + if ( junc == activeTrails ) { + activeTrails = junc->nextGlobal; + } + + // if it's a head, remove it + if ( junc == headTrails ) { + headTrails = junc->nextHead; + } + if ( junc->nextHead ) { + junc->nextHead->prevHead = junc->prevHead; + } + if ( junc->prevHead ) { + junc->prevHead->nextHead = junc->nextHead; + } + junc->nextHead = NULL; + junc->prevHead = NULL; + + // stick it in the free list + junc->prevGlobal = NULL; + junc->nextGlobal = freeTrails; + if ( freeTrails ) { + freeTrails->prevGlobal = junc; + } + freeTrails = junc; + + numTrailsInuse--; +} + +/* +=========== +CG_KillTrail +=========== +*/ +void CG_KillTrail( trailJunc_t *t ) { + trailJunc_t *next; + + next = t->nextJunc; + + // kill the trail here + t->nextJunc = NULL; + + if ( next ) { + CG_FreeTrailJunc( next ); + } +} + +/* +============== +CG_AddTrailToScene + + TODO: this can do with some major optimization +============== +*/ +static vec3_t vforward, vright, vup; + +void CG_AddTrailToScene( trailJunc_t *trail, int iteration, int numJuncs ) { + #define MAX_TRAIL_VERTS 2048 + polyVert_t verts[MAX_TRAIL_VERTS]; + polyVert_t outVerts[MAX_TRAIL_VERTS * 3]; + int k, i, n, l, numOutVerts; + polyVert_t mid; + float mod[4]; + float sInc = 0.0f, s = 0.0f; // TTimo: init + trailJunc_t *j, *jNext; + vec3_t fwd, up, p, v; + // clipping vars + #define TRAIL_FADE_CLOSE_DIST 64.0 + #define TRAIL_FADE_FAR_SCALE 4.0 + vec3_t viewProj; + float viewDist, fadeAlpha; + + // add spark shader at head position + if ( trail->flags & TJFL_SPARKHEADFLARE ) { + j = trail; + VectorCopy( j->pos, p ); + VectorMA( p, -j->width * 2, vup, p ); + VectorMA( p, -j->width * 2, vright, p ); + VectorCopy( p, verts[0].xyz ); + verts[0].st[0] = 0; + verts[0].st[1] = 0; + verts[0].modulate[0] = 255; + verts[0].modulate[1] = 255; + verts[0].modulate[2] = 255; + verts[0].modulate[3] = ( unsigned char )( j->alpha * 255.0 ); + + VectorCopy( j->pos, p ); + VectorMA( p, -j->width * 2, vup, p ); + VectorMA( p, j->width * 2, vright, p ); + VectorCopy( p, verts[1].xyz ); + verts[1].st[0] = 0; + verts[1].st[1] = 1; + verts[1].modulate[0] = 255; + verts[1].modulate[1] = 255; + verts[1].modulate[2] = 255; + verts[1].modulate[3] = ( unsigned char )( j->alpha * 255.0 ); + + VectorCopy( j->pos, p ); + VectorMA( p, j->width * 2, vup, p ); + VectorMA( p, j->width * 2, vright, p ); + VectorCopy( p, verts[2].xyz ); + verts[2].st[0] = 1; + verts[2].st[1] = 1; + verts[2].modulate[0] = 255; + verts[2].modulate[1] = 255; + verts[2].modulate[2] = 255; + verts[2].modulate[3] = ( unsigned char )( j->alpha * 255.0 ); + + VectorCopy( j->pos, p ); + VectorMA( p, j->width * 2, vup, p ); + VectorMA( p, -j->width * 2, vright, p ); + VectorCopy( p, verts[3].xyz ); + verts[3].st[0] = 1; + verts[3].st[1] = 0; + verts[3].modulate[0] = 255; + verts[3].modulate[1] = 255; + verts[3].modulate[2] = 255; + verts[3].modulate[3] = ( unsigned char )( j->alpha * 255.0 ); + + trap_R_AddPolyToScene( cgs.media.sparkFlareShader, 4, verts ); + } + +// if (trail->flags & TJFL_CROSSOVER && iteration < 1) { +// iteration = 1; +// } + + if ( !numJuncs ) { + // first count the number of juncs in the trail + j = trail; + numJuncs = 0; + sInc = 0; + while ( j ) { + numJuncs++; + + // check for a dead next junc + if ( !j->inuse && j->nextJunc && !j->nextJunc->inuse ) { + CG_KillTrail( j ); + } else if ( j->nextJunc && j->nextJunc->freed ) { + // not sure how this can happen, but it does, and causes infinite loops + j->nextJunc = NULL; + } + + if ( j->nextJunc ) { + sInc += VectorDistance( j->nextJunc->pos, j->pos ); + } + + j = j->nextJunc; + } + } + + if ( numJuncs < 2 ) { + return; + } + + if ( trail->sType == STYPE_STRETCH ) { + //sInc = ((1.0 - 0.1) / (float)(numJuncs)); // hack, the end of funnel shows a bit of the start (looping) + s = 0.05; + //s = 0.05; + } else if ( trail->sType == STYPE_REPEAT ) { + s = trail->sTex; + } + + // now traverse the list + j = trail; + jNext = j->nextJunc; + i = 0; + while ( jNext ) { + + // first get the directional vectors to the next junc + VectorSubtract( jNext->pos, j->pos, fwd ); + GetPerpendicularViewVector( cg.refdef.vieworg, j->pos, jNext->pos, up ); + + // if it's a crossover, draw it twice + if ( j->flags & TJFL_CROSSOVER ) { + if ( iteration > 0 ) { + ProjectPointOntoVector( cg.refdef.vieworg, j->pos, jNext->pos, viewProj ); + VectorSubtract( cg.refdef.vieworg, viewProj, v ); + VectorNormalize( v ); + + if ( iteration == 1 ) { + VectorMA( up, 0.3, v, up ); + } else { + VectorMA( up, -0.3, v, up ); + } + VectorNormalize( up ); + } + } + // do fading when moving towards the projection point onto the trail segment vector + else if ( !( j->flags & TJFL_NOCULL ) && ( j->widthEnd > 4 || jNext->widthEnd > 4 ) ) { + ProjectPointOntoVector( cg.refdef.vieworg, j->pos, jNext->pos, viewProj ); + viewDist = Distance( viewProj, cg.refdef.vieworg ); + if ( viewDist < ( TRAIL_FADE_CLOSE_DIST * TRAIL_FADE_FAR_SCALE ) ) { + if ( viewDist < TRAIL_FADE_CLOSE_DIST ) { + fadeAlpha = 0.0; + } else { + fadeAlpha = ( viewDist - TRAIL_FADE_CLOSE_DIST ) / ( TRAIL_FADE_CLOSE_DIST * TRAIL_FADE_FAR_SCALE ); + } + if ( fadeAlpha < j->alpha ) { + j->alpha = fadeAlpha; + } + if ( fadeAlpha < jNext->alpha ) { + jNext->alpha = fadeAlpha; + } + } + } + + // now output the QUAD for this segment + + // 1 ---- + VectorMA( j->pos, 0.5 * j->width, up, p ); + VectorCopy( p, verts[i].xyz ); + verts[i].st[0] = s; + verts[i].st[1] = 1.0; + for ( k = 0; k < 3; k++ ) + verts[i].modulate[k] = ( unsigned char )( j->color[k] * 255.0 ); + verts[i].modulate[3] = ( unsigned char )( j->alpha * 255.0 ); + + // blend this with the previous junc + if ( j != trail ) { + VectorAdd( verts[i].xyz, verts[i - 1].xyz, verts[i].xyz ); + VectorScale( verts[i].xyz, 0.5, verts[i].xyz ); + VectorCopy( verts[i].xyz, verts[i - 1].xyz ); + } else if ( j->flags & TJFL_FADEIN ) { + verts[i].modulate[3] = 0; // fade in + } + + i++; + + // 2 ---- + VectorMA( p, -1 * j->width, up, p ); + VectorCopy( p, verts[i].xyz ); + verts[i].st[0] = s; + verts[i].st[1] = 0.0; + for ( k = 0; k < 3; k++ ) + verts[i].modulate[k] = ( unsigned char )( j->color[k] * 255.0 ); + verts[i].modulate[3] = ( unsigned char )( j->alpha * 255.0 ); + + // blend this with the previous junc + if ( j != trail ) { + VectorAdd( verts[i].xyz, verts[i - 3].xyz, verts[i].xyz ); + VectorScale( verts[i].xyz, 0.5, verts[i].xyz ); + VectorCopy( verts[i].xyz, verts[i - 3].xyz ); + } else if ( j->flags & TJFL_FADEIN ) { + verts[i].modulate[3] = 0; // fade in + } + + i++; + + if ( trail->sType == STYPE_REPEAT ) { + s = jNext->sTex; + } else { + //s += sInc; + s += VectorDistance( j->pos, jNext->pos ) / sInc; + if ( s > 1.0 ) { + s = 1.0; + } + } + + // 3 ---- + VectorMA( jNext->pos, -0.5 * jNext->width, up, p ); + VectorCopy( p, verts[i].xyz ); + verts[i].st[0] = s; + verts[i].st[1] = 0.0; + for ( k = 0; k < 3; k++ ) + verts[i].modulate[k] = ( unsigned char )( jNext->color[k] * 255.0 ); + verts[i].modulate[3] = ( unsigned char )( jNext->alpha * 255.0 ); + i++; + + // 4 ---- + VectorMA( p, jNext->width, up, p ); + VectorCopy( p, verts[i].xyz ); + verts[i].st[0] = s; + verts[i].st[1] = 1.0; + for ( k = 0; k < 3; k++ ) + verts[i].modulate[k] = ( unsigned char )( jNext->color[k] * 255.0 ); + verts[i].modulate[3] = ( unsigned char )( jNext->alpha * 255.0 ); + i++; + + if ( i + 4 > MAX_TRAIL_VERTS ) { + break; + } + + j = jNext; + jNext = j->nextJunc; + } + + if ( trail->flags & TJFL_FIXDISTORT ) { + // build the list of outVerts, by dividing up the QUAD's into 4 Tri's each, so as to allow + // any shaped (convex) Quad without bilinear distortion + for ( k = 0, numOutVerts = 0; k < i; k += 4 ) { + VectorCopy( verts[k].xyz, mid.xyz ); + mid.st[0] = verts[k].st[0]; + mid.st[1] = verts[k].st[1]; + for ( l = 0; l < 4; l++ ) { + mod[l] = (float)verts[k].modulate[l]; + } + for ( n = 1; n < 4; n++ ) { + VectorAdd( verts[k + n].xyz, mid.xyz, mid.xyz ); + mid.st[0] += verts[k + n].st[0]; + mid.st[1] += verts[k + n].st[1]; + for ( l = 0; l < 4; l++ ) { + mod[l] += (float)verts[k + n].modulate[l]; + } + } + VectorScale( mid.xyz, 0.25, mid.xyz ); + mid.st[0] *= 0.25; + mid.st[1] *= 0.25; + for ( l = 0; l < 4; l++ ) { + mid.modulate[l] = ( unsigned char )( mod[l] / 4.0 ); + } + + // now output the tri's + for ( n = 0; n < 4; n++ ) { + outVerts[numOutVerts++] = verts[k + n]; + outVerts[numOutVerts++] = mid; + if ( n < 3 ) { + outVerts[numOutVerts++] = verts[k + n + 1]; + } else { + outVerts[numOutVerts++] = verts[k]; + } + } + + } + + if ( !( trail->flags & TJFL_NOPOLYMERGE ) ) { + trap_R_AddPolysToScene( trail->shader, 3, &outVerts[0], numOutVerts / 3 ); + } else { + int k; + for ( k = 0; k < numOutVerts / 3; k++ ) { + trap_R_AddPolyToScene( trail->shader, 3, &outVerts[k * 3] ); + } + } + } else + { + // send the polygons + // FIXME: is it possible to send a GL_STRIP here? We are actually sending 2x the verts we really need to + if ( !( trail->flags & TJFL_NOPOLYMERGE ) ) { + trap_R_AddPolysToScene( trail->shader, 4, &verts[0], i / 4 ); + } else { + int k; + for ( k = 0; k < i / 4; k++ ) { + trap_R_AddPolyToScene( trail->shader, 4, &verts[k * 4] ); + } + } + } + + // do we need to make another pass? + if ( trail->flags & TJFL_CROSSOVER ) { + if ( iteration < 2 ) { + CG_AddTrailToScene( trail, iteration + 1, numJuncs ); + } + } + +} + +/* +=============== +CG_AddTrails +=============== +*/ +void CG_AddTrails( void ) { + float lifeFrac; + trailJunc_t *j, *jNext; + + if ( !initTrails ) { + CG_ClearTrails(); + } + + //AngleVectors( cg.snap->ps.viewangles, vforward, vright, vup ); + VectorCopy( cg.refdef.viewaxis[0], vforward ); + VectorCopy( cg.refdef.viewaxis[1], vright ); + VectorCopy( cg.refdef.viewaxis[2], vup ); + + // update the settings for each junc + j = activeTrails; + while ( j ) { + lifeFrac = (float)( cg.time - j->spawnTime ) / (float)( j->endTime - j->spawnTime ); + if ( lifeFrac >= 1.0 ) { + j->inuse = qfalse; // flag it as dead + j->width = j->widthEnd; + j->alpha = j->alphaEnd; + if ( j->alpha > 1.0 ) { + j->alpha = 1.0; + } else if ( j->alpha < 0.0 ) { + j->alpha = 0.0; + } + VectorCopy( j->colorEnd, j->color ); + } else { + j->width = j->widthStart + ( j->widthEnd - j->widthStart ) * lifeFrac; + j->alpha = j->alphaStart + ( j->alphaEnd - j->alphaStart ) * lifeFrac; + if ( j->alpha > 1.0 ) { + j->alpha = 1.0; + } else if ( j->alpha < 0.0 ) { + j->alpha = 0.0; + } + VectorSubtract( j->colorEnd, j->colorStart, j->color ); + VectorMA( j->colorStart, lifeFrac, j->color, j->color ); + } + + j = j->nextGlobal; + } + + // draw the trailHeads + j = headTrails; + while ( j ) { + jNext = j->nextHead; // in case it gets removed + if ( !j->inuse ) { + CG_FreeTrailJunc( j ); + } else { + CG_AddTrailToScene( j, 0, 0 ); + } + j = jNext; + } +} diff --git a/src/cgame/cg_view.c b/src/cgame/cg_view.c new file mode 100644 index 0000000..819c0bd --- /dev/null +++ b/src/cgame/cg_view.c @@ -0,0 +1,1655 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// cg_view.c -- setup all the parameters (position, angle, etc) +// for a 3D rendering +#include "cg_local.h" + +//======================== +extern int notebookModel; +//======================== + +/* +============================================================================= + + MODEL TESTING + +The viewthing and gun positioning tools from Q2 have been integrated and +enhanced into a single model testing facility. + +Model viewing can begin with either "testmodel " or "testgun ". + +The names must be the full pathname after the basedir, like +"models/weapons/v_launch/tris.md3" or "players/male/tris.md3" + +Testmodel will create a fake entity 100 units in front of the current view +position, directly facing the viewer. It will remain immobile, so you can +move around it to view it from different angles. + +Testgun will cause the model to follow the player around and supress the real +view weapon model. The default frame 0 of most guns is completely off screen, +so you will probably have to cycle a couple frames to see it. + +"nextframe", "prevframe", "nextskin", and "prevskin" commands will change the +frame or skin of the testmodel. These are bound to F5, F6, F7, and F8 in +q3default.cfg. + +If a gun is being tested, the "gun_x", "gun_y", and "gun_z" variables will let +you adjust the positioning. + +Note that none of the model testing features update while the game is paused, so +it may be convenient to test with deathmatch set to 1 so that bringing down the +console doesn't pause the game. + +============================================================================= +*/ + +/* +================= +CG_TestModel_f + +Creates an entity in front of the current position, which +can then be moved around +================= +*/ +void CG_TestModel_f( void ) { + vec3_t angles; + + memset( &cg.testModelEntity, 0, sizeof( cg.testModelEntity ) ); + if ( trap_Argc() < 2 ) { + return; + } + + Q_strncpyz( cg.testModelName, CG_Argv( 1 ), MAX_QPATH ); + cg.testModelEntity.hModel = trap_R_RegisterModel( cg.testModelName ); + + if ( trap_Argc() == 3 ) { + cg.testModelEntity.backlerp = atof( CG_Argv( 2 ) ); + cg.testModelEntity.frame = 1; + cg.testModelEntity.oldframe = 0; + } + if ( !cg.testModelEntity.hModel ) { + CG_Printf( "Can't register model\n" ); + return; + } + + VectorMA( cg.refdef.vieworg, 100, cg.refdef.viewaxis[0], cg.testModelEntity.origin ); + + angles[PITCH] = 0; + angles[YAW] = 180 + cg.refdefViewAngles[1]; + angles[ROLL] = 0; + + AnglesToAxis( angles, cg.testModelEntity.axis ); + cg.testGun = qfalse; +} + +/* +================= +CG_TestGun_f + +Replaces the current view weapon with the given model +================= +*/ +void CG_TestGun_f( void ) { + CG_TestModel_f(); + cg.testGun = qtrue; + cg.testModelEntity.renderfx = RF_MINLIGHT | RF_DEPTHHACK | RF_FIRST_PERSON; +} + + +void CG_TestModelNextFrame_f( void ) { + cg.testModelEntity.frame++; + CG_Printf( "frame %i\n", cg.testModelEntity.frame ); +} + +void CG_TestModelPrevFrame_f( void ) { + cg.testModelEntity.frame--; + if ( cg.testModelEntity.frame < 0 ) { + cg.testModelEntity.frame = 0; + } + CG_Printf( "frame %i\n", cg.testModelEntity.frame ); +} + +void CG_TestModelNextSkin_f( void ) { + cg.testModelEntity.skinNum++; + CG_Printf( "skin %i\n", cg.testModelEntity.skinNum ); +} + +void CG_TestModelPrevSkin_f( void ) { + cg.testModelEntity.skinNum--; + if ( cg.testModelEntity.skinNum < 0 ) { + cg.testModelEntity.skinNum = 0; + } + CG_Printf( "skin %i\n", cg.testModelEntity.skinNum ); +} + +static void CG_AddTestModel( void ) { + int i; + + // re-register the model, because the level may have changed + cg.testModelEntity.hModel = trap_R_RegisterModel( cg.testModelName ); + if ( !cg.testModelEntity.hModel ) { + CG_Printf( "Can't register model\n" ); + return; + } + + // if testing a gun, set the origin reletive to the view origin + if ( cg.testGun ) { + VectorCopy( cg.refdef.vieworg, cg.testModelEntity.origin ); + VectorCopy( cg.refdef.viewaxis[0], cg.testModelEntity.axis[0] ); + VectorCopy( cg.refdef.viewaxis[1], cg.testModelEntity.axis[1] ); + VectorCopy( cg.refdef.viewaxis[2], cg.testModelEntity.axis[2] ); + + // allow the position to be adjusted + for ( i = 0 ; i < 3 ; i++ ) { + cg.testModelEntity.origin[i] += cg.refdef.viewaxis[0][i] * cg_gun_x.value; + cg.testModelEntity.origin[i] += cg.refdef.viewaxis[1][i] * cg_gun_y.value; + cg.testModelEntity.origin[i] += cg.refdef.viewaxis[2][i] * cg_gun_z.value; + } + } + + trap_R_AddRefEntityToScene( &cg.testModelEntity ); +} + + + +//============================================================================ + + +/* +================= +CG_CalcVrect + +Sets the coordinates of the rendered window +================= +*/ +// TTimo: unused +//static float letterbox_frac = 1.0f; // used for transitioning to letterbox for cutscenes // TODO: add to cg. + +static void CG_CalcVrect( void ) { + int xsize, ysize; + float lbheight, lbdiff; + + // NERVE - SMF + if ( cg.limboMenu ) { + float x, y, w, h; + x = LIMBO_3D_X; + y = LIMBO_3D_Y; + w = LIMBO_3D_W; + h = LIMBO_3D_H; + + cg.refdef.width = 0; + CG_AdjustFrom640( &x, &y, &w, &h ); + + cg.refdef.x = x; + cg.refdef.y = y; + cg.refdef.width = w; + cg.refdef.height = h; + return; + } + // -NERVE - SMF + + // the intermission should allways be full screen + if ( cg.snap->ps.pm_type == PM_INTERMISSION ) { + xsize = ysize = 100; + } else { + // bound normal viewsize + if ( cg_viewsize.integer < 30 ) { + trap_Cvar_Set( "cg_viewsize","30" ); + xsize = ysize = 30; + } else if ( cg_viewsize.integer > 100 ) { + trap_Cvar_Set( "cg_viewsize","100" ); + xsize = ysize = 100; + } else { + xsize = ysize = cg_viewsize.integer; + } + } + +//----(SA) added transition to/from letterbox +// normal aspect is xx:xx +// letterbox is yy:yy (85% of 'normal' height) + + lbheight = ysize * 0.85; + lbdiff = ysize - lbheight; + + if ( cg_letterbox.integer ) { + ysize = lbheight; +// if(letterbox_frac != 0) { +// letterbox_frac -= 0.01f; // (SA) TODO: make non fps dependant +// if(letterbox_frac < 0) +// letterbox_frac = 0; +// ysize += (lbdiff * letterbox_frac); +// } +// } else { +// if(letterbox_frac != 1) { +// letterbox_frac += 0.01f; // (SA) TODO: make non fps dependant +// if(letterbox_frac > 1) +// letterbox_frac = 1; +// ysize = lbheight + (lbdiff * letterbox_frac); +// } + } +//----(SA) end + + + cg.refdef.width = cgs.glconfig.vidWidth * xsize / 100; + cg.refdef.width &= ~1; + + cg.refdef.height = cgs.glconfig.vidHeight * ysize / 100; + cg.refdef.height &= ~1; + + cg.refdef.x = ( cgs.glconfig.vidWidth - cg.refdef.width ) / 2; + cg.refdef.y = ( cgs.glconfig.vidHeight - cg.refdef.height ) / 2; +} + +//============================================================================== + + +/* +=============== +CG_OffsetThirdPersonView + +=============== +*/ +#define FOCUS_DISTANCE 512 +static void CG_OffsetThirdPersonView( void ) { + vec3_t forward, right, up; + vec3_t view; + vec3_t focusAngles; + trace_t trace; + static vec3_t mins = { -4, -4, -4 }; + static vec3_t maxs = { 4, 4, 4 }; + vec3_t focusPoint; + float focusDist; + float forwardScale, sideScale; + + cg.refdef.vieworg[2] += cg.predictedPlayerState.viewheight; + + VectorCopy( cg.refdefViewAngles, focusAngles ); + + // if dead, look at killer + if ( cg.predictedPlayerState.stats[STAT_HEALTH] <= 0 ) { + focusAngles[YAW] = cg.predictedPlayerState.stats[STAT_DEAD_YAW]; + cg.refdefViewAngles[YAW] = cg.predictedPlayerState.stats[STAT_DEAD_YAW]; + } + + if ( focusAngles[PITCH] > 45 ) { + focusAngles[PITCH] = 45; // don't go too far overhead + } + AngleVectors( focusAngles, forward, NULL, NULL ); + + VectorMA( cg.refdef.vieworg, FOCUS_DISTANCE, forward, focusPoint ); + + VectorCopy( cg.refdef.vieworg, view ); + + view[2] += 8; + + cg.refdefViewAngles[PITCH] *= 0.5; + + AngleVectors( cg.refdefViewAngles, forward, right, up ); + + forwardScale = cos( cg_thirdPersonAngle.value / 180 * M_PI ); + sideScale = sin( cg_thirdPersonAngle.value / 180 * M_PI ); + VectorMA( view, -cg_thirdPersonRange.value * forwardScale, forward, view ); + VectorMA( view, -cg_thirdPersonRange.value * sideScale, right, view ); + + // trace a ray from the origin to the viewpoint to make sure the view isn't + // in a solid block. Use an 8 by 8 block to prevent the view from near clipping anything + + CG_Trace( &trace, cg.refdef.vieworg, mins, maxs, view, cg.predictedPlayerState.clientNum, MASK_SOLID ); + + if ( trace.fraction != 1.0 ) { + VectorCopy( trace.endpos, view ); + view[2] += ( 1.0 - trace.fraction ) * 32; + // try another trace to this position, because a tunnel may have the ceiling + // close enogh that this is poking out + + CG_Trace( &trace, cg.refdef.vieworg, mins, maxs, view, cg.predictedPlayerState.clientNum, MASK_SOLID ); + VectorCopy( trace.endpos, view ); + } + + + VectorCopy( view, cg.refdef.vieworg ); + + // select pitch to look at focus point from vieword + VectorSubtract( focusPoint, cg.refdef.vieworg, focusPoint ); + focusDist = sqrt( focusPoint[0] * focusPoint[0] + focusPoint[1] * focusPoint[1] ); + if ( focusDist < 1 ) { + focusDist = 1; // should never happen + } + cg.refdefViewAngles[PITCH] = -180 / M_PI * atan2( focusPoint[2], focusDist ); + cg.refdefViewAngles[YAW] -= cg_thirdPersonAngle.value; +} + + +// this causes a compiler bug on mac MrC compiler +static void CG_StepOffset( void ) { + int timeDelta; + + // smooth out stair climbing + timeDelta = cg.time - cg.stepTime; + // Ridah + if ( timeDelta < 0 ) { + cg.stepTime = cg.time; + } + if ( timeDelta < STEP_TIME ) { + cg.refdef.vieworg[2] -= cg.stepChange + * ( STEP_TIME - timeDelta ) / STEP_TIME; + } +} + +/* +================ +CG_KickAngles +================ +*/ +void CG_KickAngles( void ) { + const vec3_t centerSpeed = {2400, 2400, 2400}; + const float recoilCenterSpeed = 200; + const float recoilIgnoreCutoff = 15; + const float recoilMaxSpeed = 50; + const vec3_t maxKickAngles = {10,10,10}; + float idealCenterSpeed, kickChange; + int i, frametime, t; + float ft; + #define STEP 20 + + // this code is frametime-dependant, so split it up into small chunks + //cg.kickAngles[PITCH] = 0; + cg.recoilPitchAngle = 0; + for ( t = cg.frametime; t > 0; t -= STEP ) { + if ( t > STEP ) { + frametime = STEP; + } else { + frametime = t; + } + + ft = ( (float)frametime / 1000 ); + + // kickAngles is spring-centered + for ( i = 0; i < 3; i++ ) { + if ( cg.kickAVel[i] || cg.kickAngles[i] ) { + // apply centering forces to kickAvel + if ( cg.kickAngles[i] && frametime ) { + idealCenterSpeed = -( 2.0 * ( cg.kickAngles[i] > 0 ) - 1.0 ) * centerSpeed[i]; + if ( idealCenterSpeed ) { + cg.kickAVel[i] += idealCenterSpeed * ft; + } + } + // add the kickAVel to the kickAngles + kickChange = cg.kickAVel[i] * ft; + if ( cg.kickAngles[i] && ( cg.kickAngles[i] < 0 ) != ( kickChange < 0 ) ) { // slower when returning to center + kickChange *= 0.06; + } + // check for crossing back over the center point + if ( !cg.kickAngles[i] || ( ( cg.kickAngles[i] + kickChange ) < 0 ) == ( cg.kickAngles[i] < 0 ) ) { + cg.kickAngles[i] += kickChange; + if ( !cg.kickAngles[i] && frametime ) { + cg.kickAVel[i] = 0; + } else if ( fabs( cg.kickAngles[i] ) > maxKickAngles[i] ) { + cg.kickAngles[i] = maxKickAngles[i] * ( ( 2 * ( cg.kickAngles[i] > 0 ) ) - 1 ); + cg.kickAVel[i] = 0; // force Avel to return us to center rather than keep going outside range + } + } else { // about to cross, so just zero it out + cg.kickAngles[i] = 0; + cg.kickAVel[i] = 0; + } + } + } + + // recoil is added to input viewangles per frame + if ( cg.recoilPitch ) { + // apply max recoil + if ( fabs( cg.recoilPitch ) > recoilMaxSpeed ) { + if ( cg.recoilPitch > 0 ) { + cg.recoilPitch = recoilMaxSpeed; + } else { + cg.recoilPitch = -recoilMaxSpeed; + } + } + // apply centering forces to kickAvel + if ( frametime ) { + idealCenterSpeed = -( 2.0 * ( cg.recoilPitch > 0 ) - 1.0 ) * recoilCenterSpeed * ft; + if ( idealCenterSpeed ) { + if ( fabs( idealCenterSpeed ) < fabs( cg.recoilPitch ) ) { + cg.recoilPitch += idealCenterSpeed; + } else { // back zero out + cg.recoilPitch = 0; + } + } + } + } + if ( fabs( cg.recoilPitch ) > recoilIgnoreCutoff ) { + cg.recoilPitchAngle += cg.recoilPitch * ft; + } + } + // encode the kick angles into a 24bit number, for sending to the client exe +//----(SA) commented out since it doesn't appear to be used, and it spams the console when in "developer 1" +// trap_Cvar_Set( "cg_recoilPitch", va("%f", cg.recoilPitchAngle) ); +} + + +/* +CG_Concussive +*/ +void CG_Concussive( centity_t *cent ) { + float length; +// vec3_t dir, forward; + vec3_t vec; +// float dot; + + // + float pitchRecoilAdd, pitchAdd; + float yawRandom; + vec3_t recoil; + // + + if ( !cg.renderingThirdPerson && cent->currentState.density == cg.snap->ps.clientNum ) { + // + pitchRecoilAdd = 0; + pitchAdd = 0; + yawRandom = 0; + // + + VectorSubtract( cg.snap->ps.origin, cent->currentState.origin, vec ); + length = VectorLength( vec ); + + // pitchAdd = 12+rand()%3; + // yawRandom = 6; + + if ( length > 1024 ) { + return; + } + + pitchAdd = ( 32 / length ) * 64; + yawRandom = ( 32 / length ) * 64; + + // recoil[YAW] = crandom()*yawRandom; + if ( rand() % 100 > 50 ) { + recoil[YAW] = -yawRandom; + } else { + recoil[YAW] = yawRandom; + } + + recoil[ROLL] = -recoil[YAW]; // why not + recoil[PITCH] = -pitchAdd; + // scale it up a bit (easier to modify this while tweaking) + VectorScale( recoil, 30, recoil ); + // set the recoil + VectorCopy( recoil, cg.kickAVel ); + // set the recoil + cg.recoilPitch -= pitchRecoilAdd; + + } +} + + +/* +============== +CG_ZoomSway + sway for scoped weapons. + this takes aimspread into account so the view settles after a bit +============== +*/ +static void CG_ZoomSway( void ) { + float spreadfrac; + float phase; + + if ( !cg.zoomval ) { // not zoomed + return; + } + + if ( cg.snap->ps.eFlags & EF_MG42_ACTIVE ) { // don't draw when on mg_42 + return; + } + + spreadfrac = (float)cg.snap->ps.aimSpreadScale / 255.0; + + phase = cg.time / 1000.0 * ZOOM_PITCH_FREQUENCY * M_PI * 2; + cg.refdefViewAngles[PITCH] += ZOOM_PITCH_AMPLITUDE * sin( phase ) * ( spreadfrac + ZOOM_PITCH_MIN_AMPLITUDE ); + + phase = cg.time / 1000.0 * ZOOM_YAW_FREQUENCY * M_PI * 2; + cg.refdefViewAngles[YAW] += ZOOM_YAW_AMPLITUDE * sin( phase ) * ( spreadfrac + ZOOM_YAW_MIN_AMPLITUDE ); + +} + + + +/* +=============== +CG_OffsetFirstPersonView + +=============== +*/ +static void CG_OffsetFirstPersonView( void ) { + float *origin; + float *angles; + float bob; + float ratio; + float delta; + float speed; + float f; + vec3_t predictedVelocity; + int timeDelta; + + if ( cg.snap->ps.pm_type == PM_INTERMISSION ) { + return; + } + + origin = cg.refdef.vieworg; + angles = cg.refdefViewAngles; + + // if dead, fix the angle and don't add any kick + if ( cg.snap->ps.stats[STAT_HEALTH] <= 0 ) { + angles[ROLL] = 40; + angles[PITCH] = -15; + angles[YAW] = cg.snap->ps.stats[STAT_DEAD_YAW]; + origin[2] += cg.predictedPlayerState.viewheight; + return; + } + + // add angles based on weapon kick + VectorAdd( angles, cg.kick_angles, angles ); + + // RF, add new weapon kick angles + CG_KickAngles(); + VectorAdd( angles, cg.kickAngles, angles ); + // RF, pitch is already added + //angles[0] -= cg.kickAngles[PITCH]; + + // add angles based on damage kick + if ( cg.damageTime ) { + ratio = cg.time - cg.damageTime; + if ( ratio < DAMAGE_DEFLECT_TIME ) { + ratio /= DAMAGE_DEFLECT_TIME; + angles[PITCH] += ratio * cg.v_dmg_pitch; + angles[ROLL] += ratio * cg.v_dmg_roll; + } else { + ratio = 1.0 - ( ratio - DAMAGE_DEFLECT_TIME ) / DAMAGE_RETURN_TIME; + if ( ratio > 0 ) { + angles[PITCH] += ratio * cg.v_dmg_pitch; + angles[ROLL] += ratio * cg.v_dmg_roll; + } + } + } + + // add pitch based on fall kick +#if 0 + ratio = ( cg.time - cg.landTime ) / FALL_TIME; + if ( ratio < 0 ) { + ratio = 0; + } + angles[PITCH] += ratio * cg.fall_value; +#endif + + // add angles based on velocity + VectorCopy( cg.predictedPlayerState.velocity, predictedVelocity ); + + delta = DotProduct( predictedVelocity, cg.refdef.viewaxis[0] ); + angles[PITCH] += delta * cg_runpitch.value; + + delta = DotProduct( predictedVelocity, cg.refdef.viewaxis[1] ); + angles[ROLL] -= delta * cg_runroll.value; + + // add angles based on bob + + // make sure the bob is visible even at low speeds + speed = cg.xyspeed > 200 ? cg.xyspeed : 200; + + delta = cg.bobfracsin * cg_bobpitch.value * speed; + if ( cg.predictedPlayerState.pm_flags & PMF_DUCKED ) { + delta *= 3; // crouching + } + angles[PITCH] += delta; + delta = cg.bobfracsin * cg_bobroll.value * speed; + if ( cg.predictedPlayerState.pm_flags & PMF_DUCKED ) { + delta *= 3; // crouching accentuates roll + } + if ( cg.bobcycle & 1 ) { + delta = -delta; + } + angles[ROLL] += delta; + +//=================================== + + // add view height + origin[2] += cg.predictedPlayerState.viewheight; + + // smooth out duck height changes + timeDelta = cg.time - cg.duckTime; + if ( timeDelta < 0 ) { // Ridah + cg.duckTime = cg.time - DUCK_TIME; + } + if ( timeDelta < DUCK_TIME ) { + cg.refdef.vieworg[2] -= cg.duckChange + * ( DUCK_TIME - timeDelta ) / DUCK_TIME; + } + + // add bob height + bob = cg.bobfracsin * cg.xyspeed * cg_bobup.value; + if ( bob > 6 ) { + bob = 6; + } + + origin[2] += bob; + + + // add fall height + delta = cg.time - cg.landTime; + if ( delta < 0 ) { // Ridah + cg.landTime = cg.time - ( LAND_DEFLECT_TIME + LAND_RETURN_TIME ); + } + if ( delta < LAND_DEFLECT_TIME ) { + f = delta / LAND_DEFLECT_TIME; + cg.refdef.vieworg[2] += cg.landChange * f; + } else if ( delta < LAND_DEFLECT_TIME + LAND_RETURN_TIME ) { + delta -= LAND_DEFLECT_TIME; + f = 1.0 - ( delta / LAND_RETURN_TIME ); + cg.refdef.vieworg[2] += cg.landChange * f; + } + + // add step offset + CG_StepOffset(); + + CG_ZoomSway(); + + // adjust for 'lean' + if ( cg.predictedPlayerState.leanf != 0 ) { + //add leaning offset + vec3_t right; + cg.refdefViewAngles[2] += cg.predictedPlayerState.leanf / 2.0f; + AngleVectors( cg.refdefViewAngles, NULL, right, NULL ); + VectorMA( cg.refdef.vieworg, cg.predictedPlayerState.leanf, right, cg.refdef.vieworg ); + } + + // add kick offset + + VectorAdd( origin, cg.kick_origin, origin ); + + // pivot the eye based on a neck length +#if 0 + { +#define NECK_LENGTH 8 + vec3_t forward, up; + + cg.refdef.vieworg[2] -= NECK_LENGTH; + AngleVectors( cg.refdefViewAngles, forward, NULL, up ); + VectorMA( cg.refdef.vieworg, 3, forward, cg.refdef.vieworg ); + VectorMA( cg.refdef.vieworg, NECK_LENGTH, up, cg.refdef.vieworg ); + } +#endif +} + +//====================================================================== + +// +// Zoom controls +// + + +// probably move to server variables +float zoomTable[ZOOM_MAX_ZOOMS][2] = { +// max {out,in} + {0, 0}, + + {36, 8}, // binoc + {20, 4}, // sniper + {60, 20}, // snooper + {55, 55}, // fg42 + {55, 55} // mg42 +}; + +void CG_AdjustZoomVal( float val, int type ) { + cg.zoomval += val; + if ( cg.zoomval > zoomTable[type][ZOOM_OUT] ) { + cg.zoomval = zoomTable[type][ZOOM_OUT]; + } + if ( cg.zoomval < zoomTable[type][ZOOM_IN] ) { + cg.zoomval = zoomTable[type][ZOOM_IN]; + } +} + +void CG_ZoomIn_f( void ) { + if ( cg_entities[cg.snap->ps.clientNum].currentState.weapon == WP_SNIPERRIFLE ) { + CG_AdjustZoomVal( -( cg_zoomStepSniper.value ), ZOOM_SNIPER ); + } else if ( cg_entities[cg.snap->ps.clientNum].currentState.weapon == WP_SNOOPERSCOPE ) { + CG_AdjustZoomVal( -( cg_zoomStepSnooper.value ), ZOOM_SNOOPER ); + } else if ( cg_entities[cg.snap->ps.clientNum].currentState.weapon == WP_FG42SCOPE ) { + CG_AdjustZoomVal( -( cg_zoomStepSnooper.value ), ZOOM_FG42SCOPE ); + } else if ( cg.zoomedBinoc ) { + CG_AdjustZoomVal( -( cg_zoomStepBinoc.value ), ZOOM_BINOC ); + } +} + +void CG_ZoomOut_f( void ) { + if ( cg_entities[cg.snap->ps.clientNum].currentState.weapon == WP_SNIPERRIFLE ) { + CG_AdjustZoomVal( cg_zoomStepSniper.value, ZOOM_SNIPER ); + } else if ( cg_entities[cg.snap->ps.clientNum].currentState.weapon == WP_SNOOPERSCOPE ) { + CG_AdjustZoomVal( cg_zoomStepSnooper.value, ZOOM_SNOOPER ); + } else if ( cg_entities[cg.snap->ps.clientNum].currentState.weapon == WP_FG42SCOPE ) { + CG_AdjustZoomVal( cg_zoomStepSnooper.value, ZOOM_FG42SCOPE ); + } else if ( cg.zoomedBinoc ) { + CG_AdjustZoomVal( cg_zoomStepBinoc.value, ZOOM_BINOC ); + } +} + + +/* +============== +CG_Zoom +============== +*/ +void CG_Zoom( void ) { + if ( cg.predictedPlayerState.eFlags & EF_ZOOMING ) { + if ( cg.zoomedBinoc ) { + return; + } + cg.zoomedBinoc = qtrue; + cg.zoomTime = cg.time; + cg.zoomval = cg_zoomDefaultBinoc.value; + } else { + if ( !cg.zoomedBinoc ) { + return; + } + cg.zoomedBinoc = qfalse; + cg.zoomTime = cg.time; + + // check for scope wepon in use, and switch to if necessary + if ( cg.predictedPlayerState.weapon == WP_SNOOPERSCOPE ) { + cg.zoomval = cg_zoomDefaultSnooper.value; + } else if ( cg.predictedPlayerState.weapon == WP_SNIPERRIFLE ) { + cg.zoomval = cg_zoomDefaultSniper.value; + } else if ( cg.predictedPlayerState.weapon == WP_FG42SCOPE ) { + cg.zoomval = cg_zoomDefaultFG.value; + } else { + cg.zoomval = 0; + } + } +} + + +/* +==================== +CG_CalcFov + +Fixed fov at intermissions, otherwise account for fov variable and zooms. +==================== +*/ +#define WAVE_AMPLITUDE 1 +#define WAVE_FREQUENCY 0.4 + +static int CG_CalcFov( void ) { + static float lastfov = 90; // for transitions back from zoomed in modes + float x; + float phase; + float v; + int contents; + float fov_x, fov_y; + float zoomFov; + float f; + int inwater; + qboolean dead; + + CG_Zoom(); + + if ( cg.predictedPlayerState.stats[STAT_HEALTH] <= 0 ) { + dead = qtrue; + cg.zoomedBinoc = qfalse; + cg.zoomTime = 0; + cg.zoomval = 0; + } else { + dead = qfalse; + } + + if ( cg.predictedPlayerState.pm_type == PM_INTERMISSION ) { + // if in intermission, use a fixed value + fov_x = 90; + } else { + // user selectable + if ( cgs.dmflags & DF_FIXED_FOV ) { + // dmflag to prevent wide fov for all clients + fov_x = 90; + } else { + fov_x = cg_fov.value; + if ( fov_x < 1 ) { + fov_x = 1; + } else if ( fov_x > 160 ) { + fov_x = 160; + } + } + + // account for zooms + if ( cg.zoomval ) { + zoomFov = cg.zoomval; // (SA) use user scrolled amount + + if ( zoomFov < 1 ) { + zoomFov = 1; + } else if ( zoomFov > 160 ) { + zoomFov = 160; + } + } else { + zoomFov = lastfov; + } + + // do smooth transitions for the binocs + if ( cg.zoomedBinoc ) { // binoc zooming in + f = ( cg.time - cg.zoomTime ) / (float)ZOOM_TIME; + if ( f > 1.0 ) { + fov_x = zoomFov; + } else { + fov_x = fov_x + f * ( zoomFov - fov_x ); + } + lastfov = fov_x; + } else if ( cg.zoomval ) { // zoomed by sniper/snooper + fov_x = cg.zoomval; + lastfov = fov_x; + } else { // binoc zooming out + f = ( cg.time - cg.zoomTime ) / (float)ZOOM_TIME; + if ( f > 1.0 ) { + fov_x = fov_x; + } else { + fov_x = zoomFov + f * ( fov_x - zoomFov ); + } + } + } + + // DHM - Nerve :: zoom in for Limbo or Spectator + if ( cgs.gametype == GT_WOLF ) { + if ( cg.snap->ps.pm_flags & PMF_FOLLOW && cg.snap->ps.weapon == WP_SNIPERRIFLE ) { + fov_x = cg_zoomDefaultSniper.value; + } + } + // dhm - end + + if ( !dead && ( cg.weaponSelect == WP_SNOOPERSCOPE ) ) { + cg.refdef.rdflags |= RDF_SNOOPERVIEW; + } else { + cg.refdef.rdflags &= ~RDF_SNOOPERVIEW; + } + + if ( cg.snap->ps.persistant[PERS_HWEAPON_USE] ) { + fov_x = 55; + } + + x = cg.refdef.width / tan( fov_x / 360 * M_PI ); + fov_y = atan2( cg.refdef.height, x ); + fov_y = fov_y * 360 / M_PI; + + // warp if underwater + contents = CG_PointContents( cg.refdef.vieworg, -1 ); + if ( contents & ( CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ) ) { + phase = cg.time / 1000.0 * WAVE_FREQUENCY * M_PI * 2; + v = WAVE_AMPLITUDE * sin( phase ); + fov_x += v; + fov_y -= v; + inwater = qtrue; + cg.refdef.rdflags |= RDF_UNDERWATER; + } else { + cg.refdef.rdflags &= ~RDF_UNDERWATER; + inwater = qfalse; + } + + contents = CG_PointContents( cg.refdef.vieworg, -1 ); + if ( contents & ( CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ) ) { + cg.refdef.rdflags |= RDF_UNDERWATER; + } else { + cg.refdef.rdflags &= ~RDF_UNDERWATER; + } + + // set it + cg.refdef.fov_x = fov_x; + cg.refdef.fov_y = fov_y; + + if ( !cg.zoomedBinoc ) { + // NERVE - SMF - fix for zoomed in/out movement bug + if ( cg.zoomval ) { + if ( cg.snap->ps.weapon == WP_SNOOPERSCOPE ) { + cg.zoomSensitivity = 0.3f * ( cg.zoomval / 90.f ); // NERVE - SMF - changed to get less sensitive as you zoom in; + } +// cg.zoomSensitivity = 0.2; + else { + cg.zoomSensitivity = 0.6 * ( cg.zoomval / 90.f ); // NERVE - SMF - changed to get less sensitive as you zoom in + } +// cg.zoomSensitivity = 0.1; + } else { + cg.zoomSensitivity = 1; + } + // -NERVE - SMF + } else { + cg.zoomSensitivity = cg.refdef.fov_y / 75.0; + } + + return inwater; +} + + +/* +============== +CG_UnderwaterSounds +============== +*/ +#define UNDERWATER_BIT 8 +static void CG_UnderwaterSounds( void ) { +// trap_S_AddLoopingSound( cent->currentState.number, cent->lerpOrigin, vec3_origin, cgs.media.underWaterSound, 255 ); + trap_S_AddLoopingSound( cg.snap->ps.clientNum, cg.snap->ps.origin, vec3_origin, cgs.media.underWaterSound, 255 & ( 1 << 8 ) ); +} + + +/* +=============== +CG_DamageBlendBlob + +=============== +*/ +static void CG_DamageBlendBlob( void ) { + int t,i; + int maxTime; + refEntity_t ent; + qboolean pointDamage; + viewDamage_t *vd; + float redFlash; + + // ragePro systems can't fade blends, so don't obscure the screen + if ( cgs.glconfig.hardwareType == GLHW_RAGEPRO ) { + return; + } + + redFlash = 0; + + for ( i = 0; i < MAX_VIEWDAMAGE; i++ ) { + + vd = &cg.viewDamage[i]; + + if ( !vd->damageValue ) { + continue; + } + + maxTime = vd->damageDuration; + t = cg.time - vd->damageTime; + if ( t <= 0 || t >= maxTime ) { + vd->damageValue = 0; + continue; + } + + pointDamage = !( !vd->damageX && !vd->damageY ); + + // if not point Damage, only do flash blend + if ( !pointDamage ) { + redFlash += 10.0 * ( 1.0 - (float)t / maxTime ); + continue; + } + + memset( &ent, 0, sizeof( ent ) ); + ent.reType = RT_SPRITE; + ent.renderfx = RF_FIRST_PERSON; + + VectorMA( cg.refdef.vieworg, 8, cg.refdef.viewaxis[0], ent.origin ); + VectorMA( ent.origin, vd->damageX * -8, cg.refdef.viewaxis[1], ent.origin ); + VectorMA( ent.origin, vd->damageY * 8, cg.refdef.viewaxis[2], ent.origin ); + + ent.radius = vd->damageValue * 0.4 * ( 0.5 + 0.5 * (float)t / maxTime ) * ( 0.75 + 0.5 * fabs( sin( vd->damageTime ) ) ); + + ent.customShader = cgs.media.viewBloodAni[(int)( floor( ( (float)t / maxTime ) * 4.9 ) )]; //cgs.media.viewBloodShader; + ent.shaderRGBA[0] = 255; + ent.shaderRGBA[1] = 255; + ent.shaderRGBA[2] = 255; + ent.shaderRGBA[3] = 255; + trap_R_AddRefEntityToScene( &ent ); + + redFlash += ent.radius; + } + + /* moved over to cg_draw.c + if (cg.v_dmg_time > cg.time) { + redFlash = fabs(cg.v_dmg_pitch * ((cg.v_dmg_time - cg.time) / DAMAGE_TIME)); + + // blend the entire screen red + if (redFlash > 5) + redFlash = 5; + + memset( &ent, 0, sizeof( ent ) ); + ent.reType = RT_SPRITE; + ent.renderfx = RF_FIRST_PERSON; + + VectorMA( cg.refdef.vieworg, 8, cg.refdef.viewaxis[0], ent.origin ); + ent.radius = 80; // occupy entire screen + ent.customShader = cgs.media.viewFlashBlood; + ent.shaderRGBA[3] = (int)(180.0 * redFlash/5.0); + + trap_R_AddRefEntityToScene( &ent ); + } + */ +} + +/* +=============== +CG_CalcViewValues + +Sets cg.refdef view values +=============== +*/ +static int CG_CalcViewValues( void ) { + playerState_t *ps; + static vec3_t oldOrigin = {0,0,0}; + + memset( &cg.refdef, 0, sizeof( cg.refdef ) ); + + // strings for in game rendering + // Q_strncpyz( cg.refdef.text[0], "Park Ranger", sizeof(cg.refdef.text[0]) ); + // Q_strncpyz( cg.refdef.text[1], "19", sizeof(cg.refdef.text[1]) ); + + // calculate size of 3D view + CG_CalcVrect(); + + ps = &cg.predictedPlayerState; + + if ( cg.cameraMode ) { + vec3_t origin, angles; + float fov = 90; + float x; + + if ( trap_getCameraInfo( CAM_PRIMARY, cg.time, &origin, &angles, &fov ) ) { + VectorCopy( origin, cg.refdef.vieworg ); + angles[ROLL] = 0; + angles[PITCH] = -angles[PITCH]; // (SA) compensate for reversed pitch (this makes the game match the editor, however I'm guessing the real fix is to be done there) + VectorCopy( angles, cg.refdefViewAngles ); + AnglesToAxis( cg.refdefViewAngles, cg.refdef.viewaxis ); + + x = cg.refdef.width / tan( fov / 360 * M_PI ); + cg.refdef.fov_y = atan2( cg.refdef.height, x ); + cg.refdef.fov_y = cg.refdef.fov_y * 360 / M_PI; + cg.refdef.fov_x = fov; + + // RF, had to disable, sometimes a loadgame to a camera in the same position + // can cause the game to not know where the camera is, therefore snapshots + // dont show the correct entities + //if(VectorCompare(origin, oldOrigin)) + // return 0; + + VectorCopy( origin, oldOrigin ); + trap_SendClientCommand( va( "setCameraOrigin %f %f %f", origin[0], origin[1], origin[2] ) ); + return 0; + + } else { + cg.cameraMode = qfalse; // camera off in cgame + trap_Cvar_Set( "cg_letterbox", "0" ); + trap_SendClientCommand( "stopCamera" ); // camera off in game + trap_stopCamera( CAM_PRIMARY ); // camera off in client + + CG_Fade( 0, 0, 0, 255, 0, 0 ); // go black + CG_Fade( 0, 0, 0, 0, cg.time + 200, 1500 ); // then fadeup + } + } + + // intermission view + if ( ps->pm_type == PM_INTERMISSION ) { + VectorCopy( ps->origin, cg.refdef.vieworg ); + VectorCopy( ps->viewangles, cg.refdefViewAngles ); + AnglesToAxis( cg.refdefViewAngles, cg.refdef.viewaxis ); + return CG_CalcFov(); + } + + cg.bobcycle = ( ps->bobCycle & 128 ) >> 7; + cg.bobfracsin = fabs( sin( ( ps->bobCycle & 127 ) / 127.0 * M_PI ) ); + cg.xyspeed = sqrt( ps->velocity[0] * ps->velocity[0] + + ps->velocity[1] * ps->velocity[1] ); + + + VectorCopy( ps->origin, cg.refdef.vieworg ); + VectorCopy( ps->viewangles, cg.refdefViewAngles ); + + // add error decay + if ( cg_errorDecay.value > 0 ) { + int t; + float f; + + t = cg.time - cg.predictedErrorTime; + f = ( cg_errorDecay.value - t ) / cg_errorDecay.value; + if ( f > 0 && f < 1 ) { + VectorMA( cg.refdef.vieworg, f, cg.predictedError, cg.refdef.vieworg ); + } else { + cg.predictedErrorTime = 0; + } + } + + // Ridah, lock the viewangles if the game has told us to + if ( ps->viewlocked ) { + + /* + if (ps->viewlocked == 4) + { + centity_t *tent; + tent = &cg_entities[ps->viewlocked_entNum]; + VectorCopy (tent->currentState.apos.trBase, cg.refdefViewAngles); + } + else + */ + BG_EvaluateTrajectory( &cg_entities[ps->viewlocked_entNum].currentState.apos, cg.time, cg.refdefViewAngles ); + + if ( ps->viewlocked == 2 ) { + cg.refdefViewAngles[0] += crandom(); + cg.refdefViewAngles[1] += crandom(); + } + } + // done. + + if ( cg.renderingThirdPerson ) { + // back away from character + CG_OffsetThirdPersonView(); + } else { + // offset for local bobbing and kicks + CG_OffsetFirstPersonView(); + + // Ridah, lock the viewangles if the game has told us to + if ( ps->viewlocked == 4 ) { + vec3_t fwd; + AngleVectors( cg.refdefViewAngles, fwd, NULL, NULL ); + VectorMA( cg_entities[ps->viewlocked_entNum].currentState.pos.trBase, 16, fwd, cg.refdef.vieworg ); + } else if ( ps->viewlocked ) { + vec3_t fwd; + float oldZ; + // set our position to be behind it + oldZ = cg.refdef.vieworg[2]; + AngleVectors( cg.refdefViewAngles, fwd, NULL, NULL ); + VectorMA( cg_entities[ps->viewlocked_entNum].currentState.pos.trBase, -34, fwd, cg.refdef.vieworg ); + cg.refdef.vieworg[2] = oldZ; + } + // done. + } + + // position eye reletive to origin + AnglesToAxis( cg.refdefViewAngles, cg.refdef.viewaxis ); + + if ( cg.hyperspace ) { + cg.refdef.rdflags |= RDF_NOWORLDMODEL | RDF_HYPERSPACE; + } + + // field of view + return CG_CalcFov(); +} + + +/* +===================== +CG_PowerupTimerSounds +===================== +*/ +static void CG_PowerupTimerSounds( void ) { + int i; + int t; + + // powerup timers going away + for ( i = 0 ; i < MAX_POWERUPS ; i++ ) { + t = cg.snap->ps.powerups[i]; + if ( t <= cg.time ) { + continue; + } + if ( t - cg.time >= POWERUP_BLINKS * POWERUP_BLINK_TIME ) { + continue; + } + if ( ( t - cg.time ) / POWERUP_BLINK_TIME != ( t - cg.oldTime ) / POWERUP_BLINK_TIME ) { + trap_S_StartSound( NULL, cg.snap->ps.clientNum, CHAN_ITEM, cgs.media.wearOffSound ); + } + } +} + +//========================================================================= + +/* +============== +CG_DrawSkyBoxPortal +============== +*/ +void CG_DrawSkyBoxPortal( void ) { + static float lastfov = 90; // for transitions back from zoomed in modes + refdef_t backuprefdef; + float fov_x; + float fov_y; + float x; + char *cstr; + char *token; + float zoomFov; + float f; + static qboolean foginited = qfalse; // only set the portal fog values once + + if ( !( cstr = (char *)CG_ConfigString( CS_SKYBOXORG ) ) || !strlen( cstr ) ) { + // no skybox in this map + return; + } + + // if they are waiting at the mission stats screen, show the stats + if ( cg_gameType.integer == GT_SINGLE_PLAYER ) { + if ( strlen( cg_missionStats.string ) > 1 ) { + return; + } + } + + backuprefdef = cg.refdef; + + if ( cg_skybox.integer ) { + token = COM_ParseExt( &cstr, qfalse ); + if ( !token || !token[0] ) { + CG_Error( "CG_DrawSkyBoxPortal: error parsing skybox configstring\n" ); + } + cg.refdef.vieworg[0] = atof( token ); + + token = COM_ParseExt( &cstr, qfalse ); + if ( !token || !token[0] ) { + CG_Error( "CG_DrawSkyBoxPortal: error parsing skybox configstring\n" ); + } + cg.refdef.vieworg[1] = atof( token ); + + token = COM_ParseExt( &cstr, qfalse ); + if ( !token || !token[0] ) { + CG_Error( "CG_DrawSkyBoxPortal: error parsing skybox configstring\n" ); + } + cg.refdef.vieworg[2] = atof( token ); + + token = COM_ParseExt( &cstr, qfalse ); + if ( !token || !token[0] ) { + CG_Error( "CG_DrawSkyBoxPortal: error parsing skybox configstring\n" ); + } + fov_x = atoi( token ); + + if ( !fov_x ) { + fov_x = 90; + } + + + // setup fog the first time, ignore this part of the configstring after that + token = COM_ParseExt( &cstr, qfalse ); + if ( !token || !token[0] ) { + CG_Error( "CG_DrawSkyBoxPortal: error parsing skybox configstring. No fog state\n" ); + } else { + vec4_t fogColor; + int fogStart, fogEnd; + + if ( atoi( token ) ) { // this camera has fog + // if(!foginited) { + if ( 1 ) { + token = COM_ParseExt( &cstr, qfalse ); + if ( !token || !token[0] ) { + CG_Error( "CG_DrawSkyBoxPortal: error parsing skybox configstring. No fog[0]\n" ); + } + fogColor[0] = atof( token ); + + token = COM_ParseExt( &cstr, qfalse ); + if ( !token || !token[0] ) { + CG_Error( "CG_DrawSkyBoxPortal: error parsing skybox configstring. No fog[1]\n" ); + } + fogColor[1] = atof( token ); + + token = COM_ParseExt( &cstr, qfalse ); + if ( !token || !token[0] ) { + CG_Error( "CG_DrawSkyBoxPortal: error parsing skybox configstring. No fog[2]\n" ); + } + fogColor[2] = atof( token ); + + token = COM_ParseExt( &cstr, qfalse ); + if ( !token || !token[0] ) { + fogStart = 0; + } else { + fogStart = atoi( token ); + } + + token = COM_ParseExt( &cstr, qfalse ); + if ( !token || !token[0] ) { + fogEnd = 0; + } else { + fogEnd = atoi( token ); + } + + trap_R_SetFog( FOG_PORTALVIEW, fogStart, fogEnd, fogColor[0], fogColor[1], fogColor[2], 1.1 ); + foginited = qtrue; + } + } else { + if ( !foginited ) { + trap_R_SetFog( FOG_PORTALVIEW, 0,0,0,0,0,0 ); // init to null + foginited = qtrue; + } + } + } + + //----(SA) end + + + if ( cg.predictedPlayerState.pm_type == PM_INTERMISSION ) { + // if in intermission, use a fixed value + fov_x = 90; + } else { + // user selectable + if ( cgs.dmflags & DF_FIXED_FOV ) { + // dmflag to prevent wide fov for all clients + fov_x = 90; + } else { + fov_x = cg_fov.value; + if ( fov_x < 1 ) { + fov_x = 1; + } else if ( fov_x > 160 ) { + fov_x = 160; + } + } + + // account for zooms + if ( cg.zoomval ) { + zoomFov = cg.zoomval; // (SA) use user scrolled amount + + if ( zoomFov < 1 ) { + zoomFov = 1; + } else if ( zoomFov > 160 ) { + zoomFov = 160; + } + } else { + zoomFov = lastfov; + } + + // do smooth transitions for the binocs + if ( cg.zoomedBinoc ) { // binoc zooming in + f = ( cg.time - cg.zoomTime ) / (float)ZOOM_TIME; + if ( f > 1.0 ) { + fov_x = zoomFov; + } else { + fov_x = fov_x + f * ( zoomFov - fov_x ); + } + lastfov = fov_x; + } else if ( cg.zoomval ) { // zoomed by sniper/snooper + fov_x = cg.zoomval; + lastfov = fov_x; + } else { // binoc zooming out + f = ( cg.time - cg.zoomTime ) / (float)ZOOM_TIME; + if ( f > 1.0 ) { + fov_x = fov_x; + } else { + fov_x = zoomFov + f * ( fov_x - zoomFov ); + } + } + } + + if ( cg.weaponSelect == WP_SNOOPERSCOPE ) { + cg.refdef.rdflags |= RDF_SNOOPERVIEW; + } else { + cg.refdef.rdflags &= ~RDF_SNOOPERVIEW; + } + + if ( cg.snap->ps.persistant[PERS_HWEAPON_USE] ) { + fov_x = 55; + } + + x = cg.refdef.width / tan( fov_x / 360 * M_PI ); + fov_y = atan2( cg.refdef.height, x ); + fov_y = fov_y * 360 / M_PI; + + cg.refdef.fov_x = fov_x; + cg.refdef.fov_y = fov_y; + + cg.refdef.rdflags |= RDF_SKYBOXPORTAL; + cg.refdef.rdflags |= RDF_DRAWSKYBOX; + + } else { // end if(cg_skybox.integer) + + cg.refdef.rdflags |= RDF_SKYBOXPORTAL; + cg.refdef.rdflags &= ~RDF_DRAWSKYBOX; + } + + + cg.refdef.time = cg.time; + + // draw the skybox + trap_R_RenderScene( &cg.refdef ); + + cg.refdef = backuprefdef; +} + +/* +========================= +removed CG_DrawNotebook +========================= +*/ + + +//========================================================================= + +extern void CG_SetupDlightstyles( void ); + + +//#define DEBUGTIME_ENABLED +#ifdef DEBUGTIME_ENABLED +#define DEBUGTIME CG_Printf( "t%i:%i ", dbgCnt++, elapsed = ( trap_Milliseconds() - dbgTime ) ); dbgTime += elapsed; +#else +#define DEBUGTIME +#endif + +/* +================= +CG_DrawActiveFrame + +Generates and draws a game scene and status information at the given time. +================= +*/ +void CG_DrawActiveFrame( int serverTime, stereoFrame_t stereoView, qboolean demoPlayback ) { + int inwater; + + cg.cld = 0; // NERVE - SMF - reset clientDamage + +#ifdef DEBUGTIME_ENABLED + int dbgTime = trap_Milliseconds(),elapsed; + int dbgCnt = 0; +#endif + + cg.time = serverTime; + cg.demoPlayback = demoPlayback; + + // update cvars + CG_UpdateCvars(); +/* + // RF, if we should force a weapon, then do so + if( !cg.weaponSelect ) { + if (cg_loadWeaponSelect.integer > 0) { + cg.weaponSelect = cg_loadWeaponSelect.integer; + cg.weaponSelectTime = cg.time; + trap_Cvar_Set( "cg_loadWeaponSelect", "0" ); // turn it off + } + } +*/ +#ifdef DEBUGTIME_ENABLED + CG_Printf( "\n" ); +#endif + DEBUGTIME + + // if we are only updating the screen as a loading + // pacifier, don't even try to read snapshots + if ( cg.infoScreenText[0] != 0 ) { + CG_DrawInformation(); + return; + } + + // any looped sounds will be respecified as entities + // are added to the render list + trap_S_ClearLoopingSounds( qfalse ); + + DEBUGTIME + + // clear all the render lists + trap_R_ClearScene(); + + DEBUGTIME + + // set up cg.snap and possibly cg.nextSnap + CG_ProcessSnapshots(); + + DEBUGTIME + + // if we haven't received any snapshots yet, all + // we can draw is the information screen + if ( !cg.snap || ( cg.snap->snapFlags & SNAPFLAG_NOT_ACTIVE ) ) { + CG_DrawInformation(); + return; + } + + if ( cg.weaponSelect == WP_FG42SCOPE || cg.weaponSelect == WP_SNOOPERSCOPE || cg.weaponSelect == WP_SNIPERRIFLE ) { + float spd; + spd = VectorLength( cg.snap->ps.velocity ); + if ( spd > 180.0f ) { + switch ( cg.weaponSelect ) { + case WP_FG42SCOPE: + CG_FinishWeaponChange( cg.weaponSelect, WP_FG42 ); + break; + case WP_SNOOPERSCOPE: + CG_FinishWeaponChange( cg.weaponSelect, WP_GARAND ); + break; + case WP_SNIPERRIFLE: + CG_FinishWeaponChange( cg.weaponSelect, WP_MAUSER ); + break; + } + } + } + + DEBUGTIME + + if ( !cg.lightstylesInited ) { + CG_SetupDlightstyles(); + } + + DEBUGTIME + + // if we have been told not to render, don't + if ( cg_norender.integer ) { + return; + } + + // this counter will be bumped for every valid scene we generate + cg.clientFrame++; + + // update cg.predictedPlayerState + CG_PredictPlayerState(); + + DEBUGTIME + + // decide on third person view + cg.renderingThirdPerson = cg_thirdPerson.integer /*|| (cg.snap->ps.stats[STAT_HEALTH] <= 0)*/; + + // build cg.refdef + inwater = CG_CalcViewValues(); + + CG_CalcShakeCamera(); + CG_ApplyShakeCamera(); + + DEBUGTIME + + // RF, draw the skyboxportal + CG_DrawSkyBoxPortal(); + + DEBUGTIME + + if ( inwater ) { + CG_UnderwaterSounds(); + } + + DEBUGTIME + + // first person blend blobs, done after AnglesToAxis + if ( !cg.renderingThirdPerson ) { + CG_DamageBlendBlob(); + } + + DEBUGTIME + + // build the render lists + if ( !cg.hyperspace ) { + CG_AddPacketEntities(); // adter calcViewValues, so predicted player state is correct + CG_AddMarks(); + + DEBUGTIME + + // Rafael particles + CG_AddParticles(); + // done. + + DEBUGTIME + + CG_AddLocalEntities(); + + DEBUGTIME + } + + + CG_AddViewWeapon( &cg.predictedPlayerState ); + + + DEBUGTIME + + // Ridah, trails + if ( !cg.hyperspace ) { + CG_AddFlameChunks(); + CG_AddTrails(); // this must come last, so the trails dropped this frame get drawn + } + // done. + + DEBUGTIME + + // finish up the rest of the refdef + if ( cg.testModelEntity.hModel ) { + CG_AddTestModel(); + } + cg.refdef.time = cg.time; + memcpy( cg.refdef.areamask, cg.snap->areamask, sizeof( cg.refdef.areamask ) ); + + DEBUGTIME + + // warning sounds when powerup is wearing off + CG_PowerupTimerSounds(); + + // make sure the lagometerSample and frame timing isn't done twice when in stereo + if ( stereoView != STEREO_RIGHT ) { + cg.frametime = cg.time - cg.oldTime; + if ( cg.frametime < 0 ) { + cg.frametime = 0; + } + cg.oldTime = cg.time; + CG_AddLagometerFrameInfo(); + } + + DEBUGTIME + + // let the client system know what our weapon, holdable item and zoom settings are + trap_SetUserCmdValue( cg.weaponSelect, cg.holdableSelect, cg.zoomSensitivity, cg.cld ); + + // actually issue the rendering calls + CG_DrawActive( stereoView ); + + DEBUGTIME + + // update audio positions + trap_S_Respatialize( cg.snap->ps.clientNum, cg.refdef.vieworg, cg.refdef.viewaxis, inwater ); + + if ( cg_stats.integer ) { + CG_Printf( "cg.clientFrame:%i\n", cg.clientFrame ); + } + + DEBUGTIME +} + diff --git a/src/cgame/cg_weapons.c b/src/cgame/cg_weapons.c new file mode 100644 index 0000000..4ce1a4a --- /dev/null +++ b/src/cgame/cg_weapons.c @@ -0,0 +1,6303 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +/* + * name: cg_weapons.c + * + * desc: events and effects dealing with weapons + * +*/ + +#include "cg_local.h" + +int wolfkickModel; +int hWeaponSnd; +int hflakWeaponSnd; +int notebookModel; +int propellerModel; + +vec3_t ejectBrassCasingOrigin; + +//----(SA) +// forward decs +static int getAltWeapon( int weapnum ); +int getEquivWeapon( int weapnum ); +int CG_WeaponIndex( int weapnum, int *bank, int *cycle ); +static qboolean CG_WeaponHasAmmo( int i ); + +static int maxWeapBanks = MAX_WEAP_BANKS, maxWeapsInBank = MAX_WEAPS_IN_BANK; // JPW NERVE + +int weapBanks[MAX_WEAP_BANKS][MAX_WEAPS_IN_BANK] = { + // bank + {0, 0, 0 }, // 0 (empty) + + {WP_KNIFE, 0, 0 }, // 1 + {WP_LUGER, WP_COLT, 0 }, // 2 // WP_AKIMBO + {WP_MP40, WP_THOMPSON, WP_STEN }, // 3 + {WP_MAUSER, WP_GARAND, 0 }, // 4 + {WP_FG42, 0, 0 }, // 5 + {WP_GRENADE_LAUNCHER, WP_GRENADE_PINEAPPLE, WP_DYNAMITE }, // 6 + {WP_PANZERFAUST, 0, 0 }, // 7 + {WP_VENOM, 0, 0 }, // 8 + {WP_FLAMETHROWER, 0, 0 }, // 9 + {WP_TESLA, 0, 0 } // 10 +}; + +// JPW NERVE -- in mutiplayer, characters get knife/special on button 1, pistols on 2, 2-handed on 3 +int weapBanksMultiPlayer[MAX_WEAP_BANKS_MP][MAX_WEAPS_IN_BANK_MP] = { + {0, 0, 0, 0, 0, 0, 0, 0 }, // empty bank '0' + {WP_KNIFE, 0, 0, 0, 0, 0, 0, 0 }, + {WP_LUGER, WP_COLT, 0, 0, 0, 0, 0, 0 }, + {WP_MP40, WP_THOMPSON, WP_STEN, WP_MAUSER, WP_GARAND, WP_PANZERFAUST, WP_VENOM, WP_FLAMETHROWER }, + {WP_GRENADE_LAUNCHER, WP_GRENADE_PINEAPPLE, 0, 0, 0, 0, 0, 0, }, + {WP_CLASS_SPECIAL, 0, 0, 0, 0, 0, 0, 0, }, + {WP_DYNAMITE, 0, 0, 0, 0, 0, 0, 0 } +}; +// jpw + +//----(SA) end + + +/* +============== +CG_MachineGunEjectBrassNew +============== +*/ +static void CG_MachineGunEjectBrassNew( centity_t *cent ) { + localEntity_t *le; + refEntity_t *re; + vec3_t velocity, xvelocity; + float waterScale = 1.0f; + vec3_t v[3]; + + if ( cg_brassTime.integer <= 0 ) { + return; + } + + le = CG_AllocLocalEntity(); + re = &le->refEntity; + + velocity[0] = 16; + velocity[1] = -50 + 40 * crandom(); + velocity[2] = 100 + 50 * crandom(); + + le->leType = LE_FRAGMENT; + le->startTime = cg.time; + le->endTime = le->startTime + cg_brassTime.integer + ( cg_brassTime.integer / 4 ) * random(); + + le->pos.trType = TR_GRAVITY; + le->pos.trTime = cg.time - ( rand() & 15 ); + + AnglesToAxis( cent->lerpAngles, v ); + + VectorCopy( ejectBrassCasingOrigin, re->origin ); + + VectorCopy( re->origin, le->pos.trBase ); + + if ( CG_PointContents( re->origin, -1 ) & ( CONTENTS_WATER | CONTENTS_SLIME ) ) { //----(SA) modified since slime is no longer deadly +// if ( CG_PointContents( re->origin, -1 ) & CONTENTS_WATER ) { + waterScale = 0.10; + } + + xvelocity[0] = velocity[0] * v[0][0] + velocity[1] * v[1][0] + velocity[2] * v[2][0]; + xvelocity[1] = velocity[0] * v[0][1] + velocity[1] * v[1][1] + velocity[2] * v[2][1]; + xvelocity[2] = velocity[0] * v[0][2] + velocity[1] * v[1][2] + velocity[2] * v[2][2]; + VectorScale( xvelocity, waterScale, le->pos.trDelta ); + + AxisCopy( axisDefault, re->axis ); + re->hModel = cgs.media.smallgunBrassModel; + + le->bounceFactor = 0.4 * waterScale; + + le->angles.trType = TR_LINEAR; + le->angles.trTime = cg.time; + le->angles.trBase[0] = rand() & 31; + le->angles.trBase[1] = rand() & 31; + le->angles.trBase[2] = rand() & 31; + le->angles.trDelta[0] = 2; + le->angles.trDelta[1] = 1; + le->angles.trDelta[2] = 0; + + le->leFlags = LEF_TUMBLE; + + + { + int contents; + vec3_t end; + VectorCopy( cent->lerpOrigin, end ); + end[2] -= 24; + contents = trap_CM_PointContents( end, 0 ); + if ( contents & ( CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ) ) { + le->leBounceSoundType = LEBS_NONE; + } else { + le->leBounceSoundType = LEBS_BRASS; + } + } + + le->leMarkType = LEMT_NONE; +} + +/* +========================== +CG_MachineGunEjectBrass +========================== +*/ + +static void CG_MachineGunEjectBrass( centity_t *cent ) { + localEntity_t *le; + refEntity_t *re; + vec3_t velocity, xvelocity; + vec3_t offset, xoffset; + float waterScale = 1.0f; + vec3_t v[3]; + + if ( cg_brassTime.integer <= 0 ) { + return; + } + + if ( !( cg.snap->ps.persistant[PERS_HWEAPON_USE] ) && ( cent->currentState.clientNum == cg.snap->ps.clientNum ) ) { + CG_MachineGunEjectBrassNew( cent ); + return; + } + + le = CG_AllocLocalEntity(); + re = &le->refEntity; + + // velocity[0] = 0; + velocity[0] = 16; // Maxx Kaufman offset value + velocity[1] = -50 + 40 * crandom(); + velocity[2] = 100 + 50 * crandom(); + + le->leType = LE_FRAGMENT; + le->startTime = cg.time; + le->endTime = le->startTime + cg_brassTime.integer + ( cg_brassTime.integer / 4 ) * random(); + + le->pos.trType = TR_GRAVITY; + le->pos.trTime = cg.time - ( rand() & 15 ); + + AnglesToAxis( cent->lerpAngles, v ); + + if ( cg.snap->ps.persistant[PERS_HWEAPON_USE] ) { + offset[0] = 32; + offset[1] = -4; + offset[2] = 0; + } else if ( cg.predictedPlayerState.weapon == WP_MP40 || cg.predictedPlayerState.weapon == WP_THOMPSON ) { + offset[0] = 20; // Maxx Kaufman offset value + offset[1] = -4; + offset[2] = 24; + } else if ( cg.predictedPlayerState.weapon == WP_VENOM ) { + offset[0] = 12; + offset[1] = -4; + offset[2] = 24; + } else { + VectorClear( offset ); + } + + + + xoffset[0] = offset[0] * v[0][0] + offset[1] * v[1][0] + offset[2] * v[2][0]; + xoffset[1] = offset[0] * v[0][1] + offset[1] * v[1][1] + offset[2] * v[2][1]; + xoffset[2] = offset[0] * v[0][2] + offset[1] * v[1][2] + offset[2] * v[2][2]; + VectorAdd( cent->lerpOrigin, xoffset, re->origin ); + + VectorCopy( re->origin, le->pos.trBase ); + + if ( CG_PointContents( re->origin, -1 ) & ( CONTENTS_WATER | CONTENTS_SLIME ) ) { //----(SA) modified since slime is no longer deadly +// if ( CG_PointContents( re->origin, -1 ) & CONTENTS_WATER ) { + waterScale = 0.10; + } + + xvelocity[0] = velocity[0] * v[0][0] + velocity[1] * v[1][0] + velocity[2] * v[2][0]; + xvelocity[1] = velocity[0] * v[0][1] + velocity[1] * v[1][1] + velocity[2] * v[2][1]; + xvelocity[2] = velocity[0] * v[0][2] + velocity[1] * v[1][2] + velocity[2] * v[2][2]; + VectorScale( xvelocity, waterScale, le->pos.trDelta ); + + AxisCopy( axisDefault, re->axis ); + re->hModel = cgs.media.machinegunBrassModel; + + le->bounceFactor = 0.4 * waterScale; + + le->angles.trType = TR_LINEAR; + le->angles.trTime = cg.time; + le->angles.trBase[0] = rand() & 31; + le->angles.trBase[1] = rand() & 31; + le->angles.trBase[2] = rand() & 31; + le->angles.trDelta[0] = 2; + le->angles.trDelta[1] = 1; + le->angles.trDelta[2] = 0; + + le->leFlags = LEF_TUMBLE; + + { + int contents; + vec3_t end; + VectorCopy( cent->lerpOrigin, end ); + end[2] -= 24; + contents = trap_CM_PointContents( end, 0 ); + if ( contents & ( CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ) ) { + le->leBounceSoundType = LEBS_NONE; + } else { + le->leBounceSoundType = LEBS_BRASS; + } + } + + le->leMarkType = LEMT_NONE; +} + + +//----(SA) added +/* +============== +CG_PanzerFaustEjectBrass + toss the 'used' panzerfaust casing (unit is one-shot, disposable) +============== +*/ +static void CG_PanzerFaustEjectBrass( centity_t *cent ) { + localEntity_t *le; + refEntity_t *re; + vec3_t velocity, xvelocity; + vec3_t offset, xoffset; + float waterScale = 1.0f; + vec3_t v[3]; + + le = CG_AllocLocalEntity(); + re = &le->refEntity; + +// velocity[0] = 16; +// velocity[1] = -50 + 40 * crandom(); +// velocity[2] = 100 + 50 * crandom(); + + velocity[0] = 16; + velocity[1] = -200; + velocity[2] = 0; + + le->leType = LE_FRAGMENT; + le->startTime = cg.time; +// le->startTime = cg.time + 2000; + le->endTime = le->startTime + ( cg_brassTime.integer * 8 ) + ( cg_brassTime.integer * random() ); + + le->pos.trType = TR_GRAVITY; + le->pos.trTime = cg.time - ( rand() & 15 ); +// le->pos.trTime = cg.time - 2000; + + AnglesToAxis( cent->lerpAngles, v ); + +// offset[0] = 12; +// offset[1] = -4; +// offset[2] = 24; + + offset[0] = -24; // forward + offset[1] = -4; // left + offset[2] = 24; // up + + xoffset[0] = offset[0] * v[0][0] + offset[1] * v[1][0] + offset[2] * v[2][0]; + xoffset[1] = offset[0] * v[0][1] + offset[1] * v[1][1] + offset[2] * v[2][1]; + xoffset[2] = offset[0] * v[0][2] + offset[1] * v[1][2] + offset[2] * v[2][2]; + VectorAdd( cent->lerpOrigin, xoffset, re->origin ); + + VectorCopy( re->origin, le->pos.trBase ); + + if ( CG_PointContents( re->origin, -1 ) & ( CONTENTS_WATER | CONTENTS_SLIME ) ) { + waterScale = 0.10; + } + + xvelocity[0] = velocity[0] * v[0][0] + velocity[1] * v[1][0] + velocity[2] * v[2][0]; + xvelocity[1] = velocity[0] * v[0][1] + velocity[1] * v[1][1] + velocity[2] * v[2][1]; + xvelocity[2] = velocity[0] * v[0][2] + velocity[1] * v[1][2] + velocity[2] * v[2][2]; + VectorScale( xvelocity, waterScale, le->pos.trDelta ); + + AxisCopy( axisDefault, re->axis ); + + // (SA) make it bigger + le->sizeScale = 3.0f; + + re->hModel = cgs.media.panzerfaustBrassModel; + + le->bounceFactor = 0.4 * waterScale; + + le->angles.trType = TR_LINEAR; + le->angles.trTime = cg.time; +// le->angles.trBase[0] = rand()&31; +// le->angles.trBase[1] = rand()&31; +// le->angles.trBase[2] = rand()&31; + le->angles.trBase[0] = 0; + le->angles.trBase[1] = cent->currentState.apos.trBase[1]; // rotate to match the player + le->angles.trBase[2] = 0; +// le->angles.trDelta[0] = 2; +// le->angles.trDelta[1] = 1; +// le->angles.trDelta[2] = 0; + le->angles.trDelta[0] = 0; + le->angles.trDelta[1] = 0; + le->angles.trDelta[2] = 0; + + le->leFlags = LEF_TUMBLE | LEF_SMOKING; // (SA) probably doesn't need to be 'tumble' since it doesn't really rotate much when flying + + le->leBounceSoundType = LEBS_NONE; + + le->leMarkType = LEMT_NONE; +} +/* +============== +CG_SpearTrail + simple bubble trail behind a missile +============== +*/ +void CG_SpearTrail( centity_t *ent, const weaponInfo_t *wi ) { + int contents, lastContents; + vec3_t origin, lastPos; + entityState_t *es; + + es = &ent->currentState; + BG_EvaluateTrajectory( &es->pos, cg.time, origin ); + contents = CG_PointContents( origin, -1 ); + + BG_EvaluateTrajectory( &es->pos, ent->trailTime, lastPos ); + lastContents = CG_PointContents( lastPos, -1 ); + + ent->trailTime = cg.time; + + if ( contents & ( CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ) ) { + if ( contents & lastContents & CONTENTS_WATER ) { + CG_BubbleTrail( lastPos, origin, 1, 8 ); + } + } +} + + +// JPW NERVE -- LT pyro for marking air strikes +/* +========================== +CG_PyroSmokeTrail +========================== +*/ +void CG_PyroSmokeTrail( centity_t *ent, const weaponInfo_t *wi ) { + int step; + vec3_t origin, lastPos, dir; + int contents; + int lastContents, startTime; + entityState_t *es; + int t; + float rnd; + static float grounddir = 99; + localEntity_t *le; + + if ( grounddir == 99 ) { // pick a wind direction -- cheap trick because it can be different + grounddir = crandom(); // on different clients, but it's all smoke and mirrors anyway + + } + step = 30; + es = &ent->currentState; + startTime = ent->trailTime; + t = step * ( ( startTime + step ) / step ); + + BG_EvaluateTrajectory( &es->pos, cg.time, origin ); + contents = CG_PointContents( origin, -1 ); + + BG_EvaluateTrajectory( &es->pos, ent->trailTime, lastPos ); + lastContents = CG_PointContents( lastPos, -1 ); + + ent->trailTime = cg.time; + +/* smoke pyro works fine in water (well, it's dye in real life, might wanna change this in-game) + if ( contents & ( CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ) ) + return; +*/ + + // drop fire trail sprites + for ( ; t <= ent->trailTime ; t += step ) { + + BG_EvaluateTrajectory( &es->pos, t, lastPos ); + rnd = random(); + + //VectorCopy (ent->lerpOrigin, lastPos); + + if ( ent->currentState.density ) { // corkscrew effect + vec3_t right; + vec3_t angles; + VectorCopy( ent->currentState.apos.trBase, angles ); + angles[ROLL] += cg.time % 360; + AngleVectors( angles, NULL, right, NULL ); + VectorMA( lastPos, ent->currentState.density, right, lastPos ); + } + + dir[0] = crandom() * 5; // compute offset from flare base + dir[1] = crandom() * 5; + dir[2] = 0; + VectorAdd( lastPos,dir,origin ); // store in origin + + rnd = random(); + + dir[0] = random() * 0.25; + dir[1] = grounddir; // simulate a little wind so it looks natural + dir[2] = random(); // one direction (so smoke goes side-like) + VectorNormalize( dir ); + VectorScale( dir,45,dir ); // was 75 + + if ( !ent->currentState.otherEntityNum2 ) { // axis team, generate red smoke + le = CG_SmokePuff( origin, dir, + 25 + rnd * 110, // width + rnd * 0.5 + 0.5, rnd * 0.5 + 0.5, 1, 0.5, + 4800 + ( rand() % 2800 ), // duration was 2800+ + t, + 0, + 0, + cgs.media.smokePuffShader ); + } else { + le = CG_SmokePuff( origin, dir, + 25 + rnd * 110, // width + 1.0, rnd * 0.5 + 0.5, rnd * 0.5 + 0.5, 0.5, + 4800 + ( rand() % 2800 ), // duration was 2800+ + t, + 0, + 0, + cgs.media.smokePuffShader ); + } +// CG_ParticleExplosion( "expblue", lastPos, vec3_origin, 100 + (int)(rnd*400), 4, 4 ); // fire "flare" + + + // use the optimized local entity add +// le->leType = LE_SCALE_FADE; +/* this one works + if (rand()%4) + CG_ParticleExplosion( "blacksmokeanim", origin, dir, 2800+(int)(random()*1500), 15, 45+(int)(rnd*90) ); // smoke blacksmokeanim + else + CG_ParticleExplosion( "expblue", lastPos, vec3_origin, 100 + (int)(rnd*400), 4, 4 ); // fire "flare" +*/ + } +} +// jpw + + +// Ridah, new trail effects +/* +========================== +CG_RocketTrail +========================== +*/ +void CG_RocketTrail( centity_t *ent, const weaponInfo_t *wi ) { + int step; + vec3_t origin, lastPos; + int contents; + int lastContents, startTime; + entityState_t *es; + int t; +// localEntity_t *le; + + if ( ent->currentState.eType == ET_FLAMEBARREL ) { + step = 30; + } else if ( ent->currentState.eType == ET_FP_PARTS ) { + step = 50; + } else if ( ent->currentState.eType == ET_RAMJET ) { + step = 10; + } else { + step = 10; + } + + es = &ent->currentState; + startTime = ent->trailTime; + t = step * ( ( startTime + step ) / step ); + + BG_EvaluateTrajectory( &es->pos, cg.time, origin ); + contents = CG_PointContents( origin, -1 ); + + // if object (e.g. grenade) is stationary, don't toss up smoke + if ( ( ent->currentState.eType != ET_RAMJET ) && es->pos.trType == TR_STATIONARY ) { + ent->trailTime = cg.time; + return; + } + + BG_EvaluateTrajectory( &es->pos, ent->trailTime, lastPos ); + lastContents = CG_PointContents( lastPos, -1 ); + + ent->trailTime = cg.time; + + if ( contents & ( CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ) ) { + if ( contents & lastContents & CONTENTS_WATER ) { + CG_BubbleTrail( lastPos, origin, 3, 8 ); + } + return; + } + + // drop fire trail sprites + for ( ; t <= ent->trailTime ; t += step ) { + float rnd; + + BG_EvaluateTrajectory( &es->pos, t, lastPos ); + /* + le = CG_SmokePuff( lastPos, vec3_origin, + 5, // width + 1, 1, 1, 0.33, + 150 + rand()%350, // duration + t, + 0, + cgs.media.flameThrowerhitShader ); + + // use the optimized local entity add + le->leType = LE_SCALE_FADE; + */ + rnd = random(); + if ( ent->currentState.eType == ET_FLAMEBARREL ) { + if ( ( rand() % 100 ) > 50 ) { + CG_ParticleExplosion( "twiltb2", lastPos, vec3_origin, 100 + (int)( rnd * 400 ), 5, 7 + (int)( rnd * 10 ) ); // fire + + } + CG_ParticleExplosion( "blacksmokeanim", lastPos, vec3_origin, 800 + (int)( rnd * 1500 ), 5, 12 + (int)( rnd * 30 ) ); // smoke + } else if ( ent->currentState.eType == ET_FP_PARTS ) { + if ( ( rand() % 100 ) > 50 ) { + CG_ParticleExplosion( "twiltb2", lastPos, vec3_origin, 100 + (int)( rnd * 400 ), 5, 7 + (int)( rnd * 10 ) ); // fire + + } + CG_ParticleExplosion( "blacksmokeanim", lastPos, vec3_origin, 800 + (int)( rnd * 1500 ), 5, 12 + (int)( rnd * 30 ) ); // smoke + } else if ( ent->currentState.eType == ET_RAMJET ) { + int duration; + + VectorCopy( ent->lerpOrigin, lastPos ); + duration = 100; + CG_ParticleExplosion( "twiltb2", lastPos, vec3_origin, duration + (int)( rnd * 100 ), 5, 5 + (int)( rnd * 10 ) ); // fire + CG_ParticleExplosion( "blacksmokeanim", lastPos, vec3_origin, 400 + (int)( rnd * 750 ), 12, 24 + (int)( rnd * 30 ) ); // smoke + } else if ( ent->currentState.eType == ET_FIRE_COLUMN || ent->currentState.eType == ET_FIRE_COLUMN_SMOKE ) { + int duration; + int sizeStart; + int sizeEnd; + + //VectorCopy (ent->lerpOrigin, lastPos); + + if ( ent->currentState.density ) { // corkscrew effect + vec3_t right; + vec3_t angles; + VectorCopy( ent->currentState.apos.trBase, angles ); + angles[ROLL] += cg.time % 360; + AngleVectors( angles, NULL, right, NULL ); + VectorMA( lastPos, ent->currentState.density, right, lastPos ); + } + + duration = ent->currentState.angles[0]; + sizeStart = ent->currentState.angles[1]; + sizeEnd = ent->currentState.angles[2]; + + if ( !duration ) { + duration = 100; + } + + if ( !sizeStart ) { + sizeStart = 5; + } + + if ( !sizeEnd ) { + sizeEnd = 7; + } + + CG_ParticleExplosion( "twiltb2", lastPos, vec3_origin, duration + (int)( rnd * 400 ), sizeStart, sizeEnd + (int)( rnd * 10 ) ); // fire + + if ( ent->currentState.eType == ET_FIRE_COLUMN_SMOKE && ( rand() % 100 ) > 50 ) { + CG_ParticleExplosion( "blacksmokeanim", lastPos, vec3_origin, 800 + (int)( rnd * 1500 ), 5, 12 + (int)( rnd * 30 ) ); // smoke + } + } else + { + //CG_ParticleExplosion( "twiltb", lastPos, vec3_origin, 300+(int)(rnd*100), 4, 14+(int)(rnd*8) ); // fire + CG_ParticleExplosion( "blacksmokeanim", lastPos, vec3_origin, 800 + (int)( rnd * 1500 ), 5, 12 + (int)( rnd * 30 ) ); // smoke + } + } +/* + // spawn a smoke junction + if ((cg.time - ent->lastTrailTime) >= 50 + rand()%50) { + ent->headJuncIndex = CG_AddSmokeJunc( ent->headJuncIndex, + cgs.media.smokeTrailShader, + origin, + 4500, 0.4, 20, 80 ); + ent->lastTrailTime = cg.time; + } +*/ +// done. +} + + +// Ridah +/* +========================== +CG_GrenadeTrail +========================== +*/ +static void CG_GrenadeTrail( centity_t *ent, const weaponInfo_t *wi ) { + int step; + vec3_t origin, lastPos; + int contents; + int lastContents, startTime; + entityState_t *es; + int t; + + step = 15; // nice and smooth curves + + es = &ent->currentState; + startTime = ent->trailTime; + t = step * ( ( startTime + step ) / step ); + + BG_EvaluateTrajectory( &es->pos, cg.time, origin ); + contents = CG_PointContents( origin, -1 ); + + // if object (e.g. grenade) is stationary, don't toss up smoke + if ( es->pos.trType == TR_STATIONARY ) { + ent->trailTime = cg.time; + return; + } + + BG_EvaluateTrajectory( &es->pos, ent->trailTime, lastPos ); + lastContents = CG_PointContents( lastPos, -1 ); + + ent->trailTime = cg.time; + + if ( contents & ( CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ) ) { + if ( contents & lastContents & CONTENTS_WATER ) { + CG_BubbleTrail( lastPos, origin, 2, 8 ); + } + return; + } + +//----(SA) trying this back on for DM + + // spawn smoke junctions + for ( ; t <= ent->trailTime ; t += step ) { + BG_EvaluateTrajectory( &es->pos, t, origin ); + ent->headJuncIndex = CG_AddSmokeJunc( ent->headJuncIndex, + cgs.media.smokeTrailShader, + origin, +// 1500, 0.3, 10, 50 ); + 1000, 0.3, 2, 20 ); + ent->lastTrailTime = cg.time; + } +//----(SA) end +} +// done. + + + + +/* +========================== +CG_NailgunEjectBrass +========================== +*/ +// TTimo: unused +/* +static void CG_NailgunEjectBrass( centity_t *cent ) { + localEntity_t *smoke; + vec3_t origin; + vec3_t v[3]; + vec3_t offset; + vec3_t xoffset; + vec3_t up; + + AnglesToAxis( cent->lerpAngles, v ); + + offset[0] = 0; + offset[1] = -12; + offset[2] = 24; + + xoffset[0] = offset[0] * v[0][0] + offset[1] * v[1][0] + offset[2] * v[2][0]; + xoffset[1] = offset[0] * v[0][1] + offset[1] * v[1][1] + offset[2] * v[2][1]; + xoffset[2] = offset[0] * v[0][2] + offset[1] * v[1][2] + offset[2] * v[2][2]; + VectorAdd( cent->lerpOrigin, xoffset, origin ); + + VectorSet( up, 0, 0, 64 ); + + smoke = CG_SmokePuff( origin, up, 32, 1, 1, 1, 0.33f, 700, cg.time, 0, 0, cgs.media.smokePuffShader ); + // use the optimized local entity add + smoke->leType = LE_SCALE_FADE; +} + +static void CG_NailTrail( centity_t *ent, const weaponInfo_t *wi ) { + int step; + vec3_t origin, lastPos; + int t; + int startTime, contents; + int lastContents; + entityState_t *es; + vec3_t up; + localEntity_t *smoke; + + up[0] = 0; + up[1] = 0; + up[2] = 0; + + step = 50; + + es = &ent->currentState; + startTime = ent->trailTime; + t = step * ( (startTime + step) / step ); + + BG_EvaluateTrajectory( &es->pos, cg.time, origin ); + contents = CG_PointContents( origin, -1 ); + + // if object (e.g. grenade) is stationary, don't toss up smoke + if ( es->pos.trType == TR_STATIONARY ) { + ent->trailTime = cg.time; + return; + } + + BG_EvaluateTrajectory( &es->pos, ent->trailTime, lastPos ); + lastContents = CG_PointContents( lastPos, -1 ); + + ent->trailTime = cg.time; + + if ( contents & ( CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ) ) { + if ( contents & lastContents & CONTENTS_WATER ) { + CG_BubbleTrail( lastPos, origin, 1, 8 ); + } + return; + } + + for ( ; t <= ent->trailTime ; t += step ) { + BG_EvaluateTrajectory( &es->pos, t, lastPos ); + + smoke = CG_SmokePuff( lastPos, up, + wi->trailRadius, + 1, 1, 1, 0.33f, + wi->wiTrailTime, + t, + 0, + 0, + cgs.media.nailPuffShader ); + // use the optimized local entity add + smoke->leType = LE_SCALE_FADE; + } + +} +*/ + +/* +========================== +CG_RailTrail + SA: re-inserted this as a debug mechanism for bullets +========================== +*/ +void CG_RailTrail2( clientInfo_t *ci, vec3_t start, vec3_t end ) { + localEntity_t *le; + refEntity_t *re; + + le = CG_AllocLocalEntity(); + re = &le->refEntity; + + le->leType = LE_FADE_RGB; + le->startTime = cg.time; + le->endTime = cg.time + cg_railTrailTime.value; + le->lifeRate = 1.0 / ( le->endTime - le->startTime ); + + re->shaderTime = cg.time / 1000.0f; + re->reType = RT_RAIL_CORE; + re->customShader = cgs.media.railCoreShader; + + VectorCopy( start, re->origin ); + VectorCopy( end, re->oldorigin ); + +// // still allow different colors so we can tell AI shots from player shots, etc. + if ( ci ) { + le->color[0] = ci->color[0] * 0.75; + le->color[1] = ci->color[1] * 0.75; + le->color[2] = ci->color[2] * 0.75; + } else { + le->color[0] = 1; + le->color[1] = 0; + le->color[2] = 0; + } + le->color[3] = 1.0f; + + AxisClear( re->axis ); +} + +//void CG_RailTrailBox( clientInfo_t *ci, vec3_t start, vec3_t end) { +/* +============== +CG_RailTrail + modified so we could draw boxes for debugging as well +============== +*/ +void CG_RailTrail( clientInfo_t *ci, vec3_t start, vec3_t end, int type ) { //----(SA) added 'type' + vec3_t diff, v1, v2, v3, v4, v5, v6; + + if ( !type ) { // just a line + CG_RailTrail2( ci, start, end ); + return; + } + + // type '1' (box) + + VectorSubtract( start, end, diff ); + + VectorCopy( start, v1 ); + VectorCopy( start, v2 ); + VectorCopy( start, v3 ); + v1[0] -= diff[0]; + v2[1] -= diff[1]; + v3[2] -= diff[2]; + CG_RailTrail2( ci, start, v1 ); + CG_RailTrail2( ci, start, v2 ); + CG_RailTrail2( ci, start, v3 ); + + VectorCopy( end, v4 ); + VectorCopy( end, v5 ); + VectorCopy( end, v6 ); + v4[0] += diff[0]; + v5[1] += diff[1]; + v6[2] += diff[2]; + CG_RailTrail2( ci, end, v4 ); + CG_RailTrail2( ci, end, v5 ); + CG_RailTrail2( ci, end, v6 ); + + CG_RailTrail2( ci, v2, v6 ); + CG_RailTrail2( ci, v6, v1 ); + CG_RailTrail2( ci, v1, v5 ); + + CG_RailTrail2( ci, v2, v4 ); + CG_RailTrail2( ci, v4, v3 ); + CG_RailTrail2( ci, v3, v5 ); + +} + + + + + +/* +====================== +CG_ParseWeaponConfig + read information for weapon animations (first/length/fps) +====================== +*/ +static qboolean CG_ParseWeaponConfig( const char *filename, weaponInfo_t *wi ) { + char *text_p, *prev; + int len; + int i; + float fps; + char *token; + qboolean newfmt = qfalse; //----(SA) + char text[20000]; + fileHandle_t f; + + // load the file + len = trap_FS_FOpenFile( filename, &f, FS_READ ); + if ( len <= 0 ) { + return qfalse; + } + + if ( len >= sizeof( text ) - 1 ) { + CG_Printf( "File %s too long\n", filename ); + return qfalse; + } + + trap_FS_Read( text, len, f ); + text[len] = 0; + trap_FS_FCloseFile( f ); + + // parse the text + text_p = text; + + // read optional parameters + while ( 1 ) { + prev = text_p; // so we can unget + token = COM_Parse( &text_p ); + if ( !token ) { // get the variable + break; + } + if ( !Q_stricmp( token, "whatever_variable" ) ) { + token = COM_Parse( &text_p ); // get the value + if ( !token ) { + break; + } + continue; + } + + if ( !Q_stricmp( token, "newfmt" ) ) { + newfmt = qtrue; + continue; + } + + // if it is a number, start parsing animations + if ( Q_isnumeric( token[0] ) ) { + text_p = prev; // unget the token + break; + } + Com_Printf( "unknown token in weapon cfg '%s' is %s\n", token, filename ); + } + + + for ( i = 0 ; i < MAX_WP_ANIMATIONS ; i++ ) { + + token = COM_Parse( &text_p ); // first frame + if ( !token ) { + break; + } + wi->weapAnimations[i].firstFrame = atoi( token ); + + token = COM_Parse( &text_p ); // length + if ( !token ) { + break; + } + wi->weapAnimations[i].numFrames = atoi( token ); + + token = COM_Parse( &text_p ); // fps + if ( !token ) { + break; + } + fps = atof( token ); + if ( fps == 0 ) { + fps = 1; + } + + wi->weapAnimations[i].frameLerp = 1000 / fps; + wi->weapAnimations[i].initialLerp = 1000 / fps; + + token = COM_Parse( &text_p ); // looping frames + if ( !token ) { + break; + } + wi->weapAnimations[i].loopFrames = atoi( token ); + if ( wi->weapAnimations[i].loopFrames > wi->weapAnimations[i].numFrames ) { + wi->weapAnimations[i].loopFrames = wi->weapAnimations[i].numFrames; + } else if ( wi->weapAnimations[i].loopFrames < 0 ) { + wi->weapAnimations[i].loopFrames = 0; + } + + + // store animation/draw bits in '.moveSpeed' + + wi->weapAnimations[i].moveSpeed = 0; + + if ( newfmt ) { + token = COM_Parse( &text_p ); // barrel anim bits + if ( !token ) { + break; + } + wi->weapAnimations[i].moveSpeed = atoi( token ); + + token = COM_Parse( &text_p ); // animated weapon + if ( !token ) { + break; + } + if ( atoi( token ) ) { + wi->weapAnimations[i].moveSpeed |= ( 1 << W_MAX_PARTS ); // set the bit one higher than can be set by the barrel bits + + } + token = COM_Parse( &text_p ); // barrel hide bits (so objects can be flagged to not be drawn during all sequences (a reloading hand that comes in from off screen for that one animation for example) + if ( !token ) { + break; + } + wi->weapAnimations[i].moveSpeed |= ( ( atoi( token ) ) << 8 ); // use 2nd byte for draw bits + } + + } + + if ( i != MAX_WP_ANIMATIONS ) { + CG_Printf( "Error parsing weapon animation file: %s", filename ); + return qfalse; + } + + + return qtrue; +} + + +/* +================= +CG_RegisterWeapon + +The server says this item is used on this level +================= +*/ +void CG_RegisterWeapon( int weaponNum ) { + weaponInfo_t *weaponInfo; + gitem_t *item, *ammo; + char path[MAX_QPATH], comppath[MAX_QPATH]; + vec3_t mins, maxs; + int i; + + weaponInfo = &cg_weapons[weaponNum]; + + // don't bother trying + switch ( weaponNum ) { + case WP_NONE: + case WP_CLASS_SPECIAL: + case WP_MONSTER_ATTACK1: + case WP_MONSTER_ATTACK2: + case WP_MONSTER_ATTACK3: + case WP_GAUNTLET: + case WP_SNIPER: + case WP_MORTAR: + +// (SA) i don't know about these, but we don't have models for 'em + case WP_GRENADE_SMOKE: + case WP_MEDIC_HEAL: + return; + default: + break; + } + + + if ( weaponInfo->registered ) { + return; + } + + memset( weaponInfo, 0, sizeof( *weaponInfo ) ); + weaponInfo->registered = qtrue; + + for ( item = bg_itemlist + 1 ; item->classname ; item++ ) { + if ( item->giType == IT_WEAPON && item->giTag == weaponNum ) { + weaponInfo->item = item; + break; + } + } + if ( !item->classname ) { + CG_Error( "Couldn't find weapon %i", weaponNum ); + } + CG_RegisterItemVisuals( item - bg_itemlist ); + + // load cmodel before model so filecache works + + // alternate view weapon + weaponInfo->weaponModel[W_TP_MODEL] = trap_R_RegisterModel( item->world_model[W_TP_MODEL] ); + weaponInfo->weaponModel[W_FP_MODEL] = trap_R_RegisterModel( item->world_model[W_FP_MODEL] ); + weaponInfo->weaponModel[W_SKTP_MODEL] = trap_R_RegisterModel( item->world_model[W_SKTP_MODEL] ); + + if ( !weaponInfo->weaponModel[W_FP_MODEL] || !cg_drawFPGun.integer ) { + weaponInfo->weaponModel[W_FP_MODEL] = weaponInfo->weaponModel[W_TP_MODEL]; + } + + if ( !weaponInfo->weaponModel[W_TP_MODEL] ) { + // left commented out since we have level-loading optimization issues to still resolve. + // ie. every weapon and it's associated effects/parts/sounds etc. are loaded for every level. + // This was turned off when we started (the "only load what the level calls for" thing) because when + // DM does a "give all" and fires, he doesn't want to wait for everything to load. So perhaps a "cacheallweaps" or something. +// CG_Printf( "Couldn't register weapon model %i (unable to load view model)", weaponNum ); +// RF, I need to be able to run the game, I dont have the silencer weapon (19) +#ifndef _DEBUG +// CG_Error( "Couldn't register weapon model %i (unable to load view model)", weaponNum ); +#endif + return; + } + + + // load weapon config +//----(SA) modified. use first person model for finding weapon config name, not third + if ( item->world_model[W_FP_MODEL] ) { + COM_StripFilename( item->world_model[W_FP_MODEL], path ); + if ( !CG_ParseWeaponConfig( va( "%sweapon.cfg", path ), weaponInfo ) ) { + CG_Error( "Couldn't register weapon %i (%s) (failed to parse weapon.cfg)", weaponNum, path ); + } + } +//----(SA) end + + // calc midpoint for rotation + trap_R_ModelBounds( weaponInfo->weaponModel[W_TP_MODEL], mins, maxs ); + + for ( i = 0 ; i < 3 ; i++ ) { + weaponInfo->weaponMidpoint[i] = mins[i] + 0.5 * ( maxs[i] - mins[i] ); + } + + weaponInfo->weaponIcon[0] = trap_R_RegisterShader( item->icon ); + weaponInfo->weaponIcon[1] = trap_R_RegisterShader( va( "%s_select", item->icon ) ); // get the 'selected' icon as well + + // JOSEPH 4-17-00 + weaponInfo->ammoIcon = trap_R_RegisterShader( item->ammoicon ); + // END JOSEPH + + for ( ammo = bg_itemlist + 1 ; ammo->classname ; ammo++ ) { + if ( ( ammo->giType == IT_AMMO && ammo->giTag == BG_FindAmmoForWeapon( weaponNum ) ) ) { + break; + } + } + if ( ammo->classname && ammo->world_model[0] ) { + weaponInfo->ammoModel = trap_R_RegisterModel( ammo->world_model[0] ); + } + + if ( item->world_model[W_FP_MODEL] ) { + strcpy( comppath, item->world_model[W_FP_MODEL] ); // first try the fp view weap + } else if ( item->world_model[W_TP_MODEL] ) { + strcpy( comppath, item->world_model[W_TP_MODEL] ); // not there, use the standard view hand + + } + if ( ( !comppath || !cg_drawFPGun.integer ) && // then if it didn't find the 1st person one or you are set to not use one + item->world_model[W_TP_MODEL] ) { + strcpy( comppath, item->world_model[W_TP_MODEL] ); // use the standard view hand + + } + for ( i = W_TP_MODEL; i < W_NUM_TYPES; i++ ) + { + int j; + + if ( !item->world_model[i] ) { + strcpy( path, comppath ); + } else { + strcpy( path, item->world_model[i] ); + } + + COM_StripExtension( path, path ); + strcat( path, "_flash.md3" ); + weaponInfo->flashModel[i] = trap_R_RegisterModel( path ); + + + for ( j = 0; j < W_MAX_PARTS; j++ ) { + if ( !item->world_model[i] ) { + strcpy( path, comppath ); + } else { + strcpy( path, item->world_model[i] ); + } + COM_StripExtension( path, path ); + if ( j == W_PART_1 ) { + strcat( path, "_barrel.md3" ); + } else { + strcat( path, va( "_barrel%d.md3", j + 1 ) ); + } + weaponInfo->wpPartModels[i][j] = trap_R_RegisterModel( path ); + } + + // used for spinning belt on venom + if ( i == W_FP_MODEL ) { + if ( !item->world_model[2] ) { + strcpy( path, comppath ); + } else { + strcpy( path, item->world_model[2] ); + } + COM_StripExtension( path, path ); + strcat( path, "_barrel6b.md3" ); + weaponInfo->wpPartModels[i][W_PART_7] = trap_R_RegisterModel( path ); + } + } + + + // sniper scope model + if ( weaponNum == WP_MAUSER || weaponNum == WP_GARAND ) { + + if ( !item->world_model[W_FP_MODEL] ) { + strcpy( path, comppath ); + } else { + strcpy( path, item->world_model[W_FP_MODEL] ); + } + COM_StripExtension( path, path ); + strcat( path, "_scope.md3" ); + weaponInfo->modModel[0] = trap_R_RegisterModel( path ); + } + + if ( !item->world_model[W_FP_MODEL] ) { + strcpy( path, comppath ); + } else { + strcpy( path, item->world_model[W_FP_MODEL] ); + } + COM_StripExtension( path, path ); + strcat( path, "_hand.md3" ); + weaponInfo->handsModel = trap_R_RegisterModel( path ); + + if ( !weaponInfo->handsModel ) { + weaponInfo->handsModel = trap_R_RegisterModel( "models/weapons2/shotgun/shotgun_hand.md3" ); + } + + +//----(SA) weapon pickup 'stand' + if ( !item->world_model[W_TP_MODEL] ) { + strcpy( path, comppath ); + } else { + strcpy( path, item->world_model[W_TP_MODEL] ); + } + COM_StripExtension( path, path ); + strcat( path, "_stand.md3" ); + weaponInfo->standModel = trap_R_RegisterModel( path ); +//----(SA) end + + switch ( weaponNum ) { + case WP_MONSTER_ATTACK1: + case WP_MONSTER_ATTACK2: + case WP_MONSTER_ATTACK3: + break; + + + case WP_AKIMBO: //----(SA) added + // same as colt + MAKERGB( weaponInfo->flashDlightColor, 1.0, 0.6, 0.23 ); + weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/colt/coltf1.wav" ); + weaponInfo->flashEchoSound[0] = trap_S_RegisterSound( "sound/weapons/mp40/mp40e1.wav" ); // use same as mp40 + weaponInfo->ejectBrassFunc = CG_MachineGunEjectBrass; + + // unique + weaponInfo->reloadSound = trap_S_RegisterSound( "sound/weapons/colt/colt_reload2.wav" ); + break; + + case WP_COLT: + MAKERGB( weaponInfo->flashDlightColor, 1.0, 0.6, 0.23 ); + weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/colt/coltf1.wav" ); + weaponInfo->flashEchoSound[0] = trap_S_RegisterSound( "sound/weapons/mp40/mp40e1.wav" ); // use same as mp40 + weaponInfo->reloadSound = trap_S_RegisterSound( "sound/weapons/colt/colt_reload.wav" ); + weaponInfo->ejectBrassFunc = CG_MachineGunEjectBrass; + break; + + + case WP_KNIFE: + weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/knife/knife_slash1.wav" ); + weaponInfo->flashSound[1] = trap_S_RegisterSound( "sound/weapons/knife/knife_slash2.wav" ); + break; + + case WP_LUGER: + MAKERGB( weaponInfo->flashDlightColor, 1.0, 0.6, 0.23 ); + + weaponInfo->switchSound[0] = trap_S_RegisterSound( "sound/weapons/luger/silencerremove.wav" ); //----(SA) added + + weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/luger/lugerf1.wav" ); + weaponInfo->flashEchoSound[0] = trap_S_RegisterSound( "sound/weapons/mp40/mp40e1.wav" ); // use same as mp40 + weaponInfo->reloadSound = trap_S_RegisterSound( "sound/weapons/luger/luger_reload.wav" ); + weaponInfo->ejectBrassFunc = CG_MachineGunEjectBrass; + break; + + case WP_SILENCER: // luger mod + MAKERGB( weaponInfo->flashDlightColor, 1.0, 0.6, 0.23 ); + + weaponInfo->switchSound[0] = trap_S_RegisterSound( "sound/weapons/luger/silencerattatch.wav" ); //----(SA) added + + weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/luger/silencerf1.wav" ); + weaponInfo->reloadSound = trap_S_RegisterSound( "sound/weapons/luger/luger_reload.wav" ); + weaponInfo->ejectBrassFunc = CG_MachineGunEjectBrass; + break; + + case WP_MAUSER: + MAKERGB( weaponInfo->flashDlightColor, 1.0, 0.6, 0.23 ); + weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/mauser/mauserf1.wav" ); + weaponInfo->flashEchoSound[0] = trap_S_RegisterSound( "sound/weapons/mauser/mausere1.wav" ); + weaponInfo->lastShotSound[0] = trap_S_RegisterSound( "sound/weapons/mauser/mauserf1_last.wav" ); + weaponInfo->reloadSound = trap_S_RegisterSound( "sound/weapons/mauser/mauser_reload.wav" ); + weaponInfo->ejectBrassFunc = CG_MachineGunEjectBrass; + break; + case WP_SNIPERRIFLE: + MAKERGB( weaponInfo->flashDlightColor, 1.0, 0.6, 0.23 ); + weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/mauser/sniperf1.wav" ); + weaponInfo->flashEchoSound[0] = trap_S_RegisterSound( "sound/weapons/mauser/mausere1.wav" ); + weaponInfo->reloadSound = trap_S_RegisterSound( "sound/weapons/mauser/sniper_reload.wav" ); + weaponInfo->ejectBrassFunc = CG_MachineGunEjectBrass; + break; + + case WP_GARAND: + MAKERGB( weaponInfo->flashDlightColor, 1.0, 0.6, 0.23 ); + weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/garand/garandf1.wav" ); + weaponInfo->reloadSound = trap_S_RegisterSound( "sound/weapons/garand/garand_reload.wav" ); + weaponInfo->ejectBrassFunc = CG_MachineGunEjectBrass; + break; + case WP_SNOOPERSCOPE: + MAKERGB( weaponInfo->flashDlightColor, 1.0, 0.6, 0.23 ); + weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/garand/snooperf1.wav" ); + weaponInfo->reloadSound = trap_S_RegisterSound( "sound/weapons/garand/snooper_reload.wav" ); + weaponInfo->ejectBrassFunc = CG_MachineGunEjectBrass; + break; + + case WP_THOMPSON: + MAKERGB( weaponInfo->flashDlightColor, 1.0, 0.6, 0.23 ); + weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/thompson/thompson.wav" ); + weaponInfo->flashEchoSound[0] = trap_S_RegisterSound( "sound/weapons/mp40/mp40e1.wav" ); // use same as mp40 + weaponInfo->reloadSound = trap_S_RegisterSound( "sound/weapons/thompson/thompson_reload.wav" ); + weaponInfo->overheatSound = trap_S_RegisterSound( "sound/weapons/thompson/thompson_overheat.wav" ); + weaponInfo->ejectBrassFunc = CG_MachineGunEjectBrass; + break; + + case WP_MP40: + MAKERGB( weaponInfo->flashDlightColor, 1.0, 0.6, 0.23 ); + weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/mp40/mp40f1.wav" ); + weaponInfo->flashEchoSound[0] = trap_S_RegisterSound( "sound/weapons/mp40/mp40e1.wav" ); + weaponInfo->reloadSound = trap_S_RegisterSound( "sound/weapons/mp40/mp40_reload.wav" ); + weaponInfo->overheatSound = trap_S_RegisterSound( "sound/weapons/mp40/mp40_overheat.wav" ); + weaponInfo->ejectBrassFunc = CG_MachineGunEjectBrass; + break; + + case WP_STEN: + MAKERGB( weaponInfo->flashDlightColor, 1.0, 0.6, 0.23 ); + weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/sten/stenf1.wav" ); + weaponInfo->reloadSound = trap_S_RegisterSound( "sound/weapons/sten/sten_reload.wav" ); + weaponInfo->overheatSound = trap_S_RegisterSound( "sound/weapons/sten/sten_overheat.wav" ); + weaponInfo->ejectBrassFunc = CG_MachineGunEjectBrass; + break; + + + case WP_FG42: + case WP_FG42SCOPE: + MAKERGB( weaponInfo->flashDlightColor, 1.0, 0.6, 0.23 ); + weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/fg42/fg42f1.wav" ); + weaponInfo->flashEchoSound[0] = trap_S_RegisterSound( "sound/weapons/fg42/fg42e1.wav" ); + weaponInfo->reloadSound = trap_S_RegisterSound( "sound/weapons/fg42/fg42_reload.wav" ); + weaponInfo->ejectBrassFunc = CG_MachineGunEjectBrass; + break; + + + case WP_PANZERFAUST: + weaponInfo->ejectBrassFunc = CG_PanzerFaustEjectBrass; + weaponInfo->missileModel = trap_R_RegisterModel( "models/ammo/rocket/rocket.md3" ); + weaponInfo->missileSound = trap_S_RegisterSound( "sound/weapons/rocket/rockfly.wav" ); + weaponInfo->missileTrailFunc = CG_RocketTrail; + weaponInfo->missileDlight = 200; + weaponInfo->wiTrailTime = 2000; + weaponInfo->trailRadius = 64; + MAKERGB( weaponInfo->flashDlightColor, 0.75, 0.3, 0.0 ); + MAKERGB( weaponInfo->missileDlightColor, 0.75, 0.3, 0.0 ); + weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/rocket/rocklf1a.wav" ); + weaponInfo->reloadSound = trap_S_RegisterSound( "sound/weapons/rocket/rocklf_reload.wav" ); + cgs.media.rocketExplosionShader = trap_R_RegisterShader( "rocketExplosion" ); + break; + + case WP_MORTAR: + weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/mortar/mortarf1.wav" ); + weaponInfo->missileTrailFunc = CG_GrenadeTrail; + weaponInfo->missileDlight = 400; + weaponInfo->missileSound = trap_S_RegisterSound( "sound/weapons/rocket/rockfly.wav" ); + weaponInfo->wiTrailTime = 300; + weaponInfo->trailRadius = 32; + MAKERGB( weaponInfo->flashDlightColor, 1, 0.7, 0.5 ); + break; +// JPW NERVE + case WP_GRENADE_SMOKE: + weaponInfo->missileModel = trap_R_RegisterModel( "models/weapons2/grenade/pineapple.md3" ); + weaponInfo->missileTrailFunc = CG_PyroSmokeTrail; + weaponInfo->missileDlight = 200; + weaponInfo->wiTrailTime = 4000; + weaponInfo->trailRadius = 256; + break; +// jpw +// DHM - Nerve - temp effects + case WP_CLASS_SPECIAL: + case WP_MEDIC_HEAL: + weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/knife/knife_slash1.wav" ); + weaponInfo->flashSound[1] = trap_S_RegisterSound( "sound/weapons/knife/knife_slash2.wav" ); + break; +// dhm + case WP_GRENADE_LAUNCHER: + case WP_GRENADE_PINEAPPLE: + if ( weaponNum == WP_GRENADE_LAUNCHER ) { + weaponInfo->missileModel = trap_R_RegisterModel( "models/ammo/grenade1.md3" ); + } else { + weaponInfo->missileModel = trap_R_RegisterModel( "models/weapons2/grenade/pineapple.md3" ); + } + weaponInfo->missileTrailFunc = CG_GrenadeTrail; + weaponInfo->wiTrailTime = 700; +// weaponInfo->wiTrailTime = 2000; + weaponInfo->wiTrailTime = 1000; + weaponInfo->trailRadius = 32; + MAKERGB( weaponInfo->flashDlightColor, 1, 0.7, 0.5 ); + weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/grenade/grenlf1a.wav" ); + weaponInfo->reloadSound = trap_S_RegisterSound( "sound/weapons/grenade/grenlf_reload.wav" ); + cgs.media.grenadeExplosionShader = trap_R_RegisterShader( "grenadeExplosion" ); + break; + + case WP_DYNAMITE: + weaponInfo->missileModel = trap_R_RegisterModel( "models/ammo/dynamite.md3" ); +// weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/grenade/grenlf1a.wav" ); +// weaponInfo->reloadSound = trap_S_RegisterSound( "sound/weapons/grenade/grenlf_reload.wav" ); + cgs.media.grenadeExplosionShader = trap_R_RegisterShader( "grenadeExplosion" ); + break; + + case WP_VENOM: + MAKERGB( weaponInfo->flashDlightColor, 1.0, 0.6, 0.23 ); + weaponInfo->spinupSound = trap_S_RegisterSound( "sound/weapons/venom/venomsu1.wav" ); //----(SA) added + weaponInfo->spindownSound = trap_S_RegisterSound( "sound/weapons/venom/venomsd1.wav" ); //----(SA) added + weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/venom/venomf1.wav" ); + weaponInfo->reloadSound = trap_S_RegisterSound( "sound/weapons/venom/venom_reload.wav" ); + weaponInfo->overheatSound = trap_S_RegisterSound( "sound/weapons/venom/venom_overheat.wav" ); + weaponInfo->ejectBrassFunc = CG_MachineGunEjectBrass; + break; + + case WP_FLAMETHROWER: + //MAKERGB( weaponInfo->flashDlightColor, 1.0, 0.7, 0.4 ); + break; + + case WP_TESLA: + MAKERGB( weaponInfo->flashDlightColor, 0.2, 0.6, 1 ); + weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/tesla/teslaf1.wav" ); + weaponInfo->reloadSound = trap_S_RegisterSound( "sound/weapons/tesla/tesla_reload.wav" ); + weaponInfo->overheatSound = trap_S_RegisterSound( "sound/weapons/tesla/tesla_overheat.wav" ); + break; + + + case WP_GAUNTLET: + MAKERGB( weaponInfo->flashDlightColor, 0.6, 0.6, 1 ); + //weaponInfo->firingSound = trap_S_RegisterSound( "sound/weapons/melee/fstrun.wav" ); + weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/melee/fstatck.wav" ); + break; + + default: + MAKERGB( weaponInfo->flashDlightColor, 1, 1, 1 ); + weaponInfo->flashSound[0] = trap_S_RegisterSound( "sound/weapons/rocket/rocklf1a.wav" ); + break; + } +} + +/* +================= +CG_RegisterItemVisuals + +The server says this item is used on this level +================= +*/ +void CG_RegisterItemVisuals( int itemNum ) { + itemInfo_t *itemInfo; + gitem_t *item; + int i; + + itemInfo = &cg_items[ itemNum ]; + if ( itemInfo->registered ) { + return; + } + + item = &bg_itemlist[ itemNum ]; + + memset( itemInfo, 0, sizeof( &itemInfo ) ); + +//----(SA) umm, why was this set here? It sets registered to true, +//----(SA) then in the register weapon(below) it returns since +//----(SA) the first thing it does is to check if it's registered. + + // itemInfo->registered = qtrue; + + for ( i = 0; i < MAX_ITEM_MODELS; i++ ) + itemInfo->models[i] = trap_R_RegisterModel( item->world_model[i] ); + + + itemInfo->icons[0] = trap_R_RegisterShader( item->icon ); + if ( item->giType == IT_HOLDABLE ) { + // (SA) register alternate icons (since holdables can have multiple uses, they might have different icons to represent how many uses are left) + for ( i = 1; i < MAX_ITEM_ICONS; i++ ) + itemInfo->icons[i] = trap_R_RegisterShader( va( "%s%i", item->icon, i + 1 ) ); + } + + if ( item->giType == IT_WEAPON ) { + CG_RegisterWeapon( item->giTag ); + } + + itemInfo->registered = qtrue; //----(SA) moved this down after the registerweapon() + + wolfkickModel = trap_R_RegisterModel( "models/weapons2/foot/v_wolfoot_10f.md3" ); + hWeaponSnd = trap_S_RegisterSound( "sound/weapons/mg42/37mm.wav" ); + + hflakWeaponSnd = trap_S_RegisterSound( "sound/weapons/flak/flak.wav" ); + + notebookModel = trap_R_RegisterModel( "models/mapobjects/book/book.md3" ); + + propellerModel = trap_R_RegisterModel( "models/mapobjects/vehicles/m109_prop.md3" ); + +// JPW NERVE had to put this somewhere, this seems OK + if ( cg_gameType.integer != GT_WOLF ) { + maxWeapBanks = MAX_WEAP_BANKS; + maxWeapsInBank = MAX_WEAPS_IN_BANK; + } else { + trap_R_RegisterModel( "models/mapobjects/vehicles/m109.md3" ); + CG_RegisterWeapon( WP_GRENADE_SMOKE ); // register WP_CLASS_SPECIAL visuals here + CG_RegisterWeapon( WP_MEDIC_HEAL ); + maxWeapBanks = MAX_WEAP_BANKS_MP; + maxWeapsInBank = MAX_WEAPS_IN_BANK_MP; + } +// if player runs out of SMG ammunition, it shouldn't *also* deplete pistol ammunition. If you change this, change +// g_spawn.c as well + if ( cg_gameType.integer != GT_SINGLE_PLAYER ) { + item = BG_FindItem( "Thompson" ); + item->giAmmoIndex = WP_THOMPSON; + item = BG_FindItem( "Sten" ); + item->giAmmoIndex = WP_STEN; + item = BG_FindItem( "MP40" ); + item->giAmmoIndex = WP_MP40; + } +// jpw + + // + // powerups have an accompanying ring or sphere + // +// if ( item->giType == IT_POWERUP || item->giType == IT_HEALTH || +// item->giType == IT_ARMOR || item->giType == IT_HOLDABLE ) { +// if ( item->world_model[W_FP_MODEL] ) { +// itemInfo->models[W_FP_MODEL] = trap_R_RegisterModel( item->world_model[W_FP_MODEL] ); +// } +// } +} + + +/* +======================================================================================== + +VIEW WEAPON + +======================================================================================== +*/ + + +// +// weapon animations +// + +/* +============== +CG_GetPartFramesFromWeap + get animation info from the parent if necessary +============== +*/ +qboolean CG_GetPartFramesFromWeap( centity_t *cent, refEntity_t *part, refEntity_t *parent, int partid, weaponInfo_t *wi ) { + int i; + int frameoffset = 0; + animation_t *anim; + + anim = cent->pe.weap.animation; + + if ( partid == W_MAX_PARTS ) { + return qtrue; // primary weap model drawn for all frames right now + } + + // check draw bit + if ( anim->moveSpeed & ( 1 << ( partid + 8 ) ) ) { // hide bits are in high byte + return qfalse; // not drawn for current sequence + } + + // find part's start frame for this animation sequence + for ( i = 0; i < cent->pe.weap.animationNumber; i++ ) { + if ( wi->weapAnimations[i].moveSpeed & ( 1 << partid ) ) { // this part has animation for this sequence + frameoffset += wi->weapAnimations[i].numFrames; + } + } + + // now set the correct frame into the part + if ( anim->moveSpeed & ( 1 << partid ) ) { + part->backlerp = parent->backlerp; + part->oldframe = frameoffset + ( parent->oldframe - anim->firstFrame ); + part->frame = frameoffset + ( parent->frame - anim->firstFrame ); + } + + return qtrue; +} + + +/* +=============== +CG_SetWeapLerpFrameAnimation + +may include ANIM_TOGGLEBIT +=============== +*/ +static void CG_SetWeapLerpFrameAnimation( weaponInfo_t *wi, lerpFrame_t *lf, int newAnimation ) { + animation_t *anim; + + lf->animationNumber = newAnimation; + newAnimation &= ~ANIM_TOGGLEBIT; + + if ( newAnimation < 0 || newAnimation >= MAX_WP_ANIMATIONS ) { + CG_Error( "Bad animation number (CG_SWLFA): %i", newAnimation ); + } + + anim = &wi->weapAnimations[ newAnimation ]; + + lf->animation = anim; + lf->animationTime = lf->frameTime + anim->initialLerp; + + if ( cg_debugAnim.integer & 2 ) { + CG_Printf( "Weap Anim: %d\n", newAnimation ); + } +} + + +/* +=============== +CG_ClearWeapLerpFrame +=============== +*/ +void CG_ClearWeapLerpFrame( weaponInfo_t *wi, lerpFrame_t *lf, int animationNumber ) { + lf->frameTime = lf->oldFrameTime = cg.time; + CG_SetWeapLerpFrameAnimation( wi, lf, animationNumber ); + lf->oldFrame = lf->frame = lf->animation->firstFrame; + +} + + +/* +=============== +CG_RunWeapLerpFrame + +Sets cg.snap, cg.oldFrame, and cg.backlerp +cg.time should be between oldFrameTime and frameTime after exit +=============== +*/ +static void CG_RunWeapLerpFrame( clientInfo_t *ci, weaponInfo_t *wi, lerpFrame_t *lf, int newAnimation, float speedScale ) { + int f; + animation_t *anim; + + // debugging tool to get no animations + if ( cg_animSpeed.integer == 0 ) { + lf->oldFrame = lf->frame = lf->backlerp = 0; + return; + } + + // see if the animation sequence is switching + if ( !lf->animation ) { + CG_ClearWeapLerpFrame( wi, lf, newAnimation ); + } else if ( newAnimation != lf->animationNumber ) { + if ( ( newAnimation & ~ANIM_TOGGLEBIT ) == WEAP_RAISE || + ( newAnimation & ~ANIM_TOGGLEBIT ) == WEAP_ALTSWITCHFROM || + ( newAnimation & ~ANIM_TOGGLEBIT ) == WEAP_ALTSWITCHTO ) { + CG_ClearWeapLerpFrame( wi, lf, newAnimation ); // clear when switching to raise (since it should be out of view anyway) + } else { + CG_SetWeapLerpFrameAnimation( wi, lf, newAnimation ); + } + } + // RF, if the animation number is the same, but we are using a different weapon, we need to reset the lf->animation + //else if ( memcmp( &wi->weapAnimations[newAnimation&~ANIM_TOGGLEBIT], lf->animation, sizeof(*lf->animation) ) ) { + // CG_ClearWeapLerpFrame(wi, lf, newAnimation ); // clear when switching to raise (since it should be out of view anyway) + //} + + // if we have passed the current frame, move it to + // oldFrame and calculate a new frame + if ( cg.time >= lf->frameTime ) { + lf->oldFrame = lf->frame; + lf->oldFrameTime = lf->frameTime; + + // get the next frame based on the animation + anim = lf->animation; + if ( !anim->frameLerp ) { + return; // shouldn't happen + } + if ( cg.time < lf->animationTime ) { + lf->frameTime = lf->animationTime; // initial lerp + } else { + lf->frameTime = lf->oldFrameTime + anim->frameLerp; + } + f = ( lf->frameTime - lf->animationTime ) / anim->frameLerp; + f *= speedScale; // adjust for haste, etc + if ( f >= anim->numFrames ) { + f -= anim->numFrames; + if ( anim->loopFrames ) { + f %= anim->loopFrames; + f += anim->numFrames - anim->loopFrames; + } else { + f = anim->numFrames - 1; + // the animation is stuck at the end, so it + // can immediately transition to another sequence + lf->frameTime = cg.time; + } + } + lf->frame = anim->firstFrame + f; + if ( cg.time > lf->frameTime ) { + lf->frameTime = cg.time; + if ( cg_debugAnim.integer ) { +// CG_Printf( "Clamp lf->frameTime\n"); + } + } + } + + if ( lf->frameTime > cg.time + 200 ) { + lf->frameTime = cg.time; + } + + if ( lf->oldFrameTime > cg.time ) { + lf->oldFrameTime = cg.time; + } + // calculate current lerp value + if ( lf->frameTime == lf->oldFrameTime ) { + lf->backlerp = 0; + } else { + lf->backlerp = 1.0 - (float)( cg.time - lf->oldFrameTime ) / ( lf->frameTime - lf->oldFrameTime ); + } +} + + + +/* +============== +CG_WeaponAnimation +============== +*/ + +//----(SA) modified. this is now client-side only (server does not dictate weapon animation info) +static void CG_WeaponAnimation( playerState_t *ps, weaponInfo_t *weapon, int *weapOld, int *weap, float *weapBackLerp ) { + + centity_t *cent = &cg.predictedPlayerEntity; + clientInfo_t *ci = &cgs.clientinfo[ ps->clientNum ]; + + if ( cg_noPlayerAnims.integer ) { + *weapOld = *weap = 0; + return; + } + + CG_RunWeapLerpFrame( ci, weapon, ¢->pe.weap, ps->weapAnim, 1 ); + + *weapOld = cent->pe.weap.oldFrame; + *weap = cent->pe.weap.frame; + *weapBackLerp = cent->pe.weap.backlerp; + + if ( cg_debugAnim.integer == 3 ) { + CG_Printf( "oldframe: %d frame: %d backlerp: %f\n", cent->pe.weap.oldFrame, cent->pe.weap.frame, cent->pe.weap.backlerp ); + } +} + +//////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////// + + +// (SA) it wasn't used anyway + + +/* +============== +CG_CalculateWeaponPosition +============== +*/ +static void CG_CalculateWeaponPosition( vec3_t origin, vec3_t angles ) { + float scale; + int delta; + float fracsin, leanscale; + + VectorCopy( cg.refdef.vieworg, origin ); + VectorCopy( cg.refdefViewAngles, angles ); + + // adjust 'lean' into weapon + if ( cg.predictedPlayerState.leanf != 0 ) { + vec3_t right, up; + + leanscale = 1.0f; + + switch ( cg.predictedPlayerState.weapon ) { + case WP_GARAND: + leanscale = 3.0f; + break; + case WP_FLAMETHROWER: + case WP_TESLA: + case WP_MAUSER: + leanscale = 2.0f; + break; + + // never adjust + case WP_KNIFE: + case WP_GRENADE_LAUNCHER: + case WP_GRENADE_PINEAPPLE: + break; + + // adjust when leaning right (in case of reload) + default: + if ( cg.predictedPlayerState.leanf > 0 ) { + leanscale = 1.3f; + } + break; + } + + // reverse the roll on the weapon so it stays relatively level + angles[ROLL] -= cg.predictedPlayerState.leanf / ( leanscale * 2.0f ); + AngleVectors( angles, NULL, right, up ); + VectorMA( origin, angles[ROLL], right, origin ); + + // pitch the gun down a bit to show that firing is not allowed when leaning + angles[PITCH] += ( abs( cg.predictedPlayerState.leanf ) / 2.0f ); + + // this gives you some impression that the weapon stays in relatively the same + // position while you lean, so you appear to 'peek' over the weapon + AngleVectors( cg.refdefViewAngles, NULL, right, NULL ); + VectorMA( origin, -cg.predictedPlayerState.leanf / 4.0f, right, origin ); + } + + + // on odd legs, invert some angles + if ( cg.bobcycle & 1 ) { + scale = -cg.xyspeed; + } else { + scale = cg.xyspeed; + } + + // gun angles from bobbing + + angles[ROLL] += scale * cg.bobfracsin * 0.005; + angles[YAW] += scale * cg.bobfracsin * 0.01; + angles[PITCH] += cg.xyspeed * cg.bobfracsin * 0.005; + + // drop the weapon when landing + delta = cg.time - cg.landTime; + if ( delta < LAND_DEFLECT_TIME ) { + origin[2] += cg.landChange * 0.25 * delta / LAND_DEFLECT_TIME; + } else if ( delta < LAND_DEFLECT_TIME + LAND_RETURN_TIME ) { + origin[2] += cg.landChange * 0.25 * + ( LAND_DEFLECT_TIME + LAND_RETURN_TIME - delta ) / LAND_RETURN_TIME; + } + +#if 0 + // drop the weapon when stair climbing + delta = cg.time - cg.stepTime; + if ( delta < STEP_TIME / 2 ) { + origin[2] -= cg.stepChange * 0.25 * delta / ( STEP_TIME / 2 ); + } else if ( delta < STEP_TIME ) { + origin[2] -= cg.stepChange * 0.25 * ( STEP_TIME - delta ) / ( STEP_TIME / 2 ); + } +#endif + + // idle drift +//----(SA) adjustment for MAX KAUFMAN +// scale = cg.xyspeed + 40; + scale = 80; +//----(SA) end + fracsin = sin( cg.time * 0.001 ); + angles[ROLL] += scale * fracsin * 0.01; + angles[YAW] += scale * fracsin * 0.01; + angles[PITCH] += scale * fracsin * 0.01; + + // RF, subtract the kickAngles + VectorMA( angles, -1.0, cg.kickAngles, angles ); + +} + + +// Ridah +/* +=============== +CG_FlamethrowerFlame +=============== +*/ +static void CG_FlamethrowerFlame( centity_t *cent, vec3_t origin ) { + + if ( cent->currentState.weapon != WP_FLAMETHROWER ) { + return; + } + + if ( cent->currentState.number == cg.snap->ps.clientNum ) { + if ( cg.snap->ps.weapon != WP_FLAMETHROWER || ( cg.snap->ps.weaponstate != WEAPON_FIRING && cg.snap->ps.weaponstate != WEAPON_READY ) ) { + return; + } + } + +// if (cent->currentState.aiChar) +// CG_FireFlameChunks( cent, origin, cent->lerpAngles, 650.0 / FLAMETHROWER_RANGE, qtrue, 0 ); // fixed length for AI +// else + CG_FireFlameChunks( cent, origin, cent->lerpAngles, 1.0, qtrue, 0 ); + + return; +} +// done. + +/* +====================== +CG_MachinegunSpinAngle +====================== +*/ +//#define SPIN_SPEED 0.9 +//#define COAST_TIME 1000 +#define SPIN_SPEED 1 +#define COAST_TIME 2000 + +// TTimo: unused +/* +static float CG_MachinegunSpinAngle( centity_t *cent ) { + int delta; + float angle; + float speed; + + delta = cg.time - cent->pe.barrelTime; + if ( cent->pe.barrelSpinning ) { + angle = cent->pe.barrelAngle + delta * SPIN_SPEED; + } else { + if ( delta > COAST_TIME ) { + delta = COAST_TIME; + } + + speed = 0.5 * ( SPIN_SPEED + (float)( COAST_TIME - delta ) / COAST_TIME ); + angle = cent->pe.barrelAngle + delta * speed; + } + + if ( cent->pe.barrelSpinning == !(cent->currentState.eFlags & EF_FIRING) ) { + cent->pe.barrelTime = cg.time; + cent->pe.barrelAngle = AngleMod( angle ); + cent->pe.barrelSpinning = !!(cent->currentState.eFlags & EF_FIRING); + } + + return angle; +} +*/ + +/* +============== +CG_TeslaSpinAngle +============== +*/ + +//#define TESLA_SPINSPEED .2 +//#define TESLA_COASTTIME 2000 +#define TESLA_SPINSPEED .05 +#define TESLA_IDLESPEED .15 +#define TESLA_COASTTIME 1000 + +static float CG_TeslaSpinAngle( centity_t *cent ) { + int delta; + float angle; + float speed; + + delta = cg.time - cent->pe.barrelTime; + + angle = cent->pe.barrelAngle; + + if ( cent->currentState.eFlags & EF_FIRING ) { + angle += delta * TESLA_SPINSPEED; + } else { + angle += delta * TESLA_IDLESPEED; + } + + cent->pe.barrelAngle = AngleMod( angle ); + + cent->pe.barrelTime = cg.time; + + return AngleMod( angle ); + +//----(SA) trying new tesla effect scheme for MK +// angle = -(cent->pe.barrelAngle + delta * TESLA_SPINSPEED); +// cent->pe.barrelAngle = AngleMod( angle ); + +// if(cent->currentState.eFlags & EF_FIRING) +// cent->pe.barrelAngle += delta * TESLA_SPINSPEED; +// else +// cent->pe.barrelAngle += delta * TESLA_IDLESPEED; + + return AngleMod( cent->pe.barrelAngle ); + + + + + + return angle; + + + if ( cent->pe.barrelSpinning ) { + angle = -( cent->pe.barrelAngle + delta * TESLA_SPINSPEED ); + } else { + if ( delta > TESLA_COASTTIME ) { + delta = TESLA_COASTTIME; + } + + speed = 0.5 * ( TESLA_SPINSPEED + (float)( TESLA_COASTTIME - delta ) / TESLA_COASTTIME ); + angle = -( cent->pe.barrelAngle + delta * speed ); + } + + if ( cent->pe.barrelSpinning == !( cent->currentState.eFlags & EF_FIRING ) ) { + cent->pe.barrelTime = cg.time; + cent->pe.barrelAngle = AngleMod( angle ); + cent->pe.barrelSpinning = !!( cent->currentState.eFlags & EF_FIRING ); + } + + return angle; +} + + +//----(SA) added + +/* +====================== +CG_VenomSpinAngle +====================== +*/ + +#define VENOM_LOADTIME 2000 +#define VENOM_DELTATIME ( VENOM_LOADTIME / 10 ) // as there are 10 shots to be loaded + +static float CG_VenomSpinAngle( centity_t *cent ) { + int delta; + float ramp; + float angle; + float speed; + qboolean firing; + + delta = cg.time - cent->pe.barrelTime; + + ramp = delta % VENOM_DELTATIME; + + firing = (qboolean)( cent->currentState.eFlags & EF_FIRING ); + +// if((cent->pe.weap.animationNumber & ~ANIM_TOGGLEBIT) >= WEAP_DROP) + if ( cg.snap->ps.weaponstate != WEAPON_FIRING ) { // (SA) this seems better + firing = qfalse; + } + + + delta = cg.time - cent->pe.barrelTime; + if ( cent->pe.barrelSpinning ) { + angle = cent->pe.barrelAngle + delta * SPIN_SPEED; + } else { + if ( delta > COAST_TIME ) { + delta = COAST_TIME; + } + + speed = 0.5 * ( SPIN_SPEED + (float)( COAST_TIME - delta ) / COAST_TIME ); + angle = cent->pe.barrelAngle + delta * speed; + } + + if ( cent->pe.barrelSpinning == !firing ) { + cent->pe.barrelTime = cg.time; + cent->pe.barrelAngle = AngleMod( angle ); + cent->pe.barrelSpinning = !!firing; + + // just switching between not spinning and spinning, play the appropriate weapon sound + if ( cent->pe.barrelSpinning ) { + if ( cg_weapons[WP_VENOM].spinupSound ) { + trap_S_StartSoundEx( NULL, cent->currentState.number, CHAN_WEAPON, cg_weapons[WP_VENOM].spinupSound, SND_OKTOCUT ); + } + } else { + if ( cg_weapons[WP_VENOM].spindownSound ) { + trap_S_StartSound( NULL, cent->currentState.number, CHAN_WEAPON, cg_weapons[WP_VENOM].spindownSound ); + } + } + + } + + return angle; +} + + + + +/* +============== +CG_DrawRealWeapons +============== +*/ +qboolean CG_DrawRealWeapons( centity_t *cent ) { + + switch ( cent->currentState.aiChar ) { + case AICHAR_LOPER: + case AICHAR_SUPERSOLDIER: //----(SA) added + case AICHAR_PROTOSOLDIER: + case AICHAR_ZOMBIE: + case AICHAR_HELGA: //----(SA) added // boss1 is now helga-blob + case AICHAR_WARZOMBIE: + return qfalse; + } + + return qtrue; +} + + +/* +======================== +CG_AddWeaponWithPowerups +======================== +*/ +static void CG_AddWeaponWithPowerups( refEntity_t *gun, int powerups, playerState_t *ps, centity_t *cent ) { + + + // add powerup effects + if ( powerups & ( 1 << PW_INVIS ) ) { + gun->customShader = cgs.media.invisShader; + trap_R_AddRefEntityToScene( gun ); + } else { + trap_R_AddRefEntityToScene( gun ); + + if ( powerups & ( 1 << PW_BATTLESUIT ) ) { + gun->customShader = cgs.media.battleWeaponShader; + trap_R_AddRefEntityToScene( gun ); + } + if ( powerups & ( 1 << PW_QUAD ) ) { + gun->customShader = cgs.media.quadWeaponShader; + trap_R_AddRefEntityToScene( gun ); + } + } +/* + if (ps && ps->clientNum == cg.snap->ps.clientNum) { + float alpha, adjust; + weaponInfo_t *weapon; + + weapon = &cg_weapons[ps->weapon]; + //if (gun->hModel == weapon->handsModel) +// if (cg.snap->ps.onFireStart) + { + + // add the flames if on fire +// alpha = 2.0 * (float)(FIRE_FLASH_TIME - (cg.time - cg.snap->ps.onFireStart))/FIRE_FLASH_TIME; +alpha = 1; + if (alpha > 0) { + if (alpha >= 1.0) { + alpha = 1.0; + } + gun->shaderRGBA[3] = (unsigned char)(255.0*alpha); + // calc the fireRiseDir from the velocity + VectorNegate( cg.snap->ps.velocity, gun->fireRiseDir ); + VectorNormalize( gun->fireRiseDir ); + gun->fireRiseDir[2] += 1; + if (VectorNormalize( gun->fireRiseDir ) < 1) { + VectorClear( gun->fireRiseDir ); + gun->fireRiseDir[2] = 1; + } + // now move towards the newDir + adjust = 5.0*(0.001*cg.frametime); + VectorMA( cg.v_fireRiseDir, adjust, gun->fireRiseDir, cg.v_fireRiseDir ); + if (VectorNormalize( cg.v_fireRiseDir ) <= 0.1) { + VectorCopy( gun->fireRiseDir, cg.v_fireRiseDir ); + } + VectorCopy( cg.v_fireRiseDir, gun->fireRiseDir ); + +// gun->reFlags |= REFLAG_ONLYHAND; +gun->customShader = cgs.media.dripWetShader2; +// gun->customShader = cgs.media.onFireShader; + trap_R_AddRefEntityToScene( gun ); +// gun->shaderTime = 500; +// trap_R_AddRefEntityToScene( gun ); +gun->customShader = cgs.media.dripWetShader; +// gun->customShader = cgs.media.onFireShader2; + trap_R_AddRefEntityToScene( gun ); +// gun->reFlags &= ~REFLAG_ONLYHAND; + } + } + } +*/ +} + +/* +============== +CG_PlayerTeslaCoilFire + + TODO: this needs to be fixed for multiplay. entities being hurt need to be sent + by server to all clients, so they draw the correct effects. +============== +*/ +void CG_PlayerTeslaCoilFire( centity_t *cent, vec3_t flashorigin ) { + +#define TESLA_LIGHTNING_POINT_TIMEOUT 3000 +#define TESLA_LIGHTNING_MAX_DIST ( cent->currentState.aiChar == AICHAR_SUPERSOLDIER ? TESLA_SUPERSOLDIER_RANGE : TESLA_RANGE ) // use these to perhaps vary the distance according to aiming +#define TESLA_LIGHTNING_NORMAL_DIST ( TESLA_RANGE / 2.0 ) +#define TESLA_MAX_POINT_TESTS 10 +#define TESLA_MAX_POINT_TESTS_PERFRAME 20 + + int i, j, pointTests = 0; + vec3_t testPos, tagPos, vec; + trace_t tr; + float maxDist; + int numPoints; + vec3_t viewAngles, viewDir; + int visEnemies[16]; + float visDists[16]; + int visEnemiesSorted[MAX_TESLA_BOLTS]; + int numEnemies, numSorted = 0, best; // TTimo: init + float bestDist; + centity_t *ctrav; + vec3_t traceOrg; + int playerTeam; + + if ( cent->currentState.weapon != WP_TESLA ) { + return; + } + +// JPW NERVE no tesla in multiplayer + if ( cg_gameType.integer != GT_SINGLE_PLAYER ) { + return; + } + + //if (cent->currentState.number == cg.snap->ps.clientNum) + // VectorCopy( cg.snap->ps.viewangles, viewAngles ); + //else + VectorCopy( cent->lerpAngles, viewAngles ); + + AngleVectors( viewAngles, viewDir, NULL, NULL ); + + if ( cent->currentState.number == cg.snap->ps.clientNum ) { + VectorCopy( cg.snap->ps.origin, traceOrg ); + playerTeam = cg.snap->ps.teamNum; + } else { + VectorCopy( cent->lerpOrigin, traceOrg ); + playerTeam = cent->currentState.teamNum; + } + + maxDist = TESLA_LIGHTNING_MAX_DIST; + numPoints = MAX_TESLA_BOLTS; + + VectorCopy( flashorigin, tagPos ); + + // first, build a list of visible enemies that can be hurt by this tesla, then filter by distance + if ( !cent->pe.teslaDamageApplyTime || cent->pe.teslaDamageApplyTime < cg.time - 200 ) { + numEnemies = 0; + // check the local playing client + VectorSubtract( cg.snap->ps.origin, traceOrg, vec ); + VectorNormalize( vec ); + if ( ( cent != &cg_entities[cg.snap->ps.clientNum] ) && + ( cg.snap->ps.teamNum != playerTeam ) && + ( Distance( tagPos, cg.snap->ps.origin ) < TESLA_LIGHTNING_MAX_DIST ) && + ( DotProduct( viewDir, vec ) > 0.8 ) ) { + CG_Trace( &tr, traceOrg, NULL, NULL, cg.snap->ps.origin, cg.snap->ps.clientNum, MASK_SHOT & ~( CONTENTS_BODY ) ); + if ( tr.fraction == 1 || tr.entityNum == cg.snap->ps.clientNum ) { + visDists[numEnemies] = Distance( tagPos, cg.snap->ps.origin ); + visEnemies[numEnemies++] = cg.snap->ps.clientNum; + } else { // try head + VectorCopy( cg.snap->ps.origin, vec ); + vec[2] += cg.snap->ps.viewheight; + CG_Trace( &tr, tagPos, NULL, NULL, vec, cg.snap->ps.clientNum, MASK_SHOT & ~( CONTENTS_BODY ) ); + if ( tr.fraction == 1 || tr.entityNum == cg.snap->ps.clientNum ) { + visDists[numEnemies] = Distance( tagPos, cg.snap->ps.origin ); + visEnemies[numEnemies++] = cg.snap->ps.clientNum; + } else { // try body, from tag + VectorCopy( cg.snap->ps.origin, vec ); + CG_Trace( &tr, tagPos, NULL, NULL, vec, cg.snap->ps.clientNum, MASK_SHOT & ~( CONTENTS_BODY ) ); + if ( tr.fraction == 1 || tr.entityNum == cg.snap->ps.clientNum ) { + visDists[numEnemies] = Distance( tagPos, cg.snap->ps.origin ); + visEnemies[numEnemies++] = cg.snap->ps.clientNum; + } + } + } + } + + if ( cgs.localServer && cgs.gametype == GT_SINGLE_PLAYER ) { + // check for AI's getting hurt (TODO: bot support?) + for ( ctrav = cg_entities, i = 0; i < cgs.maxclients && numEnemies < 16; ctrav++, i++ ) { + // RF, proto and supersoldier are invulnerable to tesla +/* switch (ctrav->currentState.aiChar) { + case AICHAR_SUPERSOLDIER: + case AICHAR_PROTOSOLDIER: + continue; + } +*/ // + if ( ctrav->currentState.aiChar && + ( ctrav != cent ) && + ( ctrav->currentState.teamNum != playerTeam ) && + !( ctrav->currentState.eFlags & EF_DEAD ) && + ctrav->currentValid && // is in the visible frame + ( Distance( tagPos, ctrav->lerpOrigin ) < TESLA_LIGHTNING_MAX_DIST ) ) { + VectorSubtract( ctrav->lerpOrigin, traceOrg, vec ); + VectorNormalize( vec ); + + if ( DotProduct( viewDir, vec ) > 0.8 ) { + CG_Trace( &tr, traceOrg, NULL, NULL, ctrav->lerpOrigin, ctrav->currentState.number, MASK_SHOT & ~CONTENTS_BODY ); + if ( tr.fraction == 1 || tr.entityNum == ctrav->currentState.number ) { + visDists[numEnemies] = Distance( tagPos, ctrav->lerpOrigin ); + visEnemies[numEnemies++] = ctrav->currentState.number; + } + } + } + } + } + + // now sort by distance + for ( j = 0; j < MAX_TESLA_BOLTS; j++ ) { + visEnemiesSorted[j] = -1; + + bestDist = 99999; + best = -1; + for ( i = 0; i < numEnemies; i++ ) { + if ( visEnemies[i] < 0 ) { + continue; + } + if ( visDists[i] < bestDist ) { + bestDist = visDists[i]; + visEnemiesSorted[j] = visEnemies[i]; + best = i; + } + } + + if ( best >= 0 ) { + visEnemies[best] = -1; + numSorted = j + 1; + } + } + + // now fill in the teslaEnemy[]'s + for ( i = 0; i < MAX_TESLA_BOLTS; i++ ) { + if ( numSorted && i / numSorted < 1 /*(MAX_TESLA_BOLTS/3)*/ ) { // bolts per enemy + j = i % numSorted; + cent->pe.teslaEnemy[i] = visEnemiesSorted[j]; + // apply damage + CG_ClientDamage( visEnemiesSorted[j], cent->currentState.number, CLDMG_TESLA ); + // show the effect + cg_entities[ visEnemiesSorted[j] ].pe.teslaDamagedTime = cg.time; + } else { + if ( cent->pe.teslaEnemy[i] >= 0 ) { + cent->pe.teslaEndPointTimes[i] = 0; // make sure we find a new spot + } + cent->pe.teslaEnemy[i] = -1; + } + } + cent->pe.teslaDamageApplyTime = cg.time; + } + + for ( i = 0; i < numPoints; i++ ) { + + //if (!(rand()%3)) + // continue; + + VectorSubtract( cent->pe.teslaEndPoints[i], tagPos, vec ); + VectorNormalize( vec ); + + // if this point has timed out, find a new spot + if ( cent->pe.teslaEnemy[i] >= 0 ) { + // attacking the player + VectorSet( testPos, 6 * crandom(), + 6 * crandom(), + 20 * crandom() - 8 ); + //VectorClear( testPos ); + if ( cent->pe.teslaEnemy[i] != cg.snap->ps.clientNum ) { + VectorAdd( testPos, cg_entities[cent->pe.teslaEnemy[i]].lerpOrigin, testPos ); + } else { + VectorAdd( testPos, cg.snap->ps.origin, testPos ); + } + cent->pe.teslaEndPointTimes[i] = cg.time; // - rand()%(TESLA_LIGHTNING_POINT_TIMEOUT/2); + VectorCopy( testPos, cent->pe.teslaEndPoints[i] ); + } else if ( ( !cent->pe.teslaEndPointTimes[i] ) || + ( cent->pe.teslaEndPointTimes[i] > cg.time ) || + ( cent->pe.teslaEndPointTimes[i] < cg.time - TESLA_LIGHTNING_POINT_TIMEOUT ) || + ( VectorDistance( tagPos, cent->pe.teslaEndPoints[i] ) > maxDist ) || + ( DotProduct( viewDir, vec ) < 0.7 ) ) { + + //if (cent->currentState.groundEntityNum == ENTITYNUM_NONE) + // continue; // must be on the ground + + // find a new spot + for ( j = 0; j < TESLA_MAX_POINT_TESTS; j++ ) { + VectorSet( testPos, cg.refdef.fov_y * crandom() * 0.5, + cg.refdef.fov_x * crandom() * 0.5, + 0 ); + VectorAdd( viewAngles, testPos, testPos ); + AngleVectors( testPos, vec, NULL, NULL ); + VectorMA( tagPos, TESLA_LIGHTNING_NORMAL_DIST, vec, testPos ); + // try a trace to find a world collision + CG_Trace( &tr, tagPos, NULL, NULL, testPos, cent->currentState.number, MASK_SHOT & ~CONTENTS_BODY ); + if ( tr.fraction < 1 && tr.entityNum == ENTITYNUM_WORLD && !( tr.surfaceFlags & ( SURF_NOIMPACT | SURF_SKY ) ) ) { + // found a valid spot! + cent->pe.teslaEndPointTimes[i] = cg.time - rand() % ( TESLA_LIGHTNING_POINT_TIMEOUT / 2 ); + VectorCopy( tr.endpos, cent->pe.teslaEndPoints[i] ); + break; + } + if ( pointTests++ > TESLA_MAX_POINT_TESTS_PERFRAME ) { + j = TESLA_MAX_POINT_TESTS; + continue; + } + } + if ( j == TESLA_MAX_POINT_TESTS ) { + continue; // just don't draw this point + } + + // add an impact mark on the wall + VectorSubtract( cent->pe.teslaEndPoints[i], tagPos, vec ); + VectorNormalize( vec ); + VectorInverse( vec ); + CG_ImpactMark( cgs.media.lightningHitWallShader, cent->pe.teslaEndPoints[i], vec, random() * 360, 0.2, 0.2, 0.2, 1.0, qtrue, 4, qfalse, 300 ); + } + // + // we have a valid lightning point, so draw it + // sanity check though to make sure it's valid + if ( VectorDistance( tagPos, cent->pe.teslaEndPoints[i] ) <= maxDist ) { + CG_DynamicLightningBolt( cgs.media.lightningBoltShader, tagPos, cent->pe.teslaEndPoints[i], 1 + ( ( cg.time % ( ( i + 2 ) * ( i + 3 ) ) ) + i ) % 2, 20 + (float)( i % 3 ) * 5 + 6.0 * random(), ( cent->pe.teslaEnemy[i] < 0 ), 1.0, 0, i * i * 3 ); + + // play a zap sound + if ( cent->pe.lightningSoundTime < cg.time - 200 ) { + CG_SoundPlayIndexedScript( cgs.media.teslaZapScript, cent->pe.teslaEndPoints[i], ENTITYNUM_WORLD ); + CG_SoundPlayIndexedScript( cgs.media.teslaZapScript, cent->lerpOrigin, ENTITYNUM_WORLD ); + //trap_S_StartSound( cent->pe.teslaEndPoints[i], ENTITYNUM_WORLD, CHAN_AUTO, cgs.media.lightningSounds[rand()%3] ); + cent->pe.lightningSoundTime = cg.time + rand() % 200; + } + } + } + + if ( cg.time % 3 ) { // break it up a bit + // add the looping sound + trap_S_AddLoopingSound( cent->currentState.number, cent->lerpOrigin, vec3_origin, cgs.media.teslaLoopSound, 255 ); + } + + // drop a dynamic light out infront of us + AngleVectors( viewAngles, vec, NULL, NULL ); + VectorMA( tagPos, 300, vec, testPos ); + // try a trace to find a world collision + CG_Trace( &tr, tagPos, NULL, NULL, testPos, cent->currentState.number, MASK_SOLID ); + + if ( ( cg.time / 50 ) % ( 4 + ( cg.time % 4 ) ) == 0 ) { + // alt light + trap_R_AddLightToScene( tr.endpos, 256 + 600 * tr.fraction, 0.2, 0.6, 1, 1 ); + } else if ( ( cg.time / 50 ) % ( 4 + ( cg.time % 4 ) ) == 1 ) { + // no light + //trap_R_AddLightToScene( tr.endpos, 128 + 500*tr.fraction, 1, 1, 1, 10 ); + } else { + // blue light + trap_R_AddLightToScene( tr.endpos, 256 + 600 * tr.fraction, 0.2, 0.6, 1, 0 ); + } + + + // shake the camera a bit + CG_StartShakeCamera( 0.05, 200, cent->lerpOrigin, 100 ); + +} + +//----(SA) +/* +============== +CG_AddProtoWeapons +============== +*/ +void CG_AddProtoWeapons( refEntity_t *parent, playerState_t *ps, centity_t *cent ) { + refEntity_t gun; + + return; + + if ( !( cent->currentState.aiChar == AICHAR_PROTOSOLDIER ) ) { + return; + } + memset( &gun, 0, sizeof( gun ) ); + VectorCopy( parent->lightingOrigin, gun.lightingOrigin ); +// gun.hModel = cgs.media.protoWeapon; + gun.shadowPlane = parent->shadowPlane; + gun.renderfx = parent->renderfx; + CG_PositionEntityOnTag( &gun, parent, "tag_armright", 0, NULL ); + CG_AddWeaponWithPowerups( &gun, cent->currentState.powerups, ps, cent ); +} +//----(SA) end + + + +// Ridah +/* +============== +CG_MonsterUsingWeapon +============== +*/ +qboolean CG_MonsterUsingWeapon( centity_t *cent, int aiChar, int weaponNum ) { + return ( cent->currentState.aiChar == aiChar ) && ( cent->currentState.weapon == weaponNum ); +} + + + + + +/* +============= +CG_AddPlayerWeapon + +Used for both the view weapon (ps is valid) and the world modelother character models (ps is NULL) +The main player will have this called for BOTH cases, so effects like light and +sound should only be done on the world model case. +============= +*/ +static qboolean debuggingweapon = qfalse; + +void CG_AddPlayerWeapon( refEntity_t *parent, playerState_t *ps, centity_t *cent ) { + + refEntity_t gun; + refEntity_t barrel; + refEntity_t flash; + vec3_t angles; + weapon_t weaponNum, weapSelect; + weaponInfo_t *weapon; + centity_t *nonPredictedCent; + qboolean firing; // Ridah + + qboolean akimboFire = qfalse; //----(SA) added + + qboolean playerScaled; + qboolean drawpart, drawrealweap; + int i; + qboolean isPlayer; + + // (SA) might as well have this check consistant throughout the routine + isPlayer = (qboolean)( cent->currentState.clientNum == cg.snap->ps.clientNum ); + + weaponNum = cent->currentState.weapon; + weapSelect = cg.weaponSelect; + + if ( ps && cg.cameraMode ) { + return; + } + + // don't draw any weapons when the binocs are up + if ( cent->currentState.eFlags & EF_ZOOMING ) { + if ( isPlayer ) { + if ( !cg.renderingThirdPerson ) { + return; + } + } else { + return; + } + } + + // don't draw weapon stuff when looking through a scope + if ( weaponNum == WP_SNOOPERSCOPE || weaponNum == WP_SNIPERRIFLE || weaponNum == WP_FG42SCOPE || + weapSelect == WP_SNOOPERSCOPE || weapSelect == WP_SNIPERRIFLE || weapSelect == WP_FG42SCOPE ) { + if ( isPlayer && !cg.renderingThirdPerson ) { + return; + } + } + + // no weapon when on mg_42 + if ( cent->currentState.eFlags & EF_MG42_ACTIVE ) { + return; + } + + // DHM - Nerve :: Special case for WP_CLASS_SPECIAL + if ( cgs.gametype == GT_WOLF && weaponNum == WP_CLASS_SPECIAL ) { + switch ( cent->currentState.teamNum ) { + case PC_ENGINEER: + CG_RegisterWeapon( WP_CLASS_SPECIAL ); + weapon = &cg_weapons[WP_CLASS_SPECIAL]; + break; + case PC_MEDIC: + CG_RegisterWeapon( WP_MEDIC_HEAL ); + weapon = &cg_weapons[WP_MEDIC_HEAL]; + break; + case PC_LT: + CG_RegisterWeapon( WP_GRENADE_SMOKE ); + weapon = &cg_weapons[WP_GRENADE_SMOKE]; + break; + default: + CG_RegisterWeapon( weaponNum ); + weapon = &cg_weapons[weaponNum]; + break; + } + } else { + CG_RegisterWeapon( weaponNum ); + weapon = &cg_weapons[weaponNum]; + } + // dhm - end + + + if ( isPlayer ) { + akimboFire = BG_AkimboFireSequence( weaponNum, cg.predictedPlayerState.ammoclip[WP_AKIMBO], cg.predictedPlayerState.ammoclip[WP_COLT] ); + } else if ( ps ) { + akimboFire = BG_AkimboFireSequence( weaponNum, ps->ammoclip[WP_AKIMBO], ps->ammoclip[WP_AKIMBO] ); + } + + // add the weapon + memset( &gun, 0, sizeof( gun ) ); + VectorCopy( parent->lightingOrigin, gun.lightingOrigin ); + gun.shadowPlane = parent->shadowPlane; + gun.renderfx = parent->renderfx; + + // set custom shading for railgun refire rate + if ( ps ) { + gun.shaderRGBA[0] = 255; + gun.shaderRGBA[1] = 255; + gun.shaderRGBA[2] = 255; + gun.shaderRGBA[3] = 255; + } + + if ( ps ) { + gun.hModel = weapon->weaponModel[W_FP_MODEL]; + } else { + CG_AddProtoWeapons( parent, ps, cent ); + // skeletal guys use a different third person weapon (for different tag business) + if ( cgs.clientinfo[ cent->currentState.clientNum ].isSkeletal && weapon->weaponModel[W_SKTP_MODEL] ) { + gun.hModel = weapon->weaponModel[W_SKTP_MODEL]; + } else { + gun.hModel = weapon->weaponModel[W_TP_MODEL]; + } + } + + if ( !gun.hModel ) { + if ( debuggingweapon ) { + CG_Printf( "returning due to: !gun.hModel\n" ); + } + return; + } + + if ( weaponNum == WP_GAUNTLET ) { // (SA) this is the 'knife'. no model yet, so we can give it to the zombie and have him visually 'unarmed' + if ( debuggingweapon ) { + CG_Printf( "returning due to: weaponNum == WP_GAUNTLET\n" ); + } + return; + } + + if ( !ps && cg.snap->ps.pm_flags & PMF_LADDER && isPlayer ) { //----(SA) player on ladder + if ( debuggingweapon ) { + CG_Printf( "returning due to: !ps && cg.snap->ps.pm_flags & PMF_LADDER\n" ); + } + return; + } + + + if ( !ps ) { + // add weapon ready sound + cent->pe.lightningFiring = qfalse; + if ( ( cent->currentState.eFlags & EF_FIRING ) && weapon->firingSound ) { + // lightning gun and guantlet make a different sound when fire is held down + trap_S_AddLoopingSound( cent->currentState.number, cent->lerpOrigin, vec3_origin, weapon->firingSound, 255 ); + cent->pe.lightningFiring = qtrue; + } else if ( weapon->readySound ) { + trap_S_AddLoopingSound( cent->currentState.number, cent->lerpOrigin, vec3_origin, weapon->readySound, 255 ); + } + } + + + // Ridah + firing = ( ( cent->currentState.eFlags & EF_FIRING ) != 0 ); + + CG_PositionEntityOnTag( &gun, parent, "tag_weapon", 0, NULL ); + + playerScaled = (qboolean)( cgs.clientinfo[ cent->currentState.clientNum ].playermodelScale[0] != 0 ); + if ( !ps && playerScaled ) { // don't "un-scale" weap up in 1st person + for ( i = 0; i < 3; i++ ) { // scale weapon back up so it doesn't pick up the adjusted scale of the character models. + // this will affect any parts attached to the gun as well (barrel/bolt/flash/brass/etc.) + VectorScale( gun.axis[i], 1.0 / ( cgs.clientinfo[ cent->currentState.clientNum ].playermodelScale[i] ), gun.axis[i] ); + } + + } + + // characters that draw their own special weapon model will not draw the standard ones + if ( CG_DrawRealWeapons( cent ) ) { + drawrealweap = qtrue; + } else { + drawrealweap = qfalse; + } + + if ( ps ) { + drawpart = CG_GetPartFramesFromWeap( cent, &gun, parent, W_MAX_PARTS, weapon ); // W_MAX_PARTS specifies this as the primary view model + } else { + drawpart = qtrue; + } + + if ( drawpart && drawrealweap ) { + CG_AddWeaponWithPowerups( &gun, cent->currentState.powerups, ps, cent ); + } + + if ( isPlayer ) { + refEntity_t brass; + + // opposite tag in akimbo, since at this point the weapon + // has fired and the fire seq has switched over + if ( weaponNum == WP_AKIMBO && akimboFire ) { + CG_PositionRotatedEntityOnTag( &brass, &gun, "tag_brass2" ); + } else { + CG_PositionRotatedEntityOnTag( &brass, &gun, "tag_brass" ); + } + + VectorCopy( brass.origin, ejectBrassCasingOrigin ); + } + + memset( &barrel, 0, sizeof( barrel ) ); + VectorCopy( parent->lightingOrigin, barrel.lightingOrigin ); + barrel.shadowPlane = parent->shadowPlane; + barrel.renderfx = parent->renderfx; + + // add barrels + // attach generic weapon parts to the first person weapon. + // if a barrel should be attached for third person, add it in the (!ps) section below + angles[YAW] = angles[PITCH] = 0; + + if ( ps ) { + qboolean spunpart; + + for ( i = W_PART_1; i < W_MAX_PARTS; i++ ) { + + spunpart = qfalse; + barrel.hModel = weapon->wpPartModels[W_FP_MODEL][i]; + + // check for spinning + if ( weaponNum == WP_VENOM ) { + if ( i == W_PART_1 ) { + angles[ROLL] = CG_VenomSpinAngle( cent ); + spunpart = qtrue; + } else if ( i == W_PART_2 ) { + angles[ROLL] = -CG_VenomSpinAngle( cent ); + spunpart = qtrue; + } + // 'blurry' barel when firing + // (SA) not right now. at the moment, just spin the belt when firing, no swapout + else if ( i == W_PART_3 ) { + if ( ( cent->pe.weap.animationNumber & ~ANIM_TOGGLEBIT ) == WEAP_ATTACK1 ) { +// barrel.hModel = weapon->wpPartModels[W_FP_MODEL_SWAP][i]; + barrel.hModel = weapon->wpPartModels[W_FP_MODEL][i]; + angles[ROLL] = -CG_VenomSpinAngle( cent ); + angles[ROLL] = -( angles[ROLL] / 8.0f ); + } else { + angles[ROLL] = 0; + } + spunpart = qtrue; + } + + } else if ( weaponNum == WP_TESLA ) { + if ( i == W_PART_1 || i == W_PART_2 ) { + angles[ROLL] = CG_TeslaSpinAngle( cent ); + spunpart = qtrue; + } + } + + if ( spunpart ) { + AnglesToAxis( angles, barrel.axis ); + } + // end spinning + + + if ( barrel.hModel ) { + if ( i == W_PART_1 ) { + if ( spunpart ) { + CG_PositionRotatedEntityOnTag( &barrel, parent, "tag_barrel" ); + } else { CG_PositionEntityOnTag( &barrel, parent, "tag_barrel", 0, NULL );} + } else { + if ( spunpart ) { + CG_PositionRotatedEntityOnTag( &barrel, parent, va( "tag_barrel%d", i + 1 ) ); + } else { CG_PositionEntityOnTag( &barrel, parent, va( "tag_barrel%d", i + 1 ), 0, NULL );} + } + + drawpart = CG_GetPartFramesFromWeap( cent, &barrel, parent, i, weapon ); + + if ( drawpart && drawrealweap ) { + CG_AddWeaponWithPowerups( &barrel, cent->currentState.powerups, ps, cent ); + } + } + } + } else { // weapons with barrels drawn in third person + if ( drawrealweap ) { + if ( weaponNum == WP_VENOM ) { + + angles[ROLL] = CG_VenomSpinAngle( cent ); + AnglesToAxis( angles, barrel.axis ); + + barrel.hModel = weapon->wpPartModels[W_TP_MODEL][W_PART_1]; + CG_PositionRotatedEntityOnTag( &barrel, &gun, "tag_barrel" ); + CG_AddWeaponWithPowerups( &barrel, cent->currentState.powerups, ps, cent ); + } + } + } + + // add the scope model to the rifle if you've got it + if ( isPlayer && !cg.renderingThirdPerson ) { // (SA) for now just do it on the first person weapons + if ( weaponNum == WP_MAUSER ) { + if ( COM_BitCheck( cg.predictedPlayerState.weapons, WP_SNIPERRIFLE ) ) { + barrel.hModel = weapon->modModel[0]; + if ( barrel.hModel ) { + CG_PositionEntityOnTag( &barrel, &gun, "tag_scope", 0, NULL ); + CG_AddWeaponWithPowerups( &barrel, cent->currentState.powerups, ps, cent ); + } + } + } + } + + + // make sure we aren't looking at cg.predictedPlayerEntity for LG + nonPredictedCent = &cg_entities[cent->currentState.clientNum]; + + // if the index of the nonPredictedCent is not the same as the clientNum + // then this is a fake player (like on the single player podiums), so + // go ahead and use the cent + if ( ( nonPredictedCent - cg_entities ) != cent->currentState.clientNum ) { + nonPredictedCent = cent; + } + + + // add the flash + memset( &flash, 0, sizeof( flash ) ); + VectorCopy( parent->lightingOrigin, flash.lightingOrigin ); + flash.shadowPlane = parent->shadowPlane; + flash.renderfx = parent->renderfx; + + if ( ps ) { + flash.hModel = weapon->flashModel[W_FP_MODEL]; + } else { + flash.hModel = weapon->flashModel[W_TP_MODEL]; + } + + angles[YAW] = 0; + angles[PITCH] = 0; + angles[ROLL] = crandom() * 10; + AnglesToAxis( angles, flash.axis ); + + CG_PositionRotatedEntityOnTag( &flash, &gun, "tag_flash" ); + + // store this position for other cgame elements to access + cent->pe.gunRefEnt = gun; + cent->pe.gunRefEntFrame = cg.clientFrame; + + if ( ( weaponNum == WP_FLAMETHROWER || weaponNum == WP_TESLA ) && ( nonPredictedCent->currentState.eFlags & EF_FIRING ) ) { + // continuous flash + + } else { + + // continuous smoke after firing +#define BARREL_SMOKE_TIME 1000 + + if ( ps || cg.renderingThirdPerson || !isPlayer ) { + if ( weaponNum == WP_VENOM || weaponNum == WP_STEN ) { + if ( !cg_paused.integer ) { // don't add while paused + // hot smoking gun + if ( cg.time - cent->overheatTime < 3000 ) { + if ( !( rand() % 3 ) ) { + float alpha; + alpha = 1.0f - ( (float)( cg.time - cent->overheatTime ) / 3000.0f ); + alpha *= 0.25f; // .25 max alpha + if ( weaponNum == WP_VENOM ) { // silly thing that makes the smoke off the venom swirlier since it's spinning real fast + CG_ParticleImpactSmokePuffExtended( cgs.media.smokeParticleShader, flash.origin, tv( 0,0,1 ), 8, 1000, 8, 20, 70, alpha ); + } else { + CG_ParticleImpactSmokePuffExtended( cgs.media.smokeParticleShader, flash.origin, tv( 0,0,1 ), 8, 1000, 8, 20, 30, alpha ); + } + } + } + } + + } else if ( weaponNum == WP_PANZERFAUST ) { + if ( !cg_paused.integer ) { // don't add while paused + if ( cg.time - cent->muzzleFlashTime < BARREL_SMOKE_TIME ) { + if ( !( rand() % 5 ) ) { + float alpha; + alpha = 1.0f - ( (float)( cg.time - cent->muzzleFlashTime ) / (float)BARREL_SMOKE_TIME ); // what fraction of BARREL_SMOKE_TIME are we at + alpha *= 0.25f; // .25 max alpha + CG_ParticleImpactSmokePuffExtended( cgs.media.smokeParticleShader, flash.origin, tv( 0,0,1 ), 8, 1000, 8, 20, 30, alpha ); + } + } + } + } + } + + // impulse flash + if ( cg.time - cent->muzzleFlashTime > MUZZLE_FLASH_TIME ) { + // Ridah, blue ignition flame if not firing flamer + if ( weaponNum != WP_FLAMETHROWER && weaponNum != WP_TESLA ) { + return; + } + } + + } + + // weapons that don't need to go any further as they have no flash or light + if ( weaponNum == WP_GRENADE_LAUNCHER || + weaponNum == WP_GRENADE_PINEAPPLE || + weaponNum == WP_KNIFE || + weaponNum == WP_DYNAMITE ) { + return; + } + + if ( weaponNum == WP_STEN ) { // sten has no muzzleflash + flash.hModel = 0; + } + + // weaps with barrel smoke + if ( ps || cg.renderingThirdPerson || !isPlayer ) { + if ( !cg_paused.integer ) { // don't add while paused + if ( weaponNum == WP_STEN || weaponNum == WP_VENOM ) { + if ( cg.time - cent->muzzleFlashTime < 100 ) { +// CG_ParticleImpactSmokePuff (cgs.media.smokeParticleShader, flash.origin); + CG_ParticleImpactSmokePuffExtended( cgs.media.smokeParticleShader, flash.origin, tv( 0,0,1 ), 8, 500, 8, 20, 30, 0.25f ); + } + } + } + } + + if ( isPlayer ) { + if ( weaponNum == WP_AKIMBO ) { + if ( !cent->akimboFire ) { + CG_PositionRotatedEntityOnTag( &flash, &gun, "tag_flash2" ); + } + } + } + + + if ( flash.hModel ) { + if ( weaponNum != WP_FLAMETHROWER && weaponNum != WP_TESLA ) { //Ridah, hide the flash also for now + // RF, changed this so the muzzle flash stays onscreen for long enough to be seen + if ( cg.time - cent->muzzleFlashTime < MUZZLE_FLASH_TIME ) { +// if (firing) { // Ridah + trap_R_AddRefEntityToScene( &flash ); + } + } + } + + // Ridah, zombie fires from his head + //if (CG_MonsterUsingWeapon( cent, AICHAR_ZOMBIE, WP_MONSTER_ATTACK1 )) { + // CG_PositionEntityOnTag( &flash, parent, parent->hModel, "tag_head", NULL); + //} + + if ( ps || cg.renderingThirdPerson || !isPlayer ) { + + if ( firing ) { + // Ridah, Flamethrower effect + CG_FlamethrowerFlame( cent, flash.origin ); + + // RF, Tesla coil + CG_PlayerTeslaCoilFire( cent, flash.origin ); + + // make a dlight for the flash + if ( weapon->flashDlightColor[0] || weapon->flashDlightColor[1] || weapon->flashDlightColor[2] ) { + trap_R_AddLightToScene( flash.origin, 200 + ( rand() & 31 ), weapon->flashDlightColor[0], + weapon->flashDlightColor[1], weapon->flashDlightColor[2], 0 ); + } + } else { + if ( weaponNum == WP_FLAMETHROWER ) { + vec3_t angles; + AxisToAngles( flash.axis, angles ); + CG_FireFlameChunks( cent, flash.origin, angles, 1.0, qfalse, 0 ); + } + } + } +} + +void CG_AddPlayerFoot( refEntity_t *parent, playerState_t *ps, centity_t *cent ) { + refEntity_t wolfkick; + vec3_t kickangle; + weaponInfo_t *weapon; + weapon_t weaponNum; + int frame; + static int oldtime = 0; + + if ( !( cg.snap->ps.persistant[PERS_WOLFKICK] ) ) { + oldtime = 0; + return; + } + + weaponNum = cent->currentState.weapon; + weapon = &cg_weapons[weaponNum]; + + memset( &wolfkick, 0, sizeof( wolfkick ) ); + + VectorCopy( parent->lightingOrigin, wolfkick.lightingOrigin ); + wolfkick.shadowPlane = parent->shadowPlane; + + // note to self we want this to lerp and advance frame + wolfkick.renderfx = RF_DEPTHHACK | RF_FIRST_PERSON;; + wolfkick.hModel = wolfkickModel; + + VectorCopy( cg.refdef.vieworg, wolfkick.origin ); + //----(SA) allow offsets for testing boot model + if ( cg_gun_x.value ) { + VectorMA( wolfkick.origin, cg_gun_x.value, cg.refdef.viewaxis[0], wolfkick.origin ); + } + if ( cg_gun_y.value ) { + VectorMA( wolfkick.origin, cg_gun_y.value, cg.refdef.viewaxis[1], wolfkick.origin ); + } + if ( cg_gun_z.value ) { + VectorMA( wolfkick.origin, cg_gun_z.value, cg.refdef.viewaxis[2], wolfkick.origin ); + } + //----(SA) end + + + VectorCopy( cg.refdefViewAngles, kickangle ); + if ( kickangle[0] < 0 ) { + kickangle[0] = 0; //----(SA) avoid "Rockette" syndrome :) + } + AnglesToAxis( kickangle, wolfkick.axis ); + + + frame = cg.snap->ps.persistant[PERS_WOLFKICK]; + +// CG_Printf("frame: %d\n", frame); + + wolfkick.frame = frame; + wolfkick.oldframe = frame - 1; + wolfkick.backlerp = 1 - cg.frameInterpolation; + trap_R_AddRefEntityToScene( &wolfkick ); + +} + +/* +============== +CG_AddViewWeapon + +Add the weapon, and flash for the player's view +============== +*/ +void CG_AddViewWeapon( playerState_t *ps ) { + refEntity_t hand; + float fovOffset; + vec3_t angles; + vec3_t gunoff; + weaponInfo_t *weapon; + + if ( ps->persistant[PERS_TEAM] == TEAM_SPECTATOR ) { + return; + } + + if ( ps->pm_type == PM_INTERMISSION ) { + return; + } + + // no gun if in third person view + if ( cg.renderingThirdPerson ) { + return; + } + + // allow the gun to be completely removed + if ( !cg_drawGun.integer ) { +/* + vec3_t origin; + + if ( cg.predictedPlayerState.eFlags & EF_FIRING ) { + // special hack for lightning gun... + VectorCopy( cg.refdef.vieworg, origin ); + VectorMA( origin, -8, cg.refdef.viewaxis[2], origin ); + CG_LightningBolt( &cg_entities[ps->clientNum], origin ); + } +*/ + return; + } + + // don't draw if testing a gun model + if ( cg.testGun ) { + return; + } + + if ( ps->eFlags & EF_MG42_ACTIVE ) { + return; + } + + + // drop gun lower at higher fov + if ( cg_fov.integer > 90 ) { + fovOffset = -0.2 * ( cg_fov.integer - 90 ); + } else { + fovOffset = 0; + } + + if ( ps->weapon > WP_NONE ) { + // DHM - Nerve :: handle WP_CLASS_SPECIAL for different classes + if ( cgs.gametype == GT_WOLF && ps->weapon == WP_CLASS_SPECIAL ) { + switch ( ps->stats[ STAT_PLAYER_CLASS ] ) { + case PC_ENGINEER: + CG_RegisterWeapon( WP_CLASS_SPECIAL ); + weapon = &cg_weapons[ WP_CLASS_SPECIAL ]; + break; + case PC_MEDIC: + CG_RegisterWeapon( WP_MEDIC_HEAL ); + weapon = &cg_weapons[ WP_MEDIC_HEAL ]; + break; + case PC_LT: + CG_RegisterWeapon( WP_GRENADE_SMOKE ); + weapon = &cg_weapons[ WP_GRENADE_SMOKE ]; + break; + default: + CG_RegisterWeapon( ps->weapon ); + weapon = &cg_weapons[ ps->weapon ]; + break; + } + } else { + CG_RegisterWeapon( ps->weapon ); + weapon = &cg_weapons[ ps->weapon ]; + } + // dhm - end + + memset( &hand, 0, sizeof( hand ) ); + + // set up gun position + CG_CalculateWeaponPosition( hand.origin, angles ); + + gunoff[0] = cg_gun_x.value; + gunoff[1] = cg_gun_y.value; + gunoff[2] = cg_gun_z.value; + +//----(SA) removed + + VectorMA( hand.origin, gunoff[0], cg.refdef.viewaxis[0], hand.origin ); + VectorMA( hand.origin, gunoff[1], cg.refdef.viewaxis[1], hand.origin ); + VectorMA( hand.origin, ( gunoff[2] + fovOffset ), cg.refdef.viewaxis[2], hand.origin ); + + AnglesToAxis( angles, hand.axis ); + + if ( cg_gun_frame.integer ) { + hand.frame = hand.oldframe = cg_gun_frame.integer; + hand.backlerp = 0; + } else { // get the animation state + CG_WeaponAnimation( ps, weapon, &hand.oldframe, &hand.frame, &hand.backlerp ); //----(SA) changed + } + + + hand.hModel = weapon->handsModel; + hand.renderfx = RF_DEPTHHACK | RF_FIRST_PERSON | RF_MINLIGHT; //----(SA) + + // add everything onto the hand + CG_AddPlayerWeapon( &hand, ps, &cg.predictedPlayerEntity ); + // Ridah + + } // end "if ( ps->weapon > WP_NONE)" + + // Rafael + // add the foot + CG_AddPlayerFoot( &hand, ps, &cg.predictedPlayerEntity ); + + cg.predictedPlayerEntity.lastWeaponClientFrame = cg.clientFrame; +} + +/* +============================================================================== + +WEAPON SELECTION + +============================================================================== +*/ + +#define WP_ICON_X 38 // new sizes per MK +#define WP_ICON_X_WIDE 72 // new sizes per MK +#define WP_ICON_Y 38 +#define WP_ICON_SPACE_Y 10 +#define WP_DRAW_X 640 - WP_ICON_X - 4 // 4 is 'selected' border width +#define WP_DRAW_X_WIDE 640 - WP_ICON_X_WIDE - 4 +#define WP_DRAW_Y 4 + +// secondary fire icons +#define WP_ICON_SEC_X 18 // new sizes per MK +#define WP_ICON_SEC_Y 18 + + +/* +=================== +CG_DrawWeaponSelect +=================== +*/ +void CG_DrawWeaponSelect( void ) { + int i; + int x, y; + int curweap, curweapbank = 0, curweapcycle = 0, drawweap; + int realweap; // DHM - Nerve + int bits[MAX_WEAPONS / ( sizeof( int ) * 8 )]; + float *color; + + // don't display if dead + if ( cg.predictedPlayerState.stats[STAT_HEALTH] <= 0 ) { + return; + } + + if ( !cg.weaponSelect ) { + return; + } + + color = CG_FadeColor( cg.weaponSelectTime, WEAPON_SELECT_TIME ); + if ( !color ) { + return; + } + trap_R_SetColor( color ); + + +//----(SA) neither of these overlap the weapon selection area anymore, so let them stay + // showing weapon select clears pickup item display, but not the blend blob +// cg.itemPickupTime = 0; + + // also clear holdable list +// cg.holdableSelectTime = 0; +//----(SA) end + + // count the number of weapons owned + memcpy( bits, cg.snap->ps.weapons, sizeof( bits ) ); + + curweap = cg.weaponSelect; + + // get bank/cycle of current weapon + if ( !CG_WeaponIndex( curweap, &curweapbank, &curweapcycle ) ) { + + // weapon selected isn't a primary weapon, so draw the alternates bank + CG_WeaponIndex( getAltWeapon( curweap ), &curweapbank, &curweapcycle ); + + } + + y = WP_DRAW_Y; + + for ( i = 0; i < maxWeapsInBank; i++ ) { + + qboolean wideweap; // is the icon one of the double width ones + + // primary fire +// JPW NERVE + if ( cg_gameType.integer == GT_WOLF ) { + drawweap = weapBanksMultiPlayer[curweapbank][i]; + } else { +// jpw + drawweap = weapBanks[curweapbank][i]; + } + + realweap = drawweap; // DHM - Nerve + + switch ( drawweap ) { + case WP_THOMPSON: + case WP_MP40: + case WP_STEN: + case WP_MAUSER: + case WP_GARAND: + case WP_VENOM: + case WP_TESLA: + case WP_PANZERFAUST: + case WP_FLAMETHROWER: + case WP_FG42: + case WP_FG42SCOPE: + wideweap = qtrue; + break; + default: + wideweap = qfalse; + break; + } + + if ( wideweap ) { + x = WP_DRAW_X_WIDE; + } else { + x = WP_DRAW_X; + } + + if ( drawweap && ( bits[0] & ( 1 << drawweap ) ) ) { + // you've got it, draw it + + // DHM - Nerve :: Special case for WP_CLASS_SPECIAL + if ( cgs.gametype == GT_WOLF && drawweap == WP_CLASS_SPECIAL ) { + switch ( cg.predictedPlayerState.stats[ STAT_PLAYER_CLASS ] ) { + case PC_ENGINEER: + drawweap = WP_CLASS_SPECIAL; + break; + case PC_MEDIC: + drawweap = WP_MEDIC_HEAL; + break; + case PC_LT: + drawweap = WP_GRENADE_SMOKE; + break; + default: + break; + } + } + // dhm - end + + CG_RegisterWeapon( drawweap ); + + if ( wideweap ) { + // weapon icon + if ( realweap == curweap ) { + CG_DrawPic( x, y, WP_ICON_X_WIDE, WP_ICON_Y, cg_weapons[drawweap].weaponIcon[1] ); + } else { + CG_DrawPic( x, y, WP_ICON_X_WIDE, WP_ICON_Y, cg_weapons[drawweap].weaponIcon[0] ); + } + + // no ammo cross + if ( !CG_WeaponHasAmmo( realweap ) ) { // DHM - Nerve + CG_DrawPic( x, y, WP_ICON_X_WIDE, WP_ICON_Y, cgs.media.noammoShader ); + } + } else { + // weapon icon + if ( realweap == curweap ) { + CG_DrawPic( x, y, WP_ICON_X, WP_ICON_Y, cg_weapons[drawweap].weaponIcon[1] ); + } else { + CG_DrawPic( x, y, WP_ICON_X, WP_ICON_Y, cg_weapons[drawweap].weaponIcon[0] ); + } + + // no ammo cross + if ( !CG_WeaponHasAmmo( realweap ) ) { // DHM - Nerve + CG_DrawPic( x, y, WP_ICON_X, WP_ICON_Y, cgs.media.noammoShader ); + } + } + + } else { + continue; + } + + // secondary fire + if ( wideweap ) { + x = WP_DRAW_X_WIDE - WP_ICON_SEC_X - 4; + } else { + x = WP_DRAW_X - WP_ICON_SEC_X - 4; + } + +// JPW NERVE + if ( cg_gameType.integer == GT_WOLF ) { + drawweap = getAltWeapon( weapBanksMultiPlayer[curweapbank][i] ); + } else { +// jpw + drawweap = getAltWeapon( weapBanks[curweapbank][i] ); + } + + // clear drawweap if getaltweap() returns the same weap as passed in. (no secondary available) +// JPW NERVE + if ( cg_gameType.integer == GT_WOLF ) { + if ( drawweap == weapBanksMultiPlayer[curweapbank][i] ) { + drawweap = 0; + } + } else { +// jpw + if ( drawweap == weapBanks[curweapbank][i] ) { + drawweap = 0; + } + } + + realweap = drawweap; // DHM - Nerve + + if ( drawweap && ( bits[0] & ( 1 << drawweap ) ) ) { + // you've got it, draw it + // DHM - Nerve :: Special case for WP_CLASS_SPECIAL + if ( cgs.gametype == GT_WOLF && drawweap == WP_CLASS_SPECIAL ) { + switch ( cg.predictedPlayerState.stats[ STAT_PLAYER_CLASS ] ) { + case PC_ENGINEER: + drawweap = WP_CLASS_SPECIAL; + break; + case PC_MEDIC: + drawweap = WP_MEDIC_HEAL; + break; + case PC_LT: + drawweap = WP_GRENADE_SMOKE; + break; + default: + break; + } + } + // dhm - end + + CG_RegisterWeapon( drawweap ); + + // weapon icon + if ( realweap == cg.weaponSelect ) { + CG_DrawPic( x, y, WP_ICON_SEC_X, WP_ICON_SEC_Y, cg_weapons[drawweap].weaponIcon[1] ); + } else { + CG_DrawPic( x, y, WP_ICON_SEC_X, WP_ICON_SEC_Y, cg_weapons[drawweap].weaponIcon[0] ); + } + + // no ammo cross + if ( !CG_WeaponHasAmmo( realweap ) ) { + CG_DrawPic( x, y, WP_ICON_SEC_X, WP_ICON_SEC_Y, cgs.media.noammoShader ); + } + } + + + y += ( WP_ICON_Y + WP_ICON_SPACE_Y ); + } +} + + + + +/* +============== +CG_WeaponHasAmmo + check for ammo +============== +*/ +static qboolean CG_WeaponHasAmmo( int i ) { + if ( !( cg.predictedPlayerState.ammo[BG_FindAmmoForWeapon( i )] ) && + !( cg.predictedPlayerState.ammoclip[BG_FindClipForWeapon( i )] ) ) { + return qfalse; + } + + return qtrue; +} + + +/* +=============== +CG_WeaponSelectable +=============== +*/ +static qboolean CG_WeaponSelectable( int i ) { + + // allow the player to unselect all weapons +// if(i == WP_NONE) +// return qtrue; + + // if holding a melee weapon (chair/shield/etc.) only allow single-handed weapons + if ( cg.snap->ps.eFlags & EF_MELEE_ACTIVE ) { + if ( !( WEAPS_ONE_HANDED & ( 1 << i ) ) ) { + return qfalse; + } + } + + // allow switch out of scope for weapons where you fired the last shot while scoped + // and we left you in that view to see the result of the shot + switch ( cg.weaponSelect ) { + case WP_SNOOPERSCOPE: + if ( i == WP_GARAND ) { + return qtrue; + } + break; + case WP_SNIPERRIFLE: + if ( i == WP_MAUSER ) { + return qtrue; + } + break; + case WP_FG42SCOPE: + if ( i == WP_FG42 ) { + return qtrue; + } + break; + default: + break; + } + + + // check for weapon + if ( !( COM_BitCheck( cg.predictedPlayerState.weapons, i ) ) ) { + return qfalse; + } + + if ( !CG_WeaponHasAmmo( i ) ) { + return qfalse; + } + + return qtrue; +} + + + + +/* +============== +CG_WeaponIndex +============== +*/ +int CG_WeaponIndex( int weapnum, int *bank, int *cycle ) { + static int bnk, cyc; + + if ( weapnum <= 0 || weapnum >= WP_NUM_WEAPONS ) { + if ( bank ) { + *bank = 0; + } + if ( cycle ) { + *cycle = 0; + } + return 0; + } + + for ( bnk = 0; bnk < maxWeapBanks; bnk++ ) { + for ( cyc = 0; cyc < maxWeapsInBank; cyc++ ) { + + // end of cycle, go to next bank + if ( cg_gameType.integer != GT_WOLF ) { // JPW NERVE + if ( !weapBanks[bnk][cyc] ) { + break; + } + + // found the current weapon + if ( weapBanks[bnk][cyc] == weapnum ) { + if ( bank ) { + *bank = bnk; + } + if ( cycle ) { + *cycle = cyc; + } + return 1; + } + } +// JPW NERVE + else { + if ( !weapBanksMultiPlayer[bnk][cyc] ) { + break; + } + + // found the current weapon + if ( weapBanksMultiPlayer[bnk][cyc] == weapnum ) { + if ( bank ) { + *bank = bnk; + } + if ( cycle ) { + *cycle = cyc; + } + return 1; + } + } +// jpw + } + } + + // failed to find the weapon in the table + // probably an alternate + + return 0; +} + + + +/* +============== +getNextWeapInBank + Pass in a bank and cycle and this will return the next valid weapon higher in the cycle. + if the weap passed in is above highest in a cycle (maxWeapsInBank), this will safely loop around +============== +*/ +static int getNextWeapInBank( int bank, int cycle ) { + + cycle++; + + cycle = cycle % maxWeapsInBank; + + if ( cg_gameType.integer != GT_WOLF ) { // JPW NERVE + if ( weapBanks[bank][cycle] ) { // return next weapon in bank if there is one + return weapBanks[bank][cycle]; + } else { // return first in bank + return weapBanks[bank][0]; + } + } +// JPW NERVE + else { + if ( weapBanksMultiPlayer[bank][cycle] ) { // return next weapon in bank if there is one + return weapBanksMultiPlayer[bank][cycle]; + } else { // return first in bank + return weapBanksMultiPlayer[bank][0]; + } + } +// jpw +} + +static int getNextWeapInBankBynum( int weapnum ) { + int bank, cycle; + + if ( !CG_WeaponIndex( weapnum, &bank, &cycle ) ) { + return weapnum; + } + + return getNextWeapInBank( bank, cycle ); +} + + +/* +============== +getPrevWeapInBank + Pass in a bank and cycle and this will return the next valid weapon lower in the cycle. + if the weap passed in is the lowest in a cycle (0), this will loop around to the + top (maxWeapsInBank-1) and start down from there looking for a valid weapon position +============== +*/ +static int getPrevWeapInBank( int bank, int cycle ) { + cycle--; + if ( cycle < 0 ) { + cycle = maxWeapsInBank - 1; + } + + + if ( cg_gameType.integer != GT_WOLF ) { + while ( !weapBanks[bank][cycle] ) { + cycle--; + + if ( cycle < 0 ) { + cycle = maxWeapsInBank - 1; + } + } + return weapBanks[bank][cycle]; + } else { + while ( !weapBanksMultiPlayer[bank][cycle] ) { + cycle--; + + if ( cycle < 0 ) { + cycle = maxWeapsInBank - 1; + } + } + return weapBanksMultiPlayer[bank][cycle]; + } +} + + +static int getPrevWeapInBankBynum( int weapnum ) { + int bank, cycle; + + if ( !CG_WeaponIndex( weapnum, &bank, &cycle ) ) { + return weapnum; + } + + return getPrevWeapInBank( bank, cycle ); +} + + + +/* +============== +getNextBankWeap + Pass in a bank and cycle and this will return the next valid weapon in a higher bank. + sameBankPosition: if there's a weapon in the next bank at the same cycle, + return that (colt returns thompson for example) rather than the lowest weapon +============== +*/ +static int getNextBankWeap( int bank, int cycle, qboolean sameBankPosition ) { + bank++; + + bank = bank % maxWeapBanks; + + if ( cg_gameType.integer != GT_WOLF ) { // JPW NERVE + if ( sameBankPosition && weapBanks[bank][cycle] ) { + return weapBanks[bank][cycle]; + } else { + return weapBanks[bank][0]; + } + } +// JPW NERVE + else { + if ( sameBankPosition && weapBanksMultiPlayer[bank][cycle] ) { + return weapBanksMultiPlayer[bank][cycle]; + } else { + return weapBanksMultiPlayer[bank][0]; + } + } +// jpw +} + +/* +============== +getPrevBankWeap + Pass in a bank and cycle and this will return the next valid weapon in a lower bank. + sameBankPosition: if there's a weapon in the prev bank at the same cycle, + return that (thompson returns colt for example) rather than the highest weapon +============== +*/ +static int getPrevBankWeap( int bank, int cycle, qboolean sameBankPosition ) { + int i; + + bank--; + + if ( bank < 0 ) { // don't go below 0, cycle up to top + bank += maxWeapBanks; + } + + bank = bank % maxWeapBanks; + + if ( cg_gameType.integer != GT_WOLF ) { // JPW NERVE + if ( sameBankPosition && weapBanks[bank][cycle] ) { + return weapBanks[bank][cycle]; + } else + { // find highest weap in bank + for ( i = maxWeapsInBank - 1; i >= 0; i-- ) { + if ( weapBanks[bank][i] ) { + return weapBanks[bank][i]; + } + } + + // if it gets to here, no valid weaps in this bank, go down another bank + return getPrevBankWeap( bank, cycle, sameBankPosition ); + } + } +// JPW NERVE + else { + if ( sameBankPosition && weapBanksMultiPlayer[bank][cycle] ) { + return weapBanksMultiPlayer[bank][cycle]; + } else + { // find highest weap in bank + for ( i = maxWeapsInBank - 1; i >= 0; i-- ) { + if ( weapBanksMultiPlayer[bank][i] ) { + return weapBanksMultiPlayer[bank][i]; + } + } + + // if it gets to here, no valid weaps in this bank, go down another bank + return getPrevBankWeap( bank, cycle, sameBankPosition ); + } + } +// jpw +} + +/* +============== +getAltWeapon +============== +*/ +static int getAltWeapon( int weapnum ) { + if ( weapnum > MAX_WEAP_ALTS ) { + return weapnum; + } + + if ( weapAlts[weapnum] ) { + return weapAlts[weapnum]; + } + + return weapnum; +} + + + +/* +============== +getEquivWeapon + return the id of the opposite team's weapon. + Passing the weapnum of the mp40 returns the id of the thompson, and likewise + passing the weapnum of the thompson returns the id of the mp40. + No equivalent available will return the weapnum passed in. +============== +*/ +int getEquivWeapon( int weapnum ) { + int num = weapnum; + + switch ( weapnum ) { + // going from german to american + case WP_LUGER: num = WP_COLT; break; + case WP_MAUSER: num = WP_GARAND; break; + case WP_MP40: num = WP_THOMPSON; break; + case WP_GRENADE_LAUNCHER: num = WP_GRENADE_PINEAPPLE; break; + + // going from american to german + case WP_COLT: num = WP_LUGER; break; + case WP_GARAND: num = WP_MAUSER; break; + case WP_THOMPSON: num = WP_MP40; break; + case WP_GRENADE_PINEAPPLE: num = WP_GRENADE_LAUNCHER; break; + } + return num; +} + + + +/* +============== +CG_WeaponSuggest +============== +*/ +void CG_WeaponSuggest( int weap ) { + int bank, cycle; + + return; // not currently supported + + if ( !cg_useSuggestedWeapons.integer ) { + return; + } + + cg.weaponSelectTime = cg.time; + + CG_WeaponIndex( weap, &bank, &cycle ); // get location of this weap + + cg.lastWeapSelInBank[bank] = weap; // make this weap first priority in that bank + +} + + +/* +============== +CG_SetSniperZoom +============== +*/ + +void CG_SetSniperZoom( int lastweap, int newweap ) { + int zoomindex; + float shake = 0; + + if ( lastweap == newweap ) { + return; + } + + cg.zoomval = 0; + cg.zoomedScope = 0; + + // check for fade-outs + switch ( lastweap ) { + case WP_SNIPERRIFLE: +// cg.zoomedScope = 500; // TODO: add to zoomTable +// cg.zoomTime = cg.time; + break; + case WP_SNOOPERSCOPE: +// cg.zoomedScope = 500; // TODO: add to zoomTable +// cg.zoomTime = cg.time; + break; + case WP_FG42SCOPE: +// cg.zoomedScope = 1; // TODO: add to zoomTable +// cg.zoomTime = cg.time; + break; + } + + switch ( newweap ) { + + default: + return; // no sniper zoom, get out. + + case WP_SNIPERRIFLE: + cg.zoomval = cg_zoomDefaultSniper.value; + cg.zoomedScope = 900; // TODO: add to zoomTable + zoomindex = ZOOM_SNIPER; +// shake = 0.04; + shake = 0.03f; + break; + case WP_SNOOPERSCOPE: + cg.zoomval = cg_zoomDefaultSnooper.value; + cg.zoomedScope = 800; // TODO: add to zoomTable + zoomindex = ZOOM_SNOOPER; + shake = 0.04f; + break; + case WP_FG42SCOPE: + cg.zoomval = cg_zoomDefaultFG.value; + cg.zoomedScope = 1; // TODO: add to zoomTable + zoomindex = ZOOM_FG42SCOPE; + shake = 0.01f; + break; + } + +// if(shake) { +// (SA) all shake disabled 11/12 +// CG_StartShakeCamera( shake, 1000, cg.snap->ps.origin, 100 ); +// } + + // constrain user preferred fov to weapon limitations + if ( cg.zoomval > zoomTable[zoomindex][ZOOM_OUT] ) { + cg.zoomval = zoomTable[zoomindex][ZOOM_OUT]; + } + if ( cg.zoomval < zoomTable[zoomindex][ZOOM_IN] ) { + cg.zoomval = zoomTable[zoomindex][ZOOM_IN]; + } + + cg.zoomTime = cg.time; +} + + +/* +============== +CG_PlaySwitchSound + Get special switching sounds if they're there +============== +*/ +void CG_PlaySwitchSound( int lastweap, int newweap ) { +// weaponInfo_t *weap; +// weap = &cg_weapons[ ent->weapon ]; + sfxHandle_t switchsound; + + switchsound = cgs.media.selectSound; + + if ( getAltWeapon( lastweap ) == newweap ) { // alt switch + switch ( newweap ) { + case WP_SILENCER: + case WP_LUGER: + switchsound = cg_weapons[newweap].switchSound[0]; + break; + default: + break; + } + } + + trap_S_StartSound( NULL, cg.snap->ps.clientNum, CHAN_WEAPON, switchsound ); +} + + +/* +============== +CG_FinishWeaponChange +============== +*/ +void CG_FinishWeaponChange( int lastweap, int newweap ) { + int newbank; + + cg.weaponSelectTime = cg.time; // flash the weapon icon + + // remember which weapon in this bank was last selected so when cycling back + // to this bank, that weap will be highlighted first + if ( CG_WeaponIndex( newweap, &newbank, NULL ) ) { + cg.lastWeapSelInBank[newbank] = newweap; + } + + if ( lastweap == newweap ) { // no need to do any more than flash the icon + return; + } + + CG_PlaySwitchSound( lastweap, newweap ); //----(SA) added + + CG_SetSniperZoom( lastweap, newweap ); + + // setup for a user call to CG_LastWeaponUsed_f() + if ( lastweap == cg.lastFiredWeapon ) { + // don't set switchback for some weaps... + switch ( lastweap ) { + case WP_SNIPERRIFLE: + case WP_SNOOPERSCOPE: + case WP_FG42SCOPE: + break; + default: + cg.switchbackWeapon = lastweap; + break; + } + } else { + // if this ended up having the switchback be the same + // as the new weapon, set the switchback to the prev + // selected weapon will become the switchback + if ( cg.switchbackWeapon == newweap ) { + cg.switchbackWeapon = lastweap; + } + } + + cg.weaponSelect = newweap; +} + +/* +============== +CG_AltfireWeapon_f + for example, switching between WP_MAUSER and WP_SNIPERRIFLE +============== +*/ +void CG_AltWeapon_f( void ) { + int original, num; + + if ( !cg.snap ) { + return; + } + if ( cg.snap->ps.pm_flags & PMF_FOLLOW ) { + return; + } + + if ( cg.snap->ps.eFlags & EF_MG42_ACTIVE ) { // no alt-switching when on mg42 + return; + } + + if ( cg.time - cg.weaponSelectTime < cg_weaponCycleDelay.integer ) { + return; // force pause so holding it down won't go too fast + + } + // Don't try to switch when in the middle of reloading. + if ( cg.snap->ps.weaponstate == WEAPON_RELOADING ) { + return; + } + + original = cg.weaponSelect; + + num = getAltWeapon( original ); + + if ( CG_WeaponSelectable( num ) ) { // new weapon is valid + +//----(SA) testing mod functionality for the silencer on the luger + // (SA) this way, if you switch away from the silenced luger, + // the silencer will still be attached when you switch back + // (until you remove it) + // TODO: will need to make sure the table gets initialized properly on restart/death/whatever. + // I still think I'm going to make the weapon banks stored in the config, so this will + // just be a matter of resetting the banks to what's in the config. + switch ( original ) { + case WP_LUGER: + if ( cg.snap->ps.eFlags & EF_MELEE_ACTIVE ) { // if you're holding a chair, you can't screw on the silencer + return; + } + weapBanks[2][0] = WP_SILENCER; + break; + case WP_SILENCER: + if ( cg.snap->ps.eFlags & EF_MELEE_ACTIVE ) { // if you're holding a chair, you can't remove the silencer + return; + } + weapBanks[2][0] = WP_LUGER; + break; + + case WP_AKIMBO: + weapBanks[2][1] = WP_COLT; + break; + case WP_COLT: + weapBanks[2][1] = WP_AKIMBO; + break; + } + +//----(SA) end + CG_FinishWeaponChange( original, num ); + } +} + + +/* +============== +CG_NextWeap + + switchBanks - curweap is the last in a bank, 'qtrue' means go to the next available bank, 'qfalse' means loop to the head of the bank +============== +*/ +void CG_NextWeap( qboolean switchBanks ) { + int bank = 0, cycle = 0, newbank = 0, newcycle = 0; + int num, curweap; + qboolean nextbank = qfalse; // need to switch to the next bank of weapons? + int i, j; + + num = curweap = cg.weaponSelect; + + CG_WeaponIndex( curweap, &bank, &cycle ); // get bank/cycle of current weapon + + // if you're using an alt mode weapon, try switching back to the parent first + if ( curweap >= WP_BEGINSECONDARY && curweap <= WP_LASTSECONDARY ) { + num = getAltWeapon( curweap ); // base any further changes on the parent + if ( CG_WeaponSelectable( num ) ) { // the parent was selectable, drop back to that + CG_FinishWeaponChange( curweap, num ); + return; + } + } + + +// if(cg_cycleAllWeaps.integer || !switchBanks) { + if ( 1 ) { + for ( i = 0; i < maxWeapsInBank; i++ ) { + num = getNextWeapInBankBynum( num ); + + CG_WeaponIndex( num, NULL, &newcycle ); // get cycle of new weapon. if it's lower than the original, then it cycled around + + if ( switchBanks ) { + if ( newcycle <= cycle ) { + nextbank = qtrue; + break; + } + } else { // don't switch banks if you get to the end + + if ( num == curweap ) { // back to start, just leave it where it is + return; + } + } + + if ( CG_WeaponSelectable( num ) ) { + break; + } + } + } else { + nextbank = qtrue; + } + + if ( nextbank ) { + for ( i = 0; i < maxWeapBanks; i++ ) { +// if(cg_cycleAllWeaps.integer) + if ( 1 ) { + num = getNextBankWeap( bank + i, cycle, qfalse ); // cycling all weaps always starts the next bank at the bottom + } else { + if ( cg.lastWeapSelInBank[bank + i + 1] ) { + num = cg.lastWeapSelInBank[bank + i + 1]; + } else { + num = getNextBankWeap( bank + i, cycle, qtrue ); + } + } + + if ( num == 0 ) { + continue; + } + + if ( CG_WeaponSelectable( num ) ) { // first entry in bank was selectable, no need to scan the bank + break; + } + + CG_WeaponIndex( num, &newbank, &newcycle ); // get the bank of the new weap + + for ( j = newcycle; j < maxWeapsInBank; j++ ) { + num = getNextWeapInBank( newbank, j ); + + if ( CG_WeaponSelectable( num ) ) { // found selectable weapon + break; + } + + num = 0; + } + + if ( num ) { // a selectable weapon was found in the current bank + break; + } + } + } + + CG_FinishWeaponChange( curweap, num ); //----(SA) +} + +/* +============== +CG_PrevWeap + + switchBanks - curweap is the last in a bank + 'qtrue' - go to the next available bank + 'qfalse' - loop to the head of the bank +============== +*/ +void CG_PrevWeap( qboolean switchBanks ) { + int bank = 0, cycle = 0, newbank = 0, newcycle = 0; + int num, curweap; + qboolean prevbank = qfalse; // need to switch to the next bank of weapons? + int i, j; + + num = curweap = cg.weaponSelect; + + CG_WeaponIndex( curweap, &bank, &cycle ); // get bank/cycle of current weapon + + // if you're using an alt mode weapon, try switching back to the parent first + if ( curweap >= WP_BEGINSECONDARY && curweap <= WP_LASTSECONDARY ) { + num = getAltWeapon( curweap ); // base any further changes on the parent + if ( CG_WeaponSelectable( num ) ) { // the parent was selectable, drop back to that + CG_FinishWeaponChange( curweap, num ); + return; + } + } + + // initially, just try to find a lower weapon in the current bank +// if(cg_cycleAllWeaps.integer || !switchBanks) { + if ( 1 ) { + +// if(cycle == 0) { // already at bottom of list +// prevbank = qtrue; +// } else { + for ( i = cycle; i >= 0; i-- ) { +// num = getPrevWeapInBank(bank, i); + num = getPrevWeapInBankBynum( num ); + + CG_WeaponIndex( num, NULL, &newcycle ); // get cycle of new weapon. if it's greater than the original, then it cycled around + + if ( switchBanks ) { + if ( newcycle > ( cycle - 1 ) ) { + prevbank = qtrue; + break; + } + } else { // don't switch banks if you get to the end + if ( num == curweap ) { // back to start, just leave it where it is + return; + } + } + + if ( CG_WeaponSelectable( num ) ) { + break; + } + } +// } + } else { + prevbank = qtrue; + } + + // cycle to previous bank. + // if cycleAllWeaps: find highest weapon in bank + // else: try to find weap in bank that matches cycle position + // else: use base weap in bank + + if ( prevbank ) { + for ( i = 0; i < maxWeapBanks; i++ ) { +// if(cg_cycleAllWeaps.integer) + if ( 1 ) { + num = getPrevBankWeap( bank - i, cycle, qfalse ); // cycling all weaps always starts the next bank at the bottom + } else { + num = getPrevBankWeap( bank - i, cycle, qtrue ); + } + + if ( num == 0 ) { + continue; + } + + if ( CG_WeaponSelectable( num ) ) { // first entry in bank was selectable, no need to scan the bank + break; + } + + CG_WeaponIndex( num, &newbank, &newcycle ); // get the bank of the new weap + + for ( j = maxWeapsInBank; j > 0; j-- ) { + num = getPrevWeapInBank( newbank, j ); + + if ( CG_WeaponSelectable( num ) ) { // found selectable weapon + break; + } + + num = 0; + } + + if ( num ) { // a selectable weapon was found in the current bank + break; + } + } + } + + CG_FinishWeaponChange( curweap, num ); //----(SA) +} + + +/* +============== +CG_LastWeaponUsed_f +============== +*/ +void CG_LastWeaponUsed_f( void ) { + int lastweap; + + if ( cg.time - cg.weaponSelectTime < cg_weaponCycleDelay.integer ) { + return; // force pause so holding it down won't go too fast + + } + cg.weaponSelectTime = cg.time; // flash the current weapon icon + + // don't switchback if reloading (it nullifies the reload) + if ( cg.snap->ps.weaponstate == WEAPON_RELOADING ) { + return; + } + + if ( !cg.switchbackWeapon ) { + cg.switchbackWeapon = cg.weaponSelect; + return; + } + + if ( CG_WeaponSelectable( cg.switchbackWeapon ) ) { + lastweap = cg.weaponSelect; + CG_FinishWeaponChange( cg.weaponSelect, cg.switchbackWeapon ); + } else { // switchback no longer selectable, reset cycle + cg.switchbackWeapon = 0; + } + +} + +/* +============== +CG_NextWeaponInBank_f +============== +*/ +void CG_NextWeaponInBank_f( void ) { + + if ( cg.time - cg.weaponSelectTime < cg_weaponCycleDelay.integer ) { + return; // force pause so holding it down won't go too fast + + } + // this cvar is an option that lets the player use his weapon switching keys (probably the mousewheel) + // for zooming (binocs/snooper/sniper/etc.) + if ( cg.zoomval ) { + if ( cg_useWeapsForZoom.integer == 1 ) { + CG_ZoomIn_f(); + return; + } else if ( cg_useWeapsForZoom.integer == 2 ) { + CG_ZoomOut_f(); + return; + } + } + + cg.weaponSelectTime = cg.time; // flash the current weapon icon + + CG_NextWeap( qfalse ); +} + +/* +============== +CG_PrevWeaponInBank_f +============== +*/ +void CG_PrevWeaponInBank_f( void ) { + + if ( cg.time - cg.weaponSelectTime < cg_weaponCycleDelay.integer ) { + return; // force pause so holding it down won't go too fast + + } + // this cvar is an option that lets the player use his weapon switching keys (probably the mousewheel) + // for zooming (binocs/snooper/sniper/etc.) + if ( cg.zoomval ) { + if ( cg_useWeapsForZoom.integer == 2 ) { + CG_ZoomIn_f(); + return; + } else if ( cg_useWeapsForZoom.integer == 1 ) { + CG_ZoomOut_f(); + return; + } + } + + cg.weaponSelectTime = cg.time; // flash the current weapon icon + + CG_PrevWeap( qfalse ); +} + + +/* +============== +CG_NextWeapon_f +============== +*/ +void CG_NextWeapon_f( void ) { + + if ( !cg.snap ) { + return; + } + if ( cg.snap->ps.pm_flags & PMF_FOLLOW ) { + return; + } + + // this cvar is an option that lets the player use his weapon switching keys (probably the mousewheel) + // for zooming (binocs/snooper/sniper/etc.) + if ( cg.zoomval ) { + if ( cg_useWeapsForZoom.integer == 1 ) { + CG_ZoomIn_f(); + return; + } else if ( cg_useWeapsForZoom.integer == 2 ) { + CG_ZoomOut_f(); + return; + } + } + + if ( cg.time - cg.weaponSelectTime < cg_weaponCycleDelay.integer ) { + return; // force pause so holding it down won't go too fast + + } + cg.weaponSelectTime = cg.time; // flash the current weapon icon + + // Don't try to switch when in the middle of reloading. + // cheatinfo: The server actually would let you switch if this check were not + // present, but would discard the reload. So the when you switched + // back you'd have to start the reload over. This seems bad, however + // the delay for the current reload is already in effect, so you'd lose + // the reload time twice. (the first pause for the current weapon reload, + // and the pause when you have to reload again 'cause you canceled this one) + + if ( cg.snap->ps.weaponstate == WEAPON_RELOADING ) { + return; + } + + CG_NextWeap( qtrue ); +} + + +/* +============== +CG_PrevWeapon_f +============== +*/ +void CG_PrevWeapon_f( void ) { + // TTimo: unused + /* + int bank = 0, cycle = 0, newbank = 0, newcycle = 0; + qboolean prevbank = qfalse; // need to switch to the next bank of weapons? + */ + + if ( !cg.snap ) { + return; + } + if ( cg.snap->ps.pm_flags & PMF_FOLLOW ) { + return; + } + + // this cvar is an option that lets the player use his weapon switching keys (probably the mousewheel) + // for zooming (binocs/snooper/sniper/etc.) + if ( cg.zoomval ) { + if ( cg_useWeapsForZoom.integer == 1 ) { + CG_ZoomOut_f(); + return; + } else if ( cg_useWeapsForZoom.integer == 2 ) { + CG_ZoomIn_f(); + return; + } + } + + if ( cg.time - cg.weaponSelectTime < cg_weaponCycleDelay.integer ) { + return; // force pause so holding it down won't go too fast + + } + cg.weaponSelectTime = cg.time; // flash the current weapon icon + + // Don't try to switch when in the middle of reloading. + if ( cg.snap->ps.weaponstate == WEAPON_RELOADING ) { + return; + } + + CG_PrevWeap( qtrue ); +} + + +/* +============== +CG_WeaponBank_f + weapon keys are not generally bound directly('bind 1 weapon 1'), + rather the key is bound to a given bank ('bind 1 weaponbank 1') +============== +*/ +void CG_WeaponBank_f( void ) { + int num, i, curweap; + int curbank = 0, curcycle = 0, bank = 0, cycle = 0; + + if ( !cg.snap ) { + return; + } + + if ( cg.snap->ps.pm_flags & PMF_FOLLOW ) { + return; + } + + if ( cg.time - cg.weaponSelectTime < cg_weaponCycleDelay.integer ) { + return; // force pause so holding it down won't go too fast + + } + cg.weaponSelectTime = cg.time; // flash the current weapon icon + + // Don't try to switch when in the middle of reloading. + if ( cg.snap->ps.weaponstate == WEAPON_RELOADING ) { + return; + } + + bank = atoi( CG_Argv( 1 ) ); + + if ( bank <= 0 || bank > maxWeapBanks ) { + return; + } + + curweap = cg.weaponSelect; + CG_WeaponIndex( curweap, &curbank, &curcycle ); // get bank/cycle of current weapon + + if ( !cg.lastWeapSelInBank[bank] ) { + if ( cg_gameType.integer != GT_WOLF ) { // JPW NERVE + num = weapBanks[bank][0]; + } +// JPW NERVE + else { + num = weapBanksMultiPlayer[bank][0]; + } +// jpw + cycle -= 1; // cycle up to first weap + } else { + num = cg.lastWeapSelInBank[bank]; + CG_WeaponIndex( num, &bank, &cycle ); + if ( bank != curbank ) { + cycle -= 1; + } + + } + + for ( i = 0; i < maxWeapsInBank; i++ ) { + num = getNextWeapInBank( bank, cycle + i ); + + if ( CG_WeaponSelectable( num ) ) { + break; + } + } + + if ( i == maxWeapsInBank ) { + return; + } + + CG_FinishWeaponChange( curweap, num ); + +} + +/* +=============== +CG_Weapon_f +=============== +*/ +void CG_Weapon_f( void ) { + int num, i, curweap; + int bank = 0, cycle = 0, newbank = 0, newcycle = 0; + qboolean banked = qfalse; + + if ( !cg.snap ) { + return; + } + + if ( cg.snap->ps.pm_flags & PMF_FOLLOW ) { + return; + } + + num = atoi( CG_Argv( 1 ) ); + +// JPW NERVE +// weapon bind should execute weaponbank instead -- for splitting out class weapons, per Id request + if ( cg_gameType.integer == GT_WOLF ) { + if ( num < maxWeapBanks ) { + CG_WeaponBank_f(); + } + return; + } +// jpw + + cg.weaponSelectTime = cg.time; // flash the current weapon icon + + // Don't try to switch when in the middle of reloading. + if ( cg.snap->ps.weaponstate == WEAPON_RELOADING ) { + return; + } + + + if ( num <= WP_NONE || num > WP_NUM_WEAPONS ) { + return; + } + + curweap = cg.weaponSelect; + + CG_WeaponIndex( curweap, &bank, &cycle ); // get bank/cycle of current weapon + banked = CG_WeaponIndex( num, &newbank, &newcycle ); // get bank/cycle of requested weapon + + // the new weapon was not found in the reglar banks + // assume the player want's to go directly to it if possible + if ( !banked ) { + if ( CG_WeaponSelectable( num ) ) { + CG_FinishWeaponChange( curweap, num ); + return; + } + } + + if ( bank != newbank ) { + cycle = newcycle - 1; // drop down one from the requested weap's cycle so it will + } + // try to initially cycle up to the requested weapon + + for ( i = 0; i < maxWeapsInBank; i++ ) { + num = getNextWeapInBank( newbank, cycle + i ); + + if ( num == curweap ) { // no other weapons in bank + return; + } + + if ( CG_WeaponSelectable( num ) ) { + break; + } + } + + if ( i == maxWeapsInBank ) { + return; + } + + CG_FinishWeaponChange( curweap, num ); +} + +/* +=================== +CG_OutOfAmmoChange + +The current weapon has just run out of ammo +=================== +*/ +void CG_OutOfAmmoChange( void ) { + int i; + int bank, cycle; + int equiv = WP_NONE; + + // + // trivial switching + // + + // if you're using an alt mode weapon, try switching back to the parent + // otherwise, switch to the equivalent if you've got it + if ( cg.weaponSelect >= WP_BEGINSECONDARY && cg.weaponSelect <= WP_LASTSECONDARY ) { + cg.weaponSelect = equiv = getAltWeapon( cg.weaponSelect ); // base any further changes on the parent + if ( CG_WeaponSelectable( equiv ) ) { // the parent was selectable, drop back to that + CG_FinishWeaponChange( cg.predictedPlayerState.weapon, cg.weaponSelect ); //----(SA) + return; + } + } + + + // now try the opposite team's equivalent weap + equiv = getEquivWeapon( cg.weaponSelect ); + + if ( equiv != cg.weaponSelect && CG_WeaponSelectable( equiv ) ) { + cg.weaponSelect = equiv; + CG_FinishWeaponChange( cg.predictedPlayerState.weapon, cg.weaponSelect ); //----(SA) + return; + } + + // + // more complicated selection + // + + // didn't have available alternative or equivalent, try another weap in the bank + CG_WeaponIndex( cg.weaponSelect, &bank, &cycle ); // get bank/cycle of current weapon + + for ( i = cycle; i < maxWeapsInBank; i++ ) { + equiv = getNextWeapInBank( bank, i ); + if ( CG_WeaponSelectable( equiv ) ) { // found a reasonable replacement + cg.weaponSelect = equiv; + CG_FinishWeaponChange( cg.predictedPlayerState.weapon, cg.weaponSelect ); //----(SA) + return; + } + } + + + // still nothing available, just go to the next + // available weap using the regular selection scheme + CG_NextWeap( qtrue ); + +} + +/* +=================================================================================================== + +WEAPON EVENTS + +=================================================================================================== +*/ + +// Note to self this is dead code +void CG_MG42EFX( centity_t *cent ) { + vec3_t forward; + vec3_t point; + refEntity_t flash; + +// trap_S_StartSound( NULL, cent->currentState.number, CHAN_WEAPON, hWeaponSnd ); + + VectorCopy( cent->currentState.origin, point ); + AngleVectors( cent->currentState.angles, forward, NULL, NULL ); + VectorMA( point, 40, forward, point ); + trap_R_AddLightToScene( point, 200 + ( rand() & 31 ),1.0, 0.6, 0.23, 0 ); + + memset( &flash, 0, sizeof( flash ) ); + flash.renderfx = RF_LIGHTING_ORIGIN; + flash.hModel = cgs.media.mg42muzzleflash; + + VectorCopy( point, flash.origin ); + AnglesToAxis( cg.refdefViewAngles, flash.axis ); + + trap_R_AddRefEntityToScene( &flash ); +} + +void CG_FLAKEFX( centity_t *cent, int whichgun ) { + entityState_t *ent; + vec3_t forward, right, up; + vec3_t point; + refEntity_t flash; + + ent = ¢->currentState; + + VectorCopy( cent->currentState.pos.trBase, point ); + AngleVectors( cent->currentState.apos.trBase, forward, right, up ); + + // gun 1 and 2 were switched + if ( whichgun == 2 ) { + VectorMA( point, 136, forward, point ); + VectorMA( point, 31, up, point ); + VectorMA( point, 22, right, point ); + } else if ( whichgun == 1 ) { + VectorMA( point, 136, forward, point ); + VectorMA( point, 31, up, point ); + VectorMA( point, -22, right, point ); + } else if ( whichgun == 3 ) { + VectorMA( point, 136, forward, point ); + VectorMA( point, 10, up, point ); + VectorMA( point, 22, right, point ); + } else if ( whichgun == 4 ) { + VectorMA( point, 136, forward, point ); + VectorMA( point, 10, up, point ); + VectorMA( point, -22, right, point ); + } + + trap_R_AddLightToScene( point, 200 + ( rand() & 31 ),1.0, 0.6, 0.23, 0 ); + + memset( &flash, 0, sizeof( flash ) ); + flash.renderfx = RF_LIGHTING_ORIGIN; + flash.hModel = cgs.media.mg42muzzleflash; + + VectorCopy( point, flash.origin ); + AnglesToAxis( cg.refdefViewAngles, flash.axis ); + + trap_R_AddRefEntityToScene( &flash ); + + trap_S_StartSound( NULL, ent->number, CHAN_WEAPON, hflakWeaponSnd ); +} + + +//----(SA) +/* +============== +CG_MortarEFX + Right now mostly copied directly from Raf's MG42 FX, but with the optional addtion of smoke +============== +*/ +void CG_MortarEFX( centity_t *cent ) { + refEntity_t flash; + + if ( cent->currentState.density & 1 ) { + // smoke + CG_ParticleImpactSmokePuff( cgs.media.smokePuffShader, cent->currentState.origin ); + } + + if ( cent->currentState.density & 2 ) { + // light + trap_R_AddLightToScene( cent->currentState.origin, 200 + ( rand() & 31 ), 1.0, 1.0, 1.0, 0 ); + + // muzzle flash + memset( &flash, 0, sizeof( flash ) ); + flash.renderfx = RF_LIGHTING_ORIGIN; + flash.hModel = cgs.media.mg42muzzleflash; + VectorCopy( cent->currentState.origin, flash.origin ); + AnglesToAxis( cg.refdefViewAngles, flash.axis ); + trap_R_AddRefEntityToScene( &flash ); + } +} + +//----(SA) end + + +// RF +/* +============== +CG_WeaponFireRecoil +============== +*/ +void CG_WeaponFireRecoil( int weapon ) { +// const vec3_t maxKickAngles = {25, 30, 25}; + float pitchRecoilAdd, pitchAdd; + float yawRandom; + vec3_t recoil; + // + pitchRecoilAdd = 0; + pitchAdd = 0; + yawRandom = 0; + // + switch ( weapon ) { + case WP_LUGER: + case WP_SILENCER: + case WP_COLT: + case WP_AKIMBO: //----(SA) added + //pitchAdd = 2+rand()%3; + //yawRandom = 2; + break; + case WP_MAUSER: + case WP_GARAND: + //pitchAdd = 4+rand()%3; + //yawRandom = 4; + pitchAdd = 2; //----(SA) for DM + yawRandom = 1; //----(SA) for DM + break; + case WP_SNIPERRIFLE: + case WP_SNOOPERSCOPE: + pitchAdd = 0.6; + break; + case WP_FG42SCOPE: + case WP_FG42: + case WP_MP40: + case WP_THOMPSON: + case WP_STEN: + //pitchRecoilAdd = 1; + pitchAdd = 1 + rand() % 3; + yawRandom = 2; + + + pitchAdd *= 0.3; + yawRandom *= 0.3; + break; + case WP_PANZERFAUST: + //pitchAdd = 12+rand()%3; + //yawRandom = 6; + + CG_StartShakeCamera( 0.05, 700, cg.snap->ps.origin, 100 ); + + // push the player back instead + break; + case WP_VENOM: + pitchRecoilAdd = pow( random(),8 ) * ( 10 + VectorLength( cg.snap->ps.velocity ) / 5 ); + pitchAdd = ( rand() % 5 ) - 2; + yawRandom = 2; + + + pitchRecoilAdd *= 0.5; + pitchAdd *= 0.5; + yawRandom *= 0.5; + break; + default: + return; + } + // calc the recoil + recoil[YAW] = crandom() * yawRandom; + recoil[ROLL] = -recoil[YAW]; // why not + recoil[PITCH] = -pitchAdd; + // scale it up a bit (easier to modify this while tweaking) + VectorScale( recoil, 30, recoil ); + // set the recoil + VectorCopy( recoil, cg.kickAVel ); + // set the recoil + cg.recoilPitch -= pitchRecoilAdd; +} + + +/* +================ +CG_FireWeapon + +Caused by an EV_FIRE_WEAPON event + +================ +*/ +void CG_FireWeapon( centity_t *cent ) { + entityState_t *ent; + int c; + weaponInfo_t *weap; + sfxHandle_t *firesound; + sfxHandle_t *fireEchosound; + + ent = ¢->currentState; + + // Rafael - mg42 + if ( ( cent->currentState.clientNum == cg.snap->ps.clientNum && cg.snap->ps.persistant[PERS_HWEAPON_USE] ) || + ( cent->currentState.clientNum != cg.snap->ps.clientNum && ( cent->currentState.eFlags & EF_MG42_ACTIVE ) ) ) { + if ( cg.snap->ps.gunfx ) { + return; + } + + trap_S_StartSound( NULL, cent->currentState.number, CHAN_WEAPON, hWeaponSnd ); + //trap_S_StartSound( NULL, ent->number, CHAN_WEAPON, hWeaponSnd ); + if ( cg_brassTime.integer > 0 ) { + CG_MachineGunEjectBrass( cent ); + } + + // CG_MG42EFX (cent); + + return; + } + + if ( ent->weapon == WP_NONE ) { + return; + } + if ( ent->weapon >= WP_NUM_WEAPONS ) { + CG_Error( "CG_FireWeapon: ent->weapon >= WP_NUM_WEAPONS" ); + return; + } + weap = &cg_weapons[ ent->weapon ]; + + cg.lastFiredWeapon = ent->weapon; //----(SA) added + + // mark the entity as muzzle flashing, so when it is added it will + // append the flash to the weapon model + cent->muzzleFlashTime = cg.time; + + // RF, kick angles + if ( ent->number == cg.snap->ps.clientNum ) { + CG_WeaponFireRecoil( ent->weapon ); + } + + // lightning gun only does this this on initial press + if ( ent->weapon == WP_FLAMETHROWER ) { + if ( cent->pe.lightningFiring ) { + return; + } + } else if ( ent->weapon == WP_GRENADE_LAUNCHER || + ent->weapon == WP_GRENADE_PINEAPPLE || + ent->weapon == WP_DYNAMITE || + ent->weapon == WP_GRENADE_SMOKE ) { // JPW NERVE + if ( ent->weapon == WP_GRENADE_SMOKE ) { + CG_Printf( "smoke grenade!\n" ); + } + if ( ent->apos.trBase[0] > 0 ) { // underhand + return; + } + } + + // play quad sound if needed + if ( cent->currentState.powerups & ( 1 << PW_QUAD ) ) { + trap_S_StartSound( NULL, cent->currentState.number, CHAN_ITEM, cgs.media.quadSound ); + } + + if ( ( cent->currentState.event & ~EV_EVENT_BITS ) == EV_FIRE_WEAPON_LASTSHOT ) { + firesound = &weap->lastShotSound[0]; + fireEchosound = &weap->flashEchoSound[0]; + + // try to use the lastShotSound, but don't assume it's there. + // if a weapon without the sound calls it, drop back to regular fire sound + + for ( c = 0; c < 4; c++ ) { + if ( !firesound[c] ) { + break; + } + } + if ( !c ) { + firesound = &weap->flashSound[0]; + fireEchosound = &weap->flashEchoSound[0]; + } + } else { + firesound = &weap->flashSound[0]; + fireEchosound = &weap->flashEchoSound[0]; + } + + + // play a sound + for ( c = 0 ; c < 4 ; c++ ) { + if ( !firesound[c] ) { + break; + } + } + if ( c > 0 ) { + c = rand() % c; + if ( firesound[c] ) { + trap_S_StartSound( NULL, ent->number, CHAN_WEAPON, firesound[c] ); + + if ( fireEchosound && fireEchosound[c] ) { // check for echo + centity_t *cent; + vec3_t porg, gorg, norm; // player/gun origin + float gdist; + + cent = &cg_entities[ent->number]; + VectorCopy( cent->currentState.pos.trBase, gorg ); + VectorCopy( cg.refdef.vieworg, porg ); + VectorSubtract( gorg, porg, norm ); + gdist = VectorNormalize( norm ); + if ( gdist > 512 && gdist < 4096 ) { // temp dist. TODO: use numbers that are weapon specific + // use gorg as the new sound origin + VectorMA( cg.refdef.vieworg, 64, norm, gorg ); // sound-on-a-stick + trap_S_StartSoundEx( gorg, ent->number, CHAN_WEAPON, fireEchosound[c], SND_NOCUT ); + } + } + } + } + + // do brass ejection + if ( weap->ejectBrassFunc && cg_brassTime.integer > 0 ) { + weap->ejectBrassFunc( cent ); + } +} + + +// Ridah +/* +================= +CG_AddSparks +================= +*/ +void CG_AddSparks( vec3_t origin, vec3_t dir, int speed, int duration, int count, float randScale ) { + localEntity_t *le; + refEntity_t *re; + vec3_t velocity; + int i; + + for ( i = 0; i < count; i++ ) { + le = CG_AllocLocalEntity(); + re = &le->refEntity; + + VectorSet( velocity, dir[0] + crandom() * randScale, dir[1] + crandom() * randScale, dir[2] + crandom() * randScale ); + VectorScale( velocity, (float)speed, velocity ); + + le->leType = LE_SPARK; + le->startTime = cg.time; + le->endTime = le->startTime + duration - (int)( 0.5 * random() * duration ); + le->lastTrailTime = cg.time; + + VectorCopy( origin, re->origin ); + AxisCopy( axisDefault, re->axis ); + + le->pos.trType = TR_GRAVITY_LOW; + VectorCopy( origin, le->pos.trBase ); + VectorMA( le->pos.trBase, 2 + random() * 4, dir, le->pos.trBase ); + VectorCopy( velocity, le->pos.trDelta ); + le->pos.trTime = cg.time; + + le->refEntity.customShader = cgs.media.sparkParticleShader; + + le->bounceFactor = 0.9; + +// le->leBounceSoundType = LEBS_BLOOD; +// le->leMarkType = LEMT_BLOOD; + } +} +/* +================= +CG_AddBulletParticles +================= +*/ +void CG_AddBulletParticles( vec3_t origin, vec3_t dir, int speed, int duration, int count, float randScale ) { +// localEntity_t *le; +// refEntity_t *re; + vec3_t velocity, pos; + int i; +/* + // add the falling streaks + for (i=0; irefEntity; + + VectorSet( velocity, dir[0] + crandom()*randScale, dir[1] + crandom()*randScale, dir[2] + crandom()*randScale ); + VectorScale( velocity, (float)speed*3, velocity ); + + le->leType = LE_SPARK; + le->startTime = cg.time; + le->endTime = le->startTime + duration - (int)(0.5 * random() * duration); + le->lastTrailTime = cg.time; + + VectorCopy( origin, re->origin ); + AxisCopy( axisDefault, re->axis ); + + le->pos.trType = TR_GRAVITY; + VectorCopy( origin, le->pos.trBase ); + VectorMA( le->pos.trBase, 2 + random()*4, dir, le->pos.trBase ); + VectorCopy( velocity, le->pos.trDelta ); + le->pos.trTime = cg.time; + + le->refEntity.customShader = cgs.media.bulletParticleTrailShader; +// le->refEntity.customShader = cgs.media.sparkParticleShader; + + le->bounceFactor = 0.9; + +// le->leBounceSoundType = LEBS_BLOOD; +// le->leMarkType = LEMT_BLOOD; + } +*/ + // add the falling particles + for ( i = 0; i < count; i++ ) { + + VectorSet( velocity, dir[0] + crandom() * randScale, dir[1] + crandom() * randScale, dir[2] + crandom() * randScale ); + VectorScale( velocity, (float)speed, velocity ); + + VectorCopy( origin, pos ); + VectorMA( pos, 2 + random() * 4, dir, pos ); + + CG_ParticleBulletDebris( pos, velocity, 300 + rand() % 300 ); + + } +} + +//----(SA) from MP +/* +================= +CG_AddDirtBulletParticles +================= +*/ +void CG_AddDirtBulletParticles( vec3_t origin, vec3_t dir, int speed, int duration, int count, float randScale, + float width, float height, float alpha, char *shadername ) { // JPW NERVE + vec3_t velocity, pos; + int i; + + // add the big falling particle + VectorSet( velocity, 0, 0, (float)speed ); + + VectorCopy( origin, pos ); + +// JPW NERVE + CG_ParticleDirtBulletDebris_Core( pos, velocity, duration, width,height, alpha, shadername ); //600 + rand()%300 ); // keep central one + for ( i = 0; i < count; i++ ) { + VectorSet( velocity, dir[0] * crandom() * speed * randScale, + dir[1] * crandom() * speed * randScale, dir[2] * random() * speed ); + CG_ParticleDirtBulletDebris_Core( pos, velocity, duration + ( rand() % ( duration >> 1 ) ), // dur * 0.5, but int + width,height, alpha, shadername ); + } +} +//----(SA) end + +/* +================= +CG_AddDebris +================= +*/ +void CG_AddDebris( vec3_t origin, vec3_t dir, int speed, int duration, int count ) { + localEntity_t *le; + refEntity_t *re; + vec3_t velocity, unitvel; + float timeAdd; + int i; + + for ( i = 0; i < count; i++ ) { + le = CG_AllocLocalEntity(); + re = &le->refEntity; + + VectorSet( unitvel, dir[0] + crandom() * 0.9, dir[1] + crandom() * 0.9, fabs( dir[2] ) > 0.5 ? dir[2] * ( 0.2 + 0.8 * random() ) : random() * 0.6 ); + VectorScale( unitvel, (float)speed + (float)speed * 0.5 * crandom(), velocity ); + + le->leType = LE_DEBRIS; + le->startTime = cg.time; + le->endTime = le->startTime + duration + (int)( (float)duration * 0.8 * crandom() ); + le->lastTrailTime = cg.time; + + VectorCopy( origin, re->origin ); + AxisCopy( axisDefault, re->axis ); + + le->pos.trType = TR_GRAVITY_LOW; + VectorCopy( origin, le->pos.trBase ); + VectorCopy( velocity, le->pos.trDelta ); + le->pos.trTime = cg.time; + + timeAdd = 10.0 + random() * 40.0; + BG_EvaluateTrajectory( &le->pos, cg.time + (int)timeAdd, le->pos.trBase ); + + le->bounceFactor = 0.5; + +// if (!rand()%2) +// le->effectWidth = 0; // no flame +// else + le->effectWidth = 5 + random() * 5; + +// if (rand()%3) + le->effectFlags |= 1; // smoke trail + + +// le->leBounceSoundType = LEBS_BLOOD; +// le->leMarkType = LEMT_BLOOD; + } +} +// done. + + +/* +============== +CG_WaterRipple +============== +*/ +void CG_WaterRipple( qhandle_t shader, vec3_t loc, vec3_t dir, int size, int lifetime ) { + localEntity_t *le; + refEntity_t *re; + + le = CG_AllocLocalEntity(); + le->leType = LE_SCALE_FADE; + le->leFlags = LEF_PUFF_DONT_SCALE; + + le->startTime = cg.time; + le->endTime = cg.time + lifetime; + le->lifeRate = 1.0 / ( le->endTime - le->startTime ); + + re = &le->refEntity; + VectorCopy( loc, re->origin ); + re->shaderTime = cg.time / 1000.0f; + re->reType = RT_SPLASH; + re->radius = size; + re->customShader = shader; + re->shaderRGBA[0] = 0xff; + re->shaderRGBA[1] = 0xff; + re->shaderRGBA[2] = 0xff; + re->shaderRGBA[3] = 0xff; + le->color[3] = 1.0; +} + +/* +================= +CG_MissileHitWall + +Caused by an EV_MISSILE_MISS event, or directly by local bullet tracing + +ClientNum is a dummy field used to define what sort of effect to spawn +================= +*/ +void CG_MissileHitWall( int weapon, int clientNum, vec3_t origin, vec3_t dir, int surfFlags ) { // (SA) modified to send missilehitwall surface parameters + qhandle_t mod; + qhandle_t mark; + qhandle_t shader; + sfxHandle_t sfx, sfx2; + float radius; + float light; + vec3_t lightColor; + localEntity_t *le; + int r; + qboolean alphaFade = qfalse; + qboolean isSprite; + int duration; + // Ridah + int lightOverdraw; + vec3_t sprOrg; + vec3_t sprVel; + int i,j; + int markDuration; + +//----(SA) added + float shakeAmt; + int shakeDur, shakeRad; + shakeAmt = 0; + shakeDur = shakeRad = 0; +//----(SA) end + + mark = 0; + radius = 32; + sfx = 0; + sfx2 = 0; + mod = 0; + shader = 0; + light = 0; + lightColor[0] = 1; + lightColor[1] = 1; + lightColor[2] = 0; + // Ridah + lightOverdraw = 0; + + // set defaults + isSprite = qfalse; + duration = 600; + markDuration = -1; + + if ( surfFlags & SURF_SKY ) { + return; + } + + switch ( weapon ) { + case WP_KNIFE: + sfx = cgs.media.sfx_knifehit[4]; // different values for different types (stone/metal/wood/etc.) + mark = cgs.media.bulletMarkShader; + radius = 1 + rand() % 2; + + CG_AddBulletParticles( origin, dir, + 20, // speed + 800, // duration + 3 + rand() % 6, // count + 1.0 ); // rand scale + break; + + case WP_LUGER: + case WP_AKIMBO: //----(SA) added + case WP_COLT: + case WP_MAUSER: + case WP_GARAND: + case WP_SNIPERRIFLE: + case WP_SNOOPERSCOPE: + case WP_MP40: + case WP_FG42: + case WP_FG42SCOPE: + case WP_THOMPSON: + case WP_STEN: + case WP_SILENCER: + case WP_VENOM: + + // actually yeah. meant that. very rare. + if ( cg_gameType.integer == GT_SINGLE_PLAYER ) { // JPW NERVE + r = rand() & 31; + } else { + r = ( rand() & 3 ) + 1; // JPW NERVE increased spark frequency so players can tell where rounds are coming from in MP + + } + if ( r == 3 ) { + sfx = cgs.media.sfx_ric1; + } else if ( r == 2 ) { + sfx = cgs.media.sfx_ric2; + } else if ( r == 1 ) { + sfx = cgs.media.sfx_ric3; + } + + // clientNum is a dummy field used to define what sort of effect to spawn + + if ( !clientNum ) { + // RF, why is this here? we need sparks if clientNum = 0, used for warzombie + // if ( sfx ) + CG_AddSparks( origin, dir, + 350, // speed + 200, // duration + 15 + rand() % 7, // count + 0.2 ); // rand scale + } else if ( clientNum == 1 ) { // just do a little smoke puff + vec3_t d, o; + VectorMA( origin, 12, dir, o ); + VectorScale( dir, 7, d ); + d[2] += 16; + + // just snow + if ( surfFlags & SURF_SNOW ) { + CG_AddDirtBulletParticles( origin, dir, + 120, // speed + 900, // duration + 3, // count + 0.6f, + 20, + 4, + 0.3f, + "water_splash" ); // rand scale + } + + // grass/gravel + if ( surfFlags & ( SURF_GRASS | SURF_GRAVEL ) ) { + CG_AddDirtBulletParticles( origin, dir, + 190, // speed + 800, // duration + 3, // count + 0.1, + 60, + 10, + 0.5, + "dirt_splash" ); // rand scale + } else { + + CG_ParticleImpactSmokePuff( cgs.media.smokeParticleShader, o ); + + // some debris particles + CG_AddBulletParticles( origin, dir, + 20, // speed + 800, // duration + 3 + rand() % 6, // count + 1.0 ); // rand scale + + // just do a little one + if ( sfx && ( rand() % 3 == 0 ) ) { + CG_AddSparks( origin, dir, + 450, // speed + 300, // duration + 3 + rand() % 3, // count + 0.5 ); // rand scale + } + } + } else if ( clientNum == 2 ) { + sfx = 0; + mark = 0; + + // (SA) needed to do the CG_WaterRipple using a localent since I needed the timer reset on the shader for each shot + CG_WaterRipple( cgs.media.wakeMarkShaderAnim, origin, tv( 0, 0, 1 ), 32, 1000 ); + + CG_AddDirtBulletParticles( origin, dir, + 190, // speed + 900, // duration + 5, // count + 0.5, // rand scale + 50, // w + 12, // h + 0.125, // alpha + "water_splash" ); + + break; // (SA) testing + + // play a water splash + mod = cgs.media.waterSplashModel; + shader = cgs.media.waterSplashShader; + duration = 250; + + } + + // Ridah, optimization, only spawn the bullet hole if we are close + // enough to see it, this way we can leave other marks around a lot + // longer, since most of the time we can't actually see the bullet holes +// (SA) small modification. only do this for non-rifles (so you can see your shots hitting when you're zooming with a rifle scope) + if ( weapon == WP_FG42SCOPE || weapon == WP_SNIPERRIFLE || weapon == WP_SNOOPERSCOPE || ( Distance( cg.refdef.vieworg, origin ) < 384 ) ) { + + if ( clientNum ) { + + // mark and sound can potentially use the surface for override values + + mark = cgs.media.bulletMarkShader; // default + alphaFade = qtrue; // max made the bullet mark alpha (he'll make everything in the game out of 1024 textures, all with alpha blend funcs yet...) + radius = 1.5f + rand() % 2; // slightly larger for DM + + if ( surfFlags & SURF_METAL ) { + sfx = cgs.media.sfx_bullet_metalhit[rand() % 3]; + mark = cgs.media.bulletMarkShaderMetal; + alphaFade = qtrue; + } else if ( surfFlags & SURF_WOOD ) { + sfx = cgs.media.sfx_bullet_woodhit[rand() % 3]; + mark = cgs.media.bulletMarkShaderWood; + alphaFade = qtrue; + radius += 1; // experimenting with different mark sizes per surface + +/* + if (rand()%100 > 75) + { + gentity_t *sfx; + vec3_t start; + vec3_t dir; + + sfx = G_Spawn (); + + sfx->s.density = type; + + VectorCopy (tr.endpos, start); + + VectorCopy (muzzleTrace, dir); + VectorNegate (dir, dir); + + G_SetOrigin (sfx, start); + G_SetAngle (sfx, dir); + + G_AddEvent( sfx, EV_SHARD, DirToByte( dir )); + + sfx->think = G_FreeEntity; + sfx->nextthink = level.time + 1000; + + sfx->s.frame = 3 + (rand()%3) ; + + trap_LinkEntity (sfx); + + +void CG_Shard(centity_t *cent, vec3_t origin, vec3_t dir) + CG_Shard + + } + +*/ + + + } else if ( surfFlags & SURF_CERAMIC ) { + sfx = cgs.media.sfx_bullet_ceramichit[rand() % 3]; + mark = cgs.media.bulletMarkShaderCeramic; + alphaFade = qtrue; + radius += 2; // experimenting with different mark sizes per surface + + } else if ( surfFlags & SURF_GLASS ) { + sfx = cgs.media.sfx_bullet_glasshit[rand() % 3]; + mark = cgs.media.bulletMarkShaderGlass; + alphaFade = qtrue; + } else if ( surfFlags & SURF_GRASS ) { + + } else if ( surfFlags & SURF_GRAVEL ) { + + } else if ( surfFlags & SURF_SNOW ) { + + } else if ( surfFlags & SURF_ROOF ) { + + } else if ( surfFlags & SURF_CARPET ) { + + } + + } + } + break; + + + case WP_MORTAR: + sfx = cgs.media.sfx_rockexp; + mark = cgs.media.burnMarkShader; + markDuration = 60000; + radius = 64; + light = 300; + isSprite = qtrue; + duration = 1000; + lightColor[0] = 0.75; + lightColor[1] = 0.5; + lightColor[2] = 0.1; + + shakeAmt = 0.15f; + shakeDur = 600; + shakeRad = 700; + + VectorScale( dir, 16, sprVel ); + for ( i = 0; i < 5; i++ ) { + for ( j = 0; j < 3; j++ ) + sprOrg[j] = origin[j] + 64 * dir[j] + 24 * crandom(); + sprVel[2] += rand() % 50; +// CG_ParticleExplosion( 2, sprOrg, sprVel, 1000+rand()%250, 20, 40+rand()%60 ); + CG_ParticleExplosion( "blacksmokeanimb", sprOrg, sprVel, 3500 + rand() % 250, 10, 250 + rand() % 60 ); + } + + VectorMA( origin, 24, dir, sprOrg ); + VectorScale( dir, 64, sprVel ); + // RF, I like this new animation, feel free to revert + CG_ParticleExplosion( "expblue", sprOrg, sprVel, 1000, 20, 300 ); + //CG_ParticleExplosion( "explode1", sprOrg, sprVel, 1200, 9, 300 ); + break; + + case WP_DYNAMITE: + shader = cgs.media.rocketExplosionShader; + sfx = cgs.media.sfx_dynamiteexp; + sfx2 = cgs.media.sfx_dynamiteexpDist; + mark = cgs.media.burnMarkShader; + markDuration = 60000; + radius = 64; + light = 300; + isSprite = qtrue; + duration = 1000; + lightColor[0] = 0.75; + lightColor[1] = 0.5; + lightColor[2] = 0.1; + + shakeAmt = 0.25f; + shakeDur = 2800; + shakeRad = 8192; + + if ( cg_gameType.integer == GT_SINGLE_PLAYER ) { // JPW NERVE + for ( i = 0; i < 5; i++ ) { + for ( j = 0; j < 3; j++ ) + sprOrg[j] = origin[j] + 64 * dir[j] + 24 * crandom(); + sprVel[2] += rand() % 50; + CG_ParticleExplosion( "blacksmokeanimb", sprOrg, sprVel, + 3500 + rand() % 250, // duration + 10, // startsize + 250 + rand() % 60 ); // endsize + } + + VectorMA( origin, 16, dir, sprOrg ); + VectorScale( dir, 100, sprVel ); + + // trying this one just for now just for variety + CG_ParticleExplosion( "explode1", sprOrg, sprVel, + 1200, // duration + 9, // startsize + 300 ); // endsize + + CG_AddDebris( origin, dir, + 280, // speed + 1400, // duration + 7 + rand() % 2 ); // count + } +// JPW NERVE + else { + for ( i = 0; i < 5; i++ ) { + for ( j = 0; j < 2; j++ ) + sprOrg[j] = origin[j] + 96 * crandom(); + sprOrg[2] = origin[2] + 32 * random(); + sprVel[2] += rand() % 75; + CG_ParticleExplosion( "blacksmokeanimb", sprOrg, sprVel, + 3500 + rand() % 250, // duration + 10, // startsize + 250 + rand() % 400 ); // endsize + } + + VectorMA( origin, 16, dir, sprOrg ); + VectorScale( dir, 100, sprVel ); + + // trying this one just for now just for variety + CG_ParticleExplosion( "explode1", sprOrg, sprVel, + 1200, // duration + 9, // startsize + 250 + rand() % 400 ); // endsize + + CG_AddDebris( origin, dir, + 400 + random() * 300, // speed + 1400, // duration + 7 + rand() % 12 ); // count + } +// jpw + break; + + case WP_GRENADE_SMOKE: // JPW NERVE + case WP_GRENADE_LAUNCHER: + case WP_GRENADE_PINEAPPLE: +// mod = cgs.media.dishFlashModel; +// shader = cgs.media.grenadeExplosionShader; + shader = cgs.media.rocketExplosionShader; // copied from RL + sfx = cgs.media.sfx_rockexp; + mark = cgs.media.burnMarkShader; + markDuration = 60000; + radius = 64; + light = 300; + isSprite = qtrue; + duration = 1000; + lightColor[0] = 0.75; + lightColor[1] = 0.5; + lightColor[2] = 0.1; + + if ( weapon != WP_GRENADE_SMOKE ) { + shakeAmt = 0.15f; + shakeDur = 1000; + shakeRad = 600; + } + + // Ridah, explosion sprite animation + VectorMA( origin, 16, dir, sprOrg ); + VectorScale( dir, 100, sprVel ); + + // RF, testing new explosion animation + CG_ParticleExplosion( "expblue", sprOrg, sprVel, 700, 20, 160 ); + //CG_ParticleExplosion( "twiltb", sprOrg, sprVel, 600, 9, 100 ); + //CG_ParticleExplosion( 3, sprOrg, sprVel, 900, 9, 250 ); +/* + r = 2 + rand()%3; + for (i=0; i<3; i++) { + for (j=0;j<3;j++) sprOrg[j] = origin[j] + 14*dir[j] + 14*crandom(); + CG_ParticleExplosion( 3, sprOrg, sprVel, 800+rand()%250, 9, 60+rand()%200 ); + } +*/ + // Ridah, throw some debris + CG_AddDebris( origin, dir, + 280, // speed + 1400, // duration + // 15 + rand()%5 ); // count + 7 + rand() % 2 ); // count + + break; + case VERYBIGEXPLOSION: + case WP_PANZERFAUST: +// mod = cgs.media.dishFlashModel; +// shader = cgs.media.rocketExplosionShader; + sfx = cgs.media.sfx_rockexp; + mark = cgs.media.burnMarkShader; + markDuration = 60000; + radius = 64; + light = 600; + isSprite = qtrue; + duration = 1000; + // Ridah, changed to flamethrower colors +// lightColor[0] = 1; +// lightColor[1] = 1;//0.75; +// lightColor[2] = 0.6;//0.0; + lightColor[0] = 0.75; + lightColor[1] = 0.5; + lightColor[2] = 0.1; + + // explosion sprite animation + VectorMA( origin, 24, dir, sprOrg ); + VectorScale( dir, 64, sprVel ); + + // cam shake + if ( weapon == VERYBIGEXPLOSION ) { + shakeAmt = 0.2f; + shakeDur = 1000; + shakeRad = 1200; + } else { + shakeAmt = 0.15f; + shakeDur = 800; + shakeRad = 1000; + } + +// JPW NERVE + if ( cg_gameType.integer == GT_SINGLE_PLAYER ) { + if ( weapon == VERYBIGEXPLOSION ) { + CG_ParticleExplosion( "explode1", sprOrg, sprVel, 1200, 20, 300 ); + } else { + CG_ParticleExplosion( "explode1", sprOrg, sprVel, 1400, 40, 70 ); + } + + // NOTE: these must all have the same duration, so that we are less likely to use a wider range of images per scene + r = 2 + rand() % 3; + for ( i = 0; i < 4; i++ ) { + if ( weapon == VERYBIGEXPLOSION ) { + for ( j = 0; j < 3; j++ ) sprOrg[j] = origin[j] + 32 * dir[j] + 32 * crandom(); + CG_ParticleExplosion( "explode1", sprOrg, sprVel, 1200, 40, 160 + rand() % 120 ); + } else if ( i < 2 ) { + for ( j = 0; j < 3; j++ ) sprOrg[j] = origin[j] + 24 * dir[j] + 16 * crandom(); + CG_ParticleExplosion( "explode1", sprOrg, sprVel, 1400, 15, 40 + rand() % 30 ); + } + } + + // Ridah, throw some debris + CG_AddDebris( origin, dir, + 120, // speed + 2000, //350, // duration + // 15 + rand()%5 ); // count + 7 + rand() % 2 ); // count + } +// JPW NERVE -- multiplayer explosions over the top due to large damage radiusesesizes + else { + CG_ParticleExplosion( "explode1", sprOrg, sprVel, 1600, 20, 200 + random() * 400 ); + + // NOTE: these must all have the same duration, so that we are less likely to use a wider range of images per scene + r = 2 + rand() % 3; + for ( i = 0; i < 4; i++ ) { + for ( j = 0; j < 3; j++ ) sprOrg[j] = origin[j] + 160 * crandom(); + CG_ParticleExplosion( "explode1", sprOrg, sprVel, 1600, 40, 260 + rand() % 120 ); + } + + CG_AddDebris( origin, dir, + 400 + random() * 200, // speed + rand() % 2000 + 1000, //350, // duration + // 15 + rand()%5 ); // count + 5 + rand() % 5 ); // count + + } +// jpw +/* + // some sparks + CG_AddSparks( origin, dir, + 200, // speed + 800, // duration + 5 + rand()%10, // count + 0.8 ); // rand scale +*/ + // done. + + break; + + default: + case WP_FLAMETHROWER: + // no explosion at LG impact, it is added with the beam + return; + + break; + + } + // done. + + if ( sfx ) { + trap_S_StartSound( origin, ENTITYNUM_WORLD, CHAN_AUTO, sfx ); + } + +//----(SA) added + if ( sfx2 ) { // distant sounds for weapons with a broadcast fire sound (so you /always/ hear dynamite explosions) + trap_S_StartLocalSound( sfx2, CHAN_AUTO ); + } +//----(SA) end + + + // + // camera shake + // + if ( shakeAmt ) { + CG_StartShakeCamera( shakeAmt, shakeDur, origin, shakeRad ); + } + + + // + // create the explosion + // + if ( mod ) { + le = CG_MakeExplosion( origin, dir, + mod, shader, + duration, isSprite ); + le->light = light; + // Ridah + le->lightOverdraw = lightOverdraw; + VectorCopy( lightColor, le->lightColor ); + } + + // + // impact mark + // + if ( mark ) { + CG_ImpactMark( mark, origin, dir, random() * 360, 1,1,1,1, alphaFade, radius, qfalse, -1 ); + } +} + +/* +============== +CG_MissileHitWallSmall +============== +*/ +void CG_MissileHitWallSmall( int weapon, int clientNum, vec3_t origin, vec3_t dir ) { + qhandle_t mod; + qhandle_t mark; + qhandle_t shader; + sfxHandle_t sfx; + float radius; + float light; + vec3_t lightColor; + localEntity_t *le; +// int r; + qboolean alphaFade; + qboolean isSprite; + int duration; + // Ridah + int lightOverdraw; + vec3_t sprOrg; + vec3_t sprVel; +// int i,j; + + mark = 0; + radius = 32; + sfx = 0; + mod = 0; + shader = 0; + light = 0; + lightColor[0] = 1; + lightColor[1] = 1; + lightColor[2] = 0; + // Ridah + lightOverdraw = 0; + + // set defaults + isSprite = qfalse; + duration = 600; + + shader = cgs.media.rocketExplosionShader; // copied from RL + sfx = cgs.media.sfx_rockexp; + mark = cgs.media.burnMarkShader; + radius = 64; + light = 300; + isSprite = qtrue; + duration = 1000; + lightColor[0] = 0.75; + lightColor[1] = 0.5; + lightColor[2] = 0.1; + + // Ridah, explosion sprite animation + VectorMA( origin, 16, dir, sprOrg ); + VectorScale( dir, 64, sprVel ); + + CG_ParticleExplosion( "explode1", sprOrg, sprVel, 600, 6, 50 ); + + // Ridah, throw some debris + CG_AddDebris( origin, dir, + 280, // speed + 1400, // duration + // 15 + rand()%5 ); // count + 7 + rand() % 2 ); // count + + if ( sfx ) { + trap_S_StartSound( origin, ENTITYNUM_WORLD, CHAN_AUTO, sfx ); + } + + // + // create the explosion + // + if ( mod ) { + le = CG_MakeExplosion( origin, dir, + mod, shader, + duration, isSprite ); + le->light = light; + // Ridah + le->lightOverdraw = lightOverdraw; + VectorCopy( lightColor, le->lightColor ); + } + + // + // impact mark + // + alphaFade = ( mark == cgs.media.energyMarkShader ); // plasma fades alpha, all others fade color + // CG_ImpactMark( mark, origin, dir, random()*360, 1,1,1,1, alphaFade, radius, qfalse, 60000 ); + CG_ImpactMark( mark, origin, dir, random() * 360, 1,1,1,1, alphaFade, radius, qfalse, 0xffffffff ); + + CG_StartShakeCamera( 0.05, 300, origin, 300 ); +} + +/* +================= +CG_MissileHitPlayer +================= +*/ +void CG_MissileHitPlayer( centity_t *cent, int weapon, vec3_t origin, vec3_t dir, int entityNum ) { + int i; + + CG_Bleed( origin, entityNum ); + + // some weapons will make an explosion with the blood, while + // others will just make the blood + switch ( weapon ) { + // knives just make the flesh hit sound. no other effects + case WP_KNIFE: + i = rand() % 4; + if ( cgs.media.sfx_knifehit[i] ) { + trap_S_StartSound( origin, cent->currentState.number, CHAN_WEAPON, cgs.media.sfx_knifehit[i] ); + } + + if ( cent->currentState.number == cg.snap->ps.clientNum ) { + CG_StartShakeCamera( 0.03, 500, origin, 100 ); + } + break; + + case WP_GRENADE_LAUNCHER: + case WP_PANZERFAUST: + // this shake is /on top/ of the shake from the impact (done in CG_MissileHitWall) + CG_StartShakeCamera( 0.1, 500, origin, 100 ); + CG_MissileHitWall( weapon, 0, origin, dir, 0 ); // (SA) modified to send missilehitwall surface parameters + break; + default: + break; + } +} + + + +/* +============================================================================ + +VENOM GUN TRACING + +============================================================================ +*/ + +/* +================ +CG_VenomPellet +================ +*/ +static void CG_VenomPellet( vec3_t start, vec3_t end, int skipNum ) +{} + + +//----(SA) all changes to venom below should be mine +#define DEFAULT_VENOM_COUNT 10 +//#define DEFAULT_VENOM_SPREAD 20 +//#define DEFAULT_VENOM_SPREAD 400 +#define DEFAULT_VENOM_SPREAD 700 + +/* +================ +CG_VenomPattern + +Perform the same traces the server did to locate the +hit splashes (FIXME: random seed isn't synced anymore) + + (SA) right now this is random like a shotgun. I want to make it more + organized so that the pattern is more of a circle (with some degree of randomness) +================ +*/ +static void CG_VenomPattern( vec3_t origin, vec3_t origin2, int otherEntNum ) { + int i; + float r, u; + vec3_t end; + vec3_t forward, right, up; + + // derive the right and up vectors from the forward vector, because + // the client won't have any other information + VectorNormalize2( origin2, forward ); + PerpendicularVector( right, forward ); + CrossProduct( forward, right, up ); + + // generate the "random" spread pattern + for ( i = 0 ; i < DEFAULT_VENOM_COUNT ; i++ ) { + r = crandom() * DEFAULT_VENOM_SPREAD; + u = crandom() * DEFAULT_VENOM_SPREAD; + VectorMA( origin, 8192, forward, end ); + VectorMA( end, r, right, end ); + VectorMA( end, u, up, end ); + + CG_VenomPellet( origin, end, otherEntNum ); + } +} + +/* +============== +CG_VenomFire +============== +*/ +void CG_VenomFire( entityState_t *es, qboolean fullmode ) { + vec3_t v; + int contents; + + VectorSubtract( es->origin2, es->pos.trBase, v ); + VectorNormalize( v ); + VectorScale( v, 32, v ); + VectorAdd( es->pos.trBase, v, v ); + if ( cgs.glconfig.hardwareType != GLHW_RAGEPRO ) { + // ragepro can't alpha fade, so don't even bother with smoke + vec3_t up; + + contents = trap_CM_PointContents( es->pos.trBase, 0 ); + if ( !( contents & CONTENTS_WATER ) ) { + VectorSet( up, 0, 0, 32 ); + if ( fullmode ) { + CG_SmokePuff( v, up, 24, 1, 1, 1, 0.33, 1200, cg.time, 0, 0, cgs.media.shotgunSmokePuffShader ); // LEF_PUFF_DONT_SCALE + } +//----(SA) for the time being don't do the single shot smoke as it's position is funky +// else +// CG_SmokePuff( v, up, 4, 1, 1, 1, 0.33, 700, cg.time, 0, cgs.media.shotgunSmokePuffShader ); + } + } + if ( fullmode ) { + CG_VenomPattern( es->pos.trBase, es->origin2, es->otherEntityNum ); + } +} + +/* +============================================================================ + +BULLETS + +============================================================================ +*/ + +/* +=============== +CG_SpawnTracer +=============== +*/ +void CG_SpawnTracer( int sourceEnt, vec3_t pstart, vec3_t pend ) { + localEntity_t *le; + float dist; + vec3_t dir, ofs; + orientation_t or; + vec3_t start, end; + + VectorCopy( pstart, start ); + VectorCopy( pend, end ); + + VectorSubtract( end, start, dir ); + dist = VectorNormalize( dir ); + + if ( dist < 2.0 * cg_tracerLength.value ) { + return; // segment isnt long enough, dont bother + + } + if ( sourceEnt < cgs.maxclients ) { + // for visual purposes, find the actual tag_weapon for this client + // and offset the start and end accordingly + if ( cg_entities[sourceEnt].currentState.eFlags & EF_MG42_ACTIVE ) { // mounted + start[2] -= 32; // (SA) hack to get the tracer down below the barrel FIXME: do properly + } else { + if ( CG_GetWeaponTag( sourceEnt, "tag_flash", &or ) ) { + VectorSubtract( or.origin, start, ofs ); + if ( VectorLength( ofs ) < 64 ) { + VectorAdd( start, ofs, start ); + //VectorAdd( end, ofs, end ); + } + } + } + } + + // subtract the length of the tracer from the end point, so we dont go through the end point + VectorMA( end, -cg_tracerLength.value, dir, end ); + dist = VectorDistance( start, end ); + + le = CG_AllocLocalEntity(); + le->leType = LE_MOVING_TRACER; + le->startTime = cg.time - ( cg.frametime ? ( rand() % cg.frametime ) / 2 : 0 ); + le->endTime = le->startTime + 1000.0 * dist / cg_tracerSpeed.value; + + le->pos.trType = TR_LINEAR; + le->pos.trTime = le->startTime; + VectorCopy( start, le->pos.trBase ); + VectorScale( dir, cg_tracerSpeed.value, le->pos.trDelta ); +} + +/* +=============== +CG_DrawTracer +=============== +*/ +void CG_DrawTracer( vec3_t start, vec3_t finish ) { + vec3_t forward, right; + polyVert_t verts[4]; + vec3_t line; + + VectorSubtract( finish, start, forward ); + + line[0] = DotProduct( forward, cg.refdef.viewaxis[1] ); + line[1] = DotProduct( forward, cg.refdef.viewaxis[2] ); + + VectorScale( cg.refdef.viewaxis[1], line[1], right ); + VectorMA( right, -line[0], cg.refdef.viewaxis[2], right ); + VectorNormalize( right ); + + VectorMA( finish, cg_tracerWidth.value, right, verts[0].xyz ); + verts[0].st[0] = 1; + verts[0].st[1] = 1; + verts[0].modulate[0] = 255; + verts[0].modulate[1] = 255; + verts[0].modulate[2] = 255; + verts[0].modulate[3] = 255; + + VectorMA( finish, -cg_tracerWidth.value, right, verts[1].xyz ); + verts[1].st[0] = 1; + verts[1].st[1] = 0; + verts[1].modulate[0] = 255; + verts[1].modulate[1] = 255; + verts[1].modulate[2] = 255; + verts[1].modulate[3] = 255; + + VectorMA( start, -cg_tracerWidth.value, right, verts[2].xyz ); + verts[2].st[0] = 0; + verts[2].st[1] = 0; + verts[2].modulate[0] = 255; + verts[2].modulate[1] = 255; + verts[2].modulate[2] = 255; + verts[2].modulate[3] = 255; + + VectorMA( start, cg_tracerWidth.value, right, verts[3].xyz ); + verts[3].st[0] = 0; + verts[3].st[1] = 1; + verts[3].modulate[0] = 255; + verts[3].modulate[1] = 255; + verts[3].modulate[2] = 255; + verts[3].modulate[3] = 255; + + trap_R_AddPolyToScene( cgs.media.tracerShader, 4, verts ); +} + +/* +=============== +CG_Tracer +=============== +*/ +void CG_Tracer( vec3_t source, vec3_t dest, int sparks ) { + float len, begin, end; + vec3_t start, finish; + vec3_t midpoint; + vec3_t forward; + + // tracer + VectorSubtract( dest, source, forward ); + len = VectorNormalize( forward ); + + // start at least a little ways from the muzzle + if ( len < 100 && !sparks ) { + return; + } + begin = 50 + random() * ( len - 60 ); + end = begin + cg_tracerLength.value; + if ( end > len ) { + end = len; + } + VectorMA( source, begin, forward, start ); + VectorMA( source, end, forward, finish ); + + CG_DrawTracer( start, finish ); + + midpoint[0] = ( start[0] + finish[0] ) * 0.5; + midpoint[1] = ( start[1] + finish[1] ) * 0.5; + midpoint[2] = ( start[2] + finish[2] ) * 0.5; + + // add the tracer sound + // trap_S_StartSound( midpoint, ENTITYNUM_WORLD, CHAN_AUTO, cgs.media.tracerSound ); + +} + + +/* +====================== +CG_CalcMuzzlePoint +====================== +*/ +static qboolean CG_CalcMuzzlePoint( int entityNum, vec3_t muzzle ) { + vec3_t forward, right, up; + centity_t *cent; + int anim; + + if ( entityNum == cg.snap->ps.clientNum ) { + VectorCopy( cg.snap->ps.origin, muzzle ); + muzzle[2] += cg.snap->ps.viewheight; + AngleVectors( cg.snap->ps.viewangles, forward, NULL, NULL ); + VectorMA( muzzle, 14, forward, muzzle ); + return qtrue; + } + + cent = &cg_entities[entityNum]; +//----(SA) removed check. is this still necessary? (this way works for ai's firing mg42) should I check for mg42? +// if ( !cent->currentValid ) { +// return qfalse; +// } +//----(SA) end + + VectorCopy( cent->currentState.pos.trBase, muzzle ); + + AngleVectors( cent->currentState.apos.trBase, forward, right, up ); + anim = cent->currentState.legsAnim & ~ANIM_TOGGLEBIT; +// RF, this is all broken by scripting system +// if ( anim == LEGS_WALKCR || anim == LEGS_IDLECR || anim == LEGS_IDLE_ALT ) { +// muzzle[2] += CROUCH_VIEWHEIGHT; +// } else { + muzzle[2] += DEFAULT_VIEWHEIGHT; +// } + + VectorMA( muzzle, 14, forward, muzzle ); + + return qtrue; + +} + +/* +====================== +CG_Bullet + +Renders bullet effects. +====================== +*/ +void CG_Bullet( vec3_t end, int sourceEntityNum, vec3_t normal, qboolean flesh, int fleshEntityNum, qboolean wolfkick, int otherEntNum2 ) { + trace_t trace; + int sourceContentType = 0, destContentType = 0; // TTimo: init + vec3_t dir; + vec3_t start, trend, tmp; // JPW + static int lastBloodSpat; + centity_t *cent; + + cent = &cg_entities[fleshEntityNum]; + + // if the shooter is currently valid, calc a source point and possibly + // do trail effects + if ( sourceEntityNum >= 0 && cg_tracerChance.value > 0 ) { + if ( CG_CalcMuzzlePoint( sourceEntityNum, start ) ) { + sourceContentType = trap_CM_PointContents( start, 0 ); + destContentType = trap_CM_PointContents( end, 0 ); + + // do a complete bubble trail if necessary + if ( ( sourceContentType == destContentType ) && ( sourceContentType & CONTENTS_WATER ) ) { + CG_BubbleTrail( start, end, .5, 8 ); + } + // bubble trail from water into air + else if ( ( sourceContentType & CONTENTS_WATER ) ) { + trap_CM_BoxTrace( &trace, end, start, NULL, NULL, 0, CONTENTS_WATER ); + CG_BubbleTrail( start, trace.endpos, .5, 8 ); + } + // bubble trail from air into water + else if ( ( destContentType & CONTENTS_WATER ) ) { +// CG_Printf( "Dist: %f\n", Distance(cg.refdef.vieworg, end) ); +// CG_AddDirtBulletParticles( end, dir, +// 190, // speed +// 900, // duration +// 5, // count +// 0.5, 80, 16, 0.125, "water_splash" ); // rand scale + // only add bubbles if effect is close to viewer + if ( Distance( cg.snap->ps.origin, end ) < 1024 ) { + trap_CM_BoxTrace( &trace, start, end, NULL, NULL, 0, CONTENTS_WATER ); + CG_BubbleTrail( end, trace.endpos, .5, 8 ); + } + } + + // if not flesh, then do a moving tracer + if ( flesh ) { + // draw a tracer + if ( !wolfkick && random() < cg_tracerChance.value ) { + CG_Tracer( start, end, 0 ); + } + } else { // (not flesh) + if ( otherEntNum2 >= 0 && otherEntNum2 != ENTITYNUM_NONE ) { + CG_SpawnTracer( otherEntNum2, start, end ); + } else { + CG_SpawnTracer( sourceEntityNum, start, end ); + } + } + } + } + + // impact splash and mark + if ( flesh ) { + vec3_t origin; + int aiType; + + aiType = cg_entities[fleshEntityNum].currentState.aiChar; + + if ( fleshEntityNum < MAX_CLIENTS ) { + CG_Bleed( end, fleshEntityNum ); + } + + // play the bullet hit flesh sound + // HACK, if this is not us getting hit, make it quieter + if ( fleshEntityNum == cg.snap->ps.clientNum ) { + + // (SA) TODO: for metal guys, make metal a flag rather than an aitype check? + if ( aiType == AICHAR_PROTOSOLDIER || + aiType == AICHAR_SUPERSOLDIER ) { + CG_SoundPlayIndexedScript( cgs.media.bulletHitFleshMetalScript, NULL, fleshEntityNum ); + } else { + CG_SoundPlayIndexedScript( cgs.media.bulletHitFleshScript, NULL, fleshEntityNum ); + } + } else { + VectorSubtract( cg_entities[fleshEntityNum].lerpOrigin, cg.snap->ps.origin, origin ); + VectorMA( cg.snap->ps.origin, 3, origin, origin ); + if ( aiType == AICHAR_PROTOSOLDIER || + aiType == AICHAR_SUPERSOLDIER ) { + CG_SoundPlayIndexedScript( cgs.media.bulletHitFleshMetalScript, origin, ENTITYNUM_WORLD ); + } else { + CG_SoundPlayIndexedScript( cgs.media.bulletHitFleshScript, origin, ENTITYNUM_WORLD ); + } + } + + /* + // special FX for Zombie + if (cent->currentState.aiChar == AICHAR_ZOMBIE) { + VectorSubtract( end, start, dir ); + VectorNormalize( dir ); + // upper + trap_RB_ZombieFXAddNewHit( cent->currentState.number, end, dir ); + // lower + trap_RB_ZombieFXAddNewHit( cent->currentState.number | (1<<30), end, dir ); + return; + } + */ + + // if we haven't dropped a blood spat in a while, check if this is a good scenario + if ( lastBloodSpat > cg.time || lastBloodSpat < cg.time - 500 ) { + if ( CG_CalcMuzzlePoint( sourceEntityNum, start ) ) { + VectorSubtract( end, start, dir ); + VectorNormalize( dir ); + VectorMA( end, 128, dir, trend ); + trap_CM_BoxTrace( &trace, end, trend, NULL, NULL, 0, MASK_SHOT & ~CONTENTS_BODY ); + + if ( trace.fraction < 1 ) { + CG_ImpactMark( cgs.media.bloodDotShaders[rand() % 5], trace.endpos, trace.plane.normal, random() * 360, + 1,1,1,1, qtrue, 15 + random() * 20, qfalse, cg_bloodTime.integer * 1000 ); + lastBloodSpat = cg.time; + } else if ( lastBloodSpat < cg.time - 1000 ) { + // drop one on the ground? + VectorCopy( end, trend ); + trend[2] -= 64; + trap_CM_BoxTrace( &trace, end, trend, NULL, NULL, 0, MASK_SHOT & ~CONTENTS_BODY ); + + if ( trace.fraction < 1 ) { + CG_ImpactMark( cgs.media.bloodDotShaders[rand() % 5], trace.endpos, trace.plane.normal, random() * 360, + 1,1,1,1, qtrue, 15 + random() * 10, qfalse, cg_bloodTime.integer * 1000 ); + lastBloodSpat = cg.time; + } + } + } + } + + } else { // (not flesh) + int fromweap; + fromweap = cg_entities[sourceEntityNum].currentState.weapon; + + if ( !fromweap || cg_entities[sourceEntityNum].currentState.eFlags & EF_MG42_ACTIVE ) { // mounted + fromweap = WP_MP40; + } + + // TODO: not sure what kind of effect were going to do + if ( wolfkick ) { + return; + } + + // if we didn't hit flesh, spawn a moving tracer + // moved this up to (what seems like) the proper area. trails above. didn't always have valid start/end positions here. +// if (sourceEntityNum >= 0) { +// if(otherEntNum2 >=0 && otherEntNum2 != ENTITYNUM_NONE) +// CG_SpawnTracer( otherEntNum2, start, end ); +// else +// CG_SpawnTracer( sourceEntityNum, start, end ); +// } + + if ( CG_CalcMuzzlePoint( sourceEntityNum, start ) + || cg.snap->ps.persistant[PERS_HWEAPON_USE] ) { + vec3_t start2; + VectorSubtract( end, start, dir ); + VectorNormalize( dir ); + VectorMA( end, -4, dir, start2 ); // back off a little so it doesn't start in solid + VectorMA( end, 64, dir, dir ); + trap_CM_BoxTrace( &trace, start2, dir, NULL, NULL, 0, MASK_SHOT ); + + if ( ( trace.surfaceFlags & SURF_METAL ) || !( rand() % 10 ) || ( otherEntNum2 != ENTITYNUM_NONE ) ) { + // JPW NERVE compute new spark direction from normal & dir (rotate -dir 180 degrees about normal) + + // (SA) NOTE: isn't this done by the server and sent along in the 'normal' that's passed into this routine? (1107 g_weapon.c) + + VectorScale( dir,-1.0f,tmp ); + RotatePointAroundVector( tmp,normal,tmp,180.0f ); + CG_MissileHitWall( fromweap, 0, end, tmp, trace.surfaceFlags ); // sparks // (SA) modified to send missilehitwall surface parameters +// jpw +// CG_MissileHitWall( fromweap, 0, end, normal, trace.surfaceFlags ); // sparks // (SA) modified to send missilehitwall surface parameters + } + if ( !( sourceContentType & CONTENTS_WATER ) && ( destContentType & ( CONTENTS_WATER | CONTENTS_SLIME | CONTENTS_LAVA ) ) ) { // only when shooting /into/ water + trap_CM_BoxTrace( &trace, start, end, NULL, NULL, 0, MASK_WATER ); +// CG_Trace(&trace, start, NULL, NULL, end, -1, MASK_WATER); +// if (!(trace.surfaceFlags & SURF_NOMARKS)) { // check to see if the surface should draw splashes + CG_MissileHitWall( fromweap, 2, trace.endpos, trace.plane.normal, trace.surfaceFlags ); +// } + } else { + CG_MissileHitWall( fromweap, 1, end, normal, trace.surfaceFlags ); // smoke puff // (SA) modified to send missilehitwall surface parameters + + if ( 0 ) { + localEntity_t *le; + le = CG_AllocLocalEntity(); + le->leType = LE_EMITTER; + le->startTime = cg.time; + le->endTime = le->startTime + 20000; + le->pos.trType = TR_STATIONARY; + VectorCopy( end, le->pos.trBase ); + VectorCopy( normal, le->angles.trBase ); + le->ownerNum = 0; + } + } + } + } + +} + +/* +============ +CG_ClientDamage +============ +*/ +void CG_ClientDamage( int entnum, int enemynum, int id ) { + if ( id > CLDMG_MAX ) { + CG_Error( "CG_ClientDamage: unknown damage type: %i\n", id ); + } + + // NERVE - SMF - clientDamage commands are now sent through usercmds for multiplayer + if ( cgs.gametype == GT_WOLF ) { + if ( entnum == cg.snap->ps.clientNum ) { + // NOTE: MAX_CLIENTS currently only needs 7 bits, the rest is for id tag + cg.cld = id << 7; + cg.cld |= enemynum; + } + } + // -NERVE - SMF + else { + trap_SendClientCommand( va( "cld %i %i %i", entnum, enemynum, id ) ); + } +} + diff --git a/src/cgame/cgame.def b/src/cgame/cgame.def new file mode 100644 index 0000000..2ee748e --- /dev/null +++ b/src/cgame/cgame.def @@ -0,0 +1,3 @@ +EXPORTS + vmMain + dllEntry diff --git a/src/cgame/cgame.vcproj b/src/cgame/cgame.vcproj new file mode 100644 index 0000000..7b70149 --- /dev/null +++ b/src/cgame/cgame.vcproj @@ -0,0 +1,914 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/cgame/tr_types.h b/src/cgame/tr_types.h new file mode 100644 index 0000000..ed90d9e --- /dev/null +++ b/src/cgame/tr_types.h @@ -0,0 +1,343 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +#ifndef __TR_TYPES_H +#define __TR_TYPES_H + + +#define MAX_CORONAS 32 //----(SA) not really a reason to limit this other than trying to keep a reasonable count +#define MAX_DLIGHTS 32 // can't be increased, because bit flags are used on surfaces +#define MAX_ENTITIES 1023 // can't be increased without changing drawsurf bit packing + +// renderfx flags +#define RF_MINLIGHT 1 // allways have some light (viewmodel, some items) +#define RF_THIRD_PERSON 2 // don't draw through eyes, only mirrors (player bodies, chat sprites) +#define RF_FIRST_PERSON 4 // only draw through eyes (view weapon, damage blood blob) +#define RF_DEPTHHACK 8 // for view weapon Z crunching +#define RF_NOSHADOW 64 // don't add stencil shadows + +#define RF_LIGHTING_ORIGIN 128 // use refEntity->lightingOrigin instead of refEntity->origin + // for lighting. This allows entities to sink into the floor + // with their origin going solid, and allows all parts of a + // player to get the same lighting +#define RF_SHADOW_PLANE 256 // use refEntity->shadowPlane +#define RF_WRAP_FRAMES 512 // mod the model frames by the maxframes to allow continuous + // animation without needing to know the frame count + +#define RF_HILIGHT ( 1 << 8 ) // more than RF_MINLIGHT. For when an object is "Highlighted" (looked at/training identification/etc) +#define RF_BLINK ( 1 << 9 ) // eyes in 'blink' state + +// refdef flags +#define RDF_NOWORLDMODEL 1 // used for player configuration screen +#define RDF_HYPERSPACE 4 // teleportation effect + +// Rafael +#define RDF_SKYBOXPORTAL 8 + +#define RDF_DRAWSKYBOX 16 // the above marks a scene as being a 'portal sky'. this flag says to draw it or not + +//----(SA) +#define RDF_UNDERWATER ( 1 << 4 ) // so the renderer knows to use underwater fog when the player is underwater +#define RDF_DRAWINGSKY ( 1 << 5 ) +#define RDF_SNOOPERVIEW ( 1 << 6 ) //----(SA) added + + +typedef struct { + vec3_t xyz; + float st[2]; + byte modulate[4]; +} polyVert_t; + +typedef struct poly_s { + qhandle_t hShader; + int numVerts; + polyVert_t *verts; +} poly_t; + +typedef enum { + RT_MODEL, + RT_POLY, + RT_SPRITE, + RT_SPLASH, // ripple effect + RT_BEAM, + RT_RAIL_CORE, + RT_RAIL_CORE_TAPER, // a modified core that creates a properly texture mapped core that's wider at one end + RT_RAIL_RINGS, + RT_LIGHTNING, + RT_PORTALSURFACE, // doesn't draw anything, just info for portals + + RT_MAX_REF_ENTITY_TYPE +} refEntityType_t; + +#define ZOMBIEFX_FADEOUT_TIME 10000 + +#define REFLAG_ONLYHAND 1 // only draw hand surfaces +#define REFLAG_ZOMBIEFX 2 // special post-tesselation processing for zombie skin +#define REFLAG_ZOMBIEFX2 4 // special post-tesselation processing for zombie skin +#define REFLAG_FORCE_LOD 8 // force a low lod +#define REFLAG_ORIENT_LOD 16 // on LOD switch, align the model to the player's camera +#define REFLAG_DEAD_LOD 32 // allow the LOD to go lower than recommended +#define REFLAG_SCALEDSPHERECULL 64 // on LOD switch, align the model to the player's camera +#define REFLAG_FULL_LOD 8 // force a FULL lod + +typedef struct { + refEntityType_t reType; + int renderfx; + + qhandle_t hModel; // opaque type outside refresh + + // most recent data + vec3_t lightingOrigin; // so multi-part models can be lit identically (RF_LIGHTING_ORIGIN) + float shadowPlane; // projection shadows go here, stencils go slightly lower + + vec3_t axis[3]; // rotation vectors + vec3_t torsoAxis[3]; // rotation vectors for torso section of skeletal animation + qboolean nonNormalizedAxes; // axis are not normalized, i.e. they have scale + float origin[3]; // also used as MODEL_BEAM's "from" + int frame; // also used as MODEL_BEAM's diameter + int torsoFrame; // skeletal torso can have frame independant of legs frame + + vec3_t scale; //----(SA) added + + // previous data for frame interpolation + float oldorigin[3]; // also used as MODEL_BEAM's "to" + int oldframe; + int oldTorsoFrame; + float backlerp; // 0.0 = current, 1.0 = old + float torsoBacklerp; + + // texturing + int skinNum; // inline skin index + qhandle_t customSkin; // NULL for default skin + qhandle_t customShader; // use one image for the entire thing + + // misc + byte shaderRGBA[4]; // colors used by rgbgen entity shaders + float shaderTexCoord[2]; // texture coordinates used by tcMod entity modifiers + float shaderTime; // subtracted from refdef time to control effect start times + + // extra sprite information + float radius; + float rotation; + + // Ridah + vec3_t fireRiseDir; + + // Ridah, entity fading (gibs, debris, etc) + int fadeStartTime, fadeEndTime; + + float hilightIntensity; //----(SA) added + + int reFlags; + + int entityNum; // currentState.number, so we can attach rendering effects to specific entities (Zombie) + +} refEntity_t; + +//----(SA) + +// // +// WARNING:: synch FOG_SERVER in sv_ccmds.c if you change anything // +// // +typedef enum { + FOG_NONE, // 0 + + FOG_SKY, // 1 fog values to apply to the sky when using density fog for the world (non-distance clipping fog) (only used if(glfogsettings[FOG_MAP].registered) or if(glfogsettings[FOG_MAP].registered)) + FOG_PORTALVIEW, // 2 used by the portal sky scene + FOG_HUD, // 3 used by the 3D hud scene + + // The result of these for a given frame is copied to the scene.glFog when the scene is rendered + + // the following are fogs applied to the main world scene + FOG_MAP, // 4 use fog parameter specified using the "fogvars" in the sky shader + FOG_WATER, // 5 used when underwater + FOG_SERVER, // 6 the server has set my fog (probably a target_fog) (keep synch in sv_ccmds.c !!!) + FOG_CURRENT, // 7 stores the current values when a transition starts + FOG_LAST, // 8 stores the current values when a transition starts + FOG_TARGET, // 9 the values it's transitioning to. + + FOG_CMD_SWITCHFOG, // 10 transition to the fog specified in the second parameter of R_SetFog(...) (keep synch in sv_ccmds.c !!!) + + NUM_FOGS +} glfogType_t; + + +typedef struct { + int mode; // GL_LINEAR, GL_EXP + int hint; // GL_DONT_CARE + int startTime; // in ms + int finishTime; // in ms + float color[4]; + float start; // near + float end; // far + qboolean useEndForClip; // use the 'far' value for the far clipping plane + float density; // 0.0-1.0 + qboolean registered; // has this fog been set up? + qboolean drawsky; // draw skybox + qboolean clearscreen; // clear the GL color buffer + + int dirty; +} glfog_t; + +//----(SA) end + + +#define MAX_RENDER_STRINGS 8 +#define MAX_RENDER_STRING_LENGTH 32 + +typedef struct { + int x, y, width, height; + float fov_x, fov_y; + vec3_t vieworg; + vec3_t viewaxis[3]; // transformation matrix + + int time; // time in milliseconds for shader effects and other time dependent rendering issues + int rdflags; // RDF_NOWORLDMODEL, etc + + // 1 bits will prevent the associated area from rendering at all + byte areamask[MAX_MAP_AREA_BYTES]; + + + + + // text messages for deform text shaders + char text[MAX_RENDER_STRINGS][MAX_RENDER_STRING_LENGTH]; + + +//----(SA) added (needed to pass fog infos into the portal sky scene) + glfog_t glfog; +//----(SA) end + +} refdef_t; + + +typedef enum { + STEREO_CENTER, + STEREO_LEFT, + STEREO_RIGHT +} stereoFrame_t; + + +/* +** glconfig_t +** +** Contains variables specific to the OpenGL configuration +** being run right now. These are constant once the OpenGL +** subsystem is initialized. +*/ +typedef enum { + TC_NONE, + TC_S3TC, + TC_EXT_COMP_S3TC +} textureCompression_t; + +typedef enum { + GLDRV_ICD, // driver is integrated with window system + // WARNING: there are tests that check for + // > GLDRV_ICD for minidriverness, so this + // should always be the lowest value in this + // enum set + GLDRV_STANDALONE, // driver is a non-3Dfx standalone driver + GLDRV_VOODOO // driver is a 3Dfx standalone driver +} glDriverType_t; + +typedef enum { + GLHW_GENERIC, // where everthing works the way it should + GLHW_3DFX_2D3D, // Voodoo Banshee or Voodoo3, relevant since if this is + // the hardware type then there can NOT exist a secondary + // display adapter + GLHW_RIVA128, // where you can't interpolate alpha + GLHW_RAGEPRO, // where you can't modulate alpha on alpha textures + GLHW_PERMEDIA2 // where you don't have src*dst +} glHardwareType_t; + +typedef struct { + char renderer_string[MAX_STRING_CHARS]; + char vendor_string[MAX_STRING_CHARS]; + char version_string[MAX_STRING_CHARS]; + char extensions_string[4 * MAX_STRING_CHARS]; // this is actually too short for many current cards/drivers // (SA) doubled from 2x to 4x MAX_STRING_CHARS + + int maxTextureSize; // queried from GL + int maxActiveTextures; // multitexture ability + + int colorBits, depthBits, stencilBits; + + glDriverType_t driverType; + glHardwareType_t hardwareType; + + qboolean deviceSupportsGamma; + textureCompression_t textureCompression; + qboolean textureEnvAddAvailable; + qboolean anisotropicAvailable; //----(SA) added + float maxAnisotropy; //----(SA) added + + // vendor-specific support + // NVidia + qboolean NVFogAvailable; //----(SA) added + int NVFogMode; //----(SA) added + // ATI + int ATIMaxTruformTess; // for truform support + int ATINormalMode; // for truform support + int ATIPointMode; // for truform support + + + int vidWidth, vidHeight; + // aspect is the screen's physical width / height, which may be different + // than scrWidth / scrHeight if the pixels are non-square + // normal screens should be 4/3, but wide aspect monitors may be 16/9 + float windowAspect; + + int displayFrequency; + + // synonymous with "does rendering consume the entire screen?", therefore + // a Voodoo or Voodoo2 will have this set to TRUE, as will a Win32 ICD that + // used CDS. + qboolean isFullscreen; + qboolean stereoEnabled; + qboolean smpActive; // dual processor + + qboolean textureFilterAnisotropicAvailable; //DAJ +} glconfig_t; + + +#if !defined _WIN32 + +#define _3DFX_DRIVER_NAME "libMesaVoodooGL.so.3.1" +#define OPENGL_DRIVER_NAME "libGL.so.1" + +#else + +#define _3DFX_DRIVER_NAME "3dfxvgl" +#define OPENGL_DRIVER_NAME "opengl32" +#define WICKED3D_V5_DRIVER_NAME "gl/openglv5.dll" +#define WICKED3D_V3_DRIVER_NAME "gl/openglv3.dll" + +#endif // !defined _WIN32 + + +#endif // __TR_TYPES_H diff --git a/src/client/cl_cgame.c b/src/client/cl_cgame.c new file mode 100644 index 0000000..e9e9886 --- /dev/null +++ b/src/client/cl_cgame.c @@ -0,0 +1,1289 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// cl_cgame.c -- client system interaction with client game + +#include "client.h" + +#include "../game/botlib.h" + +extern botlib_export_t *botlib_export; + +extern qboolean loadCamera( int camNum, const char *name ); +extern void startCamera( int camNum, int time ); +extern qboolean getCameraInfo( int camNum, int time, vec3_t *origin, vec3_t *angles, float *fov ); + +// RF, this is only used when running a local server +extern void SV_SendMoveSpeedsToGame( int entnum, char *text ); +extern qboolean SV_GetModelInfo( int clientNum, char *modelName, animModelInfo_t **modelInfo ); + + +/* +==================== +CL_GetGameState +==================== +*/ +void CL_GetGameState( gameState_t *gs ) { + *gs = cl.gameState; +} + +/* +==================== +CL_GetGlconfig +==================== +*/ +void CL_GetGlconfig( glconfig_t *glconfig ) { + *glconfig = cls.glconfig; +} + + +/* +==================== +CL_GetUserCmd +==================== +*/ +qboolean CL_GetUserCmd( int cmdNumber, usercmd_t *ucmd ) { + // cmds[cmdNumber] is the last properly generated command + + // can't return anything that we haven't created yet + if ( cmdNumber > cl.cmdNumber ) { + Com_Error( ERR_DROP, "CL_GetUserCmd: %i >= %i", cmdNumber, cl.cmdNumber ); + } + + // the usercmd has been overwritten in the wrapping + // buffer because it is too far out of date + if ( cmdNumber <= cl.cmdNumber - CMD_BACKUP ) { + return qfalse; + } + + *ucmd = cl.cmds[ cmdNumber & CMD_MASK ]; + + return qtrue; +} + +int CL_GetCurrentCmdNumber( void ) { + return cl.cmdNumber; +} + + +/* +==================== +CL_GetParseEntityState +==================== +*/ +qboolean CL_GetParseEntityState( int parseEntityNumber, entityState_t *state ) { + // can't return anything that hasn't been parsed yet + if ( parseEntityNumber >= cl.parseEntitiesNum ) { + Com_Error( ERR_DROP, "CL_GetParseEntityState: %i >= %i", + parseEntityNumber, cl.parseEntitiesNum ); + } + + // can't return anything that has been overwritten in the circular buffer + if ( parseEntityNumber <= cl.parseEntitiesNum - MAX_PARSE_ENTITIES ) { + return qfalse; + } + + *state = cl.parseEntities[ parseEntityNumber & ( MAX_PARSE_ENTITIES - 1 ) ]; + return qtrue; +} + +/* +==================== +CL_GetCurrentSnapshotNumber +==================== +*/ +void CL_GetCurrentSnapshotNumber( int *snapshotNumber, int *serverTime ) { + *snapshotNumber = cl.snap.messageNum; + *serverTime = cl.snap.serverTime; +} + +/* +==================== +CL_GetSnapshot +==================== +*/ +qboolean CL_GetSnapshot( int snapshotNumber, snapshot_t *snapshot ) { + clSnapshot_t *clSnap; + int i, count; + + if ( snapshotNumber > cl.snap.messageNum ) { + Com_Error( ERR_DROP, "CL_GetSnapshot: snapshotNumber > cl.snapshot.messageNum" ); + } + + // if the frame has fallen out of the circular buffer, we can't return it + if ( cl.snap.messageNum - snapshotNumber >= PACKET_BACKUP ) { + return qfalse; + } + + // if the frame is not valid, we can't return it + clSnap = &cl.snapshots[snapshotNumber & PACKET_MASK]; + if ( !clSnap->valid ) { + return qfalse; + } + + // if the entities in the frame have fallen out of their + // circular buffer, we can't return it + if ( cl.parseEntitiesNum - clSnap->parseEntitiesNum >= MAX_PARSE_ENTITIES ) { + return qfalse; + } + + // write the snapshot + snapshot->snapFlags = clSnap->snapFlags; + snapshot->serverCommandSequence = clSnap->serverCommandNum; + snapshot->ping = clSnap->ping; + snapshot->serverTime = clSnap->serverTime; + memcpy( snapshot->areamask, clSnap->areamask, sizeof( snapshot->areamask ) ); + snapshot->ps = clSnap->ps; + count = clSnap->numEntities; + if ( count > MAX_ENTITIES_IN_SNAPSHOT ) { + Com_DPrintf( "CL_GetSnapshot: truncated %i entities to %i\n", count, MAX_ENTITIES_IN_SNAPSHOT ); + count = MAX_ENTITIES_IN_SNAPSHOT; + } + snapshot->numEntities = count; + for ( i = 0 ; i < count ; i++ ) { + snapshot->entities[i] = + cl.parseEntities[ ( clSnap->parseEntitiesNum + i ) & ( MAX_PARSE_ENTITIES - 1 ) ]; + } + + // FIXME: configstring changes and server commands!!! + + return qtrue; +} + +/* +============== +CL_SetUserCmdValue +============== +*/ +void CL_SetUserCmdValue( int userCmdValue, int holdableValue, float sensitivityScale, int cld ) { + cl.cgameUserCmdValue = userCmdValue; + cl.cgameUserHoldableValue = holdableValue; + cl.cgameSensitivity = sensitivityScale; + cl.cgameCld = cld; +} + +/* +============== +CL_AddCgameCommand +============== +*/ +void CL_AddCgameCommand( const char *cmdName ) { + Cmd_AddCommand( cmdName, NULL ); +} + +/* +============== +CL_CgameError +============== +*/ +void CL_CgameError( const char *string ) { + Com_Error( ERR_DROP, "%s", string ); +} + + +/* +===================== +CL_ConfigstringModified +===================== +*/ +void CL_ConfigstringModified( void ) { + char *old, *s; + int i, index; + char *dup; + gameState_t oldGs; + int len; + + index = atoi( Cmd_Argv( 1 ) ); + if ( index < 0 || index >= MAX_CONFIGSTRINGS ) { + Com_Error( ERR_DROP, "configstring > MAX_CONFIGSTRINGS" ); + } +// s = Cmd_Argv(2); + // get everything after "cs " + s = Cmd_ArgsFrom( 2 ); + + old = cl.gameState.stringData + cl.gameState.stringOffsets[ index ]; + if ( !strcmp( old, s ) ) { + return; // unchanged + } + + // build the new gameState_t + oldGs = cl.gameState; + + memset( &cl.gameState, 0, sizeof( cl.gameState ) ); + + // leave the first 0 for uninitialized strings + cl.gameState.dataCount = 1; + + for ( i = 0 ; i < MAX_CONFIGSTRINGS ; i++ ) { + if ( i == index ) { + dup = s; + } else { + dup = oldGs.stringData + oldGs.stringOffsets[ i ]; + } + if ( !dup[0] ) { + continue; // leave with the default empty string + } + + len = strlen( dup ); + + if ( len + 1 + cl.gameState.dataCount > MAX_GAMESTATE_CHARS ) { + Com_Error( ERR_DROP, "MAX_GAMESTATE_CHARS exceeded" ); + } + + // append it to the gameState string buffer + cl.gameState.stringOffsets[ i ] = cl.gameState.dataCount; + memcpy( cl.gameState.stringData + cl.gameState.dataCount, dup, len + 1 ); + cl.gameState.dataCount += len + 1; + } + + if ( index == CS_SYSTEMINFO ) { + // parse serverId and other cvars + CL_SystemInfoChanged(); + } + +} + + +/* +=================== +CL_GetServerCommand + +Set up argc/argv for the given command +=================== +*/ +qboolean CL_GetServerCommand( int serverCommandNumber ) { + char *s; + char *cmd; + static char bigConfigString[BIG_INFO_STRING]; + + // if we have irretrievably lost a reliable command, drop the connection + if ( serverCommandNumber <= clc.serverCommandSequence - MAX_RELIABLE_COMMANDS ) { + // when a demo record was started after the client got a whole bunch of + // reliable commands then the client never got those first reliable commands + if ( clc.demoplaying ) { + return qfalse; + } + Com_Error( ERR_DROP, "CL_GetServerCommand: a reliable command was cycled out" ); + return qfalse; + } + + if ( serverCommandNumber > clc.serverCommandSequence ) { + Com_Error( ERR_DROP, "CL_GetServerCommand: requested a command not received" ); + return qfalse; + } + + s = clc.serverCommands[ serverCommandNumber & ( MAX_RELIABLE_COMMANDS - 1 ) ]; + clc.lastExecutedServerCommand = serverCommandNumber; + + Com_DPrintf( "serverCommand: %i : %s\n", serverCommandNumber, s ); + +rescan: + Cmd_TokenizeString( s ); + cmd = Cmd_Argv( 0 ); + + if ( !strcmp( cmd, "disconnect" ) ) { + Com_Error( ERR_SERVERDISCONNECT,"Server disconnected\n" ); + } + + if ( !strcmp( cmd, "bcs0" ) ) { + Com_sprintf( bigConfigString, BIG_INFO_STRING, "cs %s \"%s", Cmd_Argv( 1 ), Cmd_Argv( 2 ) ); + return qfalse; + } + + if ( !strcmp( cmd, "bcs1" ) ) { + s = Cmd_Argv( 2 ); + if ( strlen( bigConfigString ) + strlen( s ) >= BIG_INFO_STRING ) { + Com_Error( ERR_DROP, "bcs exceeded BIG_INFO_STRING" ); + } + strcat( bigConfigString, s ); + return qfalse; + } + + if ( !strcmp( cmd, "bcs2" ) ) { + s = Cmd_Argv( 2 ); + if ( strlen( bigConfigString ) + strlen( s ) + 1 >= BIG_INFO_STRING ) { + Com_Error( ERR_DROP, "bcs exceeded BIG_INFO_STRING" ); + } + strcat( bigConfigString, s ); + strcat( bigConfigString, "\"" ); + s = bigConfigString; + goto rescan; + } + + if ( !strcmp( cmd, "cs" ) ) { + CL_ConfigstringModified(); + // reparse the string, because CL_ConfigstringModified may have done another Cmd_TokenizeString() + Cmd_TokenizeString( s ); + return qtrue; + } + + if ( !strcmp( cmd, "map_restart" ) ) { + // clear notify lines and outgoing commands before passing + // the restart to the cgame + Con_ClearNotify(); + memset( cl.cmds, 0, sizeof( cl.cmds ) ); + return qtrue; + } + + if ( !strcmp( cmd, "popup" ) ) { // direct server to client popup request, bypassing cgame +// trap_UI_Popup(Cmd_Argv(1)); +// if ( cls.state == CA_ACTIVE && !clc.demoplaying ) { +// VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_CLIPBOARD); +// Menus_OpenByName(Cmd_Argv(1)); +// } + return qfalse; + } + + + // the clientLevelShot command is used during development + // to generate 128*128 screenshots from the intermission + // point of levels for the menu system to use + // we pass it along to the cgame to make apropriate adjustments, + // but we also clear the console and notify lines here + if ( !strcmp( cmd, "clientLevelShot" ) ) { + // don't do it if we aren't running the server locally, + // otherwise malicious remote servers could overwrite + // the existing thumbnails + if ( !com_sv_running->integer ) { + return qfalse; + } + // close the console + Con_Close(); + // take a special screenshot next frame + Cbuf_AddText( "wait ; wait ; wait ; wait ; screenshot levelshot\n" ); + return qtrue; + } + + // we may want to put a "connect to other server" command here + + // cgame can now act on the command + return qtrue; +} + + +/* +==================== +CL_CM_LoadMap + +Just adds default parameters that cgame doesn't need to know about +==================== +*/ +void CL_CM_LoadMap( const char *mapname ) { + int checksum; + + CM_LoadMap( mapname, qtrue, &checksum ); +} + +/* +==================== +CL_ShutdonwCGame + +==================== +*/ +void CL_ShutdownCGame( void ) { + cls.keyCatchers &= ~KEYCATCH_CGAME; + cls.cgameStarted = qfalse; + if ( !cgvm ) { + return; + } + VM_Call( cgvm, CG_SHUTDOWN ); + VM_Free( cgvm ); + cgvm = NULL; +} + +static int FloatAsInt( float f ) { + int temp; + + *(float *)&temp = f; + + return temp; +} + +/* +==================== +CL_CgameSystemCalls + +The cgame module is making a system call +==================== +*/ +#define VMA( x ) VM_ArgPtr( args[x] ) +#define VMF( x ) ( (float *)args )[x] +int CL_CgameSystemCalls( int *args ) { + switch ( args[0] ) { + case CG_PRINT: + Com_Printf( "%s", VMA( 1 ) ); + return 0; + case CG_ERROR: + Com_Error( ERR_DROP, "%s", VMA( 1 ) ); + return 0; + case CG_MILLISECONDS: + return Sys_Milliseconds(); + case CG_CVAR_REGISTER: + Cvar_Register( VMA( 1 ), VMA( 2 ), VMA( 3 ), args[4] ); + return 0; + case CG_CVAR_UPDATE: + Cvar_Update( VMA( 1 ) ); + return 0; + case CG_CVAR_SET: + Cvar_Set( VMA( 1 ), VMA( 2 ) ); + return 0; + case CG_CVAR_VARIABLESTRINGBUFFER: + Cvar_VariableStringBuffer( VMA( 1 ), VMA( 2 ), args[3] ); + return 0; + case CG_ARGC: + return Cmd_Argc(); + case CG_ARGV: + Cmd_ArgvBuffer( args[1], VMA( 2 ), args[3] ); + return 0; + case CG_ARGS: + Cmd_ArgsBuffer( VMA( 1 ), args[2] ); + return 0; + case CG_FS_FOPENFILE: + return FS_FOpenFileByMode( VMA( 1 ), VMA( 2 ), args[3] ); + case CG_FS_READ: + FS_Read( VMA( 1 ), args[2], args[3] ); + return 0; + case CG_FS_WRITE: + return FS_Write( VMA( 1 ), args[2], args[3] ); + case CG_FS_FCLOSEFILE: + FS_FCloseFile( args[1] ); + return 0; + case CG_SENDCONSOLECOMMAND: + Cbuf_AddText( VMA( 1 ) ); + return 0; + case CG_ADDCOMMAND: + CL_AddCgameCommand( VMA( 1 ) ); + return 0; + case CG_REMOVECOMMAND: + Cmd_RemoveCommand( VMA( 1 ) ); + return 0; + case CG_SENDCLIENTCOMMAND: + CL_AddReliableCommand( VMA( 1 ) ); + return 0; + case CG_UPDATESCREEN: + // this is used during lengthy level loading, so pump message loop +// Com_EventLoop(); // FIXME: if a server restarts here, BAD THINGS HAPPEN! +// We can't call Com_EventLoop here, a restart will crash and this _does_ happen +// if there is a map change while we are downloading at pk3. +// ZOID + SCR_UpdateScreen(); + return 0; + case CG_CM_LOADMAP: + CL_CM_LoadMap( VMA( 1 ) ); + return 0; + case CG_CM_NUMINLINEMODELS: + return CM_NumInlineModels(); + case CG_CM_INLINEMODEL: + return CM_InlineModel( args[1] ); + case CG_CM_TEMPBOXMODEL: + return CM_TempBoxModel( VMA( 1 ), VMA( 2 ), qfalse ); + case CG_CM_TEMPCAPSULEMODEL: + return CM_TempBoxModel( VMA( 1 ), VMA( 2 ), qtrue ); + case CG_CM_POINTCONTENTS: + return CM_PointContents( VMA( 1 ), args[2] ); + case CG_CM_TRANSFORMEDPOINTCONTENTS: + return CM_TransformedPointContents( VMA( 1 ), args[2], VMA( 3 ), VMA( 4 ) ); + case CG_CM_BOXTRACE: + CM_BoxTrace( VMA( 1 ), VMA( 2 ), VMA( 3 ), VMA( 4 ), VMA( 5 ), args[6], args[7], /*int capsule*/ qfalse ); + return 0; + case CG_CM_TRANSFORMEDBOXTRACE: + CM_TransformedBoxTrace( VMA( 1 ), VMA( 2 ), VMA( 3 ), VMA( 4 ), VMA( 5 ), args[6], args[7], VMA( 8 ), VMA( 9 ), /*int capsule*/ qfalse ); + return 0; + case CG_CM_CAPSULETRACE: + CM_BoxTrace( VMA( 1 ), VMA( 2 ), VMA( 3 ), VMA( 4 ), VMA( 5 ), args[6], args[7], /*int capsule*/ qtrue ); + return 0; + case CG_CM_TRANSFORMEDCAPSULETRACE: + CM_TransformedBoxTrace( VMA( 1 ), VMA( 2 ), VMA( 3 ), VMA( 4 ), VMA( 5 ), args[6], args[7], VMA( 8 ), VMA( 9 ), /*int capsule*/ qtrue ); + return 0; + case CG_CM_MARKFRAGMENTS: + return re.MarkFragments( args[1], VMA( 2 ), VMA( 3 ), args[4], VMA( 5 ), args[6], VMA( 7 ) ); + case CG_S_STARTSOUND: + S_StartSound( VMA( 1 ), args[2], args[3], args[4] ); + return 0; +//----(SA) added + case CG_S_STARTSOUNDEX: + S_StartSoundEx( VMA( 1 ), args[2], args[3], args[4], args[5] ); + return 0; +//----(SA) end + case CG_S_STARTLOCALSOUND: + S_StartLocalSound( args[1], args[2] ); + return 0; + case CG_S_CLEARLOOPINGSOUNDS: + S_ClearLoopingSounds(); // (SA) modified so no_pvs sounds can function + // RF, if killall, then stop all sounds + if ( args[1] == 1 ) { + S_ClearSounds( qtrue, qfalse ); + } else if ( args[1] == 2 ) { + S_ClearSounds( qtrue, qtrue ); + } + return 0; + case CG_S_ADDLOOPINGSOUND: + // FIXME MrE: handling of looping sounds changed + S_AddLoopingSound( args[1], VMA( 2 ), VMA( 3 ), args[4], args[5], args[6] ); + return 0; +// not in use +// case CG_S_ADDREALLOOPINGSOUND: +// S_AddLoopingSound( args[1], VMA(2), VMA(3), args[4], args[5], args[6] ); +// //S_AddRealLoopingSound( args[1], VMA(2), VMA(3), args[4], args[5] ); +// return 0; + +//----(SA) added + case CG_S_STOPSTREAMINGSOUND: + S_StopEntStreamingSound( args[1] ); + return 0; +//----(SA) end + + case CG_S_STOPLOOPINGSOUND: + // RF, not functional anymore, since we reverted to old looping code + //S_StopLoopingSound( args[1] ); + return 0; + case CG_S_UPDATEENTITYPOSITION: + S_UpdateEntityPosition( args[1], VMA( 2 ) ); + return 0; +// Ridah, talking animations + case CG_S_GETVOICEAMPLITUDE: + return S_GetVoiceAmplitude( args[1] ); +// done. + case CG_S_RESPATIALIZE: + S_Respatialize( args[1], VMA( 2 ), VMA( 3 ), args[4] ); + return 0; + case CG_S_REGISTERSOUND: +#ifdef DOOMSOUND ///// (SA) DOOMSOUND + return S_RegisterSound( VMA( 1 ) ); +#else + return S_RegisterSound( VMA( 1 ), qfalse ); +#endif ///// (SA) DOOMSOUND + case CG_S_STARTBACKGROUNDTRACK: + S_StartBackgroundTrack( VMA( 1 ), VMA( 2 ), args[3] ); //----(SA) added fadeup time + return 0; + case CG_S_FADESTREAMINGSOUND: + S_FadeStreamingSound( VMF( 1 ), args[2], args[3] ); //----(SA) added music/all-streaming options + return 0; + case CG_S_STARTSTREAMINGSOUND: + S_StartStreamingSound( VMA( 1 ), VMA( 2 ), args[3], args[4], args[5] ); + return 0; + case CG_S_FADEALLSOUNDS: + S_FadeAllSounds( VMF( 1 ), args[2] ); //----(SA) added + return 0; + case CG_R_LOADWORLDMAP: + re.LoadWorld( VMA( 1 ) ); + return 0; + case CG_R_REGISTERMODEL: + return re.RegisterModel( VMA( 1 ) ); + case CG_R_REGISTERSKIN: + return re.RegisterSkin( VMA( 1 ) ); + + //----(SA) added + case CG_R_GETSKINMODEL: + return re.GetSkinModel( args[1], VMA( 2 ), VMA( 3 ) ); + case CG_R_GETMODELSHADER: + return re.GetShaderFromModel( args[1], args[2], args[3] ); + //----(SA) end + + case CG_R_REGISTERSHADER: + return re.RegisterShader( VMA( 1 ) ); + case CG_R_REGISTERFONT: + re.RegisterFont( VMA( 1 ), args[2], VMA( 3 ) ); + case CG_R_REGISTERSHADERNOMIP: + return re.RegisterShaderNoMip( VMA( 1 ) ); + case CG_R_CLEARSCENE: + re.ClearScene(); + return 0; + case CG_R_ADDREFENTITYTOSCENE: + re.AddRefEntityToScene( VMA( 1 ) ); + return 0; + case CG_R_ADDPOLYTOSCENE: + re.AddPolyToScene( args[1], args[2], VMA( 3 ) ); + return 0; + // Ridah + case CG_R_ADDPOLYSTOSCENE: + re.AddPolysToScene( args[1], args[2], VMA( 3 ), args[4] ); + return 0; + case CG_RB_ZOMBIEFXADDNEWHIT: + re.ZombieFXAddNewHit( args[1], VMA( 2 ), VMA( 3 ) ); + return 0; + // done. +// case CG_R_LIGHTFORPOINT: +// return re.LightForPoint( VMA(1), VMA(2), VMA(3), VMA(4) ); + case CG_R_ADDLIGHTTOSCENE: + re.AddLightToScene( VMA( 1 ), VMF( 2 ), VMF( 3 ), VMF( 4 ), VMF( 5 ), args[6] ); + return 0; +// case CG_R_ADDADDITIVELIGHTTOSCENE: +// re.AddAdditiveLightToScene( VMA(1), VMF(2), VMF(3), VMF(4), VMF(5) ); +// return 0; + case CG_R_ADDCORONATOSCENE: + re.AddCoronaToScene( VMA( 1 ), VMF( 2 ), VMF( 3 ), VMF( 4 ), VMF( 5 ), args[6], args[7] ); + return 0; + case CG_R_SETFOG: + re.SetFog( args[1], args[2], args[3], VMF( 4 ), VMF( 5 ), VMF( 6 ), VMF( 7 ) ); + return 0; + case CG_R_RENDERSCENE: + re.RenderScene( VMA( 1 ) ); + return 0; + case CG_R_SETCOLOR: + re.SetColor( VMA( 1 ) ); + return 0; + case CG_R_DRAWSTRETCHPIC: + re.DrawStretchPic( VMF( 1 ), VMF( 2 ), VMF( 3 ), VMF( 4 ), VMF( 5 ), VMF( 6 ), VMF( 7 ), VMF( 8 ), args[9] ); + return 0; + case CG_R_DRAWSTRETCHPIC_GRADIENT: + re.DrawStretchPicGradient( VMF( 1 ), VMF( 2 ), VMF( 3 ), VMF( 4 ), VMF( 5 ), VMF( 6 ), VMF( 7 ), VMF( 8 ), args[9], VMA( 10 ), args[11] ); + return 0; + case CG_R_MODELBOUNDS: + re.ModelBounds( args[1], VMA( 2 ), VMA( 3 ) ); + return 0; + case CG_R_LERPTAG: + return re.LerpTag( VMA( 1 ), VMA( 2 ), VMA( 3 ), args[4] ); + case CG_GETGLCONFIG: + CL_GetGlconfig( VMA( 1 ) ); + return 0; + case CG_GETGAMESTATE: + CL_GetGameState( VMA( 1 ) ); + return 0; + case CG_GETCURRENTSNAPSHOTNUMBER: + CL_GetCurrentSnapshotNumber( VMA( 1 ), VMA( 2 ) ); + return 0; + case CG_GETSNAPSHOT: + return CL_GetSnapshot( args[1], VMA( 2 ) ); + case CG_GETSERVERCOMMAND: + return CL_GetServerCommand( args[1] ); + case CG_GETCURRENTCMDNUMBER: + return CL_GetCurrentCmdNumber(); + case CG_GETUSERCMD: + return CL_GetUserCmd( args[1], VMA( 2 ) ); + case CG_SETUSERCMDVALUE: + CL_SetUserCmdValue( args[1], args[2], VMF( 3 ), args[4] ); //----(SA) modified // NERVE - SMF - added fourth arg [cld] + return 0; + case CG_MEMORY_REMAINING: + return Hunk_MemoryRemaining(); + case CG_KEY_ISDOWN: + return Key_IsDown( args[1] ); + case CG_KEY_GETCATCHER: + return Key_GetCatcher(); + case CG_KEY_SETCATCHER: + Key_SetCatcher( args[1] ); + return 0; + case CG_KEY_GETKEY: + return Key_GetKey( VMA( 1 ) ); + + + + case CG_MEMSET: + return (int)memset( VMA( 1 ), args[2], args[3] ); + case CG_MEMCPY: + return (int)memcpy( VMA( 1 ), VMA( 2 ), args[3] ); + case CG_STRNCPY: + return (int)strncpy( VMA( 1 ), VMA( 2 ), args[3] ); + case CG_SIN: + return FloatAsInt( sin( VMF( 1 ) ) ); + case CG_COS: + return FloatAsInt( cos( VMF( 1 ) ) ); + case CG_ATAN2: + return FloatAsInt( atan2( VMF( 1 ), VMF( 2 ) ) ); + case CG_SQRT: + return FloatAsInt( sqrt( VMF( 1 ) ) ); + case CG_FLOOR: + return FloatAsInt( floor( VMF( 1 ) ) ); + case CG_CEIL: + return FloatAsInt( ceil( VMF( 1 ) ) ); + case CG_ACOS: + return FloatAsInt( Q_acos( VMF( 1 ) ) ); + + case CG_PC_ADD_GLOBAL_DEFINE: + return botlib_export->PC_AddGlobalDefine( VMA( 1 ) ); + case CG_PC_LOAD_SOURCE: + return botlib_export->PC_LoadSourceHandle( VMA( 1 ) ); + case CG_PC_FREE_SOURCE: + return botlib_export->PC_FreeSourceHandle( args[1] ); + case CG_PC_READ_TOKEN: + return botlib_export->PC_ReadTokenHandle( args[1], VMA( 2 ) ); + case CG_PC_SOURCE_FILE_AND_LINE: + return botlib_export->PC_SourceFileAndLine( args[1], VMA( 2 ), VMA( 3 ) ); + + case CG_S_STOPBACKGROUNDTRACK: + S_StopBackgroundTrack(); + return 0; + + case CG_REAL_TIME: + return Com_RealTime( VMA( 1 ) ); + case CG_SNAPVECTOR: + Sys_SnapVector( VMA( 1 ) ); + return 0; + + case CG_SENDMOVESPEEDSTOGAME: + SV_SendMoveSpeedsToGame( args[1], VMA( 2 ) ); + return 0; + + case CG_CIN_PLAYCINEMATIC: + return CIN_PlayCinematic( VMA( 1 ), args[2], args[3], args[4], args[5], args[6] ); + + case CG_CIN_STOPCINEMATIC: + return CIN_StopCinematic( args[1] ); + + case CG_CIN_RUNCINEMATIC: + return CIN_RunCinematic( args[1] ); + + case CG_CIN_DRAWCINEMATIC: + CIN_DrawCinematic( args[1] ); + return 0; + + case CG_CIN_SETEXTENTS: + CIN_SetExtents( args[1], args[2], args[3], args[4], args[5] ); + return 0; + + case CG_R_REMAP_SHADER: + re.RemapShader( VMA( 1 ), VMA( 2 ), VMA( 3 ) ); + return 0; + + case CG_TESTPRINTINT: + Com_Printf( "%s%i\n", VMA( 1 ), args[2] ); + return 0; + case CG_TESTPRINTFLOAT: + Com_Printf( "%s%f\n", VMA( 1 ), VMF( 2 ) ); + return 0; + + case CG_LOADCAMERA: + return loadCamera( args[1], VMA( 2 ) ); + + case CG_STARTCAMERA: + if ( args[1] == 0 ) { // CAM_PRIMARY + cl.cameraMode = qtrue; //----(SA) added + } + startCamera( args[1], args[2] ); + return 0; + +//----(SA) added + case CG_STOPCAMERA: + if ( args[1] == 0 ) { // CAM_PRIMARY + cl.cameraMode = qfalse; + } +// stopCamera(args[1]); + return 0; +//----(SA) end + + case CG_GETCAMERAINFO: + return getCameraInfo( args[1], args[2], VMA( 3 ), VMA( 4 ), VMA( 5 ) ); + + case CG_GET_ENTITY_TOKEN: + return re.GetEntityToken( VMA( 1 ), args[2] ); + + case CG_INGAME_POPUP: + if ( VMA( 1 ) && !Q_stricmp( VMA( 1 ), "briefing" ) ) { //----(SA) added + VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_BRIEFING ); + return 0; + } + + if ( cls.state == CA_ACTIVE && !clc.demoplaying ) { + // NERVE - SMF + if ( VMA( 1 ) && !Q_stricmp( VMA( 1 ), "UIMENU_WM_PICKTEAM" ) ) { + VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_WM_PICKTEAM ); + } else if ( VMA( 1 ) && !Q_stricmp( VMA( 1 ), "UIMENU_WM_PICKPLAYER" ) ) { + VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_WM_PICKPLAYER ); + } else if ( VMA( 1 ) && !Q_stricmp( VMA( 1 ), "UIMENU_WM_QUICKMESSAGE" ) ) { + VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_WM_QUICKMESSAGE ); + } else if ( VMA( 1 ) && !Q_stricmp( VMA( 1 ), "UIMENU_WM_LIMBO" ) ) { + VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_WM_LIMBO ); + } + // -NERVE - SMF + else if ( VMA( 1 ) && !Q_stricmp( VMA( 1 ), "hbook1" ) ) { //----(SA) + VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_BOOK1 ); + } else if ( VMA( 1 ) && !Q_stricmp( VMA( 1 ), "hbook2" ) ) { //----(SA) + VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_BOOK2 ); + } else if ( VMA( 1 ) && !Q_stricmp( VMA( 1 ), "hbook3" ) ) { //----(SA) + VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_BOOK3 ); + } else if ( VMA( 1 ) && !Q_stricmp( VMA( 1 ), "pregame" ) ) { //----(SA) added + VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_PREGAME ); + } else { + VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_CLIPBOARD ); + } + } + return 0; + + // NERVE - SMF + case CG_INGAME_CLOSEPOPUP: + VM_Call( uivm, UI_KEY_EVENT, K_ESCAPE, qtrue ); + return 0; + + case CG_LIMBOCHAT: + if ( VMA( 1 ) ) { + CL_AddToLimboChat( VMA( 1 ) ); + } + return 0; + // - NERVE - SMF + + case CG_GETMODELINFO: + return SV_GetModelInfo( args[1], VMA( 2 ), VMA( 3 ) ); + + default: + Com_Error( ERR_DROP, "Bad cgame system trap: %i", args[0] ); + } + return 0; +} + +/* +==================== +CL_UpdateLevelHunkUsage + + This updates the "hunkusage.dat" file with the current map and it's hunk usage count + + This is used for level loading, so we can show a percentage bar dependant on the amount + of hunk memory allocated so far + + This will be slightly inaccurate if some settings like sound quality are changed, but these + things should only account for a small variation (hopefully) +==================== +*/ +void CL_UpdateLevelHunkUsage( void ) { + int handle; + char *memlistfile = "hunkusage.dat"; + char *buf, *outbuf; + char *buftrav, *outbuftrav; + char *token; + char outstr[256]; + int len, memusage; + + memusage = Cvar_VariableIntegerValue( "com_hunkused" ) + Cvar_VariableIntegerValue( "hunk_soundadjust" ); + + len = FS_FOpenFileByMode( memlistfile, &handle, FS_READ ); + if ( len >= 0 ) { // the file exists, so read it in, strip out the current entry for this map, and save it out, so we can append the new value + + buf = (char *)Z_Malloc( len + 1 ); + memset( buf, 0, len + 1 ); + outbuf = (char *)Z_Malloc( len + 1 ); + memset( outbuf, 0, len + 1 ); + + FS_Read( (void *)buf, len, handle ); + FS_FCloseFile( handle ); + + // now parse the file, filtering out the current map + buftrav = buf; + outbuftrav = outbuf; + outbuftrav[0] = '\0'; + while ( ( token = COM_Parse( &buftrav ) ) && token[0] ) { + if ( !Q_strcasecmp( token, cl.mapname ) ) { + // found a match + token = COM_Parse( &buftrav ); // read the size + if ( token && token[0] ) { + if ( atoi( token ) == memusage ) { // if it is the same, abort this process + Z_Free( buf ); + Z_Free( outbuf ); + return; + } + } + } else { // send it to the outbuf + Q_strcat( outbuftrav, len + 1, token ); + Q_strcat( outbuftrav, len + 1, " " ); + token = COM_Parse( &buftrav ); // read the size + if ( token && token[0] ) { + Q_strcat( outbuftrav, len + 1, token ); + Q_strcat( outbuftrav, len + 1, "\n" ); + } else { + Com_Error( ERR_DROP, "hunkusage.dat file is corrupt\n" ); + } + } + } + +#ifdef __MACOS__ //DAJ MacOS file typing + { + extern _MSL_IMP_EXP_C long _fcreator, _ftype; + _ftype = 'WlfB'; + _fcreator = 'WlfS'; + } +#endif + handle = FS_FOpenFileWrite( memlistfile ); + if ( handle < 0 ) { + Com_Error( ERR_DROP, "cannot create %s\n", memlistfile ); + } + // input file is parsed, now output to the new file + len = strlen( outbuf ); + if ( FS_Write( (void *)outbuf, len, handle ) != len ) { + Com_Error( ERR_DROP, "cannot write to %s\n", memlistfile ); + } + FS_FCloseFile( handle ); + + Z_Free( buf ); + Z_Free( outbuf ); + } + // now append the current map to the current file + FS_FOpenFileByMode( memlistfile, &handle, FS_APPEND ); + if ( handle < 0 ) { + Com_Error( ERR_DROP, "cannot write to hunkusage.dat, check disk full\n" ); + } + Com_sprintf( outstr, sizeof( outstr ), "%s %i\n", cl.mapname, memusage ); + FS_Write( outstr, strlen( outstr ), handle ); + FS_FCloseFile( handle ); + + // now just open it and close it, so it gets copied to the pak dir + len = FS_FOpenFileByMode( memlistfile, &handle, FS_READ ); + if ( len >= 0 ) { + FS_FCloseFile( handle ); + } +} + +/* +==================== +CL_InitCGame + +Should only by called by CL_StartHunkUsers +==================== +*/ +void CL_InitCGame( void ) { + const char *info; + const char *mapname; + int t1, t2; + vmInterpret_t interpret; + + t1 = Sys_Milliseconds(); + + // put away the console + Con_Close(); + + // find the current mapname + info = cl.gameState.stringData + cl.gameState.stringOffsets[ CS_SERVERINFO ]; + mapname = Info_ValueForKey( info, "mapname" ); + Com_sprintf( cl.mapname, sizeof( cl.mapname ), "maps/%s.bsp", mapname ); + + // load the dll or bytecode + if ( cl_connectedToPureServer != 0 ) { + // if sv_pure is set we only allow qvms to be loaded + interpret = VMI_COMPILED; + } else { + interpret = Cvar_VariableValue( "vm_cgame" ); + } + cgvm = VM_Create( "cgame", CL_CgameSystemCalls, interpret ); +// cgvm = VM_Create( "cgame", CL_CgameSystemCalls, Cvar_VariableValue( "vm_cgame" ) ); + if ( !cgvm ) { + Com_Error( ERR_DROP, "VM_Create on cgame failed" ); + } + cls.state = CA_LOADING; + + // init for this gamestate + // use the lastExecutedServerCommand instead of the serverCommandSequence + // otherwise server commands sent just before a gamestate are dropped + VM_Call( cgvm, CG_INIT, clc.serverMessageSequence, clc.lastExecutedServerCommand, clc.clientNum ); +// VM_Call( cgvm, CG_INIT, clc.serverMessageSequence, clc.serverCommandSequence ); + + // we will send a usercmd this frame, which + // will cause the server to send us the first snapshot + cls.state = CA_PRIMED; + + t2 = Sys_Milliseconds(); + + Com_Printf( "CL_InitCGame: %5.2f seconds\n", ( t2 - t1 ) / 1000.0 ); + + // have the renderer touch all its images, so they are present + // on the card even if the driver does deferred loading + re.EndRegistration(); + + // make sure everything is paged in + if ( !Sys_LowPhysicalMemory() ) { + Com_TouchMemory(); + } + + // clear anything that got printed + Con_ClearNotify(); + + // Ridah, update the memory usage file + CL_UpdateLevelHunkUsage(); +} + + +/* +==================== +CL_GameCommand + +See if the current console command is claimed by the cgame +==================== +*/ +qboolean CL_GameCommand( void ) { + if ( !cgvm ) { + return qfalse; + } + + return VM_Call( cgvm, CG_CONSOLE_COMMAND ); +} + + + +/* +===================== +CL_CGameRendering +===================== +*/ +void CL_CGameRendering( stereoFrame_t stereo ) { + VM_Call( cgvm, CG_DRAW_ACTIVE_FRAME, cl.serverTime, stereo, clc.demoplaying ); + VM_Debug( 0 ); +} + + +/* +================= +CL_AdjustTimeDelta + +Adjust the clients view of server time. + +We attempt to have cl.serverTime exactly equal the server's view +of time plus the timeNudge, but with variable latencies over +the internet it will often need to drift a bit to match conditions. + +Our ideal time would be to have the adjusted time approach, but not pass, +the very latest snapshot. + +Adjustments are only made when a new snapshot arrives with a rational +latency, which keeps the adjustment process framerate independent and +prevents massive overadjustment during times of significant packet loss +or bursted delayed packets. +================= +*/ + +#define RESET_TIME 500 + +void CL_AdjustTimeDelta( void ) { + int resetTime; + int newDelta; + int deltaDelta; + + cl.newSnapshots = qfalse; + + // the delta never drifts when replaying a demo + if ( clc.demoplaying ) { + return; + } + + // if the current time is WAY off, just correct to the current value + if ( com_sv_running->integer ) { + resetTime = 100; + } else { + resetTime = RESET_TIME; + } + + newDelta = cl.snap.serverTime - cls.realtime; + deltaDelta = abs( newDelta - cl.serverTimeDelta ); + + if ( deltaDelta > RESET_TIME ) { + cl.serverTimeDelta = newDelta; + cl.oldServerTime = cl.snap.serverTime; // FIXME: is this a problem for cgame? + cl.serverTime = cl.snap.serverTime; + if ( cl_showTimeDelta->integer ) { + Com_Printf( " " ); + } + } else if ( deltaDelta > 100 ) { + // fast adjust, cut the difference in half + if ( cl_showTimeDelta->integer ) { + Com_Printf( " " ); + } + cl.serverTimeDelta = ( cl.serverTimeDelta + newDelta ) >> 1; + } else { + // slow drift adjust, only move 1 or 2 msec + + // if any of the frames between this and the previous snapshot + // had to be extrapolated, nudge our sense of time back a little + // the granularity of +1 / -2 is too high for timescale modified frametimes + if ( com_timescale->value == 0 || com_timescale->value == 1 ) { + if ( cl.extrapolatedSnapshot ) { + cl.extrapolatedSnapshot = qfalse; + cl.serverTimeDelta -= 2; + } else { + // otherwise, move our sense of time forward to minimize total latency + cl.serverTimeDelta++; + } + } + } + + if ( cl_showTimeDelta->integer ) { + Com_Printf( "%i ", cl.serverTimeDelta ); + } +} + + +/* +================== +CL_FirstSnapshot +================== +*/ +void CL_FirstSnapshot( void ) { + // ignore snapshots that don't have entities + if ( cl.snap.snapFlags & SNAPFLAG_NOT_ACTIVE ) { + return; + } + cls.state = CA_ACTIVE; + + // set the timedelta so we are exactly on this first frame + cl.serverTimeDelta = cl.snap.serverTime - cls.realtime; + cl.oldServerTime = cl.snap.serverTime; + + clc.timeDemoBaseTime = cl.snap.serverTime; + + // if this is the first frame of active play, + // execute the contents of activeAction now + // this is to allow scripting a timedemo to start right + // after loading + if ( cl_activeAction->string[0] ) { + Cbuf_AddText( cl_activeAction->string ); + Cvar_Set( "activeAction", "" ); + } + + Sys_BeginProfiling(); +} + +/* +================== +CL_SetCGameTime +================== +*/ +void CL_SetCGameTime( void ) { + // getting a valid frame message ends the connection process + if ( cls.state != CA_ACTIVE ) { + if ( cls.state != CA_PRIMED ) { + return; + } + if ( clc.demoplaying ) { + // we shouldn't get the first snapshot on the same frame + // as the gamestate, because it causes a bad time skip + if ( !clc.firstDemoFrameSkipped ) { + clc.firstDemoFrameSkipped = qtrue; + return; + } + CL_ReadDemoMessage(); + } + if ( cl.newSnapshots ) { + cl.newSnapshots = qfalse; + CL_FirstSnapshot(); + } + if ( cls.state != CA_ACTIVE ) { + return; + } + } + + // if we have gotten to this point, cl.snap is guaranteed to be valid + if ( !cl.snap.valid ) { + Com_Error( ERR_DROP, "CL_SetCGameTime: !cl.snap.valid" ); + } + + // allow pause in single player + if ( sv_paused->integer && cl_paused->integer && com_sv_running->integer ) { + // paused + return; + } + + if ( cl.snap.serverTime < cl.oldFrameServerTime ) { + // Ridah, if this is a localhost, then we are probably loading a savegame + if ( !Q_stricmp( cls.servername, "localhost" ) ) { + // do nothing? + CL_FirstSnapshot(); + } else { + Com_Error( ERR_DROP, "cl.snap.serverTime < cl.oldFrameServerTime" ); + } + } + cl.oldFrameServerTime = cl.snap.serverTime; + + + // get our current view of time + + if ( clc.demoplaying && cl_freezeDemo->integer ) { + // cl_freezeDemo is used to lock a demo in place for single frame advances + + } else { + // cl_timeNudge is a user adjustable cvar that allows more + // or less latency to be added in the interest of better + // smoothness or better responsiveness. + int tn; + + tn = cl_timeNudge->integer; + if ( tn < -30 ) { + tn = -30; + } else if ( tn > 30 ) { + tn = 30; + } + + cl.serverTime = cls.realtime + cl.serverTimeDelta - tn; + + // guarantee that time will never flow backwards, even if + // serverTimeDelta made an adjustment or cl_timeNudge was changed + if ( cl.serverTime < cl.oldServerTime ) { + cl.serverTime = cl.oldServerTime; + } + cl.oldServerTime = cl.serverTime; + + // note if we are almost past the latest frame (without timeNudge), + // so we will try and adjust back a bit when the next snapshot arrives + if ( cls.realtime + cl.serverTimeDelta >= cl.snap.serverTime - 5 ) { + cl.extrapolatedSnapshot = qtrue; + } + } + + // if we have gotten new snapshots, drift serverTimeDelta + // don't do this every frame, or a period of packet loss would + // make a huge adjustment + if ( cl.newSnapshots ) { + CL_AdjustTimeDelta(); + } + + if ( !clc.demoplaying ) { + return; + } + + // if we are playing a demo back, we can just keep reading + // messages from the demo file until the cgame definately + // has valid snapshots to interpolate between + + // a timedemo will always use a deterministic set of time samples + // no matter what speed machine it is run on, + // while a normal demo may have different time samples + // each time it is played back + if ( cl_timedemo->integer ) { + if ( !clc.timeDemoStart ) { + clc.timeDemoStart = Sys_Milliseconds(); + } + clc.timeDemoFrames++; + cl.serverTime = clc.timeDemoBaseTime + clc.timeDemoFrames * 50; + } + + while ( cl.serverTime >= cl.snap.serverTime ) { + // feed another messag, which should change + // the contents of cl.snap + CL_ReadDemoMessage(); + if ( cls.state != CA_ACTIVE ) { + return; // end of demo + } + } + +} + +/* +==================== +CL_GetTag +==================== +*/ +qboolean CL_GetTag( int clientNum, char *tagname, orientation_t *or ) { + if ( !cgvm ) { + return qfalse; + } + + return VM_Call( cgvm, CG_GET_TAG, clientNum, tagname, or ); +} diff --git a/src/client/cl_cin.c b/src/client/cl_cin.c new file mode 100644 index 0000000..c01c2b9 --- /dev/null +++ b/src/client/cl_cin.c @@ -0,0 +1,1878 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: cl_cin.c + * + * desc: video and cinematic playback + * + * + * cl_glconfig.hwtype trtypes 3dfx/ragepro need 256x256 + * + *****************************************************************************/ + +//#define ADAPTED_TO_STREAMING_SOUND +// (SA) MISSIONPACK MERGE +// s_rawend for wolf is [] and for q3 is just a single value +// I need to ask Ryan if it's as simple as a constant index or +// if some more coding needs to be done. + + +#include "client.h" +//#include "snd_local.h" + +#define MAXSIZE 8 +#define MINSIZE 4 + +#define DEFAULT_CIN_WIDTH 512 +#define DEFAULT_CIN_HEIGHT 512 + +#define LETTERBOX_OFFSET 105 + + +#define ROQ_QUAD 0x1000 +#define ROQ_QUAD_INFO 0x1001 +#define ROQ_CODEBOOK 0x1002 +#define ROQ_QUAD_VQ 0x1011 +#define ROQ_QUAD_JPEG 0x1012 +#define ROQ_QUAD_HANG 0x1013 +#define ROQ_PACKET 0x1030 +#define ZA_SOUND_MONO 0x1020 +#define ZA_SOUND_STEREO 0x1021 + +#define MAX_VIDEO_HANDLES 16 + +extern glconfig_t glConfig; +extern int s_paintedtime; +extern int s_soundtime; +extern int s_rawend[]; //DAJ added [] to match definition + +#define CIN_STREAM 0 //DAJ const for the sound stream used for cinematics +static void RoQ_init( void ); + +/****************************************************************************** +* +* Class: trFMV +* +* Description: RoQ/RnR manipulation routines +* not entirely complete for first run +* +******************************************************************************/ + +static long ROQ_YY_tab[256]; +static long ROQ_UB_tab[256]; +static long ROQ_UG_tab[256]; +static long ROQ_VG_tab[256]; +static long ROQ_VR_tab[256]; +static unsigned short vq2[256 * 16 * 4]; +static unsigned short vq4[256 * 64 * 4]; +static unsigned short vq8[256 * 256 * 4]; + + +typedef struct { + byte linbuf[DEFAULT_CIN_WIDTH * DEFAULT_CIN_HEIGHT * 4 * 2]; + byte file[65536]; + short sqrTable[256]; + + unsigned int mcomp[256]; + byte *qStatus[2][32768]; + + long oldXOff, oldYOff, oldysize, oldxsize; + + int currentHandle; +} cinematics_t; + +typedef struct { + char fileName[MAX_OSPATH]; + int CIN_WIDTH, CIN_HEIGHT; + int xpos, ypos, width, height; + qboolean looping, holdAtEnd, dirty, alterGameState, silent, shader, letterBox, sound; + fileHandle_t iFile; + e_status status; + unsigned int startTime; + unsigned int lastTime; + long tfps; + long RoQPlayed; + long ROQSize; + unsigned int RoQFrameSize; + long onQuad; + long numQuads; + long samplesPerLine; + unsigned int roq_id; + long screenDelta; + + void ( *VQ0 )( byte *status, void *qdata ); + void ( *VQ1 )( byte *status, void *qdata ); + void ( *VQNormal )( byte *status, void *qdata ); + void ( *VQBuffer )( byte *status, void *qdata ); + + long samplesPerPixel; // defaults to 2 + byte* gray; + unsigned int xsize, ysize, maxsize, minsize; + + qboolean half, smootheddouble, inMemory; + long normalBuffer0; + long roq_flags; + long roqF0; + long roqF1; + long t[2]; + long roqFPS; + int playonwalls; + byte* buf; + long drawX, drawY; +} cin_cache; + +static cinematics_t cin; +static cin_cache cinTable[MAX_VIDEO_HANDLES]; +static int currentHandle = -1; +static int CL_handle = -1; + +void CIN_CloseAllVideos( void ) { + int i; + + for ( i = 0 ; i < MAX_VIDEO_HANDLES ; i++ ) { + if ( cinTable[i].fileName[0] != 0 ) { + CIN_StopCinematic( i ); + } + } +} + + +static int CIN_HandleForVideo( void ) { + int i; + + for ( i = 0 ; i < MAX_VIDEO_HANDLES ; i++ ) { + if ( cinTable[i].fileName[0] == 0 ) { + return i; + } + } + Com_Error( ERR_DROP, "CIN_HandleForVideo: none free" ); + return -1; +} + + +extern int CL_ScaledMilliseconds( void ); + +//----------------------------------------------------------------------------- +// RllSetupTable +// +// Allocates and initializes the square table. +// +// Parameters: None +// +// Returns: Nothing +//----------------------------------------------------------------------------- +static void RllSetupTable() { + int z; + + for ( z = 0; z < 128; z++ ) { + cin.sqrTable[z] = (short)( z * z ); + cin.sqrTable[z + 128] = (short)( -cin.sqrTable[z] ); + } +} + + + +//----------------------------------------------------------------------------- +// RllDecodeMonoToMono +// +// Decode mono source data into a mono buffer. +// +// Parameters: from -> buffer holding encoded data +// to -> buffer to hold decoded data +// size = number of bytes of input (= # of shorts of output) +// signedOutput = 0 for unsigned output, non-zero for signed output +// flag = flags from asset header +// +// Returns: Number of samples placed in output buffer +//----------------------------------------------------------------------------- +long RllDecodeMonoToMono( unsigned char *from,short *to,unsigned int size,char signedOutput,unsigned short flag ) { + unsigned int z; + short prev; //DAJ was int + + if ( signedOutput ) { + prev = flag - 0x8000; + } else { + prev = flag; + } + + for ( z = 0; z < size; z++ ) { + prev = to[z] = (short)( prev + cin.sqrTable[from[z]] ); + } + return size; //*sizeof(short)); +} + + +//----------------------------------------------------------------------------- +// RllDecodeMonoToStereo +// +// Decode mono source data into a stereo buffer. Output is 4 times the number +// of bytes in the input. +// +// Parameters: from -> buffer holding encoded data +// to -> buffer to hold decoded data +// size = number of bytes of input (= 1/4 # of bytes of output) +// signedOutput = 0 for unsigned output, non-zero for signed output +// flag = flags from asset header +// +// Returns: Number of samples placed in output buffer +//----------------------------------------------------------------------------- +long RllDecodeMonoToStereo( unsigned char *from,short *to,unsigned int size,char signedOutput,unsigned short flag ) { + unsigned int z; + short prev; //DAJ was int + + if ( signedOutput ) { + prev = flag - 0x8000; + } else { + prev = flag; + } + + for ( z = 0; z < size; z++ ) { + prev = (short)( prev + cin.sqrTable[from[z]] ); + to[z * 2 + 0] = to[z * 2 + 1] = (short)( prev ); + } + + return size; // * 2 * sizeof(short)); +} + + +//----------------------------------------------------------------------------- +// RllDecodeStereoToStereo +// +// Decode stereo source data into a stereo buffer. +// +// Parameters: from -> buffer holding encoded data +// to -> buffer to hold decoded data +// size = number of bytes of input (= 1/2 # of bytes of output) +// signedOutput = 0 for unsigned output, non-zero for signed output +// flag = flags from asset header +// +// Returns: Number of samples placed in output buffer +//----------------------------------------------------------------------------- +long RllDecodeStereoToStereo( unsigned char *from,short *to,unsigned int size,char signedOutput, unsigned short flag ) { + unsigned int z; + unsigned char *zz = from; + short prevL, prevR; //DAJ was int + + if ( signedOutput ) { + prevL = ( flag & 0xff00 ) - 0x8000; + prevR = ( ( flag & 0x00ff ) << 8 ) - 0x8000; + } else { + prevL = flag & 0xff00; + prevR = ( flag & 0x00ff ) << 8; + } + + for ( z = 0; z < size; z += 2 ) { + prevL = (short)( prevL + cin.sqrTable[*zz++] ); + prevR = (short)( prevR + cin.sqrTable[*zz++] ); + to[z + 0] = (short)( prevL ); + to[z + 1] = (short)( prevR ); + } + + return ( size >> 1 ); //*sizeof(short)); +} + + +//----------------------------------------------------------------------------- +// RllDecodeStereoToMono +// +// Decode stereo source data into a mono buffer. +// +// Parameters: from -> buffer holding encoded data +// to -> buffer to hold decoded data +// size = number of bytes of input (= # of bytes of output) +// signedOutput = 0 for unsigned output, non-zero for signed output +// flag = flags from asset header +// +// Returns: Number of samples placed in output buffer +//----------------------------------------------------------------------------- +long RllDecodeStereoToMono( unsigned char *from,short *to,unsigned int size,char signedOutput, unsigned short flag ) { + unsigned int z; + short prevL, prevR; //DAJ was int + + if ( signedOutput ) { + prevL = ( flag & 0xff00 ) - 0x8000; + prevR = ( ( flag & 0x00ff ) << 8 ) - 0x8000; + } else { + prevL = flag & 0xff00; + prevR = ( flag & 0x00ff ) << 8; + } + + for ( z = 0; z < size; z += 1 ) { + prevL = prevL + cin.sqrTable[from[z * 2]]; + prevR = prevR + cin.sqrTable[from[z * 2 + 1]]; + to[z] = (short)( ( prevL + prevR ) / 2 ); + } + + return size; +} + +/****************************************************************************** +* +* Function: +* +* Description: +* +******************************************************************************/ + +static void move8_32( byte *src, byte *dst, int spl ) { + double *dsrc, *ddst; + int dspl; + + dsrc = (double *)src; + ddst = (double *)dst; + dspl = spl >> 3; + + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; ddst[2] = dsrc[2]; ddst[3] = dsrc[3]; + dsrc += dspl; ddst += dspl; + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; ddst[2] = dsrc[2]; ddst[3] = dsrc[3]; + dsrc += dspl; ddst += dspl; + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; ddst[2] = dsrc[2]; ddst[3] = dsrc[3]; + dsrc += dspl; ddst += dspl; + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; ddst[2] = dsrc[2]; ddst[3] = dsrc[3]; + dsrc += dspl; ddst += dspl; + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; ddst[2] = dsrc[2]; ddst[3] = dsrc[3]; + dsrc += dspl; ddst += dspl; + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; ddst[2] = dsrc[2]; ddst[3] = dsrc[3]; + dsrc += dspl; ddst += dspl; + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; ddst[2] = dsrc[2]; ddst[3] = dsrc[3]; + dsrc += dspl; ddst += dspl; + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; ddst[2] = dsrc[2]; ddst[3] = dsrc[3]; +} + +/****************************************************************************** +* +* Function: +* +* Description: +* +******************************************************************************/ + +static void move4_32( byte *src, byte *dst, int spl ) { + double *dsrc, *ddst; + int dspl; + + dsrc = (double *)src; + ddst = (double *)dst; + dspl = spl >> 3; + + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; + dsrc += dspl; ddst += dspl; + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; + dsrc += dspl; ddst += dspl; + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; + dsrc += dspl; ddst += dspl; + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; +} + +/****************************************************************************** +* +* Function: +* +* Description: +* +******************************************************************************/ + +static void blit8_32( byte *src, byte *dst, int spl ) { + double *dsrc, *ddst; + int dspl; + + dsrc = (double *)src; + ddst = (double *)dst; + dspl = spl >> 3; + + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; ddst[2] = dsrc[2]; ddst[3] = dsrc[3]; + dsrc += 4; ddst += dspl; + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; ddst[2] = dsrc[2]; ddst[3] = dsrc[3]; + dsrc += 4; ddst += dspl; + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; ddst[2] = dsrc[2]; ddst[3] = dsrc[3]; + dsrc += 4; ddst += dspl; + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; ddst[2] = dsrc[2]; ddst[3] = dsrc[3]; + dsrc += 4; ddst += dspl; + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; ddst[2] = dsrc[2]; ddst[3] = dsrc[3]; + dsrc += 4; ddst += dspl; + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; ddst[2] = dsrc[2]; ddst[3] = dsrc[3]; + dsrc += 4; ddst += dspl; + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; ddst[2] = dsrc[2]; ddst[3] = dsrc[3]; + dsrc += 4; ddst += dspl; + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; ddst[2] = dsrc[2]; ddst[3] = dsrc[3]; +} + +/****************************************************************************** +* +* Function: +* +* Description: +* +******************************************************************************/ +#define movs double +static void blit4_32( byte *src, byte *dst, int spl ) { + movs *dsrc, *ddst; + int dspl; + + dsrc = (movs *)src; + ddst = (movs *)dst; + dspl = spl >> 3; + + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; + dsrc += 2; ddst += dspl; + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; + dsrc += 2; ddst += dspl; + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; + dsrc += 2; ddst += dspl; + ddst[0] = dsrc[0]; ddst[1] = dsrc[1]; +} + +/****************************************************************************** +* +* Function: +* +* Description: +* +******************************************************************************/ + +static void blit2_32( byte *src, byte *dst, int spl ) { + double *dsrc, *ddst; + int dspl; + + dsrc = (double *)src; + ddst = (double *)dst; + dspl = spl >> 3; + + ddst[0] = dsrc[0]; + ddst[dspl] = dsrc[1]; +} + +/****************************************************************************** +* +* Function: +* +* Description: +* +******************************************************************************/ + +static void blitVQQuad32fs( byte **status, unsigned char *data ) { + unsigned short newd, celdata, code; + unsigned int index, i; + int spl; + + newd = 0; + celdata = 0; + index = 0; + + spl = cinTable[currentHandle].samplesPerLine; + + do { + if ( !newd ) { + newd = 7; + celdata = data[0] + data[1] * 256; + data += 2; + } else { + newd--; + } + + code = ( unsigned short )( celdata & 0xc000 ); + celdata <<= 2; + + switch ( code ) { + case 0x8000: // vq code + blit8_32( (byte *)&vq8[( *data ) * 128], status[index], spl ); + data++; + index += 5; + break; + case 0xc000: // drop + index++; // skip 8x8 + for ( i = 0; i < 4; i++ ) { + if ( !newd ) { + newd = 7; + celdata = data[0] + data[1] * 256; + data += 2; + } else { + newd--; + } + + code = ( unsigned short )( celdata & 0xc000 ); celdata <<= 2; + + switch ( code ) { // code in top two bits of code + case 0x8000: // 4x4 vq code + blit4_32( (byte *)&vq4[( *data ) * 32], status[index], spl ); + data++; + break; + case 0xc000: // 2x2 vq code + blit2_32( (byte *)&vq2[( *data ) * 8], status[index], spl ); + data++; + blit2_32( (byte *)&vq2[( *data ) * 8], status[index] + 8, spl ); + data++; + blit2_32( (byte *)&vq2[( *data ) * 8], status[index] + spl * 2, spl ); + data++; + blit2_32( (byte *)&vq2[( *data ) * 8], status[index] + spl * 2 + 8, spl ); + data++; + break; + case 0x4000: // motion compensation + move4_32( status[index] + cin.mcomp[( *data )], status[index], spl ); + data++; + break; + } + index++; + } + break; + case 0x4000: // motion compensation + move8_32( status[index] + cin.mcomp[( *data )], status[index], spl ); + data++; + index += 5; + break; + case 0x0000: + index += 5; + break; + } + } while ( status[index] != NULL ); +} + +/****************************************************************************** +* +* Function: +* +* Description: +* +******************************************************************************/ + +static void ROQ_GenYUVTables( void ) { + float t_ub,t_vr,t_ug,t_vg; + long i; + + t_ub = ( 1.77200f / 2.0f ) * (float)( 1 << 6 ) + 0.5f; + t_vr = ( 1.40200f / 2.0f ) * (float)( 1 << 6 ) + 0.5f; + t_ug = ( 0.34414f / 2.0f ) * (float)( 1 << 6 ) + 0.5f; + t_vg = ( 0.71414f / 2.0f ) * (float)( 1 << 6 ) + 0.5f; + for ( i = 0; i < 256; i++ ) { + float x = (float)( 2 * i - 255 ); + + ROQ_UB_tab[i] = (long)( ( t_ub * x ) + ( 1 << 5 ) ); + ROQ_VR_tab[i] = (long)( ( t_vr * x ) + ( 1 << 5 ) ); + ROQ_UG_tab[i] = (long)( ( -t_ug * x ) ); + ROQ_VG_tab[i] = (long)( ( -t_vg * x ) + ( 1 << 5 ) ); + ROQ_YY_tab[i] = (long)( ( i << 6 ) | ( i >> 2 ) ); + } +} + +#define VQ2TO4( a,b,c,d ) { \ + *c++ = a[0]; \ + *d++ = a[0]; \ + *d++ = a[0]; \ + *c++ = a[1]; \ + *d++ = a[1]; \ + *d++ = a[1]; \ + *c++ = b[0]; \ + *d++ = b[0]; \ + *d++ = b[0]; \ + *c++ = b[1]; \ + *d++ = b[1]; \ + *d++ = b[1]; \ + *d++ = a[0]; \ + *d++ = a[0]; \ + *d++ = a[1]; \ + *d++ = a[1]; \ + *d++ = b[0]; \ + *d++ = b[0]; \ + *d++ = b[1]; \ + *d++ = b[1]; \ + a += 2; b += 2; } + +#define VQ2TO2( a,b,c,d ) { \ + *c++ = *a; \ + *d++ = *a; \ + *d++ = *a; \ + *c++ = *b; \ + *d++ = *b; \ + *d++ = *b; \ + *d++ = *a; \ + *d++ = *a; \ + *d++ = *b; \ + *d++ = *b; \ + a++; b++; } + +/****************************************************************************** +* +* Function: +* +* Description: +* +******************************************************************************/ + +static unsigned short yuv_to_rgb( long y, long u, long v ) { + long r,g,b,YY = (long)( ROQ_YY_tab[( y )] ); + + r = ( YY + ROQ_VR_tab[v] ) >> 9; + g = ( YY + ROQ_UG_tab[u] + ROQ_VG_tab[v] ) >> 8; + b = ( YY + ROQ_UB_tab[u] ) >> 9; + + if ( r < 0 ) { + r = 0; + } + if ( g < 0 ) { + g = 0; + } + if ( b < 0 ) { + b = 0; + } + if ( r > 31 ) { + r = 31; + } + if ( g > 63 ) { + g = 63; + } + if ( b > 31 ) { + b = 31; + } + + return ( unsigned short )( ( r << 11 ) + ( g << 5 ) + ( b ) ); +} + +/****************************************************************************** +* +* Function: +* +* Description: +* +******************************************************************************/ +#if defined( __MACOS__ ) + +static inline unsigned int yuv_to_rgb24( long y, long u, long v ) { + long r,g,b,YY; + + YY = (long)( ROQ_YY_tab[( y )] ); + + r = ( YY + ROQ_VR_tab[v] ) >> 6; + g = ( YY + ROQ_UG_tab[u] + ROQ_VG_tab[v] ) >> 6; + b = ( YY + ROQ_UB_tab[u] ) >> 6; + + if ( r < 0 ) { + r = 0; + } + if ( g < 0 ) { + g = 0; + } + if ( b < 0 ) { + b = 0; + } + if ( r > 255 ) { + r = 255; + } + if ( g > 255 ) { + g = 255; + } + if ( b > 255 ) { + b = 255; + } + + return ( ( r << 24 ) | ( g << 16 ) | ( b << 8 ) ) | ( 255 ); //+(255<<24)); +} + +#else +static unsigned int yuv_to_rgb24( long y, long u, long v ) { + long r,g,b,YY = (long)( ROQ_YY_tab[( y )] ); + + r = ( YY + ROQ_VR_tab[v] ) >> 6; + g = ( YY + ROQ_UG_tab[u] + ROQ_VG_tab[v] ) >> 6; + b = ( YY + ROQ_UB_tab[u] ) >> 6; + + if ( r < 0 ) { + r = 0; + } + if ( g < 0 ) { + g = 0; + } + if ( b < 0 ) { + b = 0; + } + if ( r > 255 ) { + r = 255; + } + if ( g > 255 ) { + g = 255; + } + if ( b > 255 ) { + b = 255; + } + + return LittleLong( ( r ) | ( g << 8 ) | ( b << 16 ) | ( 255 << 24 ) ); +} +#endif + +/****************************************************************************** +* +* Function: +* +* Description: +* +******************************************************************************/ + +static void decodeCodeBook( byte *input, unsigned short roq_flags ) { + long i, j, two, four; + unsigned short *aptr, *bptr, *cptr, *dptr; + long y0,y1,y2,y3,cr,cb; + byte *bbptr, *baptr, *bcptr, *bdptr; + unsigned int *iaptr, *ibptr, *icptr, *idptr; + + if ( !roq_flags ) { + two = four = 256; + } else { + two = roq_flags >> 8; + if ( !two ) { + two = 256; + } + four = roq_flags & 0xff; + } + + four *= 2; + + bptr = (unsigned short *)vq2; + + if ( !cinTable[currentHandle].half ) { + if ( !cinTable[currentHandle].smootheddouble ) { +// +// normal height +// + if ( cinTable[currentHandle].samplesPerPixel == 2 ) { + for ( i = 0; i < two; i++ ) { + y0 = (long)*input++; + y1 = (long)*input++; + y2 = (long)*input++; + y3 = (long)*input++; + cr = (long)*input++; + cb = (long)*input++; + *bptr++ = yuv_to_rgb( y0, cr, cb ); + *bptr++ = yuv_to_rgb( y1, cr, cb ); + *bptr++ = yuv_to_rgb( y2, cr, cb ); + *bptr++ = yuv_to_rgb( y3, cr, cb ); + } + + cptr = (unsigned short *)vq4; + dptr = (unsigned short *)vq8; + + for ( i = 0; i < four; i++ ) { + aptr = (unsigned short *)vq2 + ( *input++ ) * 4; + bptr = (unsigned short *)vq2 + ( *input++ ) * 4; + for ( j = 0; j < 2; j++ ) + VQ2TO4( aptr,bptr,cptr,dptr ); + } + } else if ( cinTable[currentHandle].samplesPerPixel == 4 ) { + ibptr = (unsigned int *)bptr; + for ( i = 0; i < two; i++ ) { + y0 = (long)*input++; + y1 = (long)*input++; + y2 = (long)*input++; + y3 = (long)*input++; + cr = (long)*input++; + cb = (long)*input++; + *ibptr++ = yuv_to_rgb24( y0, cr, cb ); + *ibptr++ = yuv_to_rgb24( y1, cr, cb ); + *ibptr++ = yuv_to_rgb24( y2, cr, cb ); + *ibptr++ = yuv_to_rgb24( y3, cr, cb ); + } + + icptr = (unsigned int *)vq4; + idptr = (unsigned int *)vq8; + + for ( i = 0; i < four; i++ ) { + iaptr = (unsigned int *)vq2 + ( *input++ ) * 4; + ibptr = (unsigned int *)vq2 + ( *input++ ) * 4; + for ( j = 0; j < 2; j++ ) + VQ2TO4( iaptr, ibptr, icptr, idptr ); + } + } else if ( cinTable[currentHandle].samplesPerPixel == 1 ) { + bbptr = (byte *)bptr; + for ( i = 0; i < two; i++ ) { + *bbptr++ = cinTable[currentHandle].gray[*input++]; + *bbptr++ = cinTable[currentHandle].gray[*input++]; + *bbptr++ = cinTable[currentHandle].gray[*input++]; + *bbptr++ = cinTable[currentHandle].gray[*input]; input += 3; + } + + bcptr = (byte *)vq4; + bdptr = (byte *)vq8; + + for ( i = 0; i < four; i++ ) { + baptr = (byte *)vq2 + ( *input++ ) * 4; + bbptr = (byte *)vq2 + ( *input++ ) * 4; + for ( j = 0; j < 2; j++ ) + VQ2TO4( baptr,bbptr,bcptr,bdptr ); + } + } + } else { +// +// double height, smoothed +// + if ( cinTable[currentHandle].samplesPerPixel == 2 ) { + for ( i = 0; i < two; i++ ) { + y0 = (long)*input++; + y1 = (long)*input++; + y2 = (long)*input++; + y3 = (long)*input++; + cr = (long)*input++; + cb = (long)*input++; + *bptr++ = yuv_to_rgb( y0, cr, cb ); + *bptr++ = yuv_to_rgb( y1, cr, cb ); + *bptr++ = yuv_to_rgb( ( ( y0 * 3 ) + y2 ) / 4, cr, cb ); + *bptr++ = yuv_to_rgb( ( ( y1 * 3 ) + y3 ) / 4, cr, cb ); + *bptr++ = yuv_to_rgb( ( y0 + ( y2 * 3 ) ) / 4, cr, cb ); + *bptr++ = yuv_to_rgb( ( y1 + ( y3 * 3 ) ) / 4, cr, cb ); + *bptr++ = yuv_to_rgb( y2, cr, cb ); + *bptr++ = yuv_to_rgb( y3, cr, cb ); + } + + cptr = (unsigned short *)vq4; + dptr = (unsigned short *)vq8; + + for ( i = 0; i < four; i++ ) { + aptr = (unsigned short *)vq2 + ( *input++ ) * 8; + bptr = (unsigned short *)vq2 + ( *input++ ) * 8; + for ( j = 0; j < 2; j++ ) { + VQ2TO4( aptr,bptr,cptr,dptr ); + VQ2TO4( aptr,bptr,cptr,dptr ); + } + } + } else if ( cinTable[currentHandle].samplesPerPixel == 4 ) { + ibptr = (unsigned int *)bptr; + for ( i = 0; i < two; i++ ) { + y0 = (long)*input++; + y1 = (long)*input++; + y2 = (long)*input++; + y3 = (long)*input++; + cr = (long)*input++; + cb = (long)*input++; + *ibptr++ = yuv_to_rgb24( y0, cr, cb ); + *ibptr++ = yuv_to_rgb24( y1, cr, cb ); + *ibptr++ = yuv_to_rgb24( ( ( y0 * 3 ) + y2 ) / 4, cr, cb ); + *ibptr++ = yuv_to_rgb24( ( ( y1 * 3 ) + y3 ) / 4, cr, cb ); + *ibptr++ = yuv_to_rgb24( ( y0 + ( y2 * 3 ) ) / 4, cr, cb ); + *ibptr++ = yuv_to_rgb24( ( y1 + ( y3 * 3 ) ) / 4, cr, cb ); + *ibptr++ = yuv_to_rgb24( y2, cr, cb ); + *ibptr++ = yuv_to_rgb24( y3, cr, cb ); + } + + icptr = (unsigned int *)vq4; + idptr = (unsigned int *)vq8; + + for ( i = 0; i < four; i++ ) { + iaptr = (unsigned int *)vq2 + ( *input++ ) * 8; + ibptr = (unsigned int *)vq2 + ( *input++ ) * 8; + for ( j = 0; j < 2; j++ ) { + VQ2TO4( iaptr, ibptr, icptr, idptr ); + VQ2TO4( iaptr, ibptr, icptr, idptr ); + } + } + } else if ( cinTable[currentHandle].samplesPerPixel == 1 ) { + bbptr = (byte *)bptr; + for ( i = 0; i < two; i++ ) { + y0 = (long)*input++; + y1 = (long)*input++; + y2 = (long)*input++; + y3 = (long)*input; input += 3; + *bbptr++ = cinTable[currentHandle].gray[y0]; + *bbptr++ = cinTable[currentHandle].gray[y1]; + *bbptr++ = cinTable[currentHandle].gray[( ( y0 * 3 ) + y2 ) / 4]; + *bbptr++ = cinTable[currentHandle].gray[( ( y1 * 3 ) + y3 ) / 4]; + *bbptr++ = cinTable[currentHandle].gray[( y0 + ( y2 * 3 ) ) / 4]; + *bbptr++ = cinTable[currentHandle].gray[( y1 + ( y3 * 3 ) ) / 4]; + *bbptr++ = cinTable[currentHandle].gray[y2]; + *bbptr++ = cinTable[currentHandle].gray[y3]; + } + + bcptr = (byte *)vq4; + bdptr = (byte *)vq8; + + for ( i = 0; i < four; i++ ) { + baptr = (byte *)vq2 + ( *input++ ) * 8; + bbptr = (byte *)vq2 + ( *input++ ) * 8; + for ( j = 0; j < 2; j++ ) { + VQ2TO4( baptr,bbptr,bcptr,bdptr ); + VQ2TO4( baptr,bbptr,bcptr,bdptr ); + } + } + } + } + } else { +// +// 1/4 screen +// + if ( cinTable[currentHandle].samplesPerPixel == 2 ) { + for ( i = 0; i < two; i++ ) { + y0 = (long)*input; input += 2; + y2 = (long)*input; input += 2; + cr = (long)*input++; + cb = (long)*input++; + *bptr++ = yuv_to_rgb( y0, cr, cb ); + *bptr++ = yuv_to_rgb( y2, cr, cb ); + } + + cptr = (unsigned short *)vq4; + dptr = (unsigned short *)vq8; + + for ( i = 0; i < four; i++ ) { + aptr = (unsigned short *)vq2 + ( *input++ ) * 2; + bptr = (unsigned short *)vq2 + ( *input++ ) * 2; + for ( j = 0; j < 2; j++ ) { + VQ2TO2( aptr,bptr,cptr,dptr ); + } + } + } else if ( cinTable[currentHandle].samplesPerPixel == 1 ) { + bbptr = (byte *)bptr; + + for ( i = 0; i < two; i++ ) { + *bbptr++ = cinTable[currentHandle].gray[*input]; input += 2; + *bbptr++ = cinTable[currentHandle].gray[*input]; input += 4; + } + + bcptr = (byte *)vq4; + bdptr = (byte *)vq8; + + for ( i = 0; i < four; i++ ) { + baptr = (byte *)vq2 + ( *input++ ) * 2; + bbptr = (byte *)vq2 + ( *input++ ) * 2; + for ( j = 0; j < 2; j++ ) { + VQ2TO2( baptr,bbptr,bcptr,bdptr ); + } + } + } else if ( cinTable[currentHandle].samplesPerPixel == 4 ) { + ibptr = (unsigned int *) bptr; + for ( i = 0; i < two; i++ ) { + y0 = (long)*input; input += 2; + y2 = (long)*input; input += 2; + cr = (long)*input++; + cb = (long)*input++; + *ibptr++ = yuv_to_rgb24( y0, cr, cb ); + *ibptr++ = yuv_to_rgb24( y2, cr, cb ); + } + + icptr = (unsigned int *)vq4; + idptr = (unsigned int *)vq8; + + for ( i = 0; i < four; i++ ) { + iaptr = (unsigned int *)vq2 + ( *input++ ) * 2; + ibptr = (unsigned int *)vq2 + ( *input++ ) * 2; + for ( j = 0; j < 2; j++ ) { + VQ2TO2( iaptr,ibptr,icptr,idptr ); + } + } + } + } +} + +/****************************************************************************** +* +* Function: +* +* Description: +* +******************************************************************************/ + +static void recurseQuad( long startX, long startY, long quadSize, long xOff, long yOff ) { + byte *scroff; + long bigx, bigy, lowx, lowy, useY; + long offset; + + offset = cinTable[currentHandle].screenDelta; + + lowx = lowy = 0; + bigx = cinTable[currentHandle].xsize; + bigy = cinTable[currentHandle].ysize; + + if ( bigx > cinTable[currentHandle].CIN_WIDTH ) { + bigx = cinTable[currentHandle].CIN_WIDTH; + } + if ( bigy > cinTable[currentHandle].CIN_HEIGHT ) { + bigy = cinTable[currentHandle].CIN_HEIGHT; + } + + if ( ( startX >= lowx ) && ( startX + quadSize ) <= ( bigx ) && ( startY + quadSize ) <= ( bigy ) && ( startY >= lowy ) && quadSize <= MAXSIZE ) { + useY = startY; + scroff = cin.linbuf + ( useY + ( ( cinTable[currentHandle].CIN_HEIGHT - bigy ) >> 1 ) + yOff ) * ( cinTable[currentHandle].samplesPerLine ) + ( ( ( startX + xOff ) ) * cinTable[currentHandle].samplesPerPixel ); + + cin.qStatus[0][cinTable[currentHandle].onQuad ] = scroff; + cin.qStatus[1][cinTable[currentHandle].onQuad++] = scroff + offset; + } + + if ( quadSize != MINSIZE ) { + quadSize >>= 1; + recurseQuad( startX, startY, quadSize, xOff, yOff ); + recurseQuad( startX + quadSize, startY, quadSize, xOff, yOff ); + recurseQuad( startX, startY + quadSize, quadSize, xOff, yOff ); + recurseQuad( startX + quadSize, startY + quadSize, quadSize, xOff, yOff ); + } +} + + +/****************************************************************************** +* +* Function: +* +* Description: +* +******************************************************************************/ + +static void setupQuad( long xOff, long yOff ) { + long numQuadCels, i,x,y; + byte *temp; + + if ( xOff == cin.oldXOff && yOff == cin.oldYOff && cinTable[currentHandle].ysize == cin.oldysize && cinTable[currentHandle].xsize == cin.oldxsize ) { + return; + } + + cin.oldXOff = xOff; + cin.oldYOff = yOff; + cin.oldysize = cinTable[currentHandle].ysize; + cin.oldxsize = cinTable[currentHandle].xsize; + + numQuadCels = ( cinTable[currentHandle].CIN_WIDTH * cinTable[currentHandle].CIN_HEIGHT ) / ( 16 ); + numQuadCels += numQuadCels / 4 + numQuadCels / 16; + numQuadCels += 64; // for overflow + + numQuadCels = ( cinTable[currentHandle].xsize * cinTable[currentHandle].ysize ) / ( 16 ); + numQuadCels += numQuadCels / 4; + numQuadCels += 64; // for overflow + + cinTable[currentHandle].onQuad = 0; + + for ( y = 0; y < (long)cinTable[currentHandle].ysize; y += 16 ) + for ( x = 0; x < (long)cinTable[currentHandle].xsize; x += 16 ) + recurseQuad( x, y, 16, xOff, yOff ); + + temp = NULL; + + for ( i = ( numQuadCels - 64 ); i < numQuadCels; i++ ) { + cin.qStatus[0][i] = temp; // eoq + cin.qStatus[1][i] = temp; // eoq + } +} + +/****************************************************************************** +* +* Function: +* +* Description: +* +******************************************************************************/ + +static void readQuadInfo( byte *qData ) { + if ( currentHandle < 0 ) { + return; + } + + cinTable[currentHandle].xsize = qData[0] + qData[1] * 256; + cinTable[currentHandle].ysize = qData[2] + qData[3] * 256; + cinTable[currentHandle].maxsize = qData[4] + qData[5] * 256; + cinTable[currentHandle].minsize = qData[6] + qData[7] * 256; + + cinTable[currentHandle].CIN_HEIGHT = cinTable[currentHandle].ysize; + cinTable[currentHandle].CIN_WIDTH = cinTable[currentHandle].xsize; + + cinTable[currentHandle].samplesPerLine = cinTable[currentHandle].CIN_WIDTH * cinTable[currentHandle].samplesPerPixel; + cinTable[currentHandle].screenDelta = cinTable[currentHandle].CIN_HEIGHT * cinTable[currentHandle].samplesPerLine; + + cinTable[currentHandle].half = qfalse; + cinTable[currentHandle].smootheddouble = qfalse; + + cinTable[currentHandle].VQ0 = cinTable[currentHandle].VQNormal; + cinTable[currentHandle].VQ1 = cinTable[currentHandle].VQBuffer; + + cinTable[currentHandle].t[0] = ( 0 - (unsigned int)cin.linbuf ) + (unsigned int)cin.linbuf + cinTable[currentHandle].screenDelta; + cinTable[currentHandle].t[1] = ( 0 - ( (unsigned int)cin.linbuf + cinTable[currentHandle].screenDelta ) ) + (unsigned int)cin.linbuf; + + cinTable[currentHandle].drawX = cinTable[currentHandle].CIN_WIDTH; + cinTable[currentHandle].drawY = cinTable[currentHandle].CIN_HEIGHT; + + // rage pro is very slow at 512 wide textures, voodoo can't do it at all + if ( glConfig.hardwareType == GLHW_RAGEPRO || glConfig.maxTextureSize <= 256 ) { + if ( cinTable[currentHandle].drawX > 256 ) { + cinTable[currentHandle].drawX = 256; + } + if ( cinTable[currentHandle].drawY > 256 ) { + cinTable[currentHandle].drawY = 256; + } + if ( cinTable[currentHandle].CIN_WIDTH != 256 || cinTable[currentHandle].CIN_HEIGHT != 256 ) { + Com_Printf( "HACK: approxmimating cinematic for Rage Pro or Voodoo\n" ); + } + } +#if defined( MACOS_X ) + cinTable[currentHandle].drawX = 256; + cinTable[currentHandle].drawX = 256; +#endif +} + +/****************************************************************************** +* +* Function: +* +* Description: +* +******************************************************************************/ + +static void RoQPrepMcomp( long xoff, long yoff ) { + long i, j, x, y, temp, temp2; + + i = cinTable[currentHandle].samplesPerLine; j = cinTable[currentHandle].samplesPerPixel; + if ( cinTable[currentHandle].xsize == ( cinTable[currentHandle].ysize * 4 ) && !cinTable[currentHandle].half ) { + j = j + j; i = i + i; + } + + for ( y = 0; y < 16; y++ ) { + temp2 = ( y + yoff - 8 ) * i; + for ( x = 0; x < 16; x++ ) { + temp = ( x + xoff - 8 ) * j; + cin.mcomp[( x * 16 ) + y] = cinTable[currentHandle].normalBuffer0 - ( temp2 + temp ); + } + } +} + +/****************************************************************************** +* +* Function: +* +* Description: +* +******************************************************************************/ + +static void initRoQ() { + if ( currentHandle < 0 ) { + return; + } + + cinTable[currentHandle].VQNormal = ( void( * ) ( byte *, void * ) )blitVQQuad32fs; + cinTable[currentHandle].VQBuffer = ( void( * ) ( byte *, void * ) )blitVQQuad32fs; + cinTable[currentHandle].samplesPerPixel = 4; + ROQ_GenYUVTables(); + RllSetupTable(); +} + +/****************************************************************************** +* +* Function: +* +* Description: +* +******************************************************************************/ +/* +static byte* RoQFetchInterlaced( byte *source ) { + int x, *src, *dst; + + if (currentHandle < 0) return NULL; + + src = (int *)source; + dst = (int *)cinTable[currentHandle].buf2; + + for(x=0;x<256*256;x++) { + *dst = *src; + dst++; src += 2; + } + return cinTable[currentHandle].buf2; +} +*/ +static void RoQReset() { + + if ( currentHandle < 0 ) { + return; + } + + Sys_EndStreamedFile( cinTable[currentHandle].iFile ); + + // DHM - Properly close file so we don't run out of handles + FS_FCloseFile( cinTable[currentHandle].iFile ); + cinTable[currentHandle].iFile = 0; + // dhm - end + + FS_FOpenFileRead( cinTable[currentHandle].fileName, &cinTable[currentHandle].iFile, qtrue ); + // let the background thread start reading ahead + Sys_BeginStreamedFile( cinTable[currentHandle].iFile, 0x10000 ); + Sys_StreamedRead( cin.file, 16, 1, cinTable[currentHandle].iFile ); + RoQ_init(); + cinTable[currentHandle].status = FMV_LOOPED; +} + +/****************************************************************************** +* +* Function: +* +* Description: +* +******************************************************************************/ + +static void RoQInterrupt( void ) { + byte *framedata; + short sbuf[32768]; + int ssize; + + if ( currentHandle < 0 ) { + return; + } +//resound: + Sys_StreamedRead( cin.file, cinTable[currentHandle].RoQFrameSize + 8, 1, cinTable[currentHandle].iFile ); + if ( cinTable[currentHandle].RoQPlayed >= cinTable[currentHandle].ROQSize ) { + if ( cinTable[currentHandle].holdAtEnd == qfalse ) { + if ( cinTable[currentHandle].looping ) { + RoQReset(); + } else { + cinTable[currentHandle].status = FMV_EOF; + } + } else { + cinTable[currentHandle].status = FMV_IDLE; + } + return; + } + framedata = cin.file; +// +// new frame is ready +// +redump: + switch ( cinTable[currentHandle].roq_id ) + { + case ROQ_QUAD_VQ: + if ( ( cinTable[currentHandle].numQuads & 1 ) ) { + cinTable[currentHandle].normalBuffer0 = cinTable[currentHandle].t[1]; + RoQPrepMcomp( cinTable[currentHandle].roqF0, cinTable[currentHandle].roqF1 ); + cinTable[currentHandle].VQ1( (byte *)cin.qStatus[1], framedata ); + cinTable[currentHandle].buf = cin.linbuf + cinTable[currentHandle].screenDelta; + } else { + cinTable[currentHandle].normalBuffer0 = cinTable[currentHandle].t[0]; + RoQPrepMcomp( cinTable[currentHandle].roqF0, cinTable[currentHandle].roqF1 ); + cinTable[currentHandle].VQ0( (byte *)cin.qStatus[0], framedata ); + cinTable[currentHandle].buf = cin.linbuf; + } + if ( cinTable[currentHandle].numQuads == 0 ) { // first frame + Com_Memcpy( cin.linbuf + cinTable[currentHandle].screenDelta, cin.linbuf, cinTable[currentHandle].samplesPerLine * cinTable[currentHandle].ysize ); + } + cinTable[currentHandle].numQuads++; + cinTable[currentHandle].dirty = qtrue; + break; + case ROQ_CODEBOOK: + decodeCodeBook( framedata, (unsigned short)cinTable[currentHandle].roq_flags ); + break; + case ZA_SOUND_MONO: + if ( !cinTable[currentHandle].silent ) { + ssize = RllDecodeMonoToStereo( framedata, sbuf, cinTable[currentHandle].RoQFrameSize, 0, (unsigned short)cinTable[currentHandle].roq_flags ); + S_RawSamples( ssize, 22050, 2, 1, (byte *)sbuf, 1.0f, 1.0f, CIN_STREAM ); + cinTable[currentHandle].sound = 1; + } + break; + case ZA_SOUND_STEREO: + if ( !cinTable[currentHandle].silent ) { + if ( cinTable[currentHandle].numQuads == -1 ) { + S_Update(); + Com_DPrintf( "S_Update: Setting rawend to %i\n", s_soundtime ); + s_rawend[CIN_STREAM] = s_soundtime; //DAJ added [CIN_STREAM] + } + ssize = RllDecodeStereoToStereo( framedata, sbuf, cinTable[currentHandle].RoQFrameSize, 0, (unsigned short)cinTable[currentHandle].roq_flags ); +// Com_Printf("%i\n", ssize+s_rawend[CIN_STREAM]- s_soundtime ); + S_RawSamples( ssize, 22050, 2, 2, (byte *)sbuf, 1.0f, 1.0f, CIN_STREAM ); + cinTable[currentHandle].sound = 1; + } + break; + case ROQ_QUAD_INFO: + if ( cinTable[currentHandle].numQuads == -1 ) { + readQuadInfo( framedata ); + setupQuad( 0, 0 ); + // we need to use CL_ScaledMilliseconds because of the smp mode calls from the renderer + cinTable[currentHandle].startTime = cinTable[currentHandle].lastTime = CL_ScaledMilliseconds() * com_timescale->value; + } + if ( cinTable[currentHandle].numQuads != 1 ) { + cinTable[currentHandle].numQuads = 0; + } + break; + case ROQ_PACKET: + cinTable[currentHandle].inMemory = cinTable[currentHandle].roq_flags; + cinTable[currentHandle].RoQFrameSize = 0; // for header + break; + case ROQ_QUAD_HANG: + cinTable[currentHandle].RoQFrameSize = 0; + break; + case ROQ_QUAD_JPEG: + break; + default: + cinTable[currentHandle].status = FMV_EOF; + break; + } +// +// read in next frame data +// + if ( cinTable[currentHandle].RoQPlayed >= cinTable[currentHandle].ROQSize ) { + if ( cinTable[currentHandle].holdAtEnd == qfalse ) { + if ( cinTable[currentHandle].looping ) { + RoQReset(); + } else { + cinTable[currentHandle].status = FMV_EOF; + } + } else { + cinTable[currentHandle].status = FMV_IDLE; + } + return; + } + + framedata += cinTable[currentHandle].RoQFrameSize; + cinTable[currentHandle].roq_id = framedata[0] + framedata[1] * 256; + cinTable[currentHandle].RoQFrameSize = framedata[2] + framedata[3] * 256 + framedata[4] * 65536; + cinTable[currentHandle].roq_flags = framedata[6] + framedata[7] * 256; + cinTable[currentHandle].roqF0 = (char)framedata[7]; + cinTable[currentHandle].roqF1 = (char)framedata[6]; + + if ( cinTable[currentHandle].RoQFrameSize > 65536 || cinTable[currentHandle].roq_id == 0x1084 ) { + Com_DPrintf( "roq_size>65536||roq_id==0x1084\n" ); + cinTable[currentHandle].status = FMV_EOF; + if ( cinTable[currentHandle].looping ) { + RoQReset(); + } + return; + } + if ( cinTable[currentHandle].inMemory && ( cinTable[currentHandle].status != FMV_EOF ) ) { + cinTable[currentHandle].inMemory--; framedata += 8; goto redump; + } + +// if (cinTable[currentHandle].roq_id == ZA_SOUND_STEREO) { +// / cinTable[currentHandle].RoQPlayed += cinTable[currentHandle].RoQFrameSize+8; +// goto resound; +// } + +// +// one more frame hits the dust +// +// assert(cinTable[currentHandle].RoQFrameSize <= 65536); +// r = Sys_StreamedRead( cin.file, cinTable[currentHandle].RoQFrameSize+8, 1, cinTable[currentHandle].iFile ); + cinTable[currentHandle].RoQPlayed += cinTable[currentHandle].RoQFrameSize + 8; +} + +/****************************************************************************** +* +* Function: +* +* Description: +* +******************************************************************************/ + +static void RoQ_init( void ) { + // we need to use CL_ScaledMilliseconds because of the smp mode calls from the renderer + cinTable[currentHandle].startTime = cinTable[currentHandle].lastTime = CL_ScaledMilliseconds() * com_timescale->value; + + cinTable[currentHandle].RoQPlayed = 24; + +/* get frame rate */ + cinTable[currentHandle].roqFPS = cin.file[ 6] + cin.file[ 7] * 256; + + if ( !cinTable[currentHandle].roqFPS ) { + cinTable[currentHandle].roqFPS = 30; + } + + cinTable[currentHandle].numQuads = -1; + + cinTable[currentHandle].roq_id = cin.file[ 8] + cin.file[ 9] * 256; + cinTable[currentHandle].RoQFrameSize = cin.file[10] + cin.file[11] * 256 + cin.file[12] * 65536; + cinTable[currentHandle].roq_flags = cin.file[14] + cin.file[15] * 256; + + if ( cinTable[currentHandle].RoQFrameSize > 65536 || !cinTable[currentHandle].RoQFrameSize ) { + return; + } + +} + +/****************************************************************************** +* +* Function: +* +* Description: +* +******************************************************************************/ + +static void RoQShutdown( void ) { + const char *s; + + if ( !cinTable[currentHandle].buf ) { + return; + } + + if ( cinTable[currentHandle].status == FMV_IDLE ) { + return; + } + Com_DPrintf( "finished cinematic\n" ); + cinTable[currentHandle].status = FMV_IDLE; + + if ( cinTable[currentHandle].iFile ) { + Sys_EndStreamedFile( cinTable[currentHandle].iFile ); + FS_FCloseFile( cinTable[currentHandle].iFile ); + cinTable[currentHandle].iFile = 0; + } + + if ( cinTable[currentHandle].alterGameState ) { + cls.state = CA_DISCONNECTED; + // we can't just do a vstr nextmap, because + // if we are aborting the intro cinematic with + // a devmap command, nextmap would be valid by + // the time it was referenced + s = Cvar_VariableString( "nextmap" ); + if ( s[0] ) { + Cbuf_ExecuteText( EXEC_APPEND, va( "%s\n", s ) ); + Cvar_Set( "nextmap", "" ); + } + CL_handle = -1; + } + cinTable[currentHandle].fileName[0] = 0; + currentHandle = -1; +} + +/* +================== +SCR_StopCinematic +================== +*/ +e_status CIN_StopCinematic( int handle ) { + + if ( handle < 0 || handle >= MAX_VIDEO_HANDLES || cinTable[handle].status == FMV_EOF ) { + return FMV_EOF; + } + currentHandle = handle; + + Com_DPrintf( "trFMV::stop(), closing %s\n", cinTable[currentHandle].fileName ); + + if ( !cinTable[currentHandle].buf ) { + return FMV_EOF; + } + + if ( cinTable[currentHandle].alterGameState ) { + if ( cls.state != CA_CINEMATIC ) { + return cinTable[currentHandle].status; + } + } + cinTable[currentHandle].status = FMV_EOF; + RoQShutdown(); + + return FMV_EOF; +} + +/* +================== +SCR_RunCinematic + +Fetch and decompress the pending frame +================== +*/ + + +e_status CIN_RunCinematic( int handle ) { + // bk001204 - init + int start = 0; + int thisTime = 0; + int played = 0; + + if ( handle < 0 || handle >= MAX_VIDEO_HANDLES || cinTable[handle].status == FMV_EOF ) { + return FMV_EOF; + } + + if ( cin.currentHandle != handle ) { + currentHandle = handle; + cin.currentHandle = currentHandle; + cinTable[currentHandle].status = FMV_EOF; + RoQReset(); + } + + if ( cinTable[handle].playonwalls < -1 ) { + return cinTable[handle].status; + } + + currentHandle = handle; + + if ( cinTable[currentHandle].alterGameState ) { + if ( cls.state != CA_CINEMATIC ) { + return cinTable[currentHandle].status; + } + } + + if ( cinTable[currentHandle].status == FMV_IDLE ) { + return cinTable[currentHandle].status; + } + + // we need to use CL_ScaledMilliseconds because of the smp mode calls from the renderer + thisTime = CL_ScaledMilliseconds(); + if ( cinTable[currentHandle].shader && ( abs( thisTime - cinTable[currentHandle].lastTime ) ) > 100 ) { + cinTable[currentHandle].startTime += thisTime - cinTable[currentHandle].lastTime; + } + +//----(SA) modified to use specified fps for roq's + + // we need to use CL_ScaledMilliseconds because of the smp mode calls from the renderer + cinTable[currentHandle].tfps = ( ( ( CL_ScaledMilliseconds() - cinTable[currentHandle].startTime ) * cinTable[currentHandle].roqFPS ) / 1000 ); + + start = cinTable[currentHandle].startTime; + while ( ( cinTable[currentHandle].tfps != cinTable[currentHandle].numQuads ) + && cinTable[currentHandle].status == FMV_PLAY ) + { + RoQInterrupt(); + if ( start != cinTable[currentHandle].startTime ) { + // we need to use CL_ScaledMilliseconds because of the smp mode calls from the renderer + cinTable[currentHandle].tfps = ( ( ( CL_ScaledMilliseconds() - cinTable[currentHandle].startTime ) * cinTable[currentHandle].roqFPS ) / 1000 ); + + start = cinTable[currentHandle].startTime; + } + played = 1; + } + +//DAJ added [CIN_STREAM]'s + if ( played && cinTable[currentHandle].sound ) { + if ( s_rawend[CIN_STREAM] < s_soundtime && ( s_soundtime - s_rawend[CIN_STREAM] ) < 100 ) { + cinTable[currentHandle].startTime -= ( s_soundtime - s_rawend[CIN_STREAM] ); + do { + RoQInterrupt(); + } while ( s_rawend[CIN_STREAM] < s_soundtime && cinTable[currentHandle].status == FMV_PLAY ); + } + } + + +//----(SA) end + + cinTable[currentHandle].lastTime = thisTime; + + if ( cinTable[currentHandle].status == FMV_LOOPED ) { + cinTable[currentHandle].status = FMV_PLAY; + } + + if ( cinTable[currentHandle].status == FMV_EOF ) { + if ( cinTable[currentHandle].looping ) { + RoQReset(); + } else { + RoQShutdown(); + } + } + + return cinTable[currentHandle].status; +} + +/* +================== +CL_PlayCinematic + +================== +*/ +int CIN_PlayCinematic( const char *arg, int x, int y, int w, int h, int systemBits ) { + unsigned short RoQID; + char name[MAX_OSPATH]; + int i; + + + // TODO: Laird says don't play cine's in widescreen mode + + + if ( strstr( arg, "/" ) == NULL && strstr( arg, "\\" ) == NULL ) { + Com_sprintf( name, sizeof( name ), "video/%s", arg ); + } else { + Com_sprintf( name, sizeof( name ), "%s", arg ); + } + + if ( !( systemBits & CIN_system ) ) { + for ( i = 0 ; i < MAX_VIDEO_HANDLES ; i++ ) { + if ( !Q_stricmp( cinTable[i].fileName, name ) ) { + return i; + } + } + } + + Com_DPrintf( "SCR_PlayCinematic( %s )\n", arg ); + + Com_Memset( &cin, 0, sizeof( cinematics_t ) ); + currentHandle = CIN_HandleForVideo(); + + cin.currentHandle = currentHandle; + + strcpy( cinTable[currentHandle].fileName, name ); + + cinTable[currentHandle].ROQSize = 0; + cinTable[currentHandle].ROQSize = FS_FOpenFileRead( cinTable[currentHandle].fileName, &cinTable[currentHandle].iFile, qtrue ); + + if ( cinTable[currentHandle].ROQSize <= 0 ) { + Com_DPrintf( "play(%s), ROQSize<=0\n", arg ); + cinTable[currentHandle].fileName[0] = 0; + return -1; + } + + CIN_SetExtents( currentHandle, x, y, w, h ); + CIN_SetLooping( currentHandle, ( systemBits & CIN_loop ) != 0 ); + + cinTable[currentHandle].CIN_HEIGHT = DEFAULT_CIN_HEIGHT; + cinTable[currentHandle].CIN_WIDTH = DEFAULT_CIN_WIDTH; + cinTable[currentHandle].holdAtEnd = ( systemBits & CIN_hold ) != 0; + cinTable[currentHandle].alterGameState = ( systemBits & CIN_system ) != 0; + cinTable[currentHandle].playonwalls = 1; + cinTable[currentHandle].silent = ( systemBits & CIN_silent ) != 0; + cinTable[currentHandle].shader = ( systemBits & CIN_shader ) != 0; + cinTable[currentHandle].letterBox = ( systemBits & CIN_letterBox ) != 0; + cinTable[currentHandle].sound = 0; + + if ( cinTable[currentHandle].alterGameState ) { + // close the menu + if ( uivm ) { + VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_NONE ); + } + } else { + cinTable[currentHandle].playonwalls = cl_inGameVideo->integer; + } + + initRoQ(); + + FS_Read( cin.file, 16, cinTable[currentHandle].iFile ); + + RoQID = ( unsigned short )( cin.file[0] ) + ( unsigned short )( cin.file[1] ) * 256; + if ( RoQID == 0x1084 ) { + RoQ_init(); +// FS_Read (cin.file, cinTable[currentHandle].RoQFrameSize+8, cinTable[currentHandle].iFile); + // let the background thread start reading ahead + Sys_BeginStreamedFile( cinTable[currentHandle].iFile, 0x10000 ); + + cinTable[currentHandle].status = FMV_PLAY; + Com_DPrintf( "trFMV::play(), playing %s\n", arg ); + + if ( cinTable[currentHandle].alterGameState ) { + cls.state = CA_CINEMATIC; + } + + Con_Close(); + + Com_DPrintf( "Setting rawend to %i\n", s_soundtime ); + s_rawend[CIN_STREAM] = s_soundtime; + + return currentHandle; + } + Com_DPrintf( "trFMV::play(), invalid RoQ ID\n" ); + + RoQShutdown(); + return -1; +} + +void CIN_SetExtents( int handle, int x, int y, int w, int h ) { + if ( handle < 0 || handle >= MAX_VIDEO_HANDLES || cinTable[handle].status == FMV_EOF ) { + return; + } + cinTable[handle].xpos = x; + cinTable[handle].ypos = y; + cinTable[handle].width = w; + cinTable[handle].height = h; + cinTable[handle].dirty = qtrue; +} + +void CIN_SetLooping( int handle, qboolean loop ) { + if ( handle < 0 || handle >= MAX_VIDEO_HANDLES || cinTable[handle].status == FMV_EOF ) { + return; + } + cinTable[handle].looping = loop; +} + +/* +================== +SCR_DrawCinematic + +================== +*/ +void CIN_DrawCinematic( int handle ) { + float x, y, w, h; //, barheight; + byte *buf; \ + + if ( handle < 0 || handle >= MAX_VIDEO_HANDLES || cinTable[handle].status == FMV_EOF ) { + return; + } + + if ( !cinTable[handle].buf ) { + return; + } + + x = cinTable[handle].xpos; + y = cinTable[handle].ypos; + w = cinTable[handle].width; + h = cinTable[handle].height; + buf = cinTable[handle].buf; + SCR_AdjustFrom640( &x, &y, &w, &h ); + + + if ( cinTable[handle].letterBox ) { + float barheight; + float vh; + vh = (float)cls.glconfig.vidHeight; + + barheight = ( (float)LETTERBOX_OFFSET / 480.0f ) * vh; //----(SA) added + + re.SetColor( &colorBlack[0] ); +// re.DrawStretchPic( 0, 0, SCREEN_WIDTH, LETTERBOX_OFFSET, 0, 0, 0, 0, cls.whiteShader ); +// re.DrawStretchPic( 0, SCREEN_HEIGHT-LETTERBOX_OFFSET, SCREEN_WIDTH, LETTERBOX_OFFSET, 0, 0, 0, 0, cls.whiteShader ); + //----(SA) adjust for 640x480 + re.DrawStretchPic( 0, 0, w, barheight, 0, 0, 0, 0, cls.whiteShader ); + re.DrawStretchPic( 0, vh - barheight - 1, w, barheight + 1, 0, 0, 0, 0, cls.whiteShader ); + } + + if ( cinTable[handle].dirty && ( cinTable[handle].CIN_WIDTH != cinTable[handle].drawX || cinTable[handle].CIN_HEIGHT != cinTable[handle].drawY ) ) { + int ix, iy, *buf2, *buf3, xm, ym, ll; + + xm = cinTable[handle].CIN_WIDTH / 256; + ym = cinTable[handle].CIN_HEIGHT / 256; + ll = 8; + if ( cinTable[handle].CIN_WIDTH == 512 ) { + ll = 9; + } + + buf3 = (int*)buf; + buf2 = Hunk_AllocateTempMemory( 256 * 256 * 4 ); + if ( xm == 2 && ym == 2 ) { + byte *bc2, *bc3; + int ic, iiy; + + bc2 = (byte *)buf2; + bc3 = (byte *)buf3; + for ( iy = 0; iy < 256; iy++ ) { + iiy = iy << 12; + for ( ix = 0; ix < 2048; ix += 8 ) { + for ( ic = ix; ic < ( ix + 4 ); ic++ ) { + *bc2 = ( bc3[iiy + ic] + bc3[iiy + 4 + ic] + bc3[iiy + 2048 + ic] + bc3[iiy + 2048 + 4 + ic] ) >> 2; + bc2++; + } + } + } + } else if ( xm == 2 && ym == 1 ) { + byte *bc2, *bc3; + int ic, iiy; + + bc2 = (byte *)buf2; + bc3 = (byte *)buf3; + for ( iy = 0; iy < 256; iy++ ) { + iiy = iy << 11; + for ( ix = 0; ix < 2048; ix += 8 ) { + for ( ic = ix; ic < ( ix + 4 ); ic++ ) { + *bc2 = ( bc3[iiy + ic] + bc3[iiy + 4 + ic] ) >> 1; + bc2++; + } + } + } + } else { + for ( iy = 0; iy < 256; iy++ ) { + for ( ix = 0; ix < 256; ix++ ) { + buf2[( iy << 8 ) + ix] = buf3[( ( iy * ym ) << ll ) + ( ix * xm )]; + } + } + } + re.DrawStretchRaw( x, y, w, h, 256, 256, (byte *)buf2, handle, qtrue ); + cinTable[handle].dirty = qfalse; + Hunk_FreeTempMemory( buf2 ); + return; + } + + re.DrawStretchRaw( x, y, w, h, cinTable[handle].drawX, cinTable[handle].drawY, buf, handle, cinTable[handle].dirty ); + cinTable[handle].dirty = qfalse; +} + +/* +============== +CL_PlayCinematic_f +============== +*/ +void CL_PlayCinematic_f( void ) { + char *arg, *s; + qboolean holdatend; + int bits = CIN_system; + + Com_DPrintf( "CL_PlayCinematic_f\n" ); + if ( cls.state == CA_CINEMATIC ) { + SCR_StopCinematic(); + } + + arg = Cmd_Argv( 1 ); + s = Cmd_Argv( 2 ); + + holdatend = qfalse; + if ( ( s && s[0] == '1' ) || Q_stricmp( arg,"demoend.roq" ) == 0 || Q_stricmp( arg,"end.roq" ) == 0 ) { + bits |= CIN_hold; + } + if ( s && s[0] == '2' ) { + bits |= CIN_loop; + } + if ( s && s[0] == '3' ) { + bits |= CIN_letterBox; + } + + S_StopAllSounds(); + // make sure volume is up for cine + S_FadeAllSounds( 1, 0 ); + + if ( bits & CIN_letterBox ) { + CL_handle = CIN_PlayCinematic( arg, 0, LETTERBOX_OFFSET, SCREEN_WIDTH, SCREEN_HEIGHT - ( LETTERBOX_OFFSET * 2 ), bits ); + } else { + CL_handle = CIN_PlayCinematic( arg, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, bits ); + } + + if ( CL_handle >= 0 ) { + do { + SCR_RunCinematic(); + } while ( cinTable[currentHandle].buf == NULL && cinTable[currentHandle].status == FMV_PLAY ); // wait for first frame (load codebook and sound) + } +} + + +void SCR_DrawCinematic( void ) { + if ( CL_handle >= 0 && CL_handle < MAX_VIDEO_HANDLES ) { + CIN_DrawCinematic( CL_handle ); + } +} + +void SCR_RunCinematic( void ) { + if ( CL_handle >= 0 && CL_handle < MAX_VIDEO_HANDLES ) { + CIN_RunCinematic( CL_handle ); + } +} + +void SCR_StopCinematic( void ) { + if ( CL_handle >= 0 && CL_handle < MAX_VIDEO_HANDLES ) { + CIN_StopCinematic( CL_handle ); + S_StopAllSounds(); + CL_handle = -1; + } +} + +void CIN_UploadCinematic( int handle ) { + if ( handle >= 0 && handle < MAX_VIDEO_HANDLES ) { + if ( !cinTable[handle].buf ) { + return; + } + if ( cinTable[handle].playonwalls <= 0 && cinTable[handle].dirty ) { + if ( cinTable[handle].playonwalls == 0 ) { + cinTable[handle].playonwalls = -1; + } else { + if ( cinTable[handle].playonwalls == -1 ) { + cinTable[handle].playonwalls = -2; + } else { + cinTable[handle].dirty = qfalse; + } + } + } + re.UploadCinematic( 256, 256, 256, 256, cinTable[handle].buf, handle, cinTable[handle].dirty ); + if ( cl_inGameVideo->integer == 0 && cinTable[handle].playonwalls == 1 ) { + cinTable[handle].playonwalls--; + } + } +} + diff --git a/src/client/cl_console.c b/src/client/cl_console.c new file mode 100644 index 0000000..bf6a958 --- /dev/null +++ b/src/client/cl_console.c @@ -0,0 +1,866 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// console.c + +#include "client.h" + + +int g_console_field_width = 78; + + +#define COLNSOLE_COLOR COLOR_WHITE //COLOR_BLACK + +#define NUM_CON_TIMES 4 + +//#define CON_TEXTSIZE 32768 +#define CON_TEXTSIZE 65536 // (SA) DM want's more console... + +typedef struct { + qboolean initialized; + + short text[CON_TEXTSIZE]; + int current; // line where next message will be printed + int x; // offset in current line for next print + int display; // bottom of console displays this line + + int linewidth; // characters across screen + int totallines; // total lines in console scrollback + + float xadjust; // for wide aspect screens + + float displayFrac; // aproaches finalFrac at scr_conspeed + float finalFrac; // 0.0 to 1.0 lines of console to display + + int vislines; // in scanlines + + int times[NUM_CON_TIMES]; // cls.realtime time the line was generated + // for transparent notify lines + vec4_t color; +} console_t; + +extern console_t con; + +console_t con; + +cvar_t *con_debug; +cvar_t *con_conspeed; +cvar_t *con_notifytime; + +#define DEFAULT_CONSOLE_WIDTH 78 + +vec4_t console_color = {1.0, 1.0, 1.0, 1.0}; + + +/* +================ +Con_ToggleConsole_f +================ +*/ +void Con_ToggleConsole_f( void ) { + // closing a full screen console restarts the demo loop + if ( cls.state == CA_DISCONNECTED && cls.keyCatchers == KEYCATCH_CONSOLE ) { + CL_StartDemoLoop(); + return; + } + + Field_Clear( &g_consoleField ); + g_consoleField.widthInChars = g_console_field_width; + + Con_ClearNotify(); + cls.keyCatchers ^= KEYCATCH_CONSOLE; +} + +/* +================ +Con_MessageMode_f +================ +*/ +void Con_MessageMode_f( void ) { + chat_playerNum = -1; + chat_team = qfalse; +// chat_limbo = qfalse; // NERVE - SMF + Field_Clear( &chatField ); + chatField.widthInChars = 30; + + cls.keyCatchers ^= KEYCATCH_MESSAGE; +} + +/* +================ +Con_MessageMode2_f +================ +*/ +void Con_MessageMode2_f( void ) { + chat_playerNum = -1; + chat_team = qtrue; +// chat_limbo = qfalse; // NERVE - SMF + Field_Clear( &chatField ); + chatField.widthInChars = 25; + cls.keyCatchers ^= KEYCATCH_MESSAGE; +} + +/* +================ +Con_MessageMode3_f +================ +*/ +void Con_MessageMode3_f( void ) { + chat_playerNum = VM_Call( cgvm, CG_CROSSHAIR_PLAYER ); + if ( chat_playerNum < 0 || chat_playerNum >= MAX_CLIENTS ) { + chat_playerNum = -1; + return; + } + chat_team = qfalse; +// chat_limbo = qfalse; // NERVE - SMF + Field_Clear( &chatField ); + chatField.widthInChars = 30; + cls.keyCatchers ^= KEYCATCH_MESSAGE; +} + +/* +================ +Con_MessageMode4_f +================ +*/ +void Con_MessageMode4_f( void ) { + chat_playerNum = VM_Call( cgvm, CG_LAST_ATTACKER ); + if ( chat_playerNum < 0 || chat_playerNum >= MAX_CLIENTS ) { + chat_playerNum = -1; + return; + } + chat_team = qfalse; +// chat_limbo = qfalse; // NERVE - SMF + Field_Clear( &chatField ); + chatField.widthInChars = 30; + cls.keyCatchers ^= KEYCATCH_MESSAGE; +} + +// NERVE - SMF +/* +================ +Con_StartLimboMode_f +================ +*/ +void Con_StartLimboMode_f( void ) { +// chat_playerNum = -1; +// chat_team = qfalse; + chat_limbo = qtrue; // NERVE - SMF +// Field_Clear( &chatField ); +// chatField.widthInChars = 30; + +// cls.keyCatchers ^= KEYCATCH_MESSAGE; +} + +/* +================ +Con_StopLimboMode_f +================ +*/ +void Con_StopLimboMode_f( void ) { +// chat_playerNum = -1; +// chat_team = qfalse; + chat_limbo = qfalse; // NERVE - SMF +// Field_Clear( &chatField ); +// chatField.widthInChars = 30; + +// cls.keyCatchers &= ~KEYCATCH_MESSAGE; +} +// -NERVE - SMF + +/* +================ +Con_Clear_f +================ +*/ +void Con_Clear_f( void ) { + int i; + + for ( i = 0 ; i < CON_TEXTSIZE ; i++ ) { + con.text[i] = ( ColorIndex( COLNSOLE_COLOR ) << 8 ) | ' '; + } + + Con_Bottom(); // go to end +} + + +/* +================ +Con_Dump_f + +Save the console contents out to a file +================ +*/ +void Con_Dump_f( void ) { + int l, x, i; + short *line; + fileHandle_t f; + char buffer[1024]; + + if ( Cmd_Argc() != 2 ) { + Com_Printf( "usage: condump \n" ); + return; + } + + Com_Printf( "Dumped console text to %s.\n", Cmd_Argv( 1 ) ); + +#ifdef __MACOS__ //DAJ MacOS file typing + { + extern _MSL_IMP_EXP_C long _fcreator, _ftype; + _ftype = 'TEXT'; + _fcreator = 'R*ch'; + } +#endif + f = FS_FOpenFileWrite( Cmd_Argv( 1 ) ); + if ( !f ) { + Com_Printf( "ERROR: couldn't open.\n" ); + return; + } + + // skip empty lines + for ( l = con.current - con.totallines + 1 ; l <= con.current ; l++ ) + { + line = con.text + ( l % con.totallines ) * con.linewidth; + for ( x = 0 ; x < con.linewidth ; x++ ) + if ( ( line[x] & 0xff ) != ' ' ) { + break; + } + if ( x != con.linewidth ) { + break; + } + } + + // write the remaining lines + buffer[con.linewidth] = 0; + for ( ; l <= con.current ; l++ ) + { + line = con.text + ( l % con.totallines ) * con.linewidth; + for ( i = 0; i < con.linewidth; i++ ) + buffer[i] = line[i] & 0xff; + for ( x = con.linewidth - 1 ; x >= 0 ; x-- ) + { + if ( buffer[x] == ' ' ) { + buffer[x] = 0; + } else { + break; + } + } + strcat( buffer, "\n" ); + FS_Write( buffer, strlen( buffer ), f ); + } + + FS_FCloseFile( f ); +} + + +/* +================ +Con_ClearNotify +================ +*/ +void Con_ClearNotify( void ) { + int i; + + for ( i = 0 ; i < NUM_CON_TIMES ; i++ ) { + con.times[i] = 0; + } +} + + + +/* +================ +Con_CheckResize + +If the line width has changed, reformat the buffer. +================ +*/ +void Con_CheckResize( void ) { + int i, j, width, oldwidth, oldtotallines, numlines, numchars; + MAC_STATIC short tbuf[CON_TEXTSIZE]; + + width = ( SCREEN_WIDTH / SMALLCHAR_WIDTH ) - 2; + + if ( width == con.linewidth ) { + return; + } + + if ( width < 1 ) { // video hasn't been initialized yet + width = DEFAULT_CONSOLE_WIDTH; + con.linewidth = width; + con.totallines = CON_TEXTSIZE / con.linewidth; + for ( i = 0; i < CON_TEXTSIZE; i++ ) + + con.text[i] = ( ColorIndex( COLNSOLE_COLOR ) << 8 ) | ' '; + } else + { + oldwidth = con.linewidth; + con.linewidth = width; + oldtotallines = con.totallines; + con.totallines = CON_TEXTSIZE / con.linewidth; + numlines = oldtotallines; + + if ( con.totallines < numlines ) { + numlines = con.totallines; + } + + numchars = oldwidth; + + if ( con.linewidth < numchars ) { + numchars = con.linewidth; + } + + memcpy( tbuf, con.text, CON_TEXTSIZE * sizeof( short ) ); + for ( i = 0; i < CON_TEXTSIZE; i++ ) + + con.text[i] = ( ColorIndex( COLNSOLE_COLOR ) << 8 ) | ' '; + + + for ( i = 0 ; i < numlines ; i++ ) + { + for ( j = 0 ; j < numchars ; j++ ) + { + con.text[( con.totallines - 1 - i ) * con.linewidth + j] = + tbuf[( ( con.current - i + oldtotallines ) % + oldtotallines ) * oldwidth + j]; + } + } + + Con_ClearNotify(); + } + + con.current = con.totallines - 1; + con.display = con.current; +} + + +/* +================ +Con_Init +================ +*/ +void Con_Init( void ) { + int i; + + con_notifytime = Cvar_Get( "con_notifytime", "3", 0 ); + con_conspeed = Cvar_Get( "scr_conspeed", "3", 0 ); + con_debug = Cvar_Get( "con_debug", "0", CVAR_ARCHIVE ); //----(SA) added + + Field_Clear( &g_consoleField ); + g_consoleField.widthInChars = g_console_field_width; + for ( i = 0 ; i < COMMAND_HISTORY ; i++ ) { + Field_Clear( &historyEditLines[i] ); + historyEditLines[i].widthInChars = g_console_field_width; + } + + Cmd_AddCommand( "toggleconsole", Con_ToggleConsole_f ); + Cmd_AddCommand( "messagemode", Con_MessageMode_f ); + Cmd_AddCommand( "messagemode2", Con_MessageMode2_f ); + Cmd_AddCommand( "messagemode3", Con_MessageMode3_f ); + Cmd_AddCommand( "messagemode4", Con_MessageMode4_f ); + Cmd_AddCommand( "startLimboMode", Con_StartLimboMode_f ); // NERVE - SMF + Cmd_AddCommand( "stopLimboMode", Con_StopLimboMode_f ); // NERVE - SMF + Cmd_AddCommand( "clear", Con_Clear_f ); + Cmd_AddCommand( "condump", Con_Dump_f ); +} + + +/* +=============== +Con_Linefeed +=============== +*/ +void Con_Linefeed( void ) { + int i; + + // mark time for transparent overlay + if ( con.current >= 0 ) { + con.times[con.current % NUM_CON_TIMES] = cls.realtime; + } + + con.x = 0; + if ( con.display == con.current ) { + con.display++; + } + con.current++; + for ( i = 0; i < con.linewidth; i++ ) + con.text[( con.current % con.totallines ) * con.linewidth + i] = ( ColorIndex( COLNSOLE_COLOR ) << 8 ) | ' '; +} + +/* +================ +CL_ConsolePrint + +Handles cursor positioning, line wrapping, etc +All console printing must go through this in order to be logged to disk +If no console is visible, the text will appear at the top of the game window +================ +*/ +void CL_ConsolePrint( char *txt ) { + int y; + int c, l; + int color; + + // for some demos we don't want to ever show anything on the console + if ( cl_noprint && cl_noprint->integer ) { + return; + } + + if ( !con.initialized ) { + con.color[0] = + con.color[1] = + con.color[2] = + con.color[3] = 1.0f; + con.linewidth = -1; + Con_CheckResize(); + con.initialized = qtrue; + } + + color = ColorIndex( COLNSOLE_COLOR ); + + while ( ( c = *txt ) != 0 ) { + if ( Q_IsColorString( txt ) ) { + color = ColorIndex( *( txt + 1 ) ); + txt += 2; + continue; + } + + // count word length + for ( l = 0 ; l < con.linewidth ; l++ ) { + if ( txt[l] <= ' ' ) { + break; + } + + } + + // word wrap + if ( l != con.linewidth && ( con.x + l >= con.linewidth ) ) { + Con_Linefeed(); + + } + + txt++; + + switch ( c ) + { + case '\n': + Con_Linefeed(); + break; + case '\r': + con.x = 0; + break; + default: // display character and advance + y = con.current % con.totallines; + con.text[y * con.linewidth + con.x] = ( color << 8 ) | c; + con.x++; + if ( con.x >= con.linewidth ) { + + Con_Linefeed(); + con.x = 0; + } + break; + } + } + + + // mark time for transparent overlay + + if ( con.current >= 0 ) { + con.times[con.current % NUM_CON_TIMES] = cls.realtime; + } +} + + +/* +============================================================================== + +DRAWING + +============================================================================== +*/ + + +/* +================ +Con_DrawInput + +Draw the editline after a ] prompt +================ +*/ +void Con_DrawInput( void ) { + int y; + + if ( cls.state != CA_DISCONNECTED && !( cls.keyCatchers & KEYCATCH_CONSOLE ) ) { + return; + } + + y = con.vislines - ( SMALLCHAR_HEIGHT * 2 ); + + re.SetColor( con.color ); + + SCR_DrawSmallChar( con.xadjust + 1 * SMALLCHAR_WIDTH, y, ']' ); + + Field_Draw( &g_consoleField, con.xadjust + 2 * SMALLCHAR_WIDTH, y, + SCREEN_WIDTH - 3 * SMALLCHAR_WIDTH, qtrue ); +} + + +/* +================ +Con_DrawNotify + +Draws the last few lines of output transparently over the game top +================ +*/ +void Con_DrawNotify( void ) { + int x, v; + short *text; + int i; + int time; + int skip; + int currentColor; + + currentColor = 7; + re.SetColor( g_color_table[currentColor] ); + + v = 0; + for ( i = con.current - NUM_CON_TIMES + 1 ; i <= con.current ; i++ ) + { + if ( i < 0 ) { + continue; + } + time = con.times[i % NUM_CON_TIMES]; + if ( time == 0 ) { + continue; + } + time = cls.realtime - time; + if ( time > con_notifytime->value * 1000 ) { + continue; + } + text = con.text + ( i % con.totallines ) * con.linewidth; + + if ( cl.snap.ps.pm_type != PM_INTERMISSION && cls.keyCatchers & ( KEYCATCH_UI | KEYCATCH_CGAME ) ) { + continue; + } + + for ( x = 0 ; x < con.linewidth ; x++ ) { + if ( ( text[x] & 0xff ) == ' ' ) { + continue; + } + if ( ( ( text[x] >> 8 ) & 7 ) != currentColor ) { + currentColor = ( text[x] >> 8 ) & 7; + re.SetColor( g_color_table[currentColor] ); + } + SCR_DrawSmallChar( cl_conXOffset->integer + con.xadjust + ( x + 1 ) * SMALLCHAR_WIDTH, v, text[x] & 0xff ); + } + + v += SMALLCHAR_HEIGHT; + } + + re.SetColor( NULL ); + + if ( cls.keyCatchers & ( KEYCATCH_UI | KEYCATCH_CGAME ) ) { + return; + } + + // draw the chat line + if ( cls.keyCatchers & KEYCATCH_MESSAGE ) { + if ( chat_team ) { + SCR_DrawBigString( 8, v, "say_team:", 1.0f ); + skip = 11; + } else + { + SCR_DrawBigString( 8, v, "say:", 1.0f ); + skip = 5; + } + + Field_BigDraw( &chatField, skip * BIGCHAR_WIDTH, v, + SCREEN_WIDTH - ( skip + 1 ) * BIGCHAR_WIDTH, qtrue ); + + v += BIGCHAR_HEIGHT; + } + +} + +/* +================ +Con_DrawSolidConsole + +Draws the console with the solid background +================ +*/ + +void Con_DrawSolidConsole( float frac ) { + int i, x, y; + int rows; + short *text; + int row; + int lines; +// qhandle_t conShader; + int currentColor; + vec4_t color; + + lines = cls.glconfig.vidHeight * frac; + if ( lines <= 0 ) { + return; + } + + if ( lines > cls.glconfig.vidHeight ) { + lines = cls.glconfig.vidHeight; + } + + // on wide screens, we will center the text + con.xadjust = 0; + SCR_AdjustFrom640( &con.xadjust, NULL, NULL, NULL ); + + // draw the background + y = frac * SCREEN_HEIGHT - 2; + if ( y < 1 ) { + y = 0; + } else { + SCR_DrawPic( 0, 0, SCREEN_WIDTH, y, cls.consoleShader ); + + if ( frac >= 0.5f ) { // only draw when the console is down all the way (for now) + color[0] = color[1] = color[2] = frac * 2.0f; + color[3] = 1.0f; + re.SetColor( color ); + + // draw the logo + SCR_DrawPic( 192, 70, 256, 128, cls.consoleShader2 ); + re.SetColor( NULL ); + } + } + + color[0] = 0; + color[1] = 0; + color[2] = 0; + color[3] = 0.6f; + SCR_FillRect( 0, y, SCREEN_WIDTH, 2, color ); + + // draw the version number + + re.SetColor( g_color_table[ColorIndex( COLNSOLE_COLOR )] ); + + i = strlen( Q3_VERSION ); + + for ( x = 0 ; x < i ; x++ ) { + + SCR_DrawSmallChar( cls.glconfig.vidWidth - ( i - x ) * SMALLCHAR_WIDTH, + + ( lines - ( SMALLCHAR_HEIGHT + SMALLCHAR_HEIGHT / 2 ) ), Q3_VERSION[x] ); + + } + + + // draw the text + con.vislines = lines; + rows = ( lines - SMALLCHAR_WIDTH ) / SMALLCHAR_WIDTH; // rows of text to draw + + y = lines - ( SMALLCHAR_HEIGHT * 3 ); + + // draw from the bottom up + if ( con.display != con.current ) { + // draw arrows to show the buffer is backscrolled + re.SetColor( g_color_table[ColorIndex( COLOR_WHITE )] ); + for ( x = 0 ; x < con.linewidth ; x += 4 ) + SCR_DrawSmallChar( con.xadjust + ( x + 1 ) * SMALLCHAR_WIDTH, y, '^' ); + y -= SMALLCHAR_HEIGHT; + rows--; + } + + row = con.display; + + if ( con.x == 0 ) { + row--; + } + + currentColor = 7; + re.SetColor( g_color_table[currentColor] ); + + for ( i = 0 ; i < rows ; i++, y -= SMALLCHAR_HEIGHT, row-- ) + { + if ( row < 0 ) { + break; + } + if ( con.current - row >= con.totallines ) { + // past scrollback wrap point + continue; + } + + text = con.text + ( row % con.totallines ) * con.linewidth; + + for ( x = 0 ; x < con.linewidth ; x++ ) { + if ( ( text[x] & 0xff ) == ' ' ) { + continue; + } + + if ( ( ( text[x] >> 8 ) & 7 ) != currentColor ) { + currentColor = ( text[x] >> 8 ) & 7; + re.SetColor( g_color_table[currentColor] ); + } + SCR_DrawSmallChar( con.xadjust + ( x + 1 ) * SMALLCHAR_WIDTH, y, text[x] & 0xff ); + } + } + + // draw the input prompt, user text, and cursor if desired + Con_DrawInput(); + + re.SetColor( NULL ); +} + + + +/* +================== +Con_DrawConsole +================== +*/ +void Con_DrawConsole( void ) { + // check for console width changes from a vid mode change + Con_CheckResize(); + + // if disconnected, render console full screen + switch ( cls.state ) { + case CA_UNINITIALIZED: + case CA_CONNECTING: // sending request packets to the server + case CA_CHALLENGING: // sending challenge packets to the server + case CA_CONNECTED: // netchan_t established, getting gamestate + case CA_PRIMED: // got gamestate, waiting for first frame + case CA_LOADING: // only during cgame initialization, never during main loop + if ( !con_debug->integer ) { // these are all 'no console at all' when con_debug is not set + return; + } + + if ( cls.keyCatchers & KEYCATCH_UI ) { + return; + } + + Con_DrawSolidConsole( 1.0 ); + return; + + case CA_DISCONNECTED: // not talking to a server + if ( !( cls.keyCatchers & KEYCATCH_UI ) ) { + Con_DrawSolidConsole( 1.0 ); + return; + } + break; + + case CA_ACTIVE: // game views should be displayed + if ( con.displayFrac ) { + if ( con_debug->integer == 2 ) { // 2 means draw full screen console at '~' +// Con_DrawSolidConsole( 1.0f ); + Con_DrawSolidConsole( con.displayFrac * 2.0f ); + return; + } + } + + break; + + + case CA_CINEMATIC: // playing a cinematic or a static pic, not connected to a server + default: + break; + } + + if ( con.displayFrac ) { + Con_DrawSolidConsole( con.displayFrac ); + } else { + Con_DrawNotify(); // draw notify lines + } +} + +//================================================================ + +/* +================== +Con_RunConsole + +Scroll it up or down +================== +*/ +void Con_RunConsole( void ) { + // decide on the destination height of the console + if ( cls.keyCatchers & KEYCATCH_CONSOLE ) { + con.finalFrac = 0.5; // half screen + } else { + con.finalFrac = 0; // none visible + + } + // scroll towards the destination height + if ( con.finalFrac < con.displayFrac ) { + con.displayFrac -= con_conspeed->value * cls.realFrametime * 0.001; + if ( con.finalFrac > con.displayFrac ) { + con.displayFrac = con.finalFrac; + } + + } else if ( con.finalFrac > con.displayFrac ) { + con.displayFrac += con_conspeed->value * cls.realFrametime * 0.001; + if ( con.finalFrac < con.displayFrac ) { + con.displayFrac = con.finalFrac; + } + } + +} + + +void Con_PageUp( void ) { + con.display -= 2; + if ( con.current - con.display >= con.totallines ) { + con.display = con.current - con.totallines + 1; + } +} + +void Con_PageDown( void ) { + con.display += 2; + if ( con.display > con.current ) { + con.display = con.current; + } +} + +void Con_Top( void ) { + con.display = con.totallines; + if ( con.current - con.display >= con.totallines ) { + con.display = con.current - con.totallines + 1; + } +} + +void Con_Bottom( void ) { + con.display = con.current; +} + + +void Con_Close( void ) { + if ( !com_cl_running->integer ) { + return; + } + Field_Clear( &g_consoleField ); + Con_ClearNotify(); + cls.keyCatchers &= ~KEYCATCH_CONSOLE; + con.finalFrac = 0; // none visible + con.displayFrac = 0; +} diff --git a/src/client/cl_input.c b/src/client/cl_input.c new file mode 100644 index 0000000..687251f --- /dev/null +++ b/src/client/cl_input.c @@ -0,0 +1,1037 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// cl.input.c -- builds an intended movement command to send to the server + +#include "client.h" + +unsigned frame_msec; +int old_com_frameTime; + +/* +=============================================================================== + +KEY BUTTONS + +Continuous button event tracking is complicated by the fact that two different +input sources (say, mouse button 1 and the control key) can both press the +same button, but the button should only be released when both of the +pressing key have been released. + +When a key event issues a button command (+forward, +attack, etc), it appends +its key number as argv(1) so it can be matched up with the release. + +argv(2) will be set to the time the event happened, which allows exact +control even at low framerates when the down and up events may both get qued +at the same time. + +=============================================================================== +*/ + +static kbutton_t kb[NUM_BUTTONS]; + +void IN_MLookDown( void ) { + kb[KB_MLOOK].active = qtrue; +} + +void IN_MLookUp( void ) { + kb[KB_MLOOK].active = qfalse; + if ( !cl_freelook->integer ) { + IN_CenterView(); + } +} + +void IN_KeyDown( kbutton_t *b ) { + int k; + char *c; + + c = Cmd_Argv( 1 ); + if ( c[0] ) { + k = atoi( c ); + } else { + k = -1; // typed manually at the console for continuous down + } + + if ( k == b->down[0] || k == b->down[1] ) { + return; // repeating key + } + + if ( !b->down[0] ) { + b->down[0] = k; + } else if ( !b->down[1] ) { + b->down[1] = k; + } else { + Com_Printf( "Three keys down for a button!\n" ); + return; + } + + if ( b->active ) { + return; // still down + } + + // save timestamp for partial frame summing + c = Cmd_Argv( 2 ); + b->downtime = atoi( c ); + + b->active = qtrue; + b->wasPressed = qtrue; +} + +void IN_KeyUp( kbutton_t *b ) { + int k; + char *c; + unsigned uptime; + + c = Cmd_Argv( 1 ); + if ( c[0] ) { + k = atoi( c ); + } else { + // typed manually at the console, assume for unsticking, so clear all + b->down[0] = b->down[1] = 0; + b->active = qfalse; + return; + } + + if ( b->down[0] == k ) { + b->down[0] = 0; + } else if ( b->down[1] == k ) { + b->down[1] = 0; + } else { + return; // key up without coresponding down (menu pass through) + } + if ( b->down[0] || b->down[1] ) { + return; // some other key is still holding it down + } + + b->active = qfalse; + + // save timestamp for partial frame summing + c = Cmd_Argv( 2 ); + uptime = atoi( c ); + if ( uptime ) { + b->msec += uptime - b->downtime; + } else { + b->msec += frame_msec / 2; + } + + b->active = qfalse; +} + + + +/* +=============== +CL_KeyState + +Returns the fraction of the frame that the key was down +=============== +*/ +float CL_KeyState( kbutton_t *key ) { + float val; + int msec; + + msec = key->msec; + key->msec = 0; + + if ( key->active ) { + // still down + if ( !key->downtime ) { + msec = com_frameTime; + } else { + msec += com_frameTime - key->downtime; + } + key->downtime = com_frameTime; + } + +#if 0 + if ( msec ) { + Com_Printf( "%i ", msec ); + } +#endif + + val = (float)msec / frame_msec; + if ( val < 0 ) { + val = 0; + } + if ( val > 1 ) { + val = 1; + } + + return val; +} + + + +void IN_UpDown( void ) {IN_KeyDown( &kb[KB_UP] );} +void IN_UpUp( void ) {IN_KeyUp( &kb[KB_UP] );} +void IN_DownDown( void ) {IN_KeyDown( &kb[KB_DOWN] );} +void IN_DownUp( void ) {IN_KeyUp( &kb[KB_DOWN] );} +void IN_LeftDown( void ) {IN_KeyDown( &kb[KB_LEFT] );} +void IN_LeftUp( void ) {IN_KeyUp( &kb[KB_LEFT] );} +void IN_RightDown( void ) {IN_KeyDown( &kb[KB_RIGHT] );} +void IN_RightUp( void ) {IN_KeyUp( &kb[KB_RIGHT] );} +void IN_ForwardDown( void ) {IN_KeyDown( &kb[KB_FORWARD] );} +void IN_ForwardUp( void ) {IN_KeyUp( &kb[KB_FORWARD] );} +void IN_BackDown( void ) {IN_KeyDown( &kb[KB_BACK] );} +void IN_BackUp( void ) {IN_KeyUp( &kb[KB_BACK] );} +void IN_LookupDown( void ) {IN_KeyDown( &kb[KB_LOOKUP] );} +void IN_LookupUp( void ) {IN_KeyUp( &kb[KB_LOOKUP] );} +void IN_LookdownDown( void ) {IN_KeyDown( &kb[KB_LOOKDOWN] );} +void IN_LookdownUp( void ) {IN_KeyUp( &kb[KB_LOOKDOWN] );} +void IN_MoveleftDown( void ) {IN_KeyDown( &kb[KB_MOVELEFT] );} +void IN_MoveleftUp( void ) {IN_KeyUp( &kb[KB_MOVELEFT] );} +void IN_MoverightDown( void ) {IN_KeyDown( &kb[KB_MOVERIGHT] );} +void IN_MoverightUp( void ) {IN_KeyUp( &kb[KB_MOVERIGHT] );} + +void IN_SpeedDown( void ) {IN_KeyDown( &kb[KB_SPEED] );} +void IN_SpeedUp( void ) {IN_KeyUp( &kb[KB_SPEED] );} +void IN_StrafeDown( void ) {IN_KeyDown( &kb[KB_STRAFE] );} +void IN_StrafeUp( void ) {IN_KeyUp( &kb[KB_STRAFE] );} + +void IN_Button0Down( void ) {IN_KeyDown( &kb[KB_BUTTONS0] );} +void IN_Button0Up( void ) {IN_KeyUp( &kb[KB_BUTTONS0] );} +void IN_Button1Down( void ) {IN_KeyDown( &kb[KB_BUTTONS1] );} +void IN_Button1Up( void ) {IN_KeyUp( &kb[KB_BUTTONS1] );} +void IN_UseItemDown( void ) {IN_KeyDown( &kb[KB_BUTTONS2] );} +void IN_UseItemUp( void ) {IN_KeyUp( &kb[KB_BUTTONS2] );} +void IN_Button3Down( void ) {IN_KeyDown( &kb[KB_BUTTONS3] );} +void IN_Button3Up( void ) {IN_KeyUp( &kb[KB_BUTTONS3] );} +void IN_Button4Down( void ) {IN_KeyDown( &kb[KB_BUTTONS4] );} +void IN_Button4Up( void ) {IN_KeyUp( &kb[KB_BUTTONS4] );} +// void IN_Button5Down(void) {IN_KeyDown(&kb[KB_BUTTONS5]);} +// void IN_Button5Up(void) {IN_KeyUp(&kb[KB_BUTTONS5]);} + +// void IN_Button6Down(void) {IN_KeyDown(&kb[KB_BUTTONS6]);} +// void IN_Button6Up(void) {IN_KeyUp(&kb[KB_BUTTONS6]);} + +// Rafael activate +void IN_ActivateDown( void ) {IN_KeyDown( &kb[KB_BUTTONS6] );} +void IN_ActivateUp( void ) {IN_KeyUp( &kb[KB_BUTTONS6] );} +// done. + +// Rafael Kick +void IN_KickDown( void ) {IN_KeyDown( &kb[KB_KICK] );} +void IN_KickUp( void ) {IN_KeyUp( &kb[KB_KICK] );} +// done. + +void IN_SprintDown( void ) {IN_KeyDown( &kb[KB_BUTTONS5] );} +void IN_SprintUp( void ) {IN_KeyUp( &kb[KB_BUTTONS5] );} + + +// wbuttons (wolf buttons) +void IN_Wbutton0Down( void ) { IN_KeyDown( &kb[KB_WBUTTONS0] ); } //----(SA) secondary fire button +void IN_Wbutton0Up( void ) { IN_KeyUp( &kb[KB_WBUTTONS0] ); } +void IN_ZoomDown( void ) { IN_KeyDown( &kb[KB_WBUTTONS1] ); } //----(SA) zoom key +void IN_ZoomUp( void ) { IN_KeyUp( &kb[KB_WBUTTONS1] ); } +void IN_QuickGrenDown( void ) { IN_KeyDown( &kb[KB_WBUTTONS2] ); } //----(SA) "Quickgrenade" +void IN_QuickGrenUp( void ) { IN_KeyUp( &kb[KB_WBUTTONS2] ); } +void IN_ReloadDown( void ) { IN_KeyDown( &kb[KB_WBUTTONS3] ); } //----(SA) manual weapon re-load +void IN_ReloadUp( void ) { IN_KeyUp( &kb[KB_WBUTTONS3] ); } +void IN_LeanLeftDown( void ) { IN_KeyDown( &kb[KB_WBUTTONS4] ); } //----(SA) lean left +void IN_LeanLeftUp( void ) { IN_KeyUp( &kb[KB_WBUTTONS4] ); } +void IN_LeanRightDown( void ) { IN_KeyDown( &kb[KB_WBUTTONS5] ); } //----(SA) lean right +void IN_LeanRightUp( void ) { IN_KeyUp( &kb[KB_WBUTTONS5] ); } + +// unused +void IN_Wbutton6Down( void ) { IN_KeyDown( &kb[KB_WBUTTONS6] ); } +void IN_Wbutton6Up( void ) { IN_KeyUp( &kb[KB_WBUTTONS6] ); } +void IN_Wbutton7Down( void ) { IN_KeyDown( &kb[KB_WBUTTONS7] ); } +void IN_Wbutton7Up( void ) { IN_KeyUp( &kb[KB_WBUTTONS7] ); } + + + + +void IN_ButtonDown( void ) { + IN_KeyDown( &kb[KB_BUTTONS1] ); +} +void IN_ButtonUp( void ) { + IN_KeyUp( &kb[KB_BUTTONS1] ); +} + +void IN_CenterView( void ) { + cl.viewangles[PITCH] = -SHORT2ANGLE( cl.snap.ps.delta_angles[PITCH] ); +} + +void IN_Notebook( void ) { + if ( cls.state == CA_ACTIVE && !clc.demoplaying ) { + Cvar_Set( "cg_youGotMail", "0" ); // clear icon //----(SA) added + VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_NOTEBOOK ); // startup notebook + } +} + +void IN_Help( void ) { + if ( cls.state == CA_ACTIVE && !clc.demoplaying ) { + VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_HELP ); // startup help system + } +} + + +//========================================================================== + +cvar_t *cl_upspeed; +cvar_t *cl_forwardspeed; +cvar_t *cl_sidespeed; + +cvar_t *cl_yawspeed; +cvar_t *cl_pitchspeed; + +cvar_t *cl_run; + +cvar_t *cl_anglespeedkey; + +cvar_t *cl_recoilPitch; + +/* +================ +CL_AdjustAngles + +Moves the local angle positions +================ +*/ +void CL_AdjustAngles( void ) { + float speed; + + if ( kb[KB_SPEED].active ) { + speed = 0.001 * cls.frametime * cl_anglespeedkey->value; + } else { + speed = 0.001 * cls.frametime; + } + + if ( !kb[KB_STRAFE].active ) { + cl.viewangles[YAW] -= speed * cl_yawspeed->value * CL_KeyState( &kb[KB_RIGHT] ); + cl.viewangles[YAW] += speed * cl_yawspeed->value * CL_KeyState( &kb[KB_LEFT] ); + } + + cl.viewangles[PITCH] -= speed * cl_pitchspeed->value * CL_KeyState( &kb[KB_LOOKUP] ); + cl.viewangles[PITCH] += speed * cl_pitchspeed->value * CL_KeyState( &kb[KB_LOOKDOWN] ); +} + +/* +================ +CL_KeyMove + +Sets the usercmd_t based on key states +================ +*/ +void CL_KeyMove( usercmd_t *cmd ) { + int movespeed; + int forward, side, up; + // Rafael Kick + int kick; + // done + + // + // adjust for speed key / running + // the walking flag is to keep animations consistant + // even during acceleration and develeration + // + if ( kb[KB_SPEED].active ^ cl_run->integer ) { + movespeed = 127; + cmd->buttons &= ~BUTTON_WALKING; + } else { + cmd->buttons |= BUTTON_WALKING; + movespeed = 64; + } + + forward = 0; + side = 0; + up = 0; + if ( kb[KB_STRAFE].active ) { + side += movespeed * CL_KeyState( &kb[KB_RIGHT] ); + side -= movespeed * CL_KeyState( &kb[KB_LEFT] ); + } + + side += movespeed * CL_KeyState( &kb[KB_MOVERIGHT] ); + side -= movespeed * CL_KeyState( &kb[KB_MOVELEFT] ); + +//----(SA) added + if ( cmd->buttons & BUTTON_ACTIVATE ) { + if ( side > 0 ) { + cmd->wbuttons |= WBUTTON_LEANRIGHT; + } else if ( side < 0 ) { + cmd->wbuttons |= WBUTTON_LEANLEFT; + } + + side = 0; // disallow the strafe when holding 'activate' + } +//----(SA) end + + up += movespeed * CL_KeyState( &kb[KB_UP] ); + up -= movespeed * CL_KeyState( &kb[KB_DOWN] ); + + forward += movespeed * CL_KeyState( &kb[KB_FORWARD] ); + forward -= movespeed * CL_KeyState( &kb[KB_BACK] ); + + // Rafael Kick + kick = CL_KeyState( &kb[KB_KICK] ); + // done + + if ( !( cl.snap.ps.persistant[PERS_HWEAPON_USE] ) ) { + cmd->forwardmove = ClampChar( forward ); + cmd->rightmove = ClampChar( side ); + cmd->upmove = ClampChar( up ); + + // Rafael - Kick + cmd->wolfkick = ClampChar( kick ); + // done + + } +} + +/* +================= +CL_MouseEvent +================= +*/ +void CL_MouseEvent( int dx, int dy, int time ) { + if ( cls.keyCatchers & KEYCATCH_UI ) { + VM_Call( uivm, UI_MOUSE_EVENT, dx, dy ); + } else if ( cls.keyCatchers & KEYCATCH_CGAME ) { + VM_Call( cgvm, CG_MOUSE_EVENT, dx, dy ); + } else { + cl.mouseDx[cl.mouseIndex] += dx; + cl.mouseDy[cl.mouseIndex] += dy; + } +} + +/* +================= +CL_JoystickEvent + +Joystick values stay set until changed +================= +*/ +void CL_JoystickEvent( int axis, int value, int time ) { + if ( axis < 0 || axis >= MAX_JOYSTICK_AXIS ) { + Com_Error( ERR_DROP, "CL_JoystickEvent: bad axis %i", axis ); + } + cl.joystickAxis[axis] = value; +} + +/* +================= +CL_JoystickMove +================= +*/ +void CL_JoystickMove( usercmd_t *cmd ) { + int movespeed; + float anglespeed; + + if ( kb[KB_SPEED].active ^ cl_run->integer ) { + movespeed = 2; + } else { + movespeed = 1; + cmd->buttons |= BUTTON_WALKING; + } + + if ( kb[KB_SPEED].active ) { + anglespeed = 0.001 * cls.frametime * cl_anglespeedkey->value; + } else { + anglespeed = 0.001 * cls.frametime; + } + +#ifdef __MACOS__ + cmd->rightmove = ClampChar( cmd->rightmove + cl.joystickAxis[AXIS_SIDE] ); +#else + if ( !kb[KB_STRAFE].active ) { + cl.viewangles[YAW] += anglespeed * cl_yawspeed->value * cl.joystickAxis[AXIS_SIDE]; + } else { + cmd->rightmove = ClampChar( cmd->rightmove + cl.joystickAxis[AXIS_SIDE] ); + } +#endif + if ( kb[KB_MLOOK].active ) { + cl.viewangles[PITCH] += anglespeed * cl_pitchspeed->value * cl.joystickAxis[AXIS_FORWARD]; + } else { + cmd->forwardmove = ClampChar( cmd->forwardmove + cl.joystickAxis[AXIS_FORWARD] ); + } + + cmd->upmove = ClampChar( cmd->upmove + cl.joystickAxis[AXIS_UP] ); +} + +/* +================= +CL_MouseMove +================= +*/ +void CL_MouseMove( usercmd_t *cmd ) { + float mx, my; + float accelSensitivity; + float rate; + + // allow mouse smoothing + if ( m_filter->integer ) { + mx = ( cl.mouseDx[0] + cl.mouseDx[1] ) * 0.5; + my = ( cl.mouseDy[0] + cl.mouseDy[1] ) * 0.5; + } else { + mx = cl.mouseDx[cl.mouseIndex]; + my = cl.mouseDy[cl.mouseIndex]; + } + cl.mouseIndex ^= 1; + cl.mouseDx[cl.mouseIndex] = 0; + cl.mouseDy[cl.mouseIndex] = 0; + + rate = sqrt( mx * mx + my * my ) / (float)frame_msec; + accelSensitivity = cl_sensitivity->value + rate * cl_mouseAccel->value; + + // scale by FOV + accelSensitivity *= cl.cgameSensitivity; + +/* NERVE - SMF - this has moved to CG_CalcFov to fix zoomed-in/out transition movement bug + if ( cl.snap.ps.stats[STAT_ZOOMED_VIEW] ) { + if(cl.snap.ps.weapon == WP_SNIPERRIFLE) { + accelSensitivity *= 0.1; + } + else if(cl.snap.ps.weapon == WP_SNOOPERSCOPE) { + accelSensitivity *= 0.2; + } + } +*/ + if ( rate && cl_showMouseRate->integer ) { + Com_Printf( "%f : %f\n", rate, accelSensitivity ); + } + +// Ridah, experimenting with a slow tracking gun + + // Rafael - mg42 + if ( cl.snap.ps.persistant[PERS_HWEAPON_USE] ) { + mx *= 2.5; //(accelSensitivity * 0.1); + my *= 2; //(accelSensitivity * 0.075); + } else + { + mx *= accelSensitivity; + my *= accelSensitivity; + } + + if ( !mx && !my ) { + return; + } + + // add mouse X/Y movement to cmd + if ( kb[KB_STRAFE].active ) { + cmd->rightmove = ClampChar( cmd->rightmove + m_side->value * mx ); + } else { + cl.viewangles[YAW] -= m_yaw->value * mx; + } + + if ( ( kb[KB_MLOOK].active || cl_freelook->integer ) && !kb[KB_STRAFE].active ) { + cl.viewangles[PITCH] += m_pitch->value * my; + } else { + cmd->forwardmove = ClampChar( cmd->forwardmove - m_forward->value * my ); + } +} + + +/* +============== +CL_CmdButtons +============== +*/ +void CL_CmdButtons( usercmd_t *cmd ) { + int i; + + // + // figure button bits + // send a button bit even if the key was pressed and released in + // less than a frame + // + for ( i = 0 ; i < 7 ; i++ ) { + if ( kb[KB_BUTTONS0 + i].active || kb[KB_BUTTONS0 + i].wasPressed ) { + cmd->buttons |= 1 << i; + } + kb[KB_BUTTONS0 + i].wasPressed = qfalse; + } + + for ( i = 0 ; i < 7 ; i++ ) { + if ( kb[KB_WBUTTONS0 + i].active || kb[KB_WBUTTONS0 + i].wasPressed ) { + cmd->wbuttons |= 1 << i; + } + kb[KB_WBUTTONS0 + i].wasPressed = qfalse; + } + + if ( cls.keyCatchers ) { + cmd->buttons |= BUTTON_TALK; + } + + // allow the game to know if any key at all is + // currently pressed, even if it isn't bound to anything + if ( anykeydown && !cls.keyCatchers ) { + cmd->buttons |= BUTTON_ANY; + } +} + + +/* +============== +CL_FinishMove +============== +*/ +void CL_FinishMove( usercmd_t *cmd ) { + int i; + + // copy the state that the cgame is currently sending + cmd->weapon = cl.cgameUserCmdValue; + + cmd->holdable = cl.cgameUserHoldableValue; //----(SA) modified + + // send the current server time so the amount of movement + // can be determined without allowing cheating + cmd->serverTime = cl.serverTime; + + for ( i = 0 ; i < 3 ; i++ ) { + cmd->angles[i] = ANGLE2SHORT( cl.viewangles[i] ); + } +} + + +/* +================= +CL_CreateCmd +================= +*/ +usercmd_t CL_CreateCmd( void ) { + usercmd_t cmd; + vec3_t oldAngles; + float recoilAdd; + + VectorCopy( cl.viewangles, oldAngles ); + + // keyboard angle adjustment + CL_AdjustAngles(); + + memset( &cmd, 0, sizeof( cmd ) ); + + CL_CmdButtons( &cmd ); + + // get basic movement from keyboard + CL_KeyMove( &cmd ); + + // get basic movement from mouse + CL_MouseMove( &cmd ); + + // get basic movement from joystick + CL_JoystickMove( &cmd ); + + // check to make sure the angles haven't wrapped + if ( cl.viewangles[PITCH] - oldAngles[PITCH] > 90 ) { + cl.viewangles[PITCH] = oldAngles[PITCH] + 90; + } else if ( oldAngles[PITCH] - cl.viewangles[PITCH] > 90 ) { + cl.viewangles[PITCH] = oldAngles[PITCH] - 90; + } + + // RF, set the kickAngles so aiming is effected + recoilAdd = cl_recoilPitch->value; + if ( fabs( cl.viewangles[PITCH] + recoilAdd ) < 40 ) { + cl.viewangles[PITCH] += recoilAdd; + } + // the recoilPitch has been used, so clear it out + cl_recoilPitch->value = 0; + + // store out the final values + CL_FinishMove( &cmd ); + + // draw debug graphs of turning for mouse testing + if ( cl_debugMove->integer ) { + if ( cl_debugMove->integer == 1 ) { + SCR_DebugGraph( abs( cl.viewangles[YAW] - oldAngles[YAW] ), 0 ); + } + if ( cl_debugMove->integer == 2 ) { + SCR_DebugGraph( abs( cl.viewangles[PITCH] - oldAngles[PITCH] ), 0 ); + } + } + + cmd.cld = cl.cgameCld; // NERVE - SMF + + return cmd; +} + + +/* +================= +CL_CreateNewCommands + +Create a new usercmd_t structure for this frame +================= +*/ +void CL_CreateNewCommands( void ) { + usercmd_t *cmd; + int cmdNum; + + // no need to create usercmds until we have a gamestate + if ( cls.state < CA_PRIMED ) { + return; + } + + frame_msec = com_frameTime - old_com_frameTime; + + // if running less than 5fps, truncate the extra time to prevent + // unexpected moves after a hitch + if ( frame_msec > 200 ) { + frame_msec = 200; + } + old_com_frameTime = com_frameTime; + + + // generate a command for this frame + cl.cmdNumber++; + cmdNum = cl.cmdNumber & CMD_MASK; + cl.cmds[cmdNum] = CL_CreateCmd(); + cmd = &cl.cmds[cmdNum]; +} + +/* +================= +CL_ReadyToSendPacket + +Returns qfalse if we are over the maxpackets limit +and should choke back the bandwidth a bit by not sending +a packet this frame. All the commands will still get +delivered in the next packet, but saving a header and +getting more delta compression will reduce total bandwidth. +================= +*/ +qboolean CL_ReadyToSendPacket( void ) { + int oldPacketNum; + int delta; + + // don't send anything if playing back a demo + if ( clc.demoplaying || cls.state == CA_CINEMATIC ) { + return qfalse; + } + + // If we are downloading, we send no less than 50ms between packets + if ( *clc.downloadTempName && + cls.realtime - clc.lastPacketSentTime < 50 ) { + return qfalse; + } + + // if we don't have a valid gamestate yet, only send + // one packet a second + if ( cls.state != CA_ACTIVE && + cls.state != CA_PRIMED && + !*clc.downloadTempName && + cls.realtime - clc.lastPacketSentTime < 1000 ) { + return qfalse; + } + + // send every frame for loopbacks + if ( clc.netchan.remoteAddress.type == NA_LOOPBACK ) { + return qtrue; + } + + // send every frame for LAN + if ( Sys_IsLANAddress( clc.netchan.remoteAddress ) ) { + return qtrue; + } + + // check for exceeding cl_maxpackets + if ( cl_maxpackets->integer < 15 ) { + Cvar_Set( "cl_maxpackets", "15" ); + } else if ( cl_maxpackets->integer > 100 ) { + Cvar_Set( "cl_maxpackets", "100" ); + } + oldPacketNum = ( clc.netchan.outgoingSequence - 1 ) & PACKET_MASK; + delta = cls.realtime - cl.outPackets[ oldPacketNum ].p_realtime; + if ( delta < 1000 / cl_maxpackets->integer ) { + // the accumulated commands will go out in the next packet + return qfalse; + } + + return qtrue; +} + +/* +=================== +CL_WritePacket + +Create and send the command packet to the server +Including both the reliable commands and the usercmds + +During normal gameplay, a client packet will contain something like: + +4 sequence number +2 qport +4 serverid +4 acknowledged sequence number +4 clc.serverCommandSequence + +1 clc_move or clc_moveNoDelta +1 command count + + +=================== +*/ +void CL_WritePacket( void ) { + msg_t buf; + byte data[MAX_MSGLEN]; + int i, j; + usercmd_t *cmd, *oldcmd; + usercmd_t nullcmd; + int packetNum; + int oldPacketNum; + int count, key; + + // don't send anything if playing back a demo + if ( clc.demoplaying || cls.state == CA_CINEMATIC ) { + return; + } + + memset( &nullcmd, 0, sizeof( nullcmd ) ); + oldcmd = &nullcmd; + + MSG_Init( &buf, data, sizeof( data ) ); + + MSG_Bitstream( &buf ); + // write the current serverId so the server + // can tell if this is from the current gameState + MSG_WriteLong( &buf, cl.serverId ); + + // write the last message we received, which can + // be used for delta compression, and is also used + // to tell if we dropped a gamestate + MSG_WriteLong( &buf, clc.serverMessageSequence ); + + // write the last reliable message we received + MSG_WriteLong( &buf, clc.serverCommandSequence ); + + // write any unacknowledged clientCommands + for ( i = clc.reliableAcknowledge + 1 ; i <= clc.reliableSequence ; i++ ) { + MSG_WriteByte( &buf, clc_clientCommand ); + MSG_WriteLong( &buf, i ); + MSG_WriteString( &buf, clc.reliableCommands[ i & ( MAX_RELIABLE_COMMANDS - 1 ) ] ); + } + + // we want to send all the usercmds that were generated in the last + // few packet, so even if a couple packets are dropped in a row, + // all the cmds will make it to the server + if ( cl_packetdup->integer < 0 ) { + Cvar_Set( "cl_packetdup", "0" ); + } else if ( cl_packetdup->integer > 5 ) { + Cvar_Set( "cl_packetdup", "5" ); + } + oldPacketNum = ( clc.netchan.outgoingSequence - 1 - cl_packetdup->integer ) & PACKET_MASK; + count = cl.cmdNumber - cl.outPackets[ oldPacketNum ].p_cmdNumber; + if ( count > MAX_PACKET_USERCMDS ) { + count = MAX_PACKET_USERCMDS; + Com_Printf( "MAX_PACKET_USERCMDS\n" ); + } + if ( count >= 1 ) { + if ( cl_showSend->integer ) { + Com_Printf( "(%i)", count ); + } + + // begin a client move command + if ( cl_nodelta->integer || !cl.snap.valid || clc.demowaiting + || clc.serverMessageSequence != cl.snap.messageNum ) { + MSG_WriteByte( &buf, clc_moveNoDelta ); + } else { + MSG_WriteByte( &buf, clc_move ); + } + + // write the command count + MSG_WriteByte( &buf, count ); + + // use the checksum feed in the key + key = clc.checksumFeed; + // also use the message acknowledge + key ^= clc.serverMessageSequence; + // also use the last acknowledged server command in the key + key ^= Com_HashKey( clc.serverCommands[ clc.serverCommandSequence & ( MAX_RELIABLE_COMMANDS - 1 ) ], 32 ); + + // write all the commands, including the predicted command + for ( i = 0 ; i < count ; i++ ) { + j = ( cl.cmdNumber - count + i + 1 ) & CMD_MASK; + cmd = &cl.cmds[j]; + MSG_WriteDeltaUsercmdKey( &buf, key, oldcmd, cmd ); + oldcmd = cmd; + } + } + + // + // deliver the message + // + packetNum = clc.netchan.outgoingSequence & PACKET_MASK; + cl.outPackets[ packetNum ].p_realtime = cls.realtime; + cl.outPackets[ packetNum ].p_serverTime = oldcmd->serverTime; + cl.outPackets[ packetNum ].p_cmdNumber = cl.cmdNumber; + clc.lastPacketSentTime = cls.realtime; + + if ( cl_showSend->integer ) { + Com_Printf( "%i ", buf.cursize ); + } +// Netchan_Transmit (&clc.netchan, buf.cursize, buf.data); + CL_Netchan_Transmit( &clc.netchan, &buf ); + + // clients never really should have messages large enough + // to fragment, but in case they do, fire them all off + // at once + while ( clc.netchan.unsentFragments ) { + CL_Netchan_TransmitNextFragment( &clc.netchan ); + } +} + +/* +================= +CL_SendCmd + +Called every frame to builds and sends a command packet to the server. +================= +*/ +void CL_SendCmd( void ) { + // don't send any message if not connected + if ( cls.state < CA_CONNECTED ) { + return; + } + + // don't send commands if paused + if ( com_sv_running->integer && sv_paused->integer && cl_paused->integer ) { + return; + } + + // we create commands even if a demo is playing, + CL_CreateNewCommands(); + + // don't send a packet if the last packet was sent too recently + if ( !CL_ReadyToSendPacket() ) { + if ( cl_showSend->integer ) { + Com_Printf( ". " ); + } + return; + } + + CL_WritePacket(); +} + +/* +============ +CL_InitInput +============ +*/ +void CL_InitInput( void ) { + Cmd_AddCommand( "centerview",IN_CenterView ); + + Cmd_AddCommand( "+moveup",IN_UpDown ); + Cmd_AddCommand( "-moveup",IN_UpUp ); + Cmd_AddCommand( "+movedown",IN_DownDown ); + Cmd_AddCommand( "-movedown",IN_DownUp ); + Cmd_AddCommand( "+left",IN_LeftDown ); + Cmd_AddCommand( "-left",IN_LeftUp ); + Cmd_AddCommand( "+right",IN_RightDown ); + Cmd_AddCommand( "-right",IN_RightUp ); + Cmd_AddCommand( "+forward",IN_ForwardDown ); + Cmd_AddCommand( "-forward",IN_ForwardUp ); + Cmd_AddCommand( "+back",IN_BackDown ); + Cmd_AddCommand( "-back",IN_BackUp ); + Cmd_AddCommand( "+lookup", IN_LookupDown ); + Cmd_AddCommand( "-lookup", IN_LookupUp ); + Cmd_AddCommand( "+lookdown", IN_LookdownDown ); + Cmd_AddCommand( "-lookdown", IN_LookdownUp ); + Cmd_AddCommand( "+strafe", IN_StrafeDown ); + Cmd_AddCommand( "-strafe", IN_StrafeUp ); + Cmd_AddCommand( "+moveleft", IN_MoveleftDown ); + Cmd_AddCommand( "-moveleft", IN_MoveleftUp ); + Cmd_AddCommand( "+moveright", IN_MoverightDown ); + Cmd_AddCommand( "-moveright", IN_MoverightUp ); + Cmd_AddCommand( "+speed", IN_SpeedDown ); + Cmd_AddCommand( "-speed", IN_SpeedUp ); + + Cmd_AddCommand( "+attack", IN_Button0Down ); // ---- id (primary firing) + Cmd_AddCommand( "-attack", IN_Button0Up ); +// Cmd_AddCommand ("+button0", IN_Button0Down); +// Cmd_AddCommand ("-button0", IN_Button0Up); + + Cmd_AddCommand( "+button1", IN_Button1Down ); + Cmd_AddCommand( "-button1", IN_Button1Up ); + + Cmd_AddCommand( "+useitem", IN_UseItemDown ); + Cmd_AddCommand( "-useitem", IN_UseItemUp ); + + Cmd_AddCommand( "+salute", IN_Button3Down ); //----(SA) salute + Cmd_AddCommand( "-salute", IN_Button3Up ); +// Cmd_AddCommand ("+button3", IN_Button3Down); +// Cmd_AddCommand ("-button3", IN_Button3Up); + + Cmd_AddCommand( "+button4", IN_Button4Down ); + Cmd_AddCommand( "-button4", IN_Button4Up ); + //Cmd_AddCommand ("+button5", IN_Button5Down); + //Cmd_AddCommand ("-button5", IN_Button5Up); + + //Cmd_AddCommand ("+button6", IN_Button6Down); + //Cmd_AddCommand ("-button6", IN_Button6Up); + + // Rafael Activate + Cmd_AddCommand( "+activate", IN_ActivateDown ); + Cmd_AddCommand( "-activate", IN_ActivateUp ); + // done. + + // Rafael Kick + Cmd_AddCommand( "+kick", IN_KickDown ); + Cmd_AddCommand( "-kick", IN_KickUp ); + // done + + Cmd_AddCommand( "+sprint", IN_SprintDown ); + Cmd_AddCommand( "-sprint", IN_SprintUp ); + + + // wolf buttons + Cmd_AddCommand( "+attack2", IN_Wbutton0Down ); //----(SA) secondary firing + Cmd_AddCommand( "-attack2", IN_Wbutton0Up ); + Cmd_AddCommand( "+zoom", IN_ZoomDown ); // + Cmd_AddCommand( "-zoom", IN_ZoomUp ); + Cmd_AddCommand( "+quickgren", IN_QuickGrenDown ); // + Cmd_AddCommand( "-quickgren", IN_QuickGrenUp ); + Cmd_AddCommand( "+reload", IN_ReloadDown ); // + Cmd_AddCommand( "-reload", IN_ReloadUp ); + Cmd_AddCommand( "+leanleft", IN_LeanLeftDown ); + Cmd_AddCommand( "-leanleft", IN_LeanLeftUp ); + Cmd_AddCommand( "+leanright", IN_LeanRightDown ); + Cmd_AddCommand( "-leanright", IN_LeanRightUp ); + Cmd_AddCommand( "+wbutton6", IN_Wbutton6Down ); // + Cmd_AddCommand( "-wbutton6", IN_Wbutton6Up ); + Cmd_AddCommand( "+wbutton7", IN_Wbutton7Down ); // + Cmd_AddCommand( "-wbutton7", IN_Wbutton7Up ); +//----(SA) end + + Cmd_AddCommand( "+mlook", IN_MLookDown ); + Cmd_AddCommand( "-mlook", IN_MLookUp ); + + Cmd_AddCommand( "notebook",IN_Notebook ); +// Cmd_AddCommand ("help",IN_Help); + + cl_nodelta = Cvar_Get( "cl_nodelta", "0", 0 ); + cl_debugMove = Cvar_Get( "cl_debugMove", "0", 0 ); +} + + +/* +============ +CL_ClearKeys +============ +*/ +void CL_ClearKeys( void ) { + memset( kb, 0, sizeof( kb ) ); +} diff --git a/src/client/cl_keys.c b/src/client/cl_keys.c new file mode 100644 index 0000000..a810612 --- /dev/null +++ b/src/client/cl_keys.c @@ -0,0 +1,1906 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +#include "client.h" + +/* + +key up events are sent even if in console mode + +*/ + +field_t historyEditLines[COMMAND_HISTORY]; + +int nextHistoryLine; // the last line in the history buffer, not masked +int historyLine; // the line being displayed from history buffer + // will be <= nextHistoryLine + +field_t g_consoleField; +field_t chatField; +qboolean chat_team; +qboolean chat_limbo; // NERVE - SMF + +int chat_playerNum; + + +qboolean key_overstrikeMode; + +qboolean anykeydown; +qkey_t keys[MAX_KEYS]; + + +typedef struct { + char *name; + int keynum; +} keyname_t; + +qboolean UI_checkKeyExec( int key ); // NERVE - SMF + +// names not in this list can either be lowercase ascii, or '0xnn' hex sequences +keyname_t keynames[] = +{ + {"TAB", K_TAB}, + {"ENTER", K_ENTER}, + {"ESCAPE", K_ESCAPE}, + {"SPACE", K_SPACE}, + {"BACKSPACE", K_BACKSPACE}, + {"UPARROW", K_UPARROW}, + {"DOWNARROW", K_DOWNARROW}, + {"LEFTARROW", K_LEFTARROW}, + {"RIGHTARROW", K_RIGHTARROW}, + + {"ALT", K_ALT}, + {"CTRL", K_CTRL}, + {"SHIFT", K_SHIFT}, + + {"CAPSLOCK", K_CAPSLOCK}, + + + {"F1", K_F1}, + {"F2", K_F2}, + {"F3", K_F3}, + {"F4", K_F4}, + {"F5", K_F5}, + {"F6", K_F6}, + {"F7", K_F7}, + {"F8", K_F8}, + {"F9", K_F9}, + {"F10", K_F10}, + {"F11", K_F11}, + {"F12", K_F12}, + + {"INS", K_INS}, + {"DEL", K_DEL}, + {"PGDN", K_PGDN}, + {"PGUP", K_PGUP}, + {"HOME", K_HOME}, + {"END", K_END}, + + {"MOUSE1", K_MOUSE1}, + {"MOUSE2", K_MOUSE2}, + {"MOUSE3", K_MOUSE3}, + {"MOUSE4", K_MOUSE4}, + {"MOUSE5", K_MOUSE5}, + + {"MWHEELUP", K_MWHEELUP }, + {"MWHEELDOWN", K_MWHEELDOWN }, + + {"JOY1", K_JOY1}, + {"JOY2", K_JOY2}, + {"JOY3", K_JOY3}, + {"JOY4", K_JOY4}, + {"JOY5", K_JOY5}, + {"JOY6", K_JOY6}, + {"JOY7", K_JOY7}, + {"JOY8", K_JOY8}, + {"JOY9", K_JOY9}, + {"JOY10", K_JOY10}, + {"JOY11", K_JOY11}, + {"JOY12", K_JOY12}, + {"JOY13", K_JOY13}, + {"JOY14", K_JOY14}, + {"JOY15", K_JOY15}, + {"JOY16", K_JOY16}, + {"JOY17", K_JOY17}, + {"JOY18", K_JOY18}, + {"JOY19", K_JOY19}, + {"JOY20", K_JOY20}, + {"JOY21", K_JOY21}, + {"JOY22", K_JOY22}, + {"JOY23", K_JOY23}, + {"JOY24", K_JOY24}, + {"JOY25", K_JOY25}, + {"JOY26", K_JOY26}, + {"JOY27", K_JOY27}, + {"JOY28", K_JOY28}, + {"JOY29", K_JOY29}, + {"JOY30", K_JOY30}, + {"JOY31", K_JOY31}, + {"JOY32", K_JOY32}, + + {"AUX1", K_AUX1}, + {"AUX2", K_AUX2}, + {"AUX3", K_AUX3}, + {"AUX4", K_AUX4}, + {"AUX5", K_AUX5}, + {"AUX6", K_AUX6}, + {"AUX7", K_AUX7}, + {"AUX8", K_AUX8}, + {"AUX9", K_AUX9}, + {"AUX10", K_AUX10}, + {"AUX11", K_AUX11}, + {"AUX12", K_AUX12}, + {"AUX13", K_AUX13}, + {"AUX14", K_AUX14}, + {"AUX15", K_AUX15}, + {"AUX16", K_AUX16}, + + {"KP_HOME", K_KP_HOME }, + {"KP_UPARROW", K_KP_UPARROW }, + {"KP_PGUP", K_KP_PGUP }, + {"KP_LEFTARROW", K_KP_LEFTARROW }, + {"KP_5", K_KP_5 }, + {"KP_RIGHTARROW", K_KP_RIGHTARROW }, + {"KP_END", K_KP_END }, + {"KP_DOWNARROW", K_KP_DOWNARROW }, + {"KP_PGDN", K_KP_PGDN }, + {"KP_ENTER", K_KP_ENTER }, + {"KP_INS", K_KP_INS }, + {"KP_DEL", K_KP_DEL }, + {"KP_SLASH", K_KP_SLASH }, + {"KP_MINUS", K_KP_MINUS }, + {"KP_PLUS", K_KP_PLUS }, + {"KP_NUMLOCK", K_KP_NUMLOCK }, + {"KP_STAR", K_KP_STAR }, + {"KP_EQUALS", K_KP_EQUALS }, + + {"PAUSE", K_PAUSE}, + + {"SEMICOLON", ';'}, // because a raw semicolon seperates commands + + {"COMMAND", K_COMMAND}, //mac + + {NULL,0} +}; + +keyname_t keynames_d[] = //deutsch +{ + {"TAB", K_TAB}, + {"EINGABETASTE", K_ENTER}, + {"ESC", K_ESCAPE}, + {"LEERTASTE", K_SPACE}, + {"RÜCKTASTE", K_BACKSPACE}, + {"PFEILT.AUF", K_UPARROW}, + {"PFEILT.UNTEN", K_DOWNARROW}, + {"PFEILT.LINKS", K_LEFTARROW}, + {"PFEILT.RECHTS", K_RIGHTARROW}, + + {"ALT", K_ALT}, + {"STRG", K_CTRL}, + {"UMSCHALT", K_SHIFT}, // (SA) removed one 'L' for laird 11/15/01 + + {"FESTSTELLT", K_CAPSLOCK}, + + {"F1", K_F1}, + {"F2", K_F2}, + {"F3", K_F3}, + {"F4", K_F4}, + {"F5", K_F5}, + {"F6", K_F6}, + {"F7", K_F7}, + {"F8", K_F8}, + {"F9", K_F9}, + {"F10", K_F10}, + {"F11", K_F11}, + {"F12", K_F12}, + + {"EINFG", K_INS}, + {"ENTF", K_DEL}, + {"BILD-AB", K_PGDN}, + {"BILD-AUF", K_PGUP}, + {"POS1", K_HOME}, + {"ENDE", K_END}, + + {"MAUS1", K_MOUSE1}, + {"MAUS2", K_MOUSE2}, + {"MAUS3", K_MOUSE3}, + {"MAUS4", K_MOUSE4}, + {"MAUS5", K_MOUSE5}, + + {"MRADOBEN", K_MWHEELUP }, + {"MRADUNTEN", K_MWHEELDOWN }, + + {"JOY1", K_JOY1}, + {"JOY2", K_JOY2}, + {"JOY3", K_JOY3}, + {"JOY4", K_JOY4}, + {"JOY5", K_JOY5}, + {"JOY6", K_JOY6}, + {"JOY7", K_JOY7}, + {"JOY8", K_JOY8}, + {"JOY9", K_JOY9}, + {"JOY10", K_JOY10}, + {"JOY11", K_JOY11}, + {"JOY12", K_JOY12}, + {"JOY13", K_JOY13}, + {"JOY14", K_JOY14}, + {"JOY15", K_JOY15}, + {"JOY16", K_JOY16}, + {"JOY17", K_JOY17}, + {"JOY18", K_JOY18}, + {"JOY19", K_JOY19}, + {"JOY20", K_JOY20}, + {"JOY21", K_JOY21}, + {"JOY22", K_JOY22}, + {"JOY23", K_JOY23}, + {"JOY24", K_JOY24}, + {"JOY25", K_JOY25}, + {"JOY26", K_JOY26}, + {"JOY27", K_JOY27}, + {"JOY28", K_JOY28}, + {"JOY29", K_JOY29}, + {"JOY30", K_JOY30}, + {"JOY31", K_JOY31}, + {"JOY32", K_JOY32}, + + {"AUX1", K_AUX1}, + {"AUX2", K_AUX2}, + {"AUX3", K_AUX3}, + {"AUX4", K_AUX4}, + {"AUX5", K_AUX5}, + {"AUX6", K_AUX6}, + {"AUX7", K_AUX7}, + {"AUX8", K_AUX8}, + {"AUX9", K_AUX9}, + {"AUX10", K_AUX10}, + {"AUX11", K_AUX11}, + {"AUX12", K_AUX12}, + {"AUX13", K_AUX13}, + {"AUX14", K_AUX14}, + {"AUX15", K_AUX15}, + {"AUX16", K_AUX16}, + + {"ZB_POS1", K_KP_HOME }, + {"ZB_PFEILT.AUF", K_KP_UPARROW }, + {"ZB_BILD-AUF", K_KP_PGUP }, + {"ZB_PFEILT.LINKS", K_KP_LEFTARROW }, + {"ZB_5", K_KP_5 }, + {"ZB_PFEILT.RECHTS",K_KP_RIGHTARROW }, + {"ZB_ENDE", K_KP_END }, + {"ZB_PFEILT.UNTEN", K_KP_DOWNARROW }, + {"ZB_BILD-AB", K_KP_PGDN }, + {"ZB_ENTER", K_KP_ENTER }, + {"ZB_EINFG", K_KP_INS }, + {"ZB_ENTF", K_KP_DEL }, + {"ZB_SLASH", K_KP_SLASH }, + {"ZB_MINUS", K_KP_MINUS }, + {"ZB_PLUS", K_KP_PLUS }, + {"ZB_NUM", K_KP_NUMLOCK }, + {"ZB_*", K_KP_STAR }, + {"ZB_EQUALS", K_KP_EQUALS }, + + {"PAUSE", K_PAUSE}, + + {"COMMAND", K_COMMAND}, //mac + {NULL,0} +}; //end german + +keyname_t keynames_f[] = //french +{ + {"TAB", K_TAB}, + {"ENTREE", K_ENTER}, + {"ECHAP", K_ESCAPE}, + {"ESPACE", K_SPACE}, + {"RETOUR", K_BACKSPACE}, + {"HAUT", K_UPARROW}, + {"BAS", K_DOWNARROW}, + {"GAUCHE", K_LEFTARROW}, + {"DROITE", K_RIGHTARROW}, + + {"ALT", K_ALT}, + {"CTRL", K_CTRL}, + {"MAJ", K_SHIFT}, + + {"VERRMAJ", K_CAPSLOCK}, + + {"F1", K_F1}, + {"F2", K_F2}, + {"F3", K_F3}, + {"F4", K_F4}, + {"F5", K_F5}, + {"F6", K_F6}, + {"F7", K_F7}, + {"F8", K_F8}, + {"F9", K_F9}, + {"F10", K_F10}, + {"F11", K_F11}, + {"F12", K_F12}, + + {"INSER", K_INS}, + {"SUPPR", K_DEL}, + {"PGBAS", K_PGDN}, + {"PGHAUT", K_PGUP}, + {"ORIGINE", K_HOME}, + {"FIN", K_END}, + + {"SOURIS1", K_MOUSE1}, + {"SOURIS2", K_MOUSE2}, + {"SOURIS3", K_MOUSE3}, + {"SOURIS4", K_MOUSE4}, + {"SOURIS5", K_MOUSE5}, + + {"MOLETTEHT.", K_MWHEELUP }, + {"MOLETTEBAS", K_MWHEELDOWN }, + + {"JOY1", K_JOY1}, + {"JOY2", K_JOY2}, + {"JOY3", K_JOY3}, + {"JOY4", K_JOY4}, + {"JOY5", K_JOY5}, + {"JOY6", K_JOY6}, + {"JOY7", K_JOY7}, + {"JOY8", K_JOY8}, + {"JOY9", K_JOY9}, + {"JOY10", K_JOY10}, + {"JOY11", K_JOY11}, + {"JOY12", K_JOY12}, + {"JOY13", K_JOY13}, + {"JOY14", K_JOY14}, + {"JOY15", K_JOY15}, + {"JOY16", K_JOY16}, + {"JOY17", K_JOY17}, + {"JOY18", K_JOY18}, + {"JOY19", K_JOY19}, + {"JOY20", K_JOY20}, + {"JOY21", K_JOY21}, + {"JOY22", K_JOY22}, + {"JOY23", K_JOY23}, + {"JOY24", K_JOY24}, + {"JOY25", K_JOY25}, + {"JOY26", K_JOY26}, + {"JOY27", K_JOY27}, + {"JOY28", K_JOY28}, + {"JOY29", K_JOY29}, + {"JOY30", K_JOY30}, + {"JOY31", K_JOY31}, + {"JOY32", K_JOY32}, + + {"AUX1", K_AUX1}, + {"AUX2", K_AUX2}, + {"AUX3", K_AUX3}, + {"AUX4", K_AUX4}, + {"AUX5", K_AUX5}, + {"AUX6", K_AUX6}, + {"AUX7", K_AUX7}, + {"AUX8", K_AUX8}, + {"AUX9", K_AUX9}, + {"AUX10", K_AUX10}, + {"AUX11", K_AUX11}, + {"AUX12", K_AUX12}, + {"AUX13", K_AUX13}, + {"AUX14", K_AUX14}, + {"AUX15", K_AUX15}, + {"AUX16", K_AUX16}, + + {"PN_ORIGINE", K_KP_HOME }, + {"PN_HAUT", K_KP_UPARROW }, + {"PN_PGBAS", K_KP_PGUP }, + {"PN_GAUCHE", K_KP_LEFTARROW }, + {"PN_5", K_KP_5 }, + {"PN_DROITE", K_KP_RIGHTARROW }, + {"PN_FIN", K_KP_END }, + {"PN_BAS", K_KP_DOWNARROW }, + {"PN_PGBAS", K_KP_PGDN }, + {"PN_ENTR", K_KP_ENTER }, + {"PN_INSER", K_KP_INS }, + {"PN_SUPPR", K_KP_DEL }, + {"PN_SLASH", K_KP_SLASH }, + {"PN_MOINS", K_KP_MINUS }, + {"PN_PLUS", K_KP_PLUS }, + {"PN_VERRNUM", K_KP_NUMLOCK }, + {"PN_*", K_KP_STAR }, + {"PN_EQUALS", K_KP_EQUALS }, + + {"PAUSE", K_PAUSE}, + + {"COMMAND", K_COMMAND}, //mac + + {NULL,0} +}; //end french + +keyname_t keynames_s[] = //Spanish - Updated 11/5 +{ + {"TABULADOR", K_TAB}, + {"INTRO", K_ENTER}, + {"ESC", K_ESCAPE}, + {"BARRA_ESPACIAD", K_SPACE}, + {"RETROCESO", K_BACKSPACE}, + {"CURSOR_ARRIBA", K_UPARROW}, + {"CURSOR_ABAJO", K_DOWNARROW}, + {"CURSOR_IZQDA", K_LEFTARROW}, + {"CURSOR_DERECHA", K_RIGHTARROW}, + + {"ALT", K_ALT}, + {"CTRL", K_CTRL}, + {"MAYÚS", K_SHIFT}, + + {"BLOQ_MAYÚS", K_CAPSLOCK}, + + {"F1", K_F1}, + {"F2", K_F2}, + {"F3", K_F3}, + {"F4", K_F4}, + {"F5", K_F5}, + {"F6", K_F6}, + {"F7", K_F7}, + {"F8", K_F8}, + {"F9", K_F9}, + {"F10", K_F10}, + {"F11", K_F11}, + {"F12", K_F12}, + + {"INSERT", K_INS}, + {"SUPR", K_DEL}, + {"AV_PÁG", K_PGDN}, + {"RE_PÁG", K_PGUP}, + {"INICIO", K_HOME}, + {"FIN", K_END}, + + {"RATÓN1", K_MOUSE1}, + {"RATÓN2", K_MOUSE2}, + {"RATÓN3", K_MOUSE3}, + {"RATÓN4", K_MOUSE4}, + {"RATÓN5", K_MOUSE5}, + + {"RUEDA_HACIA_ARRIBA", K_MWHEELUP }, + {"RUEDA_HACIA_ABAJO", K_MWHEELDOWN }, + + {"JOY1", K_JOY1}, + {"JOY2", K_JOY2}, + {"JOY3", K_JOY3}, + {"JOY4", K_JOY4}, + {"JOY5", K_JOY5}, + {"JOY6", K_JOY6}, + {"JOY7", K_JOY7}, + {"JOY8", K_JOY8}, + {"JOY9", K_JOY9}, + {"JOY10", K_JOY10}, + {"JOY11", K_JOY11}, + {"JOY12", K_JOY12}, + {"JOY13", K_JOY13}, + {"JOY14", K_JOY14}, + {"JOY15", K_JOY15}, + {"JOY16", K_JOY16}, + {"JOY17", K_JOY17}, + {"JOY18", K_JOY18}, + {"JOY19", K_JOY19}, + {"JOY20", K_JOY20}, + {"JOY21", K_JOY21}, + {"JOY22", K_JOY22}, + {"JOY23", K_JOY23}, + {"JOY24", K_JOY24}, + {"JOY25", K_JOY25}, + {"JOY26", K_JOY26}, + {"JOY27", K_JOY27}, + {"JOY28", K_JOY28}, + {"JOY29", K_JOY29}, + {"JOY30", K_JOY30}, + {"JOY31", K_JOY31}, + {"JOY32", K_JOY32}, + + {"AUX1", K_AUX1}, + {"AUX2", K_AUX2}, + {"AUX3", K_AUX3}, + {"AUX4", K_AUX4}, + {"AUX5", K_AUX5}, + {"AUX6", K_AUX6}, + {"AUX7", K_AUX7}, + {"AUX8", K_AUX8}, + {"AUX9", K_AUX9}, + {"AUX10", K_AUX10}, + {"AUX11", K_AUX11}, + {"AUX12", K_AUX12}, + {"AUX13", K_AUX13}, + {"AUX14", K_AUX14}, + {"AUX15", K_AUX15}, + {"AUX16", K_AUX16}, + + {"INICIO(NUM)", K_KP_HOME }, + {"ARRIBA(NUM)", K_KP_UPARROW }, + {"RE_PÁG(NUM)", K_KP_PGUP }, + {"IZQUIERDA(NUM)", K_KP_LEFTARROW }, + {"5(NUM)", K_KP_5 }, + {"DERECHA(NUM)", K_KP_RIGHTARROW }, + {"FIN(NUM)", K_KP_END }, + {"ABAJO(NUM)", K_KP_DOWNARROW }, + {"AV_PÁG(NUM)", K_KP_PGDN }, + {"INTRO(NUM)", K_KP_ENTER }, + {"INS(NUM)", K_KP_INS }, + {"SUPR(NUM)", K_KP_DEL }, + {"/(NUM)", K_KP_SLASH }, + {"-(NUM)", K_KP_MINUS }, + {"+(NUM)", K_KP_PLUS }, + {"BLOQ_NUM", K_KP_NUMLOCK }, + {"*(NUM)", K_KP_STAR }, + {"INTRO(NUM)", K_KP_EQUALS }, + + {"PAUSA", K_PAUSE}, + + {"PUNTO_Y_COMA", ';'}, // because a raw semicolon seperates commands + + {"COMANDO", K_COMMAND}, //mac + + {NULL,0} +}; + + +keyname_t keynames_i[] = //Italian +{ + {"TAB", K_TAB}, + {"INVIO", K_ENTER}, + {"ESC", K_ESCAPE}, + {"SPAZIO", K_SPACE}, + {"BACKSPACE", K_BACKSPACE}, + {"FRECCIASU", K_UPARROW}, + {"FRECCIAGIÙ", K_DOWNARROW}, + {"FRECCIASX", K_LEFTARROW}, + {"FRECCIADX", K_RIGHTARROW}, + + {"ALT", K_ALT}, + {"CTRL", K_CTRL}, + {"MAIUSC", K_SHIFT}, + + {"BLOCMAIUSC", K_CAPSLOCK}, + + {"F1", K_F1}, + {"F2", K_F2}, + {"F3", K_F3}, + {"F4", K_F4}, + {"F5", K_F5}, + {"F6", K_F6}, + {"F7", K_F7}, + {"F8", K_F8}, + {"F9", K_F9}, + {"F10", K_F10}, + {"F11", K_F11}, + {"F12", K_F12}, + + {"INS", K_INS}, + {"CANC", K_DEL}, + {"PAGGIÙ", K_PGDN}, + {"PAGGSU", K_PGUP}, + {"HOME", K_HOME}, + {"FINE", K_END}, + + {"MOUSE1", K_MOUSE1}, + {"MOUSE2", K_MOUSE2}, + {"MOUSE3", K_MOUSE3}, + {"MOUSE4", K_MOUSE4}, + {"MOUSE5", K_MOUSE5}, + + {"ROTELLASU", K_MWHEELUP }, + {"ROTELLAGIÙ", K_MWHEELDOWN }, + + {"JOY1", K_JOY1}, + {"JOY2", K_JOY2}, + {"JOY3", K_JOY3}, + {"JOY4", K_JOY4}, + {"JOY5", K_JOY5}, + {"JOY6", K_JOY6}, + {"JOY7", K_JOY7}, + {"JOY8", K_JOY8}, + {"JOY9", K_JOY9}, + {"JOY10", K_JOY10}, + {"JOY11", K_JOY11}, + {"JOY12", K_JOY12}, + {"JOY13", K_JOY13}, + {"JOY14", K_JOY14}, + {"JOY15", K_JOY15}, + {"JOY16", K_JOY16}, + {"JOY17", K_JOY17}, + {"JOY18", K_JOY18}, + {"JOY19", K_JOY19}, + {"JOY20", K_JOY20}, + {"JOY21", K_JOY21}, + {"JOY22", K_JOY22}, + {"JOY23", K_JOY23}, + {"JOY24", K_JOY24}, + {"JOY25", K_JOY25}, + {"JOY26", K_JOY26}, + {"JOY27", K_JOY27}, + {"JOY28", K_JOY28}, + {"JOY29", K_JOY29}, + {"JOY30", K_JOY30}, + {"JOY31", K_JOY31}, + {"JOY32", K_JOY32}, + + {"AUX1", K_AUX1}, + {"AUX2", K_AUX2}, + {"AUX3", K_AUX3}, + {"AUX4", K_AUX4}, + {"AUX5", K_AUX5}, + {"AUX6", K_AUX6}, + {"AUX7", K_AUX7}, + {"AUX8", K_AUX8}, + {"AUX9", K_AUX9}, + {"AUX10", K_AUX10}, + {"AUX11", K_AUX11}, + {"AUX12", K_AUX12}, + {"AUX13", K_AUX13}, + {"AUX14", K_AUX14}, + {"AUX15", K_AUX15}, + {"AUX16", K_AUX16}, + + {"TN_HOME", K_KP_HOME }, + {"TN_FRECCIASU", K_KP_UPARROW }, + {"TN_PAGGSU", K_KP_PGUP }, + {"TN_FRECCIASX", K_KP_LEFTARROW }, + {"TN_5", K_KP_5 }, + {"TN_FRECCIA_DX", K_KP_RIGHTARROW }, + {"TN_FINE", K_KP_END }, + {"TN_FRECCIAGIÙ", K_KP_DOWNARROW }, + {"TN_PAGGIÙ", K_KP_PGDN }, + {"TN_INVIO", K_KP_ENTER }, + {"TN_INS", K_KP_INS }, + {"TN_CANC", K_KP_DEL }, + {"TN_/", K_KP_SLASH }, + {"TN_-", K_KP_MINUS }, + {"TN_+", K_KP_PLUS }, + {"TN_BLOCNUM", K_KP_NUMLOCK }, + {"TN_*", K_KP_STAR }, + {"TN_=", K_KP_EQUALS }, + + {"PAUSA", K_PAUSE}, + + {"ò", ';'}, // because a raw semicolon seperates commands + + {"COMMAND", K_COMMAND}, //mac + + {NULL,0} +}; + +/* +============================================================================= + +EDIT FIELDS + +============================================================================= +*/ + + +/* +=================== +Field_Draw + +Handles horizontal scrolling and cursor blinking +x, y, amd width are in pixels +=================== +*/ +void Field_VariableSizeDraw( field_t *edit, int x, int y, int width, int size, qboolean showCursor ) { + int len; + int drawLen; + int prestep; + int cursorChar; + char str[MAX_STRING_CHARS]; + int i; + + drawLen = edit->widthInChars; + len = strlen( edit->buffer ) + 1; + + // guarantee that cursor will be visible + if ( len <= drawLen ) { + prestep = 0; + } else { + if ( edit->scroll + drawLen > len ) { + edit->scroll = len - drawLen; + if ( edit->scroll < 0 ) { + edit->scroll = 0; + } + } + prestep = edit->scroll; + +/* + if ( edit->cursor < len - drawLen ) { + prestep = edit->cursor; // cursor at start + } else { + prestep = len - drawLen; + } +*/ + } + + if ( prestep + drawLen > len ) { + drawLen = len - prestep; + } + + // extract characters from the field at + if ( drawLen >= MAX_STRING_CHARS ) { + Com_Error( ERR_DROP, "drawLen >= MAX_STRING_CHARS" ); + } + + memcpy( str, edit->buffer + prestep, drawLen ); + str[ drawLen ] = 0; + + // draw it + if ( size == SMALLCHAR_WIDTH ) { + float color[4]; + + color[0] = color[1] = color[2] = color[3] = 1.0; + SCR_DrawSmallStringExt( x, y, str, color, qfalse ); + } else { + // draw big string with drop shadow + SCR_DrawBigString( x, y, str, 1.0 ); + } + + // draw the cursor + if ( !showCursor ) { + return; + } + + if ( (int)( cls.realtime >> 8 ) & 1 ) { + return; // off blink + } + + if ( key_overstrikeMode ) { + cursorChar = 11; + } else { + cursorChar = 10; + } + + i = drawLen - ( Q_PrintStrlen( str ) + 1 ); + + if ( size == SMALLCHAR_WIDTH ) { + SCR_DrawSmallChar( x + ( edit->cursor - prestep - i ) * size, y, cursorChar ); + } else { + str[0] = cursorChar; + str[1] = 0; + SCR_DrawBigString( x + ( edit->cursor - prestep - i ) * size, y, str, 1.0 ); + + } +} + +void Field_Draw( field_t *edit, int x, int y, int width, qboolean showCursor ) { + Field_VariableSizeDraw( edit, x, y, width, SMALLCHAR_WIDTH, showCursor ); +} + +void Field_BigDraw( field_t *edit, int x, int y, int width, qboolean showCursor ) { + Field_VariableSizeDraw( edit, x, y, width, BIGCHAR_WIDTH, showCursor ); +} + +/* +================ +Field_Paste +================ +*/ +void Field_Paste( field_t *edit ) { + char *cbd; + int pasteLen, i; + + cbd = Sys_GetClipboardData(); + + if ( !cbd ) { + return; + } + + // send as if typed, so insert / overstrike works properly + pasteLen = strlen( cbd ); + for ( i = 0 ; i < pasteLen ; i++ ) { + Field_CharEvent( edit, cbd[i] ); + } + + free( cbd ); +} + +/* +================= +Field_KeyDownEvent + +Performs the basic line editing functions for the console, +in-game talk, and menu fields + +Key events are used for non-printable characters, others are gotten from char events. +================= +*/ +void Field_KeyDownEvent( field_t *edit, int key ) { + int len; + + // shift-insert is paste + if ( ( ( key == K_INS ) || ( key == K_KP_INS ) ) && keys[K_SHIFT].down ) { + Field_Paste( edit ); + return; + } + + len = strlen( edit->buffer ); + + if ( key == K_DEL ) { + if ( edit->cursor < len ) { + memmove( edit->buffer + edit->cursor, + edit->buffer + edit->cursor + 1, len - edit->cursor ); + } + return; + } + + if ( key == K_RIGHTARROW ) { + if ( edit->cursor < len ) { + edit->cursor++; + } + + if ( edit->cursor >= edit->scroll + edit->widthInChars && edit->cursor <= len ) { + edit->scroll++; + } + return; + } + + if ( key == K_LEFTARROW ) { + if ( edit->cursor > 0 ) { + edit->cursor--; + } + if ( edit->cursor < edit->scroll ) { + edit->scroll--; + } + return; + } + + if ( key == K_HOME || ( tolower( key ) == 'a' && keys[K_CTRL].down ) ) { + edit->cursor = 0; + return; + } + + if ( key == K_END || ( tolower( key ) == 'e' && keys[K_CTRL].down ) ) { + edit->cursor = len; + return; + } + + if ( key == K_INS ) { + key_overstrikeMode = !key_overstrikeMode; + return; + } +} + +/* +================== +Field_CharEvent +================== +*/ +void Field_CharEvent( field_t *edit, int ch ) { + int len; + + if ( ch == 'v' - 'a' + 1 ) { // ctrl-v is paste + Field_Paste( edit ); + return; + } + + if ( ch == 'c' - 'a' + 1 ) { // ctrl-c clears the field + Field_Clear( edit ); + return; + } + + len = strlen( edit->buffer ); + + if ( ch == 'h' - 'a' + 1 ) { // ctrl-h is backspace + if ( edit->cursor > 0 ) { + memmove( edit->buffer + edit->cursor - 1, + edit->buffer + edit->cursor, len + 1 - edit->cursor ); + edit->cursor--; + if ( edit->cursor < edit->scroll ) { + edit->scroll--; + } + } + return; + } + + if ( ch == 'a' - 'a' + 1 ) { // ctrl-a is home + edit->cursor = 0; + edit->scroll = 0; + return; + } + + if ( ch == 'e' - 'a' + 1 ) { // ctrl-e is end + edit->cursor = len; + edit->scroll = edit->cursor - edit->widthInChars; + return; + } + + // + // ignore any other non printable chars + // + if ( ch < 32 ) { + return; + } + + if ( key_overstrikeMode ) { + if ( edit->cursor == MAX_EDIT_LINE - 1 ) { + return; + } + edit->buffer[edit->cursor] = ch; + edit->cursor++; + } else { // insert mode + if ( len == MAX_EDIT_LINE - 1 ) { + return; // all full + } + memmove( edit->buffer + edit->cursor + 1, + edit->buffer + edit->cursor, len + 1 - edit->cursor ); + edit->buffer[edit->cursor] = ch; + edit->cursor++; + } + + + if ( edit->cursor >= edit->widthInChars ) { + edit->scroll++; + } + + if ( edit->cursor == len + 1 ) { + edit->buffer[edit->cursor] = 0; + } +} + +/* +============================================================================= + +CONSOLE LINE EDITING + +============================================================================== +*/ + +static const char *completionString; +static char shortestMatch[MAX_TOKEN_CHARS]; +static int matchCount; + +/* +=============== +FindMatches + +=============== +*/ +static void FindMatches( const char *s ) { + int i; + + if ( Q_stricmpn( s, completionString, strlen( completionString ) ) ) { + return; + } + matchCount++; + if ( matchCount == 1 ) { + Q_strncpyz( shortestMatch, s, sizeof( shortestMatch ) ); + return; + } + + // cut shortestMatch to the amount common with s + for ( i = 0 ; s[i] ; i++ ) { + if ( tolower( shortestMatch[i] ) != tolower( s[i] ) ) { + shortestMatch[i] = 0; + } + } +} + +/* +=============== +PrintMatches + +=============== +*/ +static void PrintMatches( const char *s ) { + if ( !Q_stricmpn( s, shortestMatch, strlen( shortestMatch ) ) ) { + Com_Printf( " %s\n", s ); + } +} + +static void keyConcatArgs( void ) { + int i; + char *arg; + + for ( i = 1 ; i < Cmd_Argc() ; i++ ) { + Q_strcat( g_consoleField.buffer, sizeof( g_consoleField.buffer ), " " ); + arg = Cmd_Argv( i ); + while ( *arg ) { + if ( *arg == ' ' ) { + Q_strcat( g_consoleField.buffer, sizeof( g_consoleField.buffer ), "\"" ); + break; + } + arg++; + } + Q_strcat( g_consoleField.buffer, sizeof( g_consoleField.buffer ), Cmd_Argv( i ) ); + if ( *arg == ' ' ) { + Q_strcat( g_consoleField.buffer, sizeof( g_consoleField.buffer ), "\"" ); + } + } +} + +static void ConcatRemaining( const char *src, const char *start ) { + char *str; + + str = strstr( src, start ); + if ( !str ) { + keyConcatArgs(); + return; + } + + str += strlen( start ); + Q_strcat( g_consoleField.buffer, sizeof( g_consoleField.buffer ), str ); +} + + +/* +=============== +CompleteCommand + +Tab expansion +=============== +*/ +static void CompleteCommand( void ) { + field_t *edit; + field_t temp; + + edit = &g_consoleField; + + // only look at the first token for completion purposes + Cmd_TokenizeString( edit->buffer ); + + completionString = Cmd_Argv( 0 ); + if ( completionString[0] == '\\' || completionString[0] == '/' ) { + completionString++; + } + matchCount = 0; + shortestMatch[0] = 0; + + if ( strlen( completionString ) == 0 ) { + return; + } + + Cmd_CommandCompletion( FindMatches ); + Cvar_CommandCompletion( FindMatches ); + + if ( matchCount == 0 ) { + return; // no matches + } + + Com_Memcpy( &temp, edit, sizeof( field_t ) ); + + if ( matchCount == 1 ) { + Com_sprintf( edit->buffer, sizeof( edit->buffer ), "\\%s", shortestMatch ); + if ( Cmd_Argc() == 1 ) { + Q_strcat( g_consoleField.buffer, sizeof( g_consoleField.buffer ), " " ); + } else { + ConcatRemaining( temp.buffer, completionString ); + } + edit->cursor = strlen( edit->buffer ); + return; + } + + // multiple matches, complete to shortest + Com_sprintf( edit->buffer, sizeof( edit->buffer ), "\\%s", shortestMatch ); + edit->cursor = strlen( edit->buffer ); + ConcatRemaining( temp.buffer, completionString ); + + Com_Printf( "]%s\n", edit->buffer ); + + // run through again, printing matches + Cmd_CommandCompletion( PrintMatches ); + Cvar_CommandCompletion( PrintMatches ); +} + + +/* +==================== +Console_Key + +Handles history and console scrollback +==================== +*/ +void Console_Key( int key ) { + // ctrl-L clears screen + if ( key == 'l' && keys[K_CTRL].down ) { + Cbuf_AddText( "clear\n" ); + return; + } + + // enter finishes the line + if ( key == K_ENTER || key == K_KP_ENTER ) { + // if not in the game explicitly prepent a slash if needed + if ( cls.state != CA_ACTIVE && g_consoleField.buffer[0] != '\\' + && g_consoleField.buffer[0] != '/' ) { + char temp[MAX_STRING_CHARS]; + + Q_strncpyz( temp, g_consoleField.buffer, sizeof( temp ) ); + Com_sprintf( g_consoleField.buffer, sizeof( g_consoleField.buffer ), "\\%s", temp ); + g_consoleField.cursor++; + } + + Com_Printf( "]%s\n", g_consoleField.buffer ); + + // leading slash is an explicit command + if ( g_consoleField.buffer[0] == '\\' || g_consoleField.buffer[0] == '/' ) { + Cbuf_AddText( g_consoleField.buffer + 1 ); // valid command + Cbuf_AddText( "\n" ); + } else { + // other text will be chat messages + if ( !g_consoleField.buffer[0] ) { + return; // empty lines just scroll the console without adding to history + } else { + Cbuf_AddText( "cmd say " ); + Cbuf_AddText( g_consoleField.buffer ); + Cbuf_AddText( "\n" ); + } + } + + // copy line to history buffer + historyEditLines[nextHistoryLine % COMMAND_HISTORY] = g_consoleField; + nextHistoryLine++; + historyLine = nextHistoryLine; + + Field_Clear( &g_consoleField ); + + g_consoleField.widthInChars = g_console_field_width; + + if ( cls.state == CA_DISCONNECTED ) { + SCR_UpdateScreen(); // force an update, because the command + } // may take some time + return; + } + + // command completion + + if ( key == K_TAB ) { + CompleteCommand(); + return; + } + + // command history (ctrl-p ctrl-n for unix style) + + //----(SA) added some mousewheel functionality to the console + if ( ( key == K_MWHEELUP && keys[K_SHIFT].down ) || ( key == K_UPARROW ) || ( key == K_KP_UPARROW ) || + ( ( tolower( key ) == 'p' ) && keys[K_CTRL].down ) ) { + if ( nextHistoryLine - historyLine < COMMAND_HISTORY + && historyLine > 0 ) { + historyLine--; + } + g_consoleField = historyEditLines[ historyLine % COMMAND_HISTORY ]; + return; + } + + //----(SA) added some mousewheel functionality to the console + if ( ( key == K_MWHEELDOWN && keys[K_SHIFT].down ) || ( key == K_DOWNARROW ) || ( key == K_KP_DOWNARROW ) || + ( ( tolower( key ) == 'n' ) && keys[K_CTRL].down ) ) { + if ( historyLine == nextHistoryLine ) { + return; + } + historyLine++; + g_consoleField = historyEditLines[ historyLine % COMMAND_HISTORY ]; + return; + } + + // console scrolling + if ( key == K_PGUP ) { + Con_PageUp(); + return; + } + + if ( key == K_PGDN ) { + Con_PageDown(); + return; + } + + if ( key == K_MWHEELUP ) { //----(SA) added some mousewheel functionality to the console + Con_PageUp(); + if ( keys[K_CTRL].down ) { // hold to accelerate scrolling + Con_PageUp(); + Con_PageUp(); + } + return; + } + + if ( key == K_MWHEELDOWN ) { //----(SA) added some mousewheel functionality to the console + Con_PageDown(); + if ( keys[K_CTRL].down ) { // hold to accelerate scrolling + Con_PageDown(); + Con_PageDown(); + } + return; + } + + // ctrl-home = top of console + if ( key == K_HOME && keys[K_CTRL].down ) { + Con_Top(); + return; + } + + // ctrl-end = bottom of console + if ( key == K_END && keys[K_CTRL].down ) { + Con_Bottom(); + return; + } + + // pass to the normal editline routine + Field_KeyDownEvent( &g_consoleField, key ); +} + +//============================================================================ + + +/* +================ +Message_Key + +In game talk message +================ +*/ +void Message_Key( int key ) { + + char buffer[MAX_STRING_CHARS]; + + + if ( key == K_ESCAPE ) { + cls.keyCatchers &= ~KEYCATCH_MESSAGE; + Field_Clear( &chatField ); + return; + } + + if ( key == K_ENTER || key == K_KP_ENTER ) { + if ( chatField.buffer[0] && cls.state == CA_ACTIVE ) { + if ( chat_playerNum != -1 ) { + + Com_sprintf( buffer, sizeof( buffer ), "tell %i \"%s\"\n", chat_playerNum, chatField.buffer ); + } else if ( chat_team ) { + + Com_sprintf( buffer, sizeof( buffer ), "say_team \"%s\"\n", chatField.buffer ); + } + // NERVE - SMF + else if ( chat_limbo ) { + + Com_sprintf( buffer, sizeof( buffer ), "say_limbo \"%s\"\n", chatField.buffer ); + } + // -NERVE - SMF + else { + Com_sprintf( buffer, sizeof( buffer ), "say \"%s\"\n", chatField.buffer ); + } + + + + CL_AddReliableCommand( buffer ); + } + cls.keyCatchers &= ~KEYCATCH_MESSAGE; + Field_Clear( &chatField ); + return; + } + + Field_KeyDownEvent( &chatField, key ); +} + +//============================================================================ + + +qboolean Key_GetOverstrikeMode( void ) { + return key_overstrikeMode; +} + + +void Key_SetOverstrikeMode( qboolean state ) { + key_overstrikeMode = state; +} + + +/* +=================== +Key_IsDown +=================== +*/ +qboolean Key_IsDown( int keynum ) { + if ( keynum == -1 ) { + return qfalse; + } + + return keys[keynum].down; +} + + +/* +=================== +Key_StringToKeynum + +Returns a key number to be used to index keys[] by looking at +the given string. Single ascii characters return themselves, while +the K_* names are matched up. + +0x11 will be interpreted as raw hex, which will allow new controlers + +to be configured even if they don't have defined names. +=================== +*/ +int Key_StringToKeynum( char *str ) { + keyname_t *kn; + + if ( !str || !str[0] ) { + return -1; + } + if ( !str[1] ) { + return str[0]; + } + + // check for hex code + if ( str[0] == '0' && str[1] == 'x' && strlen( str ) == 4 ) { + int n1, n2; + + n1 = str[2]; + if ( Q_isnumeric( n1 ) ) { + n1 -= '0'; + } else if ( n1 >= 'a' && n1 <= 'f' ) { + n1 = n1 - 'a' + 10; + } else { + n1 = 0; + } + + n2 = str[3]; + if ( Q_isnumeric( n2 ) ) { + n2 -= '0'; + } else if ( n2 >= 'a' && n2 <= 'f' ) { + n2 = n2 - 'a' + 10; + } else { + n2 = 0; + } + + return n1 * 16 + n2; + } + + // scan for a text match + for ( kn = keynames ; kn->name ; kn++ ) { + if ( !Q_stricmp( str,kn->name ) ) { + return kn->keynum; + } + } + + return -1; +} + +/* +=================== +Key_KeynumToString + +Returns a string (either a single ascii char, a K_* name, or a 0x11 hex string) for the +given keynum. +=================== +*/ +char *Key_KeynumToString( int keynum, qboolean bTranslate ) { + keyname_t *kn; + static char tinystr[5]; + int i, j; + + if ( keynum == -1 ) { + return ""; + } + + if ( keynum < 0 || keynum > 255 ) { + return ""; + } + + // check for printable ascii (don't use quote) + if ( keynum > 32 && keynum < 127 && keynum != '"' ) { + tinystr[0] = keynum; + tinystr[1] = 0; + if ( keynum == ';' && !bTranslate ) { + //fall through and use keyname table + } else { + return tinystr; + } + } + + + kn = keynames; //init to english + if ( bTranslate ) { + if ( cl_language->integer - 1 == LANGUAGE_FRENCH ) { + kn = keynames_f; //use french + } else if ( cl_language->integer - 1 == LANGUAGE_GERMAN ) { + kn = keynames_d; //use german + } else if ( cl_language->integer - 1 == LANGUAGE_ITALIAN ) { + kn = keynames_i; //use italian + } else if ( cl_language->integer - 1 == LANGUAGE_SPANISH ) { + kn = keynames_s; //use spanish + } + } + + // check for a key string + for ( ; kn->name ; kn++ ) { + if ( keynum == kn->keynum ) { + return kn->name; + } + } + + // make a hex string + i = keynum >> 4; + j = keynum & 15; + + tinystr[0] = '0'; + tinystr[1] = 'x'; + tinystr[2] = i > 9 ? i - 10 + 'a' : i + '0'; + tinystr[3] = j > 9 ? j - 10 + 'a' : j + '0'; + tinystr[4] = 0; + + return tinystr; +} + + +/* +=================== +Key_SetBinding +=================== +*/ +void Key_SetBinding( int keynum, const char *binding ) { + if ( keynum == -1 ) { + return; + } + + // free old bindings + if ( keys[ keynum ].binding ) { + Z_Free( keys[ keynum ].binding ); + } + + // allocate memory for new binding + keys[keynum].binding = CopyString( binding ); + + // consider this like modifying an archived cvar, so the + // file write will be triggered at the next oportunity + cvar_modifiedFlags |= CVAR_ARCHIVE; +} + + +/* +=================== +Key_GetBinding +=================== +*/ +char *Key_GetBinding( int keynum ) { + if ( keynum == -1 ) { + return ""; + } + + return keys[ keynum ].binding; +} + +/* +=================== +Key_GetKey +=================== +*/ + +int Key_GetKey( const char *binding ) { + int i; + + if ( binding ) { + for ( i = 0 ; i < 256 ; i++ ) { + if ( keys[i].binding && Q_stricmp( binding, keys[i].binding ) == 0 ) { + return i; + } + } + } + return -1; +} + +/* +=================== +Key_Unbind_f +=================== +*/ +void Key_Unbind_f( void ) { + int b; + + if ( Cmd_Argc() != 2 ) { + Com_Printf( "unbind : remove commands from a key\n" ); + return; + } + + b = Key_StringToKeynum( Cmd_Argv( 1 ) ); + if ( b == -1 ) { + Com_Printf( "\"%s\" isn't a valid key\n", Cmd_Argv( 1 ) ); + return; + } + + Key_SetBinding( b, "" ); +} + +/* +=================== +Key_Unbindall_f +=================== +*/ +void Key_Unbindall_f( void ) { + int i; + + for ( i = 0 ; i < 256 ; i++ ) + if ( keys[i].binding ) { + Key_SetBinding( i, "" ); + } +} + + +/* +=================== +Key_Bind_f +=================== +*/ +void Key_Bind_f( void ) { + int i, c, b; + char cmd[1024]; + + c = Cmd_Argc(); + + if ( c < 2 ) { + Com_Printf( "bind [command] : attach a command to a key\n" ); + return; + } + b = Key_StringToKeynum( Cmd_Argv( 1 ) ); + if ( b == -1 ) { + Com_Printf( "\"%s\" isn't a valid key\n", Cmd_Argv( 1 ) ); + return; + } + + if ( c == 2 ) { + if ( keys[b].binding ) { + Com_Printf( "\"%s\" = \"%s\"\n", Cmd_Argv( 1 ), keys[b].binding ); + } else { + Com_Printf( "\"%s\" is not bound\n", Cmd_Argv( 1 ) ); + } + return; + } + +// copy the rest of the command line + cmd[0] = 0; // start out with a null string + for ( i = 2 ; i < c ; i++ ) + { + strcat( cmd, Cmd_Argv( i ) ); + if ( i != ( c - 1 ) ) { + strcat( cmd, " " ); + } + } + + Key_SetBinding( b, cmd ); +} + +/* +============ +Key_WriteBindings + +Writes lines containing "bind key value" +============ +*/ +void Key_WriteBindings( fileHandle_t f ) { + int i; + + FS_Printf( f, "unbindall\n" ); + + for ( i = 0 ; i < 256 ; i++ ) { + if ( keys[i].binding && keys[i].binding[0] ) { + FS_Printf( f, "bind %s \"%s\"\n", Key_KeynumToString( i, qfalse ), keys[i].binding ); + + } + + } +} + + +/* +============ +Key_Bindlist_f + +============ +*/ +void Key_Bindlist_f( void ) { + int i; + + for ( i = 0 ; i < 256 ; i++ ) { + if ( keys[i].binding && keys[i].binding[0] ) { + Com_Printf( "%s \"%s\"\n", Key_KeynumToString( i, qfalse ), keys[i].binding ); + } + } +} + + +/* +=================== +CL_InitKeyCommands +=================== +*/ +void CL_InitKeyCommands( void ) { + // register our functions + Cmd_AddCommand( "bind",Key_Bind_f ); + Cmd_AddCommand( "unbind",Key_Unbind_f ); + Cmd_AddCommand( "unbindall",Key_Unbindall_f ); + Cmd_AddCommand( "bindlist",Key_Bindlist_f ); +} + +/* +=================== +CL_KeyEvent + +Called by the system for both key up and key down events +=================== +*/ +//static int consoleCount = 0; // TTimo: unused +void CL_KeyEvent( int key, qboolean down, unsigned time ) { + char *kb; + char cmd[1024]; + int activeMenu = 0; + + // update auto-repeat status and BUTTON_ANY status + keys[key].down = down; + + if ( down ) { + keys[key].repeats++; + if ( keys[key].repeats == 1 ) { + anykeydown++; + } + } else { + keys[key].repeats = 0; + anykeydown--; + if ( anykeydown < 0 ) { + anykeydown = 0; + } + } + +#ifdef __linux__ + if ( key == K_ENTER ) { + if ( down ) { + if ( keys[K_ALT].down ) { + Key_ClearStates(); + if ( Cvar_VariableValue( "r_fullscreen" ) == 0 ) { + Com_Printf( "Switching to fullscreen rendering\n" ); + Cvar_Set( "r_fullscreen", "1" ); + } else + { + Com_Printf( "Switching to windowed rendering\n" ); + Cvar_Set( "r_fullscreen", "0" ); + } + Cbuf_ExecuteText( EXEC_APPEND, "vid_restart\n" ); + return; + } + } + } +#endif + + // console key is hardcoded, so the user can never unbind it + if ( key == '`' || key == '~' ) { + if ( !down ) { + return; + + } + Con_ToggleConsole_f(); + return; + } + +//----(SA) added + if ( cl.cameraMode ) { + if ( !( cls.keyCatchers & ( KEYCATCH_UI | KEYCATCH_CONSOLE ) ) ) { // let menu/console handle keys if necessary + + // in cutscenes we need to handle keys specially (pausing not allowed in camera mode) + if ( ( key == K_ESCAPE || + key == K_SPACE || + key == K_ENTER ) && down ) { + if ( down ) { + CL_AddReliableCommand( "cameraInterrupt" ); + } + return; + } + + // eat all keys + if ( down ) { + return; + } + } + + if ( ( cls.keyCatchers & KEYCATCH_CONSOLE ) && key == K_ESCAPE ) { + // don't allow menu starting when console is down and camera running + return; + } + } +//----(SA) end + + + // most keys during demo playback will bring up the menu, but non-ascii + + // keys can still be used for bound actions + if ( down && ( key < 128 || key == K_MOUSE1 ) + && ( clc.demoplaying || cls.state == CA_CINEMATIC ) && !cls.keyCatchers ) { + + Cvar_Set( "nextdemo","" ); + key = K_ESCAPE; + } + +//----(SA) get the active menu if in ui mode + if ( cls.keyCatchers & KEYCATCH_UI ) { + activeMenu = VM_Call( uivm, UI_GET_ACTIVE_MENU ); + } + + + // escape is always handled special + if ( key == K_ESCAPE && down ) { + if ( cls.keyCatchers & KEYCATCH_MESSAGE ) { + // clear message mode + Message_Key( key ); + return; + } + + // escape always gets out of CGAME stuff + if ( cls.keyCatchers & KEYCATCH_CGAME ) { + cls.keyCatchers &= ~KEYCATCH_CGAME; + VM_Call( cgvm, CG_EVENT_HANDLING, CGAME_EVENT_NONE ); + return; + } + + if ( !( cls.keyCatchers & KEYCATCH_UI ) ) { + if ( cls.state == CA_ACTIVE && !clc.demoplaying ) { + VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_INGAME ); + } else { + CL_Disconnect_f(); + S_StopAllSounds(); + VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_MAIN ); + } + return; + } + + if ( activeMenu == UIMENU_PREGAME ) { // eat escape too at this point + return; + } + + VM_Call( uivm, UI_KEY_EVENT, key, down ); + return; + } + + // + // key up events only perform actions if the game key binding is + // a button command (leading + sign). These will be processed even in + // console mode and menu mode, to keep the character from continuing + // an action started before a mode switch. + // + if ( !down ) { + kb = keys[key].binding; + if ( kb && kb[0] == '+' ) { + // button commands add keynum and time as parms so that multiple + // sources can be discriminated and subframe corrected + Com_sprintf( cmd, sizeof( cmd ), "-%s %i %i\n", kb + 1, key, time ); + Cbuf_AddText( cmd ); + } + + if ( cls.keyCatchers & KEYCATCH_UI && uivm ) { + VM_Call( uivm, UI_KEY_EVENT, key, down ); + } else if ( cls.keyCatchers & KEYCATCH_CGAME && cgvm ) { + VM_Call( cgvm, CG_KEY_EVENT, key, down ); + } + + return; + } + + + // distribute the key down event to the apropriate handler + if ( cls.keyCatchers & KEYCATCH_CONSOLE ) { + Console_Key( key ); + } else if ( cls.keyCatchers & KEYCATCH_UI ) { + kb = keys[key].binding; + + if ( activeMenu == UIMENU_CLIPBOARD ) { + // any key gets out of clipboard + key = K_ESCAPE; + } else if ( activeMenu == UIMENU_PREGAME ) { + if ( key != K_MOUSE1 ) { + return; // eat all keys except mouse click + } + } else { + + // when in the notebook, check for the key bound to "notebook" and allow that as an escape key + + if ( kb ) { + if ( !Q_stricmp( "notebook", kb ) ) { + if ( VM_Call( uivm, UI_GET_ACTIVE_MENU ) == UIMENU_NOTEBOOK ) { + key = K_ESCAPE; + } + } + +// if(!Q_stricmp("help", kb)) { +// if(VM_Call( uivm, UI_GET_ACTIVE_MENU) == UIMENU_HELP) +// key = K_ESCAPE; +/// } + } + } + + if ( uivm ) { + VM_Call( uivm, UI_KEY_EVENT, key, down ); + } + + } else if ( cls.keyCatchers & KEYCATCH_CGAME ) { + if ( cgvm ) { + VM_Call( cgvm, CG_KEY_EVENT, key, down ); + } + } else if ( cls.keyCatchers & KEYCATCH_MESSAGE ) { + Message_Key( key ); + } else if ( cls.state == CA_DISCONNECTED ) { + + Console_Key( key ); + + } else { + // send the bound action + kb = keys[key].binding; + if ( !kb ) { + if ( key >= 200 ) { + Com_Printf( "%s is unbound, use controls menu to set.\n" + , Key_KeynumToString( key, qfalse ) ); + } + } else if ( kb[0] == '+' ) { + // button commands add keynum and time as parms so that multiple + // sources can be discriminated and subframe corrected + Com_sprintf( cmd, sizeof( cmd ), "%s %i %i\n", kb, key, time ); + Cbuf_AddText( cmd ); + } else { + // down-only command + Cbuf_AddText( kb ); + Cbuf_AddText( "\n" ); + } + } +} + + +/* +=================== +CL_CharEvent + +Normal keyboard characters, already shifted / capslocked / etc +=================== +*/ +void CL_CharEvent( int key ) { + // the console key should never be used as a char + if ( key == '`' || key == '~' ) { + return; + } + + // distribute the key down event to the apropriate handler + if ( cls.keyCatchers & KEYCATCH_CONSOLE ) { + Field_CharEvent( &g_consoleField, key ); + } else if ( cls.keyCatchers & KEYCATCH_UI ) { + VM_Call( uivm, UI_KEY_EVENT, key | K_CHAR_FLAG, qtrue ); + } else if ( cls.keyCatchers & KEYCATCH_MESSAGE ) { + Field_CharEvent( &chatField, key ); + } else if ( cls.state == CA_DISCONNECTED ) { + Field_CharEvent( &g_consoleField, key ); + } +} + + +/* +=================== +Key_ClearStates +=================== +*/ +void Key_ClearStates( void ) { + int i; + + anykeydown = qfalse; + + for ( i = 0 ; i < MAX_KEYS ; i++ ) { + if ( keys[i].down ) { + CL_KeyEvent( i, qfalse, 0 ); + + } + keys[i].down = 0; + keys[i].repeats = 0; + } +} + diff --git a/src/client/cl_main.c b/src/client/cl_main.c new file mode 100644 index 0000000..20abecb --- /dev/null +++ b/src/client/cl_main.c @@ -0,0 +1,3621 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// cl_main.c -- client main loop + +#include "client.h" +#include + +cvar_t *cl_nodelta; +cvar_t *cl_debugMove; + +cvar_t *cl_noprint; +cvar_t *cl_motd; + +cvar_t *rcon_client_password; +cvar_t *rconAddress; + +cvar_t *cl_timeout; +cvar_t *cl_maxpackets; +cvar_t *cl_packetdup; +cvar_t *cl_timeNudge; +cvar_t *cl_showTimeDelta; +cvar_t *cl_freezeDemo; + +cvar_t *cl_shownet = NULL; // NERVE - SMF - This is referenced in msg.c and we need to make sure it is NULL +cvar_t *cl_showSend; +cvar_t *cl_timedemo; +cvar_t *cl_avidemo; +cvar_t *cl_forceavidemo; + +cvar_t *cl_freelook; +cvar_t *cl_sensitivity; + +cvar_t *cl_mouseAccel; +cvar_t *cl_showMouseRate; + +cvar_t *m_pitch; +cvar_t *m_yaw; +cvar_t *m_forward; +cvar_t *m_side; +cvar_t *m_filter; + +cvar_t *cl_activeAction; + +cvar_t *cl_motdString; + +cvar_t *cl_allowDownload; +cvar_t *cl_conXOffset; +cvar_t *cl_inGameVideo; + +cvar_t *cl_serverStatusResendTime; +cvar_t *cl_trn; +cvar_t *cl_missionStats; +cvar_t *cl_waitForFire; + +// NERVE - SMF - localization +cvar_t *cl_language; +cvar_t *cl_debugTranslation; +// -NERVE - SMF + + +char cl_cdkey[34] = " "; + +clientActive_t cl; +clientConnection_t clc; +clientStatic_t cls; +vm_t *cgvm; + +// Structure containing functions exported from refresh DLL +refexport_t re; + +ping_t cl_pinglist[MAX_PINGREQUESTS]; + +typedef struct serverStatus_s +{ + char string[BIG_INFO_STRING]; + netadr_t address; + int time, startTime; + qboolean pending; + qboolean print; + qboolean retrieved; +} serverStatus_t; + +serverStatus_t cl_serverStatusList[MAX_SERVERSTATUSREQUESTS]; +int serverStatusCount; + +#if 0 // MrE defined __USEA3D && defined __A3D_GEOM +void hA3Dg_ExportRenderGeom( refexport_t *incoming_re ); +#endif + +extern void SV_BotFrame( int time ); +void CL_CheckForResend( void ); +void CL_ShowIP_f( void ); +void CL_ServerStatus_f( void ); +void CL_ServerStatusResponse( netadr_t from, msg_t *msg ); + + +/* +============== +CL_EndgameMenu + +Called by Com_Error when a game has ended and is dropping out to main menu in the "endgame" menu ('credits' right now) +============== +*/ +void CL_EndgameMenu( void ) { + cls.endgamemenu = qtrue; // start it next frame +} + + +/* +=============== +CL_CDDialog + +Called by Com_Error when a cd is needed +=============== +*/ +void CL_CDDialog( void ) { + cls.cddialog = qtrue; // start it next frame +} + + +/* +======================================================================= + +CLIENT RELIABLE COMMAND COMMUNICATION + +======================================================================= +*/ + +/* +====================== +CL_AddReliableCommand + +The given command will be transmitted to the server, and is gauranteed to +not have future usercmd_t executed before it is executed +====================== +*/ +void CL_AddReliableCommand( const char *cmd ) { + int index; + + // if we would be losing an old command that hasn't been acknowledged, + // we must drop the connection +// if(cl.cameraMode) +// Com_Printf ("cmd: %s\n", cmd); + + if ( clc.reliableSequence - clc.reliableAcknowledge > MAX_RELIABLE_COMMANDS ) { + Com_Error( ERR_DROP, "Client command overflow" ); + } + clc.reliableSequence++; + index = clc.reliableSequence & ( MAX_RELIABLE_COMMANDS - 1 ); + Q_strncpyz( clc.reliableCommands[ index ], cmd, sizeof( clc.reliableCommands[ index ] ) ); +} + +/* +====================== +CL_ChangeReliableCommand +====================== +*/ +void CL_ChangeReliableCommand( void ) { + int r, index, l; + + r = clc.reliableSequence - ( random() * 5 ); + index = clc.reliableSequence & ( MAX_RELIABLE_COMMANDS - 1 ); + l = strlen( clc.reliableCommands[ index ] ); + if ( l >= MAX_STRING_CHARS - 1 ) { + l = MAX_STRING_CHARS - 2; + } + clc.reliableCommands[ index ][ l ] = '\n'; + clc.reliableCommands[ index ][ l + 1 ] = '\0'; +} + +/* +======================================================================= + +CLIENT SIDE DEMO RECORDING + +======================================================================= +*/ + +/* +==================== +CL_WriteDemoMessage + +Dumps the current net message, prefixed by the length +==================== +*/ +void CL_WriteDemoMessage( msg_t *msg, int headerBytes ) { + int len, swlen; + + // write the packet sequence + len = clc.serverMessageSequence; + swlen = LittleLong( len ); + FS_Write( &swlen, 4, clc.demofile ); + + // skip the packet sequencing information + len = msg->cursize - headerBytes; + swlen = LittleLong( len ); + FS_Write( &swlen, 4, clc.demofile ); + FS_Write( msg->data + headerBytes, len, clc.demofile ); +} + + +/* +==================== +CL_StopRecording_f + +stop recording a demo +==================== +*/ +void CL_StopRecord_f( void ) { + int len; + + if ( !clc.demorecording ) { + Com_Printf( "Not recording a demo.\n" ); + return; + } + + // finish up + len = -1; + FS_Write( &len, 4, clc.demofile ); + FS_Write( &len, 4, clc.demofile ); + FS_FCloseFile( clc.demofile ); + clc.demofile = 0; + clc.demorecording = qfalse; + Com_Printf( "Stopped demo.\n" ); +} + +/* +================== +CL_DemoFilename +================== +*/ +void CL_DemoFilename( int number, char *fileName ) { + int a,b,c,d; + + if ( number < 0 || number > 9999 ) { + Com_sprintf( fileName, MAX_OSPATH, "demo9999.tga" ); + return; + } + + a = number / 1000; + number -= a * 1000; + b = number / 100; + number -= b * 100; + c = number / 10; + number -= c * 10; + d = number; + + Com_sprintf( fileName, MAX_OSPATH, "demo%i%i%i%i" + , a, b, c, d ); +} + +/* +==================== +CL_Record_f + +record + +Begins recording a demo from the current position +==================== +*/ +static char demoName[MAX_QPATH]; // compiler bug workaround +void CL_Record_f( void ) { + char name[MAX_OSPATH]; + byte bufData[MAX_MSGLEN]; + msg_t buf; + int i; + int len; + entityState_t *ent; + entityState_t nullstate; + char *s; + + if ( Cmd_Argc() > 2 ) { + Com_Printf( "record \n" ); + return; + } + + if ( clc.demorecording ) { + Com_Printf( "Already recording.\n" ); + return; + } + + if ( cls.state != CA_ACTIVE ) { + Com_Printf( "You must be in a level to record.\n" ); + return; + } + + if ( !Cvar_VariableValue( "g_synchronousClients" ) ) { + Com_Printf( "The server must have 'g_synchronousClients 1' set for demos\n" ); + return; + } + + if ( Cmd_Argc() == 2 ) { + s = Cmd_Argv( 1 ); + Q_strncpyz( demoName, s, sizeof( demoName ) ); + Com_sprintf( name, sizeof( name ), "demos/%s.dm_%d", demoName, PROTOCOL_VERSION ); + } else { + int number; + + // scan for a free demo name + for ( number = 0 ; number <= 9999 ; number++ ) { + CL_DemoFilename( number, demoName ); + Com_sprintf( name, sizeof( name ), "demos/%s.dm_%d", demoName, PROTOCOL_VERSION ); + + len = FS_ReadFile( name, NULL ); + if ( len <= 0 ) { + break; // file doesn't exist + } + } + } + + // open the demo file +#ifdef __MACOS__ //DAJ MacOS file typing + { + extern _MSL_IMP_EXP_C long _fcreator, _ftype; + _ftype = 'WlfB'; + _fcreator = 'WlfS'; + } +#endif + + Com_Printf( "recording to %s.\n", name ); + clc.demofile = FS_FOpenFileWrite( name ); + if ( !clc.demofile ) { + Com_Printf( "ERROR: couldn't open.\n" ); + return; + } + clc.demorecording = qtrue; + Q_strncpyz( clc.demoName, demoName, sizeof( clc.demoName ) ); + + // don't start saving messages until a non-delta compressed message is received + clc.demowaiting = qtrue; + + // write out the gamestate message + MSG_Init( &buf, bufData, sizeof( bufData ) ); + MSG_Bitstream( &buf ); + + // NOTE, MRE: all server->client messages now acknowledge + MSG_WriteLong( &buf, clc.reliableSequence ); + + MSG_WriteByte( &buf, svc_gamestate ); + MSG_WriteLong( &buf, clc.serverCommandSequence ); + + // configstrings + for ( i = 0 ; i < MAX_CONFIGSTRINGS ; i++ ) { + if ( !cl.gameState.stringOffsets[i] ) { + continue; + } + s = cl.gameState.stringData + cl.gameState.stringOffsets[i]; + MSG_WriteByte( &buf, svc_configstring ); + MSG_WriteShort( &buf, i ); + MSG_WriteBigString( &buf, s ); + } + + // baselines + memset( &nullstate, 0, sizeof( nullstate ) ); + for ( i = 0; i < MAX_GENTITIES ; i++ ) { + ent = &cl.entityBaselines[i]; + if ( !ent->number ) { + continue; + } + MSG_WriteByte( &buf, svc_baseline ); + MSG_WriteDeltaEntity( &buf, &nullstate, ent, qtrue ); + } + + MSG_WriteByte( &buf, svc_EOF ); + + // finished writing the gamestate stuff + + // write the client num + MSG_WriteLong( &buf, clc.clientNum ); + // write the checksum feed + MSG_WriteLong( &buf, clc.checksumFeed ); + + // finished writing the client packet + MSG_WriteByte( &buf, svc_EOF ); + + // write it to the demo file + len = LittleLong( clc.serverMessageSequence - 1 ); + FS_Write( &len, 4, clc.demofile ); + + len = LittleLong( buf.cursize ); + FS_Write( &len, 4, clc.demofile ); + FS_Write( buf.data, buf.cursize, clc.demofile ); + + // the rest of the demo file will be copied from net messages +} + +/* +======================================================================= + +CLIENT SIDE DEMO PLAYBACK + +======================================================================= +*/ + +/* +================= +CL_DemoCompleted +================= +*/ +void CL_DemoCompleted( void ) { + if ( cl_timedemo && cl_timedemo->integer ) { + int time; + + time = Sys_Milliseconds() - clc.timeDemoStart; + if ( time > 0 ) { + Com_Printf( "%i frames, %3.1f seconds: %3.1f fps\n", clc.timeDemoFrames, + time / 1000.0, clc.timeDemoFrames * 1000.0 / time ); + } + } + + CL_Disconnect( qtrue ); + CL_NextDemo(); +} + +/* +================= +CL_ReadDemoMessage +================= +*/ +void CL_ReadDemoMessage( void ) { + int r; + msg_t buf; + byte bufData[ MAX_MSGLEN ]; + int s; + + if ( !clc.demofile ) { + CL_DemoCompleted(); + return; + } + + // get the sequence number + r = FS_Read( &s, 4, clc.demofile ); + if ( r != 4 ) { + CL_DemoCompleted(); + return; + } + clc.serverMessageSequence = LittleLong( s ); + + // init the message + MSG_Init( &buf, bufData, sizeof( bufData ) ); + + // get the length + r = FS_Read( &buf.cursize, 4, clc.demofile ); + if ( r != 4 ) { + CL_DemoCompleted(); + return; + } + buf.cursize = LittleLong( buf.cursize ); + if ( buf.cursize == -1 ) { + CL_DemoCompleted(); + return; + } + if ( buf.cursize > buf.maxsize ) { + Com_Error( ERR_DROP, "CL_ReadDemoMessage: demoMsglen > MAX_MSGLEN" ); + } + r = FS_Read( buf.data, buf.cursize, clc.demofile ); + if ( r != buf.cursize ) { + Com_Printf( "Demo file was truncated.\n" ); + CL_DemoCompleted(); + return; + } + + clc.lastPacketTime = cls.realtime; + buf.readcount = 0; + CL_ParseServerMessage( &buf ); +} + +/* +==================== +CL_PlayDemo_f + +demo + +==================== +*/ +void CL_PlayDemo_f( void ) { + char name[MAX_OSPATH], extension[32]; + char *arg; + + if ( Cmd_Argc() != 2 ) { + Com_Printf( "playdemo \n" ); + return; + } + + // make sure a local server is killed + Cvar_Set( "sv_killserver", "1" ); + + CL_Disconnect( qtrue ); + + +// CL_FlushMemory(); //----(SA) MEM NOTE: in missionpack, this is moved to CL_DownloadsComplete + + + // open the demo file + arg = Cmd_Argv( 1 ); + Com_sprintf( extension, sizeof( extension ), ".dm_%d", PROTOCOL_VERSION ); + if ( !Q_stricmp( arg + strlen( arg ) - strlen( extension ), extension ) ) { + Com_sprintf( name, sizeof( name ), "demos/%s", arg ); + } else { + Com_sprintf( name, sizeof( name ), "demos/%s.dm_%d", arg, PROTOCOL_VERSION ); + } + + FS_FOpenFileRead( name, &clc.demofile, qtrue ); + if ( !clc.demofile ) { + Com_Error( ERR_DROP, "couldn't open %s", name ); + return; + } + Q_strncpyz( clc.demoName, Cmd_Argv( 1 ), sizeof( clc.demoName ) ); + + Con_Close(); + + cls.state = CA_CONNECTED; + clc.demoplaying = qtrue; + Q_strncpyz( cls.servername, Cmd_Argv( 1 ), sizeof( cls.servername ) ); + + // read demo messages until connected + while ( cls.state >= CA_CONNECTED && cls.state < CA_PRIMED ) { + CL_ReadDemoMessage(); + } + // don't get the first snapshot this frame, to prevent the long + // time from the gamestate load from messing causing a time skip + clc.firstDemoFrameSkipped = qfalse; +} + + +/* +==================== +CL_StartDemoLoop + +Closing the main menu will restart the demo loop +==================== +*/ +void CL_StartDemoLoop( void ) { + // start the demo loop again + Cbuf_AddText( "d1\n" ); + cls.keyCatchers = 0; +} + +/* +================== +CL_NextDemo + +Called when a demo or cinematic finishes +If the "nextdemo" cvar is set, that command will be issued +================== +*/ +void CL_NextDemo( void ) { + char v[MAX_STRING_CHARS]; + + Q_strncpyz( v, Cvar_VariableString( "nextdemo" ), sizeof( v ) ); + v[MAX_STRING_CHARS - 1] = 0; + Com_DPrintf( "CL_NextDemo: %s\n", v ); + if ( !v[0] ) { + return; + } + + Cvar_Set( "nextdemo","" ); + Cbuf_AddText( v ); + Cbuf_AddText( "\n" ); + Cbuf_Execute(); +} + + +//====================================================================== + +/* +===================== +CL_ShutdownAll +===================== +*/ +void CL_ShutdownAll( void ) { + + // clear sounds + S_DisableSounds(); + // shutdown CGame + CL_ShutdownCGame(); + // shutdown UI + CL_ShutdownUI(); + + // shutdown the renderer + if ( re.Shutdown ) { + re.Shutdown( qfalse ); // don't destroy window or context + } + + cls.uiStarted = qfalse; + cls.cgameStarted = qfalse; + cls.rendererStarted = qfalse; + cls.soundRegistered = qfalse; +} + +/* +================= +CL_FlushMemory + +Called by CL_MapLoading, CL_Connect_f, CL_PlayDemo_f, and CL_ParseGamestate the only +ways a client gets into a game +Also called by Com_Error +================= +*/ +void CL_FlushMemory( void ) { + + // shutdown all the client stuff + CL_ShutdownAll(); + + // if not running a server clear the whole hunk + if ( !com_sv_running->integer ) { + // clear the whole hunk + Hunk_Clear(); +// // clear collision map data +// CM_ClearMap(); + } else { + // clear all the client data on the hunk + Hunk_ClearToMark(); + } + + CL_StartHunkUsers(); +} + +/* +===================== +CL_MapLoading + +A local server is starting to load a map, so update the +screen to let the user know about it, then dump all client +memory on the hunk from cgame, ui, and renderer +===================== +*/ +void CL_MapLoading( void ) { + if ( !com_cl_running->integer ) { + return; + } + + Con_Close(); + cls.keyCatchers = 0; + +// this was for multi-threaded music +// S_StartBackgroundTrack( "sound/music/l_briefing_1.wav", "", -2); // '-2' for 'queue looping track' (QUEUED_PLAY_LOOPED) + + // if we are already connected to the local host, stay connected + if ( cls.state >= CA_CONNECTED && !Q_stricmp( cls.servername, "localhost" ) ) { + cls.state = CA_CONNECTED; // so the connect screen is drawn + memset( cls.updateInfoString, 0, sizeof( cls.updateInfoString ) ); + memset( clc.serverMessage, 0, sizeof( clc.serverMessage ) ); + memset( &cl.gameState, 0, sizeof( cl.gameState ) ); + clc.lastPacketSentTime = -9999; + SCR_UpdateScreen(); + } else { + // clear nextmap so the cinematic shutdown doesn't execute it + Cvar_Set( "nextmap", "" ); + CL_Disconnect( qtrue ); + Q_strncpyz( cls.servername, "localhost", sizeof( cls.servername ) ); + cls.state = CA_CHALLENGING; // so the connect screen is drawn + cls.keyCatchers = 0; + SCR_UpdateScreen(); + clc.connectTime = -RETRANSMIT_TIMEOUT; + NET_StringToAdr( cls.servername, &clc.serverAddress ); + // we don't need a challenge on the localhost + + CL_CheckForResend(); + } + + // make sure sound is quiet + S_FadeAllSounds( 0, 0 ); +} + +/* +===================== +CL_ClearState + +Called before parsing a gamestate +===================== +*/ +void CL_ClearState( void ) { + + S_StopAllSounds(); + + memset( &cl, 0, sizeof( cl ) ); +} + + +/* +===================== +CL_Disconnect + +Called when a connection, demo, or cinematic is being terminated. +Goes from a connected state to either a menu state or a console state +Sends a disconnect message to the server +This is also called on Com_Error and Com_Quit, so it shouldn't cause any errors +===================== +*/ +void CL_Disconnect( qboolean showMainMenu ) { + if ( !com_cl_running || !com_cl_running->integer ) { + return; + } + + // shutting down the client so enter full screen ui mode + Cvar_Set( "r_uiFullScreen", "1" ); + + if ( clc.demorecording ) { + CL_StopRecord_f(); + } + + if ( clc.download ) { + FS_FCloseFile( clc.download ); + clc.download = 0; + } + *clc.downloadTempName = *clc.downloadName = 0; + Cvar_Set( "cl_downloadName", "" ); + + if ( clc.demofile ) { + FS_FCloseFile( clc.demofile ); + clc.demofile = 0; + } + + if ( uivm && showMainMenu ) { + VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_NONE ); + } + + SCR_StopCinematic(); + S_ClearSoundBuffer( qtrue ); //----(SA) modified + + // send a disconnect message to the server + // send it a few times in case one is dropped + if ( cls.state >= CA_CONNECTED ) { + CL_AddReliableCommand( "disconnect" ); + CL_WritePacket(); + CL_WritePacket(); + CL_WritePacket(); + } + + CL_ClearState(); + + // wipe the client connection + memset( &clc, 0, sizeof( clc ) ); + + cls.state = CA_DISCONNECTED; + + // allow cheats locally +#ifndef WOLF_SP_DEMO + // except for demo + Cvar_Set( "sv_cheats", "1" ); +#endif + + // not connected to a pure server anymore + cl_connectedToPureServer = qfalse; +} + + +/* +=================== +CL_ForwardCommandToServer + +adds the current command line as a clientCommand +things like godmode, noclip, etc, are commands directed to the server, +so when they are typed in at the console, they will need to be forwarded. +=================== +*/ +void CL_ForwardCommandToServer( const char *string ) { + char *cmd; + + cmd = Cmd_Argv( 0 ); + + // ignore key up commands + if ( cmd[0] == '-' ) { + return; + } + + if ( clc.demoplaying || cls.state < CA_CONNECTED || cmd[0] == '+' ) { + Com_Printf( "Unknown command \"%s\"\n", cmd ); + return; + } + + if ( Cmd_Argc() > 1 ) { + CL_AddReliableCommand( string ); + } else { + CL_AddReliableCommand( cmd ); + } +} + +/* +=================== +CL_RequestMotd + +=================== +*/ +void CL_RequestMotd( void ) { + char info[MAX_INFO_STRING]; + + if ( !cl_motd->integer ) { + return; + } + Com_Printf( "Resolving %s\n", UPDATE_SERVER_NAME ); + if ( !NET_StringToAdr( UPDATE_SERVER_NAME, &cls.updateServer ) ) { + Com_Printf( "Couldn't resolve address\n" ); + return; + } + cls.updateServer.port = BigShort( PORT_UPDATE ); + Com_Printf( "%s resolved to %i.%i.%i.%i:%i\n", UPDATE_SERVER_NAME, + cls.updateServer.ip[0], cls.updateServer.ip[1], + cls.updateServer.ip[2], cls.updateServer.ip[3], + BigShort( cls.updateServer.port ) ); + + info[0] = 0; + Com_sprintf( cls.updateChallenge, sizeof( cls.updateChallenge ), "%i", rand() ); + + Info_SetValueForKey( info, "challenge", cls.updateChallenge ); + Info_SetValueForKey( info, "renderer", cls.glconfig.renderer_string ); + Info_SetValueForKey( info, "version", com_version->string ); + + NET_OutOfBandPrint( NS_CLIENT, cls.updateServer, "getmotd \"%s\"\n", info ); +} + + +/* +=================== +CL_RequestAuthorization + +Authorization server protocol +----------------------------- + +All commands are text in Q3 out of band packets (leading 0xff 0xff 0xff 0xff). + +Whenever the client tries to get a challenge from the server it wants to +connect to, it also blindly fires off a packet to the authorize server: + +getKeyAuthorize + +cdkey may be "demo" + + +#OLD The authorize server returns a: +#OLD +#OLD keyAthorize +#OLD +#OLD A client will be accepted if the cdkey is valid and it has not been used by any other IP +#OLD address in the last 15 minutes. + + +The server sends a: + +getIpAuthorize + +The authorize server returns a: + +ipAuthorize + +A client will be accepted if a valid cdkey was sent by that ip (only) in the last 15 minutes. +If no response is received from the authorize server after two tries, the client will be let +in anyway. +=================== +*/ +void CL_RequestAuthorization( void ) { + char nums[64]; + int i, j, l; + cvar_t *fs; + + if ( !cls.authorizeServer.port ) { + Com_Printf( "Resolving %s\n", AUTHORIZE_SERVER_NAME ); + if ( !NET_StringToAdr( AUTHORIZE_SERVER_NAME, &cls.authorizeServer ) ) { + Com_Printf( "Couldn't resolve address\n" ); + return; + } + + cls.authorizeServer.port = BigShort( PORT_AUTHORIZE ); + Com_Printf( "%s resolved to %i.%i.%i.%i:%i\n", AUTHORIZE_SERVER_NAME, + cls.authorizeServer.ip[0], cls.authorizeServer.ip[1], + cls.authorizeServer.ip[2], cls.authorizeServer.ip[3], + BigShort( cls.authorizeServer.port ) ); + } + if ( cls.authorizeServer.type == NA_BAD ) { + return; + } + + if ( Cvar_VariableValue( "fs_restrict" ) ) { + Q_strncpyz( nums, "demo", sizeof( nums ) ); + } else { + // only grab the alphanumeric values from the cdkey, to avoid any dashes or spaces + j = 0; + l = strlen( cl_cdkey ); + if ( l > 32 ) { + l = 32; + } + for ( i = 0 ; i < l ; i++ ) { + if ( ( cl_cdkey[i] >= '0' && cl_cdkey[i] <= '9' ) + || ( cl_cdkey[i] >= 'a' && cl_cdkey[i] <= 'z' ) + || ( cl_cdkey[i] >= 'A' && cl_cdkey[i] <= 'Z' ) + ) { + nums[j] = cl_cdkey[i]; + j++; + } + } + nums[j] = 0; + } + + fs = Cvar_Get( "cl_anonymous", "0", CVAR_INIT | CVAR_SYSTEMINFO ); + NET_OutOfBandPrint( NS_CLIENT, cls.authorizeServer, va( "getKeyAuthorize %i %s", fs->integer, nums ) ); +} + +/* +====================================================================== + +CONSOLE COMMANDS + +====================================================================== +*/ + +/* +================== +CL_ForwardToServer_f +================== +*/ +void CL_ForwardToServer_f( void ) { + if ( cls.state != CA_ACTIVE || clc.demoplaying ) { + Com_Printf( "Not connected to a server.\n" ); + return; + } + + // don't forward the first argument + if ( Cmd_Argc() > 1 ) { + CL_AddReliableCommand( Cmd_Args() ); + } +} + +/* +================== +CL_Setenv_f + +Mostly for controlling voodoo environment variables +================== +*/ +void CL_Setenv_f( void ) { + int argc = Cmd_Argc(); + + if ( argc > 2 ) { + char buffer[1024]; + int i; + + strcpy( buffer, Cmd_Argv( 1 ) ); + strcat( buffer, "=" ); + + for ( i = 2; i < argc; i++ ) { + strcat( buffer, Cmd_Argv( i ) ); + strcat( buffer, " " ); + } + + Q_putenv( buffer ); + } else if ( argc == 2 ) { + char *env = getenv( Cmd_Argv( 1 ) ); + + if ( env ) { + Com_Printf( "%s=%s\n", Cmd_Argv( 1 ), env ); + } else { + Com_Printf( "%s undefined\n", Cmd_Argv( 1 ), env ); + } + } +} + + +/* +================== +CL_Disconnect_f +================== +*/ +void CL_Disconnect_f( void ) { + SCR_StopCinematic(); + // RF, make sure loading variables are turned off + Cvar_Set( "savegame_loading", "0" ); + Cvar_Set( "g_reloading", "0" ); + if ( cls.state != CA_DISCONNECTED && cls.state != CA_CINEMATIC ) { + Com_Error( ERR_DISCONNECT, "Disconnected from server" ); + } +} + + +/* +================ +CL_Reconnect_f + +================ +*/ +void CL_Reconnect_f( void ) { + if ( !strlen( cls.servername ) || !strcmp( cls.servername, "localhost" ) ) { + Com_Printf( "Can't reconnect to localhost.\n" ); + return; + } + Cbuf_AddText( va( "connect %s\n", cls.servername ) ); +} + +/* +================ +CL_Connect_f + +================ +*/ +void CL_Connect_f( void ) { + char *server; + + if ( Cmd_Argc() != 2 ) { + Com_Printf( "usage: connect [server]\n" ); + return; + } + + // starting to load a map so we get out of full screen ui mode + Cvar_Set( "r_uiFullScreen", "0" ); + + // fire a message off to the motd server + CL_RequestMotd(); + + // clear any previous "server full" type messages + clc.serverMessage[0] = 0; + + server = Cmd_Argv( 1 ); + + if ( com_sv_running->integer && !strcmp( server, "localhost" ) ) { + // if running a local server, kill it + SV_Shutdown( "Server quit\n" ); + } + + // make sure a local server is killed + Cvar_Set( "sv_killserver", "1" ); + SV_Frame( 0 ); + + CL_Disconnect( qtrue ); + Con_Close(); + +// CL_FlushMemory(); //----(SA) MEM NOTE: in missionpack, this is moved to CL_DownloadsComplete + + + + Q_strncpyz( cls.servername, server, sizeof( cls.servername ) ); + + if ( !NET_StringToAdr( cls.servername, &clc.serverAddress ) ) { + Com_Printf( "Bad server address\n" ); + cls.state = CA_DISCONNECTED; + return; + } + if ( clc.serverAddress.port == 0 ) { + clc.serverAddress.port = BigShort( PORT_SERVER ); + } + Com_Printf( "%s resolved to %i.%i.%i.%i:%i\n", cls.servername, + clc.serverAddress.ip[0], clc.serverAddress.ip[1], + clc.serverAddress.ip[2], clc.serverAddress.ip[3], + BigShort( clc.serverAddress.port ) ); + + // if we aren't playing on a lan, we need to authenticate + // with the cd key + if ( NET_IsLocalAddress( clc.serverAddress ) ) { + cls.state = CA_CHALLENGING; + } else { + cls.state = CA_CONNECTING; + } + + cls.keyCatchers = 0; + clc.connectTime = -99999; // CL_CheckForResend() will fire immediately + clc.connectPacketCount = 0; + + // server connection string + Cvar_Set( "cl_currentServerAddress", server ); +} + + +/* +===================== +CL_Rcon_f + + Send the rest of the command line over as + an unconnected command. +===================== +*/ +void CL_Rcon_f( void ) { + char message[1024]; + int i; + netadr_t to; + + if ( !rcon_client_password->string ) { + Com_Printf( "You must set 'rcon_password' before\n" + "issuing an rcon command.\n" ); + return; + } + + message[0] = -1; + message[1] = -1; + message[2] = -1; + message[3] = -1; + message[4] = 0; + + strcat( message, "rcon " ); + + strcat( message, rcon_client_password->string ); + strcat( message, " " ); + + for ( i = 1 ; i < Cmd_Argc() ; i++ ) { + strcat( message, Cmd_Argv( i ) ); + strcat( message, " " ); + } + + if ( cls.state >= CA_CONNECTED ) { + to = clc.netchan.remoteAddress; + } else { + if ( !strlen( rconAddress->string ) ) { + Com_Printf( "You must either be connected,\n" + "or set the 'rconAddress' cvar\n" + "to issue rcon commands\n" ); + + return; + } + NET_StringToAdr( rconAddress->string, &to ); + if ( to.port == 0 ) { + to.port = BigShort( PORT_SERVER ); + } + } + + NET_SendPacket( NS_CLIENT, strlen( message ) + 1, message, to ); +} + +/* +================= +CL_SendPureChecksums +================= +*/ +void CL_SendPureChecksums( void ) { + const char *pChecksums; + char cMsg[MAX_INFO_VALUE]; + int i; + + // if we are pure we need to send back a command with our referenced pk3 checksums + pChecksums = FS_ReferencedPakPureChecksums(); + + // "cp" + // "Yf" + Com_sprintf( cMsg, sizeof( cMsg ), "Yf " ); + Q_strcat( cMsg, sizeof( cMsg ), pChecksums ); + for ( i = 0; i < 2; i++ ) { + cMsg[i] += 10; + } + CL_AddReliableCommand( cMsg ); +} + +/* +================= +CL_ResetPureClientAtServer +================= +*/ +void CL_ResetPureClientAtServer( void ) { + CL_AddReliableCommand( va( "vdr" ) ); +} + +/* +================= +CL_Vid_Restart_f + +Restart the video subsystem + +we also have to reload the UI and CGame because the renderer +doesn't know what graphics to reload +================= +*/ +void CL_Vid_Restart_f( void ) { + + vmCvar_t musicCvar; + + // RF, don't show percent bar, since the memory usage will just sit at the same level anyway + Cvar_Set( "com_expectedhunkusage", "-1" ); + + // don't let them loop during the restart + S_StopAllSounds(); + // shutdown the UI + CL_ShutdownUI(); + // shutdown the CGame + CL_ShutdownCGame(); + // shutdown the renderer and clear the renderer interface + CL_ShutdownRef(); + // client is no longer pure untill new checksums are sent + CL_ResetPureClientAtServer(); + // clear pak references + FS_ClearPakReferences( FS_UI_REF | FS_CGAME_REF ); + // reinitialize the filesystem if the game directory or checksum has changed + FS_ConditionalRestart( clc.checksumFeed ); + + S_BeginRegistration(); // all sound handles are now invalid + + cls.rendererStarted = qfalse; + cls.uiStarted = qfalse; + cls.cgameStarted = qfalse; + cls.soundRegistered = qfalse; + + // unpause so the cgame definately gets a snapshot and renders a frame + Cvar_Set( "cl_paused", "0" ); + + // if not running a server clear the whole hunk + if ( !com_sv_running->integer ) { + // clear the whole hunk + Hunk_Clear(); + } else { + // clear all the client data on the hunk + Hunk_ClearToMark(); + } + + // initialize the renderer interface + CL_InitRef(); + + // startup all the client stuff + CL_StartHunkUsers(); + + // start the cgame if connected + if ( cls.state > CA_CONNECTED && cls.state != CA_CINEMATIC ) { + cls.cgameStarted = qtrue; + CL_InitCGame(); + // send pure checksums + CL_SendPureChecksums(); + } + + // start music if there was any + + Cvar_Register( &musicCvar, "s_currentMusic", "", CVAR_ROM ); + if ( strlen( musicCvar.string ) ) { + S_StartBackgroundTrack( musicCvar.string, musicCvar.string, 1000 ); + } + + // fade up volume + S_FadeAllSounds( 1, 0 ); +} + +/* +================= +CL_Snd_Restart_f + +Restart the sound subsystem +The cgame and game must also be forced to restart because +handles will be invalid +================= +*/ +void CL_Snd_Restart_f( void ) { + S_Shutdown(); + S_Init(); + + CL_Vid_Restart_f(); +} + + +/* +================== +CL_PK3List_f +================== +*/ +void CL_OpenedPK3List_f( void ) { + Com_Printf( "Opened PK3 Names: %s\n", FS_LoadedPakNames() ); +} + +/* +================== +CL_PureList_f +================== +*/ +void CL_ReferencedPK3List_f( void ) { + Com_Printf( "Referenced PK3 Names: %s\n", FS_ReferencedPakNames() ); +} + +/* +================== +CL_Configstrings_f +================== +*/ +void CL_Configstrings_f( void ) { + int i; + int ofs; + + if ( cls.state != CA_ACTIVE ) { + Com_Printf( "Not connected to a server.\n" ); + return; + } + + for ( i = 0 ; i < MAX_CONFIGSTRINGS ; i++ ) { + ofs = cl.gameState.stringOffsets[ i ]; + if ( !ofs ) { + continue; + } + Com_Printf( "%4i: %s\n", i, cl.gameState.stringData + ofs ); + } +} + +/* +============== +CL_Clientinfo_f +============== +*/ +void CL_Clientinfo_f( void ) { + Com_Printf( "--------- Client Information ---------\n" ); + Com_Printf( "state: %i\n", cls.state ); + Com_Printf( "Server: %s\n", cls.servername ); + Com_Printf( "User info settings:\n" ); + Info_Print( Cvar_InfoString( CVAR_USERINFO ) ); + Com_Printf( "--------------------------------------\n" ); +} + + +//==================================================================== + +/* +================= +CL_DownloadsComplete + +Called when all downloading has been completed +================= +*/ +void CL_DownloadsComplete( void ) { + + // if we downloaded files we need to restart the file system + if ( clc.downloadRestart ) { + clc.downloadRestart = qfalse; + + FS_Restart( clc.checksumFeed ); // We possibly downloaded a pak, restart the file system to load it + + // inform the server so we get new gamestate info + CL_AddReliableCommand( "donedl" ); + + // by sending the donenl command we request a new gamestate + // so we don't want to load stuff yet + return; + } + + // let the client game init and load data + cls.state = CA_LOADING; + +//----(SA) removed some loading stuff + Com_EventLoop(); + + // if the gamestate was changed by calling Com_EventLoop + // then we loaded everything already and we don't want to do it again. + if ( cls.state != CA_LOADING ) { + return; + } + + // starting to load a map so we get out of full screen ui mode + Cvar_Set( "r_uiFullScreen", "0" ); + + // flush client memory and start loading stuff + // this will also (re)load the UI + // if this is a local client then only the client part of the hunk + // will be cleared, note that this is done after the hunk mark has been set + CL_FlushMemory(); + + // initialize the CGame + cls.cgameStarted = qtrue; + CL_InitCGame(); + + // set pure checksums + CL_SendPureChecksums(); + + CL_WritePacket(); + CL_WritePacket(); + CL_WritePacket(); +} + +/* +================= +CL_BeginDownload + +Requests a file to download from the server. Stores it in the current +game directory. +================= +*/ +void CL_BeginDownload( const char *localName, const char *remoteName ) { + + Com_DPrintf( "***** CL_BeginDownload *****\n" + "Localname: %s\n" + "Remotename: %s\n" + "****************************\n", localName, remoteName ); + + Q_strncpyz( clc.downloadName, localName, sizeof( clc.downloadName ) ); + Com_sprintf( clc.downloadTempName, sizeof( clc.downloadTempName ), "%s.tmp", localName ); + + // Set so UI gets access to it + Cvar_Set( "cl_downloadName", remoteName ); + Cvar_Set( "cl_downloadSize", "0" ); + Cvar_Set( "cl_downloadCount", "0" ); + Cvar_SetValue( "cl_downloadTime", cls.realtime ); + + clc.downloadBlock = 0; // Starting new file + clc.downloadCount = 0; + + CL_AddReliableCommand( va( "download %s", remoteName ) ); +} + +/* +================= +CL_NextDownload + +A download completed or failed +================= +*/ +void CL_NextDownload( void ) { + char *s; + char *remoteName, *localName; + + // We are looking to start a download here + if ( *clc.downloadList ) { + s = clc.downloadList; + + // format is: + // @remotename@localname@remotename@localname, etc. + + if ( *s == '@' ) { + s++; + } + remoteName = s; + + if ( ( s = strchr( s, '@' ) ) == NULL ) { + CL_DownloadsComplete(); + return; + } + + *s++ = 0; + localName = s; + if ( ( s = strchr( s, '@' ) ) != NULL ) { + *s++ = 0; + } else { + s = localName + strlen( localName ); // point at the nul byte + + } + CL_BeginDownload( localName, remoteName ); + + clc.downloadRestart = qtrue; + + // move over the rest + memmove( clc.downloadList, s, strlen( s ) + 1 ); + + return; + } + + CL_DownloadsComplete(); +} + +/* +================= +CL_InitDownloads + +After receiving a valid game state, we valid the cgame and local zip files here +and determine if we need to download them +================= +*/ +void CL_InitDownloads( void ) { + + if ( cl_allowDownload->integer && + FS_ComparePaks( clc.downloadList, sizeof( clc.downloadList ), qfalse ) ) { + + if ( *clc.downloadList ) { + // if autodownloading is not enabled on the server + cls.state = CA_CONNECTED; + CL_NextDownload(); + return; + } + + } + + CL_DownloadsComplete(); +} + +/* +================= +CL_CheckForResend + +Resend a connect message if the last one has timed out +================= +*/ +void CL_CheckForResend( void ) { + int port; + char info[MAX_INFO_STRING]; + + // don't send anything if playing back a demo + if ( clc.demoplaying ) { + return; + } + + // resend if we haven't gotten a reply yet + if ( cls.state != CA_CONNECTING && cls.state != CA_CHALLENGING ) { + return; + } + + if ( cls.realtime - clc.connectTime < RETRANSMIT_TIMEOUT ) { + return; + } + + clc.connectTime = cls.realtime; // for retransmit requests + clc.connectPacketCount++; + + + switch ( cls.state ) { + case CA_CONNECTING: + // requesting a challenge + if ( !Sys_IsLANAddress( clc.serverAddress ) ) { + CL_RequestAuthorization(); + } + NET_OutOfBandPrint( NS_CLIENT, clc.serverAddress, "getchallenge" ); + break; + + case CA_CHALLENGING: + // sending back the challenge + port = Cvar_VariableValue( "net_qport" ); + + Q_strncpyz( info, Cvar_InfoString( CVAR_USERINFO ), sizeof( info ) ); + Info_SetValueForKey( info, "protocol", va( "%i", PROTOCOL_VERSION ) ); + Info_SetValueForKey( info, "qport", va( "%i", port ) ); + Info_SetValueForKey( info, "challenge", va( "%i", clc.challenge ) ); + NET_OutOfBandPrint( NS_CLIENT, clc.serverAddress, "connect \"%s\"", info ); + // the most current userinfo has been sent, so watch for any + // newer changes to userinfo variables + cvar_modifiedFlags &= ~CVAR_USERINFO; + break; + + default: + Com_Error( ERR_FATAL, "CL_CHeckForResend: bad cls.state" ); + } +} + + +/* +=================== +CL_DisconnectPacket + +Sometimes the server can drop the client and the netchan based +disconnect can be lost. If the client continues to send packets +to the server, the server will send out of band disconnect packets +to the client so it doesn't have to wait for the full timeout period. +=================== +*/ +void CL_DisconnectPacket( netadr_t from ) { + if ( cls.state < CA_AUTHORIZING ) { + return; + } + + // if not from our server, ignore it + if ( !NET_CompareAdr( from, clc.netchan.remoteAddress ) ) { + return; + } + + // if we have received packets within three seconds, ignore it + // (it might be a malicious spoof) + if ( cls.realtime - clc.lastPacketTime < 3000 ) { + return; + } + + // drop the connection (FIXME: connection dropped dialog) + Com_Printf( "Server disconnected for unknown reason\n" ); + CL_Disconnect( qtrue ); +} + + +/* +=================== +CL_MotdPacket + +=================== +*/ +void CL_MotdPacket( netadr_t from ) { + char *challenge; + char *info; + + // if not from our server, ignore it + if ( !NET_CompareAdr( from, cls.updateServer ) ) { + return; + } + + info = Cmd_Argv( 1 ); + + // check challenge + challenge = Info_ValueForKey( info, "challenge" ); + if ( strcmp( challenge, cls.updateChallenge ) ) { + return; + } + + challenge = Info_ValueForKey( info, "motd" ); + + Q_strncpyz( cls.updateInfoString, info, sizeof( cls.updateInfoString ) ); + Cvar_Set( "cl_motdString", challenge ); +} + +/* +=================== +CL_InitServerInfo +=================== +*/ +void CL_InitServerInfo( serverInfo_t *server, serverAddress_t *address ) { + server->adr.type = NA_IP; + server->adr.ip[0] = address->ip[0]; + server->adr.ip[1] = address->ip[1]; + server->adr.ip[2] = address->ip[2]; + server->adr.ip[3] = address->ip[3]; + server->adr.port = address->port; + server->clients = 0; + server->hostName[0] = '\0'; + server->mapName[0] = '\0'; + server->maxClients = 0; + server->maxPing = 0; + server->minPing = 0; + server->ping = -1; + server->game[0] = '\0'; + server->gameType = 0; + server->netType = 0; + server->allowAnonymous = 0; +} + +#define MAX_SERVERSPERPACKET 256 + +/* +=================== +CL_ServersResponsePacket +=================== +*/ +void CL_ServersResponsePacket( netadr_t from, msg_t *msg ) { + int i, count, max, total; + serverAddress_t addresses[MAX_SERVERSPERPACKET]; + int numservers; + byte* buffptr; + byte* buffend; + + Com_Printf( "CL_ServersResponsePacket\n" ); + + if ( cls.numglobalservers == -1 ) { + // state to detect lack of servers or lack of response + cls.numglobalservers = 0; + cls.numGlobalServerAddresses = 0; + } + + if ( cls.nummplayerservers == -1 ) { + cls.nummplayerservers = 0; + } + + // parse through server response string + numservers = 0; + buffptr = msg->data; + buffend = buffptr + msg->cursize; + while ( buffptr + 1 < buffend ) { + // advance to initial token + do { + if ( *buffptr++ == '\\' ) { + break; + } + } + while ( buffptr < buffend ); + + // parse out ip + addresses[numservers].ip[0] = *buffptr++; + addresses[numservers].ip[1] = *buffptr++; + addresses[numservers].ip[2] = *buffptr++; + addresses[numservers].ip[3] = *buffptr++; + + // parse out port + addresses[numservers].port = ( *buffptr++ ) << 8; + addresses[numservers].port += *buffptr++; + addresses[numservers].port = BigShort( addresses[numservers].port ); + + // syntax check + if ( *buffptr != '\\' ) { + break; + } + + Com_DPrintf( "server: %d ip: %d.%d.%d.%d:%d\n",numservers, + addresses[numservers].ip[0], + addresses[numservers].ip[1], + addresses[numservers].ip[2], + addresses[numservers].ip[3], + addresses[numservers].port ); + + numservers++; + if ( numservers >= MAX_SERVERSPERPACKET ) { + break; + } + + // parse out EOT + if ( buffptr[1] == 'E' && buffptr[2] == 'O' && buffptr[3] == 'T' ) { + break; + } + } + + if ( cls.masterNum == 0 ) { + count = cls.numglobalservers; + max = MAX_GLOBAL_SERVERS; + } else { + count = cls.nummplayerservers; + max = MAX_OTHER_SERVERS; + } + + for ( i = 0; i < numservers && count < max; i++ ) { + // build net address + serverInfo_t *server = ( cls.masterNum == 0 ) ? &cls.globalServers[count] : &cls.mplayerServers[count]; + + CL_InitServerInfo( server, &addresses[i] ); + // advance to next slot + count++; + } + + // if getting the global list + if ( cls.masterNum == 0 ) { + if ( cls.numGlobalServerAddresses < MAX_GLOBAL_SERVERS ) { + // if we couldn't store the servers in the main list anymore + for (; i < numservers && count >= max; i++ ) { + serverAddress_t *addr; + // just store the addresses in an additional list + addr = &cls.globalServerAddresses[cls.numGlobalServerAddresses++]; + addr->ip[0] = addresses[i].ip[0]; + addr->ip[1] = addresses[i].ip[1]; + addr->ip[2] = addresses[i].ip[2]; + addr->ip[3] = addresses[i].ip[3]; + addr->port = addresses[i].port; + } + } + } + + if ( cls.masterNum == 0 ) { + cls.numglobalservers = count; + total = count + cls.numGlobalServerAddresses; + } else { + cls.nummplayerservers = count; + total = count; + } + + Com_Printf( "%d servers parsed (total %d)\n", numservers, total ); +} + +/* +================= +CL_ConnectionlessPacket + +Responses to broadcasts, etc +================= +*/ +void CL_ConnectionlessPacket( netadr_t from, msg_t *msg ) { + char *s; + char *c; + + MSG_BeginReadingOOB( msg ); + MSG_ReadLong( msg ); // skip the -1 + + s = MSG_ReadStringLine( msg ); + + Cmd_TokenizeString( s ); + + c = Cmd_Argv( 0 ); + + Com_DPrintf( "CL packet %s: %s\n", NET_AdrToString( from ), c ); + + // challenge from the server we are connecting to + if ( !Q_stricmp( c, "challengeResponse" ) ) { + if ( cls.state != CA_CONNECTING ) { + Com_Printf( "Unwanted challenge response received. Ignored.\n" ); + } else { + // start sending challenge repsonse instead of challenge request packets + clc.challenge = atoi( Cmd_Argv( 1 ) ); + cls.state = CA_CHALLENGING; + clc.connectPacketCount = 0; + clc.connectTime = -99999; + + // take this address as the new server address. This allows + // a server proxy to hand off connections to multiple servers + clc.serverAddress = from; + } + return; + } + + // server connection + if ( !Q_stricmp( c, "connectResponse" ) ) { + if ( cls.state >= CA_CONNECTED ) { + Com_Printf( "Dup connect received. Ignored.\n" ); + return; + } + if ( cls.state != CA_CHALLENGING ) { + Com_Printf( "connectResponse packet while not connecting. Ignored.\n" ); + return; + } + if ( !NET_CompareBaseAdr( from, clc.serverAddress ) ) { + Com_Printf( "connectResponse from a different address. Ignored.\n" ); + Com_Printf( "%s should have been %s\n", NET_AdrToString( from ), + NET_AdrToString( clc.serverAddress ) ); + return; + } + Netchan_Setup( NS_CLIENT, &clc.netchan, from, Cvar_VariableValue( "net_qport" ) ); + cls.state = CA_CONNECTED; + clc.lastPacketSentTime = -9999; // send first packet immediately + return; + } + + // server responding to an info broadcast + if ( !Q_stricmp( c, "infoResponse" ) ) { + CL_ServerInfoPacket( from, msg ); + return; + } + + // server responding to a get playerlist + if ( !Q_stricmp( c, "statusResponse" ) ) { + CL_ServerStatusResponse( from, msg ); + return; + } + + // a disconnect message from the server, which will happen if the server + // dropped the connection but it is still getting packets from us + if ( !Q_stricmp( c, "disconnect" ) ) { + CL_DisconnectPacket( from ); + return; + } + + // echo request from server + if ( !Q_stricmp( c, "echo" ) ) { + NET_OutOfBandPrint( NS_CLIENT, from, "%s", Cmd_Argv( 1 ) ); + return; + } + + // cd check + if ( !Q_stricmp( c, "keyAuthorize" ) ) { + // we don't use these now, so dump them on the floor + return; + } + + // global MOTD from id + if ( !Q_stricmp( c, "motd" ) ) { + CL_MotdPacket( from ); + return; + } + + // echo request from server + if ( !Q_stricmp( c, "print" ) ) { + s = MSG_ReadString( msg ); + Q_strncpyz( clc.serverMessage, s, sizeof( clc.serverMessage ) ); + Com_Printf( "%s", s ); + return; + } + + // echo request from server + if ( !Q_stricmp( c, "getserversResponse\\" ) ) { + CL_ServersResponsePacket( from, msg ); + return; + } + + Com_DPrintf( "Unknown connectionless packet command.\n" ); +} + + +/* +================= +CL_PacketEvent + +A packet has arrived from the main event loop +================= +*/ +void CL_PacketEvent( netadr_t from, msg_t *msg ) { + int headerBytes; + + clc.lastPacketTime = cls.realtime; + + if ( msg->cursize >= 4 && *(int *)msg->data == -1 ) { + CL_ConnectionlessPacket( from, msg ); + return; + } + + if ( cls.state < CA_CONNECTED ) { + return; // can't be a valid sequenced packet + } + + if ( msg->cursize < 4 ) { + Com_Printf( "%s: Runt packet\n",NET_AdrToString( from ) ); + return; + } + + // + // packet from server + // + if ( !NET_CompareAdr( from, clc.netchan.remoteAddress ) ) { + Com_DPrintf( "%s:sequenced packet without connection\n" + ,NET_AdrToString( from ) ); + // FIXME: send a client disconnect? + return; + } + + if ( !CL_Netchan_Process( &clc.netchan, msg ) ) { + return; // out of order, duplicated, etc + } + + // the header is different lengths for reliable and unreliable messages + headerBytes = msg->readcount; + + // track the last message received so it can be returned in + // client messages, allowing the server to detect a dropped + // gamestate + clc.serverMessageSequence = LittleLong( *(int *)msg->data ); + + clc.lastPacketTime = cls.realtime; + CL_ParseServerMessage( msg ); + + // + // we don't know if it is ok to save a demo message until + // after we have parsed the frame + // + if ( clc.demorecording && !clc.demowaiting ) { + CL_WriteDemoMessage( msg, headerBytes ); + } +} + +/* +================== +CL_CheckTimeout + +================== +*/ +void CL_CheckTimeout( void ) { + // + // check timeout + // + if ( ( !cl_paused->integer || !sv_paused->integer ) + && cls.state >= CA_CONNECTED && cls.state != CA_CINEMATIC + && cls.realtime - clc.lastPacketTime > cl_timeout->value * 1000 ) { + if ( ++cl.timeoutcount > 5 ) { // timeoutcount saves debugger + Com_Printf( "\nServer connection timed out.\n" ); + CL_Disconnect( qtrue ); + return; + } + } else { + cl.timeoutcount = 0; + } +} + + +//============================================================================ + +/* +================== +CL_CheckUserinfo + +================== +*/ +void CL_CheckUserinfo( void ) { + // don't add reliable commands when not yet connected + if ( cls.state < CA_CHALLENGING ) { + return; + } + // don't overflow the reliable command buffer when paused + if ( cl_paused->integer ) { + return; + } + // send a reliable userinfo update if needed + if ( cvar_modifiedFlags & CVAR_USERINFO ) { + cvar_modifiedFlags &= ~CVAR_USERINFO; + CL_AddReliableCommand( va( "userinfo \"%s\"", Cvar_InfoString( CVAR_USERINFO ) ) ); + } + +} + +/* +================== +CL_Frame + +================== +*/ +void CL_Frame( int msec ) { + + if ( !com_cl_running->integer ) { + return; + } + + if ( cls.cddialog ) { + // bring up the cd error dialog if needed + cls.cddialog = qfalse; +#ifdef __MACOS__ //DAJ hide the cursor for intro movie + VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_BRIEFING ); +#else + VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_NEED_CD ); +#endif + } else if ( cls.endgamemenu ) { + cls.endgamemenu = qfalse; + VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_ENDGAME ); + } else if ( cls.state == CA_DISCONNECTED && !( cls.keyCatchers & KEYCATCH_UI ) + && !com_sv_running->integer ) { + // if disconnected, bring up the menu + S_StopAllSounds(); + VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_MAIN ); + } + + // if recording an avi, lock to a fixed fps + if ( cl_avidemo->integer && msec ) { + // save the current screen + if ( cls.state == CA_ACTIVE || cl_forceavidemo->integer ) { + Cbuf_ExecuteText( EXEC_NOW, "screenshot silent\n" ); + } + // fixed time for next frame + msec = ( 1000 / cl_avidemo->integer ) * com_timescale->value; + if ( msec == 0 ) { + msec = 1; + } + } + + // save the msec before checking pause + cls.realFrametime = msec; + + // decide the simulation time + cls.frametime = msec; + + cls.realtime += cls.frametime; + + if ( cl_timegraph->integer ) { + SCR_DebugGraph( cls.realFrametime * 0.25, 0 ); + } + + // see if we need to update any userinfo + CL_CheckUserinfo(); + + // if we haven't gotten a packet in a long time, + // drop the connection + CL_CheckTimeout(); + + // send intentions now + CL_SendCmd(); + + // resend a connection request if necessary + CL_CheckForResend(); + + // decide on the serverTime to render + CL_SetCGameTime(); + + // update the screen + SCR_UpdateScreen(); + + // Ridah, don't update if we're doing a quick reload +// if (Cvar_VariableIntegerValue("savegame_loading") != 2) { +// // if waiting at intermission, don't update sound +// char buf[MAX_QPATH]; +// Cvar_VariableStringBuffer( "g_missionStats", buf, sizeof(buf) ); +// if (strlen(buf) <= 1 ) { +// // update audio + S_Update(); +// } +// } + + // advance local effects for next frame + SCR_RunCinematic(); + + Con_RunConsole(); + + cls.framecount++; +} + + +//============================================================================ +// Ridah, startup-caching system +typedef struct { + char name[MAX_QPATH]; + int hits; + int lastSetIndex; +} cacheItem_t; +typedef enum { + CACHE_SOUNDS, + CACHE_MODELS, + CACHE_IMAGES, + + CACHE_NUMGROUPS +} cacheGroup_t; +static cacheItem_t cacheGroups[CACHE_NUMGROUPS] = { + {{'s','o','u','n','d',0}, CACHE_SOUNDS}, + {{'m','o','d','e','l',0}, CACHE_MODELS}, + {{'i','m','a','g','e',0}, CACHE_IMAGES}, +}; +#define MAX_CACHE_ITEMS 4096 +#define CACHE_HIT_RATIO 0.75 // if hit on this percentage of maps, it'll get cached + +static int cacheIndex; +static cacheItem_t cacheItems[CACHE_NUMGROUPS][MAX_CACHE_ITEMS]; + +static void CL_Cache_StartGather_f( void ) { + cacheIndex = 0; + memset( cacheItems, 0, sizeof( cacheItems ) ); + + Cvar_Set( "cl_cacheGathering", "1" ); +} + +static void CL_Cache_UsedFile_f( void ) { + char groupStr[MAX_QPATH]; + char itemStr[MAX_QPATH]; + int i,group; + cacheItem_t *item; + + if ( Cmd_Argc() < 2 ) { + Com_Error( ERR_DROP, "usedfile without enough parameters\n" ); + return; + } + + strcpy( groupStr, Cmd_Argv( 1 ) ); + + strcpy( itemStr, Cmd_Argv( 2 ) ); + for ( i = 3; i < Cmd_Argc(); i++ ) { + strcat( itemStr, " " ); + strcat( itemStr, Cmd_Argv( i ) ); + } + Q_strlwr( itemStr ); + + // find the cache group + for ( i = 0; i < CACHE_NUMGROUPS; i++ ) { + if ( !Q_strncmp( groupStr, cacheGroups[i].name, MAX_QPATH ) ) { + break; + } + } + if ( i == CACHE_NUMGROUPS ) { + Com_Error( ERR_DROP, "usedfile without a valid cache group\n" ); + return; + } + + // see if it's already there + group = i; + for ( i = 0, item = cacheItems[group]; i < MAX_CACHE_ITEMS; i++, item++ ) { + if ( !item->name[0] ) { + // didn't find it, so add it here + Q_strncpyz( item->name, itemStr, MAX_QPATH ); + if ( cacheIndex > 9999 ) { // hack, but yeh + item->hits = cacheIndex; + } else { + item->hits++; + } + item->lastSetIndex = cacheIndex; + break; + } + if ( item->name[0] == itemStr[0] && !Q_strncmp( item->name, itemStr, MAX_QPATH ) ) { + if ( item->lastSetIndex != cacheIndex ) { + item->hits++; + item->lastSetIndex = cacheIndex; + } + break; + } + } +} + +static void CL_Cache_SetIndex_f( void ) { + if ( Cmd_Argc() < 2 ) { + Com_Error( ERR_DROP, "setindex needs an index\n" ); + return; + } + + cacheIndex = atoi( Cmd_Argv( 1 ) ); +} + +static void CL_Cache_MapChange_f( void ) { + cacheIndex++; +} + +static void CL_Cache_EndGather_f( void ) { + // save the frequently used files to the cache list file + int i, j, handle, cachePass; + char filename[MAX_QPATH]; + + cachePass = (int)floor( (float)cacheIndex * CACHE_HIT_RATIO ); + + for ( i = 0; i < CACHE_NUMGROUPS; i++ ) { + Q_strncpyz( filename, cacheGroups[i].name, MAX_QPATH ); + Q_strcat( filename, MAX_QPATH, ".cache" ); + +#ifdef __MACOS__ //DAJ MacOS file typing + { + extern _MSL_IMP_EXP_C long _fcreator, _ftype; + _ftype = 'WlfB'; + _fcreator = 'WlfS'; + } +#endif + handle = FS_FOpenFileWrite( filename ); + + for ( j = 0; j < MAX_CACHE_ITEMS; j++ ) { + // if it's a valid filename, and it's been hit enough times, cache it + if ( cacheItems[i][j].hits >= cachePass && strstr( cacheItems[i][j].name, "/" ) ) { + FS_Write( cacheItems[i][j].name, strlen( cacheItems[i][j].name ), handle ); + FS_Write( "\n", 1, handle ); + } + } + + FS_FCloseFile( handle ); + } + + Cvar_Set( "cl_cacheGathering", "0" ); +} + +// done. +//============================================================================ + +/* +================ +CL_MapRestart_f +================ +*/ +void CL_MapRestart_f( void ) { + if ( !com_cl_running ) { + return; + } + if ( !com_cl_running->integer ) { + return; + } + Com_Printf( "This command is no longer functional.\nUse \"loadgame current\" to load the current map." ); +} + +/* +================ +CL_SetRecommended_f +================ +*/ +void CL_SetRecommended_f( void ) { + if ( Cmd_Argc() > 1 ) { + Com_SetRecommended( qtrue ); + } else { + Com_SetRecommended( qfalse ); + } + +} + + + +/* +================ +CL_RefPrintf + +DLL glue +================ +*/ +#define MAXPRINTMSG 4096 +void QDECL CL_RefPrintf( int print_level, const char *fmt, ... ) { + va_list argptr; + char msg[MAXPRINTMSG]; + + va_start( argptr,fmt ); + vsprintf( msg,fmt,argptr ); + va_end( argptr ); + + if ( print_level == PRINT_ALL ) { + Com_Printf( "%s", msg ); + } else if ( print_level == PRINT_WARNING ) { + Com_Printf( S_COLOR_YELLOW "%s", msg ); // yellow + } else if ( print_level == PRINT_DEVELOPER ) { + Com_DPrintf( S_COLOR_RED "%s", msg ); // red + } +} + + + +/* +============ +CL_ShutdownRef +============ +*/ +void CL_ShutdownRef( void ) { + if ( !re.Shutdown ) { + return; + } + re.Shutdown( qtrue ); + memset( &re, 0, sizeof( re ) ); +} + +/* +============ +CL_InitRenderer +============ +*/ +void CL_InitRenderer( void ) { + // this sets up the renderer and calls R_Init + re.BeginRegistration( &cls.glconfig ); + + // load character sets + cls.charSetShader = re.RegisterShader( "gfx/2d/bigchars" ); + cls.whiteShader = re.RegisterShader( "white" ); + cls.consoleShader = re.RegisterShader( "console" ); + cls.consoleShader2 = re.RegisterShader( "console2" ); + g_console_field_width = cls.glconfig.vidWidth / SMALLCHAR_WIDTH - 2; + g_consoleField.widthInChars = g_console_field_width; +} + +/* +============================ +CL_StartHunkUsers + +After the server has cleared the hunk, these will need to be restarted +This is the only place that any of these functions are called from +============================ +*/ +void CL_StartHunkUsers( void ) { + if ( !com_cl_running ) { + return; + } + + if ( !com_cl_running->integer ) { + return; + } + + if ( !cls.rendererStarted ) { + cls.rendererStarted = qtrue; + CL_InitRenderer(); + } + + if ( !cls.soundStarted ) { + cls.soundStarted = qtrue; + S_Init(); + } + + if ( !cls.soundRegistered ) { + cls.soundRegistered = qtrue; + S_BeginRegistration(); + } + + if ( !cls.uiStarted ) { + cls.uiStarted = qtrue; + CL_InitUI(); + } +} + + +int CL_ScaledMilliseconds( void ) { + return Sys_Milliseconds() * com_timescale->value; +} + +/* +============ +CL_InitRef +============ +*/ +void CL_InitRef( void ) { + refimport_t ri; + refexport_t *ret; + + Com_Printf( "----- Initializing Renderer ----\n" ); + + ri.Cmd_AddCommand = Cmd_AddCommand; + ri.Cmd_RemoveCommand = Cmd_RemoveCommand; + ri.Cmd_Argc = Cmd_Argc; + ri.Cmd_Argv = Cmd_Argv; + ri.Cmd_ExecuteText = Cbuf_ExecuteText; + ri.Printf = CL_RefPrintf; + ri.Error = Com_Error; + ri.Milliseconds = CL_ScaledMilliseconds; + ri.Hunk_Clear = Hunk_ClearToMark; +#ifdef HUNK_DEBUG + ri.Hunk_AllocDebug = Hunk_AllocDebug; +#else + ri.Hunk_Alloc = Hunk_Alloc; +#endif + ri.Hunk_AllocateTempMemory = Hunk_AllocateTempMemory; + ri.Hunk_FreeTempMemory = Hunk_FreeTempMemory; + ri.CM_DrawDebugSurface = CM_DrawDebugSurface; + ri.FS_ReadFile = FS_ReadFile; + ri.FS_FreeFile = FS_FreeFile; + ri.FS_WriteFile = FS_WriteFile; + ri.FS_FreeFileList = FS_FreeFileList; + ri.FS_ListFiles = FS_ListFiles; + ri.FS_FileIsInPAK = FS_FileIsInPAK; + ri.FS_FileExists = FS_FileExists; + ri.Cvar_Get = Cvar_Get; + ri.Cvar_Set = Cvar_Set; + + // cinematic stuff + + ri.CIN_UploadCinematic = CIN_UploadCinematic; + ri.CIN_PlayCinematic = CIN_PlayCinematic; + ri.CIN_RunCinematic = CIN_RunCinematic; + + ret = GetRefAPI( REF_API_VERSION, &ri ); + +#if 0 // MrE defined __USEA3D && defined __A3D_GEOM + hA3Dg_ExportRenderGeom( ret ); +#endif + + Com_Printf( "-------------------------------\n" ); + + if ( !ret ) { + Com_Error( ERR_FATAL, "Couldn't initialize refresh" ); + } + + re = *ret; + + // unpause so the cgame definately gets a snapshot and renders a frame + Cvar_Set( "cl_paused", "0" ); +} + +// RF, trap manual client damage commands so users can't issue them manually +void CL_ClientDamageCommand( void ) { + // do nothing +} + +// NERVE - SMF +void CL_startMultiplayer_f( void ) { +#ifdef __MACOS__ //DAJ + Sys_StartProcess( "Wolfenstein MP", qtrue ); +#elif defined( __linux__ ) + Sys_StartProcess( "./wolf.x86", qtrue ); +#else + Sys_StartProcess( "WolfMP.exe", qtrue ); +#endif +} +// -NERVE - SMF + +//----(SA) added +/* +============== +CL_ShellExecute_URL_f +Format: + shellExecute "open" + +TTimo + show_bug.cgi?id=447 + only supporting "open" syntax for URL openings, others are not portable or need to be added on a case-by-case basis + the shellExecute syntax as been kept to remain compatible with win32 SP demo pk3, but this thing only does open URL + +============== +*/ + +void CL_ShellExecute_URL_f( void ) { + qboolean doexit; + + Com_DPrintf( "CL_ShellExecute_URL_f\n" ); + + if ( Q_stricmp( Cmd_Argv( 1 ),"open" ) ) { + Com_DPrintf( "invalid CL_ShellExecute_URL_f syntax (shellExecute \"open\" )\n" ); + return; + } + + if ( Cmd_Argc() < 4 ) { + doexit = qtrue; + } else { + doexit = (qboolean)( atoi( Cmd_Argv( 3 ) ) ); + } + + Sys_OpenURL( Cmd_Argv( 2 ),doexit ); +} +//----(SA) end +//=========================================================================================== + +/* +==================== +CL_Init +==================== +*/ +void CL_Init( void ) { + Com_Printf( "----- Client Initialization -----\n" ); + + Con_Init(); + + CL_ClearState(); + + cls.state = CA_DISCONNECTED; // no longer CA_UNINITIALIZED + + cls.realtime = 0; + + CL_InitInput(); + + // + // register our variables + // + cl_noprint = Cvar_Get( "cl_noprint", "0", 0 ); + cl_motd = Cvar_Get( "cl_motd", "1", 0 ); + + cl_timeout = Cvar_Get( "cl_timeout", "200", 0 ); + + cl_timeNudge = Cvar_Get( "cl_timeNudge", "0", CVAR_TEMP ); + cl_shownet = Cvar_Get( "cl_shownet", "0", CVAR_TEMP ); + cl_showSend = Cvar_Get( "cl_showSend", "0", CVAR_TEMP ); + cl_showTimeDelta = Cvar_Get( "cl_showTimeDelta", "0", CVAR_TEMP ); + cl_freezeDemo = Cvar_Get( "cl_freezeDemo", "0", CVAR_TEMP ); + rcon_client_password = Cvar_Get( "rconPassword", "", CVAR_TEMP ); + cl_activeAction = Cvar_Get( "activeAction", "", CVAR_TEMP ); + + cl_timedemo = Cvar_Get( "timedemo", "0", 0 ); + cl_avidemo = Cvar_Get( "cl_avidemo", "0", 0 ); + cl_forceavidemo = Cvar_Get( "cl_forceavidemo", "0", 0 ); + + rconAddress = Cvar_Get( "rconAddress", "", 0 ); + + cl_yawspeed = Cvar_Get( "cl_yawspeed", "140", CVAR_ARCHIVE ); + cl_pitchspeed = Cvar_Get( "cl_pitchspeed", "140", CVAR_ARCHIVE ); + cl_anglespeedkey = Cvar_Get( "cl_anglespeedkey", "1.5", 0 ); + + cl_maxpackets = Cvar_Get( "cl_maxpackets", "30", CVAR_ARCHIVE ); + cl_packetdup = Cvar_Get( "cl_packetdup", "1", CVAR_ARCHIVE ); + + cl_run = Cvar_Get( "cl_run", "1", CVAR_ARCHIVE ); + cl_sensitivity = Cvar_Get( "sensitivity", "5", CVAR_ARCHIVE ); + cl_mouseAccel = Cvar_Get( "cl_mouseAccel", "0", CVAR_ARCHIVE ); + cl_freelook = Cvar_Get( "cl_freelook", "1", CVAR_ARCHIVE ); + + cl_showMouseRate = Cvar_Get( "cl_showmouserate", "0", 0 ); + + cl_allowDownload = Cvar_Get( "cl_allowDownload", "0", CVAR_ARCHIVE ); + + // init autoswitch so the ui will have it correctly even + // if the cgame hasn't been started + Cvar_Get( "cg_autoswitch", "2", CVAR_ARCHIVE ); + + // Rafael - particle switch + Cvar_Get( "cg_wolfparticles", "1", CVAR_ARCHIVE ); + // done + + cl_conXOffset = Cvar_Get( "cl_conXOffset", "0", 0 ); + cl_inGameVideo = Cvar_Get( "r_inGameVideo", "1", CVAR_ARCHIVE ); + + cl_serverStatusResendTime = Cvar_Get( "cl_serverStatusResendTime", "750", 0 ); + + // RF + cl_recoilPitch = Cvar_Get( "cg_recoilPitch", "0", CVAR_ROM ); + + m_pitch = Cvar_Get( "m_pitch", "0.022", CVAR_ARCHIVE ); + m_yaw = Cvar_Get( "m_yaw", "0.022", CVAR_ARCHIVE ); + m_forward = Cvar_Get( "m_forward", "0.25", CVAR_ARCHIVE ); + m_side = Cvar_Get( "m_side", "0.25", CVAR_ARCHIVE ); + m_filter = Cvar_Get( "m_filter", "0", CVAR_ARCHIVE ); + + cl_motdString = Cvar_Get( "cl_motdString", "", CVAR_ROM ); + + Cvar_Get( "cl_maxPing", "800", CVAR_ARCHIVE ); + + // userinfo + Cvar_Get( "name", "Player", CVAR_USERINFO | CVAR_ARCHIVE ); + Cvar_Get( "rate", "3000", CVAR_USERINFO | CVAR_ARCHIVE ); + Cvar_Get( "snaps", "20", CVAR_USERINFO | CVAR_ARCHIVE ); + Cvar_Get( "model", "bj2", CVAR_USERINFO | CVAR_ARCHIVE ); // temp until we have an skeletal american model + Cvar_Get( "head", "default", CVAR_USERINFO | CVAR_ARCHIVE ); + Cvar_Get( "color", "4", CVAR_USERINFO | CVAR_ARCHIVE ); + Cvar_Get( "handicap", "100", CVAR_USERINFO | CVAR_ARCHIVE ); + Cvar_Get( "sex", "male", CVAR_USERINFO | CVAR_ARCHIVE ); + Cvar_Get( "cl_anonymous", "0", CVAR_USERINFO | CVAR_ARCHIVE ); + + Cvar_Get( "password", "", CVAR_USERINFO ); + Cvar_Get( "cg_predictItems", "1", CVAR_USERINFO | CVAR_ARCHIVE ); + +//----(SA) added + Cvar_Get( "cg_autoactivate", "1", CVAR_USERINFO | CVAR_ARCHIVE ); + Cvar_Get( "cg_emptyswitch", "0", CVAR_USERINFO | CVAR_ARCHIVE ); +//----(SA) end + + // cgame might not be initialized before menu is used + Cvar_Get( "cg_viewsize", "100", CVAR_ARCHIVE ); + + cl_missionStats = Cvar_Get( "g_missionStats", "0", CVAR_ROM ); + cl_waitForFire = Cvar_Get( "cl_waitForFire", "0", CVAR_ROM ); + + // NERVE - SMF - localization + cl_language = Cvar_Get( "cl_language", "0", CVAR_ARCHIVE ); + cl_debugTranslation = Cvar_Get( "cl_debugTranslation", "0", 0 ); + // -NERVE - SMF + + // + // register our commands + // + Cmd_AddCommand( "cmd", CL_ForwardToServer_f ); + Cmd_AddCommand( "configstrings", CL_Configstrings_f ); + Cmd_AddCommand( "clientinfo", CL_Clientinfo_f ); + Cmd_AddCommand( "snd_restart", CL_Snd_Restart_f ); + Cmd_AddCommand( "vid_restart", CL_Vid_Restart_f ); + Cmd_AddCommand( "disconnect", CL_Disconnect_f ); + Cmd_AddCommand( "record", CL_Record_f ); + Cmd_AddCommand( "demo", CL_PlayDemo_f ); + Cmd_AddCommand( "cinematic", CL_PlayCinematic_f ); + Cmd_AddCommand( "stoprecord", CL_StopRecord_f ); + Cmd_AddCommand( "connect", CL_Connect_f ); + Cmd_AddCommand( "reconnect", CL_Reconnect_f ); + Cmd_AddCommand( "localservers", CL_LocalServers_f ); + Cmd_AddCommand( "globalservers", CL_GlobalServers_f ); + Cmd_AddCommand( "rcon", CL_Rcon_f ); + Cmd_AddCommand( "setenv", CL_Setenv_f ); + Cmd_AddCommand( "ping", CL_Ping_f ); + Cmd_AddCommand( "serverstatus", CL_ServerStatus_f ); + Cmd_AddCommand( "showip", CL_ShowIP_f ); + Cmd_AddCommand( "fs_openedList", CL_OpenedPK3List_f ); + Cmd_AddCommand( "fs_referencedList", CL_ReferencedPK3List_f ); + + // Ridah, startup-caching system + Cmd_AddCommand( "cache_startgather", CL_Cache_StartGather_f ); + Cmd_AddCommand( "cache_usedfile", CL_Cache_UsedFile_f ); + Cmd_AddCommand( "cache_setindex", CL_Cache_SetIndex_f ); + Cmd_AddCommand( "cache_mapchange", CL_Cache_MapChange_f ); + Cmd_AddCommand( "cache_endgather", CL_Cache_EndGather_f ); + + Cmd_AddCommand( "updatehunkusage", CL_UpdateLevelHunkUsage ); + Cmd_AddCommand( "updatescreen", SCR_UpdateScreen ); + // done. + + // RF, add this command so clients can't bind a key to send client damage commands to the server + Cmd_AddCommand( "cld", CL_ClientDamageCommand ); + + Cmd_AddCommand( "startMultiplayer", CL_startMultiplayer_f ); // NERVE - SMF + + // TTimo + // show_bug.cgi?id=447 + Cmd_AddCommand( "shellExecute", CL_ShellExecute_URL_f ); + //Cmd_AddCommand ( "shellExecute", CL_ShellExecute_f ); //----(SA) added (mainly for opening web pages from the menu) + + // RF, prevent users from issuing a map_restart manually + Cmd_AddCommand( "map_restart", CL_MapRestart_f ); + + Cmd_AddCommand( "setRecommended", CL_SetRecommended_f ); + + CL_InitRef(); + + SCR_Init(); + + Cbuf_Execute(); + + Cvar_Set( "cl_running", "1" ); + + Com_Printf( "----- Client Initialization Complete -----\n" ); +} + + +/* +=============== +CL_Shutdown + +=============== +*/ +void CL_Shutdown( void ) { + static qboolean recursive = qfalse; + + Com_Printf( "----- CL_Shutdown -----\n" ); + + if ( recursive ) { + printf( "recursive shutdown\n" ); + return; + } + recursive = qtrue; + + CL_Disconnect( qtrue ); + + S_Shutdown(); + CL_ShutdownRef(); + + CL_ShutdownUI(); + + Cmd_RemoveCommand( "cmd" ); + Cmd_RemoveCommand( "configstrings" ); + Cmd_RemoveCommand( "userinfo" ); + Cmd_RemoveCommand( "snd_restart" ); + Cmd_RemoveCommand( "vid_restart" ); + Cmd_RemoveCommand( "disconnect" ); + Cmd_RemoveCommand( "record" ); + Cmd_RemoveCommand( "demo" ); + Cmd_RemoveCommand( "cinematic" ); + Cmd_RemoveCommand( "stoprecord" ); + Cmd_RemoveCommand( "connect" ); + Cmd_RemoveCommand( "localservers" ); + Cmd_RemoveCommand( "globalservers" ); + Cmd_RemoveCommand( "rcon" ); + Cmd_RemoveCommand( "setenv" ); + Cmd_RemoveCommand( "ping" ); + Cmd_RemoveCommand( "serverstatus" ); + Cmd_RemoveCommand( "showip" ); + Cmd_RemoveCommand( "model" ); + + // Ridah, startup-caching system + Cmd_RemoveCommand( "cache_startgather" ); + Cmd_RemoveCommand( "cache_usedfile" ); + Cmd_RemoveCommand( "cache_setindex" ); + Cmd_RemoveCommand( "cache_mapchange" ); + Cmd_RemoveCommand( "cache_endgather" ); + + Cmd_RemoveCommand( "updatehunkusage" ); + // done. + + Cvar_Set( "cl_running", "0" ); + + recursive = qfalse; + + memset( &cls, 0, sizeof( cls ) ); + + Com_Printf( "-----------------------\n" ); +} + + +static void CL_SetServerInfo( serverInfo_t *server, const char *info, int ping ) { + if ( server ) { + if ( info ) { + server->clients = atoi( Info_ValueForKey( info, "clients" ) ); + Q_strncpyz( server->hostName,Info_ValueForKey( info, "hostname" ), MAX_NAME_LENGTH ); + Q_strncpyz( server->mapName, Info_ValueForKey( info, "mapname" ), MAX_NAME_LENGTH ); + server->maxClients = atoi( Info_ValueForKey( info, "sv_maxclients" ) ); + Q_strncpyz( server->game,Info_ValueForKey( info, "game" ), MAX_NAME_LENGTH ); + server->gameType = atoi( Info_ValueForKey( info, "gametype" ) ); + server->netType = atoi( Info_ValueForKey( info, "nettype" ) ); + server->minPing = atoi( Info_ValueForKey( info, "minping" ) ); + server->maxPing = atoi( Info_ValueForKey( info, "maxping" ) ); + server->allowAnonymous = atoi( Info_ValueForKey( info, "sv_allowAnonymous" ) ); + } + server->ping = ping; + } +} + +static void CL_SetServerInfoByAddress( netadr_t from, const char *info, int ping ) { + int i; + + for ( i = 0; i < MAX_OTHER_SERVERS; i++ ) { + if ( NET_CompareAdr( from, cls.localServers[i].adr ) ) { + CL_SetServerInfo( &cls.localServers[i], info, ping ); + } + } + + for ( i = 0; i < MAX_OTHER_SERVERS; i++ ) { + if ( NET_CompareAdr( from, cls.mplayerServers[i].adr ) ) { + CL_SetServerInfo( &cls.mplayerServers[i], info, ping ); + } + } + + for ( i = 0; i < MAX_GLOBAL_SERVERS; i++ ) { + if ( NET_CompareAdr( from, cls.globalServers[i].adr ) ) { + CL_SetServerInfo( &cls.globalServers[i], info, ping ); + } + } + + for ( i = 0; i < MAX_OTHER_SERVERS; i++ ) { + if ( NET_CompareAdr( from, cls.favoriteServers[i].adr ) ) { + CL_SetServerInfo( &cls.favoriteServers[i], info, ping ); + } + } + +} + +/* +=================== +CL_ServerInfoPacket +=================== +*/ +void CL_ServerInfoPacket( netadr_t from, msg_t *msg ) { + int i, type; + char info[MAX_INFO_STRING]; + char* str; + char *infoString; + int prot; + + infoString = MSG_ReadString( msg ); + + // if this isn't the correct protocol version, ignore it + prot = atoi( Info_ValueForKey( infoString, "protocol" ) ); + if ( prot != PROTOCOL_VERSION ) { + Com_DPrintf( "Different protocol info packet: %s\n", infoString ); +// return; + } + + // iterate servers waiting for ping response + for ( i = 0; i < MAX_PINGREQUESTS; i++ ) + { + if ( cl_pinglist[i].adr.port && !cl_pinglist[i].time && NET_CompareAdr( from, cl_pinglist[i].adr ) ) { + // calc ping time + cl_pinglist[i].time = cls.realtime - cl_pinglist[i].start + 1; + Com_DPrintf( "ping time %dms from %s\n", cl_pinglist[i].time, NET_AdrToString( from ) ); + + // save of info + Q_strncpyz( cl_pinglist[i].info, infoString, sizeof( cl_pinglist[i].info ) ); + + // tack on the net type + // NOTE: make sure these types are in sync with the netnames strings in the UI + switch ( from.type ) + { + case NA_BROADCAST: + case NA_IP: + str = "udp"; + type = 1; + break; + + case NA_IPX: + case NA_BROADCAST_IPX: + str = "ipx"; + type = 2; + break; + + default: + str = "???"; + type = 0; + break; + } + Info_SetValueForKey( cl_pinglist[i].info, "nettype", va( "%d", type ) ); + CL_SetServerInfoByAddress( from, infoString, cl_pinglist[i].time ); + + return; + } + } + + // if not just sent a local broadcast or pinging local servers + if ( cls.pingUpdateSource != AS_LOCAL ) { + return; + } + + for ( i = 0 ; i < MAX_OTHER_SERVERS ; i++ ) { + // empty slot + if ( cls.localServers[i].adr.port == 0 ) { + break; + } + + // avoid duplicate + if ( NET_CompareAdr( from, cls.localServers[i].adr ) ) { + return; + } + } + + if ( i == MAX_OTHER_SERVERS ) { + Com_DPrintf( "MAX_OTHER_SERVERS hit, dropping infoResponse\n" ); + return; + } + + // add this to the list + cls.numlocalservers = i + 1; + cls.localServers[i].adr = from; + cls.localServers[i].clients = 0; + cls.localServers[i].hostName[0] = '\0'; + cls.localServers[i].mapName[0] = '\0'; + cls.localServers[i].maxClients = 0; + cls.localServers[i].maxPing = 0; + cls.localServers[i].minPing = 0; + cls.localServers[i].ping = -1; + cls.localServers[i].game[0] = '\0'; + cls.localServers[i].gameType = 0; + cls.localServers[i].netType = from.type; + cls.localServers[i].allowAnonymous = 0; + + Q_strncpyz( info, MSG_ReadString( msg ), MAX_INFO_STRING ); + if ( strlen( info ) ) { + if ( info[strlen( info ) - 1] != '\n' ) { + strncat( info, "\n", sizeof( info ) ); + } + Com_Printf( "%s: %s", NET_AdrToString( from ), info ); + } +} + +/* +=================== +CL_GetServerStatus +=================== +*/ +serverStatus_t *CL_GetServerStatus( netadr_t from ) { + serverStatus_t *serverStatus; + int i, oldest, oldestTime; + + serverStatus = NULL; + for ( i = 0; i < MAX_SERVERSTATUSREQUESTS; i++ ) { + if ( NET_CompareAdr( from, cl_serverStatusList[i].address ) ) { + return &cl_serverStatusList[i]; + } + } + for ( i = 0; i < MAX_SERVERSTATUSREQUESTS; i++ ) { + if ( cl_serverStatusList[i].retrieved ) { + return &cl_serverStatusList[i]; + } + } + oldest = -1; + oldestTime = 0; + for ( i = 0; i < MAX_SERVERSTATUSREQUESTS; i++ ) { + if ( oldest == -1 || cl_serverStatusList[i].startTime < oldestTime ) { + oldest = i; + oldestTime = cl_serverStatusList[i].startTime; + } + } + if ( oldest != -1 ) { + return &cl_serverStatusList[oldest]; + } + serverStatusCount++; + return &cl_serverStatusList[serverStatusCount & ( MAX_SERVERSTATUSREQUESTS - 1 )]; +} + +/* +=================== +CL_ServerStatus +=================== +*/ +int CL_ServerStatus( char *serverAddress, char *serverStatusString, int maxLen ) { + int i; + netadr_t to; + serverStatus_t *serverStatus; + + // if no server address then reset all server status requests + if ( !serverAddress ) { + for ( i = 0; i < MAX_SERVERSTATUSREQUESTS; i++ ) { + cl_serverStatusList[i].address.port = 0; + cl_serverStatusList[i].retrieved = qtrue; + } + return qfalse; + } + // get the address + if ( !NET_StringToAdr( serverAddress, &to ) ) { + return qfalse; + } + serverStatus = CL_GetServerStatus( to ); + // if no server status string then reset the server status request for this address + if ( !serverStatusString ) { + serverStatus->retrieved = qtrue; + return qfalse; + } + + // if this server status request has the same address + if ( NET_CompareAdr( to, serverStatus->address ) ) { + // if we recieved an response for this server status request + if ( !serverStatus->pending ) { + Q_strncpyz( serverStatusString, serverStatus->string, maxLen ); + serverStatus->retrieved = qtrue; + serverStatus->startTime = 0; + return qtrue; + } + // resend the request regularly + else if ( serverStatus->startTime < Sys_Milliseconds() - cl_serverStatusResendTime->integer ) { + serverStatus->print = qfalse; + serverStatus->pending = qtrue; + serverStatus->retrieved = qfalse; + serverStatus->time = 0; + serverStatus->startTime = Sys_Milliseconds(); + NET_OutOfBandPrint( NS_CLIENT, to, "getstatus" ); + return qfalse; + } + } + // if retrieved + else if ( serverStatus->retrieved ) { + serverStatus->address = to; + serverStatus->print = qfalse; + serverStatus->pending = qtrue; + serverStatus->retrieved = qfalse; + serverStatus->startTime = Sys_Milliseconds(); + serverStatus->time = 0; + NET_OutOfBandPrint( NS_CLIENT, to, "getstatus" ); + return qfalse; + } + return qfalse; +} + +/* +=================== +CL_ServerStatusResponse +=================== +*/ +void CL_ServerStatusResponse( netadr_t from, msg_t *msg ) { + char *s; + char info[MAX_INFO_STRING]; + int i, l, score, ping; + int len; + serverStatus_t *serverStatus; + + serverStatus = NULL; + for ( i = 0; i < MAX_SERVERSTATUSREQUESTS; i++ ) { + if ( NET_CompareAdr( from, cl_serverStatusList[i].address ) ) { + serverStatus = &cl_serverStatusList[i]; + break; + } + } + // if we didn't request this server status + if ( !serverStatus ) { + return; + } + + s = MSG_ReadStringLine( msg ); + + len = 0; + Com_sprintf( &serverStatus->string[len], sizeof( serverStatus->string ) - len, "%s", s ); + + if ( serverStatus->print ) { + Com_Printf( "Server settings:\n" ); + // print cvars + while ( *s ) { + for ( i = 0; i < 2 && *s; i++ ) { + if ( *s == '\\' ) { + s++; + } + l = 0; + while ( *s ) { + info[l++] = *s; + if ( l >= MAX_INFO_STRING - 1 ) { + break; + } + s++; + if ( *s == '\\' ) { + break; + } + } + info[l] = '\0'; + if ( i ) { + Com_Printf( "%s\n", info ); + } else { + Com_Printf( "%-24s", info ); + } + } + } + } + + len = strlen( serverStatus->string ); + Com_sprintf( &serverStatus->string[len], sizeof( serverStatus->string ) - len, "\\" ); + + if ( serverStatus->print ) { + Com_Printf( "\nPlayers:\n" ); + Com_Printf( "num: score: ping: name:\n" ); + } + for ( i = 0, s = MSG_ReadStringLine( msg ); *s; s = MSG_ReadStringLine( msg ), i++ ) { + + len = strlen( serverStatus->string ); + Com_sprintf( &serverStatus->string[len], sizeof( serverStatus->string ) - len, "\\%s", s ); + + if ( serverStatus->print ) { + score = ping = 0; + sscanf( s, "%d %d", &score, &ping ); + s = strchr( s, ' ' ); + if ( s ) { + s = strchr( s + 1, ' ' ); + } + if ( s ) { + s++; + } else { + s = "unknown"; + } + Com_Printf( "%-2d %-3d %-3d %s\n", i, score, ping, s ); + } + } + len = strlen( serverStatus->string ); + Com_sprintf( &serverStatus->string[len], sizeof( serverStatus->string ) - len, "\\" ); + + serverStatus->time = Sys_Milliseconds(); + serverStatus->address = from; + serverStatus->pending = qfalse; + if ( serverStatus->print ) { + serverStatus->retrieved = qtrue; + } +} + +/* +================== +CL_LocalServers_f +================== +*/ +void CL_LocalServers_f( void ) { + char *message; + int i, j; + netadr_t to; + + Com_Printf( "Scanning for servers on the local network...\n" ); + + // reset the list, waiting for response + cls.numlocalservers = 0; + cls.pingUpdateSource = AS_LOCAL; + + for ( i = 0; i < MAX_OTHER_SERVERS; i++ ) { + qboolean b = cls.localServers[i].visible; + Com_Memset( &cls.localServers[i], 0, sizeof( cls.localServers[i] ) ); + cls.localServers[i].visible = b; + } + Com_Memset( &to, 0, sizeof( to ) ); + + // The 'xxx' in the message is a challenge that will be echoed back + // by the server. We don't care about that here, but master servers + // can use that to prevent spoofed server responses from invalid ip + message = "\377\377\377\377getinfo xxx"; + + // send each message twice in case one is dropped + for ( i = 0 ; i < 2 ; i++ ) { + // send a broadcast packet on each server port + // we support multiple server ports so a single machine + // can nicely run multiple servers + for ( j = 0 ; j < NUM_SERVER_PORTS ; j++ ) { + to.port = BigShort( (short)( PORT_SERVER + j ) ); + + to.type = NA_BROADCAST; + NET_SendPacket( NS_CLIENT, strlen( message ), message, to ); + + to.type = NA_BROADCAST_IPX; + NET_SendPacket( NS_CLIENT, strlen( message ), message, to ); + } + } +} + +/* +================== +CL_GlobalServers_f +================== +*/ +void CL_GlobalServers_f( void ) { + netadr_t to; + int i; + int count; + char *buffptr; + char command[1024]; + + if ( Cmd_Argc() < 3 ) { + Com_Printf( "usage: globalservers [keywords]\n" ); + return; + } + + cls.masterNum = atoi( Cmd_Argv( 1 ) ); + + Com_Printf( "Requesting servers from the master...\n" ); + + // reset the list, waiting for response + // -1 is used to distinguish a "no response" + + if ( cls.masterNum == 1 ) { + NET_StringToAdr( "master.quake3world.com", &to ); + cls.nummplayerservers = -1; + cls.pingUpdateSource = AS_MPLAYER; + } else { + NET_StringToAdr( MASTER_SERVER_NAME, &to ); + cls.numglobalservers = -1; + cls.pingUpdateSource = AS_GLOBAL; + } + to.type = NA_IP; + to.port = BigShort( PORT_MASTER ); + + sprintf( command, "getservers %s", Cmd_Argv( 2 ) ); + + // tack on keywords + buffptr = command + strlen( command ); + count = Cmd_Argc(); + for ( i = 3; i < count; i++ ) + buffptr += sprintf( buffptr, " %s", Cmd_Argv( i ) ); + + // if we are a demo, automatically add a "demo" keyword + if ( Cvar_VariableValue( "fs_restrict" ) ) { + buffptr += sprintf( buffptr, " demo" ); + } + + NET_OutOfBandPrint( NS_SERVER, to, command ); +} + + +/* +================== +CL_GetPing +================== +*/ +void CL_GetPing( int n, char *buf, int buflen, int *pingtime ) { + const char *str; + int time; + int maxPing; + + if ( !cl_pinglist[n].adr.port ) { + // empty slot + buf[0] = '\0'; + *pingtime = 0; + return; + } + + str = NET_AdrToString( cl_pinglist[n].adr ); + Q_strncpyz( buf, str, buflen ); + + time = cl_pinglist[n].time; + if ( !time ) { + // check for timeout + time = cls.realtime - cl_pinglist[n].start; + maxPing = Cvar_VariableIntegerValue( "cl_maxPing" ); + if ( maxPing < 100 ) { + maxPing = 100; + } + if ( time < maxPing ) { + // not timed out yet + time = 0; + } + } + + CL_SetServerInfoByAddress( cl_pinglist[n].adr, cl_pinglist[n].info, cl_pinglist[n].time ); + + *pingtime = time; +} + +/* +================== +CL_UpdateServerInfo +================== +*/ +void CL_UpdateServerInfo( int n ) { + if ( !cl_pinglist[n].adr.port ) { + return; + } + + CL_SetServerInfoByAddress( cl_pinglist[n].adr, cl_pinglist[n].info, cl_pinglist[n].time ); +} + +/* +================== +CL_GetPingInfo +================== +*/ +void CL_GetPingInfo( int n, char *buf, int buflen ) { + if ( !cl_pinglist[n].adr.port ) { + // empty slot + if ( buflen ) { + buf[0] = '\0'; + } + return; + } + + Q_strncpyz( buf, cl_pinglist[n].info, buflen ); +} + +/* +================== +CL_ClearPing +================== +*/ +void CL_ClearPing( int n ) { + if ( n < 0 || n >= MAX_PINGREQUESTS ) { + return; + } + + cl_pinglist[n].adr.port = 0; +} + +/* +================== +CL_GetPingQueueCount +================== +*/ +int CL_GetPingQueueCount( void ) { + int i; + int count; + ping_t* pingptr; + + count = 0; + pingptr = cl_pinglist; + + for ( i = 0; i < MAX_PINGREQUESTS; i++, pingptr++ ) { + if ( pingptr->adr.port ) { + count++; + } + } + + return ( count ); +} + +/* +================== +CL_GetFreePing +================== +*/ +ping_t* CL_GetFreePing( void ) { + ping_t* pingptr; + ping_t* best; + int oldest; + int i; + int time; + + pingptr = cl_pinglist; + for ( i = 0; i < MAX_PINGREQUESTS; i++, pingptr++ ) + { + // find free ping slot + if ( pingptr->adr.port ) { + if ( !pingptr->time ) { + if ( cls.realtime - pingptr->start < 500 ) { + // still waiting for response + continue; + } + } else if ( pingptr->time < 500 ) { + // results have not been queried + continue; + } + } + + // clear it + pingptr->adr.port = 0; + return ( pingptr ); + } + + // use oldest entry + pingptr = cl_pinglist; + best = cl_pinglist; + oldest = INT_MIN; + for ( i = 0; i < MAX_PINGREQUESTS; i++, pingptr++ ) + { + // scan for oldest + time = cls.realtime - pingptr->start; + if ( time > oldest ) { + oldest = time; + best = pingptr; + } + } + + return ( best ); +} + +/* +================== +CL_Ping_f +================== +*/ +void CL_Ping_f( void ) { + netadr_t to; + ping_t* pingptr; + char* server; + + if ( Cmd_Argc() != 2 ) { + Com_Printf( "usage: ping [server]\n" ); + return; + } + + memset( &to, 0, sizeof( netadr_t ) ); + + server = Cmd_Argv( 1 ); + + if ( !NET_StringToAdr( server, &to ) ) { + return; + } + + pingptr = CL_GetFreePing(); + + memcpy( &pingptr->adr, &to, sizeof( netadr_t ) ); + pingptr->start = cls.realtime; + pingptr->time = 0; + + CL_SetServerInfoByAddress( pingptr->adr, NULL, 0 ); + + NET_OutOfBandPrint( NS_CLIENT, to, "getinfo xxx" ); +} + +/* +================== +CL_UpdateVisiblePings_f +================== +*/ +qboolean CL_UpdateVisiblePings_f( int source ) { + int slots, i; + char buff[MAX_STRING_CHARS]; + int pingTime; + int max; + qboolean status = qfalse; + + if ( source < 0 || source > AS_FAVORITES ) { + return qfalse; + } + + cls.pingUpdateSource = source; + + slots = CL_GetPingQueueCount(); + if ( slots < MAX_PINGREQUESTS ) { + serverInfo_t *server = NULL; + + max = ( source == AS_GLOBAL ) ? MAX_GLOBAL_SERVERS : MAX_OTHER_SERVERS; + switch ( source ) { + case AS_LOCAL: + server = &cls.localServers[0]; + max = cls.numlocalservers; + break; + case AS_MPLAYER: + server = &cls.mplayerServers[0]; + max = cls.nummplayerservers; + break; + case AS_GLOBAL: + server = &cls.globalServers[0]; + max = cls.numglobalservers; + break; + case AS_FAVORITES: + server = &cls.favoriteServers[0]; + max = cls.numfavoriteservers; + break; + } + for ( i = 0; i < max; i++ ) { + if ( server[i].visible ) { + if ( server[i].ping == -1 ) { + int j; + + if ( slots >= MAX_PINGREQUESTS ) { + break; + } + for ( j = 0; j < MAX_PINGREQUESTS; j++ ) { + if ( !cl_pinglist[j].adr.port ) { + continue; + } + if ( NET_CompareAdr( cl_pinglist[j].adr, server[i].adr ) ) { + // already on the list + break; + } + } + if ( j >= MAX_PINGREQUESTS ) { + status = qtrue; + for ( j = 0; j < MAX_PINGREQUESTS; j++ ) { + if ( !cl_pinglist[j].adr.port ) { + break; + } + } + memcpy( &cl_pinglist[j].adr, &server[i].adr, sizeof( netadr_t ) ); + cl_pinglist[j].start = cls.realtime; + cl_pinglist[j].time = 0; + NET_OutOfBandPrint( NS_CLIENT, cl_pinglist[j].adr, "getinfo xxx" ); + slots++; + } + } + // if the server has a ping higher than cl_maxPing or + // the ping packet got lost + else if ( server[i].ping == 0 ) { + // if we are updating global servers + if ( source == AS_GLOBAL ) { + // + if ( cls.numGlobalServerAddresses > 0 ) { + // overwrite this server with one from the additional global servers + cls.numGlobalServerAddresses--; + CL_InitServerInfo( &server[i], &cls.globalServerAddresses[cls.numGlobalServerAddresses] ); + // NOTE: the server[i].visible flag stays untouched + } + } + } + } + } + } + + if ( slots ) { + status = qtrue; + } + for ( i = 0; i < MAX_PINGREQUESTS; i++ ) { + if ( !cl_pinglist[i].adr.port ) { + continue; + } + CL_GetPing( i, buff, MAX_STRING_CHARS, &pingTime ); + if ( pingTime != 0 ) { + CL_ClearPing( i ); + status = qtrue; + } + } + + return status; +} + +/* +================== +CL_ServerStatus_f +================== +*/ +void CL_ServerStatus_f( void ) { + netadr_t to; + char *server; + serverStatus_t *serverStatus; + + Com_Memset( &to, 0, sizeof( netadr_t ) ); + + if ( Cmd_Argc() != 2 ) { + if ( cls.state != CA_ACTIVE || clc.demoplaying ) { + Com_Printf( "Not connected to a server.\n" ); + Com_Printf( "Usage: serverstatus [server]\n" ); + return; + } + server = cls.servername; + } else { + server = Cmd_Argv( 1 ); + } + + if ( !NET_StringToAdr( server, &to ) ) { + return; + } + + NET_OutOfBandPrint( NS_CLIENT, to, "getstatus" ); + + serverStatus = CL_GetServerStatus( to ); + serverStatus->address = to; + serverStatus->print = qtrue; + serverStatus->pending = qtrue; +} + +/* +================== +CL_ShowIP_f +================== +*/ +void CL_ShowIP_f( void ) { + Sys_ShowIP(); +} + +/* +================= +bool CL_CDKeyValidate +================= +*/ +qboolean CL_CDKeyValidate( const char *key, const char *checksum ) { + char ch; + byte sum; + char chs[3]; + int i, len; + + len = strlen( key ); + if ( len != CDKEY_LEN ) { + return qfalse; + } + + if ( checksum && strlen( checksum ) != CDCHKSUM_LEN ) { + return qfalse; + } + + sum = 0; + // for loop gets rid of conditional assignment warning + for ( i = 0; i < len; i++ ) { + ch = *key++; + if ( ch >= 'a' && ch <= 'z' ) { + ch -= 32; + } + switch ( ch ) { + case '2': + case '3': + case '7': + case 'A': + case 'B': + case 'C': + case 'D': + case 'G': + case 'H': + case 'J': + case 'L': + case 'P': + case 'R': + case 'S': + case 'T': + case 'W': + sum += ch; + continue; + default: + return qfalse; + } + } + + + sprintf( chs, "%02x", sum ); + + if ( checksum && !Q_stricmp( chs, checksum ) ) { + return qtrue; + } + + if ( !checksum ) { + return qtrue; + } + + return qfalse; +} + +// NERVE - SMF +/* +======================= +CL_AddToLimboChat + +======================= +*/ +void CL_AddToLimboChat( const char *str ) { + int len; + char *p, *ls; + int lastcolor; + int chatHeight; + int i; + + chatHeight = LIMBOCHAT_HEIGHT; + cl.limboChatPos = LIMBOCHAT_HEIGHT - 1; + len = 0; + + // copy old strings + for ( i = cl.limboChatPos; i > 0; i-- ) { + strcpy( cl.limboChatMsgs[i], cl.limboChatMsgs[i - 1] ); + } + + // copy new string + p = cl.limboChatMsgs[0]; + *p = 0; + + lastcolor = '7'; + + ls = NULL; + while ( *str ) { + if ( len > LIMBOCHAT_WIDTH - 1 ) { +#if 0 + if ( ls ) { + str -= ( p - ls ); + str++; + p -= ( p - ls ); + } + *p = 0; + + if ( cl.limboChatPos < LIMBOCHAT_HEIGHT - 1 ) { + cl.limboChatPos++; + } + p = cl.limboChatMsgs[cl.limboChatPos]; + *p = 0; + *p++ = Q_COLOR_ESCAPE; + *p++ = lastcolor; + len = 0; + ls = NULL; +#endif + break; + } + + if ( Q_IsColorString( str ) ) { + *p++ = *str++; + lastcolor = *str; + *p++ = *str++; + continue; + } + if ( *str == ' ' ) { + ls = p; + } + *p++ = *str++; + len++; + } + *p = 0; +} + +/* +======================= +CL_GetLimboString + +======================= +*/ +qboolean CL_GetLimboString( int index, char *buf ) { + if ( index >= LIMBOCHAT_HEIGHT ) { + return qfalse; + } + + strncpy( buf, cl.limboChatMsgs[index], 140 ); + return qtrue; +} +// -NERVE - SMF diff --git a/src/client/cl_net_chan.c b/src/client/cl_net_chan.c new file mode 100644 index 0000000..cd30b7e --- /dev/null +++ b/src/client/cl_net_chan.c @@ -0,0 +1,197 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +#include "../game/q_shared.h" +#include "../qcommon/qcommon.h" +#include "client.h" + +#if DO_NET_ENCODE +/* +============== +CL_Netchan_Encode + + // first 12 bytes of the data are always: + long serverId; + long messageAcknowledge; + long reliableAcknowledge; + +============== +*/ +static void CL_Netchan_Encode( msg_t *msg ) { + int serverId, messageAcknowledge, reliableAcknowledge; + int i, index, srdc, sbit, soob; + byte key, *string; + + if ( msg->cursize <= CL_ENCODE_START ) { + return; + } + + srdc = msg->readcount; + sbit = msg->bit; + soob = msg->oob; + + msg->bit = 0; + msg->readcount = 0; + msg->oob = 0; + + serverId = MSG_ReadLong( msg ); + messageAcknowledge = MSG_ReadLong( msg ); + reliableAcknowledge = MSG_ReadLong( msg ); + + msg->oob = soob; + msg->bit = sbit; + msg->readcount = srdc; + + string = (byte *)clc.serverCommands[ reliableAcknowledge & ( MAX_RELIABLE_COMMANDS - 1 ) ]; + index = 0; + // + key = clc.challenge ^ serverId ^ messageAcknowledge; + for ( i = CL_ENCODE_START; i < msg->cursize; i++ ) { + // modify the key with the last received now acknowledged server command + if ( !string[index] ) { + index = 0; + } + if ( string[index] > 127 || string[index] == '%' ) { + key ^= '.' << ( i & 1 ); + } else { + key ^= string[index] << ( i & 1 ); + } + index++; + // encode the data with this key + *( msg->data + i ) = ( *( msg->data + i ) ) ^ key; + } +} + +/* +============== +CL_Netchan_Decode + + // first four bytes of the data are always: + long reliableAcknowledge; + +============== +*/ +static void CL_Netchan_Decode( msg_t *msg ) { + long reliableAcknowledge, i, index; + byte key, *string; + int srdc, sbit, soob; + + srdc = msg->readcount; + sbit = msg->bit; + soob = msg->oob; + + msg->oob = 0; + + reliableAcknowledge = MSG_ReadLong( msg ); + + msg->oob = soob; + msg->bit = sbit; + msg->readcount = srdc; + + string = clc.reliableCommands[ reliableAcknowledge & ( MAX_RELIABLE_COMMANDS - 1 ) ]; + index = 0; + // xor the client challenge with the netchan sequence number (need something that changes every message) + key = clc.challenge ^ LittleLong( *(unsigned *)msg->data ); + for ( i = msg->readcount + CL_DECODE_START; i < msg->cursize; i++ ) { + // modify the key with the last sent and with this message acknowledged client command + if ( !string[index] ) { + index = 0; + } + if ( string[index] > 127 || string[index] == '%' ) { + key ^= '.' << ( i & 1 ); + } else { + key ^= string[index] << ( i & 1 ); + } + index++; + // decode the data with this key + *( msg->data + i ) = *( msg->data + i ) ^ key; + } +} +#endif + +/* +================= +CL_Netchan_TransmitNextFragment +================= +*/ +void CL_Netchan_TransmitNextFragment( netchan_t *chan ) { + Netchan_TransmitNextFragment( chan ); +} + +//byte chksum[65536]; + +/* +=============== +CL_Netchan_Transmit +================ +*/ +void CL_Netchan_Transmit( netchan_t *chan, msg_t* msg ) { +// int i; + MSG_WriteByte( msg, clc_EOF ); +// for(i=CL_ENCODE_START;icursize;i++) { +// chksum[i-CL_ENCODE_START] = msg->data[i]; +// } + +// Huff_Compress( msg, CL_ENCODE_START ); +#if DO_NET_ENCODE + CL_Netchan_Encode( msg ); +#endif + Netchan_Transmit( chan, msg->cursize, msg->data ); +} + +extern int oldsize; +int newsize = 0; + +/* +================= +CL_Netchan_Process +================= +*/ +qboolean CL_Netchan_Process( netchan_t *chan, msg_t *msg ) { + int ret; +// int i; +// static int newsize = 0; + + ret = Netchan_Process( chan, msg ); + if ( !ret ) { + return qfalse; + } +#if DO_NET_ENCODE + CL_Netchan_Decode( msg ); +#endif +// Huff_Decompress( msg, CL_DECODE_START ); +// for(i=CL_DECODE_START+msg->readcount;icursize;i++) { +// if (msg->data[i] != chksum[i-(CL_DECODE_START+msg->readcount)]) { +// Com_Error(ERR_DROP,"bad %d v %d\n", msg->data[i], chksum[i-(CL_DECODE_START+msg->readcount)]); +// } +// } + newsize += msg->cursize; +// Com_Printf("saved %d to %d (%d%%)\n", (oldsize>>3), newsize, 100-(newsize*100/(oldsize>>3))); + return qtrue; +} diff --git a/src/client/cl_parse.c b/src/client/cl_parse.c new file mode 100644 index 0000000..ea3f1a4 --- /dev/null +++ b/src/client/cl_parse.c @@ -0,0 +1,654 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// cl_parse.c -- parse a message received from the server + +#include "client.h" + +char *svc_strings[256] = { + "svc_bad", + + "svc_nop", + "svc_gamestate", + "svc_configstring", + "svc_baseline", + "svc_serverCommand", + "svc_download", + "svc_snapshot" +}; + +void SHOWNET( msg_t *msg, char *s ) { + if ( cl_shownet->integer >= 2 ) { + Com_Printf( "%3i %3i:%s\n", msg->readcount - 1, msg->cursize, s ); + } +} + + +/* +========================================================================= + +MESSAGE PARSING + +========================================================================= +*/ + +/* +================== +CL_DeltaEntity + +Parses deltas from the given base and adds the resulting entity +to the current frame +================== +*/ +void CL_DeltaEntity( msg_t *msg, clSnapshot_t *frame, int newnum, entityState_t *old, + qboolean unchanged ) { + entityState_t *state; + + // save the parsed entity state into the big circular buffer so + // it can be used as the source for a later delta + state = &cl.parseEntities[cl.parseEntitiesNum & ( MAX_PARSE_ENTITIES - 1 )]; + + if ( unchanged ) { + *state = *old; + } else { + MSG_ReadDeltaEntity( msg, old, state, newnum ); + } + + if ( state->number == ( MAX_GENTITIES - 1 ) ) { + return; // entity was delta removed + } + cl.parseEntitiesNum++; + frame->numEntities++; +} + +/* +================== +CL_ParsePacketEntities + +================== +*/ +void CL_ParsePacketEntities( msg_t *msg, clSnapshot_t *oldframe, clSnapshot_t *newframe ) { + int newnum; + entityState_t *oldstate; + int oldindex, oldnum; + + newframe->parseEntitiesNum = cl.parseEntitiesNum; + newframe->numEntities = 0; + + // delta from the entities present in oldframe + oldindex = 0; + oldstate = NULL; + if ( !oldframe ) { + oldnum = 99999; + } else { + if ( oldindex >= oldframe->numEntities ) { + oldnum = 99999; + } else { + oldstate = &cl.parseEntities[ + ( oldframe->parseEntitiesNum + oldindex ) & ( MAX_PARSE_ENTITIES - 1 )]; + oldnum = oldstate->number; + } + } + + while ( 1 ) { + // read the entity index number + newnum = MSG_ReadBits( msg, GENTITYNUM_BITS ); + + if ( newnum == ( MAX_GENTITIES - 1 ) ) { + break; + } + + if ( msg->readcount > msg->cursize ) { + Com_Error( ERR_DROP,"CL_ParsePacketEntities: end of message" ); + } + + while ( oldnum < newnum ) { + // one or more entities from the old packet are unchanged + if ( cl_shownet->integer == 3 ) { + Com_Printf( "%3i: unchanged: %i\n", msg->readcount, oldnum ); + } + CL_DeltaEntity( msg, newframe, oldnum, oldstate, qtrue ); + + oldindex++; + + if ( oldindex >= oldframe->numEntities ) { + oldnum = 99999; + } else { + oldstate = &cl.parseEntities[ + ( oldframe->parseEntitiesNum + oldindex ) & ( MAX_PARSE_ENTITIES - 1 )]; + oldnum = oldstate->number; + } + } + if ( oldnum == newnum ) { + // delta from previous state + if ( cl_shownet->integer == 3 ) { + Com_Printf( "%3i: delta: %i\n", msg->readcount, newnum ); + } + CL_DeltaEntity( msg, newframe, newnum, oldstate, qfalse ); + + oldindex++; + + if ( oldindex >= oldframe->numEntities ) { + oldnum = 99999; + } else { + oldstate = &cl.parseEntities[ + ( oldframe->parseEntitiesNum + oldindex ) & ( MAX_PARSE_ENTITIES - 1 )]; + oldnum = oldstate->number; + } + continue; + } + + if ( oldnum > newnum ) { + // delta from baseline + if ( cl_shownet->integer == 3 ) { + Com_Printf( "%3i: baseline: %i\n", msg->readcount, newnum ); + } + CL_DeltaEntity( msg, newframe, newnum, &cl.entityBaselines[newnum], qfalse ); + continue; + } + + } + + // any remaining entities in the old frame are copied over + while ( oldnum != 99999 ) { + // one or more entities from the old packet are unchanged + if ( cl_shownet->integer == 3 ) { + Com_Printf( "%3i: unchanged: %i\n", msg->readcount, oldnum ); + } + CL_DeltaEntity( msg, newframe, oldnum, oldstate, qtrue ); + + oldindex++; + + if ( oldindex >= oldframe->numEntities ) { + oldnum = 99999; + } else { + oldstate = &cl.parseEntities[ + ( oldframe->parseEntitiesNum + oldindex ) & ( MAX_PARSE_ENTITIES - 1 )]; + oldnum = oldstate->number; + } + } +} + + +/* +================ +CL_ParseSnapshot + +If the snapshot is parsed properly, it will be copied to +cl.snap and saved in cl.snapshots[]. If the snapshot is invalid +for any reason, no changes to the state will be made at all. +================ +*/ +void CL_ParseSnapshot( msg_t *msg ) { + int len; + clSnapshot_t *old; + clSnapshot_t newSnap; + int deltaNum; + int oldMessageNum; + int i, packetNum; + + // get the reliable sequence acknowledge number + // NOTE: now sent with all server to client messages + //clc.reliableAcknowledge = MSG_ReadLong( msg ); + + // read in the new snapshot to a temporary buffer + // we will only copy to cl.snap if it is valid + memset( &newSnap, 0, sizeof( newSnap ) ); + + // we will have read any new server commands in this + // message before we got to svc_snapshot + newSnap.serverCommandNum = clc.serverCommandSequence; + + newSnap.serverTime = MSG_ReadLong( msg ); + + newSnap.messageNum = clc.serverMessageSequence; + + deltaNum = MSG_ReadByte( msg ); + if ( !deltaNum ) { + newSnap.deltaNum = -1; + } else { + newSnap.deltaNum = newSnap.messageNum - deltaNum; + } + newSnap.snapFlags = MSG_ReadByte( msg ); + + // If the frame is delta compressed from data that we + // no longer have available, we must suck up the rest of + // the frame, but not use it, then ask for a non-compressed + // message + if ( newSnap.deltaNum <= 0 ) { + newSnap.valid = qtrue; // uncompressed frame + old = NULL; + clc.demowaiting = qfalse; // we can start recording now + } else { + old = &cl.snapshots[newSnap.deltaNum & PACKET_MASK]; + if ( !old->valid ) { + // should never happen + Com_Printf( "Delta from invalid frame (not supposed to happen!).\n" ); + } else if ( old->messageNum != newSnap.deltaNum ) { + // The frame that the server did the delta from + // is too old, so we can't reconstruct it properly. + Com_Printf( "Delta frame too old.\n" ); + } else if ( cl.parseEntitiesNum - old->parseEntitiesNum > MAX_PARSE_ENTITIES - 128 ) { + Com_Printf( "Delta parseEntitiesNum too old.\n" ); + } else { + newSnap.valid = qtrue; // valid delta parse + } + } + + // read areamask + len = MSG_ReadByte( msg ); + MSG_ReadData( msg, &newSnap.areamask, len ); + + // read playerinfo + SHOWNET( msg, "playerstate" ); + if ( old ) { + MSG_ReadDeltaPlayerstate( msg, &old->ps, &newSnap.ps ); + } else { + MSG_ReadDeltaPlayerstate( msg, NULL, &newSnap.ps ); + } + + // read packet entities + SHOWNET( msg, "packet entities" ); + CL_ParsePacketEntities( msg, old, &newSnap ); + + // if not valid, dump the entire thing now that it has + // been properly read + if ( !newSnap.valid ) { + return; + } + + // clear the valid flags of any snapshots between the last + // received and this one, so if there was a dropped packet + // it won't look like something valid to delta from next + // time we wrap around in the buffer + oldMessageNum = cl.snap.messageNum + 1; + + if ( newSnap.messageNum - oldMessageNum >= PACKET_BACKUP ) { + oldMessageNum = newSnap.messageNum - ( PACKET_BACKUP - 1 ); + } + for ( ; oldMessageNum < newSnap.messageNum ; oldMessageNum++ ) { + cl.snapshots[oldMessageNum & PACKET_MASK].valid = qfalse; + } + + // copy to the current good spot + cl.snap = newSnap; + cl.snap.ping = 999; + // calculate ping time + for ( i = 0 ; i < PACKET_BACKUP ; i++ ) { + packetNum = ( clc.netchan.outgoingSequence - 1 - i ) & PACKET_MASK; + if ( cl.snap.ps.commandTime >= cl.outPackets[ packetNum ].p_serverTime ) { + cl.snap.ping = cls.realtime - cl.outPackets[ packetNum ].p_realtime; + break; + } + } + // save the frame off in the backup array for later delta comparisons + cl.snapshots[cl.snap.messageNum & PACKET_MASK] = cl.snap; + + if ( cl_shownet->integer == 3 ) { + Com_Printf( " snapshot:%i delta:%i ping:%i\n", cl.snap.messageNum, + cl.snap.deltaNum, cl.snap.ping ); + } + + cl.newSnapshots = qtrue; +} + + +//===================================================================== + +int cl_connectedToPureServer; + +/* +================== +CL_SystemInfoChanged + +The systeminfo configstring has been changed, so parse +new information out of it. This will happen at every +gamestate, and possibly during gameplay. +================== +*/ +void CL_SystemInfoChanged( void ) { + char *systemInfo; + const char *s, *t; + char key[BIG_INFO_KEY]; + char value[BIG_INFO_VALUE]; + + systemInfo = cl.gameState.stringData + cl.gameState.stringOffsets[ CS_SYSTEMINFO ]; + cl.serverId = atoi( Info_ValueForKey( systemInfo, "sv_serverid" ) ); + + // don't set any vars when playing a demo + if ( clc.demoplaying ) { + return; + } + + s = Info_ValueForKey( systemInfo, "sv_cheats" ); + if ( atoi( s ) == 0 ) { + Cvar_SetCheatState(); + } + + // check pure server string + s = Info_ValueForKey( systemInfo, "sv_paks" ); + t = Info_ValueForKey( systemInfo, "sv_pakNames" ); + FS_PureServerSetLoadedPaks( s, t ); + + s = Info_ValueForKey( systemInfo, "sv_referencedPaks" ); + t = Info_ValueForKey( systemInfo, "sv_referencedPakNames" ); + FS_PureServerSetReferencedPaks( s, t ); + + + // scan through all the variables in the systeminfo and locally set cvars to match + s = systemInfo; + while ( s ) { + Info_NextPair( &s, key, value ); + if ( !key[0] ) { + break; + } + + Cvar_Set( key, value ); + } + cl_connectedToPureServer = Cvar_VariableValue( "sv_pure" ); +} + +/* +================== +CL_ParseGamestate +================== +*/ +void CL_ParseGamestate( msg_t *msg ) { + int i; + entityState_t *es; + int newnum; + entityState_t nullstate; + int cmd; + char *s; + + Con_Close(); + + clc.connectPacketCount = 0; + + // wipe local client state + CL_ClearState(); + + // a gamestate always marks a server command sequence + clc.serverCommandSequence = MSG_ReadLong( msg ); + + // parse all the configstrings and baselines + cl.gameState.dataCount = 1; // leave a 0 at the beginning for uninitialized configstrings + while ( 1 ) { + cmd = MSG_ReadByte( msg ); + + if ( cmd == svc_EOF ) { + break; + } + + if ( cmd == svc_configstring ) { + int len; + + i = MSG_ReadShort( msg ); + if ( i < 0 || i >= MAX_CONFIGSTRINGS ) { + Com_Error( ERR_DROP, "configstring > MAX_CONFIGSTRINGS" ); + } + s = MSG_ReadBigString( msg ); + len = strlen( s ); + + if ( len + 1 + cl.gameState.dataCount > MAX_GAMESTATE_CHARS ) { + Com_Error( ERR_DROP, "MAX_GAMESTATE_CHARS exceeded" ); + } + + // append it to the gameState string buffer + cl.gameState.stringOffsets[ i ] = cl.gameState.dataCount; + memcpy( cl.gameState.stringData + cl.gameState.dataCount, s, len + 1 ); + cl.gameState.dataCount += len + 1; + } else if ( cmd == svc_baseline ) { + newnum = MSG_ReadBits( msg, GENTITYNUM_BITS ); + if ( newnum < 0 || newnum >= MAX_GENTITIES ) { + Com_Error( ERR_DROP, "Baseline number out of range: %i", newnum ); + } + memset( &nullstate, 0, sizeof( nullstate ) ); + es = &cl.entityBaselines[ newnum ]; + MSG_ReadDeltaEntity( msg, &nullstate, es, newnum ); + } else { + Com_Error( ERR_DROP, "CL_ParseGamestate: bad command byte" ); + } + } + + clc.clientNum = MSG_ReadLong( msg ); + // read the checksum feed + clc.checksumFeed = MSG_ReadLong( msg ); + + // parse serverId and other cvars + CL_SystemInfoChanged(); + + // reinitialize the filesystem if the game directory has changed + if ( FS_ConditionalRestart( clc.checksumFeed ) ) { + // don't set to true because we yet have to start downloading + // enabling this can cause double loading of a map when connecting to + // a server which has a different game directory set + //clc.downloadRestart = qtrue; + } + + // This used to call CL_StartHunkUsers, but now we enter the download state before loading the + // cgame + CL_InitDownloads(); + + // make sure the game starts + Cvar_Set( "cl_paused", "0" ); +} + + +//===================================================================== + +/* +===================== +CL_ParseDownload + +A download message has been received from the server +===================== +*/ +void CL_ParseDownload( msg_t *msg ) { + int size; + unsigned char data[MAX_MSGLEN]; + int block; + + // read the data + block = MSG_ReadShort( msg ); + + if ( !block ) { + // block zero is special, contains file size + clc.downloadSize = MSG_ReadLong( msg ); + + Cvar_SetValue( "cl_downloadSize", clc.downloadSize ); + + if ( clc.downloadSize < 0 ) { + Com_Error( ERR_DROP, MSG_ReadString( msg ) ); + return; + } + } + + size = MSG_ReadShort( msg ); + if ( size > 0 ) { + MSG_ReadData( msg, data, size ); + } + + if ( clc.downloadBlock != block ) { + Com_DPrintf( "CL_ParseDownload: Expected block %d, got %d\n", clc.downloadBlock, block ); + return; + } + + // open the file if not opened yet + if ( !clc.download ) { + if ( !*clc.downloadTempName ) { + Com_Printf( "Server sending download, but no download was requested\n" ); + CL_AddReliableCommand( "stopdl" ); + return; + } + + clc.download = FS_SV_FOpenFileWrite( clc.downloadTempName ); + + if ( !clc.download ) { + Com_Printf( "Could not create %s\n", clc.downloadTempName ); + CL_AddReliableCommand( "stopdl" ); + CL_NextDownload(); + return; + } + } + + if ( size ) { + FS_Write( data, size, clc.download ); + } + + CL_AddReliableCommand( va( "nextdl %d", clc.downloadBlock ) ); + clc.downloadBlock++; + + clc.downloadCount += size; + + // So UI gets access to it + Cvar_SetValue( "cl_downloadCount", clc.downloadCount ); + + if ( !size ) { // A zero length block means EOF + if ( clc.download ) { + FS_FCloseFile( clc.download ); + clc.download = 0; + + // rename the file + FS_SV_Rename( clc.downloadTempName, clc.downloadName ); + } + *clc.downloadTempName = *clc.downloadName = 0; + Cvar_Set( "cl_downloadName", "" ); + + // send intentions now + // We need this because without it, we would hold the last nextdl and then start + // loading right away. If we take a while to load, the server is happily trying + // to send us that last block over and over. + // Write it twice to help make sure we acknowledge the download + CL_WritePacket(); + CL_WritePacket(); + + // get another file if needed + CL_NextDownload(); + } +} + +/* +===================== +CL_ParseCommandString + +Command strings are just saved off until cgame asks for them +when it transitions a snapshot +===================== +*/ +void CL_ParseCommandString( msg_t *msg ) { + char *s; + int seq; + int index; + + seq = MSG_ReadLong( msg ); + s = MSG_ReadString( msg ); + + // see if we have already executed stored it off + if ( clc.serverCommandSequence >= seq ) { + return; + } + clc.serverCommandSequence = seq; + + index = seq & ( MAX_RELIABLE_COMMANDS - 1 ); + Q_strncpyz( clc.serverCommands[ index ], s, sizeof( clc.serverCommands[ index ] ) ); +} + + +/* +===================== +CL_ParseServerMessage +===================== +*/ +void CL_ParseServerMessage( msg_t *msg ) { + int cmd; + msg_t msgback; + + msgback = *msg; + + if ( cl_shownet->integer == 1 ) { + Com_Printf( "%i ",msg->cursize ); + } else if ( cl_shownet->integer >= 2 ) { + Com_Printf( "------------------\n" ); + } + + MSG_Bitstream( msg ); + + // get the reliable sequence acknowledge number + clc.reliableAcknowledge = MSG_ReadLong( msg ); + // + if ( clc.reliableAcknowledge < clc.reliableSequence - MAX_RELIABLE_COMMANDS ) { + clc.reliableAcknowledge = clc.reliableSequence; + } + + // + // parse the message + // + while ( 1 ) { + if ( msg->readcount > msg->cursize ) { + Com_Error( ERR_DROP,"CL_ParseServerMessage: read past end of server message" ); + break; + } + + cmd = MSG_ReadByte( msg ); + + if ( cmd == svc_EOF ) { + SHOWNET( msg, "END OF MESSAGE" ); + break; + } + + if ( cl_shownet->integer >= 2 ) { + if ( !svc_strings[cmd] ) { + Com_Printf( "%3i:BAD CMD %i\n", msg->readcount - 1, cmd ); + } else { + SHOWNET( msg, svc_strings[cmd] ); + } + } + + // other commands + switch ( cmd ) { + default: + Com_Error( ERR_DROP,"CL_ParseServerMessage: Illegible server message\n" ); + break; + case svc_nop: + break; + case svc_serverCommand: + CL_ParseCommandString( msg ); + break; + case svc_gamestate: + CL_ParseGamestate( msg ); + break; + case svc_snapshot: + CL_ParseSnapshot( msg ); + break; + case svc_download: + CL_ParseDownload( msg ); + break; + } + } +} diff --git a/src/client/cl_scrn.c b/src/client/cl_scrn.c new file mode 100644 index 0000000..26c02d0 --- /dev/null +++ b/src/client/cl_scrn.c @@ -0,0 +1,555 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// cl_scrn.c -- master for refresh, status bar, console, chat, notify, etc + +#include "client.h" + +qboolean scr_initialized; // ready to draw + +cvar_t *cl_timegraph; +cvar_t *cl_debuggraph; +cvar_t *cl_graphheight; +cvar_t *cl_graphscale; +cvar_t *cl_graphshift; + +/* +================ +SCR_DrawNamedPic + +Coordinates are 640*480 virtual values +================= +*/ +void SCR_DrawNamedPic( float x, float y, float width, float height, const char *picname ) { + qhandle_t hShader; + + assert( width != 0 ); + + hShader = re.RegisterShader( picname ); + SCR_AdjustFrom640( &x, &y, &width, &height ); + re.DrawStretchPic( x, y, width, height, 0, 0, 1, 1, hShader ); +} + + +/* +================ +SCR_AdjustFrom640 + +Adjusted for resolution and screen aspect ratio +================ +*/ +void SCR_AdjustFrom640( float *x, float *y, float *w, float *h ) { + float xscale; + float yscale; + +#if 0 + // adjust for wide screens + if ( cls.glconfig.vidWidth * 480 > cls.glconfig.vidHeight * 640 ) { + *x += 0.5 * ( cls.glconfig.vidWidth - ( cls.glconfig.vidHeight * 640 / 480 ) ); + } +#endif + + // scale for screen sizes + xscale = cls.glconfig.vidWidth / 640.0; + yscale = cls.glconfig.vidHeight / 480.0; + if ( x ) { + *x *= xscale; + } + if ( y ) { + *y *= yscale; + } + if ( w ) { + *w *= xscale; + } + if ( h ) { + *h *= yscale; + } +} + +/* +================ +SCR_FillRect + +Coordinates are 640*480 virtual values +================= +*/ +void SCR_FillRect( float x, float y, float width, float height, const float *color ) { + re.SetColor( color ); + + SCR_AdjustFrom640( &x, &y, &width, &height ); + re.DrawStretchPic( x, y, width, height, 0, 0, 0, 0, cls.whiteShader ); + + re.SetColor( NULL ); +} + + +/* +================ +SCR_DrawPic + +Coordinates are 640*480 virtual values +================= +*/ +void SCR_DrawPic( float x, float y, float width, float height, qhandle_t hShader ) { + SCR_AdjustFrom640( &x, &y, &width, &height ); + re.DrawStretchPic( x, y, width, height, 0, 0, 1, 1, hShader ); +} + + + +/* +** SCR_DrawChar +** chars are drawn at 640*480 virtual screen size +*/ +static void SCR_DrawChar( int x, int y, float size, int ch ) { + int row, col; + float frow, fcol; + float ax, ay, aw, ah; + + ch &= 255; + + if ( ch == ' ' ) { + return; + } + + if ( y < -size ) { + return; + } + + ax = x; + ay = y; + aw = size; + ah = size; + SCR_AdjustFrom640( &ax, &ay, &aw, &ah ); + + row = ch >> 4; + col = ch & 15; + + frow = row * 0.0625; + fcol = col * 0.0625; + size = 0.0625; + + re.DrawStretchPic( ax, ay, aw, ah, + fcol, frow, + fcol + size, frow + size, + cls.charSetShader ); +} + +/* +** SCR_DrawSmallChar +** small chars are drawn at native screen resolution +*/ +void SCR_DrawSmallChar( int x, int y, int ch ) { + int row, col; + float frow, fcol; + float size; + + ch &= 255; + + if ( ch == ' ' ) { + return; + } + + if ( y < -SMALLCHAR_HEIGHT ) { + return; + } + + row = ch >> 4; + col = ch & 15; + + frow = row * 0.0625; + fcol = col * 0.0625; + size = 0.0625; + + re.DrawStretchPic( x, y, SMALLCHAR_WIDTH, SMALLCHAR_HEIGHT, + fcol, frow, + fcol + size, frow + size, + cls.charSetShader ); +} + + +/* +================== +SCR_DrawBigString[Color] + +Draws a multi-colored string with a drop shadow, optionally forcing +to a fixed color. + +Coordinates are at 640 by 480 virtual resolution +================== +*/ +void SCR_DrawStringExt( int x, int y, float size, const char *string, float *setColor, qboolean forceColor ) { + vec4_t color; + const char *s; + int xx; + + // draw the drop shadow + color[0] = color[1] = color[2] = 0; + color[3] = setColor[3]; + re.SetColor( color ); + s = string; + xx = x; + while ( *s ) { + if ( Q_IsColorString( s ) ) { + s += 2; + continue; + } + SCR_DrawChar( xx + 2, y + 2, size, *s ); + xx += size; + s++; + } + + + // draw the colored text + s = string; + xx = x; + re.SetColor( setColor ); + while ( *s ) { + if ( Q_IsColorString( s ) ) { + if ( !forceColor ) { + memcpy( color, g_color_table[ColorIndex( *( s + 1 ) )], sizeof( color ) ); + color[3] = setColor[3]; + re.SetColor( color ); + } + s += 2; + continue; + } + SCR_DrawChar( xx, y, size, *s ); + xx += size; + s++; + } + re.SetColor( NULL ); +} + + +void SCR_DrawBigString( int x, int y, const char *s, float alpha ) { + float color[4]; + + color[0] = color[1] = color[2] = 1.0; + color[3] = alpha; + SCR_DrawStringExt( x, y, BIGCHAR_WIDTH, s, color, qfalse ); +} + +void SCR_DrawBigStringColor( int x, int y, const char *s, vec4_t color ) { + SCR_DrawStringExt( x, y, BIGCHAR_WIDTH, s, color, qtrue ); +} + + +/* +================== +SCR_DrawSmallString[Color] + +Draws a multi-colored string with a drop shadow, optionally forcing +to a fixed color. + +Coordinates are at 640 by 480 virtual resolution +================== +*/ +void SCR_DrawSmallStringExt( int x, int y, const char *string, float *setColor, qboolean forceColor ) { + vec4_t color; + const char *s; + int xx; + + // draw the colored text + s = string; + xx = x; + re.SetColor( setColor ); + while ( *s ) { + if ( Q_IsColorString( s ) ) { + if ( !forceColor ) { + memcpy( color, g_color_table[ColorIndex( *( s + 1 ) )], sizeof( color ) ); + color[3] = setColor[3]; + re.SetColor( color ); + } + s += 2; + continue; + } + SCR_DrawSmallChar( xx, y, *s ); + xx += SMALLCHAR_WIDTH; + s++; + } + re.SetColor( NULL ); +} + + + +/* +** SCR_Strlen -- skips color escape codes +*/ +static int SCR_Strlen( const char *str ) { + const char *s = str; + int count = 0; + + while ( *s ) { + if ( Q_IsColorString( s ) ) { + s += 2; + } else { + count++; + s++; + } + } + + return count; +} + +/* +** SCR_GetBigStringWidth +*/ +int SCR_GetBigStringWidth( const char *str ) { + return SCR_Strlen( str ) * 16; +} + + +//=============================================================================== + +/* +================= +SCR_DrawDemoRecording +================= +*/ +void SCR_DrawDemoRecording( void ) { + char string[1024]; + int pos; + + if ( !clc.demorecording ) { + return; + } + + pos = FS_FTell( clc.demofile ); + sprintf( string, "RECORDING %s: %ik", clc.demoName, pos / 1024 ); + + SCR_DrawStringExt( 320 - strlen( string ) * 4, 20, 8, string, g_color_table[7], qtrue ); +} + + +/* +=============================================================================== + +DEBUG GRAPH + +=============================================================================== +*/ + +typedef struct +{ + float value; + int color; +} graphsamp_t; + +static int current; +static graphsamp_t values[1024]; + +/* +============== +SCR_DebugGraph +============== +*/ +void SCR_DebugGraph( float value, int color ) { + values[current & 1023].value = value; + values[current & 1023].color = color; + current++; +} + +/* +============== +SCR_DrawDebugGraph +============== +*/ +void SCR_DrawDebugGraph( void ) { + int a, x, y, w, i, h; + float v; + int color; + + // + // draw the graph + // + w = cls.glconfig.vidWidth; + x = 0; + y = cls.glconfig.vidHeight; + re.SetColor( g_color_table[0] ); + re.DrawStretchPic( x, y - cl_graphheight->integer, + w, cl_graphheight->integer, 0, 0, 0, 0, cls.whiteShader ); + re.SetColor( NULL ); + + for ( a = 0 ; a < w ; a++ ) + { + i = ( current - 1 - a + 1024 ) & 1023; + v = values[i].value; + color = values[i].color; + v = v * cl_graphscale->integer + cl_graphshift->integer; + + if ( v < 0 ) { + v += cl_graphheight->integer * ( 1 + (int)( -v / cl_graphheight->integer ) ); + } + h = (int)v % cl_graphheight->integer; + re.DrawStretchPic( x + w - 1 - a, y - h, 1, h, 0, 0, 0, 0, cls.whiteShader ); + } +} + +//============================================================================= + +/* +================== +SCR_Init +================== +*/ +void SCR_Init( void ) { + cl_timegraph = Cvar_Get( "timegraph", "0", CVAR_CHEAT ); + cl_debuggraph = Cvar_Get( "debuggraph", "0", CVAR_CHEAT ); + cl_graphheight = Cvar_Get( "graphheight", "32", CVAR_CHEAT ); + cl_graphscale = Cvar_Get( "graphscale", "1", CVAR_CHEAT ); + cl_graphshift = Cvar_Get( "graphshift", "0", CVAR_CHEAT ); + + scr_initialized = qtrue; +} + + +//======================================================= + +/* +================== +SCR_DrawScreenField + +This will be called twice if rendering in stereo mode +================== +*/ +void SCR_DrawScreenField( stereoFrame_t stereoFrame ) { + re.BeginFrame( stereoFrame ); + + // wide aspect ratio screens need to have the sides cleared + // unless they are displaying game renderings + if ( cls.state != CA_ACTIVE ) { + if ( cls.glconfig.vidWidth * 480 > cls.glconfig.vidHeight * 640 ) { + re.SetColor( g_color_table[0] ); + re.DrawStretchPic( 0, 0, cls.glconfig.vidWidth, cls.glconfig.vidHeight, 0, 0, 0, 0, cls.whiteShader ); + re.SetColor( NULL ); + } + } + + if ( !uivm ) { + Com_DPrintf( "draw screen without UI loaded\n" ); + return; + } + + // if the menu is going to cover the entire screen, we + // don't need to render anything under it + if ( !VM_Call( uivm, UI_IS_FULLSCREEN ) ) { + switch ( cls.state ) { + default: + Com_Error( ERR_FATAL, "SCR_DrawScreenField: bad cls.state" ); + break; + case CA_CINEMATIC: + SCR_DrawCinematic(); + break; + case CA_DISCONNECTED: + // force menu up + S_StopAllSounds(); + VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_MAIN ); + break; + case CA_CONNECTING: + case CA_CHALLENGING: + case CA_CONNECTED: + // connecting clients will only show the connection dialog + // refresh to update the time + VM_Call( uivm, UI_REFRESH, cls.realtime ); + VM_Call( uivm, UI_DRAW_CONNECT_SCREEN, qfalse ); + break; +// // Ridah, if the cgame is valid, fall through to there +// if (!cls.cgameStarted || !com_sv_running->integer) { +// // connecting clients will only show the connection dialog +// VM_Call( uivm, UI_DRAW_CONNECT_SCREEN, qfalse ); +// break; +// } + case CA_LOADING: + case CA_PRIMED: + // draw the game information screen and loading progress + CL_CGameRendering( stereoFrame ); + + // also draw the connection information, so it doesn't + // flash away too briefly on local or lan games + //if (!com_sv_running->value || Cvar_VariableIntegerValue("sv_cheats")) // Ridah, don't draw useless text if not in dev mode + VM_Call( uivm, UI_REFRESH, cls.realtime ); + VM_Call( uivm, UI_DRAW_CONNECT_SCREEN, qtrue ); + break; + case CA_ACTIVE: + CL_CGameRendering( stereoFrame ); + SCR_DrawDemoRecording(); + break; + } + } + + // the menu draws next + if ( cls.keyCatchers & KEYCATCH_UI && uivm ) { + VM_Call( uivm, UI_REFRESH, cls.realtime ); + } + + // console draws next + Con_DrawConsole(); + + // debug graph can be drawn on top of anything + if ( cl_debuggraph->integer || cl_timegraph->integer || cl_debugMove->integer ) { + SCR_DrawDebugGraph(); + } +} + +/* +================== +SCR_UpdateScreen + +This is called every frame, and can also be called explicitly to flush +text to the screen. +================== +*/ +void SCR_UpdateScreen( void ) { + static int recursive; + + if ( !scr_initialized ) { + return; // not initialized yet + } + + if ( ++recursive > 2 ) { + Com_Error( ERR_FATAL, "SCR_UpdateScreen: recursively called" ); + } + recursive = 1; + + // if running in stereo, we need to draw the frame twice + if ( cls.glconfig.stereoEnabled ) { + SCR_DrawScreenField( STEREO_LEFT ); + SCR_DrawScreenField( STEREO_RIGHT ); + } else { + SCR_DrawScreenField( STEREO_CENTER ); + } + + if ( com_speeds->integer ) { + re.EndFrame( &time_frontend, &time_backend ); + } else { + re.EndFrame( NULL, NULL ); + } + + recursive = 0; +} diff --git a/src/client/cl_ui.c b/src/client/cl_ui.c new file mode 100644 index 0000000..91551ec --- /dev/null +++ b/src/client/cl_ui.c @@ -0,0 +1,1251 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +#include "client.h" + +#include "../game/botlib.h" + +extern botlib_export_t *botlib_export; + +vm_t *uivm; + +extern char cl_cdkey[34]; + + +/* +==================== +GetClientState +==================== +*/ +static void GetClientState( uiClientState_t *state ) { + state->connectPacketCount = clc.connectPacketCount; + state->connState = cls.state; + Q_strncpyz( state->servername, cls.servername, sizeof( state->servername ) ); + Q_strncpyz( state->updateInfoString, cls.updateInfoString, sizeof( state->updateInfoString ) ); + Q_strncpyz( state->messageString, clc.serverMessage, sizeof( state->messageString ) ); + state->clientNum = cl.snap.ps.clientNum; +} + +/* +==================== +LAN_LoadCachedServers +==================== +*/ +void LAN_LoadCachedServers() { + // TTimo: stub, this is only relevant to MP, SP kills the servercache.dat (and favorites) + // show_bug.cgi?id=445 + /* + int size; + fileHandle_t fileIn; + cls.numglobalservers = cls.nummplayerservers = cls.numfavoriteservers = 0; + cls.numGlobalServerAddresses = 0; + if (FS_SV_FOpenFileRead("servercache.dat", &fileIn)) { + FS_Read(&cls.numglobalservers, sizeof(int), fileIn); + FS_Read(&cls.nummplayerservers, sizeof(int), fileIn); + FS_Read(&cls.numfavoriteservers, sizeof(int), fileIn); + FS_Read(&size, sizeof(int), fileIn); + if (size == sizeof(cls.globalServers) + sizeof(cls.favoriteServers) + sizeof(cls.mplayerServers)) { + FS_Read(&cls.globalServers, sizeof(cls.globalServers), fileIn); + FS_Read(&cls.mplayerServers, sizeof(cls.mplayerServers), fileIn); + FS_Read(&cls.favoriteServers, sizeof(cls.favoriteServers), fileIn); + } else { + cls.numglobalservers = cls.nummplayerservers = cls.numfavoriteservers = 0; + cls.numGlobalServerAddresses = 0; + } + FS_FCloseFile(fileIn); + } + */ +} + +/* +==================== +LAN_SaveServersToCache +==================== +*/ +void LAN_SaveServersToCache() { + // TTimo: stub, this is only relevant to MP, SP kills the servercache.dat (and favorites) + // show_bug.cgi?id=445 + /* + int size; + fileHandle_t fileOut; + #ifdef __MACOS__ //DAJ MacOS file typing + { + extern _MSL_IMP_EXP_C long _fcreator, _ftype; + _ftype = 'WlfB'; + _fcreator = 'WlfS'; + } + #endif + fileOut = FS_SV_FOpenFileWrite("servercache.dat"); + FS_Write(&cls.numglobalservers, sizeof(int), fileOut); + FS_Write(&cls.nummplayerservers, sizeof(int), fileOut); + FS_Write(&cls.numfavoriteservers, sizeof(int), fileOut); + size = sizeof(cls.globalServers) + sizeof(cls.favoriteServers) + sizeof(cls.mplayerServers); + FS_Write(&size, sizeof(int), fileOut); + FS_Write(&cls.globalServers, sizeof(cls.globalServers), fileOut); + FS_Write(&cls.mplayerServers, sizeof(cls.mplayerServers), fileOut); + FS_Write(&cls.favoriteServers, sizeof(cls.favoriteServers), fileOut); + FS_FCloseFile(fileOut); + */ +} + + +/* +==================== +LAN_ResetPings +==================== +*/ +static void LAN_ResetPings( int source ) { + int count,i; + serverInfo_t *servers = NULL; + count = 0; + + switch ( source ) { + case AS_LOCAL: + servers = &cls.localServers[0]; + count = MAX_OTHER_SERVERS; + break; + case AS_MPLAYER: + servers = &cls.mplayerServers[0]; + count = MAX_OTHER_SERVERS; + break; + case AS_GLOBAL: + servers = &cls.globalServers[0]; + count = MAX_GLOBAL_SERVERS; + break; + case AS_FAVORITES: + servers = &cls.favoriteServers[0]; + count = MAX_OTHER_SERVERS; + break; + } + if ( servers ) { + for ( i = 0; i < count; i++ ) { + servers[i].ping = -1; + } + } +} + +/* +==================== +LAN_AddServer +==================== +*/ +static int LAN_AddServer( int source, const char *name, const char *address ) { + int max, *count, i; + netadr_t adr; + serverInfo_t *servers = NULL; + max = MAX_OTHER_SERVERS; + count = 0; + + switch ( source ) { + case AS_LOCAL: + count = &cls.numlocalservers; + servers = &cls.localServers[0]; + break; + case AS_MPLAYER: + count = &cls.nummplayerservers; + servers = &cls.mplayerServers[0]; + break; + case AS_GLOBAL: + max = MAX_GLOBAL_SERVERS; + count = &cls.numglobalservers; + servers = &cls.globalServers[0]; + break; + case AS_FAVORITES: + count = &cls.numfavoriteservers; + servers = &cls.favoriteServers[0]; + break; + } + if ( servers && *count < max ) { + NET_StringToAdr( address, &adr ); + for ( i = 0; i < *count; i++ ) { + if ( NET_CompareAdr( servers[i].adr, adr ) ) { + break; + } + } + if ( i >= *count ) { + servers[*count].adr = adr; + Q_strncpyz( servers[*count].hostName, name, sizeof( servers[*count].hostName ) ); + servers[*count].visible = qtrue; + ( *count )++; + return 1; + } + return 0; + } + return -1; +} + +/* +==================== +LAN_RemoveServer +==================== +*/ +static void LAN_RemoveServer( int source, const char *addr ) { + int *count, i; + serverInfo_t *servers = NULL; + count = 0; + switch ( source ) { + case AS_LOCAL: + count = &cls.numlocalservers; + servers = &cls.localServers[0]; + break; + case AS_MPLAYER: + count = &cls.nummplayerservers; + servers = &cls.mplayerServers[0]; + break; + case AS_GLOBAL: + count = &cls.numglobalservers; + servers = &cls.globalServers[0]; + break; + case AS_FAVORITES: + count = &cls.numfavoriteservers; + servers = &cls.favoriteServers[0]; + break; + } + if ( servers ) { + netadr_t comp; + NET_StringToAdr( addr, &comp ); + for ( i = 0; i < *count; i++ ) { + if ( NET_CompareAdr( comp, servers[i].adr ) ) { + int j = i; + while ( j < *count - 1 ) { + Com_Memcpy( &servers[j], &servers[j + 1], sizeof( servers[j] ) ); + j++; + } + ( *count )--; + break; + } + } + } +} + + +/* +==================== +LAN_GetServerCount +==================== +*/ +static int LAN_GetServerCount( int source ) { + switch ( source ) { + case AS_LOCAL: + return cls.numlocalservers; + break; + case AS_MPLAYER: + return cls.nummplayerservers; + break; + case AS_GLOBAL: + return cls.numglobalservers; + break; + case AS_FAVORITES: + return cls.numfavoriteservers; + break; + } + return 0; +} + +/* +==================== +LAN_GetLocalServerAddressString +==================== +*/ +static void LAN_GetServerAddressString( int source, int n, char *buf, int buflen ) { + switch ( source ) { + case AS_LOCAL: + if ( n >= 0 && n < MAX_OTHER_SERVERS ) { + Q_strncpyz( buf, NET_AdrToString( cls.localServers[n].adr ), buflen ); + return; + } + break; + case AS_MPLAYER: + if ( n >= 0 && n < MAX_OTHER_SERVERS ) { + Q_strncpyz( buf, NET_AdrToString( cls.mplayerServers[n].adr ), buflen ); + return; + } + break; + case AS_GLOBAL: + if ( n >= 0 && n < MAX_GLOBAL_SERVERS ) { + Q_strncpyz( buf, NET_AdrToString( cls.globalServers[n].adr ), buflen ); + return; + } + break; + case AS_FAVORITES: + if ( n >= 0 && n < MAX_OTHER_SERVERS ) { + Q_strncpyz( buf, NET_AdrToString( cls.favoriteServers[n].adr ), buflen ); + return; + } + break; + } + buf[0] = '\0'; +} + +/* +==================== +LAN_GetServerInfo +==================== +*/ +static void LAN_GetServerInfo( int source, int n, char *buf, int buflen ) { + char info[MAX_STRING_CHARS]; + serverInfo_t *server = NULL; + info[0] = '\0'; + switch ( source ) { + case AS_LOCAL: + if ( n >= 0 && n < MAX_OTHER_SERVERS ) { + server = &cls.localServers[n]; + } + break; + case AS_MPLAYER: + if ( n >= 0 && n < MAX_OTHER_SERVERS ) { + server = &cls.mplayerServers[n]; + } + break; + case AS_GLOBAL: + if ( n >= 0 && n < MAX_GLOBAL_SERVERS ) { + server = &cls.globalServers[n]; + } + break; + case AS_FAVORITES: + if ( n >= 0 && n < MAX_OTHER_SERVERS ) { + server = &cls.favoriteServers[n]; + } + break; + } + if ( server && buf ) { + buf[0] = '\0'; + Info_SetValueForKey( info, "hostname", server->hostName ); + Info_SetValueForKey( info, "mapname", server->mapName ); + Info_SetValueForKey( info, "clients", va( "%i",server->clients ) ); + Info_SetValueForKey( info, "sv_maxclients", va( "%i",server->maxClients ) ); + Info_SetValueForKey( info, "ping", va( "%i",server->ping ) ); + Info_SetValueForKey( info, "minping", va( "%i",server->minPing ) ); + Info_SetValueForKey( info, "maxping", va( "%i",server->maxPing ) ); + Info_SetValueForKey( info, "game", server->game ); + Info_SetValueForKey( info, "gametype", va( "%i",server->gameType ) ); + Info_SetValueForKey( info, "nettype", va( "%i",server->netType ) ); + Info_SetValueForKey( info, "addr", NET_AdrToString( server->adr ) ); + Info_SetValueForKey( info, "sv_allowAnonymous", va( "%i", server->allowAnonymous ) ); + Q_strncpyz( buf, info, buflen ); + } else { + if ( buf ) { + buf[0] = '\0'; + } + } +} + +/* +==================== +LAN_GetServerPing +==================== +*/ +static int LAN_GetServerPing( int source, int n ) { + serverInfo_t *server = NULL; + switch ( source ) { + case AS_LOCAL: + if ( n >= 0 && n < MAX_OTHER_SERVERS ) { + server = &cls.localServers[n]; + } + break; + case AS_MPLAYER: + if ( n >= 0 && n < MAX_OTHER_SERVERS ) { + server = &cls.mplayerServers[n]; + } + break; + case AS_GLOBAL: + if ( n >= 0 && n < MAX_GLOBAL_SERVERS ) { + server = &cls.globalServers[n]; + } + break; + case AS_FAVORITES: + if ( n >= 0 && n < MAX_OTHER_SERVERS ) { + server = &cls.favoriteServers[n]; + } + break; + } + if ( server ) { + return server->ping; + } + return -1; +} + +/* +==================== +LAN_GetServerPtr +==================== +*/ +static serverInfo_t *LAN_GetServerPtr( int source, int n ) { + switch ( source ) { + case AS_LOCAL: + if ( n >= 0 && n < MAX_OTHER_SERVERS ) { + return &cls.localServers[n]; + } + break; + case AS_MPLAYER: + if ( n >= 0 && n < MAX_OTHER_SERVERS ) { + return &cls.mplayerServers[n]; + } + break; + case AS_GLOBAL: + if ( n >= 0 && n < MAX_GLOBAL_SERVERS ) { + return &cls.globalServers[n]; + } + break; + case AS_FAVORITES: + if ( n >= 0 && n < MAX_OTHER_SERVERS ) { + return &cls.favoriteServers[n]; + } + break; + } + return NULL; +} + +/* +==================== +LAN_CompareServers +==================== +*/ +static int LAN_CompareServers( int source, int sortKey, int sortDir, int s1, int s2 ) { + int res; + serverInfo_t *server1, *server2; + + server1 = LAN_GetServerPtr( source, s1 ); + server2 = LAN_GetServerPtr( source, s2 ); + if ( !server1 || !server2 ) { + return 0; + } + + res = 0; + switch ( sortKey ) { + case SORT_HOST: + res = Q_stricmp( server1->hostName, server2->hostName ); + break; + + case SORT_MAP: + res = Q_stricmp( server1->mapName, server2->mapName ); + break; + case SORT_CLIENTS: + if ( server1->clients < server2->clients ) { + res = -1; + } else if ( server1->clients > server2->clients ) { + res = 1; + } else { + res = 0; + } + break; + case SORT_GAME: + if ( server1->gameType < server2->gameType ) { + res = -1; + } else if ( server1->gameType > server2->gameType ) { + res = 1; + } else { + res = 0; + } + break; + case SORT_PING: + if ( server1->ping < server2->ping ) { + res = -1; + } else if ( server1->ping > server2->ping ) { + res = 1; + } else { + res = 0; + } + break; + } + + if ( sortDir ) { + if ( res < 0 ) { + return 1; + } + if ( res > 0 ) { + return -1; + } + return 0; + } + return res; +} + +/* +==================== +LAN_GetPingQueueCount +==================== +*/ +static int LAN_GetPingQueueCount( void ) { + return ( CL_GetPingQueueCount() ); +} + +/* +==================== +LAN_ClearPing +==================== +*/ +static void LAN_ClearPing( int n ) { + CL_ClearPing( n ); +} + +/* +==================== +LAN_GetPing +==================== +*/ +static void LAN_GetPing( int n, char *buf, int buflen, int *pingtime ) { + CL_GetPing( n, buf, buflen, pingtime ); +} + +/* +==================== +LAN_GetPingInfo +==================== +*/ +static void LAN_GetPingInfo( int n, char *buf, int buflen ) { + CL_GetPingInfo( n, buf, buflen ); +} + +/* +==================== +LAN_MarkServerVisible +==================== +*/ +static void LAN_MarkServerVisible( int source, int n, qboolean visible ) { + if ( n == -1 ) { + int count = MAX_OTHER_SERVERS; + serverInfo_t *server = NULL; + switch ( source ) { + case AS_LOCAL: + server = &cls.localServers[0]; + break; + case AS_MPLAYER: + server = &cls.mplayerServers[0]; + break; + case AS_GLOBAL: + server = &cls.globalServers[0]; + count = MAX_GLOBAL_SERVERS; + break; + case AS_FAVORITES: + server = &cls.favoriteServers[0]; + break; + } + if ( server ) { + for ( n = 0; n < count; n++ ) { + server[n].visible = visible; + } + } + + } else { + switch ( source ) { + case AS_LOCAL: + if ( n >= 0 && n < MAX_OTHER_SERVERS ) { + cls.localServers[n].visible = visible; + } + break; + case AS_MPLAYER: + if ( n >= 0 && n < MAX_OTHER_SERVERS ) { + cls.mplayerServers[n].visible = visible; + } + break; + case AS_GLOBAL: + if ( n >= 0 && n < MAX_GLOBAL_SERVERS ) { + cls.globalServers[n].visible = visible; + } + break; + case AS_FAVORITES: + if ( n >= 0 && n < MAX_OTHER_SERVERS ) { + cls.favoriteServers[n].visible = visible; + } + break; + } + } +} + + +/* +======================= +LAN_ServerIsVisible +======================= +*/ +static int LAN_ServerIsVisible( int source, int n ) { + switch ( source ) { + case AS_LOCAL: + if ( n >= 0 && n < MAX_OTHER_SERVERS ) { + return cls.localServers[n].visible; + } + break; + case AS_MPLAYER: + if ( n >= 0 && n < MAX_OTHER_SERVERS ) { + return cls.mplayerServers[n].visible; + } + break; + case AS_GLOBAL: + if ( n >= 0 && n < MAX_GLOBAL_SERVERS ) { + return cls.globalServers[n].visible; + } + break; + case AS_FAVORITES: + if ( n >= 0 && n < MAX_OTHER_SERVERS ) { + return cls.favoriteServers[n].visible; + } + break; + } + return qfalse; +} + +/* +======================= +LAN_UpdateVisiblePings +======================= +*/ +qboolean LAN_UpdateVisiblePings( int source ) { + return CL_UpdateVisiblePings_f( source ); +} + +/* +==================== +LAN_GetServerStatus +==================== +*/ +int LAN_GetServerStatus( char *serverAddress, char *serverStatus, int maxLen ) { + return CL_ServerStatus( serverAddress, serverStatus, maxLen ); +} + +/* +==================== +CL_GetGlConfig +==================== +*/ +static void CL_GetGlconfig( glconfig_t *config ) { + *config = cls.glconfig; +} + +/* +==================== +GetClipboardData +==================== +*/ +static void GetClipboardData( char *buf, int buflen ) { + char *cbd; + + cbd = Sys_GetClipboardData(); + + if ( !cbd ) { + *buf = 0; + return; + } + + Q_strncpyz( buf, cbd, buflen ); + + Z_Free( cbd ); +} + +/* +==================== +Key_KeynumToStringBuf +==================== +*/ +static void Key_KeynumToStringBuf( int keynum, char *buf, int buflen ) { + Q_strncpyz( buf, Key_KeynumToString( keynum, qtrue ), buflen ); +} + +/* +==================== +Key_GetBindingBuf +==================== +*/ +static void Key_GetBindingBuf( int keynum, char *buf, int buflen ) { + char *value; + + value = Key_GetBinding( keynum ); + if ( value ) { + Q_strncpyz( buf, value, buflen ); + } else { + *buf = 0; + } +} + +/* +==================== +Key_GetCatcher +==================== +*/ +int Key_GetCatcher( void ) { + return cls.keyCatchers; +} + +/* +==================== +Ket_SetCatcher +==================== +*/ +void Key_SetCatcher( int catcher ) { + cls.keyCatchers = catcher; +} + + +/* +==================== +CLUI_GetCDKey +==================== +*/ +static void CLUI_GetCDKey( char *buf, int buflen ) { + cvar_t *fs; + fs = Cvar_Get( "fs_game", "", CVAR_INIT | CVAR_SYSTEMINFO ); + if ( UI_usesUniqueCDKey() && fs && fs->string[0] != 0 ) { + memcpy( buf, &cl_cdkey[16], 16 ); + buf[16] = 0; + } else { + memcpy( buf, cl_cdkey, 16 ); + buf[16] = 0; + } +} + + +/* +==================== +CLUI_SetCDKey +==================== +*/ +static void CLUI_SetCDKey( char *buf ) { + cvar_t *fs; + fs = Cvar_Get( "fs_game", "", CVAR_INIT | CVAR_SYSTEMINFO ); + if ( UI_usesUniqueCDKey() && fs && fs->string[0] != 0 ) { + memcpy( &cl_cdkey[16], buf, 16 ); + cl_cdkey[32] = 0; + // set the flag so the fle will be written at the next opportunity + cvar_modifiedFlags |= CVAR_ARCHIVE; + } else { + memcpy( cl_cdkey, buf, 16 ); + // set the flag so the fle will be written at the next opportunity + cvar_modifiedFlags |= CVAR_ARCHIVE; + } +} + + +/* +==================== +GetConfigString +==================== +*/ +static int GetConfigString( int index, char *buf, int size ) { + int offset; + + if ( index < 0 || index >= MAX_CONFIGSTRINGS ) { + return qfalse; + } + + offset = cl.gameState.stringOffsets[index]; + if ( !offset ) { + if ( size ) { + buf[0] = 0; + } + return qfalse; + } + + Q_strncpyz( buf, cl.gameState.stringData + offset, size ); + + return qtrue; +} + +/* +==================== +FloatAsInt +==================== +*/ +static int FloatAsInt( float f ) { + int temp; + + *(float *)&temp = f; + + return temp; +} + +void *VM_ArgPtr( int intValue ); +#define VMA( x ) VM_ArgPtr( args[x] ) +#define VMF( x ) ( (float *)args )[x] + +/* +==================== +CL_UISystemCalls + +The ui module is making a system call +==================== +*/ +int CL_UISystemCalls( int *args ) { + switch ( args[0] ) { + case UI_ERROR: + Com_Error( ERR_DROP, "%s", VMA( 1 ) ); + return 0; + + case UI_PRINT: + Com_Printf( "%s", VMA( 1 ) ); + return 0; + + case UI_MILLISECONDS: + return Sys_Milliseconds(); + + case UI_CVAR_REGISTER: + Cvar_Register( VMA( 1 ), VMA( 2 ), VMA( 3 ), args[4] ); + return 0; + + case UI_CVAR_UPDATE: + Cvar_Update( VMA( 1 ) ); + return 0; + + case UI_CVAR_SET: + Cvar_Set( VMA( 1 ), VMA( 2 ) ); + return 0; + + case UI_CVAR_VARIABLEVALUE: + return FloatAsInt( Cvar_VariableValue( VMA( 1 ) ) ); + + case UI_CVAR_VARIABLESTRINGBUFFER: + Cvar_VariableStringBuffer( VMA( 1 ), VMA( 2 ), args[3] ); + return 0; + + case UI_CVAR_SETVALUE: + Cvar_SetValue( VMA( 1 ), VMF( 2 ) ); + return 0; + + case UI_CVAR_RESET: + Cvar_Reset( VMA( 1 ) ); + return 0; + + case UI_CVAR_CREATE: + Cvar_Get( VMA( 1 ), VMA( 2 ), args[3] ); + return 0; + + case UI_CVAR_INFOSTRINGBUFFER: + Cvar_InfoStringBuffer( args[1], VMA( 2 ), args[3] ); + return 0; + + case UI_ARGC: + return Cmd_Argc(); + + case UI_ARGV: + Cmd_ArgvBuffer( args[1], VMA( 2 ), args[3] ); + return 0; + + case UI_CMD_EXECUTETEXT: + Cbuf_ExecuteText( args[1], VMA( 2 ) ); + return 0; + + case UI_FS_FOPENFILE: + return FS_FOpenFileByMode( VMA( 1 ), VMA( 2 ), args[3] ); + + case UI_FS_READ: + FS_Read( VMA( 1 ), args[2], args[3] ); + return 0; + +//----(SA) added + case UI_FS_SEEK: + FS_Seek( args[1], args[2], args[3] ); + return 0; +//----(SA) end + + case UI_FS_WRITE: + FS_Write( VMA( 1 ), args[2], args[3] ); + return 0; + + case UI_FS_FCLOSEFILE: + FS_FCloseFile( args[1] ); + return 0; + + case UI_FS_DELETEFILE: + return FS_Delete( VMA( 1 ) ); + + case UI_FS_GETFILELIST: + return FS_GetFileList( VMA( 1 ), VMA( 2 ), VMA( 3 ), args[4] ); + + case UI_R_REGISTERMODEL: + return re.RegisterModel( VMA( 1 ) ); + + case UI_R_REGISTERSKIN: + return re.RegisterSkin( VMA( 1 ) ); + + case UI_R_REGISTERSHADERNOMIP: + return re.RegisterShaderNoMip( VMA( 1 ) ); + + case UI_R_CLEARSCENE: + re.ClearScene(); + return 0; + + case UI_R_ADDREFENTITYTOSCENE: + re.AddRefEntityToScene( VMA( 1 ) ); + return 0; + + case UI_R_ADDPOLYTOSCENE: + re.AddPolyToScene( args[1], args[2], VMA( 3 ) ); + return 0; + + // Ridah + case UI_R_ADDPOLYSTOSCENE: + re.AddPolysToScene( args[1], args[2], VMA( 3 ), args[4] ); + return 0; + // done. + + case UI_R_ADDLIGHTTOSCENE: + re.AddLightToScene( VMA( 1 ), VMF( 2 ), VMF( 3 ), VMF( 4 ), VMF( 5 ), args[6] ); + return 0; + + case UI_R_ADDCORONATOSCENE: + re.AddCoronaToScene( VMA( 1 ), VMF( 2 ), VMF( 3 ), VMF( 4 ), VMF( 5 ), args[6], args[7] ); + return 0; + + case UI_R_RENDERSCENE: + re.RenderScene( VMA( 1 ) ); + return 0; + + case UI_R_SETCOLOR: + re.SetColor( VMA( 1 ) ); + return 0; + + case UI_R_DRAWSTRETCHPIC: + re.DrawStretchPic( VMF( 1 ), VMF( 2 ), VMF( 3 ), VMF( 4 ), VMF( 5 ), VMF( 6 ), VMF( 7 ), VMF( 8 ), args[9] ); + return 0; + + case UI_R_MODELBOUNDS: + re.ModelBounds( args[1], VMA( 2 ), VMA( 3 ) ); + return 0; + + case UI_UPDATESCREEN: + SCR_UpdateScreen(); + return 0; + + case UI_CM_LERPTAG: + return re.LerpTag( VMA( 1 ), VMA( 2 ), VMA( 3 ), args[4] ); + + case UI_S_REGISTERSOUND: +#ifdef DOOMSOUND ///// (SA) DOOMSOUND + return S_RegisterSound( VMA( 1 ) ); +#else + return S_RegisterSound( VMA( 1 ), qfalse ); +#endif ///// (SA) DOOMSOUND + + case UI_S_STARTLOCALSOUND: + S_StartLocalSound( args[1], args[2] ); + return 0; + +//----(SA) added + case UI_S_FADESTREAMINGSOUND: + S_FadeStreamingSound( VMF( 1 ), args[2], args[3] ); + return 0; + + case UI_S_FADEALLSOUNDS: + S_FadeAllSounds( VMF( 1 ), args[2] ); + return 0; +//----(SA) end + + case UI_KEY_KEYNUMTOSTRINGBUF: + Key_KeynumToStringBuf( args[1], VMA( 2 ), args[3] ); + return 0; + + case UI_KEY_GETBINDINGBUF: + Key_GetBindingBuf( args[1], VMA( 2 ), args[3] ); + return 0; + + case UI_KEY_SETBINDING: + Key_SetBinding( args[1], VMA( 2 ) ); + return 0; + + case UI_KEY_ISDOWN: + return Key_IsDown( args[1] ); + + case UI_KEY_GETOVERSTRIKEMODE: + return Key_GetOverstrikeMode(); + + case UI_KEY_SETOVERSTRIKEMODE: + Key_SetOverstrikeMode( args[1] ); + return 0; + + case UI_KEY_CLEARSTATES: + Key_ClearStates(); + return 0; + + case UI_KEY_GETCATCHER: + return Key_GetCatcher(); + + case UI_KEY_SETCATCHER: + Key_SetCatcher( args[1] ); + return 0; + + case UI_GETCLIPBOARDDATA: + GetClipboardData( VMA( 1 ), args[2] ); + return 0; + + case UI_GETCLIENTSTATE: + GetClientState( VMA( 1 ) ); + return 0; + + case UI_GETGLCONFIG: + CL_GetGlconfig( VMA( 1 ) ); + return 0; + + case UI_GETCONFIGSTRING: + return GetConfigString( args[1], VMA( 2 ), args[3] ); + + case UI_LAN_LOADCACHEDSERVERS: + LAN_LoadCachedServers(); + return 0; + + case UI_LAN_SAVECACHEDSERVERS: + LAN_SaveServersToCache(); + return 0; + + case UI_LAN_ADDSERVER: + return LAN_AddServer( args[1], VMA( 2 ), VMA( 3 ) ); + + case UI_LAN_REMOVESERVER: + LAN_RemoveServer( args[1], VMA( 2 ) ); + return 0; + + case UI_LAN_GETPINGQUEUECOUNT: + return LAN_GetPingQueueCount(); + + case UI_LAN_CLEARPING: + LAN_ClearPing( args[1] ); + return 0; + + case UI_LAN_GETPING: + LAN_GetPing( args[1], VMA( 2 ), args[3], VMA( 4 ) ); + return 0; + + case UI_LAN_GETPINGINFO: + LAN_GetPingInfo( args[1], VMA( 2 ), args[3] ); + return 0; + + case UI_LAN_GETSERVERCOUNT: + return LAN_GetServerCount( args[1] ); + + case UI_LAN_GETSERVERADDRESSSTRING: + LAN_GetServerAddressString( args[1], args[2], VMA( 3 ), args[4] ); + return 0; + + case UI_LAN_GETSERVERINFO: + LAN_GetServerInfo( args[1], args[2], VMA( 3 ), args[4] ); + return 0; + + case UI_LAN_GETSERVERPING: + return LAN_GetServerPing( args[1], args[2] ); + + case UI_LAN_MARKSERVERVISIBLE: + LAN_MarkServerVisible( args[1], args[2], args[3] ); + return 0; + + case UI_LAN_SERVERISVISIBLE: + return LAN_ServerIsVisible( args[1], args[2] ); + + case UI_LAN_UPDATEVISIBLEPINGS: + return LAN_UpdateVisiblePings( args[1] ); + + case UI_LAN_RESETPINGS: + LAN_ResetPings( args[1] ); + return 0; + + case UI_LAN_SERVERSTATUS: + return LAN_GetServerStatus( VMA( 1 ), VMA( 2 ), args[3] ); + + case UI_LAN_COMPARESERVERS: + return LAN_CompareServers( args[1], args[2], args[3], args[4], args[5] ); + + case UI_MEMORY_REMAINING: + return Hunk_MemoryRemaining(); + + case UI_GET_CDKEY: + CLUI_GetCDKey( VMA( 1 ), args[2] ); + return 0; + + case UI_SET_CDKEY: + CLUI_SetCDKey( VMA( 1 ) ); + return 0; + + case UI_R_REGISTERFONT: + re.RegisterFont( VMA( 1 ), args[2], VMA( 3 ) ); + return 0; + + case UI_MEMSET: + return (int)memset( VMA( 1 ), args[2], args[3] ); + + case UI_MEMCPY: + return (int)memcpy( VMA( 1 ), VMA( 2 ), args[3] ); + + case UI_STRNCPY: + return (int)strncpy( VMA( 1 ), VMA( 2 ), args[3] ); + + case UI_SIN: + return FloatAsInt( sin( VMF( 1 ) ) ); + + case UI_COS: + return FloatAsInt( cos( VMF( 1 ) ) ); + + case UI_ATAN2: + return FloatAsInt( atan2( VMF( 1 ), VMF( 2 ) ) ); + + case UI_SQRT: + return FloatAsInt( sqrt( VMF( 1 ) ) ); + + case UI_FLOOR: + return FloatAsInt( floor( VMF( 1 ) ) ); + + case UI_CEIL: + return FloatAsInt( ceil( VMF( 1 ) ) ); + + case UI_PC_ADD_GLOBAL_DEFINE: + return botlib_export->PC_AddGlobalDefine( VMA( 1 ) ); + case UI_PC_LOAD_SOURCE: + return botlib_export->PC_LoadSourceHandle( VMA( 1 ) ); + case UI_PC_FREE_SOURCE: + return botlib_export->PC_FreeSourceHandle( args[1] ); + case UI_PC_READ_TOKEN: + return botlib_export->PC_ReadTokenHandle( args[1], VMA( 2 ) ); + case UI_PC_SOURCE_FILE_AND_LINE: + return botlib_export->PC_SourceFileAndLine( args[1], VMA( 2 ), VMA( 3 ) ); + + case UI_S_STOPBACKGROUNDTRACK: + S_StopBackgroundTrack(); + return 0; + case UI_S_STARTBACKGROUNDTRACK: + S_StartBackgroundTrack( VMA( 1 ), VMA( 2 ), args[3] ); //----(SA) added fadeup time + return 0; + + case UI_REAL_TIME: + return Com_RealTime( VMA( 1 ) ); + + case UI_CIN_PLAYCINEMATIC: + Com_DPrintf( "UI_CIN_PlayCinematic\n" ); + return CIN_PlayCinematic( VMA( 1 ), args[2], args[3], args[4], args[5], args[6] ); + + case UI_CIN_STOPCINEMATIC: + return CIN_StopCinematic( args[1] ); + + case UI_CIN_RUNCINEMATIC: + return CIN_RunCinematic( args[1] ); + + case UI_CIN_DRAWCINEMATIC: + CIN_DrawCinematic( args[1] ); + return 0; + + case UI_CIN_SETEXTENTS: + CIN_SetExtents( args[1], args[2], args[3], args[4], args[5] ); + return 0; + + case UI_R_REMAP_SHADER: + re.RemapShader( VMA( 1 ), VMA( 2 ), VMA( 3 ) ); + return 0; + + case UI_VERIFY_CDKEY: + return CL_CDKeyValidate( VMA( 1 ), VMA( 2 ) ); + + // NERVE - SMF + case UI_CL_GETLIMBOSTRING: + return CL_GetLimboString( args[1], VMA( 2 ) ); + // -NERVE - SMF + + default: + Com_Error( ERR_DROP, "Bad UI system trap: %i", args[0] ); + + } + + return 0; +} + +/* +==================== +CL_ShutdownUI +==================== +*/ +void CL_ShutdownUI( void ) { + cls.keyCatchers &= ~KEYCATCH_UI; + cls.uiStarted = qfalse; + if ( !uivm ) { + return; + } + VM_Call( uivm, UI_SHUTDOWN ); + VM_Free( uivm ); + uivm = NULL; +} + +/* +==================== +CL_InitUI +==================== +*/ + +void CL_InitUI( void ) { + int v; + vmInterpret_t interpret; + + // load the dll or bytecode + if ( cl_connectedToPureServer != 0 ) { + // if sv_pure is set we only allow qvms to be loaded + interpret = VMI_COMPILED; + } else { + interpret = Cvar_VariableValue( "vm_ui" ); + } + +//----(SA) always dll + +#ifdef WOLF_SP_DEMO + uivm = VM_Create( "ui", CL_UISystemCalls, VMI_NATIVE ); +#else + uivm = VM_Create( "ui", CL_UISystemCalls, Cvar_VariableValue( "vm_ui" ) ); +#endif + + if ( !uivm ) { + Com_Error( ERR_FATAL, "VM_Create on UI failed" ); + } + + // sanity check + v = VM_Call( uivm, UI_GETAPIVERSION ); + if ( v != UI_API_VERSION ) { + Com_Error( ERR_FATAL, "User Interface is version %d, expected %d", v, UI_API_VERSION ); + cls.uiStarted = qfalse; + } + + // init for this gamestate +// VM_Call( uivm, UI_INIT ); + VM_Call( uivm, UI_INIT, ( cls.state >= CA_AUTHORIZING && cls.state < CA_ACTIVE ) ); +} + + +qboolean UI_usesUniqueCDKey() { + if ( uivm ) { + return ( VM_Call( uivm, UI_HASUNIQUECDKEY ) == qtrue ); + } else { + return qfalse; + } +} + +/* +==================== +UI_GameCommand + +See if the current console command is claimed by the ui +==================== +*/ +qboolean UI_GameCommand( void ) { + if ( !uivm ) { + return qfalse; + } + + return VM_Call( uivm, UI_CONSOLE_COMMAND, cls.realtime ); +} diff --git a/src/client/client.h b/src/client/client.h new file mode 100644 index 0000000..4e8fa13 --- /dev/null +++ b/src/client/client.h @@ -0,0 +1,590 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// client.h -- primary header for client + +#include "../game/q_shared.h" +#include "../qcommon/qcommon.h" +#include "../renderer/tr_public.h" +#include "../ui/ui_public.h" +#include "keys.h" +#include "snd_public.h" +#include "../cgame/cg_public.h" +#include "../game/bg_public.h" + +#define RETRANSMIT_TIMEOUT 3000 // time between connection packet retransmits + +#define LIMBOCHAT_WIDTH 140 // NERVE - SMF +#define LIMBOCHAT_HEIGHT 7 // NERVE - SMF + +// snapshots are a view of the server at a given time +typedef struct { + qboolean valid; // cleared if delta parsing was invalid + int snapFlags; // rate delayed and dropped commands + + int serverTime; // server time the message is valid for (in msec) + + int messageNum; // copied from netchan->incoming_sequence + int deltaNum; // messageNum the delta is from + int ping; // time from when cmdNum-1 was sent to time packet was reeceived + byte areamask[MAX_MAP_AREA_BYTES]; // portalarea visibility bits + + int cmdNum; // the next cmdNum the server is expecting + playerState_t ps; // complete information about the current player at this time + + int numEntities; // all of the entities that need to be presented + int parseEntitiesNum; // at the time of this snapshot + + int serverCommandNum; // execute all commands up to this before + // making the snapshot current +} clSnapshot_t; + + + +/* +============================================================================= + +the clientActive_t structure is wiped completely at every +new gamestate_t, potentially several times during an established connection + +============================================================================= +*/ + +typedef struct { + int p_cmdNumber; // cl.cmdNumber when packet was sent + int p_serverTime; // usercmd->serverTime when packet was sent + int p_realtime; // cls.realtime when packet was sent +} outPacket_t; + +// the parseEntities array must be large enough to hold PACKET_BACKUP frames of +// entities, so that when a delta compressed message arives from the server +// it can be un-deltad from the original +#define MAX_PARSE_ENTITIES 2048 + +extern int g_console_field_width; + +typedef struct { + int timeoutcount; // it requres several frames in a timeout condition + // to disconnect, preventing debugging breaks from + // causing immediate disconnects on continue + clSnapshot_t snap; // latest received from server + + int serverTime; // may be paused during play + int oldServerTime; // to prevent time from flowing bakcwards + int oldFrameServerTime; // to check tournament restarts + int serverTimeDelta; // cl.serverTime = cls.realtime + cl.serverTimeDelta + // this value changes as net lag varies + qboolean extrapolatedSnapshot; // set if any cgame frame has been forced to extrapolate + // cleared when CL_AdjustTimeDelta looks at it + qboolean newSnapshots; // set on parse of any valid packet + + gameState_t gameState; // configstrings + char mapname[MAX_QPATH]; // extracted from CS_SERVERINFO + + int parseEntitiesNum; // index (not anded off) into cl_parse_entities[] + + int mouseDx[2], mouseDy[2]; // added to by mouse events + int mouseIndex; + int joystickAxis[MAX_JOYSTICK_AXIS]; // set by joystick events + + // cgame communicates a few values to the client system + int cgameUserCmdValue; // current weapon to add to usercmd_t + int cgameUserHoldableValue; // current holdable item to add to usercmd_t //----(SA) added + float cgameSensitivity; + int cgameCld; // NERVE - SMF + + // cmds[cmdNumber] is the predicted command, [cmdNumber-1] is the last + // properly generated command + usercmd_t cmds[CMD_BACKUP]; // each mesage will send several old cmds + int cmdNumber; // incremented each frame, because multiple + // frames may need to be packed into a single packet + + outPacket_t outPackets[PACKET_BACKUP]; // information about each packet we have sent out + + // the client maintains its own idea of view angles, which are + // sent to the server each frame. It is cleared to 0 upon entering each level. + // the server sends a delta each frame which is added to the locally + // tracked view angles to account for standing on rotating objects, + // and teleport direction changes + vec3_t viewangles; + + int serverId; // included in each client message so the server + // can tell if it is for a prior map_restart + // big stuff at end of structure so most offsets are 15 bits or less + clSnapshot_t snapshots[PACKET_BACKUP]; + + entityState_t entityBaselines[MAX_GENTITIES]; // for delta compression when not in previous frame + + entityState_t parseEntities[MAX_PARSE_ENTITIES]; + + // NERVE - SMF + char limboChatMsgs[LIMBOCHAT_HEIGHT][LIMBOCHAT_WIDTH * 3 + 1]; + int limboChatPos; + // -NERVE - SMF + + qboolean cameraMode; //----(SA) added for control of input while watching cinematics + +} clientActive_t; + +extern clientActive_t cl; + +/* +============================================================================= + +the clientConnection_t structure is wiped when disconnecting from a server, +either to go to a full screen console, play a demo, or connect to a different server + +A connection can be to either a server through the network layer or a +demo through a file. + +============================================================================= +*/ + + +typedef struct { + + int clientNum; + int lastPacketSentTime; // for retransmits during connection + int lastPacketTime; // for timeouts + + netadr_t serverAddress; + int connectTime; // for connection retransmits + int connectPacketCount; // for display on connection dialog + char serverMessage[MAX_STRING_TOKENS]; // for display on connection dialog + + int challenge; // from the server to use for connecting + int checksumFeed; // from the server for checksum calculations + + // these are our reliable messages that go to the server + int reliableSequence; + int reliableAcknowledge; // the last one the server has executed + char reliableCommands[MAX_RELIABLE_COMMANDS][MAX_TOKEN_CHARS]; + + // server message (unreliable) and command (reliable) sequence + // numbers are NOT cleared at level changes, but continue to + // increase as long as the connection is valid + + // message sequence is used by both the network layer and the + // delta compression layer + int serverMessageSequence; + + // reliable messages received from server + int serverCommandSequence; + int lastExecutedServerCommand; // last server command grabbed or executed with CL_GetServerCommand + char serverCommands[MAX_RELIABLE_COMMANDS][MAX_TOKEN_CHARS]; + + // file transfer from server + fileHandle_t download; + char downloadTempName[MAX_OSPATH]; + char downloadName[MAX_OSPATH]; + int downloadNumber; + int downloadBlock; // block we are waiting for + int downloadCount; // how many bytes we got + int downloadSize; // how many bytes we got + char downloadList[MAX_INFO_STRING]; // list of paks we need to download + qboolean downloadRestart; // if true, we need to do another FS_Restart because we downloaded a pak + + // demo information + char demoName[MAX_QPATH]; + qboolean demorecording; + qboolean demoplaying; + qboolean demowaiting; // don't record until a non-delta message is received + qboolean firstDemoFrameSkipped; + fileHandle_t demofile; + + int timeDemoFrames; // counter of rendered frames + int timeDemoStart; // cls.realtime before first frame + int timeDemoBaseTime; // each frame will be at this time + frameNum * 50 + + // big stuff at end of structure so most offsets are 15 bits or less + netchan_t netchan; +} clientConnection_t; + +extern clientConnection_t clc; + +/* +================================================================== + +the clientStatic_t structure is never wiped, and is used even when +no client connection is active at all + +================================================================== +*/ + +typedef struct { + netadr_t adr; + int start; + int time; + char info[MAX_INFO_STRING]; +} ping_t; + +typedef struct { + netadr_t adr; + char hostName[MAX_NAME_LENGTH]; + char mapName[MAX_NAME_LENGTH]; + char game[MAX_NAME_LENGTH]; + int netType; + int gameType; + int clients; + int maxClients; + int minPing; + int maxPing; + int ping; + qboolean visible; + int allowAnonymous; +} serverInfo_t; + +typedef struct { + byte ip[4]; + unsigned short port; +} serverAddress_t; + +typedef struct { + connstate_t state; // connection status + int keyCatchers; // bit flags + + qboolean cddialog; // bring up the cd needed dialog next frame + qboolean endgamemenu; // bring up the end game credits menu next frame + + char servername[MAX_OSPATH]; // name of server from original connect (used by reconnect) + + // when the server clears the hunk, all of these must be restarted + qboolean rendererStarted; + qboolean soundStarted; + qboolean soundRegistered; + qboolean uiStarted; + qboolean cgameStarted; + + int framecount; + int frametime; // msec since last frame + + int realtime; // ignores pause + int realFrametime; // ignoring pause, so console always works + + int numlocalservers; + serverInfo_t localServers[MAX_OTHER_SERVERS]; + + int numglobalservers; + serverInfo_t globalServers[MAX_GLOBAL_SERVERS]; + // additional global servers + int numGlobalServerAddresses; + serverAddress_t globalServerAddresses[MAX_GLOBAL_SERVERS]; + + int numfavoriteservers; + serverInfo_t favoriteServers[MAX_OTHER_SERVERS]; + + int nummplayerservers; + serverInfo_t mplayerServers[MAX_OTHER_SERVERS]; + + int pingUpdateSource; // source currently pinging or updating + + int masterNum; + + // update server info + netadr_t updateServer; + char updateChallenge[MAX_TOKEN_CHARS]; + char updateInfoString[MAX_INFO_STRING]; + + netadr_t authorizeServer; + + // rendering info + glconfig_t glconfig; + qhandle_t charSetShader; + qhandle_t whiteShader; + qhandle_t consoleShader; + qhandle_t consoleShader2; //----(SA) added + +} clientStatic_t; + +extern clientStatic_t cls; + +//============================================================================= + +extern vm_t *cgvm; // interface to cgame dll or vm +extern vm_t *uivm; // interface to ui dll or vm +extern refexport_t re; // interface to refresh .dll + + +// +// cvars +// +extern cvar_t *cl_nodelta; +extern cvar_t *cl_debugMove; +extern cvar_t *cl_noprint; +extern cvar_t *cl_timegraph; +extern cvar_t *cl_maxpackets; +extern cvar_t *cl_packetdup; +extern cvar_t *cl_shownet; +extern cvar_t *cl_showSend; +extern cvar_t *cl_timeNudge; +extern cvar_t *cl_showTimeDelta; +extern cvar_t *cl_freezeDemo; + +extern cvar_t *cl_yawspeed; +extern cvar_t *cl_pitchspeed; +extern cvar_t *cl_run; +extern cvar_t *cl_anglespeedkey; + +extern cvar_t *cl_recoilPitch; // RF + +extern cvar_t *cl_sensitivity; +extern cvar_t *cl_freelook; + +extern cvar_t *cl_mouseAccel; +extern cvar_t *cl_showMouseRate; + +extern cvar_t *m_pitch; +extern cvar_t *m_yaw; +extern cvar_t *m_forward; +extern cvar_t *m_side; +extern cvar_t *m_filter; + +extern cvar_t *cl_timedemo; + +extern cvar_t *cl_activeAction; + +extern cvar_t *cl_allowDownload; +extern cvar_t *cl_conXOffset; +extern cvar_t *cl_inGameVideo; + +extern cvar_t *cl_missionStats; +extern cvar_t *cl_waitForFire; + +// NERVE - SMF - localization +extern cvar_t *cl_language; +// -NERVE - SMF +//================================================= + +// +// cl_main +// + +void CL_Init( void ); +void CL_FlushMemory( void ); +void CL_ShutdownAll( void ); +void CL_AddReliableCommand( const char *cmd ); + +void CL_StartHunkUsers( void ); + +void CL_Disconnect_f( void ); +void CL_GetChallengePacket( void ); +void CL_Vid_Restart_f( void ); +void CL_Snd_Restart_f( void ); +void CL_StartDemoLoop( void ); +void CL_NextDemo( void ); +void CL_ReadDemoMessage( void ); + +void CL_InitDownloads( void ); +void CL_NextDownload( void ); + +void CL_GetPing( int n, char *buf, int buflen, int *pingtime ); +void CL_GetPingInfo( int n, char *buf, int buflen ); +void CL_ClearPing( int n ); +int CL_GetPingQueueCount( void ); + +void CL_ShutdownRef( void ); +void CL_InitRef( void ); +qboolean CL_CDKeyValidate( const char *key, const char *checksum ); +int CL_ServerStatus( char *serverAddress, char *serverStatusString, int maxLen ); + +void CL_AddToLimboChat( const char *str ); // NERVE - SMF +qboolean CL_GetLimboString( int index, char *buf ); // NERVE - SMF + +// +// cl_input +// +typedef struct { + int down[2]; // key nums holding it down + unsigned downtime; // msec timestamp + unsigned msec; // msec down this frame if both a down and up happened + qboolean active; // current state + qboolean wasPressed; // set when down, not cleared when up +} kbutton_t; + +typedef enum { + KB_LEFT, + KB_RIGHT, + KB_FORWARD, + KB_BACK, + KB_LOOKUP, + KB_LOOKDOWN, + KB_MOVELEFT, + KB_MOVERIGHT, + KB_STRAFE, + KB_SPEED, + KB_UP, + KB_DOWN, + KB_BUTTONS0, + KB_BUTTONS1, + KB_BUTTONS2, + KB_BUTTONS3, + KB_BUTTONS4, + KB_BUTTONS5, + KB_BUTTONS6, + KB_BUTTONS7, + KB_WBUTTONS0, + KB_WBUTTONS1, + KB_WBUTTONS2, + KB_WBUTTONS3, + KB_WBUTTONS4, + KB_WBUTTONS5, + KB_WBUTTONS6, + KB_WBUTTONS7, + KB_MLOOK, + KB_KICK, + + NUM_BUTTONS +} kbuttons_t; + + +void CL_ClearKeys( void ); + +void CL_InitInput( void ); +void CL_SendCmd( void ); +void CL_ClearState( void ); +void CL_ReadPackets( void ); + +void CL_WritePacket( void ); +void IN_CenterView( void ); +void IN_Notebook( void ); +void IN_Help( void ); + +//----(SA) salute +void IN_Salute( void ); +//----(SA) + +void CL_VerifyCode( void ); + +float CL_KeyState( kbutton_t *key ); +char *Key_KeynumToString( int keynum, qboolean bTranslate ); + +// +// cl_parse.c +// +extern int cl_connectedToPureServer; + +void CL_SystemInfoChanged( void ); +void CL_ParseServerMessage( msg_t *msg ); + +//==================================================================== + +void CL_ServerInfoPacket( netadr_t from, msg_t *msg ); +void CL_LocalServers_f( void ); +void CL_GlobalServers_f( void ); +void CL_FavoriteServers_f( void ); +void CL_Ping_f( void ); +qboolean CL_UpdateVisiblePings_f( int source ); + + +// +// console +// +void Con_DrawCharacter( int cx, int line, int num ); + +void Con_CheckResize( void ); +void Con_Init( void ); +void Con_Clear_f( void ); +void Con_ToggleConsole_f( void ); +void Con_DrawNotify( void ); +void Con_ClearNotify( void ); +void Con_RunConsole( void ); +void Con_DrawConsole( void ); +void Con_PageUp( void ); +void Con_PageDown( void ); +void Con_Top( void ); +void Con_Bottom( void ); +void Con_Close( void ); + + +// +// cl_scrn.c +// +void SCR_Init( void ); +void SCR_UpdateScreen( void ); + +void SCR_DebugGraph( float value, int color ); + +int SCR_GetBigStringWidth( const char *str ); // returns in virtual 640x480 coordinates + +void SCR_AdjustFrom640( float *x, float *y, float *w, float *h ); +void SCR_FillRect( float x, float y, float width, float height, + const float *color ); +void SCR_DrawPic( float x, float y, float width, float height, qhandle_t hShader ); +void SCR_DrawNamedPic( float x, float y, float width, float height, const char *picname ); + +void SCR_DrawBigString( int x, int y, const char *s, float alpha ); // draws a string with embedded color control characters with fade +void SCR_DrawBigStringColor( int x, int y, const char *s, vec4_t color ); // ignores embedded color control characters +void SCR_DrawSmallStringExt( int x, int y, const char *string, float *setColor, qboolean forceColor ); +void SCR_DrawSmallChar( int x, int y, int ch ); + + +// +// cl_cin.c +// + +void CL_PlayCinematic_f( void ); +void SCR_DrawCinematic( void ); +void SCR_RunCinematic( void ); +void SCR_StopCinematic( void ); +int CIN_PlayCinematic( const char *arg0, int xpos, int ypos, int width, int height, int bits ); +e_status CIN_StopCinematic( int handle ); +e_status CIN_RunCinematic( int handle ); +void CIN_DrawCinematic( int handle ); +void CIN_SetExtents( int handle, int x, int y, int w, int h ); +void CIN_SetLooping( int handle, qboolean loop ); +void CIN_UploadCinematic( int handle ); +void CIN_CloseAllVideos( void ); + +// +// cl_cgame.c +// +void CL_InitCGame( void ); +void CL_ShutdownCGame( void ); +qboolean CL_GameCommand( void ); +void CL_CGameRendering( stereoFrame_t stereo ); +void CL_SetCGameTime( void ); +void CL_FirstSnapshot( void ); +void CL_ShaderStateChanged( void ); +void CL_UpdateLevelHunkUsage( void ); +// +// cl_ui.c +// +void CL_InitUI( void ); +void CL_ShutdownUI( void ); +int Key_GetCatcher( void ); +void Key_SetCatcher( int catcher ); +void LAN_LoadCachedServers(); +void LAN_SaveServersToCache(); + + +// +// cl_net_chan.c +// +void CL_Netchan_Transmit( netchan_t *chan, msg_t* msg ); //int length, const byte *data ); +void CL_Netchan_TransmitNextFragment( netchan_t *chan ); +qboolean CL_Netchan_Process( netchan_t *chan, msg_t *msg ); diff --git a/src/client/keys.h b/src/client/keys.h new file mode 100644 index 0000000..961b2d7 --- /dev/null +++ b/src/client/keys.h @@ -0,0 +1,66 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +#include "../ui/keycodes.h" + +#define MAX_KEYS 256 + +typedef struct { + qboolean down; + int repeats; // if > 1, it is autorepeating + char *binding; +} qkey_t; + +extern qboolean key_overstrikeMode; +extern qkey_t keys[MAX_KEYS]; + +// NOTE TTimo the declaration of field_t and Field_Clear is now in qcommon/qcommon.h + +void Field_KeyDownEvent( field_t *edit, int key ); +void Field_CharEvent( field_t *edit, int ch ); +void Field_Draw( field_t *edit, int x, int y, int width, qboolean showCursor ); +void Field_BigDraw( field_t *edit, int x, int y, int width, qboolean showCursor ); + +#define COMMAND_HISTORY 32 +extern field_t historyEditLines[COMMAND_HISTORY]; + +extern field_t g_consoleField; +extern field_t chatField; +extern qboolean anykeydown; +extern qboolean chat_team; +extern qboolean chat_limbo; // NERVE - SMF +extern int chat_playerNum; + +void Key_WriteBindings( fileHandle_t f ); +void Key_SetBinding( int keynum, const char *binding ); +char *Key_GetBinding( int keynum ); +qboolean Key_IsDown( int keynum ); +qboolean Key_GetOverstrikeMode( void ); +void Key_SetOverstrikeMode( qboolean state ); +void Key_ClearStates( void ); +int Key_GetKey( const char *binding ); diff --git a/src/client/snd_adpcm.c b/src/client/snd_adpcm.c new file mode 100644 index 0000000..26ebd2b --- /dev/null +++ b/src/client/snd_adpcm.c @@ -0,0 +1,142 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +#include "snd_local.h" + + +void S_AdpcmEncode( short indata[], char outdata[], int len, struct adpcm_state *state ) { + // LordHavoc: removed 4-clause BSD code for Intel ADPCM codec +} + + +void S_AdpcmDecode( const char indata[], short *outdata, int len, struct adpcm_state *state ) { + // LordHavoc: removed 4-clause BSD code for Intel ADPCM codec +} + + +/* +==================== +S_AdpcmMemoryNeeded + +Returns the amount of memory (in bytes) needed to store the samples in out internal adpcm format +==================== +*/ +int S_AdpcmMemoryNeeded( const wavinfo_t *info ) { + float scale; + int scaledSampleCount; + int sampleMemory; + int blockCount; + int headerMemory; + + // determine scale to convert from input sampling rate to desired sampling rate + scale = (float)info->rate / dma.speed; + + // calc number of samples at playback sampling rate + scaledSampleCount = info->samples / scale; + + // calc memory need to store those samples using ADPCM at 4 bits per sample + sampleMemory = scaledSampleCount / 2; + + // calc number of sample blocks needed of PAINTBUFFER_SIZE + blockCount = scaledSampleCount / PAINTBUFFER_SIZE; + if ( scaledSampleCount % PAINTBUFFER_SIZE ) { + blockCount++; + } + + // calc memory needed to store the block headers + headerMemory = blockCount * sizeof( adpcm_state_t ); + + return sampleMemory + headerMemory; +} + + +/* +==================== +S_AdpcmGetSamples +==================== +*/ +void S_AdpcmGetSamples( sndBuffer *chunk, short *to ) { + adpcm_state_t state; + byte *out; + + // get the starting state from the block header + state.index = chunk->adpcm.index; + state.sample = chunk->adpcm.sample; + + out = (byte *)chunk->sndChunk; + // get samples + S_AdpcmDecode( (const char*)out, to, SND_CHUNK_SIZE_BYTE * 2, &state ); //DAJ added (const char*) +} + + +/* +==================== +S_AdpcmEncodeSound +==================== +*/ +void S_AdpcmEncodeSound( sfx_t *sfx, short *samples ) { + adpcm_state_t state; + int inOffset; + int count; + int n; + sndBuffer *newchunk, *chunk; + byte *out; + + inOffset = 0; + count = sfx->soundLength; + state.index = 0; + state.sample = samples[0]; + + chunk = NULL; + while ( count ) { + n = count; + if ( n > SND_CHUNK_SIZE_BYTE * 2 ) { + n = SND_CHUNK_SIZE_BYTE * 2; + } + + newchunk = SND_malloc(); + if ( sfx->soundData == NULL ) { + sfx->soundData = newchunk; + } else { + chunk->next = newchunk; + } + chunk = newchunk; + + // output the header + chunk->adpcm.index = state.index; + chunk->adpcm.sample = state.sample; + + out = (byte *)chunk->sndChunk; + + // encode the samples + S_AdpcmEncode( samples + inOffset, (char*)out, n, &state ); //DAJ added (char*) + + inOffset += n; + count -= n; + } +} \ No newline at end of file diff --git a/src/client/snd_dma.c b/src/client/snd_dma.c new file mode 100644 index 0000000..c13df17 --- /dev/null +++ b/src/client/snd_dma.c @@ -0,0 +1,2574 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: snd_dma.c + * + * desc: main control for any streaming sound output device + * + * $Archive: /Wolf5/src/client/snd_dma.c $ + * + *****************************************************************************/ + +#include "snd_local.h" +#include "client.h" + +void S_Play_f( void ); +void S_SoundList_f( void ); +void S_Music_f( void ); +void S_QueueMusic_f( void ); +void S_StreamingSound_f( void ); +void S_ClearSounds( qboolean clearStreaming, qboolean clearMusic ); //----(SA) modified + +void S_Update_Mix(); +void S_StopAllSounds( void ); +void S_UpdateStreamingSounds( void ); + +snd_t snd; // globals for sound + +// Ridah, streaming sounds +// !! NOTE: the first streaming sound is always the music +streamingSound_t streamingSounds[MAX_STREAMING_SOUNDS]; +int numStreamingSounds = 0; + + +void *crit; + + +// ======================================================================= +// Internal sound data & structures +// ======================================================================= + +// only begin attenuating sound volumes when outside the FULLVOLUME range +#define SOUND_FULLVOLUME 80 + +#define SOUND_ATTENUATE 0.0008f +#define SOUND_RANGE_DEFAULT 1250 + +channel_t s_channels[MAX_CHANNELS]; +channel_t loop_channels[MAX_CHANNELS]; +int numLoopChannels; + +dma_t dma; + +static int listener_number; +static vec3_t listener_origin; +static vec3_t listener_axis[3]; + +int s_soundtime; // sample PAIRS +int s_paintedtime; // sample PAIRS + +// MAX_SFX may be larger than MAX_SOUNDS because +// of custom player sounds +#define MAX_SFX 4096 +sfx_t s_knownSfx[MAX_SFX]; + + +cvar_t *s_volume; +cvar_t *s_testsound; +cvar_t *s_khz; +cvar_t *s_show; +cvar_t *s_mixahead; +cvar_t *s_mixPreStep; +cvar_t *s_musicVolume; +cvar_t *s_currentMusic; //----(SA) added +cvar_t *s_separation; +cvar_t *s_doppler; +cvar_t *s_mute; // (SA) for DM so he can 'toggle' sound on/off without disturbing volume levels +cvar_t *s_defaultsound; // (SA) added to silence the default beep sound if desired +cvar_t *cl_cacheGathering; // Ridah +cvar_t *s_wavonly; +cvar_t *s_debugMusic; //----(SA) added + + +// Rafael +cvar_t *s_nocompressed; + +// for streaming sounds +int s_rawend[MAX_STREAMING_SOUNDS]; +int s_rawpainted[MAX_STREAMING_SOUNDS]; +portable_samplepair_t s_rawsamples[MAX_STREAMING_SOUNDS][MAX_RAW_SAMPLES]; +// RF, store the volumes, since now they get adjusted at time of painting, so we can extract talking data first +portable_samplepair_t s_rawVolume[MAX_STREAMING_SOUNDS]; + + +/* +================ +S_SoundInfo_f +================ +*/ +void S_SoundInfo_f( void ) { + Com_Printf( "----- Sound Info -----\n" ); + if ( !snd.s_soundStarted ) { + Com_Printf( "sound system not started\n" ); + } else { + if ( snd.s_soundMute ) { + Com_Printf( "sound system is muted\n" ); + } + + Com_Printf( "%5d stereo\n", dma.channels - 1 ); + Com_Printf( "%5d samples\n", dma.samples ); + Com_Printf( "%5d samplebits\n", dma.samplebits ); + Com_Printf( "%5d submission_chunk\n", dma.submission_chunk ); + Com_Printf( "%5d speed\n", dma.speed ); + Com_Printf( "0x%x dma buffer\n", dma.buffer ); + if ( streamingSounds[0].file ) { + Com_Printf( "Background file: %s\n", streamingSounds[0].loop ); + } else { + Com_Printf( "No background file.\n" ); + } + + } + Com_Printf( "----------------------\n" ); +} + +void S_ChannelSetup(); + +/* +================ +S_Init +================ +*/ +void S_Init( void ) { + cvar_t *cv; + qboolean r; + + Com_Printf( "\n------- sound initialization -------\n" ); + + s_mute = Cvar_Get( "s_mute", "0", CVAR_TEMP ); //----(SA) added + s_volume = Cvar_Get( "s_volume", "0.8", CVAR_ARCHIVE ); + s_musicVolume = Cvar_Get( "s_musicvolume", "0.25", CVAR_ARCHIVE ); + s_currentMusic = Cvar_Get( "s_currentMusic", "", CVAR_ROM ); + s_separation = Cvar_Get( "s_separation", "0.5", CVAR_ARCHIVE ); + s_doppler = Cvar_Get( "s_doppler", "1", CVAR_ARCHIVE ); + s_khz = Cvar_Get( "s_khz", "22", CVAR_ARCHIVE ); + s_mixahead = Cvar_Get( "s_mixahead", "0.5", CVAR_ARCHIVE ); //DAJ was 0.2 + s_debugMusic = Cvar_Get( "s_debugMusic", "0", CVAR_TEMP ); + + s_mixPreStep = Cvar_Get( "s_mixPreStep", "0.05", CVAR_ARCHIVE ); + s_show = Cvar_Get( "s_show", "0", CVAR_CHEAT ); + s_testsound = Cvar_Get( "s_testsound", "0", CVAR_CHEAT ); + s_defaultsound = Cvar_Get( "s_defaultsound", "0", CVAR_ARCHIVE ); + s_wavonly = Cvar_Get( "s_wavonly", "0", CVAR_ARCHIVE | CVAR_LATCH ); + // Ridah + cl_cacheGathering = Cvar_Get( "cl_cacheGathering", "0", 0 ); + + // Rafael + s_nocompressed = Cvar_Get( "s_nocompressed", "0", CVAR_INIT ); + + cv = Cvar_Get( "s_initsound", "1", 0 ); + if ( !cv->integer ) { + Com_Printf( "not initializing.\n" ); + Com_Printf( "------------------------------------\n" ); + return; + } + + crit = Sys_InitializeCriticalSection(); + + Cmd_AddCommand( "play", S_Play_f ); + Cmd_AddCommand( "music", S_Music_f ); + Cmd_AddCommand( "queuemusic", S_QueueMusic_f ); + Cmd_AddCommand( "streamingsound", S_StreamingSound_f ); + Cmd_AddCommand( "s_list", S_SoundList_f ); + Cmd_AddCommand( "s_info", S_SoundInfo_f ); + Cmd_AddCommand( "s_stop", S_StopAllSounds ); + + r = SNDDMA_Init(); + Com_Printf( "------------------------------------\n" ); + + if ( r ) { + Com_Memset( &snd, 0, sizeof( snd ) ); +// Com_Memset(snd.sfxHash, 0, sizeof(sfx_t *)*LOOP_HASH); + + snd.s_soundStarted = 1; + snd.s_soundMute = 1; +// snd.s_numSfx = 0; +// snd.volTarget = 1.0f; // full volume + snd.volTarget = 0.0f; // full volume + + s_soundtime = 0; + s_paintedtime = 0; + + S_StopAllSounds(); + + S_SoundInfo_f(); + S_ChannelSetup(); + } + +} + +/* +================ +S_ChannelFree +================ +*/ +void S_ChannelFree( channel_t *v ) { + v->thesfx = NULL; + v->threadReady = qfalse; +#ifdef _DEBUG + if ( v > &s_channels[MAX_CHANNELS] || v < &s_channels[0] ) { + Com_DPrintf( "s_channel OUT OF BOUNDS\n" ); + return; + } +#endif + *(channel_t **)snd.endflist = v; + snd.endflist = v; + *(channel_t **)v = NULL; +} + +/* +================ +S_ChannelMalloc +================ +*/ +channel_t* S_ChannelMalloc() { + channel_t *v; + if ( snd.freelist == NULL ) { + return NULL; + } + // RF, be careful not to lose our freelist + if ( *(channel_t **)snd.freelist == NULL ) { + return NULL; + } +#ifdef _DEBUG + if ( *(channel_t **)snd.freelist > &s_channels[MAX_CHANNELS] || *(channel_t **)snd.freelist < &s_channels[0] ) { //DAJ extra check + Com_DPrintf( "s_channel OUT OF BOUNDS\n" ); + return NULL; + } +#endif + v = snd.freelist; + snd.freelist = *(channel_t **)snd.freelist; + v->allocTime = Sys_Milliseconds(); + return v; +} + +/* +================ +S_ChannelSetup +================ +*/ +void S_ChannelSetup() { + channel_t *p, *q; + + // clear all the sounds so they don't + Com_Memset( s_channels, 0, sizeof( s_channels ) ); + + p = s_channels;; + q = p + MAX_CHANNELS; + while ( --q > p ) { + *(channel_t **)q = q - 1; + } + + snd.endflist = q; + *(channel_t **)q = NULL; + snd.freelist = p + MAX_CHANNELS - 1; + Com_DPrintf( "Channel memory manager started\n" ); +} + +/* +================ +S_Shutdown +================ +*/ +void S_Shutdown( void ) { + if ( !snd.s_soundStarted ) { + return; + } + + Sys_EnterCriticalSection( crit ); + + SNDDMA_Shutdown(); + + snd.s_soundStarted = 0; + snd.s_soundMute = 1; + + Cmd_RemoveCommand( "play" ); + Cmd_RemoveCommand( "music" ); + Cmd_RemoveCommand( "stopsound" ); + Cmd_RemoveCommand( "soundlist" ); + Cmd_RemoveCommand( "soundinfo" ); +} + +/* +================ +S_HashSFXName + +return a hash value for the sfx name +================ +*/ +static long S_HashSFXName( const char *name ) { + int i; + long hash; + char letter; + + hash = 0; + i = 0; + while ( name[i] != '\0' ) { + letter = tolower( name[i] ); + if ( letter == '.' ) { + break; // don't include extension + } + if ( letter == '\\' ) { + letter = '/'; // damn path names + } + hash += (long)( letter ) * ( i + 119 ); + i++; + } + hash &= ( LOOP_HASH - 1 ); + return hash; +} + +/* +================== +S_FindName + +Will allocate a new sfx if it isn't found +================== +*/ +static sfx_t *S_FindName( const char *name ) { + int i; + int hash; + + sfx_t *sfx; + + if ( !name ) { + //Com_Error (ERR_FATAL, "S_FindName: NULL\n"); + name = "*default*"; + } + if ( !name[0] ) { + //Com_Error (ERR_FATAL, "S_FindName: empty name\n"); + name = "*default*"; + } + + if ( strlen( name ) >= MAX_QPATH ) { + Com_Error( ERR_FATAL, "Sound name too long: %s", name ); + } + + // Ridah, caching + if ( cl_cacheGathering->integer ) { + Cbuf_ExecuteText( EXEC_NOW, va( "cache_usedfile sound %s\n", name ) ); + } + + hash = S_HashSFXName( name ); + + sfx = snd.sfxHash[hash]; + // see if already loaded + while ( sfx ) { + if ( !Q_stricmp( sfx->soundName, name ) ) { + return sfx; + } + sfx = sfx->next; + } + + // find a free sfx + for ( i = 0 ; i < snd.s_numSfx ; i++ ) { + if ( !s_knownSfx[i].soundName[0] ) { + break; + } + } + + if ( i == snd.s_numSfx ) { + if ( snd.s_numSfx == MAX_SFX ) { + Com_Error( ERR_FATAL, "S_FindName: out of sfx_t" ); + } + snd.s_numSfx++; + } + + sfx = &s_knownSfx[i]; + Com_Memset( sfx, 0, sizeof( *sfx ) ); + strcpy( sfx->soundName, name ); + + sfx->next = snd.sfxHash[hash]; + snd.sfxHash[hash] = sfx; + + return sfx; +} + +/* +================= +S_DefaultSound +================= +*/ +void S_DefaultSound( sfx_t *sfx ) { + + int i; + + if ( s_defaultsound->integer ) { + sfx->soundLength = 512; + } else { + sfx->soundLength = 8; + } + + sfx->soundData = SND_malloc(); + sfx->soundData->next = NULL; + + if ( s_defaultsound->integer ) { + for ( i = 0 ; i < sfx->soundLength ; i++ ) { + sfx->soundData->sndChunk[i] = i; + } + } else { + for ( i = 0 ; i < sfx->soundLength ; i++ ) { + sfx->soundData->sndChunk[i] = 0; + } + } +} + +/* +=================== +S_DisableSounds + +Disables sounds until the next S_BeginRegistration. +This is called when the hunk is cleared and the sounds +are no longer valid. +=================== +*/ +void S_DisableSounds( void ) { + S_StopAllSounds(); + snd.s_soundMute = 1; +} + +/* +===================== +S_BeginRegistration + +===================== +*/ +void S_BeginRegistration( void ) { + sfx_t *sfx; + + snd.s_soundMute = 0; // we can play again + + if ( snd.s_numSfx == 0 ) { + SND_setup(); + + snd.s_numSfx = 0; + Com_Memset( s_knownSfx, 0, sizeof( s_knownSfx ) ); + Com_Memset( snd.sfxHash, 0, sizeof( sfx_t * ) * LOOP_HASH ); + + sfx = S_FindName( "***DEFAULT***" ); + S_DefaultSound( sfx ); + } +} + +/* +================== +S_RegisterSound + +Creates a default buzz sound if the file can't be loaded +================== +*/ +sfxHandle_t S_RegisterSound( const char *name, qboolean compressed ) { + sfx_t *sfx; + + compressed = qfalse; + if ( !snd.s_soundStarted ) { + return 0; + } + + if ( strlen( name ) >= MAX_QPATH ) { + Com_Printf( "Sound name exceeds MAX_QPATH\n" ); + return 0; + } + + sfx = S_FindName( name ); + if ( sfx->soundData ) { + if ( sfx->defaultSound ) { + if ( com_developer->integer ) { + Com_Printf( S_COLOR_YELLOW "WARNING: could not find %s - using default\n", sfx->soundName ); + } + return 0; + } + return sfx - s_knownSfx; + } + + sfx->inMemory = qfalse; + sfx->soundCompressed = compressed; + +// if (!compressed) { + S_memoryLoad( sfx ); +// } + + if ( sfx->defaultSound ) { + if ( com_developer->integer ) { + Com_Printf( S_COLOR_YELLOW "WARNING: could not find %s - using default\n", sfx->soundName ); + } + return 0; + } + + return sfx - s_knownSfx; +} + +/* +================= +S_memoryLoad +================= +*/ +void S_memoryLoad( sfx_t *sfx ) { + // load the sound file + if ( !S_LoadSound( sfx ) ) { +// Com_Printf( S_COLOR_YELLOW "WARNING: couldn't load sound: %s\n", sfx->soundName ); + sfx->defaultSound = qtrue; + } + sfx->inMemory = qtrue; +} + +//============================================================================= + +/* +================= +S_SpatializeOrigin + +Used for spatializing s_channels +================= +*/ +void S_SpatializeOrigin( vec3_t origin, int master_vol, int *left_vol, int *right_vol, float range ) { + vec_t dot; + vec_t dist; + vec_t lscale, rscale, scale; + vec3_t source_vec; + vec3_t vec; + +// const float dist_mult = SOUND_ATTENUATE; + float dist_mult, dist_fullvol; + + dist_fullvol = range * 0.064f; // default range of 1250 gives 80 + dist_mult = dist_fullvol * 0.00001f; // default range of 1250 gives .0008 +// dist_mult = range*0.00000064f; // default range of 1250 gives .0008 + + // calculate stereo seperation and distance attenuation + VectorSubtract( origin, listener_origin, source_vec ); + + dist = VectorNormalize( source_vec ); +// dist -= SOUND_FULLVOLUME; + dist -= dist_fullvol; + if ( dist < 0 ) { + dist = 0; // close enough to be at full volume + + } + if ( dist ) { + dist = dist / range; // FIXME: lose the divide again + } +// dist *= dist_mult; // different attenuation levels + + VectorRotate( source_vec, listener_axis, vec ); + + dot = -vec[1]; + + if ( dma.channels == 1 ) { // no attenuation = no spatialization + rscale = 1.0; + lscale = 1.0; + } else + { + rscale = 0.5 * ( 1.0 + dot ); + lscale = 0.5 * ( 1.0 - dot ); + //rscale = s_separation->value + ( 1.0 - s_separation->value ) * dot; + //lscale = s_separation->value - ( 1.0 - s_separation->value ) * dot; + if ( rscale < 0 ) { + rscale = 0; + } + if ( lscale < 0 ) { + lscale = 0; + } + } + + // add in distance effect + scale = ( 1.0 - dist ) * rscale; + *right_vol = ( master_vol * scale ); + if ( *right_vol < 0 ) { + *right_vol = 0; + } + + scale = ( 1.0 - dist ) * lscale; + *left_vol = ( master_vol * scale ); + if ( *left_vol < 0 ) { + *left_vol = 0; + } +} + +/* +==================== +S_StartSound + +Validates the parms and queues the sound up +if pos is NULL, the sound will be dynamically sourced from the entity +Entchannel 0 will never override a playing sound + + flags: (currently apply only to non-looping sounds) + SND_NORMAL 0 - (default) allow sound to be cut off only by the same sound on this channel + SND_OKTOCUT 0x001 - allow sound to be cut off by any following sounds on this channel + SND_REQUESTCUT 0x002 - allow sound to be cut off by following sounds on this channel only for sounds who request cutoff + SND_CUTOFF 0x004 - cut off sounds on this channel that are marked 'SND_REQUESTCUT' + SND_CUTOFF_ALL 0x008 - cut off all sounds on this channel +==================== +*/ +void S_ThreadStartSoundEx( vec3_t origin, int entityNum, int entchannel, sfxHandle_t sfxHandle, int flags ); + +void S_StartSoundEx( vec3_t origin, int entityNum, int entchannel, sfxHandle_t sfxHandle, int flags ) { + if ( !snd.s_soundStarted || snd.s_soundMute || ( cls.state != CA_ACTIVE && cls.state != CA_DISCONNECTED ) ) { + return; + } + + // RF, we have lots of NULL sounds using up valuable channels, so just ignore them + if ( !sfxHandle && entchannel != CHAN_WEAPON ) { // let null weapon sounds try to play. they kill any weapon sounds playing when a guy dies + return; + } + + // RF, make the call now, or else we could override following streaming sounds in the same frame, due to the delay + S_ThreadStartSoundEx( origin, entityNum, entchannel, sfxHandle, flags ); +/* + if (snd.tart < MAX_PUSHSTACK) { + sfx_t *sfx; + if (origin) { + VectorCopy( origin, snd.pushPop[snd.tart].origin ); + snd.pushPop[snd.tart].fixedOrigin = qtrue; + } else { + snd.pushPop[snd.tart].fixedOrigin = qfalse; + } + snd.pushPop[snd.tart].entityNum = entityNum; + snd.pushPop[snd.tart].entityChannel = entchannel; + snd.pushPop[snd.tart].sfx = sfxHandle; + snd.pushPop[snd.tart].flags = flags; + sfx = &s_knownSfx[ sfxHandle ]; + + if (sfx->inMemory == qfalse) { + S_memoryLoad(sfx); + } + + snd.tart++; + } +*/ +} + +void S_ThreadStartSoundEx( vec3_t origin, int entityNum, int entchannel, sfxHandle_t sfxHandle, int flags ) { + channel_t *ch; + sfx_t *sfx; + int i, oldest, chosen; + + chosen = -1; + if ( !snd.s_soundStarted || snd.s_soundMute ) { + return; + } + + if ( !origin && ( entityNum < 0 || entityNum > MAX_GENTITIES ) ) { + Com_Error( ERR_DROP, "S_StartSound: bad entitynum %i", entityNum ); + } + + if ( sfxHandle < 0 || sfxHandle >= snd.s_numSfx ) { + Com_Printf( S_COLOR_YELLOW, "S_StartSound: handle %i out of range\n", sfxHandle ); + return; + } + + sfx = &s_knownSfx[ sfxHandle ]; + + if ( s_show->integer == 1 ) { + Com_Printf( "%i : %s\n", s_paintedtime, sfx->soundName ); + } + +// Com_Printf("playing %s\n", sfx->soundName); + + sfx->lastTimeUsed = Sys_Milliseconds(); + + // check for a streaming sound that this entity is playing in this channel + // kill it if it exists + if ( entityNum >= 0 ) { + for ( i = 1; i < MAX_STREAMING_SOUNDS; i++ ) { // track 0 is music/cinematics + if ( !streamingSounds[i].file ) { + continue; + } + // check to see if this character currently has another sound streaming on the same channel + if ( ( entchannel != CHAN_AUTO ) && ( streamingSounds[i].entnum >= 0 ) && ( streamingSounds[i].channel == entchannel ) && ( streamingSounds[i].entnum == entityNum ) ) { + // found a match, override this channel + streamingSounds[i].kill = 1; + break; + } + } + } + + ch = NULL; + +//----(SA) modified + + // shut off other sounds on this channel if necessary + for ( i = 0 ; i < MAX_CHANNELS ; i++ ) { + if ( s_channels[i].entnum == entityNum && s_channels[i].thesfx && s_channels[i].entchannel == entchannel ) { + + // cutoff all on channel + if ( flags & SND_CUTOFF_ALL ) { + S_ChannelFree( &s_channels[i] ); + continue; + } + + if ( s_channels[i].flags & SND_NOCUT ) { + continue; + } + + // RF, let client voice sounds be overwritten + if ( entityNum < MAX_CLIENTS && s_channels[i].entchannel != CHAN_AUTO && s_channels[i].entchannel != CHAN_WEAPON ) { + S_ChannelFree( &s_channels[i] ); + continue; + } + + // cutoff sounds that expect to be overwritten + if ( s_channels[i].flags & SND_OKTOCUT ) { + S_ChannelFree( &s_channels[i] ); + continue; + } + + // cutoff 'weak' sounds on channel + if ( flags & SND_CUTOFF ) { + if ( s_channels[i].flags & SND_REQUESTCUT ) { + S_ChannelFree( &s_channels[i] ); + continue; + } + } + + } + } + + // re-use channel if applicable + for ( i = 0 ; i < MAX_CHANNELS ; i++ ) { + if ( s_channels[i].entnum == entityNum && s_channels[i].entchannel == entchannel && entchannel != CHAN_AUTO ) { + if ( !( s_channels[i].flags & SND_NOCUT ) && s_channels[i].thesfx == sfx ) { + ch = &s_channels[i]; + break; + } + } + } + + if ( !ch ) { + ch = S_ChannelMalloc(); + } +//----(SA) end + + if ( !ch ) { + ch = s_channels; + + oldest = sfx->lastTimeUsed; + for ( i = 0 ; i < MAX_CHANNELS ; i++, ch++ ) { + if ( ch->entnum == entityNum && ch->thesfx == sfx ) { + chosen = i; + break; + } + if ( ch->entnum != listener_number && ch->entnum == entityNum && ch->allocTime < oldest && ch->entchannel != CHAN_ANNOUNCER ) { + oldest = ch->allocTime; + chosen = i; + } + } + if ( chosen == -1 ) { + ch = s_channels; + for ( i = 0 ; i < MAX_CHANNELS ; i++, ch++ ) { + if ( ch->entnum != listener_number && ch->allocTime < oldest && ch->entchannel != CHAN_ANNOUNCER ) { + oldest = ch->allocTime; + chosen = i; + } + } + if ( chosen == -1 ) { + if ( ch->entnum == listener_number ) { + for ( i = 0 ; i < MAX_CHANNELS ; i++, ch++ ) { + if ( ch->allocTime < oldest ) { + oldest = ch->allocTime; + chosen = i; + } + } + } + if ( chosen == -1 ) { + //Com_Printf("dropping sound\n"); + return; + } + } + } + ch = &s_channels[chosen]; + ch->allocTime = sfx->lastTimeUsed; + } + +#ifdef _DEBUG + if ( ch > &s_channels[MAX_CHANNELS] || ch < &s_channels[0] ) { //DAJ extra check + Com_DPrintf( "s_channel OUT OF BOUNDS\n" ); + return; + } +#endif + if ( origin ) { + VectorCopy( origin, ch->origin ); + ch->fixed_origin = qtrue; + } else { + ch->fixed_origin = qfalse; + } + + ch->flags = flags; //----(SA) added + ch->master_vol = 127; + ch->entnum = entityNum; + ch->thesfx = sfx; + ch->entchannel = entchannel; + ch->leftvol = ch->master_vol; // these will get calced at next spatialize + ch->rightvol = ch->master_vol; // unless the game isn't running + ch->doppler = qfalse; + + if ( ch->fixed_origin ) { + S_SpatializeOrigin( ch->origin, ch->master_vol, &ch->leftvol, &ch->rightvol, SOUND_RANGE_DEFAULT ); + } else { + S_SpatializeOrigin( snd.entityPositions[ ch->entnum ], ch->master_vol, &ch->leftvol, &ch->rightvol, SOUND_RANGE_DEFAULT ); + } + + ch->startSample = START_SAMPLE_IMMEDIATE; + ch->threadReady = qtrue; +} + +/* +============== +S_StartSound +============== +*/ +void S_StartSound( vec3_t origin, int entityNum, int entchannel, sfxHandle_t sfxHandle ) { + S_StartSoundEx( origin, entityNum, entchannel, sfxHandle, 0 ); +} + + + +/* +================== +S_StartLocalSound +================== +*/ +void S_StartLocalSound( sfxHandle_t sfxHandle, int channelNum ) { + if ( !snd.s_soundStarted || snd.s_soundMute ) { + return; + } + + if ( sfxHandle < 0 || sfxHandle >= snd.s_numSfx ) { + Com_Printf( S_COLOR_YELLOW, "S_StartLocalSound: handle %i out of range\n", sfxHandle ); + return; + } + + S_StartSound( NULL, listener_number, channelNum, sfxHandle ); +} + + +/* +================== +S_ClearSoundBuffer + +If we are about to perform file access, clear the buffer +so sound doesn't stutter. +================== +*/ +void S_ClearSoundBuffer( qboolean killStreaming ) { + if ( !snd.s_soundStarted ) { + return; + } + + if ( !snd.s_soundPainted ) { // RF, buffers are clear, no point clearing again + return; + } + + snd.s_soundPainted = qfalse; + +// snd.s_clearSoundBuffer = 4; + snd.s_clearSoundBuffer = 3; + + S_ClearSounds( killStreaming, qtrue ); // do this now since you might not be allowed to in a sec (no multi-threaeded) +} + +/* +================== +S_StopAllSounds +================== +*/ +void S_StopAllSounds( void ) { + int i; + if ( !snd.s_soundStarted ) { + return; + } + + Sys_EnterCriticalSection( crit ); + +//DAJ BUGFIX for(i=0;i= MAX_LOOP_SOUNDS ) { + return; + } + if ( !volume ) { + return; + } + + if ( sfxHandle < 0 || sfxHandle >= snd.s_numSfx ) { + Com_Error( ERR_DROP, "S_AddLoopingSound: handle %i out of range", sfxHandle ); + } + + sfx = &s_knownSfx[ sfxHandle ]; + + if ( sfx->inMemory == qfalse ) { + S_memoryLoad( sfx ); + } + + if ( !sfx->soundLength ) { + Com_Error( ERR_DROP, "%s has length 0", sfx->soundName ); + } + VectorCopy( origin, snd.loopSounds[snd.numLoopSounds].origin ); + VectorCopy( velocity, snd.loopSounds[snd.numLoopSounds].velocity ); + snd.loopSounds[snd.numLoopSounds].sfx = sfx; + if ( range ) { + snd.loopSounds[snd.numLoopSounds].range = range; + } else { + snd.loopSounds[snd.numLoopSounds].range = SOUND_RANGE_DEFAULT; + } + + if ( volume & 1 << UNDERWATER_BIT ) { + snd.loopSounds[snd.numLoopSounds].loudUnderWater = qtrue; + } + + if ( volume > 255 ) { + volume = 255; + } else if ( volume < 0 ) { + volume = 0; + } + + snd.loopSounds[snd.numLoopSounds].vol = (int)( (float)volume * snd.volCurrent ); //----(SA) modified + + snd.numLoopSounds++; +} + +/* +================== +S_AddLoopSounds + +Spatialize all of the looping sounds. +All sounds are on the same cycle, so any duplicates can just +sum up the channel multipliers. +================== +*/ +void S_AddLoopSounds( void ) { + int i, j, time; + int left_total, right_total, left, right; + channel_t *ch; + loopSound_t *loop, *loop2; + static int loopFrame; + +// Sys_EnterCriticalSection(crit); + + numLoopChannels = 0; + + time = Sys_Milliseconds(); + + loopFrame++; + for ( i = 0 ; i < snd.numLoopSounds ; i++ ) { + loop = &snd.loopSounds[i]; + if ( loop->mergeFrame == loopFrame ) { + continue; // already merged into an earlier sound + } + + //if (loop->kill) { + // S_SpatializeOrigin( loop->origin, 127, &left_total, &right_total, loop->range); // 3d + //} else { + S_SpatializeOrigin( loop->origin, 90, &left_total, &right_total, loop->range ); // sphere + //} + + // adjust according to volume + left_total = (int)( (float)loop->vol * (float)left_total / 256.0 ); + right_total = (int)( (float)loop->vol * (float)right_total / 256.0 ); + + loop->sfx->lastTimeUsed = time; + + for ( j = ( i + 1 ); j < numLoopChannels ; j++ ) { + loop2 = &snd.loopSounds[j]; + if ( loop2->sfx != loop->sfx ) { + continue; + } + loop2->mergeFrame = loopFrame; + + //if (loop2->kill) { + // S_SpatializeOrigin( loop2->origin, 127, &left, &right, loop2->range); // 3d + //} else { + S_SpatializeOrigin( loop2->origin, 90, &left, &right, loop2->range ); // sphere + //} + + // adjust according to volume + left = (int)( (float)loop2->vol * (float)left / 256.0 ); + right = (int)( (float)loop2->vol * (float)right / 256.0 ); + + loop2->sfx->lastTimeUsed = time; + left_total += left; + right_total += right; + } + if ( left_total == 0 && right_total == 0 ) { + continue; // not audible + } + + // allocate a channel + ch = &loop_channels[numLoopChannels]; + + if ( left_total > 255 ) { + left_total = 255; + } + if ( right_total > 255 ) { + right_total = 255; + } + + ch->master_vol = 127; + ch->leftvol = left_total; + ch->rightvol = right_total; + ch->thesfx = loop->sfx; + // RF, disabled doppler for looping sounds for now, since we are reverting to the old looping sound code + ch->doppler = qfalse; + //ch->doppler = loop->doppler; + //ch->dopplerScale = loop->dopplerScale; + //ch->oldDopplerScale = loop->oldDopplerScale; + numLoopChannels++; + if ( numLoopChannels == MAX_CHANNELS ) { + i = snd.numLoopSounds + 1; + } + } +// Sys_LeaveCriticalSection(crit); +} + +//============================================================================= + +/* +================= +S_ByteSwapRawSamples + +If raw data has been loaded in little endien binary form, this must be done. +If raw data was calculated, as with ADPCM, this should not be called. +================= +*/ +//DAJ void S_ByteSwapRawSamples( int samples, int width, int s_channels, const byte *data ) { +void S_ByteSwapRawSamples( int samples, int width, int s_channels, short *data ) { + int i; + + if ( width != 2 ) { + return; + } +#ifndef __MACOS__ //DAJ save this test + if ( LittleShort( 256 ) == 256 ) { + return; + } +#endif + //DAJ use a faster loop technique + if ( s_channels == 2 ) { + i = samples << 1; + } else { + i = samples; + } + + do { + *data = LittleShort( *data ); + data++; +//DAJ ((short *)data)[i] = LittleShort( ((short *)data)[i] ); + } while ( --i ); +} + +/* +============ +S_GetRawSamplePointer +============ +*/ +portable_samplepair_t *S_GetRawSamplePointer() { + return s_rawsamples[0]; +} + +/* +============ +S_RawSamples + +Music streaming +============ +*/ +void S_RawSamples( int samples, int rate, int width, int s_channels, const byte *data, float lvol, float rvol, int streamingIndex ) { + int i; + int src, dst; + float scale; + int intVolumeL, intVolumeR; + + if ( !snd.s_soundStarted || ( snd.s_soundMute == 1 ) ) { + return; + } + + // volume taken into account when mixed + s_rawVolume[streamingIndex].left = 256 * lvol; + s_rawVolume[streamingIndex].right = 256 * rvol; + + intVolumeL = 256; + intVolumeR = 256; + + if ( s_rawend[streamingIndex] < s_soundtime ) { + Com_DPrintf( "S_RawSamples: resetting minumum: %i\n",s_soundtime - s_rawend[streamingIndex] ); + s_rawend[streamingIndex] = s_soundtime; + } + + scale = (float)rate / dma.speed; + + if ( s_channels == 2 && width == 2 ) { + if ( scale == 1.0 ) { // optimized case + for ( i = 0; i < samples; i++ ) + { + dst = s_rawend[streamingIndex] & ( MAX_RAW_SAMPLES - 1 ); + s_rawend[streamingIndex]++; + s_rawsamples[streamingIndex][dst].left = ( (short *)data )[i * 2] * intVolumeL; + s_rawsamples[streamingIndex][dst].right = ( (short *)data )[i * 2 + 1] * intVolumeR; + } + } else + { + for ( i = 0; ; i++ ) + { + src = i * scale; + if ( src >= samples ) { + break; + } + dst = s_rawend[streamingIndex] & ( MAX_RAW_SAMPLES - 1 ); + s_rawend[streamingIndex]++; + s_rawsamples[streamingIndex][dst].left = ( (short *)data )[src * 2] * intVolumeL; + s_rawsamples[streamingIndex][dst].right = ( (short *)data )[src * 2 + 1] * intVolumeR; + } + } + } else if ( s_channels == 1 && width == 2 ) { + for ( i = 0; ; i++ ) + { + src = i * scale; + if ( src >= samples ) { + break; + } + dst = s_rawend[streamingIndex] & ( MAX_RAW_SAMPLES - 1 ); + s_rawend[streamingIndex]++; + s_rawsamples[streamingIndex][dst].left = ( (short *)data )[src] * intVolumeL; + s_rawsamples[streamingIndex][dst].right = ( (short *)data )[src] * intVolumeR; + } + } else if ( s_channels == 2 && width == 1 ) { + intVolumeL *= 256; + intVolumeR *= 256; + + for ( i = 0 ; ; i++ ) + { + src = i * scale; + if ( src >= samples ) { + break; + } + dst = s_rawend[streamingIndex] & ( MAX_RAW_SAMPLES - 1 ); + s_rawend[streamingIndex]++; + s_rawsamples[streamingIndex][dst].left = ( (char *)data )[src * 2] * intVolumeL; + s_rawsamples[streamingIndex][dst].right = ( (char *)data )[src * 2 + 1] * intVolumeR; + } + } else if ( s_channels == 1 && width == 1 ) { + intVolumeL *= 256; + intVolumeR *= 256; + + for ( i = 0; ; i++ ) + { + src = i * scale; + if ( src >= samples ) { + break; + } + dst = s_rawend[streamingIndex] & ( MAX_RAW_SAMPLES - 1 ); + s_rawend[streamingIndex]++; + s_rawsamples[streamingIndex][dst].left = ( ( (byte *)data )[src] - 128 ) * intVolumeL; + s_rawsamples[streamingIndex][dst].right = ( ( (byte *)data )[src] - 128 ) * intVolumeR; + } + } + + if ( s_rawend[streamingIndex] > ( s_soundtime + MAX_RAW_SAMPLES ) ) { +// Com_DPrintf( "S_RawSamples: overflowed %i\n", s_rawend[streamingIndex]-(s_soundtime+ MAX_RAW_SAMPLES) ); + } +} + +//============================================================================= + +/* +===================== +S_UpdateEntityPosition + +let the sound system know where an entity currently is +====================== +*/ +void S_UpdateEntityPosition( int entityNum, const vec3_t origin ) { + if ( entityNum < 0 || entityNum > MAX_GENTITIES ) { + Com_Error( ERR_DROP, "S_UpdateEntityPosition: bad entitynum %i", entityNum ); + } + VectorCopy( origin, snd.entityPositions[entityNum] ); +} + + +/* +============ +S_Respatialize + +Change the volumes of all the playing sounds for changes in their positions +============ +*/ +void S_Respatialize( int entityNum, const vec3_t head, vec3_t axis[3], int inwater ) { + + if ( !snd.s_soundStarted || ( snd.s_soundMute == 1 ) ) { + return; + } + + listener_number = entityNum; + VectorCopy( head, listener_origin ); + VectorCopy( axis[0], listener_axis[0] ); + VectorCopy( axis[1], listener_axis[1] ); + VectorCopy( axis[2], listener_axis[2] ); +} + +void S_ThreadRespatialize() { + int i; + channel_t *ch; + vec3_t origin; + // update spatialization for dynamic sounds + ch = s_channels; + for ( i = 0 ; i < MAX_CHANNELS ; i++, ch++ ) { + if ( !ch->thesfx ) { + continue; + } + // anything coming from the view entity will always be full volume + if ( ch->entnum == listener_number ) { + ch->leftvol = ch->master_vol; + ch->rightvol = ch->master_vol; + } else { + if ( ch->fixed_origin ) { + VectorCopy( ch->origin, origin ); + } else { + VectorCopy( snd.entityPositions[ ch->entnum ], origin ); + } + + S_SpatializeOrigin( origin, ch->master_vol, &ch->leftvol, &ch->rightvol, SOUND_RANGE_DEFAULT ); + } + } +} + +/* +======================== +S_ScanChannelStarts + +Returns qtrue if any new sounds were started since the last mix +======================== +*/ +qboolean S_ScanChannelStarts( void ) { + channel_t *ch; + int i; + qboolean newSamples; + + newSamples = qfalse; + ch = s_channels; + + for ( i = 0; i < MAX_CHANNELS; i++, ch++ ) { + if ( !ch->thesfx ) { + continue; + } + // if this channel was just started this frame, + // set the sample count to it begins mixing + // into the very first sample + if ( ch->startSample == START_SAMPLE_IMMEDIATE && ch->threadReady == qtrue ) { + ch->startSample = s_paintedtime; + newSamples = qtrue; + continue; + } + + // if it is completely finished by now, clear it + if ( ch->startSample + ( ch->thesfx->soundLength ) <= s_paintedtime ) { +//----(SA) got from TA sound. correct? +// Com_Memset(ch, 0, sizeof(*ch)); + S_ChannelFree( ch ); + } + } + + return newSamples; +} + +/* +============== +S_CheckForQueuedMusic +============== +*/ +int S_CheckForQueuedMusic( void ) { + streamingSound_t *ss; + char *nextMusicVA; + + if ( !snd.nextMusicTrack[0] ) { // we didn't actually care about the length + return 0; + } + + nextMusicVA = va( "%s", snd.nextMusicTrack ); + + ss = &streamingSounds[0]; + + if ( snd.nextMusicTrackType == QUEUED_PLAY_ONCE_SILENT ) { + // do nothing. current music is dead, don't start another + } else if ( snd.nextMusicTrackType == QUEUED_PLAY_ONCE ) { + S_StartBackgroundTrack( nextMusicVA, ss->name, 0 ); // play once, then go back to looping what's currently playing + } else { // QUEUED_PLAY_LOOPED + S_StartBackgroundTrack( nextMusicVA, nextMusicVA, 0 ); // take over + } + + snd.nextMusicTrackType = 0; // clear out music queue +// snd.nextMusicTrack[0] = 0; // clear out music queue + + return 1; +} + +/* +============ +S_Update + +Called once each time through the main loop +============ +*/ + +void S_Update( void ) { + int i; + int total; + channel_t *ch; + + if ( !snd.s_soundStarted || ( snd.s_soundMute == 1 ) ) { +// Com_DPrintf ("not started or muted\n"); + return; + } + + // + // debugging output + // + if ( s_show->integer == 2 ) { + total = 0; + ch = s_channels; + for ( i = 0; i < MAX_CHANNELS; i++, ch++ ) { + if ( ch->thesfx && ( ch->leftvol || ch->rightvol ) ) { + Com_Printf( "%f %f %s\n", ch->leftvol, ch->rightvol, ch->thesfx->soundName ); // <- this is not thread safe + total++; + } + } + + Com_Printf( "----(%i)---- painted: %i\n", total, s_paintedtime ); + } + // add loopsounds + S_AddLoopSounds(); + S_UpdateThread(); +} + + +/* +============== +S_ClearSounds +============== +*/ +void S_ClearSounds( qboolean clearStreaming, qboolean clearMusic ) { + int clear; + int i; + channel_t *ch; + streamingSound_t *ss; + + Sys_EnterCriticalSection( crit ); + + // stop looping sounds + S_ClearLoopingSounds(); + + // RF, moved this up so streaming sounds dont get updated with the music, below, and leave us with a snippet off streaming sounds after we reload + if ( clearStreaming ) { // we don't want to stop guys with long dialogue from getting cut off by a file read + // RF, clear talking amplitudes + Com_Memset( s_entityTalkAmplitude, 0, sizeof( s_entityTalkAmplitude ) ); + + for ( i = 0, ss = streamingSounds; i < MAX_STREAMING_SOUNDS; i++, ss++ ) { + if ( i > 0 || clearMusic ) { + s_rawend[i] = 0; + ss->kill = 2; // get rid of it next sound update + } + } + + // RF, we should also kill all channels, since we are killing streaming sounds anyway (fixes siren in forest playing after a map_restart/loadgame + ch = s_channels; + for ( i = 0; i < MAX_CHANNELS; i++, ch++ ) { + if ( ch->thesfx ) { + S_ChannelFree( ch ); + } + } + + } + + if ( !clearMusic ) { + S_UpdateStreamingSounds(); //----(SA) added so music will get updated if not cleared + } else { + // music cleanup + snd.nextMusicTrack[0] = 0; + snd.nextMusicTrackType = 0; + } + + if ( clearStreaming && clearMusic ) { + if ( dma.samplebits == 8 ) { + clear = 0x80; + } else { + clear = 0; + } + + SNDDMA_BeginPainting(); + if ( dma.buffer ) { + Com_Memset( dma.buffer, clear, dma.samples * dma.samplebits / 8 ); + } + SNDDMA_Submit(); + + Sys_LeaveCriticalSection( crit ); + } +} + +/* +============== +S_UpdateThread +============== +*/ +void S_UpdateThread( void ) { + + if ( !snd.s_soundStarted || ( snd.s_soundMute == 1 ) ) { +// Com_DPrintf ("not started or muted\n"); + return; + } + +#ifdef TALKANIM + // default to ZERO amplitude, overwrite if sound is playing + memset( s_entityTalkAmplitude, 0, sizeof( s_entityTalkAmplitude ) ); +#endif + + if ( snd.s_clearSoundBuffer ) { + S_ClearSounds( qtrue, (qboolean)( snd.s_clearSoundBuffer >= 4 ) ); //----(SA) modified + snd.s_clearSoundBuffer = 0; + } else { + Sys_EnterCriticalSection( crit ); + + S_ThreadRespatialize(); + // add raw data from streamed samples + S_UpdateStreamingSounds(); + // mix some sound + S_Update_Mix(); + + Sys_LeaveCriticalSection( crit ); + } +} +/* +============ +S_GetSoundtime +============ +*/ +void S_GetSoundtime( void ) { + int samplepos; + static int buffers; + static int oldsamplepos; + int fullsamples; + + fullsamples = dma.samples / dma.channels; + + // it is possible to miscount buffers if it has wrapped twice between + // calls to S_Update. Oh well. + samplepos = SNDDMA_GetDMAPos(); + if ( samplepos < oldsamplepos ) { + buffers++; // buffer wrapped + + if ( s_paintedtime > 0x40000000 ) { // time to chop things off to avoid 32 bit limits + buffers = 0; + s_paintedtime = fullsamples; + S_StopAllSounds(); + } + } + oldsamplepos = samplepos; + + s_soundtime = buffers * fullsamples + samplepos / dma.channels; + +#if 0 +// check to make sure that we haven't overshot + if ( s_paintedtime < s_soundtime ) { + Com_DPrintf( "S_GetSoundtime : overflow\n" ); + s_paintedtime = s_soundtime; + } +#endif + + if ( dma.submission_chunk < 256 ) { + s_paintedtime = s_soundtime + s_mixPreStep->value * dma.speed; + } else { + s_paintedtime = s_soundtime + dma.submission_chunk; + } +} + +/* +============ +S_Update_Mix +============ +*/ +void S_Update_Mix( void ) { + unsigned endtime; + int samps; //, i; + static float lastTime = 0.0f; + float ma, op; + float thisTime, sane; + + if ( !snd.s_soundStarted || ( snd.s_soundMute == 1 ) ) { + return; + } + + // RF, this isn't used anymore, since it was causing timing problems with streaming sounds, since the + // starting of the sound is delayed, it could cause streaming sounds to be cutoff, when the steaming sound was issued after + // this sound +/* + for(i=0;ivalue * dma.speed; + op = s_mixPreStep->value + sane * dma.speed * 0.01; + + if ( op < ma ) { + ma = op; + } + + // mix ahead of current position + endtime = s_soundtime + ma; + + // mix to an even submission block size + endtime = ( endtime + dma.submission_chunk - 1 ) + & ~( dma.submission_chunk - 1 ); + + // never mix more than the complete buffer + samps = dma.samples >> ( dma.channels - 1 ); + if ( endtime - s_soundtime > samps ) { + endtime = s_soundtime + samps; + } + +//----(SA) added + // global volume fading + + // endtime or s_paintedtime or s_soundtime... + if ( s_soundtime < snd.volTime2 ) { // still has fading to do + if ( s_soundtime > snd.volTime1 ) { // has started fading + snd.volFadeFrac = ( (float)( s_soundtime - snd.volTime1 ) / (float)( snd.volTime2 - snd.volTime1 ) ); + snd.volCurrent = ( ( 1.0 - snd.volFadeFrac ) * snd.volStart + snd.volFadeFrac * snd.volTarget ); + +//DAJ Com_DPrintf( "master vol: %f\n", snd.volCurrent ); + + } else { + snd.volCurrent = snd.volStart; + } + } else { + snd.volCurrent = snd.volTarget; + } +//----(SA) end + + + SNDDMA_BeginPainting(); + S_PaintChannels( endtime ); + SNDDMA_Submit(); + + lastTime = thisTime; +} + +/* +=============================================================================== + +console functions + +=============================================================================== +*/ + +void S_Play_f( void ) { + int i; + sfxHandle_t h; + char name[256]; + + i = 1; + while ( i < Cmd_Argc() ) { + if ( !Q_strrchr( Cmd_Argv( i ), '.' ) ) { + Com_sprintf( name, sizeof( name ), "%s.wav", Cmd_Argv( 1 ) ); + } else { + Q_strncpyz( name, Cmd_Argv( i ), sizeof( name ) ); + } + h = S_RegisterSound( name, qfalse ); + if ( h ) { + S_StartLocalSound( h, CHAN_LOCAL_SOUND ); + } + i++; + } +} + +/* +============== +S_QueueMusic_f + console interface really just for testing +============== +*/ +void S_QueueMusic_f( void ) { + int type = -2; // default to setting this as the next continual loop + int c; + + c = Cmd_Argc(); + + if ( c == 3 ) { + type = atoi( Cmd_Argv( 2 ) ); + } + + if ( type != -1 ) { // clamp to valid values (-1, -2) + type = -2; + } + + // NOTE: could actually use this to touch the file now so there's not a hit when the queue'd music is played? + S_StartBackgroundTrack( Cmd_Argv( 1 ), Cmd_Argv( 1 ), type ); +} + +void S_Music_f( void ) { + int c; + + c = Cmd_Argc(); + + if ( c == 2 ) { + S_StartBackgroundTrack( Cmd_Argv( 1 ), Cmd_Argv( 1 ), 0 ); + } else if ( c == 3 ) { + S_StartBackgroundTrack( Cmd_Argv( 1 ), Cmd_Argv( 2 ), 0 ); + Q_strncpyz( streamingSounds[0].loop, Cmd_Argv( 2 ), sizeof( streamingSounds[0].loop ) ); + } else { + Com_Printf( "music [loopfile]\n" ); + return; + } +} + +// Ridah, just for testing the streaming sounds +void S_StreamingSound_f( void ) { + int c; + + c = Cmd_Argc(); + + if ( c == 2 ) { + S_StartStreamingSound( Cmd_Argv( 1 ), 0, -1, 0, 0 ); + } else if ( c == 5 ) { + S_StartStreamingSound( Cmd_Argv( 1 ), 0, atoi( Cmd_Argv( 2 ) ), atoi( Cmd_Argv( 3 ) ), atoi( Cmd_Argv( 4 ) ) ); + } else { + Com_Printf( "streamingsound [entnum channel attenuation]\n" ); + return; + } + +} + +void S_SoundList_f( void ) { + int i; + sfx_t *sfx; + int size, total; + char type[4][16]; + char mem[2][16]; + + strcpy( type[0], "16bit" ); + strcpy( type[1], "adpcm" ); + strcpy( type[2], "daub4" ); + strcpy( type[3], "mulaw" ); + strcpy( mem[0], "paged out" ); + strcpy( mem[1], "resident " ); + total = 0; + for ( sfx = s_knownSfx, i = 0 ; i < snd.s_numSfx ; i++, sfx++ ) { + size = sfx->soundLength; + total += size; + Com_Printf( "%6i[%s] : %s[%s]\n", size, type[sfx->soundCompressionMethod], sfx->soundName, mem[sfx->inMemory] ); + } + Com_Printf( "Total resident: %i\n", total ); + S_DisplayFreeMemory(); +} + + +/* +=============================================================================== + +STREAMING SOUND + +=============================================================================== +*/ + +int FGetLittleLong( const fileHandle_t f ) { + int v; + + FS_Read( &v, sizeof( v ), f ); + + return LittleLong( v ); +} + +int FGetLittleShort( const fileHandle_t f ) { + short v; + + FS_Read( &v, sizeof( v ), f ); + + return LittleShort( v ); +} + +// returns the length of the data in the chunk, or 0 if not found +int S_FindWavChunk( const fileHandle_t f, const char *chunk ) { + char name[5]; + int len; + int r; + + name[4] = 0; + len = 0; + r = FS_Read( name, 4, f ); + if ( r != 4 ) { + return 0; + } + len = FGetLittleLong( f ); + if ( len < 0 || len > 0xfffffff ) { + len = 0; + return 0; + } + len = ( len + 1 ) & ~1; // pad to word boundary +// s_nextWavChunk += len + 8; + + if ( strcmp( name, chunk ) ) { + return 0; + } + + return len; +} + + + + +/* +====================== +S_StartBackgroundTrack +====================== +*/ +void S_StartBackgroundTrack( const char *intro, const char *loop, int fadeupTime ) { + int len; + char dump[16]; +// char name[MAX_QPATH]; + char loopMusic[MAX_QPATH]; + streamingSound_t *ss; + fileHandle_t fh; + + // music is always track 0 + ss = &streamingSounds[0]; + +//----(SA) added + if ( fadeupTime < 0 ) { // queue, don't play until music fades to 0 or is stopped + // -1 - queue to play once then return to music + // -2 - queue to set as new looping music + + if ( intro && strlen( intro ) ) { + strcpy( snd.nextMusicTrack, intro ); + snd.nextMusicTrackType = fadeupTime; + if ( fadeupTime == -2 ) { + Cvar_Set( "s_currentMusic", intro ); //----(SA) so the savegame will have the right music + + } + if ( s_debugMusic->integer ) { + if ( fadeupTime == -1 ) { + Com_Printf( "MUSIC: StartBgTrack: queueing '%s' for play once\n", intro ); + } else if ( fadeupTime == -2 ) { + Com_Printf( "MUSIC: StartBgTrack: queueing '%s' as new loop\n", intro ); + } + } + + } else { + snd.nextMusicTrack[0] = 0; // clear out the next track so things go on as they are + snd.nextMusicTrackType = 0; // be quiet at the next opportunity + + // clear out looping sound in current music so that it'll stop when it's done + if ( ss && ss->loop ) { + ss->loop[0] = 0; // clear loop + } + + if ( s_debugMusic->integer ) { + Com_Printf( "S_StartBgTrack(): queue cleared\n" ); + } + } + + return; // don't actually start any queued sounds + } + + // clear out nextMusic +// snd.nextMusicTrack[0] = 0; +//----(SA) end + + if ( !snd.s_soundStarted || !crit ) { + return; + } + + Sys_EnterCriticalSection( crit ); + + if ( !intro ) { + intro = ""; + } + if ( !loop || !loop[0] ) { + Q_strncpyz( loopMusic, intro, sizeof( loopMusic ) ); + } else { + Q_strncpyz( loopMusic, loop, sizeof( loopMusic ) ); + } + + Cvar_Set( "s_currentMusic", "" ); //----(SA) so the savegame will have the right music + + if ( !Q_stricmp( loop, "onetimeonly" ) ) { // don't change the loop if you're playing a single hit + Q_strncpyz( loopMusic, ss->loop, sizeof( loopMusic ) ); + } + + Q_strncpyz( ss->loop, loopMusic, sizeof( ss->loop ) - 4 ); + + Q_strncpyz( ss->name, intro, sizeof( ss->name ) - 4 ); + COM_DefaultExtension( ss->name, sizeof( ss->name ), ".wav" ); + + // close the current sound if present, but DON'T reset s_rawend + if ( ss->file ) { + Sys_EndStreamedFile( ss->file ); + FS_FCloseFile( ss->file ); + ss->file = 0; + } + + if ( !intro[0] ) { + Com_DPrintf( "Fail to start: %s\n", ss->name ); // (SA) TEMP + Sys_LeaveCriticalSection( crit ); + return; + } + + ss->channel = 0; + ss->entnum = -1; + ss->attenuation = 0; + + fh = 0; + // + // open up a wav file and get all the info + // + FS_FOpenFileRead( ss->name, &fh, qtrue ); + if ( !fh ) { + Com_Printf( "Couldn't open streaming sound file %s\n", ss->name ); + Sys_LeaveCriticalSection( crit ); + return; + } + + // skip the riff wav header + + FS_Read( dump, 12, fh ); + + if ( !S_FindWavChunk( fh, "fmt " ) ) { + Com_Printf( "No fmt chunk in %s\n", ss->name ); + FS_FCloseFile( fh ); + Sys_LeaveCriticalSection( crit ); + return; + } + + // save name for soundinfo + ss->info.format = FGetLittleShort( fh ); + ss->info.channels = FGetLittleShort( fh ); + ss->info.rate = FGetLittleLong( fh ); + FGetLittleLong( fh ); + FGetLittleShort( fh ); + ss->info.width = FGetLittleShort( fh ) / 8; + + if ( ss->info.format != WAV_FORMAT_PCM ) { + FS_FCloseFile( fh ); + Com_Printf( "Not a microsoft PCM format wav: %s\n", ss->name ); + Sys_LeaveCriticalSection( crit ); + return; + } + + if ( ss->info.channels != 2 || ss->info.rate != 22050 ) { + Com_Printf( "WARNING: music file %s is not 22k stereo\n", ss->name ); + } + + if ( ( len = S_FindWavChunk( fh, "data" ) ) == 0 ) { + FS_FCloseFile( fh ); + Com_Printf( "No data chunk in %s\n", ss->name ); + Sys_LeaveCriticalSection( crit ); + return; + } + + if ( s_debugMusic->integer ) { + Com_Printf( "MUSIC: StartBgTrack:\n playing %s\n looping %s %d\n", intro, loopMusic, fadeupTime ); + } + + Cvar_Set( "s_currentMusic", loopMusic ); //----(SA) so the savegame will have the right music + + ss->info.samples = len / ( ss->info.width * ss->info.channels ); + + ss->samples = ss->info.samples; + + ss->fadeStartVol = 0; + ss->fadeStart = 0; + ss->fadeEnd = 0; + ss->fadeTargetVol = 0; + + if ( fadeupTime ) { + ss->fadeStart = s_soundtime; + ss->fadeEnd = s_soundtime + ( ( (float)( ss->info.rate ) / 1000.0f ) * fadeupTime ); +// ss->fadeStart = s_paintedtime; +// ss->fadeEnd = s_paintedtime + (((float)(ss->info.rate)/1000.0f ) * fadeupTime); + ss->fadeTargetVol = 1.0; + } + + // + // start the background streaming + // + Sys_BeginStreamedFile( fh, 0x10000 ); + + ss->looped = 0; //----(SA) added + + ss->file = fh; + ss->kill = 0; + numStreamingSounds++; + + Com_DPrintf( "S_StartBackgroundTrack - Success\n" ); + Sys_LeaveCriticalSection( crit ); +} + + +/* +============== +S_FadeAllSounds + +============== +*/ +void S_FadeAllSounds( float targetVol, int time ) { + + snd.volStart = snd.volCurrent; + snd.volTarget = targetVol; + + snd.volTime1 = s_soundtime; + snd.volTime2 = s_soundtime + ( ( (float)( dma.speed ) / 1000.0f ) * time ); + + // instant + if ( !time ) { + snd.volTarget = snd.volStart = snd.volCurrent = targetVol; // set it + snd.volTime1 = snd.volTime2 = 0; // no fading + } +} + + +//----(SA) added +/* +============== +S_FadeStreamingSound +============== +*/ +void S_FadeStreamingSound( float targetVol, int time, int ssNum ) { + streamingSound_t *ss; + + if ( ssNum >= numStreamingSounds ) { // invalid sound + return; + } + + ss = &streamingSounds[ssNum]; + + if ( !ss ) { + return; + } + + if ( ss->kill ) { + return; + } + + ss->fadeStartVol = 1.0f; + + if ( ssNum == 0 ) { + if ( s_debugMusic->integer ) { + Com_Printf( "MUSIC: Fade: %0.2f %d\n", targetVol, time ); + } + } + + // get current fraction if already fading/faded + if ( ss->fadeStart ) { + if ( ss->fadeEnd <= s_soundtime ) { +// if(ss->fadeEnd <= s_paintedtime) + ss->fadeStartVol = ss->fadeTargetVol; + } else { + ss->fadeStartVol = ( (float)( s_soundtime - ss->fadeStart ) / (float)( ss->fadeEnd - ss->fadeStart ) ); + } +// ss->fadeStartVol = ( (float)(s_paintedtime - ss->fadeStart)/(float)(ss->fadeEnd - ss->fadeStart) ); + } + + ss->fadeStart = s_soundtime; + ss->fadeEnd = s_soundtime + ( ( (float)( ss->info.rate ) / 1000.0f ) * time ); +// ss->fadeStart = s_paintedtime; +// ss->fadeEnd = s_paintedtime + (((float)(ss->info.rate)/1000.0f ) * time); + ss->fadeTargetVol = targetVol; +} + + +/* +============== +S_GetStreamingFade +============== +*/ +float S_GetStreamingFade( streamingSound_t *ss ) { + float oldfrac, newfrac; + +// if(ss->kill) +// return 0; + + if ( !ss->fadeStart ) { + return 1.0f; // full volume + + } + if ( ss->fadeEnd <= s_soundtime ) { // it's hit it's target +// if(ss->fadeEnd <= s_paintedtime) { // it's hit it's target + if ( ss->fadeTargetVol <= 0 ) { // faded out. die next update + ss->kill = 1; + } + return ss->fadeTargetVol; + } + + newfrac = (float)( s_soundtime - ss->fadeStart ) / (float)( ss->fadeEnd - ss->fadeStart ); +// newfrac = (float)(s_paintedtime - ss->fadeStart)/(float)(ss->fadeEnd - ss->fadeStart); + oldfrac = 1.0f - newfrac; + + return ( oldfrac * ss->fadeStartVol ) + ( newfrac * ss->fadeTargetVol ); +} + +//----(SA) end + +/* +====================== +S_StartStreamingSound + + FIXME: record the starting cg.time of the sound, so we can determine the + position by looking at the current cg.time, this way pausing or loading a + savegame won't screw up the timing of important sounds +====================== +*/ +void S_StartStreamingSound( const char *intro, const char *loop, int entnum, int channel, int attenuation ) { + int len; + char dump[16]; +// char name[MAX_QPATH]; + int i; + streamingSound_t *ss; + fileHandle_t fh; + + if ( !crit || !snd.s_soundStarted || snd.s_soundMute || cls.state != CA_ACTIVE ) { + return; + } + + Sys_EnterCriticalSection( crit ); + if ( !intro || !intro[0] ) { + if ( loop && loop[0] ) { + intro = loop; + } else { + intro = ""; + } + } + Com_DPrintf( "S_StartStreamingSound( %s, %s, %i, %i, %i )\n", intro, loop, entnum, channel, attenuation ); + + // look for a free track, but first check for overriding a currently playing sound for this entity + ss = NULL; + if ( entnum >= 0 ) { + for ( i = 1; i < MAX_STREAMING_SOUNDS; i++ ) { // track 0 is music/cinematics + if ( !streamingSounds[i].file ) { + continue; + } + // check to see if this character currently has another sound streaming on the same channel + if ( ( channel != CHAN_AUTO ) && ( streamingSounds[i].entnum >= 0 ) && ( streamingSounds[i].channel == channel ) && ( streamingSounds[i].entnum == entnum ) ) { + // found a match, override this channel + streamingSounds[i].kill = 1; + ss = &streamingSounds[i]; // use this track to start the new stream + break; + } + } + } + if ( !ss ) { + // no need to override a current stream, so look for a free track + for ( i = 1; i < MAX_STREAMING_SOUNDS; i++ ) { // track 0 is music/cinematics + if ( !streamingSounds[i].file ) { + ss = &streamingSounds[i]; + break; + } + } + } + if ( !ss ) { + if ( !s_mute->integer ) { // don't do the print if you're muted + Com_Printf( "S_StartStreamingSound: No free streaming tracks\n" ); + } + Sys_LeaveCriticalSection( crit ); + return; + } + + if ( ss->loop && loop ) { + Q_strncpyz( ss->loop, loop, sizeof( ss->loop ) - 4 ); + } else { + ss->loop[0] = 0; + } + + Q_strncpyz( ss->name, intro, sizeof( ss->name ) - 4 ); + COM_DefaultExtension( ss->name, sizeof( ss->name ), ".wav" ); + + // close the current sound if present, but DON'T reset s_rawend + if ( ss->file ) { + Sys_EndStreamedFile( ss->file ); + FS_FCloseFile( ss->file ); + ss->file = 0; + } + + if ( !intro[0] ) { + Sys_LeaveCriticalSection( crit ); + return; + } + + fh = 0; + // + // open up a wav file and get all the info + // + FS_FOpenFileRead( ss->name, &fh, qtrue ); + if ( !fh ) { + Com_Printf( "Couldn't open streaming sound file %s\n", ss->name ); + Sys_LeaveCriticalSection( crit ); + return; + } + + // skip the riff wav header + + FS_Read( dump, 12, fh ); + + if ( !S_FindWavChunk( fh, "fmt " ) ) { + Com_Printf( "No fmt chunk in %s\n", ss->name ); + FS_FCloseFile( fh ); + Sys_LeaveCriticalSection( crit ); + return; + } + + // save name for soundinfo + ss->info.format = FGetLittleShort( fh ); + ss->info.channels = FGetLittleShort( fh ); + ss->info.rate = FGetLittleLong( fh ); + FGetLittleLong( fh ); + FGetLittleShort( fh ); + ss->info.width = FGetLittleShort( fh ) / 8; + + if ( ss->info.format != WAV_FORMAT_PCM ) { + FS_FCloseFile( fh ); + Com_Printf( "Not a microsoft PCM format wav: %s\n", ss->name ); + Sys_LeaveCriticalSection( crit ); + return; + } + + //if ( ss->info.channels != 2 || ss->info.rate != 22050 ) { + // Com_Printf("WARNING: music file %s is not 22k stereo\n", ss->name ); + //} + + if ( ( len = S_FindWavChunk( fh, "data" ) ) == 0 ) { + FS_FCloseFile( fh ); + Com_Printf( "No data chunk in %s\n", ss->name ); + Sys_LeaveCriticalSection( crit ); + return; + } + + ss->info.samples = len / ( ss->info.width * ss->info.channels ); + + ss->samples = ss->info.samples; + ss->channel = channel; + ss->attenuation = attenuation; + ss->entnum = entnum; + ss->kill = 0; + + ss->fadeStartVol = 0; + ss->fadeStart = 0; + ss->fadeEnd = 0; + ss->fadeTargetVol = 0; + + // + // start the background streaming + // + Sys_BeginStreamedFile( fh, 0x10000 ); + + ss->file = fh; + numStreamingSounds++; + Sys_LeaveCriticalSection( crit ); +} + +/* +====================== +S_StopStreamingSound +====================== +*/ +void S_StopStreamingSound( int index ) { + if ( !streamingSounds[index].file ) { + return; + } + Sys_EnterCriticalSection( crit ); + streamingSounds[index].kill = 1; + Sys_LeaveCriticalSection( crit ); +} + +//----(SA) added +/* +============== +S_StopEntStreamingSound +============== +*/ +void S_StopEntStreamingSound( int entNum ) { + int i; + + if ( entNum < 0 ) { + return; + } + + for ( i = 1; i < MAX_STREAMING_SOUNDS; i++ ) { // track 0 is music/cinematics + if ( !streamingSounds[i].file ) { + continue; + } + + if ( streamingSounds[i].entnum != entNum ) { + continue; + } + + S_StopStreamingSound( i ); + s_rawend[i] = 0; // stop it /now/ + } +} +//----(SA) end + +/* +====================== +S_StopBackgroundTrack +====================== +*/ +void S_StopBackgroundTrack( void ) { + S_StopStreamingSound( 0 ); +} + +/* +====================== +S_UpdateStreamingSounds +====================== +*/ +void S_UpdateStreamingSounds( void ) { + int bufferSamples; + int fileSamples; + byte raw[30000]; // just enough to fit in a mac stack frame + int fileBytes; + int r, i; + streamingSound_t *ss; + int *re, *rp; +// qboolean looped; + float lvol, rvol; + int soundMixAheadTime; + float streamingVol = 1.0f; + + if ( !snd.s_soundStarted || !crit ) { + return; + } + + // seems like the mute would be better down lower so no timing gets messed up + +// if ( s_mute->value ) { //----(SA) sound is muted, skip everything +// return; +// } + + soundMixAheadTime = s_soundtime; // + (int)(0.35 * dma.speed); // allow for talking animations + + snd.s_soundPainted = qtrue; + + for ( i = 0, ss = streamingSounds, re = s_rawend, rp = s_rawpainted; i < MAX_STREAMING_SOUNDS; i++, ss++, re++, rp++ ) { + if ( ss->kill && ss->file ) { + fileHandle_t file; + file = ss->file; + ss->file = 0; + Sys_EndStreamedFile( file ); + FS_FCloseFile( file ); + numStreamingSounds--; + + if ( i == 0 || ss->kill == 2 ) { // kill whole channel /now/ +// memset( &s_rawsamples[i], 0, MAX_RAW_SAMPLES*sizeof(portable_samplepair_t) ); + *re = 0; // reset rawend + + } + ss->kill = 0; + continue; + } + + *rp = qfalse; + + // don't bother playing anything if musicvolume is 0 + if ( i == 0 && s_musicVolume->value <= 0 ) { + continue; + } + if ( i > 0 && s_volume->value <= 0 ) { + continue; + } + + if ( !ss->file ) { + if ( i == 0 ) { // music + // quiet now, so start up queued music if it exists + S_CheckForQueuedMusic(); + } + continue; // skip until next frame + } + + // see how many samples should be copied into the raw buffer + if ( *re < soundMixAheadTime ) { // RF, read a bit ahead of time to allow for talking animations + *re = soundMixAheadTime; + } + +// looped = qfalse; + + while ( *re < soundMixAheadTime + MAX_RAW_SAMPLES ) { + bufferSamples = MAX_RAW_SAMPLES - ( *re - soundMixAheadTime ); + + // decide how much data needs to be read from the file + fileSamples = bufferSamples * ss->info.rate / dma.speed; + + // if there are no samples due to be read this frame, abort painting + // but keep the streaming going, since it might just need to wait until + // the next frame before it needs to paint some more + if ( !fileSamples ) { + break; + } + + // don't try and read past the end of the file + if ( fileSamples > ss->samples ) { + fileSamples = ss->samples; + } + + // our max buffer size + fileBytes = fileSamples * ( ss->info.width * ss->info.channels ); + if ( fileBytes > sizeof( raw ) ) { + fileBytes = sizeof( raw ); + fileSamples = fileBytes / ( ss->info.width * ss->info.channels ); + } + + r = Sys_StreamedRead( raw, 1, fileBytes, ss->file ); + if ( r != fileBytes ) { + Com_DPrintf( "StreamedRead failure on stream sound\n" ); + ss->kill = 1; + break; + } + + // byte swap if needed + S_ByteSwapRawSamples( fileSamples, ss->info.width, ss->info.channels, (short*)raw ); + + // calculate the volume + streamingVol = S_GetStreamingFade( ss ); + + streamingVol *= snd.volCurrent; // get current global volume level + + if ( s_mute->value ) { //----(SA) sound is muted. process to maintain timing, but play at 0 volume + streamingVol = 0; + } + + if ( i == 0 ) { // music + lvol = rvol = s_musicVolume->value * streamingVol; + } else { // attenuate if required + if ( ss->entnum >= 0 && ss->attenuation ) { + int r, l; + S_SpatializeOrigin( snd.entityPositions[ ss->entnum ], s_volume->value * 255.0f, &l, &r, SOUND_RANGE_DEFAULT ); + if ( ( lvol = ( (float)l / 255.0 ) ) > 1.0 ) { + lvol = 1.0; + } + if ( ( rvol = ( (float)r / 255.0 ) ) > 1.0 ) { + rvol = 1.0; + } + lvol *= streamingVol; + rvol *= streamingVol; + } else { + lvol = rvol = s_volume->value * streamingVol; + } + } + + // add to raw buffer + S_RawSamples( fileSamples, ss->info.rate, + ss->info.width, ss->info.channels, raw, lvol, rvol, i ); + + *rp = qtrue; + + ss->samples -= fileSamples; + + if ( !ss->samples ) { // at the end of the sound + + // Queued music will take over as the new loop + // start up queued music if it exists + if ( i == 0 && snd.nextMusicTrackType ) { // queued music is queued + if ( ss->file ) { + fileHandle_t file; + file = ss->file; + ss->file = 0; + Sys_EndStreamedFile( file ); + FS_FCloseFile( file ); + numStreamingSounds--; +// memset( &s_rawsamples[i], 0, MAX_RAW_SAMPLES*sizeof(portable_samplepair_t) ); // really clear it + s_rawend[i] = 0; // reset rawend + } +/* + nextMusicVA = va("%s", snd.nextMusicTrack); + if(snd.nextMusicTrackType == QUEUED_PLAY_ONCE) { + S_StartBackgroundTrack( nextMusicVA, ss->name, 0); // play once, then go back to looping what's currently playing + } else { // QUEUED_PLAY_LOOPED + S_StartBackgroundTrack( nextMusicVA, nextMusicVA, 0); // take over + } + snd.nextMusicTrack[0] = 0; // clear out music queue +*/ + break; // this is now the music ss->file, no need to re-start next time through + } else { + // loop + if ( ss->loop && ss->loop[0] ) { + if ( ss->looped ) { + char dump[16]; + Sys_StreamSeek( ss->file, 0, FS_SEEK_SET ); // just go back to the beginning + FS_Read( dump, 12, ss->file ); + + if ( !S_FindWavChunk( ss->file, "fmt " ) ) { + ss->kill = 1; + break; + } + + // save name for soundinfo + ss->info.format = FGetLittleShort( ss->file ); + ss->info.channels = FGetLittleShort( ss->file ); + ss->info.rate = FGetLittleLong( ss->file ); + FGetLittleLong( ss->file ); + FGetLittleShort( ss->file ); + ss->info.width = FGetLittleShort( ss->file ) / 8; + ss->samples = ss->info.samples; + if ( ( S_FindWavChunk( ss->file, "data" ) ) == 0 ) { + ss->kill = 1; + } + if ( s_debugMusic->integer ) { + Com_Printf( "MUSIC: looping current track\n" ); + } + break; + } else { // start up the sound + S_StartBackgroundTrack( ss->loop, ss->loop, 0 ); + ss->looped = qtrue; // this is now the music ss->file, no need to re-start next time through + break; + } + + + // no loop, just stop + } else { + ss->kill = 1; + if ( i == 0 ) { + Cvar_Set( "s_currentMusic", "" ); //----(SA) so the savegame know's it's supposed to be quiet + + if ( s_debugMusic->integer ) { + Com_Printf( "MUSIC: Ending current track-> no loop\n" ); + } + } + + break; + } + } + } + } + } +} + + +/* +====================== +S_FreeOldestSound +====================== +*/ +void S_FreeOldestSound( void ) { + int i, oldest, used; + sfx_t *sfx; + sndBuffer *buffer, *nbuffer; + + oldest = Sys_Milliseconds(); + used = 0; + + for ( i = 1 ; i < snd.s_numSfx ; i++ ) { + sfx = &s_knownSfx[i]; + if ( sfx->inMemory && sfx->lastTimeUsed < oldest ) { + used = i; + oldest = sfx->lastTimeUsed; + } + } + + sfx = &s_knownSfx[used]; + + Com_DPrintf( "S_FreeOldestSound: freeing sound %s\n", sfx->soundName ); + + buffer = sfx->soundData; + while ( buffer != NULL ) { + nbuffer = buffer->next; + SND_free( buffer ); + buffer = nbuffer; + } + sfx->inMemory = qfalse; + sfx->soundData = NULL; +} + diff --git a/src/client/snd_local.h b/src/client/snd_local.h new file mode 100644 index 0000000..3ff7ea7 --- /dev/null +++ b/src/client/snd_local.h @@ -0,0 +1,315 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +// snd_local.h -- private sound definations + + +#include "../game/q_shared.h" +#include "../qcommon/qcommon.h" +#include "snd_public.h" + +#define PAINTBUFFER_SIZE 4096 // this is in samples + +#define SND_CHUNK_SIZE 1024 // samples +#define SND_CHUNK_SIZE_FLOAT ( SND_CHUNK_SIZE / 2 ) // floats +#define SND_CHUNK_SIZE_BYTE ( SND_CHUNK_SIZE * 2 ) // floats + +#define TALKANIM + +typedef struct { + int left; // the final values will be clamped to +/- 0x00ffff00 and shifted down + int right; +} portable_samplepair_t; + +typedef struct adpcm_state { + short sample; /* Previous output value */ + char index; /* Index into stepsize table */ +#if defined( __MACOS__ ) + char pad; /* //DAJ added pad for alignment */ +#endif +} adpcm_state_t; + +typedef struct sndBuffer_s { + short sndChunk[SND_CHUNK_SIZE]; + struct sndBuffer_s *next; + int size; + adpcm_state_t adpcm; +} sndBuffer; + +typedef struct sfx_s { + sndBuffer *soundData; + qboolean defaultSound; // couldn't be loaded, so use buzz + qboolean inMemory; // not in Memory + qboolean soundCompressed; // not in Memory + int soundCompressionMethod; + int soundLength; + char soundName[MAX_QPATH]; + int lastTimeUsed; + struct sfx_s *next; +} sfx_t; + +typedef struct { + int channels; + int samples; // mono samples in buffer + int submission_chunk; // don't mix less than this # + int samplebits; + int speed; + int samplepos; + byte *buffer; +} dma_t; + +#define START_SAMPLE_IMMEDIATE 0x7fffffff + +typedef struct loopSound_s { + vec3_t origin; + vec3_t velocity; + float range; //----(SA) added + sfx_t *sfx; + int mergeFrame; + int vol; + qboolean loudUnderWater; // (SA) set if this sound should be played at full vol even when under water (under water loop sound for ex.) +} loopSound_t; + +typedef struct +{ + int *ptr; //DAJ BUGFIX for freelist/endlist pointer + int allocTime; + int startSample; // START_SAMPLE_IMMEDIATE = set immediately on next mix + int entnum; // to allow overriding a specific sound + int entchannel; // to allow overriding a specific sound + int leftvol; // 0-255 volume after spatialization + int rightvol; // 0-255 volume after spatialization + int master_vol; // 0-255 volume before spatialization + float dopplerScale; + float oldDopplerScale; + vec3_t origin; // only use if fixed_origin is set + qboolean fixed_origin; // use origin instead of fetching entnum's origin + sfx_t *thesfx; // sfx structure + qboolean doppler; + int flags; //----(SA) added + qboolean threadReady; +} channel_t; + + +#define WAV_FORMAT_PCM 1 + + +typedef struct { + int format; + int rate; + int width; + int channels; + int samples; + int dataofs; // chunk starts this many bytes from file start +} wavinfo_t; + + +/* +==================================================================== + + SYSTEM SPECIFIC FUNCTIONS + +==================================================================== +*/ + +// initializes cycling through a DMA buffer and returns information on it +qboolean SNDDMA_Init( void ); + +// gets the current DMA position +int SNDDMA_GetDMAPos( void ); + +// shutdown the DMA xfer. +void SNDDMA_Shutdown( void ); + +void SNDDMA_BeginPainting( void ); + +void SNDDMA_Submit( void ); + +//==================================================================== + +#if defined( __MACOS__ ) + #define MAX_CHANNELS 64 +#else + #define MAX_CHANNELS 96 +#endif + +extern channel_t s_channels[MAX_CHANNELS]; +extern channel_t loop_channels[MAX_CHANNELS]; +extern int numLoopChannels; + +extern int s_paintedtime; +extern vec3_t listener_forward; +extern vec3_t listener_right; +extern vec3_t listener_up; +extern dma_t dma; + +#ifdef TALKANIM +extern unsigned char s_entityTalkAmplitude[MAX_CLIENTS]; +#endif + +//----(SA) some flags for queued music tracks +#define QUEUED_PLAY_ONCE -1 +#define QUEUED_PLAY_LOOPED -2 +#define QUEUED_PLAY_ONCE_SILENT -3 // when done it goes quiet +//----(SA) end + +// Ridah, streaming sounds +typedef struct { + fileHandle_t file; + wavinfo_t info; + int samples; + char name[MAX_QPATH]; //----(SA) added + char loop[MAX_QPATH]; + int looped; //----(SA) added + int entnum; + int channel; + int attenuation; + int kill; //----(SA) changed + + int fadeStart; //----(SA) added + int fadeEnd; //----(SA) added + float fadeStartVol; //----(SA) added + float fadeTargetVol; //----(SA) added +} streamingSound_t; + + + + +typedef struct { + vec3_t origin; + qboolean fixedOrigin; + int entityNum; + int entityChannel; + sfxHandle_t sfx; + int flags; +} s_pushStack; + +#define MAX_PUSHSTACK 64 +#define LOOP_HASH 128 +#define MAX_LOOP_SOUNDS 128 + +// removed many statics into a common sound struct +typedef struct { + sfx_t *sfxHash[LOOP_HASH]; + int numLoopSounds; + loopSound_t loopSounds[MAX_LOOP_SOUNDS]; + + float volTarget; + float volStart; + int volTime1; + int volTime2; + float volFadeFrac; + float volCurrent; + + channel_t *freelist; + channel_t *endflist; + + int s_numSfx; + + s_pushStack pushPop[MAX_PUSHSTACK]; + int tart; + + qboolean s_soundPainted; + int s_clearSoundBuffer; + + int s_soundStarted; +// qboolean s_soundMute; + int s_soundMute; // 0 - not muted, 1 - muted, 2 - no new sounds, but play out remaining sounds (so they can die if necessary) + + vec3_t entityPositions[MAX_GENTITIES]; + + char nextMusicTrack[MAX_QPATH]; // extracted from CS_MUSIC_QUEUE //----(SA) added + int nextMusicTrackType; +} snd_t; + +extern snd_t snd; // globals for sound + + + +#define MAX_STREAMING_SOUNDS 12 // need to keep it low, or the rawsamples will get too big +#define MAX_RAW_SAMPLES 16384 + +extern streamingSound_t streamingSounds[MAX_STREAMING_SOUNDS]; +extern int s_rawend[MAX_STREAMING_SOUNDS]; +extern portable_samplepair_t s_rawsamples[MAX_STREAMING_SOUNDS][MAX_RAW_SAMPLES]; +extern portable_samplepair_t s_rawVolume[MAX_STREAMING_SOUNDS]; + + +extern cvar_t *s_volume; +extern cvar_t *s_nosound; +extern cvar_t *s_khz; +extern cvar_t *s_show; +extern cvar_t *s_mixahead; +extern cvar_t *s_mute; + +extern cvar_t *s_testsound; +extern cvar_t *s_separation; +extern cvar_t *s_currentMusic; //----(SA) added +extern cvar_t *s_debugMusic; //----(SA) added + +qboolean S_LoadSound( sfx_t *sfx ); + +void SND_free( sndBuffer *v ); +sndBuffer* SND_malloc(); +void SND_setup(); + +void S_PaintChannels( int endtime ); + +void S_memoryLoad( sfx_t *sfx ); +portable_samplepair_t *S_GetRawSamplePointer(); + +// spatializes a channel +void S_Spatialize( channel_t *ch ); + +// adpcm functions +int S_AdpcmMemoryNeeded( const wavinfo_t *info ); +void S_AdpcmEncodeSound( sfx_t *sfx, short *samples ); +void S_AdpcmGetSamples( sndBuffer *chunk, short *to ); + +// wavelet function + +#define SENTINEL_MULAW_ZERO_RUN 127 +#define SENTINEL_MULAW_FOUR_BIT_RUN 126 + +void S_FreeOldestSound(); + +#define NXStream byte + +void encodeWavelet( sfx_t *sfx, short *packets ); +void decodeWavelet( sndBuffer *stream, short *packets ); + +void encodeMuLaw( sfx_t *sfx, short *packets ); +extern short mulawToShort[256]; + +extern short *sfxScratchBuffer; +extern const sfx_t *sfxScratchPointer; +extern int sfxScratchIndex; + +extern unsigned char s_entityTalkAmplitude[MAX_CLIENTS]; + +extern float S_GetStreamingFade( streamingSound_t *ss ); //----(SA) added diff --git a/src/client/snd_mem.c b/src/client/snd_mem.c new file mode 100644 index 0000000..b0dd0b3 --- /dev/null +++ b/src/client/snd_mem.c @@ -0,0 +1,446 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: snd_mem.c + * + * desc: sound caching + * + * $Archive: /Wolf5/src/client/snd_mem.c $ + * + *****************************************************************************/ + +#include "snd_local.h" + +#define DEF_COMSOUNDMEGS "24" // (SA) upped for GD + +/* +=============================================================================== + +SOUND MEMORY MANAGENT + +=============================================================================== +*/ + +static sndBuffer *buffer = NULL; +static sndBuffer *freelist = NULL; +static int inUse = 0; +static int totalInUse = 0; + +short *sfxScratchBuffer = NULL; +const sfx_t *sfxScratchPointer = NULL; +int sfxScratchIndex = 0; + +extern cvar_t *s_nocompressed; + +/* +================ +SND_free +================ +*/ +void SND_free( sndBuffer *v ) { + *(sndBuffer **)v = freelist; + freelist = (sndBuffer*)v; + inUse += sizeof( sndBuffer ); +} + +/* +================ +SND_malloc +================ +*/ +sndBuffer* SND_malloc() { + sndBuffer *v; + + while ( freelist == NULL ) { + S_FreeOldestSound(); + } + + inUse -= sizeof( sndBuffer ); + totalInUse += sizeof( sndBuffer ); + + v = freelist; + freelist = *(sndBuffer **)freelist; + v->next = NULL; + return v; +} + +/* +================ +SND_setup +================ +*/ +void SND_setup() { + sndBuffer *p, *q; + cvar_t *cv; + int scs; + + cv = Cvar_Get( "com_soundMegs", DEF_COMSOUNDMEGS, CVAR_LATCH | CVAR_ARCHIVE ); + + scs = cv->integer * 512; + + buffer = malloc( scs * sizeof( sndBuffer ) ); + // allocate the stack based hunk allocator + sfxScratchBuffer = malloc( SND_CHUNK_SIZE * sizeof( short ) * 4 ); //Hunk_Alloc(SND_CHUNK_SIZE * sizeof(short) * 4); + sfxScratchPointer = NULL; + + inUse = scs * sizeof( sndBuffer ); + p = buffer;; + q = p + scs; + while ( --q > p ) { + *(sndBuffer **)q = q - 1; + } + *(sndBuffer **)q = NULL; + freelist = p + scs - 1; + + Com_Printf( "Sound memory manager started\n" ); +} + +/* +=============================================================================== + +WAV loading + +=============================================================================== +*/ + +static byte *data_p; +static byte *iff_end; +static byte *last_chunk; +static byte *iff_data; +static int iff_chunk_len; + +/* +================ +GetLittleShort +================ +*/ +static short GetLittleShort( void ) { + short val = 0; + val = *data_p; + val = val + ( *( data_p + 1 ) << 8 ); + data_p += 2; + return val; +} + +/* +================ +GetLittleLong +================ +*/ +static int GetLittleLong( void ) { + int val = 0; + val = *data_p; + val = val + ( *( data_p + 1 ) << 8 ); + val = val + ( *( data_p + 2 ) << 16 ); + val = val + ( *( data_p + 3 ) << 24 ); + data_p += 4; + return val; +} + +/* +================ +FindNextChunk +================ +*/ +static void FindNextChunk( char *name ) { + while ( 1 ) + { + data_p = last_chunk; + + if ( data_p >= iff_end ) { // didn't find the chunk + data_p = NULL; + return; + } + + data_p += 4; + iff_chunk_len = GetLittleLong(); + if ( iff_chunk_len < 0 ) { + data_p = NULL; + return; + } + data_p -= 8; + last_chunk = data_p + 8 + ( ( iff_chunk_len + 1 ) & ~1 ); + if ( !strncmp( (char *)data_p, name, 4 ) ) { + return; + } + } +} + +/* +================ +FindChunk +================ +*/ +static void FindChunk( char *name ) { + last_chunk = iff_data; + FindNextChunk( name ); +} + +/* +============ +GetWavinfo +============ +*/ +static wavinfo_t GetWavinfo( char *name, byte *wav, int wavlength ) { + wavinfo_t info; + + Com_Memset( &info, 0, sizeof( info ) ); + + if ( !wav ) { + return info; + } + + iff_data = wav; + iff_end = wav + wavlength; + +// find "RIFF" chunk + FindChunk( "RIFF" ); + if ( !( data_p && !strncmp( (char *)data_p + 8, "WAVE", 4 ) ) ) { + Com_Printf( "Missing RIFF/WAVE chunks\n" ); + return info; + } + +// get "fmt " chunk + iff_data = data_p + 12; +// DumpChunks (); + + FindChunk( "fmt " ); + if ( !data_p ) { + Com_Printf( "Missing fmt chunk\n" ); + return info; + } + data_p += 8; + info.format = GetLittleShort(); + info.channels = GetLittleShort(); + info.rate = GetLittleLong(); + data_p += 4 + 2; + info.width = GetLittleShort() / 8; + + if ( info.format != 1 ) { + Com_Printf( "Microsoft PCM format only\n" ); + return info; + } + + +// find data chunk + FindChunk( "data" ); + if ( !data_p ) { + Com_Printf( "Missing data chunk\n" ); + return info; + } + + data_p += 4; + info.samples = GetLittleLong() / info.width; + info.dataofs = data_p - wav; + + return info; +} + +/* +================ +ResampleSfx + +resample / decimate to the current source rate +================ +*/ +static void ResampleSfx( sfx_t *sfx, int inrate, int inwidth, byte *data, qboolean compressed ) { + int outcount; + int srcsample; + float stepscale; + int i; + int sample, samplefrac, fracstep; + int part; + sndBuffer *chunk; + + stepscale = (float)inrate / dma.speed; // this is usually 0.5, 1, or 2 + + outcount = sfx->soundLength / stepscale; + sfx->soundLength = outcount; + + samplefrac = 0; + fracstep = stepscale * 256; + chunk = sfx->soundData; + + for ( i = 0 ; i < outcount ; i++ ) + { + srcsample = samplefrac >> 8; + samplefrac += fracstep; + if ( inwidth == 2 ) { + sample = LittleShort( ( (short *)data )[srcsample] ); + } else { + sample = (int)( ( unsigned char )( data[srcsample] ) - 128 ) << 8; + } + part = ( i & ( SND_CHUNK_SIZE - 1 ) ); + if ( part == 0 ) { + sndBuffer *newchunk; + newchunk = SND_malloc(); + if ( chunk == NULL ) { + sfx->soundData = newchunk; + } else { + chunk->next = newchunk; + } + chunk = newchunk; + } + + chunk->sndChunk[part] = sample; + } +} + +/* +================ +ResampleSfx + +resample / decimate to the current source rate +================ +*/ +static int ResampleSfxRaw( short *sfx, int inrate, int inwidth, int samples, byte *data ) { + int outcount; + int srcsample; + float stepscale; + int i; + int sample, samplefrac, fracstep; + + stepscale = (float)inrate / dma.speed; // this is usually 0.5, 1, or 2 + + outcount = samples / stepscale; + + samplefrac = 0; + fracstep = stepscale * 256; + + for ( i = 0 ; i < outcount ; i++ ) + { + srcsample = samplefrac >> 8; + samplefrac += fracstep; + if ( inwidth == 2 ) { + sample = LittleShort( ( (short *)data )[srcsample] ); + } else { + sample = (int)( ( unsigned char )( data[srcsample] ) - 128 ) << 8; + } + sfx[i] = sample; + } + return outcount; +} + + +//============================================================================= + +/* +============== +S_LoadSound + +The filename may be different than sfx->name in the case +of a forced fallback of a player specific sound +============== +*/ +qboolean S_LoadSound( sfx_t *sfx ) { + byte *data; + short *samples; + wavinfo_t info; + int size; + + // player specific sounds are never directly loaded + if ( sfx->soundName[0] == '*' ) { + return qfalse; + } + + // load it in + size = FS_ReadFile( sfx->soundName, (void **)&data ); + if ( !data ) { + return qfalse; + } + + info = GetWavinfo( sfx->soundName, data, size ); + if ( info.channels != 1 ) { + Com_Printf( "%s is a stereo wav file\n", sfx->soundName ); + FS_FreeFile( data ); + return qfalse; + } + + if ( info.width == 1 ) { + Com_DPrintf( S_COLOR_YELLOW "WARNING: %s is a 8 bit wav file\n", sfx->soundName ); + } + + if ( info.rate != 22050 ) { + Com_DPrintf( S_COLOR_YELLOW "WARNING: %s is not a 22kHz wav file\n", sfx->soundName ); + } + + samples = Hunk_AllocateTempMemory( info.samples * sizeof( short ) * 2 ); + + sfx->lastTimeUsed = Sys_Milliseconds() + 1; + + // each of these compression schemes works just fine + // but the 16bit quality is much nicer and with a local + // install assured we can rely upon the sound memory + // manager to do the right thing for us and page + // sound in as needed + + + if ( s_nocompressed->value ) { + sfx->soundCompressionMethod = 0; + sfx->soundLength = info.samples; + sfx->soundData = NULL; + ResampleSfx( sfx, info.rate, info.width, data + info.dataofs, qfalse ); + } else if ( sfx->soundCompressed == qtrue ) { + sfx->soundCompressionMethod = 1; + sfx->soundData = NULL; + sfx->soundLength = ResampleSfxRaw( samples, info.rate, info.width, info.samples, ( data + info.dataofs ) ); + S_AdpcmEncodeSound( sfx, samples ); +#ifdef COMPRESSION + } else if ( info.samples > ( SND_CHUNK_SIZE * 16 ) && info.width > 1 ) { + sfx->soundCompressionMethod = 3; + sfx->soundData = NULL; + sfx->soundLength = ResampleSfxRaw( samples, info.rate, info.width, info.samples, ( data + info.dataofs ) ); + encodeMuLaw( sfx, samples ); + } else if ( info.samples > ( SND_CHUNK_SIZE * 6400 ) && info.width > 1 ) { + sfx->soundCompressionMethod = 2; + sfx->soundData = NULL; + sfx->soundLength = ResampleSfxRaw( samples, info.rate, info.width, info.samples, ( data + info.dataofs ) ); + encodeWavelet( sfx, samples ); +#endif + } else { + sfx->soundCompressionMethod = 0; + sfx->soundLength = info.samples; + sfx->soundData = NULL; + ResampleSfx( sfx, info.rate, info.width, data + info.dataofs, qfalse ); + } + Hunk_FreeTempMemory( samples ); + FS_FreeFile( data ); + + return qtrue; +} + +/* +================ +S_DisplayFreeMemory +================ +*/ +void S_DisplayFreeMemory() { + Com_Printf( "%d bytes free sound buffer memory, %d total used\n", inUse, totalInUse ); +} diff --git a/src/client/snd_mix.c b/src/client/snd_mix.c new file mode 100644 index 0000000..3aab0fa --- /dev/null +++ b/src/client/snd_mix.c @@ -0,0 +1,952 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: snd_mix.c + * + * desc: portable code to mix sounds for snd_dma.c + * + * + *****************************************************************************/ + +#include "snd_local.h" + +portable_samplepair_t paintbuffer[PAINTBUFFER_SIZE]; +static int snd_vol; + +// TTimo not static, required by unix/snd_mixa.s +int *snd_p; +int snd_linear_count; +short *snd_out; + +#if !( defined __linux__ && defined __i386__ ) +#if !id386 + +/* +=================== +S_WriteLinearBlastStereo16 +=================== +*/ +void S_WriteLinearBlastStereo16( void ) { + int i; + int val; + + for ( i = 0 ; i < snd_linear_count ; i += 2 ) + { + val = snd_p[i] >> 8; + if ( val > 0x7fff ) { + snd_out[i] = 0x7fff; + } else if ( val < (short)0x8000 ) { + snd_out[i] = (short)0x8000; + } else { + snd_out[i] = val; + } + + val = snd_p[i + 1] >> 8; + if ( val > 0x7fff ) { + snd_out[i + 1] = 0x7fff; + } else if ( val < (short)0x8000 ) { + snd_out[i + 1] = (short)0x8000; + } else { + snd_out[i + 1] = val; + } + } +} + +#else // !id386 + +__declspec( naked ) void S_WriteLinearBlastStereo16( void ) { + __asm { + + push edi + push ebx + mov ecx,ds : dword ptr[snd_linear_count] + mov ebx,ds : dword ptr[snd_p] + mov edi,ds : dword ptr[snd_out] +LWLBLoopTop: + mov eax,ds : dword ptr[-8 + ebx + ecx * 4] + sar eax,8 + cmp eax,07FFFh + jg LClampHigh + cmp eax,0FFFF8000h + jnl LClampDone + mov eax,0FFFF8000h + jmp LClampDone +LClampHigh: + mov eax,07FFFh +LClampDone: + mov edx,ds : dword ptr[-4 + ebx + ecx * 4] + sar edx,8 + cmp edx,07FFFh + jg LClampHigh2 + cmp edx,0FFFF8000h + jnl LClampDone2 + mov edx,0FFFF8000h + jmp LClampDone2 +LClampHigh2: + mov edx,07FFFh +LClampDone2: + shl edx,16 + and eax,0FFFFh + or edx,eax + mov ds : dword ptr[-4 + edi + ecx * 2],edx + sub ecx,2 + jnz LWLBLoopTop + pop ebx + pop edi + ret + } +} + +#endif // !id386 + +#else // !(defined __linux__ && defined __i386__) + +// snd_mixa.s +void S_WriteLinearBlastStereo16( void ); + +#endif + +/* +=================== +S_TransferStereo16 +=================== +*/ +void S_TransferStereo16( unsigned long *pbuf, int endtime ) { + int lpos; + int ls_paintedtime; + + snd_p = (int *) paintbuffer; + ls_paintedtime = s_paintedtime; + + while ( ls_paintedtime < endtime ) + { + // handle recirculating buffer issues + lpos = ls_paintedtime & ( ( dma.samples >> 1 ) - 1 ); + + snd_out = (short *) pbuf + ( lpos << 1 ); + + snd_linear_count = ( dma.samples >> 1 ) - lpos; + if ( ls_paintedtime + snd_linear_count > endtime ) { + snd_linear_count = endtime - ls_paintedtime; + } + + snd_linear_count <<= 1; + + // write a linear blast of samples + S_WriteLinearBlastStereo16(); + + snd_p += snd_linear_count; + ls_paintedtime += ( snd_linear_count >> 1 ); + } +} + +/* +=================== +S_TransferPaintBuffer +=================== +*/ +void S_TransferPaintBuffer( int endtime ) { + int out_idx; + int count; + int out_mask; + int *p; + int step; + int val; + unsigned long *pbuf; + + pbuf = (unsigned long *)dma.buffer; + if ( !pbuf ) { + return; + } + + if ( s_testsound->integer ) { + int i; + int count; + + // write a fixed sine wave + count = ( endtime - s_paintedtime ); + for ( i = 0 ; i < count ; i++ ) { + float v; + v = sin( M_PI * 2 * i / 64 ); + paintbuffer[i].left = paintbuffer[i].right = v * 0x400000; + } + } + + + if ( dma.samplebits == 16 && dma.channels == 2 ) { + // optimized case + S_TransferStereo16( pbuf, endtime ); + } else + { // general case + p = (int *) paintbuffer; + count = ( endtime - s_paintedtime ) * dma.channels; + out_mask = dma.samples - 1; + out_idx = s_paintedtime * dma.channels & out_mask; + step = 3 - dma.channels; + + if ( dma.samplebits == 16 ) { + short *out = (short *) pbuf; + while ( count-- ) + { + val = *p >> 8; + p += step; + if ( val > 0x7fff ) { + val = 0x7fff; + } else if ( val < -32768 ) { + val = -32768; + } + out[out_idx] = val; + out_idx = ( out_idx + 1 ) & out_mask; + } + } else if ( dma.samplebits == 8 ) { + unsigned char *out = (unsigned char *) pbuf; + while ( count-- ) + { + val = *p >> 8; + p += step; + if ( val > 0x7fff ) { + val = 0x7fff; + } else if ( val < -32768 ) { + val = -32768; + } + out[out_idx] = ( val >> 8 ) + 128; + out_idx = ( out_idx + 1 ) & out_mask; + } + } + } +} + +/* +=============================================================================== + +LIP SYNCING + +=============================================================================== +*/ + +#ifdef TALKANIM + +unsigned char s_entityTalkAmplitude[MAX_CLIENTS]; + +/* +=================== +S_SetVoiceAmplitudeFrom16 +=================== +*/ +void S_SetVoiceAmplitudeFrom16( const sfx_t *sc, int sampleOffset, int count, int entnum ) { + int data, i, sfx_count; + sndBuffer *chunk; + short *samples; + + if ( count <= 0 ) { + return; // must have gone ahead of the end of the sound + } + chunk = sc->soundData; + while ( sampleOffset >= SND_CHUNK_SIZE ) { + chunk = chunk->next; + sampleOffset -= SND_CHUNK_SIZE; + if ( !chunk ) { + chunk = sc->soundData; + } + } + + sfx_count = 0; + samples = chunk->sndChunk; + for ( i = 0; i < count; i++ ) { + if ( sampleOffset >= SND_CHUNK_SIZE ) { + chunk = chunk->next; + samples = chunk->sndChunk; + sampleOffset = 0; + } + data = samples[sampleOffset++]; + if ( abs( data ) > 5000 ) { + sfx_count += ( data * 255 ) >> 8; + } + } + //Com_Printf("Voice sfx_count = %d, count = %d\n", sfx_count, count ); + // adjust the sfx_count according to the frametime (scale down for longer frametimes) + sfx_count = abs( sfx_count ); + sfx_count = (int)( (float)sfx_count / ( 2.0 * (float)count ) ); + if ( sfx_count > 255 ) { + sfx_count = 255; + } + if ( sfx_count < 25 ) { + sfx_count = 0; + } + //Com_Printf("sfx_count = %d\n", sfx_count ); + // update the amplitude for this entity + s_entityTalkAmplitude[entnum] = (unsigned char)sfx_count; +} + +/* +=================== +S_SetVoiceAmplitudeFromADPCM +=================== +*/ +void S_SetVoiceAmplitudeFromADPCM( const sfx_t *sc, int sampleOffset, int count, int entnum ) { + int data, i, sfx_count; + sndBuffer *chunk; + short *samples; + + if ( count <= 0 ) { + return; // must have gone ahead of the end of the sound + } + i = 0; + chunk = sc->soundData; + while ( sampleOffset >= ( SND_CHUNK_SIZE * 4 ) ) { + chunk = chunk->next; + sampleOffset -= ( SND_CHUNK_SIZE * 4 ); + i++; + } + + if ( i != sfxScratchIndex || sfxScratchPointer != sc ) { + S_AdpcmGetSamples( chunk, sfxScratchBuffer ); + sfxScratchIndex = i; + sfxScratchPointer = sc; + } + + sfx_count = 0; + samples = sfxScratchBuffer; + for ( i = 0; i < count; i++ ) { + if ( sampleOffset >= SND_CHUNK_SIZE * 4 ) { + chunk = chunk->next; + S_AdpcmGetSamples( chunk, sfxScratchBuffer ); + sampleOffset = 0; + sfxScratchIndex++; + } + data = samples[sampleOffset++]; + if ( abs( data ) > 5000 ) { + sfx_count += ( data * 255 ) >> 8; + } + } + //Com_Printf("Voice sfx_count = %d, count = %d\n", sfx_count, count ); + // adjust the sfx_count according to the frametime (scale down for longer frametimes) + sfx_count = abs( sfx_count ); + sfx_count = (int)( (float)sfx_count / ( 2.0 * (float)count ) ); + if ( sfx_count > 255 ) { + sfx_count = 255; + } + if ( sfx_count < 25 ) { + sfx_count = 0; + } + //Com_Printf("sfx_count = %d\n", sfx_count ); + // update the amplitude for this entity + s_entityTalkAmplitude[entnum] = (unsigned char)sfx_count; +} + +/* +=================== +S_SetVoiceAmplitudeFromWavelet +=================== +*/ +void S_SetVoiceAmplitudeFromWavelet( const sfx_t *sc, int sampleOffset, int count, int entnum ) { + int data, i, sfx_count; + sndBuffer *chunk; + short *samples; + + if ( count <= 0 ) { + return; // must have gone ahead of the end of the sound + } + i = 0; + chunk = sc->soundData; + while ( sampleOffset >= ( SND_CHUNK_SIZE_FLOAT * 4 ) ) { + chunk = chunk->next; + sampleOffset -= ( SND_CHUNK_SIZE_FLOAT * 4 ); + i++; + } + if ( i != sfxScratchIndex || sfxScratchPointer != sc ) { + decodeWavelet( chunk, sfxScratchBuffer ); + sfxScratchIndex = i; + sfxScratchPointer = sc; + } + sfx_count = 0; + samples = sfxScratchBuffer; + for ( i = 0; i < count; i++ ) { + if ( sampleOffset >= ( SND_CHUNK_SIZE_FLOAT * 4 ) ) { + chunk = chunk->next; + decodeWavelet( chunk, sfxScratchBuffer ); + sfxScratchIndex++; + sampleOffset = 0; + } + data = samples[sampleOffset++]; + if ( abs( data ) > 5000 ) { + sfx_count += ( data * 255 ) >> 8; + } + } + + //Com_Printf("Voice sfx_count = %d, count = %d\n", sfx_count, count ); + // adjust the sfx_count according to the frametime (scale down for longer frametimes) + sfx_count = abs( sfx_count ); + sfx_count = (int)( (float)sfx_count / ( 2.0 * (float)count ) ); + if ( sfx_count > 255 ) { + sfx_count = 255; + } + if ( sfx_count < 25 ) { + sfx_count = 0; + } + //Com_Printf("sfx_count = %d\n", sfx_count ); + // update the amplitude for this entity + s_entityTalkAmplitude[entnum] = (unsigned char)sfx_count; +} + +/* +=================== +S_SetVoiceAmplitudeFromMuLaw +=================== +*/ +void S_SetVoiceAmplitudeFromMuLaw( const sfx_t *sc, int sampleOffset, int count, int entnum ) { + int data, i, sfx_count; + sndBuffer *chunk; + byte *samples; + + if ( count <= 0 ) { + return; // must have gone ahead of the end of the sound + } + chunk = sc->soundData; + while ( sampleOffset >= ( SND_CHUNK_SIZE * 2 ) ) { + chunk = chunk->next; + sampleOffset -= ( SND_CHUNK_SIZE * 2 ); + if ( !chunk ) { + chunk = sc->soundData; + } + } + sfx_count = 0; + samples = (byte *)chunk->sndChunk + sampleOffset; + for ( i = 0; i < count; i++ ) { + if ( samples >= (byte *)chunk->sndChunk + ( SND_CHUNK_SIZE * 2 ) ) { + chunk = chunk->next; + samples = (byte *)chunk->sndChunk; + } + data = mulawToShort[*samples]; + if ( abs( data ) > 5000 ) { + sfx_count += ( data * 255 ) >> 8; + } + samples++; + } + //Com_Printf("Voice sfx_count = %d, count = %d\n", sfx_count, count ); + // adjust the sfx_count according to the frametime (scale down for longer frametimes) + sfx_count = abs( sfx_count ); + sfx_count = (int)( (float)sfx_count / ( 2.0 * (float)count ) ); + if ( sfx_count > 255 ) { + sfx_count = 255; + } + if ( sfx_count < 25 ) { + sfx_count = 0; + } + //Com_Printf("sfx_count = %d\n", sfx_count ); + // update the amplitude for this entity + s_entityTalkAmplitude[entnum] = (unsigned char)sfx_count; +} + +/* +=================== +S_GetVoiceAmplitude +=================== +*/ +int S_GetVoiceAmplitude( int entityNum ) { + if ( entityNum >= MAX_CLIENTS ) { + Com_Printf( "Error: S_GetVoiceAmplitude() called for a non-client\n" ); + return 0; + } + + return (int)s_entityTalkAmplitude[entityNum]; +} +#endif + +/* +=============================================================================== + +CHANNEL MIXING + +=============================================================================== +*/ + +/* +=================== +S_PaintChannelFrom16 +=================== +*/ +static void S_PaintChannelFrom16( channel_t *ch, const sfx_t *sc, int count, int sampleOffset, int bufferOffset ) { + int data, aoff, boff; + int leftvol, rightvol; + int i, j; + portable_samplepair_t *samp; + sndBuffer *chunk; + short *samples; + float ooff, fdata, fdiv, fleftvol, frightvol; + + samp = &paintbuffer[ bufferOffset ]; + + if ( ch->doppler ) { + sampleOffset = sampleOffset * ch->oldDopplerScale; + } + + chunk = sc->soundData; + while ( sampleOffset >= SND_CHUNK_SIZE ) { + chunk = chunk->next; + sampleOffset -= SND_CHUNK_SIZE; + if ( !chunk ) { + chunk = sc->soundData; + } + } + + if ( !ch->doppler ) { + leftvol = ch->leftvol * snd_vol; + rightvol = ch->rightvol * snd_vol; + + samples = chunk->sndChunk; + for ( i = 0; i < count; i++ ) { + if ( sampleOffset >= SND_CHUNK_SIZE ) { + chunk = chunk->next; + if ( chunk == NULL ) { + chunk = sc->soundData; + } + samples = chunk->sndChunk; + sampleOffset -= SND_CHUNK_SIZE; + } + data = samples[sampleOffset++]; + samp[i].left += ( data * leftvol ) >> 8; + samp[i].right += ( data * rightvol ) >> 8; + } + } else { + fleftvol = ch->leftvol * snd_vol; + frightvol = ch->rightvol * snd_vol; + + ooff = sampleOffset; + samples = chunk->sndChunk; + + for ( i = 0 ; i < count ; i++ ) { + aoff = ooff; + ooff = ooff + ch->dopplerScale; + boff = ooff; + fdata = 0; + for ( j = aoff; j < boff; j++ ) { + if ( j >= SND_CHUNK_SIZE ) { + chunk = chunk->next; + if ( !chunk ) { + chunk = sc->soundData; + } + samples = chunk->sndChunk; + ooff -= SND_CHUNK_SIZE; + } + fdata += samples[j & ( SND_CHUNK_SIZE - 1 )]; + } + fdiv = 256 * ( boff - aoff ); + samp[i].left += ( fdata * fleftvol ) / fdiv; + samp[i].right += ( fdata * frightvol ) / fdiv; + } + } +} + +/* +=================== +S_PaintChannelFromWavelet +=================== +*/ +void S_PaintChannelFromWavelet( channel_t *ch, sfx_t *sc, int count, int sampleOffset, int bufferOffset ) { + int data; + int leftvol, rightvol; + int i; + portable_samplepair_t *samp; + sndBuffer *chunk; + short *samples; + + leftvol = ch->leftvol * snd_vol; + rightvol = ch->rightvol * snd_vol; + + i = 0; + samp = &paintbuffer[ bufferOffset ]; + chunk = sc->soundData; + while ( sampleOffset >= ( SND_CHUNK_SIZE_FLOAT * 4 ) ) { + chunk = chunk->next; + sampleOffset -= ( SND_CHUNK_SIZE_FLOAT * 4 ); + i++; + } + + if ( i != sfxScratchIndex || sfxScratchPointer != sc ) { + decodeWavelet( chunk, sfxScratchBuffer ); + sfxScratchIndex = i; + sfxScratchPointer = sc; + } + + samples = sfxScratchBuffer; + + // FIXME: doppler + + for ( i = 0; i < count; i++ ) { + if ( sampleOffset >= ( SND_CHUNK_SIZE_FLOAT * 4 ) ) { + chunk = chunk->next; + decodeWavelet( chunk, sfxScratchBuffer ); + sfxScratchIndex++; + sampleOffset = 0; + } + data = samples[sampleOffset++]; + samp[i].left += ( data * leftvol ) >> 8; + samp[i].right += ( data * rightvol ) >> 8; + } +} + +/* +=================== +S_PaintChannelFromADPCM +=================== +*/ +void S_PaintChannelFromADPCM( channel_t *ch, sfx_t *sc, int count, int sampleOffset, int bufferOffset ) { + int data; + int leftvol, rightvol; + int i; + portable_samplepair_t *samp; + sndBuffer *chunk; + short *samples; + + leftvol = ch->leftvol * snd_vol; + rightvol = ch->rightvol * snd_vol; + + i = 0; + samp = &paintbuffer[ bufferOffset ]; + chunk = sc->soundData; + + if ( ch->doppler ) { + sampleOffset = sampleOffset * ch->oldDopplerScale; + } + + while ( sampleOffset >= ( SND_CHUNK_SIZE * 4 ) ) { + chunk = chunk->next; + sampleOffset -= ( SND_CHUNK_SIZE * 4 ); + i++; + } + + if ( i != sfxScratchIndex || sfxScratchPointer != sc ) { + S_AdpcmGetSamples( chunk, sfxScratchBuffer ); + sfxScratchIndex = i; + sfxScratchPointer = sc; + } + + samples = sfxScratchBuffer; + for ( i = 0; i < count; i++ ) { + if ( sampleOffset >= SND_CHUNK_SIZE * 4 ) { + chunk = chunk->next; + if ( !chunk ) { + chunk = sc->soundData; + } + S_AdpcmGetSamples( chunk, sfxScratchBuffer ); + sampleOffset = 0; + sfxScratchIndex++; + } + data = samples[sampleOffset++]; + samp[i].left += ( data * leftvol ) >> 8; + samp[i].right += ( data * rightvol ) >> 8; + } +} + +/* +=================== +S_PaintChannelFromMuLaw +=================== +*/ +void S_PaintChannelFromMuLaw( channel_t *ch, sfx_t *sc, int count, int sampleOffset, int bufferOffset ) { + int data; + int leftvol, rightvol; + int i; + portable_samplepair_t *samp; + sndBuffer *chunk; + byte *samples; + float ooff; + + leftvol = ch->leftvol * snd_vol; + rightvol = ch->rightvol * snd_vol; + + samp = &paintbuffer[ bufferOffset ]; + chunk = sc->soundData; + while ( sampleOffset >= ( SND_CHUNK_SIZE * 2 ) ) { + chunk = chunk->next; + sampleOffset -= ( SND_CHUNK_SIZE * 2 ); + if ( !chunk ) { + chunk = sc->soundData; + } + } + + if ( !ch->doppler ) { + samples = (byte *)chunk->sndChunk + sampleOffset; + for ( i = 0; i < count; i++ ) { + if ( samples >= (byte *)chunk->sndChunk + ( SND_CHUNK_SIZE * 2 ) ) { + chunk = chunk->next; + samples = (byte *)chunk->sndChunk; + } + data = mulawToShort[*samples]; + samp[i].left += ( data * leftvol ) >> 8; + samp[i].right += ( data * rightvol ) >> 8; + samples++; + } + } else { + ooff = sampleOffset; + samples = (byte *)chunk->sndChunk; + for ( i = 0; i < count; i++ ) { + if ( ooff >= SND_CHUNK_SIZE * 2 ) { + chunk = chunk->next; + if ( !chunk ) { + chunk = sc->soundData; + } + samples = (byte *)chunk->sndChunk; + ooff = 0.0; + } + data = mulawToShort[samples[(int)( ooff )]]; + ooff = ooff + ch->dopplerScale; + samp[i].left += ( data * leftvol ) >> 8; + samp[i].right += ( data * rightvol ) >> 8; + } + } +} + +#define TALK_FUTURE_SEC 0.25 // go this far into the future (seconds) + +/* +=================== +S_PaintChannels +=================== +*/ +void S_PaintChannels( int endtime ) { + int i, si; + int end; + channel_t *ch; + sfx_t *sc; + int ltime, count; + int sampleOffset; + streamingSound_t *ss; + qboolean firstPass = qtrue; + + if ( s_mute->value ) { + snd_vol = 0; + } else { + snd_vol = s_volume->value * 256; + } + + if ( snd.volCurrent < 1 ) { // only when fading (at map start/end) + snd_vol = (int)( (float)snd_vol * snd.volCurrent ); + } + + //Com_Printf ("%i to %i\n", s_paintedtime, endtime); + while ( s_paintedtime < endtime ) { + // if paintbuffer is smaller than DMA buffer + // we may need to fill it multiple times + end = endtime; + if ( endtime - s_paintedtime > PAINTBUFFER_SIZE ) { + Com_DPrintf( "endtime exceeds PAINTBUFFER_SIZE %i\n", endtime - s_paintedtime ); + end = s_paintedtime + PAINTBUFFER_SIZE; + } + + // clear pain buffer for the current time + Com_Memset( paintbuffer, 0, ( end - s_paintedtime ) * sizeof( portable_samplepair_t ) ); + // mix all streaming sounds into paint buffer + for ( si = 0, ss = streamingSounds; si < MAX_STREAMING_SOUNDS; si++, ss++ ) { + // if this streaming sound is still playing + if ( s_rawend[si] >= s_paintedtime ) { + // copy from the streaming sound source + int s; + int stop; +// float fsir, fsil; // TTimo: unused + + stop = ( end < s_rawend[si] ) ? end : s_rawend[si]; + + // precalculating this saves zillions of cycles +//DAJ fsir = ((float)s_rawVolume[si].left/255.0f); +//DAJ fsil = ((float)s_rawVolume[si].right/255.0f); + + for ( i = s_paintedtime ; i < stop ; i++ ) { + s = i & ( MAX_RAW_SAMPLES - 1 ); +//DAJ paintbuffer[i-s_paintedtime].left += (int)((float)s_rawsamples[si][s].left * fsir); +//DAJ paintbuffer[i-s_paintedtime].right += (int)((float)s_rawsamples[si][s].right * fsil); + //DAJ even faster + paintbuffer[i - s_paintedtime].left += ( s_rawsamples[si][s].left * s_rawVolume[si].left ) >> 8; + paintbuffer[i - s_paintedtime].right += ( s_rawsamples[si][s].right * s_rawVolume[si].right ) >> 8; + } + +#ifdef TALKANIM + if ( firstPass && ss->channel == CHAN_VOICE && ss->entnum < MAX_CLIENTS ) { + int talkcnt, talktime; + int sfx_count, vstop; + int data; + + // we need to go into the future, since the interpolated behaviour of the facial + // animation creates lag in the time it takes to display the current facial frame + talktime = s_paintedtime + (int)( TALK_FUTURE_SEC * (float)s_khz->integer * 1000 ); + vstop = ( talktime + 100 < s_rawend[si] ) ? talktime + 100 : s_rawend[si]; + talkcnt = 1; + sfx_count = 0; + + for ( i = talktime ; i < vstop ; i++ ) { + s = i & ( MAX_RAW_SAMPLES - 1 ); + data = abs( ( s_rawsamples[si][s].left ) / 8000 ); + if ( data > sfx_count ) { + sfx_count = data; + } + } + + if ( sfx_count > 255 ) { + sfx_count = 255; + } + if ( sfx_count < 25 ) { + sfx_count = 0; + } + + //Com_Printf("sfx_count = %d\n", sfx_count ); + + // update the amplitude for this entity + s_entityTalkAmplitude[ss->entnum] = (unsigned char)sfx_count; + } +#endif + } + } + + // paint in the channels. + ch = s_channels; + for ( i = 0; i < MAX_CHANNELS; i++, ch++ ) { + if ( ch->startSample == START_SAMPLE_IMMEDIATE || !ch->thesfx || ( ch->leftvol < 0.25 && ch->rightvol < 0.25 ) ) { + continue; + } + + ltime = s_paintedtime; + sc = ch->thesfx; + + // (SA) hmm, why was this commented out? + if ( !sc->inMemory ) { + S_memoryLoad( sc ); + } + + sampleOffset = ltime - ch->startSample; + count = end - ltime; + if ( sampleOffset + count > sc->soundLength ) { + count = sc->soundLength - sampleOffset; + } + + if ( count > 0 ) { +#ifdef TALKANIM + // Ridah, talking animations + // TODO: check that this entity has talking animations enabled! + if ( firstPass && ch->entchannel == CHAN_VOICE && ch->entnum < MAX_CLIENTS ) { + int talkofs, talkcnt, talktime; + // we need to go into the future, since the interpolated behaviour of the facial + // animation creates lag in the time it takes to display the current facial frame + talktime = ltime + (int)( TALK_FUTURE_SEC * (float)s_khz->integer * 1000 ); + talkofs = talktime - ch->startSample; + talkcnt = 100; + if ( talkofs + talkcnt < sc->soundLength ) { + if ( sc->soundCompressionMethod == 1 ) { + S_SetVoiceAmplitudeFromADPCM( sc, talkofs, talkcnt, ch->entnum ); + } else if ( sc->soundCompressionMethod == 2 ) { + S_SetVoiceAmplitudeFromWavelet( sc, talkofs, talkcnt, ch->entnum ); + } else if ( sc->soundCompressionMethod == 3 ) { + S_SetVoiceAmplitudeFromMuLaw( sc, talkofs, talkcnt, ch->entnum ); + } else { + S_SetVoiceAmplitudeFrom16( sc, talkofs, talkcnt, ch->entnum ); + } + } + } +#endif + if ( sc->soundCompressionMethod == 1 ) { + S_PaintChannelFromADPCM( ch, sc, count, sampleOffset, ltime - s_paintedtime ); + } else if ( sc->soundCompressionMethod == 2 ) { + S_PaintChannelFromWavelet( ch, sc, count, sampleOffset, ltime - s_paintedtime ); + } else if ( sc->soundCompressionMethod == 3 ) { + S_PaintChannelFromMuLaw( ch, sc, count, sampleOffset, ltime - s_paintedtime ); + } else { + S_PaintChannelFrom16( ch, sc, count, sampleOffset, ltime - s_paintedtime ); + } + } + } + + // paint in the looped channels. + ch = loop_channels; + for ( i = 0; i < numLoopChannels ; i++, ch++ ) { + sc = ch->thesfx; + if ( !ch->thesfx || ( !ch->leftvol && !ch->rightvol ) ) { + continue; + } + + ltime = s_paintedtime; + + if ( sc->soundData == NULL || sc->soundLength == 0 ) { + continue; + } + // we might have to make two passes if it + // is a looping sound effect and the end of + // the sample is hit + do { + sampleOffset = ( ltime % sc->soundLength ); + + count = end - ltime; + if ( sampleOffset + count > sc->soundLength ) { + count = sc->soundLength - sampleOffset; + } + + if ( count > 0 ) { +#ifdef TALKANIM + // Ridah, talking animations + // TODO: check that this entity has talking animations enabled! + if ( firstPass && ch->entchannel == CHAN_VOICE && ch->entnum < MAX_CLIENTS ) { + int talkofs, talkcnt, talktime; + // we need to go into the future, since the interpolated behaviour of the facial + // animation creates lag in the time it takes to display the current facial frame + talktime = ltime + (int)( TALK_FUTURE_SEC * (float)s_khz->integer * 1000 ); + talkofs = talktime % sc->soundLength; + talkcnt = 100; + if ( talkofs + talkcnt < sc->soundLength ) { + if ( sc->soundCompressionMethod == 1 ) { + S_SetVoiceAmplitudeFromADPCM( sc, talkofs, talkcnt, ch->entnum ); + } else if ( sc->soundCompressionMethod == 2 ) { + S_SetVoiceAmplitudeFromWavelet( sc, talkofs, talkcnt, ch->entnum ); + } else if ( sc->soundCompressionMethod == 3 ) { + S_SetVoiceAmplitudeFromMuLaw( sc, talkofs, talkcnt, ch->entnum ); + } else { + S_SetVoiceAmplitudeFrom16( sc, talkofs, talkcnt, ch->entnum ); + } + } + } +#endif + if ( sc->soundCompressionMethod == 1 ) { + S_PaintChannelFromADPCM( ch, sc, count, sampleOffset, ltime - s_paintedtime ); + } else if ( sc->soundCompressionMethod == 2 ) { + S_PaintChannelFromWavelet( ch, sc, count, sampleOffset, ltime - s_paintedtime ); + } else if ( sc->soundCompressionMethod == 3 ) { + S_PaintChannelFromMuLaw( ch, sc, count, sampleOffset, ltime - s_paintedtime ); + } else { + S_PaintChannelFrom16( ch, sc, count, sampleOffset, ltime - s_paintedtime ); + } + ltime += count; + } + } while ( ltime < end ); + } + + // transfer out according to DMA format + S_TransferPaintBuffer( end ); + s_paintedtime = end; + firstPass = qfalse; + } +} diff --git a/src/client/snd_public.h b/src/client/snd_public.h new file mode 100644 index 0000000..e8ccf16 --- /dev/null +++ b/src/client/snd_public.h @@ -0,0 +1,105 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +#ifdef DOOMSOUND ///// (SA) DOOMSOUND +#ifdef __cplusplus +extern "C" { +#endif +#endif ///// (SA) DOOMSOUND + +void S_Init( void ); +void S_Shutdown( void ); +void S_UpdateThread( void ); + +// if origin is NULL, the sound will be dynamically sourced from the entity +void S_StartSound( vec3_t origin, int entnum, int entchannel, sfxHandle_t sfx ); +void S_StartSoundEx( vec3_t origin, int entnum, int entchannel, sfxHandle_t sfx, int flags ); +void S_StartLocalSound( sfxHandle_t sfx, int channelNum ); + +void S_StartBackgroundTrack( const char *intro, const char *loop, int fadeupTime ); +void S_StopBackgroundTrack( void ); +void S_QueueBackgroundTrack( const char *loop ); //----(SA) added +void S_FadeStreamingSound( float targetvol, int time, int ssNum ); //----(SA) added +void S_FadeAllSounds( float targetvol, int time ); //----(SA) added + +void S_StartStreamingSound( const char *intro, const char *loop, int entnum, int channel, int attenuation ); +void S_StopStreamingSound( int index ); +void S_StopEntStreamingSound( int entNum ); //----(SA) added + +// cinematics and voice-over-network will send raw samples +// 1.0 volume will be direct output of source samples +void S_RawSamples( int samples, int rate, int width, int s_channels, + const byte *data, float lvol, float rvol, int streamingIndex ); + +// stop all sounds and the background track +void S_StopAllSounds( void ); + +// all continuous looping sounds must be added before calling S_Update +void S_ClearLoopingSounds( void ); +void S_ClearSounds( qboolean clearStreaming, qboolean clearMusic ); //----(SA) modified +void S_AddLoopingSound( int entityNum, const vec3_t origin, const vec3_t velocity, const int range, sfxHandle_t sfxHandle, int volume ); +void S_AddRealLoopingSound( int entityNum, const vec3_t origin, const vec3_t velocity, const int range, sfxHandle_t sfx ); +void S_StopLoopingSound( int entityNum ); + +#ifdef DOOMSOUND ///// (SA) DOOMSOUND +void S_ClearSoundBuffer( void ); +#endif ///// (SA) DOOMSOUND +// recompute the reletive volumes for all running sounds +// reletive to the given entityNum / orientation +void S_Respatialize( int entityNum, const vec3_t origin, vec3_t axis[3], int inwater ); + +// let the sound system know where an entity currently is +void S_UpdateEntityPosition( int entityNum, const vec3_t origin ); + +void S_Update( void ); + +void S_DisableSounds( void ); + +void S_BeginRegistration( void ); + +// RegisterSound will allways return a valid sample, even if it +// has to create a placeholder. This prevents continuous filesystem +// checks for missing files +#ifdef DOOMSOUND ///// (SA) DOOMSOUND +sfxHandle_t S_RegisterSound( const char *sample ); +#else +sfxHandle_t S_RegisterSound( const char *sample, qboolean compressed ); +#endif ///// (SA) DOOMSOUND + +void S_DisplayFreeMemory( void ); + +// +int S_GetVoiceAmplitude( int entityNum ); + + +#ifdef DOOMSOUND ///// (SA) DOOMSOUND +#ifdef __cplusplus +} +#endif +#endif ///// (SA) DOOMSOUND diff --git a/src/client/snd_wavelet.c b/src/client/snd_wavelet.c new file mode 100644 index 0000000..892fb0f --- /dev/null +++ b/src/client/snd_wavelet.c @@ -0,0 +1,280 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: snd_wavelet.c + * + * desc: + * + * + *****************************************************************************/ + +#include "snd_local.h" + +long myftol( float f ); + +#define C0 0.4829629131445341 +#define C1 0.8365163037378079 +#define C2 0.2241438680420134 +#define C3 -0.1294095225512604 + +void daub4( float b[], unsigned long n, int isign ) { + float wksp[4097]; + float *a = b - 1; // numerical recipies so a[1] = b[0] + + unsigned long nh,nh1,i,j; + + if ( n < 4 ) { + return; + } + + nh1 = ( nh = n >> 1 ) + 1; + if ( isign >= 0 ) { + for ( i = 1, j = 1; j <= n - 3; j += 2, i++ ) { + wksp[i] = C0 * a[j] + C1 * a[j + 1] + C2 * a[j + 2] + C3 * a[j + 3]; + wksp[i + nh] = C3 * a[j] - C2 * a[j + 1] + C1 * a[j + 2] - C0 * a[j + 3]; + } + wksp[i ] = C0 * a[n - 1] + C1 * a[n] + C2 * a[1] + C3 * a[2]; + wksp[i + nh] = C3 * a[n - 1] - C2 * a[n] + C1 * a[1] - C0 * a[2]; + } else { + wksp[1] = C2 * a[nh] + C1 * a[n] + C0 * a[1] + C3 * a[nh1]; + wksp[2] = C3 * a[nh] - C0 * a[n] + C1 * a[1] - C2 * a[nh1]; + for ( i = 1, j = 3; i < nh; i++ ) { + wksp[j++] = C2 * a[i] + C1 * a[i + nh] + C0 * a[i + 1] + C3 * a[i + nh1]; + wksp[j++] = C3 * a[i] - C0 * a[i + nh] + C1 * a[i + 1] - C2 * a[i + nh1]; + } + } + for ( i = 1; i <= n; i++ ) { + a[i] = wksp[i]; + } +} + +void wt1( float a[], unsigned long n, int isign ) { + unsigned long nn; + int inverseStartLength = n / 4; + if ( n < inverseStartLength ) { + return; + } + if ( isign >= 0 ) { + for ( nn = n; nn >= inverseStartLength; nn >>= 1 ) daub4( a,nn,isign ); + } else { + for ( nn = inverseStartLength; nn <= n; nn <<= 1 ) daub4( a,nn,isign ); + } +} + +/* The number of bits required by each value */ +static unsigned char numBits[] = { + 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, + 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, +}; + +byte MuLawEncode( short s ) { + unsigned long adjusted; + byte sign, exponent, mantissa; + + sign = ( s < 0 ) ? 0 : 0x80; + + if ( s < 0 ) { + s = -s; + } + adjusted = (long)s << ( 16 - sizeof( short ) * 8 ); + adjusted += 128L + 4L; + if ( adjusted > 32767 ) { + adjusted = 32767; + } + exponent = numBits[( adjusted >> 7 ) & 0xff] - 1; + mantissa = ( adjusted >> ( exponent + 3 ) ) & 0xf; + return ~( sign | ( exponent << 4 ) | mantissa ); +} + +short MuLawDecode( byte uLaw ) { + signed long adjusted; + byte exponent, mantissa; + + uLaw = ~uLaw; + exponent = ( uLaw >> 4 ) & 0x7; + mantissa = ( uLaw & 0xf ) + 16; + adjusted = ( mantissa << ( exponent + 3 ) ) - 128 - 4; + + return ( uLaw & 0x80 ) ? adjusted : -adjusted; +} + +short mulawToShort[256]; +static qboolean madeTable = qfalse; + +static int NXStreamCount; + +void NXPutc( NXStream *stream, char out ) { + stream[NXStreamCount++] = out; +} + + +void encodeWavelet( sfx_t *sfx, short *packets ) { + float wksp[4097], temp; + int i, samples, size; + sndBuffer *newchunk, *chunk; + byte *out; + + if ( !madeTable ) { + for ( i = 0; i < 256; i++ ) { + mulawToShort[i] = (float)MuLawDecode( (byte)i ); + } + madeTable = qtrue; + } + chunk = NULL; + + samples = sfx->soundLength; + while ( samples > 0 ) { + size = samples; + if ( size > ( SND_CHUNK_SIZE * 2 ) ) { + size = ( SND_CHUNK_SIZE * 2 ); + } + + if ( size < 4 ) { + size = 4; + } + + newchunk = SND_malloc(); + if ( sfx->soundData == NULL ) { + sfx->soundData = newchunk; + } else { + chunk->next = newchunk; + } + chunk = newchunk; + for ( i = 0; i < size; i++ ) { + wksp[i] = *packets; + packets++; + } + wt1( wksp, size, 1 ); + out = (byte *)chunk->sndChunk; + + for ( i = 0; i < size; i++ ) { + temp = wksp[i]; + if ( temp > 32767 ) { + temp = 32767; + } else if ( temp < -32768 ) { + temp = -32768; + } + out[i] = MuLawEncode( (short)temp ); + } + + chunk->size = size; + samples -= size; + } +} + +void decodeWavelet( sndBuffer *chunk, short *to ) { + float wksp[4097]; + int i; + byte *out; + + int size = chunk->size; + + out = (byte *)chunk->sndChunk; + for ( i = 0; i < size; i++ ) { + wksp[i] = mulawToShort[out[i]]; + } + + wt1( wksp, size, -1 ); + + if ( !to ) { + return; + } + + for ( i = 0; i < size; i++ ) { + to[i] = wksp[i]; + } +} + + +void encodeMuLaw( sfx_t *sfx, short *packets ) { + int i, samples, size, grade, poop; + sndBuffer *newchunk, *chunk; + byte *out; + + if ( !madeTable ) { + for ( i = 0; i < 256; i++ ) { + mulawToShort[i] = (float)MuLawDecode( (byte)i ); + } + madeTable = qtrue; + } + + chunk = NULL; + samples = sfx->soundLength; + grade = 0; + + while ( samples > 0 ) { + size = samples; + if ( size > ( SND_CHUNK_SIZE * 2 ) ) { + size = ( SND_CHUNK_SIZE * 2 ); + } + + newchunk = SND_malloc(); + if ( sfx->soundData == NULL ) { + sfx->soundData = newchunk; + } else { + chunk->next = newchunk; + } + chunk = newchunk; + out = (byte *)chunk->sndChunk; + for ( i = 0; i < size; i++ ) { + poop = packets[0] + grade; + if ( poop > 32767 ) { + poop = 32767; + } else if ( poop < -32768 ) { + poop = -32768; + } + out[i] = MuLawEncode( (short)poop ); + grade = poop - mulawToShort[out[i]]; + packets++; + } + chunk->size = size; + samples -= size; + } +} + +void decodeMuLaw( sndBuffer *chunk, short *to ) { + int i; + byte *out; + + int size = chunk->size; + + out = (byte *)chunk->sndChunk; + for ( i = 0; i < size; i++ ) { + to[i] = mulawToShort[out[i]]; + } +} + + diff --git a/src/extractfuncs/ChangeLog b/src/extractfuncs/ChangeLog new file mode 100644 index 0000000..c20e613 --- /dev/null +++ b/src/extractfuncs/ChangeLog @@ -0,0 +1,17 @@ +2001-12-07 Timothee Besset + +Imported from the Wolf MP version, Mac/Linux friendly version +Fixed argbase bug in *nix main +Escape BoxOnPlaneSide on linux (taken out by preprocessing) + +2001-11-02 Timothee Besset + +Modified extractfuncs to works on linux +Would still need to integrate it correctly with the build system +Changed the command line syntax of the linux ver: +screwup [-o ] [ ..] + +if none specified, is "g_funcs" + +on linux at least, those header files need to be tweaked by hand +because unresolved externs are not ignored by gcc (otherwise harmless on win32) diff --git a/src/extractfuncs/Conscript b/src/extractfuncs/Conscript new file mode 100644 index 0000000..f8be465 --- /dev/null +++ b/src/extractfuncs/Conscript @@ -0,0 +1,9 @@ +# extractfuncs building + +$env = new cons( + CC => 'gcc', + CFLAGS => '-g -DSCREWUP ' + ); + +Program $env 'extractfuncs', 'extractfuncs.c', 'l_log.c', 'l_memory.c', 'l_precomp.c', 'l_script.c'; +Install $env '#', 'extractfuncs'; diff --git a/src/extractfuncs/extractfuncs.bat b/src/extractfuncs/extractfuncs.bat new file mode 100644 index 0000000..57d45ba --- /dev/null +++ b/src/extractfuncs/extractfuncs.bat @@ -0,0 +1,3 @@ +cd ..\game +..\extractfuncs\extractfuncs *.c +cd .. \ No newline at end of file diff --git a/src/extractfuncs/extractfuncs.c b/src/extractfuncs/extractfuncs.c new file mode 100644 index 0000000..c6e9903 --- /dev/null +++ b/src/extractfuncs/extractfuncs.c @@ -0,0 +1,653 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +#include +#include +#include +#include +#include +#ifdef _WIN32 +#include +#include +#endif +#include "l_memory.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_log.h" + +typedef enum {false, true} qboolean; + +//#define PATHSEPERATOR_STR "\\" + +void Error( char *error, ... ) { + va_list argptr; + + va_start( argptr, error ); + vprintf( error, argptr ); + va_end( argptr ); + + exit( 1 ); +} + +/* +int FileLength (FILE *f) +{ + int pos; + int end; + + pos = ftell (f); + fseek (f, 0, SEEK_END); + end = ftell (f); + fseek (f, pos, SEEK_SET); + + return end; +} //end of the function FileLength + +void Remove(char *buf, int length, char *from, char *to, char *skip) +{ + int i, remove = false; + + for (i = 0; i < length; i++) + { + if (remove) + { + if ((unsigned) length - i > strlen(skip)) + { + if (!strncmp(&buf[i], skip, strlen(skip))) + { + i += strlen(skip); + } //end if + } //end if + if ((unsigned) length - i > strlen(to)) + { + if (!strncmp(&buf[i], to, strlen(to))) + { + length = i + strlen(to); + } //end if + } //end if + if (buf[i]) buf[i] = 'a'; + } //end if + else + { + if ((unsigned) length - i < strlen(from)) return; + if (!strncmp(&buf[i], from, strlen(from))) remove = true; + } //end else + } //end for +} //end of the function Remove + +void main(int argc, char *argv[]) +{ + FILE *fp; + int filelength; + char *from, *to, *skip, *ptr; + + if (argc < 2) Error("USAGE: screwup "); + fp = fopen(argv[1], "rb"); + if (!fp) Error("error opening %s\n", argv[1]); + + filelength = FileLength(fp); + ptr = malloc(filelength); + fread(ptr, filelength, 1, fp); + fclose(fp); + + from = argv[3];//"be_aas_bspq2.c"; + to = argv[4];//"BotWeaponNameFromModel"; + skip = "GetBotAPI"; + + Remove(ptr, filelength, from, to, skip); + + fp = fopen(argv[2], "wb"); + if (!fp) Error("error opening %s\n", argv[2]); + fwrite(ptr, filelength, 1, fp); + fclose(fp); + + free(ptr); +} //end of the function main +*/ + +typedef struct replacefunc_s +{ + char *name; + char *newname; + char *filename; + char dec[MAX_TOKEN]; //function declaration + struct replacefunc_s *next; +} replacefunc_t; + +replacefunc_t *replacefuncs; +int numfuncs; + +extern int Q_stricmp( const char *s1, const char *s2 ); + +// the function names +//#define DEFAULT_FUNCBASE "g_func" +static char *func_filename = "g_funcs.h"; +static char *func_filedesc = "g_func_decs.h"; + +void DumpReplaceFunctions( void ) { + replacefunc_t *rf; + char path[_MAX_PATH]; + FILE *f; + int len, newlen; + unsigned char *buf, *newbuf; + int updated; + + updated = 0; + + // dump the function header + strcpy( path, "." ); + strcat( path, PATHSEPERATOR_STR ); + strcat( path, "g_funcs.tmp" ); + Log_Open( path ); + for ( rf = replacefuncs; rf; rf = rf->next ) + { + Log_Print( "{\"%s\", (byte *)%s},\n", rf->name, rf->name ); + } //end for + Log_Print( "{0, 0}\n" ); + Log_Close(); + + // if it's different, rename the file over the real header + strcpy( path, "g_funcs.tmp" ); + f = fopen( path, "rb" ); + fseek( f, 0, SEEK_END ); + len = ftell( f ); + buf = (unsigned char *) malloc( len + 1 ); + fseek( f, 0, SEEK_SET ); + fread( buf, len, 1, f ); + buf[len] = 0; + fclose( f ); + + strcpy( path, func_filename ); + if ( f = fopen( path, "rb" ) ) { + fseek( f, 0, SEEK_END ); + newlen = ftell( f ); + newbuf = (unsigned char *) malloc( newlen + 1 ); + fseek( f, 0, SEEK_SET ); + fread( newbuf, newlen, 1, f ); + newbuf[newlen] = 0; + fclose( f ); + + if ( len != newlen || Q_stricmp( buf, newbuf ) ) { + char newpath[_MAX_PATH]; + + // delete the old file, rename the new one + strcpy( path, func_filename ); + remove( path ); + + strcpy( newpath, "g_funcs.tmp" ); + rename( newpath, path ); + + // make g_save recompile itself + remove( "debug\\g_save.obj" ); + remove( "debug\\g_save.sbr" ); + remove( "release\\g_save.obj" ); + remove( "release\\g_save.sbr" ); + + updated = 1; + } else { + // delete the old file + strcpy( path, "g_funcs.tmp" ); + remove( path ); + } + } else { + rename( "g_funcs.tmp", func_filename ); + } + + free( buf ); + free( newbuf ); + + // dump the function declarations + strcpy( path, "g_func_decs.tmp" ); + Log_Open( path ); + for ( rf = replacefuncs; rf; rf = rf->next ) + { + Log_Print( "extern %s;\n", rf->dec ); + } //end for + Log_Close(); + + // if it's different, rename the file over the real header + strcpy( path, "g_func_decs.tmp" ); + f = fopen( path, "rb" ); + fseek( f, 0, SEEK_END ); + len = ftell( f ); + buf = (unsigned char *) malloc( len + 1 ); + fseek( f, 0, SEEK_SET ); + fread( buf, len, 1, f ); + buf[len] = 0; + fclose( f ); + + strcpy( path, func_filedesc ); + if ( f = fopen( path, "rb" ) ) { + fseek( f, 0, SEEK_END ); + newlen = ftell( f ); + newbuf = (unsigned char *) malloc( newlen + 1 ); + fseek( f, 0, SEEK_SET ); + fread( newbuf, newlen, 1, f ); + newbuf[newlen] = 0; + fclose( f ); + + if ( len != newlen || Q_stricmp( buf, newbuf ) ) { + char newpath[_MAX_PATH]; + + // delete the old file, rename the new one + strcpy( path, func_filedesc ); + remove( path ); + + strcpy( newpath, "g_func_decs.tmp" ); + rename( newpath, path ); + + // make g_save recompile itself + // NOTE TTimo win32 only? (harmless on *nix anyway) + remove( "debug\\g_save.obj" ); + remove( "debug\\g_save.sbr" ); + remove( "release\\g_save.obj" ); + remove( "release\\g_save.sbr" ); + + updated = 1; + } else { + // delete the old file + strcpy( path, "g_func_decs.tmp" ); + remove( path ); + } + } else { + rename( "g_func_decs.tmp", func_filedesc ); + } + + free( buf ); + free( newbuf ); + + if ( updated ) { + printf( "Updated the function table, recompile required.\n" ); + } + +} //end of the function DumpReplaceFunctions + +replacefunc_t *FindFunctionName( char *funcname ) { + replacefunc_t *f; + + for ( f = replacefuncs; f; f = f->next ) + { + if ( !strcmp( f->name, funcname ) ) { + return f; + } + } //end for + return NULL; +} //end of the function FindFunctionName + +int MayScrewUp( char *funcname ) { + if ( !strcmp( funcname, "GetBotAPI" ) ) { + return false; + } + if ( !strcmp( funcname, "main" ) ) { + return false; + } + if ( !strcmp( funcname, "WinMain" ) ) { + return false; + } + return true; +} //end of the function MayScrewUp + +typedef struct tokenList_s { + token_t token; + struct tokenList_s *next; +} tokenList_t; + +#define MAX_TOKEN_LIST 64 +tokenList_t tokenList[MAX_TOKEN_LIST]; +int tokenListHead = 0; + +void ConcatDec( tokenList_t *list, char *str, int inc ) { +/* + if (!((list->token.type == TT_NAME) || (list->token.string[0] == '*'))) { + if (list->token.string[0] == ')' || list->token.string[0] == '(') { + if (inc++ >= 2) + return; + } else { + return; + } + } +*/ + if ( list->next ) { + ConcatDec( list->next, str, inc ); + } + strcat( str, list->token.string ); + strcat( str, " " ); +} + +void AddFunctionName( char *funcname, char *filename, tokenList_t *head ) { + replacefunc_t *f; + tokenList_t *list; + + if ( FindFunctionName( funcname ) ) { + return; + } + +#if defined( __linux__ ) + // the bad thing is, this doesn't preprocess .. on __linux__ this + // function is not implemented (q_math.c) + if ( !Q_stricmp( funcname, "BoxOnPlaneSide" ) ) { + return; + } +#endif + + // NERVE - SMF - workaround for Graeme's predifined MACOSX functions + // TTimo - looks like linux version needs to escape those too +#if defined( _WIN32 ) || defined( __linux__ ) + if ( !Q_stricmp( funcname, "qmax" ) ) { + return; + } else if ( !Q_stricmp( funcname, "qmin" ) ) { + return; + } +#endif + // -NERVE - SMF + + f = (replacefunc_t *) GetMemory( sizeof( replacefunc_t ) + strlen( funcname ) + 1 + 6 + strlen( filename ) + 1 ); + f->name = (char *) f + sizeof( replacefunc_t ); + strcpy( f->name, funcname ); + f->newname = (char *) f + sizeof( replacefunc_t ) + strlen( funcname ) + 1; + sprintf( f->newname, "F%d", numfuncs++ ); + f->filename = (char *) f + sizeof( replacefunc_t ) + strlen( funcname ) + 1 + strlen( f->newname ) + 1; + strcpy( f->filename, filename ); + f->next = replacefuncs; + replacefuncs = f; + + // construct the declaration + list = head; + f->dec[0] = '\0'; + ConcatDec( list, f->dec, 0 ); + +} //end of the function AddFunctionName + +void AddTokenToList( tokenList_t **head, token_t *token ) { + tokenList_t *newhead; + + newhead = &tokenList[tokenListHead++]; //GetMemory( sizeof( tokenList_t ) ); + if ( tokenListHead == MAX_TOKEN_LIST ) { + tokenListHead = 0; + } + + newhead->next = *head; + newhead->token = *token; + + *head = newhead; +} +/* +void KillTokenList( tokenList_t *head ) +{ + if (head->next) { + KillTokenList( head->next ); + FreeMemory( head->next ); + head->next = NULL; + } +} +*/ +void StripTokenList( tokenList_t *head ) { + tokenList_t *trav, *lastTrav; + + trav = head; + + // now go back to the start of the declaration + lastTrav = trav; + trav = trav->next; // should be on the function name now + while ( ( trav->token.type == TT_NAME ) || ( trav->token.string[0] == '*' ) ) { + lastTrav = trav; + trav = trav->next; + if ( !trav ) { + return; + } + } + // now kill everything after lastTrav +// KillTokenList( lastTrav ); + lastTrav->next = NULL; +} + +void GetFunctionNamesFromFile( char *filename ) { + source_t *source; + token_t token, lasttoken; + int indent = 0, brace; + int isStatic = 0; + tokenList_t *listHead; + + // filter some files out + if ( !Q_stricmp( filename, "bg_lib.c" ) ) { + return; + } + + listHead = NULL; + source = LoadSourceFile( filename ); + if ( !source ) { + Error( "error opening %s", filename ); + return; + } //end if +// printf("loaded %s\n", filename); +// if (!PC_ReadToken(source, &lasttoken)) +// { +// FreeSource(source); +// return; +// } //end if + while ( 1 ) + { + if ( !PC_ReadToken( source, &token ) ) { + break; + } + AddTokenToList( &listHead, &token ); + if ( token.type == TT_PUNCTUATION ) { + switch ( token.string[0] ) + { + case ';': + { + isStatic = 0; + break; + } + case '{': + { + indent++; + break; + } //end case + case '}': + { + indent--; + if ( indent < 0 ) { + indent = 0; + } + break; + } //end case + case '(': + { + if ( indent <= 0 && lasttoken.type == TT_NAME ) { + StripTokenList( listHead ); + + brace = 1; + while ( PC_ReadToken( source, &token ) ) + { + AddTokenToList( &listHead, &token ); + if ( token.string[0] == '(' ) { + brace++; + } //end if + else if ( token.string[0] == ')' ) { + brace--; + if ( brace <= 0 ) { + if ( !PC_ReadToken( source, &token ) ) { + break; + } + if ( token.string[0] == '{' ) { + indent++; + if ( !isStatic && MayScrewUp( lasttoken.string ) ) { + AddFunctionName( lasttoken.string, filename, listHead ); + } //end if + } //end if + break; + } //end if + } //end if + } //end while + } //end if + break; + } //end case + } //end if + } //end switch + if ( token.type == TT_NAME ) { + if ( token.string[0] == 's' && !strcmp( token.string, "static" ) ) { + isStatic = 1; + } + } + memcpy( &lasttoken, &token, sizeof( token_t ) ); + } //end while + FreeSource( source ); +} //end of the function GetFunctionNamesFromFile + +void WriteWhiteSpace( FILE *fp, script_t *script ) { + int c; + //write out the white space + c = PS_NextWhiteSpaceChar( script ); + while ( c ) + { + //NOTE: do NOT write out carriage returns (for unix/linux compatibility + if ( c != 13 ) { + fputc( c, fp ); + } + c = PS_NextWhiteSpaceChar( script ); + } //end while +} //end of the function WriteWhiteSpace + +void WriteString( FILE *fp, script_t *script ) { + char *ptr; + + ptr = script->endwhitespace_p; + while ( ptr < script->script_p ) + { + fputc( *ptr, fp ); + ptr++; + } //end while +} //end of the function WriteString + +void ScrewUpFile( char *oldfile, char *newfile ) { + FILE *fp; + script_t *script; + token_t token; + replacefunc_t *f; + char *ptr; + + printf( "screwing up file %s\n", oldfile ); + script = LoadScriptFile( oldfile ); + if ( !script ) { + Error( "error opening %s\n", oldfile ); + } + fp = fopen( newfile, "wb" ); + if ( !fp ) { + Error( "error opening %s\n", newfile ); + } + // + while ( PS_ReadToken( script, &token ) ) + { + WriteWhiteSpace( fp, script ); + if ( token.type == TT_NAME ) { + f = FindFunctionName( token.string ); + if ( f ) { + ptr = f->newname; + } else { ptr = token.string;} + while ( *ptr ) + { + fputc( *ptr, fp ); + ptr++; + } //end while + } //end if + else + { + WriteString( fp, script ); + } //end else + } //end while + WriteWhiteSpace( fp, script ); + FreeMemory( script ); + fclose( fp ); +} //end of the function ScrewUpFile + +int verbose = 0; + +#ifdef _WIN32 + +void main( int argc, char *argv[] ) { + WIN32_FIND_DATA filedata; + HWND handle; + int done; //, i; + + if ( argc < 2 ) { + Error( "USAGE: screwup \n" ); + } //end if + + handle = FindFirstFile( argv[1], &filedata ); + done = ( handle == INVALID_HANDLE_VALUE ); + while ( !done ) + { + if ( !( filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) ) { + // + GetFunctionNamesFromFile( filedata.cFileName ); + } //end if + //find the next file + done = !FindNextFile( handle, &filedata ); + } //end while + DumpReplaceFunctions(); +} //end of the function main + +#else + +void Usage() { + Error( "USAGE: screwup [-o ] [ ..]\n" + "no -o defaults to g_funcs.h g_func_decs.h\n" ); +} + +/* +*nix version, let the shell do the pattern matching +(that's what shells are for :-)) +*/ +int main( int argc, char *argv[] ) { + int i; + int argbase = 1; + + if ( argc < 2 ) { + Usage(); + } //end if + + if ( !Q_stricmp( argv[1],"-o" ) ) { + if ( argc < 5 ) { + Usage(); + } + func_filename = argv[2]; + func_filedesc = argv[3]; + argbase = 4; + } + + for ( i = argbase; i < argc; i++ ) + { + printf( "%d: %s\n", i, argv[i] ); + GetFunctionNamesFromFile( argv[i] ); + } + DumpReplaceFunctions(); +} + +#endif diff --git a/src/extractfuncs/extractfuncs.vcproj b/src/extractfuncs/extractfuncs.vcproj new file mode 100644 index 0000000..5af63b7 --- /dev/null +++ b/src/extractfuncs/extractfuncs.vcproj @@ -0,0 +1,428 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/extractfuncs/l_log.c b/src/extractfuncs/l_log.c new file mode 100644 index 0000000..afa0072 --- /dev/null +++ b/src/extractfuncs/l_log.c @@ -0,0 +1,199 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: l_log.c +// Function: log file stuff +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-31 +// Tab Size: 3 +//=========================================================================== + +#include +#include +#include + +#define MAX_QPATH 64 +#include "../bspc/qbsp.h" + +#define MAX_LOGFILENAMESIZE 1024 + +typedef struct logfile_s +{ + char filename[MAX_LOGFILENAMESIZE]; + FILE *fp; + int numwrites; +} logfile_t; + +logfile_t logfile; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Log_Open( char *filename ) { + if ( !filename || !strlen( filename ) ) { + printf( "openlog \n" ); + return; + } //end if + if ( logfile.fp ) { + printf( "log file %s is already opened\n", logfile.filename ); + return; + } //end if + logfile.fp = fopen( filename, "wb" ); + if ( !logfile.fp ) { + printf( "can't open the log file %s\n", filename ); + return; + } //end if + strncpy( logfile.filename, filename, MAX_LOGFILENAMESIZE ); +// printf("Opened log %s\n", logfile.filename); +} //end of the function Log_Create +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Log_Close( void ) { + if ( !logfile.fp ) { + printf( "no log file to close\n" ); + return; + } //end if + if ( fclose( logfile.fp ) ) { + printf( "can't close log file %s\n", logfile.filename ); + return; + } //end if + logfile.fp = NULL; +// printf("Closed log %s\n", logfile.filename); +} //end of the function Log_Close +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Log_Shutdown( void ) { + if ( logfile.fp ) { + Log_Close(); + } +} //end of the function Log_Shutdown +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Log_Print( char *fmt, ... ) { + va_list ap; +#ifdef WINBSPC + char buf[2048]; +#endif //WINBSPC + + if ( verbose ) { + va_start( ap, fmt ); +#ifdef WINBSPC + vsprintf( buf, fmt, ap ); + WinBSPCPrint( buf ); +#else + vprintf( fmt, ap ); +#endif //WINBSPS + va_end( ap ); + } //end if + + va_start( ap, fmt ); + if ( logfile.fp ) { + vfprintf( logfile.fp, fmt, ap ); + fflush( logfile.fp ); + } //end if + va_end( ap ); +} //end of the function Log_Print +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Log_Write( char *fmt, ... ) { + va_list ap; + + if ( !logfile.fp ) { + return; + } + va_start( ap, fmt ); + vfprintf( logfile.fp, fmt, ap ); + va_end( ap ); + fflush( logfile.fp ); +} //end of the function Log_Write +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Log_WriteTimeStamped( char *fmt, ... ) { + va_list ap; + + if ( !logfile.fp ) { + return; + } +/* fprintf(logfile.fp, "%d %02d:%02d:%02d:%02d ", + logfile.numwrites, + (int) (botlibglobals.time / 60 / 60), + (int) (botlibglobals.time / 60), + (int) (botlibglobals.time), + (int) ((int) (botlibglobals.time * 100)) - + ((int) botlibglobals.time) * 100);*/ + va_start( ap, fmt ); + vfprintf( logfile.fp, fmt, ap ); + va_end( ap ); + logfile.numwrites++; + fflush( logfile.fp ); +} //end of the function Log_Write +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +FILE *Log_FileStruct( void ) { + return logfile.fp; +} //end of the function Log_FileStruct +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void Log_Flush( void ) { + if ( logfile.fp ) { + fflush( logfile.fp ); + } +} //end of the function Log_Flush diff --git a/src/extractfuncs/l_log.h b/src/extractfuncs/l_log.h new file mode 100644 index 0000000..b1f6c1b --- /dev/null +++ b/src/extractfuncs/l_log.h @@ -0,0 +1,59 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: l_log.h +// Function: log file stuff +// Programmer: Mr Elusive (MrElusive@demigod.demon.nl) +// Last update: 1997-12-31 +// Tab Size: 3 +//=========================================================================== + +//open a log file +void Log_Open( char *filename ); +//close the current log file +void Log_Close( void ); +//close log file if present +void Log_Shutdown( void ); +//print on stdout and write to the current opened log file +void Log_Print( char *fmt, ... ); +//write to the current opened log file +void Log_Write( char *fmt, ... ); +//write to the current opened log file with a time stamp +void Log_WriteTimeStamped( char *fmt, ... ); +//returns the log file structure +FILE *Log_FileStruct( void ); +//flush log file +void Log_Flush( void ); + +int Log_Written( void ); + +#ifdef WINBSPC +void WinBSPCPrint( char *str ); +#endif //WINBSPC diff --git a/src/extractfuncs/l_memory.c b/src/extractfuncs/l_memory.c new file mode 100644 index 0000000..71526c0 --- /dev/null +++ b/src/extractfuncs/l_memory.c @@ -0,0 +1,444 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: l_memory.c + * + * desc: memory allocation + * + * + *****************************************************************************/ + +#include "../game/q_shared.h" +#include "../game/botlib.h" +#include "l_log.h" +#include "../../src/botlib/be_interface.h" + +//#define MEMDEBUG +//#define MEMORYMANEGER + +#define MEM_ID 0x12345678l +#define HUNK_ID 0x87654321l + +int allocatedmemory; +int totalmemorysize; +int numblocks; + +#ifdef MEMORYMANEGER + +typedef struct memoryblock_s +{ + unsigned long int id; + void *ptr; + int size; +#ifdef MEMDEBUG + char *label; + char *file; + int line; +#endif //MEMDEBUG + struct memoryblock_s *prev, *next; +} memoryblock_t; + +memoryblock_t *memory; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void LinkMemoryBlock( memoryblock_t *block ) { + block->prev = NULL; + block->next = memory; + if ( memory ) { + memory->prev = block; + } + memory = block; +} //end of the function LinkMemoryBlock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void UnlinkMemoryBlock( memoryblock_t *block ) { + if ( block->prev ) { + block->prev->next = block->next; + } else { memory = block->next;} + if ( block->next ) { + block->next->prev = block->prev; + } +} //end of the function UnlinkMemoryBlock +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef MEMDEBUG +void *GetMemoryDebug( unsigned long size, char *label, char *file, int line ) +#else +void *GetMemory( unsigned long size ) +#endif //MEMDEBUG +{ + void *ptr; + memoryblock_t *block; + + ptr = malloc( size + sizeof( memoryblock_t ) ); + block = (memoryblock_t *) ptr; + block->id = MEM_ID; + block->ptr = (char *) ptr + sizeof( memoryblock_t ); + block->size = size + sizeof( memoryblock_t ); +#ifdef MEMDEBUG + block->label = label; + block->file = file; + block->line = line; +#endif //MEMDEBUG + LinkMemoryBlock( block ); + allocatedmemory += block->size; + totalmemorysize += block->size + sizeof( memoryblock_t ); + numblocks++; + return block->ptr; +} //end of the function GetMemoryDebug +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef MEMDEBUG +void *GetClearedMemoryDebug( unsigned long size, char *label, char *file, int line ) +#else +void *GetClearedMemory( unsigned long size ) +#endif //MEMDEBUG +{ + void *ptr; +#ifdef MEMDEBUG + ptr = GetMemoryDebug( size, label, file, line ); +#else + ptr = GetMemory( size ); +#endif //MEMDEBUG + memset( ptr, 0, size ); + return ptr; +} //end of the function GetClearedMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef MEMDEBUG +void *GetHunkMemoryDebug( unsigned long size, char *label, char *file, int line ) +#else +void *GetHunkMemory( unsigned long size ) +#endif //MEMDEBUG +{ + void *ptr; + memoryblock_t *block; + + ptr = malloc( size + sizeof( memoryblock_t ) ); + block = (memoryblock_t *) ptr; + block->id = HUNK_ID; + block->ptr = (char *) ptr + sizeof( memoryblock_t ); + block->size = size + sizeof( memoryblock_t ); +#ifdef MEMDEBUG + block->label = label; + block->file = file; + block->line = line; +#endif //MEMDEBUG + LinkMemoryBlock( block ); + allocatedmemory += block->size; + totalmemorysize += block->size + sizeof( memoryblock_t ); + numblocks++; + return block->ptr; +} //end of the function GetHunkMemoryDebug +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef MEMDEBUG +void *GetClearedHunkMemoryDebug( unsigned long size, char *label, char *file, int line ) +#else +void *GetClearedHunkMemory( unsigned long size ) +#endif //MEMDEBUG +{ + void *ptr; +#ifdef MEMDEBUG + ptr = GetHunkMemoryDebug( size, label, file, line ); +#else + ptr = GetHunkMemory( size ); +#endif //MEMDEBUG + memset( ptr, 0, size ); + return ptr; +} //end of the function GetClearedHunkMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +memoryblock_t *BlockFromPointer( void *ptr, char *str ) { + memoryblock_t *block; + + if ( !ptr ) { +#ifdef MEMDEBUG + //char *crash = (char *) NULL; + //crash[0] = 1; + printf( PRT_FATAL, "%s: NULL pointer\n", str ); +#endif MEMDEBUG + return NULL; + } //end if + block = ( memoryblock_t * )( (char *) ptr - sizeof( memoryblock_t ) ); + if ( block->id != MEM_ID && block->id != HUNK_ID ) { + printf( PRT_FATAL, "%s: invalid memory block\n", str ); + return NULL; + } //end if + if ( block->ptr != ptr ) { + printf( PRT_FATAL, "%s: memory block pointer invalid\n", str ); + return NULL; + } //end if + return block; +} //end of the function BlockFromPointer +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FreeMemory( void *ptr ) { + memoryblock_t *block; + + block = BlockFromPointer( ptr, "FreeMemory" ); + if ( !block ) { + return; + } + UnlinkMemoryBlock( block ); + allocatedmemory -= block->size; + totalmemorysize -= block->size + sizeof( memoryblock_t ); + numblocks--; + // + if ( block->id == MEM_ID ) { + free( block ); + } //end if +} //end of the function FreeMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +int MemoryByteSize( void *ptr ) { + memoryblock_t *block; + + block = BlockFromPointer( ptr, "MemoryByteSize" ); + if ( !block ) { + return 0; + } + return block->size; +} //end of the function MemoryByteSize +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void PrintUsedMemorySize( void ) { + printf( PRT_MESSAGE, "total allocated memory: %d KB\n", allocatedmemory >> 10 ); + printf( PRT_MESSAGE, "total botlib memory: %d KB\n", totalmemorysize >> 10 ); + printf( PRT_MESSAGE, "total memory blocks: %d\n", numblocks ); +} //end of the function PrintUsedMemorySize +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void PrintMemoryLabels( void ) { + memoryblock_t *block; + int i; + + PrintUsedMemorySize(); + i = 0; + Log_Write( "\r\n" ); + for ( block = memory; block; block = block->next ) + { +#ifdef MEMDEBUG + if ( block->id == HUNK_ID ) { + Log_Write( "%6d, hunk %p, %8d: %24s line %6d: %s\r\n", i, block->ptr, block->size, block->file, block->line, block->label ); + } //end if + else + { + Log_Write( "%6d, %p, %8d: %24s line %6d: %s\r\n", i, block->ptr, block->size, block->file, block->line, block->label ); + } //end else +#endif //MEMDEBUG + i++; + } //end for +} //end of the function PrintMemoryLabels +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void DumpMemory( void ) { + memoryblock_t *block; + + for ( block = memory; block; block = memory ) + { + FreeMemory( block->ptr ); + } //end for + totalmemorysize = 0; + allocatedmemory = 0; +} //end of the function DumpMemory + +#else + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef MEMDEBUG +void *GetMemoryDebug( unsigned long size, char *label, char *file, int line ) +#else +void *GetMemory( unsigned long size ) +#endif //MEMDEBUG +{ + void *ptr; + unsigned long int *memid; + + ptr = malloc( size + sizeof( unsigned long int ) ); + if ( !ptr ) { + return NULL; + } + memid = (unsigned long int *) ptr; + *memid = MEM_ID; + return (unsigned long int *) ( (char *) ptr + sizeof( unsigned long int ) ); +} //end of the function GetMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef MEMDEBUG +void *GetClearedMemoryDebug( unsigned long size, char *label, char *file, int line ) +#else +void *GetClearedMemory( unsigned long size ) +#endif //MEMDEBUG +{ + void *ptr; +#ifdef MEMDEBUG + ptr = GetMemoryDebug( size, label, file, line ); +#else +ptr = GetMemory( size ); +#endif //MEMDEBUG +memset( ptr, 0, size ); +return ptr; +} //end of the function GetClearedMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef MEMDEBUG +void *GetHunkMemoryDebug( unsigned long size, char *label, char *file, int line ) +#else +void *GetHunkMemory( unsigned long size ) +#endif //MEMDEBUG +{ + void *ptr; + unsigned long int *memid; + + ptr = malloc( size + sizeof( unsigned long int ) ); + if ( !ptr ) { + return NULL; + } + memid = (unsigned long int *) ptr; + *memid = HUNK_ID; + return (unsigned long int *) ( (char *) ptr + sizeof( unsigned long int ) ); +} //end of the function GetHunkMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +#ifdef MEMDEBUG +void *GetClearedHunkMemoryDebug( unsigned long size, char *label, char *file, int line ) +#else +void *GetClearedHunkMemory( unsigned long size ) +#endif //MEMDEBUG +{ + void *ptr; +#ifdef MEMDEBUG + ptr = GetHunkMemoryDebug( size, label, file, line ); +#else +ptr = GetHunkMemory( size ); +#endif //MEMDEBUG +memset( ptr, 0, size ); +return ptr; +} //end of the function GetClearedHunkMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void FreeMemory( void *ptr ) { + unsigned long int *memid; + + memid = (unsigned long int *) ( (char *) ptr - sizeof( unsigned long int ) ); + + if ( *memid == MEM_ID ) { + free( memid ); + } //end if +} //end of the function FreeMemory +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void PrintUsedMemorySize( void ) { +} //end of the function PrintUsedMemorySize +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void PrintMemoryLabels( void ) { +} //end of the function PrintMemoryLabels + +#endif diff --git a/src/extractfuncs/l_memory.h b/src/extractfuncs/l_memory.h new file mode 100644 index 0000000..c33d715 --- /dev/null +++ b/src/extractfuncs/l_memory.h @@ -0,0 +1,80 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: l_memory.h + * + * desc: memory management + * + * + *****************************************************************************/ + +//#define MEMDEBUG + +#ifdef MEMDEBUG +#define GetMemory( size ) GetMemoryDebug( size, # size, __FILE__, __LINE__ ); +#define GetClearedMemory( size ) GetClearedMemoryDebug( size, # size, __FILE__, __LINE__ ); +//allocate a memory block of the given size +void *GetMemoryDebug( unsigned long size, char *label, char *file, int line ); +//allocate a memory block of the given size and clear it +void *GetClearedMemoryDebug( unsigned long size, char *label, char *file, int line ); +// +#define GetHunkMemory( size ) GetHunkMemoryDebug( size, # size, __FILE__, __LINE__ ); +#define GetClearedHunkMemory( size ) GetClearedHunkMemoryDebug( size, # size, __FILE__, __LINE__ ); +//allocate a memory block of the given size +void *GetHunkMemoryDebug( unsigned long size, char *label, char *file, int line ); +//allocate a memory block of the given size and clear it +void *GetClearedHunkMemoryDebug( unsigned long size, char *label, char *file, int line ); +#else +//allocate a memory block of the given size +void *GetMemory( unsigned long size ); +//allocate a memory block of the given size and clear it +void *GetClearedMemory( unsigned long size ); +// +#ifdef BSPC +#define GetHunkMemory GetMemory +#define GetClearedHunkMemory GetClearedMemory +#else +//allocate a memory block of the given size +void *GetHunkMemory( unsigned long size ); +//allocate a memory block of the given size and clear it +void *GetClearedHunkMemory( unsigned long size ); +#endif +#endif + +//free the given memory block +void FreeMemory( void *ptr ); +//prints the total used memory size +void PrintUsedMemorySize( void ); +//print all memory blocks with label +void PrintMemoryLabels( void ); +//returns the size of the memory block in bytes +int MemoryByteSize( void *ptr ); +//free all allocated memory +void DumpMemory( void ); diff --git a/src/extractfuncs/l_precomp.c b/src/extractfuncs/l_precomp.c new file mode 100644 index 0000000..23be10c --- /dev/null +++ b/src/extractfuncs/l_precomp.c @@ -0,0 +1,3167 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: l_precomp.c + * + * desc: pre compiler + * + * + *****************************************************************************/ + +//Notes: fix: PC_StringizeTokens + +#ifdef SCREWUP +#include +#include +#include +#include +#include +#include +#include +#include "l_memory.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_log.h" + +typedef enum {qfalse, qtrue} qboolean; + +// Ridah, ripped from q_shared.c +/* +============= +Q_strncpyz + +Safe strncpy that ensures a trailing zero +============= +*/ +extern void Error( char *error, ... ); +void Q_strncpyz( char *dest, const char *src, int destsize ) { + if ( !src ) { + Error( "Q_strncpyz: NULL src" ); + } + if ( destsize < 1 ) { + Error( "Q_strncpyz: destsize < 1" ); + } + + strncpy( dest, src, destsize - 1 ); + dest[destsize - 1] = 0; +} + +int Q_stricmpn( const char *s1, const char *s2, int n ) { + int c1, c2; + + do { + c1 = *s1++; + c2 = *s2++; + + if ( !n-- ) { + return 0; // strings are equal until end point + } + + if ( c1 != c2 ) { + if ( c1 >= 'a' && c1 <= 'z' ) { + c1 -= ( 'a' - 'A' ); + } + if ( c2 >= 'a' && c2 <= 'z' ) { + c2 -= ( 'a' - 'A' ); + } + if ( c1 != c2 ) { + return c1 < c2 ? -1 : 1; + } + } + } while ( c1 ); + + return 0; // strings are equal +} + +int Q_strncmp( const char *s1, const char *s2, int n ) { + int c1, c2; + + do { + c1 = *s1++; + c2 = *s2++; + + if ( !n-- ) { + return 0; // strings are equal until end point + } + + if ( c1 != c2 ) { + return c1 < c2 ? -1 : 1; + } + } while ( c1 ); + + return 0; // strings are equal +} + +int Q_stricmp( const char *s1, const char *s2 ) { + return Q_stricmpn( s1, s2, 99999 ); +} + + +char *Q_strlwr( char *s1 ) { + char *s; + + s = s1; + while ( *s ) { + *s = tolower( *s ); + s++; + } + return s1; +} + +char *Q_strupr( char *s1 ) { + char *s; + + s = s1; + while ( *s ) { + *s = toupper( *s ); + s++; + } + return s1; +} + +#endif //SCREWUP + +#ifdef BOTLIB +#include "../game/q_shared.h" +#include "botlib.h" +#include "be_interface.h" +#include "l_memory.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_log.h" +#endif //BOTLIB + +#ifdef MEQCC +#include "qcc.h" +#include "time.h" //time & ctime +#include "math.h" //fabs +#include "l_memory.h" +#include "l_script.h" +#include "l_precomp.h" +#include "l_log.h" + +#define qtrue true +#define qfalse false +#endif //MEQCC + +#ifdef BSPC +//include files for usage in the BSP Converter +#include "../bspc/qbsp.h" +#include "../bspc/l_log.h" +#include "../bspc/l_mem.h" +#include "l_precomp.h" + +#define qtrue true +#define qfalse false +#define Q_stricmp stricmp +#endif //BSPC + +#if defined( QUAKE ) && !defined( BSPC ) +#include "l_utils.h" +#endif //QUAKE + +//#define DEBUG_EVAL + +#define MAX_DEFINEPARMS 128 + +#define DEFINEHASHING 1 + +//directive name with parse function +typedef struct directive_s +{ + char *name; + int ( *func )( source_t *source ); +} directive_t; + +#define DEFINEHASHSIZE 1024 + +#define TOKEN_HEAP_SIZE 4096 + +int numtokens; +/* +int tokenheapinitialized; //true when the token heap is initialized +token_t token_heap[TOKEN_HEAP_SIZE]; //heap with tokens +token_t *freetokens; //free tokens from the heap +*/ + +//list with global defines added to every source loaded +define_t *globaldefines; + +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void QDECL SourceError( source_t *source, char *str, ... ) { + char text[1024]; + va_list ap; + + va_start( ap, str ); + vsprintf( text, str, ap ); + va_end( ap ); +#ifdef BOTLIB + botimport.Print( PRT_ERROR, "file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text ); +#endif //BOTLIB +#ifdef MEQCC + printf( "error: file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text ); +#endif //MEQCC +#ifdef BSPC + Log_Print( "error: file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text ); +#endif //BSPC +} //end of the function SourceError +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void QDECL SourceWarning( source_t *source, char *str, ... ) { + char text[1024]; + va_list ap; + + va_start( ap, str ); + vsprintf( text, str, ap ); + va_end( ap ); +#ifdef BOTLIB + botimport.Print( PRT_WARNING, "file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text ); +#endif //BOTLIB +#ifdef MEQCC + printf( "warning: file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text ); +#endif //MEQCC +#ifdef BSPC + Log_Print( "warning: file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text ); +#endif //BSPC +} //end of the function ScriptWarning +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_PushIndent( source_t *source, int type, int skip ) { + indent_t *indent; + + indent = (indent_t *) GetMemory( sizeof( indent_t ) ); + indent->type = type; + indent->script = source->scriptstack; + indent->skip = ( skip != 0 ); + source->skip += indent->skip; + indent->next = source->indentstack; + source->indentstack = indent; +} //end of the function PC_PushIndent +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_PopIndent( source_t *source, int *type, int *skip ) { + indent_t *indent; + + *type = 0; + *skip = 0; + + indent = source->indentstack; + if ( !indent ) { + return; + } + + //must be an indent from the current script + if ( source->indentstack->script != source->scriptstack ) { + return; + } + + *type = indent->type; + *skip = indent->skip; + source->indentstack = source->indentstack->next; + source->skip -= indent->skip; + FreeMemory( indent ); +} //end of the function PC_PopIndent +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_PushScript( source_t *source, script_t *script ) { + script_t *s; + + for ( s = source->scriptstack; s; s = s->next ) + { + if ( !Q_stricmp( s->filename, script->filename ) ) { + SourceError( source, "%s recursively included", script->filename ); + return; + } //end if + } //end for + //push the script on the script stack + script->next = source->scriptstack; + source->scriptstack = script; +} //end of the function PC_PushScript +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_InitTokenHeap( void ) { + /* + int i; + + if (tokenheapinitialized) return; + freetokens = NULL; + for (i = 0; i < TOKEN_HEAP_SIZE; i++) + { + token_heap[i].next = freetokens; + freetokens = &token_heap[i]; + } //end for + tokenheapinitialized = qtrue; + */ +} //end of the function PC_InitTokenHeap +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +token_t *PC_CopyToken( token_t *token ) { + token_t *t; + +// t = (token_t *) malloc(sizeof(token_t)); + t = (token_t *) GetMemory( sizeof( token_t ) ); +// t = freetokens; + if ( !t ) { +#ifdef BSPC + Error( "out of token space\n" ); +#else +#ifdef SCREWUP + Error( "out of token space\n" ); +#else + Com_Error( ERR_FATAL, "out of token space\n" ); +#endif +#endif + return NULL; + } //end if +// freetokens = freetokens->next; + memcpy( t, token, sizeof( token_t ) ); + t->next = NULL; + numtokens++; + return t; +} //end of the function PC_CopyToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_FreeToken( token_t *token ) { + //free(token); + FreeMemory( token ); +// token->next = freetokens; +// freetokens = token; + numtokens--; +} //end of the function PC_FreeToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_ReadSourceToken( source_t *source, token_t *token ) { + token_t *t; + script_t *script; + int type, skip; + + //if there's no token already available + while ( !source->tokens ) + { + //if there's a token to read from the script + if ( PS_ReadToken( source->scriptstack, token ) ) { + return qtrue; + } + //if at the end of the script + if ( EndOfScript( source->scriptstack ) ) { + //remove all indents of the script + while ( source->indentstack && + source->indentstack->script == source->scriptstack ) + { + SourceWarning( source, "missing #endif" ); + PC_PopIndent( source, &type, &skip ); + } //end if + } //end if + //if this was the initial script + if ( !source->scriptstack->next ) { + return qfalse; + } + //remove the script and return to the last one + script = source->scriptstack; + source->scriptstack = source->scriptstack->next; + FreeScript( script ); + } //end while + //copy the already available token + memcpy( token, source->tokens, sizeof( token_t ) ); + //free the read token + t = source->tokens; + source->tokens = source->tokens->next; + PC_FreeToken( t ); + return qtrue; +} //end of the function PC_ReadSourceToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_UnreadSourceToken( source_t *source, token_t *token ) { + token_t *t; + + t = PC_CopyToken( token ); + t->next = source->tokens; + source->tokens = t; + return qtrue; +} //end of the function PC_UnreadSourceToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_ReadDefineParms( source_t *source, define_t *define, token_t **parms, int maxparms ) { + token_t token, *t, *last; + int i, done, lastcomma, numparms, indent; + + if ( !PC_ReadSourceToken( source, &token ) ) { + SourceError( source, "define %s missing parms", define->name ); + return qfalse; + } //end if + // + if ( define->numparms > maxparms ) { + SourceError( source, "define with more than %d parameters", maxparms ); + return qfalse; + } //end if + // + for ( i = 0; i < define->numparms; i++ ) parms[i] = NULL; + //if no leading "(" + if ( strcmp( token.string, "(" ) ) { + PC_UnreadSourceToken( source, &token ); + SourceError( source, "define %s missing parms", define->name ); + return qfalse; + } //end if + //read the define parameters + for ( done = 0, numparms = 0, indent = 0; !done; ) + { + if ( numparms >= maxparms ) { + SourceError( source, "define %s with too many parms", define->name ); + return qfalse; + } //end if + if ( numparms >= define->numparms ) { + SourceWarning( source, "define %s has too many parms", define->name ); + return qfalse; + } //end if + parms[numparms] = NULL; + lastcomma = 1; + last = NULL; + while ( !done ) + { + // + if ( !PC_ReadSourceToken( source, &token ) ) { + SourceError( source, "define %s incomplete", define->name ); + return qfalse; + } //end if + // + if ( !strcmp( token.string, "," ) ) { + if ( indent <= 0 ) { + if ( lastcomma ) { + SourceWarning( source, "too many comma's" ); + } + lastcomma = 1; + break; + } //end if + } //end if + lastcomma = 0; + // + if ( !strcmp( token.string, "(" ) ) { + indent++; + continue; + } //end if + else if ( !strcmp( token.string, ")" ) ) { + if ( --indent <= 0 ) { + if ( !parms[define->numparms - 1] ) { + SourceWarning( source, "too few define parms" ); + } //end if + done = 1; + break; + } //end if + } //end if + // + if ( numparms < define->numparms ) { + // + t = PC_CopyToken( &token ); + t->next = NULL; + if ( last ) { + last->next = t; + } else { parms[numparms] = t;} + last = t; + } //end if + } //end while + numparms++; + } //end for + return qtrue; +} //end of the function PC_ReadDefineParms +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_StringizeTokens( token_t *tokens, token_t *token ) { + token_t *t; + + token->type = TT_STRING; + token->whitespace_p = NULL; + token->endwhitespace_p = NULL; + token->string[0] = '\0'; + strcat( token->string, "\"" ); + for ( t = tokens; t; t = t->next ) + { + strncat( token->string, t->string, MAX_TOKEN - strlen( token->string ) ); + } //end for + strncat( token->string, "\"", MAX_TOKEN - strlen( token->string ) ); + return qtrue; +} //end of the function PC_StringizeTokens +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_MergeTokens( token_t *t1, token_t *t2 ) { + //merging of a name with a name or number + if ( t1->type == TT_NAME && ( t2->type == TT_NAME || t2->type == TT_NUMBER ) ) { + strcat( t1->string, t2->string ); + return qtrue; + } //end if + //merging of two strings + if ( t1->type == TT_STRING && t2->type == TT_STRING ) { + //remove trailing double quote + t1->string[strlen( t1->string ) - 1] = '\0'; + //concat without leading double quote + strcat( t1->string, &t2->string[1] ); + return qtrue; + } //end if + //FIXME: merging of two number of the same sub type + return qfalse; +} //end of the function PC_MergeTokens +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +/* +void PC_PrintDefine(define_t *define) +{ + printf("define->name = %s\n", define->name); + printf("define->flags = %d\n", define->flags); + printf("define->builtin = %d\n", define->builtin); + printf("define->numparms = %d\n", define->numparms); +// token_t *parms; //define parameters +// token_t *tokens; //macro tokens (possibly containing parm tokens) +// struct define_s *next; //next defined macro in a list +} //end of the function PC_PrintDefine*/ +#if DEFINEHASHING +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_PrintDefineHashTable( define_t **definehash ) { + int i; + define_t *d; + + for ( i = 0; i < DEFINEHASHSIZE; i++ ) + { + Log_Write( "%4d:", i ); + for ( d = definehash[i]; d; d = d->hashnext ) + { + Log_Write( " %s", d->name ); + } //end for + Log_Write( "\n" ); + } //end for +} //end of the function PC_PrintDefineHashTable +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +//char primes[16] = {1, 3, 5, 7, 11, 13, 17, 19, 23, 27, 29, 31, 37, 41, 43, 47}; + +int PC_NameHash( char *name ) { + int register hash, i; + + hash = 0; + for ( i = 0; name[i] != '\0'; i++ ) + { + hash += name[i] * ( 119 + i ); + //hash += (name[i] << 7) + i; + //hash += (name[i] << (i&15)); + } //end while + hash = ( hash ^ ( hash >> 10 ) ^ ( hash >> 20 ) ) & ( DEFINEHASHSIZE - 1 ); + return hash; +} //end of the function PC_NameHash +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_AddDefineToHash( define_t *define, define_t **definehash ) { + int hash; + + hash = PC_NameHash( define->name ); + define->hashnext = definehash[hash]; + definehash[hash] = define; +} //end of the function PC_AddDefineToHash +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +define_t *PC_FindHashedDefine( define_t **definehash, char *name ) { + define_t *d; + int hash; + + hash = PC_NameHash( name ); + for ( d = definehash[hash]; d; d = d->hashnext ) + { + if ( !strcmp( d->name, name ) ) { + return d; + } + } //end for + return NULL; +} //end of the function PC_FindHashedDefine +#endif //DEFINEHASHING +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +define_t *PC_FindDefine( define_t *defines, char *name ) { + define_t *d; + + for ( d = defines; d; d = d->next ) + { + if ( !strcmp( d->name, name ) ) { + return d; + } + } //end for + return NULL; +} //end of the function PC_FindDefine +//============================================================================ +// +// Parameter: - +// Returns: number of the parm +// if no parm found with the given name -1 is returned +// Changes Globals: - +//============================================================================ +int PC_FindDefineParm( define_t *define, char *name ) { + token_t *p; + int i; + + i = 0; + for ( p = define->parms; p; p = p->next ) + { + if ( !strcmp( p->string, name ) ) { + return i; + } + i++; + } //end for + return -1; +} //end of the function PC_FindDefineParm +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_FreeDefine( define_t *define ) { + token_t *t, *next; + + //free the define parameters + for ( t = define->parms; t; t = next ) + { + next = t->next; + PC_FreeToken( t ); + } //end for + //free the define tokens + for ( t = define->tokens; t; t = next ) + { + next = t->next; + PC_FreeToken( t ); + } //end for + //free the define + FreeMemory( define ); +} //end of the function PC_FreeDefine +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_AddBuiltinDefines( source_t *source ) { + int i; + define_t *define; + struct builtin + { + char *string; + int builtin; + } builtin[] = { + "__LINE__", BUILTIN_LINE, + "__FILE__", BUILTIN_FILE, + "__DATE__", BUILTIN_DATE, + "__TIME__", BUILTIN_TIME, +// "__STDC__", BUILTIN_STDC, + NULL, 0 + }; + + for ( i = 0; builtin[i].string; i++ ) + { + define = (define_t *) GetMemory( sizeof( define_t ) + strlen( builtin[i].string ) + 1 ); + memset( define, 0, sizeof( define_t ) ); + define->name = (char *) define + sizeof( define_t ); + strcpy( define->name, builtin[i].string ); + define->flags |= DEFINE_FIXED; + define->builtin = builtin[i].builtin; + //add the define to the source +#if DEFINEHASHING + PC_AddDefineToHash( define, source->definehash ); +#else + define->next = source->defines; + source->defines = define; +#endif //DEFINEHASHING + } //end for +} //end of the function PC_AddBuiltinDefines +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_ExpandBuiltinDefine( source_t *source, define_t *define, + token_t **firsttoken, token_t **lasttoken ) { + token_t token; + unsigned long t; // time_t t; //to prevent LCC warning + char *curtime; + + memcpy( &token, &source->token, sizeof( token_t ) ); + switch ( define->builtin ) + { + case BUILTIN_LINE: + { + sprintf( token.string, "%d", source->token.line ); +#ifdef NUMBERVALUE + token.intvalue = source->token.line; + token.floatvalue = source->token.line; +#endif //NUMBERVALUE + token.type = TT_NUMBER; + token.subtype = TT_DECIMAL | TT_INTEGER; + *firsttoken = &token; + *lasttoken = &token; + break; + } //end case + case BUILTIN_FILE: + { + strcpy( token.string, source->scriptstack->filename ); + token.type = TT_NAME; + token.subtype = strlen( token.string ); + *firsttoken = &token; + *lasttoken = &token; + break; + } //end case + case BUILTIN_DATE: + { + t = time( NULL ); + curtime = ctime( &t ); + strcpy( token.string, "\"" ); + strncat( token.string, curtime + 4, 7 ); + strncat( token.string + 7, curtime + 20, 4 ); + strcat( token.string, "\"" ); + free( curtime ); + token.type = TT_NAME; + token.subtype = strlen( token.string ); + *firsttoken = &token; + *lasttoken = &token; + break; + } //end case + case BUILTIN_TIME: + { + t = time( NULL ); + curtime = ctime( &t ); + strcpy( token.string, "\"" ); + strncat( token.string, curtime + 11, 8 ); + strcat( token.string, "\"" ); + free( curtime ); + token.type = TT_NAME; + token.subtype = strlen( token.string ); + *firsttoken = &token; + *lasttoken = &token; + break; + } //end case + case BUILTIN_STDC: + default: + { + *firsttoken = NULL; + *lasttoken = NULL; + break; + } //end case + } //end switch + return qtrue; +} //end of the function PC_ExpandBuiltinDefine +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_ExpandDefine( source_t *source, define_t *define, + token_t **firsttoken, token_t **lasttoken ) { + token_t *parms[MAX_DEFINEPARMS], *dt, *pt, *t; + token_t *t1, *t2, *first, *last, *nextpt, token; + int parmnum, i; + + //if it is a builtin define + if ( define->builtin ) { + return PC_ExpandBuiltinDefine( source, define, firsttoken, lasttoken ); + } //end if + //if the define has parameters + if ( define->numparms ) { + if ( !PC_ReadDefineParms( source, define, parms, MAX_DEFINEPARMS ) ) { + return qfalse; + } +#ifdef DEBUG_EVAL + for ( i = 0; i < define->numparms; i++ ) + { + Log_Write( "define parms %d:", i ); + for ( pt = parms[i]; pt; pt = pt->next ) + { + Log_Write( "%s", pt->string ); + } //end for + } //end for +#endif //DEBUG_EVAL + } //end if + //empty list at first + first = NULL; + last = NULL; + //create a list with tokens of the expanded define + for ( dt = define->tokens; dt; dt = dt->next ) + { + parmnum = -1; + //if the token is a name, it could be a define parameter + if ( dt->type == TT_NAME ) { + parmnum = PC_FindDefineParm( define, dt->string ); + } //end if + //if it is a define parameter + if ( parmnum >= 0 ) { + for ( pt = parms[parmnum]; pt; pt = pt->next ) + { + t = PC_CopyToken( pt ); + //add the token to the list + t->next = NULL; + if ( last ) { + last->next = t; + } else { first = t;} + last = t; + } //end for + } //end if + else + { + //if stringizing operator + if ( dt->string[0] == '#' && dt->string[1] == '\0' ) { + //the stringizing operator must be followed by a define parameter + if ( dt->next ) { + parmnum = PC_FindDefineParm( define, dt->next->string ); + } else { parmnum = -1;} + // + if ( parmnum >= 0 ) { + //step over the stringizing operator + dt = dt->next; + //stringize the define parameter tokens + if ( !PC_StringizeTokens( parms[parmnum], &token ) ) { + SourceError( source, "can't stringize tokens" ); + return qfalse; + } //end if + t = PC_CopyToken( &token ); + } //end if + else + { + SourceWarning( source, "stringizing operator without define parameter" ); + continue; + } //end if + } //end if + else + { + t = PC_CopyToken( dt ); + } //end else + //add the token to the list + t->next = NULL; + if ( last ) { + last->next = t; + } else { first = t;} + last = t; + } //end else + } //end for + //check for the merging operator + for ( t = first; t; ) + { + if ( t->next ) { + //if the merging operator + if ( t->next->string[0] == '#' && t->next->string[1] == '#' ) { + t1 = t; + t2 = t->next->next; + if ( t2 ) { + if ( !PC_MergeTokens( t1, t2 ) ) { + SourceError( source, "can't merge %s with %s", t1->string, t2->string ); + return qfalse; + } //end if + PC_FreeToken( t1->next ); + t1->next = t2->next; + if ( t2 == last ) { + last = t1; + } + PC_FreeToken( t2 ); + continue; + } //end if + } //end if + } //end if + t = t->next; + } //end for + //store the first and last token of the list + *firsttoken = first; + *lasttoken = last; + //free all the parameter tokens + for ( i = 0; i < define->numparms; i++ ) + { + for ( pt = parms[i]; pt; pt = nextpt ) + { + nextpt = pt->next; + PC_FreeToken( pt ); + } //end for + } //end for + // + return qtrue; +} //end of the function PC_ExpandDefine +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_ExpandDefineIntoSource( source_t *source, define_t *define ) { + token_t *firsttoken, *lasttoken; + + if ( !PC_ExpandDefine( source, define, &firsttoken, &lasttoken ) ) { + return qfalse; + } + + if ( firsttoken && lasttoken ) { + lasttoken->next = source->tokens; + source->tokens = firsttoken; + return qtrue; + } //end if + return qfalse; +} //end of the function PC_ExpandDefineIntoSource +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_ConvertPath( char *path ) { + char *ptr; + + //remove double path seperators + for ( ptr = path; *ptr; ) + { + if ( ( *ptr == '\\' || *ptr == '/' ) && + ( *( ptr + 1 ) == '\\' || *( ptr + 1 ) == '/' ) ) { + strcpy( ptr, ptr + 1 ); + } //end if + else + { + ptr++; + } //end else + } //end while + //set OS dependent path seperators + for ( ptr = path; *ptr; ) + { + if ( *ptr == '/' || *ptr == '\\' ) { + *ptr = PATHSEPERATOR_CHAR; + } + ptr++; + } //end while +} //end of the function PC_ConvertPath +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_include( source_t *source ) { + script_t *script; + token_t token; + char path[_MAX_PATH]; +#ifdef QUAKE + foundfile_t file; +#endif //QUAKE + + if ( source->skip > 0 ) { + return qtrue; + } + // + if ( !PC_ReadSourceToken( source, &token ) ) { + SourceError( source, "#include without file name" ); + return qfalse; + } //end if + if ( token.linescrossed > 0 ) { + SourceError( source, "#include without file name" ); + return qfalse; + } //end if + if ( token.type == TT_STRING ) { + StripDoubleQuotes( token.string ); + PC_ConvertPath( token.string ); + script = LoadScriptFile( token.string ); + if ( !script ) { + strcpy( path, source->includepath ); + strcat( path, token.string ); + script = LoadScriptFile( path ); + } //end if + } //end if + else if ( token.type == TT_PUNCTUATION && *token.string == '<' ) { + strcpy( path, source->includepath ); + while ( PC_ReadSourceToken( source, &token ) ) + { + if ( token.linescrossed > 0 ) { + PC_UnreadSourceToken( source, &token ); + break; + } //end if + if ( token.type == TT_PUNCTUATION && *token.string == '>' ) { + break; + } + strncat( path, token.string, _MAX_PATH ); + } //end while + if ( *token.string != '>' ) { + SourceWarning( source, "#include missing trailing >" ); + } //end if + if ( !strlen( path ) ) { + SourceError( source, "#include without file name between < >" ); + return qfalse; + } //end if + PC_ConvertPath( path ); + script = LoadScriptFile( path ); + } //end if + else + { + SourceError( source, "#include without file name" ); + return qfalse; + } //end else +#ifdef QUAKE + if ( !script ) { + memset( &file, 0, sizeof( foundfile_t ) ); + script = LoadScriptFile( path ); + if ( script ) { + strncpy( script->filename, path, _MAX_PATH ); + } + } //end if +#endif //QUAKE + if ( !script ) { +#ifdef SCREWUP + SourceWarning( source, "file %s not found", path ); + return qtrue; +#else + SourceError( source, "file %s not found", path ); + return qfalse; +#endif //SCREWUP + } //end if + PC_PushScript( source, script ); + return qtrue; +} //end of the function PC_Directive_include +//============================================================================ +// reads a token from the current line, continues reading on the next +// line only if a backslash '\' is encountered. +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_ReadLine( source_t *source, token_t *token ) { + int crossline; + + crossline = 0; + do + { + if ( !PC_ReadSourceToken( source, token ) ) { + return qfalse; + } + + if ( token->linescrossed > crossline ) { + PC_UnreadSourceToken( source, token ); + return qfalse; + } //end if + crossline = 1; + } while ( !strcmp( token->string, "\\" ) ); + return qtrue; +} //end of the function PC_ReadLine +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_WhiteSpaceBeforeToken( token_t *token ) { + return token->endwhitespace_p - token->whitespace_p > 0; +} //end of the function PC_WhiteSpaceBeforeToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_ClearTokenWhiteSpace( token_t *token ) { + token->whitespace_p = NULL; + token->endwhitespace_p = NULL; + token->linescrossed = 0; +} //end of the function PC_ClearTokenWhiteSpace +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_undef( source_t *source ) { + token_t token; + define_t *define, *lastdefine; + int hash; + + if ( source->skip > 0 ) { + return qtrue; + } + // + if ( !PC_ReadLine( source, &token ) ) { + SourceError( source, "undef without name" ); + return qfalse; + } //end if + if ( token.type != TT_NAME ) { + PC_UnreadSourceToken( source, &token ); + SourceError( source, "expected name, found %s", token.string ); + return qfalse; + } //end if +#if DEFINEHASHING + + hash = PC_NameHash( token.string ); + for ( lastdefine = NULL, define = source->definehash[hash]; define; define = define->hashnext ) + { + if ( !strcmp( define->name, token.string ) ) { + if ( define->flags & DEFINE_FIXED ) { + SourceWarning( source, "can't undef %s", token.string ); + } //end if + else + { + if ( lastdefine ) { + lastdefine->hashnext = define->hashnext; + } else { source->definehash[hash] = define->hashnext;} + PC_FreeDefine( define ); + } //end else + break; + } //end if + lastdefine = define; + } //end for +#else //DEFINEHASHING + for ( lastdefine = NULL, define = source->defines; define; define = define->next ) + { + if ( !strcmp( define->name, token.string ) ) { + if ( define->flags & DEFINE_FIXED ) { + SourceWarning( source, "can't undef %s", token.string ); + } //end if + else + { + if ( lastdefine ) { + lastdefine->next = define->next; + } else { source->defines = define->next;} + PC_FreeDefine( define ); + } //end else + break; + } //end if + lastdefine = define; + } //end for +#endif //DEFINEHASHING + return qtrue; +} //end of the function PC_Directive_undef +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_define( source_t *source ) { + token_t token, *t, *last; + define_t *define; + + if ( source->skip > 0 ) { + return qtrue; + } + // + if ( !PC_ReadLine( source, &token ) ) { + SourceError( source, "#define without name" ); + return qfalse; + } //end if + if ( token.type != TT_NAME ) { + PC_UnreadSourceToken( source, &token ); + SourceError( source, "expected name after #define, found %s", token.string ); + return qfalse; + } //end if + //check if the define already exists +#if DEFINEHASHING + define = PC_FindHashedDefine( source->definehash, token.string ); +#else + define = PC_FindDefine( source->defines, token.string ); +#endif //DEFINEHASHING + if ( define ) { + if ( define->flags & DEFINE_FIXED ) { + SourceError( source, "can't redefine %s", token.string ); + return qfalse; + } //end if + SourceWarning( source, "redefinition of %s", token.string ); + //unread the define name before executing the #undef directive + PC_UnreadSourceToken( source, &token ); + if ( !PC_Directive_undef( source ) ) { + return qfalse; + } + //if the define was not removed (define->flags & DEFINE_FIXED) +#if DEFINEHASHING + define = PC_FindHashedDefine( source->definehash, token.string ); +#else + define = PC_FindDefine( source->defines, token.string ); +#endif //DEFINEHASHING + } //end if + //allocate define + define = (define_t *) GetMemory( sizeof( define_t ) + strlen( token.string ) + 1 ); + memset( define, 0, sizeof( define_t ) ); + define->name = (char *) define + sizeof( define_t ); + strcpy( define->name, token.string ); + //add the define to the source +#if DEFINEHASHING + PC_AddDefineToHash( define, source->definehash ); +#else //DEFINEHASHING + define->next = source->defines; + source->defines = define; +#endif //DEFINEHASHING + //if nothing is defined, just return + if ( !PC_ReadLine( source, &token ) ) { + return qtrue; + } + //if it is a define with parameters + if ( !PC_WhiteSpaceBeforeToken( &token ) && !strcmp( token.string, "(" ) ) { + //read the define parameters + last = NULL; + if ( !PC_CheckTokenString( source, ")" ) ) { + while ( 1 ) + { + if ( !PC_ReadLine( source, &token ) ) { + SourceError( source, "expected define parameter" ); + return qfalse; + } //end if + //if it isn't a name + if ( token.type != TT_NAME ) { + SourceError( source, "invalid define parameter" ); + return qfalse; + } //end if + // + if ( PC_FindDefineParm( define, token.string ) >= 0 ) { + SourceError( source, "two the same define parameters" ); + return qfalse; + } //end if + //add the define parm + t = PC_CopyToken( &token ); + PC_ClearTokenWhiteSpace( t ); + t->next = NULL; + if ( last ) { + last->next = t; + } else { define->parms = t;} + last = t; + define->numparms++; + //read next token + if ( !PC_ReadLine( source, &token ) ) { + SourceError( source, "define parameters not terminated" ); + return qfalse; + } //end if + // + if ( !strcmp( token.string, ")" ) ) { + break; + } + //then it must be a comma + if ( strcmp( token.string, "," ) ) { + SourceError( source, "define not terminated" ); + return qfalse; + } //end if + } //end while + } //end if + if ( !PC_ReadLine( source, &token ) ) { + return qtrue; + } + } //end if + //read the defined stuff + last = NULL; + do + { + t = PC_CopyToken( &token ); + if ( t->type == TT_NAME && !strcmp( t->string, define->name ) ) { + SourceError( source, "recursive define (removed recursion)" ); + continue; + } //end if + PC_ClearTokenWhiteSpace( t ); + t->next = NULL; + if ( last ) { + last->next = t; + } else { define->tokens = t;} + last = t; + } while ( PC_ReadLine( source, &token ) ); + // + if ( last ) { + //check for merge operators at the beginning or end + if ( !strcmp( define->tokens->string, "##" ) || + !strcmp( last->string, "##" ) ) { + SourceError( source, "define with misplaced ##" ); + return qfalse; + } //end if + } //end if + return qtrue; +} //end of the function PC_Directive_define +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +define_t *PC_DefineFromString( char *string ) { + script_t *script; + source_t src; + token_t *t; + int res, i; + define_t *def; + + PC_InitTokenHeap(); + + script = LoadScriptMemory( string, strlen( string ), "*extern" ); + //create a new source + memset( &src, 0, sizeof( source_t ) ); + strncpy( src.filename, "*extern", _MAX_PATH ); + src.scriptstack = script; +#if DEFINEHASHING + src.definehash = GetClearedMemory( DEFINEHASHSIZE * sizeof( define_t * ) ); +#endif //DEFINEHASHING + //create a define from the source + res = PC_Directive_define( &src ); + //free any tokens if left + for ( t = src.tokens; t; t = src.tokens ) + { + src.tokens = src.tokens->next; + PC_FreeToken( t ); + } //end for +#ifdef DEFINEHASHING + def = NULL; + for ( i = 0; i < DEFINEHASHSIZE; i++ ) + { + if ( src.definehash[i] ) { + def = src.definehash[i]; + break; + } //end if + } //end for +#else + def = src.defines; +#endif //DEFINEHASHING + // +#if DEFINEHASHING + FreeMemory( src.definehash ); +#endif //DEFINEHASHING + // + FreeScript( script ); + //if the define was created succesfully + if ( res > 0 ) { + return def; + } + //free the define if created + if ( src.defines ) { + PC_FreeDefine( def ); + } + // + return NULL; +} //end of the function PC_DefineFromString +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_AddDefine( source_t *source, char *string ) { + define_t *define; + + define = PC_DefineFromString( string ); + if ( !define ) { + return qfalse; + } +#if DEFINEHASHING + PC_AddDefineToHash( define, source->definehash ); +#else //DEFINEHASHING + define->next = source->defines; + source->defines = define; +#endif //DEFINEHASHING + return qtrue; +} //end of the function PC_AddDefine +//============================================================================ +// add a globals define that will be added to all opened sources +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_AddGlobalDefine( char *string ) { + define_t *define; + + define = PC_DefineFromString( string ); + if ( !define ) { + return qfalse; + } + define->next = globaldefines; + globaldefines = define; + return qtrue; +} //end of the function PC_AddGlobalDefine +//============================================================================ +// remove the given global define +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_RemoveGlobalDefine( char *name ) { + define_t *define; + + define = PC_FindDefine( globaldefines, name ); + if ( define ) { + PC_FreeDefine( define ); + return qtrue; + } //end if + return qfalse; +} //end of the function PC_RemoveGlobalDefine +//============================================================================ +// remove all globals defines +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_RemoveAllGlobalDefines( void ) { + define_t *define; + + for ( define = globaldefines; define; define = globaldefines ) + { + globaldefines = globaldefines->next; + PC_FreeDefine( define ); + } //end for +} //end of the function PC_RemoveAllGlobalDefines +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +define_t *PC_CopyDefine( source_t *source, define_t *define ) { + define_t *newdefine; + token_t *token, *newtoken, *lasttoken; + + newdefine = (define_t *) GetMemory( sizeof( define_t ) + strlen( define->name ) + 1 ); + //copy the define name + newdefine->name = (char *) newdefine + sizeof( define_t ); + strcpy( newdefine->name, define->name ); + newdefine->flags = define->flags; + newdefine->builtin = define->builtin; + newdefine->numparms = define->numparms; + //the define is not linked + newdefine->next = NULL; + newdefine->hashnext = NULL; + //copy the define tokens + newdefine->tokens = NULL; + for ( lasttoken = NULL, token = define->tokens; token; token = token->next ) + { + newtoken = PC_CopyToken( token ); + newtoken->next = NULL; + if ( lasttoken ) { + lasttoken->next = newtoken; + } else { newdefine->tokens = newtoken;} + lasttoken = newtoken; + } //end for + //copy the define parameters + newdefine->parms = NULL; + for ( lasttoken = NULL, token = define->parms; token; token = token->next ) + { + newtoken = PC_CopyToken( token ); + newtoken->next = NULL; + if ( lasttoken ) { + lasttoken->next = newtoken; + } else { newdefine->parms = newtoken;} + lasttoken = newtoken; + } //end for + return newdefine; +} //end of the function PC_CopyDefine +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_AddGlobalDefinesToSource( source_t *source ) { + define_t *define, *newdefine; + + for ( define = globaldefines; define; define = define->next ) + { + newdefine = PC_CopyDefine( source, define ); +#if DEFINEHASHING + PC_AddDefineToHash( newdefine, source->definehash ); +#else //DEFINEHASHING + newdefine->next = source->defines; + source->defines = newdefine; +#endif //DEFINEHASHING + } //end for +} //end of the function PC_AddGlobalDefinesToSource +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_if_def( source_t *source, int type ) { + token_t token; + define_t *d; + int skip; + + if ( !PC_ReadLine( source, &token ) ) { + SourceError( source, "#ifdef without name" ); + return qfalse; + } //end if + if ( token.type != TT_NAME ) { + PC_UnreadSourceToken( source, &token ); + SourceError( source, "expected name after #ifdef, found %s", token.string ); + return qfalse; + } //end if +#if DEFINEHASHING + d = PC_FindHashedDefine( source->definehash, token.string ); +#else + d = PC_FindDefine( source->defines, token.string ); +#endif //DEFINEHASHING + skip = ( type == INDENT_IFDEF ) == ( d == NULL ); + PC_PushIndent( source, type, skip ); + return qtrue; +} //end of the function PC_Directiveif_def +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_ifdef( source_t *source ) { + return PC_Directive_if_def( source, INDENT_IFDEF ); +} //end of the function PC_Directive_ifdef +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_ifndef( source_t *source ) { + return PC_Directive_if_def( source, INDENT_IFNDEF ); +} //end of the function PC_Directive_ifndef +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_else( source_t *source ) { + int type, skip; + + PC_PopIndent( source, &type, &skip ); + if ( !type ) { + SourceError( source, "misplaced #else" ); + return qfalse; + } //end if + if ( type == INDENT_ELSE ) { + SourceError( source, "#else after #else" ); + return qfalse; + } //end if + PC_PushIndent( source, INDENT_ELSE, !skip ); + return qtrue; +} //end of the function PC_Directive_else +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_endif( source_t *source ) { + int type, skip; + + PC_PopIndent( source, &type, &skip ); + if ( !type ) { + SourceError( source, "misplaced #endif" ); + return qfalse; + } //end if + return qtrue; +} //end of the function PC_Directive_endif +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +typedef struct operator_s +{ + int operator; + int priority; + int parentheses; + struct operator_s *prev, *next; +} operator_t; + +typedef struct value_s +{ + signed long int intvalue; + double floatvalue; + int parentheses; + struct value_s *prev, *next; +} value_t; + +int PC_OperatorPriority( int op ) { + switch ( op ) + { + case P_MUL: return 15; + case P_DIV: return 15; + case P_MOD: return 15; + case P_ADD: return 14; + case P_SUB: return 14; + + case P_LOGIC_AND: return 7; + case P_LOGIC_OR: return 6; + case P_LOGIC_GEQ: return 12; + case P_LOGIC_LEQ: return 12; + case P_LOGIC_EQ: return 11; + case P_LOGIC_UNEQ: return 11; + + case P_LOGIC_NOT: return 16; + case P_LOGIC_GREATER: return 12; + case P_LOGIC_LESS: return 12; + + case P_RSHIFT: return 13; + case P_LSHIFT: return 13; + + case P_BIN_AND: return 10; + case P_BIN_OR: return 8; + case P_BIN_XOR: return 9; + case P_BIN_NOT: return 16; + + case P_COLON: return 5; + case P_QUESTIONMARK: return 5; + } //end switch + return qfalse; +} //end of the function PC_OperatorPriority + +//#define AllocValue() GetClearedMemory(sizeof(value_t)); +//#define FreeValue(val) FreeMemory(val) +//#define AllocOperator(op) op = (operator_t *) GetClearedMemory(sizeof(operator_t)); +//#define FreeOperator(op) FreeMemory(op); + +#define MAX_VALUES 64 +#define MAX_OPERATORS 64 +#define AllocValue( val ) \ + if ( numvalues >= MAX_VALUES ) { \ + SourceError( source, "out value space\n" ); \ + error = 1; \ + break; \ + } \ + else { \ + val = &value_heap[numvalues++];} +#define FreeValue( val ) +// +#define AllocOperator( op ) \ + if ( numoperators >= MAX_OPERATORS ) { \ + SourceError( source, "out operator space\n" ); \ + error = 1; \ + break; \ + } \ + else { \ + op = &operator_heap[numoperators++];} +#define FreeOperator( op ) + +int PC_EvaluateTokens( source_t *source, token_t *tokens, signed long int *intvalue, + double *floatvalue, int integer ) { + operator_t *o, *firstoperator, *lastoperator; + value_t *v, *firstvalue, *lastvalue, *v1, *v2; + token_t *t; + int brace = 0; + int parentheses = 0; + int error = 0; + int lastwasvalue = 0; + int negativevalue = 0; + int questmarkintvalue = 0; + double questmarkfloatvalue = 0; + int gotquestmarkvalue = qfalse; + int lastoperatortype = 0; + // + operator_t operator_heap[MAX_OPERATORS]; + int numoperators = 0; + value_t value_heap[MAX_VALUES]; + int numvalues = 0; + + firstoperator = lastoperator = NULL; + firstvalue = lastvalue = NULL; + if ( intvalue ) { + *intvalue = 0; + } + if ( floatvalue ) { + *floatvalue = 0; + } + for ( t = tokens; t; t = t->next ) + { + switch ( t->type ) + { + case TT_NAME: + { + if ( lastwasvalue || negativevalue ) { + SourceError( source, "syntax error in #if/#elif" ); + error = 1; + break; + } //end if + if ( strcmp( t->string, "defined" ) ) { + SourceError( source, "undefined name %s in #if/#elif", t->string ); + error = 1; + break; + } //end if + t = t->next; + if ( !strcmp( t->string, "(" ) ) { + brace = qtrue; + t = t->next; + } //end if + if ( !t || t->type != TT_NAME ) { + SourceError( source, "defined without name in #if/#elif" ); + error = 1; + break; + } //end if + //v = (value_t *) GetClearedMemory(sizeof(value_t)); + AllocValue( v ); +#if DEFINEHASHING + if ( PC_FindHashedDefine( source->definehash, t->string ) ) +#else + if ( PC_FindDefine( source->defines, t->string ) ) +#endif //DEFINEHASHING + { + v->intvalue = 1; + v->floatvalue = 1; + } //end if + else + { + v->intvalue = 0; + v->floatvalue = 0; + } //end else + v->parentheses = parentheses; + v->next = NULL; + v->prev = lastvalue; + if ( lastvalue ) { + lastvalue->next = v; + } else { firstvalue = v;} + lastvalue = v; + if ( brace ) { + t = t->next; + if ( !t || strcmp( t->string, ")" ) ) { + SourceError( source, "defined without ) in #if/#elif" ); + error = 1; + break; + } //end if + } //end if + brace = qfalse; + // defined() creates a value + lastwasvalue = 1; + break; + } //end case + case TT_NUMBER: + { + if ( lastwasvalue ) { + SourceError( source, "syntax error in #if/#elif" ); + error = 1; + break; + } //end if + //v = (value_t *) GetClearedMemory(sizeof(value_t)); + AllocValue( v ); + if ( negativevalue ) { + v->intvalue = -(signed int) t->intvalue; + v->floatvalue = -t->floatvalue; + } //end if + else + { + v->intvalue = t->intvalue; + v->floatvalue = t->floatvalue; + } //end else + v->parentheses = parentheses; + v->next = NULL; + v->prev = lastvalue; + if ( lastvalue ) { + lastvalue->next = v; + } else { firstvalue = v;} + lastvalue = v; + //last token was a value + lastwasvalue = 1; + // + negativevalue = 0; + break; + } //end case + case TT_PUNCTUATION: + { + if ( negativevalue ) { + SourceError( source, "misplaced minus sign in #if/#elif" ); + error = 1; + break; + } //end if + if ( t->subtype == P_PARENTHESESOPEN ) { + parentheses++; + break; + } //end if + else if ( t->subtype == P_PARENTHESESCLOSE ) { + parentheses--; + if ( parentheses < 0 ) { + SourceError( source, "too many ) in #if/#elsif" ); + error = 1; + } //end if + break; + } //end else if + //check for invalid operators on floating point values + if ( !integer ) { + if ( t->subtype == P_BIN_NOT || t->subtype == P_MOD || + t->subtype == P_RSHIFT || t->subtype == P_LSHIFT || + t->subtype == P_BIN_AND || t->subtype == P_BIN_OR || + t->subtype == P_BIN_XOR ) { + SourceError( source, "illigal operator %s on floating point operands\n", t->string ); + error = 1; + break; + } //end if + } //end if + switch ( t->subtype ) + { + case P_LOGIC_NOT: + case P_BIN_NOT: + { + if ( lastwasvalue ) { + SourceError( source, "! or ~ after value in #if/#elif" ); + error = 1; + break; + } //end if + break; + } //end case + case P_SUB: + { + if ( !lastwasvalue ) { + negativevalue = 1; + break; + } //end if + } //end case + + case P_MUL: + case P_DIV: + case P_MOD: + case P_ADD: + + case P_LOGIC_AND: + case P_LOGIC_OR: + case P_LOGIC_GEQ: + case P_LOGIC_LEQ: + case P_LOGIC_EQ: + case P_LOGIC_UNEQ: + + case P_LOGIC_GREATER: + case P_LOGIC_LESS: + + case P_RSHIFT: + case P_LSHIFT: + + case P_BIN_AND: + case P_BIN_OR: + case P_BIN_XOR: + + case P_COLON: + case P_QUESTIONMARK: + { + if ( !lastwasvalue ) { + SourceError( source, "operator %s after operator in #if/#elif", t->string ); + error = 1; + break; + } //end if + break; + } //end case + default: + { + SourceError( source, "invalid operator %s in #if/#elif", t->string ); + error = 1; + break; + } //end default + } //end switch + if ( !error && !negativevalue ) { + //o = (operator_t *) GetClearedMemory(sizeof(operator_t)); + AllocOperator( o ); + o->operator = t->subtype; + o->priority = PC_OperatorPriority( t->subtype ); + o->parentheses = parentheses; + o->next = NULL; + o->prev = lastoperator; + if ( lastoperator ) { + lastoperator->next = o; + } else { firstoperator = o;} + lastoperator = o; + lastwasvalue = 0; + } //end if + break; + } //end case + default: + { + SourceError( source, "unknown %s in #if/#elif", t->string ); + error = 1; + break; + } //end default + } //end switch + if ( error ) { + break; + } + } //end for + if ( !error ) { + if ( !lastwasvalue ) { + SourceError( source, "trailing operator in #if/#elif" ); + error = 1; + } //end if + else if ( parentheses ) { + SourceError( source, "too many ( in #if/#elif" ); + error = 1; + } //end else if + } //end if + // + gotquestmarkvalue = qfalse; + questmarkintvalue = 0; + questmarkfloatvalue = 0; + //while there are operators + while ( !error && firstoperator ) + { + v = firstvalue; + for ( o = firstoperator; o->next; o = o->next ) + { + //if the current operator is nested deeper in parentheses + //than the next operator + if ( o->parentheses > o->next->parentheses ) { + break; + } + //if the current and next operator are nested equally deep in parentheses + if ( o->parentheses == o->next->parentheses ) { + //if the priority of the current operator is equal or higher + //than the priority of the next operator + if ( o->priority >= o->next->priority ) { + break; + } + } //end if + //if the arity of the operator isn't equal to 1 + if ( o->operator != P_LOGIC_NOT + && o->operator != P_BIN_NOT ) { + v = v->next; + } + //if there's no value or no next value + if ( !v ) { + SourceError( source, "mising values in #if/#elif" ); + error = 1; + break; + } //end if + } //end for + if ( error ) { + break; + } + v1 = v; + v2 = v->next; +#ifdef DEBUG_EVAL + if ( integer ) { + Log_Write( "operator %s, value1 = %d", PunctuationFromNum( source->scriptstack, o->operator ), v1->intvalue ); + if ( v2 ) { + Log_Write( "value2 = %d", v2->intvalue ); + } + } //end if + else + { + Log_Write( "operator %s, value1 = %f", PunctuationFromNum( source->scriptstack, o->operator ), v1->floatvalue ); + if ( v2 ) { + Log_Write( "value2 = %f", v2->floatvalue ); + } + } //end else +#endif //DEBUG_EVAL + switch ( o->operator ) + { + case P_LOGIC_NOT: v1->intvalue = !v1->intvalue; + v1->floatvalue = !v1->floatvalue; break; + case P_BIN_NOT: v1->intvalue = ~v1->intvalue; + break; + case P_MUL: v1->intvalue *= v2->intvalue; + v1->floatvalue *= v2->floatvalue; break; + case P_DIV: v1->intvalue /= v2->intvalue; + v1->floatvalue /= v2->floatvalue; break; + case P_MOD: v1->intvalue %= v2->intvalue; + break; + case P_ADD: v1->intvalue += v2->intvalue; + v1->floatvalue += v2->floatvalue; break; + case P_SUB: v1->intvalue -= v2->intvalue; + v1->floatvalue -= v2->floatvalue; break; + case P_LOGIC_AND: v1->intvalue = v1->intvalue && v2->intvalue; + v1->floatvalue = v1->floatvalue && v2->floatvalue; break; + case P_LOGIC_OR: v1->intvalue = v1->intvalue || v2->intvalue; + v1->floatvalue = v1->floatvalue || v2->floatvalue; break; + case P_LOGIC_GEQ: v1->intvalue = v1->intvalue >= v2->intvalue; + v1->floatvalue = v1->floatvalue >= v2->floatvalue; break; + case P_LOGIC_LEQ: v1->intvalue = v1->intvalue <= v2->intvalue; + v1->floatvalue = v1->floatvalue <= v2->floatvalue; break; + case P_LOGIC_EQ: v1->intvalue = v1->intvalue == v2->intvalue; + v1->floatvalue = v1->floatvalue == v2->floatvalue; break; + case P_LOGIC_UNEQ: v1->intvalue = v1->intvalue != v2->intvalue; + v1->floatvalue = v1->floatvalue != v2->floatvalue; break; + case P_LOGIC_GREATER: v1->intvalue = v1->intvalue > v2->intvalue; + v1->floatvalue = v1->floatvalue > v2->floatvalue; break; + case P_LOGIC_LESS: v1->intvalue = v1->intvalue < v2->intvalue; + v1->floatvalue = v1->floatvalue < v2->floatvalue; break; + case P_RSHIFT: v1->intvalue >>= v2->intvalue; + break; + case P_LSHIFT: v1->intvalue <<= v2->intvalue; + break; + case P_BIN_AND: v1->intvalue &= v2->intvalue; + break; + case P_BIN_OR: v1->intvalue |= v2->intvalue; + break; + case P_BIN_XOR: v1->intvalue ^= v2->intvalue; + break; + case P_COLON: + { + if ( !gotquestmarkvalue ) { + SourceError( source, ": without ? in #if/#elif" ); + error = 1; + break; + } //end if + if ( integer ) { + if ( !questmarkintvalue ) { + v1->intvalue = v2->intvalue; + } + } //end if + else + { + if ( !questmarkfloatvalue ) { + v1->floatvalue = v2->floatvalue; + } + } //end else + gotquestmarkvalue = qfalse; + break; + } //end case + case P_QUESTIONMARK: + { + if ( gotquestmarkvalue ) { + SourceError( source, "? after ? in #if/#elif" ); + error = 1; + break; + } //end if + questmarkintvalue = v1->intvalue; + questmarkfloatvalue = v1->floatvalue; + gotquestmarkvalue = qtrue; + break; + } //end if + } //end switch +#ifdef DEBUG_EVAL + if ( integer ) { + Log_Write( "result value = %d", v1->intvalue ); + } else { Log_Write( "result value = %f", v1->floatvalue );} +#endif //DEBUG_EVAL + if ( error ) { + break; + } + lastoperatortype = o->operator; + //if not an operator with arity 1 + if ( o->operator != P_LOGIC_NOT + && o->operator != P_BIN_NOT ) { + //remove the second value if not question mark operator + if ( o->operator != P_QUESTIONMARK ) { + v = v->next; + } + // + if ( v->prev ) { + v->prev->next = v->next; + } else { firstvalue = v->next;} + if ( v->next ) { + v->next->prev = v->prev; + } else { lastvalue = v->prev;} + //FreeMemory(v); + FreeValue( v ); + } //end if + //remove the operator + if ( o->prev ) { + o->prev->next = o->next; + } else { firstoperator = o->next;} + if ( o->next ) { + o->next->prev = o->prev; + } else { lastoperator = o->prev;} + //FreeMemory(o); + FreeOperator( o ); + } //end while + if ( firstvalue ) { + if ( intvalue ) { + *intvalue = firstvalue->intvalue; + } + if ( floatvalue ) { + *floatvalue = firstvalue->floatvalue; + } + } //end if + for ( o = firstoperator; o; o = lastoperator ) + { + lastoperator = o->next; + //FreeMemory(o); + FreeOperator( o ); + } //end for + for ( v = firstvalue; v; v = lastvalue ) + { + lastvalue = v->next; + //FreeMemory(v); + FreeValue( v ); + } //end for + if ( !error ) { + return qtrue; + } + if ( intvalue ) { + *intvalue = 0; + } + if ( floatvalue ) { + *floatvalue = 0; + } + return qfalse; +} //end of the function PC_EvaluateTokens +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Evaluate( source_t *source, signed long int *intvalue, + double *floatvalue, int integer ) { + token_t token, *firsttoken, *lasttoken; + token_t *t, *nexttoken; + define_t *define; + int defined = qfalse; + + if ( intvalue ) { + *intvalue = 0; + } + if ( floatvalue ) { + *floatvalue = 0; + } + // + if ( !PC_ReadLine( source, &token ) ) { + SourceError( source, "no value after #if/#elif" ); + return qfalse; + } //end if + firsttoken = NULL; + lasttoken = NULL; + do + { + //if the token is a name + if ( token.type == TT_NAME ) { + if ( defined ) { + defined = qfalse; + t = PC_CopyToken( &token ); + t->next = NULL; + if ( lasttoken ) { + lasttoken->next = t; + } else { firsttoken = t;} + lasttoken = t; + } //end if + else if ( !strcmp( token.string, "defined" ) ) { + defined = qtrue; + t = PC_CopyToken( &token ); + t->next = NULL; + if ( lasttoken ) { + lasttoken->next = t; + } else { firsttoken = t;} + lasttoken = t; + } //end if + else + { + //then it must be a define +#if DEFINEHASHING + define = PC_FindHashedDefine( source->definehash, token.string ); +#else + define = PC_FindDefine( source->defines, token.string ); +#endif //DEFINEHASHING + if ( !define ) { + SourceError( source, "can't evaluate %s, not defined", token.string ); + return qfalse; + } //end if + if ( !PC_ExpandDefineIntoSource( source, define ) ) { + return qfalse; + } + } //end else + } //end if + //if the token is a number or a punctuation + else if ( token.type == TT_NUMBER || token.type == TT_PUNCTUATION ) { + t = PC_CopyToken( &token ); + t->next = NULL; + if ( lasttoken ) { + lasttoken->next = t; + } else { firsttoken = t;} + lasttoken = t; + } //end else + else //can't evaluate the token + { + SourceError( source, "can't evaluate %s", token.string ); + return qfalse; + } //end else + } while ( PC_ReadLine( source, &token ) ); + // + if ( !PC_EvaluateTokens( source, firsttoken, intvalue, floatvalue, integer ) ) { + return qfalse; + } + // +#ifdef DEBUG_EVAL + Log_Write( "eval:" ); +#endif //DEBUG_EVAL + for ( t = firsttoken; t; t = nexttoken ) + { +#ifdef DEBUG_EVAL + Log_Write( " %s", t->string ); +#endif //DEBUG_EVAL + nexttoken = t->next; + PC_FreeToken( t ); + } //end for +#ifdef DEBUG_EVAL + if ( integer ) { + Log_Write( "eval result: %d", *intvalue ); + } else { Log_Write( "eval result: %f", *floatvalue );} +#endif //DEBUG_EVAL + // + return qtrue; +} //end of the function PC_Evaluate +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_DollarEvaluate( source_t *source, signed long int *intvalue, + double *floatvalue, int integer ) { + int indent, defined = qfalse; + token_t token, *firsttoken, *lasttoken; + token_t *t, *nexttoken; + define_t *define; + + if ( intvalue ) { + *intvalue = 0; + } + if ( floatvalue ) { + *floatvalue = 0; + } + // + if ( !PC_ReadSourceToken( source, &token ) ) { + SourceError( source, "no leading ( after $evalint/$evalfloat" ); + return qfalse; + } //end if + if ( !PC_ReadSourceToken( source, &token ) ) { + SourceError( source, "nothing to evaluate" ); + return qfalse; + } //end if + indent = 1; + firsttoken = NULL; + lasttoken = NULL; + do + { + //if the token is a name + if ( token.type == TT_NAME ) { + if ( defined ) { + defined = qfalse; + t = PC_CopyToken( &token ); + t->next = NULL; + if ( lasttoken ) { + lasttoken->next = t; + } else { firsttoken = t;} + lasttoken = t; + } //end if + else if ( !strcmp( token.string, "defined" ) ) { + defined = qtrue; + t = PC_CopyToken( &token ); + t->next = NULL; + if ( lasttoken ) { + lasttoken->next = t; + } else { firsttoken = t;} + lasttoken = t; + } //end if + else + { + //then it must be a define +#if DEFINEHASHING + define = PC_FindHashedDefine( source->definehash, token.string ); +#else + define = PC_FindDefine( source->defines, token.string ); +#endif //DEFINEHASHING + if ( !define ) { + SourceError( source, "can't evaluate %s, not defined", token.string ); + return qfalse; + } //end if + if ( !PC_ExpandDefineIntoSource( source, define ) ) { + return qfalse; + } + } //end else + } //end if + //if the token is a number or a punctuation + else if ( token.type == TT_NUMBER || token.type == TT_PUNCTUATION ) { + if ( *token.string == '(' ) { + indent++; + } else if ( *token.string == ')' ) { + indent--; + } + if ( indent <= 0 ) { + break; + } + t = PC_CopyToken( &token ); + t->next = NULL; + if ( lasttoken ) { + lasttoken->next = t; + } else { firsttoken = t;} + lasttoken = t; + } //end else + else //can't evaluate the token + { + SourceError( source, "can't evaluate %s", token.string ); + return qfalse; + } //end else + } while ( PC_ReadSourceToken( source, &token ) ); + // + if ( !PC_EvaluateTokens( source, firsttoken, intvalue, floatvalue, integer ) ) { + return qfalse; + } + // +#ifdef DEBUG_EVAL + Log_Write( "$eval:" ); +#endif //DEBUG_EVAL + for ( t = firsttoken; t; t = nexttoken ) + { +#ifdef DEBUG_EVAL + Log_Write( " %s", t->string ); +#endif //DEBUG_EVAL + nexttoken = t->next; + PC_FreeToken( t ); + } //end for +#ifdef DEBUG_EVAL + if ( integer ) { + Log_Write( "$eval result: %d", *intvalue ); + } else { Log_Write( "$eval result: %f", *floatvalue );} +#endif //DEBUG_EVAL + // + return qtrue; +} //end of the function PC_DollarEvaluate +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_elif( source_t *source ) { + signed long int value; + int type, skip; + + PC_PopIndent( source, &type, &skip ); + if ( !type || type == INDENT_ELSE ) { + SourceError( source, "misplaced #elif" ); + return qfalse; + } //end if + if ( !PC_Evaluate( source, &value, NULL, qtrue ) ) { + return qfalse; + } + skip = ( value == 0 ); + PC_PushIndent( source, INDENT_ELIF, skip ); + return qtrue; +} //end of the function PC_Directive_elif +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_if( source_t *source ) { + signed long int value; + int skip; + + if ( !PC_Evaluate( source, &value, NULL, qtrue ) ) { + return qfalse; + } + skip = ( value == 0 ); + PC_PushIndent( source, INDENT_IF, skip ); + return qtrue; +} //end of the function PC_Directive +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_line( source_t *source ) { + SourceError( source, "#line directive not supported" ); + return qfalse; +} //end of the function PC_Directive_line +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_error( source_t *source ) { + token_t token; + + strcpy( token.string, "" ); + PC_ReadSourceToken( source, &token ); + SourceError( source, "#error directive: %s", token.string ); + return qfalse; +} //end of the function PC_Directive_error +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_pragma( source_t *source ) { + token_t token; + + SourceWarning( source, "#pragma directive not supported" ); + while ( PC_ReadLine( source, &token ) ) ; + return qtrue; +} //end of the function PC_Directive_pragma +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void UnreadSignToken( source_t *source ) { + token_t token; + + token.line = source->scriptstack->line; + token.whitespace_p = source->scriptstack->script_p; + token.endwhitespace_p = source->scriptstack->script_p; + token.linescrossed = 0; + strcpy( token.string, "-" ); + token.type = TT_PUNCTUATION; + token.subtype = P_SUB; + PC_UnreadSourceToken( source, &token ); +} //end of the function UnreadSignToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_eval( source_t *source ) { + signed long int value; + token_t token; + + if ( !PC_Evaluate( source, &value, NULL, qtrue ) ) { + return qfalse; + } + // + token.line = source->scriptstack->line; + token.whitespace_p = source->scriptstack->script_p; + token.endwhitespace_p = source->scriptstack->script_p; + token.linescrossed = 0; + sprintf( token.string, "%d", abs( value ) ); + token.type = TT_NUMBER; + token.subtype = TT_INTEGER | TT_LONG | TT_DECIMAL; + PC_UnreadSourceToken( source, &token ); + if ( value < 0 ) { + UnreadSignToken( source ); + } + return qtrue; +} //end of the function PC_Directive_eval +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_Directive_evalfloat( source_t *source ) { + double value; + token_t token; + + if ( !PC_Evaluate( source, NULL, &value, qfalse ) ) { + return qfalse; + } + token.line = source->scriptstack->line; + token.whitespace_p = source->scriptstack->script_p; + token.endwhitespace_p = source->scriptstack->script_p; + token.linescrossed = 0; + sprintf( token.string, "%1.2f", fabs( value ) ); + token.type = TT_NUMBER; + token.subtype = TT_FLOAT | TT_LONG | TT_DECIMAL; + PC_UnreadSourceToken( source, &token ); + if ( value < 0 ) { + UnreadSignToken( source ); + } + return qtrue; +} //end of the function PC_Directive_evalfloat +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +directive_t directives[20] = +{ + {"if", PC_Directive_if}, + {"ifdef", PC_Directive_ifdef}, + {"ifndef", PC_Directive_ifndef}, + {"elif", PC_Directive_elif}, + {"else", PC_Directive_else}, + {"endif", PC_Directive_endif}, + {"include", PC_Directive_include}, + {"define", PC_Directive_define}, + {"undef", PC_Directive_undef}, + {"line", PC_Directive_line}, + {"error", PC_Directive_error}, + {"pragma", PC_Directive_pragma}, + {"eval", PC_Directive_eval}, + {"evalfloat", PC_Directive_evalfloat}, + {NULL, NULL} +}; + +int PC_ReadDirective( source_t *source ) { + token_t token; + int i; + + //read the directive name + if ( !PC_ReadSourceToken( source, &token ) ) { + SourceError( source, "found # without name" ); + return qfalse; + } //end if + //directive name must be on the same line + if ( token.linescrossed > 0 ) { + PC_UnreadSourceToken( source, &token ); + SourceError( source, "found # at end of line" ); + return qfalse; + } //end if + //if if is a name + if ( token.type == TT_NAME ) { + //find the precompiler directive + for ( i = 0; directives[i].name; i++ ) + { + if ( !strcmp( directives[i].name, token.string ) ) { + return directives[i].func( source ); + } //end if + } //end for + } //end if + SourceError( source, "unknown precompiler directive %s", token.string ); + return qfalse; +} //end of the function PC_ReadDirective +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_DollarDirective_evalint( source_t *source ) { + signed long int value; + token_t token; + + if ( !PC_DollarEvaluate( source, &value, NULL, qtrue ) ) { + return qfalse; + } + // + token.line = source->scriptstack->line; + token.whitespace_p = source->scriptstack->script_p; + token.endwhitespace_p = source->scriptstack->script_p; + token.linescrossed = 0; + sprintf( token.string, "%d", abs( value ) ); + token.type = TT_NUMBER; + token.subtype = TT_INTEGER | TT_LONG | TT_DECIMAL; +#ifdef NUMBERVALUE + token.intvalue = value; + token.floatvalue = value; +#endif //NUMBERVALUE + PC_UnreadSourceToken( source, &token ); + if ( value < 0 ) { + UnreadSignToken( source ); + } + return qtrue; +} //end of the function PC_DollarDirective_evalint +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_DollarDirective_evalfloat( source_t *source ) { + double value; + token_t token; + + if ( !PC_DollarEvaluate( source, NULL, &value, qfalse ) ) { + return qfalse; + } + token.line = source->scriptstack->line; + token.whitespace_p = source->scriptstack->script_p; + token.endwhitespace_p = source->scriptstack->script_p; + token.linescrossed = 0; + sprintf( token.string, "%1.2f", fabs( value ) ); + token.type = TT_NUMBER; + token.subtype = TT_FLOAT | TT_LONG | TT_DECIMAL; +#ifdef NUMBERVALUE + token.intvalue = (unsigned long) value; + token.floatvalue = value; +#endif //NUMBERVALUE + PC_UnreadSourceToken( source, &token ); + if ( value < 0 ) { + UnreadSignToken( source ); + } + return qtrue; +} //end of the function PC_DollarDirective_evalfloat +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +directive_t dollardirectives[20] = +{ + {"evalint", PC_DollarDirective_evalint}, + {"evalfloat", PC_DollarDirective_evalfloat}, + {NULL, NULL} +}; + +int PC_ReadDollarDirective( source_t *source ) { + token_t token; + int i; + + //read the directive name + if ( !PC_ReadSourceToken( source, &token ) ) { + SourceError( source, "found $ without name" ); + return qfalse; + } //end if + //directive name must be on the same line + if ( token.linescrossed > 0 ) { + PC_UnreadSourceToken( source, &token ); + SourceError( source, "found $ at end of line" ); + return qfalse; + } //end if + //if if is a name + if ( token.type == TT_NAME ) { + //find the precompiler directive + for ( i = 0; dollardirectives[i].name; i++ ) + { + if ( !strcmp( dollardirectives[i].name, token.string ) ) { + return dollardirectives[i].func( source ); + } //end if + } //end for + } //end if + PC_UnreadSourceToken( source, &token ); + SourceError( source, "unknown precompiler directive %s", token.string ); + return qfalse; +} //end of the function PC_ReadDirective + +#ifdef QUAKEC +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int BuiltinFunction( source_t *source ) { + token_t token; + + if ( !PC_ReadSourceToken( source, &token ) ) { + return qfalse; + } + if ( token.type == TT_NUMBER ) { + PC_UnreadSourceToken( source, &token ); + return qtrue; + } //end if + else + { + PC_UnreadSourceToken( source, &token ); + return qfalse; + } //end else +} //end of the function BuiltinFunction +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int QuakeCMacro( source_t *source ) { + int i; + token_t token; + + if ( !PC_ReadSourceToken( source, &token ) ) { + return qtrue; + } + if ( token.type != TT_NAME ) { + PC_UnreadSourceToken( source, &token ); + return qtrue; + } //end if + //find the precompiler directive + for ( i = 0; dollardirectives[i].name; i++ ) + { + if ( !strcmp( dollardirectives[i].name, token.string ) ) { + PC_UnreadSourceToken( source, &token ); + return qfalse; + } //end if + } //end for + PC_UnreadSourceToken( source, &token ); + return qtrue; +} //end of the function QuakeCMacro +#endif //QUAKEC +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_ReadToken( source_t *source, token_t *token ) { + define_t *define; + + while ( 1 ) + { + if ( !PC_ReadSourceToken( source, token ) ) { + return qfalse; + } + + //check for precompiler directives + if ( token->type == TT_PUNCTUATION && *token->string == '#' ) { +#ifdef SCREWUP // Ridah, skip all # directives + while ( PC_ReadLine( source, token ) ) ; + continue; +#endif // SCREWUP + +#ifdef QUAKEC + if ( !BuiltinFunction( source ) ) +#endif //QUAKC + { + //read the precompiler directive + if ( !PC_ReadDirective( source ) ) { + return qfalse; + } + continue; + } //end if + } //end if + if ( token->type == TT_PUNCTUATION && *token->string == '$' ) { +#ifdef QUAKEC + if ( !QuakeCMacro( source ) ) +#endif //QUAKEC + { + //read the precompiler directive + if ( !PC_ReadDollarDirective( source ) ) { + return qfalse; + } + continue; + } //end if + } //end if + //if skipping source because of conditional compilation + if ( source->skip ) { + continue; + } + //if the token is a name + if ( token->type == TT_NAME ) { + //check if the name is a define macro +#if DEFINEHASHING + define = PC_FindHashedDefine( source->definehash, token->string ); +#else + define = PC_FindDefine( source->defines, token->string ); +#endif //DEFINEHASHING + //if it is a define macro + if ( define ) { + //expand the defined macro + if ( !PC_ExpandDefineIntoSource( source, define ) ) { + return qfalse; + } + continue; + } //end if + } //end if + //copy token for unreading + memcpy( &source->token, token, sizeof( token_t ) ); + //found a token + return qtrue; + } //end while +} //end of the function PC_ReadToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_ExpectTokenString( source_t *source, char *string ) { + token_t token; + + if ( !PC_ReadToken( source, &token ) ) { + SourceError( source, "couldn't find expected %s", string ); + return qfalse; + } //end if + + if ( strcmp( token.string, string ) ) { + SourceError( source, "expected %s, found %s", string, token.string ); + return qfalse; + } //end if + return qtrue; +} //end of the function PC_ExpectTokenString +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_ExpectTokenType( source_t *source, int type, int subtype, token_t *token ) { + char str[MAX_TOKEN]; + + if ( !PC_ReadToken( source, token ) ) { + SourceError( source, "couldn't read expected token" ); + return qfalse; + } //end if + + if ( token->type != type ) { + strcpy( str, "" ); + if ( type == TT_STRING ) { + strcpy( str, "string" ); + } + if ( type == TT_LITERAL ) { + strcpy( str, "literal" ); + } + if ( type == TT_NUMBER ) { + strcpy( str, "number" ); + } + if ( type == TT_NAME ) { + strcpy( str, "name" ); + } + if ( type == TT_PUNCTUATION ) { + strcpy( str, "punctuation" ); + } + SourceError( source, "expected a %s, found %s", str, token->string ); + return qfalse; + } //end if + if ( token->type == TT_NUMBER ) { + if ( ( token->subtype & subtype ) != subtype ) { + if ( subtype & TT_DECIMAL ) { + strcpy( str, "decimal" ); + } + if ( subtype & TT_HEX ) { + strcpy( str, "hex" ); + } + if ( subtype & TT_OCTAL ) { + strcpy( str, "octal" ); + } + if ( subtype & TT_BINARY ) { + strcpy( str, "binary" ); + } + if ( subtype & TT_LONG ) { + strcat( str, " long" ); + } + if ( subtype & TT_UNSIGNED ) { + strcat( str, " unsigned" ); + } + if ( subtype & TT_FLOAT ) { + strcat( str, " float" ); + } + if ( subtype & TT_INTEGER ) { + strcat( str, " integer" ); + } + SourceError( source, "expected %s, found %s", str, token->string ); + return qfalse; + } //end if + } //end if + else if ( token->type == TT_PUNCTUATION ) { + if ( token->subtype != subtype ) { + SourceError( source, "found %s", token->string ); + return qfalse; + } //end if + } //end else if + return qtrue; +} //end of the function PC_ExpectTokenType +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_ExpectAnyToken( source_t *source, token_t *token ) { + if ( !PC_ReadToken( source, token ) ) { + SourceError( source, "couldn't read expected token" ); + return qfalse; + } //end if + else + { + return qtrue; + } //end else +} //end of the function PC_ExpectAnyToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_CheckTokenString( source_t *source, char *string ) { + token_t tok; + + if ( !PC_ReadToken( source, &tok ) ) { + return qfalse; + } + //if the token is available + if ( !strcmp( tok.string, string ) ) { + return qtrue; + } + // + PC_UnreadSourceToken( source, &tok ); + return qfalse; +} //end of the function PC_CheckTokenString +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_CheckTokenType( source_t *source, int type, int subtype, token_t *token ) { + token_t tok; + + if ( !PC_ReadToken( source, &tok ) ) { + return qfalse; + } + //if the type matches + if ( tok.type == type && + ( tok.subtype & subtype ) == subtype ) { + memcpy( token, &tok, sizeof( token_t ) ); + return qtrue; + } //end if + // + PC_UnreadSourceToken( source, &tok ); + return qfalse; +} //end of the function PC_CheckTokenType +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PC_SkipUntilString( source_t *source, char *string ) { + token_t token; + + while ( PC_ReadToken( source, &token ) ) + { + if ( !strcmp( token.string, string ) ) { + return qtrue; + } + } //end while + return qfalse; +} //end of the function PC_SkipUntilString +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_UnreadLastToken( source_t *source ) { + PC_UnreadSourceToken( source, &source->token ); +} //end of the function PC_UnreadLastToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_UnreadToken( source_t *source, token_t *token ) { + PC_UnreadSourceToken( source, token ); +} //end of the function PC_UnreadToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_SetIncludePath( source_t *source, char *path ) { + strncpy( source->includepath, path, _MAX_PATH ); + //add trailing path seperator + if ( source->includepath[strlen( source->includepath ) - 1] != '\\' && + source->includepath[strlen( source->includepath ) - 1] != '/' ) { + strcat( source->includepath, PATHSEPERATOR_STR ); + } //end if +} //end of the function PC_SetIncludePath +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PC_SetPunctuations( source_t *source, punctuation_t *p ) { + source->punctuations = p; +} //end of the function PC_SetPunctuations +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +source_t *LoadSourceFile( char *filename ) { + source_t *source; + script_t *script; + + PC_InitTokenHeap(); + + script = LoadScriptFile( filename ); + if ( !script ) { + return NULL; + } + + script->next = NULL; + + source = (source_t *) GetMemory( sizeof( source_t ) ); + memset( source, 0, sizeof( source_t ) ); + + strncpy( source->filename, filename, _MAX_PATH ); + source->scriptstack = script; + source->tokens = NULL; + source->defines = NULL; + source->indentstack = NULL; + source->skip = 0; + +#if DEFINEHASHING + source->definehash = GetClearedMemory( DEFINEHASHSIZE * sizeof( define_t * ) ); +#endif //DEFINEHASHING + PC_AddGlobalDefinesToSource( source ); + return source; +} //end of the function LoadSourceFile +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +source_t *LoadSourceMemory( char *ptr, int length, char *name ) { + source_t *source; + script_t *script; + + PC_InitTokenHeap(); + + script = LoadScriptMemory( ptr, length, name ); + if ( !script ) { + return NULL; + } + script->next = NULL; + + source = (source_t *) GetMemory( sizeof( source_t ) ); + memset( source, 0, sizeof( source_t ) ); + + strncpy( source->filename, name, _MAX_PATH ); + source->scriptstack = script; + source->tokens = NULL; + source->defines = NULL; + source->indentstack = NULL; + source->skip = 0; + +#if DEFINEHASHING + source->definehash = GetClearedMemory( DEFINEHASHSIZE * sizeof( define_t * ) ); +#endif //DEFINEHASHING + PC_AddGlobalDefinesToSource( source ); + return source; +} //end of the function LoadSourceMemory +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void FreeSource( source_t *source ) { + script_t *script; + token_t *token; + define_t *define; + indent_t *indent; + int i; + + //PC_PrintDefineHashTable(source->definehash); + //free all the scripts + while ( source->scriptstack ) + { + script = source->scriptstack; + source->scriptstack = source->scriptstack->next; + FreeScript( script ); + } //end for + //free all the tokens + while ( source->tokens ) + { + token = source->tokens; + source->tokens = source->tokens->next; + PC_FreeToken( token ); + } //end for +#if DEFINEHASHING + for ( i = 0; i < DEFINEHASHSIZE; i++ ) + { + while ( source->definehash[i] ) + { + define = source->definehash[i]; + source->definehash[i] = source->definehash[i]->hashnext; + PC_FreeDefine( define ); + } //end while + } //end for +#else //DEFINEHASHING + //free all defines + while ( source->defines ) + { + define = source->defines; + source->defines = source->defines->next; + PC_FreeDefine( define ); + } //end for +#endif //DEFINEHASHING + //free all indents + while ( source->indentstack ) + { + indent = source->indentstack; + source->indentstack = source->indentstack->next; + FreeMemory( indent ); + } //end for +#if DEFINEHASHING + // + if ( source->definehash ) { + FreeMemory( source->definehash ); + } +#endif //DEFINEHASHING + //free the source itself + FreeMemory( source ); +} //end of the function FreeSource + diff --git a/src/extractfuncs/l_precomp.h b/src/extractfuncs/l_precomp.h new file mode 100644 index 0000000..1d2b9ea --- /dev/null +++ b/src/extractfuncs/l_precomp.h @@ -0,0 +1,158 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: l_precomp.h + * + * desc: pre compiler + * + * + *****************************************************************************/ + +#ifndef _MAX_PATH + #define MAX_PATH MAX_QPATH +#endif + +#ifndef PATH_SEPERATORSTR + #if defined( WIN32 ) | defined( _WIN32 ) | defined( __NT__ ) | defined( __WINDOWS__ ) | defined( __WINDOWS_386__ ) + #define PATHSEPERATOR_STR "\\" + #else + #define PATHSEPERATOR_STR "/" + #endif +#endif +#ifndef PATH_SEPERATORCHAR + #if defined( WIN32 ) | defined( _WIN32 ) | defined( __NT__ ) | defined( __WINDOWS__ ) | defined( __WINDOWS_386__ ) + #define PATHSEPERATOR_CHAR '\\' + #else + #define PATHSEPERATOR_CHAR '/' + #endif +#endif + + +#define DEFINE_FIXED 0x0001 + +#define BUILTIN_LINE 1 +#define BUILTIN_FILE 2 +#define BUILTIN_DATE 3 +#define BUILTIN_TIME 4 +#define BUILTIN_STDC 5 + +#define INDENT_IF 0x0001 +#define INDENT_ELSE 0x0002 +#define INDENT_ELIF 0x0004 +#define INDENT_IFDEF 0x0008 +#define INDENT_IFNDEF 0x0010 + +//macro definitions +typedef struct define_s +{ + char *name; //define name + int flags; //define flags + int builtin; // > 0 if builtin define + int numparms; //number of define parameters + token_t *parms; //define parameters + token_t *tokens; //macro tokens (possibly containing parm tokens) + struct define_s *next; //next defined macro in a list + struct define_s *hashnext; //next define in the hash chain +} define_t; + +//indents +//used for conditional compilation directives: +//#if, #else, #elif, #ifdef, #ifndef +typedef struct indent_s +{ + int type; //indent type + int skip; //true if skipping current indent + script_t *script; //script the indent was in + struct indent_s *next; //next indent on the indent stack +} indent_t; + +//source file +typedef struct source_s +{ + char filename[_MAX_PATH]; //file name of the script + char includepath[_MAX_PATH]; //path to include files + punctuation_t *punctuations; //punctuations to use + script_t *scriptstack; //stack with scripts of the source + token_t *tokens; //tokens to read first + define_t *defines; //list with macro definitions + define_t **definehash; //hash chain with defines + indent_t *indentstack; //stack with indents + int skip; // > 0 if skipping conditional code + token_t token; //last read token +} source_t; + + +//read a token from the source +int PC_ReadToken( source_t *source, token_t *token ); +//expect a certain token +int PC_ExpectTokenString( source_t *source, char *string ); +//expect a certain token type +int PC_ExpectTokenType( source_t *source, int type, int subtype, token_t *token ); +//expect a token +int PC_ExpectAnyToken( source_t *source, token_t *token ); +//returns true when the token is available +int PC_CheckTokenString( source_t *source, char *string ); +//returns true an reads the token when a token with the given type is available +int PC_CheckTokenType( source_t *source, int type, int subtype, token_t *token ); +//skip tokens until the given token string is read +int PC_SkipUntilString( source_t *source, char *string ); +//unread the last token read from the script +void PC_UnreadLastToken( source_t *source ); +//unread the given token +void PC_UnreadToken( source_t *source, token_t *token ); +//read a token only if on the same line, lines are concatenated with a slash +int PC_ReadLine( source_t *source, token_t *token ); +//returns true if there was a white space in front of the token +int PC_WhiteSpaceBeforeToken( token_t *token ); +//add a define to the source +int PC_AddDefine( source_t *source, char *string ); +//add a globals define that will be added to all opened sources +int PC_AddGlobalDefine( char *string ); +//remove the given global define +int PC_RemoveGlobalDefine( char *name ); +//remove all globals defines +void PC_RemoveAllGlobalDefines( void ); +//add builtin defines +void PC_AddBuiltinDefines( source_t *source ); +//set the source include path +void PC_SetIncludePath( source_t *source, char *path ); +//set the punction set +void PC_SetPunctuations( source_t *source, punctuation_t *p ); +//load a source file +source_t *LoadSourceFile( char *filename ); +//load a source from memory +source_t *LoadSourceMemory( char *ptr, int length, char *name ); +//free the given source +void FreeSource( source_t *source ); +//print a source error +void QDECL SourceError( source_t *source, char *str, ... ); +//print a source warning +void QDECL SourceWarning( source_t *source, char *str, ... ); + diff --git a/src/extractfuncs/l_script.c b/src/extractfuncs/l_script.c new file mode 100644 index 0000000..e1b4308 --- /dev/null +++ b/src/extractfuncs/l_script.c @@ -0,0 +1,1419 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: l_script.c + * + * desc: lexicographical parser + * + * + *****************************************************************************/ + +//#define SCREWUP +//#define BOTLIB +//#define MEQCC +//#define BSPC + +#ifdef SCREWUP +#include +#include +#include +#include +#include +#include "l_memory.h" +#include "l_script.h" + +typedef enum {qfalse, qtrue} qboolean; + +#endif //SCREWUP + +#ifdef BOTLIB +//include files for usage in the bot library +#include "../game/q_shared.h" +#include "botlib.h" +#include "be_interface.h" +#include "l_script.h" +#include "l_memory.h" +#include "l_log.h" +#include "l_libvar.h" +#endif //BOTLIB + +#ifdef MEQCC +//include files for usage in MrElusive's QuakeC Compiler +#include "qcc.h" +#include "l_script.h" +#include "l_memory.h" +#include "l_log.h" + +#define qtrue true +#define qfalse false +#endif //MEQCC + +#ifdef BSPC +//include files for usage in the BSP Converter +#include "../bspc/qbsp.h" +#include "../bspc/l_log.h" +#include "../bspc/l_mem.h" + +#define qtrue true +#define qfalse false +#endif //BSPC + + +#define PUNCTABLE + +//longer punctuations first +punctuation_t default_punctuations[] = +{ + //binary operators + {">>=",P_RSHIFT_ASSIGN, NULL}, + {"<<=",P_LSHIFT_ASSIGN, NULL}, + // + {"...",P_PARMS, NULL}, + //define merge operator + {"##",P_PRECOMPMERGE, NULL}, + //logic operators + {"&&",P_LOGIC_AND, NULL}, + {"||",P_LOGIC_OR, NULL}, + {">=",P_LOGIC_GEQ, NULL}, + {"<=",P_LOGIC_LEQ, NULL}, + {"==",P_LOGIC_EQ, NULL}, + {"!=",P_LOGIC_UNEQ, NULL}, + //arithmatic operators + {"*=",P_MUL_ASSIGN, NULL}, + {"/=",P_DIV_ASSIGN, NULL}, + {"%=",P_MOD_ASSIGN, NULL}, + {"+=",P_ADD_ASSIGN, NULL}, + {"-=",P_SUB_ASSIGN, NULL}, + {"++",P_INC, NULL}, + {"--",P_DEC, NULL}, + //binary operators + {"&=",P_BIN_AND_ASSIGN, NULL}, + {"|=",P_BIN_OR_ASSIGN, NULL}, + {"^=",P_BIN_XOR_ASSIGN, NULL}, + {">>",P_RSHIFT, NULL}, + {"<<",P_LSHIFT, NULL}, + //reference operators + {"->",P_POINTERREF, NULL}, + //C++ + {"::",P_CPP1, NULL}, + {".*",P_CPP2, NULL}, + //arithmatic operators + {"*",P_MUL, NULL}, + {"/",P_DIV, NULL}, + {"%",P_MOD, NULL}, + {"+",P_ADD, NULL}, + {"-",P_SUB, NULL}, + {"=",P_ASSIGN, NULL}, + //binary operators + {"&",P_BIN_AND, NULL}, + {"|",P_BIN_OR, NULL}, + {"^",P_BIN_XOR, NULL}, + {"~",P_BIN_NOT, NULL}, + //logic operators + {"!",P_LOGIC_NOT, NULL}, + {">",P_LOGIC_GREATER, NULL}, + {"<",P_LOGIC_LESS, NULL}, + //reference operator + {".",P_REF, NULL}, + //seperators + {",",P_COMMA, NULL}, + {";",P_SEMICOLON, NULL}, + //label indication + {":",P_COLON, NULL}, + //if statement + {"?",P_QUESTIONMARK, NULL}, + //embracements + {"(",P_PARENTHESESOPEN, NULL}, + {")",P_PARENTHESESCLOSE, NULL}, + {"{",P_BRACEOPEN, NULL}, + {"}",P_BRACECLOSE, NULL}, + {"[",P_SQBRACKETOPEN, NULL}, + {"]",P_SQBRACKETCLOSE, NULL}, + // + {"\\",P_BACKSLASH, NULL}, + //precompiler operator + {"#",P_PRECOMP, NULL}, +#ifdef DOLLAR + {"$",P_DOLLAR, NULL}, +#endif //DOLLAR + {NULL, 0} +}; + +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void PS_CreatePunctuationTable( script_t *script, punctuation_t *punctuations ) { + int i; + punctuation_t *p, *lastp, *newp; + + //get memory for the table + if ( !script->punctuationtable ) { + script->punctuationtable = (punctuation_t **) + GetMemory( 256 * sizeof( punctuation_t * ) ); + } + memset( script->punctuationtable, 0, 256 * sizeof( punctuation_t * ) ); + //add the punctuations in the list to the punctuation table + for ( i = 0; punctuations[i].p; i++ ) + { + newp = &punctuations[i]; + lastp = NULL; + //sort the punctuations in this table entry on length (longer punctuations first) + for ( p = script->punctuationtable[(unsigned int) newp->p[0]]; p; p = p->next ) + { + if ( strlen( p->p ) < strlen( newp->p ) ) { + newp->next = p; + if ( lastp ) { + lastp->next = newp; + } else { script->punctuationtable[(unsigned int) newp->p[0]] = newp;} + break; + } //end if + lastp = p; + } //end for + if ( !p ) { + newp->next = NULL; + if ( lastp ) { + lastp->next = newp; + } else { script->punctuationtable[(unsigned int) newp->p[0]] = newp;} + } //end if + } //end for +} //end of the function PS_CreatePunctuationTable +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +char *PunctuationFromNum( script_t *script, int num ) { + int i; + + for ( i = 0; script->punctuations[i].p; i++ ) + { + if ( script->punctuations[i].n == num ) { + return script->punctuations[i].p; + } + } //end for + return "unkown punctuation"; +} //end of the function PunctuationFromNum +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void QDECL ScriptError( script_t *script, char *str, ... ) { + char text[1024]; + va_list ap; + + if ( script->flags & SCFL_NOERRORS ) { + return; + } + + va_start( ap, str ); + vsprintf( text, str, ap ); + va_end( ap ); +#ifdef BOTLIB + botimport.Print( PRT_ERROR, "file %s, line %d: %s\n", script->filename, script->line, text ); +#endif //BOTLIB +#ifdef MEQCC + printf( "error: file %s, line %d: %s\n", script->filename, script->line, text ); +#endif //MEQCC +#ifdef BSPC + Log_Print( "error: file %s, line %d: %s\n", script->filename, script->line, text ); +#endif //BSPC +} //end of the function ScriptError +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void QDECL ScriptWarning( script_t *script, char *str, ... ) { + char text[1024]; + va_list ap; + + if ( script->flags & SCFL_NOWARNINGS ) { + return; + } + + va_start( ap, str ); + vsprintf( text, str, ap ); + va_end( ap ); +#ifdef BOTLIB + botimport.Print( PRT_WARNING, "file %s, line %d: %s\n", script->filename, script->line, text ); +#endif //BOTLIB +#ifdef MEQCC + printf( "warning: file %s, line %d: %s\n", script->filename, script->line, text ); +#endif //MEQCC +#ifdef BSPC + Log_Print( "warning: file %s, line %d: %s\n", script->filename, script->line, text ); +#endif //BSPC +} //end of the function ScriptWarning +//=========================================================================== +// +// Parameter: - +// Returns: - +// Changes Globals: - +//=========================================================================== +void SetScriptPunctuations( script_t *script, punctuation_t *p ) { +#ifdef PUNCTABLE + if ( p ) { + PS_CreatePunctuationTable( script, p ); + } else { PS_CreatePunctuationTable( script, default_punctuations );} +#endif //PUNCTABLE + if ( p ) { + script->punctuations = p; + } else { script->punctuations = default_punctuations;} +} //end of the function SetScriptPunctuations +//============================================================================ +// Reads spaces, tabs, C-like comments etc. +// When a newline character is found the scripts line counter is increased. +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ReadWhiteSpace( script_t *script ) { + while ( 1 ) + { + //skip white space + while ( *script->script_p <= ' ' ) + { + if ( !*script->script_p ) { + return 0; + } + if ( *script->script_p == '\n' ) { + script->line++; + } + script->script_p++; + } //end while + //skip comments + if ( *script->script_p == '/' ) { + //comments // + if ( *( script->script_p + 1 ) == '/' ) { + script->script_p++; + do + { + script->script_p++; + if ( !*script->script_p ) { + return 0; + } + } //end do + while ( *script->script_p != '\n' ); + script->line++; + script->script_p++; + if ( !*script->script_p ) { + return 0; + } + continue; + } //end if + //comments /* */ + else if ( *( script->script_p + 1 ) == '*' ) { + script->script_p++; + do + { + script->script_p++; + if ( !*script->script_p ) { + return 0; + } + if ( *script->script_p == '\n' ) { + script->line++; + } + } //end do + while ( !( *script->script_p == '*' && *( script->script_p + 1 ) == '/' ) ); + script->script_p++; + if ( !*script->script_p ) { + return 0; + } + script->script_p++; + if ( !*script->script_p ) { + return 0; + } + continue; + } //end if + } //end if + break; + } //end while + return 1; +} //end of the function PS_ReadWhiteSpace +//============================================================================ +// Reads an escape character. +// +// Parameter: script : script to read from +// ch : place to store the read escape character +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ReadEscapeCharacter( script_t *script, char *ch ) { + int c, val, i; + + //step over the leading '\\' + script->script_p++; + //determine the escape character + switch ( *script->script_p ) + { + case '\\': c = '\\'; break; + case 'n': c = '\n'; break; + case 'r': c = '\r'; break; + case 't': c = '\t'; break; + case 'v': c = '\v'; break; + case 'b': c = '\b'; break; + case 'f': c = '\f'; break; + case 'a': c = '\a'; break; + case '\'': c = '\''; break; + case '\"': c = '\"'; break; + case '\?': c = '\?'; break; + case 'x': + { + script->script_p++; + for ( i = 0, val = 0; ; i++, script->script_p++ ) + { + c = *script->script_p; + if ( c >= '0' && c <= '9' ) { + c = c - '0'; + } else if ( c >= 'A' && c <= 'Z' ) { + c = c - 'A' + 10; + } else if ( c >= 'a' && c <= 'z' ) { + c = c - 'a' + 10; + } else { break;} + val = ( val << 4 ) + c; + } //end for + script->script_p--; + if ( val > 0xFF ) { + ScriptWarning( script, "too large value in escape character" ); + val = 0xFF; + } //end if + c = val; + break; + } //end case + default: //NOTE: decimal ASCII code, NOT octal + { + if ( *script->script_p < '0' || *script->script_p > '9' ) { + ScriptError( script, "unknown escape char" ); + } + for ( i = 0, val = 0; ; i++, script->script_p++ ) + { + c = *script->script_p; + if ( c >= '0' && c <= '9' ) { + c = c - '0'; + } else { break;} + val = val * 10 + c; + } //end for + script->script_p--; + if ( val > 0xFF ) { + ScriptWarning( script, "too large value in escape character" ); + val = 0xFF; + } //end if + c = val; + break; + } //end default + } //end switch + //step over the escape character or the last digit of the number + script->script_p++; + //store the escape character + *ch = c; + //succesfully read escape character + return 1; +} //end of the function PS_ReadEscapeCharacter +//============================================================================ +// Reads C-like string. Escape characters are interpretted. +// Quotes are included with the string. +// Reads two strings with a white space between them as one string. +// +// Parameter: script : script to read from +// token : buffer to store the string +// Returns: qtrue when a string was read succesfully +// Changes Globals: - +//============================================================================ +int PS_ReadString( script_t *script, token_t *token, int quote ) { + int len, tmpline; + char *tmpscript_p; + + if ( quote == '\"' ) { + token->type = TT_STRING; + } else { token->type = TT_LITERAL;} + + len = 0; + //leading quote + token->string[len++] = *script->script_p++; + // + while ( 1 ) + { + //minus 2 because trailing double quote and zero have to be appended + if ( len >= MAX_TOKEN - 2 ) { + ScriptError( script, "string longer than MAX_TOKEN = %d", MAX_TOKEN ); + return 0; + } //end if + //if there is an escape character and + //if escape characters inside a string are allowed + if ( *script->script_p == '\\' && !( script->flags & SCFL_NOSTRINGESCAPECHARS ) ) { + if ( !PS_ReadEscapeCharacter( script, &token->string[len] ) ) { + token->string[len] = 0; + return 0; + } //end if + len++; + } //end if + //if a trailing quote + else if ( *script->script_p == quote ) { + //step over the double quote + script->script_p++; + //if white spaces in a string are not allowed + if ( script->flags & SCFL_NOSTRINGWHITESPACES ) { + break; + } + // + tmpscript_p = script->script_p; + tmpline = script->line; + //read unusefull stuff between possible two following strings + if ( !PS_ReadWhiteSpace( script ) ) { + script->script_p = tmpscript_p; + script->line = tmpline; + break; + } //end if + //if there's no leading double qoute + if ( *script->script_p != quote ) { + script->script_p = tmpscript_p; + script->line = tmpline; + break; + } //end if + //step over the new leading double quote + script->script_p++; + } //end if + else + { + if ( *script->script_p == '\0' ) { + token->string[len] = 0; + ScriptError( script, "missing trailing quote" ); + return 0; + } //end if + if ( *script->script_p == '\n' ) { + token->string[len] = 0; + ScriptError( script, "newline inside string %s", token->string ); + return 0; + } //end if + token->string[len++] = *script->script_p++; + } //end else + } //end while + //trailing quote + token->string[len++] = quote; + //end string with a zero + token->string[len] = '\0'; + //the sub type is the length of the string + token->subtype = len; + return 1; +} //end of the function PS_ReadString +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ReadName( script_t *script, token_t *token ) { + int len = 0; + char c; + + token->type = TT_NAME; + do + { + token->string[len++] = *script->script_p++; + if ( len >= MAX_TOKEN ) { + ScriptError( script, "name longer than MAX_TOKEN = %d", MAX_TOKEN ); + return 0; + } //end if + c = *script->script_p; + } while ( ( c >= 'a' && c <= 'z' ) || + ( c >= 'A' && c <= 'Z' ) || + ( c >= '0' && c <= '9' ) || + c == '_' ); + token->string[len] = '\0'; + //the sub type is the length of the name + token->subtype = len; + return 1; +} //end of the function PS_ReadName +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void NumberValue( char *string, int subtype, unsigned long int *intvalue, + long double *floatvalue ) { + unsigned long int dotfound = 0; + + *intvalue = 0; + *floatvalue = 0; + //floating point number + if ( subtype & TT_FLOAT ) { + while ( *string ) + { + if ( *string == '.' ) { + if ( dotfound ) { + return; + } + dotfound = 10; + string++; + } //end if + if ( dotfound ) { + *floatvalue = *floatvalue + ( long double )( *string - '0' ) / + (long double) dotfound; + dotfound *= 10; + } //end if + else + { + *floatvalue = *floatvalue * 10.0 + ( long double )( *string - '0' ); + } //end else + string++; + } //end while + *intvalue = (unsigned long) *floatvalue; + } //end if + else if ( subtype & TT_DECIMAL ) { + while ( *string ) *intvalue = *intvalue * 10 + ( *string++ - '0' ); + *floatvalue = *intvalue; + } //end else if + else if ( subtype & TT_HEX ) { + //step over the leading 0x or 0X + string += 2; + while ( *string ) + { + *intvalue <<= 4; + if ( *string >= 'a' && *string <= 'f' ) { + *intvalue += *string - 'a' + 10; + } else if ( *string >= 'A' && *string <= 'F' ) { + *intvalue += *string - 'A' + 10; + } else { *intvalue += *string - '0';} + string++; + } //end while + *floatvalue = *intvalue; + } //end else if + else if ( subtype & TT_OCTAL ) { + //step over the first zero + string += 1; + while ( *string ) *intvalue = ( *intvalue << 3 ) + ( *string++ - '0' ); + *floatvalue = *intvalue; + } //end else if + else if ( subtype & TT_BINARY ) { + //step over the leading 0b or 0B + string += 2; + while ( *string ) *intvalue = ( *intvalue << 1 ) + ( *string++ - '0' ); + *floatvalue = *intvalue; + } //end else if +} //end of the function NumberValue +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ReadNumber( script_t *script, token_t *token ) { + int len = 0, i; + int octal, dot; + char c; +// unsigned long int intvalue = 0; +// long double floatvalue = 0; + + token->type = TT_NUMBER; + //check for a hexadecimal number + if ( *script->script_p == '0' && + ( *( script->script_p + 1 ) == 'x' || + *( script->script_p + 1 ) == 'X' ) ) { + token->string[len++] = *script->script_p++; + token->string[len++] = *script->script_p++; + c = *script->script_p; + //hexadecimal + while ( ( c >= '0' && c <= '9' ) || + ( c >= 'a' && c <= 'f' ) || + ( c >= 'A' && c <= 'A' ) ) + { + token->string[len++] = *script->script_p++; + if ( len >= MAX_TOKEN ) { + ScriptError( script, "hexadecimal number longer than MAX_TOKEN = %d", MAX_TOKEN ); + return 0; + } //end if + c = *script->script_p; + } //end while + token->subtype |= TT_HEX; + } //end if +#ifdef BINARYNUMBERS + //check for a binary number + else if ( *script->script_p == '0' && + ( *( script->script_p + 1 ) == 'b' || + *( script->script_p + 1 ) == 'B' ) ) { + token->string[len++] = *script->script_p++; + token->string[len++] = *script->script_p++; + c = *script->script_p; + //hexadecimal + while ( c == '0' || c == '1' ) + { + token->string[len++] = *script->script_p++; + if ( len >= MAX_TOKEN ) { + ScriptError( script, "binary number longer than MAX_TOKEN = %d", MAX_TOKEN ); + return 0; + } //end if + c = *script->script_p; + } //end while + token->subtype |= TT_BINARY; + } //end if +#endif //BINARYNUMBERS + else //decimal or octal integer or floating point number + { + octal = qfalse; + dot = qfalse; + if ( *script->script_p == '0' ) { + octal = qtrue; + } + while ( 1 ) + { + token->string[len++] = *script->script_p++; + if ( len >= MAX_TOKEN ) { + ScriptError( script, "number longer than MAX_TOKEN = %d", MAX_TOKEN ); + return 0; + } //end if + c = *script->script_p; + if ( c == '.' ) { + dot = qtrue; + } else if ( c == '8' || c == '9' ) { + octal = qfalse; + } else if ( c < '0' || c > '9' ) { + break; + } + } //end while + if ( octal ) { + token->subtype |= TT_OCTAL; + } else { token->subtype |= TT_DECIMAL;} + if ( dot ) { + token->subtype |= TT_FLOAT; + } + } //end else + for ( i = 0; i < 2; i++ ) + { + c = *script->script_p; + //check for a LONG number + if ( c == 'l' || c == 'L' && + !( token->subtype & TT_LONG ) ) { + script->script_p++; + token->subtype |= TT_LONG; + } //end if + //check for an UNSIGNED number + else if ( c == 'u' || c == 'U' && + !( token->subtype & ( TT_UNSIGNED | TT_FLOAT ) ) ) { + script->script_p++; + token->subtype |= TT_UNSIGNED; + } //end if + } //end for + token->string[len] = '\0'; +#ifdef NUMBERVALUE + NumberValue( token->string, token->subtype, &token->intvalue, &token->floatvalue ); +#endif //NUMBERVALUE + if ( !( token->subtype & TT_FLOAT ) ) { + token->subtype |= TT_INTEGER; + } + return 1; +} //end of the function PS_ReadNumber +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ReadLiteral( script_t *script, token_t *token ) { + token->type = TT_LITERAL; + //first quote + token->string[0] = *script->script_p++; + //check for end of file + if ( !*script->script_p ) { + ScriptError( script, "end of file before trailing \'" ); + return 0; + } //end if + //if it is an escape character + if ( *script->script_p == '\\' ) { + if ( !PS_ReadEscapeCharacter( script, &token->string[1] ) ) { + return 0; + } + } //end if + else + { + token->string[1] = *script->script_p++; + } //end else + //check for trailing quote + if ( *script->script_p != '\'' ) { + ScriptWarning( script, "too many characters in literal, ignored" ); + while ( *script->script_p && + *script->script_p != '\'' && + *script->script_p != '\n' ) + { + script->script_p++; + } //end while + if ( *script->script_p == '\'' ) { + script->script_p++; + } + } //end if + //store the trailing quote + token->string[2] = *script->script_p++; + //store trailing zero to end the string + token->string[3] = '\0'; + //the sub type is the integer literal value + token->subtype = token->string[1]; + // + return 1; +} //end of the function PS_ReadLiteral +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ReadPunctuation( script_t *script, token_t *token ) { + int len; + char *p; + punctuation_t *punc; + +#ifdef PUNCTABLE + for ( punc = script->punctuationtable[(unsigned int)*script->script_p]; punc; punc = punc->next ) + { +#else + int i; + + for ( i = 0; script->punctuations[i].p; i++ ) + { + punc = &script->punctuations[i]; +#endif //PUNCTABLE + p = punc->p; + len = strlen( p ); + //if the script contains at least as much characters as the punctuation + if ( script->script_p + len <= script->end_p ) { + //if the script contains the punctuation + if ( !strncmp( script->script_p, p, len ) ) { + strncpy( token->string, p, MAX_TOKEN ); + script->script_p += len; + token->type = TT_PUNCTUATION; + //sub type is the number of the punctuation + token->subtype = punc->n; + return 1; + } //end if + } //end if + } //end for + return 0; +} //end of the function PS_ReadPunctuation +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ReadPrimitive( script_t *script, token_t *token ) { + int len; + + len = 0; + while ( *script->script_p > ' ' && *script->script_p != ';' ) + { + if ( len >= MAX_TOKEN ) { + ScriptError( script, "primitive token longer than MAX_TOKEN = %d", MAX_TOKEN ); + return 0; + } //end if + token->string[len++] = *script->script_p++; + } //end while + token->string[len] = 0; + //copy the token into the script structure + memcpy( &script->token, token, sizeof( token_t ) ); + //primitive reading successfull + return 1; +} //end of the function PS_ReadPrimitive +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ReadToken( script_t *script, token_t *token ) { + //if there is a token available (from UnreadToken) + if ( script->tokenavailable ) { + script->tokenavailable = 0; + memcpy( token, &script->token, sizeof( token_t ) ); + return 1; + } //end if + //save script pointer + script->lastscript_p = script->script_p; + //save line counter + script->lastline = script->line; + //clear the token stuff + memset( token, 0, sizeof( token_t ) ); + //start of the white space + script->whitespace_p = script->script_p; + token->whitespace_p = script->script_p; + //read unusefull stuff + if ( !PS_ReadWhiteSpace( script ) ) { + return 0; + } + //end of the white space + script->endwhitespace_p = script->script_p; + token->endwhitespace_p = script->script_p; + //line the token is on + token->line = script->line; + //number of lines crossed before token + token->linescrossed = script->line - script->lastline; + //if there is a leading double quote + if ( *script->script_p == '\"' ) { + if ( !PS_ReadString( script, token, '\"' ) ) { + return 0; + } + } //end if + //if an literal + else if ( *script->script_p == '\'' ) { + //if (!PS_ReadLiteral(script, token)) return 0; + if ( !PS_ReadString( script, token, '\'' ) ) { + return 0; + } + } //end if + //if there is a number + else if ( ( *script->script_p >= '0' && *script->script_p <= '9' ) || + ( *script->script_p == '.' && + ( *( script->script_p + 1 ) >= '0' && *( script->script_p + 1 ) <= '9' ) ) ) { + if ( !PS_ReadNumber( script, token ) ) { + return 0; + } + } //end if + //if this is a primitive script + else if ( script->flags & SCFL_PRIMITIVE ) { + return PS_ReadPrimitive( script, token ); + } //end else if + //if there is a name + else if ( ( *script->script_p >= 'a' && *script->script_p <= 'z' ) || + ( *script->script_p >= 'A' && *script->script_p <= 'Z' ) || + *script->script_p == '_' ) { + if ( !PS_ReadName( script, token ) ) { + return 0; + } + } //end if + //check for punctuations + else if ( !PS_ReadPunctuation( script, token ) ) { + ScriptError( script, "can't read token" ); + return 0; + } //end if + //copy the token into the script structure + memcpy( &script->token, token, sizeof( token_t ) ); + //succesfully read a token + return 1; +} //end of the function PS_ReadToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ExpectTokenString( script_t *script, char *string ) { + token_t token; + + if ( !PS_ReadToken( script, &token ) ) { + ScriptError( script, "couldn't find expected %s", string ); + return 0; + } //end if + + if ( strcmp( token.string, string ) ) { + ScriptError( script, "expected %s, found %s", string, token.string ); + return 0; + } //end if + return 1; +} //end of the function PS_ExpectToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ExpectTokenType( script_t *script, int type, int subtype, token_t *token ) { + char str[MAX_TOKEN]; + + if ( !PS_ReadToken( script, token ) ) { + ScriptError( script, "couldn't read expected token" ); + return 0; + } //end if + + if ( token->type != type ) { + if ( type == TT_STRING ) { + strcpy( str, "string" ); + } + if ( type == TT_LITERAL ) { + strcpy( str, "literal" ); + } + if ( type == TT_NUMBER ) { + strcpy( str, "number" ); + } + if ( type == TT_NAME ) { + strcpy( str, "name" ); + } + if ( type == TT_PUNCTUATION ) { + strcpy( str, "punctuation" ); + } + ScriptError( script, "expected a %s, found %s", str, token->string ); + return 0; + } //end if + if ( token->type == TT_NUMBER ) { + if ( ( token->subtype & subtype ) != subtype ) { + if ( subtype & TT_DECIMAL ) { + strcpy( str, "decimal" ); + } + if ( subtype & TT_HEX ) { + strcpy( str, "hex" ); + } + if ( subtype & TT_OCTAL ) { + strcpy( str, "octal" ); + } + if ( subtype & TT_BINARY ) { + strcpy( str, "binary" ); + } + if ( subtype & TT_LONG ) { + strcat( str, " long" ); + } + if ( subtype & TT_UNSIGNED ) { + strcat( str, " unsigned" ); + } + if ( subtype & TT_FLOAT ) { + strcat( str, " float" ); + } + if ( subtype & TT_INTEGER ) { + strcat( str, " integer" ); + } + ScriptError( script, "expected %s, found %s", str, token->string ); + return 0; + } //end if + } //end if + else if ( token->type == TT_PUNCTUATION ) { + if ( subtype < 0 ) { + ScriptError( script, "BUG: wrong punctuation subtype" ); + return 0; + } //end if + if ( token->subtype != subtype ) { + ScriptError( script, "expected %s, found %s", + script->punctuations[subtype], token->string ); + return 0; + } //end if + } //end else if + return 1; +} //end of the function PS_ExpectTokenType +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_ExpectAnyToken( script_t *script, token_t *token ) { + if ( !PS_ReadToken( script, token ) ) { + ScriptError( script, "couldn't read expected token" ); + return 0; + } //end if + else + { + return 1; + } //end else +} //end of the function PS_ExpectAnyToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_CheckTokenString( script_t *script, char *string ) { + token_t tok; + + if ( !PS_ReadToken( script, &tok ) ) { + return 0; + } + //if the token is available + if ( !strcmp( tok.string, string ) ) { + return 1; + } + //token not available + script->script_p = script->lastscript_p; + return 0; +} //end of the function PS_CheckTokenString +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_CheckTokenType( script_t *script, int type, int subtype, token_t *token ) { + token_t tok; + + if ( !PS_ReadToken( script, &tok ) ) { + return 0; + } + //if the type matches + if ( tok.type == type && + ( tok.subtype & subtype ) == subtype ) { + memcpy( token, &tok, sizeof( token_t ) ); + return 1; + } //end if + //token is not available + script->script_p = script->lastscript_p; + return 0; +} //end of the function PS_CheckTokenType +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int PS_SkipUntilString( script_t *script, char *string ) { + token_t token; + + while ( PS_ReadToken( script, &token ) ) + { + if ( !strcmp( token.string, string ) ) { + return 1; + } + } //end while + return 0; +} //end of the function PS_SkipUntilString +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PS_UnreadLastToken( script_t *script ) { + script->tokenavailable = 1; +} //end of the function UnreadLastToken +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void PS_UnreadToken( script_t *script, token_t *token ) { + memcpy( &script->token, token, sizeof( token_t ) ); + script->tokenavailable = 1; +} //end of the function UnreadToken +//============================================================================ +// returns the next character of the read white space, returns NULL if none +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +char PS_NextWhiteSpaceChar( script_t *script ) { + if ( script->whitespace_p != script->endwhitespace_p ) { + return *script->whitespace_p++; + } //end if + else + { + return 0; + } //end else +} //end of the function PS_NextWhiteSpaceChar +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void StripDoubleQuotes( char *string ) { + if ( *string == '\"' ) { + strcpy( string, string + 1 ); + } //end if + if ( string[strlen( string ) - 1] == '\"' ) { + string[strlen( string ) - 1] = '\0'; + } //end if +} //end of the function StripDoubleQuotes +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void StripSingleQuotes( char *string ) { + if ( *string == '\'' ) { + strcpy( string, string + 1 ); + } //end if + if ( string[strlen( string ) - 1] == '\'' ) { + string[strlen( string ) - 1] = '\0'; + } //end if +} //end of the function StripSingleQuotes +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +long double ReadSignedFloat( script_t *script ) { + token_t token; + long double sign = 1; + + PS_ExpectAnyToken( script, &token ); + if ( !strcmp( token.string, "-" ) ) { + sign = -1; + PS_ExpectTokenType( script, TT_NUMBER, 0, &token ); + } //end if + else if ( token.type != TT_NUMBER ) { + ScriptError( script, "expected float value, found %s\n", token.string ); + } //end else if + return sign * token.floatvalue; +} //end of the function ReadSignedFloat +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +signed long int ReadSignedInt( script_t *script ) { + token_t token; + signed long int sign = 1; + + PS_ExpectAnyToken( script, &token ); + if ( !strcmp( token.string, "-" ) ) { + sign = -1; + PS_ExpectTokenType( script, TT_NUMBER, TT_INTEGER, &token ); + } //end if + else if ( token.type != TT_NUMBER || token.subtype == TT_FLOAT ) { + ScriptError( script, "expected integer value, found %s\n", token.string ); + } //end else if + return sign * token.intvalue; +} //end of the function ReadSignedInt +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void SetScriptFlags( script_t *script, int flags ) { + script->flags = flags; +} //end of the function SetScriptFlags +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int GetScriptFlags( script_t *script ) { + return script->flags; +} //end of the function GetScriptFlags +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void ResetScript( script_t *script ) { + //pointer in script buffer + script->script_p = script->buffer; + //pointer in script buffer before reading token + script->lastscript_p = script->buffer; + //begin of white space + script->whitespace_p = NULL; + //end of white space + script->endwhitespace_p = NULL; + //set if there's a token available in script->token + script->tokenavailable = 0; + // + script->line = 1; + script->lastline = 1; + //clear the saved token + memset( &script->token, 0, sizeof( token_t ) ); +} //end of the function ResetScript +//============================================================================ +// returns true if at the end of the script +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int EndOfScript( script_t *script ) { + return script->script_p >= script->end_p; +} //end of the function EndOfScript +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int NumLinesCrossed( script_t *script ) { + return script->line - script->lastline; +} //end of the function NumLinesCrossed +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int ScriptSkipTo( script_t *script, char *value ) { + int len; + char firstchar; + + firstchar = *value; + len = strlen( value ); + do + { + if ( !PS_ReadWhiteSpace( script ) ) { + return 0; + } + if ( *script->script_p == firstchar ) { + if ( !strncmp( script->script_p, value, len ) ) { + return 1; + } //end if + } //end if + script->script_p++; + } while ( 1 ); +} //end of the function ScriptSkipTo +#ifndef BOTLIB +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +int FileLength( FILE *fp ) { + int pos; + int end; + + pos = ftell( fp ); + fseek( fp, 0, SEEK_END ); + end = ftell( fp ); + fseek( fp, pos, SEEK_SET ); + + return end; +} //end of the function FileLength +#endif +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +script_t *LoadScriptFile( char *filename ) { +#ifdef BOTLIB + fileHandle_t fp; + char pathname[MAX_QPATH]; +#else + FILE *fp; +#endif + int length; + void *buffer; + script_t *script; + +#ifdef BOTLIB + Com_sprintf( pathname, MAX_QPATH, "botfiles/%s", filename ); + length = botimport.FS_FOpenFile( pathname, &fp, FS_READ ); + if ( !fp ) { + return NULL; + } +#else + fp = fopen( filename, "rb" ); + if ( !fp ) { + return NULL; + } + + length = FileLength( fp ); +#endif + + buffer = GetClearedMemory( sizeof( script_t ) + length + 1 ); + script = (script_t *) buffer; + memset( script, 0, sizeof( script_t ) ); + strcpy( script->filename, filename ); + script->buffer = (char *) buffer + sizeof( script_t ); + script->buffer[length] = 0; + script->length = length; + //pointer in script buffer + script->script_p = script->buffer; + //pointer in script buffer before reading token + script->lastscript_p = script->buffer; + //pointer to end of script buffer + script->end_p = &script->buffer[length]; + //set if there's a token available in script->token + script->tokenavailable = 0; + // + script->line = 1; + script->lastline = 1; + // + SetScriptPunctuations( script, NULL ); + // +#ifdef BOTLIB + botimport.FS_Read( script->buffer, length, fp ); + botimport.FS_FCloseFile( fp ); +#else + if ( fread( script->buffer, length, 1, fp ) != 1 ) { + FreeMemory( buffer ); + script = NULL; + } //end if + fclose( fp ); +#endif + // + return script; +} //end of the function LoadScriptFile +//============================================================================ +//load a script from the given memory with the given length +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +script_t *LoadScriptMemory( char *ptr, int length, char *name ) { + void *buffer; + script_t *script; + + buffer = GetClearedMemory( sizeof( script_t ) + length + 1 ); + script = (script_t *) buffer; + memset( script, 0, sizeof( script_t ) ); + strcpy( script->filename, name ); + script->buffer = (char *) buffer + sizeof( script_t ); + script->buffer[length] = 0; + script->length = length; + //pointer in script buffer + script->script_p = script->buffer; + //pointer in script buffer before reading token + script->lastscript_p = script->buffer; + //pointer to end of script buffer + script->end_p = &script->buffer[length]; + //set if there's a token available in script->token + script->tokenavailable = 0; + // + script->line = 1; + script->lastline = 1; + // + SetScriptPunctuations( script, NULL ); + // + memcpy( script->buffer, ptr, length ); + // + return script; +} //end of the function LoadScriptMemory +//============================================================================ +// +// Parameter: - +// Returns: - +// Changes Globals: - +//============================================================================ +void FreeScript( script_t *script ) { +#ifdef PUNCTABLE + if ( script->punctuationtable ) { + FreeMemory( script->punctuationtable ); + } +#endif //PUNCTABLE + FreeMemory( script ); +} //end of the function FreeScript diff --git a/src/extractfuncs/l_script.h b/src/extractfuncs/l_script.h new file mode 100644 index 0000000..78cb90a --- /dev/null +++ b/src/extractfuncs/l_script.h @@ -0,0 +1,266 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + + +/***************************************************************************** + * name: l_script.h + * + * desc: lexicographical parser + * + * + *****************************************************************************/ + +// Ridah, can't get it to compile without this +#ifndef QDECL + +// for windows fastcall option +#define QDECL +//======================= WIN32 DEFINES ================================= +#ifdef WIN32 +#undef QDECL +#define QDECL __cdecl +#endif +#endif +// done. + +//undef if binary numbers of the form 0b... or 0B... are not allowed +#define BINARYNUMBERS +//undef if not using the token.intvalue and token.floatvalue +#define NUMBERVALUE +//use dollar sign also as punctuation +#define DOLLAR + +//maximum token length +#define MAX_TOKEN 1024 +//maximum path length +#ifndef MAX_QPATH + #define MAX_QPATH 64 +#endif +#ifndef _MAX_PATH + #define _MAX_PATH MAX_QPATH +#endif + +//script flags +#define SCFL_NOERRORS 0x0001 +#define SCFL_NOWARNINGS 0x0002 +#define SCFL_NOSTRINGWHITESPACES 0x0004 +#define SCFL_NOSTRINGESCAPECHARS 0x0008 +#define SCFL_PRIMITIVE 0x0010 +#define SCFL_NOBINARYNUMBERS 0x0020 +#define SCFL_NONUMBERVALUES 0x0040 + +//token types +#define TT_STRING 1 // string +#define TT_LITERAL 2 // literal +#define TT_NUMBER 3 // number +#define TT_NAME 4 // name +#define TT_PUNCTUATION 5 // punctuation + +//string sub type +//--------------- +// the length of the string +//literal sub type +//---------------- +// the ASCII code of the literal +//number sub type +//--------------- +#define TT_DECIMAL 0x0008 // decimal number +#define TT_HEX 0x0100 // hexadecimal number +#define TT_OCTAL 0x0200 // octal number +#ifdef BINARYNUMBERS +#define TT_BINARY 0x0400 // binary number +#endif //BINARYNUMBERS +#define TT_FLOAT 0x0800 // floating point number +#define TT_INTEGER 0x1000 // integer number +#define TT_LONG 0x2000 // long number +#define TT_UNSIGNED 0x4000 // unsigned number +//punctuation sub type +//-------------------- +#define P_RSHIFT_ASSIGN 1 +#define P_LSHIFT_ASSIGN 2 +#define P_PARMS 3 +#define P_PRECOMPMERGE 4 + +#define P_LOGIC_AND 5 +#define P_LOGIC_OR 6 +#define P_LOGIC_GEQ 7 +#define P_LOGIC_LEQ 8 +#define P_LOGIC_EQ 9 +#define P_LOGIC_UNEQ 10 + +#define P_MUL_ASSIGN 11 +#define P_DIV_ASSIGN 12 +#define P_MOD_ASSIGN 13 +#define P_ADD_ASSIGN 14 +#define P_SUB_ASSIGN 15 +#define P_INC 16 +#define P_DEC 17 + +#define P_BIN_AND_ASSIGN 18 +#define P_BIN_OR_ASSIGN 19 +#define P_BIN_XOR_ASSIGN 20 +#define P_RSHIFT 21 +#define P_LSHIFT 22 + +#define P_POINTERREF 23 +#define P_CPP1 24 +#define P_CPP2 25 +#define P_MUL 26 +#define P_DIV 27 +#define P_MOD 28 +#define P_ADD 29 +#define P_SUB 30 +#define P_ASSIGN 31 + +#define P_BIN_AND 32 +#define P_BIN_OR 33 +#define P_BIN_XOR 34 +#define P_BIN_NOT 35 + +#define P_LOGIC_NOT 36 +#define P_LOGIC_GREATER 37 +#define P_LOGIC_LESS 38 + +#define P_REF 39 +#define P_COMMA 40 +#define P_SEMICOLON 41 +#define P_COLON 42 +#define P_QUESTIONMARK 43 + +#define P_PARENTHESESOPEN 44 +#define P_PARENTHESESCLOSE 45 +#define P_BRACEOPEN 46 +#define P_BRACECLOSE 47 +#define P_SQBRACKETOPEN 48 +#define P_SQBRACKETCLOSE 49 +#define P_BACKSLASH 50 + +#define P_PRECOMP 51 +#define P_DOLLAR 52 +//name sub type +//------------- +// the length of the name + +//punctuation +typedef struct punctuation_s +{ + char *p; //punctuation character(s) + int n; //punctuation indication + struct punctuation_s *next; //next punctuation +} punctuation_t; + +//token +typedef struct token_s +{ + char string[MAX_TOKEN]; //available token + int type; //last read token type + int subtype; //last read token sub type +#ifdef NUMBERVALUE + unsigned long int intvalue; //integer value + long double floatvalue; //floating point value +#endif //NUMBERVALUE + char *whitespace_p; //start of white space before token + char *endwhitespace_p; //start of white space before token + int line; //line the token was on + int linescrossed; //lines crossed in white space + struct token_s *next; //next token in chain +} token_t; + +//script file +typedef struct script_s +{ + char filename[_MAX_PATH]; //file name of the script + char *buffer; //buffer containing the script + char *script_p; //current pointer in the script + char *end_p; //pointer to the end of the script + char *lastscript_p; //script pointer before reading token + char *whitespace_p; //begin of the white space + char *endwhitespace_p; //end of the white space + int length; //length of the script in bytes + int line; //current line in script + int lastline; //line before reading token + int tokenavailable; //set by UnreadLastToken + int flags; //several script flags + punctuation_t *punctuations; //the punctuations used in the script + punctuation_t **punctuationtable; + token_t token; //available token + struct script_s *next; //next script in a chain +} script_t; + +//read a token from the script +int PS_ReadToken( script_t *script, token_t *token ); +//expect a certain token +int PS_ExpectTokenString( script_t *script, char *string ); +//expect a certain token type +int PS_ExpectTokenType( script_t *script, int type, int subtype, token_t *token ); +//expect a token +int PS_ExpectAnyToken( script_t *script, token_t *token ); +//returns true when the token is available +int PS_CheckTokenString( script_t *script, char *string ); +//returns true an reads the token when a token with the given type is available +int PS_CheckTokenType( script_t *script, int type, int subtype, token_t *token ); +//skip tokens until the given token string is read +int PS_SkipUntilString( script_t *script, char *string ); +//unread the last token read from the script +void PS_UnreadLastToken( script_t *script ); +//unread the given token +void PS_UnreadToken( script_t *script, token_t *token ); +//returns the next character of the read white space, returns NULL if none +char PS_NextWhiteSpaceChar( script_t *script ); +//remove any leading and trailing double quotes from the token +void StripDoubleQuotes( char *string ); +//remove any leading and trailing single quotes from the token +void StripSingleQuotes( char *string ); +//read a possible signed integer +signed long int ReadSignedInt( script_t *script ); +//read a possible signed floating point number +long double ReadSignedFloat( script_t *script ); +//set an array with punctuations, NULL restores default C/C++ set +void SetScriptPunctuations( script_t *script, punctuation_t *p ); +//set script flags +void SetScriptFlags( script_t *script, int flags ); +//get script flags +int GetScriptFlags( script_t *script ); +//reset a script +void ResetScript( script_t *script ); +//returns true if at the end of the script +int EndOfScript( script_t *script ); +//returns a pointer to the punctuation with the given number +char *PunctuationFromNum( script_t *script, int num ); +//load a script from the given file at the given offset with the given length +script_t *LoadScriptFile( char *filename ); +//load a script from the given memory with the given length +script_t *LoadScriptMemory( char *ptr, int length, char *name ); +//free a script +void FreeScript( script_t *script ); +//print a script error with filename and line number +void QDECL ScriptError( script_t *script, char *str, ... ); +//print a script warning with filename and line number +void QDECL ScriptWarning( script_t *script, char *str, ... ); + + diff --git a/src/ft2/ahangles.c b/src/ft2/ahangles.c new file mode 100644 index 0000000..18f7444 --- /dev/null +++ b/src/ft2/ahangles.c @@ -0,0 +1,128 @@ +/***************************************************************************/ +/* */ +/* ahangles.h */ +/* */ +/* A routine used to compute vector angles with limited accuracy */ +/* and very high speed (body). */ +/* */ +/* Copyright 2000 Catharon Productions Inc. */ +/* Author: David Turner */ +/* */ +/* This file is part of the Catharon Typography Project and shall only */ +/* be used, modified, and distributed under the terms of the Catharon */ +/* Open Source License that should come with this file under the name */ +/* `CatharonLicense.txt'. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/* Note that this license is compatible with the FreeType license. */ +/* */ +/***************************************************************************/ + + +#include "ahangles.h" + + +/* the following table has been automatically generated with */ +/* the `mather.py' Python script */ + +const AH_Angle ah_arctan[1L << AH_ATAN_BITS] = +{ + 0, 0, 1, 1, 1, 2, 2, 2, + 3, 3, 3, 3, 4, 4, 4, 5, + 5, 5, 6, 6, 6, 7, 7, 7, + 8, 8, 8, 9, 9, 9, 10, 10, + 10, 10, 11, 11, 11, 12, 12, 12, + 13, 13, 13, 14, 14, 14, 14, 15, + 15, 15, 16, 16, 16, 17, 17, 17, + 18, 18, 18, 18, 19, 19, 19, 20, + 20, 20, 21, 21, 21, 21, 22, 22, + 22, 23, 23, 23, 24, 24, 24, 24, + 25, 25, 25, 26, 26, 26, 26, 27, + 27, 27, 28, 28, 28, 28, 29, 29, + 29, 30, 30, 30, 30, 31, 31, 31, + 31, 32, 32, 32, 33, 33, 33, 33, + 34, 34, 34, 34, 35, 35, 35, 35, + 36, 36, 36, 36, 37, 37, 37, 38, + 38, 38, 38, 39, 39, 39, 39, 40, + 40, 40, 40, 41, 41, 41, 41, 42, + 42, 42, 42, 42, 43, 43, 43, 43, + 44, 44, 44, 44, 45, 45, 45, 45, + 46, 46, 46, 46, 46, 47, 47, 47, + 47, 48, 48, 48, 48, 48, 49, 49, + 49, 49, 50, 50, 50, 50, 50, 51, + 51, 51, 51, 51, 52, 52, 52, 52, + 52, 53, 53, 53, 53, 53, 54, 54, + 54, 54, 54, 55, 55, 55, 55, 55, + 56, 56, 56, 56, 56, 57, 57, 57, + 57, 57, 57, 58, 58, 58, 58, 58, + 59, 59, 59, 59, 59, 59, 60, 60, + 60, 60, 60, 61, 61, 61, 61, 61, + 61, 62, 62, 62, 62, 62, 62, 63, + 63, 63, 63, 63, 63, 64, 64, 64 +}; + + +LOCAL_FUNC +AH_Angle ah_angle( FT_Vector* v ) { + FT_Pos dx, dy; + AH_Angle angle; + + + dx = v->x; + dy = v->y; + + /* check trivial cases */ + if ( dy == 0 ) { + angle = 0; + if ( dx < 0 ) { + angle = AH_PI; + } + return angle; + } else if ( dx == 0 ) { + angle = AH_HALF_PI; + if ( dy < 0 ) { + angle = -AH_HALF_PI; + } + return angle; + } + + angle = 0; + if ( dx < 0 ) { + dx = -v->x; + dy = -v->y; + angle = AH_PI; + } + + if ( dy < 0 ) { + FT_Pos tmp; + + + tmp = dx; + dx = -dy; + dy = tmp; + angle -= AH_HALF_PI; + } + + if ( dx == 0 && dy == 0 ) { + return 0; + } + + if ( dx == dy ) { + angle += AH_PI / 4; + } else if ( dx > dy ) { + angle += ah_arctan[FT_DivFix( dy, dx ) >> ( 16 - AH_ATAN_BITS )]; + } else { + angle += AH_HALF_PI - + ah_arctan[FT_DivFix( dx, dy ) >> ( 16 - AH_ATAN_BITS )]; + } + + if ( angle > AH_PI ) { + angle -= AH_2PI; + } + + return angle; +} + + +/* END */ diff --git a/src/ft2/ahangles.h b/src/ft2/ahangles.h new file mode 100644 index 0000000..e16b637 --- /dev/null +++ b/src/ft2/ahangles.h @@ -0,0 +1,55 @@ +/***************************************************************************/ +/* */ +/* ahangles.h */ +/* */ +/* A routine used to compute vector angles with limited accuracy */ +/* and very high speed (specification). */ +/* */ +/* Copyright 2000 Catharon Productions Inc. */ +/* Author: David Turner */ +/* */ +/* This file is part of the Catharon Typography Project and shall only */ +/* be used, modified, and distributed under the terms of the Catharon */ +/* Open Source License that should come with this file under the name */ +/* `CatharonLicense.txt'. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/* Note that this license is compatible with the FreeType license. */ +/* */ +/***************************************************************************/ + + +#ifndef AHANGLES_H +#define AHANGLES_H + + + +#include "ahtypes.h" + +#include "ftobjs.h" + + +/* PI expressed in ah_angles -- we don't really need an important */ +/* precision, so 256 should be enough */ +#define AH_PI 256 +#define AH_2PI ( AH_PI * 2 ) +#define AH_HALF_PI ( AH_PI / 2 ) +#define AH_2PIMASK ( AH_2PI - 1 ) + +/* the number of bits used to express an arc tangent; */ +/* see the structure of the lookup table */ +#define AH_ATAN_BITS 8 + +extern +const AH_Angle ah_arctan[1L << AH_ATAN_BITS]; + + +LOCAL_DEF +AH_Angle ah_angle( FT_Vector* v ); + + +#endif /* AHANGLES_H */ + + +/* END */ diff --git a/src/ft2/ahglobal.c b/src/ft2/ahglobal.c new file mode 100644 index 0000000..8957327 --- /dev/null +++ b/src/ft2/ahglobal.c @@ -0,0 +1,397 @@ +/***************************************************************************/ +/* */ +/* ahglobal.c */ +/* */ +/* Routines used to compute global metrics automatically (body). */ +/* */ +/* Copyright 2000 Catharon Productions Inc. */ +/* Author: David Turner */ +/* */ +/* This file is part of the Catharon Typography Project and shall only */ +/* be used, modified, and distributed under the terms of the Catharon */ +/* Open Source License that should come with this file under the name */ +/* `CatharonLicense.txt'. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/* Note that this license is compatible with the FreeType license. */ +/* */ +/***************************************************************************/ + +#include "ahglobal.h" +#include "ahglyph.h" + +#define MAX_TEST_CHARACTERS 12 + +static +const char* blue_chars[ah_blue_max] = +{ + "THEZOCQS", + "HEZLOCUS", + "xzroesc", + "xzroesc", + "pqgjy" +}; + + +/* simple insertion sort */ +static +void sort_values( FT_Int count, + FT_Pos* table ) { + FT_Int i, j, swap; + + + for ( i = 1; i < count; i++ ) + { + for ( j = i; j > 1; j-- ) + { + if ( table[j] > table[j - 1] ) { + break; + } + + swap = table[j]; + table[j] = table[j - 1]; + table[j - 1] = swap; + } + } +} + + +static +FT_Error ah_hinter_compute_blues( AH_Hinter* hinter ) { + AH_Blue blue; + AH_Globals* globals = &hinter->globals->design; + FT_Pos flats [MAX_TEST_CHARACTERS]; + FT_Pos rounds[MAX_TEST_CHARACTERS]; + FT_Int num_flats; + FT_Int num_rounds; + + FT_Face face; + FT_GlyphSlot glyph; + FT_Error error; + FT_CharMap charmap; + + + face = hinter->face; + glyph = face->glyph; + + /* save current charmap */ + charmap = face->charmap; + + /* do we have a Unicode charmap in there? */ + error = FT_Select_Charmap( face, ft_encoding_unicode ); + if ( error ) { + goto Exit; + } + + /* we compute the blues simply by loading each character from the */ + /* 'blue_chars[blues]' string, then compute its top-most and */ + /* bottom-most points */ + + AH_LOG( ( "blue zones computation\n" ) ); + AH_LOG( ( "------------------------------------------------\n" ) ); + + for ( blue = ah_blue_capital_top; blue < ah_blue_max; blue++ ) + { + const char* p = blue_chars[blue]; + const char* limit = p + MAX_TEST_CHARACTERS; + FT_Pos *blue_ref, *blue_shoot; + + + AH_LOG( ( "blue %3d: ", blue ) ); + + num_flats = 0; + num_rounds = 0; + + for ( ; p < limit; p++ ) + { + FT_UInt glyph_index; + FT_Vector* extremum; + FT_Vector* points; + FT_Vector* point_limit; + FT_Vector* point; + FT_Bool round; + + + /* exit if we reach the end of the string */ + if ( !*p ) { + break; + } + + AH_LOG( ( "`%c'", *p ) ); + + /* load the character in the face -- skip unknown or empty ones */ + glyph_index = FT_Get_Char_Index( face, ( FT_UInt ) * p ); + if ( glyph_index == 0 ) { + continue; + } + + error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE ); + if ( error || glyph->outline.n_points <= 0 ) { + continue; + } + + /* now compute min or max point indices and coordinates */ + points = glyph->outline.points; + point_limit = points + glyph->outline.n_points; + point = points; + extremum = point; + point++; + + if ( AH_IS_TOP_BLUE( blue ) ) { + for ( ; point < point_limit; point++ ) + if ( point->y > extremum->y ) { + extremum = point; + } + } else + { + for ( ; point < point_limit; point++ ) + if ( point->y < extremum->y ) { + extremum = point; + } + } + + AH_LOG( ( "%5d", (int)extremum->y ) ); + + /* now, check whether the point belongs to a straight or round */ + /* segment; we first need to find in which contour the extremum */ + /* lies, then see its previous and next points */ + { + FT_Int index = extremum - points; + FT_Int n; + FT_Int first, last, prev, next, end; + FT_Pos dist; + + + last = -1; + first = 0; + + for ( n = 0; n < glyph->outline.n_contours; n++ ) + { + end = glyph->outline.contours[n]; + if ( end >= index ) { + last = end; + break; + } + first = end + 1; + } + + /* XXX: should never happen! */ + if ( last < 0 ) { + continue; + } + + /* now look for the previous and next points that are not on the */ + /* same Y coordinate. Threshold the `closeness'... */ + + prev = index; + next = prev; + + do + { + if ( prev > first ) { + prev--; + } else { + prev = last; + } + + dist = points[prev].y - extremum->y; + if ( dist < -5 || dist > 5 ) { + break; + } + + } while ( prev != index ); + + do + { + if ( next < last ) { + next++; + } else { + next = first; + } + + dist = points[next].y - extremum->y; + if ( dist < -5 || dist > 5 ) { + break; + } + + } while ( next != index ); + + /* now, set the `round' flag depending on the segment's kind */ + round = + FT_CURVE_TAG( glyph->outline.tags[prev] ) != FT_Curve_Tag_On || + FT_CURVE_TAG( glyph->outline.tags[next] ) != FT_Curve_Tag_On ; + + AH_LOG( ( "%c ", round ? 'r' : 'f' ) ); + } + + if ( round ) { + rounds[num_rounds++] = extremum->y; + } else { + flats[num_flats++] = extremum->y; + } + } + + AH_LOG( ( "\n" ) ); + + /* we have computed the contents of the `rounds' and `flats' tables, */ + /* now determine the reference and overshoot position of the blue; */ + /* we simply take the median value after a simple short */ + sort_values( num_rounds, rounds ); + sort_values( num_flats, flats ); + + blue_ref = globals->blue_refs + blue; + blue_shoot = globals->blue_shoots + blue; + if ( num_flats == 0 && num_rounds == 0 ) { + *blue_ref = -10000; + *blue_shoot = -10000; + } else if ( num_flats == 0 ) { + *blue_ref = + *blue_shoot = rounds[num_rounds / 2]; + } else if ( num_rounds == 0 ) { + *blue_ref = + *blue_shoot = flats[num_flats / 2]; + } else + { + *blue_ref = flats[num_flats / 2]; + *blue_shoot = rounds[num_rounds / 2]; + } + + /* there are sometimes problems: if the overshoot position of top */ + /* zones is under its reference position, or the opposite for bottom */ + /* zones. We must thus check everything there and correct the errors */ + if ( *blue_shoot != *blue_ref ) { + FT_Pos ref = *blue_ref; + FT_Pos shoot = *blue_shoot; + FT_Bool over_ref = ( shoot > ref ); + + + if ( AH_IS_TOP_BLUE( blue ) ^ over_ref ) { + *blue_shoot = *blue_ref = ( shoot + ref ) / 2; + } + } + + AH_LOG( ( "-- ref = %ld, shoot = %ld\n", *blue_ref, *blue_shoot ) ); + } + + /* reset original face charmap */ + FT_Set_Charmap( face, charmap ); + error = 0; + +Exit: + return error; +} + + +static +FT_Error ah_hinter_compute_widths( AH_Hinter* hinter ) { + /* scan the array of segments in each direction */ + AH_Outline* outline = hinter->glyph; + AH_Segment* segments; + AH_Segment* limit; + AH_Globals* globals = &hinter->globals->design; + FT_Pos* widths; + FT_Int dimension; + FT_Int* p_num_widths; + FT_Error error = 0; + FT_Pos edge_distance_threshold = 32000; + + + globals->num_widths = 0; + globals->num_heights = 0; + + /* For now, compute the standard width and height from the `o' */ + /* character. I started computing the stem width of the `i' and the */ + /* stem height of the "-", but it wasn't too good. Moreover, we now */ + /* have a single character that gives us standard width and height. */ + { + FT_UInt glyph_index; + + + glyph_index = FT_Get_Char_Index( hinter->face, 'o' ); + if ( glyph_index == 0 ) { + return 0; + } + + error = FT_Load_Glyph( hinter->face, glyph_index, FT_LOAD_NO_SCALE ); + if ( error ) { + goto Exit; + } + + error = ah_outline_load( hinter->glyph, hinter->face ); + if ( error ) { + goto Exit; + } + + ah_outline_compute_segments( hinter->glyph ); + ah_outline_link_segments( hinter->glyph ); + } + + segments = outline->horz_segments; + limit = segments + outline->num_hsegments; + widths = globals->heights; + p_num_widths = &globals->num_heights; + + for ( dimension = 1; dimension >= 0; dimension-- ) + { + AH_Segment* seg = segments; + AH_Segment* link; + FT_Int num_widths = 0; + + + for ( ; seg < limit; seg++ ) + { + link = seg->link; + /* we only consider stem segments there! */ + if ( link && link->link == seg && link > seg ) { + FT_Int dist; + + + dist = seg->pos - link->pos; + if ( dist < 0 ) { + dist = -dist; + } + + if ( num_widths < 12 ) { + widths[num_widths++] = dist; + } + } + } + + sort_values( num_widths, widths ); + *p_num_widths = num_widths; + + /* we will now try to find the smallest width */ + if ( num_widths > 0 && widths[0] < edge_distance_threshold ) { + edge_distance_threshold = widths[0]; + } + + segments = outline->vert_segments; + limit = segments + outline->num_vsegments; + widths = globals->widths; + p_num_widths = &globals->num_widths; + + } + + /* Now, compute the edge distance threshold as a fraction of the */ + /* smallest width in the font. Set it in `hinter.glyph' too! */ + if ( edge_distance_threshold == 32000 ) { + edge_distance_threshold = 50; + } + + /* let's try 20% */ + hinter->glyph->edge_distance_threshold = edge_distance_threshold / 5; + +Exit: + return error; +} + + +LOCAL_FUNC +FT_Error ah_hinter_compute_globals( AH_Hinter* hinter ) { + return ah_hinter_compute_widths( hinter ) || + ah_hinter_compute_blues( hinter ); +} + + +/* END */ diff --git a/src/ft2/ahglobal.h b/src/ft2/ahglobal.h new file mode 100644 index 0000000..db3ca64 --- /dev/null +++ b/src/ft2/ahglobal.h @@ -0,0 +1,42 @@ +/***************************************************************************/ +/* */ +/* ahglobal.h */ +/* */ +/* Routines used to compute global metrics automatically */ +/* (specification). */ +/* */ +/* Copyright 2000 Catharon Productions Inc. */ +/* Author: David Turner */ +/* */ +/* This file is part of the Catharon Typography Project and shall only */ +/* be used, modified, and distributed under the terms of the Catharon */ +/* Open Source License that should come with this file under the name */ +/* `CatharonLicense.txt'. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/* Note that this license is compatible with the FreeType license. */ +/* */ +/***************************************************************************/ + + +#ifndef AHGLOBAL_H +#define AHGLOBAL_H + +#include "ahtypes.h" + +#include "ftobjs.h" + +#define AH_IS_TOP_BLUE( b ) ( ( b ) == ah_blue_capital_top || \ + ( b ) == ah_blue_small_top ) + + +/* compute global metrics automatically */ +LOCAL_DEF +FT_Error ah_hinter_compute_globals( AH_Hinter* hinter ); + + +#endif /* AHGLOBAL_H */ + + +/* END */ diff --git a/src/ft2/ahglyph.c b/src/ft2/ahglyph.c new file mode 100644 index 0000000..5294ac5 --- /dev/null +++ b/src/ft2/ahglyph.c @@ -0,0 +1,1275 @@ +/***************************************************************************/ +/* */ +/* ahglyph.c */ +/* */ +/* Routines used to load and analyze a given glyph before hinting */ +/* (body). */ +/* */ +/* Copyright 2000 Catharon Productions Inc. */ +/* Author: David Turner */ +/* */ +/* This file is part of the Catharon Typography Project and shall only */ +/* be used, modified, and distributed under the terms of the Catharon */ +/* Open Source License that should come with this file under the name */ +/* `CatharonLicense.txt'. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/* Note that this license is compatible with the FreeType license. */ +/* */ +/***************************************************************************/ + + +#include "ahglyph.h" +#include "ahangles.h" +#include "ahglobal.h" + +#include + + +#define xxxAH_DEBUG_GLYPH + + +/* compute the direction value of a given vector.. */ +static +AH_Direction ah_compute_direction( FT_Pos dx, + FT_Pos dy ) { + AH_Direction dir; + FT_Pos ax = ABS( dx ); + FT_Pos ay = ABS( dy ); + + + dir = ah_dir_none; + + /* test for vertical direction */ + if ( ax * 12 < ay ) { + dir = dy > 0 ? ah_dir_up : ah_dir_down; + } + /* test for horizontal direction */ + else if ( ay * 12 < ax ) { + dir = dx > 0 ? ah_dir_right : ah_dir_left; + } + + return dir; +} + + +/*************************************************************************/ +/* */ +/* */ +/* ah_outline_new */ +/* */ +/* */ +/* Creates a new and empty AH_Outline object. */ +/* */ +LOCAL_FUNC +FT_Error ah_outline_new( FT_Memory memory, + AH_Outline** aoutline ) { + FT_Error error; + AH_Outline* outline; + + + if ( !ALLOC( outline, sizeof( *outline ) ) ) { + outline->memory = memory; + *aoutline = outline; + } + + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* ah_outline_done */ +/* */ +/* */ +/* Destroys a given AH_Outline object. */ +/* */ +LOCAL_FUNC +void ah_outline_done( AH_Outline* outline ) { + FT_Memory memory = outline->memory; + + + FREE( outline->horz_edges ); + FREE( outline->horz_segments ); + FREE( outline->contours ); + FREE( outline->points ); + + FREE( outline ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* ah_outline_save */ +/* */ +/* */ +/* Saves the content of a given AH_Outline object into a face's glyph */ +/* slot. */ +/* */ +LOCAL_FUNC +void ah_outline_save( AH_Outline* outline, + AH_Loader* gloader ) { + AH_Point* point = outline->points; + AH_Point* limit = point + outline->num_points; + FT_Vector* vec = gloader->current.outline.points; + char* tag = gloader->current.outline.tags; + + + /* we assume that the glyph loader has already been checked for storage */ + for ( ; point < limit; point++, vec++, tag++ ) + { + vec->x = point->x; + vec->y = point->y; + + if ( point->flags & ah_flah_conic ) { + tag[0] = FT_Curve_Tag_Conic; + } else if ( point->flags & ah_flah_cubic ) { + tag[0] = FT_Curve_Tag_Cubic; + } else { + tag[0] = FT_Curve_Tag_On; + } + } +} + + +/*************************************************************************/ +/* */ +/* */ +/* ah_outline_load */ +/* */ +/* */ +/* Loads an unscaled outline from a glyph slot into an AH_Outline */ +/* object. */ +/* */ +LOCAL_FUNC +FT_Error ah_outline_load( AH_Outline* outline, + FT_Face face ) { + FT_Memory memory = outline->memory; + FT_Error error = FT_Err_Ok; + FT_Outline* source = &face->glyph->outline; + FT_Int num_points = source->n_points; + FT_Int num_contours = source->n_contours; + AH_Point* points; + + + /* check arguments */ + if ( !face || + !face->size || + face->glyph->format != ft_glyph_format_outline ) { + return FT_Err_Invalid_Argument; + } + + /* first of all, reallocate the contours array if necessary */ + if ( num_contours > outline->max_contours ) { + FT_Int new_contours = ( num_contours + 3 ) & - 4; + + + if ( REALLOC_ARRAY( outline->contours, outline->max_contours, + new_contours, AH_Point* ) ) { + goto Exit; + } + + outline->max_contours = new_contours; + } + + /* then, realloc the points, segments & edges arrays if needed */ + if ( num_points > outline->max_points ) { + FT_Int news = ( num_points + 7 ) & - 8; + FT_Int max = outline->max_points; + + + if ( REALLOC_ARRAY( outline->points, max, news, AH_Point ) || + REALLOC_ARRAY( outline->horz_edges, max, news, AH_Edge ) || + REALLOC_ARRAY( outline->horz_segments, max, news, AH_Segment ) ) { + goto Exit; + } + + /* readjust some pointers */ + outline->vert_edges = outline->horz_edges + ( news >> 1 ); + outline->vert_segments = outline->horz_segments + ( news >> 1 ); + outline->max_points = news; + } + + outline->num_points = num_points; + outline->num_contours = num_contours; + + outline->num_hedges = 0; + outline->num_vedges = 0; + outline->num_hsegments = 0; + outline->num_vsegments = 0; + + /* Compute the vertical and horizontal major directions; this is */ + /* currently done by inspecting the `ft_outline_reverse_fill' flag. */ + /* However, some fonts have improper glyphs, and it'd be a good idea */ + /* to be able to re-compute these values on the fly. */ + outline->vert_major_dir = ah_dir_up; + outline->horz_major_dir = ah_dir_left; + + if ( source->flags & ft_outline_reverse_fill ) { + outline->vert_major_dir = ah_dir_down; + outline->horz_major_dir = ah_dir_right; + } + + outline->x_scale = face->size->metrics.x_scale; + outline->y_scale = face->size->metrics.y_scale; + + points = outline->points; + + { + /* do one thing at a time -- it is easier to understand, and */ + /* the code is clearer */ + AH_Point* point = points; + AH_Point* limit = point + outline->num_points; + + + /* compute coordinates */ + { + FT_Vector* vec = source->points; + FT_Fixed x_scale = outline->x_scale; + FT_Fixed y_scale = outline->y_scale; + + + for (; point < limit; vec++, point++ ) + { + point->fx = vec->x; + point->fy = vec->y; + point->ox = point->x = FT_MulFix( vec->x, x_scale ); + point->oy = point->y = FT_MulFix( vec->y, y_scale ); + + point->flags = 0; + } + } + + /* compute Bezier flags */ + { + char* tag = source->tags; + + + for ( point = points; point < limit; point++, tag++ ) + { + switch ( FT_CURVE_TAG( *tag ) ) + { + case FT_Curve_Tag_Conic: + point->flags = ah_flah_conic; break; + case FT_Curve_Tag_Cubic: + point->flags = ah_flah_cubic; break; + default: + ; + } + } + } + + /* compute `next' and `prev' */ + { + FT_Int contour_index; + AH_Point* prev; + AH_Point* first; + AH_Point* end; + + + contour_index = 0; + + first = points; + end = points + source->contours[0]; + prev = end; + + for ( point = points; point < limit; point++ ) + { + point->prev = prev; + if ( point < end ) { + point->next = point + 1; + prev = point; + } else + { + point->next = first; + contour_index++; + if ( point + 1 < limit ) { + end = points + source->contours[contour_index]; + first = point + 1; + prev = end; + } + } + } + } + + /* set-up the contours array */ + { + AH_Point** contour = outline->contours; + AH_Point** limit = contour + outline->num_contours; + short* end = source->contours; + short index = 0; + + + for ( ; contour < limit; contour++, end++ ) + { + contour[0] = points + index; + index = end[0] + 1; + } + } + + /* compute directions of in & out vectors */ + { + for ( point = points; point < limit; point++ ) + { + AH_Point* prev; + AH_Point* next; + FT_Vector vec; + + + prev = point->prev; + vec.x = point->fx - prev->fx; + vec.y = point->fy - prev->fy; + + point->in_dir = ah_compute_direction( vec.x, vec.y ); + +#ifndef AH_OPTION_NO_WEAK_INTERPOLATION + point->in_angle = ah_angle( &vec ); +#endif + + next = point->next; + vec.x = next->fx - point->fx; + vec.y = next->fy - point->fy; + + point->out_dir = ah_compute_direction( vec.x, vec.y ); + +#ifndef AH_OPTION_NO_WEAK_INTERPOLATION + point->out_angle = ah_angle( &vec ); + + { + AH_Angle delta = point->in_angle - point->out_angle; + + + if ( delta < 0 ) { + delta = -delta; + } + if ( delta < 2 ) { + point->flags |= ah_flah_weak_interpolation; + } + } + +#if 0 + if ( point->flags & ( ah_flah_conic | ah_flah_cubic ) ) { + point->flags |= ah_flah_weak_interpolation; + } +#endif + +#endif /* !AH_OPTION_NO_WEAK_INTERPOLATION */ + +#ifdef AH_OPTION_NO_STRONG_INTERPOLATION + point->flags |= ah_flah_weak_interpolation; +#endif + } + } + } + +Exit: + return error; +} + + +LOCAL_FUNC +void ah_setup_uv( AH_Outline* outline, + AH_UV source ) { + AH_Point* point = outline->points; + AH_Point* limit = point + outline->num_points; + + + for ( ; point < limit; point++ ) + { + FT_Pos u, v; + + + switch ( source ) + { + case ah_uv_fxy: + u = point->fx; + v = point->fy; + break; + case ah_uv_fyx: + u = point->fy; + v = point->fx; + break; + case ah_uv_oxy: + u = point->ox; + v = point->oy; + break; + case ah_uv_oyx: + u = point->oy; + v = point->ox; + break; + case ah_uv_yx: + u = point->y; + v = point->x; + break; + case ah_uv_ox: + u = point->x; + v = point->ox; + break; + case ah_uv_oy: + u = point->y; + v = point->oy; + break; + default: + u = point->x; + v = point->y; + break; + } + point->u = u; + point->v = v; + } +} + + +LOCAL_FUNC +void ah_outline_compute_segments( AH_Outline* outline ) { + int dimension; + AH_Segment* segments; + FT_Int* p_num_segments; + AH_Direction segment_dir; + AH_Direction major_dir; + + + segments = outline->horz_segments; + p_num_segments = &outline->num_hsegments; + major_dir = ah_dir_right; /* This value must be positive! */ + segment_dir = major_dir; + + /* set up (u,v) in each point */ + ah_setup_uv( outline, ah_uv_fyx ); + + for ( dimension = 1; dimension >= 0; dimension-- ) + { + AH_Point** contour = outline->contours; + AH_Point** contour_limit = contour + outline->num_contours; + AH_Segment* segment = segments; + FT_Int num_segments = 0; + +#ifdef AH_HINT_METRICS + AH_Point* min_point = 0; + AH_Point* max_point = 0; + FT_Pos min_coord = 32000; + FT_Pos max_coord = -32000; +#endif + + + /* do each contour separately */ + for ( ; contour < contour_limit; contour++ ) + { + AH_Point* point = contour[0]; + AH_Point* last = point->prev; + int on_edge = 0; + FT_Pos min_pos = +32000; /* minimum segment pos != min_coord */ + FT_Pos max_pos = -32000; /* maximum segment pos != max_coord */ + FT_Bool passed; + + +#ifdef AH_HINT_METRICS + if ( point->u < min_coord ) { + min_coord = point->u; + min_point = point; + } + if ( point->u > max_coord ) { + max_coord = point->u; + max_point = point; + } +#endif + + if ( point == last ) { /* skip singletons -- just in case? */ + continue; + } + + if ( ABS( last->out_dir ) == major_dir && + ABS( point->out_dir ) == major_dir ) { + /* we are already on an edge, try to locate its start */ + last = point; + + for (;; ) + { + point = point->prev; + if ( ABS( point->out_dir ) != major_dir ) { + point = point->next; + break; + } + if ( point == last ) { + break; + } + } + + } + + last = point; + passed = 0; + + for (;; ) + { + FT_Pos u, v; + + + if ( on_edge ) { + u = point->u; + if ( u < min_pos ) { + min_pos = u; + } + if ( u > max_pos ) { + max_pos = u; + } + + if ( point->out_dir != segment_dir || point == last ) { + /* we are just leaving an edge; record a new segment! */ + segment->last = point; + segment->pos = ( min_pos + max_pos ) >> 1; + + /* a segment is round if either its first or last point */ + /* is a control point */ + if ( ( segment->first->flags | point->flags ) & + ah_flah_control ) { + segment->flags |= ah_edge_round; + } + + /* compute segment size */ + min_pos = max_pos = point->v; + + v = segment->first->v; + if ( v < min_pos ) { + min_pos = v; + } + if ( v > max_pos ) { + max_pos = v; + } + + segment->min_coord = min_pos; + segment->max_coord = max_pos; + + on_edge = 0; + num_segments++; + segment++; + /* fallthrough */ + } + } + + /* now exit if we are at the start/end point */ + if ( point == last ) { + if ( passed ) { + break; + } + passed = 1; + } + + if ( !on_edge && ABS( point->out_dir ) == major_dir ) { + /* this is the start of a new segment! */ + segment_dir = point->out_dir; + + /* clear all segment fields */ + memset( segment, 0, sizeof( *segment ) ); + + segment->dir = segment_dir; + segment->flags = ah_edge_normal; + min_pos = max_pos = point->u; + segment->first = point; + segment->last = point; + segment->contour = contour; + on_edge = 1; + + if ( point == max_point ) { + max_point = 0; + } + + if ( point == min_point ) { + min_point = 0; + } + } + + point = point->next; + } + + } /* contours */ + +#ifdef AH_HINT_METRICS + /* we need to ensure that there are edges on the left-most and */ + /* right-most points of the glyph in order to hint the metrics; */ + /* we do this by inserting fake segments when needed */ + if ( dimension == 0 ) { + AH_Point* point = outline->points; + AH_Point* limit = point + outline->num_points; + + AH_Point* min_point = 0; + AH_Point* max_point = 0; + FT_Pos min_pos = 32000; + FT_Pos max_pos = -32000; + + + /* compute minimum and maximum points */ + for ( ; point < limit; point++ ) + { + FT_Pos x = point->fx; + + + if ( x < min_pos ) { + min_pos = x; + min_point = point; + } + if ( x > max_pos ) { + max_pos = x; + max_point = point; + } + } + + /* insert minimum segment */ + if ( min_point ) { + /* clear all segment fields */ + memset( segment, 0, sizeof( *segment ) ); + + segment->dir = segment_dir; + segment->flags = ah_edge_normal; + segment->first = min_point; + segment->last = min_point; + segment->pos = min_pos; + + num_segments++; + segment++; + } + + /* insert maximum segment */ + if ( max_point ) { + /* clear all segment fields */ + memset( segment, 0, sizeof( *segment ) ); + + segment->dir = segment_dir; + segment->flags = ah_edge_normal; + segment->first = max_point; + segment->last = max_point; + segment->pos = max_pos; + + num_segments++; + segment++; + } + } +#endif /* AH_HINT_METRICS */ + + *p_num_segments = num_segments; + + segments = outline->vert_segments; + major_dir = ah_dir_up; + p_num_segments = &outline->num_vsegments; + ah_setup_uv( outline, ah_uv_fxy ); + } +} + + +LOCAL_FUNC +void ah_outline_link_segments( AH_Outline* outline ) { + AH_Segment* segments; + AH_Segment* limit; + int dimension; + + + ah_setup_uv( outline, ah_uv_fyx ); + + segments = outline->horz_segments; + limit = segments + outline->num_hsegments; + + for ( dimension = 1; dimension >= 0; dimension-- ) + { + AH_Segment* seg1; + AH_Segment* seg2; + + + /* now compare each segment to the others */ + for ( seg1 = segments; seg1 < limit; seg1++ ) + { + FT_Pos best_score = 32000; + AH_Segment* best_segment = 0; + + + /* the fake segments are introduced to hint the metrics -- */ + /* we must never link them to anything */ + if ( seg1->first == seg1->last ) { + continue; + } + + for ( seg2 = segments; seg2 < limit; seg2++ ) + if ( seg1 != seg2 && seg1->dir + seg2->dir == 0 ) { + FT_Pos pos1 = seg1->pos; + FT_Pos pos2 = seg2->pos; + FT_Bool is_dir; + FT_Bool is_pos; + + + /* check that the segments are correctly oriented and */ + /* positioned to form a black distance */ + + is_dir = ( seg1->dir == outline->horz_major_dir || + seg1->dir == outline->vert_major_dir ); + is_pos = pos1 > pos2; + + if ( pos1 == pos2 || !( is_dir ^ is_pos ) ) { + continue; + } + + /* Check the two segments. We now have a better algorithm */ + /* that doesn't rely on the segment points themselves but */ + /* on their relative position. This gets rids of many */ + /* unpleasant artefacts and incorrect stem/serifs */ + /* computations. */ + + /* first of all, compute the size of the `common' height */ + { + FT_Pos min = seg1->min_coord; + FT_Pos max = seg1->max_coord; + FT_Pos len, score; + FT_Pos size1, size2; + + + size1 = max - min; + size2 = seg2->max_coord - seg2->min_coord; + + if ( min < seg2->min_coord ) { + min = seg2->min_coord; + } + + if ( max < seg2->max_coord ) { + max = seg2->max_coord; + } + + len = max - min; + score = seg2->pos - seg1->pos; + if ( score < 0 ) { + score = -score; + } + + /* before comparing the scores, take care that the segments */ + /* are really facing each other (often not for italics..) */ + if ( 4 * len >= size1 && 4 * len >= size2 ) { + if ( score < best_score ) { + best_score = score; + best_segment = seg2; + } + } + } + } + + if ( best_segment ) { + seg1->link = best_segment; + seg1->score = best_score; + + best_segment->num_linked++; + } + + + } /* edges 1 */ + + /* now, compute the `serif' segments */ + for ( seg1 = segments; seg1 < limit; seg1++ ) + { + seg2 = seg1->link; + + if ( seg2 && seg2->link != seg1 ) { + seg1->link = 0; + seg1->serif = seg2->link; + } + } + + ah_setup_uv( outline, ah_uv_fxy ); + + segments = outline->vert_segments; + limit = segments + outline->num_vsegments; + } +} + + +#ifdef AH_DEBUG_GLYPH + +/* A function used to dump the array of linked segments */ +void ah_dump_segments( AH_Outline* outline ) { + AH_Segment* segments; + AH_Segment* limit; + AH_Point* points; + FT_Int dimension; + + + points = outline->points; + segments = outline->horz_segments; + limit = segments + outline->num_hsegments; + + for ( dimension = 1; dimension >= 0; dimension-- ) + { + AH_Segment* seg; + + + printf( "Table of %s segments:\n", + !dimension ? "vertical" : "horizontal" ); + printf( " [ index | pos | dir | link | serif |" + " numl | first | start ]\n" ); + + for ( seg = segments; seg < limit; seg++ ) + { + printf( " [ %5d | %4d | %5s | %4d | %5d | %4d | %5d | %5d ]\n", + seg - segments, + (int)seg->pos, + seg->dir == ah_dir_up + ? "up" + : ( seg->dir == ah_dir_down + ? "down" + : ( seg->dir == ah_dir_left + ? "left" + : ( seg->dir == ah_dir_right + ? "right" + : "none" ) ) ), + seg->link ? ( seg->link - segments ) : -1, + seg->serif ? ( seg->serif - segments ) : -1, + (int)seg->num_linked, + seg->first - points, + seg->last - points ); + } + + segments = outline->vert_segments; + limit = segments + outline->num_vsegments; + } +} + +#endif /* AH_DEBUG_GLYPH */ + + +static +void ah_outline_compute_edges( AH_Outline* outline ) { + AH_Edge* edges; + AH_Segment* segments; + AH_Segment* segment_limit; + AH_Direction up_dir; + FT_Int* p_num_edges; + FT_Int dimension; + FT_Fixed scale; + FT_Pos edge_distance_threshold; + + + edges = outline->horz_edges; + segments = outline->horz_segments; + segment_limit = segments + outline->num_hsegments; + p_num_edges = &outline->num_hedges; + up_dir = ah_dir_right; + scale = outline->y_scale; + + for ( dimension = 1; dimension >= 0; dimension-- ) + { + AH_Edge* edge; + AH_Edge* edge_limit; /* really == edge + num_edges */ + AH_Segment* seg; + + + /*********************************************************************/ + /* */ + /* We will begin by generating a sorted table of edges for the */ + /* current direction. To do so, we simply scan each segment and try */ + /* to find an edge in our table that corresponds to its position. */ + /* */ + /* If no edge is found, we create and insert a new edge in the */ + /* sorted table. Otherwise, we simply add the segment to the edge's */ + /* list which will be processed in the second step to compute the */ + /* edge's properties. */ + /* */ + /* Note that the edges table is sorted along the segment/edge */ + /* position. */ + /* */ + /*********************************************************************/ + + edge_distance_threshold = FT_MulFix( outline->edge_distance_threshold, + scale ); + if ( edge_distance_threshold > 64 / 4 ) { + edge_distance_threshold = 64 / 4; + } + + edge_limit = edges; + for ( seg = segments; seg < segment_limit; seg++ ) + { + AH_Edge* found = 0; + + + /* look for an edge corresponding to the segment */ + for ( edge = edges; edge < edge_limit; edge++ ) + { + FT_Pos dist; + + + dist = seg->pos - edge->fpos; + if ( dist < 0 ) { + dist = -dist; + } + + dist = FT_MulFix( dist, scale ); + if ( dist < edge_distance_threshold ) { + found = edge; + break; + } + } + + if ( !found ) { + /* insert a new edge in the list and */ + /* sort according to the position */ + while ( edge > edges && edge[-1].fpos > seg->pos ) + { + edge[0] = edge[-1]; + edge--; + } + edge_limit++; + + /* clear all edge fields */ + memset( edge, 0, sizeof( *edge ) ); + + /* add the segment to the new edge's list */ + edge->first = seg; + edge->last = seg; + edge->fpos = seg->pos; + edge->opos = edge->pos = FT_MulFix( seg->pos, scale ); + seg->edge_next = seg; + } else + { + /* if an edge was found, simply add the segment to the edge's */ + /* list */ + seg->edge_next = edge->first; + edge->last->edge_next = seg; + edge->last = seg; + } + } + + *p_num_edges = edge_limit - edges; + + + /*********************************************************************/ + /* */ + /* Good, we will now compute each edge's properties according to */ + /* segments found on its position. Basically, these are: */ + /* */ + /* - edge's main direction */ + /* - stem edge, serif edge or both (which defaults to stem then) */ + /* - rounded edge, straigth or both (which defaults to straight) */ + /* - link for edge */ + /* */ + /*********************************************************************/ + + /* first of all, set the `edge' field in each segment -- this is */ + /* required in order to compute edge links */ + for ( edge = edges; edge < edge_limit; edge++ ) + { + seg = edge->first; + if ( seg ) { + do + { + seg->edge = edge; + seg = seg->edge_next; + } + while ( seg != edge->first ); + } + } + + /* now, compute each edge properties */ + for ( edge = edges; edge < edge_limit; edge++ ) + { + int is_round = 0; /* does it contain round segments? */ + int is_straight = 0; /* does it contain straight segments? */ + int ups = 0; /* number of upwards segments */ + int downs = 0; /* number of downwards segments */ + + + seg = edge->first; + + do + { + FT_Bool is_serif; + + + /* check for roundness of segment */ + if ( seg->flags & ah_edge_round ) { + is_round++; + } else { + is_straight++; + } + + /* check for segment direction */ + if ( seg->dir == up_dir ) { + ups += seg->max_coord - seg->min_coord; + } else { + downs += seg->max_coord - seg->min_coord; + } + + /* check for links -- if seg->serif is set, then seg->link must */ + /* be ignored */ + is_serif = seg->serif && seg->serif->edge != edge; + + if ( seg->link || is_serif ) { + AH_Edge* edge2; + AH_Segment* seg2; + + + edge2 = edge->link; + seg2 = seg->link; + + if ( is_serif ) { + seg2 = seg->serif; + edge2 = edge->serif; + } + + if ( edge2 ) { + FT_Pos edge_delta; + FT_Pos seg_delta; + + + edge_delta = edge->fpos - edge2->fpos; + if ( edge_delta < 0 ) { + edge_delta = -edge_delta; + } + + seg_delta = seg->pos - seg2->pos; + if ( seg_delta < 0 ) { + seg_delta = -seg_delta; + } + + if ( seg_delta < edge_delta ) { + edge2 = seg2->edge; + } + } else { + edge2 = seg2->edge; + } + + if ( is_serif ) { + edge->serif = edge2; + } else { + edge->link = edge2; + } + } + + seg = seg->edge_next; + + } while ( seg != edge->first ); + + /* set the round/straight flags */ + edge->flags = ah_edge_normal; + + if ( is_straight == 0 && is_round ) { + edge->flags |= ah_edge_round; + } + + /* set the edge's main direction */ + edge->dir = ah_dir_none; + + if ( ups > downs ) { + edge->dir = up_dir; + } else if ( ups < downs ) { + edge->dir = -up_dir; + } else if ( ups == downs ) { + edge->dir = 0; /* both up and down !! */ + + } + /* gets rid of serifs if link is set */ + /* XXX: This gets rid of many unpleasant artefacts! */ + /* Example: the `c' in cour.pfa at size 13 */ + + if ( edge->serif && edge->link ) { + edge->serif = 0; + } + } + + edges = outline->vert_edges; + segments = outline->vert_segments; + segment_limit = segments + outline->num_vsegments; + p_num_edges = &outline->num_vedges; + up_dir = ah_dir_up; + scale = outline->x_scale; + } +} + + +/*************************************************************************/ +/* */ +/* */ +/* ah_outline_detect_features */ +/* */ +/* */ +/* Performs feature detection on a given AH_Outline object. */ +/* */ +LOCAL_FUNC +void ah_outline_detect_features( AH_Outline* outline ) { + ah_outline_compute_segments( outline ); + ah_outline_link_segments( outline ); + ah_outline_compute_edges( outline ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* ah_outline_compute_blue_edges */ +/* */ +/* */ +/* Computes the `blue edges' in a given outline (i.e. those that must */ +/* be snapped to a blue zone edge (top or bottom). */ +/* */ +LOCAL_FUNC +void ah_outline_compute_blue_edges( AH_Outline* outline, + AH_Face_Globals* face_globals ) { + AH_Edge* edge = outline->horz_edges; + AH_Edge* limit = edge + outline->num_hedges; + AH_Globals* globals = &face_globals->design; + FT_Fixed y_scale = outline->y_scale; + + + /* compute for each horizontal edge, which blue zone is closer */ + for ( ; edge < limit; edge++ ) + { + AH_Blue blue; + FT_Pos* best_blue = 0; + FT_Pos best_dist; /* initial threshold */ + + + /* compute the initial threshold as a fraction of the EM size */ + best_dist = FT_MulFix( face_globals->face->units_per_EM / 40, y_scale ); + if ( best_dist > 64 / 4 ) { + best_dist = 64 / 4; + } + + for ( blue = ah_blue_capital_top; blue < ah_blue_max; blue++ ) + { + /* if it is a top zone, check for right edges -- if it is a bottom */ + /* zone, check for left edges */ + /* */ + /* of course, that's for TrueType XXX */ + FT_Bool is_top_blue = AH_IS_TOP_BLUE( blue ); + FT_Bool is_major_dir = edge->dir == outline->horz_major_dir; + + + /* if it is a top zone, the edge must be against the major */ + /* direction; if it is a bottom zone, it must be in the major */ + /* direction */ + if ( is_top_blue ^ is_major_dir ) { + FT_Pos dist; + FT_Pos* blue_pos = globals->blue_refs + blue; + + + /* first of all, compare it to the reference position */ + dist = edge->fpos - *blue_pos; + if ( dist < 0 ) { + dist = -dist; + } + + dist = FT_MulFix( dist, y_scale ); + if ( dist < best_dist ) { + best_dist = dist; + best_blue = blue_pos; + } + + /* now, compare it to the overshoot position if the edge is */ + /* rounded, and if the edge is over the reference position of a */ + /* top zone, or under the reference position of a bottom zone */ + if ( edge->flags & ah_edge_round && dist != 0 ) { + FT_Bool is_under_ref = edge->fpos < *blue_pos; + + + if ( is_top_blue ^ is_under_ref ) { + blue_pos = globals->blue_shoots + blue; + dist = edge->fpos - *blue_pos; + if ( dist < 0 ) { + dist = -dist; + } + + dist = FT_MulFix( dist, y_scale ); + if ( dist < best_dist ) { + best_dist = dist; + best_blue = blue_pos; + } + } + } + } + } + + if ( best_blue ) { + edge->blue_edge = best_blue; + } + } +} + + +/*************************************************************************/ +/* */ +/* */ +/* ah_outline_scale_blue_edges */ +/* */ +/* */ +/* This functions must be called before hinting in order to re-adjust */ +/* the contents of the detected edges (basically change the `blue */ +/* edge' pointer from `design units' to `scaled ones'). */ +/* */ +LOCAL_FUNC +void ah_outline_scale_blue_edges( AH_Outline* outline, + AH_Face_Globals* globals ) { + AH_Edge* edge = outline->horz_edges; + AH_Edge* limit = edge + outline->num_hedges; + FT_Int delta; + + + delta = globals->scaled.blue_refs - globals->design.blue_refs; + + for ( ; edge < limit; edge++ ) + { + if ( edge->blue_edge ) { + edge->blue_edge += delta; + } + } +} + + +#ifdef AH_DEBUG_GLYPH + +void ah_dump_edges( AH_Outline* outline ) { + AH_Edge* edges; + AH_Edge* limit; + AH_Segment* segments; + FT_Int dimension; + + + edges = outline->horz_edges; + limit = edges + outline->num_hedges; + segments = outline->horz_segments; + + for ( dimension = 1; dimension >= 0; dimension-- ) + { + AH_Edge* edge; + + + printf( "Table of %s edges:\n", + !dimension ? "vertical" : "horizontal" ); + printf( " [ index | pos | dir | link |" + " serif | blue | opos | pos ]\n" ); + + for ( edge = edges; edge < limit; edge++ ) + { + printf( " [ %5d | %4d | %5s | %4d | %5d | %c | %5.2f | %5.2f ]\n", + edge - edges, + (int)edge->fpos, + edge->dir == ah_dir_up + ? "up" + : ( edge->dir == ah_dir_down + ? "down" + : ( edge->dir == ah_dir_left + ? "left" + : ( edge->dir == ah_dir_right + ? "right" + : "none" ) ) ), + edge->link ? ( edge->link - edges ) : -1, + edge->serif ? ( edge->serif - edges ) : -1, + edge->blue_edge ? 'y' : 'n', + edge->opos / 64.0, + edge->pos / 64.0 ); + } + + edges = outline->vert_edges; + limit = edges + outline->num_vedges; + segments = outline->vert_segments; + } +} + +#endif /* AH_DEBUG_GLYPH */ + + +/* END */ diff --git a/src/ft2/ahglyph.h b/src/ft2/ahglyph.h new file mode 100644 index 0000000..0e50e10 --- /dev/null +++ b/src/ft2/ahglyph.h @@ -0,0 +1,86 @@ +/***************************************************************************/ +/* */ +/* ahglyph.h */ +/* */ +/* Routines used to load and analyze a given glyph before hinting */ +/* (specification). */ +/* */ +/* Copyright 2000 Catharon Productions Inc. */ +/* Author: David Turner */ +/* */ +/* This file is part of the Catharon Typography Project and shall only */ +/* be used, modified, and distributed under the terms of the Catharon */ +/* Open Source License that should come with this file under the name */ +/* `CatharonLicense.txt'. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/* Note that this license is compatible with the FreeType license. */ +/* */ +/***************************************************************************/ + + +#ifndef AHGLYPH_H +#define AHGLYPH_H + + +#include "ahtypes.h" + + +typedef enum AH_UV_ +{ + ah_uv_fxy, + ah_uv_fyx, + ah_uv_oxy, + ah_uv_oyx, + ah_uv_ox, + ah_uv_oy, + ah_uv_yx, + ah_uv_xy /* should always be last! */ + +} AH_UV; + + +LOCAL_DEF +void ah_setup_uv( AH_Outline* outline, + AH_UV source ); + + +/* AH_Outline functions - they should be typically called in this order */ + +LOCAL_DEF +FT_Error ah_outline_new( FT_Memory memory, + AH_Outline** aoutline ); + +LOCAL_DEF +FT_Error ah_outline_load( AH_Outline* outline, + FT_Face face ); + +LOCAL_DEF +void ah_outline_compute_segments( AH_Outline* outline ); + +LOCAL_DEF +void ah_outline_link_segments( AH_Outline* outline ); + +LOCAL_DEF +void ah_outline_detect_features( AH_Outline* outline ); + +LOCAL_DEF +void ah_outline_compute_blue_edges( AH_Outline* outline, + AH_Face_Globals* globals ); + +LOCAL_DEF +void ah_outline_scale_blue_edges( AH_Outline* outline, + AH_Face_Globals* globals ); + +LOCAL_DEF +void ah_outline_save( AH_Outline* outline, AH_Loader* loader ); + +LOCAL_DEF +void ah_outline_done( AH_Outline* outline ); + + +#endif /* AHGLYPH_H */ + + +/* END */ diff --git a/src/ft2/ahhint.c b/src/ft2/ahhint.c new file mode 100644 index 0000000..5cf8f08 --- /dev/null +++ b/src/ft2/ahhint.c @@ -0,0 +1,1362 @@ +/***************************************************************************/ +/* */ +/* ahhint.c */ +/* */ +/* Glyph hinter (body). */ +/* */ +/* Copyright 2000 Catharon Productions Inc. */ +/* Author: David Turner */ +/* */ +/* This file is part of the Catharon Typography Project and shall only */ +/* be used, modified, and distributed under the terms of the Catharon */ +/* Open Source License that should come with this file under the name */ +/* `CatharonLicense.txt'. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/* Note that this license is compatible with the FreeType license. */ +/* */ +/***************************************************************************/ + + +#include "ahhint.h" +#include "ahglyph.h" +#include "ahangles.h" + +#include "ftoutln.h" + + +#define FACE_GLOBALS( face ) ( (AH_Face_Globals*)( face )->autohint.data ) + +#define AH_USE_IUP + + +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** Hinting routines ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ + + +static int disable_horz_edges = 0; +static int disable_vert_edges = 0; + + +/* snap a given width in scaled coordinates to one of the */ +/* current standard widths */ +static +FT_Pos ah_snap_width( FT_Pos* widths, + FT_Int count, + FT_Pos width ) { + int n; + FT_Pos best = 64 + 32 + 2; + FT_Pos reference = width; + + + for ( n = 0; n < count; n++ ) + { + FT_Pos w; + FT_Pos dist; + + + w = widths[n]; + dist = width - w; + if ( dist < 0 ) { + dist = -dist; + } + if ( dist < best ) { + best = dist; + reference = w; + } + } + + if ( width >= reference ) { + width -= 0x21; + if ( width < reference ) { + width = reference; + } + } else + { + width += 0x21; + if ( width > reference ) { + width = reference; + } + } + + return width; +} + + +/* align one stem edge relative to the previous stem edge */ +static +void ah_align_linked_edge( AH_Hinter* hinter, + AH_Edge* base_edge, + AH_Edge* stem_edge, + int vertical ) { + FT_Pos dist = stem_edge->opos - base_edge->opos; + AH_Globals* globals = &hinter->globals->scaled; + FT_Pos sign = 1; + + + if ( dist < 0 ) { + dist = -dist; + sign = -1; + } + + if ( vertical ) { + dist = ah_snap_width( globals->heights, globals->num_heights, dist ); + + /* in the case of vertical hinting, always round */ + /* the stem heights to integer pixels */ + if ( dist >= 64 ) { + dist = ( dist + 16 ) & - 64; + } else { + dist = 64; + } + } else + { + dist = ah_snap_width( globals->widths, globals->num_widths, dist ); + + if ( hinter->flags & ah_hinter_monochrome ) { + /* monochrome horizontal hinting: snap widths to integer pixels */ + /* with a different threshold */ + if ( dist < 64 ) { + dist = 64; + } else { + dist = ( dist + 32 ) & - 64; + } + } else + { + /* for horizontal anti-aliased hinting, we adopt a more subtle */ + /* approach: we strengthen small stems, round stems whose size */ + /* is between 1 and 2 pixels to an integer, otherwise nothing */ + if ( dist < 48 ) { + dist = ( dist + 64 ) >> 1; + } else if ( dist < 128 ) { + dist = ( dist + 42 ) & - 64; + } + } + } + + stem_edge->pos = base_edge->pos + sign * dist; +} + + +static +void ah_align_serif_edge( AH_Hinter* hinter, + AH_Edge* base, + AH_Edge* serif ) { + FT_Pos dist; + FT_Pos sign = 1; + + UNUSED( hinter ); + + + dist = serif->opos - base->opos; + if ( dist < 0 ) { + dist = -dist; + sign = -1; + } + + /* do not strengthen serifs */ + if ( base->flags & ah_edge_done ) { + if ( dist > 64 ) { + dist = ( dist + 16 ) & - 64; + } else if ( dist <= 32 ) { + dist = ( dist + 33 ) >> 1; + } + } + + serif->pos = base->pos + sign * dist; +} + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** E D G E H I N T I N G ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +/* Another alternative edge hinting algorithm */ +static +void ah_hint_edges_3( AH_Hinter* hinter ) { + AH_Edge* edges; + AH_Edge* edge_limit; + AH_Outline* outline = hinter->glyph; + FT_Int dimension; + + + edges = outline->horz_edges; + edge_limit = edges + outline->num_hedges; + + for ( dimension = 1; dimension >= 0; dimension-- ) + { + AH_Edge* edge; + AH_Edge* before = 0; + AH_Edge* after = 0; + AH_Edge* anchor = 0; + int has_serifs = 0; + + + if ( disable_vert_edges && !dimension ) { + goto Next_Dimension; + } + + if ( disable_horz_edges && dimension ) { + goto Next_Dimension; + } + + /* we begin by aligning all stems relative to the blue zone */ + /* if needed -- that's only for horizontal edges */ + if ( dimension ) { + for ( edge = edges; edge < edge_limit; edge++ ) + { + FT_Pos* blue; + AH_Edge *edge1, *edge2; + + + if ( edge->flags & ah_edge_done ) { + continue; + } + + blue = edge->blue_edge; + edge1 = 0; + edge2 = edge->link; + + if ( blue ) { + edge1 = edge; + } else if ( edge2 && edge2->blue_edge ) { + blue = edge2->blue_edge; + edge1 = edge2; + edge2 = edge; + } + + if ( !edge1 ) { + continue; + } + + edge1->pos = blue[0]; + edge1->flags |= ah_edge_done; + + if ( edge2 && !edge2->blue_edge ) { + ah_align_linked_edge( hinter, edge1, edge2, dimension ); + edge2->flags |= ah_edge_done; + } + + if ( !anchor ) { + anchor = edge; + } + } + } + + /* now, we will align all stem edges, trying to maintain the */ + /* relative order of stems in the glyph.. */ + before = 0; + after = 0; + for ( edge = edges; edge < edge_limit; edge++ ) + { + AH_Edge *edge2; + + + if ( edge->flags & ah_edge_done ) { + continue; + } + + /* skip all non-stem edges */ + edge2 = edge->link; + if ( !edge2 ) { + has_serifs++; + continue; + } + + /* now, align the stem */ + + /* this should not happen, but it's better to be safe.. */ + if ( edge2->blue_edge || edge2 < edge ) { + +#if 0 + printf( "strange blue alignement, edge %d to %d\n", + edge - edges, edge2 - edges ); +#endif + + ah_align_linked_edge( hinter, edge2, edge, dimension ); + edge->flags |= ah_edge_done; + continue; + } + + { + FT_Bool min = 0; + FT_Pos delta; + + if ( !anchor ) { + edge->pos = ( edge->opos + 32 ) & - 64; + anchor = edge; + } else { + edge->pos = anchor->pos + + ( ( edge->opos - anchor->opos + 32 ) & - 64 ); + } + + edge->flags |= ah_edge_done; + + if ( edge > edges && edge->pos < edge[-1].pos ) { + edge->pos = edge[-1].pos; + min = 1; + } + + ah_align_linked_edge( hinter, edge, edge2, dimension ); + delta = 0; + if ( edge2 + 1 < edge_limit && + edge2[1].flags & ah_edge_done ) { + delta = edge2[1].pos - edge2->pos; + } + + if ( delta < 0 ) { + edge2->pos += delta; + if ( !min ) { + edge->pos += delta; + } + } + edge2->flags |= ah_edge_done; + } + } + + if ( !has_serifs ) { + goto Next_Dimension; + } + + /* now, hint the remaining edges (serifs and single) in order */ + /* to complete our processing */ + for ( edge = edges; edge < edge_limit; edge++ ) + { + if ( edge->flags & ah_edge_done ) { + continue; + } + + if ( edge->serif ) { + ah_align_serif_edge( hinter, edge->serif, edge ); + } else if ( !anchor ) { + edge->pos = ( edge->opos + 32 ) & - 64; + anchor = edge; + } else { + edge->pos = anchor->pos + + ( ( edge->opos - anchor->opos + 32 ) & - 64 ); + } + + edge->flags |= ah_edge_done; + + if ( edge > edges && edge->pos < edge[-1].pos ) { + edge->pos = edge[-1].pos; + } + + if ( edge + 1 < edge_limit && + edge[1].flags & ah_edge_done && + edge->pos > edge[1].pos ) { + edge->pos = edge[1].pos; + } + } + +Next_Dimension: + edges = outline->vert_edges; + edge_limit = edges + outline->num_vedges; + } +} + + +LOCAL_FUNC +void ah_hinter_hint_edges( AH_Hinter* hinter, + int no_horz_edges, + int no_vert_edges ) { + disable_horz_edges = no_horz_edges; + disable_vert_edges = no_vert_edges; + + /* AH_Interpolate_Blue_Edges( hinter ); -- doesn't seem to help */ + /* reduce the problem of the disappearing eye in the `e' of Times... */ + /* also, creates some artifacts near the blue zones? */ + { + ah_hint_edges_3( hinter ); + +#if 0 + /* outline optimizer removed temporarily */ + if ( hinter->flags & ah_hinter_optimize ) { + AH_Optimizer opt; + + + if ( !AH_Optimizer_Init( &opt, hinter->glyph, hinter->memory ) ) { + AH_Optimizer_Compute( &opt ); + AH_Optimizer_Done( &opt ); + } + } +#endif + + } +} + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** P O I N T H I N T I N G ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + +static +void ah_hinter_align_edge_points( AH_Hinter* hinter ) { + AH_Outline* outline = hinter->glyph; + AH_Edge* edges; + AH_Edge* edge_limit; + FT_Int dimension; + + + edges = outline->horz_edges; + edge_limit = edges + outline->num_hedges; + + for ( dimension = 1; dimension >= 0; dimension-- ) + { + AH_Edge* edge; + AH_Edge* before; + AH_Edge* after; + + + before = 0; + after = 0; + + edge = edges; + for ( ; edge < edge_limit; edge++ ) + { + /* move the points of each segment */ + /* in each edge to the edge's position */ + AH_Segment* seg = edge->first; + + + do + { + AH_Point* point = seg->first; + + + for (;; ) + { + if ( dimension ) { + point->y = edge->pos; + point->flags |= ah_flah_touch_y; + } else + { + point->x = edge->pos; + point->flags |= ah_flah_touch_x; + } + + if ( point == seg->last ) { + break; + } + + point = point->next; + } + + seg = seg->edge_next; + + } while ( seg != edge->first ); + } + + edges = outline->vert_edges; + edge_limit = edges + outline->num_vedges; + } +} + + +/* hint the strong points -- this is equivalent to the TrueType `IP' */ +static +void ah_hinter_align_strong_points( AH_Hinter* hinter ) { + AH_Outline* outline = hinter->glyph; + FT_Int dimension; + AH_Edge* edges; + AH_Edge* edge_limit; + AH_Point* points; + AH_Point* point_limit; + AH_Flags touch_flag; + + + points = outline->points; + point_limit = points + outline->num_points; + + edges = outline->horz_edges; + edge_limit = edges + outline->num_hedges; + touch_flag = ah_flah_touch_y; + + for ( dimension = 1; dimension >= 0; dimension-- ) + { + AH_Point* point; + AH_Edge* edge; + AH_Edge* before; + AH_Edge* after; + + + before = 0; + after = 0; + + if ( edges < edge_limit ) { + for ( point = points; point < point_limit; point++ ) + { + FT_Pos u, ou, fu; /* point position */ + FT_Pos delta; + + + if ( point->flags & touch_flag ) { + continue; + } + +#ifndef AH_OPTION_NO_WEAK_INTERPOLATION + /* if this point is candidate to weak interpolation, we will */ + /* interpolate it after all strong points have been processed */ + if ( point->flags & ah_flah_weak_interpolation ) { + continue; + } +#endif + + if ( dimension ) { + u = point->fy; + ou = point->oy; + } else + { + u = point->fx; + ou = point->ox; + } + + fu = u; + + /* is the point before the first edge? */ + edge = edges; + delta = edge->fpos - u; + if ( delta >= 0 ) { + u = edge->pos - ( edge->opos - ou ); + goto Store_Point; + } + + /* is the point after the last edge ? */ + edge = edge_limit - 1; + delta = u - edge->fpos; + if ( delta >= 0 ) { + u = edge->pos + ( ou - edge->opos ); + goto Store_Point; + } + + /* otherwise, interpolate the point in between */ + { + AH_Edge* before = 0; + AH_Edge* after = 0; + + + for ( edge = edges; edge < edge_limit; edge++ ) + { + if ( u == edge->fpos ) { + u = edge->pos; + goto Store_Point; + } + if ( u < edge->fpos ) { + break; + } + before = edge; + } + + for ( edge = edge_limit - 1; edge >= edges; edge-- ) + { + if ( u == edge->fpos ) { + u = edge->pos; + goto Store_Point; + } + if ( u > edge->fpos ) { + break; + } + after = edge; + } + + /* assert( before && after && before != after ) */ + u = before->pos + FT_MulDiv( fu - before->fpos, + after->pos - before->pos, + after->fpos - before->fpos ); + } + +Store_Point: + + /* save the point position */ + if ( dimension ) { + point->y = u; + } else { + point->x = u; + } + + point->flags |= touch_flag; + } + } + + edges = outline->vert_edges; + edge_limit = edges + outline->num_vedges; + touch_flag = ah_flah_touch_x; + } +} + + +#ifndef AH_OPTION_NO_WEAK_INTERPOLATION + +static +void ah_iup_shift( AH_Point* p1, + AH_Point* p2, + AH_Point* ref ) { + AH_Point* p; + FT_Pos delta = ref->u - ref->v; + + + for ( p = p1; p < ref; p++ ) + p->u = p->v + delta; + + for ( p = ref + 1; p <= p2; p++ ) + p->u = p->v + delta; +} + + +static +void ah_iup_interp( AH_Point* p1, + AH_Point* p2, + AH_Point* ref1, + AH_Point* ref2 ) { + AH_Point* p; + FT_Pos u; + FT_Pos v1 = ref1->v; + FT_Pos v2 = ref2->v; + FT_Pos d1 = ref1->u - v1; + FT_Pos d2 = ref2->u - v2; + + + if ( p1 > p2 ) { + return; + } + + if ( v1 == v2 ) { + for ( p = p1; p <= p2; p++ ) + { + FT_Pos u = p->v; + + + if ( u <= v1 ) { + u += d1; + } else { + u += d2; + } + + p->u = u; + } + return; + } + + if ( v1 < v2 ) { + for ( p = p1; p <= p2; p++ ) + { + u = p->v; + + if ( u <= v1 ) { + u += d1; + } else if ( u >= v2 ) { + u += d2; + } else { + u = ref1->u + FT_MulDiv( u - v1, ref2->u - ref1->u, v2 - v1 ); + } + + p->u = u; + } + } else + { + for ( p = p1; p <= p2; p++ ) + { + u = p->v; + + if ( u <= v2 ) { + u += d2; + } else if ( u >= v1 ) { + u += d1; + } else { + u = ref1->u + FT_MulDiv( u - v1, ref2->u - ref1->u, v2 - v1 ); + } + + p->u = u; + } + } +} + + +/* interpolate weak points -- this is equivalent to the TrueType `IUP' */ +static +void ah_hinter_align_weak_points( AH_Hinter* hinter ) { + AH_Outline* outline = hinter->glyph; + FT_Int dimension; + AH_Edge* edges; + AH_Edge* edge_limit; + AH_Point* points; + AH_Point* point_limit; + AH_Point** contour_limit; + AH_Flags touch_flag; + + + points = outline->points; + point_limit = points + outline->num_points; + + /* PASS 1: Move segment points to edge positions */ + + edges = outline->horz_edges; + edge_limit = edges + outline->num_hedges; + touch_flag = ah_flah_touch_y; + + contour_limit = outline->contours + outline->num_contours; + + ah_setup_uv( outline, ah_uv_oy ); + + for ( dimension = 1; dimension >= 0; dimension-- ) + { + AH_Point* point; + AH_Point* end_point; + AH_Point* first_point; + AH_Point** contour; + + + point = points; + contour = outline->contours; + + for ( ; contour < contour_limit; contour++ ) + { + point = *contour; + end_point = point->prev; + first_point = point; + + while ( point <= end_point && !( point->flags & touch_flag ) ) + point++; + + if ( point <= end_point ) { + AH_Point* first_touched = point; + AH_Point* cur_touched = point; + + + point++; + while ( point <= end_point ) + { + if ( point->flags & touch_flag ) { + /* we found two successive touched points; we interpolate */ + /* all contour points between them */ + ah_iup_interp( cur_touched + 1, point - 1, + cur_touched, point ); + cur_touched = point; + } + point++; + } + + if ( cur_touched == first_touched ) { + /* this is a special case: only one point was touched in the */ + /* contour; we thus simply shift the whole contour */ + ah_iup_shift( first_point, end_point, cur_touched ); + } else + { + /* now interpolate after the last touched point to the end */ + /* of the contour */ + ah_iup_interp( cur_touched + 1, end_point, + cur_touched, first_touched ); + + /* if the first contour point isn't touched, interpolate */ + /* from the contour start to the first touched point */ + if ( first_touched > points ) { + ah_iup_interp( first_point, first_touched - 1, + cur_touched, first_touched ); + } + } + } + } + + /* now save the interpolated values back to x/y */ + if ( dimension ) { + for ( point = points; point < point_limit; point++ ) + point->y = point->u; + + touch_flag = ah_flah_touch_x; + ah_setup_uv( outline, ah_uv_ox ); + } else + { + for ( point = points; point < point_limit; point++ ) + point->x = point->u; + + break; /* exit loop */ + } + } +} + +#endif /* !AH_OPTION_NO_WEAK_INTERPOLATION */ + + +LOCAL_FUNC +void ah_hinter_align_points( AH_Hinter* hinter ) { + ah_hinter_align_edge_points( hinter ); + +#ifndef AH_OPTION_NO_STRONG_INTERPOLATION + ah_hinter_align_strong_points( hinter ); +#endif + +#ifndef AH_OPTION_NO_WEAK_INTERPOLATION + ah_hinter_align_weak_points( hinter ); +#endif +} + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** H I N T E R O B J E C T M E T H O D S ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +/* scale and fit the global metrics */ +static +void ah_hinter_scale_globals( AH_Hinter* hinter, + FT_Fixed x_scale, + FT_Fixed y_scale ) { + FT_Int n; + AH_Face_Globals* globals = hinter->globals; + AH_Globals* design = &globals->design; + AH_Globals* scaled = &globals->scaled; + + + /* copy content */ + *scaled = *design; + + /* scale the standard widths & heights */ + for ( n = 0; n < design->num_widths; n++ ) + scaled->widths[n] = FT_MulFix( design->widths[n], x_scale ); + + for ( n = 0; n < design->num_heights; n++ ) + scaled->heights[n] = FT_MulFix( design->heights[n], y_scale ); + + /* scale the blue zones */ + for ( n = 0; n < ah_blue_max; n++ ) + { + FT_Pos delta, delta2; + + + delta = design->blue_shoots[n] - design->blue_refs[n]; + delta2 = delta; + if ( delta < 0 ) { + delta2 = -delta2; + } + delta2 = FT_MulFix( delta2, y_scale ); + + if ( delta2 < 32 ) { + delta2 = 0; + } else if ( delta2 < 64 ) { + delta2 = 32 + ( ( ( delta2 - 32 ) + 16 ) & - 32 ); + } else { + delta2 = ( delta2 + 32 ) & - 64; + } + + if ( delta < 0 ) { + delta2 = -delta2; + } + + scaled->blue_refs[n] = + ( FT_MulFix( design->blue_refs[n], y_scale ) + 32 ) & - 64; + scaled->blue_shoots[n] = scaled->blue_refs[n] + delta2; + } + + globals->x_scale = x_scale; + globals->y_scale = y_scale; +} + + +static +void ah_hinter_align( AH_Hinter* hinter ) { + ah_hinter_align_edge_points( hinter ); + ah_hinter_align_points( hinter ); +} + + +/* finalize a hinter object */ +void ah_hinter_done( AH_Hinter* hinter ) { + if ( hinter ) { + FT_Memory memory = hinter->memory; + + + ah_loader_done( hinter->loader ); + ah_outline_done( hinter->glyph ); + + /* note: the `globals' pointer is _not_ owned by the hinter */ + /* but by the current face object, we don't need to */ + /* release it */ + hinter->globals = 0; + hinter->face = 0; + + FREE( hinter ); + } +} + + +/* create a new empty hinter object */ +FT_Error ah_hinter_new( FT_Library library, + AH_Hinter** ahinter ) { + AH_Hinter* hinter = 0; + FT_Memory memory = library->memory; + FT_Error error; + + + *ahinter = 0; + + /* allocate object */ + if ( ALLOC( hinter, sizeof( *hinter ) ) ) { + goto Exit; + } + + hinter->memory = memory; + hinter->flags = 0; + + /* allocate outline and loader */ + error = ah_outline_new( memory, &hinter->glyph ) || + ah_loader_new( memory, &hinter->loader ) || + ah_loader_create_extra( hinter->loader ); + if ( error ) { + goto Exit; + } + + *ahinter = hinter; + +Exit: + if ( error ) { + ah_hinter_done( hinter ); + } + + return error; +} + + +/* create a face's autohint globals */ +FT_Error ah_hinter_new_face_globals( AH_Hinter* hinter, + FT_Face face, + AH_Globals* globals ) { + FT_Error error; + FT_Memory memory = hinter->memory; + AH_Face_Globals* face_globals; + + + if ( ALLOC( face_globals, sizeof( *face_globals ) ) ) { + goto Exit; + } + + hinter->face = face; + hinter->globals = face_globals; + + if ( globals ) { + face_globals->design = *globals; + } else { + ah_hinter_compute_globals( hinter ); + } + + face->autohint.data = face_globals; + face->autohint.finalizer = (FT_Generic_Finalizer) + ah_hinter_done_face_globals; + face_globals->face = face; + +Exit: + return error; +} + + +/* discard a face's autohint globals */ +void ah_hinter_done_face_globals( AH_Face_Globals* globals ) { + FT_Face face = globals->face; + FT_Memory memory = face->memory; + + + FREE( globals ); +} + + +static +FT_Error ah_hinter_load( AH_Hinter* hinter, + FT_UInt glyph_index, + FT_UInt load_flags, + FT_UInt depth ) { + FT_Face face = hinter->face; + FT_GlyphSlot slot = face->glyph; + FT_Fixed x_scale = face->size->metrics.x_scale; + FT_Fixed y_scale = face->size->metrics.y_scale; + FT_Glyph_Metrics metrics; /* temporary metrics */ + FT_Error error; + AH_Outline* outline = hinter->glyph; + AH_Loader* gloader = hinter->loader; + FT_Bool no_horz_hints = + ( load_flags & AH_HINT_NO_HORZ_EDGES ) != 0; + FT_Bool no_vert_hints = + ( load_flags & AH_HINT_NO_VERT_EDGES ) != 0; + + + /* load the glyph */ + error = FT_Load_Glyph( face, glyph_index, load_flags ); + if ( error ) { + goto Exit; + } + + /* save current glyph metrics */ + metrics = slot->metrics; + + switch ( slot->format ) + { + case ft_glyph_format_outline: + /* first of all, copy the outline points in the loader's current */ + /* extra points, which is used to keep original glyph coordinates */ + error = ah_loader_check_points( gloader, slot->outline.n_points + 2, + slot->outline.n_contours ); + if ( error ) { + goto Exit; + } + + MEM_Copy( gloader->current.extra_points, slot->outline.points, + slot->outline.n_points * sizeof( FT_Vector ) ); + + MEM_Copy( gloader->current.outline.contours, slot->outline.contours, + slot->outline.n_contours * sizeof( short ) ); + + MEM_Copy( gloader->current.outline.tags, slot->outline.tags, + slot->outline.n_points * sizeof( char ) ); + + gloader->current.outline.n_points = slot->outline.n_points; + gloader->current.outline.n_contours = slot->outline.n_contours; + + /* compute original phantom points */ + hinter->pp1.x = 0; + hinter->pp1.y = 0; + hinter->pp2.x = FT_MulFix( slot->metrics.horiAdvance, x_scale ); + hinter->pp2.y = 0; + + /* be sure to check for spacing glyphs */ + if ( slot->outline.n_points == 0 ) { + goto Hint_Metrics; + } + + /* now, load the slot image into the auto-outline, and run the */ + /* automatic hinting process */ + error = ah_outline_load( outline, face ); /* XXX: change to slot */ + if ( error ) { + goto Exit; + } + + /* perform feature detection */ + ah_outline_detect_features( outline ); + + if ( !no_horz_hints ) { + ah_outline_compute_blue_edges( outline, hinter->globals ); + ah_outline_scale_blue_edges( outline, hinter->globals ); + } + + /* perform alignment control */ + ah_hinter_hint_edges( hinter, no_horz_hints, no_vert_hints ); + ah_hinter_align( hinter ); + + /* now save the current outline into the loader's current table */ + ah_outline_save( outline, gloader ); + + /* we now need to hint the metrics according to the change in */ + /* width/positioning that occured during the hinting process */ + { + FT_Pos old_width, new_width; + FT_Pos old_advance, new_advance; + FT_Pos old_lsb, new_lsb; + AH_Edge* edge1 = outline->vert_edges; /* leftmost edge */ + AH_Edge* edge2 = edge1 + + outline->num_vedges - 1; /* rightmost edge */ + + + old_width = edge2->opos - edge1->opos; + new_width = edge2->pos - edge1->pos; + + old_advance = hinter->pp2.x; + old_lsb = edge1->opos; + new_lsb = edge1->pos; + + new_advance = old_advance + + ( new_width + new_lsb - old_width - old_lsb ); + + hinter->pp1.x = ( ( new_lsb - old_lsb ) + 32 ) & - 64; + hinter->pp2.x = ( ( edge2->pos + + ( old_advance - edge2->opos ) ) + 32 ) & - 64; + } + + /* good, we simply add the glyph to our loader's base */ + ah_loader_add( gloader ); + break; + + case ft_glyph_format_composite: + { + FT_UInt nn, num_subglyphs = slot->num_subglyphs; + FT_UInt num_base_subgs, start_point, start_contour; + FT_SubGlyph* subglyph; + + + start_point = gloader->base.outline.n_points; + start_contour = gloader->base.outline.n_contours; + + /* first of all, copy the subglyph descriptors in the glyph loader */ + error = ah_loader_check_subglyphs( gloader, num_subglyphs ); + if ( error ) { + goto Exit; + } + + MEM_Copy( gloader->current.subglyphs, slot->subglyphs, + num_subglyphs * sizeof( FT_SubGlyph ) ); + + gloader->current.num_subglyphs = num_subglyphs; + num_base_subgs = gloader->base.num_subglyphs; + + /* now, read each subglyph independently */ + for ( nn = 0; nn < num_subglyphs; nn++ ) + { + FT_Vector pp1, pp2; + FT_Pos x, y; + FT_UInt num_points, num_new_points, num_base_points; + + + /* gloader.current.subglyphs can change during glyph loading due */ + /* to re-allocation -- we must recompute the current subglyph on */ + /* each iteration */ + subglyph = gloader->base.subglyphs + num_base_subgs + nn; + + pp1 = hinter->pp1; + pp2 = hinter->pp2; + + num_base_points = gloader->base.outline.n_points; + + error = ah_hinter_load( hinter, subglyph->index, + load_flags, depth + 1 ); + if ( error ) { + goto Exit; + } + + /* recompute subglyph pointer */ + subglyph = gloader->base.subglyphs + num_base_subgs + nn; + + if ( subglyph->flags & FT_SUBGLYPH_FLAG_USE_MY_METRICS ) { + pp1 = hinter->pp1; + pp2 = hinter->pp2; + } else + { + hinter->pp1 = pp1; + hinter->pp2 = pp2; + } + + num_points = gloader->base.outline.n_points; + num_new_points = num_points - num_base_points; + + /* now perform the transform required for this subglyph */ + + if ( subglyph->flags & ( FT_SUBGLYPH_FLAG_SCALE | + FT_SUBGLYPH_FLAG_XY_SCALE | + FT_SUBGLYPH_FLAG_2X2 ) ) { + FT_Vector* cur = gloader->base.outline.points + + num_base_points; + FT_Vector* org = gloader->base.extra_points + + num_base_points; + FT_Vector* limit = cur + num_new_points; + + + for ( ; cur < limit; cur++, org++ ) + { + FT_Vector_Transform( cur, &subglyph->transform ); + FT_Vector_Transform( org, &subglyph->transform ); + } + } + + /* apply offset */ + + if ( !( subglyph->flags & FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES ) ) { + FT_Int k = subglyph->arg1; + FT_UInt l = subglyph->arg2; + FT_Vector* p1; + FT_Vector* p2; + + + if ( start_point + k >= num_base_points || + l >= (FT_UInt)num_new_points ) { + error = FT_Err_Invalid_Composite; + goto Exit; + } + + l += num_base_points; + + /* for now, only use the current point coordinates */ + /* we may consider another approach in the near future */ + p1 = gloader->base.outline.points + start_point + k; + p2 = gloader->base.outline.points + start_point + l; + + x = p1->x - p2->x; + y = p1->y - p2->y; + } else + { + x = FT_MulFix( subglyph->arg1, x_scale ); + y = FT_MulFix( subglyph->arg2, y_scale ); + + x = ( x + 32 ) & - 64; + y = ( y + 32 ) & - 64; + } + + { + FT_Outline dummy = gloader->base.outline; + + + dummy.points += num_base_points; + dummy.n_points = num_new_points; + + FT_Outline_Translate( &dummy, x, y ); + } + } + } + break; + + default: + /* we don't support other formats (yet?) */ + error = FT_Err_Unimplemented_Feature; + } + +Hint_Metrics: + if ( depth == 0 ) { + FT_BBox bbox; + + + /* we must translate our final outline by -pp1.x, and compute */ + /* the new metrics */ + if ( hinter->pp1.x ) { + FT_Outline_Translate( &gloader->base.outline, -hinter->pp1.x, 0 ); + } + + FT_Outline_Get_CBox( &gloader->base.outline, &bbox ); + bbox.xMin &= -64; + bbox.yMin &= -64; + bbox.xMax = ( bbox.xMax + 63 ) & - 64; + bbox.yMax = ( bbox.yMax + 63 ) & - 64; + + slot->metrics.width = bbox.xMax - bbox.xMin; + slot->metrics.height = bbox.yMax - bbox.yMin; + slot->metrics.horiBearingX = bbox.xMin; + slot->metrics.horiBearingY = bbox.yMax; + slot->metrics.horiAdvance = hinter->pp2.x - hinter->pp1.x; + /* XXX: TO DO - slot->linearHoriAdvance */ + + /* now copy outline into glyph slot */ + ah_loader_rewind( slot->loader ); + error = ah_loader_copy_points( slot->loader, gloader ); + if ( error ) { + goto Exit; + } + + slot->outline = slot->loader->base.outline; + slot->format = ft_glyph_format_outline; + } + +Exit: + return error; +} + + +/* load and hint a given glyph */ +FT_Error ah_hinter_load_glyph( AH_Hinter* hinter, + FT_GlyphSlot slot, + FT_Size size, + FT_UInt glyph_index, + FT_Int load_flags ) { + FT_Face face = slot->face; + FT_Error error; + FT_Fixed x_scale = size->metrics.x_scale; + FT_Fixed y_scale = size->metrics.y_scale; + AH_Face_Globals* face_globals = FACE_GLOBALS( face ); + + + /* first of all, we need to check that we're using the correct face and */ + /* global hints to load the glyph */ + if ( hinter->face != face || hinter->globals != face_globals ) { + hinter->face = face; + if ( !face_globals ) { + error = ah_hinter_new_face_globals( hinter, face, 0 ); + if ( error ) { + goto Exit; + } + } + hinter->globals = FACE_GLOBALS( face ); + face_globals = FACE_GLOBALS( face ); + } + + /* now, we must check the current character pixel size to see if we */ + /* need to rescale the global metrics */ + if ( face_globals->x_scale != x_scale || + face_globals->y_scale != y_scale ) { + ah_hinter_scale_globals( hinter, x_scale, y_scale ); + } + + load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_RECURSE; + + ah_loader_rewind( hinter->loader ); + + error = ah_hinter_load( hinter, glyph_index, load_flags, 0 ); + +Exit: + return error; +} + + +/* retrieve a face's autohint globals for client applications */ +void ah_hinter_get_global_hints( AH_Hinter* hinter, + FT_Face face, + void** global_hints, + long* global_len ) { + AH_Globals* globals = 0; + FT_Memory memory = hinter->memory; + FT_Error error; + + + /* allocate new master globals */ + if ( ALLOC( globals, sizeof( *globals ) ) ) { + goto Fail; + } + + /* compute face globals if needed */ + if ( !FACE_GLOBALS( face ) ) { + error = ah_hinter_new_face_globals( hinter, face, 0 ); + if ( error ) { + goto Fail; + } + } + + *globals = FACE_GLOBALS( face )->design; + *global_hints = globals; + *global_len = sizeof( *globals ); + + return; + +Fail: + FREE( globals ); + + *global_hints = 0; + *global_len = 0; +} + + +void ah_hinter_done_global_hints( AH_Hinter* hinter, + void* global_hints ) { + FT_Memory memory = hinter->memory; + + + FREE( global_hints ); +} + + +/* END */ diff --git a/src/ft2/ahhint.h b/src/ft2/ahhint.h new file mode 100644 index 0000000..cefc8b1 --- /dev/null +++ b/src/ft2/ahhint.h @@ -0,0 +1,65 @@ +/***************************************************************************/ +/* */ +/* ahhint.h */ +/* */ +/* Glyph hinter (declaration). */ +/* */ +/* Copyright 2000 Catharon Productions Inc. */ +/* Author: David Turner */ +/* */ +/* This file is part of the Catharon Typography Project and shall only */ +/* be used, modified, and distributed under the terms of the Catharon */ +/* Open Source License that should come with this file under the name */ +/* `CatharonLicense.txt'. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/* Note that this license is compatible with the FreeType license. */ +/* */ +/***************************************************************************/ + + +#ifndef AHHINT_H +#define AHHINT_H + + + +#include "ahglobal.h" + + +#define AH_HINT_DEFAULT 0 +#define AH_HINT_NO_ALIGNMENT 1 +#define AH_HINT_NO_HORZ_EDGES 0x20000L +#define AH_HINT_NO_VERT_EDGES 0x40000L + + +/* create a new empty hinter object */ +FT_Error ah_hinter_new( FT_Library library, + AH_Hinter** ahinter ); + +/* Load a hinted glyph in the hinter */ +FT_Error ah_hinter_load_glyph( AH_Hinter* hinter, + FT_GlyphSlot slot, + FT_Size size, + FT_UInt glyph_index, + FT_Int load_flags ); + +/* finalize a hinter object */ +void ah_hinter_done( AH_Hinter* hinter ); + +LOCAL_DEF +void ah_hinter_done_face_globals( AH_Face_Globals* globals ); + +void ah_hinter_get_global_hints( AH_Hinter* hinter, + FT_Face face, + void** global_hints, + long* global_len ); + +void ah_hinter_done_global_hints( AH_Hinter* hinter, + void* global_hints ); + + +#endif /* AHHINT_H */ + + +/* END */ diff --git a/src/ft2/ahloader.h b/src/ft2/ahloader.h new file mode 100644 index 0000000..2898ce7 --- /dev/null +++ b/src/ft2/ahloader.h @@ -0,0 +1,123 @@ +/***************************************************************************/ +/* */ +/* ahloader.h */ +/* */ +/* Glyph loader for the auto-hinting module (declaration only). */ +/* */ +/* Copyright 2000 Catharon Productions Inc. */ +/* Author: David Turner */ +/* */ +/* This file is part of the Catharon Typography Project and shall only */ +/* be used, modified, and distributed under the terms of the Catharon */ +/* Open Source License that should come with this file under the name */ +/* `CatharonLicense.txt'. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/* Note that this license is compatible with the FreeType license. */ +/* */ +/***************************************************************************/ + + +/*************************************************************************/ +/* */ +/* This defines the AH_GlyphLoader type in two different ways: */ +/* */ +/* - If the module is compiled within FreeType 2, the type is simply a */ +/* typedef to FT_GlyphLoader. */ +/* */ +/* - If the module is compiled as a standalone object, AH_GlyphLoader */ +/* has its own implementation. */ +/* */ +/*************************************************************************/ + + +#ifndef AHLOADER_H +#define AHLOADER_H + +#ifdef _STANDALONE_ + +typedef struct AH_GlyphLoad_ +{ + FT_Outline outline; /* outline */ + FT_UInt num_subglyphs; /* number of subglyphs */ + FT_SubGlyph* subglyphs; /* subglyphs */ + FT_Vector* extra_points; /* extra points table */ + +} AH_GlyphLoad; + + +struct AH_GlyphLoader_ +{ + FT_Memory memory; + FT_UInt max_points; + FT_UInt max_contours; + FT_UInt max_subglyphs; + FT_Bool use_extra; + + AH_GlyphLoad base; + AH_GlyphLoad current; + + void* other; /* for possible future extensions */ +}; + + +LOCAL_DEF +FT_Error AH_GlyphLoader_New( FT_Memory memory, + AH_GlyphLoader** aloader ); + +LOCAL_DEF +FT_Error AH_GlyphLoader_Create_Extra( AH_GlyphLoader* loader ); + +LOCAL_DEF +void AH_GlyphLoader_Done( AH_GlyphLoader* loader ); + +LOCAL_DEF +void AH_GlyphLoader_Reset( AH_GlyphLoader* loader ); + +LOCAL_DEF +void AH_GlyphLoader_Rewind( AH_GlyphLoader* loader ); + +LOCAL_DEF +FT_Error AH_GlyphLoader_Check_Points( AH_GlyphLoader* loader, + FT_UInt n_points, + FT_UInt n_contours ); + +LOCAL_DEF +FT_Error AH_GlyphLoader_Check_Subglyphs( AH_GlyphLoader* loader, + FT_UInt n_subs ); + +LOCAL_DEF +void AH_GlyphLoader_Prepare( AH_GlyphLoader* loader ); + +LOCAL_DEF +void AH_GlyphLoader_Add( AH_GlyphLoader* loader ); + +LOCAL_DEF +FT_Error AH_GlyphLoader_Copy_Points( AH_GlyphLoader* target, + FT_GlyphLoader* source ); + +#else /* _STANDALONE */ + +#include "ftobjs.h" + + #define AH_Load FT_GlyphLoad + #define AH_Loader FT_GlyphLoader + + #define ah_loader_new FT_GlyphLoader_New + #define ah_loader_done FT_GlyphLoader_Done + #define ah_loader_reset FT_GlyphLoader_Reset + #define ah_loader_rewind FT_GlyphLoader_Rewind + #define ah_loader_create_extra FT_GlyphLoader_Create_Extra + #define ah_loader_check_points FT_GlyphLoader_Check_Points + #define ah_loader_check_subglyphs FT_GlyphLoader_Check_Subglyphs + #define ah_loader_prepare FT_GlyphLoader_Prepare + #define ah_loader_add FT_GlyphLoader_Add + #define ah_loader_copy_points FT_GlyphLoader_Copy_Points + +#endif /* _STANDALONE_ */ + +#endif /* AHLOADER_H */ + + +/* END */ diff --git a/src/ft2/ahmodule.c b/src/ft2/ahmodule.c new file mode 100644 index 0000000..a183a6f --- /dev/null +++ b/src/ft2/ahmodule.c @@ -0,0 +1,114 @@ +/***************************************************************************/ +/* */ +/* ahmodule.c */ +/* */ +/* Auto-hinting module implementation (declaration). */ +/* */ +/* Copyright 2000 Catharon Productions Inc. */ +/* Author: David Turner */ +/* */ +/* This file is part of the Catharon Typography Project and shall only */ +/* be used, modified, and distributed under the terms of the Catharon */ +/* Open Source License that should come with this file under the name */ +/* `CatharonLicense.txt'. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/* Note that this license is compatible with the FreeType license. */ +/* */ +/***************************************************************************/ + + +#include "ftmodule.h" + + +#include "ahhint.h" + + +typedef struct FT_AutoHinterRec_ +{ + FT_ModuleRec root; + AH_Hinter* hinter; + +} FT_AutoHinterRec; + + +static +FT_Error ft_autohinter_init( FT_AutoHinter module ) { + return ah_hinter_new( module->root.library, &module->hinter ); +} + + +static +void ft_autohinter_done( FT_AutoHinter module ) { + ah_hinter_done( module->hinter ); +} + + +static +FT_Error ft_autohinter_load( FT_AutoHinter module, + FT_GlyphSlot slot, + FT_Size size, + FT_UInt glyph_index, + FT_ULong load_flags ) { + return ah_hinter_load_glyph( module->hinter, + slot, size, glyph_index, load_flags ); +} + + +static +void ft_autohinter_reset( FT_AutoHinter module, + FT_Face face ) { + UNUSED( module ); + + if ( face->autohint.data ) { + ah_hinter_done_face_globals( ( AH_Face_Globals* )( face->autohint.data ) ); + } +} + + +static +void ft_autohinter_get_globals( FT_AutoHinter module, + FT_Face face, + void** global_hints, + long* global_len ) { + ah_hinter_get_global_hints( module->hinter, face, + global_hints, global_len ); +} + + +static +void ft_autohinter_done_globals( FT_AutoHinter module, + void* global_hints ) { + ah_hinter_done_global_hints( module->hinter, global_hints ); +} + + +static +const FT_AutoHinter_Interface autohinter_interface = +{ + ft_autohinter_reset, + ft_autohinter_load, + ft_autohinter_get_globals, + ft_autohinter_done_globals +}; + + +const FT_Module_Class autohint_module_class = +{ + ft_module_hinter, + sizeof( FT_AutoHinterRec ), + + "autohinter", + 0x10000L, /* version 1.0 of the autohinter */ + 0x20000L, /* requires FreeType 2.0 or above */ + + (const void*)&autohinter_interface, + + (FT_Module_Constructor)ft_autohinter_init, + (FT_Module_Destructor) ft_autohinter_done, + (FT_Module_Requester) 0 +}; + + +/* END */ diff --git a/src/ft2/ahmodule.h b/src/ft2/ahmodule.h new file mode 100644 index 0000000..c9b523c --- /dev/null +++ b/src/ft2/ahmodule.h @@ -0,0 +1,32 @@ +/***************************************************************************/ +/* */ +/* ahmodule.h */ +/* */ +/* Auto-hinting module (declaration). */ +/* */ +/* Copyright 2000 Catharon Productions Inc. */ +/* Author: David Turner */ +/* */ +/* This file is part of the Catharon Typography Project and shall only */ +/* be used, modified, and distributed under the terms of the Catharon */ +/* Open Source License that should come with this file under the name */ +/* `CatharonLicense.txt'. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/* Note that this license is compatible with the FreeType license. */ +/* */ +/***************************************************************************/ + + +#ifndef AHMODULE_H +#define AHMODULE_H + +#include "ftmodule.h" + +FT_EXPORT_VAR( const FT_Module_Class ) autohint_module_class; + +#endif /* AHMODULE_H */ + + +/* END */ diff --git a/src/ft2/ahoptim.c b/src/ft2/ahoptim.c new file mode 100644 index 0000000..3b62c73 --- /dev/null +++ b/src/ft2/ahoptim.c @@ -0,0 +1,875 @@ +/***************************************************************************/ +/* */ +/* ahoptim.c */ +/* */ +/* FreeType auto hinting outline optimization (body). */ +/* */ +/* Copyright 2000 Catharon Productions Inc. */ +/* Author: David Turner */ +/* */ +/* This file is part of the Catharon Typography Project and shall only */ +/* be used, modified, and distributed under the terms of the Catharon */ +/* Open Source License that should come with this file under the name */ +/* `CatharonLicense.txt'. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/* Note that this license is compatible with the FreeType license. */ +/* */ +/***************************************************************************/ + + +/*************************************************************************/ +/* */ +/* This module is in charge of optimising the outlines produced by the */ +/* auto-hinter in direct mode. This is required at small pixel sizes in */ +/* order to ensure coherent spacing, among other things.. */ +/* */ +/* The technique used in this module is a simplified simulated */ +/* annealing. */ +/* */ +/*************************************************************************/ + + +#include "ftobjs.h" /* for ALLOC_ARRAY() and FREE() */ + + +#include "ahoptim.h" + + +/* define this macro to use brute force optimisation -- this is slow, */ +/* but a good way to perfect the distortion function `by hand' through */ +/* tweaking */ +#define AH_BRUTE_FORCE + + +#define xxxAH_DEBUG_OPTIM + + +#undef LOG +#ifdef AH_DEBUG_OPTIM + +#define LOG( x ) optim_log ## x + +#else + +#define LOG( x ) + +#endif /* AH_DEBUG_OPTIM */ + + +#ifdef AH_DEBUG_OPTIM + +#include +#include +#include + +#define FLOAT( x ) ( (float)( ( x ) / 64.0 ) ) + +static +void optim_log( const char* fmt, ... ) { + va_list ap; + + + va_start( ap, fmt ); + vprintf( fmt, ap ); + va_end( ap ); +} + + +static +void AH_Dump_Stems( AH_Optimizer* optimizer ) { + int n; + AH_Stem* stem; + + + stem = optimizer->stems; + for ( n = 0; n < optimizer->num_stems; n++, stem++ ) + { + LOG( ( " %c%2d [%.1f:%.1f]={%.1f:%.1f}=" + "<%1.f..%1.f> force=%.1f speed=%.1f\n", + optimizer->vertical ? 'V' : 'H', n, + FLOAT( stem->edge1->opos ), FLOAT( stem->edge2->opos ), + FLOAT( stem->edge1->pos ), FLOAT( stem->edge2->pos ), + FLOAT( stem->min_pos ), FLOAT( stem->max_pos ), + FLOAT( stem->force ), FLOAT( stem->velocity ) ) ); + } +} + + +static +void AH_Dump_Stems2( AH_Optimizer* optimizer ) { + int n; + AH_Stem* stem; + + + stem = optimizer->stems; + for ( n = 0; n < optimizer->num_stems; n++, stem++ ) + { + LOG( ( " %c%2d [%.1f]=<%1.f..%1.f> force=%.1f speed=%.1f\n", + optimizer->vertical ? 'V' : 'H', n, + FLOAT( stem->pos ), + FLOAT( stem->min_pos ), FLOAT( stem->max_pos ), + FLOAT( stem->force ), FLOAT( stem->velocity ) ) ); + } +} + + +static +void AH_Dump_Springs( AH_Optimizer* optimizer ) { + int n; + AH_Spring* spring; + AH_Stem* stems; + + + spring = optimizer->springs; + stems = optimizer->stems; + LOG( ( "%cSprings ", optimizer->vertical ? 'V' : 'H' ) ); + + for ( n = 0; n < optimizer->num_springs; n++, spring++ ) + { + LOG( ( " [%d-%d:%.1f:%1.f:%.1f]", + spring->stem1 - stems, spring->stem2 - stems, + FLOAT( spring->owidth ), + FLOAT( spring->stem2->pos - + ( spring->stem1->pos + spring->stem1->width ) ), + FLOAT( spring->tension ) ) ); + } + + LOG( ( "\n" ) ); +} + +#endif /* AH_DEBUG_OPTIM */ + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** COMPUTE STEMS AND SPRINGS IN AN OUTLINE ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +static +int valid_stem_segments( AH_Segment* seg1, + AH_Segment* seg2 ) { + return seg1->serif == 0 && + seg2 && + seg2->link == seg1 && + seg1->pos < seg2->pos && + seg1->min_coord <= seg2->max_coord && + seg2->min_coord <= seg1->max_coord; +} + + +/* compute all stems in an outline */ +static +int optim_compute_stems( AH_Optimizer* optimizer ) { + AH_Outline* outline = optimizer->outline; + FT_Fixed scale; + FT_Memory memory = optimizer->memory; + FT_Error error = 0; + FT_Int dimension; + AH_Edge* edges; + AH_Edge* edge_limit; + AH_Stem** p_stems; + FT_Int* p_num_stems; + + + edges = outline->horz_edges; + edge_limit = edges + outline->num_hedges; + scale = outline->y_scale; + + p_stems = &optimizer->horz_stems; + p_num_stems = &optimizer->num_hstems; + + for ( dimension = 1; dimension >= 0; dimension-- ) + { + AH_Stem* stems = 0; + FT_Int num_stems = 0; + AH_Edge* edge; + + + /* first of all, count the number of stems in this direction */ + for ( edge = edges; edge < edge_limit; edge++ ) + { + AH_Segment* seg = edge->first; + + + do + { + if ( valid_stem_segments( seg, seg->link ) ) { + num_stems++; + } + + seg = seg->edge_next; + + } while ( seg != edge->first ); + } + + /* now allocate the stems and build their table */ + if ( num_stems > 0 ) { + AH_Stem* stem; + + + if ( ALLOC_ARRAY( stems, num_stems, AH_Stem ) ) { + goto Exit; + } + + stem = stems; + for ( edge = edges; edge < edge_limit; edge++ ) + { + AH_Segment* seg = edge->first; + AH_Segment* seg2; + + + do + { + seg2 = seg->link; + if ( valid_stem_segments( seg, seg2 ) ) { + AH_Edge* edge1 = seg->edge; + AH_Edge* edge2 = seg2->edge; + + + stem->edge1 = edge1; + stem->edge2 = edge2; + stem->opos = edge1->opos; + stem->pos = edge1->pos; + stem->owidth = edge2->opos - edge1->opos; + stem->width = edge2->pos - edge1->pos; + + /* compute min_coord and max_coord */ + { + FT_Pos min_coord = seg->min_coord; + FT_Pos max_coord = seg->max_coord; + + + if ( seg2->min_coord > min_coord ) { + min_coord = seg2->min_coord; + } + + if ( seg2->max_coord < max_coord ) { + max_coord = seg2->max_coord; + } + + stem->min_coord = min_coord; + stem->max_coord = max_coord; + } + + /* compute minimum and maximum positions for stem -- */ + /* note that the left-most/bottom-most stem has always */ + /* a fixed position */ + if ( stem == stems || edge1->blue_edge || edge2->blue_edge ) { + /* this stem cannot move; it is snapped to a blue edge */ + stem->min_pos = stem->pos; + stem->max_pos = stem->pos; + } else + { + /* this edge can move; compute its min and max positions */ + FT_Pos pos1 = stem->opos; + FT_Pos pos2 = pos1 + stem->owidth - stem->width; + FT_Pos min1 = pos1 & - 64; + FT_Pos min2 = pos2 & - 64; + + + stem->min_pos = min1; + stem->max_pos = min1 + 64; + if ( min2 < min1 ) { + stem->min_pos = min2; + } else { + stem->max_pos = min2 + 64; + } + + /* XXX: just to see what it does */ + stem->max_pos += 64; + + /* just for the case where direct hinting did some */ + /* incredible things (e.g. blue edge shifts) */ + if ( stem->min_pos > stem->pos ) { + stem->min_pos = stem->pos; + } + + if ( stem->max_pos < stem->pos ) { + stem->max_pos = stem->pos; + } + } + + stem->velocity = 0; + stem->force = 0; + + stem++; + } + seg = seg->edge_next; + + } while ( seg != edge->first ); + } + } + + *p_stems = stems; + *p_num_stems = num_stems; + + edges = outline->vert_edges; + edge_limit = edges + outline->num_vedges; + scale = outline->x_scale; + + p_stems = &optimizer->vert_stems; + p_num_stems = &optimizer->num_vstems; + } + +Exit: + +#ifdef AH_DEBUG_OPTIM + AH_Dump_Stems( optimizer ); +#endif + + return error; +} + + +/* returns the spring area between two stems, 0 if none */ +static +FT_Pos stem_spring_area( AH_Stem* stem1, + AH_Stem* stem2 ) { + FT_Pos area1 = stem1->max_coord - stem1->min_coord; + FT_Pos area2 = stem2->max_coord - stem2->min_coord; + FT_Pos min = stem1->min_coord; + FT_Pos max = stem1->max_coord; + FT_Pos area; + + + /* order stems */ + if ( stem2->opos <= stem1->opos + stem1->owidth ) { + return 0; + } + + if ( min < stem2->min_coord ) { + min = stem2->min_coord; + } + + if ( max < stem2->max_coord ) { + max = stem2->max_coord; + } + + area = ( max - min ); + if ( 2 * area < area1 && 2 * area < area2 ) { + area = 0; + } + + return area; +} + + +/* compute all springs in an outline */ +static +int optim_compute_springs( AH_Optimizer* optimizer ) { + /* basically, a spring exists between two stems if most of their */ + /* surface is aligned */ + FT_Memory memory = optimizer->memory; + + AH_Stem* stems; + AH_Stem* stem_limit; + AH_Stem* stem; + int dimension; + int error = 0; + + FT_Int* p_num_springs; + AH_Spring** p_springs; + + + stems = optimizer->horz_stems; + stem_limit = stems + optimizer->num_hstems; + + p_springs = &optimizer->horz_springs; + p_num_springs = &optimizer->num_hsprings; + + for ( dimension = 1; dimension >= 0; dimension-- ) + { + FT_Int num_springs = 0; + AH_Spring* springs = 0; + + + /* first of all, count stem springs */ + for ( stem = stems; stem + 1 < stem_limit; stem++ ) + { + AH_Stem* stem2; + + + for ( stem2 = stem + 1; stem2 < stem_limit; stem2++ ) + if ( stem_spring_area( stem, stem2 ) ) { + num_springs++; + } + } + + /* then allocate and build the springs table */ + if ( num_springs > 0 ) { + AH_Spring* spring; + + + /* allocate table of springs */ + if ( ALLOC_ARRAY( springs, num_springs, AH_Spring ) ) { + goto Exit; + } + + /* fill the springs table */ + spring = springs; + for ( stem = stems; stem + 1 < stem_limit; stem++ ) + { + AH_Stem* stem2; + FT_Pos area; + + + for ( stem2 = stem + 1; stem2 < stem_limit; stem2++ ) + { + area = stem_spring_area( stem, stem2 ); + if ( area ) { + /* add a new spring here */ + spring->stem1 = stem; + spring->stem2 = stem2; + spring->owidth = stem2->opos - ( stem->opos + stem->owidth ); + spring->tension = 0; + + spring++; + } + } + } + } + *p_num_springs = num_springs; + *p_springs = springs; + + stems = optimizer->vert_stems; + stem_limit = stems + optimizer->num_vstems; + + p_springs = &optimizer->vert_springs; + p_num_springs = &optimizer->num_vsprings; + } + +Exit: + +#ifdef AH_DEBUG_OPTIM + AH_Dump_Springs( optimizer ); +#endif + + return error; +} + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** OPTIMIZE THROUGH MY STRANGE SIMULATED ANNEALING ALGO ;-) ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + +#ifndef AH_BRUTE_FORCE + +/* compute all spring tensions */ +static +void optim_compute_tensions( AH_Optimizer* optimizer ) { + AH_Spring* spring = optimizer->springs; + AH_Spring* limit = spring + optimizer->num_springs; + + + for ( ; spring < limit; spring++ ) + { + AH_Stem* stem1 = spring->stem1; + AH_Stem* stem2 = spring->stem2; + FT_Int status; + + FT_Pos width; + FT_Pos tension; + FT_Pos sign; + + + /* compute the tension; it simply is -K*(new_width-old_width) */ + width = stem2->pos - ( stem1->pos + stem1->width ); + tension = width - spring->owidth; + + sign = 1; + if ( tension < 0 ) { + sign = -1; + tension = -tension; + } + + if ( width <= 0 ) { + tension = 32000; + } else { + tension = ( tension << 10 ) / width; + } + + tension = -sign * FT_MulFix( tension, optimizer->tension_scale ); + spring->tension = tension; + + /* now, distribute tension among the englobing stems, if they */ + /* are able to move */ + status = 0; + if ( stem1->pos <= stem1->min_pos ) { + status |= 1; + } + if ( stem2->pos >= stem2->max_pos ) { + status |= 2; + } + + if ( !status ) { + tension /= 2; + } + + if ( ( status & 1 ) == 0 ) { + stem1->force -= tension; + } + + if ( ( status & 2 ) == 0 ) { + stem2->force += tension; + } + } +} + + +/* compute all stem movements -- returns 0 if nothing moved */ +static +int optim_compute_stem_movements( AH_Optimizer* optimizer ) { + AH_Stem* stems = optimizer->stems; + AH_Stem* limit = stems + optimizer->num_stems; + AH_Stem* stem = stems; + int moved = 0; + + + /* set initial forces to velocity */ + for ( stem = stems; stem < limit; stem++ ) + { + stem->force = stem->velocity; + stem->velocity /= 2; /* XXX: Heuristics */ + } + + /* compute the sum of forces applied on each stem */ + optim_compute_tensions( optimizer ); + +#ifdef AH_DEBUG_OPTIM + AH_Dump_Springs( optimizer ); + AH_Dump_Stems2( optimizer ); +#endif + + /* now, see whether something can move */ + for ( stem = stems; stem < limit; stem++ ) + { + if ( stem->force > optimizer->tension_threshold ) { + /* there is enough tension to move the stem to the right */ + if ( stem->pos < stem->max_pos ) { + stem->pos += 64; + stem->velocity = stem->force / 2; + moved = 1; + } else { + stem->velocity = 0; + } + } else if ( stem->force < optimizer->tension_threshold ) { + /* there is enough tension to move the stem to the left */ + if ( stem->pos > stem->min_pos ) { + stem->pos -= 64; + stem->velocity = stem->force / 2; + moved = 1; + } else { + stem->velocity = 0; + } + } + } + + /* return 0 if nothing moved */ + return moved; +} + +#endif /* AH_BRUTE_FORCE */ + + +/* compute current global distortion from springs */ +static +FT_Pos optim_compute_distortion( AH_Optimizer* optimizer ) { + AH_Spring* spring = optimizer->springs; + AH_Spring* limit = spring + optimizer->num_springs; + FT_Pos distortion = 0; + + + for ( ; spring < limit; spring++ ) + { + AH_Stem* stem1 = spring->stem1; + AH_Stem* stem2 = spring->stem2; + FT_Pos width; + + width = stem2->pos - ( stem1->pos + stem1->width ); + width -= spring->owidth; + if ( width < 0 ) { + width = -width; + } + + distortion += width; + } + + return distortion; +} + + +/* record stems configuration in `best of' history */ +static +void optim_record_configuration( AH_Optimizer* optimizer ) { + FT_Pos distortion; + AH_Configuration* configs = optimizer->configs; + AH_Configuration* limit = configs + optimizer->num_configs; + AH_Configuration* config; + + + distortion = optim_compute_distortion( optimizer ); + LOG( ( "config distortion = %.1f ", FLOAT( distortion * 64 ) ) ); + + /* check that we really need to add this configuration to our */ + /* sorted history */ + if ( limit > configs && limit[-1].distortion < distortion ) { + LOG( ( "ejected\n" ) ); + return; + } + + /* add new configuration at the end of the table */ + { + int n; + + + config = limit; + if ( optimizer->num_configs < AH_MAX_CONFIGS ) { + optimizer->num_configs++; + } else { + config--; + } + + config->distortion = distortion; + + for ( n = 0; n < optimizer->num_stems; n++ ) + config->positions[n] = optimizer->stems[n].pos; + } + + /* move the current configuration towards the front of the list */ + /* when necessary -- yes this is slow bubble sort ;-) */ + while ( config > configs && config[0].distortion < config[-1].distortion ) + { + AH_Configuration temp; + + + config--; + temp = config[0]; + config[0] = config[1]; + config[1] = temp; + } + LOG( ( "recorded!\n" ) ); +} + + +#ifdef AH_BRUTE_FORCE + +/* optimize outline in a single direction */ +static +void optim_compute( AH_Optimizer* optimizer ) { + int n; + FT_Bool moved; + + AH_Stem* stem = optimizer->stems; + AH_Stem* limit = stem + optimizer->num_stems; + + + /* empty, exit */ + if ( stem >= limit ) { + return; + } + + optimizer->num_configs = 0; + + stem = optimizer->stems; + for ( ; stem < limit; stem++ ) + stem->pos = stem->min_pos; + + do + { + /* record current configuration */ + optim_record_configuration( optimizer ); + + /* now change configuration */ + moved = 0; + for ( stem = optimizer->stems; stem < limit; stem++ ) + { + if ( stem->pos < stem->max_pos ) { + stem->pos += 64; + moved = 1; + break; + } + + stem->pos = stem->min_pos; + } + } while ( moved ); + + /* now, set the best stem positions */ + for ( n = 0; n < optimizer->num_stems; n++ ) + { + AH_Stem* stem = optimizer->stems + n; + FT_Pos pos = optimizer->configs[0].positions[n]; + + + stem->edge1->pos = pos; + stem->edge2->pos = pos + stem->width; + + stem->edge1->flags |= ah_edge_done; + stem->edge2->flags |= ah_edge_done; + } +} + +#else /* AH_BRUTE_FORCE */ + +/* optimize outline in a single direction */ +static +void optim_compute( AH_Optimizer* optimizer ) { + int n, counter, counter2; + + + optimizer->num_configs = 0; + optimizer->tension_scale = 0x80000L; + optimizer->tension_threshold = 64; + + /* record initial configuration threshold */ + optim_record_configuration( optimizer ); + + counter = 0; + for ( counter2 = optimizer->num_stems * 8; counter2 >= 0; counter2-- ) + { + if ( counter == 0 ) { + counter = 2 * optimizer->num_stems; + } + + if ( !optim_compute_stem_movements( optimizer ) ) { + break; + } + + optim_record_configuration( optimizer ); + + counter--; + if ( counter == 0 ) { + optimizer->tension_scale /= 2; + } + } + + /* now, set the best stem positions */ + for ( n = 0; n < optimizer->num_stems; n++ ) + { + AH_Stem* stem = optimizer->stems + n; + FT_Pos pos = optimizer->configs[0].positions[n]; + + + stem->edge1->pos = pos; + stem->edge2->pos = pos + stem->width; + + stem->edge1->flags |= ah_edge_done; + stem->edge2->flags |= ah_edge_done; + } +} + +#endif /* AH_BRUTE_FORCE */ + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** HIGH-LEVEL OPTIMIZER API ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +/* releases the optimization data */ +void AH_Optimizer_Done( AH_Optimizer* optimizer ) { + if ( optimizer ) { + FT_Memory memory = optimizer->memory; + + + FREE( optimizer->horz_stems ); + FREE( optimizer->vert_stems ); + FREE( optimizer->horz_springs ); + FREE( optimizer->vert_springs ); + FREE( optimizer->positions ); + } +} + + +/* loads the outline into the optimizer */ +int AH_Optimizer_Init( AH_Optimizer* optimizer, + AH_Outline* outline, + FT_Memory memory ) { + FT_Error error; + + + MEM_Set( optimizer, 0, sizeof( *optimizer ) ); + optimizer->outline = outline; + optimizer->memory = memory; + + LOG( ( "initializing new optimizer\n" ) ); + /* compute stems and springs */ + error = optim_compute_stems( optimizer ) || + optim_compute_springs( optimizer ); + if ( error ) { + goto Fail; + } + + /* allocate stem positions history and configurations */ + { + int n, max_stems; + + + max_stems = optimizer->num_hstems; + if ( max_stems < optimizer->num_vstems ) { + max_stems = optimizer->num_vstems; + } + + if ( ALLOC_ARRAY( optimizer->positions, + max_stems * AH_MAX_CONFIGS, FT_Pos ) ) { + goto Fail; + } + + optimizer->num_configs = 0; + for ( n = 0; n < AH_MAX_CONFIGS; n++ ) + optimizer->configs[n].positions = optimizer->positions + + n * max_stems; + } + + return error; + +Fail: + AH_Optimizer_Done( optimizer ); + return error; +} + + +/* compute optimal outline */ +void AH_Optimizer_Compute( AH_Optimizer* optimizer ) { + optimizer->num_stems = optimizer->num_hstems; + optimizer->stems = optimizer->horz_stems; + optimizer->num_springs = optimizer->num_hsprings; + optimizer->springs = optimizer->horz_springs; + + if ( optimizer->num_springs > 0 ) { + LOG( ( "horizontal optimization ------------------------\n" ) ); + optim_compute( optimizer ); + } + + optimizer->num_stems = optimizer->num_vstems; + optimizer->stems = optimizer->vert_stems; + optimizer->num_springs = optimizer->num_vsprings; + optimizer->springs = optimizer->vert_springs; + + if ( optimizer->num_springs ) { + LOG( ( "vertical optimization --------------------------\n" ) ); + optim_compute( optimizer ); + } +} + + +/* END */ diff --git a/src/ft2/ahoptim.h b/src/ft2/ahoptim.h new file mode 100644 index 0000000..9d9a4d2 --- /dev/null +++ b/src/ft2/ahoptim.h @@ -0,0 +1,127 @@ +/***************************************************************************/ +/* */ +/* ahoptim.h */ +/* */ +/* FreeType auto hinting outline optimization (declaration). */ +/* */ +/* Copyright 2000 Catharon Productions Inc. */ +/* Author: David Turner */ +/* */ +/* This file is part of the Catharon Typography Project and shall only */ +/* be used, modified, and distributed under the terms of the Catharon */ +/* Open Source License that should come with this file under the name */ +/* `CatharonLicense.txt'. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/* Note that this license is compatible with the FreeType license. */ +/* */ +/***************************************************************************/ + + +#ifndef AHOPTIM_H +#define AHOPTIM_H + + +#include "ahtypes.h" + +/* the maximal number of stem configurations to record */ +/* during optimization */ +#define AH_MAX_CONFIGS 8 + + +typedef struct AH_Stem_ +{ + FT_Pos pos; /* current position */ + FT_Pos velocity; /* current velocity */ + FT_Pos force; /* sum of current forces */ + FT_Pos width; /* normalized width */ + + FT_Pos min_pos; /* minimum grid position */ + FT_Pos max_pos; /* maximum grid position */ + + AH_Edge* edge1; /* left/bottom edge */ + AH_Edge* edge2; /* right/top edge */ + + FT_Pos opos; /* original position */ + FT_Pos owidth; /* original width */ + + FT_Pos min_coord; /* minimum coordinate */ + FT_Pos max_coord; /* maximum coordinate */ + +} AH_Stem; + + +/* A spring between two stems */ +typedef struct AH_Spring_ +{ + AH_Stem* stem1; + AH_Stem* stem2; + FT_Pos owidth; /* original width */ + FT_Pos tension; /* current tension */ + +} AH_Spring; + + +/* A configuration records the position of each stem at a given time */ +/* as well as the associated distortion */ +typedef struct AH_Configuration_ +{ + FT_Pos* positions; + FT_Long distortion; + +} AH_Configuration; + + +typedef struct AH_Optimizer_ +{ + FT_Memory memory; + AH_Outline* outline; + + FT_Int num_hstems; + AH_Stem* horz_stems; + + FT_Int num_vstems; + AH_Stem* vert_stems; + + FT_Int num_hsprings; + FT_Int num_vsprings; + AH_Spring* horz_springs; + AH_Spring* vert_springs; + + FT_Int num_configs; + AH_Configuration configs[AH_MAX_CONFIGS]; + FT_Pos* positions; + + /* during each pass, use these instead */ + FT_Int num_stems; + AH_Stem* stems; + + FT_Int num_springs; + AH_Spring* springs; + FT_Bool vertical; + + FT_Fixed tension_scale; + FT_Pos tension_threshold; + +} AH_Optimizer; + + +/* loads the outline into the optimizer */ +int AH_Optimizer_Init( AH_Optimizer* optimizer, + AH_Outline* outline, + FT_Memory memory ); + + +/* compute optimal outline */ +void AH_Optimizer_Compute( AH_Optimizer* optimizer ); + + +/* release the optimization data */ +void AH_Optimizer_Done( AH_Optimizer* optimizer ); + + +#endif /* AHOPTIM_H */ + + +/* END */ diff --git a/src/ft2/ahtypes.h b/src/ft2/ahtypes.h new file mode 100644 index 0000000..af93c5f --- /dev/null +++ b/src/ft2/ahtypes.h @@ -0,0 +1,484 @@ +/***************************************************************************/ +/* */ +/* ahtypes.h */ +/* */ +/* General types and definitions for the auto-hint module */ +/* (specification only). */ +/* */ +/* Copyright 2000 Catharon Productions Inc. */ +/* Author: David Turner */ +/* */ +/* This file is part of the Catharon Typography Project and shall only */ +/* be used, modified, and distributed under the terms of the Catharon */ +/* Open Source License that should come with this file under the name */ +/* `CatharonLicense.txt'. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/* Note that this license is compatible with the FreeType license. */ +/* */ +/***************************************************************************/ + + +#ifndef AHTYPES_H +#define AHTYPES_H + + +#include "ftobjs.h" + + +#include "ahloader.h" + + +#define xxAH_DEBUG + + +#ifdef AH_DEBUG + +#include + +#define AH_LOG( x ) printf ## x + +#else + +#define AH_LOG( x ) do ;while ( 0 ) /* nothing */ + +#endif + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** COMPILE-TIME BUILD OPTIONS ****/ +/**** ****/ +/**** Toggle these configuration macros to experiment with `features' ****/ +/**** of the auto-hinter. ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* If this option is defined, only strong interpolation will be used to */ +/* place the points between edges. Otherwise, `smooth' points are */ +/* detected and later hinted through weak interpolation to correct some */ +/* unpleasant artefacts. */ +/* */ +#undef AH_OPTION_NO_WEAK_INTERPOLATION + + +/*************************************************************************/ +/* */ +/* If this option is defined, only weak interpolation will be used to */ +/* place the points between edges. Otherwise, `strong' points are */ +/* detected and later hinted through strong interpolation to correct */ +/* some unpleasant artefacts. */ +/* */ +#undef AH_OPTION_NO_STRONG_INTERPOLATION + + +/*************************************************************************/ +/* */ +/* Undefine this macro if you don't want to hint the metrics. There is */ +/* no reason to do this (at least for non-CJK scripts), except for */ +/* experimentation. */ +/* */ +#define AH_HINT_METRICS + + +/*************************************************************************/ +/* */ +/* Define this macro if you do not want to insert extra edges at a */ +/* glyph's x and y extremum (if there isn't one already available). */ +/* This helps to reduce a number of artefacts and allows hinting of */ +/* metrics. */ +/* */ +#undef AH_OPTION_NO_EXTREMUM_EDGES + + +/* don't touch for now */ +#define AH_MAX_WIDTHS 12 +#define AH_MAX_HEIGHTS 12 + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** TYPE DEFINITIONS ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +/* see agangles.h */ +typedef FT_Int AH_Angle; + + +/* hint flags */ +#define ah_flah_none 0 + +/* bezier control points flags */ +#define ah_flah_conic 1 +#define ah_flah_cubic 2 +#define ah_flah_control ( ah_flah_conic | ah_flah_cubic ) + +/* extrema flags */ +#define ah_flah_extrema_x 4 +#define ah_flah_extrema_y 8 + +/* roundness */ +#define ah_flah_round_x 16 +#define ah_flah_round_y 32 + +/* touched */ +#define ah_flah_touch_x 64 +#define ah_flah_touch_y 128 + +/* weak interpolation */ +#define ah_flah_weak_interpolation 256 + +typedef FT_Int AH_Flags; + + +/* edge hint flags */ +#define ah_edge_normal 0 +#define ah_edge_round 1 +#define ah_edge_serif 2 +#define ah_edge_done 4 + +typedef FT_Int AH_Edge_Flags; + + +/* hint directions -- the values are computed so that two vectors are */ +/* in opposite directions iff `dir1+dir2 == 0' */ +#define ah_dir_none 4 +#define ah_dir_right 1 +#define ah_dir_left -1 +#define ah_dir_up 2 +#define ah_dir_down -2 + +typedef FT_Int AH_Direction; + + +typedef struct AH_Point AH_Point; +typedef struct AH_Segment AH_Segment; +typedef struct AH_Edge AH_Edge; + + +/*************************************************************************/ +/* */ +/* */ +/* AH_Point */ +/* */ +/* */ +/* A structure used to model an outline point to the AH_Outline type. */ +/* */ +/* */ +/* flags :: The current point hint flags. */ +/* */ +/* ox, oy :: The current original scaled coordinates. */ +/* */ +/* fx, fy :: The current coordinates in font units. */ +/* */ +/* x, y :: The current hinter coordinates. */ +/* */ +/* u, v :: Point coordinates -- meaning varies with context. */ +/* */ +/* in_dir :: The direction of the inwards vector (prev->point). */ +/* */ +/* out_dir :: The direction of the outwards vector (point->next). */ +/* */ +/* in_angle :: The angle of the inwards vector. */ +/* */ +/* out_angle :: The angle of the outwards vector. */ +/* */ +/* next :: The next point in same contour. */ +/* */ +/* prev :: The previous point in same contour. */ +/* */ +struct AH_Point +{ + AH_Flags flags; /* point flags used by hinter */ + FT_Pos ox, oy; + FT_Pos fx, fy; + FT_Pos x, y; + FT_Pos u, v; + + AH_Direction in_dir; /* direction of inwards vector */ + AH_Direction out_dir; /* direction of outwards vector */ + + AH_Angle in_angle; + AH_Angle out_angle; + + AH_Point* next; /* next point in contour */ + AH_Point* prev; /* previous point in contour */ +}; + + +/*************************************************************************/ +/* */ +/* */ +/* AH_Segment */ +/* */ +/* */ +/* A structure used to describe an edge segment to the auto-hinter. */ +/* A segment is simply a sequence of successive points located on the */ +/* same horizontal or vertical `position', in a given direction. */ +/* */ +/* */ +/* flags :: The segment edge flags (straight, rounded, etc.). */ +/* */ +/* dir :: The segment direction. */ +/* */ +/* first :: The first point in the segment. */ +/* */ +/* last :: The last point in the segment. */ +/* */ +/* contour :: A pointer to the first point of the segment's */ +/* contour. */ +/* */ +/* pos :: The segment position in font units. */ +/* */ +/* size :: The segment size. */ +/* */ +/* edge :: The edge of the current segment. */ +/* */ +/* edge_next :: The next segment on the same edge. */ +/* */ +/* link :: The pairing segment for this edge. */ +/* */ +/* serif :: The primary segment for serifs. */ +/* */ +/* num_linked :: The number of other segments that link to this one. */ +/* */ +/* score :: Used to score the segment when selecting them. */ +/* */ +struct AH_Segment +{ + AH_Edge_Flags flags; + AH_Direction dir; + + AH_Point* first; /* first point in edge segment */ + AH_Point* last; /* last point in edge segment */ + AH_Point** contour; /* ptr to first point of segment's contour */ + + FT_Pos pos; /* position of segment */ + FT_Pos min_coord; /* minimum coordinate of segment */ + FT_Pos max_coord; /* maximum coordinate of segment */ + + AH_Edge* edge; + AH_Segment* edge_next; + + AH_Segment* link; /* link segment */ + AH_Segment* serif; /* primary segment for serifs */ + FT_Pos num_linked; /* number of linked segments */ + FT_Int score; +}; + + +/*************************************************************************/ +/* */ +/* */ +/* AH_Edge */ +/* */ +/* */ +/* A structure used to describe an edge, which really is a horizontal */ +/* or vertical coordinate to be hinted depending on the segments */ +/* located on it. */ +/* */ +/* */ +/* flags :: The segment edge flags (straight, rounded, etc.). */ +/* */ +/* dir :: The main segment direction on this edge. */ +/* */ +/* first :: The first edge segment. */ +/* */ +/* last :: The last edge segment. */ +/* */ +/* fpos :: The original edge position in font units. */ +/* */ +/* opos :: The original scaled edge position. */ +/* */ +/* pos :: The hinted edge position. */ +/* */ +/* link :: The linked edge. */ +/* */ +/* serif :: The serif edge. */ +/* */ +/* num_paired :: The number of other edges that pair to this one. */ +/* */ +/* score :: Used to score the edge when selecting them. */ +/* */ +/* blue_edge :: Indicate the blue zone edge this edge is related to. */ +/* Only set for some of the horizontal edges in a Latin */ +/* font. */ +/* */ +struct AH_Edge +{ + AH_Edge_Flags flags; + AH_Direction dir; + + AH_Segment* first; + AH_Segment* last; + + FT_Pos fpos; + FT_Pos opos; + FT_Pos pos; + + AH_Edge* link; + AH_Edge* serif; + FT_Int num_linked; + + FT_Int score; + FT_Pos* blue_edge; +}; + + +/* an outline as seen by the hinter */ +typedef struct AH_Outline_ +{ + FT_Memory memory; + + AH_Direction vert_major_dir; /* vertical major direction */ + AH_Direction horz_major_dir; /* horizontal major direction */ + + FT_Fixed x_scale; + FT_Fixed y_scale; + FT_Pos edge_distance_threshold; + + FT_Int max_points; + FT_Int num_points; + AH_Point* points; + + FT_Int max_contours; + FT_Int num_contours; + AH_Point** contours; + + FT_Int num_hedges; + AH_Edge* horz_edges; + + FT_Int num_vedges; + AH_Edge* vert_edges; + + FT_Int num_hsegments; + AH_Segment* horz_segments; + + FT_Int num_vsegments; + AH_Segment* vert_segments; + +} AH_Outline; + + +#define ah_blue_capital_top 0 /* THEZOCQS */ +#define ah_blue_capital_bottom ( ah_blue_capital_top + 1 ) /* HEZLOCUS */ +#define ah_blue_small_top ( ah_blue_capital_bottom + 1 ) /* xzroesc */ +#define ah_blue_small_bottom ( ah_blue_small_top + 1 ) /* xzroesc */ +#define ah_blue_small_minor ( ah_blue_small_bottom + 1 ) /* pqgjy */ +#define ah_blue_max ( ah_blue_small_minor + 1 ) + +typedef FT_Int AH_Blue; + + +#define ah_hinter_monochrome 1 +#define ah_hinter_optimize 2 + +typedef FT_Int AH_Hinter_Flags; + + +/*************************************************************************/ +/* */ +/* */ +/* AH_Globals */ +/* */ +/* */ +/* Holds the global metrics for a given font face (be it in design */ +/* units or scaled pixel values). */ +/* */ +/* */ +/* num_widths :: The number of widths. */ +/* */ +/* num_heights :: The number of heights. */ +/* */ +/* widths :: Snap widths, including standard one. */ +/* */ +/* heights :: Snap height, including standard one. */ +/* */ +/* blue_refs :: The reference positions of blue zones. */ +/* */ +/* blue_shoots :: The overshoot positions of blue zones. */ +/* */ +typedef struct AH_Globals_ +{ + FT_Int num_widths; + FT_Int num_heights; + + FT_Pos widths [AH_MAX_WIDTHS]; + FT_Pos heights[AH_MAX_HEIGHTS]; + + FT_Pos blue_refs [ah_blue_max]; + FT_Pos blue_shoots[ah_blue_max]; + +} AH_Globals; + + +/*************************************************************************/ +/* */ +/* */ +/* AH_Face_Globals */ +/* */ +/* */ +/* Holds the complete global metrics for a given font face (i.e., the */ +/* design units version + a scaled version + the current scales */ +/* used). */ +/* */ +/* */ +/* face :: A handle to the source face object */ +/* */ +/* design :: The globals in font design units. */ +/* */ +/* scaled :: Scaled globals in sub-pixel values. */ +/* */ +/* x_scale :: The current horizontal scale. */ +/* */ +/* y_scale :: The current vertical scale. */ +/* */ +typedef struct AH_Face_Globals_ +{ + FT_Face face; + AH_Globals design; + AH_Globals scaled; + FT_Fixed x_scale; + FT_Fixed y_scale; + FT_Bool control_overshoot; + +} AH_Face_Globals; + + +typedef struct AH_Hinter +{ + FT_Memory memory; + AH_Hinter_Flags flags; + + FT_Int algorithm; + FT_Face face; + + AH_Face_Globals* globals; + + AH_Outline* glyph; + + AH_Loader* loader; + FT_Vector pp1; + FT_Vector pp2; + +} AH_Hinter; + + +#endif /* AHTYPES_H */ + + +/* END */ diff --git a/src/ft2/autohint.h b/src/ft2/autohint.h new file mode 100644 index 0000000..05951f6 --- /dev/null +++ b/src/ft2/autohint.h @@ -0,0 +1,195 @@ +/***************************************************************************/ +/* */ +/* autohint.h */ +/* */ +/* High-level `autohint' module-specific interface (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +/*************************************************************************/ +/* */ +/* The auto-hinter is used to load and automatically hint glyphs if a */ +/* format-specific hinter isn't available. */ +/* */ +/*************************************************************************/ + + +#ifndef AUTOHINT_H +#define AUTOHINT_H + + +/*************************************************************************/ +/* */ +/* A small technical note regarding automatic hinting in order to */ +/* clarify this module interface. */ +/* */ +/* An automatic hinter might compute two kinds of data for a given face: */ +/* */ +/* - global hints: Usually some metrics that describe global properties */ +/* of the face. It is computed by scanning more or less */ +/* agressively the glyphs in the face, and thus can be */ +/* very slow to compute (even if the size of global */ +/* hints is really small). */ +/* */ +/* - glyph hints: These describe some important features of the glyph */ +/* outline, as well as how to align them. They are */ +/* generally much faster to compute than global hints. */ +/* */ +/* The current FreeType auto-hinter does a pretty good job while */ +/* performing fast computations for both global and glyph hints. */ +/* However, we might be interested in introducing more complex and */ +/* powerful algorithms in the future, like the one described in the John */ +/* D. Hobby paper, which unfortunately requires a lot more horsepower. */ +/* */ +/* Because a sufficiently sophisticated font management system would */ +/* typically implement an LRU cache of opened face objects to reduce */ +/* memory usage, it is a good idea to be able to avoid recomputing */ +/* global hints every time the same face is re-opened. */ +/* */ +/* We thus provide the ability to cache global hints outside of the face */ +/* object, in order to speed up font re-opening time. Of course, this */ +/* feature is purely optional, so most client programs won't even notice */ +/* it. */ +/* */ +/* I initially thought that it would be a good idea to cache the glyph */ +/* hints too. However, my general idea now is that if you really need */ +/* to cache these too, you are simply in need of a new font format, */ +/* where all this information could be stored within the font file and */ +/* decoded on the fly. */ +/* */ +/*************************************************************************/ + + +#include "freetype.h" + + +typedef struct FT_AutoHinterRec_ *FT_AutoHinter; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_AutoHinter_Get_Global_Func */ +/* */ +/* */ +/* Retrieves the global hints computed for a given face object the */ +/* resulting data is dissociated from the face and will survive a */ +/* call to FT_Done_Face(). It must be discarded through the API */ +/* FT_AutoHinter_Done_Global_Func(). */ +/* */ +/* */ +/* hinter :: A handle to the source auto-hinter. */ +/* */ +/* face :: A handle to the source face object. */ +/* */ +/* */ +/* global_hints :: A typeless pointer to the global hints. */ +/* */ +/* global_len :: The size in bytes of the global hints. */ +/* */ +typedef void ( *FT_AutoHinter_Get_Global_Func )( + FT_AutoHinter hinter, + FT_Face face, + void** global_hints, + long* global_len ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_AutoHinter_Done_Global_Func */ +/* */ +/* */ +/* Discards the global hints retrieved through */ +/* FT_AutoHinter_Get_Global_Func(). This is the only way these hints */ +/* are freed from memory. */ +/* */ +/* */ +/* hinter :: A handle to the auto-hinter module. */ +/* */ +/* global :: A pointer to retrieved global hints to discard. */ +/* */ +typedef void ( *FT_AutoHinter_Done_Global_Func )( FT_AutoHinter hinter, + void* global ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_AutoHinter_Reset_Func */ +/* */ +/* */ +/* This function is used to recompute the global metrics in a given */ +/* font. This is useful when global font data changes (e.g. Multiple */ +/* Masters fonts where blend coordinates change). */ +/* */ +/* */ +/* hinter :: A handle to the source auto-hinter. */ +/* */ +/* face :: A handle to the face. */ +/* */ +typedef void ( *FT_AutoHinter_Reset_Func )( FT_AutoHinter hinter, + FT_Face face ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_AutoHinter_Load_Func */ +/* */ +/* */ +/* This function is used to load, scale, and automatically hint a */ +/* glyph from a given face. */ +/* */ +/* */ +/* face :: A handle to the face. */ +/* glyph_index :: The glyph index. */ +/* load_flags :: The load flags. */ +/* */ +/* */ +/* This function is capable of loading composite glyphs by hinting */ +/* each sub-glyph independently (which improves quality). */ +/* */ +/* It will call the font driver with FT_Load_Glyph(), with */ +/* FT_LOAD_NO_SCALE set. */ +/* */ +typedef FT_Error ( *FT_AutoHinter_Load_Func )( FT_AutoHinter hinter, + FT_GlyphSlot slot, + FT_Size size, + FT_UInt glyph_index, + FT_ULong load_flags ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_AutoHinter_Interface */ +/* */ +/* */ +/* The auto-hinter module's interface. */ +/* */ +typedef struct FT_AutoHinter_Interface +{ + FT_AutoHinter_Reset_Func reset_face; + FT_AutoHinter_Load_Func load_glyph; + + FT_AutoHinter_Get_Global_Func get_global_hints; + FT_AutoHinter_Done_Global_Func done_global_hints; + +} FT_AutoHinter_Interface; + + +#endif /* AUTOHINT_H */ + + +/* END */ diff --git a/src/ft2/freetype.h b/src/ft2/freetype.h new file mode 100644 index 0000000..530910c --- /dev/null +++ b/src/ft2/freetype.h @@ -0,0 +1,2286 @@ +/***************************************************************************/ +/* */ +/* freetype.h */ +/* */ +/* FreeType high-level API and common types (specification only). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef FREETYPE_H +#define FREETYPE_H + + +/*************************************************************************/ +/* */ +/* The `raster' component duplicates some of the declarations in */ +/* freetype.h for stand-alone use if _FREETYPE_ isn't defined. */ +/* */ +#define _FREETYPE_ + + +/*************************************************************************/ +/* */ +/* The FREETYPE_MAJOR and FREETYPE_MINOR macros are used to version the */ +/* new FreeType design, which is able to host several kinds of font */ +/* drivers. It starts at 2.0. */ +/* */ +#define FREETYPE_MAJOR 2 +#define FREETYPE_MINOR 0 + + +#include "ftconfig.h" /* read configuration information */ +#include "fterrors.h" +#include "fttypes.h" + + +#ifdef __cplusplus +extern "C" { +#endif + + +/*************************************************************************/ +/*************************************************************************/ +/* */ +/* B A S I C T Y P E S */ +/* */ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Glyph_Metrics */ +/* */ +/* */ +/* A structure used to model the metrics of a single glyph. Note */ +/* that values are expressed in 26.6 fractional pixel format or in */ +/* font units, depending on context. */ +/* */ +/* */ +/* width :: The glyph's width. */ +/* */ +/* height :: The glyph's height. */ +/* */ +/* horiBearingX :: Horizontal left side bearing. */ +/* */ +/* horiBearingY :: Horizontal top side bearing. */ +/* */ +/* horiAdvance :: Horizontal advance width. */ +/* */ +/* vertBearingX :: Vertical left side bearing. */ +/* */ +/* vertBearingY :: Vertical top side bearing. */ +/* */ +/* vertAdvance :: Vertical advance height. */ +/* */ +typedef struct FT_Glyph_Metrics_ +{ + FT_Pos width; /* glyph width */ + FT_Pos height; /* glyph height */ + + FT_Pos horiBearingX; /* left side bearing in horizontal layouts */ + FT_Pos horiBearingY; /* top side bearing in horizontal layouts */ + FT_Pos horiAdvance; /* advance width for horizontal layout */ + + FT_Pos vertBearingX; /* left side bearing in vertical layouts */ + FT_Pos vertBearingY; /* top side bearing in vertical layouts */ + FT_Pos vertAdvance; /* advance height for vertical layout */ + +} FT_Glyph_Metrics; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Generic_Finalizer */ +/* */ +/* */ +/* Describes a function used to destroy the `client' data of any */ +/* FreeType object. See the description of the FT_Generic type for */ +/* details of usage. */ +/* */ +/* */ +/* The address of the FreeType object which is under finalization. */ +/* Its client data is accessed through its `generic' field. */ +/* */ +typedef void ( *FT_Generic_Finalizer )( void* object ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Generic */ +/* */ +/* */ +/* Client applications often need to associate their own data to a */ +/* variety of FreeType core objects. For example, a text layout API */ +/* might want to associate a glyph cache to a given size object. */ +/* */ +/* Most FreeType object contains a `generic' field, of type */ +/* FT_Generic, which usage is left to client applications and font */ +/* servers. */ +/* */ +/* It can be used to store a pointer to client-specific data, as well */ +/* as the address of a `finalizer' function, which will be called by */ +/* FreeType when the object is destroyed (for example, the previous */ +/* client example would put the address of the glyph cache destructor */ +/* in the `finalizer' field). */ +/* */ +/* */ +/* data :: A typeless pointer to any client-specified data. This */ +/* field is completely ignored by the FreeType library. */ +/* */ +/* finalizer :: A pointer to a `generic finalizer' function, which */ +/* will be called when the object is destroyed. If this */ +/* field is set to NULL, no code will be called. */ +/* */ +typedef struct FT_Generic_ +{ + void* data; + FT_Generic_Finalizer finalizer; + +} FT_Generic; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Bitmap_Size */ +/* */ +/* */ +/* An extremely simple structure used to model the size of a bitmap */ +/* strike (i.e., a bitmap instance of the font for a given */ +/* resolution) in a fixed-size font face. This is used for the */ +/* `available_sizes' field of the FT_Face_Properties structure. */ +/* */ +/* */ +/* height :: The character height in pixels. */ +/* */ +/* width :: The character width in pixels. */ +/* */ +typedef struct FT_Bitmap_Size_ +{ + FT_Short height; + FT_Short width; + +} FT_Bitmap_Size; + + +/*************************************************************************/ +/*************************************************************************/ +/* */ +/* O B J E C T C L A S S E S */ +/* */ +/*************************************************************************/ +/*************************************************************************/ + +/*************************************************************************/ +/* */ +/* */ +/* FT_Library */ +/* */ +/* */ +/* A handle to a FreeType library instance. Each `library' is */ +/* completely independent from the others; it is the `root' of a set */ +/* of objects like fonts, faces, sizes, etc. */ +/* */ +/* It also embeds a system object (see FT_System), as well as a */ +/* scan-line converter object (see FT_Raster). */ +/* */ +/* */ +/* Library objects are created through FT_Init_FreeType(). */ +/* */ +typedef struct FT_LibraryRec_ *FT_Library; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Module */ +/* */ +/* */ +/* A handle to a given FreeType module object. Each module can be a */ +/* font driver, a renderer, or anything else that provides services */ +/* to the formers. */ +/* */ +typedef struct FT_ModuleRec_* FT_Module; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Driver */ +/* */ +/* */ +/* A handle to a given FreeType font driver object. Each font driver */ +/* is able to create faces, sizes, glyph slots, and charmaps from the */ +/* resources whose format it supports. */ +/* */ +/* A driver can support either bitmap, graymap, or scalable font */ +/* formats. */ +/* */ +typedef struct FT_DriverRec_* FT_Driver; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Renderer */ +/* */ +/* */ +/* A handle to a given FreeType renderer. A renderer is in charge of */ +/* converting a glyph image to a bitmap, when necessary. Each */ +/* supports a given glyph image format, and one or more target */ +/* surface depths. */ +/* */ +typedef struct FT_RendererRec_* FT_Renderer; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Face */ +/* */ +/* */ +/* A handle to a given driver face object. A face object contains */ +/* all the instance and glyph independent data of a font file */ +/* typeface. */ +/* */ +/* A face object is created from a resource object through the */ +/* new_face() method of a given driver. */ +/* */ +typedef struct FT_FaceRec_* FT_Face; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Size */ +/* */ +/* */ +/* A handle to a given driver size object. Such an object models the */ +/* _resolution_ AND _size_ dependent state of a given driver face */ +/* size. */ +/* */ +/* A size object is always created from a given face object. It is */ +/* discarded automatically by its parent face. */ +/* */ +typedef struct FT_SizeRec_* FT_Size; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_GlyphSlot */ +/* */ +/* */ +/* A handle to a given `glyph slot'. A slot is a container where it */ +/* is possible to load any of the glyphs contained within its parent */ +/* face. */ +/* */ +/* A glyph slot is created from a given face object. It is discarded */ +/* automatically by its parent face. */ +/* */ +typedef struct FT_GlyphSlotRec_* FT_GlyphSlot; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_CharMap */ +/* */ +/* */ +/* A handle to a given character map. A charmap is used to translate */ +/* character codes in a given encoding into glyph indexes for its */ +/* parent's face. Some font formats may provide several charmaps per */ +/* font. */ +/* */ +/* A charmap is created from a given face object. It is discarded */ +/* automatically by its parent face. */ +/* */ +typedef struct FT_CharMapRec_* FT_CharMap; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Encoding */ +/* */ +/* */ +/* An enumeration used to specify encodings supported by charmaps. */ +/* Used in the FT_Select_CharMap() API function. */ +/* */ +/* */ +/* Because of 32-bit charcodes defined in Unicode (i.e., surrogates), */ +/* all character codes must be expressed as FT_Longs. */ +/* */ +typedef enum FT_Encoding_ +{ + ft_encoding_none = 0, + ft_encoding_symbol = FT_MAKE_TAG( 's', 'y', 'm', 'b' ), + ft_encoding_unicode = FT_MAKE_TAG( 'u', 'n', 'i', 'c' ), + ft_encoding_latin_2 = FT_MAKE_TAG( 'l', 'a', 't', '2' ), + ft_encoding_sjis = FT_MAKE_TAG( 's', 'j', 'i', 's' ), + ft_encoding_gb2312 = FT_MAKE_TAG( 'g', 'b', ' ', ' ' ), + ft_encoding_big5 = FT_MAKE_TAG( 'b', 'i', 'g', '5' ), + ft_encoding_wansung = FT_MAKE_TAG( 'w', 'a', 'n', 's' ), + ft_encoding_johab = FT_MAKE_TAG( 'j', 'o', 'h', 'a' ), + + ft_encoding_adobe_standard = FT_MAKE_TAG( 'A', 'D', 'O', 'B' ), + ft_encoding_adobe_expert = FT_MAKE_TAG( 'A', 'D', 'B', 'E' ), + ft_encoding_adobe_custom = FT_MAKE_TAG( 'A', 'D', 'B', 'C' ), + + ft_encoding_apple_roman = FT_MAKE_TAG( 'a', 'r', 'm', 'n' ) + + /* other encodings might be defined in the future */ + +} FT_Encoding; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_CharMapRec */ +/* */ +/* */ +/* The base charmap class. */ +/* */ +/* */ +/* face :: A handle to the parent face object. */ +/* */ +/* flags :: A set of bit flags used to describe the charmap. */ +/* Each bit indicates that a given encoding is */ +/* supported. */ +/* */ +/* platform_id :: An ID number describing the platform for the */ +/* following encoding ID. This comes directly from */ +/* the TrueType specification and should be emulated */ +/* for other formats. */ +/* */ +/* encoding_id :: A platform specific encoding number. This also */ +/* comes from the TrueType specification and should be */ +/* emulated similarly. */ +/* */ +/* */ +/* We STRONGLY recommmend emulating a Unicode charmap for drivers */ +/* that do not support TrueType or OpenType. */ +/* */ +typedef struct FT_CharMapRec_ +{ + FT_Face face; + FT_Encoding encoding; + FT_UShort platform_id; + FT_UShort encoding_id; + +} FT_CharMapRec; + + +/*************************************************************************/ +/*************************************************************************/ +/* */ +/* B A S E O B J E C T C L A S S E S */ +/* */ +/*************************************************************************/ +/*************************************************************************/ + +/*************************************************************************/ +/* */ +/* FreeType base face class */ +/* */ +/* */ +/* FT_FaceRec */ +/* */ +/* */ +/* FreeType root face class structure. A face object models the */ +/* resolution and point-size independent data found in a font file. */ +/* */ +/* */ +/* num_faces :: In the case where the face is located in a */ +/* collection (i.e., a resource which embeds */ +/* several faces), this is the total number of */ +/* faces found in the resource. 1 by default. */ +/* */ +/* face_index :: The index of the face in its resource. */ +/* Usually, this is 0 for all normal font */ +/* formats. It can be more in the case of */ +/* collections (which embed several fonts in a */ +/* single resource/file). */ +/* */ +/* face_flags :: A set of bit flags that give important */ +/* information about the face; see the */ +/* FT_FACE_FLAG_XXX macros for details. */ +/* */ +/* style_flags :: A set of bit flags indicating the style of */ +/* the face (i.e., italic, bold, underline, */ +/* etc). */ +/* */ +/* num_glyphs :: The total number of glyphs in the face. */ +/* */ +/* family_name :: The face's family name. This is an ASCII */ +/* string, usually in English, which describes */ +/* the typeface's family (like `Times New */ +/* Roman', `Bodoni', `Garamond', etc). This */ +/* is a least common denominator used to list */ +/* fonts. Some formats (TrueType & OpenType) */ +/* provide localized and Unicode versions of */ +/* this string. Applications should use the */ +/* format specific interface to access them. */ +/* */ +/* style_name :: The face's style name. This is an ASCII */ +/* string, usually in English, which describes */ +/* the typeface's style (like `Italic', */ +/* `Bold', `Condensed', etc). Not all font */ +/* formats provide a style name, so this field */ +/* is optional, and can be set to NULL. As */ +/* for `family_name', some formats provide */ +/* localized/Unicode versions of this string. */ +/* Applications should use the format specific */ +/* interface to access them. */ +/* */ +/* num_fixed_sizes :: The number of fixed sizes available in this */ +/* face. This should be set to 0 for scalable */ +/* fonts, unless its resource includes a */ +/* complete set of glyphs (called a `strike') */ +/* for the specified size. */ +/* */ +/* available_sizes :: An array of sizes specifying the available */ +/* bitmap/graymap sizes that are contained in */ +/* in the font resource. Should be set to */ +/* NULL if the field `num_fixed_sizes' is set */ +/* to 0. */ +/* */ +/* num_charmaps :: The total number of character maps in the */ +/* face. */ +/* */ +/* charmaps :: A table of pointers to the face's charmaps */ +/* Used to scan the list of available charmaps */ +/* this table might change after a call to */ +/* FT_Attach_File/Stream (e.g. when it used */ +/* to hook and additional encoding/CMap to */ +/* the face object). */ +/* */ +/* generic :: A field reserved for client uses. See the */ +/* FT_Generic type description. */ +/* */ +/* bbox :: The font bounding box. Coordinates are */ +/* expressed in font units (see units_per_EM). */ +/* The box is large enough to contain any */ +/* glyph from the font. Thus, bbox.yMax can */ +/* be seen as the `maximal ascender', */ +/* bbox.yMin as the `minimal descender', and */ +/* the maximal glyph width is given by */ +/* `bbox.xMax-bbox.xMin' (not to be confused */ +/* with the maximal _advance_width_). Only */ +/* relevant for scalable formats. */ +/* */ +/* units_per_EM :: The number of font units per EM square for */ +/* this face. This is typically 2048 for */ +/* TrueType fonts, 1000 for Type1 fonts, and */ +/* should be set to the (unrealistic) value 1 */ +/* for fixed-sizes fonts. Only relevant for */ +/* scalable formats. */ +/* */ +/* ascender :: The face's ascender is the vertical */ +/* distance from the baseline to the topmost */ +/* point of any glyph in the face. This */ +/* field's value is positive, expressed in */ +/* font units. Some font designs use a value */ +/* different from `bbox.yMax'. Only relevant */ +/* for scalable formats. */ +/* */ +/* descender :: The face's descender is the vertical */ +/* distance from the baseline to the */ +/* bottommost point of any glyph in the face. */ +/* This field's value is positive, expressed */ +/* in font units. Some font designs use a */ +/* value different from `-bbox.yMin'. Only */ +/* relevant for scalable formats. */ +/* */ +/* height :: The face's height is the vertical distance */ +/* from one baseline to the next when writing */ +/* several lines of text. Its value is always */ +/* positive, expressed in font units. The */ +/* value can be computed as */ +/* `ascender+descender+line_gap' where the */ +/* value of `line_gap' is also called */ +/* `external leading'. Only relevant for */ +/* scalable formats. */ +/* */ +/* max_advance_width :: The maximal advance width, in font units, */ +/* for all glyphs in this face. This can be */ +/* used to make word wrapping computations */ +/* faster. Only relevant for scalable */ +/* formats. */ +/* */ +/* max_advance_height :: The maximal advance height, in font units, */ +/* for all glyphs in this face. This is only */ +/* relevant for vertical layouts, and should */ +/* be set to the `height' for fonts that do */ +/* not provide vertical metrics. Only */ +/* relevant for scalable formats. */ +/* */ +/* underline_position :: The position, in font units, of the */ +/* underline line for this face. It's the */ +/* center of the underlining stem. Only */ +/* relevant for scalable formats. */ +/* */ +/* underline_thickness :: The thickness, in font units, of the */ +/* underline for this face. Only relevant for */ +/* scalable formats. */ +/* */ +/* driver :: A handle to the face's parent driver */ +/* object. */ +/* */ +/* memory :: A handle to the face's parent memory */ +/* object. Used for the allocation of */ +/* subsequent objects. */ +/* */ +/* stream :: A handle to the face's stream. */ +/* */ +/* glyph :: The face's associated glyph slot(s). This */ +/* object is created automatically with a new */ +/* face object. However, certain kinds of */ +/* applications (mainly tools like converters) */ +/* can need more than one slot to ease their */ +/* task. */ +/* */ +/* sizes_list :: The list of child sizes for this face. */ +/* */ +/* max_points :: The maximal number of points used to store */ +/* the vectorial outline of any glyph in this */ +/* face. If this value cannot be known in */ +/* advance, or if the face isn't scalable, */ +/* this should be set to 0. Only relevant for */ +/* scalable formats. */ +/* */ +/* max_contours :: The maximal number of contours used to */ +/* store the vectorial outline of any glyph in */ +/* this face. If this value cannot be known */ +/* in advance, or if the face isn't scalable, */ +/* this should be set to 0. Only relevant for */ +/* scalable formats. */ +/* */ +/* transform_matrix :: A 2x2 matrix of 16.16 coefficients used */ +/* to transform glyph outlines after they are */ +/* loaded from the font. Only used by the */ +/* convenience functions. */ +/* */ +/* transform_delta :: A translation vector used to transform */ +/* glyph outlines after they are loaded from */ +/* the font. Only used by the convenience */ +/* functions. */ +/* */ +/* transform_flags :: Some flags used to classify the transform. */ +/* Only used by the convenience functions. */ +/* */ +typedef struct FT_FaceRec_ +{ + FT_Long num_faces; + FT_Long face_index; + + FT_Long face_flags; + FT_Long style_flags; + + FT_Long num_glyphs; + + FT_String* family_name; + FT_String* style_name; + + FT_Int num_fixed_sizes; + FT_Bitmap_Size* available_sizes; + + /* the face's table of available charmaps */ + FT_Int num_charmaps; + FT_CharMap* charmaps; + + FT_Generic generic; + + /* the following are only relevant for scalable outlines */ + FT_BBox bbox; + + FT_UShort units_per_EM; + FT_Short ascender; + FT_Short descender; + FT_Short height; + + FT_Short max_advance_width; + FT_Short max_advance_height; + + FT_Short underline_position; + FT_Short underline_thickness; + + FT_GlyphSlot glyph; + FT_Size size; + + /************************************************************/ + /* The following fields should be considered private and */ + /* rarely, if ever, used directly by client applications. */ + + FT_Driver driver; + FT_Memory memory; + FT_Stream stream; + + FT_CharMap charmap; + FT_ListRec sizes_list; + + FT_Generic autohint; + void* extensions; + + FT_UShort max_points; + FT_Short max_contours; + + FT_Matrix transform_matrix; + FT_Vector transform_delta; + FT_Int transform_flags; + +} FT_FaceRec; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_FACE_FLAG_SCALABLE */ +/* */ +/* */ +/* A bit-field constant, used to indicate that a given face provides */ +/* vectorial outlines (i.e., TrueType or Type1). This doesn't */ +/* prevent embedding of bitmap strikes though, i.e., a given face can */ +/* have both this bit set, and a `num_fixed_sizes' property > 0. */ +/* */ +#define FT_FACE_FLAG_SCALABLE 1 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_FACE_FLAG_FIXED_SIZES */ +/* */ +/* */ +/* A bit-field constant, used to indicate that a given face contains */ +/* `fixed sizes', i.e., bitmap strikes for some given pixel sizes. */ +/* See the `num_fixed_sizes' and `available_sizes' face properties */ +/* for more information. */ +/* */ +#define FT_FACE_FLAG_FIXED_SIZES 2 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_FACE_FLAG_FIXED_WIDTH */ +/* */ +/* */ +/* A bit-field constant, used to indicate that a given face contains */ +/* fixed-width characters (like Courier, Lucida, MonoType, etc.). */ +/* */ +#define FT_FACE_FLAG_FIXED_WIDTH 4 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_FACE_FLAG_SFNT */ +/* */ +/* */ +/* A bit-field constant, used to indicate that a given face uses the */ +/* `sfnt' storage fomat. For now, this means TrueType or OpenType. */ +/* */ +#define FT_FACE_FLAG_SFNT 8 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_FACE_FLAG_HORIZONTAL */ +/* */ +/* */ +/* A bit-field constant, used to indicate that a given face contains */ +/* horizontal glyph metrics. This should be set for all common */ +/* formats, but who knows. */ +/* */ +#define FT_FACE_FLAG_HORIZONTAL 0x10 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_FACE_FLAG_VERTICAL */ +/* */ +/* */ +/* A bit-field constant, used to indicate that a given face contains */ +/* vertical glyph metrics. If not set, the glyph loader will */ +/* synthetize vertical metrics itself to help display vertical text */ +/* correctly. */ +/* */ +#define FT_FACE_FLAG_VERTICAL 0x20 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_FACE_FLAG_KERNING */ +/* */ +/* */ +/* A bit-field constant, used to indicate that a given face contains */ +/* kerning information. When set, this information can be retrieved */ +/* through the function FT_Get_Kerning(). Note that when unset, this */ +/* function will always return the kerning vector (0,0). */ +/* */ +#define FT_FACE_FLAG_KERNING 0x40 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_FACE_FLAG_FAST_GLYPHS */ +/* */ +/* */ +/* A bit-field constant, used to indicate that the glyphs in a given */ +/* font can be retrieved very quickly, and that a glyph cache is thus */ +/* not necessary for any of its child size objects. */ +/* */ +/* This flag should really be set for fixed-size formats like FNT, */ +/* where each glyph bitmap is available directly in binary form */ +/* without any kind of compression. */ +/* */ +#define FT_FACE_FLAG_FAST_GLYPHS 0x80 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_FACE_FLAG_MULTIPLE_MASTERS */ +/* */ +/* */ +/* A bit-field constant, used to indicate that the font contains */ +/* multiple masters and is capable of interpolating between them. */ +/* */ +#define FT_FACE_FLAG_MULTIPLE_MASTERS 0x100 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_FACE_FLAG_GLYPH_NAMES */ +/* */ +/* */ +/* A bit-field constant, used to indicate that the font contains */ +/* glyph names that can be retrieved through FT_Get_Glyph_Name(). */ +/* */ +#define FT_FACE_FLAG_GLYPH_NAMES 0x200 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_FACE_FLAG_EXTERNAL_STREAM */ +/* */ +/* */ +/* This bit field is used internally by FreeType to indicate that */ +/* a face's stream was provided by the client application and should */ +/* not be destroyed by FT_Done_Face(). */ +/* */ +#define FT_FACE_FLAG_EXTERNAL_STREAM 0x4000 + + +#define FT_HAS_HORIZONTAL( face ) \ + ( face->face_flags & FT_FACE_FLAG_HORIZONTAL ) +#define FT_HAS_VERTICAL( face ) \ + ( face->face_flags & FT_FACE_FLAG_VERTICAL ) +#define FT_HAS_KERNING( face ) \ + ( face->face_flags & FT_FACE_FLAG_KERNING ) +#define FT_IS_SCALABLE( face ) \ + ( face->face_flags & FT_FACE_FLAG_SCALABLE ) +#define FT_IS_SFNT( face ) \ + ( face->face_flags & FT_FACE_FLAG_SFNT ) +#define FT_IS_FIXED_WIDTH( face ) \ + ( face->face_flags & FT_FACE_FLAG_FIXED_WIDTH ) +#define FT_HAS_FIXED_SIZES( face ) \ + ( face->face_flags & FT_FACE_FLAG_FIXED_SIZES ) +#define FT_HAS_FAST_GLYPHS( face ) \ + ( face->face_flags & FT_FACE_FLAG_FAST_GLYPHS ) +#define FT_HAS_GLYPH_NAMES( face ) \ + ( face->face_flags & FT_FACE_FLAG_GLYPH_NAMES ) + +#define FT_HAS_MULTIPLE_MASTERS( face ) \ + ( face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS ) + + +/*************************************************************************/ +/* */ +/* */ +/* FT_STYLE_FLAG_ITALIC */ +/* */ +/* */ +/* A bit-field constant, used to indicate that a given face is */ +/* italicized. */ +/* */ +#define FT_STYLE_FLAG_ITALIC 1 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_STYLE_FLAG_BOLD */ +/* */ +/* */ +/* A bit-field constant, used to indicate that a given face is */ +/* emboldened. */ +/* */ +#define FT_STYLE_FLAG_BOLD 2 + + +/*************************************************************************/ +/* */ +/* FreeType base size metrics */ +/* */ +/* */ +/* FT_Size_Metrics */ +/* */ +/* */ +/* The size metrics structure returned scaled important distances for */ +/* a given size object. */ +/* */ +/* */ +/* x_ppem :: The character width, expressed in integer pixels. */ +/* This is the width of the EM square expressed in */ +/* pixels, hence the term `ppem' (pixels per EM). */ +/* */ +/* y_ppem :: The character height, expressed in integer pixels. */ +/* This is the height of the EM square expressed in */ +/* pixels, hence the term `ppem' (pixels per EM). */ +/* */ +/* x_scale :: A simple 16.16 fixed point format coefficient used */ +/* to scale horizontal distances expressed in font */ +/* units to fractional (26.6) pixel coordinates. */ +/* */ +/* y_scale :: A simple 16.16 fixed point format coefficient used */ +/* to scale vertical distances expressed in font */ +/* units to fractional (26.6) pixel coordinates. */ +/* */ +/* x_resolution :: The horizontal device resolution for this size */ +/* object, expressed in integer dots per inches */ +/* (dpi). As a convention, fixed font formats set */ +/* this value to 72. */ +/* */ +/* y_resolution :: The vertical device resolution for this size */ +/* object, expressed in integer dots per inches */ +/* (dpi). As a convention, fixed font formats set */ +/* this value to 72. */ +/* */ +/* ascender :: The ascender, expressed in 26.6 fixed point */ +/* pixels. Always positive. */ +/* */ +/* descender :: The descender, expressed in 26.6 fixed point */ +/* pixels. Always positive. */ +/* */ +/* height :: The text height, expressed in 26.6 fixed point */ +/* pixels. Always positive. */ +/* */ +/* max_advance :: Maximum horizontal advance, expressed in 26.6 */ +/* fixed point pixels. Always positive. */ +/* */ +/* */ +/* The values of `ascender', `descender', and `height' are only the */ +/* scaled versions of `face->ascender', `face->descender', and */ +/* `face->height'. */ +/* */ +/* Unfortunately, due to glyph hinting, these values might not be */ +/* exact for certain fonts, they thus must be treated as unreliable */ +/* with an error margin of at least one pixel! */ +/* */ +/* Indeed, the only way to get the exact pixel ascender and descender */ +/* is to render _all_ glyphs. As this would be a definite */ +/* performance hit, it is up to client applications to perform such */ +/* computations. */ +/* */ +typedef struct FT_Size_Metrics_ +{ + FT_UShort x_ppem; /* horizontal pixels per EM */ + FT_UShort y_ppem; /* vertical pixels per EM */ + + FT_Fixed x_scale; /* two scales used to convert font units */ + FT_Fixed y_scale; /* to 26.6 frac. pixel coordinates.. */ + + FT_Pos ascender; /* ascender in 26.6 frac. pixels */ + FT_Pos descender; /* descender in 26.6 frac. pixels */ + FT_Pos height; /* text height in 26.6 frac. pixels */ + FT_Pos max_advance; /* max horizontal advance, in 26.6 pixels */ + +} FT_Size_Metrics; + + +/*************************************************************************/ +/* */ +/* FreeType base size class */ +/* */ +/* */ +/* FT_SizeRec */ +/* */ +/* */ +/* FreeType root size class structure. A size object models the */ +/* resolution and pointsize dependent data of a given face. */ +/* */ +/* */ +/* face :: Handle to the parent face object. */ +/* */ +/* generic :: A typeless pointer, which is unused by the FreeType */ +/* library or any of its drivers. It can be used by */ +/* client applications to link their own data to each size */ +/* object. */ +/* */ +/* metrics :: Metrics for this size object. This field is read-only. */ +/* */ +typedef struct FT_SizeRec_ +{ + FT_Face face; /* parent face object */ + FT_Generic generic; /* generic pointer for client uses */ + FT_Size_Metrics metrics; /* size metrics */ + +} FT_SizeRec; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_SubGlyph */ +/* */ +/* */ +/* The subglyph structure is an internal object used to describe */ +/* subglyphs (for example, in the case of composites). */ +/* */ +/* */ +/* The subglyph implementation is not part of the high-level API, */ +/* hence the forward structure declaration. */ +/* */ +typedef struct FT_SubGlyph_ FT_SubGlyph; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_GlyphLoader */ +/* */ +/* */ +/* The glyph loader is an internal object used to load several glyphs */ +/* together (for example, in the case of composites). */ +/* */ +/* */ +/* The glyph loader implementation is not part of the high-level API, */ +/* hence the forward structure declaration. */ +/* */ +typedef struct FT_GlyphLoader_ FT_GlyphLoader; + + +/*************************************************************************/ +/* */ +/* FreeType Glyph Slot base class */ +/* */ +/* */ +/* FT_GlyphSlotRec */ +/* */ +/* */ +/* FreeType root glyph slot class structure. A glyph slot is a */ +/* container where individual glyphs can be loaded, be they */ +/* vectorial or bitmap/graymaps. */ +/* */ +/* */ +/* library :: A handle to the FreeType library instance */ +/* this slot belongs to. */ +/* */ +/* face :: A handle to the parent face object. */ +/* */ +/* next :: In some cases (like some font tools), several */ +/* glyph slots per face object can be a good */ +/* thing. As this is rare, the glyph slots are */ +/* listed through a direct, single-linked list */ +/* using its `next' field. */ +/* */ +/* generic :: A typeless pointer which is unused by the */ +/* FreeType library or any of its drivers. It */ +/* can be used by client applications to link */ +/* their own data to each size object. */ +/* */ +/* metrics :: The metrics of the last loaded glyph in the */ +/* slot. The returned values depend on the last */ +/* load flags (see the FT_Load_Glyph() API */ +/* function) and can be expressed either in 26.6 */ +/* fractional pixels or font units. */ +/* */ +/* Note that even when the glyph image is */ +/* transformed, the metrics are not. */ +/* */ +/* linearHoriAdvance :: For scalable formats only, this field holds */ +/* the linearly scaled horizontal advance width */ +/* for the glyph (i.e. the scaled and unhinted */ +/* value of the hori advance). This can be */ +/* important to perform correct WYSIWYG layout */ +/* */ +/* Note that this value is expressed by default */ +/* in 16.16 pixels. However, when the glyph is */ +/* loaded with the FT_LOAD_UNSCALED_LINEAR flag, */ +/* this field contains simply the value of the */ +/* advance in original font units. */ +/* */ +/* linearVertAdvance :: For scalable formats only, this field holds */ +/* the linearly scaled vertical advance height */ +/* for the glyph. See linearHoriAdvance for */ +/* comments. */ +/* */ +/* advance :: This is the transformed advance width for the */ +/* glyph. */ +/* */ +/* format :: This field indicates the format of the image */ +/* contained in the glyph slot. Typically */ +/* ft_glyph_format_bitmap, */ +/* ft_glyph_format_outline, and */ +/* ft_glyph_format_composite, but others are */ +/* possible. */ +/* */ +/* bitmap :: This field is used as a bitmap descriptor */ +/* when the slot format is */ +/* ft_glyph_format_bitmap. Note that the */ +/* address and content of the bitmap buffer can */ +/* change between calls of FT_Load_Glyph() and a */ +/* few other functions. */ +/* */ +/* bitmap_left :: This is the bitmap's left bearing expressed */ +/* in integer pixels. Of course, this is only */ +/* valid if the format is */ +/* ft_glyph_format_bitmap. */ +/* */ +/* bitmap_top :: This is the bitmap's top bearing expressed in */ +/* integer pixels. Remember that this is the */ +/* distance from the baseline to the top-most */ +/* glyph scanline, upwards y-coordinates being */ +/* *positive*. */ +/* */ +/* outline :: The outline descriptor for the current glyph */ +/* image if its format is */ +/* ft_glyph_bitmap_outline. */ +/* */ +/* num_subglyphs :: The number of subglyphs in a composite glyph. */ +/* This format is only valid for the composite */ +/* glyph format, that should normally only be */ +/* loaded with the FT_LOAD_NO_RECURSE flag. */ +/* */ +/* subglyphs :: An array of subglyph descriptors for */ +/* composite glyphs. There are `num_subglyphs' */ +/* elements in there. */ +/* */ +/* control_data :: Certain font drivers can also return the */ +/* control data for a given glyph image (e.g. */ +/* TrueType bytecode, Type 1 charstrings, etc.). */ +/* This field is a pointer to such data. */ +/* */ +/* control_len :: This is the length in bytes of the control */ +/* data. */ +/* */ +/* other :: Really wicked formats can use this pointer to */ +/* present their own glyph image to client apps. */ +/* Note that the app will need to know about the */ +/* image format. */ +/* */ +/* loader :: This is a private object for the glyph slot. */ +/* Do not touch this. */ +/* */ +/* */ +/* If FT_Load_Glyph() is called with default flags (FT_LOAD_DEFAULT), */ +/* the glyph image is loaded in the glyph slot in its native format */ +/* (e.g. a vectorial outline for TrueType and Type 1 formats). */ +/* */ +/* This image can later be converted into a bitmap by calling */ +/* FT_Render_Glyph(). This function finds the current renderer for */ +/* the native image's format then invokes it. */ +/* */ +/* The renderer is in charge of transforming the native image through */ +/* the slot's face transformation fields, then convert it into a */ +/* bitmap that is returned in `slot->bitmap'. */ +/* */ +/* Note that `slot->bitmap_left' and `slot->bitmap_top' are also used */ +/* to specify the position of the bitmap relative to the current pen */ +/* position (e.g. coordinates [0,0] on the baseline). Of course, */ +/* `slot->format' is also changed to `ft_glyph_format_bitmap' . */ +/* */ +typedef struct FT_GlyphSlotRec_ +{ + FT_Library library; + FT_Face face; + FT_GlyphSlot next; + FT_UInt flags; + FT_Generic generic; + + FT_Glyph_Metrics metrics; + FT_Fixed linearHoriAdvance; + FT_Fixed linearVertAdvance; + FT_Vector advance; + + FT_Glyph_Format format; + + FT_Bitmap bitmap; + FT_Int bitmap_left; + FT_Int bitmap_top; + + FT_Outline outline; + + FT_UInt num_subglyphs; + FT_SubGlyph* subglyphs; + + void* control_data; + long control_len; + + void* other; + + /* private fields */ + FT_GlyphLoader* loader; + +} FT_GlyphSlotRec; + + +/*************************************************************************/ +/*************************************************************************/ +/* */ +/* F U N C T I O N S */ +/* */ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Init_FreeType */ +/* */ +/* */ +/* Initializes a new FreeType library object. The set of drivers */ +/* that are registered by this function is determined at build time. */ +/* */ +/* */ +/* library :: A handle to a new library object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Init_FreeType( FT_Library * library ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Done_FreeType */ +/* */ +/* */ +/* Destroys a given FreeType library object and all of its childs, */ +/* including resources, drivers, faces, sizes, etc. */ +/* */ +/* */ +/* library :: A handle to the target library object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Done_FreeType( FT_Library library ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Open_Flags */ +/* */ +/* */ +/* An enumeration used to list the bit flags used within */ +/* FT_Open_Args(). */ +/* */ +/* */ +/* ft_open_memory :: This is a memory-based stream. */ +/* */ +/* ft_open_stream :: Copy the stream from the `stream' field. */ +/* */ +/* ft_open_pathname :: Create a new input stream from a C pathname. */ +/* */ +/* ft_open_driver :: Use the `driver' field. */ +/* */ +/* ft_open_params :: Use the `num_params' & `params' field. */ +/* */ +typedef enum +{ + ft_open_memory = 1, + ft_open_stream = 2, + ft_open_pathname = 4, + ft_open_driver = 8, + ft_open_params = 16 + +} FT_Open_Flags; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Parameter */ +/* */ +/* */ +/* A simple structure used to pass more or less generic parameters */ +/* to FT_Open_Face(). */ +/* */ +/* */ +/* tag :: A 4-byte identification tag. */ +/* */ +/* data :: A pointer to the parameter data. */ +/* */ +/* */ +/* The id and function of parameters are driver-specific. */ +/* */ +typedef struct FT_Parameter_ +{ + FT_ULong tag; + FT_Pointer data; + +} FT_Parameter; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Open_Args */ +/* */ +/* */ +/* A structure used to indicate how to open a new font file/stream. */ +/* A pointer to such a structure can be used as a parameter for the */ +/* functions FT_Open_Face() & FT_Attach_Stream(). */ +/* */ +/* */ +/* flags :: A set of bit flags indicating how to use the */ +/* structure. */ +/* */ +/* memory_base :: The first byte of the file in memory. */ +/* */ +/* memory_size :: The size in bytes of the file in memory. */ +/* */ +/* pathname :: A pointer to an 8-bit file pathname. */ +/* */ +/* stream :: A handle to a source stream object. */ +/* */ +/* driver :: This field is exclusively used by FT_Open_Face(); */ +/* it simply specifies the font driver to use to open */ +/* the face. If set to 0, FreeType will try to load */ +/* the face with each one of the drivers in its list. */ +/* */ +/* num_params :: The number of extra parameters. */ +/* */ +/* params :: Extra parameters passed to the font driver when */ +/* opening a new face. */ +/* */ +/* */ +/* `stream_type' determines which fields are used to create a new */ +/* input stream. */ +/* */ +/* If it is `ft_stream_memory', a new memory-based stream will be */ +/* created using the memory block specified by `memory_base' and */ +/* `memory_size'. */ +/* */ +/* If it is `ft_stream_pathname', a new stream will be created with */ +/* the `pathname' field, calling the system-specific FT_New_Stream() */ +/* function. */ +/* */ +/* If is is `ft_stream_copy', then the content of `stream' will be */ +/* copied to a new input stream object. The object will be closed */ +/* and destroyed when the face is destroyed itself. Note that this */ +/* means that you should not close the stream before the library */ +/* does! */ +/* */ +typedef struct FT_Open_Args_ +{ + FT_Open_Flags flags; + FT_Byte* memory_base; + FT_Long memory_size; + FT_String* pathname; + FT_Stream stream; + FT_Module driver; + FT_Int num_params; + FT_Parameter* params; + +} FT_Open_Args; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_New_Face */ +/* */ +/* */ +/* Creates a new face object from a given resource and typeface index */ +/* using a pathname to the font file. */ +/* */ +/* */ +/* library :: A handle to the library resource. */ +/* */ +/* */ +/* pathname :: A path to the font file. */ +/* */ +/* face_index :: The index of the face within the resource. The */ +/* first face has index 0. */ +/* */ +/* aface :: A handle to a new face object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* Unlike FreeType 1.x, this function automatically creates a glyph */ +/* slot for the face object which can be accessed directly through */ +/* `face->glyph'. */ +/* */ +/* Note that additional slots can be added to each face with the */ +/* FT_New_GlyphSlot() API function. Slots are linked in a single */ +/* list through their `next' field. */ +/* */ +/* FT_New_Face() can be used to determine and/or check the font */ +/* format of a given font resource. If the `face_index' field is */ +/* negative, the function will _not_ return any face handle in */ +/* `*face'. Its return value should be 0 if the resource is */ +/* recognized, or non-zero if not. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_New_Face( FT_Library library, + const char* filepathname, + FT_Long face_index, + FT_Face * face ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_New_Memory_Face */ +/* */ +/* */ +/* Creates a new face object from a given resource and typeface index */ +/* using a font file already loaded into memory. */ +/* */ +/* */ +/* library :: A handle to the library resource. */ +/* */ +/* */ +/* file_base :: A pointer to the beginning of the font data. */ +/* */ +/* file_size :: The size of the memory chunk used by the font data. */ +/* */ +/* face_index :: The index of the face within the resource. The */ +/* first face has index 0. */ +/* */ +/* face :: A handle to a new face object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* Unlike FreeType 1.x, this function automatically creates a glyph */ +/* slot for the face object which can be accessed directly through */ +/* `face->glyph'. */ +/* */ +/* Note that additional slots can be added to each face with the */ +/* FT_New_GlyphSlot() API function. Slots are linked in a single */ +/* list through their `next' field. */ +/* */ +/* FT_New_Memory_Face() can be used to determine and/or check the */ +/* font format of a given font resource. If the `face_index' field */ +/* is negative, the function will _not_ return any face handle in */ +/* `*face'. Its return value should be 0 if the resource is */ +/* recognized, or non-zero if not. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_New_Memory_Face( FT_Library library, + FT_Byte * file_base, + FT_Long file_size, + FT_Long face_index, + FT_Face * face ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Open_Face */ +/* */ +/* */ +/* Opens a face object from a given resource and typeface index using */ +/* an `FT_Open_Args' structure. If the face object doesn't exist, it */ +/* will be created. */ +/* */ +/* */ +/* library :: A handle to the library resource. */ +/* */ +/* */ +/* args :: A pointer to an `FT_Open_Args' structure which must */ +/* be filled by the caller. */ +/* */ +/* face_index :: The index of the face within the resource. The */ +/* first face has index 0. */ +/* */ +/* aface :: A handle to a new face object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* Unlike FreeType 1.x, this function automatically creates a glyph */ +/* slot for the face object which can be accessed directly through */ +/* `face->glyph'. */ +/* */ +/* Note that additional slots can be added to each face with the */ +/* FT_New_GlyphSlot() API function. Slots are linked in a single */ +/* list through their `next' field. */ +/* */ +/* FT_Open_Face() can be used to determine and/or check the font */ +/* format of a given font resource. If the `face_index' field is */ +/* negative, the function will _not_ return any face handle in */ +/* `*face'. Its return value should be 0 if the resource is */ +/* recognized, or non-zero if not. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Open_Face( FT_Library library, + FT_Open_Args * args, + FT_Long face_index, + FT_Face * face ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Attach_File */ +/* */ +/* */ +/* `Attaches' a given font file to an existing face. This is usually */ +/* to read additional information for a single face object. For */ +/* example, it is used to read the AFM files that come with Type 1 */ +/* fonts in order to add kerning data and other metrics. */ +/* */ +/* */ +/* face :: The target face object. */ +/* */ +/* */ +/* filepathname :: An 8-bit pathname naming the `metrics' file. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* If your font file is in memory, or if you want to provide your */ +/* own input stream object, use FT_Attach_Stream(). */ +/* */ +/* The meaning of the `attach' action (i.e., what really happens when */ +/* the new file is read) is not fixed by FreeType itself. It really */ +/* depends on the font format (and thus the font driver). */ +/* */ +/* Client applications are expected to know what they are doing */ +/* when invoking this function. Most drivers simply do not implement */ +/* file attachments. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Attach_File( FT_Face face, + const char* filepathname ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Attach_Stream */ +/* */ +/* */ +/* This function is similar to FT_Attach_File() with the exception */ +/* that it reads the attachment from an arbitrary stream. */ +/* */ +/* */ +/* face :: The target face object. */ +/* */ +/* parameters :: A pointer to an FT_Open_Args structure used to */ +/* describe the input stream to FreeType. */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* The meaning of the `attach' (i.e. what really happens when the */ +/* new file is read) is not fixed by FreeType itself. It really */ +/* depends on the font format (and thus the font driver). */ +/* */ +/* Client applications are expected to know what they are doing */ +/* when invoking this function. Most drivers simply do not implement */ +/* file attachments. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Attach_Stream( FT_Face face, + FT_Open_Args * parameters ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Done_Face */ +/* */ +/* */ +/* Discards a given face object, as well as all of its child slots */ +/* and sizes. */ +/* */ +/* */ +/* face :: A handle to a target face object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Done_Face( FT_Face face ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Set_Char_Size */ +/* */ +/* */ +/* Sets the character dimensions of a given face object. The */ +/* `char_width' and `char_height' values are used for the width and */ +/* height, respectively, expressed in 26.6 fractional points. */ +/* */ +/* If the horizontal or vertical resolution values are zero, a */ +/* default value of 72dpi is used. Similarly, if one of the */ +/* character dimensions is zero, its value is set equal to the other. */ +/* */ +/* */ +/* size :: A handle to a target size object. */ +/* */ +/* */ +/* char_width :: The character width, in 26.6 fractional points. */ +/* */ +/* char_height :: The character height, in 26.6 fractional */ +/* points. */ +/* */ +/* horz_resolution :: The horizontal resolution. */ +/* */ +/* vert_resolution :: The vertical resolution. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* When dealing with fixed-size faces (i.e., non-scalable formats), */ +/* use the function FT_Set_Pixel_Sizes(). */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Set_Char_Size( FT_Face face, + FT_F26Dot6 char_width, + FT_F26Dot6 char_height, + FT_UInt horz_resolution, + FT_UInt vert_resolution ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Set_Pixel_Sizes */ +/* */ +/* */ +/* Sets the character dimensions of a given face object. The width */ +/* and height are expressed in integer pixels. */ +/* */ +/* If one of the character dimensions is zero, its value is set equal */ +/* to the other. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* */ +/* */ +/* pixel_width :: The character width, in integer pixels. */ +/* */ +/* pixel_height :: The character height, in integer pixels. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Set_Pixel_Sizes( FT_Face face, + FT_UInt pixel_width, + FT_UInt pixel_height ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Load_Glyph */ +/* */ +/* */ +/* A function used to load a single glyph within a given glyph slot, */ +/* for a given size. */ +/* */ +/* */ +/* face :: A handle to the target face object where the glyph */ +/* will be loaded. */ +/* */ +/* glyph_index :: The index of the glyph in the font file. */ +/* */ +/* load_flags :: A flag indicating what to load for this glyph. The */ +/* FT_LOAD_XXX constants can be used to control the */ +/* glyph loading process (e.g., whether the outline */ +/* should be scaled, whether to load bitmaps or not, */ +/* whether to hint the outline, etc). */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* If the glyph image is not a bitmap, and if the bit flag */ +/* FT_LOAD_IGNORE_TRANSFORM is unset, the glyph image will be */ +/* transformed with the information passed to a previous call to */ +/* FT_Set_Transform. */ +/* */ +/* Note that this also transforms the `face.glyph.advance' field, but */ +/* *not* the values in `face.glyph.metrics'. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Load_Glyph( FT_Face face, + FT_UInt glyph_index, + FT_Int load_flags ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Load_Char */ +/* */ +/* */ +/* A function used to load a single glyph within a given glyph slot, */ +/* for a given size, according to its character code. */ +/* */ +/* */ +/* face :: A handle to a target face object where the glyph */ +/* will be loaded. */ +/* */ +/* char_code :: The glyph's character code, according to the */ +/* current charmap used in the face. */ +/* */ +/* load_flags :: A flag indicating what to load for this glyph. The */ +/* FT_LOAD_XXX constants can be used to control the */ +/* glyph loading process (e.g., whether the outline */ +/* should be scaled, whether to load bitmaps or not, */ +/* whether to hint the outline, etc). */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* If the face has no current charmap, or if the character code */ +/* is not defined in the charmap, this function will return an */ +/* error. */ +/* */ +/* If the glyph image is not a bitmap, and if the bit flag */ +/* FT_LOAD_IGNORE_TRANSFORM is unset, the glyph image will be */ +/* transformed with the information passed to a previous call to */ +/* FT_Set_Transform(). */ +/* */ +/* Note that this also transforms the `face.glyph.advance' field, but */ +/* *not* the values in `face.glyph.metrics'. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Load_Char( FT_Face face, + FT_ULong char_code, + FT_Int load_flags ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_LOAD_NO_SCALE */ +/* */ +/* */ +/* A bit field constant, used with FT_Load_Glyph() to indicate that */ +/* the vector outline being loaded should not be scaled to 26.6 */ +/* fractional pixels, but kept in notional units. */ +/* */ +#define FT_LOAD_NO_SCALE 1 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_LOAD_NO_HINTING */ +/* */ +/* */ +/* A bit-field constant, used with FT_Load_Glyph() to indicate that */ +/* the vector outline being loaded should not be fitted to the pixel */ +/* grid but simply scaled to 26.6 fractional pixels. */ +/* */ +/* This flag is ignored if FT_LOAD_NO_SCALE is set. */ +/* */ +#define FT_LOAD_NO_HINTING 2 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_LOAD_RENDER */ +/* */ +/* */ +/* A bit-field constant, used with FT_Load_Glyph() to indicate that */ +/* the function should load the glyph and immediately convert it into */ +/* a bitmap, if necessary, by calling FT_Render_Glyph(). */ +/* */ +/* Note that by default, FT_Load_Glyph() loads the glyph image in its */ +/* native format. */ +/* */ +#define FT_LOAD_RENDER 4 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_LOAD_NO_BITMAP */ +/* */ +/* */ +/* A bit-field constant, used with FT_Load_Glyph() to indicate that */ +/* the function should not load the bitmap or pixmap of a given */ +/* glyph. This is useful when you do not want to load the embedded */ +/* bitmaps of scalable formats, as the native glyph image will be */ +/* loaded, and can then be rendered through FT_Render_Glyph(). */ +/* */ +#define FT_LOAD_NO_BITMAP 8 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_LOAD_VERTICAL_LAYOUT */ +/* */ +/* */ +/* A bit-field constant, used with FT_Load_Glyph() to indicate that */ +/* the glyph image should be prepared for vertical layout. This */ +/* basically means that `face.glyph.advance' will correspond to the */ +/* vertical advance height (instead of the default horizontal */ +/* advance width), and that the glyph image will translated to match */ +/* the vertical bearings positions. */ +/* */ +#define FT_LOAD_VERTICAL_LAYOUT 16 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_LOAD_FORCE_AUTOHINT */ +/* */ +/* */ +/* A bit-field constant, used with FT_Load_Glyph() to indicate that */ +/* the function should try to auto-hint the glyphs, even if a driver */ +/* specific hinter is available. */ +/* */ +#define FT_LOAD_FORCE_AUTOHINT 32 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_LOAD_CROP_BITMAP */ +/* */ +/* */ +/* A bit-field constant, used with FT_Load_Glyph() to indicate that */ +/* the font driver should try to crop the bitmap (i.e. remove all */ +/* space around its black bits) when loading it. For now, this */ +/* really only works with embedded bitmaps in TrueType fonts. */ +/* */ +#define FT_LOAD_CROP_BITMAP 64 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_LOAD_PEDANTIC */ +/* */ +/* */ +/* A bit-field constant, used with FT_Load_Glyph() to indicate that */ +/* the glyph loader should perform a pedantic bytecode */ +/* interpretation. Many popular fonts come with broken glyph */ +/* programs. When this flag is set, loading them will return an */ +/* error. Otherwise, errors are ignored by the loader, sometimes */ +/* resulting in ugly glyphs. */ +/* */ +#define FT_LOAD_PEDANTIC 128 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH */ +/* */ +/* */ +/* A bit-field constant, used with FT_Load_Glyph() to indicate that */ +/* the glyph loader should ignore the global advance width defined */ +/* in the font. As far as we know, this is only used by the */ +/* X-TrueType font server, in order to deal correctly with the */ +/* incorrect metrics contained in DynaLab's TrueType CJK fonts. */ +/* */ +#define FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH 512 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_LOAD_NO_RECURSE */ +/* */ +/* */ +/* A bit-field constant, used with FT_Load_Glyph() to indicate that */ +/* the glyph loader should not load composite glyph recursively. */ +/* Rather, when a composite glyph is encountered, it should set */ +/* the values of `num_subglyphs' and `subglyphs', as well as set */ +/* `face->glyph.format' to ft_glyph_format_composite. */ +/* */ +/* This is for use by the auto-hinter and possibly other tools. */ +/* For nearly all applications, this flags should be left unset */ +/* when invoking FT_Load_Glyph(). */ +/* */ +/* Note that the flag forces the load of unscaled glyphs. */ +/* */ +#define FT_LOAD_NO_RECURSE 1024 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_LOAD_IGNORE_TRANSFORM */ +/* */ +/* */ +/* A bit-field constant, used with FT_Load_Glyph() to indicate that */ +/* the glyph loader should not try to transform the loaded glyph */ +/* image. */ +/* */ +#define FT_LOAD_IGNORE_TRANSFORM 2048 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_LOAD_MONOCHROME */ +/* */ +/* */ +/* Only used with FT_LOAD_RENDER set, it indicates that the returned */ +/* glyph image should be 1-bit monochrome. This really tells the */ +/* glyph loader to use `ft_render_mode_mono' when calling */ +/* FT_Render_Glyph(). */ +/* */ +#define FT_LOAD_MONOCHROME 4096 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_LOAD_LINEAR_DESIGN */ +/* */ +/* */ +/* A bit-field constant, used with FT_Load_Glyph() to indicate that */ +/* the function should return the linearly scaled metrics expressed */ +/* in original font units, instead of the default 16.16 pixel values. */ +/* */ +#define FT_LOAD_LINEAR_DESIGN 8192 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_LOAD_DEFAULT */ +/* */ +/* */ +/* A bit-field constant, used with FT_Load_Glyph() to indicate that */ +/* the function should try to load the glyph normally, i.e., */ +/* embedded bitmaps are favored over outlines, vectors are always */ +/* scaled and grid-fitted. */ +/* */ +#define FT_LOAD_DEFAULT 0 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Set_Transform */ +/* */ +/* */ +/* A function used to set the transformation that is applied to glyph */ +/* images just before they are converted to bitmaps in a glyph slot */ +/* when FT_Render_Glyph() is called. */ +/* */ +/* */ +/* face :: A handle to the source face object. */ +/* */ +/* */ +/* matrix :: A pointer to the transformation's 2x2 matrix. Use 0 for */ +/* the identity matrix. */ +/* delta :: A pointer to the translation vector. Use 0 for the null */ +/* vector. */ +/* */ +/* */ +/* The transformation is only applied to scalable image formats after */ +/* the glyph has been loaded. It means that hinting is unaltered by */ +/* the transformation and is performed on the character size given in */ +/* the last call to FT_Set_Char_Sizes() or FT_Set_Pixel_Sizes(). */ +/* */ +FT_EXPORT_DEF( void ) FT_Set_Transform( FT_Face face, + FT_Matrix * matrix, + FT_Vector * delta ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Render_Mode */ +/* */ +/* */ +/* An enumeration type that lists the render modes supported by the */ +/* FreeType 2 renderer(s). A renderer is in charge of converting a */ +/* glyph image into a bitmap. */ +/* */ +/* */ +/* ft_render_mode_normal :: This is the default render mode; it */ +/* corresponds to 8-bit anti-aliased */ +/* bitmaps, using 256 levels of gray. */ +/* */ +/* ft_render_mode_mono :: This render mode is used to produce 1-bit */ +/* monochrome bitmaps. */ +/* */ +/* */ +/* There is no render mode to produce 8-bit `monochrome' bitmaps -- */ +/* you have to make the conversion yourself if you need such things */ +/* (besides, FreeType is not a graphics library). */ +/* */ +/* More modes might appear later for specific display modes (e.g. TV, */ +/* LCDs, etc.). They will be supported through the simple addition */ +/* of a renderer module, with no changes to the rest of the engine. */ +/* */ +typedef enum FT_Render_Mode_ +{ + ft_render_mode_normal = 0, + ft_render_mode_mono = 1 + +} FT_Render_Mode; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Render_Glyph */ +/* */ +/* */ +/* Converts a given glyph image to a bitmap. It does so by */ +/* inspecting the glyph image format, find the relevant renderer, and */ +/* invoke it. */ +/* */ +/* */ +/* slot :: A handle to the glyph slot containing the image to */ +/* convert. */ +/* */ +/* render_mode :: This is the render mode used to render the glyph */ +/* image into a bitmap. See FT_Render_Mode for a list */ +/* of possible values. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Render_Glyph( FT_GlyphSlot slot, + FT_UInt render_mode ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Kerning_Mode */ +/* */ +/* */ +/* An enumeration used to specify which kerning values to return in */ +/* FT_Get_Kerning(). */ +/* */ +/* */ +/* ft_kerning_default :: Return scaled and grid-fitted kerning */ +/* distances (value is 0). */ +/* */ +/* ft_kerning_unfitted :: Return scaled but un-grid-fitted kerning */ +/* distances. */ +/* */ +/* ft_kerning_unscaled :: Return the kerning vector in original font */ +/* units. */ +/* */ +typedef enum FT_Kerning_Mode_ +{ + ft_kerning_default = 0, + ft_kerning_unfitted, + ft_kerning_unscaled + +} FT_Kerning_Mode; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Get_Kerning */ +/* */ +/* */ +/* Returns the kerning vector between two glyphs of a same face. */ +/* */ +/* */ +/* face :: A handle to a source face object. */ +/* */ +/* left_glyph :: The index of the left glyph in the kern pair. */ +/* */ +/* right_glyph :: The index of the right glyph in the kern pair. */ +/* */ +/* kern_mode :: See FT_Kerning_Mode() for more information. */ +/* Determines the scale/dimension of the returned */ +/* kerning vector. */ +/* */ +/* */ +/* kerning :: The kerning vector. This is in font units for */ +/* scalable formats, and in pixels for fixed-sizes */ +/* formats. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* Only horizontal layouts (left-to-right & right-to-left) are */ +/* supported by this method. Other layouts, or more sophisticated */ +/* kernings, are out of the scope of this API function -- they can be */ +/* implemented through format-specific interfaces. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Get_Kerning( FT_Face face, + FT_UInt left_glyph, + FT_UInt right_glyph, + FT_UInt kern_mode, + FT_Vector * kerning ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Get_Glyph_Name */ +/* */ +/* */ +/* Retrieves the ASCII name of a given glyph in a face. This only */ +/* works for those faces where FT_HAS_GLYPH_NAME(face) returns true. */ +/* */ +/* */ +/* face :: A handle to a source face object. */ +/* */ +/* glyph_index :: The glyph index. */ +/* */ +/* buffer :: A pointer to a target buffer where the name will be */ +/* copied to. */ +/* */ +/* buffer_max :: The maximal number of bytes available in the */ +/* buffer. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* An error is returned if the face doesn't provide glyph names or if */ +/* the glyph index is invalid. In all cases of failure, the first */ +/* byte of `buffer' will be set to 0 to indicate an empty name. */ +/* */ +/* The glyph name is truncated to fit within the buffer if it is too */ +/* long. The returned string is always zero-terminated. */ +/* */ +/* This function is not compiled within the library if the config */ +/* macro FT_CONFIG_OPTION_NO_GLYPH_NAMES is defined in */ +/* `include/freetype/config/ftoptions.h' */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Get_Glyph_Name( FT_Face face, + FT_UInt glyph_index, + FT_Pointer buffer, + FT_UInt buffer_max ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Select_Charmap */ +/* */ +/* */ +/* Selects a given charmap by its encoding tag (as listed in */ +/* `freetype.h'). */ +/* */ +/* */ +/* face :: A handle to the source face object. */ +/* */ +/* encoding :: A handle to the selected charmap. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* This function will return an error if no charmap in the face */ +/* corresponds to the encoding queried here. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Select_Charmap( FT_Face face, + FT_Encoding encoding ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Set_Charmap */ +/* */ +/* */ +/* Selects a given charmap for character code to glyph index */ +/* decoding. */ +/* */ +/* */ +/* face :: A handle to the source face object. */ +/* charmap :: A handle to the selected charmap. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* This function will return an error if the charmap is not part of */ +/* the face (i.e., if it is not listed in the face->charmaps[] */ +/* table). */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Set_Charmap( FT_Face face, + FT_CharMap charmap ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Get_Char_Index */ +/* */ +/* */ +/* Returns the glyph index of a given character code. This function */ +/* uses a charmap object to do the translation. */ +/* */ +/* */ +/* face :: A handle to the source face object. */ +/* */ +/* charcode :: The character code. */ +/* */ +/* */ +/* The glyph index. 0 means `undefined character code'. */ +/* */ +FT_EXPORT_DEF( FT_UInt ) FT_Get_Char_Index( FT_Face face, + FT_ULong charcode ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_MulDiv */ +/* */ +/* */ +/* A very simple function used to perform the computation `(a*b)/c' */ +/* with maximal accuracy (it uses a 64-bit intermediate integer */ +/* whenever necessary). */ +/* */ +/* This function isn't necessarily as fast as some processor specific */ +/* operations, but is at least completely portable. */ +/* */ +/* */ +/* a :: The first multiplier. */ +/* b :: The second multiplier. */ +/* c :: The divisor. */ +/* */ +/* */ +/* The result of `(a*b)/c'. This function never traps when trying to */ +/* divide by zero; it simply returns `MaxInt' or `MinInt' depending */ +/* on the signs of `a' and `b'. */ +/* */ +FT_EXPORT_DEF( FT_Long ) FT_MulDiv( FT_Long a, + FT_Long b, + FT_Long c ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_MulFix */ +/* */ +/* */ +/* A very simple function used to perform the computation */ +/* `(a*b)/0x10000' with maximal accuracy. Most of the time this is */ +/* used to multiply a given value by a 16.16 fixed float factor. */ +/* */ +/* */ +/* a :: The first multiplier. */ +/* b :: The second multiplier. Use a 16.16 factor here whenever */ +/* possible (see note below). */ +/* */ +/* */ +/* The result of `(a*b)/0x10000'. */ +/* */ +/* */ +/* This function has been optimized for the case where the absolute */ +/* value of `a' is less than 2048, and `b' is a 16.16 scaling factor. */ +/* As this happens mainly when scaling from notional units to */ +/* fractional pixels in FreeType, it resulted in noticeable speed */ +/* improvements between versions 2.x and 1.x. */ +/* */ +/* As a conclusion, always try to place a 16.16 factor as the */ +/* _second_ argument of this function; this can make a great */ +/* difference. */ +/* */ +FT_EXPORT_DEF( FT_Long ) FT_MulFix( FT_Long a, + FT_Long b ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_DivFix */ +/* */ +/* */ +/* A very simple function used to perform the computation */ +/* `(a*0x10000)/b' with maximal accuracy. Most of the time, this is */ +/* used to divide a given value by a 16.16 fixed float factor. */ +/* */ +/* */ +/* a :: The first multiplier. */ +/* b :: The second multiplier. Use a 16.16 factor here whenever */ +/* possible (see note below). */ +/* */ +/* */ +/* The result of `(a*0x10000)/b'. */ +/* */ +/* */ +/* The optimization for FT_DivFix() is simple: If (a << 16) fits in */ +/* 32 bits, then the division is computed directly. Otherwise, we */ +/* use a specialized version of the old FT_MulDiv64(). */ +/* */ +FT_EXPORT_DEF( FT_Long ) FT_DivFix( FT_Long a, + FT_Long b ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Vector_Transform */ +/* */ +/* */ +/* Transforms a single vector through a 2x2 matrix. */ +/* */ +/* */ +/* vector :: The target vector to transform. */ +/* */ +/* */ +/* matrix :: A pointer to the source 2x2 matrix. */ +/* */ +/* */ +/* Yes. */ +/* */ +/* */ +/* The result is undefined if either `vector' or `matrix' is invalid. */ +/* */ +FT_EXPORT_DEF( void ) FT_Vector_Transform( FT_Vector * vec, + FT_Matrix * matrix ); + + + +#ifdef __cplusplus +} +#endif + + +#endif /* FREETYPE_H */ + + +/* END */ diff --git a/src/ft2/ftcalc.c b/src/ft2/ftcalc.c new file mode 100644 index 0000000..403eafa --- /dev/null +++ b/src/ft2/ftcalc.c @@ -0,0 +1,777 @@ +/***************************************************************************/ +/* */ +/* ftcalc.c */ +/* */ +/* Arithmetic computations (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + +/*************************************************************************/ +/* */ +/* Support for 1-complement arithmetic has been totally dropped in this */ +/* release. You can still write your own code if you need it. */ +/* */ +/*************************************************************************/ + +/*************************************************************************/ +/* */ +/* Implementing basic computation routines. */ +/* */ +/* FT_MulDiv(), FT_MulFix(), and FT_DivFix() are declared in freetype.h. */ +/* */ +/*************************************************************************/ + + +#include "ftcalc.h" +#include "ftdebug.h" +#include "ftobjs.h" + + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_calc + + +#ifdef FT_CONFIG_OPTION_OLD_CALCS + +static const FT_Long ft_square_roots[63] = +{ + 1L, 1L, 2L, 3L, 4L, 5L, 8L, 11L, + 16L, 22L, 32L, 45L, 64L, 90L, 128L, 181L, + 256L, 362L, 512L, 724L, 1024L, 1448L, 2048L, 2896L, + 4096L, 5892L, 8192L, 11585L, 16384L, 23170L, 32768L, 46340L, + + 65536L, 92681L, 131072L, 185363L, 262144L, 370727L, + 524288L, 741455L, 1048576L, 1482910L, 2097152L, 2965820L, + 4194304L, 5931641L, 8388608L, 11863283L, 16777216L, 23726566L, + + 33554432L, 47453132L, 67108864L, 94906265L, + 134217728L, 189812531L, 268435456L, 379625062L, + 536870912L, 759250125L, 1073741824L, 1518500250L, + 2147483647L +}; + +#else + +/*************************************************************************/ +/* */ +/* */ +/* FT_Sqrt32 */ +/* */ +/* */ +/* Computes the square root of an Int32 integer (which will be */ +/* handled as an unsigned long value). */ +/* */ +/* */ +/* x :: The value to compute the root for. */ +/* */ +/* */ +/* The result of `sqrt(x)'. */ +/* */ +FT_EXPORT_FUNC( FT_Int32 ) FT_Sqrt32( FT_Int32 x ) +{ + FT_ULong val, root, newroot, mask; + + + root = 0; + mask = 0x40000000L; + val = (FT_ULong)x; + + do + { + newroot = root + mask; + if ( newroot <= val ) { + val -= newroot; + root = newroot + mask; + } + + root >>= 1; + mask >>= 2; + + } while ( mask != 0 ); + + return root; +} + +#endif /* FT_CONFIG_OPTION_OLD_CALCS */ + + +#ifdef FT_LONG64 + +/*************************************************************************/ +/* */ +/* */ +/* FT_MulDiv */ +/* */ +/* */ +/* A very simple function used to perform the computation `(a*b)/c' */ +/* with maximal accuracy (it uses a 64-bit intermediate integer */ +/* whenever necessary). */ +/* */ +/* This function isn't necessarily as fast as some processor specific */ +/* operations, but is at least completely portable. */ +/* */ +/* */ +/* a :: The first multiplier. */ +/* b :: The second multiplier. */ +/* c :: The divisor. */ +/* */ +/* */ +/* The result of `(a*b)/c'. This function never traps when trying to */ +/* divide by zero; it simply returns `MaxInt' or `MinInt' depending */ +/* on the signs of `a' and `b'. */ +/* */ +FT_EXPORT_FUNC( FT_Long ) FT_MulDiv( FT_Long a, + FT_Long b, + FT_Long c ) +{ + FT_Int s; + + + s = 1; + if ( a < 0 ) { + a = -a; s = -s; + } + if ( b < 0 ) { + b = -b; s = -s; + } + if ( c < 0 ) { + c = -c; s = -s; + } + + return s * ( c > 0 ? ( (FT_Int64)a * b + ( c >> 1 ) ) / c + : 0x7FFFFFFFL ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_MulFix */ +/* */ +/* */ +/* A very simple function used to perform the computation */ +/* `(a*b)/0x10000' with maximal accuracy. Most of the time this is */ +/* used to multiply a given value by a 16.16 fixed float factor. */ +/* */ +/* */ +/* a :: The first multiplier. */ +/* b :: The second multiplier. Use a 16.16 factor here whenever */ +/* possible (see note below). */ +/* */ +/* */ +/* The result of `(a*b)/0x10000'. */ +/* */ +/* */ +/* This function has been optimized for the case where the absolute */ +/* value of `a' is less than 2048, and `b' is a 16.16 scaling factor. */ +/* As this happens mainly when scaling from notional units to */ +/* fractional pixels in FreeType, it resulted in noticeable speed */ +/* improvements between versions 2.x and 1.x. */ +/* */ +/* As a conclusion, always try to place a 16.16 factor as the */ +/* _second_ argument of this function; this can make a great */ +/* difference. */ +/* */ +FT_EXPORT_FUNC( FT_Long ) FT_MulFix( FT_Long a, + FT_Long b ) +{ + FT_Int s; + + + s = 1; + if ( a < 0 ) { + a = -a; s = -s; + } + if ( b < 0 ) { + b = -b; s = -s; + } + + return s * (FT_Long)( ( (FT_Int64)a * b + 0x8000 ) >> 16 ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_DivFix */ +/* */ +/* */ +/* A very simple function used to perform the computation */ +/* `(a*0x10000)/b' with maximal accuracy. Most of the time, this is */ +/* used to divide a given value by a 16.16 fixed float factor. */ +/* */ +/* */ +/* a :: The first multiplier. */ +/* b :: The second multiplier. Use a 16.16 factor here whenever */ +/* possible (see note below). */ +/* */ +/* */ +/* The result of `(a*0x10000)/b'. */ +/* */ +/* */ +/* The optimization for FT_DivFix() is simple: If (a << 16) fits in */ +/* 32 bits, then the division is computed directly. Otherwise, we */ +/* use a specialized version of the old FT_MulDiv64(). */ +/* */ +FT_EXPORT_FUNC( FT_Long ) FT_DivFix( FT_Long a, + FT_Long b ) +{ + FT_Int32 s; + FT_UInt32 q; + + + s = a; a = ABS( a ); + s ^= b; b = ABS( b ); + + if ( b == 0 ) { + /* check for division by 0 */ + q = 0x7FFFFFFFL; + } else { + /* compute result directly */ + q = ( (FT_Int64)a << 16 ) / b; + } + + return (FT_Int32)( s < 0 ? -q : q ); +} + + +#ifdef FT_CONFIG_OPTION_OLD_CALCS + +/* a helper function for FT_Sqrt64() */ + +static +int ft_order64( FT_Int64 z ) { + int j = 0; + + + while ( z ) + { + z = (unsigned FT_INT64)z >> 1; + j++; + } + return j - 1; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Sqrt64 */ +/* */ +/* */ +/* Computes the square root of a 64-bit value. That sounds stupid, */ +/* but it is needed to obtain maximal accuracy in the TrueType */ +/* bytecode interpreter. */ +/* */ +/* */ +/* l :: A 64-bit integer. */ +/* */ +/* */ +/* The 32-bit square-root. */ +/* */ +FT_EXPORT_FUNC( FT_Int32 ) FT_Sqrt64( FT_Int64 l ) +{ + FT_Int64 r, s; + + + if ( l <= 0 ) { + return 0; + } + if ( l == 1 ) { + return 1; + } + + r = ft_square_roots[ft_order64( l )]; + + do + { + s = r; + r = ( r + l / r ) >> 1; + + } while ( r > s || r * r > l ); + + return r; +} + +#endif /* FT_CONFIG_OPTION_OLD_CALCS */ + + +#else /* FT_LONG64 */ + + +/*************************************************************************/ +/* */ +/* */ +/* FT_MulDiv */ +/* */ +/* */ +/* A very simple function used to perform the computation `(a*b)/c' */ +/* with maximal accuracy (it uses a 64-bit intermediate integer */ +/* whenever necessary). */ +/* */ +/* This function isn't necessarily as fast as some processor specific */ +/* operations, but is at least completely portable. */ +/* */ +/* */ +/* a :: The first multiplier. */ +/* b :: The second multiplier. */ +/* c :: The divisor. */ +/* */ +/* */ +/* The result of `(a*b)/c'. This function never traps when trying to */ +/* divide by zero; it simply returns `MaxInt' or `MinInt' depending */ +/* on the signs of `a' and `b'. */ +/* */ +/* */ +/* The FT_MulDiv() function has been optimized thanks to ideas from */ +/* Graham Asher. The trick is to optimize computation if everything */ +/* fits within 32 bits (a rather common case). */ +/* */ +/* We compute `a*b+c/2', then divide it by `c' (positive values). */ +/* */ +/* 46340 is FLOOR(SQRT(2^31-1)). */ +/* */ +/* if ( a <= 46340 && b <= 46340 ) then ( a*b <= 0x7FFEA810 ) */ +/* */ +/* 0x7FFFFFFF - 0x7FFEA810 = 0x157F0 */ +/* */ +/* if ( c < 0x157F0*2 ) then ( a*b+c/2 <= 0x7FFFFFFF ) */ +/* */ +/* and 2*0x157F0 = 176096. */ +/* */ +FT_EXPORT_FUNC( FT_Long ) FT_MulDiv( FT_Long a, + FT_Long b, + FT_Long c ) +{ + long s; + + + if ( a == 0 || b == c ) { + return a; + } + + s = a; a = ABS( a ); + s ^= b; b = ABS( b ); + s ^= c; c = ABS( c ); + + if ( a <= 46340 && b <= 46340 && c <= 176095L && c > 0 ) { + a = ( a * b + ( c >> 1 ) ) / c; + } else if ( c > 0 ) { + FT_Int64 temp, temp2; + + + FT_MulTo64( a, b, &temp ); + temp2.hi = (FT_Int32)( c >> 31 ); + temp2.lo = (FT_UInt32)( c / 2 ); + FT_Add64( &temp, &temp2, &temp ); + a = FT_Div64by32( &temp, c ); + } else { + a = 0x7FFFFFFFL; + } + + return ( s < 0 ? -a : a ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_MulFix */ +/* */ +/* */ +/* A very simple function used to perform the computation */ +/* `(a*b)/0x10000' with maximal accuracy. Most of the time, this is */ +/* used to multiply a given value by a 16.16 fixed float factor. */ +/* */ +/* */ +/* a :: The first multiplier. */ +/* b :: The second multiplier. Use a 16.16 factor here whenever */ +/* possible (see note below). */ +/* */ +/* */ +/* The result of `(a*b)/0x10000'. */ +/* */ +/* */ +/* The optimization for FT_MulFix() is different. We could simply be */ +/* happy by applying the same principles as with FT_MulDiv(), because */ +/* */ +/* c = 0x10000 < 176096 */ +/* */ +/* However, in most cases, we have a `b' with a value around 0x10000 */ +/* which is greater than 46340. */ +/* */ +/* According to some testing, most cases have `a' < 2048, so a good */ +/* idea is to use bounds like 2048 and 1048576 (=floor((2^31-1)/2048) */ +/* for `a' and `b', respectively. */ +/* */ +FT_EXPORT_FUNC( FT_Long ) FT_MulFix( FT_Long a, + FT_Long b ) +{ + FT_Long s; + FT_ULong ua, ub; + + + if ( a == 0 || b == 0x10000L ) { + return a; + } + + s = a; a = ABS( a ); + s ^= b; b = ABS( b ); + + ua = (FT_ULong)a; + ub = (FT_ULong)b; + + if ( ua <= 2048 && ub <= 1048576L ) { + ua = ( ua * ub + 0x8000 ) >> 16; + } else + { + FT_ULong al = ua & 0xFFFF; + + + ua = ( ua >> 16 ) * ub + + al * ( ub >> 16 ) + + ( al * ( ub & 0xFFFF ) >> 16 ); + } + + return ( s < 0 ? -(FT_Long)ua : ua ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_DivFix */ +/* */ +/* */ +/* A very simple function used to perform the computation */ +/* `(a*0x10000)/b' with maximal accuracy. Most of the time, this is */ +/* used to divide a given value by a 16.16 fixed float factor. */ +/* */ +/* */ +/* a :: The first multiplier. */ +/* b :: The second multiplier. Use a 16.16 factor here whenever */ +/* possible (see note below). */ +/* */ +/* */ +/* The result of `(a*0x10000)/b'. */ +/* */ +/* */ +/* The optimization for FT_DivFix() is simple: If (a << 16) fits into */ +/* 32 bits, then the division is computed directly. Otherwise, we */ +/* use a specialized version of the old FT_MulDiv64(). */ +/* */ +FT_EXPORT_FUNC( FT_Long ) FT_DivFix( FT_Long a, + FT_Long b ) +{ + FT_Int32 s; + FT_UInt32 q; + + + s = a; a = ABS( a ); + s ^= b; b = ABS( b ); + + if ( b == 0 ) { + /* check for division by 0 */ + q = 0x7FFFFFFFL; + } else if ( ( a >> 16 ) == 0 ) { + /* compute result directly */ + q = (FT_UInt32)( a << 16 ) / (FT_UInt32)b; + } else + { + /* we need more bits; we have to do it by hand */ + FT_UInt32 c; + + + q = ( a / b ) << 16; + c = a % b; + + /* we must compute C*0x10000/B: we simply shift C and B so */ + /* C becomes smaller than 16 bits */ + while ( c >> 16 ) + { + c >>= 1; + b <<= 1; + } + + q += ( c << 16 ) / b; + } + + return ( s < 0 ? -(FT_Int32)q : (FT_Int32)q ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Add64 */ +/* */ +/* */ +/* Add two Int64 values. */ +/* */ +/* */ +/* x :: A pointer to the first value to be added. */ +/* y :: A pointer to the second value to be added. */ +/* */ +/* */ +/* z :: A pointer to the result of `x + y'. */ +/* */ +/* */ +/* Will be wrapped by the ADD_64() macro. */ +/* */ +FT_EXPORT_FUNC( void ) FT_Add64( FT_Int64 * x, + FT_Int64 * y, + FT_Int64 * z ) +{ + register FT_UInt32 lo, hi; + + + lo = x->lo + y->lo; + hi = x->hi + y->hi + ( lo < x->lo ); + + z->lo = lo; + z->hi = hi; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_MulTo64 */ +/* */ +/* */ +/* Multiplies two Int32 integers. Returns an Int64 integer. */ +/* */ +/* */ +/* x :: The first multiplier. */ +/* y :: The second multiplier. */ +/* */ +/* */ +/* z :: A pointer to the result of `x * y'. */ +/* */ +/* */ +/* Will be wrapped by the MUL_64() macro. */ +/* */ +FT_EXPORT_FUNC( void ) FT_MulTo64( FT_Int32 x, + FT_Int32 y, + FT_Int64 * z ) +{ + FT_Int32 s; + + + s = x; x = ABS( x ); + s ^= y; y = ABS( y ); + + { + FT_UInt32 lo1, hi1, lo2, hi2, lo, hi, i1, i2; + + + lo1 = x & 0x0000FFFF; hi1 = x >> 16; + lo2 = y & 0x0000FFFF; hi2 = y >> 16; + + lo = lo1 * lo2; + i1 = lo1 * hi2; + i2 = lo2 * hi1; + hi = hi1 * hi2; + + /* Check carry overflow of i1 + i2 */ + i1 += i2; + if ( i1 < i2 ) { + hi += 1L << 16; + } + + hi += i1 >> 16; + i1 = i1 << 16; + + /* Check carry overflow of i1 + lo */ + lo += i1; + hi += ( lo < i1 ); + + z->lo = lo; + z->hi = hi; + } + + if ( s < 0 ) { + z->lo = ( FT_UInt32 ) - (FT_Int32)z->lo; + z->hi = ~z->hi + !( z->lo ); + } +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Div64by32 */ +/* */ +/* */ +/* Divides an Int64 value by an Int32 value. Returns an Int32 */ +/* integer. */ +/* */ +/* */ +/* x :: A pointer to the dividend. */ +/* y :: The divisor. */ +/* */ +/* */ +/* The result of `x / y'. */ +/* */ +/* */ +/* Will be wrapped by the DIV_64() macro. */ +/* */ +FT_EXPORT_FUNC( FT_Int32 ) FT_Div64by32( FT_Int64 * x, + FT_Int32 y ) +{ + FT_Int32 s; + FT_UInt32 q, r, i, lo; + + + s = x->hi; + if ( s < 0 ) { + x->lo = ( FT_UInt32 ) - (FT_Int32)x->lo; + x->hi = ~x->hi + !( x->lo ); + } + s ^= y; y = ABS( y ); + + /* Shortcut */ + if ( x->hi == 0 ) { + if ( y > 0 ) { + q = x->lo / y; + } else { + q = 0x7FFFFFFFL; + } + + return ( s < 0 ? -(FT_Int32)q : (FT_Int32)q ); + } + + r = x->hi; + lo = x->lo; + + if ( r >= (FT_UInt32)y ) { /* we know y is to be treated as unsigned here */ + return ( s < 0 ? 0x80000001UL : 0x7FFFFFFFUL ); + } + /* Return Max/Min Int32 if division overflow. */ + /* This includes division by zero! */ + q = 0; + for ( i = 0; i < 32; i++ ) + { + r <<= 1; + q <<= 1; + r |= lo >> 31; + + if ( r >= (FT_UInt32)y ) { + r -= y; + q |= 1; + } + lo <<= 1; + } + + return ( s < 0 ? -(FT_Int32)q : (FT_Int32)q ); +} + + +#ifdef FT_CONFIG_OPTION_OLD_CALCS + + +/* two helper functions for FT_Sqrt64() */ + +static +void FT_Sub64( FT_Int64* x, + FT_Int64* y, + FT_Int64* z ) { + register FT_UInt32 lo, hi; + + + lo = x->lo - y->lo; + hi = x->hi - y->hi - ( (FT_Int32)lo < 0 ); + + z->lo = lo; + z->hi = hi; +} + + +static +int ft_order64( FT_Int64* z ) { + FT_UInt32 i; + int j; + + + i = z->lo; + j = 0; + if ( z->hi ) { + i = z->hi; + j = 32; + } + + while ( i > 0 ) + { + i >>= 1; + j++; + } + return j - 1; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Sqrt64 */ +/* */ +/* */ +/* Computes the square root of a 64-bits value. That sounds stupid, */ +/* but it is needed to obtain maximal accuracy in the TrueType */ +/* bytecode interpreter. */ +/* */ +/* */ +/* z :: A pointer to a 64-bit integer. */ +/* */ +/* */ +/* The 32-bit square-root. */ +/* */ +FT_EXPORT_FUNC( FT_Int32 ) FT_Sqrt64( FT_Int64 * l ) +{ + FT_Int64 l2; + FT_Int32 r, s; + + + if ( (FT_Int32)l->hi < 0 || + ( l->hi == 0 && l->lo == 0 ) ) { + return 0; + } + + s = ft_order64( l ); + if ( s == 0 ) { + return 1; + } + + r = ft_square_roots[s]; + do + { + s = r; + r = ( r + FT_Div64by32( l, r ) ) >> 1; + FT_MulTo64( r, r, &l2 ); + FT_Sub64( l, &l2, &l2 ); + + } while ( r > s || (FT_Int32)l2.hi < 0 ); + + return r; +} + +#endif /* FT_CONFIG_OPTION_OLD_CALCS */ + +#endif /* FT_LONG64 */ + + +/* END */ diff --git a/src/ft2/ftcalc.h b/src/ft2/ftcalc.h new file mode 100644 index 0000000..39cd451 --- /dev/null +++ b/src/ft2/ftcalc.h @@ -0,0 +1,123 @@ +/***************************************************************************/ +/* */ +/* ftcalc.h */ +/* */ +/* Arithmetic computations (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef FTCALC_H +#define FTCALC_H + +#include "freetype.h" +#include "ftconfig.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +#ifdef LONG64 + + +typedef INT64 FT_Int64; + +#define ADD_64( x, y, z ) z = ( x ) + ( y ) +#define MUL_64( x, y, z ) z = (FT_Int64)( x ) * ( y ) + +#define DIV_64( x, y ) ( ( x ) / ( y ) ) + + +#ifdef FT_CONFIG_OPTION_OLD_CALCS + +#define SQRT_64( z ) FT_Sqrt64( z ) + +FT_EXPORT_DEF( FT_Int32 ) FT_Sqrt64( FT_Int64 l ); + +#endif /* FT_CONFIG_OPTION_OLD_CALCS */ + + +#else /* LONG64 */ + + +typedef struct FT_Int64_ +{ + FT_UInt32 lo; + FT_UInt32 hi; + +} FT_Int64; + + +#define ADD_64( x, y, z ) FT_Add64( &x, &y, &z ) +#define MUL_64( x, y, z ) FT_MulTo64( x, y, &z ) +#define DIV_64( x, y ) FT_Div64by32( &x, y ) + + +FT_EXPORT_DEF( void ) FT_Add64( FT_Int64 * x, + FT_Int64 * y, + FT_Int64 * z ); + +FT_EXPORT_DEF( void ) FT_MulTo64( FT_Int32 x, + FT_Int32 y, + FT_Int64 * z ); + +FT_EXPORT_DEF( FT_Int32 ) FT_Div64by32( FT_Int64 * x, + FT_Int32 y ); + + +#ifdef FT_CONFIG_OPTION_OLD_CALCS + +#define SQRT_64( z ) FT_Sqrt64( &z ) + +FT_EXPORT_DEF( FT_Int32 ) FT_Sqrt64( FT_Int64 * x ); + +#endif /* FT_CONFIG_OPTION_OLD_CALCS */ + + +#endif /* LONG64 */ + + +#ifndef FT_CONFIG_OPTION_OLD_CALCS + +#define SQRT_32( x ) FT_Sqrt32( x ) + +BASE_DEF( FT_Int32 ) FT_Sqrt32( FT_Int32 x ); + +#endif /* !FT_CONFIG_OPTION_OLD_CALCS */ + + +/*************************************************************************/ +/* */ +/* FT_MulDiv() and FT_MulFix() are declared in freetype.h. */ +/* */ +/*************************************************************************/ + + +#define INT_TO_F26DOT6( x ) ( (FT_Long)( x ) << 6 ) +#define INT_TO_F2DOT14( x ) ( (FT_Long)( x ) << 14 ) +#define INT_TO_FIXED( x ) ( (FT_Long)( x ) << 16 ) +#define F2DOT14_TO_FIXED( x ) ( (FT_Long)( x ) << 2 ) +#define FLOAT_TO_FIXED( x ) ( (FT_Long)( x * 65536.0 ) ) + +#define ROUND_F26DOT6( x ) ( x >= 0 ? ( ( ( x ) + 32 ) & - 64 ) \ + : ( -( ( 32 - ( x ) ) & - 64 ) ) ) + + +#ifdef __cplusplus +} +#endif + +#endif /* FTCALC_H */ + + +/* END */ diff --git a/src/ft2/ftconfig.h b/src/ft2/ftconfig.h new file mode 100644 index 0000000..23988c7 --- /dev/null +++ b/src/ft2/ftconfig.h @@ -0,0 +1,187 @@ +/***************************************************************************/ +/* */ +/* ftconfig.h */ +/* */ +/* ANSI-specific configuration file (specification only). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +/*************************************************************************/ +/* */ +/* This header file contains a number of macro definitions that are used */ +/* by the rest of the engine. Most of the macros here are automatically */ +/* determined at compile time, and you should not need to change it to */ +/* port FreeType, except to compile the library with a non-ANSI */ +/* compiler. */ +/* */ +/* Note however that if some specific modifications are needed, we */ +/* advise you to place a modified copy in your build directory. */ +/* */ +/* The build directory is usually `freetype/builds/', and */ +/* contains system-specific files that are always included first when */ +/* building the library. */ +/* */ +/* This ANSI version should stay in `include/freetype/config'. */ +/* */ +/*************************************************************************/ + + +#ifndef FTCONFIG_H +#define FTCONFIG_H + + +/* Include the header file containing all developer build options */ +#include "ftoption.h" + + +/*************************************************************************/ +/* */ +/* PLATFORM-SPECIFIC CONFIGURATION MACROS */ +/* */ +/* These macros can be toggled to suit a specific system. The current */ +/* ones are defaults used to compile FreeType in an ANSI C environment */ +/* (16bit compilers are also supported). Copy this file to your own */ +/* `freetype/builds/' directory, and edit it to port the engine. */ +/* */ +/*************************************************************************/ + + +/* We use values to know the sizes of the types. */ +#include + +/* The number of bytes in an `int' type. */ +#if UINT_MAX == 0xFFFFFFFF +#define FT_SIZEOF_INT 4 +#elif UINT_MAX == 0xFFFF +#define FT_SIZEOF_INT 2 +#elif UINT_MAX > 0xFFFFFFFF && UINT_MAX == 0xFFFFFFFFFFFFFFFF +#define FT_SIZEOF_INT 8 +#else +#error "Unsupported number of bytes in `int' type!" +#endif + +/* The number of bytes in a `long' type. */ +#if ULONG_MAX == 0xFFFFFFFF +#define FT_SIZEOF_LONG 4 +#elif ULONG_MAX > 0xFFFFFFFF && ULONG_MAX == 0xFFFFFFFFFFFFFFFF +#define FT_SIZEOF_LONG 8 +#else +#error "Unsupported number of bytes in `long' type!" +#endif + + +/* Preferred alignment of data */ +#define FT_ALIGNMENT 8 + + +/* UNUSED is a macro used to indicate that a given parameter is not used */ +/* -- this is only used to get rid of unpleasant compiler warnings */ +#ifndef FT_UNUSED +#define FT_UNUSED( arg ) ( ( arg ) = ( arg ) ) +#endif + + +/*************************************************************************/ +/* */ +/* AUTOMATIC CONFIGURATION MACROS */ +/* */ +/* These macros are computed from the ones defined above. Don't touch */ +/* their definition, unless you know precisely what you are doing. No */ +/* porter should need to mess with them. */ +/* */ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* IntN types */ +/* */ +/* Used to guarantee the size of some specific integers. */ +/* */ +typedef signed short FT_Int16; +typedef unsigned short FT_UInt16; + +#if FT_SIZEOF_INT == 4 + +typedef signed int FT_Int32; +typedef unsigned int FT_UInt32; + +#elif FT_SIZEOF_LONG == 4 + +typedef signed long FT_Int32; +typedef unsigned long FT_UInt32; + +#else +#error "no 32bit type found -- please check your configuration files" +#endif + +#if FT_SIZEOF_LONG == 8 + +/* FT_LONG64 must be defined if a 64-bit type is available */ +#define FT_LONG64 +#define FT_INT64 long + +#else + + +/*************************************************************************/ +/* */ +/* Many compilers provide the non-ANSI `long long' 64-bit type. You can */ +/* activate it by defining the FTCALC_USE_LONG_LONG macro in */ +/* `ftoption.h'. */ +/* */ +/* Note that this will produce many -ansi warnings during library */ +/* compilation, and that in many cases, the generated code will be */ +/* neither smaller nor faster! */ +/* */ +#ifdef FTCALC_USE_LONG_LONG + +#define FT_LONG64 +#define FT_INT64 long long + +#endif /* FTCALC_USE_LONG_LONG */ +#endif /* FT_SIZEOF_LONG == 8 */ + + +#ifdef FT_MAKE_OPTION_SINGLE_OBJECT +#define LOCAL_DEF static +#define LOCAL_FUNC static +#else +#define LOCAL_DEF extern +#define LOCAL_FUNC /* nothing */ +#endif + +#ifdef FT_MAKE_OPTION_SINGLE_LIBRARY_OBJECT +#define BASE_DEF( x ) static x +#define BASE_FUNC( x ) static x +#else +#define BASE_DEF( x ) extern x +#define BASE_FUNC( x ) extern x +#endif + +#ifndef FT_EXPORT_DEF +#define FT_EXPORT_DEF( x ) extern x +#endif + +#ifndef FT_EXPORT_FUNC +#define FT_EXPORT_FUNC( x ) extern x +#endif + +#ifndef FT_EXPORT_VAR +#define FT_EXPORT_VAR( x ) extern x +#endif + +#endif /* FTCONFIG_H */ + + +/* END */ diff --git a/src/ft2/ftdebug.c b/src/ft2/ftdebug.c new file mode 100644 index 0000000..8f99460 --- /dev/null +++ b/src/ft2/ftdebug.c @@ -0,0 +1,124 @@ +/***************************************************************************/ +/* */ +/* ftdebug.c */ +/* */ +/* Debugging and logging component (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +/*************************************************************************/ +/* */ +/* This component contains various macros and functions used to ease the */ +/* debugging of the FreeType engine. Its main purpose is in assertion */ +/* checking, tracing, and error detection. */ +/* */ +/* There are now three debugging modes: */ +/* */ +/* - trace mode */ +/* */ +/* Error and trace messages are sent to the log file (which can be the */ +/* standard error output). */ +/* */ +/* - error mode */ +/* */ +/* Only error messages are generated. */ +/* */ +/* - release mode: */ +/* */ +/* No error message is sent or generated. The code is free from any */ +/* debugging parts. */ +/* */ +/*************************************************************************/ + + +#include "ftdebug.h" + +#ifdef FT_DEBUG_LEVEL_TRACE +char ft_trace_levels[trace_max]; +#endif + + +#if defined( FT_DEBUG_LEVEL_ERROR ) || defined( FT_DEBUG_LEVEL_TRACE ) + + +#include +#include +#include + + +FT_EXPORT_FUNC( void ) FT_Message( const char* fmt, ... ) +{ + va_list ap; + + + va_start( ap, fmt ); + vprintf( fmt, ap ); + va_end( ap ); +} + + +FT_EXPORT_FUNC( void ) FT_Panic( const char* fmt, ... ) +{ + va_list ap; + + + va_start( ap, fmt ); + vprintf( fmt, ap ); + va_end( ap ); + + exit( EXIT_FAILURE ); +} + + +#ifdef FT_DEBUG_LEVEL_TRACE + + +/*************************************************************************/ +/* */ +/* */ +/* FT_SetTraceLevel */ +/* */ +/* */ +/* Sets the trace level for debugging. */ +/* */ +/* */ +/* component :: The component which should be traced. See ftdebug.h */ +/* for a complete list. If set to `trace_any', all */ +/* components will be traced. */ +/* level :: The tracing level. */ +/* */ +FT_EXPORT_FUNC( void ) FT_SetTraceLevel( FT_Trace component, + char level ) +{ + if ( component >= trace_max ) { + return; + } + + /* if component is `trace_any', change _all_ levels at once */ + if ( component == trace_any ) { + int n; + + + for ( n = trace_any; n < trace_max; n++ ) + ft_trace_levels[n] = level; + } else { /* otherwise, only change individual component */ + ft_trace_levels[component] = level; + } +} + +#endif /* FT_DEBUG_LEVEL_TRACE */ + +#endif /* FT_DEBUG_LEVEL_TRACE || FT_DEBUG_LEVEL_ERROR */ + + +/* END */ diff --git a/src/ft2/ftdebug.h b/src/ft2/ftdebug.h new file mode 100644 index 0000000..b6e0515 --- /dev/null +++ b/src/ft2/ftdebug.h @@ -0,0 +1,225 @@ +/***************************************************************************/ +/* */ +/* ftdebug.h */ +/* */ +/* Debugging and logging component (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef FTDEBUG_H +#define FTDEBUG_H + +#include "ftconfig.h" +/* FT_DEBUG_LEVEL_ERROR */ + +#ifdef __cplusplus +extern "C" { +#endif + + +/* A very stupid pre-processor trick. See K&R version 2 */ +/* section A12.3 for details... */ +/* */ +/* It is also described in the section `Separate */ +/* Expansion of Macro Arguments' in the info file */ +/* `cpp.info', describing GNU cpp. */ +/* */ +#define FT_CAT( x, y ) x ## y +#define FT_XCAT( x, y ) FT_CAT( x, y ) + + +#ifdef FT_DEBUG_LEVEL_TRACE + + +/* note that not all levels are used currently */ + +typedef enum FT_Trace_ +{ + /* the first level must always be `trace_any' */ + trace_any = 0, + + /* base components */ + trace_aaraster, /* anti-aliasing raster (ftgrays.c) */ + trace_calc, /* calculations (ftcalc.c) */ + trace_extend, /* extension manager (ftextend.c) */ + trace_glyph, /* glyph manager (ftglyph.c) */ + trace_io, /* i/o monitoring (ftsystem.c) */ + trace_init, /* initialization (ftinit.c) */ + trace_list, /* list manager (ftlist.c) */ + trace_memory, /* memory manager (ftobjs.c) */ + trace_mm, /* MM interface (ftmm.c) */ + trace_objs, /* base objects (ftobjs.c) */ + trace_outline, /* outline management (ftoutln.c) */ + trace_raster, /* rasterizer (ftraster.c) */ + trace_stream, /* stream manager (ftstream.c) */ + + /* SFNT driver components */ + trace_sfobjs, /* SFNT object handler (sfobjs.c) */ + trace_ttcmap, /* charmap handler (ttcmap.c) */ + trace_ttload, /* basic TrueType tables (ttload.c) */ + trace_ttpost, /* PS table processing (ttpost.c) */ + trace_ttsbit, /* TrueType sbit handling (ttsbit.c) */ + + /* TrueType driver components */ + trace_ttdriver, /* TT font driver (ttdriver.c) */ + trace_ttgload, /* TT glyph loader (ttgload.c) */ + trace_ttinterp, /* bytecode interpreter (ttinterp.c) */ + trace_ttobjs, /* TT objects manager (ttobjs.c) */ + trace_ttpload, /* TT data/program loader (ttpload.c) */ + + /* Type 1 driver components */ + trace_t1driver, + trace_t1gload, + trace_t1hint, + trace_t1load, + trace_t1objs, + + /* experimental Type 1 driver components */ + trace_z1driver, + trace_z1gload, + trace_z1hint, + trace_z1load, + trace_z1objs, + trace_z1parse, + + /* Type 2 driver components */ + trace_t2driver, + trace_t2gload, + trace_t2load, + trace_t2objs, + trace_t2parse, + + /* CID driver components */ + trace_cidafm, + trace_ciddriver, + trace_cidgload, + trace_cidload, + trace_cidobjs, + trace_cidparse, + + /* Windows fonts component */ + trace_winfnt, + + /* the last level must always be `trace_max' */ + trace_max + +} FT_Trace; + + +/* declared in ftdebug.c */ +extern char ft_trace_levels[trace_max]; + + +/*************************************************************************/ +/* */ +/* IMPORTANT! */ +/* */ +/* Each component must define the macro FT_COMPONENT to a valid FT_Trace */ +/* value before using any TRACE macro. */ +/* */ +/*************************************************************************/ + + +#define FT_TRACE( level, varformat ) \ + do \ + { \ + if ( ft_trace_levels[FT_COMPONENT] >= level ) { \ + FT_XCAT( FT_Message, varformat );} \ + } while ( 0 ) + + +FT_EXPORT_DEF( void ) FT_SetTraceLevel( FT_Trace component, + char level ); + + +#elif defined( FT_DEBUG_LEVEL_ERROR ) + + +#define FT_TRACE( level, varformat ) do ;while ( 0 ) /* nothing */ + + +#else /* release mode */ + + +#define FT_Assert( condition ) do ;while ( 0 ) /* nothing */ + +#define FT_TRACE( level, varformat ) do ;while ( 0 ) /* nothing */ +#define FT_ERROR( varformat ) do ;while ( 0 ) /* nothing */ + + +#endif /* FT_DEBUG_LEVEL_TRACE, FT_DEBUG_LEVEL_ERROR */ + + +/*************************************************************************/ +/* */ +/* Define macros and functions that are common to the debug and trace */ +/* modes. */ +/* */ +/* You need vprintf() to be able to compile ftdebug.c. */ +/* */ +/*************************************************************************/ + + +#if defined( FT_DEBUG_LEVEL_TRACE ) || defined( FT_DEBUG_LEVEL_ERROR ) + + +#include "stdio.h" /* for vprintf() */ + + +#define FT_Assert( condition ) \ + do \ + { \ + if ( !( condition ) ) { \ + FT_Panic( "assertion failed on line %d of file %s\n", \ + __LINE__, __FILE__ );} \ + } while ( 0 ) + +/* print a message */ +FT_EXPORT_DEF( void ) FT_Message( const char* fmt, ... ); + +/* print a message and exit */ +FT_EXPORT_DEF( void ) FT_Panic( const char* fmt, ... ); + +#define FT_ERROR( varformat ) FT_XCAT( FT_Message, varformat ) + + +#endif /* FT_DEBUG_LEVEL_TRACE || FT_DEBUG_LEVEL_ERROR */ + + +/*************************************************************************/ +/* */ +/* You need two opening resp. closing parentheses! */ +/* */ +/* Example: FT_TRACE0(( "Value is %i", foo )) */ +/* */ +/*************************************************************************/ + +#define FT_TRACE0( varformat ) FT_TRACE( 0, varformat ) +#define FT_TRACE1( varformat ) FT_TRACE( 1, varformat ) +#define FT_TRACE2( varformat ) FT_TRACE( 2, varformat ) +#define FT_TRACE3( varformat ) FT_TRACE( 3, varformat ) +#define FT_TRACE4( varformat ) FT_TRACE( 4, varformat ) +#define FT_TRACE5( varformat ) FT_TRACE( 5, varformat ) +#define FT_TRACE6( varformat ) FT_TRACE( 6, varformat ) +#define FT_TRACE7( varformat ) FT_TRACE( 7, varformat ) + + +#ifdef __cplusplus +} +#endif + + +#endif /* FTDEBUG_H */ + + +/* END */ diff --git a/src/ft2/ftdriver.h b/src/ft2/ftdriver.h new file mode 100644 index 0000000..45f04e5 --- /dev/null +++ b/src/ft2/ftdriver.h @@ -0,0 +1,182 @@ +/***************************************************************************/ +/* */ +/* ftdriver.h */ +/* */ +/* FreeType font driver interface (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef FTDRIVER_H +#define FTDRIVER_H + + +#include "ftmodule.h" +#include "ftconfig.h" /* for FT_DEBUG_LEVEL_TRACE, */ + + +typedef FT_Error ( *FTDriver_initFace )( FT_Stream stream, + FT_Face face, + FT_Int typeface_index, + FT_Int num_params, + FT_Parameter* parameters ); + +typedef void ( *FTDriver_doneFace )( FT_Face face ); + + +typedef FT_Error ( *FTDriver_initSize )( FT_Size size ); + +typedef void ( *FTDriver_doneSize )( FT_Size size ); + + +typedef FT_Error ( *FTDriver_initGlyphSlot )( FT_GlyphSlot slot ); + +typedef void ( *FTDriver_doneGlyphSlot )( FT_GlyphSlot slot ); + + +typedef FT_Error ( *FTDriver_setCharSizes )( FT_Size size, + FT_F26Dot6 char_width, + FT_F26Dot6 char_height, + FT_UInt horz_resolution, + FT_UInt vert_resolution ); + +typedef FT_Error ( *FTDriver_setPixelSizes )( FT_Size size, + FT_UInt pixel_width, + FT_UInt pixel_height ); + +typedef FT_Error ( *FTDriver_loadGlyph )( FT_GlyphSlot slot, + FT_Size size, + FT_UInt glyph_index, + FT_Int load_flags ); + + +typedef FT_UInt ( *FTDriver_getCharIndex )( FT_CharMap charmap, + FT_Long charcode ); + +typedef FT_Error ( *FTDriver_getKerning )( FT_Face face, + FT_UInt left_glyph, + FT_UInt right_glyph, + FT_Vector* kerning ); + + +typedef FT_Error ( *FTDriver_attachFile )( FT_Face face, + FT_Stream stream ); + + +typedef FT_Error ( *FTDriver_getAdvances )( FT_Face face, + FT_UInt first, + FT_UInt count, + FT_Bool vertical, + FT_UShort* advances ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Driver_Class */ +/* */ +/* */ +/* The font driver class. This structure mostly contains pointers to */ +/* driver methods. */ +/* */ +/* */ +/* root :: The parent module. */ +/* */ +/* face_object_size :: The size of a face object in bytes. */ +/* */ +/* size_object_size :: The size of a size object in bytes. */ +/* */ +/* slot_object_size :: The size of a glyph object in bytes. */ +/* */ +/* init_face :: The format-specific face constructor. */ +/* */ +/* done_face :: The format-specific face destructor. */ +/* */ +/* init_size :: The format-specific size constructor. */ +/* */ +/* done_size :: The format-specific size destructor. */ +/* */ +/* init_slot :: The format-specific slot constructor. */ +/* */ +/* done_slot :: The format-specific slot destructor. */ +/* */ +/* set_char_sizes :: A handle to a function used to set the new */ +/* character size in points + resolution. Can be */ +/* set to 0 to indicate default behaviour. */ +/* */ +/* set_pixel_sizes :: A handle to a function used to set the new */ +/* character size in pixels. Can be set to 0 to */ +/* indicate default behaviour. */ +/* */ +/* load_glyph :: A function handle to load a given glyph image */ +/* in a slot. This field is mandatory! */ +/* */ +/* get_char_index :: A function handle to return the glyph index of */ +/* a given character for a given charmap. This */ +/* field is mandatory! */ +/* */ +/* get_kerning :: A function handle to return the unscaled */ +/* kerning for a given pair of glyphs. Can be */ +/* set to 0 if the format doesn't support */ +/* kerning. */ +/* */ +/* attach_file :: This function handle is used to read */ +/* additional data for a face from another */ +/* file/stream. For example, this can be used to */ +/* add data from AFM or PFM files on a Type 1 */ +/* face, or a CIDMap on a CID-keyed face. */ +/* */ +/* get_advances :: A function handle used to return the advances */ +/* of 'count' glyphs, starting at `index'. the */ +/* `vertical' flags must be set when vertical */ +/* advances are queried. The advances buffer is */ +/* caller-allocated. */ +/* */ +/* */ +/* Most function pointers, with the exception of `load_glyph' and */ +/* `get_char_index' can be set to 0 to indicate a default behaviour. */ +/* */ +typedef struct FT_Driver_Class_ +{ + FT_Module_Class root; + + FT_Int face_object_size; + FT_Int size_object_size; + FT_Int slot_object_size; + + FTDriver_initFace init_face; + FTDriver_doneFace done_face; + + FTDriver_initSize init_size; + FTDriver_doneSize done_size; + + FTDriver_initGlyphSlot init_slot; + FTDriver_doneGlyphSlot done_slot; + + FTDriver_setCharSizes set_char_sizes; + FTDriver_setPixelSizes set_pixel_sizes; + + FTDriver_loadGlyph load_glyph; + FTDriver_getCharIndex get_char_index; + + FTDriver_getKerning get_kerning; + FTDriver_attachFile attach_file; + + FTDriver_getAdvances get_advances; + +} FT_Driver_Class; + + +#endif /* FTDRIVER_H */ + + +/* END */ diff --git a/src/ft2/fterrors.h b/src/ft2/fterrors.h new file mode 100644 index 0000000..e5f5882 --- /dev/null +++ b/src/ft2/fterrors.h @@ -0,0 +1,166 @@ +/***************************************************************************/ +/* */ +/* fterrors.h */ +/* */ +/* FreeType error codes (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +/*************************************************************************/ +/* */ +/* This file is used to define the FreeType error enumeration constants */ +/* It can also be used to create an error message table easily with */ +/* something like: */ +/* */ +/* { */ +/* #undef FTERRORS_H */ +/* #define FT_ERRORDEF( e, v, s ) { e, s }, */ +/* #define FT_ERROR_START_LIST { */ +/* #define FT_ERROR_END_LIST { 0, 0 } }; */ +/* */ +/* const struct */ +/* { */ +/* int err_code; */ +/* const char* err_msg */ +/* } ft_errors[] = */ +/* */ +/* #include */ +/* } */ +/* */ +/*************************************************************************/ + + +#ifndef FTERRORS_H +#define FTERRORS_H + + +#ifndef FT_ERRORDEF + +#define FT_ERRORDEF( e, v, s ) e = v, +#define FT_ERROR_START_LIST enum { +#define FT_ERROR_END_LIST FT_Err_Max }; + +#endif /* !FT_ERRORDEF */ + + +#ifdef FT_ERROR_START_LIST +FT_ERROR_START_LIST +#endif + +FT_ERRORDEF( FT_Err_Ok, 0x0000, \ + "no error" ) +FT_ERRORDEF( FT_Err_Cannot_Open_Resource, 0x0001, \ + "can't open stream" ) +FT_ERRORDEF( FT_Err_Unknown_File_Format, 0x0002, \ + "unknown file format" ) +FT_ERRORDEF( FT_Err_Invalid_File_Format, 0x0003, \ + "broken file" ) + +FT_ERRORDEF( FT_Err_Invalid_Argument, 0x0010, \ + "invalid argument" ) +FT_ERRORDEF( FT_Err_Invalid_Handle, 0x0011, \ + "invalid object handle" ) +FT_ERRORDEF( FT_Err_Invalid_Glyph_Index, 0x0012, \ + "invalid glyph index" ) +FT_ERRORDEF( FT_Err_Invalid_Character_Code, 0x0013, \ + "invalid character code" ) + +FT_ERRORDEF( FT_Err_Unimplemented_Feature, 0x0020, \ + "unimplemented feature" ) +FT_ERRORDEF( FT_Err_Invalid_Glyph_Format, 0x0021, \ + "unsupported glyph image format" ) +FT_ERRORDEF( FT_Err_Cannot_Render_Glyph, 0x0022, \ + "cannot render this glyph format" ) + +FT_ERRORDEF( FT_Err_Invalid_Library_Handle, 0x0030, \ + "invalid library handle" ) +FT_ERRORDEF( FT_Err_Invalid_Driver_Handle, 0x0031, \ + "invalid module handle" ) +FT_ERRORDEF( FT_Err_Invalid_Face_Handle, 0x0032, \ + "invalid face handle" ) +FT_ERRORDEF( FT_Err_Invalid_Size_Handle, 0x0033, \ + "invalid size handle" ) +FT_ERRORDEF( FT_Err_Invalid_Slot_Handle, 0x0034, \ + "invalid glyph slot handle" ) +FT_ERRORDEF( FT_Err_Invalid_CharMap_Handle, 0x0035, \ + "invalid charmap handle" ) +FT_ERRORDEF( FT_Err_Invalid_Outline, 0x0036, \ + "invalid outline" ) +FT_ERRORDEF( FT_Err_Invalid_Version, 0x0037, \ + "invalid FreeType version" ) +FT_ERRORDEF( FT_Err_Lower_Module_Version, 0x0038, \ + "module version is too low" ) + +FT_ERRORDEF( FT_Err_Too_Many_Drivers, 0x0040, \ + "too many modules" ) +FT_ERRORDEF( FT_Err_Too_Many_Extensions, 0x0041, \ + "too many extensions" ) + +FT_ERRORDEF( FT_Err_Out_Of_Memory, 0x0050, \ + "out of memory" ) +FT_ERRORDEF( FT_Err_Unlisted_Object, 0x0051, \ + "unlisted object" ) + +FT_ERRORDEF( FT_Err_Invalid_Stream_Handle, 0x0060, \ + "invalid stream handle" ) +FT_ERRORDEF( FT_Err_Cannot_Open_Stream, 0x0061, \ + "cannot open stream" ) +FT_ERRORDEF( FT_Err_Invalid_Stream_Seek, 0x0062, \ + "invalid stream seek" ) +FT_ERRORDEF( FT_Err_Invalid_Stream_Skip, 0x0063, \ + "invalid stream skip" ) +FT_ERRORDEF( FT_Err_Invalid_Stream_Read, 0x0064, \ + "invalid stream read" ) +FT_ERRORDEF( FT_Err_Invalid_Stream_Operation, 0x0065, \ + "invalid stream operation" ) +FT_ERRORDEF( FT_Err_Invalid_Frame_Operation, 0x0066, \ + "invalid frame operation" ) +FT_ERRORDEF( FT_Err_Nested_Frame_Access, 0x0067, \ + "nested frame access" ) +FT_ERRORDEF( FT_Err_Invalid_Frame_Read, 0x0068, \ + "invalid frame read" ) + +FT_ERRORDEF( FT_Err_Invalid_Composite, 0x0070, \ + "invalid composite glyph" ) +FT_ERRORDEF( FT_Err_Too_Many_Hints, 0x0071, \ + "too many hints" ) + +FT_ERRORDEF( FT_Err_Raster_Uninitialized, 0x0080, \ + "raster uninitialized" ) +FT_ERRORDEF( FT_Err_Raster_Corrupted, 0x0081, \ + "raster corrupted" ) +FT_ERRORDEF( FT_Err_Raster_Overflow, 0x0082, \ + "raster overflow" ) +FT_ERRORDEF( FT_Err_Raster_Negative_Height, 0x0083, \ + "negative height while rastering" ) + +/* range 0x400 - 0x4FF is reserved for TrueType specific stuff */ + +/* range 0x500 - 0x5FF is reserved for CFF specific stuff */ + +/* range 0x600 - 0x6FF is reserved for Type1 specific stuff */ + +#ifdef FT_ERROR_END_LIST +FT_ERROR_END_LIST +#endif + + +#undef FT_ERROR_START_LIST +#undef FT_ERROR_END_LIST +#undef FT_ERRORDEF + + +#endif /* FTERRORS_H */ + + +/* END */ diff --git a/src/ft2/ftextend.c b/src/ft2/ftextend.c new file mode 100644 index 0000000..cb812ab --- /dev/null +++ b/src/ft2/ftextend.c @@ -0,0 +1,332 @@ +/***************************************************************************/ +/* */ +/* ftextend.h */ +/* */ +/* FreeType extensions implementation (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + +/*************************************************************************/ +/* */ +/* This is an updated version of the extension component, now located */ +/* in the main library's source directory. It allows the dynamic */ +/* registration/use of various face object extensions through a simple */ +/* API. */ +/* */ +/*************************************************************************/ + + +#include "ftextend.h" +#include "ftdebug.h" + + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_extend + + +typedef struct FT_Extension_Registry_ +{ + FT_Int num_extensions; + FT_Long cur_offset; + FT_Extension_Class classes[FT_MAX_EXTENSIONS]; + +} FT_Extension_Registry; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Init_Extensions */ +/* */ +/* */ +/* Initializes the extension component. */ +/* */ +/* */ +/* driver :: A handle to the driver object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error FT_Init_Extensions( FT_Driver driver ) { + FT_Error error; + FT_Memory memory; + FT_Extension_Registry* registry; + + + memory = driver->root.library->memory; + if ( ALLOC( registry, sizeof( *registry ) ) ) { + return error; + } + + registry->num_extensions = 0; + registry->cur_offset = 0; + driver->extensions = registry; + + FT_TRACE2( ( "FT_Init_Extensions: success\n" ) ); + + return FT_Err_Ok; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Done_Extensions */ +/* */ +/* */ +/* Finalizes the extension component. */ +/* */ +/* */ +/* driver :: A handle to the driver object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error FT_Done_Extensions( FT_Driver driver ) { + FT_Memory memory = driver->root.memory; + + + FREE( driver->extensions ); + return FT_Err_Ok; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Register_Extension */ +/* */ +/* */ +/* Registers a new extension. */ +/* */ +/* */ +/* driver :: A handle to the driver object. */ +/* class :: A pointer to a class describing the extension. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Register_Extension( + FT_Driver driver, + FT_Extension_Class * clazz ) +{ + FT_Extension_Registry* registry; + + + if ( !driver ) { + return FT_Err_Invalid_Driver_Handle; + } + + if ( !clazz ) { + return FT_Err_Invalid_Argument; + } + + registry = (FT_Extension_Registry*)driver->extensions; + if ( registry ) { + FT_Int n = registry->num_extensions; + FT_Extension_Class* cur = registry->classes + n; + + + if ( n >= FT_MAX_EXTENSIONS ) { + return FT_Err_Too_Many_Extensions; + } + + *cur = *clazz; + + cur->offset = registry->cur_offset; + + registry->num_extensions++; + registry->cur_offset += + ( cur->size + FT_ALIGNMENT - 1 ) & - FT_ALIGNMENT; + + FT_TRACE1( ( "FT_Register_Extension: `%s' successfully registered\n", + cur->id ) ); + } + + return FT_Err_Ok; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Get_Extension */ +/* */ +/* */ +/* Queries an extension block by an extension ID string. */ +/* */ +/* */ +/* face :: A handle to the face object. */ +/* extension_id :: An ID string identifying the extension. */ +/* */ +/* */ +/* extension_interface :: A generic pointer, usually pointing to a */ +/* table of functions implementing the */ +/* extension interface. */ +/* */ +/* */ +/* A generic pointer to the extension block. */ +/* */ +FT_EXPORT_FUNC( void* ) FT_Get_Extension( + FT_Face face, + const char* extension_id, + void** extension_interface ) +{ + FT_Extension_Registry* registry; + + + if ( !face || !extension_id || !extension_interface ) { + return 0; + } + + registry = (FT_Extension_Registry*)face->driver->extensions; + if ( registry && face->extensions ) { + FT_Extension_Class* cur = registry->classes; + FT_Extension_Class* limit = cur + registry->num_extensions; + + + for ( ; cur < limit; cur++ ) + if ( strcmp( cur->id, extension_id ) == 0 ) { + *extension_interface = cur->interface; + + FT_TRACE1( ( "FT_Get_Extension: got `%s'\n", extension_id ) ); + + return ( void* )( (char*)face->extensions + cur->offset ); + } + } + + /* could not find the extension id */ + + FT_ERROR( ( "FT_Get_Extension: couldn't find `%s'\n", extension_id ) ); + + *extension_interface = 0; + + return 0; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Destroy_Extensions */ +/* */ +/* */ +/* Destroys all extensions within a face object. */ +/* */ +/* */ +/* face :: A handle to the face object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* Called by the face object destructor. */ +/* */ +LOCAL_FUNC +FT_Error FT_Destroy_Extensions( FT_Face face ) { + FT_Extension_Registry* registry; + FT_Memory memory; + + + registry = (FT_Extension_Registry*)face->driver->extensions; + if ( registry && face->extensions ) { + FT_Extension_Class* cur = registry->classes; + FT_Extension_Class* limit = cur + registry->num_extensions; + + + for ( ; cur < limit; cur++ ) + { + char* ext = (char*)face->extensions + cur->offset; + + if ( cur->finalize ) { + cur->finalize( ext, face ); + } + } + + memory = face->driver->root.memory; + FREE( face->extensions ); + } + + return FT_Err_Ok; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Create_Extensions */ +/* */ +/* */ +/* Creates an extension object within a face object for all */ +/* registered extensions. */ +/* */ +/* */ +/* face :: A handle to the face object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* Called by the face object constructor. */ +/* */ +LOCAL_FUNC +FT_Error FT_Create_Extensions( FT_Face face ) { + FT_Extension_Registry* registry; + FT_Memory memory; + FT_Error error; + + + face->extensions = 0; + + /* load extensions registry; exit successfully if none is there */ + + registry = (FT_Extension_Registry*)face->driver->extensions; + if ( !registry ) { + return FT_Err_Ok; + } + + memory = face->driver->root.memory; + if ( ALLOC( face->extensions, registry->cur_offset ) ) { + return error; + } + + { + FT_Extension_Class* cur = registry->classes; + FT_Extension_Class* limit = cur + registry->num_extensions; + + + for ( ; cur < limit; cur++ ) + { + char* ext = (char*)face->extensions + cur->offset; + + if ( cur->init ) { + error = cur->init( ext, face ); + if ( error ) { + break; + } + } + } + } + + return error; +} + + +/* END */ diff --git a/src/ft2/ftextend.h b/src/ft2/ftextend.h new file mode 100644 index 0000000..ee16df3 --- /dev/null +++ b/src/ft2/ftextend.h @@ -0,0 +1,178 @@ +/***************************************************************************/ +/* */ +/* ftextend.h */ +/* */ +/* FreeType extensions implementation (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef FTEXTEND_H +#define FTEXTEND_H + + +#include "ftobjs.h" + + +#ifdef __cplusplus +extern "C" { +#endif + + +/*************************************************************************/ +/* */ +/* The extensions don't need to be integrated at compile time into the */ +/* engine, only at link time. */ +/* */ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Extension_Initializer */ +/* */ +/* */ +/* Each new face object can have several extensions associated with */ +/* it at creation time. This function is used to initialize given */ +/* extension data for a given face. */ +/* */ +/* */ +/* ext :: A typeless pointer to the extension data. */ +/* */ +/* face :: A handle to the source face object the extension is */ +/* associated with. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* In case of error, the initializer should not destroy the extension */ +/* data, as the finalizer will get called later by the function's */ +/* caller. */ +/* */ +typedef FT_Error ( *FT_Extension_Initializer )( void* ext, + FT_Face face ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Extension_Finalizer */ +/* */ +/* */ +/* Each new face object can have several extensions associated with */ +/* it at creation time. This function is used to finalize given */ +/* extension data for a given face; it occurs before the face object */ +/* itself is finalized. */ +/* */ +/* */ +/* ext :: A typeless pointer to the extension data. */ +/* */ +/* face :: A handle to the source face object the extension is */ +/* associated with. */ +/* */ +typedef void ( *FT_Extension_Finalizer )( void* ext, + FT_Face face ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Extension_Class */ +/* */ +/* */ +/* A simple structure used to describe a given extension to the */ +/* FreeType base layer. An FT_Extension_Class is used as a parameter */ +/* for FT_Register_Extension(). */ +/* */ +/* */ +/* id :: The extension's ID. This is a normal C string that */ +/* is used to uniquely reference the extension's */ +/* interface. */ +/* */ +/* size :: The size in bytes of the extension data that must be */ +/* associated with each face object. */ +/* */ +/* init :: A pointer to the extension data's initializer. */ +/* */ +/* finalize :: A pointer to the extension data's finalizer. */ +/* */ +/* interface :: This pointer can be anything, but should usually */ +/* point to a table of function pointers which implement */ +/* the extension's interface. */ +/* */ +/* offset :: This field is set and used within the base layer and */ +/* should be set to 0 when registering an extension */ +/* through FT_Register_Extension(). It contains an */ +/* offset within the face's extension block for the */ +/* current extension's data. */ +/* */ +typedef struct FT_Extension_Class_ +{ + const char* id; + FT_ULong size; + FT_Extension_Initializer init; + FT_Extension_Finalizer finalize; + void* interface; + + FT_ULong offset; + +} FT_Extension_Class; + + +FT_EXPORT_DEF( FT_Error ) FT_Register_Extension( + FT_Driver driver, + FT_Extension_Class * clazz ); + + +#ifdef FT_CONFIG_OPTION_EXTEND_ENGINE + + +/* Initialize the extension component */ +LOCAL_DEF +FT_Error FT_Init_Extensions( FT_Library library ); + +/* Finalize the extension component */ +LOCAL_DEF +FT_Error FT_Done_Extensions( FT_Library library ); + +/* Create an extension within a face object. Called by the */ +/* face object constructor. */ +LOCAL_DEF +FT_Error FT_Create_Extensions( FT_Face face ); + +/* Destroy all extensions within a face object. Called by the */ +/* face object destructor. */ +LOCAL_DEF +FT_Error FT_Destroy_Extensions( FT_Face face ); + + +#endif + + +/* return an extension's data & interface according to its ID */ +FT_EXPORT_DEF( void* ) FT_Get_Extension( + FT_Face face, + const char* extension_id, + void** extension_interface ); + + +#ifdef __cplusplus +} +#endif + + +#endif /* FTEXTEND_H */ + + +/* END */ diff --git a/src/ft2/ftglyph.c b/src/ft2/ftglyph.c new file mode 100644 index 0000000..75b2bbd --- /dev/null +++ b/src/ft2/ftglyph.c @@ -0,0 +1,1185 @@ +/***************************************************************************/ +/* */ +/* ftglyph.c */ +/* */ +/* FreeType convenience functions to handle glyphs (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + +/*************************************************************************/ +/* */ +/* This file contains the definition of several convenience functions */ +/* that can be used by client applications to easily retrieve glyph */ +/* bitmaps and outlines from a given face. */ +/* */ +/* These functions should be optional if you are writing a font server */ +/* or text layout engine on top of FreeType. However, they are pretty */ +/* handy for many other simple uses of the library. */ +/* */ +/*************************************************************************/ + + +#include "ftglyph.h" +#include "ftoutln.h" +#include "ftobjs.h" + + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_glyph + + +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** Convenience functions ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Matrix_Multiply */ +/* */ +/* */ +/* Performs the matrix operation `b = a*b'. */ +/* */ +/* */ +/* a :: A pointer to matrix `a'. */ +/* */ +/* */ +/* b :: A pointer to matrix `b'. */ +/* */ +/* */ +/* Yes. */ +/* */ +/* */ +/* The result is undefined if either `a' or `b' is zero. */ +/* */ +FT_EXPORT_FUNC( void ) FT_Matrix_Multiply( FT_Matrix * a, + FT_Matrix * b ) +{ + FT_Fixed xx, xy, yx, yy; + + + if ( !a || !b ) { + return; + } + + xx = FT_MulFix( a->xx, b->xx ) + FT_MulFix( a->xy, b->yx ); + xy = FT_MulFix( a->xx, b->xy ) + FT_MulFix( a->xy, b->yy ); + yx = FT_MulFix( a->yx, b->xx ) + FT_MulFix( a->yy, b->yx ); + yy = FT_MulFix( a->yx, b->xy ) + FT_MulFix( a->yy, b->yy ); + + b->xx = xx; b->xy = xy; + b->yx = yx; b->yy = yy; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Matrix_Invert */ +/* */ +/* */ +/* Inverts a 2x2 matrix. Returns an error if it can't be inverted. */ +/* */ +/* */ +/* matrix :: A pointer to the target matrix. Remains untouched in */ +/* case of error. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* Yes. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Matrix_Invert( FT_Matrix * matrix ) +{ + FT_Pos delta, xx, yy; + + + if ( !matrix ) { + return FT_Err_Invalid_Argument; + } + + /* compute discriminant */ + delta = FT_MulFix( matrix->xx, matrix->yy ) - + FT_MulFix( matrix->xy, matrix->yx ); + + if ( !delta ) { + return FT_Err_Invalid_Argument; /* matrix can't be inverted */ + + } + matrix->xy = -FT_DivFix( matrix->xy, delta ); + matrix->yx = -FT_DivFix( matrix->yx, delta ); + + xx = matrix->xx; + yy = matrix->yy; + + matrix->xx = FT_DivFix( yy, delta ); + matrix->yy = FT_DivFix( xx, delta ); + + return FT_Err_Ok; +} + + +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** FT_BitmapGlyph support ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ + +static +FT_Error ft_bitmap_copy( FT_Memory memory, + FT_Bitmap* source, + FT_Bitmap* target ) { + FT_Error error; + FT_Int pitch = source->pitch; + FT_ULong size; + + + *target = *source; + + if ( pitch < 0 ) { + pitch = -pitch; + } + + size = (FT_ULong)( pitch * source->rows ); + + if ( !ALLOC( target->buffer, size ) ) { + MEM_Copy( source->buffer, target->buffer, size ); + } + + return error; +} + + +static +FT_Error ft_bitmap_glyph_init( FT_BitmapGlyph glyph, + FT_GlyphSlot slot ) { + FT_Error error = FT_Err_Ok; + FT_Library library = FT_GLYPH( glyph )->library; + FT_Memory memory = library->memory; + + + if ( slot->format != ft_glyph_format_bitmap ) { + error = FT_Err_Invalid_Glyph_Format; + goto Exit; + } + + /* grab the bitmap in the slot - do lazy copying whenever possible */ + glyph->bitmap = slot->bitmap; + glyph->left = slot->bitmap_left; + glyph->top = slot->bitmap_top; + + if ( slot->flags & ft_glyph_own_bitmap ) { + slot->flags &= ~ft_glyph_own_bitmap; + } else + { + /* copy the bitmap into a new buffer */ + error = ft_bitmap_copy( memory, &slot->bitmap, &glyph->bitmap ); + } + +Exit: + return error; +} + + +static +FT_Error ft_bitmap_glyph_copy( FT_BitmapGlyph source, + FT_BitmapGlyph target ) { + FT_Memory memory = source->root.library->memory; + + + target->left = source->left; + target->top = source->top; + + return ft_bitmap_copy( memory, &source->bitmap, &target->bitmap ); +} + + +static +void ft_bitmap_glyph_done( FT_BitmapGlyph glyph ) { + FT_Memory memory = FT_GLYPH( glyph )->library->memory; + + + FREE( glyph->bitmap.buffer ); +} + + +static +void ft_bitmap_glyph_bbox( FT_BitmapGlyph glyph, + FT_BBox* cbox ) { + cbox->xMin = glyph->left << 6; + cbox->xMax = cbox->xMin + ( glyph->bitmap.width << 6 ); + cbox->yMax = glyph->top << 6; + cbox->yMin = cbox->xMax - ( glyph->bitmap.rows << 6 ); +} + + +const FT_Glyph_Class ft_bitmap_glyph_class = +{ + sizeof( FT_BitmapGlyphRec ), + ft_glyph_format_bitmap, + + (FT_Glyph_Init_Func) ft_bitmap_glyph_init, + (FT_Glyph_Done_Func) ft_bitmap_glyph_done, + (FT_Glyph_Copy_Func) ft_bitmap_glyph_copy, + (FT_Glyph_Transform_Func)0, + (FT_Glyph_BBox_Func) ft_bitmap_glyph_bbox, + (FT_Glyph_Prepare_Func) 0 +}; + + +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** FT_OutlineGlyph support ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ + + +static +FT_Error ft_outline_glyph_init( FT_OutlineGlyph glyph, + FT_GlyphSlot slot ) { + FT_Error error = FT_Err_Ok; + FT_Library library = FT_GLYPH( glyph )->library; + FT_Outline* source = &slot->outline; + FT_Outline* target = &glyph->outline; + + + /* check format in glyph slot */ + if ( slot->format != ft_glyph_format_outline ) { + error = FT_Err_Invalid_Glyph_Format; + goto Exit; + } + + /* allocate new outline */ + error = FT_Outline_New( library, source->n_points, source->n_contours, + &glyph->outline ); + if ( error ) { + goto Exit; + } + + /* copy it */ + MEM_Copy( target->points, source->points, + source->n_points * sizeof( FT_Vector ) ); + + MEM_Copy( target->tags, source->tags, + source->n_points * sizeof( FT_Byte ) ); + + MEM_Copy( target->contours, source->contours, + source->n_contours * sizeof( FT_Short ) ); + + /* copy all flags, except the `ft_outline_owner' one */ + target->flags = source->flags | ft_outline_owner; + +Exit: + return error; +} + + +static +void ft_outline_glyph_done( FT_OutlineGlyph glyph ) { + FT_Outline_Done( FT_GLYPH( glyph )->library, &glyph->outline ); +} + + +static +FT_Error ft_outline_glyph_copy( FT_OutlineGlyph source, + FT_OutlineGlyph target ) { + FT_Error error; + FT_Library library = FT_GLYPH( source )->library; + + + error = FT_Outline_New( library, source->outline.n_points, + source->outline.n_contours, &target->outline ); + if ( !error ) { + FT_Outline_Copy( &source->outline, &target->outline ); + } + + return error; +} + + +static +void ft_outline_glyph_transform( FT_OutlineGlyph glyph, + FT_Matrix* matrix, + FT_Vector* delta ) { + if ( matrix ) { + FT_Outline_Transform( &glyph->outline, matrix ); + } + + if ( delta ) { + FT_Outline_Translate( &glyph->outline, delta->x, delta->y ); + } +} + + +static +void ft_outline_glyph_bbox( FT_OutlineGlyph glyph, + FT_BBox* bbox ) { + FT_Outline_Get_CBox( &glyph->outline, bbox ); +} + + +static +FT_Error ft_outline_glyph_prepare( FT_OutlineGlyph glyph, + FT_GlyphSlot slot ) { + slot->format = ft_glyph_format_outline; + slot->outline = glyph->outline; + slot->outline.flags &= ~ft_outline_owner; + + return FT_Err_Ok; +} + + +const FT_Glyph_Class ft_outline_glyph_class = +{ + sizeof( FT_OutlineGlyphRec ), + ft_glyph_format_outline, + + (FT_Glyph_Init_Func) ft_outline_glyph_init, + (FT_Glyph_Done_Func) ft_outline_glyph_done, + (FT_Glyph_Copy_Func) ft_outline_glyph_copy, + (FT_Glyph_Transform_Func)ft_outline_glyph_transform, + (FT_Glyph_BBox_Func) ft_outline_glyph_bbox, + (FT_Glyph_Prepare_Func) ft_outline_glyph_prepare +}; + + +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** FT_Glyph class and API ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ + +static +FT_Error ft_new_glyph( FT_Library library, + const FT_Glyph_Class* clazz, + FT_Glyph* aglyph ) { + FT_Memory memory = library->memory; + FT_Error error; + FT_Glyph glyph; + + + *aglyph = 0; + + if ( !ALLOC( glyph, clazz->glyph_size ) ) { + glyph->library = library; + glyph->clazz = clazz; + glyph->format = clazz->glyph_format; + + *aglyph = glyph; + } + + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Glyph_Copy */ +/* */ +/* */ +/* A function used to copy a glyph image. */ +/* */ +/* */ +/* source :: A handle to the source glyph object. */ +/* */ +/* */ +/* target :: A handle to the target glyph object. 0 in case of */ +/* error. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Glyph_Copy( FT_Glyph source, + FT_Glyph * target ) +{ + FT_Glyph copy; + FT_Error error; + const FT_Glyph_Class* clazz; + + + /* check arguments */ + if ( !target || !source || !source->clazz ) { + error = FT_Err_Invalid_Argument; + goto Exit; + } + + *target = 0; + + clazz = source->clazz; + error = ft_new_glyph( source->library, clazz, © ); + if ( error ) { + goto Exit; + } + + if ( clazz->glyph_copy ) { + error = clazz->glyph_copy( source, copy ); + } + + if ( error ) { + FT_Done_Glyph( copy ); + } else { + *target = copy; + } + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Get_Glyph */ +/* */ +/* */ +/* A function used to extract a glyph image from a slot. */ +/* */ +/* */ +/* slot :: A handle to the source glyph slot. */ +/* */ +/* */ +/* aglyph :: A handle to the glyph object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Get_Glyph( FT_GlyphSlot slot, + FT_Glyph * aglyph ) +{ + FT_Library library = slot->library; + FT_Error error; + FT_Glyph glyph; + + const FT_Glyph_Class* clazz = 0; + + + if ( !slot ) { + return FT_Err_Invalid_Slot_Handle; + } + + if ( !aglyph ) { + return FT_Err_Invalid_Argument; + } + + /* if it is a bitmap, that's easy :-) */ + if ( slot->format == ft_glyph_format_bitmap ) { + clazz = &ft_bitmap_glyph_class; + } + /* it it is an outline too */ + else if ( slot->format == ft_glyph_format_outline ) { + clazz = &ft_outline_glyph_class; + } else + { + /* try to find a renderer that supports the glyph image format */ + FT_Renderer render = FT_Lookup_Renderer( library, slot->format, 0 ); + + + if ( render ) { + clazz = &render->glyph_class; + } + } + + if ( !clazz ) { + error = FT_Err_Invalid_Glyph_Format; + goto Exit; + } + + /* create FT_Glyph object */ + error = ft_new_glyph( library, clazz, &glyph ); + if ( error ) { + goto Exit; + } + + /* copy advance while converting it to 16.16 format */ + glyph->advance.x = slot->advance.x << 10; + glyph->advance.y = slot->advance.y << 10; + + /* now import the image from the glyph slot */ + error = clazz->glyph_init( glyph, slot ); + + /* if an error occurred, destroy the glyph */ + if ( error ) { + FT_Done_Glyph( glyph ); + } else { + *aglyph = glyph; + } + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Glyph_Transform */ +/* */ +/* */ +/* Transforms a glyph image if its format is scalable. */ +/* */ +/* */ +/* glyph :: A handle to the target glyph object. */ +/* */ +/* matrix :: A pointer to a 2x2 matrix to apply. */ +/* */ +/* delta :: A pointer to a 2d vector to apply. Coordinates are */ +/* expressed in 1/64th of a pixel. */ +/* */ +/* */ +/* FreeType error code (the glyph format is not scalable if it is */ +/* not zero). */ +/* */ +/* */ +/* The 2x2 transformation matrix is also applied to the glyph's */ +/* advance vector. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Glyph_Transform( FT_Glyph glyph, + FT_Matrix * matrix, + FT_Vector * delta ) +{ + const FT_Glyph_Class* clazz; + FT_Error error = FT_Err_Ok; + + + if ( !glyph || !glyph->clazz ) { + error = FT_Err_Invalid_Argument; + } else + { + clazz = glyph->clazz; + if ( clazz->glyph_transform ) { + /* transform glyph image */ + clazz->glyph_transform( glyph, matrix, delta ); + + /* transform advance vector */ + if ( matrix ) { + FT_Vector_Transform( &glyph->advance, matrix ); + } + } else { + error = FT_Err_Invalid_Glyph_Format; + } + } + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Glyph_Get_CBox */ +/* */ +/* */ +/* Returns the glyph image's bounding box. */ +/* */ +/* */ +/* glyph :: A handle to the source glyph object. */ +/* */ +/* mode :: A set of bit flags that indicate how to interpret the */ +/* returned bounding box values. */ +/* */ +/* */ +/* box :: The glyph bounding box. Coordinates are expressed in */ +/* 1/64th of pixels if it is grid-fitted. */ +/* */ +/* */ +/* Coordinates are relative to the glyph origin, using the Y-upwards */ +/* convention. */ +/* */ +/* If `ft_glyph_bbox_subpixels' is set in `mode', the bbox */ +/* coordinates are returned in 26.6 pixels (i.e. 1/64th of pixels). */ +/* Otherwise, coordinates are expressed in integer pixels. */ +/* */ +/* Note that the maximum coordinates are exclusive, which means that */ +/* one can compute the width and height of the glyph image (be it in */ +/* integer or 26.6 pixels) as: */ +/* */ +/* width = bbox.xMax - bbox.xMin; */ +/* height = bbox.yMax - bbox.yMin; */ +/* */ +/* Note also that for 26.6 coordinates, if the */ +/* `ft_glyph_bbox_gridfit' flag is set in `mode;, the coordinates */ +/* will also be grid-fitted, which corresponds to: */ +/* */ +/* bbox.xMin = FLOOR(bbox.xMin); */ +/* bbox.yMin = FLOOR(bbox.yMin); */ +/* bbox.xMax = CEILING(bbox.xMax); */ +/* bbox.yMax = CEILING(bbox.yMax); */ +/* */ +/* The default value (0) for `bbox_mode' is `ft_glyph_bbox_pixels'. */ +/* */ +FT_EXPORT_FUNC( void ) FT_Glyph_Get_CBox( FT_Glyph glyph, + FT_UInt bbox_mode, + FT_BBox * cbox ) +{ + const FT_Glyph_Class* clazz; + FT_Error error = FT_Err_Ok; + + + if ( !cbox || !glyph || !glyph->clazz ) { + error = FT_Err_Invalid_Argument; + } else + { + clazz = glyph->clazz; + if ( !clazz->glyph_bbox ) { + error = FT_Err_Invalid_Glyph_Format; + } else + { + /* retrieve bbox in 26.6 coordinates */ + clazz->glyph_bbox( glyph, cbox ); + + /* perform grid fitting if needed */ + if ( bbox_mode & ft_glyph_bbox_gridfit ) { + cbox->xMin &= -64; + cbox->yMin &= -64; + cbox->xMax = ( cbox->xMax + 63 ) & - 64; + cbox->yMax = ( cbox->yMax + 63 ) & - 64; + } + + /* convert to integer pixels if needed */ + if ( !( bbox_mode & ft_glyph_bbox_subpixels ) ) { + cbox->xMin >>= 6; + cbox->yMin >>= 6; + cbox->xMax >>= 6; + cbox->yMax >>= 6; + } + } + } + return; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Glyph_To_Bitmap */ +/* */ +/* */ +/* Converts a given glyph object to a bitmap glyph object. */ +/* */ +/* */ +/* glyph :: A pointer to a handle to the target glyph. */ +/* */ +/* */ +/* render_mode :: A set of bit flags that describe how the data is */ +/* */ +/* */ +/* origin :: A pointer to a vector used to translate the glyph */ +/* image before rendering. Can be 0 (if no */ +/* translation). The origin is expressed in */ +/* 26.6 pixels. */ +/* */ +/* destroy :: A boolean that indicates that the original glyph */ +/* image should be destroyed by this function. It is */ +/* never destroyed in case of error. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* The glyph image is translated with the `origin' vector before */ +/* rendering. In case of error, it it translated back to its */ +/* original position and the glyph is left untouched. */ +/* */ +/* The first parameter is a pointer to a FT_Glyph handle, that will */ +/* be replaced by this function. Typically, you would use (omitting */ +/* error handling): */ +/* */ +/* */ +/* { */ +/* FT_Glyph glyph; */ +/* FT_BitmapGlyph glyph_bitmap; */ +/* */ +/* */ +/* // load glyph */ +/* error = FT_Load_Char( face, glyph_index, FT_LOAD_DEFAUT ); */ +/* */ +/* // extract glyph image */ +/* error = FT_Get_Glyph( face->glyph, &glyph ); */ +/* */ +/* // convert to a bitmap (default render mode + destroy old) */ +/* if ( glyph->format != ft_glyph_format_bitmap ) */ +/* { */ +/* error = FT_Glyph_To_Bitmap( &glyph, ft_render_mode_default, */ +/* 0, 1 ); */ +/* if ( error ) // glyph unchanged */ +/* ... */ +/* } */ +/* */ +/* // access bitmap content by typecasting */ +/* glyph_bitmap = (FT_BitmapGlyph)glyph; */ +/* */ +/* // do funny stuff with it, like blitting/drawing */ +/* ... */ +/* */ +/* // discard glyph image (bitmap or not) */ +/* FT_Done_Glyph( glyph ); */ +/* } */ +/* */ +/* */ +/* This function will always fail if the glyph's format isn't */ +/* scalable. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Glyph_To_Bitmap( FT_Glyph * the_glyph, + FT_ULong render_mode, + FT_Vector * origin, + FT_Bool destroy ) +{ + FT_GlyphSlotRec dummy; + FT_Error error; + FT_Glyph glyph; + FT_BitmapGlyph bitmap; + + const FT_Glyph_Class* clazz; + + + /* check argument */ + if ( !the_glyph ) { + goto Bad; + } + + /* we render the glyph into a glyph bitmap using a `dummy' glyph slot */ + /* then calling FT_Render_Glyph_Internal() */ + + glyph = *the_glyph; + if ( !glyph ) { + goto Bad; + } + + clazz = glyph->clazz; + if ( !clazz || !clazz->glyph_prepare ) { + goto Bad; + } + + MEM_Set( &dummy, 0, sizeof( dummy ) ); + dummy.library = glyph->library; + dummy.format = clazz->glyph_format; + + /* if `origin' is set, translate the glyph image */ + if ( origin ) { + FT_Glyph_Transform( glyph, 0, origin ); + } + + /* create result bitmap glyph */ + error = ft_new_glyph( glyph->library, &ft_bitmap_glyph_class, + (FT_Glyph*)&bitmap ); + if ( error ) { + goto Exit; + } + + /* prepare dummy slot for rendering */ + error = clazz->glyph_prepare( glyph, &dummy ) || + FT_Render_Glyph_Internal( glyph->library, &dummy, render_mode ); + + if ( !destroy && origin ) { + FT_Vector v; + + + v.x = -origin->x; + v.y = -origin->y; + FT_Glyph_Transform( glyph, 0, &v ); + } + + /* in case of succes, copy the bitmap to the glyph bitmap */ + if ( !error ) { + error = ft_bitmap_glyph_init( bitmap, &dummy ); + if ( error ) { + /* this should never happen, but let's be safe */ + FT_Done_Glyph( FT_GLYPH( bitmap ) ); + goto Exit; + } + + if ( destroy ) { + FT_Done_Glyph( glyph ); + } + + *the_glyph = FT_GLYPH( bitmap ); + } + +Exit: + return error; + +Bad: + error = FT_Err_Invalid_Argument; + goto Exit; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Done_Glyph */ +/* */ +/* */ +/* Destroys a given glyph. */ +/* */ +/* */ +/* glyph :: A handle to the target glyph object. */ +/* */ +FT_EXPORT_FUNC( void ) FT_Done_Glyph( FT_Glyph glyph ) +{ + if ( glyph ) { + FT_Memory memory = glyph->library->memory; + const FT_Glyph_Class* clazz = glyph->clazz; + + + if ( clazz->glyph_done ) { + clazz->glyph_done( glyph ); + } + + FREE( glyph ); + } +} + + +#if 0 + +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** EXPERIMENTAL EMBOLDENING/OUTLINING SUPPORT ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ + +/* Compute the norm of a vector */ + +#ifdef FT_CONFIG_OPTION_OLD_CALCS + +static +FT_Pos ft_norm( FT_Vector* vec ) { + FT_Int64 t1, t2; + + + MUL_64( vec->x, vec->x, t1 ); + MUL_64( vec->y, vec->y, t2 ); + ADD_64( t1, t2, t1 ); + + return (FT_Pos)SQRT_64( t1 ); +} + +#else /* FT_CONFIG_OPTION_OLD_CALCS */ + +static +FT_Pos ft_norm( FT_Vector* vec ) { + FT_F26Dot6 u, v, d; + FT_Int shift; + FT_ULong H, L, L2, hi, lo, med; + + + u = vec->x; if ( u < 0 ) { + u = -u; + } + v = vec->y; if ( v < 0 ) { + v = -v; + } + + if ( u < v ) { + d = u; + u = v; + v = d; + } + + /* check that we are not trying to normalize zero! */ + if ( u == 0 ) { + return 0; + } + + /* compute (u*u + v*v) on 64 bits with two 32-bit registers [H:L] */ + hi = (FT_ULong)u >> 16; + lo = (FT_ULong)u & 0xFFFF; + med = hi * lo; + + H = hi * hi + ( med >> 15 ); + med <<= 17; + L = lo * lo + med; + if ( L < med ) { + H++; + } + + hi = (FT_ULong)v >> 16; + lo = (FT_ULong)v & 0xFFFF; + med = hi * lo; + + H += hi * hi + ( med >> 15 ); + med <<= 17; + L2 = lo * lo + med; + if ( L2 < med ) { + H++; + } + + L += L2; + if ( L < L2 ) { + H++; + } + + /* if the value is smaller than 32 bits */ + shift = 0; + if ( H == 0 ) { + while ( ( L & 0xC0000000UL ) == 0 ) + { + L <<= 2; + shift++; + } + return ( FT_Sqrt32( L ) >> shift ); + } else + { + while ( H ) + { + L = ( L >> 2 ) | ( H << 30 ); + H >>= 2; + shift++; + } + return ( FT_Sqrt32( L ) << shift ); + } +} + +#endif /* FT_CONFIG_OPTION_OLD_CALCS */ + + +static +int ft_test_extrema( FT_Outline* outline, + int n ) { + FT_Vector *prev, *cur, *next; + FT_Pos product; + FT_Int first, last; + + + /* we need to compute the `previous' and `next' point */ + /* for these extrema. */ + cur = outline->points + n; + prev = cur - 1; + next = cur + 1; + + first = 0; + for ( c = 0; c < outline->n_contours; c++ ) + { + last = outline->contours[c]; + + if ( n == first ) { + prev = outline->points + last; + } + + if ( n == last ) { + next = outline->points + first; + } + + first = last + 1; + } + + product = FT_MulDiv( cur->x - prev->x, /* in.x */ + next->y - cur->y, /* out.y */ + 0x40 ) + - + FT_MulDiv( cur->y - prev->y, /* in.y */ + next->x - cur->x, /* out.x */ + 0x40 ); + + if ( product ) { + product = product > 0 ? 1 : -1; + } + + return product; +} + + +/* Compute the orientation of path filling. It differs between TrueType */ +/* and Type1 formats. We could use the `ft_outline_reverse_fill' flag, */ +/* but it is better to re-compute it directly (it seems that this flag */ +/* isn't correctly set for some weird composite glyphs currently). */ +/* */ +/* We do this by computing bounding box points, and computing their */ +/* curvature. */ +/* */ +/* The function returns either 1 or -1. */ +/* */ +static +int ft_get_orientation( FT_Outline* outline ) { + FT_BBox box; + FT_BBox indices; + int n, last; + + + indices.xMin = -1; + indices.yMin = -1; + indices.xMax = -1; + indices.yMax = -1; + + box.xMin = box.yMin = 32767; + box.xMax = box.yMax = -32768; + + /* is it empty ? */ + if ( outline->n_contours < 1 ) { + return 1; + } + + last = outline->contours[outline->n_contours - 1]; + + for ( n = 0; n <= last; n++ ) + { + FT_Pos x, y; + + + x = outline->points[n].x; + if ( x < box.xMin ) { + box.xMin = x; + indices.xMin = n; + } + if ( x > box.xMax ) { + box.xMax = x; + indices.xMax = n; + } + + y = outline->points[n].y; + if ( y < box.yMin ) { + box.yMin = y; + indices.yMin = n; + } + if ( y > box.yMax ) { + box.yMax = y; + indices.yMax = n; + } + } + + /* test orientation of the xmin */ + return ft_test_extrema( outline, indices.xMin ) || + ft_test_extrema( outline, indices.yMin ) || + ft_test_extrema( outline, indices.xMax ) || + ft_test_extrema( outline, indices.yMax ) || + 1; /* this is an empty glyph? */ +} + + +static +FT_Error ft_embolden( FT_Face original, + FT_Outline* outline, + FT_Pos* advance ) { + FT_Vector u, v; + FT_Vector* points; + FT_Vector cur, prev, next; + FT_Pos distance; + int c, n, first, orientation; + + FT_UNUSED( advance ); + + + /* compute control distance */ + distance = FT_MulFix( original->em_size / 60, + original->size->metrics.y_scale ); + + orientation = ft_get_orientation( &original->glyph->outline ); + + points = original->glyph->outline.points; + + first = 0; + for ( c = 0; c < outline->n_contours; c++ ) + { + int last = outline->contours[c]; + + + prev = points[last]; + + for ( n = first; n <= last; n++ ) + { + FT_Pos norm, delta, d; + FT_Vector in, out; + + + cur = points[n]; + if ( n < last ) { + next = points[n + 1]; + } else { next = points[first];} + + /* compute the in and out vectors */ + in.x = cur.x - prev.x; + in.y = cur.y - prev.y; + + out.x = next.x - cur.x; + out.y = next.y - cur.y; + + /* compute U and V */ + norm = ft_norm( &in ); + u.x = orientation * FT_DivFix( in.y, norm ); + u.y = orientation * -FT_DivFix( in.x, norm ); + + norm = ft_norm( &out ); + v.x = orientation * FT_DivFix( out.y, norm ); + v.y = orientation * -FT_DivFix( out.x, norm ); + + d = distance; + + if ( ( outline->flags[n] & FT_Curve_Tag_On ) == 0 ) { + d *= 2; + } + + /* Check discriminant for parallel vectors */ + delta = FT_MulFix( u.x, v.y ) - FT_MulFix( u.y, v.x ); + if ( delta > FT_BOLD_THRESHOLD || delta < -FT_BOLD_THRESHOLD ) { + /* Move point -- compute A and B */ + FT_Pos x, y, A, B; + + + A = d + FT_MulFix( cur.x, u.x ) + FT_MulFix( cur.y, u.y ); + B = d + FT_MulFix( cur.x, v.x ) + FT_MulFix( cur.y, v.y ); + + x = FT_MulFix( A, v.y ) - FT_MulFix( B, u.y ); + y = FT_MulFix( B, u.x ) - FT_MulFix( A, v.x ); + + outline->points[n].x = distance + FT_DivFix( x, delta ); + outline->points[n].y = distance + FT_DivFix( y, delta ); + } else + { + /* Vectors are nearly parallel */ + FT_Pos x, y; + + + x = distance + cur.x + FT_MulFix( d, u.x + v.x ) / 2; + y = distance + cur.y + FT_MulFix( d, u.y + v.y ) / 2; + + outline->points[n].x = x; + outline->points[n].y = y; + } + + prev = cur; + } + + first = last + 1; + } + + if ( advance ) { + *advance = ( *advance + distance * 4 ) & - 64; + } + + return 0; +} + +#endif /* 0 -- EXPERIMENTAL STUFF! */ + + +/* END */ diff --git a/src/ft2/ftglyph.h b/src/ft2/ftglyph.h new file mode 100644 index 0000000..4aaa2f9 --- /dev/null +++ b/src/ft2/ftglyph.h @@ -0,0 +1,422 @@ +/***************************************************************************/ +/* */ +/* ftglyph.h */ +/* */ +/* FreeType convenience functions to handle glyphs (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +/*************************************************************************/ +/* */ +/* This file contains the definition of several convenience functions */ +/* that can be used by client applications to easily retrieve glyph */ +/* bitmaps and outlines from a given face. */ +/* */ +/* These functions should be optional if you are writing a font server */ +/* or text layout engine on top of FreeType. However, they are pretty */ +/* handy for many other simple uses of the library. */ +/* */ +/*************************************************************************/ + + +#ifndef FTGLYPH_H +#define FTGLYPH_H + +#include "freetype.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* forward declaration to a private type */ +typedef struct FT_Glyph_Class_ FT_Glyph_Class; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_GlyphRec */ +/* */ +/* */ +/* The root glyph structure contains a given glyph image plus its */ +/* advance width in 16.16 fixed float format. */ +/* */ +/* */ +/* library :: A handle to the FreeType library object. */ +/* */ +/* clazz :: A pointer to the glyph's class. Private. */ +/* */ +/* format :: The format of the glyph's image. */ +/* */ +/* advance :: A 16.16 vector that gives the glyph's advance width. */ +/* */ +typedef struct FT_GlyphRec_ +{ + FT_Library library; + const FT_Glyph_Class* clazz; + FT_Glyph_Format format; + FT_Vector advance; + +} FT_GlyphRec, *FT_Glyph; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_BitmapGlyphRec */ +/* */ +/* */ +/* A structure used for bitmap glyph images. This really is a */ +/* `sub-class' of `FT_GlyphRec'. */ +/* */ +/* */ +/* root :: The root FT_Glyph fields. */ +/* */ +/* left :: The left-side bearing, i.e., the horizontal distance */ +/* from the current pen position to the left border of the */ +/* glyph bitmap. */ +/* */ +/* top :: The top-side bearing, i.e., the vertical distance from */ +/* the current pen position to the top border of the glyph */ +/* bitmap. This distance is positive for upwards-y! */ +/* */ +/* bitmap :: A descriptor for the bitmap. */ +/* */ +/* */ +/* You can typecast FT_Glyph to FT_BitmapGlyph if you have */ +/* glyph->format == ft_glyph_format_bitmap. This lets you access */ +/* the bitmap's contents easily. */ +/* */ +/* The corresponding pixel buffer is always owned by the BitmapGlyph */ +/* and is thus created and destroyed with it. */ +/* */ +typedef struct FT_BitmapGlyphRec_ +{ + FT_GlyphRec root; + FT_Int left; + FT_Int top; + FT_Bitmap bitmap; + +} FT_BitmapGlyphRec, *FT_BitmapGlyph; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_OutlineGlyphRec */ +/* */ +/* */ +/* A structure used for outline (vectorial) glyph images. This */ +/* really is a `sub-class' of `FT_GlyphRec'. */ +/* */ +/* */ +/* root :: The root FT_Glyph fields. */ +/* */ +/* outline :: A descriptor for the outline. */ +/* */ +/* */ +/* You can typecast FT_Glyph to FT_OutlineGlyph if you have */ +/* glyph->format == ft_glyph_format_outline. This lets you access */ +/* the outline's content easily. */ +/* */ +/* As the outline is extracted from a glyph slot, its coordinates are */ +/* expressed normally in 26.6 pixels, unless the flag */ +/* FT_LOAD_NO_SCALE was used in FT_Load_Glyph() or FT_Load_Char(). */ +/* */ +/* The outline's tables are always owned by the object and are */ +/* destroyed with it. */ +/* */ +typedef struct FT_OutlineGlyphRec_ +{ + FT_GlyphRec root; + FT_Outline outline; + +} FT_OutlineGlyphRec, *FT_OutlineGlyph; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Get_Glyph */ +/* */ +/* */ +/* A function used to extract a glyph image from a slot. */ +/* */ +/* */ +/* slot :: A handle to the source glyph slot. */ +/* */ +/* */ +/* aglyph :: A handle to the glyph object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Get_Glyph( FT_GlyphSlot slot, + FT_Glyph * aglyph ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Glyph_Copy */ +/* */ +/* */ +/* A function used to copy a glyph image. */ +/* */ +/* */ +/* source :: A handle to the source glyph object. */ +/* */ +/* */ +/* target :: A handle to the target glyph object. 0 in case of */ +/* error. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Glyph_Copy( FT_Glyph source, + FT_Glyph * target ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Glyph_Transform */ +/* */ +/* */ +/* Transforms a glyph image if its format is scalable. */ +/* */ +/* */ +/* glyph :: A handle to the target glyph object. */ +/* */ +/* matrix :: A pointer to a 2x2 matrix to apply. */ +/* */ +/* delta :: A pointer to a 2d vector to apply. Coordinates are */ +/* expressed in 1/64th of a pixel. */ +/* */ +/* */ +/* FreeType error code (the glyph format is not scalable if it is */ +/* not zero). */ +/* */ +/* */ +/* The 2x2 transformation matrix is also applied to the glyph's */ +/* advance vector. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Glyph_Transform( FT_Glyph glyph, + FT_Matrix * matrix, + FT_Vector * delta ); + + +enum +{ + ft_glyph_bbox_pixels = 0, + ft_glyph_bbox_subpixels = 1, + ft_glyph_bbox_gridfit = 2 +}; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Glyph_Get_CBox */ +/* */ +/* */ +/* Returns the glyph image's bounding box. */ +/* */ +/* */ +/* glyph :: A handle to the source glyph object. */ +/* */ +/* mode :: A set of bit flags that indicate how to interpret the */ +/* returned bounding box values. */ +/* */ +/* */ +/* box :: The glyph bounding box. Coordinates are expressed in */ +/* 1/64th of pixels if it is grid-fitted. */ +/* */ +/* */ +/* Coordinates are relative to the glyph origin, using the Y-upwards */ +/* convention. */ +/* */ +/* If `ft_glyph_bbox_subpixels' is set in `mode', the bbox */ +/* coordinates are returned in 26.6 pixels (i.e. 1/64th of pixels). */ +/* Otherwise, coordinates are expressed in integer pixels. */ +/* */ +/* Note that the maximum coordinates are exclusive, which means that */ +/* one can compute the width and height of the glyph image (be it in */ +/* integer or 26.6 pixels) as: */ +/* */ +/* width = bbox.xMax - bbox.xMin; */ +/* height = bbox.yMax - bbox.yMin; */ +/* */ +/* Note also that for 26.6 coordinates, if the */ +/* `ft_glyph_bbox_gridfit' flag is set in `mode;, the coordinates */ +/* will also be grid-fitted, which corresponds to: */ +/* */ +/* bbox.xMin = FLOOR(bbox.xMin); */ +/* bbox.yMin = FLOOR(bbox.yMin); */ +/* bbox.xMax = CEILING(bbox.xMax); */ +/* bbox.yMax = CEILING(bbox.yMax); */ +/* */ +/* The default value (0) for `bbox_mode' is `ft_glyph_bbox_pixels'. */ +/* */ +FT_EXPORT_DEF( void ) FT_Glyph_Get_CBox( FT_Glyph glyph, + FT_UInt bbox_mode, + FT_BBox * cbox ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Glyph_To_Bitmap */ +/* */ +/* */ +/* Converts a given glyph object to a bitmap glyph object. */ +/* */ +/* */ +/* glyph :: A pointer to a handle to the target glyph. */ +/* */ +/* */ +/* render_mode :: A set of bit flags that describe how the data is */ +/* */ +/* */ +/* origin :: A pointer to a vector used to translate the glyph */ +/* image before rendering. Can be 0 (if no */ +/* translation). The origin is expressed in */ +/* 26.6 pixels. */ +/* */ +/* destroy :: A boolean that indicates that the original glyph */ +/* image should be destroyed by this function. It is */ +/* never destroyed in case of error. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* The glyph image is translated with the `origin' vector before */ +/* rendering. In case of error, it it translated back to its */ +/* original position and the glyph is left untouched. */ +/* */ +/* The first parameter is a pointer to a FT_Glyph handle, that will */ +/* be replaced by this function. Typically, you would use (omitting */ +/* error handling): */ +/* */ +/* */ +/* { */ +/* FT_Glyph glyph; */ +/* FT_BitmapGlyph glyph_bitmap; */ +/* */ +/* */ +/* // load glyph */ +/* error = FT_Load_Char( face, glyph_index, FT_LOAD_DEFAUT ); */ +/* */ +/* // extract glyph image */ +/* error = FT_Get_Glyph( face->glyph, &glyph ); */ +/* */ +/* // convert to a bitmap (default render mode + destroy old) */ +/* if ( glyph->format != ft_glyph_format_bitmap ) */ +/* { */ +/* error = FT_Glyph_To_Bitmap( &glyph, ft_render_mode_default, */ +/* 0, 1 ); */ +/* if ( error ) // glyph unchanged */ +/* ... */ +/* } */ +/* */ +/* // access bitmap content by typecasting */ +/* glyph_bitmap = (FT_BitmapGlyph)glyph; */ +/* */ +/* // do funny stuff with it, like blitting/drawing */ +/* ... */ +/* */ +/* // discard glyph image (bitmap or not) */ +/* FT_Done_Glyph( glyph ); */ +/* } */ +/* */ +/* */ +/* This function will always fail if the glyph's format isn't */ +/* scalable. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Glyph_To_Bitmap( FT_Glyph * the_glyph, + FT_ULong render_mode, + FT_Vector * origin, + FT_Bool destroy ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Done_Glyph */ +/* */ +/* */ +/* Destroys a given glyph. */ +/* */ +/* */ +/* glyph :: A handle to the target glyph object. */ +/* */ +FT_EXPORT_DEF( void ) FT_Done_Glyph( FT_Glyph glyph ); + + +/* other helpful functions */ + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Matrix_Multiply */ +/* */ +/* */ +/* Performs the matrix operation `b = a*b'. */ +/* */ +/* */ +/* a :: A pointer to matrix `a'. */ +/* */ +/* */ +/* b :: A pointer to matrix `b'. */ +/* */ +/* */ +/* Yes. */ +/* */ +/* */ +/* The result is undefined if either `a' or `b' is zero. */ +/* */ +FT_EXPORT_DEF( void ) FT_Matrix_Multiply( FT_Matrix * a, + FT_Matrix * b ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Matrix_Invert */ +/* */ +/* */ +/* Inverts a 2x2 matrix. Returns an error if it can't be inverted. */ +/* */ +/* */ +/* matrix :: A pointer to the target matrix. Remains untouched in */ +/* case of error. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* Yes. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Matrix_Invert( FT_Matrix * matrix ); + + +#ifdef __cplusplus +} +#endif + +#endif /* FTGLYPH_H */ + + +/* END */ diff --git a/src/ft2/ftgrays.c b/src/ft2/ftgrays.c new file mode 100644 index 0000000..55f2ac9 --- /dev/null +++ b/src/ft2/ftgrays.c @@ -0,0 +1,1996 @@ +/***************************************************************************/ +/* */ +/* ftgrays.c */ +/* */ +/* A new `perfect' anti-aliasing renderer (body). */ +/* */ +/* Copyright 2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + +/*************************************************************************/ +/* */ +/* This file can be compiled without the rest of the FreeType engine, */ +/* by defining the _STANDALONE_ macro when compiling it. You also need */ +/* to put the files `ftgrays.h' and `ftimage.h' into the current */ +/* compilation directory. Typically, you could do something like */ +/* */ +/* - copy `src/base/ftgrays.c' to your current directory */ +/* */ +/* - copy `include/freetype/ftimage.h' and */ +/* `include/freetype/ftgrays.h' to the same directory */ +/* */ +/* - compile `ftgrays' with the _STANDALONE_ macro defined, as in */ +/* */ +/* cc -c -D_STANDALONE_ ftgrays.c */ +/* */ +/* The renderer can be initialized with a call to */ +/* `ft_grays_raster.grays_raster_new'; an anti-aliased bitmap can be */ +/* generated with a call to `ft_grays_raster.grays_raster_render'. */ +/* */ +/* See the comments and documentation in the file `ftimage.h' for */ +/* more details on how the raster works. */ +/* */ +/*************************************************************************/ + +/*************************************************************************/ +/* */ +/* This is a new anti-aliasing scan-converter for FreeType 2. The */ +/* algorithm used here is _very_ different from the one in the standard */ +/* `ftraster' module. Actually, `ftgrays' computes the _exact_ */ +/* coverage of the outline on each pixel cell. */ +/* */ +/* It is based on ideas that I initially found in Raph Levien's */ +/* excellent LibArt graphics library (see http://www.levien.com/libart */ +/* for more information, though the web pages do not tell anything */ +/* about the renderer; you'll have to dive into the source code to */ +/* understand how it works). */ +/* */ +/* Note, however, that this is a _very_ different implementation */ +/* compared to Raph's. Coverage information is stored in a very */ +/* different way, and I don't use sorted vector paths. Also, it */ +/* doesn't use floating point values. */ +/* */ +/* This renderer has the following advantages: */ +/* */ +/* - It doesn't need an intermediate bitmap. Instead, one can supply */ +/* a callback function that will be called by the renderer to draw */ +/* gray spans on any target surface. You can thus do direct */ +/* composition on any kind of bitmap, provided that you give the */ +/* renderer the right callback. */ +/* */ +/* - A perfect anti-aliaser, i.e., it computes the _exact_ coverage on */ +/* each pixel cell */ +/* */ +/* - It performs a single pass on the outline (the `standard' FT2 */ +/* renderer makes two passes). */ +/* */ +/* - It can easily be modified to render to _any_ number of gray levels */ +/* cheaply. */ +/* */ +/* - For small (< 20) pixel sizes, it is faster than the standard */ +/* renderer. */ +/* */ +/*************************************************************************/ + + +#include /* for memcpy() */ + + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_aaraster + + +#ifdef _STANDALONE_ + + +#define ErrRaster_Invalid_Mode -2 +#define ErrRaster_Invalid_Outline -1 + +#include "ftimage.h" +#include "ftgrays.h" + +/* This macro is used to indicate that a function parameter is unused. */ +/* Its purpose is simply to reduce compiler warnings. Note also that */ +/* simply defining it as `(void)x' doesn't avoid warnings with certain */ +/* ANSI compilers (e.g. LCC). */ +#define FT_UNUSED( x ) ( x ) = ( x ) + +/* Disable the tracing mechanism for simplicity -- developers can */ +/* activate it easily by redefining these two macros. */ +#ifndef FT_ERROR +#define FT_ERROR( x ) do ;while ( 0 ) /* nothing */ +#endif + +#ifndef FT_TRACE +#define FT_TRACE( x ) do ;while ( 0 ) /* nothing */ +#endif + + +#else /* _STANDALONE_ */ + + + +#include "ftgrays.h" + + +#include "ftobjs.h" /* for FT_UNUSED() */ +#include "ftdebug.h" /* for FT_TRACE() and FT_ERROR() */ +#include "ftoutln.h" /* for FT_Outline_Decompose() */ + +#define ErrRaster_Invalid_Mode FT_Err_Cannot_Render_Glyph +#define ErrRaster_Invalid_Outline FT_Err_Invalid_Outline + + +#endif /* _STANDALONE_ */ + + +/* define this to dump debugging information */ +#define xxxDEBUG_GRAYS + +/* as usual, for the speed hungry :-) */ + +#ifndef FT_STATIC_RASTER + + +#define RAS_ARG PRaster raster +#define RAS_ARG_ PRaster raster, + +#define RAS_VAR raster +#define RAS_VAR_ raster, + +#define ras ( *raster ) + + +#else /* FT_STATIC_RASTER */ + + +#define RAS_ARG /* empty */ +#define RAS_ARG_ /* empty */ +#define RAS_VAR /* empty */ +#define RAS_VAR_ /* empty */ + +static TRaster ras; + + +#endif /* FT_STATIC_RASTER */ + + +/* must be at least 6 bits! */ +#define PIXEL_BITS 8 + +#define ONE_PIXEL ( 1L << PIXEL_BITS ) +#define PIXEL_MASK ( -1L << PIXEL_BITS ) +#define TRUNC( x ) ( ( x ) >> PIXEL_BITS ) +#define SUBPIXELS( x ) ( ( x ) << PIXEL_BITS ) +#define FLOOR( x ) ( ( x ) & - ONE_PIXEL ) +#define CEILING( x ) ( ( ( x ) + ONE_PIXEL - 1 ) & - ONE_PIXEL ) +#define ROUND( x ) ( ( ( x ) + ONE_PIXEL / 2 ) & - ONE_PIXEL ) + +#if PIXEL_BITS >= 6 +#define UPSCALE( x ) ( ( x ) << ( PIXEL_BITS - 6 ) ) +#define DOWNSCALE( x ) ( ( x ) >> ( PIXEL_BITS - 6 ) ) +#else +#define UPSCALE( x ) ( ( x ) >> ( 6 - PIXEL_BITS ) ) +#define DOWNSCALE( x ) ( ( x ) << ( 6 - PIXEL_BITS ) ) +#endif + +/* Define this if you want to use a more compact storage scheme. This */ +/* increases the number of cells available in the render pool but slows */ +/* down the rendering a bit. It is useful if you have a really tiny */ +/* render pool. */ +#define xxxGRAYS_COMPACT + + +/*************************************************************************/ +/* */ +/* TYPE DEFINITIONS */ +/* */ +typedef int TScan; /* integer scanline/pixel coordinate */ +typedef long TPos; /* sub-pixel coordinate */ + +/* maximal number of gray spans in a call to the span callback */ +#define FT_MAX_GRAY_SPANS 32 + + +#ifdef GRAYS_COMPACT + +typedef struct TCell_ +{ + short x : 14; + short y : 14; + int cover : PIXEL_BITS + 2; + int area : PIXEL_BITS * 2 + 2; + +} TCell, *PCell; + +#else /* GRAYS_COMPACT */ + +typedef struct TCell_ +{ + TScan x; + TScan y; + int cover; + int area; + +} TCell, *PCell; + +#endif /* GRAYS_COMPACT */ + + +typedef struct TRaster_ +{ + PCell cells; + int max_cells; + int num_cells; + + TScan min_ex, max_ex; + TScan min_ey, max_ey; + + int area; + int cover; + int invalid; + + TScan ex, ey; + TScan cx, cy; + TPos x, y; + + TScan last_ey; + + FT_Vector bez_stack[32 * 3]; + int lev_stack[32]; + + FT_Outline outline; + FT_Bitmap target; + + FT_Span gray_spans[FT_MAX_GRAY_SPANS]; + int num_gray_spans; + + FT_Raster_Span_Func render_span; + void* render_span_data; + int span_y; + + int band_size; + int band_shoot; + int conic_level; + int cubic_level; + + void* memory; + +} TRaster, *PRaster; + + +/*************************************************************************/ +/* */ +/* Initialize the cells table. */ +/* */ +static +void init_cells( RAS_ARG_ void* buffer, + long byte_size ) { + ras.cells = (PCell)buffer; + ras.max_cells = byte_size / sizeof( TCell ); + ras.num_cells = 0; + ras.area = 0; + ras.cover = 0; + ras.invalid = 1; +} + + +/*************************************************************************/ +/* */ +/* Compute the outline bounding box. */ +/* */ +static +void compute_cbox( RAS_ARG_ FT_Outline* outline ) { + FT_Vector* vec = outline->points; + FT_Vector* limit = vec + outline->n_points; + + + if ( outline->n_points <= 0 ) { + ras.min_ex = ras.max_ex = 0; + ras.min_ey = ras.max_ey = 0; + return; + } + + ras.min_ex = ras.max_ex = vec->x; + ras.min_ey = ras.max_ey = vec->y; + + vec++; + + for ( ; vec < limit; vec++ ) + { + TPos x = vec->x; + TPos y = vec->y; + + + if ( x < ras.min_ex ) { + ras.min_ex = x; + } + if ( x > ras.max_ex ) { + ras.max_ex = x; + } + if ( y < ras.min_ey ) { + ras.min_ey = y; + } + if ( y > ras.max_ey ) { + ras.max_ey = y; + } + } + + /* truncate the bounding box to integer pixels */ + ras.min_ex = ras.min_ex >> 6; + ras.min_ey = ras.min_ey >> 6; + ras.max_ex = ( ras.max_ex + 63 ) >> 6; + ras.max_ey = ( ras.max_ey + 63 ) >> 6; +} + + +/*************************************************************************/ +/* */ +/* Record the current cell in the table. */ +/* */ +static +int record_cell( RAS_ARG ) { + PCell cell; + + + if ( !ras.invalid && ( ras.area | ras.cover ) ) { + if ( ras.num_cells >= ras.max_cells ) { + return 1; + } + + cell = ras.cells + ras.num_cells++; + cell->x = ras.ex - ras.min_ex; + cell->y = ras.ey - ras.min_ey; + cell->area = ras.area; + cell->cover = ras.cover; + } + + return 0; +} + + +/*************************************************************************/ +/* */ +/* Set the current cell to a new position. */ +/* */ +static +int set_cell( RAS_ARG_ TScan ex, + TScan ey ) { + int invalid, record, clean; + + + /* Move the cell pointer to a new position. We set the `invalid' */ + /* flag to indicate that the cell isn't part of those we're interested */ + /* in during the render phase. This means that: */ + /* */ + /* . the new vertical position must be within min_ey..max_ey-1. */ + /* . the new horizontal position must be strictly less than max_ex */ + /* */ + /* Note that if a cell is to the left of the clipping region, it is */ + /* actually set to the (min_ex-1) horizontal position. */ + + record = 0; + clean = 1; + + invalid = ( ey < ras.min_ey || ey >= ras.max_ey || ex >= ras.max_ex ); + if ( !invalid ) { + /* All cells that are on the left of the clipping region go to the */ + /* min_ex - 1 horizontal position. */ + if ( ex < ras.min_ex ) { + ex = ras.min_ex - 1; + } + + /* if our position is new, then record the previous cell */ + if ( ex != ras.ex || ey != ras.ey ) { + record = 1; + } else { + clean = ras.invalid; /* do not clean if we didn't move from */ + } + /* a valid cell */ + } + + /* record the previous cell if needed (i.e., if we changed the cell */ + /* position, of changed the `invalid' flag) */ + if ( ( ras.invalid != invalid || record ) && record_cell( RAS_VAR ) ) { + return 1; + } + + if ( clean ) { + ras.area = 0; + ras.cover = 0; + } + + ras.invalid = invalid; + ras.ex = ex; + ras.ey = ey; + return 0; +} + + +/*************************************************************************/ +/* */ +/* Start a new contour at a given cell. */ +/* */ +static +void start_cell( RAS_ARG_ TScan ex, + TScan ey ) { + if ( ex < ras.min_ex ) { + ex = ras.min_ex - 1; + } + + ras.area = 0; + ras.cover = 0; + ras.ex = ex; + ras.ey = ey; + ras.last_ey = SUBPIXELS( ey ); + ras.invalid = 0; + + (void)set_cell( RAS_VAR_ ex, ey ); +} + + +/*************************************************************************/ +/* */ +/* Render a scanline as one or more cells. */ +/* */ +static +int render_scanline( RAS_ARG_ TScan ey, + TPos x1, + TScan y1, + TPos x2, + TScan y2 ) { + TScan ex1, ex2, fx1, fx2, delta; + long p, first, dx; + int incr, lift, mod, rem; + + + dx = x2 - x1; + + ex1 = TRUNC( x1 ); /* if (ex1 >= ras.max_ex) ex1 = ras.max_ex-1; */ + ex2 = TRUNC( x2 ); /* if (ex2 >= ras.max_ex) ex2 = ras.max_ex-1; */ + fx1 = x1 - SUBPIXELS( ex1 ); + fx2 = x2 - SUBPIXELS( ex2 ); + + /* trivial case. Happens often */ + if ( y1 == y2 ) { + return set_cell( RAS_VAR_ ex2, ey ); + } + + /* everything is located in a single cell. That is easy! */ + /* */ + if ( ex1 == ex2 ) { + delta = y2 - y1; + ras.area += ( fx1 + fx2 ) * delta; + ras.cover += delta; + return 0; + } + + /* ok, we'll have to render a run of adjacent cells on the same */ + /* scanline... */ + /* */ + p = ( ONE_PIXEL - fx1 ) * ( y2 - y1 ); + first = ONE_PIXEL; + incr = 1; + + if ( dx < 0 ) { + p = fx1 * ( y2 - y1 ); + first = 0; + incr = -1; + dx = -dx; + } + + delta = p / dx; + mod = p % dx; + if ( mod < 0 ) { + delta--; + mod += dx; + } + + ras.area += ( fx1 + first ) * delta; + ras.cover += delta; + + ex1 += incr; + if ( set_cell( RAS_VAR_ ex1, ey ) ) { + goto Error; + } + y1 += delta; + + if ( ex1 != ex2 ) { + p = ONE_PIXEL * ( y2 - y1 ); + lift = p / dx; + rem = p % dx; + if ( rem < 0 ) { + lift--; + rem += dx; + } + + mod -= dx; + + while ( ex1 != ex2 ) + { + delta = lift; + mod += rem; + if ( mod >= 0 ) { + mod -= dx; + delta++; + } + + ras.area += ONE_PIXEL * delta; + ras.cover += delta; + y1 += delta; + ex1 += incr; + if ( set_cell( RAS_VAR_ ex1, ey ) ) { + goto Error; + } + } + } + + delta = y2 - y1; + ras.area += ( fx2 + ONE_PIXEL - first ) * delta; + ras.cover += delta; + + return 0; + +Error: + return 1; +} + + +/*************************************************************************/ +/* */ +/* Render a given line as a series of scanlines. */ +/* */ +static +int render_line( RAS_ARG_ TPos to_x, + TPos to_y ) { + TScan ey1, ey2, fy1, fy2; + TPos dx, dy, x, x2; + int p, rem, mod, lift, delta, first, incr; + + + ey1 = TRUNC( ras.last_ey ); + ey2 = TRUNC( to_y ); /* if (ey2 >= ras.max_ey) ey2 = ras.max_ey-1; */ + fy1 = ras.y - ras.last_ey; + fy2 = to_y - SUBPIXELS( ey2 ); + + dx = to_x - ras.x; + dy = to_y - ras.y; + + /* XXX: we should do something about the trivial case where dx == 0, */ + /* as it happens very often! */ + + /* perform vertical clipping */ + { + TScan min, max; + + + min = ey1; + max = ey2; + if ( ey1 > ey2 ) { + min = ey2; + max = ey1; + } + if ( min >= ras.max_ey || max < ras.min_ey ) { + goto End; + } + } + + /* everything is on a single scanline */ + if ( ey1 == ey2 ) { + if ( render_scanline( RAS_VAR_ ey1, ras.x, fy1, to_x, fy2 ) ) { + goto Error; + } + goto End; + } + + /* ok, we have to render several scanlines */ + p = ( ONE_PIXEL - fy1 ) * dx; + first = ONE_PIXEL; + incr = 1; + + if ( dy < 0 ) { + p = fy1 * dx; + first = 0; + incr = -1; + dy = -dy; + } + + delta = p / dy; + mod = p % dy; + if ( mod < 0 ) { + delta--; + mod += dy; + } + + x = ras.x + delta; + if ( render_scanline( RAS_VAR_ ey1, ras.x, fy1, x, first ) ) { + goto Error; + } + + ey1 += incr; + if ( set_cell( RAS_VAR_ TRUNC( x ), ey1 ) ) { + goto Error; + } + + if ( ey1 != ey2 ) { + p = ONE_PIXEL * dx; + lift = p / dy; + rem = p % dy; + if ( rem < 0 ) { + lift--; + rem += dy; + } + mod -= dy; + + while ( ey1 != ey2 ) + { + delta = lift; + mod += rem; + if ( mod >= 0 ) { + mod -= dy; + delta++; + } + + x2 = x + delta; + if ( render_scanline( RAS_VAR_ ey1, + x, ONE_PIXEL - first, x2, first ) ) { + goto Error; + } + x = x2; + ey1 += incr; + if ( set_cell( RAS_VAR_ TRUNC( x ), ey1 ) ) { + goto Error; + } + } + } + + if ( render_scanline( RAS_VAR_ ey1, + x, ONE_PIXEL - first, to_x, fy2 ) ) { + goto Error; + } + +End: + ras.x = to_x; + ras.y = to_y; + ras.last_ey = SUBPIXELS( ey2 ); + + return 0; + +Error: + return 1; +} + + +static +void split_conic( FT_Vector* base ) { + TPos a, b; + + + base[4].x = base[2].x; + b = base[1].x; + a = base[3].x = ( base[2].x + b ) / 2; + b = base[1].x = ( base[0].x + b ) / 2; + base[2].x = ( a + b ) / 2; + + base[4].y = base[2].y; + b = base[1].y; + a = base[3].y = ( base[2].y + b ) / 2; + b = base[1].y = ( base[0].y + b ) / 2; + base[2].y = ( a + b ) / 2; +} + + +static +int render_conic( RAS_ARG_ FT_Vector* control, + FT_Vector* to ) { + TPos dx, dy; + int top, level; + int* levels; + FT_Vector* arc; + + + dx = DOWNSCALE( ras.x ) + to->x - ( control->x << 1 ); + if ( dx < 0 ) { + dx = -dx; + } + dy = DOWNSCALE( ras.y ) + to->y - ( control->y << 1 ); + if ( dy < 0 ) { + dy = -dy; + } + if ( dx < dy ) { + dx = dy; + } + + level = 1; + dx = dx / ras.conic_level; + while ( dx > 0 ) + { + dx >>= 1; + level++; + } + + /* a shortcut to speed things up */ + if ( level <= 1 ) { + /* we compute the mid-point directly in order to avoid */ + /* calling split_conic() */ + TPos to_x, to_y, mid_x, mid_y; + + + to_x = UPSCALE( to->x ); + to_y = UPSCALE( to->y ); + mid_x = ( ras.x + to_x + 2 * UPSCALE( control->x ) ) / 4; + mid_y = ( ras.y + to_y + 2 * UPSCALE( control->y ) ) / 4; + + return render_line( RAS_VAR_ mid_x, mid_y ) || + render_line( RAS_VAR_ to_x, to_y ); + } + + arc = ras.bez_stack; + levels = ras.lev_stack; + top = 0; + levels[0] = level; + + arc[0].x = UPSCALE( to->x ); + arc[0].y = UPSCALE( to->y ); + arc[1].x = UPSCALE( control->x ); + arc[1].y = UPSCALE( control->y ); + arc[2].x = ras.x; + arc[2].y = ras.y; + + while ( top >= 0 ) + { + level = levels[top]; + if ( level > 1 ) { + /* check that the arc crosses the current band */ + TPos min, max, y; + + + min = max = arc[0].y; + + y = arc[1].y; + if ( y < min ) { + min = y; + } + if ( y > max ) { + max = y; + } + + y = arc[2].y; + if ( y < min ) { + min = y; + } + if ( y > max ) { + max = y; + } + + if ( TRUNC( min ) >= ras.max_ey || TRUNC( max ) < 0 ) { + goto Draw; + } + + split_conic( arc ); + arc += 2; + top++; + levels[top] = levels[top - 1] = level - 1; + continue; + } + +Draw: + { + TPos to_x, to_y, mid_x, mid_y; + + + to_x = arc[0].x; + to_y = arc[0].y; + mid_x = ( ras.x + to_x + 2 * arc[1].x ) / 4; + mid_y = ( ras.y + to_y + 2 * arc[1].y ) / 4; + + if ( render_line( RAS_VAR_ mid_x, mid_y ) || + render_line( RAS_VAR_ to_x, to_y ) ) { + return 1; + } + + top--; + arc -= 2; + } + } + return 0; +} + + +static +void split_cubic( FT_Vector* base ) { + TPos a, b, c, d; + + + base[6].x = base[3].x; + c = base[1].x; + d = base[2].x; + base[1].x = a = ( base[0].x + c ) / 2; + base[5].x = b = ( base[3].x + d ) / 2; + c = ( c + d ) / 2; + base[2].x = a = ( a + c ) / 2; + base[4].x = b = ( b + c ) / 2; + base[3].x = ( a + b ) / 2; + + base[6].y = base[3].y; + c = base[1].y; + d = base[2].y; + base[1].y = a = ( base[0].y + c ) / 2; + base[5].y = b = ( base[3].y + d ) / 2; + c = ( c + d ) / 2; + base[2].y = a = ( a + c ) / 2; + base[4].y = b = ( b + c ) / 2; + base[3].y = ( a + b ) / 2; +} + + +static +int render_cubic( RAS_ARG_ FT_Vector* control1, + FT_Vector* control2, + FT_Vector* to ) { + TPos dx, dy, da, db; + int top, level; + int* levels; + FT_Vector* arc; + + + dx = DOWNSCALE( ras.x ) + to->x - ( control1->x << 1 ); + if ( dx < 0 ) { + dx = -dx; + } + dy = DOWNSCALE( ras.y ) + to->y - ( control1->y << 1 ); + if ( dy < 0 ) { + dy = -dy; + } + if ( dx < dy ) { + dx = dy; + } + da = dx; + + dx = DOWNSCALE( ras.x ) + to->x - 3 * ( control1->x + control2->x ); + if ( dx < 0 ) { + dx = -dx; + } + dy = DOWNSCALE( ras.y ) + to->y - 3 * ( control1->x + control2->y ); + if ( dy < 0 ) { + dy = -dy; + } + if ( dx < dy ) { + dx = dy; + } + db = dx; + + level = 1; + da = da / ras.cubic_level; + db = db / ras.conic_level; + while ( da > 0 || db > 0 ) + { + da >>= 1; + db >>= 2; + level++; + } + + if ( level <= 1 ) { + TPos to_x, to_y, mid_x, mid_y; + + + to_x = UPSCALE( to->x ); + to_y = UPSCALE( to->y ); + mid_x = ( ras.x + to_x + + 3 * UPSCALE( control1->x + control2->x ) ) / 8; + mid_y = ( ras.y + to_y + + 3 * UPSCALE( control1->y + control2->y ) ) / 8; + + return render_line( RAS_VAR_ mid_x, mid_y ) || + render_line( RAS_VAR_ to_x, to_y ); + } + + arc = ras.bez_stack; + arc[0].x = UPSCALE( to->x ); + arc[0].y = UPSCALE( to->y ); + arc[1].x = UPSCALE( control2->x ); + arc[1].y = UPSCALE( control2->y ); + arc[2].x = UPSCALE( control1->x ); + arc[2].y = UPSCALE( control1->y ); + arc[3].x = ras.x; + arc[3].y = ras.y; + + levels = ras.lev_stack; + top = 0; + levels[0] = level; + + while ( top >= 0 ) + { + level = levels[top]; + if ( level > 1 ) { + /* check that the arc crosses the current band */ + TPos min, max, y; + + + min = max = arc[0].y; + y = arc[1].y; + if ( y < min ) { + min = y; + } + if ( y > max ) { + max = y; + } + y = arc[2].y; + if ( y < min ) { + min = y; + } + if ( y > max ) { + max = y; + } + y = arc[3].y; + if ( y < min ) { + min = y; + } + if ( y > max ) { + max = y; + } + if ( TRUNC( min ) >= ras.max_ey || TRUNC( max ) < 0 ) { + goto Draw; + } + split_cubic( arc ); + arc += 3; + top++; + levels[top] = levels[top - 1] = level - 1; + continue; + } + +Draw: + { + TPos to_x, to_y, mid_x, mid_y; + + + to_x = arc[0].x; + to_y = arc[0].y; + mid_x = ( ras.x + to_x + 3 * ( arc[1].x + arc[2].x ) ) / 8; + mid_y = ( ras.y + to_y + 3 * ( arc[1].y + arc[2].y ) ) / 8; + + if ( render_line( RAS_VAR_ mid_x, mid_y ) || + render_line( RAS_VAR_ to_x, to_y ) ) { + return 1; + } + top--; + arc -= 3; + } + } + return 0; +} + + +/* a macro comparing two cell pointers. Returns true if a <= b. */ +#if 1 + +#define PACK( a ) ( ( (long)( a )->y << 16 ) + ( a )->x ) +#define LESS_THAN( a, b ) ( PACK( a ) < PACK( b ) ) + +#else /* 1 */ + +#define LESS_THAN( a, b ) ( ( a )->y < ( b )->y || \ + ( ( a )->y == ( b )->y && ( a )->x < ( b )->x ) ) + +#endif /* 1 */ + +#define SWAP_CELLS( a, b, temp ) do \ + { \ + temp = *( a ); \ + *( a ) = *( b ); \ + *( b ) = temp; \ + } while ( 0 ) +#define DEBUG_SORT +#define QUICK_SORT + +#ifdef SHELL_SORT + +/* a simple shell sort algorithm that works directly on our */ +/* cells table */ +static +void shell_sort( PCell cells, + int count ) { + PCell i, j, limit = cells + count; + TCell temp; + int gap; + + + /* compute initial gap */ + for ( gap = 0; ++gap < count; gap *= 3 ) + ; + + while ( gap /= 3 ) + { + for ( i = cells + gap; i < limit; i++ ) + { + for ( j = i - gap; ; j -= gap ) + { + PCell k = j + gap; + + + if ( LESS_THAN( j, k ) ) { + break; + } + + SWAP_CELLS( j, k, temp ); + + if ( j < cells + gap ) { + break; + } + } + } + } +} + +#endif /* SHELL_SORT */ + + +#ifdef QUICK_SORT + +/* This is a non-recursive quicksort that directly process our cells */ +/* array. It should be faster than calling the stdlib qsort(), and we */ +/* can even tailor our insertion threshold... */ + +#define QSORT_THRESHOLD 9 /* below this size, a sub-array will be sorted */ + /* through a normal insertion sort */ + +static +void quick_sort( PCell cells, + int count ) { + PCell stack[40]; /* should be enough ;-) */ + PCell* top; /* top of stack */ + PCell base, limit; + TCell temp; + + + limit = cells + count; + base = cells; + top = stack; + + for (;; ) + { + int len = limit - base; + PCell i, j, pivot; + + + if ( len > QSORT_THRESHOLD ) { + /* we use base + len/2 as the pivot */ + pivot = base + len / 2; + SWAP_CELLS( base, pivot, temp ); + + i = base + 1; + j = limit - 1; + + /* now ensure that *i <= *base <= *j */ + if ( LESS_THAN( j, i ) ) { + SWAP_CELLS( i, j, temp ); + } + + if ( LESS_THAN( base, i ) ) { + SWAP_CELLS( base, i, temp ); + } + + if ( LESS_THAN( j, base ) ) { + SWAP_CELLS( base, j, temp ); + } + + for (;; ) + { + do i++;while ( LESS_THAN( i, base ) ); + do j--;while ( LESS_THAN( base, j ) ); + + if ( i > j ) { + break; + } + + SWAP_CELLS( i, j, temp ); + } + + SWAP_CELLS( base, j, temp ); + + /* now, push the largest sub-array */ + if ( j - base > limit - i ) { + top[0] = base; + top[1] = j; + base = i; + } else + { + top[0] = i; + top[1] = limit; + limit = j; + } + top += 2; + } else + { + /* the sub-array is small, perform insertion sort */ + j = base; + i = j + 1; + + for ( ; i < limit; j = i, i++ ) + { + for ( ; LESS_THAN( j + 1, j ); j-- ) + { + SWAP_CELLS( j + 1, j, temp ); + if ( j == base ) { + break; + } + } + } + if ( top > stack ) { + top -= 2; + base = top[0]; + limit = top[1]; + } else { + break; + } + } + } +} + +#endif /* QUICK_SORT */ + + +#ifdef DEBUG_GRAYS +#ifdef DEBUG_SORT + +static +int check_sort( PCell cells, + int count ) { + PCell p, q; + + + for ( p = cells + count - 2; p >= cells; p-- ) + { + q = p + 1; + if ( !LESS_THAN( p, q ) ) { + return 0; + } + } + return 1; +} + +#endif /* DEBUG_SORT */ +#endif /* DEBUG_GRAYS */ + + +static +int Move_To( FT_Vector* to, + FT_Raster raster ) { + TPos x, y; + + + /* record current cell, if any */ + record_cell( (PRaster)raster ); + + /* start to a new position */ + x = UPSCALE( to->x ); + y = UPSCALE( to->y ); + start_cell( (PRaster)raster, TRUNC( x ), TRUNC( y ) ); + ( (PRaster)raster )->x = x; + ( (PRaster)raster )->y = y; + return 0; +} + + +static +int Line_To( FT_Vector* to, + FT_Raster raster ) { + return render_line( (PRaster)raster, + UPSCALE( to->x ), UPSCALE( to->y ) ); +} + + +static +int Conic_To( FT_Vector* control, + FT_Vector* to, + FT_Raster raster ) { + return render_conic( (PRaster)raster, control, to ); +} + + +static +int Cubic_To( FT_Vector* control1, + FT_Vector* control2, + FT_Vector* to, + FT_Raster raster ) { + return render_cubic( (PRaster)raster, control1, control2, to ); +} + + +static +void grays_render_span( int y, + int count, + FT_Span* spans, + PRaster raster ) { + unsigned char* p; + FT_Bitmap* map = &raster->target; + + + /* first of all, compute the scanline offset */ + p = (unsigned char*)map->buffer - y * map->pitch; + if ( map->pitch >= 0 ) { + p += ( map->rows - 1 ) * map->pitch; + } + + for ( ; count > 0; count--, spans++ ) + { + if ( spans->coverage ) +#if 1 + { memset( p + spans->x, (unsigned char)spans->coverage, spans->len );} +#else /* 1 */ + { + q = p + spans->x; + limit = q + spans->len; + for ( ; q < limit; q++ ) + q[0] = (unsigned char)spans->coverage; + } +#endif /* 1 */ + } +} + + +#ifdef DEBUG_GRAYS + +#include + +static +void dump_cells( RAS_ARG ) { + PCell cell, limit; + int y = -1; + + + cell = ras.cells; + limit = cell + ras.num_cells; + + for ( ; cell < limit; cell++ ) + { + if ( cell->y != y ) { + fprintf( stderr, "\n%2d: ", cell->y ); + y = cell->y; + } + fprintf( stderr, "[%d %d %d]", + cell->x, cell->area, cell->cover ); + } + fprintf( stderr, "\n" ); +} + +#endif /* DEBUG_GRAYS */ + + +static +void grays_hline( RAS_ARG_ TScan x, + TScan y, + TPos area, + int acount ) { + FT_Span* span; + int count; + int coverage; + + + /* compute the coverage line's coverage, depending on the */ + /* outline fill rule */ + /* */ + /* the coverage percentage is area/(PIXEL_BITS*PIXEL_BITS*2) */ + /* */ + coverage = area >> ( PIXEL_BITS * 2 + 1 - 8 ); /* use range 0..256 */ + + if ( ras.outline.flags & ft_outline_even_odd_fill ) { + if ( coverage < 0 ) { + coverage = -coverage; + } + + while ( coverage >= 512 ) + coverage -= 512; + + if ( coverage > 256 ) { + coverage = 512 - coverage; + } else if ( coverage == 256 ) { + coverage = 255; + } + } else + { + /* normal non-zero winding rule */ + if ( coverage < 0 ) { + coverage = -coverage; + } + + if ( coverage >= 256 ) { + coverage = 255; + } + } + + y += ras.min_ey; + x += ras.min_ex; + + if ( coverage ) { + /* see if we can add this span to the current list */ + count = ras.num_gray_spans; + span = ras.gray_spans + count - 1; + if ( count > 0 && + ras.span_y == y && + (int)span->x + span->len == (int)x && + span->coverage == coverage ) { + span->len += acount; + return; + } + + if ( ras.span_y != y || count >= FT_MAX_GRAY_SPANS ) { + if ( ras.render_span ) { + ras.render_span( ras.span_y, count, ras.gray_spans, + ras.render_span_data ); + } + /* ras.render_span( span->y, ras.gray_spans, count ); */ + +#ifdef DEBUG_GRAYS + + if ( ras.span_y >= 0 ) { + int n; + + + fprintf( stderr, "y=%3d ", ras.span_y ); + span = ras.gray_spans; + for ( n = 0; n < count; n++, span++ ) + fprintf( stderr, "[%d..%d]:%02x ", + span->x, span->x + span->len - 1, span->coverage ); + fprintf( stderr, "\n" ); + } + +#endif /* DEBUG_GRAYS */ + + ras.num_gray_spans = 0; + ras.span_y = y; + + count = 0; + span = ras.gray_spans; + } else { + span++; + } + + /* add a gray span to the current list */ + span->x = (short)x; + span->len = (unsigned short)acount; + span->coverage = (unsigned char)coverage; + ras.num_gray_spans++; + } +} + + +static +void grays_sweep( RAS_ARG_ FT_Bitmap* target ) { + TScan x, y, cover, area; + PCell start, cur, limit; + + FT_UNUSED( target ); + + + cur = ras.cells; + limit = cur + ras.num_cells; + + cover = 0; + ras.span_y = -1; + ras.num_gray_spans = 0; + + for (;; ) + { + start = cur; + y = start->y; + x = start->x; + + area = start->area; + cover += start->cover; + + /* accumulate all start cells */ + for (;; ) + { + ++cur; + if ( cur >= limit || cur->y != start->y || cur->x != start->x ) { + break; + } + + area += cur->area; + cover += cur->cover; + } + + /* if the start cell has a non-null area, we must draw an */ + /* individual gray pixel there */ + if ( area && x >= 0 ) { + grays_hline( RAS_VAR_ x, y, cover * ( ONE_PIXEL * 2 ) - area, 1 ); + x++; + } + + if ( x < 0 ) { + x = 0; + } + + if ( cur < limit && start->y == cur->y ) { + /* draw a gray span between the start cell and the current one */ + if ( cur->x > x ) { + grays_hline( RAS_VAR_ x, y, + cover * ( ONE_PIXEL * 2 ), cur->x - x ); + } + } else + { + /* draw a gray span until the end of the clipping region */ + if ( cover && x < ras.max_ex - ras.min_ex ) { + grays_hline( RAS_VAR_ x, y, + cover * ( ONE_PIXEL * 2 ), + ras.max_ex - x - ras.min_ex ); + } + cover = 0; + } + + if ( cur >= limit ) { + break; + } + } + + if ( ras.render_span && ras.num_gray_spans > 0 ) { + ras.render_span( ras.span_y, ras.num_gray_spans, + ras.gray_spans, ras.render_span_data ); + } + +#ifdef DEBUG_GRAYS + + { + int n; + FT_Span* span; + + + fprintf( stderr, "y=%3d ", ras.span_y ); + span = ras.gray_spans; + for ( n = 0; n < ras.num_gray_spans; n++, span++ ) + fprintf( stderr, "[%d..%d]:%02x ", + span->x, span->x + span->len - 1, span->coverage ); + fprintf( stderr, "\n" ); + } + +#endif /* DEBUG_GRAYS */ + +} + + +#ifdef _STANDALONE_ + +/*************************************************************************/ +/* */ +/* The following function should only compile in stand_alone mode, */ +/* i.e., when building this component without the rest of FreeType. */ +/* */ +/*************************************************************************/ + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_Decompose */ +/* */ +/* */ +/* Walks over an outline's structure to decompose it into individual */ +/* segments and Bezier arcs. This function is also able to emit */ +/* `move to' and `close to' operations to indicate the start and end */ +/* of new contours in the outline. */ +/* */ +/* */ +/* outline :: A pointer to the source target. */ +/* */ +/* interface :: A table of `emitters', i.e,. function pointers called */ +/* during decomposition to indicate path operations. */ +/* */ +/* user :: A typeless pointer which is passed to each emitter */ +/* during the decomposition. It can be used to store */ +/* the state during the decomposition. */ +/* */ +/* */ +/* Error code. 0 means sucess. */ +/* */ +static +int FT_Outline_Decompose( FT_Outline* outline, + FT_Outline_Funcs* interface, + void* user ) { +#undef SCALED +#define SCALED( x ) ( ( ( x ) << shift ) - delta ) + + FT_Vector v_last; + FT_Vector v_control; + FT_Vector v_start; + + FT_Vector* point; + FT_Vector* limit; + char* tags; + + int n; /* index of contour in outline */ + int first; /* index of first point in contour */ + int error; + char tag; /* current point's state */ + + int shift = interface->shift; + FT_Pos delta = interface->delta; + + + first = 0; + + for ( n = 0; n < outline->n_contours; n++ ) + { + int last; /* index of last point in contour */ + + + last = outline->contours[n]; + limit = outline->points + last; + + v_start = outline->points[first]; + v_last = outline->points[last]; + + v_start.x = SCALED( v_start.x ); v_start.y = SCALED( v_start.y ); + v_last.x = SCALED( v_last.x ); v_last.y = SCALED( v_last.y ); + + v_control = v_start; + + point = outline->points + first; + tags = outline->tags + first; + tag = FT_CURVE_TAG( tags[0] ); + + /* A contour cannot start with a cubic control point! */ + if ( tag == FT_Curve_Tag_Cubic ) { + goto Invalid_Outline; + } + + /* check first point to determine origin */ + if ( tag == FT_Curve_Tag_Conic ) { + /* first point is conic control. Yes, this happens. */ + if ( FT_CURVE_TAG( outline->tags[last] ) == FT_Curve_Tag_On ) { + /* start at last point if it is on the curve */ + v_start = v_last; + limit--; + } else + { + /* if both first and last points are conic, */ + /* start at their middle and record its position */ + /* for closure */ + v_start.x = ( v_start.x + v_last.x ) / 2; + v_start.y = ( v_start.y + v_last.y ) / 2; + + v_last = v_start; + } + point--; + tags--; + } + + error = interface->move_to( &v_start, user ); + if ( error ) { + goto Exit; + } + + while ( point < limit ) + { + point++; + tags++; + + tag = FT_CURVE_TAG( tags[0] ); + switch ( tag ) + { + case FT_Curve_Tag_On: /* emit a single line_to */ + { + FT_Vector vec; + + + vec.x = SCALED( point->x ); + vec.y = SCALED( point->y ); + + error = interface->line_to( &vec, user ); + if ( error ) { + goto Exit; + } + continue; + } + + case FT_Curve_Tag_Conic: /* consume conic arcs */ + { + v_control.x = SCALED( point->x ); + v_control.y = SCALED( point->y ); + +Do_Conic: + if ( point < limit ) { + FT_Vector vec; + FT_Vector v_middle; + + + point++; + tags++; + tag = FT_CURVE_TAG( tags[0] ); + + vec.x = SCALED( point->x ); + vec.y = SCALED( point->y ); + + if ( tag == FT_Curve_Tag_On ) { + error = interface->conic_to( &v_control, &vec, user ); + if ( error ) { + goto Exit; + } + continue; + } + + if ( tag != FT_Curve_Tag_Conic ) { + goto Invalid_Outline; + } + + v_middle.x = ( v_control.x + vec.x ) / 2; + v_middle.y = ( v_control.y + vec.y ) / 2; + + error = interface->conic_to( &v_control, &v_middle, user ); + if ( error ) { + goto Exit; + } + + v_control = vec; + goto Do_Conic; + } + + error = interface->conic_to( &v_control, &v_start, user ); + goto Close; + } + + default: /* FT_Curve_Tag_Cubic */ + { + FT_Vector vec1, vec2; + + + if ( point + 1 > limit || + FT_CURVE_TAG( tags[1] ) != FT_Curve_Tag_Cubic ) { + goto Invalid_Outline; + } + + point += 2; + tags += 2; + + vec1.x = SCALED( point[-2].x ); vec1.y = SCALED( point[-2].y ); + vec2.x = SCALED( point[-1].x ); vec2.y = SCALED( point[-1].y ); + + if ( point <= limit ) { + FT_Vector vec; + + + vec.x = SCALED( point->x ); + vec.y = SCALED( point->y ); + + error = interface->cubic_to( &vec1, &vec2, &vec, user ); + if ( error ) { + goto Exit; + } + continue; + } + + error = interface->cubic_to( &vec1, &vec2, &v_start, user ); + goto Close; + } + } + } + + /* close the contour with a line segment */ + error = interface->line_to( &v_start, user ); + +Close: + if ( error ) { + goto Exit; + } + + first = last + 1; + } + + return 0; + +Exit: + return error; + +Invalid_Outline: + return ErrRaster_Invalid_Outline; +} + +#endif /* _STANDALONE_ */ + + +typedef struct TBand_ +{ + FT_Pos min, max; + +} TBand; + + +static +int grays_convert_glyph( RAS_ARG_ FT_Outline* outline ) { + static + FT_Outline_Funcs interface = + { + (FT_Outline_MoveTo_Func) Move_To, + (FT_Outline_LineTo_Func) Line_To, + (FT_Outline_ConicTo_Func)Conic_To, + (FT_Outline_CubicTo_Func)Cubic_To, + 0, + 0 + }; + + TBand bands[40], *band; + int n, num_bands; + TPos min, max, max_y; + + + /* Set up state in the raster object */ + compute_cbox( RAS_VAR_ outline ); + + /* clip to target bitmap, exit if nothing to do */ + if ( ras.max_ex <= 0 || ras.min_ex >= ras.target.width || + ras.max_ey <= 0 || ras.min_ey >= ras.target.rows ) { + return 0; + } + + if ( ras.min_ex < 0 ) { + ras.min_ex = 0; + } + if ( ras.min_ey < 0 ) { + ras.min_ey = 0; + } + + if ( ras.max_ex > ras.target.width ) { + ras.max_ex = ras.target.width; + } + if ( ras.max_ey > ras.target.rows ) { + ras.max_ey = ras.target.rows; + } + + /* simple heuristic used to speed-up the bezier decomposition -- see */ + /* the code in render_conic() and render_cubic() for more details */ + ras.conic_level = 32; + ras.cubic_level = 16; + + { + int level = 0; + + + if ( ras.max_ex > 24 || ras.max_ey > 24 ) { + level++; + } + if ( ras.max_ex > 120 || ras.max_ey > 120 ) { + level += 2; + } + + ras.conic_level <<= level; + ras.cubic_level <<= level; + } + + /* setup vertical bands */ + num_bands = ( ras.max_ey - ras.min_ey ) / ras.band_size; + if ( num_bands == 0 ) { + num_bands = 1; + } + if ( num_bands >= 39 ) { + num_bands = 39; + } + + ras.band_shoot = 0; + + min = ras.min_ey; + max_y = ras.max_ey; + + for ( n = 0; n < num_bands; n++, min = max ) + { + max = min + ras.band_size; + if ( n == num_bands - 1 || max > max_y ) { + max = max_y; + } + + bands[0].min = min; + bands[0].max = max; + band = bands; + + while ( band >= bands ) + { + FT_Pos bottom, top, middle; + int error; + + + ras.num_cells = 0; + ras.invalid = 1; + ras.min_ey = band->min; + ras.max_ey = band->max; + + error = FT_Outline_Decompose( outline, &interface, &ras ) || + record_cell( RAS_VAR ); + + if ( !error ) { +#ifdef SHELL_SORT + shell_sort( ras.cells, ras.num_cells ); +#else + quick_sort( ras.cells, ras.num_cells ); +#endif + +#ifdef DEBUG_GRAYS + check_sort( ras.cells, ras.num_cells ); + dump_cells( RAS_VAR ); +#endif + + grays_sweep( RAS_VAR_ & ras.target ); + band--; + continue; + } + + /* render pool overflow, we will reduce the render band by half */ + bottom = band->min; + top = band->max; + middle = bottom + ( ( top - bottom ) >> 1 ); + + /* waoow! This is too complex for a single scanline, something */ + /* must be really rotten here! */ + if ( middle == bottom ) { +#ifdef DEBUG_GRAYS + fprintf( stderr, "Rotten glyph!\n" ); +#endif + return 1; + } + + if ( bottom - top >= ras.band_size ) { + ras.band_shoot++; + } + + band[1].min = bottom; + band[1].max = middle; + band[0].min = middle; + band[0].max = top; + band++; + } + } + + if ( ras.band_shoot > 8 && ras.band_size > 16 ) { + ras.band_size = ras.band_size / 2; + } + + return 0; +} + + +extern +int grays_raster_render( PRaster raster, + FT_Raster_Params* params ) { + FT_Outline* outline = (FT_Outline*)params->source; + FT_Bitmap* target_map = params->target; + + + if ( !raster || !raster->cells || !raster->max_cells ) { + return -1; + } + + /* return immediately if the outline is empty */ + if ( outline->n_points == 0 || outline->n_contours <= 0 ) { + return 0; + } + + if ( !outline || !outline->contours || !outline->points ) { + return ErrRaster_Invalid_Outline; + } + + if ( outline->n_points != + outline->contours[outline->n_contours - 1] + 1 ) { + return ErrRaster_Invalid_Outline; + } + + if ( !target_map || !target_map->buffer ) { + return -1; + } + + /* XXX: this version does not support monochrome rendering yet! */ + if ( !( params->flags & ft_raster_flag_aa ) ) { + return ErrRaster_Invalid_Mode; + } + + ras.outline = *outline; + ras.target = *target_map; + ras.num_cells = 0; + ras.invalid = 1; + + ras.render_span = (FT_Raster_Span_Func)grays_render_span; + ras.render_span_data = &ras; + + if ( params->flags & ft_raster_flag_direct ) { + ras.render_span = (FT_Raster_Span_Func)params->gray_spans; + ras.render_span_data = params->user; + } + + return grays_convert_glyph( (PRaster)raster, outline ); +} + + +/**** RASTER OBJECT CREATION: In standalone mode, we simply use *****/ +/**** a static object. *****/ + +#ifdef _STANDALONE_ + +static +int grays_raster_new( void* memory, + FT_Raster* araster ) { + static TRaster the_raster; + + FT_UNUSED( memory ); + + + *araster = ( FT_Raster ) & the_raster; + memset( &the_raster, 0, sizeof( the_raster ) ); + + return 0; +} + + +static +void grays_raster_done( FT_Raster raster ) { + /* nothing */ + FT_UNUSED( raster ); +} + +#else /* _STANDALONE_ */ + +static +int grays_raster_new( FT_Memory memory, + FT_Raster* araster ) { + FT_Error error; + PRaster raster; + + + *araster = 0; + if ( !ALLOC( raster, sizeof( TRaster ) ) ) { + raster->memory = memory; + *araster = (FT_Raster)raster; + } + + return error; +} + + +static +void grays_raster_done( FT_Raster raster ) { + FT_Memory memory = (FT_Memory)( (PRaster)raster )->memory; + + + FREE( raster ); +} + +#endif /* _STANDALONE_ */ + + +static +void grays_raster_reset( FT_Raster raster, + const char* pool_base, + long pool_size ) { + PRaster rast = (PRaster)raster; + + + if ( raster && pool_base && pool_size >= 4096 ) { + init_cells( rast, (char*)pool_base, pool_size ); + } + + rast->band_size = ( pool_size / sizeof( TCell ) ) / 8; +} + + +FT_Raster_Funcs ft_grays_raster = +{ + ft_glyph_format_outline, + + (FT_Raster_New_Func) grays_raster_new, + (FT_Raster_Reset_Func) grays_raster_reset, + (FT_Raster_Set_Mode_Func)0, + (FT_Raster_Render_Func) grays_raster_render, + (FT_Raster_Done_Func) grays_raster_done +}; + + +/* END */ diff --git a/src/ft2/ftgrays.h b/src/ft2/ftgrays.h new file mode 100644 index 0000000..3990d5e --- /dev/null +++ b/src/ft2/ftgrays.h @@ -0,0 +1,48 @@ +/***************************************************************************/ +/* */ +/* ftgrays.h */ +/* */ +/* FreeType smooth renderer declaration */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + +#ifndef FTGRAYS_H +#define FTGRAYS_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ftimage.h" + +/*************************************************************************/ +/* */ +/* To make ftgrays.h independent from configuration files we check */ +/* whether FT_EXPORT_DEF has been defined already. */ +/* */ +/* On some systems and compilers (Win32 mostly), an extra keyword is */ +/* necessary to compile the library as a DLL. */ +/* */ +#ifndef FT_EXPORT_VAR +#define FT_EXPORT_VAR( x ) extern x +#endif + +FT_EXPORT_VAR( FT_Raster_Funcs ) ft_grays_raster; + +#ifdef __cplusplus +} +#endif + +#endif /* FTGRAYS_H */ + + +/* END */ diff --git a/src/ft2/ftimage.h b/src/ft2/ftimage.h new file mode 100644 index 0000000..e0acb64 --- /dev/null +++ b/src/ft2/ftimage.h @@ -0,0 +1,1003 @@ +/***************************************************************************/ +/* */ +/* ftimage.h */ +/* */ +/* FreeType glyph image formats and default raster interface */ +/* (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + +/*************************************************************************/ +/* */ +/* Note: A `raster' is simply a scan-line converter, used to render */ +/* FT_Outlines into FT_Bitmaps. */ +/* */ +/*************************************************************************/ + + +#ifndef FTIMAGE_H +#define FTIMAGE_H + + +#ifdef __cplusplus +extern "C" { +#endif + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Pos */ +/* */ +/* */ +/* The type FT_Pos is a 32-bit integer used to store vectorial */ +/* coordinates. Depending on the context, these can represent */ +/* distances in integer font units, or 26.6 fixed float pixel */ +/* coordinates. */ +/* */ +typedef signed long FT_Pos; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Vector */ +/* */ +/* */ +/* A simple structure used to store a 2D vector; coordinates are of */ +/* the FT_Pos type. */ +/* */ +/* */ +/* x :: The horizontal coordinate. */ +/* y :: The vertical coordinate. */ +/* */ +typedef struct FT_Vector_ +{ + FT_Pos x; + FT_Pos y; + +} FT_Vector; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Pixel_Mode */ +/* */ +/* */ +/* An enumeration type used to describe the format of pixels in a */ +/* given bitmap. Note that additional formats may be added in the */ +/* future. */ +/* */ +/* */ +/* ft_pixel_mode_mono :: A monochrome bitmap (1 bit/pixel). */ +/* */ +/* ft_pixel_mode_grays :: An 8-bit gray-levels bitmap. Note that the */ +/* total number of gray levels is given in the */ +/* `num_grays' field of the FT_Bitmap */ +/* structure. */ +/* */ +/* ft_pixel_mode_pal2 :: A 2-bit paletted bitmap. */ +/* Currently unused by FreeType. */ +/* */ +/* ft_pixel_mode_pal4 :: A 4-bit paletted bitmap. */ +/* Currently unused by FreeType. */ +/* */ +/* ft_pixel_mode_pal8 :: An 8-bit paletted bitmap. */ +/* Currently unused by FreeType. */ +/* */ +/* ft_pixel_mode_rgb15 :: A 15-bit RGB bitmap. Uses 5:5:5 encoding. */ +/* Currently unused by FreeType. */ +/* */ +/* ft_pixel_mode_rgb16 :: A 16-bit RGB bitmap. Uses 5:6:5 encoding. */ +/* Currently unused by FreeType. */ +/* */ +/* ft_pixel_mode_rgb24 :: A 24-bit RGB bitmap. */ +/* Currently unused by FreeType. */ +/* */ +/* ft_pixel_mode_rgb32 :: A 32-bit RGB bitmap. */ +/* Currently unused by FreeType. */ +/* */ +/* */ +/* Some anti-aliased bitmaps might be embedded in TrueType fonts */ +/* using formats pal2 or pal4, though no fonts presenting those have */ +/* been found to date. */ +/* */ +typedef enum FT_Pixel_Mode_ +{ + ft_pixel_mode_none = 0, + ft_pixel_mode_mono, + ft_pixel_mode_grays, + ft_pixel_mode_pal2, + ft_pixel_mode_pal4, + ft_pixel_mode_pal8, + ft_pixel_mode_rgb15, + ft_pixel_mode_rgb16, + ft_pixel_mode_rgb24, + ft_pixel_mode_rgb32, + + ft_pixel_mode_max /* do not remove */ + +} FT_Pixel_Mode; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Palette_Mode */ +/* */ +/* */ +/* An enumeration type used to describe the format of a bitmap */ +/* palette, used with ft_pixel_mode_pal4 and ft_pixel_mode_pal8. */ +/* */ +/* */ +/* ft_palette_mode_rgb :: The palette is an array of 3-bytes RGB */ +/* records. */ +/* */ +/* ft_palette_mode_rgba :: The palette is an array of 4-bytes RGBA */ +/* records. */ +/* */ +/* */ +/* As ft_pixel_mode_pal2, pal4 and pal8 are currently unused by */ +/* FreeType, these types are not handled by the library itself. */ +/* */ +typedef enum FT_Palette_Mode_ +{ + ft_palette_mode_rgb = 0, + ft_palette_mode_rgba, + + ft_palettte_mode_max /* do not remove */ + +} FT_Palette_Mode; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Bitmap */ +/* */ +/* */ +/* A structure used to describe a bitmap or pixmap to the raster. */ +/* Note that we now manage pixmaps of various depths through the */ +/* `pixel_mode' field. */ +/* */ +/* */ +/* rows :: The number of bitmap rows. */ +/* */ +/* width :: The number of pixels in bitmap row. */ +/* */ +/* pitch :: The pitch's absolute value is the number of bytes */ +/* taken by one bitmap row, including padding. */ +/* However, the pitch is positive when the bitmap has */ +/* a `down' flow, and negative when it has an `up' */ +/* flow. In all cases, the pitch is an offset to add */ +/* to a bitmap pointer in order to go down one row. */ +/* */ +/* buffer :: A typeless pointer to the bitmap buffer. This */ +/* value should be aligned on 32-bit boundaries in */ +/* most cases. */ +/* */ +/* num_grays :: This field is only used with */ +/* `ft_pixel_mode_grays'; it gives the number of gray */ +/* levels used in the bitmap. */ +/* */ +/* pixel_mode :: The pixel_mode, i.e., how pixel bits are stored. */ +/* */ +/* palette_mode :: This field is only used with paletted pixel modes; */ +/* it indicates how the palette is stored. */ +/* */ +/* palette :: A typeless pointer to the bitmap palette; only */ +/* used for paletted pixel modes. */ +/* */ +/* */ +/* For now, the only pixel mode supported by FreeType are mono and */ +/* grays. However, drivers might be added in the future to support */ +/* more `colorful' options. */ +/* */ +/* When using pixel modes pal2, pal4 and pal8 with a void `palette' */ +/* field, a gray pixmap with respectively 4, 16, and 256 levels of */ +/* gray is assumed. This, in order to be compatible with some */ +/* embedded bitmap formats defined in the TrueType specification. */ +/* */ +/* Note that no font was found presenting such embedded bitmaps, so */ +/* this is currently completely unhandled by the library. */ +/* */ +typedef struct FT_Bitmap_ +{ + int rows; + int width; + int pitch; + unsigned char* buffer; + short num_grays; + char pixel_mode; + char palette_mode; + void* palette; + +} FT_Bitmap; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline */ +/* */ +/* */ +/* This structure is used to describe an outline to the scan-line */ +/* converter. */ +/* */ +/* */ +/* n_contours :: The number of contours in the outline. */ +/* */ +/* n_points :: The number of points in the outline. */ +/* */ +/* points :: A pointer to an array of `n_points' FT_Vector */ +/* elements, giving the outline's point coordinates. */ +/* */ +/* tags :: A pointer to an array of `n_points' chars, giving */ +/* giving each outline point's type. If bit 0 is */ +/* unset, the point is 'off' the curve, i.e. a Bezier */ +/* control point, while it is `on' when unset. */ +/* */ +/* Bit 1 is meaningful for `off' points only. If set, */ +/* it indicates a third-order Bezier arc control point; */ +/* and a second-order control point if unset. */ +/* */ +/* contours :: An array of `n_contours' shorts, giving the end */ +/* point of each contour within the outline. For */ +/* example, the first contour is defined by the points */ +/* `0' to `contours[0]', the second one is defined by */ +/* the points `contours[0]+1' to `contours[1]', etc. */ +/* */ +/* flags :: A set of bit flags used to characterize the outline */ +/* and give hints to the scan-converter and hinter on */ +/* how to convert/grid-fit it. See FT_Outline_Flags. */ +/* */ +typedef struct FT_Outline_ +{ + short n_contours; /* number of contours in glyph */ + short n_points; /* number of points in the glyph */ + + FT_Vector* points; /* the outline's points */ + char* tags; /* the points flags */ + short* contours; /* the contour end points */ + + int flags; /* outline masks */ + +} FT_Outline; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_Flags */ +/* */ +/* */ +/* A simple type used to enumerates the flags in an outline's */ +/* `outline_flags' field. */ +/* */ +/* */ +/* ft_outline_owner :: If set, this flag indicates that the */ +/* outline's field arrays (i.e. */ +/* `points', `flags' & `contours') are */ +/* `owned' by the outline object, and */ +/* should thus be freed when it is */ +/* destroyed. */ +/* */ +/* ft_outline_even_odd_fill :: By default, outlines are filled using */ +/* the non-zero winding rule. If set to */ +/* 1, the outline will be filled using */ +/* the even-odd fill rule (only works */ +/* with the smooth raster). */ +/* */ +/* ft_outline_reverse_fill :: By default, outside contours of an */ +/* outline are oriented in clock-wise */ +/* direction, as defined in the TrueType */ +/* specification. This flag is set if */ +/* the outline uses the opposite */ +/* direction (typically for Type 1 */ +/* fonts). This flag is ignored by the */ +/* scan-converter. However, it is very */ +/* important for the auto-hinter. */ +/* */ +/* ft_outline_ignore_dropouts :: By default, the scan converter will */ +/* try to detect drop-outs in an outline */ +/* and correct the glyph bitmap to */ +/* ensure consistent shape continuity. */ +/* If set, this flag hints the scan-line */ +/* converter to ignore such cases. */ +/* */ +/* ft_outline_high_precision :: This flag indicates that the */ +/* scan-line converter should try to */ +/* convert this outline to bitmaps with */ +/* the highest possible quality. It is */ +/* typically set for small character */ +/* sizes. Note that this is only a */ +/* hint, that might be completely */ +/* ignored by a given scan-converter. */ +/* */ +/* ft_outline_single_pass :: This flag is set to force a given */ +/* scan-converter to only use a single */ +/* pass over the outline to render a */ +/* bitmap glyph image. Normally, it is */ +/* set for very large character sizes. */ +/* It is only a hint, that might be */ +/* completely ignored by a given */ +/* scan-converter. */ +/* */ +typedef enum FT_Outline_Flags_ +{ + ft_outline_none = 0, + ft_outline_owner = 1, + ft_outline_even_odd_fill = 2, + ft_outline_reverse_fill = 4, + ft_outline_ignore_dropouts = 8, + ft_outline_high_precision = 256, + ft_outline_single_pass = 512 + +} FT_Outline_Flags; + + +#define FT_CURVE_TAG( flag ) ( flag & 3 ) + +#define FT_Curve_Tag_On 1 +#define FT_Curve_Tag_Conic 0 +#define FT_Curve_Tag_Cubic 2 + +#define FT_Curve_Tag_Touch_X 8 /* reserved for the TrueType hinter */ +#define FT_Curve_Tag_Touch_Y 16 /* reserved for the TrueType hinter */ + +#define FT_Curve_Tag_Touch_Both ( FT_Curve_Tag_Touch_X | \ + FT_Curve_Tag_Touch_Y ) + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_MoveTo_Func */ +/* */ +/* */ +/* A function pointer type used to describe the signature of a `move */ +/* to' function during outline walking/decomposition. */ +/* */ +/* A `move to' is emitted to start a new contour in an outline. */ +/* */ +/* */ +/* to :: A pointer to the target point of the `move to'. */ +/* */ +/* user :: A typeless pointer which is passed from the caller of the */ +/* decomposition function. */ +/* */ +/* */ +/* Error code. 0 means success. */ +/* */ +typedef int ( *FT_Outline_MoveTo_Func )( FT_Vector* to, + void* user ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_LineTo_Func */ +/* */ +/* */ +/* A function pointer type used to describe the signature of a `line */ +/* to' function during outline walking/decomposition. */ +/* */ +/* A `line to' is emitted to indicate a segment in the outline. */ +/* */ +/* */ +/* to :: A pointer to the target point of the `line to'. */ +/* */ +/* user :: A typeless pointer which is passed from the caller of the */ +/* decomposition function. */ +/* */ +/* */ +/* Error code. 0 means success. */ +/* */ +typedef int ( *FT_Outline_LineTo_Func )( FT_Vector* to, + void* user ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_ConicTo_Func */ +/* */ +/* */ +/* A function pointer type use to describe the signature of a `conic */ +/* to' function during outline walking/decomposition. */ +/* */ +/* A `conic to' is emitted to indicate a second-order Bezier arc in */ +/* the outline. */ +/* */ +/* */ +/* control :: An intermediate control point between the last position */ +/* and the new target in `to'. */ +/* */ +/* to :: A pointer to the target end point of the conic arc. */ +/* */ +/* user :: A typeless pointer which is passed from the caller of */ +/* the decomposition function. */ +/* */ +/* */ +/* Error code. 0 means success. */ +/* */ +typedef int ( *FT_Outline_ConicTo_Func )( FT_Vector* control, + FT_Vector* to, + void* user ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_CubicTo_Func */ +/* */ +/* */ +/* A function pointer type used to describe the signature of a `cubic */ +/* to' function during outline walking/decomposition. */ +/* */ +/* A `cubic to' is emitted to indicate a third-order Bezier arc. */ +/* */ +/* */ +/* control1 :: A pointer to the first Bezier control point. */ +/* */ +/* control2 :: A pointer to the second Bezier control point. */ +/* */ +/* to :: A pointer to the target end point. */ +/* */ +/* user :: A typeless pointer which is passed from the caller of */ +/* the decomposition function. */ +/* */ +/* */ +/* Error code. 0 means success. */ +/* */ +typedef int ( *FT_Outline_CubicTo_Func )( FT_Vector* control1, + FT_Vector* control2, + FT_Vector* to, + void* user ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_Funcs */ +/* */ +/* */ +/* A structure to hold various function pointers used during outline */ +/* decomposition in order to emit segments, conic, and cubic Beziers, */ +/* as well as `move to' and `close to' operations. */ +/* */ +/* */ +/* move_to :: The `move to' emitter. */ +/* */ +/* line_to :: The segment emitter. */ +/* */ +/* conic_to :: The second-order Bezier arc emitter. */ +/* */ +/* cubic_to :: The third-order Bezier arc emitter. */ +/* */ +/* shift :: The shift that is applied to coordinates before they */ +/* are sent to the emitter. */ +/* */ +/* delta :: The delta that is applied to coordinates before they */ +/* are sent to the emitter, but after the shift. */ +/* */ +/* */ +/* The point coordinates sent to the emitters are the transformed */ +/* version of the original coordinates (this is important for high */ +/* accuracy during scan-conversion). The transformation is simple: */ +/* */ +/* x' = (x << shift) - delta */ +/* y' = (x << shift) - delta */ +/* */ +/* Set the value of `shift' and `delta' to 0 to get the original */ +/* point coordinates. */ +/* */ +typedef struct FT_Outline_Funcs_ +{ + FT_Outline_MoveTo_Func move_to; + FT_Outline_LineTo_Func line_to; + FT_Outline_ConicTo_Func conic_to; + FT_Outline_CubicTo_Func cubic_to; + + int shift; + FT_Pos delta; + +} FT_Outline_Funcs; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_IMAGE_TAG */ +/* */ +/* */ +/* This macro converts four letter tags into an unsigned long. */ +/* */ +#define FT_IMAGE_TAG( _x1, _x2, _x3, _x4 ) \ + ( ( (unsigned long)_x1 << 24 ) | \ + ( (unsigned long)_x2 << 16 ) | \ + ( (unsigned long)_x3 << 8 ) | \ + (unsigned long)_x4 ) + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Glyph_Format */ +/* */ +/* */ +/* An enumeration type used to describe the format of a given glyph */ +/* image. Note that this version of FreeType only supports two image */ +/* formats, even though future font drivers will be able to register */ +/* their own format. */ +/* */ +/* */ +/* ft_glyph_format_composite :: The glyph image is a composite of */ +/* several other images. This glyph */ +/* format is _only_ used with the */ +/* FT_LOAD_FLAG_NO_RECURSE flag (XXX: */ +/* Which is currently unimplemented). */ +/* */ +/* ft_glyph_format_bitmap :: The glyph image is a bitmap, and can */ +/* be described as a FT_Bitmap. */ +/* */ +/* ft_glyph_format_outline :: The glyph image is a vectorial image */ +/* made of bezier control points, and */ +/* can be described as a FT_Outline. */ +/* */ +/* ft_glyph_format_plotter :: The glyph image is a vectorial image */ +/* made of plotter lines (some T1 fonts */ +/* like Hershey contain glyph in this */ +/* format). */ +/* */ +typedef enum FT_Glyph_Format_ +{ + ft_glyph_format_none = 0, + ft_glyph_format_composite = FT_IMAGE_TAG( 'c', 'o', 'm', 'p' ), + ft_glyph_format_bitmap = FT_IMAGE_TAG( 'b', 'i', 't', 's' ), + ft_glyph_format_outline = FT_IMAGE_TAG( 'o', 'u', 't', 'l' ), + ft_glyph_format_plotter = FT_IMAGE_TAG( 'p', 'l', 'o', 't' ) + +} FT_Glyph_Format; + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/***** *****/ +/***** R A S T E R D E F I N I T I O N S *****/ +/***** *****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* A raster is a scan converter, in charge of rendering an outline into */ +/* a a bitmap. This section contains the public API for rasters. */ +/* */ +/* Note that in FreeType 2, all rasters are now encapsulated within */ +/* specific modules called `renderers'. See `freetype/ftrender.h' for */ +/* more details on renderers. */ +/* */ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Raster */ +/* */ +/* */ +/* A handle (pointer) to a raster object. Each object can be used */ +/* independently to convert an outline into a bitmap or pixmap. */ +/* */ +typedef struct FT_RasterRec_* FT_Raster; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Span */ +/* */ +/* */ +/* A structure used to model a single span of gray (or black) pixels */ +/* when rendering a monochrome or anti-aliased bitmap. */ +/* */ +/* */ +/* x :: The span's horizontal start position. */ +/* */ +/* len :: The span's length in pixels. */ +/* */ +/* coverage :: The span color/coverage, ranging from 0 (background) */ +/* to 255 (foreground). Only used for anti-aliased */ +/* rendering. */ +/* */ +/* */ +/* This structure is used by the span drawing callback type named */ +/* FT_Raster_Span_Func(), which takes the y-coordinate of the span as */ +/* a parameter. */ +/* */ +/* The coverage value is always between 0 and 255, even if the number */ +/* of gray levels have been set through FT_Set_Gray_Levels(). */ +/* */ +typedef struct FT_Span_ +{ + short x; + unsigned short len; + unsigned char coverage; + +} FT_Span; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Raster_Span_Func */ +/* */ +/* */ +/* A function used as a call-back by the anti-aliased renderer in */ +/* order to let client applications draw themselves the gray pixel */ +/* spans on each scan line. */ +/* */ +/* */ +/* y :: The scanline's y-coordinate. */ +/* */ +/* count :: The number of spans to draw on this scanline. */ +/* */ +/* spans :: A table of `count' spans to draw on the scanline. */ +/* */ +/* user :: User-supplied data that is passed to the callback. */ +/* */ +/* */ +/* This callback allows client applications to directly render the */ +/* gray spans of the anti-aliased bitmap to any kind of surfaces. */ +/* */ +/* This can be used to write anti-aliased outlines directly to a */ +/* given background bitmap, and even perform translucency. */ +/* */ +/* Note that the `count' field cannot be greater than a fixed value */ +/* defined by the FT_MAX_GRAY_SPANS configuration macro in */ +/* ftoption.h. By default, this value is set to 32, which means that */ +/* if there are more than 32 spans on a given scanline, the callback */ +/* will be called several times with the same `y' parameter in order */ +/* to draw all callbacks. */ +/* */ +/* Otherwise, the callback is only called once per scan-line, and */ +/* only for those scanlines that do have `gray' pixels on them. */ +/* */ +typedef void ( *FT_Raster_Span_Func )( int y, + int count, + FT_Span* spans, + void* user ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Raster_BitTest_Func */ +/* */ +/* */ +/* A function used as a call-back by the monochrome scan-converter */ +/* to test whether a given target pixel is already set to the drawing */ +/* `color'. These tests are crucial to implement drop-out control */ +/* per-se the TrueType spec. */ +/* */ +/* */ +/* y :: The pixel's y-coordinate. */ +/* */ +/* x :: The pixel's x-coordinate. */ +/* */ +/* user :: User-supplied data that is passed to the callback. */ +/* */ +/* */ +/* 1 if the pixel is `set', 0 otherwise. */ +/* */ +typedef int ( *FT_Raster_BitTest_Func )( int y, + int x, + void* user ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Raster_BitSet_Func */ +/* */ +/* */ +/* A function used as a call-back by the monochrome scan-converter */ +/* to set an individual target pixel. This is crucial to implement */ +/* drop-out control according to the TrueType specification. */ +/* */ +/* */ +/* y :: The pixel's y-coordinate. */ +/* */ +/* x :: The pixel's x-coordinate. */ +/* */ +/* user :: User-supplied data that is passed to the callback. */ +/* */ +/* */ +/* 1 if the pixel is `set', 0 otherwise. */ +/* */ +typedef void ( *FT_Raster_BitSet_Func )( int y, + int x, + void* user ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Raster_Flag */ +/* */ +/* */ +/* An enumeration to list the bit flags as used in the `flags' field */ +/* of a FT_Raster_Params structure. */ +/* */ +/* */ +/* ft_raster_flag_default :: This value is 0. */ +/* */ +/* ft_raster_flag_aa :: Requests the rendering of an */ +/* anti-aliased glyph bitmap. If unset, a */ +/* monchrome bitmap will be rendered. */ +/* */ +/* ft_raster_flag_direct :: Requests direct rendering over the */ +/* target bitmap. Direct rendering uses */ +/* user-provided callbacks in order to */ +/* perform direct drawing or composition */ +/* over an existing bitmap. If this bit is */ +/* unset, the content of the target bitmap */ +/* *must be zeroed*! */ +/* */ +typedef enum +{ + ft_raster_flag_default = 0, + ft_raster_flag_aa = 1, + ft_raster_flag_direct = 2 + +} FT_Raster_Flag; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Raster_Params */ +/* */ +/* */ +/* A structure to hold the arguments used by a raster's render */ +/* function. */ +/* */ +/* */ +/* target :: The target bitmap. */ +/* */ +/* source :: A pointer to the source glyph image (e.g. an */ +/* FT_Outline). */ +/* */ +/* flags :: The rendering flags. */ +/* */ +/* gray_spans :: The gray span drawing callback. */ +/* */ +/* black_spans :: The black span drawing callback. */ +/* */ +/* bit_test :: The bit test callback. */ +/* */ +/* bit_set :: The bit set callback. */ +/* */ +/* user :: User-supplied data that is passed to each drawing */ +/* callback. */ +/* */ +/* */ +/* An anti-aliased glyph bitmap is drawn if the ft_raster_flag_aa bit */ +/* flag is set in the `flags' field, otherwise a monochrome bitmap */ +/* will be generated. */ +/* */ +/* If the ft_raster_flag_direct bit flag is set in `flags', the */ +/* raster will call the `gray_spans' callback to draw gray pixel */ +/* spans, in the case of an aa glyph bitmap, it will call */ +/* `black_spans', and `bit_test' and `bit_set' in the case of a */ +/* monochrome bitmap. This allows direct composition over a */ +/* pre-existing bitmap through user-provided callbacks to perform the */ +/* span drawing/composition. */ +/* */ +/* Note that the `bit_test' and `bit_set' callbacks are required when */ +/* rendering a monochrome bitmap, as they are crucial to implement */ +/* correct drop-out control as defined in the TrueType specification. */ +/* */ +typedef struct FT_Raster_Params_ +{ + FT_Bitmap* target; + void* source; + int flags; + FT_Raster_Span_Func gray_spans; + FT_Raster_Span_Func black_spans; + FT_Raster_BitTest_Func bit_test; + FT_Raster_BitSet_Func bit_set; + void* user; + +} FT_Raster_Params; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Raster_New_Func */ +/* */ +/* */ +/* A function used to create a new raster object. */ +/* */ +/* */ +/* memory :: A handle to the memory allocator. */ +/* */ +/* */ +/* raster :: A handle to the new raster object. */ +/* */ +/* */ +/* Error code. 0 means success. */ +/* */ +/* */ +/* The `memory' parameter is a typeless pointer in order to avoid */ +/* un-wanted dependencies on the rest of the FreeType code. In */ +/* practice, it is a FT_Memory, i.e., a handle to the standard */ +/* FreeType memory allocator. However, this field can be completely */ +/* ignored by a given raster implementation. */ +/* */ +typedef int ( *FT_Raster_New_Func )( void* memory, + FT_Raster* raster ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Raster_Done_Func */ +/* */ +/* */ +/* A function used to destroy a given raster object. */ +/* */ +/* */ +/* raster :: A handle to the raster object. */ +/* */ +typedef void ( *FT_Raster_Done_Func )( FT_Raster raster ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Raster_Reset_Func */ +/* */ +/* */ +/* FreeType provides an area of memory called the `render pool', */ +/* available to all registered rasters. This pool can be freely used */ +/* during a given scan-conversion but is shared by all rasters. Its */ +/* content is thus transient. */ +/* */ +/* This function is called each time the render pool changes, or just */ +/* after a new raster object is created. */ +/* */ +/* */ +/* raster :: A handle to the new raster object. */ +/* */ +/* pool_base :: The address in memory of the render pool. */ +/* */ +/* pool_size :: The size in bytes of the render pool. */ +/* */ +/* */ +/* Rasters can ignore the render pool and rely on dynamic memory */ +/* allocation if they want to (a handle to the memory allocator is */ +/* passed to the raster constructor). However, this is not */ +/* recommended for efficiency purposes. */ +/* */ +typedef void ( *FT_Raster_Reset_Func )( FT_Raster raster, + unsigned char* pool_base, + unsigned long pool_size ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Raster_Set_Mode_Func */ +/* */ +/* */ +/* This function is a generic facility to change modes or attributes */ +/* in a given raster. This can be used for debugging purposes, or */ +/* simply to allow implementation-specific `features' in a given */ +/* raster module. */ +/* */ +/* */ +/* raster :: A handle to the new raster object. */ +/* */ +/* mode :: A 4-byte tag used to name the mode or property. */ +/* */ +/* args :: A pointer to the new mode/property to use. */ +/* */ +typedef int ( *FT_Raster_Set_Mode_Func )( FT_Raster raster, + unsigned long mode, + void* args ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Raster_Render_Func */ +/* */ +/* */ +/* Invokes a given raster to scan-convert a given glyph image into a */ +/* target bitmap. */ +/* */ +/* */ +/* raster :: A handle to the raster object. */ +/* */ +/* params :: A pointer to a FT_Raster_Params structure used to store */ +/* the rendering parameters. */ +/* */ +/* */ +/* Error code. 0 means success. */ +/* */ +/* */ +/* The exact format of the source image depends on the raster's glyph */ +/* format defined in its FT_Raster_Funcs structure. It can be an */ +/* FT_Outline or anything else in order to support a large array of */ +/* glyph formats. */ +/* */ +/* Note also that the render function can fail and return a */ +/* FT_Err_Unimplemented_Feature error code if the raster used does */ +/* not support direct composition. */ +/* */ +/* XXX: For now, the standard raster doesn't support direct */ +/* composition but this should change for the final release (see */ +/* the files demos/src/ftgrays.c and demos/src/ftgrays2.c for */ +/* examples of distinct implementations which support direct */ +/* composition). */ +/* */ +typedef int ( *FT_Raster_Render_Func )( FT_Raster raster, + FT_Raster_Params* params ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Raster_Funcs */ +/* */ +/* */ +/* A structure used to describe a given raster class to the library. */ +/* */ +/* */ +/* glyph_format :: The supported glyph format for this raster. */ +/* */ +/* raster_new :: The raster constructor. */ +/* */ +/* raster_reset :: Used to reset the render pool within the raster. */ +/* */ +/* raster_render :: A function to render a glyph into a given bitmap. */ +/* */ +/* raster_done :: The raster destructor. */ +/* */ +typedef struct FT_Raster_Funcs_ +{ + FT_Glyph_Format glyph_format; + FT_Raster_New_Func raster_new; + FT_Raster_Reset_Func raster_reset; + FT_Raster_Set_Mode_Func raster_set_mode; + FT_Raster_Render_Func raster_render; + FT_Raster_Done_Func raster_done; + +} FT_Raster_Funcs; + + +#ifdef __cplusplus +} +#endif + + +#endif /* FTIMAGE_H */ + + +/* END */ diff --git a/src/ft2/ftinit.c b/src/ft2/ftinit.c new file mode 100644 index 0000000..33497df --- /dev/null +++ b/src/ft2/ftinit.c @@ -0,0 +1,161 @@ +/***************************************************************************/ +/* */ +/* ftinit.c */ +/* */ +/* FreeType initialization layer (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + +/*************************************************************************/ +/* */ +/* The purpose of this file is to implement the following two */ +/* functions: */ +/* */ +/* FT_Add_Default_Modules(): */ +/* This function is used to add the set of default modules to a */ +/* fresh new library object. The set is taken from the header file */ +/* `freetype/config/ftmodule.h'. See the document `FreeType 2.0 */ +/* Build System' for more information. */ +/* */ +/* FT_Init_FreeType(): */ +/* This function creates a system object for the current platform, */ +/* builds a library out of it, then calls FT_Default_Drivers(). */ +/* */ +/* Note that even if FT_Init_FreeType() uses the implementation of the */ +/* system object defined at build time, client applications are still */ +/* able to provide their own `ftsystem.c'. */ +/* */ +/*************************************************************************/ + + +#include "ftconfig.h" +#include "ftobjs.h" +#include "ftdebug.h" +#include "ftmodule.h" +#include "ttdriver.h" +#include "ahmodule.h" +#include "sfdriver.h" +#include "ftsmooth.h" +#include "ftrend1.h" + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_init + +#undef FT_USE_MODULE +#define FT_USE_MODULE( x ) extern const FT_Module_Class * x; + +#ifdef macintosh +FT_USE_MODULE( fond_driver_class ) +#endif +#include "ftmodule.h" + +#undef FT_USE_MODULE +#define FT_USE_MODULE( x ) (const FT_Module_Class*)&x, + +static +const FT_Module_Class* ft_default_modules[] = +{ + //FT_USE_MODULE(autohint_module_class) + FT_USE_MODULE( ft_raster1_renderer_class ) + FT_USE_MODULE( sfnt_module_class ) + FT_USE_MODULE( ft_smooth_renderer_class ) + FT_USE_MODULE( tt_driver_class ) + +#include "ftmodule.h" + 0 +}; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Add_Default_Modules */ +/* */ +/* */ +/* Adds the set of default drivers to a given library object. */ +/* This is only useful when you create a library object with */ +/* FT_New_Library() (usually to plug a custom memory manager). */ +/* */ +/* */ +/* library :: A handle to a new library object. */ +/* */ +FT_EXPORT_FUNC( void ) FT_Add_Default_Modules( FT_Library library ) +{ + FT_Error error; + const FT_Module_Class** cur; + + + /* test for valid `library' delayed to FT_Add_Module() */ + + cur = ft_default_modules; + while ( *cur ) + { + error = FT_Add_Module( library, *cur ); + /* notify errors, but don't stop */ + if ( error ) { + FT_ERROR( ( "FT_Add_Default_Module: Cannot install `%s', error = %x\n", + ( *cur )->module_name, error ) ); + } + cur++; + } +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Init_FreeType */ +/* */ +/* */ +/* Initializes a new FreeType library object. The set of drivers */ +/* that are registered by this function is determined at build time. */ +/* */ +/* */ +/* library :: A handle to a new library object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Init_FreeType( FT_Library * library ) +{ + FT_Error error; + FT_Memory memory; + + + /* First of all, allocate a new system object -- this function is part */ + /* of the system-specific component, i.e. `ftsystem.c'. */ + + memory = FT_New_Memory(); + if ( !memory ) { + FT_ERROR( ( "FT_Init_FreeType: cannot find memory manager\n" ) ); + return FT_Err_Unimplemented_Feature; + } + + /* build a library out of it, then fill it with the set of */ + /* default drivers. */ + + error = FT_New_Library( memory, library ); + if ( !error ) { + FT_Add_Default_Modules( *library ); + } + + return error; +} + + +/* END */ diff --git a/src/ft2/ftlist.c b/src/ft2/ftlist.c new file mode 100644 index 0000000..3ea0704 --- /dev/null +++ b/src/ft2/ftlist.c @@ -0,0 +1,310 @@ +/***************************************************************************/ +/* */ +/* ftlist.c */ +/* */ +/* Generic list support for FreeType (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + +/*************************************************************************/ +/* */ +/* This file implements functions relative to list processing. Its */ +/* data structures are defined in `freetype/internal/ftlist.h'. */ +/* */ +/*************************************************************************/ + + +#include "ftlist.h" +#include "ftdebug.h" +#include "ftobjs.h" + + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_list + + +/*************************************************************************/ +/* */ +/* */ +/* FT_List_Find */ +/* */ +/* */ +/* Finds the list node for a given listed object. */ +/* */ +/* */ +/* list :: A pointer to the parent list. */ +/* data :: The address of the listed object. */ +/* */ +/* */ +/* List node. NULL if it wasn't found. */ +/* */ +BASE_FUNC( FT_ListNode ) FT_List_Find( FT_List list, + void* data ) +{ + FT_ListNode cur; + + + cur = list->head; + while ( cur ) + { + if ( cur->data == data ) { + return cur; + } + + cur = cur->next; + } + + return (FT_ListNode)0; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_List_Add */ +/* */ +/* */ +/* Appends an element to the end of a list. */ +/* */ +/* */ +/* list :: A pointer to the parent list. */ +/* node :: The node to append. */ +/* */ +BASE_FUNC( void ) FT_List_Add( FT_List list, + FT_ListNode node ) +{ + FT_ListNode before = list->tail; + + + node->next = 0; + node->prev = before; + + if ( before ) { + before->next = node; + } else { + list->head = node; + } + + list->tail = node; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_List_Insert */ +/* */ +/* */ +/* Inserts an element at the head of a list. */ +/* */ +/* */ +/* list :: A pointer to parent list. */ +/* node :: The node to insert. */ +/* */ +BASE_FUNC( void ) FT_List_Insert( FT_List list, + FT_ListNode node ) +{ + FT_ListNode after = list->head; + + + node->next = after; + node->prev = 0; + + if ( !after ) { + list->tail = node; + } else { + after->prev = node; + } + + list->head = node; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_List_Remove */ +/* */ +/* */ +/* Removes a node from a list. This function doesn't check whether */ +/* the node is in the list! */ +/* */ +/* */ +/* node :: The node to remove. */ +/* */ +/* */ +/* list :: A pointer to the parent list. */ +/* */ +BASE_FUNC( void ) FT_List_Remove( FT_List list, + FT_ListNode node ) +{ + FT_ListNode before, after; + + + before = node->prev; + after = node->next; + + if ( before ) { + before->next = after; + } else { + list->head = after; + } + + if ( after ) { + after->prev = before; + } else { + list->tail = before; + } +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_List_Up */ +/* */ +/* */ +/* Moves a node to the head/top of a list. Used to maintain LRU */ +/* lists. */ +/* */ +/* */ +/* list :: A pointer to the parent list. */ +/* node :: The node to move. */ +/* */ +BASE_FUNC( void ) FT_List_Up( FT_List list, + FT_ListNode node ) +{ + FT_ListNode before, after; + + + before = node->prev; + after = node->next; + + /* check whether we are already on top of the list */ + if ( !before ) { + return; + } + + before->next = after; + + if ( after ) { + after->prev = before; + } else { + list->tail = before; + } + + node->prev = 0; + node->next = list->head; + list->head->prev = node; + list->head = node; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_List_Iterate */ +/* */ +/* */ +/* Parses a list and calls a given iterator function on each element. */ +/* Note that parsing is stopped as soon as one of the iterator calls */ +/* returns a non-zero value. */ +/* */ +/* */ +/* list :: A handle to the list. */ +/* iterator :: An interator function, called on each node of the */ +/* list. */ +/* user :: A user-supplied field which is passed as the second */ +/* argument to the iterator. */ +/* */ +/* */ +/* The result (a FreeType error code) of the last iterator call. */ +/* */ +BASE_FUNC( FT_Error ) FT_List_Iterate( FT_List list, + FT_List_Iterator iterator, + void* user ) +{ + FT_ListNode cur = list->head; + FT_Error error = FT_Err_Ok; + + + while ( cur ) + { + FT_ListNode next = cur->next; + + + error = iterator( cur, user ); + if ( error ) { + break; + } + + cur = next; + } + + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_List_Finalize */ +/* */ +/* */ +/* Destroys all elements in the list as well as the list itself. */ +/* */ +/* */ +/* list :: A handle to the list. */ +/* */ +/* destroy :: A list destructor that will be applied to each element */ +/* of the list. */ +/* */ +/* memory :: The current memory object which handles deallocation. */ +/* */ +/* user :: A user-supplied field which is passed as the last */ +/* argument to the destructor. */ +/* */ +BASE_FUNC( void ) FT_List_Finalize( FT_List list, + FT_List_Destructor destroy, + FT_Memory memory, + void* user ) +{ + FT_ListNode cur; + + + cur = list->head; + while ( cur ) + { + FT_ListNode next = cur->next; + void* data = cur->data; + + + if ( destroy ) { + destroy( memory, data, user ); + } + + FREE( cur ); + cur = next; + } + + list->head = 0; + list->tail = 0; +} + + +/* END */ diff --git a/src/ft2/ftlist.h b/src/ft2/ftlist.h new file mode 100644 index 0000000..8473833 --- /dev/null +++ b/src/ft2/ftlist.h @@ -0,0 +1,113 @@ +/***************************************************************************/ +/* */ +/* ftlist.c */ +/* */ +/* Generic list support for FreeType (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +/*************************************************************************/ +/* */ +/* This file implements functions relative to list processing. Its */ +/* data structures are defined in `freetype.h'. */ +/* */ +/*************************************************************************/ + + +#ifndef FTLIST_H +#define FTLIST_H + +#include "freetype.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +FT_EXPORT_DEF( FT_ListNode ) FT_List_Find( FT_List list, + void* data ); + +FT_EXPORT_DEF( void ) FT_List_Add( FT_List list, + FT_ListNode node ); + +FT_EXPORT_DEF( void ) FT_List_Insert( FT_List list, + FT_ListNode node ); + +FT_EXPORT_DEF( void ) FT_List_Remove( FT_List list, + FT_ListNode node ); + +FT_EXPORT_DEF( void ) FT_List_Up( FT_List list, + FT_ListNode node ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_List_Iterator */ +/* */ +/* */ +/* An FT_List iterator function which is called during a list parse */ +/* by FT_List_Iterate(). */ +/* */ +/* */ +/* node :: The current iteration list node. */ +/* */ +/* user :: A typeless pointer passed to FT_List_Iterate(). */ +/* Can be used to point to the iteration's state. */ +/* */ +typedef FT_Error ( *FT_List_Iterator )( FT_ListNode node, + void* user ); + + +FT_EXPORT_DEF( FT_Error ) FT_List_Iterate( FT_List list, + FT_List_Iterator iterator, + void* user ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_List_Destructor */ +/* */ +/* */ +/* An FT_List iterator function which is called during a list */ +/* finalization by FT_List_Finalize() to destroy all elements in a */ +/* given list. */ +/* */ +/* */ +/* system :: The current system object. */ +/* */ +/* data :: The current object to destroy. */ +/* */ +/* user :: A typeless pointer passed to FT_List_Iterate(). It can */ +/* be used to point to the iteration's state. */ +/* */ +typedef void ( *FT_List_Destructor )( FT_Memory memory, + void* data, + void* user ); + + +FT_EXPORT_DEF( void ) FT_List_Finalize( FT_List list, + FT_List_Destructor destroy, + FT_Memory memory, + void* user ); + + +#ifdef __cplusplus +} +#endif + +#endif /* FTLIST_H */ + + +/* END */ diff --git a/src/ft2/ftmemory.h b/src/ft2/ftmemory.h new file mode 100644 index 0000000..702dda6 --- /dev/null +++ b/src/ft2/ftmemory.h @@ -0,0 +1,127 @@ +/***************************************************************************/ +/* */ +/* ftmemory.h */ +/* */ +/* The FreeType memory management macros (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef FTMEMORY_H +#define FTMEMORY_H + + +#include "ftconfig.h" +#include "fttypes.h" + + +/*************************************************************************/ +/* */ +/* */ +/* FT_SET_ERROR */ +/* */ +/* */ +/* This macro is used to set an implicit `error' variable to a given */ +/* expression's value (usually a function call), and convert it to a */ +/* boolean which is set whenever the value is != 0. */ +/* */ +#undef FT_SET_ERROR +#define FT_SET_ERROR( expression ) \ + ( ( error = ( expression ) ) != 0 ) + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** ****/ +/**** M E M O R Y ****/ +/**** ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + +BASE_DEF( FT_Error ) FT_Alloc( FT_Memory memory, + FT_Long size, + void** P ); + +BASE_DEF( FT_Error ) FT_Realloc( FT_Memory memory, + FT_Long current, + FT_Long size, + void** P ); + +BASE_DEF( void ) FT_Free( FT_Memory memory, + void** P ); + + + +/* This `#include' is needed by the MEM_xxx() macros; it should be */ +/* available on all platforms we know of. */ +#include + +#define MEM_Set( dest, byte, count ) memset( dest, byte, count ) + +#define MEM_Copy( dest, source, count ) memcpy( dest, source, count ) + +#define MEM_Move( dest, source, count ) memmove( dest, source, count ) + + +/*************************************************************************/ +/* */ +/* We now support closures to produce completely reentrant code. This */ +/* means the allocation functions now takes an additional argument */ +/* (`memory'). It is a handle to a given memory object, responsible for */ +/* all low-level operations, including memory management and */ +/* synchronisation. */ +/* */ +/* In order to keep our code readable and use the same macros in the */ +/* font drivers and the rest of the library, MEM_Alloc(), ALLOC(), and */ +/* ALLOC_ARRAY() now use an implicit variable, `memory'. It must be */ +/* defined at all locations where a memory operation is queried. */ +/* */ +#define MEM_Alloc( _pointer_, _size_ ) \ + FT_Alloc( memory, _size_, (void**)&( _pointer_ ) ) + +#define MEM_Alloc_Array( _pointer_, _count_, _type_ ) \ + FT_Alloc( memory, ( _count_ ) * sizeof( _type_ ), \ + (void**)&( _pointer_ ) ) + +#define MEM_Realloc( _pointer_, _current_, _size_ ) \ + FT_Realloc( memory, _current_, _size_, (void**)&( _pointer_ ) ) + +#define MEM_Realloc_Array( _pointer_, _current_, _new_, _type_ ) \ + FT_Realloc( memory, ( _current_ ) * sizeof( _type_ ), \ + ( _new_ ) * sizeof( _type_ ), (void**)&( _pointer_ ) ) + +#define ALLOC( _pointer_, _size_ ) \ + FT_SET_ERROR( MEM_Alloc( _pointer_, _size_ ) ) + +#define REALLOC( _pointer_, _current_, _size_ ) \ + FT_SET_ERROR( MEM_Realloc( _pointer_, _current_, _size_ ) ) + +#define ALLOC_ARRAY( _pointer_, _count_, _type_ ) \ + FT_SET_ERROR( MEM_Alloc( _pointer_, \ + ( _count_ ) * sizeof( _type_ ) ) ) + +#define REALLOC_ARRAY( _pointer_, _current_, _count_, _type_ ) \ + FT_SET_ERROR( MEM_Realloc( _pointer_, \ + ( _current_ ) * sizeof( _type_ ), \ + ( _count_ ) * sizeof( _type_ ) ) ) + +#define FREE( _pointer_ ) FT_Free( memory, (void**)&( _pointer_ ) ) + + +#endif /* FTMEMORY_H */ + + +/* END */ diff --git a/src/ft2/ftmm.c b/src/ft2/ftmm.c new file mode 100644 index 0000000..a031d14 --- /dev/null +++ b/src/ft2/ftmm.c @@ -0,0 +1,179 @@ +/***************************************************************************/ +/* */ +/* ftmm.c */ +/* */ +/* Multiple Master font support (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#include "ftmm.h" +#include "ftobjs.h" + + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_mm + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Get_Multi_Master */ +/* */ +/* */ +/* Retrieves the Multiple Master descriptor of a given font. */ +/* */ +/* */ +/* face :: A handle to the source face. */ +/* */ +/* */ +/* master :: The Multiple Masters descriptor. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Get_Multi_Master( FT_Face face, + FT_Multi_Master * master ) +{ + FT_Error error; + + + if ( !face ) { + return FT_Err_Invalid_Face_Handle; + } + + error = FT_Err_Invalid_Argument; + + if ( FT_HAS_MULTIPLE_MASTERS( face ) ) { + FT_Driver driver = face->driver; + FT_Get_MM_Func func; + + + func = (FT_Get_MM_Func)driver->root.clazz->get_interface( + FT_MODULE( driver ), "get_mm" ); + if ( func ) { + error = func( face, master ); + } + } + + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Set_MM_Design_Coordinates */ +/* */ +/* */ +/* For Multiple Masters fonts, choose an interpolated font design */ +/* through design coordinates. */ +/* */ +/* */ +/* face :: A handle to the source face. */ +/* */ +/* num_coords :: The number of design coordinates (must be equal to */ +/* the number of axes in the font). */ +/* */ +/* coords :: The design coordinates. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Set_MM_Design_Coordinates( + FT_Face face, + FT_UInt num_coords, + FT_Long * coords ) +{ + FT_Error error; + + + if ( !face ) { + return FT_Err_Invalid_Face_Handle; + } + + error = FT_Err_Invalid_Argument; + + if ( FT_HAS_MULTIPLE_MASTERS( face ) ) { + FT_Driver driver = face->driver; + FT_Set_MM_Design_Func func; + + + func = (FT_Set_MM_Design_Func)driver->root.clazz->get_interface( + FT_MODULE( driver ), "set_mm_design" ); + if ( func ) { + error = func( face, num_coords, coords ); + } + } + + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Set_MM_Blend_Coordinates */ +/* */ +/* */ +/* For Multiple Masters fonts, choose an interpolated font design */ +/* through normalized blend coordinates. */ +/* */ +/* */ +/* face :: A handle to the source face. */ +/* */ +/* num_coords :: The number of design coordinates (must be equal to */ +/* the number of axes in the font). */ +/* */ +/* coords :: The design coordinates (each one must be between 0 */ +/* and 1.0). */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Set_MM_Blend_Coordinates( + FT_Face face, + FT_UInt num_coords, + FT_Fixed * coords ) +{ + FT_Error error; + + + if ( !face ) { + return FT_Err_Invalid_Face_Handle; + } + + error = FT_Err_Invalid_Argument; + + if ( FT_HAS_MULTIPLE_MASTERS( face ) ) { + FT_Driver driver = face->driver; + FT_Set_MM_Blend_Func func; + + + func = (FT_Set_MM_Blend_Func)driver->root.clazz->get_interface( + FT_MODULE( driver ), "set_mm_blend" ); + if ( func ) { + error = func( face, num_coords, coords ); + } + } + + return error; +} + + +/* END */ diff --git a/src/ft2/ftmm.h b/src/ft2/ftmm.h new file mode 100644 index 0000000..f7daf73 --- /dev/null +++ b/src/ft2/ftmm.h @@ -0,0 +1,175 @@ +/***************************************************************************/ +/* */ +/* ftmm.h */ +/* */ +/* FreeType Multiple Master font interface (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef FTMM_H +#define FTMM_H + +#include "t1tables.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +/*************************************************************************/ +/* */ +/* */ +/* FT_MM_Axis */ +/* */ +/* */ +/* A simple structure used to model a given axis in design space for */ +/* Multiple Masters fonts. */ +/* */ +/* */ +/* name :: The axis's name. */ +/* */ +/* minimum :: The axis's minimum design coordinate. */ +/* */ +/* maximum :: The axis's maximum design coordinate. */ +/* */ +typedef struct FT_MM_Axis_ +{ + FT_String* name; + FT_Long minimum; + FT_Long maximum; + +} FT_MM_Axis; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Multi_Master */ +/* */ +/* */ +/* A structure used to model the axes and space of a Multiple Masters */ +/* font. */ +/* */ +/* */ +/* num_axis :: Number of axes. Cannot exceed 4. */ +/* */ +/* num_designs :: Number of designs; should ne normally 2^num_axis */ +/* even though the Type 1 specification strangely */ +/* allows for intermediate designs to be present. This */ +/* number cannot exceed 16. */ +/* */ +/* axis :: A table of axis descriptors. */ +/* */ +typedef struct FT_Multi_Master_ +{ + FT_UInt num_axis; + FT_UInt num_designs; + FT_MM_Axis axis[T1_MAX_MM_AXIS]; + +} FT_Multi_Master; + + +typedef FT_Error ( *FT_Get_MM_Func )( FT_Face face, + FT_Multi_Master* master ); + +typedef FT_Error ( *FT_Set_MM_Design_Func )( FT_Face face, + FT_UInt num_coords, + FT_Long* coords ); + +typedef FT_Error ( *FT_Set_MM_Blend_Func )( FT_Face face, + FT_UInt num_coords, + FT_Long* coords ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Get_Multi_Master */ +/* */ +/* */ +/* Retrieves the Multiple Master descriptor of a given font. */ +/* */ +/* */ +/* face :: A handle to the source face. */ +/* */ +/* */ +/* master :: The Multiple Masters descriptor. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Get_Multi_Master( FT_Face face, + FT_Multi_Master * master ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Set_MM_Design_Coordinates */ +/* */ +/* */ +/* For Multiple Masters fonts, choose an interpolated font design */ +/* through design coordinates. */ +/* */ +/* */ +/* face :: A handle to the source face. */ +/* */ +/* num_coords :: The number of design coordinates (must be equal to */ +/* the number of axes in the font). */ +/* */ +/* coords :: The design coordinates. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Set_MM_Design_Coordinates( + FT_Face face, + FT_UInt num_coords, + FT_Long * coords ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Set_MM_Blend_Coordinates */ +/* */ +/* */ +/* For Multiple Masters fonts, choose an interpolated font design */ +/* through normalized blend coordinates. */ +/* */ +/* */ +/* face :: A handle to the source face. */ +/* */ +/* num_coords :: The number of design coordinates (must be equal to */ +/* the number of axes in the font). */ +/* */ +/* coords :: The design coordinates (each one must be between 0 */ +/* and 1.0). */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Set_MM_Blend_Coordinates( + FT_Face face, + FT_UInt num_coords, + FT_Fixed * coords ); + + +#ifdef __cplusplus +} +#endif + +#endif /* FTMM_H */ + + +/* END */ diff --git a/src/ft2/ftmodule.h b/src/ft2/ftmodule.h new file mode 100644 index 0000000..f2ab117 --- /dev/null +++ b/src/ft2/ftmodule.h @@ -0,0 +1,274 @@ +/***************************************************************************/ +/* */ +/* ftmodule.h */ +/* */ +/* FreeType modules public interface (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef FTMODULE_H +#define FTMODULE_H + +#include "freetype.h" + + +#ifdef __cplusplus +extern "C" { +#endif + + +/* module bit flags */ +typedef enum FT_Module_Flags_ +{ + ft_module_font_driver = 1, /* this module is a font driver */ + ft_module_renderer = 2, /* this module is a renderer */ + ft_module_hinter = 4, /* this module is a glyph hinter */ + ft_module_styler = 8, /* this module is a styler */ + + ft_module_driver_scalable = 0x100, /* the driver supports scalable */ + /* fonts */ + ft_module_driver_no_outlines = 0x200, /* the driver does not support */ + /* vector outlines */ + ft_module_driver_has_hinter = 0x400 /* the driver provides its own */ + /* hinter */ + +} FT_Module_Flags; + + +typedef void ( *FT_Module_Interface )( void ); + +typedef FT_Error ( *FT_Module_Constructor )( FT_Module module ); + +typedef void ( *FT_Module_Destructor )( FT_Module module ); + +typedef FT_Module_Interface ( *FT_Module_Requester )( FT_Module module, + const char* name ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Module_Class */ +/* */ +/* */ +/* The module class descriptor. */ +/* */ +/* */ +/* module_flags :: Bit flags describing the module. */ +/* */ +/* module_size :: The size of one module object/instance in */ +/* bytes. */ +/* */ +/* module_name :: The name of the module. */ +/* */ +/* module_version :: The version, as a 16.16 fixed number */ +/* (major.minor). */ +/* */ +/* module_requires :: The version of FreeType this module requires */ +/* (starts at version 2.0, i.e 0x20000) */ +/* */ +/* module_init :: A function used to initialize (not create) a */ +/* new module object. */ +/* */ +/* module_done :: A function used to finalize (not destroy) a */ +/* given module object */ +/* */ +/* get_interface :: Queries a given module for a specific */ +/* interface by name. */ +/* */ +typedef struct FT_Module_Class_ +{ + FT_ULong module_flags; + FT_Int module_size; + const FT_String* module_name; + FT_Fixed module_version; + FT_Fixed module_requires; + + const void* module_interface; + + FT_Module_Constructor module_init; + FT_Module_Destructor module_done; + FT_Module_Requester get_interface; + +} FT_Module_Class; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Add_Module */ +/* */ +/* */ +/* Adds a new module to a given library instance. */ +/* */ +/* */ +/* library :: A handle to the library object. */ +/* */ +/* clazz :: A pointer to class descriptor for the module. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* An error will be returned if a module already exists by that name, */ +/* or if the module requires a version of FreeType that is too great. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Add_Module( FT_Library library, + const FT_Module_Class * clazz ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Get_Module */ +/* */ +/* */ +/* Finds a module by its name. */ +/* */ +/* */ +/* library :: A handle to the library object. */ +/* */ +/* module_name :: The module's name (as an ASCII string). */ +/* */ +/* */ +/* A module handle. 0 if none was found. */ +/* */ +/* */ +/* You should better be familiar with FreeType internals to know */ +/* which module to look for :-) */ +/* */ +FT_EXPORT_DEF( FT_Module ) FT_Get_Module( FT_Library library, + const char* module_name ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Remove_Module */ +/* */ +/* */ +/* Removes a given module from a library instance. */ +/* */ +/* */ +/* library :: A handle to a library object. */ +/* */ +/* module :: A handle to a module object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* The module object is destroyed by the function in case of success. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Remove_Module( FT_Library library, + FT_Module module ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_New_Library */ +/* */ +/* */ +/* This function is used to create a new FreeType library instance */ +/* from a given memory object. It is thus possible to use libraries */ +/* with distinct memory allocators within the same program. */ +/* */ +/* */ +/* memory :: A handle to the original memory object. */ +/* */ +/* */ +/* alibrary :: A pointer to handle of a new library object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_New_Library( FT_Memory memory, + FT_Library * library ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Done_Library */ +/* */ +/* */ +/* Discards a given library object. This closes all drivers and */ +/* discards all resource objects. */ +/* */ +/* */ +/* library :: A handle to the target library. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Done_Library( FT_Library library ); + + + +typedef void ( *FT_DebugHook_Func )( void* arg ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Set_Debug_Hook */ +/* */ +/* */ +/* Sets a debug hook function for debugging the interpreter of a font */ +/* format. */ +/* */ +/* */ +/* library :: A handle to the library object. */ +/* */ +/* hook_index :: The index of the debug hook. You should use the */ +/* values defined in ftobjs.h, e.g. */ +/* FT_DEBUG_HOOK_TRUETYPE */ +/* */ +/* debug_hook :: The function used to debug the interpreter. */ +/* */ +/* */ +/* Currently, four debug hook slots are available, but only two (for */ +/* the TrueType and the Type 1 interpreter) are defined. */ +/* */ +FT_EXPORT_DEF( void ) FT_Set_Debug_Hook( FT_Library library, + FT_UInt hook_index, + FT_DebugHook_Func debug_hook ); + + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Add_Default_Modules */ +/* */ +/* */ +/* Adds the set of default drivers to a given library object. */ +/* This is only useful when you create a library object with */ +/* FT_New_Library() (usually to plug a custom memory manager). */ +/* */ +/* */ +/* library :: A handle to a new library object. */ +/* */ +FT_EXPORT_DEF( void ) FT_Add_Default_Modules( FT_Library library ); + + +#ifdef __cplusplus +} +#endif + + +#endif /* FTMODULE_H */ + + +/* END */ diff --git a/src/ft2/ftnames.c b/src/ft2/ftnames.c new file mode 100644 index 0000000..446e204 --- /dev/null +++ b/src/ft2/ftnames.c @@ -0,0 +1,68 @@ +/***************************************************************************/ +/* */ +/* ftnames.c */ +/* */ +/* Simple interface to access SFNT name tables (which are used */ +/* to hold font names, copyright info, notices, etc.). */ +/* */ +/* This is _not_ used to retrieve glyph names! */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#include "ftnames.h" +#include "tttypes.h" + + +#ifdef FT_CONFIG_OPTION_SFNT_NAMES + + +FT_EXPORT_FUNC( FT_UInt ) FT_Get_Sfnt_Name_Count( FT_Face face ) +{ + return face && ( FT_IS_SFNT( face ) ? ( (TT_Face)face )->num_names : 0 ); +} + + +FT_EXPORT_FUNC( FT_Error ) FT_Get_Sfnt_Name( FT_Face face, + FT_UInt index, + FT_SfntName * aname ) +{ + FT_Error error = FT_Err_Invalid_Argument; + + + if ( aname && face && FT_IS_SFNT( face ) ) { + TT_Face ttface = (TT_Face)face; + + + if ( index < ttface->num_names ) { + TT_NameRec* name = ttface->name_table.names + index; + + + aname->platform_id = name->platformID; + aname->encoding_id = name->encodingID; + aname->language_id = name->languageID; + aname->name_id = name->nameID; + aname->string = (FT_Byte*)name->string; + aname->string_len = name->stringLength; + + error = FT_Err_Ok; + } + } + + return error; +} + + +#endif /* FT_CONFIG_OPTION_SFNT_NAMES */ + + +/* END */ diff --git a/src/ft2/ftnames.h b/src/ft2/ftnames.h new file mode 100644 index 0000000..392250a --- /dev/null +++ b/src/ft2/ftnames.h @@ -0,0 +1,52 @@ +/***************************************************************************/ +/* */ +/* ftnames.h */ +/* */ +/* Simple interface to access SFNT name tables (which are used */ +/* to hold font names, copyright info, notices, etc.). */ +/* */ +/* This is _not_ used to retrieve glyph names! */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef FTNAMES_H +#define FTNAMES_H + + +#include "freetype.h" + + +typedef struct FT_SfntName_ +{ + FT_UShort platform_id; + FT_UShort encoding_id; + FT_UShort language_id; + FT_UShort name_id; + + FT_Byte* string; + FT_UInt string_len; /* in bytes */ + +} FT_SfntName; + + +FT_EXPORT_DEF( FT_UInt ) FT_Get_Sfnt_Name_Count( FT_Face face ); + +FT_EXPORT_DEF( FT_Error ) FT_Get_Sfnt_Name( FT_Face face, + FT_UInt index, + FT_SfntName * aname ); + + +#endif /* FTNAMES_H */ + + +/* END */ diff --git a/src/ft2/ftobjs.c b/src/ft2/ftobjs.c new file mode 100644 index 0000000..6e0bf05 --- /dev/null +++ b/src/ft2/ftobjs.c @@ -0,0 +1,3280 @@ +/***************************************************************************/ +/* */ +/* ftobjs.c */ +/* */ +/* The FreeType private base classes (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#include "ftobjs.h" +#include "ftlist.h" +#include "ftdebug.h" +#include "ftstream.h" + +#include "tttables.h" + +#include /* for strcmp() */ + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** ****/ +/**** M E M O R Y ****/ +/**** ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_memory + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Alloc */ +/* */ +/* */ +/* Allocates a new block of memory. The returned area is always */ +/* zero-filled; this is a strong convention in many FreeType parts. */ +/* */ +/* */ +/* memory :: A handle to a given `memory object' which handles */ +/* allocation. */ +/* */ +/* size :: The size in bytes of the block to allocate. */ +/* */ +/* */ +/* P :: A pointer to the fresh new block. It should be set to */ +/* NULL if `size' is 0, or in case of error. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +BASE_FUNC( FT_Error ) FT_Alloc( FT_Memory memory, + FT_Long size, + void** P ) +{ + FT_Assert( P != 0 ); + + if ( size > 0 ) { + *P = memory->alloc( memory, size ); + if ( !*P ) { + FT_ERROR( ( "FT_Alloc:" ) ); + FT_ERROR( ( " Out of memory? (%ld requested)\n", + size ) ); + + return FT_Err_Out_Of_Memory; + } + MEM_Set( *P, 0, size ); + } else { + *P = NULL; + } + + FT_TRACE7( ( "FT_Alloc:" ) ); + FT_TRACE7( ( " size = %ld, block = 0x%08p, ref = 0x%08p\n", + size, *P, P ) ); + + return FT_Err_Ok; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Realloc */ +/* */ +/* */ +/* Reallocates a block of memory pointed to by `*P' to `Size' bytes */ +/* from the heap, possibly changing `*P'. */ +/* */ +/* */ +/* memory :: A handle to a given `memory object' which handles */ +/* reallocation. */ +/* */ +/* current :: The current block size in bytes. */ +/* */ +/* size :: The new block size in bytes. */ +/* */ +/* */ +/* P :: A pointer to the fresh new block. It should be set to */ +/* NULL if `size' is 0, or in case of error. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* All callers of FT_Realloc() _must_ provide the current block size */ +/* as well as the new one. */ +/* */ +BASE_FUNC( FT_Error ) FT_Realloc( FT_Memory memory, + FT_Long current, + FT_Long size, + void** P ) +{ + void* Q; + + + FT_Assert( P != 0 ); + + /* if the original pointer is NULL, call FT_Alloc() */ + if ( !*P ) { + return FT_Alloc( memory, size, P ); + } + + /* if the new block if zero-sized, clear the current one */ + if ( size <= 0 ) { + FT_Free( memory, P ); + return FT_Err_Ok; + } + + Q = memory->realloc( memory, current, size, *P ); + if ( !Q ) { + goto Fail; + } + + *P = Q; + return FT_Err_Ok; + +Fail: + FT_ERROR( ( "FT_Realloc:" ) ); + FT_ERROR( ( " Failed (current %ld, requested %ld)\n", + current, size ) ); + return FT_Err_Out_Of_Memory; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Free */ +/* */ +/* */ +/* Releases a given block of memory allocated through FT_Alloc(). */ +/* */ +/* */ +/* memory :: A handle to a given `memory object' which handles */ +/* memory deallocation */ +/* */ +/* P :: This is the _address_ of a _pointer_ which points to the */ +/* allocated block. It is always set to NULL on exit. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* If P or *P are NULL, this function should return successfully. */ +/* This is a strong convention within all of FreeType and its */ +/* drivers. */ +/* */ +BASE_FUNC( void ) FT_Free( FT_Memory memory, + void** P ) +{ + FT_TRACE7( ( "FT_Free:" ) ); + FT_TRACE7( ( " Freeing block 0x%08p, ref 0x%08p\n", + P, P ? *P : (void*)0 ) ); + + if ( P && *P ) { + memory->free( memory, *P ); + *P = 0; + } +} + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** ****/ +/**** S T R E A M ****/ +/**** ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* ft_new_input_stream */ +/* */ +/* */ +/* Creates a new input stream object from an FT_Open_Args structure. */ +/* */ +/* */ +/* The function expects a valid `astream' parameter. */ +/* */ +static +FT_Error ft_new_input_stream( FT_Library library, + FT_Open_Args* args, + FT_Stream* astream ) { + FT_Error error; + FT_Memory memory; + FT_Stream stream; + + + if ( !library ) { + return FT_Err_Invalid_Library_Handle; + } + + if ( !args ) { + return FT_Err_Invalid_Argument; + } + + *astream = 0; + memory = library->memory; + if ( ALLOC( stream, sizeof( *stream ) ) ) { + goto Exit; + } + + stream->memory = memory; + + /* now, look at the stream flags */ + if ( args->flags & ft_open_memory ) { + error = 0; + FT_New_Memory_Stream( library, + args->memory_base, + args->memory_size, + stream ); + } else if ( args->flags & ft_open_pathname ) { + error = FT_New_Stream( args->pathname, stream ); + stream->pathname.pointer = args->pathname; + } else if ( args->flags & ft_open_stream && args->stream ) { + *stream = *( args->stream ); + stream->memory = memory; + } else { + error = FT_Err_Invalid_Argument; + } + + if ( error ) { + FREE( stream ); + } + + *astream = stream; + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Done_Stream */ +/* */ +/* */ +/* Closes and destroys a stream object. */ +/* */ +/* */ +/* stream :: The stream to be closed and destroyed. */ +/* */ +FT_EXPORT_FUNC( void ) FT_Done_Stream( FT_Stream stream ) +{ + if ( stream && stream->close ) { + stream->close( stream ); + } +} + + +static +void ft_done_stream( FT_Stream* astream ) { + FT_Stream stream = *astream; + FT_Memory memory = stream->memory; + + + if ( stream->close ) { + stream->close( stream ); + } + + FREE( stream ); + *astream = 0; +} + + +#undef FT_COMPONENT +#define FT_COMPONENT trace_objs + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** ****/ +/**** G L Y P H L O A D E R ****/ +/**** ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* The glyph loader is a simple object which is used to load a set of */ +/* glyphs easily. It is critical for the correct loading of composites. */ +/* */ +/* Ideally, one can see it as a stack of abstract `glyph' objects. */ +/* */ +/* loader.base Is really the bottom of the stack. It describes a */ +/* single glyph image made of the juxtaposition of */ +/* several glyphs (those `in the stack'). */ +/* */ +/* loader.current Describes the top of the stack, on which a new */ +/* glyph can be loaded. */ +/* */ +/* Rewind Clears the stack. */ +/* Prepare Set up `loader.current' for addition of a new glyph */ +/* image. */ +/* Add Add the `current' glyph image to the `base' one, */ +/* and prepare for another one. */ +/* */ +/* The glyph loader is now a base object. Each driver used to */ +/* re-implement it in one way or the other, which wasted code and */ +/* energy. */ +/* */ +/*************************************************************************/ + + +/* create a new glyph loader */ +BASE_FUNC( FT_Error ) FT_GlyphLoader_New( FT_Memory memory, + FT_GlyphLoader * *aloader ) +{ + FT_GlyphLoader* loader; + FT_Error error; + + + if ( !ALLOC( loader, sizeof( *loader ) ) ) { + loader->memory = memory; + *aloader = loader; + } + return error; +} + + +/* rewind the glyph loader - reset counters to 0 */ +BASE_FUNC( void ) FT_GlyphLoader_Rewind( FT_GlyphLoader * loader ) +{ + FT_GlyphLoad* base = &loader->base; + FT_GlyphLoad* current = &loader->current; + + + base->outline.n_points = 0; + base->outline.n_contours = 0; + base->num_subglyphs = 0; + + *current = *base; +} + + +/* reset the glyph loader, frees all allocated tables */ +/* and starts from zero */ +BASE_FUNC( void ) FT_GlyphLoader_Reset( FT_GlyphLoader * loader ) +{ + FT_Memory memory = loader->memory; + + + FREE( loader->base.outline.points ); + FREE( loader->base.outline.tags ); + FREE( loader->base.outline.contours ); + FREE( loader->base.extra_points ); + FREE( loader->base.subglyphs ); + + loader->max_points = 0; + loader->max_contours = 0; + loader->max_subglyphs = 0; + + FT_GlyphLoader_Rewind( loader ); +} + + +/* delete a glyph loader */ +BASE_FUNC( void ) FT_GlyphLoader_Done( FT_GlyphLoader * loader ) +{ + if ( loader ) { + FT_Memory memory = loader->memory; + + + FT_GlyphLoader_Reset( loader ); + FREE( loader ); + } +} + + +/* re-adjust the `current' outline fields */ +static +void FT_GlyphLoader_Adjust_Points( FT_GlyphLoader* loader ) { + FT_Outline* base = &loader->base.outline; + FT_Outline* current = &loader->current.outline; + + + current->points = base->points + base->n_points; + current->tags = base->tags + base->n_points; + current->contours = base->contours + base->n_contours; + + /* handle extra points table - if any */ + if ( loader->use_extra ) { + loader->current.extra_points = + loader->base.extra_points + base->n_points; + } +} + + +BASE_FUNC( FT_Error ) FT_GlyphLoader_Create_Extra( + FT_GlyphLoader * loader ) +{ + FT_Error error; + FT_Memory memory = loader->memory; + + + if ( !ALLOC_ARRAY( loader->base.extra_points, + loader->max_points, FT_Vector ) ) { + loader->use_extra = 1; + FT_GlyphLoader_Adjust_Points( loader ); + } + return error; +} + + +/* re-adjust the `current' subglyphs field */ +static +void FT_GlyphLoader_Adjust_Subglyphs( FT_GlyphLoader* loader ) { + FT_GlyphLoad* base = &loader->base; + FT_GlyphLoad* current = &loader->current; + + + current->subglyphs = base->subglyphs + base->num_subglyphs; +} + + +/* Ensure that we can add `n_points' and `n_contours' to our glyph. this */ +/* function reallocates its outline tables if necessary. Note that it */ +/* DOESN'T change the number of points within the loader! */ +/* */ +BASE_FUNC( FT_Error ) FT_GlyphLoader_Check_Points( + FT_GlyphLoader * loader, + FT_UInt n_points, + FT_UInt n_contours ) +{ + FT_Memory memory = loader->memory; + FT_Error error = FT_Err_Ok; + FT_Outline* base = &loader->base.outline; + FT_Outline* current = &loader->current.outline; + FT_Bool adjust = 1; + + FT_UInt new_max; + + + /* check points & tags */ + new_max = base->n_points + current->n_points + n_points; + if ( new_max > loader->max_points ) { + new_max = ( new_max + 7 ) & - 8; + if ( REALLOC_ARRAY( base->points, base->n_points, + new_max, FT_Vector ) || + REALLOC_ARRAY( base->tags, base->n_points, + new_max, FT_Byte ) ) { + goto Exit; + } + + if ( loader->use_extra && + REALLOC_ARRAY( loader->base.extra_points, base->n_points, + new_max, FT_Vector ) ) { + goto Exit; + } + + adjust = 1; + loader->max_points = new_max; + } + + /* check contours */ + new_max = base->n_contours + current->n_contours + + n_contours; + if ( new_max > loader->max_contours ) { + new_max = ( new_max + 3 ) & - 4; + if ( REALLOC_ARRAY( base->contours, base->n_contours, + new_max, FT_Short ) ) { + goto Exit; + } + + adjust = 1; + loader->max_contours = new_max; + } + + if ( adjust ) { + FT_GlyphLoader_Adjust_Points( loader ); + } + +Exit: + return error; +} + + +/* Ensure that we can add `n_subglyphs' to our glyph. this function */ +/* reallocates its subglyphs table if necessary. Note that it DOES */ +/* NOT change the number of subglyphs within the loader! */ +/* */ +BASE_FUNC( FT_Error ) FT_GlyphLoader_Check_Subglyphs( + FT_GlyphLoader * loader, + FT_UInt n_subs ) +{ + FT_Memory memory = loader->memory; + FT_Error error = FT_Err_Ok; + FT_UInt new_max; + + FT_GlyphLoad* base = &loader->base; + FT_GlyphLoad* current = &loader->current; + + + new_max = base->num_subglyphs + current->num_subglyphs + n_subs; + if ( new_max > loader->max_subglyphs ) { + new_max = ( new_max + 1 ) & - 2; + if ( REALLOC_ARRAY( base->subglyphs, base->num_subglyphs, + new_max, FT_SubGlyph ) ) { + goto Exit; + } + + loader->max_subglyphs = new_max; + + FT_GlyphLoader_Adjust_Subglyphs( loader ); + } + +Exit: + return error; +} + + +/* prepare loader for the addition of a new glyph on top of the base one */ +BASE_FUNC( void ) FT_GlyphLoader_Prepare( FT_GlyphLoader * loader ) +{ + FT_GlyphLoad* current = &loader->current; + + + current->outline.n_points = 0; + current->outline.n_contours = 0; + current->num_subglyphs = 0; + + FT_GlyphLoader_Adjust_Points( loader ); + FT_GlyphLoader_Adjust_Subglyphs( loader ); +} + + +/* add current glyph to the base image - and prepare for another */ +BASE_FUNC( void ) FT_GlyphLoader_Add( FT_GlyphLoader * loader ) +{ + FT_GlyphLoad* base = &loader->base; + FT_GlyphLoad* current = &loader->current; + + FT_UInt n_curr_contours = current->outline.n_contours; + FT_UInt n_base_points = base->outline.n_points; + FT_UInt n; + + + base->outline.n_points += current->outline.n_points; + base->outline.n_contours += current->outline.n_contours; + base->num_subglyphs += current->num_subglyphs; + + /* adjust contours count in newest outline */ + for ( n = 0; n < n_curr_contours; n++ ) + current->outline.contours[n] += n_base_points; + + /* prepare for another new glyph image */ + FT_GlyphLoader_Prepare( loader ); +} + + +BASE_FUNC( FT_Error ) FT_GlyphLoader_Copy_Points( FT_GlyphLoader * target, + FT_GlyphLoader * source ) +{ + FT_Error error; + FT_UInt num_points = source->base.outline.n_points; + FT_UInt num_contours = source->base.outline.n_contours; + + + error = FT_GlyphLoader_Check_Points( target, num_points, num_contours ); + if ( !error ) { + FT_Outline* out = &target->base.outline; + FT_Outline* in = &source->base.outline; + + + MEM_Copy( out->points, in->points, + num_points * sizeof( FT_Vector ) ); + MEM_Copy( out->tags, in->tags, + num_points * sizeof( char ) ); + MEM_Copy( out->contours, in->contours, + num_contours * sizeof( short ) ); + + /* do we need to copy the extra points? */ + if ( target->use_extra && source->use_extra ) { + MEM_Copy( target->base.extra_points, source->base.extra_points, + num_points * sizeof( FT_Vector ) ); + } + + out->n_points = num_points; + out->n_contours = num_contours; + + FT_GlyphLoader_Adjust_Points( target ); + } + + return error; +} + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** ****/ +/**** FACE, SIZE & GLYPH SLOT OBJECTS ****/ +/**** ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +static +FT_Error ft_glyphslot_init( FT_GlyphSlot slot ) { + FT_Driver driver = slot->face->driver; + FT_Driver_Class* clazz = driver->clazz; + FT_Memory memory = driver->root.memory; + FT_Error error = FT_Err_Ok; + + + slot->library = driver->root.library; + + if ( FT_DRIVER_USES_OUTLINES( driver ) ) { + error = FT_GlyphLoader_New( memory, &slot->loader ); + } + + if ( !error && clazz->init_slot ) { + error = clazz->init_slot( slot ); + } + + return error; +} + + +static +void ft_glyphslot_clear( FT_GlyphSlot slot ) { + /* free bitmap if needed */ + if ( slot->flags & ft_glyph_own_bitmap ) { + FT_Memory memory = FT_FACE_MEMORY( slot->face ); + + + FREE( slot->bitmap.buffer ); + slot->flags &= ~ft_glyph_own_bitmap; + } + + /* clear all public fields in the glyph slot */ + MEM_Set( &slot->metrics, 0, sizeof( slot->metrics ) ); + MEM_Set( &slot->outline, 0, sizeof( slot->outline ) ); + MEM_Set( &slot->bitmap, 0, sizeof( slot->bitmap ) ); + + slot->bitmap_left = 0; + slot->bitmap_top = 0; + slot->num_subglyphs = 0; + slot->subglyphs = 0; + slot->control_data = 0; + slot->control_len = 0; + slot->other = 0; + slot->format = ft_glyph_format_none; + + slot->linearHoriAdvance = 0; + slot->linearVertAdvance = 0; +} + + +static +void ft_glyphslot_done( FT_GlyphSlot slot ) { + FT_Driver driver = slot->face->driver; + FT_Driver_Class* clazz = driver->clazz; + FT_Memory memory = driver->root.memory; + + + /* free bitmap buffer if needed */ + if ( slot->flags & ft_glyph_own_bitmap ) { + FREE( slot->bitmap.buffer ); + } + + /* free glyph loader */ + if ( FT_DRIVER_USES_OUTLINES( driver ) ) { + FT_GlyphLoader_Done( slot->loader ); + slot->loader = 0; + } + + if ( clazz->done_slot ) { + clazz->done_slot( slot ); + } +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_New_GlyphSlot */ +/* */ +/* */ +/* It is sometimes useful to have more than one glyph slot for a */ +/* given face object. This function is used to create additional */ +/* slots. All of them are automatically discarded when the face is */ +/* destroyed. */ +/* */ +/* */ +/* face :: A handle to a parent face object. */ +/* */ +/* */ +/* aslot :: A handle to a new glyph slot object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_New_GlyphSlot( FT_Face face, + FT_GlyphSlot * aslot ) +{ + FT_Error error; + FT_Driver driver; + FT_Driver_Class* clazz; + FT_Memory memory; + FT_GlyphSlot slot; + + + if ( !face || !aslot || !face->driver ) { + return FT_Err_Invalid_Argument; + } + + *aslot = 0; + + driver = face->driver; + clazz = driver->clazz; + memory = driver->root.memory; + + FT_TRACE4( ( "FT_New_GlyphSlot: Creating new slot object\n" ) ); + if ( !ALLOC( slot, clazz->slot_object_size ) ) { + slot->face = face; + + error = ft_glyphslot_init( slot ); + if ( error ) { + ft_glyphslot_done( slot ); + FREE( slot ); + goto Exit; + } + + *aslot = slot; + } + +Exit: + FT_TRACE4( ( "FT_New_GlyphSlot: Return %d\n", error ) ); + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Done_GlyphSlot */ +/* */ +/* */ +/* Destroys a given glyph slot. Remember however that all slots are */ +/* automatically destroyed with its parent. Using this function is */ +/* not always mandatory. */ +/* */ +/* */ +/* slot :: A handle to a target glyph slot. */ +/* */ +FT_EXPORT_FUNC( void ) FT_Done_GlyphSlot( FT_GlyphSlot slot ) +{ + if ( slot ) { + FT_Driver driver = slot->face->driver; + FT_Memory memory = driver->root.memory; + FT_GlyphSlot* parent; + FT_GlyphSlot cur; + + + /* Remove slot from its parent face's list */ + parent = &slot->face->glyph; + cur = *parent; + + while ( cur ) + { + if ( cur == slot ) { + *parent = cur->next; + ft_glyphslot_done( slot ); + FREE( slot ); + break; + } + cur = cur->next; + } + } +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Set_Transform */ +/* */ +/* */ +/* A function used to set the transformation that is applied to glyph */ +/* images just before they are converted to bitmaps in a glyph slot */ +/* when FT_Render_Glyph() is called. */ +/* */ +/* */ +/* face :: A handle to the source face object. */ +/* */ +/* */ +/* matrix :: A pointer to the transformation's 2x2 matrix. Use 0 for */ +/* the identity matrix. */ +/* delta :: A pointer to the translation vector. Use 0 for the null */ +/* vector. */ +/* */ +/* */ +/* The transformation is only applied to scalable image formats after */ +/* the glyph has been loaded. It means that hinting is unaltered by */ +/* the transformation and is performed on the character size given in */ +/* the last call to FT_Set_Char_Sizes() or FT_Set_Pixel_Sizes(). */ +/* */ +FT_EXPORT_FUNC( void ) FT_Set_Transform( FT_Face face, + FT_Matrix * matrix, + FT_Vector * delta ) +{ + if ( !face ) { + return; + } + + face->transform_flags = 0; + + if ( !matrix ) { + face->transform_matrix.xx = 0x10000L; + face->transform_matrix.xy = 0; + face->transform_matrix.yx = 0; + face->transform_matrix.yy = 0x10000L; + matrix = &face->transform_matrix; + } else { + face->transform_matrix = *matrix; + } + + /* set transform_flags bit flag 0 if `matrix' isn't the identity */ + if ( ( matrix->xy | matrix->yx ) || + matrix->xx != 0x10000L || + matrix->yy != 0x10000L ) { + face->transform_flags |= 1; + } + + if ( !delta ) { + face->transform_delta.x = 0; + face->transform_delta.y = 0; + delta = &face->transform_delta; + } else { + face->transform_delta = *delta; + } + + /* set transform_flags bit flag 1 if `delta' isn't the null vector */ + if ( delta->x | delta->y ) { + face->transform_flags |= 2; + } +} + + +static FT_Renderer ft_lookup_glyph_renderer( FT_GlyphSlot slot ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Load_Glyph */ +/* */ +/* */ +/* A function used to load a single glyph within a given glyph slot, */ +/* for a given size. */ +/* */ +/* */ +/* face :: A handle to the target face object where the glyph */ +/* will be loaded. */ +/* */ +/* glyph_index :: The index of the glyph in the font file. */ +/* */ +/* load_flags :: A flag indicating what to load for this glyph. The */ +/* FT_LOAD_XXX constants can be used to control the */ +/* glyph loading process (e.g., whether the outline */ +/* should be scaled, whether to load bitmaps or not, */ +/* whether to hint the outline, etc). */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* If the glyph image is not a bitmap, and if the bit flag */ +/* FT_LOAD_IGNORE_TRANSFORM is unset, the glyph image will be */ +/* transformed with the information passed to a previous call to */ +/* FT_Set_Transform. */ +/* */ +/* Note that this also transforms the `face.glyph.advance' field, but */ +/* *not* the values in `face.glyph.metrics'. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Load_Glyph( FT_Face face, + FT_UInt glyph_index, + FT_Int load_flags ) +{ + FT_Error error; + FT_Driver driver; + FT_GlyphSlot slot; + FT_Library library; + FT_Bool autohint; + FT_Module hinter; + + + if ( !face || !face->size || !face->glyph ) { + return FT_Err_Invalid_Face_Handle; + } + + if ( glyph_index >= (FT_UInt)face->num_glyphs ) { + return FT_Err_Invalid_Argument; + } + + slot = face->glyph; + ft_glyphslot_clear( slot ); + + driver = face->driver; + + /* when the flag NO_RECURSE is set, we disable hinting and scaling */ + if ( load_flags & FT_LOAD_NO_RECURSE ) { + load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING; + } + + /* do we need to load the glyph through the auto-hinter? */ + library = driver->root.library; + hinter = library->auto_hinter; + autohint = hinter && + !( load_flags & ( FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING ) ); + if ( autohint ) { + if ( FT_DRIVER_HAS_HINTER( driver ) && + !( load_flags & FT_LOAD_FORCE_AUTOHINT ) ) { + autohint = 0; + } + } + + if ( autohint ) { + FT_AutoHinter_Interface* hinting; + + + hinting = (FT_AutoHinter_Interface*)hinter->clazz->module_interface; + error = hinting->load_glyph( (FT_AutoHinter)hinter, slot, face->size, + glyph_index, load_flags ); + } else { + error = driver->clazz->load_glyph( slot, + face->size, + glyph_index, + load_flags ); + } + if ( error ) { + goto Exit; + } + + /* compute the advance */ + if ( load_flags & FT_LOAD_VERTICAL_LAYOUT ) { + slot->advance.x = 0; + slot->advance.y = slot->metrics.vertAdvance; + } else + { + slot->advance.x = slot->metrics.horiAdvance; + slot->advance.y = 0; + } + + /* now, transform the glyph image when needed */ + if ( face->transform_flags ) { + /* get renderer */ + FT_Renderer renderer = ft_lookup_glyph_renderer( slot ); + + + if ( renderer ) { + error = renderer->clazz->transform_glyph( renderer, slot, + &face->transform_matrix, + &face->transform_delta ); + } + /* transform advance */ + FT_Vector_Transform( &slot->advance, &face->transform_matrix ); + } + + /* do we need to render the image now? */ + if ( !error && + slot->format != ft_glyph_format_bitmap && + slot->format != ft_glyph_format_composite && + load_flags & FT_LOAD_RENDER ) { + error = FT_Render_Glyph( slot, + ( load_flags & FT_LOAD_MONOCHROME ) + ? ft_render_mode_mono + : ft_render_mode_normal ); + } + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Load_Char */ +/* */ +/* */ +/* A function used to load a single glyph within a given glyph slot, */ +/* for a given size, according to its character code. */ +/* */ +/* */ +/* face :: A handle to a target face object where the glyph */ +/* will be loaded. */ +/* */ +/* char_code :: The glyph's character code, according to the */ +/* current charmap used in the face. */ +/* */ +/* load_flags :: A flag indicating what to load for this glyph. The */ +/* FT_LOAD_XXX constants can be used to control the */ +/* glyph loading process (e.g., whether the outline */ +/* should be scaled, whether to load bitmaps or not, */ +/* whether to hint the outline, etc). */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* If the face has no current charmap, or if the character code */ +/* is not defined in the charmap, this function will return an */ +/* error. */ +/* */ +/* If the glyph image is not a bitmap, and if the bit flag */ +/* FT_LOAD_IGNORE_TRANSFORM is unset, the glyph image will be */ +/* transformed with the information passed to a previous call to */ +/* FT_Set_Transform(). */ +/* */ +/* Note that this also transforms the `face.glyph.advance' field, but */ +/* *not* the values in `face.glyph.metrics'. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Load_Char( FT_Face face, + FT_ULong char_code, + FT_Int load_flags ) +{ + FT_UInt glyph_index; + + + if ( !face ) { + return FT_Err_Invalid_Face_Handle; + } + + glyph_index = (FT_UInt)char_code; + if ( face->charmap ) { + glyph_index = FT_Get_Char_Index( face, char_code ); + } + + return FT_Load_Glyph( face, glyph_index, load_flags ); +} + + +/* destructor for sizes list */ +static +void destroy_size( FT_Memory memory, + FT_Size size, + FT_Driver driver ) { + /* finalize client-specific data */ + if ( size->generic.finalizer ) { + size->generic.finalizer( size ); + } + + /* finalize format-specific stuff */ + if ( driver->clazz->done_size ) { + driver->clazz->done_size( size ); + } + + FREE( size ); +} + + +/* destructor for faces list */ +static +void destroy_face( FT_Memory memory, + FT_Face face, + FT_Driver driver ) { + FT_Driver_Class* clazz = driver->clazz; + + + /* discard auto-hinting data */ + if ( face->autohint.finalizer ) { + face->autohint.finalizer( face->autohint.data ); + } + + /* Discard glyph slots for this face */ + /* Beware! FT_Done_GlyphSlot() changes the field `face->slot' */ + while ( face->glyph ) + FT_Done_GlyphSlot( face->glyph ); + + /* Discard all sizes for this face */ + FT_List_Finalize( &face->sizes_list, + (FT_List_Destructor)destroy_size, + memory, + driver ); + face->size = 0; + + /* Now discard client data */ + if ( face->generic.finalizer ) { + face->generic.finalizer( face ); + } + + /* finalize format-specific stuff */ + if ( clazz->done_face ) { + clazz->done_face( face ); + } + + /* close the stream for this face if needed */ + if ( ( face->face_flags & FT_FACE_FLAG_EXTERNAL_STREAM ) == 0 ) { + ft_done_stream( &face->stream ); + } + + /* get rid of it */ + FREE( face ); +} + + +static +void Destroy_Driver( FT_Driver driver ) { + FT_List_Finalize( &driver->faces_list, + (FT_List_Destructor)destroy_face, + driver->root.memory, + driver ); + + /* check whether we need to drop the driver's glyph loader */ + if ( FT_DRIVER_USES_OUTLINES( driver ) ) { + FT_GlyphLoader_Done( driver->glyph_loader ); + } +} + + +/*************************************************************************/ +/* */ +/* */ +/* open_face */ +/* */ +/* */ +/* This function does some work for FT_Open_Face(). */ +/* */ +static +FT_Error open_face( FT_Driver driver, + FT_Stream stream, + FT_Long face_index, + FT_Int num_params, + FT_Parameter* params, + FT_Face* aface ) { + FT_Memory memory; + FT_Driver_Class* clazz; + FT_Face face = 0; + FT_Error error; + + + clazz = driver->clazz; + memory = driver->root.memory; + + /* allocate the face object and perform basic initialization */ + if ( ALLOC( face, clazz->face_object_size ) ) { + goto Fail; + } + + face->driver = driver; + face->memory = memory; + face->stream = stream; + + error = clazz->init_face( stream, + face, + face_index, + num_params, + params ); + if ( error ) { + goto Fail; + } + + *aface = face; + +Fail: + if ( error ) { + clazz->done_face( face ); + FREE( face ); + *aface = 0; + } + + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_New_Face */ +/* */ +/* */ +/* Creates a new face object from a given resource and typeface index */ +/* using a pathname to the font file. */ +/* */ +/* */ +/* library :: A handle to the library resource. */ +/* */ +/* */ +/* pathname :: A path to the font file. */ +/* */ +/* face_index :: The index of the face within the resource. The */ +/* first face has index 0. */ +/* */ +/* aface :: A handle to a new face object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* Unlike FreeType 1.x, this function automatically creates a glyph */ +/* slot for the face object which can be accessed directly through */ +/* `face->glyph'. */ +/* */ +/* Note that additional slots can be added to each face with the */ +/* FT_New_GlyphSlot() API function. Slots are linked in a single */ +/* list through their `next' field. */ +/* */ +/* FT_New_Face() can be used to determine and/or check the font */ +/* format of a given font resource. If the `face_index' field is */ +/* negative, the function will _not_ return any face handle in */ +/* `*face'. Its return value should be 0 if the resource is */ +/* recognized, or non-zero if not. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_New_Face( FT_Library library, + const char* pathname, + FT_Long face_index, + FT_Face * aface ) +{ + FT_Open_Args args; + + + /* test for valid `library' and `aface' delayed to FT_Open_Face() */ + if ( !pathname ) { + return FT_Err_Invalid_Argument; + } + + args.flags = ft_open_pathname; + args.pathname = (char*)pathname; + + return FT_Open_Face( library, &args, face_index, aface ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_New_Memory_Face */ +/* */ +/* */ +/* Creates a new face object from a given resource and typeface index */ +/* using a font file already loaded into memory. */ +/* */ +/* */ +/* library :: A handle to the library resource. */ +/* */ +/* */ +/* file_base :: A pointer to the beginning of the font data. */ +/* */ +/* file_size :: The size of the memory chunk used by the font data. */ +/* */ +/* face_index :: The index of the face within the resource. The */ +/* first face has index 0. */ +/* */ +/* face :: A handle to a new face object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* Unlike FreeType 1.x, this function automatically creates a glyph */ +/* slot for the face object which can be accessed directly through */ +/* `face->glyph'. */ +/* */ +/* Note that additional slots can be added to each face with the */ +/* FT_New_GlyphSlot() API function. Slots are linked in a single */ +/* list through their `next' field. */ +/* */ +/* FT_New_Memory_Face() can be used to determine and/or check the */ +/* font format of a given font resource. If the `face_index' field */ +/* is negative, the function will _not_ return any face handle in */ +/* `*face'. Its return value should be 0 if the resource is */ +/* recognized, or non-zero if not. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_New_Memory_Face( FT_Library library, + FT_Byte * file_base, + FT_Long file_size, + FT_Long face_index, + FT_Face * face ) +{ + FT_Open_Args args; + + + /* test for valid `library' and `face' delayed to FT_Open_Face() */ + if ( !file_base ) { + return FT_Err_Invalid_Argument; + } + + args.flags = ft_open_memory; + args.memory_base = file_base; + args.memory_size = file_size; + + return FT_Open_Face( library, &args, face_index, face ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Open_Face */ +/* */ +/* */ +/* Opens a face object from a given resource and typeface index using */ +/* an `FT_Open_Args' structure. If the face object doesn't exist, it */ +/* will be created. */ +/* */ +/* */ +/* library :: A handle to the library resource. */ +/* */ +/* */ +/* args :: A pointer to an `FT_Open_Args' structure which must */ +/* be filled by the caller. */ +/* */ +/* face_index :: The index of the face within the resource. The */ +/* first face has index 0. */ +/* */ +/* aface :: A handle to a new face object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* Unlike FreeType 1.x, this function automatically creates a glyph */ +/* slot for the face object which can be accessed directly through */ +/* `face->glyph'. */ +/* */ +/* Note that additional slots can be added to each face with the */ +/* FT_New_GlyphSlot() API function. Slots are linked in a single */ +/* list through their `next' field. */ +/* */ +/* FT_Open_Face() can be used to determine and/or check the font */ +/* format of a given font resource. If the `face_index' field is */ +/* negative, the function will _not_ return any face handle in */ +/* `*face'. Its return value should be 0 if the resource is */ +/* recognized, or non-zero if not. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Open_Face( FT_Library library, + FT_Open_Args * args, + FT_Long face_index, + FT_Face * aface ) +{ + FT_Error error; + FT_Driver driver; + FT_Memory memory; + FT_Stream stream; + FT_Face face = 0; + FT_ListNode node = 0; + + + /* test for valid `library' and `args' delayed to */ + /* ft_new_input_stream() */ + + if ( !aface ) { + return FT_Err_Invalid_Argument; + } + + *aface = 0; + + /* create input stream */ + error = ft_new_input_stream( library, args, &stream ); + if ( error ) { + goto Exit; + } + + memory = library->memory; + + /* If the font driver is specified in the `args' structure, use */ + /* it. Otherwise, we scan the list of registered drivers. */ + if ( args->flags & ft_open_driver && args->driver ) { + driver = FT_DRIVER( args->driver ); + + /* not all modules are drivers, so check... */ + if ( FT_MODULE_IS_DRIVER( driver ) ) { + FT_Int num_params = 0; + FT_Parameter* params = 0; + + + if ( args->flags & ft_open_params ) { + num_params = args->num_params; + params = args->params; + } + + error = open_face( driver, stream, face_index, + num_params, params, &face ); + if ( !error ) { + goto Success; + } + } else { + error = FT_Err_Invalid_Handle; + } + + ft_done_stream( &stream ); + goto Fail; + } else + { + /* check each font driver for an appropriate format */ + FT_Module* cur = library->modules; + FT_Module* limit = cur + library->num_modules; + + + for ( ; cur < limit; cur++ ) + { + /* not all modules are font drivers, so check... */ + if ( FT_MODULE_IS_DRIVER( cur[0] ) ) { + FT_Int num_params = 0; + FT_Parameter* params = 0; + + + driver = FT_DRIVER( cur[0] ); + + if ( args->flags & ft_open_params ) { + num_params = args->num_params; + params = args->params; + } + + error = open_face( driver, stream, face_index, + num_params, params, &face ); + if ( !error ) { + goto Success; + } + + if ( error != FT_Err_Unknown_File_Format ) { + goto Fail; + } + } + } + + ft_done_stream( &stream ); + + /* no driver is able to handle this format */ + error = FT_Err_Unknown_File_Format; + goto Fail; + } + +Success: + FT_TRACE4( ( "FT_New_Face: New face object, adding to list\n" ) ); + + /* set the FT_FACE_FLAG_EXTERNAL_STREAM bit for FT_Done_Face */ + if ( args->flags & ft_open_stream && args->stream ) { + face->face_flags |= FT_FACE_FLAG_EXTERNAL_STREAM; + } + + /* add the face object to its driver's list */ + if ( ALLOC( node, sizeof( *node ) ) ) { + goto Fail; + } + + node->data = face; + /* don't assume driver is the same as face->driver, so use */ + /* face->driver instead. */ + FT_List_Add( &face->driver->faces_list, node ); + + /* now allocate a glyph slot object for the face */ + { + FT_GlyphSlot slot; + + + FT_TRACE4( ( "FT_Open_Face: Creating glyph slot\n" ) ); + + error = FT_New_GlyphSlot( face, &slot ); + if ( error ) { + goto Fail; + } + + face->glyph = slot; + } + + /* finally, allocate a size object for the face */ + { + FT_Size size; + + + FT_TRACE4( ( "FT_Open_Face: Creating size object\n" ) ); + + error = FT_New_Size( face, &size ); + if ( error ) { + goto Fail; + } + + face->size = size; + } + + /* initialize transformation for convenience functions */ + face->transform_matrix.xx = 0x10000L; + face->transform_matrix.xy = 0; + face->transform_matrix.yx = 0; + face->transform_matrix.yy = 0x10000L; + + face->transform_delta.x = 0; + face->transform_delta.y = 0; + + *aface = face; + goto Exit; + +Fail: + FT_Done_Face( face ); + +Exit: + FT_TRACE4( ( "FT_Open_Face: Return %d\n", error ) ); + + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Attach_File */ +/* */ +/* */ +/* `Attaches' a given font file to an existing face. This is usually */ +/* to read additional information for a single face object. For */ +/* example, it is used to read the AFM files that come with Type 1 */ +/* fonts in order to add kerning data and other metrics. */ +/* */ +/* */ +/* face :: The target face object. */ +/* */ +/* */ +/* filepathname :: An 8-bit pathname naming the `metrics' file. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* If your font file is in memory, or if you want to provide your */ +/* own input stream object, use FT_Attach_Stream(). */ +/* */ +/* The meaning of the `attach' action (i.e., what really happens when */ +/* the new file is read) is not fixed by FreeType itself. It really */ +/* depends on the font format (and thus the font driver). */ +/* */ +/* Client applications are expected to know what they are doing */ +/* when invoking this function. Most drivers simply do not implement */ +/* file attachments. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Attach_File( FT_Face face, + const char* filepathname ) +{ + FT_Open_Args open; + + + /* test for valid `face' delayed to FT_Attach_Stream() */ + + if ( !filepathname ) { + return FT_Err_Invalid_Argument; + } + + open.flags = ft_open_pathname; + open.pathname = (char*)filepathname; + + return FT_Attach_Stream( face, &open ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Attach_Stream */ +/* */ +/* */ +/* This function is similar to FT_Attach_File() with the exception */ +/* that it reads the attachment from an arbitrary stream. */ +/* */ +/* */ +/* face :: The target face object. */ +/* */ +/* parameters :: A pointer to an FT_Open_Args structure used to */ +/* describe the input stream to FreeType. */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* The meaning of the `attach' (i.e. what really happens when the */ +/* new file is read) is not fixed by FreeType itself. It really */ +/* depends on the font format (and thus the font driver). */ +/* */ +/* Client applications are expected to know what they are doing */ +/* when invoking this function. Most drivers simply do not implement */ +/* file attachments. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Attach_Stream( FT_Face face, + FT_Open_Args * parameters ) +{ + FT_Stream stream; + FT_Error error; + FT_Driver driver; + + FT_Driver_Class* clazz; + + + /* test for valid `parameters' delayed to ft_new_input_stream() */ + + if ( !face ) { + return FT_Err_Invalid_Face_Handle; + } + + driver = face->driver; + if ( !driver ) { + return FT_Err_Invalid_Driver_Handle; + } + + error = ft_new_input_stream( driver->root.library, parameters, &stream ); + if ( error ) { + goto Exit; + } + + /* we implement FT_Attach_Stream in each driver through the */ + /* `attach_file' interface */ + + error = FT_Err_Unimplemented_Feature; + clazz = driver->clazz; + if ( clazz->attach_file ) { + error = clazz->attach_file( face, stream ); + } + + /* close the attached stream */ + if ( !parameters->stream || ( parameters->flags & ft_open_stream ) ) { + ft_done_stream( &stream ); + } + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Done_Face */ +/* */ +/* */ +/* Discards a given face object, as well as all of its child slots */ +/* and sizes. */ +/* */ +/* */ +/* face :: A handle to a target face object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Done_Face( FT_Face face ) +{ + FT_Error error; + FT_Driver driver; + FT_Memory memory; + FT_ListNode node; + + + error = FT_Err_Invalid_Face_Handle; + if ( face && face->driver ) { + driver = face->driver; + memory = driver->root.memory; + + /* find face in driver's list */ + node = FT_List_Find( &driver->faces_list, face ); + if ( node ) { + /* remove face object from the driver's list */ + FT_List_Remove( &driver->faces_list, node ); + FREE( node ); + + /* now destroy the object proper */ + destroy_face( memory, face, driver ); + error = FT_Err_Ok; + } + } + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_New_Size */ +/* */ +/* */ +/* Creates a new size object from a given face object. */ +/* */ +/* */ +/* face :: A handle to a parent face object. */ +/* */ +/* */ +/* asize :: A handle to a new size object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_New_Size( FT_Face face, + FT_Size * asize ) +{ + FT_Error error; + FT_Memory memory; + FT_Driver driver; + FT_Driver_Class* clazz; + + FT_Size size = 0; + FT_ListNode node = 0; + + + if ( !face ) { + return FT_Err_Invalid_Face_Handle; + } + + if ( !asize ) { + return FT_Err_Invalid_Size_Handle; + } + + if ( !face->driver ) { + return FT_Err_Invalid_Driver_Handle; + } + + *asize = 0; + + driver = face->driver; + clazz = driver->clazz; + memory = face->memory; + + /* Allocate new size object and perform basic initialisation */ + if ( ALLOC( size, clazz->size_object_size ) || + ALLOC( node, sizeof( FT_ListNodeRec ) ) ) { + goto Exit; + } + + size->face = face; + + if ( clazz->init_size ) { + error = clazz->init_size( size ); + } + + /* in case of success, add to the face's list */ + if ( !error ) { + *asize = size; + node->data = size; + FT_List_Add( &face->sizes_list, node ); + } + +Exit: + if ( error ) { + FREE( node ); + FREE( size ); + } + + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Done_Size */ +/* */ +/* */ +/* Discards a given size object. */ +/* */ +/* */ +/* size :: A handle to a target size object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Done_Size( FT_Size size ) +{ + FT_Error error; + FT_Driver driver; + FT_Memory memory; + FT_Face face; + FT_ListNode node; + + + if ( !size ) { + return FT_Err_Invalid_Size_Handle; + } + + face = size->face; + if ( !face ) { + return FT_Err_Invalid_Face_Handle; + } + + driver = face->driver; + if ( !driver ) { + return FT_Err_Invalid_Driver_Handle; + } + + memory = driver->root.memory; + + error = FT_Err_Ok; + node = FT_List_Find( &face->sizes_list, size ); + if ( node ) { + FT_List_Remove( &face->sizes_list, node ); + FREE( node ); + + if ( face->size == size ) { + face->size = 0; + if ( face->sizes_list.head ) { + face->size = (FT_Size)( face->sizes_list.head->data ); + } + } + + destroy_size( memory, size, driver ); + } else { + error = FT_Err_Invalid_Size_Handle; + } + + return FT_Err_Ok; +} + + +static +void ft_recompute_scaled_metrics( FT_Face face, + FT_Size_Metrics* metrics ) { + /* Compute root ascender, descender, test height, and max_advance */ + + metrics->ascender = ( FT_MulFix( face->ascender, + metrics->y_scale ) + 32 ) & - 64; + + metrics->descender = ( FT_MulFix( face->descender, + metrics->y_scale ) + 32 ) & - 64; + + metrics->height = ( FT_MulFix( face->height, + metrics->y_scale ) + 32 ) & - 64; + + metrics->max_advance = ( FT_MulFix( face->max_advance_width, + metrics->x_scale ) + 32 ) & - 64; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Set_Char_Size */ +/* */ +/* */ +/* Sets the character dimensions of a given face object. The */ +/* `char_width' and `char_height' values are used for the width and */ +/* height, respectively, expressed in 26.6 fractional points. */ +/* */ +/* If the horizontal or vertical resolution values are zero, a */ +/* default value of 72dpi is used. Similarly, if one of the */ +/* character dimensions is zero, its value is set equal to the other. */ +/* */ +/* */ +/* size :: A handle to a target size object. */ +/* */ +/* */ +/* char_width :: The character width, in 26.6 fractional points. */ +/* */ +/* char_height :: The character height, in 26.6 fractional */ +/* points. */ +/* */ +/* horz_resolution :: The horizontal resolution. */ +/* */ +/* vert_resolution :: The vertical resolution. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* When dealing with fixed-size faces (i.e., non-scalable formats), */ +/* use the function FT_Set_Pixel_Sizes(). */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Set_Char_Size( FT_Face face, + FT_F26Dot6 char_width, + FT_F26Dot6 char_height, + FT_UInt horz_resolution, + FT_UInt vert_resolution ) +{ + FT_Error error = FT_Err_Ok; + FT_Driver driver; + FT_Memory memory; + FT_Driver_Class* clazz; + FT_Size_Metrics* metrics; + FT_Long dim_x, dim_y; + + + if ( !face || !face->size || !face->driver ) { + return FT_Err_Invalid_Face_Handle; + } + + driver = face->driver; + metrics = &face->size->metrics; + + if ( !char_width ) { + char_width = char_height; + } else if ( !char_height ) { + char_height = char_width; + } + + if ( !horz_resolution ) { + horz_resolution = 72; + } + + if ( !vert_resolution ) { + vert_resolution = 72; + } + + driver = face->driver; + clazz = driver->clazz; + memory = driver->root.memory; + + /* default processing -- this can be overridden by the driver */ + if ( char_width < 1 * 64 ) { + char_width = 1 * 64; + } + if ( char_height < 1 * 64 ) { + char_height = 1 * 64; + } + + /* Compute pixel sizes in 26.6 units */ + dim_x = ( ( ( char_width * horz_resolution ) / 72 ) + 32 ) & - 64; + dim_y = ( ( ( char_height * vert_resolution ) / 72 ) + 32 ) & - 64; + + metrics->x_ppem = (FT_UShort)( dim_x >> 6 ); + metrics->y_ppem = (FT_UShort)( dim_y >> 6 ); + + metrics->x_scale = 0x10000L; + metrics->y_scale = 0x10000L; + + if ( face->face_flags & FT_FACE_FLAG_SCALABLE ) { + metrics->x_scale = FT_DivFix( dim_x, face->units_per_EM ); + metrics->y_scale = FT_DivFix( dim_y, face->units_per_EM ); + + ft_recompute_scaled_metrics( face, metrics ); + } + + if ( clazz->set_char_sizes ) { + error = clazz->set_char_sizes( face->size, + char_width, + char_height, + horz_resolution, + vert_resolution ); + } + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Set_Pixel_Sizes */ +/* */ +/* */ +/* Sets the character dimensions of a given face object. The width */ +/* and height are expressed in integer pixels. */ +/* */ +/* If one of the character dimensions is zero, its value is set equal */ +/* to the other. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* */ +/* */ +/* pixel_width :: The character width, in integer pixels. */ +/* */ +/* pixel_height :: The character height, in integer pixels. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Set_Pixel_Sizes( FT_Face face, + FT_UInt pixel_width, + FT_UInt pixel_height ) +{ + FT_Error error = FT_Err_Ok; + FT_Driver driver; + FT_Memory memory; + FT_Driver_Class* clazz; + FT_Size_Metrics* metrics = &face->size->metrics; + + + if ( !face || !face->size || !face->driver ) { + return FT_Err_Invalid_Face_Handle; + } + + driver = face->driver; + clazz = driver->clazz; + memory = driver->root.memory; + + /* default processing -- this can be overridden by the driver */ + if ( pixel_width == 0 ) { + pixel_width = pixel_height; + } else if ( pixel_height == 0 ) { + pixel_height = pixel_width; + } + + if ( pixel_width < 1 ) { + pixel_width = 1; + } + if ( pixel_height < 1 ) { + pixel_height = 1; + } + + metrics->x_ppem = pixel_width; + metrics->y_ppem = pixel_height; + + if ( face->face_flags & FT_FACE_FLAG_SCALABLE ) { + metrics->x_scale = FT_DivFix( metrics->x_ppem << 6, + face->units_per_EM ); + + metrics->y_scale = FT_DivFix( metrics->y_ppem << 6, + face->units_per_EM ); + + ft_recompute_scaled_metrics( face, metrics ); + } + + if ( clazz->set_pixel_sizes ) { + error = clazz->set_pixel_sizes( face->size, + pixel_width, + pixel_height ); + } + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Get_Kerning */ +/* */ +/* */ +/* Returns the kerning vector between two glyphs of a same face. */ +/* */ +/* */ +/* face :: A handle to a source face object. */ +/* */ +/* left_glyph :: The index of the left glyph in the kern pair. */ +/* */ +/* right_glyph :: The index of the right glyph in the kern pair. */ +/* */ +/* kern_mode :: See FT_Kerning_Mode() for more information. */ +/* Determines the scale/dimension of the returned */ +/* kerning vector. */ +/* */ +/* */ +/* kerning :: The kerning vector. This is in font units for */ +/* scalable formats, and in pixels for fixed-sizes */ +/* formats. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* Only horizontal layouts (left-to-right & right-to-left) are */ +/* supported by this method. Other layouts, or more sophisticated */ +/* kernings, are out of the scope of this API function -- they can be */ +/* implemented through format-specific interfaces. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Get_Kerning( FT_Face face, + FT_UInt left_glyph, + FT_UInt right_glyph, + FT_UInt kern_mode, + FT_Vector * kerning ) +{ + FT_Error error = FT_Err_Ok; + FT_Driver driver; + FT_Memory memory; + + + if ( !face ) { + return FT_Err_Invalid_Face_Handle; + } + + if ( !kerning ) { + return FT_Err_Invalid_Argument; + } + + driver = face->driver; + memory = driver->root.memory; + + kerning->x = 0; + kerning->y = 0; + + if ( driver->clazz->get_kerning ) { + error = driver->clazz->get_kerning( face, + left_glyph, + right_glyph, + kerning ); + if ( !error ) { + if ( kern_mode != ft_kerning_unscaled ) { + kerning->x = FT_MulFix( kerning->x, face->size->metrics.x_scale ); + kerning->y = FT_MulFix( kerning->y, face->size->metrics.y_scale ); + + if ( kern_mode != ft_kerning_unfitted ) { + kerning->x = ( kerning->x + 32 ) & - 64; + kerning->y = ( kerning->y + 32 ) & - 64; + } + } + } + } + + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Select_Charmap */ +/* */ +/* */ +/* Selects a given charmap by its encoding tag (as listed in */ +/* `freetype.h'). */ +/* */ +/* */ +/* face :: A handle to the source face object. */ +/* */ +/* encoding :: A handle to the selected charmap. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* This function will return an error if no charmap in the face */ +/* corresponds to the encoding queried here. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Select_Charmap( FT_Face face, + FT_Encoding encoding ) +{ + FT_CharMap* cur; + FT_CharMap* limit; + + + if ( !face ) { + return FT_Err_Invalid_Face_Handle; + } + + cur = face->charmaps; + if ( !cur ) { + return FT_Err_Invalid_CharMap_Handle; + } + + limit = cur + face->num_charmaps; + + for ( ; cur < limit; cur++ ) + { + if ( cur[0]->encoding == encoding ) { + face->charmap = cur[0]; + return 0; + } + } + + return FT_Err_Invalid_Argument; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Set_Charmap */ +/* */ +/* */ +/* Selects a given charmap for character code to glyph index */ +/* decoding. */ +/* */ +/* */ +/* face :: A handle to the source face object. */ +/* charmap :: A handle to the selected charmap. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* This function will return an error if the charmap is not part of */ +/* the face (i.e., if it is not listed in the face->charmaps[] */ +/* table). */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Set_Charmap( FT_Face face, + FT_CharMap charmap ) +{ + FT_CharMap* cur; + FT_CharMap* limit; + + + if ( !face ) { + return FT_Err_Invalid_Face_Handle; + } + + cur = face->charmaps; + if ( !cur ) { + return FT_Err_Invalid_CharMap_Handle; + } + + limit = cur + face->num_charmaps; + + for ( ; cur < limit; cur++ ) + { + if ( cur[0] == charmap ) { + face->charmap = cur[0]; + return 0; + } + } + return FT_Err_Invalid_Argument; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Get_Char_Index */ +/* */ +/* */ +/* Returns the glyph index of a given character code. This function */ +/* uses a charmap object to do the translation. */ +/* */ +/* */ +/* face :: A handle to the source face object. */ +/* */ +/* charcode :: The character code. */ +/* */ +/* */ +/* The glyph index. 0 means `undefined character code'. */ +/* */ +FT_EXPORT_FUNC( FT_UInt ) FT_Get_Char_Index( FT_Face face, + FT_ULong charcode ) +{ + FT_UInt result; + FT_Driver driver; + + + result = 0; + if ( face && face->charmap ) { + driver = face->driver; + result = driver->clazz->get_char_index( face->charmap, charcode ); + } + return result; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Get_Glyph_Name */ +/* */ +/* */ +/* Retrieves the ASCII name of a given glyph in a face. This only */ +/* works for those faces where FT_HAS_GLYPH_NAME(face) returns true. */ +/* */ +/* */ +/* face :: A handle to a source face object. */ +/* */ +/* glyph_index :: The glyph index. */ +/* */ +/* buffer :: A pointer to a target buffer where the name will be */ +/* copied to. */ +/* */ +/* buffer_max :: The maximal number of bytes available in the */ +/* buffer. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* An error is returned if the face doesn't provide glyph names or if */ +/* the glyph index is invalid. In all cases of failure, the first */ +/* byte of `buffer' will be set to 0 to indicate an empty name. */ +/* */ +/* The glyph name is truncated to fit within the buffer if it is too */ +/* long. The returned string is always zero-terminated. */ +/* */ +/* This function is not compiled within the library if the config */ +/* macro FT_CONFIG_OPTION_NO_GLYPH_NAMES is defined in */ +/* `include/freetype/config/ftoptions.h' */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Get_Glyph_Name( FT_Face face, + FT_UInt glyph_index, + FT_Pointer buffer, + FT_UInt buffer_max ) +{ + FT_Error error = FT_Err_Invalid_Argument; + + + /* clean up buffer */ + if ( buffer && buffer_max > 0 ) { + ( (FT_Byte*)buffer )[0] = 0; + } + + if ( face && + glyph_index < (FT_UInt)face->num_glyphs && + FT_HAS_GLYPH_NAMES( face ) ) { + /* now, lookup for glyph name */ + FT_Driver driver = face->driver; + FT_Module_Class* clazz = FT_MODULE_CLASS( driver ); + + + if ( clazz->get_interface ) { + FT_Glyph_Name_Requester requester; + + + requester = (FT_Glyph_Name_Requester)clazz->get_interface( + FT_MODULE( driver ), "glyph_name" ); + if ( requester ) { + error = requester( face, glyph_index, buffer, buffer_max ); + } + } + } + + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Get_Sfnt_Table */ +/* */ +/* */ +/* Returns a pointer to a given SFNT table within a face. */ +/* */ +/* */ +/* face :: A handle to the source face object. */ +/* tag :: An index of an SFNT table. */ +/* */ +/* */ +/* A type-less pointer to the table. This will be 0 in case of */ +/* error, or if the corresponding table was not found *OR* loaded */ +/* from the file. */ +/* */ +/* */ +/* The table is owned by the face object, and disappears with it. */ +/* */ +/* This function is only useful to access SFNT tables that are loaded */ +/* by the sfnt/truetype/opentype drivers. See the FT_Sfnt_Tag */ +/* enumeration in `tttables.h' for a list. */ +/* */ +/* You can load any table with a different function.. XXX */ +/* */ +FT_EXPORT_FUNC( void* ) FT_Get_Sfnt_Table( FT_Face face, + FT_Sfnt_Tag tag ) +{ + void* table = 0; + FT_Get_Sfnt_Table_Func func; + FT_Driver driver; + + + if ( !face || !FT_IS_SFNT( face ) ) { + goto Exit; + } + + driver = face->driver; + func = (FT_Get_Sfnt_Table_Func)driver->root.clazz->get_interface( + FT_MODULE( driver ), "get_sfnt" ); + if ( func ) { + table = func( face, tag ); + } + +Exit: + return table; +} + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** ****/ +/**** R E N D E R E R S ****/ +/**** ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + +/* lookup a renderer by glyph format in the library's list */ +BASE_FUNC( FT_Renderer ) FT_Lookup_Renderer( FT_Library library, + FT_Glyph_Format format, + FT_ListNode * node ) +{ + FT_ListNode cur; + FT_Renderer result = 0; + + + if ( !library ) { + goto Exit; + } + + cur = library->renderers.head; + + if ( node ) { + if ( *node ) { + cur = ( *node )->next; + } + *node = 0; + } + + while ( cur ) + { + FT_Renderer renderer = FT_RENDERER( cur->data ); + + + if ( renderer->glyph_format == format ) { + if ( node ) { + *node = cur; + } + + result = renderer; + break; + } + cur = cur->next; + } + +Exit: + return result; +} + + +static +FT_Renderer ft_lookup_glyph_renderer( FT_GlyphSlot slot ) { + FT_Face face = slot->face; + FT_Library library = FT_FACE_LIBRARY( face ); + FT_Renderer result = library->cur_renderer; + + + if ( !result || result->glyph_format != slot->format ) { + result = FT_Lookup_Renderer( library, slot->format, 0 ); + } + + return result; +} + + +static +void ft_set_current_renderer( FT_Library library ) { + FT_Renderer renderer; + + + renderer = FT_Lookup_Renderer( library, ft_glyph_format_outline, 0 ); + library->cur_renderer = renderer; +} + + +static +FT_Error ft_add_renderer( FT_Module module ) { + FT_Library library = module->library; + FT_Memory memory = library->memory; + FT_Error error; + FT_ListNode node; + + + if ( ALLOC( node, sizeof( *node ) ) ) { + goto Exit; + } + + { + FT_Renderer render = FT_RENDERER( module ); + FT_Renderer_Class* clazz = (FT_Renderer_Class*)module->clazz; + + + render->clazz = clazz; + render->glyph_format = clazz->glyph_format; + + /* allocate raster object if needed */ + if ( clazz->glyph_format == ft_glyph_format_outline && + clazz->raster_class->raster_new ) { + error = clazz->raster_class->raster_new( memory, &render->raster ); + if ( error ) { + goto Fail; + } + + render->raster_render = clazz->raster_class->raster_render; + render->render = clazz->render_glyph; + } + + /* add to list */ + node->data = module; + FT_List_Add( &library->renderers, node ); + + ft_set_current_renderer( library ); + } + +Fail: + if ( error ) { + FREE( node ); + } + +Exit: + return error; +} + + +static +void ft_remove_renderer( FT_Module module ) { + FT_Library library = module->library; + FT_Memory memory = library->memory; + FT_ListNode node; + + + node = FT_List_Find( &library->renderers, module ); + if ( node ) { + FT_Renderer render = FT_RENDERER( module ); + + + /* release raster object, if any */ + if ( render->raster ) { + render->clazz->raster_class->raster_done( render->raster ); + } + + /* remove from list */ + FT_List_Remove( &library->renderers, node ); + FREE( node ); + + ft_set_current_renderer( library ); + } +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Get_Renderer */ +/* */ +/* */ +/* Retrieves the current renderer for a given glyph format. */ +/* */ +/* */ +/* library :: A handle to the library object. */ +/* */ +/* format :: The glyph format. */ +/* */ +/* */ +/* A renderer handle. 0 if none found. */ +/* */ +/* */ +/* An error will be returned if a module already exists by that name, */ +/* or if the module requires a version of FreeType that is too great. */ +/* */ +/* To add a new renderer, simply use FT_Add_Module(). To retrieve a */ +/* renderer by its name, use FT_Get_Module(). */ +/* */ +FT_EXPORT_FUNC( FT_Renderer ) FT_Get_Renderer( FT_Library library, + FT_Glyph_Format format ) +{ + /* test for valid `library' delayed to FT_Lookup_Renderer() */ + + return FT_Lookup_Renderer( library, format, 0 ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Set_Renderer */ +/* */ +/* */ +/* Sets the current renderer to use, and set additional mode. */ +/* */ +/* */ +/* library :: A handle to the library object. */ +/* */ +/* renderer :: A handle to the renderer object. */ +/* */ +/* num_params :: The number of additional parameters. */ +/* */ +/* parameters :: Additional parameters. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* In case of success, the renderer will be used to convert glyph */ +/* images in the renderer's known format into bitmaps. */ +/* */ +/* This doesn't change the current renderer for other formats. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Set_Renderer( FT_Library library, + FT_Renderer renderer, + FT_UInt num_params, + FT_Parameter * parameters ) +{ + FT_ListNode node; + FT_Error error = FT_Err_Ok; + + + if ( !library ) { + return FT_Err_Invalid_Library_Handle; + } + + if ( !renderer ) { + return FT_Err_Invalid_Argument; + } + + node = FT_List_Find( &library->renderers, renderer ); + if ( !node ) { + error = FT_Err_Invalid_Argument; + goto Exit; + } + + FT_List_Up( &library->renderers, node ); + + if ( renderer->glyph_format == ft_glyph_format_outline ) { + library->cur_renderer = renderer; + } + + if ( num_params > 0 ) { + FTRenderer_setMode set_mode = renderer->clazz->set_mode; + + + for ( ; num_params > 0; num_params-- ) + { + error = set_mode( renderer, parameters->tag, parameters->data ); + if ( error ) { + break; + } + } + } + +Exit: + return error; +} + + +LOCAL_FUNC +FT_Error FT_Render_Glyph_Internal( FT_Library library, + FT_GlyphSlot slot, + FT_UInt render_mode ) { + FT_Error error = FT_Err_Ok; + FT_Renderer renderer; + + + /* if it is already a bitmap, no need to do anything */ + switch ( slot->format ) + { + case ft_glyph_format_bitmap: /* already a bitmap, don't do anything */ + break; + + default: + { + FT_ListNode node = 0; + FT_Bool update = 0; + + + /* small shortcut for the very common case */ + if ( slot->format == ft_glyph_format_outline ) { + renderer = library->cur_renderer; + node = library->renderers.head; + } else { + renderer = FT_Lookup_Renderer( library, slot->format, &node ); + } + + error = FT_Err_Unimplemented_Feature; + while ( renderer ) + { + error = renderer->render( renderer, slot, render_mode, 0 ); + if ( !error || error != FT_Err_Cannot_Render_Glyph ) { + break; + } + + /* FT_Err_Cannot_Render_Glyph is returned if the render mode */ + /* is unsupported by the current renderer for this glyph image */ + /* format. */ + + /* now, look for another renderer that supports the same */ + /* format. */ + renderer = FT_Lookup_Renderer( library, slot->format, &node ); + update = 1; + } + + /* if we changed the current renderer for the glyph image format */ + /* we need to select it as the next current one */ + if ( !error && update && renderer ) { + FT_Set_Renderer( library, renderer, 0, 0 ); + } + } + } + + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Render_Glyph */ +/* */ +/* */ +/* Converts a given glyph image to a bitmap. It does so by */ +/* inspecting the glyph image format, find the relevant renderer, and */ +/* invoke it. */ +/* */ +/* */ +/* slot :: A handle to the glyph slot containing the image to */ +/* convert. */ +/* */ +/* render_mode :: This is the render mode used to render the glyph */ +/* image into a bitmap. See FT_Render_Mode for a list */ +/* of possible values. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Render_Glyph( FT_GlyphSlot slot, + FT_UInt render_mode ) +{ + FT_Library library; + + + if ( !slot ) { + return FT_Err_Invalid_Argument; + } + + library = FT_FACE_LIBRARY( slot->face ); + + return FT_Render_Glyph_Internal( library, slot, render_mode ); +} + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** ****/ +/**** M O D U L E S ****/ +/**** ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* Destroy_Module */ +/* */ +/* */ +/* Destroys a given module object. For drivers, this also destroys */ +/* all child faces. */ +/* */ +/* */ +/* module :: A handle to the target driver object. */ +/* */ +/* */ +/* The driver _must_ be LOCKED! */ +/* */ +static +void Destroy_Module( FT_Module module ) { + FT_Memory memory = module->memory; + FT_Module_Class* clazz = module->clazz; + FT_Library library = module->library; + + + /* finalize client-data - before anything else */ + if ( module->generic.finalizer ) { + module->generic.finalizer( module ); + } + + if ( library && library->auto_hinter == module ) { + library->auto_hinter = 0; + } + + /* if the module is a renderer */ + if ( FT_MODULE_IS_RENDERER( module ) ) { + ft_remove_renderer( module ); + } + + /* if the module is a font driver, add some steps */ + if ( FT_MODULE_IS_DRIVER( module ) ) { + Destroy_Driver( FT_DRIVER( module ) ); + } + + /* finalize the module object */ + if ( clazz->module_done ) { + clazz->module_done( module ); + } + + /* discard it */ + FREE( module ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Add_Module */ +/* */ +/* */ +/* Adds a new module to a given library instance. */ +/* */ +/* */ +/* library :: A handle to the library object. */ +/* */ +/* clazz :: A pointer to class descriptor for the module. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* An error will be returned if a module already exists by that name, */ +/* or if the module requires a version of FreeType that is too great. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Add_Module( FT_Library library, + const FT_Module_Class * clazz ) +{ + FT_Error error; + FT_Memory memory; + FT_Module module; + FT_UInt nn; + + +#define FREETYPE_VER_FIXED ( ( (FT_Long)FREETYPE_MAJOR << 16 ) | \ + FREETYPE_MINOR ) + + if ( !library ) { + return FT_Err_Invalid_Library_Handle; + } + + if ( !clazz ) { + return FT_Err_Invalid_Argument; + } + + /* check freetype version */ + if ( clazz->module_requires > FREETYPE_VER_FIXED ) { + return FT_Err_Invalid_Version; + } + + /* look for a module with the same name in the library's table */ + for ( nn = 0; nn < library->num_modules; nn++ ) + { + module = library->modules[nn]; + if ( strcmp( module->clazz->module_name, clazz->module_name ) == 0 ) { + /* this installed module has the same name, compare their versions */ + if ( clazz->module_version <= module->clazz->module_version ) { + return FT_Err_Lower_Module_Version; + } + + /* remove the module from our list, then exit the loop to replace */ + /* it by our new version.. */ + FT_Remove_Module( library, module ); + break; + } + } + + memory = library->memory; + error = FT_Err_Ok; + + if ( library->num_modules >= FT_MAX_MODULES ) { + error = FT_Err_Too_Many_Drivers; + goto Exit; + } + + /* allocate module object */ + if ( ALLOC( module,clazz->module_size ) ) { + goto Exit; + } + + /* base initialization */ + module->library = library; + module->memory = memory; + module->clazz = (FT_Module_Class*)clazz; + + /* check whether the module is a renderer - this must be performed */ + /* before the normal module initialization */ + if ( FT_MODULE_IS_RENDERER( module ) ) { + /* add to the renderers list */ + error = ft_add_renderer( module ); + if ( error ) { + goto Fail; + } + } + + /* is the module a auto-hinter? */ + if ( FT_MODULE_IS_HINTER( module ) ) { + library->auto_hinter = module; + } + + /* if the module is a font driver */ + if ( FT_MODULE_IS_DRIVER( module ) ) { + /* allocate glyph loader if needed */ + FT_Driver driver = FT_DRIVER( module ); + + + driver->clazz = (FT_Driver_Class*)module->clazz; + if ( FT_DRIVER_USES_OUTLINES( driver ) ) { + error = FT_GlyphLoader_New( memory, &driver->glyph_loader ); + if ( error ) { + goto Fail; + } + } + } + + if ( clazz->module_init ) { + error = clazz->module_init( module ); + if ( error ) { + goto Fail; + } + } + + /* add module to the library's table */ + library->modules[library->num_modules++] = module; + +Exit: + return error; + +Fail: + if ( FT_MODULE_IS_DRIVER( module ) ) { + FT_Driver driver = FT_DRIVER( module ); + + + if ( FT_DRIVER_USES_OUTLINES( driver ) ) { + FT_GlyphLoader_Done( driver->glyph_loader ); + } + } + + if ( FT_MODULE_IS_RENDERER( module ) ) { + FT_Renderer renderer = FT_RENDERER( module ); + + + if ( renderer->raster ) { + renderer->clazz->raster_class->raster_done( renderer->raster ); + } + } + + FREE( module ); + goto Exit; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Get_Module */ +/* */ +/* */ +/* Finds a module by its name. */ +/* */ +/* */ +/* library :: A handle to the library object. */ +/* */ +/* module_name :: The module's name (as an ASCII string). */ +/* */ +/* */ +/* A module handle. 0 if none was found. */ +/* */ +/* */ +/* You should better be familiar with FreeType internals to know */ +/* which module to look for :-) */ +/* */ +FT_EXPORT_FUNC( FT_Module ) FT_Get_Module( FT_Library library, + const char* module_name ) +{ + FT_Module result = 0; + FT_Module* cur; + FT_Module* limit; + + + if ( !library || !module_name ) { + return result; + } + + cur = library->modules; + limit = cur + library->num_modules; + + for ( ; cur < limit; cur++ ) + if ( strcmp( cur[0]->clazz->module_name, module_name ) == 0 ) { + result = cur[0]; + break; + } + + return result; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Get_Module_Interface */ +/* */ +/* */ +/* Finds a module and returns its specific interface as a typeless */ +/* pointer. */ +/* */ +/* */ +/* library :: A handle to the library object. */ +/* */ +/* module_name :: The module's name (as an ASCII string). */ +/* */ +/* */ +/* A module-specific interface if available, 0 otherwise. */ +/* */ +/* */ +/* You should better be familiar with FreeType internals to know */ +/* which module to look for, and what its interface is :-) */ +/* */ +BASE_FUNC( const void* ) FT_Get_Module_Interface( FT_Library library, + const char* mod_name ) +{ + FT_Module module; + + + /* test for valid `library' delayed to FT_Get_Module() */ + + module = FT_Get_Module( library, mod_name ); + + return module ? module->clazz->module_interface : 0; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Remove_Module */ +/* */ +/* */ +/* Removes a given module from a library instance. */ +/* */ +/* */ +/* library :: A handle to a library object. */ +/* */ +/* module :: A handle to a module object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* The module object is destroyed by the function in case of success. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Remove_Module( FT_Library library, + FT_Module module ) +{ + /* try to find the module from the table, then remove it from there */ + + if ( !library ) { + return FT_Err_Invalid_Library_Handle; + } + + if ( module ) { + FT_Module* cur = library->modules; + FT_Module* limit = cur + library->num_modules; + + + for ( ; cur < limit; cur++ ) + { + if ( cur[0] == module ) { + /* remove it from the table */ + library->num_modules--; + limit--; + while ( cur < limit ) + { + cur[0] = cur[1]; + cur++; + } + limit[0] = 0; + + /* destroy the module */ + Destroy_Module( module ); + + return FT_Err_Ok; + } + } + } + return FT_Err_Invalid_Driver_Handle; +} + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** ****/ +/**** L I B R A R Y ****/ +/**** ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* FT_New_Library */ +/* */ +/* */ +/* This function is used to create a new FreeType library instance */ +/* from a given memory object. It is thus possible to use libraries */ +/* with distinct memory allocators within the same program. */ +/* */ +/* */ +/* memory :: A handle to the original memory object. */ +/* */ +/* */ +/* alibrary :: A pointer to handle of a new library object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_New_Library( FT_Memory memory, + FT_Library * alibrary ) +{ + FT_Library library = 0; + FT_Error error; + + + if ( !memory ) { + return FT_Err_Invalid_Argument; + } + + /* first of all, allocate the library object */ + if ( ALLOC( library, sizeof( *library ) ) ) { + return error; + } + + library->memory = memory; + + /* allocate the render pool */ + library->raster_pool_size = FT_RENDER_POOL_SIZE; + if ( ALLOC( library->raster_pool, FT_RENDER_POOL_SIZE ) ) { + goto Fail; + } + + /* That's ok now */ + *alibrary = library; + + return FT_Err_Ok; + +Fail: + FREE( library ); + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Done_Library */ +/* */ +/* */ +/* Discards a given library object. This closes all drivers and */ +/* discards all resource objects. */ +/* */ +/* */ +/* library :: A handle to the target library. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Done_Library( FT_Library library ) +{ + FT_Memory memory; + FT_UInt n; + + + if ( !library ) { + return FT_Err_Invalid_Library_Handle; + } + + memory = library->memory; + + /* Discard client-data */ + if ( library->generic.finalizer ) { + library->generic.finalizer( library ); + } + + /* Close all modules in the library */ + for ( n = 0; n < library->num_modules; n++ ) + { + FT_Module module = library->modules[n]; + + + if ( module ) { + Destroy_Module( module ); + library->modules[n] = 0; + } + } + + /* Destroy raster objects */ + FREE( library->raster_pool ); + library->raster_pool_size = 0; + + FREE( library ); + return FT_Err_Ok; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Set_Debug_Hook */ +/* */ +/* */ +/* Sets a debug hook function for debugging the interpreter of a font */ +/* format. */ +/* */ +/* */ +/* library :: A handle to the library object. */ +/* */ +/* hook_index :: The index of the debug hook. You should use the */ +/* values defined in ftobjs.h, e.g. */ +/* FT_DEBUG_HOOK_TRUETYPE */ +/* */ +/* debug_hook :: The function used to debug the interpreter. */ +/* */ +/* */ +/* Currently, four debug hook slots are available, but only two (for */ +/* the TrueType and the Type 1 interpreter) are defined. */ +/* */ +FT_EXPORT_FUNC( void ) FT_Set_Debug_Hook( FT_Library library, + FT_UInt hook_index, + FT_DebugHook_Func debug_hook ) +{ + if ( library && debug_hook && + hook_index < + ( sizeof( library->debug_hooks ) / sizeof( void* ) ) ) { + library->debug_hooks[hook_index] = debug_hook; + } +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Done_FreeType */ +/* */ +/* */ +/* Destroys a given FreeType library object and all of its childs, */ +/* including resources, drivers, faces, sizes, etc. */ +/* */ +/* */ +/* library :: A handle to the target library object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Done_FreeType( FT_Library library ) +{ + /* test for valid `library' delayed to FT_Done_Library() */ + + /* Discard the library object */ + FT_Done_Library( library ); + + return FT_Err_Ok; +} + + +/* END */ diff --git a/src/ft2/ftobjs.h b/src/ft2/ftobjs.h new file mode 100644 index 0000000..002c9e7 --- /dev/null +++ b/src/ft2/ftobjs.h @@ -0,0 +1,532 @@ +/***************************************************************************/ +/* */ +/* ftobjs.h */ +/* */ +/* The FreeType private base classes (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +/*************************************************************************/ +/* */ +/* This file contains the definition of all internal FreeType classes. */ +/* */ +/*************************************************************************/ + + +#ifndef FTOBJS_H +#define FTOBJS_H + +#include "ftmemory.h" +#include "ftrender.h" +#include "ftdriver.h" +#include "autohint.h" + + +#ifdef __cplusplus +extern "C" { +#endif + + +/*************************************************************************/ +/* */ +/* Some generic definitions. */ +/* */ +#ifndef TRUE +#define TRUE 1 +#endif + +#ifndef FALSE +#define FALSE 0 +#endif + +#ifndef NULL +#define NULL (void*)0 +#endif + +#ifndef UNUSED +#define UNUSED( arg ) ( ( arg ) = ( arg ) ) +#endif + + +/*************************************************************************/ +/* */ +/* The min and max functions missing in C. As usual, be careful not to */ +/* write things like MIN( a++, b++ ) to avoid side effects. */ +/* */ +#ifndef MIN +#define MIN( a, b ) ( ( a ) < ( b ) ? ( a ) : ( b ) ) +#endif + +#ifndef MAX +#define MAX( a, b ) ( ( a ) > ( b ) ? ( a ) : ( b ) ) +#endif + +#ifndef ABS +#define ABS( a ) ( ( a ) < 0 ? -( a ) : ( a ) ) +#endif + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** ****/ +/**** M O D U L E S ****/ +/**** ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* FT_ModuleRec */ +/* */ +/* */ +/* A module object instance. */ +/* */ +/* */ +/* clazz :: A pointer to the module's class. */ +/* */ +/* library :: A handle to the parent library object. */ +/* */ +/* memory :: A handle to the memory manager. */ +/* */ +/* generic :: A generic structure for user-level extensibility (?). */ +/* */ +typedef struct FT_ModuleRec_ +{ + FT_Module_Class* clazz; + FT_Library library; + FT_Memory memory; + FT_Generic generic; + +} FT_ModuleRec; + + +/* typecast an object to a FT_Module */ +#define FT_MODULE( x ) ( (FT_Module)( x ) ) +#define FT_MODULE_CLASS( x ) FT_MODULE( x )->clazz +#define FT_MODULE_LIBRARY( x ) FT_MODULE( x )->library +#define FT_MODULE_MEMORY( x ) FT_MODULE( x )->memory + +#define FT_MODULE_IS_DRIVER( x ) ( FT_MODULE_CLASS( x )->module_flags & \ + ft_module_font_driver ) + +#define FT_MODULE_IS_RENDERER( x ) ( FT_MODULE_CLASS( x )->module_flags & \ + ft_module_renderer ) + +#define FT_MODULE_IS_HINTER( x ) ( FT_MODULE_CLASS( x )->module_flags & \ + ft_module_hinter ) + +#define FT_MODULE_IS_STYLER( x ) ( FT_MODULE_CLASS( x )->module_flags & \ + ft_module_styler ) + +#define FT_DRIVER_IS_SCALABLE( x ) ( FT_MODULE_CLASS( x )->module_flags & \ + ft_module_driver_scalable ) + +#define FT_DRIVER_USES_OUTLINES( x ) !( FT_MODULE_CLASS( x )->module_flags & \ + ft_module_driver_no_outlines ) + +#define FT_DRIVER_HAS_HINTER( x ) ( FT_MODULE_CLASS( x )->module_flags & \ + ft_module_driver_has_hinter ) + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Get_Module_Interface */ +/* */ +/* */ +/* Finds a module and returns its specific interface as a typeless */ +/* pointer. */ +/* */ +/* */ +/* library :: A handle to the library object. */ +/* */ +/* module_name :: The module's name (as an ASCII string). */ +/* */ +/* */ +/* A module-specific interface if available, 0 otherwise. */ +/* */ +/* */ +/* You should better be familiar with FreeType internals to know */ +/* which module to look for, and what its interface is :-) */ +/* */ +BASE_DEF( const void* ) FT_Get_Module_Interface( FT_Library library, + const char* mod_name ); + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** ****/ +/**** FACE, SIZE & GLYPH SLOT OBJECTS ****/ +/**** ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + +/* a few macros used to perform easy typecasts with minimal brain damage */ + +#define FT_FACE( x ) ( (FT_Face)( x ) ) +#define FT_SIZE( x ) ( (FT_Size)( x ) ) +#define FT_SLOT( x ) ( (FT_GlyphSlot)( x ) ) + +#define FT_FACE_DRIVER( x ) FT_FACE( x )->driver +#define FT_FACE_LIBRARY( x ) FT_FACE_DRIVER( x )->root.library +#define FT_FACE_MEMORY( x ) FT_FACE( x )->memory + +#define FT_SIZE_FACE( x ) FT_SIZE( x )->face +#define FT_SLOT_FACE( x ) FT_SLOT( x )->face + +#define FT_FACE_SLOT( x ) FT_FACE( x )->glyph +#define FT_FACE_SIZE( x ) FT_FACE( x )->size + + +/* this must be kept exported -- tt will be used later in our own */ +/* high-level caching font manager called SemTex (way after the */ +/* 2.0 release though */ +FT_EXPORT_DEF( FT_Error ) FT_New_Size( FT_Face face, + FT_Size * size ); + +FT_EXPORT_DEF( FT_Error ) FT_Done_Size( FT_Size size ); + + +FT_EXPORT_DEF( FT_Error ) FT_New_GlyphSlot( FT_Face face, + FT_GlyphSlot * aslot ); + +FT_EXPORT_DEF( void ) FT_Done_GlyphSlot( FT_GlyphSlot slot ); + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** ****/ +/**** G L Y P H L O A D E R ****/ +/**** ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +#define FT_SUBGLYPH_FLAG_ARGS_ARE_WORDS 1 +#define FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES 2 +#define FT_SUBGLYPH_FLAG_ROUND_XY_TO_GRID 4 +#define FT_SUBGLYPH_FLAG_SCALE 8 +#define FT_SUBGLYPH_FLAG_XY_SCALE 0x40 +#define FT_SUBGLYPH_FLAG_2X2 0x80 +#define FT_SUBGLYPH_FLAG_USE_MY_METRICS 0x200 + + +enum +{ + ft_glyph_own_bitmap = 1 +}; + + +struct FT_SubGlyph_ +{ + FT_Int index; + FT_UShort flags; + FT_Int arg1; + FT_Int arg2; + FT_Matrix transform; +}; + + +typedef struct FT_GlyphLoad_ +{ + FT_Outline outline; /* outline */ + FT_UInt num_subglyphs; /* number of subglyphs */ + FT_SubGlyph* subglyphs; /* subglyphs */ + FT_Vector* extra_points; /* extra points table */ + +} FT_GlyphLoad; + + +struct FT_GlyphLoader_ +{ + FT_Memory memory; + FT_UInt max_points; + FT_UInt max_contours; + FT_UInt max_subglyphs; + FT_Bool use_extra; + + FT_GlyphLoad base; + FT_GlyphLoad current; + + void* other; /* for possible future extension? */ + +}; + + +BASE_DEF( FT_Error ) FT_GlyphLoader_New( FT_Memory memory, + FT_GlyphLoader * *aloader ); + +BASE_DEF( FT_Error ) FT_GlyphLoader_Create_Extra( + FT_GlyphLoader * loader ); + +BASE_DEF( void ) FT_GlyphLoader_Done( FT_GlyphLoader * loader ); + +BASE_DEF( void ) FT_GlyphLoader_Reset( FT_GlyphLoader * loader ); + +BASE_DEF( void ) FT_GlyphLoader_Rewind( FT_GlyphLoader * loader ); + +BASE_DEF( FT_Error ) FT_GlyphLoader_Check_Points( + FT_GlyphLoader * loader, + FT_UInt n_points, + FT_UInt n_contours ); + +BASE_DEF( FT_Error ) FT_GlyphLoader_Check_Subglyphs( + FT_GlyphLoader * loader, + FT_UInt n_subs ); + +BASE_DEF( void ) FT_GlyphLoader_Prepare( FT_GlyphLoader * loader ); + +BASE_DEF( void ) FT_GlyphLoader_Add( FT_GlyphLoader * loader ); + +BASE_DEF( FT_Error ) FT_GlyphLoader_Copy_Points( FT_GlyphLoader * target, + FT_GlyphLoader * source ); + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** ****/ +/**** R E N D E R E R S ****/ +/**** ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +#define FT_RENDERER( x ) ( (FT_Renderer)( x ) ) +#define FT_GLYPH( x ) ( (FT_Glyph)( x ) ) +#define FT_BITMAP_GLYPH( x ) ( (FT_BitmapGlyph)( x ) ) +#define FT_OUTLINE_GLYPH( x ) ( (FT_OutlineGlyph)( x ) ) + + +typedef struct FT_RendererRec_ +{ + FT_ModuleRec root; + FT_Renderer_Class* clazz; + FT_Glyph_Format glyph_format; + const FT_Glyph_Class glyph_class; + + FT_Raster raster; + FT_Raster_Render_Func raster_render; + FTRenderer_render render; + +} FT_RendererRec; + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** ****/ +/**** F O N T D R I V E R S ****/ +/**** ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +/* typecast a module into a driver easily */ +#define FT_DRIVER( x ) ( (FT_Driver)( x ) ) + +/* typecast a module as a driver, and get its driver class */ +#define FT_DRIVER_CLASS( x ) FT_DRIVER( x )->clazz + + +/*************************************************************************/ +/* */ +/* */ +/* FT_DriverRec */ +/* */ +/* */ +/* The root font driver class. A font driver is responsible for */ +/* managing and loading font files of a given format. */ +/* */ +/* */ +/* root :: Contains the fields of the root module class. */ +/* */ +/* clazz :: A pointer to the font driver's class. Note that */ +/* this is NOT root.clazz. `class' wasn't used */ +/* as it is a reserved word in C++. */ +/* */ +/* faces_list :: The list of faces currently opened by this */ +/* driver. */ +/* */ +/* extensions :: A typeless pointer to the driver's extensions */ +/* registry, if they are supported through the */ +/* configuration macro FT_CONFIG_OPTION_EXTENSIONS. */ +/* */ +/* glyph_loader :: The glyph loader for all faces managed by this */ +/* driver. This object isn't defined for unscalable */ +/* formats. */ +/* */ +typedef struct FT_DriverRec_ +{ + FT_ModuleRec root; + FT_Driver_Class* clazz; + + FT_ListRec faces_list; + void* extensions; + + FT_GlyphLoader* glyph_loader; + +} FT_DriverRec; + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** ****/ +/**** L I B R A R I E S ****/ +/**** ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +#define FT_DEBUG_HOOK_TRUETYPE 0 +#define FT_DEBUG_HOOK_TYPE1 1 + + +/*************************************************************************/ +/* */ +/* */ +/* FT_LibraryRec */ +/* */ +/* */ +/* The FreeType library class. This is the root of all FreeType */ +/* data. Use FT_New_Library() to create a library object, and */ +/* FT_Done_Library() to discard it and all child objects. */ +/* */ +/* */ +/* memory :: The library's memory object. Manages memory */ +/* allocation. */ +/* */ +/* generic :: Client data variable. Used to extend the */ +/* Library class by higher levels and clients. */ +/* */ +/* num_modules :: The number of modules currently registered */ +/* within this library. This is set to 0 for new */ +/* libraries. New modules are added through the */ +/* FT_Add_Module() API function. */ +/* */ +/* modules :: A table used to store handles to the currently */ +/* registered modules. Note that each font driver */ +/* contains a list of its opened faces. */ +/* */ +/* renderers :: The list of renderers currently registered */ +/* within the library. */ +/* */ +/* cur_renderer :: The current outline renderer. This is a */ +/* shortcut used to avoid parsing the list on */ +/* each call to FT_Outline_Render(). It is a */ +/* handle to the current renderer for the */ +/* ft_glyph_format_outline format. */ +/* */ +/* auto_hinter :: XXX */ +/* */ +/* raster_pool :: The raster object's render pool. This can */ +/* ideally be changed dynamically at run-time. */ +/* */ +/* raster_pool_size :: The size of the render pool in bytes. */ +/* */ +/* debug_hooks :: XXX */ +/* */ +typedef struct FT_LibraryRec_ +{ + FT_Memory memory; /* library's memory manager */ + + FT_Generic generic; + + FT_UInt num_modules; + FT_Module modules[FT_MAX_MODULES]; /* module objects */ + + FT_ListRec renderers; /* list of renderers */ + FT_Renderer cur_renderer; /* current outline renderer */ + FT_Module auto_hinter; + + FT_Byte* raster_pool; /* scan-line conversion */ + /* render pool */ + FT_ULong raster_pool_size; /* size of render pool in bytes */ + + FT_DebugHook_Func debug_hooks[4]; + +} FT_LibraryRec; + + +BASE_DEF( FT_Renderer ) FT_Lookup_Renderer( FT_Library library, + FT_Glyph_Format format, + FT_ListNode * node ); + +BASE_DEF( FT_Error ) FT_Render_Glyph_Internal( FT_Library library, + FT_GlyphSlot slot, + FT_UInt render_mode ); + +typedef FT_Error ( *FT_Glyph_Name_Requester )( FT_Face face, + FT_UInt glyph_index, + FT_Pointer buffer, + FT_UInt buffer_max ); + + +#ifndef FT_CONFIG_OPTION_NO_DEFAULT_SYSTEM + + +FT_EXPORT_DEF( FT_Error ) FT_New_Stream( const char* filepathname, + FT_Stream astream ); + +FT_EXPORT_DEF( void ) FT_Done_Stream( FT_Stream stream ); + +FT_EXPORT_DEF( FT_Memory ) FT_New_Memory( void ); + + +#endif /* !FT_CONFIG_OPTION_NO_DEFAULT_SYSTEM */ + + +/* Define default raster's interface. The default raster is located in */ +/* `src/base/ftraster.c' */ +/* */ +/* Client applications can register new rasters through the */ +/* FT_Set_Raster() API. */ + +#ifndef FT_NO_DEFAULT_RASTER +FT_EXPORT_VAR( FT_Raster_Funcs ) ft_default_raster; +#endif + + +#ifdef __cplusplus +} +#endif + + +#endif /* FTOBJS_H */ + + +/* END */ diff --git a/src/ft2/ftoption.h b/src/ft2/ftoption.h new file mode 100644 index 0000000..137087c --- /dev/null +++ b/src/ft2/ftoption.h @@ -0,0 +1,395 @@ +/***************************************************************************/ +/* */ +/* ftoption.h */ +/* */ +/* User-selectable configuration macros (specification only). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef FTOPTION_H +#define FTOPTION_H + + +/*************************************************************************/ +/* */ +/* USER-SELECTABLE CONFIGURATION MACROS */ +/* */ +/* These macros can be toggled by developers to enable or disable */ +/* certain aspects of FreeType. This is a default file, where all major */ +/* options are enabled. */ +/* */ +/* Note that if some modifications are required for your build, we */ +/* advise you to put a modified copy of this file in your build */ +/* directory, rather than modifying it in-place. */ +/* */ +/* The build directory is normally `freetype/builds/' and */ +/* contains build or system-specific files that are included in */ +/* priority when building the library. */ +/* */ +/*************************************************************************/ + + +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** G E N E R A L F R E E T Y P E 2 C O N F I G U R A T I O N ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* Convenience functions support */ +/* */ +/* Some functions of the FreeType 2 API are provided as a convenience */ +/* for client applications and developers. However, they are not */ +/* required to build and run the library itself. */ +/* */ +/* By defining this configuration macro, you'll disable the */ +/* compilation of these functions at build time. This can be useful */ +/* to reduce the library's code size when you don't need any of */ +/* these functions. */ +/* */ +/* All convenience functions are declared as such in their */ +/* documentation. */ +/* */ +#undef FT_CONFIG_OPTION_NO_CONVENIENCE_FUNCS + + +/*************************************************************************/ +/* */ +/* Alternate Glyph Image Format support */ +/* */ +/* By default, the glyph images returned by the FreeType glyph loader */ +/* can either be a pixmap or a vectorial outline defined through */ +/* Bezier control points. When defining the following configuration */ +/* macro, some font drivers will be able to register alternate */ +/* glyph image formats. */ +/* */ +/* Unset this macro if you are sure that you will never use a font */ +/* driver with an alternate glyph format; this will reduce the size of */ +/* the base layer code. */ +/* */ +/* Note that a few Type 1 fonts, as well as Windows `vector' fonts */ +/* use a vector `plotter' format that isn't supported when this */ +/* macro is undefined. */ +/* */ +#define FT_CONFIG_OPTION_ALTERNATE_GLYPH_FORMATS + + +/*************************************************************************/ +/* */ +/* Glyph Postscript Names handling */ +/* */ +/* By default, FreeType 2 is compiled with the `PSNames' module. This */ +/* This module is in charge of converting a glyph name string into a */ +/* Unicode value, or return a Macintosh standard glyph name for the */ +/* use with the TrueType `post' table. */ +/* */ +/* Undefine this macro if you do not want `PSNames' compiled in your */ +/* build of FreeType. This has the following effects: */ +/* */ +/* - The TrueType driver will provide its own set of glyph names, */ +/* if you build it to support postscript names in the TrueType */ +/* `post' table. */ +/* */ +/* - The Type 1 driver will not be able to synthetize a Unicode */ +/* charmap out of the glyphs found in the fonts. */ +/* */ +/* You would normally undefine this configuration macro when building */ +/* a version of FreeType that doesn't contain a Type 1 or CFF driver. */ +/* */ +#define FT_CONFIG_OPTION_POSTSCRIPT_NAMES + + +/*************************************************************************/ +/* */ +/* Postscript Names to Unicode Values support */ +/* */ +/* By default, FreeType 2 is built with the `PSNames' module compiled */ +/* in. Among other things, the module is used to convert a glyph name */ +/* into a Unicode value. This is especially useful in order to */ +/* synthetize on the fly a Unicode charmap from the CFF/Type 1 driver */ +/* through a big table named the `Adobe Glyph List' (AGL). */ +/* */ +/* Undefine this macro if you do not want the Adobe Glyph List */ +/* compiled in your `PSNames' module. The Type 1 driver will not be */ +/* able to synthetize a Unicode charmap out of the glyphs found in the */ +/* fonts. */ +/* */ +#define FT_CONFIG_OPTION_ADOBE_GLYPH_LIST + + +/*************************************************************************/ +/* */ +/* Many compilers provide the non-ANSI `long long' 64-bit type. You can */ +/* activate it by defining the FTCALC_USE_LONG_LONG macro. Note that */ +/* this will produce many -ansi warnings during library compilation, and */ +/* that in many cases the generated code will not be smaller or faster! */ +/* */ +#undef FTCALC_USE_LONG_LONG + + +/*************************************************************************/ +/* */ +/* DLL export compilation */ +/* */ +/* When compiling FreeType as a DLL, some systems/compilers need a */ +/* special keyword in front OR after the return type of function */ +/* declarations. */ +/* */ +/* Two macros are used within the FreeType source code to define */ +/* exported library functions: FT_EXPORT_DEF and FT_EXPORT_FUNC. */ +/* */ +/* FT_EXPORT_DEF( return_type ) */ +/* */ +/* is used in a function declaration, as in */ +/* */ +/* FT_EXPORT_DEF( FT_Error ) */ +/* FT_Init_FreeType( FT_Library* alibrary ); */ +/* */ +/* */ +/* FT_EXPORT_FUNC( return_type ) */ +/* */ +/* is used in a function definition, as in */ +/* */ +/* FT_EXPORT_FUNC( FT_Error ) */ +/* FT_Init_FreeType( FT_Library* alibrary ) */ +/* { */ +/* ... some code ... */ +/* return FT_Err_Ok; */ +/* } */ +/* */ +/* You can provide your own implementation of FT_EXPORT_DEF and */ +/* FT_EXPORT_FUNC here if you want. If you leave them undefined, they */ +/* will be later automatically defined as `extern return_type' to */ +/* allow normal compilation. */ +/* */ +#undef FT_EXPORT_DEF +#undef FT_EXPORT_FUNC + + +/*************************************************************************/ +/* */ +/* Debug level */ +/* */ +/* FreeType can be compiled in debug or trace mode. In debug mode, */ +/* errors are reported through the `ftdebug' component. In trace */ +/* mode, additional messages are sent to the standard output during */ +/* execution. */ +/* */ +/* Define FT_DEBUG_LEVEL_ERROR to build the library in debug mode. */ +/* Define FT_DEBUG_LEVEL_TRACE to build it in trace mode. */ +/* */ +/* Don't define any of these macros to compile in `release' mode! */ +/* */ +//#define FT_DEBUG_LEVEL_ERROR +//#define FT_DEBUG_LEVEL_TRACE + + +/*************************************************************************/ +/* */ +/* Computation Algorithms */ +/* */ +/* Used for debugging, this configuration macro should disappear */ +/* soon. */ +/* */ +#define FT_CONFIG_OPTION_OLD_CALCS + + +/*************************************************************************/ +/* */ +/* The size in bytes of the render pool used by the scan-line converter */ +/* to do all of its work. */ +/* */ +/* This must be greater than 4kByte. */ +/* */ +#define FT_RENDER_POOL_SIZE 16384 + + +/*************************************************************************/ +/* */ +/* FT_MAX_MODULES */ +/* */ +/* The maximum number of modules that can be registered in a single */ +/* FreeType library object. 16 is the default. */ +/* */ +#define FT_MAX_MODULES 16 + + +/*************************************************************************/ +/* */ +/* FT_MAX_EXTENSIONS */ +/* */ +/* The maximum number of extensions that can be registered in a single */ +/* font driver. 8 is the default. */ +/* */ +/* If you don't know what this means, you certainly do not need to */ +/* change this value. */ +/* */ +#define FT_MAX_EXTENSIONS 8 + + +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** S F N T D R I V E R C O N F I G U R A T I O N ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* Define TT_CONFIG_OPTION_EMBEDDED_BITMAPS if you want to support */ +/* embedded bitmaps in all formats using the SFNT module (namely */ +/* TrueType & OpenType). */ +/* */ +#define TT_CONFIG_OPTION_EMBEDDED_BITMAPS + + +/*************************************************************************/ +/* */ +/* Define TT_CONFIG_OPTION_POSTSCRIPT_NAMES if you want to be able to */ +/* load and enumerate the glyph Postscript names in a TrueType or */ +/* OpenType file. */ +/* */ +/* Note that when you do not compile the `PSNames' module by undefining */ +/* the above FT_CONFIG_OPTION_POSTSCRIPT_NAMES, the `sfnt' module will */ +/* contain additional code used to read the PS Names table from a font. */ +/* */ +/* (By default, the module uses `PSNames' to extract glyph names.) */ +/* */ +#define TT_CONFIG_OPTION_POSTSCRIPT_NAMES + + +/*************************************************************************/ +/* */ +/* Define TT_CONFIG_OPTION_SFNT_NAMES if your applications need to */ +/* access the internal name table in a SFNT-based format like TrueType */ +/* or OpenType. The name table contains various strings used to */ +/* describe the font, like family name, copyright, version, etc. It */ +/* does not contain any glyph name though. */ +/* */ +/* Accessing SFNT names is done through the functions declared in */ +/* `freetype/ftnames.h'. */ +/* */ +#define TT_CONFIG_OPTION_SFNT_NAMES + + +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** T R U E T Y P E D R I V E R C O N F I G U R A T I O N ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ + +/*************************************************************************/ +/* */ +/* Define TT_CONFIG_OPTION_BYTECODE_INTERPRETER if you want to compile */ +/* a bytecode interpreter in the TrueType driver. Note that there are */ +/* important patent issues related to the use of the interpreter. */ +/* */ +/* By undefining this, you will only compile the code necessary to load */ +/* TrueType glyphs without hinting. */ +/* */ +#define TT_CONFIG_OPTION_BYTECODE_INTERPRETER + + +/*************************************************************************/ +/* */ +/* Define TT_CONFIG_OPTION_INTERPRETER_SWITCH to compile the TrueType */ +/* bytecode interpreter with a huge switch statement, rather than a call */ +/* table. This results in smaller and faster code for a number of */ +/* architectures. */ +/* */ +/* Note however that on some compiler/processor combinations, undefining */ +/* this macro will generate faster, though larger, code. */ +/* */ +#define TT_CONFIG_OPTION_INTERPRETER_SWITCH + + +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** T Y P E 1 D R I V E R C O N F I G U R A T I O N ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* T1_MAX_STACK_DEPTH is the maximal depth of the token stack used by */ +/* the Type 1 parser (see t1load.c). A minimum of 16 is required. */ +/* */ +#define T1_MAX_STACK_DEPTH 16 + + +/*************************************************************************/ +/* */ +/* T1_MAX_DICT_DEPTH is the maximal depth of nest dictionaries and */ +/* arrays in the Type 1 stream (see t1load.c). A minimum of 4 is */ +/* required. */ +/* */ +#define T1_MAX_DICT_DEPTH 5 + + +/*************************************************************************/ +/* */ +/* T1_MAX_SUBRS_CALLS details the maximum number of nested sub-routine */ +/* calls during glyph loading. */ +/* */ +#define T1_MAX_SUBRS_CALLS 8 + + +/*************************************************************************/ +/* */ +/* T1_MAX_CHARSTRING_OPERANDS is the charstring stack's capacity. */ +/* */ +#define T1_MAX_CHARSTRINGS_OPERANDS 32 + + +/*************************************************************************/ +/* */ +/* Define T1_CONFIG_OPTION_DISABLE_HINTER if you want to generate a */ +/* driver with no hinter. This can be useful to debug the parser. */ +/* */ +#undef T1_CONFIG_OPTION_DISABLE_HINTER + + +/*************************************************************************/ +/* */ +/* Define this configuration macro if you want to prevent the */ +/* compilation of `t1afm', which is in charge of reading Type 1 AFM */ +/* files into an existing face. Note that if set, the T1 driver will be */ +/* unable to produce kerning distances. */ +/* */ +#undef T1_CONFIG_OPTION_NO_AFM + + +/*************************************************************************/ +/* */ +/* Define this configuration macro if you want to prevent the */ +/* compilation of the Multiple Masters font support in the Type 1 */ +/* driver. */ +/* */ +#undef T1_CONFIG_OPTION_NO_MM_SUPPORT + + +#endif /* FTOPTION_H */ + + +/* END */ diff --git a/src/ft2/ftoutln.c b/src/ft2/ftoutln.c new file mode 100644 index 0000000..0c45e4d --- /dev/null +++ b/src/ft2/ftoutln.c @@ -0,0 +1,861 @@ +/***************************************************************************/ +/* */ +/* ftoutln.c */ +/* */ +/* FreeType outline management (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +/*************************************************************************/ +/* */ +/* All functions are declared in freetype.h. */ +/* */ +/*************************************************************************/ + + +#include "ftoutln.h" +#include "ftobjs.h" + + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_outline + + +static +const FT_Outline null_outline = { 0, 0, 0, 0, 0, 0 }; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_Decompose */ +/* */ +/* */ +/* Walks over an outline's structure to decompose it into individual */ +/* segments and Bezier arcs. This function is also able to emit */ +/* `move to' and `close to' operations to indicate the start and end */ +/* of new contours in the outline. */ +/* */ +/* */ +/* outline :: A pointer to the source target. */ +/* */ +/* interface :: A table of `emitters', i.e,. function pointers called */ +/* during decomposition to indicate path operations. */ +/* */ +/* user :: A typeless pointer which is passed to each emitter */ +/* during the decomposition. It can be used to store */ +/* the state during the decomposition. */ +/* */ +/* */ +/* FreeType error code. 0 means sucess. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Outline_Decompose( + FT_Outline * outline, + FT_Outline_Funcs * interface, + void* user ) +{ +#undef SCALED +#define SCALED( x ) ( ( ( x ) << shift ) - delta ) + + FT_Vector v_last; + FT_Vector v_control; + FT_Vector v_start; + + FT_Vector* point; + FT_Vector* limit; + char* tags; + + FT_Error error; + + FT_Int n; /* index of contour in outline */ + FT_UInt first; /* index of first point in contour */ + char tag; /* current point's state */ + + FT_Int shift; + FT_Pos delta; + + + if ( !outline || !interface ) { + return FT_Err_Invalid_Argument; + } + + shift = interface->shift; + delta = interface->delta; + first = 0; + + for ( n = 0; n < outline->n_contours; n++ ) + { + FT_Int last; /* index of last point in contour */ + + + last = outline->contours[n]; + limit = outline->points + last; + + v_start = outline->points[first]; + v_last = outline->points[last]; + + v_start.x = SCALED( v_start.x ); v_start.y = SCALED( v_start.y ); + v_last.x = SCALED( v_last.x ); v_last.y = SCALED( v_last.y ); + + v_control = v_start; + + point = outline->points + first; + tags = outline->tags + first; + tag = FT_CURVE_TAG( tags[0] ); + + /* A contour cannot start with a cubic control point! */ + if ( tag == FT_Curve_Tag_Cubic ) { + goto Invalid_Outline; + } + + /* check first point to determine origin */ + if ( tag == FT_Curve_Tag_Conic ) { + /* first point is conic control. Yes, this happens. */ + if ( FT_CURVE_TAG( outline->tags[last] ) == FT_Curve_Tag_On ) { + /* start at last point if it is on the curve */ + v_start = v_last; + limit--; + } else + { + /* if both first and last points are conic, */ + /* start at their middle and record its position */ + /* for closure */ + v_start.x = ( v_start.x + v_last.x ) / 2; + v_start.y = ( v_start.y + v_last.y ) / 2; + + v_last = v_start; + } + point--; + tags--; + } + + error = interface->move_to( &v_start, user ); + if ( error ) { + goto Exit; + } + + while ( point < limit ) + { + point++; + tags++; + + tag = FT_CURVE_TAG( tags[0] ); + switch ( tag ) + { + case FT_Curve_Tag_On: /* emit a single line_to */ + { + FT_Vector vec; + + + vec.x = SCALED( point->x ); + vec.y = SCALED( point->y ); + + error = interface->line_to( &vec, user ); + if ( error ) { + goto Exit; + } + continue; + } + + case FT_Curve_Tag_Conic: /* consume conic arcs */ + v_control.x = SCALED( point->x ); + v_control.y = SCALED( point->y ); + +Do_Conic: + if ( point < limit ) { + FT_Vector vec; + FT_Vector v_middle; + + + point++; + tags++; + tag = FT_CURVE_TAG( tags[0] ); + + vec.x = SCALED( point->x ); + vec.y = SCALED( point->y ); + + if ( tag == FT_Curve_Tag_On ) { + error = interface->conic_to( &v_control, &vec, user ); + if ( error ) { + goto Exit; + } + continue; + } + + if ( tag != FT_Curve_Tag_Conic ) { + goto Invalid_Outline; + } + + v_middle.x = ( v_control.x + vec.x ) / 2; + v_middle.y = ( v_control.y + vec.y ) / 2; + + error = interface->conic_to( &v_control, &v_middle, user ); + if ( error ) { + goto Exit; + } + + v_control = vec; + goto Do_Conic; + } + + error = interface->conic_to( &v_control, &v_start, user ); + goto Close; + + default: /* FT_Curve_Tag_Cubic */ + { + FT_Vector vec1, vec2; + + + if ( point + 1 > limit || + FT_CURVE_TAG( tags[1] ) != FT_Curve_Tag_Cubic ) { + goto Invalid_Outline; + } + + point += 2; + tags += 2; + + vec1.x = SCALED( point[-2].x ); vec1.y = SCALED( point[-2].y ); + vec2.x = SCALED( point[-1].x ); vec2.y = SCALED( point[-1].y ); + + if ( point <= limit ) { + FT_Vector vec; + + + vec.x = SCALED( point->x ); + vec.y = SCALED( point->y ); + + error = interface->cubic_to( &vec1, &vec2, &vec, user ); + if ( error ) { + goto Exit; + } + continue; + } + + error = interface->cubic_to( &vec1, &vec2, &v_start, user ); + goto Close; + } + } + } + + /* close the contour with a line segment */ + error = interface->line_to( &v_start, user ); + +Close: + if ( error ) { + goto Exit; + } + + first = last + 1; + } + + return 0; + +Exit: + return error; + +Invalid_Outline: + return FT_Err_Invalid_Outline; +} + + +FT_EXPORT_FUNC( FT_Error ) FT_Outline_New_Internal( + FT_Memory memory, + FT_UInt numPoints, + FT_Int numContours, + FT_Outline * outline ) +{ + FT_Error error; + + + if ( !outline || !memory ) { + return FT_Err_Invalid_Argument; + } + + *outline = null_outline; + + if ( ALLOC_ARRAY( outline->points, numPoints * 2L, FT_Pos ) || + ALLOC_ARRAY( outline->tags, numPoints, FT_Byte ) || + ALLOC_ARRAY( outline->contours, numContours, FT_UShort ) ) { + goto Fail; + } + + outline->n_points = (FT_UShort)numPoints; + outline->n_contours = (FT_Short)numContours; + outline->flags |= ft_outline_owner; + + return FT_Err_Ok; + +Fail: + outline->flags |= ft_outline_owner; + FT_Outline_Done_Internal( memory, outline ); + + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_New */ +/* */ +/* */ +/* Creates a new outline of a given size. */ +/* */ +/* */ +/* library :: A handle to the library object from where the */ +/* outline is allocated. Note however that the new */ +/* outline will NOT necessarily be FREED, when */ +/* destroying the library, by FT_Done_FreeType(). */ +/* */ +/* numPoints :: The maximal number of points within the outline. */ +/* */ +/* numContours :: The maximal number of contours within the outline. */ +/* */ +/* */ +/* outline :: A handle to the new outline. NULL in case of */ +/* error. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* No. */ +/* */ +/* */ +/* The reason why this function takes a `library' parameter is simply */ +/* to use the library's memory allocator. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Outline_New( FT_Library library, + FT_UInt numPoints, + FT_Int numContours, + FT_Outline * outline ) +{ + if ( !library ) { + return FT_Err_Invalid_Library_Handle; + } + + return FT_Outline_New_Internal( library->memory, numPoints, + numContours, outline ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_Copy */ +/* */ +/* */ +/* Copies an outline into another one. Both objects must have the */ +/* same sizes (number of points & number of contours) when this */ +/* function is called. */ +/* */ +/* */ +/* source :: A handle to the source outline. */ +/* */ +/* */ +/* target :: A handle to the target outline. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Outline_Copy( FT_Outline * source, + FT_Outline * target ) +{ + FT_Int is_owner; + + + if ( !source || !target || + source->n_points != target->n_points || + source->n_contours != target->n_contours ) { + return FT_Err_Invalid_Argument; + } + + MEM_Copy( target->points, source->points, + source->n_points * sizeof( FT_Vector ) ); + + MEM_Copy( target->tags, source->tags, + source->n_points * sizeof( FT_Byte ) ); + + MEM_Copy( target->contours, source->contours, + source->n_contours * sizeof( FT_Short ) ); + + /* copy all flags, except the `ft_outline_owner' one */ + is_owner = target->flags & ft_outline_owner; + target->flags = source->flags; + + target->flags &= ~ft_outline_owner; + target->flags |= is_owner; + + return FT_Err_Ok; +} + + +FT_EXPORT_FUNC( FT_Error ) FT_Outline_Done_Internal( FT_Memory memory, + FT_Outline * outline ) +{ + if ( outline ) { + if ( outline->flags & ft_outline_owner ) { + FREE( outline->points ); + FREE( outline->tags ); + FREE( outline->contours ); + } + *outline = null_outline; + + return FT_Err_Ok; + } else { + return FT_Err_Invalid_Argument; + } +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_Done */ +/* */ +/* */ +/* Destroys an outline created with FT_Outline_New(). */ +/* */ +/* */ +/* library :: A handle of the library object used to allocate the */ +/* outline. */ +/* */ +/* outline :: A pointer to the outline object to be discarded. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* No. */ +/* */ +/* */ +/* If the outline's `owner' field is not set, only the outline */ +/* descriptor will be released. */ +/* */ +/* The reason why this function takes an `outline' parameter is */ +/* simply to use FT_Free(). */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Outline_Done( FT_Library library, + FT_Outline * outline ) +{ + /* check for valid `outline' in FT_Outline_Done_Internal() */ + + if ( !library ) { + return FT_Err_Invalid_Library_Handle; + } + + return FT_Outline_Done_Internal( library->memory, outline ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_Get_CBox */ +/* */ +/* */ +/* Returns an outline's `control box'. The control box encloses all */ +/* the outline's points, including Bezier control points. Though it */ +/* coincides with the exact bounding box for most glyphs, it can be */ +/* slightly larger in some situations (like when rotating an outline */ +/* which contains Bezier outside arcs). */ +/* */ +/* Computing the control box is very fast, while getting the bounding */ +/* box can take much more time as it needs to walk over all segments */ +/* and arcs in the outline. To get the latter, you can use the */ +/* `ftbbox' component which is dedicated to this single task. */ +/* */ +/* */ +/* outline :: A pointer to the source outline descriptor. */ +/* */ +/* */ +/* cbox :: The outline's control box. */ +/* */ +/* */ +/* Yes. */ +/* */ +FT_EXPORT_FUNC( void ) FT_Outline_Get_CBox( FT_Outline * outline, + FT_BBox * cbox ) +{ + FT_Pos xMin, yMin, xMax, yMax; + + + if ( outline && cbox ) { + if ( outline->n_points == 0 ) { + xMin = 0; + yMin = 0; + xMax = 0; + yMax = 0; + } else + { + FT_Vector* vec = outline->points; + FT_Vector* limit = vec + outline->n_points; + + + xMin = xMax = vec->x; + yMin = yMax = vec->y; + vec++; + + for ( ; vec < limit; vec++ ) + { + FT_Pos x, y; + + + x = vec->x; + if ( x < xMin ) { + xMin = x; + } + if ( x > xMax ) { + xMax = x; + } + + y = vec->y; + if ( y < yMin ) { + yMin = y; + } + if ( y > yMax ) { + yMax = y; + } + } + } + cbox->xMin = xMin; + cbox->xMax = xMax; + cbox->yMin = yMin; + cbox->yMax = yMax; + } +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_Translate */ +/* */ +/* */ +/* Applies a simple translation to the points of an outline. */ +/* */ +/* */ +/* outline :: A pointer to the target outline descriptor. */ +/* */ +/* xOffset :: The horizontal offset. */ +/* */ +/* yOffset :: The vertical offset. */ +/* */ +/* */ +/* Yes. */ +/* */ +FT_EXPORT_FUNC( void ) FT_Outline_Translate( FT_Outline * outline, + FT_Pos xOffset, + FT_Pos yOffset ) +{ + FT_UShort n; + FT_Vector* vec = outline->points; + + + for ( n = 0; n < outline->n_points; n++ ) + { + vec->x += xOffset; + vec->y += yOffset; + vec++; + } +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_Reverse */ +/* */ +/* */ +/* Reverses the drawing direction of an outline. This is used to */ +/* ensure consistent fill conventions for mirrored glyphs. */ +/* */ +/* */ +/* outline :: A pointer to the target outline descriptor. */ +/* */ +/* */ +/* This functions toggles the bit flag `ft_outline_reverse_fill' in */ +/* the outline's `flags' field. */ +/* */ +FT_EXPORT_FUNC( void ) FT_Outline_Reverse( FT_Outline * outline ) +{ + FT_UShort n; + FT_Int first, last; + + + first = 0; + + for ( n = 0; n < outline->n_contours; n++ ) + { + last = outline->contours[n]; + + /* reverse point table */ + { + FT_Vector* p = outline->points + first; + FT_Vector* q = outline->points + last; + FT_Vector swap; + + + while ( p < q ) + { + swap = *p; + *p = *q; + *q = swap; + p++; + q--; + } + } + + /* reverse tags table */ + { + char* p = outline->tags + first; + char* q = outline->tags + last; + char swap; + + + while ( p < q ) + { + swap = *p; + *p = *q; + *q = swap; + p++; + q--; + } + } + + first = last + 1; + } + + outline->flags ^= ft_outline_reverse_fill; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_Render */ +/* */ +/* */ +/* Renders an outline within a bitmap using the current scan-convert. */ +/* This functions uses an FT_Raster_Params structure as an argument, */ +/* allowing advanced features like direct composition, translucency, */ +/* etc. */ +/* */ +/* */ +/* library :: A handle to a FreeType library object. */ +/* */ +/* outline :: A pointer to the source outline descriptor. */ +/* */ +/* params :: A pointer to a FT_Raster_Params structure used to */ +/* describe the rendering operation. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* YES. Rendering is synchronized, so that concurrent calls to the */ +/* scan-line converter will be serialized. */ +/* */ +/* */ +/* You should know what you are doing and how FT_Raster_Params works */ +/* to use this function. */ +/* */ +/* The field `params.source' will be set to `outline' before the scan */ +/* converter is called, which means that the value you give to it is */ +/* actually ignored. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Outline_Render( FT_Library library, + FT_Outline * outline, + FT_Raster_Params * params ) +{ + FT_Error error; + FT_Bool update = 0; + FT_Renderer renderer; + FT_ListNode node; + + + if ( !library ) { + return FT_Err_Invalid_Library_Handle; + } + + if ( !params ) { + return FT_Err_Invalid_Argument; + } + + renderer = library->cur_renderer; + node = library->renderers.head; + + params->source = (void*)outline; + + error = FT_Err_Cannot_Render_Glyph; + while ( renderer ) + { + error = renderer->raster_render( renderer->raster, params ); + if ( !error || error != FT_Err_Cannot_Render_Glyph ) { + break; + } + + /* FT_Err_Cannot_Render_Glyph is returned if the render mode */ + /* is unsupported by the current renderer for this glyph image */ + /* format */ + + /* now, look for another renderer that supports the same */ + /* format */ + renderer = FT_Lookup_Renderer( library, ft_glyph_format_outline, + &node ); + update = 1; + } + + /* if we changed the current renderer for the glyph image format */ + /* we need to select it as the next current one */ + if ( !error && update && renderer ) { + FT_Set_Renderer( library, renderer, 0, 0 ); + } + + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_Get_Bitmap */ +/* */ +/* */ +/* Renders an outline within a bitmap. The outline's image is simply */ +/* OR-ed to the target bitmap. */ +/* */ +/* */ +/* library :: A handle to a FreeType library object. */ +/* */ +/* outline :: A pointer to the source outline descriptor. */ +/* */ +/* map :: A pointer to the target bitmap descriptor. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* YES. Rendering is synchronized, so that concurrent calls to the */ +/* scan-line converter will be serialized. */ +/* */ +/* */ +/* This function does NOT CREATE the bitmap, it only renders an */ +/* outline image within the one you pass to it! */ +/* */ +/* It will use the raster correponding to the default glyph format. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_Outline_Get_Bitmap( FT_Library library, + FT_Outline * outline, + FT_Bitmap * bitmap ) +{ + FT_Raster_Params params; + + + if ( !bitmap ) { + return FT_Err_Invalid_Argument; + } + + /* other checks are delayed to FT_Outline_Render() */ + + params.target = bitmap; + params.flags = 0; + + if ( bitmap->pixel_mode == ft_pixel_mode_grays ) { + params.flags |= ft_raster_flag_aa; + } + + return FT_Outline_Render( library, outline, ¶ms ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Vector_Transform */ +/* */ +/* */ +/* Transforms a single vector through a 2x2 matrix. */ +/* */ +/* */ +/* vector :: The target vector to transform. */ +/* */ +/* */ +/* matrix :: A pointer to the source 2x2 matrix. */ +/* */ +/* */ +/* Yes. */ +/* */ +/* */ +/* The result is undefined if either `vector' or `matrix' is invalid. */ +/* */ +FT_EXPORT_FUNC( void ) FT_Vector_Transform( FT_Vector * vector, + FT_Matrix * matrix ) +{ + FT_Pos xz, yz; + + + if ( !vector || !matrix ) { + return; + } + + xz = FT_MulFix( vector->x, matrix->xx ) + + FT_MulFix( vector->y, matrix->xy ); + + yz = FT_MulFix( vector->x, matrix->yx ) + + FT_MulFix( vector->y, matrix->yy ); + + vector->x = xz; + vector->y = yz; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_Transform */ +/* */ +/* */ +/* Applies a simple 2x2 matrix to all of an outline's points. Useful */ +/* for applying rotations, slanting, flipping, etc. */ +/* */ +/* */ +/* outline :: A pointer to the target outline descriptor. */ +/* */ +/* matrix :: A pointer to the transformation matrix. */ +/* */ +/* */ +/* Yes. */ +/* */ +/* */ +/* You can use FT_Outline_Translate() if you need to translate the */ +/* outline's points. */ +/* */ +FT_EXPORT_FUNC( void ) FT_Outline_Transform( FT_Outline * outline, + FT_Matrix * matrix ) +{ + FT_Vector* vec = outline->points; + FT_Vector* limit = vec + outline->n_points; + + + for ( ; vec < limit; vec++ ) + FT_Vector_Transform( vec, matrix ); +} + +/* END */ diff --git a/src/ft2/ftoutln.h b/src/ft2/ftoutln.h new file mode 100644 index 0000000..3ca36ba --- /dev/null +++ b/src/ft2/ftoutln.h @@ -0,0 +1,344 @@ +/***************************************************************************/ +/* */ +/* ftoutln.h */ +/* */ +/* Support for the FT_Outline type used to store glyph shapes of */ +/* most scalable font formats (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef FTOUTLN_H +#define FTOUTLN_H + + +#include "freetype.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_Decompose */ +/* */ +/* */ +/* Walks over an outline's structure to decompose it into individual */ +/* segments and Bezier arcs. This function is also able to emit */ +/* `move to' and `close to' operations to indicate the start and end */ +/* of new contours in the outline. */ +/* */ +/* */ +/* outline :: A pointer to the source target. */ +/* */ +/* interface :: A table of `emitters', i.e,. function pointers called */ +/* during decomposition to indicate path operations. */ +/* */ +/* user :: A typeless pointer which is passed to each emitter */ +/* during the decomposition. It can be used to store */ +/* the state during the decomposition. */ +/* */ +/* */ +/* FreeType error code. 0 means sucess. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Outline_Decompose( + FT_Outline * outline, + FT_Outline_Funcs * sinterface, + void* user ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_New */ +/* */ +/* */ +/* Creates a new outline of a given size. */ +/* */ +/* */ +/* library :: A handle to the library object from where the */ +/* outline is allocated. Note however that the new */ +/* outline will NOT necessarily be FREED, when */ +/* destroying the library, by FT_Done_FreeType(). */ +/* */ +/* numPoints :: The maximal number of points within the outline. */ +/* */ +/* numContours :: The maximal number of contours within the outline. */ +/* */ +/* */ +/* outline :: A handle to the new outline. NULL in case of */ +/* error. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* No. */ +/* */ +/* */ +/* The reason why this function takes a `library' parameter is simply */ +/* to use the library's memory allocator. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Outline_New( FT_Library library, + FT_UInt numPoints, + FT_Int numContours, + FT_Outline * outline ); + + +FT_EXPORT_DEF( FT_Error ) FT_Outline_New_Internal( + FT_Memory memory, + FT_UInt numPoints, + FT_Int numContours, + FT_Outline * outline ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_Done */ +/* */ +/* */ +/* Destroys an outline created with FT_Outline_New(). */ +/* */ +/* */ +/* library :: A handle of the library object used to allocate the */ +/* outline. */ +/* */ +/* outline :: A pointer to the outline object to be discarded. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* No. */ +/* */ +/* */ +/* If the outline's `owner' field is not set, only the outline */ +/* descriptor will be released. */ +/* */ +/* The reason why this function takes an `outline' parameter is */ +/* simply to use FT_Free(). */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Outline_Done( FT_Library library, + FT_Outline * outline ); + + +FT_EXPORT_DEF( FT_Error ) FT_Outline_Done_Internal( FT_Memory memory, + FT_Outline * outline ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_Get_CBox */ +/* */ +/* */ +/* Returns an outline's `control box'. The control box encloses all */ +/* the outline's points, including Bezier control points. Though it */ +/* coincides with the exact bounding box for most glyphs, it can be */ +/* slightly larger in some situations (like when rotating an outline */ +/* which contains Bezier outside arcs). */ +/* */ +/* Computing the control box is very fast, while getting the bounding */ +/* box can take much more time as it needs to walk over all segments */ +/* and arcs in the outline. To get the latter, you can use the */ +/* `ftbbox' component which is dedicated to this single task. */ +/* */ +/* */ +/* outline :: A pointer to the source outline descriptor. */ +/* */ +/* */ +/* cbox :: The outline's control box. */ +/* */ +/* */ +/* Yes. */ +/* */ +FT_EXPORT_DEF( void ) FT_Outline_Get_CBox( FT_Outline * outline, + FT_BBox * cbox ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_Translate */ +/* */ +/* */ +/* Applies a simple translation to the points of an outline. */ +/* */ +/* */ +/* outline :: A pointer to the target outline descriptor. */ +/* */ +/* xOffset :: The horizontal offset. */ +/* */ +/* yOffset :: The vertical offset. */ +/* */ +/* */ +/* Yes. */ +/* */ +FT_EXPORT_DEF( void ) FT_Outline_Translate( FT_Outline * outline, + FT_Pos xOffset, + FT_Pos yOffset ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_Copy */ +/* */ +/* */ +/* Copies an outline into another one. Both objects must have the */ +/* same sizes (number of points & number of contours) when this */ +/* function is called. */ +/* */ +/* */ +/* source :: A handle to the source outline. */ +/* */ +/* */ +/* target :: A handle to the target outline. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Outline_Copy( FT_Outline * source, + FT_Outline * target ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Vector_Transform */ +/* */ +/* */ +/* Transforms a single vector through a 2x2 matrix. */ +/* */ +/* */ +/* vector :: The target vector to transform. */ +/* */ +/* */ +/* matrix :: A pointer to the source 2x2 matrix. */ +/* */ +/* */ +/* Yes. */ +/* */ +/* */ +/* The result is undefined if either `vector' or `matrix' is invalid. */ +/* */ +FT_EXPORT_DEF( void ) FT_Outline_Transform( FT_Outline * outline, + FT_Matrix * matrix ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_Reverse */ +/* */ +/* */ +/* Reverses the drawing direction of an outline. This is used to */ +/* ensure consistent fill conventions for mirrored glyphs. */ +/* */ +/* */ +/* outline :: A pointer to the target outline descriptor. */ +/* */ +/* */ +/* This functions toggles the bit flag `ft_outline_reverse_fill' in */ +/* the outline's `flags' field. */ +/* */ +/* It shouldn't be used by a normal client application, unless it */ +/* knows what it is doing. */ +/* */ +FT_EXPORT_DEF( void ) FT_Outline_Reverse( FT_Outline * outline ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_Get_Bitmap */ +/* */ +/* */ +/* Renders an outline within a bitmap. The outline's image is simply */ +/* OR-ed to the target bitmap. */ +/* */ +/* */ +/* library :: A handle to a FreeType library object. */ +/* */ +/* outline :: A pointer to the source outline descriptor. */ +/* */ +/* map :: A pointer to the target bitmap descriptor. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* YES. Rendering is synchronized, so that concurrent calls to the */ +/* scan-line converter will be serialized. */ +/* */ +/* */ +/* This function does NOT CREATE the bitmap, it only renders an */ +/* outline image within the one you pass to it! */ +/* */ +/* It will use the raster correponding to the default glyph format. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Outline_Get_Bitmap( FT_Library library, + FT_Outline * outline, + FT_Bitmap * bitmap ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Outline_Render */ +/* */ +/* */ +/* Renders an outline within a bitmap using the current scan-convert. */ +/* This functions uses an FT_Raster_Params structure as an argument, */ +/* allowing advanced features like direct composition, translucency, */ +/* etc. */ +/* */ +/* */ +/* library :: A handle to a FreeType library object. */ +/* */ +/* outline :: A pointer to the source outline descriptor. */ +/* */ +/* params :: A pointer to a FT_Raster_Params structure used to */ +/* describe the rendering operation. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* YES. Rendering is synchronized, so that concurrent calls to the */ +/* scan-line converter will be serialized. */ +/* */ +/* */ +/* You should know what you are doing and how FT_Raster_Params works */ +/* to use this function. */ +/* */ +/* The field `params.source' will be set to `outline' before the scan */ +/* converter is called, which means that the value you give to it is */ +/* actually ignored. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Outline_Render( FT_Library library, + FT_Outline * outline, + FT_Raster_Params * params ); + + +#ifdef __cplusplus +} +#endif + + +#endif /* FTOUTLN_H */ + + +/* END */ diff --git a/src/ft2/ftraster.c b/src/ft2/ftraster.c new file mode 100644 index 0000000..8c17d0d --- /dev/null +++ b/src/ft2/ftraster.c @@ -0,0 +1,3251 @@ +/***************************************************************************/ +/* */ +/* ftraster.c */ +/* */ +/* The FreeType glyph rasterizer (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + +/*************************************************************************/ +/* */ +/* This is a rewrite of the FreeType 1.x scan-line converter */ +/* */ +/*************************************************************************/ + + +#include "ftraster.h" +#include "ftcalc.h" /* for FT_MulDiv() only */ + + +/*************************************************************************/ +/* */ +/* A simple technical note on how the raster works */ +/* ----------------------------------------------- */ +/* */ +/* Converting an outline into a bitmap is achieved in several steps: */ +/* */ +/* 1 - Decomposing the outline into successive `profiles'. Each */ +/* profile is simply an array of scanline intersections on a given */ +/* dimension. A profile's main attributes are */ +/* */ +/* o its scanline position boundaries, i.e. `Ymin' and `Ymax'. */ +/* */ +/* o an array of intersection coordinates for each scanline */ +/* between `Ymin' and `Ymax'. */ +/* */ +/* o a direction, indicating whether it was built going `up' or */ +/* `down', as this is very important for filling rules. */ +/* */ +/* 2 - Sweeping the target map's scanlines in order to compute segment */ +/* `spans' which are then filled. Additionally, this pass */ +/* performs drop-out control. */ +/* */ +/* The outline data is parsed during step 1 only. The profiles are */ +/* built from the bottom of the render pool, used as a stack. The */ +/* following graphics shows the profile list under construction: */ +/* */ +/* ____________________________________________________________ _ _ */ +/* | | | | | */ +/* | profile | coordinates for | profile | coordinates for |--> */ +/* | 1 | profile 1 | 2 | profile 2 |--> */ +/* |_________|___________________|_________|_________________|__ _ _ */ +/* */ +/* ^ ^ */ +/* | | */ +/* start of render pool top */ +/* */ +/* The top of the profile stack is kept in the `top' variable. */ +/* */ +/* As you can see, a profile record is pushed on top of the render */ +/* pool, which is then followed by its coordinates/intersections. If */ +/* a change of direction is detected in the outline, a new profile is */ +/* generated until the end of the outline. */ +/* */ +/* Note that when all profiles have been generated, the function */ +/* Finalize_Profile_Table() is used to record, for each profile, its */ +/* bottom-most scanline as well as the scanline above its upmost */ +/* boundary. These positions are called `y-turns' because they (sort */ +/* of) correspond to local extrema. They are stored in a sorted list */ +/* built from the top of the render pool as a downwards stack: */ +/* */ +/* _ _ _______________________________________ */ +/* | | */ +/* <--| sorted list of | */ +/* <--| extrema scanlines | */ +/* _ _ __________________|____________________| */ +/* */ +/* ^ ^ */ +/* | | */ +/* maxBuff sizeBuff = end of pool */ +/* */ +/* This list is later used during the sweep phase in order to */ +/* optimize performance (see technical note on the sweep below). */ +/* */ +/* Of course, the raster detects whether the two stacks collide and */ +/* handles the situation propertly. */ +/* */ +/*************************************************************************/ + + +/*************************************************************************/ +/*************************************************************************/ +/** **/ +/** CONFIGURATION MACROS **/ +/** **/ +/*************************************************************************/ +/*************************************************************************/ + +/* define DEBUG_RASTER if you want to compile a debugging version */ +#define xxxDEBUG_RASTER + +/* The default render pool size in bytes */ +#define RASTER_RENDER_POOL 8192 + +/* undefine FT_RASTER_OPTION_ANTI_ALIASING if you do not want to support */ +/* 5-levels anti-aliasing */ +#ifdef FT_CONFIG_OPTION_5_GRAY_LEVELS +#define FT_RASTER_OPTION_ANTI_ALIASING +#endif + +/* The size of the two-lines intermediate bitmap used */ +/* for anti-aliasing, in bytes. */ +#define RASTER_GRAY_LINES 2048 + + +/*************************************************************************/ +/*************************************************************************/ +/** **/ +/** OTHER MACROS (do not change) **/ +/** **/ +/*************************************************************************/ +/*************************************************************************/ + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_raster + + +#ifdef _STANDALONE_ + + +/* This macro is used to indicate that a function parameter is unused. */ +/* Its purpose is simply to reduce compiler warnings. Note also that */ +/* simply defining it as `(void)x' doesn't avoid warnings with certain */ +/* ANSI compilers (e.g. LCC). */ +#define FT_UNUSED( x ) ( x ) = ( x ) + +/* Disable the tracing mechanism for simplicity -- developers can */ +/* activate it easily by redefining these two macros. */ +#ifndef FT_ERROR +#define FT_ERROR( x ) do ;while ( 0 ) /* nothing */ +#endif + +#ifndef FT_TRACE +#define FT_TRACE( x ) do ;while ( 0 ) /* nothing */ +#endif + +#define Raster_Err_None 0 +#define Raster_Err_Not_Ini -1 +#define Raster_Err_Overflow -2 +#define Raster_Err_Neg_Height -3 +#define Raster_Err_Invalid -4 +#define Raster_Err_Unsupported -5 + + +#else /* _STANDALONE_ */ + + +#include "ftobjs.h" +#include "ftdebug.h" /* for FT_TRACE() and FT_ERROR() */ + +#define Raster_Err_None FT_Err_Ok +#define Raster_Err_Not_Ini FT_Err_Raster_Uninitialized +#define Raster_Err_Overflow FT_Err_Raster_Overflow +#define Raster_Err_Neg_Height FT_Err_Raster_Negative_Height +#define Raster_Err_Invalid FT_Err_Invalid_Outline +#define Raster_Err_Unsupported FT_Err_Unimplemented_Feature + + +#endif /* _STANDALONE_ */ + + +/* FMulDiv means `Fast MulDiv'; it is used in case where `b' is */ +/* typically a small value and the result of a*b is known to fit into */ +/* 32 bits. */ +#define FMulDiv( a, b, c ) ( ( a ) * ( b ) / ( c ) ) + +/* On the other hand, SMulDiv means `Slow MulDiv', and is used typically */ +/* for clipping computations. It simply uses the FT_MulDiv() function */ +/* defined in `ftcalc.h'. */ +#define SMulDiv FT_MulDiv + +/* The rasterizer is a very general purpose component; please leave */ +/* the following redefinitions there (you never know your target */ +/* environment). */ + +#ifndef TRUE +#define TRUE 1 +#endif + +#ifndef FALSE +#define FALSE 0 +#endif + +#ifndef NULL +#define NULL (void*)0 +#endif + +#ifndef SUCCESS +#define SUCCESS 0 +#endif + +#ifndef FAILURE +#define FAILURE 1 +#endif + + +#define MaxBezier 32 /* The maximum number of stacked Bezier curves. */ + /* Setting this constant to more than 32 is a */ + /* pure waste of space. */ + +#define Pixel_Bits 6 /* fractional bits of *input* coordinates */ + + +/*************************************************************************/ +/*************************************************************************/ +/** **/ +/** SIMPLE TYPE DECLARATIONS **/ +/** **/ +/*************************************************************************/ +/*************************************************************************/ + +typedef int Int; +typedef unsigned int UInt; +typedef short Short; +typedef unsigned short UShort, *PUShort; +typedef long Long, *PLong; +typedef unsigned long ULong; + +typedef unsigned char Byte, *PByte; +typedef char Bool; + +typedef struct TPoint_ +{ + Long x; + Long y; + +} TPoint; + + +typedef enum TFlow_ +{ + Flow_None = 0, + Flow_Up = 1, + Flow_Down = -1 + +} TFlow; + + +/* States of each line, arc, and profile */ +typedef enum TStates_ +{ + Unknown, + Ascending, + Descending, + Flat + +} TStates; + + +typedef struct TProfile_ TProfile; +typedef TProfile* PProfile; + +struct TProfile_ +{ + FT_F26Dot6 X; /* current coordinate during sweep */ + PProfile link; /* link to next profile - various purpose */ + PLong offset; /* start of profile's data in render pool */ + Int flow; /* Profile orientation: Asc/Descending */ + Long height; /* profile's height in scanlines */ + Long start; /* profile's starting scanline */ + + UShort countL; /* number of lines to step before this */ + /* profile becomes drawable */ + + PProfile next; /* next profile in same contour, used */ + /* during drop-out control */ +}; + +typedef PProfile TProfileList; +typedef PProfile* PProfileList; + + +/* Simple record used to implement a stack of bands, required */ +/* by the sub-banding mechanism */ +typedef struct TBand_ +{ + Short y_min; /* band's minimum */ + Short y_max; /* band's maximum */ + +} TBand; + + +#define AlignProfileSize \ + ( ( sizeof( TProfile ) + sizeof( long ) - 1 ) / sizeof( long ) ) + + +#ifdef TT_STATIC_RASTER + + +#define RAS_ARGS /* void */ +#define RAS_ARG /* void */ + +#define RAS_VARS /* void */ +#define RAS_VAR /* void */ + +#define FT_UNUSED_RASTER do ;while ( 0 ) + + +#else /* TT_STATIC_RASTER */ + + +#define RAS_ARGS TRaster_Instance * raster, +#define RAS_ARG TRaster_Instance * raster + +#define RAS_VARS raster, +#define RAS_VAR raster + +#define FT_UNUSED_RASTER FT_UNUSED( raster ) + + +#endif /* TT_STATIC_RASTER */ + + +typedef struct TRaster_Instance_ TRaster_Instance; + + +/* prototypes used for sweep function dispatch */ +typedef void Function_Sweep_Init ( RAS_ARGS Short * min, + Short * max ); + +typedef void Function_Sweep_Span ( RAS_ARGS Short y, + FT_F26Dot6 x1, + FT_F26Dot6 x2, + PProfile left, + PProfile right ); + +typedef void Function_Sweep_Step ( RAS_ARG ); + + +/* NOTE: These operations are only valid on 2's complement processors */ + +#define FLOOR( x ) ( ( x ) & - ras.precision ) +#define CEILING( x ) ( ( ( x ) + ras.precision - 1 ) & - ras.precision ) +#define TRUNC( x ) ( (signed long)( x ) >> ras.precision_bits ) +#define FRAC( x ) ( ( x ) & ( ras.precision - 1 ) ) +#define SCALED( x ) ( ( ( x ) << ras.scale_shift ) - ras.precision_half ) + +/* Note that I have moved the location of some fields in the */ +/* structure to ensure that the most used variables are used */ +/* at the top. Thus, their offset can be coded with less */ +/* opcodes, and it results in a smaller executable. */ + +struct TRaster_Instance_ +{ + Int precision_bits; /* precision related variables */ + Int precision; + Int precision_half; + Long precision_mask; + Int precision_shift; + Int precision_step; + Int precision_jitter; + + Int scale_shift; /* == precision_shift for bitmaps */ + /* == precision_shift+1 for pixmaps */ + + PLong buff; /* The profiles buffer */ + PLong sizeBuff; /* Render pool size */ + PLong maxBuff; /* Profiles buffer size */ + PLong top; /* Current cursor in buffer */ + + FT_Error error; + + Int numTurns; /* number of Y-turns in outline */ + + TPoint* arc; /* current Bezier arc pointer */ + + UShort bWidth; /* target bitmap width */ + PByte bTarget; /* target bitmap buffer */ + PByte gTarget; /* target pixmap buffer */ + + Long lastX, lastY, minY, maxY; + + UShort num_Profs; /* current number of profiles */ + + Bool fresh; /* signals a fresh new profile which */ + /* 'start' field must be completed */ + Bool joint; /* signals that the last arc ended */ + /* exactly on a scanline. Allows */ + /* removal of doublets */ + PProfile cProfile; /* current profile */ + PProfile fProfile; /* head of linked list of profiles */ + PProfile gProfile; /* contour's first profile in case */ + /* of impact */ + + TStates state; /* rendering state */ + + FT_Bitmap target; /* description of target bit/pixmap */ + FT_Outline outline; + + Long traceOfs; /* current offset in target bitmap */ + Long traceG; /* current offset in target pixmap */ + + Short traceIncr; /* sweep's increment in target bitmap */ + + Short gray_min_x; /* current min x during gray rendering */ + Short gray_max_x; /* current max x during gray rendering */ + + /* dispatch variables */ + + Function_Sweep_Init* Proc_Sweep_Init; + Function_Sweep_Span* Proc_Sweep_Span; + Function_Sweep_Span* Proc_Sweep_Drop; + Function_Sweep_Step* Proc_Sweep_Step; + + Byte dropOutControl; /* current drop_out control method */ + + Bool second_pass; /* indicates wether a horizontal pass */ + /* should be performed to control */ + /* drop-out accurately when calling */ + /* Render_Glyph. Note that there is */ + /* no horizontal pass during gray */ + /* rendering. */ + + TPoint arcs[2 * MaxBezier + 1]; /* The Bezier stack */ + + TBand band_stack[16]; /* band stack used for sub-banding */ + Int band_top; /* band stack top */ + + Int count_table[256]; /* Look-up table used to quickly count */ + /* set bits in a gray 2x2 cell */ + + void* memory; + +#ifdef FT_RASTER_OPTION_ANTI_ALIASING + + Byte grays[5]; /* Palette of gray levels used for */ + /* render. */ + + Byte gray_lines[RASTER_GRAY_LINES]; + /* Intermediate table used to render the */ + /* graylevels pixmaps. */ + /* gray_lines is a buffer holding two */ + /* monochrome scanlines */ + + Short gray_width; /* width in bytes of one monochrome */ + /* intermediate scanline of gray_lines. */ + /* Each gray pixel takes 2 bits long there */ + + /* The gray_lines must hold 2 lines, thus with size */ + /* in bytes of at least `gray_width*2'. */ + +#endif /* FT_RASTER_ANTI_ALIASING */ + +#if 0 + PByte flags; /* current flags table */ + PUShort outs; /* current outlines table */ + FT_Vector* coords; + + UShort nPoints; /* number of points in current glyph */ + Short nContours; /* number of contours in current glyph */ +#endif + +}; + + +#ifdef FT_CONFIG_OPTION_STATIC_RASTER + +static TRaster_Instance cur_ras; +#define ras cur_ras + +#else + +#define ras ( *raster ) + +#endif /* FT_CONFIG_OPTION_STATIC_RASTER */ + + +/*************************************************************************/ +/*************************************************************************/ +/** **/ +/** PROFILES COMPUTATION **/ +/** **/ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* Set_High_Precision */ +/* */ +/* */ +/* Sets precision variables according to param flag. */ +/* */ +/* */ +/* High :: Set to True for high precision (typically for ppem < 18), */ +/* false otherwise. */ +/* */ +static +void Set_High_Precision( RAS_ARGS Int High ) { + if ( High ) { + ras.precision_bits = 10; + ras.precision_step = 128; + ras.precision_jitter = 24; + } else + { + ras.precision_bits = 6; + ras.precision_step = 32; + ras.precision_jitter = 2; + } + + FT_TRACE6( ( "Set_High_Precision(%s)\n", High ? "true" : "false" ) ); + + ras.precision = 1L << ras.precision_bits; + ras.precision_half = ras.precision / 2; + ras.precision_shift = ras.precision_bits - Pixel_Bits; + ras.precision_mask = -ras.precision; +} + + +/*************************************************************************/ +/* */ +/* */ +/* New_Profile */ +/* */ +/* */ +/* Creates a new profile in the render pool. */ +/* */ +/* */ +/* aState :: The state/orientation of the new profile. */ +/* */ +/* */ +/* SUCCESS on success. FAILURE in case of overflow or of incoherent */ +/* profile. */ +/* */ +static +Bool New_Profile( RAS_ARGS TStates aState ) { + if ( !ras.fProfile ) { + ras.cProfile = (PProfile)ras.top; + ras.fProfile = ras.cProfile; + ras.top += AlignProfileSize; + } + + if ( ras.top >= ras.maxBuff ) { + ras.error = Raster_Err_Overflow; + return FAILURE; + } + + switch ( aState ) + { + case Ascending: + ras.cProfile->flow = Flow_Up; + FT_TRACE6( ( "New ascending profile = %lx\n", (long)ras.cProfile ) ); + break; + + case Descending: + ras.cProfile->flow = Flow_Down; + FT_TRACE6( ( "New descending profile = %lx\n", (long)ras.cProfile ) ); + break; + + default: + FT_ERROR( ( "New_Profile: invalid profile direction!\n" ) ); + ras.error = Raster_Err_Invalid; + return FAILURE; + } + + ras.cProfile->start = 0; + ras.cProfile->height = 0; + ras.cProfile->offset = ras.top; + ras.cProfile->link = (PProfile)0; + ras.cProfile->next = (PProfile)0; + + if ( !ras.gProfile ) { + ras.gProfile = ras.cProfile; + } + + ras.state = aState; + ras.fresh = TRUE; + ras.joint = FALSE; + + return SUCCESS; +} + + +/*************************************************************************/ +/* */ +/* */ +/* End_Profile */ +/* */ +/* */ +/* Finalizes the current profile. */ +/* */ +/* */ +/* SUCCESS on success. FAILURE in case of overflow or incoherency. */ +/* */ +static +Bool End_Profile( RAS_ARG ) { + Long h; + PProfile oldProfile; + + + h = ras.top - ras.cProfile->offset; + + if ( h < 0 ) { + FT_ERROR( ( "End_Profile: negative height encountered!\n" ) ); + ras.error = Raster_Err_Neg_Height; + return FAILURE; + } + + if ( h > 0 ) { + FT_TRACE6( ( "Ending profile %lx, start = %ld, height = %ld\n", + (long)ras.cProfile, ras.cProfile->start, h ) ); + + oldProfile = ras.cProfile; + ras.cProfile->height = h; + ras.cProfile = (PProfile)ras.top; + + ras.top += AlignProfileSize; + + ras.cProfile->height = 0; + ras.cProfile->offset = ras.top; + oldProfile->next = ras.cProfile; + ras.num_Profs++; + } + + if ( ras.top >= ras.maxBuff ) { + FT_TRACE1( ( "overflow in End_Profile\n" ) ); + ras.error = Raster_Err_Overflow; + return FAILURE; + } + + ras.joint = FALSE; + + return SUCCESS; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Insert_Y_Turn */ +/* */ +/* */ +/* Inserts a salient into the sorted list placed on top of the render */ +/* pool. */ +/* */ +/* */ +/* New y scanline position. */ +/* */ +/* */ +/* SUCCESS on success. FAILURE in case of overflow. */ +/* */ +static +Bool Insert_Y_Turn( RAS_ARGS Int y ) { + PLong y_turns; + Int y2, n; + + + n = ras.numTurns - 1; + y_turns = ras.sizeBuff - ras.numTurns; + + /* look for first y value that is <= */ + while ( n >= 0 && y < y_turns[n] ) + n--; + + /* if it is <, simply insert it, ignore if == */ + if ( n >= 0 && y > y_turns[n] ) { + while ( n >= 0 ) + { + y2 = y_turns[n]; + y_turns[n] = y; + y = y2; + n--; + } + } + + if ( n < 0 ) { + if ( ras.maxBuff <= ras.top ) { + ras.error = Raster_Err_Overflow; + return FAILURE; + } + ras.maxBuff--; + ras.numTurns++; + ras.sizeBuff[-ras.numTurns] = y; + } + + return SUCCESS; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Finalize_Profile_Table */ +/* */ +/* */ +/* Adjusts all links in the profiles list. */ +/* */ +/* */ +/* SUCCESS on success. FAILURE in case of overflow. */ +/* */ +static +Bool Finalize_Profile_Table( RAS_ARG ) { + Int bottom, top; + UShort n; + PProfile p; + + + n = ras.num_Profs; + + if ( n > 1 ) { + p = ras.fProfile; + while ( n > 0 ) + { + if ( n > 1 ) { + p->link = (PProfile)( p->offset + p->height ); + } else { + p->link = NULL; + } + + switch ( p->flow ) + { + case Flow_Down: + bottom = p->start - p->height + 1; + top = p->start; + p->start = bottom; + p->offset += p->height - 1; + break; + + case Flow_Up: + default: + bottom = p->start; + top = p->start + p->height - 1; + } + + if ( Insert_Y_Turn( RAS_VARS bottom ) || + Insert_Y_Turn( RAS_VARS top + 1 ) ) { + return FAILURE; + } + + p = p->link; + n--; + } + } else { + ras.fProfile = NULL; + } + + return SUCCESS; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Split_Conic */ +/* */ +/* */ +/* Subdivides one conic Bezier into two joint sub-arcs in the Bezier */ +/* stack. */ +/* */ +/* */ +/* None (subdivided Bezier is taken from the top of the stack). */ +/* */ +/* */ +/* This routine is the `beef' of this component. It is _the_ inner */ +/* loop that should be optimized to hell to get the best performance. */ +/* */ +static +void Split_Conic( TPoint* base ) { + Long a, b; + + + base[4].x = base[2].x; + b = base[1].x; + a = base[3].x = ( base[2].x + b ) / 2; + b = base[1].x = ( base[0].x + b ) / 2; + base[2].x = ( a + b ) / 2; + + base[4].y = base[2].y; + b = base[1].y; + a = base[3].y = ( base[2].y + b ) / 2; + b = base[1].y = ( base[0].y + b ) / 2; + base[2].y = ( a + b ) / 2; + + /* hand optimized. gcc doesn't seem to be too good at common */ + /* expression substitution and instruction scheduling ;-) */ +} + + +/*************************************************************************/ +/* */ +/* */ +/* Split_Cubic */ +/* */ +/* */ +/* Subdivides a third-order Bezier arc into two joint sub-arcs in the */ +/* Bezier stack. */ +/* */ +/* */ +/* This routine is the `beef' of the component. It is one of _the_ */ +/* inner loops that should be optimized like hell to get the best */ +/* performance. */ +/* */ +static +void Split_Cubic( TPoint* base ) { + Long a, b, c, d; + + + base[6].x = base[3].x; + c = base[1].x; + d = base[2].x; + base[1].x = a = ( base[0].x + c + 1 ) >> 1; + base[5].x = b = ( base[3].x + d + 1 ) >> 1; + c = ( c + d + 1 ) >> 1; + base[2].x = a = ( a + c + 1 ) >> 1; + base[4].x = b = ( b + c + 1 ) >> 1; + base[3].x = ( a + b + 1 ) >> 1; + + base[6].y = base[3].y; + c = base[1].y; + d = base[2].y; + base[1].y = a = ( base[0].y + c + 1 ) >> 1; + base[5].y = b = ( base[3].y + d + 1 ) >> 1; + c = ( c + d + 1 ) >> 1; + base[2].y = a = ( a + c + 1 ) >> 1; + base[4].y = b = ( b + c + 1 ) >> 1; + base[3].y = ( a + b + 1 ) >> 1; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Line_Up */ +/* */ +/* */ +/* Computes the x-coordinates of an ascending line segment and stores */ +/* them in the render pool. */ +/* */ +/* */ +/* x1 :: The x-coordinate of the segment's start point. */ +/* */ +/* y1 :: The y-coordinate of the segment's start point. */ +/* */ +/* x2 :: The x-coordinate of the segment's end point. */ +/* */ +/* y2 :: The y-coordinate of the segment's end point. */ +/* */ +/* miny :: A lower vertical clipping bound value. */ +/* */ +/* maxy :: An upper vertical clipping bound value. */ +/* */ +/* */ +/* SUCCESS on success, FAILURE on render pool overflow. */ +/* */ +static +Bool Line_Up( RAS_ARGS Long x1, + Long y1, + Long x2, + Long y2, + Long miny, + Long maxy ) { + Long Dx, Dy; + Int e1, e2, f1, f2, size; /* XXX: is `Short' sufficient? */ + Long Ix, Rx, Ax; + + PLong top; + + + Dx = x2 - x1; + Dy = y2 - y1; + + if ( Dy <= 0 || y2 < miny || y1 > maxy ) { + return SUCCESS; + } + + if ( y1 < miny ) { + /* Take care: miny-y1 can be a very large value; we use */ + /* a slow MulDiv function to avoid clipping bugs */ + x1 += SMulDiv( Dx, miny - y1, Dy ); + e1 = TRUNC( miny ); + f1 = 0; + } else + { + e1 = TRUNC( y1 ); + f1 = FRAC( y1 ); + } + + if ( y2 > maxy ) { + /* x2 += FMulDiv( Dx, maxy - y2, Dy ); UNNECESSARY */ + e2 = TRUNC( maxy ); + f2 = 0; + } else + { + e2 = TRUNC( y2 ); + f2 = FRAC( y2 ); + } + + if ( f1 > 0 ) { + if ( e1 == e2 ) { + return SUCCESS; + } else + { + x1 += FMulDiv( Dx, ras.precision - f1, Dy ); + e1 += 1; + } + } else + if ( ras.joint ) { + ras.top--; + ras.joint = FALSE; + } + + ras.joint = ( f2 == 0 ); + + if ( ras.fresh ) { + ras.cProfile->start = e1; + ras.fresh = FALSE; + } + + size = e2 - e1 + 1; + if ( ras.top + size >= ras.maxBuff ) { + ras.error = Raster_Err_Overflow; + return FAILURE; + } + + if ( Dx > 0 ) { + Ix = ( ras.precision * Dx ) / Dy; + Rx = ( ras.precision * Dx ) % Dy; + Dx = 1; + } else + { + Ix = -( ( ras.precision * -Dx ) / Dy ); + Rx = ( ras.precision * -Dx ) % Dy; + Dx = -1; + } + + Ax = -Dy; + top = ras.top; + + while ( size > 0 ) + { + *top++ = x1; + + x1 += Ix; + Ax += Rx; + if ( Ax >= 0 ) { + Ax -= Dy; + x1 += Dx; + } + size--; + } + + ras.top = top; + return SUCCESS; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Line_Down */ +/* */ +/* */ +/* Computes the x-coordinates of an descending line segment and */ +/* stores them in the render pool. */ +/* */ +/* */ +/* x1 :: The x-coordinate of the segment's start point. */ +/* */ +/* y1 :: The y-coordinate of the segment's start point. */ +/* */ +/* x2 :: The x-coordinate of the segment's end point. */ +/* */ +/* y2 :: The y-coordinate of the segment's end point. */ +/* */ +/* miny :: A lower vertical clipping bound value. */ +/* */ +/* maxy :: An upper vertical clipping bound value. */ +/* */ +/* */ +/* SUCCESS on success, FAILURE on render pool overflow. */ +/* */ +static +Bool Line_Down( RAS_ARGS Long x1, + Long y1, + Long x2, + Long y2, + Long miny, + Long maxy ) { + Bool result, fresh; + + + fresh = ras.fresh; + + result = Line_Up( RAS_VARS x1, -y1, x2, -y2, -maxy, -miny ); + + if ( fresh && !ras.fresh ) { + ras.cProfile->start = -ras.cProfile->start; + } + + return result; +} + + +/* A function type describing the functions used to split Bezier arcs */ +typedef void ( *TSplitter )( TPoint* base ); + + +/*************************************************************************/ +/* */ +/* */ +/* Bezier_Up */ +/* */ +/* */ +/* Computes the x-coordinates of an ascending Bezier arc and stores */ +/* them in the render pool. */ +/* */ +/* */ +/* degree :: The degree of the Bezier arc (either 2 or 3). */ +/* */ +/* splitter :: The function to split Bezier arcs. */ +/* */ +/* miny :: A lower vertical clipping bound value. */ +/* */ +/* maxy :: An upper vertical clipping bound value. */ +/* */ +/* */ +/* SUCCESS on success, FAILURE on render pool overflow. */ +/* */ +static +Bool Bezier_Up( RAS_ARGS Int degree, + TSplitter splitter, + Long miny, + Long maxy ) { + Long y1, y2, e, e2, e0; + Short f1; + + TPoint* arc; + TPoint* start_arc; + + PLong top; + + + arc = ras.arc; + y1 = arc[degree].y; + y2 = arc[0].y; + top = ras.top; + + if ( y2 < miny || y1 > maxy ) { + goto Fin; + } + + e2 = FLOOR( y2 ); + + if ( e2 > maxy ) { + e2 = maxy; + } + + e0 = miny; + + if ( y1 < miny ) { + e = miny; + } else + { + e = CEILING( y1 ); + f1 = FRAC( y1 ); + e0 = e; + + if ( f1 == 0 ) { + if ( ras.joint ) { + top--; + ras.joint = FALSE; + } + + *top++ = arc[degree].x; + + e += ras.precision; + } + } + + if ( ras.fresh ) { + ras.cProfile->start = TRUNC( e0 ); + ras.fresh = FALSE; + } + + if ( e2 < e ) { + goto Fin; + } + + if ( ( top + TRUNC( e2 - e ) + 1 ) >= ras.maxBuff ) { + ras.top = top; + ras.error = Raster_Err_Overflow; + return FAILURE; + } + + start_arc = arc; + + while ( arc >= start_arc && e <= e2 ) + { + ras.joint = FALSE; + + y2 = arc[0].y; + + if ( y2 > e ) { + y1 = arc[degree].y; + if ( y2 - y1 >= ras.precision_step ) { + splitter( arc ); + arc += degree; + } else + { + *top++ = arc[degree].x + FMulDiv( arc[0].x - arc[degree].x, + e - y1, y2 - y1 ); + arc -= degree; + e += ras.precision; + } + } else + { + if ( y2 == e ) { + ras.joint = TRUE; + *top++ = arc[0].x; + + e += ras.precision; + } + arc -= degree; + } + } + +Fin: + ras.top = top; + ras.arc -= degree; + return SUCCESS; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Bezier_Down */ +/* */ +/* */ +/* Computes the x-coordinates of an descending Bezier arc and stores */ +/* them in the render pool. */ +/* */ +/* */ +/* degree :: The degree of the Bezier arc (either 2 or 3). */ +/* */ +/* splitter :: The function to split Bezier arcs. */ +/* */ +/* miny :: A lower vertical clipping bound value. */ +/* */ +/* maxy :: An upper vertical clipping bound value. */ +/* */ +/* */ +/* SUCCESS on success, FAILURE on render pool overflow. */ +/* */ +static +Bool Bezier_Down( RAS_ARGS Int degree, + TSplitter splitter, + Long miny, + Long maxy ) { + TPoint* arc = ras.arc; + Bool result, fresh; + + + arc[0].y = -arc[0].y; + arc[1].y = -arc[1].y; + arc[2].y = -arc[2].y; + if ( degree > 2 ) { + arc[3].y = -arc[3].y; + } + + fresh = ras.fresh; + + result = Bezier_Up( RAS_VARS degree, splitter, -maxy, -miny ); + + if ( fresh && !ras.fresh ) { + ras.cProfile->start = -ras.cProfile->start; + } + + arc[0].y = -arc[0].y; + return result; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Line_To */ +/* */ +/* */ +/* Injects a new line segment and adjusts Profiles list. */ +/* */ +/* */ +/* x :: The x-coordinate of the segment's end point (its start point */ +/* is stored in `LastX'). */ +/* */ +/* y :: The y-coordinate of the segment's end point (its start point */ +/* is stored in `LastY'). */ +/* */ +/* */ +/* SUCCESS on success, FAILURE on render pool overflow or incorrect */ +/* profile. */ +/* */ +static +Bool Line_To( RAS_ARGS Long x, + Long y ) { + /* First, detect a change of direction */ + + switch ( ras.state ) + { + case Unknown: + if ( y > ras.lastY ) { + if ( New_Profile( RAS_VARS Ascending ) ) { + return FAILURE; + } + } else + { + if ( y < ras.lastY ) { + if ( New_Profile( RAS_VARS Descending ) ) { + return FAILURE; + } + } + } + break; + + case Ascending: + if ( y < ras.lastY ) { + if ( End_Profile( RAS_VAR ) || + New_Profile( RAS_VARS Descending ) ) { + return FAILURE; + } + } + break; + + case Descending: + if ( y > ras.lastY ) { + if ( End_Profile( RAS_VAR ) || + New_Profile( RAS_VARS Ascending ) ) { + return FAILURE; + } + } + break; + + default: + ; + } + + /* Then compute the lines */ + + switch ( ras.state ) + { + case Ascending: + if ( Line_Up( RAS_VARS ras.lastX, ras.lastY, + x, y, ras.minY, ras.maxY ) ) { + return FAILURE; + } + break; + + case Descending: + if ( Line_Down( RAS_VARS ras.lastX, ras.lastY, + x, y, ras.minY, ras.maxY ) ) { + return FAILURE; + } + break; + + default: + ; + } + + ras.lastX = x; + ras.lastY = y; + + return SUCCESS; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Conic_To */ +/* */ +/* */ +/* Injects a new conic arc and adjusts the profile list. */ +/* */ +/* */ +/* cx :: The x-coordinate of the arc's new control point. */ +/* */ +/* cy :: The y-coordinate of the arc's new control point. */ +/* */ +/* x :: The x-coordinate of the arc's end point (its start point is */ +/* stored in `LastX'). */ +/* */ +/* y :: The y-coordinate of the arc's end point (its start point is */ +/* stored in `LastY'). */ +/* */ +/* */ +/* SUCCESS on success, FAILURE on render pool overflow or incorrect */ +/* profile. */ +/* */ +static +Bool Conic_To( RAS_ARGS Long cx, + Long cy, + Long x, + Long y ) { + Long y1, y2, y3, x3, ymin, ymax; + TStates state_bez; + + + ras.arc = ras.arcs; + ras.arc[2].x = ras.lastX; + ras.arc[2].y = ras.lastY; + ras.arc[1].x = cx; ras.arc[1].y = cy; + ras.arc[0].x = x; ras.arc[0].y = y; + + do + { + y1 = ras.arc[2].y; + y2 = ras.arc[1].y; + y3 = ras.arc[0].y; + x3 = ras.arc[0].x; + + /* first, categorize the Bezier arc */ + + if ( y1 <= y3 ) { + ymin = y1; + ymax = y3; + } else + { + ymin = y3; + ymax = y1; + } + + if ( y2 < ymin || y2 > ymax ) { + /* this arc has no given direction, split it! */ + Split_Conic( ras.arc ); + ras.arc += 2; + } else if ( y1 == y3 ) { + /* this arc is flat, ignore it and pop it from the Bezier stack */ + ras.arc -= 2; + } else + { + /* the arc is y-monotonous, either ascending or descending */ + /* detect a change of direction */ + state_bez = y1 < y3 ? Ascending : Descending; + if ( ras.state != state_bez ) { + /* finalize current profile if any */ + if ( ras.state != Unknown && + End_Profile( RAS_VAR ) ) { + goto Fail; + } + + /* create a new profile */ + if ( New_Profile( RAS_VARS state_bez ) ) { + goto Fail; + } + } + + /* now call the appropriate routine */ + if ( state_bez == Ascending ) { + if ( Bezier_Up( RAS_VARS 2, Split_Conic, ras.minY, ras.maxY ) ) { + goto Fail; + } + } else + if ( Bezier_Down( RAS_VARS 2, Split_Conic, ras.minY, ras.maxY ) ) { + goto Fail; + } + } + + } while ( ras.arc >= ras.arcs ); + + ras.lastX = x3; + ras.lastY = y3; + + return SUCCESS; + +Fail: + return FAILURE; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Cubic_To */ +/* */ +/* */ +/* Injects a new cubic arc and adjusts the profile list. */ +/* */ +/* */ +/* cx1 :: The x-coordinate of the arc's first new control point. */ +/* */ +/* cy1 :: The y-coordinate of the arc's first new control point. */ +/* */ +/* cx2 :: The x-coordinate of the arc's second new control point. */ +/* */ +/* cy2 :: The y-coordinate of the arc's second new control point. */ +/* */ +/* x :: The x-coordinate of the arc's end point (its start point is */ +/* stored in `LastX'). */ +/* */ +/* y :: The y-coordinate of the arc's end point (its start point is */ +/* stored in `LastY'). */ +/* */ +/* */ +/* SUCCESS on success, FAILURE on render pool overflow or incorrect */ +/* profile. */ +/* */ +static +Bool Cubic_To( RAS_ARGS Long cx1, + Long cy1, + Long cx2, + Long cy2, + Long x, + Long y ) { + Long y1, y2, y3, y4, x4, ymin1, ymax1, ymin2, ymax2; + TStates state_bez; + + + ras.arc = ras.arcs; + ras.arc[3].x = ras.lastX; + ras.arc[3].y = ras.lastY; + ras.arc[2].x = cx1; ras.arc[2].y = cy1; + ras.arc[1].x = cx2; ras.arc[1].y = cy2; + ras.arc[0].x = x; ras.arc[0].y = y; + + do + { + y1 = ras.arc[3].y; + y2 = ras.arc[2].y; + y3 = ras.arc[1].y; + y4 = ras.arc[0].y; + x4 = ras.arc[0].x; + + /* first, categorize the Bezier arc */ + + if ( y1 <= y4 ) { + ymin1 = y1; + ymax1 = y4; + } else + { + ymin1 = y4; + ymax1 = y1; + } + + if ( y2 <= y3 ) { + ymin2 = y2; + ymax2 = y3; + } else + { + ymin2 = y3; + ymax2 = y2; + } + + if ( ymin2 < ymin1 || ymax2 > ymax1 ) { + /* this arc has no given direction, split it! */ + Split_Cubic( ras.arc ); + ras.arc += 3; + } else if ( y1 == y4 ) { + /* this arc is flat, ignore it and pop it from the Bezier stack */ + ras.arc -= 3; + } else + { + state_bez = ( y1 <= y4 ) ? Ascending : Descending; + + /* detect a change of direction */ + if ( ras.state != state_bez ) { + if ( ras.state != Unknown && + End_Profile( RAS_VAR ) ) { + goto Fail; + } + + if ( New_Profile( RAS_VARS state_bez ) ) { + goto Fail; + } + } + + /* compute intersections */ + if ( state_bez == Ascending ) { + if ( Bezier_Up( RAS_VARS 3, Split_Cubic, ras.minY, ras.maxY ) ) { + goto Fail; + } + } else + if ( Bezier_Down( RAS_VARS 3, Split_Cubic, ras.minY, ras.maxY ) ) { + goto Fail; + } + } + + } while ( ras.arc >= ras.arcs ); + + ras.lastX = x4; + ras.lastY = y4; + + return SUCCESS; + +Fail: + return FAILURE; +} + + +#undef SWAP_ +#define SWAP_( x, y ) do \ + { \ + Long swap = x; \ + \ + \ + x = y; \ + y = swap; \ + } while ( 0 ) + + +/*************************************************************************/ +/* */ +/* */ +/* Decompose_Curve */ +/* */ +/* */ +/* Scans the outline arays in order to emit individual segments and */ +/* Beziers by calling Line_To() and Bezier_To(). It handles all */ +/* weird cases, like when the first point is off the curve, or when */ +/* there are simply no `on' points in the contour! */ +/* */ +/* */ +/* first :: The index of the first point in the contour. */ +/* */ +/* last :: The index of the last point in the contour. */ +/* */ +/* flipped :: If set, flip the direction of the curve. */ +/* */ +/* */ +/* SUCCESS on success, FAILURE on error. */ +/* */ +static +Bool Decompose_Curve( RAS_ARGS UShort first, + UShort last, + int flipped ) { + FT_Vector v_last; + FT_Vector v_control; + FT_Vector v_start; + + FT_Vector* points; + FT_Vector* point; + FT_Vector* limit; + char* tags; + + char tag; /* current point's state */ + + + points = ras.outline.points; + limit = points + last; + + v_start.x = SCALED( points[first].x ); + v_start.y = SCALED( points[first].y ); + v_last.x = SCALED( points[last].x ); + v_last.y = SCALED( points[last].y ); + + if ( flipped ) { + SWAP_( v_start.x, v_start.y ); + SWAP_( v_last.x, v_last.y ); + } + + v_control = v_start; + + point = points + first; + tags = ras.outline.tags + first; + tag = FT_CURVE_TAG( tags[0] ); + + /* A contour cannot start with a cubic control point! */ + if ( tag == FT_Curve_Tag_Cubic ) { + goto Invalid_Outline; + } + + /* check first point to determine origin */ + if ( tag == FT_Curve_Tag_Conic ) { + /* first point is conic control. Yes, this happens. */ + if ( FT_CURVE_TAG( ras.outline.tags[last] ) == FT_Curve_Tag_On ) { + /* start at last point if it is on the curve */ + v_start = v_last; + limit--; + } else + { + /* if both first and last points are conic, */ + /* start at their middle and record its position */ + /* for closure */ + v_start.x = ( v_start.x + v_last.x ) / 2; + v_start.y = ( v_start.y + v_last.y ) / 2; + + v_last = v_start; + } + point--; + tags--; + } + + ras.lastX = v_start.x; + ras.lastY = v_start.y; + + while ( point < limit ) + { + point++; + tags++; + + tag = FT_CURVE_TAG( tags[0] ); + + switch ( tag ) + { + case FT_Curve_Tag_On: /* emit a single line_to */ + { + Long x, y; + + + x = SCALED( point->x ); + y = SCALED( point->y ); + if ( flipped ) { + SWAP_( x, y ); + } + + if ( Line_To( RAS_VARS x, y ) ) { + goto Fail; + } + continue; + } + + case FT_Curve_Tag_Conic: /* consume conic arcs */ + v_control.x = SCALED( point[0].x ); + v_control.y = SCALED( point[0].y ); + + if ( flipped ) { + SWAP_( v_control.x, v_control.y ); + } + +Do_Conic: + if ( point < limit ) { + FT_Vector v_middle; + Long x, y; + + + point++; + tags++; + tag = FT_CURVE_TAG( tags[0] ); + + x = SCALED( point[0].x ); + y = SCALED( point[0].y ); + + if ( flipped ) { + SWAP_( x, y ); + } + + if ( tag == FT_Curve_Tag_On ) { + if ( Conic_To( RAS_VARS v_control.x, v_control.y, x, y ) ) { + goto Fail; + } + continue; + } + + if ( tag != FT_Curve_Tag_Conic ) { + goto Invalid_Outline; + } + + v_middle.x = ( v_control.x + x ) / 2; + v_middle.y = ( v_control.y + y ) / 2; + + if ( Conic_To( RAS_VARS v_control.x, v_control.y, + v_middle.x, v_middle.y ) ) { + goto Fail; + } + + v_control.x = x; + v_control.y = y; + + goto Do_Conic; + } + + if ( Conic_To( RAS_VARS v_control.x, v_control.y, + v_start.x, v_start.y ) ) { + goto Fail; + } + + goto Close; + + default: /* FT_Curve_Tag_Cubic */ + { + Long x1, y1, x2, y2, x3, y3; + + + if ( point + 1 > limit || + FT_CURVE_TAG( tags[1] ) != FT_Curve_Tag_Cubic ) { + goto Invalid_Outline; + } + + point += 2; + tags += 2; + + x1 = SCALED( point[-2].x ); + y1 = SCALED( point[-2].y ); + x2 = SCALED( point[-1].x ); + y2 = SCALED( point[-1].y ); + x3 = SCALED( point[ 0].x ); + y3 = SCALED( point[ 0].y ); + + if ( flipped ) { + SWAP_( x1, y1 ); + SWAP_( x2, y2 ); + SWAP_( x3, y3 ); + } + + if ( point <= limit ) { + if ( Cubic_To( RAS_VARS x1, y1, x2, y2, x3, y3 ) ) { + goto Fail; + } + continue; + } + + if ( Cubic_To( RAS_VARS x1, y1, x2, y2, v_start.x, v_start.y ) ) { + goto Fail; + } + goto Close; + } + } + } + + /* close the contour with a line segment */ + if ( Line_To( RAS_VARS v_start.x, v_start.y ) ) { + goto Fail; + } + +Close: + return SUCCESS; + +Invalid_Outline: + ras.error = Raster_Err_Invalid; + +Fail: + return FAILURE; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Convert_Glyph */ +/* */ +/* */ +/* Converts a glyph into a series of segments and arcs and makes a */ +/* profiles list with them. */ +/* */ +/* */ +/* flipped :: If set, flip the direction of curve. */ +/* */ +/* */ +/* SUCCESS on success, FAILURE if any error was encountered during */ +/* rendering. */ +/* */ +static +Bool Convert_Glyph( RAS_ARGS int flipped ) { + Short i; + UShort start; + + PProfile lastProfile; + + + ras.fProfile = NULL; + ras.joint = FALSE; + ras.fresh = FALSE; + + ras.maxBuff = ras.sizeBuff - AlignProfileSize; + + ras.numTurns = 0; + + ras.cProfile = (PProfile)ras.top; + ras.cProfile->offset = ras.top; + ras.num_Profs = 0; + + start = 0; + + for ( i = 0; i < ras.outline.n_contours; i++ ) + { + ras.state = Unknown; + ras.gProfile = NULL; + + if ( Decompose_Curve( RAS_VARS start, ras.outline.contours[i], flipped ) ) { + return FAILURE; + } + + start = ras.outline.contours[i] + 1; + + /* We must now see whether the extreme arcs join or not */ + if ( FRAC( ras.lastY ) == 0 && + ras.lastY >= ras.minY && + ras.lastY <= ras.maxY ) { + if ( ras.gProfile && ras.gProfile->flow == ras.cProfile->flow ) { + ras.top--; + } + } + /* Note that ras.gProfile can be nil if the contour was too small */ + /* to be drawn. */ + + lastProfile = ras.cProfile; + if ( End_Profile( RAS_VAR ) ) { + return FAILURE; + } + + /* close the `next profile in contour' linked list */ + if ( ras.gProfile ) { + lastProfile->next = ras.gProfile; + } + } + + if ( Finalize_Profile_Table( RAS_VAR ) ) { + return FAILURE; + } + + return ( ras.top < ras.maxBuff ? SUCCESS : FAILURE ); +} + + +/*************************************************************************/ +/*************************************************************************/ +/** **/ +/** SCAN-LINE SWEEPS AND DRAWING **/ +/** **/ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* Init_Linked */ +/* */ +/* Initializes an empty linked list. */ +/* */ +static +void Init_Linked( TProfileList* l ) { + *l = NULL; +} + + +/*************************************************************************/ +/* */ +/* InsNew */ +/* */ +/* Inserts a new profile in a linked list. */ +/* */ +static +void InsNew( PProfileList list, + PProfile profile ) { + PProfile *old, current; + Long x; + + + old = list; + current = *old; + x = profile->X; + + while ( current ) + { + if ( x < current->X ) { + break; + } + old = ¤t->link; + current = *old; + } + + profile->link = current; + *old = profile; +} + + +/*************************************************************************/ +/* */ +/* DelOld */ +/* */ +/* Removes an old profile from a linked list. */ +/* */ +static +void DelOld( PProfileList list, + PProfile profile ) { + PProfile *old, current; + + + old = list; + current = *old; + + while ( current ) + { + if ( current == profile ) { + *old = current->link; + return; + } + + old = ¤t->link; + current = *old; + } + + /* we should never get there, unless the profile was not part of */ + /* the list. */ +} + + +/*************************************************************************/ +/* */ +/* Update */ +/* */ +/* Update all X offsets of a drawing list. */ +/* */ +static +void Update( PProfile first ) { + PProfile current = first; + + + while ( current ) + { + current->X = *current->offset; + current->offset += current->flow; + current->height--; + current = current->link; + } +} + + +/*************************************************************************/ +/* */ +/* Sort */ +/* */ +/* Sorts a trace list. In 95%, the list is already sorted. We need */ +/* an algorithm which is fast in this case. Bubble sort is enough */ +/* and simple. */ +/* */ +static +void Sort( PProfileList list ) { + PProfile *old, current, next; + + + /* First, set the new X coordinate of each profile */ + Update( *list ); + + /* Then sort them */ + old = list; + current = *old; + + if ( !current ) { + return; + } + + next = current->link; + + while ( next ) + { + if ( current->X <= next->X ) { + old = ¤t->link; + current = *old; + + if ( !current ) { + return; + } + } else + { + *old = next; + current->link = next->link; + next->link = current; + + old = list; + current = *old; + } + + next = current->link; + } +} + + +/*************************************************************************/ +/* */ +/* Vertical Sweep Procedure Set */ +/* */ +/* These four routines are used during the vertical black/white sweep */ +/* phase by the generic Draw_Sweep() function. */ +/* */ +/*************************************************************************/ + +static +void Vertical_Sweep_Init( RAS_ARGS Short* min, + Short* max ) { + Long pitch = ras.target.pitch; + + FT_UNUSED( max ); + + + ras.traceIncr = ( Short ) - pitch; + ras.traceOfs = -*min * pitch; + if ( pitch > 0 ) { + ras.traceOfs += ( ras.target.rows - 1 ) * pitch; + } + + ras.gray_min_x = 0; + ras.gray_max_x = 0; +} + + +static +void Vertical_Sweep_Span( RAS_ARGS Short y, + FT_F26Dot6 x1, + FT_F26Dot6 x2, + PProfile left, + PProfile right ) { + Long e1, e2; + Short c1, c2; + Byte f1, f2; + Byte* target; + + FT_UNUSED( y ); + FT_UNUSED( left ); + FT_UNUSED( right ); + + + /* Drop-out control */ + + e1 = TRUNC( CEILING( x1 ) ); + + if ( x2 - x1 - ras.precision <= ras.precision_jitter ) { + e2 = e1; + } else { + e2 = TRUNC( FLOOR( x2 ) ); + } + + if ( e2 >= 0 && e1 < ras.bWidth ) { + if ( e1 < 0 ) { + e1 = 0; + } + if ( e2 >= ras.bWidth ) { + e2 = ras.bWidth - 1; + } + + c1 = (Short)( e1 >> 3 ); + c2 = (Short)( e2 >> 3 ); + + f1 = (unsigned char)0xFF >> ( e1 & 7 ); + f2 = ~( (unsigned char)0x7F >> ( e2 & 7 ) ); + + if ( ras.gray_min_x > c1 ) { + ras.gray_min_x = c1; + } + if ( ras.gray_max_x < c2 ) { + ras.gray_max_x = c2; + } + + target = ras.bTarget + ras.traceOfs + c1; + c2 -= c1; + + if ( c2 > 0 ) { + target[0] |= f1; + + /* memset() is slower than the following code on many platforms. */ + /* This is due to the fact that, in the vast majority of cases, */ + /* the span length in bytes is relatively small. */ + c2--; + while ( c2 > 0 ) + { + *( ++target ) = 0xFF; + c2--; + } + target[1] |= f2; + } else { + *target |= ( f1 & f2 ); + } + } +} + + +static +void Vertical_Sweep_Drop( RAS_ARGS Short y, + FT_F26Dot6 x1, + FT_F26Dot6 x2, + PProfile left, + PProfile right ) { + Long e1, e2; + Short c1, f1; + + + /* Drop-out control */ + + e1 = CEILING( x1 ); + e2 = FLOOR( x2 ); + + if ( e1 > e2 ) { + if ( e1 == e2 + ras.precision ) { + switch ( ras.dropOutControl ) + { + case 1: + e1 = e2; + break; + + case 4: + e1 = CEILING( ( x1 + x2 + 1 ) / 2 ); + break; + + case 2: + case 5: + /* Drop-out Control Rule #4 */ + + /* The spec is not very clear regarding rule #4. It */ + /* presents a method that is way too costly to implement */ + /* while the general idea seems to get rid of `stubs'. */ + /* */ + /* Here, we only get rid of stubs recognized if: */ + /* */ + /* upper stub: */ + /* */ + /* - P_Left and P_Right are in the same contour */ + /* - P_Right is the successor of P_Left in that contour */ + /* - y is the top of P_Left and P_Right */ + /* */ + /* lower stub: */ + /* */ + /* - P_Left and P_Right are in the same contour */ + /* - P_Left is the successor of P_Right in that contour */ + /* - y is the bottom of P_Left */ + /* */ + + /* FIXXXME: uncommenting this line solves the disappearing */ + /* bit problem in the `7' of verdana 10pts, but */ + /* makes a new one in the `C' of arial 14pts */ + +#if 0 + if ( x2 - x1 < ras.precision_half ) +#endif + { + /* upper stub test */ + if ( left->next == right && left->height <= 0 ) { + return; + } + + /* lower stub test */ + if ( right->next == left && left->start == y ) { + return; + } + } + + /* check that the rightmost pixel isn't set */ + + e1 = TRUNC( e1 ); + + c1 = (Short)( e1 >> 3 ); + f1 = e1 & 7; + + if ( e1 >= 0 && e1 < ras.bWidth && + ras.bTarget[ras.traceOfs + c1] & ( 0x80 >> f1 ) ) { + return; + } + + if ( ras.dropOutControl == 2 ) { + e1 = e2; + } else { + e1 = CEILING( ( x1 + x2 + 1 ) / 2 ); + } + + break; + + default: + return; /* unsupported mode */ + } + } else { + return; + } + } + + e1 = TRUNC( e1 ); + + if ( e1 >= 0 && e1 < ras.bWidth ) { + c1 = (Short)( e1 >> 3 ); + f1 = e1 & 7; + + if ( ras.gray_min_x > c1 ) { + ras.gray_min_x = c1; + } + if ( ras.gray_max_x < c1 ) { + ras.gray_max_x = c1; + } + + ras.bTarget[ras.traceOfs + c1] |= (char)( 0x80 >> f1 ); + } +} + + +static +void Vertical_Sweep_Step( RAS_ARG ) { + ras.traceOfs += ras.traceIncr; +} + + +/***********************************************************************/ +/* */ +/* Horizontal Sweep Procedure Set */ +/* */ +/* These four routines are used during the horizontal black/white */ +/* sweep phase by the generic Draw_Sweep() function. */ +/* */ +/***********************************************************************/ + +static +void Horizontal_Sweep_Init( RAS_ARGS Short* min, + Short* max ) { + /* nothing, really */ + FT_UNUSED( raster ); + FT_UNUSED( min ); + FT_UNUSED( max ); +} + + +static +void Horizontal_Sweep_Span( RAS_ARGS Short y, + FT_F26Dot6 x1, + FT_F26Dot6 x2, + PProfile left, + PProfile right ) { + Long e1, e2; + PByte bits; + Byte f1; + + FT_UNUSED( left ); + FT_UNUSED( right ); + + + if ( x2 - x1 < ras.precision ) { + e1 = CEILING( x1 ); + e2 = FLOOR( x2 ); + + if ( e1 == e2 ) { + bits = ras.bTarget + ( y >> 3 ); + f1 = (Byte)( 0x80 >> ( y & 7 ) ); + + e1 = TRUNC( e1 ); + + if ( e1 >= 0 && e1 < ras.target.rows ) { + PByte p; + + + p = bits - e1 * ras.target.pitch; + if ( ras.target.pitch > 0 ) { + p += ( ras.target.rows - 1 ) * ras.target.pitch; + } + + p[0] |= f1; + } + } + } +} + + +static +void Horizontal_Sweep_Drop( RAS_ARGS Short y, + FT_F26Dot6 x1, + FT_F26Dot6 x2, + PProfile left, + PProfile right ) { + Long e1, e2; + PByte bits; + Byte f1; + + + /* During the horizontal sweep, we only take care of drop-outs */ + + e1 = CEILING( x1 ); + e2 = FLOOR( x2 ); + + if ( e1 > e2 ) { + if ( e1 == e2 + ras.precision ) { + switch ( ras.dropOutControl ) + { + case 1: + e1 = e2; + break; + + case 4: + e1 = CEILING( ( x1 + x2 + 1 ) / 2 ); + break; + + case 2: + case 5: + + /* Drop-out Control Rule #4 */ + + /* The spec is not very clear regarding rule #4. It */ + /* presents a method that is way too costly to implement */ + /* while the general idea seems to get rid of `stubs'. */ + /* */ + + /* rightmost stub test */ + if ( left->next == right && left->height <= 0 ) { + return; + } + + /* leftmost stub test */ + if ( right->next == left && left->start == y ) { + return; + } + + /* check that the rightmost pixel isn't set */ + + e1 = TRUNC( e1 ); + + bits = ras.bTarget + ( y >> 3 ); + f1 = (Byte)( 0x80 >> ( y & 7 ) ); + + bits -= e1 * ras.target.pitch; + if ( ras.target.pitch > 0 ) { + bits += ( ras.target.rows - 1 ) * ras.target.pitch; + } + + if ( e1 >= 0 && + e1 < ras.target.rows && + *bits & f1 ) { + return; + } + + if ( ras.dropOutControl == 2 ) { + e1 = e2; + } else { + e1 = CEILING( ( x1 + x2 + 1 ) / 2 ); + } + + break; + + default: + return; /* unsupported mode */ + } + } else { + return; + } + } + + bits = ras.bTarget + ( y >> 3 ); + f1 = (Byte)( 0x80 >> ( y & 7 ) ); + + e1 = TRUNC( e1 ); + + if ( e1 >= 0 && e1 < ras.target.rows ) { + bits -= e1 * ras.target.pitch; + if ( ras.target.pitch > 0 ) { + bits += ( ras.target.rows - 1 ) * ras.target.pitch; + } + + bits[0] |= f1; + } +} + + +static +void Horizontal_Sweep_Step( RAS_ARG ) { + /* Nothing, really */ + FT_UNUSED( raster ); +} + + +#ifdef FT_RASTER_OPTION_ANTI_ALIASING + + +/*************************************************************************/ +/* */ +/* Vertical Gray Sweep Procedure Set */ +/* */ +/* These two routines are used during the vertical gray-levels sweep */ +/* phase by the generic Draw_Sweep() function. */ +/* */ +/* NOTES */ +/* */ +/* - The target pixmap's width *must* be a multiple of 4. */ +/* */ +/* - You have to use the function Vertical_Sweep_Span() for the gray */ +/* span call. */ +/* */ +/*************************************************************************/ + +static +void Vertical_Gray_Sweep_Init( RAS_ARGS Short* min, + Short* max ) { + Long pitch, byte_len; + + + *min = *min & - 2; + *max = ( *max + 3 ) & - 2; + + ras.traceOfs = 0; + pitch = ras.target.pitch; + byte_len = -pitch; + ras.traceIncr = (Short)byte_len; + ras.traceG = ( *min / 2 ) * byte_len; + + if ( pitch > 0 ) { + ras.traceG += ( ras.target.rows - 1 ) * pitch; + byte_len = -byte_len; + } + + ras.gray_min_x = (Short)byte_len; + ras.gray_max_x = -(Short)byte_len; +} + + +static +void Vertical_Gray_Sweep_Step( RAS_ARG ) { + Int c1, c2; + PByte pix, bit, bit2; + Int* count = ras.count_table; + Byte* grays; + + + ras.traceOfs += ras.gray_width; + + if ( ras.traceOfs > ras.gray_width ) { + pix = ras.gTarget + ras.traceG + ras.gray_min_x * 4; + grays = ras.grays; + + if ( ras.gray_max_x >= 0 ) { + Long last_pixel = ras.target.width - 1; + Int last_cell = last_pixel >> 2; + Int last_bit = last_pixel & 3; + Bool over = 0; + + + if ( ras.gray_max_x >= last_cell && last_bit != 3 ) { + ras.gray_max_x = last_cell - 1; + over = 1; + } + + if ( ras.gray_min_x < 0 ) { + ras.gray_min_x = 0; + } + + bit = ras.bTarget + ras.gray_min_x; + bit2 = bit + ras.gray_width; + + c1 = ras.gray_max_x - ras.gray_min_x; + + while ( c1 >= 0 ) + { + c2 = count[*bit] + count[*bit2]; + + if ( c2 ) { + pix[0] = grays[( c2 >> 12 ) & 0x000F]; + pix[1] = grays[( c2 >> 8 ) & 0x000F]; + pix[2] = grays[( c2 >> 4 ) & 0x000F]; + pix[3] = grays[ c2 & 0x000F]; + + *bit = 0; + *bit2 = 0; + } + + bit++; + bit2++; + pix += 4; + c1--; + } + + if ( over ) { + c2 = count[*bit] + count[*bit2]; + if ( c2 ) { + switch ( last_bit ) + { + case 2: + pix[2] = grays[( c2 >> 4 ) & 0x000F]; + case 1: + pix[1] = grays[( c2 >> 8 ) & 0x000F]; + default: + pix[0] = grays[( c2 >> 12 ) & 0x000F]; + } + + *bit = 0; + *bit2 = 0; + } + } + } + + ras.traceOfs = 0; + ras.traceG += ras.traceIncr; + + ras.gray_min_x = 32000; + ras.gray_max_x = -32000; + } +} + + +static +void Horizontal_Gray_Sweep_Span( RAS_ARGS Short y, + FT_F26Dot6 x1, + FT_F26Dot6 x2, + PProfile left, + PProfile right ) { + /* nothing, really */ + FT_UNUSED( raster ); + FT_UNUSED( y ); + FT_UNUSED( x1 ); + FT_UNUSED( x2 ); + FT_UNUSED( left ); + FT_UNUSED( right ); +} + + +static +void Horizontal_Gray_Sweep_Drop( RAS_ARGS Short y, + FT_F26Dot6 x1, + FT_F26Dot6 x2, + PProfile left, + PProfile right ) { + Long e1, e2; + PByte pixel; + Byte color; + + + /* During the horizontal sweep, we only take care of drop-outs */ + e1 = CEILING( x1 ); + e2 = FLOOR( x2 ); + + if ( e1 > e2 ) { + if ( e1 == e2 + ras.precision ) { + switch ( ras.dropOutControl ) + { + case 1: + e1 = e2; + break; + + case 4: + e1 = CEILING( ( x1 + x2 + 1 ) / 2 ); + break; + + case 2: + case 5: + + /* Drop-out Control Rule #4 */ + + /* The spec is not very clear regarding rule #4. It */ + /* presents a method that is way too costly to implement */ + /* while the general idea seems to get rid of `stubs'. */ + /* */ + + /* rightmost stub test */ + if ( left->next == right && left->height <= 0 ) { + return; + } + + /* leftmost stub test */ + if ( right->next == left && left->start == y ) { + return; + } + + if ( ras.dropOutControl == 2 ) { + e1 = e2; + } else { + e1 = CEILING( ( x1 + x2 + 1 ) / 2 ); + } + + break; + + default: + return; /* unsupported mode */ + } + } else { + return; + } + } + + if ( e1 >= 0 ) { + if ( x2 - x1 >= ras.precision_half ) { + color = ras.grays[2]; + } else { + color = ras.grays[1]; + } + + e1 = TRUNC( e1 ) / 2; + if ( e1 < ras.target.rows ) { + pixel = ras.gTarget - e1 * ras.target.pitch + y / 2; + if ( ras.target.pitch > 0 ) { + pixel += ( ras.target.rows - 1 ) * ras.target.pitch; + } + + if ( pixel[0] == ras.grays[0] ) { + pixel[0] = color; + } + } + } +} + + +#endif /* FT_RASTER_OPTION_ANTI_ALIASING */ + + +/*************************************************************************/ +/* */ +/* Generic Sweep Drawing routine */ +/* */ +/*************************************************************************/ + +static +Bool Draw_Sweep( RAS_ARG ) { + Short y, y_change, y_height; + + PProfile P, Q, P_Left, P_Right; + + Short min_Y, max_Y, top, bottom, dropouts; + + Long x1, x2, xs, e1, e2; + + TProfileList wait; + TProfileList draw_left, draw_right; + + + /* Init empty linked lists */ + + Init_Linked( &wait ); + + Init_Linked( &draw_left ); + Init_Linked( &draw_right ); + + /* first, compute min and max Y */ + + P = ras.fProfile; + max_Y = (Short)TRUNC( ras.minY ); + min_Y = (Short)TRUNC( ras.maxY ); + + while ( P ) + { + Q = P->link; + + bottom = (Short)P->start; + top = (Short)P->start + P->height - 1; + + if ( min_Y > bottom ) { + min_Y = bottom; + } + if ( max_Y < top ) { + max_Y = top; + } + + P->X = 0; + InsNew( &wait, P ); + + P = Q; + } + + /* Check the Y-turns */ + if ( ras.numTurns == 0 ) { + ras.error = Raster_Err_Invalid; + return FAILURE; + } + + /* Now inits the sweep */ + + ras.Proc_Sweep_Init( RAS_VARS & min_Y, &max_Y ); + + /* Then compute the distance of each profile from min_Y */ + + P = wait; + + while ( P ) + { + P->countL = P->start - min_Y; + P = P->link; + } + + /* Let's go */ + + y = min_Y; + y_height = 0; + + if ( ras.numTurns > 0 && + ras.sizeBuff[-ras.numTurns] == min_Y ) { + ras.numTurns--; + } + + while ( ras.numTurns > 0 ) + { + /* look in the wait list for new activations */ + + P = wait; + + while ( P ) + { + Q = P->link; + P->countL -= y_height; + if ( P->countL == 0 ) { + DelOld( &wait, P ); + + switch ( P->flow ) + { + case Flow_Up: + InsNew( &draw_left, P ); + break; + + case Flow_Down: + InsNew( &draw_right, P ); + break; + } + } + + P = Q; + } + + /* Sort the drawing lists */ + + Sort( &draw_left ); + Sort( &draw_right ); + + y_change = (Short)ras.sizeBuff[-ras.numTurns--]; + y_height = y_change - y; + + while ( y < y_change ) + { + /* Let's trace */ + + dropouts = 0; + + P_Left = draw_left; + P_Right = draw_right; + + while ( P_Left ) + { + x1 = P_Left->X; + x2 = P_Right->X; + + if ( x1 > x2 ) { + xs = x1; + x1 = x2; + x2 = xs; + } + + if ( x2 - x1 <= ras.precision ) { + e1 = FLOOR( x1 ); + e2 = CEILING( x2 ); + + if ( ras.dropOutControl != 0 && + ( e1 > e2 || e2 == e1 + ras.precision ) ) { + /* a drop out was detected */ + + P_Left->X = x1; + P_Right->X = x2; + + /* mark profile for drop-out processing */ + P_Left->countL = 1; + dropouts++; + + goto Skip_To_Next; + } + } + + ras.Proc_Sweep_Span( RAS_VARS y, x1, x2, P_Left, P_Right ); + +Skip_To_Next: + + P_Left = P_Left->link; + P_Right = P_Right->link; + } + + /* now perform the dropouts _after_ the span drawing -- */ + /* drop-outs processing has been moved out of the loop */ + /* for performance tuning */ + if ( dropouts > 0 ) { + goto Scan_DropOuts; + } + +Next_Line: + + ras.Proc_Sweep_Step( RAS_VAR ); + + y++; + + if ( y < y_change ) { + Sort( &draw_left ); + Sort( &draw_right ); + } + } + + /* Now finalize the profiles that needs it */ + + { + PProfile Q, P; + + + P = draw_left; + while ( P ) + { + Q = P->link; + if ( P->height == 0 ) { + DelOld( &draw_left, P ); + } + P = Q; + } + } + + { + PProfile Q, P = draw_right; + + + while ( P ) + { + Q = P->link; + if ( P->height == 0 ) { + DelOld( &draw_right, P ); + } + P = Q; + } + } + } + + /* for gray-scaling, flushes the bitmap scanline cache */ + while ( y <= max_Y ) + { + ras.Proc_Sweep_Step( RAS_VAR ); + y++; + } + + return SUCCESS; + +Scan_DropOuts: + + P_Left = draw_left; + P_Right = draw_right; + + while ( P_Left ) + { + if ( P_Left->countL ) { + P_Left->countL = 0; +#if 0 + dropouts--; /* -- this is useful when debugging only */ +#endif + ras.Proc_Sweep_Drop( RAS_VARS y, + P_Left->X, + P_Right->X, + P_Left, + P_Right ); + } + + P_Left = P_Left->link; + P_Right = P_Right->link; + } + + goto Next_Line; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Render_Single_Pass */ +/* */ +/* */ +/* Performs one sweep with sub-banding. */ +/* */ +/* */ +/* flipped :: If set, flip the direction of the outline. */ +/* */ +/* */ +/* Renderer error code. */ +/* */ +static +int Render_Single_Pass( RAS_ARGS Bool flipped ) { + Short i, j, k; + + + while ( ras.band_top >= 0 ) + { + ras.maxY = (Long)ras.band_stack[ras.band_top].y_max * ras.precision; + ras.minY = (Long)ras.band_stack[ras.band_top].y_min * ras.precision; + + ras.top = ras.buff; + + ras.error = Raster_Err_None; + + if ( Convert_Glyph( RAS_VARS flipped ) ) { + if ( ras.error != Raster_Err_Overflow ) { + return FAILURE; + } + + ras.error = Raster_Err_None; + + /* sub-banding */ + +#ifdef DEBUG_RASTER + ClearBand( RAS_VARS TRUNC( ras.minY ), TRUNC( ras.maxY ) ); +#endif + + i = ras.band_stack[ras.band_top].y_min; + j = ras.band_stack[ras.band_top].y_max; + + k = ( i + j ) / 2; + + if ( ras.band_top >= 7 || k < i ) { + ras.band_top = 0; + ras.error = Raster_Err_Invalid; + + return ras.error; + } + + ras.band_stack[ras.band_top + 1].y_min = k; + ras.band_stack[ras.band_top + 1].y_max = j; + + ras.band_stack[ras.band_top].y_max = k - 1; + + ras.band_top++; + } else + { + if ( ras.fProfile ) { + if ( Draw_Sweep( RAS_VAR ) ) { + return ras.error; + } + } + ras.band_top--; + } + } + + return SUCCESS; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Render_Glyph */ +/* */ +/* */ +/* Renders a glyph in a bitmap. Sub-banding if needed. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* XXX Fixme: ftraster's error codes don't harmonize with FT2's ones! */ +/* */ +LOCAL_FUNC +FT_Error Render_Glyph( RAS_ARG ) { + FT_Error error; + + + Set_High_Precision( RAS_VARS ras.outline.flags & + ft_outline_high_precision ); + ras.scale_shift = ras.precision_shift; + ras.dropOutControl = 2; + ras.second_pass = !( ras.outline.flags & ft_outline_single_pass ); + + /* Vertical Sweep */ + ras.Proc_Sweep_Init = Vertical_Sweep_Init; + ras.Proc_Sweep_Span = Vertical_Sweep_Span; + ras.Proc_Sweep_Drop = Vertical_Sweep_Drop; + ras.Proc_Sweep_Step = Vertical_Sweep_Step; + + ras.band_top = 0; + ras.band_stack[0].y_min = 0; + ras.band_stack[0].y_max = ras.target.rows - 1; + + ras.bWidth = ras.target.width; + ras.bTarget = (Byte*)ras.target.buffer; + + if ( ( error = Render_Single_Pass( RAS_VARS 0 ) ) != 0 ) { + return error; + } + + /* Horizontal Sweep */ + if ( ras.second_pass && ras.dropOutControl != 0 ) { + ras.Proc_Sweep_Init = Horizontal_Sweep_Init; + ras.Proc_Sweep_Span = Horizontal_Sweep_Span; + ras.Proc_Sweep_Drop = Horizontal_Sweep_Drop; + ras.Proc_Sweep_Step = Horizontal_Sweep_Step; + + ras.band_top = 0; + ras.band_stack[0].y_min = 0; + ras.band_stack[0].y_max = ras.target.width - 1; + + if ( ( error = Render_Single_Pass( RAS_VARS 1 ) ) != 0 ) { + return error; + } + } + + return FT_Err_Ok; +} + + +#ifdef FT_RASTER_OPTION_ANTI_ALIASING + + +/*************************************************************************/ +/* */ +/* */ +/* Render_Gray_Glyph */ +/* */ +/* */ +/* Renders a glyph with grayscaling. Sub-banding if needed. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error Render_Gray_Glyph( RAS_ARG ) { + Long pixel_width; + FT_Error error; + + + Set_High_Precision( RAS_VARS ras.outline.flags & + ft_outline_high_precision ); + ras.scale_shift = ras.precision_shift + 1; + ras.dropOutControl = 2; + ras.second_pass = !( ras.outline.flags & ft_outline_single_pass ); + + /* Vertical Sweep */ + + ras.band_top = 0; + ras.band_stack[0].y_min = 0; + ras.band_stack[0].y_max = 2 * ras.target.rows - 1; + + ras.bWidth = ras.gray_width; + pixel_width = 2 * ( ( ras.target.width + 3 ) >> 2 ); + + if ( ras.bWidth > pixel_width ) { + ras.bWidth = pixel_width; + } + + ras.bWidth = ras.bWidth * 8; + ras.bTarget = (Byte*)ras.gray_lines; + ras.gTarget = (Byte*)ras.target.buffer; + + ras.Proc_Sweep_Init = Vertical_Gray_Sweep_Init; + ras.Proc_Sweep_Span = Vertical_Sweep_Span; + ras.Proc_Sweep_Drop = Vertical_Sweep_Drop; + ras.Proc_Sweep_Step = Vertical_Gray_Sweep_Step; + + error = Render_Single_Pass( RAS_VARS 0 ); + if ( error ) { + return error; + } + + /* Horizontal Sweep */ + if ( ras.second_pass && ras.dropOutControl != 0 ) { + ras.Proc_Sweep_Init = Horizontal_Sweep_Init; + ras.Proc_Sweep_Span = Horizontal_Gray_Sweep_Span; + ras.Proc_Sweep_Drop = Horizontal_Gray_Sweep_Drop; + ras.Proc_Sweep_Step = Horizontal_Sweep_Step; + + ras.band_top = 0; + ras.band_stack[0].y_min = 0; + ras.band_stack[0].y_max = ras.target.width * 2 - 1; + + error = Render_Single_Pass( RAS_VARS 1 ); + if ( error ) { + return error; + } + } + + return FT_Err_Ok; +} + +#else /* FT_RASTER_OPTION_ANTI_ALIASING */ + +LOCAL_FUNC +FT_Error Render_Gray_Glyph( RAS_ARG ) { + FT_UNUSED_RASTER; + + return FT_Err_Cannot_Render_Glyph; +} + +#endif /* FT_RASTER_OPTION_ANTI_ALIASING */ + + +static +void ft_black_init( TRaster_Instance* raster ) { + FT_UInt n; + FT_ULong c; + + + /* setup count table */ + for ( n = 0; n < 256; n++ ) + { + c = ( n & 0x55 ) + ( ( n & 0xAA ) >> 1 ); + + c = ( ( c << 6 ) & 0x3000 ) | + ( ( c << 4 ) & 0x0300 ) | + ( ( c << 2 ) & 0x0030 ) | + ( c & 0x0003 ); + + raster->count_table[n] = c; + } + +#ifdef FT_RASTER_OPTION_ANTI_ALIASING + + /* set default 5-levels gray palette */ + for ( n = 0; n < 5; n++ ) + raster->grays[n] = n * 255 / 4; + + raster->gray_width = RASTER_GRAY_LINES / 2; + +#endif +} + + +/**** RASTER OBJECT CREATION: In standalone mode, we simply use *****/ +/**** a static object. *****/ + + +#ifdef _STANDALONE_ + + +static +int ft_black_new( void* memory, + FT_Raster *araster ) { + static FT_RasterRec_ the_raster; + + + *araster = &the_raster; + memset( &the_raster, sizeof( the_raster ), 0 ); + ft_black_init( &the_raster ); + + return 0; +} + + +static +void ft_black_done( FT_Raster raster ) { + /* nothing */ + raster->init = 0; +} + + +#else /* _STANDALONE_ */ + + +static +int ft_black_new( FT_Memory memory, + TRaster_Instance** araster ) { + FT_Error error; + TRaster_Instance* raster; + + + *araster = 0; + if ( !ALLOC( raster, sizeof( *raster ) ) ) { + raster->memory = memory; + ft_black_init( raster ); + + *araster = raster; + } + + return error; +} + + +static +void ft_black_done( TRaster_Instance* raster ) { + FT_Memory memory = (FT_Memory)raster->memory; + FREE( raster ); +} + + +#endif /* _STANDALONE_ */ + + +static +void ft_black_reset( TRaster_Instance* raster, + const char* pool_base, + long pool_size ) { + if ( raster && pool_base && pool_size >= 4096 ) { + /* save the pool */ + raster->buff = (PLong)pool_base; + raster->sizeBuff = raster->buff + pool_size / sizeof( Long ); + } +} + + +static +void ft_black_set_mode( TRaster_Instance* raster, + unsigned long mode, + const char* palette ) { +#ifdef FT_RASTER_OPTION_ANTI_ALIASING + + if ( mode == FT_MAKE_TAG( 'p', 'a', 'l', '5' ) ) { + /* set 5-levels gray palette */ + raster->grays[0] = palette[0]; + raster->grays[1] = palette[1]; + raster->grays[2] = palette[2]; + raster->grays[3] = palette[3]; + raster->grays[4] = palette[4]; + } + +#else + + FT_UNUSED( raster ); + FT_UNUSED( mode ); + FT_UNUSED( palette ); + +#endif +} + + +static +int ft_black_render( TRaster_Instance* raster, + FT_Raster_Params* params ) { + FT_Outline* outline = (FT_Outline*)params->source; + FT_Bitmap* target_map = params->target; + + + if ( !raster || !raster->buff || !raster->sizeBuff ) { + return Raster_Err_Not_Ini; + } + + if ( !outline || !outline->contours || !outline->points ) { + return Raster_Err_Invalid; + } + + /* return immediately if the outline is empty */ + if ( outline->n_points == 0 || outline->n_contours <= 0 ) { + return Raster_Err_None; + } + + if ( outline->n_points != outline->contours[outline->n_contours - 1] + 1 ) { + return Raster_Err_Invalid; + } + + if ( !target_map || !target_map->buffer ) { + return Raster_Err_Invalid; + } + + /* this version of the raster does not support direct rendering, sorry */ + if ( params->flags & ft_raster_flag_direct ) { + return Raster_Err_Unsupported; + } + + ras.outline = *outline; + ras.target = *target_map; + + return ( ( params->flags & ft_raster_flag_aa ) + ? Render_Gray_Glyph( raster ) + : Render_Glyph( raster ) ); +} + + +FT_Raster_Funcs ft_standard_raster = +{ + ft_glyph_format_outline, + (FT_Raster_New_Func) ft_black_new, + (FT_Raster_Reset_Func) ft_black_reset, + (FT_Raster_Set_Mode_Func)ft_black_set_mode, + (FT_Raster_Render_Func) ft_black_render, + (FT_Raster_Done_Func) ft_black_done +}; + + +/* END */ diff --git a/src/ft2/ftraster.h b/src/ft2/ftraster.h new file mode 100644 index 0000000..f7cebe9 --- /dev/null +++ b/src/ft2/ftraster.h @@ -0,0 +1,50 @@ +/***************************************************************************/ +/* */ +/* ftraster.h */ +/* */ +/* The FreeType glyph rasterizer (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used */ +/* modified and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef FTRASTER_H +#define FTRASTER_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ftimage.h" + + +/*************************************************************************/ +/* */ +/* Uncomment the following line if you are using ftraster.c as a */ +/* standalone module, fully independent of FreeType. */ +/* */ +/* #define _STANDALONE_ */ + +#ifndef FT_EXPORT_VAR +#define FT_EXPORT_VAR( x ) extern x +#endif + +FT_EXPORT_VAR( FT_Raster_Funcs ) ft_standard_raster; + +#ifdef __cplusplus +} +#endif + + +#endif /* FTRASTER_H */ + + +/* END */ diff --git a/src/ft2/ftrend1.c b/src/ft2/ftrend1.c new file mode 100644 index 0000000..78af227 --- /dev/null +++ b/src/ft2/ftrend1.c @@ -0,0 +1,264 @@ +/***************************************************************************/ +/* */ +/* ftrend1.c */ +/* */ +/* The FreeType glyph rasterizer interface (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#include "ftobjs.h" +#include "ftoutln.h" + + + +#include "ftrend1.h" +#include "ftraster.h" + + +/* initialize renderer -- init its raster */ +static +FT_Error ft_raster1_init( FT_Renderer render ) { + FT_Library library = FT_MODULE_LIBRARY( render ); + + + render->clazz->raster_class->raster_reset( render->raster, + library->raster_pool, + library->raster_pool_size ); + + return FT_Err_Ok; +} + + +/* set render-specific mode */ +static +FT_Error ft_raster1_set_mode( FT_Renderer render, + FT_ULong mode_tag, + FT_Pointer data ) { + /* we simply pass it to the raster */ + return render->clazz->raster_class->raster_set_mode( render->raster, + mode_tag, + data ); +} + + +/* transform a given glyph image */ +static +FT_Error ft_raster1_transform( FT_Renderer render, + FT_GlyphSlot slot, + FT_Matrix* matrix, + FT_Vector* delta ) { + FT_Error error = FT_Err_Ok; + + + if ( slot->format != render->glyph_format ) { + error = FT_Err_Invalid_Argument; + goto Exit; + } + + if ( matrix ) { + FT_Outline_Transform( &slot->outline, matrix ); + } + + if ( delta ) { + FT_Outline_Translate( &slot->outline, delta->x, delta->y ); + } + +Exit: + return error; +} + + +/* return the glyph's control box */ +static +void ft_raster1_get_cbox( FT_Renderer render, + FT_GlyphSlot slot, + FT_BBox* cbox ) { + MEM_Set( cbox, 0, sizeof( *cbox ) ); + + if ( slot->format == render->glyph_format ) { + FT_Outline_Get_CBox( &slot->outline, cbox ); + } +} + + +/* convert a slot's glyph image into a bitmap */ +static +FT_Error ft_raster1_render( FT_Renderer render, + FT_GlyphSlot slot, + FT_UInt mode, + FT_Vector* origin ) { + FT_Error error; + FT_Outline* outline; + FT_BBox cbox; + FT_UInt width, height, pitch; + FT_Bitmap* bitmap; + FT_Memory memory; + + FT_Raster_Params params; + + + /* check glyph image format */ + if ( slot->format != render->glyph_format ) { + error = FT_Err_Invalid_Argument; + goto Exit; + } + + /* check rendering mode */ + if ( mode != ft_render_mode_mono ) { + /* raster1 is only capable of producing monochrome bitmaps */ + if ( render->clazz == &ft_raster1_renderer_class ) { + return FT_Err_Cannot_Render_Glyph; + } + } else + { + /* raster5 is only capable of producing 5-gray-levels bitmaps */ + if ( render->clazz == &ft_raster5_renderer_class ) { + return FT_Err_Cannot_Render_Glyph; + } + } + + outline = &slot->outline; + + /* translate the outline to the new origin if needed */ + if ( origin ) { + FT_Outline_Translate( outline, origin->x, origin->y ); + } + + /* compute the control box, and grid fit it */ + FT_Outline_Get_CBox( outline, &cbox ); + + cbox.xMin &= -64; + cbox.yMin &= -64; + cbox.xMax = ( cbox.xMax + 63 ) & - 64; + cbox.yMax = ( cbox.yMax + 63 ) & - 64; + + width = ( cbox.xMax - cbox.xMin ) >> 6; + height = ( cbox.yMax - cbox.yMin ) >> 6; + bitmap = &slot->bitmap; + memory = render->root.memory; + + /* release old bitmap buffer */ + if ( slot->flags & ft_glyph_own_bitmap ) { + FREE( bitmap->buffer ); + slot->flags &= ~ft_glyph_own_bitmap; + } + + /* allocate new one, depends on pixel format */ + if ( !( mode & ft_render_mode_mono ) ) { + /* we pad to 32 bits, only for backwards compatibility with FT 1.x */ + pitch = ( width + 3 ) & - 4; + bitmap->pixel_mode = ft_pixel_mode_grays; + bitmap->num_grays = 256; + } else + { + pitch = ( width + 7 ) >> 3; + bitmap->pixel_mode = ft_pixel_mode_mono; + } + + bitmap->width = width; + bitmap->rows = height; + bitmap->pitch = pitch; + + if ( ALLOC( bitmap->buffer, (FT_ULong)pitch * height ) ) { + goto Exit; + } + + slot->flags |= ft_glyph_own_bitmap; + + /* translate outline to render it into the bitmap */ + FT_Outline_Translate( outline, -cbox.xMin, -cbox.yMin ); + + /* set up parameters */ + params.target = bitmap; + params.source = outline; + params.flags = 0; + + if ( bitmap->pixel_mode == ft_pixel_mode_grays ) { + params.flags |= ft_raster_flag_aa; + } + + /* render outline into the bitmap */ + error = render->raster_render( render->raster, ¶ms ); + if ( error ) { + goto Exit; + } + + slot->format = ft_glyph_format_bitmap; + slot->bitmap_left = cbox.xMin >> 6; + slot->bitmap_top = cbox.yMax >> 6; + +Exit: + return error; +} + + +const FT_Renderer_Class ft_raster1_renderer_class = +{ + { + ft_module_renderer, + sizeof( FT_RendererRec ), + + "raster1", + 0x10000L, + 0x20000L, + + 0, /* module specific interface */ + + (FT_Module_Constructor)ft_raster1_init, + (FT_Module_Destructor) 0, + (FT_Module_Requester) 0 + }, + + ft_glyph_format_outline, + + (FTRenderer_render) ft_raster1_render, + (FTRenderer_transform)ft_raster1_transform, + (FTRenderer_getCBox) ft_raster1_get_cbox, + (FTRenderer_setMode) ft_raster1_set_mode, + + (FT_Raster_Funcs*) &ft_standard_raster +}; + + +/* this renderer is _NOT_ part of the default modules, you'll need */ +/* to register it by hand in your application. It should only be */ +/* used for backwards-compatibility with FT 1.x anyway. */ +const FT_Renderer_Class ft_raster5_renderer_class = +{ + { + ft_module_renderer, + sizeof( FT_RendererRec ), + + "raster5", + 0x10000L, + 0x20000L, + + 0, /* module specific interface */ + + (FT_Module_Constructor)ft_raster1_init, + (FT_Module_Destructor) 0, + (FT_Module_Requester) 0 + }, + + ft_glyph_format_outline, + + (FTRenderer_render) ft_raster1_render, + (FTRenderer_transform)ft_raster1_transform, + (FTRenderer_getCBox) ft_raster1_get_cbox, + (FTRenderer_setMode) ft_raster1_set_mode, + + (FT_Raster_Funcs*) &ft_standard_raster +}; + + +/* END */ diff --git a/src/ft2/ftrend1.h b/src/ft2/ftrend1.h new file mode 100644 index 0000000..51f26b3 --- /dev/null +++ b/src/ft2/ftrend1.h @@ -0,0 +1,37 @@ +/***************************************************************************/ +/* */ +/* ftrend1.h */ +/* */ +/* The FreeType glyph rasterizer interface (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef FTREND1_H +#define FTREND1_H + +#include "ftrender.h" + + +FT_EXPORT_VAR( const FT_Renderer_Class ) ft_raster1_renderer_class; + +/* this renderer is _NOT_ part of the default modules, you'll need */ +/* to register it by hand in your application. It should only be */ +/* used for backwards-compatibility with FT 1.x anyway. */ +/* */ +FT_EXPORT_VAR( const FT_Renderer_Class ) ft_raster5_renderer_class; + + +#endif /* FTREND1_H */ + + +/* END */ diff --git a/src/ft2/ftrender.h b/src/ft2/ftrender.h new file mode 100644 index 0000000..90142be --- /dev/null +++ b/src/ft2/ftrender.h @@ -0,0 +1,191 @@ +/***************************************************************************/ +/* */ +/* ftrender.h */ +/* */ +/* FreeType renderer modules public interface (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef FTRENDER_H +#define FTRENDER_H + +#include "ftmodule.h" +#include "ftglyph.h" + + +#ifdef __cplusplus +extern "C" { +#endif + + +/* create a new glyph object */ +typedef FT_Error ( *FT_Glyph_Init_Func )( FT_Glyph glyph, + FT_GlyphSlot slot ); + +/* destroys a given glyph object */ +typedef void ( *FT_Glyph_Done_Func )( FT_Glyph glyph ); + +typedef void ( *FT_Glyph_Transform_Func )( FT_Glyph glyph, + FT_Matrix* matrix, + FT_Vector* delta ); + +typedef void ( *FT_Glyph_BBox_Func )( FT_Glyph glyph, + FT_BBox* abbox ); + +typedef FT_Error ( *FT_Glyph_Copy_Func )( FT_Glyph source, + FT_Glyph target ); + +typedef FT_Error ( *FT_Glyph_Prepare_Func )( FT_Glyph glyph, + FT_GlyphSlot slot ); + +struct FT_Glyph_Class_ +{ + FT_UInt glyph_size; + FT_Glyph_Format glyph_format; + FT_Glyph_Init_Func glyph_init; + FT_Glyph_Done_Func glyph_done; + FT_Glyph_Copy_Func glyph_copy; + FT_Glyph_Transform_Func glyph_transform; + FT_Glyph_BBox_Func glyph_bbox; + FT_Glyph_Prepare_Func glyph_prepare; +}; + + +typedef FT_Error ( *FTRenderer_render )( FT_Renderer renderer, + FT_GlyphSlot slot, + FT_UInt mode, + FT_Vector* origin ); + +typedef FT_Error ( *FTRenderer_transform )( FT_Renderer renderer, + FT_GlyphSlot slot, + FT_Matrix* matrix, + FT_Vector* delta ); + +typedef void ( *FTRenderer_getCBox )( FT_Renderer renderer, + FT_GlyphSlot slot, + FT_BBox* cbox ); + +typedef FT_Error ( *FTRenderer_setMode )( FT_Renderer renderer, + FT_ULong mode_tag, + FT_Pointer mode_ptr ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Renderer_Class */ +/* */ +/* */ +/* The renderer module class descriptor. */ +/* */ +/* */ +/* root :: The root FT_Module_Class fields. */ +/* */ +/* glyph_format :: The glyph image format this renderer handles. */ +/* */ +/* render_glyph :: A method used to render the image that is in a */ +/* given glyph slot into a bitmap. */ +/* */ +/* set_mode :: A method used to pass additional parameters. */ +/* */ +/* raster_class :: For `ft_glyph_format_outline' renderers only, this */ +/* is a pointer to its raster's class. */ +/* */ +/* raster :: For `ft_glyph_format_outline' renderers only. this */ +/* is a pointer to the corresponding raster object, */ +/* if any. */ +/* */ +typedef struct FT_Renderer_Class_ +{ + FT_Module_Class root; + + FT_Glyph_Format glyph_format; + + FTRenderer_render render_glyph; + FTRenderer_transform transform_glyph; + FTRenderer_getCBox get_glyph_cbox; + FTRenderer_setMode set_mode; + + FT_Raster_Funcs* raster_class; + +} FT_Renderer_Class; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Get_Renderer */ +/* */ +/* */ +/* Retrieves the current renderer for a given glyph format. */ +/* */ +/* */ +/* library :: A handle to the library object. */ +/* */ +/* format :: The glyph format. */ +/* */ +/* */ +/* A renderer handle. 0 if none found. */ +/* */ +/* */ +/* An error will be returned if a module already exists by that name, */ +/* or if the module requires a version of FreeType that is too great. */ +/* */ +/* To add a new renderer, simply use FT_Add_Module(). To retrieve a */ +/* renderer by its name, use FT_Get_Module(). */ +/* */ +FT_EXPORT_DEF( FT_Renderer ) FT_Get_Renderer( FT_Library library, + FT_Glyph_Format format ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Set_Renderer */ +/* */ +/* */ +/* Sets the current renderer to use, and set additional mode. */ +/* */ +/* */ +/* library :: A handle to the library object. */ +/* */ +/* renderer :: A handle to the renderer object. */ +/* */ +/* num_params :: The number of additional parameters. */ +/* */ +/* parameters :: Additional parameters. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* In case of success, the renderer will be used to convert glyph */ +/* images in the renderer's known format into bitmaps. */ +/* */ +/* This doesn't change the current renderer for other formats. */ +/* */ +FT_EXPORT_DEF( FT_Error ) FT_Set_Renderer( FT_Library library, + FT_Renderer renderer, + FT_UInt num_params, + FT_Parameter * parameters ); + + +#ifdef __cplusplus +} +#endif + + +#endif /* FTRENDER_H */ + + +/* END */ diff --git a/src/ft2/ftsmooth.c b/src/ft2/ftsmooth.c new file mode 100644 index 0000000..25c5009 --- /dev/null +++ b/src/ft2/ftsmooth.c @@ -0,0 +1,219 @@ +/***************************************************************************/ +/* */ +/* ftsmooth.c */ +/* */ +/* Anti-aliasing renderer interface (body). */ +/* */ +/* Copyright 2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#include "ftobjs.h" +#include "ftoutln.h" + + +#ifdef FT_FLAT_COMPILE + +#include "ftsmooth.h" +#include "ftgrays.h" + +#else + +#include "ftsmooth.h" +#include "ftgrays.h" + +#endif + + +/* initialize renderer -- init its raster */ +static +FT_Error ft_smooth_init( FT_Renderer render ) { + FT_Library library = FT_MODULE_LIBRARY( render ); + + + render->clazz->raster_class->raster_reset( render->raster, + library->raster_pool, + library->raster_pool_size ); + + return 0; +} + + +/* sets render-specific mode */ +static +FT_Error ft_smooth_set_mode( FT_Renderer render, + FT_ULong mode_tag, + FT_Pointer data ) { + /* we simply pass it to the raster */ + return render->clazz->raster_class->raster_set_mode( render->raster, + mode_tag, + data ); +} + +/* transform a given glyph image */ +static +FT_Error ft_smooth_transform( FT_Renderer render, + FT_GlyphSlot slot, + FT_Matrix* matrix, + FT_Vector* delta ) { + FT_Error error = FT_Err_Ok; + + + if ( slot->format != render->glyph_format ) { + error = FT_Err_Invalid_Argument; + goto Exit; + } + + if ( matrix ) { + FT_Outline_Transform( &slot->outline, matrix ); + } + + if ( delta ) { + FT_Outline_Translate( &slot->outline, delta->x, delta->y ); + } + +Exit: + return error; +} + + +/* return the glyph's control box */ +static +void ft_smooth_get_cbox( FT_Renderer render, + FT_GlyphSlot slot, + FT_BBox* cbox ) { + MEM_Set( cbox, 0, sizeof( *cbox ) ); + + if ( slot->format == render->glyph_format ) { + FT_Outline_Get_CBox( &slot->outline, cbox ); + } +} + + +/* convert a slot's glyph image into a bitmap */ +static +FT_Error ft_smooth_render( FT_Renderer render, + FT_GlyphSlot slot, + FT_UInt mode, + FT_Vector* origin ) { + FT_Error error; + FT_Outline* outline; + FT_BBox cbox; + FT_UInt width, height, pitch; + FT_Bitmap* bitmap; + FT_Memory memory; + + FT_Raster_Params params; + + + /* check glyph image format */ + if ( slot->format != render->glyph_format ) { + error = FT_Err_Invalid_Argument; + goto Exit; + } + + /* check mode */ + if ( mode != ft_render_mode_normal ) { + return FT_Err_Cannot_Render_Glyph; + } + + outline = &slot->outline; + + /* translate the outline to the new origin if needed */ + if ( origin ) { + FT_Outline_Translate( outline, origin->x, origin->y ); + } + + /* compute the control box, and grid fit it */ + FT_Outline_Get_CBox( outline, &cbox ); + + cbox.xMin &= -64; + cbox.yMin &= -64; + cbox.xMax = ( cbox.xMax + 63 ) & - 64; + cbox.yMax = ( cbox.yMax + 63 ) & - 64; + + width = ( cbox.xMax - cbox.xMin ) >> 6; + height = ( cbox.yMax - cbox.yMin ) >> 6; + bitmap = &slot->bitmap; + memory = render->root.memory; + + /* release old bitmap buffer */ + if ( slot->flags & ft_glyph_own_bitmap ) { + FREE( bitmap->buffer ); + slot->flags &= ~ft_glyph_own_bitmap; + } + + /* allocate new one, depends on pixel format */ + pitch = width; + bitmap->pixel_mode = ft_pixel_mode_grays; + bitmap->num_grays = 256; + bitmap->width = width; + bitmap->rows = height; + bitmap->pitch = pitch; + + if ( ALLOC( bitmap->buffer, (FT_ULong)pitch * height ) ) { + goto Exit; + } + + slot->flags |= ft_glyph_own_bitmap; + + /* translate outline to render it into the bitmap */ + FT_Outline_Translate( outline, -cbox.xMin, -cbox.yMin ); + + /* set up parameters */ + params.target = bitmap; + params.source = outline; + params.flags = ft_raster_flag_aa; + + /* render outline into the bitmap */ + error = render->raster_render( render->raster, ¶ms ); + if ( error ) { + goto Exit; + } + + slot->format = ft_glyph_format_bitmap; + slot->bitmap_left = cbox.xMin >> 6; + slot->bitmap_top = cbox.yMax >> 6; + +Exit: + return error; +} + + +const FT_Renderer_Class ft_smooth_renderer_class = +{ + { + ft_module_renderer, + sizeof( FT_RendererRec ), + + "smooth", + 0x10000L, + 0x20000L, + + 0, /* module specific interface */ + + (FT_Module_Constructor)ft_smooth_init, + (FT_Module_Destructor) 0, + (FT_Module_Requester) 0 + }, + + ft_glyph_format_outline, + + (FTRenderer_render) ft_smooth_render, + (FTRenderer_transform)ft_smooth_transform, + (FTRenderer_getCBox) ft_smooth_get_cbox, + (FTRenderer_setMode) ft_smooth_set_mode, + + (FT_Raster_Funcs*) &ft_grays_raster +}; + + +/* END */ diff --git a/src/ft2/ftsmooth.h b/src/ft2/ftsmooth.h new file mode 100644 index 0000000..8da0594 --- /dev/null +++ b/src/ft2/ftsmooth.h @@ -0,0 +1,35 @@ +/***************************************************************************/ +/* */ +/* ftsmooth.h */ +/* */ +/* Anti-aliasing renderer interface (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef FTSMOOTH_H +#define FTSMOOTH_H + +#include "ftrender.h" + +#ifndef FT_CONFIG_OPTION_NO_STD_RASTER +FT_EXPORT_VAR( const FT_Renderer_Class ) ft_std_renderer_class; +#endif + +#ifndef FT_CONFIG_OPTION_NO_SMOOTH_RASTER +FT_EXPORT_VAR( const FT_Renderer_Class ) ft_smooth_renderer_class; +#endif + +#endif /* FTSMOOTH_H */ + + +/* END */ diff --git a/src/ft2/ftstream.c b/src/ft2/ftstream.c new file mode 100644 index 0000000..1e5468a --- /dev/null +++ b/src/ft2/ftstream.c @@ -0,0 +1,807 @@ +/***************************************************************************/ +/* */ +/* ftstream.c */ +/* */ +/* I/O stream support (body). */ +/* */ +/* Copyright 2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#include "ftstream.h" +#include "ftdebug.h" + + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_stream + + +BASE_FUNC( void ) FT_New_Memory_Stream( FT_Library library, + FT_Byte * base, + FT_ULong size, + FT_Stream stream ) +{ + stream->memory = library->memory; + stream->base = base; + stream->size = size; + stream->pos = 0; + stream->cursor = 0; + stream->read = 0; + stream->close = 0; +} + + +BASE_FUNC( FT_Error ) FT_Seek_Stream( FT_Stream stream, + FT_ULong pos ) +{ + FT_Error error; + + + stream->pos = pos; + + if ( stream->read ) { + if ( stream->read( stream, pos, 0, 0 ) ) { + FT_ERROR( ( "FT_Seek_Stream:" ) ); + FT_ERROR( ( " invalid i/o; pos = 0x%lx, size = 0x%lx\n", + pos, stream->size ) ); + + error = FT_Err_Invalid_Stream_Operation; + } else { + error = FT_Err_Ok; + } + } + /* note that seeking to the first position after the file is valid */ + else if ( pos > stream->size ) { + FT_ERROR( ( "FT_Seek_Stream:" ) ); + FT_ERROR( ( " invalid i/o; pos = 0x%lx, size = 0x%lx\n", + pos, stream->size ) ); + + error = FT_Err_Invalid_Stream_Operation; + } else { + error = FT_Err_Ok; + } + + return error; +} + + +BASE_FUNC( FT_Error ) FT_Skip_Stream( FT_Stream stream, + FT_Long distance ) +{ + return FT_Seek_Stream( stream, (FT_ULong)( stream->pos + distance ) ); +} + + +BASE_FUNC( FT_Long ) FT_Stream_Pos( FT_Stream stream ) +{ + return stream->pos; +} + + +BASE_FUNC( FT_Error ) FT_Read_Stream( FT_Stream stream, + FT_Byte * buffer, + FT_ULong count ) +{ + return FT_Read_Stream_At( stream, stream->pos, buffer, count ); +} + + +BASE_FUNC( FT_Error ) FT_Read_Stream_At( FT_Stream stream, + FT_ULong pos, + FT_Byte * buffer, + FT_ULong count ) +{ + FT_Error error = FT_Err_Ok; + FT_ULong read_bytes; + + + if ( pos >= stream->size ) { + FT_ERROR( ( "FT_Read_Stream_At:" ) ); + FT_ERROR( ( " invalid i/o; pos = 0x%lx, size = 0x%lx\n", + pos, stream->size ) ); + + return FT_Err_Invalid_Stream_Operation; + } + + if ( stream->read ) { + read_bytes = stream->read( stream, pos, buffer, count ); + } else + { + read_bytes = stream->size - pos; + if ( read_bytes > count ) { + read_bytes = count; + } + + MEM_Copy( buffer, stream->base + pos, read_bytes ); + } + + stream->pos = pos + read_bytes; + + if ( read_bytes < count ) { + FT_ERROR( ( "FT_Read_Stream_At:" ) ); + FT_ERROR( ( " invalid read; expected %lu bytes, got %lu\n", + count, read_bytes ) ); + + error = FT_Err_Invalid_Stream_Operation; + } + + return error; +} + + +BASE_FUNC( FT_Error ) FT_Extract_Frame( FT_Stream stream, + FT_ULong count, + FT_Byte * *pbytes ) +{ + FT_Error error; + + + error = FT_Access_Frame( stream, count ); + if ( !error ) { + *pbytes = (FT_Byte*)stream->cursor; + + /* equivalent to FT_Forget_Frame(), with no memory block release */ + stream->cursor = 0; + stream->limit = 0; + } + + return error; +} + + +BASE_FUNC( void ) FT_Release_Frame( FT_Stream stream, + FT_Byte * *pbytes ) +{ + if ( stream->read ) { + FT_Memory memory = stream->memory; + + + FREE( *pbytes ); + } + *pbytes = 0; +} + + +BASE_FUNC( FT_Error ) FT_Access_Frame( FT_Stream stream, + FT_ULong count ) +{ + FT_Error error = FT_Err_Ok; + FT_ULong read_bytes; + + + /* check for nested frame access */ + FT_Assert( stream && stream->cursor == 0 ); + + if ( stream->read ) { + /* allocate the frame in memory */ + FT_Memory memory = stream->memory; + + + if ( ALLOC( stream->base, count ) ) { + goto Exit; + } + + /* read it */ + read_bytes = stream->read( stream, stream->pos, + stream->base, count ); + if ( read_bytes < count ) { + FT_ERROR( ( "FT_Access_Frame:" ) ); + FT_ERROR( ( " invalid read; expected %lu bytes, got %lu\n", + count, read_bytes ) ); + + FREE( stream->base ); + error = FT_Err_Invalid_Stream_Operation; + } + stream->cursor = stream->base; + stream->limit = stream->cursor + count; + stream->pos += read_bytes; + } else + { + /* check current and new position */ + if ( stream->pos >= stream->size || + stream->pos + count > stream->size ) { + FT_ERROR( ( "FT_Access_Frame:" ) ); + FT_ERROR( ( " invalid i/o; pos = 0x%lx, count = %lu, size = 0x%lx\n", + stream->pos, count, stream->size ) ); + + error = FT_Err_Invalid_Stream_Operation; + goto Exit; + } + + /* set cursor */ + stream->cursor = stream->base + stream->pos; + stream->limit = stream->cursor + count; + stream->pos += count; + } + +Exit: + return error; +} + + +BASE_FUNC( void ) FT_Forget_Frame( FT_Stream stream ) +{ + /* IMPORTANT: The assertion stream->cursor != 0 was removed, given */ + /* that it is possible to access a frame of length 0 in */ + /* some weird fonts (usually, when accessing an array of */ + /* 0 records, like in some strange kern tables). */ + /* */ + /* In this case, the loader code handles the 0-length table */ + /* gracefully; however, stream.cursor is really set to 0 by the */ + /* FT_Access_Frame() call, and this is not an error. */ + /* */ + FT_Assert( stream ); + + if ( stream->read ) { + FT_Memory memory = stream->memory; + + + FREE( stream->base ); + } + stream->cursor = 0; + stream->limit = 0; +} + + +BASE_FUNC( FT_Char ) FT_Get_Char( FT_Stream stream ) +{ + FT_Char result; + + + FT_Assert( stream && stream->cursor ); + + result = 0; + if ( stream->cursor < stream->limit ) { + result = *stream->cursor++; + } + + return result; +} + + +BASE_FUNC( FT_Short ) FT_Get_Short( FT_Stream stream ) +{ + FT_Byte* p; + FT_Short result; + + + FT_Assert( stream && stream->cursor ); + + result = 0; + p = stream->cursor; + if ( p + 1 < stream->limit ) { + result = NEXT_Short( p ); + } + stream->cursor = p; + + return result; +} + + +BASE_FUNC( FT_Short ) FT_Get_ShortLE( FT_Stream stream ) +{ + FT_Byte* p; + FT_Short result; + + + FT_Assert( stream && stream->cursor ); + + result = 0; + p = stream->cursor; + if ( p + 1 < stream->limit ) { + result = NEXT_ShortLE( p ); + } + stream->cursor = p; + + return result; +} + + +BASE_FUNC( FT_Long ) FT_Get_Offset( FT_Stream stream ) +{ + FT_Byte* p; + FT_Long result; + + + FT_Assert( stream && stream->cursor ); + + result = 0; + p = stream->cursor; + if ( p + 2 < stream->limit ) { + result = NEXT_Offset( p ); + } + stream->cursor = p; + return result; +} + + +BASE_FUNC( FT_Long ) FT_Get_Long( FT_Stream stream ) +{ + FT_Byte* p; + FT_Long result; + + + FT_Assert( stream && stream->cursor ); + + result = 0; + p = stream->cursor; + if ( p + 3 < stream->limit ) { + result = NEXT_Long( p ); + } + stream->cursor = p; + return result; +} + + +BASE_FUNC( FT_Long ) FT_Get_LongLE( FT_Stream stream ) +{ + FT_Byte* p; + FT_Long result; + + + FT_Assert( stream && stream->cursor ); + + result = 0; + p = stream->cursor; + if ( p + 3 < stream->limit ) { + result = NEXT_LongLE( p ); + } + stream->cursor = p; + return result; +} + + +BASE_FUNC( FT_Char ) FT_Read_Char( FT_Stream stream, + FT_Error * error ) +{ + FT_Byte result = 0; + + + FT_Assert( stream ); + + *error = FT_Err_Ok; + + if ( stream->read ) { + if ( stream->read( stream, stream->pos, &result, 1L ) != 1L ) { + goto Fail; + } + } else + { + if ( stream->pos < stream->size ) { + result = stream->base[stream->pos]; + } else { + goto Fail; + } + } + stream->pos++; + + return result; + +Fail: + *error = FT_Err_Invalid_Stream_Operation; + FT_ERROR( ( "FT_Read_Char:" ) ); + FT_ERROR( ( " invalid i/o; pos = 0x%lx, size = 0x%lx\n", + stream->pos, stream->size ) ); + + return 0; +} + + +BASE_FUNC( FT_Short ) FT_Read_Short( FT_Stream stream, + FT_Error * error ) +{ + FT_Byte reads[2]; + FT_Byte* p = 0; + FT_Short result = 0; + + + FT_Assert( stream ); + + *error = FT_Err_Ok; + + if ( stream->pos + 1 < stream->size ) { + if ( stream->read ) { + if ( stream->read( stream, stream->pos, reads, 2L ) != 2L ) { + goto Fail; + } + + p = reads; + } else + { + p = stream->base + stream->pos; + } + + if ( p ) { + result = NEXT_Short( p ); + } + } else { + goto Fail; + } + + stream->pos += 2; + + return result; + +Fail: + *error = FT_Err_Invalid_Stream_Operation; + FT_ERROR( ( "FT_Read_Short:" ) ); + FT_ERROR( ( " invalid i/o; pos = 0x%lx, size = 0x%lx\n", + stream->pos, stream->size ) ); + + return 0; +} + + +BASE_FUNC( FT_Short ) FT_Read_ShortLE( FT_Stream stream, + FT_Error * error ) +{ + FT_Byte reads[2]; + FT_Byte* p = 0; + FT_Short result = 0; + + + FT_Assert( stream ); + + *error = FT_Err_Ok; + + if ( stream->pos + 1 < stream->size ) { + if ( stream->read ) { + if ( stream->read( stream, stream->pos, reads, 2L ) != 2L ) { + goto Fail; + } + + p = reads; + } else + { + p = stream->base + stream->pos; + } + + if ( p ) { + result = NEXT_ShortLE( p ); + } + } else { + goto Fail; + } + + stream->pos += 2; + + return result; + +Fail: + *error = FT_Err_Invalid_Stream_Operation; + FT_ERROR( ( "FT_Read_Short:" ) ); + FT_ERROR( ( " invalid i/o; pos = 0x%lx, size = 0x%lx\n", + stream->pos, stream->size ) ); + + return 0; +} + + +BASE_FUNC( FT_Long ) FT_Read_Offset( FT_Stream stream, + FT_Error * error ) +{ + FT_Byte reads[3]; + FT_Byte* p = 0; + FT_Long result = 0; + + + FT_Assert( stream ); + + *error = FT_Err_Ok; + + if ( stream->pos + 2 < stream->size ) { + if ( stream->read ) { + if ( stream->read( stream, stream->pos, reads, 3L ) != 3L ) { + goto Fail; + } + + p = reads; + } else + { + p = stream->base + stream->pos; + } + + if ( p ) { + result = NEXT_Offset( p ); + } + } else { + goto Fail; + } + + stream->pos += 3; + + return result; + +Fail: + *error = FT_Err_Invalid_Stream_Operation; + FT_ERROR( ( "FT_Read_Offset:" ) ); + FT_ERROR( ( " invalid i/o; pos = 0x%lx, size = 0x%lx\n", + stream->pos, stream->size ) ); + + return 0; +} + + +BASE_FUNC( FT_Long ) FT_Read_Long( FT_Stream stream, + FT_Error * error ) +{ + FT_Byte reads[4]; + FT_Byte* p = 0; + FT_Long result = 0; + + + FT_Assert( stream ); + + *error = FT_Err_Ok; + + if ( stream->pos + 3 < stream->size ) { + if ( stream->read ) { + if ( stream->read( stream, stream->pos, reads, 4L ) != 4L ) { + goto Fail; + } + + p = reads; + } else + { + p = stream->base + stream->pos; + } + + if ( p ) { + result = NEXT_Long( p ); + } + } else { + goto Fail; + } + + stream->pos += 4; + + return result; + +Fail: + FT_ERROR( ( "FT_Read_Long:" ) ); + FT_ERROR( ( " invalid i/o; pos = 0x%lx, size = 0x%lx\n", + stream->pos, stream->size ) ); + *error = FT_Err_Invalid_Stream_Operation; + + return 0; +} + + +BASE_FUNC( FT_Long ) FT_Read_LongLE( FT_Stream stream, + FT_Error * error ) +{ + FT_Byte reads[4]; + FT_Byte* p = 0; + FT_Long result = 0; + + + FT_Assert( stream ); + + *error = FT_Err_Ok; + + if ( stream->pos + 3 < stream->size ) { + if ( stream->read ) { + if ( stream->read( stream, stream->pos, reads, 4L ) != 4L ) { + goto Fail; + } + + p = reads; + } else + { + p = stream->base + stream->pos; + } + + if ( p ) { + result = NEXT_LongLE( p ); + } + } else { + goto Fail; + } + + stream->pos += 4; + + return result; + +Fail: + FT_ERROR( ( "FT_Read_Long:" ) ); + FT_ERROR( ( " invalid i/o; pos = 0x%lx, size = 0x%lx\n", + stream->pos, stream->size ) ); + *error = FT_Err_Invalid_Stream_Operation; + + return 0; +} + + +BASE_FUNC( FT_Error ) FT_Read_Fields( FT_Stream stream, + const FT_Frame_Field * fields, + void* structure ) +{ + FT_Error error; + FT_Bool frame_accessed = 0; + + + if ( !fields || !stream ) { + return FT_Err_Invalid_Argument; + } + + error = FT_Err_Ok; + do + { + FT_ULong value; + FT_Int sign_shift; + FT_Byte* p; + + + switch ( fields->value ) + { + case ft_frame_start: /* access a new frame */ + error = FT_Access_Frame( stream, fields->offset ); + if ( error ) { + goto Exit; + } + + frame_accessed = 1; + fields++; + continue; /* loop! */ + + case ft_frame_bytes: /* read a byte sequence */ + case ft_frame_skip: /* skip some bytes */ + { + FT_Int len = fields->size; + + + if ( stream->cursor + len > stream->limit ) { + error = FT_Err_Invalid_Stream_Operation; + goto Exit; + } + + if ( fields->value == ft_frame_bytes ) { + p = (FT_Byte*)structure + fields->offset; + MEM_Copy( p, stream->cursor, len ); + } + stream->cursor += len; + fields++; + continue; + } + + case ft_frame_byte: + case ft_frame_schar: /* read a single byte */ + value = GET_Byte(); + sign_shift = 24; + break; + + case ft_frame_short_be: + case ft_frame_ushort_be: /* read a 2-byte big-endian short */ + value = GET_UShort(); + sign_shift = 16; + break; + + case ft_frame_short_le: + case ft_frame_ushort_le: /* read a 2-byte little-endian short */ + { + FT_Byte* p; + + + value = 0; + p = stream->cursor; + + if ( p + 1 < stream->limit ) { + value = ( FT_UShort)p[0] | ( (FT_UShort)p[1] << 8 ); + stream->cursor += 2; + } + sign_shift = 16; + break; + } + + case ft_frame_long_be: + case ft_frame_ulong_be: /* read a 4-byte big-endian long */ + value = GET_ULong(); + sign_shift = 0; + break; + + case ft_frame_long_le: + case ft_frame_ulong_le: /* read a 4-byte little-endian long */ + { + FT_Byte* p; + + + value = 0; + p = stream->cursor; + + if ( p + 3 < stream->limit ) { + value = (FT_ULong)p[0] | + ( (FT_ULong)p[1] << 8 ) | + ( (FT_ULong)p[2] << 16 ) | + ( (FT_ULong)p[3] << 24 ); + stream->cursor += 4; + } + sign_shift = 0; + break; + } + + case ft_frame_off3_be: + case ft_frame_uoff3_be: /* read a 3-byte big-endian long */ + value = GET_UOffset(); + sign_shift = 8; + break; + + case ft_frame_off3_le: + case ft_frame_uoff3_le: /* read a 3-byte little-endian long */ + { + FT_Byte* p; + + + value = 0; + p = stream->cursor; + + if ( p + 2 < stream->limit ) { + value = (FT_ULong)p[0] | + ( (FT_ULong)p[1] << 8 ) | + ( (FT_ULong)p[2] << 16 ); + stream->cursor += 3; + } + sign_shift = 8; + break; + } + + default: + /* otherwise, exit the loop */ + goto Exit; + } + + /* now, compute the signed value is necessary */ + if ( fields->value & FT_FRAME_OP_SIGNED ) { + value = (FT_ULong)( (FT_Int32)( value << sign_shift ) >> sign_shift ); + } + + /* finally, store the value in the object */ + + p = (FT_Byte*)structure + fields->offset; + switch ( fields->size ) + { + case 1: + *(FT_Byte*)p = (FT_Byte)value; + break; + + case 2: + *(FT_UShort*)p = (FT_UShort)value; + break; + + case 4: + *(FT_UInt32*)p = (FT_UInt32)value; + break; + + default: /* for 64-bit systems */ + *(FT_ULong*)p = (FT_ULong)value; + } + + /* go to next field */ + fields++; + } + while ( 1 ); + +Exit: + /* close the frame if it was opened by this read */ + if ( frame_accessed ) { + FT_Forget_Frame( stream ); + } + + return error; +} + + +/* END */ diff --git a/src/ft2/ftstream.h b/src/ft2/ftstream.h new file mode 100644 index 0000000..45cf2f1 --- /dev/null +++ b/src/ft2/ftstream.h @@ -0,0 +1,361 @@ +/***************************************************************************/ +/* */ +/* ftstream.h */ +/* */ +/* Stream handling(specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef FTSTREAM_H +#define FTSTREAM_H + +#include "ftobjs.h" + + +#ifdef __cplusplus +extern "C" { +#endif + + +/* format of an 8-bit frame_op value = [ xxxxx | e | s ] */ +/* s is set to 1 if the value is signed, */ +/* e is set to 1 if the value is little-endian */ +/* xxxxx is a command */ + +#define FT_FRAME_OP_SHIFT 2 +#define FT_FRAME_OP_SIGNED 1 +#define FT_FRAME_OP_LITTLE 2 +#define FT_FRAME_OP_COMMAND( x ) ( x >> FT_FRAME_OP_SHIFT ) + +#define FT_MAKE_FRAME_OP( command, little, sign ) \ + ( ( command << FT_FRAME_OP_SHIFT ) | ( little << 1 ) | sign ) + +#define FT_FRAME_OP_END 0 +#define FT_FRAME_OP_START 1 /* start a new frame */ +#define FT_FRAME_OP_BYTE 2 /* read 1-byte value */ +#define FT_FRAME_OP_SHORT 3 /* read 2-byte value */ +#define FT_FRAME_OP_LONG 4 /* read 4-byte value */ +#define FT_FRAME_OP_OFF3 5 /* read 3-byte value */ +#define FT_FRAME_OP_BYTES 6 /* read a bytes sequence */ + + +typedef enum FT_Frame_Op_ +{ + ft_frame_end = 0, + ft_frame_start = FT_MAKE_FRAME_OP( FT_FRAME_OP_START, 0, 0 ), + + ft_frame_byte = FT_MAKE_FRAME_OP( FT_FRAME_OP_BYTE, 0, 0 ), + ft_frame_schar = FT_MAKE_FRAME_OP( FT_FRAME_OP_BYTE, 0, 1 ), + + ft_frame_ushort_be = FT_MAKE_FRAME_OP( FT_FRAME_OP_SHORT, 0, 0 ), + ft_frame_short_be = FT_MAKE_FRAME_OP( FT_FRAME_OP_SHORT, 0, 1 ), + ft_frame_ushort_le = FT_MAKE_FRAME_OP( FT_FRAME_OP_SHORT, 1, 0 ), + ft_frame_short_le = FT_MAKE_FRAME_OP( FT_FRAME_OP_SHORT, 1, 1 ), + + ft_frame_ulong_be = FT_MAKE_FRAME_OP( FT_FRAME_OP_LONG, 0, 0 ), + ft_frame_ulong_le = FT_MAKE_FRAME_OP( FT_FRAME_OP_LONG, 0, 1 ), + ft_frame_long_be = FT_MAKE_FRAME_OP( FT_FRAME_OP_LONG, 1, 0 ), + ft_frame_long_le = FT_MAKE_FRAME_OP( FT_FRAME_OP_LONG, 1, 1 ), + + ft_frame_uoff3_be = FT_MAKE_FRAME_OP( FT_FRAME_OP_OFF3, 0, 0 ), + ft_frame_uoff3_le = FT_MAKE_FRAME_OP( FT_FRAME_OP_OFF3, 0, 1 ), + ft_frame_off3_be = FT_MAKE_FRAME_OP( FT_FRAME_OP_OFF3, 1, 0 ), + ft_frame_off3_le = FT_MAKE_FRAME_OP( FT_FRAME_OP_OFF3, 1, 1 ), + + ft_frame_bytes = FT_MAKE_FRAME_OP( FT_FRAME_OP_BYTES, 0, 0 ), + ft_frame_skip = FT_MAKE_FRAME_OP( FT_FRAME_OP_BYTES, 0, 1 ) + +} FT_Frame_Op; + + +typedef struct FT_Frame_Field_ +{ + FT_Frame_Op value; + char size; + FT_UShort offset; + +} FT_Frame_Field; + + +/* make-up a FT_Frame_Field out of a structure type and a field name */ +#define FT_FIELD_REF( s, f ) ( ( (s*)0 )->f ) + +#define FT_FRAME_FIELD( frame_op, struct_type, field ) \ + { \ + frame_op, \ + sizeof( FT_FIELD_REF( struct_type,field ) ), \ + (FT_UShort)(char*)&FT_FIELD_REF( struct_type, field ) \ + } + +#define FT_MAKE_EMPTY_FIELD( frame_op ) { frame_op, 0, 0 } + +#define FT_FRAME_START( s ) { ft_frame_start, 0, s } +#define FT_FRAME_END { ft_frame_end, 0, 0 } + +#define FT_FRAME_LONG( s, f ) FT_FRAME_FIELD( ft_frame_long_be, s, f ) +#define FT_FRAME_ULONG( s, f ) FT_FRAME_FIELD( ft_frame_ulong_be, s, f ) +#define FT_FRAME_SHORT( s, f ) FT_FRAME_FIELD( ft_frame_short_be, s, f ) +#define FT_FRAME_USHORT( s, f ) FT_FRAME_FIELD( ft_frame_ushort_be, s, f ) +#define FT_FRAME_BYTE( s, f ) FT_FRAME_FIELD( ft_frame_byte, s, f ) +#define FT_FRAME_CHAR( s, f ) FT_FRAME_FIELD( ft_frame_schar, s, f ) + +#define FT_FRAME_LONG_LE( s, f ) FT_FRAME_FIELD( ft_frame_long_le, s, f ) +#define FT_FRAME_ULONG_LE( s, f ) FT_FRAME_FIELD( ft_frame_ulong_le, s, f ) +#define FT_FRAME_SHORT_LE( s, f ) FT_FRAME_FIELD( ft_frame_short_le, s, f ) +#define FT_FRAME_USHORT_LE( s, f ) FT_FRAME_FIELD( ft_frame_ushort_le, s, f ) + +#define FT_FRAME_SKIP_LONG { ft_frame_long_be, 0, 0 } +#define FT_FRAME_SKIP_SHORT { ft_frame_short_be, 0, 0 } +#define FT_FRAME_SKIP_BYTE { ft_frame_byte, 0, 0 } + +#define FT_FRAME_BYTES( struct_type, field, count ) \ + { \ + ft_frame_bytes, \ + count, \ + (FT_UShort)(char*)&FT_FIELD_REF( struct_type, field ) \ + } +#define FT_FRAME_SKIP_BYTES( count ) { ft_frame_skip, count, 0 } + + + +/*************************************************************************/ +/* */ +/* integer extraction macros -- the `buffer' parameter must ALWAYS be of */ +/* type `char*' or equivalent (1-byte elements). */ +/* */ +#define NEXT_Char( buffer ) \ + ( (signed char)*buffer++ ) +#define NEXT_Byte( buffer ) \ + ( (unsigned char)*buffer++ ) + +#define NEXT_Short( buffer ) \ + ( buffer += 2, \ + ( (short)( (signed char)buffer[-2] << 8 ) | \ + (unsigned char)buffer[-1] ) ) + +#define NEXT_UShort( buffer ) \ + ( (unsigned short)NEXT_Short( buffer ) ) + +#define NEXT_Offset( buffer ) \ + ( buffer += 3, \ + ( ( (long)(signed char)buffer[-3] << 16 ) | \ + ( (long)(unsigned char)buffer[-2] << 8 ) | \ + (long)(unsigned char)buffer[-1] ) ) + +#define NEXT_UOffset( buffer ) \ + ( (unsigned long)NEXT_Offset( buffer ) ) + +#define NEXT_Long( buffer ) \ + ( buffer += 4, \ + ( ( (long)(signed char)buffer[-4] << 24 ) | \ + ( (long)(unsigned char)buffer[-3] << 16 ) | \ + ( (long)(unsigned char)buffer[-2] << 8 ) | \ + (long)(unsigned char)buffer[-1] ) ) + +#define NEXT_ULong( buffer ) \ + ( (unsigned long)NEXT_Long( buffer ) ) + + +#define NEXT_ShortLE( buffer ) \ + ( buffer += 2, \ + ( (short)( (signed char)buffer[-1] << 8 ) | \ + (unsigned char)buffer[-2] ) ) + +#define NEXT_UShortLE( buffer ) \ + ( (unsigned short)NEXT_ShortLE( buffer ) ) + +#define NEXT_OffsetLE( buffer ) \ + ( buffer += 3, \ + ( ( (long)(signed char)buffer[-1] << 16 ) | \ + ( (long)(unsigned char)buffer[-2] << 8 ) | \ + (long)(unsigned char)buffer[-3] ) ) + +#define NEXT_UOffsetLE( buffer ) \ + ( (unsigned long)NEXT_OffsetLE( buffer ) ) + + +#define NEXT_LongLE( buffer ) \ + ( buffer += 4, \ + ( ( (long)(signed char)buffer[-1] << 24 ) | \ + ( (long)(unsigned char)buffer[-2] << 16 ) | \ + ( (long)(unsigned char)buffer[-3] << 8 ) | \ + (long)(unsigned char)buffer[-4] ) ) + +#define NEXT_ULongLE( buffer ) \ + ( (unsigned long)NEXT_LongLE( buffer ) ) + + +/*************************************************************************/ +/* */ +/* Each GET_xxxx() macro uses an implicit `stream' variable. */ +/* */ +#define FT_GET_MACRO( func, type ) ( (type)func( stream ) ) + +#define GET_Char() FT_GET_MACRO( FT_Get_Char, FT_Char ) +#define GET_Byte() FT_GET_MACRO( FT_Get_Char, FT_Byte ) +#define GET_Short() FT_GET_MACRO( FT_Get_Short, FT_Short ) +#define GET_UShort() FT_GET_MACRO( FT_Get_Short, FT_UShort ) +#define GET_Offset() FT_GET_MACRO( FT_Get_Offset, FT_Long ) +#define GET_UOffset() FT_GET_MACRO( FT_Get_Offset, FT_ULong ) +#define GET_Long() FT_GET_MACRO( FT_Get_Long, FT_Long ) +#define GET_ULong() FT_GET_MACRO( FT_Get_Long, FT_ULong ) +#define GET_Tag4() FT_GET_MACRO( FT_Get_Long, FT_ULong ) + +#define GET_ShortLE() FT_GET_MACRO( FT_Get_ShortLE, FT_Short ) +#define GET_UShortLE() FT_GET_MACRO( FT_Get_ShortLE, FT_UShort ) +#define GET_LongLE() FT_GET_MACRO( FT_Get_LongLE, FT_Short ) +#define GET_ULongLE() FT_GET_MACRO( FT_Get_LongLE, FT_Short ) + +#define FT_READ_MACRO( func, type, var ) \ + ( var = (type)func( stream, &error ), \ + error != FT_Err_Ok ) + +#define READ_Byte( var ) FT_READ_MACRO( FT_Read_Char, FT_Byte, var ) +#define READ_Char( var ) FT_READ_MACRO( FT_Read_Char, FT_Char, var ) +#define READ_Short( var ) FT_READ_MACRO( FT_Read_Short, FT_Short, var ) +#define READ_UShort( var ) FT_READ_MACRO( FT_Read_Short, FT_UShort, var ) +#define READ_Offset( var ) FT_READ_MACRO( FT_Read_Offset, FT_Long, var ) +#define READ_UOffset( var ) FT_READ_MACRO( FT_Read_Offset, FT_ULong, var ) +#define READ_Long( var ) FT_READ_MACRO( FT_Read_Long, FT_Long, var ) +#define READ_ULong( var ) FT_READ_MACRO( FT_Read_Long, FT_ULong, var ) + +#define READ_ShortLE( var ) FT_READ_MACRO( FT_Read_ShortLE, FT_Short, var ) +#define READ_UShortLE( var ) FT_READ_MACRO( FT_Read_ShortLE, FT_UShort, var ) +#define READ_LongLE( var ) FT_READ_MACRO( FT_Read_LongLE, FT_Long, var ) +#define READ_ULongLE( var ) FT_READ_MACRO( FT_Read_LongLE, FT_ULong, var ) + + +BASE_DEF( void ) FT_New_Memory_Stream( FT_Library library, + FT_Byte * base, + FT_ULong size, + FT_Stream stream ); + +BASE_DEF( FT_Error ) FT_Seek_Stream( FT_Stream stream, + FT_ULong pos ); + +BASE_DEF( FT_Error ) FT_Skip_Stream( FT_Stream stream, + FT_Long distance ); + +BASE_DEF( FT_Long ) FT_Stream_Pos( FT_Stream stream ); + + +BASE_DEF( FT_Error ) FT_Read_Stream( FT_Stream stream, + FT_Byte * buffer, + FT_ULong count ); + +BASE_DEF( FT_Error ) FT_Read_Stream_At( FT_Stream stream, + FT_ULong pos, + FT_Byte * buffer, + FT_ULong count ); + +BASE_DEF( FT_Error ) FT_Access_Frame( FT_Stream stream, + FT_ULong count ); + +BASE_DEF( void ) FT_Forget_Frame( FT_Stream stream ); + +BASE_DEF( FT_Error ) FT_Extract_Frame( FT_Stream stream, + FT_ULong count, + FT_Byte * *pbytes ); + +BASE_DEF( void ) FT_Release_Frame( FT_Stream stream, + FT_Byte * *pbytes ); + +BASE_DEF( FT_Char ) FT_Get_Char( FT_Stream stream ); + +BASE_DEF( FT_Short ) FT_Get_Short( FT_Stream stream ); + +BASE_DEF( FT_Long ) FT_Get_Offset( FT_Stream stream ); + +BASE_DEF( FT_Long ) FT_Get_Long( FT_Stream stream ); + +BASE_DEF( FT_Short ) FT_Get_ShortLE( FT_Stream stream ); + +BASE_DEF( FT_Long ) FT_Get_LongLE( FT_Stream stream ); + + +BASE_DEF( FT_Char ) FT_Read_Char( FT_Stream stream, + FT_Error * error ); + +BASE_DEF( FT_Short ) FT_Read_Short( FT_Stream stream, + FT_Error * error ); + +BASE_DEF( FT_Long ) FT_Read_Offset( FT_Stream stream, + FT_Error * error ); + +BASE_DEF( FT_Long ) FT_Read_Long( FT_Stream stream, + FT_Error * error ); + +BASE_DEF( FT_Short ) FT_Read_ShortLE( FT_Stream stream, + FT_Error * error ); + +BASE_DEF( FT_Long ) FT_Read_LongLE( FT_Stream stream, + FT_Error * error ); + +BASE_DEF( FT_Error ) FT_Read_Fields( FT_Stream stream, + const FT_Frame_Field * fields, + void* structure ); + + +#define USE_Stream( resource, stream ) \ + FT_SET_ERROR( FT_Open_Stream( resource, stream ) ) + +#define DONE_Stream( stream ) \ + FT_Done_Stream( stream ) + + +#define ACCESS_Frame( size ) \ + FT_SET_ERROR( FT_Access_Frame( stream, size ) ) + +#define FORGET_Frame() \ + FT_Forget_Frame( stream ) + +#define EXTRACT_Frame( size, bytes ) \ + FT_SET_ERROR( FT_Extract_Frame( stream, size, \ + (FT_Byte**)&( bytes ) ) ) + +#define RELEASE_Frame( bytes ) \ + FT_Release_Frame( stream, (FT_Byte**)&( bytes ) ) + +#define FILE_Seek( position ) \ + FT_SET_ERROR( FT_Seek_Stream( stream, position ) ) + +#define FILE_Skip( distance ) \ + FT_SET_ERROR( FT_Skip_Stream( stream, distance ) ) + +#define FILE_Pos() \ + FT_Stream_Pos( stream ) + +#define FILE_Read( buffer, count ) \ + FT_SET_ERROR( FT_Read_Stream( stream, \ + (FT_Byte*)buffer, \ + count ) ) + +#define FILE_Read_At( position, buffer, count ) \ + FT_SET_ERROR( FT_Read_Stream_At( stream, \ + position, \ + (FT_Byte*)buffer, \ + count ) ) + +#define READ_Fields( fields, object ) \ + ( ( error = FT_Read_Fields( stream, fields, object ) ) != FT_Err_Ok ) + + +#ifdef __cplusplus +} +#endif + + +#endif /* FTSTREAM_H */ + + +/* END */ diff --git a/src/ft2/ftsystem.c b/src/ft2/ftsystem.c new file mode 100644 index 0000000..46b75be --- /dev/null +++ b/src/ft2/ftsystem.c @@ -0,0 +1,293 @@ +/***************************************************************************/ +/* */ +/* ftsystem.c */ +/* */ +/* ANSI-specific FreeType low-level system interface (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + +/*************************************************************************/ +/* */ +/* This file contains the default interface used by FreeType to access */ +/* low-level, i.e. memory management, i/o access as well as thread */ +/* synchronisation. It can be replaced by user-specific routines if */ +/* necessary. */ +/* */ +/*************************************************************************/ + + +#include "ftconfig.h" +#include "ftdebug.h" +#include "ftsystem.h" +#include "fterrors.h" +#include "fttypes.h" + +#include +#include +#include + + +/*************************************************************************/ +/* */ +/* MEMORY MANAGEMENT INTERFACE */ +/* */ +/*************************************************************************/ + +/*************************************************************************/ +/* */ +/* It is not necessary to do any error checking for the */ +/* allocation-related functions. This will be done by the higher level */ +/* routines like FT_Alloc() or FT_Realloc(). */ +/* */ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* ft_alloc */ +/* */ +/* */ +/* The memory allocation function. */ +/* */ +/* */ +/* memory :: A pointer to the memory object. */ +/* */ +/* size :: The requested size in bytes. */ +/* */ +/* */ +/* block :: The address of newly allocated block. */ +/* */ +static +void* ft_alloc( FT_Memory memory, + long size ) { + FT_UNUSED( memory ); + + return malloc( size ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* ft_realloc */ +/* */ +/* */ +/* The memory reallocation function. */ +/* */ +/* */ +/* memory :: A pointer to the memory object. */ +/* */ +/* cur_size :: The current size of the allocated memory block. */ +/* */ +/* new_size :: The newly requested size in bytes. */ +/* */ +/* block :: The current address of the block in memory. */ +/* */ +/* */ +/* The address of the reallocated memory block. */ +/* */ +static +void* ft_realloc( FT_Memory memory, + long cur_size, + long new_size, + void* block ) { + FT_UNUSED( memory ); + FT_UNUSED( cur_size ); + + return realloc( block, new_size ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* ft_free */ +/* */ +/* */ +/* The memory release function. */ +/* */ +/* */ +/* memory :: A pointer to the memory object. */ +/* */ +/* block :: The address of block in memory to be freed. */ +/* */ +static +void ft_free( FT_Memory memory, + void* block ) { + FT_UNUSED( memory ); + + free( block ); +} + + +/*************************************************************************/ +/* */ +/* RESOURCE MANAGEMENT INTERFACE */ +/* */ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_io + +/* We use the macro STREAM_FILE for convenience to extract the */ +/* system-specific stream handle from a given FreeType stream object */ +#define STREAM_FILE( stream ) ( (FILE*)stream->descriptor.pointer ) + + +/*************************************************************************/ +/* */ +/* */ +/* ft_close_stream */ +/* */ +/* */ +/* The function to close a stream. */ +/* */ +/* */ +/* stream :: A pointer to the stream object. */ +/* */ +static +void ft_close_stream( FT_Stream stream ) { + fclose( STREAM_FILE( stream ) ); + + stream->descriptor.pointer = NULL; + stream->size = 0; + stream->base = 0; +} + + +/*************************************************************************/ +/* */ +/* */ +/* ft_io_stream */ +/* */ +/* */ +/* The function to open a stream. */ +/* */ +/* */ +/* stream :: A pointer to the stream object. */ +/* */ +/* offset :: The position in the data stream to start reading. */ +/* */ +/* buffer :: The address of buffer to store the read data. */ +/* */ +/* count :: The number of bytes to read from the stream. */ +/* */ +/* */ +/* The number of bytes actually read. */ +/* */ +static +unsigned long ft_io_stream( FT_Stream stream, + unsigned long offset, + unsigned char* buffer, + unsigned long count ) { + FILE* file; + + + file = STREAM_FILE( stream ); + + fseek( file, offset, SEEK_SET ); + + return (unsigned long)fread( buffer, 1, count, file ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_New_Stream */ +/* */ +/* */ +/* Creates a new stream object. */ +/* */ +/* */ +/* filepathname :: The name of the stream (usually a file) to be */ +/* opened. */ +/* */ +/* stream :: A pointer to the stream object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) FT_New_Stream( const char* filepathname, + FT_Stream stream ) +{ + FILE* file; + + + if ( !stream ) { + return FT_Err_Invalid_Stream_Handle; + } + + file = fopen( filepathname, "rb" ); + if ( !file ) { + FT_ERROR( ( "FT_New_Stream:" ) ); + FT_ERROR( ( " could not open `%s'\n", filepathname ) ); + + return FT_Err_Cannot_Open_Resource; + } + + fseek( file, 0, SEEK_END ); + stream->size = ftell( file ); + fseek( file, 0, SEEK_SET ); + + stream->descriptor.pointer = file; + stream->pathname.pointer = (char*)filepathname; + stream->pos = 0; + + stream->read = ft_io_stream; + stream->close = ft_close_stream; + + FT_TRACE1( ( "FT_New_Stream:" ) ); + FT_TRACE1( ( " opened `%s' (%d bytes) successfully\n", + filepathname, stream->size ) ); + + return FT_Err_Ok; +} + + +/*************************************************************************/ +/* */ +/* */ +/* FT_New_Memory */ +/* */ +/* */ +/* Creates a new memory object. */ +/* */ +/* */ +/* A pointer to the new memory object. 0 in case of error. */ +/* */ +FT_EXPORT_FUNC( FT_Memory ) FT_New_Memory( void ) +{ + FT_Memory memory; + + + memory = (FT_Memory)malloc( sizeof( *memory ) ); + if ( memory ) { + memory->user = 0; + memory->alloc = ft_alloc; + memory->realloc = ft_realloc; + memory->free = ft_free; + } + + return memory; +} + + +/* END */ diff --git a/src/ft2/ftsystem.h b/src/ft2/ftsystem.h new file mode 100644 index 0000000..22931cb --- /dev/null +++ b/src/ft2/ftsystem.h @@ -0,0 +1,101 @@ +/***************************************************************************/ +/* */ +/* ftsystem.h */ +/* */ +/* FreeType low-level system interface definition (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef FTSYSTEM_H +#define FTSYSTEM_H + + +/*************************************************************************/ +/* */ +/* M E M O R Y M A N A G E M E N T */ +/* */ +/*************************************************************************/ + + +typedef struct FT_MemoryRec_* FT_Memory; + + +typedef void* ( *FT_Alloc_Func )( FT_Memory memory, + long size ); + +typedef void ( *FT_Free_Func )( FT_Memory memory, + void* block ); + +typedef void* ( *FT_Realloc_Func )( FT_Memory memory, + long cur_size, + long new_size, + void* block ); + + +struct FT_MemoryRec_ +{ + void* user; + FT_Alloc_Func alloc; + FT_Free_Func free; + FT_Realloc_Func realloc; +}; + + +/*************************************************************************/ +/* */ +/* I / O M A N A G E M E N T */ +/* */ +/*************************************************************************/ + + +typedef union FT_StreamDesc_ +{ + long value; + void* pointer; + +} FT_StreamDesc; + + +typedef struct FT_StreamRec_* FT_Stream; + + +typedef unsigned long ( *FT_Stream_IO )( FT_Stream stream, + unsigned long offset, + unsigned char* buffer, + unsigned long count ); + +typedef void ( *FT_Stream_Close )( FT_Stream stream ); + + +struct FT_StreamRec_ +{ + unsigned char* base; + unsigned long size; + unsigned long pos; + + FT_StreamDesc descriptor; + FT_StreamDesc pathname; /* ignored by FreeType -- */ + /* useful for debugging */ + FT_Stream_IO read; + FT_Stream_Close close; + + FT_Memory memory; + unsigned char* cursor; + unsigned char* limit; +}; + + +#endif /* FTSYSTEM_H */ + + +/* END */ diff --git a/src/ft2/fttypes.h b/src/ft2/fttypes.h new file mode 100644 index 0000000..209ede0 --- /dev/null +++ b/src/ft2/fttypes.h @@ -0,0 +1,400 @@ +/***************************************************************************/ +/* */ +/* fttypes.h */ +/* */ +/* FreeType simple types definitions (specification only). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef FTTYPES_H +#define FTTYPES_H + + +#include "ftsystem.h" +#include "ftimage.h" + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Bool */ +/* */ +/* */ +/* A typedef of unsigned char, used for simple booleans. */ +/* */ +typedef unsigned char FT_Bool; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_FWord */ +/* */ +/* */ +/* A signed 16-bit integer used to store a distance in original font */ +/* units. */ +/* */ +typedef signed short FT_FWord; /* distance in FUnits */ + + +/*************************************************************************/ +/* */ +/* */ +/* FT_UFWord */ +/* */ +/* */ +/* An unsigned 16-bit integer used to store a distance in original */ +/* font units. */ +/* */ +typedef unsigned short FT_UFWord; /* unsigned distance */ + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Char */ +/* */ +/* */ +/* A simple typedef for the _signed_ char type. */ +/* */ +typedef signed char FT_Char; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Byte */ +/* */ +/* */ +/* A simple typedef for the _unsigned_ char type. */ +/* */ +typedef unsigned char FT_Byte; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_String */ +/* */ +/* */ +/* A simple typedef for the char type, usually used for strings. */ +/* */ +typedef char FT_String; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Short */ +/* */ +/* */ +/* A typedef for signed short. */ +/* */ +typedef signed short FT_Short; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_UShort */ +/* */ +/* */ +/* A typedef for unsigned short. */ +/* */ +typedef unsigned short FT_UShort; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Int */ +/* */ +/* */ +/* A typedef for the int type. */ +/* */ +typedef int FT_Int; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_UInt */ +/* */ +/* */ +/* A typedef for the unsigned int type. */ +/* */ +typedef unsigned int FT_UInt; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Long */ +/* */ +/* */ +/* A typedef for signed long. */ +/* */ +typedef signed long FT_Long; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_ULong */ +/* */ +/* */ +/* A typedef for unsigned long. */ +/* */ +typedef unsigned long FT_ULong; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_F2Dot14 */ +/* */ +/* */ +/* A signed 2.14 fixed float type used for unit vectors. */ +/* */ +typedef signed short FT_F2Dot14; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_F26Dot6 */ +/* */ +/* */ +/* A signed 26.6 fixed float type used for vectorial pixel */ +/* coordinates. */ +/* */ +typedef signed long FT_F26Dot6; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Fixed */ +/* */ +/* */ +/* This type is used to store 16.16 fixed float values, like scales */ +/* or matrix coefficients. */ +/* */ +typedef signed long FT_Fixed; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Error */ +/* */ +/* */ +/* The FreeType error code type. A value of 0 is always interpreted */ +/* as a successful operation. */ +/* */ +typedef int FT_Error; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Pointer */ +/* */ +/* */ +/* A simple typedef for a typeless pointer. */ +/* */ +typedef void* FT_Pointer; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_UnitVector */ +/* */ +/* */ +/* A simple structure used to store a 2D vector unit vector. Uses */ +/* FT_F2Dot14 types. */ +/* */ +/* */ +/* x :: Horizontal coordinate. */ +/* */ +/* y :: Vertical coordinate. */ +/* */ +typedef struct FT_UnitVector_ +{ + FT_F2Dot14 x; + FT_F2Dot14 y; + +} FT_UnitVector; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Matrix */ +/* */ +/* */ +/* A simple structure used to store a 2x2 matrix. Coefficients are */ +/* in 16.16 fixed float format. The computation performed is: */ +/* */ +/* { */ +/* x' = x*xx + y*xy */ +/* y' = x*yx + y*yy */ +/* } */ +/* */ +/* */ +/* xx :: Matrix coefficient. */ +/* */ +/* xy :: Matrix coefficient. */ +/* */ +/* yx :: Matrix coefficient. */ +/* */ +/* yy :: Matrix coefficient. */ +/* */ +typedef struct FT_Matrix_ +{ + FT_Fixed xx, xy; + FT_Fixed yx, yy; + +} FT_Matrix; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_BBox */ +/* */ +/* */ +/* A structure used to hold an outline's bounding box, i.e., the */ +/* coordinates of its extrema in the horizontal and vertical */ +/* directions. */ +/* */ +/* */ +/* xMin :: The horizontal minimum (left-most). */ +/* */ +/* yMin :: The vertical minimum (bottom-most). */ +/* */ +/* xMax :: The horizontal maximum (right-most). */ +/* */ +/* yMax :: The vertical maximum (top-most). */ +/* */ +typedef struct FT_BBox_ +{ + FT_Pos xMin, yMin; + FT_Pos xMax, yMax; + +} FT_BBox; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_MAKE_TAG */ +/* */ +/* */ +/* This macro converts four letter tags which are used to label */ +/* TrueType tables into an unsigned long to be used within FreeType. */ +/* */ +#define FT_MAKE_TAG( _x1, _x2, _x3, _x4 ) \ + ( ( (FT_ULong)_x1 << 24 ) | \ + ( (FT_ULong)_x2 << 16 ) | \ + ( (FT_ULong)_x3 << 8 ) | \ + (FT_ULong)_x4 ) + + +/*************************************************************************/ +/*************************************************************************/ +/* */ +/* L I S T M A N A G E M E N T */ +/* */ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* FT_ListNode */ +/* */ +/* */ +/* Many elements and objects in FreeType are listed through a */ +/* FT_List record (see FT_ListRec). As its name suggests, a */ +/* FT_ListNode is a handle to a single list element. */ +/* */ +typedef struct FT_ListNodeRec_* FT_ListNode; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_List */ +/* */ +/* */ +/* A handle to a list record (see FT_ListRec). */ +/* */ +typedef struct FT_ListRec_* FT_List; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_ListNodeRec */ +/* */ +/* */ +/* A structure used to hold a single list element. */ +/* */ +/* */ +/* prev :: The previous element in the list. NULL if first. */ +/* */ +/* next :: The next element in the list. NULL if last. */ +/* */ +/* data :: A typeless pointer to the listed object. */ +/* */ +typedef struct FT_ListNodeRec_ +{ + FT_ListNode prev; + FT_ListNode next; + void* data; + +} FT_ListNodeRec; + + +/*************************************************************************/ +/* */ +/* */ +/* FT_ListRec */ +/* */ +/* */ +/* A structure used to hold a simple doubly-linked list. These are */ +/* used in many parts of FreeType. */ +/* */ +/* */ +/* head :: The head (first element) of doubly-linked list. */ +/* */ +/* tail :: The tail (last element) of doubly-linked list. */ +/* */ +typedef struct FT_ListRec_ +{ + FT_ListNode head; + FT_ListNode tail; + +} FT_ListRec; + + +#define FT_IS_EMPTY( list ) ( ( list ).head == 0 ) + + +#endif /* FTTYPES_H */ + + +/* END */ diff --git a/src/ft2/psnames.h b/src/ft2/psnames.h new file mode 100644 index 0000000..2965b0b --- /dev/null +++ b/src/ft2/psnames.h @@ -0,0 +1,220 @@ +/***************************************************************************/ +/* */ +/* psnames.h */ +/* */ +/* High-level interface for the `PSNames' module (in charge of */ +/* various functions related to Postscript glyph names conversion). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef PSNAMES_H +#define PSNAMES_H + + +#include "freetype.h" + + +/*************************************************************************/ +/* */ +/* */ +/* PS_Unicode_Value_Func */ +/* */ +/* */ +/* A function used to return the Unicode index corresponding to a */ +/* given glyph name. */ +/* */ +/* */ +/* glyph_name :: The glyph name. */ +/* */ +/* */ +/* The Unicode character index resp. the non-Unicode value 0xFFFF if */ +/* the glyph name has no known Unicode meaning. */ +/* */ +/* */ +/* This function is able to map several different glyph names to the */ +/* same Unicode value, according to the rules defined in the Adobe */ +/* Glyph List table. */ +/* */ +/* This function will not be compiled if the configuration macro */ +/* FT_CONFIG_OPTION_ADOBE_GLYPH_LIST is undefined. */ +/* */ +typedef FT_ULong ( *PS_Unicode_Value_Func )( const char* glyph_name ); + + +/*************************************************************************/ +/* */ +/* */ +/* PS_Unicode_Index_Func */ +/* */ +/* */ +/* A function used to return the glyph index corresponding to a given */ +/* Unicode value. */ +/* */ +/* */ +/* num_glyphs :: The number of glyphs in the face. */ +/* */ +/* glyph_names :: An array of glyph name pointers. */ +/* */ +/* unicode :: The Unicode value. */ +/* */ +/* */ +/* The glyph index resp. 0xFFFF if no glyph corresponds to this */ +/* Unicode value. */ +/* */ +/* */ +/* This function is able to recognize several glyph names per Unicode */ +/* value, according to the Adobe Glyph List. */ +/* */ +/* This function will not be compiled if the configuration macro */ +/* FT_CONFIG_OPTION_ADOBE_GLYPH_LIST is undefined. */ +/* */ +typedef FT_UInt ( *PS_Unicode_Index_Func )( FT_UInt num_glyphs, + const char** glyph_names, + FT_ULong unicode ); + + +/*************************************************************************/ +/* */ +/* */ +/* PS_Macintosh_Name_Func */ +/* */ +/* */ +/* A function used to return the glyph name corresponding to an Apple */ +/* glyph name index. */ +/* */ +/* */ +/* name_index :: The index of the Mac name. */ +/* */ +/* */ +/* The glyph name, or 0 if the index is invalid. */ +/* */ +/* */ +/* This function will not be compiled if the configuration macro */ +/* FT_CONFIG_OPTION_POSTSCRIPT_NAMES is undefined. */ +/* */ +typedef const char* ( *PS_Macintosh_Name_Func )( FT_UInt name_index ); + + +typedef const char* ( *PS_Adobe_Std_Strings_Func )( FT_UInt string_index ); + + +typedef struct PS_UniMap_ +{ + FT_UInt unicode; + FT_UInt glyph_index; + +} PS_UniMap; + + +/*************************************************************************/ +/* */ +/* */ +/* PS_Unicodes */ +/* */ +/* */ +/* A simple table used to map Unicode values to glyph indices. It is */ +/* built by the PS_Build_Unicodes table according to the glyphs */ +/* present in a font file. */ +/* */ +/* */ +/* num_codes :: The number of glyphs in the font that match a given */ +/* Unicode value. */ +/* */ +/* unicodes :: An array of unicode values, sorted in increasing */ +/* order. */ +/* */ +/* gindex :: An array of glyph indices, corresponding to each */ +/* Unicode value. */ +/* */ +/* */ +/* Use the function PS_Lookup_Unicode() to retrieve the glyph index */ +/* corresponding to a given Unicode character code. */ +/* */ +typedef struct PS_Unicodes_ +{ + FT_UInt num_maps; + PS_UniMap* maps; + +} PS_Unicodes; + + +typedef FT_Error ( *PS_Build_Unicodes_Func )( FT_Memory memory, + FT_UInt num_glyphs, + const char** glyph_names, + PS_Unicodes* unicodes ); + +typedef FT_UInt ( *PS_Lookup_Unicode_Func )( PS_Unicodes* unicodes, + FT_UInt unicode ); + + +/*************************************************************************/ +/* */ +/* */ +/* PSNames_Interface */ +/* */ +/* */ +/* This structure defines the PSNames interface. */ +/* */ +/* */ +/* unicode_value :: A function used to convert a glyph name */ +/* into a Unicode character code. */ +/* */ +/* build_unicodes :: A function which builds up the Unicode */ +/* mapping table. */ +/* */ +/* lookup_unicode :: A function used to return the glyph index */ +/* corresponding to a given Unicode */ +/* character. */ +/* */ +/* macintosh_name :: A function used to return the standard */ +/* Apple glyph Postscript name corresponding */ +/* to a given string index (used by the */ +/* TrueType `post' table). */ +/* */ +/* adobe_std_strings :: A function that returns a pointer to a */ +/* Adobe Standard String for a given SID. */ +/* */ +/* adobe_std_encoding :: A table of 256 unsigned shorts that maps */ +/* character codes in the Adobe Standard */ +/* Encoding to SIDs. */ +/* */ +/* adobe_expert_encoding :: A table of 256 unsigned shorts that maps */ +/* character codes in the Adobe Expert */ +/* Encoding to SIDs. */ +/* */ +/* */ +/* `unicode_value' and `unicode_index' will be set to 0 if the */ +/* configuration macro FT_CONFIG_OPTION_ADOBE_GLYPH_LIST is */ +/* undefined. */ +/* */ +/* `macintosh_name' will be set to 0 if the configuration macro */ +/* FT_CONFIG_OPTION_POSTSCRIPT_NAMES is undefined. */ +/* */ +typedef struct PSNames_Interface_ +{ + PS_Unicode_Value_Func unicode_value; + PS_Build_Unicodes_Func build_unicodes; + PS_Lookup_Unicode_Func lookup_unicode; + PS_Macintosh_Name_Func macintosh_name; + + PS_Adobe_Std_Strings_Func adobe_std_strings; + const unsigned short* adobe_std_encoding; + const unsigned short* adobe_expert_encoding; + +} PSNames_Interface; + + +#endif /* PSNAMES_H */ + + +/* END */ diff --git a/src/ft2/sfdriver.c b/src/ft2/sfdriver.c new file mode 100644 index 0000000..703b077 --- /dev/null +++ b/src/ft2/sfdriver.c @@ -0,0 +1,211 @@ +/***************************************************************************/ +/* */ +/* sfdriver.c */ +/* */ +/* High-level SFNT driver interface (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#include "sfnt.h" +#include "ftobjs.h" + + + +#include "sfdriver.h" +#include "ttload.h" +#include "ttsbit.h" +#include "ttpost.h" +#include "ttcmap.h" +#include "sfobjs.h" + +#include /* for strcmp() */ + + +static +void* get_sfnt_table( TT_Face face, + FT_Sfnt_Tag tag ) { + void* table; + + + switch ( tag ) + { + case ft_sfnt_head: + table = &face->header; + break; + + case ft_sfnt_hhea: + table = &face->horizontal; + break; + + case ft_sfnt_vhea: + table = face->vertical_info ? &face->vertical : 0; + break; + + case ft_sfnt_os2: + table = face->os2.version == 0xFFFF ? 0 : &face->os2; + break; + + case ft_sfnt_post: + table = &face->postscript; + break; + + case ft_sfnt_maxp: + table = &face->max_profile; + break; + + case ft_sfnt_pclt: + table = face->pclt.Version ? &face->pclt : 0; + break; + + default: + table = 0; + } + + return table; +} + + +#ifdef TT_CONFIG_OPTION_POSTSCRIPT_NAMES + + +static +FT_Error get_sfnt_glyph_name( TT_Face face, + FT_UInt glyph_index, + FT_Pointer buffer, + FT_UInt buffer_max ) { + FT_String* gname; + FT_Error error; + + + error = TT_Get_PS_Name( face, glyph_index, &gname ); + if ( !error && buffer_max > 0 ) { + FT_UInt len = strlen( gname ); + + + if ( len >= buffer_max ) { + len = buffer_max - 1; + } + + MEM_Copy( buffer, gname, len ); + ( (FT_Byte*)buffer )[len] = 0; + } + + return error; +} + + +#endif /* TT_CONFIG_OPTION_POSTSCRIPT_NAMES */ + + +static +FT_Module_Interface SFNT_Get_Interface( FT_Module module, + const char* interface ) { + FT_UNUSED( module ); + + if ( strcmp( interface, "get_sfnt" ) == 0 ) { + return (FT_Module_Interface)get_sfnt_table; + } + +#ifdef TT_CONFIG_OPTION_POSTSCRIPT_NAMES + if ( strcmp( interface, "glyph_name" ) == 0 ) { + return (FT_Module_Interface)get_sfnt_glyph_name; + } +#endif + return 0; +} + + +static +const SFNT_Interface sfnt_interface = +{ + TT_Goto_Table, + + SFNT_Init_Face, + SFNT_Load_Face, + SFNT_Done_Face, + SFNT_Get_Interface, + + TT_Load_Any, + TT_Load_SFNT_Header, + TT_Load_Directory, + + TT_Load_Header, + TT_Load_Metrics_Header, + TT_Load_CMap, + TT_Load_MaxProfile, + TT_Load_OS2, + TT_Load_PostScript, + + TT_Load_Names, + TT_Free_Names, + + TT_Load_Hdmx, + TT_Free_Hdmx, + + TT_Load_Kern, + TT_Load_Gasp, + TT_Load_PCLT, + +#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS + + /* see `ttsbit.h' */ + TT_Load_SBit_Strikes, + TT_Load_SBit_Image, + TT_Free_SBit_Strikes, + +#else /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ + + 0, + 0, + 0, + +#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ + +#ifdef TT_CONFIG_OPTION_POSTSCRIPT_NAMES + + /* see `ttpost.h' */ + TT_Get_PS_Name, + TT_Free_Post_Names, + +#else /* TT_CONFIG_OPTION_POSTSCRIPT_NAMES */ + + 0, + 0, + +#endif /* TT_CONFIG_OPTION_POSTSCRIPT_NAMES */ + + /* see `ttcmap.h' */ + TT_CharMap_Load, + TT_CharMap_Free, +}; + + +const +FT_Module_Class sfnt_module_class = +{ + 0, /* not a font driver or renderer */ + sizeof( FT_ModuleRec ), + + "sfnt", /* driver name */ + 0x10000L, /* driver version 1.0 */ + 0x20000L, /* driver requires FreeType 2.0 or higher */ + + (const void*)&sfnt_interface, /* module specific interface */ + + (FT_Module_Constructor)0, + (FT_Module_Destructor) 0, + (FT_Module_Requester) SFNT_Get_Interface +}; + + +/* END */ diff --git a/src/ft2/sfdriver.h b/src/ft2/sfdriver.h new file mode 100644 index 0000000..07bd555 --- /dev/null +++ b/src/ft2/sfdriver.h @@ -0,0 +1,29 @@ +/***************************************************************************/ +/* */ +/* sfdriver.h */ +/* */ +/* High-level SFNT driver interface (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef SFDRIVER_H +#define SFDRIVER_H + +#include "ftmodule.h" + +FT_EXPORT_VAR( const FT_Module_Class ) sfnt_module_class; + +#endif /* SFDRIVER_H */ + + +/* END */ diff --git a/src/ft2/sfnt.h b/src/ft2/sfnt.h new file mode 100644 index 0000000..4d81f9a --- /dev/null +++ b/src/ft2/sfnt.h @@ -0,0 +1,492 @@ +/***************************************************************************/ +/* */ +/* sfnt.h */ +/* */ +/* High-level `sfnt' driver interface (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef SFNT_H +#define SFNT_H + + +#include "freetype.h" +#include "ftdriver.h" +#include "tttypes.h" + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Init_Face_Func */ +/* */ +/* */ +/* First part of the SFNT face object initialization. This will find */ +/* the face in a SFNT file or collection, and load its format tag in */ +/* face->format_tag. */ +/* */ +/* */ +/* stream :: The input stream. */ +/* */ +/* face :: A handle to the target face object. */ +/* */ +/* face_index :: The index of the TrueType font, if we are opening a */ +/* collection. */ +/* */ +/* num_params :: The number of additional parameters. */ +/* */ +/* params :: Optional additional parameters. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* The stream cursor must be at the font file's origin. */ +/* */ +/* This function recognizes fonts embedded in a `TrueType */ +/* collection'. */ +/* */ +/* Once the format tag has been validated by the font driver, it */ +/* should then call the TT_Load_Face_Func() callback to read the rest */ +/* of the SFNT tables in the object. */ +/* */ +typedef +FT_Error ( *TT_Init_Face_Func )( FT_Stream stream, + TT_Face face, + FT_Int face_index, + FT_Int num_params, + FT_Parameter* params ); + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_Face_Func */ +/* */ +/* */ +/* Second part of the SFNT face object initialization. This will */ +/* load the common SFNT tables (head, OS/2, maxp, metrics, etc.) in */ +/* the face object. */ +/* */ +/* */ +/* stream :: The input stream. */ +/* */ +/* face :: A handle to the target face object. */ +/* */ +/* face_index :: The index of the TrueType font, if we are opening a */ +/* collection. */ +/* */ +/* num_params :: The number of additional parameters. */ +/* */ +/* params :: Optional additional parameters. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* This function must be called after TT_Init_Face_Func(). */ +/* */ +typedef +FT_Error ( *TT_Load_Face_Func )( FT_Stream stream, + TT_Face face, + FT_Int face_index, + FT_Int num_params, + FT_Parameter* params ); + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Done_Face_Func */ +/* */ +/* */ +/* A callback used to delete the common SFNT data from a face. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* */ +/* */ +/* This function does NOT destroy the face object. */ +/* */ +typedef +void ( *TT_Done_Face_Func )( TT_Face face ); + + +typedef +FT_Module_Interface ( *SFNT_Get_Interface_Func )( FT_Module module, + const char* interface ); + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_SFNT_Header_Func */ +/* */ +/* */ +/* Loads the header of a SFNT font file. Supports collections. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* */ +/* stream :: The input stream. */ +/* */ +/* face_index :: The index of the TrueType font, if we are opening a */ +/* collection. */ +/* */ +/* */ +/* sfnt :: The SFNT header. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* The stream cursor must be at the font file's origin. */ +/* */ +/* This function recognizes fonts embedded in a `TrueType */ +/* collection'. */ +/* */ +/* This function checks that the header is valid by looking at the */ +/* values of `search_range', `entry_selector', and `range_shift'. */ +/* */ +typedef +FT_Error ( *TT_Load_SFNT_Header_Func )( TT_Face face, + FT_Stream stream, + FT_Long face_index, + SFNT_Header* sfnt ); + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_Directory_Func */ +/* */ +/* */ +/* Loads the table directory into a face object. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* */ +/* stream :: The input stream. */ +/* */ +/* sfnt :: The SFNT header. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* The stream cursor must be on the first byte after the 4-byte font */ +/* format tag. This is the case just after a call to */ +/* TT_Load_Format_Tag(). */ +/* */ +typedef +FT_Error ( *TT_Load_Directory_Func )( TT_Face face, + FT_Stream stream, + SFNT_Header* sfnt ); + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_Any_Func */ +/* */ +/* */ +/* Loads any font table into client memory. */ +/* */ +/* */ +/* face :: The face object to look for. */ +/* */ +/* tag :: The tag of table to load. Use the value 0 if you want */ +/* to access the whole font file, else set this parameter */ +/* to a valid TrueType table tag that you can forge with */ +/* the MAKE_TT_TAG macro. */ +/* */ +/* offset :: The starting offset in the table (or the file if */ +/* tag == 0). */ +/* */ +/* length :: The address of the decision variable: */ +/* */ +/* If length == NULL: */ +/* Loads the whole table. Returns an error if */ +/* `offset' == 0! */ +/* */ +/* If *length == 0: */ +/* Exits immediately; returning the length of the given */ +/* table or of the font file, depending on the value of */ +/* `tag'. */ +/* */ +/* If *length != 0: */ +/* Loads the next `length' bytes of table or font, */ +/* starting at offset `offset' (in table or font too). */ +/* */ +/* */ +/* buffer :: The address of target buffer. */ +/* */ +/* */ +/* TrueType error code. 0 means success. */ +/* */ +typedef +FT_Error ( *TT_Load_Any_Func )( TT_Face face, + FT_ULong tag, + FT_Long offset, + FT_Byte* buffer, + FT_ULong* length ); + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_SBit_Image_Func */ +/* */ +/* */ +/* Loads a given glyph sbit image from the font resource. This also */ +/* returns its metrics. */ +/* */ +/* */ +/* face :: The target face object. */ +/* */ +/* x_ppem :: The horizontal resolution in points per EM. */ +/* */ +/* y_ppem :: The vertical resolution in points per EM. */ +/* */ +/* glyph_index :: The current glyph index. */ +/* */ +/* stream :: The input stream. */ +/* */ +/* */ +/* map :: The target pixmap. */ +/* */ +/* metrics :: A big sbit metrics structure for the glyph image. */ +/* */ +/* */ +/* FreeType error code. 0 means success. Returns an error if no */ +/* glyph sbit exists for the index. */ +/* */ +/* */ +/* The `map.buffer' field is always freed before the glyph is loaded. */ +/* */ +typedef +FT_Error ( *TT_Load_SBit_Image_Func )( TT_Face face, + FT_Int x_ppem, + FT_Int y_ppem, + FT_UInt glyph_index, + FT_UInt load_flags, + FT_Stream stream, + FT_Bitmap* map, + TT_SBit_Metrics* metrics ); + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Get_PS_Name_Func */ +/* */ +/* */ +/* Gets the PostScript glyph name of a glyph. */ +/* */ +/* */ +/* index :: The glyph index. */ +/* */ +/* PSname :: The address of a string pointer. Will be NULL in case */ +/* of error, otherwise it is a pointer to the glyph name. */ +/* */ +/* You must not modify the returned string! */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +typedef +FT_Error ( *TT_Get_PS_Name_Func )( TT_Face face, + FT_UInt index, + FT_String** PSname ); + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_Metrics_Func */ +/* */ +/* */ +/* Loads the horizontal or vertical header in a face object. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* */ +/* stream :: The input stream. */ +/* */ +/* vertical :: A boolean flag. If set, load vertical metrics. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +typedef +FT_Error ( *TT_Load_Metrics_Func )( TT_Face face, + FT_Stream stream, + FT_Bool vertical ); + + +/*************************************************************************/ +/* */ +/* */ +/* TT_CharMap_Load_Func */ +/* */ +/* */ +/* Loads a given TrueType character map into memory. */ +/* */ +/* */ +/* face :: A handle to the parent face object. */ +/* */ +/* stream :: A handle to the current stream object. */ +/* */ +/* */ +/* cmap :: A pointer to a cmap object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* The function assumes that the stream is already in use (i.e., */ +/* opened). In case of error, all partially allocated tables are */ +/* released. */ +/* */ +typedef +FT_Error ( *TT_CharMap_Load_Func )( TT_Face face, + TT_CMapTable* cmap, + FT_Stream input ); + + +/*************************************************************************/ +/* */ +/* */ +/* TT_CharMap_Free_Func */ +/* */ +/* */ +/* Destroys a character mapping table. */ +/* */ +/* */ +/* face :: A handle to the parent face object. */ +/* */ +/* cmap :: A handle to a cmap object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +typedef +FT_Error ( *TT_CharMap_Free_Func )( TT_Face face, + TT_CMapTable* cmap ); + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_Table_Func */ +/* */ +/* */ +/* Loads a given TrueType table. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* */ +/* stream :: The input stream. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* The function will use `face->goto_table' to seek the stream to */ +/* the start of the table. */ +/* */ +typedef +FT_Error ( *TT_Load_Table_Func )( TT_Face face, + FT_Stream stream ); + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Free_Table_Func */ +/* */ +/* */ +/* Frees a given TrueType table. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* */ +typedef +void ( *TT_Free_Table_Func )( TT_Face face ); + + +/*************************************************************************/ +/* */ +/* */ +/* SFNT_Interface */ +/* */ +/* */ +/* This structure holds pointers to the functions used to load and */ +/* free the basic tables that are required in a `sfnt' font file. */ +/* */ +/* */ +/* Check the various xxx_Func() descriptions for details. */ +/* */ +typedef struct SFNT_Interface_ +{ + TT_Goto_Table_Func goto_table; + + TT_Init_Face_Func init_face; + TT_Load_Face_Func load_face; + TT_Done_Face_Func done_face; + SFNT_Get_Interface_Func get_interface; + + TT_Load_Any_Func load_any; + TT_Load_SFNT_Header_Func load_sfnt_header; + TT_Load_Directory_Func load_directory; + + /* these functions are called by `load_face' but they can also */ + /* be called from external modules, if there is a need to do so */ + TT_Load_Table_Func load_header; + TT_Load_Metrics_Func load_metrics; + TT_Load_Table_Func load_charmaps; + TT_Load_Table_Func load_max_profile; + TT_Load_Table_Func load_os2; + TT_Load_Table_Func load_psnames; + + TT_Load_Table_Func load_names; + TT_Free_Table_Func free_names; + + /* optional tables */ + TT_Load_Table_Func load_hdmx; + TT_Free_Table_Func free_hdmx; + + TT_Load_Table_Func load_kerning; + TT_Load_Table_Func load_gasp; + TT_Load_Table_Func load_pclt; + + /* see `ttsbit.h' */ + TT_Load_Table_Func load_sbits; + TT_Load_SBit_Image_Func load_sbit_image; + TT_Free_Table_Func free_sbits; + + /* see `ttpost.h' */ + TT_Get_PS_Name_Func get_psname; + TT_Free_Table_Func free_psnames; + + /* see `ttcmap.h' */ + TT_CharMap_Load_Func load_charmap; + TT_CharMap_Free_Func free_charmap; + +} SFNT_Interface; + + +#endif /* SFNT_H */ + + +/* END */ diff --git a/src/ft2/sfobjs.c b/src/ft2/sfobjs.c new file mode 100644 index 0000000..6f42776 --- /dev/null +++ b/src/ft2/sfobjs.c @@ -0,0 +1,555 @@ +/***************************************************************************/ +/* */ +/* sfobjs.c */ +/* */ +/* SFNT object management (base). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + + +#include "sfobjs.h" + + +#include "sfnt.h" +#include "psnames.h" +#include "ttnameid.h" +#include "tterrors.h" + + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_sfobjs + + +/*************************************************************************/ +/* */ +/* */ +/* Get_Name */ +/* */ +/* */ +/* Returns a given ENGLISH name record in ASCII. */ +/* */ +/* */ +/* face :: A handle to the source face object. */ +/* */ +/* nameid :: The name id of the name record to return. */ +/* */ +/* */ +/* Character string. NULL if no name is present. */ +/* */ +static +FT_String* Get_Name( TT_Face face, + FT_UShort nameid ) { + FT_Memory memory = face->root.memory; + FT_UShort n; + TT_NameRec* rec; + FT_Bool wide_chars = 1; + + + rec = face->name_table.names; + for ( n = 0; n < face->name_table.numNameRecords; n++, rec++ ) + { + if ( rec->nameID == nameid ) { + /* found the name -- now create an ASCII string from it */ + FT_Bool found = 0; + + + /* test for Microsoft English language */ + if ( rec->platformID == TT_PLATFORM_MICROSOFT && + rec->encodingID <= TT_MS_ID_UNICODE_CS && + ( rec->languageID & 0x3FF ) == 0x009 ) { + found = 1; + } + /* test for Apple Unicode encoding */ + else if ( rec->platformID == TT_PLATFORM_APPLE_UNICODE ) { + found = 1; + } + /* test for Apple Roman */ + else if ( rec->platformID == TT_PLATFORM_MACINTOSH && + rec->languageID == TT_MAC_ID_ROMAN ) { + found = 1; + wide_chars = 0; + } + + /* found a Unicode name */ + if ( found ) { + FT_String* string; + FT_UInt len; + + + if ( wide_chars ) { + FT_UInt m; + + + len = (FT_UInt)rec->stringLength / 2; + if ( MEM_Alloc( string, len + 1 ) ) { + return NULL; + } + + for ( m = 0; m < len; m++ ) + string[m] = rec->string[2 * m + 1]; + } else + { + len = rec->stringLength; + if ( MEM_Alloc( string, len + 1 ) ) { + return NULL; + } + + MEM_Copy( string, rec->string, len ); + } + + string[len] = '\0'; + return string; + } + } + } + + return NULL; +} + + +static +FT_Encoding find_encoding( int platform_id, + int encoding_id ) { + typedef struct TEncoding + { + int platform_id; + int encoding_id; + FT_Encoding encoding; + + } TEncoding; + + static + const TEncoding tt_encodings[] = + { + { TT_PLATFORM_ISO, -1, ft_encoding_unicode }, + + { TT_PLATFORM_APPLE_UNICODE, -1, ft_encoding_unicode }, + + { TT_PLATFORM_MACINTOSH, TT_MAC_ID_ROMAN, ft_encoding_apple_roman }, + + { TT_PLATFORM_MICROSOFT, TT_MS_ID_UNICODE_CS, ft_encoding_unicode }, + { TT_PLATFORM_MICROSOFT, TT_MS_ID_SJIS, ft_encoding_sjis }, + { TT_PLATFORM_MICROSOFT, TT_MS_ID_GB2312, ft_encoding_gb2312 }, + { TT_PLATFORM_MICROSOFT, TT_MS_ID_BIG_5, ft_encoding_big5 }, + { TT_PLATFORM_MICROSOFT, TT_MS_ID_WANSUNG, ft_encoding_wansung }, + { TT_PLATFORM_MICROSOFT, TT_MS_ID_JOHAB, ft_encoding_johab } + }; + + const TEncoding *cur, *limit; + + + cur = tt_encodings; + limit = cur + sizeof( tt_encodings ) / sizeof( tt_encodings[0] ); + + for ( ; cur < limit; cur++ ) + { + if ( cur->platform_id == platform_id ) { + if ( cur->encoding_id == encoding_id || + cur->encoding_id == -1 ) { + return cur->encoding; + } + } + } + + return ft_encoding_none; +} + + +LOCAL_FUNC +FT_Error SFNT_Init_Face( FT_Stream stream, + TT_Face face, + FT_Int face_index, + FT_Int num_params, + FT_Parameter* params ) { + FT_Error error; + FT_Library library = face->root.driver->root.library; + SFNT_Interface* sfnt; + SFNT_Header sfnt_header; + + /* for now, parameters are unused */ + FT_UNUSED( num_params ); + FT_UNUSED( params ); + + sfnt = (SFNT_Interface*)face->sfnt; + if ( !sfnt ) { + sfnt = (SFNT_Interface*)FT_Get_Module_Interface( library, "sfnt" ); + if ( !sfnt ) { + error = FT_Err_Invalid_File_Format; + goto Exit; + } + + face->sfnt = sfnt; + face->goto_table = sfnt->goto_table; + } + + if ( !face->psnames ) { + face->psnames = (PSNames_Interface*) + FT_Get_Module_Interface( library, "psnames" ); + } + + /* check that we have a valid TrueType file */ + error = sfnt->load_sfnt_header( face, stream, face_index, &sfnt_header ); + if ( error ) { + goto Exit; + } + + face->format_tag = sfnt_header.format_tag; + face->num_tables = sfnt_header.num_tables; + + /* Load font directory */ + error = sfnt->load_directory( face, stream, &sfnt_header ); + if ( error ) { + goto Exit; + } + + face->root.num_faces = face->ttc_header.count; + if ( face->root.num_faces < 1 ) { + face->root.num_faces = 1; + } + +Exit: + return error; +} + + +#undef LOAD_ +#define LOAD_( x ) ( ( error = sfnt->load_ ## x( face, stream ) ) \ + != TT_Err_Ok ) + + +LOCAL_FUNC +FT_Error SFNT_Load_Face( FT_Stream stream, + TT_Face face, + FT_Int face_index, + FT_Int num_params, + FT_Parameter* params ) { + FT_Error error; + SFNT_Interface* sfnt = (SFNT_Interface*)face->sfnt; + + FT_UNUSED( face_index ); + FT_UNUSED( num_params ); + FT_UNUSED( params ); + + + /* Load tables */ + if ( LOAD_( header ) || + LOAD_( max_profile ) || + + /* load the `hhea' & `hmtx' tables at once */ + ( error = sfnt->load_metrics( face, stream, 0 ) ) != TT_Err_Ok || + + /* try to load the `vhea' & `vmtx' at once if present */ + ( error = sfnt->load_metrics( face, stream, 1 ) ) != TT_Err_Ok || + + LOAD_( charmaps ) || + LOAD_( names ) || + LOAD_( os2 ) || + LOAD_( psnames ) ) { + goto Exit; + } + + /* the optional tables */ + +#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS + /* embedded bitmap support. */ + if ( sfnt->load_sbits && LOAD_( sbits ) ) { + goto Exit; + } +#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ + + if ( LOAD_( hdmx ) || + LOAD_( gasp ) || + LOAD_( kerning ) || + LOAD_( pclt ) ) { + goto Exit; + } + +#ifdef TT_CONFIG_OPTION_EXTEND_ENGINE + if ( ( error = TT_Extension_Create( face ) ) != TT_Err_Ok ) { + goto Exit; + } +#endif + + face->root.family_name = Get_Name( face, TT_NAME_ID_FONT_FAMILY ); + face->root.style_name = Get_Name( face, TT_NAME_ID_FONT_SUBFAMILY ); + + /* now set up root fields */ + { + FT_Face root = &face->root; + FT_Int flags; + TT_CharMap charmap; + FT_Int n; + FT_Memory memory; + + + memory = root->memory; + + /*********************************************************************/ + /* */ + /* Compute face flags. */ + /* */ + flags = FT_FACE_FLAG_SCALABLE | /* scalable outlines */ + FT_FACE_FLAG_SFNT | /* SFNT file format */ + FT_FACE_FLAG_HORIZONTAL; /* horizontal data */ + +#ifdef TT_CONFIG_OPTION_POSTSCRIPT_NAMES + /* might need more polish to detect the presence of a Postscript */ + /* name table in the font */ + flags |= FT_FACE_FLAG_GLYPH_NAMES; +#endif + + /* fixed width font? */ + if ( face->postscript.isFixedPitch ) { + flags |= FT_FACE_FLAG_FIXED_WIDTH; + } + + /* vertical information? */ + if ( face->vertical_info ) { + flags |= FT_FACE_FLAG_VERTICAL; + } + + /* kerning available ? */ + if ( face->kern_pairs ) { + flags |= FT_FACE_FLAG_KERNING; + } + + root->face_flags = flags; + + /*********************************************************************/ + /* */ + /* Compute style flags. */ + /* */ + flags = 0; + + if ( face->os2.version != 0xFFFF ) { + /* we have an OS/2 table; use the `fsSelection' field */ + if ( face->os2.fsSelection & 1 ) { + flags |= FT_STYLE_FLAG_ITALIC; + } + + if ( face->os2.fsSelection & 32 ) { + flags |= FT_STYLE_FLAG_BOLD; + } + } else + { + /* this is an old Mac font, use the header field */ + if ( face->header.Mac_Style & 1 ) { + flags |= FT_STYLE_FLAG_BOLD; + } + + if ( face->header.Mac_Style & 2 ) { + flags |= FT_STYLE_FLAG_ITALIC; + } + } + + root->style_flags = flags; + + /*********************************************************************/ + /* */ + /* Polish the charmaps. */ + /* */ + /* Try to set the charmap encoding according to the platform & */ + /* encoding ID of each charmap. */ + /* */ + charmap = face->charmaps; + root->num_charmaps = face->num_charmaps; + + /* allocate table of pointers */ + if ( ALLOC_ARRAY( root->charmaps, root->num_charmaps, FT_CharMap ) ) { + goto Exit; + } + + for ( n = 0; n < root->num_charmaps; n++, charmap++ ) + { + FT_Int platform = charmap->cmap.platformID; + FT_Int encoding = charmap->cmap.platformEncodingID; + + + charmap->root.face = (FT_Face)face; + charmap->root.platform_id = platform; + charmap->root.encoding_id = encoding; + charmap->root.encoding = find_encoding( platform, encoding ); + + /* now, set root->charmap with a unicode charmap */ + /* wherever available */ + if ( !root->charmap && + charmap->root.encoding == ft_encoding_unicode ) { + root->charmap = (FT_CharMap)charmap; + } + + root->charmaps[n] = (FT_CharMap)charmap; + } + +#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS + + if ( face->num_sbit_strikes ) { + root->num_fixed_sizes = face->num_sbit_strikes; + if ( ALLOC_ARRAY( root->available_sizes, + face->num_sbit_strikes, + FT_Bitmap_Size ) ) { + return error; + } + + for ( n = 0 ; n < face->num_sbit_strikes ; n++ ) + { + root->available_sizes[n].width = + face->sbit_strikes[n].x_ppem; + root->available_sizes[n].height = + face->sbit_strikes[n].y_ppem; + } + } else { + +#else /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ + + { + root->num_fixed_sizes = 0; + root->available_sizes = 0; + } + +#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ { + + /*********************************************************************/ + /* */ + /* Set up metrics. */ + /* */ + root->bbox.xMin = face->header.xMin; + } + root->bbox.yMin = face->header.yMin; + root->bbox.xMax = face->header.xMax; + root->bbox.yMax = face->header.yMax; + root->units_per_EM = face->header.Units_Per_EM; + + /* The ascender/descender/height are computed from the OS/2 table */ + /* when found. Otherwise, they're taken from the horizontal header. */ + if ( face->os2.version != 0xFFFF ) { + root->ascender = face->os2.sTypoAscender; + root->descender = -face->os2.sTypoDescender; + root->height = root->ascender + root->descender + + face->os2.sTypoLineGap; + } else + { + root->ascender = face->horizontal.Ascender; + root->descender = face->horizontal.Descender; + root->height = root->ascender + root->descender + + face->horizontal.Line_Gap; + } + + root->max_advance_width = face->horizontal.advance_Width_Max; + + root->max_advance_height = face->vertical_info + ? face->vertical.advance_Height_Max + : root->height; + + root->underline_position = face->postscript.underlinePosition; + root->underline_thickness = face->postscript.underlineThickness; + + /* root->max_points -- already set up */ + /* root->max_contours -- already set up */ + } + +Exit: + return error; +} + + +#undef LOAD_ + + +LOCAL_FUNC +void SFNT_Done_Face( TT_Face face ) { + FT_Memory memory = face->root.memory; + SFNT_Interface* sfnt = (SFNT_Interface*)face->sfnt; + + + if ( sfnt ) { + /* destroy the postscript names table if it is loaded */ + if ( sfnt->free_psnames ) { + sfnt->free_psnames( face ); + } + + /* destroy the embedded bitmaps table if it is loaded */ + if ( sfnt->free_sbits ) { + sfnt->free_sbits( face ); + } + } + + /* freeing the kerning table */ + FREE( face->kern_pairs ); + face->num_kern_pairs = 0; + + /* freeing the collection table */ + FREE( face->ttc_header.offsets ); + face->ttc_header.count = 0; + + /* freeing table directory */ + FREE( face->dir_tables ); + face->num_tables = 0; + + /* freeing the character mapping tables */ + if ( sfnt && sfnt->load_charmaps ) { + FT_UShort n; + + + for ( n = 0; n < face->num_charmaps; n++ ) + sfnt->free_charmap( face, &face->charmaps[n].cmap ); + } + + FREE( face->charmaps ); + face->num_charmaps = 0; + + FREE( face->root.charmaps ); + face->root.num_charmaps = 0; + face->root.charmap = 0; + + /* freeing the horizontal metrics */ + FREE( face->horizontal.long_metrics ); + FREE( face->horizontal.short_metrics ); + + /* freeing the vertical ones, if any */ + if ( face->vertical_info ) { + FREE( face->vertical.long_metrics ); + FREE( face->vertical.short_metrics ); + face->vertical_info = 0; + } + + /* freeing the gasp table */ + FREE( face->gasp.gaspRanges ); + face->gasp.numRanges = 0; + + /* freeing the name table */ + sfnt->free_names( face ); + + /* freeing the hdmx table */ + sfnt->free_hdmx( face ); + + /* freeing family and style name */ + FREE( face->root.family_name ); + FREE( face->root.style_name ); + + /* freeing sbit size table */ + face->root.num_fixed_sizes = 0; + if ( face->root.available_sizes ) { + FREE( face->root.available_sizes ); + } + + face->sfnt = 0; +} + + +/* END */ diff --git a/src/ft2/sfobjs.h b/src/ft2/sfobjs.h new file mode 100644 index 0000000..89b8621 --- /dev/null +++ b/src/ft2/sfobjs.h @@ -0,0 +1,57 @@ +/***************************************************************************/ +/* */ +/* sfobjs.h */ +/* */ +/* SFNT object management (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef SFOBJS_H +#define SFOBJS_H + +#include "sfnt.h" +#include "ftobjs.h" + + +#ifdef __cplusplus +extern "C" { +#endif + + +LOCAL_DEF +FT_Error SFNT_Init_Face( FT_Stream stream, + TT_Face face, + FT_Int face_index, + FT_Int num_params, + FT_Parameter* params ); + +LOCAL_DEF +FT_Error SFNT_Load_Face( FT_Stream stream, + TT_Face face, + FT_Int face_index, + FT_Int num_params, + FT_Parameter* params ); + +LOCAL_DEF +void SFNT_Done_Face( TT_Face face ); + + +#ifdef __cplusplus +} +#endif + + +#endif /* SFDRIVER_H */ + + +/* END */ diff --git a/src/ft2/t1tables.h b/src/ft2/t1tables.h new file mode 100644 index 0000000..ecf41f1 --- /dev/null +++ b/src/ft2/t1tables.h @@ -0,0 +1,235 @@ +/***************************************************************************/ +/* */ +/* t1tables.h */ +/* */ +/* Basic Type 1/Type 2 tables definitions and interface (specification */ +/* only). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef T1TABLES_H +#define T1TABLES_H + + +#include "freetype.h" + + +/* Note that we separate font data in T1_FontInfo and T1_Private */ +/* structures in order to support Multiple Master fonts. */ + + +/*************************************************************************/ +/* */ +/* */ +/* T1_FontInfo */ +/* */ +/* */ +/* A structure used to model a Type1/Type2 FontInfo dictionary. Note */ +/* that for Multiple Master fonts, each instance has its own */ +/* FontInfo. */ +/* */ +typedef struct T1_FontInfo +{ + FT_String* version; + FT_String* notice; + FT_String* full_name; + FT_String* family_name; + FT_String* weight; + FT_Long italic_angle; + FT_Bool is_fixed_pitch; + FT_Short underline_position; + FT_UShort underline_thickness; + +} T1_FontInfo; + + +/*************************************************************************/ +/* */ +/* */ +/* T1_Private */ +/* */ +/* */ +/* A structure used to model a Type1/Type2 FontInfo dictionary. Note */ +/* that for Multiple Master fonts, each instance has its own Private */ +/* dict. */ +/* */ +typedef struct T1_Private +{ + FT_Int unique_id; + FT_Int lenIV; + + FT_Byte num_blue_values; + FT_Byte num_other_blues; + FT_Byte num_family_blues; + FT_Byte num_family_other_blues; + + FT_Short blue_values[14]; + FT_Short other_blues[10]; + + FT_Short family_blues [14]; + FT_Short family_other_blues[10]; + + FT_Fixed blue_scale; + FT_Int blue_shift; + FT_Int blue_fuzz; + + FT_UShort standard_width[1]; + FT_UShort standard_height[1]; + + FT_Byte num_snap_widths; + FT_Byte num_snap_heights; + FT_Bool force_bold; + FT_Bool round_stem_up; + + FT_Short snap_widths [13]; /* reserve one place for the std */ + FT_Short snap_heights[13]; /* reserve one place for the std */ + + FT_Long language_group; + FT_Long password; + + FT_Short min_feature[2]; + +} T1_Private; + + +/*************************************************************************/ +/* */ +/* */ +/* T1_Blend_Flags */ +/* */ +/* */ +/* A set of flags used to indicate which fields are present in a */ +/* given blen dictionary (font info or private). Used to support */ +/* Multiple Masters fonts. */ +/* */ +typedef enum +{ + /* required fields in a FontInfo blend dictionary */ + t1_blend_underline_position = 0, + t1_blend_underline_thickness, + t1_blend_italic_angle, + + /* required fields in a Private blend dictionary */ + t1_blend_blue_values, + t1_blend_other_blues, + t1_blend_standard_width, + t1_blend_standard_height, + t1_blend_stem_snap_widths, + t1_blend_stem_snap_heights, + t1_blend_blue_scale, + t1_blend_blue_shift, + t1_blend_family_blues, + t1_blend_family_other_blues, + t1_blend_force_bold, + + /* never remove */ + t1_blend_max + +} T1_Blend_Flags; + + +/* maximum number of Multiple Masters designs, as defined in the spec */ +#define T1_MAX_MM_DESIGNS 16 + +/* maximum number of Multiple Masters axes, as defined in the spec */ +#define T1_MAX_MM_AXIS 4 + +/* maximum number of elements in a design map */ +#define T1_MAX_MM_MAP_POINTS 20 + + +/* this structure is used to store the BlendDesignMap entry for an axis */ +typedef struct T1_DesignMap_ +{ + FT_Byte num_points; + FT_Fixed* design_points; + FT_Fixed* blend_points; + +} T1_DesignMap; + + +typedef struct T1_Blend_ +{ + FT_UInt num_designs; + FT_UInt num_axis; + + FT_String* axis_names[T1_MAX_MM_AXIS]; + FT_Fixed* design_pos[T1_MAX_MM_DESIGNS]; + T1_DesignMap design_map[T1_MAX_MM_AXIS]; + + FT_Fixed* weight_vector; + FT_Fixed* default_weight_vector; + + T1_FontInfo* font_infos[T1_MAX_MM_DESIGNS + 1]; + T1_Private* privates [T1_MAX_MM_DESIGNS + 1]; + + FT_ULong blend_bitflags; + +} T1_Blend; + + +typedef struct CID_FontDict_ +{ + T1_Private private_dict; + + FT_UInt len_buildchar; + FT_Fixed forcebold_threshold; + FT_Pos stroke_width; + FT_Fixed expansion_factor; + + FT_Byte paint_type; + FT_Byte font_type; + FT_Matrix font_matrix; + + FT_UInt num_subrs; + FT_ULong subrmap_offset; + FT_Int sd_bytes; + +} CID_FontDict; + + +typedef struct CID_Info_ +{ + FT_String* cid_font_name; + FT_Fixed cid_version; + FT_Int cid_font_type; + + FT_String* registry; + FT_String* ordering; + FT_Int supplement; + + T1_FontInfo font_info; + FT_BBox font_bbox; + FT_ULong uid_base; + + FT_Int num_xuid; + FT_ULong xuid[16]; + + + FT_ULong cidmap_offset; + FT_Int fd_bytes; + FT_Int gd_bytes; + FT_ULong cid_count; + + FT_Int num_dicts; + CID_FontDict* font_dicts; + + FT_ULong data_offset; + +} CID_Info; + + +#endif /* T1TABLES_H */ + + +/* END */ diff --git a/src/ft2/ttcmap.c b/src/ft2/ttcmap.c new file mode 100644 index 0000000..4eb1a77 --- /dev/null +++ b/src/ft2/ttcmap.c @@ -0,0 +1,550 @@ +/***************************************************************************/ +/* */ +/* ttcmap.c */ +/* */ +/* TrueType character mapping table (cmap) support (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#include "ftdebug.h" +#include "tterrors.h" + + +#include "ttload.h" +#include "ttcmap.h" + + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_ttcmap + + +static FT_UInt code_to_index0( TT_CMapTable* charmap, + FT_ULong char_code ); +static FT_UInt code_to_index2( TT_CMapTable* charmap, + FT_ULong char_code ); +static FT_UInt code_to_index4( TT_CMapTable* charmap, + FT_ULong char_code ); +static FT_UInt code_to_index6( TT_CMapTable* charmap, + FT_ULong char_code ); + + +/*************************************************************************/ +/* */ +/* */ +/* TT_CharMap_Load */ +/* */ +/* */ +/* Loads a given TrueType character map into memory. */ +/* */ +/* */ +/* face :: A handle to the parent face object. */ +/* stream :: A handle to the current stream object. */ +/* */ +/* */ +/* table :: A pointer to a cmap object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* The function assumes that the stream is already in use (i.e., */ +/* opened). In case of error, all partially allocated tables are */ +/* released. */ +/* */ +LOCAL_FUNC +FT_Error TT_CharMap_Load( TT_Face face, + TT_CMapTable* cmap, + FT_Stream stream ) { + FT_Error error; + FT_Memory memory; + FT_UShort num_SH, num_Seg, i; + + FT_UShort u, l; + + TT_CMap0* cmap0; + TT_CMap2* cmap2; + TT_CMap4* cmap4; + TT_CMap6* cmap6; + + TT_CMap2SubHeader* cmap2sub; + TT_CMap4Segment* segments; + + + if ( cmap->loaded ) { + return TT_Err_Ok; + } + + memory = stream->memory; + + if ( FILE_Seek( cmap->offset ) ) { + return error; + } + + switch ( cmap->format ) + { + case 0: + cmap0 = &cmap->c.cmap0; + + if ( ALLOC( cmap0->glyphIdArray, 256L ) || + FILE_Read( cmap0->glyphIdArray, 256L ) ) { + goto Fail; + } + + cmap->get_index = code_to_index0; + break; + + case 2: + num_SH = 0; + cmap2 = &cmap->c.cmap2; + + /* allocate subheader keys */ + + if ( ALLOC_ARRAY( cmap2->subHeaderKeys, 256, FT_UShort ) || + ACCESS_Frame( 512L ) ) { + goto Fail; + } + + for ( i = 0; i < 256; i++ ) + { + u = GET_UShort() / 8; + cmap2->subHeaderKeys[i] = u; + + if ( num_SH < u ) { + num_SH = u; + } + } + + FORGET_Frame(); + + /* load subheaders */ + + cmap2->numGlyphId = l = + ( ( cmap->length - 2L * ( 256 + 3 ) - num_SH * 8L ) & 0xFFFF ) / 2; + + if ( ALLOC_ARRAY( cmap2->subHeaders, + num_SH + 1, + TT_CMap2SubHeader ) || + ACCESS_Frame( ( num_SH + 1 ) * 8L ) ) { + goto Fail; + } + + cmap2sub = cmap2->subHeaders; + + for ( i = 0; i <= num_SH; i++ ) + { + cmap2sub->firstCode = GET_UShort(); + cmap2sub->entryCount = GET_UShort(); + cmap2sub->idDelta = GET_Short(); + /* we apply the location offset immediately */ + cmap2sub->idRangeOffset = GET_UShort() - ( num_SH - i ) * 8 - 2; + + cmap2sub++; + } + + FORGET_Frame(); + + /* load glyph IDs */ + + if ( ALLOC_ARRAY( cmap2->glyphIdArray, l, FT_UShort ) || + ACCESS_Frame( l * 2L ) ) { + goto Fail; + } + + for ( i = 0; i < l; i++ ) + cmap2->glyphIdArray[i] = GET_UShort(); + + FORGET_Frame(); + + cmap->get_index = code_to_index2; + break; + + case 4: + cmap4 = &cmap->c.cmap4; + + /* load header */ + + if ( ACCESS_Frame( 8L ) ) { + goto Fail; + } + + cmap4->segCountX2 = GET_UShort(); + cmap4->searchRange = GET_UShort(); + cmap4->entrySelector = GET_UShort(); + cmap4->rangeShift = GET_UShort(); + + num_Seg = cmap4->segCountX2 / 2; + + FORGET_Frame(); + + /* load segments */ + + if ( ALLOC_ARRAY( cmap4->segments, + num_Seg, + TT_CMap4Segment ) || + ACCESS_Frame( ( num_Seg * 4 + 1 ) * 2L ) ) { + goto Fail; + } + + segments = cmap4->segments; + + for ( i = 0; i < num_Seg; i++ ) + segments[i].endCount = GET_UShort(); + + (void)GET_UShort(); + + for ( i = 0; i < num_Seg; i++ ) + segments[i].startCount = GET_UShort(); + + for ( i = 0; i < num_Seg; i++ ) + segments[i].idDelta = GET_Short(); + + for ( i = 0; i < num_Seg; i++ ) + segments[i].idRangeOffset = GET_UShort(); + + FORGET_Frame(); + + cmap4->numGlyphId = l = + ( ( cmap->length - ( 16L + 8L * num_Seg ) ) & 0xFFFF ) / 2; + + /* load IDs */ + + if ( ALLOC_ARRAY( cmap4->glyphIdArray, l, FT_UShort ) || + ACCESS_Frame( l * 2L ) ) { + goto Fail; + } + + for ( i = 0; i < l; i++ ) + cmap4->glyphIdArray[i] = GET_UShort(); + + FORGET_Frame(); + + cmap->get_index = code_to_index4; + + cmap4->last_segment = cmap4->segments; + break; + + case 6: + cmap6 = &cmap->c.cmap6; + + if ( ACCESS_Frame( 4L ) ) { + goto Fail; + } + + cmap6->firstCode = GET_UShort(); + cmap6->entryCount = GET_UShort(); + + FORGET_Frame(); + + l = cmap6->entryCount; + + if ( ALLOC_ARRAY( cmap6->glyphIdArray, + cmap6->entryCount, + FT_Short ) || + ACCESS_Frame( l * 2L ) ) { + goto Fail; + } + + for ( i = 0; i < l; i++ ) + cmap6->glyphIdArray[i] = GET_UShort(); + + FORGET_Frame(); + cmap->get_index = code_to_index6; + break; + + default: /* corrupt character mapping table */ + return TT_Err_Invalid_CharMap_Format; + + } + + return TT_Err_Ok; + +Fail: + TT_CharMap_Free( face, cmap ); + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_CharMap_Free */ +/* */ +/* */ +/* Destroys a character mapping table. */ +/* */ +/* */ +/* face :: A handle to the parent face object. */ +/* cmap :: A handle to a cmap object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_CharMap_Free( TT_Face face, + TT_CMapTable* cmap ) { + FT_Memory memory; + + + if ( !cmap ) { + return TT_Err_Ok; + } + + memory = face->root.driver->root.memory; + + switch ( cmap->format ) + { + case 0: + FREE( cmap->c.cmap0.glyphIdArray ); + break; + + case 2: + FREE( cmap->c.cmap2.subHeaderKeys ); + FREE( cmap->c.cmap2.subHeaders ); + FREE( cmap->c.cmap2.glyphIdArray ); + break; + + case 4: + FREE( cmap->c.cmap4.segments ); + FREE( cmap->c.cmap4.glyphIdArray ); + cmap->c.cmap4.segCountX2 = 0; + break; + + case 6: + FREE( cmap->c.cmap6.glyphIdArray ); + cmap->c.cmap6.entryCount = 0; + break; + + default: + /* invalid table format, do nothing */ + ; + } + + cmap->loaded = FALSE; + return TT_Err_Ok; +} + + +/*************************************************************************/ +/* */ +/* */ +/* code_to_index0 */ +/* */ +/* */ +/* Converts the character code into a glyph index. Uses format 0. */ +/* `charCode' must be in the range 0x00-0xFF (otherwise 0 is */ +/* returned). */ +/* */ +/* */ +/* charCode :: The wanted character code. */ +/* cmap0 :: A pointer to a cmap table in format 0. */ +/* */ +/* */ +/* Glyph index into the glyphs array. 0 if the glyph does not exist. */ +/* */ +static +FT_UInt code_to_index0( TT_CMapTable* cmap, + FT_ULong charCode ) { + TT_CMap0* cmap0 = &cmap->c.cmap0; + + + return ( charCode <= 0xFF ? cmap0->glyphIdArray[charCode] : 0 ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* code_to_index2 */ +/* */ +/* */ +/* Converts the character code into a glyph index. Uses format 2. */ +/* */ +/* */ +/* charCode :: The wanted character code. */ +/* cmap2 :: A pointer to a cmap table in format 2. */ +/* */ +/* */ +/* Glyph index into the glyphs array. 0 if the glyph does not exist. */ +/* */ +static +FT_UInt code_to_index2( TT_CMapTable* cmap, + FT_ULong charCode ) { + FT_UInt result, index1, offset; + FT_UInt char_lo; + FT_ULong char_hi; + TT_CMap2SubHeader* sh2; + TT_CMap2* cmap2; + + + cmap2 = &cmap->c.cmap2; + result = 0; + char_lo = (FT_UInt)( charCode & 0xFF ); + char_hi = charCode >> 8; + + if ( char_hi == 0 ) { + /* an 8-bit character code -- we use the subHeader 0 in this case */ + /* to test whether the character code is in the charmap */ + if ( cmap2->subHeaderKeys[char_lo] == 0 ) { + result = cmap2->glyphIdArray[char_lo]; + } + } else + { + /* a 16-bit character code */ + index1 = cmap2->subHeaderKeys[char_hi & 0xFF]; + if ( index1 ) { + sh2 = cmap2->subHeaders + index1; + char_lo -= sh2->firstCode; + + if ( char_lo < sh2->entryCount ) { + offset = sh2->idRangeOffset / 2 + char_lo; + if ( offset < cmap2->numGlyphId ) { + result = cmap2->glyphIdArray[offset]; + if ( result ) { + result = ( result + sh2->idDelta ) & 0xFFFF; + } + } + } + } + } + + return result; +} + + +/*************************************************************************/ +/* */ +/* */ +/* code_to_index4 */ +/* */ +/* */ +/* Converts the character code into a glyph index. Uses format 4. */ +/* */ +/* */ +/* charCode :: The wanted character code. */ +/* cmap4 :: A pointer to a cmap table in format 4. */ +/* */ +/* */ +/* Glyph index into the glyphs array. 0 if the glyph does not exist. */ +/* */ +static +FT_UInt code_to_index4( TT_CMapTable* cmap, + FT_ULong charCode ) { + FT_UInt result, index1, segCount; + TT_CMap4* cmap4; + TT_CMap4Segment *seg4, *limit; + + + cmap4 = &cmap->c.cmap4; + result = 0; + segCount = cmap4->segCountX2 / 2; + seg4 = cmap4->segments; + limit = seg4 + segCount; + + /* check against the last segment */ + seg4 = cmap4->last_segment; + + /* the following is equivalent to performing two tests, as in */ + /* */ + /* if ( charCode >= seg4->startCount && charCode <= seg4->endCount ) */ + /* */ + /* Yes, that's a bit strange, but it's faster, and the idea behind */ + /* the cache is to significantly speed up charcode to glyph index */ + /* conversion. */ + + if ( (FT_ULong)( charCode - seg4->startCount ) < + (FT_ULong)( seg4->endCount - seg4->startCount ) ) { + goto Found; + } + + for ( seg4 = cmap4->segments; seg4 < limit; seg4++ ) + { + /* the ranges are sorted in increasing order. If we are out of */ + /* the range here, the char code isn't in the charmap, so exit. */ + + if ( charCode > seg4->endCount ) { + continue; + } + + if ( charCode >= seg4->startCount ) { + goto Found; + } + } + return 0; + +Found: + cmap4->last_segment = seg4; + + /* if the idRangeOffset is 0, we can compute the glyph index */ + /* directly */ + + if ( seg4->idRangeOffset == 0 ) { + result = ( charCode + seg4->idDelta ) & 0xFFFF; + } else + { + /* otherwise, we must use the glyphIdArray to do it */ + index1 = seg4->idRangeOffset / 2 + + ( charCode - seg4->startCount ) + + ( seg4 - cmap4->segments ) + - segCount; + + if ( index1 < cmap4->numGlyphId && + cmap4->glyphIdArray[index1] != 0 ) { + result = ( cmap4->glyphIdArray[index1] + seg4->idDelta ) & 0xFFFF; + } + } + + return result; +} + + +/*************************************************************************/ +/* */ +/* */ +/* code_to_index6 */ +/* */ +/* */ +/* Converts the character code into a glyph index. Uses format 6. */ +/* */ +/* */ +/* charCode :: The wanted character code. */ +/* cmap6 :: A pointer to a cmap table in format 6. */ +/* */ +/* */ +/* Glyph index into the glyphs array. 0 if the glyph does not exist. */ +/* */ +static +FT_UInt code_to_index6( TT_CMapTable* cmap, + FT_ULong charCode ) { + TT_CMap6* cmap6; + FT_UInt result = 0; + + + cmap6 = &cmap->c.cmap6; + result = 0; + charCode -= cmap6->firstCode; + + if ( charCode < cmap6->entryCount ) { + result = cmap6->glyphIdArray[charCode]; + } + + return result; +} + + +/* END */ diff --git a/src/ft2/ttcmap.h b/src/ft2/ttcmap.h new file mode 100644 index 0000000..2c22cba --- /dev/null +++ b/src/ft2/ttcmap.h @@ -0,0 +1,45 @@ +/***************************************************************************/ +/* */ +/* ttcmap.h */ +/* */ +/* TrueType character mapping table (cmap) support (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef TTCMAP_H +#define TTCMAP_H + +#include "tttypes.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +LOCAL_DEF +FT_Error TT_CharMap_Load( TT_Face face, + TT_CMapTable* cmap, + FT_Stream input ); + +LOCAL_DEF +FT_Error TT_CharMap_Free( TT_Face face, + TT_CMapTable* cmap ); + +#ifdef __cplusplus +} +#endif + +#endif /* TTCMAP_H */ + + +/* END */ diff --git a/src/ft2/ttdriver.c b/src/ft2/ttdriver.c new file mode 100644 index 0000000..cbe1502 --- /dev/null +++ b/src/ft2/ttdriver.c @@ -0,0 +1,501 @@ +/***************************************************************************/ +/* */ +/* ttdriver.c */ +/* */ +/* TrueType font driver implementation (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#include "ftdebug.h" +#include "ftstream.h" +#include "sfnt.h" +#include "ttnameid.h" + + +#include "ttdriver.h" +#include "ttgload.h" + + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_ttdriver + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** ****/ +/**** F A C E S ****/ +/**** ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +#undef PAIR_TAG +#define PAIR_TAG( left, right ) ( ( (FT_ULong)left << 16 ) | \ + (FT_ULong)right ) + + +/*************************************************************************/ +/* */ +/* */ +/* Get_Kerning */ +/* */ +/* */ +/* A driver method used to return the kerning vector between two */ +/* glyphs of the same face. */ +/* */ +/* */ +/* face :: A handle to the source face object. */ +/* */ +/* left_glyph :: The index of the left glyph in the kern pair. */ +/* */ +/* right_glyph :: The index of the right glyph in the kern pair. */ +/* */ +/* */ +/* kerning :: The kerning vector. This is in font units for */ +/* scalable formats, and in pixels for fixed-sizes */ +/* formats. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* Only horizontal layouts (left-to-right & right-to-left) are */ +/* supported by this function. Other layouts, or more sophisticated */ +/* kernings, are out of scope of this method (the basic driver */ +/* interface is meant to be simple). */ +/* */ +/* They can be implemented by format-specific interfaces. */ +/* */ +static +FT_Error Get_Kerning( TT_Face face, + FT_UInt left_glyph, + FT_UInt right_glyph, + FT_Vector* kerning ) { + TT_Kern_0_Pair* pair; + + + if ( !face ) { + return TT_Err_Invalid_Face_Handle; + } + + kerning->x = 0; + kerning->y = 0; + + if ( face->kern_pairs ) { + /* there are some kerning pairs in this font file! */ + FT_ULong search_tag = PAIR_TAG( left_glyph, right_glyph ); + FT_Long left, right; + + + left = 0; + right = face->num_kern_pairs - 1; + + while ( left <= right ) + { + FT_Int middle = left + ( ( right - left ) >> 1 ); + FT_ULong cur_pair; + + + pair = face->kern_pairs + middle; + cur_pair = PAIR_TAG( pair->left, pair->right ); + + if ( cur_pair == search_tag ) { + goto Found; + } + + if ( cur_pair < search_tag ) { + left = middle + 1; + } else { + right = middle - 1; + } + } + } + +Exit: + return TT_Err_Ok; + +Found: + kerning->x = pair->value; + goto Exit; +} + + +#undef PAIR_TAG + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** ****/ +/**** S I Z E S ****/ +/**** ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* Set_Char_Sizes */ +/* */ +/* */ +/* A driver method used to reset a size's character sizes (horizontal */ +/* and vertical) expressed in fractional points. */ +/* */ +/* */ +/* char_width :: The character width expressed in 26.6 */ +/* fractional points. */ +/* */ +/* char_height :: The character height expressed in 26.6 */ +/* fractional points. */ +/* */ +/* horz_resolution :: The horizontal resolution of the output device. */ +/* */ +/* vert_resolution :: The vertical resolution of the output device. */ +/* */ +/* */ +/* size :: A handle to the target size object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +static +FT_Error Set_Char_Sizes( TT_Size size, + FT_F26Dot6 char_width, + FT_F26Dot6 char_height, + FT_UInt horz_resolution, + FT_UInt vert_resolution ) { + FT_Size_Metrics* metrics = &size->root.metrics; + TT_Face face = (TT_Face)size->root.face; + FT_Long dim_x, dim_y; + + + /* This bit flag, when set, indicates that the pixel size must be */ + /* truncated to an integer. Nearly all TrueType fonts have this */ + /* bit set, as hinting won't work really well otherwise. */ + /* */ + /* However, for those rare fonts who do not set it, we override */ + /* the default computations performed by the base layer. I */ + /* really don't know whether this is useful, but hey, that's the */ + /* spec :-) */ + /* */ + if ( ( face->header.Flags & 8 ) == 0 ) { + /* Compute pixel sizes in 26.6 units */ + dim_x = ( char_width * horz_resolution ) / 72; + dim_y = ( char_height * vert_resolution ) / 72; + + metrics->x_scale = FT_DivFix( dim_x, face->root.units_per_EM ); + metrics->y_scale = FT_DivFix( dim_y, face->root.units_per_EM ); + + metrics->x_ppem = (FT_UShort)( dim_x >> 6 ); + metrics->y_ppem = (FT_UShort)( dim_y >> 6 ); + } + + size->ttmetrics.valid = FALSE; + + return TT_Reset_Size( size ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* Set_Pixel_Sizes */ +/* */ +/* */ +/* A driver method used to reset a size's character sizes (horizontal */ +/* and vertical) expressed in integer pixels. */ +/* */ +/* */ +/* pixel_width :: The character width expressed in integer pixels. */ +/* */ +/* pixel_height :: The character height expressed in integer pixels. */ +/* */ +/* */ +/* size :: A handle to the target size object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +static +FT_Error Set_Pixel_Sizes( TT_Size size, + FT_UInt pixel_width, + FT_UInt pixel_height ) { + FT_UNUSED( pixel_width ); + FT_UNUSED( pixel_height ); + + /* many things have been pre-computed by the base layer */ + + size->ttmetrics.valid = FALSE; + + return TT_Reset_Size( size ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* Load_Glyph */ +/* */ +/* */ +/* A driver method used to load a glyph within a given glyph slot. */ +/* */ +/* */ +/* slot :: A handle to the target slot object where the glyph */ +/* will be loaded. */ +/* */ +/* size :: A handle to the source face size at which the glyph */ +/* must be scaled, loaded, etc. */ +/* */ +/* glyph_index :: The index of the glyph in the font file. */ +/* */ +/* load_flags :: A flag indicating what to load for this glyph. The */ +/* FTLOAD_??? constants can be used to control the */ +/* glyph loading process (e.g., whether the outline */ +/* should be scaled, whether to load bitmaps or not, */ +/* whether to hint the outline, etc). */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +static +FT_Error Load_Glyph( TT_GlyphSlot slot, + TT_Size size, + FT_UShort glyph_index, + FT_UInt load_flags ) { + FT_Error error; + + + if ( !slot ) { + return TT_Err_Invalid_Glyph_Handle; + } + + /* check whether we want a scaled outline or bitmap */ + if ( !size ) { + load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING; + } + + if ( load_flags & FT_LOAD_NO_SCALE ) { + size = NULL; + } + + /* reset the size object if necessary */ + if ( size ) { + /* these two object must have the same parent */ + if ( size->root.face != slot->face ) { + return TT_Err_Invalid_Face_Handle; + } + + if ( !size->ttmetrics.valid ) { + if ( FT_SET_ERROR( TT_Reset_Size( size ) ) ) { + return error; + } + } + } + + /* now load the glyph outline if necessary */ + error = TT_Load_Glyph( size, slot, glyph_index, load_flags ); + + /* force drop-out mode to 2 - irrelevant now */ + /* slot->outline.dropout_mode = 2; */ + + return error; +} + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** ****/ +/**** C H A R A C T E R M A P P I N G S ****/ +/**** ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + +/*************************************************************************/ +/* */ +/* */ +/* Get_Char_Index */ +/* */ +/* */ +/* Uses a charmap to return a given character code's glyph index. */ +/* */ +/* */ +/* charmap :: A handle to the source charmap object. */ +/* charcode :: The character code. */ +/* */ +/* */ +/* Glyph index. 0 means `undefined character code'. */ +/* */ +static +FT_UInt Get_Char_Index( TT_CharMap charmap, + FT_Long charcode ) { + FT_Error error; + TT_Face face; + TT_CMapTable* cmap; + + + cmap = &charmap->cmap; + face = (TT_Face)charmap->root.face; + + /* Load table if needed */ + if ( !cmap->loaded ) { + SFNT_Interface* sfnt = (SFNT_Interface*)face->sfnt; + + + error = sfnt->load_charmap( face, cmap, face->root.stream ); + if ( error ) { + return 0; + } + + cmap->loaded = TRUE; + } + + if ( cmap->get_index ) { + return cmap->get_index( cmap, charcode ); + } else { + return 0; + } +} + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/**** ****/ +/**** ****/ +/**** D R I V E R I N T E R F A C E ****/ +/**** ****/ +/**** ****/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +static +FT_Module_Interface tt_get_interface( TT_Driver driver, + const char* interface ) { + FT_Module sfntd = FT_Get_Module( driver->root.root.library, + "sfnt" ); + SFNT_Interface* sfnt; + + + /* only return the default interface from the SFNT module */ + if ( sfntd ) { + sfnt = ( SFNT_Interface* )( sfntd->clazz->module_interface ); + if ( sfnt ) { + return sfnt->get_interface( FT_MODULE( driver ), interface ); + } + } + + return 0; +} + + +/* The FT_DriverInterface structure is defined in ftdriver.h. */ + +const FT_Driver_Class tt_driver_class = +{ + { + ft_module_font_driver | + ft_module_driver_scalable | +#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER + ft_module_driver_has_hinter, +#else + 0, +#endif + + sizeof( TT_DriverRec ), + + "truetype", /* driver name */ + 0x10000L, /* driver version == 1.0 */ + 0x20000L, /* driver requires FreeType 2.0 or above */ + + (void*)0, /* driver specific interface */ + + (FT_Module_Constructor)TT_Init_Driver, + (FT_Module_Destructor) TT_Done_Driver, + (FT_Module_Requester) tt_get_interface, + }, + + sizeof( TT_FaceRec ), + sizeof( TT_SizeRec ), + sizeof( FT_GlyphSlotRec ), + + + (FTDriver_initFace) TT_Init_Face, + (FTDriver_doneFace) TT_Done_Face, + (FTDriver_initSize) TT_Init_Size, + (FTDriver_doneSize) TT_Done_Size, + (FTDriver_initGlyphSlot)0, + (FTDriver_doneGlyphSlot)0, + + (FTDriver_setCharSizes) Set_Char_Sizes, + (FTDriver_setPixelSizes)Set_Pixel_Sizes, + (FTDriver_loadGlyph) Load_Glyph, + (FTDriver_getCharIndex) Get_Char_Index, + + (FTDriver_getKerning) Get_Kerning, + (FTDriver_attachFile) 0, + (FTDriver_getAdvances) 0 +}; + + +#ifdef FT_CONFIG_OPTION_DYNAMIC_DRIVERS + + +/*************************************************************************/ +/* */ +/* */ +/* getDriverInterface */ +/* */ +/* */ +/* This function is used when compiling the TrueType driver as a */ +/* shared library (`.DLL' or `.so'). It will be used by the */ +/* high-level library of FreeType to retrieve the address of the */ +/* driver's generic interface. */ +/* */ +/* It shouldn't be implemented in a static build, as each driver must */ +/* have the same function as an exported entry point. */ +/* */ +/* */ +/* The address of the TrueType's driver generic interface. The */ +/* format-specific interface can then be retrieved through the method */ +/* interface->get_format_interface. */ +/* */ +EXPORT_FUNC( const FT_Driver_Class* ) getDriverClass( void ) +{ + return &tt_driver_class; +} + + +#endif /* CONFIG_OPTION_DYNAMIC_DRIVERS */ + + +/* END */ diff --git a/src/ft2/ttdriver.h b/src/ft2/ttdriver.h new file mode 100644 index 0000000..27e2a05 --- /dev/null +++ b/src/ft2/ttdriver.h @@ -0,0 +1,31 @@ +/***************************************************************************/ +/* */ +/* ttdriver.h */ +/* */ +/* High-level TrueType driver interface (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef TTDRIVER_H +#define TTDRIVER_H + +#include "ftdriver.h" + + +FT_EXPORT_VAR( const FT_Driver_Class ) tt_driver_class; + + +#endif /* TTDRIVER_H */ + + +/* END */ diff --git a/src/ft2/tterrors.h b/src/ft2/tterrors.h new file mode 100644 index 0000000..d922944 --- /dev/null +++ b/src/ft2/tterrors.h @@ -0,0 +1,121 @@ +/***************************************************************************/ +/* */ +/* tterrors.h */ +/* */ +/* TrueType error ID definitions (specification only). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef TTERRORS_H +#define TTERRORS_H + + +/*************************************************************************/ +/* */ +/* Error codes declaration */ +/* */ +/* The error codes are grouped in `classes' used to indicate the `level' */ +/* at which the error happened. The class is given by an error code's */ +/* high byte. */ +/* */ +/*************************************************************************/ + + +/* Success is always 0. */ + +#define TT_Err_Ok FT_Err_Ok + +/* High level API errors. */ + +#define TT_Err_Invalid_File_Format FT_Err_Invalid_File_Format +#define TT_Err_Invalid_Argument FT_Err_Invalid_Argument +#define TT_Err_Invalid_Driver_Handle FT_Err_Invalid_Driver_Handle +#define TT_Err_Invalid_Face_Handle FT_Err_Invalid_Face_Handle +#define TT_Err_Invalid_Instance_Handle FT_Err_Invalid_Size_Handle +#define TT_Err_Invalid_Glyph_Handle FT_Err_Invalid_Slot_Handle +#define TT_Err_Invalid_CharMap_Handle FT_Err_Invalid_CharMap_Handle +#define TT_Err_Invalid_Glyph_Index FT_Err_Invalid_Glyph_Index + +#define TT_Err_Unimplemented_Feature FT_Err_Unimplemented_Feature + +#define TT_Err_Invalid_Engine FT_Err_Invalid_Driver_Handle + +/* Internal errors. */ + +#define TT_Err_Out_Of_Memory FT_Err_Out_Of_Memory +#define TT_Err_Unlisted_Object FT_Err_Unlisted_Object + +/* General glyph outline errors. */ + +#define TT_Err_Too_Many_Ins FT_Err_Too_Many_Hints +#define TT_Err_Invalid_Composite FT_Err_Invalid_Composite + +/* Bytecode interpreter error codes. */ + +/* These error codes are produced by the TrueType */ +/* bytecode interpreter. They usually indicate a */ +/* broken font file, a broken glyph within a font */ +/* file, or a bug in the interpreter! */ + +#define TT_Err_Invalid_Opcode 0x400 +#define TT_Err_Too_Few_Arguments 0x401 +#define TT_Err_Stack_Overflow 0x402 +#define TT_Err_Code_Overflow 0x403 +#define TT_Err_Bad_Argument 0x404 +#define TT_Err_Divide_By_Zero 0x405 +#define TT_Err_Storage_Overflow 0x406 +#define TT_Err_Cvt_Overflow 0x407 +#define TT_Err_Invalid_Reference 0x408 +#define TT_Err_Invalid_Distance 0x409 +#define TT_Err_Interpolate_Twilight 0x40A +#define TT_Err_Debug_OpCode 0x40B +#define TT_Err_ENDF_In_Exec_Stream 0x40C +#define TT_Err_Out_Of_CodeRanges 0x40D +#define TT_Err_Nested_DEFS 0x40E +#define TT_Err_Invalid_CodeRange 0x40F +#define TT_Err_Invalid_Displacement 0x410 +#define TT_Err_Execution_Too_Long 0x411 +#define TT_Err_Too_Many_Function_Defs 0x412 +#define TT_Err_Too_Many_Instruction_Defs 0x413 + +/* Other TrueType specific error codes. */ + +#define TT_Err_Table_Missing 0x420 +#define TT_Err_Too_Many_Extensions 0x421 +#define TT_Err_Extensions_Unsupported 0x422 +#define TT_Err_Invalid_Extension_Id 0x423 + +#define TT_Err_No_Vertical_Data 0x424 + +#define TT_Err_Max_Profile_Missing 0x430 +#define TT_Err_Header_Table_Missing 0x431 +#define TT_Err_Horiz_Header_Missing 0x432 +#define TT_Err_Locations_Missing 0x433 +#define TT_Err_Name_Table_Missing 0x434 +#define TT_Err_CMap_Table_Missing 0x435 +#define TT_Err_Hmtx_Table_Missing 0x436 +#define TT_Err_OS2_Table_Missing 0x437 +#define TT_Err_Post_Table_Missing 0x438 + +#define TT_Err_Invalid_Horiz_Metrics 0x440 +#define TT_Err_Invalid_CharMap_Format 0x441 +#define TT_Err_Invalid_PPem 0x442 +#define TT_Err_Invalid_Vert_Metrics 0x443 + +#define TT_Err_Could_Not_Find_Context 0x450 + + +#endif /* TTERRORS_H */ + + +/* END */ diff --git a/src/ft2/ttgload.c b/src/ft2/ttgload.c new file mode 100644 index 0000000..d7bce20 --- /dev/null +++ b/src/ft2/ttgload.c @@ -0,0 +1,1432 @@ +/***************************************************************************/ +/* */ +/* ttgload.c */ +/* */ +/* TrueType Glyph Loader (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#include "ftdebug.h" +#include "ftcalc.h" +#include "ftstream.h" +#include "sfnt.h" +#include "tttags.h" +#include "ftoutln.h" + + +#include "ttgload.h" + + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_ttgload + + +/*************************************************************************/ +/* */ +/* Composite font flags. */ +/* */ +#define ARGS_ARE_WORDS 0x001 +#define ARGS_ARE_XY_VALUES 0x002 +#define ROUND_XY_TO_GRID 0x004 +#define WE_HAVE_A_SCALE 0x008 +/* reserved 0x010 */ +#define MORE_COMPONENTS 0x020 +#define WE_HAVE_AN_XY_SCALE 0x040 +#define WE_HAVE_A_2X2 0x080 +#define WE_HAVE_INSTR 0x100 +#define USE_MY_METRICS 0x200 + + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Get_Metrics */ +/* */ +/* */ +/* Returns the horizontal or vertical metrics in font units for a */ +/* given glyph. The metrics are the left side bearing (resp. top */ +/* side bearing) and advance width (resp. advance height). */ +/* */ +/* */ +/* header :: A pointer to either the horizontal or vertical metrics */ +/* structure. */ +/* */ +/* index :: The glyph index. */ +/* */ +/* */ +/* bearing :: The bearing, either left side or top side. */ +/* */ +/* advance :: The advance width resp. advance height. */ +/* */ +/* */ +/* This function will much probably move to another component in the */ +/* near future, but I haven't decided which yet. */ +/* */ +LOCAL_FUNC +void TT_Get_Metrics( TT_HoriHeader* header, + FT_UInt index, + FT_Short* bearing, + FT_UShort* advance ) { + TT_LongMetrics* longs_m; + FT_UShort k = header->number_Of_HMetrics; + + + if ( index < k ) { + longs_m = (TT_LongMetrics*)header->long_metrics + index; + *bearing = longs_m->bearing; + *advance = longs_m->advance; + } else + { + *bearing = ( (TT_ShortMetrics*)header->short_metrics )[index - k]; + *advance = ( (TT_LongMetrics*)header->long_metrics )[k - 1].advance; + } +} + + +/*************************************************************************/ +/* */ +/* Returns the horizontal metrics in font units for a given glyph. If */ +/* `check' is true, take care of monospaced fonts by returning the */ +/* advance width maximum. */ +/* */ +static +void Get_HMetrics( TT_Face face, + FT_UInt index, + FT_Bool check, + FT_Short* lsb, + FT_UShort* aw ) { + TT_Get_Metrics( &face->horizontal, index, lsb, aw ); + + if ( check && face->postscript.isFixedPitch ) { + *aw = face->horizontal.advance_Width_Max; + } +} + + +/*************************************************************************/ +/* */ +/* Returns the advance width table for a given pixel size if it is */ +/* found in the font's `hdmx' table (if any). */ +/* */ +static +FT_Byte* Get_Advance_Widths( TT_Face face, + FT_UShort ppem ) { + FT_UShort n; + + for ( n = 0; n < face->hdmx.num_records; n++ ) + if ( face->hdmx.records[n].ppem == ppem ) { + return face->hdmx.records[n].widths; + } + + return NULL; +} + + +#define cur_to_org( n, zone ) \ + MEM_Copy( ( zone )->org, ( zone )->cur, n * sizeof( FT_Vector ) ) + +#define org_to_cur( n, zone ) \ + MEM_Copy( ( zone )->cur, ( zone )->org, n * sizeof( FT_Vector ) ) + + +/*************************************************************************/ +/* */ +/* Translates an array of coordinates. */ +/* */ +static +void translate_array( FT_UInt n, + FT_Vector* coords, + FT_Pos delta_x, + FT_Pos delta_y ) { + FT_UInt k; + + + if ( delta_x ) { + for ( k = 0; k < n; k++ ) + coords[k].x += delta_x; + } + + if ( delta_y ) { + for ( k = 0; k < n; k++ ) + coords[k].y += delta_y; + } +} + + +static +void tt_prepare_zone( TT_GlyphZone* zone, + FT_GlyphLoad* load, + FT_UInt start_point, + FT_UInt start_contour ) { + zone->n_points = load->outline.n_points - start_point; + zone->n_contours = load->outline.n_contours - start_contour; + zone->org = load->extra_points + start_point; + zone->cur = load->outline.points + start_point; + zone->tags = (FT_Byte*)load->outline.tags + start_point; + zone->contours = (FT_UShort*)load->outline.contours + start_contour; +} + + +#undef IS_HINTED +#define IS_HINTED( flags ) ( ( flags & FT_LOAD_NO_HINTING ) == 0 ) + + +/*************************************************************************/ +/* */ +/* The following functions are used by default with TrueType fonts. */ +/* However, they can be replaced by alternatives if we need to support */ +/* TrueType-compressed formats (like MicroType) in the future. */ +/* */ +/*************************************************************************/ + +static +FT_Error TT_Access_Glyph_Frame( TT_Loader* loader, + FT_UInt glyph_index, + FT_ULong offset, + FT_UInt byte_count ) { + FT_Error error; + FT_Stream stream = loader->stream; + + + /* the following line sets the `error' variable through macros! */ + (void)( FILE_Seek( offset ) || ACCESS_Frame( byte_count ) ); + + FT_TRACE5( ( "Glyph %ld\n", glyph_index ) ); + return error; +} + + +static +void TT_Forget_Glyph_Frame( TT_Loader* loader ) { + FT_Stream stream = loader->stream; + + + FORGET_Frame(); +} + + +static +FT_Error TT_Load_Glyph_Header( TT_Loader* loader ) { + FT_Stream stream = loader->stream; + + + loader->n_contours = GET_Short(); + + loader->bbox.xMin = GET_Short(); + loader->bbox.yMin = GET_Short(); + loader->bbox.xMax = GET_Short(); + loader->bbox.yMax = GET_Short(); + + FT_TRACE5( ( " # of contours: %d\n", loader->n_contours ) ); + FT_TRACE5( ( " xMin: %4d xMax: %4d\n", loader->bbox.xMin, + loader->bbox.xMax ) ); + FT_TRACE5( ( " yMin: %4d yMax: %4d\n", loader->bbox.yMin, + loader->bbox.yMax ) ); + + return FT_Err_Ok; +} + + +static +FT_Error TT_Load_Simple_Glyph( TT_Loader* load ) { + FT_Error error; + FT_Stream stream = load->stream; + FT_GlyphLoader* gloader = load->gloader; + FT_Int n_contours = load->n_contours; + FT_Outline* outline; + TT_Face face = (TT_Face)load->face; + TT_GlyphSlot slot = (TT_GlyphSlot)load->glyph; + FT_UShort n_ins; + FT_Int n, n_points; + + + /* reading the contours endpoints & number of points */ + { + short* cur = gloader->current.outline.contours; + short* limit = cur + n_contours; + + + for ( ; cur < limit; cur++ ) + cur[0] = GET_UShort(); + + n_points = 0; + if ( n_contours > 0 ) { + n_points = cur[-1] + 1; + } + + error = FT_GlyphLoader_Check_Points( gloader, n_points + 2, 0 ); + if ( error ) { + goto Fail; + } + + outline = &gloader->current.outline; + } + + /* reading the bytecode instructions */ + slot->control_len = 0; + slot->control_data = 0; + + n_ins = GET_UShort(); + + FT_TRACE5( ( " Instructions size: %d\n", n_ins ) ); + + if ( n_ins > face->max_profile.maxSizeOfInstructions ) { + FT_TRACE0( ( "ERROR: Too many instructions!\n" ) ); + error = TT_Err_Too_Many_Ins; + goto Fail; + } + + if ( stream->cursor + n_ins > stream->limit ) { + FT_TRACE0( ( "ERROR: Instruction count mismatch!\n" ) ); + error = TT_Err_Too_Many_Ins; + goto Fail; + } + +#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER + + if ( ( load->load_flags & + ( FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING ) ) == 0 && + load->instructions ) { + slot->control_len = n_ins; + slot->control_data = load->instructions; + + MEM_Copy( load->instructions, stream->cursor, n_ins ); + } + +#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */ + + stream->cursor += n_ins; + + /* reading the point tags */ + + { + FT_Byte* flag = (FT_Byte*)outline->tags; + FT_Byte* limit = flag + n_points; + FT_Byte c, count; + + + for ( ; flag < limit; flag++ ) + { + *flag = c = GET_Byte(); + if ( c & 8 ) { + for ( count = GET_Byte(); count > 0; count-- ) + *++flag = c; + } + } + } + + /* reading the X coordinates */ + + { + FT_Vector* vec = outline->points; + FT_Vector* limit = vec + n_points; + FT_Byte* flag = (FT_Byte*)outline->tags; + FT_Pos x = 0; + + + for ( ; vec < limit; vec++, flag++ ) + { + FT_Pos y = 0; + + + if ( *flag & 2 ) { + y = GET_Byte(); + if ( ( *flag & 16 ) == 0 ) { + y = -y; + } + } else if ( ( *flag & 16 ) == 0 ) { + y = GET_Short(); + } + + x += y; + vec->x = x; + } + } + + /* reading the Y coordinates */ + + { + FT_Vector* vec = gloader->current.outline.points; + FT_Vector* limit = vec + n_points; + FT_Byte* flag = (FT_Byte*)outline->tags; + FT_Pos x = 0; + + + for ( ; vec < limit; vec++, flag++ ) + { + FT_Pos y = 0; + + + if ( *flag & 4 ) { + y = GET_Byte(); + if ( ( *flag & 32 ) == 0 ) { + y = -y; + } + } else if ( ( *flag & 32 ) == 0 ) { + y = GET_Short(); + } + + x += y; + vec->y = x; + } + } + + /* clear the touch tags */ + for ( n = 0; n < n_points; n++ ) + outline->tags[n] &= FT_Curve_Tag_On; + + outline->n_points = n_points; + outline->n_contours = n_contours; + +Fail: + return error; +} + + +static +FT_Error TT_Load_Composite_Glyph( TT_Loader* loader ) { + FT_Error error; + FT_Stream stream = loader->stream; + FT_GlyphLoader* gloader = loader->gloader; + FT_SubGlyph* subglyph; + FT_UInt num_subglyphs; + + + num_subglyphs = 0; + + do + { + FT_Fixed xx, xy, yy, yx; + + + /* check that we can load a new subglyph */ + error = FT_GlyphLoader_Check_Subglyphs( gloader, num_subglyphs + 1 ); + if ( error ) { + goto Fail; + } + + subglyph = gloader->current.subglyphs + num_subglyphs; + + subglyph->arg1 = subglyph->arg2 = 0; + + subglyph->flags = GET_UShort(); + subglyph->index = GET_UShort(); + + /* read arguments */ + if ( subglyph->flags & ARGS_ARE_WORDS ) { + subglyph->arg1 = GET_Short(); + subglyph->arg2 = GET_Short(); + } else + { + subglyph->arg1 = GET_Char(); + subglyph->arg2 = GET_Char(); + } + + /* read transform */ + xx = yy = 0x10000L; + xy = yx = 0; + + if ( subglyph->flags & WE_HAVE_A_SCALE ) { + xx = (FT_Fixed)GET_Short() << 2; + yy = xx; + } else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE ) { + xx = (FT_Fixed)GET_Short() << 2; + yy = (FT_Fixed)GET_Short() << 2; + } else if ( subglyph->flags & WE_HAVE_A_2X2 ) { + xx = (FT_Fixed)GET_Short() << 2; + xy = (FT_Fixed)GET_Short() << 2; + yx = (FT_Fixed)GET_Short() << 2; + yy = (FT_Fixed)GET_Short() << 2; + } + + subglyph->transform.xx = xx; + subglyph->transform.xy = xy; + subglyph->transform.yx = yx; + subglyph->transform.yy = yy; + + num_subglyphs++; + + } while ( subglyph->flags & MORE_COMPONENTS ); + + gloader->current.num_subglyphs = num_subglyphs; + +#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER + { + /* we must undo the ACCESS_Frame in order to point to the */ + /* composite instructions, if we find some. */ + /* we will process them later... */ + /* */ + loader->ins_pos = FILE_Pos() + stream->cursor - stream->limit; + } +#endif + +Fail: + return error; +} + + +LOCAL_FUNC +void TT_Init_Glyph_Loading( TT_Face face ) { + face->access_glyph_frame = TT_Access_Glyph_Frame; + face->read_glyph_header = TT_Load_Glyph_Header; + face->read_simple_glyph = TT_Load_Simple_Glyph; + face->read_composite_glyph = TT_Load_Composite_Glyph; + face->forget_glyph_frame = TT_Forget_Glyph_Frame; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Process_Simple_Glyph */ +/* */ +/* */ +/* Once a simple glyph has been loaded, it needs to be processed. */ +/* Usually, this means scaling and hinting through bytecode */ +/* interpretation. */ +/* */ +static +FT_Error TT_Process_Simple_Glyph( TT_Loader* load, + FT_Bool debug ) { + FT_GlyphLoader* gloader = load->gloader; + FT_Outline* outline = &gloader->current.outline; + FT_UInt n_points = outline->n_points; + FT_UInt n_ins; + TT_GlyphZone* zone = &load->zone; + FT_Error error = FT_Err_Ok; + + + n_ins = load->glyph->control_len; + + /* add shadow points */ + + /* Now add the two shadow points at n and n + 1. */ + /* We need the left side bearing and advance width. */ + + { + FT_Vector* pp1; + FT_Vector* pp2; + + + /* pp1 = xMin - lsb */ + pp1 = outline->points + n_points; + pp1->x = load->bbox.xMin - load->left_bearing; + pp1->y = 0; + + /* pp2 = pp1 + aw */ + pp2 = pp1 + 1; + pp2->x = pp1->x + load->advance; + pp2->y = 0; + + outline->tags[n_points ] = 0; + outline->tags[n_points + 1] = 0; + } + + /* Note that we return two more points that are not */ + /* part of the glyph outline. */ + + n_points += 2; + + /* set up zone for hinting */ + tt_prepare_zone( zone, &gloader->current, 0, 0 ); + + /* eventually scale the glyph */ + if ( !( load->load_flags & FT_LOAD_NO_SCALE ) ) { + FT_Vector* vec = zone->cur; + FT_Vector* limit = vec + n_points; + FT_Fixed x_scale = load->size->metrics.x_scale; + FT_Fixed y_scale = load->size->metrics.y_scale; + + + /* first scale the glyph points */ + for ( ; vec < limit; vec++ ) + { + vec->x = FT_MulFix( vec->x, x_scale ); + vec->y = FT_MulFix( vec->y, y_scale ); + } + } + + cur_to_org( n_points, zone ); + + /* eventually hint the glyph */ + if ( IS_HINTED( load->load_flags ) ) { + FT_Pos x = zone->org[n_points - 2].x; + + + x = ( ( x + 32 ) & - 64 ) - x; + translate_array( n_points, zone->org, x, 0 ); + + org_to_cur( n_points, zone ); + + zone->cur[n_points - 1].x = ( zone->cur[n_points - 1].x + 32 ) & - 64; + +#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER + + /* now consider hinting */ + if ( n_ins > 0 ) { + error = TT_Set_CodeRange( load->exec, tt_coderange_glyph, + load->exec->glyphIns, n_ins ); + if ( error ) { + goto Exit; + } + + load->exec->is_composite = FALSE; + load->exec->pedantic_hinting = (FT_Bool)( load->load_flags & + FT_LOAD_PEDANTIC ); + load->exec->pts = *zone; + load->exec->pts.n_points += 2; + + error = TT_Run_Context( load->exec, debug ); + if ( error && load->exec->pedantic_hinting ) { + goto Exit; + } + + error = FT_Err_Ok; /* ignore bytecode errors in non-pedantic mode */ + } + +#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */ + + } + + /* save glyph phantom points */ + if ( !load->preserve_pps ) { + load->pp1 = zone->cur[n_points - 2]; + load->pp2 = zone->cur[n_points - 1]; + } + +#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER +Exit: +#endif + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* load_truetype_glyph */ +/* */ +/* */ +/* Loads a given truetype glyph. Handles composites and uses a */ +/* TT_Loader object. */ +/* */ +static +FT_Error load_truetype_glyph( TT_Loader* loader, + FT_UInt glyph_index ) { + FT_Stream stream = loader->stream; + FT_Error error; + TT_Face face = (TT_Face)loader->face; + FT_ULong offset; + FT_Int contours_count; + FT_UInt index, num_points, num_contours, count; + FT_Fixed x_scale, y_scale; + FT_ULong ins_offset; + FT_GlyphLoader* gloader = loader->gloader; + FT_Bool opened_frame = 0; + + + /* check glyph index */ + index = glyph_index; + if ( index >= (FT_UInt)face->root.num_glyphs ) { + error = TT_Err_Invalid_Glyph_Index; + goto Exit; + } + + loader->glyph_index = glyph_index; + num_contours = 0; + num_points = 0; + ins_offset = 0; + + x_scale = 0x10000L; + y_scale = 0x10000L; + if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) { + x_scale = loader->size->metrics.x_scale; + y_scale = loader->size->metrics.y_scale; + } + + /* get horizontal metrics */ + { + FT_Short left_bearing; + FT_UShort advance_width; + + + Get_HMetrics( face, index, + ( FT_Bool ) !( loader->load_flags & + FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH ), + &left_bearing, + &advance_width ); + + loader->left_bearing = left_bearing; + loader->advance = advance_width; + } + + offset = face->glyph_locations[index]; + count = 0; + + if ( index < (FT_UInt)face->num_locations - 1 ) { + count = face->glyph_locations[index + 1] - offset; + } + + if ( count == 0 ) { + /* as described by Frederic Loyer, these are spaces, and */ + /* not the unknown glyph. */ + loader->bbox.xMin = 0; + loader->bbox.xMax = 0; + loader->bbox.yMin = 0; + loader->bbox.yMax = 0; + + loader->pp1.x = 0; + loader->pp2.x = loader->advance; + + if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) { + loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale ); + } + +#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER + + if ( loader->exec ) { + loader->exec->glyphSize = 0; + } + +#endif + + error = FT_Err_Ok; + goto Exit; + } + + offset = loader->glyf_offset + offset; + + /* access glyph frame */ + error = face->access_glyph_frame( loader, glyph_index, offset, count ); + if ( error ) { + goto Exit; + } + + opened_frame = 1; + + /* read first glyph header */ + error = face->read_glyph_header( loader ); + if ( error ) { + goto Fail; + } + + contours_count = loader->n_contours; + + count -= 10; + + loader->pp1.x = loader->bbox.xMin - loader->left_bearing; + loader->pp1.y = 0; + loader->pp2.x = loader->pp1.x + loader->advance; + loader->pp2.y = 0; + + if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) { + loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale ); + loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale ); + } + + /***********************************************************************/ + /***********************************************************************/ + /***********************************************************************/ + + /* if it is a simple glyph, load it */ + + if ( contours_count >= 0 ) { + /* check that we can add the contours to the glyph */ + error = FT_GlyphLoader_Check_Points( gloader, 0, contours_count ); + if ( error ) { + goto Fail; + } + + error = face->read_simple_glyph( loader ); + if ( error ) { + goto Fail; + } + +#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER + + { + TT_Size size = (TT_Size)loader->size; + + + error = TT_Process_Simple_Glyph( loader, + (FT_Bool)( size && size->debug ) ); + } + +#else + + error = TT_Process_Simple_Glyph( loader, 0 ); + +#endif + + if ( error ) { + goto Fail; + } + + FT_GlyphLoader_Add( gloader ); + + /* Note: We could have put the simple loader source there */ + /* but the code is fat enough already :-) */ + } + /***********************************************************************/ + /***********************************************************************/ + /***********************************************************************/ + /* otherwise, load a composite! */ + else + { + TT_GlyphSlot glyph = (TT_GlyphSlot)loader->glyph; + FT_UInt start_point, start_contour; + FT_ULong ins_pos; /* position of composite instructions, if any */ + + + /* for each subglyph, read composite header */ + start_point = gloader->base.outline.n_points; + start_contour = gloader->base.outline.n_contours; + + error = face->read_composite_glyph( loader ); + if ( error ) { + goto Fail; + } + + ins_pos = loader->ins_pos; + face->forget_glyph_frame( loader ); + opened_frame = 0; + + /* if the flag FT_LOAD_NO_RECURSE is set, we return the subglyph */ + /* `as is' in the glyph slot (the client application will be */ + /* responsible for interpreting this data)... */ + /* */ + if ( loader->load_flags & FT_LOAD_NO_RECURSE ) { + /* set up remaining glyph fields */ + FT_GlyphLoader_Add( gloader ); + + glyph->num_subglyphs = gloader->base.num_subglyphs; + glyph->format = ft_glyph_format_composite; + glyph->subglyphs = gloader->base.subglyphs; + + goto Exit; + } + + /*********************************************************************/ + /*********************************************************************/ + /*********************************************************************/ + + /* Now, read each subglyph independently. */ + { + FT_Int n, num_base_points, num_new_points; + FT_SubGlyph* subglyph = 0; + + FT_UInt num_subglyphs = gloader->current.num_subglyphs; + FT_UInt num_base_subgs = gloader->base.num_subglyphs; + + + FT_GlyphLoader_Add( gloader ); + + for ( n = 0; n < (FT_Int)num_subglyphs; n++ ) + { + FT_Vector pp1, pp2; + FT_Pos x, y; + + + /* Each time we call load_truetype_glyph in this loop, the */ + /* value of `gloader.base.subglyphs' can change due to table */ + /* reallocations. We thus need to recompute the subglyph */ + /* pointer on each iteration. */ + subglyph = gloader->base.subglyphs + num_base_subgs + n; + + pp1 = loader->pp1; + pp2 = loader->pp2; + + num_base_points = gloader->base.outline.n_points; + + error = load_truetype_glyph( loader, subglyph->index ); + if ( error ) { + goto Fail; + } + + subglyph = gloader->base.subglyphs + num_base_subgs + n; + + if ( subglyph->flags & USE_MY_METRICS ) { + pp1 = loader->pp1; + pp2 = loader->pp2; + } else + { + loader->pp1 = pp1; + loader->pp2 = pp2; + } + + num_points = gloader->base.outline.n_points; + + num_new_points = num_points - num_base_points; + + /* now perform the transform required for this subglyph */ + + if ( subglyph->flags & ( WE_HAVE_A_SCALE | + WE_HAVE_AN_XY_SCALE | + WE_HAVE_A_2X2 ) ) { + FT_Vector* cur = gloader->base.outline.points + + num_base_points; + FT_Vector* org = gloader->base.extra_points + + num_base_points; + FT_Vector* limit = cur + num_new_points; + + + for ( ; cur < limit; cur++, org++ ) + { + FT_Vector_Transform( cur, &subglyph->transform ); + FT_Vector_Transform( org, &subglyph->transform ); + } + } + + /* apply offset */ + + if ( !( subglyph->flags & ARGS_ARE_XY_VALUES ) ) { + FT_UInt k = subglyph->arg1; + FT_UInt l = subglyph->arg2; + FT_Vector* p1; + FT_Vector* p2; + + + if ( start_point + k >= (FT_UInt)num_base_points || + l >= (FT_UInt)num_new_points ) { + error = TT_Err_Invalid_Composite; + goto Fail; + } + + l += num_base_points; + + p1 = gloader->base.outline.points + start_point + k; + p2 = gloader->base.outline.points + start_point + l; + + x = p1->x - p2->x; + y = p1->y - p2->y; + } else + { + x = subglyph->arg1; + y = subglyph->arg2; + + if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) ) { + x = FT_MulFix( x, x_scale ); + y = FT_MulFix( y, y_scale ); + + if ( subglyph->flags & ROUND_XY_TO_GRID ) { + x = ( x + 32 ) & - 64; + y = ( y + 32 ) & - 64; + } + } + } + + translate_array( num_new_points, loader->zone.cur, x, y ); + cur_to_org( num_new_points, &loader->zone ); + } + + /*******************************************************************/ + /*******************************************************************/ + /*******************************************************************/ + + /* we have finished loading all sub-glyphs; now, look for */ + /* instructions for this composite! */ + +#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER + + if ( num_subglyphs > 0 && + loader->exec && + ins_pos > 0 && + subglyph->flags & WE_HAVE_INSTR ) { + FT_UShort n_ins; + TT_ExecContext exec = loader->exec; + TT_GlyphZone* pts; + FT_Vector* pp1; + + + /* read size of instructions */ + if ( FILE_Seek( ins_pos ) || + READ_UShort( n_ins ) ) { + goto Fail; + } + FT_TRACE5( ( " Instructions size = %d\n", n_ins ) ); + + /* in some fonts? */ + if ( n_ins == 0xFFFF ) { + n_ins = 0; + } + + /* check it */ + if ( n_ins > face->max_profile.maxSizeOfInstructions ) { + FT_TRACE0( ( "Too many instructions (%d) in composite glyph %ld\n", + n_ins, subglyph->index ) ); + return TT_Err_Too_Many_Ins; + } + + /* read the instructions */ + if ( FILE_Read( exec->glyphIns, n_ins ) ) { + goto Fail; + } + + glyph->control_data = exec->glyphIns; + glyph->control_len = n_ins; + + error = TT_Set_CodeRange( exec, + tt_coderange_glyph, + exec->glyphIns, + n_ins ); + if ( error ) { + goto Fail; + } + + /* prepare the execution context */ + tt_prepare_zone( &exec->pts, &gloader->base, + start_point, start_contour ); + pts = &exec->pts; + + pts->n_points = num_points + 2; + pts->n_contours = gloader->base.outline.n_contours; + + /* add phantom points */ + pp1 = pts->cur + num_points; + pp1[0] = loader->pp1; + pp1[1] = loader->pp2; + + pts->tags[num_points ] = 0; + pts->tags[num_points + 1] = 0; + + /* if hinting, round the phantom points */ + if ( IS_HINTED( loader->load_flags ) ) { + pp1[0].x = ( ( loader->pp1.x + 32 ) & - 64 ); + pp1[1].x = ( ( loader->pp2.x + 32 ) & - 64 ); + } + + { + FT_UInt k; + + + for ( k = 0; k < num_points; k++ ) + pts->tags[k] &= FT_Curve_Tag_On; + } + + cur_to_org( num_points + 2, pts ); + + /* now consider hinting */ + if ( IS_HINTED( loader->load_flags ) && n_ins > 0 ) { + exec->is_composite = TRUE; + exec->pedantic_hinting = + (FT_Bool)( loader->load_flags & FT_LOAD_PEDANTIC ); + + error = TT_Run_Context( exec, ( (TT_Size)loader->size )->debug ); + if ( error && exec->pedantic_hinting ) { + goto Fail; + } + } + + /* save glyph origin and advance points */ + loader->pp1 = pp1[0]; + loader->pp2 = pp1[1]; + } + +#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */ + + } + /* end of composite loading */ + } + + /***********************************************************************/ + /***********************************************************************/ + /***********************************************************************/ + +Fail: + if ( opened_frame ) { + face->forget_glyph_frame( loader ); + } + +Exit: + return error; +} + + +static +void compute_glyph_metrics( TT_Loader* loader, + FT_UInt glyph_index ) { + FT_BBox bbox; + TT_Face face = (TT_Face)loader->face; + FT_Fixed x_scale, y_scale; + TT_GlyphSlot glyph = loader->glyph; + TT_Size size = (TT_Size)loader->size; + + + x_scale = 0x10000L; + y_scale = 0x10000L; + if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) { + x_scale = size->root.metrics.x_scale; + y_scale = size->root.metrics.y_scale; + } + + if ( glyph->format != ft_glyph_format_composite ) { + glyph->outline.flags &= ~ft_outline_single_pass; + + /* copy outline to our glyph slot */ + FT_GlyphLoader_Copy_Points( glyph->loader, loader->gloader ); + glyph->outline = glyph->loader->base.outline; + + /* translate array so that (0,0) is the glyph's origin */ + FT_Outline_Translate( &glyph->outline, -loader->pp1.x, 0 ); + + FT_Outline_Get_CBox( &glyph->outline, &bbox ); + + if ( IS_HINTED( loader->load_flags ) ) { + /* grid-fit the bounding box */ + bbox.xMin &= -64; + bbox.yMin &= -64; + bbox.xMax = ( bbox.xMax + 63 ) & - 64; + bbox.yMax = ( bbox.yMax + 63 ) & - 64; + } + } else { + bbox = loader->bbox; + } + + /* get the device-independent horizontal advance. It is scaled later */ + /* by the base layer. */ + { + FT_Pos advance = loader->advance; + + + /* the flag FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH was introduced to */ + /* correctly support DynaLab fonts, which have an incorrect */ + /* `advance_Width_Max' field! It is used, to my knowledge, */ + /* exclusively in the X-TrueType font server. */ + /* */ + if ( face->postscript.isFixedPitch && + ( loader->load_flags & FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH ) == 0 ) { + advance = face->horizontal.advance_Width_Max; + } + + /* we need to return the advance in font units in linearHoriAdvance, */ + /* it will be scaled later by the base layer. */ + glyph->linearHoriAdvance = advance; + } + + glyph->metrics.horiBearingX = bbox.xMin; + glyph->metrics.horiBearingY = bbox.yMax; + glyph->metrics.horiAdvance = loader->pp2.x - loader->pp1.x; + + /* Now take care of vertical metrics. In the case where there is */ + /* no vertical information within the font (relatively common), make */ + /* up some metrics by `hand'... */ + + { + FT_Short top_bearing; /* vertical top side bearing (EM units) */ + FT_UShort advance_height; /* vertical advance height (EM units) */ + + FT_Pos left; /* scaled vertical left side bearing */ + FT_Pos Top; /* scaled original vertical top side bearing */ + FT_Pos top; /* scaled vertical top side bearing */ + FT_Pos advance; /* scaled vertical advance height */ + + + /* Get the unscaled `tsb' and `ah' */ + if ( face->vertical_info && + face->vertical.number_Of_VMetrics > 0 ) { + /* Don't assume that both the vertical header and vertical */ + /* metrics are present in the same font :-) */ + + TT_Get_Metrics( (TT_HoriHeader*)&face->vertical, + glyph_index, + &top_bearing, + &advance_height ); + } else + { + /* Make up the distances from the horizontal header. */ + + /* NOTE: The OS/2 values are the only `portable' ones, */ + /* which is why we use them, if there is an OS/2 */ + /* table in the font. Otherwise, we use the */ + /* values defined in the horizontal header. */ + /* */ + /* NOTE2: The sTypoDescender is negative, which is why */ + /* we compute the baseline-to-baseline distance */ + /* here with: */ + /* ascender - descender + linegap */ + /* */ + if ( face->os2.version != 0xFFFF ) { + top_bearing = face->os2.sTypoLineGap / 2; + advance_height = (FT_UShort)( face->os2.sTypoAscender - + face->os2.sTypoDescender + + face->os2.sTypoLineGap ); + } else + { + top_bearing = face->horizontal.Line_Gap / 2; + advance_height = (FT_UShort)( face->horizontal.Ascender + + face->horizontal.Descender + + face->horizontal.Line_Gap ); + } + } + + /* We must adjust the top_bearing value from the bounding box given */ + /* in the glyph header to te bounding box calculated with */ + /* FT_Get_Outline_CBox(). */ + + /* scale the metrics */ + if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) ) { + Top = FT_MulFix( top_bearing, y_scale ); + top = FT_MulFix( top_bearing + loader->bbox.yMax, y_scale ) + - bbox.yMax; + advance = FT_MulFix( advance_height, y_scale ); + } else + { + Top = top_bearing; + top = top_bearing + loader->bbox.yMax - bbox.yMax; + advance = advance_height; + } + + /* set the advance height in design units. It is scaled later by */ + /* the base layer. */ + glyph->linearVertAdvance = advance_height; + + /* XXX: for now, we have no better algorithm for the lsb, but it */ + /* should work fine. */ + /* */ + left = ( bbox.xMin - bbox.xMax ) / 2; + + /* grid-fit them if necessary */ + if ( IS_HINTED( loader->load_flags ) ) { + left &= -64; + top = ( top + 63 ) & - 64; + advance = ( advance + 32 ) & - 64; + } + + glyph->metrics.vertBearingX = left; + glyph->metrics.vertBearingY = top; + glyph->metrics.vertAdvance = advance; + } + + /* adjust advance width to the value contained in the hdmx table */ + if ( !face->postscript.isFixedPitch && size && + IS_HINTED( loader->load_flags ) ) { + FT_Byte* widths = Get_Advance_Widths( face, + size->root.metrics.x_ppem ); + + + if ( widths ) { + glyph->metrics.horiAdvance = widths[glyph_index] << 6; + } + } + + /* set glyph dimensions */ + glyph->metrics.width = bbox.xMax - bbox.xMin; + glyph->metrics.height = bbox.yMax - bbox.yMin; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_Glyph */ +/* */ +/* */ +/* A function used to load a single glyph within a given glyph slot, */ +/* for a given size. */ +/* */ +/* */ +/* glyph :: A handle to a target slot object where the glyph */ +/* will be loaded. */ +/* */ +/* size :: A handle to the source face size at which the glyph */ +/* must be scaled/loaded. */ +/* */ +/* glyph_index :: The index of the glyph in the font file. */ +/* */ +/* load_flags :: A flag indicating what to load for this glyph. The */ +/* FT_LOAD_XXX constants can be used to control the */ +/* glyph loading process (e.g., whether the outline */ +/* should be scaled, whether to load bitmaps or not, */ +/* whether to hint the outline, etc). */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Load_Glyph( TT_Size size, + TT_GlyphSlot glyph, + FT_UShort glyph_index, + FT_UInt load_flags ) { + SFNT_Interface* sfnt; + TT_Face face; + FT_Stream stream; + FT_Memory memory; + FT_Error error; + TT_Loader loader; + + + face = (TT_Face)glyph->face; + sfnt = (SFNT_Interface*)face->sfnt; + stream = face->root.stream; + memory = face->root.memory; + error = 0; + + if ( !size || ( load_flags & FT_LOAD_NO_SCALE ) || + ( load_flags & FT_LOAD_NO_RECURSE ) ) { + size = NULL; + load_flags |= FT_LOAD_NO_SCALE | + FT_LOAD_NO_HINTING | + FT_LOAD_NO_BITMAP; + } + + glyph->num_subglyphs = 0; + +#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS + + /* try to load embedded bitmap if any */ + if ( size && + ( load_flags & FT_LOAD_NO_BITMAP ) == 0 && + sfnt->load_sbits ) { + TT_SBit_Metrics metrics; + + + error = sfnt->load_sbit_image( face, + size->root.metrics.x_ppem, + size->root.metrics.y_ppem, + glyph_index, + load_flags, + stream, + &glyph->bitmap, + &metrics ); + if ( !error ) { + glyph->outline.n_points = 0; + glyph->outline.n_contours = 0; + + glyph->metrics.width = (FT_Pos)metrics.width << 6; + glyph->metrics.height = (FT_Pos)metrics.height << 6; + + glyph->metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6; + glyph->metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6; + glyph->metrics.horiAdvance = (FT_Pos)metrics.horiAdvance << 6; + + glyph->metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6; + glyph->metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6; + glyph->metrics.vertAdvance = (FT_Pos)metrics.vertAdvance << 6; + + glyph->format = ft_glyph_format_bitmap; + if ( load_flags & FT_LOAD_VERTICAL_LAYOUT ) { + glyph->bitmap_left = metrics.vertBearingX; + glyph->bitmap_top = metrics.vertBearingY; + } else + { + glyph->bitmap_left = metrics.horiBearingX; + glyph->bitmap_top = metrics.horiBearingY; + } + return error; + } + } + +#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ + + /* seek to the beginning of the glyph table. For Type 42 fonts */ + /* the table might be accessed from a Postscript stream or something */ + /* else... */ + + error = face->goto_table( face, TTAG_glyf, stream, 0 ); + if ( error ) { + FT_ERROR( ( "TT_Load_Glyph: could not access glyph table\n" ) ); + goto Exit; + } + + MEM_Set( &loader, 0, sizeof( loader ) ); + + /* update the glyph zone bounds */ + { + FT_GlyphLoader* gloader = FT_FACE_DRIVER( face )->glyph_loader; + + + loader.gloader = gloader; + + FT_GlyphLoader_Rewind( gloader ); + + tt_prepare_zone( &loader.zone, &gloader->base, 0, 0 ); + tt_prepare_zone( &loader.base, &gloader->base, 0, 0 ); + } + +#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER + + if ( size ) { + /* query new execution context */ + loader.exec = size->debug ? size->context : TT_New_Context( face ); + if ( !loader.exec ) { + return TT_Err_Could_Not_Find_Context; + } + + TT_Load_Context( loader.exec, face, size ); + loader.instructions = loader.exec->glyphIns; + + /* load default graphics state - if needed */ + if ( size->GS.instruct_control & 2 ) { + loader.exec->GS = tt_default_graphics_state; + } + } + +#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */ + + /* clear all outline flags, except the `owner' one */ + glyph->outline.flags = 0; + + if ( size && size->root.metrics.y_ppem < 24 ) { + glyph->outline.flags |= ft_outline_high_precision; + } + + /* let's initialize the rest of our loader now */ + + loader.load_flags = load_flags; + + loader.face = (FT_Face)face; + loader.size = (FT_Size)size; + loader.glyph = (FT_GlyphSlot)glyph; + loader.stream = stream; + + loader.glyf_offset = FILE_Pos(); + +#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER + + /* if the cvt program has disabled hinting, the argument */ + /* is ignored. */ + if ( size && ( size->GS.instruct_control & 1 ) ) { + loader.load_flags |= FT_LOAD_NO_HINTING; + } + +#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */ + + /* Main loading loop */ + glyph->format = ft_glyph_format_outline; + glyph->num_subglyphs = 0; + error = load_truetype_glyph( &loader, glyph_index ); + if ( !error ) { + compute_glyph_metrics( &loader, glyph_index ); + } + +#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER + + if ( !size || !size->debug ) { + TT_Done_Context( loader.exec ); + } + +#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */ + +Exit: + return error; +} + + +/* END */ diff --git a/src/ft2/ttgload.h b/src/ft2/ttgload.h new file mode 100644 index 0000000..361675d --- /dev/null +++ b/src/ft2/ttgload.h @@ -0,0 +1,57 @@ +/***************************************************************************/ +/* */ +/* ttgload.h */ +/* */ +/* TrueType Glyph Loader (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef TTGLOAD_H +#define TTGLOAD_H + + +#include "ttobjs.h" + +#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER +#include "ttinterp.h" +#endif + + +#ifdef __cplusplus +extern "C" { +#endif + + +LOCAL_DEF +void TT_Get_Metrics( TT_HoriHeader* header, + FT_UInt index, + FT_Short* bearing, + FT_UShort* advance ); + +LOCAL_DEF +void TT_Init_Glyph_Loading( TT_Face face ); + +LOCAL_DEF +FT_Error TT_Load_Glyph( TT_Size size, + TT_GlyphSlot glyph, + FT_UShort glyph_index, + FT_UInt load_flags ); + +#ifdef __cplusplus +} +#endif + +#endif /* TTGLOAD_H */ + + +/* END */ diff --git a/src/ft2/ttinterp.c b/src/ft2/ttinterp.c new file mode 100644 index 0000000..cf26388 --- /dev/null +++ b/src/ft2/ttinterp.c @@ -0,0 +1,7349 @@ +/***************************************************************************/ +/* */ +/* ttinterp.c */ +/* */ +/* TrueType bytecode interpreter (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#include "ftdebug.h" +#include "ftcalc.h" +#include "ftsystem.h" + + +#include "ttinterp.h" + +#include "tterrors.h" + + +#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER + + +#define TT_MULFIX FT_MulFix +#define TT_MULDIV FT_MulDiv + +#define TT_INT64 FT_Int64 + + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_ttinterp + +#undef NO_APPLE_PATENT +#define APPLE_THRESHOLD 0x4000000L + +/*************************************************************************/ +/* */ +/* In order to detect infinite loops in the code, we set up a counter */ +/* within the run loop. A single stroke of interpretation is now */ +/* limitet to a maximal number of opcodes defined below. */ +/* */ +#define MAX_RUNNABLE_OPCODES 1000000L + + +/*************************************************************************/ +/* */ +/* There are two kinds of implementations: */ +/* */ +/* a. static implementation */ +/* */ +/* The current execution context is a static variable, which fields */ +/* are accessed directly by the interpreter during execution. The */ +/* context is named `cur'. */ +/* */ +/* This version is non-reentrant, of course. */ +/* */ +/* b. indirect implementation */ +/* */ +/* The current execution context is passed to _each_ function as its */ +/* first argument, and each field is thus accessed indirectly. */ +/* */ +/* This version is fully re-entrant. */ +/* */ +/* The idea is that an indirect implementation may be slower to execute */ +/* on low-end processors that are used in some systems (like 386s or */ +/* even 486s). */ +/* */ +/* As a consequence, the indirect implementation is now the default, as */ +/* its performance costs can be considered negligible in our context. */ +/* Note, however, that we kept the same source with macros because: */ +/* */ +/* - The code is kept very close in design to the Pascal code used for */ +/* development. */ +/* */ +/* - It's much more readable that way! */ +/* */ +/* - It's still open to experimentation and tuning. */ +/* */ +/*************************************************************************/ + + +#ifndef TT_CONFIG_OPTION_STATIC_INTERPRETER /* indirect implementation */ + +#define CUR ( *exc ) /* see ttobjs.h */ + +#else /* static implementation */ + +#define CUR cur + +static +TT_ExecContextRec cur; /* static exec. context variable */ + +/* apparently, we have a _lot_ of direct indexing when accessing */ +/* the static `cur', which makes the code bigger (due to all the */ +/* four bytes addresses). */ + +#endif /* TT_CONFIG_OPTION_STATIC_INTERPRETER */ + + +/*************************************************************************/ +/* */ +/* The instruction argument stack. */ +/* */ +#define INS_ARG EXEC_OP_ FT_Long * args /* see ttobjs.h for EXEC_OP_ */ + + +/*************************************************************************/ +/* */ +/* This macro is used whenever `exec' is unused in a function, to avoid */ +/* stupid warnings from pedantic compilers. */ +/* */ +#define FT_UNUSED_EXEC FT_UNUSED( CUR ) + + +/*************************************************************************/ +/* */ +/* This macro is used whenever `args' is unused in a function, to avoid */ +/* stupid warnings from pedantic compilers. */ +/* */ +#define FT_UNUSED_ARG FT_UNUSED_EXEC; FT_UNUSED( args ) + + +/*************************************************************************/ +/* */ +/* The following macros hide the use of EXEC_ARG and EXEC_ARG_ to */ +/* increase readabilty of the code. */ +/* */ +/*************************************************************************/ + + +#define SKIP_Code() \ + SkipCode( EXEC_ARG ) + +#define GET_ShortIns() \ + GetShortIns( EXEC_ARG ) + +#define NORMalize( x, y, v ) \ + Normalize( EXEC_ARG_ x, y, v ) + +#define SET_SuperRound( scale, flags ) \ + SetSuperRound( EXEC_ARG_ scale, flags ) + +#define ROUND_None( d, c ) \ + Round_None( EXEC_ARG_ d, c ) + +#define INS_Goto_CodeRange( range, ip ) \ + Ins_Goto_CodeRange( EXEC_ARG_ range, ip ) + +#define CUR_Func_project( x, y ) \ + CUR.func_project( EXEC_ARG_ x, y ) + +#define CUR_Func_move( z, p, d ) \ + CUR.func_move( EXEC_ARG_ z, p, d ) + +#define CUR_Func_dualproj( x, y ) \ + CUR.func_dualproj( EXEC_ARG_ x, y ) + +#define CUR_Func_freeProj( x, y ) \ + CUR.func_freeProj( EXEC_ARG_ x, y ) + +#define CUR_Func_round( d, c ) \ + CUR.func_round( EXEC_ARG_ d, c ) + +#define CUR_Func_read_cvt( index ) \ + CUR.func_read_cvt( EXEC_ARG_ index ) + +#define CUR_Func_write_cvt( index, val ) \ + CUR.func_write_cvt( EXEC_ARG_ index, val ) + +#define CUR_Func_move_cvt( index, val ) \ + CUR.func_move_cvt( EXEC_ARG_ index, val ) + +#define CURRENT_Ratio() \ + Current_Ratio( EXEC_ARG ) + +#define CURRENT_Ppem() \ + Current_Ppem( EXEC_ARG ) + +#define CUR_Ppem() \ + Cur_PPEM( EXEC_ARG ) + +#define CALC_Length() \ + Calc_Length( EXEC_ARG ) + +#define INS_SxVTL( a, b, c, d ) \ + Ins_SxVTL( EXEC_ARG_ a, b, c, d ) + +#define COMPUTE_Funcs() \ + Compute_Funcs( EXEC_ARG ) + +#define COMPUTE_Round( a ) \ + Compute_Round( EXEC_ARG_ a ) + +#define COMPUTE_Point_Displacement( a, b, c, d ) \ + Compute_Point_Displacement( EXEC_ARG_ a, b, c, d ) + +#define MOVE_Zp2_Point( a, b, c, t ) \ + Move_Zp2_Point( EXEC_ARG_ a, b, c, t ) + + +/*************************************************************************/ +/* */ +/* Instruction dispatch function, as used by the interpreter. */ +/* */ +typedef void ( *TInstruction_Function )( INS_ARG ); + + +/*************************************************************************/ +/* */ +/* A simple bounds-checking macro. */ +/* */ +#define BOUNDS( x, n ) ( (FT_UInt)( x ) >= (FT_UInt)( n ) ) + + +#undef SUCCESS +#define SUCCESS 0 + +#undef FAILURE +#define FAILURE 1 + + +/*************************************************************************/ +/* */ +/* CODERANGE FUNCTIONS */ +/* */ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Goto_CodeRange */ +/* */ +/* */ +/* Switches to a new code range (updates the code related elements in */ +/* `exec', and `IP'). */ +/* */ +/* */ +/* range :: The new execution code range. */ +/* */ +/* IP :: The new IP in the new code range. */ +/* */ +/* */ +/* exec :: The target execution context. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Goto_CodeRange( TT_ExecContext exec, + FT_Int range, + FT_Long IP ) { + TT_CodeRange* coderange; + + + FT_Assert( range >= 1 && range <= 3 ); + + coderange = &exec->codeRangeTable[range - 1]; + + FT_Assert( coderange->base != NULL ); + + /* NOTE: Because the last instruction of a program may be a CALL */ + /* which will return to the first byte *after* the code */ + /* range, we test for IP <= Size instead of IP < Size. */ + /* */ + FT_Assert( (FT_ULong)IP <= coderange->size ); + + exec->code = coderange->base; + exec->codeSize = coderange->size; + exec->IP = IP; + exec->curRange = range; + + return TT_Err_Ok; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Set_CodeRange */ +/* */ +/* */ +/* Sets a code range. */ +/* */ +/* */ +/* range :: The code range index. */ +/* */ +/* base :: The new code base. */ +/* */ +/* length :: The range size in bytes. */ +/* */ +/* */ +/* exec :: The target execution context. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Set_CodeRange( TT_ExecContext exec, + FT_Int range, + void* base, + FT_Long length ) { + FT_Assert( range >= 1 && range <= 3 ); + + exec->codeRangeTable[range - 1].base = (FT_Byte*)base; + exec->codeRangeTable[range - 1].size = length; + + return TT_Err_Ok; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Clear_CodeRange */ +/* */ +/* */ +/* Clears a code range. */ +/* */ +/* */ +/* range :: The code range index. */ +/* */ +/* */ +/* exec :: The target execution context. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* Does not set the Error variable. */ +/* */ +LOCAL_FUNC +FT_Error TT_Clear_CodeRange( TT_ExecContext exec, + FT_Int range ) { + FT_Assert( range >= 1 && range <= 3 ); + + exec->codeRangeTable[range - 1].base = NULL; + exec->codeRangeTable[range - 1].size = 0; + + return TT_Err_Ok; +} + + +/*************************************************************************/ +/* */ +/* EXECUTION CONTEXT ROUTINES */ +/* */ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Destroy_Context */ +/* */ +/* */ +/* Destroys a given context. */ +/* */ +/* */ +/* exec :: A handle to the target execution context. */ +/* */ +/* memory :: A handle to the parent memory object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* Only the glyph loader and debugger should call this function. */ +/* */ +LOCAL_FUNC +FT_Error TT_Destroy_Context( TT_ExecContext exec, + FT_Memory memory ) { + /* free composite load stack */ + FREE( exec->loadStack ); + exec->loadSize = 0; + + /* points zone */ + exec->maxPoints = 0; + exec->maxContours = 0; + + /* free stack */ + FREE( exec->stack ); + exec->stackSize = 0; + + /* free call stack */ + FREE( exec->callStack ); + exec->callSize = 0; + exec->callTop = 0; + + /* free glyph code range */ + FREE( exec->glyphIns ); + exec->glyphSize = 0; + + exec->size = NULL; + exec->face = NULL; + + FREE( exec ); + return TT_Err_Ok; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Init_Context */ +/* */ +/* */ +/* Initializes a context object. */ +/* */ +/* */ +/* memory :: A handle to the parent memory object. */ +/* */ +/* face :: A handle to the source TrueType face object. */ +/* */ +/* */ +/* exec :: A handle to the target execution context. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +static +FT_Error Init_Context( TT_ExecContext exec, + TT_Face face, + FT_Memory memory ) { + FT_Error error; + + + FT_TRACE1( ( "Init_Context: new object at 0x%08p, parent = 0x%08p\n", + exec, face ) ); + + exec->memory = memory; + exec->callSize = 32; + + if ( ALLOC_ARRAY( exec->callStack, exec->callSize, TT_CallRec ) ) { + goto Fail_Memory; + } + + /* all values in the context are set to 0 already, but this is */ + /* here as a remainder */ + exec->maxPoints = 0; + exec->maxContours = 0; + + exec->stackSize = 0; + exec->loadSize = 0; + exec->glyphSize = 0; + + exec->stack = NULL; + exec->loadStack = NULL; + exec->glyphIns = NULL; + + exec->face = face; + exec->size = NULL; + + return TT_Err_Ok; + +Fail_Memory: + FT_ERROR( ( "Init_Context: not enough memory for 0x%08lx\n", + (FT_Long)exec ) ); + TT_Destroy_Context( exec, memory ); + + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Update_Max */ +/* */ +/* */ +/* Checks the size of a buffer and reallocates it if necessary. */ +/* */ +/* */ +/* memory :: A handle to the parent memory object. */ +/* */ +/* multiplier :: The size in bytes of each element in the buffer. */ +/* */ +/* new_max :: The new capacity (size) of the buffer. */ +/* */ +/* */ +/* size :: The address of the buffer's current size expressed */ +/* in elements. */ +/* */ +/* buff :: The address of the buffer base pointer. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +static +FT_Error Update_Max( FT_Memory memory, + FT_ULong* size, + FT_Long multiplier, + void** buff, + FT_ULong new_max ) { + FT_Error error; + + + if ( *size < new_max ) { + FREE( *buff ); + if ( ALLOC( *buff, new_max * multiplier ) ) { + return error; + } + *size = new_max; + } + + return TT_Err_Ok; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_Context */ +/* */ +/* */ +/* Prepare an execution context for glyph hinting. */ +/* */ +/* */ +/* face :: A handle to the source face object. */ +/* */ +/* size :: A handle to the source size object. */ +/* */ +/* */ +/* exec :: A handle to the target execution context. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* Only the glyph loader and debugger should call this function. */ +/* */ +LOCAL_FUNC +FT_Error TT_Load_Context( TT_ExecContext exec, + TT_Face face, + TT_Size size ) { + FT_Int i; + FT_ULong tmp; + TT_MaxProfile* maxp; + FT_Error error; + + + exec->face = face; + maxp = &face->max_profile; + exec->size = size; + + if ( size ) { + exec->numFDefs = size->num_function_defs; + exec->maxFDefs = size->max_function_defs; + exec->numIDefs = size->num_instruction_defs; + exec->maxIDefs = size->max_instruction_defs; + exec->FDefs = size->function_defs; + exec->IDefs = size->instruction_defs; + exec->tt_metrics = size->ttmetrics; + exec->metrics = size->root.metrics; + + exec->maxFunc = size->max_func; + exec->maxIns = size->max_ins; + + for ( i = 0; i < TT_MAX_CODE_RANGES; i++ ) + exec->codeRangeTable[i] = size->codeRangeTable[i]; + + /* set graphics state */ + exec->GS = size->GS; + + exec->cvtSize = size->cvt_size; + exec->cvt = size->cvt; + + exec->storeSize = size->storage_size; + exec->storage = size->storage; + + exec->twilight = size->twilight; + } + + error = Update_Max( exec->memory, + &exec->loadSize, + sizeof( TT_SubGlyphRec ), + (void**)&exec->loadStack, + exec->face->max_components + 1 ); + if ( error ) { + return error; + } + + /* XXX: We reserve a little more elements on the stack to deal safely */ + /* with broken fonts like arialbs, courbs, timesbs, etc. */ + tmp = exec->stackSize; + error = Update_Max( exec->memory, + &tmp, + sizeof( FT_F26Dot6 ), + (void**)&exec->stack, + maxp->maxStackElements + 32 ); + exec->stackSize = (FT_UInt)tmp; + if ( error ) { + return error; + } + + tmp = exec->glyphSize; + error = Update_Max( exec->memory, + &tmp, + sizeof( FT_Byte ), + (void**)&exec->glyphIns, + maxp->maxSizeOfInstructions ); + exec->glyphSize = (FT_UShort)tmp; + if ( error ) { + return error; + } + + exec->pts.n_points = 0; + exec->pts.n_contours = 0; + + exec->instruction_trap = FALSE; + + return TT_Err_Ok; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Save_Context */ +/* */ +/* */ +/* Saves the code ranges in a `size' object. */ +/* */ +/* */ +/* exec :: A handle to the source execution context. */ +/* */ +/* */ +/* size :: A handle to the target size object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* Only the glyph loader and debugger should call this function. */ +/* */ +LOCAL_FUNC +FT_Error TT_Save_Context( TT_ExecContext exec, + TT_Size size ) { + FT_Int i; + + + /* XXXX: Will probably disappear soon with all the code range */ + /* management, which is now rather obsolete. */ + /* */ + size->num_function_defs = exec->numFDefs; + size->num_instruction_defs = exec->numIDefs; + + size->max_func = exec->maxFunc; + size->max_ins = exec->maxIns; + + for ( i = 0; i < TT_MAX_CODE_RANGES; i++ ) + size->codeRangeTable[i] = exec->codeRangeTable[i]; + + return TT_Err_Ok; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Run_Context */ +/* */ +/* */ +/* Executes one or more instructions in the execution context. */ +/* */ +/* */ +/* debug :: A Boolean flag. If set, the function sets some internal */ +/* variables and returns immediately, otherwise TT_RunIns() */ +/* is called. */ +/* */ +/* This is commented out currently. */ +/* */ +/* */ +/* exec :: A handle to the target execution context. */ +/* */ +/* */ +/* TrueTyoe error code. 0 means success. */ +/* */ +/* */ +/* Only the glyph loader and debugger should call this function. */ +/* */ +LOCAL_FUNC +FT_Error TT_Run_Context( TT_ExecContext exec, + FT_Bool debug ) { + FT_Error error; + + + if ( ( error = TT_Goto_CodeRange( exec, tt_coderange_glyph, 0 ) ) + != TT_Err_Ok ) { + return error; + } + + exec->zp0 = exec->pts; + exec->zp1 = exec->pts; + exec->zp2 = exec->pts; + + exec->GS.gep0 = 1; + exec->GS.gep1 = 1; + exec->GS.gep2 = 1; + + exec->GS.projVector.x = 0x4000; + exec->GS.projVector.y = 0x0000; + + exec->GS.freeVector = exec->GS.projVector; + exec->GS.dualVector = exec->GS.projVector; + + exec->GS.round_state = 1; + exec->GS.loop = 1; + + /* some glyphs leave something on the stack. so we clean it */ + /* before a new execution. */ + exec->top = 0; + exec->callTop = 0; + +#if 1 + FT_UNUSED( debug ); + + return exec->face->interpreter( exec ); +#else + if ( !debug ) { + return TT_RunIns( exec ); + } else { + return TT_Err_Ok; + } +#endif +} + + +const TT_GraphicsState tt_default_graphics_state = +{ + 0, 0, 0, + { 0x4000, 0 }, + { 0x4000, 0 }, + { 0x4000, 0 }, + 1, 64, 1, + TRUE, 68, 0, 0, 9, 3, + 0, FALSE, 2, 1, 1, 1 +}; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_New_Context */ +/* */ +/* */ +/* Queries the face context for a given font. Note that there is */ +/* now a _single_ execution context in the TrueType driver which is */ +/* shared among faces. */ +/* */ +/* */ +/* face :: A handle to the source face object. */ +/* */ +/* */ +/* A handle to the execution context. Initialized for `face'. */ +/* */ +/* */ +/* Only the glyph loader and debugger should call this function. */ +/* */ +FT_EXPORT_FUNC( TT_ExecContext ) TT_New_Context( TT_Face face ) +{ + TT_Driver driver; + TT_ExecContext exec; + FT_Memory memory; + + + if ( !face ) { + return 0; + } + + driver = (TT_Driver)face->root.driver; + + memory = driver->root.root.memory; + exec = driver->context; + + if ( !driver->context ) { + FT_Error error; + + + /* allocate object */ + if ( ALLOC( exec, sizeof( *exec ) ) ) { + goto Exit; + } + + /* initialize it */ + error = Init_Context( exec, face, memory ); + if ( error ) { + goto Fail; + } + + /* store it into the driver */ + driver->context = exec; + } + +Exit: + return driver->context; + +Fail: + FREE( exec ); + + return 0; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Done_Context */ +/* */ +/* */ +/* Discards an execution context. */ +/* */ +/* */ +/* exec :: A handle to the target execution context. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* Only the glyph loader and debugger should call this function. */ +/* */ +LOCAL_FUNC +FT_Error TT_Done_Context( TT_ExecContext exec ) { + /* Nothing at all for now */ + FT_UNUSED( exec ); + + return TT_Err_Ok; +} + + +#ifdef FT_CONFIG_OPTION_OLD_CALCS + +static FT_F26Dot6 Norm( FT_F26Dot6 X, + FT_F26Dot6 Y ) { + TT_INT64 T1, T2; + + + MUL_64( X, X, T1 ); + MUL_64( Y, Y, T2 ); + + ADD_64( T1, T2, T1 ); + + return (FT_F26Dot6)SQRT_64( T1 ); +} + +#endif /* FT_CONFIG_OPTION_OLD_CALCS */ + + +/*************************************************************************/ +/* */ +/* Before an opcode is executed, the interpreter verifies that there are */ +/* enough arguments on the stack, with the help of the Pop_Push_Count */ +/* table. */ +/* */ +/* For each opcode, the first column gives the number of arguments that */ +/* are popped from the stack; the second one gives the number of those */ +/* that are pushed in result. */ +/* */ +/* Note that for opcodes with a varying number of parameters, either 0 */ +/* or 1 arg is verified before execution, depending on the nature of the */ +/* instruction: */ +/* */ +/* - if the number of arguments is given by the bytecode stream or the */ +/* loop variable, 0 is chosen. */ +/* */ +/* - if the first argument is a count n that is followed by arguments */ +/* a1 .. an, then 1 is chosen. */ +/* */ +/*************************************************************************/ + + +#undef PACK +#define PACK( x, y ) ( ( x << 4 ) | y ) + + +static +const FT_Byte Pop_Push_Count[256] = +{ + /* opcodes are gathered in groups of 16 */ + /* please keep the spaces as they are */ + + /* SVTCA y */ PACK( 0, 0 ), + /* SVTCA x */ PACK( 0, 0 ), + /* SPvTCA y */ PACK( 0, 0 ), + /* SPvTCA x */ PACK( 0, 0 ), + /* SFvTCA y */ PACK( 0, 0 ), + /* SFvTCA x */ PACK( 0, 0 ), + /* SPvTL // */ PACK( 2, 0 ), + /* SPvTL + */ PACK( 2, 0 ), + /* SFvTL // */ PACK( 2, 0 ), + /* SFvTL + */ PACK( 2, 0 ), + /* SPvFS */ PACK( 2, 0 ), + /* SFvFS */ PACK( 2, 0 ), + /* GPV */ PACK( 0, 2 ), + /* GFV */ PACK( 0, 2 ), + /* SFvTPv */ PACK( 0, 0 ), + /* ISECT */ PACK( 5, 0 ), + + /* SRP0 */ PACK( 1, 0 ), + /* SRP1 */ PACK( 1, 0 ), + /* SRP2 */ PACK( 1, 0 ), + /* SZP0 */ PACK( 1, 0 ), + /* SZP1 */ PACK( 1, 0 ), + /* SZP2 */ PACK( 1, 0 ), + /* SZPS */ PACK( 1, 0 ), + /* SLOOP */ PACK( 1, 0 ), + /* RTG */ PACK( 0, 0 ), + /* RTHG */ PACK( 0, 0 ), + /* SMD */ PACK( 1, 0 ), + /* ELSE */ PACK( 0, 0 ), + /* JMPR */ PACK( 1, 0 ), + /* SCvTCi */ PACK( 1, 0 ), + /* SSwCi */ PACK( 1, 0 ), + /* SSW */ PACK( 1, 0 ), + + /* DUP */ PACK( 1, 2 ), + /* POP */ PACK( 1, 0 ), + /* CLEAR */ PACK( 0, 0 ), + /* SWAP */ PACK( 2, 2 ), + /* DEPTH */ PACK( 0, 1 ), + /* CINDEX */ PACK( 1, 1 ), + /* MINDEX */ PACK( 1, 0 ), + /* AlignPTS */ PACK( 2, 0 ), + /* INS_$28 */ PACK( 0, 0 ), + /* UTP */ PACK( 1, 0 ), + /* LOOPCALL */ PACK( 2, 0 ), + /* CALL */ PACK( 1, 0 ), + /* FDEF */ PACK( 1, 0 ), + /* ENDF */ PACK( 0, 0 ), + /* MDAP[0] */ PACK( 1, 0 ), + /* MDAP[1] */ PACK( 1, 0 ), + + /* IUP[0] */ PACK( 0, 0 ), + /* IUP[1] */ PACK( 0, 0 ), + /* SHP[0] */ PACK( 0, 0 ), + /* SHP[1] */ PACK( 0, 0 ), + /* SHC[0] */ PACK( 1, 0 ), + /* SHC[1] */ PACK( 1, 0 ), + /* SHZ[0] */ PACK( 1, 0 ), + /* SHZ[1] */ PACK( 1, 0 ), + /* SHPIX */ PACK( 1, 0 ), + /* IP */ PACK( 0, 0 ), + /* MSIRP[0] */ PACK( 2, 0 ), + /* MSIRP[1] */ PACK( 2, 0 ), + /* AlignRP */ PACK( 0, 0 ), + /* RTDG */ PACK( 0, 0 ), + /* MIAP[0] */ PACK( 2, 0 ), + /* MIAP[1] */ PACK( 2, 0 ), + + /* NPushB */ PACK( 0, 0 ), + /* NPushW */ PACK( 0, 0 ), + /* WS */ PACK( 2, 0 ), + /* RS */ PACK( 1, 1 ), + /* WCvtP */ PACK( 2, 0 ), + /* RCvt */ PACK( 1, 1 ), + /* GC[0] */ PACK( 1, 1 ), + /* GC[1] */ PACK( 1, 1 ), + /* SCFS */ PACK( 2, 0 ), + /* MD[0] */ PACK( 2, 1 ), + /* MD[1] */ PACK( 2, 1 ), + /* MPPEM */ PACK( 0, 1 ), + /* MPS */ PACK( 0, 1 ), + /* FlipON */ PACK( 0, 0 ), + /* FlipOFF */ PACK( 0, 0 ), + /* DEBUG */ PACK( 1, 0 ), + + /* LT */ PACK( 2, 1 ), + /* LTEQ */ PACK( 2, 1 ), + /* GT */ PACK( 2, 1 ), + /* GTEQ */ PACK( 2, 1 ), + /* EQ */ PACK( 2, 1 ), + /* NEQ */ PACK( 2, 1 ), + /* ODD */ PACK( 1, 1 ), + /* EVEN */ PACK( 1, 1 ), + /* IF */ PACK( 1, 0 ), + /* EIF */ PACK( 0, 0 ), + /* AND */ PACK( 2, 1 ), + /* OR */ PACK( 2, 1 ), + /* NOT */ PACK( 1, 1 ), + /* DeltaP1 */ PACK( 1, 0 ), + /* SDB */ PACK( 1, 0 ), + /* SDS */ PACK( 1, 0 ), + + /* ADD */ PACK( 2, 1 ), + /* SUB */ PACK( 2, 1 ), + /* DIV */ PACK( 2, 1 ), + /* MUL */ PACK( 2, 1 ), + /* ABS */ PACK( 1, 1 ), + /* NEG */ PACK( 1, 1 ), + /* FLOOR */ PACK( 1, 1 ), + /* CEILING */ PACK( 1, 1 ), + /* ROUND[0] */ PACK( 1, 1 ), + /* ROUND[1] */ PACK( 1, 1 ), + /* ROUND[2] */ PACK( 1, 1 ), + /* ROUND[3] */ PACK( 1, 1 ), + /* NROUND[0] */ PACK( 1, 1 ), + /* NROUND[1] */ PACK( 1, 1 ), + /* NROUND[2] */ PACK( 1, 1 ), + /* NROUND[3] */ PACK( 1, 1 ), + + /* WCvtF */ PACK( 2, 0 ), + /* DeltaP2 */ PACK( 1, 0 ), + /* DeltaP3 */ PACK( 1, 0 ), + /* DeltaCn[0] */ PACK( 1, 0 ), + /* DeltaCn[1] */ PACK( 1, 0 ), + /* DeltaCn[2] */ PACK( 1, 0 ), + /* SROUND */ PACK( 1, 0 ), + /* S45Round */ PACK( 1, 0 ), + /* JROT */ PACK( 2, 0 ), + /* JROF */ PACK( 2, 0 ), + /* ROFF */ PACK( 0, 0 ), + /* INS_$7B */ PACK( 0, 0 ), + /* RUTG */ PACK( 0, 0 ), + /* RDTG */ PACK( 0, 0 ), + /* SANGW */ PACK( 1, 0 ), + /* AA */ PACK( 1, 0 ), + + /* FlipPT */ PACK( 0, 0 ), + /* FlipRgON */ PACK( 2, 0 ), + /* FlipRgOFF */ PACK( 2, 0 ), + /* INS_$83 */ PACK( 0, 0 ), + /* INS_$84 */ PACK( 0, 0 ), + /* ScanCTRL */ PACK( 1, 0 ), + /* SDVPTL[0] */ PACK( 2, 0 ), + /* SDVPTL[1] */ PACK( 2, 0 ), + /* GetINFO */ PACK( 1, 1 ), + /* IDEF */ PACK( 1, 0 ), + /* ROLL */ PACK( 3, 3 ), + /* MAX */ PACK( 2, 1 ), + /* MIN */ PACK( 2, 1 ), + /* ScanTYPE */ PACK( 1, 0 ), + /* InstCTRL */ PACK( 2, 0 ), + /* INS_$8F */ PACK( 0, 0 ), + + /* INS_$90 */ PACK( 0, 0 ), + /* INS_$91 */ PACK( 0, 0 ), + /* INS_$92 */ PACK( 0, 0 ), + /* INS_$93 */ PACK( 0, 0 ), + /* INS_$94 */ PACK( 0, 0 ), + /* INS_$95 */ PACK( 0, 0 ), + /* INS_$96 */ PACK( 0, 0 ), + /* INS_$97 */ PACK( 0, 0 ), + /* INS_$98 */ PACK( 0, 0 ), + /* INS_$99 */ PACK( 0, 0 ), + /* INS_$9A */ PACK( 0, 0 ), + /* INS_$9B */ PACK( 0, 0 ), + /* INS_$9C */ PACK( 0, 0 ), + /* INS_$9D */ PACK( 0, 0 ), + /* INS_$9E */ PACK( 0, 0 ), + /* INS_$9F */ PACK( 0, 0 ), + + /* INS_$A0 */ PACK( 0, 0 ), + /* INS_$A1 */ PACK( 0, 0 ), + /* INS_$A2 */ PACK( 0, 0 ), + /* INS_$A3 */ PACK( 0, 0 ), + /* INS_$A4 */ PACK( 0, 0 ), + /* INS_$A5 */ PACK( 0, 0 ), + /* INS_$A6 */ PACK( 0, 0 ), + /* INS_$A7 */ PACK( 0, 0 ), + /* INS_$A8 */ PACK( 0, 0 ), + /* INS_$A9 */ PACK( 0, 0 ), + /* INS_$AA */ PACK( 0, 0 ), + /* INS_$AB */ PACK( 0, 0 ), + /* INS_$AC */ PACK( 0, 0 ), + /* INS_$AD */ PACK( 0, 0 ), + /* INS_$AE */ PACK( 0, 0 ), + /* INS_$AF */ PACK( 0, 0 ), + + /* PushB[0] */ PACK( 0, 1 ), + /* PushB[1] */ PACK( 0, 2 ), + /* PushB[2] */ PACK( 0, 3 ), + /* PushB[3] */ PACK( 0, 4 ), + /* PushB[4] */ PACK( 0, 5 ), + /* PushB[5] */ PACK( 0, 6 ), + /* PushB[6] */ PACK( 0, 7 ), + /* PushB[7] */ PACK( 0, 8 ), + /* PushW[0] */ PACK( 0, 1 ), + /* PushW[1] */ PACK( 0, 2 ), + /* PushW[2] */ PACK( 0, 3 ), + /* PushW[3] */ PACK( 0, 4 ), + /* PushW[4] */ PACK( 0, 5 ), + /* PushW[5] */ PACK( 0, 6 ), + /* PushW[6] */ PACK( 0, 7 ), + /* PushW[7] */ PACK( 0, 8 ), + + /* MDRP[00] */ PACK( 1, 0 ), + /* MDRP[01] */ PACK( 1, 0 ), + /* MDRP[02] */ PACK( 1, 0 ), + /* MDRP[03] */ PACK( 1, 0 ), + /* MDRP[04] */ PACK( 1, 0 ), + /* MDRP[05] */ PACK( 1, 0 ), + /* MDRP[06] */ PACK( 1, 0 ), + /* MDRP[07] */ PACK( 1, 0 ), + /* MDRP[08] */ PACK( 1, 0 ), + /* MDRP[09] */ PACK( 1, 0 ), + /* MDRP[10] */ PACK( 1, 0 ), + /* MDRP[11] */ PACK( 1, 0 ), + /* MDRP[12] */ PACK( 1, 0 ), + /* MDRP[13] */ PACK( 1, 0 ), + /* MDRP[14] */ PACK( 1, 0 ), + /* MDRP[15] */ PACK( 1, 0 ), + + /* MDRP[16] */ PACK( 1, 0 ), + /* MDRP[17] */ PACK( 1, 0 ), + /* MDRP[18] */ PACK( 1, 0 ), + /* MDRP[19] */ PACK( 1, 0 ), + /* MDRP[20] */ PACK( 1, 0 ), + /* MDRP[21] */ PACK( 1, 0 ), + /* MDRP[22] */ PACK( 1, 0 ), + /* MDRP[23] */ PACK( 1, 0 ), + /* MDRP[24] */ PACK( 1, 0 ), + /* MDRP[25] */ PACK( 1, 0 ), + /* MDRP[26] */ PACK( 1, 0 ), + /* MDRP[27] */ PACK( 1, 0 ), + /* MDRP[28] */ PACK( 1, 0 ), + /* MDRP[29] */ PACK( 1, 0 ), + /* MDRP[30] */ PACK( 1, 0 ), + /* MDRP[31] */ PACK( 1, 0 ), + + /* MIRP[00] */ PACK( 2, 0 ), + /* MIRP[01] */ PACK( 2, 0 ), + /* MIRP[02] */ PACK( 2, 0 ), + /* MIRP[03] */ PACK( 2, 0 ), + /* MIRP[04] */ PACK( 2, 0 ), + /* MIRP[05] */ PACK( 2, 0 ), + /* MIRP[06] */ PACK( 2, 0 ), + /* MIRP[07] */ PACK( 2, 0 ), + /* MIRP[08] */ PACK( 2, 0 ), + /* MIRP[09] */ PACK( 2, 0 ), + /* MIRP[10] */ PACK( 2, 0 ), + /* MIRP[11] */ PACK( 2, 0 ), + /* MIRP[12] */ PACK( 2, 0 ), + /* MIRP[13] */ PACK( 2, 0 ), + /* MIRP[14] */ PACK( 2, 0 ), + /* MIRP[15] */ PACK( 2, 0 ), + + /* MIRP[16] */ PACK( 2, 0 ), + /* MIRP[17] */ PACK( 2, 0 ), + /* MIRP[18] */ PACK( 2, 0 ), + /* MIRP[19] */ PACK( 2, 0 ), + /* MIRP[20] */ PACK( 2, 0 ), + /* MIRP[21] */ PACK( 2, 0 ), + /* MIRP[22] */ PACK( 2, 0 ), + /* MIRP[23] */ PACK( 2, 0 ), + /* MIRP[24] */ PACK( 2, 0 ), + /* MIRP[25] */ PACK( 2, 0 ), + /* MIRP[26] */ PACK( 2, 0 ), + /* MIRP[27] */ PACK( 2, 0 ), + /* MIRP[28] */ PACK( 2, 0 ), + /* MIRP[29] */ PACK( 2, 0 ), + /* MIRP[30] */ PACK( 2, 0 ), + /* MIRP[31] */ PACK( 2, 0 ) +}; + + +static +const FT_Char opcode_length[256] = +{ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + -1,-1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 2, 3, 4, 5, 6, 7, 8, 9, 3, 5, 7, 9, 11,13,15,17, + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 +}; + +static +const FT_Vector Null_Vector = {0,0}; + + +#undef PACK + + +#undef NULL_Vector +#define NULL_Vector (FT_Vector*)&Null_Vector + + +/*************************************************************************/ +/* */ +/* */ +/* Current_Ratio */ +/* */ +/* */ +/* Returns the current aspect ratio scaling factor depending on the */ +/* projection vector's state and device resolutions. */ +/* */ +/* */ +/* The aspect ratio in 16.16 format, always <= 1.0 . */ +/* */ +static +FT_Long Current_Ratio( EXEC_OP ) { + if ( CUR.tt_metrics.ratio ) { + return CUR.tt_metrics.ratio; + } + + if ( CUR.GS.projVector.y == 0 ) { + CUR.tt_metrics.ratio = CUR.tt_metrics.x_ratio; + } else if ( CUR.GS.projVector.x == 0 ) { + CUR.tt_metrics.ratio = CUR.tt_metrics.y_ratio; + } else + { + FT_Long x, y; + + +#ifdef FT_CONFIG_OPTION_OLD_CALCS + + x = TT_MULDIV( CUR.GS.projVector.x, CUR.tt_metrics.x_ratio, 0x4000 ); + y = TT_MULDIV( CUR.GS.projVector.y, CUR.tt_metrics.y_ratio, 0x4000 ); + CUR.tt_metrics.ratio = Norm( x, y ); + +#else + + x = TT_MULDIV( CUR.GS.projVector.x, CUR.tt_metrics.x_ratio, 0x8000 ); + y = TT_MULDIV( CUR.GS.projVector.y, CUR.tt_metrics.y_ratio, 0x8000 ); + CUR.tt_metrics.ratio = FT_Sqrt32( x * x + y * y ) << 1; + +#endif /* FT_CONFIG_OPTION_OLD_CALCS */ + + } + + return CUR.tt_metrics.ratio; +} + + +static +FT_Long Current_Ppem( EXEC_OP ) { + return TT_MULFIX( CUR.tt_metrics.ppem, CURRENT_Ratio() ); +} + + +/*************************************************************************/ +/* */ +/* Functions related to the control value table (CVT). */ +/* */ +/*************************************************************************/ + + +static +FT_F26Dot6 Read_CVT( EXEC_OP_ FT_ULong index ) { + return CUR.cvt[index]; +} + + +static +FT_F26Dot6 Read_CVT_Stretched( EXEC_OP_ FT_ULong index ) { + return TT_MULFIX( CUR.cvt[index], CURRENT_Ratio() ); +} + + +static +void Write_CVT( EXEC_OP_ FT_ULong index, + FT_F26Dot6 value ) { + CUR.cvt[index] = value; +} + + +static +void Write_CVT_Stretched( EXEC_OP_ FT_ULong index, + FT_F26Dot6 value ) { + CUR.cvt[index] = FT_DivFix( value, CURRENT_Ratio() ); +} + + +static +void Move_CVT( EXEC_OP_ FT_ULong index, + FT_F26Dot6 value ) { + CUR.cvt[index] += value; +} + + +static +void Move_CVT_Stretched( EXEC_OP_ FT_ULong index, + FT_F26Dot6 value ) { + CUR.cvt[index] += FT_DivFix( value, CURRENT_Ratio() ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* GetShortIns */ +/* */ +/* */ +/* Returns a short integer taken from the instruction stream at */ +/* address IP. */ +/* */ +/* */ +/* Short read at code[IP]. */ +/* */ +/* */ +/* This one could become a macro. */ +/* */ +static FT_Short GetShortIns( EXEC_OP ) { + /* Reading a byte stream so there is no endianess (DaveP) */ + CUR.IP += 2; + return (FT_Short)( ( CUR.code[CUR.IP - 2] << 8 ) + + CUR.code[CUR.IP - 1] ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* Ins_Goto_CodeRange */ +/* */ +/* */ +/* Goes to a certain code range in the instruction stream. */ +/* */ +/* */ +/* aRange :: The index of the code range. */ +/* */ +/* aIP :: The new IP address in the code range. */ +/* */ +/* */ +/* SUCCESS or FAILURE. */ +/* */ +static +FT_Bool Ins_Goto_CodeRange( EXEC_OP_ FT_Int aRange, + FT_ULong aIP ) { + TT_CodeRange* range; + + + if ( aRange < 1 || aRange > 3 ) { + CUR.error = TT_Err_Bad_Argument; + return FAILURE; + } + + range = &CUR.codeRangeTable[aRange - 1]; + + if ( range->base == NULL ) { /* invalid coderange */ + CUR.error = TT_Err_Invalid_CodeRange; + return FAILURE; + } + + /* NOTE: Because the last instruction of a program may be a CALL */ + /* which will return to the first byte *after* the code */ + /* range, we test for AIP <= Size, instead of AIP < Size. */ + + if ( aIP > range->size ) { + CUR.error = TT_Err_Code_Overflow; + return FAILURE; + } + + CUR.code = range->base; + CUR.codeSize = range->size; + CUR.IP = aIP; + CUR.curRange = aRange; + + return SUCCESS; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Direct_Move */ +/* */ +/* */ +/* Moves a point by a given distance along the freedom vector. The */ +/* point will be `touched'. */ +/* */ +/* */ +/* point :: The index of the point to move. */ +/* */ +/* distance :: The distance to apply. */ +/* */ +/* */ +/* zone :: The affected glyph zone. */ +/* */ +static +void Direct_Move( EXEC_OP_ TT_GlyphZone* zone, + FT_UShort point, + FT_F26Dot6 distance ) { + FT_F26Dot6 v; + + + v = CUR.GS.freeVector.x; + + if ( v != 0 ) { + +#ifdef NO_APPLE_PATENT + + if ( ABS( CUR.F_dot_P ) > APPLE_THRESHOLD ) { + zone->cur[point].x += distance; + } + +#else + + zone->cur[point].x += TT_MULDIV( distance, + v * 0x10000L, + CUR.F_dot_P ); + +#endif + + zone->tags[point] |= FT_Curve_Tag_Touch_X; + } + + v = CUR.GS.freeVector.y; + + if ( v != 0 ) { + +#ifdef NO_APPLE_PATENT + + if ( ABS( CUR.F_dot_P ) > APPLE_THRESHOLD ) { + zone->cur[point].y += distance; + } + +#else + + zone->cur[point].y += TT_MULDIV( distance, + v * 0x10000L, + CUR.F_dot_P ); + +#endif + + zone->tags[point] |= FT_Curve_Tag_Touch_Y; + } +} + + +/*************************************************************************/ +/* */ +/* Special versions of Direct_Move() */ +/* */ +/* The following versions are used whenever both vectors are both */ +/* along one of the coordinate unit vectors, i.e. in 90% of the cases. */ +/* */ +/*************************************************************************/ + + +static +void Direct_Move_X( EXEC_OP_ TT_GlyphZone* zone, + FT_UShort point, + FT_F26Dot6 distance ) { + FT_UNUSED_EXEC; + + zone->cur[point].x += distance; + zone->tags[point] |= FT_Curve_Tag_Touch_X; +} + + +static +void Direct_Move_Y( EXEC_OP_ TT_GlyphZone* zone, + FT_UShort point, + FT_F26Dot6 distance ) { + FT_UNUSED_EXEC; + + zone->cur[point].y += distance; + zone->tags[point] |= FT_Curve_Tag_Touch_Y; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Round_None */ +/* */ +/* */ +/* Does not round, but adds engine compensation. */ +/* */ +/* */ +/* distance :: The distance (not) to round. */ +/* */ +/* compensation :: The engine compensation. */ +/* */ +/* */ +/* The compensated distance. */ +/* */ +/* */ +/* The TrueType specification says very few about the relationship */ +/* between rounding and engine compensation. However, it seems from */ +/* the description of super round that we should add the compensation */ +/* before rounding. */ +/* */ +static +FT_F26Dot6 Round_None( EXEC_OP_ FT_F26Dot6 distance, + FT_F26Dot6 compensation ) { + FT_F26Dot6 val; + + FT_UNUSED_EXEC; + + + if ( distance >= 0 ) { + val = distance + compensation; + if ( val < 0 ) { + val = 0; + } + } else { + val = distance - compensation; + if ( val > 0 ) { + val = 0; + } + } + return val; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Round_To_Grid */ +/* */ +/* */ +/* Rounds value to grid after adding engine compensation. */ +/* */ +/* */ +/* distance :: The distance to round. */ +/* */ +/* compensation :: The engine compensation. */ +/* */ +/* */ +/* Rounded distance. */ +/* */ +static +FT_F26Dot6 Round_To_Grid( EXEC_OP_ FT_F26Dot6 distance, + FT_F26Dot6 compensation ) { + FT_F26Dot6 val; + + FT_UNUSED_EXEC; + + + if ( distance >= 0 ) { + val = distance + compensation + 32; + if ( val > 0 ) { + val &= ~63; + } else { + val = 0; + } + } else + { + val = -( ( compensation - distance + 32 ) & - 64 ); + if ( val > 0 ) { + val = 0; + } + } + + return val; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Round_To_Half_Grid */ +/* */ +/* */ +/* Rounds value to half grid after adding engine compensation. */ +/* */ +/* */ +/* distance :: The distance to round. */ +/* */ +/* compensation :: The engine compensation. */ +/* */ +/* */ +/* Rounded distance. */ +/* */ +static +FT_F26Dot6 Round_To_Half_Grid( EXEC_OP_ FT_F26Dot6 distance, + FT_F26Dot6 compensation ) { + FT_F26Dot6 val; + + FT_UNUSED_EXEC; + + + if ( distance >= 0 ) { + val = ( ( distance + compensation ) & - 64 ) + 32; + if ( val < 0 ) { + val = 0; + } + } else + { + val = -( ( ( compensation - distance ) & - 64 ) + 32 ); + if ( val > 0 ) { + val = 0; + } + } + + return val; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Round_Down_To_Grid */ +/* */ +/* */ +/* Rounds value down to grid after adding engine compensation. */ +/* */ +/* */ +/* distance :: The distance to round. */ +/* */ +/* compensation :: The engine compensation. */ +/* */ +/* */ +/* Rounded distance. */ +/* */ +static +FT_F26Dot6 Round_Down_To_Grid( EXEC_OP_ FT_F26Dot6 distance, + FT_F26Dot6 compensation ) { + FT_F26Dot6 val; + + FT_UNUSED_EXEC; + + + if ( distance >= 0 ) { + val = distance + compensation; + if ( val > 0 ) { + val &= ~63; + } else { + val = 0; + } + } else + { + val = -( ( compensation - distance ) & - 64 ); + if ( val > 0 ) { + val = 0; + } + } + + return val; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Round_Up_To_Grid */ +/* */ +/* */ +/* Rounds value up to grid after adding engine compensation. */ +/* */ +/* */ +/* distance :: The distance to round. */ +/* */ +/* compensation :: The engine compensation. */ +/* */ +/* */ +/* Rounded distance. */ +/* */ +static +FT_F26Dot6 Round_Up_To_Grid( EXEC_OP_ FT_F26Dot6 distance, + FT_F26Dot6 compensation ) { + FT_F26Dot6 val; + + + FT_UNUSED_EXEC; + + if ( distance >= 0 ) { + val = distance + compensation + 63; + if ( val > 0 ) { + val &= ~63; + } else { + val = 0; + } + } else + { + val = -( ( compensation - distance + 63 ) & - 64 ); + if ( val > 0 ) { + val = 0; + } + } + + return val; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Round_To_Double_Grid */ +/* */ +/* */ +/* Rounds value to double grid after adding engine compensation. */ +/* */ +/* */ +/* distance :: The distance to round. */ +/* */ +/* compensation :: The engine compensation. */ +/* */ +/* */ +/* Rounded distance. */ +/* */ +static +FT_F26Dot6 Round_To_Double_Grid( EXEC_OP_ FT_F26Dot6 distance, + FT_F26Dot6 compensation ) { + FT_F26Dot6 val; + + FT_UNUSED_EXEC; + + + if ( distance >= 0 ) { + val = distance + compensation + 16; + if ( val > 0 ) { + val &= ~31; + } else { + val = 0; + } + } else + { + val = -( ( compensation - distance + 16 ) & - 32 ); + if ( val > 0 ) { + val = 0; + } + } + + return val; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Round_Super */ +/* */ +/* */ +/* Super-rounds value to grid after adding engine compensation. */ +/* */ +/* */ +/* distance :: The distance to round. */ +/* */ +/* compensation :: The engine compensation. */ +/* */ +/* */ +/* Rounded distance. */ +/* */ +/* */ +/* The TrueType specification says very few about the relationship */ +/* between rounding and engine compensation. However, it seems from */ +/* the description of super round that we should add the compensation */ +/* before rounding. */ +/* */ +static +FT_F26Dot6 Round_Super( EXEC_OP_ FT_F26Dot6 distance, + FT_F26Dot6 compensation ) { + FT_F26Dot6 val; + + + if ( distance >= 0 ) { + val = ( distance - CUR.phase + CUR.threshold + compensation ) & + - CUR.period; + if ( val < 0 ) { + val = 0; + } + val += CUR.phase; + } else + { + val = -( ( CUR.threshold - CUR.phase - distance + compensation ) & + - CUR.period ); + if ( val > 0 ) { + val = 0; + } + val -= CUR.phase; + } + + return val; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Round_Super_45 */ +/* */ +/* */ +/* Super-rounds value to grid after adding engine compensation. */ +/* */ +/* */ +/* distance :: The distance to round. */ +/* */ +/* compensation :: The engine compensation. */ +/* */ +/* */ +/* Rounded distance. */ +/* */ +/* */ +/* There is a separate function for Round_Super_45() as we may need */ +/* greater precision. */ +/* */ +static +FT_F26Dot6 Round_Super_45( EXEC_OP_ FT_F26Dot6 distance, + FT_F26Dot6 compensation ) { + FT_F26Dot6 val; + + + if ( distance >= 0 ) { + val = ( ( distance - CUR.phase + CUR.threshold + compensation ) / + CUR.period ) * CUR.period; + if ( val < 0 ) { + val = 0; + } + val += CUR.phase; + } else + { + val = -( ( ( CUR.threshold - CUR.phase - distance + compensation ) / + CUR.period ) * CUR.period ); + if ( val > 0 ) { + val = 0; + } + val -= CUR.phase; + } + + return val; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Compute_Round */ +/* */ +/* */ +/* Sets the rounding mode. */ +/* */ +/* */ +/* round_mode :: The rounding mode to be used. */ +/* */ +static +void Compute_Round( EXEC_OP_ FT_Byte round_mode ) { + switch ( round_mode ) + { + case TT_Round_Off: + CUR.func_round = (TT_Round_Func)Round_None; + break; + + case TT_Round_To_Grid: + CUR.func_round = (TT_Round_Func)Round_To_Grid; + break; + + case TT_Round_Up_To_Grid: + CUR.func_round = (TT_Round_Func)Round_Up_To_Grid; + break; + + case TT_Round_Down_To_Grid: + CUR.func_round = (TT_Round_Func)Round_Down_To_Grid; + break; + + case TT_Round_To_Half_Grid: + CUR.func_round = (TT_Round_Func)Round_To_Half_Grid; + break; + + case TT_Round_To_Double_Grid: + CUR.func_round = (TT_Round_Func)Round_To_Double_Grid; + break; + + case TT_Round_Super: + CUR.func_round = (TT_Round_Func)Round_Super; + break; + + case TT_Round_Super_45: + CUR.func_round = (TT_Round_Func)Round_Super_45; + break; + } +} + + +/*************************************************************************/ +/* */ +/* */ +/* SetSuperRound */ +/* */ +/* */ +/* Sets Super Round parameters. */ +/* */ +/* */ +/* GridPeriod :: Grid period */ +/* selector :: SROUND opcode */ +/* */ +static +void SetSuperRound( EXEC_OP_ FT_F26Dot6 GridPeriod, + FT_Long selector ) { + switch ( (FT_Int)( selector & 0xC0 ) ) + { + case 0: + CUR.period = GridPeriod / 2; + break; + + case 0x40: + CUR.period = GridPeriod; + break; + + case 0x80: + CUR.period = GridPeriod * 2; + break; + + /* This opcode is reserved, but... */ + + case 0xC0: + CUR.period = GridPeriod; + break; + } + + switch ( (FT_Int)( selector & 0x30 ) ) + { + case 0: + CUR.phase = 0; + break; + + case 0x10: + CUR.phase = CUR.period / 4; + break; + + case 0x20: + CUR.phase = CUR.period / 2; + break; + + case 0x30: + CUR.phase = GridPeriod * 3 / 4; + break; + } + + if ( ( selector & 0x0F ) == 0 ) { + CUR.threshold = CUR.period - 1; + } else { + CUR.threshold = ( (FT_Int)( selector & 0x0F ) - 4 ) * CUR.period / 8; + } + + CUR.period /= 256; + CUR.phase /= 256; + CUR.threshold /= 256; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Project */ +/* */ +/* */ +/* Computes the projection of vector given by (v2-v1) along the */ +/* current projection vector. */ +/* */ +/* */ +/* v1 :: First input vector. */ +/* v2 :: Second input vector. */ +/* */ +/* */ +/* The distance in F26dot6 format. */ +/* */ +static +FT_F26Dot6 Project( EXEC_OP_ FT_Vector* v1, + FT_Vector* v2 ) { + return TT_MULDIV( v1->x - v2->x, CUR.GS.projVector.x, 0x4000 ) + + TT_MULDIV( v1->y - v2->y, CUR.GS.projVector.y, 0x4000 ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* Dual_Project */ +/* */ +/* */ +/* Computes the projection of the vector given by (v2-v1) along the */ +/* current dual vector. */ +/* */ +/* */ +/* v1 :: First input vector. */ +/* v2 :: Second input vector. */ +/* */ +/* */ +/* The distance in F26dot6 format. */ +/* */ +static +FT_F26Dot6 Dual_Project( EXEC_OP_ FT_Vector* v1, + FT_Vector* v2 ) { + return TT_MULDIV( v1->x - v2->x, CUR.GS.dualVector.x, 0x4000 ) + + TT_MULDIV( v1->y - v2->y, CUR.GS.dualVector.y, 0x4000 ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* Free_Project */ +/* */ +/* */ +/* Computes the projection of the vector given by (v2-v1) along the */ +/* current freedom vector. */ +/* */ +/* */ +/* v1 :: First input vector. */ +/* v2 :: Second input vector. */ +/* */ +/* */ +/* The distance in F26dot6 format. */ +/* */ +static +FT_F26Dot6 Free_Project( EXEC_OP_ FT_Vector* v1, + FT_Vector* v2 ) { + return TT_MULDIV( v1->x - v2->x, CUR.GS.freeVector.x, 0x4000 ) + + TT_MULDIV( v1->y - v2->y, CUR.GS.freeVector.y, 0x4000 ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* Project_x */ +/* */ +/* */ +/* Computes the projection of the vector given by (v2-v1) along the */ +/* horizontal axis. */ +/* */ +/* */ +/* v1 :: First input vector. */ +/* v2 :: Second input vector. */ +/* */ +/* */ +/* The distance in F26dot6 format. */ +/* */ +static +FT_F26Dot6 Project_x( EXEC_OP_ FT_Vector* v1, + FT_Vector* v2 ) { + FT_UNUSED_EXEC; + + return ( v1->x - v2->x ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* Project_y */ +/* */ +/* */ +/* Computes the projection of the vector given by (v2-v1) along the */ +/* vertical axis. */ +/* */ +/* */ +/* v1 :: First input vector. */ +/* v2 :: Second input vector. */ +/* */ +/* */ +/* The distance in F26dot6 format. */ +/* */ +static +FT_F26Dot6 Project_y( EXEC_OP_ FT_Vector* v1, + FT_Vector* v2 ) { + FT_UNUSED_EXEC; + + return ( v1->y - v2->y ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* Compute_Funcs */ +/* */ +/* */ +/* Computes the projection and movement function pointers according */ +/* to the current graphics state. */ +/* */ +static +void Compute_Funcs( EXEC_OP ) { + if ( CUR.GS.freeVector.x == 0x4000 ) { + CUR.func_freeProj = (TT_Project_Func)Project_x; + CUR.F_dot_P = CUR.GS.projVector.x * 0x10000L; + } else + { + if ( CUR.GS.freeVector.y == 0x4000 ) { + CUR.func_freeProj = (TT_Project_Func)Project_y; + CUR.F_dot_P = CUR.GS.projVector.y * 0x10000L; + } else + { + CUR.func_freeProj = (TT_Project_Func)Free_Project; + CUR.F_dot_P = (FT_Long)CUR.GS.projVector.x * CUR.GS.freeVector.x * 4 + + (FT_Long)CUR.GS.projVector.y * CUR.GS.freeVector.y * 4; + } + } + + if ( CUR.GS.projVector.x == 0x4000 ) { + CUR.func_project = (TT_Project_Func)Project_x; + } else + { + if ( CUR.GS.projVector.y == 0x4000 ) { + CUR.func_project = (TT_Project_Func)Project_y; + } else { + CUR.func_project = (TT_Project_Func)Project; + } + } + + if ( CUR.GS.dualVector.x == 0x4000 ) { + CUR.func_dualproj = (TT_Project_Func)Project_x; + } else + { + if ( CUR.GS.dualVector.y == 0x4000 ) { + CUR.func_dualproj = (TT_Project_Func)Project_y; + } else { + CUR.func_dualproj = (TT_Project_Func)Dual_Project; + } + } + + CUR.func_move = (TT_Move_Func)Direct_Move; + + if ( CUR.F_dot_P == 0x40000000L ) { + if ( CUR.GS.freeVector.x == 0x4000 ) { + CUR.func_move = (TT_Move_Func)Direct_Move_X; + } else + { + if ( CUR.GS.freeVector.y == 0x4000 ) { + CUR.func_move = (TT_Move_Func)Direct_Move_Y; + } + } + } + + /* at small sizes, F_dot_P can become too small, resulting */ + /* in overflows and `spikes' in a number of glyphs like `w'. */ + + if ( ABS( CUR.F_dot_P ) < 0x4000000L ) { + CUR.F_dot_P = 0x40000000L; + } + + /* Disable cached aspect ratio */ + CUR.tt_metrics.ratio = 0; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Normalize */ +/* */ +/* */ +/* Norms a vector. */ +/* */ +/* */ +/* Vx :: The horizontal input vector coordinate. */ +/* Vy :: The vertical input vector coordinate. */ +/* */ +/* */ +/* R :: The normed unit vector. */ +/* */ +/* */ +/* Returns FAILURE if a vector parameter is zero. */ +/* */ +/* */ +/* In case Vx and Vy are both zero, Normalize() returns SUCCESS, and */ +/* R is undefined. */ +/* */ + +#ifdef FT_CONFIG_OPTION_OLD_CALCS + +static +FT_Bool Normalize( EXEC_OP_ FT_F26Dot6 Vx, + FT_F26Dot6 Vy, + FT_UnitVector* R ) { + FT_F26Dot6 W; + FT_Bool S1, S2; + + FT_UNUSED_EXEC; + + + if ( ABS( Vx ) < 0x10000L && ABS( Vy ) < 0x10000L ) { + Vx *= 0x100; + Vy *= 0x100; + + W = Norm( Vx, Vy ); + + if ( W == 0 ) { + /* XXX: UNDOCUMENTED! It seems that it is possible to try */ + /* to normalize the vector (0,0). Return immediately. */ + return SUCCESS; + } + + R->x = (FT_F2Dot14)FT_MulDiv( Vx, 0x4000L, W ); + R->y = (FT_F2Dot14)FT_MulDiv( Vy, 0x4000L, W ); + + return SUCCESS; + } + + W = Norm( Vx, Vy ); + + Vx = FT_MulDiv( Vx, 0x4000L, W ); + Vy = FT_MulDiv( Vy, 0x4000L, W ); + + W = Vx * Vx + Vy * Vy; + + /* Now, we want that Sqrt( W ) = 0x4000 */ + /* Or 0x1000000 <= W < 0x1004000 */ + + if ( Vx < 0 ) { + Vx = -Vx; + S1 = TRUE; + } else { + S1 = FALSE; + } + + if ( Vy < 0 ) { + Vy = -Vy; + S2 = TRUE; + } else { + S2 = FALSE; + } + + while ( W < 0x1000000L ) + { + /* We need to increase W by a minimal amount */ + if ( Vx < Vy ) { + Vx++; + } else { + Vy++; + } + + W = Vx * Vx + Vy * Vy; + } + + while ( W >= 0x1004000L ) + { + /* We need to decrease W by a minimal amount */ + if ( Vx < Vy ) { + Vx--; + } else { + Vy--; + } + + W = Vx * Vx + Vy * Vy; + } + + /* Note that in various cases, we can only */ + /* compute a Sqrt(W) of 0x3FFF, eg. Vx = Vy */ + + if ( S1 ) { + Vx = -Vx; + } + + if ( S2 ) { + Vy = -Vy; + } + + R->x = (FT_F2Dot14)Vx; /* Type conversion */ + R->y = (FT_F2Dot14)Vy; /* Type conversion */ + + return SUCCESS; +} + +#else + +static +FT_Bool Normalize( EXEC_OP_ FT_F26Dot6 Vx, + FT_F26Dot6 Vy, + FT_UnitVector* R ) { + FT_F26Dot6 u, v, d; + FT_Int shift; + FT_ULong H, L, L2, hi, lo, med; + + + u = ABS( Vx ); + v = ABS( Vy ); + + if ( u < v ) { + d = u; + u = v; + v = d; + } + + R->x = 0; + R->y = 0; + + /* check that we are not trying to normalise zero! */ + if ( u == 0 ) { + return SUCCESS; + } + + /* compute (u*u + v*v) on 64 bits with two 32-bit registers [H:L] */ + hi = (FT_ULong)u >> 16; + lo = (FT_ULong)u & 0xFFFF; + med = hi * lo; + + H = hi * hi + ( med >> 15 ); + med <<= 17; + L = lo * lo + med; + if ( L < med ) { + H++; + } + + hi = (FT_ULong)v >> 16; + lo = (FT_ULong)v & 0xFFFF; + med = hi * lo; + + H += hi * hi + ( med >> 15 ); + med <<= 17; + L2 = lo * lo + med; + if ( L2 < med ) { + H++; + } + + L += L2; + if ( L < L2 ) { + H++; + } + + /* if the value is smaller than 32-bits */ + if ( H == 0 ) { + shift = 0; + while ( ( L & 0xC0000000L ) == 0 ) + { + L <<= 2; + shift++; + } + + d = FT_Sqrt32( L ); + R->x = (FT_F2Dot14)TT_MULDIV( Vx << shift, 0x4000, d ); + R->y = (FT_F2Dot14)TT_MULDIV( Vy << shift, 0x4000, d ); + } + /* if the value is greater than 64-bits */ + else + { + shift = 0; + while ( H ) + { + L = ( L >> 2 ) | ( H << 30 ); + H >>= 2; + shift++; + } + + d = FT_Sqrt32( L ); + R->x = (FT_F2Dot14)TT_MULDIV( Vx >> shift, 0x4000, d ); + R->y = (FT_F2Dot14)TT_MULDIV( Vy >> shift, 0x4000, d ); + } + + { + FT_ULong x, y, w; + FT_Int sx, sy; + + + sx = R->x >= 0 ? 1 : -1; + sy = R->y >= 0 ? 1 : -1; + x = (FT_ULong)sx * R->x; + y = (FT_ULong)sy * R->y; + + w = x * x + y * y; + + /* we now want to adjust (x,y) in order to have sqrt(w) == 0x4000 */ + /* which means 0x1000000 <= w < 0x1004000 */ + while ( w <= 0x10000000L ) + { + /* increment the smallest coordinate */ + if ( x < y ) { + x++; + } else { + y++; + } + + w = x * x + y * y; + } + + while ( w >= 0x10040000L ) + { + /* decrement the smallest coordinate */ + if ( x < y ) { + x--; + } else { + y--; + } + + w = x * x + y * y; + } + + R->x = sx * x; + R->y = sy * y; + } + + return SUCCESS; +} + +#endif /* FT_CONFIG_OPTION_OLD_CALCS */ + + +/*************************************************************************/ +/* */ +/* Here we start with the implementation of the various opcodes. */ +/* */ +/*************************************************************************/ + + +static +FT_Bool Ins_SxVTL( EXEC_OP_ FT_UShort aIdx1, + FT_UShort aIdx2, + FT_Int aOpc, + FT_UnitVector* Vec ) { + FT_Long A, B, C; + FT_Vector* p1; + FT_Vector* p2; + + + if ( BOUNDS( aIdx1, CUR.zp2.n_points ) || + BOUNDS( aIdx2, CUR.zp1.n_points ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return FAILURE; + } + + p1 = CUR.zp1.cur + aIdx2; + p2 = CUR.zp2.cur + aIdx1; + + A = p1->x - p2->x; + B = p1->y - p2->y; + + if ( ( aOpc & 1 ) != 0 ) { + C = B; /* counter clockwise rotation */ + B = A; + A = -C; + } + + NORMalize( A, B, Vec ); + + return SUCCESS; +} + + +/* When not using the big switch statements, the interpreter uses a */ +/* call table defined later below in this source. Each opcode must */ +/* thus have a corresponding function, even trivial ones. */ +/* */ +/* They are all defined there. */ + +#define DO_SVTCA \ + { \ + FT_Short A, B; \ + \ + \ + A = (FT_Short)( CUR.opcode & 1 ) << 14; \ + B = A ^ (FT_Short)0x4000; \ + \ + CUR.GS.freeVector.x = A; \ + CUR.GS.projVector.x = A; \ + CUR.GS.dualVector.x = A; \ + \ + CUR.GS.freeVector.y = B; \ + CUR.GS.projVector.y = B; \ + CUR.GS.dualVector.y = B; \ + \ + COMPUTE_Funcs(); \ + } + + +#define DO_SPVTCA \ + { \ + FT_Short A, B; \ + \ + \ + A = (FT_Short)( CUR.opcode & 1 ) << 14; \ + B = A ^ (FT_Short)0x4000; \ + \ + CUR.GS.projVector.x = A; \ + CUR.GS.dualVector.x = A; \ + \ + CUR.GS.projVector.y = B; \ + CUR.GS.dualVector.y = B; \ + \ + COMPUTE_Funcs(); \ + } + + +#define DO_SFVTCA \ + { \ + FT_Short A, B; \ + \ + \ + A = (FT_Short)( CUR.opcode & 1 ) << 14; \ + B = A ^ (FT_Short)0x4000; \ + \ + CUR.GS.freeVector.x = A; \ + CUR.GS.freeVector.y = B; \ + \ + COMPUTE_Funcs(); \ + } + + +#define DO_SPVTL \ + if ( INS_SxVTL( (FT_UShort)args[1], \ + (FT_UShort)args[0], \ + CUR.opcode, \ + &CUR.GS.projVector ) == SUCCESS ) \ + { \ + CUR.GS.dualVector = CUR.GS.projVector; \ + COMPUTE_Funcs(); \ + } + + +#define DO_SFVTL \ + if ( INS_SxVTL( (FT_UShort)args[1], \ + (FT_UShort)args[0], \ + CUR.opcode, \ + &CUR.GS.freeVector ) == SUCCESS ) { \ + COMPUTE_Funcs();} + + +#define DO_SFVTPV \ + CUR.GS.freeVector = CUR.GS.projVector; \ + COMPUTE_Funcs(); + + +#define DO_SPVFS \ + { \ + FT_Short S; \ + FT_Long X, Y; \ + \ + \ + /* Only use low 16bits, then sign extend */ \ + S = (FT_Short)args[1]; \ + Y = (FT_Long)S; \ + S = (FT_Short)args[0]; \ + X = (FT_Long)S; \ + \ + NORMalize( X, Y, &CUR.GS.projVector ); \ + \ + CUR.GS.dualVector = CUR.GS.projVector; \ + COMPUTE_Funcs(); \ + } + + +#define DO_SFVFS \ + { \ + FT_Short S; \ + FT_Long X, Y; \ + \ + \ + /* Only use low 16bits, then sign extend */ \ + S = (FT_Short)args[1]; \ + Y = (FT_Long)S; \ + S = (FT_Short)args[0]; \ + X = S; \ + \ + NORMalize( X, Y, &CUR.GS.freeVector ); \ + COMPUTE_Funcs(); \ + } + + +#define DO_GPV \ + args[0] = CUR.GS.projVector.x; \ + args[1] = CUR.GS.projVector.y; + + +#define DO_GFV \ + args[0] = CUR.GS.freeVector.x; \ + args[1] = CUR.GS.freeVector.y; + + +#define DO_SRP0 \ + CUR.GS.rp0 = (FT_UShort)args[0]; + + +#define DO_SRP1 \ + CUR.GS.rp1 = (FT_UShort)args[0]; + + +#define DO_SRP2 \ + CUR.GS.rp2 = (FT_UShort)args[0]; + + +#define DO_RTHG \ + CUR.GS.round_state = TT_Round_To_Half_Grid; \ + CUR.func_round = (TT_Round_Func)Round_To_Half_Grid; + + +#define DO_RTG \ + CUR.GS.round_state = TT_Round_To_Grid; \ + CUR.func_round = (TT_Round_Func)Round_To_Grid; + + +#define DO_RTDG \ + CUR.GS.round_state = TT_Round_To_Double_Grid; \ + CUR.func_round = (TT_Round_Func)Round_To_Double_Grid; + + +#define DO_RUTG \ + CUR.GS.round_state = TT_Round_Up_To_Grid; \ + CUR.func_round = (TT_Round_Func)Round_Up_To_Grid; + + +#define DO_RDTG \ + CUR.GS.round_state = TT_Round_Down_To_Grid; \ + CUR.func_round = (TT_Round_Func)Round_Down_To_Grid; + + +#define DO_ROFF \ + CUR.GS.round_state = TT_Round_Off; \ + CUR.func_round = (TT_Round_Func)Round_None; + + +#define DO_SROUND \ + SET_SuperRound( 0x4000, args[0] ); \ + CUR.GS.round_state = TT_Round_Super; \ + CUR.func_round = (TT_Round_Func)Round_Super; + + +#define DO_S45ROUND \ + SET_SuperRound( 0x2D41, args[0] ); \ + CUR.GS.round_state = TT_Round_Super_45; \ + CUR.func_round = (TT_Round_Func)Round_Super_45; + + +#define DO_SLOOP \ + if ( args[0] < 0 ) { \ + CUR.error = TT_Err_Bad_Argument;} \ + else { \ + CUR.GS.loop = args[0];} + + +#define DO_SMD \ + CUR.GS.minimum_distance = args[0]; + + +#define DO_SCVTCI \ + CUR.GS.control_value_cutin = (FT_F26Dot6)args[0]; + + +#define DO_SSWCI \ + CUR.GS.single_width_cutin = (FT_F26Dot6)args[0]; + + +/* XXX: UNDOCUMENTED! or bug in the Windows engine? */ +/* */ +/* It seems that the value that is read here is */ +/* expressed in 16.16 format rather than in font */ +/* units. */ +/* */ +#define DO_SSW \ + CUR.GS.single_width_value = (FT_F26Dot6)( args[0] >> 10 ); + + +#define DO_FLIPON \ + CUR.GS.auto_flip = TRUE; + + +#define DO_FLIPOFF \ + CUR.GS.auto_flip = FALSE; + + +#define DO_SDB \ + CUR.GS.delta_base = (FT_Short)args[0]; + + +#define DO_SDS \ + CUR.GS.delta_shift = (FT_Short)args[0]; + + +#define DO_MD /* nothing */ + + +#define DO_MPPEM \ + args[0] = CURRENT_Ppem(); + + +/* Note: The pointSize should be irrelevant in a given font program; */ +/* we thus decide to return only the ppem. */ +#if 0 + +#define DO_MPS \ + args[0] = CUR.metrics.pointSize; + +#else + +#define DO_MPS \ + args[0] = CURRENT_Ppem(); + +#endif /* 0 */ + + +#define DO_DUP \ + args[1] = args[0]; + + +#define DO_CLEAR \ + CUR.new_top = 0; + + +#define DO_SWAP \ + { \ + FT_Long L; \ + \ + \ + L = args[0]; \ + args[0] = args[1]; \ + args[1] = L; \ + } + + +#define DO_DEPTH \ + args[0] = CUR.top; + + +#define DO_CINDEX \ + { \ + FT_Long L; \ + \ + \ + L = args[0]; \ + \ + if ( L <= 0 || L > CUR.args ) { \ + CUR.error = TT_Err_Invalid_Reference;} \ + else { \ + args[0] = CUR.stack[CUR.args - L];} \ + } + + +#define DO_JROT \ + if ( args[1] != 0 ) \ + { \ + CUR.IP += args[0]; \ + CUR.step_ins = FALSE; \ + } + + +#define DO_JMPR \ + CUR.IP += args[0]; \ + CUR.step_ins = FALSE; + + +#define DO_JROF \ + if ( args[1] == 0 ) \ + { \ + CUR.IP += args[0]; \ + CUR.step_ins = FALSE; \ + } + + +#define DO_LT \ + args[0] = ( args[0] < args[1] ); + + +#define DO_LTEQ \ + args[0] = ( args[0] <= args[1] ); + + +#define DO_GT \ + args[0] = ( args[0] > args[1] ); + + +#define DO_GTEQ \ + args[0] = ( args[0] >= args[1] ); + + +#define DO_EQ \ + args[0] = ( args[0] == args[1] ); + + +#define DO_NEQ \ + args[0] = ( args[0] != args[1] ); + + +#define DO_ODD \ + args[0] = ( ( CUR_Func_round( args[0], 0 ) & 127 ) == 64 ); + + +#define DO_EVEN \ + args[0] = ( ( CUR_Func_round( args[0], 0 ) & 127 ) == 0 ); + + +#define DO_AND \ + args[0] = ( args[0] && args[1] ); + + +#define DO_OR \ + args[0] = ( args[0] || args[1] ); + + +#define DO_NOT \ + args[0] = !args[0]; + + +#define DO_ADD \ + args[0] += args[1]; + + +#define DO_SUB \ + args[0] -= args[1]; + + +#define DO_DIV \ + if ( args[1] == 0 ) { \ + CUR.error = TT_Err_Divide_By_Zero;} \ + else { \ + args[0] = TT_MULDIV( args[0], 64L, args[1] );} + + +#define DO_MUL \ + args[0] = TT_MULDIV( args[0], args[1], 64L ); + + +#define DO_ABS \ + args[0] = ABS( args[0] ); + + +#define DO_NEG \ + args[0] = -args[0]; + + +#define DO_FLOOR \ + args[0] &= -64; + + +#define DO_CEILING \ + args[0] = ( args[0] + 63 ) & - 64; + + +#define DO_RS \ + { \ + FT_ULong I = (FT_ULong)args[0]; \ + \ + \ + if ( BOUNDS( I, CUR.storeSize ) ) \ + { \ + if ( CUR.pedantic_hinting ) \ + { \ + ARRAY_BOUND_ERROR; \ + } \ + else { \ + args[0] = 0;} \ + } \ + else { \ + args[0] = CUR.storage[I];} \ + } + + +#define DO_WS \ + { \ + FT_ULong I = (FT_ULong)args[0]; \ + \ + \ + if ( BOUNDS( I, CUR.storeSize ) ) \ + { \ + if ( CUR.pedantic_hinting ) \ + { \ + ARRAY_BOUND_ERROR; \ + } \ + } \ + else { \ + CUR.storage[I] = args[1];} \ + } + + +#define DO_RCVT \ + { \ + FT_ULong I = (FT_ULong)args[0]; \ + \ + \ + if ( BOUNDS( I, CUR.cvtSize ) ) \ + { \ + if ( CUR.pedantic_hinting ) \ + { \ + ARRAY_BOUND_ERROR; \ + } \ + else { \ + args[0] = 0;} \ + } \ + else { \ + args[0] = CUR_Func_read_cvt( I );} \ + } + + +#define DO_WCVTP \ + { \ + FT_ULong I = (FT_ULong)args[0]; \ + \ + \ + if ( BOUNDS( I, CUR.cvtSize ) ) \ + { \ + if ( CUR.pedantic_hinting ) \ + { \ + ARRAY_BOUND_ERROR; \ + } \ + } \ + else { \ + CUR_Func_write_cvt( I, args[1] );} \ + } + + +#define DO_WCVTF \ + { \ + FT_ULong I = (FT_ULong)args[0]; \ + \ + \ + if ( BOUNDS( I, CUR.cvtSize ) ) \ + { \ + if ( CUR.pedantic_hinting ) \ + { \ + ARRAY_BOUND_ERROR; \ + } \ + } \ + else { \ + CUR.cvt[I] = TT_MULFIX( args[1], CUR.tt_metrics.scale );} \ + } + + +#define DO_DEBUG \ + CUR.error = TT_Err_Debug_OpCode; + + +#define DO_ROUND \ + args[0] = CUR_Func_round( \ + args[0], \ + CUR.tt_metrics.compensations[CUR.opcode - 0x68] ); + + +#define DO_NROUND \ + args[0] = ROUND_None( args[0], \ + CUR.tt_metrics.compensations[CUR.opcode - 0x6C] ); + + +#define DO_MAX \ + if ( args[1] > args[0] ) { \ + args[0] = args[1];} + + +#define DO_MIN \ + if ( args[1] < args[0] ) { \ + args[0] = args[1];} + + +#ifndef TT_CONFIG_OPTION_INTERPRETER_SWITCH + + +#undef ARRAY_BOUND_ERROR +#define ARRAY_BOUND_ERROR \ + { \ + CUR.error = TT_Err_Invalid_Reference; \ + return; \ + } + + +/*************************************************************************/ +/* */ +/* SVTCA[a]: Set (F and P) Vectors to Coordinate Axis */ +/* Opcode range: 0x00-0x01 */ +/* Stack: --> */ +/* */ +static +void Ins_SVTCA( INS_ARG ) { + DO_SVTCA +} + + +/*************************************************************************/ +/* */ +/* SPVTCA[a]: Set PVector to Coordinate Axis */ +/* Opcode range: 0x02-0x03 */ +/* Stack: --> */ +/* */ +static +void Ins_SPVTCA( INS_ARG ) { + DO_SPVTCA +} + + +/*************************************************************************/ +/* */ +/* SFVTCA[a]: Set FVector to Coordinate Axis */ +/* Opcode range: 0x04-0x05 */ +/* Stack: --> */ +/* */ +static +void Ins_SFVTCA( INS_ARG ) { + DO_SFVTCA +} + + +/*************************************************************************/ +/* */ +/* SPVTL[a]: Set PVector To Line */ +/* Opcode range: 0x06-0x07 */ +/* Stack: uint32 uint32 --> */ +/* */ +static +void Ins_SPVTL( INS_ARG ) { + DO_SPVTL +} + + +/*************************************************************************/ +/* */ +/* SFVTL[a]: Set FVector To Line */ +/* Opcode range: 0x08-0x09 */ +/* Stack: uint32 uint32 --> */ +/* */ +static +void Ins_SFVTL( INS_ARG ) { + DO_SFVTL +} + + +/*************************************************************************/ +/* */ +/* SFVTPV[]: Set FVector To PVector */ +/* Opcode range: 0x0E */ +/* Stack: --> */ +/* */ +static +void Ins_SFVTPV( INS_ARG ) { + DO_SFVTPV +} + + +/*************************************************************************/ +/* */ +/* SPVFS[]: Set PVector From Stack */ +/* Opcode range: 0x0A */ +/* Stack: f2.14 f2.14 --> */ +/* */ +static +void Ins_SPVFS( INS_ARG ) { + DO_SPVFS +} + + +/*************************************************************************/ +/* */ +/* SFVFS[]: Set FVector From Stack */ +/* Opcode range: 0x0B */ +/* Stack: f2.14 f2.14 --> */ +/* */ +static +void Ins_SFVFS( INS_ARG ) { + DO_SFVFS +} + + +/*************************************************************************/ +/* */ +/* GPV[]: Get Projection Vector */ +/* Opcode range: 0x0C */ +/* Stack: ef2.14 --> ef2.14 */ +/* */ +static +void Ins_GPV( INS_ARG ) { + DO_GPV +} + + +/*************************************************************************/ +/* GFV[]: Get Freedom Vector */ +/* Opcode range: 0x0D */ +/* Stack: ef2.14 --> ef2.14 */ +/* */ +static +void Ins_GFV( INS_ARG ) { + DO_GFV +} + + +/*************************************************************************/ +/* */ +/* SRP0[]: Set Reference Point 0 */ +/* Opcode range: 0x10 */ +/* Stack: uint32 --> */ +/* */ +static +void Ins_SRP0( INS_ARG ) { + DO_SRP0 +} + + +/*************************************************************************/ +/* */ +/* SRP1[]: Set Reference Point 1 */ +/* Opcode range: 0x11 */ +/* Stack: uint32 --> */ +/* */ +static +void Ins_SRP1( INS_ARG ) { + DO_SRP1 +} + + +/*************************************************************************/ +/* */ +/* SRP2[]: Set Reference Point 2 */ +/* Opcode range: 0x12 */ +/* Stack: uint32 --> */ +/* */ +static +void Ins_SRP2( INS_ARG ) { + DO_SRP2 +} + + +/*************************************************************************/ +/* */ +/* RTHG[]: Round To Half Grid */ +/* Opcode range: 0x19 */ +/* Stack: --> */ +/* */ +static +void Ins_RTHG( INS_ARG ) { + DO_RTHG +} + + +/*************************************************************************/ +/* */ +/* RTG[]: Round To Grid */ +/* Opcode range: 0x18 */ +/* Stack: --> */ +/* */ +static +void Ins_RTG( INS_ARG ) { + DO_RTG +} + + +/*************************************************************************/ +/* RTDG[]: Round To Double Grid */ +/* Opcode range: 0x3D */ +/* Stack: --> */ +/* */ +static +void Ins_RTDG( INS_ARG ) { + DO_RTDG +} + + +/*************************************************************************/ +/* RUTG[]: Round Up To Grid */ +/* Opcode range: 0x7C */ +/* Stack: --> */ +/* */ +static +void Ins_RUTG( INS_ARG ) { + DO_RUTG +} + + +/*************************************************************************/ +/* */ +/* RDTG[]: Round Down To Grid */ +/* Opcode range: 0x7D */ +/* Stack: --> */ +/* */ +static +void Ins_RDTG( INS_ARG ) { + DO_RDTG +} + + +/*************************************************************************/ +/* */ +/* ROFF[]: Round OFF */ +/* Opcode range: 0x7A */ +/* Stack: --> */ +/* */ +static +void Ins_ROFF( INS_ARG ) { + DO_ROFF +} + + +/*************************************************************************/ +/* */ +/* SROUND[]: Super ROUND */ +/* Opcode range: 0x76 */ +/* Stack: Eint8 --> */ +/* */ +static +void Ins_SROUND( INS_ARG ) { + DO_SROUND +} + + +/*************************************************************************/ +/* */ +/* S45ROUND[]: Super ROUND 45 degrees */ +/* Opcode range: 0x77 */ +/* Stack: uint32 --> */ +/* */ +static +void Ins_S45ROUND( INS_ARG ) { + DO_S45ROUND +} + + +/*************************************************************************/ +/* */ +/* SLOOP[]: Set LOOP variable */ +/* Opcode range: 0x17 */ +/* Stack: int32? --> */ +/* */ +static +void Ins_SLOOP( INS_ARG ) { + DO_SLOOP +} + + +/*************************************************************************/ +/* */ +/* SMD[]: Set Minimum Distance */ +/* Opcode range: 0x1A */ +/* Stack: f26.6 --> */ +/* */ +static +void Ins_SMD( INS_ARG ) { + DO_SMD +} + + +/*************************************************************************/ +/* */ +/* SCVTCI[]: Set Control Value Table Cut In */ +/* Opcode range: 0x1D */ +/* Stack: f26.6 --> */ +/* */ +static +void Ins_SCVTCI( INS_ARG ) { + DO_SCVTCI +} + + +/*************************************************************************/ +/* */ +/* SSWCI[]: Set Single Width Cut In */ +/* Opcode range: 0x1E */ +/* Stack: f26.6 --> */ +/* */ +static +void Ins_SSWCI( INS_ARG ) { + DO_SSWCI +} + + +/*************************************************************************/ +/* */ +/* SSW[]: Set Single Width */ +/* Opcode range: 0x1F */ +/* Stack: int32? --> */ +/* */ +static +void Ins_SSW( INS_ARG ) { + DO_SSW +} + + +/*************************************************************************/ +/* */ +/* FLIPON[]: Set auto-FLIP to ON */ +/* Opcode range: 0x4D */ +/* Stack: --> */ +/* */ +static +void Ins_FLIPON( INS_ARG ) { + DO_FLIPON +} + + +/*************************************************************************/ +/* */ +/* FLIPOFF[]: Set auto-FLIP to OFF */ +/* Opcode range: 0x4E */ +/* Stack: --> */ +/* */ +static +void Ins_FLIPOFF( INS_ARG ) { + DO_FLIPOFF +} + + +/*************************************************************************/ +/* */ +/* SANGW[]: Set ANGle Weight */ +/* Opcode range: 0x7E */ +/* Stack: uint32 --> */ +/* */ +static +void Ins_SANGW( INS_ARG ) { + /* instruction not supported anymore */ +} + + +/*************************************************************************/ +/* */ +/* SDB[]: Set Delta Base */ +/* Opcode range: 0x5E */ +/* Stack: uint32 --> */ +/* */ +static +void Ins_SDB( INS_ARG ) { + DO_SDB +} + + +/*************************************************************************/ +/* */ +/* SDS[]: Set Delta Shift */ +/* Opcode range: 0x5F */ +/* Stack: uint32 --> */ +/* */ +static +void Ins_SDS( INS_ARG ) { + DO_SDS +} + + +/*************************************************************************/ +/* */ +/* MPPEM[]: Measure Pixel Per EM */ +/* Opcode range: 0x4B */ +/* Stack: --> Euint16 */ +/* */ +static +void Ins_MPPEM( INS_ARG ) { + DO_MPPEM +} + + +/*************************************************************************/ +/* */ +/* MPS[]: Measure Point Size */ +/* Opcode range: 0x4C */ +/* Stack: --> Euint16 */ +/* */ +static +void Ins_MPS( INS_ARG ) { + DO_MPS +} + + +/*************************************************************************/ +/* */ +/* DUP[]: DUPlicate the top stack's element */ +/* Opcode range: 0x20 */ +/* Stack: StkElt --> StkElt StkElt */ +/* */ +static +void Ins_DUP( INS_ARG ) { + DO_DUP +} + + +/*************************************************************************/ +/* */ +/* POP[]: POP the stack's top element */ +/* Opcode range: 0x21 */ +/* Stack: StkElt --> */ +/* */ +static +void Ins_POP( INS_ARG ) { + /* nothing to do */ +} + + +/*************************************************************************/ +/* */ +/* CLEAR[]: CLEAR the entire stack */ +/* Opcode range: 0x22 */ +/* Stack: StkElt... --> */ +/* */ +static +void Ins_CLEAR( INS_ARG ) { + DO_CLEAR +} + + +/*************************************************************************/ +/* */ +/* SWAP[]: SWAP the stack's top two elements */ +/* Opcode range: 0x23 */ +/* Stack: 2 * StkElt --> 2 * StkElt */ +/* */ +static +void Ins_SWAP( INS_ARG ) { + DO_SWAP +} + + +/*************************************************************************/ +/* */ +/* DEPTH[]: return the stack DEPTH */ +/* Opcode range: 0x24 */ +/* Stack: --> uint32 */ +/* */ +static +void Ins_DEPTH( INS_ARG ) { + DO_DEPTH +} + + +/*************************************************************************/ +/* */ +/* CINDEX[]: Copy INDEXed element */ +/* Opcode range: 0x25 */ +/* Stack: int32 --> StkElt */ +/* */ +static +void Ins_CINDEX( INS_ARG ) { + DO_CINDEX +} + + +/*************************************************************************/ +/* */ +/* EIF[]: End IF */ +/* Opcode range: 0x59 */ +/* Stack: --> */ +/* */ +static +void Ins_EIF( INS_ARG ) { + /* nothing to do */ +} + + +/*************************************************************************/ +/* */ +/* JROT[]: Jump Relative On True */ +/* Opcode range: 0x78 */ +/* Stack: StkElt int32 --> */ +/* */ +static +void Ins_JROT( INS_ARG ) { + DO_JROT +} + + +/*************************************************************************/ +/* */ +/* JMPR[]: JuMP Relative */ +/* Opcode range: 0x1C */ +/* Stack: int32 --> */ +/* */ +static +void Ins_JMPR( INS_ARG ) { + DO_JMPR +} + + +/*************************************************************************/ +/* */ +/* JROF[]: Jump Relative On False */ +/* Opcode range: 0x79 */ +/* Stack: StkElt int32 --> */ +/* */ +static +void Ins_JROF( INS_ARG ) { + DO_JROF +} + + +/*************************************************************************/ +/* */ +/* LT[]: Less Than */ +/* Opcode range: 0x50 */ +/* Stack: int32? int32? --> bool */ +/* */ +static +void Ins_LT( INS_ARG ) { + DO_LT +} + + +/*************************************************************************/ +/* */ +/* LTEQ[]: Less Than or EQual */ +/* Opcode range: 0x51 */ +/* Stack: int32? int32? --> bool */ +/* */ +static +void Ins_LTEQ( INS_ARG ) { + DO_LTEQ +} + + +/*************************************************************************/ +/* */ +/* GT[]: Greater Than */ +/* Opcode range: 0x52 */ +/* Stack: int32? int32? --> bool */ +/* */ +static +void Ins_GT( INS_ARG ) { + DO_GT +} + + +/*************************************************************************/ +/* */ +/* GTEQ[]: Greater Than or EQual */ +/* Opcode range: 0x53 */ +/* Stack: int32? int32? --> bool */ +/* */ +static +void Ins_GTEQ( INS_ARG ) { + DO_GTEQ +} + + +/*************************************************************************/ +/* */ +/* EQ[]: EQual */ +/* Opcode range: 0x54 */ +/* Stack: StkElt StkElt --> bool */ +/* */ +static +void Ins_EQ( INS_ARG ) { + DO_EQ +} + + +/*************************************************************************/ +/* */ +/* NEQ[]: Not EQual */ +/* Opcode range: 0x55 */ +/* Stack: StkElt StkElt --> bool */ +/* */ +static +void Ins_NEQ( INS_ARG ) { + DO_NEQ +} + + +/*************************************************************************/ +/* */ +/* ODD[]: Is ODD */ +/* Opcode range: 0x56 */ +/* Stack: f26.6 --> bool */ +/* */ +static +void Ins_ODD( INS_ARG ) { + DO_ODD +} + + +/*************************************************************************/ +/* */ +/* EVEN[]: Is EVEN */ +/* Opcode range: 0x57 */ +/* Stack: f26.6 --> bool */ +/* */ +static +void Ins_EVEN( INS_ARG ) { + DO_EVEN +} + + +/*************************************************************************/ +/* */ +/* AND[]: logical AND */ +/* Opcode range: 0x5A */ +/* Stack: uint32 uint32 --> uint32 */ +/* */ +static +void Ins_AND( INS_ARG ) { + DO_AND +} + + +/*************************************************************************/ +/* */ +/* OR[]: logical OR */ +/* Opcode range: 0x5B */ +/* Stack: uint32 uint32 --> uint32 */ +/* */ +static +void Ins_OR( INS_ARG ) { + DO_OR +} + + +/*************************************************************************/ +/* */ +/* NOT[]: logical NOT */ +/* Opcode range: 0x5C */ +/* Stack: StkElt --> uint32 */ +/* */ +static +void Ins_NOT( INS_ARG ) { + DO_NOT +} + + +/*************************************************************************/ +/* */ +/* ADD[]: ADD */ +/* Opcode range: 0x60 */ +/* Stack: f26.6 f26.6 --> f26.6 */ +/* */ +static +void Ins_ADD( INS_ARG ) { + DO_ADD +} + + +/*************************************************************************/ +/* */ +/* SUB[]: SUBtract */ +/* Opcode range: 0x61 */ +/* Stack: f26.6 f26.6 --> f26.6 */ +/* */ +static +void Ins_SUB( INS_ARG ) { + DO_SUB +} + + +/*************************************************************************/ +/* */ +/* DIV[]: DIVide */ +/* Opcode range: 0x62 */ +/* Stack: f26.6 f26.6 --> f26.6 */ +/* */ +static +void Ins_DIV( INS_ARG ) { + DO_DIV +} + + +/*************************************************************************/ +/* */ +/* MUL[]: MULtiply */ +/* Opcode range: 0x63 */ +/* Stack: f26.6 f26.6 --> f26.6 */ +/* */ +static +void Ins_MUL( INS_ARG ) { + DO_MUL +} + + +/*************************************************************************/ +/* */ +/* ABS[]: ABSolute value */ +/* Opcode range: 0x64 */ +/* Stack: f26.6 --> f26.6 */ +/* */ +static +void Ins_ABS( INS_ARG ) { + DO_ABS +} + + +/*************************************************************************/ +/* */ +/* NEG[]: NEGate */ +/* Opcode range: 0x65 */ +/* Stack: f26.6 --> f26.6 */ +/* */ +static +void Ins_NEG( INS_ARG ) { + DO_NEG +} + + +/*************************************************************************/ +/* */ +/* FLOOR[]: FLOOR */ +/* Opcode range: 0x66 */ +/* Stack: f26.6 --> f26.6 */ +/* */ +static +void Ins_FLOOR( INS_ARG ) { + DO_FLOOR +} + + +/*************************************************************************/ +/* */ +/* CEILING[]: CEILING */ +/* Opcode range: 0x67 */ +/* Stack: f26.6 --> f26.6 */ +/* */ +static +void Ins_CEILING( INS_ARG ) { + DO_CEILING +} + + +/*************************************************************************/ +/* */ +/* RS[]: Read Store */ +/* Opcode range: 0x43 */ +/* Stack: uint32 --> uint32 */ +/* */ +static +void Ins_RS( INS_ARG ) { + DO_RS +} + + +/*************************************************************************/ +/* */ +/* WS[]: Write Store */ +/* Opcode range: 0x42 */ +/* Stack: uint32 uint32 --> */ +/* */ +static +void Ins_WS( INS_ARG ) { + DO_WS +} + + +/*************************************************************************/ +/* */ +/* WCVTP[]: Write CVT in Pixel units */ +/* Opcode range: 0x44 */ +/* Stack: f26.6 uint32 --> */ +/* */ +static +void Ins_WCVTP( INS_ARG ) { + DO_WCVTP +} + + +/*************************************************************************/ +/* */ +/* WCVTF[]: Write CVT in Funits */ +/* Opcode range: 0x70 */ +/* Stack: uint32 uint32 --> */ +/* */ +static +void Ins_WCVTF( INS_ARG ) { + DO_WCVTF +} + + +/*************************************************************************/ +/* */ +/* RCVT[]: Read CVT */ +/* Opcode range: 0x45 */ +/* Stack: uint32 --> f26.6 */ +/* */ +static +void Ins_RCVT( INS_ARG ) { + DO_RCVT +} + + +/*************************************************************************/ +/* */ +/* AA[]: Adjust Angle */ +/* Opcode range: 0x7F */ +/* Stack: uint32 --> */ +/* */ +static +void Ins_AA( INS_ARG ) { + /* intentionally no longer supported */ +} + + +/*************************************************************************/ +/* */ +/* DEBUG[]: DEBUG. Unsupported. */ +/* Opcode range: 0x4F */ +/* Stack: uint32 --> */ +/* */ +/* Note: The original instruction pops a value from the stack. */ +/* */ +static +void Ins_DEBUG( INS_ARG ) { + DO_DEBUG +} + + +/*************************************************************************/ +/* */ +/* ROUND[ab]: ROUND value */ +/* Opcode range: 0x68-0x6B */ +/* Stack: f26.6 --> f26.6 */ +/* */ +static +void Ins_ROUND( INS_ARG ) { + DO_ROUND +} + + +/*************************************************************************/ +/* */ +/* NROUND[ab]: No ROUNDing of value */ +/* Opcode range: 0x6C-0x6F */ +/* Stack: f26.6 --> f26.6 */ +/* */ +static +void Ins_NROUND( INS_ARG ) { + DO_NROUND +} + + +/*************************************************************************/ +/* */ +/* MAX[]: MAXimum */ +/* Opcode range: 0x68 */ +/* Stack: int32? int32? --> int32 */ +/* */ +static +void Ins_MAX( INS_ARG ) { + DO_MAX +} + + +/*************************************************************************/ +/* */ +/* MIN[]: MINimum */ +/* Opcode range: 0x69 */ +/* Stack: int32? int32? --> int32 */ +/* */ +static +void Ins_MIN( INS_ARG ) { + DO_MIN +} + + +#endif /* !TT_CONFIG_OPTION_INTERPRETER_SWITCH */ + + +/*************************************************************************/ +/* */ +/* The following functions are called as is within the switch statement. */ +/* */ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* MINDEX[]: Move INDEXed element */ +/* Opcode range: 0x26 */ +/* Stack: int32? --> StkElt */ +/* */ +static +void Ins_MINDEX( INS_ARG ) { + FT_Long L, K; + + + L = args[0]; + + if ( L <= 0 || L > CUR.args ) { + CUR.error = TT_Err_Invalid_Reference; + return; + } + + K = CUR.stack[CUR.args - L]; + + MEM_Move( &CUR.stack[CUR.args - L ], + &CUR.stack[CUR.args - L + 1], + ( L - 1 ) * sizeof( FT_Long ) ); + + CUR.stack[CUR.args - 1] = K; +} + + +/*************************************************************************/ +/* */ +/* ROLL[]: ROLL top three elements */ +/* Opcode range: 0x8A */ +/* Stack: 3 * StkElt --> 3 * StkElt */ +/* */ +static +void Ins_ROLL( INS_ARG ) { + FT_Long A, B, C; + + FT_UNUSED_EXEC; + + + A = args[2]; + B = args[1]; + C = args[0]; + + args[2] = C; + args[1] = A; + args[0] = B; +} + + +/*************************************************************************/ +/* */ +/* MANAGING THE FLOW OF CONTROL */ +/* */ +/* Instructions appear in the specification's order. */ +/* */ +/*************************************************************************/ + + +static +FT_Bool SkipCode( EXEC_OP ) { + CUR.IP += CUR.length; + + if ( CUR.IP < CUR.codeSize ) { + CUR.opcode = CUR.code[CUR.IP]; + + CUR.length = opcode_length[CUR.opcode]; + if ( CUR.length < 0 ) { + if ( CUR.IP + 1 > CUR.codeSize ) { + goto Fail_Overflow; + } + CUR.length = CUR.code[CUR.IP + 1] + 2; + } + + if ( CUR.IP + CUR.length <= CUR.codeSize ) { + return SUCCESS; + } + } + +Fail_Overflow: + CUR.error = TT_Err_Code_Overflow; + return FAILURE; +} + + +/*************************************************************************/ +/* */ +/* IF[]: IF test */ +/* Opcode range: 0x58 */ +/* Stack: StkElt --> */ +/* */ +static +void Ins_IF( INS_ARG ) { + FT_Int nIfs; + FT_Bool Out; + + + if ( args[0] != 0 ) { + return; + } + + nIfs = 1; + Out = 0; + + do + { + if ( SKIP_Code() == FAILURE ) { + return; + } + + switch ( CUR.opcode ) + { + case 0x58: /* IF */ + nIfs++; + break; + + case 0x1B: /* ELSE */ + Out = ( nIfs == 1 ); + break; + + case 0x59: /* EIF */ + nIfs--; + Out = ( nIfs == 0 ); + break; + } + } while ( Out == 0 ); +} + + +/*************************************************************************/ +/* */ +/* ELSE[]: ELSE */ +/* Opcode range: 0x1B */ +/* Stack: --> */ +/* */ +static +void Ins_ELSE( INS_ARG ) { + FT_Int nIfs; + + FT_UNUSED_ARG; + + + nIfs = 1; + + do + { + if ( SKIP_Code() == FAILURE ) { + return; + } + + switch ( CUR.opcode ) + { + case 0x58: /* IF */ + nIfs++; + break; + + case 0x59: /* EIF */ + nIfs--; + break; + } + } while ( nIfs != 0 ); +} + + +/*************************************************************************/ +/* */ +/* DEFINING AND USING FUNCTIONS AND INSTRUCTIONS */ +/* */ +/* Instructions appear in the specification's order. */ +/* */ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* FDEF[]: Function DEFinition */ +/* Opcode range: 0x2C */ +/* Stack: uint32 --> */ +/* */ +static +void Ins_FDEF( INS_ARG ) { + FT_ULong n; + TT_DefRecord* rec; + TT_DefRecord* limit; + + + /* some font programs are broken enough to redefine functions! */ + /* We will then parse the current table. */ + + rec = CUR.FDefs; + limit = rec + CUR.numFDefs; + n = args[0]; + + for ( ; rec < limit; rec++ ) + { + if ( rec->opc == n ) { + break; + } + } + + if ( rec == limit ) { + /* check that there is enough room for new functions */ + if ( CUR.numFDefs >= CUR.maxFDefs ) { + CUR.error = TT_Err_Too_Many_Function_Defs; + return; + } + CUR.numFDefs++; + } + + rec->range = CUR.curRange; + rec->opc = n; + rec->start = CUR.IP + 1; + rec->active = TRUE; + + if ( n > CUR.maxFunc ) { + CUR.maxFunc = n; + } + + /* Now skip the whole function definition. */ + /* We don't allow nested IDEFS & FDEFs. */ + + while ( SKIP_Code() == SUCCESS ) + { + switch ( CUR.opcode ) + { + case 0x89: /* IDEF */ + case 0x2C: /* FDEF */ + CUR.error = TT_Err_Nested_DEFS; + return; + + case 0x2D: /* ENDF */ + return; + } + } +} + + +/*************************************************************************/ +/* */ +/* ENDF[]: END Function definition */ +/* Opcode range: 0x2D */ +/* Stack: --> */ +/* */ +static +void Ins_ENDF( INS_ARG ) { + TT_CallRec* pRec; + + FT_UNUSED_ARG; + + + if ( CUR.callTop <= 0 ) { /* We encountered an ENDF without a call */ + CUR.error = TT_Err_ENDF_In_Exec_Stream; + return; + } + + CUR.callTop--; + + pRec = &CUR.callStack[CUR.callTop]; + + pRec->Cur_Count--; + + CUR.step_ins = FALSE; + + if ( pRec->Cur_Count > 0 ) { + CUR.callTop++; + CUR.IP = pRec->Cur_Restart; + } else { + /* Loop through the current function */ + INS_Goto_CodeRange( pRec->Caller_Range, + pRec->Caller_IP ); + } + + /* Exit the current call frame. */ + + /* NOTE: If the last intruction of a program is a */ + /* CALL or LOOPCALL, the return address is */ + /* always out of the code range. This is a */ + /* valid address, and it is why we do not test */ + /* the result of Ins_Goto_CodeRange() here! */ +} + + +/*************************************************************************/ +/* */ +/* CALL[]: CALL function */ +/* Opcode range: 0x2B */ +/* Stack: uint32? --> */ +/* */ +static +void Ins_CALL( INS_ARG ) { + FT_ULong F; + TT_CallRec* pCrec; + TT_DefRecord* def; + + + /* first of all, check the index */ + + F = args[0]; + if ( BOUNDS( F, CUR.maxFunc + 1 ) ) { + goto Fail; + } + + /* Except for some old Apple fonts, all functions in a TrueType */ + /* font are defined in increasing order, starting from 0. This */ + /* means that we normally have */ + /* */ + /* CUR.maxFunc+1 == CUR.numFDefs */ + /* CUR.FDefs[n].opc == n for n in 0..CUR.maxFunc */ + /* */ + /* If this isn't true, we need to look up the function table. */ + + def = CUR.FDefs + F; + if ( CUR.maxFunc + 1 != CUR.numFDefs || def->opc != F ) { + /* look up the FDefs table */ + TT_DefRecord* limit; + + + def = CUR.FDefs; + limit = def + CUR.numFDefs; + + while ( def < limit && def->opc != F ) + def++; + + if ( def == limit ) { + goto Fail; + } + } + + /* check that the function is active */ + if ( !def->active ) { + goto Fail; + } + + /* check the call stack */ + if ( CUR.callTop >= CUR.callSize ) { + CUR.error = TT_Err_Stack_Overflow; + return; + } + + pCrec = CUR.callStack + CUR.callTop; + + pCrec->Caller_Range = CUR.curRange; + pCrec->Caller_IP = CUR.IP + 1; + pCrec->Cur_Count = 1; + pCrec->Cur_Restart = def->start; + + CUR.callTop++; + + INS_Goto_CodeRange( def->range, + def->start ); + + CUR.step_ins = FALSE; + return; + +Fail: + CUR.error = TT_Err_Invalid_Reference; +} + + +/*************************************************************************/ +/* */ +/* LOOPCALL[]: LOOP and CALL function */ +/* Opcode range: 0x2A */ +/* Stack: uint32? Eint16? --> */ +/* */ +static +void Ins_LOOPCALL( INS_ARG ) { + FT_ULong F; + TT_CallRec* pCrec; + TT_DefRecord* def; + + + /* first of all, check the index */ + F = args[1]; + if ( BOUNDS( F, CUR.maxFunc + 1 ) ) { + goto Fail; + } + + /* Except for some old Apple fonts, all functions in a TrueType */ + /* font are defined in increasing order, starting from 0. This */ + /* means that we normally have */ + /* */ + /* CUR.maxFunc+1 == CUR.numFDefs */ + /* CUR.FDefs[n].opc == n for n in 0..CUR.maxFunc */ + /* */ + /* If this isn't true, we need to look up the function table. */ + + def = CUR.FDefs + F; + if ( CUR.maxFunc + 1 != CUR.numFDefs || def->opc != F ) { + /* look up the FDefs table */ + TT_DefRecord* limit; + + + def = CUR.FDefs; + limit = def + CUR.numFDefs; + + while ( def < limit && def->opc != F ) + def++; + + if ( def == limit ) { + goto Fail; + } + } + + /* check that the function is active */ + if ( !def->active ) { + goto Fail; + } + + /* check stack */ + if ( CUR.callTop >= CUR.callSize ) { + CUR.error = TT_Err_Stack_Overflow; + return; + } + + if ( args[0] > 0 ) { + pCrec = CUR.callStack + CUR.callTop; + + pCrec->Caller_Range = CUR.curRange; + pCrec->Caller_IP = CUR.IP + 1; + pCrec->Cur_Count = (FT_Int)args[0]; + pCrec->Cur_Restart = def->start; + + CUR.callTop++; + + INS_Goto_CodeRange( def->range, def->start ); + + CUR.step_ins = FALSE; + } + return; + +Fail: + CUR.error = TT_Err_Invalid_Reference; +} + + +/*************************************************************************/ +/* */ +/* IDEF[]: Instruction DEFinition */ +/* Opcode range: 0x89 */ +/* Stack: Eint8 --> */ +/* */ +static +void Ins_IDEF( INS_ARG ) { + TT_DefRecord* def; + TT_DefRecord* limit; + + + /* First of all, look for the same function in our table */ + + def = CUR.IDefs; + limit = def + CUR.numIDefs; + + for ( ; def < limit; def++ ) + if ( def->opc == (FT_ULong)args[0] ) { + break; + } + + if ( def == limit ) { + /* check that there is enough room for a new instruction */ + if ( CUR.numIDefs >= CUR.maxIDefs ) { + CUR.error = TT_Err_Too_Many_Instruction_Defs; + return; + } + CUR.numIDefs++; + } + + def->opc = args[0]; + def->start = CUR.IP + 1; + def->range = CUR.curRange; + def->active = TRUE; + + if ( (FT_ULong)args[0] > CUR.maxIns ) { + CUR.maxIns = args[0]; + } + + /* Now skip the whole function definition. */ + /* We don't allow nested IDEFs & FDEFs. */ + + while ( SKIP_Code() == SUCCESS ) + { + switch ( CUR.opcode ) + { + case 0x89: /* IDEF */ + case 0x2C: /* FDEF */ + CUR.error = TT_Err_Nested_DEFS; + return; + case 0x2D: /* ENDF */ + return; + } + } +} + + +/*************************************************************************/ +/* */ +/* PUSHING DATA ONTO THE INTERPRETER STACK */ +/* */ +/* Instructions appear in the specification's order. */ +/* */ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* NPUSHB[]: PUSH N Bytes */ +/* Opcode range: 0x40 */ +/* Stack: --> uint32... */ +/* */ +static +void Ins_NPUSHB( INS_ARG ) { + FT_UShort L, K; + + + L = (FT_UShort)CUR.code[CUR.IP + 1]; + + if ( BOUNDS( L, CUR.stackSize + 1 - CUR.top ) ) { + CUR.error = TT_Err_Stack_Overflow; + return; + } + + for ( K = 1; K <= L; K++ ) + args[K - 1] = CUR.code[CUR.IP + K + 1]; + + CUR.new_top += L; +} + + +/*************************************************************************/ +/* */ +/* NPUSHW[]: PUSH N Words */ +/* Opcode range: 0x41 */ +/* Stack: --> int32... */ +/* */ +static +void Ins_NPUSHW( INS_ARG ) { + FT_UShort L, K; + + + L = (FT_UShort)CUR.code[CUR.IP + 1]; + + if ( BOUNDS( L, CUR.stackSize + 1 - CUR.top ) ) { + CUR.error = TT_Err_Stack_Overflow; + return; + } + + CUR.IP += 2; + + for ( K = 0; K < L; K++ ) + args[K] = GET_ShortIns(); + + CUR.step_ins = FALSE; + CUR.new_top += L; +} + + +/*************************************************************************/ +/* */ +/* PUSHB[abc]: PUSH Bytes */ +/* Opcode range: 0xB0-0xB7 */ +/* Stack: --> uint32... */ +/* */ +static +void Ins_PUSHB( INS_ARG ) { + FT_UShort L, K; + + + L = (FT_UShort)CUR.opcode - 0xB0 + 1; + + if ( BOUNDS( L, CUR.stackSize + 1 - CUR.top ) ) { + CUR.error = TT_Err_Stack_Overflow; + return; + } + + for ( K = 1; K <= L; K++ ) + args[K - 1] = CUR.code[CUR.IP + K]; +} + + +/*************************************************************************/ +/* */ +/* PUSHW[abc]: PUSH Words */ +/* Opcode range: 0xB8-0xBF */ +/* Stack: --> int32... */ +/* */ +static +void Ins_PUSHW( INS_ARG ) { + FT_UShort L, K; + + + L = (FT_UShort)CUR.opcode - 0xB8 + 1; + + if ( BOUNDS( L, CUR.stackSize + 1 - CUR.top ) ) { + CUR.error = TT_Err_Stack_Overflow; + return; + } + + CUR.IP++; + + for ( K = 0; K < L; K++ ) + args[K] = GET_ShortIns(); + + CUR.step_ins = FALSE; +} + + +/*************************************************************************/ +/* */ +/* MANAGING THE GRAPHICS STATE */ +/* */ +/* Instructions appear in the specs' order. */ +/* */ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* GC[a]: Get Coordinate projected onto */ +/* Opcode range: 0x46-0x47 */ +/* Stack: uint32 --> f26.6 */ +/* */ +/* BULLSHIT: Measures from the original glyph must be taken along the */ +/* dual projection vector! */ +/* */ +static void Ins_GC( INS_ARG ) { + FT_ULong L; + FT_F26Dot6 R; + + + L = (FT_ULong)args[0]; + + if ( BOUNDS( L, CUR.zp2.n_points ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + return; + } else { + R = 0; + } + } else + { + if ( CUR.opcode & 1 ) { + R = CUR_Func_dualproj( CUR.zp2.org + L, NULL_Vector ); + } else { + R = CUR_Func_project( CUR.zp2.cur + L, NULL_Vector ); + } + } + + args[0] = R; +} + + +/*************************************************************************/ +/* */ +/* SCFS[]: Set Coordinate From Stack */ +/* Opcode range: 0x48 */ +/* Stack: f26.6 uint32 --> */ +/* */ +/* Formula: */ +/* */ +/* OA := OA + ( value - OA.p )/( f.p ) * f */ +/* */ +static +void Ins_SCFS( INS_ARG ) { + FT_Long K; + FT_UShort L; + + + L = (FT_UShort)args[0]; + + if ( BOUNDS( L, CUR.zp2.n_points ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return; + } + + K = CUR_Func_project( CUR.zp2.cur + L, NULL_Vector ); + + CUR_Func_move( &CUR.zp2, L, args[1] - K ); + + /* not part of the specs, but here for safety */ + + if ( CUR.GS.gep2 == 0 ) { + CUR.zp2.org[L] = CUR.zp2.cur[L]; + } +} + + +/*************************************************************************/ +/* */ +/* MD[a]: Measure Distance */ +/* Opcode range: 0x49-0x4A */ +/* Stack: uint32 uint32 --> f26.6 */ +/* */ +/* BULLSHIT: Measure taken in the original glyph must be along the dual */ +/* projection vector. */ +/* */ +/* Second BULLSHIT: Flag attributes are inverted! */ +/* 0 => measure distance in original outline */ +/* 1 => measure distance in grid-fitted outline */ +/* */ +/* Third one: `zp0 - zp1', and not `zp2 - zp1! */ +/* */ +static +void Ins_MD( INS_ARG ) { + FT_UShort K, L; + FT_F26Dot6 D; + + + K = (FT_UShort)args[1]; + L = (FT_UShort)args[0]; + + if ( BOUNDS( L, CUR.zp0.n_points ) || + BOUNDS( K, CUR.zp1.n_points ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + return; + } + D = 0; + } else + { + if ( CUR.opcode & 1 ) { + D = CUR_Func_project( CUR.zp0.cur + L, CUR.zp1.cur + K ); + } else { + D = CUR_Func_dualproj( CUR.zp0.org + L, CUR.zp1.org + K ); + } + } + + args[0] = D; +} + + +/*************************************************************************/ +/* */ +/* SDPVTL[a]: Set Dual PVector to Line */ +/* Opcode range: 0x86-0x87 */ +/* Stack: uint32 uint32 --> */ +/* */ +static +void Ins_SDPVTL( INS_ARG ) { + FT_Long A, B, C; + FT_UShort p1, p2; /* was FT_Int in pas type ERROR */ + + + p1 = (FT_UShort)args[1]; + p2 = (FT_UShort)args[0]; + + if ( BOUNDS( p2, CUR.zp1.n_points ) || + BOUNDS( p1, CUR.zp2.n_points ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return; + } + + { + FT_Vector* v1 = CUR.zp1.org + p2; + FT_Vector* v2 = CUR.zp2.org + p1; + + + A = v1->x - v2->x; + B = v1->y - v2->y; + } + + if ( ( CUR.opcode & 1 ) != 0 ) { + C = B; /* counter clockwise rotation */ + B = A; + A = -C; + } + + NORMalize( A, B, &CUR.GS.dualVector ); + + { + FT_Vector* v1 = CUR.zp1.cur + p2; + FT_Vector* v2 = CUR.zp2.cur + p1; + + + A = v1->x - v2->x; + B = v1->y - v2->y; + } + + if ( ( CUR.opcode & 1 ) != 0 ) { + C = B; /* counter clockwise rotation */ + B = A; + A = -C; + } + + NORMalize( A, B, &CUR.GS.projVector ); + + COMPUTE_Funcs(); +} + + +/*************************************************************************/ +/* */ +/* SZP0[]: Set Zone Pointer 0 */ +/* Opcode range: 0x13 */ +/* Stack: uint32 --> */ +/* */ +static +void Ins_SZP0( INS_ARG ) { + switch ( (FT_Int)args[0] ) + { + case 0: + CUR.zp0 = CUR.twilight; + break; + + case 1: + CUR.zp0 = CUR.pts; + break; + + default: + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return; + } + + CUR.GS.gep0 = (FT_UShort)args[0]; +} + + +/*************************************************************************/ +/* */ +/* SZP1[]: Set Zone Pointer 1 */ +/* Opcode range: 0x14 */ +/* Stack: uint32 --> */ +/* */ +static +void Ins_SZP1( INS_ARG ) { + switch ( (FT_Int)args[0] ) + { + case 0: + CUR.zp1 = CUR.twilight; + break; + + case 1: + CUR.zp1 = CUR.pts; + break; + + default: + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return; + } + + CUR.GS.gep1 = (FT_UShort)args[0]; +} + + +/*************************************************************************/ +/* */ +/* SZP2[]: Set Zone Pointer 2 */ +/* Opcode range: 0x15 */ +/* Stack: uint32 --> */ +/* */ +static +void Ins_SZP2( INS_ARG ) { + switch ( (FT_Int)args[0] ) + { + case 0: + CUR.zp2 = CUR.twilight; + break; + + case 1: + CUR.zp2 = CUR.pts; + break; + + default: + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return; + } + + CUR.GS.gep2 = (FT_UShort)args[0]; +} + + +/*************************************************************************/ +/* */ +/* SZPS[]: Set Zone PointerS */ +/* Opcode range: 0x16 */ +/* Stack: uint32 --> */ +/* */ +static +void Ins_SZPS( INS_ARG ) { + switch ( (FT_Int)args[0] ) + { + case 0: + CUR.zp0 = CUR.twilight; + break; + + case 1: + CUR.zp0 = CUR.pts; + break; + + default: + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return; + } + + CUR.zp1 = CUR.zp0; + CUR.zp2 = CUR.zp0; + + CUR.GS.gep0 = (FT_UShort)args[0]; + CUR.GS.gep1 = (FT_UShort)args[0]; + CUR.GS.gep2 = (FT_UShort)args[0]; +} + + +/*************************************************************************/ +/* */ +/* INSTCTRL[]: INSTruction ConTRoL */ +/* Opcode range: 0x8e */ +/* Stack: int32 int32 --> */ +/* */ +static +void Ins_INSTCTRL( INS_ARG ) { + FT_Long K, L; + + + K = args[1]; + L = args[0]; + + if ( K < 1 || K > 2 ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return; + } + + if ( L != 0 ) { + L = K; + } + + CUR.GS.instruct_control = + (FT_Byte)( CUR.GS.instruct_control & ~(FT_Byte)K ) | (FT_Byte)L; +} + + +/*************************************************************************/ +/* */ +/* SCANCTRL[]: SCAN ConTRoL */ +/* Opcode range: 0x85 */ +/* Stack: uint32? --> */ +/* */ +static +void Ins_SCANCTRL( INS_ARG ) { + FT_Int A; + + + /* Get Threshold */ + A = (FT_Int)( args[0] & 0xFF ); + + if ( A == 0xFF ) { + CUR.GS.scan_control = TRUE; + return; + } else if ( A == 0 ) { + CUR.GS.scan_control = FALSE; + return; + } + + A *= 64; + +#if 0 + if ( ( args[0] & 0x100 ) != 0 && CUR.metrics.pointSize <= A ) { + CUR.GS.scan_control = TRUE; + } +#endif + + if ( ( args[0] & 0x200 ) != 0 && CUR.tt_metrics.rotated ) { + CUR.GS.scan_control = TRUE; + } + + if ( ( args[0] & 0x400 ) != 0 && CUR.tt_metrics.stretched ) { + CUR.GS.scan_control = TRUE; + } + +#if 0 + if ( ( args[0] & 0x800 ) != 0 && CUR.metrics.pointSize > A ) { + CUR.GS.scan_control = FALSE; + } +#endif + + if ( ( args[0] & 0x1000 ) != 0 && CUR.tt_metrics.rotated ) { + CUR.GS.scan_control = FALSE; + } + + if ( ( args[0] & 0x2000 ) != 0 && CUR.tt_metrics.stretched ) { + CUR.GS.scan_control = FALSE; + } +} + + +/*************************************************************************/ +/* */ +/* SCANTYPE[]: SCAN TYPE */ +/* Opcode range: 0x8D */ +/* Stack: uint32? --> */ +/* */ +static +void Ins_SCANTYPE( INS_ARG ) { + /* for compatibility with future enhancements, */ + /* we must ignore new modes */ + + if ( args[0] >= 0 && args[0] <= 5 ) { + if ( args[0] == 3 ) { + args[0] = 2; + } + + CUR.GS.scan_type = (FT_Int)args[0]; + } +} + + +/*************************************************************************/ +/* */ +/* MANAGING OUTLINES */ +/* */ +/* Instructions appear in the specification's order. */ +/* */ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* FLIPPT[]: FLIP PoinT */ +/* Opcode range: 0x80 */ +/* Stack: uint32... --> */ +/* */ +static +void Ins_FLIPPT( INS_ARG ) { + FT_UShort point; + + FT_UNUSED_ARG; + + + if ( CUR.top < CUR.GS.loop ) { + CUR.error = TT_Err_Too_Few_Arguments; + return; + } + + while ( CUR.GS.loop > 0 ) + { + CUR.args--; + + point = (FT_UShort)CUR.stack[CUR.args]; + + if ( BOUNDS( point, CUR.pts.n_points ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + return; + } + } else { + CUR.pts.tags[point] ^= FT_Curve_Tag_On; + } + + CUR.GS.loop--; + } + + CUR.GS.loop = 1; + CUR.new_top = CUR.args; +} + + +/*************************************************************************/ +/* */ +/* FLIPRGON[]: FLIP RanGe ON */ +/* Opcode range: 0x81 */ +/* Stack: uint32 uint32 --> */ +/* */ +static +void Ins_FLIPRGON( INS_ARG ) { + FT_UShort I, K, L; + + + K = (FT_UShort)args[1]; + L = (FT_UShort)args[0]; + + if ( BOUNDS( K, CUR.pts.n_points ) || + BOUNDS( L, CUR.pts.n_points ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return; + } + + for ( I = L; I <= K; I++ ) + CUR.pts.tags[I] |= FT_Curve_Tag_On; +} + + +/*************************************************************************/ +/* */ +/* FLIPRGOFF: FLIP RanGe OFF */ +/* Opcode range: 0x82 */ +/* Stack: uint32 uint32 --> */ +/* */ +static +void Ins_FLIPRGOFF( INS_ARG ) { + FT_UShort I, K, L; + + + K = (FT_UShort)args[1]; + L = (FT_UShort)args[0]; + + if ( BOUNDS( K, CUR.pts.n_points ) || + BOUNDS( L, CUR.pts.n_points ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return; + } + + for ( I = L; I <= K; I++ ) + CUR.pts.tags[I] &= ~FT_Curve_Tag_On; +} + + +static +FT_Bool Compute_Point_Displacement( EXEC_OP_ FT_F26Dot6* x, + FT_F26Dot6* y, + TT_GlyphZone* zone, + FT_UShort* refp ) { + TT_GlyphZone zp; + FT_UShort p; + FT_F26Dot6 d; + + + if ( CUR.opcode & 1 ) { + zp = CUR.zp0; + p = CUR.GS.rp1; + } else + { + zp = CUR.zp1; + p = CUR.GS.rp2; + } + + if ( BOUNDS( p, zp.n_points ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return FAILURE; + } + + *zone = zp; + *refp = p; + + d = CUR_Func_project( zp.cur + p, zp.org + p ); + +#ifdef NO_APPLE_PATENT + + *x = TT_MULDIV( d, CUR.GS.freeVector.x, 0x4000 ); + *y = TT_MULDIV( d, CUR.GS.freeVector.y, 0x4000 ); + +#else + + *x = TT_MULDIV( d, + (FT_Long)CUR.GS.freeVector.x * 0x10000L, + CUR.F_dot_P ); + *y = TT_MULDIV( d, + (FT_Long)CUR.GS.freeVector.y * 0x10000L, + CUR.F_dot_P ); + +#endif /* NO_APPLE_PATENT */ + + return SUCCESS; +} + + +static +void Move_Zp2_Point( EXEC_OP_ FT_UShort point, + FT_F26Dot6 dx, + FT_F26Dot6 dy, + FT_Bool touch ) { + if ( CUR.GS.freeVector.x != 0 ) { + CUR.zp2.cur[point].x += dx; + if ( touch ) { + CUR.zp2.tags[point] |= FT_Curve_Tag_Touch_X; + } + } + + if ( CUR.GS.freeVector.y != 0 ) { + CUR.zp2.cur[point].y += dy; + if ( touch ) { + CUR.zp2.tags[point] |= FT_Curve_Tag_Touch_Y; + } + } +} + + +/*************************************************************************/ +/* */ +/* SHP[a]: SHift Point by the last point */ +/* Opcode range: 0x32-0x33 */ +/* Stack: uint32... --> */ +/* */ +static +void Ins_SHP( INS_ARG ) { + TT_GlyphZone zp; + FT_UShort refp; + + FT_F26Dot6 dx, + dy; + FT_UShort point; + + FT_UNUSED_ARG; + + + if ( CUR.top < CUR.GS.loop ) { + CUR.error = TT_Err_Invalid_Reference; + return; + } + + if ( COMPUTE_Point_Displacement( &dx, &dy, &zp, &refp ) ) { + return; + } + + while ( CUR.GS.loop > 0 ) + { + CUR.args--; + point = (FT_UShort)CUR.stack[CUR.args]; + + if ( BOUNDS( point, CUR.zp2.n_points ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + return; + } + } else { + /* XXX: UNDOCUMENTED! SHP touches the points */ + MOVE_Zp2_Point( point, dx, dy, TRUE ); + } + + CUR.GS.loop--; + } + + CUR.GS.loop = 1; + CUR.new_top = CUR.args; +} + + +/*************************************************************************/ +/* */ +/* SHC[a]: SHift Contour */ +/* Opcode range: 0x34-35 */ +/* Stack: uint32 --> */ +/* */ +static +void Ins_SHC( INS_ARG ) { + TT_GlyphZone zp; + FT_UShort refp; + FT_F26Dot6 dx, + dy; + + FT_Short contour; + FT_UShort first_point, last_point, i; + + + contour = (FT_UShort)args[0]; + + if ( BOUNDS( contour, CUR.pts.n_contours ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return; + } + + if ( COMPUTE_Point_Displacement( &dx, &dy, &zp, &refp ) ) { + return; + } + + if ( contour == 0 ) { + first_point = 0; + } else { + first_point = CUR.pts.contours[contour - 1] + 1; + } + + last_point = CUR.pts.contours[contour]; + + /* XXX: this is probably wrong... at least it prevents memory */ + /* corruption when zp2 is the twilight zone */ + if ( last_point > CUR.zp2.n_points ) { + if ( CUR.zp2.n_points > 0 ) { + last_point = CUR.zp2.n_points - 1; + } else { + last_point = 0; + } + } + + /* XXX: UNDOCUMENTED! SHC doesn't touch the points */ + for ( i = first_point; i <= last_point; i++ ) + { + if ( zp.cur != CUR.zp2.cur || refp != i ) { + MOVE_Zp2_Point( i, dx, dy, FALSE ); + } + } +} + + +/*************************************************************************/ +/* */ +/* SHZ[a]: SHift Zone */ +/* Opcode range: 0x36-37 */ +/* Stack: uint32 --> */ +/* */ +static +void Ins_SHZ( INS_ARG ) { + TT_GlyphZone zp; + FT_UShort refp; + FT_F26Dot6 dx, + dy; + + FT_UShort last_point, i; + + + if ( BOUNDS( args[0], 2 ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return; + } + + if ( COMPUTE_Point_Displacement( &dx, &dy, &zp, &refp ) ) { + return; + } + + if ( CUR.zp2.n_points > 0 ) { + last_point = CUR.zp2.n_points - 1; + } else { + last_point = 0; + } + + /* XXX: UNDOCUMENTED! SHZ doesn't touch the points */ + for ( i = 0; i <= last_point; i++ ) + { + if ( zp.cur != CUR.zp2.cur || refp != i ) { + MOVE_Zp2_Point( i, dx, dy, FALSE ); + } + } +} + + +/*************************************************************************/ +/* */ +/* SHPIX[]: SHift points by a PIXel amount */ +/* Opcode range: 0x38 */ +/* Stack: f26.6 uint32... --> */ +/* */ +static +void Ins_SHPIX( INS_ARG ) { + FT_F26Dot6 dx, dy; + FT_UShort point; + + + if ( CUR.top < CUR.GS.loop + 1 ) { + CUR.error = TT_Err_Invalid_Reference; + return; + } + + dx = TT_MULDIV( args[0], + (FT_Long)CUR.GS.freeVector.x, + 0x4000 ); + dy = TT_MULDIV( args[0], + (FT_Long)CUR.GS.freeVector.y, + 0x4000 ); + + while ( CUR.GS.loop > 0 ) + { + CUR.args--; + + point = (FT_UShort)CUR.stack[CUR.args]; + + if ( BOUNDS( point, CUR.zp2.n_points ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + return; + } + } else { + MOVE_Zp2_Point( point, dx, dy, TRUE ); + } + + CUR.GS.loop--; + } + + CUR.GS.loop = 1; + CUR.new_top = CUR.args; +} + + +/*************************************************************************/ +/* */ +/* MSIRP[a]: Move Stack Indirect Relative Position */ +/* Opcode range: 0x3A-0x3B */ +/* Stack: f26.6 uint32 --> */ +/* */ +static +void Ins_MSIRP( INS_ARG ) { + FT_UShort point; + FT_F26Dot6 distance; + + + point = (FT_UShort)args[0]; + + if ( BOUNDS( point, CUR.zp1.n_points ) || + BOUNDS( CUR.GS.rp0, CUR.zp0.n_points ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return; + } + + /* XXX: UNDOCUMENTED! behaviour */ + if ( CUR.GS.gep0 == 0 ) { /* if in twilight zone */ + CUR.zp1.org[point] = CUR.zp0.org[CUR.GS.rp0]; + CUR.zp1.cur[point] = CUR.zp1.org[point]; + } + + distance = CUR_Func_project( CUR.zp1.cur + point, + CUR.zp0.cur + CUR.GS.rp0 ); + + CUR_Func_move( &CUR.zp1, point, args[1] - distance ); + + CUR.GS.rp1 = CUR.GS.rp0; + CUR.GS.rp2 = point; + + if ( ( CUR.opcode & 1 ) != 0 ) { + CUR.GS.rp0 = point; + } +} + + +/*************************************************************************/ +/* */ +/* MDAP[a]: Move Direct Absolute Point */ +/* Opcode range: 0x2E-0x2F */ +/* Stack: uint32 --> */ +/* */ +static +void Ins_MDAP( INS_ARG ) { + FT_UShort point; + FT_F26Dot6 cur_dist, + distance; + + + point = (FT_UShort)args[0]; + + if ( BOUNDS( point, CUR.zp0.n_points ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return; + } + + /* XXX: Is there some undocumented feature while in the */ + /* twilight zone? ? */ + if ( ( CUR.opcode & 1 ) != 0 ) { + cur_dist = CUR_Func_project( CUR.zp0.cur + point, NULL_Vector ); + distance = CUR_Func_round( cur_dist, + CUR.tt_metrics.compensations[0] ) - cur_dist; + } else { + distance = 0; + } + + CUR_Func_move( &CUR.zp0, point, distance ); + + CUR.GS.rp0 = point; + CUR.GS.rp1 = point; +} + + +/*************************************************************************/ +/* */ +/* MIAP[a]: Move Indirect Absolute Point */ +/* Opcode range: 0x3E-0x3F */ +/* Stack: uint32 uint32 --> */ +/* */ +static +void Ins_MIAP( INS_ARG ) { + FT_ULong cvtEntry; + FT_UShort point; + FT_F26Dot6 distance, + org_dist; + + + cvtEntry = (FT_ULong)args[1]; + point = (FT_UShort)args[0]; + + if ( BOUNDS( point, CUR.zp0.n_points ) || + BOUNDS( cvtEntry, CUR.cvtSize ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return; + } + + /* UNDOCUMENTED! */ + /* */ + /* The behaviour of an MIAP instruction is quite */ + /* different when used in the twilight zone. */ + /* */ + /* First, no control value cutin test is performed */ + /* as it would fail anyway. Second, the original */ + /* point, i.e. (org_x,org_y) of zp0.point, is set */ + /* to the absolute, unrounded distance found in */ + /* the CVT. */ + /* */ + /* This is used in the CVT programs of the Microsoft */ + /* fonts Arial, Times, etc., in order to re-adjust */ + /* some key font heights. It allows the use of the */ + /* IP instruction in the twilight zone, which */ + /* otherwise would be `illegal' according to the */ + /* specification. */ + /* */ + /* We implement it with a special sequence for the */ + /* twilight zone. This is a bad hack, but it seems */ + /* to work. */ + + distance = CUR_Func_read_cvt( cvtEntry ); + + if ( CUR.GS.gep0 == 0 ) { /* If in twilight zone */ + CUR.zp0.org[point].x = TT_MULDIV( CUR.GS.freeVector.x, + distance, 0x4000 ); + CUR.zp0.org[point].y = TT_MULDIV( CUR.GS.freeVector.y, + distance, 0x4000 ); + CUR.zp0.cur[point] = CUR.zp0.org[point]; + } + + org_dist = CUR_Func_project( CUR.zp0.cur + point, NULL_Vector ); + + if ( ( CUR.opcode & 1 ) != 0 ) { /* rounding and control cutin flag */ + if ( ABS( distance - org_dist ) > CUR.GS.control_value_cutin ) { + distance = org_dist; + } + + distance = CUR_Func_round( distance, CUR.tt_metrics.compensations[0] ); + } + + CUR_Func_move( &CUR.zp0, point, distance - org_dist ); + + CUR.GS.rp0 = point; + CUR.GS.rp1 = point; +} + + +/*************************************************************************/ +/* */ +/* MDRP[abcde]: Move Direct Relative Point */ +/* Opcode range: 0xC0-0xDF */ +/* Stack: uint32 --> */ +/* */ +static +void Ins_MDRP( INS_ARG ) { + FT_UShort point; + FT_F26Dot6 org_dist, distance; + + + point = (FT_UShort)args[0]; + + if ( BOUNDS( point, CUR.zp1.n_points ) || + BOUNDS( CUR.GS.rp0, CUR.zp0.n_points ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return; + } + + /* XXX: Is there some undocumented feature while in the */ + /* twilight zone? */ + + org_dist = CUR_Func_dualproj( CUR.zp1.org + point, + CUR.zp0.org + CUR.GS.rp0 ); + + /* single width cutin test */ + + if ( ABS( org_dist ) < CUR.GS.single_width_cutin ) { + if ( org_dist >= 0 ) { + org_dist = CUR.GS.single_width_value; + } else { + org_dist = -CUR.GS.single_width_value; + } + } + + /* round flag */ + + if ( ( CUR.opcode & 4 ) != 0 ) { + distance = CUR_Func_round( + org_dist, + CUR.tt_metrics.compensations[CUR.opcode & 3] ); + } else { + distance = ROUND_None( + org_dist, + CUR.tt_metrics.compensations[CUR.opcode & 3] ); + } + + /* minimum distance flag */ + + if ( ( CUR.opcode & 8 ) != 0 ) { + if ( org_dist >= 0 ) { + if ( distance < CUR.GS.minimum_distance ) { + distance = CUR.GS.minimum_distance; + } + } else + { + if ( distance > -CUR.GS.minimum_distance ) { + distance = -CUR.GS.minimum_distance; + } + } + } + + /* now move the point */ + + org_dist = CUR_Func_project( CUR.zp1.cur + point, + CUR.zp0.cur + CUR.GS.rp0 ); + + CUR_Func_move( &CUR.zp1, point, distance - org_dist ); + + CUR.GS.rp1 = CUR.GS.rp0; + CUR.GS.rp2 = point; + + if ( ( CUR.opcode & 16 ) != 0 ) { + CUR.GS.rp0 = point; + } +} + + +/*************************************************************************/ +/* */ +/* MIRP[abcde]: Move Indirect Relative Point */ +/* Opcode range: 0xE0-0xFF */ +/* Stack: int32? uint32 --> */ +/* */ +static +void Ins_MIRP( INS_ARG ) { + FT_UShort point; + FT_ULong cvtEntry; + + FT_F26Dot6 cvt_dist, + distance, + cur_dist, + org_dist; + + + point = (FT_UShort)args[0]; + cvtEntry = (FT_ULong)( args[1] + 1 ); + + /* XXX: UNDOCUMENTED! cvt[-1] = 0 always */ + + if ( BOUNDS( point, CUR.zp1.n_points ) || + BOUNDS( cvtEntry, CUR.cvtSize + 1 ) || + BOUNDS( CUR.GS.rp0, CUR.zp0.n_points ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return; + } + + if ( !cvtEntry ) { + cvt_dist = 0; + } else { + cvt_dist = CUR_Func_read_cvt( cvtEntry - 1 ); + } + + /* single width test */ + + if ( ABS( cvt_dist ) < CUR.GS.single_width_cutin ) { + if ( cvt_dist >= 0 ) { + cvt_dist = CUR.GS.single_width_value; + } else { + cvt_dist = -CUR.GS.single_width_value; + } + } + + /* XXX: UNDOCUMENTED! -- twilight zone */ + + if ( CUR.GS.gep1 == 0 ) { + CUR.zp1.org[point].x = CUR.zp0.org[CUR.GS.rp0].x + + TT_MULDIV( cvt_dist, + CUR.GS.freeVector.x, + 0x4000 ); + + CUR.zp1.org[point].y = CUR.zp0.org[CUR.GS.rp0].y + + TT_MULDIV( cvt_dist, + CUR.GS.freeVector.y, + 0x4000 ); + + CUR.zp1.cur[point] = CUR.zp1.org[point]; + } + + org_dist = CUR_Func_dualproj( CUR.zp1.org + point, + CUR.zp0.org + CUR.GS.rp0 ); + + cur_dist = CUR_Func_project( CUR.zp1.cur + point, + CUR.zp0.cur + CUR.GS.rp0 ); + + /* auto-flip test */ + + if ( CUR.GS.auto_flip ) { + if ( ( org_dist ^ cvt_dist ) < 0 ) { + cvt_dist = -cvt_dist; + } + } + + /* control value cutin and round */ + + if ( ( CUR.opcode & 4 ) != 0 ) { + /* XXX: UNDOCUMENTED! Only perform cut-in test when both points */ + /* refer to the same zone. */ + + if ( CUR.GS.gep0 == CUR.GS.gep1 ) { + if ( ABS( cvt_dist - org_dist ) >= CUR.GS.control_value_cutin ) { + cvt_dist = org_dist; + } + } + + distance = CUR_Func_round( + cvt_dist, + CUR.tt_metrics.compensations[CUR.opcode & 3] ); + } else { + distance = ROUND_None( + cvt_dist, + CUR.tt_metrics.compensations[CUR.opcode & 3] ); + } + + /* minimum distance test */ + + if ( ( CUR.opcode & 8 ) != 0 ) { + if ( org_dist >= 0 ) { + if ( distance < CUR.GS.minimum_distance ) { + distance = CUR.GS.minimum_distance; + } + } else + { + if ( distance > -CUR.GS.minimum_distance ) { + distance = -CUR.GS.minimum_distance; + } + } + } + + CUR_Func_move( &CUR.zp1, point, distance - cur_dist ); + + CUR.GS.rp1 = CUR.GS.rp0; + + if ( ( CUR.opcode & 16 ) != 0 ) { + CUR.GS.rp0 = point; + } + + /* XXX: UNDOCUMENTED! */ + + CUR.GS.rp2 = point; +} + + +/*************************************************************************/ +/* */ +/* ALIGNRP[]: ALIGN Relative Point */ +/* Opcode range: 0x3C */ +/* Stack: uint32 uint32... --> */ +/* */ +static +void Ins_ALIGNRP( INS_ARG ) { + FT_UShort point; + FT_F26Dot6 distance; + + FT_UNUSED_ARG; + + + if ( CUR.top < CUR.GS.loop || + BOUNDS( CUR.GS.rp0, CUR.zp0.n_points ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return; + } + + while ( CUR.GS.loop > 0 ) + { + CUR.args--; + + point = (FT_UShort)CUR.stack[CUR.args]; + + if ( BOUNDS( point, CUR.zp1.n_points ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + return; + } + } else + { + distance = CUR_Func_project( CUR.zp1.cur + point, + CUR.zp0.cur + CUR.GS.rp0 ); + + CUR_Func_move( &CUR.zp1, point, -distance ); + } + + CUR.GS.loop--; + } + + CUR.GS.loop = 1; + CUR.new_top = CUR.args; +} + + +/*************************************************************************/ +/* */ +/* ISECT[]: moves point to InterSECTion */ +/* Opcode range: 0x0F */ +/* Stack: 5 * uint32 --> */ +/* */ +static +void Ins_ISECT( INS_ARG ) { + FT_UShort point, + a0, a1, + b0, b1; + + FT_F26Dot6 discriminant; + + FT_F26Dot6 dx, dy, + dax, day, + dbx, dby; + + FT_F26Dot6 val; + + FT_Vector R; + + + point = (FT_UShort)args[0]; + + a0 = (FT_UShort)args[1]; + a1 = (FT_UShort)args[2]; + b0 = (FT_UShort)args[3]; + b1 = (FT_UShort)args[4]; + + if ( BOUNDS( b0, CUR.zp0.n_points ) || + BOUNDS( b1, CUR.zp0.n_points ) || + BOUNDS( a0, CUR.zp1.n_points ) || + BOUNDS( a1, CUR.zp1.n_points ) || + BOUNDS( point, CUR.zp2.n_points ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return; + } + + dbx = CUR.zp0.cur[b1].x - CUR.zp0.cur[b0].x; + dby = CUR.zp0.cur[b1].y - CUR.zp0.cur[b0].y; + + dax = CUR.zp1.cur[a1].x - CUR.zp1.cur[a0].x; + day = CUR.zp1.cur[a1].y - CUR.zp1.cur[a0].y; + + dx = CUR.zp0.cur[b0].x - CUR.zp1.cur[a0].x; + dy = CUR.zp0.cur[b0].y - CUR.zp1.cur[a0].y; + + CUR.zp2.tags[point] |= FT_Curve_Tag_Touch_Both; + + discriminant = TT_MULDIV( dax, -dby, 0x40 ) + + TT_MULDIV( day, dbx, 0x40 ); + + if ( ABS( discriminant ) >= 0x40 ) { + val = TT_MULDIV( dx, -dby, 0x40 ) + TT_MULDIV( dy, dbx, 0x40 ); + + R.x = TT_MULDIV( val, dax, discriminant ); + R.y = TT_MULDIV( val, day, discriminant ); + + CUR.zp2.cur[point].x = CUR.zp1.cur[a0].x + R.x; + CUR.zp2.cur[point].y = CUR.zp1.cur[a0].y + R.y; + } else + { + /* else, take the middle of the middles of A and B */ + + CUR.zp2.cur[point].x = ( CUR.zp1.cur[a0].x + + CUR.zp1.cur[a1].x + + CUR.zp0.cur[b0].x + + CUR.zp0.cur[b1].x ) / 4; + CUR.zp2.cur[point].y = ( CUR.zp1.cur[a0].y + + CUR.zp1.cur[a1].y + + CUR.zp0.cur[b0].y + + CUR.zp0.cur[b1].y ) / 4; + } +} + + +/*************************************************************************/ +/* */ +/* ALIGNPTS[]: ALIGN PoinTS */ +/* Opcode range: 0x27 */ +/* Stack: uint32 uint32 --> */ +/* */ +static +void Ins_ALIGNPTS( INS_ARG ) { + FT_UShort p1, p2; + FT_F26Dot6 distance; + + + p1 = (FT_UShort)args[0]; + p2 = (FT_UShort)args[1]; + + if ( BOUNDS( args[0], CUR.zp1.n_points ) || + BOUNDS( args[1], CUR.zp0.n_points ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return; + } + + distance = CUR_Func_project( CUR.zp0.cur + p2, + CUR.zp1.cur + p1 ) / 2; + + CUR_Func_move( &CUR.zp1, p1, distance ); + CUR_Func_move( &CUR.zp0, p2, -distance ); +} + + +/*************************************************************************/ +/* */ +/* IP[]: Interpolate Point */ +/* Opcode range: 0x39 */ +/* Stack: uint32... --> */ +/* */ +static +void Ins_IP( INS_ARG ) { + FT_F26Dot6 org_a, org_b, org_x, + cur_a, cur_b, cur_x, + distance; + FT_UShort point; + + FT_UNUSED_ARG; + + + if ( CUR.top < CUR.GS.loop ) { + CUR.error = TT_Err_Invalid_Reference; + return; + } + + /* XXX: There are some glyphs in some braindead but popular */ + /* fonts out there (e.g. [aeu]grave in monotype.ttf) */ + /* calling IP[] with bad values of rp[12]. */ + /* Do something sane when this odd thing happens. */ + + if ( BOUNDS( CUR.GS.rp1, CUR.zp0.n_points ) || + BOUNDS( CUR.GS.rp2, CUR.zp1.n_points ) ) { + org_a = cur_a = 0; + org_b = cur_b = 0; + } else + { + org_a = CUR_Func_dualproj( CUR.zp0.org + CUR.GS.rp1, NULL_Vector ); + org_b = CUR_Func_dualproj( CUR.zp1.org + CUR.GS.rp2, NULL_Vector ); + + cur_a = CUR_Func_project( CUR.zp0.cur + CUR.GS.rp1, NULL_Vector ); + cur_b = CUR_Func_project( CUR.zp1.cur + CUR.GS.rp2, NULL_Vector ); + } + + while ( CUR.GS.loop > 0 ) + { + CUR.args--; + + point = (FT_UShort)CUR.stack[CUR.args]; + if ( BOUNDS( point, CUR.zp2.n_points ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + return; + } + } else + { + org_x = CUR_Func_dualproj( CUR.zp2.org + point, NULL_Vector ); + cur_x = CUR_Func_project( CUR.zp2.cur + point, NULL_Vector ); + + if ( ( org_a <= org_b && org_x <= org_a ) || + ( org_a > org_b && org_x >= org_a ) ) { + + distance = ( cur_a - org_a ) + ( org_x - cur_x ); + } else if ( ( org_a <= org_b && org_x >= org_b ) || + ( org_a > org_b && org_x < org_b ) ) { + + distance = ( cur_b - org_b ) + ( org_x - cur_x ); + } else { + /* note: it seems that rounding this value isn't a good */ + /* idea (cf. width of capital `S' in Times) */ + + distance = TT_MULDIV( cur_b - cur_a, + org_x - org_a, + org_b - org_a ) + ( cur_a - cur_x ); + } + + CUR_Func_move( &CUR.zp2, point, distance ); + } + + CUR.GS.loop--; + } + + CUR.GS.loop = 1; + CUR.new_top = CUR.args; +} + + +/*************************************************************************/ +/* */ +/* UTP[a]: UnTouch Point */ +/* Opcode range: 0x29 */ +/* Stack: uint32 --> */ +/* */ +static +void Ins_UTP( INS_ARG ) { + FT_UShort point; + FT_Byte mask; + + + point = (FT_UShort)args[0]; + + if ( BOUNDS( point, CUR.zp0.n_points ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + return; + } + + mask = 0xFF; + + if ( CUR.GS.freeVector.x != 0 ) { + mask &= ~FT_Curve_Tag_Touch_X; + } + + if ( CUR.GS.freeVector.y != 0 ) { + mask &= ~FT_Curve_Tag_Touch_Y; + } + + CUR.zp0.tags[point] &= mask; +} + + +/* Local variables for Ins_IUP: */ +struct LOC_Ins_IUP +{ + FT_Vector* orgs; /* original and current coordinate */ + FT_Vector* curs; /* arrays */ +}; + + +static +void Shift( FT_UInt p1, + FT_UInt p2, + FT_UInt p, + struct LOC_Ins_IUP* LINK ) { + FT_UInt i; + FT_F26Dot6 x; + + + x = LINK->curs[p].x - LINK->orgs[p].x; + + for ( i = p1; i < p; i++ ) + LINK->curs[i].x += x; + + for ( i = p + 1; i <= p2; i++ ) + LINK->curs[i].x += x; +} + + +static +void Interp( FT_UInt p1, + FT_UInt p2, + FT_UInt ref1, + FT_UInt ref2, + struct LOC_Ins_IUP* LINK ) { + FT_UInt i; + FT_F26Dot6 x, x1, x2, d1, d2; + + + if ( p1 > p2 ) { + return; + } + + x1 = LINK->orgs[ref1].x; + d1 = LINK->curs[ref1].x - LINK->orgs[ref1].x; + x2 = LINK->orgs[ref2].x; + d2 = LINK->curs[ref2].x - LINK->orgs[ref2].x; + + if ( x1 == x2 ) { + for ( i = p1; i <= p2; i++ ) + { + x = LINK->orgs[i].x; + + if ( x <= x1 ) { + x += d1; + } else { + x += d2; + } + + LINK->curs[i].x = x; + } + return; + } + + if ( x1 < x2 ) { + for ( i = p1; i <= p2; i++ ) + { + x = LINK->orgs[i].x; + + if ( x <= x1 ) { + x += d1; + } else + { + if ( x >= x2 ) { + x += d2; + } else { + x = LINK->curs[ref1].x + + TT_MULDIV( x - x1, + LINK->curs[ref2].x - LINK->curs[ref1].x, + x2 - x1 ); + } + } + LINK->curs[i].x = x; + } + return; + } + + /* x2 < x1 */ + + for ( i = p1; i <= p2; i++ ) + { + x = LINK->orgs[i].x; + if ( x <= x2 ) { + x += d2; + } else + { + if ( x >= x1 ) { + x += d1; + } else { + x = LINK->curs[ref1].x + + TT_MULDIV( x - x1, + LINK->curs[ref2].x - LINK->curs[ref1].x, + x2 - x1 ); + } + } + LINK->curs[i].x = x; + } +} + + +/*************************************************************************/ +/* */ +/* IUP[a]: Interpolate Untouched Points */ +/* Opcode range: 0x30-0x31 */ +/* Stack: --> */ +/* */ +static +void Ins_IUP( INS_ARG ) { + struct LOC_Ins_IUP V; + FT_Byte mask; + + FT_UInt first_point; /* first point of contour */ + FT_UInt end_point; /* end point (last+1) of contour */ + + FT_UInt first_touched; /* first touched point in contour */ + FT_UInt cur_touched; /* current touched point in contour */ + + FT_UInt point; /* current point */ + FT_Short contour; /* current contour */ + + FT_UNUSED_ARG; + + + if ( CUR.opcode & 1 ) { + mask = FT_Curve_Tag_Touch_X; + V.orgs = CUR.pts.org; + V.curs = CUR.pts.cur; + } else + { + mask = FT_Curve_Tag_Touch_Y; + V.orgs = ( FT_Vector* )( (FT_Pos*)CUR.pts.org + 1 ); + V.curs = ( FT_Vector* )( (FT_Pos*)CUR.pts.cur + 1 ); + } + + contour = 0; + point = 0; + + do + { + end_point = CUR.pts.contours[contour]; + first_point = point; + + while ( point <= end_point && ( CUR.pts.tags[point] & mask ) == 0 ) + point++; + + if ( point <= end_point ) { + first_touched = point; + cur_touched = point; + + point++; + + while ( point <= end_point ) + { + if ( ( CUR.pts.tags[point] & mask ) != 0 ) { + if ( point > 0 ) { + Interp( cur_touched + 1, + point - 1, + cur_touched, + point, + &V ); + } + cur_touched = point; + } + + point++; + } + + if ( cur_touched == first_touched ) { + Shift( first_point, end_point, cur_touched, &V ); + } else + { + Interp( (FT_UShort)( cur_touched + 1 ), + end_point, + cur_touched, + first_touched, + &V ); + + if ( first_touched > 0 ) { + Interp( first_point, + first_touched - 1, + cur_touched, + first_touched, + &V ); + } + } + } + contour++; + } while ( contour < CUR.pts.n_contours ); +} + + +/*************************************************************************/ +/* */ +/* DELTAPn[]: DELTA exceptions P1, P2, P3 */ +/* Opcode range: 0x5D,0x71,0x72 */ +/* Stack: uint32 (2 * uint32)... --> */ +/* */ +static +void Ins_DELTAP( INS_ARG ) { + FT_ULong k, nump; + FT_UShort A; + FT_ULong C; + FT_Long B; + + + nump = (FT_ULong)args[0]; /* some points theoretically may occur more + than once, thus UShort isn't enough */ + + for ( k = 1; k <= nump; k++ ) + { + if ( CUR.args < 2 ) { + CUR.error = TT_Err_Too_Few_Arguments; + return; + } + + CUR.args -= 2; + + A = (FT_UShort)CUR.stack[CUR.args + 1]; + B = CUR.stack[CUR.args]; + + /* XXX: Because some popular fonts contain some invalid DeltaP */ + /* instructions, we simply ignore them when the stacked */ + /* point reference is off limit, rather than returning an */ + /* error. As a delta instruction doesn't change a glyph */ + /* in great ways, this shouldn't be a problem. */ + + if ( !BOUNDS( A, CUR.zp0.n_points ) ) { + C = ( (FT_ULong)B & 0xF0 ) >> 4; + + switch ( CUR.opcode ) + { + case 0x5D: + break; + + case 0x71: + C += 16; + break; + + case 0x72: + C += 32; + break; + } + + C += CUR.GS.delta_base; + + if ( CURRENT_Ppem() == (FT_Long)C ) { + B = ( (FT_ULong)B & 0xF ) - 8; + if ( B >= 0 ) { + B++; + } + B = B * 64 / ( 1L << CUR.GS.delta_shift ); + + CUR_Func_move( &CUR.zp0, A, B ); + } + } else + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + } + } + + CUR.new_top = CUR.args; +} + + +/*************************************************************************/ +/* */ +/* DELTACn[]: DELTA exceptions C1, C2, C3 */ +/* Opcode range: 0x73,0x74,0x75 */ +/* Stack: uint32 (2 * uint32)... --> */ +/* */ +static +void Ins_DELTAC( INS_ARG ) { + FT_ULong nump, k; + FT_ULong A, C; + FT_Long B; + + + nump = (FT_ULong)args[0]; + + for ( k = 1; k <= nump; k++ ) + { + if ( CUR.args < 2 ) { + CUR.error = TT_Err_Too_Few_Arguments; + return; + } + + CUR.args -= 2; + + A = (FT_ULong)CUR.stack[CUR.args + 1]; + B = CUR.stack[CUR.args]; + + if ( BOUNDS( A, CUR.cvtSize ) ) { + if ( CUR.pedantic_hinting ) { + CUR.error = TT_Err_Invalid_Reference; + return; + } + } else + { + C = ( (FT_ULong)B & 0xF0 ) >> 4; + + switch ( CUR.opcode ) + { + case 0x73: + break; + + case 0x74: + C += 16; + break; + + case 0x75: + C += 32; + break; + } + + C += CUR.GS.delta_base; + + if ( CURRENT_Ppem() == (FT_Long)C ) { + B = ( (FT_ULong)B & 0xF ) - 8; + if ( B >= 0 ) { + B++; + } + B = B * 64 / ( 1L << CUR.GS.delta_shift ); + + CUR_Func_move_cvt( A, B ); + } + } + } + + CUR.new_top = CUR.args; +} + + +/*************************************************************************/ +/* */ +/* MISC. INSTRUCTIONS */ +/* */ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* GETINFO[]: GET INFOrmation */ +/* Opcode range: 0x88 */ +/* Stack: uint32 --> uint32 */ +/* */ +/* XXX: According to Apple specs, bits 1 & 2 of the argument ought to be */ +/* consulted before rotated/stretched info is returned. */ +static +void Ins_GETINFO( INS_ARG ) { + FT_Long K; + + + K = 0; + + /* We return then Windows 3.1 version number */ + /* for the font scaler */ + if ( ( args[0] & 1 ) != 0 ) { + K = 3; + } + + /* Has the glyph been rotated ? */ + if ( CUR.tt_metrics.rotated ) { + K |= 0x80; + } + + /* Has the glyph been stretched ? */ + if ( CUR.tt_metrics.stretched ) { + K |= 0x100; + } + + args[0] = K; +} + + +static +void Ins_UNKNOWN( INS_ARG ) { + TT_DefRecord* def = CUR.IDefs; + TT_DefRecord* limit = def + CUR.numIDefs; + + FT_UNUSED_ARG; + + + for ( ; def < limit; def++ ) + { + if ( def->opc == CUR.opcode && def->active ) { + TT_CallRec* call; + + + if ( CUR.callTop >= CUR.callSize ) { + CUR.error = TT_Err_Stack_Overflow; + return; + } + + call = CUR.callStack + CUR.callTop++; + + call->Caller_Range = CUR.curRange; + call->Caller_IP = CUR.IP + 1; + call->Cur_Count = 1; + call->Cur_Restart = def->start; + + INS_Goto_CodeRange( def->range, def->start ); + + CUR.step_ins = FALSE; + return; + } + } + + CUR.error = TT_Err_Invalid_Opcode; +} + + +#ifndef TT_CONFIG_OPTION_INTERPRETER_SWITCH + + +static +TInstruction_Function Instruct_Dispatch[256] = +{ + /* Opcodes are gathered in groups of 16. */ + /* Please keep the spaces as they are. */ + + /* SVTCA y */ Ins_SVTCA, + /* SVTCA x */ Ins_SVTCA, + /* SPvTCA y */ Ins_SPVTCA, + /* SPvTCA x */ Ins_SPVTCA, + /* SFvTCA y */ Ins_SFVTCA, + /* SFvTCA x */ Ins_SFVTCA, + /* SPvTL // */ Ins_SPVTL, + /* SPvTL + */ Ins_SPVTL, + /* SFvTL // */ Ins_SFVTL, + /* SFvTL + */ Ins_SFVTL, + /* SPvFS */ Ins_SPVFS, + /* SFvFS */ Ins_SFVFS, + /* GPV */ Ins_GPV, + /* GFV */ Ins_GFV, + /* SFvTPv */ Ins_SFVTPV, + /* ISECT */ Ins_ISECT, + + /* SRP0 */ Ins_SRP0, + /* SRP1 */ Ins_SRP1, + /* SRP2 */ Ins_SRP2, + /* SZP0 */ Ins_SZP0, + /* SZP1 */ Ins_SZP1, + /* SZP2 */ Ins_SZP2, + /* SZPS */ Ins_SZPS, + /* SLOOP */ Ins_SLOOP, + /* RTG */ Ins_RTG, + /* RTHG */ Ins_RTHG, + /* SMD */ Ins_SMD, + /* ELSE */ Ins_ELSE, + /* JMPR */ Ins_JMPR, + /* SCvTCi */ Ins_SCVTCI, + /* SSwCi */ Ins_SSWCI, + /* SSW */ Ins_SSW, + + /* DUP */ Ins_DUP, + /* POP */ Ins_POP, + /* CLEAR */ Ins_CLEAR, + /* SWAP */ Ins_SWAP, + /* DEPTH */ Ins_DEPTH, + /* CINDEX */ Ins_CINDEX, + /* MINDEX */ Ins_MINDEX, + /* AlignPTS */ Ins_ALIGNPTS, + /* INS_0x28 */ Ins_UNKNOWN, + /* UTP */ Ins_UTP, + /* LOOPCALL */ Ins_LOOPCALL, + /* CALL */ Ins_CALL, + /* FDEF */ Ins_FDEF, + /* ENDF */ Ins_ENDF, + /* MDAP[0] */ Ins_MDAP, + /* MDAP[1] */ Ins_MDAP, + + /* IUP[0] */ Ins_IUP, + /* IUP[1] */ Ins_IUP, + /* SHP[0] */ Ins_SHP, + /* SHP[1] */ Ins_SHP, + /* SHC[0] */ Ins_SHC, + /* SHC[1] */ Ins_SHC, + /* SHZ[0] */ Ins_SHZ, + /* SHZ[1] */ Ins_SHZ, + /* SHPIX */ Ins_SHPIX, + /* IP */ Ins_IP, + /* MSIRP[0] */ Ins_MSIRP, + /* MSIRP[1] */ Ins_MSIRP, + /* AlignRP */ Ins_ALIGNRP, + /* RTDG */ Ins_RTDG, + /* MIAP[0] */ Ins_MIAP, + /* MIAP[1] */ Ins_MIAP, + + /* NPushB */ Ins_NPUSHB, + /* NPushW */ Ins_NPUSHW, + /* WS */ Ins_WS, + /* RS */ Ins_RS, + /* WCvtP */ Ins_WCVTP, + /* RCvt */ Ins_RCVT, + /* GC[0] */ Ins_GC, + /* GC[1] */ Ins_GC, + /* SCFS */ Ins_SCFS, + /* MD[0] */ Ins_MD, + /* MD[1] */ Ins_MD, + /* MPPEM */ Ins_MPPEM, + /* MPS */ Ins_MPS, + /* FlipON */ Ins_FLIPON, + /* FlipOFF */ Ins_FLIPOFF, + /* DEBUG */ Ins_DEBUG, + + /* LT */ Ins_LT, + /* LTEQ */ Ins_LTEQ, + /* GT */ Ins_GT, + /* GTEQ */ Ins_GTEQ, + /* EQ */ Ins_EQ, + /* NEQ */ Ins_NEQ, + /* ODD */ Ins_ODD, + /* EVEN */ Ins_EVEN, + /* IF */ Ins_IF, + /* EIF */ Ins_EIF, + /* AND */ Ins_AND, + /* OR */ Ins_OR, + /* NOT */ Ins_NOT, + /* DeltaP1 */ Ins_DELTAP, + /* SDB */ Ins_SDB, + /* SDS */ Ins_SDS, + + /* ADD */ Ins_ADD, + /* SUB */ Ins_SUB, + /* DIV */ Ins_DIV, + /* MUL */ Ins_MUL, + /* ABS */ Ins_ABS, + /* NEG */ Ins_NEG, + /* FLOOR */ Ins_FLOOR, + /* CEILING */ Ins_CEILING, + /* ROUND[0] */ Ins_ROUND, + /* ROUND[1] */ Ins_ROUND, + /* ROUND[2] */ Ins_ROUND, + /* ROUND[3] */ Ins_ROUND, + /* NROUND[0] */ Ins_NROUND, + /* NROUND[1] */ Ins_NROUND, + /* NROUND[2] */ Ins_NROUND, + /* NROUND[3] */ Ins_NROUND, + + /* WCvtF */ Ins_WCVTF, + /* DeltaP2 */ Ins_DELTAP, + /* DeltaP3 */ Ins_DELTAP, + /* DeltaCn[0] */ Ins_DELTAC, + /* DeltaCn[1] */ Ins_DELTAC, + /* DeltaCn[2] */ Ins_DELTAC, + /* SROUND */ Ins_SROUND, + /* S45Round */ Ins_S45ROUND, + /* JROT */ Ins_JROT, + /* JROF */ Ins_JROF, + /* ROFF */ Ins_ROFF, + /* INS_0x7B */ Ins_UNKNOWN, + /* RUTG */ Ins_RUTG, + /* RDTG */ Ins_RDTG, + /* SANGW */ Ins_SANGW, + /* AA */ Ins_AA, + + /* FlipPT */ Ins_FLIPPT, + /* FlipRgON */ Ins_FLIPRGON, + /* FlipRgOFF */ Ins_FLIPRGOFF, + /* INS_0x83 */ Ins_UNKNOWN, + /* INS_0x84 */ Ins_UNKNOWN, + /* ScanCTRL */ Ins_SCANCTRL, + /* SDPVTL[0] */ Ins_SDPVTL, + /* SDPVTL[1] */ Ins_SDPVTL, + /* GetINFO */ Ins_GETINFO, + /* IDEF */ Ins_IDEF, + /* ROLL */ Ins_ROLL, + /* MAX */ Ins_MAX, + /* MIN */ Ins_MIN, + /* ScanTYPE */ Ins_SCANTYPE, + /* InstCTRL */ Ins_INSTCTRL, + /* INS_0x8F */ Ins_UNKNOWN, + + /* INS_0x90 */ Ins_UNKNOWN, + /* INS_0x91 */ Ins_UNKNOWN, + /* INS_0x92 */ Ins_UNKNOWN, + /* INS_0x93 */ Ins_UNKNOWN, + /* INS_0x94 */ Ins_UNKNOWN, + /* INS_0x95 */ Ins_UNKNOWN, + /* INS_0x96 */ Ins_UNKNOWN, + /* INS_0x97 */ Ins_UNKNOWN, + /* INS_0x98 */ Ins_UNKNOWN, + /* INS_0x99 */ Ins_UNKNOWN, + /* INS_0x9A */ Ins_UNKNOWN, + /* INS_0x9B */ Ins_UNKNOWN, + /* INS_0x9C */ Ins_UNKNOWN, + /* INS_0x9D */ Ins_UNKNOWN, + /* INS_0x9E */ Ins_UNKNOWN, + /* INS_0x9F */ Ins_UNKNOWN, + + /* INS_0xA0 */ Ins_UNKNOWN, + /* INS_0xA1 */ Ins_UNKNOWN, + /* INS_0xA2 */ Ins_UNKNOWN, + /* INS_0xA3 */ Ins_UNKNOWN, + /* INS_0xA4 */ Ins_UNKNOWN, + /* INS_0xA5 */ Ins_UNKNOWN, + /* INS_0xA6 */ Ins_UNKNOWN, + /* INS_0xA7 */ Ins_UNKNOWN, + /* INS_0xA8 */ Ins_UNKNOWN, + /* INS_0xA9 */ Ins_UNKNOWN, + /* INS_0xAA */ Ins_UNKNOWN, + /* INS_0xAB */ Ins_UNKNOWN, + /* INS_0xAC */ Ins_UNKNOWN, + /* INS_0xAD */ Ins_UNKNOWN, + /* INS_0xAE */ Ins_UNKNOWN, + /* INS_0xAF */ Ins_UNKNOWN, + + /* PushB[0] */ Ins_PUSHB, + /* PushB[1] */ Ins_PUSHB, + /* PushB[2] */ Ins_PUSHB, + /* PushB[3] */ Ins_PUSHB, + /* PushB[4] */ Ins_PUSHB, + /* PushB[5] */ Ins_PUSHB, + /* PushB[6] */ Ins_PUSHB, + /* PushB[7] */ Ins_PUSHB, + /* PushW[0] */ Ins_PUSHW, + /* PushW[1] */ Ins_PUSHW, + /* PushW[2] */ Ins_PUSHW, + /* PushW[3] */ Ins_PUSHW, + /* PushW[4] */ Ins_PUSHW, + /* PushW[5] */ Ins_PUSHW, + /* PushW[6] */ Ins_PUSHW, + /* PushW[7] */ Ins_PUSHW, + + /* MDRP[00] */ Ins_MDRP, + /* MDRP[01] */ Ins_MDRP, + /* MDRP[02] */ Ins_MDRP, + /* MDRP[03] */ Ins_MDRP, + /* MDRP[04] */ Ins_MDRP, + /* MDRP[05] */ Ins_MDRP, + /* MDRP[06] */ Ins_MDRP, + /* MDRP[07] */ Ins_MDRP, + /* MDRP[08] */ Ins_MDRP, + /* MDRP[09] */ Ins_MDRP, + /* MDRP[10] */ Ins_MDRP, + /* MDRP[11] */ Ins_MDRP, + /* MDRP[12] */ Ins_MDRP, + /* MDRP[13] */ Ins_MDRP, + /* MDRP[14] */ Ins_MDRP, + /* MDRP[15] */ Ins_MDRP, + + /* MDRP[16] */ Ins_MDRP, + /* MDRP[17] */ Ins_MDRP, + /* MDRP[18] */ Ins_MDRP, + /* MDRP[19] */ Ins_MDRP, + /* MDRP[20] */ Ins_MDRP, + /* MDRP[21] */ Ins_MDRP, + /* MDRP[22] */ Ins_MDRP, + /* MDRP[23] */ Ins_MDRP, + /* MDRP[24] */ Ins_MDRP, + /* MDRP[25] */ Ins_MDRP, + /* MDRP[26] */ Ins_MDRP, + /* MDRP[27] */ Ins_MDRP, + /* MDRP[28] */ Ins_MDRP, + /* MDRP[29] */ Ins_MDRP, + /* MDRP[30] */ Ins_MDRP, + /* MDRP[31] */ Ins_MDRP, + + /* MIRP[00] */ Ins_MIRP, + /* MIRP[01] */ Ins_MIRP, + /* MIRP[02] */ Ins_MIRP, + /* MIRP[03] */ Ins_MIRP, + /* MIRP[04] */ Ins_MIRP, + /* MIRP[05] */ Ins_MIRP, + /* MIRP[06] */ Ins_MIRP, + /* MIRP[07] */ Ins_MIRP, + /* MIRP[08] */ Ins_MIRP, + /* MIRP[09] */ Ins_MIRP, + /* MIRP[10] */ Ins_MIRP, + /* MIRP[11] */ Ins_MIRP, + /* MIRP[12] */ Ins_MIRP, + /* MIRP[13] */ Ins_MIRP, + /* MIRP[14] */ Ins_MIRP, + /* MIRP[15] */ Ins_MIRP, + + /* MIRP[16] */ Ins_MIRP, + /* MIRP[17] */ Ins_MIRP, + /* MIRP[18] */ Ins_MIRP, + /* MIRP[19] */ Ins_MIRP, + /* MIRP[20] */ Ins_MIRP, + /* MIRP[21] */ Ins_MIRP, + /* MIRP[22] */ Ins_MIRP, + /* MIRP[23] */ Ins_MIRP, + /* MIRP[24] */ Ins_MIRP, + /* MIRP[25] */ Ins_MIRP, + /* MIRP[26] */ Ins_MIRP, + /* MIRP[27] */ Ins_MIRP, + /* MIRP[28] */ Ins_MIRP, + /* MIRP[29] */ Ins_MIRP, + /* MIRP[30] */ Ins_MIRP, + /* MIRP[31] */ Ins_MIRP +}; + + +#endif /* !TT_CONFIG_OPTION_INTERPRETER_SWITCH */ + + +/*************************************************************************/ +/* */ +/* RUN */ +/* */ +/* This function executes a run of opcodes. It will exit in the */ +/* following cases: */ +/* */ +/* - Errors (in which case it returns FALSE). */ +/* */ +/* - Reaching the end of the main code range (returns TRUE). */ +/* Reaching the end of a code range within a function call is an */ +/* error. */ +/* */ +/* - After executing one single opcode, if the flag `Instruction_Trap' */ +/* is set to TRUE (returns TRUE). */ +/* */ +/* On exit whith TRUE, test IP < CodeSize to know wether it comes from */ +/* an instruction trap or a normal termination. */ +/* */ +/* */ +/* Note: The documented DEBUG opcode pops a value from the stack. This */ +/* behaviour is unsupported; here a DEBUG opcode is always an */ +/* error. */ +/* */ +/* */ +/* THIS IS THE INTERPRETER'S MAIN LOOP. */ +/* */ +/* Instructions appear in the specification's order. */ +/* */ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* TT_RunIns */ +/* */ +/* */ +/* Executes one or more instruction in the execution context. This */ +/* is the main function of the TrueType opcode interpreter. */ +/* */ +/* */ +/* exec :: A handle to the target execution context. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* Only the object manager and debugger should call this function. */ +/* */ +/* This function is publicly exported because it is directly */ +/* invoked by the TrueType debugger. */ +/* */ +FT_EXPORT_FUNC( FT_Error ) TT_RunIns( TT_ExecContext exc ) +{ + FT_Long ins_counter = 0; /* executed instructions counter */ + + +#ifdef TT_CONFIG_OPTION_STATIC_RASTER + cur = *exc; +#endif + + /* set CVT functions */ + CUR.tt_metrics.ratio = 0; + if ( CUR.metrics.x_ppem != CUR.metrics.y_ppem ) { + /* non-square pixels, use the stretched routines */ + CUR.func_read_cvt = Read_CVT_Stretched; + CUR.func_write_cvt = Write_CVT_Stretched; + CUR.func_move_cvt = Move_CVT_Stretched; + } else + { + /* square pixels, use normal routines */ + CUR.func_read_cvt = Read_CVT; + CUR.func_write_cvt = Write_CVT; + CUR.func_move_cvt = Move_CVT; + } + + COMPUTE_Funcs(); + COMPUTE_Round( (FT_Byte)exc->GS.round_state ); + + do + { + CUR.opcode = CUR.code[CUR.IP]; + + if ( ( CUR.length = opcode_length[CUR.opcode] ) < 0 ) { + if ( CUR.IP + 1 > CUR.codeSize ) { + goto LErrorCodeOverflow_; + } + + CUR.length = CUR.code[CUR.IP + 1] + 2; + } + + if ( CUR.IP + CUR.length > CUR.codeSize ) { + goto LErrorCodeOverflow_; + } + + /* First, let's check for empty stack and overflow */ + CUR.args = CUR.top - ( Pop_Push_Count[CUR.opcode] >> 4 ); + + /* `args' is the top of the stack once arguments have been popped. */ + /* One can also interpret it as the index of the last argument. */ + if ( CUR.args < 0 ) { + CUR.error = TT_Err_Too_Few_Arguments; + goto LErrorLabel_; + } + + CUR.new_top = CUR.args + ( Pop_Push_Count[CUR.opcode] & 15 ); + + /* `new_top' is the new top of the stack, after the instruction's */ + /* execution. `top' will be set to `new_top' after the `switch' */ + /* statement. */ + if ( CUR.new_top > CUR.stackSize ) { + CUR.error = TT_Err_Stack_Overflow; + goto LErrorLabel_; + } + + CUR.step_ins = TRUE; + CUR.error = TT_Err_Ok; + +#ifdef TT_CONFIG_OPTION_INTERPRETER_SWITCH + + { + FT_Long* args = CUR.stack + CUR.args; + FT_Byte opcode = CUR.opcode; + + +#undef ARRAY_BOUND_ERROR +#define ARRAY_BOUND_ERROR goto Set_Invalid_Ref + + + switch ( opcode ) + { + case 0x00: /* SVTCA y */ + case 0x01: /* SVTCA x */ + case 0x02: /* SPvTCA y */ + case 0x03: /* SPvTCA x */ + case 0x04: /* SFvTCA y */ + case 0x05: /* SFvTCA x */ + { + FT_Short AA, BB; + + + AA = (FT_Short)( opcode & 1 ) << 14; + BB = AA ^ (FT_Short)0x4000; + + if ( opcode < 4 ) { + CUR.GS.projVector.x = AA; + CUR.GS.projVector.y = BB; + + CUR.GS.dualVector.x = AA; + CUR.GS.dualVector.y = BB; + } + + if ( ( opcode & 2 ) == 0 ) { + CUR.GS.freeVector.x = AA; + CUR.GS.freeVector.y = BB; + } + + COMPUTE_Funcs(); + } + break; + + case 0x06: /* SPvTL // */ + case 0x07: /* SPvTL + */ + DO_SPVTL + break; + + case 0x08: /* SFvTL // */ + case 0x09: /* SFvTL + */ + DO_SFVTL + break; + + case 0x0A: /* SPvFS */ + DO_SPVFS + break; + + case 0x0B: /* SFvFS */ + DO_SFVFS + break; + + case 0x0C: /* GPV */ + DO_GPV + break; + + case 0x0D: /* GFV */ + DO_GFV + break; + + case 0x0E: /* SFvTPv */ + DO_SFVTPV + break; + + case 0x0F: /* ISECT */ + Ins_ISECT( EXEC_ARG_ args ); + break; + + case 0x10: /* SRP0 */ + DO_SRP0 + break; + + case 0x11: /* SRP1 */ + DO_SRP1 + break; + + case 0x12: /* SRP2 */ + DO_SRP2 + break; + + case 0x13: /* SZP0 */ + Ins_SZP0( EXEC_ARG_ args ); + break; + + case 0x14: /* SZP1 */ + Ins_SZP1( EXEC_ARG_ args ); + break; + + case 0x15: /* SZP2 */ + Ins_SZP2( EXEC_ARG_ args ); + break; + + case 0x16: /* SZPS */ + Ins_SZPS( EXEC_ARG_ args ); + break; + + case 0x17: /* SLOOP */ + DO_SLOOP + break; + + case 0x18: /* RTG */ + DO_RTG + break; + + case 0x19: /* RTHG */ + DO_RTHG + break; + + case 0x1A: /* SMD */ + DO_SMD + break; + + case 0x1B: /* ELSE */ + Ins_ELSE( EXEC_ARG_ args ); + break; + + case 0x1C: /* JMPR */ + DO_JMPR + break; + + case 0x1D: /* SCVTCI */ + DO_SCVTCI + break; + + case 0x1E: /* SSWCI */ + DO_SSWCI + break; + + case 0x1F: /* SSW */ + DO_SSW + break; + + case 0x20: /* DUP */ + DO_DUP + break; + + case 0x21: /* POP */ + /* nothing :-) */ + break; + + case 0x22: /* CLEAR */ + DO_CLEAR + break; + + case 0x23: /* SWAP */ + DO_SWAP + break; + + case 0x24: /* DEPTH */ + DO_DEPTH + break; + + case 0x25: /* CINDEX */ + DO_CINDEX + break; + + case 0x26: /* MINDEX */ + Ins_MINDEX( EXEC_ARG_ args ); + break; + + case 0x27: /* ALIGNPTS */ + Ins_ALIGNPTS( EXEC_ARG_ args ); + break; + + case 0x28: /* ???? */ + Ins_UNKNOWN( EXEC_ARG_ args ); + break; + + case 0x29: /* UTP */ + Ins_UTP( EXEC_ARG_ args ); + break; + + case 0x2A: /* LOOPCALL */ + Ins_LOOPCALL( EXEC_ARG_ args ); + break; + + case 0x2B: /* CALL */ + Ins_CALL( EXEC_ARG_ args ); + break; + + case 0x2C: /* FDEF */ + Ins_FDEF( EXEC_ARG_ args ); + break; + + case 0x2D: /* ENDF */ + Ins_ENDF( EXEC_ARG_ args ); + break; + + case 0x2E: /* MDAP */ + case 0x2F: /* MDAP */ + Ins_MDAP( EXEC_ARG_ args ); + break; + + + case 0x30: /* IUP */ + case 0x31: /* IUP */ + Ins_IUP( EXEC_ARG_ args ); + break; + + case 0x32: /* SHP */ + case 0x33: /* SHP */ + Ins_SHP( EXEC_ARG_ args ); + break; + + case 0x34: /* SHC */ + case 0x35: /* SHC */ + Ins_SHC( EXEC_ARG_ args ); + break; + + case 0x36: /* SHZ */ + case 0x37: /* SHZ */ + Ins_SHZ( EXEC_ARG_ args ); + break; + + case 0x38: /* SHPIX */ + Ins_SHPIX( EXEC_ARG_ args ); + break; + + case 0x39: /* IP */ + Ins_IP( EXEC_ARG_ args ); + break; + + case 0x3A: /* MSIRP */ + case 0x3B: /* MSIRP */ + Ins_MSIRP( EXEC_ARG_ args ); + break; + + case 0x3C: /* AlignRP */ + Ins_ALIGNRP( EXEC_ARG_ args ); + break; + + case 0x3D: /* RTDG */ + DO_RTDG + break; + + case 0x3E: /* MIAP */ + case 0x3F: /* MIAP */ + Ins_MIAP( EXEC_ARG_ args ); + break; + + case 0x40: /* NPUSHB */ + Ins_NPUSHB( EXEC_ARG_ args ); + break; + + case 0x41: /* NPUSHW */ + Ins_NPUSHW( EXEC_ARG_ args ); + break; + + case 0x42: /* WS */ + DO_WS + break; + +Set_Invalid_Ref: + CUR.error = TT_Err_Invalid_Reference; + break; + + case 0x43: /* RS */ + DO_RS + break; + + case 0x44: /* WCVTP */ + DO_WCVTP + break; + + case 0x45: /* RCVT */ + DO_RCVT + break; + + case 0x46: /* GC */ + case 0x47: /* GC */ + Ins_GC( EXEC_ARG_ args ); + break; + + case 0x48: /* SCFS */ + Ins_SCFS( EXEC_ARG_ args ); + break; + + case 0x49: /* MD */ + case 0x4A: /* MD */ + Ins_MD( EXEC_ARG_ args ); + break; + + case 0x4B: /* MPPEM */ + DO_MPPEM + break; + + case 0x4C: /* MPS */ + DO_MPS + break; + + case 0x4D: /* FLIPON */ + DO_FLIPON + break; + + case 0x4E: /* FLIPOFF */ + DO_FLIPOFF + break; + + case 0x4F: /* DEBUG */ + DO_DEBUG + break; + + case 0x50: /* LT */ + DO_LT + break; + + case 0x51: /* LTEQ */ + DO_LTEQ + break; + + case 0x52: /* GT */ + DO_GT + break; + + case 0x53: /* GTEQ */ + DO_GTEQ + break; + + case 0x54: /* EQ */ + DO_EQ + break; + + case 0x55: /* NEQ */ + DO_NEQ + break; + + case 0x56: /* ODD */ + DO_ODD + break; + + case 0x57: /* EVEN */ + DO_EVEN + break; + + case 0x58: /* IF */ + Ins_IF( EXEC_ARG_ args ); + break; + + case 0x59: /* EIF */ + /* do nothing */ + break; + + case 0x5A: /* AND */ + DO_AND + break; + + case 0x5B: /* OR */ + DO_OR + break; + + case 0x5C: /* NOT */ + DO_NOT + break; + + case 0x5D: /* DELTAP1 */ + Ins_DELTAP( EXEC_ARG_ args ); + break; + + case 0x5E: /* SDB */ + DO_SDB + break; + + case 0x5F: /* SDS */ + DO_SDS + break; + + case 0x60: /* ADD */ + DO_ADD + break; + + case 0x61: /* SUB */ + DO_SUB + break; + + case 0x62: /* DIV */ + DO_DIV + break; + + case 0x63: /* MUL */ + DO_MUL + break; + + case 0x64: /* ABS */ + DO_ABS + break; + + case 0x65: /* NEG */ + DO_NEG + break; + + case 0x66: /* FLOOR */ + DO_FLOOR + break; + + case 0x67: /* CEILING */ + DO_CEILING + break; + + case 0x68: /* ROUND */ + case 0x69: /* ROUND */ + case 0x6A: /* ROUND */ + case 0x6B: /* ROUND */ + DO_ROUND + break; + + case 0x6C: /* NROUND */ + case 0x6D: /* NROUND */ + case 0x6E: /* NRRUND */ + case 0x6F: /* NROUND */ + DO_NROUND + break; + + case 0x70: /* WCVTF */ + DO_WCVTF + break; + + case 0x71: /* DELTAP2 */ + case 0x72: /* DELTAP3 */ + Ins_DELTAP( EXEC_ARG_ args ); + break; + + case 0x73: /* DELTAC0 */ + case 0x74: /* DELTAC1 */ + case 0x75: /* DELTAC2 */ + Ins_DELTAC( EXEC_ARG_ args ); + break; + + case 0x76: /* SROUND */ + DO_SROUND + break; + + case 0x77: /* S45Round */ + DO_S45ROUND + break; + + case 0x78: /* JROT */ + DO_JROT + break; + + case 0x79: /* JROF */ + DO_JROF + break; + + case 0x7A: /* ROFF */ + DO_ROFF + break; + + case 0x7B: /* ???? */ + Ins_UNKNOWN( EXEC_ARG_ args ); + break; + + case 0x7C: /* RUTG */ + DO_RUTG + break; + + case 0x7D: /* RDTG */ + DO_RDTG + break; + + case 0x7E: /* SANGW */ + case 0x7F: /* AA */ + /* nothing - obsolete */ + break; + + case 0x80: /* FLIPPT */ + Ins_FLIPPT( EXEC_ARG_ args ); + break; + + case 0x81: /* FLIPRGON */ + Ins_FLIPRGON( EXEC_ARG_ args ); + break; + + case 0x82: /* FLIPRGOFF */ + Ins_FLIPRGOFF( EXEC_ARG_ args ); + break; + + case 0x83: /* UNKNOWN */ + case 0x84: /* UNKNOWN */ + Ins_UNKNOWN( EXEC_ARG_ args ); + break; + + case 0x85: /* SCANCTRL */ + Ins_SCANCTRL( EXEC_ARG_ args ); + break; + + case 0x86: /* SDPVTL */ + case 0x87: /* SDPVTL */ + Ins_SDPVTL( EXEC_ARG_ args ); + break; + + case 0x88: /* GETINFO */ + Ins_GETINFO( EXEC_ARG_ args ); + break; + + case 0x89: /* IDEF */ + Ins_IDEF( EXEC_ARG_ args ); + break; + + case 0x8A: /* ROLL */ + Ins_ROLL( EXEC_ARG_ args ); + break; + + case 0x8B: /* MAX */ + DO_MAX + break; + + case 0x8C: /* MIN */ + DO_MIN + break; + + case 0x8D: /* SCANTYPE */ + Ins_SCANTYPE( EXEC_ARG_ args ); + break; + + case 0x8E: /* INSTCTRL */ + Ins_INSTCTRL( EXEC_ARG_ args ); + break; + + case 0x8F: + Ins_UNKNOWN( EXEC_ARG_ args ); + break; + + default: + if ( opcode >= 0xE0 ) { + Ins_MIRP( EXEC_ARG_ args ); + } else if ( opcode >= 0xC0 ) { + Ins_MDRP( EXEC_ARG_ args ); + } else if ( opcode >= 0xB8 ) { + Ins_PUSHW( EXEC_ARG_ args ); + } else if ( opcode >= 0xB0 ) { + Ins_PUSHB( EXEC_ARG_ args ); + } else { + Ins_UNKNOWN( EXEC_ARG_ args ); + } + } + + } + +#else + + Instruct_Dispatch[CUR.opcode]( EXEC_ARG_ & CUR.stack[CUR.args] ); + +#endif /* TT_CONFIG_OPTION_INTERPRETER_SWITCH */ + + if ( CUR.error != TT_Err_Ok ) { + switch ( CUR.error ) + { + case TT_Err_Invalid_Opcode: /* looking for redefined instructions */ + { + TT_DefRecord* def = CUR.IDefs; + TT_DefRecord* limit = def + CUR.numIDefs; + + + for ( ; def < limit; def++ ) + { + if ( def->active && CUR.opcode == def->opc ) { + TT_CallRec* callrec; + + + if ( CUR.callTop >= CUR.callSize ) { + CUR.error = TT_Err_Invalid_Reference; + goto LErrorLabel_; + } + + callrec = &CUR.callStack[CUR.callTop]; + + callrec->Caller_Range = CUR.curRange; + callrec->Caller_IP = CUR.IP + 1; + callrec->Cur_Count = 1; + callrec->Cur_Restart = def->start; + + if ( INS_Goto_CodeRange( def->range, def->start ) == FAILURE ) { + goto LErrorLabel_; + } + + goto LSuiteLabel_; + } + } + } + + CUR.error = TT_Err_Invalid_Opcode; + goto LErrorLabel_; + +#if 0 + break; /* Unreachable code warning suppression. */ + /* Leave to remind in case a later change the editor */ + /* to consider break; */ +#endif + + default: + goto LErrorLabel_; + +#if 0 + break; +#endif + } + } + + CUR.top = CUR.new_top; + + if ( CUR.step_ins ) { + CUR.IP += CUR.length; + } + + /* increment instruction counter and check if we didn't */ + /* run this program for too long (e.g. infinite loops). */ + if ( ++ins_counter > MAX_RUNNABLE_OPCODES ) { + return TT_Err_Execution_Too_Long; + } + +LSuiteLabel_: + if ( CUR.IP >= CUR.codeSize ) { + if ( CUR.callTop > 0 ) { + CUR.error = TT_Err_Code_Overflow; + goto LErrorLabel_; + } else { + goto LNo_Error_; + } + } + } while ( !CUR.instruction_trap ); + +LNo_Error_: + +#ifdef TT_CONFIG_OPTION_STATIC_RASTER + *exc = cur; +#endif + + return TT_Err_Ok; + +LErrorCodeOverflow_: + CUR.error = TT_Err_Code_Overflow; + +LErrorLabel_: + +#ifdef TT_CONFIG_OPTION_STATIC_RASTER + *exc = cur; +#endif + + return CUR.error; +} + + +#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */ + + +/* END */ diff --git a/src/ft2/ttinterp.h b/src/ft2/ttinterp.h new file mode 100644 index 0000000..fab608d --- /dev/null +++ b/src/ft2/ttinterp.h @@ -0,0 +1,270 @@ +/***************************************************************************/ +/* */ +/* ttinterp.h */ +/* */ +/* TrueType bytecode interpreter (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef TTINTERP_H +#define TTINTERP_H + + +#include "ttobjs.h" + + +#ifdef __cplusplus +extern "C" { +#endif + + +#ifndef TT_CONFIG_OPTION_STATIC_INTEPRETER /* indirect implementation */ + +#define EXEC_OP_ TT_ExecContext exc, +#define EXEC_OP TT_ExecContext exc +#define EXEC_ARG_ exc, +#define EXEC_ARG exc + +#else /* static implementation */ + +#define EXEC_OP_ /* void */ +#define EXEC_OP /* void */ +#define EXEC_ARG_ /* void */ +#define EXEC_ARG /* void */ + +#endif /* TT_CONFIG_OPTION_STATIC_INTERPRETER */ + + +/*************************************************************************/ +/* */ +/* Rounding mode constants. */ +/* */ +#define TT_Round_Off 5 +#define TT_Round_To_Half_Grid 0 +#define TT_Round_To_Grid 1 +#define TT_Round_To_Double_Grid 2 +#define TT_Round_Up_To_Grid 4 +#define TT_Round_Down_To_Grid 3 +#define TT_Round_Super 6 +#define TT_Round_Super_45 7 + + +/*************************************************************************/ +/* */ +/* Function types used by the interpreter, depending on various modes */ +/* (e.g. the rounding mode, whether to render a vertical or horizontal */ +/* line etc). */ +/* */ +/*************************************************************************/ + +/* Rounding function */ +typedef FT_F26Dot6 ( *TT_Round_Func )( EXEC_OP_ FT_F26Dot6 distance, + FT_F26Dot6 compensation ); + +/* Point displacement along the freedom vector routine */ +typedef void ( *TT_Move_Func )( EXEC_OP_ TT_GlyphZone* zone, + FT_UInt point, + FT_F26Dot6 distance ); + +/* Distance projection along one of the projection vectors */ +typedef FT_F26Dot6 ( *TT_Project_Func )( EXEC_OP_ FT_Vector* v1, + FT_Vector* v2 ); + +/* reading a cvt value. Take care of non-square pixels if necessary */ +typedef FT_F26Dot6 ( *TT_Get_CVT_Func )( EXEC_OP_ FT_ULong index ); + +/* setting or moving a cvt value. Take care of non-square pixels */ +/* if necessary */ +typedef void ( *TT_Set_CVT_Func )( EXEC_OP_ FT_ULong index, + FT_F26Dot6 value ); + + +/*************************************************************************/ +/* */ +/* This structure defines a call record, used to manage function calls. */ +/* */ +typedef struct TT_CallRec_ +{ + FT_Int Caller_Range; + FT_Long Caller_IP; + FT_Long Cur_Count; + FT_Long Cur_Restart; + +} TT_CallRec, *TT_CallStack; + + +/*************************************************************************/ +/* */ +/* The main structure for the interpreter which collects all necessary */ +/* variables and states. */ +/* */ +typedef struct TT_ExecContextRec_ +{ + TT_Face face; + TT_Size size; + FT_Memory memory; + + /* instructions state */ + + FT_Error error; /* last execution error */ + + FT_Long top; /* top of exec. stack */ + + FT_UInt stackSize; /* size of exec. stack */ + FT_Long* stack; /* current exec. stack */ + + FT_Long args; + FT_UInt new_top; /* new top after exec. */ + + TT_GlyphZone zp0, /* zone records */ + zp1, + zp2, + pts, + twilight; + + FT_Size_Metrics metrics; + TT_Size_Metrics tt_metrics; /* size metrics */ + + TT_GraphicsState GS; /* current graphics state */ + + FT_Int curRange; /* current code range number */ + FT_Byte* code; /* current code range */ + FT_Long IP; /* current instruction pointer */ + FT_Long codeSize; /* size of current range */ + + FT_Byte opcode; /* current opcode */ + FT_Int length; /* length of current opcode */ + + FT_Bool step_ins; /* true if the interpreter must */ + /* increment IP after ins. exec */ + FT_Long cvtSize; + FT_Long* cvt; + + FT_UInt glyphSize; /* glyph instructions buffer size */ + FT_Byte* glyphIns; /* glyph instructions buffer */ + + FT_UInt numFDefs; /* number of function defs */ + FT_UInt maxFDefs; /* maximum number of function defs */ + TT_DefArray FDefs; /* table of FDefs entries */ + + FT_UInt numIDefs; /* number of instruction defs */ + FT_UInt maxIDefs; /* maximum number of ins defs */ + TT_DefArray IDefs; /* table of IDefs entries */ + + FT_UInt maxFunc; /* maximum function index */ + FT_UInt maxIns; /* maximum instruction index */ + + FT_Int callTop, /* top of call stack during execution */ + callSize; /* size of call stack */ + TT_CallStack callStack; /* call stack */ + + FT_UShort maxPoints; /* capacity of this context's `pts' */ + FT_Short maxContours; /* record, expressed in points and */ + /* contours. */ + + TT_CodeRangeTable codeRangeTable; /* table of valid code ranges */ + /* useful for the debugger */ + + FT_UShort storeSize; /* size of current storage */ + FT_Long* storage; /* storage area */ + + FT_F26Dot6 period; /* values used for the */ + FT_F26Dot6 phase; /* `SuperRounding' */ + FT_F26Dot6 threshold; + +#if 0 + /* this seems to be unused */ + FT_Int cur_ppem; /* ppem along the current proj vector */ +#endif + + FT_Bool instruction_trap; /* If `True', the interpreter will */ + /* exit after each instruction */ + + TT_GraphicsState default_GS; /* graphics state resulting from */ + /* the prep program */ + FT_Bool is_composite; /* true if the glyph is composite */ + FT_Bool pedantic_hinting; /* true for pedantic interpretation */ + + /* latest interpreter additions */ + + FT_Long F_dot_P; /* dot product of freedom and projection */ + /* vectors */ + TT_Round_Func func_round; /* current rounding function */ + + TT_Project_Func func_project, /* current projection function */ + func_dualproj, /* current dual proj. function */ + func_freeProj; /* current freedom proj. func */ + + TT_Move_Func func_move; /* current point move function */ + + TT_Get_CVT_Func func_read_cvt; /* read a cvt entry */ + TT_Set_CVT_Func func_write_cvt; /* write a cvt entry (in pixels) */ + TT_Set_CVT_Func func_move_cvt; /* incr a cvt entry (in pixels) */ + + FT_ULong loadSize; + TT_SubGlyph_Stack loadStack; /* loading subglyph stack */ + +} TT_ExecContextRec; + + +extern const TT_GraphicsState tt_default_graphics_state; + + +LOCAL_DEF +FT_Error TT_Goto_CodeRange( TT_ExecContext exec, + FT_Int range, + FT_Long IP ); + +LOCAL_DEF +FT_Error TT_Set_CodeRange( TT_ExecContext exec, + FT_Int range, + void* base, + FT_Long length ); + +LOCAL_DEF +FT_Error TT_Clear_CodeRange( TT_ExecContext exec, + FT_Int range ); + +FT_EXPORT_DEF( TT_ExecContext ) TT_New_Context( TT_Face face ); + +LOCAL_DEF +FT_Error TT_Done_Context( TT_ExecContext exec ); + +LOCAL_DEF +FT_Error TT_Destroy_Context( TT_ExecContext exec, + FT_Memory memory ); + +LOCAL_DEF +FT_Error TT_Load_Context( TT_ExecContext exec, + TT_Face face, + TT_Size size ); + +LOCAL_DEF +FT_Error TT_Save_Context( TT_ExecContext exec, + TT_Size ins ); + +LOCAL_DEF +FT_Error TT_Run_Context( TT_ExecContext exec, + FT_Bool debug ); + +FT_EXPORT_DEF( FT_Error ) TT_RunIns( TT_ExecContext exec ); + + +#ifdef __cplusplus +} +#endif + +#endif /* TTINTERP_H */ + + +/* END */ diff --git a/src/ft2/ttload.c b/src/ft2/ttload.c new file mode 100644 index 0000000..d363765 --- /dev/null +++ b/src/ft2/ttload.c @@ -0,0 +1,1673 @@ +/***************************************************************************/ +/* */ +/* ttload.c */ +/* */ +/* Load the basic TrueType tables, i.e., tables that can be either in */ +/* TTF or OTF fonts (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#include "ftdebug.h" +#include "tterrors.h" +#include "tttags.h" + + +#include "ttload.h" +#include "ttcmap.h" + + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_ttload + + +/*************************************************************************/ +/* */ +/* */ +/* TT_LookUp_Table */ +/* */ +/* */ +/* Looks for a TrueType table by name. */ +/* */ +/* */ +/* face :: A face object handle. */ +/* tag :: The searched tag. */ +/* */ +/* */ +/* A pointer to the table directory entry. 0 if not found. */ +/* */ +LOCAL_FUNC +TT_Table* TT_LookUp_Table( TT_Face face, + FT_ULong tag ) { + TT_Table* entry; + TT_Table* limit; + + + FT_TRACE3( ( "TT_LookUp_Table: %08p, `%c%c%c%c'\n", + face, + (FT_Char)( tag >> 24 ), + (FT_Char)( tag >> 16 ), + (FT_Char)( tag >> 8 ), + (FT_Char)( tag ) ) ); + + entry = face->dir_tables; + limit = entry + face->num_tables; + + for ( ; entry < limit; entry++ ) + { + if ( entry->Tag == tag ) { + return entry; + } + } + + FT_TRACE3( ( " Could not find table!\n" ) ); + return 0; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Goto_Table */ +/* */ +/* */ +/* Looks for a TrueType table by name, then seek a stream to it. */ +/* */ +/* */ +/* face :: A face object handle. */ +/* tag :: The searched tag. */ +/* stream :: The stream to seek when the table is found. */ +/* */ +/* */ +/* length :: The length of the table if found, undefined otherwise. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Goto_Table( TT_Face face, + FT_ULong tag, + FT_Stream stream, + FT_ULong* length ) { + TT_Table* table; + FT_Error error; + + + table = TT_LookUp_Table( face, tag ); + if ( table ) { + if ( length ) { + *length = table->Length; + } + + (void)FILE_Seek( table->Offset ); + } else { + error = TT_Err_Table_Missing; + } + + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_SFNT_Header */ +/* */ +/* */ +/* Loads the header of a SFNT font file. Supports collections. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* stream :: The input stream. */ +/* face_index :: If the font is a collection, the number of the font */ +/* in the collection, ignored otherwise. */ +/* */ +/* */ +/* sfnt :: The SFNT header. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* The stream cursor must be at the font file's origin. */ +/* */ +/* This function recognizes fonts embedded in a `TrueType collection' */ +/* */ +/* The header will be checked whether it is valid by looking at the */ +/* values of `search_range', `entry_selector', and `range_shift'. */ +/* */ +LOCAL_FUNC +FT_Error TT_Load_SFNT_Header( TT_Face face, + FT_Stream stream, + FT_Long face_index, + SFNT_Header* sfnt ) { + FT_Error error; + FT_ULong format_tag; + FT_Memory memory = stream->memory; + + const FT_Frame_Field sfnt_header_fields[] = + { + FT_FRAME_START( 8 ), + FT_FRAME_USHORT( SFNT_Header, num_tables ), + FT_FRAME_USHORT( SFNT_Header, search_range ), + FT_FRAME_USHORT( SFNT_Header, entry_selector ), + FT_FRAME_USHORT( SFNT_Header, range_shift ), + FT_FRAME_END + }; + + const FT_Frame_Field ttc_header_fields[] = + { + FT_FRAME_START( 8 ), + FT_FRAME_LONG( TTC_Header, version ), + FT_FRAME_LONG( TTC_Header, count ), + FT_FRAME_END + }; + + + FT_TRACE2( ( "TT_Load_SFNT_Header: %08p, %ld\n", + face, face_index ) ); + + face->ttc_header.tag = 0; + face->ttc_header.version = 0; + face->ttc_header.count = 0; + + face->num_tables = 0; + + /* first of all, read the first 4 bytes. If it is `ttcf', then the */ + /* file is a TrueType collection, otherwise it can be any other */ + /* kind of font. */ + if ( READ_ULong( format_tag ) ) { + goto Exit; + } + + if ( format_tag == TTAG_ttcf ) { + FT_Int n; + + + FT_TRACE3( ( "TT_Load_SFNT_Header: file is a collection\n" ) ); + + /* it's a TrueType collection, i.e. a file containing several */ + /* font files. Read the font directory now */ + if ( READ_Fields( ttc_header_fields, &face->ttc_header ) ) { + goto Exit; + } + + /* now read the offsets of each font in the file */ + if ( ALLOC_ARRAY( face->ttc_header.offsets, + face->ttc_header.count, + FT_ULong ) || + ACCESS_Frame( face->ttc_header.count * 4L ) ) { + goto Exit; + } + + for ( n = 0; n < face->ttc_header.count; n++ ) + face->ttc_header.offsets[n] = GET_ULong(); + + FORGET_Frame(); + + /* check face index */ + if ( face_index >= face->ttc_header.count ) { + error = TT_Err_Bad_Argument; + goto Exit; + } + + /* seek to the appropriate TrueType file, then read tag */ + if ( FILE_Seek( face->ttc_header.offsets[face_index] ) || + READ_Long( format_tag ) ) { + goto Exit; + } + } + + /* the format tag was read, now check the rest of the header */ + sfnt->format_tag = format_tag; + if ( READ_Fields( sfnt_header_fields, sfnt ) ) { + goto Exit; + } + + /* now, check the values of `num_tables', `seach_range', etc. */ + { + FT_UInt num_tables = sfnt->num_tables; + FT_ULong entry_selector = 1L << sfnt->entry_selector; + + + /* IMPORTANT: Many fonts have an incorrect `search_range' value, so */ + /* we only check the `entry_selector' correctness here. */ + /* */ + if ( num_tables == 0 || + entry_selector > num_tables || + entry_selector * 2 <= num_tables ) { + FT_TRACE2( ( "TT_Load_SFNT_Header: file is not SFNT!\n" ) ); + error = FT_Err_Unknown_File_Format; + } + } + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_Directory */ +/* */ +/* */ +/* Loads the table directory into a face object. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* */ +/* */ +/* stream :: The input stream. */ +/* sfnt :: The SFNT directory header. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* The stream cursor must be at the font file's origin. */ +/* */ +LOCAL_FUNC +FT_Error TT_Load_Directory( TT_Face face, + FT_Stream stream, + SFNT_Header* sfnt ) { + FT_Error error; + FT_Memory memory = stream->memory; + + TT_Table *entry, *limit; + + + FT_TRACE2( ( "TT_Load_Directory: %08p\n", face ) ); + + FT_TRACE2( ( "-- Tables count: %12u\n", sfnt->num_tables ) ); + FT_TRACE2( ( "-- Format version: %08lx\n", sfnt->format_tag ) ); + + face->num_tables = sfnt->num_tables; + + if ( ALLOC_ARRAY( face->dir_tables, + face->num_tables, + TT_Table ) ) { + goto Exit; + } + + if ( ACCESS_Frame( face->num_tables * 16L ) ) { + goto Exit; + } + + entry = face->dir_tables; + limit = entry + face->num_tables; + + for ( ; entry < limit; entry++ ) + { /* loop through the tables and get all entries */ + entry->Tag = GET_Tag4(); + entry->CheckSum = GET_ULong(); + entry->Offset = GET_Long(); + entry->Length = GET_Long(); + + FT_TRACE2( ( " %c%c%c%c - %08lx - %08lx\n", + (FT_Char)( entry->Tag >> 24 ), + (FT_Char)( entry->Tag >> 16 ), + (FT_Char)( entry->Tag >> 8 ), + (FT_Char)( entry->Tag ), + entry->Offset, + entry->Length ) ); + } + + FORGET_Frame(); + + FT_TRACE2( ( "Directory loaded\n\n" ) ); + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_Any */ +/* */ +/* */ +/* Loads any font table into client memory. */ +/* */ +/* */ +/* face :: The face object to look for. */ +/* */ +/* tag :: The tag of table to load. Use the value 0 if you want */ +/* to access the whole font file, else set this parameter */ +/* to a valid TrueType table tag that you can forge with */ +/* the MAKE_TT_TAG macro. */ +/* */ +/* offset :: The starting offset in the table (or the file if */ +/* tag == 0). */ +/* */ +/* length :: The address of the decision variable: */ +/* */ +/* If length == NULL: */ +/* Loads the whole table. Returns an error if */ +/* `offset' == 0! */ +/* */ +/* If *length == 0: */ +/* Exits immediately; returning the length of the given */ +/* table or of the font file, depending on the value of */ +/* `tag'. */ +/* */ +/* If *length != 0: */ +/* Loads the next `length' bytes of table or font, */ +/* starting at offset `offset' (in table or font too). */ +/* */ +/* */ +/* buffer :: The address of target buffer. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Load_Any( TT_Face face, + FT_ULong tag, + FT_Long offset, + FT_Byte* buffer, + FT_ULong* length ) { + FT_Error error; + FT_Stream stream; + TT_Table* table; + FT_ULong size; + + + if ( tag != 0 ) { + /* look for tag in font directory */ + table = TT_LookUp_Table( face, tag ); + if ( !table ) { + error = TT_Err_Table_Missing; + goto Exit; + } + + offset += table->Offset; + size = table->Length; + } else { + /* tag == 0 -- the user wants to access the font file directly */ + size = face->root.stream->size; + } + + if ( length && *length == 0 ) { + *length = size; + + return TT_Err_Ok; + } + + if ( length ) { + size = *length; + } + + stream = face->root.stream; + (void)FILE_Read_At( offset, buffer, size ); + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_Header */ +/* */ +/* */ +/* Loads the TrueType font header. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* stream :: The input stream. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Load_Header( TT_Face face, + FT_Stream stream ) { + FT_Error error; + TT_Header* header; + + static const FT_Frame_Field header_fields[] = + { + FT_FRAME_START( 54 ), + FT_FRAME_ULONG( TT_Header, Table_Version ), + FT_FRAME_ULONG( TT_Header, Font_Revision ), + FT_FRAME_LONG( TT_Header, CheckSum_Adjust ), + FT_FRAME_LONG( TT_Header, Magic_Number ), + FT_FRAME_USHORT( TT_Header, Flags ), + FT_FRAME_USHORT( TT_Header, Units_Per_EM ), + FT_FRAME_LONG( TT_Header, Created[0] ), + FT_FRAME_LONG( TT_Header, Created[1] ), + FT_FRAME_LONG( TT_Header, Modified[0] ), + FT_FRAME_LONG( TT_Header, Modified[1] ), + FT_FRAME_SHORT( TT_Header, xMin ), + FT_FRAME_SHORT( TT_Header, yMin ), + FT_FRAME_SHORT( TT_Header, xMax ), + FT_FRAME_SHORT( TT_Header, yMax ), + FT_FRAME_USHORT( TT_Header, Mac_Style ), + FT_FRAME_USHORT( TT_Header, Lowest_Rec_PPEM ), + FT_FRAME_SHORT( TT_Header, Font_Direction ), + FT_FRAME_SHORT( TT_Header, Index_To_Loc_Format ), + FT_FRAME_SHORT( TT_Header, Glyph_Data_Format ), + FT_FRAME_END + }; + + + FT_TRACE2( ( "Load_TT_Header: %08p\n", face ) ); + + error = face->goto_table( face, TTAG_head, stream, 0 ); + if ( error ) { + FT_TRACE0( ( "Font Header is missing!\n" ) ); + goto Exit; + } + + header = &face->header; + + if ( READ_Fields( header_fields, header ) ) { + goto Exit; + } + + FT_TRACE2( ( " Units per EM: %8u\n", header->Units_Per_EM ) ); + FT_TRACE2( ( " IndexToLoc: %8d\n", header->Index_To_Loc_Format ) ); + FT_TRACE2( ( "Font Header Loaded.\n" ) ); + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_MaxProfile */ +/* */ +/* */ +/* Loads the maximum profile into a face object. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* stream :: The input stream. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Load_MaxProfile( TT_Face face, + FT_Stream stream ) { + FT_Error error; + TT_MaxProfile* maxProfile = &face->max_profile; + + const FT_Frame_Field maxp_fields[] = + { + FT_FRAME_START( 32 ), + FT_FRAME_ULONG( TT_MaxProfile, version ), + FT_FRAME_USHORT( TT_MaxProfile, numGlyphs ), + FT_FRAME_USHORT( TT_MaxProfile, maxPoints ), + FT_FRAME_USHORT( TT_MaxProfile, maxContours ), + FT_FRAME_USHORT( TT_MaxProfile, maxCompositePoints ), + FT_FRAME_USHORT( TT_MaxProfile, maxCompositeContours ), + FT_FRAME_USHORT( TT_MaxProfile, maxZones ), + FT_FRAME_USHORT( TT_MaxProfile, maxTwilightPoints ), + FT_FRAME_USHORT( TT_MaxProfile, maxStorage ), + FT_FRAME_USHORT( TT_MaxProfile, maxFunctionDefs ), + FT_FRAME_USHORT( TT_MaxProfile, maxInstructionDefs ), + FT_FRAME_USHORT( TT_MaxProfile, maxStackElements ), + FT_FRAME_USHORT( TT_MaxProfile, maxSizeOfInstructions ), + FT_FRAME_USHORT( TT_MaxProfile, maxComponentElements ), + FT_FRAME_USHORT( TT_MaxProfile, maxComponentDepth ), + FT_FRAME_END + }; + + + FT_TRACE2( ( "Load_TT_MaxProfile: %08p\n", face ) ); + + error = face->goto_table( face, TTAG_maxp, stream, 0 ); + if ( error ) { + goto Exit; + } + + if ( READ_Fields( maxp_fields, maxProfile ) ) { + goto Exit; + } + + /* XXX: an adjustment that is necessary to load certain */ + /* broken fonts like `Keystrokes MT' :-( */ + /* */ + /* We allocate 64 function entries by default when */ + /* the maxFunctionDefs field is null. */ + + if ( maxProfile->maxFunctionDefs == 0 ) { + maxProfile->maxFunctionDefs = 64; + } + + face->root.num_glyphs = maxProfile->numGlyphs; + + face->root.max_points = MAX( maxProfile->maxCompositePoints, + maxProfile->maxPoints ); + + face->root.max_contours = MAX( maxProfile->maxCompositeContours, + maxProfile->maxContours ); + + face->max_components = (FT_ULong)maxProfile->maxComponentElements + + maxProfile->maxComponentDepth; + + /* XXX: some fonts have maxComponents set to 0; we will */ + /* then use 16 of them by default. */ + if ( face->max_components == 0 ) { + face->max_components = 16; + } + + /* We also increase maxPoints and maxContours in order to support */ + /* some broken fonts. */ + face->root.max_points += 8; + face->root.max_contours += 4; + + FT_TRACE2( ( "MAXP loaded.\n" ) ); + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_Metrics */ +/* */ +/* */ +/* Loads the horizontal or vertical metrics table into a face object. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* stream :: The input stream. */ +/* vertical :: A boolean flag. If set, load vertical metrics. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +static +FT_Error TT_Load_Metrics( TT_Face face, + FT_Stream stream, + FT_Bool vertical ) { + FT_Error error; + FT_Memory memory = stream->memory; + + FT_ULong table_len; + FT_Long num_shorts, num_longs, num_shorts_checked; + + TT_LongMetrics** longs; + TT_ShortMetrics** shorts; + + + FT_TRACE2( ( "TT_Load_%s_Metrics: %08p\n", vertical ? "Vertical" + : "Horizontal", + face ) ); + + if ( vertical ) { + /* The table is optional, quit silently if it wasn't found */ + /* XXX: Some fonts have a valid vertical header with a non-null */ + /* `number_of_VMetrics' fields, but no corresponding `vmtx' */ + /* table to get the metrics from (e.g. mingliu). */ + /* */ + /* For safety, we set the field to 0! */ + /* */ + error = face->goto_table( face, TTAG_vmtx, stream, &table_len ); + if ( error ) { + /* Set number_Of_VMetrics to 0! */ + FT_TRACE2( ( " no vertical header in file.\n" ) ); + face->vertical.number_Of_VMetrics = 0; + error = TT_Err_Ok; + goto Exit; + } + + num_longs = face->vertical.number_Of_VMetrics; + longs = (TT_LongMetrics**)&face->vertical.long_metrics; + shorts = (TT_ShortMetrics**)&face->vertical.short_metrics; + } else + { + error = face->goto_table( face, TTAG_hmtx, stream, &table_len ); + if ( error ) { + FT_ERROR( ( " no horizontal metrics in file!\n" ) ); + error = TT_Err_Hmtx_Table_Missing; + goto Exit; + } + + num_longs = face->horizontal.number_Of_HMetrics; + longs = (TT_LongMetrics**)&face->horizontal.long_metrics; + shorts = (TT_ShortMetrics**)&face->horizontal.short_metrics; + } + + /* never trust derived values */ + + num_shorts = face->max_profile.numGlyphs - num_longs; + num_shorts_checked = ( table_len - num_longs * 4L ) / 2; + + if ( num_shorts < 0 ) { + FT_ERROR( ( "TT_Load_%s_Metrics: more metrics than glyphs!\n", + vertical ? "Vertical" + : "Horizontal" ) ); + + error = vertical ? TT_Err_Invalid_Vert_Metrics + : TT_Err_Invalid_Horiz_Metrics; + goto Exit; + } + + if ( ALLOC_ARRAY( *longs, num_longs, TT_LongMetrics ) || + ALLOC_ARRAY( *shorts, num_shorts, TT_ShortMetrics ) ) { + goto Exit; + } + + if ( ACCESS_Frame( table_len ) ) { + goto Exit; + } + + { + TT_LongMetrics* cur = *longs; + TT_LongMetrics* limit = cur + num_longs; + + + for ( ; cur < limit; cur++ ) + { + cur->advance = GET_UShort(); + cur->bearing = GET_Short(); + } + } + + /* do we have an inconsistent number of metric values? */ + { + TT_ShortMetrics* cur = *shorts; + TT_ShortMetrics* limit = cur + MIN( num_shorts, num_shorts_checked ); + + + for ( ; cur < limit; cur++ ) + *cur = GET_Short(); + + /* we fill up the missing left side bearings with the */ + /* last valid value. Since this will occur for buggy CJK */ + /* fonts usually only, nothing serious will happen */ + if ( num_shorts > num_shorts_checked && num_shorts_checked > 0 ) { + FT_Short val = *( shorts )[num_shorts_checked - 1]; + + + limit = *shorts + num_shorts; + for ( ; cur < limit; cur++ ) + *cur = val; + } + } + + FORGET_Frame(); + + FT_TRACE2( ( "loaded\n" ) ); + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_Metrics_Header */ +/* */ +/* */ +/* Loads the horizontal or vertical header in a face object. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* stream :: The input stream. */ +/* vertical :: A boolean flag. If set, load vertical metrics. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Load_Metrics_Header( TT_Face face, + FT_Stream stream, + FT_Bool vertical ) { + FT_Error error; + TT_HoriHeader* header; + + const FT_Frame_Field metrics_header_fields[] = + { + FT_FRAME_START( 36 ), + FT_FRAME_ULONG( TT_HoriHeader, Version ), + FT_FRAME_SHORT( TT_HoriHeader, Ascender ), + FT_FRAME_SHORT( TT_HoriHeader, Descender ), + FT_FRAME_SHORT( TT_HoriHeader, Line_Gap ), + FT_FRAME_USHORT( TT_HoriHeader, advance_Width_Max ), + FT_FRAME_SHORT( TT_HoriHeader, min_Left_Side_Bearing ), + FT_FRAME_SHORT( TT_HoriHeader, min_Right_Side_Bearing ), + FT_FRAME_SHORT( TT_HoriHeader, xMax_Extent ), + FT_FRAME_SHORT( TT_HoriHeader, caret_Slope_Rise ), + FT_FRAME_SHORT( TT_HoriHeader, caret_Slope_Run ), + FT_FRAME_SHORT( TT_HoriHeader, Reserved[0] ), + FT_FRAME_SHORT( TT_HoriHeader, Reserved[1] ), + FT_FRAME_SHORT( TT_HoriHeader, Reserved[2] ), + FT_FRAME_SHORT( TT_HoriHeader, Reserved[3] ), + FT_FRAME_SHORT( TT_HoriHeader, Reserved[4] ), + FT_FRAME_SHORT( TT_HoriHeader, metric_Data_Format ), + FT_FRAME_USHORT( TT_HoriHeader, number_Of_HMetrics ), + FT_FRAME_END + }; + + + FT_TRACE2( ( vertical ? "Vertical header " : "Horizontal header " ) ); + + if ( vertical ) { + face->vertical_info = 0; + + /* The vertical header table is optional, so return quietly if */ + /* we don't find it. */ + error = face->goto_table( face, TTAG_vhea, stream, 0 ); + if ( error ) { + error = TT_Err_Ok; + goto Exit; + } + + face->vertical_info = 1; + header = (TT_HoriHeader*)&face->vertical; + } else + { + /* The horizontal header is mandatory; return an error if we */ + /* don't find it. */ + error = face->goto_table( face, TTAG_hhea, stream, 0 ); + if ( error ) { + error = TT_Err_Horiz_Header_Missing; + goto Exit; + } + + header = &face->horizontal; + } + + if ( READ_Fields( metrics_header_fields, header ) ) { + goto Exit; + } + + header->long_metrics = NULL; + header->short_metrics = NULL; + + FT_TRACE2( ( "loaded\n" ) ); + + /* Now try to load the corresponding metrics */ + + error = TT_Load_Metrics( face, stream, vertical ); + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_Names */ +/* */ +/* */ +/* Loads the name records. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* stream :: The input stream. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Load_Names( TT_Face face, + FT_Stream stream ) { + FT_Error error; + FT_Memory memory = stream->memory; + + FT_ULong table_pos, table_len; + FT_ULong storageSize; + + TT_NameTable* names; + + const FT_Frame_Field name_table_fields[] = + { + FT_FRAME_START( 6 ), + FT_FRAME_USHORT( TT_NameTable, format ), + FT_FRAME_USHORT( TT_NameTable, numNameRecords ), + FT_FRAME_USHORT( TT_NameTable, storageOffset ), + FT_FRAME_END + }; + + const FT_Frame_Field name_record_fields[] = + { + /* no FT_FRAME_START */ + FT_FRAME_USHORT( TT_NameRec, platformID ), + FT_FRAME_USHORT( TT_NameRec, encodingID ), + FT_FRAME_USHORT( TT_NameRec, languageID ), + FT_FRAME_USHORT( TT_NameRec, nameID ), + FT_FRAME_USHORT( TT_NameRec, stringLength ), + FT_FRAME_USHORT( TT_NameRec, stringOffset ), + FT_FRAME_END + }; + + + FT_TRACE2( ( "Names " ) ); + + error = face->goto_table( face, TTAG_name, stream, &table_len ); + if ( error ) { + /* The name table is required so indicate failure. */ + FT_TRACE2( ( "is missing!\n" ) ); + error = TT_Err_Name_Table_Missing; + goto Exit; + } + + table_pos = FILE_Pos(); + + names = &face->name_table; + + if ( READ_Fields( name_table_fields, names ) ) { + goto Exit; + } + + /* Allocate the array of name records. */ + if ( ALLOC_ARRAY( names->names, + names->numNameRecords, + TT_NameRec ) || + ACCESS_Frame( names->numNameRecords * 12L ) ) { + goto Exit; + } + + /* Load the name records and determine how much storage is needed */ + /* to hold the strings themselves. */ + { + TT_NameRec* cur = names->names; + TT_NameRec* limit = cur + names->numNameRecords; + + + storageSize = 0; + + for ( ; cur < limit; cur++ ) + { + FT_ULong upper; + + + (void)READ_Fields( name_record_fields, cur ); + + upper = (FT_ULong)( cur->stringOffset + cur->stringLength ); + if ( upper > storageSize ) { + storageSize = upper; + } + } + } + + FORGET_Frame(); + + if ( storageSize > 0 ) { + /* allocate the name storage area in memory, then read it */ + if ( ALLOC( names->storage, storageSize ) || + FILE_Read_At( table_pos + names->storageOffset, + names->storage, storageSize ) ) { + goto Exit; + } + + /* Go through and assign the string pointers to the name records. */ + { + TT_NameRec* cur = names->names; + TT_NameRec* limit = cur + names->numNameRecords; + + + for ( ; cur < limit; cur++ ) + cur->string = names->storage + cur->stringOffset; + } + +#ifdef FT_DEBUG_LEVEL_TRACE + + /* Print Name Record Table in case of debugging */ + { + TT_NameRec* cur = names->names; + TT_NameRec* limit = cur + names->numNameRecords; + + + for ( ; cur < limit; cur++ ) + { + FT_UInt j; + + + FT_TRACE3( ( "%d %d %x %d\n ", + cur->platformID, + cur->encodingID, + cur->languageID, + cur->nameID ) ); + + /* I know that M$ encoded strings are Unicode, */ + /* but this works reasonable well for debugging purposes. */ + if ( cur->string ) { + for ( j = 0; j < cur->stringLength; j++ ) + { + FT_Char c = *( cur->string + j ); + + + if ( (FT_Byte)c < 128 ) { + FT_TRACE3( ( "%c", c ) ); + } + } + } + } + } + FT_TRACE3( ( "\n" ) ); + +#endif /* FT_DEBUG_LEVEL_TRACE */ + + } + FT_TRACE2( ( "loaded\n" ) ); + + /* everything went well, update face->num_names */ + face->num_names = names->numNameRecords; + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Free_Names */ +/* */ +/* */ +/* Frees the name records. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* */ +LOCAL_FUNC +void TT_Free_Names( TT_Face face ) { + FT_Memory memory = face->root.driver->root.memory; + TT_NameTable* names = &face->name_table; + + + /* free strings table */ + FREE( names->names ); + + /* free strings storage */ + FREE( names->storage ); + + names->numNameRecords = 0; + names->format = 0; + names->storageOffset = 0; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_CMap */ +/* */ +/* */ +/* Loads the cmap directory in a face object. The cmaps itselves are */ +/* loaded on demand in the `ttcmap.c' module. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* stream :: A handle to the input stream. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Load_CMap( TT_Face face, + FT_Stream stream ) { + FT_Error error; + FT_Memory memory = stream->memory; + FT_Long table_start; + TT_CMapDir cmap_dir; + + const FT_Frame_Field cmap_fields[] = + { + FT_FRAME_START( 4 ), + FT_FRAME_USHORT( TT_CMapDir, tableVersionNumber ), + FT_FRAME_USHORT( TT_CMapDir, numCMaps ), + FT_FRAME_END + }; + + const FT_Frame_Field cmap_rec_fields[] = + { + FT_FRAME_START( 6 ), + FT_FRAME_USHORT( TT_CMapTable, format ), + FT_FRAME_USHORT( TT_CMapTable, length ), + FT_FRAME_USHORT( TT_CMapTable, version ), + FT_FRAME_END + }; + + + FT_TRACE2( ( "CMaps " ) ); + + error = face->goto_table( face, TTAG_cmap, stream, 0 ); + if ( error ) { + error = TT_Err_CMap_Table_Missing; + goto Exit; + } + + table_start = FILE_Pos(); + + if ( READ_Fields( cmap_fields, &cmap_dir ) ) { + goto Exit; + } + + /* reserve space in face table for cmap tables */ + if ( ALLOC_ARRAY( face->charmaps, + cmap_dir.numCMaps, + TT_CharMapRec ) ) { + goto Exit; + } + + face->num_charmaps = cmap_dir.numCMaps; + { + TT_CharMap charmap = face->charmaps; + TT_CharMap limit = charmap + face->num_charmaps; + + + /* read the header of each charmap first */ + if ( ACCESS_Frame( face->num_charmaps * 8L ) ) { + goto Exit; + } + + for ( ; charmap < limit; charmap++ ) + { + TT_CMapTable* cmap; + + + charmap->root.face = (FT_Face)face; + cmap = &charmap->cmap; + + cmap->loaded = FALSE; + cmap->platformID = GET_UShort(); + cmap->platformEncodingID = GET_UShort(); + cmap->offset = (FT_ULong)GET_Long(); + } + + FORGET_Frame(); + + /* now read the rest of each table */ + for ( charmap = face->charmaps; charmap < limit; charmap++ ) + { + TT_CMapTable* cmap = &charmap->cmap; + + + if ( FILE_Seek( table_start + (FT_Long)cmap->offset ) || + READ_Fields( cmap_rec_fields, cmap ) ) { + goto Exit; + } + + cmap->offset = FILE_Pos(); + } + } + + FT_TRACE2( ( "loaded\n" ) ); + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_OS2 */ +/* */ +/* */ +/* Loads the OS2 table. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* stream :: A handle to the input stream. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Load_OS2( TT_Face face, + FT_Stream stream ) { + FT_Error error; + TT_OS2* os2; + + const FT_Frame_Field os2_fields[] = + { + FT_FRAME_START( 78 ), + FT_FRAME_USHORT( TT_OS2, version ), + FT_FRAME_SHORT( TT_OS2, xAvgCharWidth ), + FT_FRAME_USHORT( TT_OS2, usWeightClass ), + FT_FRAME_USHORT( TT_OS2, usWidthClass ), + FT_FRAME_SHORT( TT_OS2, fsType ), + FT_FRAME_SHORT( TT_OS2, ySubscriptXSize ), + FT_FRAME_SHORT( TT_OS2, ySubscriptYSize ), + FT_FRAME_SHORT( TT_OS2, ySubscriptXOffset ), + FT_FRAME_SHORT( TT_OS2, ySubscriptYOffset ), + FT_FRAME_SHORT( TT_OS2, ySuperscriptXSize ), + FT_FRAME_SHORT( TT_OS2, ySuperscriptYSize ), + FT_FRAME_SHORT( TT_OS2, ySuperscriptXOffset ), + FT_FRAME_SHORT( TT_OS2, ySuperscriptYOffset ), + FT_FRAME_SHORT( TT_OS2, yStrikeoutSize ), + FT_FRAME_SHORT( TT_OS2, yStrikeoutPosition ), + FT_FRAME_SHORT( TT_OS2, sFamilyClass ), + FT_FRAME_BYTE( TT_OS2, panose[0] ), + FT_FRAME_BYTE( TT_OS2, panose[1] ), + FT_FRAME_BYTE( TT_OS2, panose[2] ), + FT_FRAME_BYTE( TT_OS2, panose[3] ), + FT_FRAME_BYTE( TT_OS2, panose[4] ), + FT_FRAME_BYTE( TT_OS2, panose[5] ), + FT_FRAME_BYTE( TT_OS2, panose[6] ), + FT_FRAME_BYTE( TT_OS2, panose[7] ), + FT_FRAME_BYTE( TT_OS2, panose[8] ), + FT_FRAME_BYTE( TT_OS2, panose[9] ), + FT_FRAME_ULONG( TT_OS2, ulUnicodeRange1 ), + FT_FRAME_ULONG( TT_OS2, ulUnicodeRange2 ), + FT_FRAME_ULONG( TT_OS2, ulUnicodeRange3 ), + FT_FRAME_ULONG( TT_OS2, ulUnicodeRange4 ), + FT_FRAME_BYTE( TT_OS2, achVendID[0] ), + FT_FRAME_BYTE( TT_OS2, achVendID[1] ), + FT_FRAME_BYTE( TT_OS2, achVendID[2] ), + FT_FRAME_BYTE( TT_OS2, achVendID[3] ), + + FT_FRAME_USHORT( TT_OS2, fsSelection ), + FT_FRAME_USHORT( TT_OS2, usFirstCharIndex ), + FT_FRAME_USHORT( TT_OS2, usLastCharIndex ), + FT_FRAME_SHORT( TT_OS2, sTypoAscender ), + FT_FRAME_SHORT( TT_OS2, sTypoDescender ), + FT_FRAME_SHORT( TT_OS2, sTypoLineGap ), + FT_FRAME_USHORT( TT_OS2, usWinAscent ), + FT_FRAME_USHORT( TT_OS2, usWinDescent ), + FT_FRAME_END + }; + + const FT_Frame_Field os2_fields_extra[] = + { + FT_FRAME_START( 8 ), + FT_FRAME_ULONG( TT_OS2, ulCodePageRange1 ), + FT_FRAME_ULONG( TT_OS2, ulCodePageRange2 ), + FT_FRAME_END + }; + + const FT_Frame_Field os2_fields_extra2[] = + { + FT_FRAME_START( 10 ), + FT_FRAME_SHORT( TT_OS2, sxHeight ), + FT_FRAME_SHORT( TT_OS2, sCapHeight ), + FT_FRAME_USHORT( TT_OS2, usDefaultChar ), + FT_FRAME_USHORT( TT_OS2, usBreakChar ), + FT_FRAME_USHORT( TT_OS2, usMaxContext ), + FT_FRAME_END + }; + + + FT_TRACE2( ( "OS/2 Table " ) ); + + /* We now support old Mac fonts where the OS/2 table doesn't */ + /* exist. Simply put, we set the `version' field to 0xFFFF */ + /* and test this value each time we need to access the table. */ + error = face->goto_table( face, TTAG_OS2, stream, 0 ); + if ( error ) { + FT_TRACE2( ( "is missing!\n" ) ); + face->os2.version = 0xFFFF; + error = TT_Err_Ok; + goto Exit; + } + + os2 = &face->os2; + + if ( READ_Fields( os2_fields, os2 ) ) { + goto Exit; + } + + os2->ulCodePageRange1 = 0; + os2->ulCodePageRange2 = 0; + + if ( os2->version >= 0x0001 ) { + /* only version 1 tables */ + if ( READ_Fields( os2_fields_extra, os2 ) ) { + goto Exit; + } + + if ( os2->version >= 0x0002 ) { + /* only version 2 tables */ + if ( READ_Fields( os2_fields_extra2, os2 ) ) { + goto Exit; + } + } + } + + FT_TRACE2( ( "loaded\n" ) ); + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_Postscript */ +/* */ +/* */ +/* Loads the Postscript table. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* stream :: A handle to the input stream. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Load_PostScript( TT_Face face, + FT_Stream stream ) { + FT_Error error; + TT_Postscript* post = &face->postscript; + + static const FT_Frame_Field post_fields[] = + { + FT_FRAME_START( 32 ), + FT_FRAME_ULONG( TT_Postscript, FormatType ), + FT_FRAME_ULONG( TT_Postscript, italicAngle ), + FT_FRAME_SHORT( TT_Postscript, underlinePosition ), + FT_FRAME_SHORT( TT_Postscript, underlineThickness ), + FT_FRAME_ULONG( TT_Postscript, isFixedPitch ), + FT_FRAME_ULONG( TT_Postscript, minMemType42 ), + FT_FRAME_ULONG( TT_Postscript, maxMemType42 ), + FT_FRAME_ULONG( TT_Postscript, minMemType1 ), + FT_FRAME_ULONG( TT_Postscript, maxMemType1 ), + FT_FRAME_END + }; + + + FT_TRACE2( ( "PostScript " ) ); + + error = face->goto_table( face, TTAG_post, stream, 0 ); + if ( error ) { + return TT_Err_Post_Table_Missing; + } + + if ( READ_Fields( post_fields, post ) ) { + return error; + } + + /* we don't load the glyph names, we do that in another */ + /* module (ttpost). */ + FT_TRACE2( ( "loaded\n" ) ); + + return TT_Err_Ok; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_PCLT */ +/* */ +/* */ +/* Loads the PCL 5 Table. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* stream :: A handle to the input stream. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Load_PCLT( TT_Face face, + FT_Stream stream ) { + static const FT_Frame_Field pclt_fields[] = + { + FT_FRAME_START( 54 ), + FT_FRAME_ULONG( TT_PCLT, Version ), + FT_FRAME_ULONG( TT_PCLT, FontNumber ), + FT_FRAME_USHORT( TT_PCLT, Pitch ), + FT_FRAME_USHORT( TT_PCLT, xHeight ), + FT_FRAME_USHORT( TT_PCLT, Style ), + FT_FRAME_USHORT( TT_PCLT, TypeFamily ), + FT_FRAME_USHORT( TT_PCLT, CapHeight ), + FT_FRAME_BYTES( TT_PCLT, TypeFace, 16 ), + FT_FRAME_BYTES( TT_PCLT, CharacterComplement, 8 ), + FT_FRAME_BYTES( TT_PCLT, FileName, 6 ), + FT_FRAME_CHAR( TT_PCLT, StrokeWeight ), + FT_FRAME_CHAR( TT_PCLT, WidthType ), + FT_FRAME_BYTE( TT_PCLT, SerifStyle ), + FT_FRAME_BYTE( TT_PCLT, Reserved ), + FT_FRAME_END + }; + + FT_Error error; + TT_PCLT* pclt = &face->pclt; + + + FT_TRACE2( ( "PCLT " ) ); + + /* optional table */ + error = face->goto_table( face, TTAG_PCLT, stream, 0 ); + if ( error ) { + FT_TRACE2( ( "missing (optional)\n" ) ); + pclt->Version = 0; + return TT_Err_Ok; + } + + if ( READ_Fields( pclt_fields, pclt ) ) { + goto Exit; + } + + FT_TRACE2( ( "loaded\n" ) ); + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_Gasp */ +/* */ +/* */ +/* Loads the `gasp' table into a face object. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* stream :: The input stream. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Load_Gasp( TT_Face face, + FT_Stream stream ) { + FT_Error error; + FT_Memory memory = stream->memory; + + FT_UInt j,num_ranges; + TT_GaspRange* gaspranges; + + + FT_TRACE2( ( "TT_Load_Gasp: %08p\n", face ) ); + + /* the gasp table is optional */ + error = face->goto_table( face, TTAG_gasp, stream, 0 ); + if ( error ) { + return TT_Err_Ok; + } + + if ( ACCESS_Frame( 4L ) ) { + goto Exit; + } + + face->gasp.version = GET_UShort(); + face->gasp.numRanges = GET_UShort(); + + FORGET_Frame(); + + num_ranges = face->gasp.numRanges; + FT_TRACE3( ( "number of ranges = %d\n", num_ranges ) ); + + if ( ALLOC_ARRAY( gaspranges, num_ranges, TT_GaspRange ) || + ACCESS_Frame( num_ranges * 4L ) ) { + goto Exit; + } + + face->gasp.gaspRanges = gaspranges; + + for ( j = 0; j < num_ranges; j++ ) + { + gaspranges[j].maxPPEM = GET_UShort(); + gaspranges[j].gaspFlag = GET_UShort(); + + FT_TRACE3( ( " [max:%d flag:%d]", + gaspranges[j].maxPPEM, + gaspranges[j].gaspFlag ) ); + } + FT_TRACE3( ( "\n" ) ); + + FORGET_Frame(); + FT_TRACE2( ( "GASP loaded\n" ) ); + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_Kern */ +/* */ +/* */ +/* Loads the first kerning table with format 0 in the font. Only */ +/* accepts the first horizontal kerning table. Developers should use */ +/* the `ftxkern' extension to access other kerning tables in the font */ +/* file, if they really want to. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* stream :: The input stream. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Load_Kern( TT_Face face, + FT_Stream stream ) { + FT_Error error; + FT_Memory memory = stream->memory; + + FT_UInt n, num_tables, version; + + + /* the kern table is optional; exit silently if it is missing */ + error = face->goto_table( face, TTAG_kern, stream, 0 ); + if ( error ) { + return TT_Err_Ok; + } + + if ( ACCESS_Frame( 4L ) ) { + goto Exit; + } + + version = GET_UShort(); + num_tables = GET_UShort(); + + FORGET_Frame(); + + for ( n = 0; n < num_tables; n++ ) + { + FT_UInt coverage; + FT_UInt length; + + + if ( ACCESS_Frame( 6L ) ) { + goto Exit; + } + + version = GET_UShort(); /* version */ + length = GET_UShort() - 6; /* substract header length */ + coverage = GET_UShort(); + + FORGET_Frame(); + + if ( coverage == 0x0001 ) { + FT_UInt num_pairs; + TT_Kern_0_Pair* pair; + TT_Kern_0_Pair* limit; + + + /* found a horizontal format 0 kerning table! */ + if ( ACCESS_Frame( 8L ) ) { + goto Exit; + } + + num_pairs = GET_UShort(); + + /* skip the rest */ + + FORGET_Frame(); + + /* allocate array of kerning pairs */ + if ( ALLOC_ARRAY( face->kern_pairs, num_pairs, TT_Kern_0_Pair ) || + ACCESS_Frame( 6L * num_pairs ) ) { + goto Exit; + } + + pair = face->kern_pairs; + limit = pair + num_pairs; + for ( ; pair < limit; pair++ ) + { + pair->left = GET_UShort(); + pair->right = GET_UShort(); + pair->value = GET_UShort(); + } + + FORGET_Frame(); + + face->num_kern_pairs = num_pairs; + face->kern_table_index = n; + goto Exit; + } + + if ( FILE_Skip( length ) ) { + goto Exit; + } + } + + /* no kern table found -- doesn't matter */ + face->kern_table_index = -1; + face->num_kern_pairs = 0; + face->kern_pairs = NULL; + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_Hdmx */ +/* */ +/* */ +/* Loads the horizontal device metrics table. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* stream :: A handle to the input stream. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Load_Hdmx( TT_Face face, + FT_Stream stream ) { + FT_Error error; + FT_Memory memory = stream->memory; + + TT_Hdmx* hdmx = &face->hdmx; + FT_Long num_glyphs; + FT_Long record_size; + + + hdmx->version = 0; + hdmx->num_records = 0; + hdmx->records = 0; + + /* this table is optional */ + error = face->goto_table( face, TTAG_hdmx, stream, 0 ); + if ( error ) { + return TT_Err_Ok; + } + + if ( ACCESS_Frame( 8L ) ) { + goto Exit; + } + + hdmx->version = GET_UShort(); + hdmx->num_records = GET_Short(); + record_size = GET_Long(); + + FORGET_Frame(); + + /* Only recognize format 0 */ + if ( hdmx->version != 0 ) { + goto Exit; + } + + if ( ALLOC_ARRAY( hdmx->records, hdmx->num_records, TT_HdmxRec ) ) { + goto Exit; + } + + num_glyphs = face->root.num_glyphs; + record_size -= num_glyphs + 2; + + { + TT_HdmxRec* cur = hdmx->records; + TT_HdmxRec* limit = cur + hdmx->num_records; + + + for ( ; cur < limit; cur++ ) + { + /* read record */ + if ( READ_Byte( cur->ppem ) || + READ_Byte( cur->max_width ) ) { + goto Exit; + } + + if ( ALLOC( cur->widths, num_glyphs ) || + FILE_Read( cur->widths, num_glyphs ) ) { + goto Exit; + } + + /* skip padding bytes */ + if ( record_size > 0 && FILE_Skip( record_size ) ) { + goto Exit; + } + } + } + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Free_Hdmx */ +/* */ +/* */ +/* Frees the horizontal device metrics table. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* */ +LOCAL_FUNC +void TT_Free_Hdmx( TT_Face face ) { + if ( face ) { + FT_Int n; + FT_Memory memory = face->root.driver->root.memory; + + + for ( n = 0; n < face->hdmx.num_records; n++ ) + FREE( face->hdmx.records[n].widths ); + + FREE( face->hdmx.records ); + face->hdmx.num_records = 0; + } +} + + +/* END */ diff --git a/src/ft2/ttload.h b/src/ft2/ttload.h new file mode 100644 index 0000000..96499fb --- /dev/null +++ b/src/ft2/ttload.h @@ -0,0 +1,132 @@ +/***************************************************************************/ +/* */ +/* ttload.h */ +/* */ +/* Load the basic TrueType tables, i.e., tables that can be either in */ +/* TTF or OTF fonts (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef TTLOAD_H +#define TTLOAD_H + + +#include "ftstream.h" +#include "tttypes.h" + + +#ifdef __cplusplus +extern "C" { +#endif + + +LOCAL_DEF +TT_Table* TT_LookUp_Table( TT_Face face, + FT_ULong tag ); + +LOCAL_DEF +FT_Error TT_Goto_Table( TT_Face face, + FT_ULong tag, + FT_Stream stream, + FT_ULong* length ); + + +LOCAL_DEF +FT_Error TT_Load_SFNT_Header( TT_Face face, + FT_Stream stream, + FT_Long face_index, + SFNT_Header* sfnt ); +LOCAL_DEF +FT_Error TT_Load_Directory( TT_Face face, + FT_Stream stream, + SFNT_Header* sfnt ); + +LOCAL_DEF +FT_Error TT_Load_Any( TT_Face face, + FT_ULong tag, + FT_Long offset, + FT_Byte* buffer, + FT_ULong* length ); + + +LOCAL_DEF +FT_Error TT_Load_Header( TT_Face face, + FT_Stream stream ); + + +LOCAL_DEF +FT_Error TT_Load_Metrics_Header( TT_Face face, + FT_Stream stream, + FT_Bool vertical ); + + +LOCAL_DEF +FT_Error TT_Load_CMap( TT_Face face, + FT_Stream stream ); + + +LOCAL_DEF +FT_Error TT_Load_MaxProfile( TT_Face face, + FT_Stream stream ); + + +LOCAL_DEF +FT_Error TT_Load_Names( TT_Face face, + FT_Stream stream ); + + +LOCAL_DEF +FT_Error TT_Load_OS2( TT_Face face, + FT_Stream stream ); + + +LOCAL_DEF +FT_Error TT_Load_PostScript( TT_Face face, + FT_Stream stream ); + + +LOCAL_DEF +FT_Error TT_Load_Hdmx( TT_Face face, + FT_Stream stream ); + +LOCAL_DEF +FT_Error TT_Load_PCLT( TT_Face face, + FT_Stream stream ); + +LOCAL_DEF +void TT_Free_Names( TT_Face face ); + + +LOCAL_DEF +void TT_Free_Hdmx( TT_Face face ); + + +LOCAL_DEF +FT_Error TT_Load_Kern( TT_Face face, + FT_Stream stream ); + + +LOCAL_DEF +FT_Error TT_Load_Gasp( TT_Face face, + FT_Stream stream ); + + +#ifdef __cplusplus +} +#endif + + +#endif /* TTLOAD_H */ + + +/* END */ diff --git a/src/ft2/ttnameid.h b/src/ft2/ttnameid.h new file mode 100644 index 0000000..feb94ea --- /dev/null +++ b/src/ft2/ttnameid.h @@ -0,0 +1,698 @@ +/***************************************************************************/ +/* */ +/* ttmakeid.h */ +/* */ +/* TrueType name ID definitions (specification only). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef TTNAMEID_H +#define TTNAMEID_H + + +/*************************************************************************/ +/* */ +/* Possible values for the `platform' identifier code in the name */ +/* records of the TTF `name' table. */ +/* */ +#define TT_PLATFORM_APPLE_UNICODE 0 +#define TT_PLATFORM_MACINTOSH 1 +#define TT_PLATFORM_ISO 2 /* deprecated */ +#define TT_PLATFORM_MICROSOFT 3 + + +/*************************************************************************/ +/* */ +/* Possible values of the platform specific encoding identifier field in */ +/* the name records of the TTF `name' table if the `platform' identifier */ +/* code is TT_PLATFORM_APPLE_UNICODE. */ +/* */ +#define TT_APPLE_ID_DEFAULT 0 +#define TT_APPLE_ID_UNICODE_1_1 1 /* specify Hangul at U+34xx */ +#define TT_APPLE_ID_ISO_10646 2 /* deprecated */ +#define TT_APPLE_ID_UNICODE_2_0 3 /* or later */ + + +/*************************************************************************/ +/* */ +/* Possible values of the platform specific encoding identifier field in */ +/* the name records of the TTF `name' table if the `platform' identifier */ +/* code is TT_PLATFORM_MACINTOSH. */ +/* */ +#define TT_MAC_ID_ROMAN 0 +#define TT_MAC_ID_JAPANESE 1 +#define TT_MAC_ID_TRADITIONAL_CHINESE 2 +#define TT_MAC_ID_KOREAN 3 +#define TT_MAC_ID_ARABIC 4 +#define TT_MAC_ID_HEBREW 5 +#define TT_MAC_ID_GREEK 6 +#define TT_MAC_ID_RUSSIAN 7 +#define TT_MAC_ID_RSYMBOL 8 +#define TT_MAC_ID_DEVANAGARI 9 +#define TT_MAC_ID_GURMUKHI 10 +#define TT_MAC_ID_GUJARATI 11 +#define TT_MAC_ID_ORIYA 12 +#define TT_MAC_ID_BENGALI 13 +#define TT_MAC_ID_TAMIL 14 +#define TT_MAC_ID_TELUGU 15 +#define TT_MAC_ID_KANNADA 16 +#define TT_MAC_ID_MALAYALAM 17 +#define TT_MAC_ID_SINHALESE 18 +#define TT_MAC_ID_BURMESE 19 +#define TT_MAC_ID_KHMER 20 +#define TT_MAC_ID_THAI 21 +#define TT_MAC_ID_LAOTIAN 22 +#define TT_MAC_ID_GEORGIAN 23 +#define TT_MAC_ID_ARMENIAN 24 +#define TT_MAC_ID_MALDIVIAN 25 +#define TT_MAC_ID_SIMPLIFIED_CHINESE 25 +#define TT_MAC_ID_TIBETAN 26 +#define TT_MAC_ID_MONGOLIAN 27 +#define TT_MAC_ID_GEEZ 28 +#define TT_MAC_ID_SLAVIC 29 +#define TT_MAC_ID_VIETNAMESE 30 +#define TT_MAC_ID_SINDHI 31 +#define TT_MAC_ID_UNINTERP 32 + + +/*************************************************************************/ +/* */ +/* Possible values of the platform specific encoding identifier field in */ +/* the name records of the TTF `name' table if the `platform' identifier */ +/* code is TT_PLATFORM_ISO. */ +/* */ +/* This use is now deprecated. */ +/* */ +#define TT_ISO_ID_7BIT_ASCII 0 +#define TT_ISO_ID_10646 1 +#define TT_ISO_ID_8859_1 2 + + +/*************************************************************************/ +/* */ +/* possible values of the platform specific encoding identifier field in */ +/* the name records of the TTF `name' table if the `platform' identifier */ +/* code is TT_PLATFORM_MICROSOFT. */ +/* */ +#define TT_MS_ID_SYMBOL_CS 0 +#define TT_MS_ID_UNICODE_CS 1 +#define TT_MS_ID_SJIS 2 +#define TT_MS_ID_GB2312 3 +#define TT_MS_ID_BIG_5 4 +#define TT_MS_ID_WANSUNG 5 +#define TT_MS_ID_JOHAB 6 + + +/*************************************************************************/ +/* */ +/* Possible values of the language identifier field in the name records */ +/* of the TTF `name' table if the `platform' identifier code is */ +/* TT_PLATFORM_MACINTOSH. */ +/* */ +/* The canonical source for the Apple assigned Language ID's is at */ +/* */ +/* http://fonts.apple.com/TTRefMan/RM06/Chap6name.html */ +/* */ +#define TT_MAC_LANGID_ENGLISH 0 +#define TT_MAC_LANGID_FRENCH 1 +#define TT_MAC_LANGID_GERMAN 2 +#define TT_MAC_LANGID_ITALIAN 3 +#define TT_MAC_LANGID_DUTCH 4 +#define TT_MAC_LANGID_SWEDISH 5 +#define TT_MAC_LANGID_SPANISH 6 +#define TT_MAC_LANGID_DANISH 7 +#define TT_MAC_LANGID_PORTUGUESE 8 +#define TT_MAC_LANGID_NORWEGIAN 9 +#define TT_MAC_LANGID_HEBREW 10 +#define TT_MAC_LANGID_JAPANESE 11 +#define TT_MAC_LANGID_ARABIC 12 +#define TT_MAC_LANGID_FINNISH 13 +#define TT_MAC_LANGID_GREEK 14 +#define TT_MAC_LANGID_ICELANDIC 15 +#define TT_MAC_LANGID_MALTESE 16 +#define TT_MAC_LANGID_TURKISH 17 +#define TT_MAC_LANGID_CROATIAN 18 +#define TT_MAC_LANGID_CHINESE_TRADITIONAL 19 +#define TT_MAC_LANGID_URDU 20 +#define TT_MAC_LANGID_HINDI 21 +#define TT_MAC_LANGID_THAI 22 +#define TT_MAC_LANGID_KOREAN 23 +#define TT_MAC_LANGID_LITHUANIAN 24 +#define TT_MAC_LANGID_POLISH 25 +#define TT_MAC_LANGID_HUNGARIAN 26 +#define TT_MAC_LANGID_ESTONIAN 27 +#define TT_MAC_LANGID_LETTISH 28 +#define TT_MAC_LANGID_SAAMISK 29 +#define TT_MAC_LANGID_FAEROESE 30 +#define TT_MAC_LANGID_FARSI 31 +#define TT_MAC_LANGID_RUSSIAN 32 +#define TT_MAC_LANGID_CHINESE_SIMPLIFIED 33 +#define TT_MAC_LANGID_FLEMISH 34 +#define TT_MAC_LANGID_IRISH 35 +#define TT_MAC_LANGID_ALBANIAN 36 +#define TT_MAC_LANGID_ROMANIAN 37 +#define TT_MAC_LANGID_CZECH 38 +#define TT_MAC_LANGID_SLOVAK 39 +#define TT_MAC_LANGID_SLOVENIAN 40 +#define TT_MAC_LANGID_YIDDISH 41 +#define TT_MAC_LANGID_SERBIAN 42 +#define TT_MAC_LANGID_MACEDONIAN 43 +#define TT_MAC_LANGID_BULGARIAN 44 +#define TT_MAC_LANGID_UKRAINIAN 45 +#define TT_MAC_LANGID_BYELORUSSIAN 46 +#define TT_MAC_LANGID_UZBEK 47 +#define TT_MAC_LANGID_KAZAKH 48 +#define TT_MAC_LANGID_AZERBAIJANI 49 +#define TT_MAC_LANGID_AZERBAIJANI_CYRILLIC_SCRIPT 49 +#define TT_MAC_LANGID_AZERBAIJANI_ARABIC_SCRIPT 50 +#define TT_MAC_LANGID_ARMENIAN 51 +#define TT_MAC_LANGID_GEORGIAN 52 +#define TT_MAC_LANGID_MOLDAVIAN 53 +#define TT_MAC_LANGID_KIRGHIZ 54 +#define TT_MAC_LANGID_TAJIKI 55 +#define TT_MAC_LANGID_TURKMEN 56 +#define TT_MAC_LANGID_MONGOLIAN 57 +#define TT_MAC_LANGID_MONGOLIAN_MONGOLIAN_SCRIPT 57 +#define TT_MAC_LANGID_MONGOLIAN_CYRILLIC_SCRIPT 58 +#define TT_MAC_LANGID_PASHTO 59 +#define TT_MAC_LANGID_KURDISH 60 +#define TT_MAC_LANGID_KASHMIRI 61 +#define TT_MAC_LANGID_SINDHI 62 +#define TT_MAC_LANGID_TIBETAN 63 +#define TT_MAC_LANGID_NEPALI 64 +#define TT_MAC_LANGID_SANSKRIT 65 +#define TT_MAC_LANGID_MARATHI 66 +#define TT_MAC_LANGID_BENGALI 67 +#define TT_MAC_LANGID_ASSAMESE 68 +#define TT_MAC_LANGID_GUJARATI 69 +#define TT_MAC_LANGID_PUNJABI 70 +#define TT_MAC_LANGID_ORIYA 71 +#define TT_MAC_LANGID_MALAYALAM 72 +#define TT_MAC_LANGID_KANNADA 73 +#define TT_MAC_LANGID_TAMIL 74 +#define TT_MAC_LANGID_TELUGU 75 +#define TT_MAC_LANGID_SINHALESE 76 +#define TT_MAC_LANGID_BURMESE 77 +#define TT_MAC_LANGID_KHMER 78 +#define TT_MAC_LANGID_LAO 79 +#define TT_MAC_LANGID_VIETNAMESE 80 +#define TT_MAC_LANGID_INDONESIAN 81 +#define TT_MAC_LANGID_TAGALOG 82 +#define TT_MAC_LANGID_MALAY_ROMAN_SCRIPT 83 +#define TT_MAC_LANGID_MALAY_ARABIC_SCRIPT 84 +#define TT_MAC_LANGID_AMHARIC 85 +#define TT_MAC_LANGID_TIGRINYA 86 +#define TT_MAC_LANGID_GALLA 87 +#define TT_MAC_LANGID_SOMALI 88 +#define TT_MAC_LANGID_SWAHILI 89 +#define TT_MAC_LANGID_RUANDA 90 +#define TT_MAC_LANGID_RUNDI 91 +#define TT_MAC_LANGID_CHEWA 92 +#define TT_MAC_LANGID_MALAGASY 93 +#define TT_MAC_LANGID_ESPERANTO 94 +#define TT_MAC_LANGID_WELSH 128 +#define TT_MAC_LANGID_BASQUE 129 +#define TT_MAC_LANGID_CATALAN 130 +#define TT_MAC_LANGID_LATIN 131 +#define TT_MAC_LANGID_QUECHUA 132 +#define TT_MAC_LANGID_GUARANI 133 +#define TT_MAC_LANGID_AYMARA 134 +#define TT_MAC_LANGID_TATAR 135 +#define TT_MAC_LANGID_UIGHUR 136 +#define TT_MAC_LANGID_DZONGKHA 137 +#define TT_MAC_LANGID_JAVANESE 138 +#define TT_MAC_LANGID_SUNDANESE 139 + + +#if 0 /* these seem to be errors that have been dropped */ + +#define TT_MAC_LANGID_SCOTTISH_GAELIC 140 +#define TT_MAC_LANGID_IRISH_GAELIC 141 + +#endif + + +/* The following codes are new as of 2000-03-10 */ +#define TT_MAC_LANGID_GALICIAN 140 +#define TT_MAC_LANGID_AFRIKAANS 141 +#define TT_MAC_LANGID_BRETON 142 +#define TT_MAC_LANGID_INUKTITUT 143 +#define TT_MAC_LANGID_SCOTTISH_GAELIC 144 +#define TT_MAC_LANGID_MANX_GAELIC 145 +#define TT_MAC_LANGID_IRISH_GAELIC 146 +#define TT_MAC_LANGID_TONGAN 147 +#define TT_MAC_LANGID_GREEK_POLYTONIC 148 +#define TT_MAC_LANGID_GREELANDIC 149 +#define TT_MAC_LANGID_AZERBAIJANI_ROMAN_SCRIPT 150 + + +/*************************************************************************/ +/* */ +/* Possible values of the language identifier field in the name records */ +/* of the TTF `name' table if the `platform' identifier code is */ +/* TT_PLATFORM_MICROSOFT. */ +/* */ +/* The canonical source for the MS assigned LCID's is at */ +/* */ +/* http://www.microsoft.com/typography/OTSPEC/lcid-cp.txt */ +/* */ +#define TT_MS_LANGID_ARABIC_SAUDI_ARABIA 0x0401 +#define TT_MS_LANGID_ARABIC_IRAQ 0x0801 +#define TT_MS_LANGID_ARABIC_EGYPT 0x0c01 +#define TT_MS_LANGID_ARABIC_LIBYA 0x1001 +#define TT_MS_LANGID_ARABIC_ALGERIA 0x1401 +#define TT_MS_LANGID_ARABIC_MOROCCO 0x1801 +#define TT_MS_LANGID_ARABIC_TUNISIA 0x1c01 +#define TT_MS_LANGID_ARABIC_OMAN 0x2001 +#define TT_MS_LANGID_ARABIC_YEMEN 0x2401 +#define TT_MS_LANGID_ARABIC_SYRIA 0x2801 +#define TT_MS_LANGID_ARABIC_JORDAN 0x2c01 +#define TT_MS_LANGID_ARABIC_LEBANON 0x3001 +#define TT_MS_LANGID_ARABIC_KUWAIT 0x3401 +#define TT_MS_LANGID_ARABIC_UAE 0x3801 +#define TT_MS_LANGID_ARABIC_BAHRAIN 0x3c01 +#define TT_MS_LANGID_ARABIC_QATAR 0x4001 +#define TT_MS_LANGID_BULGARIAN_BULGARIA 0x0402 +#define TT_MS_LANGID_CATALAN_SPAIN 0x0403 +#define TT_MS_LANGID_CHINESE_TAIWAN 0x0404 +#define TT_MS_LANGID_CHINESE_PRC 0x0804 +#define TT_MS_LANGID_CHINESE_HONG_KONG 0x0c04 +#define TT_MS_LANGID_CHINESE_SINGAPORE 0x1004 +#define TT_MS_LANGID_CHINESE_MACAU 0x1404 +#define TT_MS_LANGID_CZECH_CZECH_REPUBLIC 0x0405 +#define TT_MS_LANGID_DANISH_DENMARK 0x0406 +#define TT_MS_LANGID_GERMAN_GERMANY 0x0407 +#define TT_MS_LANGID_GERMAN_SWITZERLAND 0x0807 +#define TT_MS_LANGID_GERMAN_AUSTRIA 0x0c07 +#define TT_MS_LANGID_GERMAN_LUXEMBOURG 0x1007 +#define TT_MS_LANGID_GERMAN_LIECHTENSTEI 0x1407 +#define TT_MS_LANGID_GREEK_GREECE 0x0408 +#define TT_MS_LANGID_ENGLISH_UNITED_STATES 0x0409 +#define TT_MS_LANGID_ENGLISH_UNITED_KINGDOM 0x0809 +#define TT_MS_LANGID_ENGLISH_AUSTRALIA 0x0c09 +#define TT_MS_LANGID_ENGLISH_CANADA 0x1009 +#define TT_MS_LANGID_ENGLISH_NEW_ZEALAND 0x1409 +#define TT_MS_LANGID_ENGLISH_IRELAND 0x1809 +#define TT_MS_LANGID_ENGLISH_SOUTH_AFRICA 0x1c09 +#define TT_MS_LANGID_ENGLISH_JAMAICA 0x2009 +#define TT_MS_LANGID_ENGLISH_CARIBBEAN 0x2409 +#define TT_MS_LANGID_ENGLISH_BELIZE 0x2809 +#define TT_MS_LANGID_ENGLISH_TRINIDAD 0x2c09 +#define TT_MS_LANGID_ENGLISH_ZIMBABWE 0x3009 +#define TT_MS_LANGID_ENGLISH_PHILIPPINES 0x3409 +#define TT_MS_LANGID_SPANISH_SPAIN_TRADITIONAL_SORT 0x040a +#define TT_MS_LANGID_SPANISH_MEXICO 0x080a +#define TT_MS_LANGID_SPANISH_SPAIN_INTERNATIONAL_SORT 0x0c0a +#define TT_MS_LANGID_SPANISH_GUATEMALA 0x100a +#define TT_MS_LANGID_SPANISH_COSTA_RICA 0x140a +#define TT_MS_LANGID_SPANISH_PANAMA 0x180a +#define TT_MS_LANGID_SPANISH_DOMINICAN_REPUBLIC 0x1c0a +#define TT_MS_LANGID_SPANISH_VENEZUELA 0x200a +#define TT_MS_LANGID_SPANISH_COLOMBIA 0x240a +#define TT_MS_LANGID_SPANISH_PERU 0x280a +#define TT_MS_LANGID_SPANISH_ARGENTINA 0x2c0a +#define TT_MS_LANGID_SPANISH_ECUADOR 0x300a +#define TT_MS_LANGID_SPANISH_CHILE 0x340a +#define TT_MS_LANGID_SPANISH_URUGUAY 0x380a +#define TT_MS_LANGID_SPANISH_PARAGUAY 0x3c0a +#define TT_MS_LANGID_SPANISH_BOLIVIA 0x400a +#define TT_MS_LANGID_SPANISH_EL_SALVADOR 0x440a +#define TT_MS_LANGID_SPANISH_HONDURAS 0x480a +#define TT_MS_LANGID_SPANISH_NICARAGUA 0x4c0a +#define TT_MS_LANGID_SPANISH_PUERTO_RICO 0x500a +#define TT_MS_LANGID_FINNISH_FINLAND 0x040b +#define TT_MS_LANGID_FRENCH_FRANCE 0x040c +#define TT_MS_LANGID_FRENCH_BELGIUM 0x080c +#define TT_MS_LANGID_FRENCH_CANADA 0x0c0c +#define TT_MS_LANGID_FRENCH_SWITZERLAND 0x100c +#define TT_MS_LANGID_FRENCH_LUXEMBOURG 0x140c +#define TT_MS_LANGID_FRENCH_MONACO 0x180c +#define TT_MS_LANGID_HEBREW_ISRAEL 0x040d +#define TT_MS_LANGID_HUNGARIAN_HUNGARY 0x040e +#define TT_MS_LANGID_ICELANDIC_ICELAND 0x040f +#define TT_MS_LANGID_ITALIAN_ITALY 0x0410 +#define TT_MS_LANGID_ITALIAN_SWITZERLAND 0x0810 +#define TT_MS_LANGID_JAPANESE_JAPAN 0x0411 +#define TT_MS_LANGID_KOREAN_EXTENDED_WANSUNG_KOREA 0x0412 +#define TT_MS_LANGID_KOREAN_JOHAB_KOREA 0x0812 +#define TT_MS_LANGID_DUTCH_NETHERLANDS 0x0413 +#define TT_MS_LANGID_DUTCH_BELGIUM 0x0813 +#define TT_MS_LANGID_NORWEGIAN_NORWAY_BOKMAL 0x0414 +#define TT_MS_LANGID_NORWEGIAN_NORWAY_NYNORSK 0x0814 +#define TT_MS_LANGID_POLISH_POLAND 0x0415 +#define TT_MS_LANGID_PORTUGUESE_BRAZIL 0x0416 +#define TT_MS_LANGID_PORTUGUESE_PORTUGAL 0x0816 +#define TT_MS_LANGID_RHAETO_ROMANIC_SWITZERLAND 0x0417 +#define TT_MS_LANGID_ROMANIAN_ROMANIA 0x0418 +#define TT_MS_LANGID_MOLDAVIAN_MOLDAVIA 0x0818 +#define TT_MS_LANGID_RUSSIAN_RUSSIA 0x0419 +#define TT_MS_LANGID_RUSSIAN_MOLDAVIA 0x0819 +#define TT_MS_LANGID_CROATIAN_CROATIA 0x041a +#define TT_MS_LANGID_SERBIAN_SERBIA_LATIN 0x081a +#define TT_MS_LANGID_SERBIAN_SERBIA_CYRILLIC 0x0c1a +#define TT_MS_LANGID_SLOVAK_SLOVAKIA 0x041b +#define TT_MS_LANGID_ALBANIAN_ALBANIA 0x041c +#define TT_MS_LANGID_SWEDISH_SWEDEN 0x041d +#define TT_MS_LANGID_SWEDISH_FINLAND 0x081d +#define TT_MS_LANGID_THAI_THAILAND 0x041e +#define TT_MS_LANGID_TURKISH_TURKEY 0x041f +#define TT_MS_LANGID_URDU_PAKISTAN 0x0420 +#define TT_MS_LANGID_INDONESIAN_INDONESIA 0x0421 +#define TT_MS_LANGID_UKRAINIAN_UKRAINE 0x0422 +#define TT_MS_LANGID_BELARUSIAN_BELARUS 0x0423 +#define TT_MS_LANGID_SLOVENE_SLOVENIA 0x0424 +#define TT_MS_LANGID_ESTONIAN_ESTONIA 0x0425 +#define TT_MS_LANGID_LATVIAN_LATVIA 0x0426 +#define TT_MS_LANGID_LITHUANIAN_LITHUANIA 0x0427 +#define TT_MS_LANGID_CLASSIC_LITHUANIAN_LITHUANIA 0x0827 +#define TT_MS_LANGID_MAORI_NEW_ZEALAND 0x0428 +#define TT_MS_LANGID_FARSI_IRAN 0x0429 +#define TT_MS_LANGID_VIETNAMESE_VIET_NAM 0x042a +#define TT_MS_LANGID_ARMENIAN_ARMENIA 0x042b +#define TT_MS_LANGID_AZERI_AZERBAIJAN_LATIN 0x042c +#define TT_MS_LANGID_AZERI_AZERBAIJAN_CYRILLIC 0x082c +#define TT_MS_LANGID_BASQUE_SPAIN 0x042d +#define TT_MS_LANGID_SORBIAN_GERMANY 0x042e +#define TT_MS_LANGID_MACEDONIAN_MACEDONIA 0x042f +#define TT_MS_LANGID_SUTU_SOUTH_AFRICA 0x0430 +#define TT_MS_LANGID_TSONGA_SOUTH_AFRICA 0x0431 +#define TT_MS_LANGID_TSWANA_SOUTH_AFRICA 0x0432 +#define TT_MS_LANGID_VENDA_SOUTH_AFRICA 0x0433 +#define TT_MS_LANGID_XHOSA_SOUTH_AFRICA 0x0434 +#define TT_MS_LANGID_ZULU_SOUTH_AFRICA 0x0435 +#define TT_MS_LANGID_AFRIKAANS_SOUTH_AFRICA 0x0436 +#define TT_MS_LANGID_GEORGIAN_GEORGIA 0x0437 +#define TT_MS_LANGID_FAEROESE_FAEROE_ISLANDS 0x0438 +#define TT_MS_LANGID_HINDI_INDIA 0x0439 +#define TT_MS_LANGID_MALTESE_MALTA 0x043a +#define TT_MS_LANGID_SAAMI_LAPONIA 0x043b +#define TT_MS_LANGID_IRISH_GAELIC_IRELAND 0x043c +#define TT_MS_LANGID_SCOTTISH_GAELIC_UNITED_KINGDOM 0x083c +#define TT_MS_LANGID_MALAY_MALAYSIA 0x043e +#define TT_MS_LANGID_MALAY_BRUNEI_DARUSSALAM 0x083e +#define TT_MS_LANGID_KAZAK_KAZAKSTAN 0x043f +#define TT_MS_LANGID_SWAHILI_KENYA 0x0441 +#define TT_MS_LANGID_UZBEK_UZBEKISTAN_LATIN 0x0443 +#define TT_MS_LANGID_UZBEK_UZBEKISTAN_CYRILLIC 0x0843 +#define TT_MS_LANGID_TATAR_TATARSTAN 0x0444 +#define TT_MS_LANGID_BENGALI_INDIA 0x0445 +#define TT_MS_LANGID_PUNJABI_INDIA 0x0446 +#define TT_MS_LANGID_GUJARATI_INDIA 0x0447 +#define TT_MS_LANGID_ORIYA_INDIA 0x0448 +#define TT_MS_LANGID_TAMIL_INDIA 0x0449 +#define TT_MS_LANGID_TELUGU_INDIA 0x044a +#define TT_MS_LANGID_KANNADA_INDIA 0x044b +#define TT_MS_LANGID_MALAYALAM_INDIA 0x044c +#define TT_MS_LANGID_ASSAMESE_INDIA 0x044d +#define TT_MS_LANGID_MARATHI_INDIA 0x044e +#define TT_MS_LANGID_SANSKRIT_INDIA 0x044f +#define TT_MS_LANGID_KONKANI_INDIA 0x0457 + + +/*************************************************************************/ +/* */ +/* Possible values of the `name' identifier field in the name records of */ +/* the TTF `name' table. These values are platform independent. */ +/* */ +#define TT_NAME_ID_COPYRIGHT 0 +#define TT_NAME_ID_FONT_FAMILY 1 +#define TT_NAME_ID_FONT_SUBFAMILY 2 +#define TT_NAME_ID_UNIQUE_ID 3 +#define TT_NAME_ID_FULL_NAME 4 +#define TT_NAME_ID_VERSION_STRING 5 +#define TT_NAME_ID_PS_NAME 6 +#define TT_NAME_ID_TRADEMARK 7 + +/* the following values are from the OpenType spec */ +#define TT_NAME_ID_MANUFACTURER 8 +#define TT_NAME_ID_DESIGNER 9 +#define TT_NAME_ID_DESCRIPTION 10 +#define TT_NAME_ID_VENDOR_URL 11 +#define TT_NAME_ID_DESIGNER_URL 12 +#define TT_NAME_ID_LICENSE 13 +#define TT_NAME_ID_LICENSE_URL 14 +/* number 15 is reserved */ +#define TT_NAME_ID_PREFERRED_FAMILY 16 +#define TT_NAME_ID_PREFERRED_SUBFAMILY 17 +#define TT_NAME_ID_MAC_FULL_NAME 18 + +/* The following code is new as of 2000-01-21 */ +#define TT_NAME_ID_SAMPLE_TEXT 19 + + +/*************************************************************************/ +/* */ +/* Bit mask values for the Unicode Ranges from the TTF `OS2 ' table. */ +/* */ +/* Updated 02-Jul-2000. */ +/* */ + +/* General Scripts Area */ + +/* Bit 0 C0 Controls and Basic Latin */ +#define TT_UCR_BASIC_LATIN ( 1L << 0 ) /* U+0020-U+007E */ +/* Bit 1 C1 Controls and Latin-1 Supplement */ +#define TT_UCR_LATIN1_SUPPLEMENT ( 1L << 1 ) /* U+00A0-U+00FF */ +/* Bit 2 Latin Extended-A */ +#define TT_UCR_LATIN_EXTENDED_A ( 1L << 2 ) /* U+0100-U+017F */ +/* Bit 3 Latin Extended-B */ +#define TT_UCR_LATIN_EXTENDED_B ( 1L << 3 ) /* U+0180-U+024F */ +/* Bit 4 IPA Extensions */ +#define TT_UCR_IPA_EXTENSIONS ( 1L << 4 ) /* U+0250-U+02AF */ +/* Bit 5 Spacing Modifier Letters */ +#define TT_UCR_SPACING_MODIFIER ( 1L << 5 ) /* U+02B0-U+02FF */ +/* Bit 6 Combining Diacritical Marks */ +#define TT_UCR_COMBINING_DIACRITICS ( 1L << 6 ) /* U+0300-U+036F */ +/* Bit 7 Greek */ +#define TT_UCR_GREEK ( 1L << 7 ) /* U+0370-U+03FF */ +/* Bit 8 is reserved (was: Greek Symbols and Coptic) */ +/* Bit 9 Cyrillic */ +#define TT_UCR_CYRILLIC ( 1L << 9 ) /* U+0400-U+04FF */ +/* Bit 10 Armenian */ +#define TT_UCR_ARMENIAN ( 1L << 10 ) /* U+0530-U+058F */ +/* Bit 11 Hebrew */ +#define TT_UCR_HEBREW ( 1L << 11 ) /* U+0590-U+05FF */ +/* Bit 12 is reserved (was: Hebrew Extended) */ +/* Bit 13 Arabic */ +#define TT_UCR_ARABIC ( 1L << 13 ) /* U+0600-U+06FF */ +/* Bit 14 is reserved (was: Arabic Extended) */ +/* Bit 15 Devanagari */ +#define TT_UCR_DEVANAGARI ( 1L << 15 ) /* U+0900-U+097F */ +/* Bit 16 Bengali */ +#define TT_UCR_BENGALI ( 1L << 16 ) /* U+0980-U+09FF */ +/* Bit 17 Gurmukhi */ +#define TT_UCR_GURMUKHI ( 1L << 17 ) /* U+0A00-U+0A7F */ +/* Bit 18 Gujarati */ +#define TT_UCR_GUJARATI ( 1L << 18 ) /* U+0A80-U+0AFF */ +/* Bit 19 Oriya */ +#define TT_UCR_ORIYA ( 1L << 19 ) /* U+0B00-U+0B7F */ +/* Bit 20 Tamil */ +#define TT_UCR_TAMIL ( 1L << 20 ) /* U+0B80-U+0BFF */ +/* Bit 21 Telugu */ +#define TT_UCR_TELUGU ( 1L << 21 ) /* U+0C00-U+0C7F */ +/* Bit 22 Kannada */ +#define TT_UCR_KANNADA ( 1L << 22 ) /* U+0C80-U+0CFF */ +/* Bit 23 Malayalam */ +#define TT_UCR_MALAYALAM ( 1L << 23 ) /* U+0D00-U+0D7F */ +/* Bit 24 Thai */ +#define TT_UCR_THAI ( 1L << 24 ) /* U+0E00-U+0E7F */ +/* Bit 25 Lao */ +#define TT_UCR_LAO ( 1L << 25 ) /* U+0E80-U+0EFF */ +/* Bit 26 Georgian */ +#define TT_UCR_GEORGIAN ( 1L << 26 ) /* U+10A0-U+10FF */ +/* Bit 27 is reserved (was Georgian Extended) */ +/* Bit 28 Hangul Jamo */ +#define TT_UCR_HANGUL_JAMO ( 1L << 28 ) /* U+1100-U+11FF */ +/* Bit 29 Latin Extended Additional */ +#define TT_UCR_LATIN_EXTENDED_ADDITIONAL ( 1L << 29 ) /* U+1E00-U+1EFF */ +/* Bit 30 Greek Extended */ +#define TT_UCR_GREEK_EXTENDED ( 1L << 30 ) /* U+1F00-U+1FFF */ + +/* Symbols Area */ + +/* Bit 31 General Punctuation */ +#define TT_UCR_GENERAL_PUNCTUATION ( 1L << 31 ) /* U+2000-U+206F */ +/* Bit 32 Superscripts And Subscripts */ +#define TT_UCR_SUPERSCRIPTS_SUBSCRIPTS ( 1L << 0 ) /* U+2070-U+209F */ +/* Bit 33 Currency Symbols */ +#define TT_UCR_CURRENCY_SYMBOLS ( 1L << 1 ) /* U+20A0-U+20CF */ +/* Bit 34 Combining Diacritical Marks For Symbols */ +#define TT_UCR_COMBINING_DIACRITICS_SYMB ( 1L << 2 ) /* U+20D0-U+20FF */ +/* Bit 35 Letterlike Symbols */ +#define TT_UCR_LETTERLIKE_SYMBOLS ( 1L << 3 ) /* U+2100-U+214F */ +/* Bit 36 Number Forms */ +#define TT_UCR_NUMBER_FORMS ( 1L << 4 ) /* U+2150-U+218F */ +/* Bit 37 Arrows */ +#define TT_UCR_ARROWS ( 1L << 5 ) /* U+2190-U+21FF */ +/* Bit 38 Mathematical Operators */ +#define TT_UCR_MATHEMATICAL_OPERATORS ( 1L << 6 ) /* U+2200-U+22FF */ +/* Bit 39 Miscellaneous Technical */ +#define TT_UCR_MISCELLANEOUS_TECHNICAL ( 1L << 7 ) /* U+2300-U+23FF */ +/* Bit 40 Control Pictures */ +#define TT_UCR_CONTROL_PICTURES ( 1L << 8 ) /* U+2400-U+243F */ +/* Bit 41 Optical Character Recognition */ +#define TT_UCR_OCR ( 1L << 9 ) /* U+2440-U+245F */ +/* Bit 42 Enclosed Alphanumerics */ +#define TT_UCR_ENCLOSED_ALPHANUMERICS ( 1L << 10 ) /* U+2460-U+24FF */ +/* Bit 43 Box Drawing */ +#define TT_UCR_BOX_DRAWING ( 1L << 11 ) /* U+2500-U+257F */ +/* Bit 44 Block Elements */ +#define TT_UCR_BLOCK_ELEMENTS ( 1L << 12 ) /* U+2580-U+259F */ +/* Bit 45 Geometric Shapes */ +#define TT_UCR_GEOMETRIC_SHAPES ( 1L << 13 ) /* U+25A0-U+25FF */ +/* Bit 46 Miscellaneous Symbols */ +#define TT_UCR_MISCELLANEOUS_SYMBOLS ( 1L << 14 ) /* U+2600-U+26FF */ +/* Bit 47 Dingbats */ +#define TT_UCR_DINGBATS ( 1L << 15 ) /* U+2700-U+27BF */ + +/* CJK Phonetics and Symbols Area */ + +/* Bit 48 CJK Symbols And Punctuation */ +#define TT_UCR_CJK_SYMBOLS ( 1L << 16 ) /* U+3000-U+303F */ +/* Bit 49 Hiragana */ +#define TT_UCR_HIRAGANA ( 1L << 17 ) /* U+3040-U+309F */ +/* Bit 50 Katakana */ +#define TT_UCR_KATAKANA ( 1L << 18 ) /* U+30A0-U+30FF */ +/* Bit 51 Bopomofo + Extended Bopomofo */ +#define TT_UCR_BOPOMOFO ( 1L << 19 ) /* U+3100-U+312F */ + /* U+31A0-U+31BF */ +/* Bit 52 Hangul Compatibility Jamo */ +#define TT_UCR_HANGUL_COMPATIBILITY_JAMO ( 1L << 20 ) /* U+3130-U+318F */ +/* Bit 53 CJK Miscellaneous */ +#define TT_UCR_CJK_MISC ( 1L << 21 ) /* U+3190-U+319F */ +/* Bit 54 Enclosed CJK Letters And Months */ +#define TT_UCR_ENCLOSED_CJK_LETTERS_MONTHS ( 1L << 22 ) /* U+3200-U+32FF */ +/* Bit 55 CJK Compatibility */ +#define TT_UCR_CJK_COMPATIBILITY ( 1L << 23 ) /* U+3300-U+33FF */ + +/* Hangul Syllables Area */ + +/* Bit 56 Hangul */ +#define TT_UCR_HANGUL ( 1L << 24 ) /* U+AC00-U+D7A3 */ + +/* Surrogates Area */ + +/* Bit 57 Surrogates */ +#define TT_UCR_SURROGATES ( 1L << 25 ) /* U+D800-U+DFFF */ +/* Bit 58 is reserved for Unicode SubRanges */ + +/* CJK Ideographs Area */ + +/* Bit 59 CJK Unified Ideographs + */ +/* CJK Radical Supplement + */ +/* Kangxi Radicals + */ +/* Ideographic Description + */ +/* CJK Unified Ideographs Extension A */ +#define TT_UCR_CJK_UNIFIED_IDEOGRAPHS ( 1L << 27 ) /* U+4E00-U+9FFF */ + /* U+2E80-U+2EFF */ + /* U+2F00-U+2FDF */ + /* U+2FF0-U+2FFF */ + /* U+34E0-U+4DB5 */ + +/* Private Use Area */ + +/* Bit 60 Private Use */ +#define TT_UCR_PRIVATE_USE ( 1L << 28 ) /* U+E000-U+F8FF */ + +/* Compatibility Area and Specials */ + +/* Bit 61 CJK Compatibility Ideographs */ +#define TT_UCR_CJK_COMPATIBILITY_IDEOGRAPHS ( 1L << 29 ) /* U+F900-U+FAFF */ +/* Bit 62 Alphabetic Presentation Forms */ +#define TT_UCR_ALPHABETIC_PRESENTATION_FORMS ( 1L << 30 ) /* U+FB00-U+FB4F */ +/* Bit 63 Arabic Presentation Forms-A */ +#define TT_UCR_ARABIC_PRESENTATIONS_A ( 1L << 31 ) /* U+FB50-U+FDFF */ +/* Bit 64 Combining Half Marks */ +#define TT_UCR_COMBINING_HALF_MARKS ( 1L << 0 ) /* U+FE20-U+FE2F */ +/* Bit 65 CJK Compatibility Forms */ +#define TT_UCR_CJK_COMPATIBILITY_FORMS ( 1L << 1 ) /* U+FE30-U+FE4F */ +/* Bit 66 Small Form Variants */ +#define TT_UCR_SMALL_FORM_VARIANTS ( 1L << 2 ) /* U+FE50-U+FE6F */ +/* Bit 67 Arabic Presentation Forms-B */ +#define TT_UCR_ARABIC_PRESENTATIONS_B ( 1L << 3 ) /* U+FE70-U+FEFE */ +/* Bit 68 Halfwidth And Fullwidth Forms */ +#define TT_UCR_HALFWIDTH_FULLWIDTH_FORMS ( 1L << 4 ) /* U+FF00-U+FFEF */ +/* Bit 69 Specials */ +#define TT_UCR_SPECIALS ( 1L << 5 ) /* U+FFF0-U+FFFD */ +/* Bit 70 Tibetan */ +#define TT_UCR_TIBETAN ( 1L << 6 ) /* U+0F00-U+0FCF */ +/* Bit 71 Syriac */ +#define TT_UCR_SYRIAC ( 1L << 7 ) /* U+0700-U+074F */ +/* Bit 72 Thaana */ +#define TT_UCR_THAANA ( 1L << 8 ) /* U+0780-U+07BF */ +/* Bit 73 Sinhala */ +#define TT_UCR_SINHALA ( 1L << 9 ) /* U+0D80-U+0DFF */ +/* Bit 74 Myanmar */ +#define TT_UCR_MYANMAR ( 1L << 10 ) /* U+1000-U+109F */ +/* Bit 75 Ethiopic */ +#define TT_UCR_ETHIOPIC ( 1L << 11 ) /* U+1200-U+12BF */ +/* Bit 76 Cherokee */ +#define TT_UCR_CHEROKEE ( 1L << 12 ) /* U+13A0-U+13FF */ +/* Bit 77 Canadian Aboriginal Syllabics */ +#define TT_UCR_CANADIAN_ABORIGINAL_SYLLABICS ( 1L << 13 ) /* U+1400-U+14DF */ +/* Bit 78 Ogham */ +#define TT_UCR_OGHAM ( 1L << 14 ) /* U+1680-U+169F */ +/* Bit 79 Runic */ +#define TT_UCR_RUNIC ( 1L << 15 ) /* U+16A0-U+16FF */ +/* Bit 80 Khmer */ +#define TT_UCR_KHMER ( 1L << 16 ) /* U+1780-U+17FF */ +/* Bit 81 Mongolian */ +#define TT_UCR_MONGOLIAN ( 1L << 17 ) /* U+1800-U+18AF */ +/* Bit 82 Braille */ +#define TT_UCR_BRAILLE ( 1L << 18 ) /* U+2800-U+28FF */ +/* Bit 83 Yi + Yi Radicals */ +#define TT_UCR_YI ( 1L << 19 ) /* U+A000-U+A48C */ + /* U+A490-U+A4CF */ + + +/*************************************************************************/ +/* */ +/* Some compilers have a very limited length of identifiers. */ +/* */ +#if defined( __TURBOC__ ) && __TURBOC__ < 0x0410 || defined( __PACIFIC__ ) +#define HAVE_LIMIT_ON_IDENTS +#endif + + +#ifndef HAVE_LIMIT_ON_IDENTS + + +/*************************************************************************/ +/* */ +/* Here some alias #defines in order to be clearer. */ +/* */ +/* These are not always #defined to stay within the 31 character limit */ +/* which some compilers have. */ +/* */ +/* Credits go to Dave Hoo for pointing out that modern */ +/* Borland compilers (read: from BC++ 3.1 on) can increase this limit. */ +/* If you get a warning with such a compiler, use the -i40 switch. */ +/* */ +#define TT_UCR_ARABIC_PRESENTATION_FORMS_A \ + TT_UCR_ARABIC_PRESENTATIONS_A +#define TT_UCR_ARABIC_PRESENTATION_FORMS_B \ + TT_UCR_ARABIC_PRESENTATIONS_B + +#define TT_UCR_COMBINING_DIACRITICAL_MARKS \ + TT_UCR_COMBINING_DIACRITICS +#define TT_UCR_COMBINING_DIACRITICAL_MARKS_SYMB \ + TT_UCR_COMBINING_DIACRITICS_SYMB + + +#endif /* !HAVE_LIMIT_ON_IDENTS */ + + +#endif /* TTNAMEID_H */ + + +/* END */ diff --git a/src/ft2/ttobjs.c b/src/ft2/ttobjs.c new file mode 100644 index 0000000..832e476 --- /dev/null +++ b/src/ft2/ttobjs.c @@ -0,0 +1,727 @@ +/***************************************************************************/ +/* */ +/* ttobjs.c */ +/* */ +/* Objects manager (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#include "ftdebug.h" +#include "ftcalc.h" +#include "ftstream.h" +#include "ttnameid.h" +#include "tttags.h" + +#include "sfnt.h" +#include "psnames.h" + + +#include "ttgload.h" +#include "ttpload.h" + +#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER +#include "ttinterp.h" +#endif + +#include "tterrors.h" + + + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_ttobjs + + +/*************************************************************************/ +/* */ +/* GLYPH ZONE FUNCTIONS */ +/* */ +/*************************************************************************/ + +/*************************************************************************/ +/* */ +/* */ +/* TT_Done_GlyphZone */ +/* */ +/* */ +/* Deallocates a glyph zone. */ +/* */ +/* */ +/* zone :: A pointer to the target glyph zone. */ +/* */ +LOCAL_FUNC +void TT_Done_GlyphZone( TT_GlyphZone* zone ) { + FT_Memory memory = zone->memory; + + + FREE( zone->contours ); + FREE( zone->tags ); + FREE( zone->cur ); + FREE( zone->org ); + + zone->max_points = zone->n_points = 0; + zone->max_contours = zone->n_contours = 0; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_New_GlyphZone */ +/* */ +/* */ +/* Allocates a new glyph zone. */ +/* */ +/* */ +/* memory :: A handle to the current memory object. */ +/* */ +/* maxPoints :: The capacity of glyph zone in points. */ +/* */ +/* maxContours :: The capacity of glyph zone in contours. */ +/* */ +/* */ +/* zone :: A pointer to the target glyph zone record. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_New_GlyphZone( FT_Memory memory, + FT_UShort maxPoints, + FT_Short maxContours, + TT_GlyphZone* zone ) { + FT_Error error; + + + if ( maxPoints > 0 ) { + maxPoints += 2; + } + + MEM_Set( zone, 0, sizeof( *zone ) ); + zone->memory = memory; + + if ( ALLOC_ARRAY( zone->org, maxPoints * 2, FT_F26Dot6 ) || + ALLOC_ARRAY( zone->cur, maxPoints * 2, FT_F26Dot6 ) || + ALLOC_ARRAY( zone->tags, maxPoints, FT_Byte ) || + ALLOC_ARRAY( zone->contours, maxContours, FT_UShort ) ) { + TT_Done_GlyphZone( zone ); + } + + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Init_Face */ +/* */ +/* */ +/* Initializes a given TrueType face object. */ +/* */ +/* */ +/* stream :: The source font stream. */ +/* */ +/* face_index :: The index of the font face in the resource. */ +/* */ +/* num_params :: Number of additional generic parameters. Ignored. */ +/* */ +/* params :: Additional generic parameters. Ignored. */ +/* */ +/* */ +/* face :: The newly built face object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_DEF +FT_Error TT_Init_Face( FT_Stream stream, + TT_Face face, + FT_Int face_index, + FT_Int num_params, + FT_Parameter* params ) { + FT_Error error; + FT_Library library; + SFNT_Interface* sfnt; + + + library = face->root.driver->root.library; + sfnt = (SFNT_Interface*)FT_Get_Module_Interface( library, "sfnt" ); + if ( !sfnt ) { + goto Bad_Format; + } + + /* create input stream from resource */ + if ( FILE_Seek( 0 ) ) { + goto Exit; + } + + /* check that we have a valid TrueType file */ + error = sfnt->init_face( stream, face, face_index, num_params, params ); + if ( error ) { + goto Exit; + } + + /* We must also be able to accept Mac/GX fonts, as well as OT ones */ + if ( face->format_tag != 0x00010000L && /* MS fonts */ + face->format_tag != TTAG_true ) { /* Mac fonts */ + FT_TRACE2( ( "[not a valid TTF font]\n" ) ); + goto Bad_Format; + } + + /* If we are performing a simple font format check, exit immediately */ + if ( face_index < 0 ) { + return TT_Err_Ok; + } + + /* Load font directory */ + error = sfnt->load_face( stream, face, face_index, num_params, params ); + if ( error ) { + goto Exit; + } + + error = TT_Load_Locations( face, stream ) || + TT_Load_CVT( face, stream ) || + TT_Load_Programs( face, stream ); + + /* initialize standard glyph loading routines */ + TT_Init_Glyph_Loading( face ); + +Exit: + return error; + +Bad_Format: + error = FT_Err_Unknown_File_Format; + goto Exit; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Done_Face */ +/* */ +/* */ +/* Finalizes a given face object. */ +/* */ +/* */ +/* face :: A pointer to the face object to destroy. */ +/* */ +LOCAL_DEF +void TT_Done_Face( TT_Face face ) { + FT_Memory memory = face->root.memory; + FT_Stream stream = face->root.stream; + + SFNT_Interface* sfnt = (SFNT_Interface*)face->sfnt; + + + /* for `extended TrueType formats' (i.e. compressed versions) */ + if ( face->extra.finalizer ) { + face->extra.finalizer( face->extra.data ); + } + + if ( sfnt ) { + sfnt->done_face( face ); + } + + /* freeing the locations table */ + FREE( face->glyph_locations ); + face->num_locations = 0; + + /* freeing the CVT */ + FREE( face->cvt ); + face->cvt_size = 0; + + /* freeing the programs */ + RELEASE_Frame( face->font_program ); + RELEASE_Frame( face->cvt_program ); + face->font_program_size = 0; + face->cvt_program_size = 0; +} + + +/*************************************************************************/ +/* */ +/* SIZE FUNCTIONS */ +/* */ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Init_Size */ +/* */ +/* */ +/* Initializes a new TrueType size object. */ +/* */ +/* */ +/* size :: A handle to the size object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_DEF +FT_Error TT_Init_Size( TT_Size size ) { + FT_Error error = TT_Err_Ok; + + +#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER + + TT_Face face = (TT_Face)size->root.face; + FT_Memory memory = face->root.memory; + FT_Int i; + + TT_ExecContext exec; + FT_UShort n_twilight; + TT_MaxProfile* maxp = &face->max_profile; + + + size->ttmetrics.valid = FALSE; + + size->max_function_defs = maxp->maxFunctionDefs; + size->max_instruction_defs = maxp->maxInstructionDefs; + + size->num_function_defs = 0; + size->num_instruction_defs = 0; + + size->max_func = 0; + size->max_ins = 0; + + size->cvt_size = face->cvt_size; + size->storage_size = maxp->maxStorage; + + /* Set default metrics */ + { + FT_Size_Metrics* metrics = &size->root.metrics; + TT_Size_Metrics* metrics2 = &size->ttmetrics; + + + metrics->x_ppem = 0; + metrics->y_ppem = 0; + + metrics2->rotated = FALSE; + metrics2->stretched = FALSE; + + /* set default compensation (all 0) */ + for ( i = 0; i < 4; i++ ) + metrics2->compensations[i] = 0; + } + + /* allocate function defs, instruction defs, cvt, and storage area */ + if ( ALLOC_ARRAY( size->function_defs, + size->max_function_defs, + TT_DefRecord ) || + + ALLOC_ARRAY( size->instruction_defs, + size->max_instruction_defs, + TT_DefRecord ) || + + ALLOC_ARRAY( size->cvt, + size->cvt_size, FT_Long ) || + + ALLOC_ARRAY( size->storage, + size->storage_size, FT_Long ) ) { + + goto Fail_Memory; + } + + /* reserve twilight zone */ + n_twilight = maxp->maxTwilightPoints; + error = TT_New_GlyphZone( memory, n_twilight, 0, &size->twilight ); + if ( error ) { + goto Fail_Memory; + } + + size->twilight.n_points = n_twilight; + + /* set `face->interpreter' according to the debug hook present */ + { + FT_Library library = face->root.driver->root.library; + + + face->interpreter = (TT_Interpreter) + library->debug_hooks[FT_DEBUG_HOOK_TRUETYPE]; + if ( !face->interpreter ) { + face->interpreter = (TT_Interpreter)TT_RunIns; + } + } + + /* Fine, now execute the font program! */ + exec = size->context; + /* size objects used during debugging have their own context */ + if ( !size->debug ) { + exec = TT_New_Context( face ); + } + + if ( !exec ) { + error = TT_Err_Could_Not_Find_Context; + goto Fail_Memory; + } + + size->GS = tt_default_graphics_state; + TT_Load_Context( exec, face, size ); + + exec->callTop = 0; + exec->top = 0; + + exec->period = 64; + exec->phase = 0; + exec->threshold = 0; + + { + FT_Size_Metrics* metrics = &exec->metrics; + TT_Size_Metrics* tt_metrics = &exec->tt_metrics; + + + metrics->x_ppem = 0; + metrics->y_ppem = 0; + metrics->x_scale = 0; + metrics->y_scale = 0; + + tt_metrics->ppem = 0; + tt_metrics->scale = 0; + tt_metrics->ratio = 0x10000L; + } + + exec->instruction_trap = FALSE; + + exec->cvtSize = size->cvt_size; + exec->cvt = size->cvt; + + exec->F_dot_P = 0x10000L; + + /* allow font program execution */ + TT_Set_CodeRange( exec, + tt_coderange_font, + face->font_program, + face->font_program_size ); + + /* disable CVT and glyph programs coderange */ + TT_Clear_CodeRange( exec, tt_coderange_cvt ); + TT_Clear_CodeRange( exec, tt_coderange_glyph ); + + if ( face->font_program_size > 0 ) { + error = TT_Goto_CodeRange( exec, tt_coderange_font, 0 ); + if ( !error ) { + error = face->interpreter( exec ); + } + + if ( error ) { + goto Fail_Exec; + } + } else { + error = TT_Err_Ok; + } + + TT_Save_Context( exec, size ); + + if ( !size->debug ) { + TT_Done_Context( exec ); + } + +#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */ + + size->ttmetrics.valid = FALSE; + return error; + +#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER + +Fail_Exec: + if ( !size->debug ) { + TT_Done_Context( exec ); + } + +Fail_Memory: + +#endif + + TT_Done_Size( size ); + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Done_Size */ +/* */ +/* */ +/* The TrueType size object finalizer. */ +/* */ +/* */ +/* size :: A handle to the target size object. */ +/* */ +LOCAL_FUNC +void TT_Done_Size( TT_Size size ) { + +#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER + + FT_Memory memory = size->root.face->memory; + + + if ( size->debug ) { + /* the debug context must be deleted by the debugger itself */ + size->context = NULL; + size->debug = FALSE; + } + + FREE( size->cvt ); + size->cvt_size = 0; + + /* free storage area */ + FREE( size->storage ); + size->storage_size = 0; + + /* twilight zone */ + TT_Done_GlyphZone( &size->twilight ); + + FREE( size->function_defs ); + FREE( size->instruction_defs ); + + size->num_function_defs = 0; + size->max_function_defs = 0; + size->num_instruction_defs = 0; + size->max_instruction_defs = 0; + + size->max_func = 0; + size->max_ins = 0; + +#endif + + size->ttmetrics.valid = FALSE; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Reset_Size */ +/* */ +/* */ +/* Resets a TrueType size when resolutions and character dimensions */ +/* have been changed. */ +/* */ +/* */ +/* size :: A handle to the target size object. */ +/* */ +LOCAL_DEF +FT_Error TT_Reset_Size( TT_Size size ) { + TT_Face face; + FT_Error error = TT_Err_Ok; + + FT_Size_Metrics* metrics; + + + if ( size->ttmetrics.valid ) { + return TT_Err_Ok; + } + + face = (TT_Face)size->root.face; + + metrics = &size->root.metrics; + + if ( metrics->x_ppem < 1 || metrics->y_ppem < 1 ) { + return TT_Err_Invalid_PPem; + } + + /* compute new transformation */ + if ( metrics->x_ppem >= metrics->y_ppem ) { + size->ttmetrics.scale = metrics->x_scale; + size->ttmetrics.ppem = metrics->x_ppem; + size->ttmetrics.x_ratio = 0x10000L; + size->ttmetrics.y_ratio = FT_MulDiv( metrics->y_ppem, + 0x10000L, + metrics->x_ppem ); + } else + { + size->ttmetrics.scale = metrics->y_scale; + size->ttmetrics.ppem = metrics->y_ppem; + size->ttmetrics.x_ratio = FT_MulDiv( metrics->x_ppem, + 0x10000L, + metrics->y_ppem ); + size->ttmetrics.y_ratio = 0x10000L; + } + + /* Compute root ascender, descender, test height, and max_advance */ + metrics->ascender = ( FT_MulFix( face->root.ascender, + metrics->y_scale ) + 32 ) & - 64; + metrics->descender = ( FT_MulFix( face->root.descender, + metrics->y_scale ) + 32 ) & - 64; + metrics->height = ( FT_MulFix( face->root.height, + metrics->y_scale ) + 32 ) & - 64; + metrics->max_advance = ( FT_MulFix( face->root.max_advance_width, + metrics->x_scale ) + 32 ) & - 64; + +#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER + + { + TT_ExecContext exec; + FT_UInt i, j; + + + /* Scale the cvt values to the new ppem. */ + /* We use by default the y ppem to scale the CVT. */ + for ( i = 0; i < size->cvt_size; i++ ) + size->cvt[i] = FT_MulFix( face->cvt[i], size->ttmetrics.scale ); + + /* All twilight points are originally zero */ + for ( j = 0; j < size->twilight.n_points; j++ ) + { + size->twilight.org[j].x = 0; + size->twilight.org[j].y = 0; + size->twilight.cur[j].x = 0; + size->twilight.cur[j].y = 0; + } + + /* clear storage area */ + for ( i = 0; i < size->storage_size; i++ ) + size->storage[i] = 0; + + size->GS = tt_default_graphics_state; + + /* get execution context and run prep program */ + if ( size->debug ) { + exec = size->context; + } else { + exec = TT_New_Context( face ); + } + /* debugging instances have their own context */ + + if ( !exec ) { + return TT_Err_Could_Not_Find_Context; + } + + TT_Load_Context( exec, face, size ); + + TT_Set_CodeRange( exec, + tt_coderange_cvt, + face->cvt_program, + face->cvt_program_size ); + + TT_Clear_CodeRange( exec, tt_coderange_glyph ); + + exec->instruction_trap = FALSE; + + exec->top = 0; + exec->callTop = 0; + + if ( face->cvt_program_size > 0 ) { + error = TT_Goto_CodeRange( exec, tt_coderange_cvt, 0 ); + if ( error ) { + goto End; + } + + if ( !size->debug ) { + error = face->interpreter( exec ); + } + } else { + error = TT_Err_Ok; + } + + size->GS = exec->GS; + /* save default graphics state */ + +End: + TT_Save_Context( exec, size ); + + if ( !size->debug ) { + TT_Done_Context( exec ); + } + /* debugging instances keep their context */ + } + +#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */ + + if ( !error ) { + size->ttmetrics.valid = TRUE; + } + + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Init_Driver */ +/* */ +/* */ +/* Initializes a given TrueType driver object. */ +/* */ +/* */ +/* driver :: A handle to the target driver object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Init_Driver( TT_Driver driver ) { + FT_Error error; + + + /* set `extra' in glyph loader */ + error = FT_GlyphLoader_Create_Extra( FT_DRIVER( driver )->glyph_loader ); + + /* init extension registry if needed */ + +#ifdef TT_CONFIG_OPTION_EXTEND_ENGINE + if ( !error ) { + return TT_Init_Extensions( driver ); + } +#endif + + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Done_Driver */ +/* */ +/* */ +/* Finalizes a given TrueType driver. */ +/* */ +/* */ +/* driver :: A handle to the target TrueType driver. */ +/* */ +LOCAL_FUNC +void TT_Done_Driver( TT_Driver driver ) { + /* destroy extensions registry if needed */ + +#ifdef TT_CONFIG_OPTION_EXTEND_ENGINE + + TT_Done_Extensions( driver ); + +#endif + +#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER + + /* destroy the execution context */ + if ( driver->context ) { + TT_Destroy_Context( driver->context, driver->root.root.memory ); + driver->context = NULL; + } + +#endif + +} + + +/* END */ diff --git a/src/ft2/ttobjs.h b/src/ft2/ttobjs.h new file mode 100644 index 0000000..eff7454 --- /dev/null +++ b/src/ft2/ttobjs.h @@ -0,0 +1,412 @@ +/***************************************************************************/ +/* */ +/* ttobjs.h */ +/* */ +/* Objects manager (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef TTOBJS_H +#define TTOBJS_H + + +#include "ftobjs.h" +#include "tttypes.h" +#include "tterrors.h" + + +#ifdef __cplusplus +extern "C" { +#endif + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Driver */ +/* */ +/* */ +/* A handle to a TrueType driver object. */ +/* */ +typedef struct TT_DriverRec_* TT_Driver; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Instance */ +/* */ +/* */ +/* A handle to a TrueType size object. */ +/* */ +typedef struct TT_SizeRec_* TT_Size; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_GlyphSlot */ +/* */ +/* */ +/* A handle to a TrueType glyph slot object. */ +/* */ +/* */ +/* This is a direct typedef of FT_GlyphSlot, as there is nothing */ +/* specific about the TrueType glyph slot. */ +/* */ +typedef FT_GlyphSlot TT_GlyphSlot; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_GraphicsState */ +/* */ +/* */ +/* The TrueType graphics state used during bytecode interpretation. */ +/* */ +typedef struct TT_GraphicsState_ +{ + FT_UShort rp0; + FT_UShort rp1; + FT_UShort rp2; + + FT_UnitVector dualVector; + FT_UnitVector projVector; + FT_UnitVector freeVector; + + FT_Long loop; + FT_F26Dot6 minimum_distance; + FT_Int round_state; + + FT_Bool auto_flip; + FT_F26Dot6 control_value_cutin; + FT_F26Dot6 single_width_cutin; + FT_F26Dot6 single_width_value; + FT_Short delta_base; + FT_Short delta_shift; + + FT_Byte instruct_control; + FT_Bool scan_control; + FT_Int scan_type; + + FT_UShort gep0; + FT_UShort gep1; + FT_UShort gep2; + +} TT_GraphicsState; + + +LOCAL_DEF void TT_Done_GlyphZone( TT_GlyphZone* zone ); + +LOCAL_DEF FT_Error TT_New_GlyphZone( FT_Memory memory, + FT_UShort maxPoints, + FT_Short maxContours, + TT_GlyphZone* zone ); + + +/*************************************************************************/ +/* */ +/* EXECUTION SUBTABLES */ +/* */ +/* These sub-tables relate to instruction execution. */ +/* */ +/*************************************************************************/ + + +#define TT_MAX_CODE_RANGES 3 + + +/*************************************************************************/ +/* */ +/* There can only be 3 active code ranges at once: */ +/* - the Font Program */ +/* - the CVT Program */ +/* - a glyph's instructions set */ +/* */ +typedef enum TT_CodeRange_Tag_ +{ + tt_coderange_none = 0, + tt_coderange_font, + tt_coderange_cvt, + tt_coderange_glyph + +} TT_CodeRange_Tag; + + +typedef struct TT_CodeRange_ +{ + FT_Byte* base; + FT_ULong size; + +} TT_CodeRange; + +typedef TT_CodeRange TT_CodeRangeTable[TT_MAX_CODE_RANGES]; + + +/*************************************************************************/ +/* */ +/* Defines a function/instruction definition record. */ +/* */ +typedef struct TT_DefRecord_ +{ + FT_Int range; /* in which code range is it located? */ + FT_Long start; /* where does it start? */ + FT_UInt opc; /* function #, or instruction code */ + FT_Bool active; /* is it active? */ + +} TT_DefRecord, *TT_DefArray; + + +/*************************************************************************/ +/* */ +/* Subglyph transformation record. */ +/* */ +typedef struct TT_Transform_ +{ + FT_Fixed xx, xy; /* transformation matrix coefficients */ + FT_Fixed yx, yy; + FT_F26Dot6 ox, oy; /* offsets */ + +} TT_Transform; + + +/*************************************************************************/ +/* */ +/* Subglyph loading record. Used to load composite components. */ +/* */ +typedef struct TT_SubglyphRec_ +{ + FT_Long index; /* subglyph index; initialized with -1 */ + FT_Bool is_scaled; /* is the subglyph scaled? */ + FT_Bool is_hinted; /* should it be hinted? */ + FT_Bool preserve_pps; /* preserve phantom points? */ + + FT_Long file_offset; + + FT_BBox bbox; + FT_Pos left_bearing; + FT_Pos advance; + + TT_GlyphZone zone; + + FT_Long arg1; /* first argument */ + FT_Long arg2; /* second argument */ + + FT_UShort element_flag; /* current load element flag */ + + TT_Transform transform; /* transformation matrix */ + + FT_Vector pp1, pp2; /* phantom points */ + +} TT_SubGlyphRec, *TT_SubGlyph_Stack; + + +/*************************************************************************/ +/* */ +/* A note regarding non-squared pixels: */ +/* */ +/* (This text will probably go into some docs at some time; for now, it */ +/* is kept here to explain some definitions in the TIns_Metrics */ +/* record). */ +/* */ +/* The CVT is a one-dimensional array containing values that control */ +/* certain important characteristics in a font, like the height of all */ +/* capitals, all lowercase letter, default spacing or stem width/height. */ +/* */ +/* These values are found in FUnits in the font file, and must be scaled */ +/* to pixel coordinates before being used by the CVT and glyph programs. */ +/* Unfortunately, when using distinct x and y resolutions (or distinct x */ +/* and y pointsizes), there are two possible scalings. */ +/* */ +/* A first try was to implement a `lazy' scheme where all values were */ +/* scaled when first used. However, while some values are always used */ +/* in the same direction, some others are used under many different */ +/* circumstances and orientations. */ +/* */ +/* I have found a simpler way to do the same, and it even seems to work */ +/* in most of the cases: */ +/* */ +/* - All CVT values are scaled to the maximum ppem size. */ +/* */ +/* - When performing a read or write in the CVT, a ratio factor is used */ +/* to perform adequate scaling. Example: */ +/* */ +/* x_ppem = 14 */ +/* y_ppem = 10 */ +/* */ +/* We choose ppem = x_ppem = 14 as the CVT scaling size. All cvt */ +/* entries are scaled to it. */ +/* */ +/* x_ratio = 1.0 */ +/* y_ratio = y_ppem/ppem (< 1.0) */ +/* */ +/* We compute the current ratio like: */ +/* */ +/* - If projVector is horizontal, */ +/* ratio = x_ratio = 1.0 */ +/* */ +/* - if projVector is vertical, */ +/* ratio = y_ratio */ +/* */ +/* - else, */ +/* ratio = sqrt( (proj.x * x_ratio) ^ 2 + (proj.y * y_ratio) ^ 2 ) */ +/* */ +/* Reading a cvt value returns */ +/* ratio * cvt[index] */ +/* */ +/* Writing a cvt value in pixels: */ +/* cvt[index] / ratio */ +/* */ +/* The current ppem is simply */ +/* ratio * ppem */ +/* */ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* Metrics used by the TrueType size and context objects. */ +/* */ +typedef struct TT_Size_Metrics_ +{ + /* for non-square pixels */ + FT_Long x_ratio; + FT_Long y_ratio; + + FT_UShort ppem; /* maximum ppem size */ + FT_Long ratio; /* current ratio */ + FT_Fixed scale; + + FT_F26Dot6 compensations[4]; /* device-specific compensations */ + + FT_Bool valid; + + FT_Bool rotated; /* `is the glyph rotated?'-flag */ + FT_Bool stretched; /* `is the glyph stretched?'-flag */ + +} TT_Size_Metrics; + + +/*************************************************************************/ +/* */ +/* TrueType size class. */ +/* */ +typedef struct TT_SizeRec_ +{ + FT_SizeRec root; + + TT_Size_Metrics ttmetrics; + +#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER + + FT_UInt num_function_defs; /* number of function definitions */ + FT_UInt max_function_defs; + TT_DefArray function_defs; /* table of function definitions */ + + FT_UInt num_instruction_defs; /* number of ins. definitions */ + FT_UInt max_instruction_defs; + TT_DefArray instruction_defs; /* table of ins. definitions */ + + FT_UInt max_func; + FT_UInt max_ins; + + TT_CodeRangeTable codeRangeTable; + + TT_GraphicsState GS; + + FT_ULong cvt_size; /* the scaled control value table */ + FT_Long* cvt; + + FT_UShort storage_size; /* The storage area is now part of */ + FT_Long* storage; /* the instance */ + + TT_GlyphZone twilight; /* The instance's twilight zone */ + + /* debugging variables */ + + /* When using the debugger, we must keep the */ + /* execution context tied to the instance */ + /* object rather than asking it on demand. */ + + FT_Bool debug; + TT_ExecContext context; + +#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */ + +} TT_SizeRec; + + +/*************************************************************************/ +/* */ +/* TrueType driver class. */ +/* */ +typedef struct TT_DriverRec_ +{ + FT_DriverRec root; + TT_ExecContext context; /* execution context */ + TT_GlyphZone zone; /* glyph loader points zone */ + + void* extension_component; + +} TT_DriverRec; + + +/*************************************************************************/ +/* */ +/* Face functions */ +/* */ +LOCAL_DEF +FT_Error TT_Init_Face( FT_Stream stream, + TT_Face face, + FT_Int face_index, + FT_Int num_params, + FT_Parameter* params ); + +LOCAL_DEF +void TT_Done_Face( TT_Face face ); + + +/*************************************************************************/ +/* */ +/* Size functions */ +/* */ +LOCAL_DEF +FT_Error TT_Init_Size( TT_Size size ); + +LOCAL_DEF +void TT_Done_Size( TT_Size size ); + +LOCAL_DEF +FT_Error TT_Reset_Size( TT_Size size ); + + +/*************************************************************************/ +/* */ +/* Driver functions */ +/* */ +LOCAL_DEF +FT_Error TT_Init_Driver( TT_Driver driver ); + +LOCAL_DEF +void TT_Done_Driver( TT_Driver driver ); + + +#ifdef __cplusplus +} +#endif + +#endif /* TTOBJS_H */ + + +/* END */ diff --git a/src/ft2/ttpload.c b/src/ft2/ttpload.c new file mode 100644 index 0000000..892e1cd --- /dev/null +++ b/src/ft2/ttpload.c @@ -0,0 +1,266 @@ +/***************************************************************************/ +/* */ +/* ttpload.h */ +/* */ +/* TrueType glyph data/program tables loader (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#include "ftdebug.h" +#include "ftobjs.h" +#include "ftstream.h" +#include "tttags.h" + +#include "ttpload.h" + +#include "tterrors.h" + + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_ttpload + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_Locations */ +/* */ +/* */ +/* Loads the locations table. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* */ +/* */ +/* stream :: The input stream. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Load_Locations( TT_Face face, + FT_Stream stream ) { + FT_Error error; + FT_Memory memory = stream->memory; + FT_Short LongOffsets; + FT_ULong table_len; + + + FT_TRACE2( ( "Locations " ) ); + LongOffsets = face->header.Index_To_Loc_Format; + + error = face->goto_table( face, TTAG_loca, stream, &table_len ); + if ( error ) { + error = TT_Err_Locations_Missing; + goto Exit; + } + + if ( LongOffsets != 0 ) { + face->num_locations = (FT_UShort)( table_len >> 2 ); + + FT_TRACE2( ( "(32bit offsets): %12d ", face->num_locations ) ); + + if ( ALLOC_ARRAY( face->glyph_locations, + face->num_locations, + FT_Long ) ) { + goto Exit; + } + + if ( ACCESS_Frame( face->num_locations * 4L ) ) { + goto Exit; + } + + { + FT_Long* loc = face->glyph_locations; + FT_Long* limit = loc + face->num_locations; + + + for ( ; loc < limit; loc++ ) + *loc = GET_Long(); + } + + FORGET_Frame(); + } else + { + face->num_locations = (FT_UShort)( table_len >> 1 ); + + FT_TRACE2( ( "(16bit offsets): %12d ", face->num_locations ) ); + + if ( ALLOC_ARRAY( face->glyph_locations, + face->num_locations, + FT_Long ) ) { + goto Exit; + } + + if ( ACCESS_Frame( face->num_locations * 2L ) ) { + goto Exit; + } + { + FT_Long* loc = face->glyph_locations; + FT_Long* limit = loc + face->num_locations; + + + for ( ; loc < limit; loc++ ) + *loc = (FT_Long)( (FT_ULong)GET_UShort() * 2 ); + } + FORGET_Frame(); + } + + FT_TRACE2( ( "loaded\n" ) ); + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_CVT */ +/* */ +/* */ +/* Loads the control value table into a face object. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* */ +/* */ +/* stream :: A handle to the input stream. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Load_CVT( TT_Face face, + FT_Stream stream ) { + FT_Error error; + FT_Memory memory = stream->memory; + FT_ULong table_len; + + + FT_TRACE2( ( "CVT " ) ); + + error = face->goto_table( face, TTAG_cvt, stream, &table_len ); + if ( error ) { + FT_TRACE2( ( "is missing!\n" ) ); + + face->cvt_size = 0; + face->cvt = NULL; + error = TT_Err_Ok; + + goto Exit; + } + + face->cvt_size = table_len / 2; + + if ( ALLOC_ARRAY( face->cvt, + face->cvt_size, + FT_Short ) ) { + goto Exit; + } + + if ( ACCESS_Frame( face->cvt_size * 2L ) ) { + goto Exit; + } + + { + FT_Short* cur = face->cvt; + FT_Short* limit = cur + face->cvt_size; + + + for ( ; cur < limit; cur++ ) + *cur = GET_Short(); + } + + FORGET_Frame(); + FT_TRACE2( ( "loaded\n" ) ); + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_Progams */ +/* */ +/* */ +/* Loads the font program and the cvt program. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* */ +/* */ +/* stream :: A handle to the input stream. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Load_Programs( TT_Face face, + FT_Stream stream ) { + FT_Error error; + FT_ULong table_len; + + + FT_TRACE2( ( "Font program " ) ); + + /* The font program is optional */ + error = face->goto_table( face, TTAG_fpgm, stream, &table_len ); + if ( error ) { + face->font_program = NULL; + face->font_program_size = 0; + + FT_TRACE2( ( "is missing!\n" ) ); + } else + { + face->font_program_size = table_len; + if ( EXTRACT_Frame( table_len, face->font_program ) ) { + goto Exit; + } + + FT_TRACE2( ( "loaded, %12d bytes\n", face->font_program_size ) ); + } + + FT_TRACE2( ( "Prep program " ) ); + + error = face->goto_table( face, TTAG_prep, stream, &table_len ); + if ( error ) { + face->cvt_program = NULL; + face->cvt_program_size = 0; + error = TT_Err_Ok; + + FT_TRACE2( ( "is missing!\n" ) ); + } else + { + face->cvt_program_size = table_len; + if ( EXTRACT_Frame( table_len, face->cvt_program ) ) { + goto Exit; + } + + FT_TRACE2( ( "loaded, %12d bytes\n", face->cvt_program_size ) ); + } + +Exit: + return error; +} + + +/* END */ diff --git a/src/ft2/ttpload.h b/src/ft2/ttpload.h new file mode 100644 index 0000000..eecf236 --- /dev/null +++ b/src/ft2/ttpload.h @@ -0,0 +1,51 @@ +/***************************************************************************/ +/* */ +/* ttpload.h */ +/* */ +/* TrueType glyph data/program tables loader (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef TTPLOAD_H +#define TTPLOAD_H + +#include "tttypes.h" + + +#ifdef __cplusplus +extern "C" { +#endif + + +LOCAL_DEF +FT_Error TT_Load_Locations( TT_Face face, + FT_Stream stream ); + +LOCAL_DEF +FT_Error TT_Load_CVT( TT_Face face, + FT_Stream stream ); + +LOCAL_DEF +FT_Error TT_Load_Programs( TT_Face face, + FT_Stream stream ); + + +#ifdef __cplusplus +} +#endif + + +#endif /* TTPLOAD_H */ + + +/* END */ diff --git a/src/ft2/ttpost.c b/src/ft2/ttpost.c new file mode 100644 index 0000000..811087b --- /dev/null +++ b/src/ft2/ttpost.c @@ -0,0 +1,528 @@ +/***************************************************************************/ +/* */ +/* ttpost.c */ +/* */ +/* Postcript name table processing for TrueType and OpenType fonts */ +/* (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + +/*************************************************************************/ +/* */ +/* The post table is not completely loaded by the core engine. This */ +/* file loads the missing PS glyph names and implements an API to access */ +/* them. */ +/* */ +/*************************************************************************/ + + +#include "ftstream.h" +#include "tterrors.h" +#include "tttags.h" + + +#include "ttpost.h" +#include "ttload.h" + + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_ttpost + + +/* If this configuration macro is defined, we rely on the `PSNames' */ +/* module to grab the glyph names. */ + +#ifdef FT_CONFIG_OPTION_POSTSCRIPT_NAMES + + +#include "psnames.h" + +#define MAC_NAME( x ) ( (FT_String*)psnames->macintosh_name( x ) ) + + +#else /* FT_CONFIG_OPTION_POSTSCRIPT_NAMES */ + + +/* Otherwise, we ignore the `PSNames' module, and provide our own */ +/* table of Mac names. Thus, it is possible to build a version of */ +/* FreeType without the Type 1 driver & PSNames module. */ + +#define MAC_NAME( x ) TT_Post_Default_Names[x] + +/* the 258 default Mac PS glyph names */ + +FT_String* TT_Post_Default_Names[258] = +{ + /* 0 */ + ".notdef", ".null", "CR", "space", "exclam", + "quotedbl", "numbersign", "dollar", "percent", "ampersand", + /* 10 */ + "quotesingle", "parenleft", "parenright", "asterisk", "plus", + "comma", "hyphen", "period", "slash", "zero", + /* 20 */ + "one", "two", "three", "four", "five", + "six", "seven", "eight", "nine", "colon", + /* 30 */ + "semicolon", "less", "equal", "greater", "question", + "at", "A", "B", "C", "D", + /* 40 */ + "E", "F", "G", "H", "I", + "J", "K", "L", "M", "N", + /* 50 */ + "O", "P", "Q", "R", "S", + "T", "U", "V", "W", "X", + /* 60 */ + "Y", "Z", "bracketleft", "backslash", "bracketright", + "asciicircum", "underscore", "grave", "a", "b", + /* 70 */ + "c", "d", "e", "f", "g", + "h", "i", "j", "k", "l", + /* 80 */ + "m", "n", "o", "p", "q", + "r", "s", "t", "u", "v", + /* 90 */ + "w", "x", "y", "z", "braceleft", + "bar", "braceright", "asciitilde", "Adieresis", "Aring", + /* 100 */ + "Ccedilla", "Eacute", "Ntilde", "Odieresis", "Udieresis", + "aacute", "agrave", "acircumflex", "adieresis", "atilde", + /* 110 */ + "aring", "ccedilla", "eacute", "egrave", "ecircumflex", + "edieresis", "iacute", "igrave", "icircumflex", "idieresis", + /* 120 */ + "ntilde", "oacute", "ograve", "ocircumflex", "odieresis", + "otilde", "uacute", "ugrave", "ucircumflex", "udieresis", + /* 130 */ + "dagger", "degree", "cent", "sterling", "section", + "bullet", "paragraph", "germandbls", "registered", "copyright", + /* 140 */ + "trademark", "acute", "dieresis", "notequal", "AE", + "Oslash", "infinity", "plusminus", "lessequal", "greaterequal", + /* 150 */ + "yen", "mu", "partialdiff", "summation", "product", + "pi", "integral", "ordfeminine", "ordmasculine", "Omega", + /* 160 */ + "ae", "oslash", "questiondown", "exclamdown", "logicalnot", + "radical", "florin", "approxequal", "Delta", "guillemotleft", + /* 170 */ + "guillemotright", "ellipsis", "nbspace", "Agrave", "Atilde", + "Otilde", "OE", "oe", "endash", "emdash", + /* 180 */ + "quotedblleft", "quotedblright", "quoteleft", "quoteright", "divide", + "lozenge", "ydieresis", "Ydieresis", "fraction", "currency", + /* 190 */ + "guilsinglleft", "guilsinglright", "fi", "fl", "daggerdbl", + "periodcentered", "quotesinglbase", "quotedblbase", "perthousand", "Acircumflex", + /* 200 */ + "Ecircumflex", "Aacute", "Edieresis", "Egrave", "Iacute", + "Icircumflex", "Idieresis", "Igrave", "Oacute", "Ocircumflex", + /* 210 */ + "apple", "Ograve", "Uacute", "Ucircumflex", "Ugrave", + "dotlessi", "circumflex", "tilde", "macron", "breve", + /* 220 */ + "dotaccent", "ring", "cedilla", "hungarumlaut", "ogonek", + "caron", "Lslash", "lslash", "Scaron", "scaron", + /* 230 */ + "Zcaron", "zcaron", "brokenbar", "Eth", "eth", + "Yacute", "yacute", "Thorn", "thorn", "minus", + /* 240 */ + "multiply", "onesuperior", "twosuperior", "threesuperior", "onehalf", + "onequarter", "threequarters", "franc", "Gbreve", "gbreve", + /* 250 */ + "Idot", "Scedilla", "scedilla", "Cacute", "cacute", + "Ccaron", "ccaron", "dmacron", +}; + + +#endif /* FT_CONFIG_OPTION_POSTSCRIPT_NAMES */ + + +static +FT_Error Load_Format_20( TT_Face face, + FT_Stream stream ) { + FT_Memory memory = stream->memory; + FT_Error error; + + FT_Int num_glyphs; + FT_Int num_names; + + FT_UShort* glyph_indices = 0; + FT_Char** name_strings = 0; + + + if ( READ_UShort( num_glyphs ) ) { + goto Exit; + } + + /* UNDOCUMENTED! The number of glyphs in this table can be smaller */ + /* than the value in the maxp table (cf. cyberbit.ttf). */ + + /* There already exist fonts which have more than 32768 glyph names */ + /* in this table, so the test for this threshold has been dropped. */ + + if ( num_glyphs > face->root.num_glyphs ) { + error = TT_Err_Invalid_File_Format; + goto Exit; + } + + /* load the indices */ + { + FT_Int n; + + + if ( ALLOC_ARRAY( glyph_indices, num_glyphs, FT_UShort ) || + ACCESS_Frame( num_glyphs * 2L ) ) { + goto Fail; + } + + for ( n = 0; n < num_glyphs; n++ ) + glyph_indices[n] = GET_UShort(); + + FORGET_Frame(); + } + + /* compute number of names stored in table */ + { + FT_Int n; + + + num_names = 0; + + for ( n = 0; n < num_glyphs; n++ ) + { + FT_Int index; + + + index = glyph_indices[n]; + if ( index >= 258 ) { + index -= 257; + if ( index > num_names ) { + num_names = index; + } + } + } + } + + /* now load the name strings */ + { + FT_Int n; + + + if ( ALLOC_ARRAY( name_strings, num_names, FT_Char* ) ) { + goto Fail; + } + + for ( n = 0; n < num_names; n++ ) + { + FT_UInt len; + + + if ( READ_Byte( len ) || + ALLOC_ARRAY( name_strings[n], len + 1, FT_Char ) || + FILE_Read( name_strings[n], len ) ) { + goto Fail1; + } + + name_strings[n][len] = '\0'; + } + } + + /* all right, set table fields and exit successfuly */ + { + TT_Post_20* table = &face->postscript_names.names.format_20; + + + table->num_glyphs = num_glyphs; + table->num_names = num_names; + table->glyph_indices = glyph_indices; + table->glyph_names = name_strings; + } + return TT_Err_Ok; + + +Fail1: + { + FT_Int n; + + + for ( n = 0; n < num_names; n++ ) + FREE( name_strings[n] ); + } + +Fail: + FREE( name_strings ); + FREE( glyph_indices ); + +Exit: + return error; +} + + +static +FT_Error Load_Format_25( TT_Face face, + FT_Stream stream ) { + FT_Memory memory = stream->memory; + FT_Error error; + + FT_Int num_glyphs; + FT_Char* offset_table = 0; + + + /* UNDOCUMENTED! This value appears only in the Apple TT specs. */ + if ( READ_UShort( num_glyphs ) ) { + goto Exit; + } + + /* check the number of glyphs */ + if ( num_glyphs > face->root.num_glyphs || num_glyphs > 258 ) { + error = TT_Err_Invalid_File_Format; + goto Exit; + } + + if ( ALLOC( offset_table, num_glyphs ) || + FILE_Read( offset_table, num_glyphs ) ) { + goto Fail; + } + + /* now check the offset table */ + { + FT_Int n; + + + for ( n = 0; n < num_glyphs; n++ ) + { + FT_Long index = (FT_Long)n + offset_table[n]; + + + if ( index < 0 || index > num_glyphs ) { + error = TT_Err_Invalid_File_Format; + goto Fail; + } + } + } + + /* OK, set table fields and exit successfuly */ + { + TT_Post_25* table = &face->postscript_names.names.format_25; + + + table->num_glyphs = num_glyphs; + table->offsets = offset_table; + } + + return TT_Err_Ok; + +Fail: + FREE( offset_table ); + +Exit: + return error; +} + + +static +FT_Error Load_Post_Names( TT_Face face ) { + FT_Stream stream; + FT_Error error; + + /* get a stream for the face's resource */ + stream = face->root.stream; + + /* seek to the beginning of the PS names table */ + error = face->goto_table( face, TTAG_post, stream, 0 ); + if ( error ) { + goto Exit; + } + + /* now read postscript table */ + switch ( face->postscript.FormatType ) + { + case 0x00020000L: + error = Load_Format_20( face, stream ); + break; + + case 0x00028000L: + error = Load_Format_25( face, stream ); + break; + + default: + error = TT_Err_Invalid_File_Format; + } + + face->postscript_names.loaded = 1; + +Exit: + return error; +} + + +LOCAL_FUNC +void TT_Free_Post_Names( TT_Face face ) { + FT_Memory memory = face->root.memory; + TT_Post_Names* names = &face->postscript_names; + + + if ( names->loaded ) { + switch ( face->postscript.FormatType ) + { + case 0x00020000L: + { + TT_Post_20* table = &names->names.format_20; + FT_UInt n; + + + FREE( table->glyph_indices ); + table->num_glyphs = 0; + + for ( n = 0; n < table->num_names; n++ ) + FREE( table->glyph_names[n] ); + + FREE( table->glyph_names ); + table->num_names = 0; + } + break; + + case 0x00028000L: + { + TT_Post_25* table = &names->names.format_25; + + + FREE( table->offsets ); + table->num_glyphs = 0; + } + break; + } + } + names->loaded = 0; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Get_PS_Name */ +/* */ +/* */ +/* Gets the PostScript glyph name of a glyph. */ +/* */ +/* */ +/* face :: A handle to the parent face. */ +/* */ +/* index :: The glyph index. */ +/* */ +/* PSname :: The address of a string pointer. Will be NULL in case */ +/* of error, otherwise it is a pointer to the glyph name. */ +/* */ +/* You must not modify the returned string! */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Get_PS_Name( TT_Face face, + FT_UInt index, + FT_String** PSname ) { + FT_Error error; + TT_Post_Names* names; + +#ifdef FT_CONFIG_OPTION_POSTSCRIPT_NAMES + PSNames_Interface* psnames; +#endif + + + if ( !face ) { + return TT_Err_Invalid_Face_Handle; + } + + if ( index >= (FT_UInt)face->root.num_glyphs ) { + return TT_Err_Invalid_Glyph_Index; + } + +#ifdef FT_CONFIG_OPTION_POSTSCRIPT_NAMES + psnames = (PSNames_Interface*)face->psnames; + if ( !psnames ) { + return TT_Err_Unimplemented_Feature; + } +#endif + + names = &face->postscript_names; + + /* `.notdef' by default */ + *PSname = MAC_NAME( 0 ); + + switch ( face->postscript.FormatType ) + { + case 0x00010000L: + if ( index < 258 ) { /* paranoid checking */ + *PSname = MAC_NAME( index ); + } + break; + + case 0x00020000L: + { + TT_Post_20* table = &names->names.format_20; + + + if ( !names->loaded ) { + error = Load_Post_Names( face ); + if ( error ) { + break; + } + } + + if ( index < table->num_glyphs ) { + FT_UShort name_index = table->glyph_indices[index]; + + + if ( name_index < 258 ) { + *PSname = MAC_NAME( name_index ); + } else { + *PSname = (FT_String*)table->glyph_names[name_index - 258]; + } + } + } + break; + + case 0x00028000L: + { + TT_Post_25* table = &names->names.format_25; + + + if ( !names->loaded ) { + error = Load_Post_Names( face ); + if ( error ) { + break; + } + } + + if ( index < table->num_glyphs ) { /* paranoid checking */ + index += table->offsets[index]; + *PSname = MAC_NAME( index ); + } + } + break; + + case 0x00030000L: + break; /* nothing to do */ + } + + return TT_Err_Ok; +} + + +/* END */ diff --git a/src/ft2/ttpost.h b/src/ft2/ttpost.h new file mode 100644 index 0000000..86708f7 --- /dev/null +++ b/src/ft2/ttpost.h @@ -0,0 +1,52 @@ +/***************************************************************************/ +/* */ +/* ttpost.h */ +/* */ +/* Postcript name table processing for TrueType and OpenType fonts */ +/* (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef TTPOST_H +#define TTPOST_H + +#include "ftconfig.h" +#include "tttypes.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +#define TT_Err_Invalid_Post_Table_Format 0x0B00 +#define TT_Err_Invalid_Post_Table 0x0B01 + + +LOCAL_DEF +FT_Error TT_Get_PS_Name( TT_Face face, + FT_UInt index, + FT_String** PSname ); + +LOCAL_DEF +void TT_Free_Post_Names( TT_Face face ); + + +#ifdef __cplusplus +} +#endif + + +#endif /* TTPOST_H */ + + +/* END */ diff --git a/src/ft2/ttsbit.c b/src/ft2/ttsbit.c new file mode 100644 index 0000000..bb7bcaf --- /dev/null +++ b/src/ft2/ttsbit.c @@ -0,0 +1,1449 @@ +/***************************************************************************/ +/* */ +/* ttsbit.c */ +/* */ +/* TrueType and OpenType embedded bitmap support (body). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#include "ftdebug.h" +#include "tterrors.h" +#include "tttags.h" + +#include "ttsbit.h" + + + +/*************************************************************************/ +/* */ +/* The macro FT_COMPONENT is used in trace mode. It is an implicit */ +/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ +/* messages during execution. */ +/* */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_ttsbit + + +/*************************************************************************/ +/* */ +/* */ +/* blit_sbit */ +/* */ +/* */ +/* Blits a bitmap from an input stream into a given target. Supports */ +/* x and y offsets as well as byte padded lines. */ +/* */ +/* */ +/* target :: The target bitmap/pixmap. */ +/* */ +/* source :: The input packed bitmap data. */ +/* */ +/* line_bits :: The number of bits per line. */ +/* */ +/* byte_padded :: A flag which is true if lines are byte-padded. */ +/* */ +/* x_offset :: The horizontal offset. */ +/* */ +/* y_offset :: The vertical offset. */ +/* */ +/* */ +/* IMPORTANT: The x and y offsets are relative to the top corner of */ +/* the target bitmap (unlike the normal TrueType */ +/* convention). A positive y offset indicates a downwards */ +/* direction! */ +/* */ +static +void blit_sbit( FT_Bitmap* target, + FT_Byte* source, + FT_Int line_bits, + FT_Bool byte_padded, + FT_Int x_offset, + FT_Int y_offset ) { + FT_Byte* line_buff; + FT_Int line_incr; + FT_Int height; + + FT_UShort acc; + FT_Byte loaded; + + + /* first of all, compute starting write position */ + line_incr = target->pitch; + line_buff = target->buffer; + + if ( line_incr < 0 ) { + line_buff -= line_incr * ( target->rows - 1 ); + } + + line_buff += ( x_offset >> 3 ) + y_offset * line_incr; + + /***********************************************************************/ + /* */ + /* We use the extra-classic `accumulator' trick to extract the bits */ + /* from the source byte stream. */ + /* */ + /* Namely, the variable `acc' is a 16-bit accumulator containing the */ + /* last `loaded' bits from the input stream. The bits are shifted to */ + /* the upmost position in `acc'. */ + /* */ + /***********************************************************************/ + + acc = 0; /* clear accumulator */ + loaded = 0; /* no bits were loaded */ + + for ( height = target->rows; height > 0; height-- ) + { + FT_Byte* cur = line_buff; /* current write cursor */ + FT_Int count = line_bits; /* # of bits to extract per line */ + FT_Byte shift = x_offset & 7; /* current write shift */ + FT_Byte space = 8 - shift; + + + /* first of all, read individual source bytes */ + if ( count >= 8 ) { + count -= 8; + { + do + { + FT_Byte val; + + + /* ensure that there are at least 8 bits in the accumulator */ + if ( loaded < 8 ) { + acc |= ( FT_UShort ) * source++ << ( 8 - loaded ); + loaded += 8; + } + + /* now write one byte */ + val = (FT_Byte)( acc >> 8 ); + if ( shift ) { + cur[0] |= val >> shift; + cur[1] |= val << space; + } else { + cur[0] |= val; + } + + cur++; + acc <<= 8; /* remove bits from accumulator */ + loaded -= 8; + count -= 8; + + } while ( count >= 0 ); + } + + /* restore `count' to correct value */ + count += 8; + } + + /* now write remaining bits (count < 8) */ + if ( count > 0 ) { + FT_Byte val; + + + /* ensure that there are at least `count' bits in the accumulator */ + if ( loaded < count ) { + acc |= ( FT_UShort ) * source++ << ( 8 - loaded ); + loaded += 8; + } + + /* now write remaining bits */ + val = ( (FT_Byte)( acc >> 8 ) ) & ~( 0xFF >> count ); + cur[0] |= val >> shift; + + if ( count > space ) { + cur[1] |= val << space; + } + + acc <<= count; + loaded -= count; + } + + /* now, skip to next line */ + if ( byte_padded ) { + acc = loaded = 0; /* clear accumulator on byte-padded lines */ + + } + line_buff += line_incr; + } +} + + +const FT_Frame_Field sbit_metrics_fields[] = +{ + FT_FRAME_START( 8 ), + FT_FRAME_BYTE( TT_SBit_Metrics, height ), + FT_FRAME_BYTE( TT_SBit_Metrics, width ), + + FT_FRAME_CHAR( TT_SBit_Metrics, horiBearingX ), + FT_FRAME_CHAR( TT_SBit_Metrics, horiBearingY ), + FT_FRAME_BYTE( TT_SBit_Metrics, horiAdvance ), + + FT_FRAME_CHAR( TT_SBit_Metrics, vertBearingX ), + FT_FRAME_CHAR( TT_SBit_Metrics, vertBearingY ), + FT_FRAME_BYTE( TT_SBit_Metrics, vertAdvance ), + FT_FRAME_END +}; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_SBit_Const_Metrics */ +/* */ +/* */ +/* Loads the metrics for `EBLC' index tables format 2 and 5. */ +/* */ +/* */ +/* range :: The target range. */ +/* */ +/* stream :: The input stream. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +static +FT_Error Load_SBit_Const_Metrics( TT_SBit_Range* range, + FT_Stream stream ) { + FT_Error error; + + + if ( READ_ULong( range->image_size ) ) { + return error; + } + + return READ_Fields( sbit_metrics_fields, &range->metrics ); +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_SBit_Range_Codes */ +/* */ +/* */ +/* Loads the range codes for `EBLC' index tables format 4 and 5. */ +/* */ +/* */ +/* range :: The target range. */ +/* */ +/* stream :: The input stream. */ +/* */ +/* load_offsets :: A flag whether to load the glyph offset table. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +static +FT_Error Load_SBit_Range_Codes( TT_SBit_Range* range, + FT_Stream stream, + FT_Bool load_offsets ) { + FT_Error error; + FT_ULong count, n, size; + FT_Memory memory = stream->memory; + + + if ( READ_ULong( count ) ) { + goto Exit; + } + + range->num_glyphs = count; + + /* Allocate glyph offsets table if needed */ + if ( load_offsets ) { + if ( ALLOC_ARRAY( range->glyph_offsets, count, FT_ULong ) ) { + goto Exit; + } + + size = count * 4L; + } else { + size = count * 2L; + } + + /* Allocate glyph codes table and access frame */ + if ( ALLOC_ARRAY( range->glyph_codes, count, FT_UShort ) || + ACCESS_Frame( size ) ) { + goto Exit; + } + + for ( n = 0; n < count; n++ ) + { + range->glyph_codes[n] = GET_UShort(); + + if ( load_offsets ) { + range->glyph_offsets[n] = (FT_ULong)range->image_offset + + GET_UShort(); + } + } + + FORGET_Frame(); + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_SBit_Range */ +/* */ +/* */ +/* Loads a given `EBLC' index/range table. */ +/* */ +/* */ +/* range :: The target range. */ +/* */ +/* stream :: The input stream. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +static +FT_Error Load_SBit_Range( TT_SBit_Range* range, + FT_Stream stream ) { + FT_Error error; + FT_Memory memory = stream->memory; + + + switch ( range->index_format ) + { + case 1: /* variable metrics with 4-byte offsets */ + case 3: /* variable metrics with 2-byte offsets */ + { + FT_ULong num_glyphs, n; + FT_Int size_elem; + FT_Bool large = ( range->index_format == 1 ); + + + num_glyphs = range->last_glyph - range->first_glyph + 1L; + range->num_glyphs = num_glyphs; + num_glyphs++; /* XXX: BEWARE - see spec */ + + size_elem = large ? 4 : 2; + + if ( ALLOC_ARRAY( range->glyph_offsets, + num_glyphs, FT_ULong ) || + ACCESS_Frame( num_glyphs * size_elem ) ) { + goto Exit; + } + + for ( n = 0; n < num_glyphs; n++ ) + range->glyph_offsets[n] = (FT_ULong)( range->image_offset + + ( large ? GET_ULong() + : GET_UShort() ) ); + FORGET_Frame(); + } + break; + + case 2: /* all glyphs have identical metrics */ + error = Load_SBit_Const_Metrics( range, stream ); + break; + + case 4: + error = Load_SBit_Range_Codes( range, stream, 1 ); + break; + + case 5: + error = Load_SBit_Const_Metrics( range, stream ) || + Load_SBit_Range_Codes( range, stream, 0 ); + break; + + default: + error = TT_Err_Invalid_File_Format; + } + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_SBit_Strikes */ +/* */ +/* */ +/* Loads the table of embedded bitmap sizes for this face. */ +/* */ +/* */ +/* face :: The target face object. */ +/* */ +/* stream :: The input stream. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +LOCAL_FUNC +FT_Error TT_Load_SBit_Strikes( TT_Face face, + FT_Stream stream ) { + FT_Error error = 0; + FT_Memory memory = stream->memory; + FT_Fixed version; + FT_ULong num_strikes; + FT_ULong table_base; + + const FT_Frame_Field sbit_line_metrics_fields[] = + { + /* no FT_FRAME_START */ + FT_FRAME_CHAR( TT_SBit_Line_Metrics, ascender ), + FT_FRAME_CHAR( TT_SBit_Line_Metrics, descender ), + FT_FRAME_BYTE( TT_SBit_Line_Metrics, max_width ), + + FT_FRAME_CHAR( TT_SBit_Line_Metrics, caret_slope_numerator ), + FT_FRAME_CHAR( TT_SBit_Line_Metrics, caret_slope_denominator ), + FT_FRAME_CHAR( TT_SBit_Line_Metrics, caret_offset ), + + FT_FRAME_CHAR( TT_SBit_Line_Metrics, min_origin_SB ), + FT_FRAME_CHAR( TT_SBit_Line_Metrics, min_advance_SB ), + FT_FRAME_CHAR( TT_SBit_Line_Metrics, max_before_BL ), + FT_FRAME_CHAR( TT_SBit_Line_Metrics, min_after_BL ), + FT_FRAME_CHAR( TT_SBit_Line_Metrics, pads[0] ), + FT_FRAME_CHAR( TT_SBit_Line_Metrics, pads[1] ), + FT_FRAME_END + }; + + const FT_Frame_Field strike_start_fields[] = + { + /* no FT_FRAME_START */ + FT_FRAME_ULONG( TT_SBit_Strike, ranges_offset ), + FT_FRAME_SKIP_LONG, + FT_FRAME_ULONG( TT_SBit_Strike, num_ranges ), + FT_FRAME_ULONG( TT_SBit_Strike, color_ref ), + FT_FRAME_END + }; + + const FT_Frame_Field strike_end_fields[] = + { + /* no FT_FRAME_START */ + FT_FRAME_USHORT( TT_SBit_Strike, start_glyph ), + FT_FRAME_USHORT( TT_SBit_Strike, end_glyph ), + FT_FRAME_BYTE( TT_SBit_Strike, x_ppem ), + FT_FRAME_BYTE( TT_SBit_Strike, y_ppem ), + FT_FRAME_BYTE( TT_SBit_Strike, bit_depth ), + FT_FRAME_CHAR( TT_SBit_Strike, flags ), + FT_FRAME_END + }; + + + face->num_sbit_strikes = 0; + + /* this table is optional */ + error = face->goto_table( face, TTAG_EBLC, stream, 0 ); + if ( error ) { + error = face->goto_table( face, TTAG_bloc, stream, 0 ); + } + if ( error ) { + error = 0; + goto Exit; + } + + table_base = FILE_Pos(); + if ( ACCESS_Frame( 8L ) ) { + goto Exit; + } + + version = GET_Long(); + num_strikes = GET_ULong(); + + FORGET_Frame(); + + /* check version number and strike count */ + if ( version != 0x00020000L || + num_strikes >= 0x10000L ) { + FT_ERROR( ( "TT_Load_SBit_Strikes: invalid table version!\n" ) ); + error = TT_Err_Invalid_File_Format; + + goto Exit; + } + + /* allocate the strikes table */ + if ( ALLOC_ARRAY( face->sbit_strikes, num_strikes, TT_SBit_Strike ) ) { + goto Exit; + } + + face->num_sbit_strikes = num_strikes; + + /* now read each strike table separately */ + { + TT_SBit_Strike* strike = face->sbit_strikes; + FT_ULong count = num_strikes; + + + if ( ACCESS_Frame( 48L * num_strikes ) ) { + goto Exit; + } + + while ( count > 0 ) + { + (void)READ_Fields( strike_start_fields, strike ); + + (void)READ_Fields( sbit_line_metrics_fields, &strike->hori ); + (void)READ_Fields( sbit_line_metrics_fields, &strike->vert ); + + (void)READ_Fields( strike_end_fields, strike ); + + count--; + strike++; + } + + FORGET_Frame(); + } + + /* allocate the index ranges for each strike table */ + { + TT_SBit_Strike* strike = face->sbit_strikes; + FT_ULong count = num_strikes; + + + while ( count > 0 ) + { + TT_SBit_Range* range; + FT_ULong count2 = strike->num_ranges; + + + if ( ALLOC_ARRAY( strike->sbit_ranges, + strike->num_ranges, + TT_SBit_Range ) ) { + goto Exit; + } + + /* read each range */ + if ( FILE_Seek( table_base + strike->ranges_offset ) || + ACCESS_Frame( strike->num_ranges * 8L ) ) { + goto Exit; + } + + range = strike->sbit_ranges; + while ( count2 > 0 ) + { + range->first_glyph = GET_UShort(); + range->last_glyph = GET_UShort(); + range->table_offset = table_base + strike->ranges_offset + + GET_ULong(); + count2--; + range++; + } + + FORGET_Frame(); + + /* Now, read each index table */ + count2 = strike->num_ranges; + range = strike->sbit_ranges; + while ( count2 > 0 ) + { + /* Read the header */ + if ( FILE_Seek( range->table_offset ) || + ACCESS_Frame( 8L ) ) { + goto Exit; + } + + range->index_format = GET_UShort(); + range->image_format = GET_UShort(); + range->image_offset = GET_ULong(); + + FORGET_Frame(); + + error = Load_SBit_Range( range, stream ); + if ( error ) { + goto Exit; + } + + count2--; + range++; + } + + count--; + strike++; + } + } + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Free_SBit_Strikes */ +/* */ +/* */ +/* Releases the embedded bitmap tables. */ +/* */ +/* */ +/* face :: The target face object. */ +/* */ +LOCAL_FUNC +void TT_Free_SBit_Strikes( TT_Face face ) { + FT_Memory memory = face->root.memory; + TT_SBit_Strike* strike = face->sbit_strikes; + TT_SBit_Strike* strike_limit = strike + face->num_sbit_strikes; + + + if ( strike ) { + for ( ; strike < strike_limit; strike++ ) + { + TT_SBit_Range* range = strike->sbit_ranges; + TT_SBit_Range* range_limit = range + strike->num_ranges; + + + if ( range ) { + for ( ; range < range_limit; range++ ) + { + /* release the glyph offsets and codes tables */ + /* where appropriate */ + FREE( range->glyph_offsets ); + FREE( range->glyph_codes ); + } + } + FREE( strike->sbit_ranges ); + strike->num_ranges = 0; + } + FREE( face->sbit_strikes ); + } + face->num_sbit_strikes = 0; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Find_SBit_Range */ +/* */ +/* */ +/* Scans a given strike's ranges and return, for a given glyph */ +/* index, the corresponding sbit range, and `EBDT' offset. */ +/* */ +/* */ +/* glyph_index :: The glyph index. */ +/* strike :: The source/current sbit strike. */ +/* */ +/* */ +/* arange :: The sbit range containing the glyph index. */ +/* aglyph_offset :: The offset of the glyph data in `EBDT' table. */ +/* */ +/* */ +/* FreeType error code. 0 means the glyph index was found. */ +/* */ +static +FT_Error Find_SBit_Range( FT_UInt glyph_index, + TT_SBit_Strike* strike, + TT_SBit_Range** arange, + FT_ULong* aglyph_offset ) { + TT_SBit_Range *range, *range_limit; + + + /* check whether the glyph index is within this strike's */ + /* glyph range */ + if ( glyph_index < strike->start_glyph || + glyph_index > strike->end_glyph ) { + goto Fail; + } + + /* scan all ranges in strike */ + range = strike->sbit_ranges; + range_limit = range + strike->num_ranges; + if ( !range ) { + goto Fail; + } + + for ( ; range < range_limit; range++ ) + { + if ( glyph_index >= range->first_glyph && + glyph_index <= range->last_glyph ) { + FT_UShort delta = glyph_index - range->first_glyph; + + + switch ( range->index_format ) + { + case 1: + case 3: + *aglyph_offset = range->glyph_offsets[delta]; + break; + + case 2: + *aglyph_offset = range->image_offset + + range->image_size * delta; + break; + + case 4: + case 5: + { + FT_ULong n; + + + for ( n = 0; n < range->num_glyphs; n++ ) + { + if ( range->glyph_codes[n] == glyph_index ) { + if ( range->index_format == 4 ) { + *aglyph_offset = range->glyph_offsets[n]; + } else { + *aglyph_offset = range->image_offset + + n * range->image_size; + } + goto Found; + } + } + } + + /* fall-through */ + default: + goto Fail; + } + +Found: + /* return successfully! */ + *arange = range; + return 0; + } + } + +Fail: + *arange = 0; + *aglyph_offset = 0; + + return TT_Err_Invalid_Argument; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Find_SBit_Image */ +/* */ +/* */ +/* Checks whether an embedded bitmap (an `sbit') exists for a given */ +/* glyph, at given x and y ppems. */ +/* */ +/* */ +/* face :: The target face object. */ +/* glyph_index :: The glyph index. */ +/* x_ppem :: The horizontal resolution in points per EM. */ +/* y_ppem :: The vertical resolution in points per EM. */ +/* */ +/* */ +/* arange :: The SBit range containing the glyph index. */ +/* astrike :: The SBit strike containing the glyph index. */ +/* aglyph_offset :: The offset of the glyph data in `EBDT' table. */ +/* */ +/* */ +/* FreeType error code. 0 means success. Returns */ +/* TT_Err_Invalid_Argument if no sbit exists for the requested glyph. */ +/* */ +static +FT_Error Find_SBit_Image( TT_Face face, + FT_UInt glyph_index, + FT_Int x_ppem, + FT_Int y_ppem, + + TT_SBit_Range** arange, + TT_SBit_Strike** astrike, + FT_ULong* aglyph_offset ) { + TT_SBit_Strike* strike = face->sbit_strikes; + TT_SBit_Strike* strike_limit = strike + face->num_sbit_strikes; + + + if ( !strike ) { + goto Fail; + } + + for ( ; strike < strike_limit; strike++ ) + { + if ( strike->x_ppem == x_ppem && strike->y_ppem == y_ppem ) { + FT_Error error; + + + error = Find_SBit_Range( glyph_index, strike, + arange, aglyph_offset ); + if ( error ) { + goto Fail; + } + + *astrike = strike; + + return TT_Err_Ok; + } + } + +Fail: + /* no embedded bitmap for this glyph in face */ + *arange = 0; + *astrike = 0; + *aglyph_offset = 0; + + return TT_Err_Invalid_Argument; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Load_SBit_Metrics */ +/* */ +/* */ +/* Gets the big metrics for a given SBit. */ +/* */ +/* */ +/* stream :: The input stream. */ +/* */ +/* range :: The SBit range containing the glyph. */ +/* */ +/* */ +/* big_metrics :: A big SBit metrics structure for the glyph. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* The stream cursor must be positioned at the glyph's offset within */ +/* the `EBDT' table before the call. */ +/* */ +/* If the image format uses variable metrics, the stream cursor is */ +/* positioned just after the metrics header in the `EBDT' table on */ +/* function exit. */ +/* */ +static +FT_Error Load_SBit_Metrics( FT_Stream stream, + TT_SBit_Range* range, + TT_SBit_Metrics* metrics ) { + FT_Error error = TT_Err_Ok; + + + switch ( range->image_format ) + { + case 1: + case 2: + case 8: + /* variable small metrics */ + { + TT_SBit_Small_Metrics smetrics; + + const FT_Frame_Field sbit_small_metrics_fields[] = + { + FT_FRAME_START( 5 ), + FT_FRAME_BYTE( TT_SBit_Small_Metrics, height ), + FT_FRAME_BYTE( TT_SBit_Small_Metrics, width ), + FT_FRAME_CHAR( TT_SBit_Small_Metrics, bearingX ), + FT_FRAME_CHAR( TT_SBit_Small_Metrics, bearingY ), + FT_FRAME_BYTE( TT_SBit_Small_Metrics, advance ), + FT_FRAME_END + }; + + + /* read small metrics */ + if ( READ_Fields( sbit_small_metrics_fields, &smetrics ) ) { + goto Exit; + } + + /* convert it to a big metrics */ + metrics->height = smetrics.height; + metrics->width = smetrics.width; + metrics->horiBearingX = smetrics.bearingX; + metrics->horiBearingY = smetrics.bearingY; + metrics->horiAdvance = smetrics.advance; + + /* these metrics are made up at a higher level when */ + /* needed. */ + metrics->vertBearingX = 0; + metrics->vertBearingY = 0; + metrics->vertAdvance = 0; + } + break; + + case 6: + case 7: + case 9: + /* variable big metrics */ + (void)READ_Fields( sbit_metrics_fields, metrics ); + break; + + case 5: + default: /* constant metrics */ + if ( range->index_format == 2 || range->index_format == 5 ) { + *metrics = range->metrics; + } else { + return TT_Err_Invalid_File_Format; + } + } + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* Crop_Bitmap */ +/* */ +/* */ +/* Crops a bitmap to its tightest bounding box, and adjusts its */ +/* metrics. */ +/* */ +/* */ +/* image :: The input glyph slot. */ +/* */ +/* metrics :: The corresponding metrics structure. */ +/* */ +static +void Crop_Bitmap( FT_Bitmap* map, + TT_SBit_Metrics* metrics ) { + /***********************************************************************/ + /* */ + /* In this situation, some bounding boxes of embedded bitmaps are too */ + /* large. We need to crop it to a reasonable size. */ + /* */ + /* --------- */ + /* | | ----- */ + /* | *** | |***| */ + /* | * | | * | */ + /* | * | ------> | * | */ + /* | * | | * | */ + /* | * | | * | */ + /* | *** | |***| */ + /* --------- ----- */ + /* */ + /***********************************************************************/ + + FT_Int rows, count; + FT_Long line_len; + FT_Byte* line; + + + /***********************************************************************/ + /* */ + /* first of all, check the top-most lines of the bitmap, and remove */ + /* them if they're empty. */ + /* */ + { + line = (FT_Byte*)map->buffer; + rows = map->rows; + line_len = map->pitch; + + + for ( count = 0; count < rows; count++ ) + { + FT_Byte* cur = line; + FT_Byte* limit = line + line_len; + + + for ( ; cur < limit; cur++ ) + if ( cur[0] ) { + goto Found_Top; + } + + /* the current line was empty - skip to next one */ + line = limit; + } + +Found_Top: + /* check that we have at least one filled line */ + if ( count >= rows ) { + goto Empty_Bitmap; + } + + /* now, crop the empty upper lines */ + if ( count > 0 ) { + line = (FT_Byte*)map->buffer; + + MEM_Move( line, line + count * line_len, + ( rows - count ) * line_len ); + + metrics->height -= count; + metrics->horiBearingY -= count; + metrics->vertBearingY -= count; + + map->rows -= count; + rows -= count; + } + } + + /***********************************************************************/ + /* */ + /* second, crop the lower lines */ + /* */ + { + line = (FT_Byte*)map->buffer + ( rows - 1 ) * line_len; + + for ( count = 0; count < rows; count++ ) + { + FT_Byte* cur = line; + FT_Byte* limit = line + line_len; + + + for ( ; cur < limit; cur++ ) + if ( cur[0] ) { + goto Found_Bottom; + } + + /* the current line was empty - skip to previous one */ + line -= line_len; + } + +Found_Bottom: + if ( count > 0 ) { + metrics->height -= count; + rows -= count; + map->rows -= count; + } + } + + /***********************************************************************/ + /* */ + /* third, get rid of the space on the left side of the glyph */ + /* */ + do + { + FT_Byte* limit; + + + line = (FT_Byte*)map->buffer; + limit = line + rows * line_len; + + for ( ; line < limit; line += line_len ) + if ( line[0] & 0x80 ) { + goto Found_Left; + } + + /* shift the whole glyph one pixel to the left */ + line = (FT_Byte*)map->buffer; + limit = line + rows * line_len; + + for ( ; line < limit; line += line_len ) + { + FT_Int n, width = map->width; + FT_Byte old; + FT_Byte* cur = line; + + + old = cur[0] << 1; + for ( n = 8; n < width; n += 8 ) + { + FT_Byte val; + + + val = cur[1]; + cur[0] = old | ( val >> 7 ); + old = val << 1; + cur++; + } + cur[0] = old; + } + + map->width--; + metrics->horiBearingX++; + metrics->vertBearingX++; + metrics->width--; + + } while ( map->width > 0 ); + +Found_Left: + + /***********************************************************************/ + /* */ + /* finally, crop the bitmap width to get rid of the space on the right */ + /* side of the glyph. */ + /* */ + do + { + FT_Int right = map->width - 1; + FT_Byte* limit; + FT_Byte mask; + + + line = (FT_Byte*)map->buffer + ( right >> 3 ); + limit = line + rows * line_len; + mask = 0x80 >> ( right & 7 ); + + for ( ; line < limit; line += line_len ) + if ( line[0] & mask ) { + goto Found_Right; + } + + /* crop the whole glyph to the right */ + map->width--; + metrics->width--; + + } while ( map->width > 0 ); + +Found_Right: + /* all right, the bitmap was cropped */ + return; + +Empty_Bitmap: + map->width = 0; + map->rows = 0; + map->pitch = 0; + map->pixel_mode = ft_pixel_mode_mono; +} + + +static +FT_Error Load_SBit_Single( FT_Bitmap* map, + FT_Int x_offset, + FT_Int y_offset, + FT_Int pix_bits, + FT_UShort image_format, + TT_SBit_Metrics* metrics, + FT_Stream stream ) { + FT_Error error; + + + /* check that the source bitmap fits into the target pixmap */ + if ( x_offset < 0 || x_offset + metrics->width > map->width || + y_offset < 0 || y_offset + metrics->height > map->rows ) { + error = TT_Err_Invalid_Argument; + + goto Exit; + } + + { + FT_Int glyph_width = metrics->width; + FT_Int glyph_height = metrics->height; + FT_Int glyph_size; + FT_Int line_bits = pix_bits * glyph_width; + FT_Bool pad_bytes = 0; + + + /* compute size of glyph image */ + switch ( image_format ) + { + case 1: /* byte-padded formats */ + case 6: + { + FT_Int line_length; + + + switch ( pix_bits ) + { + case 1: line_length = ( glyph_width + 7 ) >> 3; break; + case 2: line_length = ( glyph_width + 3 ) >> 2; break; + case 4: line_length = ( glyph_width + 1 ) >> 1; break; + default: line_length = glyph_width; + } + + glyph_size = glyph_height * line_length; + pad_bytes = 1; + } + break; + + case 2: + case 5: + case 7: + line_bits = glyph_width * pix_bits; + glyph_size = ( glyph_height * line_bits + 7 ) >> 3; + break; + + default: /* invalid format */ + return TT_Err_Invalid_File_Format; + } + + /* Now read data and draw glyph into target pixmap */ + if ( ACCESS_Frame( glyph_size ) ) { + goto Exit; + } + + /* don't forget to multiply `x_offset' by `map->pix_bits' as */ + /* the sbit blitter doesn't make a difference between pixmap */ + /* depths. */ + blit_sbit( map, (FT_Byte*)stream->cursor, line_bits, pad_bytes, + x_offset * pix_bits, y_offset ); + + FORGET_Frame(); + } + +Exit: + return error; +} + + +static +FT_Error Load_SBit_Image( TT_SBit_Strike* strike, + TT_SBit_Range* range, + FT_ULong ebdt_pos, + FT_ULong glyph_offset, + FT_Bitmap* map, + FT_Int x_offset, + FT_Int y_offset, + FT_Stream stream, + TT_SBit_Metrics* metrics ) { + FT_Memory memory = stream->memory; + FT_Error error; + + + /* place stream at beginning of glyph data and read metrics */ + if ( FILE_Seek( ebdt_pos + glyph_offset ) ) { + goto Exit; + } + + error = Load_SBit_Metrics( stream, range, metrics ); + if ( error ) { + goto Exit; + } + + /* this function is recursive. At the top-level call, the */ + /* field map.buffer is NULL. We thus begin by finding the */ + /* dimensions of the higher-level glyph to allocate the */ + /* final pixmap buffer */ + if ( map->buffer == 0 ) { + FT_Long size; + + + map->width = metrics->width; + map->rows = metrics->height; + + switch ( strike->bit_depth ) + { + case 1: + map->pixel_mode = ft_pixel_mode_mono; + map->pitch = ( map->width + 7 ) >> 3; + break; + + case 2: + map->pixel_mode = ft_pixel_mode_pal2; + map->pitch = ( map->width + 3 ) >> 2; + break; + + case 4: + map->pixel_mode = ft_pixel_mode_pal4; + map->pitch = ( map->width + 1 ) >> 1; + break; + + case 8: + map->pixel_mode = ft_pixel_mode_grays; + map->pitch = map->width; + break; + + default: + return TT_Err_Invalid_File_Format; + } + + size = map->rows * map->pitch; + + /* check that there is no empty image */ + if ( size == 0 ) { + goto Exit; /* exit successfully! */ + + } + if ( ALLOC( map->buffer, size ) ) { + goto Exit; + } + } + + switch ( range->image_format ) + { + case 1: /* single sbit image - load it */ + case 2: + case 5: + case 6: + case 7: + return Load_SBit_Single( map, x_offset, y_offset, strike->bit_depth, + range->image_format, metrics, stream ); + + case 8: /* compound format */ + FT_Skip_Stream( stream, 1L ); + /* fallthrough */ + + case 9: + break; + + default: /* invalid image format */ + return TT_Err_Invalid_File_Format; + } + + /* All right, we have a compound format. First of all, read */ + /* the array of elements. */ + { + TT_SBit_Component* components; + TT_SBit_Component* comp; + FT_UShort num_components, count; + + + if ( READ_UShort( num_components ) || + ALLOC_ARRAY( components, num_components, TT_SBit_Component ) ) { + goto Exit; + } + + count = num_components; + + if ( ACCESS_Frame( 4L * num_components ) ) { + goto Fail_Memory; + } + + for ( comp = components; count > 0; count--, comp++ ) + { + comp->glyph_code = GET_UShort(); + comp->x_offset = GET_Char(); + comp->y_offset = GET_Char(); + } + + FORGET_Frame(); + + /* Now recursively load each element glyph */ + count = num_components; + comp = components; + for ( ; count > 0; count--, comp++ ) + { + TT_SBit_Range* elem_range; + TT_SBit_Metrics elem_metrics; + FT_ULong elem_offset; + + + /* find the range for this element */ + error = Find_SBit_Range( comp->glyph_code, + strike, + &elem_range, + &elem_offset ); + if ( error ) { + goto Fail_Memory; + } + + /* now load the element, recursively */ + error = Load_SBit_Image( strike, + elem_range, + ebdt_pos, + elem_offset, + map, + x_offset + comp->x_offset, + y_offset + comp->y_offset, + stream, + &elem_metrics ); + if ( error ) { + goto Fail_Memory; + } + } + +Fail_Memory: + FREE( components ); + } + +Exit: + return error; +} + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_SBit_Image */ +/* */ +/* */ +/* Loads a given glyph sbit image from the font resource. This also */ +/* returns its metrics. */ +/* */ +/* */ +/* face :: The target face object. */ +/* */ +/* x_ppem :: The horizontal resolution in points per EM. */ +/* */ +/* y_ppem :: The vertical resolution in points per EM. */ +/* */ +/* glyph_index :: The current glyph index. */ +/* */ +/* load_flags :: The glyph load flags (the code checks for the flag */ +/* FT_LOAD_CROP_BITMAP */ +/* */ +/* stream :: The input stream. */ +/* */ +/* */ +/* map :: The target pixmap. */ +/* */ +/* metrics :: A big sbit metrics structure for the glyph image. */ +/* */ +/* */ +/* FreeType error code. 0 means success. Returns an error if no */ +/* glyph sbit exists for the index. */ +/* */ +/* */ +/* The `map.buffer' field is always freed before the glyph is loaded. */ +/* */ +LOCAL_FUNC +FT_Error TT_Load_SBit_Image( TT_Face face, + FT_Int x_ppem, + FT_Int y_ppem, + FT_UInt glyph_index, + FT_UInt load_flags, + FT_Stream stream, + FT_Bitmap* map, + TT_SBit_Metrics* metrics ) { + FT_Error error; + FT_Memory memory = stream->memory; + FT_ULong ebdt_pos, glyph_offset; + + TT_SBit_Strike* strike; + TT_SBit_Range* range; + + + /* Check whether there is a glyph sbit for the current index */ + error = Find_SBit_Image( face, glyph_index, x_ppem, y_ppem, + &range, &strike, &glyph_offset ); + if ( error ) { + goto Exit; + } + + /* now, find the location of the `EBDT' table in */ + /* the font file */ + error = face->goto_table( face, TTAG_EBDT, stream, 0 ); + if ( error ) { + error = face->goto_table( face, TTAG_bdat, stream, 0 ); + } + if ( error ) { + goto Exit; + } + + ebdt_pos = FILE_Pos(); + + /* clear the bitmap & load the bitmap */ + if ( face->root.glyph->flags & ft_glyph_own_bitmap ) { + FREE( map->buffer ); + } + + map->rows = map->pitch = map->width = 0; + + error = Load_SBit_Image( strike, range, ebdt_pos, glyph_offset, + map, 0, 0, stream, metrics ); + if ( error ) { + goto Exit; + } + + /* the glyph slot owns this bitmap buffer */ + face->root.glyph->flags |= ft_glyph_own_bitmap; + + /* setup vertical metrics if needed */ + if ( strike->flags & 1 ) { + /* in case of a horizontal strike only */ + FT_Int advance; + FT_Int top; + + + advance = strike->hori.ascender - strike->hori.descender; + top = advance / 10; + + /* some heuristic values */ + + metrics->vertBearingX = -metrics->width / 2; + metrics->vertBearingY = advance / 10; + metrics->vertAdvance = advance * 12 / 10; + } + + /* Crop the bitmap now, unless specified otherwise */ + if ( load_flags & FT_LOAD_CROP_BITMAP ) { + Crop_Bitmap( map, metrics ); + } + +Exit: + return error; +} + + +/* END */ diff --git a/src/ft2/ttsbit.h b/src/ft2/ttsbit.h new file mode 100644 index 0000000..0b3598e --- /dev/null +++ b/src/ft2/ttsbit.h @@ -0,0 +1,57 @@ +/***************************************************************************/ +/* */ +/* ttsbit.h */ +/* */ +/* TrueType and OpenType embedded bitmap support (specification). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef TTSBIT_H +#define TTSBIT_H + + +#include "ttload.h" + + +#ifdef __cplusplus +extern "C" { +#endif + + +LOCAL_DEF +FT_Error TT_Load_SBit_Strikes( TT_Face face, + FT_Stream stream ); + +LOCAL_DEF +void TT_Free_SBit_Strikes( TT_Face face ); + +LOCAL_DEF +FT_Error TT_Load_SBit_Image( TT_Face face, + FT_Int x_ppem, + FT_Int y_ppem, + FT_UInt glyph_index, + FT_UInt load_flags, + FT_Stream stream, + FT_Bitmap* map, + TT_SBit_Metrics* metrics ); + + +#ifdef __cplusplus +} +#endif + + +#endif /* TTSBIT_H */ + + +/* END */ diff --git a/src/ft2/tttables.h b/src/ft2/tttables.h new file mode 100644 index 0000000..030dd8b --- /dev/null +++ b/src/ft2/tttables.h @@ -0,0 +1,583 @@ +/***************************************************************************/ +/* */ +/* tttables.h */ +/* */ +/* Basic SFNT/TrueType tables definitions and interface */ +/* (specification only). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef TTTABLES_H +#define TTTABLES_H + + +#include "freetype.h" + + +#ifdef __cplusplus +extern "C" { +#endif + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Header */ +/* */ +/* */ +/* A structure used to model a TrueType font header table. All */ +/* fields follow the TrueType specification. */ +/* */ +typedef struct TT_Header_ +{ + FT_Fixed Table_Version; + FT_Fixed Font_Revision; + + FT_Long CheckSum_Adjust; + FT_Long Magic_Number; + + FT_UShort Flags; + FT_UShort Units_Per_EM; + + FT_Long Created [2]; + FT_Long Modified[2]; + + FT_Short xMin; + FT_Short yMin; + FT_Short xMax; + FT_Short yMax; + + FT_UShort Mac_Style; + FT_UShort Lowest_Rec_PPEM; + + FT_Short Font_Direction; + FT_Short Index_To_Loc_Format; + FT_Short Glyph_Data_Format; + +} TT_Header; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_HoriHeader */ +/* */ +/* */ +/* A structure used to model a TrueType horizontal header, the `hhea' */ +/* table, as well as the corresponding horizontal metrics table, */ +/* i.e., the `hmtx' table. */ +/* */ +/* */ +/* Version :: The table version. */ +/* */ +/* Ascender :: The font's ascender, i.e., the distance */ +/* from the baseline to the top-most of all */ +/* glyph points found in the font. */ +/* */ +/* This value is invalid in many fonts, as */ +/* it is usually set by the font designer, */ +/* and often reflects only a portion of the */ +/* glyphs found in the font (maybe ASCII). */ +/* */ +/* You should use the `sTypoAscender' field */ +/* of the OS/2 table instead if you want */ +/* the correct one. */ +/* */ +/* Descender :: The font's descender, i.e., the distance */ +/* from the baseline to the bottom-most of */ +/* all glyph points found in the font. It */ +/* is negative. */ +/* */ +/* This value is invalid in many fonts, as */ +/* it is usually set by the font designer, */ +/* and often reflects only a portion of the */ +/* glyphs found in the font (maybe ASCII). */ +/* */ +/* You should use the `sTypoDescender' */ +/* field of the OS/2 table instead if you */ +/* want the correct one. */ +/* */ +/* Line_Gap :: The font's line gap, i.e., the distance */ +/* to add to the ascender and descender to */ +/* get the BTB, i.e., the */ +/* baseline-to-baseline distance for the */ +/* font. */ +/* */ +/* advance_Width_Max :: This field is the maximum of all advance */ +/* widths found in the font. It can be */ +/* used to compute the maximum width of an */ +/* arbitrary string of text. */ +/* */ +/* min_Left_Side_Bearing :: The minimum left side bearing of all */ +/* glyphs within the font. */ +/* */ +/* min_Right_Side_Bearing :: The minimum right side bearing of all */ +/* glyphs within the font. */ +/* */ +/* xMax_Extent :: The maximum horizontal extent (i.e., the */ +/* `width' of a glyph's bounding box) for */ +/* all glyphs in the font. */ +/* */ +/* caret_Slope_Rise :: The rise coefficient of the cursor's */ +/* slope of the cursor (slope=rise/run). */ +/* */ +/* caret_Slope_Run :: The run coefficient of the cursor's */ +/* slope. */ +/* */ +/* Reserved :: 10 reserved bytes. */ +/* */ +/* metric_Data_Format :: Always 0. */ +/* */ +/* number_Of_HMetrics :: Number of HMetrics entries in the `hmtx' */ +/* table -- this value can be smaller than */ +/* the total number of glyphs in the font. */ +/* */ +/* long_metrics :: A pointer into the `hmtx' table. */ +/* */ +/* short_metrics :: A pointer into the `hmtx' table. */ +/* */ +/* */ +/* IMPORTANT: The TT_HoriHeader and TT_VertHeader structures should */ +/* be identical except for the names of their fields which */ +/* are different. */ +/* */ +/* This ensures that a single function in the `ttload' */ +/* module is able to read both the horizontal and vertical */ +/* headers. */ +/* */ +typedef struct TT_HoriHeader_ +{ + FT_Fixed Version; + FT_Short Ascender; + FT_Short Descender; + FT_Short Line_Gap; + + FT_UShort advance_Width_Max; /* advance width maximum */ + + FT_Short min_Left_Side_Bearing; /* minimum left-sb */ + FT_Short min_Right_Side_Bearing; /* minimum right-sb */ + FT_Short xMax_Extent; /* xmax extents */ + FT_Short caret_Slope_Rise; + FT_Short caret_Slope_Run; + FT_Short caret_Offset; + + FT_Short Reserved[4]; + + FT_Short metric_Data_Format; + FT_UShort number_Of_HMetrics; + + /* The following fields are not defined by the TrueType specification */ + /* but they're used to connect the metrics header to the relevant */ + /* `HMTX' table. */ + + void* long_metrics; + void* short_metrics; + +} TT_HoriHeader; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_VertHeader */ +/* */ +/* */ +/* A structure used to model a TrueType vertical header, the `vhea' */ +/* table, as well as the corresponding vertical metrics table, i.e., */ +/* the `vmtx' table. */ +/* */ +/* */ +/* Version :: The table version. */ +/* */ +/* Ascender :: The font's ascender, i.e., the distance */ +/* from the baseline to the top-most of */ +/* all glyph points found in the font. */ +/* */ +/* This value is invalid in many fonts, as */ +/* it is usually set by the font designer, */ +/* and often reflects only a portion of */ +/* the glyphs found in the font (maybe */ +/* ASCII). */ +/* */ +/* You should use the `sTypoAscender' */ +/* field of the OS/2 table instead if you */ +/* want the correct one. */ +/* */ +/* Descender :: The font's descender, i.e., the */ +/* distance from the baseline to the */ +/* bottom-most of all glyph points found */ +/* in the font. It is negative. */ +/* */ +/* This value is invalid in many fonts, as */ +/* it is usually set by the font designer, */ +/* and often reflects only a portion of */ +/* the glyphs found in the font (maybe */ +/* ASCII). */ +/* */ +/* You should use the `sTypoDescender' */ +/* field of the OS/2 table instead if you */ +/* want the correct one. */ +/* */ +/* Line_Gap :: The font's line gap, i.e., the distance */ +/* to add to the ascender and descender to */ +/* get the BTB, i.e., the */ +/* baseline-to-baseline distance for the */ +/* font. */ +/* */ +/* advance_Height_Max :: This field is the maximum of all */ +/* advance heights found in the font. It */ +/* can be used to compute the maximum */ +/* height of an arbitrary string of text. */ +/* */ +/* min_Top_Side_Bearing :: The minimum top side bearing of all */ +/* glyphs within the font. */ +/* */ +/* min_Bottom_Side_Bearing :: The minimum bottom side bearing of all */ +/* glyphs within the font. */ +/* */ +/* yMax_Extent :: The maximum vertical extent (i.e., the */ +/* `height' of a glyph's bounding box) for */ +/* all glyphs in the font. */ +/* */ +/* caret_Slope_Rise :: The rise coefficient of the cursor's */ +/* slope of the cursor (slope=rise/run). */ +/* */ +/* caret_Slope_Run :: The run coefficient of the cursor's */ +/* slope. */ +/* */ +/* Reserved :: 10 reserved bytes. */ +/* */ +/* metric_Data_Format :: Always 0. */ +/* */ +/* number_Of_HMetrics :: Number of VMetrics entries in the */ +/* `vmtx' table -- this value can be */ +/* smaller than the total number of glyphs */ +/* in the font. */ +/* */ +/* long_metrics :: A pointer into the `vmtx' table. */ +/* */ +/* short_metrics :: A pointer into the `vmtx' table. */ +/* */ +/* */ +/* IMPORTANT: The TT_HoriHeader and TT_VertHeader structures should */ +/* be identical except for the names of their fields which */ +/* are different. */ +/* */ +/* This ensures that a single function in the `ttload' */ +/* module is able to read both the horizontal and vertical */ +/* headers. */ +/* */ +typedef struct TT_VertHeader_ +{ + FT_Fixed Version; + FT_Short Ascender; + FT_Short Descender; + FT_Short Line_Gap; + + FT_UShort advance_Height_Max; /* advance height maximum */ + + FT_Short min_Top_Side_Bearing; /* minimum left-sb or top-sb */ + FT_Short min_Bottom_Side_Bearing; /* minimum right-sb or bottom-sb */ + FT_Short yMax_Extent; /* xmax or ymax extents */ + FT_Short caret_Slope_Rise; + FT_Short caret_Slope_Run; + FT_Short caret_Offset; + + FT_Short Reserved[4]; + + FT_Short metric_Data_Format; + FT_UShort number_Of_VMetrics; + + /* The following fields are not defined by the TrueType specification */ + /* but they're used to connect the metrics header to the relevant */ + /* `HMTX' or `VMTX' table. */ + + void* long_metrics; + void* short_metrics; + +} TT_VertHeader; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_OS2 */ +/* */ +/* */ +/* A structure used to model a TrueType OS/2 table. This is the long */ +/* table version. All fields comply to the TrueType specification. */ +/* */ +/* Note that we now support old Mac fonts which do not include an */ +/* OS/2 table. In this case, the `version' field is always set to */ +/* 0xFFFF. */ +/* */ +typedef struct TT_OS2_ +{ + FT_UShort version; /* 0x0001 - more or 0xFFFF */ + FT_Short xAvgCharWidth; + FT_UShort usWeightClass; + FT_UShort usWidthClass; + FT_Short fsType; + FT_Short ySubscriptXSize; + FT_Short ySubscriptYSize; + FT_Short ySubscriptXOffset; + FT_Short ySubscriptYOffset; + FT_Short ySuperscriptXSize; + FT_Short ySuperscriptYSize; + FT_Short ySuperscriptXOffset; + FT_Short ySuperscriptYOffset; + FT_Short yStrikeoutSize; + FT_Short yStrikeoutPosition; + FT_Short sFamilyClass; + + FT_Byte panose[10]; + + FT_ULong ulUnicodeRange1; /* Bits 0-31 */ + FT_ULong ulUnicodeRange2; /* Bits 32-63 */ + FT_ULong ulUnicodeRange3; /* Bits 64-95 */ + FT_ULong ulUnicodeRange4; /* Bits 96-127 */ + + FT_Char achVendID[4]; + + FT_UShort fsSelection; + FT_UShort usFirstCharIndex; + FT_UShort usLastCharIndex; + FT_Short sTypoAscender; + FT_Short sTypoDescender; + FT_Short sTypoLineGap; + FT_UShort usWinAscent; + FT_UShort usWinDescent; + + /* only version 1 tables: */ + + FT_ULong ulCodePageRange1; /* Bits 0-31 */ + FT_ULong ulCodePageRange2; /* Bits 32-63 */ + + /* only version 2 tables: */ + + FT_Short sxHeight; + FT_Short sCapHeight; + FT_UShort usDefaultChar; + FT_UShort usBreakChar; + FT_UShort usMaxContext; + +} TT_OS2; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Postscript */ +/* */ +/* */ +/* A structure used to model a TrueType Postscript table. All fields */ +/* comply to the TrueType table. This structure does not reference */ +/* the Postscript glyph names, which can be nevertheless accessed */ +/* with the `ttpost' module. */ +/* */ +typedef struct TT_Postscript_ +{ + FT_Fixed FormatType; + FT_Fixed italicAngle; + FT_Short underlinePosition; + FT_Short underlineThickness; + FT_ULong isFixedPitch; + FT_ULong minMemType42; + FT_ULong maxMemType42; + FT_ULong minMemType1; + FT_ULong maxMemType1; + + /* Glyph names follow in the file, but we don't */ + /* load them by default. See the ttpost.c file. */ + +} TT_Postscript; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_PCLT */ +/* */ +/* */ +/* A structure used to model a TrueType PCLT table. All fields */ +/* comply to the TrueType table. */ +/* */ +typedef struct TT_PCLT_ +{ + FT_Fixed Version; + FT_ULong FontNumber; + FT_UShort Pitch; + FT_UShort xHeight; + FT_UShort Style; + FT_UShort TypeFamily; + FT_UShort CapHeight; + FT_UShort SymbolSet; + FT_Char TypeFace[16]; + FT_Char CharacterComplement[8]; + FT_Char FileName[6]; + FT_Char StrokeWeight; + FT_Char WidthType; + FT_Byte SerifStyle; + FT_Byte Reserved; + +} TT_PCLT; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_MaxProfile */ +/* */ +/* */ +/* The maximum profile is a table containing many max values which */ +/* can be used to pre-allocate arrays. This ensures that no memory */ +/* allocation occurs during a glyph load. */ +/* */ +/* */ +/* version :: The version number. */ +/* */ +/* numGlyphs :: The number of glyphs in this TrueType */ +/* font. */ +/* */ +/* maxPoints :: The maximum number of points in a */ +/* non-composite TrueType glyph. See also */ +/* the structure element */ +/* `maxCompositePoints'. */ +/* */ +/* maxContours :: The maximum number of contours in a */ +/* non-composite TrueType glyph. See also */ +/* the structure element */ +/* `maxCompositeContours'. */ +/* */ +/* maxCompositePoints :: The maximum number of points in a */ +/* composite TrueType glyph. See also the */ +/* structure element `maxPoints'. */ +/* */ +/* maxCompositeContours :: The maximum number of contours in a */ +/* composite TrueType glyph. See also the */ +/* structure element `maxContours'. */ +/* */ +/* maxZones :: The maximum number of zones used for */ +/* glyph hinting. */ +/* */ +/* maxTwilightPoints :: The maximum number of points in the */ +/* twilight zone used for glyph hinting. */ +/* */ +/* maxStorage :: The maximum number of elements in the */ +/* storage area used for glyph hinting. */ +/* */ +/* maxFunctionDefs :: The maximum number of function */ +/* definitions in the TrueType bytecode for */ +/* this font. */ +/* */ +/* maxInstructionDefs :: The maximum number of instruction */ +/* definitions in the TrueType bytecode for */ +/* this font. */ +/* */ +/* maxStackElements :: The maximum number of stack elements used */ +/* during bytecode interpretation. */ +/* */ +/* maxSizeOfInstructions :: The maximum number of TrueType opcodes */ +/* used for glyph hinting. */ +/* */ +/* maxComponentElements :: An obscure value related to composite */ +/* glyphs definitions. */ +/* */ +/* maxComponentDepth :: An obscure value related to composite */ +/* glyphs definitions. Probably the maximum */ +/* number of simple glyphs in a composite. */ +/* */ +/* */ +/* This structure is only used during font loading. */ +/* */ +typedef struct TT_MaxProfile_ +{ + FT_Fixed version; + FT_UShort numGlyphs; + FT_UShort maxPoints; + FT_UShort maxContours; + FT_UShort maxCompositePoints; + FT_UShort maxCompositeContours; + FT_UShort maxZones; + FT_UShort maxTwilightPoints; + FT_UShort maxStorage; + FT_UShort maxFunctionDefs; + FT_UShort maxInstructionDefs; + FT_UShort maxStackElements; + FT_UShort maxSizeOfInstructions; + FT_UShort maxComponentElements; + FT_UShort maxComponentDepth; + +} TT_MaxProfile; + + +typedef enum +{ + ft_sfnt_head = 0, + ft_sfnt_maxp = 1, + ft_sfnt_os2 = 2, + ft_sfnt_hhea = 3, + ft_sfnt_vhea = 4, + ft_sfnt_post = 5, + ft_sfnt_pclt = 6, + + sfnt_max /* don't remove */ + +} FT_Sfnt_Tag; + + +/* internal use only */ +typedef void* ( *FT_Get_Sfnt_Table_Func )( FT_Face face, + FT_Sfnt_Tag tag ); + + +/*************************************************************************/ +/* */ +/* */ +/* FT_Get_Sfnt_Table */ +/* */ +/* */ +/* Returns a pointer to a given SFNT table within a face. */ +/* */ +/* */ +/* face :: A handle to the source. */ +/* */ +/* tag :: The index of the SFNT table. */ +/* */ +/* */ +/* A type-less pointer to the table. This will be 0 in case of */ +/* error, or if the corresponding table was not found *OR* loaded */ +/* from the file. */ +/* */ +/* */ +/* The table is owned by the face object and disappears with it. */ +/* */ +/* This function is only useful to access SFNT tables that are loaded */ +/* by the sfnt/truetype/opentype drivers. See FT_Sfnt_tag for a */ +/* list. */ +/* */ +/* You can load any table using the (internal) SFNT_Interface */ +/* structure -- this is available via FT_Get_Module_Interface(). */ +/* */ +FT_EXPORT_DEF( void* ) FT_Get_Sfnt_Table( FT_Face face, + FT_Sfnt_Tag tag ); + +#ifdef __cplusplus +} +#endif + + +#endif /* TTTABLES_H */ + + +/* END */ diff --git a/src/ft2/tttags.h b/src/ft2/tttags.h new file mode 100644 index 0000000..12e19fa --- /dev/null +++ b/src/ft2/tttags.h @@ -0,0 +1,66 @@ +/***************************************************************************/ +/* */ +/* tttags.h */ +/* */ +/* Tags for TrueType tables (specification only). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef TTAGS_H +#define TTAGS_H + + +#include "freetype.h" + + +#define TTAG_cmap FT_MAKE_TAG( 'c', 'm', 'a', 'p' ) +#define TTAG_cvt FT_MAKE_TAG( 'c', 'v', 't', ' ' ) +#define TTAG_CFF FT_MAKE_TAG( 'C', 'F', 'F', ' ' ) +#define TTAG_DSIG FT_MAKE_TAG( 'D', 'S', 'I', 'G' ) +#define TTAG_bdat FT_MAKE_TAG( 'b', 'd', 'a', 't' ) +#define TTAG_bloc FT_MAKE_TAG( 'b', 'l', 'o', 'c' ) +#define TTAG_EBDT FT_MAKE_TAG( 'E', 'B', 'D', 'T' ) +#define TTAG_EBLC FT_MAKE_TAG( 'E', 'B', 'L', 'C' ) +#define TTAG_EBSC FT_MAKE_TAG( 'E', 'B', 'S', 'C' ) +#define TTAG_fpgm FT_MAKE_TAG( 'f', 'p', 'g', 'm' ) +#define TTAG_fvar FT_MAKE_TAG( 'f', 'v', 'a', 'r' ) +#define TTAG_gasp FT_MAKE_TAG( 'g', 'a', 's', 'p' ) +#define TTAG_glyf FT_MAKE_TAG( 'g', 'l', 'y', 'f' ) +#define TTAG_GSUB FT_MAKE_TAG( 'G', 'S', 'U', 'B' ) +#define TTAG_hdmx FT_MAKE_TAG( 'h', 'd', 'm', 'x' ) +#define TTAG_head FT_MAKE_TAG( 'h', 'e', 'a', 'd' ) +#define TTAG_hhea FT_MAKE_TAG( 'h', 'h', 'e', 'a' ) +#define TTAG_hmtx FT_MAKE_TAG( 'h', 'm', 't', 'x' ) +#define TTAG_kern FT_MAKE_TAG( 'k', 'e', 'r', 'n' ) +#define TTAG_loca FT_MAKE_TAG( 'l', 'o', 'c', 'a' ) +#define TTAG_LTSH FT_MAKE_TAG( 'L', 'T', 'S', 'H' ) +#define TTAG_maxp FT_MAKE_TAG( 'm', 'a', 'x', 'p' ) +#define TTAG_MMSD FT_MAKE_TAG( 'M', 'M', 'S', 'D' ) +#define TTAG_MMFX FT_MAKE_TAG( 'M', 'M', 'F', 'X' ) +#define TTAG_name FT_MAKE_TAG( 'n', 'a', 'm', 'e' ) +#define TTAG_OS2 FT_MAKE_TAG( 'O', 'S', '/', '2' ) +#define TTAG_OTTO FT_MAKE_TAG( 'O', 'T', 'T', 'O' ) +#define TTAG_PCLT FT_MAKE_TAG( 'P', 'C', 'L', 'T' ) +#define TTAG_post FT_MAKE_TAG( 'p', 'o', 's', 't' ) +#define TTAG_prep FT_MAKE_TAG( 'p', 'r', 'e', 'p' ) +#define TTAG_true FT_MAKE_TAG( 't', 'r', 'u', 'e' ) +#define TTAG_ttc FT_MAKE_TAG( 't', 't', 'c', ' ' ) +#define TTAG_ttcf FT_MAKE_TAG( 't', 't', 'c', 'f' ) +#define TTAG_VDMX FT_MAKE_TAG( 'V', 'D', 'M', 'X' ) +#define TTAG_vhea FT_MAKE_TAG( 'v', 'h', 'e', 'a' ) +#define TTAG_vmtx FT_MAKE_TAG( 'v', 'm', 't', 'x' ) + +#endif /* TTAGS_H */ + + +/* END */ diff --git a/src/ft2/tttypes.h b/src/ft2/tttypes.h new file mode 100644 index 0000000..b22b381 --- /dev/null +++ b/src/ft2/tttypes.h @@ -0,0 +1,1582 @@ +/***************************************************************************/ +/* */ +/* tttypes.h */ +/* */ +/* Basic SFNT/TrueType type definitions and interface (specification */ +/* only). */ +/* */ +/* Copyright 1996-2000 by */ +/* David Turner, Robert Wilhelm, and Werner Lemberg. */ +/* */ +/* This file is part of the FreeType project, and may only be used, */ +/* modified, and distributed under the terms of the FreeType project */ +/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ +/* this file you indicate that you have read the license and */ +/* understand and accept it fully. */ +/* */ +/***************************************************************************/ + + +#ifndef TTTYPES_H +#define TTTYPES_H + + +#include "tttables.h" + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/*** ***/ +/*** ***/ +/*** REQUIRED TRUETYPE/OPENTYPE TABLES DEFINITIONS ***/ +/*** ***/ +/*** ***/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* TTC_Header */ +/* */ +/* */ +/* TrueType collection header. This table contains the offsets of */ +/* the font headers of each distinct TrueType face in the file. */ +/* */ +/* */ +/* tag :: Must be `ttc ' to indicate a TrueType collection. */ +/* */ +/* version :: The version number. */ +/* */ +/* count :: The number of faces in the collection. The */ +/* specification says this should be an unsigned long, but */ +/* we use a signed long since we need the value -1 for */ +/* specific purposes. */ +/* */ +/* offsets :: The offsets of the font headers, one per face. */ +/* */ +typedef struct TTC_Header_ +{ + FT_ULong tag; + FT_Fixed version; + FT_Long count; + FT_ULong* offsets; + +} TTC_Header; + + +/*************************************************************************/ +/* */ +/* */ +/* SFNT_Header */ +/* */ +/* */ +/* SFNT file format header. */ +/* */ +/* */ +/* format_tag :: The font format tag. */ +/* */ +/* num_tables :: The number of tables in file. */ +/* */ +/* search_range :: Must be 16*(max power of 2 <= num_tables). */ +/* */ +/* entry_selector :: Must be log2 of search_range/16. */ +/* */ +/* range_shift :: Must be num_tables*16 - search_range. */ +/* */ +typedef struct SFNT_Header_ +{ + FT_ULong format_tag; + FT_UShort num_tables; + FT_UShort search_range; + FT_UShort entry_selector; + FT_UShort range_shift; + +} SFNT_Header; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_TableDir */ +/* */ +/* */ +/* This structure models a TrueType table directory. It is used to */ +/* access the various tables of the font face. */ +/* */ +/* */ +/* version :: The version number; starts with 0x00010000. */ +/* */ +/* numTables :: The number of tables. */ +/* */ +/* searchRange :: Unused. */ +/* */ +/* entrySelector :: Unused. */ +/* */ +/* rangeShift :: Unused. */ +/* */ +/* */ +/* This structure is only used during font opening. */ +/* */ +typedef struct TT_TableDir_ +{ + FT_Fixed version; /* should be 0x10000 */ + FT_UShort numTables; /* number of tables */ + + FT_UShort searchRange; /* These parameters are only used */ + FT_UShort entrySelector; /* for a dichotomy search in the */ + FT_UShort rangeShift; /* directory. We ignore them. */ + +} TT_TableDir; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Table */ +/* */ +/* */ +/* This structure describes a given table of a TrueType font. */ +/* */ +/* */ +/* Tag :: A four-bytes tag describing the table. */ +/* */ +/* CheckSum :: The table checksum. This value can be ignored. */ +/* */ +/* Offset :: The offset of the table from the start of the TrueType */ +/* font in its resource. */ +/* */ +/* Length :: The table length (in bytes). */ +/* */ +typedef struct TT_Table_ +{ + FT_ULong Tag; /* table type */ + FT_ULong CheckSum; /* table checksum */ + FT_ULong Offset; /* table file offset */ + FT_ULong Length; /* table length */ + +} TT_Table; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_CMapDir */ +/* */ +/* */ +/* This structure describes the directory of the `cmap' table, */ +/* containing the font's character mappings table. */ +/* */ +/* */ +/* tableVersionNumber :: The version number. */ +/* */ +/* numCMaps :: The number of charmaps in the font. */ +/* */ +/* */ +/* This structure is only used during font loading. */ +/* */ +typedef struct TT_CMapDir_ +{ + FT_UShort tableVersionNumber; + FT_UShort numCMaps; + +} TT_CMapDir; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_CMapDirEntry */ +/* */ +/* */ +/* This structure describes a charmap in a TrueType font. */ +/* */ +/* */ +/* platformID :: An ID used to specify for which platform this */ +/* charmap is defined (FreeType manages all platforms). */ +/* */ +/* encodingID :: A platform-specific ID used to indicate which source */ +/* encoding is used in this charmap. */ +/* */ +/* offset :: The offset of the charmap relative to the start of */ +/* the `cmap' table. */ +/* */ +/* */ +/* This structure is only used during font loading. */ +/* */ +typedef struct TT_CMapDirEntry_ +{ + FT_UShort platformID; + FT_UShort platformEncodingID; + FT_Long offset; + +} TT_CMapDirEntry; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_LongMetrics */ +/* */ +/* */ +/* A structure modeling the long metrics of the `hmtx' and `vmtx' */ +/* TrueType tables. The values are expressed in font units. */ +/* */ +/* */ +/* advance :: The advance width or height for the glyph. */ +/* */ +/* bearing :: The left-side or top-side bearing for the glyph. */ +/* */ +typedef struct TT_LongMetrics_ +{ + FT_UShort advance; + FT_Short bearing; + +} TT_LongMetrics; + + +/*************************************************************************/ +/* */ +/* TT_ShortMetrics */ +/* */ +/* */ +/* A simple type to model the short metrics of the `hmtx' and `vmtx' */ +/* tables. */ +/* */ +typedef FT_Short TT_ShortMetrics; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_NameRec */ +/* */ +/* */ +/* A structure modeling TrueType name records. Name records are used */ +/* to store important strings like family name, style name, */ +/* copyright, etc. in _localized_ versions (i.e., language, encoding, */ +/* etc). */ +/* */ +/* */ +/* platformID :: The ID of the name's encoding platform. */ +/* */ +/* encodingID :: The platform-specific ID for the name's encoding. */ +/* */ +/* languageID :: The platform-specific ID for the name's language. */ +/* */ +/* nameID :: The ID specifying what kind of name this is. */ +/* */ +/* stringLength :: The length of the string in bytes. */ +/* */ +/* stringOffset :: The offset to the string in the `name' table. */ +/* */ +/* string :: A pointer to the string's bytes. Note that these */ +/* are usually UTF-16 encoded characters. */ +/* */ +typedef struct TT_NameRec_ +{ + FT_UShort platformID; + FT_UShort encodingID; + FT_UShort languageID; + FT_UShort nameID; + FT_UShort stringLength; + FT_UShort stringOffset; + + /* this last field is not defined in the spec */ + /* but used by the FreeType engine */ + + FT_Byte* string; + +} TT_NameRec; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_NameTable */ +/* */ +/* */ +/* A structure modeling the TrueType name table. */ +/* */ +/* */ +/* format :: The format of the name table. */ +/* */ +/* numNameRecords :: The number of names in table. */ +/* */ +/* storageOffset :: The offset of the name table in the `name' */ +/* TrueType table. */ +/* */ +/* names :: An array of name records. */ +/* */ +/* storage :: The names storage area. */ +/* */ +typedef struct TT_NameTable_ +{ + FT_UShort format; + FT_UShort numNameRecords; + FT_UShort storageOffset; + TT_NameRec* names; + FT_Byte* storage; + +} TT_NameTable; + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/*** ***/ +/*** ***/ +/*** OPTIONAL TRUETYPE/OPENTYPE TABLES DEFINITIONS ***/ +/*** ***/ +/*** ***/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* TT_GaspRange */ +/* */ +/* */ +/* A tiny structure used to model a gasp range according to the */ +/* TrueType specification. */ +/* */ +/* */ +/* maxPPEM :: The maximum ppem value to which `gaspFlag' applies. */ +/* */ +/* gaspFlag :: A flag describing the grid-fitting and anti-aliasing */ +/* modes to be used. */ +/* */ +typedef struct TT_GaspRange_ +{ + FT_UShort maxPPEM; + FT_UShort gaspFlag; + +} TT_GaspRange; + + +#define TT_GASP_GRIDFIT 0x01 +#define TT_GASP_DOGRAY 0x02 + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Gasp */ +/* */ +/* */ +/* A structure modeling the TrueType `gasp' table used to specify */ +/* grid-fitting and anti-aliasing behaviour. */ +/* */ +/* */ +/* version :: The version number. */ +/* */ +/* numRanges :: The number of gasp ranges in table. */ +/* */ +/* gaspRanges :: An array of gasp ranges. */ +/* */ +typedef struct TT_Gasp_ +{ + FT_UShort version; + FT_UShort numRanges; + TT_GaspRange* gaspRanges; + +} TT_Gasp; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_HdmxRec */ +/* */ +/* */ +/* A small structure used to model the pre-computed widths of a given */ +/* size. They are found in the `hdmx' table. */ +/* */ +/* */ +/* ppem :: The pixels per EM value at which these metrics apply. */ +/* */ +/* max_width :: The maximum advance width for this metric. */ +/* */ +/* widths :: An array of widths. Note: These are 8-bit bytes. */ +/* */ +typedef struct TT_HdmxRec_ +{ + FT_Byte ppem; + FT_Byte max_width; + FT_Byte* widths; + +} TT_HdmxRec; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Hdmx */ +/* */ +/* */ +/* A structure used to model the `hdmx' table, which contains */ +/* pre-computed widths for a set of given sizes/dimensions. */ +/* */ +/* */ +/* version :: The version number. */ +/* */ +/* num_records :: The number of hdmx records. */ +/* */ +/* records :: An array of hdmx records. */ +/* */ +typedef struct TT_Hdmx_ +{ + FT_UShort version; + FT_Short num_records; + TT_HdmxRec* records; + +} TT_Hdmx; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Kern_0_Pair */ +/* */ +/* */ +/* A structure used to model a kerning pair for the kerning table */ +/* format 0. The engine now loads this table if it finds one in the */ +/* font file. */ +/* */ +/* */ +/* left :: The index of the left glyph in pair. */ +/* */ +/* right :: The index of the right glyph in pair. */ +/* */ +/* value :: The kerning distance. A positive value spaces the */ +/* glyphs, a negative one makes them closer. */ +/* */ +typedef struct TT_Kern_0_Pair_ +{ + FT_UShort left; /* index of left glyph in pair */ + FT_UShort right; /* index of right glyph in pair */ + FT_FWord value; /* kerning value */ + +} TT_Kern_0_Pair; + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/*** ***/ +/*** ***/ +/*** EMBEDDED BITMAPS SUPPORT ***/ +/*** ***/ +/*** ***/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* TT_SBit_Metrics */ +/* */ +/* */ +/* A structure used to hold the big metrics of a given glyph bitmap */ +/* in a TrueType or OpenType font. These are usually found in the */ +/* `EBDT' (Microsoft) or `bdat' (Apple) table. */ +/* */ +/* */ +/* height :: The glyph height in pixels. */ +/* */ +/* width :: The glyph width in pixels. */ +/* */ +/* horiBearingX :: The horizontal left bearing. */ +/* */ +/* horiBearingY :: The horizontal top bearing. */ +/* */ +/* horiAdvance :: The horizontal advance. */ +/* */ +/* vertBearingX :: The vertical left bearing. */ +/* */ +/* vertBearingY :: The vertical top bearing. */ +/* */ +/* vertAdvance :: The vertical advance. */ +/* */ +typedef struct TT_SBit_Metrics_ +{ + FT_Byte height; + FT_Byte width; + + FT_Char horiBearingX; + FT_Char horiBearingY; + FT_Byte horiAdvance; + + FT_Char vertBearingX; + FT_Char vertBearingY; + FT_Byte vertAdvance; + +} TT_SBit_Metrics; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_SBit_Small_Metrics */ +/* */ +/* */ +/* A structure used to hold the small metrics of a given glyph bitmap */ +/* in a TrueType or OpenType font. These are usually found in the */ +/* `EBDT' (Microsoft) or the `bdat' (Apple) table. */ +/* */ +/* */ +/* height :: The glyph height in pixels. */ +/* */ +/* width :: The glyph width in pixels. */ +/* */ +/* bearingX :: The left-side bearing. */ +/* */ +/* bearingY :: The top-side bearing. */ +/* */ +/* advance :: The advance width or height. */ +/* */ +typedef struct TT_SBit_Small_Metrics_ +{ + FT_Byte height; + FT_Byte width; + + FT_Char bearingX; + FT_Char bearingY; + FT_Byte advance; + +} TT_SBit_Small_Metrics; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_SBit_Line_Metrics */ +/* */ +/* */ +/* A structure used to describe the text line metrics of a given */ +/* bitmap strike, for either a horizontal or vertical layout. */ +/* */ +/* */ +/* ascender :: The ascender in pixels. */ +/* */ +/* descender :: The descender in pixels. */ +/* */ +/* max_width :: The maximum glyph width in pixels. */ +/* */ +/* caret_slope_enumerator :: Rise of the caret slope, typically set */ +/* to 1 for non-italic fonts. */ +/* */ +/* caret_slope_denominator :: Rise of the caret slope, typically set */ +/* to 0 for non-italic fonts. */ +/* */ +/* caret_offset :: Offset in pixels to move the caret for */ +/* proper positioning. */ +/* */ +/* min_origin_SB :: Minimum of horiBearingX (resp. */ +/* vertBearingY). */ +/* min_advance_SB :: Minimum of */ +/* */ +/* horizontal advance - */ +/* ( horiBearingX + width ) */ +/* */ +/* resp. */ +/* */ +/* vertical advance - */ +/* ( vertBearingY + height ) */ +/* */ +/* max_before_BL :: Maximum of horiBearingY (resp. */ +/* vertBearingY). */ +/* */ +/* min_after_BL :: Minimum of */ +/* */ +/* horiBearingY - height */ +/* */ +/* resp. */ +/* */ +/* vertBearingX - width */ +/* */ +/* pads :: Unused (to make the size of the record */ +/* a multiple of 32 bits. */ +/* */ +typedef struct TT_SBit_Line_Metrics_ +{ + FT_Char ascender; + FT_Char descender; + FT_Byte max_width; + FT_Char caret_slope_numerator; + FT_Char caret_slope_denominator; + FT_Char caret_offset; + FT_Char min_origin_SB; + FT_Char min_advance_SB; + FT_Char max_before_BL; + FT_Char min_after_BL; + FT_Char pads[2]; + +} TT_SBit_Line_Metrics; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_SBit_Range */ +/* */ +/* */ +/* A TrueType/OpenType subIndexTable as defined in the `EBLC' */ +/* (Microsoft) or `bloc' (Apple) tables. */ +/* */ +/* */ +/* first_glyph :: The first glyph index in the range. */ +/* */ +/* last_glyph :: The last glyph index in the range. */ +/* */ +/* index_format :: The format of index table. Valid values are 1 */ +/* to 5. */ +/* */ +/* image_format :: The format of `EBDT' image data. */ +/* */ +/* image_offset :: The offset to image data in `EBDT'. */ +/* */ +/* image_size :: For index formats 2 and 5. This is the size in */ +/* bytes of each glyph bitmap. */ +/* */ +/* big_metrics :: For index formats 2 and 5. This is the big */ +/* metrics for each glyph bitmap. */ +/* */ +/* num_glyphs :: For index formats 4 and 5. This is the number of */ +/* glyphs in the code array. */ +/* */ +/* glyph_offsets :: For index formats 1 and 3. */ +/* */ +/* glyph_codes :: For index formats 4 and 5. */ +/* */ +/* table_offset :: The offset of the index table in the `EBLC' */ +/* table. Only used during strike loading. */ +/* */ +typedef struct TT_SBit_Range +{ + FT_UShort first_glyph; + FT_UShort last_glyph; + + FT_UShort index_format; + FT_UShort image_format; + FT_ULong image_offset; + + FT_ULong image_size; + TT_SBit_Metrics metrics; + FT_ULong num_glyphs; + + FT_ULong* glyph_offsets; + FT_UShort* glyph_codes; + + FT_ULong table_offset; + +} TT_SBit_Range; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_SBit_Strike */ +/* */ +/* */ +/* A structure used describe a given bitmap strike in the `EBLC' */ +/* (Microsoft) or `bloc' (Apple) tables. */ +/* */ +/* */ +/* num_index_ranges :: The number of index ranges. */ +/* */ +/* index_ranges :: An array of glyph index ranges. */ +/* */ +/* color_ref :: Unused. A color reference? */ +/* */ +/* hori :: The line metrics for horizontal layouts. */ +/* */ +/* vert :: The line metrics for vertical layouts. */ +/* */ +/* start_glyph :: The lowest glyph index for this strike. */ +/* */ +/* end_glyph :: The highest glyph index for this strike. */ +/* */ +/* x_ppem :: The number of horizontal pixels per EM. */ +/* */ +/* y_ppem :: The number of vertical pixels per EM. */ +/* */ +/* bit_depth :: The bit depth. Valid values are 1, 2, 4, */ +/* and 8. */ +/* */ +/* flags :: Is this a vertical or horizontal strike? */ +/* */ +typedef struct TT_SBit_Strike_ +{ + FT_Int num_ranges; + TT_SBit_Range* sbit_ranges; + FT_ULong ranges_offset; + + FT_ULong color_ref; + + TT_SBit_Line_Metrics hori; + TT_SBit_Line_Metrics vert; + + FT_UShort start_glyph; + FT_UShort end_glyph; + + FT_Byte x_ppem; + FT_Byte y_ppem; + + FT_Byte bit_depth; + FT_Char flags; + +} TT_SBit_Strike; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_SBit_Component */ +/* */ +/* */ +/* A simple structure to describe a compound sbit element. */ +/* */ +/* */ +/* glyph_code :: The element's glyph index. */ +/* */ +/* x_offset :: The element's left bearing. */ +/* */ +/* y_offset :: The element's top bearing. */ +/* */ +typedef struct TT_SBit_Component_ +{ + FT_UShort glyph_code; + + FT_Char x_offset; + FT_Char y_offset; + +} TT_SBit_Component; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_SBit_Scale */ +/* */ +/* */ +/* A structure used describe a given bitmap scaling table, as defined */ +/* in the `EBSC' table. */ +/* */ +/* */ +/* hori :: The horizontal line metrics. */ +/* */ +/* vert :: The vertical line metrics. */ +/* */ +/* x_ppem :: The number of horizontal pixels per EM. */ +/* */ +/* y_ppem :: The number of vertical pixels per EM. */ +/* */ +/* x_ppem_substitute :: Substitution x_ppem value. */ +/* */ +/* y_ppem_substitute :: Substitution y_ppem value. */ +/* */ +typedef struct TT_SBit_Scale_ +{ + TT_SBit_Line_Metrics hori; + TT_SBit_Line_Metrics vert; + + FT_Byte x_ppem; + FT_Byte y_ppem; + + FT_Byte x_ppem_substitute; + FT_Byte y_ppem_substitute; + +} TT_SBit_Scale; + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/*** ***/ +/*** ***/ +/*** POSTSCRIPT GLYPH NAMES SUPPORT ***/ +/*** ***/ +/*** ***/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Post_20 */ +/* */ +/* */ +/* Postscript names sub-table, format 2.0. Stores the PS name of */ +/* each glyph in the font face. */ +/* */ +/* */ +/* num_glyphs :: The number of named glyphs in the table. */ +/* */ +/* num_names :: The number of PS names stored in the table. */ +/* */ +/* glyph_indices :: The indices of the glyphs in the names arrays. */ +/* */ +/* glyph_names :: The PS names not in Mac Encoding. */ +/* */ +typedef struct TT_Post_20_ +{ + FT_UShort num_glyphs; + FT_UShort num_names; + FT_UShort* glyph_indices; + FT_Char** glyph_names; + +} TT_Post_20; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Post_25 */ +/* */ +/* */ +/* Postscript names sub-table, format 2.5. Stores the PS name of */ +/* each glyph in the font face. */ +/* */ +/* */ +/* num_glyphs :: The number of glyphs in the table. */ +/* */ +/* offsets :: An array of signed offsets in a normal Mac */ +/* Postscript name encoding. */ +/* */ +typedef struct TT_Post_25_ +{ + FT_UShort num_glyphs; + FT_Char* offsets; + +} TT_Post_25; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Post_Names */ +/* */ +/* */ +/* Postscript names table, either format 2.0 or 2.5. */ +/* */ +/* */ +/* loaded :: A flag to indicate whether the PS names are loaded. */ +/* */ +/* format_20 :: The sub-table used for format 2.0. */ +/* */ +/* format_25 :: The sub-table used for format 2.5. */ +/* */ +typedef struct TT_Post_Names_ +{ + FT_Bool loaded; + + union + { + TT_Post_20 format_20; + TT_Post_25 format_25; + + } names; + +} TT_Post_Names; + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/*** ***/ +/*** ***/ +/*** TRUETYPE CHARMAPS SUPPORT ***/ +/*** ***/ +/*** ***/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +/* format 0 */ + +typedef struct TT_CMap0_ +{ + FT_Byte* glyphIdArray; + +} TT_CMap0; + + +/* format 2 */ + +typedef struct TT_CMap2SubHeader_ +{ + FT_UShort firstCode; /* first valid low byte */ + FT_UShort entryCount; /* number of valid low bytes */ + FT_Short idDelta; /* delta value to glyphIndex */ + FT_UShort idRangeOffset; /* offset from here to 1st code */ + +} TT_CMap2SubHeader; + + +typedef struct TT_CMap2_ +{ + FT_UShort* subHeaderKeys; + /* high byte mapping table */ + /* value = subHeader index * 8 */ + + TT_CMap2SubHeader* subHeaders; + FT_UShort* glyphIdArray; + FT_UShort numGlyphId; /* control value */ + +} TT_CMap2; + + +/* format 4 */ + +typedef struct TT_CMap4Segment_ +{ + FT_UShort endCount; + FT_UShort startCount; + FT_Short idDelta; + FT_UShort idRangeOffset; + +} TT_CMap4Segment; + + +typedef struct TT_CMap4_ +{ + FT_UShort segCountX2; /* number of segments * 2 */ + FT_UShort searchRange; /* these parameters can be used */ + FT_UShort entrySelector; /* for a binary search */ + FT_UShort rangeShift; + + TT_CMap4Segment* segments; + FT_UShort* glyphIdArray; + FT_UShort numGlyphId; /* control value */ + + TT_CMap4Segment* last_segment; /* last used segment; this is a small */ + /* cache to potentially increase speed */ +} TT_CMap4; + + +/* format 6 */ + +typedef struct TT_CMap6_ +{ + FT_UShort firstCode; /* first character code of subrange */ + FT_UShort entryCount; /* number of character codes in subrange */ + + FT_UShort* glyphIdArray; + +} TT_CMap6; + + +typedef struct TT_CMapTable_ TT_CMapTable; + + +typedef +FT_UInt ( *TT_CharMap_Func )( TT_CMapTable* charmap, + FT_ULong char_code ); + + +/* charmap table */ +struct TT_CMapTable_ +{ + FT_UShort platformID; + FT_UShort platformEncodingID; + FT_UShort format; + FT_UShort length; + FT_UShort version; + + FT_Bool loaded; + FT_ULong offset; + + union + { + TT_CMap0 cmap0; + TT_CMap2 cmap2; + TT_CMap4 cmap4; + TT_CMap6 cmap6; + } c; + + TT_CharMap_Func get_index; +}; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_CharMapRec */ +/* */ +/* */ +/* The TrueType character map object type. */ +/* */ +/* */ +/* root :: The parent character map structure. */ +/* */ +/* cmap :: The used character map. */ +/* */ +typedef struct TT_CharMapRec_ +{ + FT_CharMapRec root; + TT_CMapTable cmap; + +} TT_CharMapRec; + + +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ +/*** ***/ +/*** ***/ +/*** ORIGINAL TT_FACE CLASS DEFINITION ***/ +/*** ***/ +/*** ***/ +/*************************************************************************/ +/*************************************************************************/ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* This structure/class is defined here because it is common to the */ +/* following formats: TTF, OpenType-TT, and OpenType-CFF. */ +/* */ +/* Note, however, that the classes TT_Size, TT_GlyphSlot, and TT_CharMap */ +/* are not shared between font drivers, and are thus defined normally in */ +/* `ttobjs.h'. */ +/* */ +/*************************************************************************/ + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Face */ +/* */ +/* */ +/* A handle to a TrueType face/font object. A TT_Face encapsulates */ +/* the resolution and scaling independent parts of a TrueType font */ +/* resource. */ +/* */ +/* */ +/* The TT_Face structure is also used as a `parent class' for the */ +/* OpenType-CFF class (T2_Face). */ +/* */ +typedef struct TT_FaceRec_* TT_Face; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_CharMap */ +/* */ +/* */ +/* A handle to a TrueType character mapping object. */ +/* */ +typedef struct TT_CharMapRec_* TT_CharMap; + + +/* a function type used for the truetype bytecode interpreter hooks */ +typedef FT_Error ( *TT_Interpreter )( void* exec_context ); + +/* forward declaration */ +typedef struct TT_Loader_ TT_Loader; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Goto_Table_Func */ +/* */ +/* */ +/* Seeks a stream to the start of a given TrueType table. */ +/* */ +/* */ +/* face :: A handle to the target face object. */ +/* */ +/* tag :: A 4-byte tag used to name the table. */ +/* */ +/* stream :: The input stream. */ +/* */ +/* */ +/* length :: The length of the table in bytes. Set to 0 if not */ +/* needed. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* The stream cursor must be at the font file's origin. */ +/* */ +typedef +FT_Error ( *TT_Goto_Table_Func )( TT_Face face, + FT_ULong tag, + FT_Stream stream, + FT_ULong* length ); + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Access_Glyph_Frame_Func */ +/* */ +/* */ +/* Seeks a stream to the start of a given glyph element, and opens a */ +/* frame for it. */ +/* */ +/* */ +/* loader :: The current TrueType glyph loader object. */ +/* */ +/* glyph index :: The index of the glyph to access. */ +/* */ +/* offset :: The offset of the glyph according to the */ +/* `locations' table. */ +/* */ +/* byte_count :: The size of the frame in bytes. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +/* */ +/* This function is normally equivalent to FILE_Seek(offset) */ +/* followed by ACCESS_Frame(byte_count) with the loader's stream, but */ +/* alternative formats (e.g. compressed ones) might use something */ +/* different. */ +/* */ +typedef +FT_Error ( *TT_Access_Glyph_Frame_Func )( TT_Loader* loader, + FT_UInt glyph_index, + FT_ULong offset, + FT_UInt byte_count ); + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Load_Glyph_Element_Func */ +/* */ +/* */ +/* Reads one glyph element (its header, a simple glyph, or a */ +/* composite) from the loader's current stream frame. */ +/* */ +/* */ +/* loader :: The current TrueType glyph loader object. */ +/* */ +/* */ +/* FreeType error code. 0 means success. */ +/* */ +typedef +FT_Error ( *TT_Load_Glyph_Element_Func )( TT_Loader* loader ); + + +/*************************************************************************/ +/* */ +/* */ +/* TT_Forget_Glyph_Frame_Func */ +/* */ +/* */ +/* Closes the current loader stream frame for the glyph. */ +/* */ +/* */ +/* loader :: The current TrueType glyph loader object. */ +/* */ +typedef +void ( *TT_Forget_Glyph_Frame_Func )( TT_Loader* loader ); + + + +/*************************************************************************/ +/* */ +/* TrueType Face Type */ +/* */ +/* */ +/* TT_Face */ +/* */ +/* */ +/* The TrueType face class. These objects model the resolution and */ +/* point-size independent data found in a TrueType font file. */ +/* */ +/* */ +/* root :: The base FT_Face structure, managed by the */ +/* base layer. */ +/* */ +/* ttc_header :: The TrueType collection header, used when */ +/* the file is a `ttc' rather than a `ttf'. */ +/* For ordinary font files, the field */ +/* `ttc_header.count' is set to 0. */ +/* */ +/* format_tag :: The font format tag. */ +/* */ +/* num_tables :: The number of TrueType tables in this font */ +/* file. */ +/* */ +/* dir_tables :: The directory of TrueType tables for this */ +/* font file. */ +/* */ +/* header :: The font's font header (`head' table). */ +/* Read on font opening. */ +/* */ +/* horizontal :: The font's horizontal header (`hhea' */ +/* table). This field also contains the */ +/* associated horizontal metrics table */ +/* (`hmtx'). */ +/* */ +/* max_profile :: The font's maximum profile table. Read on */ +/* font opening. Note that some maximum */ +/* values cannot be taken directly from this */ +/* table. We thus define additional fields */ +/* below to hold the computed maxima. */ +/* */ +/* max_components :: The maximum number of glyph components */ +/* required to load any composite glyph from */ +/* this font. Used to size the load stack. */ +/* */ +/* vertical_info :: A boolean which is set when the font file */ +/* contains vertical metrics. If not, the */ +/* value of the `vertical' field is */ +/* undefined. */ +/* */ +/* vertical :: The font's vertical header (`vhea' table). */ +/* This field also contains the associated */ +/* vertical metrics table (`vmtx'), if found. */ +/* IMPORTANT: The contents of this field is */ +/* undefined if the `verticalInfo' field is */ +/* unset. */ +/* */ +/* num_names :: The number of name records within this */ +/* TrueType font. */ +/* */ +/* name_table :: The table of name records (`name'). */ +/* */ +/* os2 :: The font's OS/2 table (`OS/2'). */ +/* */ +/* postscript :: The font's PostScript table (`post' */ +/* table). The PostScript glyph names are */ +/* not loaded by the driver on face opening. */ +/* See the `ttpost' module for more details. */ +/* */ +/* num_charmaps :: The number of character mappings in the */ +/* font. */ +/* */ +/* charmaps :: The array of charmap objects for this font */ +/* file. Note that this field is a typeless */ +/* pointer. The Reason is that the format of */ +/* charmaps varies with the underlying font */ +/* format and cannot be determined here. */ +/* */ +/* goto_table :: A function called by each TrueType table */ +/* loader to position a stream's cursor to */ +/* the start of a given table according to */ +/* its tag. It defaults to TT_Goto_Face but */ +/* can be different for strange formats (e.g. */ +/* Type 42). */ +/* */ +/* access_glyph_frame :: XXX */ +/* */ +/* read_glyph_header :: XXX */ +/* */ +/* read_simple_glyph :: XXX */ +/* */ +/* read_composite_glyph :: XXX */ +/* */ +/* forget_glyph_frame :: XXX */ +/* */ +/* sfnt :: A pointer to the SFNT `driver' interface. */ +/* */ +/* psnames :: A pointer to the `PSNames' module */ +/* interface. */ +/* */ +/* hdmx :: The face's horizontal device metrics */ +/* (`hdmx' table). This table is optional in */ +/* TrueType/OpenType fonts. */ +/* */ +/* gasp :: The grid-fitting and scaling properties */ +/* table (`gasp'). This table is optional in */ +/* TrueType/OpenType fonts. */ +/* */ +/* pclt :: XXX */ +/* */ +/* num_sbit_strikes :: The number of sbit strikes, i.e., bitmap */ +/* sizes, embedded in this font. */ +/* */ +/* sbit_strikes :: An array of sbit strikes embedded in this */ +/* font. This table is optional in a */ +/* TrueType/OpenType font. */ +/* */ +/* num_sbit_scales :: The number of sbit scales for this font. */ +/* */ +/* sbit_scales :: Array of sbit scales embedded in this */ +/* font. This table is optional in a */ +/* TrueType/OpenType font. */ +/* */ +/* postscript_names :: A table used to store the Postscript names */ +/* of the glyphs for this font. See the */ +/* file `ttconfig.h' for comments on the */ +/* TT_CONFIG_OPTION_POSTSCRIPT_NAMES option. */ +/* */ +/* num_locations :: The number of glyph locations in this */ +/* TrueType file. This should be */ +/* identical to the number of glyphs. */ +/* Ignored for Type 2 fonts. */ +/* */ +/* glyph_locations :: An array of longs. These are offsets to */ +/* glyph data within the `glyf' table. */ +/* Ignored for Type 2 font faces. */ +/* */ +/* font_program_size :: Size in bytecodes of the face's font */ +/* program. 0 if none defined. Ignored for */ +/* Type 2 fonts. */ +/* */ +/* font_program :: The face's font program (bytecode stream) */ +/* executed at load time, also used during */ +/* glyph rendering. Comes from the `fpgm' */ +/* table. Ignored for Type 2 font fonts. */ +/* */ +/* cvt_program_size :: The size in bytecodes of the face's cvt */ +/* program. Ignored for Type 2 fonts. */ +/* */ +/* cvt_program :: The face's cvt program (bytecode stream) */ +/* executed each time an instance/size is */ +/* changed/reset. Comes from the `prep' */ +/* table. Ignored for Type 2 fonts. */ +/* */ +/* cvt_size :: Size of the control value table (in */ +/* entries). Ignored for Type 2 fonts. */ +/* */ +/* cvt :: The face's original control value table. */ +/* Coordinates are expressed in unscaled font */ +/* units. Comes from the `cvt ' table. */ +/* Ignored for Type 2 fonts. */ +/* */ +/* num_kern_pairs :: The number of kerning pairs present in the */ +/* font file. The engine only loads the */ +/* first horizontal format 0 kern table it */ +/* finds in the font file. You should use */ +/* the `ttxkern' structures if you want to */ +/* access other kerning tables. Ignored */ +/* for Type 2 fonts. */ +/* */ +/* kern_table_index :: The index of the kerning table in the font */ +/* kerning directory. Only used by the */ +/* ttxkern extension to avoid data */ +/* duplication. Ignored for Type 2 fonts. */ +/* */ +/* interpreter :: A pointer to the TrueType bytecode */ +/* interpreters field is also used to hook */ +/* the debugger in `ttdebug'. */ +/* */ +/* extra :: XXX */ +/* */ +typedef struct TT_FaceRec_ +{ + FT_FaceRec root; + + TTC_Header ttc_header; + + FT_ULong format_tag; + FT_UShort num_tables; + TT_Table* dir_tables; + + TT_Header header; /* TrueType header table */ + TT_HoriHeader horizontal; /* TrueType horizontal header */ + + TT_MaxProfile max_profile; + FT_ULong max_components; + + FT_Bool vertical_info; + TT_VertHeader vertical; /* TT Vertical header, if present */ + + FT_Int num_names; /* number of name records */ + TT_NameTable name_table; /* name table */ + + TT_OS2 os2; /* TrueType OS/2 table */ + TT_Postscript postscript; /* TrueType Postscript table */ + + FT_Int num_charmaps; + TT_CharMap charmaps; /* array of TT_CharMapRec */ + + TT_Goto_Table_Func goto_table; + + TT_Access_Glyph_Frame_Func access_glyph_frame; + TT_Load_Glyph_Element_Func read_glyph_header; + TT_Load_Glyph_Element_Func read_simple_glyph; + TT_Load_Glyph_Element_Func read_composite_glyph; + TT_Forget_Glyph_Frame_Func forget_glyph_frame; + + /* a typeless pointer to the SFNT_Interface table used to load */ + /* the basic TrueType tables in the face object */ + void* sfnt; + + /* a typeless pointer to the PSNames_Interface table used to */ + /* handle glyph names <-> unicode & Mac values */ + void* psnames; + + /***********************************************************************/ + /* */ + /* Optional TrueType/OpenType tables */ + /* */ + /***********************************************************************/ + + /* horizontal device metrics */ + TT_Hdmx hdmx; + + /* grid-fitting and scaling table */ + TT_Gasp gasp; /* the `gasp' table */ + + /* PCL 5 table */ + TT_PCLT pclt; + + /* embedded bitmaps support */ + FT_Int num_sbit_strikes; + TT_SBit_Strike* sbit_strikes; + + FT_Int num_sbit_scales; + TT_SBit_Scale* sbit_scales; + + /* postscript names table */ + TT_Post_Names postscript_names; + + + /***********************************************************************/ + /* */ + /* TrueType-specific fields (ignored by the OTF-Type2 driver) */ + /* */ + /***********************************************************************/ + + /* the glyph locations */ + FT_UShort num_locations; + FT_Long* glyph_locations; + + /* the font program, if any */ + FT_ULong font_program_size; + FT_Byte* font_program; + + /* the cvt program, if any */ + FT_ULong cvt_program_size; + FT_Byte* cvt_program; + + /* the original, unscaled, control value table */ + FT_ULong cvt_size; + FT_Short* cvt; + + /* the format 0 kerning table, if any */ + FT_Int num_kern_pairs; + FT_Int kern_table_index; + TT_Kern_0_Pair* kern_pairs; + + /* A pointer to the bytecode interpreter to use. This is also */ + /* used to hook the debugger for the `ttdebug' utility. */ + TT_Interpreter interpreter; + + + /***********************************************************************/ + /* */ + /* Other tables or fields. This is used by derivative formats like */ + /* OpenType. */ + /* */ + /***********************************************************************/ + + FT_Generic extra; + +} TT_FaceRec; + + +/*************************************************************************/ +/* */ +/* */ +/* TT_GlyphZone */ +/* */ +/* */ +/* A glyph zone is used to load, scale and hint glyph outline */ +/* coordinates. */ +/* */ +/* */ +/* memory :: A handle to the memory manager. */ +/* */ +/* max_points :: The maximal size in points of the zone. */ +/* */ +/* max_contours :: Max size in links contours of thez one. */ +/* */ +/* n_points :: The current number of points in the zone. */ +/* */ +/* n_contours :: The current number of contours in the zone. */ +/* */ +/* org :: The original glyph coordinates (font */ +/* units/scaled). */ +/* */ +/* cur :: The current glyph coordinates (scaled/hinted). */ +/* */ +/* tags :: The point control tags. */ +/* */ +/* contours :: The contours end points. */ +/* */ +typedef struct TT_GlyphZone_ +{ + FT_Memory memory; + FT_UShort max_points; + FT_UShort max_contours; + FT_UShort n_points; /* number of points in zone */ + FT_Short n_contours; /* number of contours */ + + FT_Vector* org; /* original point coordinates */ + FT_Vector* cur; /* current point coordinates */ + + FT_Byte* tags; /* current touch flags */ + FT_UShort* contours; /* contour end points */ + +} TT_GlyphZone; + + +/* handle to execution context */ +typedef struct TT_ExecContextRec_* TT_ExecContext; + +/* glyph loader structure */ +struct TT_Loader_ +{ + FT_Face face; + FT_Size size; + FT_GlyphSlot glyph; + FT_GlyphLoader* gloader; + + FT_ULong load_flags; + FT_UInt glyph_index; + + FT_Stream stream; + FT_Int byte_len; + + FT_Short n_contours; + FT_BBox bbox; + FT_Int left_bearing; + FT_Int advance; + FT_Bool preserve_pps; + FT_Vector pp1; + FT_Vector pp2; + + FT_ULong glyf_offset; + + /* the zone where we load our glyphs */ + TT_GlyphZone base; + TT_GlyphZone zone; + + TT_ExecContext exec; + FT_Byte* instructions; + FT_ULong ins_pos; + + /* for possible extensibility in other formats */ + void* other; + +}; + + +#endif /* TTTYPES_H */ + + +/* END */ diff --git a/src/game/ai_cast.c b/src/game/ai_cast.c new file mode 100644 index 0000000..6551000 --- /dev/null +++ b/src/game/ai_cast.c @@ -0,0 +1,1003 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: ai_cast.c +// Function: Wolfenstein AI Character Routines +// Programmer: Ridah +// Tab Size: 4 (real tabs) +//=========================================================================== + +#include "../game/g_local.h" +#include "../game/q_shared.h" +#include "../game/botlib.h" //bot lib interface +#include "../game/be_aas.h" +#include "../game/be_ea.h" +#include "../game/be_ai_gen.h" +#include "../game/be_ai_goal.h" +#include "../game/be_ai_move.h" +#include "../botai/botai.h" //bot ai interface + +#include "ai_cast.h" + +/* +The Wolfenstein AI uses the bot movement functions, and goal handling. + +The actual core thinking and decision making is handled by the Cast AI, +typically within the ai_cast*.c files. + +Some modifications to the botlib and botai are to be expected, the extent of those +changes is currently unknown. + +Currently, this seems to the the best approach, since if we're going to +use the AAS for navigation, we want to avoid having to re-write the movement +routines which are heavily associated with the AAS information. +*/ + +//cast states (allocated at run-time) +cast_state_t *caststates; +//number of characters +int numcast; +// +qboolean saveGamePending; +// +// minimum time between thinks (maximum is double this) +int aicast_thinktime; +// maximum number of character thinks at once +int aicast_maxthink; +// maximum clients +int aicast_maxclients; +// skill scale (0.0 -> 1.0) +float aicast_skillscale; + +// cvar to enable aicast debugging, set higher for more levels of debugging +vmCvar_t aicast_debug; +vmCvar_t aicast_debugname; +vmCvar_t aicast_scripts; + +// string versions of the attributes used for per-level, per-character definitions +char *castAttributeStrings[] = +{ + "RUNNING_SPEED", // max = 300 (running speed) + "WALKING_SPEED", // max = 300 (walking speed) + "CROUCHING_SPEED", // max = 300 (crouching speed) + "FOV", // max = 360 (field of view) + "YAW_SPEED", // max = 300 (yaw speed) + "LEADER", // max = 1.0 (ability to lead an AI squadron) + "AIM_SKILL", // max = 1.0 (skill while aiming) + "AIM_ACCURACY", // max = 1.0 (accuracy of firing) + "ATTACK_SKILL", // max = 1.0 (ability to attack and do other things, like retreat) + "REACTION_TIME", // max = 1.0 (upon seeing enemy, wait this long before reaction) + "ATTACK_CROUCH", // max = 1.0 (likely to crouch while firing) + "IDLE_CROUCH", // max = 1.0 (likely to crouch while idling) + "AGGRESSION", // max = 1.0 (willingness to fight till the death) + "TACTICAL", // max = 1.0 (ability to use strategy to their advantage, also behaviour whilst hunting enemy, more likely to creep around) + "CAMPER", // max = 1.0 (set this to make them stay in the spot they are spawned) + "ALERTNESS", // max = 1.0 (ability to notice enemies at long range) + "STARTING_HEALTH", + "HEARING_SCALE", + "HEARING_SCALE_NOT_PVS", + "INNER_DETECTION_RADIUS", + "PAIN_THRESHOLD_SCALE", + + NULL +}; + +/* +============ +AICast_Printf +============ +*/ +void AICast_Printf( int type, const char *fmt, ... ) { + char str[2048]; + va_list ap; + + va_start( ap, fmt ); + vsprintf( str, fmt, ap ); + va_end( ap ); + + switch ( type ) { + case AICAST_PRT_ALWAYS: { + G_Printf( "%s", str ); + break; + } + default: { + if ( aicast_debug.integer >= type ) { + G_Printf( "%s", str ); + } + break; + } + } +} + +/* +============ +AICast_GetCastState +============ +*/ +cast_state_t *AICast_GetCastState( int entitynum ) { + if ( entitynum < 0 || entitynum > level.maxclients ) { + return NULL; + } + // + return &( caststates[ entitynum ] ); +} + +/* +============== +AICast_SetupClient +============== +*/ +int AICast_SetupClient( int client ) { + cast_state_t *cs; + bot_state_t *bs; + + if ( !botstates[client] ) { + botstates[client] = G_Alloc( sizeof( bot_state_t ) ); + memset( botstates[client], 0, sizeof( bot_state_t ) ); + } + bs = botstates[client]; + + if ( bs->inuse ) { + BotAI_Print( PRT_FATAL, "client %d already setup\n", client ); + return qfalse; + } + + cs = AICast_GetCastState( client ); + cs->bs = bs; + + //allocate a goal state + bs->gs = trap_BotAllocGoalState( client ); + + bs->inuse = qtrue; + bs->client = client; + bs->entitynum = client; + bs->setupcount = qtrue; + bs->entergame_time = trap_AAS_Time(); + bs->ms = trap_BotAllocMoveState(); + + return qtrue; +} + +/* +============== +AICast_ShutdownClient +============== +*/ +int AICast_ShutdownClient( int client ) { + cast_state_t *cs; + bot_state_t *bs; + + if ( !( bs = botstates[client] ) ) { + return BLERR_NOERROR; + } + if ( !bs->inuse ) { + BotAI_Print( PRT_ERROR, "client %d already shutdown\n", client ); + return BLERR_AICLIENTALREADYSHUTDOWN; + } + + cs = AICast_GetCastState( client ); + // + memset( cs, 0, sizeof( cast_state_t ) ); + numcast--; + + // now do the other bot stuff + +#ifdef DEBUG +// botai_import.DebugLineDelete(bs->debugline); +#endif //DEBUG + + trap_BotFreeMoveState( bs->ms ); + //free the goal state + trap_BotFreeGoalState( bs->gs ); + // + //clear the bot state + memset( bs, 0, sizeof( bot_state_t ) ); + //set the inuse flag to qfalse + bs->inuse = qfalse; + //everything went ok + return BLERR_NOERROR; +} + +/* +============ +AICast_AddCastToGame +============ +*/ +//----(SA) modified this for head separation +gentity_t *AICast_AddCastToGame( gentity_t *ent, char *castname, char *model, char *head, char *sex, char *color, char *handicap ) { + int clientNum; + gentity_t *bot; + char userinfo[MAX_INFO_STRING]; + usercmd_t cmd; + + // create the bot's userinfo + userinfo[0] = '\0'; + + Info_SetValueForKey( userinfo, "name", castname ); + Info_SetValueForKey( userinfo, "rate", "25000" ); + Info_SetValueForKey( userinfo, "snaps", "20" ); + Info_SetValueForKey( userinfo, "handicap", handicap ); + Info_SetValueForKey( userinfo, "model", model ); + Info_SetValueForKey( userinfo, "head", head ); + Info_SetValueForKey( userinfo, "color", color ); + + // have the server allocate a client slot + clientNum = trap_BotAllocateClient(); + if ( clientNum == -1 ) { + G_Printf( S_COLOR_RED "BotAllocateClient failed\n" ); + return NULL; + } + bot = &g_entities[ clientNum ]; + bot->r.svFlags |= SVF_BOT; + bot->r.svFlags |= SVF_CASTAI; // flag it for special Cast AI behaviour + + // register the userinfo + trap_SetUserinfo( bot->s.number, userinfo ); + + // have it connect to the game as a normal client +//----(SA) ClientConnect requires a third 'isbot' parameter. setting to qfalse and noting + ClientConnect( bot->s.number, qtrue, qfalse ); +//----(SA) end + + // copy the origin/angles across + VectorCopy( ent->s.origin, bot->s.origin ); + VectorCopy( ent->s.angles, bot->s.angles ); + + memset( &cmd, 0, sizeof( cmd ) ); + ClientBegin( bot->s.number ); + + // set up the ai + AICast_SetupClient( bot->s.number ); + + return bot; +} + +/* +============ +AICast_CheckLevelAttributes +============ +*/ +void AICast_CheckLevelAttributes( cast_state_t *cs, gentity_t *ent, char **ppStr ) { + char *s; + int i; + + if ( !*ppStr ) { + return; + } + + while ( 1 ) { + s = COM_Parse( ppStr ); + if ( !s[0] || !Q_strncmp( s, "}", 2 ) ) { // end of attributes + break; + } + // + for ( i = 0; i < AICAST_MAX_ATTRIBUTES; i++ ) { + if ( !Q_strcasecmp( s, castAttributeStrings[i] ) ) { + // found a match, read in the value + s = COM_Parse( ppStr ); + if ( !s[0] ) { // end of attributes + break; + } + // set the attribute + cs->attributes[i] = atof( s ); + break; + } + } + } +} + +/* +============ +AICast_SetAASIndex +============ +*/ +void AICast_SetAASIndex( cast_state_t *cs ) { + if ( aiDefaults[cs->aiCharacter].bboxType == BBOX_SMALL ) { + cs->aasWorldIndex = AASWORLD_STANDARD; + cs->travelflags = AICAST_TFL_DEFAULT; + } else if ( aiDefaults[cs->aiCharacter].bboxType == BBOX_LARGE ) { + cs->aasWorldIndex = AASWORLD_LARGE; + cs->travelflags = AICAST_TFL_DEFAULT & ~TFL_DONOTENTER_LARGE; + } else { + Com_Error( ERR_DROP, "AICast_SetAASIndex: unsupported bounds size (%i)", aiDefaults[cs->aiCharacter].bboxType ); + } + + if ( !cs->attributes[ATTACK_CROUCH] ) { + cs->travelflags &= ~TFL_CROUCH; + } +} + +/* +============ +AICast_CreateCharacter + + returns 0 if unable to create the character +============ +*/ +gentity_t *AICast_CreateCharacter( gentity_t *ent, float *attributes, cast_weapon_info_t *weaponInfo, char *castname, char *model, char *head, char *sex, char *color, char *handicap ) { + gentity_t *newent; + gclient_t *client; + cast_state_t *cs; + char **ppStr; + int j; + + if ( g_gametype.integer != GT_SINGLE_PLAYER ) { // no cast AI in multiplayer + return NULL; + } + // are bots enabled? + if ( !trap_Cvar_VariableIntegerValue( "bot_enable" ) ) { + G_Printf( S_COLOR_RED "ERROR: Unable to spawn %s, 'bot_enable' is not set\n", ent->classname ); + return NULL; + } + // + // make sure we have a free slot for them + // + if ( level.numPlayingClients + 1 > aicast_maxclients ) { + G_Error( "Exceeded sv_maxclients (%d), unable to create %s\n", aicast_maxclients, ent->classname ); + return NULL; + } + // + // add it to the list (only do this if everything else passed) + // + + newent = AICast_AddCastToGame( ent, castname, model, head, sex, color, handicap ); + + if ( !newent ) { + return NULL; + } + client = newent->client; + // + // setup the character.. + // + cs = AICast_GetCastState( newent->s.number ); + // + cs->aiCharacter = ent->aiCharacter; + client->ps.aiChar = ent->aiCharacter; + // setup the attributes + memcpy( cs->attributes, attributes, sizeof( cs->attributes ) ); + ppStr = &ent->aiAttributes; + AICast_CheckLevelAttributes( cs, ent, ppStr ); + // + AICast_SetAASIndex( cs ); + // make sure they face the right direction + VectorCopy( ent->s.angles, cs->ideal_viewangles ); + // factor in the delta_angles + for ( j = 0; j < 3; j++ ) { + cs->viewangles[j] = AngleMod( newent->s.angles[j] - SHORT2ANGLE( newent->client->ps.delta_angles[j] ) ); + } + VectorCopy( ent->s.angles, newent->s.angles ); + VectorCopy( ent->s.origin, cs->startOrigin ); + // + cs->lastEnemy = -1; + cs->enemyNum = -1; + cs->leaderNum = -1; + cs->castScriptStatus.scriptGotoEnt = -1; + // + newent->aiName = ent->aiName; + newent->aiTeam = ent->aiTeam; + newent->targetname = ent->targetname; + // + newent->AIScript_AlertEntity = ent->AIScript_AlertEntity; + newent->aiInactive = ent->aiInactive; + newent->aiCharacter = cs->aiCharacter; + // + // parse the AI script for this character (if applicable) + cs->aiFlags |= AIFL_CORPSESIGHTING; // this is on by default for all characters, disabled if they have a "friendlysightcorpse" script event + AICast_ScriptParse( cs ); + // + // setup bounding boxes + //VectorCopy( mins, client->ps.mins ); + //VectorCopy( maxs, client->ps.maxs ); + AIChar_SetBBox( newent, cs, qfalse ); + client->ps.friction = cs->attributes[RUNNING_SPEED] / 300.0; + // + // clear weapons/ammo + client->ps.weapon = 0; + memcpy( client->ps.weapons, weaponInfo->startingWeapons, sizeof( weaponInfo->startingWeapons ) ); + memcpy( client->ps.ammo, weaponInfo->startingAmmo, sizeof( client->ps.ammo ) ); + // + // starting health + if ( ent->health ) { + newent->health = client->ps.stats[STAT_HEALTH] = client->ps.stats[STAT_MAX_HEALTH] = ent->health; + } else { + newent->health = client->ps.stats[STAT_HEALTH] = client->ps.stats[STAT_MAX_HEALTH] = cs->attributes[STARTING_HEALTH]; + } + // + cs->weaponInfo = weaponInfo; + // + cs->lastThink = level.time; + // + newent->pain = AICast_Pain; + newent->die = AICast_Die; + // + //update the attack inventory values + AICast_UpdateBattleInventory( cs, cs->enemyNum ); + +//----(SA) make sure all clips are loaded so we don't hear everyone loading up +// (we don't want to do this inside AICast_UpdateBattleInventory(), only on spawn or giveweapon) + for ( j = 0; j < WP_NUM_WEAPONS; j++ ) { + Fill_Clip( &client->ps, j ); + } +//----(SA) end + + // select a weapon + AICast_ChooseWeapon( cs, qfalse ); + + // + // set the default function, overwrite if necessary + cs->aiFlags |= AIFL_JUST_SPAWNED; + AIFunc_DefaultStart( cs ); + // + numcast++; + // + return newent; +} + +/* +============ +AICast_Init + + called at each level start, before the world and it's entities have been spawned +============ +*/ +static int numSpawningCast; + +void AICast_Init( void ) { + vmCvar_t cvar; + int i; + + numcast = 0; + numSpawningCast = 0; + saveGamePending = qtrue; + + trap_Cvar_Register( &aicast_debug, "aicast_debug", "0", 0 ); + trap_Cvar_Register( &aicast_debugname, "aicast_debugname", "", 0 ); + trap_Cvar_Register( &aicast_scripts, "aicast_scripts", "1", 0 ); + + // (aicast_thinktime / sv_fps) * aicast_maxthink = number of cast's to think between each aicast frame + // so.. + // (100 / 20) * 6 = 30 + // + // so if the level has more than 30 AI cast's, they could start to bunch up, resulting in slower thinks + + trap_Cvar_Register( &cvar, "aicast_thinktime", "50", 0 ); + aicast_thinktime = trap_Cvar_VariableIntegerValue( "aicast_thinktime" ); + + trap_Cvar_Register( &cvar, "aicast_maxthink", "4", 0 ); + aicast_maxthink = trap_Cvar_VariableIntegerValue( "aicast_maxthink" ); + + aicast_maxclients = trap_Cvar_VariableIntegerValue( "sv_maxclients" ); + + aicast_skillscale = (float)trap_Cvar_VariableIntegerValue( "g_gameSkill" ) / (float)GSKILL_MAX; + + caststates = G_Alloc( aicast_maxclients * sizeof( cast_state_t ) ); + memset( caststates, 0, sizeof( caststates ) ); + for ( i = 0; i < MAX_CLIENTS; i++ ) { + caststates[i].entityNum = i; + } + +/* RF, this is useless, since the AAS hasnt been loaded yet + // try and load in the AAS now, so we can interact with it during spawning of entities + i = 0; + trap_AAS_SetCurrentWorld(0); + while (!trap_AAS_Initialized() && (i++ < 10)) { + trap_BotLibStartFrame((float) level.time / 1000); + } +*/ +} + +/* +=============== +AICast_FindEntityForName +=============== +*/ +gentity_t *AICast_FindEntityForName( char *name ) { + gentity_t *trav; + int i; + + for ( trav = g_entities, i = 0; i < aicast_maxclients; i++, trav++ ) { + if ( !trav->inuse ) { + continue; + } + if ( !trav->client ) { + continue; + } + if ( !trav->aiName ) { + continue; + } + if ( strcmp( trav->aiName, name ) ) { + continue; + } + return trav; + } + return NULL; +} + +/* +=============== +AICast_TravEntityForName +=============== +*/ +gentity_t *AICast_TravEntityForName( gentity_t *startent, char *name ) { + gentity_t *trav; + + if ( !startent ) { + trav = g_entities; + } else { + trav = startent + 1; + } + + for ( ; trav < g_entities + aicast_maxclients; trav++ ) { + if ( !trav->inuse ) { + continue; + } + if ( !trav->client ) { + continue; + } + if ( !trav->aiName ) { + continue; + } + if ( strcmp( trav->aiName, name ) ) { + continue; + } + return trav; + } + return NULL; +} + +/* +============ +AIChar_AIScript_AlertEntity + + triggered spawning, called from AI scripting +============ +*/ +void AIChar_AIScript_AlertEntity( gentity_t *ent ) { + vec3_t mins, maxs; + int numTouch, touch[10], i; + cast_state_t *cs; + + if ( !ent->aiInactive ) { + return; + } + + cs = AICast_GetCastState( ent->s.number ); + + // if the current bounding box is invalid, then wait + VectorAdd( ent->r.currentOrigin, ent->r.mins, mins ); + VectorAdd( ent->r.currentOrigin, ent->r.maxs, maxs ); + trap_UnlinkEntity( ent ); + + numTouch = trap_EntitiesInBox( mins, maxs, touch, 10 ); + + // check that another client isn't inside us + if ( numTouch ) { + for ( i = 0; i < numTouch; i++ ) { + // RF, note we should only check against clients since zombies need to spawn inside func_explosive (so they dont clip into view after it explodes) + if ( g_entities[touch[i]].client && g_entities[touch[i]].r.contents == CONTENTS_BODY ) { + //if (g_entities[touch[i]].r.contents & MASK_PLAYERSOLID) + break; + } + } + if ( i == numTouch ) { + numTouch = 0; + } + } + + if ( numTouch ) { + // invalid location + cs->aiFlags |= AIFL_WAITINGTOSPAWN; + return; + } + + // RF, has to disable this so I could test some maps which have erroneously placed alertentity calls + //ent->AIScript_AlertEntity = NULL; + cs->aiFlags &= ~AIFL_WAITINGTOSPAWN; + ent->aiInactive = qfalse; + trap_LinkEntity( ent ); + + // trigger a spawn script event + AICast_ScriptEvent( AICast_GetCastState( ent->s.number ), "spawn", "" ); + // make it think so we update animations/angles + AICast_Think( ent->s.number, (float)FRAMETIME / 1000 ); + cs->lastThink = level.time; + AICast_UpdateInput( cs, FRAMETIME ); + trap_BotUserCommand( cs->bs->client, &( cs->lastucmd ) ); +} + + +/* +================ +AICast_DelayedSpawnCast +================ +*/ +void AICast_DelayedSpawnCast( gentity_t *ent, int castType ) { + int i; + + // ............................ + // head separation + if ( !ent->aiSkin ) { + G_SpawnString( "skin", "", &ent->aiSkin ); + } + if ( !ent->aihSkin ) { + G_SpawnString( "head", "default", &ent->aihSkin ); + } + G_SpawnInt( "aiteam", "-1", &ent->aiTeam ); + // ............................ + + +//----(SA) make sure client registers the default weapons for this char + for ( i = 0; aiDefaults[ent->aiCharacter].weapons[i]; i++ ) { + RegisterItem( BG_FindItemForWeapon( aiDefaults[ent->aiCharacter].weapons[i] ) ); + } +//----(SA) end + + // we have to wait a bit before spawning it, otherwise the server will just delete it, since it's treated like a client + ent->think = AIChar_spawn; + ent->nextthink = level.time + FRAMETIME * 4; // have to wait more than 3 frames, since the server runs 3 frames before it clears all clients + + // we don't really want to start this character right away, but if we don't spawn the client + // now, if the game gets saved after the character spawns in, when it gets re-loaded, the client + // won't get spawned properly. + if ( ent->spawnflags & 1 ) { // TriggerSpawn + ent->AIScript_AlertEntity = AIChar_AIScript_AlertEntity; + ent->aiInactive = qtrue; + } + + // RF, had to move this down since some dev maps don't properly spawn the guys in, so we + // get a crash when transitioning between levels after they all spawn at once (overloading + // the client/server command buffers) + ent->nextthink += FRAMETIME * ( ( numSpawningCast + 1 ) / 3 ); // space them out a bit so we don't overflow the client + + ent->aiCharacter = castType; + numSpawningCast++; +} + +/* +================== +AICast_CastScriptThink +================== +*/ +void AICast_CastScriptThink( void ) { + int i; + gentity_t *ent; + cast_state_t *cs; + + for ( i = 0, ent = g_entities, cs = caststates; i < level.maxclients; i++, ent++, cs++ ) { + if ( !ent->inuse ) { + continue; + } + if ( !cs->bs ) { + continue; + } + if ( ent->health <= 0 ) { + continue; + } + AICast_ScriptRun( cs, qfalse ); + } +} + +/* +================== +AICast_EnableRenderingThink +================== +*/ +void AICast_EnableRenderingThink( gentity_t *ent ) { + trap_Cvar_Set( "cg_norender", "0" ); +// trap_S_FadeAllSound(1.0f, 1000); // fade sound up + G_FreeEntity( ent ); +} + +/* +================== +AICast_CheckLoadGame + + at the start of a level, the game is either saved, or loaded + + we must wait for all AI to spawn themselves, and a real client to connect +================== +*/ +void AICast_CheckLoadGame( void ) { + char loading[4]; + gentity_t *ent = NULL; // TTimo: VC6 'may be used without having been init' + qboolean ready; + cast_state_t *pcs; + + // have we already done the save or load? + if ( !saveGamePending ) { + return; + } + + // tell the cgame NOT to render the scene while we are waiting for things to settle + trap_Cvar_Set( "cg_norender", "1" ); + + trap_Cvar_VariableStringBuffer( "savegame_loading", loading, sizeof( loading ) ); + +// reloading = qtrue; + trap_Cvar_Set( "g_reloading", "1" ); + + if ( strlen( loading ) > 0 && atoi( loading ) != 0 ) { + // screen should be black if we are at this stage + trap_SetConfigstring( CS_SCREENFADE, va( "1 %i 1", level.time - 10 ) ); + +// if (!reloading && atoi(loading) == 2) { + if ( !( g_reloading.integer ) && atoi( loading ) == 2 ) { + // (SA) hmm, this seems redundant when it sets it above... +// reloading = qtrue; // this gets reset at the Map_Restart() since the server unloads the game dll + trap_Cvar_Set( "g_reloading", "1" ); + } + + ready = qtrue; + if ( numSpawningCast != numcast ) { + ready = qfalse; + } else if ( !( ent = AICast_FindEntityForName( "player" ) ) ) { + ready = qfalse; + } else if ( !ent->client || ent->client->pers.connected != CON_CONNECTED ) { + ready = qfalse; + } + + if ( ready ) { + trap_Cvar_Set( "savegame_loading", "0" ); // in-case it aborts + saveGamePending = qfalse; + G_LoadGame( NULL ); // always load the "current" savegame + + // RF, spawn a thinker that will enable rendering after the client has had time to process the entities and setup the display + //trap_Cvar_Set( "cg_norender", "0" ); + ent = G_Spawn(); + ent->nextthink = level.time + 200; + ent->think = AICast_EnableRenderingThink; + + // wait for the clients to return from faded screen + //trap_SetConfigstring( CS_SCREENFADE, va("0 %i 1500", level.time + 500) ); + trap_SetConfigstring( CS_SCREENFADE, va( "0 %i 750", level.time + 500 ) ); + level.reloadPauseTime = level.time + 1100; + + // make sure sound fades up + trap_SendServerCommand( -1, va( "snd_fade 1 %d", 2000 ) ); //----(SA) added + + AICast_CastScriptThink(); + } + } else { + + ready = qtrue; + if ( numSpawningCast != numcast ) { + ready = qfalse; + } else if ( !( ent = AICast_FindEntityForName( "player" ) ) ) { + ready = qfalse; + } else if ( !ent->client || ent->client->pers.connected != CON_CONNECTED ) { + ready = qfalse; + } + + // not loading a game, we must be in a new level, so look for some persistant data to read in, then save the game + if ( ready ) { + G_LoadPersistant(); // make sure we save the game after we have brought across the items + + trap_Cvar_Set( "g_totalPlayTime", "0" ); // reset play time + trap_Cvar_Set( "g_attempts", "0" ); + pcs = AICast_GetCastState( ent->s.number ); + pcs->totalPlayTime = 0; + pcs->lastLoadTime = 0; + pcs->attempts = 0; + + // RF, disabled, since the pregame menu turns this off after the button is pressed, this isn't + // required here + // RF, spawn a thinker that will enable rendering after the client has had time to process the entities and setup the display + //trap_Cvar_Set( "cg_norender", "0" ); + //ent = G_Spawn(); + //ent->nextthink = level.time + 200; + //ent->think = AICast_EnableRenderingThink; + + saveGamePending = qfalse; + + // wait for the clients to return from faded screen +// trap_SetConfigstring( CS_SCREENFADE, va("0 %i 1500", level.time + 500) ); +// trap_SetConfigstring( CS_SCREENFADE, va("0 %i 750", level.time + 500) ); + // (SA) send a command that will be interpreted for both the screenfade and any other effects (music cues, pregame menu, etc) + +// briefing menu will handle transition, just set a cvar for it to check for drawing the 'continue' button + trap_SendServerCommand( -1, "rockandroll\n" ); + + level.reloadPauseTime = level.time + 1100; + + AICast_CastScriptThink(); + } + } +} + +/* +=============== +AICast_SolidsInBBox +=============== +*/ +qboolean AICast_SolidsInBBox( vec3_t pos, vec3_t mins, vec3_t maxs, int entnum, int mask ) { + trace_t tr; + + if ( g_entities[entnum].health <= 0 ) { + return qfalse; + } + + trap_Trace( &tr, pos, mins, maxs, pos, entnum, mask ); + if ( tr.startsolid || tr.allsolid ) { + return qtrue; + } else { + return qfalse; + } +} + +/* +=============== +AICast_Activate +=============== +*/ +void AICast_Activate( int activatorNum, int entNum ) { + cast_state_t *cs; + + cs = AICast_GetCastState( entNum ); + if ( cs->activate ) { + cs->activate( entNum, activatorNum ); + } + + AICast_Printf( AICAST_PRT_DEBUG, "activated entity # %i\n", entNum ); +} + +/* +================ +AICast_NoFlameDamage +================ +*/ +qboolean AICast_NoFlameDamage( int entNum ) { + cast_state_t *cs; + + if ( entNum >= MAX_CLIENTS ) { + return qfalse; + } + + // DHM - Nerve :: Not in multiplayer + if ( g_gametype.integer != GT_SINGLE_PLAYER ) { + return qfalse; + } + + cs = AICast_GetCastState( entNum ); + return ( ( cs->aiFlags & AIFL_NO_FLAME_DAMAGE ) != 0 ); +} + +/* +================ +AICast_SetFlameDamage +================ +*/ +void AICast_SetFlameDamage( int entNum, qboolean status ) { + cast_state_t *cs; + + if ( entNum >= MAX_CLIENTS ) { + return; + } + + // DHM - Nerve :: Not in multiplayer + if ( g_gametype.integer != GT_SINGLE_PLAYER ) { + return; + } + + cs = AICast_GetCastState( entNum ); + + if ( status ) { + cs->aiFlags |= AIFL_NO_FLAME_DAMAGE; + } else { + cs->aiFlags &= ~AIFL_NO_FLAME_DAMAGE; + } +} + +/* +=============== +G_SetAASBlockingEntity + + Adjusts routing so AI knows it can't move through this entity +=============== +*/ +void G_SetAASBlockingEntity( gentity_t *ent, qboolean blocking ) { + ent->AASblocking = blocking; + trap_AAS_SetAASBlockingEntity( ent->r.absmin, ent->r.absmax, blocking ); +} + +/* +=============== +AICast_AdjustIdealYawForMover +=============== +*/ +void AICast_AdjustIdealYawForMover( int entnum, float yaw ) { + cast_state_t *cs = AICast_GetCastState( entnum ); + // + cs->ideal_viewangles[YAW] += yaw; +} + +/* +=============== +AICast_AgePlayTime +=============== +*/ +void AICast_AgePlayTime( int entnum ) { + cast_state_t *cs = AICast_GetCastState( entnum ); + // + if ( saveGamePending ) { + return; + } +// if (reloading) + if ( g_reloading.integer ) { + return; + } + // + if ( ( level.time - cs->lastLoadTime ) > 1000 ) { + if ( /*(level.time - cs->lastLoadTime) < 2000 &&*/ ( level.time - cs->lastLoadTime ) > 0 ) { + cs->totalPlayTime += level.time - cs->lastLoadTime; + trap_Cvar_Set( "g_totalPlayTime", va( "%i", cs->totalPlayTime ) ); + } + // + cs->lastLoadTime = level.time; + } +} + +/* +=============== +AICast_NoReload +=============== +*/ +int AICast_NoReload( int entnum ) { + cast_state_t *cs = AICast_GetCastState( entnum ); + // + return ( ( cs->aiFlags & AIFL_NO_RELOAD ) != 0 ); +} + + +/* +============== +AICast_PlayTime +============== +*/ +int AICast_PlayTime( int entnum ) { + cast_state_t *cs = AICast_GetCastState( entnum ); + return ( cs->totalPlayTime ); +} + +/* +============== +AICast_NumAttempts +============== +*/ +int AICast_NumAttempts( int entnum ) { + cast_state_t *cs = AICast_GetCastState( entnum ); + return ( cs->attempts ); +} + +void AICast_RegisterPain( int entnum ) { + cast_state_t *cs = AICast_GetCastState( entnum ); + if ( cs ) { + cs->lastPain = level.time; + } +} \ No newline at end of file diff --git a/src/game/ai_cast.h b/src/game/ai_cast.h new file mode 100644 index 0000000..2284992 --- /dev/null +++ b/src/game/ai_cast.h @@ -0,0 +1,746 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: ai_cast.h +// Function: Wolfenstein AI Character Routines +// Programmer: Ridah +// Tab Size: 4 (real tabs) +//=========================================================================== + +#include "../botai/ai_main.h" // just so we can use the structures +#include "../botai/ai_dmq3.h" // just so we can use the structures + +#include "ai_cast_fight.h" + +// +// constants/defines +// +#define MAX_AIFUNCS 15 // if we go over this per frame, likely to have an infinite loop +// +#define SIGHT_PER_SEC 50 // do this many sight iterations per second +// +// Cast AI specific action flags (get translated into ucmd's +#define CASTACTION_WALK 1 +// +#define MAX_SCRIPT_ACCUM_BUFFERS 8 +// +#define AICAST_PRT_ALWAYS 0 +#define AICAST_PRT_DEBUG 1 +// +#define DEBUG_FOLLOW_DIST 96 +// +#define MAX_LEADER_DIST 256 +// +#define AASWORLD_STANDARD 0 +#define AASWORLD_LARGE 1 +// +// use this for returning the length of an anim +#define ANIMLENGTH( frames,fps ) ( ( frames * 1000 ) / fps ) +// +#define AICAST_TFL_DEFAULT TFL_DEFAULT & ~( TFL_JUMPPAD | TFL_ROCKETJUMP | TFL_BFGJUMP | TFL_GRAPPLEHOOK | TFL_DOUBLEJUMP | TFL_RAMPJUMP | TFL_STRAFEJUMP | TFL_LAVA ) //----(SA) modified since slime is no longer deadly +//#define AICAST_TFL_DEFAULT TFL_DEFAULT & ~(TFL_JUMPPAD|TFL_ROCKETJUMP|TFL_BFGJUMP|TFL_GRAPPLEHOOK|TFL_DOUBLEJUMP|TFL_RAMPJUMP|TFL_STRAFEJUMP|TFL_SLIME|TFL_LAVA) +// +// AI flags +#define AIFL_CATCH_GRENADE 0x1 +#define AIFL_NO_FLAME_DAMAGE 0x2 +#define AIFL_FIRED 0x4 +#define AIFL_LAND_ANIM_PLAYED 0x8 +#define AIFL_ROLL_ANIM 0x10 +#define AIFL_FLIP_ANIM 0x20 +#define AIFL_STAND_IDLE2 0x40 +#define AIFL_NOAVOID 0x80 // if set, this AI will ignore requests for us to move out the way +#define AIFL_NOPAIN 0x100 // don't stop for pain anims +#define AIFL_WALKFORWARD 0x200 // only walk forward +#define AIFL_DENYACTION 0x400 // used by scripting to prevent dynamic code from executing certain behaviour +#define AIFL_VIEWLOCKED 0x800 // prevent anything outside movement routines from changing view +#define AIFL_CORPSESIGHTING 0x1000 // share information through friendly corpses +#define AIFL_WAITINGTOSPAWN 0x2000 // waiting until space is clear to spawn in to the game +#define AIFL_JUST_SPAWNED 0x4000 +#define AIFL_NO_RELOAD 0x8000 // this character doesn't need to reload +#define AIFL_TALKING 0x10000 +#define AIFL_NO_HEADLOOK 0x20000 +#define AIFL_ATTACK_CROUCH 0x40000 +#define AIFL_MISCFLAG1 0x80000 // used various bits of code, temporarily +#define AIFL_MISCFLAG2 0x100000 // used various bits of code, temporarily +#define AIFL_ZOOMING 0x200000 +#define AIFL_NO_HEADSHOT_DMG 0x400000 +#define AIFL_DIVE_ANIM 0x800000 // able to dive to cover +#define AIFL_NO_TESLA_DAMAGE 0x1000000 +#define AIFL_EXPLICIT_ROUTING 0x2000000 // direct routing towards ai_markers, rather than using AAS +#define AIFL_DISMOUNTING 0x4000000 +#define AIFL_SPECIAL_FUNC 0x8000000 // prevent external interuption of current think func + +// +// predict events +typedef enum +{ + PREDICTSTOP_NONE, + PREDICTSTOP_HITENT, + PREDICTSTOP_HITCLIENT +} predictStop_t; +// +typedef enum +{ + AITEAM_NAZI, + AITEAM_ALLIES, + AITEAM_MONSTER, + AITEAM_SPARE1, + AITEAM_SPARE2, + AITEAM_SPARE3, + AITEAM_SPARE4, + AITEAM_NEUTRAL +} AITeam_t; +// +typedef enum +{ + BBOX_SMALL, + BBOX_LARGE +} BBoxType_t; +// +// attributes +// !!! NOTE: any changes to this must be reflected in the attributeStrings in ai_cast.c +typedef enum +{ + RUNNING_SPEED, // max = 300 (running speed) + WALKING_SPEED, // max = 300 (walking speed) + CROUCHING_SPEED, // max = 300 (crouching speed) + FOV, // max = 360 (field of view) + YAW_SPEED, // max = 300 (yaw speed, so we can make zombie's turn slowly) + LEADER, // max = 1.0 (ability to lead an AI squadron) + AIM_SKILL, // max = 1.0 (skill while aiming) + AIM_ACCURACY, // max = 1.0 (accuracy of firing) + ATTACK_SKILL, // max = 1.0 (ability to attack and do other things, like retreat) + REACTION_TIME, // max = 1.0 (upon seeing enemy, wait this long before reaction) + ATTACK_CROUCH, // max = 1.0 (likely to crouch while firing) + IDLE_CROUCH, // max = 1.0 (likely to crouch while idling) + AGGRESSION, // max = 1.0 (willingness to fight till the death) + TACTICAL, // max = 1.0 (ability to use strategy to their advantage, also behaviour whilst hunting enemy, more likely to creep around) + CAMPER, // max = 1.0 (set this to make them stay in the spot they are spawned) + ALERTNESS, // max = 1.0 (ability to notice enemies at long range) + STARTING_HEALTH, // MAX = 999 (starting health) + HEARING_SCALE, // max = 999 (multiply default hearing ranges by this) + HEARING_SCALE_NOT_PVS, // max = 999 (multiply hearing range by this if outside PVS) + INNER_DETECTION_RADIUS, // default = 512 (enemies within this range trigger immediate combat mode + PAIN_THRESHOLD_SCALE, // default = 1.0 + + AICAST_MAX_ATTRIBUTES + +} castAttributes_t; +// +typedef enum +{ + SIGHTSOUNDSCRIPT, + ATTACKSOUNDSCRIPT, + ORDERSSOUNDSCRIPT, + DEATHSOUNDSCRIPT, + QUIETDEATHSOUNDSCRIPT, //----(SA) ADDED FOR SILENT DEATHS (SNIPER/KNIFE) + FLAMEDEATHSOUNDSCRIPT, //----(SA) ADDED FOR FLAMING + PAINSOUNDSCRIPT, + + STAYSOUNDSCRIPT, + FOLLOWSOUNDSCRIPT, + ORDERSDENYSOUNDSCRIPT, + MISC1SOUNDSCRIPT, + + MAX_AI_EVENT_SOUNDS +} AIEventSounds_t; +// +typedef struct { + char *name; + float attributes[AICAST_MAX_ATTRIBUTES]; + + char *soundScripts[MAX_AI_EVENT_SOUNDS]; + + int aiTeam; + char *skin; + int weapons[8]; + int bboxType; + vec2_t crouchstandZ; + int aiFlags; + + char *( *aifuncAttack1 )( struct cast_state_s *cs ); //use this battle aifunc for monster_attack1 + char *( *aifuncAttack2 )( struct cast_state_s *cs ); //use this battle aifunc for monster_attack2 + char *( *aifuncAttack3 )( struct cast_state_s *cs ); //use this battle aifunc for monster_attack2 + + char *loopingSound; // play this sound constantly while alive + + aistateEnum_t aiState; +} AICharacterDefaults_t; + +// +// script flags +#define SFL_NOCHANGEWEAPON 0x1 +#define SFL_NOAIDAMAGE 0x2 +#define SFL_FRIENDLYSIGHTCORPSE_TRIGGERED 0x4 +#define SFL_WAITING_RESTORE 0x8 +#define SFL_FIRST_CALL 0x10 +// +// attributes strings (used for per-entity attribute definitions) +// NOTE: these must match the attributes above) +extern char *castAttributeStrings[]; +extern AICharacterDefaults_t aiDefaults[NUM_CHARACTERS]; +// +// structure defines +// +#define AIVIS_ENEMY 1 +#define AIVIS_INSPECTED 2 // we have inspected them once already +#define AIVIS_INSPECT 4 // we should inspect them when we get a chance +#define AIVIS_PROCESS_SIGHTING 8 // so we know if we have or haven't processed the sighting since they were last seen +#define AIVIS_SIGHT_SCRIPT_CALLED 0x10 // set once sight script has been called.. only call once +// +// share range +#define AIVIS_SHARE_RANGE 384 // if we are within this range of a friendly, share their vis info +// +#define MAX_CHASE_MARKERS 3 +#define CHASE_MARKER_INTERVAL 1000 +// +#define COMBAT_TIMEOUT 8000 +// sight info +typedef struct +{ + int flags; + int lastcheck_timestamp; + int real_visible_timestamp; + int real_update_timestamp; + int real_notvisible_timestamp; + int visible_timestamp; // time we last recorded a sighting + vec3_t visible_pos; // position we last knew of them being at (could be hearing, etc) + vec3_t real_visible_pos; // position we last physically saw them + vec3_t visible_vel; // velocity during last sighting + int notvisible_timestamp; // last time we didn't see the entity (used for reaction delay) + vec3_t chase_marker[MAX_CHASE_MARKERS]; + int chase_marker_count; + int lastcheck_health; +} cast_visibility_t; +// +// starting weapons, ammo, etc +typedef struct +{ + int startingWeapons[MAX_WEAPONS / ( sizeof( int ) * 8 )]; + int startingAmmo[MAX_WEAPONS]; // starting ammo values for each weapon (set to 999 for unlimited) +} cast_weapon_info_t; +// +// scripting +typedef struct +{ + char *actionString; + qboolean ( *actionFunc )( struct cast_state_s *cs, char *params ); +} cast_script_stack_action_t; +// +typedef struct +{ + // + // set during script parsing + cast_script_stack_action_t *action; // points to an action to perform + char *params; +} cast_script_stack_item_t; +// +#define AICAST_MAX_SCRIPT_STACK_ITEMS 64 +// +typedef struct +{ + cast_script_stack_item_t items[AICAST_MAX_SCRIPT_STACK_ITEMS]; + int numItems; +} cast_script_stack_t; +// +typedef struct +{ + int eventNum; // index in scriptEvents[] + char *params; // trigger targetname, etc + cast_script_stack_t stack; +} cast_script_event_t; +// +typedef struct +{ + char *eventStr; + qboolean ( *eventMatch )( cast_script_event_t *event, char *eventParm ); +} cast_script_event_define_t; +// +typedef struct +{ + int castScriptStackHead, castScriptStackChangeTime; + int castScriptEventIndex; // current event containing stack of actions to perform + // scripting system AI variables (set by scripting system, used directly by AI) + int scriptId; // incremented each time the script changes + int scriptFlags; + int scriptNoAttackTime; + int scriptNoMoveTime; + int playAnimViewlockTime; + int scriptGotoEnt; // just goto them, then resume normal behaviour (don't follow) + int scriptGotoId; + vec3_t scriptWaitPos; + int scriptWaitMovetime; + vec3_t scriptWaitHidePos; + int scriptWaitHideTime; + int scriptNoSightTime; + int scriptAttackEnt; // we should always attack this AI if they are alive + vec3_t playanim_viewangles; +} cast_script_status_t; +// +typedef struct +{ + aistateEnum_t currentState; + aistateEnum_t nextState; + int nextStateTimer; // time left until "newState" is reached +} aistate_t; +// +typedef enum +{ + MS_DEFAULT, + MS_WALK, + MS_RUN, + MS_CROUCH +} movestate_t; +// +typedef enum +{ + MSTYPE_NONE, + MSTYPE_TEMPORARY, + MSTYPE_PERMANENT +} movestateType_t; +// +// +typedef struct aicast_checkattack_cache_s +{ + int enemy; + qboolean allowHitWorld; + int time; + int weapon; + qboolean result; +} aicast_checkattack_cache_t; +// +// -------------------------------------------------------------------------------- +// the main cast structure +typedef struct cast_state_s +{ + bot_state_t *bs; + int entityNum; + + int aasWorldIndex; // set this according to our bounding box type + + // Cast specific information follows. Add to this as needed, this way the bot_state_t structure + // remains untouched. + + int aiCharacter; + int aiFlags; + int lastThink; // time they last thinked, so we can vary the think times + int actionFlags; // cast AI specific movement flags + int lastPain, lastPainDamage; + int travelflags; + int thinkFuncChangeTime; + + aistateEnum_t aiState; + movestate_t movestate; // walk, run, crouch etc (can be specified in a script) + movestateType_t movestateType; // temporary, permanent, etc + + float attributes[AICAST_MAX_ATTRIBUTES]; + // these define the abilities of each cast AI + + // scripting system + int numCastScriptEvents; + cast_script_event_t *castScriptEvents; // contains a list of actions to perform for each event type + cast_script_status_t castScriptStatus; // current status of scripting + cast_script_status_t castScriptStatusCurrent; // scripting status to use for backups + cast_script_status_t castScriptStatusBackup; // perm backup of status of scripting, only used by backup and restore commands + int scriptCallIndex; // inc'd each time a script is called + int scriptAnimTime, scriptAnimNum; // last time an anim was played using scripting + // the accumulation buffer + int scriptAccumBuffer[MAX_SCRIPT_ACCUM_BUFFERS]; + + // + cast_weapon_info_t *weaponInfo; // FIXME: make this a list, so they can have multiple weapons? + cast_visibility_t vislist[MAX_CLIENTS]; // array of all other client entities, allocated at level start-up + int weaponFireTimes[MAX_WEAPONS]; + + char *( *aifunc )( struct cast_state_s *cs ); //current AI function + char *( *oldAifunc )( struct cast_state_s *cs ); // just so we can restore the last aiFunc if required + + char *( *aifuncAttack1 )( struct cast_state_s *cs ); //use this battle aifunc for monster_attack1 + char *( *aifuncAttack2 )( struct cast_state_s *cs ); //use this battle aifunc for monster_attack2 + char *( *aifuncAttack3 )( struct cast_state_s *cs ); //use this battle aifunc for monster_attack2 + + void ( *painfunc )( gentity_t *ent, gentity_t *attacker, int damage, vec3_t point ); + void ( *deathfunc )( gentity_t *ent, gentity_t *attacker, int damage, int mod ); //----(SA) added mod + void ( *sightfunc )( gentity_t *ent, gentity_t *other, int lastSight ); + + //int (*getDeathAnim)(gentity_t *ent, gentity_t *attacker, int damage); + void ( *sightEnemy )( gentity_t *ent, gentity_t *other ); + void ( *sightFriend )( gentity_t *ent, gentity_t *other ); + + void ( *activate )( int entNum, int activatorNum ); + + // + // !!! NOTE: make sure any entityNum type variables get initialized + // to -1 in AICast_CreateCharacter(), or they'll be defaulting to + // the player (index 0) + // + + // goal/AI stuff + + int followEntity; + float followDist; + qboolean followIsGoto; // we are really just going to the entity, but should wait until scripting tells us we can stop + int followTime; // if this runs out, the scripting has probably been interupted + qboolean followSlowApproach; + + int leaderNum; // entnum of player we are following + + float speedScale; // so we can vary movement speed + + float combatGoalTime; + vec3_t combatGoalOrigin; + + int lastGetHidePos; + int startAttackCount; // incremented each time we start a standing attack + // used to make sure we only find a combat spot once per attack + int combatSpotAttackCount; + int combatSpotDelayTime; + int startBattleChaseTime; + + int blockedTime; // time they were last blocked by a solid entity + int obstructingTime; // time that we should move so we are not obstructing someone else + vec3_t obstructingPos; + + int blockedAvoidTime; + float blockedAvoidYaw; + + int deathTime; + int rebirthTime, revivingTime; + + // battle values + int enemyHeight; + int enemyDist; + + vec3_t takeCoverPos, takeCoverEnemyPos; + int takeCoverTime; + + int attackSpotTime; + + int triggerReleaseTime; + + int lastWeaponFired; // set each time a weapon is fired. used to detect when a weapon has been fired from within scripting + vec3_t lastWeaponFiredPos; + int lastWeaponFiredWeaponNum; + + // idle behaviour stuff + int lastEnemy, nextIdleAngleChange; + float idleYawChange, idleYaw; + + qboolean crouchHideFlag; + + int doorMarker, doorEntNum; + + // Rafael + int attackSNDtime; + int attacksnd; + int painSoundTime; + int firstSightTime; + qboolean secondDeadTime; + // done + + int startGrenadeFlushTime; + int lockViewAnglesTime; + int grenadeFlushEndTime; + int grenadeFlushFiring; + + int dangerEntity; + int dangerEntityValidTime; // dangerEntity is valid until this time expires + vec3_t dangerEntityPos; // dangerEntity is predicted to end up here + int dangerEntityTimestamp; // time this danger was recorded + float dangerDist; + + int mountedEntity; // mg42, etc that we have mounted + int inspectBodyTime; + vec3_t startOrigin; + + int damageQuota; + int damageQuotaTime; + + int dangerLastGetAvoid; + int lastAvoid; + + int doorMarkerTime, doorMarkerNum, doorMarkerDoor; + + int pauseTime; // absolutely don't move move while this is > level.time + + aicast_checkattack_cache_t checkAttackCache; + + int secretsFound; + + int attempts; + + qboolean grenadeGrabFlag; // if this is set, we need to play the anim before we can grab it + + vec3_t lastMoveToPosGoalOrg; // if this changes, we should reset the Bot Avoid Reach + + int noAttackTime; // used by dynamic AI to stop attacking for set time + + int lastRollMove; + int lastFlipMove; + + vec3_t stimFlyAttackPos; + + int lastDodgeRoll; // last time we rolled to get out of our enemies direct aim + int battleRollTime; + + vec3_t viewlock_viewangles; + int grenadeKickWeapon; + + int animHitCount; // for stepping through the frames on which to inflict damage + + int totalPlayTime, lastLoadTime; + + int queryStartTime, queryCountValidTime, queryCount, queryAlertSightTime; + + int lastScriptSound; + + int inspectNum; + + int scriptPauseTime; + + int bulletImpactEntity; + int bulletImpactTime; // last time we heard/saw a bullet impact + int bulletImpactIgnoreTime; + vec3_t bulletImpactStart, bulletImpactEnd; + + int audibleEventTime; + vec3_t audibleEventOrg; + int audibleEventEnt; + + int battleChaseMarker, battleChaseMarkerDir; + + int lastBattleHunted; // last time an enemy decided to hunt us + int battleHuntPauseTime, battleHuntViewTime; + + int lastAttackCrouch; + + int lastMoveThink; // last time we ran our ClientThink() + + int numEnemies; // last count of enemies that are currently pursuing us + + int noReloadTime; // dont reload prematurely until this time has expired + + int lastValidAreaNum[2]; // last valid area within each AAS world + int lastValidAreaTime[2]; // time we last got the area + + int weaponNum; // our current weapon + int enemyNum; // our current enemy + vec3_t ideal_viewangles, viewangles; + usercmd_t lastucmd; + int attackcrouch_time; + int bFlags; + + int deadSinkStartTime; + + int lastActivate; + + vec3_t loperLeapVel; + // ------------------------------------------------------------------------------------------- + // if working on a post release patch, new variables should ONLY be inserted after this point + // ------------------------------------------------------------------------------------------- + +} cast_state_t; +// +#define CSFOFS( x ) ( (int)&( ( (cast_state_t *)0 )->x ) ) +// +typedef struct aicast_predictmove_s +{ + vec3_t endpos; //position at the end of movement prediction + vec3_t velocity; //velocity at the end of movement prediction + int presencetype; //presence type at end of movement prediction + int stopevent; //event that made the prediction stop + float time; //time predicted ahead + int frames; //number of frames predicted ahead + int numtouch; + int touchents[MAXTOUCH]; + int groundEntityNum; +} aicast_predictmove_t; +// +// variables/globals +// +//cast states +extern cast_state_t *caststates; +//number of characters +extern int numcast; +// +// minimum time between thinks (maximum is double this) +extern int aicast_thinktime; +// maximum number of character thinks at once +extern int aicast_maxthink; +// maximum clients +extern int aicast_maxclients; +// skill scale +extern float aicast_skillscale; +// +// cvar to enable aicast debugging, set higher for more levels of debugging +extern vmCvar_t aicast_debug; +extern vmCvar_t aicast_debugname; +extern vmCvar_t aicast_scripts; +// +// +// procedure defines +// +// ai_cast.c +void AIChar_SetBBox( gentity_t *ent, cast_state_t *cs, qboolean useHeadTag ); +void AICast_Printf( int type, const char *fmt, ... ); +gentity_t *AICast_CreateCharacter( gentity_t *ent, float *attributes, cast_weapon_info_t *weaponInfo, char *castname, char *model, char *head, char *sex, char *color, char *handicap ); +void AICast_Init( void ); +void AICast_DelayedSpawnCast( gentity_t *ent, int castType ); +qboolean AICast_SolidsInBBox( vec3_t pos, vec3_t mins, vec3_t maxs, int entnum, int mask ); +void AICast_CheckLevelAttributes( cast_state_t *cs, gentity_t *ent, char **ppStr ); +// +// ai_cast_sight.c +void AICast_SightUpdate( int numchecks ); +qboolean AICast_VisibleFromPos( vec3_t srcpos, int srcnum, + vec3_t destpos, int destnum, qboolean updateVisPos ); +void AICast_UpdateVisibility( gentity_t *srcent, gentity_t *destent, qboolean shareVis, qboolean directview ); +qboolean AICast_CheckVisibility( gentity_t *srcent, gentity_t *destent ); +// +// ai_cast_debug.c +void AICast_DBG_InitAIFuncs( void ); +void AICast_DBG_AddAIFunc( cast_state_t *cs, char *funcname ); +void AICast_DBG_ListAIFuncs( cast_state_t *cs, int numprint ); +void AICast_DBG_RouteTable_f( vec3_t org, char *param ); +int Sys_MilliSeconds( void ); +void AICast_DebugFrame( cast_state_t *cs ); +// +// ai_cast_funcs.c +void AICast_SpecialFunc( cast_state_t *cs ); +bot_moveresult_t *AICast_MoveToPos( cast_state_t *cs, vec3_t pos, int entnum ); +float AICast_SpeedScaleForDistance( cast_state_t *cs, float startdist, float idealDist ); +char *AIFunc_DefaultStart( cast_state_t *cs ); +char *AIFunc_IdleStart( cast_state_t *cs ); +char *AIFunc_ChaseGoalIdleStart( cast_state_t *cs, int entitynum, float reachdist ); +char *AIFunc_ChaseGoalStart( cast_state_t *cs, int entitynum, float reachdist, qboolean slowApproach ); +char *AIFunc_BattleChaseStart( cast_state_t *cs ); +char *AIFunc_BattleStart( cast_state_t *cs ); +char *AIFunc_DoorMarkerStart( cast_state_t *cs, int doornum, int markernum ); +char *AIFunc_DoorMarker( cast_state_t *cs ); +char *AIFunc_BattleTakeCoverStart( cast_state_t *cs ); +char *AIFunc_GrenadeFlushStart( cast_state_t *cs ); +char *AIFunc_AvoidDangerStart( cast_state_t *cs ); +char *AIFunc_BattleMG42Start( cast_state_t *cs ); +char *AIFunc_InspectBodyStart( cast_state_t *cs ); +char *AIFunc_GrenadeKickStart( cast_state_t *cs ); +char *AIFunc_InspectFriendlyStart( cast_state_t *cs, int entnum ); +char *AIFunc_InspectBulletImpactStart( cast_state_t *cs ); +char *AIFunc_InspectAudibleEventStart( cast_state_t *cs, int entnum ); +char *AIFunc_BattleAmbushStart( cast_state_t *cs ); +char *AIFunc_BattleHuntStart( cast_state_t *cs ); +// +// ai_cast_func_attack.c +char *AIFunc_ZombieFlameAttackStart( cast_state_t *cs ); +char *AIFunc_ZombieAttack2Start( cast_state_t *cs ); +char *AIFunc_ZombieMeleeStart( cast_state_t *cs ); +char *AIFunc_LoperAttack1Start( cast_state_t *cs ); +char *AIFunc_LoperAttack2Start( cast_state_t *cs ); +char *AIFunc_LoperAttack3Start( cast_state_t *cs ); +char *AIFunc_StimSoldierAttack1Start( cast_state_t *cs ); +char *AIFunc_StimSoldierAttack2Start( cast_state_t *cs ); +char *AIFunc_BlackGuardAttack1Start( cast_state_t *cs ); +char *AIFunc_RejectAttack1Start( cast_state_t *cs ); //----(SA) +char *AIFunc_WarriorZombieMeleeStart( cast_state_t *cs ); +char *AIFunc_WarriorZombieSightStart( cast_state_t *cs ); +char *AIFunc_WarriorZombieDefenseStart( cast_state_t *cs ); +// +// ai_cast_func_boss1.c +char *AIFunc_Helga_SpiritAttack_Start( cast_state_t *cs ); +char *AIFunc_Helga_MeleeStart( cast_state_t *cs ); +char *AIFunc_FlameZombie_PortalStart( cast_state_t *cs ); +char *AIFunc_Heinrich_MeleeStart( cast_state_t *cs ); +char *AIFunc_Heinrich_RaiseDeadStart( cast_state_t *cs ); +char *AIFunc_Heinrich_SpawnSpiritsStart( cast_state_t *cs ); +void AICast_Heinrich_SoundPrecache( void ); +// +// ai_cast_fight.c +qboolean AICast_StateChange( cast_state_t *cs, aistateEnum_t newaistate ); +void AICast_WeaponSway( cast_state_t *cs, vec3_t ofs ); +int AICast_ScanForEnemies( cast_state_t *cs, int *enemies ); +void AICast_UpdateBattleInventory( cast_state_t *cs, int enemy ); +float AICast_Aggression( cast_state_t *cs ); +int AICast_WantsToChase( cast_state_t *cs ); +int AICast_WantsToTakeCover( cast_state_t *cs, qboolean attacking ); +qboolean AICast_EntityVisible( cast_state_t *cs, int enemynum, qboolean directview ); +bot_moveresult_t AICast_CombatMove( cast_state_t *cs, int tfl ); +qboolean AICast_AimAtEnemy( cast_state_t *cs ); +qboolean AICast_CheckAttackAtPos( int entnum, int enemy, vec3_t pos, qboolean ducking, qboolean allowHitWorld ); +qboolean AICast_CheckAttack( cast_state_t *cs, int enemy, qboolean allowHitWorld ); +void AICast_ProcessAttack( cast_state_t *cs ); +void AICast_ChooseWeapon( cast_state_t *cs, qboolean battleFunc ); +qboolean AICast_GetTakeCoverPos( cast_state_t *cs, int enemyNum, vec3_t enemyPos, vec3_t returnPos ); +qboolean AICast_CanMoveWhileFiringWeapon( int weaponnum ); +float AICast_GetWeaponSoundRange( int weapon ); +qboolean AICast_StopAndAttack( cast_state_t *cs ); +qboolean AICast_WantToRetreat( cast_state_t *cs ); +int AICast_SafeMissileFire( gentity_t *ent, int duration, int enemyNum, vec3_t enemyPos, int selfNum, vec3_t endPos ); +void AIChar_AttackSound( cast_state_t *cs ); +qboolean AICast_GotEnoughAmmoForWeapon( cast_state_t *cs, int weapon ); +qboolean AICast_HostileEnemy( cast_state_t *cs, int enemynum ); +qboolean AICast_QueryEnemy( cast_state_t *cs, int enemynum ); +void AICast_AudibleEvent( int srcnum, vec3_t pos, float range ); +qboolean AICast_WeaponUsable( cast_state_t *cs, int weaponNum ); +float AICast_WeaponRange( cast_state_t *cs, int weaponnum ); + +// +// ai_cast_events.c +void AICast_Pain( gentity_t *targ, gentity_t *attacker, int damage, vec3_t point ); +void AICast_Die( gentity_t *self, gentity_t *inflictor, gentity_t *attacker, int damage, int meansOfDeath ); +void AICast_Sight( gentity_t *ent, gentity_t *other, int lastSight ); +void AICast_EndChase( cast_state_t *cs ); +void AICast_ProcessActivate( int entNum, int activatorNum ); +// +// ai_cast_think.c +void AICast_Think( int client, float thinktime ); +void AICast_UpdateInput( cast_state_t *cs, int time ); +void AICast_InputToUserCommand( cast_state_t * cs, bot_input_t * bi, usercmd_t * ucmd, int delta_angles[3] ); +void AICast_PredictMovement( cast_state_t *cs, int numframes, float frametime, aicast_predictmove_t *move, usercmd_t *ucmd, int checkHitEnt ); +void AICast_Blocked( cast_state_t *cs, bot_moveresult_t *moveresult, int activate, bot_goal_t *goal ); +qboolean AICast_RequestCrouchAttack( cast_state_t *cs, vec3_t org, float time ); +qboolean AICast_GetAvoid( cast_state_t *cs, bot_goal_t *goal, vec3_t outpos, qboolean reverse, int blockEnt ); +void AICast_QueryThink( cast_state_t *cs ); +void AICast_DeadClipWalls( cast_state_t *cs ); +void AICast_IdleReload( cast_state_t *cs ); +// +// ai_cast_script.c +qboolean AICast_ScriptRun( cast_state_t *cs, qboolean force ); +// +// ai_cast_soldier.c +void AIChar_spawn( gentity_t *ent ); +// +// other/external defines +void BotCheckAir( bot_state_t *bs ); +void BotUpdateInput( bot_state_t *bs, int time ); +float AngleDifference( float ang1, float ang2 ); +float BotChangeViewAngle( float angle, float ideal_angle, float speed ); +void BotInputToUserCommand( bot_input_t * bi, usercmd_t * ucmd, int delta_angles[3] ); +void GibEntity( gentity_t *self, int killer ); +void GibHead( gentity_t *self, int killer ); +// +extern bot_state_t *botstates[MAX_CLIENTS]; diff --git a/src/game/ai_cast_characters.c b/src/game/ai_cast_characters.c new file mode 100644 index 0000000..ec414de --- /dev/null +++ b/src/game/ai_cast_characters.c @@ -0,0 +1,1822 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +/* + * name: ai_cast_characters.c + * + * desc: + * + * $Archive: /Wolf5/src/game/ai_cast_characters.c $ +*/ + +#include "g_local.h" +#include "../game/botlib.h" //bot lib interface +#include "../game/be_aas.h" +#include "../game/be_ea.h" +#include "../game/be_ai_gen.h" +#include "../game/be_ai_goal.h" +#include "../game/be_ai_move.h" +#include "../botai/botai.h" //bot ai interface + +#include "ai_cast.h" + +//--------------------------------------------------------------------------- +// Character specific attributes (defaults, these can be altered in the editor (TODO!)) +AICharacterDefaults_t aiDefaults[NUM_CHARACTERS] = { + + //AICHAR_NONE + {0}, + + //AICHAR_SOLDIER + { + "Soldier", + { + 220, // running speed + 90, // walking speed + 80, // crouching speed + 90, // Field of View + 200, // Yaw Speed // RF change + 0.0, // leader + 0.5, // aim skill + 0.5, // aim accuracy + 0.75, // attack skill + 0.5, // reaction time + 0.4, // attack crouch + 0.0, // idle crouch + 0.5, // aggression + 0.8, // tactical + 0.0, // camper + 16000, // alertness + 100, // starting health + 1.0, // hearing scale + 0.9, // not in pvs hearing scale + 512, // relaxed detection radius + 1.0, // pain threshold multiplier + }, + { + "infantrySightPlayer", + "infantryAttackPlayer", + "infantryOrders", + "infantryDeath", + "infantrySilentDeath", //----(SA) added + "infantryFlameDeath", //----(SA) added + "infantryPain", + "infantryStay", // stay - you're told to stay put + "infantryFollow", // follow - go with ordering player ("i'm with you" rather than "yes sir!") + "infantryOrdersDeny", // deny - refuse orders (doing something else) + }, + AITEAM_NAZI, // team + "infantryss/default", // default model/skin + {WP_MP40,WP_GRENADE_LAUNCHER}, // starting weapons + BBOX_SMALL, {32,48}, // bbox, crouch/stand height + AIFL_CATCH_GRENADE | AIFL_STAND_IDLE2, // flags + NULL, NULL, NULL, // special attack routine + NULL, // looping sound + AISTATE_RELAXED + }, + + //AICHAR_AMERICAN + { + "American", + { + 220, // running speed + 90, // walking speed + 80, // crouching speed + 90, // Field of View + 200, // Yaw Speed // RF change + 0.0, // leader + 0.70, // aim skill + 0.70, // aim accuracy + 0.75, // attack skill + 0.5, // reaction time + 0.3, // attack crouch + 0.0, // idle crouch + 0.5, // aggression + 0.8, // tactical + 0.0, // camper + 16000, // alertness + 100, // starting health + 1.0, // hearing scale + 0.9, // not in pvs hearing scale + 512, // relaxed detection radius + 1.0, // pain threshold multiplier + }, + { + "americanSightPlayer", + "americanAttackPlayer", + "americanOrders", + "americanDeath", + "americanSilentDeath", //----(SA) added + "americanFlameDeath", //----(SA) added + "americanPain", + "americanStay", // stay - you're told to stay put + "americanFollow", // follow - go with ordering player ("i'm with you" rather than "yes sir!") + "americanOrdersDeny", // deny - refuse orders (doing something else) + }, + AITEAM_ALLIES, + "american/default", + {WP_THOMPSON,WP_GRENADE_PINEAPPLE}, + BBOX_SMALL, {32,48}, + AIFL_CATCH_GRENADE | AIFL_STAND_IDLE2, + NULL, NULL, NULL, + NULL, + AISTATE_RELAXED + }, + + //AICHAR_ZOMBIE + { + "Zombie", + { + 200, // running speed //----(SA) DK requested change + 60, // walking speed //----(SA) DK requested change + 80, // crouching speed + 90, // Field of View + 350, // Yaw Speed + 0.0, // leader + 0.70, // aim skill + 0.70, // aim accuracy + 0.75, // attack skill + 0.1, // reaction time + 0.0, // attack crouch + 0.0, // idle crouch + 1.0, // aggression + 0.0, // tactical + 0.0, // camper + 16000, // alertness + 180, // starting health + 1.0, // hearing scale + 0.9, // not in pvs hearing scale + 512, // relaxed detection radius + 1.0, // pain threshold multiplier + }, + { + "zombieSightPlayer", + "zombieAttackPlayer", + "zombieOrders", + "zombieDeath", + "zombieSilentDeath", //----(SA) added + "zombieFlameDeath", //----(SA) added + "zombiePain", + "sound/weapons/melee/fstatck.wav", // stay - you're told to stay put + "sound/weapons/melee/fstmiss.wav", // follow - go with ordering player ("i'm with you" rather than "yes sir!") + "zombieOrdersDeny", // deny - refuse orders (doing something else) + }, + AITEAM_MONSTER, + "zombie/default", + { /*WP_GAUNTLET,*/ WP_MONSTER_ATTACK2, WP_MONSTER_ATTACK3}, + BBOX_SMALL, {32,48}, + /*AIFL_NOPAIN|AIFL_WALKFORWARD|*/ AIFL_NO_RELOAD, + AIFunc_ZombieFlameAttackStart, AIFunc_ZombieAttack2Start, AIFunc_ZombieMeleeStart, + NULL, + AISTATE_ALERT + }, + + //AICHAR_WARZOMBIE + { + "WarriorZombie", + { + 250, // running speed (SA) upped from 200->250 per Mike/DK + 60, // walking speed + 80, // crouching speed + 90, // Field of View + 350, // Yaw Speed + 0.0, // leader + 0.70, // aim skill + 0.70, // aim accuracy + 0.75, // attack skill + 0.1, // reaction time + 0.0, // attack crouch + 0.0, // idle crouch + 1.0, // aggression + 0.0, // tactical + 0.0, // camper + 16000, // alertness + 180, // starting health + 1.0, // hearing scale + 0.9, // not in pvs hearing scale + 512, // relaxed detection radius + 1.0, // pain threshold multiplier + }, + { + "warzombieSightPlayer", + "warzombieAttackPlayer", + "warzombieOrders", + "warzombieDeath", + "warzombieSilentDeath", //----(SA) added + "warzombieFlameDeath", //----(SA) added + "warzombiePain", +//----(SA) changed per DK +// "sound/weapons/melee/fstatck.wav", // stay - you're told to stay put + "sound/weapons/melee/warz_hit.wav", +// "sound/weapons/melee/fstmiss.wav", // follow - go with ordering player ("i'm with you" rather than "yes sir!") + "sound/weapons/melee/warz_miss.wav", + "warzombieOrdersDeny", // deny - refuse orders (doing something else) + }, + AITEAM_MONSTER, + "warrior/crypt2", + {WP_MONSTER_ATTACK1,WP_MONSTER_ATTACK2,WP_MONSTER_ATTACK3}, + BBOX_SMALL, {10,48}, // very low defense position + AIFL_NO_RELOAD, + AIFunc_WarriorZombieMeleeStart, /*AIFunc_WarriorZombieSightStart*/ NULL, AIFunc_WarriorZombieDefenseStart, + NULL, + AISTATE_ALERT + }, + + //AICHAR_VENOM + { + "Venom", + { + 110, // running speed + 100, // walking speed + 80, // crouching speed + 90, // Field of View + 200, // Yaw Speed + 0.0, // leader + 0.70, // aim skill + 0.70, // aim accuracy + 0.75, // attack skill + 0.5, // reaction time + 0.05, // attack crouch + 0.0, // idle crouch + 0.9, // aggression + 0.2, // tactical + 0.0, // camper + 16000, // alertness + 240, // starting health + 1.0, // hearing scale + 0.9, // not in pvs hearing scale + 512, // relaxed detection radius + 1.0, // pain threshold multiplier + }, + { + "venomSightPlayer", + "venomAttackPlayer", + "venomOrders", + "venomDeath", + "venomSilentDeath", //----(SA) added + "venomFlameDeath", //----(SA) added + "venomPain", + "venomStay", // stay - you're told to stay put + "venomFollow", // follow - go with ordering player ("i'm with you" rather than "yes sir!") + "venomOrdersDeny", // deny - refuse orders (doing something else) + }, + AITEAM_NAZI, + "venom/default", + {WP_FLAMETHROWER}, + BBOX_SMALL, {32,48}, + AIFL_NO_FLAME_DAMAGE | AIFL_WALKFORWARD | AIFL_NO_RELOAD, // |AIFL_NO_HEADSHOT_DMG, + NULL, NULL, NULL, + NULL, + AISTATE_RELAXED + }, + + //AICHAR_LOPER + { + "Loper", + { + 220, // running speed + 70, // walking speed + 220, // crouching speed + 90, // Field of View + 200, // Yaw Speed + 0.0, // leader + 0.70, // aim skill + 0.70, // aim accuracy + 0.75, // attack skill + 0.8, // reaction time + 0.05, // attack crouch + 0.0, // idle crouch + 1.0, // aggression + 0.1, // tactical + 0.0, // camper + 16000, // alertness + 500, // starting health + 1.0, // hearing scale + 0.9, // not in pvs hearing scale + 512, // relaxed detection radius + 1.0, // pain threshold multiplier + }, + { + "loperSightPlayer", + "loperAttackPlayer", + "loperOrders", + "loperDeath", + "loperSilentDeath", //----(SA) added + "loperFlameDeath", //----(SA) added + "loperPain", + "loperAttack2Start", // stay - you're told to stay put + "loperAttackStart", // follow - go with ordering player ("i'm with you" rather than "yes sir!") + "loperHit1", // deny - refuse orders (doing something else) + "loperHit2", // misc1 + }, + AITEAM_MONSTER, + "loper/default", + { /*WP_MONSTER_ATTACK1,*/ WP_MONSTER_ATTACK2,WP_MONSTER_ATTACK3}, + BBOX_LARGE, {32,32}, // large is for wide characters + AIFL_NO_RELOAD, + NULL /*AIFunc_LoperAttack1Start*/, AIFunc_LoperAttack2Start, AIFunc_LoperAttack3Start, + "sound/world/electloop.wav", + AISTATE_ALERT + }, + + //AICHAR_ELITEGUARD + { + "Elite Guard", + { + 230, // running speed + 90, // walking speed + 100, // crouching speed + 90, // Field of View + 200, // Yaw Speed // RF change + 0.0, // leader + 0.5, // aim skill + 1.0, // aim accuracy + 0.9, // attack skill + 0.3, // reaction time + 0.4, // attack crouch + 0.0, // idle crouch + 0.5, // aggression + 1.0, // tactical + 0.0, // camper + 16000, // alertness + 120, // starting health + 1.0, // hearing scale + 0.9, // not in pvs hearing scale + 512, // relaxed detection radius + 1.0, // pain threshold multiplier + }, + { + "eliteGuardSightPlayer", + "eliteGuardAttackPlayer", + "eliteGuardOrders", + "eliteGuardDeath", + "eliteGuardSilentDeath", //----(SA) added + "eliteGuardFlameDeath", //----(SA) added + "eliteGuardPain", + "eliteGuardStay", // stay - you're told to stay put + "eliteGuardFollow", // follow - go with ordering player ("i'm with you" rather than "yes sir!") + "eliteGuardOrdersDeny", // deny - refuse orders (doing something else) + }, + AITEAM_NAZI, + "eliteguard/default", + {WP_SILENCER}, //----(SA) TODO: replace w/ "silenced luger" + BBOX_SMALL, {32,48}, + AIFL_CATCH_GRENADE | AIFL_STAND_IDLE2, + NULL, NULL, NULL, + NULL, + AISTATE_RELAXED + }, + + //AICHAR_STIMSOLDIER1 + { + "Stim Soldier", + { + 170, // running speed + 100, // walking speed + 90, // crouching speed + 90, // Field of View + 150, // Yaw Speed + 0.0, // leader + 0.7, // aim skill + 1.0, // aim accuracy + 0.9, // attack skill + 0.6, // reaction time + 0.05, // attack crouch + 0.0, // idle crouch + 0.9, // aggression + 0.1, // tactical + 0.0, // camper + 16000, // alertness + 300, // starting health + 1.0, // hearing scale + 0.9, // not in pvs hearing scale + 512, // relaxed detection radius + 1.0, // pain threshold multiplier + }, + { + "stimSoldierSightPlayer", + "stimSoldierAttackPlayer", + "stimSoldierOrders", + "stimSoldierDeath", + "stimSoldierSilentDeath", //----(SA) added + "stimSoldeirFlameDeath", //----(SA) added + "stimSoldierPain", + "stimSoldierStay", // stay - you're told to stay put + "stimSoldierFollow", // follow - go with ordering player ("i'm with you" rather than "yes sir!") + "stimSoldierOrdersDeny", // deny - refuse orders (doing something else) + }, + AITEAM_NAZI, + "stim/default", + {WP_MONSTER_ATTACK2}, // TODO: dual machinegun attack + BBOX_LARGE, {48,64}, + AIFL_NO_RELOAD, + NULL, AIFunc_StimSoldierAttack2Start, NULL, + NULL, + AISTATE_ALERT + }, + + //AICHAR_STIMSOLDIER2 + { + "Stim Soldier", + { + 170, // running speed + 100, // walking speed + 90, // crouching speed + 90, // Field of View + 150, // Yaw Speed + 0.0, // leader + 0.7, // aim skill + 1.0, // aim accuracy + 0.9, // attack skill + 0.6, // reaction time + 0.05, // attack crouch + 0.0, // idle crouch + 0.9, // aggression + 0.1, // tactical + 0.0, // camper + 16000, // alertness + 300, // starting health + 1.0, // hearing scale + 0.9, // not in pvs hearing scale + 512, // relaxed detection radius + 1.0, // pain threshold multiplier + }, + { + "stimSoldierSightPlayer", + "stimSoldierAttackPlayer", + "stimSoldierOrders", + "stimSoldierDeath", + "stimSoldierSilentDeath", //----(SA) added + "stimSoldierFlameDeath", //----(SA) added + "stimSoldierPain", + "stimSoldierStay", // stay - you're told to stay put + "stimSoldierFollow", // follow - go with ordering player ("i'm with you" rather than "yes sir!") + "stimSoldierOrdersDeny", // deny - refuse orders (doing something else) + }, + AITEAM_NAZI, + "stim/default", + {WP_MP40, WP_MONSTER_ATTACK1}, // attack1 is leaping rocket attack + BBOX_LARGE, {48,64}, + AIFL_NO_RELOAD, + AIFunc_StimSoldierAttack1Start, NULL, NULL, + NULL, + AISTATE_ALERT + }, + + //AICHAR_STIMSOLDIER3 + { + "Stim Soldier", + { + 170, // running speed + 100, // walking speed + 90, // crouching speed + 90, // Field of View + 150, // Yaw Speed + 0.0, // leader + 0.7, // aim skill + 1.0, // aim accuracy + 0.9, // attack skill + 0.6, // reaction time + 0.05, // attack crouch + 0.0, // idle crouch + 0.9, // aggression + 0.1, // tactical + 0.0, // camper + 16000, // alertness + 300, // starting health + 1.0, // hearing scale + 0.9, // not in pvs hearing scale + 512, // relaxed detection radius + 1.0, // pain threshold multiplier + }, + { + "stimSoldierSightPlayer", + "stimSoldierAttackPlayer", + "stimSoldierOrders", + "stimSoldierDeath", + "stimSoldierSilentDeath", //----(SA) added + "stimSoldierFlameDeath", //----(SA) added + "stimSoldierPain", + "stimSoldierStay", // stay - you're told to stay put + "stimSoldierFollow", // follow - go with ordering player ("i'm with you" rather than "yes sir!") + "stimSoldierOrdersDeny", // deny - refuse orders (doing something else) + }, + AITEAM_NAZI, + "stim/default", + {WP_MP40, WP_TESLA}, // no monster_attack1, since that's only used for the jumping rocket attack + BBOX_LARGE, {48,64}, + AIFL_NO_RELOAD, + AIFunc_StimSoldierAttack1Start, NULL, NULL, + NULL, + AISTATE_ALERT + }, + + //AICHAR_SUPERSOLDIER + { + "Super Soldier", + { + 170, // running speed + 100, // walking speed + 90, // crouching speed + 90, // Field of View + 150, // Yaw Speed + 0.0, // leader + 0.7, // aim skill + 1.0, // aim accuracy + 0.9, // attack skill + 0.6, // reaction time + 0.05, // attack crouch + 0.0, // idle crouch + 1.0, // aggression + 0.0, // tactical + 0.0, // camper + 16000, // alertness + 300, // starting health + 1.0, // hearing scale + 0.9, // not in pvs hearing scale + 512, // relaxed detection radius + 2.0, // pain threshold multiplier + }, + { + "superSoldierSightPlayer", + "superSoldierAttackPlayer", + "superSoldierOrders", + "superSoldierDeath", + "superSoldierSilentDeath", //----(SA) added + "superSoldierFlameDeath", //----(SA) added + "superSoldierPain", + "superSoldierStay", // stay - you're told to stay put + "superSoldierFollow", // follow - go with ordering player ("i'm with you" rather than "yes sir!") + "superSoldierOrdersDeny", // deny - refuse orders (doing something else) + }, + AITEAM_NAZI, + "supersoldier/default", + {WP_VENOM}, + BBOX_LARGE, {48,64}, + AIFL_NO_RELOAD | AIFL_NO_FLAME_DAMAGE | AIFL_NO_TESLA_DAMAGE, + NULL, NULL, NULL, + NULL, + AISTATE_ALERT + }, + + //AICHAR_BLACKGUARD + { + "Black Guard", + { + 220, // running speed + 90, // walking speed + 100, // crouching speed + 90, // Field of View + 300, // Yaw Speed + 0.0, // leader + 0.5, // aim skill + 0.8, // aim accuracy + 0.9, // attack skill + 0.3, // reaction time + 0.4, // attack crouch + 0.0, // idle crouch + 0.5, // aggression + 1.0, // tactical + 0.0, // camper + 16000, // alertness + 120, // starting health + 1.0, // hearing scale + 0.9, // not in pvs hearing scale + 512, // relaxed detection radius + 1.0, // pain threshold multiplier + }, + { + "blackGuardSightPlayer", + "blackGuardAttackPlayer", + "blackGuardOrders", + "blackGuardDeath", + "blackGuardSilentDeath", //----(SA) added + "blackGuardFlameDeath", //----(SA) added + "blackGuardPain", + "blackGuardStay", // stay - you're told to stay put + "blackGuardFollow", // follow - go with ordering player ("i'm with you" rather than "yes sir!") + "blackGuardOrdersDeny", // deny - refuse orders (doing something else) + }, + AITEAM_NAZI, + "blackguard/default", +// {WP_MP40, WP_GRENADE_LAUNCHER, WP_MONSTER_ATTACK1}, // attack1 is melee kick + {WP_FG42, WP_FG42SCOPE, WP_GRENADE_LAUNCHER, WP_MONSTER_ATTACK1}, // attack1 is melee kick + BBOX_SMALL, {32,48}, + AIFL_CATCH_GRENADE | AIFL_FLIP_ANIM | AIFL_STAND_IDLE2, + AIFunc_BlackGuardAttack1Start, NULL, NULL, + NULL, + AISTATE_RELAXED + }, + + //AICHAR_PROTOSOLDIER + { + "Protosoldier", + { + 170, // running speed + 100, // walking speed + 90, // crouching speed + 90, // Field of View + 230, // Yaw Speed + 0.0, // leader + 0.7, // aim skill + 1.0, // aim accuracy + 0.9, // attack skill + 0.2, // reaction time + 0.05, // attack crouch + 0.0, // idle crouch + 0.9, // aggression + 0.1, // tactical + 0.0, // camper + 16000, // alertness + 300, // starting health + 1.0, // hearing scale + 0.9, // not in pvs hearing scale + 512, // relaxed detection radius + 2.0, // pain threshold multiplier + }, + { + "protoSoldierSightPlayer", + "protoSoldierAttackPlayer", + "protoSoldierOrders", + "protoSoldierDeath", + "protoSoldierSilentDeath", //----(SA) added + "protoSoldierFlameDeath", //----(SA) added + "protoSoldierPain", + "protoSoldierStay", // stay - you're told to stay put + "protoSoldierFollow", // follow - go with ordering player ("i'm with you" rather than "yes sir!") + "protoSoldierOrdersDeny", // deny - refuse orders (doing something else) + }, + AITEAM_NAZI, + "protosoldier/default", + {WP_VENOM}, + BBOX_LARGE, {48,64}, + AIFL_NO_TESLA_DAMAGE | AIFL_NO_FLAME_DAMAGE | AIFL_WALKFORWARD | AIFL_NO_RELOAD, + NULL, NULL, NULL, + NULL, + AISTATE_ALERT + }, + + // AICHAR_FROGMAN + { + "Frogman", + { + 170, // running speed + 100, // walking speed + 90, // crouching speed + 90, // Field of View + 150, // Yaw Speed + 0.0, // leader + 0.7, // aim skill + 1.0, // aim accuracy + 0.9, // attack skill + 0.6, // reaction time + 0.05, // attack crouch + 0.0, // idle crouch + 0.9, // aggression + 0.1, // tactical + 0.0, // camper + 16000, // alertness + 200, // starting health + 1.0, // hearing scale + 0.9, // not in pvs hearing scale + 512, // relaxed detection radius + 1.0, // pain threshold multiplier + }, + { + "frogmanSightPlayer", + "frogmanAttackPlayer", + "frogmanOrders", + "frogmanDeath", + "frogmanSilentDeath", //----(SA) added + "frogmanFlameDeath", //----(SA) added + "frogmanPain", + "frogmanStay", // stay - you're told to stay put + "frogmanFollow", // follow - go with ordering player ("i'm with you" rather than "yes sir!") + "frogmanOrdersDeny", // deny - refuse orders (doing something else) + }, + AITEAM_NAZI, + "frogman/default", + {0}, + BBOX_SMALL, {32,48}, // bbox, crouch/stand height + 0, + NULL, NULL, NULL, + NULL, + AISTATE_RELAXED + }, + + //AICHAR_HELGA + { + "Helga", + { + 140, // running speed + 90, // walking speed + 80, // crouching speed + 90, // Field of View + 200, // Yaw Speed + 0.0, // leader + 0.5, // aim skill + 0.5, // aim accuracy + 0.75, // attack skill + 0.5, // reaction time + 0.0, // attack crouch + 0.0, // idle crouch + 1.0, // aggression + 0.0, // tactical + 0.0, // camper + 16000, // alertness + 100, // starting health + 1.0, // hearing scale + 0.9, // not in pvs hearing scale + 512, // relaxed detection radius + 3.0, // pain threshold multiplier + }, + { + "helgaAttackPlayer", + "helgaAttackPlayer", + "helgaOrders", + "helgaDeath", + "helgaSilentDeath", //----(SA) added + "helgaFlameDeath", //----(SA) added + "helgaAttackPlayer", + "sound/weapons/melee/fstatck.wav", // stay - you're told to stay put + "helgaFollow", // follow - go with ordering player ("i'm with you" rather than "yes sir!") + "helgaOrdersDeny", // deny - refuse orders (doing something else) + }, + AITEAM_MONSTER, // team + "beast/default", // default model/skin + {WP_MONSTER_ATTACK1,WP_MONSTER_ATTACK2 /*,WP_MONSTER_ATTACK3*/}, // starting weapons + BBOX_LARGE, {90,90}, // bbox, crouch/stand height + AIFL_WALKFORWARD | AIFL_NO_RELOAD, + AIFunc_Helga_MeleeStart, AIFunc_Helga_SpiritAttack_Start, NULL, // special attack routine + NULL, + AISTATE_ALERT + }, + + //AICHAR_HEINRICH + { + "Heinrich", + { + 170, // running speed + 100, // walking speed + 90, // crouching speed + 90, // Field of View + 130, // Yaw Speed + 0.0, // leader + 0.7, // aim skill + 1.0, // aim accuracy + 0.9, // attack skill + 0.2, // reaction time + 0.05, // attack crouch + 0.0, // idle crouch + 1.0, // aggression + 0.0, // tactical + 0.0, // camper + 16000, // alertness + 2000, // starting health + 1.0, // hearing scale + 0.9, // not in pvs hearing scale + 512, // relaxed detection radius + 5.0, // pain threshold multiplier + }, + { + "heinrichSightPlayer", + "heinrichAttackPlayer", + "heinrichOrders", + "heinrichDeath", + "heinrichSilentDeath", + "heinrichFlameDeath", //----(SA) added + "heinrichPain", + "heinrichStay", // stay - you're told to stay put + "heinrichFollow", // follow - go with ordering player ("i'm with you" rather than "yes sir!") + "heinrichStomp", // deny - refuse orders (doing something else) + }, + AITEAM_NAZI, + "heinrich/default", + {WP_MONSTER_ATTACK1,WP_MONSTER_ATTACK2,WP_MONSTER_ATTACK3}, // attack3 is given to him by scripting + BBOX_LARGE, {72,72}, // (SA) height is not exact. just eyeballed. + AIFL_NO_FLAME_DAMAGE | AIFL_WALKFORWARD | AIFL_NO_RELOAD, + AIFunc_Heinrich_MeleeStart, AIFunc_Heinrich_RaiseDeadStart, AIFunc_Heinrich_SpawnSpiritsStart, + NULL, + AISTATE_ALERT + }, + + //AICHAR_PARTISAN + { + "Partisan", + { + 220, // running speed + 90, // walking speed + 80, // crouching speed + 90, // Field of View + 300, // Yaw Speed + 0.0, // leader + 0.70, // aim skill + 0.70, // aim accuracy + 0.75, // attack skill + 0.5, // reaction time + 0.3, // attack crouch + 0.0, // idle crouch + 0.5, // aggression + 0.8, // tactical + 0.0, // camper + 16000, // alertness + 100, // starting health + 1.0, // hearing scale + 0.9, // not in pvs hearing scale + 512, // relaxed detection radius + 1.0, // pain threshold multiplier + }, + { + "partisanSightPlayer", + "partisanAttackPlayer", + "partisanOrders", + "partisanDeath", + "partisanSilentDeath", //----(SA) added + "partisanFlameDeath", //----(SA) added + "partisanPain", + "partisanStay", + "partisanFollow", + "partisanOrdersDeny", + }, + AITEAM_ALLIES, //----(SA) changed affiliation for DK + "partisan/default", + {WP_THOMPSON}, + BBOX_SMALL, {32,48}, + AIFL_CATCH_GRENADE | AIFL_STAND_IDLE2, + NULL, NULL, NULL, + NULL, + AISTATE_RELAXED + }, + + //AICHAR_CIVILIAN + { + "Civilian", + { + 220, // running speed + 90, // walking speed + 80, // crouching speed + 90, // Field of View + 300, // Yaw Speed + 0.0, // leader + 0.70, // aim skill + 0.70, // aim accuracy + 0.75, // attack skill + 0.5, // reaction time + 0.3, // attack crouch + 0.0, // idle crouch + 0.5, // aggression + 0.8, // tactical + 0.0, // camper + 16000, // alertness + 100, // starting health + 1.0, // hearing scale + 0.9, // not in pvs hearing scale + 512, // relaxed detection radius + 1.0, // pain threshold multiplier + }, + { + "civilianSightPlayer", + "civilianAttackPlayer", + "civilianOrders", + "civilianDeath", + "civilianSilentDeath", //----(SA) added + "civilianFlameDeath", //----(SA) added + "civilianPain", + "civilianStay", + "civilianFollow", + "civilianOrdersDeny", + }, + AITEAM_NEUTRAL, //----(SA) changed affiliation for DK + "civilian/default", + {0}, + BBOX_SMALL, {32,48}, + AIFL_CATCH_GRENADE | AIFL_STAND_IDLE2, + NULL, NULL, NULL, + NULL, + AISTATE_RELAXED + }, + +}; +//--------------------------------------------------------------------------- + +//--------------------------------------------------------------------------- +// Bounding boxes +static vec3_t bbmins[2] = {{-18, -18, -24},{-32,-32,-24}}; +static vec3_t bbmaxs[2] = {{ 18, 18, 48},{ 32, 32, 68}}; +//static float crouchMaxZ[2] = {32,48}; // same as player, will head be ok? // TTimo: unused +//--------------------------------------------------------------------------- + +//--------------------------------------------------------------------------- +// Weapon info +cast_weapon_info_t weaponInfo; +//--------------------------------------------------------------------------- + +/* +============ +AIChar_SetBBox + + FIXME: pass a maxZ into this so we can tailor the height for each character, + since height isn't important for the AAS routing (whereas width is very important) +============ +*/ +void AIChar_SetBBox( gentity_t *ent, cast_state_t *cs, qboolean useHeadTag ) { + vec3_t bbox[2]; + trace_t tr; + orientation_t or; + + if ( !useHeadTag ) { + VectorCopy( bbmins[cs->aasWorldIndex], ent->client->ps.mins ); + VectorCopy( bbmaxs[cs->aasWorldIndex], ent->client->ps.maxs ); + ent->client->ps.maxs[2] = aiDefaults[cs->aiCharacter].crouchstandZ[1]; + VectorCopy( ent->client->ps.mins, ent->r.mins ); + VectorCopy( ent->client->ps.maxs, ent->r.maxs ); + ent->client->ps.crouchMaxZ = aiDefaults[cs->aiCharacter].crouchstandZ[0]; + ent->s.density = cs->aasWorldIndex; + } else if ( trap_GetTag( ent->s.number, "tag_head", &or ) ) { // if not found, then just leave it + or.origin[2] -= ent->client->ps.origin[2]; // convert to local coordinates + or.origin[2] += 11; + if ( or.origin[2] < 0 ) { + or.origin[2] = 0; + } + if ( or.origin[2] > aiDefaults[cs->aiCharacter].crouchstandZ[1] + 30 ) { + or.origin[2] = aiDefaults[cs->aiCharacter].crouchstandZ[1] + 30; + } + + memset( &tr, 0, sizeof( tr ) ); + + // check that the new height is ok first, otherwise leave it alone + VectorCopy( bbmins[cs->aasWorldIndex], bbox[0] ); + VectorCopy( bbmaxs[cs->aasWorldIndex], bbox[1] ); + // set the head tag height + bbox[1][2] = or.origin[2]; + + if ( bbox[1][2] > ent->client->ps.maxs[2] ) { + // check this area is clear + trap_TraceCapsule( &tr, ent->client->ps.origin, bbox[0], bbox[1], ent->client->ps.origin, ent->s.number, ent->clipmask ); + } + + if ( !tr.startsolid && !tr.allsolid ) { + VectorCopy( bbox[0], ent->client->ps.mins ); + VectorCopy( bbox[1], ent->client->ps.maxs ); + VectorCopy( ent->client->ps.mins, ent->r.mins ); + VectorCopy( ent->client->ps.maxs, ent->r.maxs ); + ent->client->ps.crouchMaxZ = aiDefaults[cs->aiCharacter].crouchstandZ[0]; + ent->s.density = cs->aasWorldIndex; + } + } + + // if they are linked, then relink to update bbox + if ( ent->r.linked ) { + trap_LinkEntity( ent ); + } +} + +/* +============ +AIChar_Death +============ +*/ +void AIChar_Death( gentity_t *ent, gentity_t *attacker, int damage, int mod ) { //----(SA) added mod + // need this check otherwise sound will overwrite gib message + if ( ent->health > GIB_HEALTH ) { + if ( ent->client->ps.eFlags & EF_HEADSHOT ) { + G_AddEvent( ent, EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[ent->aiCharacter].soundScripts[QUIETDEATHSOUNDSCRIPT] ) ); + } else { + switch ( mod ) { //----(SA) modified to add 'quiet' deaths + case MOD_KNIFE_STEALTH: + case MOD_SNIPERRIFLE: + case MOD_SNOOPERSCOPE: + G_AddEvent( ent, EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[ent->aiCharacter].soundScripts[QUIETDEATHSOUNDSCRIPT] ) ); + break; + case MOD_FLAMETHROWER: + G_AddEvent( ent, EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[ent->aiCharacter].soundScripts[FLAMEDEATHSOUNDSCRIPT] ) ); //----(SA) added + break; + default: + G_AddEvent( ent, EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[ent->aiCharacter].soundScripts[DEATHSOUNDSCRIPT] ) ); + break; + } + } + } +} + +/* +============= +AIChar_GetPainLocation +============= +*/ +int AIChar_GetPainLocation( gentity_t *ent, vec3_t point ) { + static char *painTagNames[] = { + "tag_head", + "tag_chest", + "tag_torso", + "tag_groin", + "tag_armright", + "tag_armleft", + "tag_legright", + "tag_legleft", + NULL, + }; + + int tagIndex, bestTag; + float bestDist, dist; + orientation_t or; + + // first make sure the client is able to retrieve tag information + if ( !trap_GetTag( ent->s.number, painTagNames[0], &or ) ) { + return 0; + } + + // find a correct animation to play, based on the body orientation at previous frame + for ( tagIndex = 0, bestDist = 0, bestTag = -1; painTagNames[tagIndex]; tagIndex++ ) { + // grab the tag with this name + if ( trap_GetTag( ent->s.number, painTagNames[tagIndex], &or ) ) { + dist = VectorDistance( or.origin, point ); + if ( !bestDist || dist < bestDist ) { + bestTag = tagIndex; + bestDist = dist; + } + } + } + + if ( bestTag >= 0 ) { + return bestTag + 1; + } + + return 0; +} + +/* +============ +AIChar_Pain +============ +*/ +void AIChar_Pain( gentity_t *ent, gentity_t *attacker, int damage, vec3_t point ) { + #define PAIN_THRESHOLD 25 + #define STUNNED_THRESHOLD 30 + cast_state_t *cs; + float dist; + qboolean forceStun = qfalse; + float painThreshold, stunnedThreshold; + + cs = AICast_GetCastState( ent->s.number ); + + if ( g_testPain.integer == 1 ) { + ent->health = ent->client->pers.maxHealth; // debugging + } + + if ( g_testPain.integer != 2 ) { + if ( level.time < cs->painSoundTime ) { + return; + } + } + + painThreshold = PAIN_THRESHOLD * cs->attributes[PAIN_THRESHOLD_SCALE]; + stunnedThreshold = STUNNED_THRESHOLD * cs->attributes[PAIN_THRESHOLD_SCALE]; + + // if they are already playing another animation, we might get confused and cut it off, so don't play a pain + if ( ent->client->ps.torsoTimer || ent->client->ps.legsTimer ) { + return; + } + + // if we are waiting for our weapon to fire (throwing a grenade) + if ( ent->client->ps.weaponDelay ) { + return; + } + + if ( attacker->s.weapon == WP_FLAMETHROWER && !( cs->aiFlags & AIFL_NO_FLAME_DAMAGE ) ) { // flames should be recognized more often, since they stay onfire until they're dead anyway + painThreshold = 1; + stunnedThreshold = 99999; // dont be stunned + } + + // HACK: if the attacker is using the flamethrower, don't do any special pain anim or sound + // FIXME: we should pass in the MOD here, since they could have fired a grenade, then switched weapons + //if (attacker->s.weapon == WP_FLAMETHROWER) { + // return; + //} + + if ( !Q_stricmp( attacker->classname, "props_statue" ) ) { + damage = 99999; // try and force a stun + forceStun = qtrue; + } + + if ( attacker->s.weapon == WP_TESLA ) { + damage *= 2; + if ( cs->attributes[PAIN_THRESHOLD_SCALE] <= 1.0 ) { + damage = 99999; + } + } + + // now check the damageQuota to see if we should play a pain animation + // first reduce the current damageQuota with time + if ( cs->damageQuotaTime && cs->damageQuota > 0 ) { + cs->damageQuota -= (int)( ( 1.0 + ( g_gameskill.value / GSKILL_MAX ) ) * ( (float)( level.time - cs->damageQuotaTime ) / 1000 ) * ( 7.5 + cs->attributes[ATTACK_SKILL] * 10.0 ) ); + if ( cs->damageQuota < 0 ) { + cs->damageQuota = 0; + } + } + + // if it's been a long time since our last pain, scale it up + if ( cs->painSoundTime < level.time - 1000 ) { + float scale; + scale = (float)( level.time - cs->painSoundTime - 1000 ) / 1000.0; + if ( scale > 4.0 ) { + scale = 4.0; + } + damage = (int)( (float)damage * ( 1.0 + ( scale * ( 1.0 - 0.5 * g_gameskill.value / GSKILL_MAX ) ) ) ); + } + + // adjust the new damage with distance, if they are really close, scale it down, to make it + // harder to get through the game by continually rushing the enemies + if ( ( attacker->s.weapon != WP_TESLA ) && ( ( dist = VectorDistance( ent->r.currentOrigin, attacker->r.currentAngles ) ) < 384 ) ) { + damage -= (int)( (float)damage * ( 1.0 - ( dist / 384.0 ) ) * ( 0.5 + 0.5 * g_gameskill.value / GSKILL_MAX ) ); + } + + // add the new damage + cs->damageQuota += damage; + cs->damageQuotaTime = level.time; + + if ( forceStun ) { + damage = 99999; // try and force a stun + cs->damageQuota = painThreshold + 1; + } + + // if it's over the threshold, play a pain + + // don't do this if crouching, or we might clip through the world + + if ( g_testPain.integer == 2 || ( cs->damageQuota > painThreshold ) ) { + int delay; + + // stunned? + if ( damage > stunnedThreshold && ( forceStun || ( rand() % 2 ) ) ) { // stunned + BG_UpdateConditionValue( ent->s.number, ANIM_COND_STUNNED, qtrue, qfalse ); + } + // enemy weapon + if ( attacker->client ) { + BG_UpdateConditionValue( ent->s.number, ANIM_COND_ENEMY_WEAPON, attacker->s.weapon, qtrue ); + } + if ( point ) { + // location + BG_UpdateConditionValue( ent->s.number, ANIM_COND_IMPACT_POINT, AIChar_GetPainLocation( ent, point ), qtrue ); + } else { + BG_UpdateConditionValue( ent->s.number, ANIM_COND_IMPACT_POINT, 0, qfalse ); + } + + // pause while we play a pain + delay = BG_AnimScriptEvent( &ent->client->ps, ANIM_ET_PAIN, qfalse, qtrue ); + + // turn off temporary conditions + BG_UpdateConditionValue( ent->s.number, ANIM_COND_STUNNED, 0, qfalse ); + BG_UpdateConditionValue( ent->s.number, ANIM_COND_ENEMY_WEAPON, 0, qfalse ); + BG_UpdateConditionValue( ent->s.number, ANIM_COND_IMPACT_POINT, 0, qfalse ); + + if ( delay >= 0 ) { + // setup game stuff to handle the character movements, etc + cs->pauseTime = level.time + delay + 250; + cs->lockViewAnglesTime = cs->pauseTime; + // make sure we stop crouching + cs->attackcrouch_time = 0; + // don't fire while in pain? + cs->triggerReleaseTime = cs->pauseTime; + // stay crouching if we were before the pain + if ( cs->bs->cur_ps.viewheight == cs->bs->cur_ps.crouchViewHeight ) { + cs->attackcrouch_time = level.time + (float)( cs->pauseTime - level.time ) + 500; + } + } + + // if we didnt just play a scripted sound, then play one of the default sounds + if ( cs->lastScriptSound < level.time ) { + G_AddEvent( ent, EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[ent->aiCharacter].soundScripts[PAINSOUNDSCRIPT] ) ); + } + + // reset the quota + cs->damageQuota = 0; + cs->damageQuotaTime = 0; + // + cs->painSoundTime = cs->pauseTime + (int)( 1000 * ( g_gameskill.value / GSKILL_MAX ) ); // add a bit more of a buffer before the next one + } + +} + +/* +============ +AIChar_Sight +============ +*/ +void AIChar_Sight( gentity_t *ent, gentity_t *other, int lastSight ) { + cast_state_t *cs; + + cs = AICast_GetCastState( ent->s.number ); + + // if we are in noattack mode, don't make sounds + if ( cs->castScriptStatus.scriptNoAttackTime >= level.time ) { + return; + } + if ( cs->noAttackTime >= level.time ) { + return; + } + + // if they have recently played a script sound, then ignore this + if ( cs->lastScriptSound > level.time - 4000 ) { + return; + } + + if ( !AICast_SameTeam( cs, other->s.number ) ) { + if ( !cs->firstSightTime || cs->firstSightTime < ( level.time - 15000 ) ) { + //G_AddEvent( ent, EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[ent->aiCharacter].sightSoundScript ) ); + } + cs->firstSightTime = level.time; + } + +} + +/* +===================== +AIChar_AttackSND + + NOTE: this should just lookup a sound script for this character/weapon combo +===================== +*/ +void AIChar_AttackSound( cast_state_t *cs ) { + + gentity_t *ent; + + ent = &g_entities [cs->entityNum]; + + if ( cs->attackSNDtime > level.time ) { + return; + } + + // if we are in noattack mode, don't make sounds + if ( cs->castScriptStatus.scriptNoAttackTime >= level.time ) { + return; + } + if ( cs->noAttackTime >= level.time ) { + return; + } + + // Ridah, only yell when throwing grenades every now and then, since it's not very "stealthy" + if ( cs->weaponNum == WP_GRENADE_LAUNCHER && rand() % 5 ) { + return; + } + + cs->attackSNDtime = level.time + 5000 + ( 1000 * rand() % 10 ); + + AICast_ScriptEvent( cs, "attacksound", ent->aiName ); + if ( cs->aiFlags & AIFL_DENYACTION ) { + return; + } + + if ( cs->weaponNum == WP_LUGER ) { + G_AddEvent( ent, EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[ent->aiCharacter].soundScripts[ORDERSSOUNDSCRIPT] ) ); + } else { + G_AddEvent( ent, EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[ent->aiCharacter].soundScripts[ATTACKSOUNDSCRIPT] ) ); + } + +} + +/* +============ +AIChar_spawn +============ +*/ +void AIChar_spawn( gentity_t *ent ) { + gentity_t *newent; + cast_state_t *cs; + AICharacterDefaults_t *aiCharDefaults; + int i; + static int lastCall; + static int numCalls; + + // if there are other cast's waiting to spawn before us, wait for them + for ( i = MAX_CLIENTS, newent = &g_entities[MAX_CLIENTS]; i < MAX_GENTITIES; i++, newent++ ) { + if ( !newent->inuse ) { + continue; + } + if ( newent->think != AIChar_spawn ) { + continue; + } + if ( newent == ent ) { + break; // we are the first in line + } + // still waiting for someone else + ent->nextthink = level.time + FRAMETIME; + return; + } + + // if the client hasn't connected yet, wait around + if ( !AICast_FindEntityForName( "player" ) ) { + ent->nextthink = level.time + FRAMETIME; + return; + } + + if ( lastCall == level.time ) { + if ( numCalls++ > 2 ) { + ent->nextthink = level.time + FRAMETIME; + return; // spawned enough this frame already + } + } else { + numCalls = 0; + } + lastCall = level.time; + + aiCharDefaults = &aiDefaults[ent->aiCharacter]; + + // ............................ + // setup weapon info + // + // starting weapons/ammo + memset( &weaponInfo, 0, sizeof( weaponInfo ) ); + for ( i = 0; aiCharDefaults->weapons[i]; i++ ) { + //weaponInfo.startingWeapons[(aiCharDefaults->weapons[i] / 32)] |= ( 1 << aiCharDefaults->weapons[i] ); + //weaponInfo.startingWeapons[0] |= ( 1 << aiCharDefaults->weapons[i] ); + + COM_BitSet( weaponInfo.startingWeapons, aiCharDefaults->weapons[i] ); + if ( aiCharDefaults->weapons[i] == WP_GRENADE_LAUNCHER ) { // give them a bunch of grenades, but not an unlimited supply + weaponInfo.startingAmmo[BG_FindAmmoForWeapon( aiCharDefaults->weapons[i] )] = 6; + } else { + weaponInfo.startingAmmo[BG_FindAmmoForWeapon( aiCharDefaults->weapons[i] )] = 999; + } + } + // + // use the default skin if nothing specified + if ( !ent->aiSkin || !strlen( ent->aiSkin ) ) { + ent->aiSkin = aiCharDefaults->skin; + } + // ............................ + // + // create the character + + // (there will always be an ent->aiSkin (SA)) + newent = AICast_CreateCharacter( ent, aiCharDefaults->attributes, &weaponInfo, aiCharDefaults->name, ent->aiSkin, ent->aihSkin, "m", "7", "100" ); + + if ( !newent ) { + G_FreeEntity( ent ); + return; + } + // copy any character-specific information to the new entity (like editor fields, etc) + // + // copy this across so killing ai can trigger a target + newent->target = ent->target; + // + newent->classname = ent->classname; + newent->r.svFlags |= ( ent->r.svFlags & SVF_NOFOOTSTEPS ); + newent->aiCharacter = ent->aiCharacter; + newent->client->ps.aiChar = ent->aiCharacter; + newent->spawnflags = ent->spawnflags; + newent->aiTeam = ent->aiTeam; + if ( newent->aiTeam < 0 ) { + newent->aiTeam = aiCharDefaults->aiTeam; + } + newent->client->ps.teamNum = newent->aiTeam; + // + // kill the old entity + G_FreeEntity( ent ); + // attach to the new entity + ent = newent; + // + // precache any specific sounds + // + // ... + // + // get the cast state + cs = AICast_GetCastState( ent->s.number ); + // + // setup any character specific cast_state variables + cs->deathfunc = AIChar_Death; + cs->painfunc = AIChar_Pain; + cs->aiFlags |= aiCharDefaults->aiFlags; + cs->aiState = aiCharDefaults->aiState; + // + cs->queryCountValidTime = -1; + // + // randomly choose idle animation + if ( cs->aiFlags & AIFL_STAND_IDLE2 ) { + newent->client->ps.eFlags |= EF_STAND_IDLE2; + } + // + // attach any event specific functions (pain, death, etc) + // + //cs->getDeathAnim = AIChar_getDeathAnim; + cs->sightfunc = AIChar_Sight; + if ( ent->aiTeam == AITEAM_ALLIES || ent->aiTeam == AITEAM_NEUTRAL ) { // friendly + cs->activate = AICast_ProcessActivate; + } else { + cs->activate = NULL; + } + cs->aifuncAttack1 = aiCharDefaults->aifuncAttack1; + cs->aifuncAttack2 = aiCharDefaults->aifuncAttack2; + cs->aifuncAttack3 = aiCharDefaults->aifuncAttack3; + // + // looping sound? + if ( aiCharDefaults->loopingSound ) { + ent->s.loopSound = G_SoundIndex( aiCharDefaults->loopingSound ); + } + // + // precache sounds for this character + for ( i = 0; i < MAX_AI_EVENT_SOUNDS; i++ ) { + if ( aiDefaults[ent->aiCharacter].soundScripts[i] ) { + G_SoundIndex( aiDefaults[ent->aiCharacter].soundScripts[i] ); + } + } + // + if ( ent->aiCharacter == AICHAR_HEINRICH ) { + AICast_Heinrich_SoundPrecache(); + } + // + // special spawnflag stuff + if ( ent->spawnflags & 2 ) { + cs->secondDeadTime = qtrue; + } + // + // init scripting + cs->castScriptStatus.castScriptEventIndex = -1; + cs->castScriptStatus.scriptAttackEnt = -1; + // + // set crouch move speed + ent->client->ps.crouchSpeedScale = cs->attributes[CROUCHING_SPEED] / cs->attributes[RUNNING_SPEED]; + // + // check for some anims which we can use for special behaviours + if ( BG_GetAnimScriptEvent( &ent->client->ps, ANIM_ET_ROLL ) >= 0 ) { + cs->aiFlags |= AIFL_ROLL_ANIM; + } + if ( BG_GetAnimScriptEvent( &ent->client->ps, ANIM_ET_FLIP ) >= 0 ) { + cs->aiFlags |= AIFL_FLIP_ANIM; + } + if ( BG_GetAnimScriptEvent( &ent->client->ps, ANIM_ET_DIVE ) >= 0 ) { + cs->aiFlags |= AIFL_DIVE_ANIM; + } + // HACK + if ( ent->aiName && !Q_stricmp( ent->aiName, "deathshead" ) ) { + cs->aiFlags |= AIFL_NO_FLAME_DAMAGE; + } + // + // check for no headshot damage + if ( cs->aiFlags & AIFL_NO_HEADSHOT_DMG ) { + ent->headshotDamageScale = 0.0; + } + // set these values now so scripting system isn't relying on a Think having been run prior to running a script + //origin of the cast + VectorCopy( ent->client->ps.origin, cs->bs->origin ); + //velocity of the cast + VectorCopy( ent->client->ps.velocity, cs->bs->velocity ); + //playerstate + cs->bs->cur_ps = ent->client->ps; + // + if ( !ent->aiInactive ) { + // trigger a spawn script event + AICast_ScriptEvent( cs, "spawn", "" ); + } else { + trap_UnlinkEntity( ent ); + } + +} + +//---------------------------------------------------------------------------------------------------------------------------- +/*QUAKED ai_soldier (1 0.25 0) (-16 -16 -24) (16 16 64) TriggerSpawn NoRevive +soldier entity +"skin" the .skin file to use for this character (must exist in the player characters directory, otherwise 'infantryss/default' is used) +"head" the .skin file to use for his head (must exist in the pc's dir, otherwise 'default' is used) +"ainame" name of AI +*/ +/* +-------- MODEL FOR RADIANT ONLY - DO NOT SET THIS AS A KEY -------- +model="models\mapobjects\characters\test\nazi.md3" +*/ +/* +============ +SP_ai_soldier +============ +*/ +void SP_ai_soldier( gentity_t *ent ) { + AICast_DelayedSpawnCast( ent, AICHAR_SOLDIER ); +} + +//---------------------------------------------------------------------------------------------------------------------------- +/*QUAKED ai_american (1 0.25 0) (-16 -16 -24) (16 16 64) TriggerSpawn NoRevive +american entity +"skin" the .skin file to use for this character (must exist in the player characters directory, otherwise 'american/default' is used) +"head" the .skin file to use for his head (must exist in the pc's dir, otherwise 'default' is used) +"ainame" name of AI +*/ + +/* +============ +SP_ai_american +============ +*/ +void SP_ai_american( gentity_t *ent ) { + AICast_DelayedSpawnCast( ent, AICHAR_AMERICAN ); +} + +//---------------------------------------------------------------------------------------------------------------------------- +/*QUAKED ai_zombie (1 0.25 0) (-16 -16 -24) (16 16 64) TriggerSpawn NoRevive PortalZombie +zombie entity +"skin" the .skin file to use for this character (must exist in the player characters directory, otherwise 'zombie/default' is used) +"head" the .skin file to use for his head (must exist in the pc's dir, otherwise 'default' is used) +"ainame" name of AI +*/ + +/* +============ +SP_ai_zombie +============ +*/ +void SP_ai_zombie( gentity_t *ent ) { + ent->r.svFlags |= SVF_NOFOOTSTEPS; + AICast_DelayedSpawnCast( ent, AICHAR_ZOMBIE ); +} + + +//---------------------------------------------------------------------------------------------------------------------------- +/*QUAKED ai_warzombie (1 0.25 0) (-16 -16 -24) (16 16 64) TriggerSpawn NoRevive PortalZombie +warrior zombie entity +"skin" the .skin file to use for this character (must exist in the player characters directory, otherwise 'warrior/default' is used) +"head" the .skin file to use for his head (must exist in the pc's dir, otherwise 'default' is used) +"ainame" name of AI +*/ + +/* +============ +SP_ai_warzombie +============ +*/ +void SP_ai_warzombie( gentity_t *ent ) { + AICast_DelayedSpawnCast( ent, AICHAR_WARZOMBIE ); +} + + +//---------------------------------------------------------------------------------------------------------------------------- +/*QUAKED ai_venom (1 0.25 0) (-16 -16 -24) (16 16 64) TriggerSpawn NoRevive +venom entity +"skin" the .skin file to use for this character (must exist in the player characters directory, otherwise 'venom/default' is used) +"head" the .skin file to use for his head (must exist in the pc's dir, otherwise 'default' is used) +"ainame" name of AI +*/ + +/* +============ +SP_ai_venom +============ +*/ +void SP_ai_venom( gentity_t *ent ) { + ent->r.svFlags |= SVF_NOFOOTSTEPS; + AICast_DelayedSpawnCast( ent, AICHAR_VENOM ); +} + + +//---------------------------------------------------------------------------------------------------------------------------- +/*QUAKED ai_loper (1 0.25 0) (-32 -32 -24) (32 32 48) TriggerSpawn NoRevive +loper entity +"skin" the .skin file to use for this character (must exist in the player characters directory, otherwise 'loper/default' is used) +"head" the .skin file to use for his head (must exist in the pc's dir, otherwise 'default' is used) +"ainame" name of AI +*/ + +/* +============ +SP_ai_loper +============ +*/ +void SP_ai_loper( gentity_t *ent ) { + ent->r.svFlags |= SVF_NOFOOTSTEPS; + AICast_DelayedSpawnCast( ent, AICHAR_LOPER ); + // + level.loperZapSound = G_SoundIndex( "loperZap" ); +} + +//---------------------------------------------------------------------------------------------------------------------------- +/*QUAKED ai_boss_helga (1 0.25 0) (-16 -16 -24) (16 16 64) TriggerSpawn NoRevive +helga entity +"skin" the .skin file to use for this character (must exist in the player characters directory, otherwise 'helga/default' is used) +"head" the .skin file to use for his head (must exist in the pc's dir, otherwise 'default' is used) +"ainame" name of AI +*/ + +/* +============ +SP_ai_boss_helga +============ +*/ +void SP_ai_boss_helga( gentity_t *ent ) { + AICast_DelayedSpawnCast( ent, AICHAR_HELGA ); +} + +//---------------------------------------------------------------------------------------------------------------------------- +/*QUAKED ai_boss_heinrich (1 0.25 0) (-32 -32 -24) (32 32 156) TriggerSpawn NoRevive +heinrich entity +"skin" the .skin file to use for this character (must exist in the player characters directory, otherwise 'helga/default' is used) +"head" the .skin file to use for his head (must exist in the pc's dir, otherwise 'default' is used) +"ainame" name of AI +*/ + +/* +============ +SP_ai_boss_heinrich +============ +*/ +void SP_ai_boss_heinrich( gentity_t *ent ) { + AICast_DelayedSpawnCast( ent, AICHAR_HEINRICH ); +} + +//---------------------------------------------------------------------------------------------------------------------------- +/*QUAKED ai_partisan (1 0.25 0) (-16 -16 -24) (16 16 64) TriggerSpawn NoRevive +"skin" the .skin file to use for this character (must exist in the player characters directory, otherwise 'partisan/default' is used) +"head" the .skin file to use for his head (must exist in the pc's dir, otherwise 'default' is used) +"ainame" name of AI +*/ + +/* +============ +SP_ai_partisan +============ +*/ +void SP_ai_partisan( gentity_t *ent ) { + AICast_DelayedSpawnCast( ent, AICHAR_PARTISAN ); +} + +//---------------------------------------------------------------------------------------------------------------------------- +/*QUAKED ai_civilian (1 0.25 0) (-16 -16 -24) (16 16 64) TriggerSpawn NoRevive +"skin" the .skin file to use for this character (must exist in the player characters directory, otherwise 'civilian/default' is used) +"head" the .skin file to use for his head (must exist in the pc's dir, otherwise 'default' is used) +"ainame" name of AI +*/ + +/* +============ +SP_ai_civilian +============ +*/ +void SP_ai_civilian( gentity_t *ent ) { + AICast_DelayedSpawnCast( ent, AICHAR_CIVILIAN ); +} + +//---------------------------------------------------------------------------------------------------------------------------- +/*QUAKED ai_eliteguard (1 0.25 0) (-16 -16 -24) (16 16 64) TriggerSpawn NoRevive +elite guard entity +"skin" the .skin file to use for this character (must exist in the player characters directory, otherwise 'eliteguard/default' is used) +"head" the .skin file to use for his head (must exist in the pc's dir, otherwise 'default' is used) +"ainame" name of AI +*/ + +/* +============ +SP_ai_eliteguard +============ +*/ +void SP_ai_eliteguard( gentity_t *ent ) { + AICast_DelayedSpawnCast( ent, AICHAR_ELITEGUARD ); +} + + +//---------------------------------------------------------------------------------------------------------------------------- +/*QUAKED ai_frogman (1 0.25 0) (-16 -16 -24) (16 16 64) TriggerSpawn NoRevive +elite guard entity +"skin" the .skin file to use for this character (must exist in the player characters directory, otherwise 'frogman/default' is used) +"head" the .skin file to use for his head (must exist in the pc's dir, otherwise 'default' is used) +"ainame" name of AI +*/ + +/* +============ +SP_ai_frogman +============ +*/ +void SP_ai_frogman( gentity_t *ent ) { + ent->r.svFlags |= SVF_NOFOOTSTEPS; + AICast_DelayedSpawnCast( ent, AICHAR_FROGMAN ); +} + + +//---------------------------------------------------------------------------------------------------------------------------- +/*QUAKED ai_stimsoldier_dual (1 0.25 0) (-32 -32 -24) (32 32 64) TriggerSpawn NoRevive +stim soldier entity +"skin" the .skin file to use for this character (must exist in the player characters directory, otherwise 'stim/default' is used) +"head" the .skin file to use for his head (must exist in the pc's dir, otherwise 'default' is used) +"ainame" name of AI +*/ + +/* +============ +SP_ai_stimsoldier_dual +============ +*/ +void SP_ai_stimsoldier_dual( gentity_t *ent ) { + AICast_DelayedSpawnCast( ent, AICHAR_STIMSOLDIER1 ); + // + level.stimSoldierFlySound = G_SoundIndex( "sound/stimsoldier/flyloop.wav" ); +} + +//---------------------------------------------------------------------------------------------------------------------------- +/*QUAKED ai_stimsoldier_rocket (1 0.25 0) (-32 -32 -24) (32 32 64) TriggerSpawn NoRevive +stim soldier entity +"skin" the .skin file to use for this character (must exist in the player characters directory, otherwise 'stim/default' is used) +"head" the .skin file to use for his head (must exist in the pc's dir, otherwise 'default' is used) +"ainame" name of AI +*/ + +/* +============ +SP_ai_stimsoldier_rocket +============ +*/ +void SP_ai_stimsoldier_rocket( gentity_t *ent ) { + AICast_DelayedSpawnCast( ent, AICHAR_STIMSOLDIER2 ); + // + level.stimSoldierFlySound = G_SoundIndex( "sound/stimsoldier/flyloop.wav" ); +} + +//---------------------------------------------------------------------------------------------------------------------------- +/*QUAKED ai_stimsoldier_tesla (1 0.25 0) (-32 -32 -24) (32 32 64) TriggerSpawn NoRevive +stim soldier entity +"skin" the .skin file to use for this character (must exist in the player characters directory, otherwise 'stim/default' is used) +"head" the .skin file to use for his head (must exist in the pc's dir, otherwise 'default' is used) +"ainame" name of AI +*/ + +/* +============ +SP_ai_stimsoldier_tesla +============ +*/ +void SP_ai_stimsoldier_tesla( gentity_t *ent ) { + AICast_DelayedSpawnCast( ent, AICHAR_STIMSOLDIER3 ); + // + level.stimSoldierFlySound = G_SoundIndex( "sound/stimsoldier/flyloop.wav" ); +} + +//---------------------------------------------------------------------------------------------------------------------------- +/*QUAKED ai_supersoldier (1 0.25 0) (-32 -32 -24) (32 32 64) TriggerSpawn NoRevive +supersoldier entity +"skin" the .skin file to use for this character (must exist in the player characters directory, otherwise 'supersoldier/default' is used) +"head" the .skin file to use for his head (must exist in the pc's dir, otherwise 'default' is used) +"ainame" name of AI +*/ + +/* +============ +SP_ai_supersoldier +============ +*/ +void SP_ai_supersoldier( gentity_t *ent ) { + AICast_DelayedSpawnCast( ent, AICHAR_SUPERSOLDIER ); +} + +//---------------------------------------------------------------------------------------------------------------------------- +/*QUAKED ai_protosoldier (1 0.25 0) (-32 -32 -24) (32 32 64) TriggerSpawn NoRevive +protosoldier entity +"skin" the .skin file to use for this character (must exist in the player characters directory, otherwise 'protosoldier/default' is used) +"head" the .skin file to use for his head (must exist in the pc's dir, otherwise 'default' is used) +"ainame" name of AI +*/ + +/* +============ +SP_ai_protosoldier +============ +*/ +void SP_ai_protosoldier( gentity_t *ent ) { + AICast_DelayedSpawnCast( ent, AICHAR_PROTOSOLDIER ); +} + +//---------------------------------------------------------------------------------------------------------------------------- +/*QUAKED ai_blackguard (1 0.25 0) (-16 -16 -24) (16 16 64) TriggerSpawn NoRevive +black guard entity +"skin" the .skin file to use for this character (must exist in the player characters directory, otherwise 'blackguard/default' is used) +"head" the .skin file to use for his head (must exist in the pc's dir, otherwise 'default' is used) +"ainame" name of AI +*/ + +/* +============ +SP_ai_blackguard +============ +*/ +void SP_ai_blackguard( gentity_t *ent ) { + AICast_DelayedSpawnCast( ent, AICHAR_BLACKGUARD ); +} diff --git a/src/game/ai_cast_debug.c b/src/game/ai_cast_debug.c new file mode 100644 index 0000000..e32b202 --- /dev/null +++ b/src/game/ai_cast_debug.c @@ -0,0 +1,237 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: ai_cast_debug.c +// Function: Wolfenstein AI Character Routines +// Programmer: Ridah +// Tab Size: 4 (real tabs) +//=========================================================================== + +#include "g_local.h" +#include "../game/botlib.h" //bot lib interface +#include "../game/be_aas.h" +#include "../game/be_ea.h" +#include "../game/be_ai_gen.h" +#include "../game/be_ai_goal.h" +#include "../game/be_ai_move.h" +#include "../botai/botai.h" //bot ai interface + +#include "ai_cast.h" + +static int numaifuncs; +static char *aifuncs[MAX_AIFUNCS]; + +/* +========== +AICast_DBG_InitAIFuncs +========== +*/ +void AICast_DBG_InitAIFuncs( void ) { + numaifuncs = 0; +} + +/* +========== +AICast_DBG_AddAIFunc +========== +*/ +void AICast_DBG_AddAIFunc( cast_state_t *cs, char *funcname ) { + if ( aicast_debug.integer ) { + if ( aicast_debug.integer != 2 || ( g_entities[cs->entityNum].aiName && !strcmp( aicast_debugname.string, g_entities[cs->entityNum].aiName ) ) ) { + G_Printf( "%s: %s\n", g_entities[cs->entityNum].aiName, funcname ); + } + } + aifuncs[numaifuncs] = funcname; + numaifuncs++; +} + +/* +========== +AICast_DBG_ListAIFuncs +========== +*/ +void AICast_DBG_ListAIFuncs( cast_state_t *cs, int numprint ) { + int i; + + if ( aicast_debug.integer != 2 || ( g_entities[cs->entityNum].aiName && !strcmp( aicast_debugname.string, g_entities[cs->entityNum].aiName ) ) ) { + AICast_Printf( AICAST_PRT_DEBUG, S_COLOR_RED "AICast_ProcessAIFunctions: executed more than %d AI funcs\n", MAX_AIFUNCS ); + for ( i = MAX_AIFUNCS - numprint; i < MAX_AIFUNCS; i++ ) + AICast_Printf( AICAST_PRT_DEBUG, "%s, ", aifuncs[i] ); + AICast_Printf( AICAST_PRT_DEBUG, "\n" ); + } +} + +/* +========== +AICast_DebugFrame +========== +*/ +void AICast_DebugFrame( cast_state_t *cs ) { + gentity_t *ent; + + if ( aicast_debug.integer ) { + ent = &g_entities[cs->entityNum]; + + if ( cs->castScriptStatus.castScriptEventIndex >= 0 ) { + ent->client->ps.eFlags |= EF_TALK; + } else { + ent->client->ps.eFlags &= ~EF_TALK; + } + } +} + +/* +=========== +AICast_DBG_RouteTable_f +=========== +*/ +void AICast_DBG_RouteTable_f( vec3_t org, char *param ) { + static int srcarea = 0, dstarea = 0; +// extern botlib_export_t botlib; // TTimo: unused + + if ( !param || strlen( param ) < 1 ) { + trap_Printf( "You must specify 'src', 'dest' or 'show'\n" ); + return; + } + + trap_AAS_SetCurrentWorld( 0 ); // use the default world, which should have a routetable + + if ( Q_stricmp( param, "toggle" ) == 0 ) { + trap_AAS_RT_ShowRoute( vec3_origin, -666, -666 ); // stupid toggle hack + return; + } + + if ( Q_stricmp( param, "src" ) == 0 ) { // set the src + srcarea = 1 + trap_AAS_PointAreaNum( org ); + return; + } else if ( Q_stricmp( param, "dest" ) == 0 ) { + dstarea = 1 + trap_AAS_PointAreaNum( org ); + } + + if ( srcarea && dstarea ) { // show the path + trap_AAS_RT_ShowRoute( org, srcarea - 1, dstarea - 1 ); + } else + { + trap_Printf( "You must specify 'src' & 'dest' first\n" ); + } +} + +/* +=============== +AICast_DBG_Spawn_f +=============== +*/ +void AICast_DBG_Spawn_f( gclient_t *client, char *cmd ) { + extern qboolean G_CallSpawn( gentity_t *ent ); + gentity_t *ent; + vec3_t dir; + + ent = G_Spawn(); + ent->classname = G_Alloc( strlen( cmd ) + 1 ); + strcpy( ent->classname, cmd ); + AngleVectors( client->ps.viewangles, dir, NULL, NULL ); + VectorMA( client->ps.origin, 96, dir, ent->s.origin ); + + if ( !G_CallSpawn( ent ) ) { + G_Printf( "Error: unable to spawn \"%s\" entity\n", cmd ); + } +} + +/* +=============== +AICast_DBG_Cmd_f + + General entry point for all "aicast ..." commands +=============== +*/ +void AICast_DBG_Cmd_f( int clientNum ) { + gentity_t *ent; + char cmd[MAX_TOKEN_CHARS]; + + ent = g_entities + clientNum; + if ( !ent->client ) { + return; // not fully in game yet + } + + // get the first word following "aicast" + trap_Argv( 1, cmd, sizeof( cmd ) ); + + if ( Q_stricmp( cmd, "dbg_routetable" ) == 0 ) { + trap_Argv( 2, cmd, sizeof( cmd ) ); + AICast_DBG_RouteTable_f( ent->client->ps.origin, cmd ); + return; + } + if ( Q_stricmp( cmd, "spawn" ) == 0 ) { + // spawn a given character + trap_Argv( 2, cmd, sizeof( cmd ) ); + AICast_DBG_Spawn_f( ent->client, cmd ); + return; + } + if ( Q_stricmp( cmd, "getname" ) == 0 ) { + // get name of character we're looking at +// AICast_DBG_GetName_f(ent); + return; + } + if ( Q_stricmp( cmd, "followme" ) == 0 ) { + // tell character to follow us + trap_Argv( 2, cmd, sizeof( cmd ) ); +// AICast_DBG_FollowMe_f(ent->client, cmd); + return; + } +} +/* +// Ridah, faster Win32 code +#ifdef _WIN32 +#undef MAX_PATH // this is an ugly hack, to temporarily ignore the current definition, since it's also defined in windows.h +#include +#undef MAX_PATH +#define MAX_PATH MAX_QPATH +#endif + +int Sys_MilliSeconds(void) +{ +// Ridah, faster Win32 code +#ifdef _WIN32 + int sys_curtime; + static qboolean initialized = qfalse; + static int sys_timeBase; + + if (!initialized) { + sys_timeBase = timeGetTime(); + initialized = qtrue; + } + sys_curtime = timeGetTime() - sys_timeBase; + + return sys_curtime; +#else + return clock() * 1000 / CLOCKS_PER_SEC; +#endif +} //end of the function Sys_MilliSeconds +*/ diff --git a/src/game/ai_cast_events.c b/src/game/ai_cast_events.c new file mode 100644 index 0000000..5b8dd8f --- /dev/null +++ b/src/game/ai_cast_events.c @@ -0,0 +1,594 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: ai_cast_events.c +// Function: Wolfenstein AI Character Events +// Programmer: Ridah +// Tab Size: 4 (real tabs) +//=========================================================================== + +#include "../game/g_local.h" +#include "../game/q_shared.h" +#include "../game/botlib.h" //bot lib interface +#include "../game/be_aas.h" +#include "../game/be_ea.h" +#include "../game/be_ai_gen.h" +#include "../game/be_ai_goal.h" +#include "../game/be_ai_move.h" +#include "../botai/botai.h" //bot ai interface + +#include "ai_cast.h" + +/* +Contains response functions for various events that require specific handling +for Cast AI's. +*/ + +/* +============ +AICast_Sight +============ +*/ +void AICast_Sight( gentity_t *ent, gentity_t *other, int lastSight ) { + cast_state_t *cs, *ocs; + + cs = AICast_GetCastState( ent->s.number ); + ocs = AICast_GetCastState( other->s.number ); + + // + // call the sightfunc for this cast, so we can play associated sounds, or do any character-specific things + // + if ( cs->sightfunc ) { + // factor in the reaction time + if ( AICast_EntityVisible( cs, other->s.number, qfalse ) ) { + cs->sightfunc( ent, other, lastSight ); + } + } + + if ( other->aiName && other->health <= 0 ) { + + // they died since we last saw them + if ( ocs->deathTime > lastSight ) { + if ( !AICast_SameTeam( cs, other->s.number ) ) { + AICast_ScriptEvent( cs, "enemysightcorpse", other->aiName ); + } else if ( !( cs->castScriptStatus.scriptFlags & SFL_FRIENDLYSIGHTCORPSE_TRIGGERED ) ) { + cs->castScriptStatus.scriptFlags |= SFL_FRIENDLYSIGHTCORPSE_TRIGGERED; + AICast_ScriptEvent( cs, "friendlysightcorpse", "" ); + } + } + + // if this is the first time, call the sight script event + } else if ( !lastSight && other->aiName ) { + if ( !AICast_SameTeam( cs, other->s.number ) ) { + // disabled.. triggered when entering combat mode + //AICast_ScriptEvent( cs, "enemysight", other->aiName ); + } else { + AICast_ScriptEvent( cs, "sight", other->aiName ); + } + } +} + +/* +============ +AICast_Pain +============ +*/ +void AICast_Pain( gentity_t *targ, gentity_t *attacker, int damage, vec3_t point ) { + cast_state_t *cs; + + cs = AICast_GetCastState( targ->s.number ); + + // print debugging message + if ( aicast_debug.integer == 2 && attacker->s.number == 0 ) { + G_Printf( "hit %s %i\n", targ->aiName, targ->health ); + } + + // if we are below alert mode, then go there immediately + if ( cs->aiState < AISTATE_ALERT ) { + AICast_StateChange( cs, AISTATE_ALERT ); + } + + if ( cs->aiFlags & AIFL_NOPAIN ) { + return; + } + + // process the event (turn to face the attacking direction? go into hide/retreat state?) + // need to weigh up the situation, but foremost, an inactive AI cast should always react in some way to being hurt + cs->lastPain = level.time; + + // record the sighting (FIXME: silent weapons shouldn't do this, but the AI should react in some way) + if ( attacker->client ) { + AICast_UpdateVisibility( targ, attacker, qtrue, qtrue ); + } + + // if either of us are neutral, then we are now enemies + if ( targ->aiTeam == AITEAM_NEUTRAL || attacker->aiTeam == AITEAM_NEUTRAL ) { + cs->vislist[attacker->s.number].flags |= AIVIS_ENEMY; + } + + AICast_ScriptEvent( cs, "painenemy", attacker->aiName ); + + AICast_ScriptEvent( cs, "pain", va( "%d %d", targ->health, targ->health + damage ) ); + + if ( cs->aiFlags & AIFL_DENYACTION ) { + // dont play any sounds + return; + } + + // + // call the painfunc for this cast, so we can play associated sounds, or do any character-specific things + // + if ( cs->painfunc ) { + cs->painfunc( targ, attacker, damage, point ); + } +} + +/* +============ +AICast_Die +============ +*/ +void AICast_Die( gentity_t *self, gentity_t *inflictor, gentity_t *attacker, int damage, int meansOfDeath ) { + int contents; + int killer; + cast_state_t *cs; + qboolean nogib = qtrue; + char mapname[MAX_QPATH]; + + // print debugging message + if ( aicast_debug.integer == 2 && attacker->s.number == 0 ) { + G_Printf( "killed %s\n", self->aiName ); + } + + cs = AICast_GetCastState( self->s.number ); + + if ( attacker ) { + killer = attacker->s.number; + } else { + killer = ENTITYNUM_WORLD; + } + + // record the sighting (FIXME: silent weapons shouldn't do this, but the AI should react in some way) + if ( attacker->client ) { + AICast_UpdateVisibility( self, attacker, qtrue, qtrue ); + } + + if ( self->aiCharacter == AICHAR_HEINRICH || self->aiCharacter == AICHAR_HELGA || self->aiCharacter == AICHAR_SUPERSOLDIER || self->aiCharacter == AICHAR_PROTOSOLDIER ) { + if ( self->health <= GIB_HEALTH ) { + self->health = -1; + } + } + + // the zombie should show special effect instead of gibbing + if ( self->aiCharacter == AICHAR_ZOMBIE && cs->secondDeadTime ) { + if ( cs->secondDeadTime > 1 ) { + // we are already totally dead + self->health += damage; // don't drop below gib_health if we weren't already below it + return; + } +/* + if (!cs->rebirthTime) + { + self->health = -999; + damage = 999; + } else if ( self->health >= GIB_HEALTH ) { + // while waiting for rebirth, we only "die" if we drop below gib health + return; + } +*/ + // always gib + self->health = -999; + damage = 999; + } + + // Zombies are very fragile against highly explosives + if ( self->aiCharacter == AICHAR_ZOMBIE && damage > 20 && inflictor != attacker ) { + self->health = -999; + damage = 999; + } + + // process the event + if ( self->client->ps.pm_type == PM_DEAD ) { + // already dead + if ( self->health < GIB_HEALTH ) { + if ( self->aiCharacter == AICHAR_ZOMBIE ) { + // RF, changed this so Zombies always gib now + GibEntity( self, killer ); + nogib = qfalse; +/* + // Zombie has special exploding cloud effect + if (attacker != inflictor || attacker->s.weapon == WP_VENOM) + { + GibEntity( self, killer ); + nogib = qfalse; + } else { + // Zombie will decompose upon dying + self->client->ps.eFlags |= EF_MONSTER_EFFECT2; + self->s.effect2Time = level.time+200; + self->health = -1; + } +*/ + self->takedamage = qfalse; + self->r.contents = 0; + cs->secondDeadTime = 2; + cs->rebirthTime = 0; + cs->revivingTime = 0; + } else { + body_die( self, inflictor, attacker, damage, meansOfDeath ); + return; + } + } + + } else { // this is our first death, so set everything up + + if ( level.intermissiontime ) { + return; + } + + self->client->ps.pm_type = PM_DEAD; + + self->enemy = attacker; + + // drop a weapon? + // if client is in a nodrop area, don't drop anything + contents = trap_PointContents( self->r.currentOrigin, -1 ); + if ( !( contents & CONTENTS_NODROP ) ) { + TossClientItems( self ); + } + + // make sure the client doesn't forget about this entity until it's set to "dead" frame + // otherwise it might replay it's death animation if it goes out and into client view + self->r.svFlags |= SVF_BROADCAST; + + self->takedamage = qtrue; // can still be gibbed + + self->s.weapon = WP_NONE; + if ( cs->bs ) { + cs->weaponNum = WP_NONE; + } + self->client->ps.weapon = WP_NONE; + + self->s.powerups = 0; + self->r.contents = CONTENTS_CORPSE; + + self->s.angles[0] = 0; + self->s.angles[1] = self->client->ps.viewangles[1]; + self->s.angles[2] = 0; + + VectorCopy( self->s.angles, self->client->ps.viewangles ); + + self->s.loopSound = 0; + + self->r.maxs[2] = -8; + self->client->ps.maxs[2] = self->r.maxs[2]; + + // remove powerups + memset( self->client->ps.powerups, 0, sizeof( self->client->ps.powerups ) ); + + //cs->rebirthTime = 0; + + // never gib in a nodrop + if ( self->health <= GIB_HEALTH ) { + if ( self->aiCharacter == AICHAR_ZOMBIE ) { + // RF, changed this so Zombies always gib now + GibEntity( self, killer ); + nogib = qfalse; + } else if ( !( contents & CONTENTS_NODROP ) ) { + body_die( self, inflictor, attacker, damage, meansOfDeath ); + //GibEntity( self, killer ); + nogib = qfalse; + } + } + + // if we are a zombie, and lying down during our first death, then we should just die + if ( !( self->aiCharacter == AICHAR_ZOMBIE && cs->secondDeadTime && cs->rebirthTime ) ) { + + // set enemy weapon + BG_UpdateConditionValue( self->s.number, ANIM_COND_ENEMY_WEAPON, 0, qfalse ); + if ( attacker->client ) { + BG_UpdateConditionValue( self->s.number, ANIM_COND_ENEMY_WEAPON, inflictor->s.weapon, qtrue ); + } else { + BG_UpdateConditionValue( self->s.number, ANIM_COND_ENEMY_WEAPON, 0, qfalse ); + } + + // set enemy location + BG_UpdateConditionValue( self->s.number, ANIM_COND_ENEMY_POSITION, 0, qfalse ); + if ( infront( self, inflictor ) ) { + BG_UpdateConditionValue( self->s.number, ANIM_COND_ENEMY_POSITION, POSITION_INFRONT, qtrue ); + } else { + BG_UpdateConditionValue( self->s.number, ANIM_COND_ENEMY_POSITION, POSITION_BEHIND, qtrue ); + } + + if ( self->takedamage ) { // only play the anim if we haven't gibbed + // play the animation + BG_AnimScriptEvent( &self->client->ps, ANIM_ET_DEATH, qfalse, qtrue ); + } + + // set gib delay + if ( cs->aiCharacter == AICHAR_HEINRICH || cs->aiCharacter == AICHAR_HELGA ) { + cs->lastLoadTime = level.time + self->client->ps.torsoTimer - 200; + } + + // set this flag so no other anims override us + self->client->ps.eFlags |= EF_DEAD; + self->s.eFlags |= EF_DEAD; + + // make sure we dont move around while on the ground + //self->flags |= FL_NO_HEADCHECK; + + } + + // if end map, sink into ground + cs->deadSinkStartTime = 0; + if ( cs->aiCharacter == AICHAR_WARZOMBIE ) { + trap_Cvar_VariableStringBuffer( "mapname", mapname, sizeof( mapname ) ); + if ( !Q_strncmp( mapname, "end", 3 ) ) { // !! FIXME: post beta2, make this a spawnflag! + cs->deadSinkStartTime = level.time + 4000; + } + } + } + + if ( nogib ) { + // set for rebirth + if ( self->aiCharacter == AICHAR_ZOMBIE ) { + if ( !cs->secondDeadTime ) { + cs->rebirthTime = level.time + 5000 + rand() % 2000; + // RF, only set for gib at next death, if NoRevive is not set + if ( !( self->spawnflags & 2 ) ) { + cs->secondDeadTime = qtrue; + } + cs->revivingTime = 0; + } else if ( cs->secondDeadTime > 1 ) { + cs->rebirthTime = 0; + cs->revivingTime = 0; + cs->deathTime = level.time; + } + } else { + // the body can still be gibbed + self->die = body_die; + } + } + + trap_LinkEntity( self ); + + // kill, instanly, any streaming sound the character had going + G_AddEvent( &g_entities[self->s.number], EV_STOPSTREAMINGSOUND, 0 ); + + // mark the time of death + cs->deathTime = level.time; + + // dying ai's can trigger a target + if ( !cs->rebirthTime ) { + G_UseTargets( self, self ); + // really dead now, so call the script + AICast_ScriptEvent( cs, "death", attacker->aiName ? attacker->aiName : "" ); + // call the deathfunc for this cast, so we can play associated sounds, or do any character-specific things + if ( !( cs->aiFlags & AIFL_DENYACTION ) && cs->deathfunc ) { + cs->deathfunc( self, attacker, damage, meansOfDeath ); //----(SA) added mod + } + } else { + // really dead now, so call the script + AICast_ScriptEvent( cs, "fakedeath", "" ); + // call the deathfunc for this cast, so we can play associated sounds, or do any character-specific things + if ( !( cs->aiFlags & AIFL_DENYACTION ) && cs->deathfunc ) { + cs->deathfunc( self, attacker, damage, meansOfDeath ); //----(SA) added mod + } + } +} + +/* +=============== +AICast_EndChase +=============== +*/ +void AICast_EndChase( cast_state_t *cs ) { + // anything? +} + +/* +=============== +AICast_AIDoor_Touch +=============== +*/ +void AICast_AIDoor_Touch( gentity_t *ent, gentity_t *aidoor_trigger, gentity_t *door ) { + cast_state_t *cs, *ocs; + gentity_t *trav; + int i; + trace_t tr; + vec3_t mins, pos, dir; + + cs = AICast_GetCastState( ent->s.number ); + + if ( !cs->bs ) { + return; + } + + // does the aidoor have ai_marker's? + if ( !aidoor_trigger->targetname ) { + G_Printf( "trigger_aidoor has no ai_marker's at %s\n", vtos( ent->r.currentOrigin ) ); + return; + } + + // are we heading for an ai_marker? + if ( cs->aifunc == AIFunc_DoorMarker ) { + return; + } + + // if they are moving away from the door, ignore them + if ( VectorLength( cs->bs->velocity ) > 1 ) { + VectorAdd( door->r.absmin, door->r.absmax, pos ); + VectorScale( pos, 0.5, pos ); + VectorSubtract( pos, cs->bs->origin, dir ); + if ( DotProduct( cs->bs->velocity, dir ) < 0 ) { + return; + } + } + + // TTimo: gcc: suggest () around assignment used as truth value + for ( trav = NULL; ( trav = G_Find( trav, FOFS( target ), aidoor_trigger->targetname ) ); ) { + // make sure the marker is vacant + trap_Trace( &tr, trav->r.currentOrigin, ent->r.mins, ent->r.maxs, trav->r.currentOrigin, ent->s.number, ent->clipmask ); + if ( tr.startsolid ) { + continue; + } + // search all other AI's, to see if they are heading for this marker + for ( i = 0, ocs = AICast_GetCastState( 0 ); i < aicast_maxclients; i++, ocs++ ) { + if ( !ocs->bs ) { + continue; + } + if ( ocs->aifunc != AIFunc_DoorMarker ) { + continue; + } + if ( ocs->doorMarker != trav->s.number ) { + continue; + } + // found a match + break; + } + if ( i < aicast_maxclients ) { + continue; + } + // make sure there is a clear path + VectorCopy( ent->r.mins, mins ); + mins[2] += 16; // step height + trap_Trace( &tr, ent->r.currentOrigin, mins, ent->r.maxs, trav->r.currentOrigin, ent->s.number, ent->clipmask ); + if ( tr.fraction < 1.0 ) { + continue; + } + // the marker is vacant and available + cs->doorMarkerTime = level.time; + cs->doorMarkerNum = trav->s.number; + cs->doorMarkerDoor = door->s.number; + break; + } +} + +/* +============ +AICast_ProcessActivate +============ +*/ +void AICast_ProcessActivate( int entNum, int activatorNum ) { + cast_state_t *cs; + gentity_t *newent, *ent, *activator; + gclient_t *client; + + cs = AICast_GetCastState( entNum ); + client = &level.clients[entNum]; + ent = &g_entities[entNum]; + activator = &g_entities[activatorNum]; + + if ( cs->lastActivate > level.time - 1000 ) { + return; + } + cs->lastActivate = level.time; + + if ( !AICast_SameTeam( cs, activatorNum ) ) { + + if ( ent->aiTeam == AITEAM_NEUTRAL ) { + AICast_ScriptEvent( cs, "activate", g_entities[activatorNum].aiName ); + } + + return; + } + + // try running the activate event, if it denies us the request, then abort + cs->aiFlags &= ~AIFL_DENYACTION; + AICast_ScriptEvent( cs, "activate", g_entities[activatorNum].aiName ); + if ( cs->aiFlags & AIFL_DENYACTION ) { + return; + } + + // if we are doing something else + if ( cs->castScriptStatus.castScriptEventIndex >= 0 ) { + if ( ent->eventTime != level.time ) { + G_AddEvent( &g_entities[entNum], EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[cs->aiCharacter].soundScripts[ORDERSDENYSOUNDSCRIPT] ) ); + } + return; + } + + // if we are already following them, stop following + if ( cs->leaderNum == activatorNum ) { + if ( ent->eventTime != level.time ) { + G_AddEvent( &g_entities[entNum], EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[cs->aiCharacter].soundScripts[STAYSOUNDSCRIPT] ) ); + } + + cs->leaderNum = -1; + + // create a goal at this position + newent = G_Spawn(); + newent->classname = "AI_wait_goal"; + newent->r.ownerNum = entNum; + G_SetOrigin( newent, cs->bs->origin ); + AIFunc_ChaseGoalStart( cs, newent->s.number, 128, qtrue ); + + //AIFunc_IdleStart( cs ); + } else { // start following + int count, i; + cast_state_t *tcs; + + // if they already have enough followers, deny + for ( count = 0, i = 0, tcs = caststates; i < level.maxclients; i++, tcs++ ) { + if ( tcs->bs && tcs != cs && tcs->entityNum != activatorNum && g_entities[tcs->entityNum].health > 0 && tcs->leaderNum == activatorNum ) { + count++; + } + } + if ( count >= 3 ) { + if ( ent->eventTime != level.time ) { + G_AddEvent( &g_entities[entNum], EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[cs->aiCharacter].soundScripts[ORDERSDENYSOUNDSCRIPT] ) ); + } + return; + } + + if ( ent->eventTime != level.time ) { + G_AddEvent( &g_entities[entNum], EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[cs->aiCharacter].soundScripts[FOLLOWSOUNDSCRIPT] ) ); + } + + // if they have a wait goal, free it + if ( cs->followEntity >= MAX_CLIENTS && g_entities[cs->followEntity].classname && !strcmp( g_entities[cs->followEntity].classname, "AI_wait_goal" ) ) { + G_FreeEntity( &g_entities[cs->followEntity] ); + } + + cs->followEntity = -1; + cs->leaderNum = activatorNum; + } +} + +/* +================ +AICast_RecordScriptSound +================ +*/ +void AICast_RecordScriptSound( int client ) { + cast_state_t *cs; + + cs = AICast_GetCastState( client ); + cs->lastScriptSound = level.time; +} diff --git a/src/game/ai_cast_fight.c b/src/game/ai_cast_fight.c new file mode 100644 index 0000000..4640b31 --- /dev/null +++ b/src/game/ai_cast_fight.c @@ -0,0 +1,2371 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: ai_cast_fight.c +// Function: Wolfenstein AI Character Fighting/Combat +// Programmer: Ridah +// Tab Size: 4 (real tabs) +//=========================================================================== + +#include "../game/g_local.h" +#include "../game/q_shared.h" +#include "../game/botlib.h" //bot lib interface +#include "../game/be_aas.h" +#include "../game/be_ea.h" +#include "../game/be_ai_gen.h" +#include "../game/be_ai_goal.h" +#include "../game/be_ai_move.h" +#include "../game/be_ai_weap.h" +#include "../botai/botai.h" //bot ai interface + +#include "ai_cast.h" + +/* +Support routines for the Decision Making layer. +*/ + +// FIXME: go through here and convert all weapon/character parameters to #define's +// and move them to a seperate header file for easy modification + +/* +================= +AICast_StateChange + + returns qfalse if scripting has denied the action +================= +*/ +qboolean AICast_StateChange( cast_state_t *cs, aistateEnum_t newaistate ) { + gentity_t *ent; + int result, scriptIndex; + aistateEnum_t oldstate; + + ent = &g_entities[cs->entityNum]; + + oldstate = cs->aiState; + cs->aiState = newaistate; + + // RF, if the state is the same, ignore + if ( oldstate == newaistate ) { + return qtrue; + } + + // if moving from query mode, kill the anim and pausetime + if ( oldstate == AISTATE_QUERY ) { + // stop playing the animation + ent->client->ps.torsoTimer = 0; + ent->client->ps.legsTimer = 0; + cs->pauseTime = 0; + } + + // if moving to combat mode, default back to normal movetype (fast) + if ( newaistate == AISTATE_COMBAT ) { + cs->movestate = MS_DEFAULT; + cs->movestateType = MSTYPE_NONE; + } + + scriptIndex = cs->scriptCallIndex; + + // check scripting to see if this event should be ignored (no anim or handling) + cs->aiFlags &= ~AIFL_DENYACTION; + AICast_ScriptEvent( cs, "statechange", va( "%s %s", animStateStr[oldstate].string, animStateStr[newaistate].string ) ); + + if ( !( cs->aiFlags & AIFL_DENYACTION ) ) { + // if no script was found, try enemysight + if ( newaistate == AISTATE_COMBAT && cs->scriptCallIndex == scriptIndex && + !( cs->vislist[cs->enemyNum].flags & AIVIS_SIGHT_SCRIPT_CALLED ) ) { // no script was found, so default back to enemysight + AICast_ScriptEvent( cs, "enemysight", g_entities[cs->enemyNum].aiName ); + cs->vislist[cs->enemyNum].flags |= AIVIS_SIGHT_SCRIPT_CALLED; + if ( !( cs->aiFlags & AIFL_DENYACTION ) ) { + G_AddEvent( ent, EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[ent->aiCharacter].soundScripts[SIGHTSOUNDSCRIPT] ) ); + } + + if ( cs->aiFlags & AIFL_DENYACTION ) { + // don't run any dynamic handling or default anims + return qfalse; + } + } + + // look for an animation + result = BG_AnimScriptStateChange( &ent->client->ps, newaistate, oldstate ); + + if ( result > 0 ) { + // pause while the animation plays + cs->pauseTime = level.time + result; + } + } + + // set query mode fields + if ( newaistate == AISTATE_QUERY ) { + cs->queryStartTime = level.time; + if ( cs->queryCountValidTime < level.time ) { + cs->queryCount = 0; + } else { + cs->queryCount++; + } + cs->queryCountValidTime = level.time + 60000; // one minute + switch ( cs->queryCount ) { + case 0: + cs->queryAlertSightTime = level.time + 1000; + break; + case 1: + cs->queryAlertSightTime = level.time + 500; + break; + default: + cs->queryAlertSightTime = -1; // IMMEDIATE COMBAT MODE + break; + } + } + + return qtrue; +} + +/* +================= +AICast_ScanForEnemies + + returns the number of enemies visible, filling the "enemies" list before exiting + + if we only found queryEnemies (possibly hostile, but not sure) while relaxed, + then a negative count is returned +================= +*/ +int AICast_ScanForEnemies( cast_state_t *cs, int *enemies ) { + int i, j, enemyCount, queryCount, friendlyAlertCount; + static float distances[MAX_CLIENTS]; + static int sortedEnemies[MAX_CLIENTS]; + float lastDist; + int best, oldEnemy, oldPauseTime; + cast_state_t *ocs; + + if ( cs->castScriptStatus.scriptAttackEnt >= 0 ) { + if ( g_entities[cs->castScriptStatus.scriptAttackEnt].health <= 0 ) { + cs->castScriptStatus.scriptAttackEnt = -1; + } else { + // if we are not in combat mode, then an enemy should trigger a state change straight to combat mode + if ( cs->aiState < AISTATE_COMBAT ) { + AICast_StateChange( cs, AISTATE_COMBAT ); // just go straight to combat mode + } + enemies[0] = cs->castScriptStatus.scriptAttackEnt; + return 1; + } + } + + if ( cs->castScriptStatus.scriptNoAttackTime >= level.time ) { + return qfalse; + } + + if ( cs->noAttackTime >= level.time ) { + return qfalse; + } + + if ( cs->castScriptStatus.scriptNoSightTime >= level.time ) { + return qfalse; + } + + if ( cs->pauseTime > level.time ) { + return qfalse; + } + + enemyCount = 0; + queryCount = 0; + friendlyAlertCount = 0; + + // while we're here, may as well check for some baddies + for ( i = 0; i < g_maxclients.integer; i++ ) + { + if ( g_entities[i].inuse ) { + // try not to commit suicide + if ( i != cs->entityNum ) { + if ( AICast_EntityVisible( cs, i, qfalse ) ) { + // how should we deal with them? + if ( ( g_entities[i].health > 0 ) && AICast_HostileEnemy( cs, i ) ) { // visible and a baddy! + enemies[enemyCount] = i; + enemyCount++; + queryCount = 0; + friendlyAlertCount = 0; + } else if ( !enemyCount && ( g_entities[i].health > 0 ) && AICast_QueryEnemy( cs, i ) && ( cs->vislist[i].flags & AIVIS_PROCESS_SIGHTING ) ) { + enemies[queryCount] = i; + queryCount++; + friendlyAlertCount = 0; + } else if ( !queryCount && !enemyCount && ( cs->vislist[i].flags & AIVIS_INSPECT ) ) { + enemies[friendlyAlertCount] = i; + friendlyAlertCount++; + } + // the sighting has been processed + cs->vislist[i].flags &= ~AIVIS_PROCESS_SIGHTING; + } + } + } + } + + if ( !enemyCount ) { + if ( queryCount ) { + enemyCount = queryCount; + } else if ( friendlyAlertCount ) { + enemyCount = friendlyAlertCount; + } + } + + if ( !enemyCount ) { // nothing worth doing anything about + // look for audible events that we should investigate + if ( cs->audibleEventTime && cs->audibleEventTime < level.time && cs->audibleEventTime > level.time - 2000 ) { + return -4; + } + // look for bullet impacts that have occured recently + if ( cs->bulletImpactTime && cs->bulletImpactTime < level.time && cs->bulletImpactTime > level.time - 1000 ) { + return -3; + } + return 0; + } + + // sort the enemies by distance + for ( i = 0; i < enemyCount; i++ ) { + distances[i] = Distance( cs->bs->origin, g_entities[enemies[i]].client->ps.origin ); + if ( !distances[i] ) { + G_Printf( "WARNING: zero distance between enemies:\n%s at %s, %s at %s\n", g_entities[cs->entityNum].aiName, vtos( cs->bs->origin ), g_entities[enemies[i]].aiName, vtos( g_entities[enemies[i]].client->ps.origin ) ); + distances[i] = 999998; // try to ignore them (HACK) + } + } + for ( j = 0; j < enemyCount; j++ ) { + lastDist = 999999; + best = -1; + for ( i = 0; i < enemyCount; i++ ) { + if ( distances[i] && distances[i] < lastDist ) { + lastDist = distances[i]; + best = i; + } + } + if ( best < 0 ) { + G_Error( "error sorting enemies by distance\n" ); + } + sortedEnemies[j] = enemies[best]; + distances[best] = -1; + } + memcpy( enemies, sortedEnemies, sizeof( int ) * enemyCount ); + + // if we are not in combat mode, then an enemy should trigger a state change straight to combat mode + if ( !queryCount && !friendlyAlertCount && enemyCount && cs->aiState < AISTATE_COMBAT ) { + // face them while making the transition + oldEnemy = cs->enemyNum; + // set it temporarily + cs->enemyNum = enemies[0]; + // + AICast_AimAtEnemy( cs ); + AICast_StateChange( cs, AISTATE_COMBAT ); // just go straight to combat mode + // set it back + cs->enemyNum = oldEnemy; + } + + // if we are in relaxed state, and we see a query enemy, then go into query mode + if ( queryCount ) { + if ( cs->aiState == AISTATE_RELAXED ) { + // go into query mode + if ( AICast_StateChange( cs, AISTATE_QUERY ) ) { + cs->enemyNum = enemies[0]; // lock onto the closest potential enemy + return -1; + } + return 0; // scripting obviously doesn't want us to progress from relaxed just yet + } + // else ignore the query mode, since we are already above relaxed mode + return 0; + } + if ( friendlyAlertCount ) { + // call a script event + oldPauseTime = cs->scriptPauseTime; + if ( g_entities[enemies[0]].health <= 0 ) { + AICast_ForceScriptEvent( cs, "inspectbodystart", g_entities[enemies[0]].aiName ); + if ( cs->aiFlags & AIFL_DENYACTION ) { + // ignore this friendly + cs->vislist[enemies[0]].flags |= AIVIS_INSPECTED; // they have been notified + cs->vislist[enemies[0]].flags &= ~AIVIS_INSPECT; // they have been notified + return 0; + } + } + // + if ( cs->aiState < AISTATE_COMBAT ) { + // go into alert mode, and return this entity so we can inspect it or something, + // but let dynamic AI sort out what it wants to do + if ( cs->aiState == AISTATE_ALERT || AICast_StateChange( cs, AISTATE_ALERT ) ) { + // only return the entity if they are in combat mode or dead + ocs = AICast_GetCastState( enemies[0] ); + if ( ( g_entities[enemies[0]].health <= 0 ) || ( ocs->aiState >= AISTATE_COMBAT ) ) { + return -2; + } + } + return 0; // scripting failed or they're not worth physically inspecting + } + // ignore the friendly, we have our hands full + return 0; + } + + // must be hostile enemy(s) found, so return them + return enemyCount; +} + +/* +================== +AICast_EntityVisible +================== +*/ +qboolean AICast_EntityVisible( cast_state_t *cs, int enemynum, qboolean directview ) { + cast_visibility_t *vis; + int last_visible; + int reactionTime; + float dist; + + if ( enemynum >= MAX_CLIENTS ) { + return qtrue; // FIXME: do a visibility calculation on non-client entities? + + } + vis = &cs->vislist[enemynum]; + + if ( !vis->visible_timestamp && !vis->real_visible_timestamp ) { + return qfalse; // they are not visible at all + + } + if ( directview ) { + last_visible = vis->real_visible_timestamp; + } else { + last_visible = vis->visible_timestamp; + } + + reactionTime = (int)( 1000 * cs->attributes[REACTION_TIME] ); + if ( cs->startAttackCount > 1 ) { + // we recently saw them, so we are more "aware" of their presence + reactionTime /= 2; + } + + // if they are close, we should react faster + if ( cs->bs && enemynum == cs->enemyNum ) { + dist = cs->enemyDist; + } else { + dist = VectorDistance( g_entities[cs->entityNum].client->ps.origin, cs->vislist[enemynum].visible_pos ); + } + if ( dist < 384 ) { + reactionTime *= 0.5 + 0.5 * ( dist / 384 ); + } + + if ( vis->notvisible_timestamp < ( level.time - reactionTime ) ) { + // make sure we've seen them since we've last not seen them (since the visibility checking is spread amongst server frames) + if ( vis->notvisible_timestamp < last_visible ) { + return qtrue; + } + } + + // we can't directly see them, but if they've just left our sight, pretend we can see them for another second or so + + if ( !directview && last_visible ) { + if ( vis->notvisible_timestamp > last_visible ) { + if ( vis->notvisible_timestamp < ( last_visible + 5000 ) ) { + return qtrue; + } + } + } + + + return qfalse; +} + +/* +================== +AICast_HostileEnemy + + returns qtrue if the entity is hostile +================== +*/ +qboolean AICast_HostileEnemy( cast_state_t *cs, int enemynum ) { + // if we hate them, they are an enemy + if ( cs->vislist[enemynum].flags & AIVIS_ENEMY ) { + return qtrue; + } else { + return qfalse; + } +} + +/* +================== +AICast_QueryEnemy + + returns qtrue if the entity can become hostile (they hurt us or we recognize them) +================== +*/ +qboolean AICast_QueryEnemy( cast_state_t *cs, int enemynum ) { + + if ( g_entities[cs->entityNum].aiTeam != g_entities[enemynum].aiTeam ) { + if ( g_entities[cs->entityNum].aiTeam == AITEAM_MONSTER || g_entities[enemynum].aiTeam == AITEAM_MONSTER ) { + // monsters hate all non-monsters and vice-versa + return qtrue; + } else { + // neutral's can only possibly become hostile if they hurt us, otherwise they are assumed to be harmless + if ( g_entities[cs->entityNum].aiTeam == AITEAM_NEUTRAL || g_entities[enemynum].aiTeam == AITEAM_NEUTRAL ) { + return qfalse; // one of us is neutral, assume harmless + } + return qtrue; + } + } else { // same team + return qfalse; // be cool bitch + } + +} + +/* +================== +AICast_SameTeam + + the player is always team 1, AI's default to team 0 + + MONSTER's hate everyone else except other MONSTER's + + NEUTRAL's are cool with everyone that hasn't hurt them +================== +*/ +qboolean AICast_SameTeam( cast_state_t *cs, int enemynum ) { + + if ( g_entities[cs->entityNum].aiTeam != g_entities[enemynum].aiTeam ) { + if ( g_entities[cs->entityNum].aiTeam == AITEAM_NEUTRAL || g_entities[enemynum].aiTeam == AITEAM_NEUTRAL ) { + // if we hate them, they are an enemy + if ( cs->vislist[enemynum].flags & AIVIS_ENEMY ) { + return qfalse; + } else { + return qtrue; + } + } else { + return qfalse; // they are an enemy + } + } else { + return qtrue; // be cool bitch + } + +} + +/* +================== +AICast_WeaponRange +================== +*/ +float AICast_WeaponRange( cast_state_t *cs, int weaponnum ) { + switch ( weaponnum ) { + case WP_TESLA: + switch ( cs->aiCharacter ) { + case AICHAR_SUPERSOLDIER: // BOSS2 + // if they have a panzer, give this weapon a shorter range + if ( !COM_BitCheck( cs->bs->cur_ps.weapons, WP_PANZERFAUST ) ) { + return TESLA_SUPERSOLDIER_RANGE; + } + } + return ( TESLA_RANGE * 0.9 ) - 50; // allow for bounding box + case WP_FLAMETHROWER: + return ( FLAMETHROWER_RANGE * 0.5 ) - 50; // allow for bounding box + case WP_PANZERFAUST: + return 8000; + + case WP_GRENADE_LAUNCHER: + case WP_GRENADE_PINEAPPLE: + return 800; + case WP_MONSTER_ATTACK1: + switch ( cs->aiCharacter ) { + case AICHAR_HEINRICH: + if ( cs->weaponFireTimes[weaponnum] < level.time - 8000 ) { + return 500; // lots of room for stomping + } else { + return 120; // come in real close + } + case AICHAR_HELGA: // helga BOSS1 melee + return 80; + case AICHAR_WARZOMBIE: + return 80; // make it larger so we can start swinging early, and move in while swinging + case AICHAR_LOPER: // close attack, head-butt, fist + return 60; + case AICHAR_BLACKGUARD: + return BLACKGUARD_MELEE_RANGE; + case AICHAR_STIMSOLDIER3: + return TESLA_RANGE; + case AICHAR_ZOMBIE: // zombie flaming attack + return ZOMBIE_FLAME_RADIUS - 50; // get well within range before starting + } + break; + case WP_MONSTER_ATTACK2: + switch ( cs->aiCharacter ) { + case AICHAR_HEINRICH: + return 8000; + case AICHAR_ZOMBIE: // zombie spirit attack + return 1000; + case AICHAR_HELGA: // zombie spirit attack + return 1900; + case AICHAR_LOPER: // loper leap attack + return 8000; // use it to gain on them also + } + break; + case WP_MONSTER_ATTACK3: + switch ( cs->aiCharacter ) { + case AICHAR_HEINRICH: // spirits + return 50000; + case AICHAR_LOPER: // loper ground attack + return LOPER_GROUND_RANGE; + case AICHAR_WARZOMBIE: // warzombie defense + return 2000; + case AICHAR_ZOMBIE: + return 44; + } + break; + + // Rafael added these changes as per Mikes request + case WP_MAUSER: + case WP_GARAND: + case WP_SNIPERRIFLE: + case WP_SNOOPERSCOPE: + return 8000; + break; + + + } + // default range + return 3000; +} + +/* +================== +AICast_CheckAttack_real +================== +*/ +qboolean AICast_CheckAttack_real( cast_state_t *cs, int enemy, qboolean allowHitWorld ) { + //float points; + vec3_t forward, right, start, end, dir, up, angles; + weaponinfo_t wi; + trace_t trace; + float traceDist; + static vec3_t smins = {-6, -6, -6}, smaxs = {6, 6, 6}; + static vec3_t fmins = {-30, -30, -24}, fmaxs = {30, 30, 24}; + float *mins, *maxs; + float halfHeight; + int traceMask; + int fuzzyCount, i; + gentity_t *ent, *enemyEnt; + float dist; + int passEnt; + int weapnum; + // + if ( enemy < 0 ) { + return qfalse; + } + ent = &g_entities[cs->entityNum]; + enemyEnt = &g_entities[enemy]; + // + if ( cs->bs ) { + weapnum = cs->weaponNum; + } else { + weapnum = ent->client->ps.weapon; + } + // + if ( !weapnum ) { + return qfalse; + } + // + // don't attack while in air (like on a ladder) + if ( !ent->waterlevel && ent->client->ps.groundEntityNum == ENTITYNUM_NONE && !ent->active ) { + // stim is allowed to fire while in air for flying attack + if ( !ent->client->ps.powerups[PW_FLIGHT] ) { + return qfalse; + } + } + // + if ( ent->health <= 0 ) { + return qfalse; + } + // can't attack without any ammo + if ( cs->bs ) { + if ( !AICast_GotEnoughAmmoForWeapon( cs, cs->weaponNum ) ) { + return qfalse; + } + } + // special case: warzombie should play laughing anim at first sight + if ( cs->aiCharacter == AICHAR_WARZOMBIE && weapnum == WP_MONSTER_ATTACK2 ) { + return qtrue; + } + // + //if the enemy isn't directly visible + if ( !allowHitWorld && cs->vislist[enemy].real_visible_timestamp != cs->vislist[enemy].real_update_timestamp ) { + return qfalse; + } + // + //get the weapon info (FIXME: hard-code the weapon info?) + memset( &wi, 0, sizeof( weaponinfo_t ) ); + // + traceMask = MASK_SHOT; // FIXME: assign mask's to different weapons + //end point aiming at + if ( !ent->active ) { + //get the start point shooting from + VectorCopy( enemyEnt->r.currentOrigin, start ); + start[2] += enemyEnt->client->ps.viewheight; + VectorCopy( ent->r.currentOrigin, end ); + end[2] += ent->client->ps.viewheight; + VectorSubtract( start, end, dir ); + vectoangles( dir, angles ); + AngleVectors( angles, forward, right, up ); + CalcMuzzlePoint( &g_entities[cs->entityNum], weapnum, forward, right, up, start ); + + traceDist = AICast_WeaponRange( cs, weapnum ); + switch ( weapnum ) { + case WP_GAUNTLET: + mins = NULL; + maxs = NULL; + break; + case WP_DYNAMITE: + case WP_PANZERFAUST: + case WP_GRENADE_LAUNCHER: + case WP_GRENADE_PINEAPPLE: + traceMask = MASK_MISSILESHOT; + mins = smins; + maxs = smaxs; + break; + case WP_FLAMETHROWER: + mins = fmins; + maxs = fmaxs; + break; + default: + mins = smins; + maxs = smaxs; + break; + } + passEnt = cs->entityNum; + + // don't try too far + dist = Distance( start, enemyEnt->r.currentOrigin ); + fuzzyCount = 6; + if ( traceDist > dist ) { + traceDist = dist; + } else { + dist -= enemyEnt->r.maxs[0]; // subtract distance to edge of bounding box edge + if ( traceDist < dist ) { + return qfalse; + } + } + } else { + gentity_t *mg42; + // we are mounted on a weapon + mg42 = &g_entities[cs->mountedEntity]; + VectorCopy( enemyEnt->r.currentOrigin, start ); + start[2] += enemyEnt->client->ps.viewheight; + VectorCopy( mg42->r.currentOrigin, end ); + VectorSubtract( start, end, dir ); + vectoangles( dir, angles ); + AngleVectors( angles, forward, right, up ); + + VectorCopy( mg42->r.currentOrigin, start ); + VectorMA( start, 16, forward, start ); + VectorMA( start, 16, up, start ); + // snap to integer coordinates for more efficient network bandwidth usage + SnapVector( start ); + + traceDist = 8192; + mins = NULL; + maxs = NULL; + if ( mg42->mg42BaseEnt >= 0 ) { + passEnt = mg42->mg42BaseEnt; + } else { + passEnt = cs->entityNum; + } + + // don't try too far + dist = Distance( start, enemyEnt->r.currentOrigin ); + if ( traceDist > dist ) { + traceDist = dist; + fuzzyCount = 6; + } else { //if (dist > traceDist - 32) { + return qfalse; + } + /*} else { + fuzzyCount = 0; + }*/ + } + + for ( i = 0; i <= fuzzyCount; i++ ) { + VectorMA( start, traceDist, forward, end ); + + // fuzzy end point + if ( i > 0 ) { + VectorMA( end, enemyEnt->r.maxs[0] * 0.9 * (float)( ( i % 2 ) * 2 - 1 ), right, end ); + halfHeight = ( enemyEnt->r.maxs[2] - enemyEnt->r.mins[2] ) / 2.0; + end[2] = ( enemyEnt->r.currentOrigin[2] + enemyEnt->r.mins[2] ) + halfHeight; + VectorMA( end, halfHeight * 0.9 * ( ( (float)( ( i - 1 ) - ( ( i - 1 ) % 2 ) ) / 2 - 1.0 ) ), up, end ); + } + + if ( /*allowHitWorld &&*/ !trap_InPVS( start, end ) ) { + // not possibly attackable + //continue; + return qfalse; + } + + trap_Trace( &trace, start, mins, maxs, end, passEnt, traceMask ); + if ( trace.fraction == 1.0 ) { + if ( !trace.startsolid ) { + return qtrue; // not sure why, but this fixes blackguards in chateau shooting through glass ceiling + } + //return qfalse; + continue; + } + //if won't hit the enemy + if ( trace.entityNum != enemy ) { + + // RF, assume we can shoot through props (chairs, etc) + if ( g_entities[trace.entityNum].takedamage && g_entities[trace.entityNum].health > 0 && + !Q_strncmp( g_entities[trace.entityNum].classname, "props_", 6 ) ) { + return qtrue; + } + + if ( !allowHitWorld ) { + continue; + } + + if ( trace.startsolid ) { + continue; + } + + //if the entity is a client + if ( trace.entityNum >= 0 && trace.entityNum < MAX_CLIENTS ) { + //if a teammate is hit + if ( AICast_SameTeam( cs, trace.entityNum ) ) { + return qfalse; + } + } + //if the projectile does a radial damage + if ( cs->weaponNum == WP_PANZERFAUST ) { + if ( Distance( trace.endpos, g_entities[enemy].s.pos.trBase ) > 120 ) { + continue; + } + //FIXME: check if a teammate gets radial damage + } + } + // will successfully hit enemy + return qtrue; + } + // + return qfalse; +} + +/* +================== +AICast_CheckAttackAtPos +================== +*/ +qboolean AICast_CheckAttackAtPos( int entnum, int enemy, vec3_t pos, qboolean ducking, qboolean allowHitWorld ) { + gentity_t *ent; + vec3_t savepos; + int saveview; + qboolean rval; + cast_state_t *cs; + + cs = AICast_GetCastState( entnum ); + ent = &g_entities[cs->bs->entitynum]; + + VectorCopy( ent->r.currentOrigin, savepos ); + VectorCopy( pos, ent->r.currentOrigin ); + + saveview = ent->client->ps.viewheight; + if ( ducking ) { + if ( ent->client->ps.viewheight != ent->client->ps.crouchViewHeight ) { + ent->client->ps.viewheight = ent->client->ps.crouchViewHeight; + } + } else { + if ( ent->client->ps.viewheight != ent->client->ps.standViewHeight ) { + ent->client->ps.viewheight = ent->client->ps.standViewHeight; + } + } + + rval = AICast_CheckAttack_real( cs, enemy, allowHitWorld ); + + VectorCopy( savepos, ent->r.currentOrigin ); + ent->client->ps.viewheight = saveview; + + return rval; +} + +/* +================== +AICast_CheckAttack + + optimization, uses the cache to avoid possible duplicate calls with same world paramaters +================== +*/ +qboolean AICast_CheckAttack( cast_state_t *cs, int enemy, qboolean allowHitWorld ) { + if ( cs->bs ) { + if ( ( cs->checkAttackCache.time == level.time ) + && ( cs->checkAttackCache.enemy == enemy ) + && ( cs->checkAttackCache.weapon == cs->weaponNum ) + && ( cs->checkAttackCache.allowHitWorld == allowHitWorld ) ) { + //G_Printf( "checkattack cache hit\n" ); + return ( cs->checkAttackCache.result ); + } else { + cs->checkAttackCache.allowHitWorld = allowHitWorld; + cs->checkAttackCache.enemy = enemy; + cs->checkAttackCache.time = level.time; + cs->checkAttackCache.weapon = cs->weaponNum; + return ( cs->checkAttackCache.result = AICast_CheckAttack_real( cs, enemy, allowHitWorld ) ); + } + } else { + return AICast_CheckAttack_real( cs, enemy, allowHitWorld ); + } +} + +/* +================== +AICast_UpdateBattleInventory +================== +*/ +void AICast_UpdateBattleInventory( cast_state_t *cs, int enemy ) { + vec3_t dir; + int i; + + if ( enemy >= 0 ) { + VectorSubtract( cs->vislist[cs->enemyNum].visible_pos, cs->bs->origin, dir ); + cs->enemyHeight = (int) dir[2]; + cs->enemyDist = (int) VectorLength( dir ); + } + + // stock up ammo that should never run out + for ( i = 0; i < WP_NUM_WEAPONS; i++ ) { + if ( ( i >= WP_MONSTER_ATTACK1 && i <= WP_MONSTER_ATTACK3 ) || ( g_entities[cs->bs->entitynum].client->ps.ammo[ BG_FindAmmoForWeapon( i )] > 800 ) ) { + //g_entities[cs->bs->entitynum].client->ps.ammo[ BG_FindAmmoForWeapon(i)] = 999; + Add_Ammo( &g_entities[cs->entityNum], i, 999, qfalse ); + } + } + + BotAI_GetClientState( cs->entityNum, &( cs->bs->cur_ps ) ); + +} + +/* +============== +AICast_WeaponWantScale +============== +*/ +float AICast_WeaponWantScale( cast_state_t *cs, int weapon ) { + switch ( weapon ) { + case WP_GAUNTLET: + return 0.1; + case WP_FLAMETHROWER: + return 2.0; // if we have this up close, definately use it + default: + return 1.0; + } +} + +/* +============== +AICast_GotEnoughAmmoForWeapon +============== +*/ +qboolean AICast_GotEnoughAmmoForWeapon( cast_state_t *cs, int weapon ) { + gentity_t *ent; + int ammo, clip; + + ent = &g_entities[cs->entityNum]; + ammo = ent->client->ps.ammo[BG_FindAmmoForWeapon( weapon )]; + clip = ent->client->ps.ammoclip[BG_FindClipForWeapon( weapon )]; + + // TODO!! check some kind of weapon list that holds the minimum requirements for each weapon + switch ( weapon ) { + case WP_GAUNTLET: + return qtrue; + default: + return (qboolean)( ( clip >= ammoTable[weapon].uses ) || ( ammo >= ammoTable[weapon].uses ) ); //----(SA) + } +} + +/* +============== +AICast_WeaponUsable + + This is used to prevent weapons from being selected, even if they have ammo. + + This can be used to add a delay between firing for special attacks, or make certain + that certain weapons are only selected within a certain range or under certain conditions. + + NOTE: that monster_attack2 will always override monster_attack1 if both are usable +============== +*/ +qboolean AICast_WeaponUsable( cast_state_t *cs, int weaponNum ) { + int delay, oldweap, hitclient; + float dist = -1; + gentity_t *ent, *grenade; + + if ( cs->enemyNum >= 0 ) { + dist = Distance( cs->bs->origin, g_entities[cs->enemyNum].s.pos.trBase ); + } + + oldweap = cs->weaponNum; + ent = &g_entities[cs->entityNum]; + delay = -1; + + // just return qfalse if this weapon isn't ready for use + switch ( weaponNum ) { + // don't attempt to lob a grenade more than this often, since we will abort a grenade + // throw if it's not safe, we shouldn't keep switching back too quickly + case WP_DYNAMITE: + case WP_GRENADE_LAUNCHER: + case WP_GRENADE_PINEAPPLE: + if ( cs->enemyNum < 0 ) { + return qfalse; + } + delay = 5000; + if ( dist > 0 && dist < 200 ) { + return qfalse; + } + if ( cs->weaponFireTimes[weaponNum] < level.time - delay ) { + // make sure it's safe + CalcMuzzlePoints( ent, weaponNum ); + grenade = weapon_grenadelauncher_fire( ent, weaponNum ); + hitclient = AICast_SafeMissileFire( grenade, grenade->nextthink - level.time, cs->enemyNum, g_entities[cs->enemyNum].s.pos.trBase, cs->entityNum, NULL ); + G_FreeEntity( grenade ); + if ( hitclient > -1 ) { + return qtrue; + } else { + return qfalse; // it's not safe + } + } + break; + case WP_TESLA: + switch ( cs->aiCharacter ) { + case AICHAR_STIMSOLDIER3: + if ( dist < 0 || dist >= TESLA_RANGE ) { + return qfalse; + } + } + break; + case WP_MONSTER_ATTACK1: + switch ( g_entities[cs->entityNum].aiCharacter ) { + case AICHAR_ZOMBIE: // zombie flaming attack + delay = 4000; + if ( dist < 0 ) { // || dist < 128) { + return qfalse; + } + if ( dist > 1200 ) { + return qfalse; + } + if ( cs->enemyNum < 0 ) { + return qfalse; + } + //if (cs->vislist[cs->enemyNum].notvisible_timestamp > level.time - 500) { + // return qfalse; + //} + break; + + // melee attacks are always available + case AICHAR_LOPER: + case AICHAR_WARZOMBIE: + return qtrue; // always usable + + case AICHAR_STIMSOLDIER2: + delay = 7000; + if ( dist < 0 || dist < 300 ) { + return qfalse; + } + break; + case AICHAR_STIMSOLDIER3: // stim flying tesla attack + delay = 7000; + if ( dist < 0 || dist < 300 ) { + return qfalse; + } + break; + case AICHAR_BLACKGUARD: + delay = 5000; + if ( dist < 0 || dist > BLACKGUARD_MELEE_RANGE ) { + return qfalse; + } + break; + default: + delay = -1; + break; + } + break; + case WP_MONSTER_ATTACK2: + switch ( g_entities[cs->entityNum].aiCharacter ) { + case AICHAR_HEINRICH: + delay = 6000; + break; + case AICHAR_WARZOMBIE: + delay = 9999999; + break; + case AICHAR_ZOMBIE: + delay = 6000; + // zombie "flying spirit" attack + if ( dist < 64 ) { + return qfalse; + } + if ( dist > 1200 ) { + return qfalse; + } + if ( cs->enemyNum < 0 ) { + return qfalse; + } + if ( cs->vislist[cs->enemyNum].notvisible_timestamp > level.time - 1500 ) { + return qfalse; + } + break; + case AICHAR_HELGA: + delay = 8000; + // zombie "flying spirit" attack + if ( dist < 0 || dist < 80 ) { + return qfalse; + } + if ( dist > 2000 ) { + return qfalse; + } + if ( cs->enemyNum < 0 ) { + return qfalse; + } + if ( cs->vislist[cs->enemyNum].notvisible_timestamp > level.time - 1500 ) { + return qfalse; + } + break; + case AICHAR_LOPER: // loper leap attack + if ( cs->bs->areanum && VectorLength( cs->bs->velocity ) > 1 ) { // if we are in a valid area, and are persuing, then leave a delay + // if there isn't a direct trace to our enemy, then fail + if ( cs->enemyNum >= 0 ) { + trace_t trace; + vec3_t mins; + VectorCopy( cs->bs->cur_ps.mins, mins ); + mins[0] = 0; + trap_Trace( &trace, g_entities[cs->entityNum].client->ps.origin, mins, cs->bs->cur_ps.maxs, g_entities[cs->enemyNum].client->ps.origin, cs->entityNum, g_entities[cs->entityNum].clipmask ); + if ( trace.entityNum != cs->enemyNum && trace.fraction < 1.0 ) { + return qfalse; + } + } + delay = 4500; + if ( dist < 200 ) { + return qfalse; + } + } else { + delay = 0; // jump to get out of trouble + } + break; + default: + delay = -1; + break; + } + break; + case WP_MONSTER_ATTACK3: + switch ( g_entities[cs->entityNum].aiCharacter ) { + case AICHAR_HEINRICH: // spirits + delay = 7000; + break; + case AICHAR_LOPER: // loper ground zap + delay = 3500; + if ( dist < 0 || dist > LOPER_GROUND_RANGE ) { + return qfalse; + } + break; + case AICHAR_WARZOMBIE: // warzombie defense + delay = 7000; + if ( dist < 120 || dist > 2000 ) { + return qfalse; + } + break; + case AICHAR_ZOMBIE: + return qtrue; // always usable + default: + delay = -1; + break; + } + break; + default: + delay = -1; + } + // + return ( !cs->weaponFireTimes[weaponNum] || ( cs->weaponFireTimes[weaponNum] < level.time - delay ) ); +} + +/* +============== +AICast_ChooseWeapon +============== +*/ +void AICast_ChooseWeapon( cast_state_t *cs, qboolean battleFunc ) { + int i; + int *ammo; + float wantScale, bestWantScale, enemyDist = 0; + qboolean inRange = qfalse, thisInRange, gotOne; + + BotAI_GetClientState( cs->entityNum, &( cs->bs->cur_ps ) ); + ammo = cs->bs->cur_ps.ammo; + bestWantScale = 0.0; + + if ( cs->enemyNum >= 0 ) { + enemyDist = VectorDistance( g_entities[cs->enemyNum].s.pos.trBase, cs->bs->origin ); + // subtract distance to edge of bounding box + enemyDist -= g_entities[cs->enemyNum].r.maxs[0]; + } + + if ( cs->bs->cur_ps.weaponstate == WEAPON_RAISING || + cs->bs->cur_ps.weaponstate == WEAPON_RAISING_TORELOAD || //----(SA) added + cs->bs->cur_ps.weaponstate == WEAPON_DROPPING || + cs->bs->cur_ps.weaponstate == WEAPON_DROPPING_TORELOAD ) { //----(SA) added + return; + } + +// disabled this, makes grenade guy keep trying to throw a grenade he doesn't have +// if (cs->bs->cur_ps.weaponDelay || cs->bs->cur_ps.weaponTime) +// return; + + if ( cs->weaponNum && ( cs->castScriptStatus.scriptFlags & SFL_NOCHANGEWEAPON ) ) { + if ( AICast_GotEnoughAmmoForWeapon( cs, cs->weaponNum ) && AICast_WeaponUsable( cs, cs->weaponNum ) ) { + return; + } else { + cs->castScriptStatus.scriptFlags &= ~SFL_NOCHANGEWEAPON; + } + } else { + if ( cs->weaponNum == WP_GRENADE_LAUNCHER || cs->weaponNum == WP_GRENADE_PINEAPPLE ) { + cs->weaponNum = WP_NONE; // dont use grenades at will + } + } + + gotOne = qfalse; + + // choose the best weapon to fight with + for ( i = 0; i < WP_NUM_WEAPONS; i++ ) { + if ( i == WP_GRENADE_LAUNCHER || i == WP_GRENADE_PINEAPPLE ) { + continue; // never choose grenades at will, only when going into grenade flush mode + } + + if ( !battleFunc && ( i == WP_MONSTER_ATTACK1 ) && cs->aifuncAttack1 ) { + continue; // only choose this weapon from within AIFunc_BattleStart() + } + if ( !battleFunc && ( i == WP_MONSTER_ATTACK2 ) && cs->aifuncAttack2 ) { + continue; // only choose this weapon from within AIFunc_BattleStart() + } + if ( !battleFunc && ( i == WP_MONSTER_ATTACK3 ) && cs->aifuncAttack3 ) { + continue; // only choose this weapon from within AIFunc_BattleStart() + } + + if ( COM_BitCheck( cs->bs->cur_ps.weapons, i ) ) { + gotOne = qtrue; + // check that our ammo is enough + if ( !AICast_GotEnoughAmmoForWeapon( cs, i ) || + !AICast_WeaponUsable( cs, i ) ) { + continue; + } + // get the wantScale for this weapon given the current circumstances (0.0 - 1.0) + wantScale = AICast_WeaponWantScale( cs, i ); + thisInRange = qfalse; + // in range? + if ( enemyDist && AICast_WeaponRange( cs, i ) > enemyDist ) { + thisInRange = qtrue; + } + // + if ( ( !inRange && thisInRange ) || ( ( !inRange || thisInRange ) && ( wantScale >= bestWantScale ) ) ) { + cs->weaponNum = i; + bestWantScale = wantScale; + if ( thisInRange ) { // we have found a weapon inside attackable range, don't override with one outside range + inRange = qtrue; + } + } + } + } + + if ( !gotOne && ( cs->weaponNum < WP_MONSTER_ATTACK1 || cs->weaponNum > WP_MONSTER_ATTACK3 ) ) { + if ( g_cheats.integer && ( !cs->bs->cur_ps.weapons[0] && !cs->bs->cur_ps.weapons[1] ) ) { +// (SA) the print statement is a bit much. lots of actors have no ammo... +// G_Printf( "AI: %s has no ammo\n", g_entities[cs->entityNum].aiName); + } + // select no weapon + cs->weaponNum = WP_NONE; + // if we have no weapons at all, we dont need to switch + if ( !cs->bs->cur_ps.weapons[0] && !cs->bs->cur_ps.weapons[1] ) { + g_entities[cs->entityNum].client->ps.weapon = WP_NONE; + } + } +} + +/* +================== +AICast_Aggression + + Check all possible reasons why we shouldn't attack, returning a value from 1.0 (fully willing) + to 0.0 (please don't hurt me). +================== +*/ +float AICast_Aggression( cast_state_t *cs ) { + bot_state_t *bs; + float scale, dist; + int painTime; + int *ammo; + + bs = cs->bs; + + // if we are out of ammo, we should never chase + ammo = cs->bs->cur_ps.ammo; + if ( g_entities[cs->entityNum].aiTeam != AITEAM_MONSTER ) { + if ( !AICast_GotEnoughAmmoForWeapon( cs, cs->weaponNum ) ) { + return 0; + } + } + + // start fully willing to attack + scale = 1.0; + + //if the enemy is located way higher + //if (cs->enemyHeight > 200) + // scale -= (cs->enemyHeight)/800.0; + + //if very low on health + if ( bs->cur_ps.stats[STAT_HEALTH] < 50 ) { + scale -= ( 1.0 - cs->attributes[AGGRESSION] ) * ( 1.0 - ( (float)bs->cur_ps.stats[STAT_HEALTH] / 50.0 ) ); + } + + // if they've recently hit us, factor that in, so we get scared off by being + // damaged, but later return once we've regained our confidence + painTime = 15000 - (int)( 10000.0 * cs->attributes[AGGRESSION] * cs->attributes[AGGRESSION] ); + if ( cs->lastPain + painTime > level.time ) { + scale -= 3 * ( 1.0 - cs->attributes[AGGRESSION] ) * ( (float)( cs->lastPain + painTime - level.time ) / (float)painTime ); + } + + // if we just rolled, stay out of view if we jumped behind cover + painTime = 10000 - (int)( 10000.0 * cs->attributes[AGGRESSION] * cs->attributes[AGGRESSION] ); + if ( cs->battleRollTime + painTime > level.time ) { + scale -= 2 * ( 1.0 - cs->attributes[AGGRESSION] ) * ( (float)( cs->battleRollTime + painTime - level.time ) / (float)painTime ); + } + + // gain in confidence the further we are away + if ( cs->enemyNum >= 0 ) { + dist = Distance( cs->bs->origin, g_entities[cs->enemyNum].s.pos.trBase ); + //if (dist > 512) { + scale += ( dist - 800.0 ) / ( 8000.0 ); + //} + } + + // if our weapon is reloading, we should hide + if ( cs->bs->cur_ps.weaponTime > 0 ) { + scale -= ( (float)cs->bs->cur_ps.weaponTime / 1000.0 ); + } + + scale *= cs->attributes[AGGRESSION]; + + // this should increase the chances of an ambush attack + if ( cs->entityNum >= 0 && ( ( level.time + 2000 * g_entities[cs->entityNum].aiTeam ) % ( 4000 + 500 * g_entities[cs->entityNum].aiTeam ) ) > 4000 ) { + if ( cs->vislist[cs->entityNum].visible_timestamp > level.time - 10000 ) { + scale += 0.3 * (float)( level.time - cs->vislist[cs->entityNum].visible_timestamp ) / 10000.0; + } + } + + if ( scale < 0 ) { + scale = 0; + } + + return scale; +} + +/* +================== +AICast_WantsToChase +================== +*/ +int AICast_WantsToChase( cast_state_t *cs ) { + int *ammo; + ammo = cs->bs->cur_ps.ammo; + if ( g_entities[cs->entityNum].aiTeam != AITEAM_MONSTER ) { + if ( !AICast_GotEnoughAmmoForWeapon( cs, cs->weaponNum ) ) { + return qfalse; + } + } + if ( cs->attributes[AGGRESSION] == 1.0 ) { + return qtrue; + } + if ( AICast_Aggression( cs ) > 0.6 ) { + return qtrue; + } + return qfalse; +} + +/* +================== +AICast_WantsToTakeCover +================== +*/ +int AICast_WantsToTakeCover( cast_state_t *cs, qboolean attacking ) { + float aggrScale; + int *ammo; + + ammo = cs->bs->cur_ps.ammo; + if ( g_entities[cs->entityNum].aiTeam != AITEAM_MONSTER ) { + if ( !cs->weaponNum ) { + return qtrue; + } + if ( !AICast_GotEnoughAmmoForWeapon( cs, cs->weaponNum ) ) { + return qtrue; + } + } + if ( cs->attributes[AGGRESSION] == 1.0 ) { + return qfalse; + } + // if currently attacking, we should stick around if not getting hurt + if ( attacking ) { + aggrScale = 1.2; + } else { aggrScale = 0.8 /*+ 0.4 * random()*/;} + // + // if currently following someone, we should be more aggressive + if ( cs->leaderNum >= 0 ) { + aggrScale *= 3; + } + // + // Dodge enemy aim? + if ( cs->attributes[AGGRESSION] < 1.0 && attacking && ( cs->enemyNum >= 0 ) && ( g_entities[cs->enemyNum].client->ps.weapon ) && ( cs->attributes[TACTICAL] > 0.5 ) && ( cs->aiFlags & AIFL_ROLL_ANIM ) && ( VectorLength( cs->bs->cur_ps.velocity ) < 1 ) ) { + vec3_t aim, enemyVec; + // are they aiming at us? + AngleVectors( g_entities[cs->enemyNum].client->ps.viewangles, aim, NULL, NULL ); + VectorSubtract( cs->bs->origin, g_entities[cs->enemyNum].r.currentOrigin, enemyVec ); + VectorNormalize( enemyVec ); + // if they are looking at us, we should avoid them + if ( DotProduct( aim, enemyVec ) > 0.97 ) { + //G_Printf("%s: I'm in danger, I should probably avoid\n", g_entities[cs->entityNum].aiName); + aggrScale *= 0.6; + } + } + // + // FIXME: instead of a constant, call a "attack danger" + // function, so we only attack if our aggression is greater than + // the danger + if ( AICast_Aggression( cs ) * aggrScale < 0.4 ) { + //G_Printf("%s: run for your life!\n", g_entities[cs->entityNum].aiName); + return qtrue; + } + // + return qfalse; +} + +/* +================== +AICast_CombatMove +================== +*/ +bot_moveresult_t AICast_CombatMove( cast_state_t *cs, int tfl ) { + bot_state_t *bs; + float attack_skill, croucher, dist; + vec3_t forward, backward; //, up = {0, 0, 1}; + bot_moveresult_t moveresult; + bot_goal_t goal; + + bs = cs->bs; + + //get the enemy entity info + memset( &moveresult, 0, sizeof( bot_moveresult_t ) ); + // + attack_skill = cs->attributes[ATTACK_SKILL]; + croucher = ( cs->attributes[ATTACK_CROUCH] > 0.1 ); + + //initialize the movement state + BotSetupForMovement( bs ); + //direction towards the enemy + VectorSubtract( cs->vislist[cs->enemyNum].visible_pos, bs->origin, forward ); + //the distance towards the enemy + dist = VectorNormalize( forward ); + VectorNegate( forward, backward ); + // + // do we have somewhere we are trying to get to? + if ( cs->combatGoalTime > level.time ) { + if ( VectorLength( cs->combatGoalOrigin ) > 1 ) { + //create the chase goal + goal.areanum = BotPointAreaNum( cs->combatGoalOrigin ); + VectorCopy( cs->combatGoalOrigin, goal.origin ); + + // if we are really close, stop going for it + // FIXME: a better way of doing this, so we don't stop short of the goal? + if ( ( dist = Distance( goal.origin, cs->bs->origin ) ) < 32 ) { + if ( cs->combatGoalTime > level.time + 3000 ) { + cs->combatGoalTime = level.time + 2000 + rand() % 1000; + cs->combatSpotDelayTime = level.time + 4000 + rand() % 3000; + } + VectorClear( cs->combatGoalOrigin ); + } else { + aicast_predictmove_t move; + // + AICast_MoveToPos( cs, cs->combatGoalOrigin, -1 ); + cs->speedScale = AICast_SpeedScaleForDistance( cs, dist, 32 ); + // + // if we are going to move out of view very soon, stop moving + AICast_PredictMovement( cs, 1, 0.8, &move, &cs->lastucmd, -1 ); + // + if ( move.numtouch || !AICast_CheckAttackAtPos( cs->entityNum, cs->enemyNum, move.endpos, qfalse, qfalse ) ) { + // abort the manouver, reached a good spot + cs->combatGoalTime = 0; + cs->combatSpotAttackCount = cs->startAttackCount; + } + } + + // if we are there, and the enemy can see us, but we cant hit them, abort immediately + } else if ( !AICast_CheckAttack( cs, cs->enemyNum, qfalse ) && + AICast_VisibleFromPos( cs->vislist[cs->enemyNum].visible_pos, cs->enemyNum, cs->bs->origin, cs->entityNum, qfalse ) ) { + cs->combatGoalTime = 0; + cs->combatSpotAttackCount = cs->startAttackCount; + } + } else { // look for a good position to move to? + if ( ( ( cs->attributes[CAMPER] < random() ) + && ( cs->takeCoverTime < level.time ) + && ( cs->combatSpotAttackCount < cs->startAttackCount ) + && ( cs->combatSpotDelayTime < level.time ) ) ) { + + if ( ( cs->attributes[TACTICAL] > 0.3 + random() * 0.5 ) + && trap_AAS_RT_GetHidePos( cs->bs->origin, cs->bs->entitynum, cs->bs->areanum, cs->vislist[cs->enemyNum].visible_pos, cs->enemyNum, BotPointAreaNum( cs->vislist[cs->enemyNum].visible_pos ), cs->combatGoalOrigin ) ) { + cs->combatGoalTime = level.time + 10000; // give us plenty of time to get there + //cs->combatSpotAttackCount = cs->startAttackCount + 3; // don't keep moving around to different positions on our own + cs->combatSpotDelayTime = level.time + 3000 + rand() % 3000; + } else { + // don't look again until we've moved + //cs->combatSpotAttackCount = cs->startAttackCount; + cs->combatSpotDelayTime = level.time + 3000 + rand() % 3000; + } + } + } + // + return moveresult; +} + +/* +================== +AICast_WeaponSway + + Some weapons should be "sprayed" around a bit while firing +================== +*/ +void AICast_WeaponSway( cast_state_t *cs, vec3_t ofs ) { + VectorClear( ofs ); + switch ( cs->weaponNum ) { + case WP_MONSTER_ATTACK1: + if ( cs->aiCharacter != AICHAR_ZOMBIE ) { + break; // only allow flaming zombie beyond here + } + case WP_FLAMETHROWER: + ofs[PITCH] = ( 3.0 + 4.0 * sin( ( (float)level.time / 320.0 ) ) ) * sin( ( (float)level.time / 500.0 ) ); + ofs[YAW] = ( 6.0 + 8.0 * sin( ( (float)level.time / 250.0 ) ) ) * sin( ( (float)level.time / 400.0 ) ); + ofs[ROLL] = 0; + break; + case WP_VENOM: + ofs[PITCH] = 2 * (float)cos( ( level.time / 200 ) ); + ofs[YAW] = 10 * (float)sin( ( level.time / 150 ) ) * (float)sin( ( level.time / 100 ) ); + ofs[ROLL] = 0; + break; + } +} + +/* +================== +AICast_AimAtEnemy +================== +*/ +qboolean AICast_AimAtEnemy( cast_state_t *cs ) { + bot_state_t *bs; + float aim_skill, aim_accuracy; + vec3_t dir, bestorigin, start, enemyOrg; +// vec3_t mins = {-4,-4,-4}, maxs = {4, 4, 4}; + float dist; + cast_visibility_t *vis; + + // + if ( cs->castScriptStatus.scriptNoAttackTime >= ( level.time + 500 ) ) { + return qfalse; + } + // + if ( cs->noAttackTime >= level.time ) { + return qfalse; + } + // + bs = cs->bs; + // + //if the bot has no enemy + if ( cs->enemyNum < 0 ) { + return qfalse; + } + // + aim_skill = cs->attributes[AIM_SKILL]; + aim_accuracy = AICast_GetAccuracy( cs->entityNum ); + if ( aim_accuracy <= 0 ) { + aim_accuracy = 0.0001; + } + + // StimSoldier is very good at firing Rocket Launcher + if ( cs->aiCharacter == AICHAR_STIMSOLDIER2 && cs->weaponNum == WP_PANZERFAUST ) { + aim_skill = 1; + aim_accuracy = 1; + } + + //get the weapon information + + //get the enemy entity information + vis = &cs->vislist[cs->enemyNum]; + if ( vis->visible_timestamp < vis->lastcheck_timestamp ) { + // use our last visible position of them + if ( vis->real_visible_timestamp == vis->lastcheck_timestamp ) { + VectorCopy( vis->real_visible_pos, bestorigin ); + } else { + VectorCopy( vis->visible_pos, bestorigin ); + } + } else { + // we can see them, if this weapon isn't a direct-hit weapon (bullets), + // then predict where they are going to be + if ( cs->weaponNum == WP_GRENADE_LAUNCHER || cs->weaponNum == WP_GRENADE_PINEAPPLE ) { + aicast_predictmove_t move; + AICast_PredictMovement( AICast_GetCastState( cs->enemyNum ), 1, 1.0, &move, &g_entities[cs->enemyNum].client->pers.cmd, -1 ); + VectorCopy( move.endpos, bestorigin ); + } else { // they are visible, use actual position + VectorCopy( g_entities[cs->enemyNum].client->ps.origin, bestorigin ); + } + } + + bestorigin[2] += g_entities[cs->enemyNum].client->ps.viewheight; + //get the start point shooting from + //NOTE: the x and y projectile start offsets are ignored + VectorCopy( bs->origin, start ); + start[2] += bs->cur_ps.viewheight; + // + VectorCopy( bestorigin, enemyOrg ); + // + // grenade hack: aim grenades at their feet if they are close + if ( cs->weaponNum == WP_GRENADE_LAUNCHER || cs->weaponNum == WP_GRENADE_PINEAPPLE ) { + if ( Distance( start, bestorigin ) < 180 ) { + bestorigin[2] = enemyOrg[2] + g_entities[cs->enemyNum].r.mins[2] + crandom() * 20; + } else if ( Distance( start, bestorigin ) > 400 ) { // aim up a bit for distance + bestorigin[2] += 12 + Distance( start, bestorigin ) / 50 + crandom() * 20; + } + } + dist = Distance( bs->eye, bestorigin ); + // rocket launcher should aim ahead + if ( cs->weaponNum == WP_PANZERFAUST ) { + VectorMA( bestorigin, aim_skill * aim_skill * ( dist / 900 ), g_entities[cs->enemyNum].client->ps.velocity, bestorigin ); + // if they are close, aim down at their feet + if ( dist < 512 ) { + bestorigin[2] -= ( VectorLength( g_entities[cs->enemyNum].client->ps.velocity ) / 500.0 ) * ( 1.0 - dist / 2048 ) * ( bestorigin[2] - ( g_entities[cs->enemyNum].client->ps.origin[2] + g_entities[cs->enemyNum].client->ps.mins[2] ) ); + } + } + // if the enemy is moving, they are harder to hit + if ( dist > 256 ) { + VectorMA( bestorigin, ( 0.3 + 0.7 * ( 1 - aim_accuracy ) ) * 0.4 * sin( (float)level.time / ( 500.0 + ( 100.0 * ( ( cs->entityNum + 3 ) % 4 ) ) ) ), g_entities[cs->enemyNum].client->ps.velocity, bestorigin ); + } + // if we are good at aiming, we should aim ahead of where they are now + // since by the time we have rotated to that direction, some time will have passed + if ( aim_skill > 0.2 ) { + VectorMA( bestorigin, aim_skill * 0.2, g_entities[cs->enemyNum].client->ps.velocity, bestorigin ); + } + //get aim direction + VectorSubtract( bestorigin, bs->eye, dir ); + //set the ideal view angles + vectoangles( dir, cs->ideal_viewangles ); + + return qtrue; // do real aim checking after we've moved the angles +} + +/* +================== +AICast_CanMoveWhileFiringWeapon +================== +*/ +qboolean AICast_CanMoveWhileFiringWeapon( int weaponnum ) { + switch ( weaponnum ) { + case WP_MAUSER: + case WP_GARAND: + case WP_SNIPERRIFLE: //----(SA) added + case WP_SNOOPERSCOPE: //----(SA) added + //case WP_FG42SCOPE: //----(SA) added + case WP_PANZERFAUST: + return qfalse; + default: + return qtrue; + } +} + +/* +================ +AICast_RandomTriggerRelease +================ +*/ +qboolean AICast_RandomTriggerRelease( cast_state_t *cs ) { + // some characters override all weapon settings for trigger release + switch ( cs->aiCharacter ) { + case AICHAR_BLACKGUARD: // this is here since his "ready" frame is different to his firing frame, so it looks wierd to keep swapping between them + case AICHAR_STIMSOLDIER1: + case AICHAR_STIMSOLDIER2: + case AICHAR_STIMSOLDIER3: + return qfalse; + } + + switch ( cs->weaponNum ) { + case WP_MP40: + case WP_VENOM: + case WP_FG42SCOPE: + case WP_FG42: + //case WP_FLAMETHROWER: + return qtrue; + default: + return qfalse; + } +} + +/* +================== +AICast_ProcessAttack + + NOTE: this should always be called after the movement has been processed +================== +*/ +void AICast_ProcessAttack( cast_state_t *cs ) { + bot_state_t *bs; + + // if our enemy is dead, stop attacking them + if ( cs->enemyNum >= 0 && g_entities[cs->enemyNum].health <= 0 ) { + return; + } + // + if ( cs->castScriptStatus.scriptNoAttackTime >= level.time ) { + return; + } + // + if ( cs->noAttackTime >= level.time ) { + return; + } + // select a weapon + AICast_ChooseWeapon( cs, qfalse ); + // + if ( cs->weaponNum == WP_NONE ) { + return; + } + // never fire grenades from within here (needs special AIFunc_GrenadeFlush() code) + if ( cs->weaponNum == WP_GRENADE_LAUNCHER || cs->weaponNum == WP_GRENADE_PINEAPPLE ) { + return; + } + // + bs = cs->bs; + // check for not firing while moving flag, if present, abort attack if any movement has been issued + if ( !AICast_CanMoveWhileFiringWeapon( cs->weaponNum ) ) { + // if we are moving, don't fire + bot_input_t bi; + if ( cs->bs->cur_ps.weaponTime > 200 ) { + // if we recently fired, don't let us move for a bit + cs->speedScale = 0; + AICast_AimAtEnemy( cs ); // keep looking at them regardless + } + // if we're trying to move somewhere, don't let us shoot, until we've arrived + trap_EA_GetInput( bs->client, (float) level.time / 1000, &bi ); + if ( ( cs->castScriptStatus.scriptNoMoveTime < level.time ) && + ( ( bi.actionflags & ACTION_MOVEFORWARD ) || + ( bi.actionflags & ACTION_MOVEBACK ) || + ( bi.actionflags & ACTION_MOVELEFT ) || + ( bi.actionflags & ACTION_MOVERIGHT ) || + ( bi.speed ) ) ) { + return; + } + } + // + //if not a "walk forward" AI, then aim at the enemy regardless of whether we can attack them or not + if ( !( cs->aiFlags & AIFL_WALKFORWARD ) ) { + if ( !AICast_AimAtEnemy( cs ) ) { + return; + } + } + // + // if we are stuck in this position, we should duck if we can't hit them + if ( !AICast_CheckAttack( cs, cs->enemyNum, qfalse ) ) { + // we should duck if the enemy is shooting at us, and we can't hit them + if ( cs->attributes[ATTACK_CROUCH] && ( cs->castScriptStatus.scriptNoMoveTime >= level.time ) ) { + if ( !AICast_CheckAttackAtPos( cs->entityNum, cs->enemyNum, cs->bs->origin, qfalse, qfalse ) ) { + cs->attackcrouch_time = level.time + 2000; + } else { + cs->attackcrouch_time = 0; // we can attack them if we stand, so go for it + } + } + return; + } + // + //if we are a "walk forward" AI, then aim at the enemy only if we can attack them + if ( cs->aiFlags & AIFL_WALKFORWARD ) { + if ( !AICast_AimAtEnemy( cs ) ) { + return; + } + } + // + // release the trigger every now and then + if ( AICast_RandomTriggerRelease( cs ) && cs->triggerReleaseTime < ( level.time - 500 ) ) { + if ( rand() % 5 == 0 ) { + cs->triggerReleaseTime = level.time + 100 + rand() % 100; + return; + } + } + // + if ( cs->triggerReleaseTime > level.time ) { + return; + } + // + // FIXME: handle fire-on-release weapons? + trap_EA_Attack( bs->client ); + // + cs->bFlags |= BFL_ATTACKED; + +} + +/* +============== +AICast_GetTakeCoverPos +============== +*/ +qboolean AICast_GetTakeCoverPos( cast_state_t *cs, int enemyNum, vec3_t enemyPos, vec3_t returnPos ) { + cs->crouchHideFlag = qfalse; + // + if ( cs->castScriptStatus.scriptNoMoveTime > level.time ) { + return qfalse; + } + // + cs->lastGetHidePos = level.time; + // + // can we just crouch? + if ( ( cs->attackcrouch_time < level.time ) + && ( enemyNum < aicast_maxclients ) + && AICast_CheckAttackAtPos( cs->entityNum, enemyNum, cs->bs->origin, qfalse, qfalse ) + && !AICast_CheckAttackAtPos( cs->entityNum, enemyNum, cs->bs->origin, qtrue, qfalse ) ) { + + // do a more thorough check to see if the enemy can see us if we crouch + vec3_t omaxs; + gentity_t *ent; + qboolean visible; + + ent = &g_entities[cs->entityNum]; + VectorCopy( ent->r.maxs, omaxs ); + ent->r.maxs[2] = ent->client->ps.crouchMaxZ + 4; // + 4 to be safe + + visible = AICast_VisibleFromPos( g_entities[enemyNum].r.currentOrigin, enemyNum, cs->bs->origin, cs->entityNum, qfalse ); + + ent->r.maxs[2] = omaxs[2]; + + if ( !visible ) { + VectorCopy( enemyPos, cs->takeCoverEnemyPos ); + VectorCopy( cs->bs->origin, returnPos ); + cs->crouchHideFlag = qtrue; + return qtrue; + } + } + // if we are in a void, then we can't hide + // look for a hiding spot + if ( cs->bs->areanum && trap_AAS_RT_GetHidePos( cs->bs->origin, cs->bs->entitynum, cs->bs->areanum, enemyPos, enemyNum, BotPointAreaNum( enemyPos ), returnPos ) ) { + return qtrue; + } + // if we are hiding from a dangerous entity, try and avoid it + if ( cs->dangerEntity == enemyNum && cs->dangerEntityValidTime > level.time ) { + if ( cs->dangerLastGetAvoid > level.time - 750 ) { + return qtrue; + } else if ( AICast_GetAvoid( cs, NULL, cs->takeCoverPos, qtrue, cs->dangerEntity ) ) { + cs->dangerLastGetAvoid = level.time; + return qtrue; + } + } + // + return qfalse; +} + +/* +============== +AICast_AIDamageOK +============== +*/ +qboolean AICast_AIDamageOK( cast_state_t *cs, cast_state_t *ocs ) { + if ( cs->castScriptStatus.scriptFlags & SFL_NOAIDAMAGE ) { + return qfalse; + } else { + + if ( cs->aiCharacter == AICHAR_LOPER && ocs->aiCharacter == AICHAR_LOPER ) { + return qfalse; + } + + return qtrue; + } +} + +/* +=============== +AICast_RecordWeaponFire + + used for scripting, so we know when the weapon has been fired +=============== +*/ +void AICast_RecordWeaponFire( gentity_t *ent ) { + cast_state_t *cs; + float range; + + cs = AICast_GetCastState( ent->s.number ); + cs->lastWeaponFired = level.time; + cs->lastWeaponFiredWeaponNum = ent->client->ps.weapon; + VectorCopy( ent->r.currentOrigin, cs->lastWeaponFiredPos ); + + cs->weaponFireTimes[cs->lastWeaponFiredWeaponNum] = level.time; + + // do sighting + range = AICast_GetWeaponSoundRange( cs->lastWeaponFiredWeaponNum ); + + AICast_AudibleEvent( cs->entityNum, cs->lastWeaponFiredPos, range ); + + if ( cs->bs ) { // real player's don't need to play AI sounds + AIChar_AttackSound( cs ); + } +} + +/* +=============== +AICast_GetWeaponSoundRange +=============== +*/ +float AICast_GetWeaponSoundRange( int weapon ) { + // NOTE: made this a case, that way changing the ordering of weapons won't cause problems, as it would + // with an array lookup + + switch ( weapon ) { + case WP_NONE: + return 0; + case WP_KNIFE: + case WP_GAUNTLET: + case WP_STEN: + case WP_SILENCER: + return 64; + case WP_GRENADE_LAUNCHER: + case WP_GRENADE_PINEAPPLE: + return 1500; + case WP_GARAND: + case WP_SNOOPERSCOPE: + return 128; + case WP_LUGER: + case WP_COLT: + case WP_AKIMBO: + return 700; + + case WP_MONSTER_ATTACK1: + case WP_MONSTER_ATTACK2: + case WP_MONSTER_ATTACK3: + // TODO: case for each monster + return 1000; + + case WP_MP40: + case WP_THOMPSON: + return 1000; + + case WP_FG42: + case WP_FG42SCOPE: + return 1500; + + case WP_SNIPERRIFLE: + case WP_MAUSER: + return 2000; + + case WP_DYNAMITE: + return 3000; + + case WP_PANZERFAUST: + case WP_VENOM: + case WP_FLAMETHROWER: + case WP_TESLA: + return 1000; + } + + G_Error( "AICast_GetWeaponSoundRange: unknown weapon index: %i\n", weapon ); + return 0; // shutup the compiler +} + +/* +=============== +AICast_StopAndAttack + + returns qtrue if they should go back to a battle state to attack, + qfalse if they should keep chasing while they attack (like the Zombie) +=============== +*/ +qboolean AICast_StopAndAttack( cast_state_t *cs ) { + float dist = -1; + cast_state_t *ecs; + + if ( cs->enemyNum >= 0 ) { + dist = Distance( cs->bs->origin, g_entities[cs->enemyNum].r.currentOrigin ); + } + + switch ( cs->weaponNum ) { + + // if they are using Venom, and are too far away to be effective, then keep chasing + case WP_VENOM: + if ( dist > 300 ) { + return qfalse; + } + // if we haven't injured them in a while, advance + if ( cs->enemyNum >= 0 ) { + ecs = AICast_GetCastState( cs->enemyNum ); + if ( ecs->lastPain < level.time - 3000 ) { + return qfalse; + } + } + break; + // if they are using tesla (SUPERSOLDIER / BOSS2) try and get close + case WP_TESLA: + if ( dist > 128 /*&& (level.time%10000 < 8000)*/ ) { + return qfalse; + } + // if we haven't injured them in a while, advance + if ( cs->enemyNum >= 0 ) { + ecs = AICast_GetCastState( cs->enemyNum ); + if ( ecs->lastPain < level.time - 3000 ) { + return qfalse; + } + } + break; + case WP_PANZERFAUST: + // if we haven't injured them in a while, advance + if ( ( cs->aiCharacter == AICHAR_PROTOSOLDIER || cs->aiCharacter == AICHAR_SUPERSOLDIER ) && cs->enemyNum >= 0 ) { + if ( dist > 300 ) { + return qfalse; + } + ecs = AICast_GetCastState( cs->enemyNum ); + if ( ecs->lastPain < level.time - 3000 ) { + return qfalse; + } + } + break; + case WP_FLAMETHROWER: + // if we haven't injured them in a while, advance + if ( cs->aiCharacter == AICHAR_VENOM && cs->enemyNum >= 0 ) { + ecs = AICast_GetCastState( cs->enemyNum ); + if ( ecs->lastPain < level.time - 3000 ) { + return qfalse; + } + } + break; + + } + + return qtrue; +} + +/* +=============== +AICast_GetAccuracy +=============== +*/ +float AICast_GetAccuracy( int entnum ) { + #define AICAST_VARIABLE_ACC_ENABLED 1 + #define AICAST_ACC_VISTIME ( 500 + ( 3500 * ( 1.0 - aicast_skillscale ) ) ) + #define AICAST_ACC_SCALE 0.4 + cast_state_t *cs; + float acc; + + cs = AICast_GetCastState( entnum ); + // the more they stay in our sights, the more accurate we get + acc = cs->attributes[AIM_ACCURACY]; + + if ( AICAST_VARIABLE_ACC_ENABLED ) { + if ( cs->enemyNum >= 0 ) { + if ( cs->vislist[cs->enemyNum].real_notvisible_timestamp < level.time - AICAST_ACC_VISTIME ) { + acc += 0.5 * AICAST_ACC_SCALE; + } else { + acc += AICAST_ACC_SCALE * ( (float)( -0.5 * AICAST_ACC_VISTIME + level.time - cs->vislist[cs->enemyNum].real_notvisible_timestamp ) / (float)( AICAST_ACC_VISTIME ) ); + } + + if ( acc > 1.0 ) { + acc = 1.0; + } else if ( acc < 0.0 ) { + acc = 0.0; + } + } + } + return ( acc ); +} + +/* +============== +AICast_WantToRetreat +============== +*/ +qboolean AICast_WantToRetreat( cast_state_t *cs ) { + int *ammo; + + ammo = cs->bs->cur_ps.ammo; + if ( g_entities[cs->entityNum].aiTeam != AITEAM_MONSTER ) { + if ( !cs->weaponNum ) { + return qtrue; + } + if ( !AICast_GotEnoughAmmoForWeapon( cs, cs->weaponNum ) ) { + return qtrue; + } + } + + if ( cs->attributes[AGGRESSION] >= 1.0 && cs->attributes[TACTICAL] <= 0.2 ) { + return qfalse; + } + + // RF, (Last Minute Hack) big dudes should never retreat + if ( cs->aasWorldIndex != 0 ) { + return qfalse; + } + + if ( cs->leaderNum < 0 ) { + if ( ( cs->attributes[TACTICAL] > 0.11 + random() * 0.5 ) && + ( ( cs->bs->cur_ps.weaponTime > 500 ) || + ( ( cs->takeCoverTime < level.time - 100 ) && + ( AICast_WantsToTakeCover( cs, qtrue ) ) ) ) ) { + return qtrue; + } + } + // + return qfalse; +} + +/* +============== +AICast_SafeMissileFire + + checks to see if firing the missile will be successful, neutral, or dangerous to us or a friendly +============== +*/ +int AICast_SafeMissileFire( gentity_t *ent, int duration, int enemyNum, vec3_t enemyPos, int selfNum, vec3_t endPos ) { + int rval; + vec3_t org; + gentity_t *trav; + + if ( !G_PredictMissile( ent, duration, org, qtrue ) ) { + // not point firing, since it won't explode + return 0; + } + + if ( endPos ) { + VectorCopy( org, endPos ); + } + + // at end of life, so do radius damage + rval = ( Distance( org, enemyPos ) < ent->splashRadius ) && AICast_VisibleFromPos( org, ent->s.number, enemyPos, enemyNum, qfalse ); + if ( rval ) { + // don't hurt ourselves + if ( Distance( org, g_entities[selfNum].r.currentOrigin ) < ent->splashRadius * 1.5 ) { + return -1; + } + // make sure we don't injure a friendly + for ( trav = g_entities; trav < g_entities + g_maxclients.integer; trav++ ) { + if ( !trav->inuse ) { + continue; + } + if ( !trav->client ) { + continue; + } + if ( trav->health <= 0 ) { + continue; + } + if ( trav->s.number == selfNum ) { + continue; + } + if ( AICast_SameTeam( AICast_GetCastState( selfNum ), trav->s.number ) ) { + if ( Distance( org, trav->r.currentOrigin ) < ent->splashRadius ) { + return -1; + } + } + } + } + // if it overshot the mark + if ( !rval && Distance( g_entities[ent->r.ownerNum].r.currentOrigin, org ) > Distance( g_entities[ent->r.ownerNum].r.currentOrigin, enemyPos ) ) { + return -2; // so the AI can try aiming down a bit next time + } + // + return rval; +} + +/* +============= +AICast_CheckDangerousEntity + + check to see if the given entity can harm an AI character, if so, informs them + appropriately +============= +*/ +void AICast_CheckDangerousEntity( gentity_t *ent, int dangerFlags, float dangerDist, float tacticalLevel, float aggressionLevel, qboolean hurtFriendly ) { + vec3_t org, fwd, vec; + cast_state_t *cs, *dcs; + gentity_t *trav; + int i, endTime; + float dist; + // + // + if ( dangerFlags & DANGER_MISSILE ) { + // predict where the entity will explode + if ( !( endTime = G_PredictMissile( ent, ent->nextthink - level.time, org, qtrue ) ) ) { + return; // missile won't explode, so no danger + } + } else { + // just avoid it for a bit, then forget it + endTime = level.time + 1000; + VectorCopy( ent->r.currentOrigin, org ); + } + if ( dangerFlags & DANGER_CLIENTAIM ) { + AngleVectors( ent->client->ps.viewangles, fwd, NULL, NULL ); + } + // + if ( ent->client ) { + dcs = AICast_GetCastState( ent->s.number ); + } else { + dcs = NULL; + } + // + // see if this will hurt anyone + for ( trav = g_entities, cs = AICast_GetCastState( 0 ), i = 0; i < level.numPlayingClients; cs++, trav++ ) { + if ( !trav->inuse || !trav->client ) { + continue; + } + i++; // found a connected client + if ( trav == ent ) { // don't be afraid of ourself + continue; + } + if ( trav->health <= 0 ) { + continue; + } + if ( !cs->bs ) { // not an AI, they should look out for themselves + continue; + } + if ( cs->castScriptStatus.scriptNoSightTime >= level.time ) { + continue; // absolutely no sight (or hear) information allowed + } + if ( !hurtFriendly && ent->s.number < MAX_CLIENTS && AICast_SameTeam( cs, ent->s.number ) ) { + continue; // trust that friends will not hurt us + } + if ( ( dangerFlags & DANGER_FLAMES ) && ( cs->aiFlags & AIFL_NO_FLAME_DAMAGE ) ) { + continue; // venom not effected by flames + } + if ( cs->attributes[TACTICAL] < tacticalLevel ) { // not smart enough + continue; + } + if ( cs->aiState >= AISTATE_COMBAT && cs->attributes[AGGRESSION] > aggressionLevel ) { // we are too aggressive to worry about being hurt by this + continue; + } + // if they are below alert mode, and the danger is not in FOV, then ignore it + if ( cs->aiState < AISTATE_ALERT ) { + vec3_t ang, dir; + VectorSubtract( ent->r.currentOrigin, cs->bs->origin, dir ); + VectorNormalize( dir ); + vectoangles( dir, ang ); + if ( !AICast_InFieldOfVision( cs->viewangles, cs->attributes[FOV], ang ) ) { + // can't see it + continue; + } + } + if ( ent->client && + ( !cs->vislist[ent->s.number].visible_timestamp || ( cs->vislist[ent->s.number].visible_timestamp < level.time - 3000 ) ) ) { + // (!dcs->vislist[trav->s.number].visible_timestamp || (dcs->vislist[trav->s.number].visible_timestamp < level.time - 3000)))) + continue; // not aware of them, and they're not aware of us + } + // are they in danger? + if ( cs->dangerEntityValidTime < level.time + 50 ) { + VectorSubtract( cs->bs->cur_ps.origin, org, vec ); + dist = VectorLength( vec ); + if ( dist < dangerDist ) { + if ( dangerFlags & DANGER_CLIENTAIM ) { + // also check aiming + if ( DotProduct( vec, fwd ) < ( dist * 0.95 - 100 ) ) { + continue; + } + } + // + cs->aiFlags &= ~AIFL_DENYACTION; + AICast_ScriptEvent( cs, "avoiddanger", ent->classname ); + if ( cs->aiFlags & AIFL_DENYACTION ) { + continue; + } + cs->dangerEntity = ent->s.number; + VectorCopy( org, cs->dangerEntityPos ); + cs->dangerEntityValidTime = endTime + 50; + cs->dangerDist = dangerDist * 1.5; // when we hide from it, get a good distance away + cs->dangerEntityTimestamp = level.time; + } + } + } +} + + +qboolean AICast_HasFiredWeapon( int entNum, int weapon ) { + if ( AICast_GetCastState( entNum )->weaponFireTimes[weapon] ) { + return qtrue; + } + + return qfalse; +} + +qboolean AICast_AllowFlameDamage( int entNum ) { + // DHM - Nerve :: caststates are not initialized in multiplayer + if ( g_gametype.integer != GT_SINGLE_PLAYER ) { + return qtrue; + } + // dhm + + if ( caststates[entNum].aiFlags & AIFL_NO_FLAME_DAMAGE ) { + return qfalse; + } + return qtrue; +} + +/* +============= +AICast_ProcessBullet +============= +*/ +void AICast_ProcessBullet( gentity_t *attacker, vec3_t start, vec3_t end ) { + gentity_t *tent; + int i; + float dist; + vec3_t vProj, vDir, dir; + cast_state_t *cs; + + VectorSubtract( end, start, dir ); + + // RF, AI should hear this pass by them really closely, or hitting a wall close by + for ( cs = caststates, tent = g_entities, i = 0; i < level.maxclients; i++, tent++, cs++ ) { + if ( !tent->inuse ) { + continue; + } + if ( tent == attacker ) { + continue; + } + if ( tent->aiInactive ) { + continue; + } + if ( tent->health <= 0 ) { + continue; + } + if ( cs->castScriptStatus.scriptNoSightTime > level.time ) { + continue; + } + if ( !( tent->r.svFlags & SVF_CASTAI ) ) { + continue; + } + if ( cs->aiState >= AISTATE_COMBAT ) { // RF add // already fighting, not interested in bullet impacts + continue; + } + if ( cs->bulletImpactIgnoreTime > level.time ) { + continue; + } + dist = Distance( tent->client->ps.origin, end ); + if ( dist <= cs->attributes[INNER_DETECTION_RADIUS] ) { + // close enough to hear/see the impact? + // first check pvs + if ( !trap_InPVS( tent->client->ps.origin, end ) ) { + continue; + } + // heard it + goto heard; + } + // are they within radius of the bullet path to hear it travel through the air? + ProjectPointOntoVector( tent->client->ps.origin, start, end, vProj ); + VectorSubtract( vProj, start, vDir ); + if ( DotProduct( vDir, dir ) < 0 ) { // they are behind the path of the bullet + continue; + } + if ( Distance( vProj, tent->client->ps.origin ) > 0.5 * cs->attributes[INNER_DETECTION_RADIUS] ) { + continue; + } +heard: + // call the script event + AICast_ScriptEvent( cs, "bulletimpact", "" ); + if ( cs->aiFlags & AIFL_DENYACTION ) { + continue; // ignore the bullet + } + // + cs->bulletImpactTime = level.time + 100 + rand() % 200; // random reaction delay; + VectorCopy( start, cs->bulletImpactStart ); + VectorCopy( end, cs->bulletImpactEnd ); + cs->bulletImpactEntity = attacker->s.number; + } +} + +/* +================ +AICast_AudibleEvent +================ +*/ +void AICast_AudibleEvent( int srcnum, vec3_t pos, float range ) { + int i; + cast_state_t *cs, *scs = 0; + gentity_t *ent, *sent; + float adjustedRange, localDist; + + // DHM - Nerve :: caststates are not initialized in multiplayer + if ( g_gametype.integer != GT_SINGLE_PLAYER ) { + return; + } + // dhm + + if ( g_debugAudibleEvents.integer ) { + G_Printf( "AICast_AudibleEvent: (%0.1f %0.1f %0.1f) range: %0.0f\n", pos[0], pos[1], pos[2], range ); + } + + sent = &g_entities[srcnum]; + if ( sent->flags & FL_NOTARGET ) { + if ( g_debugAudibleEvents.integer ) { + G_Printf( "NOTARGET enabled, aborting\n" ); + } + return; + } + if ( srcnum < level.maxclients ) { + scs = AICast_GetCastState( srcnum ); + } + + for ( ent = g_entities, cs = caststates, i = 0; i < level.maxclients; i++, cs++, ent++ ) { + if ( !cs->bs ) { + continue; + } + if ( ent == sent ) { + continue; + } + if ( cs->castScriptStatus.scriptNoSightTime > level.time ) { + continue; + } + if ( ent->health <= 0 ) { + continue; + } + // if within range, and this sound was made by an enemy + if ( scs ) { + if ( ( srcnum < level.maxclients ) && scs->aiState < AISTATE_COMBAT && !AICast_QueryEnemy( cs, srcnum ) ) { + continue; + } + } + // calculate the adjusted range according to this AI's hearing abilities + adjustedRange = range * cs->attributes[HEARING_SCALE]; + localDist = DistanceSquared( pos, ent->s.pos.trBase ); + if ( localDist > adjustedRange * adjustedRange ) { // fast out if already outside range + continue; + } + if ( !trap_InPVS( pos, ent->s.pos.trBase ) ) { + adjustedRange *= cs->attributes[HEARING_SCALE_NOT_PVS]; + } + if ( localDist > adjustedRange * adjustedRange ) { + continue; + } + // we heard it + + if ( g_debugAudibleEvents.integer ) { + G_Printf( "AICast_AudibleEvent heard: %s \"%s\" (dist:%0.0f s:%0.2f pvss:%0.2f)\n", ent->classname, ent->aiName, ( sqrt( localDist ) ), cs->attributes[HEARING_SCALE], cs->attributes[HEARING_SCALE_NOT_PVS] ); + } + + cs->audibleEventTime = level.time + 200 + rand() % 300; // random reaction delay + VectorCopy( pos, cs->audibleEventOrg ); + cs->audibleEventEnt = ent->s.number; + } +} diff --git a/src/game/ai_cast_fight.h b/src/game/ai_cast_fight.h new file mode 100644 index 0000000..a7b02eb --- /dev/null +++ b/src/game/ai_cast_fight.h @@ -0,0 +1,42 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: ai_cast_fight.h +// Function: Wolfenstein AI Fighting Values +// Programmer: Ridah +// Tab Size: 4 (real tabs) +//=========================================================================== + +// This is where all constants should reside, which decide when or if a character +// can attack it's enemy. + +#define LOPER_GROUND_RANGE 200 +#define BLACKGUARD_MELEE_RANGE 48 +#define REJECT_MELEE_RANGE 90 //----(SA) added diff --git a/src/game/ai_cast_func_attack.c b/src/game/ai_cast_func_attack.c new file mode 100644 index 0000000..57c9eae --- /dev/null +++ b/src/game/ai_cast_func_attack.c @@ -0,0 +1,1259 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: ai_cast_funcs.c +// Function: Wolfenstein AI Character Decision Making +// Programmer: Ridah +// Tab Size: 4 (real tabs) +//=========================================================================== + +#include "../game/g_local.h" +#include "../game/q_shared.h" +#include "../game/botlib.h" //bot lib interface +#include "../game/be_aas.h" +#include "../game/be_ea.h" +#include "../game/be_ai_gen.h" +#include "../game/be_ai_goal.h" +#include "../game/be_ai_move.h" +#include "../botai/botai.h" //bot ai interface + +#include "ai_cast.h" + +//================================================================================= +// +// ZOMBIE SPECIAL ATTACKS +// +//================================================================================= + +/* +============ +AIFunc_ZombieFlameAttack() + + Zombie "Flaming Bats" attack. + + NOTE: this actually uses the EFFECT3 slot for client-side effects (others are taken) +============ +*/ + +#define ZOMBIE_FLAME_DURATION 4000 + +char *AIFunc_ZombieFlameAttack( cast_state_t *cs ) { + bot_state_t *bs; + gentity_t *ent; + // + ent = &g_entities[cs->entityNum]; + bs = cs->bs; + // + ent->s.onFireEnd = level.time + 2000; + // + if ( ent->health < 0 ) { + ent->s.onFireEnd = 0; + return AIFunc_DefaultStart( cs ); + } + // + if ( cs->enemyNum < 0 ) { + ent->s.onFireEnd = level.time + 1500; + ent->client->ps.torsoTimer = 0; + ent->client->ps.legsTimer = 0; + return AIFunc_DefaultStart( cs ); + } +/* disabled, keep going so they cant come back for the easy kill + // + // if we can't see them anymore, abort immediately + if (cs->vislist[cs->enemyNum].real_visible_timestamp != cs->vislist[cs->enemyNum].real_update_timestamp) { + ent->s.onFireEnd = level.time + 1500; + ent->client->ps.torsoTimer = 0; + ent->client->ps.legsTimer = 0; + return AIFunc_DefaultStart( cs ); + } +*/ + // if outside range, move closer + if ( VectorDistance( cs->bs->origin, cs->vislist[cs->enemyNum].visible_pos ) > ZOMBIE_FLAME_RADIUS ) { + ent->s.onFireEnd = level.time + 1500; + ent->client->ps.torsoTimer = 0; + ent->client->ps.legsTimer = 0; + return AIFunc_DefaultStart( cs ); + } + // we are firing this weapon, so record it + cs->weaponFireTimes[WP_MONSTER_ATTACK1] = level.time; + // once an attack has started, only abort once the player leaves our view, or time runs out + if ( cs->thinkFuncChangeTime < level.time - ZOMBIE_FLAME_DURATION ) { + + // finish this attack + ent->client->ps.torsoTimer = 0; + ent->client->ps.legsTimer = 0; + return AIFunc_DefaultStart( cs ); + + } else { + + //ent->client->ps.torsoTimer = 400; + //ent->client->ps.legsTimer = 400; + + // draw the client-side effect + ent->client->ps.eFlags |= EF_MONSTER_EFFECT3; + + // inform the client of our enemies position + //VectorCopy( g_entities[cs->enemyNum].client->ps.origin, ent->s.origin2 ); + //ent->s.origin2[2] += g_entities[cs->enemyNum].client->ps.viewheight; + + // keep facing them + AICast_AimAtEnemy( cs ); + + // look slightly downwards since animation is facing upwards slightly + cs->ideal_viewangles[PITCH] += 20; + } + // + // + return NULL; +} + +char *AIFunc_ZombieFlameAttackStart( cast_state_t *cs ) { + gentity_t *ent; + // + ent = &g_entities[cs->entityNum]; + ent->s.otherEntityNum2 = cs->enemyNum; + ent->s.effect3Time = level.time; + // + // dont turn + cs->ideal_viewangles[YAW] = cs->viewangles[YAW]; + //cs->ideal_viewangles[PITCH] = -45; // look upwards + // start the flame + ent->s.onFireStart = level.time; + ent->s.onFireEnd = level.time + ZOMBIE_FLAME_DURATION; + // + // set the correct animation + BG_PlayAnimName( &ent->client->ps, "both_attack1", ANIM_BP_BOTH, qtrue, qfalse, qtrue ); + ent->client->ps.legsTimer = ZOMBIE_FLAME_DURATION; + ent->client->ps.torsoTimer = ZOMBIE_FLAME_DURATION; + // + cs->aifunc = AIFunc_ZombieFlameAttack; + return "AIFunc_ZombieFlameAttack"; +} + + + +/* +============ +AIFunc_ZombieAttack2() + + Zombie "Evil Spirit" attack. + + Character draws the light from surrounding walls (expanding negative light) and builds + up to the release of a flying translucent skull with trail effect (and beady eyes). + + Spirit should track it's enemy slightly, inflicting lots of damage, removing sprint bar, + and effecting sight temporarily. + + Speed of spirit is effected by skill level, higher skill = faster speed + + Spirits inflicting AI soldiers should kill instantly, removing all flesh from the + soldier's face (draw skull under head model, then fade head model away over a short period). +============ +*/ +extern void weapon_zombiespirit( gentity_t *ent, gentity_t *missile ); + +#define ZOMBIE_SPIRIT_BUILDUP_TIME 6000 // last for this long +#define ZOMBIE_SPIRIT_FADEOUT_TIME 1000 +#define ZOMBIE_SPIRIT_DLIGHT_RADIUS_MAX 256 +#define ZOMBIE_SPIRIT_FIRE_INTERVAL 1000 + +int lastZombieSpiritAttack; + +char *AIFunc_ZombieAttack2( cast_state_t *cs ) { + bot_state_t *bs; + gentity_t *ent; + // + ent = &g_entities[cs->entityNum]; + bs = cs->bs; + // + if ( cs->enemyNum < 0 ) { + return AIFunc_DefaultStart( cs ); + } + // + // if we can't see them anymore, abort immediately + if ( cs->vislist[cs->enemyNum].real_visible_timestamp != cs->vislist[cs->enemyNum].real_update_timestamp ) { + return AIFunc_DefaultStart( cs ); + } + // + lastZombieSpiritAttack = level.time; + // we are firing this weapon, so record it + cs->weaponFireTimes[WP_MONSTER_ATTACK2] = level.time; + // once an attack has started, only abort once the player leaves our view, or time runs out + if ( cs->thinkFuncChangeTime < level.time - ZOMBIE_SPIRIT_BUILDUP_TIME ) { + // if enough time has elapsed, finish this attack + if ( level.time > cs->thinkFuncChangeTime + ZOMBIE_SPIRIT_BUILDUP_TIME + ZOMBIE_SPIRIT_FADEOUT_TIME ) { + return AIFunc_DefaultStart( cs ); + } + } else { + + // set torso to the correct animation + // TODO + //ent->client->ps.torsoTimer = 300; // leave enough time to cancel if we stop coming in here, but stay in the anim if we come back next thing + + // draw the client-side effect + ent->client->ps.eFlags |= EF_MONSTER_EFFECT; + + // inform the client of our enemies position + VectorCopy( g_entities[cs->enemyNum].client->ps.origin, ent->s.origin2 ); + ent->s.origin2[2] += g_entities[cs->enemyNum].client->ps.viewheight; + } + // + // + return NULL; +} + +char *AIFunc_ZombieAttack2Start( cast_state_t *cs ) { + gentity_t *ent; + // + // don't allow 2 consecutive spirit attacks at once + if ( lastZombieSpiritAttack <= level.time && lastZombieSpiritAttack > level.time - 1000 ) { + return NULL; + } + lastZombieSpiritAttack = level.time; + // + ent = &g_entities[cs->entityNum]; + ent->s.otherEntityNum2 = cs->enemyNum; + ent->s.effect1Time = level.time; + // + // dont turn + cs->ideal_viewangles[YAW] = cs->viewangles[YAW]; + // set torso to the correct animation + // TODO + //ent->client->ps.torsoAnim = + // ( ( ent->client->ps.torsoAnim & ANIM_TOGGLEBIT ) ^ ANIM_TOGGLEBIT ) | BOTH_SALUTE; // FIXME: need a specific anim for this + // + cs->aifunc = AIFunc_ZombieAttack2; + return "AIFunc_ZombieAttack2"; +} + + +/* +================ +AIFunc_ZombieMelee +================ +*/ + +int zombieHitDamage[5] = { + 16, + 16, + 16, + 12, + 20 +}; + +#define NUM_ZOMBIE_ANIMS 5 +int zombieHitTimes[NUM_ZOMBIE_ANIMS][3] = { // up to three hits per attack + {ANIMLENGTH( 11,20 ),-1}, + {ANIMLENGTH( 9,20 ),-1}, + {ANIMLENGTH( 9,20 ),-1}, + {ANIMLENGTH( 8,20 ),ANIMLENGTH( 16,20 ),-1}, + {ANIMLENGTH( 8,20 ),ANIMLENGTH( 15,20 ),ANIMLENGTH( 24,20 )}, +}; + +char *AIFunc_ZombieMelee( cast_state_t *cs ) { + gentity_t *ent = &g_entities[cs->entityNum]; + int hitDelay = -1, anim; + trace_t *tr; + cast_state_t *ecs = AICast_GetCastState( cs->enemyNum ); + aicast_predictmove_t move; + float enemyDist; + + if ( !ent->client->ps.torsoTimer ) { + return AIFunc_DefaultStart( cs ); + } + + if ( ecs ) { + + anim = ( ent->client->ps.torsoAnim & ~ANIM_TOGGLEBIT ) - BG_AnimationIndexForString( "attack1", cs->entityNum ); + if ( anim < 0 || anim >= NUM_ZOMBIE_ANIMS ) { + // animation interupted + return AIFunc_DefaultStart( cs ); + } + if ( zombieHitTimes[anim][cs->animHitCount] >= 0 && cs->animHitCount < 3 ) { + + if ( !cs->animHitCount ) { + hitDelay = zombieHitTimes[anim][cs->animHitCount]; + } else { + hitDelay = zombieHitTimes[anim][cs->animHitCount] - zombieHitTimes[anim][cs->animHitCount - 1]; + } + + // check for inflicting damage + if ( level.time - cs->weaponFireTimes[cs->weaponNum] > hitDelay ) { + // do melee damage + if ( ( tr = CheckMeleeAttack( ent, AICast_WeaponRange( cs, cs->weaponNum ) + 4.0, qfalse ) ) && ( tr->entityNum == cs->enemyNum ) ) { + G_Damage( &g_entities[tr->entityNum], ent, ent, vec3_origin, tr->endpos, + zombieHitDamage[anim], 0, MOD_GAUNTLET ); + G_AddEvent( ent, EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[ent->aiCharacter].soundScripts[STAYSOUNDSCRIPT] ) ); + } else { + G_AddEvent( ent, EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[ent->aiCharacter].soundScripts[FOLLOWSOUNDSCRIPT] ) ); + } + cs->weaponFireTimes[cs->weaponNum] = level.time; + cs->animHitCount++; + } + } + // face them + AICast_AimAtEnemy( cs ); + if ( !ent->client->ps.legsTimer || zombieHitTimes[anim][cs->animHitCount] < 0 || cs->animHitCount >= 3 ) { + // if they are outside range, move forward + AICast_PredictMovement( ecs, 2, 0.5, &move, &g_entities[cs->enemyNum].client->pers.cmd, -1 ); + enemyDist = Distance( move.endpos, cs->bs->origin ); + enemyDist -= g_entities[cs->enemyNum].r.maxs[0]; + enemyDist -= ent->r.maxs[0]; + if ( /*anim != 4 &&*/ ( enemyDist > 16 ) ) { // we can get closer + if ( ent->client->ps.legsTimer ) { + ent->client->ps.legsTimer = 0; // allow legs us to move + if ( cs->castScriptStatus.scriptNoMoveTime < level.time + 200 ) { // dont move until the legs are done lerping out of attack anim + cs->castScriptStatus.scriptNoMoveTime = level.time + 200; + } + } + if ( !ent->client->ps.legsTimer && cs->castScriptStatus.scriptNoMoveTime < level.time ) { + trap_EA_MoveForward( cs->entityNum ); + } + } + } + + } + + return NULL; +} + +/* +================ +AIFunc_ZombieMeleeStart +================ +*/ +char *AIFunc_ZombieMeleeStart( cast_state_t *cs ) { + gentity_t *ent; + + ent = &g_entities[cs->entityNum]; + cs->weaponFireTimes[cs->weaponNum] = level.time; + cs->animHitCount = 0; + + // face them + AICast_AimAtEnemy( cs ); + + // audible sound + AIChar_AttackSound( cs ); + + // play an anim + BG_UpdateConditionValue( cs->entityNum, ANIM_COND_WEAPON, cs->weaponNum, qtrue ); + BG_AnimScriptEvent( &ent->client->ps, ANIM_ET_FIREWEAPON, qfalse, qtrue ); + + cs->aifunc = AIFunc_ZombieMelee; + return "AIFunc_ZombieMelee"; +} + +//================================================================================= +// +// LOPER MELEE ATTACK +// +//================================================================================= + +#define NUM_LOPERMELEE_ANIMS 2 +int loperHitTimes[NUM_LOPERMELEE_ANIMS] = { + ( ANIMLENGTH( 3,15 ) ), + ( ANIMLENGTH( 6,15 ) ) +}; + +#define LOPER_MELEE_DAMAGE 20 +#define LOPER_MELEE_RANGE 48 + +/* +=============== +AIFunc_LoperAttack1() + + Loper's close range melee attack +=============== +*/ +char *AIFunc_LoperAttack1( cast_state_t *cs ) { + trace_t *tr; + gentity_t *ent; + int anim; + // + ent = &g_entities[cs->entityNum]; + // + // draw the client-side lightning effect + //ent->client->ps.eFlags |= EF_MONSTER_EFFECT; + // + // have we inflicted the damage? + if ( cs->weaponFireTimes[WP_MONSTER_ATTACK1] > cs->thinkFuncChangeTime ) { + // has the animation finished? + if ( !ent->client->ps.legsTimer ) { + return AIFunc_DefaultStart( cs ); + } + return NULL; // just wait for anim to finish + } + // ready to inflict damage? + anim = ( ent->client->ps.legsAnim & ~ANIM_TOGGLEBIT ) - BG_AnimationIndexForString( "legs_extra", cs->entityNum ); + if ( cs->thinkFuncChangeTime < level.time - loperHitTimes[anim] ) { + // check for damage + // TTimo: gcc: suggests () around assignment used as truth value + if ( ( tr = CheckMeleeAttack( &g_entities[cs->entityNum], LOPER_MELEE_RANGE, qfalse ) ) ) { + G_Damage( &g_entities[tr->entityNum], ent, ent, vec3_origin, tr->endpos, + LOPER_MELEE_DAMAGE, 0, MOD_LOPER_HIT ); + // sound + if ( anim == 0 ) { + G_AddEvent( ent, EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[ent->aiCharacter].soundScripts[ORDERSDENYSOUNDSCRIPT] ) ); + } else { + G_AddEvent( ent, EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[ent->aiCharacter].soundScripts[MISC1SOUNDSCRIPT] ) ); + } + } + cs->weaponFireTimes[WP_MONSTER_ATTACK1] = level.time; + } + + return NULL; +} + + +char *AIFunc_LoperAttack1Start( cast_state_t *cs ) { + gentity_t *ent; + // + ent = &g_entities[cs->entityNum]; + // face them + AICast_AimAtEnemy( cs ); + // start the animation + if ( rand() % 2 ) { + G_AddEvent( ent, EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[ent->aiCharacter].soundScripts[FOLLOWSOUNDSCRIPT] ) ); + BG_PlayAnimName( &ent->client->ps, "legs_extra", ANIM_BP_LEGS, qtrue, qfalse, qtrue ); + } else { + G_AddEvent( ent, EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[ent->aiCharacter].soundScripts[STAYSOUNDSCRIPT] ) ); + BG_PlayAnimName( &ent->client->ps, "legs_extra2", ANIM_BP_LEGS, qtrue, qfalse, qtrue ); + } + // + cs->aifunc = AIFunc_LoperAttack1; + return "AIFunc_LoperAttack1"; +} + +//================================================================================= +// +// LOPER LEAP ATTACK +// +//================================================================================= + +#define LOPER_LEAP_ANIM LEGS_EXTRA3 +#define LOPER_LEAP_FPS 15 + +// update for a version of the loper new today (6/26) +//#define LOPER_LEAP_FRAME_COUNT 4 +#define LOPER_LEAP_FRAME_COUNT 10 +#define LOPER_LEAP_DURATION ( LOPER_LEAP_FRAME_COUNT*( 1000 / LOPER_LEAP_FPS ) ) + + +#define LOPER_LAND_ANIM LEGS_EXTRA4 +#define LOPER_LAND_FPS 15 + +// update for a version of the loper new today (6/26) +//#define LOPER_LAND_FRAME_COUNT 17 +#define LOPER_LAND_FRAME_COUNT 21 +#define LOPER_LAND_DURATION ( LOPER_LAND_FRAME_COUNT*( 1000 / LOPER_LAND_FPS ) ) + + +#define LOPER_LEAP_DAMAGE 10 +#define LOPER_LEAP_DELAY 100 // note, this needs to be something, or they'll think they're done before they even started +#define LOPER_LEAP_RANGE 200 +#define LOPER_LEAP_VELOCITY_START 750.0 +#define LOPER_LEAP_VELOCITY_END 1050.0 +#define LOPER_LEAP_VELOCITY_Z 300 +#define LOPER_LEAP_LAND_MOMENTUM 250 + +/* +=============== +AIFunc_LoperAttack2() + + Loper's leaping long range attack +=============== +*/ +char *AIFunc_LoperAttack2( cast_state_t *cs ) { + gentity_t *ent; + vec3_t vec; + qboolean onGround = qfalse; + // + ent = &g_entities[cs->entityNum]; + // + // are we waiting to inflict damage? + if ( ( cs->enemyNum >= 0 ) && ( cs->weaponFireTimes[WP_MONSTER_ATTACK2] < level.time - 50 ) && + ( cs->bs->cur_ps.groundEntityNum == ENTITYNUM_NONE ) ) { + // ready to inflict damage? + if ( cs->thinkFuncChangeTime < level.time - LOPER_LEAP_DELAY ) { + // check for damage + if ( VectorDistance( cs->bs->origin, g_entities[cs->enemyNum].client->ps.origin ) < LOPER_LEAP_RANGE ) { + // draw the client-side lightning effect + ent->client->ps.eFlags |= EF_MONSTER_EFFECT; + // do the damage + G_Damage( &g_entities[cs->enemyNum], ent, ent, vec3_origin, cs->bs->origin, LOPER_LEAP_DAMAGE, 0, MOD_LOPER_LEAP ); + G_Sound( &g_entities[cs->entityNum], level.loperZapSound ); + cs->weaponFireTimes[WP_MONSTER_ATTACK2] = level.time; + } + } + } + // + // landed? + if ( cs->bs->cur_ps.groundEntityNum != ENTITYNUM_NONE ) { + onGround = qtrue; + } else { // predict a landing + aicast_predictmove_t move; + float changeTime; + AICast_PredictMovement( cs, 1, 0.2, &move, &cs->lastucmd, cs->enemyNum ); + if ( move.groundEntityNum != ENTITYNUM_NONE ) { + onGround = qtrue; + } + // + // adjust velocity + VectorCopy( cs->loperLeapVel, vec ); + vec[2] = 0; + VectorNormalize( vec ); + changeTime = 2.0 * ( 0.001 * ( level.time - cs->thinkFuncChangeTime ) ); + if ( changeTime > 1.0 ) { + changeTime = 1.0; + } + VectorScale( vec, LOPER_LEAP_VELOCITY_START + changeTime * ( LOPER_LEAP_VELOCITY_END - LOPER_LEAP_VELOCITY_START ), vec ); + g_entities[cs->entityNum].s.pos.trDelta[0] = vec[0]; + g_entities[cs->entityNum].s.pos.trDelta[1] = vec[1]; + } + // + if ( onGround || ( cs->aiFlags & AIFL_LAND_ANIM_PLAYED ) ) { + // if we just started the attack recently, we probably haven't had a chance to get airborne yet + if ( cs->thinkFuncChangeTime < level.time - LOPER_LEAP_DELAY ) { + // loper is back on ground, wait for animation to play out + if ( !( cs->aiFlags & AIFL_LAND_ANIM_PLAYED ) ) { + BG_PlayAnimName( &ent->client->ps, "legs_extra4", ANIM_BP_LEGS, qtrue, qfalse, qtrue ); + // + cs->aiFlags |= AIFL_LAND_ANIM_PLAYED; + // TODO:play the landing thud + } + // + if ( ent->client->ps.legsTimer < 800 ) { // we're done + ent->client->ps.legsTimer = 0; + return AIFunc_DefaultStart( cs ); + } + // keep moving slightly in our facing direction to simulate landing momentum + AngleVectors( cs->viewangles, vec, NULL, NULL ); + trap_EA_Move( cs->entityNum, vec, ( (float)ent->client->ps.legsTimer / (float)LOPER_LAND_DURATION ) * (float)LOPER_LEAP_LAND_MOMENTUM ); + return NULL; + } + } + ent->client->ps.legsTimer = 500; // stay on this until landing + return NULL; +} + +char *AIFunc_LoperAttack2Start( cast_state_t *cs ) { + gentity_t *ent; + vec3_t vec, avec; + // + ent = &g_entities[cs->entityNum]; + // + if ( cs->enemyNum < 0 ) { + return AIFunc_DefaultStart( cs ); + } + // face them + AICast_AimAtEnemy( cs ); + // if not facing them yet, wait + VectorSubtract( g_entities[cs->enemyNum].client->ps.origin, cs->bs->origin, vec ); + VectorNormalize( vec ); + AngleVectors( cs->viewangles, avec, NULL, NULL ); + if ( DotProduct( vec, avec ) < 0.9 ) { + //cs->aifunc = AIFunc_LoperAttack2Start; + return NULL; + } + // OK, start the animation + BG_PlayAnimName( &ent->client->ps, "legs_extra3", ANIM_BP_LEGS, qtrue, qfalse, qtrue ); + ent->client->ps.legsTimer = 500; // stay on this until landing + // send us hurtling towards our enemy + VectorScale( vec, LOPER_LEAP_VELOCITY_START, vec ); + vec[2] = LOPER_LEAP_VELOCITY_Z; + VectorCopy( vec, ent->client->ps.velocity ); + VectorCopy( vec, cs->loperLeapVel ); + // + cs->aiFlags &= ~AIFL_LAND_ANIM_PLAYED; + // play the sound + // TODO + // + cs->aifunc = AIFunc_LoperAttack2; + return "AIFunc_LoperAttack2"; +} + +//================================================================================= +// +// LOPER GROUND ATTACK +// +//================================================================================= + +#define LOPER_GROUND_ANIM LEGS_EXTRA5 +#define LOPER_GROUND_FPS 10 + +#define LOPER_GROUND_FRAME_COUNT 8 +#define LOPER_GROUND_DURATION ( LOPER_GROUND_FRAME_COUNT*( 1000 / LOPER_GROUND_FPS ) ) + +#define LOPER_GROUND_DAMAGE 20 + +/* +=============== +AIFunc_LoperAttack3() + + Loper's ground electrical attack +=============== +*/ +char *AIFunc_LoperAttack3( cast_state_t *cs ) { + gentity_t *ent; + qboolean hitClient = qfalse; + // + ent = &g_entities[cs->entityNum]; + // + // done with this attack? + if ( !ent->client->ps.legsTimer ) { + cs->pauseTime = level.time + 600; // don't move until effect is done + ent->client->ps.legsTimer = 600; // stay down until effect is done + return AIFunc_DefaultStart( cs ); + } + // ready to inflict damage? + if ( cs->thinkFuncChangeTime < level.time - 900 ) { + // + // draw the client-side lightning effect + ent->client->ps.eFlags |= EF_MONSTER_EFFECT3; + //ent->s.effect3Time = level.time + 500;//cs->thinkFuncChangeTime + LOPER_GROUND_DELAY - 200; + // + // are we waiting to inflict damage? + if ( cs->weaponFireTimes[WP_MONSTER_ATTACK3] < level.time - 100 ) { + // check for damage + hitClient = G_RadiusDamage( cs->bs->origin, ent, LOPER_GROUND_DAMAGE, LOPER_GROUND_RANGE, ent, MOD_LOPER_GROUND ); + // + cs->weaponFireTimes[WP_MONSTER_ATTACK3] = level.time; + // TODO: client-side visual effect + // TODO: throw them backwards (away from us) + } else { + hitClient = qtrue; // so we don't abort + } + // + if ( !hitClient && ( ent->client->ps.legsTimer > 400 ) && cs->thinkFuncChangeTime < ( level.time - 1000 ) ) { // we're done with this attack + cs->pauseTime = level.time + 400; // don't move until effect is done + ent->client->ps.legsTimer = 400; // stay down until effect is done + return AIFunc_DefaultStart( cs ); + } + } + // + return NULL; +} + +char *AIFunc_LoperAttack3Start( cast_state_t *cs ) { + gentity_t *ent; + // + ent = &g_entities[cs->entityNum]; + // + // face them + AICast_AimAtEnemy( cs ); + // play the animation + BG_PlayAnimName( &ent->client->ps, "legs_extra5", ANIM_BP_LEGS, qtrue, qfalse, qtrue ); + // + // play the buildup sound + // TODO + // + cs->aifunc = AIFunc_LoperAttack3; + return "AIFunc_LoperAttack3"; +} + +//================================================================================= +// +// STIM SOLDIER FLYING ATTACK +// +//================================================================================= + +#define STIMSOLDIER_FLYJUMP_ANIM LEGS_EXTRA1 +#define STIMSOLDIER_FLYJUMP_FPS 15 +#define STIMSOLDIER_FLYJUMP_FRAME_COUNT 28 +#define STIMSOLDIER_FLYJUMP_DURATION ( STIMSOLDIER_FLYJUMP_FRAME_COUNT*( 1000 / STIMSOLDIER_FLYJUMP_FPS ) ) +#define STIMSOLDIER_FLYJUMP_DELAY ( STIMSOLDIER_FLYJUMP_DURATION + 3000 ) + +// hover plays continuously +#define STIMSOLDIER_FLYHOVER_ANIM LEGS_EXTRA2 +#define STIMSOLDIER_FLYHOVER_FPS 5 + +#define STIMSOLDIER_FLYLAND_ANIM LEGS_LAND +#define STIMSOLDIER_FLYLAND_FPS 15 +#define STIMSOLDIER_FLYLAND_FRAME_COUNT 14 +#define STIMSOLDIER_FLYLAND_DURATION ( STIMSOLDIER_FLYLAND_FRAME_COUNT*( 1000 / STIMSOLDIER_FLYLAND_FPS ) ) + +#define STIMSOLDIER_STARTJUMP_DELAY ( STIMSOLDIER_FLYJUMP_DURATION*0.5 ) + +char *AIFunc_StimSoldierAttack1( cast_state_t *cs ) { + gentity_t *ent; + vec3_t vec; + static vec3_t up = {0,0,1}; + // + ent = &g_entities[cs->entityNum]; + cs->weaponFireTimes[WP_MONSTER_ATTACK1] = level.time; + // face them + AICast_AimAtEnemy( cs ); + // + // are we done with this attack? + if ( cs->thinkFuncChangeTime < level.time - STIMSOLDIER_FLYJUMP_DELAY ) { + // have we hit the ground yet? + if ( ent->s.groundEntityNum != ENTITYNUM_NONE ) { + // we are on something, have we started the landing animation? + if ( !( cs->aiFlags & AIFL_LAND_ANIM_PLAYED ) ) { + ent->client->ps.legsAnim = + ( ( ent->client->ps.legsAnim & ANIM_TOGGLEBIT ) ^ ANIM_TOGGLEBIT ) | STIMSOLDIER_FLYLAND_ANIM; + ent->client->ps.legsTimer = STIMSOLDIER_FLYLAND_DURATION; // stay down until attack is finished + // + cs->noAttackTime = level.time + STIMSOLDIER_FLYLAND_DURATION; + cs->aiFlags |= AIFL_LAND_ANIM_PLAYED; + } else { + if ( !ent->client->ps.legsTimer ) { // animation has finished, resume AI + return AIFunc_DefaultStart( cs ); + } + } + } else { + // still flying + } + return NULL; + } + // + // are we ready to start flying? + if ( cs->thinkFuncChangeTime < ( level.time - STIMSOLDIER_STARTJUMP_DELAY ) ) { + if ( !ent->client->ps.powerups[PW_FLIGHT] ) { + // play a special ignition sound? + } + ent->client->ps.powerups[PW_FLIGHT] = 1; // let them fly + ent->s.loopSound = level.stimSoldierFlySound; + ent->client->ps.eFlags |= EF_MONSTER_EFFECT; // client-side stim engine effect + if ( ent->s.effect1Time != ( cs->thinkFuncChangeTime + STIMSOLDIER_STARTJUMP_DELAY ) ) { + ent->s.effect1Time = ( cs->thinkFuncChangeTime + STIMSOLDIER_STARTJUMP_DELAY ); + // start the hovering animation + ent->client->ps.legsAnim = + ( ( ent->client->ps.legsAnim & ANIM_TOGGLEBIT ) ^ ANIM_TOGGLEBIT ) | STIMSOLDIER_FLYHOVER_ANIM; + } + // give us some upwards velocity? + if ( cs->thinkFuncChangeTime > level.time - STIMSOLDIER_FLYJUMP_DURATION * 0.9 ) { + trap_EA_Move( cs->entityNum, up, 300 ); + //trap_EA_Jump(cs->entityNum); + VectorCopy( cs->bs->origin, cs->stimFlyAttackPos ); + } else { + // attack them + // + // if we can't attack, abort + if ( AICast_CheckAttack( cs, cs->enemyNum, qfalse ) ) { + // apply weapons.. + trap_EA_Attack( cs->entityNum ); + } + // we're done here + cs->thinkFuncChangeTime = -9999; + } + } else { + // still on ground, so move forward to account for stepping animation + AngleVectors( cs->viewangles, vec, NULL, NULL ); + trap_EA_Move( cs->entityNum, vec, 300 ); + } + // + if ( ent->client->ps.legsTimer < 1000 ) { + ent->client->ps.legsTimer = 1000; // stay down until effect is done + } + // + return NULL; +} + +char *AIFunc_StimSoldierAttack1Start( cast_state_t *cs ) { + gentity_t *ent; + //static vec3_t mins={-96,-96,0}, maxs={96,96,72}; + vec3_t pos, dir; + trace_t tr; + // + cs->weaponFireTimes[cs->weaponNum] = level.time; + ent = &g_entities[cs->entityNum]; + // + // face them + AICast_AimAtEnemy( cs ); + // first, check if this is a good place to start the flying attack + AngleVectors( cs->ideal_viewangles, dir, NULL, NULL ); + VectorMA( cs->bs->origin, 300, dir, pos ); + pos[2] += 128; + trap_Trace( &tr, cs->bs->origin, cs->bs->cur_ps.mins, cs->bs->cur_ps.maxs, pos, cs->entityNum, ent->clipmask ); + if ( tr.startsolid || tr.allsolid ) { + return NULL; // not a good place + } + // check we can attack them from there + // select our special weapon (rocket launcher or tesla) + if ( COM_BitCheck( cs->bs->cur_ps.weapons, WP_PANZERFAUST ) ) { + cs->weaponNum = WP_PANZERFAUST; + } else if ( COM_BitCheck( cs->bs->cur_ps.weapons, WP_TESLA ) ) { + cs->weaponNum = WP_TESLA; + } else { // no weapon? + G_Error( "stim soldier tried special jump attack without a tesla or rocket launcher\n" ); + } + if ( !AICast_CheckAttackAtPos( cs->entityNum, cs->enemyNum, pos, qfalse, qfalse ) ) { + AICast_ChooseWeapon( cs, qfalse ); + return NULL; + } + // play the animation + ent->client->ps.legsAnim = + ( ( ent->client->ps.legsAnim & ANIM_TOGGLEBIT ) ^ ANIM_TOGGLEBIT ) | STIMSOLDIER_FLYJUMP_ANIM; + ent->client->ps.legsTimer = STIMSOLDIER_FLYJUMP_DELAY; // stay down until attack is finished + // + cs->aiFlags &= ~AIFL_LAND_ANIM_PLAYED; + // play the buildup sound + // TODO + // + cs->aifunc = AIFunc_StimSoldierAttack1; + return "AIFunc_StimSoldierAttack1"; +} + +//================================================================================= +// +// STIM SOLDIER DUAL MACHINEGUN ATTACK +// +//================================================================================= + +char *AIFunc_StimSoldierAttack2( cast_state_t *cs ) { + return NULL; +} + +char *AIFunc_StimSoldierAttack2Start( cast_state_t *cs ) { + gentity_t *ent; + // + cs->weaponFireTimes[cs->weaponNum] = level.time; + ent = &g_entities[cs->entityNum]; + // + // face them + AICast_AimAtEnemy( cs ); + // TODO! + G_Printf( "TODO: stim dual machinegun attack\n" ); + // + cs->aifunc = AIFunc_StimSoldierAttack2; + return "AIFunc_StimSoldierAttack2"; +} + +//================================================================================= +// +// BLACK GUARD MELEE KICK ATTACK +// +//================================================================================= + +#define BLACKGUARD_KICK_DELAY 300 +#define BLACKGUARD_KICK_RANGE BLACKGUARD_MELEE_RANGE + 16 +#define BLACKGUARD_KICK_DAMAGE 25 + +char *AIFunc_BlackGuardAttack1( cast_state_t *cs ) { + gentity_t *ent = &g_entities[cs->entityNum]; + trace_t *tr; + vec3_t fwd; + + if ( !ent->client->ps.legsTimer ) { + return AIFunc_DefaultStart( cs ); + } + // + if ( cs->enemyNum < 0 ) { + return NULL; + } + // time for the melee? + if ( !( cs->aiFlags & AIFL_MISCFLAG1 ) ) { + // face them + AICast_AimAtEnemy( cs ); + // ready for damage? + if ( cs->thinkFuncChangeTime < level.time - BLACKGUARD_KICK_DELAY ) { + cs->aiFlags |= AIFL_MISCFLAG1; + // keep checking for impact status + tr = CheckMeleeAttack( ent, BLACKGUARD_KICK_RANGE, qfalse ); + // do melee damage? + if ( tr && ( tr->entityNum == cs->enemyNum ) ) { + AngleVectors( cs->viewangles, fwd, NULL, NULL ); + G_Damage( &g_entities[tr->entityNum], ent, ent, fwd, tr->endpos, BLACKGUARD_KICK_DAMAGE, 0, MOD_GAUNTLET ); + // throw them in direction of impact + fwd[2] = 0.5; + VectorMA( g_entities[cs->enemyNum].client->ps.velocity, 300, fwd, g_entities[cs->enemyNum].client->ps.velocity ); + } + } + } + + return NULL; +} + +char *AIFunc_BlackGuardAttack1Start( cast_state_t *cs ) { + gentity_t *ent = &g_entities[cs->entityNum]; + // + cs->weaponFireTimes[cs->weaponNum] = level.time; + // face them + AICast_AimAtEnemy( cs ); + // audible sound + AIChar_AttackSound( cs ); + // start the animation + BG_PlayAnimName( &ent->client->ps, "kick", ANIM_BP_BOTH, qtrue, qfalse, qtrue ); + // clear flags + cs->aiFlags &= ~( AIFL_MISCFLAG1 | AIFL_MISCFLAG2 ); + // + cs->aifunc = AIFunc_BlackGuardAttack1; + return "AIFunc_BlackGuardAttack1"; +} + + +//================================================================================= +// +// REJECT X-CREATURE +// +// Attacks are: backhand slap, blowtorch (small flamethrower) +// +//================================================================================= + +////// Backhand attack (slap) + +/* +============== +AIFunc_RejectAttack1 +============== +*/ +char *AIFunc_RejectAttack1( cast_state_t *cs ) { + return NULL; +} + +/* +============== +AIFunc_RejectAttack1Start +============== +*/ +char *AIFunc_RejectAttack1Start( cast_state_t *cs ) { + gentity_t *ent; + + ent = &g_entities[cs->entityNum]; + ent->s.effect1Time = level.time; + cs->ideal_viewangles[YAW] = cs->viewangles[YAW]; + cs->aifunc = AIFunc_RejectAttack1; + return "AIFunc_RejectAttack1"; +} + + +//================================================================================= +// +// WARRIOR ZOMBIE +// +// Standing melee attacks +// +//================================================================================= + +int warriorHitDamage[5] = { + 16, + 16, + 16, + 12, + 20 +}; + +#define NUM_WARRIOR_ANIMS 5 +int warriorHitTimes[NUM_WARRIOR_ANIMS][3] = { // up to three hits per attack + {ANIMLENGTH( 10,20 ),-1}, + {ANIMLENGTH( 15,20 ),-1}, + {ANIMLENGTH( 18,20 ),-1}, + {ANIMLENGTH( 15,20 ),-1}, + {ANIMLENGTH( 14,20 ),-1}, +}; + +/* +================ +AIFunc_WarriorZombieMelee +================ +*/ +char *AIFunc_WarriorZombieMelee( cast_state_t *cs ) { + gentity_t *ent = &g_entities[cs->entityNum]; + int hitDelay = -1, anim; + trace_t *tr; + cast_state_t *ecs = AICast_GetCastState( cs->enemyNum ); + aicast_predictmove_t move; + float enemyDist; + + if ( !ent->client->ps.torsoTimer ) { + return AIFunc_DefaultStart( cs ); + } + // + if ( cs->enemyNum < 0 ) { + return NULL; + } + if ( ecs ) { + + anim = ( ent->client->ps.torsoAnim & ~ANIM_TOGGLEBIT ) - BG_AnimationIndexForString( "attack1", cs->entityNum ); + if ( anim < 0 || anim >= NUM_WARRIOR_ANIMS ) { + // animation interupted + return AIFunc_DefaultStart( cs ); + } + if ( warriorHitTimes[anim][cs->animHitCount] >= 0 && cs->animHitCount < 3 ) { + + if ( !cs->animHitCount ) { + hitDelay = warriorHitTimes[anim][cs->animHitCount]; + } else { + hitDelay = warriorHitTimes[anim][cs->animHitCount] - warriorHitTimes[anim][cs->animHitCount - 1]; + } + + // check for inflicting damage + if ( level.time - cs->weaponFireTimes[cs->weaponNum] > hitDelay ) { + // do melee damage + if ( ( tr = CheckMeleeAttack( ent, 44, qfalse ) ) && ( tr->entityNum == cs->enemyNum ) ) { + G_Damage( &g_entities[tr->entityNum], ent, ent, vec3_origin, tr->endpos, + warriorHitDamage[anim], 0, MOD_GAUNTLET ); + G_AddEvent( ent, EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[ent->aiCharacter].soundScripts[STAYSOUNDSCRIPT] ) ); + } else { + G_AddEvent( ent, EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[ent->aiCharacter].soundScripts[FOLLOWSOUNDSCRIPT] ) ); + } + cs->weaponFireTimes[cs->weaponNum] = level.time; + cs->animHitCount++; + } + } + // face them + AICast_AimAtEnemy( cs ); + if ( anim < 3 ) { // back handed-swinging, dont allow legs to move + // if they are outside range, move forward + AICast_PredictMovement( ecs, 2, 0.5, &move, &g_entities[cs->enemyNum].client->pers.cmd, -1 ); + enemyDist = Distance( move.endpos, cs->bs->origin ); + enemyDist -= g_entities[cs->enemyNum].r.maxs[0]; + enemyDist -= ent->r.maxs[0]; + if ( enemyDist > 16 ) { // we can get closer + if ( ent->client->ps.legsTimer ) { + ent->client->ps.legsTimer = 0; // allow legs us to move + if ( cs->castScriptStatus.scriptNoMoveTime < level.time + 200 ) { // dont move until the legs are done lerping out of attack anim + cs->castScriptStatus.scriptNoMoveTime = level.time + 200; + } + } + if ( cs->castScriptStatus.scriptNoMoveTime < level.time ) { + trap_EA_MoveForward( cs->entityNum ); + } + } + } + } + + return NULL; +} + +/* +================ +AIFunc_WarriorZombieMeleeStart +================ +*/ +char *AIFunc_WarriorZombieMeleeStart( cast_state_t *cs ) { + gentity_t *ent; + + ent = &g_entities[cs->entityNum]; + ent->s.effect1Time = level.time; + cs->ideal_viewangles[YAW] = cs->viewangles[YAW]; + cs->weaponFireTimes[cs->weaponNum] = level.time; + cs->animHitCount = 0; + + // face them + AICast_AimAtEnemy( cs ); + + // audible sound + AIChar_AttackSound( cs ); + + // play an anim + BG_UpdateConditionValue( cs->entityNum, ANIM_COND_WEAPON, cs->weaponNum, qtrue ); + BG_AnimScriptEvent( &ent->client->ps, ANIM_ET_FIREWEAPON, qfalse, qtrue ); + + // stop charging + BG_UpdateConditionValue( cs->entityNum, ANIM_COND_CHARGING, 0, qfalse ); + ent->flags &= ~FL_WARZOMBIECHARGE; + + cs->aifunc = AIFunc_WarriorZombieMelee; + return "AIFunc_WarriorZombieMelee"; +} + +// Warrior "sight" animation +/* +================ +AIFunc_WarriorZombieSight +================ +*/ +char *AIFunc_WarriorZombieSight( cast_state_t *cs ) { + gentity_t *ent = &g_entities[cs->entityNum]; + + if ( !ent->client->ps.torsoTimer ) { + return AIFunc_DefaultStart( cs ); + } + return NULL; +} + +/* +================ +AIFunc_WarriorZombieSightStart +================ +*/ +char *AIFunc_WarriorZombieSightStart( cast_state_t *cs ) { + gentity_t *ent; + +// RF, disabled + return NULL; + + ent = &g_entities[cs->entityNum]; + cs->ideal_viewangles[YAW] = cs->viewangles[YAW]; + cs->weaponFireTimes[cs->weaponNum] = level.time; + + // face them + AICast_AimAtEnemy( cs ); + + // anim + BG_AnimScriptEvent( &ent->client->ps, ANIM_ET_FIRSTSIGHT, qfalse, qtrue ); + //BG_PlayAnimName( &ent->client->ps, "first_sight", ANIM_BP_BOTH, qtrue, qfalse, qtrue ); + + cs->aifunc = AIFunc_WarriorZombieSight; + return "AIFunc_WarriorZombieSight"; +} + +/* +================ +AIFunc_WarriorZombieDefense +================ +*/ +char *AIFunc_WarriorZombieDefense( cast_state_t *cs ) { + gentity_t *ent, *enemy; + vec3_t enemyDir, vec; + float dist; + + ent = &g_entities[cs->entityNum]; + + if ( !( ent->flags & FL_DEFENSE_GUARD ) ) { + if ( cs->weaponFireTimes[cs->weaponNum] < level.time - 100 ) { + return AIFunc_DefaultStart( cs ); + } + return NULL; + } + + if ( ( cs->enemyNum < 0 ) || ( cs->dangerEntityValidTime >= level.time ) ) { + ent->flags &= ~FL_DEFENSE_GUARD; + ent->client->ps.torsoTimer = 0; + ent->client->ps.legsTimer = 0; + return NULL; + } + + enemy = &g_entities[cs->enemyNum]; + + if ( cs->thinkFuncChangeTime < level.time - 1500 ) { + // if we cant see them + if ( !AICast_EntityVisible( cs, cs->enemyNum, qtrue ) ) { + ent->flags &= ~FL_DEFENSE_GUARD; + ent->client->ps.torsoTimer = 0; + ent->client->ps.legsTimer = 0; + return NULL; + } + + // if our enemy isn't using a dangerous weapon + if ( enemy->client->ps.weapon < WP_LUGER || enemy->client->ps.weapon > WP_CLASS_SPECIAL ) { + ent->flags &= ~FL_DEFENSE_GUARD; + ent->client->ps.torsoTimer = 0; + ent->client->ps.legsTimer = 0; + return NULL; + } + + // if our enemy isn't looking right at us, abort + VectorSubtract( ent->client->ps.origin, enemy->client->ps.origin, vec ); + dist = VectorNormalize( vec ); + if ( dist > 512 ) { + dist = 512; + } + AngleVectors( enemy->client->ps.viewangles, enemyDir, NULL, NULL ); + if ( DotProduct( vec, enemyDir ) < ( 0.98 - 0.2 * ( dist / 512 ) ) ) { + ent->flags &= ~FL_DEFENSE_GUARD; + ent->client->ps.torsoTimer = 0; + ent->client->ps.legsTimer = 0; + return NULL; + } + } + + cs->weaponFireTimes[cs->weaponNum] = level.time; + + if ( !ent->client->ps.torsoTimer ) { + ent->flags &= ~FL_DEFENSE_GUARD; + ent->client->ps.torsoTimer = 0; + ent->client->ps.legsTimer = 0; + return NULL; + } + + // face them + AICast_AimAtEnemy( cs ); + // crouching position, use smaller bounding box + trap_EA_Crouch( cs->bs->client ); + + return NULL; +} + +/* +================ +AIFunc_WarriorZombieDefenseStart +================ +*/ +char *AIFunc_WarriorZombieDefenseStart( cast_state_t *cs ) { + gentity_t *ent, *enemy; + vec3_t enemyDir, vec; + float dist; + static int lastWarriorDefense; + + if ( lastWarriorDefense <= level.time && lastWarriorDefense > level.time - 3000 ) { + return NULL; // dont all go into defense at once + } + lastWarriorDefense = level.time; + + ent = &g_entities[cs->entityNum]; + enemy = &g_entities[cs->enemyNum]; + + // if our enemy isn't using a dangerous weapon + if ( enemy->client->ps.weapon < WP_LUGER || enemy->client->ps.weapon > WP_CLASS_SPECIAL ) { + return NULL; + } + + // if we are doing a goto + if ( cs->followEntity >= 0 ) { + return NULL; + } + + // if our enemy isn't looking right at us, abort + VectorSubtract( ent->client->ps.origin, enemy->client->ps.origin, vec ); + dist = VectorNormalize( vec ); + if ( dist > 512 ) { + dist = 512; + } + if ( dist < 128 ) { + return NULL; + } + AngleVectors( enemy->client->ps.viewangles, enemyDir, NULL, NULL ); + if ( DotProduct( vec, enemyDir ) < ( 0.98 - 0.2 * ( dist / 512 ) ) ) { + return NULL; + } + + cs->weaponFireTimes[cs->weaponNum] = level.time; + + // face them + AICast_AimAtEnemy( cs ); + + // anim + BG_UpdateConditionValue( cs->entityNum, ANIM_COND_WEAPON, cs->weaponNum, qtrue ); + BG_AnimScriptEvent( &ent->client->ps, ANIM_ET_FIREWEAPON, qfalse, qtrue ); + ent->client->ps.torsoTimer = 3000; + ent->client->ps.legsTimer = 3000; + + ent->flags |= FL_DEFENSE_GUARD; + + // when they come out of defense mode, go into charge mode + BG_UpdateConditionValue( cs->entityNum, ANIM_COND_CHARGING, 1, qfalse ); + ent->flags |= FL_WARZOMBIECHARGE; + + cs->aifunc = AIFunc_WarriorZombieDefense; + return "AIFunc_WarriorZombieDefense"; +} diff --git a/src/game/ai_cast_func_boss1.c b/src/game/ai_cast_func_boss1.c new file mode 100644 index 0000000..2e37c75 --- /dev/null +++ b/src/game/ai_cast_func_boss1.c @@ -0,0 +1,1034 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: ai_cast_funcs.c +// Function: Wolfenstein AI Character Decision Making +// Programmer: Ridah +// Tab Size: 4 (real tabs) +//=========================================================================== + +#include "../game/g_local.h" +#include "../game/q_shared.h" +#include "../game/botlib.h" //bot lib interface +#include "../game/be_aas.h" +#include "../game/be_ea.h" +#include "../game/be_ai_gen.h" +#include "../game/be_ai_goal.h" +#include "../game/be_ai_move.h" +#include "../botai/botai.h" //bot ai interface + +#include "ai_cast.h" + +// TTimo: unused +//static vec3_t forward, right, up; + +//================================================================================= +// +// Helga, the first boss +// +//================================================================================= + +#define HELGA_SPIRIT_BUILDUP_TIME 8000 // last for this long +#define HELGA_SPIRIT_FADEOUT_TIME 1000 +#define HELGA_SPIRIT_DLIGHT_RADIUS_MAX 384 +#define HELGA_SPIRIT_FIRE_INTERVAL 1000 + +extern int lastZombieSpiritAttack; + +char *AIFunc_Helga_SpiritAttack( cast_state_t *cs ) { + bot_state_t *bs; + gentity_t *ent; + // + cs->aiFlags |= AIFL_SPECIAL_FUNC; + ent = &g_entities[cs->entityNum]; + bs = cs->bs; + // make sure we're still playing the right anim + if ( ( ent->client->ps.torsoAnim & ~ANIM_TOGGLEBIT ) - BG_AnimationIndexForString( "attack1", cs->entityNum ) ) { + return AIFunc_DefaultStart( cs ); + } + // + if ( cs->enemyNum < 0 ) { + ent->client->ps.torsoTimer = 0; + ent->client->ps.legsTimer = 0; + return AIFunc_DefaultStart( cs ); + } + // + // if we can't see them anymore, abort immediately + if ( cs->vislist[cs->enemyNum].real_visible_timestamp != cs->vislist[cs->enemyNum].real_update_timestamp ) { + ent->client->ps.torsoTimer = 0; + ent->client->ps.legsTimer = 0; + return AIFunc_DefaultStart( cs ); + } + // we are firing this weapon, so record it + cs->weaponFireTimes[WP_MONSTER_ATTACK2] = level.time; + // + // once an attack has started, only abort once the player leaves our view, or time runs out + if ( cs->thinkFuncChangeTime < level.time - HELGA_SPIRIT_BUILDUP_TIME ) { + // if enough time has elapsed, finish this attack + if ( level.time > cs->thinkFuncChangeTime + HELGA_SPIRIT_BUILDUP_TIME + HELGA_SPIRIT_FADEOUT_TIME ) { + ent->client->ps.torsoTimer = 0; + ent->client->ps.legsTimer = 0; + return AIFunc_DefaultStart( cs ); + } + } else { + + // set timers + ent->client->ps.torsoTimer = 1000; + ent->client->ps.legsTimer = 1000; + + // draw the client-side effect + ent->client->ps.eFlags |= EF_MONSTER_EFFECT; + + // inform the client of our enemies position + VectorCopy( g_entities[cs->enemyNum].client->ps.origin, ent->s.origin2 ); + ent->s.origin2[2] += g_entities[cs->enemyNum].client->ps.viewheight; + } + // + // + return NULL; +} + +char *AIFunc_Helga_SpiritAttack_Start( cast_state_t *cs ) { + gentity_t *ent; + // + ent = &g_entities[cs->entityNum]; + ent->s.otherEntityNum2 = cs->enemyNum; + ent->s.effect1Time = level.time; + cs->aiFlags |= AIFL_SPECIAL_FUNC; + // + // dont turn + cs->ideal_viewangles[YAW] = cs->viewangles[YAW]; + // play an anim + BG_UpdateConditionValue( cs->entityNum, ANIM_COND_WEAPON, WP_MONSTER_ATTACK2, qtrue ); + BG_AnimScriptEvent( &ent->client->ps, ANIM_ET_FIREWEAPON, qfalse, qtrue ); + // + cs->aifunc = AIFunc_Helga_SpiritAttack; + return "AIFunc_Helga_SpiritAttack"; +} + +//================================================================================= +// +// Standing melee attacks +// +//================================================================================= + +#define NUM_HELGA_ANIMS 3 +#define MAX_HELGA_IMPACTS 3 +int helgaHitTimes[NUM_HELGA_ANIMS][MAX_HELGA_IMPACTS] = { // up to three hits per attack + {ANIMLENGTH( 16,20 ),-1}, + {ANIMLENGTH( 11,20 ),ANIMLENGTH( 19,20 ),-1}, + {ANIMLENGTH( 10,20 ),ANIMLENGTH( 17,20 ),ANIMLENGTH( 26,20 )}, +}; +int helgaHitDamage[NUM_HELGA_ANIMS] = { + 20, + 14, + 12 +}; + +/* +================ +AIFunc_Helga_Melee +================ +*/ +char *AIFunc_Helga_Melee( cast_state_t *cs ) { + gentity_t *ent = &g_entities[cs->entityNum]; + gentity_t *enemy; + cast_state_t *ecs; + int hitDelay = -1, anim; + trace_t tr; + float enemyDist; + aicast_predictmove_t move; + vec3_t vec; + + cs->aiFlags |= AIFL_SPECIAL_FUNC; + + if ( !ent->client->ps.torsoTimer || !ent->client->ps.legsTimer ) { + cs->aiFlags &= ~AIFL_SPECIAL_FUNC; + return AIFunc_DefaultStart( cs ); + } + + if ( cs->enemyNum < 0 ) { + ent->client->ps.legsTimer = 0; // allow legs us to move + ent->client->ps.torsoTimer = 0; // allow legs us to move + cs->aiFlags &= ~AIFL_SPECIAL_FUNC; + return AIFunc_DefaultStart( cs ); + } + + ecs = AICast_GetCastState( cs->enemyNum ); + enemy = &g_entities[cs->enemyNum]; + + anim = ( ent->client->ps.torsoAnim & ~ANIM_TOGGLEBIT ) - BG_AnimationIndexForString( "attack3", cs->entityNum ); + if ( anim < 0 || anim >= NUM_HELGA_ANIMS ) { + // animation interupted + cs->aiFlags &= ~AIFL_SPECIAL_FUNC; + return AIFunc_DefaultStart( cs ); + //G_Error( "AIFunc_HelgaZombieMelee: helgaBoss using invalid or unknown attack anim" ); + } + if ( cs->animHitCount < MAX_HELGA_IMPACTS && helgaHitTimes[anim][cs->animHitCount] >= 0 ) { + + // face them + VectorCopy( cs->bs->origin, vec ); + vec[2] += ent->client->ps.viewheight; + VectorSubtract( enemy->client->ps.origin, vec, vec ); + VectorNormalize( vec ); + vectoangles( vec, cs->ideal_viewangles ); + cs->ideal_viewangles[PITCH] = AngleNormalize180( cs->ideal_viewangles[PITCH] ); + + // get hitDelay + if ( !cs->animHitCount ) { + hitDelay = helgaHitTimes[anim][cs->animHitCount]; + } else { + hitDelay = helgaHitTimes[anim][cs->animHitCount] - helgaHitTimes[anim][cs->animHitCount - 1]; + } + + // check for inflicting damage + if ( level.time - cs->weaponFireTimes[cs->weaponNum] > hitDelay ) { + // do melee damage + enemyDist = VectorDistance( enemy->r.currentOrigin, ent->r.currentOrigin ); + enemyDist -= g_entities[cs->enemyNum].r.maxs[0]; + enemyDist -= ent->r.maxs[0]; + if ( enemyDist < 10 + AICast_WeaponRange( cs, cs->weaponNum ) ) { + trap_Trace( &tr, ent->r.currentOrigin, NULL, NULL, enemy->r.currentOrigin, ent->s.number, MASK_SHOT ); + if ( tr.entityNum == cs->enemyNum ) { + G_Damage( &g_entities[tr.entityNum], ent, ent, vec3_origin, tr.endpos, + helgaHitDamage[anim], 0, MOD_GAUNTLET ); + G_AddEvent( enemy, EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[ent->aiCharacter].soundScripts[STAYSOUNDSCRIPT] ) ); + } + } + cs->weaponFireTimes[cs->weaponNum] = level.time; + cs->animHitCount++; + } + } + + // if they are outside range, move forward + AICast_PredictMovement( ecs, 2, 0.3, &move, &g_entities[cs->enemyNum].client->pers.cmd, -1 ); + VectorSubtract( move.endpos, cs->bs->origin, vec ); + vec[2] = 0; + enemyDist = VectorLength( vec ); + enemyDist -= g_entities[cs->enemyNum].r.maxs[0]; + enemyDist -= ent->r.maxs[0]; + if ( enemyDist > 8 ) { // we can get closer + //if (!ent->client->ps.legsTimer) { + // cs->castScriptStatus.scriptNoMoveTime = 0; + trap_EA_MoveForward( cs->entityNum ); + //} + //ent->client->ps.legsTimer = 0; // allow legs us to move + } + + return NULL; +} + +/* +================ +AIFunc_Helga_MeleeStart +================ +*/ +char *AIFunc_Helga_MeleeStart( cast_state_t *cs ) { + gentity_t *ent; + + ent = &g_entities[cs->entityNum]; + ent->s.effect1Time = level.time; + cs->ideal_viewangles[YAW] = cs->viewangles[YAW]; + cs->weaponFireTimes[cs->weaponNum] = level.time; + cs->animHitCount = 0; + cs->aiFlags |= AIFL_SPECIAL_FUNC; + + // face them + AICast_AimAtEnemy( cs ); + + // play an anim + BG_UpdateConditionValue( cs->entityNum, ANIM_COND_WEAPON, cs->weaponNum, qtrue ); + BG_AnimScriptEvent( &ent->client->ps, ANIM_ET_FIREWEAPON, qfalse, qtrue ); + + // play a sound + G_AddEvent( ent, EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[ent->aiCharacter].soundScripts[ATTACKSOUNDSCRIPT] ) ); + + cs->aifunc = AIFunc_Helga_Melee; + cs->aifunc( cs ); // think once now, to prevent a delay + return "AIFunc_Helga_Melee"; +} + + +//=================================================================== + +/* +============== +AIFunc_FlameZombie_Portal +============== +*/ +char *AIFunc_FlameZombie_Portal( cast_state_t *cs ) { + gentity_t *ent = &g_entities[cs->entityNum]; + // + if ( cs->thinkFuncChangeTime < level.time - PORTAL_ZOMBIE_SPAWNTIME ) { + // HACK, make them aware of the player + AICast_UpdateVisibility( &g_entities[cs->entityNum], AICast_FindEntityForName( "player" ), qfalse, qtrue ); + ent->s.time2 = 0; // turn spawning effect off + return AIFunc_DefaultStart( cs ); + } + // + return NULL; +} + +/* +============== +AIFunc_FlameZombie_PortalStart +============== +*/ +char *AIFunc_FlameZombie_PortalStart( cast_state_t *cs ) { + gentity_t *ent = &g_entities[cs->entityNum]; + // + ent->s.time2 = level.time + 200; // hijacking this for portal spawning effect + // + // play a special animation + ent->client->ps.torsoAnim = + ( ( ent->client->ps.torsoAnim & ANIM_TOGGLEBIT ) ^ ANIM_TOGGLEBIT ) | BOTH_EXTRA1; + ent->client->ps.legsAnim = + ( ( ent->client->ps.legsAnim & ANIM_TOGGLEBIT ) ^ ANIM_TOGGLEBIT ) | BOTH_EXTRA1; + ent->client->ps.torsoTimer = PORTAL_ZOMBIE_SPAWNTIME - 200; + ent->client->ps.legsTimer = PORTAL_ZOMBIE_SPAWNTIME - 200; + // + cs->thinkFuncChangeTime = level.time; + // + cs->aifunc = AIFunc_FlameZombie_Portal; + return "AIFunc_FlameZombie_Portal"; +} + + +//================================================================================= +// +// Heinrich, the LAST boss +// +//================================================================================= + +// +// Special Sound Precache +// + +typedef enum +{ + HEINRICH_SWORDIMPACT, + HEINRICH_SWORDLUNGE_START, + HEINRICH_SWORDKNOCKBACK_START, + HEINRICH_SWORDKNOCKBACK_WEAPON, + HEINRICH_SWORDSIDESLASH_START, + HEINRICH_SWORDSIDESLASH_WEAPON, + HEINRICH_EARTHQUAKE_START, + HEINRICH_RAISEDEAD_START, + HEINRICH_TAUNT_GOODHEALTH, + HEINRICH_TAUNT_LOWHEALTH, + + MAX_HEINRICH_SOUNDS +} heinrichSounds_t; + +char *heinrichSounds[MAX_HEINRICH_SOUNDS] = { + "heinrichSwordImpact", + "heinrichSwordLungeStart", + "heinrichSwordKnockbackStart", + "heinrichSwordKnockbackWeapon", + "heinrichSwordSideSlashStart", + "heinrichSwordSideSlashWeapon", + "heinrichSwordEarthquakeStart", + "heinrichRaiseWarriorStart", + "heinrichTauntGoodHealth", + "heinrichTauntLowHealth", +}; + +int heinrichSoundIndex[MAX_HEINRICH_SOUNDS]; + +void AICast_Heinrich_SoundPrecache( void ) { + int i; + for ( i = 0; i < MAX_HEINRICH_SOUNDS; i++ ) { + heinrichSoundIndex[i] = G_SoundIndex( heinrichSounds[i] ); + } +} + +void AICast_Heinrich_Taunt( cast_state_t *cs ) { + gentity_t *ent = &g_entities[cs->entityNum]; + static int lastTaunt; + // sound + if ( ent->health > cs->attributes[STARTING_HEALTH] * 0.25 ) { + if ( lastTaunt > level.time || lastTaunt < level.time - 20000 ) { + G_AddEvent( ent, EV_GENERAL_SOUND, heinrichSoundIndex[HEINRICH_TAUNT_GOODHEALTH] ); + lastTaunt = level.time; + } + } else { + if ( lastTaunt > level.time || lastTaunt < level.time - 40000 ) { + G_AddEvent( ent, EV_GENERAL_SOUND, heinrichSoundIndex[HEINRICH_TAUNT_LOWHEALTH] ); + lastTaunt = level.time; + } + } +} + +#define HEINRICH_LUNGE_DELAY ANIMLENGTH( 15,20 ) +#define HEINRICH_LUNGE_RANGE 170 +#define HEINRICH_LUNGE_DAMAGE ( 50 + rand() % 20 ) + +char *AIFunc_Heinrich_SwordLunge( cast_state_t *cs ) { + gentity_t *ent = &g_entities[cs->entityNum]; + trace_t *tr; + vec3_t left; + float enemyDist; + aicast_predictmove_t move; + vec3_t vec; + cast_state_t *ecs; + + cs->aiFlags |= AIFL_SPECIAL_FUNC; + + if ( cs->enemyNum < 0 ) { + if ( ent->client->ps.torsoTimer ) { + return NULL; + } + return AIFunc_DefaultStart( cs ); + } + + ecs = AICast_GetCastState( cs->enemyNum ); + + + if ( ent->client->ps.torsoTimer < 500 ) { + if ( !ent->client->ps.legsTimer ) { + trap_EA_MoveForward( cs->entityNum ); + } + ent->client->ps.legsTimer = 0; + ent->client->ps.torsoTimer = 0; + cs->castScriptStatus.scriptNoMoveTime = 0; + AICast_Heinrich_Taunt( cs ); + return AIFunc_BattleChaseStart( cs ); + } + + // time for the melee? + if ( cs->enemyNum >= 0 && !( cs->aiFlags & AIFL_MISCFLAG1 ) ) { + // face them + AICast_AimAtEnemy( cs ); + // keep checking for impact status + tr = CheckMeleeAttack( ent, HEINRICH_LUNGE_RANGE, qfalse ); +/* // do we need to move? + if (!(tr && (tr->entityNum == cs->enemyNum))) { + ent->client->ps.legsTimer = 0; + cs->castScriptStatus.scriptNoMoveTime = 0; + trap_EA_MoveForward( cs->entityNum ); + } +*/ // ready for damage? + if ( cs->thinkFuncChangeTime < level.time - HEINRICH_LUNGE_DELAY ) { + cs->aiFlags |= AIFL_MISCFLAG1; + // do melee damage + if ( tr && ( tr->entityNum == cs->enemyNum ) ) { + G_Damage( &g_entities[tr->entityNum], ent, ent, left, tr->endpos, HEINRICH_LUNGE_DAMAGE, 0, MOD_GAUNTLET ); + // sound + G_AddEvent( ent, EV_GENERAL_SOUND, heinrichSoundIndex[HEINRICH_SWORDIMPACT] ); + } + } + } + + // if they are outside range, move forward + AICast_PredictMovement( ecs, 2, 0.3, &move, &g_entities[cs->enemyNum].client->pers.cmd, -1 ); + VectorSubtract( move.endpos, cs->bs->origin, vec ); + vec[2] = 0; + enemyDist = VectorLength( vec ); + enemyDist -= g_entities[cs->enemyNum].r.maxs[0]; + enemyDist -= ent->r.maxs[0]; + if ( enemyDist > 30 ) { // we can get closer + if ( ent->client->ps.legsTimer ) { + cs->castScriptStatus.scriptNoMoveTime = level.time + 100; + ent->client->ps.legsTimer = 0; // allow legs to move us + } + if ( cs->castScriptStatus.scriptNoMoveTime < level.time ) { + trap_EA_MoveForward( cs->entityNum ); + } + } + + return NULL; +} + +char *AIFunc_Heinrich_SwordLungeStart( cast_state_t *cs ) { + gentity_t *ent = &g_entities[cs->entityNum]; +// gentity_t *enemy = &g_entities[cs->enemyNum]; + + cs->aiFlags |= AIFL_SPECIAL_FUNC; + // sound + G_AddEvent( ent, EV_GENERAL_SOUND, heinrichSoundIndex[HEINRICH_SWORDLUNGE_START] ); + // face them + AICast_AimAtEnemy( cs ); + // clear flags + cs->aiFlags &= ~( AIFL_MISCFLAG1 | AIFL_MISCFLAG2 ); + // play the anim + BG_PlayAnimName( &ent->client->ps, "attack9", ANIM_BP_BOTH, qtrue, qfalse, qtrue ); + // start the func + cs->aifunc = AIFunc_Heinrich_SwordLunge; + return "AIFunc_Heinrich_SwordLunge"; +} + +#define HEINRICH_KNOCKBACK_DELAY ANIMLENGTH( 26,20 ) +#define HEINRICH_KNOCKBACK_RANGE 150 +#define HEINRICH_KNOCKBACK_DAMAGE ( 60 + rand() % 20 ) + +char *AIFunc_Heinrich_SwordKnockback( cast_state_t *cs ) { + gentity_t *ent = &g_entities[cs->entityNum]; + trace_t *tr; + vec3_t right, left; +// float enemyDist; +// aicast_predictmove_t move; +// vec3_t vec; + cast_state_t *ecs; + + cs->aiFlags |= AIFL_SPECIAL_FUNC; + + if ( cs->enemyNum < 0 ) { + if ( ent->client->ps.torsoTimer ) { + return NULL; + } + return AIFunc_DefaultStart( cs ); + } + + ecs = AICast_GetCastState( cs->enemyNum ); + + if ( ent->client->ps.torsoTimer < 500 ) { + if ( !ent->client->ps.legsTimer ) { + trap_EA_MoveForward( cs->entityNum ); + } + ent->client->ps.legsTimer = 0; + ent->client->ps.torsoTimer = 0; + cs->castScriptStatus.scriptNoMoveTime = 0; + AICast_Heinrich_Taunt( cs ); + return AIFunc_BattleChaseStart( cs ); + } + + // time for the melee? + if ( cs->enemyNum >= 0 && !( cs->aiFlags & AIFL_MISCFLAG1 ) ) { + // face them + AICast_AimAtEnemy( cs ); + // keep checking for impact status + tr = CheckMeleeAttack( ent, HEINRICH_KNOCKBACK_RANGE, qfalse ); +/* // do we need to move? + if (!(tr && (tr->entityNum == cs->enemyNum))) { + ent->client->ps.legsTimer = 0; + cs->castScriptStatus.scriptNoMoveTime = 0; + trap_EA_MoveForward( cs->entityNum ); + } +*/ // ready for damage? + if ( cs->thinkFuncChangeTime < level.time - HEINRICH_KNOCKBACK_DELAY ) { + cs->aiFlags |= AIFL_MISCFLAG1; + // do melee damage + if ( tr && ( tr->entityNum == cs->enemyNum ) ) { + AngleVectors( cs->viewangles, NULL, right, NULL ); + VectorNegate( right, left ); + G_Damage( &g_entities[tr->entityNum], ent, ent, left, tr->endpos, HEINRICH_KNOCKBACK_DAMAGE, 0, MOD_GAUNTLET ); + // sound + G_AddEvent( ent, EV_GENERAL_SOUND, heinrichSoundIndex[HEINRICH_SWORDIMPACT] ); + // throw them in direction of impact + if ( ( ent->client->ps.torsoAnim & ~ANIM_TOGGLEBIT ) == BG_AnimationIndexForString( "attack2", cs->entityNum ) ) { + // right + right[2] = 0.5; + VectorMA( g_entities[cs->enemyNum].client->ps.velocity, 400, right, g_entities[cs->enemyNum].client->ps.velocity ); + } else { + // left + left[2] = 0.5; + VectorMA( g_entities[cs->enemyNum].client->ps.velocity, 400, left, g_entities[cs->enemyNum].client->ps.velocity ); + } + } + } + } +/* DISABLED FOR SWORDKNOCKBACK..looks bad + // if they are outside range, move forward + AICast_PredictMovement( ecs, 2, 0.3, &move, &g_entities[cs->enemyNum].client->pers.cmd, -1 ); + VectorSubtract( move.endpos, cs->bs->origin, vec ); + vec[2] = 0; + enemyDist = VectorLength( vec ); + enemyDist -= g_entities[cs->enemyNum].r.maxs[0]; + enemyDist -= ent->r.maxs[0]; + if (enemyDist > 30) { // we can get closer + if (ent->client->ps.legsTimer) { + cs->castScriptStatus.scriptNoMoveTime = level.time + 100; + ent->client->ps.legsTimer = 0; // allow legs to move us + } + if (cs->castScriptStatus.scriptNoMoveTime < level.time) { + trap_EA_MoveForward(cs->entityNum); + } + } +*/ + return NULL; +} + +char *AIFunc_Heinrich_SwordKnockbackStart( cast_state_t *cs ) { + gentity_t *ent = &g_entities[cs->entityNum]; +// gentity_t *enemy = &g_entities[cs->enemyNum]; + + cs->aiFlags |= AIFL_SPECIAL_FUNC; + // sound + G_AddEvent( ent, EV_GENERAL_SOUND, heinrichSoundIndex[HEINRICH_SWORDKNOCKBACK_START] ); + // weapon sound + G_AddEvent( ent, EV_GENERAL_SOUND, heinrichSoundIndex[HEINRICH_SWORDKNOCKBACK_WEAPON] ); + // face them + AICast_AimAtEnemy( cs ); + // clear flags + cs->aiFlags &= ~( AIFL_MISCFLAG1 | AIFL_MISCFLAG2 ); + // play the anim + if ( rand() % 2 ) { + BG_PlayAnimName( &ent->client->ps, "attack2", ANIM_BP_BOTH, qtrue, qfalse, qtrue ); + } else { + BG_PlayAnimName( &ent->client->ps, "attack3", ANIM_BP_BOTH, qtrue, qfalse, qtrue ); + } + // start the func + cs->aifunc = AIFunc_Heinrich_SwordKnockback; + return "AIFunc_Heinrich_SwordKnockback"; +} + +#define HEINRICH_SLASH_DELAY ANIMLENGTH( 17,25 ) +#define HEINRICH_SLASH_RANGE 140 +#define HEINRICH_SLASH_DAMAGE ( 30 + rand() % 15 ) + +char *AIFunc_Heinrich_SwordSideSlash( cast_state_t *cs ) { + gentity_t *ent = &g_entities[cs->entityNum]; + trace_t *tr; + vec3_t right, left; + float enemyDist; + aicast_predictmove_t move; + vec3_t vec; + cast_state_t *ecs; + + cs->aiFlags |= AIFL_SPECIAL_FUNC; + + if ( cs->enemyNum < 0 ) { + if ( ent->client->ps.torsoTimer ) { + return NULL; + } + return AIFunc_DefaultStart( cs ); + } + + ecs = AICast_GetCastState( cs->enemyNum ); + + if ( ent->client->ps.torsoTimer < 500 ) { + if ( !ent->client->ps.legsTimer ) { + trap_EA_MoveForward( cs->entityNum ); + } + ent->client->ps.legsTimer = 0; + ent->client->ps.torsoTimer = 0; + cs->castScriptStatus.scriptNoMoveTime = 0; + AICast_Heinrich_Taunt( cs ); + return AIFunc_BattleChaseStart( cs ); + } + + // time for the melee? + if ( cs->enemyNum >= 0 && !( cs->aiFlags & AIFL_MISCFLAG1 ) ) { + // face them + AICast_AimAtEnemy( cs ); + // keep checking for impact status + tr = CheckMeleeAttack( ent, HEINRICH_SLASH_RANGE, qfalse ); + // ready for damage? + if ( cs->thinkFuncChangeTime < level.time - HEINRICH_SLASH_DELAY ) { + cs->aiFlags |= AIFL_MISCFLAG1; + // do melee damage + if ( tr && ( tr->entityNum == cs->enemyNum ) ) { + AngleVectors( cs->viewangles, NULL, right, NULL ); + VectorNegate( right, left ); + G_Damage( &g_entities[tr->entityNum], ent, ent, left, tr->endpos, HEINRICH_SLASH_DAMAGE, 0, MOD_GAUNTLET ); + // sound + G_AddEvent( ent, EV_GENERAL_SOUND, heinrichSoundIndex[HEINRICH_SWORDIMPACT] ); + // throw them in direction of impact + left[2] = 0.5; + VectorMA( g_entities[cs->enemyNum].client->ps.velocity, 400, left, g_entities[cs->enemyNum].client->ps.velocity ); + } + } + } + + // if they are outside range, move forward + AICast_PredictMovement( ecs, 2, 0.3, &move, &g_entities[cs->enemyNum].client->pers.cmd, -1 ); + VectorSubtract( move.endpos, cs->bs->origin, vec ); + vec[2] = 0; + enemyDist = VectorLength( vec ); + enemyDist -= g_entities[cs->enemyNum].r.maxs[0]; + enemyDist -= ent->r.maxs[0]; + if ( enemyDist > 30 ) { // we can get closer + if ( ent->client->ps.legsTimer ) { + cs->castScriptStatus.scriptNoMoveTime = level.time + 100; + ent->client->ps.legsTimer = 0; // allow legs to move us + } + if ( cs->castScriptStatus.scriptNoMoveTime < level.time ) { + trap_EA_MoveForward( cs->entityNum ); + } + } + + return NULL; +} + +char *AIFunc_Heinrich_SwordSideSlashStart( cast_state_t *cs ) { + gentity_t *ent = &g_entities[cs->entityNum]; + + cs->aiFlags |= AIFL_SPECIAL_FUNC; + // sound + G_AddEvent( ent, EV_GENERAL_SOUND, heinrichSoundIndex[HEINRICH_SWORDSIDESLASH_START] ); + // weapon sound + G_AddEvent( ent, EV_GENERAL_SOUND, heinrichSoundIndex[HEINRICH_SWORDSIDESLASH_WEAPON] ); + // face them + AICast_AimAtEnemy( cs ); + // clear flags + cs->aiFlags &= ~( AIFL_MISCFLAG1 | AIFL_MISCFLAG2 ); + // play the anim + BG_PlayAnimName( &ent->client->ps, "attack8", ANIM_BP_BOTH, qtrue, qfalse, qtrue ); + // start the func + cs->aifunc = AIFunc_Heinrich_SwordSideSlash; + return "AIFunc_Heinrich_SwordSideSlash"; +} + +#define HEINRICH_STOMP_DELAY 900 +#define HEINRICH_STOMP_RANGE 1024.0 +#define HEINRICH_STOMP_VELOCITY_Z 420 +#define HEINRICH_STOMP_DAMAGE 35 + +char *AIFunc_Heinrich_Earthquake( cast_state_t *cs ) { + gentity_t *ent = &g_entities[cs->entityNum]; + gentity_t *enemy; + cast_state_t *ecs; + vec3_t enemyVec; + float enemyDist, scale; + trace_t *tr; + + cs->aiFlags |= AIFL_SPECIAL_FUNC; + + if ( cs->enemyNum < 0 ) { + if ( !ent->client->ps.torsoTimer ) { + return AIFunc_DefaultStart( cs ); + } + return NULL; + } + + enemy = &g_entities[cs->enemyNum]; + ecs = AICast_GetCastState( cs->enemyNum ); + + VectorMA( enemy->r.currentOrigin, HEINRICH_STOMP_DELAY, enemy->client->ps.velocity, enemyVec ); + enemyDist = VectorDistance( ent->r.currentOrigin, enemyVec ); + + if ( ent->client->ps.torsoTimer < 500 ) { + int rnd; + aicast_predictmove_t move; + vec3_t vec; + + AICast_PredictMovement( ecs, 2, 0.5, &move, &g_entities[cs->enemyNum].client->pers.cmd, -1 ); + VectorSubtract( move.endpos, cs->bs->origin, vec ); + vec[2] = 0; + enemyDist = VectorLength( vec ); + enemyDist -= g_entities[cs->enemyNum].r.maxs[0]; + enemyDist -= ent->r.maxs[0]; + // + if ( enemyDist < 140 ) { + // combo attack + rnd = rand() % 3; + switch ( rnd ) { + case 0: + return AIFunc_Heinrich_SwordSideSlashStart( cs ); + case 1: + return AIFunc_Heinrich_SwordKnockbackStart( cs ); + case 2: + return AIFunc_Heinrich_SwordLungeStart( cs ); + } + } else { // back to roaming + ent->client->ps.legsTimer = 0; + ent->client->ps.torsoTimer = 0; + cs->castScriptStatus.scriptNoMoveTime = 0; + AICast_Heinrich_Taunt( cs ); + return AIFunc_DefaultStart( cs ); + } + } + + // time for the thump? + if ( !( cs->aiFlags & AIFL_MISCFLAG1 ) ) { + // face them + AICast_AimAtEnemy( cs ); + // ready for damage? + if ( cs->thinkFuncChangeTime < level.time - HEINRICH_STOMP_DELAY ) { + cs->aiFlags |= AIFL_MISCFLAG1; + // play the stomp sound + G_AddEvent( ent, EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[ent->aiCharacter].soundScripts[ORDERSDENYSOUNDSCRIPT] ) ); + // check for striking the player + tr = CheckMeleeAttack( ent, 70, qfalse ); + // do melee damage + if ( tr && ( tr->entityNum == cs->enemyNum ) ) { + G_Damage( &g_entities[tr->entityNum], ent, ent, vec3_origin, tr->endpos, HEINRICH_STOMP_DAMAGE, 0, MOD_GAUNTLET ); + } + // call the debris trigger + AICast_ScriptEvent( cs, "trigger", "quake" ); + } + } + + enemyDist = Distance( enemy->s.pos.trBase, ent->s.pos.trBase ); + + // do the earthquake effects + if ( cs->thinkFuncChangeTime < level.time - HEINRICH_STOMP_DELAY ) { + // throw the player into the air, if they are on the ground + if ( ( enemy->s.groundEntityNum != ENTITYNUM_NONE ) && enemyDist < HEINRICH_STOMP_RANGE ) { + scale = 0.5 + 0.5 * ( (float)ent->client->ps.torsoTimer / 1000.0 ); + if ( scale > 1.0 ) { + scale = 1.0; + } + VectorSubtract( ent->s.pos.trBase, enemy->s.pos.trBase, enemyVec ); + VectorScale( enemyVec, 2.0 * ( 0.6 + 0.5 * random() ) * scale * ( 0.6 + 0.6 * ( 1.0 - ( enemyDist / HEINRICH_STOMP_RANGE ) ) ), enemyVec ); + enemyVec[2] = scale * HEINRICH_STOMP_VELOCITY_Z * ( 1.0 - 0.5 * ( enemyDist / HEINRICH_STOMP_RANGE ) ); + // bounce the player using this velocity + VectorAdd( enemy->client->ps.velocity, enemyVec, enemy->client->ps.velocity ); + } + } + + return NULL; +} + +char *AIFunc_Heinrich_MeleeStart( cast_state_t *cs ) { + gentity_t *ent = &g_entities[cs->entityNum]; + gentity_t *enemy = &g_entities[cs->enemyNum]; + int rnd; + static int lastStomp; + + if ( cs->enemyNum < 0 ) { + return NULL; + } + + // record weapon fire + cs->weaponFireTimes[cs->weaponNum] = level.time; + // face them + AICast_AimAtEnemy( cs ); + // clear flags + cs->aiFlags &= ~( AIFL_MISCFLAG1 | AIFL_MISCFLAG2 ); + // decide which attack to use + if ( VectorDistance( ent->r.currentOrigin, enemy->r.currentOrigin ) < 60 ) { + rnd = 0; // sword slash up close + } else if ( VectorDistance( ent->r.currentOrigin, enemy->r.currentOrigin ) >= HEINRICH_SLASH_RANGE ) { + rnd = 1; // too far away, stomp + } else { + // pick at random + rnd = rand() % 2; + } + // + switch ( rnd ) { + case 0: + { + int rnd = rand() % 3; + switch ( rnd ) { + case 0: + return AIFunc_Heinrich_SwordSideSlashStart( cs ); + case 1: + return AIFunc_Heinrich_SwordKnockbackStart( cs ); + case 2: + return AIFunc_Heinrich_SwordLungeStart( cs ); + } + } + case 1: + // dont do stomp too often + if ( lastStomp > level.time - 12000 ) { // plenty of time to let debris disappear + return NULL; + } + lastStomp = level.time; + cs->aiFlags |= AIFL_SPECIAL_FUNC; + // sound + G_AddEvent( ent, EV_GENERAL_SOUND, heinrichSoundIndex[HEINRICH_EARTHQUAKE_START] ); + // play the anim + BG_PlayAnimName( &ent->client->ps, "attack7", ANIM_BP_BOTH, qtrue, qfalse, qtrue ); + // start the func + cs->aifunc = AIFunc_Heinrich_Earthquake; + return "AIFunc_Heinrich_Earthquake"; + } + // shutup compiler + return NULL; +} + +#define HEINRICH_RAISEDEAD_DELAY 1200 +#define HEINRICH_RAISEDEAD_COUNT 3 +int lastRaise; + +char *AIFunc_Heinrich_RaiseDead( cast_state_t *cs ) { + int i; + gentity_t *ent = &g_entities[cs->entityNum]; + gentity_t *enemy = &g_entities[cs->enemyNum]; + gentity_t *trav, *closest; + float closestDist, dist; + // + cs->aiFlags |= AIFL_SPECIAL_FUNC; + if ( cs->enemyNum < 0 ) { + if ( !ent->client->ps.torsoTimer ) { + return AIFunc_DefaultStart( cs ); + } + return NULL; + } + // + // record weapon fire + cs->weaponFireTimes[cs->weaponNum] = level.time; + // + if ( !ent->client->ps.torsoTimer ) { + return AIFunc_DefaultStart( cs ); + } + if ( ent->count2 && lastRaise < level.time - HEINRICH_RAISEDEAD_DELAY ) { + lastRaise = level.time; + // summons the closest warrior + closest = NULL; + closestDist = 0; // shutup the compiler + for ( i = 0, trav = g_entities; i < level.maxclients; i++, trav++ ) { + if ( !trav->inuse ) { + continue; + } + if ( !trav->aiInactive ) { + continue; + } + if ( trav->aiCharacter != AICHAR_WARZOMBIE ) { + continue; + } + dist = VectorDistance( trav->s.pos.trBase, enemy->r.currentOrigin ); + if ( !closest || dist < closestDist ) { + closest = trav; + closestDist = dist; + } + } + // + if ( closest ) { + closest->AIScript_AlertEntity( closest ); + // make them aware of the player + AICast_UpdateVisibility( closest, enemy, qtrue, qtrue ); + // reduce the count + ent->count2--; + } + } + // + return NULL; +} + +char *AIFunc_Heinrich_RaiseDeadStart( cast_state_t *cs ) { + int i, cnt, free; + gentity_t *ent = &g_entities[cs->entityNum]; +// gentity_t *enemy = &g_entities[cs->enemyNum]; + gentity_t *trav, *spirits; + float circleDist; + // + // count the number of active warriors + cnt = 0; + free = 0; + for ( i = 0, trav = g_entities; i < level.maxclients; i++, trav++ ) { + if ( !trav->inuse ) { + continue; + } + if ( trav->aiCharacter != AICHAR_WARZOMBIE ) { + continue; + } + if ( trav->aiInactive ) { + free++; + continue; + } + if ( trav->health <= 0 ) { + continue; + } + cnt++; + } + // + if ( cnt < HEINRICH_RAISEDEAD_COUNT && free ) { // need a new one + cs->aiFlags &= ~AIFL_MISCFLAG1; + ent->count2 = HEINRICH_RAISEDEAD_COUNT - cnt; + lastRaise = level.time; + cs->aiFlags |= AIFL_SPECIAL_FUNC; + // start the animation + BG_PlayAnimName( &ent->client->ps, "attack4", ANIM_BP_BOTH, qtrue, qfalse, qtrue ); + // play the sound + G_AddEvent( ent, EV_GENERAL_SOUND, heinrichSoundIndex[HEINRICH_RAISEDEAD_START] ); + // start the func + cs->aifunc = AIFunc_Heinrich_RaiseDead; + return "AIFunc_Heinrich_RaiseDead"; + } + // enable all the spirit spawners + trav = NULL; + // TTimo: gcc: suggest () around assignment used as truth value + while ( ( trav = G_Find( trav, FOFS( classname ), "func_bats" ) ) ) { + if ( !trav->active && trav->spawnflags & 4 ) { + trav->active = 1; // let them release spirits now + } + } + // is the player outside the circle? + trav = NULL; + // TTimo: gcc: suggest () around assignment used as truth value + while ( ( trav = G_Find( trav, FOFS( classname ), "func_bats" ) ) ) { + if ( trav->spawnflags & 4 ) { + spirits = trav; + circleDist = trav->radius; + trav = G_Find( NULL, FOFS( targetname ), trav->target ); + if ( trav ) { + if ( VectorDistance( g_entities[0].s.pos.trBase, trav->s.origin ) > circleDist ) { + cs->aiFlags &= ~AIFL_MISCFLAG1; + ent->count2 = 0; + cs->aiFlags |= AIFL_SPECIAL_FUNC; + // start the animation + BG_PlayAnimName( &ent->client->ps, "attack4", ANIM_BP_BOTH, qtrue, qfalse, qtrue ); + // play the sound + G_AddEvent( ent, EV_GENERAL_SOUND, heinrichSoundIndex[HEINRICH_RAISEDEAD_START] ); + // start the func + cs->aifunc = AIFunc_Heinrich_RaiseDead; + return "AIFunc_Heinrich_RaiseDead"; + } + } + break; + } + } + // + return NULL; +} + +char *AIFunc_Heinrich_SpawnSpiritsStart( cast_state_t *cs ) { + gentity_t *ent = &g_entities[cs->entityNum]; + gentity_t *trav, *spirits; + float circleDist; + // + // enable all the spirit spawners + trav = NULL; + // TTimo: gcc: suggest () around assignment used as truth value + while ( ( trav = G_Find( trav, FOFS( classname ), "func_bats" ) ) ) { + if ( !trav->active && trav->spawnflags & 4 ) { + trav->active = 1; // let them release spirits now + } + } + // is the player outside the circle? + trav = NULL; + // TTimo: gcc: suggest () around assignment used as truth value + while ( ( trav = G_Find( trav, FOFS( classname ), "func_bats" ) ) ) { + if ( trav->spawnflags & 4 ) { + spirits = trav; + circleDist = trav->radius; + trav = G_Find( NULL, FOFS( targetname ), trav->target ); + if ( trav ) { + if ( VectorDistance( g_entities[0].s.pos.trBase, trav->s.origin ) > circleDist ) { + cs->aiFlags &= ~AIFL_MISCFLAG1; + ent->count2 = 0; + cs->aiFlags |= AIFL_SPECIAL_FUNC; + // start the animation + BG_PlayAnimName( &ent->client->ps, "attack4", ANIM_BP_BOTH, qtrue, qfalse, qtrue ); + // play the sound + G_AddEvent( ent, EV_GENERAL_SOUND, heinrichSoundIndex[HEINRICH_RAISEDEAD_START] ); + // start the func + cs->aifunc = AIFunc_Heinrich_RaiseDead; // just do raise dead, without raising any warriors + return "AIFunc_Heinrich_RaiseDead"; + } + } + break; + } + } + // + return NULL; +} \ No newline at end of file diff --git a/src/game/ai_cast_funcs.c b/src/game/ai_cast_funcs.c new file mode 100644 index 0000000..c581f22 --- /dev/null +++ b/src/game/ai_cast_funcs.c @@ -0,0 +1,5137 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +/* + * name: ai_cast_funcs.c + * + * desc: Wolfenstein AI Character Decision Making + * +*/ + + +#include "../game/g_local.h" +#include "../game/q_shared.h" +#include "../game/botlib.h" //bot lib interface +#include "../game/be_aas.h" +#include "../game/be_ea.h" +#include "../game/be_ai_gen.h" +#include "../game/be_ai_goal.h" +#include "../game/be_ai_move.h" +#include "../botai/botai.h" //bot ai interface + +#include "ai_cast.h" + +/* +This file contains the generic thinking states for the characters. + +Different types of movement or behaviour will be represented by +a seperate thinking function, which may or may not pass control +over to a new behaviour function. + +If control is passed onto a new function, the string name of the +current function is returned, mostly for debugging purposes. + +!!! NOTE: control must not be passed to a new AI func from outside of +this file. A new AI func must only be called from within another AI func. + +This gives us the ability to keep all code related to sections of AI +self-contained, so adding new features to the AI will be less likely to +step on other areas of AI. +*/ + +static int enemies[MAX_CLIENTS], numEnemies; + +// this is used to prevent try/abort/try/abort/etc grenade flush behaviour +static int lastGrenadeFlush = 0; + +#define AICAST_LEADERDIST_MAX 240 // try and stay at least this close to them when nothing else to do +#define AICAST_LEADERDIST_MIN 64 // get this close if we have a clear line of sight to them + +char *AIFunc_BattleChase( cast_state_t *cs ); +char *AIFunc_Battle( cast_state_t *cs ); + +static bot_moveresult_t *moveresult; + +/* +============ +AIFunc_Restore() + + restores the last aifunc that was backed up +============ +*/ +char *AIFunc_Restore( cast_state_t *cs ) { + // if the old aifunc was BattleChase, set it back to Battle, in case we have found a good position + if ( cs->oldAifunc == AIFunc_BattleChase ) { + cs->oldAifunc = AIFunc_Battle; + } + cs->aifunc = cs->oldAifunc; + return cs->aifunc( cs ); +} + +/* +============ +AICast_GetRandomViewAngle() +============ +*/ +float AICast_GetRandomViewAngle( cast_state_t *cs, float tracedist ) { + int cnt, passent, contents_mask; + vec3_t vec, dir, start, end; + trace_t trace; + float bestdist, bestyaw; + + cnt = 0; + VectorClear( vec ); + // + VectorCopy( cs->bs->origin, start ); + start[2] += cs->bs->cur_ps.viewheight; + // + passent = cs->entityNum; + contents_mask = CONTENTS_SOLID | CONTENTS_PLAYERCLIP | CONTENTS_WATER | CONTENTS_SLIME; +// contents_mask = CONTENTS_SOLID|CONTENTS_PLAYERCLIP|CONTENTS_WATER; + bestdist = 0; + bestyaw = 0; + // + while ( cnt++ < 4 ) + { + vec[YAW] = random() * 360.0; + // + AngleVectors( vec, dir, NULL, NULL ); + VectorMA( start, tracedist, dir, end ); + // + trap_Trace( &trace, start, NULL, NULL, end, passent, contents_mask ); + // + if ( trace.fraction >= 1 ) { + return vec[YAW]; + } else if ( !bestdist || bestdist < trace.fraction ) { + bestdist = trace.fraction; + bestyaw = vec[YAW]; + } + } + // + if ( bestdist ) { + return bestyaw; + } + // just return their current direction + return cs->ideal_viewangles[YAW]; +} + +/* +============ +AICast_MoveToPos() + + returns a pointer to the moveresult it used to make the move, so we can investigate it + outside of this function +============ +*/ +bot_moveresult_t *AICast_MoveToPos( cast_state_t *cs, vec3_t pos, int entnum ) { + bot_goal_t goal; + vec3_t /*target,*/ dir; + static bot_moveresult_t lmoveresult; + int tfl; + bot_state_t *bs; + float dist; + +//int pretime = Sys_MilliSeconds(); + + moveresult = NULL; + + if ( cs->castScriptStatus.scriptNoMoveTime > level.time ) { + return NULL; + } + if ( cs->pauseTime > level.time ) { + return NULL; + } + // + bs = cs->bs; + tfl = cs->travelflags; + //if in lava or slime the bot should be able to get out + if ( BotInLava( bs ) ) { + tfl |= TFL_LAVA; + } + if ( BotInSlime( bs ) ) { + tfl |= TFL_SLIME; + } + // + //create the chase goal + memset( &goal, 0, sizeof( goal ) ); + goal.entitynum = entnum; + if ( entnum >= 0 && entnum < level.maxclients && caststates[entnum].lastValidAreaTime[cs->aasWorldIndex] > level.time - 100 ) { + goal.areanum = caststates[entnum].lastValidAreaNum[cs->aasWorldIndex]; + } else { + goal.areanum = BotPointAreaNum( pos ); + if ( entnum >= 0 && entnum < level.maxclients ) { + if ( !goal.areanum ) { + // use the last valid area + goal.areanum = caststates[entnum].lastValidAreaNum[cs->aasWorldIndex]; + } + } + } + VectorCopy( pos, goal.origin ); + VectorSet( goal.mins, -8, -8, -8 ); + VectorSet( goal.maxs, 8, 8, 8 ); + if ( entnum > -1 && entnum == cs->followEntity && !cs->followSlowApproach ) { + goal.flags |= GFL_NOSLOWAPPROACH; // just speed right passed it + } + // + // debugging, show the route + if ( aicast_debug.integer == 2 && ( g_entities[cs->entityNum].aiName && !strcmp( aicast_debugname.string, g_entities[cs->entityNum].aiName ) ) ) { + trap_AAS_RT_ShowRoute( cs->bs->origin, cs->bs->areanum, goal.areanum ); + } + // + //initialize the movement state + BotSetupForMovement( bs ); + //if this is a slow moving creature, don't use avoidreach + if ( cs->attributes[RUNNING_SPEED] < 100 ) { + //reset the avoid reach, otherwise bot is stuck in current area + trap_BotResetAvoidReach( bs->ms ); + } else if ( !VectorCompare( cs->lastMoveToPosGoalOrg, pos ) ) { + //reset the avoid reach, otherwise bot is stuck in current area + trap_BotResetAvoidReach( bs->ms ); + VectorCopy( pos, cs->lastMoveToPosGoalOrg ); + } + //move towards the goal + if ( !( cs->aiFlags & AIFL_EXPLICIT_ROUTING ) || ( entnum < 0 ) || Q_strcasecmp( g_entities[entnum].classname, "ai_marker" ) ) { + // use AAS routing + trap_BotMoveToGoal( &lmoveresult, bs->ms, &goal, tfl ); + //if the movement failed + if ( lmoveresult.failure ) { + + //reset the avoid reach, otherwise bot is stuck in current area + trap_BotResetAvoidReach( bs->ms ); + //BotAI_Print(PRT_MESSAGE, "movement failure %d\n", lmoveresult.traveltype); + // clear all movement + trap_EA_Move( cs->entityNum, vec3_origin, 0 ); + + } else { + + if ( entnum > 0 && goal.areanum && entnum >= 0 && entnum < level.maxclients ) { // NOTE: dont do this for the player + // save this destination point + caststates[entnum].lastValidAreaNum[cs->aasWorldIndex] = goal.areanum; + caststates[entnum].lastValidAreaTime[cs->aasWorldIndex] = level.time; + } + + if ( lmoveresult.flags & ( MOVERESULT_MOVEMENTVIEW | MOVERESULT_SWIMVIEW ) ) { + VectorCopy( lmoveresult.ideal_viewangles, cs->ideal_viewangles ); + VectorCopy( cs->ideal_viewangles, cs->viewlock_viewangles ); + cs->aiFlags |= AIFL_VIEWLOCKED; + } else if ( !( cs->bFlags & BFL_ATTACKED ) ) { // if we are attacking, don't change angles + bot_input_t bi; + + trap_EA_GetInput( bs->client, 0.1, &bi ); + if ( VectorLength( lmoveresult.movedir ) < 0.5 ) { + VectorSubtract( goal.origin, bs->origin, dir ); + vectoangles( dir, cs->ideal_viewangles ); + } else { + // use our velocity if we are moving + if ( VectorNormalize2( cs->bs->cur_ps.velocity, dir ) > 1 ) { + vectoangles( dir, cs->ideal_viewangles ); + } else { + vectoangles( lmoveresult.movedir, cs->ideal_viewangles ); + } + } + cs->ideal_viewangles[2] *= 0.5; + // look towards our future direction (like looking around a corner as we approach it) + if ( !( cs->aiFlags & AIFL_WALKFORWARD ) && ( lmoveresult.flags & MOVERESULT_FUTUREVIEW ) ) { + if ( AngleDifference( cs->ideal_viewangles[1], lmoveresult.ideal_viewangles[1] ) > 45 ) { + cs->ideal_viewangles[1] -= 45; + } else if ( AngleDifference( cs->ideal_viewangles[1], lmoveresult.ideal_viewangles[1] ) < -45 ) { + cs->ideal_viewangles[1] += 45; + } else { + cs->ideal_viewangles[1] = lmoveresult.ideal_viewangles[1]; + } + cs->ideal_viewangles[1] = AngleNormalize360( cs->ideal_viewangles[1] ); + cs->ideal_viewangles[0] = lmoveresult.ideal_viewangles[0]; + cs->ideal_viewangles[0] = 0.5 * AngleNormalize180( cs->ideal_viewangles[0] ); + } + } + + } + + } else { // manual routing towards markers + + VectorSubtract( pos, cs->bs->origin, dir ); + if ( ( dist = VectorNormalize( dir ) ) < 64 ) { + trap_EA_Move( cs->entityNum, dir, 100.0 + 300.0 * ( dist / 64.0 ) ); + } else { + trap_EA_Move( cs->entityNum, dir, 400 ); + } + + // look towards the marker also + vectoangles( dir, cs->ideal_viewangles ); + cs->ideal_viewangles[2] *= 0.5; + + } + // this must go last so we face the direction we avoid move + AICast_Blocked( cs, &lmoveresult, qfalse, &goal ); + +//G_Printf("MoveToPos: %i ms\n", -pretime + Sys_MilliSeconds() ); +/* +// debug, print movement info +if(0) // (SA) added to hide the print +{ +bot_input_t bi; + +trap_EA_GetInput(cs->bs->client, (float) level.time / 1000, &bi); +G_Printf("spd: %i\n", (int)bi.speed ); +} +*/ + return ( moveresult = &lmoveresult ); +} + +/* +============ +AICast_SpeedScaleForDistance() +============ +*/ +float AICast_SpeedScaleForDistance( cast_state_t *cs, float startdist, float idealDist ) { +#define PREDICT_TIME_WALK 0.2 +#define PREDICT_TIME_CROUCH 0.2 +#define PREDICT_TIME_RUN 0.3 + float speed, dist; + + dist = startdist - idealDist; + if ( dist < 1 ) { + dist = 1; + } + + // if walking + if ( cs->movestate == MS_WALK ) { + speed = cs->attributes[WALKING_SPEED]; + if ( speed * PREDICT_TIME_WALK > dist ) { + return 0.2 + 0.8 * ( dist / ( speed * PREDICT_TIME_WALK ) ); + } else { + return 1.0; + } + } else + // if crouching + if ( cs->movestate == MS_CROUCH || cs->attackcrouch_time > level.time ) { + speed = cs->attributes[CROUCHING_SPEED]; + if ( speed * PREDICT_TIME_CROUCH > dist ) { + return 0.3 + 0.7 * ( dist / ( speed * PREDICT_TIME_CROUCH ) ); + } else { + return 1.0; + } + } else + // running + { + speed = cs->attributes[RUNNING_SPEED]; + if ( speed * PREDICT_TIME_RUN > dist ) { + return 0.2 + 0.8 * ( dist / ( speed * PREDICT_TIME_RUN ) ); + } else { + return 1.0; + } + } +} + +/* +============ +AICast_SpecialFunc +============ +*/ +void AICast_SpecialFunc( cast_state_t *cs ) { + gentity_t *ent = &g_entities[cs->entityNum]; + gentity_t *enemy = NULL; + + if ( cs->enemyNum >= 0 ) { + enemy = &g_entities[cs->enemyNum]; + } + + switch ( cs->aiCharacter ) { + case AICHAR_WARZOMBIE: + // disable defense unless we want it + ent->flags &= ~FL_DEFENSE_CROUCH; + // if we are pursuing the player from a distance, use our "crouch moving defense" + if ( ( enemy ) + && ( cs->vislist[cs->enemyNum].real_visible_timestamp > level.time - 5000 ) + && ( Distance( cs->bs->origin, enemy->s.pos.trBase ) > 200 ) + && ( Distance( cs->bs->origin, enemy->s.pos.trBase ) < 600 ) + && ( cs->bs->cur_ps.groundEntityNum != ENTITYNUM_NONE ) + //&& (infront( ent, enemy )) + && ( infront( enemy, ent ) ) ) { + // crouch + trap_EA_Crouch( cs->entityNum ); + // enable defense pose + ent->flags |= FL_DEFENSE_CROUCH; + } + break; + case AICHAR_HELGA: + // if she has recently finished a spirit attack, go into charge mode + if ( ( cs->weaponFireTimes[WP_MONSTER_ATTACK2] && ( cs->weaponFireTimes[WP_MONSTER_ATTACK2] > level.time - 12000 ) ) || + ( cs->weaponFireTimes[WP_MONSTER_ATTACK1] && ( cs->weaponFireTimes[WP_MONSTER_ATTACK1] > level.time - 6000 ) ) ) { + BG_UpdateConditionValue( cs->entityNum, ANIM_COND_CHARGING, 1, qfalse ); + cs->actionFlags &= ~CASTACTION_WALK; + } else { // not charging + BG_UpdateConditionValue( cs->entityNum, ANIM_COND_CHARGING, 0, qfalse ); + } + // + if ( ent->health <= 0 && ent->takedamage ) { + if ( ent->client->ps.torsoTimer < 500 ) { + // blow up + GibEntity( ent, 0 ); + ent->takedamage = qfalse; + ent->r.contents = 0; + ent->health = GIB_HEALTH - 1; + } + } + break; + case AICHAR_HEINRICH: + if ( ( ent->health <= 0.25 * cs->attributes[STARTING_HEALTH] ) + || ( cs->weaponFireTimes[WP_MONSTER_ATTACK1] > level.time - 6000 ) // walk for period after attack + || ( cs->weaponFireTimes[WP_MONSTER_ATTACK1] % 8000 < 3000 ) ) { // dont run constantly + cs->actionFlags |= CASTACTION_WALK; + } else { // charging + cs->actionFlags &= ~CASTACTION_WALK; + } + // allow running while attacking + if ( ent->client->ps.torsoTimer && !ent->client->ps.legsTimer ) { + cs->actionFlags &= ~CASTACTION_WALK; + } + // + if ( ent->health <= 0 && ent->takedamage ) { + if ( ent->client->ps.torsoTimer < 500 ) { + // blow up + GibEntity( ent, 0 ); + ent->takedamage = qfalse; + ent->r.contents = 0; + ent->health = GIB_HEALTH - 1; + } + // blow up other warriors left around + if ( !ent->takedamage || ( ent->count2 < level.time && ent->client->ps.torsoTimer < 4000 ) ) { + int i; + gentity_t *trav; + for ( i = 0, trav = g_entities; i < level.maxclients; i++, trav++ ) { + if ( !trav->inuse ) { + continue; + } + if ( trav->aiCharacter != AICHAR_WARZOMBIE ) { + continue; + } + if ( trav->aiInactive ) { + continue; + } + if ( trav->health <= 0 ) { + continue; + } + // blow it up, set some delay + G_Damage( trav, ent, ent, NULL, NULL, 99999, 0, MOD_CRUSH ); + if ( ent->takedamage ) { + ent->count2 = level.time + 200 + rand() % 1500; + } + } + } + } + break; + case AICHAR_ZOMBIE: + if ( COM_BitCheck( cs->bs->cur_ps.weapons, WP_MONSTER_ATTACK1 ) ) { // flaming zombie, run + BG_UpdateConditionValue( cs->entityNum, ANIM_COND_CHARGING, 1, qfalse ); + } + break; + } +} + +/* +============ +AIFunc_Idle() + + The cast AI is standing around, contemplating the meaning of life +============ +*/ +char *AIFunc_Idle( cast_state_t *cs ) { + gentity_t *ent = &g_entities[cs->entityNum]; + + // we are in an idle state, looking for something to do + + // + // do we need to avoid a danger? + if ( cs->dangerEntityValidTime >= level.time ) { + if ( !AICast_GetTakeCoverPos( cs, cs->dangerEntity, cs->dangerEntityPos, cs->takeCoverPos ) ) { + // shit?? + } + // go to a position that cannot be seen from the dangerPos + cs->takeCoverTime = cs->dangerEntityValidTime + 1000; + cs->attackcrouch_time = 0; + return AIFunc_AvoidDangerStart( cs ); + } + // + // are we waiting for a door? + if ( cs->doorMarkerTime > level.time - 100 ) { + return AIFunc_DoorMarkerStart( cs, cs->doorMarkerDoor, cs->doorMarkerNum ); + } + // + // do we need to go to our leader? + if ( cs->leaderNum >= 0 && Distance( cs->bs->origin, g_entities[cs->leaderNum].r.currentOrigin ) > MAX_LEADER_DIST ) { + return AIFunc_ChaseGoalStart( cs, cs->leaderNum, AICAST_LEADERDIST_MAX, qtrue ); + } + // + // look for things we should attack + numEnemies = AICast_ScanForEnemies( cs, enemies ); + if ( numEnemies == -1 ) { // query mode + return NULL; + } else if ( numEnemies == -2 ) { // inspection may be required + char *retval; + // TTimo: gcc: suggest () around assignment used as truth value + if ( ( retval = AIFunc_InspectFriendlyStart( cs, enemies[0] ) ) ) { + return retval; + } + } else if ( numEnemies == -3 ) { // bullet impact + if ( cs->aiState < AISTATE_COMBAT ) { + return AIFunc_InspectBulletImpactStart( cs ); + } + } else if ( numEnemies == -4 ) { // audible event + if ( cs->aiState < AISTATE_COMBAT ) { + return AIFunc_InspectAudibleEventStart( cs, cs->audibleEventEnt ); + } + } else if ( numEnemies > 0 ) { + int i; + + cs->enemyNum = -1; + // choose an enemy + for ( i = 0; i < numEnemies; i++ ) { + if ( Distance( cs->bs->origin, cs->vislist[enemies[i]].visible_pos ) > 16 ) { // if we are really close to the last place we saw them, no point trying to attack, since we'll just end up back here + if ( cs->enemyNum < 0 ) { + cs->enemyNum = enemies[i]; + } else if ( AICast_CheckAttack( cs, enemies[i], qfalse ) ) { + cs->enemyNum = enemies[i]; + return AIFunc_BattleStart( cs ); + } + } + } + if ( cs->enemyNum >= 0 ) { + if ( ( ( cs->leaderNum < 0 ) || ( cs->thinkFuncChangeTime < level.time - 3000 ) ) && AICast_WantsToChase( cs ) ) { // don't leave our leader as soon as we get to them + return AIFunc_BattleStart( cs ); + } else if ( AICast_EntityVisible( AICast_GetCastState( cs->enemyNum ), cs->entityNum, qtrue ) || AICast_CheckAttack( AICast_GetCastState( cs->enemyNum ), cs->entityNum, qfalse ) ) { + // if we are tactical enough, look for a hiding spot + if ( !( cs->leaderNum >= 0 ) && cs->attributes[TACTICAL] > 0.4 && cs->attributes[AGGRESSION] < 1.0 ) { + // they can see us, and we want to hide from them + if ( AICast_GetTakeCoverPos( cs, cs->enemyNum, cs->vislist[cs->enemyNum].visible_pos, cs->takeCoverPos ) ) { + // go to a position that cannot be seen from the last place we saw the enemy, and wait there for some time + cs->takeCoverTime = level.time + 2000 + rand() % 3000; + return AIFunc_BattleTakeCoverStart( cs ); + } + } + // attack them if nothing else to do, since they can attack us here + return AIFunc_BattleStart( cs ); + } else if ( cs->leaderNum < 0 ) { // we should pursue if no leader, and not wanting to hide + return AIFunc_BattleStart( cs ); + } else { + // they can't see us anyway, so ignore them + cs->lastEnemy = cs->enemyNum; // at least face them if they come to get us + cs->enemyNum = -1; + // crouching makes us look like we are hiding, which is what we are doing + if ( cs->attributes[ATTACK_CROUCH] > 0.5 ) { + cs->attackcrouch_time = level.time + 1000; + } + } + } + } + // + // if we are in combat mode, then we should relax, since we dont have an enemy + if ( cs->aiState >= AISTATE_COMBAT ) { + AICast_StateChange( cs, AISTATE_ALERT ); + } + // + // if we couldn't find anything, see if our previous enemy is still around, if so, go find them + // this is an attempt to prevent guys from running away to hide from something, never to + // be seen again. They shouldn't really "forget" that they are indeed soldiers. + if ( !( cs->leaderNum >= 0 ) && cs->lastEnemy >= 0 && g_entities[cs->lastEnemy].health > 0 && cs->vislist[cs->lastEnemy].real_visible_timestamp < level.time - 5000 && + cs->takeCoverTime < level.time - 5000 ) { + cs->enemyNum = cs->lastEnemy; // just go to the place we last saw them + return AIFunc_BattleStart( cs ); + } + // + // if we've recently been in a fight, keep looking around, so we don't look stupid + if ( cs->lastEnemy >= 0 ) { + // we have been in a battle, so face our enemy in anticipation of their return + if ( ent->aiTeam != AITEAM_ALLIES ) { + vec3_t dir; + // + //VectorSubtract( cs->vislist[cs->lastEnemy].visible_pos, cs->bs->origin, dir ); + // hack, use their real angles, prevent them from looking dumb when the player returns + VectorSubtract( g_entities[cs->lastEnemy].s.pos.trBase, cs->bs->origin, dir ); + if ( VectorLength( dir ) < 1 ) { + cs->ideal_viewangles[PITCH] = 0; + } else { + VectorNormalize( dir ); + vectoangles( dir, cs->ideal_viewangles ); + cs->ideal_viewangles[PITCH] = AngleNormalize180( cs->ideal_viewangles[PITCH] ) * 0.5; + } + } else if ( cs->attributes[TACTICAL] && cs->nextIdleAngleChange < level.time ) { + // wait a second before changing again + if ( ( cs->nextIdleAngleChange + 3000 ) < level.time ) { + + // FIXME: This could be changed to use some AAS sampling, which would: + // + // Given a src area, pick a random dest area which is visible from that area + // and return it's position, which we'd then use to set the next view angles + // + // This would result in more efficient, more realistic behaviour, since they'd + // also use PITCH angles to look at areas above/below them + + cs->idleYaw = AICast_GetRandomViewAngle( cs, 512 ); + + if ( abs( AngleDelta( cs->idleYaw, cs->ideal_viewangles[YAW] ) ) < 45 ) { + cs->nextIdleAngleChange = level.time + 1000 + rand() % 2500; + } else { // do really fast + cs->nextIdleAngleChange = level.time + 500; + } + + // adjust with time + cs->idleYawChange = AngleDelta( cs->idleYaw, cs->ideal_viewangles[YAW] ); + /// ((float)(cs->nextIdleAngleChange - level.time) / 1000.0); + + cs->ideal_viewangles[PITCH] = 0; + } + } else if ( cs->idleYawChange ) { + cs->idleYawChange = AngleDelta( cs->idleYaw, cs->ideal_viewangles[YAW] ); + cs->ideal_viewangles[YAW] = AngleMod( cs->ideal_viewangles[YAW] + ( cs->idleYawChange * cs->bs->thinktime ) ); + } + } + + // check for a movement we should be making + if ( cs->obstructingTime > level.time ) { + AICast_MoveToPos( cs, cs->obstructingPos, -1 ); + if ( cs->movestate != MS_CROUCH ) { + cs->movestate = MS_WALK; + } + cs->movestateType = MSTYPE_TEMPORARY; + } + + // set head look flag if no enemy + if ( cs->enemyNum < 0 && cs->attributes[TACTICAL] >= 0.5 && !( cs->aiFlags & AIFL_NO_HEADLOOK ) ) { + g_entities[cs->entityNum].client->ps.eFlags |= EF_HEADLOOK; + } + + // reload? + AICast_IdleReload( cs ); + + return NULL; +} + +/* +============ +AIFunc_IdleStart() +============ +*/ +char *AIFunc_IdleStart( cast_state_t *cs ) { + g_entities[cs->entityNum].flags &= ~FL_AI_GRENADE_KICK; + // stop following + cs->followEntity = -1; + // if our enemy has just died, inspect the body + if ( cs->enemyNum >= 0 ) { + if ( g_entities[cs->entityNum].aiTeam == AITEAM_NAZI && g_entities[cs->enemyNum].health <= 0 ) { + return AIFunc_InspectBodyStart( cs ); + } else { + cs->enemyNum = -1; + } + } + // make sure we don't avoid any areas when we start again + trap_BotInitAvoidReach( cs->bs->ms ); + + // randomly choose idle animation +//----(SA) try always using the 'casual' stand on spawn and change to crouching one when 'alerted' + if ( cs->aiFlags & AIFL_STAND_IDLE2 ) { +// if (rand()%2 || (cs->lastEnemy < 0 && cs->aiFlags & AIFL_TALKING)) + g_entities[cs->entityNum].client->ps.eFlags |= EF_STAND_IDLE2; +// else +// g_entities[cs->entityNum].client->ps.eFlags &= ~EF_STAND_IDLE2; + } + + cs->aifunc = AIFunc_Idle; + return "AIFunc_Idle"; +} + +/* +============ +AIFunc_InspectFriendly() +============ +*/ +char *AIFunc_InspectFriendly( cast_state_t *cs ) { + gentity_t *followent, *ent; + bot_state_t *bs; + vec3_t destorg; + float dist; + qboolean moved = qfalse; + + ent = &g_entities[cs->entityNum]; + + // if we have an enemy, attack now! + if ( cs->enemyNum >= 0 ) { + return AIFunc_BattleStart( cs ); + } + + cs->followEntity = cs->inspectNum; + cs->followDist = 64; + + cs->scriptPauseTime = level.time + 4000; // wait for at least this long before resuming any scripted walking, etc + + // do we need to avoid a danger? + if ( cs->dangerEntityValidTime >= level.time ) { + if ( AICast_GetTakeCoverPos( cs, cs->dangerEntity, cs->dangerEntityPos, cs->takeCoverPos ) ) { + // go to a position that cannot be seen from the dangerPos + cs->takeCoverTime = cs->dangerEntityValidTime + 1000; + cs->attackcrouch_time = 0; + cs->castScriptStatus.scriptGotoId = -1; + cs->movestate = MS_DEFAULT; + cs->movestateType = MSTYPE_NONE; + return AIFunc_AvoidDangerStart( cs ); + } + } + // + // are we waiting for a door? + if ( cs->doorMarkerTime > level.time - 100 ) { + return AIFunc_DoorMarkerStart( cs, cs->doorMarkerDoor, cs->doorMarkerNum ); + } + + followent = &g_entities[cs->followEntity]; + + // if the entity is not ready yet + if ( !followent->inuse ) { + // if it's a connecting client, wait + if ( cs->followEntity < MAX_CLIENTS + && ( ( followent->client && followent->client->pers.connected == CON_CONNECTING ) + || ( level.time < 3000 ) ) ) { + return AIFunc_ChaseGoalIdleStart( cs, cs->followEntity, cs->followDist ); + } else // stop following it + { + AICast_EndChase( cs ); + return AIFunc_IdleStart( cs ); + } + } + + if ( followent->client ) { + VectorCopy( followent->client->ps.origin, destorg ); + } else { + VectorCopy( followent->r.currentOrigin, destorg ); + } + + // they are ready, are they inside range? FIXME: make configurable + dist = Distance( destorg, cs->bs->origin ); + if ( !( dist < cs->followDist && ( ent->waterlevel || ( cs->bs->cur_ps.groundEntityNum != ENTITYNUM_NONE ) ) ) ) { + // + // go to them + // + bs = cs->bs; + + // set this flag so we know when we;ve just reached them + cs->aiFlags |= AIFL_MISCFLAG1; + + // move straight to them if we can + if ( !moved && + ( cs->bs->cur_ps.groundEntityNum != ENTITYNUM_NONE || g_entities[cs->entityNum].waterlevel > 1 ) ) { + aicast_predictmove_t move; + vec3_t dir; + bot_input_t bi; + usercmd_t ucmd; + trace_t tr; + qboolean simTest = qfalse; + + if ( cs->attributes[RUNNING_SPEED] < 120 ) { + simTest = qtrue; + } + + if ( !simTest ) { + // trace will eliminate most unsuccessful paths + trap_Trace( &tr, cs->bs->origin, NULL /*g_entities[cs->entityNum].r.mins*/, NULL /*g_entities[cs->entityNum].r.maxs*/, followent->r.currentOrigin, cs->entityNum, g_entities[cs->entityNum].clipmask ); + if ( tr.entityNum == cs->followEntity || tr.fraction == 1 ) { + simTest = qtrue; + } + } + + if ( simTest ) { + // try walking straight to them + VectorSubtract( followent->r.currentOrigin, cs->bs->origin, dir ); + VectorNormalize( dir ); + if ( !ent->waterlevel ) { + dir[2] = 0; + } + //trap_EA_Move(cs->entityNum, dir, 400); + trap_EA_GetInput( cs->entityNum, (float) level.time / 1000, &bi ); + VectorCopy( dir, bi.dir ); + bi.speed = 400; + bi.actionflags = 0; + AICast_InputToUserCommand( cs, &bi, &ucmd, bs->cur_ps.delta_angles ); + AICast_PredictMovement( cs, 10, 0.8, &move, &ucmd, cs->followEntity ); + + if ( move.stopevent == PREDICTSTOP_HITENT ) { // success! + trap_EA_Move( cs->entityNum, dir, 400 ); // set the movement + vectoangles( dir, cs->ideal_viewangles ); + cs->ideal_viewangles[2] *= 0.5; + moved = qtrue; + } else { // clear movement + //trap_EA_Move(cs->entityNum, dir, 0); + } + } + } + // + if ( !moved ) { + // use AAS routing + moveresult = AICast_MoveToPos( cs, followent->r.currentOrigin, cs->followEntity ); + // if we cant get there, face the path to the enemy + if ( !moveresult || moveresult->failure ) { + // if we can get a visible target, then face it + if ( !( cs->aiFlags & AIFL_MISCFLAG2 ) ) { + if ( trap_AAS_GetRouteFirstVisPos( followent->r.currentOrigin, cs->bs->origin, cs->travelflags, cs->takeCoverEnemyPos ) ) { + cs->aiFlags |= AIFL_MISCFLAG2; + } else { + // if it failed, just use their origin for now, but keep checking + VectorCopy( followent->r.currentOrigin, cs->takeCoverEnemyPos ); + } + } + VectorSubtract( cs->takeCoverEnemyPos, cs->bs->origin, destorg ); + VectorNormalize( destorg ); + vectoangles( destorg, cs->ideal_viewangles ); + } + } + + // should we slow down? + if ( cs->followDist && cs->followSlowApproach ) { + cs->speedScale = AICast_SpeedScaleForDistance( cs, dist, cs->followDist ); + } +/* + // check for a movement we should be making + if (cs->obstructingTime > level.time) + { + AICast_MoveToPos( cs, cs->obstructingPos, -1 ); + if (cs->movestate != MS_CROUCH) { + cs->movestate = MS_WALK; + } + cs->movestateType = MSTYPE_TEMPORARY; + } +*/ + } else if ( cs->aiFlags & AIFL_MISCFLAG1 ) { + cs->aiFlags &= ~AIFL_MISCFLAG1; + if ( g_entities[cs->inspectNum].health <= 0 ) { + // call a script event + cs->aiFlags &= ~AIFL_DENYACTION; + AICast_ForceScriptEvent( cs, "inspectbodyend", g_entities[cs->inspectNum].aiName ); + if ( cs->aiFlags & AIFL_DENYACTION ) { + // relinguish control back to scripting + return AIFunc_DefaultStart( cs ); + } + } else { // force a visibility update so we get their vis also + AICast_UpdateVisibility( ent, &g_entities[cs->inspectNum], qtrue, qtrue ); + } + } + + { + int numEnemies; + // + // look for things we should attack + numEnemies = AICast_ScanForEnemies( cs, enemies ); + if ( numEnemies == -1 ) { // query mode + return NULL; + } else if ( numEnemies == -2 ) { // inspection + // only override current objective if we are inspecting a dead guy, and the new inspect target is fighting someone + if ( ( g_entities[cs->inspectNum].health <= 0 ) && ( g_entities[enemies[0]].health > 0 ) ) { + return AIFunc_InspectFriendlyStart( cs, enemies[0] ); + } + } + // RF, disabled this, if we are interrupted, scripts might not work right, and anyway, this is only a bullet, not as if it's a dead guy or anything + //else if (numEnemies == -3) // bullet impact + //{ + // if (cs->aiState < AISTATE_COMBAT) { + // return AIFunc_InspectBulletImpactStart( cs ); + // } + //} + else if ( numEnemies > 0 ) { + int i; + + cs->enemyNum = enemies[0]; // just attack the first one + // override with a visible enemy + for ( i = 1; i < numEnemies; i++ ) { + if ( AICast_CheckAttack( cs, enemies[i], qfalse ) ) { + cs->enemyNum = enemies[i]; + break; + } else if ( cs->enemyNum < 0 ) { + cs->lastEnemy = enemies[i]; + } + } + + return AIFunc_BattleStart( cs ); + } + } + + if ( cs->nextIdleAngleChange < level.time ) { + // wait a second before changing again + if ( ( cs->nextIdleAngleChange + 3000 ) < level.time ) { + + // FIXME: This could be changed to use some AAS sampling, which would: + // + // Given a src area, pick a random dest area which is visible from that area + // and return it's position, which we'd then use to set the next view angles + // + // This would result in more efficient, more realistic behaviour, since they'd + // also use PITCH angles to look at areas above/below them + + cs->idleYaw = AICast_GetRandomViewAngle( cs, 512 ); + + if ( abs( AngleDelta( cs->idleYaw, cs->ideal_viewangles[YAW] ) ) < 45 ) { + cs->nextIdleAngleChange = level.time + 1000 + rand() % 2500; + } else { // do really fast + cs->nextIdleAngleChange = level.time + 500; + } + + // adjust with time + cs->idleYawChange = AngleDelta( cs->idleYaw, cs->ideal_viewangles[YAW] ); + /// ((float)(cs->nextIdleAngleChange - level.time) / 1000.0); + + cs->ideal_viewangles[PITCH] = 0; + } + } else if ( cs->idleYawChange ) { + cs->idleYawChange = AngleDelta( cs->idleYaw, cs->ideal_viewangles[YAW] ); + cs->ideal_viewangles[YAW] = AngleMod( cs->ideal_viewangles[YAW] + ( cs->idleYawChange * cs->bs->thinktime ) ); + } + + // set head look flag if no enemy + if ( cs->enemyNum < 0 && cs->attributes[TACTICAL] >= 0.5 && !( cs->aiFlags & AIFL_NO_HEADLOOK ) ) { + g_entities[cs->entityNum].client->ps.eFlags |= EF_HEADLOOK; + } + + // reload? + AICast_IdleReload( cs ); + + return NULL; +} + +/* +============ +AIFunc_InspectFriendlyStart +============ +*/ +char *AIFunc_InspectFriendlyStart( cast_state_t *cs, int entnum ) { + cast_state_t *ocs; + + ocs = AICast_GetCastState( entnum ); + + // we are about to deal with the request for inspection + cs->vislist[entnum].flags &= ~AIVIS_INSPECT; + cs->scriptPauseTime = level.time + 4000; // wait for at least this long before resuming any scripted walking, etc + + // + cs->aiFlags &= ~AIFL_MISCFLAG2; + + if ( ocs->aiState >= AISTATE_COMBAT || g_entities[entnum].health <= 0 ) { + // mark this character as having been inspected + cs->vislist[entnum].flags |= AIVIS_INSPECTED; + } + + // what should we do? wait here? hide? go see them? + + // if dead, go see them + if ( g_entities[entnum].health <= 0 ) { + cs->inspectNum = entnum; + cs->aifunc = AIFunc_InspectFriendly; + return "AIFunc_InspectFriendlyStart"; + } + + // not dead, so call scripting event + AICast_ForceScriptEvent( cs, "inspectfriendlycombatstart", g_entities[entnum].aiName ); + if ( cs->aiFlags & AIFL_DENYACTION ) { + // ignore this friendly forever and ever amen + cs->vislist[entnum].flags |= AIVIS_INSPECTED; + return NULL; + } + + // if they are in combat, then act according to aggressiveness + if ( ocs->aiState >= AISTATE_COMBAT ) { + if ( cs->attributes[AGGRESSION] < 0.3 ) { + if ( AICast_GetTakeCoverPos( cs, entnum, g_entities[entnum].client->ps.origin, cs->takeCoverPos ) ) { + cs->takeCoverTime = level.time + 10000; // hide for 10 seconds + cs->scriptPauseTime = cs->takeCoverTime; + // crouch there if possible + if ( cs->attributes[ATTACK_CROUCH] > 0.1 ) { + cs->attackcrouch_time = level.time + 3000; + } + return AIFunc_BattleTakeCoverStart( cs ); + } + } + } + + // if still around, then we need to go to them + cs->inspectNum = entnum; + cs->aifunc = AIFunc_InspectFriendly; + return "AIFunc_InspectFriendly"; +} + +/* +============ +AIFunc_InspectBulletImpact() +============ +*/ +char *AIFunc_InspectBulletImpact( cast_state_t *cs ) { + gentity_t *ent; + vec3_t v1; + gclient_t *client; + // + client = &level.clients[cs->entityNum]; + // + ent = &g_entities[cs->entityNum]; + // + cs->bulletImpactIgnoreTime = level.time + 800; + // + // do we need to avoid a danger? + if ( cs->dangerEntityValidTime >= level.time ) { + if ( AICast_GetTakeCoverPos( cs, cs->dangerEntity, cs->dangerEntityPos, cs->takeCoverPos ) ) { + // go to a position that cannot be seen from the dangerPos + cs->takeCoverTime = cs->dangerEntityValidTime + 1000; + cs->attackcrouch_time = 0; + cs->castScriptStatus.scriptGotoId = -1; + cs->movestate = MS_DEFAULT; + cs->movestateType = MSTYPE_NONE; + return AIFunc_AvoidDangerStart( cs ); + } + } + // wait until we are looking at the impact + if ( cs->aiFlags & AIFL_MISCFLAG2 ) { + // pause any scripting + cs->scriptPauseTime = level.time + 1000; + // look at bullet impact + VectorSubtract( cs->bulletImpactEnd, cs->bs->origin, v1 ); + VectorNormalize( v1 ); + vectoangles( v1, cs->ideal_viewangles ); + // + // if we are facing that direction, we've looked at the impact point + if ( fabs( cs->ideal_viewangles[YAW] - cs->viewangles[YAW] ) < 1 ) { + cs->aiFlags &= ~AIFL_MISCFLAG2; + } + return NULL; + } else if ( cs->aiFlags & AIFL_MISCFLAG1 ) { + // clear the flag now + cs->aiFlags &= ~AIFL_MISCFLAG1; + // start looking back at bullet + VectorSubtract( cs->bulletImpactStart, cs->bs->origin, v1 ); + VectorNormalize( v1 ); + vectoangles( v1, cs->ideal_viewangles ); + if ( cs->aiState < AISTATE_ALERT ) { + // change to alert state + if ( !AICast_StateChange( cs, AISTATE_ALERT ) ) { + if ( cs->lastEnemy < 0 && cs->enemyNum < 0 ) { + // look back at our original angles + VectorCopy( ent->s.angles, cs->ideal_viewangles ); + } + // stop doing whatever we are doing, and return control to scripting + cs->scriptPauseTime = 0; + return AIFunc_IdleStart( cs ); + } + // make sure we didnt change thinkfunc + if ( cs->aifunc != AIFunc_InspectBulletImpact ) { + //G_Error( "scripting passed control out of AIFunc_InspectBulletImpact(), this is bad" ); + return NULL; + } + } + // pause any scripting + if ( ent->client->ps.legsTimer ) { + cs->scriptPauseTime = level.time + ent->client->ps.legsTimer; + } else { // just wait for a few seconds looking at the source + cs->scriptPauseTime = level.time + 3500; + } + } + // are we done? + if ( cs->scriptPauseTime < level.time ) { + if ( cs->lastEnemy < 0 && cs->enemyNum < 0 ) { + // look back at our original angles + VectorCopy( ent->s.angles, cs->ideal_viewangles ); + } + return AIFunc_IdleStart( cs ); + } + // + // reload? + AICast_IdleReload( cs ); + // + // check for enemies + { + int numEnemies; + // + // look for things we should attack + numEnemies = AICast_ScanForEnemies( cs, enemies ); + if ( numEnemies == -2 ) { // inspection + // only override current objective if we are inspecting a dead guy, and the new inspect target is fighting someone + if ( ( g_entities[cs->inspectNum].health <= 0 ) && ( g_entities[enemies[0]].health > 0 ) ) { + + return AIFunc_InspectFriendlyStart( cs, enemies[0] ); + } + } else if ( numEnemies > 0 ) { + int i; + + cs->enemyNum = enemies[0]; // just attack the first one + // override with a visible enemy + for ( i = 1; i < numEnemies; i++ ) { + if ( AICast_CheckAttack( cs, enemies[i], qfalse ) ) { + cs->enemyNum = enemies[i]; + break; + } else if ( cs->enemyNum < 0 ) { + cs->lastEnemy = enemies[i]; + } + } + + return AIFunc_BattleStart( cs ); + } + } + // + return NULL; +} + +/* +============ +AIFunc_InspectBulletImpactStart() +============ +*/ +char *AIFunc_InspectBulletImpactStart( cast_state_t *cs ) { + int oldScriptIndex; + // set the impact timer so we ignore bullets while inspecting this one + cs->bulletImpactIgnoreTime = level.time + 5000; + // pause any scripting + cs->scriptPauseTime = level.time + 1000; + // set this so we know if we've started the trace back to the bullet origin + cs->aiFlags |= AIFL_MISCFLAG1; + cs->aiFlags |= AIFL_MISCFLAG2; + // + // call the script event + oldScriptIndex = cs->scriptCallIndex; + AICast_ScriptEvent( cs, "bulletimpactsound", "" ); + if ( oldScriptIndex == cs->scriptCallIndex ) { + // no script event, so call the animation script + BG_AnimScriptEvent( &g_entities[cs->entityNum].client->ps, ANIM_ET_BULLETIMPACT, qfalse, qtrue ); + } + // + // if the origin is not visible, set the bullet origin to the closest visible area from the src + if ( !trap_InPVS( cs->bulletImpactStart, cs->bs->origin ) ) { + // if it fails, then just look at the source + trap_AAS_GetRouteFirstVisPos( g_entities[cs->bulletImpactEntity].s.pos.trBase, cs->bs->origin, cs->travelflags, cs->bulletImpactStart ); + } + // + cs->aifunc = AIFunc_InspectBulletImpact; + return "AIFunc_InspectBulletImpact"; +} + +/* +============ +AIFunc_InspectAudibleEvent() +============ +*/ +char *AIFunc_InspectAudibleEvent( cast_state_t *cs ) { + gentity_t *ent; + bot_state_t *bs; + vec3_t destorg, vec; + float dist; + qboolean moved = qfalse; + + ent = &g_entities[cs->entityNum]; + + // if we have an enemy, attack now! + if ( cs->enemyNum >= 0 ) { + return AIFunc_BattleStart( cs ); + } + + cs->followDist = 64; + + // do we need to avoid a danger? + if ( cs->dangerEntityValidTime >= level.time ) { + if ( AICast_GetTakeCoverPos( cs, cs->dangerEntity, cs->dangerEntityPos, cs->takeCoverPos ) ) { + // go to a position that cannot be seen from the dangerPos + cs->takeCoverTime = cs->dangerEntityValidTime + 1000; + cs->attackcrouch_time = 0; + cs->castScriptStatus.scriptGotoId = -1; + cs->movestate = MS_DEFAULT; + cs->movestateType = MSTYPE_NONE; + return AIFunc_AvoidDangerStart( cs ); + } + } + // + // are we waiting for a door? + if ( cs->doorMarkerTime > level.time - 100 ) { + return AIFunc_DoorMarkerStart( cs, cs->doorMarkerDoor, cs->doorMarkerNum ); + } + + // are we just looking for now? + if ( cs->aiFlags & AIFL_MISCFLAG2 ) { + if ( cs->scriptPauseTime <= level.time ) { + return AIFunc_DefaultStart( cs ); + } + return NULL; + } + + VectorCopy( cs->audibleEventOrg, destorg ); + + // they are ready, are they inside range? FIXME: make configurable + dist = Distance( destorg, cs->bs->origin ); + if ( !( dist < cs->followDist && ( ent->waterlevel || ( cs->bs->cur_ps.groundEntityNum != ENTITYNUM_NONE ) ) ) ) { + // + // go to them + // + bs = cs->bs; + + // set this flag so we know when we;ve just reached them + cs->aiFlags |= AIFL_MISCFLAG1; + + // if not overly aggressive, pursue with caution + if ( cs->attributes[AGGRESSION] <= 0.8 ) { + cs->movestate = MS_CROUCH; + cs->movestateType = MSTYPE_TEMPORARY; + } + + // move straight to them if we can + if ( !moved && + ( cs->bs->cur_ps.groundEntityNum != ENTITYNUM_NONE || g_entities[cs->entityNum].waterlevel > 1 ) ) { + aicast_predictmove_t move; + vec3_t dir; + bot_input_t bi; + usercmd_t ucmd; + trace_t tr; + qboolean simTest = qfalse; + + if ( cs->attributes[RUNNING_SPEED] < 120 ) { + simTest = qtrue; + } + + if ( !simTest ) { + // trace will eliminate most unsuccessful paths + trap_Trace( &tr, cs->bs->origin, NULL /*g_entities[cs->entityNum].r.mins*/, NULL /*g_entities[cs->entityNum].r.maxs*/, destorg, cs->entityNum, g_entities[cs->entityNum].clipmask ); + if ( tr.fraction == 1 ) { + simTest = qtrue; + } + } + + if ( simTest ) { + // try walking straight to them + gentity_t *gent; + // + gent = G_Spawn(); + VectorCopy( destorg, gent->r.currentOrigin ); + // + VectorSubtract( destorg, cs->bs->origin, dir ); + VectorNormalize( dir ); + if ( !ent->waterlevel ) { + dir[2] = 0; + } + //trap_EA_Move(cs->entityNum, dir, 400); + trap_EA_GetInput( cs->entityNum, (float) level.time / 1000, &bi ); + VectorCopy( dir, bi.dir ); + bi.speed = 400; + bi.actionflags = 0; + AICast_InputToUserCommand( cs, &bi, &ucmd, bs->cur_ps.delta_angles ); + AICast_PredictMovement( cs, 10, 0.8, &move, &ucmd, gent->s.number ); + // + if ( move.stopevent == PREDICTSTOP_HITENT ) { // success! + trap_EA_Move( cs->entityNum, dir, 400 ); + vectoangles( dir, cs->ideal_viewangles ); + cs->ideal_viewangles[2] *= 0.5; + moved = qtrue; + } else { // clear movement + //trap_EA_Move(cs->entityNum, dir, 0); + } + // + G_FreeEntity( gent ); + } + } + // + if ( !moved ) { + // use AAS routing + moveresult = AICast_MoveToPos( cs, destorg, -1 ); + // if we cant get there, do something else + if ( moveresult && moveresult->failure ) { + + // if we can get a visible target, then face it + if ( trap_AAS_GetRouteFirstVisPos( cs->audibleEventOrg, cs->bs->origin, cs->travelflags, destorg ) ) { + cs->aiFlags |= AIFL_MISCFLAG2; + VectorSubtract( destorg, cs->bs->origin, destorg ); + VectorNormalize( destorg ); + vectoangles( destorg, cs->ideal_viewangles ); + return NULL; + } + + if ( cs->lastEnemy < 0 && cs->enemyNum < 0 ) { + // look back at our original angles + VectorCopy( ent->s.angles, cs->ideal_viewangles ); + } + return AIFunc_DefaultStart( cs ); + } else if ( !moveresult ) { // face it? + if ( trap_InPVS( destorg, cs->bs->origin ) ) { + VectorSubtract( destorg, cs->bs->origin, vec ); + VectorNormalize( vec ); + vectoangles( vec, cs->ideal_viewangles ); + } + } + } + + // should we slow down? + if ( cs->followDist && cs->followSlowApproach ) { + cs->speedScale = AICast_SpeedScaleForDistance( cs, dist, cs->followDist ); + } +/* + // check for a movement we should be making + if (cs->obstructingTime > level.time) + { + AICast_MoveToPos( cs, cs->obstructingPos, -1 ); + if (cs->movestate != MS_CROUCH) { + cs->movestate = MS_WALK; + } + cs->movestateType = MSTYPE_TEMPORARY; + } +*/ + } else if ( cs->aiFlags & AIFL_MISCFLAG1 ) { + cs->aiFlags &= ~AIFL_MISCFLAG1; + // call a script event + cs->aiFlags &= ~AIFL_DENYACTION; + AICast_ForceScriptEvent( cs, "inspectsoundend", g_entities[cs->audibleEventEnt].aiName ); + if ( cs->aiFlags & AIFL_DENYACTION ) { + // relinguish control back to scripting + return AIFunc_DefaultStart( cs ); + } + } else { + // look around randomly + if ( cs->battleHuntViewTime < level.time ) { + cs->battleHuntViewTime = level.time + 700 + rand() % 1000; + // set a random viewangle + cs->ideal_viewangles[YAW] = AngleMod( cs->ideal_viewangles[YAW] + ( 45.0 + random() * 45.0 ) * ( 2 * ( rand() % 2 ) - 1 ) ); + } + // + if ( cs->scriptPauseTime < level.time ) { + // we're done waiting around here + if ( cs->lastEnemy < 0 && cs->enemyNum < 0 ) { + // look back at our original angles + VectorCopy( ent->s.angles, cs->ideal_viewangles ); + } + return AIFunc_DefaultStart( cs ); + } + } + + { + int numEnemies; + // + // look for things we should attack + numEnemies = AICast_ScanForEnemies( cs, enemies ); + if ( numEnemies == -1 ) { // query mode + return NULL; + } else if ( numEnemies == -2 ) { // inspection + // only override current objective if we are inspecting a dead guy, and the new inspect target is fighting someone + if ( ( g_entities[cs->inspectNum].health <= 0 ) && ( g_entities[enemies[0]].health > 0 ) ) { + return AIFunc_InspectFriendlyStart( cs, enemies[0] ); + } + } else if ( numEnemies == -4 ) { // NEW audible event + //if (cs->aiState < AISTATE_COMBAT) { + return AIFunc_InspectAudibleEventStart( cs, cs->audibleEventEnt ); + //} + } else if ( numEnemies > 0 ) { + int i; + + cs->enemyNum = enemies[0]; // just attack the first one + // override with a visible enemy + for ( i = 1; i < numEnemies; i++ ) { + if ( AICast_CheckAttack( cs, enemies[i], qfalse ) ) { + cs->enemyNum = enemies[i]; + break; + } else if ( cs->enemyNum < 0 ) { + cs->lastEnemy = enemies[i]; + } + } + + return AIFunc_BattleStart( cs ); + } + } + + // set head look flag if no enemy + if ( cs->enemyNum < 0 && cs->attributes[TACTICAL] >= 0.5 && !( cs->aiFlags & AIFL_NO_HEADLOOK ) ) { + g_entities[cs->entityNum].client->ps.eFlags |= EF_HEADLOOK; + } + + // reload? + AICast_IdleReload( cs ); + + return NULL; +} + +/* +============ +AIFunc_InspectAudibleEventStart +============ +*/ +char *AIFunc_InspectAudibleEventStart( cast_state_t *cs, int entnum ) { + cast_state_t *ocs; + int oldScriptIndex; + + ocs = AICast_GetCastState( entnum ); + + // we have now processed the audible event (whether we act on it or not) + cs->audibleEventTime = -9999; + + // trigger a script event, which has the ability to deny the request + oldScriptIndex = cs->scriptCallIndex; + AICast_ForceScriptEvent( cs, "inspectsoundstart", g_entities[cs->audibleEventEnt].aiName ); + if ( cs->aiFlags & AIFL_DENYACTION ) { + return NULL; + } + + // if not in alert mode, go there now + if ( cs->aiState < AISTATE_ALERT ) { + AICast_StateChange( cs, AISTATE_ALERT ); + } + + if ( oldScriptIndex == cs->scriptCallIndex ) { + BG_AnimScriptEvent( &g_entities[cs->entityNum].client->ps, ANIM_ET_INSPECTSOUND, qfalse, qtrue ); + } + + // pause the scripting for now + cs->scriptPauseTime = level.time + 4000; // wait for at least this long before resuming any scripted walking, etc + + // set this when we decide to just look, rather than pursue + cs->aiFlags &= ~AIFL_MISCFLAG2; + + // what should we do? wait here? hide? go see them? + + // if dead, go see them + if ( g_entities[entnum].health <= 0 ) { + cs->inspectNum = entnum; + cs->aifunc = AIFunc_InspectFriendly; + return "AIFunc_InspectFriendlyStart"; + } + + // if they are in combat, then act according to aggressiveness + if ( ocs->aiState >= AISTATE_COMBAT ) { + if ( cs->attributes[AGGRESSION] < 0.3 ) { + if ( AICast_GetTakeCoverPos( cs, entnum, g_entities[entnum].client->ps.origin, cs->takeCoverPos ) ) { + cs->takeCoverTime = level.time + 10000; // hide for 10 seconds + cs->scriptPauseTime = cs->takeCoverTime; + // crouch there if possible + if ( cs->attributes[ATTACK_CROUCH] > 0.1 ) { + cs->attackcrouch_time = level.time + 3000; + } + return AIFunc_BattleTakeCoverStart( cs ); + } + } + } + + cs->aifunc = AIFunc_InspectAudibleEvent; + return "AIFunc_InspectAudibleEvent"; +} + +/* +============ +AIFunc_ChaseGoalIdle() +============ +*/ +char *AIFunc_ChaseGoalIdle( cast_state_t *cs ) { + gentity_t *followent; + vec3_t dir; + + if ( cs->followEntity < 0 ) { + AICast_EndChase( cs ); + return AIFunc_IdleStart( cs ); + } + + followent = &g_entities[cs->followEntity]; + + // CHECK: will this interfere with scripting? + // + // do we need to avoid a danger? + if ( cs->dangerEntityValidTime >= level.time ) { + if ( AICast_GetTakeCoverPos( cs, cs->dangerEntity, cs->dangerEntityPos, cs->takeCoverPos ) ) { + // go to a position that cannot be seen from the dangerPos + cs->takeCoverTime = cs->dangerEntityValidTime + 1000; + cs->attackcrouch_time = 0; + return AIFunc_AvoidDangerStart( cs ); + } + } + // + // are we waiting for a door? + if ( cs->doorMarkerTime > level.time - 100 ) { + return AIFunc_DoorMarkerStart( cs, cs->doorMarkerDoor, cs->doorMarkerNum ); + } + // + // if the player is not ready yet, wait + if ( !followent->inuse ) { + return NULL; + } + + // has the scripting stopped asking us to pursue this goal? + if ( cs->followIsGoto && ( cs->followTime < level.time ) ) { + return AIFunc_Idle( cs ); + } + + // they are ready, are they outside range? + if ( Distance( followent->r.currentOrigin, cs->bs->origin ) > cs->followDist ) { + return AIFunc_ChaseGoalStart( cs, cs->followEntity, cs->followDist, qtrue ); + } + + // check for a movement we should be making + if ( cs->obstructingTime > level.time ) { + AICast_MoveToPos( cs, cs->obstructingPos, -1 ); + cs->speedScale = cs->attributes[WALKING_SPEED] / cs->attributes[RUNNING_SPEED]; + } + // if we have an enemy, fire if they're visible + else if ( cs->enemyNum >= 0 ) { + //attack the enemy if possible + AICast_ProcessAttack( cs ); + } + // if we had an enemy recently, face them + else if ( cs->lastEnemy >= 0 ) { + vec3_t dir; + // + VectorSubtract( cs->vislist[cs->lastEnemy].visible_pos, cs->bs->origin, dir ); + if ( VectorLength( dir ) < 1 ) { + cs->ideal_viewangles[PITCH] = 0; + } else { + VectorNormalize( dir ); + vectoangles( dir, cs->ideal_viewangles ); + } + // reload? + AICast_IdleReload( cs ); + } else if ( followent->client ) { + // face them + VectorSubtract( followent->r.currentOrigin, cs->bs->origin, dir ); + dir[2] += followent->client->ps.viewheight - g_entities[cs->bs->entitynum].client->ps.viewheight; + VectorNormalize( dir ); + vectoangles( dir, cs->ideal_viewangles ); + } + + // look for things we should attack + numEnemies = AICast_ScanForEnemies( cs, enemies ); + if ( numEnemies == -1 ) { // query mode + return NULL; + } else if ( numEnemies == -2 ) { // inspection may be required + char *retval; + // TTimo: gcc: suggest () around assignment used as truth value + if ( ( retval = AIFunc_InspectFriendlyStart( cs, enemies[0] ) ) ) { + return retval; + } + } else if ( numEnemies == -3 ) { // bullet impact + if ( cs->aiState < AISTATE_COMBAT ) { + return AIFunc_InspectBulletImpactStart( cs ); + } + } else if ( numEnemies == -4 ) { // audible event + if ( cs->aiState < AISTATE_COMBAT ) { + return AIFunc_InspectAudibleEventStart( cs, cs->audibleEventEnt ); + } + } else if ( numEnemies > 0 ) { + cs->enemyNum = enemies[0]; // just attack the first one + } + + // set head look flag if no enemy + if ( cs->enemyNum < 0 && cs->attributes[TACTICAL] >= 0.5 && !( cs->aiFlags & AIFL_NO_HEADLOOK ) ) { + g_entities[cs->entityNum].client->ps.eFlags |= EF_HEADLOOK; + } + + return NULL; +} + +/* +============ +AIFunc_ChaseGoalIdleStart() +============ +*/ +char *AIFunc_ChaseGoalIdleStart( cast_state_t *cs, int entitynum, float reachdist ) { + // make sure we don't avoid any areas when we start again + trap_BotInitAvoidReach( cs->bs->ms ); + + // if we are following someone, always use the default (ready for action) anim + if ( entitynum < MAX_CLIENTS ) { + g_entities[cs->entityNum].client->ps.eFlags &= ~EF_STAND_IDLE2; + } else { + // randomly choose idle animation +//----(SA) try always using the 'casual' stand on spawn and change to crouching one when 'alerted' + if ( cs->aiFlags & AIFL_STAND_IDLE2 ) { +// if (cs->lastEnemy < 0) + g_entities[cs->entityNum].client->ps.eFlags |= EF_STAND_IDLE2; +// else +// g_entities[cs->entityNum].client->ps.eFlags &= ~EF_STAND_IDLE2; + } + } + + cs->followEntity = entitynum; + cs->followDist = reachdist; + cs->aifunc = AIFunc_ChaseGoalIdle; + return "AIFunc_ChaseGoalIdle"; +} + +/* +============ +AIFunc_ChaseGoal() +============ +*/ +char *AIFunc_ChaseGoal( cast_state_t *cs ) { + gentity_t *followent, *ent; + bot_state_t *bs; + vec3_t destorg; + float dist; + qboolean moved = qfalse; + + ent = &g_entities[cs->entityNum]; + + if ( cs->followEntity < 0 ) { + AICast_EndChase( cs ); + return AIFunc_IdleStart( cs ); + } + + // CHECK: will this mess with scripting? + // + // do we need to avoid a danger? + if ( cs->dangerEntityValidTime >= level.time ) { + if ( AICast_GetTakeCoverPos( cs, cs->dangerEntity, cs->dangerEntityPos, cs->takeCoverPos ) ) { + // go to a position that cannot be seen from the dangerPos + cs->takeCoverTime = cs->dangerEntityValidTime + 1000; + cs->attackcrouch_time = 0; + cs->castScriptStatus.scriptGotoId = -1; + cs->movestate = MS_DEFAULT; + cs->movestateType = MSTYPE_NONE; + return AIFunc_AvoidDangerStart( cs ); + } + } + // + // are we waiting for a door? + if ( cs->doorMarkerTime > level.time - 100 ) { + return AIFunc_DoorMarkerStart( cs, cs->doorMarkerDoor, cs->doorMarkerNum ); + } + + followent = &g_entities[cs->followEntity]; + + // if the entity is not ready yet + if ( !followent->inuse ) { + // if it's a connecting client, wait + if ( cs->followEntity < MAX_CLIENTS + && ( ( followent->client && followent->client->pers.connected == CON_CONNECTING ) + || ( level.time < 3000 ) ) ) { + return AIFunc_ChaseGoalIdleStart( cs, cs->followEntity, cs->followDist ); + } else // stop following it + { + AICast_EndChase( cs ); + return AIFunc_IdleStart( cs ); + } + } + + // has the scripting stopped asking us to pursue this goal? + if ( cs->followIsGoto && ( cs->followTime < level.time ) ) { + return AIFunc_IdleStart( cs ); + } + + if ( followent->client ) { + VectorCopy( followent->client->ps.origin, destorg ); + } else { + VectorCopy( followent->r.currentOrigin, destorg ); + } + + // they are ready, are they inside range? FIXME: make configurable + dist = Distance( destorg, cs->bs->origin ); + if ( cs->followSlowApproach && dist < cs->followDist && ( ent->waterlevel || ( cs->bs->cur_ps.groundEntityNum != ENTITYNUM_NONE ) ) ) { + // if this is a scripted GOTO, stop following now + if ( cs->followEntity == cs->castScriptStatus.scriptGotoEnt ) { + AICast_EndChase( cs ); + return AIFunc_IdleStart( cs ); + } + // if we have reached our leader + else + { + if ( cs->followEntity == cs->leaderNum ) { + if ( dist < AICAST_LEADERDIST_MIN ) { + AICast_EndChase( cs ); + return AIFunc_IdleStart( cs ); + } else { + trace_t tr; + // if we have a clear line to our leader, move closer, since there may be others following also + trap_Trace( &tr, cs->bs->origin, cs->bs->cur_ps.mins, cs->bs->cur_ps.maxs, g_entities[cs->followEntity].r.currentOrigin, cs->entityNum, g_entities[cs->entityNum].clipmask ); + if ( tr.entityNum != cs->followEntity ) { + AICast_EndChase( cs ); + return AIFunc_IdleStart( cs ); + } + // if we have crouching ability, then use it while we are just moving closer + if ( cs->attributes[ATTACK_CROUCH] > 0.1 ) { + cs->attackcrouch_time = level.time + 1000; + } + } + } else + { + return AIFunc_ChaseGoalIdleStart( cs, cs->followEntity, cs->followDist ); + } + } + } + // + // go to them + // + bs = cs->bs; + // + // RF, disabled this, MIKE sees dead people + //if (followent->client && followent->health <= 0) { + // AICast_EndChase( cs ); + // return AIFunc_IdleStart(cs); + //} + + // move straight to them if we can + if ( !moved && + ( cs->bs->cur_ps.groundEntityNum != ENTITYNUM_NONE || g_entities[cs->entityNum].waterlevel > 1 ) ) { + aicast_predictmove_t move; + vec3_t dir; + bot_input_t bi; + usercmd_t ucmd; + trace_t tr; + qboolean simTest = qfalse; + float frameTime = 0.8, goaldist; + + if ( cs->attributes[RUNNING_SPEED] < 120 ) { + simTest = qtrue; + } + + if ( !simTest ) { + // trace will eliminate most unsuccessful paths + trap_Trace( &tr, cs->bs->origin, NULL /*g_entities[cs->entityNum].r.mins*/, NULL /*g_entities[cs->entityNum].r.maxs*/, followent->r.currentOrigin, cs->entityNum, g_entities[cs->entityNum].clipmask ); + if ( tr.entityNum == cs->followEntity || tr.fraction == 1 ) { + simTest = qtrue; + } + } + + if ( simTest ) { + // try walking straight to them + VectorSubtract( followent->r.currentOrigin, cs->bs->origin, dir ); + if ( !ent->waterlevel ) { + dir[2] = 0; + } + goaldist = VectorNormalize( dir ); + //trap_EA_Move(cs->entityNum, dir, 400); + trap_EA_GetInput( cs->entityNum, (float) level.time / 1000, &bi ); + VectorCopy( dir, bi.dir ); + bi.speed = 400; + bi.actionflags = 0; + AICast_InputToUserCommand( cs, &bi, &ucmd, bs->cur_ps.delta_angles ); + AICast_PredictMovement( cs, 10, frameTime, &move, &ucmd, cs->followEntity ); + + if ( move.stopevent == PREDICTSTOP_HITENT ) { // success! + // make sure we didnt spend a lot of time sliding along an obstacle + if ( ( move.frames * frameTime ) < ( 1.0 + ( goaldist / ( bs->cur_ps.speed * bs->cur_ps.runSpeedScale ) ) ) ) { + trap_EA_Move( cs->entityNum, dir, 400 ); + vectoangles( dir, cs->ideal_viewangles ); + cs->ideal_viewangles[2] *= 0.5; + moved = qtrue; + } + } + if ( !moved ) { + //trap_EA_Move(cs->entityNum, dir, 0); + } + } + } + // + if ( !moved ) { + // use AAS routing + moveresult = AICast_MoveToPos( cs, followent->r.currentOrigin, cs->followEntity ); + if ( moveresult && moveresult->failure ) { + // shit? + } + } + + // should we slow down? + if ( cs->followDist && cs->followSlowApproach && cs->followDist < 48 ) { + cs->speedScale = AICast_SpeedScaleForDistance( cs, dist, cs->followDist ); + } + + // check for a movement we should be making + if ( cs->obstructingTime > level.time ) { + AICast_MoveToPos( cs, cs->obstructingPos, -1 ); + if ( cs->movestate != MS_CROUCH ) { + cs->movestate = MS_WALK; + } + cs->movestateType = MSTYPE_TEMPORARY; + } + + // if we have an enemy, fire if they're visible + if ( cs->enemyNum >= 0 ) { //attack the enemy if possible + AICast_ProcessAttack( cs ); + } else { + int numEnemies; + // + // look for things we should attack + numEnemies = AICast_ScanForEnemies( cs, enemies ); + if ( numEnemies == -1 ) { // query mode + return NULL; + } else if ( numEnemies == -2 ) { // inspection may be required + char *retval; + // TTimo: gcc: suggest () around assignment used as truth value + if ( ( retval = AIFunc_InspectFriendlyStart( cs, enemies[0] ) ) ) { + return retval; + } + } else if ( numEnemies == -3 ) { // bullet impact + if ( cs->aiState < AISTATE_COMBAT ) { + return AIFunc_InspectBulletImpactStart( cs ); + } + } else if ( numEnemies == -4 ) { // audible event + if ( cs->aiState < AISTATE_COMBAT ) { + return AIFunc_InspectAudibleEventStart( cs, cs->audibleEventEnt ); + } + } else if ( numEnemies > 0 ) { + int i; + + cs->enemyNum = enemies[0]; // just attack the first one + // override with a visible enemy + for ( i = 1; i < numEnemies; i++ ) { + if ( AICast_CheckAttack( cs, enemies[i], qfalse ) ) { + cs->enemyNum = enemies[i]; + break; + } else if ( cs->enemyNum < 0 ) { + cs->lastEnemy = enemies[i]; + } + } + } + // reload? + AICast_IdleReload( cs ); + } + + // set head look flag if no enemy + if ( cs->enemyNum < 0 && cs->attributes[TACTICAL] >= 0.5 && !( cs->aiFlags & AIFL_NO_HEADLOOK ) ) { + g_entities[cs->entityNum].client->ps.eFlags |= EF_HEADLOOK; + } + + return NULL; + +} + +/* +============ +AIFunc_ChaseGoalStart() +============ +*/ +char *AIFunc_ChaseGoalStart( cast_state_t *cs, int entitynum, float reachdist, qboolean slowApproach ) { + cs->followEntity = entitynum; + cs->followDist = reachdist; + cs->followIsGoto = qfalse; + cs->followSlowApproach = slowApproach; + cs->aifunc = AIFunc_ChaseGoal; + return "AIFunc_ChaseGoal"; +} + +/* +============ +AIFunc_DoorMarker() +============ +*/ +char *AIFunc_DoorMarker( cast_state_t *cs ) { + gentity_t *followent, *door; + bot_state_t *bs; + vec3_t destorg; + float dist; + // + // do we need to avoid a danger? + if ( cs->dangerEntityValidTime >= level.time ) { + if ( !AICast_GetTakeCoverPos( cs, cs->dangerEntity, cs->dangerEntityPos, cs->takeCoverPos ) ) { + // shit?? + } + // go to a position that cannot be seen from the dangerPos + cs->takeCoverTime = cs->dangerEntityValidTime + 1000; + cs->attackcrouch_time = 0; + return AIFunc_AvoidDangerStart( cs ); + } + + followent = &g_entities[cs->doorMarker]; + + // if the entity is not ready yet + if ( !followent->inuse ) { + cs->doorMarkerTime = 0; + //return AIFunc_DefaultStart( cs ); + return AIFunc_Restore( cs ); + } + + // if the door is open or idle + door = &g_entities[cs->doorEntNum]; + if ( ( !door->key ) && + ( door->s.apos.trType == TR_STATIONARY && door->s.pos.trType == TR_STATIONARY ) ) { + cs->doorMarkerTime = 0; + //return AIFunc_DefaultStart( cs ); + return AIFunc_Restore( cs ); + } + + // if we have an enemy, fire if they're visible + if ( cs->enemyNum >= 0 ) { //attack the enemy if possible + AICast_ProcessAttack( cs ); + } + + // they are ready, are they inside range? FIXME: make configurable + dist = Distance( destorg, cs->bs->origin ); + if ( dist < 12 ) { + // check for a movement we should be making + if ( cs->obstructingTime > level.time ) { + AICast_MoveToPos( cs, cs->obstructingPos, -1 ); + } + // if the door is locked, resume + if ( followent->key ) { + return AIFunc_Restore( cs ); + } + return NULL; + } + + // go to it + // + bs = cs->bs; + // + moveresult = AICast_MoveToPos( cs, followent->r.currentOrigin, followent->s.number ); + // if we cant get there, forget it + if ( moveresult && moveresult->failure ) { + return AIFunc_Restore( cs ); + } + // should we slow down? + if ( cs->followDist ) { + cs->speedScale = AICast_SpeedScaleForDistance( cs, dist, cs->followDist ); + } + // reload? + AICast_IdleReload( cs ); + return NULL; + +} + +/* +============ +AIFunc_DoorMarkerStart() +============ +*/ +char *AIFunc_DoorMarkerStart( cast_state_t *cs, int doornum, int markernum ) { + cs->doorEntNum = doornum; + cs->doorMarker = markernum; + cs->oldAifunc = cs->aifunc; + cs->aifunc = AIFunc_DoorMarker; + return "AIFunc_DoorMarker"; +} + +/* +============= +AIFunc_BattleRoll() +============= +*/ +char *AIFunc_BattleRoll( cast_state_t *cs ) { + gclient_t *client = &level.clients[cs->entityNum]; + vec3_t dir; + // + // record the time + cs->lastRollMove = level.time; + client->ps.eFlags |= EF_NOSWINGANGLES; + // + if ( !client->ps.torsoTimer ) { + if ( cs->battleRollTime < level.time ) { + return AIFunc_Restore( cs ); + } else { + // attack? + if ( cs->enemyNum >= 0 ) { + AICast_ProcessAttack( cs ); + } + } + } + if ( g_entities[cs->entityNum].health <= 0 ) { + return AIFunc_DefaultStart( cs ); + } + // + trap_EA_Crouch( cs->entityNum ); + cs->attackcrouch_time = level.time + 500; + // all characters so far only move during the first second of animation + if ( cs->thinkFuncChangeTime > level.time - 1000 ) { + // just move in the direction of our ideal_viewangles + AngleVectors( cs->ideal_viewangles, dir, NULL, NULL ); + trap_EA_Move( cs->entityNum, dir, 300 ); + // we are crouching, move a little faster than normal + cs->speedScale = 1.5; + } else if ( cs->takeCoverTime > level.time ) { + // + // if we are taking Cover, use this position, if it's bad, we'll just look for a better spot once we're done here + VectorCopy( cs->bs->origin, cs->takeCoverPos ); + } else if ( cs->enemyNum >= 0 ) { + // + // start turning towards our enemy + AICast_ProcessAttack( cs ); + } + // + return NULL; +} + +/* +============= +AIFunc_BattleRollStart() +============= +*/ +char *AIFunc_BattleRollStart( cast_state_t *cs, vec3_t vec ) { + int duration; +// gclient_t *client = &level.clients[cs->entityNum]; + // + // backup the current thinkfunc, so we can return to it when done + cs->oldAifunc = cs->aifunc; + // + // face the direction of movement + vectoangles( vec, cs->ideal_viewangles ); + // do the roll + duration = BG_AnimScriptEvent( &g_entities[cs->entityNum].client->ps, ANIM_ET_ROLL, qfalse, qtrue ); + // + if ( duration < 0 ) { // it failed + return NULL; + } + // add some duration to make sure it fully plays out + duration += 100; + g_entities[cs->entityNum].client->ps.legsTimer = duration; + g_entities[cs->entityNum].client->ps.torsoTimer = duration; + // + cs->noAttackTime = level.time + duration - 200; + // set the duration + cs->battleRollTime = level.time + duration; + // move into crouch position + //cs->attackcrouch_time = level.time + (duration) + 1000; + // record the time + cs->lastRollMove = level.time; + // + // make sure we move this frame + AIFunc_BattleRoll( cs ); + // + cs->aifunc = AIFunc_BattleRoll; + return "AIFunc_BattleRoll"; +} + +/* +============= +AIFunc_BattleDiveStart() +============= +*/ +char *AIFunc_BattleDiveStart( cast_state_t *cs, vec3_t vec ) { + int duration; +// gclient_t *client = &level.clients[cs->entityNum]; + // + // backup the current thinkfunc, so we can return to it when done + cs->oldAifunc = cs->aifunc; + // + // face the direction of movement + vectoangles( vec, cs->ideal_viewangles ); + // force crouching anim + BG_UpdateConditionValue( cs->entityNum, ANIM_COND_CROUCHING, qtrue, qfalse ); + // do the roll + duration = BG_AnimScriptEvent( &g_entities[cs->entityNum].client->ps, ANIM_ET_DIVE, qfalse, qtrue ); + // + if ( duration < 0 ) { // it failed + return NULL; + } + // + cs->noAttackTime = level.time + duration - 200; + // set the duration + cs->battleRollTime = level.time + duration; + // move into crouch position + //cs->attackcrouch_time = level.time + (duration) + 1000; + // record the time + cs->lastRollMove = level.time; + // + // make sure we move this frame + AIFunc_BattleRoll( cs ); + // + cs->aifunc = AIFunc_BattleRoll; + return "AIFunc_BattleRoll"; +} + +/* +============= +AIFunc_FlipMove() +============= +*/ +char *AIFunc_FlipMove( cast_state_t *cs ) { + gclient_t *client = &level.clients[cs->entityNum]; + vec3_t dir; + // + if ( !client->ps.torsoTimer ) { + cs->attackcrouch_time = 0; + return AIFunc_Restore( cs ); + } + if ( g_entities[cs->entityNum].health <= 0 ) { + return AIFunc_DefaultStart( cs ); + } + // + // just move in the direction of our ideal_viewangles + AngleVectors( cs->ideal_viewangles, dir, NULL, NULL ); + trap_EA_Move( cs->entityNum, dir, 400 ); + // if we are crouching, move a little faster than normal + if ( cs->attackcrouch_time > level.time ) { + cs->speedScale = 1.5; + } + // + return NULL; +} + +/* +============= +AIFunc_FlipMoveStart() +============= +*/ +char *AIFunc_FlipMoveStart( cast_state_t *cs, vec3_t vec ) { + int duration; +// gclient_t *client = &level.clients[cs->entityNum]; + // + // backup the current thinkfunc, so we can return to it when done + cs->oldAifunc = cs->aifunc; + // + // record the time + cs->lastRollMove = level.time; + // face the direction of movement + vectoangles( vec, cs->ideal_viewangles ); + cs->noAttackTime = level.time + 1200; + // do the roll + duration = BG_AnimScriptEvent( &g_entities[cs->entityNum].client->ps, ANIM_ET_ROLL, qfalse, qfalse ); + // + if ( duration < 0 ) { // it failed + return NULL; + } + // move into crouch position + cs->attackcrouch_time = level.time + 800; + // + // make sure we move this frame + AIFunc_FlipMove( cs ); + // + cs->aifunc = AIFunc_FlipMove; + return "AIFunc_FlipMove"; +} + +/* +============= +AIFunc_BattleHunt() +============= +*/ +char *AIFunc_BattleHunt( cast_state_t *cs ) { + const float chaseDist = 32; + gentity_t *followent, *ent; + bot_state_t *bs; + vec3_t destorg; + qboolean moved = qfalse; +// gclient_t *client = &level.clients[cs->entityNum]; + char *rval; + float dist; + cast_state_t *ocs; + int *ammo, i; + + ent = &g_entities[cs->entityNum]; + + // + // do we need to avoid a danger? + if ( cs->dangerEntityValidTime >= level.time ) { + if ( !AICast_GetTakeCoverPos( cs, cs->dangerEntity, cs->dangerEntityPos, cs->takeCoverPos ) ) { + // shit?? + } + // go to a position that cannot be seen from the dangerPos + cs->takeCoverTime = cs->dangerEntityValidTime + 1000; + cs->attackcrouch_time = 0; + return AIFunc_AvoidDangerStart( cs ); + } + // + // are we waiting for a door? + if ( cs->doorMarkerTime > level.time - 100 ) { + return AIFunc_DoorMarkerStart( cs, cs->doorMarkerDoor, cs->doorMarkerNum ); + } + // + bs = cs->bs; + // + if ( cs->enemyNum < 0 ) { + return AIFunc_IdleStart( cs ); + } + // + ocs = AICast_GetCastState( cs->enemyNum ); + // + if ( cs->aiFlags & AIFL_ATTACK_CROUCH ) { + cs->attackcrouch_time = level.time + 1000; + } + // + followent = &g_entities[cs->enemyNum]; + // + // if the entity is not ready yet + if ( !followent->inuse ) { + // if it's a connecting client, wait + if ( !( ( cs->enemyNum < MAX_CLIENTS ) + && ( ( followent->client && followent->client->pers.connected == CON_CONNECTING ) + || ( level.time < 3000 ) ) ) ) { // they don't exist anymore, stop attacking + cs->enemyNum = -1; + } + + return AIFunc_IdleStart( cs ); + } + // + // if we can see them, go back to an attack state + AICast_ChooseWeapon( cs, qtrue ); // enable special weapons, if we cant get them, change back + if ( AICast_EntityVisible( cs, cs->enemyNum, qtrue ) // take into account reaction time + && AICast_CheckAttack( cs, cs->enemyNum, qfalse ) + && cs->obstructingTime < level.time ) { + if ( AICast_StopAndAttack( cs ) ) { + // TTimo: gcc: suggest () around assignment used as truth value + if ( ( rval = AIFunc_BattleStart( cs ) ) ) { + return rval; + } + } else { // just attack them now and keep chasing + AICast_ProcessAttack( cs ); + } + AICast_ChooseWeapon( cs, qfalse ); + } else + { + int numEnemies, shouldAttack; + + AICast_ChooseWeapon( cs, qfalse ); + + ammo = cs->bs->cur_ps.ammo; + shouldAttack = qfalse; + numEnemies = AICast_ScanForEnemies( cs, enemies ); + if ( numEnemies == -1 ) { // query mode + return NULL; + } else if ( numEnemies == -2 ) { // inspection may be required + char *retval; + if ( cs->aiState < AISTATE_COMBAT ) { + // TTimo: gcc: suggest () around assignment used as truth value + if ( ( retval = AIFunc_InspectFriendlyStart( cs, enemies[0] ) ) ) { + return retval; + } + } + } else if ( numEnemies == -3 ) { // bullet impact + if ( cs->aiState < AISTATE_COMBAT ) { + return AIFunc_InspectBulletImpactStart( cs ); + } + } else if ( numEnemies == -4 ) { // audible event + if ( cs->aiState < AISTATE_COMBAT ) { + return AIFunc_InspectAudibleEventStart( cs, cs->audibleEventEnt ); + } + } else if ( AICast_GotEnoughAmmoForWeapon( cs, cs->bs->cur_ps.weapon ) ) { + if ( numEnemies > 0 ) { + // default to the first known enemy, overwrite if we find a clearer shot + cs->enemyNum = enemies[0]; + // + for ( i = 0; i < numEnemies; i++ ) { + if ( AICast_CheckAttack( cs, enemies[i], qfalse ) || AICast_CheckAttack( AICast_GetCastState( enemies[i] ), cs->entityNum, qfalse ) ) { + cs->enemyNum = enemies[i]; + shouldAttack = qtrue; + break; + } else if ( cs->enemyNum < 0 ) { + cs->lastEnemy = enemies[i]; + } + } + // note: next frame we'll process this new enemy an begin an attack if necessary + } + } + AICast_ChooseWeapon( cs, qfalse ); + } + + // have we spent enough time in combat mode? + if ( cs->aiState == AISTATE_COMBAT ) { + if ( cs->vislist[cs->enemyNum].visible_timestamp < level.time - COMBAT_TIMEOUT ) { + AICast_StateChange( cs, AISTATE_ALERT ); + } + } + + // while hunting, use crouch mode if possible + if ( cs->attributes[ATTACK_CROUCH] >= 0.1 ) { + cs->attackcrouch_time = level.time + 1000; + } + + if ( cs->battleHuntPauseTime ) { + if ( cs->battleHuntPauseTime < level.time ) { + // pausetime has expired, so go into ambush mode + if ( AICast_GetTakeCoverPos( cs, cs->enemyNum, cs->vislist[cs->enemyNum].chase_marker[cs->battleChaseMarker], cs->takeCoverPos ) ) { + // wait in ambush, for them to return + VectorCopy( cs->vislist[cs->enemyNum].chase_marker[cs->battleChaseMarker], cs->combatGoalOrigin ); + return AIFunc_BattleAmbushStart( cs ); + } + // couldn't find a spot, so just stay here? + VectorCopy( cs->bs->origin, cs->combatGoalOrigin ); + VectorCopy( cs->bs->origin, cs->takeCoverPos ); + return AIFunc_BattleAmbushStart( cs ); + } else { + // stay here, looking around + if ( cs->battleHuntViewTime < level.time ) { + cs->battleHuntViewTime = level.time + 700 + rand() % 1000; + // set a random viewangle + cs->ideal_viewangles[YAW] = AngleMod( cs->ideal_viewangles[YAW] + ( 45.0 + random() * 45.0 ) * ( 2 * ( rand() % 2 ) - 1 ) ); + cs->ideal_viewangles[PITCH] = 0; + } + } + } else { + // cycle through markers + VectorCopy( cs->vislist[cs->enemyNum].chase_marker[cs->battleChaseMarker], destorg ); + if ( ( dist = Distance( destorg, cs->bs->origin ) ) < chaseDist ) { + if ( cs->battleChaseMarker == ( cs->vislist[cs->enemyNum].chase_marker_count - 1 ) ) { + cs->battleHuntPauseTime = level.time + 4000; + cs->battleHuntViewTime = level.time + 1000; + } else { + cs->battleChaseMarker += cs->battleChaseMarkerDir; + if ( cs->battleChaseMarker > cs->vislist[cs->enemyNum].chase_marker_count ) { + cs->battleChaseMarkerDir *= -1; + cs->battleChaseMarker = cs->vislist[cs->enemyNum].chase_marker_count - 1; + } + if ( cs->battleChaseMarker < 0 ) { + cs->battleChaseMarkerDir = 1; + cs->battleChaseMarker = 0; + } + } + } + // + if ( cs->battleHuntPauseTime < level.time ) { + // just go to them + if ( !moved && cs->leaderNum < 0 ) { + moveresult = AICast_MoveToPos( cs, destorg, cs->enemyNum ); + if ( moveresult && moveresult->failure ) { // no path, so go back to idle behaviour + // try to go to ambush mode + cs->enemyNum = -1; + return AIFunc_DefaultStart( cs ); + } else { + moved = qtrue; + } + // slow down real close to the goal, so we don't go passed it + cs->speedScale = AICast_SpeedScaleForDistance( cs, dist, chaseDist ); + } + } + } + + // reload? + AICast_IdleReload( cs ); + + return NULL; +} + +/* +============= +AIFunc_BattleHuntStart() +============= +*/ +char *AIFunc_BattleHuntStart( cast_state_t *cs ) { + cs->combatGoalTime = 0; + cs->battleChaseMarker = 0; + cs->battleChaseMarkerDir = 1; + cs->battleHuntPauseTime = 0; + // + cs->aifunc = AIFunc_BattleHunt; + return "AIFunc_BattleHunt"; +} + +/* +============= +AIFunc_BattleAmbush() +============= +*/ +char *AIFunc_BattleAmbush( cast_state_t *cs ) { + bot_state_t *bs; + vec3_t destorg, vec; + float dist, moveDist; + int enemies[MAX_CLIENTS], numEnemies, i; + qboolean shouldAttack, idleYaw; + aicast_predictmove_t move; + int *ammo; + vec3_t dir; +// gclient_t *client = &level.clients[cs->entityNum]; + // + // do we need to avoid a danger? + if ( cs->dangerEntityValidTime >= level.time ) { + if ( AICast_GetTakeCoverPos( cs, cs->dangerEntity, cs->dangerEntityPos, cs->takeCoverPos ) ) { + // go to a position that cannot be seen from the dangerPos + cs->takeCoverTime = cs->dangerEntityValidTime + 1000; + cs->attackcrouch_time = 0; + return AIFunc_AvoidDangerStart( cs ); + } + // if not found, then keep trying, hopefully a spot will free up soon so we can run the hidepos function + } + // + // are we waiting for a door? + if ( cs->doorMarkerTime > level.time - 100 ) { + return AIFunc_DoorMarkerStart( cs, cs->doorMarkerDoor, cs->doorMarkerNum ); + } + + // we need to move towards it + bs = cs->bs; + // + // note: removing this will cause problems down below! + if ( cs->enemyNum < 0 ) { + return AIFunc_IdleStart( cs ); + } + // + // have we spent enough time in combat mode? + if ( cs->aiState == AISTATE_COMBAT ) { + if ( cs->vislist[cs->enemyNum].visible_timestamp < level.time - COMBAT_TIMEOUT ) { + AICast_StateChange( cs, AISTATE_ALERT ); + } + } + // while hunting, use crouch mode if possible + if ( cs->attributes[ATTACK_CROUCH] >= 0.1 ) { + cs->attackcrouch_time = level.time + 2000; + } + // + VectorCopy( cs->takeCoverPos, destorg ); + VectorSubtract( destorg, cs->bs->origin, vec ); + vec[2] *= 0.2; + dist = VectorLength( vec ); + // + // update the chase marker + if ( cs->vislist[cs->enemyNum].chase_marker_count > 0 ) { + VectorCopy( cs->vislist[cs->enemyNum].chase_marker[cs->vislist[cs->enemyNum].chase_marker_count - 1], cs->combatGoalOrigin ); + } + // + // look for things we should attack + // if we are out of ammo, we shouldn't bother trying to attack (and we should keep hiding) + ammo = cs->bs->cur_ps.ammo; + shouldAttack = qfalse; + numEnemies = AICast_ScanForEnemies( cs, enemies ); + + // we shouldnt be interrupted from BattleAmbush mode, so try to handle these without interference + if ( numEnemies == -1 ) { // query mode + // ... + } else if ( numEnemies == -2 ) { // inspection may be required + cs->vislist[enemies[0]].flags |= AIVIS_INSPECTED; // they have been notified + cs->vislist[enemies[0]].flags &= ~AIVIS_INSPECT; // they have been notified + } else if ( numEnemies == -3 ) { // bullet impact + // ... + } else if ( numEnemies == -4 ) { // audible event + // ... + } else if ( numEnemies > 0 ) { + + if ( AICast_GotEnoughAmmoForWeapon( cs, cs->weaponNum ) ) { + // default to the first known enemy, overwrite if we find a clearer shot + cs->enemyNum = enemies[0]; + // + for ( i = 0; i < numEnemies; i++ ) { + // if (we can get them from here) or (they can get us, AND we have stopped going to our ambush spot) + if ( ( AICast_EntityVisible( cs, enemies[i], qfalse ) && AICast_CheckAttack( cs, enemies[i], qfalse ) ) || + ( ( VectorLength( cs->takeCoverPos ) < 1 || dist <= 8 ) && ( AICast_EntityVisible( AICast_GetCastState( enemies[i] ), cs->entityNum, qfalse ) || AICast_CheckAttack( AICast_GetCastState( enemies[i] ), cs->entityNum, qfalse ) || AICast_EntityVisible( AICast_GetCastState( enemies[i] ), cs->entityNum, qtrue ) ) ) ) { + cs->enemyNum = enemies[i]; + return AIFunc_BattleStart( cs ); + } else if ( cs->enemyNum < 0 ) { + cs->lastEnemy = enemies[i]; + } else if ( AICast_EntityVisible( cs, enemies[i], qfalse ) ) { + bot_input_t bi_back; + // try and move to them, if successful, then start chasing + trap_EA_GetInput( cs->entityNum, (float) level.time / 1000, &bi_back ); + if ( AICast_MoveToPos( cs, g_entities[enemies[i]].client->ps.origin, enemies[i] ) ) { + if ( !moveresult->failure ) { + cs->enemyNum = enemies[i]; + return AIFunc_BattleChaseStart( cs ); + } + } else { + trap_EA_ResetInput( cs->entityNum, &bi_back ); + } + } + } + } else { + AICast_ChooseWeapon( cs, qfalse ); + // + if ( !AICast_GotEnoughAmmoForWeapon( cs, cs->weaponNum ) ) { + // NO AMMO LEFT!! + // hide? + if ( AICast_GetTakeCoverPos( cs, cs->enemyNum, cs->vislist[cs->enemyNum].visible_pos, cs->takeCoverPos ) ) { + // go to a position that cannot be seen from the last place we saw the enemy, and wait there for some time + cs->takeCoverTime = level.time + 2000 + rand() % 3000; + return AIFunc_BattleTakeCoverStart( cs ); + } + } + } + + } + // + // keep hiding forever + cs->takeCoverTime = level.time + 1000; + // + memset( &move, 0, sizeof( move ) ); + // + // are we close enough to the goal? + if ( VectorLength( cs->takeCoverPos ) > 1 && dist > 8 && ( cs->obstructingTime < level.time ) /*&& !shouldAttack*/ ) { + const float simTime = 0.8; + float enemyDist; + // + // we haven't reached it yet, make sure we at least wait there for a few seconds after arriving + cs->takeCoverTime = level.time + 2000 + rand() % 2000; + // + moveresult = AICast_MoveToPos( cs, destorg, -1 ); + if ( moveresult ) { + //if the movement failed + if ( moveresult->failure ) { + //reset the avoid reach, otherwise bot is stuck in current area + trap_BotResetAvoidReach( bs->ms ); + // couldn't get there, so stop trying to get there + VectorClear( cs->takeCoverPos ); + dist = 0; + } + // + if ( moveresult->blocked ) { + // abort the TakeCover + VectorClear( cs->takeCoverPos ); + dist = 0; + } + } + // + // NOTE: this is also used by hidepos prediction (below) + // if we are going to bump into something soon, abort it + AICast_PredictMovement( cs, 1, simTime, &move, &cs->lastucmd, -1 ); + enemyDist = Distance( cs->bs->origin, g_entities[cs->enemyNum].s.pos.trBase ); + VectorSubtract( move.endpos, cs->bs->origin, vec ); + moveDist = VectorNormalize( vec ); + // + if ( ( move.numtouch && move.touchents[0] < aicast_maxclients ) // hit something + // or moved closer to the enemy + || ( ( enemyDist < 128 ) + && ( ( enemyDist - 1 ) > ( Distance( move.endpos, g_entities[cs->enemyNum].s.pos.trBase ) ) ) ) ) { + // abort the manouver + VectorClear( cs->takeCoverPos ); + dist = 0; + } + // + // we should slow down on approaching the destination point + else if ( dist < 64 ) { + cs->speedScale = AICast_SpeedScaleForDistance( cs, dist, 0 ); + } + + // don't actually hide, check if we are about to, so we can hide right here + if ( !( cs->aiFlags & AIFL_MISCFLAG1 ) ) { + if ( move.numtouch || !AICast_VisibleFromPos( move.endpos, cs->entityNum, cs->combatGoalOrigin, cs->enemyNum, qfalse ) ) { + // abort the manouver, reached a good spot + cs->aiFlags |= AIFL_MISCFLAG1; + VectorCopy( cs->bs->origin, cs->takeCoverPos ); + } + } + + } else { + // + // check for a movement we should be making + if ( cs->obstructingTime > level.time ) { + AICast_MoveToPos( cs, cs->obstructingPos, -1 ); + } + // if we have some enemies that we can attack immediately (without going anywhere to chase them) + if ( shouldAttack ) { + return AIFunc_BattleStart( cs ); + } + // do we need to go to our leader? + else if ( cs->leaderNum >= 0 && Distance( cs->bs->origin, g_entities[cs->leaderNum].r.currentOrigin ) > MAX_LEADER_DIST ) { + // wait until we've been hiding for long enough + if ( level.time > cs->takeCoverTime ) { + return AIFunc_ChaseGoalStart( cs, cs->leaderNum, AICAST_LEADERDIST_MAX, qtrue ); + } + } + // else, crouch while we hide + if ( cs->attributes[ATTACK_CROUCH] > 0.1 || cs->crouchHideFlag ) { + cs->attackcrouch_time = level.time + 2000; + } + } + // + if ( !( cs->aiFlags & AIFL_WALKFORWARD ) || !VectorLength( cs->bs->cur_ps.velocity ) ) { + idleYaw = qtrue; + // if we know who we are hiding from, face them (hack, so they dont face stupid directions) + if ( cs->enemyNum >= 0 ) { + VectorSubtract( g_entities[cs->enemyNum].s.pos.trBase, cs->bs->origin, dir ); + vectoangles( dir, cs->ideal_viewangles ); + idleYaw = qfalse; + } else if ( cs->lastEnemy >= 0 ) { + VectorSubtract( g_entities[cs->lastEnemy].s.pos.trBase, cs->bs->origin, dir ); + vectoangles( dir, cs->ideal_viewangles ); + idleYaw = qfalse; + } + // if we can see the place we are hiding from, look at it + if ( idleYaw && AICast_VisibleFromPos( cs->bs->origin, cs->entityNum, cs->combatGoalOrigin, cs->lastEnemy, qfalse ) ) { + // face the position we are retreating from + VectorSubtract( cs->combatGoalOrigin, cs->bs->origin, dir ); + dir[2] = 0; + if ( VectorNormalize( dir ) > 4 ) { + idleYaw = qfalse; + vectoangles( dir, cs->ideal_viewangles ); + } + + } + // + if ( idleYaw ) { // look around randomly (but not straight into walls) + + if ( cs->nextIdleAngleChange < level.time ) { + // wait a second before changing again + if ( ( cs->nextIdleAngleChange + 3000 ) < level.time ) { + + // FIXME: This could be changed to use some AAS sampling, which would: + // + // Given a src area, pick a random dest area which is visible from that area + // and return it's position, which we'd then use to set the next view angles + // + // This would result in more efficient, more realistic behaviour, since they'd + // also use PITCH angles to look at areas above/below them + + cs->idleYaw = AICast_GetRandomViewAngle( cs, 512 ); + + if ( abs( AngleDelta( cs->idleYaw, cs->ideal_viewangles[YAW] ) ) < 45 ) { + cs->nextIdleAngleChange = level.time + 1000 + rand() % 2500; + } else { // do really fast + cs->nextIdleAngleChange = level.time + 500; + } + + // adjust with time + cs->idleYawChange = AngleDelta( cs->idleYaw, cs->ideal_viewangles[YAW] ); + /// ((float)(cs->nextIdleAngleChange - level.time) / 1000.0); + + cs->ideal_viewangles[PITCH] = 0; + } + } else if ( cs->idleYawChange ) { + cs->idleYawChange = AngleDelta( cs->idleYaw, cs->ideal_viewangles[YAW] ); + cs->ideal_viewangles[YAW] = AngleMod( cs->ideal_viewangles[YAW] + ( cs->idleYawChange * cs->bs->thinktime ) ); + } + + } + } + // + if ( !cs->crouchHideFlag && cs->enemyNum < 0 ) { // no enemy, and no need to crouch, so stop crouching + if ( cs->attackcrouch_time > level.time + 1000 ) { + cs->attackcrouch_time = level.time + 1000; + } + } + + // reload? + AICast_IdleReload( cs ); + + return NULL; +} + +/* +============= +AIFunc_BattleAmbushStart() +============= +*/ +char *AIFunc_BattleAmbushStart( cast_state_t *cs ) { + if ( !AICast_CanMoveWhileFiringWeapon( cs->weaponNum ) ) { + // always run to the cover point + cs->attackcrouch_time = 0; + } else if ( cs->attackcrouch_time > level.time + 1000 ) { + cs->attackcrouch_time = level.time + 1000; + } + + // + // start a crouch attack? + if ( cs->attributes[ATTACK_CROUCH] > 0.1 && cs->attackcrouch_time >= level.time ) { + // continue + cs->attackcrouch_time = level.time + 1000; + } + // if we arent crouching, start crouching soon after we start retreating + if ( cs->attributes[ATTACK_CROUCH] > 0.1 ) { + cs->aiFlags |= AIFL_ATTACK_CROUCH; + } else { + cs->aiFlags &= ~AIFL_ATTACK_CROUCH; + } + + // miscflag1 used to set predicted point as our goal, so we dont keep setting this over and over + cs->aiFlags &= ~AIFL_MISCFLAG1; + + cs->aifunc = AIFunc_BattleAmbush; + return "AIFunc_BattleAmbush"; +} + +/* +============ +AIFunc_BattleChase() +============ +*/ +char *AIFunc_BattleChase( cast_state_t *cs ) { + const float chaseDist = 32; + gentity_t *followent, *ent; + bot_state_t *bs; + vec3_t destorg; + qboolean moved = qfalse; + gclient_t *client = &level.clients[cs->entityNum]; + char *rval; + float dist; + cast_state_t *ocs; + + ent = &g_entities[cs->entityNum]; + + // + // do we need to avoid a danger? + if ( cs->dangerEntityValidTime >= level.time ) { + if ( !AICast_GetTakeCoverPos( cs, cs->dangerEntity, cs->dangerEntityPos, cs->takeCoverPos ) ) { + // shit?? + } + // go to a position that cannot be seen from the dangerPos + cs->takeCoverTime = cs->dangerEntityValidTime + 1000; + cs->attackcrouch_time = 0; + return AIFunc_AvoidDangerStart( cs ); + } + // + // are we waiting for a door? + if ( cs->doorMarkerTime > level.time - 100 ) { + return AIFunc_DoorMarkerStart( cs, cs->doorMarkerDoor, cs->doorMarkerNum ); + } + + bs = cs->bs; + // + if ( cs->enemyNum < 0 ) { + return AIFunc_IdleStart( cs ); + } + // + // Retreat? + if ( AICast_WantToRetreat( cs ) ) { + if ( AICast_GetTakeCoverPos( cs, cs->enemyNum, cs->vislist[cs->enemyNum].visible_pos, cs->takeCoverPos ) ) { + // go to a position that cannot be seen from the last place we saw the enemy, and wait there for some time + cs->takeCoverTime = level.time + 2000 + rand() % 3000; + return AIFunc_BattleTakeCoverStart( cs ); + } + } + // + ocs = AICast_GetCastState( cs->enemyNum ); + // + if ( cs->aiFlags & AIFL_ATTACK_CROUCH ) { + if ( cs->attackcrouch_time > level.time || ( cs->thinkFuncChangeTime < level.time - 1000 ) ) { + cs->attackcrouch_time = level.time + 1000; + } + } + // + followent = &g_entities[cs->enemyNum]; + // + // if the entity is not ready yet + if ( !followent->inuse ) { + // if it's a connecting client, wait + if ( !( ( cs->enemyNum < MAX_CLIENTS ) + && ( ( followent->client && followent->client->pers.connected == CON_CONNECTING ) + || ( level.time < 3000 ) ) ) ) { // they don't exist anymore, stop attacking + cs->enemyNum = -1; + } + + return AIFunc_IdleStart( cs ); + } + // + // if we can see them, go back to an attack state + AICast_ChooseWeapon( cs, qtrue ); // enable special weapons, if we cant get them, change back + if ( AICast_EntityVisible( cs, cs->enemyNum, qtrue ) // take into account reaction time + && AICast_CheckAttack( cs, cs->enemyNum, qfalse ) + && cs->obstructingTime < level.time ) { + if ( AICast_StopAndAttack( cs ) ) { + // TTimo: gcc: suggest () around assignment used as truth value + if ( ( rval = AIFunc_BattleStart( cs ) ) ) { + return rval; + } + } else { // just attack them now and keep chasing + AICast_ProcessAttack( cs ); + } + AICast_ChooseWeapon( cs, qfalse ); + } else + { + AICast_ChooseWeapon( cs, qfalse ); + // not visible, go to their previously visible position + /* + if (!cs->vislist[cs->enemyNum].visible_timestamp || Distance( bs->origin, cs->vislist[cs->enemyNum].visible_pos ) < 16) + { + // we're done attacking, go back to default state, which in turn will recall previous state + // + return AIFunc_DefaultStart( cs ); + } + */ + } + // + // find the chase position + if ( followent->client ) { + // go to the last visible position + VectorCopy( cs->vislist[cs->enemyNum].visible_pos, destorg ); + // if we have reached it, go into hunt mode + if ( ( dist = Distance( destorg, cs->bs->origin ) ) < chaseDist ) { + // if we haven't been hunted for a while, do so + if ( ocs->lastBattleHunted < level.time - 5000 ) { + ocs->lastBattleHunted = level.time; + return AIFunc_BattleHuntStart( cs ); + } + // otherwise, go into ambush mode + if ( AICast_GetTakeCoverPos( cs, cs->enemyNum, cs->vislist[cs->enemyNum].real_visible_pos, cs->takeCoverPos ) ) { + VectorCopy( cs->vislist[cs->enemyNum].real_visible_pos, cs->combatGoalOrigin ); + return AIFunc_BattleAmbushStart( cs ); + } + // couldn't find a spot, so just stay here? + VectorCopy( cs->bs->origin, cs->combatGoalOrigin ); + VectorCopy( cs->bs->origin, cs->takeCoverPos ); + return AIFunc_BattleAmbushStart( cs ); + } + } else // assume we know where other entities are + { + VectorCopy( followent->s.pos.trBase, destorg ); + dist = Distance( cs->bs->origin, destorg ); + } + // + // if the enemy is inside a CONTENTS_DONOTENTER brush, and we are close enough, stop chasing them + if ( AICast_EntityVisible( cs, cs->enemyNum, qtrue ) && VectorDistance( cs->bs->origin, destorg ) < 384 ) { + if ( trap_PointContents( destorg, cs->enemyNum ) & ( CONTENTS_DONOTENTER | CONTENTS_DONOTENTER_LARGE ) ) { + // just stay here, and hope they move out of the brush without finding a spot where they can hit us but we can't hit them + return NULL; + } + } + // + // is there someone else we can go for instead? + numEnemies = AICast_ScanForEnemies( cs, enemies ); + if ( numEnemies == -1 ) { // query mode + return NULL; + } else if ( numEnemies == -2 ) { // inspection may be required + char *retval; + // TTimo: gcc: suggest () around assignment used as truth value + if ( ( retval = AIFunc_InspectFriendlyStart( cs, enemies[0] ) ) ) { + return retval; + } + } + AICast_ChooseWeapon( cs, qtrue ); // enable special weapons, if we cant get them, change back + if ( numEnemies > 0 ) { + int i; + for ( i = 0; i < numEnemies; i++ ) { + if ( enemies[i] != cs->enemyNum && AICast_CheckAttack( cs, enemies[i], qfalse ) ) { + cs->enemyNum = enemies[i]; + return AIFunc_BattleStart( cs ); + } + } + } + AICast_ChooseWeapon( cs, qfalse ); + + // + // if we only recently saw them, face them + // + /* RF: disabled 9/19/01, characters like boss2 supersoldier are forced to walk backwards and look wierd + if (cs->vislist[cs->enemyNum].visible_timestamp > level.time - 3000) { + AICast_AimAtEnemy( cs ); // be ready for an attack if they become visible again + //if (cs->attributes[ATTACK_CROUCH] > 0.1) { // crouching for combat + // cs->attackcrouch_time = level.time + 1000; + //} + } + */ + + // + // Lob a Grenade? + // if we haven't thrown a grenade in a bit, go into "grenade flush mode" + if ( ( lastGrenadeFlush > level.time || lastGrenadeFlush < level.time - 5000 ) && + ( cs->aiState >= AISTATE_COMBAT ) && + ( cs->castScriptStatus.castScriptEventIndex < 0 ) && + ( cs->startGrenadeFlushTime < level.time - 3000 ) && + ( COM_BitCheck( cs->bs->cur_ps.weapons, WP_GRENADE_LAUNCHER ) ) && + ( AICast_GotEnoughAmmoForWeapon( cs, WP_GRENADE_LAUNCHER ) ) && + ( cs->weaponFireTimes[WP_GRENADE_LAUNCHER] < level.time - (int)( 1000 + aicast_skillscale * 1000 ) ) && + ( ( cs->weaponNum == WP_GRENADE_LAUNCHER ) || !( cs->castScriptStatus.scriptFlags & SFL_NOCHANGEWEAPON ) ) && + ( Distance( cs->bs->origin, cs->vislist[cs->enemyNum].visible_pos ) > 100 ) && + ( Distance( cs->bs->origin, cs->vislist[cs->enemyNum].visible_pos ) < 1400 ) ) { + // try and flush them out with a grenade + //G_Printf("pineapple?\n"); + return AIFunc_GrenadeFlushStart( cs ); + } else if ( ( lastGrenadeFlush > level.time || lastGrenadeFlush < level.time - 5000 ) && + ( cs->aiState >= AISTATE_COMBAT ) && + ( cs->castScriptStatus.castScriptEventIndex < 0 ) && + ( cs->startGrenadeFlushTime < level.time - 3000 ) && + ( COM_BitCheck( cs->bs->cur_ps.weapons, WP_GRENADE_PINEAPPLE ) ) && + ( AICast_GotEnoughAmmoForWeapon( cs, WP_GRENADE_PINEAPPLE ) ) && + ( cs->weaponFireTimes[WP_GRENADE_PINEAPPLE] < level.time - (int)( 1000 + aicast_skillscale * 1000 ) ) && + ( ( cs->weaponNum == WP_GRENADE_PINEAPPLE ) || !( cs->castScriptStatus.scriptFlags & SFL_NOCHANGEWEAPON ) ) && + ( Distance( cs->bs->origin, cs->vislist[cs->enemyNum].visible_pos ) > 100 ) && + ( Distance( cs->bs->origin, cs->vislist[cs->enemyNum].visible_pos ) < 1400 ) ) { + // try and flush them out with a grenade + //G_Printf("pineapple?\n"); + return AIFunc_GrenadeFlushStart( cs ); + } + // + // Flaming Zombie? Shoot flames while running + if ( ( cs->aiCharacter == AICHAR_ZOMBIE ) && + ( IS_FLAMING_ZOMBIE( ent->s ) ) && + ( fabs( cs->ideal_viewangles[YAW] - cs->viewangles[YAW] ) < 5 ) ) { + if ( fabs( sin( ( level.time + cs->entityNum * 314 ) / 1000 ) * cos( ( level.time + cs->entityNum * 267 ) / 979 ) ) < 0.5 ) { + ent->s.time = level.time + 800; + } + } + // reload? + AICast_IdleReload( cs ); + + if ( dist < chaseDist ) { + return NULL; + } + + // + // go to them + // + // ........................................................... + // Do the movement.. + // + // move straight to them if we can + if ( !moved && cs->leaderNum < 0 && + ( cs->bs->cur_ps.groundEntityNum != ENTITYNUM_NONE || g_entities[cs->entityNum].waterlevel > 1 ) && + AICast_EntityVisible( cs, cs->enemyNum, qtrue ) ) { + aicast_predictmove_t move; + vec3_t dir; + bot_input_t bi; + usercmd_t ucmd; + trace_t tr; + + // trace will eliminate most unsuccessful paths + trap_Trace( &tr, cs->bs->origin, NULL, NULL, followent->r.currentOrigin, cs->entityNum, g_entities[cs->entityNum].clipmask ); + if ( tr.entityNum == followent->s.number ) { + // try walking straight to them + VectorSubtract( followent->r.currentOrigin, cs->bs->origin, dir ); + VectorNormalize( dir ); + if ( !ent->waterlevel ) { + dir[2] = 0; + } + //trap_EA_Move(cs->entityNum, dir, 400); + trap_EA_GetInput( cs->entityNum, (float) level.time / 1000, &bi ); + VectorCopy( dir, bi.dir ); + bi.speed = 400; + bi.actionflags = 0; + AICast_InputToUserCommand( cs, &bi, &ucmd, bs->cur_ps.delta_angles ); + AICast_PredictMovement( cs, 5, 2.0, &move, &ucmd, cs->enemyNum ); + + if ( move.stopevent == PREDICTSTOP_HITENT ) { // success! + trap_EA_Move( cs->entityNum, dir, 400 ); + // RF, if we are really close, we might be stuck on a corner, so randomly move sideways + if ( ( VectorLength( followent->client->ps.velocity ) < 50 ) && ( dist < 10 + ( sqrt( cs->bs->cur_ps.maxs[0] * cs->bs->cur_ps.maxs[0] * 8.0 ) / 2.0 + sqrt( followent->client->ps.maxs[0] * followent->client->ps.maxs[0] * 8.0 ) / 2.0 ) ) ) { + // if the box trace is unsuccessful + trap_Trace( &tr, cs->bs->origin, cs->bs->cur_ps.mins, cs->bs->cur_ps.maxs, followent->r.currentOrigin, cs->entityNum, g_entities[cs->entityNum].clipmask ); + if ( tr.entityNum != followent->s.number ) { + if ( level.time % 6000 < 2000 ) { + trap_EA_MoveRight( cs->entityNum ); + } else { + trap_EA_MoveLeft( cs->entityNum ); + } + } + } + vectoangles( dir, cs->ideal_viewangles ); + cs->ideal_viewangles[2] *= 0.5; + moved = qtrue; + } else { // clear movement + //trap_EA_Move(cs->entityNum, dir, 0); + } + } + } + // + // if they are visible, but not attackable, look for a spot where we can attack them, and head + // for there. This should prevent AI's getting stuck in a bunch. + if ( !moved && cs->weaponNum >= WP_LUGER && cs->weaponNum <= WP_AKIMBO && cs->attributes[TACTICAL] >= 0.1 ) { + // + // check for another movement we should be making + if ( cs->obstructingTime > level.time ) { + AICast_MoveToPos( cs, cs->obstructingPos, -1 ); + moved = qtrue; + } + // + if ( cs->leaderNum >= 0 ) { + if ( cs->combatGoalTime < level.time ) { + if ( cs->attackSpotTime < level.time ) { + cs->attackSpotTime = level.time + 500 + rand() % 500; + if ( trap_AAS_FindAttackSpotWithinRange( cs->entityNum, cs->leaderNum, cs->enemyNum, MAX_LEADER_DIST, AICAST_TFL_DEFAULT, cs->combatGoalOrigin ) ) { + cs->combatGoalTime = level.time + 2000; + } + } + } + if ( cs->combatGoalTime > level.time ) { + if ( Distance( cs->combatGoalOrigin, g_entities[cs->leaderNum].r.currentOrigin ) > MAX_LEADER_DIST ) { + // go find a new combatSpot + cs->combatGoalTime = 0; + } else { + // go to the combat spot + moveresult = AICast_MoveToPos( cs, cs->combatGoalOrigin, -1 ); + if ( moveresult && moveresult->failure ) { // no path, so go back to idle behaviour + cs->combatGoalTime = 0; + } else { + moved = qtrue; + if ( Distance( cs->bs->origin, cs->combatGoalOrigin ) < 32 ) { + cs->combatGoalTime = 0; + } + } + } + } else { + // we can't find a way to get to our enemy, so go back to our leader if outside range + // do we need to go to our leader? + if ( Distance( cs->bs->origin, g_entities[cs->leaderNum].r.currentOrigin ) > MAX_LEADER_DIST ) { + return AIFunc_ChaseGoalStart( cs, cs->leaderNum, AICAST_LEADERDIST_MAX, qtrue ); + } + } + } else { + if ( cs->combatGoalTime < level.time ) { + if ( cs->attackSpotTime < level.time ) { + cs->attackSpotTime = level.time + 500 + rand() % 500; + if ( trap_AAS_FindAttackSpotWithinRange( cs->entityNum, cs->entityNum, cs->enemyNum, 512, AICAST_TFL_DEFAULT, cs->combatGoalOrigin ) ) { + cs->combatGoalTime = level.time + 2000; + } + } + } + if ( cs->combatGoalTime > level.time ) { + // go to the combat spot + moveresult = AICast_MoveToPos( cs, cs->combatGoalOrigin, -1 ); + if ( moveresult && moveresult->failure ) { // no path, so go back to idle behaviour + cs->combatGoalTime = 0; + } else { + moved = qtrue; + if ( Distance( cs->bs->origin, cs->combatGoalOrigin ) < 32 ) { + cs->combatGoalTime = 0; + cs->attackSpotTime = level.time + 12000; // dont go to another combatspot for some time, prevent repetitive behaviour + } + } + } + } + } + // just go to them + if ( !moved && cs->leaderNum < 0 ) { + moveresult = AICast_MoveToPos( cs, destorg, cs->enemyNum ); + if ( moveresult && moveresult->failure ) { // no path, so try and hude from them + // pausetime has expired, so go into ambush mode + if ( AICast_GetTakeCoverPos( cs, cs->enemyNum, cs->vislist[cs->enemyNum].real_visible_pos, cs->takeCoverPos ) ) { + // wait in ambush, for them to return + VectorCopy( cs->bs->origin, cs->combatGoalOrigin ); + return AIFunc_BattleAmbushStart( cs ); + } + // HACK, help lopers get out of bad spots + if ( cs->aiCharacter == AICHAR_LOPER ) { + cs->weaponFireTimes[WP_MONSTER_ATTACK2] = 0; + } + // couldn't find a spot, so just stay here? + if ( cs->bs->areanum ) { // if our area is valid + VectorCopy( cs->bs->origin, cs->combatGoalOrigin ); + VectorCopy( cs->bs->origin, cs->takeCoverPos ); + return AIFunc_BattleAmbushStart( cs ); + } + } else { + moved = qtrue; + } + } + // + // slow down real close to the goal, so we don't go passed it + cs->speedScale = AICast_SpeedScaleForDistance( cs, dist, chaseDist ); + // + // ........................................................... + // speed up over some time + #define BATTLE_CHASE_ACCEL_TIME 300 + if ( ( cs->attributes[RUNNING_SPEED] > 170 ) && ( cs->weaponNum != WP_GAUNTLET ) && ( level.time < ( cs->startBattleChaseTime + BATTLE_CHASE_ACCEL_TIME ) ) ) { + float ideal; + + ideal = 0.5 + 0.5 * ( 1.0 - ( (float)( ( cs->startBattleChaseTime + BATTLE_CHASE_ACCEL_TIME ) - level.time ) / BATTLE_CHASE_ACCEL_TIME ) ); + if ( ideal < cs->speedScale ) { + cs->speedScale = ideal; + } + } + // + // if we are going to reach them soon, predict the attack + { + float simTime = 1.5; + aicast_predictmove_t move; + float moveDist; + vec3_t vec; + // + if ( cs->weaponNum == WP_GAUNTLET ) { + simTime = 0.5; + } + // + AICast_PredictMovement( cs, 1, simTime, &move, &cs->lastucmd, cs->enemyNum ); + VectorSubtract( move.endpos, cs->bs->origin, vec ); + moveDist = VectorNormalize( vec ); + // + if ( cs->weaponNum == WP_GAUNTLET ) { + if ( move.stopevent == PREDICTSTOP_HITENT ) { + AICast_AimAtEnemy( cs ); + trap_EA_Attack( bs->client ); + cs->bFlags |= BFL_ATTACKED; + } + } + // + // do we went to play a diving animation into a cover position? + else if ( ( ( cs->attributes[TACTICAL] > 0.85 ) && ( cs->aiFlags & AIFL_ROLL_ANIM ) && !client->ps.torsoTimer && !client->ps.legsTimer && ( cs->lastRollMove < level.time - 800 ) && ( move.numtouch == 0 ) && ( moveDist > simTime * cs->attributes[RUNNING_SPEED] * 0.98 ) && move.groundEntityNum == ENTITYNUM_WORLD ) && + ( AICast_CheckAttackAtPos( cs->entityNum, cs->enemyNum, move.endpos, cs->attackcrouch_time > level.time, qfalse ) ) ) { + cs->takeCoverTime = 0; + return AIFunc_BattleRollStart( cs, vec ); + } + // + else if ( cs->aiFlags & AIFL_FLIP_ANIM && cs->lastRollMove < level.time - 800 && !client->ps.torsoTimer && cs->castScriptStatus.castScriptEventIndex < 0 && move.numtouch == 0 && moveDist > simTime * cs->attributes[RUNNING_SPEED] * 0.9 && move.groundEntityNum == ENTITYNUM_WORLD && cs->attackcrouch_time < level.time ) { + int destarea, simarea, starttravel, simtravel; + // if we'll be closer after the move, proceed + destarea = BotPointAreaNum( destorg ); + simarea = BotPointAreaNum( move.endpos ); + starttravel = trap_AAS_AreaTravelTimeToGoalArea( cs->bs->areanum, cs->bs->origin, destarea, cs->travelflags ); + simtravel = trap_AAS_AreaTravelTimeToGoalArea( simarea, move.endpos, destarea, cs->travelflags ); + if ( simtravel < starttravel ) { + return AIFunc_FlipMoveStart( cs, vec ); + } + } + // slow down? so we don't go too far from behind the obstruction which is protecting us + else if ( !( cs->aiFlags & AIFL_WALKFORWARD ) && ( VectorDistance( cs->bs->origin, g_entities[cs->enemyNum].s.pos.trBase ) < AICast_WeaponRange( cs, cs->weaponNum ) ) && + ( cs->obstructingTime < level.time ) && ( cs->attributes[TACTICAL] > 0.1 ) && + ( AICast_VisibleFromPos( cs->vislist[cs->enemyNum].visible_pos, cs->enemyNum, move.endpos, cs->entityNum, qfalse ) ) ) { + // start a crouch attack? + //if (cs->attributes[ATTACK_CROUCH] > 0.1) { + // cs->attackcrouch_time = level.time + 3000; + //else + cs->attackcrouch_time = 0; + if ( cs->bs->cur_ps.viewheight > cs->bs->cur_ps.crouchViewHeight && cs->attributes[RUNNING_SPEED] * cs->speedScale > 120 ) { + cs->speedScale = 120.0 * cs->attributes[RUNNING_SPEED]; + } + // also face them, ready for the attack + if ( cs->attributes[RUNNING_SPEED] > 140 ) { + AICast_AimAtEnemy( cs ); + } + /* disabled, causes them to use up ammo in the clip before they get visible + if ((cs->castScriptStatus.scriptNoAttackTime < level.time) && (cs->noAttackTime < level.time)) { + // if we are using a bullet weapon, start firing now + switch (cs->weaponNum) { + case WP_MP40: + case WP_VENOM: + case WP_THOMPSON: + case WP_STEN: //----(SA) added + trap_EA_Attack(cs->entityNum); + } + } + */ + } + } + + // reload? + AICast_IdleReload( cs ); + + return NULL; +} + +/* +============ +AIFunc_BattleChaseStart() +============ +*/ +char *AIFunc_BattleChaseStart( cast_state_t *cs ) { + cs->startBattleChaseTime = level.time; + cs->combatGoalTime = 0; + cs->battleChaseMarker = -99; + cs->battleChaseMarkerDir = 1; + // don't wait too long before taking cover, if we just aborted one + if ( cs->takeCoverTime > level.time ) { + cs->takeCoverTime = level.time + 1500 + rand() % 500; + } + // + // start a crouch attack? + if ( cs->attributes[ATTACK_CROUCH] > 0.1 ) { + cs->aiFlags |= AIFL_ATTACK_CROUCH; + } else { + cs->aiFlags &= ~AIFL_ATTACK_CROUCH; + } + // + cs->aifunc = AIFunc_BattleChase; + return "AIFunc_BattleChase"; +} + +/* +============ +AIFunc_AvoidDanger() +============ +*/ +char *AIFunc_AvoidDanger( cast_state_t *cs ) { + bot_state_t *bs; + vec3_t destorg, vec; + float dist; + int enemies[MAX_CLIENTS], numEnemies, i; + qboolean shouldAttack; + gentity_t *ent; + trace_t tr; + vec3_t end; + gentity_t *danger; + + // we need to move towards it + bs = cs->bs; + ent = g_entities + cs->entityNum; + // + // TODO: if we are on fire, play the correct torso animation + if ( ent->s.onFireEnd > level.time ) { + // set the animation, and a short timer, but long enough to last until the next frame + //if (g_cheats.integer) G_Printf( "TODO: torso onfire animation\n" ); + } + // + // is the danger gone? + if ( cs->dangerEntityValidTime < level.time ) { + return AIFunc_DefaultStart( cs ); + } + // + // special case: if it's a grenade, and it's going to land near us with some time left before it + // explodes, try and kick it back + // + danger = &g_entities[cs->dangerEntity]; + if ( ent->s.onFireEnd < level.time ) { + if ( ( danger->s.weapon == WP_GRENADE_LAUNCHER || danger->s.weapon == WP_GRENADE_PINEAPPLE ) && + ( danger->nextthink - level.time > 1500 ) && + ( level.lastGrenadeKick < level.time - 3000 ) && + ( cs->aiFlags & AIFL_CATCH_GRENADE ) && + !( danger->flags & FL_AI_GRENADE_KICK ) ) { + // if it was thrown by a friend of ours, leave it alone + if ( !AICast_SameTeam( cs, danger->r.ownerNum ) ) { + if ( G_PredictMissile( danger, danger->nextthink - level.time, cs->takeCoverPos, qfalse ) ) { + // make sure it's a valid position, and drop it down to the ground + cs->takeCoverPos[2] += -ent->r.mins[2] + 12; + VectorCopy( cs->takeCoverPos, end ); + end[2] -= 90; + trap_Trace( &tr, cs->takeCoverPos, ent->r.mins, ent->r.maxs, end, cs->entityNum, MASK_SOLID ); + VectorCopy( tr.endpos, cs->takeCoverPos ); + if ( !tr.startsolid && ( tr.fraction < 1.0 ) && + VectorDistance( cs->bs->origin, cs->takeCoverPos ) < cs->attributes[RUNNING_SPEED] * 0.0004 * ( danger->nextthink - level.time - 2000 ) ) { + + // check for a clear path to the grenade + trap_Trace( &tr, cs->bs->origin, ent->r.mins, ent->r.maxs, cs->takeCoverPos, cs->entityNum, MASK_SOLID ); + + if ( VectorDistance( tr.endpos, cs->takeCoverPos ) < 8 ) { + danger->flags |= FL_AI_GRENADE_KICK; + ent->flags |= FL_AI_GRENADE_KICK; + level.lastGrenadeKick = level.time; + return AIFunc_GrenadeKickStart( cs ); // we should decide our course of action within this start function (dive or return grenade) + } + } + } + } + // if it's really close to us, and we're heading for it, may as well pick it up + if ( VectorLength( danger->s.pos.trDelta ) < 10 && VectorDistance( danger->r.currentOrigin, cs->bs->origin ) < 128 && + ( level.lastGrenadeKick < level.time - 3000 ) && + ( cs->aiFlags & AIFL_CATCH_GRENADE ) ) { + vec3_t vec; + VectorSubtract( danger->r.currentOrigin, cs->bs->origin, vec ); + if ( DotProduct( vec, cs->bs->velocity ) > 0 ) { + danger->flags |= FL_AI_GRENADE_KICK; + ent->flags |= FL_AI_GRENADE_KICK; + level.lastGrenadeKick = level.time; + return AIFunc_GrenadeKickStart( cs ); // we should decide our course of action within this start function (dive or return grenade) + } + } + } + } + // + if ( g_entities[cs->dangerEntity].inuse ) { + // is our current destination still safe? + if ( Distance( cs->dangerEntityPos, cs->takeCoverPos ) < cs->dangerDist && + AICast_VisibleFromPos( cs->dangerEntityPos, cs->dangerEntity, cs->takeCoverPos, cs->entityNum, qfalse ) ) { + //G_Printf("current coverPos is dangerous, looking for a better place..\n" ); + if ( !AICast_GetTakeCoverPos( cs, cs->dangerEntity, cs->dangerEntityPos, cs->takeCoverPos ) ) { + // just run away from it ??? + } + } + } else { + // the entity isn't here anymore, so stop hiding + cs->dangerEntityValidTime = -1; + return AIFunc_DefaultStart( cs ); + } + // + VectorCopy( cs->takeCoverPos, destorg ); + VectorSubtract( destorg, cs->bs->origin, vec ); + vec[2] *= 0.2; + dist = VectorLength( vec ); + // + shouldAttack = qfalse; + if ( ent->s.onFireEnd < level.time ) { + // look for things we should attack + numEnemies = AICast_ScanForEnemies( cs, enemies ); + if ( numEnemies > 0 ) { + // default to the first known enemy, overwrite if we find a clearer shot + cs->enemyNum = enemies[0]; + // + for ( i = 0; i < numEnemies; i++ ) { + if ( AICast_CheckAttack( cs, enemies[i], qfalse ) || AICast_CheckAttack( AICast_GetCastState( enemies[i] ), cs->entityNum, qfalse ) ) { + cs->enemyNum = enemies[i]; + shouldAttack = qtrue; + break; + } else if ( cs->enemyNum < 0 ) { + cs->lastEnemy = enemies[i]; + } + } + } + } + // + // if we are now safe from the danger, stop running away + if ( cs->dangerEntity >= MAX_CLIENTS && Distance( cs->dangerEntityPos, cs->bs->origin ) > cs->dangerDist * 1.5 ) { + // don't move, wait for danger to pass + } else + // are we close enough to the goal? + if ( dist > 8 ) { + moveresult = AICast_MoveToPos( cs, destorg, -1 ); + if ( moveresult ) { + //if the movement failed + if ( moveresult->failure || moveresult->blocked ) { + //reset the avoid reach, otherwise bot is stuck in current area + trap_BotResetAvoidReach( bs->ms ); + if ( g_entities[cs->dangerEntity].inuse ) { + // find a better spot? + AICast_GetTakeCoverPos( cs, cs->dangerEntity, cs->dangerEntityPos, cs->takeCoverPos ); + } else { + VectorCopy( cs->bs->origin, cs->takeCoverPos ); + } + } + } + if ( ent->s.onFireEnd < level.time ) { + // slow down real close to the goal, so we don't go passed it + cs->speedScale = AICast_SpeedScaleForDistance( cs, dist, 0 ); + } + // + // pretend we can still see them while we run to our hide pos, this way they are less likely + // to forget about their enemy once they get there + if ( ent->s.onFireEnd < level.time && cs->enemyNum >= 0 && cs->vislist[cs->enemyNum].real_visible_timestamp && ( cs->vislist[cs->enemyNum].real_visible_timestamp > level.time - 10000 ) ) { + AICast_UpdateVisibility( &g_entities[cs->entityNum], &g_entities[cs->enemyNum], qfalse, cs->vislist[cs->enemyNum].real_visible_timestamp == cs->vislist[cs->enemyNum].lastcheck_timestamp ); + } + + } else { + // set our origin as the hidepos, that way if we are still in danger, we should find a better spot + VectorCopy( cs->bs->origin, cs->takeCoverPos ); + // check for a movement we should be making + if ( cs->obstructingTime > level.time ) { + AICast_MoveToPos( cs, cs->obstructingPos, -1 ); + } + + // if we are on fire, never stop + if ( ent->s.onFireEnd > level.time ) { + VectorCopy( cs->bs->origin, cs->dangerEntityPos ); + cs->dangerEntityValidTime = level.time + 10000; + } + + } + // + // if we should be attacking something on our way + if ( shouldAttack ) { + //attack the enemy if possible + AICast_ProcessAttack( cs ); + } else { //if (dist < 48) { + // if we've recently been in a fight, look towards the enemy + if ( cs->lastEnemy >= 0 ) { + // if we only just recently saw them, face that direction + if ( cs->vislist[cs->lastEnemy].visible_timestamp > ( level.time - 20000 ) ) { + vec3_t dir; + // + VectorSubtract( cs->vislist[cs->lastEnemy].visible_pos, cs->bs->origin, dir ); + VectorNormalize( dir ); + vectoangles( dir, cs->ideal_viewangles ); + } + } + } + + // reload? + AICast_IdleReload( cs ); + + return NULL; +} + +/* +============ +AIFunc_AvoidDangerStart() +============ +*/ +char *AIFunc_AvoidDangerStart( cast_state_t *cs ) { + // + //if (!AICast_CanMoveWhileFiringWeapon( cs->weaponNum )) { + // always run to the cover point + cs->attackcrouch_time = 0; + //} + // make sure we move if we are allowed (scripting will overwrite this if necessary) + cs->castScriptStatus.scriptNoMoveTime = 0; + // resume following once danger has gone + cs->castScriptStatus.scriptGotoId = -1; + // + cs->aifunc = AIFunc_AvoidDanger; + return "AIFunc_AvoidDanger"; +} + +/* +============ +AIFunc_BattleTakeCover() +============ +*/ +char *AIFunc_BattleTakeCover( cast_state_t *cs ) { + bot_state_t *bs; + vec3_t destorg, vec; + float dist, moveDist; + int enemies[MAX_CLIENTS], numEnemies, i; + qboolean shouldAttack; + aicast_predictmove_t move; + int *ammo; + gclient_t *client = &level.clients[cs->entityNum]; + // + // do we need to avoid a danger? + if ( cs->dangerEntityValidTime >= level.time ) { + if ( !AICast_GetTakeCoverPos( cs, cs->dangerEntity, cs->dangerEntityPos, cs->takeCoverPos ) ) { + // shit?? + } + // go to a position that cannot be seen from the dangerPos + cs->takeCoverTime = cs->dangerEntityValidTime + 1000; + cs->attackcrouch_time = 0; + return AIFunc_AvoidDangerStart( cs ); + } + // + // are we waiting for a door? + if ( cs->doorMarkerTime > level.time - 100 ) { + return AIFunc_DoorMarkerStart( cs, cs->doorMarkerDoor, cs->doorMarkerNum ); + } + + // we need to move towards it + bs = cs->bs; + // + // note: removing this will cause problems down below! + if ( cs->enemyNum < 0 ) { + return AIFunc_IdleStart( cs ); + } + // + if ( VectorLength( cs->takeCoverPos ) < 1 ) { + dist = 0; + } else { + VectorCopy( cs->takeCoverPos, destorg ); + VectorSubtract( destorg, cs->bs->origin, vec ); + vec[2] *= 0.2; + dist = VectorLength( vec ); + } + // + // look for things we should attack + // if we are out of ammo, we shouldn't bother trying to attack (and we should keep hiding) + ammo = cs->bs->cur_ps.ammo; + shouldAttack = qfalse; + numEnemies = AICast_ScanForEnemies( cs, enemies ); + if ( numEnemies == -1 ) { // query mode + return NULL; + } else if ( numEnemies == -2 ) { // inspection may be required + char *retval; + // TTimo: gcc: suggest () around assignment used as truth value + if ( ( retval = AIFunc_InspectFriendlyStart( cs, enemies[0] ) ) ) { + return retval; + } + } else if ( numEnemies == -3 ) { // bullet impact + if ( cs->aiState < AISTATE_COMBAT ) { + return AIFunc_InspectBulletImpactStart( cs ); + } + } else if ( numEnemies == -4 ) { // audible event + if ( cs->aiState < AISTATE_COMBAT ) { + return AIFunc_InspectAudibleEventStart( cs, cs->audibleEventEnt ); + } + } else if ( numEnemies > 0 ) { + + if ( AICast_GotEnoughAmmoForWeapon( cs, cs->weaponNum ) ) { + // default to the first known enemy, overwrite if we find a clearer shot + cs->enemyNum = enemies[0]; + // + for ( i = 0; i < numEnemies; i++ ) { + if ( AICast_CheckAttack( cs, enemies[i], qfalse ) || AICast_CheckAttack( AICast_GetCastState( enemies[i] ), cs->entityNum, qfalse ) || + AICast_EntityVisible( AICast_GetCastState( enemies[i] ), cs->entityNum, qtrue ) ) { + if ( ( cs->aiFlags & AIFL_WALKFORWARD ) || ( dist <= 12 ) ) { + // we are at our hidepos, abort! + cs->enemyNum = enemies[i]; + return AIFunc_BattleStart( cs ); + } else { + shouldAttack = qtrue; // fire at them as we go + } + } else if ( cs->enemyNum < 0 ) { + cs->lastEnemy = enemies[i]; + } + } + + } else { + AICast_ChooseWeapon( cs, qfalse ); + // + if ( dist <= 12 ) { + if ( !AICast_GotEnoughAmmoForWeapon( cs, cs->weaponNum ) ) { + // NO AMMO LEFT!! + // hide? + if ( AICast_GetTakeCoverPos( cs, cs->enemyNum, cs->vislist[cs->enemyNum].visible_pos, cs->takeCoverPos ) ) { + // go to a position that cannot be seen from the last place we saw the enemy, and wait there for some time + cs->takeCoverTime = level.time + 2000 + rand() % 3000; + //return AIFunc_BattleTakeCoverStart( cs ); + } + } + } + } + + } + // + //if (!shouldAttack) + // always do this check, if our destination sucks, abort it + { + // if the enemy can see our hide position, find a better spot + if ( AICast_VisibleFromPos( cs->vislist[cs->enemyNum].visible_pos, cs->enemyNum, cs->takeCoverPos, bs->entitynum, qfalse ) ) { + if ( !AICast_GetTakeCoverPos( cs, cs->enemyNum, cs->vislist[cs->enemyNum].visible_pos, cs->takeCoverPos ) ) { + // shit!! umm.. try and fire? + return AIFunc_BattleStart( cs ); + } else { // recalc distance + VectorCopy( cs->takeCoverPos, destorg ); + VectorSubtract( destorg, cs->bs->origin, vec ); + vec[2] *= 0.2; + dist = VectorLength( vec ); + } + } else if ( dist < 8 ) { + // if they can see us, find a better spot + if ( AICast_EntityVisible( AICast_GetCastState( cs->enemyNum ), cs->entityNum, qtrue ) || AICast_CheckAttack( AICast_GetCastState( cs->enemyNum ), cs->entityNum, qfalse ) ) { + if ( !AICast_GetTakeCoverPos( cs, cs->enemyNum, cs->vislist[cs->enemyNum].visible_pos, cs->takeCoverPos ) ) { + // shit!! umm.. try and fire? + return AIFunc_BattleStart( cs ); + } else { // recalc distance + VectorCopy( cs->takeCoverPos, destorg ); + VectorSubtract( destorg, cs->bs->origin, vec ); + vec[2] *= 0.2; + dist = VectorLength( vec ); + } + } + } + //cs->takeCoverTime = level.time + 1000; + } + // + // pretend we can still see them while we run to our hide pos, this way they are less likely + // to forget about their enemy once they get there +// DISABLED: doesn't work well with new AI system + //if (cs->enemyNum >= 0 && cs->vislist[cs->enemyNum].real_visible_timestamp && (cs->vislist[cs->enemyNum].real_visible_timestamp > level.time - 2000)) { + // AICast_UpdateVisibility( &g_entities[cs->entityNum], &g_entities[cs->enemyNum], qfalse, cs->vislist[cs->enemyNum].real_visible_timestamp == cs->vislist[cs->enemyNum].lastcheck_timestamp ); + //} + // + memset( &move, 0, sizeof( move ) ); + // + // are we close enough to the goal? + if ( VectorLength( cs->takeCoverPos ) > 1 && dist > 8 ) { + const float simTime = 1.5; + float enemyDist; + // + // we haven't reached it yet, make sure we at least wait there for a few seconds after arriving + cs->takeCoverTime = level.time + 2000 + rand() % 2000; + // + moveresult = AICast_MoveToPos( cs, destorg, -1 ); + if ( moveresult ) { + //if the movement failed + if ( moveresult->failure ) { + //reset the avoid reach, otherwise bot is stuck in current area + trap_BotResetAvoidReach( bs->ms ); + // couldn't get there, so stop trying to get there + VectorClear( cs->takeCoverPos ); + dist = 0; + } + // + if ( moveresult->blocked ) { + // abort the TakeCover + VectorClear( cs->takeCoverPos ); + dist = 0; + } + } + // + // if we are going to bump into something soon, abort it + AICast_PredictMovement( cs, 1, simTime, &move, &cs->lastucmd, -1 ); + enemyDist = Distance( cs->bs->origin, g_entities[cs->enemyNum].s.pos.trBase ); + VectorSubtract( move.endpos, cs->bs->origin, vec ); + moveDist = VectorNormalize( vec ); + // + if ( ( move.numtouch && move.touchents[0] < aicast_maxclients ) // hit something + // or moved closer to the enemy + || ( ( enemyDist < 128 ) + && ( ( enemyDist - 1 ) > ( Distance( move.endpos, g_entities[cs->enemyNum].s.pos.trBase ) ) ) ) ) { + // abort the manouver + VectorClear( cs->takeCoverPos ); + dist = 0; + } + // + // do we want to play a rolling animation into a cover position? + else if ( ( cs->aiFlags & AIFL_DIVE_ANIM && !client->ps.torsoTimer && cs->castScriptStatus.castScriptEventIndex < 0 && cs->lastRollMove < level.time - 800 && move.numtouch == 0 && ( moveDist > simTime * cs->attributes[RUNNING_SPEED] * 0.98 ) && move.groundEntityNum == ENTITYNUM_WORLD ) && + ( shouldAttack && !AICast_VisibleFromPos( g_entities[cs->enemyNum].s.pos.trBase, cs->enemyNum, move.endpos, cs->entityNum, qfalse ) ) ) { + VectorClear( cs->takeCoverPos ); // stay there when done rolling + return AIFunc_BattleDiveStart( cs, vec ); + } + // + // we should slow down on approaching the destination point + else if ( dist < 64 ) { + cs->speedScale = AICast_SpeedScaleForDistance( cs, dist, 0 ); + } + // + // if they cant see us, then stay here + if ( !( cs->aiFlags & AIFL_MISCFLAG1 ) && !AICast_VisibleFromPos( cs->vislist[cs->enemyNum].real_visible_pos, cs->enemyNum, move.endpos, cs->entityNum, qfalse ) + && !AICast_VisibleFromPos( cs->vislist[cs->enemyNum].real_visible_pos, cs->enemyNum, cs->bs->origin, cs->entityNum, qfalse ) + && trap_AAS_PointAreaNum( move.endpos ) ) { // make sure the endpos is in a valid area + VectorCopy( move.endpos, cs->takeCoverPos ); + dist = 0; + cs->aiFlags |= AIFL_MISCFLAG1; // dont do this again + } + // + if ( cs->aiFlags & AIFL_FLIP_ANIM && cs->lastRollMove < level.time - 800 && !client->ps.torsoTimer && cs->castScriptStatus.castScriptEventIndex < 0 && move.numtouch == 0 && moveDist > simTime * cs->attributes[RUNNING_SPEED] * 0.9 && move.groundEntityNum == ENTITYNUM_WORLD && cs->attackcrouch_time < level.time ) { + int destarea, simarea, starttravel, simtravel; + // if we'll be closer after the move, proceed + destarea = BotPointAreaNum( destorg ); + simarea = BotPointAreaNum( move.endpos ); + starttravel = trap_AAS_AreaTravelTimeToGoalArea( cs->bs->areanum, cs->bs->origin, destarea, cs->travelflags ); + simtravel = trap_AAS_AreaTravelTimeToGoalArea( simarea, move.endpos, destarea, cs->travelflags ); + if ( simtravel < starttravel ) { + return AIFunc_FlipMoveStart( cs, vec ); + } + } + // set crouching status + //if (dist && (cs->thinkFuncChangeTime < level.time - 2000) && (cs->crouchHideFlag || cs->aiFlags & AIFL_ATTACK_CROUCH)) { + if ( cs->crouchHideFlag || ( ( cs->thinkFuncChangeTime < level.time - 2000 ) && ( cs->aiFlags & AIFL_ATTACK_CROUCH ) ) ) { + cs->attackcrouch_time = level.time + 1000; + } + + } else { + // + // have we been Taking Cover for enough time? + if ( level.time > cs->takeCoverTime ) { + return AIFunc_DefaultStart( cs ); + } + // + // check for a movement we should be making + if ( cs->obstructingTime > level.time ) { + VectorClear( cs->takeCoverPos ); + AICast_MoveToPos( cs, cs->obstructingPos, -1 ); + } + // if we have some enemies that we can attack immediately (without going anywhere to chase them) + if ( shouldAttack ) { + return AIFunc_BattleStart( cs ); + } + // if we have some enemies in sight, but they can't attack us, flee if possible, otherwise if we are not afraid, go attack them + else if ( numEnemies ) { + + // are they reloading? if so we should attack! + if ( g_entities[cs->entityNum].client->ps.weaponDelay < 100 + && g_entities[cs->enemyNum].client->ps.weaponDelay > 1100 ) { + if ( AICast_GotEnoughAmmoForWeapon( cs, cs->weaponNum ) && AICast_WeaponUsable( cs, cs->weaponNum ) ) { + return AIFunc_BattleStart( cs ); + } + } + + // we can't hit them and they cant hit us, so dont bother doing anything + + //if (!AICast_GetTakeCoverPos( cs, cs->enemyNum, cs->vislist[cs->enemyNum].visible_pos, cs->takeCoverPos )) { + //if (!AICast_WantsToTakeCover(cs, qfalse)) + //return AIFunc_BattleStart( cs ); + //} + } + // do we need to go to our leader? + else if ( cs->leaderNum >= 0 && Distance( cs->bs->origin, g_entities[cs->leaderNum].r.currentOrigin ) > MAX_LEADER_DIST ) { + // wait until we've been hiding for long enough + if ( level.time > cs->takeCoverTime ) { + return AIFunc_ChaseGoalStart( cs, cs->leaderNum, AICAST_LEADERDIST_MAX, qtrue ); + } + } + + // else, crouch while we hide + if ( cs->attributes[ATTACK_CROUCH] > 0.1 || cs->crouchHideFlag ) { + cs->attackcrouch_time = level.time + 1000; + } + } + // + // if we should be attacking something on our way + if ( shouldAttack ) { + vec3_t vec, dir; + float dist; + // + // if they are close, and we're heading for them, we should abort this manouver + VectorSubtract( g_entities[cs->enemyNum].client->ps.origin, bs->origin, vec ); + if ( ( dist = VectorNormalize( vec ) ) < 256 ) { + VectorCopy( bs->velocity, vec ); + vec[2] = 0; + if ( VectorNormalize2( vec, dir ) > 20 ) { // we are moving + if ( DotProduct( dir, vec ) > 0.4 ) { + // abort + return AIFunc_BattleStart( cs ); + } + } + } + // + // if the enemy can see our hide position, abort the manouver + if ( ( cs->thinkFuncChangeTime < level.time - 1000 ) && ( AICast_VisibleFromPos( g_entities[cs->enemyNum].client->ps.origin, cs->enemyNum, cs->takeCoverPos, bs->entitynum, qfalse ) ) ) { + // abort + return AIFunc_BattleStart( cs ); + } + // + // if we are tactical and can crouch, do so + if ( !move.numtouch && ( cs->thinkFuncChangeTime < level.time - 2000 ) && ( dist > 128 ) && cs->attributes[TACTICAL] > 0.4 && cs->attributes[ATTACK_CROUCH] > 0.1 && + ( cs->attackcrouch_time >= level.time ) ) { + cs->attackcrouch_time = level.time + 1000; + } + // + //attack the enemy if possible + AICast_ProcessAttack( cs ); + // + } else /*if (dist < 48)*/ { + // if we've recently been in a fight, look towards the enemy + if ( cs->enemyNum >= 0 ) { + AICast_AimAtEnemy( cs ); + } else if ( cs->lastEnemy >= 0 ) { + // if we are not moving, face them + if ( VectorLength( cs->bs->cur_ps.velocity ) < 50 ) { + vec3_t dir; + // + VectorSubtract( cs->vislist[cs->lastEnemy].visible_pos, cs->bs->origin, dir ); + VectorNormalize( dir ); + vectoangles( dir, cs->ideal_viewangles ); + } + } else if ( !cs->crouchHideFlag ) { // no enemy, and no need to crouch, so stop crouching + //if (cs->attackcrouch_time > level.time + 1000) { + // cs->attackcrouch_time = level.time + 1000; + //} + } + // reload? + AICast_IdleReload( cs ); + } + + return NULL; +} + +/* +============ +AIFunc_BattleTakeCoverStart() +============ +*/ +char *AIFunc_BattleTakeCoverStart( cast_state_t *cs ) { +// debugging +#ifdef DEBUG +// if (cs->attributes[AGGRESSION] >= 1.0) +// AICast_Printf( 0, "AI taking cover with full aggression!\n" ); +#endif + + if ( !AICast_CanMoveWhileFiringWeapon( cs->weaponNum ) ) { + // always run to the cover point + cs->attackcrouch_time = 0; + cs->aiFlags &= ~AIFL_ATTACK_CROUCH; + } else { + // if we arent crouching, start crouching soon after we start retreating + if ( cs->attributes[ATTACK_CROUCH] > 0.1 ) { + cs->aiFlags |= AIFL_ATTACK_CROUCH; + } else { + cs->aiFlags &= ~AIFL_ATTACK_CROUCH; + } + cs->attackcrouch_time = 0; + } + + // miscflag1 used to set predicted point as our goal, so we dont keep setting this over and over + cs->aiFlags &= ~AIFL_MISCFLAG1; + + cs->aifunc = AIFunc_BattleTakeCover; + return "AIFunc_BattleTakeCover"; +} + +/* +============ +AIFunc_GrenadeFlush() +============ +*/ +char *AIFunc_GrenadeFlush( cast_state_t *cs ) { + vec3_t dir; + gentity_t *followent, *grenade, *ent; + bot_state_t *bs; + vec3_t destorg, endPos; + qboolean moved = qfalse; + int hitclient; + //qboolean attacked = qfalse; // TTimo: unused + float dist, oldyaw; + int grenadeType; + int *ammo; + + bs = cs->bs; + ent = &g_entities[cs->entityNum]; + // + // if we are throwing the grenade, keep holding down fire + + ammo = cs->bs->cur_ps.ammo; + if ( AICast_GotEnoughAmmoForWeapon( cs, WP_GRENADE_LAUNCHER ) ) { + grenadeType = WP_GRENADE_LAUNCHER; + } else if ( AICast_GotEnoughAmmoForWeapon( cs, WP_GRENADE_PINEAPPLE ) ) { + grenadeType = WP_GRENADE_PINEAPPLE; + } else { // not enough ammo, abort + return AIFunc_DefaultStart( cs ); + } + + // (SA) probably read the fweapon from t + if ( cs->grenadeFlushFiring ) { + // are we still moving? + if ( VectorLength( cs->bs->cur_ps.velocity ) ) { + // keep waiting + // pause for a bit, so the grenade comes out correctly + cs->lockViewAnglesTime = level.time + 1200; + if ( cs->castScriptStatus.scriptNoMoveTime < level.time + 1200 ) { + cs->castScriptStatus.scriptNoMoveTime = level.time + 1200; + } + return NULL; + } + if ( cs->weaponFireTimes[cs->grenadeFlushFiring] < cs->thinkFuncChangeTime ) { + // have we switched weapons? + if ( cs->weaponNum != cs->grenadeFlushFiring ) { + // damn + hitclient = -1; + } else { + // keep checking it's ok + CalcMuzzlePoints( ent, grenadeType ); + // fire a dummy grenade + grenade = weapon_grenadelauncher_fire( ent, WP_GRENADE_LAUNCHER ); + // check to see what will happen + hitclient = AICast_SafeMissileFire( grenade, grenade->nextthink - level.time, cs->enemyNum, destorg, cs->entityNum, endPos ); + // kill the grenade + G_FreeEntity( grenade ); + if ( hitclient != 1 ) { // it wont hit them, abort + hitclient = -1; // a miss is as bad as a friendly kill + } + } + if ( hitclient == -1 ) { // doh + //G_Printf("aborted grenade\n"); + cs->castScriptStatus.scriptNoMoveTime = 0; + cs->lockViewAnglesTime = 0; + AICast_ChooseWeapon( cs, qfalse ); + return AIFunc_DefaultStart( cs ); + } + if ( !cs->bs->cur_ps.grenadeTimeLeft ) { + // hold fire button down + trap_EA_Attack( bs->client ); + cs->bFlags |= BFL_ATTACKED; + } + cs->lockViewAnglesTime = level.time + 500; + return NULL; + } + // the grenade/pineapple has been released! + cs->lockViewAnglesTime = -1; + cs->startGrenadeFlushTime = level.time + 2000 + rand() % 2000; // dont throw one again for a bit + return AIFunc_DefaultStart( cs ); + } + // + // do we need to avoid a danger? + if ( cs->dangerEntityValidTime >= level.time ) { + if ( !AICast_GetTakeCoverPos( cs, cs->dangerEntity, cs->dangerEntityPos, cs->takeCoverPos ) ) { + // shit?? + } + // go to a position that cannot be seen from the dangerPos + cs->takeCoverTime = cs->dangerEntityValidTime + 1000; + cs->attackcrouch_time = 0; + return AIFunc_AvoidDangerStart( cs ); + } + // + // are we waiting for a door? + if ( cs->doorMarkerTime > level.time - 100 ) { + return AIFunc_DoorMarkerStart( cs, cs->doorMarkerDoor, cs->doorMarkerNum ); + } + // + if ( cs->weaponNum && ( cs->castScriptStatus.scriptFlags & SFL_NOCHANGEWEAPON ) ) { + return AIFunc_IdleStart( cs ); + } + // + if ( cs->enemyNum < 0 ) { + return AIFunc_IdleStart( cs ); + } + // + // if we have started a script, abort the grenade flush + if ( cs->castScriptStatus.castScriptEventIndex >= 0 ) { + return AIFunc_IdleStart( cs ); + } + // trying for too long? + if ( cs->startGrenadeFlushTime < level.time - 4000 ) { + cs->startGrenadeFlushTime = level.time; + return AIFunc_IdleStart( cs ); + } + // + followent = &g_entities[cs->enemyNum]; + // + // if the entity is not ready yet + if ( !followent->inuse ) { + // if it's a connecting client, wait + if ( !( ( cs->enemyNum < MAX_CLIENTS ) + && ( ( followent->client && followent->client->pers.connected == CON_CONNECTING ) + || ( level.time < 3000 ) ) ) ) { // they don't exist anymore, stop attacking + cs->enemyNum = -1; + } + return AIFunc_IdleStart( cs ); + } + // + // if we can see them, go back to an attack state after some time + if ( AICast_CheckAttack( cs, cs->enemyNum, qfalse ) + && cs->obstructingTime < level.time ) { // give us some time to throw the grenade, otherwise go back to attack state + //if ((cs->grenadeFlushEndTime > 0 && cs->grenadeFlushEndTime < level.time)) { + //G_Printf("aborting, enemy is attackable\n"); + return AIFunc_BattleStart( cs ); + //} else if (cs->grenadeFlushEndTime < 0) { + // cs->grenadeFlushEndTime = level.time + 1500; + //} + //attack the enemy if possible + //AICast_ProcessAttack( cs ); + //attacked = qtrue; + } else { + // not visible, go to their previously visible position + if ( !cs->vislist[cs->enemyNum].visible_timestamp || Distance( bs->origin, cs->vislist[cs->enemyNum].real_visible_pos ) < 16 ) { + // we're done attacking, go back to default state, which in turn will recall previous state + // + // face the direction they currently are from this position (bit of a hack, but it looks best) + VectorSubtract( g_entities[cs->enemyNum].client->ps.origin, cs->vislist[cs->enemyNum].visible_pos, dir ); + vectoangles( dir, cs->ideal_viewangles ); + // + //G_Printf("aborting, reached visible pos\n"); + return AIFunc_DefaultStart( cs ); + } + } + + // is there someone else we can go for instead? + numEnemies = AICast_ScanForEnemies( cs, enemies ); + if ( numEnemies == -1 ) { // query mode + return NULL; + } else if ( numEnemies == -2 ) { // inspection may be required + char *retval; + // TTimo: gcc: suggest () around assignment used as truth value + if ( ( retval = AIFunc_InspectFriendlyStart( cs, enemies[0] ) ) ) { + return retval; + } + } else if ( !( cs->bFlags & BFL_ATTACKED ) && numEnemies > 0 ) { + int i; + for ( i = 0; i < numEnemies; i++ ) { + if ( enemies[i] != cs->enemyNum && AICast_CheckAttack( cs, enemies[i], qfalse ) ) { + cs->enemyNum = enemies[i]; + //G_Printf("aborting, other enemy\n"); + return AIFunc_BattleStart( cs ); + } + } + } + + if ( followent->client ) { // go to the last visible position + VectorCopy( cs->vislist[cs->enemyNum].visible_pos, destorg ); + } else // assume we know where other entities are + { + VectorCopy( followent->s.pos.trBase, destorg ); + } + // + dist = VectorDistance( destorg, cs->bs->origin ); + // + if ( cs->vislist[cs->enemyNum].lastcheck_timestamp > cs->vislist[cs->enemyNum].real_visible_timestamp || + dist > 128 ) { + // + // go to them + // + if ( followent->client && followent->health <= 0 ) { + cs->enemyNum = -1; + //G_Printf("aborting, enemy dead\n"); + return AIFunc_DefaultStart( cs ); + } + // + // ........................................................... + // Do the movement.. + // + // move straight to them if we can + if ( ( cs->leaderNum < 0 ) && + ( cs->bs->cur_ps.groundEntityNum != ENTITYNUM_NONE || g_entities[cs->entityNum].waterlevel > 1 ) ) { + aicast_predictmove_t move; + vec3_t dir; + bot_input_t bi; + usercmd_t ucmd; + trace_t tr; + + // trace will eliminate most unsuccessful paths + trap_Trace( &tr, cs->bs->origin, g_entities[cs->entityNum].r.mins, g_entities[cs->entityNum].r.maxs, followent->r.currentOrigin, cs->entityNum, g_entities[cs->entityNum].clipmask ); + if ( tr.entityNum == followent->s.number ) { + // try walking straight to them + VectorSubtract( followent->r.currentOrigin, cs->bs->origin, dir ); + VectorNormalize( dir ); + if ( !ent->waterlevel ) { + dir[2] = 0; + } + //trap_EA_Move(cs->entityNum, dir, 400); + trap_EA_GetInput( cs->entityNum, (float) level.time / 1000, &bi ); + VectorCopy( dir, bi.dir ); + bi.speed = 400; + bi.actionflags = 0; + AICast_InputToUserCommand( cs, &bi, &ucmd, bs->cur_ps.delta_angles ); + AICast_PredictMovement( cs, 5, 2.0, &move, &ucmd, cs->enemyNum ); + + if ( move.stopevent == PREDICTSTOP_HITENT ) { // success! + trap_EA_Move( cs->entityNum, dir, 400 ); + vectoangles( dir, cs->ideal_viewangles ); + cs->ideal_viewangles[2] *= 0.5; + moved = qtrue; + } else { // clear movement + //trap_EA_Move(cs->entityNum, dir, 0); + } + } + } + // just go to them + if ( !moved ) { + moveresult = AICast_MoveToPos( cs, destorg, cs->enemyNum ); + if ( moveresult && moveresult->failure ) { // no path, so go back to idle behaviour + cs->enemyNum = -1; + //G_Printf("aborting, movement failure\n"); + return AIFunc_DefaultStart( cs ); + } else { + moved = qtrue; + } + } + } + // + // ........................................................... + // if we throw a grenade from here, will it get their last visible position? + // + CalcMuzzlePoints( ent, grenadeType ); + // fire a dummy grenade + grenade = weapon_grenadelauncher_fire( ent, WP_GRENADE_LAUNCHER ); + // check to see what will happen + hitclient = AICast_SafeMissileFire( grenade, grenade->nextthink - level.time, cs->enemyNum, destorg, cs->entityNum, endPos ); + // kill the grenade + G_FreeEntity( grenade ); + //if (!attacked) + // cs->weaponNum = grenadeType; // select grenade launcher + // set our angles for the next frame + oldyaw = cs->ideal_viewangles[YAW]; + AICast_AimAtEnemy( cs ); + // if we can't see them, keep facing our movement dir, but use the pitch information + if ( !AICast_EntityVisible( cs, cs->enemyNum, qtrue ) ) { + cs->ideal_viewangles[YAW] = oldyaw; + } + + if ( hitclient == 1 ) { + // it will hit their last visible position + // give us some time to aim and adjust + if ( cs->thinkFuncChangeTime < level.time - 200 ) { + cs->bFlags |= BFL_ATTACKED; + cs->weaponNum = grenadeType; // select grenade launcher + cs->grenadeFlushFiring = cs->weaponNum; + // pause for a bit, so the grenade comes out correctly + cs->lockViewAnglesTime = level.time + 1200; + if ( cs->castScriptStatus.scriptNoMoveTime < level.time + 1200 ) { + cs->castScriptStatus.scriptNoMoveTime = level.time + 1200; + } + return NULL; + } + } else if ( hitclient == -1 ) { + // hit a friendly + cs->startGrenadeFlushTime = level.time + 3000; // don't try again for a while + //G_Printf("aborting, too dangerous\n"); + return AIFunc_DefaultStart( cs ); + } else if ( hitclient == -2 ) { + // went too far, so angle down a bit + cs->ideal_viewangles[PITCH] += 15 * random(); + } else { + if ( cs->thinkFuncChangeTime < level.time - 200 ) { + // if it went reasonably close to them, but safe from us, then fire away + if ( Distance( endPos, cs->bs->origin ) > 100 + Distance( endPos, g_entities[cs->enemyNum].r.currentOrigin ) ) { + trap_EA_Attack( bs->client ); + cs->bFlags |= BFL_ATTACKED; + cs->weaponNum = grenadeType; // select grenade launcher + cs->grenadeFlushFiring = cs->weaponNum; + // pause for a bit, so the grenade comes out correctly + cs->lockViewAnglesTime = level.time + 1200; + if ( cs->castScriptStatus.scriptNoMoveTime < level.time + 1200 ) { + cs->castScriptStatus.scriptNoMoveTime = level.time + 1200; + } + return NULL; + } + } + cs->ideal_viewangles[PITCH] += -10 * random(); + } + // + return NULL; +} + +/* +============ +AIFunc_GrenadeFlushStart() +============ +*/ +char *AIFunc_GrenadeFlushStart( cast_state_t *cs ) { + lastGrenadeFlush = level.time; // + rand()%2000; + cs->startGrenadeFlushTime = level.time; + cs->grenadeFlushEndTime = -1; + cs->lockViewAnglesTime = 0; + cs->combatGoalTime = 0; + cs->grenadeFlushFiring = qfalse; + // don't wait too long before taking cover, if we just aborted one + if ( cs->takeCoverTime > level.time + 1000 ) { + cs->takeCoverTime = level.time + 500 + rand() % 500; + } + // + cs->aifunc = AIFunc_GrenadeFlush; + return "AIFunc_GrenadeFlush"; +} + +/* +============ +AIFunc_BattleMG42() +============ +*/ +char *AIFunc_BattleMG42( cast_state_t *cs ) { + bot_state_t *bs; + gentity_t *mg42, *ent; + vec3_t angles, vec, bestangles; + qboolean unmount = qfalse; + + mg42 = &g_entities[cs->mountedEntity]; + ent = &g_entities[cs->entityNum]; + bs = cs->bs; + + // have we dismounted the MG42? + if ( !ent->active ) { + return AIFunc_DefaultStart( cs ); + } + + // are we waiting to dismount + if ( cs->aiFlags & AIFL_DISMOUNTING ) { + // face straight forward + VectorCopy( mg42->s.angles, cs->ideal_viewangles ); + // only dismount when facing forwards + if ( fabs( AngleDifference( mg42->s.angles[YAW], cs->viewangles[YAW] ) ) < 10 ) { + // try and unmount + Cmd_Activate_f( ent ); + } + return NULL; + } + +/* RF, disabled this since the gun will unmount them when it is destroyed + // if the mg42 is dead, dismount + if (mg42->health <= 0) { + cs->aiFlags |= AIFL_DISMOUNTING; + AICast_ScriptEvent( cs, "forced_mg42_unmount", "destroyed" ); + return NULL; + } +*/ + // if enemy is dead, stop attacking them + if ( g_entities[cs->enemyNum].health <= 0 ) { + cs->enemyNum = -1; + } + + //if no enemy, or our current enemy isn't attackable, look for a better enemy + if ( cs->enemyNum >= 0 ) { + if ( cs->vislist[cs->enemyNum].real_visible_timestamp && cs->vislist[cs->enemyNum].real_visible_timestamp > ( level.time - 5000 ) ) { + VectorSubtract( cs->vislist[cs->enemyNum].real_visible_pos, mg42->r.currentOrigin, vec ); + } else if ( cs->vislist[cs->enemyNum].visible_timestamp && cs->vislist[cs->enemyNum].visible_timestamp > ( level.time - 5000 ) ) { + VectorSubtract( cs->vislist[cs->enemyNum].visible_pos, mg42->r.currentOrigin, vec ); + } else { // just aim straight forward + AngleVectors( mg42->s.angles, vec, NULL, NULL ); + } + + VectorNormalize( vec ); + vectoangles( vec, angles ); + angles[PITCH] = AngleNormalize180( angles[PITCH] ); + VectorCopy( angles, bestangles ); + } + + // check for enemy outside harc + if ( cs->enemyNum < 0 || + !AICast_CheckAttack( cs, cs->enemyNum, qfalse ) || + ( fabs( AngleDifference( angles[YAW], mg42->s.angles[YAW] ) ) > mg42->harc ) || + ( angles[PITCH] < 0 && angles[PITCH] + 5 < -mg42->varc ) || + ( angles[PITCH] > 0 && angles[PITCH] - 5 > 5.0 ) ) { + qboolean shouldAttack; + + // look for a better enemy + numEnemies = AICast_ScanForEnemies( cs, enemies ); + shouldAttack = qfalse; + if ( numEnemies > 0 ) { + int i; + // default to the first known enemy, overwrite if we find a clearer shot + cs->enemyNum = enemies[0]; + // + // unmount unless we find an enemy within harc + unmount = qtrue; + // + for ( i = 0; i < numEnemies; i++ ) { + /* + if (!cs->vislist[enemies[i]].real_visible_timestamp || + (cs->vislist[enemies[i]].real_visible_timestamp < level.time - 2000)) { + // we can't see them, ignore them + continue; + } + */ + + // if they are in the range + if ( cs->vislist[enemies[i]].real_visible_timestamp > ( level.time - 5000 ) ) { + VectorSubtract( cs->vislist[enemies[i]].real_visible_pos, mg42->r.currentOrigin, vec ); + } else { + VectorSubtract( cs->vislist[enemies[i]].visible_pos, mg42->r.currentOrigin, vec ); + } + VectorNormalize( vec ); + vectoangles( vec, angles ); + angles[PITCH] = AngleNormalize180( angles[PITCH] ); + if ( !( ( fabs( AngleDifference( angles[YAW], mg42->s.angles[YAW] ) ) > mg42->harc ) || + ( angles[YAW] < 0 && angles[YAW] + 2 < -mg42->varc ) || + ( angles[YAW] > 0 && angles[YAW] - 2 > 5.0 ) ) ) { + // + // found someone inside harc, so dont unmount + unmount = qfalse; + // + if ( AICast_CheckAttack( cs, enemies[i], qfalse ) ) { + VectorCopy( angles, bestangles ); + cs->enemyNum = enemies[i]; + shouldAttack = qtrue; + break; + } else if ( AICast_CheckAttack( cs, enemies[i], qtrue ) ) { + // keep firing at anything behind solids, in case they find a position where they can shoot us, but our checkattack() doesn't find a clear shot + VectorCopy( angles, bestangles ); + cs->enemyNum = enemies[i]; + shouldAttack = qtrue; + } + } + } + } + + if ( !shouldAttack ) { + // keep firing at anything behind solids, in case they find a position where they can shoot us, but our checkattack() doesn't find a clear shot + if ( cs->enemyNum < 0 || !AICast_CheckAttack( cs, cs->enemyNum, qtrue ) || + ( !cs->vislist[cs->enemyNum].real_visible_timestamp || + ( cs->vislist[cs->enemyNum].real_visible_timestamp < level.time - 2000 ) ) ) { + // face straight forward + cs->ideal_viewangles[PITCH] = 0; + return NULL; + } + } + + // if we had possible enemies, but couldnt find one to attack, then dismount now + if ( unmount ) { + AICast_ScriptEvent( cs, "forced_mg42_unmount", NULL ); + if ( !( cs->aiFlags & AIFL_DENYACTION ) ) { + cs->aiFlags |= AIFL_DISMOUNTING; + return NULL; + } + } + } + // + // hold down fire, and track them + // + // TODO: play a special "holding mg42" torso animation + // + VectorCopy( angles, cs->ideal_viewangles ); + if ( cs->triggerReleaseTime < level.time ) { + trap_EA_Attack( bs->client ); + cs->bFlags |= BFL_ATTACKED; + + if ( cs->triggerReleaseTime < level.time - 3000 ) { + cs->triggerReleaseTime = level.time + 700 + rand() % 700; + } + } + // + return NULL; +} + +/* +============ +AIFunc_BattleMG42Start() +============ +*/ +char *AIFunc_BattleMG42Start( cast_state_t *cs ) { + cs->aiFlags &= ~AIFL_DISMOUNTING; + // + cs->aifunc = AIFunc_BattleMG42; + return "AIFunc_BattleMG42"; +} + +/* +============ +AIFunc_InspectBody() + + go up to the enemy, and have a good look at them, randomly taunt them +============ +*/ +char *AIFunc_InspectBody( cast_state_t *cs ) { + bot_state_t *bs; + vec3_t destorg, enemyOrg; + // + // stop crouching + cs->attackcrouch_time = 0; + // + // do we need to avoid a danger? + if ( cs->dangerEntityValidTime >= level.time ) { + if ( !AICast_GetTakeCoverPos( cs, cs->dangerEntity, cs->dangerEntityPos, cs->takeCoverPos ) ) { + // shit?? + } + // go to a position that cannot be seen from the dangerPos + cs->takeCoverTime = cs->dangerEntityValidTime + 1000; + cs->attackcrouch_time = 0; + return AIFunc_AvoidDangerStart( cs ); + } + // + // are we waiting for a door? + if ( cs->doorMarkerTime > level.time - 100 ) { + return AIFunc_DoorMarkerStart( cs, cs->doorMarkerDoor, cs->doorMarkerNum ); + } + // + // if running a script + if ( cs->castScriptStatus.castScriptEventIndex >= 0 ) { + cs->enemyNum = -1; + return AIFunc_IdleStart( cs ); + } + // + bs = cs->bs; + // + if ( cs->enemyNum < 0 ) { + return AIFunc_IdleStart( cs ); + } + // + // look for things we should attack + numEnemies = AICast_ScanForEnemies( cs, enemies ); + if ( numEnemies == -1 ) { // query mode + return NULL; + } else if ( numEnemies == -2 ) { // inspection may be required + char *retval; + // TTimo: gcc: suggest () around assignment used as truth value + if ( ( retval = AIFunc_InspectFriendlyStart( cs, enemies[0] ) ) ) { + return retval; + } + } else if ( numEnemies == -3 ) { // bullet impact + if ( cs->aiState < AISTATE_COMBAT ) { + return AIFunc_InspectBulletImpactStart( cs ); + } + } else if ( numEnemies == -4 ) { // audible event + if ( cs->aiState < AISTATE_COMBAT ) { + return AIFunc_InspectAudibleEventStart( cs, cs->audibleEventEnt ); + } + } else if ( numEnemies > 0 ) { + cs->enemyNum = enemies[0]; // just attack the first one + return AIFunc_BattleStart( cs ); + } + // + VectorCopy( cs->vislist[cs->enemyNum].visible_pos, enemyOrg ); + if ( ( cs->inspectBodyTime < 0 ) && ( Distance( cs->bs->origin, enemyOrg ) > 64 ) ) { + // if they were gibbed, don't go all the way + if ( g_entities[cs->enemyNum].health < GIB_HEALTH && ( Distance( cs->bs->origin, enemyOrg ) < 180 ) ) { + cs->inspectBodyTime = level.time + 1000 + rand() % 1000; + trap_EA_Gesture( cs->entityNum ); + G_AddEvent( &g_entities[cs->entityNum], EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[cs->aiCharacter].soundScripts[ORDERSSOUNDSCRIPT] ) ); + } + // walk to them + if ( cs->movestate != MS_CROUCH ) { + cs->movestate = MS_WALK; + } + cs->movestateType = MSTYPE_TEMPORARY; + // + moveresult = AICast_MoveToPos( cs, enemyOrg, -1 ); + //if the movement failed + if ( moveresult && ( moveresult->failure || moveresult->blocked ) ) { + //reset the avoid reach, otherwise bot is stuck in current area + trap_BotResetAvoidReach( bs->ms ); + // couldn't get there, so stop trying to get there + cs->enemyNum = -1; + return AIFunc_IdleStart( cs ); + } + if ( Distance( cs->bs->origin, enemyOrg ) < 180 ) { + // look down at them + VectorSubtract( enemyOrg, cs->bs->origin, destorg ); + destorg[2] -= 20; + VectorNormalize( destorg ); + vectoangles( destorg, cs->ideal_viewangles ); + } + } else if ( cs->inspectBodyTime < 0 ) { + // just reached them + cs->inspectBodyTime = level.time + 1000 + rand() % 1000; + trap_EA_Gesture( cs->entityNum ); + G_AddEvent( &g_entities[cs->entityNum], EV_GENERAL_SOUND, G_SoundIndex( aiDefaults[cs->aiCharacter].soundScripts[ORDERSSOUNDSCRIPT] ) ); + } else if ( cs->inspectBodyTime < level.time ) { + vec3_t vec; + VectorSubtract( cs->startOrigin, cs->bs->origin, vec ); + vec[2] = 0; + // ready to go back to start position + if ( VectorLength( vec ) > 64 ) { + if ( cs->movestate != MS_CROUCH ) { + cs->movestate = MS_WALK; + } + cs->movestateType = MSTYPE_TEMPORARY; + moveresult = AICast_MoveToPos( cs, cs->startOrigin, -1 ); + //if the movement failed + if ( moveresult && ( moveresult->failure || moveresult->blocked ) ) { + //reset the avoid reach, otherwise bot is stuck in current area + trap_BotResetAvoidReach( bs->ms ); + // couldn't get there, so stop trying to get there + cs->enemyNum = -1; + return AIFunc_IdleStart( cs ); + } + // stay looking at them for a bit after starting to walk back + if ( cs->inspectBodyTime + 750 > level.time ) { + // look down at them + VectorSubtract( enemyOrg, cs->bs->origin, destorg ); + destorg[2] -= 20; + VectorNormalize( destorg ); + vectoangles( destorg, cs->ideal_viewangles ); + } + } else { + cs->attackSNDtime = level.time; + cs->enemyNum = -1; + return AIFunc_IdleStart( cs ); + } + } + // + return NULL; +} + +/* +============ +AIFunc_InspectBodyStart() +============ +*/ +char *AIFunc_InspectBodyStart( cast_state_t *cs ) { + static int lastInspect; + // + // if an inspection was already started not long ago, forget it + if ( lastInspect <= level.time && lastInspect > level.time - 1000 ) { + cs->inspectBodyTime = 1; // go back to start position + } else { + lastInspect = level.time; + cs->inspectBodyTime = -1; + } + cs->aifunc = AIFunc_InspectBody; + return "AIFunc_InspectBody"; +} + +/* +============ +AIFunc_GrenadeKick() +============ +*/ +char *AIFunc_GrenadeKick( cast_state_t *cs ) { + bot_state_t *bs; + vec3_t destorg, vec; + float dist, speed; + int enemies[MAX_CLIENTS], numEnemies, i; + qboolean shouldAttack; + int *ammo; + gentity_t *danger; + gentity_t *ent; + vec3_t end; + trace_t tr; + vec3_t dir; + int weapon; + + // !!! NOTE: the only way control should pass out of here, is by calling AIFunc_DefaultStart() + + ent = &g_entities[cs->entityNum]; + danger = &g_entities[cs->dangerEntity]; + + weapon = cs->grenadeKickWeapon; + + // just to be sure, give us the grenade launcher + //ent->client->ps.weapons |= (1 << weapon); + + // + // NOTE: ignore all danger, since we are trying to solve the situation anyway + // + // we need to move towards it + bs = cs->bs; + // + // are we throwing it back? + if ( cs->grenadeFlushFiring ) { + // wait until the animation is done + if ( !ent->client->ps.legsTimer ) { + return AIFunc_DefaultStart( cs ); + } + // wait till its finished + return NULL; + /* + cs->weaponNum = weapon; // select grenade launcher + // + if (cs->weaponFireTimes[weapon] < cs->thinkFuncChangeTime) { + if (!cs->bs->cur_ps.grenadeTimeLeft) { + // hold fire button down + AICast_AimAtEnemy( cs ); + trap_EA_Attack(bs->client); + cs->bFlags |= BFL_ATTACKED; + } + // + return NULL; + } + // the grenade has been released! + // + // modify the explode time + g_entities[ent->grenadeFired].nextthink = ent->grenadeExplodeTime; + if (g_entities[ent->grenadeFired].nextthink < level.time + 200) { // cut them some slack + g_entities[ent->grenadeFired].nextthink = level.time + 200 + rand()%500; + } + // make sure no-one tries to throw this back again (hot potatoe syndrome) + g_entities[ent->grenadeFired].flags |= FL_AI_GRENADE_KICK; + // + cs->grenadeFlushFiring = qfalse; + cs->lockViewAnglesTime = -1; + cs->startGrenadeFlushTime = level.time + 2000 + rand()%2000; // dont throw one again for a bit + level.lastGrenadeKick = level.time; + return AIFunc_DefaultStart( cs ); + */ + } + // +/* + // have we caught the grenade? + if (!(ent->flags & FL_AI_GRENADE_KICK)) { + // select grenades + cs->weaponNum = weapon; // select grenade launcher + AICast_AimAtEnemy( cs ); + // hold fire + trap_EA_Attack(bs->client); + cs->bFlags |= BFL_ATTACKED; + cs->grenadeFlushFiring = qtrue; + // + return NULL; + } +*/ + // + // is it about to explode in our face? + if ( level.time > danger->nextthink - (int)( 2.0 * VectorDistance( cs->bs->origin, danger->r.currentOrigin ) ) ) { + // abort!! + if ( !AICast_GetTakeCoverPos( cs, cs->dangerEntity, cs->dangerEntityPos, cs->takeCoverPos ) ) { + // shit?? + } + // go to a position that cannot be seen from the dangerPos + cs->takeCoverTime = danger->nextthink + 1000; + cs->attackcrouch_time = 0; + level.lastGrenadeKick = level.time; + return AIFunc_AvoidDangerStart( cs ); + } + // + /* + // are we close enough to start crouching? + if (danger->s.pos.trDelta[2] < 40 && VectorDistance( danger->r.currentOrigin, cs->bs->origin ) < 48 && (danger->r.currentOrigin[2] < cs->bs->origin[2]) && + VectorLength(danger->s.pos.trDelta) < 40) { + // crouch to pick it up + cs->attackcrouch_time = level.time + 300; + } + */ + cs->attackcrouch_time = 0; // animation is played from standing start + // + // are we close enough to pick it up? + if ( /*cs->grenadeGrabFlag <= 0 || */ + ( danger->s.pos.trDelta[2] < 20 && VectorDistance( danger->r.currentOrigin, cs->bs->origin ) < 48 && ( danger->r.currentOrigin[2] < cs->bs->origin[2] ) && + VectorLength( danger->s.pos.trDelta ) < 50 ) ) { + // + // we have a choice here, either pick up and return, or just kick it +// if ((cs->grenadeGrabFlag == -1) || (cs->grenadeGrabFlag == qtrue && level.time > danger->nextthink - 2000)) { // kick + + // play the kick anim + if ( cs->grenadeGrabFlag == qtrue ) { + AICast_AimAtEnemy( cs ); + // play the kick anim + BG_AnimScriptEvent( &ent->client->ps, ANIM_ET_KICKGRENADE, qfalse, qtrue ); + cs->grenadeGrabFlag = -1; + // stop the grenade from moving away + danger->s.pos.trDelta[0] = 0; + danger->s.pos.trDelta[1] = 0; + if ( danger->s.pos.trDelta[2] > 0 ) { + danger->s.pos.trDelta[2] = 0; + } + } else if ( ent->client->ps.legsTimer < 800 ) { + // send the grenade on its way + cs->grenadeFlushFiring = qtrue; + AngleVectors( cs->viewangles, dir, NULL, NULL ); + dir[2] = 0.4; + VectorNormalize( dir ); + speed = 400; + if ( cs->enemyNum >= 0 ) { + speed = 1.5 * VectorDistance( danger->r.currentOrigin, g_entities[cs->enemyNum].r.currentOrigin ); + if ( speed > 650 ) { + speed = 650; + } + } + VectorScale( dir, speed, danger->s.pos.trDelta ); + danger->r.ownerNum = ent->s.number; // we are now the owner, let it pass through us + danger->s.pos.trTime = level.time - 50; // move a bit on the very first frame + VectorCopy( danger->r.currentOrigin, danger->s.pos.trBase ); + danger->s.pos.trType = TR_GRAVITY; + SnapVector( danger->s.pos.trDelta ); // save net bandwidth + } +/* + } else { // throw + + if (cs->grenadeGrabFlag == qtrue) { + AICast_AimAtEnemy( cs ); + // play the pickup anim + BG_AnimScriptEvent( &ent->client->ps, ANIM_ET_PICKUPGRENADE, qfalse, qtrue ); + cs->grenadeGrabFlag = qfalse; + // stop the grenade from moving away + danger->s.pos.trDelta[0] = 0; + danger->s.pos.trDelta[1] = 0; + if (danger->s.pos.trDelta[2] > 0) { + danger->s.pos.trDelta[2] = 0; + } + } else if (ent->client->ps.legsTimer < 400) { + // send the grenade on its way + cs->grenadeFlushFiring = qtrue; + AngleVectors( cs->viewangles, dir, NULL, NULL ); + dir[2] = 0.4; + VectorNormalize( dir ); + speed = 500; + if (cs->enemyNum >= 0) { + speed = 2*VectorDistance(danger->r.currentOrigin, g_entities[cs->enemyNum].r.currentOrigin); + if (speed > 650) + speed = 650; + } + VectorScale( dir, speed, danger->s.pos.trDelta ); + trap_LinkEntity( danger ); + } else if (ent->client->ps.legsTimer < 800) { + // stop showing the grenade + trap_UnlinkEntity( danger ); + } + } +*/ + // + return NULL; + } + // + cs->grenadeGrabFlag = qtrue; // we must play the anim before we can grab it + // + // is the danger gone? + if ( level.time > cs->dangerEntityValidTime || !danger->inuse ) { + return AIFunc_DefaultStart( cs ); + } + // + // update the predicted position of the grenade + if ( G_PredictMissile( danger, danger->nextthink - level.time, cs->takeCoverPos, qfalse ) ) { + // make sure it's a valid position, and drop it down to the ground + cs->takeCoverPos[2] += -ent->r.mins[2] + 8; + VectorCopy( cs->takeCoverPos, end ); + end[2] -= 80; + trap_Trace( &tr, cs->takeCoverPos, ent->r.mins, ent->r.maxs, end, cs->entityNum, MASK_SOLID ); + if ( tr.startsolid ) { // not a valid position, abort + level.lastGrenadeKick = level.time; + return AIFunc_DefaultStart( cs ); + } + VectorCopy( tr.endpos, cs->takeCoverPos ); + } else { // prediction failed, so use current position + VectorCopy( danger->r.currentOrigin, cs->takeCoverPos ); + cs->takeCoverPos[2] += 16; // lift it off the floor + } + + VectorCopy( cs->takeCoverPos, destorg ); + VectorSubtract( destorg, cs->bs->origin, vec ); + //vec[2] *= 0.2; + dist = VectorLength( vec ); + // + // look for things we should attack + // if we are out of ammo, we shouldn't bother trying to attack + ammo = cs->bs->cur_ps.ammo; + shouldAttack = qfalse; + numEnemies = 0; + if ( AICast_GotEnoughAmmoForWeapon( cs, cs->weaponNum ) ) { + numEnemies = AICast_ScanForEnemies( cs, enemies ); + if ( numEnemies == -1 ) { // query mode + return NULL; + } + if ( numEnemies == -2 ) { // inspection may be required + char *retval; + // TTimo: gcc: suggest () around assignment used as truth value + if ( ( retval = AIFunc_InspectFriendlyStart( cs, enemies[0] ) ) ) { + return retval; + } + } + if ( numEnemies > 0 ) { + // default to the first known enemy, overwrite if we find a clearer shot + cs->enemyNum = enemies[0]; + // + for ( i = 0; i < numEnemies; i++ ) { + if ( AICast_CheckAttack( cs, enemies[i], qfalse ) || AICast_CheckAttack( AICast_GetCastState( enemies[i] ), cs->entityNum, qfalse ) ) { + cs->enemyNum = enemies[i]; + shouldAttack = qtrue; + break; + } else if ( cs->enemyNum < 0 ) { + cs->lastEnemy = enemies[i]; + } + } + } + } + // + // are we close enough to the goal? + if ( dist > 12 ) { // not close enough + // + moveresult = AICast_MoveToPos( cs, destorg, -1 ); + if ( moveresult ) { + //if the movement failed + if ( moveresult->failure ) { + //reset the avoid reach, otherwise bot is stuck in current area + trap_BotResetAvoidReach( bs->ms ); + // couldn't get there, so stop trying to get there + level.lastGrenadeKick = level.time; + return AIFunc_DefaultStart( cs ); + } + // + if ( moveresult->blocked ) { // abort if we get blocked at any point + level.lastGrenadeKick = level.time; + return AIFunc_DefaultStart( cs ); + } + } + // we should slow down on approaching it + cs->speedScale = AICast_SpeedScaleForDistance( cs, dist, 0 ); + } +/* // + // if we are close, put our weapon away and get ready to catch it + if (VectorDistance( danger->r.currentOrigin, cs->bs->origin ) < 128) { + // put weapon away, select grenades + // FIXME: does this fail if we don't have any grenades? + cs->weaponNum = weapon; // select grenade launcher + shouldAttack = qfalse; // don't attack until we've caught it + } +*/ + // if we should be attacking something on our way + if ( shouldAttack ) { + vec3_t vec, dir; + + //attack the enemy if possible + AICast_ProcessAttack( cs ); + // + // if they are close, and we're heading for them, we should abort this manouver + VectorSubtract( g_entities[cs->enemyNum].client->ps.origin, bs->origin, vec ); + if ( VectorNormalize( vec ) < 64 ) { + if ( VectorNormalize2( bs->velocity, dir ) > 20 ) { // we are moving + if ( DotProduct( dir, vec ) > 0 ) { + // abort + level.lastGrenadeKick = level.time; + return AIFunc_DefaultStart( cs ); + } + } + } + } else { + // face the direction that the grenade is coming + VectorSubtract( danger->r.currentOrigin, cs->bs->origin, dir ); + dir[2] = 0; + VectorNormalize( dir ); + vectoangles( dir, cs->ideal_viewangles ); + } + + return NULL; +} + +/* +============= +AIFunc_GrenadeKickStart() +============= +*/ +char *AIFunc_GrenadeKickStart( cast_state_t *cs ) { + gentity_t *danger; + gentity_t *ent; + //gentity_t *trav; + //int numFriends, i; + + //G_Printf( "Excuse me, you dropped something\n" ); + + ent = &g_entities[cs->entityNum]; + danger = &g_entities[cs->dangerEntity]; + // should we dive onto the grenade? + /* + if (danger->s.pos.trDelta[2] < 30) { + // count the number of friends near us + numFriends = 0; + for (i=0, trav=g_entities; iinuse) + continue; + if (trav->aiInactive) + continue; + if (trav->health <= 0) + continue; + if (!AICast_SameTeam( cs, i )) + continue; + if (VectorDistance( cs->takeCoverPos, trav->r.currentOrigin ) > 200) + continue; + numFriends++; + } + // if there are enough friends around, and we have a clear path to the position, sacrifice ourself! + if (numFriends > 2) { + trace_t tr; + trap_Trace( &tr, cs->bs->origin, ent->r.mins, ent->r.maxs, cs->takeCoverPos, cs->entityNum, MASK_SOLID ); + if (tr.fraction == 1.0 && !tr.startsolid) { + return AIFunc_GrenadeDiveStart( cs ); + } + } + } + */ + // + // we have decided to kick or throw the grenade away + cs->grenadeKickWeapon = danger->s.weapon; + cs->grenadeFlushFiring = qfalse; + cs->aifunc = AIFunc_GrenadeKick; + return "AIFunc_GrenadeKick"; +} + +/* +============ +AIFunc_Battle() +============ +*/ +char *AIFunc_Battle( cast_state_t *cs ) { + bot_moveresult_t moveresult; + int tfl; + bot_state_t *bs; + gentity_t *ent, *enemy; + + ent = &g_entities[cs->entityNum]; + enemy = &g_entities[cs->enemyNum]; + + // if we are not in combat mode, then go there now! + if ( cs->aiState < AISTATE_COMBAT ) { + AICast_StateChange( cs, AISTATE_COMBAT ); // just go straight to combat mode + } + // + // do we need to avoid a danger? + if ( cs->dangerEntityValidTime >= level.time ) { + if ( !AICast_GetTakeCoverPos( cs, cs->dangerEntity, cs->dangerEntityPos, cs->takeCoverPos ) ) { + // shit?? + } + // go to a position that cannot be seen from the dangerPos + cs->takeCoverTime = cs->dangerEntityValidTime + 1000; + cs->attackcrouch_time = 0; + return AIFunc_AvoidDangerStart( cs ); + } + // + // are we waiting for a door? + if ( cs->doorMarkerTime > level.time - 100 ) { + return AIFunc_DoorMarkerStart( cs, cs->doorMarkerDoor, cs->doorMarkerNum ); + } + // + // do we need to go to our leader? + if ( cs->leaderNum >= 0 && Distance( cs->bs->origin, g_entities[cs->leaderNum].r.currentOrigin ) > MAX_LEADER_DIST ) { + return AIFunc_ChaseGoalStart( cs, cs->leaderNum, AICAST_LEADERDIST_MAX, qtrue ); + } + bs = cs->bs; + //if no enemy + if ( cs->enemyNum < 0 ) { + // go back to whatever our default action is + return AIFunc_DefaultStart( cs ); + } + // + if ( enemy->health <= 0 ) { + // go back to whatever our default action is + if ( g_entities[cs->entityNum].aiTeam == AITEAM_NAZI ) { + return AIFunc_InspectBodyStart( cs ); + } else { + return AIFunc_DefaultStart( cs ); + } + } + // + // if we are not in a good attacking position, we should chase + if ( !AICast_StopAndAttack( cs ) ) { + return AIFunc_BattleChaseStart( cs ); + } + // + // if the enemy is no longer visible + if ( ( cs->bs->cur_ps.weaponTime < 100 ) // if reloading, don't chase until ready + && ( cs->castScriptStatus.scriptNoMoveTime < level.time ) + && ( /*!AICast_EntityVisible( cs, cs->enemyNum, qtrue ) ||*/ !AICast_CheckAttack( cs, cs->enemyNum, qfalse ) ) ) { + + // if we are in a void, then try to avoid so we get out of it + if ( !cs->bs->areanum ) { + if ( cs->obstructingTime >= level.time ) { + // move there + trap_EA_Move( cs->entityNum, cs->takeCoverPos, 200 ); + } else if ( AICast_GetAvoid( cs, NULL, cs->takeCoverPos, qtrue, cs->enemyNum ) ) { + VectorSubtract( cs->takeCoverPos, cs->bs->origin, cs->takeCoverPos ); + if ( VectorNormalize( cs->takeCoverPos ) > 60 ) { + cs->obstructingTime = level.time + 1000 + rand() % 600; + } + return NULL; + } + } else + // if we are heading for a combatGoal, give us some time to get there + if ( cs->combatGoalTime > level.time ) { + if ( cs->combatGoalTime > level.time + 3000 ) { + cs->combatGoalTime = level.time + 2000 + rand() % 1000; + cs->combatSpotDelayTime = level.time + 4000 + rand() % 3000; + } + } else + if ( cs->leaderNum >= 0 ) { + // chase them, nothing else to do + return AIFunc_BattleChaseStart( cs ); + } else + // if we weren't moving, it is likely they have dodged back behind something, ready to duck out and take another + // shot. so, we could fool them by hiding from the position we last saw them from, in the hope that when they + // return to fire at us, we won't be in their sight. + if ( cs->attributes[TACTICAL] > 0.3 + && cs->attributes[AGGRESSION] < 1.0 + && cs->attributes[AGGRESSION] < ( random() + 0.5 * cs->attributes[TACTICAL] ) + && ( cs->takeCoverTime < level.time ) + && AICast_GetTakeCoverPos( cs, cs->enemyNum, cs->vislist[cs->enemyNum].real_visible_pos, cs->takeCoverPos ) ) { + // start taking cover + cs->takeCoverTime = level.time + 2000 + rand() % 4000; // only move a little bit + //cs->attackcrouch_time = 0; // get out of here real quick + return AIFunc_BattleTakeCoverStart( cs ); + } else + // if we haven't thrown a grenade in a bit, go into "grenade flush mode" + if ( ( lastGrenadeFlush > level.time || lastGrenadeFlush < level.time - 7000 ) && + ( cs->aiState >= AISTATE_COMBAT ) && + ( cs->castScriptStatus.castScriptEventIndex < 0 ) && + ( ( ( COM_BitCheck( cs->bs->cur_ps.weapons, WP_GRENADE_LAUNCHER ) ) && + ( AICast_GotEnoughAmmoForWeapon( cs, WP_GRENADE_LAUNCHER ) ) && + ( cs->weaponFireTimes[WP_GRENADE_LAUNCHER] < level.time - (int)( aicast_skillscale * 3000 ) ) ) || + ( ( COM_BitCheck( cs->bs->cur_ps.weapons, WP_GRENADE_PINEAPPLE ) ) && + ( AICast_GotEnoughAmmoForWeapon( cs, WP_GRENADE_PINEAPPLE ) ) && + ( cs->weaponFireTimes[WP_GRENADE_PINEAPPLE] < level.time - (int)( aicast_skillscale * 3000 ) ) ) ) && + !( cs->weaponNum && ( cs->castScriptStatus.scriptFlags & SFL_NOCHANGEWEAPON ) ) && + ( Distance( cs->bs->origin, cs->vislist[cs->enemyNum].real_visible_pos ) > 100 ) && + ( Distance( cs->bs->origin, cs->vislist[cs->enemyNum].real_visible_pos ) < 1200 ) && + ( AICast_WantsToChase( cs ) ) ) { + // try and flush them out with a grenade + //G_Printf("get outta there..\n"); + return AIFunc_GrenadeFlushStart( cs ); + } else + // not visible, should we chase them? + if ( AICast_WantsToChase( cs ) ) { + // chase them + return AIFunc_BattleChaseStart( cs ); + } else + // Take Cover? + if ( AICast_WantsToTakeCover( cs, qfalse ) + && ( cs->takeCoverTime < level.time ) + && AICast_GetTakeCoverPos( cs, cs->enemyNum, cs->vislist[cs->enemyNum].real_visible_pos, cs->takeCoverPos ) ) { + // go to a position that cannot be seen from the last place we saw the enemy, and wait there for some time + cs->takeCoverTime = level.time + 4000 + rand() % 2000; + //cs->attackcrouch_time = 0; + return AIFunc_BattleTakeCoverStart( cs ); + } else + { + // chase them, nothing else to do + return AIFunc_BattleChaseStart( cs ); + } + } + // if we are obstructing someone else, move out the way + if ( cs->obstructingTime > level.time ) { + // setup a combatgoal in the obstructionYaw direction + //cs->combatGoalTime = level.time + 10; + //VectorCopy( cs->obstructingPos, cs->combatGoalOrigin ); + AICast_MoveToPos( cs, cs->obstructingPos, -1 ); + // if not crouching, walk instead of running + cs->speedScale = cs->attributes[WALKING_SPEED] / cs->attributes[RUNNING_SPEED]; + } + // if the enemy is really close, avoid them + else if ( ( cs->obstructingTime < ( level.time - 500 + rand() % 300 ) ) && + ( Distance( cs->bs->origin, cs->vislist[cs->enemyNum].real_visible_pos ) < 100 ) ) { + if ( AICast_GetAvoid( cs, NULL, cs->obstructingPos, qtrue, cs->enemyNum ) ) { + cs->obstructingTime = level.time + 500; + } else { + cs->obstructingTime = level.time - 1; // wait a bit before trying again + } + } + // + // setup for the fight + // + tfl = cs->travelflags; + //if in lava or slime the bot should be able to get out + if ( BotInLava( bs ) ) { + tfl |= TFL_LAVA; + } + if ( BotInSlime( bs ) ) { + tfl |= TFL_SLIME; + } + // + /* + moveresult = AICast_CombatMove(cs, tfl); + //if the movement failed + if (moveresult.failure) { + //reset the avoid reach, otherwise bot is stuck in current area + trap_BotResetAvoidReach(bs->ms); + // reset the combatgoal + cs->combatGoalTime = 0; + } else if (cs->combatGoalTime > level.time && VectorLength(cs->bs->cur_ps.velocity)) { // crouch if moving? + if (cs->attributes[ATTACK_CROUCH] > 0.1) { + AICast_RequestCrouchAttack( cs, cs->bs->origin, 0.5 ); + } + } + */ + // if we are crouching, don't stay down for too long after we finish fighting + if ( cs->aiFlags & AIFL_ATTACK_CROUCH ) { + if ( cs->attackcrouch_time > level.time || ( cs->thinkFuncChangeTime < level.time - 1000 ) ) { + cs->attackcrouch_time = level.time + 1000; + } + } else { + cs->attackcrouch_time = 0; // only set it if we need it + } + // + AICast_Blocked( cs, &moveresult, qfalse, NULL ); + // + // Retreat? + if ( cs->castScriptStatus.scriptNoMoveTime < level.time && AICast_WantToRetreat( cs ) ) { + if ( AICast_GetTakeCoverPos( cs, cs->enemyNum, cs->vislist[cs->enemyNum].visible_pos, cs->takeCoverPos ) ) { + // go to a position that cannot be seen from the last place we saw the enemy, and wait there for some time + cs->takeCoverTime = level.time + 2000 + rand() % 3000; + return AIFunc_BattleTakeCoverStart( cs ); + } + } + // + // Lob a Grenade? + // if we haven't thrown a grenade in a bit, go into "grenade flush mode" + if ( ( lastGrenadeFlush > level.time || lastGrenadeFlush < level.time - 7000 ) && + ( cs->aiState >= AISTATE_COMBAT ) && + ( cs->castScriptStatus.castScriptEventIndex < 0 ) && + ( cs->startGrenadeFlushTime < level.time - 3000 ) && + ( COM_BitCheck( cs->bs->cur_ps.weapons, WP_GRENADE_LAUNCHER ) ) && + ( AICast_GotEnoughAmmoForWeapon( cs, WP_GRENADE_LAUNCHER ) ) && + ( cs->weaponFireTimes[WP_GRENADE_LAUNCHER] < level.time - (int)( aicast_skillscale * 3000 ) ) && + ( ( cs->weaponNum == WP_GRENADE_LAUNCHER ) || !( cs->castScriptStatus.scriptFlags & SFL_NOCHANGEWEAPON ) ) && + ( Distance( cs->bs->origin, cs->vislist[cs->enemyNum].real_visible_pos ) > 100 ) && + ( Distance( cs->bs->origin, cs->vislist[cs->enemyNum].real_visible_pos ) < 2000 ) ) { + // try and flush them out with a grenade + //G_Printf("pineapple?\n"); + return AIFunc_GrenadeFlushStart( cs ); + } + if ( ( lastGrenadeFlush > level.time || lastGrenadeFlush < level.time - 7000 ) && + ( cs->aiState >= AISTATE_COMBAT ) && + ( cs->castScriptStatus.castScriptEventIndex < 0 ) && + ( cs->startGrenadeFlushTime < level.time - 3000 ) && + ( COM_BitCheck( cs->bs->cur_ps.weapons, WP_GRENADE_PINEAPPLE ) ) && + ( AICast_GotEnoughAmmoForWeapon( cs, WP_GRENADE_PINEAPPLE ) ) && + ( cs->weaponFireTimes[WP_GRENADE_PINEAPPLE] < level.time - (int)( aicast_skillscale * 3000 ) ) && + ( ( cs->weaponNum == WP_GRENADE_PINEAPPLE ) || !( cs->castScriptStatus.scriptFlags & SFL_NOCHANGEWEAPON ) ) && + ( Distance( cs->bs->origin, cs->vislist[cs->enemyNum].real_visible_pos ) > 100 ) && + ( Distance( cs->bs->origin, cs->vislist[cs->enemyNum].real_visible_pos ) < 2000 ) ) { + // try and flush them out with a grenade + //G_Printf("pineapple?\n"); + return AIFunc_GrenadeFlushStart( cs ); + } + // + // Dodge enemy aim? + if ( ( cs->attributes[AGGRESSION] < 1.0 ) && + ( ent->client->ps.weapon ) && + ( ent->client->ps.groundEntityNum == ENTITYNUM_WORLD ) && + ( !cs->lastRollMove || cs->lastRollMove < level.time - 4000 ) && + ( cs->attributes[TACTICAL] > 0.5 ) && ( cs->aiFlags & AIFL_ROLL_ANIM ) && + ( VectorLength( cs->bs->cur_ps.velocity ) < 1 ) ) { + vec3_t aim, enemyVec, right; + // are they aiming at us? + AngleVectors( enemy->client->ps.viewangles, aim, right, NULL ); + VectorSubtract( cs->bs->origin, enemy->r.currentOrigin, enemyVec ); + VectorNormalize( enemyVec ); + // if they are looking at us, we should avoid them + if ( DotProduct( aim, enemyVec ) > 0.97 ) { + aicast_predictmove_t move; + vec3_t dir; + bot_input_t bi, bi_back; + usercmd_t ucmd; + float simTime = 0.8; + + cs->lastRollMove = level.time; + + trap_EA_GetInput( cs->entityNum, (float) level.time / 1000, &bi_back ); + trap_EA_ResetInput( cs->entityNum, NULL ); + if ( level.time % 200 < 100 ) { + VectorNegate( right, dir ); + } else { VectorCopy( right, dir );} + trap_EA_Move( cs->entityNum, dir, 400 ); + trap_EA_GetInput( cs->entityNum, (float) level.time / 1000, &bi ); + VectorCopy( dir, bi.dir ); + AICast_InputToUserCommand( cs, &bi, &ucmd, bs->cur_ps.delta_angles ); + AICast_PredictMovement( cs, 4, simTime / 4, &move, &ucmd, cs->enemyNum ); + + trap_EA_ResetInput( cs->entityNum, &bi_back ); + + if ( move.groundEntityNum == ENTITYNUM_WORLD && + VectorDistance( move.endpos, cs->bs->origin ) > simTime * cs->attributes[RUNNING_SPEED] * 0.8 ) { + // good enough + if ( AICast_CheckAttackAtPos( cs->entityNum, cs->enemyNum, move.endpos, cs->bs->cur_ps.viewheight == cs->bs->cur_ps.crouchViewHeight, qfalse ) ) { + cs->takeCoverTime = 0; + return AIFunc_BattleRollStart( cs, dir ); + } + } + } + } + // + // reload? + if ( ( cs->bs->cur_ps.weaponstate != WEAPON_RELOADING ) && ( cs->bs->cur_ps.ammoclip[BG_FindClipForWeapon( cs->bs->cur_ps.weapon )] < (int)( ammoTable[cs->bs->cur_ps.weapon].uses ) ) ) { + if ( AICast_GotEnoughAmmoForWeapon( cs, cs->weaponNum ) ) { + trap_EA_Reload( cs->entityNum ); + } else { // no ammo, switch? + AICast_ChooseWeapon( cs, qfalse ); + if ( cs->weaponNum == WP_NONE ) { + // no ammo, get out of here + return AIFunc_DefaultStart( cs ); + } + if ( !AICast_GotEnoughAmmoForWeapon( cs, cs->weaponNum ) ) { + // no ammo, get out of here + return AIFunc_DefaultStart( cs ); + } + } + } else { + //attack the enemy if possible + AICast_ProcessAttack( cs ); + } + // + return NULL; +} + +/* +============ +AIFunc_BattleStart() +============ +*/ +char *AIFunc_BattleStart( cast_state_t *cs ) { + char *rval; + int lastweap; + // make sure we don't avoid any areas when we start again + trap_BotInitAvoidReach( cs->bs->ms ); + // wait some time before taking cover again + cs->takeCoverTime = level.time + 300 + rand() % ( 2000 + (int)( 2000.0 * cs->attributes[AGGRESSION] ) ); + // wait some time before going to a combat spot + cs->combatSpotDelayTime = level.time + 1500 + rand() % 2500; + // + // start a crouch attack? + if ( ( random() * 3.0 + 1.0 < cs->attributes[ATTACK_CROUCH] ) + && AICast_RequestCrouchAttack( cs, cs->bs->origin, 0.0 ) ) { + cs->aiFlags |= AIFL_ATTACK_CROUCH; + } else { + cs->attackcrouch_time = 0; + cs->aiFlags &= ~AIFL_ATTACK_CROUCH; + } + // + cs->lastEnemy = cs->enemyNum; + cs->startAttackCount++; + cs->crouchHideFlag = qfalse; + // + // get out of talking state + cs->aiFlags &= ~AIFL_TALKING; + // + //update the attack inventory values + AICast_UpdateBattleInventory( cs, cs->enemyNum ); + // + // if we have a special attack, call the correct AI routine +recheck: + rval = NULL; + // ignore special attacks until we are facing our enemy + if ( fabs( AngleDifference( cs->ideal_viewangles[YAW], cs->viewangles[YAW] ) ) < 10 ) { + // select a weapon + AICast_ChooseWeapon( cs, qtrue ); + // + if ( ( cs->weaponNum == WP_MONSTER_ATTACK1 ) && cs->aifuncAttack1 ) { + if ( AICast_CheckAttack( cs, cs->enemyNum, qfalse ) ) { + rval = cs->aifuncAttack1( cs ); + } else { + rval = AIFunc_BattleChaseStart( cs ); + } + } else if ( ( cs->weaponNum == WP_MONSTER_ATTACK2 ) && cs->aifuncAttack2 ) { + if ( AICast_CheckAttack( cs, cs->enemyNum, qfalse ) ) { + rval = cs->aifuncAttack2( cs ); + } else { + rval = AIFunc_BattleChaseStart( cs ); + } + } else if ( ( cs->weaponNum == WP_MONSTER_ATTACK3 ) && cs->aifuncAttack3 ) { + if ( AICast_CheckAttack( cs, cs->enemyNum, qfalse ) ) { + rval = cs->aifuncAttack3( cs ); + } else { + rval = AIFunc_BattleChaseStart( cs ); + } + } + // + if ( !rval && cs->weaponNum >= WP_MONSTER_ATTACK1 && cs->weaponNum <= WP_MONSTER_ATTACK3 ) { + // don't use this weapon again for a while + cs->weaponFireTimes[cs->weaponNum] = level.time; + // select a different weapon + lastweap = cs->weaponNum; + AICast_ChooseWeapon( cs, qfalse ); // qfalse so we don't choose a special weapon + if ( cs->weaponNum == lastweap ) { + return NULL; + } + // try again + goto recheck; + } + } else { // normal weapons + // select a weapon + AICast_ChooseWeapon( cs, qfalse ); + rval = NULL; + } + // + if ( !rval ) { // use the generic battle routine for all "normal" weapons + if ( cs->weaponNum >= WP_MONSTER_ATTACK1 && cs->weaponNum <= WP_MONSTER_ATTACK3 ) { + // monster attacks are not allowed to go into the normal battle mode + return NULL; + } else { + cs->aifunc = AIFunc_Battle; + return "AIFunc_Battle"; + } + } + // + // we decided to start a special monster attack + return rval; +} + +/* +============ +AIFunc_DefaultStart() +============ +*/ +char *AIFunc_DefaultStart( cast_state_t *cs ) { + qboolean first = qfalse; + char *rval = NULL; + // + if ( cs->aiFlags & AIFL_JUST_SPAWNED ) { + first = qtrue; + cs->aiFlags &= ~AIFL_JUST_SPAWNED; + } + // + switch ( cs->aiCharacter ) { + case AICHAR_ZOMBIE: + // portal zombie, requires spawning effect + if ( first && ( g_entities[cs->entityNum].spawnflags & 4 ) ) { + return AIFunc_FlameZombie_PortalStart( cs ); + } + break; + } + // + // if they have an enemy, then pursue + if ( cs->enemyNum >= 0 && ( cs->aifunc != AIFunc_Battle ) ) { // make sure we haven't just come from there + rval = AIFunc_BattleStart( cs ); + } + // + if ( !rval ) { + return AIFunc_IdleStart( cs ); + } + // + return rval; +} diff --git a/src/game/ai_cast_global.h b/src/game/ai_cast_global.h new file mode 100644 index 0000000..9485f94 --- /dev/null +++ b/src/game/ai_cast_global.h @@ -0,0 +1,85 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: ai_cast_global.h +// Function: Global AI Cast defines +// Programmer: Ridah +// Tab Size: 4 (real tabs) +//=========================================================================== + +// TTimo no typedef, "warning: useless keyword or type name in empty declaration" +struct cast_state_s; + +#define AICAST_AIM_SPREAD 2048.0 // a really bad shooter will offset a maximum of this per shot, from the end point of the 8192 trace length + +#define DANGER_MISSILE ( 1 << 0 ) +#define DANGER_CLIENTAIM ( 1 << 1 ) +#define DANGER_FLAMES ( 1 << 2 ) + +extern qboolean saveGamePending; + +qboolean AICast_SameTeam( struct cast_state_s *cs, int enemynum ); +struct cast_state_s *AICast_GetCastState( int entitynum ); +void AICast_ScriptLoad( void ); +void AICast_ScriptEvent( struct cast_state_s *cs, char *eventStr, char *params ); +void AICast_ForceScriptEvent( struct cast_state_s *cs, char *eventStr, char *params ); +qboolean AICast_AIDamageOK( struct cast_state_s *cs, struct cast_state_s *ocs ); +gentity_t *AICast_FindEntityForName( char *name ); +gentity_t *AICast_TravEntityForName( gentity_t *startent, char *name ); +void AICast_ScriptParse( struct cast_state_s *cs ); +void AICast_StartFrame( int time ); +void AICast_StartServerFrame( int time ); +void AICast_RecordWeaponFire( gentity_t *ent ); +void AICast_AIDoor_Touch( gentity_t *ent, gentity_t *aidoor_trigger, gentity_t *door ); +float AICast_GetAccuracy( int entnum ); +void AICast_Activate( int activatorNum, int entNum ); +void AICast_CheckDangerousEntity( gentity_t *ent, int dangerFlags, float dangerDist, float tacticalLevel, float aggressionLevel, qboolean hurtFriendly ); +qboolean AICast_NoFlameDamage( int entNum ); +void AICast_SetFlameDamage( int entNum, qboolean status ); +qboolean AICast_HasFiredWeapon( int entNum, int weapon ); +void G_SetAASBlockingEntity( gentity_t *ent, qboolean blocking ); +qboolean AICast_InFieldOfVision( vec3_t viewangles, float fov, vec3_t angles ); +qboolean AICast_VisibleFromPos( vec3_t srcpos, int srcnum, + vec3_t destpos, int destnum, qboolean updateVisPos ); +qboolean AICast_AllowFlameDamage( int entNum ); +void AICast_AdjustIdealYawForMover( int entnum, float yaw ); +void AICast_AgePlayTime( int entnum ); +int AICast_NoReload( int entnum ); +void AICast_RecordScriptSound( int client ); +void AICast_UpdateVisibility( gentity_t *srcent, gentity_t *destent, qboolean shareVis, qboolean directview ); +void AICast_ProcessBullet( gentity_t *attacker, vec3_t start, vec3_t end ); +void AICast_AudibleEvent( int srcnum, vec3_t pos, float range ); + +//----(SA) added +int AICast_PlayTime( int entnum ); +int AICast_NumAttempts( int entnum ); +//----(SA) end + +void AICast_RegisterPain( int entnum ); diff --git a/src/game/ai_cast_script.c b/src/game/ai_cast_script.c new file mode 100644 index 0000000..c580937 --- /dev/null +++ b/src/game/ai_cast_script.c @@ -0,0 +1,807 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: ai_cast_script.c +// Function: Wolfenstein AI Character Scripting +// Programmer: Ridah +// Tab Size: 4 (real tabs) +//=========================================================================== + +#include "../game/g_local.h" +#include "../game/q_shared.h" +#include "../game/botlib.h" //bot lib interface +#include "../game/be_aas.h" +#include "../game/be_ea.h" +#include "../game/be_ai_gen.h" +#include "../game/be_ai_goal.h" +#include "../game/be_ai_move.h" +#include "../botai/botai.h" //bot ai interface + +#include "ai_cast.h" + +/* +Scripting that allows the designers to control the behaviour of AI characters +according to each different scenario. +*/ + +// action functions need to be declared here so they can be accessed in the scriptAction table +qboolean AICast_ScriptAction_GotoMarker( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_WalkToMarker( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_CrouchToMarker( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_GotoCast( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_WalkToCast( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_CrouchToCast( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_Wait( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_AbortIfLoadgame( cast_state_t *cs, char *params ); //----(SA) added +qboolean AICast_ScriptAction_Trigger( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_FollowCast( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_PlaySound( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_NoAttack( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_Attack( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_PlayAnim( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_ClearAnim( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_SetAmmo( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_SetClip( cast_state_t *cs, char *params ); //----(SA) added +qboolean AICast_ScriptAction_SelectWeapon( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_GiveArmor( cast_state_t *cs, char *params ); //----(SA) added +qboolean AICast_ScriptAction_SetArmor( cast_state_t *cs, char *params ); //----(SA) added +qboolean AICast_ScriptAction_SuggestWeapon( cast_state_t *cs, char *params ); //----(SA) added +qboolean AICast_ScriptAction_GiveWeapon( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_GiveInventory( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_TakeWeapon( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_Movetype( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_AlertEntity( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_SaveGame( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_FireAtTarget( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_GodMode( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_Accum( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_SpawnCast( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_MissionFailed( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_ObjectiveMet( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_ObjectivesNeeded( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_NoAIDamage( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_Print( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_FaceTargetAngles( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_ResetScript( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_Mount( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_Unmount( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_SavePersistant( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_ChangeLevel( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_EndGame( cast_state_t *cs, char *params ); //----(SA) added +qboolean AICast_ScriptAction_Teleport( cast_state_t *cs, char *params ); //----(SA) added +qboolean AICast_ScriptAction_FoundSecret( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_NoSight( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_Sight( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_NoAvoid( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_Avoid( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_Attrib( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_DenyAction( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_LightningDamage( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_Headlook( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_BackupScript( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_RestoreScript( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_StateType( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_KnockBack( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_Zoom( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_Parachute( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_Cigarette( cast_state_t *cs, char *params ); //----(SA) added +qboolean AICast_ScriptAction_StartCam( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_StopCam( cast_state_t *cs, char *params ); //----(SA) added +qboolean AICast_ScriptAction_StartCamBlack( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_EntityScriptName( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_AIScriptName( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_SetHealth( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_NoTarget( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_Cvar( cast_state_t *cs, char *params ); + +qboolean AICast_ScriptAction_MusicStart( cast_state_t *cs, char *params ); //----(SA) +qboolean AICast_ScriptAction_MusicPlay( cast_state_t *cs, char *params ); //----(SA) +qboolean AICast_ScriptAction_MusicStop( cast_state_t *cs, char *params ); //----(SA) +qboolean AICast_ScriptAction_MusicFade( cast_state_t *cs, char *params ); //----(SA) +qboolean AICast_ScriptAction_MusicQueue( cast_state_t *cs, char *params ); //----(SA) + +qboolean AICast_ScriptAction_ExplicitRouting( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_LockPlayer( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_AnimCondition( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_PushAway( cast_state_t *cs, char *params ); +qboolean AICast_ScriptAction_CatchFire( cast_state_t *cs, char *params ); + +// these are the actions that each event can call +cast_script_stack_action_t scriptActions[] = +{ + {"gotomarker", AICast_ScriptAction_GotoMarker}, + {"runtomarker", AICast_ScriptAction_GotoMarker}, + {"walktomarker", AICast_ScriptAction_WalkToMarker}, + {"crouchtomarker", AICast_ScriptAction_CrouchToMarker}, + {"gotocast", AICast_ScriptAction_GotoCast}, + {"runtocast", AICast_ScriptAction_GotoCast}, + {"walktocast", AICast_ScriptAction_WalkToCast}, + {"crouchtocast", AICast_ScriptAction_CrouchToCast}, + {"followcast", AICast_ScriptAction_FollowCast}, + {"playsound", AICast_ScriptAction_PlaySound}, + {"playanim", AICast_ScriptAction_PlayAnim}, + {"clearanim", AICast_ScriptAction_ClearAnim}, + {"wait", AICast_ScriptAction_Wait}, + {"abort_if_loadgame",AICast_ScriptAction_AbortIfLoadgame}, //----(SA) added + {"trigger", AICast_ScriptAction_Trigger}, + {"setammo", AICast_ScriptAction_SetAmmo}, + {"setclip", AICast_ScriptAction_SetClip}, //----(SA) added + {"selectweapon", AICast_ScriptAction_SelectWeapon}, + {"noattack", AICast_ScriptAction_NoAttack}, + {"suggestweapon", AICast_ScriptAction_SuggestWeapon}, //----(SA) added + {"attack", AICast_ScriptAction_Attack}, + {"givearmor", AICast_ScriptAction_GiveArmor}, //----(SA) added + {"setarmor", AICast_ScriptAction_SetArmor}, //----(SA) added + {"giveinventory", AICast_ScriptAction_GiveInventory}, + {"giveweapon", AICast_ScriptAction_GiveWeapon}, + {"takeweapon", AICast_ScriptAction_TakeWeapon}, + {"movetype", AICast_ScriptAction_Movetype}, + {"alertentity", AICast_ScriptAction_AlertEntity}, + {"savegame", AICast_ScriptAction_SaveGame}, + {"fireattarget", AICast_ScriptAction_FireAtTarget}, + {"godmode", AICast_ScriptAction_GodMode}, + {"accum", AICast_ScriptAction_Accum}, + {"spawncast", AICast_ScriptAction_SpawnCast}, + {"missionfailed", AICast_ScriptAction_MissionFailed}, + {"missionsuccess", AICast_ScriptAction_ObjectiveMet}, + {"objectivemet", AICast_ScriptAction_ObjectiveMet}, // dupe of missionsuccess so scripts can changeover to a more logical name + {"objectivesneeded",AICast_ScriptAction_ObjectivesNeeded}, + {"noaidamage", AICast_ScriptAction_NoAIDamage}, + {"print", AICast_ScriptAction_Print}, + {"facetargetangles",AICast_ScriptAction_FaceTargetAngles}, + {"resetscript", AICast_ScriptAction_ResetScript}, + {"mount", AICast_ScriptAction_Mount}, + {"unmount", AICast_ScriptAction_Unmount}, + {"savepersistant", AICast_ScriptAction_SavePersistant}, + {"changelevel", AICast_ScriptAction_ChangeLevel}, + {"endgame", AICast_ScriptAction_EndGame}, //----(SA) added + {"teleport", AICast_ScriptAction_Teleport}, //----(SA) added + {"foundsecret", AICast_ScriptAction_FoundSecret}, + {"nosight", AICast_ScriptAction_NoSight}, + {"sight", AICast_ScriptAction_Sight}, + {"noavoid", AICast_ScriptAction_NoAvoid}, + {"avoid", AICast_ScriptAction_Avoid}, + {"attrib", AICast_ScriptAction_Attrib}, + {"denyactivate", AICast_ScriptAction_DenyAction}, + {"lightningdamage", AICast_ScriptAction_LightningDamage}, + {"deny", AICast_ScriptAction_DenyAction}, + {"headlook", AICast_ScriptAction_Headlook}, + {"backupscript", AICast_ScriptAction_BackupScript}, + {"restorescript", AICast_ScriptAction_RestoreScript}, + {"statetype", AICast_ScriptAction_StateType}, + {"knockback", AICast_ScriptAction_KnockBack}, + {"zoom", AICast_ScriptAction_Zoom}, + {"parachute", AICast_ScriptAction_Parachute}, + {"cigarette", AICast_ScriptAction_Cigarette}, //----(SA) added + {"startcam", AICast_ScriptAction_StartCam}, + {"startcamblack", AICast_ScriptAction_StartCamBlack}, + {"stopcam", AICast_ScriptAction_StopCam}, //----(SA) added + {"entityscriptname",AICast_ScriptAction_EntityScriptName}, + {"aiscriptname", AICast_ScriptAction_AIScriptName}, + {"sethealth", AICast_ScriptAction_SetHealth}, + {"notarget", AICast_ScriptAction_NoTarget}, + {"cvar", AICast_ScriptAction_Cvar}, + +//----(SA) added some music interface + {"mu_start", AICast_ScriptAction_MusicStart}, // (char *new_music, int time) // time to fade in + {"mu_play", AICast_ScriptAction_MusicPlay}, // (char *new_music) + {"mu_stop", AICast_ScriptAction_MusicStop}, // (int time) // time to fadeout + {"mu_fade", AICast_ScriptAction_MusicFade}, // (float target_volume, int time) // time to fade to target + {"mu_queue", AICast_ScriptAction_MusicQueue}, // (char *new_music) // music that will start when previous fades to 0 +//----(SA) end + + {"explicit_routing", AICast_ScriptAction_ExplicitRouting}, + {"lockplayer", AICast_ScriptAction_LockPlayer}, + {"anim_condition", AICast_ScriptAction_AnimCondition}, + {"pushaway", AICast_ScriptAction_PushAway}, + {"catchfire", AICast_ScriptAction_CatchFire}, + + {NULL, NULL} +}; + +qboolean AICast_EventMatch_StringEqual( cast_script_event_t *event, char *eventParm ); +qboolean AICast_EventMatch_IntInRange( cast_script_event_t *event, char *eventParm ); + +// the list of events that can start an action sequence +// NOTE!!: only append to this list, DO NOT INSERT!! +cast_script_event_define_t scriptEvents[] = +{ + {"spawn", NULL}, // called as each character is spawned into the game + {"playerstart", NULL}, // called when player hits 'start' button + {"enemysight", AICast_EventMatch_StringEqual}, // enemy has been sighted for the first time (once only) + {"sight", AICast_EventMatch_StringEqual}, // non-enemy has been sighted for the first time (once only) + {"enemydead", AICast_EventMatch_StringEqual}, // our enemy is now dead + {"trigger", AICast_EventMatch_StringEqual}, // something has triggered us (always followed by an identifier) + {"pain", AICast_EventMatch_IntInRange}, // we've been hurt + {"death", AICast_EventMatch_StringEqual}, // RIP + {"activate", AICast_EventMatch_StringEqual}, // "param" has just activated us + {"enemysightcorpse",AICast_EventMatch_StringEqual}, // sighted the given enemy as a corpse, for the first time + {"friendlysightcorpse", NULL}, // sighted a friendly as a corpse for the first time + {"avoiddanger", AICast_EventMatch_StringEqual}, // we are avoiding something dangerous + {"blocked", AICast_EventMatch_StringEqual}, // blocked by someone else + {"statechange", AICast_EventMatch_StringEqual}, // changing aistates + {"bulletimpact", NULL}, + {"inspectbodystart", AICast_EventMatch_StringEqual}, // starting to travel to body for inspection + {"inspectbodyend", AICast_EventMatch_StringEqual}, // reached body for inspection + {"inspectsoundstart", AICast_EventMatch_StringEqual}, // reached sound for inspection + {"inspectsoundend", AICast_EventMatch_StringEqual}, // reached sound for inspection + {"attacksound", AICast_EventMatch_StringEqual}, // play a custom attack sound, and/or deny playing the default sound + {"fakedeath", NULL}, + {"bulletimpactsound", NULL}, + {"inspectfriendlycombatstart", NULL}, + {"painenemy", AICast_EventMatch_StringEqual}, + {"forced_mg42_unmount", NULL}, + + {NULL, NULL} +}; + + +/* +=============== +AICast_EventMatch_StringEqual +=============== +*/ +qboolean AICast_EventMatch_StringEqual( cast_script_event_t *event, char *eventParm ) { + if ( !event->params || !event->params[0] || ( eventParm && !Q_strcasecmp( event->params, eventParm ) ) ) { + return qtrue; + } else { + return qfalse; + } +} + +/* +=============== +AICast_EventMatch_IntInRange +=============== +*/ +qboolean AICast_EventMatch_IntInRange( cast_script_event_t *event, char *eventParm ) { + char *pString, *token; + int int1, int2, eInt; + + // get the cast name + pString = eventParm; + token = COM_ParseExt( &pString, qfalse ); + int1 = atoi( token ); + token = COM_ParseExt( &pString, qfalse ); + int2 = atoi( token ); + + eInt = atoi( event->params ); + + if ( eventParm && eInt > int1 && eInt <= int2 ) { + return qtrue; + } else { + return qfalse; + } +} + +/* +=============== +AICast_EventForString +=============== +*/ +int AICast_EventForString( char *string ) { + int i; + + for ( i = 0; scriptEvents[i].eventStr; i++ ) + { + if ( !Q_strcasecmp( string, scriptEvents[i].eventStr ) ) { + return i; + } + } + + return -1; +} + +//----(SA) added + +/* +=============== +AICast_ActionForString +=============== +*/ +cast_script_stack_action_t *AICast_ActionForString( cast_state_t *cs, char *string ) { + int i; + + for ( i = 0; scriptActions[i].actionString; i++ ) + { + if ( !Q_strcasecmp( string, scriptActions[i].actionString ) ) { + if ( !Q_strcasecmp( string, "foundsecret" ) ) { + level.numSecrets++; + G_SendMissionStats(); + } + return &scriptActions[i]; + } + } + + return NULL; +} + +/* +============= +AICast_ScriptLoad + + Loads the script for the current level into the buffer +============= +*/ +void AICast_ScriptLoad( void ) { + char filename[MAX_QPATH]; + vmCvar_t mapname; + fileHandle_t f; + int len; + + level.scriptAI = NULL; + + trap_Cvar_VariableStringBuffer( "ai_scriptName", filename, sizeof( filename ) ); + if ( strlen( filename ) > 0 ) { + trap_Cvar_Register( &mapname, "ai_scriptName", "", CVAR_ROM ); + } else { + trap_Cvar_Register( &mapname, "mapname", "", CVAR_SERVERINFO | CVAR_ROM ); + } + Q_strncpyz( filename, "maps/", sizeof( filename ) ); + Q_strcat( filename, sizeof( filename ), mapname.string ); + Q_strcat( filename, sizeof( filename ), ".ai" ); + + len = trap_FS_FOpenFile( filename, &f, FS_READ ); + + // make sure we clear out the temporary scriptname + trap_Cvar_Set( "ai_scriptName", "" ); + + if ( len < 0 ) { + return; + } + + level.scriptAI = G_Alloc( len ); + trap_FS_Read( level.scriptAI, len, f ); + + trap_FS_FCloseFile( f ); + + return; +} + +/* +============== +AICast_ScriptParse + + Parses the script for the given character +============== +*/ + +void AICast_ScriptParse( cast_state_t *cs ) { + #define MAX_SCRIPT_EVENTS 64 + gentity_t *ent; + char *pScript; + char *token; + qboolean wantName; + qboolean inScript; + int eventNum; + cast_script_event_t events[MAX_SCRIPT_EVENTS]; + int numEventItems; + cast_script_event_t *curEvent; + char params[MAX_QPATH]; + cast_script_stack_action_t *action; + int i; + int bracketLevel; + qboolean buildScript; //----(SA) added + + if ( !level.scriptAI ) { + return; + } + + ent = &g_entities[cs->entityNum]; + if ( !ent->aiName ) { + return; + } + + buildScript = trap_Cvar_VariableIntegerValue( "com_buildScript" ); + buildScript = qtrue; + + pScript = level.scriptAI; + wantName = qtrue; + inScript = qfalse; + COM_BeginParseSession( "AICast_ScriptParse" ); + bracketLevel = 0; + numEventItems = 0; + + memset( events, 0, sizeof( events ) ); + + while ( 1 ) + { + token = COM_Parse( &pScript ); + + if ( !token[0] ) { + if ( !wantName ) { + G_Error( "AICast_ScriptParse(), Error (line %d): '}' expected, end of script found.\n", COM_GetCurrentParseLine() ); + } + break; + } + + // end of script + if ( token[0] == '}' ) { + if ( inScript ) { + break; + } + if ( wantName ) { + G_Error( "AICast_ScriptParse(), Error (line %d): '}' found, but not expected.\n", COM_GetCurrentParseLine() ); + } + wantName = qtrue; + } else if ( token[0] == '{' ) { + if ( wantName ) { + G_Error( "AICast_ScriptParse(), Error (line %d): '{' found, NAME expected.\n", COM_GetCurrentParseLine() ); + } + } else if ( wantName ) { + if ( !Q_strcasecmp( ent->aiName, token ) ) { + inScript = qtrue; + numEventItems = 0; + } + wantName = qfalse; + } else if ( inScript ) { + if ( !Q_strcasecmp( token, "attributes" ) ) { + // read in all the attributes + AICast_CheckLevelAttributes( cs, ent, &pScript ); + continue; + } + eventNum = AICast_EventForString( token ); + if ( eventNum < 0 ) { + G_Error( "AICast_ScriptParse(), Error (line %d): unknown event: %s.\n", COM_GetCurrentParseLine(), token ); + } + if ( numEventItems >= MAX_SCRIPT_EVENTS ) { + G_Error( "AICast_ScriptParse(), Error (line %d): MAX_SCRIPT_EVENTS reached (%d)\n", COM_GetCurrentParseLine(), MAX_SCRIPT_EVENTS ); + } + + // if this is a "friendlysightcorpse" event, then disable corpse vis sharing + if ( !Q_stricmp( token, "friendlysightcorpse" ) ) { + cs->aiFlags &= ~AIFL_CORPSESIGHTING; + } + + curEvent = &events[numEventItems]; + curEvent->eventNum = eventNum; + memset( params, 0, sizeof( params ) ); + + // parse any event params before the start of this event's actions + while ( ( token = COM_Parse( &pScript ) ) && ( token[0] != '{' ) ) + { + if ( !token[0] ) { + G_Error( "AICast_ScriptParse(), Error (line %d): '}' expected, end of script found.\n", COM_GetCurrentParseLine() ); + } + + if ( eventNum == 13 ) { // statechange event, check params + if ( strlen( token ) > 1 ) { + if ( BG_IndexForString( token, animStateStr, qtrue ) < 0 ) { + G_Error( "AICast_ScriptParse(), Error (line %d): unknown state type '%s'.\n", COM_GetCurrentParseLine(), token ); + } + } + } + + if ( strlen( params ) ) { // add a space between each param + Q_strcat( params, sizeof( params ), " " ); + } + Q_strcat( params, sizeof( params ), token ); + } + + if ( strlen( params ) ) { // copy the params into the event + curEvent->params = G_Alloc( strlen( params ) + 1 ); + Q_strncpyz( curEvent->params, params, strlen( params ) + 1 ); + } + + // parse the actions for this event + while ( ( token = COM_Parse( &pScript ) ) && ( token[0] != '}' ) ) + { + if ( !token[0] ) { + G_Error( "AICast_ScriptParse(), Error (line %d): '}' expected, end of script found.\n", COM_GetCurrentParseLine() ); + } + + action = AICast_ActionForString( cs, token ); + if ( !action ) { + G_Error( "AICast_ScriptParse(), Error (line %d): unknown action: %s.\n", COM_GetCurrentParseLine(), token ); + } + + curEvent->stack.items[curEvent->stack.numItems].action = action; + + memset( params, 0, sizeof( params ) ); + token = COM_ParseExt( &pScript, qfalse ); + for ( i = 0; token[0]; i++ ) + { + if ( strlen( params ) ) { // add a space between each param + Q_strcat( params, sizeof( params ), " " ); + } + + if ( i == 0 ) { + // Special case: playsound's need to be cached on startup to prevent in-game pauses + if ( !Q_stricmp( action->actionString, "playsound" ) ) { + G_SoundIndex( token ); + } + +//----(SA) added a bit more + if ( buildScript && ( + !Q_stricmp( action->actionString, "mu_start" ) || + !Q_stricmp( action->actionString, "mu_play" ) || + !Q_stricmp( action->actionString, "mu_queue" ) || + !Q_stricmp( action->actionString, "startcam" ) || + !Q_stricmp( action->actionString, "startcamblack" ) ) + ) { + if ( strlen( token ) ) { // we know there's a [0], but don't know if it's '0' + trap_SendServerCommand( cs->entityNum, va( "addToBuild %s\n", token ) ); + } + } + + if ( !Q_stricmp( action->actionString, "giveweapon" ) ) { // register weapon for client pre-loading + gitem_t *weap = BG_FindItem2( token ); // (SA) FIXME: rats, need to fix this for weapon names with spaces: 'mauser rifle' +// if(weap) + RegisterItem( weap ); // don't be nice, just do it. if it can't find it, you'll bomb out to the error menu + } +//----(SA) end + } + + if ( strrchr( token,' ' ) ) { // need to wrap this param in quotes since it has more than one word + Q_strcat( params, sizeof( params ), "\"" ); + } + + Q_strcat( params, sizeof( params ), token ); + + if ( strrchr( token,' ' ) ) { // need to wrap this param in quotes since it has more than one word + Q_strcat( params, sizeof( params ), "\"" ); + } + + token = COM_ParseExt( &pScript, qfalse ); + } + + if ( strlen( params ) ) { // copy the params into the event + curEvent->stack.items[curEvent->stack.numItems].params = G_Alloc( strlen( params ) + 1 ); + Q_strncpyz( curEvent->stack.items[curEvent->stack.numItems].params, params, strlen( params ) + 1 ); + } + + curEvent->stack.numItems++; + + if ( curEvent->stack.numItems >= AICAST_MAX_SCRIPT_STACK_ITEMS ) { + G_Error( "AICast_ScriptParse(): script exceeded MAX_SCRIPT_ITEMS (%d), line %d\n", AICAST_MAX_SCRIPT_STACK_ITEMS, COM_GetCurrentParseLine() ); + } + } + + numEventItems++; + } else // skip this character completely + { + // TTimo: gcc: suggest () around assignment used as truth value + while ( ( token = COM_Parse( &pScript ) ) ) + { + if ( !token[0] ) { + G_Error( "AICast_ScriptParse(), Error (line %d): '}' expected, end of script found.\n", COM_GetCurrentParseLine() ); + } else if ( token[0] == '{' ) { + bracketLevel++; + } else if ( token[0] == '}' ) { + if ( !--bracketLevel ) { + break; + } + } + } + } + } + + // alloc and copy the events into the cast_state_t for this cast + if ( numEventItems > 0 ) { + cs->castScriptEvents = G_Alloc( sizeof( cast_script_event_t ) * numEventItems ); + memcpy( cs->castScriptEvents, events, sizeof( cast_script_event_t ) * numEventItems ); + cs->numCastScriptEvents = numEventItems; + + cs->castScriptStatus.castScriptEventIndex = -1; + } +} + +/* +================ +AICast_ScriptChange +================ +*/ +void AICast_ScriptChange( cast_state_t *cs, int newScriptNum ) { + cast_script_status_t scriptStatusBackup; + + cs->scriptCallIndex++; + + // backup the current scripting + scriptStatusBackup = cs->castScriptStatus; + + // set the new script to this cast, and reset script status + cs->castScriptStatus.castScriptStackHead = 0; + cs->castScriptStatus.castScriptStackChangeTime = level.time; + cs->castScriptStatus.castScriptEventIndex = newScriptNum; + cs->castScriptStatus.scriptId = scriptStatusBackup.scriptId + 1; + cs->castScriptStatus.scriptGotoId = -1; + cs->castScriptStatus.scriptGotoEnt = -1; + cs->castScriptStatus.scriptFlags |= SFL_FIRST_CALL; + + // try and run the script, if it doesn't finish, then abort the current script (discard backup) + if ( AICast_ScriptRun( cs, qtrue ) ) { + // completed successfully + cs->castScriptStatus.castScriptStackHead = scriptStatusBackup.castScriptStackHead; + cs->castScriptStatus.castScriptStackChangeTime = scriptStatusBackup.castScriptStackChangeTime; + cs->castScriptStatus.castScriptEventIndex = scriptStatusBackup.castScriptEventIndex; + cs->castScriptStatus.scriptId = scriptStatusBackup.scriptId; + cs->castScriptStatus.scriptFlags = scriptStatusBackup.scriptFlags; + } +} + +/* +================ +AICast_ScriptEvent + + An event has occured, for which a script may exist +================ +*/ +void AICast_ScriptEvent( struct cast_state_s *cs, char *eventStr, char *params ) { + int i, eventNum; + + eventNum = -1; + + // find out which event this is + for ( i = 0; scriptEvents[i].eventStr; i++ ) + { + if ( !Q_strcasecmp( eventStr, scriptEvents[i].eventStr ) ) { // match found + eventNum = i; + break; + } + } + + if ( eventNum < 0 ) { + if ( g_cheats.integer ) { // dev mode + G_Printf( "devmode-> AICast_ScriptEvent(), unknown event: %s\n", eventStr ); + } + } + + // show debugging info + if ( ( ( aicast_debug.integer == 1 ) || + ( ( aicast_debug.integer == 2 ) && + ( ( strlen( aicast_debugname.string ) < 1 ) || ( g_entities[cs->entityNum].aiName && !strcmp( aicast_debugname.string, g_entities[cs->entityNum].aiName ) ) ) ) ) ) { + G_Printf( "(%s) AIScript event: %s %s ", g_entities[cs->entityNum].aiName, eventStr, params ); + } + + cs->aiFlags &= ~AIFL_DENYACTION; + + // see if this cast has this event + for ( i = 0; i < cs->numCastScriptEvents; i++ ) + { + if ( cs->castScriptEvents[i].eventNum == eventNum ) { + if ( ( !cs->castScriptEvents[i].params ) + || ( !scriptEvents[eventNum].eventMatch || scriptEvents[eventNum].eventMatch( &cs->castScriptEvents[i], params ) ) ) { + + // show debugging info + if ( ( ( aicast_debug.integer == 1 ) || + ( ( aicast_debug.integer == 2 ) && + ( ( strlen( aicast_debugname.string ) < 1 ) || ( g_entities[cs->entityNum].aiName && !strcmp( aicast_debugname.string, g_entities[cs->entityNum].aiName ) ) ) ) ) ) { + G_Printf( "found, calling script\n", g_entities[cs->entityNum].aiName, eventStr, params ); + } + + AICast_ScriptChange( cs, i ); + break; + } + } + } + + // show debugging info + if ( ( ( aicast_debug.integer == 1 ) || + ( ( aicast_debug.integer == 2 ) && + ( ( strlen( aicast_debugname.string ) < 1 ) || ( g_entities[cs->entityNum].aiName && !strcmp( aicast_debugname.string, g_entities[cs->entityNum].aiName ) ) ) ) ) ) { + if ( i == cs->numCastScriptEvents ) { + G_Printf( "not found\n" ); + } + } + +} + +/* +================ +AICast_ForceScriptEvent + + Definately run this event now, overriding any paised state +================ +*/ +void AICast_ForceScriptEvent( struct cast_state_s *cs, char *eventStr, char *params ) { + int oldPauseTime; + + oldPauseTime = cs->scriptPauseTime; + cs->scriptPauseTime = 0; + + AICast_ScriptEvent( cs, eventStr, params ); + + cs->scriptPauseTime = oldPauseTime; +} + +/* +============= +AICast_ScriptRun + + returns qtrue if the script completed +============= +*/ +qboolean AICast_ScriptRun( cast_state_t *cs, qboolean force ) { + cast_script_stack_t *stack; + + if ( !aicast_scripts.integer ) { + return qtrue; + } + + if ( cs->castScriptStatus.castScriptEventIndex < 0 ) { + return qtrue; + } + + if ( !cs->castScriptEvents ) { + cs->castScriptStatus.castScriptEventIndex = -1; + return qtrue; + } + + // only allow the PLAYER'S spawn function through if we're NOT still waiting on everything to finish loading in + if ( !cs->entityNum && saveGamePending && Q_stricmp( "spawn", scriptEvents[cs->castScriptEvents[cs->castScriptStatus.castScriptEventIndex].eventNum].eventStr ) ) { + //char loading[4]; + //trap_Cvar_VariableStringBuffer( "savegame_loading", loading, sizeof(loading) ); + //if (strlen( loading ) > 0 && atoi(loading) != 0) // we're loading a savegame + return qfalse; + } + + if ( !force && ( cs->scriptPauseTime >= level.time ) ) { + return qtrue; + } + + stack = &cs->castScriptEvents[cs->castScriptStatus.castScriptEventIndex].stack; + + if ( !stack->numItems ) { + cs->castScriptStatus.castScriptEventIndex = -1; + return qtrue; + } + + while ( cs->castScriptStatus.castScriptStackHead < stack->numItems ) + { + // + // show debugging info + if ( ( cs->castScriptStatus.castScriptStackChangeTime == level.time ) && + ( ( aicast_debug.integer == 1 ) || + ( ( aicast_debug.integer == 2 ) && + ( ( strlen( aicast_debugname.string ) < 1 ) || ( g_entities[cs->entityNum].aiName && !strcmp( aicast_debugname.string, g_entities[cs->entityNum].aiName ) ) ) ) ) ) { + G_Printf( "(%s) AIScript command: %s %s\n", g_entities[cs->entityNum].aiName, stack->items[cs->castScriptStatus.castScriptStackHead].action->actionString, ( stack->items[cs->castScriptStatus.castScriptStackHead].params ? stack->items[cs->castScriptStatus.castScriptStackHead].params : "" ) ); + } + // + if ( !stack->items[cs->castScriptStatus.castScriptStackHead].action->actionFunc( cs, stack->items[cs->castScriptStatus.castScriptStackHead].params ) ) { + // check that we are still running the same script that we were when we call the action + if ( cs->castScriptStatus.castScriptEventIndex >= 0 && stack == &cs->castScriptEvents[cs->castScriptStatus.castScriptEventIndex].stack ) { + cs->castScriptStatus.scriptFlags &= ~SFL_FIRST_CALL; + } + return qfalse; + } + // move to the next action in the script + cs->castScriptStatus.castScriptStackHead++; + // record the time that this new item became active + cs->castScriptStatus.castScriptStackChangeTime = level.time; + // reset misc stuff + cs->castScriptStatus.scriptGotoId = -1; + cs->castScriptStatus.scriptGotoEnt = -1; + cs->castScriptStatus.scriptFlags |= SFL_FIRST_CALL; + } + + cs->castScriptStatus.castScriptEventIndex = -1; + + return qtrue; +} diff --git a/src/game/ai_cast_script_actions.c b/src/game/ai_cast_script_actions.c new file mode 100644 index 0000000..746e6e5 --- /dev/null +++ b/src/game/ai_cast_script_actions.c @@ -0,0 +1,3108 @@ +/* +=========================================================================== + +Return to Castle Wolfenstein single player GPL Source Code +Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. + +This file is part of the Return to Castle Wolfenstein single player GPL Source Code (“RTCW SP Source Code”). + +RTCW SP Source Code 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 3 of the License, or +(at your option) any later version. + +RTCW SP Source Code 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 RTCW SP Source Code. If not, see . + +In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ + +//=========================================================================== +// +// Name: ai_cast_script_actions.c +// Function: Wolfenstein AI Character Scripting +// Programmer: Ridah +// Tab Size: 4 (real tabs) +//=========================================================================== + +#include "../game/g_local.h" +#include "../game/q_shared.h" +#include "../game/botlib.h" //bot lib interface +#include "../game/be_aas.h" +#include "../game/be_ea.h" +#include "../game/be_ai_gen.h" +#include "../game/be_ai_goal.h" +#include "../game/be_ai_move.h" +#include "../botai/botai.h" //bot ai interface + +#include "ai_cast.h" + +/* +Contains the code to handle the various commands available with an event script. + +These functions will return true if the action has been performed, and the script +should proceed to the next item on the list. +*/ + +/* +=============== +AICast_NoAttackIfNotHurtSinceLastScriptAction + + Not an actual command, this is just used by the script code +=============== +*/ +void AICast_NoAttackIfNotHurtSinceLastScriptAction( cast_state_t *cs ) { + if ( cs->castScriptStatus.scriptNoAttackTime > level.time ) { + return; + } + + // if not moving, we should attack + if ( VectorLength( cs->bs->velocity ) < 10 ) { + return; + } + + // if our enemy is in the direction we are moving, don't hold back + if ( cs->enemyNum >= 0 && cs->castScriptStatus.scriptGotoEnt >= 0 ) { + vec3_t v; + + VectorSubtract( g_entities[cs->enemyNum].r.currentOrigin, cs->bs->origin, v ); + if ( DotProduct( cs->bs->velocity, v ) > 0 ) { + return; + } + } + + if ( cs->lastPain < cs->castScriptStatus.castScriptStackChangeTime ) { + cs->castScriptStatus.scriptNoAttackTime = level.time + FRAMETIME; + } +} + +/* +=============== +AICast_ScriptAction_GotoMarker + + syntax: gotomarker [firetarget [noattack]] [nostop] OR runtomarker [firetarget [noattack]] [nostop] +=============== +*/ +qboolean AICast_ScriptAction_GotoMarker( cast_state_t *cs, char *params ) { +#define SCRIPT_REACHGOAL_DIST 8 + char *pString, *token; + gentity_t *ent; + vec3_t vec, org; + int i, diff; + qboolean slowApproach; + + ent = NULL; + + // if we are avoiding danger, then wait for the danger to pass + if ( cs->castScriptStatus.scriptGotoId < 0 && cs->dangerEntityValidTime > level.time ) { + return qfalse; + } + // if we are in a special func, then wait + if ( cs->aiFlags & AIFL_SPECIAL_FUNC ) { + return qfalse; + } + + pString = params; + token = COM_ParseExt( &pString, qfalse ); + if ( !token[0] ) { + G_Error( "AI scripting: gotomarker must have an targetname\n" ); + } + + // if we already are going to the marker, just use that, and check if we're in range + if ( cs->castScriptStatus.scriptGotoEnt >= 0 && cs->castScriptStatus.scriptGotoId == cs->thinkFuncChangeTime ) { + ent = &g_entities[cs->castScriptStatus.scriptGotoEnt]; + if ( ent->targetname && !Q_strcasecmp( ent->targetname, token ) ) { + // if we're not slowing down, then check for passing the marker, otherwise check distance only + VectorSubtract( ent->r.currentOrigin, cs->bs->origin, vec ); + // + if ( cs->followSlowApproach && VectorLength( vec ) < cs->followDist ) { + cs->followTime = 0; + AIFunc_IdleStart( cs ); // resume normal AI + return qtrue; + } else if ( !cs->followSlowApproach && VectorLength( vec ) < 64 /*&& DotProduct(cs->bs->cur_ps.velocity, vec) < 0*/ ) { + cs->followTime = 0; + AIFunc_IdleStart( cs ); // resume normal AI + return qtrue; + } else + { + // do we have a firetarget ? + token = COM_ParseExt( &pString, qfalse ); + if ( !token[0] || !Q_stricmp( token,"nostop" ) ) { + AICast_NoAttackIfNotHurtSinceLastScriptAction( cs ); + } else { // yes we do + // find this targetname + ent = G_Find( NULL, FOFS( targetname ), token ); + if ( !ent ) { + ent = AICast_FindEntityForName( token ); + if ( !ent ) { + G_Error( "AI Scripting: gotomarker cannot find targetname \"%s\"\n", token ); + } + } + // set the view angle manually + BG_EvaluateTrajectory( &ent->s.pos, level.time, org ); + VectorSubtract( org, cs->bs->origin, vec ); + VectorNormalize( vec ); + vectoangles( vec, cs->ideal_viewangles ); + // noattack? + token = COM_ParseExt( &pString, qfalse ); + if ( !token[0] || Q_stricmp( token,"noattack" ) ) { + qboolean fire = qtrue; + // if it's an AI, and they aren't visible, dont shoot + if ( ent->r.svFlags & SVF_CASTAI ) { + if ( cs->vislist[ent->s.number].real_visible_timestamp != cs->vislist[ent->s.number].lastcheck_timestamp ) { + fire = qfalse; + } + } + if ( fire ) { + for ( i = 0; i < 2; i++ ) { + diff = abs( AngleDifference( cs->viewangles[i], cs->ideal_viewangles[i] ) ); + if ( diff < 20 ) { + // dont reload prematurely + cs->noReloadTime = level.time + 1000; + // force fire + trap_EA_Attack( cs->bs->client ); + // + cs->bFlags |= BFL_ATTACKED; + // dont reload prematurely + cs->noReloadTime = level.time + 200; + } + } + } + } + } + cs->followTime = level.time + 500; + return qfalse; + } + } else + { + ent = NULL; + } + } + + // find the ai_marker with the given "targetname" + // TTimo: gcc: suggest () around assignment used as truth value + while ( ( ent = G_Find( ent, FOFS( classname ), "ai_marker" ) ) ) + { + if ( ent->targetname && !Q_strcasecmp( ent->targetname, token ) ) { + break; + } + } + + if ( !ent ) { + G_Error( "AI Scripting: can't find ai_marker with \"targetname\" = \"%s\"\n", token ); + } + + if ( Distance( cs->bs->origin, ent->r.currentOrigin ) < SCRIPT_REACHGOAL_DIST ) { // we made it + return qtrue; + } + + cs->castScriptStatus.scriptNoMoveTime = 0; + cs->castScriptStatus.scriptGotoEnt = ent->s.number; + // + // slow approach to the goal? + if ( !params || !strstr( params," nostop" ) ) { + slowApproach = qtrue; + } else { + slowApproach = qfalse; + } + // + AIFunc_ChaseGoalStart( cs, ent->s.number, ( slowApproach ? SCRIPT_REACHGOAL_DIST : 32 ), slowApproach ); + cs->followIsGoto = qtrue; + cs->followTime = 0x7fffffff; // make sure it gets through for the first frame + cs->castScriptStatus.scriptGotoId = cs->thinkFuncChangeTime; + + AICast_NoAttackIfNotHurtSinceLastScriptAction( cs ); + return qfalse; +} + +/* +=============== +AICast_ScriptAction_WalkToMarker + + syntax: walktomarker [firetarget [noattack]] [nostop] +=============== +*/ +qboolean AICast_ScriptAction_WalkToMarker( cast_state_t *cs, char *params ) { + // if we are avoiding danger, then wait for the danger to pass + if ( cs->castScriptStatus.scriptGotoId < 0 && cs->dangerEntityValidTime > level.time ) { + return qfalse; + } + // if we are in a special func, then wait + if ( cs->aiFlags & AIFL_SPECIAL_FUNC ) { + return qfalse; + } + if ( !AICast_ScriptAction_GotoMarker( cs, params ) || ( !strstr( params, " nostop" ) && VectorLength( cs->bs->cur_ps.velocity ) ) ) { + cs->movestate = MS_WALK; + cs->movestateType = MSTYPE_TEMPORARY; + AICast_NoAttackIfNotHurtSinceLastScriptAction( cs ); + return qfalse; + } + + return qtrue; +} + + +/* +=============== +AICast_ScriptAction_CrouchToMarker + + syntax: crouchtomarker [firetarget [noattack]] [nostop] +=============== +*/ +qboolean AICast_ScriptAction_CrouchToMarker( cast_state_t *cs, char *params ) { + // if we are avoiding danger, then wait for the danger to pass + if ( cs->castScriptStatus.scriptGotoId < 0 && cs->dangerEntityValidTime > level.time ) { + return qfalse; + } + // if we are in a special func, then wait + if ( cs->aiFlags & AIFL_SPECIAL_FUNC ) { + return qfalse; + } + if ( !AICast_ScriptAction_GotoMarker( cs, params ) || ( !strstr( params, " nostop" ) && VectorLength( cs->bs->cur_ps.velocity ) ) ) { + cs->movestate = MS_CROUCH; + cs->movestateType = MSTYPE_TEMPORARY; + AICast_NoAttackIfNotHurtSinceLastScriptAction( cs ); + return qfalse; + } + + return qtrue; +} + +/* +=============== +AICast_ScriptAction_GotoCast + + syntax: gotocast [firetarget [noattack]] OR runtocast [firetarget [noattack]] +=============== +*/ +qboolean AICast_ScriptAction_GotoCast( cast_state_t *cs, char *params ) { +#define SCRIPT_REACHCAST_DIST 64 + char *pString, *token; + gentity_t *ent; + vec3_t vec, org; + int i, diff; + + ent = NULL; + + // if we are avoiding danger, then wait for the danger to pass + if ( cs->castScriptStatus.scriptGotoId < 0 && cs->dangerEntityValidTime > level.time ) { + return qfalse; + } + // if we are in a special func, then wait + if ( cs->aiFlags & AIFL_SPECIAL_FUNC ) { + return qfalse; + } + + pString = params; + token = COM_ParseExt( &pString, qfalse ); + if ( !token[0] ) { + G_Error( "AI scripting: gotocast must have an ainame\n" ); + } + + // if we already are going to the marker, just use that, and check if we're in range + if ( cs->castScriptStatus.scriptGotoEnt >= 0 && cs->castScriptStatus.scriptGotoId == cs->thinkFuncChangeTime ) { + ent = &g_entities[cs->castScriptStatus.scriptGotoEnt]; + if ( ent->targetname && !Q_strcasecmp( ent->targetname, token ) ) { + if ( Distance( cs->bs->origin, ent->r.currentOrigin ) < cs->followDist ) { + cs->followTime = 0; + AIFunc_IdleStart( cs ); // resume normal AI + return qtrue; + } else + { + // do we have a firetarget ? + token = COM_ParseExt( &pString, qfalse ); + if ( !token[0] ) { + AICast_NoAttackIfNotHurtSinceLastScriptAction( cs ); + } else { // yes we do + // find this targetname + ent = G_Find( NULL, FOFS( targetname ), token ); + if ( !ent ) { + ent = AICast_FindEntityForName( token ); + if ( !ent ) { + G_Error( "AI Scripting: gotomarker cannot find targetname \"%s\"\n", token ); + } + } + + // set the view angle manually + BG_EvaluateTrajectory( &ent->s.pos, level.time, org ); + VectorSubtract( org, cs->bs->origin, vec ); + VectorNormalize( vec ); + vectoangles( vec, cs->ideal_viewangles ); + // noattack? + token = COM_ParseExt( &pString, qfalse ); + if ( !token[0] || Q_stricmp( token,"noattack" ) ) { + qboolean fire = qtrue; + // if it's an AI, and they aren't visible, dont shoot + if ( ent->r.svFlags & SVF_CASTAI ) { + if ( cs->vislist[ent->s.number].real_visible_timestamp != cs->vislist[ent->s.number].lastcheck_timestamp ) { + fire = qfalse; + } + } + if ( fire ) { + for ( i = 0; i < 2; i++ ) { + diff = abs( AngleDifference( cs->viewangles[i], cs->ideal_viewangles[i] ) ); + if ( diff < 20 ) { + // dont reload prematurely + cs->noReloadTime = level.time + 1000; + // force fire + trap_EA_Attack( cs->bs->client ); + // + cs->bFlags |= BFL_ATTACKED; + // dont reload prematurely + cs->noReloadTime = level.time + 200; + } + } + } + } + } + cs->followTime = level.time + 500; + return qfalse; + } + } else + { + ent = NULL; + } + } + + // find the cast/player with the given "name" + ent = AICast_FindEntityForName( token ); + if ( !ent ) { + G_Error( "AI Scripting: can't find AI cast with \"ainame\" = \"%s\"\n", token ); + } + + if ( Distance( cs->bs->origin, ent->r.currentOrigin ) < SCRIPT_REACHCAST_DIST ) { // we made it + return qtrue; + } + + if ( !ent ) { + G_Error( "AI Scripting: can't find ai_marker with \"targetname\" = \"%s\"\n", token ); + } + + cs->castScriptStatus.scriptNoMoveTime = 0; + cs->castScriptStatus.scriptGotoEnt = ent->s.number; + // + AIFunc_ChaseGoalStart( cs, ent->s.number, SCRIPT_REACHCAST_DIST, qtrue ); + cs->followTime = 0x7fffffff; + AICast_NoAttackIfNotHurtSinceLastScriptAction( cs ); + cs->castScriptStatus.scriptGotoId = cs->thinkFuncChangeTime; + + return qfalse; +} + +/* +=============== +AICast_ScriptAction_WalkToCast + + syntax: walktocast [firetarget [noattack]] +=============== +*/ +qboolean AICast_ScriptAction_WalkToCast( cast_state_t *cs, char *params ) { + // if we are avoiding danger, then wait for the danger to pass + if ( cs->castScriptStatus.scriptGotoId < 0 && cs->dangerEntityValidTime > level.time ) { + return qfalse; + } + // if we are in a special func, then wait + if ( cs->aiFlags & AIFL_SPECIAL_FUNC ) { + return qfalse; + } + if ( !AICast_ScriptAction_GotoCast( cs, params ) ) { + cs->movestate = MS_WALK; + cs->movestateType = MSTYPE_TEMPORARY; + AICast_NoAttackIfNotHurtSinceLastScriptAction( cs ); + return qfalse; + } + + return qtrue; +} + + +/* +=============== +AICast_ScriptAction_CrouchToCast + + syntax: crouchtocast [firetarget [noattack]] +=============== +*/ +qboolean AICast_ScriptAction_CrouchToCast( cast_state_t *cs, char *params ) { + // if we are avoiding danger, then wait for the danger to pass + if ( cs->castScriptStatus.scriptGotoId < 0 && cs->dangerEntityValidTime > level.time ) { + return qfalse; + } + // if we are in a special func, then wait + if ( cs->aiFlags & AIFL_SPECIAL_FUNC ) { + return qfalse; + } + if ( !AICast_ScriptAction_GotoCast( cs, params ) ) { + cs->movestate = MS_CROUCH; + cs->movestateType = MSTYPE_TEMPORARY; + AICast_NoAttackIfNotHurtSinceLastScriptAction( cs ); + return qfalse; + } + + return qtrue; +} + + +/* +============== +AICast_ScriptAction_AbortIfLoadgame +============== +*/ +qboolean AICast_ScriptAction_AbortIfLoadgame( cast_state_t *cs, char *params ) { + char loading[4]; + + trap_Cvar_VariableStringBuffer( "savegame_loading", loading, sizeof( loading ) ); + + if ( strlen( loading ) > 0 && atoi( loading ) != 0 ) { + // abort the current script + cs->castScriptStatus.castScriptStackHead = cs->castScriptEvents[cs->castScriptStatus.castScriptEventIndex].stack.numItems; + } + + return qtrue; +} + + +/* +================= +AICast_ScriptAction_Wait + + syntax: wait [moverange] [facetarget] + + moverange defaults to 200, allows some monouverability to avoid fire or attack +================= +*/ +qboolean AICast_ScriptAction_Wait( cast_state_t *cs, char *params ) { + char *pString, *token, *facetarget; + int duration; + float moverange; + float dist; + gentity_t *ent; + vec3_t org, vec; + + // if we are in a special func, then wait + if ( cs->aiFlags & AIFL_SPECIAL_FUNC ) { + return qfalse; + } + // EXPERIMENTAL: if they are on fire, or avoiding danger, let them loose until it passes (or they die) + if ( cs->dangerEntityValidTime > level.time ) { + cs->castScriptStatus.scriptNoMoveTime = -1; + return qfalse; + } + + if ( ( cs->castScriptStatus.scriptFlags & SFL_FIRST_CALL ) && cs->bs ) { + // first call, init the waitPos + VectorCopy( cs->bs->origin, cs->castScriptStatus.scriptWaitPos ); + } + + // get the duration + pString = params; + token = COM_ParseExt( &pString, qfalse ); + if ( !token[0] ) { + G_Error( "AI scripting: wait must have a duration\n" ); + } + if ( !Q_stricmp( token, "forever" ) ) { + duration = level.time + 10000; + } else { + duration = atoi( token ); + } + + // if this is for the player, don't worry about enforcing the moverange + if ( !cs->bs ) { + return ( cs->castScriptStatus.castScriptStackChangeTime + duration < level.time ); + } + + token = COM_ParseExt( &pString, qfalse ); + // if this token is a number, then assume it is the moverange, otherwise we have a default moverange with a facetarget + moverange = -999; + facetarget = NULL; + if ( token[0] ) { + if ( toupper( token[0] ) >= 'A' && toupper( token[0] ) <= 'Z' ) { + facetarget = token; + } else { // we found a moverange + moverange = atof( token ); + token = COM_ParseExt( &pString, qfalse ); + if ( token[0] ) { + facetarget = token; + } + } + } + + // default to no moverange + //if (moverange == -999) { + // moverange = 200; + //} + + if ( moverange != 0 ) { // default to 200 if no range given + if ( moverange > 0 ) { + dist = Distance( cs->bs->origin, cs->castScriptStatus.scriptWaitPos ); + // if we are able to move, and have an enemy + if ( ( cs->castScriptStatus.scriptWaitMovetime < level.time ) + && ( cs->enemyNum >= 0 ) ) { + + // if we can attack them, or they can't attack us, stay here + // TTimo: gcc: warning: suggest parentheses around && within || + if ( AICast_CheckAttack( cs, cs->enemyNum, qfalse ) + || ( !AICast_EntityVisible( AICast_GetCastState( cs->enemyNum ), cs->entityNum, qfalse ) + && !AICast_CheckAttack( AICast_GetCastState( cs->enemyNum ), cs->entityNum, qfalse ) ) ) { + cs->castScriptStatus.scriptNoMoveTime = level.time + 200; + } + + } + // if outside range, move towards waitPos + if ( ( !cs->bs || !cs->bs->cur_ps.legsTimer ) && ( ( ( cs->castScriptStatus.scriptWaitMovetime > level.time ) && ( dist > 32 ) ) || ( dist > moverange ) ) ) { + cs->castScriptStatus.scriptNoMoveTime = 0; + AICast_MoveToPos( cs, cs->castScriptStatus.scriptWaitPos, 0 ); + if ( dist > 64 ) { + cs->castScriptStatus.scriptWaitMovetime = level.time + 600; + } + } else + // if we are reloading, look for somewhere to hide + if ( cs->castScriptStatus.scriptWaitHideTime > level.time || cs->bs->cur_ps.weaponTime > 500 ) { + if ( cs->castScriptStatus.scriptWaitHideTime < level.time ) { + // look for a hide pos within the wait range + + } + cs->castScriptStatus.scriptWaitHideTime = level.time + 500; + } + } + } else { + cs->castScriptStatus.scriptNoMoveTime = cs->castScriptStatus.castScriptStackChangeTime + duration; + } + + // do we have a facetarget ? + if ( facetarget ) { // yes we do + // find this targetname + ent = G_Find( NULL, FOFS( targetname ), facetarget ); + if ( !ent ) { + ent = AICast_FindEntityForName( facetarget ); + if ( !ent ) { + G_Error( "AI Scripting: wait cannot find targetname \"%s\"\n", token ); + } + } + // set the view angle manually + BG_EvaluateTrajectory( &ent->s.pos, level.time, org ); + VectorSubtract( org, cs->bs->origin, vec ); + VectorNormalize( vec ); + vectoangles( vec, cs->ideal_viewangles ); + } + + return ( cs->castScriptStatus.castScriptStackChangeTime + duration < level.time ); +} + +/* +================= +AICast_ScriptAction_Trigger + + syntax: trigger + + Calls the specified trigger for the given ai character +================= +*/ +qboolean AICast_ScriptAction_Trigger( cast_state_t *cs, char *params ) { + gentity_t *ent; + char *pString, *token; + int oldId; + + // get the cast name + pString = params; + token = COM_ParseExt( &pString, qfalse ); + if ( !token[0] ) { + G_Error( "AI scripting: trigger must have a name and an identifier\n" ); + } + + ent = AICast_FindEntityForName( token ); + if ( !ent ) { + ent = G_Find( &g_entities[MAX_CLIENTS], FOFS( scriptName ), token ); + if ( !ent ) { + if ( trap_Cvar_VariableIntegerValue( "developer" ) ) { + G_Printf( "AI Scripting: can't find AI cast with \"ainame\" = \"%s\"\n", params ); + } + return qtrue; + } + } + + token = COM_ParseExt( &pString, qfalse ); + if ( !token[0] ) { + G_Error( "AI scripting: trigger must have a name and an identifier\n" ); + } + + oldId = cs->castScriptStatus.scriptId; + if ( ent->client ) { + AICast_ScriptEvent( AICast_GetCastState( ent->s.number ), "trigger", token ); + } else { + G_Script_ScriptEvent( ent, "trigger", token ); + } + + // if the script changed, return false so we don't muck with it's variables + return ( oldId == cs->castScriptStatus.scriptId ); +} + +/* +=================== +AICast_ScriptAction_FollowCast + + syntax: followcast +=================== +*/ +qboolean AICast_ScriptAction_FollowCast( cast_state_t *cs, char *params ) { + gentity_t *ent; + + // find the cast/player with the given "name" + ent = AICast_FindEntityForName( params ); + if ( !ent ) { + G_Error( "AI Scripting: can't find AI cast with \"ainame\" = \"%s\"\n", params ); + } + + AIFunc_ChaseGoalStart( cs, ent->s.number, 64, qtrue ); + + return qtrue; +}; + +/* +================ +AICast_ScriptAction_PlaySound + + syntax: playsound + + Currently only allows playing on the VOICE channel, unless you use a sound script (yay) +================ +*/ +qboolean AICast_ScriptAction_PlaySound( cast_state_t *cs, char *params ) { + if ( !params ) { + G_Error( "AI Scripting: syntax error\n\nplaysound \n" ); + } + + G_AddEvent( &g_entities[cs->bs->entitynum], EV_GENERAL_SOUND, G_SoundIndex( params ) ); + + // assume we are talking + cs->aiFlags |= AIFL_TALKING; + + // randomly choose idle animation + if ( cs->aiFlags & AIFL_STAND_IDLE2 ) { + if ( cs->lastEnemy < 0 && cs->aiFlags & AIFL_TALKING ) { + g_entities[cs->entityNum].client->ps.eFlags |= EF_STAND_IDLE2; + } else { + g_entities[cs->entityNum].client->ps.eFlags &= ~EF_STAND_IDLE2; + } + } + + return qtrue; +} + +/* +================= +AICast_ScriptAction_NoAttack + + syntax: noattack +================= +*/ +qboolean AICast_ScriptAction_NoAttack( cast_state_t *cs, char *params ) { + if ( !params ) { + G_Error( "AI Scripting: syntax error\n\nnoattack \n" ); + } + + cs->castScriptStatus.scriptNoAttackTime = level.time + atoi( params ); + + return qtrue; +} + +/* +================= +AICast_ScriptAction_Attack + + syntax: attack [ainame] + + Resumes attacking after a noattack was issued + + if ainame is given, we will attack only that entity as long as they are alive +================= +*/ +qboolean AICast_ScriptAction_Attack( cast_state_t *cs, char *params ) { + gentity_t *ent; + + cs->castScriptStatus.scriptNoAttackTime = 0; + + // if we have specified an aiName, then we should attack only this person + if ( params ) { + ent = AICast_FindEntityForName( params ); + if ( !ent ) { + G_Error( "AI Scripting: \"attack\" command unable to find aiName \"%s\"", params ); + } + cs->castScriptStatus.scriptAttackEnt = ent->s.number; + cs->enemyNum = ent->s.number; + } else { + cs->castScriptStatus.scriptAttackEnt = -1; + } + + return qtrue; +} + +/* +================= +AICast_ScriptAction_PlayAnim + + syntax: playanim [legs/torso/both] [HOLDFRAME] [numLoops/FOREVER] [target] + + NOTE: any new animations that are needed by the scripting system, will need to be added here +================= +*/ +qboolean AICast_ScriptAction_PlayAnim( cast_state_t *cs, char *params ) { + char *pString, *token, tokens[3][MAX_QPATH]; + int i, endtime, duration, numLoops; + gclient_t *client; + gentity_t *ent; + vec3_t org, vec; + qboolean forever = qfalse, setAngles = qfalse; + qboolean holdframe = qfalse; + + pString = params; + + client = &level.clients[cs->entityNum]; + + if ( level.animScriptData.modelInfo[level.animScriptData.clientModels[cs->entityNum] - 1]->version > 1 ) { // new (scripted) model + + // read the name + token = COM_ParseExt( &pString, qfalse ); + if ( !token || !token[0] ) { + G_Error( "AI Scripting: syntax error\n\nplayanim \n" ); + } + Q_strncpyz( tokens[0], token, sizeof( tokens[0] ) ); + Q_strlwr( tokens[0] ); + + // read the body part + token = COM_ParseExt( &pString, qfalse ); + if ( !token || !token[0] ) { + G_Error( "AI Scripting: syntax error\n\nplayanim \n" ); + } + Q_strncpyz( tokens[1], token, sizeof( tokens[1] ) ); + Q_strlwr( tokens[1] ); + + // read the HOLDFRAME (optional) + token = COM_ParseExt( &pString, qfalse ); + if ( token && token[0] && !Q_strcasecmp( token, "holdframe" ) ) { + holdframe = qtrue; + // read the numLoops (optional) + token = COM_ParseExt( &pString, qfalse ); + } + // token is parsed above + if ( token && token[0] ) { + if ( !Q_strcasecmp( token, "forever" ) ) { + forever = qtrue; + numLoops = -1; + // read the target (optional) + token = COM_ParseExt( &pString, qfalse ); + } else { + numLoops = atoi( token ); + if ( !numLoops ) { // must be the target, so set loops to 1 + numLoops = 1; + } else { + // read the target (optional) + token = COM_ParseExt( &pString, qfalse ); + } + } + + if ( token && token[0] ) { + // find this targetname + ent = G_Find( NULL, FOFS( targetname ), token ); + if ( !ent ) { + ent = AICast_FindEntityForName( token ); + if ( !ent ) { + G_Error( "AI Scripting: playanim cannot find targetname \"%s\"\n", token ); + } + } + // set the view angle manually + BG_EvaluateTrajectory( &ent->s.pos, level.time, org ); + VectorSubtract( org, cs->bs->origin, vec ); + VectorNormalize( vec ); + vectoangles( vec, cs->ideal_viewangles ); + VectorCopy( cs->ideal_viewangles, cs->castScriptStatus.playanim_viewangles ); + setAngles = qtrue; + } + + } else { + numLoops = 1; + } + + if ( cs->castScriptStatus.scriptFlags & SFL_FIRST_CALL ) { + // first time in here, play the anim + duration = BG_PlayAnimName( &( client->ps ), tokens[0], BG_IndexForString( tokens[1], animBodyPartsStr, qfalse ), qtrue, qfalse, qtrue ); + if ( numLoops == -1 ) { + cs->scriptAnimTime = 0x7fffffff; // maximum time allowed + } else { + cs->scriptAnimTime = level.time + ( numLoops * duration ); + } + if ( !strcmp( tokens[1], "torso" ) ) { + cs->scriptAnimNum = client->ps.torsoAnim & ~ANIM_TOGGLEBIT; + // adjust the duration according to numLoops + if ( numLoops > 1 ) { + client->ps.torsoTimer += duration * numLoops; + } else if ( numLoops == -1 ) { + client->ps.torsoTimer = 9999; + } + } else { // dont move + cs->scriptAnimNum = client->ps.legsAnim & ~ANIM_TOGGLEBIT; + cs->castScriptStatus.scriptNoMoveTime = cs->scriptAnimTime; + // lock the viewangles + if ( !cs->castScriptStatus.playAnimViewlockTime || cs->castScriptStatus.playAnimViewlockTime < level.time ) { + VectorCopy( cs->ideal_viewangles, cs->castScriptStatus.playanim_viewangles ); + } + cs->castScriptStatus.playAnimViewlockTime = cs->scriptAnimTime; + // adjust the duration according to numLoops + if ( numLoops > 1 ) { + client->ps.legsTimer += duration * ( numLoops - 1 ); + if ( !strcmp( tokens[1], "both" ) ) { + client->ps.torsoTimer += duration * ( numLoops - 1 ); + } + } else if ( numLoops == -1 ) { + client->ps.legsTimer = 9999; + if ( !strcmp( tokens[1], "both" ) ) { + client->ps.torsoTimer = 9999; + } + } + } + } else { + if ( holdframe ) { + // make sure it doesn't stop before the next command can be processed + if ( !strcmp( tokens[1], "torso" ) ) { + if ( client->ps.torsoTimer < 400 ) { + client->ps.torsoTimer = 400; + } + } else if ( !strcmp( tokens[1], "legs" ) ) { + if ( client->ps.legsTimer < 400 ) { + client->ps.legsTimer = 400; + } + } else if ( !strcmp( tokens[1], "both" ) ) { + if ( client->ps.torsoTimer < 400 ) { + client->ps.torsoTimer = 400; + } + if ( client->ps.legsTimer < 400 ) { + client->ps.legsTimer = 400; + } + } + } + // keep it looping if forever + if ( forever ) { + if ( !strcmp( tokens[1], "torso" ) ) { + client->ps.torsoTimer = 9999; + } else { + client->ps.legsTimer = 9999; + if ( !strcmp( tokens[1], "both" ) ) { + client->ps.torsoTimer = 9999; + } + } + return qfalse; + } + // wait for the anim to stop playing + if ( cs->scriptAnimTime <= level.time ) { + return qtrue; + } + } + + return qfalse; + + } else { // old model + + for ( i = 0; i < 3; i++ ) { + token = COM_ParseExt( &pString, qfalse ); + if ( !token || !token[0] ) { + //G_Error("AI Scripting: syntax error\n\nplayanim [legs/torso/both]\n"); + G_Printf( "AI Scripting: syntax error\n\nplayanim \n" ); + return qtrue; + } else { + Q_strncpyz( tokens[i], token, sizeof( tokens[i] ) ); + } + } + + Q_strlwr( tokens[2] ); + + endtime = cs->castScriptStatus.castScriptStackChangeTime + atoi( tokens[1] ); + duration = endtime - level.time + 200; // so animations don't run out before starting a next animation + if ( duration > 2000 ) { + duration = 2000; + } + + cs->scriptAnimTime = level.time; + + if ( duration < 200 ) { + return qtrue; // done playing animation + + } + // call the anims directly based on the animation token + + //if (cs->castScriptStatus.castScriptStackChangeTime == level.time) { + for ( i = 0; i < MAX_ANIMATIONS; i++ ) { + if ( !Q_strcasecmp( tokens[0], animStrings[i] ) ) { + if ( !Q_strcasecmp( tokens[2],"torso" ) ) { + if ( ( client->ps.torsoAnim & ~ANIM_TOGGLEBIT ) != i ) { + client->ps.torsoAnim = ( ( client->ps.torsoAnim & ANIM_TOGGLEBIT ) ^ ANIM_TOGGLEBIT ) | i; + } + client->ps.torsoTimer = duration; + } else if ( !Q_strcasecmp( tokens[2],"legs" ) ) { + if ( ( client->ps.legsAnim & ~ANIM_TOGGLEBIT ) != i ) { + client->ps.legsAnim = ( ( client->ps.legsAnim & ANIM_TOGGLEBIT ) ^ ANIM_TOGGLEBIT ) | i; + } + client->ps.legsTimer = duration; + } else if ( !Q_strcasecmp( tokens[2],"both" ) ) { + if ( ( client->ps.torsoAnim & ~ANIM_TOGGLEBIT ) != i ) { + client->ps.torsoAnim = ( ( client->ps.torsoAnim & ANIM_TOGGLEBIT ) ^ ANIM_TOGGLEBIT ) | i; + } + client->ps.torsoTimer = duration; + if ( ( client->ps.legsAnim & ~ANIM_TOGGLEBIT ) != i ) { + client->ps.legsAnim = ( ( client->ps.legsAnim & ANIM_TOGGLEBIT ) ^ ANIM_TOGGLEBIT ) | i; + } + client->ps.legsTimer = duration; + } else { + G_Printf( "AI Scripting: syntax error\n\nplayanim \n" ); + } + break; + } + } + if ( i == MAX_ANIMATIONS ) { + G_Printf( "AI Scripting: playanim has unknown or invalid animation \"%s\"\n", tokens[0] ); + } + //} + + if ( !strcmp( tokens[2], "torso" ) ) { + cs->scriptAnimNum = client->ps.torsoAnim & ~ANIM_TOGGLEBIT; + } else { + cs->scriptAnimNum = client->ps.legsAnim & ~ANIM_TOGGLEBIT; + } + + if ( cs->castScriptStatus.scriptNoMoveTime < level.time + 300 ) { + cs->castScriptStatus.scriptNoMoveTime = level.time + 300; + } + if ( cs->castScriptStatus.scriptNoAttackTime < level.time + 300 ) { + cs->castScriptStatus.scriptNoAttackTime = level.time + 300; + } + return qfalse; + } +}; + +/* +================= +AICast_ScriptAction_ClearAnim + + stops any animation that is currently playing +================= +*/ +qboolean AICast_ScriptAction_ClearAnim( cast_state_t *cs, char *params ) { + gclient_t *client; + + client = &level.clients[cs->entityNum]; + + client->ps.torsoTimer = 0; + client->ps.legsTimer = 0; + + // let us move again + cs->castScriptStatus.scriptNoMoveTime = 0; + + return qtrue; +} + +/* +================= +AICast_ScriptAction_SetAmmo + + syntax: setammo +================= +*/ +qboolean AICast_ScriptAction_SetAmmo( cast_state_t *cs, char *params ) { + char *pString, *token; + int weapon; + int i; + + pString = params; + + token = COM_ParseExt( &pString, qfalse ); + if ( !token[0] ) { + G_Error( "AI Scripting: setammo without ammo identifier\n" ); + } + + weapon = WP_NONE; + + for ( i = 1; bg_itemlist[i].classname; i++ ) + { + //----(SA) first try the name they see in the editor, then the pickup name + if ( !Q_strcasecmp( token, bg_itemlist[i].classname ) ) { + weapon = bg_itemlist[i].giTag; + break; + } + + if ( !Q_strcasecmp( token, bg_itemlist[i].pickup_name ) ) { + weapon = bg_itemlist[i].giTag; + break; + } + } + + token = COM_ParseExt( &pString, qfalse ); + if ( !token[0] ) { + G_Error( "AI Scripting: setammo without ammo count\n" ); + } + + if ( weapon != WP_NONE ) { + // give them the ammo + + if ( atoi( token ) ) { + int amt; + amt = atoi( token ); + if ( amt > 50 + ammoTable[BG_FindAmmoForWeapon( weapon )].maxammo ) { + amt = 999; // unlimited + } + Add_Ammo( &g_entities[cs->entityNum], weapon, amt, qtrue ); + } else { + // remove ammo for this weapon + g_entities[cs->entityNum].client->ps.ammo[BG_FindAmmoForWeapon( weapon )] = 0; + g_entities[cs->entityNum].client->ps.ammoclip[BG_FindClipForWeapon( weapon )] = 0; + } + + } else { + + if ( g_cheats.integer ) { + G_Printf( "--SCRIPTER WARNING-- AI Scripting: setammo: unknown ammo \"%s\"", params ); + } + return qfalse; // (SA) temp as scripts transition to new names + + } + + return qtrue; +}; + +/* +================= +AICast_ScriptAction_SetClip + + syntax: setclip + +================= +*/ +qboolean AICast_ScriptAction_SetClip( cast_state_t *cs, char *params ) { + char *pString, *token; + int weapon; + int i; + + pString = params; + + token = COM_ParseExt( &pString, qfalse ); + if ( !token[0] ) { + G_Error( "AI Scripting: setclip without weapon identifier\n" ); + } + + weapon = WP_NONE; + + for ( i = 1; bg_itemlist[i].classname; i++ ) + { + //----(SA) first try the name they see in the editor, then the pickup name + if ( !Q_strcasecmp( token, bg_itemlist[i].classname ) ) { + weapon = bg_itemlist[i].giTag; + break; + } + + if ( !Q_strcasecmp( token, bg_itemlist[i].pickup_name ) ) { + weapon = bg_itemlist[i].giTag; + break; + } + } + + token = COM_ParseExt( &pString, qfalse ); + if ( !token[0] ) { + G_Error( "AI Scripting: setclip without ammo count\n" ); + } + + if ( weapon != WP_NONE ) { + + int spillover = atoi( token ) - ammoTable[weapon].maxclip; + + if ( spillover > 0 ) { + // there was excess, put it in storage and fill the clip + g_entities[cs->entityNum].client->ps.ammo[BG_FindAmmoForWeapon( weapon )] += spillover; + g_entities[cs->entityNum].client->ps.ammoclip[BG_FindClipForWeapon( weapon )] = ammoTable[weapon].maxclip; + } else { + // set the clip amount to the exact specified value + g_entities[cs->entityNum].client->ps.ammoclip[weapon] = atoi( token ); + } + + } else { +// G_Printf( "--SCRIPTER WARNING-- AI Scripting: setclip: unknown weapon \"%s\"", params ); + return qfalse; // (SA) temp as scripts transition to new names +// G_Error( "AI Scripting: setclip: unknown weapon \"%s\"", params ); + } + + return qtrue; +}; + + + +/* +============== +AICast_ScriptAction_SuggestWeapon +============== +*/ +qboolean AICast_ScriptAction_SuggestWeapon( cast_state_t *cs, char *params ) { + int weapon; + int i; + //int suggestedweaps = 0; // TTimo: unused + + weapon = WP_NONE; + + for ( i = 1; bg_itemlist[i].classname; i++ ) + { + //----(SA) first try the name they see in the editor, then the pickup name + if ( !Q_strcasecmp( params, bg_itemlist[i].classname ) ) { + weapon = bg_itemlist[i].giTag; + break; + } + + if ( !Q_strcasecmp( params, bg_itemlist[i].pickup_name ) ) { + weapon = bg_itemlist[i].giTag; + break; + } + } + + if ( weapon != WP_NONE ) { + G_AddEvent( &g_entities[cs->entityNum], EV_SUGGESTWEAP, weapon ); + } else { + G_Error( "AI Scripting: suggestweapon: unknown weapon \"%s\"", params ); + } + + return qtrue; + +} + + +/* +================= +AICast_ScriptAction_SelectWeapon + + syntax: selectweapon +================= +*/ +qboolean AICast_ScriptAction_SelectWeapon( cast_state_t *cs, char *params ) { + int weapon; + int i; + + weapon = WP_NONE; + + for ( i = 1; bg_itemlist[i].classname; i++ ) + { + //----(SA) first try the name they see in the editor, then the pickup name + if ( !Q_strcasecmp( params, bg_itemlist[i].classname ) ) { + weapon = bg_itemlist[i].giTag; + break; + } + + if ( !Q_strcasecmp( params, bg_itemlist[i].pickup_name ) ) { + weapon = bg_itemlist[i].giTag; + break; + } + } + + if ( weapon != WP_NONE ) { + if ( cs->bs ) { + cs->weaponNum = weapon; + } + cs->castScriptStatus.scriptFlags |= SFL_NOCHANGEWEAPON; + + g_entities[cs->entityNum].client->ps.weapon = weapon; + g_entities[cs->entityNum].client->ps.weaponstate = WEAPON_READY; + + if ( !cs->aiCharacter ) { // only do this for player + //vmCvar_t cvar; + // + g_entities[cs->entityNum].client->ps.weaponTime = 750; // (SA) HACK: FIXME: TODO: delay to catch initial weapon reload + // tell it which weapon to use after spawning in + //trap_Cvar_Register( &cvar, "cg_loadWeaponSelect", "0", CVAR_ROM ); + //trap_Cvar_Set( "cg_loadWeaponSelect", va("%i", g_entities[cs->entityNum].client->ps.weapon ) ); + } + + } else { +// G_Printf( "--SCRIPTER WARNING-- AI Scripting: selectweapon: unknown weapon \"%s\"", params ); +// return qfalse; // (SA) temp as scripts transition to new names + G_Error( "AI Scripting: selectweapon: unknown weapon \"%s\"", params ); + } + + return qtrue; +}; + + + +//----(SA) added +/* +============== +AICast_ScriptAction_GiveArmor + syntax: setarmor + +============== +*/ +qboolean AICast_ScriptAction_SetArmor( cast_state_t *cs, char *params ) { + if ( !params || !params[0] ) { + G_Error( "AI Scripting: setarmor requires an armor value" ); + } + + g_entities[cs->entityNum].client->ps.stats[STAT_ARMOR] += atoi( params ); + + return qtrue; +} + +/* +============== +AICast_ScriptAction_GiveArmor + + syntax: givearmor + + will probably be more like: + syntax: givearmor +============== +*/ +qboolean AICast_ScriptAction_GiveArmor( cast_state_t *cs, char *params ) { + int i; + gitem_t *item = 0; + + for ( i = 1; bg_itemlist[i].classname; i++ ) { + //----(SA) first try the name they see in the editor, then the pickup name + if ( !Q_strcasecmp( params, bg_itemlist[i].classname ) ) { + item = &bg_itemlist[i]; + } + + if ( !Q_strcasecmp( params, bg_itemlist[i].pickup_name ) ) { + item = &bg_itemlist[i]; + } + } + + if ( !item ) { // item not found + G_Error( "AI Scripting: givearmor%s, unknown item", params ); + } + + if ( item->giType == IT_ARMOR ) { + g_entities[cs->entityNum].client->ps.stats[STAT_ARMOR] += item->quantity; + if ( g_entities[cs->entityNum].client->ps.stats[STAT_ARMOR] > 100 ) { + g_entities[cs->entityNum].client->ps.stats[STAT_ARMOR] = 100; + } + } + + return qtrue; +} +//----(SA) end + + + +/* +================= +AICast_ScriptAction_GiveWeapon + + syntax: giveweapon +================= +*/ +qboolean AICast_ScriptAction_GiveWeapon( cast_state_t *cs, char *params ) { + int weapon; + int i; + gentity_t *ent = &g_entities[cs->entityNum]; + + weapon = WP_NONE; + + for ( i = 1; bg_itemlist[i].classname; i++ ) + { + //----(SA) first try the name they see in the editor, then the pickup name + if ( !Q_strcasecmp( params, bg_itemlist[i].classname ) ) { + weapon = bg_itemlist[i].giTag; + break; + } + + if ( !Q_strcasecmp( params, bg_itemlist[i].pickup_name ) ) { + weapon = bg_itemlist[i].giTag; + } + } + + if ( weapon == WP_COLT ) { + // if you had the colt already, now you've got two! + if ( COM_BitCheck( g_entities[cs->entityNum].client->ps.weapons, WP_COLT ) ) { + weapon = WP_AKIMBO; + } + } + + if ( weapon != WP_NONE ) { + COM_BitSet( g_entities[cs->entityNum].client->ps.weapons, weapon ); + +//----(SA) some weapons always go together (and they share a clip, so this is okay) + if ( weapon == WP_GARAND ) { + COM_BitSet( g_entities[cs->entityNum].client->ps.weapons, WP_SNOOPERSCOPE ); + } + if ( weapon == WP_SNOOPERSCOPE ) { + COM_BitSet( g_entities[cs->entityNum].client->ps.weapons, WP_GARAND ); + } + if ( weapon == WP_FG42 ) { + COM_BitSet( g_entities[cs->entityNum].client->ps.weapons, WP_FG42SCOPE ); + } + if ( weapon == WP_SNIPERRIFLE ) { + COM_BitSet( g_entities[cs->entityNum].client->ps.weapons, WP_MAUSER ); + } +//----(SA) end + + // monsters have full ammo for their attacks + // knife gets infinite ammo too + if ( !Q_strncasecmp( params, "monsterattack", 13 ) || weapon == WP_KNIFE ) { + g_entities[cs->entityNum].client->ps.ammo[BG_FindAmmoForWeapon( weapon )] = 999; + Fill_Clip( &g_entities[cs->entityNum].client->ps, weapon ); //----(SA) added + } + // conditional flags + if ( ent->aiCharacter == AICHAR_ZOMBIE ) { + if ( COM_BitCheck( ent->client->ps.weapons, WP_MONSTER_ATTACK1 ) ) { + cs->aiFlags |= AIFL_NO_FLAME_DAMAGE; + SET_FLAMING_ZOMBIE( ent->s, 1 ); + } + } + } else { + G_Error( "AI Scripting: giveweapon %s, unknown weapon", params ); + } + + return qtrue; +}; + +/* +================= +AICast_ScriptAction_TakeWeapon + + syntax: takeweapon +================= +*/ +qboolean AICast_ScriptAction_TakeWeapon( cast_state_t *cs, char *params ) { + int weapon; + int i; + + weapon = WP_NONE; + + if ( !Q_stricmp( params, "all" ) ) { + + // clear out all weapons + memset( g_entities[cs->entityNum].client->ps.weapons, 0, sizeof( g_entities[cs->entityNum].client->ps.weapons ) ); + memset( g_entities[cs->entityNum].client->ps.ammo, 0, sizeof( g_entities[cs->entityNum].client->ps.ammo ) ); + memset( g_entities[cs->entityNum].client->ps.ammoclip, 0, sizeof( g_entities[cs->entityNum].client->ps.ammoclip ) ); + cs->weaponNum = WP_NONE; + + } else { + + + for ( i = 1; bg_itemlist[i].classname; i++ ) + { + //----(SA) first try the name they see in the editor, then the pickup name + if ( !Q_strcasecmp( params, bg_itemlist[i].classname ) ) { + weapon = bg_itemlist[i].giTag; + break; + } + + if ( !Q_strcasecmp( params, bg_itemlist[i].pickup_name ) ) { + weapon = bg_itemlist[i].giTag; + break; + } + } + + if ( weapon != WP_NONE ) { + qboolean clear; + + if ( weapon == WP_AKIMBO ) { + // take both the colt /and/ the akimbo weapons when 'akimbo' is specified + COM_BitClear( g_entities[cs->entityNum].client->ps.weapons, WP_COLT ); + } else if ( weapon == WP_COLT ) { + // take 'akimbo' first if it's there, then take 'colt' + if ( COM_BitCheck( g_entities[cs->entityNum].client->ps.weapons, WP_AKIMBO ) ) { + weapon = WP_AKIMBO; + } + } + + // + COM_BitClear( g_entities[cs->entityNum].client->ps.weapons, weapon ); + // also remove the ammo for this weapon + // but first make sure we dont have any other weapons that use the same ammo + clear = qtrue; + for ( i = 0; i < WP_NUM_WEAPONS; i++ ) { + if ( BG_FindAmmoForWeapon( weapon ) != BG_FindAmmoForWeapon( i ) ) { + continue; + } + if ( COM_BitCheck( g_entities[cs->entityNum].client->ps.weapons, i ) ) { + clear = qfalse; + } + } + if ( clear ) { +// (SA) temp only. commented out for pistol guys in escape1 +// g_entities[cs->entityNum].client->ps.ammo[BG_FindAmmoForWeapon(weapon)] = 0; + } + } else { + G_Error( "AI Scripting: takeweapon %s, unknown weapon", params ); + } + + } + + if ( !g_entities[cs->entityNum].client->ps.weapons ) { + if ( cs->bs ) { + cs->weaponNum = WP_NONE; + } else { + g_entities[cs->entityNum].client->ps.weapon = WP_NONE; + } + } + + return qtrue; +}; + + + +//----(SA) added + +/* +============== +AICast_ScriptAction_GiveInventory +============== +*/ +qboolean AICast_ScriptAction_GiveInventory( cast_state_t *cs, char *params ) { + int i; + gitem_t *item = 0; + + for ( i = 1; bg_itemlist[i].classname; i++ ) { + //----(SA) first try the name they see in the editor, then the pickup name + if ( !Q_strcasecmp( params, bg_itemlist[i].classname ) ) { + item = &bg_itemlist[i]; + } + + if ( !Q_strcasecmp( params, bg_itemlist[i].pickup_name ) ) { + item = &bg_itemlist[i]; + } + } + + if ( !item ) { // item not found + G_Error( "AI Scripting: giveinventory %s, unknown item", params ); + } + + if ( item->giType == IT_KEY ) { + g_entities[cs->entityNum].client->ps.stats[STAT_KEYS] |= ( 1 << item->giTag ); + } else if ( item->giType == IT_HOLDABLE ) { + // (SA) TODO + } + + return qtrue; +}; + + +//----(SA) end + + + +/* +================= +AICast_ScriptAction_Movetype + + syntax: movetype + + Sets this character's movement type, will exist until another movetype command is called +================= +*/ +qboolean AICast_ScriptAction_Movetype( cast_state_t *cs, char *params ) { + if ( !Q_strcasecmp( params, "walk" ) ) { + cs->movestate = MS_WALK; + cs->movestateType = MSTYPE_PERMANENT; + return qtrue; + } + if ( !Q_strcasecmp( params, "run" ) ) { + cs->movestate = MS_RUN; + cs->movestateType = MSTYPE_PERMANENT; + return qtrue; + } + if ( !Q_strcasecmp( params, "crouch" ) ) { + cs->movestate = MS_CROUCH; + cs->movestateType = MSTYPE_PERMANENT; + return qtrue; + } + if ( !Q_strcasecmp( params, "default" ) ) { + cs->movestate = MS_DEFAULT; + cs->movestateType = MSTYPE_NONE; + return qtrue; + } + + return qtrue; +} +/* +================= +AICast_ScriptAction_AlertEntity + + syntax: alertentity +================= +*/ +qboolean AICast_ScriptAction_AlertEntity( cast_state_t *cs, char *params ) { + gentity_t *ent; + + if ( !params || !params[0] ) { + G_Error( "AI Scripting: alertentity without targetname\n" ); + } + + // find this targetname + ent = G_Find( NULL, FOFS( targetname ), params ); + if ( !ent ) { + ent = G_Find( NULL, FOFS( aiName ), params ); // look for an AI + if ( !ent || !ent->client ) { // accept only AI for aiName check + G_Error( "AI Scripting: alertentity cannot find targetname \"%s\"\n", params ); + } + } + + // call this entity's AlertEntity function + if ( !ent->AIScript_AlertEntity ) { + if ( !ent->client && ent->use && !Q_stricmp( ent->classname, "ai_trigger" ) ) { + ent->use( ent, NULL, NULL ); + return qtrue; + } + + if ( aicast_debug.integer ) { + G_Printf( "AI Scripting: alertentity \"%s\" (classname = %s) doesn't have an \"AIScript_AlertEntity\" function\n", params, ent->classname ); + } + //G_Error( "AI Scripting: alertentity \"%s\" (classname = %s) doesn't have an \"AIScript_AlertEntity\" function\n", params, ent->classname ); + return qtrue; + } + + ent->AIScript_AlertEntity( ent ); + + return qtrue; +} + +/* +================= +AICast_ScriptAction_SaveGame + + NOTE: only use this command in "player" scripts, not for AI + + syntax: savegame +================= +*/ +qboolean AICast_ScriptAction_SaveGame( cast_state_t *cs, char *params ) { + char *pString, *saveName; + pString = params; + + if ( cs->bs ) { + G_Error( "AI Scripting: savegame attempted on a non-player" ); + } + +//----(SA) check for parameter + saveName = COM_ParseExt( &pString, qfalse ); + if ( !saveName[0] ) { + G_SaveGame( NULL ); // save the default "current" savegame + } else { + G_SaveGame( saveName ); + } +//----(SA) end + + return qtrue; +} + +/* +================= +AICast_ScriptAction_FireAtTarget + + syntax: fireattarget [duration] +================= +*/ +qboolean AICast_ScriptAction_FireAtTarget( cast_state_t *cs, char *params ) { + gentity_t *ent; + vec3_t vec, org, src; + char *pString, *token; + float diff; + int i; + + pString = params; + + token = COM_ParseExt( &pString, qfalse ); + if ( !token[0] ) { + G_Error( "AI Scripting: fireattarget without a targetname\n" ); + } + + if ( !cs->bs ) { + G_Error( "AI Scripting: fireattarget called for non-AI character\n" ); + } + + // find this targetname + ent = G_Find( NULL, FOFS( targetname ), token ); + if ( !ent ) { + ent = AICast_FindEntityForName( token ); + if ( !ent ) { + G_Error( "AI Scripting: fireattarget cannot find targetname/aiName \"%s\"\n", token ); + } + } + + // if this is our first call for this fireattarget, record the ammo count + if ( cs->castScriptStatus.scriptFlags & SFL_FIRST_CALL ) { + cs->lastWeaponFired = 0; + } + // make sure we don't move or shoot while turning to our target + if ( cs->castScriptStatus.scriptNoAttackTime < level.time ) { + cs->castScriptStatus.scriptNoAttackTime = level.time + 500; + } + // dont reload prematurely + cs->noReloadTime = level.time + 1000; + // don't move while firing at all + //if (cs->castScriptStatus.scriptNoMoveTime < level.time) { + cs->castScriptStatus.scriptNoMoveTime = level.time + 500; + //} + // let us move our view, whether it looks bad or not + cs->castScriptStatus.playAnimViewlockTime = 0; + // set the view angle manually + BG_EvaluateTrajectory( &ent->s.pos, level.time, org ); + VectorCopy( cs->bs->origin, src ); + src[2] += cs->bs->cur_ps.viewheight; + VectorSubtract( org, src, vec ); + VectorNormalize( vec ); + vectoangles( vec, cs->ideal_viewangles ); + for ( i = 0; i < 2; i++ ) { + diff = abs( AngleDifference( cs->bs->cur_ps.viewangles[i], cs->ideal_viewangles[i] ) ); + if ( VectorCompare( vec3_origin, ent->s.pos.trDelta ) ) { + if ( diff ) { + return qfalse; // not facing yet + } + } else { + if ( diff > 25 ) { // allow some slack when target is moving + return qfalse; + } + } + } + + // force fire + trap_EA_Attack( cs->bs->client ); + // + cs->bFlags |= BFL_ATTACKED; + // + // if we haven't fired yet + if ( !cs->lastWeaponFired ) { + return qfalse; + } + // + // do we need to stay and fire for a duration? + token = COM_ParseExt( &pString, qfalse ); + if ( !token[0] ) { + return qtrue; // no need to wait around + } + // only return true if we've been firing for long enough + return ( ( cs->castScriptStatus.castScriptStackChangeTime + atoi( token ) ) < level.time ); +} + +/* +================= +AICast_ScriptAction_GodMode + + syntax: godmode +================= +*/ +qboolean AICast_ScriptAction_GodMode( cast_state_t *cs, char *params ) { + if ( !params || !params[0] ) { + G_Error( "AI Scripting: godmode requires an on/off specifier\n" ); + } + + if ( !Q_stricmp( params, "on" ) ) { + g_entities[cs->bs->entitynum].flags |= FL_GODMODE; + } else if ( !Q_stricmp( params, "off" ) ) { + g_entities[cs->bs->entitynum].flags &= ~FL_GODMODE; + } else { + G_Error( "AI Scripting: godmode requires an on/off specifier\n" ); + } + + return qtrue; +} + +/* +================= +AICast_ScriptAction_Accum + + syntax: accum + + Commands: + + accum inc + accum abort_if_less_than + accum abort_if_greater_than + accum abort_if_not_equal + accum abort_if_equal + accum set + accum random + accum bitset + accum bitreset + accum abort_if_bitset + accum abort_if_not_bitset +================= +*/ +qboolean AICast_ScriptAction_Accum( cast_state_t *cs, char *params ) { + char *pString, *token, lastToken[MAX_QPATH]; + int bufferIndex; + + pString = params; + + token = COM_ParseExt( &pString, qfalse ); + if ( !token[0] ) { + G_Error( "AI Scripting: accum without a buffer index\n" ); + } + + bufferIndex = atoi( token ); + if ( bufferIndex >= MAX_SCRIPT_ACCUM_BUFFERS ) { + G_Error( "AI Scripting: accum buffer is outside range (0 - %i)\n", MAX_SCRIPT_ACCUM_BUFFERS ); + } + + token = COM_ParseExt( &pString, qfalse ); + if ( !token[0] ) { + G_Error( "AI Scripting: accum without a command\n" ); + } + + Q_strncpyz( lastToken, token, sizeof( lastToken ) ); + token = COM_ParseExt( &pString, qfalse ); + + if ( !Q_stricmp( lastToken, "inc" ) ) { + if ( !token[0] ) { + G_Error( "AI Scripting: accum %s requires a parameter\n", lastToken ); + } + cs->scriptAccumBuffer[bufferIndex] += atoi( token ); + } else if ( !Q_stricmp( lastToken, "abort_if_less_than" ) ) { + if ( !token[0] ) { + G_Error( "AI Scripting: accum %s requires a parameter\n", lastToken ); + } + if ( cs->scriptAccumBuffer[bufferIndex] < atoi( token ) ) { + // abort the current script + cs->castScriptStatus.castScriptStackHead = cs->castScriptEvents[cs->castScriptStatus.castScriptEventIndex].stack.numItems; + } + } else if ( !Q_stricmp( lastToken, "abort_if_greater_than" ) ) { + if ( !token[0] ) { + G_Error( "AI Scripting: accum %s requires a parameter\n", lastToken ); + } + if ( cs->scriptAccumBuffer[bufferIndex] > atoi( token ) ) { + // abort the current script + cs->castScriptStatus.castScriptStackHead = cs->castScriptEvents[cs->castScriptStatus.castScriptEventIndex].stack.numItems; + } + } else if ( !Q_stricmp( lastToken, "abort_if_not_equal" ) ) { + if ( !token[0] ) { + G_Error( "AI Scripting: accum %s requires a parameter\n", lastToken ); + } + if ( cs->scriptAccumBuffer[bufferIndex] != atoi( token ) ) { + // abort the current script + cs->castScriptStatus.castScriptStackHead = cs->castScriptEvents[cs->castScriptStatus.castScriptEventIndex].stack.numItems; + } + } else if ( !Q_stricmp( lastToken, "abort_if_equal" ) ) { + if ( !token[0] ) { + G_Error( "AI Scripting: accum %s requires a parameter\n", lastToken ); + } + if ( cs->scriptAccumBuffer[bufferIndex] == atoi( token ) ) { + // abort the current script + cs->castScriptStatus.castScriptStackHead = cs->castScriptEvents[cs->castScriptStatus.castScriptEventIndex].stack.numItems; + } + } else if ( !Q_stricmp( lastToken, "bitset" ) ) { + if ( !token[0] ) { + G_Error( "AI Scripting: accum %s requires a parameter\n", lastToken ); + } + cs->scriptAccumBuffer[bufferIndex] |= ( 1 << atoi( token ) ); + } else if ( !Q_stricmp( lastToken, "bitreset" ) ) { + if ( !token[0] ) { + G_Error( "AI Scripting: accum %s requires a parameter\n", lastToken ); + } + cs->scriptAccumBuffer[bufferIndex] &= ~( 1 << atoi( token ) ); + } else if ( !Q_stricmp( lastToken, "abort_if_bitset" ) ) { + if ( !token[0] ) { + G_Error( "AI Scripting: accum %s requires a parameter\n", lastToken ); + } + if ( cs->scriptAccumBuffer[bufferIndex] & ( 1 << atoi( token ) ) ) { + // abort the current script + cs->castScriptStatus.castScriptStackHead = cs->castScriptEvents[cs->castScriptStatus.castScriptEventIndex].stack.numItems; + } + } else if ( !Q_stricmp( lastToken, "abort_if_not_bitset" ) ) { + if ( !token[0] ) { + G_Error( "AI Scripting: accum %s requires a parameter\n", lastToken ); + } + if ( !( cs->scriptAccumBuffer[bufferIndex] & ( 1 << atoi( token ) ) ) ) { + // abort the current script + cs->castScriptStatus.castScriptStackHead = cs->castScriptEvents[cs->castScriptStatus.castScriptEventIndex].stack.numItems; + } + } else if ( !Q_stricmp( lastToken, "set" ) ) { + if ( !token[0] ) { + G_Error( "AI Scripting: accum %s requires a parameter\n", lastToken ); + } + cs->scriptAccumBuffer[bufferIndex] = atoi( token ); + } else if ( !Q_stricmp( lastToken, "random" ) ) { + if ( !token[0] ) { + G_Error( "AI Scripting: accum %s requires a parameter\n", lastToken ); + } + cs->scriptAccumBuffer[bufferIndex] = rand() % atoi( token ); + } else { + G_Error( "AI Scripting: accum %s: unknown command\n", params ); + } + + return qtrue; +} + +/* +================= +AICast_ScriptAction_SpawnCast + + syntax: spawncast + + is the entity marker which has the position and angles of where we want the new + cast AI to spawn +================= +*/ +qboolean AICast_ScriptAction_SpawnCast( cast_state_t *cs, char *params ) { +// char *pString, *token; +// char *classname; +// gentity_t *targetEnt, *newCast; + + G_Error( "AI Scripting: spawncast is no longer functional. Use trigger_spawn instead.\n" ); + return qfalse; +/* + if (!params[0]) { + G_Error( "AI Scripting: spawncast without a classname\n" ); + } + + pString = params; + + token = COM_ParseExt( &pString, qfalse ); + if (!token[0]) { + G_Error( "AI Scripting: spawncast without a classname\n" ); + } + + classname = G_Alloc( strlen(token)+1 ); + Q_strncpyz( classname, token, strlen(token)+1 ); + + token = COM_ParseExt( &pString, qfalse ); + if (!token[0]) { + G_Error( "AI Scripting: spawncast without a targetname\n" ); + } + + targetEnt = G_Find( NULL, FOFS(targetname), token ); + if (!targetEnt) { + G_Error( "AI Scripting: cannot find targetname \"%s\"\n", token ); + } + + token = COM_ParseExt( &pString, qfalse ); + if (!token[0]) { + G_Error( "AI Scripting: spawncast without an ainame\n" ); + } + + newCast = G_Spawn(); + newCast->classname = classname; + VectorCopy( targetEnt->s.origin, newCast->s.origin ); + VectorCopy( targetEnt->s.angles, newCast->s.angles ); + newCast->aiName = G_Alloc( strlen(token)+1 ); + Q_strncpyz( newCast->aiName, token, strlen(token)+1 ); + + if (!G_CallSpawn( newCast )) { + G_Error( "AI Scripting: spawncast for unknown entity \"%s\"\n", newCast->classname ); + } + + return qtrue; +*/ +} + +/* +================= +AICast_ScriptAction_MissionFailed + + syntax: missionfailed