Go Back   EQEmulator Home > EQEmulator Forums > Misc > Misc::Off Topic

Misc::Off Topic Want to talk about something that has nothing to do with EverQuest or the emulator? Post here.

Reply
 
Thread Tools Display Modes
  #1  
Old 10-21-2011, 10:55 PM
revloc02c's Avatar
revloc02c
Hill Giant
 
Join Date: Aug 2010
Location: UT
Posts: 215
Default What's this structure called?

I know this kind of structure is used in programming a lot:
Code:
0 = None
1 = Warrior
2 = Cleric
4 = Paladin
8 = Ranger
16 = Shadow Knight
32 = Druid
64 = Monk
128 = Bard
256 = Rogue
512 = Shaman
1024 = Necromancer
2048 = Wizard
4096 = Magician
8192 = Enchanter
16384 = Beastlord
32768 = Berserker
65535 = Any/All
...and I want to write some code that will convert the number into a list of classes. Should be easy to Google right, but I don't know what keywords to use--I don't know what these kinds of structures are called. Anyone know?

And if you're willing to save me a step and you happen to know, anyone have an algorithm that would produce a list from the number? Or know where to look for one?
Reply With Quote
  #2  
Old 10-22-2011, 12:11 AM
Congdar
Developer
 
Join Date: Jul 2007
Location: my own little world
Posts: 751
Default

i think the term you are looking for is bitmask
http://stu.mp/2004/06/a-quick-bitmas...ogrammers.html
__________________
The Realm
Reply With Quote
  #3  
Old 10-22-2011, 11:44 AM
revloc02c's Avatar
revloc02c
Hill Giant
 
Join Date: Aug 2010
Location: UT
Posts: 215
Default

Perfect Congdar, thanks...and even a link to boot.
Reply With Quote
  #4  
Old 10-22-2011, 07:42 PM
revloc02c's Avatar
revloc02c
Hill Giant
 
Join Date: Aug 2010
Location: UT
Posts: 215
Default

