How to randomly modify many entities that have the same model?

Posted By: joao13pt

How to randomly modify many entities that have the same model? - 06/07/13 20:01

Hello!
So, i have, for example, 5 entities, all of them using the same model.mdl
When i modify one of them via a Contact* struct, by randomly modifying each of the vertex's, ALL of the 5 entities are affected. Why? Obviously because they all share the same model.mdl
How do i avoid this? I remember, in The Elder Scrolls Oblivion, there was this "Base" entity of a sword, for example, and all of the other same 100 swords used in the game, that could be modified (they'd break, or get stronger, or weaker, ...) were called "references" and each had a specific "Unique ID"
I've tried creating an algorithm that mimics this, using ent_clone, but this won't work, because, the only result i saw was the original being deleted, and the cloned one replacing the original...which made no sense
How do i make this work?
Thank you!
Posted By: Uhrwerk

Re: How to randomly modify many entities that have the same model? - 06/07/13 20:12

ent_clone is the right approach for you. Post your code an we will see why it won't work out for you.
Posted By: joao13pt

Re: How to randomly modify many entities that have the same model? - 06/07/13 22:38

I just do this simple process:
(just an example)

ENTITY* ent1 = ent_create("model.mdl", vector(0,0,0), NULL)
ENTITY* ent2 = ent_clone(ent1);
(then i place it at a diferent position from ent1)
ent2.x = 50

The result is ent1 disapearing and ent2 showing up
Basically this only removed the first entity and replaced it with the clone, which makes absolutelly no sense
tell me, please, how should i use ent_clone?
Posted By: Anonymous

Re: How to randomly modify many entities that have the same model? - 06/07/13 22:57

EDIT* Sorry I misunderstood.
Posted By: joao13pt

Re: How to randomly modify many entities that have the same model? - 06/07/13 23:09

Yes, i know that
I understand how ent_clone works and what it does, the thing is:
using the simple example code i posted earlier, the result is the original entity being deleted, and being replaced by the clone entity. So, i only end up with 1 entity after all, so something isn't right
I hope you understand my point.
I know how ent_clone works, i understand it perfectly, i just don't understand why it has this effect with my code.


------------------ EDIT -----------------
I'll explain with a solid example:
I have this Planet
And around it i want to place an asteroid field
The asteroids are inicially cubes but are later randomly deformed, vertex by vertex
the whole algorithm for deforming randomly and placing randomly around the planet is very easy for me.
The problem i'm having is that, the second asteroid i place, and the third, and fourth and so on, they will ALWAYS be exaclty like the first one, because they are ALL sharing the same model
my goal is to avoid this, basically i want each new entity(asteroid) to be a clone of the initial one (base asteroid -> initial undeformed cube) so i can deform it later and perhaps give it a diferent texture too, this way i could have thousands of diferent possibilities
But the thing is, using ent_clone will only make the original entity to be eliminated, and the final clone, will replace the original entity. So, instead of having as many asteroids as i want, i ALWAYS end up with 1 single asteroid
how do i avoid this situation?
Posted By: Anonymous

Re: How to randomly modify many entities that have the same model? - 06/07/13 23:27

100 asteroids
100 ent_create(s) and 100 ent_clone(s) = 1 entity at the end?
Just wondering and hope someone who knows answers.

I did the testing and ent_clone is not a ent_duplicate it shifts the entity from a shared mdl to cloned mdl that is not shared . It doesn't seem to duplicate the entity so you would have to create all you asteroids then clone them to unshared virtual(created in memory only) mdls and lastly modify them individually.

Code:
ENITY* astroids[100];
for(I=0; I < 100; I++)
{
astroid[I] = ent_create(.......);
astroid[I] = ent_clone(astriod[I]);
}

Posted By: Uhrwerk

Re: How to randomly modify many entities that have the same model? - 06/08/13 01:16

Originally Posted By: joao13pt
Basically this only removed the first entity and replaced it with the clone, which makes absolutelly no sense
tell me, please, how should i use ent_clone?

You misunderstood the concept. ent_clone does not make a copy of the entity itself but of its mesh and skin. There is no entity removed and no new one created in your example. Instead you have just moved the original entity.

The proper usage is to first call ent_clone on an entity and then modify the mesh / skin as you see fits. The return value if ent_clone is the original entity, or NULL in case it failed.

So in summry:calling ent_clone let's you change an entities skin and mesh without changing the skin and mesh of the other entities sharing the corresponding model. ent_clone does not copy entities. You still have to create them with ent_create or place them via WED.

http://www.conitec.net/beta/ent_clone.htm
Posted By: Anonymous

Re: How to randomly modify many entities that have the same model? - 06/08/13 04:31

It is another example of the manual not being clear. And the name of the instruction is misleading I would rename it to ent_make_individual or something like that. Maybe ent_unshare. The example code doesn't even show ent_clone. I would have believe is way if I hadn't read long ago the exact same kinda post.
Posted By: sivan

Re: How to randomly modify many entities that have the same model? - 06/08/13 06:30

yes, cloning means something different in lite-c and in biotechnology.
Posted By: joao13pt

Re: How to randomly modify many entities that have the same model? - 06/08/13 14:04

Thank you! I now have a better picture of what ent_clone does, something the manual should explain a bit better
I've already tried it with 2 asteroids, doing exactly like you guys explained to me:

first ent_create
then ent_clone

and since the objects are randomly deformed, their diferences were obvious, which means its the correct way to use ent_clone
Thank you all!

By the way, did you just create an array of Entities?

Originally Posted By: Malice

Code:
ENITY* astroids[100];



Why did i never think of this!?
Posted By: joao13pt

Re: How to randomly modify many entities that have the same model? - 06/08/13 15:48

Now that cloning meshes works, their skins won't clone
I generated 200 randomly deformed, completelly diferent asteroids side by side, and slowed the skin modifying process so that i could se with my own eyes, in "slow motion" what hapenned
The result was ALL of the entities that started from the asteroid.mdl, shared the same skin
below im posting 2 screenshots, observe how all of the "asteroids" always have the same color



Posted By: Uhrwerk

Re: How to randomly modify many entities that have the same model? - 06/08/13 16:32

joao13pt, please, we cannot guess your mistakes. If you want us to help you gotta post your code.
Posted By: joao13pt

Re: How to randomly modify many entities that have the same model? - 06/08/13 17:12

Fair enough, my mistake, sorry

Code:
BMAP* texture2 = "empty.bmp";

//first, i start with this function (ignore the name) its job is to 
//color a square sized bitmap randomly, im 99% shure my problem doesn't come from this function. 
//Notice it doesn't return anything

void bmap_square_drawColor(BMAP* bmap, var alpha)
{
	var format = bmap_lock(bmap, 0);
	var pixel;
	var width = bmap_width(bmap);
	var height = bmap_height(bmap);
	var i, j;
	
	for(j=0 ; j < width ; j++)
	{
		pixel = pixel_for_vec(vector(random(255),random(255),random(255)), alpha, format);
		for(i=0 ; i< width ; i++)
		{
			pixel = pixel_for_vec(vector(random(255),random(255),random(255)), alpha, format);
			pixel_to_bmap(bmap, i, j, pixel);
		}
	}
	bmap_unlock(bmap);
	
}

//then there's the function to deform the initial cubes 
//into smaller randomly deformed spheres, actually

function cube_deform(ENTITY* model, var ray) 
{
  int i = ent_status(model,0); 
  for (; i>0; i--) 
{    														 
  	CONTACT* c = ent_getvertex(model,NULL,i);   
  	vec_normalize(c.x, ray);
  	c.y += random(50)-50;
  	c.z += random(50)-50;
  	c.v = NULL;   // set c.v to NULL to change c.x,y,z 
  	ent_setvertex(model,c,i);					 
  	}
  	
	ent_fixnormals(model, 0); // fix the not updating shading (shadow) on the model

}

//these next 2 functions serve to generate the entities
//call on them cube_deform() and bmap_square_drawColor()

function asteroid_modify(ENTITY* ast_i)
{
	set(ast_i,SHADOW);
	cube_deform(ast_i, (100 + random(50)));
	ast_i.scale_x = 0.05;
	ast_i.scale_y= 0.05;
	ast_i.scale_z = 0.05;
	ent_cloneskin(ast_i);
	bmap_square_drawColor(texture2, 255);
	ent_setskin(ast_i,texture2,1);
}

function generate_asteroids()
{
	ENTITY* asteroid[200]; //initialize asteroid array
	int i;
	for (i=0 ; i<200 ; i++)
		{
			asteroid[i] = ent_create("cube_small.mdl", vector(500,(0 + 20*i),0), NULL);
			ent_clone(asteroid[i]);
			wait(40);
			bmap_square_drawColor(texture2, 255);
			asteroid_modify(asteroid[i]);
		}
}



So, it should be: for each new asteroid in the array, when reading asteroid_modify(), first they're deformed, then scaled down, then their skin is cloned (not needed, but i used it anyway), then i call on their skin (texture2) bmap_square_drawColor() to randomly color each of them, then i set this modified skin as each of the entities's skin (texture2 after bmap_square_drawColor() being called on it)

since i call
Code:
ent_cloneskin(ast_i);
bmap_square_drawColor(texture2, 255);
ent_setskin(ast_i,texture2,1);


for each asteroid in the array, each one of them should be stuck with a diferently coloured texture2, shouldn't it?

EDIT: i know the code is a mess, and it isn't even complete, its way to big and messy, good luck finding the mistake (i know there's one somewhere)
And thank you
Posted By: Wjbender

