aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/newview/llgenepool.cpp
blob: 95b15f3eed31c5eac2f0c3792159aad288eae487 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/** 
 * @file llgenepool.cpp
 * @brief LLGenePool class implementation
 *
 * Copyright (c) 2002-2007, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlife.com/developers/opensource/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at http://secondlife.com/developers/opensource/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 */

#include "llviewerprecompiledheaders.h"

#include "llgenepool.h"
#include "llxmltree.h"
#include "llvoavatar.h"
#include "lluuid.h"
#include "llagent.h"
#include "llviewervisualparam.h"
#include "llxmltree.h"
#include "llviewerimagelist.h"
#include "llappearance.h"

#include "lldir.h"

LLGenePool::LLGenePool()
	:
	mLoaded( FALSE )
{
}

LLGenePool::~LLGenePool()
{
	for( S32 i = 0; i < mArchetypes.count(); i++ )
	{
		delete mArchetypes[i];
	}
}


BOOL LLGenePool::load()
{
	char filename[MAX_PATH]; /*Flawfinder: ignore*/

	strncpy(filename,gDirUtilp->getExpandedFilename(LL_PATH_CHARACTER,"genepool.xml").c_str(), sizeof(filename) -1);		/*Flawfinder: ignore*/	
        filename[sizeof(filename) -1] = '\0';
	if( mLoaded )
	{
		return TRUE;
	}

	LLXmlTree xml_tree;
	BOOL success = xml_tree.parseFile( filename, FALSE );
	if( !success )
	{
		return FALSE;
	}

	LLXmlTreeNode* root = xml_tree.getRoot();
	if( !root )
	{
		return FALSE;
	}

	//-------------------------------------------------------------------------
	// <linden_genepool version="1.0"> (root)
	//-------------------------------------------------------------------------
	if( !root->hasName( "linden_genepool" ) )
	{
		llwarns << "Invalid linden_genepool file header: " << filename << llendl;
		return FALSE;
	}

	LLString version;
	static LLStdStringHandle version_string = LLXmlTree::addAttributeString("version");
	if( !root->getFastAttributeString( version_string, version ) || (version != "1.0") )
	{
		llwarns << "Invalid linden_genepool file version: " << version << llendl;
		return FALSE;
	}

	//-------------------------------------------------------------------------
	// <archetype>
	//-------------------------------------------------------------------------
	for (LLXmlTreeNode* child = root->getChildByName( "archetype" );
		 child;
		 child = root->getNextNamedChild())
	{
		if( !loadNodeArchetype( child ) )
		{
			return FALSE;
		}
	}

	mLoaded = TRUE;
	return TRUE;
}


//-----------------------------------------------------------------------------
// loadNodeArchetype(): loads <archetype> node from XML tree
//-----------------------------------------------------------------------------
BOOL LLGenePool::loadNodeArchetype( LLXmlTreeNode* node )
{
	llassert( node->hasName( "archetype" ) );

	LLAppearance* archetype = new LLAppearance();
	BOOL success = TRUE;

	LLVOAvatar* avatar = gAgent.getAvatarObject();
	if( !avatar )
	{
		return FALSE;
	}

	LLXmlTreeNode* child;
	for (child = node->getChildByName( "param" );
		 child;
		 child = node->getNextNamedChild())
	{
		F32 value;
		static LLStdStringHandle value_string = LLXmlTree::addAttributeString("value");
		if( !child->getFastAttributeF32( value_string, value ) )
		{
			llwarns << "avatar genepool file: <param> missing value attribute" << llendl;
			success = FALSE;
			break;
		}
			
		S32 id;
		static LLStdStringHandle id_string = LLXmlTree::addAttributeString("id");
		if( !child->getFastAttributeS32( id_string, id ) )
		{
			llwarns << "avatar genepool file: <param> missing id attribute" << llendl;
			success = FALSE;
			break;
		}

		LLVisualParam* param = avatar->getVisualParam( id );
		if( param )
		{
			archetype->addParam( id, value );
		}
		else
		{
			llwarns << "avatar genepool file: ignoring invalid <param> with id: " << id << llendl;
		}
	}
	for (child = node->getChildByName( "texture" );
		 child;
		 child = node->getNextNamedChild())
	{
		LLUUID uuid;
		static LLStdStringHandle uuid_string = LLXmlTree::addAttributeString("uuid");
		if( !child->getFastAttributeUUID( uuid_string, uuid ) )
		{
			llwarns << "avatar genepool file: <texture> missing uuid attribute" << llendl;
			success = FALSE;
			break;
		}
			
		S32 te;
		static LLStdStringHandle te_string = LLXmlTree::addAttributeString("te");
		if( !child->getFastAttributeS32( te_string, te ) )
		{
			llwarns << "avatar genepool file: <texture> missing te attribute" << llendl;
			success = FALSE;
			break;
		}

		archetype->addTexture( te, uuid );
	}

	if( success )
	{
		mArchetypes.put( archetype );
	}
	else
	{
		delete archetype;
	}
	return success;
}

