forum.webdiplomacy.net

webDip dev coordination forum / public access todo list
It is currently Wed Dec 13, 2017 3:43 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Sun May 08, 2011 6:59 am 
Offline

Joined: Wed Jul 29, 2009 10:22 am
Posts: 841
Hi.

for a variant I need to know what Coasts a territory has (basically the reverse of the ParentCoastID.)
All the data is there, but I have no clue how to get it in JavaScript.
At the moment I do a "switch" command and add them all "by hand", but I'd like to do a more generic approach.

Code:
if( this.Unit.Territory.coastParent.supply == true && this.Unit.Territory.coastParent.type == 'Coast' )
{
   target = (this.Unit.type == 'Fleet') ? 'army' : 'fleet';

   switch (this.Unit.Territory.id)
   {
      case "150": // Annam
      case "44":  // Armenia
      case "125": // Peking
         this.typeChoices['TransformRiver']='transform to fleet at River Coast';
         this.typeChoices['TransformSea']  ='transform to fleet at Sea Coast';               
         break;
      case "67":  // Novogorad
      case "144": // Siam
      case "7":   // Tugnusess
         this.typeChoices['TransformEast']='transform to fleet at East Coast';
         this.typeChoices['TransformWest']='transform to fleet at West Coast';               
         break;
      case "122": // Yupi
         this.typeChoices['TransformEast'] ='transform to fleet at East Coast';
         this.typeChoices['TransformRiver']='transform to fleet at River Coast';
         this.typeChoices['TransformSouth']='transform to fleet at South Coast';               
         break;
      default:
         this.typeChoices['Transform']='transform to ' + target;
   }
}


Oliver


Top
 Profile  
 
PostPosted: Sun May 08, 2011 12:20 pm 
Offline
Site Admin

Joined: Sat Jun 28, 2008 6:24 am
Posts: 892
Hmm, that's a tricky one. Just to make sure I'm following; for a given parent coast (e.g. Spain) you want to get all the child coasts (e.g. Spain (South Coast), Spain (North Coast)) ?

How best to do that depends really on the context you'll be needing it in; if you'll need to repeatedly and quickly reference parent coast data then you'll need to build a dictionary, but if it'll be a rare requirement it might be more efficient to just look through all territories as/when required.

My instinct says you'll want to generate a dictionary though, and that you'll want to create a function that runs right after board/load.js which goes through the loaded Territories array and generates a parentTerritoryID=>{childTerritoryIDs} dictionary, which you'll then be able to refer to as necessary

The simplest, quickest, easiest solution is this:
Code:
function findChildCoasts(coastParent) {
var childCoasts = new Array();
Territories.map(function(t) { if(t.coastParent.id == coastParent.id) { childCoasts.push(t); } });
return childCoasts;
}

That function will then return all child coasts (if any) for a given territory in an array. But each time it's called it'll have to iterate through all coasts, so if used many times during order generation it will give a significant performance hit on low-power devices. (And I haven't tested it)


Top
 Profile  
 
PostPosted: Sun May 08, 2011 12:44 pm 
Offline

Joined: Wed Jul 29, 2009 10:22 am
Posts: 841
Thanks.
That should do the trick.
For the context:
I have some maps that utilize the transform command. (You can turn a fleet into an army and vice versa at a coastal SC).
If you have an army and a territory with 2 coasts you need to specify what coast the fleet should be made.

Oliver


Top
 Profile  
 
PostPosted: Sun May 08, 2011 8:12 pm 
Offline

Joined: Wed Oct 08, 2008 12:47 pm
Posts: 726
Nice.
Just as a thought, would it be worth rewriting the caching script such that this sort of thing is saved in the variant cache rather than creating the dictionary afresh each time you load the variant?


Top
 Profile  
 
PostPosted: Sun May 08, 2011 9:53 pm 
Offline

Joined: Wed Jul 29, 2009 10:22 am
Posts: 841
The code I use now:
Code:
      OrderObj.updateTypeChoices = function () {
         this.typeChoices = {
            'Hold': 'hold', 'Move': 'move', 'Support hold': 'support hold', 'Support move': 'support move'
         };
         
         if( this.Unit.type == 'Fleet' && this.Unit.Territory.type == 'Sea' )
            this.typeChoices['Convoy']='convoy';
         
         if( this.Unit.Territory.coastParent.supply == true && this.Unit.Territory.coastParent.type == 'Coast' )
         {
            target = (this.Unit.type == 'Fleet') ? 'army' : 'fleet';

            found=false;
            if (this.Unit.type == 'Army')
            {
               Territories.each(function(p)
               {
                  var t=p[1];
                  if(t.coastParent.id == this.Unit.Territory.id && t.coastParent.id != t.id)
                  {
                     found=true;
                     name=t.name.substring(t.name.indexOf("(") + 1, t.name.indexOf(")"))
                     this.typeChoices['Transform_' + t.id] = 'transform to fleet -> ' + name;
                  }
               },this);
            }
            if (found == false)
               this.typeChoices['Transform_' + this.Unit.Territory.id]='transform to ' + target;

         }
         return this.typeChoices;
      };


Top
 Profile  
 
PostPosted: Tue May 10, 2011 10:04 pm 
Offline

Joined: Wed Jul 29, 2009 10:22 am
Posts: 841
Small (final) note.
The full code in action is implemented in the Karibik variant: http://www.vdiplomacy.com/download.php?download=45
Thanks for all your help. I'm still open for optimization-suggestions.

Oliver


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group