Author Topic: Glest tower defense  (Read 45293 times)

me5

  • Guest
Re: Glest tower defense
« Reply #100 on: 25 August 2009, 09:37:39 »
??? = three questionmarks

hailstone

  • GAE Team
  • Battle Machine
  • ********
  • Posts: 1,568
    • View Profile
Re: Glest tower defense
« Reply #101 on: 25 August 2009, 11:05:27 »
Is it possible to mix text from the XML file and the one from lua file.
Anything defined in the lua file should be usable after you call dofile(), at least within the same xml element, although I think it does span others.
Glest Advanced Engine - Admin/Programmer
https://sourceforge.net/projects/glestae/

me5

  • Guest
Re: Glest tower defense
« Reply #102 on: 25 August 2009, 16:43:18 »
Sorry, I ment the language file (myscenario_english.lng). ::)
What I want to do is just get rid of this three questionmarks when I try to output something on the screen.
When I do "setDispayText("this is my text")" I get this on the screen " ???this is my text??? ".
When I use a text which I define in the languagefile there are no questionmarks.
My problem is that I need to output a variable of my xml file followed by some text.

hailstone

  • GAE Team
  • Battle Machine
  • ********
  • Posts: 1,568
    • View Profile
Re: Glest tower defense
« Reply #103 on: 26 August 2009, 04:49:49 »
I don't think that's possible currently. Can you describe what you are trying to output?
Glest Advanced Engine - Admin/Programmer
https://sourceforge.net/projects/glestae/

Omega

  • MegaGlest Team
  • Dragon
  • ********
  • Posts: 6,167
  • Professional bug writer
    • View Profile
    • Personal site
Re: Glest tower defense
« Reply #104 on: 26 August 2009, 05:03:57 »
Hmm, try to use setDisplayText("hello"+a). However, the only way to remove the ??? is to change the source. The file glest_game\global\lang.cpp has the code you need. The simplest way is to find the following code:

Code: [Select]
string Lang::get(const string &s) const {
try {
      return strings.getString(s);
} catch (exception &) {
return "???" + s + "???";
}
}

string Lang::getScenarioString(const string &s){
try{
return scenarioStrings.getString(s);
}
catch(exception &){
return "???" + s + "???";
}
}
And change it to:
Code: [Select]
string Lang::get(const string &s) const {
try {
      return strings.getString(s);
} catch (exception &) {
return s;
}
}

string Lang::getScenarioString(const string &s){
try{
return scenarioStrings.getString(s);
}
catch(exception &){
return s;
}
}
A better way would be to check for an ini value first though, and if it is true, show the ???, and if false, don't show. The ini value could be something like languageStringDebug or something, and be defaultly OFF.

Military will use the above code, to ensure if I accidently miss something in the LNG, it won't display dirty text that no-one except the tester should see.
Edit the MegaGlest wiki: http://docs.megaglest.org/

My personal projects: http://github.com/KatrinaHoffert

me5

  • Guest
Re: Glest tower defense
« Reply #105 on: 26 August 2009, 07:19:17 »
So no way to do this without changing code in the cpp file? I thought it would be possible only to change things in my scenary file.
(I try to output a variable followed by some text for example: "Your nuber is: 3". Where "Your number is:" is a text and 3 is a integer variable from my scenaryfile.)

hailstone

  • GAE Team
  • Battle Machine
  • ********
  • Posts: 1,568
    • View Profile
Re: Glest tower defense
« Reply #106 on: 26 August 2009, 07:42:35 »
Perhaps if there was a function like defineScenarioString('nameofvariable', text) then they could be added and modified dynamically. I don't like the idea of not using the lang system but for some things it doesn't make sense so you want some way to purposefully add in texts that aren't intended to be translated. The other option would be to have a setRawDisplayText("string") function. I think with a debug option to disable/enable would defeat the purpose of having the question marks since you would have texts not intended for translation in with the ones that are but missing.

PS: My thousandth post.  ;D
« Last Edit: 26 August 2009, 07:47:14 by hailstone »
Glest Advanced Engine - Admin/Programmer
https://sourceforge.net/projects/glestae/

me5

  • Guest
