PHP, Zend Framework and Other Crazy Stuff
Archive for February, 2006
QS Evolved: The Ship Model
Feb 28th
I’ve gotten a few questions (again) about needing to add more ships to QS Evolved over what is available from Solar Empire and QS2. I’ve blogged about this before so here’s the short version.
QS Evolved uses a hierarchical system. At the top are Fleets. Fleets are a collection of Ships. Here’s the kicker, Ships are a collection of Ship Components which infer attributes and value upon the Ship. I know it’s not instantly intuitive but ships are not a fixed entity. There are fixed Ships you can buy – however these are standard offerings only – even they can be modified over time. The big clue is Ship Production. If you want a ship you have two options. Buy it or make it. If you make it you can within reason design your ship from the Hull up, add components to it, and manufacture it. In the end what the ship is capable of is your choice – you designed it!
The other thing about ships is they’ll carry a few new limitations. Some weapons may require a finite supply of ammunition. Some may depend on your ship having sufficient power to fire them. Similar restrictions impact engines and shields. The idea behind all this is to remove the stupidly obvious scenario in QS2/SE that a game with unlimited resources will tend to identical fleet strengths – i.e. a stalemate, and usually one that emerges within days – not weeks. It also adds more strategic flavour to the game – if the ship components are up to you, then you can make a lot of choices to favour your tactics. You could outfit a ship with nothing but big powerful engines – let a Warship try catching you then
.
The attribute model of ships also enables ships to continually improve their performance – again within reason, and bounded by the capabilities of available components. Rather than having a ton of ships with limited abilities, you’ll have a smaller (25-50 range max) more easily managed set of ships which you could spend weeks constantly improving in small and large ways.
Absolutely key to everything is a simple assumption:
“Ships are a valuable asset. Losing a ship imposes a cost in time and cash.”
In Solar Empire and QS2 ships are NOT a valuable asset. You can lose 150 ships, and rebuild your fleet from scratch in a few days if the game settings are liberal (which they nearly always are). This is a big let down – ships are not worth upgrading unless they have a real value, enough that losing even a handful is a painful experience. The more value driven they are – the more incentive to upgrade, improve and increase their inherent value more. The more these occurs the more time and cash it takes to replace lost ships.
Like I said before, I am not cloning Solar Empire. This is a new Ship Model where losing a ship is a big deal – not an inconvenience solved by visiting a shipyard at Earth. A well upgraded ship with top-notch components could have cost you 5 days of mined resources. Even if you dragged all your other ships up to a superminer configuration it could still take 3 days of mining to even come close to replacing it – and that’s without the time needed to build it, and maybe collect any rare resources required for the construction of its rarer components.
So keep this in mind. When QSE is playable you will be able to manage up to 50 ships. Personally I’d prefer less – but lets say 50. To get truly powerful ships you’ll need to build them on planets. Doing this takes time, and a lot of cash. Losing a ship will be a big deal – it could take days to replace if of a high level. Ships are therefore a valuable asset – sacrificing them is still a strategic option, but a costly one if done for something stupid
. Ship management will require actual thought – those of a strategic mind would do well to plan carefully.
PHP Applications using UTF-8 – should we believe them?
Feb 28th
It’s not everyday you see both these terms side by side. PHP has poor UTF-8 support. Well, actually its non-existent once you ignore the optional libraries that many shared hosts probably won’t compile with PHP. Now many will know all about setting up HTML pages for UTF-8, for many its the latest programming mantra. Unfortunately just setting a content-type is not enough. This will work completely for English webpages – and only English webpages using unaccented roman characters [a-z0-9].
So what is this mysterious character encoding and why is it so very important to get it right?
The problem is the 16 ISO-8859′s (ISO-8859-16 is a necessity for the EU Euro symbol I think), ASCII, and dozens of other language and custom character encodings to support native character sets. In short its a case of too many cooks. To try and unify all these variants and offer a single standard supporting all character sets (and therefore all languages) we have Unicode, and by extension UTF-8 encoding of the Unicode character set. With UTF-8, a UTF-8 encoded file can represent anything from standard ASCII (read English) to Hindi and more. For a multi-lingual web application this is the best thing since fire was invented – or would be if PHP supported UTF-8 and PHP developers figured out that string functions in PHP at present are doomed to failure with UTF-8.
Consider the example of a Sourceforge page – https://sourceforge.net/users/maugrim_t_r/ . Yes, that’s my profile
. However there’s a small problem. The page insists it is encoded in UTF-8 (view the source). That’s not true. You see, a simple European accented character in my first name is not displayed – its replaced with a ?. Why? Because its not UTF-8 encoded. Ha! So much for internationalisation on Sourceforge. It claims to be UTF-8 – but its obviously not UTF-8. The truth is that only parts (if any) are actually UTF-8. The rest is most likely ISO-8859-1 – i.e. ASCII English.
One could blame me – I’m a bad person who uses a name containing a Gaelic accented character (equivalent to the french a-acute character). Why not blame the French why we’re at it, or the rest of those pesky non-English speaking people?
Its because of this that many self-proclaimed web apps purporting to support internationalisation or localisation (we use the abbreviation I18N for the latter) are being unrealistic. Many will support I18N for English, and a subset of European languages where accented characters can be given an English character variant, or maybe if they use a template system with UTF-8 encoded templates that require no manipulation – i.e. no database storage or use of PHP string functions. Some will require the mbstring library be available for PHP.
There are still problems. Take a simple example. I create a form accepting a username of no more than 32 characters. So someone from India drops by and enters a Hindi username – 32 Hindi characters. Now the form is submitted. Everything is great, right? We just check for 32 characters. But wait – there is no character length function in PHP! And no, strlen() is not that – not at all.
strlen() does NOT count characters – it counts BYTES. Never forget that distinction lest it burn you. Now UTF-8 is a multi-byte encoding. Keeping in mind UTF-8 incorporates single-byte ASCII we can say the string “Reaper” has 6 bytes, equalling 6 characters. This is what most of us already do – its the evil assumption. But what about Hindi? Hindi uses multi-byte characters.
A 32 character Hindi string will actually contain a lot more than 32 bytes – some languages may take up to 32 * 6bytes, i.e. 192 bytes to represent a 32 character string! Result – our filtering logic fails miserably, a victim of our false assumption that the whole world speaks English and writes with Roman characters.
There are other deeply set practices that fail to account for UTF-8. While Perl supports UTF-8 in RegExp, PHP’s PCRE does not. How many of us use regular expressions in form validation? Probably the same number who use strlen() to count characters… There are other examples. Basically you could list all the string functions.
There is a solution however – we can compile (or edit php.ini for dynamic inclusion) the mbstring library for PHP. However this has one flaw – it’s not a default library which means many shared hosts will not actually support it. So much for PHP’s UTF-8 support. The other solution is to wait for PHP6 which will have native UTF-8 support. But then what about all those hosts who’ll persist in using PHP4 because PHP5 is not backwards compatible?
In short, unless you’re a native English speaker – people using PHP and outputting UTF-8 html while claiming to support I18N are (in the majority) completely ignorant. Afterall for an English speaker there is zero difference between using ISO-8859-1, ASCII and UTF-8. Its the rest of the world that must jump through hoops. Even worse – many of the UTF-8 HTML out there is not even encoded in UTF-8. Often its just a typical ISO-8859 document (written on WindowsXP in almost all cases) with a UTF-8 content-type charset – this largely explains why name is commonly corrupted with question marks and boxes on many English sites claiming (falsely) to be UTF-8 encoded.
My name is not P?draic or Pdraic – it’s Pádraic! ![]()
Template-Lite
Feb 27th
Sort of linked to my last entry, the guys over at Smarty have stirred something up of a hornet’s nest with their trademarking of the “Smarty” name. As an open source project its fine for them to do so, but of course we free loading lovers don’t like the thought. More relevantly the developer of Smarty-Lite was recently forced as a result to stop using the term “Smarty” in their library.
One day I’m going to find a trademark database and figure out how a common use phrase evaded trademarking before now… I could have sworn it was a registered trademark at the very least in the US and for a non-related company. One assumes the Smarty copyright holders did perform a database search.
Anyways, in one of those obscure lengthy possibly illegible blog posts (just kidding), Panama Jack got around to posting a link to the Smarty-Lite followup renamed Template-Lite that he will be developing. The Template-Lite website announces the release of version 1.0.0 since February 17th. Really someone should have blogged this earlier! Main point is it uses around half the memory of Smarty and operates faster as a result.
I haven’t tested it myself, just got curious and poked around in the code. A few items of note. It’s a put together very well, so little wonder its using less memory – most of the Smarty bloat has been excised. I did note it was missing the “assign_by_ref()” method, and escapes using htmlspecialchars() – not htmlentities(‘string’, ENT_QUOTES, ‘UTF-8′) as is best practice (charset being variable obviously).
The first is likely an easy addition – would be a good one too since I do a spot of referencing in my View layer for QSE, and developers with dangerously large arrays don’t like copying them all over the place. I don’t use Smarty’s escaping mechanism so it doesn’t bother me – template designers however will. Not entirely certain what Smarty does for escaping…
I think I’ll try this out over the next few days and figure out how much memory it actually saves me.
An aside:
Just on the Smarty trademark notices and copyrights in general. Whoever wrote that crap should buy an English dictionary and look up the the term “clarity” before touching a keyboard again. Most of the terms are vague and one talks about limiting permissions covered by Fair Use (it reads that way at least). Whole page stinks of corporate bluster. I think I see what may irk people…
What in God’s name does this mean? Have I broken copyright law by opening their homepage? Is the public banned from viewing graphical and layout elements? C’mon guys, at least hire a lawyer to write this stuff.
The rights specified above to view, download and print the documents and information available on this Site are not applicable to the graphical elements, design or layout of this Site.
Go hop, I own a printer and you let me download your pages. Bleh.
ADOdb Lite
Feb 27th
Last September I decided to make a switch from the ADOdb Database Abstraction library to a smaller more memory efficient alternative. Of course, I’m talking about ADOdb-Lite created by Panama Jack (the guy in charge of Alien Assault Trader’s development). Making the switch was not done lightly. ADOdb is an extremely popular abstraction package with a lot of solid support and features. On the switch side it hogs memory. ADOdb-Lite‘s claims to reduce the memory usage substantially while still offering a similar (though reduced) feature set was very attractive.
The switchover overall has been worthwhile. Its worth noting that at the time ADOdb-Lite was a very new library so it took some time to settle down into a stable form. Since that time I’ve reported two bugs and requested one function change, and seen other issues like the unexpected return of full arrays not associated arrays (no numeral keys) a few versions back resolved.
Some ADOdb features are yet missing. There are no Caching functions, which removes the possibility of caching results to file. Now PanamaJack justifies this by noting in-memory caching features of MySQL – incidentally Postgres has something similar. Lets talk DBMS’s in general before justifying everything with MySQL. I rarely even use MySQL. But at the moment its not a concern for me – to be honest the library is open source, so if I do need it I’ll hack it in there with the nifty module system.
Also missing is the Performance Monitor offered by ADOdb. Again this is one of those extra rarely used features. It is quite useful, but I’m not certain how in-depth this was originally integrated into ADOdb. This doesn’t particularly bother me since I use ADOdb-Lite as the abstraction layer in a Data Access object – I can fit in a perfmon clone without touching the ADOdb-Lite source code. Of course it’s pointless at the moment given a DAO does not necessitate optimised SQL. Unfortunately its a complex item to add to a clearly defined DAO-TransferObject or ActiveRecord setup without turning the DAO into a collection of transaction scripts…
Another small niggle in the system are the potential typo-style errors. ADOdb-Lite was either developed on a plain text editor, or a non error reporting IDE. Eclipse still complains a lot about ADOdb-Lite having notice level errors, and in earlier versions potential parse errors. Small niggle, I have a preference for IDE quality assurance as a first line error pickup mechanism. The library does ship with tests, which although not Unit Tests do a fair job of exercising the Library. They’re imperfect however for a full test suite – they can’t cover all DBMS’s supported at one session. Hardly the developer’s fault – it’s just the nature of an abstraction library.
Overall ADOdb-Lite is definitely worth a look at. It does a fantastic job of reducing memory usage, enough that it makes up a substantial percentage of QSE’s memory use reduction over QS2. It’s matured over the last half year to a stable dependable library. It lacks some features of ADOdb, but these tend to be small-use features most developers wouldn’t even touch. Of course that may be a bitter pill for some developers to swallow. A good trend is a steady pickup of ADOdb-Lite – I’ve seen a few projects make the conversion recently.
In summary, if you’re expecting to develop an application for public consumption in enviroments that may include non-accelerator enabled shared hosts then ADOdb-Lite is a solid alternative to ADOdb. It’s still maturing, and has some small issues but for most common use-cases it’s worth looking at.
QS Evolved Screenshots!
Feb 27th
After putting in a lot of hours on Quantum Star SE Evolved this weekend I have moved onwards from my earlier 0.18 test release. That was aimed at exercising the Partholan backend to find any last obvious bugs. This weekend I finally got into the meat and bones of the game. I started with a diversion into the Installer which had a few issues in 0.18 (most annoying was the lack of a proper theme for it!). I’ve updated and added back the Install theme however and the results are quite satisfying. I’ve added a few screenshots to this post to show it off.
The opening Installer page.
The Installer is an attempt to simplify the often horrible level of detail some PHP applications will require. Most of the in-depth options are hidden and can be changed from their defaults later from the Administration Control Panel. In most cases they won’t need adjusting. A lot of the messy items from Solar Empire and QS2 have been removed. QSE automatically figures out the URL and full system path with little trouble. Database choices are down to a minimum with any specialised requirements managed fluidly by the backend. There’s been an effort to simplify the number of directories requiring permission changes (just Data and TplCache).
Now on the game side I’ve upped the ante and created a simplified CSS based theme called BlueSpace. It’s layout is flexible and completely CSS driven. Tables are being used only to support our form layouts and even that can potentially be replaced with a CSS style when I spend the time needed.
The temporary Admin CP page using the BlueSpace theme.
The theme is pure CSS. I spent time wondering how to manage theming and making it as flexible as possible, but in the end the usual lack of theme making on open source games decided the issue. Themes are now nothing more than customised headers, footers and CSS – that in itself adds most of the flexibility needed. The templates themselves are fixed XHTML though obviously you can alter these on a private server – I’ll note this means text is not changeable if you decided to upgrade directly from the standard QSE development releases without text diffs. This by effect decided how to manage translations. My translations approach is going to be to pre-generate translated versions of the base “tpl-en” English templates into separate Template directories; tpl-fr, tpl-cz tpl-fr-CA, etc. In effect I want to remove the need to translate on-the-fly at runtime anything except Game Module content. So lets just translate and store the templates as semi-independent entities, allowing perhaps some minimal CSS adjusting for things like text direction. The approach makes possible a more manageable translation effort matching full English sentences to translations – not the flawed key phrase matching that is very hard to get right.
Anyway to round out this post, the game is moving ahead steadily now that Partholan is feature frozen. I’m currently playing about with the Administration Control Panel to add the most necessary functions such as the Game Creator, Game Manager, and the updated Universe Generator. I have to admit feeling some growing pride with QSE. It’s hard to get that from working on a technical blunder like the original QS2. Evolved is waaaay better
. I’m having fun working on it at least…
Another BlueSpace view – the Create Game form view.
Can’t wait to get a starting Star System view finished so I can start adding in all the funky fun stuff like Fleet navigation and Homeworlds. Looks like yet another more complete release by week’s end! ![]()
