Skip to content

Commit

Permalink
Fix script object events.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Mar 18, 2023
1 parent b4b8b75 commit 51d6cae
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 78 deletions.
80 changes: 5 additions & 75 deletions src/script_noxscript_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
"image"

"github.com/noxworld-dev/noxscript/ns/asm"
"github.com/noxworld-dev/noxscript/ns/v4"
"github.com/noxworld-dev/opennox-lib/object"
ns4 "github.com/noxworld-dev/noxscript/ns/v4"

"github.com/noxworld-dev/opennox/v1/legacy"
"github.com/noxworld-dev/opennox/v1/server"
Expand Down Expand Up @@ -87,41 +86,6 @@ var noxScriptBuiltins = [asm.BuiltinGetScore + 1]noxscript.Builtin{
asm.BuiltinSecondTimerWithArg: nsSecondTimerArg,
asm.BuiltinFrameTimerWithArg: nsFrameTimerArg,
asm.BuiltinCancelTimer: nsCancelTimer,
asm.BuiltinGetTrigger: nsGetTrigger,
asm.BuiltinGetCaller: nsGetCaller,
asm.BuiltinSetCallback: nsSetCallback,
asm.BuiltinIsTrigger: nsIsTrigger,
asm.BuiltinIsCaller: nsIsCaller,
}

func nsGetTrigger(vm noxscript.VM) int {
vm.PushHandleNS(vm.NoxScript().GetTrigger())
return 0
}

func nsGetCaller(vm noxscript.VM) int {
vm.PushHandleNS(vm.NoxScript().GetCaller())
return 0
}

func nsIsTrigger(vm noxscript.VM) int {
obj := vm.PopObjectNS()
if obj == nil {
vm.PushBool(false)
return 0
}
vm.PushBool(vm.NoxScript().IsTrigger(obj))
return 0
}

func nsIsCaller(vm noxscript.VM) int {
obj := vm.PopObjectNS()
if obj == nil {
vm.PushBool(false)
return 0
}
vm.PushBool(vm.NoxScript().IsCaller(obj))
return 0
}

