Outpost Universe Forums

Projects & Development => Outpost 2 Programming & Development => Topic started by: Sirbomber on April 12, 2010, 08:12:09 PM

Title: Simple Question
Post by: Sirbomber on April 12, 2010, 08:12:09 PM
I really dunno how to word this question... Basically, instead of doing this:
Code: [Select]
if (unitWeapon == mapLaser || unitWeapon == mapRailGun || ...)
I want to do something like this:
Code: [Select]
if (unitWeapon == anEdenWeapon)
Where that will return true if the unit's weapon is a weapon available to Eden.

I'm pretty sure this is doable (and relatively easy), I just forget how...
Title: Simple Question
Post by: Hooman on April 12, 2010, 11:15:57 PM
I don't know of anything built in. You probably just have to write your own function.

Code: [Select]
bool IsEdenWeapon(weaponType)
{
  switch(weaponType)
  {
    case mapLaser:
    case mapRailGun:
    // ...
    return true;  // Use switch case fall through (no "break" statement)
  }

  // Nope
  return false;
}



if (IsEdenWeapon(weaponType))
{
  // Eden weapon type code goes here
}


// Or for the alternate case
if (!IsEdenWeapon(weaponType))
{
  // Non-Eden weapon type code goes here
}
Title: Simple Question
Post by: Sirbomber on April 13, 2010, 06:56:50 AM
Hmm, I could've sworn there was something that could do that...  But I guess not.  Well, thanks anyways.
Title: Simple Question
Post by: Hooman on April 13, 2010, 09:51:21 PM
Well, there is now. I thought that would be useful generic functionality, so I added some functions to the OP2Helper project.

Code: [Select]
bool IsEdenOnlyWeapon(map_id weaponType);
bool IsPlymouthOnlyWeapon(map_id weaponType);
bool IsCommonWeapon(map_id weaponType);
bool IsEdenWeapon(map_id weaponType);  // Common or Eden only
bool IsPlymouthWeapon(map_id weaponType);  // Common or Plymouth only
Title: Simple Question
Post by: Sirbomber on April 13, 2010, 10:50:51 PM
Ah, well, that works.

I think I was thinking of an enum, which kinda does what I wanted but not really...  Oh well.  Irrelevant now.
Title: Simple Question
Post by: Hooman on April 14, 2010, 03:37:57 AM
Enums are basically just named constants. They don't really provide any if shortcuts. At least not in C++. If you arrange the constants nicely though, you can get some degree of shortcuts. But we're dealing with an already specified set of constants which we can't reorder, and they're not ordered nicely.

If you could reorder constants to keep releated ones consecutive, you can do something like:
Code: [Select]
if (weaponType >= mapLaser && weaponType <= mapAcidCloud)
{
  // ....
}

Mind you, you might want to add secondary constants in a case like that, such as mapEdenFirstWeapon, mapEdenLastWeapon. It would make the if statement slightly clearer, and the needed additions to the enum would be fairly obvious in meaning.
Code: [Select]
enum map_id
{
  // ...

  mapEdenFirstWeapon,
  mapLaser = mapEdenFirstWeapon,
  // ...
  mapAcidCloud,
  mapEdenLastWeapon = mapAcidCloud,

  // ...
};

I'm not sure if you can use the enum constants within the enum declaration though, so the values on the right of the = might need to be actual numbers.

But yeah, irrelevant.
 
Title: Simple Question
Post by: Moley on April 14, 2010, 06:25:09 AM
don't know if your code was supposed to work...
but you have an assignment operator backwards...
Title: Simple Question
Post by: Hooman on April 15, 2010, 03:07:46 AM
Where?
 
Title: Simple Question
Post by: Hidiot on April 15, 2010, 05:41:55 AM
mapLaser = mapEdenFirstWeapon
Title: Simple Question
Post by: Hooman on April 15, 2010, 11:27:57 PM
Well, it can't be the other way around if you declare the one on the right first.

I just thought I'd put the range delimiting ones on the outside, kind of like parenthesis.