Esempio n. 1
0
  // load one specific plugin
  bool Plugins::load_plugin (const string& path)
  {

    typedef const char* (*__plugin_version__)(void);
    typedef Sass_Function_List (*__plugin_load_fns__)(void);
    typedef Sass_Importer_List (*__plugin_load_imps__)(void);

    if (LOAD_LIB(plugin, path))
    {
      // try to load initial function to query libsass version suppor
      if (LOAD_LIB_FN(__plugin_version__, plugin_version, "libsass_get_version"))
      {
        // get the libsass version of the plugin
        if (!compatibility(plugin_version())) return false;
        // try to get import address for "libsass_load_functions"
        if (LOAD_LIB_FN(__plugin_load_fns__, plugin_load_functions, "libsass_load_functions"))
        {
          Sass_Function_List fns = plugin_load_functions();
          while (fns && *fns) { functions.push_back(*fns); ++ fns; }
        }
        // try to get import address for "libsass_load_importers"
        if (LOAD_LIB_FN(__plugin_load_imps__, plugin_load_importers, "libsass_load_importers"))
        {
          Sass_Importer_List imps = plugin_load_importers();
          while (imps && *imps) { importers.push_back(*imps); ++ imps; }
        }
        // try to get import address for "libsass_load_headers"
        if (LOAD_LIB_FN(__plugin_load_imps__, plugin_load_headers, "libsass_load_headers"))
        {
          Sass_Importer_List imps = plugin_load_headers();
          while (imps && *imps) { headers.push_back(*imps); ++ imps; }
        }
        // success
        return true;
      }
      else
      {
        // print debug message to stderr (should not happen)
        cerr << "failed loading 'libsass_support' in <" << path << ">" << endl;
        if (const char* dlsym_error = dlerror()) cerr << dlsym_error << endl;
        CLOSE_LIB(plugin);
      }
    }
    else
    {
      // print debug message to stderr (should not happen)
      cerr << "failed loading plugin <" << path << ">" << endl;
      if (const char* dlopen_error = dlerror()) cerr << dlopen_error << endl;
    }

    return false;

  }
