Tiberium code help

Tiberium code help

Discuss game development here, from a distinct game project to an accessible third-party mutator, down to the interaction and design of individual units if you like.

Moderator: Moderators

Post Reply
User avatar
code_man
Posts: 260
Joined: 19 Jan 2014, 13:10

Tiberium code help

Post by code_man »

I want to mimic command and conquers tiberium system.

So far i have these problems.

1) Despite adding the harvestStorage tag to my unit (a reclaim only), it simply reclaims the tiberium and returns it right back to the pool of metal.

2) AllowCommand does not prevent non-harvesters from reclaiming tiberium features.
harvesters cant reclaim anything at all, but only with non-recursive commands, recursively it reclaims anything.

Code: Select all

function gadget:AllowCommand (unitID, unit_def, unitTeam, cmdID, cmdParams, cmdOptions, cmdTag, synced)
    if cmdID == CMD.RECLAIM and #cmdParams == 1 then
        if UnitDefs[unit_def].name == "harvester" then
            if cmdParams[1] > Game.maxUnits and FeatureDefs[cmdParams[1] - Game.maxUnits] then
                local fid = cmdParams[1] - Game.maxUnits
                local fd = FeatureDefs[fid]
                --assert (fd)
                if fd.name == "tiberium" then
                    return true
                end
            end
            return false
        end
        if cmdParams[1] > Game.maxUnits and FeatureDefs[cmdParams[1] - Game.maxUnits].name == "tiberium" then
            return false
        end
    end
    return true
end
The assert stuff was told me to put in in #moddev but i have no idea why really, someone elaborate please.

3) I need a recalim catcher for builders so they can clear area for construction without returning the resources reclaimed to the resource pool.

4) Harvester idle harvesting code causes them to freeze and not accept new orders.

Code: Select all

function gadget:UnitIdle (unit, unitDef, unitTeam)
    if UnitDefs[unitDef].name == "harvester" then
       if true then -- add button to toggle auto harvesting
           local x, y, z = Spring.GetUnitPosition (unit)
           GG.Delay.DelayCall (Spring.GiveOrderToUnit, {unit, CMD.PATROL, {x, y, z}, {CMD.OPT_SHIFT}}, 1)
       end
    end
end
5) I dont have much of an idea on how to have blossom trees spawn in middle of all large metal spots.
Right now tiberium crystals get spawned on all metal spots and automatically respwaned after 10m.

6) I have no idea how i can effectively make tiberium spread, not to mention having it be done without lagging up the game.

7) Im not sure if i should make tiberium to units and give a weapon to them to mimic infantry damage from tiberium.
Or is there a better way?

8) I would want refineries and silos to be able to keep track how much resource they contain so if they explode they could give proprtionate tiberium in the area.
Is this possible with using the new harvester tags too? Or must i assign each one an implicit counter?
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7049
Joined: 16 Nov 2004, 13:08

Re: Tiberium code help

Post by zwzsg »

local fd = FeatureDefs[fid]
assert (fd)
-- is to be sure the featuredef was got, and would throw an error otherwise.

assert is useful to debug, it prevents you from running code based on false assumptions.

As for 7, I would use Spring.AddUnitDamage
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6240
Joined: 29 Apr 2005, 01:14

Re: Tiberium code help

Post by FLOZi »

You are using Spring 98.0?

Try reclaiming a unit rather than a feature
User avatar
code_man
Posts: 260
Joined: 19 Jan 2014, 13:10

Re: Tiberium code help

Post by code_man »

Sorry for delay, i got hung up on some projects of mine and 98, but i guess most of you were too.

@ zwzsg
Il keep that in mind, thanks.
FLOZi wrote:You are using Spring 98.0?

Try reclaiming a unit rather than a feature
Yeah im on 98 alright and also i dont know what that has to do with anything but well i tried and it doesnt give anything back at all, strange.
While if i reclaim a unit with the conyard it does give back cash.

I also have another problem that i need help with: my tiberium feature uses block = false, now the problem is it allows the player to build over the tiberium and i cant have that obviously.

I really need help with points 1-4, otherwise my game wont get too far.
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

Re: Tiberium code help

Post by Silentwings »

I can't help much with (1) and to some extent (2) because I know nothing about harvesting or C&C. If you explain precisely what you are wanting to happen in a self contained way then I might be of some use. I don't know if others are in the same position.

With (3), it's not clear to me what you mean by "reclaim catcher" and consequently I am confused about what you want. Typically you can give a build command on an area containing reclaimable features and the engine will then tell the unit in question to reclaim those features before building (so is the problem that your code in (2) prevents this?!).

