Advertisement

C++ Workshop - Project 2

Started by September 27, 2006 04:56 PM
94 comments, last by Warlord_Shaun 16 years, 9 months ago
Quote:
Original post by westond
Im thinking of trying to compress my classes a bit by hashing some of my ID variables.


What do you hope to achieve by doing this?

Quote:
Original post by westond
I have a set of variables that give an individual game cell a location in the game world...

int zone_id
int zone_y
int zone_x

each zone id can be broken down into a description of the area of the world its in as well as being the declaration of the array the cells are held in.


Why not just use a structure or a class? What compells you to try and fit this all into a integer?

Quote:
Original post by westond
zone_id=1001 World zone 1 sub zone 1
zone_id=1101 World zone 1 town zone 1 subzone 1
etc...

the position is array1001<zone_y,zone_x>

what I would like to do is create a cell id that combines all this info
ex.
zone_id=1001
zone_y=2
zone_x=1

long cell_id=100121

but i need a way of breaking that up when I need zone_id, zone_y, and zone_x.

I've looked, but cant find anything that will just read the first 4 digits of cell_id and assign it to zone_id and the 5th digit to zone_y and so on. Im sure there is an easy way of doing this that doesnt involve character arrays and casting. Or am I mistaken?


If you were using bit flags, it would be possible to just pull off the bits, but without writing a little code, there's no way to pull off the characters. What you're doing is essentially trying to treat all this info as a concatenated string. (see long cell_id) Now, if you dont care about how your cell_id appears, you can also use Unions. With a union, you create a byte aligned set of values which all share the same space in memory...for example:

union CellInfo{    long cell_id;  // 32 bit long integer    struct    {        short zone_id; // 16 bit short integer        char zone_x;   // 8 bit byte        char zone_y;   // 8 bit byte                       // 32 bits total    } cell_info;};


By creating a union from the cell_id and the struct, you can now refer to the same space in memory in multiple ways. For example:

CellInfo myCell;// This line, although assigning a value to cell_id, actually stores stuff// in the same memory which contains zone_id, x, and y...with the first 16// bits being the zone_id, the next 8 being the x, and the last 8 being the y.myCell.cell_id = (id << 16) | (x << 8) | y;// You can then access the fields as follows...long cell_id = myCell.cell_id;short zone_id = myCell.cell_info.zone_id;  // this contains the value of "id" abovechar zone_x = myCell.cell_info.zone_x; // this contains the value of "x" above.


I'm still not sure WHY you'd want to do this, but here's a possible way to do it. I dont believe unions are in the C++ book, as they are C features, and not strictly C++. In general, you should just store your x, y, and id's in a class. There's no reason that I can think of to hash them all into a single variable. But in either event, I hope this helps.

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
is this an mmorpg you are talking about, I am just asking out of curiosity, I am new to C++, and am intrested in learning how to do it.
Advertisement
To the guy asking if this is an MMORPG... I wish. This is just the RPG portion. But the goal is to get up to the MMO. I still have a long way to go before I can even think aobut that... sigh.

To jwalsh,

Well, it is in a class called Cell. Each cell is an object. The y,x values will be used to move around a zone by array arithmatic and such. As it stands my class has these 4 data members cell_id, zone_id, zone_y, zone_x. Rather than writing 4+ methods to access each one seperately Id like to combine them into one cell id and then do all my operations on that single value. It will also , I think, make it easier to dynamically build the world at run time. All of the cell data is stored in a comma delim file. The structure of the object as it stands has 19 data members with more being added. So the more clever I can get with how I deal with the data.. the shorter and easier it is to pull all that info from the file in. Less chance for me to mess it up I guess.


Quote:
Original post by jwalsh

What do you hope to achieve by doing this?


Why not just use a structure or a class? What compells you to try and fit this all into a integer?


If you were using bit flags, it would be possible to just pull off the bits, but without writing a little code, there's no way to pull off the characters. What you're doing is essentially trying to treat all this info as a concatenated string. (see long cell_id) Now, if you dont care about how your cell_id appears, you can also use Unions. With a union, you create a byte aligned set of values which all share the same space in memory...for example:



By creating a union from the cell_id and the struct, you can now refer to the same space in memory in multiple ways. For example:



I'm still not sure WHY you'd want to do this, but here's a possible way to do it. I dont believe unions are in the C++ book, as they are C features, and not strictly C++. In general, you should just store your x, y, and id's in a class. There's no reason that I can think of to hash them all into a single variable. But in either event, I hope this helps.

Cheers!








I'm planning on creating a base class for all my game objects. This would be a abstact class that has public methods for retriving and storing the information to and from the file.
forget it, mine isnt coming out on schedule (btw, whens the deadline anyway?), im gonna take my time on this one...
The deadline is October 31st. It appears, however, that interest in the project and in the workshop has largely trailed off.

I'll be posting the remaining chapters all at once, for those who are still following and do have questions on those chapters.

I'm surprised no one either completed project 2, nor asked the questions necessary for them to do so. At any rate, just wanted to give you cut-off date.

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Advertisement
Quote:
Original post by jwalsh
I'm surprised no one either completed project 2, nor asked the questions necessary for them to do so. At any rate, just wanted to give you cut-off date.




I think Project 2 was a shade overwhelming (at least it was for me) - there's a lot of new functionality to be added, and in my personal case I've had to rework some older code, which is taking way more time than it should.

At the least, I'd like to see what Project 3 and 4 were (just to have a "finish line" for folks to work towards)



Here is the source and here is the compiled version (plus sample data files) of what I've thrown together so far. It still misses reading monsters from file, potions, and shops/banks (not a theoretical problem, deriving and using a static extra member would do the trick), and the gold piles need to be cleaned up after emptying. However, it kinda takes the motivation to write it all out away that I feel that this will not be taken further; I know there is no obstacle to adding the final polish, but it feels like wasted time. This links to my question I asked before: how long should such a mini-project take a practicing programmer? Less than a day? A day? A couple of days? More than half of my time was used for debugging; is that normal? I ended up rewriting my code a lot, it has barely anything left in it from Project 1; is that normal, considering that we should develop code-reusage skills and design in a way that would allow us to minimize the amount of extra work to be done?
.. realized I should've asked my question above, but oh well.

What's been killing my time project wise is this: I want to have multiple types of "stuff" (Swords vs. Shields vs. Potions, for instance). So I set up an Item class, then inherit into Weapon, Shield, Potion, etc. But as soon as I:

Item *myItem;
myItem = new ShinySword;

all my methods respond as an Item, not as a ShinySword.

I know I'm missing something obvious; can someone tell me what it is? ;)
You probably didn't make the methods virtual in Item and override them in your subclasses.

This topic is closed to new replies.

Advertisement