Re: How to randomly modify many entities that have the same model? - 06/08/13 18:43

Hi , i did not look into your code but wanted to ask if
you know how to use random_seed

If not then your color will be the same always anyway i think.
Posted By: Anonymous

Re: How to randomly modify many entities that have the same model? - 06/08/13 18:51

I can't test right now but does this work ?


Code:
BMAP* bmp_temp;
function asteroid_modify(ENTITY* ast_i)
{
        ast_i = ent_cloneskin(ast_i); // doesn't seem needed.
        bmp_temp = ent_getskin(ast_i,1);
	bmap_square_drawColor(bmp_temp, 255);
        wait_for(bmp_square_drawColor);
	ent_setskin(ast_i,bmap_temp,1);

	set(ast_i,SHADOW);
	cube_deform(ast_i, (100 + random(50)));
	ast_i.scale_x = 0.05;
	ast_i.scale_y= 0.05;
	ast_i.scale_z = 0.05;
	}

ENTITY* asteroid[200]; //initialize asteroid array
// Don't think you have to move the array to global but might be part of the problem.

function generate_asteroids()
{
		int i;
	for (i=0 ; i<200 ; i++)
		{
			asteroid[i] = ent_create("cube_small.mdl", vector(500,(0 + 20*i),0), NULL);
			ent_clone(asteroid[i]);
			wait(40);
			//bmap_square_drawColor(texture2, 255); // why 2 calls?
			asteroid_modify(asteroid[i]);
		}
}