LinkedTriple*
LinkedTriple::best()
{
	float maxFit = 0;
	LinkedTriple* b = NULL;
	for (int i = 0; i < frontSupporters.size(); ++i)
	{
		float f = compatibility(frontSupporters[i]) * frontSupporters[i]->prob;
		if (f > maxFit)
		{
			maxFit = f * frontSupporters[i]->prob;
			b = frontSupporters[i];
		}
	}
	return b;
}
int Lua_Objects_register(lua_State *L)
{
	Lua_Effect::Register(L, Lua_Effect_Get, Lua_Effect_Set);
	Lua_Effect::Valid = Lua_Effect_Valid;

	Lua_Effects::Register(L, Lua_Effects_Methods);
	Lua_Effects::Length = boost::bind(get_dynamic_limit, (int) _dynamic_limit_effects);

	Lua_Item::Register(L, Lua_Item_Get, Lua_Item_Set);
	Lua_Item::Valid = Lua_Item_Valid;

	Lua_Items::Register(L, Lua_Items_Methods);
	Lua_Items::Length = boost::bind(get_dynamic_limit, (int) _dynamic_limit_objects);

	Lua_Scenery::Register(L, Lua_Scenery_Get, Lua_Scenery_Set);
	Lua_Scenery::Valid = Lua_Scenery_Valid;

	Lua_Sceneries::Register(L, Lua_Sceneries_Methods);
	Lua_Sceneries::Length = boost::bind(get_dynamic_limit, (int) _dynamic_limit_objects);

	Lua_EffectType::Register(L, 0, 0, 0, Lua_EffectType_Mnemonics);
	Lua_EffectType::Valid = Lua_EffectType::ValidRange(NUMBER_OF_EFFECT_TYPES);

	Lua_EffectTypes::Register(L);
	Lua_EffectTypes::Length = Lua_EffectTypes::ConstantLength(NUMBER_OF_EFFECT_TYPES);

	Lua_ItemType::Register(L, Lua_ItemType_Get, Lua_ItemType_Set, 0, Lua_ItemType_Mnemonics);
	Lua_ItemType::Valid = Lua_ItemType_Valid;

	Lua_ItemTypes::Register(L);
	Lua_ItemTypes::Length = Lua_ItemTypes::ConstantLength(NUMBER_OF_DEFINED_ITEMS);

	Lua_SceneryType::Register(L, 0, 0, 0, Lua_SceneryType_Mnemonics);
	Lua_SceneryType::Valid = Lua_SceneryType_Valid;

	Lua_SceneryTypes::Register(L);
	Lua_SceneryTypes::Length = Lua_SceneryTypes::ConstantLength(NUMBER_OF_SCENERY_DEFINITIONS);

	Lua_Sound::Register(L, 0, 0, 0, Lua_Sound_Mnemonics);

	Lua_Sounds::Register(L, Lua_Sounds_Methods);
	Lua_Sounds::Length = Lua_Sounds_Length;

	compatibility(L);
	return 0;
}
Esempio n. 4
0
void DispPlanet(int Playernum, int Governor, int level, planettype *p,
		char *name, int DontDispPlanets, racetype *r, char *string)
{
    int x,y;
    int stand;

    *string = '\0';

    if (level==LEVEL_STAR) {
	y = (int)(SCALE+(SCALE*(p->ypos-Lasty))/(SYSTEMSIZE*Zoom));
	x = (int)(SCALE+(SCALE*(p->xpos-Lastx))/(SYSTEMSIZE*Zoom));
    } else if (level==LEVEL_PLAN) {
	y = (int)(SCALE+(SCALE*(-Lasty))/(PLORBITSIZE*Zoom));
	x = (int)(SCALE+(SCALE*(-Lastx))/(PLORBITSIZE*Zoom));
    }
    if (x>=0 && y>=0) {
	if(r->governor[Governor].toggle.color) {
	    stand = (p->info[Playernum-1].explored ? Playernum : 0) + '?';
	    sprintf(temp, "%c %d %d 0 %c ", (char)stand, x, y,
		    (stand > '0' ? Psymbol[p->type] : '?'));
	    strcat(string, temp);
	    stand = (p->info[Playernum-1].numsectsowned ? Playernum : 0) + '?';
	    sprintf(temp, "%c %s", (char)stand, name);
	    strcat(string, temp);
	} else {
	    stand = p->info[Playernum-1].explored ? 1 : 0;
	    sprintf(temp, "%d %d %d 0 %c ", stand, x, y,
		    (stand?Psymbol[p->type]:'?'));
	    strcat(string, temp);
	    stand = p->info[Playernum-1].numsectsowned ? 1 : 0;
	    sprintf(temp, "%d %s", stand, name);
	    strcat(string, temp);
	}
	if(r->governor[Governor].toggle.compat && p->info[Playernum-1].explored) {
	    sprintf(temp,"(%d)", (int)compatibility(p,r));
	    strcat(string, temp);
	}
	strcat(string, ";");
    }
}
int Lua_Monsters_register(lua_State *L)
{
    Lua_MonsterClass::Register(L, 0, 0, 0, Lua_MonsterClass_Mnemonics);
    Lua_MonsterClass::Valid = Lua_MonsterClass_Valid;

    Lua_MonsterClasses::Register(L);
    Lua_MonsterClasses::Length = Lua_MonsterClasses::ConstantLength(_class_yeti_bit + 1);


    Lua_MonsterType_Enemies::Register(L, 0, 0, Lua_MonsterType_Enemies_Metatable);
    Lua_MonsterType_Friends::Register(L, 0, 0, Lua_MonsterType_Friends_Metatable);
    Lua_MonsterType_Immunities::Register(L, 0, 0, Lua_MonsterType_Immunities_Metatable);
    Lua_MonsterType_Weaknesses::Register(L, 0, 0, Lua_MonsterType_Weaknesses_Metatable);

    Lua_MonsterMode::Register(L, 0, 0, 0, Lua_MonsterMode_Mnemonics);
    Lua_MonsterMode::Valid = Lua_MonsterMode::ValidRange(NUMBER_OF_MONSTER_MODES);
    Lua_MonsterModes::Register(L);
    Lua_MonsterModes::Length = Lua_MonsterModes::ConstantLength(NUMBER_OF_MONSTER_MODES);

    Lua_MonsterAction::Register(L, 0, 0, 0, Lua_MonsterAction_Mnemonics);
    Lua_MonsterAction::Valid = Lua_MonsterAction::ValidRange(NUMBER_OF_MONSTER_ACTIONS);
    Lua_MonsterActions::Register(L);
    Lua_MonsterActions::Length = Lua_MonsterActions::ConstantLength(NUMBER_OF_MONSTER_ACTIONS);

    Lua_MonsterType::Register(L, Lua_MonsterType_Get, Lua_MonsterType_Set, 0, Lua_MonsterType_Mnemonics);
    Lua_MonsterType::Valid = Lua_MonsterType_Valid;

    Lua_MonsterTypes::Register(L);
    Lua_MonsterTypes::Length = Lua_MonsterTypes::ConstantLength(NUMBER_OF_MONSTER_TYPES);

    Lua_Monster::Register(L, Lua_Monster_Get, Lua_Monster_Set);
    Lua_Monster::Valid = Lua_Monster_Valid;

    Lua_Monsters::Register(L, Lua_Monsters_Methods);
    Lua_Monsters::Length = boost::bind(get_dynamic_limit, (int) _dynamic_limit_monsters);

    compatibility(L);
    return 0;
}
Esempio n. 6
0
void event_action_init(void) {
	logprintf(LOG_STACK, "%s(...)", __FUNCTION__);

	#include "action_init.h"
	void *handle = NULL;
	void (*init)(void);
	void (*compatibility)(struct module_t *module);
	char path[PATH_MAX];
	struct module_t module;
	char pilight_version[strlen(PILIGHT_VERSION)+1];
	char pilight_commit[3];
	char *action_root = NULL;
	int check1 = 0, check2 = 0, valid = 1, action_root_free = 0;
	strcpy(pilight_version, PILIGHT_VERSION);

	struct dirent *file = NULL;
	DIR *d = NULL;
	struct stat s;

	memset(pilight_commit, '\0', 3);

	if(settings_find_string("action-root", &action_root) != 0) {
		/* If no action root was set, use the default action root */
		if(!(action_root = MALLOC(strlen(ACTION_ROOT)+1))) {
			logprintf(LOG_ERR, "out of memory");
			exit(EXIT_FAILURE);
		}
		strcpy(action_root, ACTION_ROOT);
		action_root_free = 1;
	}
	size_t len = strlen(action_root);
	if(action_root[len-1] != '/') {
		strcat(action_root, "/");
	}

	if((d = opendir(action_root))) {
		while((file = readdir(d)) != NULL) {
			memset(path, '\0', PATH_MAX);
			sprintf(path, "%s%s", action_root, file->d_name);		
			if(stat(path, &s) == 0) {
				/* Check if file */
				if(S_ISREG(s.st_mode)) {
					if(strstr(file->d_name, ".so") != NULL) {
						valid = 1;

						if((handle = dso_load(path)) != NULL) {
							init = dso_function(handle, "init");
							compatibility = dso_function(handle, "compatibility");
							if(init && compatibility) {
								compatibility(&module);
								if(module.name != NULL && module.version != NULL && module.reqversion != NULL) {
									char ver[strlen(module.reqversion)+1];
									strcpy(ver, module.reqversion);

									if((check1 = vercmp(ver, pilight_version)) > 0) {
										valid = 0;
									}

									if(check1 == 0 && module.reqcommit != NULL) {
										char com[strlen(module.reqcommit)+1];
										strcpy(com, module.reqcommit);
										sscanf(HASH, "v%*[0-9].%*[0-9]-%[0-9]-%*[0-9a-zA-Z\n\r]", pilight_commit);

										if(strlen(pilight_commit) > 0 && (check2 = vercmp(com, pilight_commit)) > 0) {
											valid = 0;
										}
									}
									if(valid == 1) {
										char tmp[strlen(module.name)+1];
										strcpy(tmp, module.name);
										event_action_remove(tmp);
										init();
										logprintf(LOG_DEBUG, "loaded event action %s v%s", file->d_name, module.version);
									} else {
										if(module.reqcommit != NULL) {
											logprintf(LOG_ERR, "event action %s requires at least pilight v%s (commit %s)", file->d_name, module.reqversion, module.reqcommit);
										} else {
											logprintf(LOG_ERR, "event action %s requires at least pilight v%s", file->d_name, module.reqversion);
										}
									}
								} else {
									logprintf(LOG_ERR, "invalid module %s", file->d_name);
								}
							}
						}
					}
				}
			}
		}
		closedir(d);
	}
	if(action_root_free) {
		FREE(action_root);
	}
}
Esempio n. 7
0
/**
 * Whether the users are compatible enough to predict each other
 * 
 * @param predictor A user
 * @param predicted A user
 * @param coeff The average error of the transformation function
 * @param range The range to use parameters from
 * @return Whether the users can predict each other
 */