I did it more because I found the problem interesting rather than the solution being useful (and the problem was rather intriguing...some say I'm a geek that way). But since it's done I might as well post it:
Code:
-- ----------------------------
-- Function structure for `fn_displayClasses`
-- ----------------------------
DROP FUNCTION IF EXISTS `fn_displayClasses`;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` FUNCTION `fn_displayClasses`(classes int) RETURNS varchar(128) CHARSET utf8
BEGIN
	-- receives bitmask number for classes and converts it to a list of classes
	DECLARE classList VARCHAR(128);
	SET classList = "";

	IF (classes & 1) = 1 THEN
		SET classList = CONCAT(classList, "Warrior, ");
	END IF;
	IF (classes & 2) = 2 THEN
		SET classList = CONCAT(classList, "Cleric, ");
	END IF;
	IF (classes & 4) = 4 THEN
		SET classList = CONCAT(classList, "Paladin, ");
	END IF;
	IF (classes & 8) = 8 THEN
		SET classList = CONCAT(classList, "Ranger, ");
	END IF;
	IF (classes & 16) = 16 THEN
		SET classList = CONCAT(classList, "Shadow Knight, ");
	END IF;
	IF (classes & 32) = 32 THEN
		SET classList = CONCAT(classList, "Druid, ");
	END IF;
	IF (classes & 64) = 64 THEN
		SET classList = CONCAT(classList, "Monk, ");
	END IF;
	IF (classes & 128) = 128 THEN
		SET classList = CONCAT(classList, "Bard, ");
	END IF;
	IF (classes & 256) = 256 THEN
		SET classList = CONCAT(classList, "Rogue, ");
	END IF;
	IF (classes & 512) = 512 THEN
		SET classList = CONCAT(classList, "Shaman, ");
	END IF;
	IF (classes & 1024) = 1024 THEN
		SET classList = CONCAT(classList, "Necromancer, ");
	END IF;
	IF (classes & 2048) = 2048 THEN
		SET classList = CONCAT(classList, "Wizard, ");
	END IF;
	IF (classes & 4096) = 4096 THEN
		SET classList = CONCAT(classList, "Magician, ");
	END IF;
	IF (classes & 8192) = 8192 THEN
		SET classList = CONCAT(classList, "Enchanter, ");
	END IF;
	IF (classes & 16384) = 16384 THEN
		SET classList = CONCAT(classList, "Beastlord, ");
	END IF;
	IF (classes & 32768) = 32768 THEN
		SET classList = CONCAT(classList, "Berserker, ");
	END IF;

	RETURN classList;

END
;;
DELIMITER ;
I write and run a lot of SQL queries, so having a function that displays classes is something I will use. Didn't really need it, but it was fun to figure out. Maybe someone will want to use it...for something. Cheers.
Reply With Quote
  #5  
Old 10-22-2011, 08:42 PM
oakweb
Banned
 
Join Date: Oct 2011
Location: philippines
Posts: 1
Default

this is great and useful. thanks
Reply With Quote
  #6  
Old 10-22-2011, 09:22 PM
revloc02c's Avatar
revloc02c
Hill Giant
 
Join Date: Aug 2010
Location: UT
Posts: 215
Default

Oh. Well thanks. I will make one for races too, but prolly won't get to it for a couple days. I posted in Off-Topic because when I was looking for the name of the structure it was off-topic.
Reply With Quote
  #7  
Old 10-23-2011, 02:09 AM
revloc02c's Avatar
revloc02c
Hill Giant
 
Join Date: Aug 2010
Location: UT
Posts: 215
Default

Umm, there were some bugs in the code, so here's the edited version with all the fixes and updates:
Code:
-- ----------------------------
-- Function structure for `fn_displayClasses`
-- ----------------------------
DROP FUNCTION IF EXISTS `fn_displayClasses`;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` FUNCTION `fn_displayClasses`(classes int) RETURNS varchar(128) CHARSET utf8
BEGIN
	-- receives bitmask number for classes and converts it to a list of classes

	DECLARE classList VARCHAR(128);
	SET classList = "";

	IF classes = 0 THEN
		SET classList = CONCAT(classList, "None, ");
	END IF;
	IF (classes & 1) = 1 THEN
		SET classList = CONCAT(classList, "WAR, ");
	END IF;
	IF (classes & 2) = 2 THEN
		SET classList = CONCAT(classList, "CLR, ");
	END IF;
	IF (classes & 4) = 4 THEN
		SET classList = CONCAT(classList, "PAL, ");
	END IF;
	IF (classes & 8) = 8 THEN
		SET classList = CONCAT(classList, "RGN, ");
	END IF;
	IF (classes & 16) = 16 THEN
		SET classList = CONCAT(classList, "SHD, ");
	END IF;
	IF (classes & 32) = 32 THEN
		SET classList = CONCAT(classList, "DRU, ");
	END IF;
	IF (classes & 64) = 64 THEN
		SET classList = CONCAT(classList, "MNK, ");
	END IF;
	IF (classes & 128) = 128 THEN
		SET classList = CONCAT(classList, "BRD, ");
	END IF;
	IF (classes & 256) = 256 THEN
		SET classList = CONCAT(classList, "ROG, ");
	END IF;
	IF (classes & 512) = 512 THEN
		SET classList = CONCAT(classList, "SHM, ");
	END IF;
	IF (classes & 1024) = 1024 THEN
		SET classList = CONCAT(classList, "NEC, ");
	END IF;
	IF (classes & 2048) = 2048 THEN
		SET classList = CONCAT(classList, "WIZ, ");
	END IF;
	IF (classes & 4096) = 4096 THEN
		SET classList = CONCAT(classList, "MAG, ");
	END IF;
	IF (classes & 8192) = 8192 THEN
		SET classList = CONCAT(classList, "ENC, ");
	END IF;
	IF (classes & 16384) = 16384 THEN
		SET classList = CONCAT(classList, "BST, ");
	END IF;
	IF (classes & 32768) = 32768 THEN
		SET classList = CONCAT(classList, "BER, ");
	END IF;
	IF classes = 65535 THEN
		SET classList = "ALL, ";
	END IF;

	SET classList = LEFT(classList, (LENGTH(classList) - 2));
	RETURN classList;

END
;;
DELIMITER ;
Also made one for races:
Code:
-- ----------------------------
-- Function structure for `fn_displayRaces`
-- ----------------------------
DROP FUNCTION IF EXISTS `fn_displayRaces`;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` FUNCTION `fn_displayRaces`(races int) RETURNS varchar(128) CHARSET utf8
BEGIN
	-- receives bitmask number for races and converts it to a list of races

	DECLARE raceList VARCHAR(128);
	SET raceList = "";

	IF races = 0 THEN
		SET raceList = CONCAT(raceList, "None, ");
	END IF;
	IF (races & 1) = 1 THEN
		SET raceList = CONCAT(raceList, "HUM, ");
	END IF;
	IF (races & 2) = 2 THEN
		SET raceList = CONCAT(raceList, "BAR, ");
	END IF;
	IF (races & 4) = 4 THEN
		SET raceList = CONCAT(raceList, "ERU, ");
	END IF;
	IF (races & 8) = 8 THEN
		SET raceList = CONCAT(raceList, "ELF, ");
	END IF;
	IF (races & 16) = 16 THEN
		SET raceList = CONCAT(raceList, "HIE, ");
	END IF;
	IF (races & 32) = 32 THEN
		SET raceList = CONCAT(raceList, "DEF, ");
	END IF;
	IF (races & 64) = 64 THEN
		SET raceList = CONCAT(raceList, "HEF, ");
	END IF;
	IF (races & 128) = 128 THEN
		SET raceList = CONCAT(raceList, "DWF, ");
	END IF;
	IF (races & 256) = 256 THEN
		SET raceList = CONCAT(raceList, "TRL, ");
	END IF;
	IF (races & 512) = 512 THEN
		SET raceList = CONCAT(raceList, "OGR, ");
	END IF;
	IF (races & 1024) = 1024 THEN
		SET raceList = CONCAT(raceList, "HFL, ");
	END IF;
	IF (races & 2048) = 2048 THEN
		SET raceList = CONCAT(raceList, "GNM, ");
	END IF;
	IF (races & 4096) = 4096 THEN
		SET raceList = CONCAT(raceList, "IKS, ");
	END IF;
	IF (races & 8192) = 8192 THEN
		SET raceList = CONCAT(raceList, "VAH, ");
	END IF;
	IF (races & 16384) = 16384 THEN
		SET raceList = CONCAT(raceList, "FRG, ");
	END IF;
	IF (races & 32768) = 32768 THEN
		SET raceList = CONCAT(raceList, "Shroud, ");
	END IF;
	IF races = 65535 THEN
		SET raceList = "ALL, ";
	END IF;

	SET raceList = LEFT(raceList, (LENGTH(raceList) - 2));
	RETURN raceList;

END
;;
DELIMITER ;
And here's an example of how you might use them:
Code:
SELECT id, name, weight, itemtype, price, (damage/delay) AS 'Dmg/Dly', classes, fn_displayClasses(classes) AS "Classes List", races, fn_displayRaces(races) AS "Races List"
	FROM items
	WHERE itemtype < 10
	ORDER BY itemtype;
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

   

All times are GMT -4. The time now is 09:36 AM.


 

Everquest is a registered trademark of Daybreak Game Company LLC.
EQEmulator is not associated or affiliated in any way with Daybreak Game Company LLC.
Except where otherwise noted, this site is licensed under a Creative Commons License.
       
Powered by vBulletin®, Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Template by Bluepearl Design and vBulletin Templates - Ver3.3