Ok I'm just guessing and off-the-cuff trying to figure it out. If this helps, Great! if not some else probably knows better.

One thing I don't know is if after a clone the entity have a new pointer... does ent_clone(x) after the shifted entity still points to 'x' ?

@wjbender - I though random start the game (first call) at the same number not every call, is that wrong?
Posted By: joao13pt

Re: How to randomly modify many entities that have the same model? - 06/08/13 19:15

@wjbender - Im completelly shure that this problem doesnt come there, i've tested it myself, thanks for the help anyways, every opinion is apreciatted here smile

@Malice - It's a good ideia, but in that array, each entity had its own clone, so even if the clone and the original have the same pointer, it wouldn't make sense for the skin to still be shared by ALL the entities with the same model, specially after i did that ent_clone.

-------------------- EDIT --------------------

I had to make a few changes to the code you wrote, because if i do this
bmp_tmp = ent_getskin(ast_i)
the function that colours the bitmaps will fail, because i think
bmap_lock(bmap, 0);
will return an error or something
but it dind't work anyways, i'll keep on trying till it works
Posted By: Wjbender

Re: How to randomly modify many entities that have the same model? - 06/08/13 19:43

@malice im not staking my life on this statement but i almost think (if started the same everytime) that subsequent random results would follow same pattern unlesa random_seed(0) were used ..feel free to correct me.

@joa you could ignore this reply if you tested as said.. Dont want to derail
Posted By: Uhrwerk

