logo logo

 Back to main page

The NWNX Community Forum

 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
Database Conversion - The Scripting Side

 
Post new topic   Reply to topic    nwnx.org Forum Index -> Database related
View previous topic :: View next topic  
Author Message
Baaleos



Joined: 02 Sep 2007
Posts: 830

PostPosted: Wed Apr 30, 2008 12:26    Post subject: Database Conversion - The Scripting Side Reply with quote

I currently run a World of Rhun server, and I have implimented NWNX with Simtools (by Funkyswerve)

I was wondering if anyone could give me the low down on how to convert the scripting methods from a bioware db using script, to a aps mysql type set.


Specifically, the bioware db currently being used is for the artifact system that accompanys the Rhun mod. (It needs to be able to store objects)

From what I know, the SetPersistentObject() function that comes with aps merely hooks the current StoreCampaignObject() function that is already being used.

Is there any point me converting the system over? Since a SetPersistentObject() would be doing exactly the same as a StoreCampaignObject() call?

Does the Hooking increase system performance, or db reliability?
From what I understand, it would add an extra database dependancy for this functionality.
Eg - Query the nwnx db, then query the bioware db based on the results from the nwnx db - Opposed to the simpler query the bioware db.

Can somone advise?

I have no doubt that nwnx is definitly the way to go for Persistent ints, and strings and such, its just objects that I am concerned about, since I dont think these objects are actually stored in sql, but rather in the bioware db still.
Back to top
View user's profile Send private message
FunkySwerve



Joined: 02 Jun 2005
Posts: 377

PostPosted: Thu May 01, 2008 6:18    Post subject: Reply with quote

They're stored in text format in the datbase, the bioware database is completely univolved, and waaaaaaaay slower. You should definitely use it.

As far as storing things in the database goes, what you need to understand is how the variable is associated with the object. With the bioware database, you set it on an object. The nwnx function also takes an object to store the variable (even if the var is another object), but the association simply puts two values into two database columns. For pcs, it inputs pc name and playername, for object it just inputs the tag. Other than that, all you have to worry about the variable name and value. This means using the vars is very similar, with one exception - there is no Campaign Name field in the Persistent int. If you want, you can easily fold the campaign name and variable name into the persistent int's variable name, which is the very simplest way to go about coverting. If you want an example, or that's at all unclear, just post one of your scripts and I'll convert it for you.

Funky
Back to top
View user's profile Send private message
Baaleos



Joined: 02 Sep 2007
Posts: 830

PostPosted: Thu May 01, 2008 9:03    Post subject: Hehe Reply with quote

Lol, no matter where I post, it always seems to be you giving me a helping hand Funky. Hehe.

Anyhow, Im gonna attach the script inc that comes with Rhun, it was writen by Shayan, and was released few years back, Im sure Shay doesnt mind it being posted, since its freely available on nwnvault.

The comment at the begining of the code regarding the system being unreliable is Shayan's own comment, I believe the reason he believed it to be unreliable was because of its dependency on Bioware db. Ordinarily bioware db seem to work well.... but when you are dealing with a Rhun mod, or a mod that is 100+mb in size, it does seem to suffer. I've found that the db takes sometimes an hour or two to actually update.

(Eg - Player finding an artifact, me querying the db via dm wand, and it takes an hour before the Artifact Log actually registers as <playername> having acquired the artifact.)

If you could give any suggestions on where to start in re-writing this system, or even any suggestions on how to improve the system outside of nwnx would be great.

Currently this script is tied in with onspawn scripts and ondeath scripts. As it uses the nwn hotu treasure chest method. Low,Med,High,Unique and "Artifact" chests. The death or spawn scripts determine the probability of each treasure type spawning.

I think if this inc file were to be re-writen in such a way that it preserved the function definition layouts that it could mean very little else has to be changed in the mod. Eg - Preserve the functions, just change whats inside them.

Code:
//This is a very out-dated database system for storing artifacts. It's best that you not use it
//as it is somewhat unreliable.

