From cbce9ab414fb128e9ff8af10e45996ec741b1c4e Mon Sep 17 00:00:00 2001
From: eathenabot <eathenabot@54d463be-8e91-2dee-dedb-b68131a5f0ec>
Date: Sat, 31 Mar 2012 22:52:58 +0000
Subject: [PATCH] * Merged changes up to eAthena 15085.

git-svn-id: https://svn.code.sf.net/p/rathena/svn/trunk@15819 54d463be-8e91-2dee-dedb-b68131a5f0ec
---
 doc/script_commands.txt | 24 +++++++++++++-----------
 src/map/script.c        | 25 ++++++++++++++++++-------
 2 files changed, 31 insertions(+), 18 deletions(-)

diff --git a/doc/script_commands.txt b/doc/script_commands.txt
index 4292017cac..aa5e0441e2 100644
--- a/doc/script_commands.txt
+++ b/doc/script_commands.txt
@@ -2027,17 +2027,6 @@ Whatever it returns is determined by type.
 
 ---------------------------------------
 
-*getnpcid(<type>)
-
-This function will return the GID of an NPC.
-Type can be:
-
- 0 - The NPC that the running script is attached to.
-
-Useful for making an NPC perform an action using script commands that require a GID (e.g. unit*)
-
----------------------------------------
-
 *getarraysize(<array name>)
 
 This function returns the number of values that are contained inside the 
@@ -2141,6 +2130,19 @@ if( getcharid(2) == 0 ) mes "Only members of a guild are allowed here!";
 
 ---------------------------------------
 
+*getnpcid(<type>{,"<npc name>"});
+
+Retrieves IDs of the currently invoked NPC. If a unique npc name is
+given, IDs of that NPC are retrieved instead. Type specifies what ID
+to retrieve and can be one of the following:
+
+    0 - Unit ID (GID)
+
+If an invalid type is given or the NPC does not exist, return value
+is 0.
+
+---------------------------------------
+
 *getchildid()
 *getmotherid()
 *getfatherid()
diff --git a/src/map/script.c b/src/map/script.c
index 6504c41cbe..53a8e0fd9a 100644
--- a/src/map/script.c
+++ b/src/map/script.c
@@ -6358,18 +6358,29 @@ BUILDIN_FUNC(getcharid)
  *------------------------------------------*/
 BUILDIN_FUNC(getnpcid)
 {
-	int num;
-	
-	switch (num = script_getnum(st,2)) {
+	int num = script_getnum(st,2);
+	struct npc_data* nd;
+
+	if( script_hasdata(st,3) )
+	{// unique npc name
+		if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
+		{
+			ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
+			script_pushint(st,0);
+			return 1;
+		}
+	}
+
+	switch (num) {
 		case 0:
-			script_pushint(st,st->oid);
+			script_pushint(st,nd ? nd->bl.id : st->oid);
 			break;
 		default:
 			ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
 			script_pushint(st,0);
-			break;
+			return 1;
 	}
-	
+
 	return 0;
 }
 /*==========================================
@@ -15932,7 +15943,7 @@ struct script_function buildin_func[] = {
 	BUILDIN_DEF(checkweight,"vi"),
 	BUILDIN_DEF(readparam,"i?"),
 	BUILDIN_DEF(getcharid,"i?"),
-	BUILDIN_DEF(getnpcid,"i"),
+	BUILDIN_DEF(getnpcid,"i?"),
 	BUILDIN_DEF(getpartyname,"i"),
 	BUILDIN_DEF(getpartymember,"i?"),
 	BUILDIN_DEF(getpartyleader,"i?"),