From 2864134d79c5a176e570b54fe58974a3bec87eff Mon Sep 17 00:00:00 2001
From: remoitnane <remoit(DOT)nane(AT)gmail(DOT)com>
Date: Wed, 8 Sep 2010 01:37:20 -0700
Subject: Simplify party invitation logic and clean up comments

This eliminates part of the client's burden to manage the ID of the
player sending the invitation. This also introduces a new response
value (3) for the invitation acknowledgement packet (0x00fd) to
indicate the party is full.

Resolves: TMW Mantis #676
---
 src/map/clif.c  | 28 ++++++++++++++++------
 src/map/party.c | 72 +++++++++++++++++++++++++++++++++++++++------------------
 2 files changed, 71 insertions(+), 29 deletions(-)

diff --git a/src/map/clif.c b/src/map/clif.c
index 81a57f1..378f076 100644
--- a/src/map/clif.c
+++ b/src/map/clif.c
@@ -5267,7 +5267,9 @@ int clif_party_info (struct party *p, int fd)
 }
 
 /*==========================================
- * �p�[�e�B���U
+ * Relay a party invitation.
+ *
+ * (S 00fe <sender_ID>.l <party_name>.24B)
  *------------------------------------------
  */
 int clif_party_invite (struct map_session_data *sd,
@@ -5281,7 +5283,7 @@ int clif_party_invite (struct map_session_data *sd,
 
     fd = tsd->fd;
 
-    if ((p = party_search (sd->status.party_id)) == NULL)
+    if (!(p = party_search (sd->status.party_id)))
         return 0;
 
     WFIFOW (fd, 0) = 0xfe;
@@ -5292,7 +5294,16 @@ int clif_party_invite (struct map_session_data *sd,
 }
 
 /*==========================================
- * �p�[�e�B���U����
+ * Relay the response to a party invitation.
+ *
+ * (R 00fd <name>.24B <flag>.B)
+ *
+ * flag:
+ *  0 The character is already in a party.
+ *  1 The invitation was rejected.
+ *  2 The invitation was accepted.
+ *  3 The party is full.
+ *  4 The character is in the same party.
  *------------------------------------------
  */
 int clif_party_inviteack (struct map_session_data *sd, char *nick, int flag)
@@ -8442,17 +8453,20 @@ void clif_parse_CreateParty2 (int fd, struct map_session_data *sd)
 }
 
 /*==========================================
- * �p�[�e�B�Ɋ��U
+ * Process invitation to join a party.
+ *
+ * (S 00fc <account_ID>.l)
  *------------------------------------------
  */
 void clif_parse_PartyInvite (int fd, struct map_session_data *sd)
 {
-    printf ("Party Invite!\n");
     party_invite (sd, RFIFOL (fd, 2));
 }
 
 /*==========================================
- * �p�[�e�B���U�ԓ�
+ * Process reply to party invitation.
+ *
+ * (S 00ff <account_ID>.l <flag>.l)
  *------------------------------------------
  */
 void clif_parse_ReplyPartyInvite (int fd, struct map_session_data *sd)
@@ -8464,7 +8478,7 @@ void clif_parse_ReplyPartyInvite (int fd, struct map_session_data *sd)
     }
     else
     {
-        party_reply_invite (sd, RFIFOL (fd, 2), -1);
+        party_reply_invite (sd, RFIFOL (fd, 2), 0);
         clif_skill_fail (sd, 1, 0, 4);
     }
 }
diff --git a/src/map/party.c b/src/map/party.c
index 8ddfca2..a6e9f24 100644
--- a/src/map/party.c
+++ b/src/map/party.c
@@ -219,45 +219,63 @@ int party_recv_info (struct party *sp)
     return 0;
 }
 
-// �p�[�e�B�ւ̊��U
+/* Process party invitation from sd to account_id. */
 int party_invite (struct map_session_data *sd, int account_id)
 {
     struct map_session_data *tsd = map_id2sd (account_id);
     struct party *p = party_search (sd->status.party_id);
     int  i;
+    int  full = 1; /* Indicates whether or not there's room for one more. */
 
     nullpo_retr (0, sd);
 
-    if (tsd == NULL || p == NULL)
+    if (!tsd || !p || !tsd->fd)
         return 0;
 
-    printf ("\tA\n");
-
     if (!battle_config.invite_request_check)
     {
+        /* Disallow the invitation under these conditions. */
         if (tsd->guild_invite > 0 || tsd->trade_partner || tsd->npc_id
             || tsd->npc_shopid || pc_checkskill (tsd, NV_PARTY) < 1)
         {
-            clif_party_inviteack (sd, tsd->status.name, 0);
+            clif_party_inviteack (sd, tsd->status.name, 1);
             return 0;
         }
     }
-    printf ("\tB\n");
+
+    /* The target player is already in a party, or has a pending invitation. */
     if (tsd->status.party_id > 0 || tsd->party_invite > 0)
-    {                           // ����̏����m�F
+    {
         clif_party_inviteack (sd, tsd->status.name, 0);
         return 0;
     }
-    printf ("\tC\n");
+
     for (i = 0; i < MAX_PARTY; i++)
-    {                           // ���A�J�E���g�m�F
+    {
+        /*
+         * A character from the target account is already in the same party.
+         * The response isn't strictly accurate, as they're separate
+         * characters, but we're making do with what was already in place and
+         * leaving this (mostly) alone for now.
+         */
         if (p->member[i].account_id == account_id)
         {
-            clif_party_inviteack (sd, tsd->status.name, 0);
+            clif_party_inviteack (sd, tsd->status.name, 1);
             return 0;
         }
+
+        if (!p->member[i].account_id)
+            full = 0;
     }
-    printf ("\tD\n");
+
+    /* There isn't enough room for a new member. */
+    if (full)
+    {
+        clif_party_inviteack (sd, tsd->status.name, 3);
+        return 0;
+    }
+
+    /* Otherwise, relay the invitation to the target player. */
     tsd->party_invite = sd->status.party_id;
     tsd->party_invite_account = sd->status.account_id;
 
@@ -265,26 +283,36 @@ int party_invite (struct map_session_data *sd, int account_id)
     return 0;
 }
 
-// �p�[�e�B���U�ւ̕ԓ�
+/* Process response to party invitation. */
 int party_reply_invite (struct map_session_data *sd, int account_id, int flag)
 {
-    struct map_session_data *tsd = map_id2sd (account_id);
-
     nullpo_retr (0, sd);
 
+    /* There is no pending invitation. */
+    if (!sd->party_invite || !sd->party_invite_account)
+        return 0;
+
+    /*
+     * Only one invitation can be pending, so these have to be the same. Since
+     * the client continues to send the wrong ID, and it's already managed on
+     * this side of things, the sent ID is being ignored.
+     */
+    account_id = sd->party_invite_account;
+
+    /* The invitation was accepted. */
     if (flag == 1)
-    {                           // ����
-        //inter�I�֒lj��v��
         intif_party_addmember (sd->party_invite, sd->status.account_id);
-        return 0;
-    }
+    /* The invitation was rejected. */
     else
-    {                           // ����
+    {
+        /* This is the player who sent the invitation. */
+        struct map_session_data *tsd = NULL;
+
         sd->party_invite = 0;
         sd->party_invite_account = 0;
-        if (tsd == NULL)
-            return 0;
-        clif_party_inviteack (tsd, sd->status.name, 1);
+
+        if ((tsd = map_id2sd (account_id)))
+            clif_party_inviteack (tsd, sd->status.name, 1);
     }
     return 0;
 }
-- 
cgit v1.2.3-70-g09d2