forked from 0ad/0ad
Some fixes to the resource pool counters to make them dynamically lengthen based on caption length, and to adjust Housing / Population when buildings and units are created. (It probably doesn't yet deny creation of a unit if it would exceed population limit, though.) Also fixed a bug where an attempt to modify a resource value failed if it was zero.
This was SVN commit r3351.
This commit is contained in:
parent
b781c898df
commit
66d780eb49
@ -167,6 +167,17 @@ function entityInit()
|
|||||||
{
|
{
|
||||||
this.traits.id = new Object();
|
this.traits.id = new Object();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the entity either costs population or adds to it,
|
||||||
|
if (this.traits.population)
|
||||||
|
{
|
||||||
|
// If the entity increases the population limit (provides Housing),
|
||||||
|
if (this.traits.population.add)
|
||||||
|
getGUIGlobal().giveResources ("Housing", this.traits.population.add);
|
||||||
|
// If the entity occupies population slots (occupies Housing),
|
||||||
|
if (this.traits.population.rem)
|
||||||
|
getGUIGlobal().giveResources ("Population", this.traits.population.rem);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// Generate entity's personal name (if it needs one).
|
// Generate entity's personal name (if it needs one).
|
||||||
@ -773,7 +784,7 @@ function entityCheckQueueReq( entity, template )
|
|||||||
{
|
{
|
||||||
case "POPULATION":
|
case "POPULATION":
|
||||||
// If the item costs more of this resource type than we have,
|
// If the item costs more of this resource type than we have,
|
||||||
if (resources[resource] > (localPlayer.resource["HOUSING"]-localPlayer.resource[resourceU]))
|
if (template.traits.population.rem > (localPlayer.resource["HOUSING"]-localPlayer.resource[resourceU]))
|
||||||
{
|
{
|
||||||
// Return an error.
|
// Return an error.
|
||||||
return ("Insufficient Housing; " + (resources[resource]-localPlayer.resource["HOUSING"]-localPlayer.resource.valueOf()[resourceU].toString()) + " required.");
|
return ("Insufficient Housing; " + (resources[resource]-localPlayer.resource["HOUSING"]-localPlayer.resource.valueOf()[resourceU].toString()) + " required.");
|
||||||
|
@ -39,6 +39,11 @@ function startMap (mapName, losSetting, openWindow)
|
|||||||
btCode = new Array("");
|
btCode = new Array("");
|
||||||
messageBox(400, 200, "The game could not be started with the given parameters. You probably have entered an invalid map name.", "Error", 0, btCaptions, btCode);
|
messageBox(400, 200, "The game could not be started with the given parameters. You probably have entered an invalid map name.", "Error", 0, btCaptions, btCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialise Resource Pools by attaching them to the Player object.
|
||||||
|
// (CPlayer code takes care of giving a copy to each player.)
|
||||||
|
createResources();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ====================================================================
|
// ====================================================================
|
||||||
@ -109,12 +114,6 @@ function setupSession ()
|
|||||||
// Do essentials that can only be done when the session has been loaded ...
|
// Do essentials that can only be done when the session has been loaded ...
|
||||||
// For example, create the resource types, scores, etc, for each player.
|
// For example, create the resource types, scores, etc, for each player.
|
||||||
|
|
||||||
// Initialise Resource Pools by attaching them to the Player object.
|
|
||||||
// (CPlayer code takes care of giving a copy to each player.)
|
|
||||||
createResources();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
if (sessionType == "Skirmish")
|
if (sessionType == "Skirmish")
|
||||||
{
|
{
|
||||||
|
@ -56,12 +56,13 @@ function worldClickHandler(event)
|
|||||||
args[1]=event.y;
|
args[1]=event.y;
|
||||||
break;
|
break;
|
||||||
// entity target commands
|
// entity target commands
|
||||||
case NMT_AttackMelee:
|
// I'm guessing we no longer require these now that they have become generic events?
|
||||||
case NMT_Gather:
|
// case NMT_AttackMelee:
|
||||||
case NMT_Heal:
|
// case NMT_Gather:
|
||||||
args[0]=event.entity;
|
// case NMT_Heal:
|
||||||
args[1]=null;
|
// args[0]=event.entity;
|
||||||
break;
|
// args[1]=null;
|
||||||
|
// break;
|
||||||
case NMT_Generic:
|
case NMT_Generic:
|
||||||
args[0]=event.entity;
|
args[0]=event.entity;
|
||||||
args[1]=event.action;
|
args[1]=event.action;
|
||||||
|
@ -56,9 +56,9 @@ function addResource (resourceName, resourceQty)
|
|||||||
|
|
||||||
// Store resource's name and starting value.
|
// Store resource's name and starting value.
|
||||||
localPlayer.resource.valueOf()[resourceNameU] = resourceQty;
|
localPlayer.resource.valueOf()[resourceNameU] = resourceQty;
|
||||||
// Update GUI resource counter.
|
|
||||||
if (resourceName != "Housing")
|
// Dynamically adjust width of resource counter based on caption length.
|
||||||
getGUIObjectByName ("snResourceCounter_" + resourceName).caption = resourceQty;
|
refreshResource (resourceName);
|
||||||
|
|
||||||
console.write( "Added " + resourceName + " (" + resourceQty + ")" );
|
console.write( "Added " + resourceName + " (" + resourceQty + ")" );
|
||||||
}
|
}
|
||||||
@ -74,20 +74,21 @@ function setResources (resourceName, resourceQty)
|
|||||||
// Create uppercase name.
|
// Create uppercase name.
|
||||||
resourceNameU = resourceName.toUpperCase();
|
resourceNameU = resourceName.toUpperCase();
|
||||||
|
|
||||||
if ( localPlayer.resource.valueOf()[resourceNameU] )
|
// if ( localPlayer.resource.valueOf()[resourceNameU] )
|
||||||
{
|
// {
|
||||||
// Set resource value.
|
// Set resource value.
|
||||||
localPlayer.resource.valueOf()[resourceNameU] = resourceQty;
|
localPlayer.resource.valueOf()[resourceNameU] = resourceQty;
|
||||||
// Update GUI resource counter.
|
|
||||||
getGUIObjectByName ("snResourceCounter_" + resourceName).caption = localPlayer.resource.valueOf()[resourceNameU];
|
// Dynamically adjust width of resource counter based on caption length.
|
||||||
|
refreshResource (resourceName);
|
||||||
|
|
||||||
console.write ("Resource set to " + resourceQty + " " + resourceName + ".");
|
console.write ("Resource set to " + resourceQty + " " + resourceName + ".");
|
||||||
return ( true );
|
return ( true );
|
||||||
}
|
// }
|
||||||
|
|
||||||
// If the resource wasn't in the list, report an error.
|
// If the resource wasn't in the list, report an error.
|
||||||
console.write ("Failed to set resource " + resourceName + " to " + resourceQty);
|
// console.write ("Failed to set resource " + resourceName + " to " + resourceQty);
|
||||||
return ( false ) ;
|
// return ( false ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ====================================================================
|
// ====================================================================
|
||||||
@ -100,21 +101,22 @@ function giveResources (resourceName, resourceQty)
|
|||||||
resourceName = toTitleCase (resourceName);
|
resourceName = toTitleCase (resourceName);
|
||||||
// Create uppercase name.
|
// Create uppercase name.
|
||||||
resourceNameU = resourceName.toUpperCase();
|
resourceNameU = resourceName.toUpperCase();
|
||||||
|
console.write (localPlayer.resource.valueOf()[resourceNameU]);
|
||||||
if ( localPlayer.resource.valueOf()[resourceNameU] )
|
// if ( localPlayer.resource.valueOf()[resourceNameU] )
|
||||||
{
|
// {
|
||||||
// Set resource value.
|
// Set resource value.
|
||||||
localPlayer.resource.valueOf()[resourceNameU] += resourceQty;
|
localPlayer.resource.valueOf()[resourceNameU] += resourceQty;
|
||||||
// Update GUI resource counter.
|
|
||||||
getGUIObjectByName ("snResourceCounter_" + resourceName).caption = localPlayer.resource.valueOf()[resourceNameU];
|
// Dynamically adjust width of resource counter based on caption length.
|
||||||
|
refreshResource (resourceName);
|
||||||
|
|
||||||
console.write ("Earned " + resourceQty + " " + resourceName + ".");
|
console.write ("Earned " + resourceQty + " " + resourceName + ".");
|
||||||
return ( true );
|
return ( true );
|
||||||
}
|
// }
|
||||||
|
|
||||||
// If the resource wasn't in the list, report an error.
|
// // If the resource wasn't in the list, report an error.
|
||||||
console.write ("Failed to add " + resourceQty + " to resource " + resourceName);
|
// console.write ("Failed to add " + resourceQty + " to resource " + resourceName);
|
||||||
return ( false );
|
// return ( false );
|
||||||
}
|
}
|
||||||
|
|
||||||
// ====================================================================
|
// ====================================================================
|
||||||
@ -128,20 +130,56 @@ function deductResources (resourceName, resourceQty)
|
|||||||
// Create uppercase name.
|
// Create uppercase name.
|
||||||
resourceNameU = resourceName.toUpperCase();
|
resourceNameU = resourceName.toUpperCase();
|
||||||
|
|
||||||
if( localPlayer.resource.valueOf()[resourceNameU] )
|
// if( localPlayer.resource.valueOf()[resourceNameU] )
|
||||||
{
|
// {
|
||||||
// Set resource value.
|
// Set resource value.
|
||||||
localPlayer.resource.valueOf()[resourceNameU] -= resourceQty;
|
localPlayer.resource.valueOf()[resourceNameU] -= resourceQty;
|
||||||
// Update GUI resource counter.
|
|
||||||
getGUIObjectByName ("snResourceCounter_" + resourceName).caption = localPlayer.resource.valueOf()[resourceNameU];
|
// Dynamically adjust width of resource counter based on caption length.
|
||||||
|
refreshResource (resourceName);
|
||||||
|
|
||||||
console.write("Deducted " + resourceQty + " " + resourceName + ".");
|
console.write("Deducted " + resourceQty + " " + resourceName + ".");
|
||||||
return( true );
|
return( true );
|
||||||
}
|
// }
|
||||||
|
|
||||||
// If the resource wasn't in the list, report an error.
|
// // If the resource wasn't in the list, report an error.
|
||||||
console.write ("Failed to deduct " + resourceQty + " from resource " + resourceName);
|
// console.write ("Failed to deduct " + resourceQty + " from resource " + resourceName);
|
||||||
return false;
|
// return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ====================================================================
|
||||||
|
|
||||||
|
function refreshResource (resourceName)
|
||||||
|
{
|
||||||
|
// Ignore the "Housing" resource ... It doesn't work like normal resources and doesn't have a counter to resize.
|
||||||
|
if (resourceName == "Housing")
|
||||||
|
resourceName = "Population";
|
||||||
|
|
||||||
|
// Update GUI resource counter caption.
|
||||||
|
if (resourceName != "Population")
|
||||||
|
getGUIObjectByName ("snResourceCounter_" + resourceName).caption = localPlayer.resource.valueOf()[resourceNameU];
|
||||||
|
else
|
||||||
|
getGUIObjectByName ("snResourceCounter_Population").caption = localPlayer.resource.valueOf()["POPULATION"] + "/" + localPlayer.resource.valueOf()["HOUSING"];
|
||||||
|
|
||||||
|
// Get the index of the resource readout to be resized.
|
||||||
|
crdResult = getCrd ("snResourceCounter_" + resourceName, true);
|
||||||
|
// Get resource readout object.
|
||||||
|
resourceObject = getGUIObjectByName ("snResourceCounter_" + resourceName);
|
||||||
|
|
||||||
|
// For each coordinate group stored for this control,
|
||||||
|
for (coordGroup in Crd[crdResult].coord)
|
||||||
|
{
|
||||||
|
// Set X position so that resources always are immediately next to each other. (Except for Food, which is always the leftmost resource.)
|
||||||
|
if (resourceName != "Food")
|
||||||
|
Crd[crdResult].coord[coordGroup].x
|
||||||
|
= Crd[crdResult-1].coord[coordGroup].x + Crd[crdResult-1].coord[coordGroup].width + 5;
|
||||||
|
|
||||||
|
// Set width of readout based on length of caption.
|
||||||
|
Crd[crdResult].coord[coordGroup].width = snConst.MiniIcon.Width+5 + resourceObject.caption.length * 10;
|
||||||
|
|
||||||
|
// Recalculate readout's size coordinates.
|
||||||
|
Crd[crdResult].size[coordGroup] = calcCrdArray (Crd[crdResult].coord[coordGroup].rx, Crd[crdResult].coord[coordGroup].ry, Crd[crdResult].coord[coordGroup].x, Crd[crdResult].coord[coordGroup].y, Crd[crdResult].coord[coordGroup].width, Crd[crdResult].coord[coordGroup].height, Crd[crdResult].coord[coordGroup].rx2, Crd[crdResult].coord[coordGroup].ry2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ====================================================================
|
// ====================================================================
|
||||||
|
@ -58,15 +58,25 @@ function addCrd (name, group, rx, ry, x, y, width, height, rx2, ry2)
|
|||||||
// ====================================================================
|
// ====================================================================
|
||||||
|
|
||||||
// Return coordinate object with a given name.
|
// Return coordinate object with a given name.
|
||||||
function getCrd (name)
|
// Optionally can choose to return the index to the coordinate, rather than the coordinate itself.
|
||||||
|
function getCrd (name, byIndex)
|
||||||
{
|
{
|
||||||
for (getCrdLoop = 0; getCrdLoop <= Crd.last; getCrdLoop++)
|
for (getCrdLoop = 0; getCrdLoop <= Crd.last; getCrdLoop++)
|
||||||
{
|
{
|
||||||
if (Crd[getCrdLoop].name == name)
|
if (Crd[getCrdLoop].name == name)
|
||||||
return Crd[getCrdLoop];
|
{
|
||||||
|
// If only index requested, just return index.
|
||||||
|
if (byIndex)
|
||||||
|
return getCrdLoop;
|
||||||
|
else
|
||||||
|
// Otherwise return the whole coordinate array for this object.
|
||||||
|
return Crd[getCrdLoop];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.write ("Coordinate " + name + " not found in call to getCrd().");
|
console.write ("Coordinate " + name + " not found in call to getCrd().");
|
||||||
|
// Return -1 to indicate failure.
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ====================================================================
|
// ====================================================================
|
||||||
@ -157,3 +167,22 @@ function calcCrdArray (rx, ry, x, y, width, height, rx2, ry2)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ====================================================================
|
// ====================================================================
|
||||||
|
|
||||||
|
// Set an existing coord of a given name to a new value.
|
||||||
|
function setCrd (name, newCrd)
|
||||||
|
{
|
||||||
|
// Get the index to the given coordinate.
|
||||||
|
crdResult = getCrd (name, true);
|
||||||
|
if (crdResult != -1)
|
||||||
|
{
|
||||||
|
// Set new value of this coordinate.
|
||||||
|
Crd[crdResult] = newCrd;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
console.write ("Failed to setCrd() + " + name);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ====================================================================
|
||||||
|
Loading…
Reference in New Issue
Block a user