Quake-III-Arena

Quake III Arena GPL Source Release
Log | Files | Refs

be_ai_gen.c (3954B)


      1 /*
      2 ===========================================================================
      3 Copyright (C) 1999-2005 Id Software, Inc.
      4 
      5 This file is part of Quake III Arena source code.
      6 
      7 Quake III Arena source code is free software; you can redistribute it
      8 and/or modify it under the terms of the GNU General Public License as
      9 published by the Free Software Foundation; either version 2 of the License,
     10 or (at your option) any later version.
     11 
     12 Quake III Arena source code is distributed in the hope that it will be
     13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 GNU General Public License for more details.
     16 
     17 You should have received a copy of the GNU General Public License
     18 along with Foobar; if not, write to the Free Software
     19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20 ===========================================================================
     21 */
     22 
     23 /*****************************************************************************
     24  * name:		be_ai_gen.c
     25  *
     26  * desc:		genetic selection
     27  *
     28  * $Archive: /MissionPack/code/botlib/be_ai_gen.c $
     29  *
     30  *****************************************************************************/
     31 
     32 #include "../game/q_shared.h"
     33 #include "l_memory.h"
     34 #include "l_log.h"
     35 #include "l_utils.h"
     36 #include "l_script.h"
     37 #include "l_precomp.h"
     38 #include "l_struct.h"
     39 #include "aasfile.h"
     40 #include "../game/botlib.h"
     41 #include "../game/be_aas.h"
     42 #include "be_aas_funcs.h"
     43 #include "be_interface.h"
     44 #include "../game/be_ai_gen.h"
     45 
     46 //===========================================================================
     47 //
     48 // Parameter:			-
     49 // Returns:				-
     50 // Changes Globals:		-
     51 //===========================================================================
     52 int GeneticSelection(int numranks, float *rankings)
     53 {
     54 	float sum, select;
     55 	int i, index;
     56 
     57 	sum = 0;
     58 	for (i = 0; i < numranks; i++)
     59 	{
     60 		if (rankings[i] < 0) continue;
     61 		sum += rankings[i];
     62 	} //end for
     63 	if (sum > 0)
     64 	{
     65 		//select a bot where the ones with the higest rankings have
     66 		//the highest chance of being selected
     67 		select = random() * sum;
     68 		for (i = 0; i < numranks; i++)
     69 		{
     70 			if (rankings[i] < 0) continue;
     71 			sum -= rankings[i];
     72 			if (sum <= 0) return i;
     73 		} //end for
     74 	} //end if
     75 	//select a bot randomly
     76 	index = random() * numranks;
     77 	for (i = 0; i < numranks; i++)
     78 	{
     79 		if (rankings[index] >= 0) return index;
     80 		index = (index + 1) % numranks;
     81 	} //end for
     82 	return 0;
     83 } //end of the function GeneticSelection
     84 //===========================================================================
     85 //
     86 // Parameter:			-
     87 // Returns:				-
     88 // Changes Globals:		-
     89 //===========================================================================
     90 int GeneticParentsAndChildSelection(int numranks, float *ranks, int *parent1, int *parent2, int *child)
     91 {
     92 	float rankings[256], max;
     93 	int i;
     94 
     95 	if (numranks > 256)
     96 	{
     97 		botimport.Print(PRT_WARNING, "GeneticParentsAndChildSelection: too many bots\n");
     98 		*parent1 = *parent2 = *child = 0;
     99 		return qfalse;
    100 	} //end if
    101 	for (max = 0, i = 0; i < numranks; i++)
    102 	{
    103 		if (ranks[i] < 0) continue;
    104 		max++;
    105 	} //end for
    106 	if (max < 3)
    107 	{
    108 		botimport.Print(PRT_WARNING, "GeneticParentsAndChildSelection: too few valid bots\n");
    109 		*parent1 = *parent2 = *child = 0;
    110 		return qfalse;
    111 	} //end if
    112 	Com_Memcpy(rankings, ranks, sizeof(float) * numranks);
    113 	//select first parent
    114 	*parent1 = GeneticSelection(numranks, rankings);
    115 	rankings[*parent1] = -1;
    116 	//select second parent
    117 	*parent2 = GeneticSelection(numranks, rankings);
    118 	rankings[*parent2] = -1;
    119 	//reverse the rankings
    120 	max = 0;
    121 	for (i = 0; i < numranks; i++)
    122 	{
    123 		if (rankings[i] < 0) continue;
    124 		if (rankings[i] > max) max = rankings[i];
    125 	} //end for
    126 	for (i = 0; i < numranks; i++)
    127 	{
    128 		if (rankings[i] < 0) continue;
    129 		rankings[i] = max - rankings[i];
    130 	} //end for
    131 	//select child
    132 	*child = GeneticSelection(numranks, rankings);
    133 	return qtrue;
    134 } //end of the function GeneticParentsAndChildSelection