Files
jak-project/goal_src/jak2/engine/entity/actor-link-h.gc
T
water111 637990314b wip: better stack var support (#4222)
Closes #736

---------

Co-authored-by: Hat Kid <6624576+Hat-Kid@users.noreply.github.com>
2026-04-19 00:14:44 +02:00

399 lines
12 KiB
Common Lisp

;;-*-Lisp-*-
(in-package goal)
;; name: actor-link-h.gc
;; name in dgo: actor-link-h
;; dgos: ENGINE, GAME
(define-extern entity-by-name (function string entity))
(define-extern entity-by-type (function type entity-actor))
(define-extern entity-by-aid (function uint entity))
;; DECOMP BEGINS
;; WARN: Return type mismatch entity vs entity-actor.
(defun entity-actor-lookup ((arg0 res-lump) (arg1 symbol) (arg2 int))
"Given an entity (the res-lump), look up a reference to another entity and return that entity."
(let* ((sv-16 (new 'static 'res-tag))
(v1-1 (res-lump-data arg0 arg1 pointer :tag-ptr (& sv-16)))
)
(the-as
entity-actor
(when (and v1-1 (< arg2 (the-as int (-> sv-16 elt-count))))
(if (= (-> sv-16 elt-type) string)
(entity-by-name (the-as string (-> (the-as (pointer uint32) (&+ v1-1 (* arg2 4))))))
(entity-by-aid (-> (the-as (pointer uint32) (&+ v1-1 (* arg2 4)))))
)
)
)
)
)
;; WARN: Check prologue - tricky store of r0
(defun entity-actor-count ((arg0 res-lump) (arg1 symbol))
"Get the number of entities that this res references under the name.
This works on more than just next/prev."
(let ((sv-16 (new 'static 'res-tag)))
(if (res-lump-data arg0 arg1 pointer :tag-ptr (& sv-16))
(the-as int (-> sv-16 elt-count))
0
)
)
)
(deftype actor-link-info (basic)
"A linked list of actors.
Actors allocate this on their process heap if they have a
`next-actor` or `prev-actor` defined in their lump
and use it for entity lookups."
((process process)
(next entity-actor)
(prev entity-actor)
)
(:methods
(new (symbol type process symbol) _type_)
(get-matching-actor-type-mask (_type_ type) int)
(actor-count-before (_type_) int)
(link-to-next-and-prev-actor (_type_) actor-link-info)
(get-next (_type_) entity-actor)
(get-prev (_type_) entity-actor)
(get-next-process (_type_) process)
(get-prev-process (_type_) process)
(apply-function-forward (_type_ (function entity-actor object object) object) int)
(apply-function-reverse (_type_ (function entity-actor object object) object) int)
(apply-all (_type_ (function entity-actor object object) object) int)
(send-to-all (_type_ symbol) none)
(send-to-all-after (_type_ symbol) object)
(send-to-all-before (_type_ symbol) object)
(send-to-next-and-prev (_type_ symbol) none)
(send-to-next (_type_ symbol) none)
(send-to-prev (_type_ symbol) none)
(actor-count (_type_) int)
)
)
(defmethod next-actor ((this entity-actor))
(entity-actor-lookup this 'next-actor 0)
)
(defmethod prev-actor ((this entity-actor))
(entity-actor-lookup this 'prev-actor 0)
)
(defmethod new actor-link-info ((allocation symbol) (type-to-make type) (arg0 process) (arg1 symbol))
"Set up an actor-link-info for the given process.
The entity of this process should be the entity-actor
that will get this actor-link-info."
(let* ((a0-1 (-> arg0 entity))
(s4-0 (entity-actor-lookup a0-1 'next-actor 0))
(a0-2 (-> arg0 entity))
(s3-0 (entity-actor-lookup a0-2 'prev-actor 0))
)
(when (or (not arg1) s4-0 s3-0)
(let ((v0-2 (object-new allocation type-to-make (the-as int (-> type-to-make size)))))
(set! (-> v0-2 process) arg0)
(set! (-> v0-2 next) s4-0)
(set! (-> v0-2 prev) s3-0)
v0-2
)
)
)
)
(defmethod get-next ((this actor-link-info))
(-> this next)
)
(defmethod get-prev ((this actor-link-info))
(-> this prev)
)
;; WARN: Return type mismatch basic vs process.
(defmethod get-next-process ((this actor-link-info))
"Get the process for the next, if it exists."
(the-as process (and (-> this next) (-> this next extra process)))
)
;; WARN: Return type mismatch basic vs process.
(defmethod get-prev-process ((this actor-link-info))
"Get the process for the prev, if it exists."
(the-as process (and (-> this prev) (-> this prev extra process)))
)
(defmethod link-to-next-and-prev-actor ((this actor-link-info))
"Redo the linking in the constructor by looking up the next/prev actor."
(let ((a0-1 (-> this process entity)))
(set! (-> this next) (entity-actor-lookup a0-1 'next-actor 0))
)
(let ((a0-2 (-> this process entity)))
(set! (-> this prev) (entity-actor-lookup a0-2 'prev-actor 0))
)
this
)
(defmethod apply-function-forward ((this actor-link-info) (arg0 (function entity-actor object object)) (arg1 object))
"Iterate forward through actors, and apply this function. Starts at (-> this next)
If the function returns truthy, stop iterating."
(let ((s3-0 (-> this next)))
(while s3-0
(if (arg0 s3-0 arg1)
(return (the-as int #f))
)
(set! s3-0 (entity-actor-lookup s3-0 'next-actor 0))
)
)
0
)
(defmethod apply-function-reverse ((this actor-link-info) (arg0 (function entity-actor object object)) (arg1 object))
"Iterate backward through actors and apply function.
If the function returns truth, stop iterating."
(let ((s3-0 (-> this prev)))
(while s3-0
(if (arg0 s3-0 arg1)
(return (the-as int #f))
)
(set! s3-0 (entity-actor-lookup s3-0 'prev-actor 0))
)
)
0
)
(defmethod apply-all ((this actor-link-info) (arg0 (function entity-actor object object)) (arg1 object))
"Apply to all entities. Starts at the back and hits everyone, including this object."
(let ((s4-0 (-> this process entity)))
(while (let ((a0-2 s4-0))
(entity-actor-lookup a0-2 'prev-actor 0)
)
(set! s4-0 (entity-actor-lookup s4-0 'prev-actor 0))
)
(while s4-0
(if (arg0 s4-0 arg1)
(return (the-as int #f))
)
(let ((a0-4 s4-0))
(set! s4-0 (entity-actor-lookup a0-4 'next-actor 0))
)
)
)
0
)
(defmethod send-to-all-after ((this actor-link-info) (arg0 symbol))
"Send an event to all processes after this link with no parameters."
(with-pp
(let ((s4-0 (-> this next))
(s5-0 (the-as object #f))
)
(while s4-0
(let ((a0-1 (-> s4-0 extra process)))
(when a0-1
(let ((a1-1 (new 'stack-no-clear 'event-message-block)))
(set! (-> a1-1 from) (process->ppointer pp))
(set! (-> a1-1 num-params) 0)
(set! (-> a1-1 message) arg0)
(set! s5-0 (or (send-event-function a0-1 a1-1) s5-0))
)
)
)
(set! s4-0 (entity-actor-lookup s4-0 'next-actor 0))
)
s5-0
)
)
)
(defmethod send-to-all-before ((this actor-link-info) (arg0 symbol))
"Send an event to all processes before this link with no parameters."
(with-pp
(let ((s4-0 (-> this prev))
(s5-0 (the-as object #f))
)
(while s4-0
(let ((a0-1 (-> s4-0 extra process)))
(when a0-1
(let ((a1-1 (new 'stack-no-clear 'event-message-block)))
(set! (-> a1-1 from) (process->ppointer pp))
(set! (-> a1-1 num-params) 0)
(set! (-> a1-1 message) arg0)
(set! s5-0 (or (send-event-function a0-1 a1-1) s5-0))
)
)
)
(set! s4-0 (entity-actor-lookup s4-0 'prev-actor 0))
)
s5-0
)
)
)
;; WARN: Return type mismatch symbol vs none.
(defmethod send-to-next ((this actor-link-info) (arg0 symbol))
"Send event arg0 to the next actor's process."
(let ((a0-1 (-> this next)))
(when a0-1
(let ((a0-2 (-> a0-1 extra process)))
(if a0-2
(send-event a0-2 arg0)
)
)
)
)
(none)
)
;; WARN: Return type mismatch symbol vs none.
(defmethod send-to-prev ((this actor-link-info) (arg0 symbol))
"Send event arg1 to the next actor's process."
(let ((a0-1 (-> this prev)))
(when a0-1
(let ((a0-2 (-> a0-1 extra process)))
(if a0-2
(send-event a0-2 arg0)
)
)
)
)
(none)
)
;; WARN: Return type mismatch symbol vs none.
(defmethod send-to-next-and-prev ((this actor-link-info) (arg0 symbol))
"Send an event to both next and prev with no params."
(send-to-next this arg0)
(send-to-prev this arg0)
(none)
)
;; WARN: Return type mismatch symbol vs none.
(defmethod send-to-all ((this actor-link-info) (arg0 symbol))
(send-to-all-after this arg0)
(send-to-all-before this arg0)
(none)
)
(defmethod actor-count ((this actor-link-info))
"Count the number of actors in the entire list."
(let ((s5-0 (-> this process entity))
(gp-0 0)
)
(while (let ((a0-2 s5-0))
(entity-actor-lookup a0-2 'prev-actor 0)
)
(set! s5-0 (entity-actor-lookup s5-0 'prev-actor 0))
)
(while s5-0
(+! gp-0 1)
(let ((a0-3 s5-0))
(set! s5-0 (entity-actor-lookup a0-3 'next-actor 0))
)
)
gp-0
)
)
(defmethod get-matching-actor-type-mask ((this actor-link-info) (arg0 type))
"Iterate through _all_ actors that are part of this actor list.
If the nth actor is type matching-type, then set the nth bit of the result."
(let ((s3-0 (-> this process entity))
(s5-0 0)
)
(let ((s4-0 1))
(while (let ((a0-2 s3-0))
(entity-actor-lookup a0-2 'prev-actor 0)
)
(set! s3-0 (entity-actor-lookup s3-0 'prev-actor 0))
)
(while s3-0
(if (= (-> s3-0 etype) arg0)
(set! s5-0 (logior s5-0 s4-0))
)
(let ((a0-3 s3-0))
(set! s3-0 (entity-actor-lookup a0-3 'next-actor 0))
)
(set! s4-0 (* s4-0 2))
)
)
s5-0
)
)
(defmethod actor-count-before ((this actor-link-info))
"Get the number of actors before this actor in the list."
(let* ((s5-0 (-> this process entity))
(s4-0 s5-0)
(gp-0 0)
)
(while (let ((a0-2 s4-0))
(entity-actor-lookup a0-2 'prev-actor 0)
)
(set! s4-0 (entity-actor-lookup s4-0 'prev-actor 0))
)
(while (!= s4-0 s5-0)
(+! gp-0 1)
(let ((a0-3 s4-0))
(set! s4-0 (entity-actor-lookup a0-3 'next-actor 0))
)
)
gp-0
)
)
(defun actor-link-subtask-complete-hook ((arg0 entity-actor) (arg1 (pointer symbol)))
"Sets arg1 if the thing is complete. Does not continue the apply if the complete perm is set."
(cond
((logtest? (-> arg0 extra perm status) (entity-perm-status subtask-complete))
(set! (-> arg1 0) #t)
#f
)
(else
(set! (-> arg1 0) #f)
#t
)
)
)
(defun actor-link-subtask-incomplete-count-hook ((arg0 entity-actor) (arg1 (pointer uint64)))
(cond
((logtest? (-> arg0 extra perm status) (entity-perm-status subtask-complete))
#f
)
(else
(+! (-> arg1 0) 1)
#f
)
)
)
(defun actor-link-dead-hook ((arg0 entity-actor) (arg1 (pointer symbol)))
"Sets arg1 if the thing is dead. Does not continue the apply if the dead perm is set."
(cond
((logtest? (-> arg0 extra perm status) (entity-perm-status dead))
(set! (-> arg1 0) #t)
#f
)
(else
(set! (-> arg1 0) #f)
#t
)
)
)
(defun alt-actor-list-subtask-incomplete-count ((arg0 process-drawable))
"Get the number of alt-actors which do not have the complete bit set in their perm."
(let ((s4-0 (entity-actor-count (-> arg0 entity) 'alt-actor))
(gp-0 0)
)
(dotimes (s3-0 s4-0)
(let ((a0-3 (entity-actor-lookup (-> arg0 entity) 'alt-actor s3-0)))
(if (or (not a0-3) (not (logtest? (-> a0-3 extra perm status) (entity-perm-status subtask-complete))))
(+! gp-0 1)
)
)
)
gp-0
)
)