const int ENABLE_ARTIFACT_SYSTEM = FALSE;
string ARTIFACT_DATABASE = "ARTIFACT_DB";
string ARTIFACT_TAG = "RHUN_ARTIFACT";
string ARTIFACT_COUNT = "ARTIFACT_COUNT";
string ARTIFACT_CHEST_TAG = "ARTIFACT_CHEST";
string ARTIFACT_DETECT_DB = "ARTIFACT_DETECT_DB";
void WriteToArtifactLogDB(object oItem, object oCreature, int iCategory);
object GetArtifactChest()
{
   object oChest = GetObjectByTag(ARTIFACT_CHEST_TAG);
   return oChest;
}
int GetIsArtifact(object oItem)
{
   string sTag = GetTag(oItem);
   if(FindSubString(sTag, ARTIFACT_TAG) >= 0)
   {
      return TRUE;
   }
   return FALSE;
}
int ArtifactCount()
{
  return GetCampaignInt(ARTIFACT_DATABASE, ARTIFACT_COUNT);
}
void SetArtifactCount(int iCount)
{
   SetCampaignInt(ARTIFACT_DATABASE, ARTIFACT_COUNT, iCount);
}
void DecreaseArtifactCount()
{
  int iCount = ArtifactCount();
  iCount--;
  SetArtifactCount(iCount);
}
void IncreaseArtifactCount()
{
  int iCount = ArtifactCount();
  iCount = iCount + 1;
  SetArtifactCount(iCount);
}
void StoreArtifact(object oArtifact)
{
    int iCount = ArtifactCount();
    StoreCampaignObject(ARTIFACT_DATABASE, ARTIFACT_TAG + IntToString(iCount + 1), oArtifact);
}

object RetrieveArtifact(int iNumber, object oNewOwner)
{
    location lOwnerLoc = GetLocation(oNewOwner);
    object oItem = RetrieveCampaignObject(ARTIFACT_DATABASE, ARTIFACT_TAG + IntToString(iNumber), lOwnerLoc, oNewOwner);
    return oItem;
}

void OnArtifactAcquire()
{
    object oItem = GetModuleItemAcquired();
    object oCreature = GetModuleItemAcquiredBy();
    if(GetIsDM(oCreature))
    {  return; }
    if(GetIsArtifact(oItem))
    {
       if(GetIsPC(oCreature))
       {
          WriteToArtifactLogDB(oItem, oCreature, 200);
       }
       else
       {
          WriteToArtifactLogDB(oItem, oCreature, 100);
       }

    }
}



int ArtifactPickedUpCount()
{
   int iCount = GetCampaignInt(ARTIFACT_DETECT_DB, "ARTS_PICKED_COUNT");
   return iCount;
}

void SetPickedUpArtifactCount(int iCount)
{
  SetCampaignInt(ARTIFACT_DETECT_DB, "ARTS_PICKED_COUNT", iCount);
}

void IncreasePickedUpCount()
{
   int iCount =  ArtifactPickedUpCount();
   iCount++;
   SetPickedUpArtifactCount(iCount);
}
void DecreasePickedUpCount()
{
   int iCount =  ArtifactPickedUpCount();
   iCount--;
   SetPickedUpArtifactCount(iCount);
}

int ArtifactSpawnedCount()
{
   int iCount = GetCampaignInt(ARTIFACT_DETECT_DB, "ARTS_COUNT");
   return iCount;
}

void SetSpawnedArtifactCount(int iCount)
{
  SetCampaignInt(ARTIFACT_DETECT_DB, "ARTS_COUNT", iCount);
}

void IncreaseSpawnedCount()
{
   int iCount =  ArtifactSpawnedCount();
   iCount++;
   SetSpawnedArtifactCount(iCount);
}
void DecreaseSpawnedCount()
{
   int iCount =  ArtifactSpawnedCount();
   iCount--;
   SetSpawnedArtifactCount(iCount);
}

void WriteToArtifactLogDB(object oItem, object oCreature, int iCategory)
{

   //spawned on creature
   if(iCategory == 100)
   {
       int iCount =  ArtifactSpawnedCount();
       SetCampaignString(ARTIFACT_DETECT_DB, "ARTIFACT_ITEM_" + IntToString(iCount + 1), GetName(oItem));
       SetCampaignString(ARTIFACT_DETECT_DB, "ARTIFACT_ITEM_ON_" + IntToString(iCount + 1), GetName(oCreature));
       IncreaseSpawnedCount();
   }
   else if(iCategory == 200)
   {
       //Added since AcquireItem events fires onEntry as well >.<
       int Spawned = GetCampaignInt(ARTIFACT_DETECT_DB, "ARTIFACT_" + GetResRef(oItem));
       if(Spawned)
       { return; }

       int iCount = ArtifactPickedUpCount();
       SetCampaignString(ARTIFACT_DETECT_DB, "ARTIFACT_PICKEDUP_" + IntToString(iCount + 1), GetName(oItem));
       SetCampaignString(ARTIFACT_DETECT_DB, "ARTIFACT_PICKEDUP_BY_" + IntToString(iCount + 1), GetName(oCreature));
       SetCampaignInt(ARTIFACT_DETECT_DB, "ARTIFACT_" + GetResRef(oItem), TRUE);
       IncreasePickedUpCount();
   }
}