With (2) and (4), you are asking things that rely on a lot of code/defs beyond what you've posted above so probably seeing the whole game is needed to debug those. E.g. there is nothing visible in (4) that would cause units to freeze or reject future orders.

For (6), you could e.g. use gadget:GameFrame(n) and just bail out if e.g. n%30~=0. If n%30==0 then GetAllFeatures, for each returned feature that is a tiberium have some low probability of spawning another somewhere random inside a small circle around it, if you can find space.

For (5), I suggest saving it for later. You'll probably end up wanting to modify the metalspot finding code in e.g. http://imolarpg.dyndns.org/trac/balates ... finder.lua
harvesters cant reclaim anything at all, but only with non-recursive commands,
I don't know what you mean by a "recursive command".
I also have another problem that i need help with: my tiberium feature uses block = false, now the problem is it allows the player to build over the tiberium and i cant have that obviously.
http://imolarpg.dyndns.org/trac/balates ... _mines.lua is likely what you are looking for here.
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: Tiberium code help

Post by jK »

1) Despite adding the harvestStorage tag to my unit (a reclaim only), it simply reclaims the tiberium and returns it right back to the pool of metal.
It's very bugged in 98, currently working on the patch (it will also add the ability to harvest energy).
User avatar
code_man
Posts: 260
Joined: 19 Jan 2014, 13:10

Re: Tiberium code help

Post by code_man »

jK wrote:It's very bugged in 98, currently working on the patch (it will also add the ability to harvest energy).
Oh bummer, well i guess i can forget about point 1 for the time being atleast.
Dont haste for me if you would, my game is nowhere near releasable.
Silentwings wrote:I can't help much with (1) and to some extent (2) because I know nothing about harvesting or C&C. If you explain precisely what you are wanting to happen in a self contained way then I might be of some use. I don't know if others are in the same position.
Well you prolly have heard that the first 3 parts of C&C are now freeware, i suggest you play them, they rule.
Anyway back to biz.
So about point 2: the thing is want my harverster units to reclaim only special marked features called tiberium.
Now the code i posted prevents them from reclaiming anything, tiberum included if i issue a direct reclaim order on a specific feature.
But if i issue an area reclaim, it will reclaim any feature.
I don't know what you mean by a "recursive command".
I was told that AllowCommand when called on an area recurses itself on any single thingie in the area that orders will be issued on, obviously this seems not to be the case, unless i severely fucked it up.

As you may guess from the code i want the revrse to be possible for builders, to reclaim any features that are not tiberium too.
With (3), it's not clear to me what you mean by "reclaim catcher" and consequently I am confused about what you want. Typically you can give a build command on an area containing reclaimable features and the engine will then tell the unit in question to reclaim those features before building (so is the problem that your code in (2) prevents this?!).
So point 3 is related to point 2.
I need builders to be able to reclaim features to allow for construction, but i want them to not give back the reclaimed resource.
But i cant just disable reclamation resources all along because i still need harversters to be able to harvest and from what i could gather this is a form of reclaim.
With (2) and (4), you are asking things that rely on a lot of code/defs beyond what you've posted above so probably seeing the whole game is needed to debug those. E.g. there is nothing visible in (4) that would cause units to freeze or reject future orders.
My game has very few code, im positive its that.
But if you need to see something specific, i could post it.
For (6), you could e.g. use gadget:GameFrame(n) and just bail out if e.g. n%30~=0. If n%30==0 then GetAllFeatures, for each returned feature that is a tiberium have some low probability of spawning another somewhere random inside a small circle around it, if you can find space.

For (5), I suggest saving it for later. You'll probably end up wanting to modify the metalspot finding code in e.g. http://imolarpg.dyndns.org/trac/balates ... finder.lua
Il prolly wait with those.
http://imolarpg.dyndns.org/trac/balates ... _mines.lua is likely what you are looking for here.
Il check it out.
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

Re: Tiberium code help

Post by Silentwings »

