Jump to content

Optimized Blend Functions Fixed Strtok


Recommended Posts

 

 

 

#include "stdafx.h"
#include "constants.h"
#include "log.h"
#include "locale_service.h"
#include "item.h"
#include "blend_item.h"

struct BLEND_ITEM_INFO
{
	DWORD item_vnum;
	int apply_type;
	int apply_value[MAX_BLEND_ITEM_VALUE];
	int apply_duration[MAX_BLEND_ITEM_VALUE];
};

typedef std::vector<BLEND_ITEM_INFO*> T_BLEND_ITEM_INFO;
T_BLEND_ITEM_INFO s_blend_info;

bool Blend_Item_init()
{
	char file_name[256];
	snprintf (file_name, sizeof (file_name), "%s/blend.txt", LocaleService_GetBasePath().c_str());
	sys_log (0, "Blend_Item_init %s ", file_name);

	for (const auto& blend_item_info : s_blend_info)
	{
		M2_DELETE (blend_item_info);
	}

	s_blend_info.clear();

	if (false == Blend_Item_load (file_name))
	{
		sys_err ("<Blend_Item_init> fail");
		return false;
	}

	return true;
}

bool Blend_Item_load (char* file)
{
	FILE* fp;
	char one_line[256];
	const char* delim = " \t\r\n";
	char* v;

	BLEND_ITEM_INFO* blend_item_info;

	if (0 == file || 0 == file[0])
	{
		return false;
	}

	if ((fp = fopen (file, "r")) == 0)
	{
		return false;
	}

	while (fgets (one_line, 256, fp))
	{
		if (one_line[0] == '#')
		{
			continue;
		}

		const char* token_string = strtok (one_line, delim);

		if (nullptr == token_string)
		{
			continue;
		}

		TOKEN ("section")
		{
			blend_item_info = M2_NEW BLEND_ITEM_INFO;
			memset (blend_item_info, 0x00, sizeof (BLEND_ITEM_INFO));
		}
		else TOKEN ("item_vnum")
		{
			v = strtok (nullptr, delim);

			if (nullptr == v)
			{
				fclose (fp);
				return false;
			}

			str_to_number (blend_item_info->item_vnum, v);
		}
		else TOKEN ("apply_type")
		{
			v = strtok (nullptr, delim);

			if (nullptr == v)
			{
				fclose (fp);
				return false;
			}
			blend_item_info->apply_type = FN_get_apply_type (v);
		}
		else TOKEN ("apply_value")
		{
			for (int i = 0; i < MAX_BLEND_ITEM_VALUE; ++i)
			{
				v = strtok (nullptr, delim);

				if (nullptr == v)
				{
					fclose (fp);
					return false;
				}

				str_to_number (blend_item_info->apply_value[i], v);
			}
		}
		else TOKEN ("apply_duration")
		{
			for (int i = 0; i < MAX_BLEND_ITEM_VALUE; ++i)
			{
				v = strtok (nullptr, delim);

				if (nullptr == v)
				{
					fclose (fp);
					return false;
				}

				str_to_number (blend_item_info->apply_duration[i], v);
			}
		}
		else TOKEN ("end")
		{
			s_blend_info.push_back (blend_item_info);
		}
	}

	fclose (fp);
	return true;
}

static int FN_random_index()
{
	int	percent = number (1, 100);

	if (percent <= 10)
	{
		return 0;
	}
	else if (percent <= 30)
	{
		return 1;
	}
	else if (percent <= 70)
	{
		return 2;
	}
	else if (percent <= 90)
	{
		return 3;
	}
	else
	{
		return 4;
	}

	return 0;
}

static int FN_ECS_random_index()
{
	int	percent = number (1, 100);
	if (percent <= 5)
	{
		return 0;
	}
	else if (percent <= 15)
	{
		return 1;
	}
	else if (percent <= 60)
	{
		return 2;
	}
	else if (percent <= 85)
	{
		return 3;
	}
	else
	{
		return 4;
	}

	return 0;
}

bool Blend_Item_set_value (LPITEM item)
{
	for (const auto& blend_info : s_blend_info)
	{
		if (blend_info->item_vnum == item->GetVnum())
		{
			int	apply_type;
			int	apply_value;
			int	apply_duration;

			if (item->GetVnum() == 51002)
			{
				apply_type = blend_info->apply_type;
				apply_value = blend_info->apply_value[FN_ECS_random_index()];
				apply_duration = blend_info->apply_duration[FN_ECS_random_index()];
			}
			else
			{
				apply_type = blend_info->apply_type;
				apply_value = blend_info->apply_value[FN_random_index()];
				apply_duration = blend_info->apply_duration[FN_random_index()];
			}

			sys_log (0, "blend_item : type : %d, value : %d, du : %d", apply_type, apply_value, apply_duration);
			item->SetSocket (0, apply_type);
			item->SetSocket (1, apply_value);
			item->SetSocket (2, apply_duration);
			return true;
		}
	}
	return false;
}

bool Blend_Item_find (DWORD item_vnum)
{
	BLEND_ITEM_INFO* blend_info;
	for (const auto& blend_item_info : s_blend_info)
	{
		if (blend_info->item_vnum == item_vnum)
		{
			return true;
		}
	}

	return false;
}

 

http://paste.ubuntu.com/15718092/

Have fun.

This fix all blend bugs and issues.     Also fix the unsafe strtok();

And also const  helping the compiler to optimize your code, and you prevent yourself from making unintentional bugs

Removed macro.

Also contain some features auto transform and nullptr transform.

 

  1. A new parameter, s1max, prevents strtok_s from storing outside of the string being tokenized. (The string being divided into tokens is both an input and output of the function since strtok_s stores null characters into the string.)
  2. A new parameter, ptr, eliminates the static internal state that prevents strtok from being re-entrant (Subclause 1.1.12). (The ISO/IEC 9899 function wcstok and the ISO/IEC 9945 (POSIX) function strtok_r fix this problem identically.)

 

 

Regards ds_aim.

 

  • Metin2 Dev 1
  • Love 7
Link to comment
Share on other sites

  • 9 months later...

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.