Handle error checking improvements in the API layer

API calls now check the reference count of handles passed to them,
rejecting dead handles. Although these handles are technically
valid, they're off-limits for the API, as Bad Things(TM) will
happen on the engine side if we keep talking about the dead.

a2_TypeOf() now returns a proper negated error code instead of
-1 in case of failure.