CobInstance.cpp questions

CobInstance.cpp questions

Discuss the source code and development of Spring Engine in general from a technical point of view. Patches go here too.

Moderator: Moderators

Post Reply
User avatar
Dragon45
Posts: 2883
Joined: 16 Aug 2004, 04:36

CobInstance.cpp questions

Post by Dragon45 »

In trying to implement the rand function, I looked and decided that the best (only) place I could really find to implement it in was the CobInstance.cpp definition file.

So I added "#define RAND 21" (around line 66) after the rest of the defines, and around line 795, in the GetUnitVal member definition added the following:

Code: Select all

	case RAND:
		return ( ( CGlobalSyncedStuff::randInt() % (p2-p1+1) ) + p1 );
(This will return a number between the two arguments, inclusive, passed to the RAND function that is in the unit's cob file)

but I was wondeirng if this is the best place to put it. The GetUnitVal (and, I suspect, the rest of the class) deals with certain direct properties of units- STANDINGMOVEORDER, HEALTH, ARMORED, etc - are all variable properties of units specific to the units themselves. RAND(x,y), however, is not really a specific property of a unit, but rather, a sort of universal function that returns a certain value; its more like a ... well, something else entirely. It has no equivalent in TA scripting, not that I can think of.

So, I'm wondering: Is this the best place to put it? It somehow doesn't fit in with the current object tree. Or is there something i'm missing, or should I create a new class that is a part of the COB engine that includes this and other (future possible) "universal" functions?

Second thing I'm wondering is why in the GetUnitVal (and i believe the SetUnitVal) function some of the cases have breaks after them, and others do not. It's probably just some logic flow that I'm not understanding, but I just want to make sure.

Thanks

-Dragon45
Fnordia
Former Engine Dev
Posts: 425
Joined: 13 Aug 2004, 16:11

Post by Fnordia »

I think you are going about it in the wrong way.. Perhaps it is not obvious how it is designed though. :)

Anyway, GetUnitVal is a function called when the opcode GET_UNIT_VALUE (0x10042000) (and another one as well, the difference being the number of arguments to pop of the stack) is encountered. This is function used for a lot of various stuff, both finding out info about the unit such as health, and performing trigonometric calculations. The values we support are the ones found in exptype.h which comes from OTA. It is certainly possible to add new functions here, but you'd had to write a matching unit script.

The rand function used by OTA scripts is however not called by using GetUnitVal, but is in fact another opcode, 0x10041000. The opcode parsing and execution is performed in CobThread.cpp, the implementation for rand can be found around line 455.

I did notice now that the range arguments are supposed to be treated as inclusive, which they aren't right now. I'll fix that in cvs.

You can see that the rand function is indeed working by building a Pyro for example, and looking at the end of the barrel. It has some colored blocks that are toggled randomly, handled by the function PilotLight in the pyro script.

As for the break/return, are you wondering why not all cases are terminated by a break? Or why ACTIVATION and BUSY first return but then have a break after anyway? In the second case it's just a leftover from when they were implemented I guess, the breaks there have no effect.
User avatar
Dragon45
Posts: 2883
Joined: 16 Aug 2004, 04:36

Post by Dragon45 »

Ah, thanks for the speedy reply. It's very helpful :)

Yes, I was indeed going about it in the wrong way, and I thank you for clarifying before I wasted any more time on something that was already implemented, albeit incorrectly - the reason that myself (and others) thought that the RAND function was nonexistent in Spring was because of the fact that some third party units use something like

if ( rand(x,y) == y ) to execute certain actions, so when Y was never called, the action never executed. Thanks for cleaning that up :D

Also, thanks for clearing up the issues with the whole opcode business and CobThread; i guess my check of the class tree wasn't thorough enough :P

Finally, I was wondering if in the CVS, you could in the CobInstance.cpp file under Get and SetUnitVal, put breaks after the respective cases even if it really doesn't matter in terms of logic; it just makes the code look a lot cleaner and look less confusing :P
Post Reply

Return to “Engine”