Can someone have a look at my code please?
It is very frustrating when you need more time for trying to fix a bug then for coding the whole mod.
(My problem is that some units just fall down without being killed and I get an Error (Can not find unit to get position).)
What I was thinking my code is doing:
(most important things)
Create a group of monsters (called: daemons.units).
Create a array/table to store the unitIDs of monsters which reached the finisharea (near the castle).
Every tick glest should look for the position of the units and give the next waypoint to a unit when it reached a waypoint.
When a unit reaches the finisharea and there are more then one unit (in daemons.units) it is removed from the group.
When a unit is killed and not over the finisharea it is also removed from the group.
<?xml version="1.0" standalone="yes" ?>
<scenario>
<difficulty value="0"/>
<players>
<player control="human" faction="defence" team="1"/>
<player control="cpu" faction="attack" team="2"/>
<player control="closed"/>
<player control="closed"/>
</players>
<map value="glestTD_1"/>
<tileset value="forest"/>
<tech-tree value="td_1"/>
<default-resources value="false"/>
<default-units value="false"/>
<default-victory-conditions value="false"/>
<scripts>
<startup>
----------------------------------------------------------------------------
-- Unit Creation and Group Management --
----------------------------------------------------------------------------
-- creates a unit and puts its id in group
function createAndGroup ( group, type, faction, pos )
createUnit ( type, faction, pos );
group[#group+1] = lastCreatedUnit ();
end
-- create a group of num units of a single unit type
function groupOfType ( group, type, faction, num, pos )
for i=1,num do createAndGroup ( group, type, faction, pos ) end
end
-- create a group of units of different types
function groupFromTypes ( group, types, faction, pos )
for i=1,#types do createAndGroup ( group, types[i], faction, pos ) end
end
-- remove a unit id from a group
function removeFromGroup ( group, unitId )
for i= 1,#group do
if ( group[i] == unitId ) then
table.remove ( group, i );
--return
break;
end
end
end
-- checks if unit belongs to group
function unitIsinGroup ( group, unitId )
for i=1,#group do
if ( group[i] == unitId ) then
return true;
--break;
end
end
return false
end
<!-- -->
function cleanGroup ( group )
for i=1,#group do
table.remove ( group, 1 );
end
end
----------------------------------------------------------------------------
-- Group Commands --
----------------------------------------------------------------------------
-- give a group of units move commands to location
function groupMoveCommon ( units, location )
for i=1,#units do givePositionCommand ( units[i], "move", location ) end
end
-- give a group of units move commands to distinct locations
function groupMoveSpecific ( units, locations )
for i=1,#units do givePositionCommand ( units[i], "move", locations[i] ) end
end
-- give a group of units attack commands to location
function groupAttack ( units, pos )
for i=1,#units do givePositionCommand ( units[i], "attack", pos ) end
end
----------------------------------------------------------------------------
-- Geometry --
----------------------------------------------------------------------------
-- is pos in rectangle
function pointIsIn ( pos, rectangle )
if ( pos[1] >= rectangle[1] and rectangle[3] > pos[1]
and pos[2] >= rectangle[2] and rectangle[4] > pos[2] ) then
return true;
end
return false;
end
-- returns a rectangle around the point 'pos'
function areaAround ( pos )
return { pos[1]-5, pos[2]-5, pos[1]+5, pos[2]+5 };
end
----------------------------------------------------------------------------
-- Start-Up --
----------------------------------------------------------------------------
-- the patrol table will store the waypoints and regions around them
patrol = {};
patrol.waypoints = {{49,81}, {49,30}, {5,30}};
patrol.regions = {};
for i=1,#patrol.waypoints do
patrol.regions[#patrol.regions+1] = areaAround(patrol.waypoints[i]);
end
-- Make a group of 6 daemons...
disableAi (1);
daemons = {};
daemons.units = {};
groupOfType ( daemons.units, "daemon", 1, 6, patrol.waypoints[1] );
--disableAi(1)
createUnit ( "worker", 0, {30,40} );
createUnit ( "worker", 0, {15,20} );
createUnit ( "worker", 0, {20,20} );
createUnit ( "ground1", 0, {40,60} );
createUnit ( "ground1", 0, {60,60} );
createUnit ( "castle2", 0, {5,30} );
createUnit ( "hitman", 0, {100,20} );
createUnit ( "victim", 1, {94,20} );
setCameraPosition(startLocation(0));
giveResource('gold', 0, 2000);
giveResource('stone', 0, 300);
giveResource('wood', 0, 2000);
giveResource('food', 0, 500);
showMessage('Welcome', 'welcome')
tickCount = 0;
startTime = os.time();
finish=areaAround({15,30});
lives=100;
a=true;
wave=0;
overfinish = {};
</startup>
<unitDied>
--timeSinceStart = os.time() - startTime;
if(unitIsinGroup ( daemons.units, lastDeadUnit () ) ) then
setDisplayText("OVERFINISH" .. #overfinish); --hhhhhhhhhhhhhhhhhhhhhhhhh
if (not(unitIsinGroup ( overfinish, lastDeadUnit ()) )) then
giveResource('gold', 0, 500);
giveResource('stone', 0, 50);
giveResource('wood', 0, 100);
giveResource('food', 0, 50);
end
if ((#daemons.units) == 1) then
removeFromGroup ( daemons.units, lastDeadUnit ())
vitality =false;
diedat= tickCount;
--cleanGroup (daemons.units)
--cleanGroup (overfinish)
if (wave == 19) then
setDisplayText("This was the last wave");
if (lives > 0) then
setPlayerAsWinner(0)
endGame()
end
end
wave=wave+1;
end
if ((#daemons.units) > 1) then
removeFromGroup ( daemons.units, lastDeadUnit ())
end
end
if ( lastDeadUnitName() == "victim" ) then
a = not a;
if (a==true) then
x=94
else
x=106
end
createUnit ( "victim", 1, {x,20} );
tickCount = tickCount + 1;
if( (#daemons.units) >= 1 and (tickCount > 5)) then
for u=1,#daemons.units,1 do
if (not(unitIsinGroup ( overfinish, daemons.units[u]) )) then
local cur_pos = unitPosition(daemons.units[u]);
--check the position of units and give them their next waypoint
if ( pointIsIn(cur_pos, patrol.regions[1]) ) then
givePositionCommand(daemons.units[u], 'move', patrol.waypoints[2]);
elseif( pointIsIn(cur_pos, patrol.regions[2]) ) then
givePositionCommand(daemons.units[u], 'move', patrol.waypoints[3]);
--elseif( pointIsIn(cur_pos, patrol.regions[3]) ) then
--givePositionCommand(daemons.units[u], 'move', patrol.waypoints[4]);
--elseif( pointIsIn(cur_pos, patrol.regions[4]) ) then
--givePositionCommand(daemons.units[u], 'move', patrol.waypoints[4]);
end
--check if unit reached finisharea
if ( pointIsIn(cur_pos, finish) ) then
overfinish[#overfinish+1] = daemons.units[u];
lives=lives - 1;
setDisplayText("Amount of lives: " .. lives);
if (lives ==0) then
--we lost
--setDisplayText(lives);
clearDisplayText()
setPlayerAsWinner(1)
setPlayerAsWinner(2)
setPlayerAsWinner(3)
endGame()
end
end
end
end
--remove units which made it to the finisharea
for j=1, #overfinish,1 do
if (not(#overfinish==0 ))then
--if (not(#overfinish >= #daemons.units))then
if( (#daemons.units) > 1 ) then --let last unit in group -> when killed vitality becomes false
removeFromGroup ( daemons.units, overfinish[j])
end
--end
end
end
end
end
if ((vitality==false) ) then
if (((tickCount- diedat) == 15) and (wave==1)) then
vitality = true;
--cleanGroup (daemons.units)
cleanGroup (overfinish)
setDisplayText('warning1')
groupOfType ( daemons.units, "guard", 1, 6, patrol.waypoints[1] );
--groupMoveCommon ( daemons.units, patrol.waypoints[2] );
setDisplayText(#daemons.units); --hhhhhhhhhhhhhhhhhhhhhhhhh
elseif (((tickCount- diedat) == 15) and (wave==2)) then
vitality = true;
setDisplayText('warning2')
groupOfType ( daemons.units, "horseman", 1, 3, patrol.waypoints[1] );
groupMoveCommon ( daemons.units, patrol.waypoints[2] );
end
end
</unitDied>
</scripts>
</scenario>
The funny thing is when I turn line 300 into a commentline two of the gourads will fall down where not doing that forces
only one guard to fall down.
I was thinking if I remove all the elements of both array/tables before the 2nd wave comes it would be the same as at start but it seems that it doesn't matter.
@silnarm
You logged once my stuff. How can I do that? Is there a tut somewhere?