Well you prolly have heard that the first 3 parts of C&C are now freeware, i suggest you play them, they rule.
Thanks, but thats not going to happen.
the thing is want my harverster units to reclaim only special marked features called tiberium.
Now the code i posted prevents them from reclaiming anything, tiberum included if i issue a direct reclaim order on a specific feature.
As said above, I rate this as not possible to debug without direct access to your game.
But if i issue an area reclaim, it will reclaim any feature.
Your code checks the number of cmdParams. CMD.RECLAIM is this http://springrts.com/wiki/Lua_CMDs#CMDT ... RE_OR_AREA type of command. If you give a command to reclaim a single unit or feature then #cmdParams==1 and your code functions as you expect. An area reclaim command will have #cmdParams==4, which your code does not check for.
I was told that AllowCommand when called on an area recurses itself on any single thingie in the area that orders will be issued on,
This is generally not true, as you can see from the variety of things on http://springrts.com/wiki/Lua_CMDs. To use that page: every CMD has an associated CMDTYPE, and this, combined with the number of cmdParams (and, to distinguish unitIDs from featureIDs, whether or not the ID is >=Game.maxUnits) tells the engine how to interpret those params. The CMD and CMDTYPE tables are both accessible from lua.
I need builders to be able to reclaim features to allow for construction, but i want them to not give back the reclaimed resource. But i cant just disable reclamation resources all along because i still need harversters to be able to harvest and from what i could gather this is a form of reclaim.
Allow the command and put some code into gadget:AllowFeatureBuildStep to control whether or not it actually recovers the resources. See e.g. http://imolarpg.dyndns.org/trac/balates ... ix.lua#L33 for an example of how reclaim mechanics can be modified.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6240
Joined: 29 Apr 2005, 01:14

Re: Tiberium code help

Post by FLOZi »

code_man wrote:
FLOZi wrote:You are using Spring 98.0?

Try reclaiming a unit rather than a feature
Yeah im on 98 alright and also i dont know what that has to do with anything but well i tried and it doesnt give anything back at all, strange.
While if i reclaim a unit with the conyard it does give back cash.
Sorry I wasn't clear - in 98 harvestStorage will work only with units, not features (if at all). This is why it didn't give anything back - it went into harvest storage.

Anywho, await jK's improvements. :)
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: Tiberium code help

Post by jK »

FLOZi wrote:Anywho, await jK's improvements. :)
done
User avatar
code_man
Posts: 260
Joined: 19 Jan 2014, 13:10

Re: Tiberium code help

Post by code_man »

Well i was a bit lazy but now i try to get back at it.

I havent touched harvester auto-reclaim stuff, but its not even needed at this point.
Silentwings wrote: This is generally not true, as you can see from the variety of things on http://springrts.com/wiki/Lua_CMDs. To use that page: every CMD has an associated CMDTYPE, and this, combined with the number of cmdParams (and, to distinguish unitIDs from featureIDs, whether or not the ID is >=Game.maxUnits) tells the engine how to interpret those params. The CMD and CMDTYPE tables are both accessible from lua.
So what i must do is to get every single feature within the given area and checn and replace each reclaim order with said feature instead?
Allow the command and put some code into gadget:AllowFeatureBuildStep to control whether or not it actually recovers the resources. See e.g. http://imolarpg.dyndns.org/trac/balates ... ix.lua#L33 for an example of how reclaim mechanics can be modified.
I looked at this, but i dont undestand how this is supposed to work.
Or can i make reclaim give back 0 resources and only catch defined special resource reclemation to give back its amount?

Another new problem i am facing is i want tiberium placement not to happen on filled spots, but the following code fails entirely.

Code: Select all

local function PlaceResources (resource_list)
    local i = 0
    local j = 0
    local feature
    while (i < Game.mapSizeX / 16) do
        j = 0
        while (j < Game.mapSizeZ / 16) do
            if (Spring.GetMetalAmount (i, j) > 0) and (Spring.GetGroundHeight (i * 16, j * 16) > 0) and (Spring.GetUnitsInRectangle (i * 16, j * 16, i * 16 + 15, j * 16 + 15) == nil) and (Spring.GetFeaturesInRectangle (i * 16, j * 16, i * 16 + 15, j * 16 + 15) == nil) then
                feature = Spring.CreateFeature (resource_list[1], i * 16, 0, j * 16)
                Spring.SetFeatureDirection (feature, math.random (0, 359), 0, 90)
            end
            j = j + 1
        end
        i = i + 1
    end
end
The problem being GetUnitsInRectangle and/or GetFeaturesInRectangle, but i have no idea what could be wrong.
They should return nil if no features/units are found in said area right? Spots are not blocked so they should.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6240
Joined: 29 Apr 2005, 01:14

Re: Tiberium code help

Post by FLOZi »

Can't assume the result is nil - could be other map features or corpses present?
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Tiberium code help

Post by knorke »

I think GetFeaturesInRectangle & friends never return nil. maybe for invalid coordinates?
If there are no things in their area they seem to return {} (empty table) which is not the same as nil.
I tested like:

Code: Select all

    local i = 0
    local j = 0    
    while (i < Game.mapSizeX / 16) do
        j = 0
        while (j < Game.mapSizeZ / 16) do
            if (Spring.GetMetalAmount (i, j) > 0) and (Spring.GetGroundHeight (i * 16, j * 16) > 0)  then
                Spring.Echo (j,i)
				local features = Spring.GetFeaturesInRectangle (i * 16, j * 16, i * 16 + 15, j * 16 + 15)
				Spring.MarkerAddPoint (i*16,500, j*16, #features)
            end
            j = j + 1
        end
        i = i + 1
    end
it counts the features on metal spots and puts marker: (notice the two 1's)
Image

So instead of
if Spring.GetFeaturesInRectangle() == nil
do like
if #Spring.GetFeaturesInRectangle() ==0

If GetFeaturesInRectangle really returned nil, then above example would error but it does not.

---
Maybe already noticed how ugly it is to have the i*16, j*16 everywhere.
Better do /16 for the metalmap-coordinates and for the rest can just use i,j as coordinates.
And why not x,z as coordinates name? ;)
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7049
Joined: 16 Nov 2004, 13:08

Re: Tiberium code help

Post by zwzsg »

I like to use constructions such as:
if #(Spring.GetFeaturesInRectangle() or {})==0
for cases where I'm not fully certain the function won't ever return nil
may be a bit Out Of Topic, sorry
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Tiberium code help

Post by knorke »

With userinput or reading files or other "dangerous things" one can not be cautious enough but some things one should trust.
Otherwise it looks silly and reminds me of this:
Image

/edit: clueless looking at things:
https://github.com/spring/spring/blob/d ... .cpp#L2420
So there is this lua_State* L or something.
And here is ProcessFeatures, which is called by all the GetXinY functions.
https://github.com/spring/spring/blob/d ... .cpp#L2391
Some Lua table is created with lua_createtable(L, featureCount, 0); ?
Then some featureIDs get pushed into the table with lua_pushnumber(L, feature->id);
But even if nothing gets pushed into the table - the table is still there?
So I do not see how GetFeaturesInRectangle could return nil?
User avatar
code_man
Posts: 260
Joined: 19 Jan 2014, 13:10

Re: Tiberium code help

Post by code_man »

That did the trick, thanks for the advice people.

I tried with too == {}, but oddly enough this does not work while # == 0 does, why does this not work?
Also i tried with nil because the wiki said that so assumed that it was that when i first wrote this.

With this out of my head, my primary concern remains to intercept resources during reclemation, i still have no real idea how to do this.

Also, i noticed that modrules has no parameter for actual feature reclemation ratio, perhaps it would be useful to have one.

Plus i still dont know how i can allow an area reclemation command effectively, recursing on each feature whetever to allow it or not.
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: Tiberium code help

Post by gajop »

== compares tables by reference, and you usually don't want that.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Tiberium code help

Post by knorke »

jK removed the "returns nil" from wiki:
http://springrts.com/mediawiki/index.ph ... ldid=29054
I tried with too == {}, but oddly enough this does not work while # == 0 does, why does this not work?

Code: Select all

a = {}
b = {}
if (a==b) then *this will be FALSE*

Code: Select all

a = {}
a = b
if (a==b) then *this will be TRUE*
http://www.luafaq.org/gotchas.html#T9 explains better but with more technobabble

Also, i noticed that modrules has no parameter for actual feature reclemation ratio
It is instead per unit and per feature, for example reclaimTime
http://springrts.com/wiki/Gamedev:Featu ... _Resources
User avatar
code_man
Posts: 260
Joined: 19 Jan 2014, 13:10

Re: Tiberium code help

Post by code_man »

Another lua trap i stepped in, thanks for hint il sure need it in the future.

@Knorke, i was more meaning something like http://springrts.com/wiki/Modrules.lua#reclaim unitEfficiency, but for features rather than units.

Also please help me with the other 2 said problems, i really need those to or my game is going to be very hackish.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Tiberium code help

Post by knorke »

@Knorke, i was more meaning something like http://springrts.com/wiki/Modrules.lua#reclaim unitEfficiency, but for features rather than units.
Does not seem to exist for historical reasons.

The other 2 questions are with commands & resources, without testing can only give some rough direction.

intercept resources during reclemation:
Guess would be something like this:
http://springrts.com/wiki/Lua:Callins :arrow: AllowFeatureBuildStep
But from wiki text it seems that the return value completly blocks reclaim progress instead of just blocking resources.
If you want the feature to be reclaimed but give no resources, then maybe you have to instantly steal the resources with some resource-related function (use ctrl f) from http://springrts.com/wiki/Lua_SyncedCtrl
Seems bit silly, not sure if there is better way.
You could also set all features to 0 metal & 0 energy but long enough reclaimTime that they do not instantly disappear. Then the reclaiming gives no resources at all and you use resource-related functions to set resources without having to worry about what the engine does?

area reclemation command effectively:
Test around a bit what the parameters are in AllowCommand or look for examples...
Post Reply

Return to “Game Development”