// Creates new parameters for the given wearable and applies them to the avatar.
void LLGenePool::spawn( EWearableType type )
{
	LLVOAvatar* avatar = gAgent.getAvatarObject();
	if( !avatar )
	{
		return;
	}
	
	if( !mLoaded )
	{
		if( !load() )
		{
			return;
		}
	}

	if( mArchetypes.count() < 1 )
	{
		return;
	}

	// Only consider archetypes that have the same sex as you have already.
	LLVisualParam* male_param = avatar->getVisualParam( "male" );
	S32 male_param_id = male_param->getID();
	F32 sex_weight = male_param->getWeight();

	S32 i = 0;
	S32 j = 0;
	S32 k = 0;
	const S32 MAX_CYCLES = 1000;
	S32 cycles = 0;

	F32 cur_sex_weight = 0.f;
	do
	{
		i = rand() % mArchetypes.count();
		cur_sex_weight = *(mArchetypes[i]->mParamMap[ male_param_id ]);
		cycles++;
		if (cur_sex_weight != sex_weight)
		{
			break;
		}
	} while((cycles < MAX_CYCLES));

	if( cycles >= MAX_CYCLES )
	{
		return;
	}

	LLAppearance* arch1 = mArchetypes[i];
	LLAppearance* arch2 = NULL;
	LLAppearance* arch3 = NULL;

	if( mArchetypes.count() > 1 )
	{
		cycles = 0;
    	do
    	{
    		j = rand() % mArchetypes.count();
			cur_sex_weight = *(mArchetypes[j]->mParamMap[ male_param_id ]);

			cycles++;
		} while( 
			(cycles < MAX_CYCLES) && 
			( (i == j) || (cur_sex_weight != sex_weight) )
		);

		if( cycles >= MAX_CYCLES )
		{
			return;
		}

    	arch2 = mArchetypes[j];
	}

	if( mArchetypes.count() > 2 )
	{
		cycles = 0;
    	do
    	{
    		k = rand() % mArchetypes.count();
			cur_sex_weight = *(mArchetypes[k]->mParamMap[ male_param_id ]);
			cycles++;
		} while( 
			(cycles < MAX_CYCLES) && 
			( (i == k) || (j == k) || (cur_sex_weight != sex_weight) )
		);

		if( cycles >= MAX_CYCLES )
		{
			return;
		}

    	arch3 = mArchetypes[k];
	}

	// Lame generation of barycentric coordinates
	F32 b1 = F32( rand() ) / RAND_MAX;
	F32 b2 = (F32( rand() ) / RAND_MAX) * (1.f - b1);
	F32 b3 = 1.f - b1 - b2;

//	ESex old_sex = avatar->getSex();

	// Pull params
	for( LLVisualParam* param = avatar->getFirstVisualParam(); param; param = avatar->getNextVisualParam() )
	{
		if( (((LLViewerVisualParam*)param)->getWearableType() == type) && (param->getGroup() == VISUAL_PARAM_GROUP_TWEAKABLE) )
		{
			S32 param_id = param->getID();

			// don't try to blend male/female param...as fp innaccuracy will break the [0 | 1] semantics
			if (param_id != male_param_id)
			{

				F32* weight1p = arch1->getParam( param_id );
				F32 weight1 = weight1p ? *weight1p : param->getDefaultWeight();
				F32 net_weight = weight1;
				
				if( arch2 && arch3 )
				{
    				F32* weight2p = arch2->getParam( param_id );
					F32 weight2 = weight2p ? *weight2p : param->getDefaultWeight();

    				F32* weight3p = arch3->getParam( param_id );
					F32 weight3 = weight3p ? *weight3p : param->getDefaultWeight();
					
					net_weight = b1 * weight1 + b2 * weight2 + b3 * weight3;
				}
			
				param->setAnimationTarget(net_weight, TRUE);
			}
		}
	}

	// Find the archetype with the greatest influence
	LLAppearance* dominant_arch = arch1;
	if( (b2 > b1) && (b2 > b3) )
	{
		dominant_arch = arch2;
	}
	else
	if( (b3 > b1) && (b3 > b2) )
	{
		dominant_arch = arch3;
	}


	// Pull Textures from the dominant archetype
	for( S32 te = 0; te < LLVOAvatar::TEX_NUM_ENTRIES; te++ )
	{
		if( LLVOAvatar::isTextureIndexBaked( te ) )
		{
			continue;
		}

		if( LLVOAvatar::getTEWearableType( te ) == type )
		{
			LLUUID image_id = dominant_arch->getTexture( te );
			if( image_id.isNull() )
			{
				image_id = LLVOAvatar::getDefaultTEImageID( te );
			}

			LLViewerImage* image = gImageList.getImage( image_id );
			if( image )
			{
				avatar->setLocTexTE( te, image, TRUE );
			}
		}
	}

//	avatar->setVisualParamWeight( "male", sex_weight );

	avatar->startAppearanceAnimation(TRUE, TRUE);
	avatar->updateVisualParams();

// 	ESex new_sex = avatar->getSex();
//	if( old_sex != new_sex )
//	{
//		avatar->updateSexDependentLayerSets( TRUE );
//	}	
	
	avatar->updateMeshTextures();
    gAgent.sendAgentSetAppearance();
}