Renegade Public Forums
C&C: Renegade --> Dying since 2003™, resurrected in 2024!
Home » Renegade Discussions » Mod Forum » weird reaction with const w_chart
weird reaction with const w_chart [message #464060] Fri, 09 March 2012 06:21 Go to next message
robbyke is currently offline  robbyke
Messages: 348
Registered: September 2010
Location: Belgium
Karma: 0
Recruit
::onchat call
Toggle Spoiler


start of the called function
Toggle Spoiler


keyhook function
Toggle Spoiler


what happens:

if i use a command in game the command works.
but the message is just a ! according to the game

index.php?t=getfile&id=14028&private=0

but when i use my chathook the message is more and the command wont work
index.php?t=getfile&id=14027&private=0


now i wonder what i do wrong.
probably my conversion.


Owner of kambot TT server

kambot.freeforums.org
Re: weird reaction with const w_chart [message #464063 is a reply to message #464060] Fri, 09 March 2012 06:53 Go to previous messageGo to next message
iRANian is currently offline  iRANian
Messages: 4308
Registered: April 2011
Karma: 0
General (4 Stars)
You're using %s with a wchar_t in formatted functions, you need to use %S. Casting a char to wchar_t (if it works) can cause memory corruption. Instead of using wchar_t, use StringClass. Instead of using Console_Input, use a wrapper function that takes formated input like Console_Output() does. This is how I would write the code:

Toggle Spoiler


Not sure if it actually runs correctly though, didn't bother checking. You can also use the __FUNCTION__ and __LINE__ macros to grab the function the code is executing and the line number while debugging.


Long time and well respected Renegade community member, programmer, modder and tester.

Scripts 4.0 private beta tester since May 2011.

My Renegade server plugins releases
Re: weird reaction with const w_chart [message #464083 is a reply to message #464060] Fri, 09 March 2012 10:51 Go to previous messageGo to next message
robbyke is currently offline  robbyke
Messages: 348
Registered: September 2010
Location: Belgium
Karma: 0
Recruit
thanks this helped alot

made me understand stringclass better to


Owner of kambot TT server

kambot.freeforums.org
Re: weird reaction with const w_chart [message #464170 is a reply to message #464083] Sat, 10 March 2012 17:05 Go to previous messageGo to next message
iRANian is currently offline  iRANian
Messages: 4308
Registered: April 2011
Karma: 0
General (4 Stars)
robbyke wrote on Fri, 09 March 2012 10:51

thanks this helped alot

made me understand stringclass better to

Cheers no problem. I suggest you take a look at HashTemplateClass and HashTemplateIterator too, they provide hash maps/tables, also known as dictionaries in other languages. If you're gonna be building a bot that responds to commands, it's a nice idea to store them in a hash table, with as key the text that triggers the command and the value being a pointer to a class that inherits a generic chat command class, so you can call a function from a class when it is found in the hash table, which is done almost instantaneously, instead of having to go over a list of all triggers to trigger a specific function, which is very slow and makes the code to check what command to trigger very long-winded code.

I've written an example for this that I haven't tested, I hope this helps you:

derp.cpp:
#include "General.h"
#include "derp.h"
#include "HashTemplateClass.h"
#include "HashTemplateIterator.h"

// This macro creates a new Class on the heap and cals Startup for it
#define REGISTER_COMMAND(Class, Trigger, registrant) Class *registrant = new Class; registrant->Startup(Trigger)

HashTemplateClass<StringClass, ChatCommand *> ChatCommand::CommandsMap;

void ChatCommand::Load()
{
	REGISTER_COMMAND(Help, "!help", Help_Registrant); // Registers a new Help ChatCommand objectcalled with !help
	REGISTER_COMMAND(Help, "!h", HelpAlias_Registrant); // Registers a new Help ChatCommand object called with !h
}

void ChatCommand::Unload()
{
}

// This simply adds the command to our CommandsMap with the trigger, it can later be expanded to do more
void ChatCommand::Startup(const char* Trigger)
{
	ChatCommand::CommandsMap.Insert(Trigger, this); // 'this' is a pointer to the current class
}

// This is a virtual method, this is used for the default for classes that don't have the Activate() method
void ChatCommand::Activate(int ID, int Type, StringClass Msg)
{
	Console("MSG this command hasn't been implemented yet!");
}

// This is the only thing we nee to implement for a new class inheriting from ChatCommand
void Help::Activate(int ID, int Type, StringClass Msg)
{
	Console("MSG This is a fancy help command!");
}

// Example chat hook code to get the above to work
bool KamBot::OnChat(int PlayerID, TextMessageEnum Type, const wchar_t *Message, int recieverID)
{
	StringClass Msg = Message;

	if (ChatCommand::CommandsMap.Exists(Msg)) // Does our hash map contain a trigger that is the same as the input message?
	{ // If so
		ChatCommand* c = ChatCommand::CommandsMap.Get(Msg, 0); // Get the ChatCommand pointer that's indexed for the chat message
		c->Activate(PlayerID, Type, Msg); // With our ChatCommand pointer called 'c', call the Activate() function
	}

	return true;
}


derp.h:
class ChatCommand 
{
public:	

	// Static functions and data, dont need to be called from an object
	static HashTemplateClass<StringClass, ChatCommand *> CommandsMap; // This is our hash map, we put ChatCommand object pointers here
																	// That are triggered by a StringClass trigger text

	static void Load(); // This is our loading code, call this from somewhere
	static void Unload(); // Unloads all chat commands related stuff

	void Startup(const char* Trigger); // Loads a ChatCommand object and adds it to the hash table

	virtual void Activate(int ID, int Type, StringClass Msg); // The default Activate() function called if not defined by an inheriting class
};

// a class that inherits the ChatCommand class, this one implements the !help command
class Help : public ChatCommand
{
public:
	void Activate(int ID, int Type, StringClass Msg); // The code to execute when this function is called in the chat hook by its trigger
};


This code should respond to ingame chat that's "!h" or "!help", but I haven't tested it.


Long time and well respected Renegade community member, programmer, modder and tester.

Scripts 4.0 private beta tester since May 2011.

My Renegade server plugins releases

[Updated on: Sat, 10 March 2012 17:08]

Report message to a moderator

Re: weird reaction with const w_chart [message #464182 is a reply to message #464170] Sat, 10 March 2012 18:28 Go to previous messageGo to next message
Ethenal is currently offline  Ethenal
Messages: 2532
Registered: January 2007
Location: US of A
Karma: 0
General (2 Stars)

iRANian wrote on Sat, 10 March 2012 18:05

If you're gonna be building a bot that responds to commands, it's a nice idea to store them in a hash table, with as key the text that triggers the command and the value being a pointer to a class that inherits a generic chat command class, so you can call a function from a class when it is found in the hash table, which is done almost instantaneously, instead of having to go over a list of all triggers to trigger a specific function, which is very slow and makes the code to check what command to trigger very long-winded code.

Isn't that essentially the same thing? Even if you're using a hash table, does it not internally have to go down the list of values until it finds the one with the name you want?


-TLS-DJ-EYE-K wrote on Mon, 18 March 2013 07:29

Instead of showing us that u aren't more inteligent than a Toast, maybe you should start becomming good in renegade Thumbs Up

Re: weird reaction with const w_chart [message #464205 is a reply to message #464060] Sun, 11 March 2012 05:19 Go to previous messageGo to next message
iRANian is currently offline  iRANian
Messages: 4308
Registered: April 2011
Karma: 0
General (4 Stars)
No, it's basically an array where a hash function is used to index values with based on the key, and when you want to grab a value the index is calculated from the hash of the key.

See: http://www.pcmag.com/encyclopedia_term/0,2542,t=hash+table&i=44129,00.asp

A very simplified view of it would be like:

Array[hash_function(key)] = value;
printf("%s", Array[hash_function(key)]);

Although it requires a lot of internal work and in C++ you need to use std::unordered_map (see http://en.wikipedia.org/wiki/Unordered_map_(C%2B%2B)) or in case of using the scripts.dll API HashTemplateClass can be used.


Long time and well respected Renegade community member, programmer, modder and tester.

Scripts 4.0 private beta tester since May 2011.

My Renegade server plugins releases
Re: weird reaction with const w_chart [message #464210 is a reply to message #464060] Sun, 11 March 2012 06:54 Go to previous messageGo to next message
robbyke is currently offline  robbyke
Messages: 348
Registered: September 2010
Location: Belgium
Karma: 0
Recruit
ok this is pretty funny the code i have used to work like that but with the 4.0 i had to modify it because chatcommandclass was gona.

now u understand how that works and ill have remake all the commands again Very Happy

thanks alot this helps me great deal im really starting to understand everythin bit by bit


Owner of kambot TT server

kambot.freeforums.org
Re: weird reaction with const w_chart [message #464212 is a reply to message #464060] Sun, 11 March 2012 07:50 Go to previous messageGo to next message
iRANian is currently offline  iRANian
Messages: 4308
Registered: April 2011
Karma: 0
General (4 Stars)
You'll need a tokenizer class to split the message into tokens, I ported the SSGM 2.0.2 tokenizer class to 4.0, if you need that I can give you it.

Long time and well respected Renegade community member, programmer, modder and tester.

Scripts 4.0 private beta tester since May 2011.

My Renegade server plugins releases
Re: weird reaction with const w_chart [message #464213 is a reply to message #464060] Sun, 11 March 2012 08:32 Go to previous messageGo to next message
jonwil is currently offline  jonwil
Messages: 3557
Registered: February 2003
Karma: 0
General (3 Stars)

Look at CommandLineParser.h, that is part of 4.0 and what the ban system plugin, extraconsolecommands plugin, mute plugin, spectate plugin and sudden death plugin are using.


Jonathan Wilson aka Jonwil
Creator and Lead Coder of the Custom scripts.dll
Renegade Engine Guru
Creator and Lead Coder of TT.DLL
Official member of Tiberian Technologies
Re: weird reaction with const w_chart [message #464214 is a reply to message #464060] Sun, 11 March 2012 08:34 Go to previous messageGo to next message
robbyke is currently offline  robbyke
Messages: 348
Registered: September 2010
Location: Belgium
Karma: 0
Recruit
i tried to convert it once but i didnt understand vector then(stil not)

so ye if you want to it would be nice


Owner of kambot TT server

kambot.freeforums.org
Re: weird reaction with const w_chart [message #464216 is a reply to message #464060] Sun, 11 March 2012 08:45 Go to previous messageGo to next message
jonwil is currently offline  jonwil
Messages: 3557
Registered: February 2003
Karma: 0
General (3 Stars)

commandlineparser is already in 4.0.


Jonathan Wilson aka Jonwil
Creator and Lead Coder of the Custom scripts.dll
Renegade Engine Guru
Creator and Lead Coder of TT.DLL
Official member of Tiberian Technologies
Re: weird reaction with const w_chart [message #464217 is a reply to message #464214] Sun, 11 March 2012 09:03 Go to previous messageGo to next message
iRANian is currently offline  iRANian
Messages: 4308
Registered: April 2011
Karma: 0
General (4 Stars)
robbyke wrote on Sun, 11 March 2012 08:34

i tried to convert it once but i didnt understand vector then(stil not)

so ye if you want to it would be nice

Alright, it should work the same way the stuff in SSGM 2.0.2 worked like (accessing [0] gives you the full string, [1] gives you the first token). Not sure if there's any issues with it.

Tokenizer.cpp:
Toggle Spoiler


Tokenizer.h:
Toggle Spoiler


Long time and well respected Renegade community member, programmer, modder and tester.

Scripts 4.0 private beta tester since May 2011.

My Renegade server plugins releases
Re: weird reaction with const w_chart [message #464219 is a reply to message #464060] Sun, 11 March 2012 09:07 Go to previous messageGo to next message
iRANian is currently offline  iRANian
Messages: 4308
Registered: April 2011
Karma: 0
General (4 Stars)
If you modify Activate() to take a Tokenizer object instead of StringClass, you could write commands in a similar way to this:

void Ping::Activate(int ID, int Type, Tokenizer Msg)
{
	if (Msg.Size() > 1)
	{
		int Count = Functions::Get_Part_Names_Fixed(Msg[2]);
		if (Count < 1)
		{
			Functions::Page(ID,"Player not found.");
		}
		else if (Count > 1)
		{
			Functions::Page(ID, "Multiple players found.");
		}
		else
		{
			int OtherID = Get_Player_ID(Functions::Get_Part_Name_Fixed(Msg[2]));
			Player_t* p = Player::Get(OtherID);
			Functions::Page(ID,"%s's ping is %d.", p->Nick, Get_Ping(p->PlayerId));
		}
	}
	else
	{
		Functions::Page(ID,"Your ping is %d.", Get_Ping(ID));
	}
}


Long time and well respected Renegade community member, programmer, modder and tester.

Scripts 4.0 private beta tester since May 2011.

My Renegade server plugins releases
Re: weird reaction with const w_chart [message #464232 is a reply to message #464060] Sun, 11 March 2012 14:04 Go to previous messageGo to next message
robbyke is currently offline  robbyke
Messages: 348
Registered: September 2010
Location: Belgium
Karma: 0
Recruit
jonwil commandlineparser seems to be for consolo or at least those plugins use it for console commands while im bussy with ingame commands

Owner of kambot TT server

kambot.freeforums.org
Re: weird reaction with const w_chart [message #464257 is a reply to message #464060] Sun, 11 March 2012 19:18 Go to previous messageGo to next message
jonwil is currently offline  jonwil
Messages: 3557
Registered: February 2003
Karma: 0
General (3 Stars)

CommandLineParser is usable for anything where you have a bunch of different items all separated by spaces.

so a string like "a 1.4 ddd 4" can be parsed by it.
Or any string like that.
Its not just for console commands.


Jonathan Wilson aka Jonwil
Creator and Lead Coder of the Custom scripts.dll
Renegade Engine Guru
Creator and Lead Coder of TT.DLL
Official member of Tiberian Technologies
Re: weird reaction with const w_chart [message #464290 is a reply to message #464257] Mon, 12 March 2012 06:26 Go to previous messageGo to next message
Jerad2142 is currently offline  Jerad2142
Messages: 3812
Registered: July 2006
Location: USA
Karma: 6
General (3 Stars)
This is a real rough/bad example of how I THINK it works; I'm looking at it on my break, have never used it before, and am not sure if you can stick string class into Console_Input. However, implementing the tokenizer should be at least SOMEWHAT similar to this:
Tokenizer TokenString = new Tokenizer();
TokenString = "Cool Story Bro";
Console_Input(TokenString[3]);

If it works you should see "BRO" as an unknown command popup in pink text.


Re: weird reaction with const w_chart [message #464308 is a reply to message #464060] Mon, 12 March 2012 11:28 Go to previous message
iRANian is currently offline  iRANian
Messages: 4308
Registered: April 2011
Karma: 0
General (4 Stars)
It just takes a char* as input, e.g.

Tokenizer derp("yo 1 2");
Console("MSG %s", derp[3]);

//Output is "2".


Long time and well respected Renegade community member, programmer, modder and tester.

Scripts 4.0 private beta tester since May 2011.

My Renegade server plugins releases

[Updated on: Mon, 12 March 2012 11:29]

Report message to a moderator

Previous Topic: need some help whit hud text
Next Topic: C&C_Nodewar :: WIP
Goto Forum:
  


Current Time: Wed Dec 18 16:23:53 MST 2024

Total time taken to generate the page: 0.01557 seconds