flyinian
Member
To update everyone...
Here is the code I've been working on.
It appears to work as intended.
I am able to:
1. Have it randomly select a target from a grid.
2. Apply damage to target.
3. Continue applying damage to targets until all enemies are dead or the damage is used up.
4. Probably break on me after I make this post.
If you see any issues with the code below, let me know.
Thank you.
Here is the code I've been working on.
It appears to work as intended.
I am able to:
1. Have it randomly select a target from a grid.
2. Apply damage to target.
3. Continue applying damage to targets until all enemies are dead or the damage is used up.
4. Probably break on me after I make this post.
If you see any issues with the code below, let me know.
Thank you.
GML:
if(keyboard_check_pressed(ord("T"))) // Used for debugging
{
attack = true;
}
else
{
attack = false;
};
if(attack) // Used for debugging
{
show_debug_message("Total Health begin: " + string(ds_grid_get(enemy,6,11))); // Used for debugging
while (damage > 0 && ds_grid_get_sum(enemy,1,1,1,10) > 0) // While there is damage and health
{
show_debug_message(" "); // Used for debugging
show_debug_message("***!Attack Begins!***"); // Used for debugging
show_debug_message(" "); // Used for debugging
var range = irandom_range(1,10); // sets the range for target selection
var target = ds_grid_get(enemy,1,range); // Establishes the selected target
show_debug_message("Beginning Damage: " + string(damage)); // Used for debugging
show_debug_message("Grid Number Rolled: " + string(range)); // Used for debugging
show_debug_message("Target Found(Health): " + string(target)); // Used for debugging
if (target >= damage)
{
show_debug_message("__Target Has **More** Health Than Damage__"); // Used for debugging
target -= damage; // Applies the damage to the target's health.
damage -= damage; // Forces the damage to be 0 because all of it has been used.
ds_grid_set(enemy,1,range,target); // Updates the grid with the new values
show_debug_message("Target's Health After Damage Applied: " + string(target)); // Used for debugging
}
else
{
show_debug_message(" "); // Used for debugging
show_debug_message("__Target Has **Less** Health Than Damage__"); // Used for debugging
show_debug_message("The Equation: Beginning Damage - Target's Health = Difference. Difference - Beginning Damage = Temporary Damage. Apply Temporary Damage To Target's Health. New Damage = Difference"); // Used for debugging
difference = damage - target; // Finds the difference.
tempdamage = damage - difference; // Find the damage to be applied to target's health without going over.
damage = difference; // Sets damage to the difference to account for damage usage on target.
target -= tempdamage; // Subtracts the temporary damagefrom the target's health
ds_grid_set(enemy,1,range,target); // Updates target's health within the grid.
show_debug_message("Difference: " + string(difference)); // Used for debugging
show_debug_message("Temporary Damage: " + string(tempdamage)); // Used for debugging
show_debug_message("Target's Health: " + string(target)); // Used for debugging
show_debug_message("New Damage: " + string(damage)); // Used for debugging
};
if (damage <= 0)
{
show_debug_message("out of damage"); // Used for debugging
};
// setting or "updating" the total health region, prevents an infinite loop. This occurs when there is more health than damage. Has to do with how the damage and health respond to each other.
ds_grid_set(enemy,6,11,ds_grid_get_sum(enemy,1,1,1,10)); // Updates the total health in the given region. used to prevent an infinite while loop when total health exceeds total damage.
attack_count += 1; // Keeps tracks of number of attacks performed
show_debug_message("Total Health Sum: " + string(ds_grid_get_sum(enemy,1,1,1,10))); // Used for debugging
show_debug_message("attack count: " + string(attack_count)); // Used for debugging
show_debug_message(" "); // Used for debugging
show_debug_message("***!Attack End!***"); // Used for debugging
show_debug_message(" "); // Used for debugging
};
};
I am planning on taking this step by step and getting the code to work before moving to the next section. Hopefully, this helps to break it down so, it's easier to understand what is going on.
I am currently trying to have the list to check if it has any dead enemies and if it does, add those dead enemies to the "deadenemylist" and delete it from the "enemylist". it runs but, I don't think it returns the correct values.
I am starting with this part of the code because from my understanding, it would be best to check if any enemies are alive before doing anything else.
I'm not using instances for the enemies. I have a ds map that holds the information of the enemy.
----------------------------------------------------------------------------------
-------------------------------
I am currently trying to have the list to check if it has any dead enemies and if it does, add those dead enemies to the "deadenemylist" and delete it from the "enemylist". it runs but, I don't think it returns the correct values.
I am starting with this part of the code because from my understanding, it would be best to check if any enemies are alive before doing anything else.
I'm not using instances for the enemies. I have a ds map that holds the information of the enemy.
GML:
// Enemy exists variables
var enemylist = ds_list_create(); // creates the given ds list, list for living enemies
ds_list_add(enemylist,"Count 1", "Count 2", "Count 3"); // add to the ds list
var find_enemy = ds_list_find_value(enemylist, i); // finds the value for the given ds list position
var enemy_target = EnemyMap[? find_enemy]; // allows the ds map to be used/changed
var no_enemies = ds_list_empty(enemylist); // creates the control variable for enemy existance
var list_size = ds_list_size(enemylist) - 1; // gets the list size, and minuses one - as indexes start at zero
var list_random_pos = irandom_range(0, list_size); // sets the random range to between zero and end size
var enemy_Count = ds_list_find_value(enemylist, list_random_pos); // finds the value at that index position (the returned number of list_random_pos)
var dead_enemy = ds_list_find_index(enemylist,0); // holds the dead enemy value
// Enemy doesn't exist variables
var deadenemylist = ds_list_create(); // creates the given ds list, list for dead enemies
var dead_list_size = ds_list_size(deadenemylist) - 1; // gets the list size, and minuses one - as indexes start at zero
var dead_enemy_count = ds_list_find_value(deadenemylist, dead_list_size); // finds the value at that index position (the returned number of dead_list_size )
// here is the logic I created for the code. i'll be changing it overtime.
// 1. Need to initialize the lists. Check.
// 2. Need to create the variables for controlling the list selection and such. May need work.
// 3. Need to check to see if the EnemyList has any enemies. If it does, it needs to check to see if any of them holds a health of zero or less.
// 4. If any enemy in enemylist has a health of zero or less, delete it from enemylist and add it to deadenemylist.
// 5. Need to check to see if the deadenemylist has any enemies with a health of 1 or more.
// 6. If any enemy in deadenemylist has health 1 or more, add it to the enemylist and delete it from deadenemylist.
// 7. Once the updated Enemylist has been established, randomize the list and select an enemy.
// 8. Once an enemy has been selected, run the check to see if the damage is more than the enemy health.
// 9. if the damage is more than the enemy health, apply what is needed to kill the enemy and save the remaining damage.
// 10. run through the randomizing and selection process again to obtain a new enemy, check if the damage is greater than enemy health, if not apply the damage. if it was, repeat step 7.
// 11. If there are no enemies to apply damage to, delete the damage and display a no enemies to attack message.
// below, I am checking "enemylist" for any dead enemies. if any exists it will add them to "deadenemylist" and deletes them from "enemylist".
{
do // do the following
{
if(ds_list_find_index(enemylist,dead_enemy)) // if an enemy with a health of 0 is found in enemy list
{
ds_list_add(deadenemylist,dead_enemy); // add the dead enemy to the deadenemylist
ds_list_delete(enemylist,dead_enemy); // delete the dead enemy from the enemylist
}
}
until dead_enemy == -1; // until all dead enemies have been removed from enemylist and added to deadenemylist
show_debug_message("dead_enemy: " + string(dead_enemy)); // returns -1, this is correct since it at the end of the code so, it'll return -1
show_debug_message("enemylist size: " + string(list_size)); // returns 2, this should be 0 because, enemies start with 0 health so every enemy is dead.
show_debug_message("deadenemylist: " + string(ds_list_size(deadenemylist))); // returns 0, this should be 3 because, there should be 3 dead enemies.
};
----------------------------------------------------------------------------------
After going through tutorials and forum threads, I managed to make some progress on my combat system.
I have the randomization working so far with high hopes that it won't break or become useless to me because it doesn't do what I want it to.
Now, I need to check the ds_list "enemylist" if any of the enemies have a value of 0 which means they are dead or have no units to fight. This is where I need the help.
if an enemy returns a value of "0", then it will be deleted from the "enemeylist" for now. later, I'll be adding another list "deadenemylist" to store dead enemies just in case they gain health/units once again.
I've tried using ds_list_find_value/index in an if statement and haven't had any luck.
As i was creating this post, I thought about making the "deadenemylist" and if a position has a value greater than 0, then add that position to "enemylist". I guess in other words, making the "deadenemylist" a controler list that dictates if the enemy is added to the "enemylist" or not. Would this work?
NOTE: EnemyList[? enemy_key1] is a ds map while enemylist is a list.
I have the randomization working so far with high hopes that it won't break or become useless to me because it doesn't do what I want it to.
Now, I need to check the ds_list "enemylist" if any of the enemies have a value of 0 which means they are dead or have no units to fight. This is where I need the help.
if an enemy returns a value of "0", then it will be deleted from the "enemeylist" for now. later, I'll be adding another list "deadenemylist" to store dead enemies just in case they gain health/units once again.
I've tried using ds_list_find_value/index in an if statement and haven't had any luck.
As i was creating this post, I thought about making the "deadenemylist" and if a position has a value greater than 0, then add that position to "enemylist". I guess in other words, making the "deadenemylist" a controler list that dictates if the enemy is added to the "enemylist" or not. Would this work?
NOTE: EnemyList[? enemy_key1] is a ds map while enemylist is a list.
GML:
// randomization combat
// creation of ds lists
var enemylist;
var deadenemylist
enemylist = ds_list_create();
deadenemylist = ds_list_create();
ds_list_add(enemylist,"Count 1", "Count 2", "Count 3");
var i = 0;
var enemy_key1 = ds_list_find_value(enemylist, i);
var enemy1 = EnemyList[? enemy_key1];
var noenemy = false;
var size = ds_list_size(enemylist) - 1;
var list_range = irandom_range(0, size);
var value = ds_list_find_value(enemylist, list_range);
ds_list_add(deadenemylist, value);
//go through living enemy and find a target
for ( i = 0; i < size; ++i)
{
if(ds_list_size(enemylist >= 0)) // if enemy are in the list
{
list_range = irandom_range(0, size); //randomizes the enemy list
value = ds_list_find_value(enemylist, list_range); // find a target in the list
show_debug_message("target found");
show_debug_message("value: " +string(value));
enemy_key1 = value; // sets the selected target as the target for damage to be applied to.
show_debug_message("enemy_key1: " +string(enemy_key1));
show_debug_message("target set: " + string (enemy_key1));
/*if(ds_list_find_value(enemylist,value) < ds_list_find_index(enemylist,0)) // i've done this and various versions of this and only get errors.
{
ds_list_delete(enemylist, value);
};*/
};
};
//if no enemies
if(ds_list_empty(enemylist))
{
noenemy = true;
show_debug_message("Enemy list is empty");
};
if(noenemy = true){
show_debug_message("no enemy numbers to deduct");
exit;
}
else
{
show_debug_message("Enemy exists");
enemy1 -= EnemyList[? "totalcountdifference"];
EnemyList[? enemy_key1] = enemy1;
show_debug_message("target attacked: " + string (enemy_key1));
show_debug_message("Enemy Count adjusted");
};
};
};
So, I have cleaned up my code and continued developing it. Made little progress.
I am still looking for help with the code to get it working how I'd like it to.
There is some code not shown and it is used for calculating troops numbers(health) and their stats. that code appears to work fine.
so far I've managed to get working:
1. I've managed to get it to randomly select an enemy.
2. I believe I got it to add an enemy if it has a value of zero health to the "deadenemylist".(died)
3. I believe I got it to add an enemy to the "enemylist" if it has more than zero health.(revived)
Issues I currently have with my combat system:
1. I have an issue where it appears that it still subtracts health from an enemy count even though that enemy has no health. This may be due to the code not shown. Or, it randomly selects a dead enemy and still subtracts health from it which effects my other variables that keep track of the combined health across all enemies.
2. In the debugger, I have the "enemylist" and "deadenemylist" show their count and it continuously increases as enemies die/revive. Would this be an issue or is that just how it calculates it? There are only 3 enemies, so that number should be split between "enemylist" and "deadenemylist" and should not exceed(?) 3 but, it'll continuously increase over 3. I usually see it increase by 2's.
3. if the enemy health is less than the damage applied, i want it to apply the remaining damage to a randomly selected enemy. This may be done using my non shown code and used a if statement?..
NOTE: EnemyList[? enemy_key1] is a ds map while enemylist is a list.
I am still looking for help with the code to get it working how I'd like it to.
There is some code not shown and it is used for calculating troops numbers(health) and their stats. that code appears to work fine.
so far I've managed to get working:
1. I've managed to get it to randomly select an enemy.
2. I believe I got it to add an enemy if it has a value of zero health to the "deadenemylist".(died)
3. I believe I got it to add an enemy to the "enemylist" if it has more than zero health.(revived)
Issues I currently have with my combat system:
1. I have an issue where it appears that it still subtracts health from an enemy count even though that enemy has no health. This may be due to the code not shown. Or, it randomly selects a dead enemy and still subtracts health from it which effects my other variables that keep track of the combined health across all enemies.
2. In the debugger, I have the "enemylist" and "deadenemylist" show their count and it continuously increases as enemies die/revive. Would this be an issue or is that just how it calculates it? There are only 3 enemies, so that number should be split between "enemylist" and "deadenemylist" and should not exceed(?) 3 but, it'll continuously increase over 3. I usually see it increase by 2's.
3. if the enemy health is less than the damage applied, i want it to apply the remaining damage to a randomly selected enemy. This may be done using my non shown code and used a if statement?..
NOTE: EnemyList[? enemy_key1] is a ds map while enemylist is a list.
GML:
//enemies exist variables
var enemylist = ds_list_create();
ds_list_add(enemylist,"Count 1", "Count 2", "Count 3");
var i = 0;
var enemy_key1 = ds_list_find_value(enemylist, i);
var enemy1 = EnemyList[? enemy_key1];
var noenemy = ds_list_empty(enemylist);
var size = ds_list_size(enemylist) - 1;
var list_range = irandom_range(0, size);
var value = ds_list_find_value(enemylist, list_range);
// no enemy exist variables
var deadenemylist = ds_list_create();
var zz = 0;
var dsize = ds_list_size(deadenemylist) - 1;
var dvalue = ds_list_find_value(deadenemylist, dsize);
if(!noenemy) //there are enemies in the list
{
for ( i = 0; i < size; i++)
{
list_range = irandom_range(0, size);
value = ds_list_find_value(enemylist, list_range);
if(ds_list_find_index(enemylist,value) <= 0)
{
ds_list_add(deadenemylist, 0);
ds_list_delete(enemylist, 0);
};
enemy_key1 = value;
enemy1 -= EnemyList[? "totalcountdifference"];
EnemyList[? enemy_key1] = enemy1;
}
}
else //there aren't any enemies in the list
{
for ( zz = 0; zz < dsize; zz++)
{
dvalue = ds_list_find_value(deadenemylist, dsize);
if(ds_list_find_index(deadenemylist,dvalue) > 0)
{
ds_list_add(enemylist,dvalue);
ds_list_delete(deadenemylist,dvalue);
};
}
};
};
};
Last edited: