Last active
April 1, 2021 01:52
-
-
Save gesslar/dace8ee44e73bd5756205ee8444d2685 to your computer and use it in GitHub Desktop.
An override for efun::environment with arguments to travel up the inventory of a given object to find all environments in order upwards
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Returns the environment of an object, or all the environments above an | |
* object, or a single environment at a given position. | |
* | |
* Example: | |
* backpack | |
* -> pouch | |
* -> bean | |
* | |
* environment( bean ) ; // returns pouch | |
* environment( bean, 0 ) ; // returns pouch | |
* environment( bean, 1 ) ; // returns backpack | |
* environment( bean, -1 ) ; // returns ({ pouch, backpack }) | |
* environment( bean, 902839) ; // returns 0 | |
* | |
* if called from within the bean object, argument 1 is unnecessary | |
* environment() ; // returns pouch | |
* environment( 0 ) ; // returns pouch | |
* environment( 1 ) ; // returns backpack | |
* environment( -1 ) ; // returns ({ pouch, backpack }) | |
* environment( 902839 ) ; // returns 0 | |
*/ | |
varargs mixed environment( mixed arg1, int arg2 ) | |
{ | |
int depth ; | |
object ob ; | |
if( nullp( arg1 ) ) | |
{ | |
ob = previous_object() ; | |
depth = 1; | |
} | |
else if( objectp( arg1 ) ) | |
{ | |
ob = arg1 ; | |
depth = 1 ; | |
} | |
else if( intp( arg1 ) ) | |
{ | |
ob = previous_object() ; | |
depth = arg1 + 1 ; | |
} | |
if( !nullp( arg2 ) ) | |
{ | |
if( intp( arg2 ) ) | |
{ | |
if( intp( arg1 ) ) | |
{ | |
error("environment: Error in argument 2") ; | |
} | |
depth = arg2 + 1 ; | |
} | |
} | |
if( depth == 0 ) // retrieve all | |
{ | |
object env, *envs = ({ }) ; | |
while( env = efun::environment( ob ) ) | |
{ | |
envs = ({ envs..., env }) ; | |
ob = env ; | |
} | |
return envs ; | |
} | |
else | |
{ | |
object env ; | |
while( depth-- && env = efun::environment( ob ) ) | |
{ | |
ob = env ; | |
} | |
return env ; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment