void CreateADecodedMap(object oPC, int iMapLevel);
void CreateMapCopy(object oPC, object oMap);

void main()
{
  object oPC = OBJECT_SELF;
  object oMapUsed = GetLocalObject(oPC,"oMapUsed");
  DeleteLocalObject(oPC,"oMapUsed");

  int iMapSkill = GetCampaignInt("UOACraft","iMapSkill",oPC);
  int iMapChance = iMapSkill;

  if (iMapChance <350)
   {
    iMapChance = GetAbilityScore(oPC,ABILITY_INTELLIGENCE)*5;
    iMapChance = iMapChance +(GetAbilityScore(oPC,ABILITY_WISDOM)*3);
    iMapChance = iMapChance +(GetAbilityScore(oPC,ABILITY_DEXTERITY)*2);
    iMapChance = iMapChance * 3;
    if (iMapChance >350) iMapChance = 350;
    if (iMapSkill > iMapChance) iMapChance = iMapSkill;
   }

  int iMapLevel = StringToInt(GetStringRight(GetTag(oMapUsed),2));
  if (iMapLevel==0) iMapLevel=10;

  int iMapChanceNeeded = iMapLevel*100;


  AssignCommand(oPC,ActionPlayAnimation(ANIMATION_FIREFORGET_READ,0.5,5.0));
  AssignCommand(oPC,PlaySound("as_na_grassmove3"));
  AssignCommand(oPC,DelayCommand(6.0,PlaySound("as_na_grassmove1")));
  AssignCommand(oPC,DelayCommand(5.0,ActionPlayAnimation(ANIMATION_FIREFORGET_READ,0.5,4.0)));
  FloatingTextStringOnCreature("You begin decoding the map.",oPC,FALSE);

  if (iMapChance<iMapChanceNeeded)
   {
    DelayCommand(9.5,FloatingTextStringOnCreature("You have no idea how to decode this map..",oPC,FALSE));
    return;
   }

  DestroyObject(oMapUsed,9.0);
  DelayCommand(10.0,CreateADecodedMap(oPC,iMapLevel));
}

void CreateADecodedMap(object oPC, int iMapLevel)
 {
  string sResource;
  switch (iMapLevel)
   {
    case 2:{sResource="_uoa_tmap_lvl002";break;}
    case 3:{sResource="_uoa_tmap_lvl003";break;}
    case 4:{sResource="_uoa_tmap_lvl004";break;}
    case 5:{sResource="_uoa_tmap_lvl005";break;}
    case 6:{sResource="_uoa_tmap_lvl006";break;}
    case 7:{sResource="_uoa_tmap_lvl007";break;}
    case 8:{sResource="_uoa_tmap_lvl008";break;}
    case 9:{sResource="_uoa_tmap_lvl009";break;}
    case 10:{sResource="_uoa_tmap_lvl010";break;}
    default:{sResource="_uoa_tmap_lvl001";break;}
   }

  object oMap = CreateItemOnObject(sResource,oPC,1);
  DelayCommand(1.0,CreateMapCopy(oPC,oMap));
  return;
 }

void CreateMapCopy(object oPC, object oMap)
 {
  //In this segment of code you need to set
  //exactly how many treasure chest waypoints there are
  //in your module.
  int iMaxLocations = 1;
  //You must set this variable or else the maps will
  //all point to the same location.

  int iMapLocation = 1;
  if (iMaxLocations>1) iMapLocation = Random(iMaxLocations)+1;

  string sNewTag = "_UOA_TMAP_"+IntToString(iMapLocation);
  CopyObject(oMap,GetLocation(oPC),oPC,sNewTag);
  DestroyObject(oMap,2.0);
  DelayCommand(1.0,FloatingTextStringOnCreature("You carefully decode the treasure map, noting the location of the buried treasure.",oPC,FALSE));
  return;
 }