# On-the-fly object creation

This topic is 2558 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys

Just a fairly simple problem that I can't quite get straight in my head.

I want to be able to create ships (age of sail battle ships, frieghters etc)

They're all essentially the same thing - a boat of whatever size with a number of guns, sails, men and so on.

In my head, I can get a basic layout of the classes. There's a vessel (anything that floats) extended by freighters, men of war, transports etc, and implement rowable, sailable etc interfaces. I've not got it perfected as a hierarchy yet, but I'm on the right lines.

What I can't quite work out, is how to deal with guns on board the ships. I want each gun to be an individual object, so that it can have it's own loading time, damage etc. However, a ship carries up to 5 different types of gun, on up to 4 decks. ie a ship up to 40 guns will have one deck, but 40-60 may have one or two decks depending on type.

When creating the player's ships, I know what resources I want to give them so I can define this however I like - using decks of a set number of guns. However, if I just want to create a new ship within a certain class, how would I best go about this?

ie if I want to create a warship for my player to encounter, but I don't know whether I want a big or small ship, I could just randomise the number of guns between 0 and 100, and create a ship with that many guns. But I need my ship-creation system to take the approximate number of guns, map it to a set of available size ships (ie there are 36/40/44 gun ships, but not 35, 37 or 42), and then create the ship with the right number of decks, and the right number and type of guns for the type of ship, on the right decks.

I'm presuming I need to use a factory pattern style system, where the factory has some logic to create the ship - but I can't work out how to best do it without a LOT of condition tests and manual work. Are there any simpler ideas I can put into practice?

Any help appreciated, and any amount of detail, please feel free to ask for clarification if I've been vague/ambiguous.

Thanks
Jon

##### Share on other sites
Don't model it with an inheritance hierarchy, model it with data.

##### Share on other sites

Don't model it with an inheritance hierarchy, model it with data.

A ship instance owns several gun deck instances; gun decks can contain information and logic related to crew and reloading guns (although normally sharing targets and orders to fire, aim and wait, each deck operates independently), and they can own gun instances with their artillery related data and logic. Gun instances in the same deck need not be identical; apart from individual peculiarities of guns of the same model (e.g. high quality of the barrel improving range a bit), the deck can contain different guns. The ship can contain details of row and sail movement; do you need more than physical parameters and crew needs?

I cannot put your question about creating ships in context. How random are the ships and the random encounters? Do you choose ship types and hull sizes and put a reasonable number and selection of guns in them, or do you fit a ship of the right size and a sufficient gun crew to a given set of guns? (In other words, is it a game about ships in general, about cannon battles, or about something else?)

Why do you want to depart from the tried and true model of few predefined ship types and vary them individually? Unpredictability might reduce a player's trust in the tactics he has learned; even tracking damage and repairs of standard ship models might be a disturbance, unless the state of an enemy ship is implausibly explicit.

##### Share on other sites
Why not just loop through the amount of guns each deck has, then choose a ship size depending on the largest amount of guns a deck has (I gather that's what you want to do)

Something like this:

 int largestdecksize = 0; //loop though every deck for ( int i = 1; i <= numberofdecks; i++) { //find the deck with the largest amount of guns if (largestdecksize < deck.numberofguns) largestdecksize = deck.numberofguns } //Choose appropriate deck size if(largestdecksize < 11) shipsize = small; if(largestdecksize > 10 && largestdecksize < 21) shipsize = medium; if(largestdecksize > 20 && largestdecksize < 31) shipsize = big; if(largestdecksize > 30 largestdecksize < 41) shipsize = verybig; if(largestdecksize > 40 largestdecksize < 51) shipsize = massive; //etc 

Hopefully that's something like what you're asking?

##### Share on other sites
Malboro: I hadn't though of that - I could just specify the number of guns and decide on the calibre on the fly... I was trying to create the right guns and then transfer it onto suitable decks, which involves more work.

Lorenzo - it's not completely random: the user will be able to tell approximately what they're up against. visually there will only be about 5 sizes of ships with some visual variations to further separate them into perhaps 3 sub-groups, a faster, weaker one, the middle ground and a slower, stronger one. ie it should be fairly obvious when something is significantly more powerful and should be avoided or small enough to pose no threat: although it's intended for there to be a slight grey area with similar sized ships it will not be so drastic as to make much difference in a 1-vs-1 situation. The problems are intended to come from situations where there is perhaps one big threat which the player can outrun, but two smaller enemies who are aiming to slow the player down, or where a freighter convoy is being protected and the player has to weigh up whether it is possible to break in and take a prize or two before the escort become a problem etc... there will never be a situation where the player can't tell what they're up against: the problem for me is merely that there are too many variations to sensibly model completely in a database, it makes much more sense to do some of the work at runtime.

return0, I don't intend to model state in the inheritance hierarchy, but there are some behavioural differences which I will need to account for with different styles of ship: I won't be modelling all 15 (+variations, so more like 60) possible ship sizes, doubled for freighters: but I will be modelling the difference between rowing vessels, freighters and men-o-war as they are behaviourally very different.

Thanks guys, will take all of the above on board.

• 16
• 9
• 13
• 41
• 15