PDA

View Full Version : What's this structure called?


revloc02c
10-21-2011, 10:55 PM
I know this kind of structure is used in programming a lot:
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?

Congdar
10-22-2011, 12:11 AM
i think the term you are looking for is bitmask
http://stu.mp/2004/06/a-quick-bitmask-howto-for-programmers.html

revloc02c
10-22-2011, 11:44 AM
Perfect Congdar, thanks...and even a link to boot.

revloc02c
10-22-2011, 07:42 PM
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:

-- ----------------------------
-- 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.

oakweb
10-22-2011, 08:42 PM
this is great and useful.:) thanks

revloc02c
10-22-2011, 09:22 PM
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.

revloc02c
10-23-2011, 02:09 AM
Umm, there were some bugs in the code, so here's the edited version with all the fixes and updates:

-- ----------------------------
-- 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:

-- ----------------------------
-- 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:

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;