func nsSecondTimer(vm noxscript.VM) int {
Expand Down Expand Up @@ -160,7 +124,7 @@ func nsFrameTimerArg(vm noxscript.VM) int {

// TODO: migrate all usage of `nox_server_scriptExecuteFnForEachGroupObj_502670` to use these funcs below.

func eachObjectNS(s *Server, g *server.MapGroup, fnc func(obj ns.Obj) bool) {
func eachObjectNS(s *Server, g *server.MapGroup, fnc func(obj ns4.Obj) bool) {
if g == nil {
return
}
Expand All @@ -176,7 +140,7 @@ func eachObjectNS(s *Server, g *server.MapGroup, fnc func(obj ns.Obj) bool) {
}
}

func eachObjectRecursiveNS(s *Server, g *server.MapGroup, fnc func(obj ns.Obj) bool) bool { // nox_server_scriptExecuteFnForEachGroupObj_502670
func eachObjectRecursiveNS(s *Server, g *server.MapGroup, fnc func(obj ns4.Obj) bool) bool { // nox_server_scriptExecuteFnForEachGroupObj_502670
if g == nil {
return true // just skip this group
}
Expand All @@ -199,7 +163,7 @@ func eachObjectRecursiveNS(s *Server, g *server.MapGroup, fnc func(obj ns.Obj) b
return true
}

func eachWaypointRecursive(s *Server, g *server.MapGroup, fnc func(wp ns.WaypointObj) bool) bool {
func eachWaypointRecursive(s *Server, g *server.MapGroup, fnc func(wp ns4.WaypointObj) bool) bool {
if g == nil {
return true
}
Expand All @@ -222,7 +186,7 @@ func eachWaypointRecursive(s *Server, g *server.MapGroup, fnc func(wp ns.Waypoin
return true
}

func eachWallRecursive(s *Server, g *server.MapGroup, fnc func(w ns.WallObj) bool) bool {
func eachWallRecursive(s *Server, g *server.MapGroup, fnc func(w ns4.WallObj) bool) bool {
if g == nil {
return true
}
Expand Down Expand Up @@ -253,40 +217,6 @@ func nsCancelTimer(vm noxscript.VM) int {
return 0
}

func nsSetCallback(vm noxscript.VM) int {
s := vm.(*noxScript)
fnc := int32(s.PopU32())
ev := ns.ObjectEvent(s.PopU32())
u := asObjectS(s.PopObject())
if u == nil || !u.Class().Has(object.ClassMonster) {
return 0
}
ud := u.UpdateDataMonster()
switch ev {
case ns.EventEnemySighted: // Enemy sighted
ud.ScriptEnemySighted.Func = fnc
case ns.EventLookingForEnemy: // Looking for enemy
ud.ScriptLookingForEnemy.Func = fnc
case ns.EventDeath: // Death
ud.ScriptDeath.Func = fnc
case ns.EventChangeFocus: // Change focus
ud.ScriptChangeFocus.Func = fnc
case ns.EventIsHit: // Is hit
ud.ScriptIsHit.Func = fnc
case ns.EventRetreat: // Retreat
ud.ScriptRetreat.Func = fnc
case ns.EventCollision: // Collision
ud.ScriptCollision.Func = fnc
case ns.EventEnemyHeard: // Enemy heard
ud.ScriptHearEnemy.Func = fnc
case ns.EventEndOfWaypoint: // End of waypoint
ud.ScriptEndOfWaypoint.Func = fnc
case ns.EventLostEnemy: // Lost sight of enemy
ud.ScriptLostEnemy.Func = fnc
}
return 0
}

func sliceEqual(a, b []uint32) bool {
if len(a) != len(b) {
return false
Expand Down
31 changes: 28 additions & 3 deletions src/script_ns_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,9 +768,34 @@ func (obj nsObj) ResumeLevel(percent float32) {
obj.Object.SetRegroupLevel(percent)
}

func (obj nsObj) OnEvent(event ns4.ObjectEvent, fnc ns4.Func) {
//TODO implement me
panic("implement me")
func (obj nsObj) OnEvent(ev ns4.ObjectEvent, fnc ns4.Func) {
if !obj.Class().Has(object.ClassMonster) {
return
}
ind := int32(obj.s.noxScript.AsFuncIndex(fnc))
ud := obj.UpdateDataMonster()
switch ev {
case ns4.EventEnemySighted: // Enemy sighted
ud.ScriptEnemySighted.Func = ind
case ns4.EventLookingForEnemy: // Looking for enemy
ud.ScriptLookingForEnemy.Func = ind
case ns4.EventDeath: // Death
ud.ScriptDeath.Func = ind
case ns4.EventChangeFocus: // Change focus
ud.ScriptChangeFocus.Func = ind
case ns4.EventIsHit: // Is hit
ud.ScriptIsHit.Func = ind
case ns4.EventRetreat: // Retreat
ud.ScriptRetreat.Func = ind
case ns4.EventCollision: // Collision
ud.ScriptCollision.Func = ind
case ns4.EventEnemyHeard: // Enemy heard
ud.ScriptHearEnemy.Func = ind
case ns4.EventEndOfWaypoint: // End of waypoint
ud.ScriptEndOfWaypoint.Func = ind
case ns4.EventLostEnemy: // Lost sight of enemy
ud.ScriptLostEnemy.Func = ind
}
}

type nsObjGroup struct {
Expand Down
45 changes: 45 additions & 0 deletions src/server/noxscript/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
)

func init() {
Register(asm.BuiltinGetTrigger, nsGetTrigger)
Register(asm.BuiltinGetCaller, nsGetCaller)
Register(asm.BuiltinIsTrigger, nsIsTrigger)
Register(asm.BuiltinIsCaller, nsIsCaller)
Register(asm.BuiltinObject, nsObject)
Register(asm.BuiltinCreateObject, nsCreateObject)
Register(asm.BuiltinGetObjectX, nsObjectX)
Expand Down Expand Up @@ -85,6 +89,37 @@ func init() {
Register(asm.BuiltinRunAway, nsRunAway)
Register(asm.BuiltinCreatureGuard, nsGuard)
Register(asm.BuiltinPauseObject, nsPause)
Register(asm.BuiltinSetCallback, nsSetCallback)
}

func nsGetTrigger(vm VM) int {
vm.PushHandleNS(vm.NoxScript().GetTrigger())
return 0
}

func nsGetCaller(vm VM) int {
vm.PushHandleNS(vm.NoxScript().GetCaller())
return 0
}

func nsIsTrigger(vm VM) int {
obj := vm.PopObjectNS()
if obj == nil {
vm.PushBool(false)
return 0
}
vm.PushBool(vm.NoxScript().IsTrigger(obj))
return 0
}

func nsIsCaller(vm VM) int {
obj := vm.PopObjectNS()
if obj == nil {
vm.PushBool(false)
return 0
}
vm.PushBool(vm.NoxScript().IsCaller(obj))
return 0
}

func nsObject(vm VM) int {
Expand Down Expand Up @@ -759,3 +794,13 @@ func nsPause(vm VM) int {
}
return 0
}

func nsSetCallback(vm VM) int {
fnc := int32(vm.PopU32())
ev := ns4.ObjectEvent(vm.PopU32())
obj := vm.PopObjectNS()
if obj != nil {
obj.OnEvent(ev, int(fnc))
}
return 0
}

0 comments on commit 51d6cae

Please sign in to comment.