From 753569c7e17b4e8a8e2605c939aea856da723b51 Mon Sep 17 00:00:00 2001 From: hgn Date: Mon, 23 Dec 2024 17:40:35 +0000 Subject: [PATCH] Add scripting file --- build.c | 3 + content_skaterift/maps/mp_hq/main.mdl | Bin 115432 -> 115432 bytes src/metascene.c | 81 +++------------ src/metascene.h | 80 +++++++++++++++ src/skaterift.c | 26 +++-- src/skaterift_script.c | 142 ++++++++++++++++++++++++++ src/skaterift_script.h | 4 + src/skeleton.h | 1 + 8 files changed, 263 insertions(+), 74 deletions(-) create mode 100644 src/skaterift_script.c create mode 100644 src/skaterift_script.h diff --git a/build.c b/build.c index ebd4954..c7e1a5d 100644 --- a/build.c +++ b/build.c @@ -335,6 +335,9 @@ int main( int argc, char *argv[] ) if( vg_long_opt( "no-asan", NULL ) ) vg_test_env.debug_asan = 0; + + if( (arg = vg_long_opt_arg( "strdjb2", NULL )) ) + printf( "vg_strdjb2('%s'): %u\n", arg, vg_strdjb2(arg) ); } vg_success( "All scripts completed\n" ); diff --git a/content_skaterift/maps/mp_hq/main.mdl b/content_skaterift/maps/mp_hq/main.mdl index d7e7800b90cdb65a897362c6ea3a9748889d8fb9..f619619045aa922a9639325d981941df163a7ae6 100644 GIT binary patch delta 35 rcmaFS%KoC2eZw6_fnPDIFN>3lG7Cx+N>YnU7@FTOZhynb_~!rsC7BQ~ delta 35 rcmaFS%KoC2eZw6_0Yf+T)ybtL#mT98sS3HNCCzUbx4&Uz{Br;R3gHhx diff --git a/src/metascene.c b/src/metascene.c index ea9bc38..674337b 100644 --- a/src/metascene.c +++ b/src/metascene.c @@ -1,6 +1,8 @@ #include "metascene.h" #include "vg/vg_magi.h" +struct _cutscene _cutscene; + void metascene_load( ms_context *ms, const char *path, void *alloc ) { af_open( &ms->af, path, MS_VERSION_MIN, MS_VERSION_NR, alloc ); @@ -26,72 +28,21 @@ void metascene_load( ms_context *ms, const char *path, void *alloc ) } } -struct +struct cs_instance *_cutscene_get_first_model_instance( const char *mdl_name ) { - ms_context meta; - void *arena; - - bool ready; - - struct model_ref + for( u32 i=0; i<_cutscene.instance_count; i ++ ) { - const char *name; - u32 name_hash; - u32 reference_count; - - mdl_context mdl; - - struct cs_skeleton + struct cs_instance *inst = &_cutscene.instances[i]; + struct model_ref *mref = &_cutscene.refs[ inst->ref_id ]; + + if( vg_str_eq( mdl_name, mref->name ) ) { - struct skeleton sk; - u32 skinning_offset; + return inst; } - *skeletons; - u32 total_skinning_bones; - } - * refs; - u32 unique_refs; - - struct cs_instance - { - u32 ref_id; - m4x3f *skinning_data; } - * instances; - u32 instance_count; - - struct cs_sampler - { - ms_strip *strip; - ms_override *override; - union - { - struct - { - ms_track *track; - f32 *target; - u32 keyframe; - u32 semantic; - } - curves; - - struct - { - struct skeleton *ref_sk; - m4x3f *skinning_data; - } - skeleton; - }; - } - samplers[32]; - u32 active_samplers; - - ent_camera *active_camera; - u32 strip; - f32 time; + return NULL; } -_cutscene; /* * Find associated entity data. We should also probably do this on the world @@ -162,8 +113,8 @@ static void _cutscene_get_strip_asoc( ms_strip *strip, static void sync_cutscene_loaded( void *payload, u32 size ) { - vg_info( "Cutscene ready\n" ); - _cutscene.ready = 1; + vg_info( "Cutscene loaded\n" ); + _cutscene.state = k_cutscene_state_ready; } static void cutscene_load_thread( void *data ) @@ -307,7 +258,7 @@ static int cmd_cutscene_play( int argc, const char *argv[] ) { if( argc == 1 ) { - if( _cutscene.ready ) + if( _cutscene.state != k_cutscene_state_none ) { vg_info( "Resetting cutscene..\n" ); _cutscene.time = 0.0f; @@ -318,7 +269,7 @@ static int cmd_cutscene_play( int argc, const char *argv[] ) if( vg_loader_availible() ) { - _cutscene.ready = 0; + _cutscene.state = k_cutscene_state_loading; _cutscene.time = 0.0f; _cutscene.strip = 0; _cutscene.active_samplers = 0; @@ -462,7 +413,7 @@ ent_camera *_cutscene_active_camera(void) void cutscene_update( f32 delta ) { - if( !_cutscene.ready ) + if( _cutscene.state != k_cutscene_state_playing ) return; _cutscene.time += delta; @@ -695,7 +646,7 @@ void cutscene_update( f32 delta ) void cutscene_render( world_instance *world, vg_camera *cam ) { - if( _cutscene.ready ) + if( _cutscene.state >= k_cutscene_state_ready ) { for( u32 i=0; i<_cutscene.instance_count; i ++ ) { diff --git a/src/metascene.h b/src/metascene.h index 86b4990..010e3a1 100644 --- a/src/metascene.h +++ b/src/metascene.h @@ -74,8 +74,88 @@ struct ms_curve_keyframe v2f co, l, r; }; +struct cs_instance +{ + u32 ref_id; + m4x3f *skinning_data; +}; + +#include "skeleton.h" + +struct _cutscene +{ + ms_context meta; + void *arena; + + enum cutscene_state + { + k_cutscene_state_none, + k_cutscene_state_loading, + k_cutscene_state_ready, + k_cutscene_state_playing, + k_cutscene_state_done + } + state; + + struct model_ref + { + const char *name; + u32 name_hash; + u32 reference_count; + + mdl_context mdl; + + struct cs_skeleton + { + struct skeleton sk; + u32 skinning_offset; + } + *skeletons; + u32 total_skinning_bones; + } + * refs; + u32 unique_refs; + + struct cs_instance *instances; + u32 instance_count; + + struct cs_sampler + { + ms_strip *strip; + ms_override *override; + union + { + struct + { + ms_track *track; + f32 *target; + u32 keyframe; + u32 semantic; + } + curves; + + struct + { + struct skeleton *ref_sk; + m4x3f *skinning_data; + } + skeleton; + }; + } + samplers[32]; + u32 active_samplers; + + ent_camera *active_camera; + + u32 strip; + f32 time; +} +extern _cutscene; + void metascene_load( ms_context *ms, const char *path, void *alloc ); void cutscene_init(void); void cutscene_render( world_instance *world, vg_camera *cam ); void cutscene_update( f32 delta ); ent_camera *_cutscene_active_camera(void); + +struct cs_instance *_cutscene_get_first_model_instance( const char *mdl_name ); diff --git a/src/skaterift.c b/src/skaterift.c index cb2e8e8..ed7acef 100644 --- a/src/skaterift.c +++ b/src/skaterift.c @@ -51,6 +51,7 @@ #include "player_render.h" #include "control_overlay.h" #include "client.h" +#include "skaterift_script.h" struct skaterift_globals skaterift = { @@ -153,6 +154,7 @@ static void skaterift_load_player_content(void) void game_load(void) { + _skaterift_script_init(); vg_console_reg_cmd( "load_world", skaterift_load_world_command, NULL ); vg_console_reg_var( "immobile", &localplayer.immobile, k_var_dtype_i32, 0 ); vg_loader_step( menu_init, NULL ); @@ -219,7 +221,8 @@ void vg_pre_update(void) if( !g_client.loaded ) return; - draw_origin_axis(); + //draw_origin_axis(); + _skaterift_script_update(); addon_system_pre_update(); skateshop_world_preview_preupdate(); network_update(); @@ -503,8 +506,6 @@ static void skaterift_composite_maincamera(void) vg_camera_finalize( &g_render.cam ); } -static void temp_not_done_1(void); - static void render_main_game(void) { if( skaterift.activity == k_skaterift_replay ) @@ -518,7 +519,7 @@ static void render_main_game(void) localplayer.deferred_frame_record = 0; } - temp_not_done_1(); + //temp_not_done_1(); animate_remote_players(); player__pre_render(); @@ -693,17 +694,24 @@ void vg_gui( ui_context *ctx ) #include "metascene.c" #include "control_overlay.c" #include "ent_camera.c" +#include "skaterift_script.c" - +#if 0 static void temp_not_done_1(void) { if( _cutscene.ready ) { - struct skeleton *sk = &localplayer.skeleton; - for( u32 i=0; ibone_count; i ++ ) + struct cs_instance *inst = _cutscene_get_first_model_instance( + "playermodels/skaterift_john/ch_john" ); + + if( inst ) { - m4x3_copy( _cutscene.instances[1].skinning_data[i], - localplayer.final_mtx[i] ); + struct skeleton *sk = &localplayer.skeleton; + for( u32 i=0; ibone_count; i ++ ) + { + m4x3_copy( inst->skinning_data[i], localplayer.final_mtx[i] ); + } } } } +#endif diff --git a/src/skaterift_script.c b/src/skaterift_script.c new file mode 100644 index 0000000..95fd7db --- /dev/null +++ b/src/skaterift_script.c @@ -0,0 +1,142 @@ +/* + * One day in the future, all will be well with the abstraction gods, and these + * scripts will run in an interpreter just like a real scripting language. That + * of course assumes I believe in the abstraction gods, but I am not a religious + * man. + */ + +enum escript_event +{ + k_escript_event_call = 0, + k_escript_event_update = 1 +}; + +/* you can add anything you want to this. */ +enum escript_state +{ + k_escript_state_none, + k_escript_state_loading, + k_escript_state_initializing, + k_escript_state_playing, + k_escript_state_end +}; + +/* This is the development one */ +static bool _skaterift_script_test( enum escript_event ev ) +{ + static u32 state; + static struct cs_instance *override_inst; + + if( ev == k_escript_event_call ) + { + state = k_escript_state_loading; + override_inst = NULL; + vg_info( "test:state = loading\n" ); + } + + /* scene + * --------------------------------------------------------------- */ + + if( state == k_escript_state_loading ) + { + if( cmd_cutscene_play( 1, (const char *[]){ "metascenes/test_scene.ms" } ) ) + { + state = k_escript_state_initializing; + vg_info( "test:state = initializing\n" ); + } + } + + if( state == k_escript_state_initializing ) + { + if( _cutscene.state == k_cutscene_state_ready ) + { + override_inst = _cutscene_get_first_model_instance( + "playermodels/skaterift_john/ch_john" ); + + if( override_inst ) + { + state = k_escript_state_playing; + vg_info( "test:state = playing\n" ); + } + else + { + vg_error( "test: Failed to find player target" ); + return 1; + } + } + } + + if( state == k_escript_state_playing ) + { + if( _cutscene.state == k_cutscene_state_done ) + { + state = k_escript_state_end; + vg_info( "test:state = end\n" ); + // TODO: Unload cutscene? + return 1; + } + } + + return 0; +} + +enum escript_script_id +{ + k_escript_script_id_test = 0, + k_escript_script_id_max +}; + +struct +{ + enum escript_script_id script_id; +} +static _script = { .script_id = k_escript_script_id_max }; + +struct script_binding +{ + const char *alias; + bool( *jump )( enum escript_event ev ); +} +_script_bindings[] = +{ + [k_escript_script_id_test] = { "test", _skaterift_script_test } +}; + +static int _skaterift_script_hook( int argc, const char *argv[] ) +{ + if( argc != 1 ) + { + vg_error( "Usage: script \n" ); + return 0; + } + + for( u32 i=0; ialias ) ) + { + _script.script_id = i; + if( bind->jump( k_escript_event_call ) ) + _script.script_id = k_escript_script_id_max; + + return 1; + } + } + + vg_error( "No such script '%s'\n", argv[0] ); + return 1; +} + +void _skaterift_script_update(void) +{ + if( _script.script_id != k_escript_script_id_max ) + { + if( _script_bindings[ _script.script_id ].jump( k_escript_event_update ) ) + _script.script_id = k_escript_script_id_max; + } +} + +void _skaterift_script_init(void) +{ + vg_console_reg_cmd( "script", _skaterift_script_hook, NULL ); +} diff --git a/src/skaterift_script.h b/src/skaterift_script.h new file mode 100644 index 0000000..35cdb9d --- /dev/null +++ b/src/skaterift_script.h @@ -0,0 +1,4 @@ +#pragma once + +void _skaterift_script_update(void); +void _skaterift_script_init(void); diff --git a/src/skeleton.h b/src/skeleton.h index f8acad7..814ac55 100644 --- a/src/skeleton.h +++ b/src/skeleton.h @@ -5,6 +5,7 @@ #pragma once #include "vg/vg_lines.h" #include "model.h" +#include "metascene.h" struct skeleton { -- 2.25.1