• This is a read only backup of the old Emudevs forum. If you want to have anything removed, please message me on Discord: KittyKaev

Need help with an script.

Status
Not open for further replies.

Darthye

Enthusiast
Hey, i need help making some changes in this script: http://pastebin.com/zLV9b6EX
What i want to do, is to make the spawn of an specific theme (themes are handled in the db) be automatic every day at X hour, removing completely the npc gossip functions.
After doing some research, i noticed that UNIX_TIMESTAMP was the function that needs to be used to do this, but making an:
Code:
if (UNIX_TIMESTAMP(0000-00-00 00:00:00))
didn't work.

Could somebody please help me or try to do it?

EDIT: Read also response #2 to this post. If you are going to help me you must know that both ways of fixing my "problem" are ok, but i prefer the one is explained at this comment (#1).
(My problem is that i want to make the players know when they are going to be able to spawn another theme throught the NPC. With the way in response #1 the problem is fixing the UNIX_TIMESTAMP thing, IDK how to deal with it. With the way in response #2 the problem is making the global announce appear in-game, read also #6 to take a look at my try to fix it).
 
Last edited:

Darthye

Enthusiast
Or another solution for my problem would be a way to add in the script itself a function to check how many minutes are left to be able to spawn a theme again, and if there is exactly 5 min left (for example), automatically a global announce pops up in the server.

Any of the two ways are ok for me.
Help please?

EDIT: Read also response #6 to this post to make an idea of what i mean.
 
Last edited:

Tommy

Founder
Honestly, I've never used the system before; however, there are quite a few tables to get use to.

















All of that will help you. It is their wiki, but I doubt they really are descriptive about anything. They normally just slap down their table name and columns then leave it. :/

I'm sure it will be of help. If you need further assistance, I could look into creating it for you when I get time.
 

Darthye

Enthusiast
Honestly, I've never used the system before; however, there are quite a few tables to get use to.

















All of that will help you. It is their wiki, but I doubt they really are descriptive about anything. They normally just slap down their table name and columns then leave it. :/

I'm sure it will be of help. If you need further assistance, I could look into creating it for you when I get time.


As i supossed after some checking i did, game_event can't fix my problem, well its not exactly that, is that it isn't related in any way in what i want to do.
BUT i got another quick idea to do my purpose, the only problem is that i don't know how to make this work.

Look, my actual script with my modifications and lets say "final" is this one: http://pastebin.com/juQ7iJvx
In the database, the table evento_gurubashi_lastspawn has the column 'time'. This column handles the time in miliseconds, from 10 minutes (in miliseconds idk how much it is) to 0. When the timer ends you can spawn another theme using the NPC. (I think the script updates the time constantly because when you go to check the NPC it says: There is X minutes left to be able to spawn another theme. And in the table evento_gurubashi_lastspawn i also see the miliseconds decrease if i refresh the table).
The thing is, i wanted to add some code to the script or create a new script, that makes a global server announce when 5 minutes are left, just that.
I tried with:
Code:
void alertServer(const char * name, int msg)
{
  	QueryResult result;
	  result = WorldDatabase.PQuery("SELECT `time` FROM `evento_gurubashi_lastspawn` where 'time' = 350000"); // 350000 in miliseconds = 5 minutes
	if (result)
	  sWorld->SendGlobalText("Try fix.", NULL);
}

And it compiles fine, but it just doesn't work in game. I added it to the script.
Please Tommy, could u help me to do that? I think its something really simple, but i just don't figure out how to do it.

Regards
 

Tommy

Founder
As i supossed after some checking i did, game_event can't fix my problem, well its not exactly that, is that it isn't related in any way in what i want to do.
BUT i got another quick idea to do my purpose, the only problem is that i don't know how to make this work.

Look, my actual script with my modifications and lets say "final" is this one: http://pastebin.com/juQ7iJvx
In the database, the table evento_gurubashi_lastspawn has the column 'time'. This column handles the time in miliseconds, from 10 minutes (in miliseconds idk how much it is) to 0. When the timer ends you can spawn another theme using the NPC. (I think the script updates the time constantly because when you go to check the NPC it says: There is X minutes left to be able to spawn another theme. And in the table evento_gurubashi_lastspawn i also see the miliseconds decrease if i refresh the table).
The thing is, i wanted to add some code to the script or create a new script, that makes a global server announce when 5 minutes are left, just that.
I tried with:
Code:
void alertServer(const char * name, int msg)
{
      QueryResult result;
      result = WorldDatabase.PQuery("SELECT `time` FROM `evento_gurubashi_lastspawn` where 'time' = 350000"); // 350000 in miliseconds = 5 minutes
    if (result)
      sWorld->SendGlobalText("Try fix.", NULL);
}

And it compiles fine, but it just doesn't work in game. I added it to the script.
Please Tommy, could u help me to do that? I think its something really simple, but i just don't figure out how to do it.

Regards

The Game Event system would be great for this, I don't see how you don't see that. Your choice to use it or not.

About your code, the reason why it doesn't work is because you aren't getting the time value itself. Also make sure all your naming is correct, etc.

Code:
void alertServer(const char * name, int msg)
{
      QueryResult result;
      result = WorldDatabase.PQuery("SELECT `time` FROM `evento_gurubashi_lastspawn` where 'time' = 350000"); // 350000 in miliseconds = 5 minutes
    if (result)
    {
        do
        {
            Field* fields = result->Fetch();
            
            if (fields[0].GetInt32() <= 350000)
                sWorld->SendGlobalText("Try fix.", NULL);
        }while(result->NextRow());
    }
}

To simplify what I've done to the code, you could just get the count that would return 1 or 0 if a specific column has 350000 ms left. Honestly, it would probably be best to use the 'getMSTime()' function and get the difference from that old ms time to the current time when you called 'getMSTime()'
 

Darthye

Enthusiast
The Game Event system would be great for this, I don't see how you don't see that. Your choice to use it or not.

About your code, the reason why it doesn't work is because you aren't getting the time value itself. Also make sure all your naming is correct, etc.

Code:
void alertServer(const char * name, int msg)
{
      QueryResult result;
      result = WorldDatabase.PQuery("SELECT `time` FROM `evento_gurubashi_lastspawn` where 'time' = 350000"); // 350000 in miliseconds = 5 minutes
    if (result)
    {
        do
        {
            Field* fields = result->Fetch();
            
            if (fields[0].GetInt32() <= 350000)
                sWorld->SendGlobalText("Try fix.", NULL);
        }while(result->NextRow());
    }
}

To simplify what I've done to the code, you could just get the count that would return 1 or 0 if a specific column has 350000 ms left. Honestly, it would probably be best to use the 'getMSTime()' function and get the difference from that old ms time to the current time when you called 'getMSTime()'


Thank your for your time Tommy, and i really appreciate your help. The problem is that i made a mistake, i thought that the time stored in evento_gurubashi_lastspawn was the timer decreasing to 0, and when it was 0 you can spawn another theme again. It is not like that, the time stored in the 'time' column of that table, is the UNIX_TIMESTAMP value converted to timestamp (UNIX_TIMESTAMP = 0000-00-00 00:00:00 // timestamp = 000000000). [The time that is stored on the table is the exact day/year/hour in timestamp when you selected the theme.]
I tried this:

Code:
void alertServer(const char * name, int msg)
{
    QueryResult result;
      result = WorldDatabase.PQuery("SELECT `time` FROM `evento_gurubashi_lastspawn`"); // 350000 in miliseconds = 5 minutes
    if (result)
    {
        do
        {
            Field* fields = result->Fetch();
            
            if (UNIX_TIMESTAMP('time'+60))
                sWorld->SendGlobalText("Try fix.", NULL);
        }while(result->NextRow());
    }
}
but it just didn't work. This was an attempt to make a global server announce 1 minute after you selected the theme desired to be spawned in the NPC.

To get the timestamp value of UNIX_TIMESTAMP(0000-00-00 00:00:00) use this query. (On the MySQL database. I just added it, it could help)
Code:
SELECT UNIX_TIMESTAMP('2013-01-07 15:01:00');

Any ideas/help please?
 
Last edited:

Tommy

Founder
Thank your for your time Tommy, and i really appreciate your help. The problem is that i made a mistake, i thought that the time stored in evento_gurubashi_lastspawn was the timer decreasing to 0, and when it was 0 you can spawn another theme again. It is not like that, the time stored in the 'time' column of that table, is the UNIX_TIMESTAMP value converted to timestamp (UNIX_TIMESTAMP = 0000-00-00 00:00:00 // timestamp = 000000000). [The time that is stored on the table is the exact day/year/hour in timestamp when you selected the theme.]
I tried this:

Code:
void alertServer(const char * name, int msg)
{
    QueryResult result;
      result = WorldDatabase.PQuery("SELECT `time` FROM `evento_gurubashi_lastspawn`"); // 350000 in miliseconds = 5 minutes
    if (result)
    {
        do
        {
            Field* fields = result->Fetch();
            
            if (UNIX_TIMESTAMP('time'+60))
                sWorld->SendGlobalText("Try fix.", NULL);
        }while(result->NextRow());
    }
}
but it just didn't work. This was an attempt to make a global server announce 1 minute after you selected the theme desired to be spawned in the NPC.

To get the timestamp value of UNIX_TIMESTAMP(0000-00-00 00:00:00) use this query. (On the MySQL database. I just added it, it could help)
Code:
SELECT UNIX_TIMESTAMP('2013-01-07 15:01:00');

Any ideas/help please?

I think this is getting to complicated for a simple idea. Like I mentioned before, getMSTime() would be best for this, seeing you can return the difference from when you called getMSTime() to when you call another function to get the difference. Imo, would be better than doing this timed stuff and somewhat less code.

Can you give me your table structure?
 

Darthye

Enthusiast
I think this is getting to complicated for a simple idea. Like I mentioned before, getMSTime() would be best for this, seeing you can return the difference from when you called getMSTime() to when you call another function to get the difference. Imo, would be better than doing this timed stuff and somewhat less code.

Can you give me your table structure?

Here i pasted you the structure of the tables (without data of the columns): http://pastebin.com/t7NbRt1H
Yes, i agree with you that a simple idea is getting complicated, but i just don't figure out how to do it correctly, im confused. I don't know how to do the whole thing with getMSTime(); anyways, my C++ is pretty basic-medium and intuitional and i just started a short time ago.

If you could help me doing it, it would be awesome, anyways, thank you for the help you already gave to me.

Peace.

PS: I know my english isn't perfect as im not native-english speaker so sorry about possible mistakes in my reposts to the post.
 

Darthye

Enthusiast
Hey, this afternoon i was trying to deal with getMSTime() and making a difference be returned to send a global message to the server. Only trash and unuseful code, as expected. I can't make it work, also tried to do it in a few different ways but no luck at the moment.

Any ideas?
 

Tommy

Founder
Here i pasted you the structure of the tables (without data of the columns): http://pastebin.com/t7NbRt1H
Yes, i agree with you that a simple idea is getting complicated, but i just don't figure out how to do it correctly, im confused. I don't know how to do the whole thing with getMSTime(); anyways, my C++ is pretty basic-medium and intuitional and i just started a short time ago.

If you could help me doing it, it would be awesome, anyways, thank you for the help you already gave to me.

Peace.

PS: I know my english isn't perfect as im not native-english speaker so sorry about possible mistakes in my reposts to the post.

Hey, this afternoon i was trying to deal with getMSTime() and making a difference be returned to send a global message to the server. Only trash and unuseful code, as expected. I can't make it work, also tried to do it in a few different ways but no luck at the moment.

Any ideas?

Let's break it down.

All you want is to announce server wide that a theme is ready or has been activated?
 

Darthye

Enthusiast
Let's break it down.

All you want is to announce server wide that a theme is ready or has been activated?
Well, my desired purpose was being able to create a if (UNIX_TIMESTAMP...) condition and rewriting a bit the script. With this condition i can remove the NPC use and make everything automatic, setting an hour (same hour every day) in the UNIX_TIMESTAMP condition, and then automatic announce/theme spawn. This is what i would really need.

The other way that i told in post #2 was more likely a "hackfix". This way is making a global announce 5 minutes before you can activate a theme in the NPC again.


Both things will solve my problem (one or the other), but way #1 would be better and easier i think.

Regards.
 
Last edited:

Tommy

Founder
Well, my desired purpose was being able to create a if (UNIX_TIMESTAMP...) condition and rewriting a bit the script. With this condition i can remove the NPC use and make everything automatic, setting an hour (same hour every day) in the UNIX_TIMESTAMP condition, and then automatic announce/theme spawn. This is what i would really need.

The other way that i told in post #2 was more likely a "hackfix". This way is making a global announce 5 minutes before you can activate a theme in the NPC again.


Both things will solve my problem (one or the other), but way #1 would be better and easier i think.

Regards.

Alright, I'll see what I can do when I'm not outside. Could be awhile because I won't be active this week.
 

Tommy

Founder
What's bad with Creature scripts is that a player must be near for it to work, which is quite sad. I don't see how you're going to get it to work besides using WorldScript. Perhaps WorldScript might be too much, but I think you should stick with an invisible trigger and when a player is around the arena, it will spawn a theme. I'm not sure if you should support the WorldScript idea considering it recommends not to use heavy code.

I'll write the base out for both and see how you react to it.

Anyway, I cleared up the indentation since it was horrible:


Here's the script for the trigger: (Which I would really recommend)

Code:
class NPC_Evento_Gurubashi : public CreatureScript
{
public:
    NPC_Evento_Gurubashi() : CreatureScript("NPC_Evento_Gurubashi") {}

    struct NPC_Evento_GurubashiAI : public ScriptedAI
    {
        NPC_Evento_GurubashiAI(Creature* creature) : ScriptedAI(creature) { }

        uint32 spawnTimer;
        uint32 hasSpawnTimer;

        void Reset()
        {
            spawnTimer = 1000; // Spawn a theme asap!
        }

        void UpdateAI(uint32 diff)
        {
            if (spawnTimer <= diff)
            {
                QueryResult result;
                result = WorldDatabase.PQuery("DELETE FROM `evento_gurubashi_lastspawn`");
                result = WorldDatabase.PQuery("INSERT INTO `evento_gurubashi_lastspawn` VALUES (%u)", time (NULL));
                result = WorldDatabase.PQuery("SELECT `x`, `y`, `z`, `o`, `entry` FROM `evento_gurubashi_spawns` WHERE `theme` = %u", rand()%1); // rand() % NUMBER will spawn a random theme, make sure the number matches the amount of themes you have spawned
                if (result)
                {
                    do
                    {
                        Field *fields = result->Fetch();
                        me->SummonGameObject(fields[4].GetInt32(), fields[0].GetFloat(), fields[1].GetFloat(), fields[2].GetFloat(), fields[3].GetFloat(), 0, 0, 0, 0, 600); 
                    }while (result->NextRow());
                }
                spawnTimer = 350000; // Reset the timer
                hasSpawnTimer = 60000; // A minute after it was spawned?
           }
           else
               spawnTimer -= diff;

           if (hasSpawnTimer <= diff)
               sWorld->SendGlobalText("A theme at gurubashi has spawned!", NULL);
           else
               hasSpawnTimer -= diff;
        }
    };

    CreatureAI* GetAI(Creature* creature) const
    {
        return new NPC_Evento_GurubashiAI(creature);
    }
};

void AddSC_Script_Evento_Gurubashi()
{
    new NPC_Evento_Gurubashi();
}


Here's the WorldScript script:

Code:
#include "MapManager.h"
          
class NPC_Evento_Gurubashi : public WorldScript
{
public:
    NPC_Evento_Gurubashi() : WorldScript("NPC_Evento_Gurubashi") { }

    uint32 spawnTimer;
    uint32 hasSpawnTimer;

    void OnStartup()
    {
        spawnTimer = 1000; // Spawn asap
    }

    void OnWorldUpdate(uint32 diff)
    {
        if (spawnTimer <= diff)
        {
            Map* map = sMapMgr->FindMap(0, 0); // Gurubashi Map - Not an instance?
            QueryResult result;
            result = WorldDatabase.PQuery("DELETE FROM `evento_gurubashi_lastspawn`");
            result = WorldDatabase.PQuery("INSERT INTO `evento_gurubashi_lastspawn` VALUES (%u)", time (NULL));
            result = WorldDatabase.PQuery("SELECT `x`, `y`, `z`, `o`, `entry` FROM `evento_gurubashi_spawns` WHERE `theme` = %u", rand()%1); // rand() % NUMBER will spawn a random theme, make sure the number matches the amount of themes you have spawned
            if (result)
            {
                do
                {
                    Field *fields = result->Fetch();
                    GameObject* object = new GameObject;
                    uint32 guidLow = sObjectMgr->GenerateLowGuid(HIGHGUID_GAMEOBJECT);

                    const GameObjectTemplate* objectInfo = sObjectMgr->GetGameObjectTemplate(fields[4].GetInt32());

                    if (!objectInfo)
                    {
                        spawnTimer = 350000;
                        return;
                    }

                    if (!object->Create(guidLow, objectInfo->entry, map, 1, fields[0].GetFloat(), fields[1].GetFloat(), fields[2].GetFloat(), fields[3].GetFloat(), 0.0f, 0.0f, 0.0f, 0.0f, 0, GO_STATE_READY))
                    {
                        delete object;
                        spawnTimer = 350000;
                        return;
                    }
                    sObjectMgr->AddGameobjectToGrid(guidLow, sObjectMgr->GetGOData(guidLow));
                }while (result->NextRow());
            }
            spawnTimer = 350000; // Reset the timer
            hasSpawnTimer = 60000; // A minute after it was spawned?
        }

        if (hasSpawnTimer <= diff)
            sWorld->SendGlobalText("A theme at gurubashi has spawned!", NULL);
        else
            hasSpawnTimer -= diff;
    }
};

void AddSC_setup_guru()
{
    new NPC_Evento_Gurubashi;
}


Now, like I mentioned before with the trigger, a player must be around the trigger for anything to spawn, which is great I guess so it doesn't keep changing the theme if no players are online. I haven't tested any of this, so it would be great for some feedback. I know you wanted the UNIX_TIMESTAMP, but that doesn't include what I said about creatures and players have to be around them to spawn a theme. Enjoy.
 

Darthye

Enthusiast
@Tommy

Hey Tommy, thanks for replying!
Give me a few hours as im a bit busy atm to test it and i will report you the results.

I really apreciatte your help, thought you got tired of me!
 
Status
Not open for further replies.
Top