Re: How to randomly modify many entities that have the same model? - 06/08/13 20:11

Ok, bmap_square_drawColor is generating a noise texture. I don't know if this is your original intention. A simple fill of the whole bitmap might be easier to identify as different for a start. But that's not the point.

The error is in here:
Code:
function asteroid_modify(ENTITY* ast_i)
{
	[...]
	ent_cloneskin(ast_i);
	bmap_square_drawColor(texture2, 255);
	ent_setskin(ast_i,texture2,1);
}


First you clone the entity and then you assign the same skin to all of them with ent_setskin. That does not make any sense at all. You have to call bmap_square_drawColor on every individual skin of the entities. You can grab the sking with bmap_for_entity. ent_setskin sets the skin as the name says. It does not copy any content.
Posted By: joao13pt

Re: How to randomly modify many entities that have the same model? - 06/08/13 20:21

I'VE GOT IT! grin
Thank you for your help, all of you!
I'll explain:
intially, i declared the bitmap BMAP* texture2 = "empty.bmp"
empty is a totally white 256x256 bitmap
the thing is, by setting the skin of 200 different entities to the same texture2, would, obviously make them all have the exact same skin
So i thought about this, and then i remembered bmap_createblack, which creates a black bitmap, with whatever size and format i want
eventhough i repetead the bmap declaration at the start of the script, i did something diferent:
instead of
BMAP* texture2 = "some image file";
i did this
BMAP* texture;
then, in the function that colours the bitmaps, i did some changes too

Code:
void bmap_create_draw(ENTITY* ent, var alpha)
{
	texture = bmap_createblack(256, 256, 888);
	
	var format = bmap_lock(texture, 0);
	var pixel;
	var width = bmap_width(texture);
	var height = bmap_height(texture);
	var i, j;
	
	for(j=0 ; j < width ; j++)
	{
		pixel = pixel_for_vec(vector(random(255),random(255),random(255)), alpha, format);
		for(i=0 ; i< width ; i++)
		{
			pixel = pixel_for_vec(vector(random(255),random(255),random(255)), alpha, format);
			pixel_to_bmap(texture, i, j, pixel);
		}
	}
	bmap_unlock(texture);
	
	ent_setskin(ent, texture, 1);
	
}



the algorithm's pretty much what it was initially, but now as an argument i use the pointer to the entity i want to have the skin being coloured, so that in the end i can do this
ent_setskin(ent, texture, 1);
and here's the main diference
texture = bmap_createblack(256, 256, 888);
the previously declared BMAP* texture; is now "generated" multiple times as a black bitmap, which is later coloured and skinned in one of the randomly generated and deformed asteroid entities
it works perfectly, now i have the basis to make these so called "asteroids" more realistic and generate them more dinamically
Posted By: Uhrwerk

Re: How to randomly modify many entities that have the same model? - 06/08/13 20:23

You don't need to use bmap_createblack. Use bmap_for_entity instead.
Posted By: Nems

Re: How to randomly modify many entities that have the same model? - 06/08/13 20:50

Great solutions resourcing joao13pt, your going great guns....and Uhrwek, Malice and wjbender, you guys are phenominal in your assistance and expertise...inspiring...
Posted By: Anonymous

Re: How to randomly modify many entities that have the same model? - 06/08/13 21:28

@Wjbender - Just wanted to say I was wrong. From what I read in the manual you are totally right every random() call fallows the same pattern unless you call random_seed(0)
once.

"or random_seed(random(x)); before every random() call. If you like to make things over-pointless like me wink "

@Nems - Thank you, and thanks for including me with the others - who are REALLY talented users.
Posted By: Uhrwerk

Re: How to randomly modify many entities that have the same model? - 06/08/13 22:02

@Nems: Wow. Thank you very much for the compliment. blush

@Malice: you should really(!) think about what your code snippet concerning random does. grin
Posted By: joao13pt

Re: How to randomly modify many entities that have the same model? - 06/08/13 23:01

Thank you Nems for that wonderfull reply! smile
Im really enjoying being part of this community!
And i also thank Malice, Uhrwerk and Wjbender for your help!
My project (Procedural Planets + Randomly Generated Universes) is coming to life because of all the help i get from this community!
© 2024 lite-C Forums