Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 135,921 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,578 people online right now. Registration is fast and FREE... Join Now!




RPG game code optimization

 
Reply to this topicStart new topic

RPG game code optimization

JBrace1990
15 May, 2008 - 04:22 PM
Post #1

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 476



Thanked: 22 times
Dream Kudos: 350
My Contributions
currently, I have this code. I wonder if there's a better way of coding this...

CODE
     $enemy_final_array = array("enemy_type" => $enemy_type, "enemy_subtype" => $enemy_sub_type, "attack" => ($enemy_array['attack'] / 100) * $enemy_subtype_array['attack'], "defense" => ($enemy_array['defense'] / 100) * $enemy_subtype_array['defense'], "speed" => ($enemy_array['speed'] / 100) * $enemy_subtype_array['speed'], "magic_attack" => ($enemy_array['magic_attack'] / 100) * $enemy_subtype_array['magic_attack'], "magic_defense" => ($enemy_array['magic_defense'] / 100) * $enemy_subtype_array['magic_defense'], "exp" => ($enemy_array['exp'] / 100) * $enemy_subtype_array['exp'], "effect" => $enemy_array['effect'], "enemy_total" => $enemy_type." ".$enemy_sub_type, "hp" => ($enemy_array['hp'] / 100) * $enemy_subtype_array['hp'], "hp_max" => ($enemy_array['hp_max'] / 100) * $enemy_subtype_array['hp_max'],);


as you can see, it's pretty ugly. so, anyone have any suggestions?

I guess you can tell form the code what i'm planning, but if you can't, here's what it is:
1. a race and subrace are selected from a set list.
2. the subrace is a multiplier, so 100% is normal strength, 200% is double strength, Etc.
3. the final array is given to me, added into a database, and the user is sent into a battle against his enemy.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: RPG Game Code Optimization
20 May, 2008 - 10:39 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,173



Thanked: 208 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
The only suggestion I really have is that you take your calculations out of the array definition and do them before you stick them in the array. This will allow a couple advantages...

1) Quickly allow you to locate the formulas needed without having to dig through the array definition. There will be tweaking of those formulas no doubt, so you want to locate them quickly.

2) Shortens up the actual definition of the array so it may be easier to read for you.

3) Allows you to extend the formulas without having to modify the array definition. Maybe in the future the formula will grow into a full fledge function and instead of altering the array, you can call the function above, validate the result and let the result flow into the array definition.

Other than that, I see no real way to shorten and optimize this up. It is just defining the array. Now if it had been multi-step process of building up the array, then you may have optimization choices. Either way this line of code should be running rather efficient.

Hope that helps! smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:09AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month