Procedure GetGameInfo(name)
  Return GetMember([Party], name)
End Procedure

Procedure SetGameInfo(name, value)
  Return SetMember([Party], name, value)
End Procedure

Procedure TestFlag(name)
  Return GetMember([Party], name) = True
End Procedure

Procedure SetFlag(name)
  Return SetMember([Party], name, True)
End Procedure

Procedure ClearFlag(name)
  Return SetMember([Party], name, False)
End Procedure

Procedure GetPartyMembers()
  Return [Party].Member 'List of Hero
End Procedure

Procedure AddPartyMember(who)
  Dim we = GetPartyMembers()
  If we.Count > 0 Then
    Dim last = we[we.Count]
    who.Area = last.Area
    who.X = last.X
    who.Y = last.Y
    who.Z = last.Z
    who.Motion = last.Motion
    who.Visible = last.Visible
  End If
  [Party].Member.Add(who)
  who.OrderInClass = [Party].Member.Count
  Dim pm = GetPrimaryActor()
  If GetElements("Hero").Contains(pm) Then
    pm = [Party].Member[1]
    SetPrimaryActor(pm)
    SetGameInfo("Primary", pm)
  End If
End Procedure

Procedure RemovePartyMember(who)
  Dim we = GetPartyMembers()
  Dim index = we.IndexOf(who)
  If index > 0 Then
    For i = index To we.Count - 1
      Dim front = we[i]
      Dim back = we[i + 1]
      back.X = front.X
      back.Y = front.Y
      back.Z = front.Z
      back.Motion = front.Motion
      back.OrderInClass = front.OrderInClass
    Next
    [Party].Member.RemoveAt(index)
    who.Area = Nothing
    who.X = 0
    who.Y = 0
    who.Z = 0
    who.Visible = Nothing
    who.OrderInClass = 0
  End If
  Dim pm = GetPrimaryActor()
  If GetElements("Hero").Contains(pm) Then
    pm = [Party].Member[1]
    SetPrimaryActor(pm)
    SetGameInfo("Primary", pm)
  End If
End Procedure

Procedure ExchangePartyMember(a, b)
  Dim x = a.X
  Dim y = a.Y
  Dim z = a.Z
  Dim m = a.Motion
  Dim oic = a.OrderInClass
  a.X = b.X
  a.Y = b.Y
  a.Z = b.Z
  a.Motion = b.Motion
  a.OrderInClass = b.OrderInClass
  b.X = x
  b.Y = y
  b.Z = z
  b.Motion = m
  b.OrderInClass = oic
  [Party].Member[a.OrderInClass] = a
  [Party].Member[b.OrderInClass] = b
  Dim pm = GetPrimaryActor()
  If GetElements("Hero").Contains(pm) Then
    pm = [Party].Member[1]
    SetPrimaryActor(pm)
    SetGameInfo("Primary", pm)
  End If
End Procedure

Procedure CurePerfectly(who, include_status)
  who.HP = who.MaxHP
  who.MP = who.MaxMP
  If include_status Then who.Status = 0x0
End Procedure

Procedure CureUsPerfectly(include_status)
  Dim we = GetPartyMembers()
  For i = 1 To we.Count
    CurePerfectly(we[i])
  Next
End Procedure

Procedure AnybodyHas(the_tool)
  Dim we = GetPartyMembers()
  For i = 1 To we.Count
    Dim who = we[i]
    If who.ToolList.Contains(the_tool) OrElse _
      who.Weapon = the_tool OrElse _
      who.Armor = the_tool OrElse _
      who.Shield = the_tool OrElse _
      who.Helmet = the_tool Then Return True
  Next
  Return False
End Procedure

Procedure GiveUsTool(the_tool)
  GetPartyMembers()[1].ToolList.Add(the_tool)
  Return True
End Procedure

Procedure TakeOurTool(the_tool)
  Dim we = GetPartyMembers()
  For i = 1 To we.Count
    Dim who = we[i]
    If who.ToolList.Contains(the_tool) Then
      who.ToolList.Remove(the_tool)
      Return True
    End If
    If who.Weapon = the_tool Then
      who.Weapon = Nothing
      Return True
    End If
    If who.Armor = the_tool Then
      who.Armor = Nothing
      Return True
    End If
    If who.Shield = the_tool Then
      who.Shield = Nothing
      Return True
    End If
    If who.Helmet = the_tool Then
      who.Helmet = Nothing
      Return True
    End If
  Next
  Return False
End Procedure

Procedure CheckOurMoney()
  Return [Party].Money
End Procedure

Procedure HaveEnoughMoney(sum)
  Return [Party].Money >= sum
End Procedure

Procedure PayMoney(sum)
  If [Party].Money >= sum Then
    [Party].Money -= sum
    Return True
  Else
    Return False
  End If
End Procedure

Procedure ReceiveMoney(sum)
  IF 1000000 - [Party].Money >= sum Then
    [Party].Money += sum
    Return True
  Else
    [Party].Money = 1000000
    Return False
  End If
End Procedure

Procedure Equip(who)
  who.DamageAP = {0,0,0,0}
  who.DamageDP = {0,0,0,0}
  who.StatusDP = 0
  who.StatusFX = 0
  who.Resistance = 0
  who.Strength = who.BaseStrength
  who.Vitality = who.BaseVitality
  who.Intelligence = who.BaseIntelligence
  who.Agility = who.BaseAgility
  who.Luck = who.BaseLuck
  If who.Weapon <> Nothing Then
    For j = 1 To 4
      who.DamageAP[j] = who.Weapon.DamageAP[j]
    Next
    who.StatusFX = who.Weapon.StatusFX
  End If
  Dim eq_list = CreateList()
  If who.Armor <> Nothing Then eq_list.Add(who.Armor)
  If who.Shield <> Nothing Then eq_list.Add(who.Shield)
  If who.Helmet <> Nothing Then eq_list.Add(who.Helmet)
  For i = 1 To eq_list.Count
    Dim eq = eq_list[i]
    For j = 1 To 4
      who.DamageDP[j] += eq.DamageDP[j]
    Next
    who.StatusDP += eq.StatusDP
    who.Strength += eq.Ability[1]
    who.Vitality += eq.Ability[2]
    who.Intelligence += eq.Ability[3]
    who.Agility += eq.Ability[4]
    who.Luck += eq.Ability[5]
    who.Resistance = who.Resistance Or eq_list[i].Resistance
  Next
End Procedure