void ReadArtifactDBLogToUser(object oPC)
{
    SendMessageToPC(oPC, "------Reading Artifact DB Log: ------");
    int iCount =  ArtifactSpawnedCount();
    string ItemName;
    string Owner;
    int i = 1;
    SendMessageToPC(oPC, " ");
    SendMessageToPC(oPC, "List of Spawned Items and Creatures who spawned them:");
    SendMessageToPC(oPC, " ");
    while(i != iCount)
    {
       if(iCount == 0)
       { break; }
       ItemName = GetCampaignString(ARTIFACT_DETECT_DB, "ARTIFACT_ITEM_" + IntToString(i));
       Owner = GetCampaignString(ARTIFACT_DETECT_DB, "ARTIFACT_ITEM_ON_" + IntToString(i));
       SendMessageToPC(oPC, IntToString(i) + ".     " + ItemName + " was spawned on: " + Owner);
       i++;
    }
    iCount =  ArtifactPickedUpCount();
    i = 1;
    SendMessageToPC(oPC, " ");
    SendMessageToPC(oPC, "List of Pickedup Items and Players who acquired them:");
    SendMessageToPC(oPC, " ");
    while(i != iCount)
    {
       if(iCount == 0)
       { break; }
       ItemName = GetCampaignString(ARTIFACT_DETECT_DB, "ARTIFACT_PICKEDUP_" + IntToString(i));
       Owner =  GetCampaignString(ARTIFACT_DETECT_DB, "ARTIFACT_PICKEDUP_BY_" + IntToString(i));
       SendMessageToPC(oPC, IntToString(i) + ".     " + ItemName + " was pickedup by: " + Owner);
       i++;
    }
    SendMessageToPC(oPC, " ");
    SendMessageToPC(oPC, "-----------End of List------------");
}
object SpawnArtifactOnCreature(int iRandom, object Owner)
{
  int iMax = ArtifactCount();
  //out of artifacts
  if(iMax == 0)
  {
     return OBJECT_INVALID;
  }
  object oArtifact = RetrieveArtifact(iRandom, Owner);
  //If it is not the last artifact in the order...
  if(iRandom != iMax)
  {
      object oChest = GetArtifactChest();
      object LastArtifact = RetrieveArtifact(iMax, oChest);
      StoreCampaignObject(ARTIFACT_DATABASE, ARTIFACT_TAG + IntToString(iRandom), LastArtifact);
      SetPlotFlag(LastArtifact, FALSE);
      DestroyObject(LastArtifact);
   }
  DecreaseArtifactCount();
  return oArtifact;
}

void InitializeArtifactContainer()
{
  if(!ENABLE_ARTIFACT_SYSTEM)
  { return; }

  object oCont = GetArtifactChest();
  //Do this only once.
  if(!GetLocalInt(oCont, "CHEST_INITIALIZED"))
  {
      //Chest initializes only once -ever.
      if(GetCampaignInt(ARTIFACT_DATABASE, "CHEST_LOADED"))
      {
        SetLocalInt(oCont, "CHEST_INITIALIZED", TRUE);
        //Get rid of the existing artifacts that have been added to the database,
        //but in the container.
        object oItem = GetFirstItemInInventory(oCont);
        while(GetIsObjectValid(oItem))
        {
           SetPlotFlag(oItem, FALSE);
           DestroyObject(oItem, 0.1);
           oItem = GetNextItemInInventory(oCont);
        }
        return;
      }
      else
      {
         object oItem = GetFirstItemInInventory(oCont);
         while(GetIsObjectValid(oItem))
          {
             StoreArtifact(oItem);
             IncreaseArtifactCount();
             SetPlotFlag(oItem, FALSE);
             DestroyObject(oItem, 0.1);
             oItem = GetNextItemInInventory(oCont);
          }
        SetCampaignInt(ARTIFACT_DATABASE, "CHEST_LOADED", TRUE);

      }
  }
  else
  {
     return;
  }
}

void ArtifactChestOnClose()
{
   object oCont = OBJECT_SELF;
   object oUser = GetLastClosedBy();
   SendMessageToPC(oUser, "Adding items to the artifact chest...");
   object oItem = GetFirstItemInInventory (oCont);
   int Count = ArtifactCount();
   while(GetIsObjectValid(oItem))
   {
     StoreArtifact(oItem);
     DestroyObject(oItem, 0.1);
     Count++;
     oItem = GetNextItemInInventory(oCont);
   }
   SetArtifactCount(Count);
   SendMessageToPC(oUser, "Items were added to the artifact chest successfully.");
}

void SendArtifactCountMessage(object oPlayer)
{
   SendMessageToPC(oPlayer, "Server Artifact Count: " + IntToString(ArtifactCount()));
}

void DeleteArtifactDatabase()
{
   DestroyCampaignDatabase(ARTIFACT_DATABASE);
   DestroyCampaignDatabase(ARTIFACT_DETECT_DB);
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    nwnx.org Forum Index -> Database related All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group