From f22862aa4ed9f6e38863c9120bfb9c3ae5765c12 Mon Sep 17 00:00:00 2001 From: CharlesCatYT Date: Tue, 20 Feb 2024 20:39:55 -0500 Subject: [PATCH] a lot of lua stuff, experimental yes custom states, video functions, camera functions, --- source/animateatlas/AtlasFrameMaker.hx | 2 +- source/backend/Mods.hx | 3 +- source/backend/MusicBeatSubstate.hx | 44 +- source/cutscenes/DialogueBoxPsych.hx | 10 +- source/import.hx | 9 + source/psychlua/CallbackHandler.hx | 2 +- source/psychlua/CameraFunctions.hx | 79 ++++ source/psychlua/CustomState.hx | 117 +++++ source/psychlua/CustomSubstate.hx | 22 +- source/psychlua/DeprecatedFunctions.hx | 40 +- source/psychlua/ExtraFunctions.hx | 77 ++-- source/psychlua/FlxAnimateFunctions.hx | 10 +- source/psychlua/FunkinLua.hx | 563 ++++++++++++++++--------- source/psychlua/HScript.hx | 73 ++-- source/psychlua/LuaUtils.hx | 87 ++-- source/psychlua/ReflectionFunctions.hx | 10 +- source/psychlua/ScriptHandler.hx | 402 ++++++++++++++++++ source/psychlua/TextFunctions.hx | 16 +- source/psychlua/VideoFunctions.hx | 118 ++++++ source/states/PlayState.hx | 502 ++++------------------ source/states/TitleState.hx | 30 +- source/substates/GameOverSubstate.hx | 12 +- 22 files changed, 1467 insertions(+), 761 deletions(-) create mode 100644 source/psychlua/CameraFunctions.hx create mode 100644 source/psychlua/CustomState.hx create mode 100644 source/psychlua/ScriptHandler.hx create mode 100644 source/psychlua/VideoFunctions.hx diff --git a/source/animateatlas/AtlasFrameMaker.hx b/source/animateatlas/AtlasFrameMaker.hx index 8250aff..5e8a9ba 100644 --- a/source/animateatlas/AtlasFrameMaker.hx +++ b/source/animateatlas/AtlasFrameMaker.hx @@ -46,7 +46,7 @@ class AtlasFrameMaker extends FlxFramesCollection if (Paths.fileExists('images/$key/spritemap1.json', TEXT)) { - PlayState.instance.addTextToDebug("Only Spritemaps made with Adobe Animate 2018 are supported", FlxColor.RED); + psychlua.ScriptHandler.addTextToDebug("Only Spritemaps made with Adobe Animate 2018 are supported", FlxColor.RED); trace("Only Spritemaps made with Adobe Animate 2018 are supported"); return null; } diff --git a/source/backend/Mods.hx b/source/backend/Mods.hx index 9c4661e..ce23786 100644 --- a/source/backend/Mods.hx +++ b/source/backend/Mods.hx @@ -27,7 +27,8 @@ class Mods 'weeks', 'fonts', 'scripts', - 'achievements' + 'achievements', + 'states' ]; private static var globalMods:Array = []; diff --git a/source/backend/MusicBeatSubstate.hx b/source/backend/MusicBeatSubstate.hx index f89272e..9027754 100644 --- a/source/backend/MusicBeatSubstate.hx +++ b/source/backend/MusicBeatSubstate.hx @@ -30,6 +30,18 @@ class MusicBeatSubstate extends FlxSubState inline function get_controls():Controls return Controls.instance; + override function create() + { + // Moves debug group to front of this substate + if (psychlua.ScriptHandler.luaDebugGroup != null) + { + FlxG.state.remove(psychlua.ScriptHandler.luaDebugGroup); + add(psychlua.ScriptHandler.luaDebugGroup); + } + + super.create(); + } + override function update(elapsed:Float) { //everyStep(); @@ -136,16 +148,30 @@ class MusicBeatSubstate extends FlxSubState { if (curStep % 4 == 0) beatHit(); + + if (!FlxG.state.persistentUpdate) // Prevents scripts from being called twice + { + psychlua.ScriptHandler.setOnScripts('curStep', curStep); + psychlua.ScriptHandler.callOnScripts('onStepHit'); + } } public function beatHit():Void { - //do literally nothing dumbass + if (!FlxG.state.persistentUpdate) + { + psychlua.ScriptHandler.setOnScripts('curBeat', curBeat); + psychlua.ScriptHandler.callOnScripts('onBeatHit'); + } } - + public function sectionHit():Void { - //yep, you guessed it, nothing again, dumbass + if (!FlxG.state.persistentUpdate) + { + psychlua.ScriptHandler.setOnScripts('curSection', curSection); + psychlua.ScriptHandler.callOnScripts('onSectionHit'); + } } function getBeatsOnSection() @@ -154,4 +180,16 @@ class MusicBeatSubstate extends FlxSubState if(PlayState.SONG != null && PlayState.SONG.notes[curSection] != null) val = PlayState.SONG.notes[curSection].sectionBeats; return val == null ? 4 : val; } + + override function destroy() + { + // Moves debug group back to original state + if (psychlua.ScriptHandler.luaDebugGroup != null) + { + remove(psychlua.ScriptHandler.luaDebugGroup); + FlxG.state.add(psychlua.ScriptHandler.luaDebugGroup); + } + + super.destroy(); + } } diff --git a/source/cutscenes/DialogueBoxPsych.hx b/source/cutscenes/DialogueBoxPsych.hx index 5ea4742..51ea415 100644 --- a/source/cutscenes/DialogueBoxPsych.hx +++ b/source/cutscenes/DialogueBoxPsych.hx @@ -68,7 +68,7 @@ class DialogueBoxPsych extends FlxSpriteGroup { if (ignoreThisFrame) { - PlayState.instance.callOnScripts('onDialogueStart'); // It's here, because scripts might want to access PlayState.instance.psychDialogue (aka this) + psychlua.ScriptHandler.callOnScripts('onDialogueStart'); // It's here, because scripts might want to access PlayState.instance.psychDialogue (aka this) startNextDialog(); ignoreThisFrame = false; @@ -83,7 +83,7 @@ class DialogueBoxPsych extends FlxSpriteGroup if (bgFade.alpha > 0.5) bgFade.alpha = 0.5; - var ret:Dynamic = PlayState.instance.callOnScripts('onDialogueConfirm', null, false); + var ret:Dynamic = psychlua.ScriptHandler.callOnScripts('onDialogueConfirm', null, false); if ((confirmDialogue || Controls.instance.ACCEPT) && ret != FunkinLua.Function_Stop) { if (!typedText.finishedText) @@ -205,7 +205,7 @@ class DialogueBoxPsych extends FlxSpriteGroup var event:DialogueEvent = curDialogue.events[i]; // Call the current event - PlayState.instance.callOnScripts('onDialogueEvent', [event.type, event.arguments]); + psychlua.ScriptHandler.callOnScripts('onDialogueEvent', [event.type, event.arguments]); } } @@ -552,7 +552,7 @@ class DialogueBoxPsych extends FlxSpriteGroup currentCharAlpha = char.alpha; // Call Update - var ret:Dynamic = PlayState.instance.callOnScripts('onCharacterMove', [tag, char.curCharacter, char.startingPos, offsetPos, elapsed], false); + var ret:Dynamic = psychlua.ScriptHandler.callOnScripts('onCharacterMove', [tag, char.curCharacter, char.startingPos, offsetPos, elapsed], false); if (ret != FunkinLua.Function_Stop) { @@ -657,7 +657,7 @@ class DialogueBoxPsych extends FlxSpriteGroup typedText.setScale(0.7); typedText.onUpdate = function(text:String):Void { - PlayState.instance.callOnScripts('onDialogueTextUpdate', [text]); + psychlua.ScriptHandler.callOnScripts('onDialogueTextUpdate', [text]); }; add(typedText); } diff --git a/source/import.hx b/source/import.hx index 0307edf..ba5ad2d 100644 --- a/source/import.hx +++ b/source/import.hx @@ -9,9 +9,18 @@ import backend.Discord; import llua.*; import llua.Lua; import psychlua.*; +#else import psychlua.FunkinLua; +import psychlua.LuaUtils; +import psychlua.HScript; +import psychlua.ScriptHandler; +#end + +#if SScript +import tea.SScript; #end + // FlxAnimate #if flxanimate import flxanimate.*; diff --git a/source/psychlua/CallbackHandler.hx b/source/psychlua/CallbackHandler.hx index e03226b..7b4f859 100644 --- a/source/psychlua/CallbackHandler.hx +++ b/source/psychlua/CallbackHandler.hx @@ -19,7 +19,7 @@ class CallbackHandler if(last == null || last.lua != l) { //trace('looping thru scripts'); - for (script in PlayState.instance.luaArray) + for (script in psychlua.ScriptHandler.luaArray) if(script != FunkinLua.lastCalledScript && script != null && script.lua == l) { //trace('found script'); diff --git a/source/psychlua/CameraFunctions.hx b/source/psychlua/CameraFunctions.hx new file mode 100644 index 0000000..6aa20cf --- /dev/null +++ b/source/psychlua/CameraFunctions.hx @@ -0,0 +1,79 @@ +package psychlua; + +class CameraFunctions +{ + public static function implement(funk:FunkinLua) + { + var lua = funk.lua; + Lua_helper.add_callback(lua, "makeLuaCamera", function(tag:String, ?x:Float, ?y:Float, ?width:Int, ?height:Int, ?zoom:Float) { + tag = tag.replace('.', ''); + LuaUtils.resetTag(tag, psychlua.ScriptHandler.modchartCameras); + var newCam:FlxCamera = new FlxCamera(x, y, width, height, zoom); + newCam.bgColor.alpha = 0; + psychlua.ScriptHandler.modchartCameras.set(tag, newCam); + }); + + + Lua_helper.add_callback(lua, "addLuaCamera", function(tag:String, defaultDraw:Bool = false) { + if(psychlua.ScriptHandler.modchartCameras.exists(tag)) { + var shit:FlxCamera = psychlua.ScriptHandler.modchartCameras.get(tag); + FlxG.cameras.add(shit, defaultDraw); + } + }); + Lua_helper.add_callback(lua, "removeLuaCamera", function(tag:String, destroy:Bool = true) { + if(!psychlua.ScriptHandler.modchartCameras.exists(tag)) { + return; + } + + var pee:FlxCamera = psychlua.ScriptHandler.modchartCameras.get(tag); + if(destroy) pee.kill(); + + FlxG.cameras.remove(pee, false); + if(destroy) { + pee.destroy(); + psychlua.ScriptHandler.modchartCameras.remove(tag); + } + }); + + Lua_helper.add_callback(lua, "getCameraOrder", function(camera:String) { + var cam:FlxCamera = LuaUtils.cameraFromString(camera); + if(cam != null) return FlxG.cameras.list.indexOf(cam); + FunkinLua.luaTrace("getCameraOrder: Default camera doesn't exist!", false, false, FlxColor.RED); + return -1; + }); + Lua_helper.add_callback(lua, "setCameraOrder", function(camera:String, position:Int) { + var cam:FlxCamera = LuaUtils.cameraFromString(camera); + if(cam != null) { + if(FlxG.cameras.list.contains(cam)){ + var list:Array = FlxG.cameras.list.copy(); + var defaults:Array = @:privateAccess FlxG.cameras.defaults.copy(); + + for(flxCam in list) FlxG.cameras.remove(flxCam, false); + + list.remove(cam); + list.insert(position, cam); + + for(flxCam in list){ + var defaultDraw = defaults.contains(flxCam); + FlxG.cameras.add(flxCam, defaultDraw); + } + + return; + }else{ + FunkinLua.luaTrace('setCameraOrder: Camera $camera isn\'t in the list yet! Use "addLuaCamera($camera)" first', false, false, FlxColor.RED); + } + } + FunkinLua.luaTrace("setCameraOrder: Default camera doesn't exist!", false, false, FlxColor.RED); + }); + + Lua_helper.add_callback(lua, "cameraShake", function(camera:String, ?intensity:Float, ?duration:Float) { + LuaUtils.cameraFromString(camera).shake(intensity, duration); + }); + Lua_helper.add_callback(lua, "cameraFlash", function(camera:String, ?color:String, ?duration:Float, ?forced:Bool) { + LuaUtils.cameraFromString(camera).flash(CoolUtil.colorFromString(color), duration, null, forced); + }); + Lua_helper.add_callback(lua, "cameraFade", function(camera:String, ?color:String, ?duration:Float, ?fadeIn:Bool, ?forced:Bool) { + LuaUtils.cameraFromString(camera).fade(CoolUtil.colorFromString(color), duration, fadeIn, null, forced); + }); + } +} \ No newline at end of file diff --git a/source/psychlua/CustomState.hx b/source/psychlua/CustomState.hx new file mode 100644 index 0000000..a0e7258 --- /dev/null +++ b/source/psychlua/CustomState.hx @@ -0,0 +1,117 @@ +package psychlua; + +import flixel.FlxObject; +import flixel.FlxState; +import flixel.addons.transition.FlxTransitionableState; + +final packageMap:Map = [ + 'CustomState' => 'psychlua.', + + 'FlashingState' => 'states.', + 'TitleState' => 'states.', + 'OutdatedState' => 'states.', + 'MainMenuState' => 'states.', + 'StoryMenuState' => 'states.', + 'PlayState' => 'states.', + 'FreeplayState' => 'states.', + 'ModsMenuState' => 'states.', + 'CreditsState' => 'states.', + 'AchievementsMenuState' => 'states.', + + 'OptionsState' => 'options.', + 'NoteOffsetState' => 'options.', + + 'CharacterEditorState' => 'states.editors.', + 'ChartingState' => 'states.editors.', + 'DialogueCharacterEditorState' => 'states.editors.', + 'MasterEditorMenu' => 'states.editors.', + 'MenuCharacterEditorState' => 'states.editors.', + 'NoteSplashDebugState' => 'states.editors.', + 'WeekEditorState' => 'states.editors.' +]; + +class CustomState extends MusicBeatState +{ + public static var name:String = 'unnamed'; + public static var newName:Bool = false; + public static var instance:CustomState; + + #if LUA_ALLOWED + public static function implement(funk:FunkinLua) + { + var lua:State = funk.lua; + Lua_helper.add_callback(lua, "switchState", function(name:String, ?args:Array = null, ?load:Bool = false, ?stopMusic:Bool = false) { + if (args == null) args = []; + if (!name.contains('.') && packageMap.get(name) != null) + name = packageMap.get(name) + name; + + var state:FlxState = Type.createInstance(Type.resolveClass(name), args); + + if(state != null) { + if(load) { + LoadingState.loadAndSwitchState(() -> state, stopMusic); + } else { + if (stopMusic) FlxG.sound.music.stop(); + FlxG.switchState(() -> state); + } + } else { + FunkinLua.luaTrace('switchState: State "$name" doesn\'t exist!', false, false, FlxColor.RED); + } + }); + Lua_helper.add_callback(lua, "setSkipTransition", function(skipIn:Null, skipOut:Null) { + if(skipIn != null) FlxTransitionableState.skipNextTransIn = skipIn; + if(skipOut != null) FlxTransitionableState.skipNextTransOut = skipOut; + }); + Lua_helper.add_callback(lua, "resetState", function() { + FlxG.resetState(); + }); + Lua_helper.add_callback(lua, "clearStoredMemory", function() { + Paths.clearStoredMemory(); + }); + Lua_helper.add_callback(lua, "clearStoredMemory", function() { + Paths.clearStoredMemory(); + }); + Lua_helper.add_callback(lua, "clearUnusedMemory", function() { + Paths.clearUnusedMemory(); + }); + } + #end + + override function create() + { + instance = this; + newName = false; + + persistentUpdate = persistentDraw = true; + initPsychCamera(); + + psychlua.ScriptHandler.startScripts(); + #if LUA_ALLOWED psychlua.ScriptHandler.startLuasNamed('states/$name.lua'); #end + #if HSCRIPT_ALLOWED psychlua.ScriptHandler.startLuasNamed('states/$name.hx'); #end + + super.create(); + psychlua.ScriptHandler.callOnScripts('onCreatePost'); + } + + public function new(name:String) + { + CustomState.name = name; + newName = true; + super(); + } + + override function update(elapsed:Float) + { + psychlua.ScriptHandler.callOnScripts('onUpdate', [elapsed]); + super.update(elapsed); + psychlua.ScriptHandler.callOnScripts('onUpdatePost', [elapsed]); + } + + override function destroy() + { + psychlua.ScriptHandler.destroyScripts(); + if(!newName) name = 'unnamed'; + + super.destroy(); + } +} \ No newline at end of file diff --git a/source/psychlua/CustomSubstate.hx b/source/psychlua/CustomSubstate.hx index 01f09f7..194ded8 100644 --- a/source/psychlua/CustomSubstate.hx +++ b/source/psychlua/CustomSubstate.hx @@ -31,8 +31,8 @@ class CustomSubstate extends MusicBeatSubstate } } PlayState.instance.openSubState(new CustomSubstate(name)); - PlayState.instance.setOnHScript('customSubstate', instance); - PlayState.instance.setOnHScript('customSubstateName', name); + psychlua.ScriptHandler.setOnHScript('customSubstate', instance); + psychlua.ScriptHandler.setOnHScript('customSubstateName', name); } public static function closeCustomSubstate() @@ -50,8 +50,8 @@ class CustomSubstate extends MusicBeatSubstate { if(instance != null) { - var tagObject:FlxObject = cast (PlayState.instance.variables.get(tag), FlxObject); - #if LUA_ALLOWED if(tagObject == null) tagObject = cast (PlayState.instance.modchartSprites.get(tag), FlxObject); #end + var tagObject:FlxObject = cast (psychlua.ScriptHandler.variables.get(tag), FlxObject); + #if LUA_ALLOWED if(tagObject == null) tagObject = cast (psychlua.ScriptHandler.modchartSprites.get(tag), FlxObject); #end if(tagObject != null) { @@ -67,9 +67,9 @@ class CustomSubstate extends MusicBeatSubstate { instance = this; - PlayState.instance.callOnScripts('onCustomSubstateCreate', [name]); + psychlua.ScriptHandler.callOnScripts('onCustomSubstateCreate', [name]); super.create(); - PlayState.instance.callOnScripts('onCustomSubstateCreatePost', [name]); + psychlua.ScriptHandler.callOnScripts('onCustomSubstateCreatePost', [name]); } public function new(name:String) @@ -81,18 +81,18 @@ class CustomSubstate extends MusicBeatSubstate override function update(elapsed:Float) { - PlayState.instance.callOnScripts('onCustomSubstateUpdate', [name, elapsed]); + psychlua.ScriptHandler.callOnScripts('onCustomSubstateUpdate', [name, elapsed]); super.update(elapsed); - PlayState.instance.callOnScripts('onCustomSubstateUpdatePost', [name, elapsed]); + psychlua.ScriptHandler.callOnScripts('onCustomSubstateUpdatePost', [name, elapsed]); } override function destroy() { - PlayState.instance.callOnScripts('onCustomSubstateDestroy', [name]); + psychlua.ScriptHandler.callOnScripts('onCustomSubstateDestroy', [name]); name = 'unnamed'; - PlayState.instance.setOnHScript('customSubstate', null); - PlayState.instance.setOnHScript('customSubstateName', name); + psychlua.ScriptHandler.setOnHScript('customSubstate', null); + psychlua.ScriptHandler.setOnHScript('customSubstateName', name); super.destroy(); } } diff --git a/source/psychlua/DeprecatedFunctions.hx b/source/psychlua/DeprecatedFunctions.hx index c9d33ff..93260b9 100644 --- a/source/psychlua/DeprecatedFunctions.hx +++ b/source/psychlua/DeprecatedFunctions.hx @@ -46,13 +46,13 @@ class DeprecatedFunctions }); Lua_helper.add_callback(lua, "luaSpriteMakeGraphic", function(tag:String, width:Int, height:Int, color:String) { FunkinLua.luaTrace("luaSpriteMakeGraphic is deprecated! Use makeGraphic instead", false, true); - if(PlayState.instance.modchartSprites.exists(tag)) - PlayState.instance.modchartSprites.get(tag).makeGraphic(width, height, CoolUtil.colorFromString(color)); + if(psychlua.ScriptHandler.modchartSprites.exists(tag)) + psychlua.ScriptHandler.modchartSprites.get(tag).makeGraphic(width, height, CoolUtil.colorFromString(color)); }); Lua_helper.add_callback(lua, "luaSpriteAddAnimationByPrefix", function(tag:String, name:String, prefix:String, framerate:Int = 24, loop:Bool = true) { FunkinLua.luaTrace("luaSpriteAddAnimationByPrefix is deprecated! Use addAnimationByPrefix instead", false, true); - if(PlayState.instance.modchartSprites.exists(tag)) { - var cock:ModchartSprite = PlayState.instance.modchartSprites.get(tag); + if(psychlua.ScriptHandler.modchartSprites.exists(tag)) { + var cock:ModchartSprite = psychlua.ScriptHandler.modchartSprites.get(tag); cock.animation.addByPrefix(name, prefix, framerate, loop); if(cock.animation.curAnim == null) { cock.animation.play(name, true); @@ -61,13 +61,13 @@ class DeprecatedFunctions }); Lua_helper.add_callback(lua, "luaSpriteAddAnimationByIndices", function(tag:String, name:String, prefix:String, indices:String, framerate:Int = 24) { FunkinLua.luaTrace("luaSpriteAddAnimationByIndices is deprecated! Use addAnimationByIndices instead", false, true); - if(PlayState.instance.modchartSprites.exists(tag)) { + if(psychlua.ScriptHandler.modchartSprites.exists(tag)) { var strIndices:Array = indices.trim().split(','); var die:Array = []; for (i in 0...strIndices.length) { die.push(Std.parseInt(strIndices[i])); } - var pussy:ModchartSprite = PlayState.instance.modchartSprites.get(tag); + var pussy:ModchartSprite = psychlua.ScriptHandler.modchartSprites.get(tag); pussy.animation.addByIndices(name, prefix, die, '', framerate, false); if(pussy.animation.curAnim == null) { pussy.animation.play(name, true); @@ -76,14 +76,14 @@ class DeprecatedFunctions }); Lua_helper.add_callback(lua, "luaSpritePlayAnimation", function(tag:String, name:String, forced:Bool = false) { FunkinLua.luaTrace("luaSpritePlayAnimation is deprecated! Use playAnim instead", false, true); - if(PlayState.instance.modchartSprites.exists(tag)) { - PlayState.instance.modchartSprites.get(tag).animation.play(name, forced); + if(psychlua.ScriptHandler.modchartSprites.exists(tag)) { + psychlua.ScriptHandler.modchartSprites.get(tag).animation.play(name, forced); } }); Lua_helper.add_callback(lua, "setLuaSpriteCamera", function(tag:String, camera:String = '') { FunkinLua.luaTrace("setLuaSpriteCamera is deprecated! Use setObjectCamera instead", false, true); - if(PlayState.instance.modchartSprites.exists(tag)) { - PlayState.instance.modchartSprites.get(tag).cameras = [LuaUtils.cameraFromString(camera)]; + if(psychlua.ScriptHandler.modchartSprites.exists(tag)) { + psychlua.ScriptHandler.modchartSprites.get(tag).cameras = [LuaUtils.cameraFromString(camera)]; return true; } FunkinLua.luaTrace("Lua sprite with tag: " + tag + " doesn't exist!"); @@ -91,16 +91,16 @@ class DeprecatedFunctions }); Lua_helper.add_callback(lua, "setLuaSpriteScrollFactor", function(tag:String, scrollX:Float, scrollY:Float) { FunkinLua.luaTrace("setLuaSpriteScrollFactor is deprecated! Use setScrollFactor instead", false, true); - if(PlayState.instance.modchartSprites.exists(tag)) { - PlayState.instance.modchartSprites.get(tag).scrollFactor.set(scrollX, scrollY); + if(psychlua.ScriptHandler.modchartSprites.exists(tag)) { + psychlua.ScriptHandler.modchartSprites.get(tag).scrollFactor.set(scrollX, scrollY); return true; } return false; }); Lua_helper.add_callback(lua, "scaleLuaSprite", function(tag:String, x:Float, y:Float) { FunkinLua.luaTrace("scaleLuaSprite is deprecated! Use scaleObject instead", false, true); - if(PlayState.instance.modchartSprites.exists(tag)) { - var shit:ModchartSprite = PlayState.instance.modchartSprites.get(tag); + if(psychlua.ScriptHandler.modchartSprites.exists(tag)) { + var shit:ModchartSprite = psychlua.ScriptHandler.modchartSprites.get(tag); shit.scale.set(x, y); shit.updateHitbox(); return true; @@ -109,32 +109,32 @@ class DeprecatedFunctions }); Lua_helper.add_callback(lua, "getPropertyLuaSprite", function(tag:String, variable:String) { FunkinLua.luaTrace("getPropertyLuaSprite is deprecated! Use getProperty instead", false, true); - if(PlayState.instance.modchartSprites.exists(tag)) { + if(psychlua.ScriptHandler.modchartSprites.exists(tag)) { var killMe:Array = variable.split('.'); if(killMe.length > 1) { - var coverMeInPiss:Dynamic = Reflect.getProperty(PlayState.instance.modchartSprites.get(tag), killMe[0]); + var coverMeInPiss:Dynamic = Reflect.getProperty(psychlua.ScriptHandler.modchartSprites.get(tag), killMe[0]); for (i in 1...killMe.length-1) { coverMeInPiss = Reflect.getProperty(coverMeInPiss, killMe[i]); } return Reflect.getProperty(coverMeInPiss, killMe[killMe.length-1]); } - return Reflect.getProperty(PlayState.instance.modchartSprites.get(tag), variable); + return Reflect.getProperty(psychlua.ScriptHandler.modchartSprites.get(tag), variable); } return null; }); Lua_helper.add_callback(lua, "setPropertyLuaSprite", function(tag:String, variable:String, value:Dynamic) { FunkinLua.luaTrace("setPropertyLuaSprite is deprecated! Use setProperty instead", false, true); - if(PlayState.instance.modchartSprites.exists(tag)) { + if(psychlua.ScriptHandler.modchartSprites.exists(tag)) { var killMe:Array = variable.split('.'); if(killMe.length > 1) { - var coverMeInPiss:Dynamic = Reflect.getProperty(PlayState.instance.modchartSprites.get(tag), killMe[0]); + var coverMeInPiss:Dynamic = Reflect.getProperty(psychlua.ScriptHandler.modchartSprites.get(tag), killMe[0]); for (i in 1...killMe.length-1) { coverMeInPiss = Reflect.getProperty(coverMeInPiss, killMe[i]); } Reflect.setProperty(coverMeInPiss, killMe[killMe.length-1], value); return true; } - Reflect.setProperty(PlayState.instance.modchartSprites.get(tag), variable, value); + Reflect.setProperty(psychlua.ScriptHandler.modchartSprites.get(tag), variable, value); return true; } FunkinLua.luaTrace("setPropertyLuaSprite: Lua sprite with tag: " + tag + " doesn't exist!"); diff --git a/source/psychlua/ExtraFunctions.hx b/source/psychlua/ExtraFunctions.hx index 7ccbbc6..199751b 100644 --- a/source/psychlua/ExtraFunctions.hx +++ b/source/psychlua/ExtraFunctions.hx @@ -89,61 +89,61 @@ class ExtraFunctions Lua_helper.add_callback(lua, "keyJustPressed", function(name:String = '') { name = name.toLowerCase(); switch(name) { - case 'left': return PlayState.instance.controls.NOTE_LEFT_P; - case 'down': return PlayState.instance.controls.NOTE_DOWN_P; - case 'up': return PlayState.instance.controls.NOTE_UP_P; - case 'right': return PlayState.instance.controls.NOTE_RIGHT_P; - default: return PlayState.instance.controls.justPressed(name); + case 'left': return Controls.instance.NOTE_LEFT_P; + case 'down': return Controls.instance.NOTE_DOWN_P; + case 'up': return Controls.instance.NOTE_UP_P; + case 'right': return Controls.instance.NOTE_RIGHT_P; + default: return Controls.instance.justPressed(name); } return false; }); Lua_helper.add_callback(lua, "keyPressed", function(name:String = '') { name = name.toLowerCase(); switch(name) { - case 'left': return PlayState.instance.controls.NOTE_LEFT; - case 'down': return PlayState.instance.controls.NOTE_DOWN; - case 'up': return PlayState.instance.controls.NOTE_UP; - case 'right': return PlayState.instance.controls.NOTE_RIGHT; - default: return PlayState.instance.controls.pressed(name); + case 'left': return Controls.instance.NOTE_LEFT; + case 'down': return Controls.instance.NOTE_DOWN; + case 'up': return Controls.instance.NOTE_UP; + case 'right': return Controls.instance.NOTE_RIGHT; + default: return Controls.instance.pressed(name); } return false; }); Lua_helper.add_callback(lua, "keyReleased", function(name:String = '') { name = name.toLowerCase(); switch(name) { - case 'left': return PlayState.instance.controls.NOTE_LEFT_R; - case 'down': return PlayState.instance.controls.NOTE_DOWN_R; - case 'up': return PlayState.instance.controls.NOTE_UP_R; - case 'right': return PlayState.instance.controls.NOTE_RIGHT_R; - default: return PlayState.instance.controls.justReleased(name); + case 'left': return Controls.instance.NOTE_LEFT_R; + case 'down': return Controls.instance.NOTE_DOWN_R; + case 'up': return Controls.instance.NOTE_UP_R; + case 'right': return Controls.instance.NOTE_RIGHT_R; + default: return Controls.instance.justReleased(name); } return false; }); // Save data management - Lua_helper.add_callback(lua, "initSaveData", function(name:String, ?folder:String = 'psychenginemods') { - if(!PlayState.instance.modchartSaves.exists(name)) + Lua_helper.add_callback(lua, "initSaveData", function(name:String, ?folder:String = 'alleywaymods') { + if(!psychlua.ScriptHandler.modchartSaves.exists(name)) { var save:FlxSave = new FlxSave(); // folder goes unused for flixel 5 users. @BeastlyGhost save.bind(name, CoolUtil.getSavePath() + '/' + folder); - PlayState.instance.modchartSaves.set(name, save); + psychlua.ScriptHandler.modchartSaves.set(name, save); return; } FunkinLua.luaTrace('initSaveData: Save file already initialized: ' + name); }); Lua_helper.add_callback(lua, "flushSaveData", function(name:String) { - if(PlayState.instance.modchartSaves.exists(name)) + if(psychlua.ScriptHandler.modchartSaves.exists(name)) { - PlayState.instance.modchartSaves.get(name).flush(); + psychlua.ScriptHandler.modchartSaves.get(name).flush(); return; } FunkinLua.luaTrace('flushSaveData: Save file not initialized: ' + name, false, false, FlxColor.RED); }); Lua_helper.add_callback(lua, "getDataFromSave", function(name:String, field:String, ?defaultValue:Dynamic = null) { - if(PlayState.instance.modchartSaves.exists(name)) + if(psychlua.ScriptHandler.modchartSaves.exists(name)) { - var saveData = PlayState.instance.modchartSaves.get(name).data; + var saveData = psychlua.ScriptHandler.modchartSaves.get(name).data; if(Reflect.hasField(saveData, field)) return Reflect.field(saveData, field); else @@ -153,18 +153,18 @@ class ExtraFunctions return defaultValue; }); Lua_helper.add_callback(lua, "setDataFromSave", function(name:String, field:String, value:Dynamic) { - if(PlayState.instance.modchartSaves.exists(name)) + if(psychlua.ScriptHandler.modchartSaves.exists(name)) { - Reflect.setField(PlayState.instance.modchartSaves.get(name).data, field, value); + Reflect.setField(psychlua.ScriptHandler.modchartSaves.get(name).data, field, value); return; } FunkinLua.luaTrace('setDataFromSave: Save file not initialized: ' + name, false, false, FlxColor.RED); }); Lua_helper.add_callback(lua, "eraseSaveData", function(name:String) { - if (PlayState.instance.modchartSaves.exists(name)) + if (psychlua.ScriptHandler.modchartSaves.exists(name)) { - PlayState.instance.modchartSaves.get(name).erase(); + psychlua.ScriptHandler.modchartSaves.get(name).erase(); return; } FunkinLua.luaTrace('eraseSaveData: Save file not initialized: ' + name, false, false, FlxColor.RED); @@ -237,6 +237,9 @@ class ExtraFunctions Lua_helper.add_callback(lua, "getTextFromFile", function(path:String, ?ignoreModFolders:Bool = false) { return Paths.getTextFromFile(path, ignoreModFolders); }); + Lua_helper.add_callback(lua, "parseJsonFromFile", function(path:String, ?ignoreModFolders:Bool = false) { + return tjson.TJSON.parse(Paths.getTextFromFile(path, ignoreModFolders)); + }); Lua_helper.add_callback(lua, "directoryFileList", function(folder:String) { var list:Array = []; #if sys @@ -289,5 +292,27 @@ class ExtraFunctions Lua_helper.add_callback(lua, "getRandomBool", function(chance:Float = 50) { return FlxG.random.bool(chance); }); + Lua_helper.add_callback(lua, "getRandomSign", function(chance:Float = 50) { + return FlxG.random.sign(chance); + }); + Lua_helper.add_callback(lua, "getRandomColor", function(?min:String, ?max:String, ?alpha:Int, greyScale:Bool = false) { + var min:FlxColor = CoolUtil.colorFromString(min); + var max:FlxColor = CoolUtil.colorFromString(max); + return FlxG.random.color(min, max, alpha, greyScale); + }); + Lua_helper.add_callback(lua, "getRandomObject", function(objs:Array, weights:Array, start:Int = 0, ?end:Null) { + return FlxG.random.getObject(objs, weights, start, end); + }); + Lua_helper.add_callback(lua, "shuffleArray", function(array:Array) { + FlxG.random.shuffle(array); + }); + + // Math + Lua_helper.add_callback(lua, "bound", function(value:Float, ?min:Float, ?max:Float) { + return FlxMath.bound(value, min, max); + }); + Lua_helper.add_callback(lua, "lerp", function(a:Float, b:Float, ratio:Float) { + return FlxMath.lerp(a, b, ratio); + }); } } diff --git a/source/psychlua/FlxAnimateFunctions.hx b/source/psychlua/FlxAnimateFunctions.hx index 157eda1..63e0e4d 100644 --- a/source/psychlua/FlxAnimateFunctions.hx +++ b/source/psychlua/FlxAnimateFunctions.hx @@ -10,7 +10,7 @@ class FlxAnimateFunctions var lua:State = funk.lua; Lua_helper.add_callback(lua, "makeFlxAnimateSprite", function(tag:String, ?x:Float = 0, ?y:Float = 0, ?loadFolder:String = null) { tag = tag.replace('.', ''); - var lastSprite = PlayState.instance.variables.get(tag); + var lastSprite = psychlua.ScriptHandler.variables.get(tag); if(lastSprite != null) { lastSprite.kill(); @@ -20,18 +20,18 @@ class FlxAnimateFunctions var mySprite:ModchartAnimateSprite = new ModchartAnimateSprite(x, y); if(loadFolder != null) loadAtlasCustom(mySprite, loadFolder); - PlayState.instance.variables.set(tag, mySprite); + psychlua.ScriptHandler.variables.set(tag, mySprite); mySprite.active = true; }); Lua_helper.add_callback(lua, "loadAnimateAtlas", function(tag:String, folderOrImg:Dynamic, ?spriteJson:Dynamic = null, ?animationJson:Dynamic = null) { - var spr:FlxAnimate = PlayState.instance.variables.get(tag); + var spr:FlxAnimate = psychlua.ScriptHandler.variables.get(tag); if(spr != null) loadAtlasCustom(spr, folderOrImg, spriteJson, animationJson); }); Lua_helper.add_callback(lua, "addAnimationBySymbol", function(tag:String, name:String, symbol:String, ?framerate:Float = 24, ?loop:Bool = false, ?matX:Float = 0, ?matY:Float = 0) { - var obj:Dynamic = PlayState.instance.variables.get(tag); + var obj:Dynamic = psychlua.ScriptHandler.variables.get(tag); if(cast (obj, FlxAnimate) == null) return false; obj.anim.addBySymbol(name, symbol, framerate, loop, matX, matY); @@ -45,7 +45,7 @@ class FlxAnimateFunctions Lua_helper.add_callback(lua, "addAnimationBySymbolIndices", function(tag:String, name:String, symbol:String, ?indices:Any = null, ?framerate:Float = 24, ?loop:Bool = false, ?matX:Float = 0, ?matY:Float = 0) { - var obj:Dynamic = PlayState.instance.variables.get(tag); + var obj:Dynamic = psychlua.ScriptHandler.variables.get(tag); if(cast (obj, FlxAnimate) == null) return false; if(indices == null) diff --git a/source/psychlua/FunkinLua.hx b/source/psychlua/FunkinLua.hx index e73340a..338a65a 100644 --- a/source/psychlua/FunkinLua.hx +++ b/source/psychlua/FunkinLua.hx @@ -9,6 +9,7 @@ import openfl.utils.Assets; import openfl.display.BitmapData; import flixel.FlxBasic; import flixel.FlxObject; +import flixel.group.FlxGroup; import flixel.addons.transition.FlxTransitionableState; #if (!flash && sys) @@ -80,8 +81,9 @@ class FunkinLua { LuaL.openlibs(lua); this.scriptName = scriptName.trim(); - var game:PlayState = PlayState.instance; - game.luaArray.push(this); + var game:Dynamic = FlxG.state; + var gameName:Array = Type.getClassName(Type.getClass(game)).split('.'); + psychlua.ScriptHandler.luaArray.push(this); var myFolder:Array = this.scriptName.split('/'); #if MODS_ALLOWED @@ -89,6 +91,19 @@ class FunkinLua { this.modFolder = myFolder[1]; #end + // Meta shit + set('metaTitle', FlxG.stage.application.meta.get('name')); + set('metaFile', FlxG.stage.application.meta.get('file')); + set('metaCompany', FlxG.stage.application.meta.get('company')); + + set('debugMode', (#if debug true #else false #end)); + set('modsAllowed', (#if MODS_ALLOWED true #else false #end)); + //set('luaAllowed', (#if LUA_ALLOWED true #else false #end)); + set('hScriptAllowed', (#if HSCRIPT_ALLOWED true #else false #end)); + set('achievementsAllowed', (#if ACHIEVEMENTS_ALLOWED true #else false #end)); + set('discordAllowed', (#if DISCORD_ALLOWED true #else false #end)); + set('videosAllowed', (#if VIDEOS_ALLOWED true #else false #end)); + // Lua shit set('Function_StopLua', Function_StopLua); set('Function_StopHScript', Function_StopHScript); @@ -101,15 +116,25 @@ class FunkinLua { // Song/Week shit set('curBpm', Conductor.bpm); - set('bpm', PlayState.SONG.bpm); - set('scrollSpeed', PlayState.SONG.speed); set('crochet', Conductor.crochet); set('stepCrochet', Conductor.stepCrochet); - set('songLength', FlxG.sound.music.length); - set('songName', PlayState.SONG.song); - set('songPath', Paths.formatToSongPath(PlayState.SONG.song)); set('startedCountdown', false); - set('curStage', PlayState.SONG.stage); + + if(PlayState.SONG != null) { + set('bpm', PlayState.SONG.bpm); + set('scrollSpeed', PlayState.SONG.speed); + set('songLength', FlxG.sound.music.length); + set('songName', PlayState.SONG.song); + set('songPath', Paths.formatToSongPath(PlayState.SONG.song)); + set('curStage', PlayState.SONG.stage); + set('hasVocals', PlayState.SONG.needsVoices); + set('arrowSkin', PlayState.SONG.arrowSkin); + + // Character shit + set('boyfriendName', PlayState.SONG.player1); + set('dadName', PlayState.SONG.player2); + set('gfName', PlayState.SONG.gfVersion); + } set('isStoryMode', PlayState.isStoryMode); set('difficulty', PlayState.storyDifficulty); @@ -119,7 +144,6 @@ class FunkinLua { set('weekRaw', PlayState.storyWeek); set('week', WeekData.weeksList[PlayState.storyWeek]); set('seenCutscene', PlayState.seenCutscene); - set('hasVocals', PlayState.SONG.needsVoices); // Camera poo set('cameraX', 0); @@ -154,34 +178,31 @@ class FunkinLua { set('gfSection', false); // Gameplay settings - set('healthGainMult', game.healthGain); - set('healthLossMult', game.healthLoss); - set('playbackRate', #if FLX_PITCH game.playbackRate #else 1 #end); - set('guitarHeroSustains', game.guitarHeroSustains); - set('instakillOnMiss', game.instakillOnMiss); - set('botPlay', game.cpuControlled); - set('practice', game.practiceMode); - - for (i in 0...4) { - set('defaultPlayerStrumX' + i, 0); - set('defaultPlayerStrumY' + i, 0); - set('defaultOpponentStrumX' + i, 0); - set('defaultOpponentStrumY' + i, 0); + if (game == PlayState.instance) { + set('healthGainMult', game.healthGain); + set('healthLossMult', game.healthLoss); + set('playbackRate', #if FLX_PITCH game.playbackRate #else 1 #end); + set('guitarHeroSustains', game.guitarHeroSustains); + set('instakillOnMiss', game.instakillOnMiss); + set('botPlay', game.cpuControlled); + set('practice', game.practiceMode); + + for (i in 0...4) { + set('defaultPlayerStrumX' + i, 0); + set('defaultPlayerStrumY' + i, 0); + set('defaultOpponentStrumX' + i, 0); + set('defaultOpponentStrumY' + i, 0); + } + + // Default character positions woooo + set('defaultBoyfriendX', game.BF_X); + set('defaultBoyfriendY', game.BF_Y); + set('defaultOpponentX', game.DAD_X); + set('defaultOpponentY', game.DAD_Y); + set('defaultGirlfriendX', game.GF_X); + set('defaultGirlfriendY', game.GF_Y); } - // Default character positions woooo - set('defaultBoyfriendX', game.BF_X); - set('defaultBoyfriendY', game.BF_Y); - set('defaultOpponentX', game.DAD_X); - set('defaultOpponentY', game.DAD_Y); - set('defaultGirlfriendX', game.GF_X); - set('defaultGirlfriendY', game.GF_Y); - - // Character shit - set('boyfriendName', PlayState.SONG.player1); - set('dadName', PlayState.SONG.player2); - set('gfName', PlayState.SONG.gfVersion); - // Some settings, no jokes set('downscroll', ClientPrefs.data.downScroll); set('middlescroll', ClientPrefs.data.middleScroll); @@ -197,9 +218,13 @@ class FunkinLua { set('noResetButton', ClientPrefs.data.noReset); set('lowQuality', ClientPrefs.data.lowQuality); set('shadersEnabled', ClientPrefs.data.shaders); + set('scriptName', scriptName); set('currentModDirectory', Mods.currentModDirectory); + set('stateName', gameName[gameName.length-1]); + set('customStateName', CustomState.name); + // Noteskin/Splash set('noteSkin', ClientPrefs.data.noteSkin); set('noteSkinPostfix', Note.getNoteSkinPostfix()); @@ -219,7 +244,7 @@ class FunkinLua { // Lua_helper.add_callback(lua, "getRunningScripts", function(){ var runningScripts:Array = []; - for (script in game.luaArray) + for (script in psychlua.ScriptHandler.luaArray) runningScripts.push(script.scriptName); return runningScripts; @@ -228,38 +253,53 @@ class FunkinLua { addLocalCallback("setOnScripts", function(varName:String, arg:Dynamic, ?ignoreSelf:Bool = false, ?exclusions:Array = null) { if(exclusions == null) exclusions = []; if(ignoreSelf && !exclusions.contains(scriptName)) exclusions.push(scriptName); - game.setOnScripts(varName, arg, exclusions); + psychlua.ScriptHandler.setOnScripts(varName, arg, exclusions); }); addLocalCallback("setOnHScript", function(varName:String, arg:Dynamic, ?ignoreSelf:Bool = false, ?exclusions:Array = null) { if(exclusions == null) exclusions = []; if(ignoreSelf && !exclusions.contains(scriptName)) exclusions.push(scriptName); - game.setOnHScript(varName, arg, exclusions); + psychlua.ScriptHandler.setOnHScript(varName, arg, exclusions); }); addLocalCallback("setOnLuas", function(varName:String, arg:Dynamic, ?ignoreSelf:Bool = false, ?exclusions:Array = null) { if(exclusions == null) exclusions = []; if(ignoreSelf && !exclusions.contains(scriptName)) exclusions.push(scriptName); - game.setOnLuas(varName, arg, exclusions); + psychlua.ScriptHandler.setOnLuas(varName, arg, exclusions); }); - addLocalCallback("callOnScripts", function(funcName:String, ?args:Array = null, ?ignoreStops=false, ?ignoreSelf:Bool = true, ?excludeScripts:Array = null, ?excludeValues:Array = null) { + addLocalCallback("callOnScripts", function(funcName:String, ?args:Array = null, ?ignoreStops:Bool = false, ?ignoreSelf:Bool = true, ?excludeScripts:Array = null, ?excludeValues:Array = null) { if(excludeScripts == null) excludeScripts = []; if(ignoreSelf && !excludeScripts.contains(scriptName)) excludeScripts.push(scriptName); - game.callOnScripts(funcName, args, ignoreStops, excludeScripts, excludeValues); + psychlua.ScriptHandler.callOnScripts(funcName, args, ignoreStops, excludeScripts, excludeValues); return true; }); - addLocalCallback("callOnLuas", function(funcName:String, ?args:Array = null, ?ignoreStops=false, ?ignoreSelf:Bool = true, ?excludeScripts:Array = null, ?excludeValues:Array = null) { + addLocalCallback("callOnLuas", function(funcName:String, ?args:Array = null, ?ignoreStops:Bool = false, ?ignoreSelf:Bool = true, ?excludeScripts:Array = null, ?excludeValues:Array = null) { if(excludeScripts == null) excludeScripts = []; if(ignoreSelf && !excludeScripts.contains(scriptName)) excludeScripts.push(scriptName); - game.callOnLuas(funcName, args, ignoreStops, excludeScripts, excludeValues); + psychlua.ScriptHandler.callOnLuas(funcName, args, ignoreStops, excludeScripts, excludeValues); return true; }); - addLocalCallback("callOnHScript", function(funcName:String, ?args:Array = null, ?ignoreStops=false, ?ignoreSelf:Bool = true, ?excludeScripts:Array = null, ?excludeValues:Array = null) { + addLocalCallback("callOnHScript", function(funcName:String, ?args:Array = null, ?ignoreStops:Bool = false, ?ignoreSelf:Bool = true, ?excludeScripts:Array = null, ?excludeValues:Array = null) { if(excludeScripts == null) excludeScripts = []; if(ignoreSelf && !excludeScripts.contains(scriptName)) excludeScripts.push(scriptName); - game.callOnHScript(funcName, args, ignoreStops, excludeScripts, excludeValues); + psychlua.ScriptHandler.callOnHScript(funcName, args, ignoreStops, excludeScripts, excludeValues); return true; }); + addLocalCallback("startLuasNamed", function(path:String) { + if(!path.endsWith('.lua')) path += '.lua'; + psychlua.ScriptHandler.startLuasNamed(path); + return true; + }); + addLocalCallback("startHScriptsNamed", function(path:String) { + if(!path.endsWith('.hx')) path += '.hx'; + psychlua.ScriptHandler.startHScriptsNamed(path); + return true; + }); + + addLocalCallback("detatchFromTag", function(tag:String) { + return psychlua.ScriptHandler.detatchFromTag(tag); + }); + Lua_helper.add_callback(lua, "callScript", function(luaFile:String, funcName:String, ?args:Array = null) { if(args == null){ args = []; @@ -267,7 +307,7 @@ class FunkinLua { var foundScript:String = findScript(luaFile); if(foundScript != null) - for (luaInstance in game.luaArray) + for (luaInstance in psychlua.ScriptHandler.luaArray) if(luaInstance.scriptName == foundScript) { luaInstance.call(funcName, args); @@ -278,7 +318,7 @@ class FunkinLua { Lua_helper.add_callback(lua, "getGlobalFromScript", function(luaFile:String, global:String) { // returns the global from a script var foundScript:String = findScript(luaFile); if(foundScript != null) - for (luaInstance in game.luaArray) + for (luaInstance in psychlua.ScriptHandler.luaArray) if(luaInstance.scriptName == foundScript) { Lua.getglobal(luaInstance.lua, global); @@ -301,7 +341,7 @@ class FunkinLua { Lua_helper.add_callback(lua, "setGlobalFromScript", function(luaFile:String, global:String, val:Dynamic) { // returns the global from a script var foundScript:String = findScript(luaFile); if(foundScript != null) - for (luaInstance in game.luaArray) + for (luaInstance in psychlua.ScriptHandler.luaArray) if(luaInstance.scriptName == foundScript) luaInstance.set(global, val); }); @@ -309,7 +349,7 @@ class FunkinLua { var foundScript:String = findScript(luaFile); if(foundScript != null) { - for (luaInstance in game.luaArray) + for (luaInstance in psychlua.ScriptHandler.luaArray) { if(luaInstance.scriptName == foundScript) { @@ -366,18 +406,41 @@ class FunkinLua { Lua_helper.add_callback(lua, "isRunning", function(luaFile:String) { var foundScript:String = findScript(luaFile); if(foundScript != null) - for (luaInstance in game.luaArray) + for (luaInstance in psychlua.ScriptHandler.luaArray) if(luaInstance.scriptName == foundScript) return true; return false; }); Lua_helper.add_callback(lua, "setVar", function(varName:String, value:Dynamic) { - PlayState.instance.variables.set(varName, value); + psychlua.ScriptHandler.variables.set(varName, value); return value; }); Lua_helper.add_callback(lua, "getVar", function(varName:String) { - return PlayState.instance.variables.get(varName); + return psychlua.ScriptHandler.variables.get(varName); + }); + Lua_helper.add_callback(lua, "removeVar", function(varName:String) { + return psychlua.ScriptHandler.variables.remove(varName); + }); + Lua_helper.add_callback(lua, "setGlobalVar", function(varName:String, value:Dynamic, replace:Bool = true) { + if(psychlua.ScriptHandler.globalVariables.get(varName) == null || psychlua.ScriptHandler.globalVariables.get(varName) != null && replace) + psychlua.ScriptHandler.globalVariables.set(varName, value); + return value; + }); + Lua_helper.add_callback(lua, "getGlobalVar", function(varName:String) { + return psychlua.ScriptHandler.globalVariables.get(varName); + }); + Lua_helper.add_callback(lua, "removeGlobalVar", function(varName:String) { + return psychlua.ScriptHandler.globalVariables.remove(varName); + }); + Lua_helper.add_callback(lua, "removeVarOnReset", function(varName:String, set:Bool = true) { + if(ScriptHandler.globalVariables.exists(varName)) + if(set) + ScriptHandler.resetVariables.push(varName) + else + ScriptHandler.resetVariables.remove(varName) + else + luaTrace('removeVarOnReset: Global var "' + varName + '" doesn\'t exist!'); }); Lua_helper.add_callback(lua, "addLuaScript", function(luaFile:String, ?ignoreAlreadyRunning:Bool = false) { //would be dope asf. @@ -385,7 +448,7 @@ class FunkinLua { if(foundScript != null) { if(!ignoreAlreadyRunning) - for (luaInstance in game.luaArray) + for (luaInstance in psychlua.ScriptHandler.luaArray) if(luaInstance.scriptName == foundScript) { luaTrace('addLuaScript: The script "' + foundScript + '" is already running!'); @@ -403,14 +466,14 @@ class FunkinLua { if(foundScript != null) { if(!ignoreAlreadyRunning) - for (script in game.hscriptArray) + for (script in psychlua.ScriptHandler.hscriptArray) if(script.origin == foundScript) { luaTrace('addHScript: The script "' + foundScript + '" is already running!'); return; } - PlayState.instance.initHScript(foundScript); + psychlua.ScriptHandler.initHScript(foundScript); return; } luaTrace("addHScript: Script doesn't exist!", false, false, FlxColor.RED); @@ -423,7 +486,7 @@ class FunkinLua { if(foundScript != null) { if(!ignoreAlreadyRunning) - for (luaInstance in game.luaArray) + for (luaInstance in psychlua.ScriptHandler.luaArray) if(luaInstance.scriptName == foundScript) { luaInstance.stop(); @@ -439,7 +502,7 @@ class FunkinLua { var foundScript:String = findScript(luaFile, '.hx'); if(foundScript != null){ if(!ignoreAlreadyRunning) - for (script in game.hscriptArray) + for (script in psychlua.ScriptHandler.hscriptArray) if(script.origin == foundScript){ trace('Closing script ' + script.origin); script.destroy(); @@ -500,6 +563,24 @@ class FunkinLua { spr.loadGraphic(Paths.image(image), animated, gridX, gridY); } }); + Lua_helper.add_callback(lua, "loadGraphicFromSprite", function(varTo:String, varFrom:String) { + var killMeA:Array = varTo.split('.'); + var killMeB:Array = varFrom.split('.'); + var sprA:FlxSprite = LuaUtils.getObjectDirectly(killMeA[0]); + var sprB:FlxSprite = LuaUtils.getObjectDirectly(killMeB[0]); + + if(killMeA.length > 1) { + sprA = LuaUtils.getVarInArray(LuaUtils.getPropertyLoop(killMeA), killMeA[killMeA.length-1]); + } + if(killMeB.length > 1) { + sprB = LuaUtils.getVarInArray(LuaUtils.getPropertyLoop(killMeB), killMeB[killMeB.length-1]); + } + + if(sprA != null) + { + sprA.loadGraphicFromSprite(sprB); + } + }); Lua_helper.add_callback(lua, "loadFrames", function(variable:String, image:String, spriteType:String = "sparrow") { var split:Array = variable.split('.'); var spr:FlxSprite = LuaUtils.getObjectDirectly(split[0]); @@ -549,21 +630,21 @@ class FunkinLua { if(penisExam != null) { if(values != null) { var myOptions:LuaTweenOptions = LuaUtils.getLuaTween(options); - game.modchartTweens.set(tag, FlxTween.tween(penisExam, values, duration, { + psychlua.ScriptHandler.modchartTweens.set(tag, FlxTween.tween(penisExam, values, duration, { type: myOptions.type, ease: myOptions.ease, startDelay: myOptions.startDelay, loopDelay: myOptions.loopDelay, onUpdate: function(twn:FlxTween) { - if(myOptions.onUpdate != null) game.callOnLuas(myOptions.onUpdate, [tag, vars]); + if(myOptions.onUpdate != null) psychlua.ScriptHandler.callOnLuas(myOptions.onUpdate, [tag, vars]); }, onStart: function(twn:FlxTween) { - if(myOptions.onStart != null) game.callOnLuas(myOptions.onStart, [tag, vars]); + if(myOptions.onStart != null) psychlua.ScriptHandler.callOnLuas(myOptions.onStart, [tag, vars]); }, onComplete: function(twn:FlxTween) { - if(myOptions.onComplete != null) game.callOnLuas(myOptions.onComplete, [tag, vars]); - if(twn.type == FlxTweenType.ONESHOT || twn.type == FlxTweenType.BACKWARD) game.modchartTweens.remove(tag); + if(myOptions.onComplete != null) psychlua.ScriptHandler.callOnLuas(myOptions.onComplete, [tag, vars]); + if(twn.type == FlxTweenType.ONESHOT || twn.type == FlxTweenType.BACKWARD) psychlua.ScriptHandler.modchartTweens.remove(tag); } })); } else { @@ -574,6 +655,10 @@ class FunkinLua { } }); + Lua_helper.add_callback(lua, "getTweenEase", function(ease:String, t:Float) { + return LuaUtils.getTweenEaseByString(ease)(t); + }); + Lua_helper.add_callback(lua, "doTweenX", function(tag:String, vars:String, value:Dynamic, duration:Float, ease:String) { oldTweenFunction(tag, vars, {x: value}, duration, ease, 'doTweenX'); }); @@ -594,10 +679,10 @@ class FunkinLua { if(penisExam != null) { var curColor:FlxColor = penisExam.color; curColor.alphaFloat = penisExam.alpha; - game.modchartTweens.set(tag, FlxTween.color(penisExam, duration, curColor, CoolUtil.colorFromString(targetColor), {ease: LuaUtils.getTweenEaseByString(ease), + psychlua.ScriptHandler.modchartTweens.set(tag, FlxTween.color(penisExam, duration, curColor, CoolUtil.colorFromString(targetColor), {ease: LuaUtils.getTweenEaseByString(ease), onComplete: function(twn:FlxTween) { - game.modchartTweens.remove(tag); - game.callOnLuas('onTweenCompleted', [tag, vars]); + psychlua.ScriptHandler.modchartTweens.remove(tag); + psychlua.ScriptHandler.callOnLuas('onTweenCompleted', [tag, vars]); } })); } else { @@ -605,17 +690,29 @@ class FunkinLua { } }); + Lua_helper.add_callback(lua, "doTweenNumber", function(tag:String, from:Float, to:Float, duration:Float, ease:String) { + psychlua.ScriptHandler.modchartTweens.set(tag, FlxTween.num(from, to, duration, {ease: LuaUtils.getTweenEaseByString(ease)}, function(v) { + psychlua.ScriptHandler.callOnLuas('onTweenUpdate', [tag, v]); + })); + }); + + Lua_helper.add_callback(lua, "cancelTween", function(tag:String) { + LuaUtils.cancelTween(tag); + }); + //Tween shit, but for strums - Lua_helper.add_callback(lua, "noteTweenX", function(tag:String, note:Int, value:Dynamic, duration:Float, ease:String) { + if(game == PlayState.instance) { + var game:PlayState = game; + Lua_helper.add_callback(lua, "noteTweenX", function(tag:String, note:Int, value:Dynamic, duration:Float, ease:String) { LuaUtils.cancelTween(tag); if(note < 0) note = 0; var testicle:StrumNote = game.strumLineNotes.members[note % game.strumLineNotes.length]; if(testicle != null) { - game.modchartTweens.set(tag, FlxTween.tween(testicle, {x: value}, duration, {ease: LuaUtils.getTweenEaseByString(ease), + psychlua.ScriptHandler.modchartTweens.set(tag, FlxTween.tween(testicle, {x: value}, duration, {ease: LuaUtils.getTweenEaseByString(ease), onComplete: function(twn:FlxTween) { - game.callOnLuas('onTweenCompleted', [tag]); - game.modchartTweens.remove(tag); + psychlua.ScriptHandler.callOnLuas('onTweenCompleted', [tag]); + psychlua.ScriptHandler.modchartTweens.remove(tag); } })); } @@ -626,10 +723,10 @@ class FunkinLua { var testicle:StrumNote = game.strumLineNotes.members[note % game.strumLineNotes.length]; if(testicle != null) { - game.modchartTweens.set(tag, FlxTween.tween(testicle, {y: value}, duration, {ease: LuaUtils.getTweenEaseByString(ease), + psychlua.ScriptHandler.modchartTweens.set(tag, FlxTween.tween(testicle, {y: value}, duration, {ease: LuaUtils.getTweenEaseByString(ease), onComplete: function(twn:FlxTween) { - game.callOnLuas('onTweenCompleted', [tag]); - game.modchartTweens.remove(tag); + psychlua.ScriptHandler.callOnLuas('onTweenCompleted', [tag]); + psychlua.ScriptHandler.modchartTweens.remove(tag); } })); } @@ -640,10 +737,10 @@ class FunkinLua { var testicle:StrumNote = game.strumLineNotes.members[note % game.strumLineNotes.length]; if(testicle != null) { - game.modchartTweens.set(tag, FlxTween.tween(testicle, {angle: value}, duration, {ease: LuaUtils.getTweenEaseByString(ease), + psychlua.ScriptHandler.modchartTweens.set(tag, FlxTween.tween(testicle, {angle: value}, duration, {ease: LuaUtils.getTweenEaseByString(ease), onComplete: function(twn:FlxTween) { - game.callOnLuas('onTweenCompleted', [tag]); - game.modchartTweens.remove(tag); + psychlua.ScriptHandler.callOnLuas('onTweenCompleted', [tag]); + psychlua.ScriptHandler.modchartTweens.remove(tag); } })); } @@ -654,14 +751,43 @@ class FunkinLua { var testicle:StrumNote = game.strumLineNotes.members[note % game.strumLineNotes.length]; if(testicle != null) { - game.modchartTweens.set(tag, FlxTween.tween(testicle, {direction: value}, duration, {ease: LuaUtils.getTweenEaseByString(ease), + psychlua.ScriptHandler.modchartTweens.set(tag, FlxTween.tween(testicle, {direction: value}, duration, {ease: LuaUtils.getTweenEaseByString(ease), + onComplete: function(twn:FlxTween) { + psychlua.ScriptHandler.callOnLuas('onTweenCompleted', [tag]); + psychlua.ScriptHandler.modchartTweens.remove(tag); + } + })); + } + }); + Lua_helper.add_callback(lua, "noteTweenAngle", function(tag:String, note:Int, value:Dynamic, duration:Float, ease:String) { + LuaUtils.cancelTween(tag); + if(note < 0) note = 0; + var testicle:StrumNote = game.strumLineNotes.members[note % game.strumLineNotes.length]; + + if(testicle != null) { + psychlua.ScriptHandler.modchartTweens.set(tag, FlxTween.tween(testicle, {angle: value}, duration, {ease: LuaUtils.getTweenEaseByString(ease), onComplete: function(twn:FlxTween) { - game.callOnLuas('onTweenCompleted', [tag]); - game.modchartTweens.remove(tag); + psychlua.ScriptHandler.callOnLuas('onTweenCompleted', [tag]); + psychlua.ScriptHandler.modchartTweens.remove(tag); } })); } }); + Lua_helper.add_callback(lua, "noteTweenAlpha", function(tag:String, note:Int, value:Dynamic, duration:Float, ease:String) { + LuaUtils.cancelTween(tag); + if(note < 0) note = 0; + var testicle:StrumNote = game.strumLineNotes.members[note % game.strumLineNotes.length]; + + if(testicle != null) { + psychlua.ScriptHandler.modchartTweens.set(tag, FlxTween.tween(testicle, {alpha: value}, duration, {ease: LuaUtils.getTweenEaseByString(ease), + onComplete: function(twn:FlxTween) { + psychlua.ScriptHandler.callOnLuas('onTweenCompleted', [tag]); + psychlua.ScriptHandler.modchartTweens.remove(tag); + } + })); + } + }); + } //file functions! (from dragshot's psych engine xt) Lua_helper.add_callback(lua, "folderExists", function(targetFolder:String = null, modOnly:Bool = false) { @@ -786,46 +912,14 @@ class FunkinLua { } return released; }); - Lua_helper.add_callback(lua, "noteTweenAngle", function(tag:String, note:Int, value:Dynamic, duration:Float, ease:String) { - LuaUtils.cancelTween(tag); - if(note < 0) note = 0; - var testicle:StrumNote = game.strumLineNotes.members[note % game.strumLineNotes.length]; - - if(testicle != null) { - game.modchartTweens.set(tag, FlxTween.tween(testicle, {angle: value}, duration, {ease: LuaUtils.getTweenEaseByString(ease), - onComplete: function(twn:FlxTween) { - game.callOnLuas('onTweenCompleted', [tag]); - game.modchartTweens.remove(tag); - } - })); - } - }); - Lua_helper.add_callback(lua, "noteTweenAlpha", function(tag:String, note:Int, value:Dynamic, duration:Float, ease:String) { - LuaUtils.cancelTween(tag); - if(note < 0) note = 0; - var testicle:StrumNote = game.strumLineNotes.members[note % game.strumLineNotes.length]; - - if(testicle != null) { - game.modchartTweens.set(tag, FlxTween.tween(testicle, {alpha: value}, duration, {ease: LuaUtils.getTweenEaseByString(ease), - onComplete: function(twn:FlxTween) { - game.callOnLuas('onTweenCompleted', [tag]); - game.modchartTweens.remove(tag); - } - })); - } - }); - - Lua_helper.add_callback(lua, "cancelTween", function(tag:String) { - LuaUtils.cancelTween(tag); - }); Lua_helper.add_callback(lua, "runTimer", function(tag:String, time:Float = 1, loops:Int = 1) { LuaUtils.cancelTimer(tag); - game.modchartTimers.set(tag, new FlxTimer().start(time, function(tmr:FlxTimer) { + psychlua.ScriptHandler.modchartTimers.set(tag, new FlxTimer().start(time, function(tmr:FlxTimer) { if(tmr.finished) { - game.modchartTimers.remove(tag); + psychlua.ScriptHandler.modchartTimers.remove(tag); } - game.callOnLuas('onTimerCompleted', [tag, tmr.loops, tmr.loopsLeft]); + psychlua.ScriptHandler.callOnLuas('onTimerCompleted', [tag, tmr.loops, tmr.loopsLeft]); //trace('Timer Completed: ' + tag); }, loops)); }); @@ -884,6 +978,18 @@ class FunkinLua { Lua_helper.add_callback(lua, "getColorFromString", function(color:String) return FlxColor.fromString(color)); Lua_helper.add_callback(lua, "getColorFromHex", function(color:String) return FlxColor.fromString('#$color')); + // The other color functions + Lua_helper.add_callback(lua, "getColorFromInt", function(color:Int) return FlxColor.fromInt(color)); + Lua_helper.add_callback(lua, "getColorFromRGB", function(r:Int, g:Int, b:Int, a:Int = 255) return FlxColor.fromRGB(r,g,b,a)); + Lua_helper.add_callback(lua, "getColorFromRGBFloat", function(r:Float, g:Float, b:Float, a:Float = 1) return FlxColor.fromRGBFloat(r,g,b,a)); + Lua_helper.add_callback(lua, "getColorFromCMYK", function(c:Float, m:Float, y:Float, b:Float, a:Float = 1) return FlxColor.fromCMYK(c,m,y,b,a)); + Lua_helper.add_callback(lua, "getColorFromHSB", function(h:Float, s:Float, b:Float, a:Float = 1) return FlxColor.fromHSB(h,s,b,a)); + Lua_helper.add_callback(lua, "getColorFromHSL", function(h:Float, s:Float, l:Float, a:Float = 1) return FlxColor.fromHSL(h,s,l,a)); + + Lua_helper.add_callback(lua, "interpolateColor", function(color1:String, color2:String, factor:Float = 0.5) + return FlxColor.interpolate(CoolUtil.colorFromString(color1), CoolUtil.colorFromString(color2), factor) + ); + // precaching Lua_helper.add_callback(lua, "addCharacterToList", function(name:String, type:String) { var charType:Int = 0; @@ -960,6 +1066,10 @@ class FunkinLua { Lua_helper.add_callback(lua, "getSongPosition", function() { return Conductor.songPosition; }); + Lua_helper.add_callback(lua, "setSongPosition", function(pos:Float) { + Conductor.songPosition = pos; + return pos; + }); Lua_helper.add_callback(lua, "getCharacterX", function(type:String) { switch(type.toLowerCase()) { @@ -1009,16 +1119,6 @@ class FunkinLua { game.moveCamera(isDad); return isDad; }); - Lua_helper.add_callback(lua, "cameraShake", function(camera:String, intensity:Float, duration:Float) { - LuaUtils.cameraFromString(camera).shake(intensity, duration); - }); - - Lua_helper.add_callback(lua, "cameraFlash", function(camera:String, color:String, duration:Float,forced:Bool) { - LuaUtils.cameraFromString(camera).flash(CoolUtil.colorFromString(color), duration, null,forced); - }); - Lua_helper.add_callback(lua, "cameraFade", function(camera:String, color:String, duration:Float,forced:Bool) { - LuaUtils.cameraFromString(camera).fade(CoolUtil.colorFromString(color), duration, false,null,forced); - }); Lua_helper.add_callback(lua, "setRatingPercent", function(value:Float) { game.ratingPercent = value; }); @@ -1107,22 +1207,22 @@ class FunkinLua { Lua_helper.add_callback(lua, "makeLuaSprite", function(tag:String, ?image:String = null, ?x:Float = 0, ?y:Float = 0) { tag = tag.replace('.', ''); - LuaUtils.resetSpriteTag(tag); + LuaUtils.resetTag(tag, psychlua.ScriptHandler.modchartSprites); var leSprite:ModchartSprite = new ModchartSprite(x, y); if(image != null && image.length > 0) { leSprite.loadGraphic(Paths.image(image)); } - game.modchartSprites.set(tag, leSprite); + psychlua.ScriptHandler.modchartSprites.set(tag, leSprite); leSprite.active = true; }); Lua_helper.add_callback(lua, "makeAnimatedLuaSprite", function(tag:String, ?image:String = null, ?x:Float = 0, ?y:Float = 0, ?spriteType:String = "sparrow") { tag = tag.replace('.', ''); - LuaUtils.resetSpriteTag(tag); + LuaUtils.resetTag(tag, psychlua.ScriptHandler.modchartSprites); var leSprite:ModchartSprite = new ModchartSprite(x, y); LuaUtils.loadFrames(leSprite, image, spriteType); - game.modchartSprites.set(tag, leSprite); + psychlua.ScriptHandler.modchartSprites.set(tag, leSprite); }); Lua_helper.add_callback(lua, "makeGraphic", function(obj:String, width:Int = 256, height:Int = 256, color:String = 'FFFFFF') { @@ -1144,6 +1244,18 @@ class FunkinLua { return false; }); + Lua_helper.add_callback(lua, "findAnimationByPrefix", function(obj:String, prefix:String, log:Bool = true):Int { + var animFrames:Array = []; + + var obj:Dynamic = LuaUtils.getObjectDirectly(obj, false); + if(obj != null && obj.animation != null) @:privateAccess obj.animation.findByPrefix(animFrames, prefix, log); + + var i:Int = 0; + for(frame in animFrames) i++; + + return i; + }); + Lua_helper.add_callback(lua, "addAnimation", function(obj:String, name:String, frames:Array, framerate:Int = 24, loop:Bool = true) { var obj:Dynamic = LuaUtils.getObjectDirectly(obj, false); if(obj != null && obj.animation != null) @@ -1188,8 +1300,8 @@ class FunkinLua { }); Lua_helper.add_callback(lua, "setScrollFactor", function(obj:String, scrollX:Float, scrollY:Float) { - if(game.getLuaObject(obj,false)!=null) { - game.getLuaObject(obj,false).scrollFactor.set(scrollX, scrollY); + if(psychlua.ScriptHandler.getLuaObject(obj,false)!=null) { + psychlua.ScriptHandler.getLuaObject(obj,false).scrollFactor.set(scrollX, scrollY); return; } @@ -1200,25 +1312,28 @@ class FunkinLua { }); Lua_helper.add_callback(lua, "addLuaSprite", function(tag:String, front:Bool = false) { var mySprite:FlxSprite = null; - if(game.modchartSprites.exists(tag)) mySprite = game.modchartSprites.get(tag); - else if(game.variables.exists(tag)) mySprite = game.variables.get(tag); + if(psychlua.ScriptHandler.modchartSprites.exists(tag)) mySprite = psychlua.ScriptHandler.modchartSprites.get(tag); + else if(psychlua.ScriptHandler.variables.exists(tag)) mySprite = psychlua.ScriptHandler.variables.get(tag); if(mySprite == null) return false; - if(front) + if(front || LuaUtils.getTargetInstance().members.length == 0) LuaUtils.getTargetInstance().add(mySprite); else { - if(!game.isDead) - game.insert(game.members.indexOf(LuaUtils.getLowestCharacterGroup()), mySprite); + if(game == PlayState.instance) + if(!game.isDead) + game.insert(game.members.indexOf(LuaUtils.getLowestCharacterGroup()), mySprite); + else + GameOverSubstate.instance.insert(GameOverSubstate.instance.members.indexOf(GameOverSubstate.instance.boyfriend), mySprite); else - GameOverSubstate.instance.insert(GameOverSubstate.instance.members.indexOf(GameOverSubstate.instance.boyfriend), mySprite); + LuaUtils.getTargetInstance().insert(LuaUtils.getTargetInstance().members.length, mySprite); } return true; }); Lua_helper.add_callback(lua, "setGraphicSize", function(obj:String, x:Int, y:Int = 0, updateHitbox:Bool = true) { - if(game.getLuaObject(obj)!=null) { - var shit:FlxSprite = game.getLuaObject(obj); + if(psychlua.ScriptHandler.getLuaObject(obj)!=null) { + var shit:FlxSprite = psychlua.ScriptHandler.getLuaObject(obj); shit.setGraphicSize(x, y); if(updateHitbox) shit.updateHitbox(); return; @@ -1238,8 +1353,8 @@ class FunkinLua { luaTrace('setGraphicSize: Couldnt find object: ' + obj, false, false, FlxColor.RED); }); Lua_helper.add_callback(lua, "scaleObject", function(obj:String, x:Float, y:Float, updateHitbox:Bool = true) { - if(game.getLuaObject(obj)!=null) { - var shit:FlxSprite = game.getLuaObject(obj); + if(psychlua.ScriptHandler.getLuaObject(obj)!=null) { + var shit:FlxSprite = psychlua.ScriptHandler.getLuaObject(obj); shit.scale.set(x, y); if(updateHitbox) shit.updateHitbox(); return; @@ -1259,8 +1374,8 @@ class FunkinLua { luaTrace('scaleObject: Couldnt find object: ' + obj, false, false, FlxColor.RED); }); Lua_helper.add_callback(lua, "updateHitbox", function(obj:String) { - if(game.getLuaObject(obj)!=null) { - var shit:FlxSprite = game.getLuaObject(obj); + if(psychlua.ScriptHandler.getLuaObject(obj)!=null) { + var shit:FlxSprite = psychlua.ScriptHandler.getLuaObject(obj); shit.updateHitbox(); return; } @@ -1281,11 +1396,11 @@ class FunkinLua { }); Lua_helper.add_callback(lua, "removeLuaSprite", function(tag:String, destroy:Bool = true) { - if(!game.modchartSprites.exists(tag)) { + if(!psychlua.ScriptHandler.modchartSprites.exists(tag)) { return; } - var pee:ModchartSprite = game.modchartSprites.get(tag); + var pee:ModchartSprite = psychlua.ScriptHandler.modchartSprites.get(tag); if(destroy) { pee.kill(); } @@ -1293,18 +1408,87 @@ class FunkinLua { LuaUtils.getTargetInstance().remove(pee, true); if(destroy) { pee.destroy(); - game.modchartSprites.remove(tag); + psychlua.ScriptHandler.modchartSprites.remove(tag); + } + }); + + Lua_helper.add_callback(lua, "makeLuaGroup", function(tag:String, max:Int = 0) { + tag = tag.replace('.', ''); + LuaUtils.resetTag(tag, ScriptHandler.modchartGroups); + var leGroup:FlxGroup = new FlxGroup(max); + ScriptHandler.modchartGroups.set(tag, leGroup); + leGroup.active = true; + }); + Lua_helper.add_callback(lua, "insertToGroup", function(arr:String, obj:String, index:Int = -1) { + var groupOrArray:Dynamic = ScriptHandler.modchartGroups.get(arr) != null ? ScriptHandler.modchartGroups.get(arr) : Reflect.getProperty(LuaUtils.getTargetInstance(), arr); + var realObject:Dynamic = ScriptHandler.getLuaObject(obj) != null ? ScriptHandler.getLuaObject(obj) : Reflect.getProperty(LuaUtils.getTargetInstance(), obj); + + if(Std.isOfType(groupOrArray, FlxTypedGroup) && realObject != null) { + if(index < 0) groupOrArray.add(realObject); + else groupOrArray.insert(index, realObject); + } + }); + Lua_helper.add_callback(lua, "addLuaGroup", function(tag:String, front:Bool = false) { + var myGroup:FlxGroup = ScriptHandler.modchartGroups.get(tag); + if(myGroup == null) return false; + + if(front || LuaUtils.getTargetInstance().members.length == 0) + LuaUtils.getTargetInstance().add(myGroup); + else + { + if(FlxG.state == PlayState.instance) + if(PlayState.instance.isDead) + GameOverSubstate.instance.insert(GameOverSubstate.instance.members.indexOf(GameOverSubstate.instance.boyfriend), myGroup); + else + PlayState.instance.insert(PlayState.instance.members.indexOf(LuaUtils.getLowestCharacterGroup()), myGroup); + else + LuaUtils.getTargetInstance().insert(LuaUtils.getTargetInstance().members.length, myGroup); + } + return true; + }); + Lua_helper.add_callback(lua, "removeLuaGroup", function(tag:String, destroy:Bool = true) { + if(!ScriptHandler.modchartGroups.exists(tag)) { + return; + } + + var pee:FlxGroup = ScriptHandler.modchartGroups.get(tag); + if(destroy) { + pee.kill(); + } + + LuaUtils.getTargetInstance().remove(pee, true); + if(destroy) { + pee.destroy(); + ScriptHandler.modchartGroups.remove(tag); } }); Lua_helper.add_callback(lua, "luaSpriteExists", function(tag:String) { - return game.modchartSprites.exists(tag); + return psychlua.ScriptHandler.modchartSprites.exists(tag); }); Lua_helper.add_callback(lua, "luaTextExists", function(tag:String) { - return game.modchartTexts.exists(tag); + return psychlua.ScriptHandler.modchartTexts.exists(tag); }); Lua_helper.add_callback(lua, "luaSoundExists", function(tag:String) { - return game.modchartSounds.exists(tag); + return psychlua.ScriptHandler.modchartSounds.exists(tag); + }); + Lua_helper.add_callback(lua, "luaTweenExists", function(tag:String) { + return psychlua.ScriptHandler.modchartTweens.exists(tag); + }); + Lua_helper.add_callback(lua, "luaTimerExists", function(tag:String) { + return psychlua.ScriptHandler.modchartTimers.exists(tag); + }); + Lua_helper.add_callback(lua, "luaSaveExists", function(tag:String) { + return psychlua.ScriptHandler.modchartSaves.exists(tag); + }); + Lua_helper.add_callback(lua, "luaCameraExists", function(tag:String) { + return psychlua.ScriptHandler.modchartCameras.exists(tag); + }); + Lua_helper.add_callback(lua, "luaVideoExists", function(tag:String) { + return psychlua.ScriptHandler.modchartVideos.exists(tag); + }); + Lua_helper.add_callback(lua, "luaGroupExists", function(tag:String) { + return ScriptHandler.modchartGroups.exists(tag); }); Lua_helper.add_callback(lua, "setHealthBarColors", function(left:String, right:String) { @@ -1327,7 +1511,7 @@ class FunkinLua { }); Lua_helper.add_callback(lua, "setObjectCamera", function(obj:String, camera:String = '') { - var real = game.getLuaObject(obj); + var real = psychlua.ScriptHandler.getLuaObject(obj); if(real!=null){ real.cameras = [LuaUtils.cameraFromString(camera)]; return true; @@ -1347,7 +1531,7 @@ class FunkinLua { return false; }); Lua_helper.add_callback(lua, "setBlendMode", function(obj:String, blend:String = '') { - var real = game.getLuaObject(obj); + var real = psychlua.ScriptHandler.getLuaObject(obj); if(real != null) { real.blend = LuaUtils.blendModeFromString(blend); return true; @@ -1367,7 +1551,7 @@ class FunkinLua { return false; }); Lua_helper.add_callback(lua, "screenCenter", function(obj:String, pos:String = 'xy') { - var spr:FlxSprite = game.getLuaObject(obj); + var spr:FlxSprite = psychlua.ScriptHandler.getLuaObject(obj); if(spr==null){ var split:Array = obj.split('.'); @@ -1399,7 +1583,7 @@ class FunkinLua { var objectsArray:Array = []; for (i in 0...namesArray.length) { - var real = game.getLuaObject(namesArray[i]); + var real = psychlua.ScriptHandler.getLuaObject(namesArray[i]); if(real!=null) { objectsArray.push(real); } else { @@ -1492,46 +1676,46 @@ class FunkinLua { Lua_helper.add_callback(lua, "playSound", function(sound:String, volume:Float = 1, ?tag:String = null) { if(tag != null && tag.length > 0) { tag = tag.replace('.', ''); - if(game.modchartSounds.exists(tag)) { - game.modchartSounds.get(tag).stop(); + if(psychlua.ScriptHandler.modchartSounds.exists(tag)) { + psychlua.ScriptHandler.modchartSounds.get(tag).stop(); } - game.modchartSounds.set(tag, FlxG.sound.play(Paths.sound(sound), volume, false, null, true, function() { - game.modchartSounds.remove(tag); - game.callOnLuas('onSoundFinished', [tag]); + psychlua.ScriptHandler.modchartSounds.set(tag, FlxG.sound.play(Paths.sound(sound), volume, false, null, true, function() { + psychlua.ScriptHandler.modchartSounds.remove(tag); + psychlua.ScriptHandler.callOnLuas('onSoundFinished', [tag]); })); return; } FlxG.sound.play(Paths.sound(sound), volume); }); Lua_helper.add_callback(lua, "stopSound", function(tag:String) { - if(tag != null && tag.length > 1 && game.modchartSounds.exists(tag)) { - game.modchartSounds.get(tag).stop(); - game.modchartSounds.remove(tag); + if(tag != null && tag.length > 1 && psychlua.ScriptHandler.modchartSounds.exists(tag)) { + psychlua.ScriptHandler.modchartSounds.get(tag).stop(); + psychlua.ScriptHandler.modchartSounds.remove(tag); } }); Lua_helper.add_callback(lua, "pauseSound", function(tag:String) { - if(tag != null && tag.length > 1 && game.modchartSounds.exists(tag)) { - game.modchartSounds.get(tag).pause(); + if(tag != null && tag.length > 1 && psychlua.ScriptHandler.modchartSounds.exists(tag)) { + psychlua.ScriptHandler.modchartSounds.get(tag).pause(); } }); Lua_helper.add_callback(lua, "resumeSound", function(tag:String) { - if(tag != null && tag.length > 1 && game.modchartSounds.exists(tag)) { - game.modchartSounds.get(tag).play(); + if(tag != null && tag.length > 1 && psychlua.ScriptHandler.modchartSounds.exists(tag)) { + psychlua.ScriptHandler.modchartSounds.get(tag).play(); } }); Lua_helper.add_callback(lua, "soundFadeIn", function(tag:String, duration:Float, fromValue:Float = 0, toValue:Float = 1) { if(tag == null || tag.length < 1) { FlxG.sound.music.fadeIn(duration, fromValue, toValue); - } else if(game.modchartSounds.exists(tag)) { - game.modchartSounds.get(tag).fadeIn(duration, fromValue, toValue); + } else if(psychlua.ScriptHandler.modchartSounds.exists(tag)) { + psychlua.ScriptHandler.modchartSounds.get(tag).fadeIn(duration, fromValue, toValue); } }); Lua_helper.add_callback(lua, "soundFadeOut", function(tag:String, duration:Float, toValue:Float = 0) { if(tag == null || tag.length < 1) { FlxG.sound.music.fadeOut(duration, toValue); - } else if(game.modchartSounds.exists(tag)) { - game.modchartSounds.get(tag).fadeOut(duration, toValue); + } else if(psychlua.ScriptHandler.modchartSounds.exists(tag)) { + psychlua.ScriptHandler.modchartSounds.get(tag).fadeOut(duration, toValue); } }); Lua_helper.add_callback(lua, "soundFadeCancel", function(tag:String) { @@ -1539,11 +1723,11 @@ class FunkinLua { if(FlxG.sound.music.fadeTween != null) { FlxG.sound.music.fadeTween.cancel(); } - } else if(game.modchartSounds.exists(tag)) { - var theSound:FlxSound = game.modchartSounds.get(tag); + } else if(psychlua.ScriptHandler.modchartSounds.exists(tag)) { + var theSound:FlxSound = psychlua.ScriptHandler.modchartSounds.get(tag); if(theSound.fadeTween != null) { theSound.fadeTween.cancel(); - game.modchartSounds.remove(tag); + psychlua.ScriptHandler.modchartSounds.remove(tag); } } }); @@ -1552,8 +1736,8 @@ class FunkinLua { if(FlxG.sound.music != null) { return FlxG.sound.music.volume; } - } else if(game.modchartSounds.exists(tag)) { - return game.modchartSounds.get(tag).volume; + } else if(psychlua.ScriptHandler.modchartSounds.exists(tag)) { + return psychlua.ScriptHandler.modchartSounds.get(tag).volume; } return 0; }); @@ -1562,19 +1746,19 @@ class FunkinLua { if(FlxG.sound.music != null) { FlxG.sound.music.volume = value; } - } else if(game.modchartSounds.exists(tag)) { - game.modchartSounds.get(tag).volume = value; + } else if(psychlua.ScriptHandler.modchartSounds.exists(tag)) { + psychlua.ScriptHandler.modchartSounds.get(tag).volume = value; } }); Lua_helper.add_callback(lua, "getSoundTime", function(tag:String) { - if(tag != null && tag.length > 0 && game.modchartSounds.exists(tag)) { - return game.modchartSounds.get(tag).time; + if(tag != null && tag.length > 0 && psychlua.ScriptHandler.modchartSounds.exists(tag)) { + return psychlua.ScriptHandler.modchartSounds.get(tag).time; } return 0; }); Lua_helper.add_callback(lua, "setSoundTime", function(tag:String, value:Float) { - if(tag != null && tag.length > 0 && game.modchartSounds.exists(tag)) { - var theSound:FlxSound = game.modchartSounds.get(tag); + if(tag != null && tag.length > 0 && psychlua.ScriptHandler.modchartSounds.exists(tag)) { + var theSound:FlxSound = psychlua.ScriptHandler.modchartSounds.get(tag); if(theSound != null) { var wasResumed:Bool = theSound.playing; theSound.pause(); @@ -1584,14 +1768,14 @@ class FunkinLua { } }); Lua_helper.add_callback(lua, "getSoundPitch", function(tag:String) { - if(tag != null && tag.length > 0 && game.modchartSounds.exists(tag)) { - return game.modchartSounds.get(tag).pitch; + if(tag != null && tag.length > 0 && psychlua.ScriptHandler.modchartSounds.exists(tag)) { + return psychlua.ScriptHandler.modchartSounds.get(tag).pitch; } return 0; }); Lua_helper.add_callback(lua, "setSoundPitch", function(tag:String, value:Float, doPause:Bool = false) { - if(tag != null && tag.length > 0 && game.modchartSounds.exists(tag)) { - var theSound:FlxSound = game.modchartSounds.get(tag); + if(tag != null && tag.length > 0 && psychlua.ScriptHandler.modchartSounds.exists(tag)) { + var theSound:FlxSound = psychlua.ScriptHandler.modchartSounds.get(tag); if(theSound != null) { var wasResumed:Bool = theSound.playing; if (doPause) theSound.pause(); @@ -1616,7 +1800,7 @@ class FunkinLua { #end // - Lua_helper.add_callback(lua, "debugPrint", function(text:Dynamic = '', color:String = 'WHITE') PlayState.instance.addTextToDebug(text, CoolUtil.colorFromString(color))); + Lua_helper.add_callback(lua, "debugPrint", function(text:Dynamic = '', color:String = 'WHITE') psychlua.ScriptHandler.addTextToDebug(text, CoolUtil.colorFromString(color))); addLocalCallback("close", function() { closed = true; @@ -1627,10 +1811,13 @@ class FunkinLua { #if desktop DiscordClient.addLuaCallbacks(lua); #end #if SScript HScript.implement(this); #end #if ACHIEVEMENTS_ALLOWED Achievements.addLuaCallbacks(lua); #end + #if VIDEOS_ALLOWED VideoFunctions.implement(this); #end ReflectionFunctions.implement(this); TextFunctions.implement(this); + CameraFunctions.implement(this); ExtraFunctions.implement(this); #if flxanimate FlxAnimateFunctions.implement(this); #end + CustomState.implement(this); CustomSubstate.implement(this); ShaderFunctions.implement(this); DeprecatedFunctions.implement(this); @@ -1726,7 +1913,7 @@ class FunkinLua { public function stop() { #if LUA_ALLOWED - PlayState.instance.luaArray.remove(this); + psychlua.ScriptHandler.luaArray.remove(this); closed = true; if(lua == null) { @@ -1785,10 +1972,10 @@ class FunkinLua { #if LUA_ALLOWED var target:Dynamic = LuaUtils.tweenPrepare(tag, vars); if(target != null) { - PlayState.instance.modchartTweens.set(tag, FlxTween.tween(target, tweenValue, duration, {ease: LuaUtils.getTweenEaseByString(ease), + psychlua.ScriptHandler.modchartTweens.set(tag, FlxTween.tween(target, tweenValue, duration, {ease: LuaUtils.getTweenEaseByString(ease), onComplete: function(twn:FlxTween) { - PlayState.instance.modchartTweens.remove(tag); - PlayState.instance.callOnLuas('onTweenCompleted', [tag, vars]); + psychlua.ScriptHandler.modchartTweens.remove(tag); + psychlua.ScriptHandler.callOnLuas('onTweenCompleted', [tag, vars]); } })); } else { @@ -1803,7 +1990,7 @@ class FunkinLua { if(deprecated && !getBool('luaDeprecatedWarnings')) { return; } - PlayState.instance.addTextToDebug(text, color); + psychlua.ScriptHandler.addTextToDebug(text, color); trace(text); } #end diff --git a/source/psychlua/HScript.hx b/source/psychlua/HScript.hx index e299dbf..8254cc4 100644 --- a/source/psychlua/HScript.hx +++ b/source/psychlua/HScript.hx @@ -6,7 +6,6 @@ import psychlua.FunkinLua; import psychlua.CustomSubstate; #if HSCRIPT_ALLOWED -import tea.SScript; class HScript extends SScript { public var parentLua:FunkinLua; @@ -35,7 +34,7 @@ class HScript extends SScript @:privateAccess if(hs.parsingException != null) { - PlayState.instance.addTextToDebug('ERROR ON LOADING (${hs.origin}): ${hs.parsingException.message}', FlxColor.RED); + psychlua.ScriptHandler.addTextToDebug('ERROR ON LOADING (${hs.origin}): ${hs.parsingException.message}', FlxColor.RED); } } } @@ -104,6 +103,7 @@ class HScript extends SScript set('Conductor', Conductor); set('ClientPrefs', ClientPrefs); set('CoolUtil', CoolUtil); + set('psychlua.ScriptHandler', psychlua.ScriptHandler); set('Socket', backend.Socket); set('GitCommitMacro', macro.GitCommitMacro); #if ACHIEVEMENTS_ALLOWED @@ -113,6 +113,7 @@ class HScript extends SScript set('Alphabet', Alphabet); set('Note', objects.Note); set('CustomSubstate', CustomSubstate); + set('CustomState', CustomState); #if (!flash && sys) set('FlxRuntimeShader', flixel.addons.display.FlxRuntimeShader); #end @@ -124,34 +125,43 @@ class HScript extends SScript // Functions & Variables set('setVar', function(name:String, value:Dynamic) { - PlayState.instance.variables.set(name, value); + psychlua.ScriptHandler.variables.set(name, value); return value; }); set('getVar', function(name:String) { var result:Dynamic = null; - if(PlayState.instance.variables.exists(name)) result = PlayState.instance.variables.get(name); + if(psychlua.ScriptHandler.variables.exists(name)) result = psychlua.ScriptHandler.variables.get(name); return result; }); set('removeVar', function(name:String) { - if(PlayState.instance.variables.exists(name)) - { - PlayState.instance.variables.remove(name); - return true; - } - return false; + return psychlua.ScriptHandler.variables.remove(name); + }); + set('setGlobalVar', function(name:String, value:Dynamic, replace:Bool) { + if(psychlua.ScriptHandler.globalVariables.get(name) == null || psychlua.ScriptHandler.globalVariables.get(name) != null && replace) + psychlua.ScriptHandler.globalVariables.set(name, value); + return value; + }); + set('getGlobalVar', function(name:String) { + var result:Dynamic = null; + if(psychlua.ScriptHandler.globalVariables.exists(name)) result = psychlua.ScriptHandler.globalVariables.get(name); + return result; + }); + set('removeGlobalVar', function(name:String) + { + return psychlua.ScriptHandler.globalVariables.remove(name); }); set('debugPrint', function(text:String, ?color:FlxColor = null) { if(color == null) color = FlxColor.WHITE; - PlayState.instance.addTextToDebug(text, color); + psychlua.ScriptHandler.addTextToDebug(text, color); }); set('getModSetting', function(saveTag:String, ?modName:String = null) { if(modName == null) { if(this.modFolder == null) { - PlayState.instance.addTextToDebug('getModSetting: Argument #2 is null and script is not inside a packed Mod folder!', FlxColor.RED); + psychlua.ScriptHandler.addTextToDebug('getModSetting: Argument #2 is null and script is not inside a packed Mod folder!', FlxColor.RED); return null; } modName = this.modFolder; @@ -207,33 +217,33 @@ class HScript extends SScript set('keyJustPressed', function(name:String = '') { name = name.toLowerCase(); switch(name) { - case 'left': return PlayState.instance.controls.NOTE_LEFT_P; - case 'down': return PlayState.instance.controls.NOTE_DOWN_P; - case 'up': return PlayState.instance.controls.NOTE_UP_P; - case 'right': return PlayState.instance.controls.NOTE_RIGHT_P; - default: return PlayState.instance.controls.justPressed(name); + case 'left': return Controls.instance.NOTE_LEFT_P; + case 'down': return Controls.instance.NOTE_DOWN_P; + case 'up': return Controls.instance.NOTE_UP_P; + case 'right': return Controls.instance.NOTE_RIGHT_P; + default: return Controls.instance.justPressed(name); } return false; }); set('keyPressed', function(name:String = '') { name = name.toLowerCase(); switch(name) { - case 'left': return PlayState.instance.controls.NOTE_LEFT; - case 'down': return PlayState.instance.controls.NOTE_DOWN; - case 'up': return PlayState.instance.controls.NOTE_UP; - case 'right': return PlayState.instance.controls.NOTE_RIGHT; - default: return PlayState.instance.controls.pressed(name); + case 'left': return Controls.instance.NOTE_LEFT; + case 'down': return Controls.instance.NOTE_DOWN; + case 'up': return Controls.instance.NOTE_UP; + case 'right': return Controls.instance.NOTE_RIGHT; + default: return Controls.instance.pressed(name); } return false; }); set('keyReleased', function(name:String = '') { name = name.toLowerCase(); switch(name) { - case 'left': return PlayState.instance.controls.NOTE_LEFT_R; - case 'down': return PlayState.instance.controls.NOTE_DOWN_R; - case 'up': return PlayState.instance.controls.NOTE_UP_R; - case 'right': return PlayState.instance.controls.NOTE_RIGHT_R; - default: return PlayState.instance.controls.justReleased(name); + case 'left': return Controls.instance.NOTE_LEFT_R; + case 'down': return Controls.instance.NOTE_DOWN_R; + case 'up': return Controls.instance.NOTE_UP_R; + case 'right': return Controls.instance.NOTE_RIGHT_R; + default: return Controls.instance.justReleased(name); } return false; }); @@ -244,7 +254,7 @@ class HScript extends SScript set('createGlobalCallback', function(name:String, func:Dynamic) { #if LUA_ALLOWED - for (script in PlayState.instance.luaArray) + for (script in psychlua.ScriptHandler.luaArray) if(script != null && script.lua != null && !script.closed) Lua_helper.add_callback(script.lua, name, func); #end @@ -288,6 +298,7 @@ class HScript extends SScript set('buildTarget', FunkinLua.getBuildTarget()); set('customSubstate', CustomSubstate.instance); set('customSubstateName', CustomSubstate.name); + set('gameName', Type.getClassName(Type.getClass(FlxG.state))); set('Function_Stop', FunkinLua.Function_Stop); set('Function_Continue', FunkinLua.Function_Continue); @@ -314,7 +325,7 @@ class HScript extends SScript } } - public function executeCode(?funcToRun:String = null, ?funcArgs:Array = null):TeaCall + public function executeCode(?funcToRun:String = null, ?funcArgs:Array = null):Tea { if(funcToRun == null) return null; @@ -339,7 +350,7 @@ class HScript extends SScript return callValue; } - public function executeFunction(funcToRun:String = null, funcArgs:Array):TeaCall + public function executeFunction(funcToRun:String = null, funcArgs:Array):Tea { if (funcToRun == null) return null; @@ -353,7 +364,7 @@ class HScript extends SScript funk.addLocalCallback("runHaxeCode", function(codeToRun:String, ?varsToBring:Any = null, ?funcToRun:String = null, ?funcArgs:Array = null):Dynamic { #if SScript initHaxeModuleCode(funk, codeToRun, varsToBring); - var retVal:TeaCall = funk.hscript.executeCode(funcToRun, funcArgs); + var retVal:Tea = funk.hscript.executeCode(funcToRun, funcArgs); if (retVal != null) { if(retVal.succeeded) diff --git a/source/psychlua/LuaUtils.hx b/source/psychlua/LuaUtils.hx index fa0f5a5..f73e0ef 100644 --- a/source/psychlua/LuaUtils.hx +++ b/source/psychlua/LuaUtils.hx @@ -40,9 +40,9 @@ class LuaUtils if(splitProps.length > 1) { var target:Dynamic = null; - if(PlayState.instance.variables.exists(splitProps[0])) + if(psychlua.ScriptHandler.variables.exists(splitProps[0])) { - var retVal:Dynamic = PlayState.instance.variables.get(splitProps[0]); + var retVal:Dynamic = psychlua.ScriptHandler.variables.get(splitProps[0]); if(retVal != null) target = retVal; } @@ -66,9 +66,9 @@ class LuaUtils return value; } - if(PlayState.instance.variables.exists(variable)) + if(psychlua.ScriptHandler.variables.exists(variable)) { - PlayState.instance.variables.set(variable, value); + psychlua.ScriptHandler.variables.set(variable, value); return value; } Reflect.setProperty(instance, variable, value); @@ -80,9 +80,9 @@ class LuaUtils if(splitProps.length > 1) { var target:Dynamic = null; - if(PlayState.instance.variables.exists(splitProps[0])) + if(psychlua.ScriptHandler.variables.exists(splitProps[0])) { - var retVal:Dynamic = PlayState.instance.variables.get(splitProps[0]); + var retVal:Dynamic = psychlua.ScriptHandler.variables.get(splitProps[0]); if(retVal != null) target = retVal; } @@ -103,9 +103,9 @@ class LuaUtils return instance.get(variable); } - if(PlayState.instance.variables.exists(variable)) + if(psychlua.ScriptHandler.variables.exists(variable)) { - var retVal:Dynamic = PlayState.instance.variables.get(variable); + var retVal:Dynamic = psychlua.ScriptHandler.variables.get(variable); if(retVal != null) return retVal; } @@ -165,7 +165,7 @@ class LuaUtils { FlxG.save.data.modSettings.remove(modName); #if (LUA_ALLOWED || HSCRIPT_ALLOWED) - PlayState.instance.addTextToDebug('getModSetting: $path could not be found!', FlxColor.RED); + psychlua.ScriptHandler.addTextToDebug('getModSetting: $path could not be found!', FlxColor.RED); #else FlxG.log.warn('getModSetting: $path could not be found!'); #end @@ -174,7 +174,7 @@ class LuaUtils if(settings.exists(saveTag)) return settings.get(saveTag); #if (LUA_ALLOWED || HSCRIPT_ALLOWED) - PlayState.instance.addTextToDebug('getModSetting: "$saveTag" could not be found inside $modName\'s settings!', FlxColor.RED); + psychlua.ScriptHandler.addTextToDebug('getModSetting: "$saveTag" could not be found inside $modName\'s settings!', FlxColor.RED); #else FlxG.log.warn('getModSetting: "$saveTag" could not be found inside $modName\'s settings!'); #end @@ -231,7 +231,7 @@ class LuaUtils switch(objectName) { case 'this' | 'instance' | 'game': - return PlayState.instance; + return getTargetInstance(); default: var obj:Dynamic = PlayState.instance.getLuaObject(objectName, checkForTextsToo); @@ -242,7 +242,7 @@ class LuaUtils inline public static function getTextObject(name:String):FlxText { - return #if LUA_ALLOWED PlayState.instance.modchartTexts.exists(name) ? PlayState.instance.modchartTexts.get(name) : #end Reflect.getProperty(PlayState.instance, name); + return #if LUA_ALLOWED psychlua.ScriptHandler.modchartTexts.exists(name) ? psychlua.ScriptHandler.modchartTexts.get(name) : #end Reflect.getProperty(getTargetInstance, name); } public static function isOfTypes(value:Any, types:Array) @@ -256,7 +256,10 @@ class LuaUtils public static inline function getTargetInstance() { - return PlayState.instance.isDead ? GameOverSubstate.instance : PlayState.instance; + if(FlxG.state == PlayState.instance) + return PlayState.instance.isDead ? GameOverSubstate.instance : PlayState.instance; + + return FlxG.state; } public static inline function getLowestCharacterGroup():FlxSpriteGroup @@ -326,40 +329,38 @@ class LuaUtils } } - public static function resetTextTag(tag:String) { + public static function resetTag(tag:String, map:Map) { #if LUA_ALLOWED - if(!PlayState.instance.modchartTexts.exists(tag)) { + if(!map.exists(tag)) { return; } - var target:FlxText = PlayState.instance.modchartTexts.get(tag); + var target:Dynamic = map.get(tag); target.kill(); - PlayState.instance.remove(target, true); + getTargetInstance().remove(target, true); target.destroy(); - PlayState.instance.modchartTexts.remove(tag); + map.remove(tag); #end } - public static function resetSpriteTag(tag:String) { + public static function resetVideoTag(tag:String) { #if LUA_ALLOWED - if(!PlayState.instance.modchartSprites.exists(tag)) { + if(!psychlua.ScriptHandler.modchartSprites.exists(tag)) { return; } - var target:ModchartSprite = PlayState.instance.modchartSprites.get(tag); - target.kill(); - PlayState.instance.remove(target, true); - target.destroy(); - PlayState.instance.modchartSprites.remove(tag); + var pee:FlxVideoSprite = psychlua.ScriptHandler.modchartVideos.get(tag); + pee.destroy(); + psychlua.ScriptHandler.modchartVideos.remove(tag); #end } public static function cancelTween(tag:String) { #if LUA_ALLOWED - if(PlayState.instance.modchartTweens.exists(tag)) { - PlayState.instance.modchartTweens.get(tag).cancel(); - PlayState.instance.modchartTweens.get(tag).destroy(); - PlayState.instance.modchartTweens.remove(tag); + if(psychlua.ScriptHandler.modchartTweens.exists(tag)) { + psychlua.ScriptHandler.modchartTweens.get(tag).cancel(); + psychlua.ScriptHandler.modchartTweens.get(tag).destroy(); + psychlua.ScriptHandler.modchartTweens.remove(tag); } #end } @@ -374,11 +375,11 @@ class LuaUtils public static function cancelTimer(tag:String) { #if LUA_ALLOWED - if(PlayState.instance.modchartTimers.exists(tag)) { - var theTimer:FlxTimer = PlayState.instance.modchartTimers.get(tag); + if(psychlua.ScriptHandler.modchartTimers.exists(tag)) { + var theTimer:FlxTimer = psychlua.ScriptHandler.modchartTimers.get(tag); theTimer.cancel(); theTimer.destroy(); - PlayState.instance.modchartTimers.remove(tag); + psychlua.ScriptHandler.modchartTimers.remove(tag); } #end } @@ -472,10 +473,22 @@ class LuaUtils } public static function cameraFromString(cam:String):FlxCamera { - switch(cam.toLowerCase()) { - case 'camhud' | 'hud': return PlayState.instance.camHUD; - case 'camother' | 'other': return PlayState.instance.camOther; - } - return PlayState.instance.camGame; + #if LUA_ALLOWED + if(psychlua.ScriptHandler.modchartCameras.exists(cam)) return psychlua.ScriptHandler.modchartCameras.get(cam); + #end + + if(FlxG.state == PlayState.instance) + switch(cam.toLowerCase()) { + case 'camgame' | 'game': return PlayState.instance.camGame; + case 'camhud' | 'hud': return PlayState.instance.camHUD; + case 'camother' | 'other': return PlayState.instance.camOther; + } + + #if (LUA_ALLOWED || HSCRIPT_ALLOWED) + FunkinLua.luaTrace('cameraFromString: "$cam" could not be found! Grabbing default...'); + #else + FlxG.log.add('cameraFromString: "$cam" could not be found! Grabbing default...'); + #end + return FlxG.camera; } } \ No newline at end of file diff --git a/source/psychlua/ReflectionFunctions.hx b/source/psychlua/ReflectionFunctions.hx index 7ddf988..cb0135c 100644 --- a/source/psychlua/ReflectionFunctions.hx +++ b/source/psychlua/ReflectionFunctions.hx @@ -132,7 +132,7 @@ class ReflectionFunctions }); Lua_helper.add_callback(lua, "removeFromGroup", function(obj:String, index:Int, dontDestroy:Bool = false) { - var groupOrArray:Dynamic = Reflect.getProperty(LuaUtils.getTargetInstance(), obj); + var groupOrArray:Dynamic = ScriptHandler.modchartGroups.get(obj) != null ? ScriptHandler.modchartGroups.get(obj) : Reflect.getProperty(LuaUtils.getTargetInstance(), obj); if (Std.isOfType(groupOrArray, FlxTypedGroup)) { var sex = groupOrArray.members[index]; @@ -158,7 +158,7 @@ class ReflectionFunctions Lua_helper.add_callback(lua, "createInstance", function(variableToSave:String, className:String, ?args:Array = null) { variableToSave = variableToSave.trim().replace('.', ''); - if (!PlayState.instance.variables.exists(variableToSave)) + if (!psychlua.ScriptHandler.variables.exists(variableToSave)) { if (args == null) args = []; @@ -172,7 +172,7 @@ class ReflectionFunctions var obj:Dynamic = Type.createInstance(myType, args); if (obj != null) - PlayState.instance.variables.set(variableToSave, obj); + psychlua.ScriptHandler.variables.set(variableToSave, obj); else FunkinLua.luaTrace('createInstance: Failed to create $variableToSave, arguments are possibly wrong.', false, false, FlxColor.RED); @@ -184,9 +184,9 @@ class ReflectionFunctions }); Lua_helper.add_callback(lua, "addInstance", function(objectName:String, ?inFront:Bool = false) { - if (PlayState.instance.variables.exists(objectName)) + if (psychlua.ScriptHandler.variables.exists(objectName)) { - var obj:Dynamic = PlayState.instance.variables.get(objectName); + var obj:Dynamic = psychlua.ScriptHandler.variables.get(objectName); if (inFront) LuaUtils.getTargetInstance().add(obj); else diff --git a/source/psychlua/ScriptHandler.hx b/source/psychlua/ScriptHandler.hx new file mode 100644 index 0000000..076bf49 --- /dev/null +++ b/source/psychlua/ScriptHandler.hx @@ -0,0 +1,402 @@ +package psychlua; + +import flixel.group.FlxGroup; +import flixel.util.FlxSave; +import openfl.utils.Assets as OpenFlAssets; + +class ScriptHandler +{ + public static var variables:Map = new Map(); + public static var globalVariables:Map = new Map(); + public static var resetVariables:Array = []; + + #if LUA_ALLOWED + public static var luaArray:Array = []; + public static var modchartTweens:Map = new Map(); + public static var modchartSprites:Map = new Map(); + public static var modchartTimers:Map = new Map(); + public static var modchartSounds:Map = new Map(); + public static var modchartTexts:Map = new Map(); + public static var modchartSaves:Map = new Map(); + public static var modchartGroups:Map = new Map(); + // new shit! + public static var modchartCameras:Map = new Map(); + #if VIDEOS_ALLOWED public static var modchartVideos:Map = new Map(); #end + #end + + #if HSCRIPT_ALLOWED + public static var hscriptArray:Array = []; + public static var instancesExclude:Array = []; + #end + + public static var camDebug:FlxCamera; + public static var luaDebugGroup:FlxTypedGroup; + + public static function startScripts() { + #if (LUA_ALLOWED || HSCRIPT_ALLOWED) + if(!FlxG.signals.preGameReset.has(resetGlobalVars)) FlxG.signals.preGameReset.add(resetGlobalVars); + + camDebug = new FlxCamera(); + camDebug.bgColor.alpha = 0; + FlxG.cameras.add(camDebug, false); + FlxG.cameras.cameraAdded.add(moveDebugCam); + + luaDebugGroup = new FlxTypedGroup(); + luaDebugGroup.camera = camDebug; + FlxG.state.add(luaDebugGroup); + + #if (LUA_ALLOWED || HSCRIPT_ALLOWED) + for (folder in Mods.directoriesWithFile(Paths.getSharedPath(), 'scripts/')) + for (file in FileSystem.readDirectory(folder)) + { + #if LUA_ALLOWED + if(file.toLowerCase().endsWith('.lua')) + new FunkinLua(folder + file); + #end + + #if HSCRIPT_ALLOWED + if(file.toLowerCase().endsWith('.hx')) + initHScript(folder + file); + #end + } + #end + #end + } + + static function resetGlobalVars() { + for (tag in resetVariables) globalVariables.remove(tag); + resetVariables = []; + } + + static function moveDebugCam(cam:FlxCamera) { + if (Std.isOfType(cam, backend.PsychCamera)) + { + camDebug = new FlxCamera(); + camDebug.bgColor.alpha = 0; + luaDebugGroup.camera = camDebug; + FlxG.cameras.add(camDebug, false); + return; + } + if (cam != camDebug && camDebug != null && @:privateAccess FlxG.game._nextState == null && FlxG.cameras.list.length > 1 && camDebug != FlxG.cameras.list[FlxG.cameras.list.length-1]) + { + if(FlxG.cameras.list.contains(camDebug)) FlxG.cameras.remove(camDebug, false); + FlxG.cameras.add(camDebug, false); + } + } + + public static function addTextToDebug(text:String, color:FlxColor) { + if(luaDebugGroup == null) return; + var newText:DebugLuaText = luaDebugGroup.recycle(DebugLuaText); + newText.text = text; + newText.color = color; + newText.disableTime = 6; + newText.alpha = 1; + newText.setPosition(10, 8 - newText.height); + + luaDebugGroup.forEachAlive(function(spr:DebugLuaText) { + spr.y += newText.height + 2; + }); + luaDebugGroup.add(newText); + + Sys.println(text); + } + + public static function getLuaObject(tag:String, text:Bool = true):Dynamic { + #if LUA_ALLOWED + if(modchartSprites.exists(tag)) return modchartSprites.get(tag); + if(text && modchartTexts.exists(tag)) return modchartTexts.get(tag); + #if VIDEOS_ALLOWED + if(modchartVideos.exists(tag)) return modchartVideos.get(tag); + #end + if(modchartCameras.exists(tag)) return modchartCameras.get(tag); + if(modchartGroups.exists(tag)) return modchartGroups.get(tag); + if(variables.exists(tag)) return variables.get(tag); + #end + return null; + } + + public static function detatchFromTag(tag:String, text:Bool = true) { + #if LUA_ALLOWED + if(modchartSprites.exists(tag)) return modchartSprites.remove(tag); + if(text && modchartTexts.exists(tag)) return modchartTexts.remove(tag); + #if VIDEOS_ALLOWED + if(modchartVideos.exists(tag)) return modchartVideos.remove(tag); + #end + if(modchartCameras.exists(tag)) return modchartCameras.remove(tag); + if(modchartGroups.exists(tag)) return modchartGroups.remove(tag); + if(variables.exists(tag)) return variables.remove(tag); + #end + return false; + } + + #if LUA_ALLOWED + public static function startLuasNamed(luaFile:String) + { + #if MODS_ALLOWED + var luaToLoad:String = Paths.modFolders(luaFile); + if(!FileSystem.exists(luaToLoad)) + luaToLoad = Paths.getSharedPath(luaFile); + + if(FileSystem.exists(luaToLoad)) + #elseif sys + var luaToLoad:String = Paths.getSharedPath(luaFile); + if(OpenFlAssets.exists(luaToLoad)) + #end + { + for (script in luaArray) + if(script.scriptName == luaToLoad) return false; + + new FunkinLua(luaToLoad); + return true; + } + return false; + } + #end + + #if HSCRIPT_ALLOWED + public static function startHScriptsNamed(scriptFile:String) + { + #if MODS_ALLOWED + var scriptToLoad:String = Paths.modFolders(scriptFile); + if(!FileSystem.exists(scriptToLoad)) + scriptToLoad = Paths.getSharedPath(scriptFile); + #else + var scriptToLoad:String = Paths.getSharedPath(scriptFile); + #end + + if(FileSystem.exists(scriptToLoad)) + { + if (SScript.global.exists(scriptToLoad)) return false; + + initHScript(scriptToLoad); + return true; + } + return false; + } + + public static function initHScript(file:String) + { + try + { + var newScript:HScript = new HScript(null, file); + if(newScript.parsingException != null) + { + addTextToDebug('ERROR ON LOADING: ${newScript.parsingException.message}', FlxColor.RED); + newScript.destroy(); + return; + } + + hscriptArray.push(newScript); + if(newScript.exists('onCreate')) + { + var callValue = newScript.call('onCreate'); + if(!callValue.succeeded) + { + for (e in callValue.exceptions) + { + if (e != null) + { + var len:Int = e.message.indexOf('\n') + 1; + if(len <= 0) len = e.message.length; + addTextToDebug('ERROR ($file: onCreate) - ${e.message.substr(0, len)}', FlxColor.RED); + } + } + + newScript.destroy(); + hscriptArray.remove(newScript); + trace('failed to initialize tea interp!!! ($file)'); + } + else trace('initialized tea interp successfully: $file'); + } + + } + catch(e) + { + var len:Int = e.message.indexOf('\n') + 1; + if(len <= 0) len = e.message.length; + addTextToDebug('ERROR - ' + e.message.substr(0, len), FlxColor.RED); + var newScript:HScript = cast (SScript.global.get(file), HScript); + if(newScript != null) + { + newScript.destroy(); + hscriptArray.remove(newScript); + } + } + } + #end + + public static function callOnScripts(funcToCall:String, args:Array = null, ignoreStops = false, exclusions:Array = null, excludeValues:Array = null):Dynamic { + if(args == null) args = []; + if(exclusions == null) exclusions = []; + if(excludeValues == null) excludeValues = [FunkinLua.Function_Continue]; + + var result:Dynamic = callOnLuas(funcToCall, args, ignoreStops, exclusions, excludeValues); + if(result == null || excludeValues.contains(result)) result = callOnHScript(funcToCall, args, ignoreStops, exclusions, excludeValues); + return result; + } + + public static function callOnLuas(funcToCall:String, args:Array = null, ignoreStops = false, exclusions:Array = null, excludeValues:Array = null):Dynamic { + var returnVal:Dynamic = FunkinLua.Function_Continue; + #if LUA_ALLOWED + if(args == null) args = []; + if(exclusions == null) exclusions = []; + if(excludeValues == null) excludeValues = [FunkinLua.Function_Continue]; + + var arr:Array = []; + for (script in luaArray) + { + if(script.closed) + { + arr.push(script); + continue; + } + + if(exclusions.contains(script.scriptName)) + continue; + + var myValue:Dynamic = script.call(funcToCall, args); + if((myValue == FunkinLua.Function_StopLua || myValue == FunkinLua.Function_StopAll) && !excludeValues.contains(myValue) && !ignoreStops) + { + returnVal = myValue; + break; + } + + if(myValue != null && !excludeValues.contains(myValue)) + returnVal = myValue; + + if(script.closed) arr.push(script); + } + + if(arr.length > 0) + for (script in arr) + luaArray.remove(script); + #end + return returnVal; + } + + public static function callOnHScript(funcToCall:String, args:Array = null, ?ignoreStops:Bool = false, exclusions:Array = null, excludeValues:Array = null):Dynamic { + var returnVal:Dynamic = FunkinLua.Function_Continue; + + #if HSCRIPT_ALLOWED + if(exclusions == null) exclusions = new Array(); + if(excludeValues == null) excludeValues = new Array(); + excludeValues.push(FunkinLua.Function_Continue); + + var len:Int = hscriptArray.length; + if (len < 1) + return returnVal; + for(i in 0...len) { + var script:HScript = hscriptArray[i]; + if(script == null || !script.exists(funcToCall) || exclusions.contains(script.origin)) + continue; + + var myValue:Dynamic = null; + try { + var callValue = script.call(funcToCall, args); + if(!callValue.succeeded) + { + var e = callValue.exceptions[0]; + if(e != null) + { + var len:Int = e.message.indexOf('\n') + 1; + if(len <= 0) len = e.message.length; + addTextToDebug('ERROR (${callValue.calledFunction}) - ' + e.message.substr(0, len), FlxColor.RED); + } + } + else + { + myValue = callValue.returnValue; + if((myValue == FunkinLua.Function_StopHScript || myValue == FunkinLua.Function_StopAll) && !excludeValues.contains(myValue) && !ignoreStops) + { + returnVal = myValue; + break; + } + + if(myValue != null && !excludeValues.contains(myValue)) + returnVal = myValue; + } + } + } + #end + + return returnVal; + } + + public static function setOnScripts(variable:String, arg:Dynamic, exclusions:Array = null) { + if(exclusions == null) exclusions = []; + setOnLuas(variable, arg, exclusions); + setOnHScript(variable, arg, exclusions); + } + + public static function setOnLuas(variable:String, arg:Dynamic, exclusions:Array = null) { + #if LUA_ALLOWED + if(exclusions == null) exclusions = []; + for (script in luaArray) { + if(exclusions.contains(script.scriptName)) + continue; + + script.set(variable, arg); + } + #end + } + + public static function setOnHScript(variable:String, arg:Dynamic, exclusions:Array = null) { + #if HSCRIPT_ALLOWED + if(exclusions == null) exclusions = []; + for (script in hscriptArray) { + if(exclusions.contains(script.origin)) + continue; + + if(!instancesExclude.contains(variable)) + instancesExclude.push(variable); + script.set(variable, arg); + } + #end + } + + public static function destroyScripts() + { + #if (LUA_ALLOWED || HSCRIPT_ALLOWED) + variables.clear(); + FlxG.cameras.cameraAdded.remove(moveDebugCam); + + #if LUA_ALLOWED + #if VIDEOS_ALLOWED + // They don't get destroyed on their own + if(luaArray.length > 0) + for (tag in modchartVideos.keys()) + luaArray[0].call('removeLuaVideo', [tag]); + #end + + for (lua in luaArray) + { + lua.call('onDestroy', []); + lua.stop(); + } + + luaArray = []; + modchartTweens.clear(); + modchartSprites.clear(); + modchartTimers.clear(); + modchartSounds.clear(); + modchartTexts.clear(); + modchartSaves.clear(); + modchartGroups.clear(); + modchartCameras.clear(); + modchartVideos.clear(); + FunkinLua.customFunctions.clear(); + #end + + #if HSCRIPT_ALLOWED + for (script in hscriptArray) + if (script != null) { + script.call('onDestroy'); + script.destroy(); + } + + hscriptArray = []; + instancesExclude = []; + #end + #end + } +} \ No newline at end of file diff --git a/source/psychlua/TextFunctions.hx b/source/psychlua/TextFunctions.hx index 9863f03..b8239e1 100644 --- a/source/psychlua/TextFunctions.hx +++ b/source/psychlua/TextFunctions.hx @@ -8,13 +8,13 @@ class TextFunctions var game:PlayState = PlayState.instance; Lua_helper.add_callback(lua, "makeLuaText", function(tag:String, text:String, width:Int, x:Float, y:Float) { tag = tag.replace('.', ''); - LuaUtils.resetTextTag(tag); + LuaUtils.resetTag(tag, psychlua.ScriptHandler.modchartTexts); var leText:FlxText = new FlxText(x, y, width, text, 16); leText.setFormat(Paths.font("vcr.ttf"), 16, FlxColor.WHITE, CENTER, FlxTextBorderStyle.OUTLINE, FlxColor.BLACK); - leText.cameras = [game.camHUD]; + leText.cameras = [game != null ? game.camHUD : FlxG.camera]; leText.scrollFactor.set(); leText.borderSize = 2; - game.modchartTexts.set(tag, leText); + psychlua.ScriptHandler.modchartTexts.set(tag, leText); }); Lua_helper.add_callback(lua, "setTextString", function(tag:String, text:String) { @@ -167,17 +167,17 @@ class TextFunctions }); Lua_helper.add_callback(lua, "addLuaText", function(tag:String) { - if(game.modchartTexts.exists(tag)) { - var shit:FlxText = game.modchartTexts.get(tag); + if(psychlua.ScriptHandler.modchartTexts.exists(tag)) { + var shit:FlxText = psychlua.ScriptHandler.modchartTexts.get(tag); LuaUtils.getTargetInstance().add(shit); } }); Lua_helper.add_callback(lua, "removeLuaText", function(tag:String, destroy:Bool = true) { - if(!game.modchartTexts.exists(tag)) { + if(!psychlua.ScriptHandler.modchartTexts.exists(tag)) { return; } - var pee:FlxText = game.modchartTexts.get(tag); + var pee:FlxText = psychlua.ScriptHandler.modchartTexts.get(tag); if(destroy) { pee.kill(); } @@ -185,7 +185,7 @@ class TextFunctions LuaUtils.getTargetInstance().remove(pee, true); if(destroy) { pee.destroy(); - game.modchartTexts.remove(tag); + psychlua.ScriptHandler.modchartTexts.remove(tag); } }); } diff --git a/source/psychlua/VideoFunctions.hx b/source/psychlua/VideoFunctions.hx new file mode 100644 index 0000000..e028dec --- /dev/null +++ b/source/psychlua/VideoFunctions.hx @@ -0,0 +1,118 @@ +package psychlua; + +import substates.GameOverSubstate; + +class VideoFunctions +{ + public static function implement(funk:FunkinLua) + { + var lua = funk.lua; + Lua_helper.add_callback(lua, "makeLuaVideo", function(tag:String, x:Float = 0, y:Float = 0) { + tag = tag.replace('.', ''); + LuaUtils.resetVideoTag(tag); + var leVid:FlxVideoSprite = new FlxVideoSprite(x, y); + leVid.antialiasing = ClientPrefs.data.antialiasing; + psychlua.ScriptHandler.modchartVideos.set(tag, leVid); + leVid.active = true; + }); + Lua_helper.add_callback(lua, "addLuaVideo", function(tag:String, front:Bool = false) { + if(psychlua.ScriptHandler.modchartVideos.exists(tag)) { + var shit:FlxVideoSprite = psychlua.ScriptHandler.modchartVideos.get(tag); + + if(front || LuaUtils.getTargetInstance().members.length == 0) + LuaUtils.getTargetInstance().add(shit); + else + { + if(FlxG.state == PlayState.instance) + if(!PlayState.instance.isDead) + FlxG.state.insert(PlayState.instance.members.indexOf(LuaUtils.getLowestCharacterGroup()), shit); + else + GameOverSubstate.instance.insert(GameOverSubstate.instance.members.indexOf(GameOverSubstate.instance.boyfriend), shit); + else + LuaUtils.getTargetInstance().insert(LuaUtils.getTargetInstance().members.length, shit); + } + } + }); + Lua_helper.add_callback(lua, "removeLuaVideo", function(tag:String, destroy:Bool = true) { + if(!psychlua.ScriptHandler.modchartVideos.exists(tag)) { + return; + } + + var pee:FlxVideoSprite = psychlua.ScriptHandler.modchartVideos.get(tag); + if(destroy) { + pee.kill(); + } + + LuaUtils.getTargetInstance().remove(pee, true); + pee.stop(); + if(destroy) { + pee.destroy(); + psychlua.ScriptHandler.modchartVideos.remove(tag); + } + }); + + Lua_helper.add_callback(lua, "playLuaVideo", function(tag:String, path:String, loop:Bool = false) { + if(psychlua.ScriptHandler.modchartVideos.exists(tag)) { + var vid:FlxVideoSprite = psychlua.ScriptHandler.modchartVideos.get(tag); + if(path != null && path.length > 0) { + vid.play(Paths.video(path), loop); + } + + vid.bitmap.onOpening.add(function() { + psychlua.ScriptHandler.callOnLuas('onVideoOpening', [tag]); + }, true); + vid.bitmap.onPlaying.add(function() { + psychlua.ScriptHandler.callOnLuas('onVideoPlaying', [tag]); + }, true); + vid.bitmap.onStopped.add(function() { + psychlua.ScriptHandler.callOnLuas('onVideoStopped', [tag]); + }, true); + vid.bitmap.onPaused.add(function() { + psychlua.ScriptHandler.callOnLuas('onVideoPaused', [tag]); + }, true); + vid.bitmap.onEndReached.add(function() { + psychlua.ScriptHandler.callOnLuas('onVideoEnd', [tag]); + }, true); + vid.bitmap.onEncounteredError.add(function() { + psychlua.ScriptHandler.callOnLuas('onVideoError', [tag]); + }, true); + } + }); + Lua_helper.add_callback(lua, "stopLuaVideo", function(tag:String) { + if(psychlua.ScriptHandler.modchartVideos.exists(tag)) { + var vid:FlxVideoSprite = psychlua.ScriptHandler.modchartVideos.get(tag); + vid.stop(); + } + }); + Lua_helper.add_callback(lua, "pauseLuaVideo", function(tag:String) { + if(psychlua.ScriptHandler.modchartVideos.exists(tag)) { + var vid:FlxVideoSprite = psychlua.ScriptHandler.modchartVideos.get(tag); + vid.pause(); + } + }); + Lua_helper.add_callback(lua, "resumeLuaVideo", function(tag:String) { + if(psychlua.ScriptHandler.modchartVideos.exists(tag)) { + var vid:FlxVideoSprite = psychlua.ScriptHandler.modchartVideos.get(tag); + vid.resume(); + } + }); + + Lua_helper.add_callback(lua, "getVideoTime", function(tag:String) { + if(tag != null && tag.length > 0 && psychlua.ScriptHandler.modchartVideos.exists(tag)) { + return psychlua.ScriptHandler.modchartVideos.get(tag).bitmap.time; + } + return 0; + }); + Lua_helper.add_callback(lua, "setVideoTime", function(tag:String, value:Int) { + if(tag != null && tag.length > 0 && psychlua.ScriptHandler.modchartSounds.exists(tag)) { + var vid:FlxVideoSprite = psychlua.ScriptHandler.modchartVideos.get(tag); + if(vid != null) { + var wasResumed:Bool = vid.bitmap.isPlaying; + vid.pause(); + vid.bitmap.time = value; + if(wasResumed) vid.resume(); + } + } + }); + } +} \ No newline at end of file diff --git a/source/states/PlayState.hx b/source/states/PlayState.hx index f54285f..6adf3c9 100644 --- a/source/states/PlayState.hx +++ b/source/states/PlayState.hx @@ -53,18 +53,6 @@ import objects.Note.EventNote; import objects.*; import states.stages.objects.*; -#if LUA_ALLOWED -import psychlua.*; -#else -import psychlua.FunkinLua; -import psychlua.LuaUtils; -import psychlua.HScript; -#end - -#if SScript -import tea.SScript; -#end - class PlayState extends MusicBeatState { public static var STRUM_X = 48.5; @@ -89,21 +77,6 @@ class PlayState extends MusicBeatState public var boyfriendMap:Map = new Map(); public var dadMap:Map = new Map(); public var gfMap:Map = new Map(); - public var variables:Map = new Map(); - - #if HSCRIPT_ALLOWED - public var hscriptArray:Array = []; - public var instancesExclude:Array = []; - #end - - #if LUA_ALLOWED - public var modchartTweens:Map = new Map(); - public var modchartSprites:Map = new Map(); - public var modchartTimers:Map = new Map(); - public var modchartSounds:Map = new Map(); - public var modchartTexts:Map = new Map(); - public var modchartSaves:Map = new Map(); - #end public var BF_X:Float = 770; public var BF_Y:Float = 100; @@ -256,10 +229,7 @@ class PlayState extends MusicBeatState // Lua shit public static var instance:PlayState; - #if LUA_ALLOWED public var luaArray:Array = []; #end - #if (LUA_ALLOWED || HSCRIPT_ALLOWED) - private var luaDebugGroup:FlxTypedGroup; - #end + public var introSoundsSuffix:String = ''; // Less laggy controls @@ -407,37 +377,16 @@ class PlayState extends MusicBeatState add(dadGroup); add(boyfriendGroup); - #if LUA_ALLOWED - luaDebugGroup = new FlxTypedGroup(); - luaDebugGroup.cameras = [camOther]; - add(luaDebugGroup); - #end + // GLOBAL SCRIPTS + psychlua.ScriptHandler.startScripts(); - // "GLOBAL" SCRIPTS - #if (LUA_ALLOWED || HSCRIPT_ALLOWED) - var foldersToCheck:Array = Mods.directoriesWithFile(Paths.getSharedPath(), 'scripts/'); - for (folder in foldersToCheck) - for (file in FileSystem.readDirectory(folder)) - { - #if LUA_ALLOWED - if(file.toLowerCase().endsWith('.lua')) - new FunkinLua(folder + file); - #end - #if HSCRIPT_ALLOWED - if(file.toLowerCase().endsWith('.hx')) - initHScript(folder + file); - #end - } - #end + // STATE SCRIPTS + #if LUA_ALLOWED psychlua.ScriptHandler.startLuasNamed('states/PlayState.lua'); #end + #if HSCRIPT_ALLOWED psychlua.ScriptHandler.startHScriptsNamed('states/PlayState.hx'); #end // STAGE SCRIPTS - #if LUA_ALLOWED - startLuasNamed('stages/' + curStage + '.lua'); - #end - - #if HSCRIPT_ALLOWED - startHScriptsNamed('stages/' + curStage + '.hx'); - #end + #if LUA_ALLOWED psychlua.ScriptHandler.startLuasNamed('stages/$curStage.lua'); #end + #if HSCRIPT_ALLOWED psychlua.ScriptHandler.startHScriptsNamed('stages/$curStage.hx'); #end if (!stageData.hide_girlfriend) { @@ -621,16 +570,16 @@ class PlayState extends MusicBeatState #if LUA_ALLOWED for (notetype in noteTypes) - startLuasNamed('custom_notetypes/' + notetype + '.lua'); + psychlua.ScriptHandler.startLuasNamed('custom_notetypes/' + notetype + '.lua'); for (event in eventsPushed) - startLuasNamed('custom_events/' + event + '.lua'); + psychlua.ScriptHandler.startLuasNamed('custom_events/' + event + '.lua'); #end #if HSCRIPT_ALLOWED for (notetype in noteTypes) - startHScriptsNamed('custom_notetypes/' + notetype + '.hx'); + psychlua.ScriptHandler.startHScriptsNamed('custom_notetypes/' + notetype + '.hx'); for (event in eventsPushed) - startHScriptsNamed('custom_events/' + event + '.hx'); + psychlua.ScriptHandler.startHScriptsNamed('custom_events/' + event + '.hx'); #end noteTypes = null; eventsPushed = null; @@ -652,7 +601,7 @@ class PlayState extends MusicBeatState #if HSCRIPT_ALLOWED if(file.toLowerCase().endsWith('.hx')) - initHScript(folder + file); + psychlua.ScriptHandler.initHScript(folder + file); #end } #end @@ -687,7 +636,7 @@ class PlayState extends MusicBeatState FlxG.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress); FlxG.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease); - callOnScripts('onCreatePost'); + psychlua.ScriptHandler.callOnScripts('onCreatePost'); cacheCountdown(); cachePopUpScore(); @@ -743,28 +692,10 @@ class PlayState extends MusicBeatState playbackRate = value; FlxAnimationController.globalSpeed = value; Conductor.safeZoneOffset = (ClientPrefs.data.safeFrames / 60) * 1000 * value; - setOnScripts('playbackRate', playbackRate); + psychlua.ScriptHandler.setOnScripts('playbackRate', playbackRate); return value; } - #if (LUA_ALLOWED || HSCRIPT_ALLOWED) - public function addTextToDebug(text:String, color:FlxColor) { - var newText:psychlua.DebugLuaText = luaDebugGroup.recycle(psychlua.DebugLuaText); - newText.text = text; - newText.color = color; - newText.disableTime = 6; - newText.alpha = 1; - newText.setPosition(10, 8 - newText.height); - - luaDebugGroup.forEachAlive(function(spr:psychlua.DebugLuaText) { - spr.y += newText.height + 2; - }); - luaDebugGroup.add(newText); - - Sys.println(text); - } - #end - public function reloadHealthBarColors() { var dadCol = dad.healthColorArray; var bfCol = boyfriend.healthColorArray; @@ -830,7 +761,7 @@ class PlayState extends MusicBeatState #end if(doPush){ - for (script in luaArray) + for (script in psychlua.ScriptHandler.luaArray) { if(script.scriptName == luaFile){ doPush = false; @@ -859,16 +790,16 @@ class PlayState extends MusicBeatState if(SScript.global.exists(scriptFile)) doPush = false; - if(doPush) initHScript(scriptFile); + if(doPush) psychlua.ScriptHandler.initHScript(scriptFile); } #end } public function getLuaObject(tag:String, text:Bool=true):FlxSprite { #if LUA_ALLOWED - if(modchartSprites.exists(tag)) return modchartSprites.get(tag); - if(text && modchartTexts.exists(tag)) return modchartTexts.get(tag); - if(variables.exists(tag)) return variables.get(tag); + if(psychlua.ScriptHandler.modchartSprites.exists(tag)) return psychlua.ScriptHandler.modchartSprites.get(tag); + if(text && psychlua.ScriptHandler.modchartTexts.exists(tag)) return psychlua.ScriptHandler.modchartTexts.get(tag); + if(psychlua.ScriptHandler.variables.exists(tag)) return psychlua.ScriptHandler.variables.get(tag); #end return null; } @@ -994,32 +925,32 @@ class PlayState extends MusicBeatState public function startCountdown() { if(startedCountdown) { - callOnScripts('onStartCountdown'); + psychlua.ScriptHandler.callOnScripts('onStartCountdown'); return false; } seenCutscene = true; inCutscene = false; - var ret:Dynamic = callOnScripts('onStartCountdown', null, true); + var ret:Dynamic = psychlua.ScriptHandler.callOnScripts('onStartCountdown', null, true); if(ret != FunkinLua.Function_Stop) { if (skipCountdown || startOnTime > 0) skipArrowStartTween = true; generateStaticArrows(0); generateStaticArrows(1); for (i in 0...playerStrums.length) { - setOnScripts('defaultPlayerStrumX' + i, playerStrums.members[i].x); - setOnScripts('defaultPlayerStrumY' + i, playerStrums.members[i].y); + psychlua.ScriptHandler.setOnScripts('defaultPlayerStrumX' + i, playerStrums.members[i].x); + psychlua.ScriptHandler.setOnScripts('defaultPlayerStrumY' + i, playerStrums.members[i].y); } for (i in 0...opponentStrums.length) { - setOnScripts('defaultOpponentStrumX' + i, opponentStrums.members[i].x); - setOnScripts('defaultOpponentStrumY' + i, opponentStrums.members[i].y); + psychlua.ScriptHandler.setOnScripts('defaultOpponentStrumX' + i, opponentStrums.members[i].x); + psychlua.ScriptHandler.setOnScripts('defaultOpponentStrumY' + i, opponentStrums.members[i].y); //if(ClientPrefs.data.middleScroll) opponentStrums.members[i].visible = false; } startedCountdown = true; Conductor.songPosition = -Conductor.crochet * 5; - setOnScripts('startedCountdown', true); - callOnScripts('onCountdownStarted', null); + psychlua.ScriptHandler.setOnScripts('startedCountdown', true); + psychlua.ScriptHandler.callOnScripts('onCountdownStarted', null); var swagCounter:Int = 0; @@ -1098,8 +1029,8 @@ class PlayState extends MusicBeatState }); stagesFunc(function(stage:BaseStage) stage.countdownTick(tick, swagCounter)); - callOnLuas('onCountdownTick', [swagCounter]); - callOnHScript('onCountdownTick', [tick, swagCounter]); + psychlua.ScriptHandler.callOnLuas('onCountdownTick', [swagCounter]); + psychlua.ScriptHandler.callOnHScript('onCountdownTick', [tick, swagCounter]); swagCounter += 1; }, 5); @@ -1178,7 +1109,7 @@ class PlayState extends MusicBeatState public function updateScore(miss:Bool = false) { - var ret:Dynamic = callOnScripts('preUpdateScore', [miss], true); + var ret:Dynamic = psychlua.ScriptHandler.callOnScripts('preUpdateScore', [miss], true); if (ret == FunkinLua.Function_Stop) return; @@ -1198,7 +1129,7 @@ class PlayState extends MusicBeatState if (!miss && !cpuControlled) doScoreBop(); - callOnScripts('onUpdateScore', [miss]); + psychlua.ScriptHandler.callOnScripts('onUpdateScore', [miss]); } public function doScoreBop():Void { @@ -1239,11 +1170,11 @@ class PlayState extends MusicBeatState public function startNextDialogue() { dialogueCount++; - callOnScripts('onNextDialogue', [dialogueCount]); + psychlua.ScriptHandler.callOnScripts('onNextDialogue', [dialogueCount]); } public function skipDialogue() { - callOnScripts('onSkipDialogue', [dialogueCount]); + psychlua.ScriptHandler.callOnScripts('onSkipDialogue', [dialogueCount]); } function startSong():Void @@ -1278,8 +1209,8 @@ class PlayState extends MusicBeatState DiscordClient.changePresence(detailsText, SONG.song + " (" + storyDifficultyText + ")", iconP2.getCharacter(), true, songLength); #end #end - setOnScripts('songLength', songLength); - callOnScripts('onSongStart'); + psychlua.ScriptHandler.setOnScripts('songLength', songLength); + psychlua.ScriptHandler.callOnScripts('onSongStart'); } var debugNum:Int = 0; @@ -1459,7 +1390,7 @@ class PlayState extends MusicBeatState } function eventEarlyTrigger(event:EventNote):Float { - var returnedValue:Null = callOnScripts('eventEarlyTrigger', [event.event, event.value1, event.value2, event.strumTime], true, [], [0]); + var returnedValue:Null = psychlua.ScriptHandler.callOnScripts('eventEarlyTrigger', [event.event, event.value1, event.value2, event.strumTime], true, [], [0]); if(returnedValue != null && returnedValue != 0 && returnedValue != FunkinLua.Function_Continue) { return returnedValue; } @@ -1485,7 +1416,7 @@ class PlayState extends MusicBeatState }; eventNotes.push(subEvent); eventPushed(subEvent); - callOnScripts('onEventPushed', [subEvent.event, subEvent.value1 != null ? subEvent.value1 : '', subEvent.value2 != null ? subEvent.value2 : '', subEvent.strumTime]); + psychlua.ScriptHandler.callOnScripts('onEventPushed', [subEvent.event, subEvent.value1 != null ? subEvent.value1 : '', subEvent.value2 != null ? subEvent.value2 : '', subEvent.strumTime]); } public var skipArrowStartTween:Bool = false; //for lua @@ -1548,8 +1479,8 @@ class PlayState extends MusicBeatState char.colorTween.active = false; #if LUA_ALLOWED - for (tween in modchartTweens) tween.active = false; - for (timer in modchartTimers) timer.active = false; + for (tween in psychlua.ScriptHandler.modchartTweens) tween.active = false; + for (timer in psychlua.ScriptHandler.modchartTimers) timer.active = false; #end } @@ -1578,12 +1509,12 @@ class PlayState extends MusicBeatState char.colorTween.active = true; #if LUA_ALLOWED - for (tween in modchartTweens) tween.active = true; - for (timer in modchartTimers) timer.active = true; + for (tween in psychlua.ScriptHandler.modchartTweens) tween.active = true; + for (timer in psychlua.ScriptHandler.modchartTimers) timer.active = true; #end paused = false; - callOnScripts('onResume'); + psychlua.ScriptHandler.callOnScripts('onResume'); resetRPC(startTimer != null && startTimer.finished); } } @@ -1656,12 +1587,12 @@ class PlayState extends MusicBeatState } else boyfriendIdleTime = 0; } else FlxG.camera.followLerp = 0; - callOnScripts('onUpdate', [elapsed]); + psychlua.ScriptHandler.callOnScripts('onUpdate', [elapsed]); super.update(elapsed); - setOnScripts('curDecStep', curDecStep); - setOnScripts('curDecBeat', curDecBeat); + psychlua.ScriptHandler.setOnScripts('curDecStep', curDecStep); + psychlua.ScriptHandler.setOnScripts('curDecBeat', curDecBeat); if(ClientPrefs.data.botplaySine){ if(botplayTxt != null && botplayTxt.visible){ @@ -1676,7 +1607,7 @@ class PlayState extends MusicBeatState if(controls.PAUSE && startedCountdown && canPause) { - var ret:Dynamic = callOnScripts('onPause', null, true); + var ret:Dynamic = psychlua.ScriptHandler.callOnScripts('onPause', null, true); if(ret != FunkinLua.Function_Stop) openPauseMenu(); } @@ -1747,8 +1678,8 @@ class PlayState extends MusicBeatState notes.insert(0, dunceNote); dunceNote.spawned = true; - callOnLuas('onSpawnNote', [notes.members.indexOf(dunceNote), dunceNote.noteData, dunceNote.noteType, dunceNote.isSustainNote, dunceNote.strumTime]); - callOnHScript('onSpawnNote', [dunceNote]); + psychlua.ScriptHandler.callOnLuas('onSpawnNote', [notes.members.indexOf(dunceNote), dunceNote.noteData, dunceNote.noteType, dunceNote.isSustainNote, dunceNote.strumTime]); + psychlua.ScriptHandler.callOnHScript('onSpawnNote', [dunceNote]); var index:Int = unspawnNotes.indexOf(dunceNote); unspawnNotes.splice(index, 1); @@ -1823,10 +1754,10 @@ class PlayState extends MusicBeatState } #end - setOnScripts('cameraX', camFollow.x); - setOnScripts('cameraY', camFollow.y); - setOnScripts('botPlay', cpuControlled); - callOnScripts('onUpdatePost', [elapsed]); + psychlua.ScriptHandler.setOnScripts('cameraX', camFollow.x); + psychlua.ScriptHandler.setOnScripts('cameraY', camFollow.y); + psychlua.ScriptHandler.setOnScripts('botPlay', cpuControlled); + psychlua.ScriptHandler.callOnScripts('onUpdatePost', [elapsed]); } public dynamic function updateIcons(elapsed:Float){ @@ -1946,7 +1877,7 @@ class PlayState extends MusicBeatState function doDeathCheck(?skipHealthCheck:Bool = false) { if (((skipHealthCheck && instakillOnMiss) || health <= 0) && !practiceMode && !isDead) { - var ret:Dynamic = callOnScripts('onGameOver', null, true); + var ret:Dynamic = psychlua.ScriptHandler.callOnScripts('onGameOver', null, true); if(ret != FunkinLua.Function_Stop) { boyfriend.stunned = true; deathCounter++; @@ -1959,10 +1890,10 @@ class PlayState extends MusicBeatState persistentUpdate = false; persistentDraw = false; #if LUA_ALLOWED - for (tween in modchartTweens) { + for (tween in psychlua.ScriptHandler.modchartTweens) { tween.active = true; } - for (timer in modchartTimers) { + for (timer in psychlua.ScriptHandler.modchartTimers) { timer.active = true; } #end @@ -2155,7 +2086,7 @@ class PlayState extends MusicBeatState boyfriend.alpha = lastAlpha; iconP1.changeIcon(boyfriend.healthIcon); } - setOnScripts('boyfriendName', boyfriend.curCharacter); + psychlua.ScriptHandler.setOnScripts('boyfriendName', boyfriend.curCharacter); case 1: if(dad.curCharacter != value2) { @@ -2177,7 +2108,7 @@ class PlayState extends MusicBeatState dad.alpha = lastAlpha; iconP2.changeIcon(dad.healthIcon); } - setOnScripts('dadName', dad.curCharacter); + psychlua.ScriptHandler.setOnScripts('dadName', dad.curCharacter); case 2: if(gf != null) @@ -2193,7 +2124,7 @@ class PlayState extends MusicBeatState gf = gfMap.get(value2); gf.alpha = lastAlpha; } - setOnScripts('gfName', gf.curCharacter); + psychlua.ScriptHandler.setOnScripts('gfName', gf.curCharacter); } } reloadHealthBarColors(); @@ -2231,7 +2162,7 @@ class PlayState extends MusicBeatState var len:Int = e.message.indexOf('\n') + 1; if(len <= 0) len = e.message.length; #if (LUA_ALLOWED || HSCRIPT_ALLOWED) - addTextToDebug('ERROR ("Set Property" Event) - ' + e.message.substr(0, len), FlxColor.RED); + psychlua.ScriptHandler.addTextToDebug('ERROR ("Set Property" Event) - ' + e.message.substr(0, len), FlxColor.RED); #else FlxG.log.warn('ERROR ("Set Property" Event) - ' + e.message.substr(0, len)); #end @@ -2243,7 +2174,7 @@ class PlayState extends MusicBeatState } stagesFunc(function(stage:BaseStage) stage.eventCalled(eventName, value1, value2, flValue1, flValue2, strumTime)); - callOnScripts('onEvent', [eventName, value1, value2, strumTime]); + psychlua.ScriptHandler.callOnScripts('onEvent', [eventName, value1, value2, strumTime]); } function moveCameraSection(?sec:Null):Void { @@ -2258,13 +2189,13 @@ class PlayState extends MusicBeatState camFollow.x += gf.cameraPosition[0] + girlfriendCameraOffset[0]; camFollow.y += gf.cameraPosition[1] + girlfriendCameraOffset[1]; tweenCamIn(); - callOnScripts('onMoveCamera', ['gf']); + psychlua.ScriptHandler.callOnScripts('onMoveCamera', ['gf']); return; } var isDad:Bool = (SONG.notes[sec].mustHitSection != true); moveCamera(isDad); - callOnScripts('onMoveCamera', [isDad ? 'dad' : 'boyfriend']); + psychlua.ScriptHandler.callOnScripts('onMoveCamera', [isDad ? 'dad' : 'boyfriend']); } var cameraTwn:FlxTween; @@ -2360,7 +2291,7 @@ class PlayState extends MusicBeatState checkForAchievement([weekNoMiss, 'ur_bad', 'ur_good', 'hype', 'two_keys', 'toastie', 'debugger']); #end - var ret:Dynamic = callOnScripts('onEndSong', null, true); + var ret:Dynamic = psychlua.ScriptHandler.callOnScripts('onEndSong', null, true); if(ret != FunkinLua.Function_Stop && !transitioning) { var percent:Float = CoolUtil.floorDecimal(ratingPercent * 100, 2); @@ -2660,7 +2591,7 @@ class PlayState extends MusicBeatState { if(cpuControlled || paused || inCutscene || key < 0 || key >= playerStrums.length || !generatedMusic || endingSong || boyfriend.stunned) return; - var ret:Dynamic = callOnScripts('onKeyPressPre', [key]); + var ret:Dynamic = psychlua.ScriptHandler.callOnScripts('onKeyPressPre', [key]); if(ret == FunkinLua.Function_Stop) return; // more accurate hit time for the ratings? @@ -2693,7 +2624,7 @@ class PlayState extends MusicBeatState } else if(shouldMiss) { - callOnScripts('onGhostTap', [key]); + psychlua.ScriptHandler.callOnScripts('onGhostTap', [key]); noteMissPress(key); } @@ -2708,7 +2639,7 @@ class PlayState extends MusicBeatState spr.playAnim('pressed'); spr.resetAnim = 0; } - callOnScripts('onKeyPress', [key]); + psychlua.ScriptHandler.callOnScripts('onKeyPress', [key]); } public static function sortHitNotes(a:Note, b:Note):Int @@ -2731,7 +2662,7 @@ class PlayState extends MusicBeatState { if(cpuControlled || !startedCountdown || paused || key < 0 || key >= playerStrums.length) return; - var ret:Dynamic = callOnScripts('onKeyReleasePre', [key]); + var ret:Dynamic = psychlua.ScriptHandler.callOnScripts('onKeyReleasePre', [key]); if(ret == FunkinLua.Function_Stop) return; var spr:StrumNote = playerStrums.members[key]; @@ -2740,7 +2671,7 @@ class PlayState extends MusicBeatState spr.playAnim('static'); spr.resetAnim = 0; } - callOnScripts('onKeyRelease', [key]); + psychlua.ScriptHandler.callOnScripts('onKeyRelease', [key]); } public static function getKeyFromEvent(arr:Array, key:FlxKey):Int @@ -2815,8 +2746,8 @@ class PlayState extends MusicBeatState }); noteMissCommon(daNote.noteData, daNote); - var result:Dynamic = callOnLuas('noteMiss', [notes.members.indexOf(daNote), daNote.noteData, daNote.noteType, daNote.isSustainNote]); - if(result != FunkinLua.Function_Stop && result != FunkinLua.Function_StopHScript && result != FunkinLua.Function_StopAll) callOnHScript('noteMiss', [daNote]); + var result:Dynamic = psychlua.ScriptHandler.callOnLuas('noteMiss', [notes.members.indexOf(daNote), daNote.noteData, daNote.noteType, daNote.isSustainNote]); + if(result != FunkinLua.Function_Stop && result != FunkinLua.Function_StopHScript && result != FunkinLua.Function_StopAll) psychlua.ScriptHandler.callOnHScript('noteMiss', [daNote]); } function noteMissPress(direction:Int = 1):Void //You pressed a key when there was no notes to press for this key @@ -2825,7 +2756,7 @@ class PlayState extends MusicBeatState noteMissCommon(direction); FlxG.sound.play(Paths.soundRandom('missnote', 1, 3), FlxG.random.float(0.1, 0.2)); - callOnScripts('noteMissPress', [direction]); + psychlua.ScriptHandler.callOnScripts('noteMissPress', [direction]); } function noteMissCommon(direction:Int, note:Note = null) @@ -2958,8 +2889,8 @@ class PlayState extends MusicBeatState strumPlayAnim(true, Std.int(Math.abs(note.noteData)), Conductor.stepCrochet * 1.25 / 1000 / playbackRate); note.hitByOpponent = true; - var result:Dynamic = callOnLuas('opponentNoteHitPost', [notes.members.indexOf(note), Math.abs(note.noteData), note.noteType, note.isSustainNote]); - if(result != FunkinLua.Function_Stop && result != FunkinLua.Function_StopHScript && result != FunkinLua.Function_StopAll) callOnHScript('opponentNoteHitPost', [note]); + var result:Dynamic = psychlua.ScriptHandler.callOnLuas('opponentNoteHitPost', [notes.members.indexOf(note), Math.abs(note.noteData), note.noteType, note.isSustainNote]); + if(result != FunkinLua.Function_Stop && result != FunkinLua.Function_StopHScript && result != FunkinLua.Function_StopAll) psychlua.ScriptHandler.callOnHScript('opponentNoteHitPost', [note]); if(!note.isSustainNote) invalidateNote(note); } @@ -2971,8 +2902,8 @@ class PlayState extends MusicBeatState var isSus:Bool = note.isSustainNote; //GET OUT OF MY HEAD, GET OUT OF MY HEAD, GET OUT OF MY HEAD var leData:Int = Math.round(Math.abs(note.noteData)); var leType:String = note.noteType; - var result:Dynamic = callOnLuas('goodNoteHit', [notes.members.indexOf(note), leData, leType, isSus]); - if(result != FunkinLua.Function_Stop && result != FunkinLua.Function_StopHScript && result != FunkinLua.Function_StopAll) callOnHScript('goodNoteHit', [note]); + var result:Dynamic = psychlua.ScriptHandler.callOnLuas('goodNoteHit', [notes.members.indexOf(note), leData, leType, isSus]); + if(result != FunkinLua.Function_Stop && result != FunkinLua.Function_StopHScript && result != FunkinLua.Function_StopAll) psychlua.ScriptHandler.callOnHScript('goodNoteHit', [note]); note.wasGoodHit = true; if (ClientPrefs.data.hitsoundVolume > 0 && !note.hitsoundDisabled) FlxG.sound.play(Paths.sound(note.hitsound), ClientPrefs.data.hitsoundVolume); @@ -3032,8 +2963,8 @@ class PlayState extends MusicBeatState if (guitarHeroSustains && note.isSustainNote) gainHealth = false; if (gainHealth) health += note.hitHealth * healthGain; - var result:Dynamic = callOnLuas('goodNoteHitPost', [notes.members.indexOf(note), leData, leType, isSus]); - if(result != FunkinLua.Function_Stop && result != FunkinLua.Function_StopHScript && result != FunkinLua.Function_StopAll) callOnHScript('goodNoteHitPost', [note]); + var result:Dynamic = psychlua.ScriptHandler.callOnLuas('goodNoteHitPost', [notes.members.indexOf(note), leData, leType, isSus]); + if(result != FunkinLua.Function_Stop && result != FunkinLua.Function_StopHScript && result != FunkinLua.Function_StopAll) psychlua.ScriptHandler.callOnHScript('goodNoteHitPost', [note]); if(!note.isSustainNote) invalidateNote(note); } @@ -3058,27 +2989,7 @@ class PlayState extends MusicBeatState } override function destroy() { - #if LUA_ALLOWED - for (i in 0...luaArray.length) { - var lua:FunkinLua = luaArray[0]; - lua.call('onDestroy', []); - lua.stop(); - } - luaArray = []; - FunkinLua.customFunctions.clear(); - #end - - #if HSCRIPT_ALLOWED - for (script in hscriptArray) - if(script != null) - { - script.call('onDestroy'); - script.destroy(); - } - - while (hscriptArray.length > 0) - hscriptArray.pop(); - #end + psychlua.ScriptHandler.destroyScripts(); FlxG.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyPress); FlxG.stage.removeEventListener(KeyboardEvent.KEY_UP, onKeyRelease); @@ -3116,8 +3027,6 @@ class PlayState extends MusicBeatState } lastStepHit = curStep; - setOnScripts('curStep', curStep); - callOnScripts('onStepHit'); } var lastBeatHit:Int = -1; @@ -3144,9 +3053,6 @@ class PlayState extends MusicBeatState lastBeatHit = curBeat; //buttplug fuckery ButtplugUtils.sendPayload(bpPayload); - - setOnScripts('curBeat', curBeat); - callOnScripts('onBeatHit'); } public function characterBopper(beat:Int):Void { @@ -3180,238 +3086,16 @@ class PlayState extends MusicBeatState if (SONG.notes[curSection].changeBPM) { Conductor.bpm = SONG.notes[curSection].bpm; - setOnScripts('curBpm', Conductor.bpm); - setOnScripts('crochet', Conductor.crochet); - setOnScripts('stepCrochet', Conductor.stepCrochet); + psychlua.ScriptHandler.setOnScripts('curBpm', Conductor.bpm); + psychlua.ScriptHandler.setOnScripts('crochet', Conductor.crochet); + psychlua.ScriptHandler.setOnScripts('stepCrochet', Conductor.stepCrochet); } - setOnScripts('mustHitSection', SONG.notes[curSection].mustHitSection); - setOnScripts('altAnim', SONG.notes[curSection].altAnim); - setOnScripts('gfSection', SONG.notes[curSection].gfSection); + psychlua.ScriptHandler.setOnScripts('mustHitSection', SONG.notes[curSection].mustHitSection); + psychlua.ScriptHandler.setOnScripts('altAnim', SONG.notes[curSection].altAnim); + psychlua.ScriptHandler.setOnScripts('gfSection', SONG.notes[curSection].gfSection); } super.sectionHit(); - - setOnScripts('curSection', curSection); - callOnScripts('onSectionHit'); - } - - #if LUA_ALLOWED - public function startLuasNamed(luaFile:String) - { - #if MODS_ALLOWED - var luaToLoad:String = Paths.modFolders(luaFile); - if(!FileSystem.exists(luaToLoad)) - luaToLoad = Paths.getSharedPath(luaFile); - - if(FileSystem.exists(luaToLoad)) - #elseif sys - var luaToLoad:String = Paths.getSharedPath(luaFile); - if(OpenFlAssets.exists(luaToLoad)) - #end - { - for (script in luaArray) - if(script.scriptName == luaToLoad) return false; - - new FunkinLua(luaToLoad); - return true; - } - return false; - } - #end - - #if HSCRIPT_ALLOWED - public function startHScriptsNamed(scriptFile:String) - { - var scriptToLoad:String = Paths.modFolders(scriptFile); - if(!FileSystem.exists(scriptToLoad)) - scriptToLoad = Paths.getSharedPath(scriptFile); - - if(FileSystem.exists(scriptToLoad)) - { - if (SScript.global.exists(scriptToLoad)) return false; - - initHScript(scriptToLoad); - return true; - } - return false; - } - - public function initHScript(file:String) - { - try - { - var newScript:HScript = new HScript(null, file); - if(newScript.parsingException != null) - { - addTextToDebug('ERROR ON LOADING: ${newScript.parsingException.message}', FlxColor.RED); - newScript.destroy(); - return; - } - - hscriptArray.push(newScript); - if(newScript.exists('onCreate')) - { - var callValue = newScript.call('onCreate'); - if(!callValue.succeeded) - { - { - for (e in callValue.exceptions) - { - if (e != null) - { - var len:Int = e.message.indexOf('\n') + 1; - if(len <= 0) len = e.message.length; - addTextToDebug('ERROR ($file: onCreate) - ${e.message.substr(0, len)}', FlxColor.RED); - } - } - } - newScript.destroy(); - hscriptArray.remove(newScript); - trace('failed to initialize tea interp!!! ($file)'); - } - else trace('initialized tea interp successfully: $file'); - } - - } - catch(e) - { - var len:Int = e.message.indexOf('\n') + 1; - if(len <= 0) len = e.message.length; - addTextToDebug('ERROR ($file) - ' + e.message.substr(0, len), FlxColor.RED); - var newScript:HScript = cast (SScript.global.get(file), HScript); - if(newScript != null) - { - newScript.destroy(); - hscriptArray.remove(newScript); - } - } - } - #end - - public function callOnScripts(funcToCall:String, args:Array = null, ignoreStops = false, exclusions:Array = null, excludeValues:Array = null):Dynamic { - var returnVal:Dynamic = psychlua.FunkinLua.Function_Continue; - if(args == null) args = []; - if(exclusions == null) exclusions = []; - if(excludeValues == null) excludeValues = [psychlua.FunkinLua.Function_Continue]; - - var result:Dynamic = callOnLuas(funcToCall, args, ignoreStops, exclusions, excludeValues); - if(result == null || excludeValues.contains(result)) result = callOnHScript(funcToCall, args, ignoreStops, exclusions, excludeValues); - return result; - } - - public function callOnLuas(funcToCall:String, args:Array = null, ignoreStops = false, exclusions:Array = null, excludeValues:Array = null):Dynamic { - var returnVal:Dynamic = FunkinLua.Function_Continue; - #if LUA_ALLOWED - if(args == null) args = []; - if(exclusions == null) exclusions = []; - if(excludeValues == null) excludeValues = [FunkinLua.Function_Continue]; - - var len:Int = luaArray.length; - var i:Int = 0; - while(i < len) - { - var script:FunkinLua = luaArray[i]; - if(exclusions.contains(script.scriptName)) - { - i++; - continue; - } - - var myValue:Dynamic = script.call(funcToCall, args); - if((myValue == FunkinLua.Function_StopLua || myValue == FunkinLua.Function_StopAll) && !excludeValues.contains(myValue) && !ignoreStops) - { - returnVal = myValue; - break; - } - - if(myValue != null && !excludeValues.contains(myValue)) - returnVal = myValue; - - if(!script.closed) i++; - else len--; - } - #end - return returnVal; - } - public function callOnHScript(funcToCall:String, args:Array = null, ?ignoreStops:Bool = false, exclusions:Array = null, excludeValues:Array = null):Dynamic { - var returnVal:Dynamic = psychlua.FunkinLua.Function_Continue; - - #if HSCRIPT_ALLOWED - if(exclusions == null) exclusions = new Array(); - if(excludeValues == null) excludeValues = new Array(); - excludeValues.push(psychlua.FunkinLua.Function_Continue); - - var len:Int = hscriptArray.length; - if (len < 1) - return returnVal; - for(i in 0...len) - { - var script:HScript = hscriptArray[i]; - if(script == null || !script.exists(funcToCall) || exclusions.contains(script.origin)) - continue; - - var myValue:Dynamic = null; - try - { - var callValue = script.call(funcToCall, args); - if(!callValue.succeeded) - { - var e = callValue.exceptions[0]; - if(e != null){ - var len:Int = e.message.indexOf('\n') + 1; - if(len <= 0) len = e.message.length; - FunkinLua.luaTrace('ERROR (${script.origin}: ${callValue.calledFunction}) - ' + e.message.substr(0, len), true, false, FlxColor.RED); - } - } - else - { - myValue = callValue.returnValue; - if((myValue == FunkinLua.Function_StopHScript || myValue == FunkinLua.Function_StopAll) && !excludeValues.contains(myValue) && !ignoreStops) - { - returnVal = myValue; - break; - } - - if(myValue != null && !excludeValues.contains(myValue)) - returnVal = myValue; - } - } - } - #end - - return returnVal; - } - - public function setOnScripts(variable:String, arg:Dynamic, exclusions:Array = null) { - if(exclusions == null) exclusions = []; - setOnLuas(variable, arg, exclusions); - setOnHScript(variable, arg, exclusions); - } - - public function setOnLuas(variable:String, arg:Dynamic, exclusions:Array = null) { - #if LUA_ALLOWED - if(exclusions == null) exclusions = []; - for (script in luaArray) { - if(exclusions.contains(script.scriptName)) - continue; - - script.set(variable, arg); - } - #end - } - - public function setOnHScript(variable:String, arg:Dynamic, exclusions:Array = null) { - #if HSCRIPT_ALLOWED - if(exclusions == null) exclusions = []; - for (script in hscriptArray) { - if(exclusions.contains(script.origin)) - continue; - - if(!instancesExclude.contains(variable)) - instancesExclude.push(variable); - script.set(variable, arg); - } - #end } function strumPlayAnim(isDad:Bool, id:Int, time:Float) { @@ -3432,12 +3116,12 @@ class PlayState extends MusicBeatState public var ratingPercent:Float; public var ratingFC:String; public function RecalculateRating(badHit:Bool = false) { - setOnScripts('score', songScore); - setOnScripts('misses', songMisses); - setOnScripts('hits', songHits); - setOnScripts('combo', combo); + psychlua.ScriptHandler.setOnScripts('score', songScore); + psychlua.ScriptHandler.setOnScripts('misses', songMisses); + psychlua.ScriptHandler.setOnScripts('hits', songHits); + psychlua.ScriptHandler.setOnScripts('combo', combo); - var ret:Dynamic = callOnScripts('onRecalculateRating', null, true); + var ret:Dynamic = psychlua.ScriptHandler.callOnScripts('onRecalculateRating', null, true); if(ret != FunkinLua.Function_Stop) { ratingName = '?'; @@ -3460,9 +3144,9 @@ class PlayState extends MusicBeatState fullComboFunction(); } updateScore(badHit); // score will only update after rating is calculated, if it's a badHit, it shouldn't bounce - setOnScripts('rating', ratingPercent); - setOnScripts('ratingName', ratingName); - setOnScripts('ratingFC', ratingFC); + psychlua.ScriptHandler.setOnScripts('rating', ratingPercent); + psychlua.ScriptHandler.setOnScripts('ratingName', ratingName); + psychlua.ScriptHandler.setOnScripts('ratingFC', ratingFC); } function fullComboUpdate() diff --git a/source/states/TitleState.hx b/source/states/TitleState.hx index 45ad329..4dace9b 100644 --- a/source/states/TitleState.hx +++ b/source/states/TitleState.hx @@ -46,6 +46,8 @@ typedef TitleData = { } class TitleState extends MusicBeatState { + public static var instance:TitleState; + public static var muteKeys:Array = [FlxKey.ZERO]; public static var volumeDownKeys:Array = [FlxKey.NUMPADMINUS, FlxKey.MINUS]; public static var volumeUpKeys:Array = [FlxKey.NUMPADPLUS, FlxKey.PLUS]; @@ -79,6 +81,8 @@ class TitleState extends MusicBeatState { override public function create():Void { + instance = this; + Paths.clearStoredMemory(); #if LUA_ALLOWED @@ -93,6 +97,13 @@ class TitleState extends MusicBeatState { FlxG.game.focusLostFramerate = 60; FlxG.keys.preventDefaultKeys = [TAB]; + // GLOBAL SCRIPTS + psychlua.ScriptHandler.startScripts(); + + // STATE SCRIPTS + #if LUA_ALLOWED psychlua.ScriptHandler.startLuasNamed('states/TitleState.lua'); #end + #if HSCRIPT_ALLOWED psychlua.ScriptHandler.startHScriptsNamed('states/TitleState.hx'); #end + curWacky = FlxG.random.getObject(getIntroTextShit()); super.create(); @@ -177,6 +188,8 @@ class TitleState extends MusicBeatState { }); } } + + psychlua.ScriptHandler.callOnScripts('onCreatePost'); } var logoBl:FlxSprite; @@ -330,8 +343,6 @@ class TitleState extends MusicBeatState { else initialized = true; - //PlayState.instance.callOnLuas("startIntro", []); - Paths.clearUnusedMemory(); } @@ -361,8 +372,6 @@ class TitleState extends MusicBeatState { override function update(elapsed:Float) { - //PlayState.instance.callOnLuas("updateTitle", [elapsed]); - if (FlxG.sound.music != null) Conductor.songPosition = FlxG.sound.music.time; @@ -515,7 +524,9 @@ class TitleState extends MusicBeatState { if(controls.UI_RIGHT) swagShader.hue += elapsed * 0.1; } + psychlua.ScriptHandler.callOnScripts('onUpdate', [elapsed]); super.update(elapsed); + psychlua.ScriptHandler.callOnScripts('onUpdatePost', [elapsed]); } function createCoolText(textArray:Array, ?offset:Float = 0) @@ -558,6 +569,8 @@ class TitleState extends MusicBeatState { { super.beatHit(); + psychlua.ScriptHandler.callOnScripts('onBeatHit'); + if(logoBl != null) logoBl.animation.play('bump', true); @@ -628,6 +641,8 @@ class TitleState extends MusicBeatState { var increaseVolume:Bool = false; function skipIntro():Void { + psychlua.ScriptHandler.callOnScripts('onSkipIntro'); + if (!skippedIntro) { if (playJingle) //Ignore deez @@ -717,4 +732,11 @@ class TitleState extends MusicBeatState { skippedIntro = true; } } + + override function destroy() + { + psychlua.ScriptHandler.destroyScripts(); + instance = null; + super.destroy(); + } } diff --git a/source/substates/GameOverSubstate.hx b/source/substates/GameOverSubstate.hx index 7f03d5e..a4fad5a 100644 --- a/source/substates/GameOverSubstate.hx +++ b/source/substates/GameOverSubstate.hx @@ -40,13 +40,13 @@ class GameOverSubstate extends MusicBeatSubstate override function create() { instance = this; - PlayState.instance.callOnScripts('onGameOverStart', []); + psychlua.ScriptHandler.callOnScripts('onGameOverStart', []); super.create(); } public function new(x:Float, y:Float, camX:Float, camY:Float) { super(); - PlayState.instance.setOnScripts('inGameOver', true); + psychlua.ScriptHandler.setOnScripts('inGameOver', true); Conductor.songPosition = 0; boyfriend = new Character(x, y, characterName, true); boyfriend.x += boyfriend.positionArray[0]; @@ -67,7 +67,7 @@ class GameOverSubstate extends MusicBeatSubstate override function update(elapsed:Float) { super.update(elapsed); - PlayState.instance.callOnScripts('onUpdate', [elapsed]); + psychlua.ScriptHandler.callOnScripts('onUpdate', [elapsed]); if (controls.ACCEPT) { endBullshit(); @@ -85,7 +85,7 @@ class GameOverSubstate extends MusicBeatSubstate else FlxG.switchState(() -> new FreeplayState()); FlxG.sound.playMusic(Paths.music('freakyMenu')); - PlayState.instance.callOnScripts('onGameOverConfirm', [false]); + psychlua.ScriptHandler.callOnScripts('onGameOverConfirm', [false]); } if (boyfriend.animation.curAnim != null) @@ -124,7 +124,7 @@ class GameOverSubstate extends MusicBeatSubstate { Conductor.songPosition = FlxG.sound.music.time; } - PlayState.instance.callOnScripts('onUpdatePost', [elapsed]); + psychlua.ScriptHandler.callOnScripts('onUpdatePost', [elapsed]); } var isEnding:Bool = false; @@ -149,7 +149,7 @@ class GameOverSubstate extends MusicBeatSubstate FlxG.resetState(); }); }); - PlayState.instance.callOnScripts('onGameOverConfirm', [true]); + psychlua.ScriptHandler.callOnScripts('onGameOverConfirm', [true]); } }