Fixed a crash caused by NPC variable references in callfunc

- Fixes bugreport:8074, thanks to ahmadshidqi
  http://hercules.ws/board/tracker/issue-8074-help-crash/
- Also fixed some DBMap allocation mistakes introduced in 4f3156b.
- Added testcases to the self-test script.
- Made possible thanks to Ind.

Signed-off-by: Haru <haru@dotalux.com>