ID:195023
 
//Title: isDirectChild
//Credit to: Darkcampainger
//Contributed by: DarkCampainger

// Returns if childType is a direct child of parentType
proc/isDirectChild(childType, parentType)
// Check if the child is derived form the parent at all
if(!ispath(childType,parentType))
return 0
// Check that the types are not equivalent
if(childType == parentType)
return 0
// If it's the direct descendant, there should only be one more / in its type
return !findtext("[childType]","/",length("[parentType]")+2)

// Example output:
// isDirectChild(/obj/item/knife, /obj/item) = 1
// isDirectChild(/obj/item/knife/steel, /obj/item) = 0
// isDirectChild(/obj/button, /obj/item) = 0
Here's my version:

proc/isDirectChild(child,parent)
ASSERT(ispath(child)&&ispath(parent))
if((child!=parent)&&ispath(child,parent))
var/list/types=typesof(parent)
types-=parent
types-=typesof(child)
var/type
while(types.len)
type=types[types.len]
if(ispath(child,type))
return 0
types-=typesof(type)
return 1
return 0

// Example output:
// isDirectChild(/atom,/atom) = 0
// isDirectChild(/turf,/atom) = 1
// isDirectChild(/obj,/atom) = 0
// isDirectChild(/obj,/atom/movable) = 1
In response to Jemai1
Jemai1 wrote:
//   isDirectChild(/obj,/atom/movable) = 1


Ah, good point. I hadn't considered types with altered parent_types.