Re: Glest tower defense
« Reply #107 on: 28 August 2009, 07:28:47 »
Ok here is my attempt of something TD-like (with your help)
Be aware this is a limitted version. One can download it only 10 times.
(tried to use filefront -> "Validating Data ....."  >:( )
(maybe someone could upload it again if needed)

http://rapidshare.com/files/272475529/glestTD.zip.html

positve:
it works

negative:
only one type of towers
you can not win  ;D

things I wished to have while making this scenario
-turn off the switching from day to night (is this a timer?)
-hurt/health option for units (giveHP(unitId) so one can kill,hurt or add some HP to units)
-some commands (Guard, Harvest, Patrol, and Repair)
-in the map editor: show current position of cursor (like: 10/50 (x-value/y-value))
-an option in the map editor where one can build objects units can run over but not build things on it

me5

  • Guest
Re: Glest tower defense
« Reply #108 on: 31 August 2009, 09:49:10 »
Is it possible to make a lightning-tower? As the name says the tower should emit lightnings which hit the monsters.

ElimiNator

  • Airship
  • ********
  • Posts: 3,391
  • The MegaGlest Moder.
    • View Profile
Re: Glest tower defense
« Reply #109 on: 31 August 2009, 13:41:40 »
Yes you can make them, in mater of fact there is one in by ballz tech.

see also the ballz topic here.

That tower you see with the ball on top is a lightning tower.
Get the Vbros': Packs 1, 2, 3, 4, and 5!

modman

  • Guest
Re: Glest tower defense
« Reply #110 on: 31 August 2009, 18:15:36 »
Rapid Share kind of stinks.  I will try to upload it to File Front myself, but hopefully you don't have those annoying svns...

me5

  • Guest
Re: Glest tower defense
« Reply #111 on: 1 September 2009, 16:13:32 »
No svns.
I hope you're more patient then me cause I gave up after 10 minutes (I think so) "Validating Data .....". :)

me5

  • Guest
Re: Glest tower defense
« Reply #112 on: 24 September 2009, 07:34:37 »
After making a new tower I realised that my code is still crappy so here is my new problem.
When two units are killed at the same time only one is removed from the group. I also tried to save the unitid of every dead unit in an array but it did not work maybe I do it the wrong way. It seems like glest only output one unitid when two units died at the same time.
 here is my code:

Code: [Select]

<?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;
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;

end
  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 8 daemons...
disableAi (1);
daemons = {};
daemons.units = {};
groupOfType ( daemons.units, "initiate", 1, 5, 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 ( "ground2", 0, {40,56} );
--createUnit ( "ground1", 0, {40,52} );
--createUnit ( "ground2", 0, {40,48} );
--createUnit ( "frosttower", 0, {40,44} );

createUnit ( "ground1", 0, {60,60} );
--createUnit ( "ground2", 0, {60,56} );
--createUnit ( "ground1", 0, {60,52} );
--createUnit ( "ground2", 0, {60,48} );
--createUnit ( "defense_tower", 0, {60,44} );


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 = {};
dead = {}

</startup>

 
<unitDied>

--timeSinceStart = os.time() - startTime;

if(unitIsinGroup ( daemons.units, lastDeadUnit () ) ) then
dead[#dead+1] = lastDeadUnit ()
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 ())
for j=1, #overfinish do
table.remove ( overfinish, j );
end
setDisplayText(#daemons.units); --hhhhhhhhhhhhhhhhhhhhhhhhh
vitality =false;
diedat= tickCount;


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
for j=1, #dead do
if (not(#dead==0 ))then

if( (#daemons.units) > 1 ) then --let last unit in group to bekilled ->vitality becomes false
removeFromGroup ( daemons.units, dead[j])


end
end
end
setDisplayText(#daemons.units); --hhhhhhhhhhhhhhhhhhhhhhhhh

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 do

--if (not(unitIsinGroup ( overfinish, daemons.units[u]) )) then
local cur_pos = unitPosition(daemons.units[u]);

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






if ( pointIsIn(cur_pos, finish) ) then
if (not(unitIsinGroup ( overfinish, daemons.units[u]) ))then
overfinish[#overfinish+1] = daemons.units[u];
lives=lives - 1;

setDisplayText("Amount of lives: " .. lives);
end

if (lives ==0) then
--we lost
--setDisplayText(lives);

clearDisplayText()
setPlayerAsWinner(1)
setPlayerAsWinner(2)
setPlayerAsWinner(3)
endGame()

end
end
end

for j=1, #overfinish do
if (not(#overfinish==0 ))then

if( (#daemons.units) > 1 ) then --let last unit in group to bekilled ->vitality becomes false
removeFromGroup ( daemons.units, overfinish[j])


end


end
end





end

end





if ((vitality==false) ) then
if (((tickCount- diedat) == 15) and (wave==1)) then
vitality = true;
setDisplayText('warning1')
groupOfType ( daemons.units, "guard", 1, 5, 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>


here is the whole problem: http://www.filefront.com/14590423/td_1.7z

silnarm

  • GAE Team
  • Behemoth
  • ********
  • Posts: 1,373
    • View Profile
Re: Glest tower defense
« Reply #113 on: 24 September 2009, 09:45:21 »
After making a new tower I realised that my code is still crappy so here is my new problem.
When two units are killed at the same time only one is removed from the group. I also tried to save the unitid of every dead unit in an array but it did not work maybe I do it the wrong way. It seems like glest only output one unitid when two units died at the same time.

Hmmm.... well the unitDied event should definitely be firing for each and every unit. I did actually move the 'call' that makes this happen in GAE, because units dying from degeneration weren't triggering it, so I guess it's possible Glest is missing some other cases too, but I think units killed in the 'normal' way should all trigger unitDied events.

So, unfortunately the problem is probably in your code ;) ... and there's a bit of it I see! I'll have to get back to you... hopefully on the weekend...
Glest Advanced Engine - Code Monkey

Timeline | Downloads

Omega

  • MegaGlest Team
  • Dragon
  • ********
  • Posts: 6,167
  • Professional bug writer
    • View Profile
    • Personal site
Re: Glest tower defense
« Reply #114 on: 24 September 2009, 16:26:27 »
BTW, in a similar note, I noticed GAE wouldn't detect some builds at high speed. This stumped me for a while, since I don't like to test my scenarios at a snail pace, especially if I end up playing it like 7 times.

Ugh...
Edit the MegaGlest wiki: http://docs.megaglest.org/

My personal projects: http://github.com/KatrinaHoffert

silnarm

  • GAE Team
  • Behemoth
  • ********
  • Posts: 1,373
    • View Profile
Re: Glest tower defense
« Reply #115 on: 25 September 2009, 03:46:12 »
BTW, in a similar note, I noticed GAE wouldn't detect some builds at high speed. This stumped me for a while, since I don't like to test my scenarios at a snail pace, especially if I end up playing it like 7 times.

Ugh...

This can't be the case.  It might be missing some some-how, but not because of the game speed.

Death by degeneration wasn't triggering unitDied, do any of the buildings have HP regen? that may not be triggering unitCreated events... I'll have a look tonight, but I think that again, for most normal situations it should be working...
Glest Advanced Engine - Code Monkey

Timeline | Downloads

Omega

  • MegaGlest Team
  • Dragon
  • ********
  • Posts: 6,167
  • Professional bug writer
    • View Profile
    • Personal site
Re: Glest tower defense
« Reply #116 on: 25 September 2009, 21:25:08 »
It only affected defensive buildings with low health (not sure what, but they ranged from 2000 [MGT] to 2500 [howitzer]).

Larger health buildings like the outpost (7000) seemed fine.

I was using a <unitCreatedOfType type="x"> code.

Must have something to do with that low health!
Edit the MegaGlest wiki: http://docs.megaglest.org/

My personal projects: http://github.com/KatrinaHoffert

silnarm

  • GAE Team
  • Behemoth
  • ********
  • Posts: 1,373
    • View Profile
Re: Glest tower defense
« Reply #117 on: 6 October 2009, 06:27:14 »
Ok, sorry for the delay, my hope of having my new error handling and reporting in GAE solving this one for me didn't come to fruition.  So I started logging stuff..

Code: [Select]
Log file

37: unidDied, id=0 : initiate
37: Group before unitDied [5] 0, 1, 2, 3, 4
37: unit IS in badGuys group
37: Group after unitDied [4] 1, 2, 3, 4
37: 
39: unidDied, id=4 : initiate
39: Group before unitDied [4] 1, 2, 3, 4
39: unit IS in badGuys group
39: Group after unitDied [3] 1, 2, 3
39: 
39: unidDied, id=2 : initiate
39: Group before unitDied [3] 1, 2, 3
39: unit IS in badGuys group
39: Group after unitDied [2] 1, 3
39: 
40: unidDied, id=3 : initiate
40: Group before unitDied [2] 1, 3
40: unit IS in badGuys group
40: Group after unitDied [1] 1
40: 
47: unidDied, id=1 : initiate
47: Group before unitDied [1] 1
47: unit IS in badGuys group
47: Group after unitDied [0]
47: 
63: Group created [5] 47, 48, 49, 50, 51
102: unidDied, id=48 : guard
102: Group before unitDied [1] 50
102: unit IS NOT in badGuys group
102: Group after unitDied [1] 50
102: 
102: unidDied, id=50 : guard
102: Group before unitDied [1] 50
102: unit IS in badGuys group
102: Group after unitDied [0]
102: 
103: unidDied, id=51 : guard
103: Group before unitDied [0]
103: unit IS NOT in badGuys group
103: Group after unitDied [0]
103: 
104: unidDied, id=49 : guard
104: Group before unitDied [0]
104: unit IS NOT in badGuys group
104: Group after unitDied [0]
104: 
104: unidDied, id=47 : guard
104: Group before unitDied [0]
104: unit IS NOT in badGuys group
104: Group after unitDied [0]
104: 
121: unidDied, id=110 : horseman
121: Group before unitDied [3] 109, 110, 111
121: unit IS in badGuys group
121: Group after unitDied [2] 109, 111
121: 
127: unidDied, id=109 : horseman
127: Group before unitDied [2] 109, 111
127: unit IS in badGuys group
127: Group after unitDied [1] 111
127: 

So the problem is with the Guards group, somehow the group is getting culled before any of them die.

I suspect this may be the problem:
Code: [Select]
for j=1, #overfinish do
if( (#daemons.units) > 1 ) then --let last unit in group to bekilled ->vitality becomes false
removeFromGroup ( daemons.units, overfinish[j])
end
end

That's from the 'tick' event... the gaurds all seem to make it the 'finish' area, at which point on the next tick they are removed from their group, without having died...

At least that's what I think is happening... don't have time to experiment more atm, and I didn't have time to figure out exactly what you were trying to do with everything... Hope this helps! Good luck.
Glest Advanced Engine - Code Monkey

Timeline | Downloads

me5

  • Guest
Re: Glest tower defense
« Reply #118 on: 6 October 2009, 15:58:41 »
Are units killed when I remove them from their group? Do I not only remove the id of the unit from the array/table?
Are there other ways to kill a unit besides attacking by another unit?

silnarm

  • GAE Team
  • Behemoth
  • ********
  • Posts: 1,373
    • View Profile
Re: Glest tower defense
« Reply #119 on: 9 October 2009, 11:50:56 »
Are units killed when I remove them from their group? Do I not only remove the id of the unit from the array/table?
Are there other ways to kill a unit besides attacking by another unit?

Units are not killed when you remove them from their group, no, the groups exist strictly in the Lua.  But you are removing the guard's from their group when they get to the 'finish' area, before they die.  Then trying to remove them from the group when they do die... hence your problem.
Glest Advanced Engine - Code Monkey

Timeline | Downloads

me5

  • Guest
Re: Glest tower defense
« Reply #120 on: 18 October 2009, 17:30:30 »
Sorry silnarm I still don't get it.  ::)
First I thougt that the guards are killed at the same time by the tower but now I think that they only die (falling down on the ground without a reason) because another unit reaches the finisharea or is killed by the tower.
I removed some lines from my code but the problem is the same (I think)
Code: [Select]
<?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;

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;



end

  end

end

               

<!--    -->

function cleanGroup ( group )

for i=1,#group do

table.remove ( group, i );

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 8 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



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 ())

for j=1, #overfinish do

table.remove ( overfinish, j );

end

setDisplayText(#daemons.units); --hhhhhhhhhhhhhhhhhhhhhhhhh

vitality =false;

diedat= tickCount;





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 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







if ( pointIsIn(cur_pos, finish) ) then

if (not(unitIsinGroup ( overfinish, daemons.units[u]) ))then

overfinish[#overfinish+1] = daemons.units[u];

lives=lives - 1;

setDisplayText("Amount of lives: " .. lives);

end



if (lives ==0) then

--we lost

--setDisplayText(lives);



clearDisplayText()

setPlayerAsWinner(1)

setPlayerAsWinner(2)

setPlayerAsWinner(3)

endGame()



end

end

end



--remove units which made it to finisharea

for j=1, #overfinish do

if (not(#overfinish==0 ))then

--if (not(#overfinish >= #daemons.units))then

if( (#daemons.units) > 1 ) then --let last unit in group to bekilled ->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)

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>

If a unit reaches the finisharea it should be removed from the group but not again when the same unit dies.
I was thinking that this line would prevent the unit to be removed a second time.
Code: [Select]
if(unitIsinGroup ( daemons.units, lastDeadUnit () )  ) then

(If the unit reached the finisharea it is removed from the daemons.units table/array --> woun't be removed a second time cause it is not in the group anymore)

me5

  • Guest
Re: Glest tower defense
« Reply #121 on: 12 November 2009, 08:34:15 »
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.

Code: [Select]
<?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?

silnarm

  • GAE Team
  • Behemoth
  • ********
  • Posts: 1,373
    • View Profile
Re: Glest tower defense
« Reply #122 on: 12 November 2009, 08:53:32 »
Hey, sorry dude, missed that last one... will have another look at what you've done, but with the script getting so large, my next response might be mostly on how to restructure/organise it for further develepment...
Glest Advanced Engine - Code Monkey

Timeline | Downloads

me5

  • Guest
Re: Glest tower defense
« Reply #123 on: 12 November 2009, 14:38:24 »
No problem, everything that farthers my mod/code would be nice. ;D

Ligerteeth

  • Guest
Re: Glest tower defense
« Reply #124 on: 15 November 2009, 08:15:40 »
i have a sort of hackish solution to doing this without a scenario. Have the attacking units have an absurdly high sight value, 200 or something, so that as soon as the spawn they rush toward the enemy troops/base. For the "defensive archer" sort of thing, make them basically defense towers that look like people. Add resource management and building or not or as you wish and enjoy :)

 

anything