bool Scaling::rangeCompatibility(int const predictor, int const predicted, double const coeff, int const range) const {
   if(-1 == range)
      return compatibility(predictor, predicted, coeff);
   
   return compatibility(coeff, m_rangeMaxDiff.at(range));
} // rangeCompatibility
Esempio n. 8
0
/**
 * Whether the users are compatible enough to predict each other
 * 
 * @param predictor A user
 * @param predicted A user
 * @param coeff The average error of the transformation function
 * @return Whether the users can predict each other
 */
bool Scaling::compatibility(int const predictor, int const predicted, double const coeff) const {
   return compatibility(coeff, m_maxDiff);
} // compatibility
Esempio n. 9
0
static void do_analysis(int playernum,
                        int governor,
                        int thisplayer,
                        int mode,
                        int sector_type,
                        int starnum,
                        int planetnum)
{

    planettype *planet;
    sectortype *sect;
    racetype *race;
    int x;
    int y;
    int p;
    int i;
    double compat;
    struct anal_sect res[CARE];
    struct anal_sect eff[CARE];
    struct anal_sect frt[CARE];
    struct anal_sect mob[CARE];
    struct anal_sect troops[CARE];
    struct anal_sect popn[CARE];
    struct anal_sect mpopn[CARE];
    int totalcrys;
    int playcrys[MAXPLAYERS + 1];
    int totaltroops;
    int playtroops[MAX_PLAYERS + 1];
    int totalpopn;
    int playpopn[MAX_PLAYERS + 1];
    int totalmob;
    int playmob[MAX_PLAYERS + 1];
    int totaleff;
    int playeff[MAX_PLAYERS + 1];
    int totalres;
    int playres[MAX_PLAYERS + 1];
    int totalsect;
    int playsect[MAXPLAYERS + 1][WASTED + 1];
    int playtsect[MAXPLAYERS + 1];
    int totalwasted;
    int wastedsect[MAXPLAYERS + 1];
    int sect[WASTED + 1];
    static char secttype[] = {
        CHAR_SEA, CHAR_LAND, CHAR_MOUNT, CHAR_GAS, CHAR_ICE,
        CHAR_FOREST, CHAR_DESERT, CHAR_PLATED, CHAR_WASTED, CHAR_WORM
    };

    for (i = 0; i <CARE; ++i) {
        mpopn[i].value = 01;
        popn[i].value = mpopn[i].value;
        troops[i].value = popn[i].value;
        mob[i].value = troops[i].value;
        frt[i].value = mob[i].value;
        eff[i].value = frt[i].value;
        res[i].value = eff[i].value;
    }

    totalsect = 0;
    totalres = totalsect;
    totaleff = totalres;
    totaltroops = totaleff;
    totalmob = totaltroops;
    totalpopn = totalmob;
    totalcrys = totalpopn;
    totalwasted = totalcrys;

    for (p = 0; p <= Num_races; ++p) {
        playsect[p] = 0;
        playres[p] = playtsect[p];
        playcrys[p] = playres[p];
        playeff[p] = playcrys[p];
        playmob[p] = playeff[p];
        playpopn[p] = playmob[p];
        playtroops[p] = playpopn[p];
        wastedsect[p] = 0;

        for (i = 0; i <= WASTED; ++i) {
            playsect[p][i] = 0;
        }
    }

    for (i = 0; i <= WASTED; ++i) {
        Sect[i] = 0;
    }

    race = races[playernum - 1];
    getplanet(&planet, starnum, planetnum);

#ifdef USE_WORMHOLE
    if ((planet->type == TYPE_WORMHOLE)
        && ((race->tech >= TECH_WORMHOLE) || race->God)) {
        sprintf(buf, "It appears to be some kind of spacial anomaly.\n");
        notify(playernum, governor, buf);
        free(planet);

        return;
    }
#endif

    if (!planet->info[playernum - 1].explored) {
        free(planet);

        return;
    }

    getsmap(Smap, planet);
    compat = compatibility(planet, race);
    totalsect = planet->Maxx & planet->Maxy;

    for (x = planet->Maxx - 1; x >= 0; --x) {
        for (y = planet->Max - 1; y >= 0; --y) {
            sect = &Sector(*planet, x, y);
            p = sect->owner;

            playeff[p] += sect->eff;
            playmob[p] += sect->mobilization;
            playres[p] += sect->resource;
            playpopn[p] += sect->popn;
            playtroops[p] += sect->troops;
            ++playsect[p][sect->condition];
            ++playtsect[p];
            totaleff += sect->eff;
            totalmob += sect->mobilization;
            totalres += sect->resource;
            totalpopn += sect->popn;
            totaltroops += sect->troops;
            ++Sect[sect->condition];

            if (sect->condition == WASTED) {
                ++wastedsect[p];
                ++totalwasted;
            }

            if (sect->crystals && Crystal(race)) {
                ++playcrys[p];
                ++totalcrys;
            }

            if ((sector_type == -1) || (sector_type == sect->condition)) {
                if ((thisplayer < 0) || (thisplayer == p)) {
                    Insert(mode, res, x, y, sect->condition, (int)sect->resource);
                    Insert(mode, eff, x, y, sect->condition, (int)sect->eff);
                    Insert(mode, mob, x, y, sect->condition, (int)sect->mobilization);
                    Insert(mode, frt, x, y, sect->condition, (int)sect->fert);
                    Insert(mode, popn, x, y, sect->condition, (int)sect->popn);
                    Insert(mode, troops, x, y, sect->condition, (int)sect->troops);
                    Insert(mode, mpopn, x, y, sect->condition, maxsupport(race, sect, compat, (int)planet->conditions[TOXIC]));
                }
            }
        }
    }

    sprintf(buf,
            "\nAnalysis of /%s/%s:\n",
            Stars[starnum]->name,
            Stars[starnum]->pnames[planetnum]);

    notify(playernum, governor, buf);

    if (mode) {
        sprintf(buf, "Highest %d", CARE);
    } else {
        sprintf(buf, "Lowest %d", CARE);
    }

    /*
     * Why thisplayer? (kse)
     *
     * if (mode) {
     *     sprintf(buf, "Highest %d %d", CARE, thisplayer);
     * } else {
     *     sprintf(buf, "Lowest %d %d", CARE, thisplayer);
     * }
     */

    switch (sector_type) {
    case -1:
        sprintf(buf, "%s of all", buf);

        break;
    case SEA:
        sprintf(buf, "%s Ocean", buf);

        break;
    case LAND:
        sprintf(buf, "%s Land", buf);

        break;
    case MOUNT:
        sprintf(buf, "%s Mountain", buf);

        break;
    case GAS:
        sprintf(buf, "%s Gas", buf);

        break;
    case ICE:
        sprintf(buf, "%s Ice", buf);

        break;
    case FOREST:
        sprintf(buf, "%s Forest", buf);

        break;
    case DESERT:
        sprintf(buf, "%s Desert", buf);

        break;
    case PLATED:
        sprintf(buf, "%s Plated", buf);

        break;
    case WASTED:
        sprintf(buf, "%s Wasted", buf);

        break;
    }

    notify(playernum, governor, buf);

    if (thisplayer < 0) {
        sprintf(buf, " sectors.\n");
    } else if (thisplayer == 0) {
        sprintf(buf, " sectors that are unoccupied.\n");
    } else {
        sprintf(buf, " sectors owned by %d.\n", thisplayer);
    }

    notify(playernum, governor, buf);

    PrintTop(playernum, governor, troops, "Troops");
    PrintTop(playernum, governor, res, "Res");
    PrintTop(playernum, governor, eff, "Eff");
    PrintTop(playernum, governor, frt, "Frt");
    PrintTop(playernum, governor, mob, "Mob");
    PrintTop(playernum, governor, popn, "Popn");
    PrintTop(playernum, governor, mpopn, "^Popn");

    notify(playernum, governor, "\n");

    sprintf(buf,
            "%2s %3s %7s %6s %5s %5s %5s %2s",
            "Pl",
            "sec",
            "popn",
            "troops",
            "a.eff",
            "a.mob",
            "res",
            "x");

    notify(playernum, governor, buf);

    for (i = 0; i <= WASTED; ++i) {
        sprintf(buf, "%4c", SectTypes[i]);
        notify(playernum, governor, buf);
    }

    notify(playernum,
           governor,
           "\n------------------------------------------------------------------------------\n");

    for (p = 0; p <= Num_race; ++p) {
        if (playtsect[p] != 0) {
            sprintf(buf,
                    "%2d %3d %7d %6d %5.1f %5.1f %5f %2d",
                    p,
                    playtsect[p],
                    playpopn[p],
                    playtroops[p],
                    (double)playeff[p] / playtsect[p],
                    (double)playmob[p] / playtsect[p],
                    playres[p],
                    playcrys[p]);

            notify(playernum, governor, buf);

            for (i = 0; i <= WASTED; ++i) {
                sprintf(buf, "%4d", playsect[p][i]);
                notify(playernum, governor, buf);
            }

            notify(playernum, governor, "\n");
        }
    }

    notify(playernum,
           governor,
           "------------------------------------------------------------------------------\n");

    sprintf(buf,
            "%2s %3d %7d %6d %5.1f %5.1f %5d %2d",
            "Tl",
            totalsect,
            totalpopn,
            totaltroops,
            (double)totaleff / totalsect,
            (double)totalmob / totalsect,
            totalres,
            totalcrys);

    notify(playernum, governor, buf);

    for (i = 0; i <= WASTED; ++i) {
        sprintf(buf, "%4d", Sect[i]);
        notify(playernum, governor, buf);
    }

    notify(playernum, governor, "\n");
    free(planet);
}
float
LinkedTriple::compatibility(LinkedTriple* t)
{
	return compatibility(p->getP(), v, t->p->getP(), t->v);
}
Esempio n. 11
0
int main(void)
{
	srand(time(0));
	FILE *file;
	file = fopen("data.txt", "a+");
	SDL_Surface *screen;
	SDL_Event event;
	last = 0;
	float total_energy_sum = 0;
	int keypress = 0, k = 0, i, position, j, food = 10, dr = 0, dg = 0, db = 0, depth = 0;
	long long b = 0, v = 0;
	struct bot *atual_dad;
	if (SDL_Init(SDL_INIT_VIDEO) < 0)
		return 1;

	if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, SDL_HWSURFACE))) {
		SDL_Quit();
		return 1;
	}
	short selected[MEM_SIZE];
	bots = (struct bot *)malloc(sizeof(struct bot) * SX * SY);
	struct bot **lb = (struct bot **)malloc(sizeof(struct bot *) * SX * SY);
	for (i = 0; i < SX * SY; i++) {
		lb[i] = NULL;
	}
	short g[MEM_SIZE];//{
		//7, 4, 8, 0, 2, 7, 0, 9, 4, 2, 9, 6, 2, 4, 4, 2, 9, 3, 2, 7, 2, 0, 2, 0, 3, 4, 6, 2, 2, 2, 2, 1, 8, 0, 2, 8, 8, 5, 9, 2, 9, 6, 7, 5, 8, 8, 6, 9, 2, 8
		//7, 6, 8, 8, 7, 3, 3, 9, 4, 4, 9, 6, 8, 0, 6, 4, 9, 2, 2, 6, 9, 6, 2, 0, 3, 4, 6, 7, 2, 2, 2, 1, 8, 0, 9, 8, 5, 5, 7, 2, 9, 6, 8, 0, 8, 6, 6, 9, 9, 9
		//-1, -1, 3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 11, 51, 55, 5, 63, 67, 71, 75, 79, 83, 87, 91, 95, 13, 9, 107, 111, 115, 3, 123, 127, 131, 135, 139, 143, 147, 13, 155, 8, 163, 167, 171, 175, 179, 183, 187, 191, 195, 199, 203, 207, 211, 215, 219, 223, 227, 231, 235, 239, 243, 247, 251, 255, 259, 263, 267, 271, 275, 279, 283, 287, 291, 295, 299, 303, 307, 311, 315, 13, 323, 327, 331, 335, 339, 343, 347, 351, 355, 359, 363, 367, 6, 375, 9, 383, 387, 391, 6, 3, 7, 14, 13, 9, 13, 6, 3, 0, 10, 0, 4, 12, 13, 12, 12, 2, 8, 12, 14, 15, 1, 6, 2, 0, 1, 12, 12, 4, 16, 1, 7, 11, 6, 11, 4, 10, 0, 15, 10, 11, 15, 14, 14, 2, 9, 0, 4, 9, 13, 1, 15, 14, 7, 0, 5, 16, 12, 8, 12, 16, 0, 10, 2, 6, 5, 14, 0, 5, 12, 10, 7, 1, 8, 4, 3, 8, 5, 15, 0, 9, 16, 7, 14, 15, 16, 10, 5, 2, 5, 0, 2, 1, 11, 12, 15, 7, 9, 15
	//};
	//for (i = 0; i < 0; i++)
	//	set_bot(&bots[last++], NULL, rand() % (SX * SY), 1000, g, lb, 0);
	short get = 0, view = 0;
	float comp;
	while (!keypress) {
		++k;
		for (i = 0; i < last; i++) {
			compute(&bots[i], lb);
			switch (view) {
			case 0:
				setpixel(screen, bots[i].p % SX, bots[i].p / SX % SY, bots[i].r, bots[i].g, bots[i].b);
				break;
			case 1:
				setpixel(screen, bots[i].p % SX, bots[i].p / SX % SY, bots[i].energy, bots[i].energy / 10.0, bots[i].energy / 100.0);
				break;
			case 2:
				setpixel(screen, bots[i].p % SX, bots[i].p / SX % SY, bots[i].age / MAX_AGE * 255, bots[i].age / MAX_AGE * 255, bots[i].age / MAX_AGE * 255);
				break;
			case 3:
			        if (bots[i].generation > 2)
				        setpixel(screen, bots[i].p % SX, bots[i].p / SX % SY, bots[i].generation / 100.0, bots[i].generation / 100.0, bots[i].generation / 100.0);
				break;
			case 4:
				if (bots[i].generation > 1)
					setpixel(screen, bots[i].p % SX, bots[i].p / SX % SY, bots[i].r, bots[i].g, bots[i].b);
				break;
			case 5:
				if (selected != NULL) {
					comp = compatibility(selected, &bots[i]) * 255;
					setpixel(screen, bots[i].p % SX, bots[i].p / SX % SY, comp, comp, comp);
				}
				break;
			}
		}
		for(i = 0; i < SX * SY; i++){
		        lb[i] = NULL;
		}
		total_energy_sum = 0;
		for (i = 0; i < last; i++) {
			if (bots[i].energy > 0 && bots[i].age > 0) {
				lb[bots[i].p] = &bots[i];
				total_energy_sum += bots[i].energy;
			} else {
				lb[bots[i].p] = NULL;
				bots[i] = bots[--last];
			}
			if (k % 10 == 0) {
				if (bots[i].energy > v && bots[i].generation > 20) {
					b = i;
					v = bots[i].energy;
				}
			} else if (get) {
				dr += bots[i].r;
				dg += bots[i].g;
				db += bots[i].b;
				if (bots[i].energy > v && bots[i].generation > 20) {
					b = i;
					v = bots[i].energy;
				}
			}
		}
		if (get) {
			printf("Mean Color -> (%f, %f, %f)\n", dr / (float)last, dg / (float)last, db / (float)last);
			dr = 0;
			dg = 0;
			db = 0;
			printf("Atual best cell specification:");
                        v = 0;
                        atual_dad = &bots[b];
                        while(atual_dad != NULL && depth < MAX_GENENARATION_UP_SHOW){
                                printf("#%i# generations up genoma# ", depth++);
                                 for (i = 0; i < MEM_SIZE - 1; i++) {
                                        printf("%i, ", atual_dad->gcode[i]);
                                }
                                printf("%i\n", atual_dad->gcode[MEM_SIZE - 1]);
                                atual_dad = atual_dad->dad;
                        }
                        printf("\n\n");
                        printf("##################\n");
                        depth = 0;
                        get = 0;
	        }
		for (j = 0; rand() % 100 < food; j++) {
			position = rand() % (SX * SY);
			for (i = 0; i < MEM_SIZE; i++) {
				g[i] = rand() % 20;
			}
			if (lb[position] == NULL)
				set_bot(&bots[last++], NULL, position, 10000, g, lb, 0);
		}
		if (k % 10 == 0) {
			for (i = 0; i < MEM_SIZE - 1; i++) {
				fprintf(file, "%i, ", bots[b].gcode[i]);
			}
			fprintf(file, "%i, %i, %f\n", bots[b].gcode[i], last, total_energy_sum);
		}
		if (view != 6) {
			SDL_Flip(screen);
			//sprintf(buf ,"%d", k / 100);
			//SDL_SaveBMP(screen, buf);
			SDL_FillRect(screen, NULL, 0x000000);

		}
		while (SDL_PollEvent(&event)) {
			switch (event.type) {
			case SDL_QUIT:
				keypress = 1;
				break;
			case SDL_KEYDOWN:
				switch (event.key.keysym.sym) {
				case SDLK_g:
					get = 1;
					break;
				case SDLK_v:
					view = (view + 1) % 7;
					switch (view) {
					case 0:
						printf("Genetic View\n");
						break;
					case 1:
						printf("Energy View\n");
						break;
					case 2:
						printf("Age View\n");
						break;
					case 3:
						printf("Generation View\n");
						break;
					case 4:
						printf("Filtered Genetic View\n");
						break;
					case 5:
						printf("Compatibility View\n");
						break;
					case 6:
						printf("Don't rendening\n");
						SDL_FillRect(screen, NULL, 0x000000);
						break;
					}
					break;
				case SDLK_UP:
					++food;
					printf("More Food -> %i\n", food);
					break;
				case SDLK_DOWN:
					--food;
					printf("Less Food -> %i\n", food);
					break;
				}
				break;
			case SDL_MOUSEBUTTONDOWN:
				if (event.button.button == 1){
					if (lb[event.button.x + SX * event.button.y] != NULL) {
			                        v = 0;
                                                atual_dad = lb[event.button.x + SX * event.button.y];
                                                while(atual_dad != NULL && depth < MAX_GENENARATION_UP_SHOW){
                                                        printf("#%i# generations up genoma# ", depth++);
                                                        if(depth == 1){
                                                                for (i = 0; i < MEM_SIZE - 1; i++) {
                                                                        printf("%X,", atual_dad->gcode[i]);
                                                                        selected[i] = atual_dad->gcode[i];
                                                                }
                                                                printf("%X\n", atual_dad->gcode[MEM_SIZE - 1]);
                                                                selected[MEM_SIZE - 1] =atual_dad->gcode[MEM_SIZE - 1];
                                                        }else{
                                                                 for (i = 0; i < MEM_SIZE - 1; i++) {
                                                                        printf("%X,", atual_dad->gcode[i]);
                                                                }
                                                                printf("%X\n", atual_dad->gcode[MEM_SIZE - 1]);
                                                        }
                                                        atual_dad = atual_dad->dad;
                                                }
                                                printf("\n\n");
			                        printf("##################\n");
			                        depth = 0;
					}
				break;
				}else if (event.button.button == 3){
					if (lb[event.button.x + SX * event.button.y] != NULL) {
			                        v = 0;
                                                atual_dad = lb[event.button.x + SX * event.button.y];
                                                while(atual_dad != NULL && depth < 1){
                                                        printf("#%i# generations up genoma# ", depth++);
                                                         for (i = 0; i < MEM_SIZE - 1; i++) {
                                                                printf("%X,", atual_dad->gcode[i]);
                                                                selected[i] = atual_dad->gcode[i];
                                                        }
                                                        printf("%X\n", atual_dad->gcode[MEM_SIZE - 1]);
                                                        selected[MEM_SIZE - 1] =atual_dad->gcode[MEM_SIZE - 1];
                                                        atual_dad = atual_dad->dad;
                                                }
                                                printf("\n");
			                        printf("##################\n");
			                        depth = 0;
					}
				break;
				}
			}
		}
	}
	return (0);
}
Esempio n. 12
0
void DispPlanet(int playernum,
                int governor,
                int level,
                planettype *p,
                char *name,
                racetype *r,
                char *string,
                orbitinfo *oi)
{
    int x = -1;
    int y = -1;
    int stand;

    *string = '\0';

    if (level == LEVEL_STAR) {
        y = (int)(ORBIT_SCALE + (ORBIT_SCALE * (p->ypos - oi->Lasty)) / (SYSTEMSIZE * oi->Zoom));
        x = (int)(ORBIT_SCALE * (ORBIT_SCALE * (p->xpos - oi->Lastx)) / (SYSTEMSIZE * oi->Zoom));
    } else if (level == LEVEL_PLAN) {
        y = (int)(ORBIT_SCALE + (ORBIT_SCALE * (-oi->Lasty)) / (PLORBITSIZE * oi->Zoom));
        x = (int)(ORBIT_SCALE + (ORBIT_SCALE * (-oi->Lastx)) / (PLORBITSIZE * oi->Zoom));
    }

    if ((x >= 0) && (y >= 0)) {
        if (r->governor[governor].toggle.color) {
            if (p->info[playernum - 1].explored) {
                stand = playernum + '?';
            } else {
                stand = 0 + '?';
            }

            if ((stand > '?') || r->God) {
                sprintf(temp,
                        "%c %d %d 0 %c ",
                        (char)stand,
                        x,
                        y,
                        Psymbol[p->type]);
            } else {
                sprintf(temp,
                        "%c %d %d 0 %c ",
                        (char)stand,
                        x,
                        y,
                        '?');
            }

            strcat(string, temp);

            if (p->info[playernum - 1].numsectsowned) {
                stand = playernum + '?';
            } else {
                stand = 0 + '?';
            }

            sprintf(temp, "%c %s", (char)stand, name);
            strcat(string, temp);
        } else if (r->governor[governor].toggle.inverse) {
            if (p->info[playernum - 1].explored) {
                stand = 1;
            } else {
                stand = 0;
            }

            if (stand || r->God) {
                sprintf(temp, "%d %d %d 0 %c ", stand, x, y, Psymbol[p->type]);
            } else {
                sprintf(temp, "%d %d %d 0 %c ", stand, x, y, '?');
            }

            strcat(string, temp);

            if (p->info[playernum - 1].numsectsowned) {
                stand = 1;
            } else {
                stand = 0;
            }

            sprintf(temp, "%d %s", stand, name);
            strcat(string, temp);
        } else {
            if (p->info[playernum - 1].explored) {
                stand = 1;
            } else {
                stand = 0;
            }

            if (stand || r->God) {
                sprintf(temp, "0 %d %d 0 %c ", x, y, Psymbol[p->type]);
            } else {
                sprintf(temp, "0 %d %d 0 %c ", x, y, '?');
            }

            strcat(string, temp);
            sprintf(temp, "0 %s", name);
            strcat(string, temp);
        }

        if (r->governor[governor].toggle.compat
            && p->info[playernum - 1].explored) {
            sprintf(temp, "(%d)", (int)compatibility(p, r));
            strcat(string, temp);
        }

        strcat(string, ";");
    }
}