@@ -75,7 +75,8 @@ String *luacon_lastError;
75
75
String lastCode;
76
76
77
77
int *lua_el_mode;
78
- LuaSmartRef *lua_el_func, *lua_gr_func, *lua_cd_func;
78
+ LuaSmartRef *lua_el_func, *lua_gr_func;
79
+ std::vector<LuaSmartRef> luaCtypeDrawHandlers, luaCreateHandlers, luaCreateAllowedHandlers, luaChangeTypeHandlers;
79
80
80
81
int getPartIndex_curIdx;
81
82
int tptProperties; // Table for some TPT properties
@@ -333,15 +334,18 @@ tpt.partsdata = nil");
333
334
}
334
335
lua_setfield (l, tptProperties, " eltransition" );
335
336
336
- lua_cd_func_v = std::vector<LuaSmartRef>(PT_NUM, l);
337
- lua_cd_func = &lua_cd_func_v[0 ];
338
337
lua_gr_func_v = std::vector<LuaSmartRef>(PT_NUM, l);
339
338
lua_gr_func = &lua_gr_func_v[0 ];
340
339
lua_el_func_v = std::vector<LuaSmartRef>(PT_NUM, l);
341
340
lua_el_func = &lua_el_func_v[0 ];
342
341
lua_el_mode_v = std::vector<int >(PT_NUM, 0 );
343
342
lua_el_mode = &lua_el_mode_v[0 ];
344
343
344
+ luaCtypeDrawHandlers = std::vector<LuaSmartRef>(PT_NUM, l);
345
+ luaCreateHandlers = std::vector<LuaSmartRef>(PT_NUM, l);
346
+ luaCreateAllowedHandlers = std::vector<LuaSmartRef>(PT_NUM, l);
347
+ luaChangeTypeHandlers = std::vector<LuaSmartRef>(PT_NUM, l);
348
+
345
349
// make tpt.* a metatable
346
350
lua_newtable (l);
347
351
lua_pushlightuserdata (l, this );
@@ -2685,12 +2689,73 @@ int LuaScriptInterface::elements_allocate(lua_State * l)
2685
2689
return 1 ;
2686
2690
}
2687
2691
2692
+ void luaCreateWrapper (ELEMENT_CREATE_FUNC_ARGS)
2693
+ {
2694
+ if (luaCreateHandlers[sim->parts [i].type ])
2695
+ {
2696
+ lua_rawgeti (luacon_ci->l , LUA_REGISTRYINDEX, luaCreateHandlers[sim->parts [i].type ]);
2697
+ lua_pushinteger (luacon_ci->l , i);
2698
+ lua_pushinteger (luacon_ci->l , x);
2699
+ lua_pushinteger (luacon_ci->l , y);
2700
+ lua_pushinteger (luacon_ci->l , t);
2701
+ lua_pushinteger (luacon_ci->l , v);
2702
+ if (lua_pcall (luacon_ci->l , 5 , 0 , 0 ))
2703
+ {
2704
+ luacon_ci->Log (CommandInterface::LogError, " In create func: " + luacon_geterror ());
2705
+ lua_pop (luacon_ci->l , 1 );
2706
+ }
2707
+ }
2708
+ }
2709
+
2710
+ bool luaCreateAllowedWrapper (ELEMENT_CREATE_ALLOWED_FUNC_ARGS)
2711
+ {
2712
+ bool ret = false ;
2713
+ if (luaCreateAllowedHandlers[t])
2714
+ {
2715
+ lua_rawgeti (luacon_ci->l , LUA_REGISTRYINDEX, luaCreateAllowedHandlers[t]);
2716
+ lua_pushinteger (luacon_ci->l , i);
2717
+ lua_pushinteger (luacon_ci->l , x);
2718
+ lua_pushinteger (luacon_ci->l , y);
2719
+ lua_pushinteger (luacon_ci->l , t);
2720
+ if (lua_pcall (luacon_ci->l , 4 , 1 , 0 ))
2721
+ {
2722
+ luacon_ci->Log (CommandInterface::LogError, " In create allowed: " + luacon_geterror ());
2723
+ lua_pop (luacon_ci->l , 1 );
2724
+ }
2725
+ else
2726
+ {
2727
+ if (lua_isboolean (luacon_ci->l , -1 ))
2728
+ ret = lua_toboolean (luacon_ci->l , -1 );
2729
+ lua_pop (luacon_ci->l , 1 );
2730
+ }
2731
+ }
2732
+ return ret;
2733
+ }
2734
+
2735
+ void luaChangeTypeWrapper (ELEMENT_CHANGETYPE_FUNC_ARGS)
2736
+ {
2737
+ if (luaChangeTypeHandlers[sim->parts [i].type ])
2738
+ {
2739
+ lua_rawgeti (luacon_ci->l , LUA_REGISTRYINDEX, luaChangeTypeHandlers[sim->parts [i].type ]);
2740
+ lua_pushinteger (luacon_ci->l , i);
2741
+ lua_pushinteger (luacon_ci->l , x);
2742
+ lua_pushinteger (luacon_ci->l , y);
2743
+ lua_pushinteger (luacon_ci->l , from);
2744
+ lua_pushinteger (luacon_ci->l , to);
2745
+ if (lua_pcall (luacon_ci->l , 5 , 0 , 0 ))
2746
+ {
2747
+ luacon_ci->Log (CommandInterface::LogError, " In change type: " + luacon_geterror ());
2748
+ lua_pop (luacon_ci->l , 1 );
2749
+ }
2750
+ }
2751
+ }
2752
+
2688
2753
static bool luaCtypeDrawWrapper (CTYPEDRAW_FUNC_ARGS)
2689
2754
{
2690
2755
bool ret = false ;
2691
- if (lua_cd_func [sim->parts [i].type ])
2756
+ if (luaCtypeDrawHandlers [sim->parts [i].type ])
2692
2757
{
2693
- lua_rawgeti (luacon_ci->l , LUA_REGISTRYINDEX, lua_cd_func [sim->parts [i].type ]);
2758
+ lua_rawgeti (luacon_ci->l , LUA_REGISTRYINDEX, luaCtypeDrawHandlers [sim->parts [i].type ]);
2694
2759
lua_pushinteger (luacon_ci->l , i);
2695
2760
lua_pushinteger (luacon_ci->l , t);
2696
2761
lua_pushinteger (luacon_ci->l , v);
@@ -2758,19 +2823,74 @@ int LuaScriptInterface::elements_element(lua_State * l)
2758
2823
}
2759
2824
lua_pop (l, 1 );
2760
2825
2826
+ lua_getfield (l, -1 , " Create" );
2827
+ if (lua_type (l, -1 ) == LUA_TFUNCTION)
2828
+ {
2829
+ luaCreateHandlers[id].Assign (-1 );
2830
+ luacon_sim->elements [id].Create = luaCreateWrapper;
2831
+ }
2832
+ else if (lua_type (l, -1 ) == LUA_TBOOLEAN && !lua_toboolean (l, -1 ))
2833
+ {
2834
+ luaCreateHandlers[id].Clear ();
2835
+ luacon_sim->elements [id].Create = nullptr ;
2836
+ }
2837
+ lua_pop (l, 1 );
2838
+
2839
+ lua_getfield (l, -1 , " CreateAllowed" );
2840
+ if (lua_type (l, -1 ) == LUA_TFUNCTION)
2841
+ {
2842
+ luaCreateAllowedHandlers[id].Assign (-1 );
2843
+ luacon_sim->elements [id].CreateAllowed = luaCreateAllowedWrapper;
2844
+ }
2845
+ else if (lua_type (l, -1 ) == LUA_TBOOLEAN && !lua_toboolean (l, -1 ))
2846
+ {
2847
+ luaCreateAllowedHandlers[id].Clear ();
2848
+ luacon_sim->elements [id].CreateAllowed = nullptr ;
2849
+ }
2850
+ lua_pop (l, 1 );
2851
+
2852
+ lua_getfield (l, -1 , " ChangeType" );
2853
+ if (lua_type (l, -1 ) == LUA_TFUNCTION)
2854
+ {
2855
+ luaChangeTypeHandlers[id].Assign (-1 );
2856
+ luacon_sim->elements [id].ChangeType = luaChangeTypeWrapper;
2857
+ }
2858
+ else if (lua_type (l, -1 ) == LUA_TBOOLEAN && !lua_toboolean (l, -1 ))
2859
+ {
2860
+ luaChangeTypeHandlers[id].Clear ();
2861
+ luacon_sim->elements [id].ChangeType = nullptr ;
2862
+ }
2863
+ lua_pop (l, 1 );
2864
+
2761
2865
lua_getfield (l, -1 , " CtypeDraw" );
2762
2866
if (lua_type (l, -1 ) == LUA_TFUNCTION)
2763
2867
{
2764
- lua_cd_func [id].Assign (-1 );
2868
+ luaCtypeDrawHandlers [id].Assign (-1 );
2765
2869
luacon_sim->elements [id].CtypeDraw = luaCtypeDrawWrapper;
2766
2870
}
2767
2871
else if (lua_type (l, -1 ) == LUA_TBOOLEAN && !lua_toboolean (l, -1 ))
2768
2872
{
2769
- lua_cd_func [id].Clear ();
2873
+ luaCtypeDrawHandlers [id].Clear ();
2770
2874
luacon_sim->elements [id].CtypeDraw = nullptr ;
2771
2875
}
2772
2876
lua_pop (l, 1 );
2773
2877
2878
+ lua_getfield (l, -1 , " DefaultProperties" );
2879
+ if (lua_type (l, -1 ) == LUA_TTABLE)
2880
+ {
2881
+ for (auto &prop : Particle::GetProperties ())
2882
+ {
2883
+ lua_getfield (l, -1 , prop.Name .c_str ());
2884
+ if (lua_type (l, -1 ) != LUA_TNIL)
2885
+ {
2886
+ auto propertyAddress = reinterpret_cast <intptr_t >((reinterpret_cast <unsigned char *>(&luacon_sim->elements [id].DefaultProperties )) + prop.Offset );
2887
+ LuaSetProperty (l, prop, propertyAddress, -1 );
2888
+ }
2889
+ lua_pop (l, 1 );
2890
+ }
2891
+ }
2892
+ lua_pop (l, 1 );
2893
+
2774
2894
luacon_model->BuildMenus ();
2775
2895
luacon_sim->init_can_move ();
2776
2896
luacon_ren->graphicscache [id].isready = 0 ;
@@ -2787,8 +2907,20 @@ int LuaScriptInterface::elements_element(lua_State * l)
2787
2907
LuaGetProperty (l, prop, propertyAddress);
2788
2908
lua_setfield (l, -2 , prop.Name .c_str ());
2789
2909
}
2910
+
2790
2911
lua_pushstring (l, luacon_sim->elements [id].Identifier .c_str ());
2791
2912
lua_setfield (l, -2 , " Identifier" );
2913
+
2914
+ lua_newtable (l);
2915
+ int tableIdx = lua_gettop (l);
2916
+ for (auto &prop : Particle::GetProperties ())
2917
+ {
2918
+ auto propertyAddress = reinterpret_cast <intptr_t >((reinterpret_cast <unsigned char *>(&luacon_sim->elements [id].DefaultProperties )) + prop.Offset );
2919
+ LuaGetProperty (l, prop, propertyAddress);
2920
+ lua_setfield (l, tableIdx, prop.Name .c_str ());
2921
+ }
2922
+ lua_setfield (l, -2 , " DefaultProperties" );
2923
+
2792
2924
return 1 ;
2793
2925
}
2794
2926
}
@@ -2829,7 +2961,6 @@ int LuaScriptInterface::elements_property(lua_State * l)
2829
2961
luacon_model->BuildMenus ();
2830
2962
luacon_sim->init_can_move ();
2831
2963
luacon_ren->graphicscache [id].isready = 0 ;
2832
- return 0 ;
2833
2964
}
2834
2965
else if (propertyName == " Update" )
2835
2966
{
@@ -2857,7 +2988,6 @@ int LuaScriptInterface::elements_property(lua_State * l)
2857
2988
lua_el_mode[id] = 0 ;
2858
2989
luacon_sim->elements [id].Update = NULL ;
2859
2990
}
2860
- return 0 ;
2861
2991
}
2862
2992
else if (propertyName == " Graphics" )
2863
2993
{
@@ -2871,26 +3001,78 @@ int LuaScriptInterface::elements_property(lua_State * l)
2871
3001
luacon_sim->elements [id].Graphics = NULL ;
2872
3002
}
2873
3003
luacon_ren->graphicscache [id].isready = 0 ;
2874
- return 0 ;
3004
+ }
3005
+ else if (propertyName == " Create" )
3006
+ {
3007
+ if (lua_type (l, 3 ) == LUA_TFUNCTION)
3008
+ {
3009
+ luaCreateHandlers[id].Assign (3 );
3010
+ luacon_sim->elements [id].Create = luaCreateWrapper;
3011
+ }
3012
+ else if (lua_type (l, 3 ) == LUA_TBOOLEAN && !lua_toboolean (l, 3 ))
3013
+ {
3014
+ luaCreateHandlers[id].Clear ();
3015
+ luacon_sim->elements [id].Create = nullptr ;
3016
+ }
3017
+ }
3018
+ else if (propertyName == " CreateAllowed" )
3019
+ {
3020
+ if (lua_type (l, 3 ) == LUA_TFUNCTION)
3021
+ {
3022
+ luaCreateAllowedHandlers[id].Assign (3 );
3023
+ luacon_sim->elements [id].CreateAllowed = luaCreateAllowedWrapper;
3024
+ }
3025
+ else if (lua_type (l, 3 ) == LUA_TBOOLEAN && !lua_toboolean (l, 3 ))
3026
+ {
3027
+ luaCreateAllowedHandlers[id].Clear ();
3028
+ luacon_sim->elements [id].CreateAllowed = nullptr ;
3029
+ }
3030
+ }
3031
+ else if (propertyName == " ChangeType" )
3032
+ {
3033
+ if (lua_type (l, 3 ) == LUA_TFUNCTION)
3034
+ {
3035
+ luaChangeTypeHandlers[id].Assign (3 );
3036
+ luacon_sim->elements [id].ChangeType = luaChangeTypeWrapper;
3037
+ }
3038
+ else if (lua_type (l, 3 ) == LUA_TBOOLEAN && !lua_toboolean (l, 3 ))
3039
+ {
3040
+ luaChangeTypeHandlers[id].Clear ();
3041
+ luacon_sim->elements [id].ChangeType = nullptr ;
3042
+ }
2875
3043
}
2876
3044
else if (propertyName == " CtypeDraw" )
2877
3045
{
2878
3046
if (lua_type (l, 3 ) == LUA_TFUNCTION)
2879
3047
{
2880
- lua_cd_func [id].Assign (3 );
3048
+ luaCtypeDrawHandlers [id].Assign (3 );
2881
3049
luacon_sim->elements [id].CtypeDraw = luaCtypeDrawWrapper;
2882
3050
}
2883
3051
else if (lua_type (l, 3 ) == LUA_TBOOLEAN && !lua_toboolean (l, 3 ))
2884
3052
{
2885
- lua_cd_func [id].Clear ();
3053
+ luaCtypeDrawHandlers [id].Clear ();
2886
3054
luacon_sim->elements [id].CtypeDraw = nullptr ;
2887
3055
}
2888
- return 0 ;
3056
+ }
3057
+ else if (propertyName == " DefaultProperties" )
3058
+ {
3059
+ luaL_checktype (l, 3 , LUA_TTABLE);
3060
+ for (auto &prop : Particle::GetProperties ())
3061
+ {
3062
+ lua_getfield (l, -1 , prop.Name .c_str ());
3063
+ if (lua_type (l, -1 ) != LUA_TNIL)
3064
+ {
3065
+ auto propertyAddress = reinterpret_cast <intptr_t >((reinterpret_cast <unsigned char *>(&luacon_sim->elements [id].DefaultProperties )) + prop.Offset );
3066
+ LuaSetProperty (l, prop, propertyAddress, -1 );
3067
+ }
3068
+ lua_pop (l, 1 );
3069
+ }
2889
3070
}
2890
3071
else
2891
3072
{
2892
3073
return luaL_error (l, " Invalid element property" );
2893
3074
}
3075
+ return 0 ;
2894
3076
}
2895
3077
else
2896
3078
{
@@ -2905,6 +3087,18 @@ int LuaScriptInterface::elements_property(lua_State * l)
2905
3087
lua_pushstring (l, luacon_sim->elements [id].Identifier .c_str ());
2906
3088
return 1 ;
2907
3089
}
3090
+ else if (propertyName == " DefaultProperties" )
3091
+ {
3092
+ lua_newtable (l);
3093
+ int tableIdx = lua_gettop (l);
3094
+ for (auto &prop : Particle::GetProperties ())
3095
+ {
3096
+ auto propertyAddress = reinterpret_cast <intptr_t >((reinterpret_cast <unsigned char *>(&luacon_sim->elements [id].DefaultProperties )) + prop.Offset );
3097
+ LuaGetProperty (l, prop, propertyAddress);
3098
+ lua_setfield (l, tableIdx, prop.Name .c_str ());
3099
+ }
3100
+ return 1 ;
3101
+ }
2908
3102
else
2909
3103
{
2910
3104
return luaL_error (l, " Invalid element property" );
0 commit comments