Add scripting file
authorhgn <hgodden00@gmail.com>
Mon, 23 Dec 2024 17:40:35 +0000 (17:40 +0000)
committerhgn <hgodden00@gmail.com>
Mon, 23 Dec 2024 17:40:35 +0000 (17:40 +0000)
build.c
content_skaterift/maps/mp_hq/main.mdl
src/metascene.c
src/metascene.h
src/skaterift.c
src/skaterift_script.c [new file with mode: 0644]
src/skaterift_script.h [new file with mode: 0644]
src/skeleton.h

diff --git a/build.c b/build.c
index ebd4954815f1956d414e53abe5cc974adcc46d22..c7e1a5dce7788e701a4dcc2bfd093ce627bddae2 100644 (file)
--- 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" );
index d7e7800b90cdb65a897362c6ea3a9748889d8fb9..f619619045aa922a9639325d981941df163a7ae6 100644 (file)
Binary files a/content_skaterift/maps/mp_hq/main.mdl and b/content_skaterift/maps/mp_hq/main.mdl differ
index ea9bc380b80b68c304075d3f5bf37b4130da37cb..674337be46021d5a2d6773c222b721d5ebba6d4b 100644 (file)
@@ -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 ++ )
       {
index 86b4990a31423f92733ac90e4962c4db72678e1f..010e3a1514fa628032bfefd0b41884589f379279 100644 (file)
@@ -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 );
index cb2e8e863e69329857de5d3fe41ea20f96de2607..ed7acef3e5b132db515585afe51093832d2241d4 100644 (file)
@@ -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; i<sk->bone_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; i<sk->bone_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 (file)
index 0000000..95fd7db
--- /dev/null
@@ -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 <x>\n" );
+      return 0;
+   }
+
+   for( u32 i=0; i<k_escript_script_id_max; i ++ )
+   {
+      struct script_binding *bind = &_script_bindings[i];
+      if( vg_str_eq( argv[0], bind->alias ) ) 
+      {
+         _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 (file)
index 0000000..35cdb9d
--- /dev/null
@@ -0,0 +1,4 @@
+#pragma once
+
+void _skaterift_script_update(void);
+void _skaterift_script_init(void);
index f8acad75fa6960cd63851b930a2ec1d181f50892..814ac55008f81ada748cc81a7ae06b00f2e08b0b 100644 (file)
@@ -5,6 +5,7 @@
 #pragma once
 #include "vg/vg_lines.h"
 #include "model.h"
+#include "metascene.h"
 
 struct skeleton
 {