Battle::Battle(std::vector<Creature*>& _combatants) : combatants(_combatants) { battleOptions = Dialogue("What will you do?", { "Attack", "Defend" }); std::map<std::string, int> names; for (auto com : combatants) { if (com->GetID() == "Player") continue; if (names.count(com->GetName()) == 0) names[com->GetName()] = 0; else if (names[com->GetName()] == 0) names[com->GetName()] = 1; } for (auto& com : combatants) { std::string newName = com->GetName(); if (names.count(com->GetName()) > 0 && names[com->GetName()] > 0) { newName += " (" + std::to_string(names[com->GetName()]) + ")"; names[com->GetName()] += 1; } com->GetName() = newName; } }
void buildatlas_area(std::vector<Area>& atlas, std::vector<Item>& items, std::vector<Weapon>& weapons, std::vector<Armour>& armour, std::vector<Creature>& creatures) { // Area definitions are somewhat more complicated: atlas.push_back(Area(Dialogue( // Standard dialogue definiton "You are in room 1", // Description { "Go to room 2", "Search" }), // Choices Inventory( // Area inventory { std::make_pair(&items[0], 5) // Pair of item and quantity }, { std::make_pair(&weapons[0], 1), // Pair of weapon and quantity std::make_pair(&weapons[1], 1) }, { std::make_pair(&armour[0], 1), std::make_pair(&armour[1], 1)// Pair of armour and quantity }), { // Creatures })); atlas.push_back(Area(Dialogue( "You are in room 2", { "Go to room 1", "Search" }), Inventory( { std::make_pair(&items[0], 10), std::make_pair(&items[1], 1) }, { }, { }), { &creatures[0] })); return; }
Battle::Battle(std::vector<Creature*>& combatants) { this->combatants = combatants; // Construct the menu this->battleOptions = Dialogue("What will you do?", { "Attack", "Defend" }); // Store the unique creature names and whether there is // only one or more of them. This code assumes that the // creatures have not already been assigned unique names, // which should be the case as a battle cannot be left // and then resumed again std::map<std::string, int> names; for(auto com : this->combatants) { // Skip the player, who shouldn't be renamed if(com->id == "player") continue; // If the name hasn't been recorded and the creature // isn't the player, record the name if(names.count(com->name) == 0) { names[com->name] = 0; } // If there is already one creature, record there are being // more than one. We don't want the actual number, simply // that there's more and so we should label them. else if(names[com->name] == 0) { names[com->name] = 1; } } // Creature unique names for the combatants for(auto& com : this->combatants) { std::string newName = com->name; // If the name is marked as being shared by more than // one creature if(names.count(com->name) > 0 && names[com->name] > 0) { // Append (1) to the end of the name, and then increase the // number for the next creature newName += " (" + std::to_string(names[com->name]) + ")"; names[com->name] += 1; } // Change the creature name to the new one, which might just be // the same as the original com->name = newName; } }
void UpdateAI(const uint32 uiDiff) override { Dialogue(uiDiff); npc_escortAI::UpdateAI(uiDiff); if (!m_creature->SelectHostileTarget() || !m_creature->getVictim()) return; if (m_uiWitherStrikeTimer < uiDiff) { if (DoCastSpellIfCan(m_creature->getVictim(), SPELL_WITHER_STRIKE) == CAST_OK) m_uiWitherStrikeTimer = urand(3000, 6000); } else m_uiWitherStrikeTimer -= uiDiff; DoMeleeAttackIfReady(); }
static void MailResult(const ExecConfig *config, char *file) { int sd, count = 0, anomaly = false; char prev_file[CF_BUFSIZE], vbuff[CF_BUFSIZE]; struct hostent *hp; struct sockaddr_in raddr; struct servent *server; struct stat statbuf; #if defined __linux__ || defined __NetBSD__ || defined __FreeBSD__ || defined __OpenBSD__ time_t now = time(NULL); #endif FILE *fp; CfOut(cf_verbose, "", "Mail result...\n"); if (cfstat(file, &statbuf) == -1) { return; } snprintf(prev_file, CF_BUFSIZE - 1, "%s/outputs/previous", CFWORKDIR); MapName(prev_file); if (statbuf.st_size == 0) { unlink(file); CfDebug("Nothing to report in %s\n", file); return; } if (CompareResult(file, prev_file) == 0) { CfOut(cf_verbose, "", "Previous output is the same as current so do not mail it\n"); return; } if ((strlen(config->mail_server) == 0) || (strlen(config->mail_to_address) == 0)) { /* Syslog should have done this */ CfOut(cf_verbose, "", "Empty mail server or address - skipping"); return; } if (config->mail_max_lines == 0) { CfDebug("Not mailing: EmailMaxLines was zero\n"); return; } CfDebug("Mailing results of (%s) to (%s)\n", file, config->mail_to_address); /* Check first for anomalies - for subject header */ if ((fp = fopen(file, "r")) == NULL) { CfOut(cf_inform, "fopen", "!! Couldn't open file %s", file); return; } while (!feof(fp)) { vbuff[0] = '\0'; if (fgets(vbuff, CF_BUFSIZE, fp) == NULL) { break; } if (strstr(vbuff, "entropy")) { anomaly = true; break; } } fclose(fp); if ((fp = fopen(file, "r")) == NULL) { CfOut(cf_inform, "fopen", "Couldn't open file %s", file); return; } CfDebug("Looking up hostname %s\n\n", config->mail_server); if ((hp = gethostbyname(config->mail_server)) == NULL) { printf("Unknown host: %s\n", config->mail_server); printf("Make sure that fully qualified names can be looked up at your site.\n"); fclose(fp); return; } if ((server = getservbyname("smtp", "tcp")) == NULL) { CfOut(cf_inform, "getservbyname", "Unable to lookup smtp service"); fclose(fp); return; } memset(&raddr, 0, sizeof(raddr)); raddr.sin_port = (unsigned int) server->s_port; raddr.sin_addr.s_addr = ((struct in_addr *) (hp->h_addr))->s_addr; raddr.sin_family = AF_INET; CfDebug("Connecting...\n"); if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { CfOut(cf_inform, "socket", "Couldn't open a socket"); fclose(fp); return; } if (connect(sd, (void *) &raddr, sizeof(raddr)) == -1) { CfOut(cf_inform, "connect", "Couldn't connect to host %s\n", config->mail_server); fclose(fp); cf_closesocket(sd); return; } /* read greeting */ if (!Dialogue(sd, NULL)) { goto mail_err; } sprintf(vbuff, "HELO %s\r\n", config->fq_name); CfDebug("%s", vbuff); if (!Dialogue(sd, vbuff)) { goto mail_err; } if (strlen(config->mail_from_address) == 0) { sprintf(vbuff, "MAIL FROM: <cfengine@%s>\r\n", config->fq_name); CfDebug("%s", vbuff); } else { sprintf(vbuff, "MAIL FROM: <%s>\r\n", config->mail_from_address); CfDebug("%s", vbuff); } if (!Dialogue(sd, vbuff)) { goto mail_err; } sprintf(vbuff, "RCPT TO: <%s>\r\n", config->mail_to_address); CfDebug("%s", vbuff); if (!Dialogue(sd, vbuff)) { goto mail_err; } if (!Dialogue(sd, "DATA\r\n")) { goto mail_err; } if (anomaly) { sprintf(vbuff, "Subject: %s **!! [%s/%s]\r\n", MailSubject(), config->fq_name, config->ip_address); CfDebug("%s", vbuff); } else { sprintf(vbuff, "Subject: %s [%s/%s]\r\n", MailSubject(), config->fq_name, config->ip_address); CfDebug("%s", vbuff); } send(sd, vbuff, strlen(vbuff), 0); #if defined __linux__ || defined __NetBSD__ || defined __FreeBSD__ || defined __OpenBSD__ strftime(vbuff, CF_BUFSIZE, "Date: %a, %d %b %Y %H:%M:%S %z\r\n", localtime(&now)); send(sd, vbuff, strlen(vbuff), 0); #endif if (strlen(config->mail_from_address) == 0) { sprintf(vbuff, "From: cfengine@%s\r\n", config->fq_name); CfDebug("%s", vbuff); } else { sprintf(vbuff, "From: %s\r\n", config->mail_from_address); CfDebug("%s", vbuff); } send(sd, vbuff, strlen(vbuff), 0); sprintf(vbuff, "To: %s\r\n\r\n", config->mail_to_address); CfDebug("%s", vbuff); send(sd, vbuff, strlen(vbuff), 0); while (!feof(fp)) { vbuff[0] = '\0'; if (fgets(vbuff, CF_BUFSIZE, fp) == NULL) { break; } CfDebug("%s", vbuff); if (strlen(vbuff) > 0) { vbuff[strlen(vbuff) - 1] = '\r'; strcat(vbuff, "\n"); count++; send(sd, vbuff, strlen(vbuff), 0); } if ((config->mail_max_lines != INF_LINES) && (count > config->mail_max_lines)) { sprintf(vbuff, "\r\n[Mail truncated by cfengine. File is at %s on %s]\r\n", file, config->fq_name); send(sd, vbuff, strlen(vbuff), 0); break; } } if (!Dialogue(sd, ".\r\n")) { CfDebug("mail_err\n"); goto mail_err; } Dialogue(sd, "QUIT\r\n"); CfDebug("Done sending mail\n"); fclose(fp); cf_closesocket(sd); return; mail_err: fclose(fp); cf_closesocket(sd); CfOut(cf_log, "", "Cannot mail to %s.", config->mail_to_address); }
static void MailResult(const ExecConfig *config, const char *file) { #if defined __linux__ || defined __NetBSD__ || defined __FreeBSD__ || defined __OpenBSD__ time_t now = time(NULL); #endif Log(LOG_LEVEL_VERBOSE, "Mail result..."); { struct stat statbuf; if (stat(file, &statbuf) == -1) { return; } if (statbuf.st_size == 0) { unlink(file); Log(LOG_LEVEL_DEBUG, "Nothing to report in file '%s'", file); return; } } { char prev_file[CF_BUFSIZE]; snprintf(prev_file, CF_BUFSIZE - 1, "%s/outputs/previous", CFWORKDIR); MapName(prev_file); if (CompareResult(file, prev_file) == 0) { Log(LOG_LEVEL_VERBOSE, "Previous output is the same as current so do not mail it"); return; } } if ((strlen(config->mail_server) == 0) || (strlen(config->mail_to_address) == 0)) { /* Syslog should have done this */ Log(LOG_LEVEL_VERBOSE, "Empty mail server or address - skipping"); return; } if (config->mail_max_lines == 0) { Log(LOG_LEVEL_DEBUG, "Not mailing: EmailMaxLines was zero"); return; } Log(LOG_LEVEL_DEBUG, "Mailing results of '%s' to '%s'", file, config->mail_to_address); /* Check first for anomalies - for subject header */ FILE *fp = fopen(file, "r"); if (!fp) { Log(LOG_LEVEL_INFO, "Couldn't open file '%s'. (fopen: %s)", file, GetErrorStr()); return; } bool anomaly = false; char vbuff[CF_BUFSIZE]; while (!feof(fp)) { vbuff[0] = '\0'; if (fgets(vbuff, sizeof(vbuff), fp) == NULL) { break; } if (strstr(vbuff, "entropy")) { anomaly = true; break; } } fclose(fp); if ((fp = fopen(file, "r")) == NULL) { Log(LOG_LEVEL_INFO, "Couldn't open file '%s'. (fopen: %s)", file, GetErrorStr()); return; } struct hostent *hp = gethostbyname(config->mail_server); if (!hp) { Log(LOG_LEVEL_ERR, "While mailing agent output, unknown host '%s'. Make sure that fully qualified names can be looked up at your site.", config->mail_server); fclose(fp); return; } struct servent *server = getservbyname("smtp", "tcp"); if (!server) { Log(LOG_LEVEL_INFO, "Unable to lookup smtp service. (getservbyname: %s)", GetErrorStr()); fclose(fp); return; } struct sockaddr_in raddr; memset(&raddr, 0, sizeof(raddr)); raddr.sin_port = (unsigned int) server->s_port; raddr.sin_addr.s_addr = ((struct in_addr *) (hp->h_addr))->s_addr; raddr.sin_family = AF_INET; Log(LOG_LEVEL_DEBUG, "Connecting..."); int sd = socket(AF_INET, SOCK_STREAM, 0); if (sd == -1) { Log(LOG_LEVEL_INFO, "Couldn't open a socket. (socket: %s)", GetErrorStr()); fclose(fp); return; } if (connect(sd, (void *) &raddr, sizeof(raddr)) == -1) { Log(LOG_LEVEL_INFO, "Couldn't connect to host '%s'. (connect: %s)", config->mail_server, GetErrorStr()); fclose(fp); cf_closesocket(sd); return; } /* read greeting */ if (!Dialogue(sd, NULL)) { goto mail_err; } sprintf(vbuff, "HELO %s\r\n", config->fq_name); Log(LOG_LEVEL_DEBUG, "%s", vbuff); if (!Dialogue(sd, vbuff)) { goto mail_err; } if (strlen(config->mail_from_address) == 0) { sprintf(vbuff, "MAIL FROM: <cfengine@%s>\r\n", config->fq_name); Log(LOG_LEVEL_DEBUG, "%s", vbuff); } else { sprintf(vbuff, "MAIL FROM: <%s>\r\n", config->mail_from_address); Log(LOG_LEVEL_DEBUG, "%s", vbuff); } if (!Dialogue(sd, vbuff)) { goto mail_err; } sprintf(vbuff, "RCPT TO: <%s>\r\n", config->mail_to_address); Log(LOG_LEVEL_DEBUG, "%s", vbuff); if (!Dialogue(sd, vbuff)) { goto mail_err; } if (!Dialogue(sd, "DATA\r\n")) { goto mail_err; } char mailsubject_anomaly_prefix[8]; if (anomaly) { strcpy(mailsubject_anomaly_prefix, "**!! "); } else { mailsubject_anomaly_prefix[0] = '\0'; } if (SafeStringLength(config->mail_subject) == 0) { snprintf(vbuff, sizeof(vbuff), "Subject: %s[%s/%s]\r\n", mailsubject_anomaly_prefix, config->fq_name, config->ip_address); Log(LOG_LEVEL_DEBUG, "%s", vbuff); } else { snprintf(vbuff, sizeof(vbuff), "Subject: %s%s\r\n", mailsubject_anomaly_prefix, config->mail_subject); Log(LOG_LEVEL_DEBUG, "%s", vbuff); } send(sd, vbuff, strlen(vbuff), 0); /* send X-CFEngine SMTP header if mailsubject set */ if (SafeStringLength(config->mail_subject) > 0) { unsigned char digest[EVP_MAX_MD_SIZE + 1]; char buffer[EVP_MAX_MD_SIZE * 4]; char *existing_policy_server = ReadPolicyServerFile(GetWorkDir()); HashPubKey(PUBKEY, digest, CF_DEFAULT_DIGEST); snprintf(vbuff, sizeof(vbuff), "X-CFEngine: vfqhost=\"%s\";ip-addresses=\"%s\";policyhub=\"%s\";pkhash=\"%s\"\r\n", VFQNAME, config->ip_addresses, existing_policy_server, HashPrintSafe(CF_DEFAULT_DIGEST, true, digest, buffer)); send(sd, vbuff, strlen(vbuff), 0); free(existing_policy_server); } #if defined __linux__ || defined __NetBSD__ || defined __FreeBSD__ || defined __OpenBSD__ strftime(vbuff, CF_BUFSIZE, "Date: %a, %d %b %Y %H:%M:%S %z\r\n", localtime(&now)); send(sd, vbuff, strlen(vbuff), 0); #endif if (strlen(config->mail_from_address) == 0) { sprintf(vbuff, "From: cfengine@%s\r\n", config->fq_name); Log(LOG_LEVEL_DEBUG, "%s", vbuff); } else { sprintf(vbuff, "From: %s\r\n", config->mail_from_address); Log(LOG_LEVEL_DEBUG, "%s", vbuff); } send(sd, vbuff, strlen(vbuff), 0); sprintf(vbuff, "To: %s\r\n\r\n", config->mail_to_address); Log(LOG_LEVEL_DEBUG, "%s", vbuff); send(sd, vbuff, strlen(vbuff), 0); int count = 0; while (!feof(fp)) { vbuff[0] = '\0'; if (fgets(vbuff, sizeof(vbuff), fp) == NULL) { break; } Log(LOG_LEVEL_DEBUG, "%s", vbuff); if (strlen(vbuff) > 0) { vbuff[strlen(vbuff) - 1] = '\r'; strcat(vbuff, "\n"); count++; send(sd, vbuff, strlen(vbuff), 0); } if ((config->mail_max_lines != INF_LINES) && (count > config->mail_max_lines)) { sprintf(vbuff, "\r\n[Mail truncated by cfengine. File is at %s on %s]\r\n", file, config->fq_name); send(sd, vbuff, strlen(vbuff), 0); break; } } if (!Dialogue(sd, ".\r\n")) { Log(LOG_LEVEL_DEBUG, "mail_err\n"); goto mail_err; } Dialogue(sd, "QUIT\r\n"); Log(LOG_LEVEL_DEBUG, "Done sending mail"); fclose(fp); cf_closesocket(sd); return; mail_err: fclose(fp); cf_closesocket(sd); Log(LOG_LEVEL_INFO, "Cannot mail to %s.", config->mail_to_address); }
void Battle::NextTurn() { std::queue<BattleEvent> events; std::sort(combatants.begin(), combatants.end(), [](Creature* a, Creature* b) { return a->GetAgility() > b->GetAgility(); }); for (auto com : combatants) { if (com->GetID() == "Player") { Dialogue targetSelection = Dialogue("Who?", {}); for (auto target : combatants) if (target->GetID() != "Player") targetSelection.AddChoice(target->GetName()); int choice = battleOptions.Activate(); switch (choice) { default: case 1: { int position = targetSelection.Activate(); for (int i = 0; i < position; i++) if (combatants[i]->GetID() == "Player") position++; Creature* target = combatants[position - 1]; events.push(BattleEvent(com, target, BattleEventType::ATTACK)); break; } case 2: { events.push(BattleEvent(com, nullptr, BattleEventType::DEFEND)); break; } } } else { Creature* player = *std::find_if(combatants.begin(), combatants.end(), [](Creature* a) { return a->GetID() == "Player"; }); events.push(BattleEvent(com, player, BattleEventType::ATTACK)); } } while (!events.empty()) { BattleEvent event = events.front(); switch (event.GetType()) { case BattleEventType::ATTACK: { auto a = combatants.begin(); auto b = combatants.end(); if (std::find(a, b, event.GetSource()) == b || std::find(a, b, event.GetTarget()) == b) break; std::cout << event.GetSource()->GetName() << " attacks " << event.GetTarget()->GetName() << " for " << event.Run() << " damage!" << std::endl; if (event.GetTarget()->GetHP() <= 0) Kill(event.GetTarget()); break; } case BattleEventType::DEFEND: std::cout << event.GetSource()->GetName() << " defends!\n"; break; default: break; } events.pop(); } }
void Battle::next_turn() { // Queue of battle events. Fastest combatants will be // at the start of the queue, and so will go first, // whereas slower ones will be at the back std::queue<BattleEvent> events; // Sort the combatants in agility order std::sort(combatants.begin(), combatants.end(), [](Creature* a, Creature* b) { return a->agility < b->agility; }); // Iterate over the combatants and decide what they should do, // before adding the action to the event queue. for(auto com : this->combatants) { if(com->id == "player") { // Create the target selection dialogue Dialogue targetSelection = Dialogue("Who?", {}); // Created every turn because some combatants may die for(auto target : this->combatants) { if(target->id != "player") { targetSelection.addChoice(target->name); } } // Ask the player for their action (attack or defend) int choice = this->battleOptions.activate(); switch(choice) { default: case 1: { // Player is attacking, so ask for the target. // Dialogue returns the number of the choice, but we // want the pointer that name corresponds to and so we // must look it up in the combatant vector Creature* target = this->combatants[targetSelection.activate()-1]; // Add the attack command to the event queue events.push(BattleEvent(com, target, BattleEventType::ATTACK)); break; } case 2: { // Player is defending, so do nothing events.push(BattleEvent(com, nullptr, BattleEventType::DEFEND)); break; } } } else { // Simple enemy AI where enemy constantly attacks player Creature* player = *std::find_if(this->combatants.begin(), this->combatants.end(), [](Creature* a) { return a->id == "player"; }); events.push(BattleEvent(com, player, BattleEventType::ATTACK)); } } // Take each event from the queue in turn and process them, // displaying the results while(!events.empty()) { // Take event from the front of the queue BattleEvent event = events.front(); switch(event.type) { case BattleEventType::ATTACK: { // The event can't be run if either the source or the // target were slain previously in this turn, so we // must check that they're valid first auto a = this->combatants.begin(); auto b = this->combatants.end(); if(std::find(a, b, event.source) == b || std::find(a, b, event.target) == b) { break; } std::cout << event.source->name << " attacks " << event.target->name << " for " << event.run() << " damage!\n"; // Delete slain enemies if(event.target->hp <= 0) { this->kill(event.target); } break; } case BattleEventType::DEFEND: std::cout << event.source->name << " defends!\n"; break; default: break; } events.pop(); } }
void MailResult(char *file,char *to) { int sd, sent, count = 0, anomaly = false; char domain[256], prev_file[CF_BUFSIZE]; struct hostent *hp; struct sockaddr_in raddr; struct servent *server; struct stat statbuf; time_t now = time(NULL); FILE *fp; if ((strlen(VMAILSERVER) == 0) || (strlen(to) == 0)) { /* Syslog should have done this */ return; } if (stat(file,&statbuf) == -1) { exit(0); } snprintf(prev_file,CF_BUFSIZE-1,"%s/outputs/previous",CFWORKDIR); if (statbuf.st_size == 0) { unlink(file); Debug("Nothing to report in %s\n",file); return; } if (CompareResult(file,prev_file) == 0 ) { Verbose("Previous output is the same as current so do not mail it\n"); return; } if (MAXLINES == 0) { Debug("Not mailing: EmailMaxLines was zero\n"); return; } Debug("Mailing results of (%s) to (%s)\n",file,to); if (strlen(to) == 0) { return; } /* Check first for anomalies - for subject header */ if ((fp=fopen(file,"r")) == NULL) { snprintf(VBUFF,CF_BUFSIZE-1,"Couldn't open file %s",file); CfLog(cfinform,VBUFF,"fopen"); return; } while (!feof(fp)) { VBUFF[0] = '\0'; fgets(VBUFF,CF_BUFSIZE,fp); if (strstr(VBUFF,"entropy")) { anomaly = true; break; } } fclose(fp); if ((fp=fopen(file,"r")) == NULL) { snprintf(VBUFF,CF_BUFSIZE-1,"Couldn't open file %s",file); CfLog(cfinform,VBUFF,"fopen"); return; } Debug("Looking up hostname %s\n\n",VMAILSERVER); if ((hp = gethostbyname(VMAILSERVER)) == NULL) { printf("Unknown host: %s\n", VMAILSERVER); printf("Make sure that fully qualified names can be looked up at your site!\n"); printf("i.e. www.gnu.org, not just www. If you use NIS or /etc/hosts\n"); printf("make sure that the full form is registered too as an alias!\n"); fclose(fp); return; } if ((server = getservbyname("smtp","tcp")) == NULL) { CfLog(cfinform,"Unable to lookup smtp service","getservbyname"); fclose(fp); return; } memset(&raddr,0,sizeof(raddr)); raddr.sin_port = (unsigned int) server->s_port; raddr.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr; raddr.sin_family = AF_INET; Debug("Connecting...\n"); if ((sd = socket(AF_INET,SOCK_STREAM,0)) == -1) { CfLog(cfinform,"Couldn't open a socket","socket"); fclose(fp); return; } if (connect(sd,(void *) &raddr,sizeof(raddr)) == -1) { snprintf(OUTPUT,CF_BUFSIZE*2,"Couldn't connect to host %s\n",VMAILSERVER); CfLog(cfinform,OUTPUT,"connect"); fclose(fp); close(sd); return; } /* read greeting */ if (!Dialogue(sd,NULL)) { goto mail_err; } sprintf(VBUFF,"HELO %s\r\n",VFQNAME); Debug("%s",VBUFF); if (!Dialogue(sd,VBUFF)) { goto mail_err; } if (strlen(MAILFROM) == 0) { sprintf(VBUFF,"MAIL FROM: <cfengine@%s>\r\n",VFQNAME); Debug("%s",VBUFF); } else { sprintf(VBUFF,"MAIL FROM: <%s>\r\n",MAILFROM); Debug("%s",VBUFF); } if (!Dialogue(sd,VBUFF)) { goto mail_err; } sprintf(VBUFF,"RCPT TO: <%s>\r\n",to); Debug("%s",VBUFF); if (!Dialogue(sd,VBUFF)) { goto mail_err; } if (!Dialogue(sd,"DATA\r\n")) { goto mail_err; } if (anomaly) { sprintf(VBUFF,"Subject: **!! (%s/%s)\r\n",VFQNAME,VIPADDRESS); Debug("%s",VBUFF); } else { sprintf(VBUFF,"Subject: (%s/%s)\r\n",VFQNAME,VIPADDRESS); Debug("%s",VBUFF); } sent=send(sd,VBUFF,strlen(VBUFF),0); #if defined LINUX || defined NETBSD || defined FREEBSD || defined OPENBSD strftime(VBUFF,CF_BUFSIZE,"Date: %a, %d %b %Y %H:%M:%S %z\r\n",localtime(&now)); sent=send(sd,VBUFF,strlen(VBUFF),0); #endif if (strlen(MAILFROM) == 0) { sprintf(VBUFF,"From: cfengine@%s\r\n",VFQNAME); Debug("%s",VBUFF); } else { sprintf(VBUFF,"From: %s\r\n",MAILFROM); Debug("%s",VBUFF); } sent=send(sd,VBUFF,strlen(VBUFF),0); sprintf(VBUFF,"To: %s\r\n\r\n",to); Debug("%s",VBUFF); sent=send(sd,VBUFF,strlen(VBUFF),0); while(!feof(fp)) { VBUFF[0] = '\0'; fgets(VBUFF,CF_BUFSIZE,fp); Debug("%s",VBUFF); if (strlen(VBUFF) > 0) { VBUFF[strlen(VBUFF)-1] = '\r'; strcat(VBUFF, "\n"); count++; sent=send(sd,VBUFF,strlen(VBUFF),0); } if ((MAXLINES != INF_LINES) && (count > MAXLINES)) { sprintf(VBUFF,"\r\n[Mail truncated by cfengine. File is at %s on %s]\r\n",file,VFQNAME); sent=send(sd,VBUFF,strlen(VBUFF),0); break; } } if (!Dialogue(sd,".\r\n")) { Debug("mail_err\n"); goto mail_err; } Dialogue(sd,"QUIT\r\n"); Debug("Done sending mail\n"); fclose(fp); close(sd); return; mail_err: fclose(fp); close(sd); sprintf(VBUFF, "Cannot mail to %s.", to); CfLog(cflogonly,VBUFF,""); }
bool Story::loadDialogue(const char* file) { std::ifstream dialogueFile; dialogueFile.open(file); if(!dialogueFile.is_open()) { return false; } int stateIndex = -1; //int dialogueIndex = 0; while(!dialogueFile.eof()) { int i; std::string dialogueString = ""; dialogueFile >> i; if(i > -1) { stateIndex++; std::vector<Dialogue> dialogues; std::getline(dialogueFile, dialogueString); dialogues.push_back(Dialogue(dialogueString, i)); dialogueTree.push_back(dialogues); } else { int newState; //while(! dialogueFile >> newState) bool intFound = false; std::string word; while(!intFound) { dialogueFile >> word; if(std::isdigit(word[0])) { newState = std::atoi(word.data()); intFound = true; } else { dialogueString += word; dialogueString += " "; } } dialogueTree[stateIndex].push_back(Dialogue(dialogueString, newState)); } } dialogueFile.close(); return true; }
void SceneMenu::Init() { srand(time(NULL)); icon = 31.6; icon2 = 19; menuIcon = 116; choose = menushop.STARTGAME; c_option = menushop.O_SETTING; Input = "Menu"; Dialogue("Text//Dialogue1.txt"); // Set background color to dark blue glClearColor(0.0f, 0.0f, 0.4f, 0.0f); //Enable depth buffer and depth testing glEnable(GL_DEPTH_TEST); //Enable back face culling //glEnable(GL_CULL_FACE); //Default to fill mode glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // Generate a default VAO for now glGenVertexArrays(1, &m_vertexArrayID); glBindVertexArray(m_vertexArrayID); //m_programID = LoadShaders("Shader//Texture.vertexshader", "Shader//Blending.fragmentshader"); m_programID = LoadShaders("Shader//Texture.vertexshader", "Shader//Text.fragmentshader"); m_parameters[U_MVP] = glGetUniformLocation(m_programID, "MVP"); m_parameters[U_MODELVIEW] = glGetUniformLocation(m_programID, "MV"); m_parameters[U_MODELVIEW_INVERSE_TRANSPOSE] = glGetUniformLocation(m_programID, "MV_inverse_transpose"); m_parameters[U_MATERIAL_AMBIENT] = glGetUniformLocation(m_programID, "material.kAmbient"); m_parameters[U_MATERIAL_DIFFUSE] = glGetUniformLocation(m_programID, "material.kDiffuse"); m_parameters[U_MATERIAL_SPECULAR] = glGetUniformLocation(m_programID, "material.kSpecular"); m_parameters[U_MATERIAL_SHININESS] = glGetUniformLocation(m_programID, "material.kShininess"); m_parameters[U_LIGHT0_POSITION] = glGetUniformLocation(m_programID, "lights[0].position_cameraspace"); m_parameters[U_LIGHT0_COLOR] = glGetUniformLocation(m_programID, "lights[0].color"); m_parameters[U_LIGHT0_POWER] = glGetUniformLocation(m_programID, "lights[0].power"); m_parameters[U_LIGHT0_KC] = glGetUniformLocation(m_programID, "lights[0].kC"); m_parameters[U_LIGHT0_KL] = glGetUniformLocation(m_programID, "lights[0].kL"); m_parameters[U_LIGHT0_KQ] = glGetUniformLocation(m_programID, "lights[0].kQ"); m_parameters[U_LIGHT0_TYPE] = glGetUniformLocation(m_programID, "lights[0].type"); m_parameters[U_LIGHT0_SPOTDIRECTION] = glGetUniformLocation(m_programID, "lights[0].spotDirection"); m_parameters[U_LIGHT0_COSCUTOFF] = glGetUniformLocation(m_programID, "lights[0].cosCutoff"); m_parameters[U_LIGHT0_COSINNER] = glGetUniformLocation(m_programID, "lights[0].cosInner"); m_parameters[U_LIGHT0_EXPONENT] = glGetUniformLocation(m_programID, "lights[0].exponent"); m_parameters[U_LIGHTENABLED] = glGetUniformLocation(m_programID, "lightEnabled"); m_parameters[U_NUMLIGHTS] = glGetUniformLocation(m_programID, "numLights"); // Get a handle for our "colorTexture" uniform m_parameters[U_COLOR_TEXTURE_ENABLED] = glGetUniformLocation(m_programID, "colorTextureEnabled"); m_parameters[U_COLOR_TEXTURE] = glGetUniformLocation(m_programID, "colorTexture"); // Get a handle for our "textColor" uniform m_parameters[U_TEXT_ENABLED] = glGetUniformLocation(m_programID, "textEnabled"); m_parameters[U_TEXT_COLOR] = glGetUniformLocation(m_programID, "textColor"); //Enable blending glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glUseProgram(m_programID); light[0].type = Light::LIGHT_POINT; light[0].position.Set(250, 55, -90); light[0].color.Set(1, 1, 1); light[0].power = 4; light[0].kC = 5.f; light[0].kL = 0.01f; light[0].kQ = 0.001f; light[0].cosCutOff = cos(Math::DegreeToRadian(45)); light[0].cosInner = cos(Math::DegreeToRadian(30)); light[0].exponent = 3.f; light[0].spotDirection.Set(0.f, 1.f, 0.f); //Make sure you pass uniform parameters after glUseProgram() glUniform1i(m_parameters[U_LIGHT0_TYPE], light[0].type); glUniform3fv(m_parameters[U_LIGHT0_COLOR], 1, &light[0].color.r); glUniform1f(m_parameters[U_LIGHT0_POWER], light[0].power); glUniform1f(m_parameters[U_LIGHT0_KC], light[0].kC); glUniform1f(m_parameters[U_LIGHT0_KL], light[0].kL); glUniform1f(m_parameters[U_LIGHT0_KQ], light[0].kQ); glUniform1f(m_parameters[U_LIGHT0_COSCUTOFF], light[0].cosCutOff); glUniform1f(m_parameters[U_LIGHT0_COSINNER], light[0].cosInner); glUniform1f(m_parameters[U_LIGHT0_EXPONENT], light[0].exponent); glUniform1i(m_parameters[U_NUMLIGHTS], 6); //Initialize camera settings camera.Init(Vector3(-300, -10, 0), Vector3(0, 0, 0), Vector3(0, 1, 0)); meshList[GEO_REF_AXES] = MeshBuilder::GenerateAxes("reference", 1000, 1000, 1000); //meshList[GEO_CUBE] = MeshBuilder::GenerateCube("cube", Color(1, 1, 0)); meshList[GEO_QUAD] = MeshBuilder::GenerateRepeatQuad("quad", Color(1, 1, 0), 1, 1, 10); meshList[GEO_QUAD]->textureID = LoadTGA("Image//Tile.tga"); meshList[GEO_QUAD]->material.kAmbient.Set(0.1f, 0.1f, 0.1f); meshList[GEO_QUAD]->material.kDiffuse.Set(0.6f, 0.6f, 0.6f); meshList[GEO_QUAD]->material.kSpecular.Set(0.7f, 0.7f, 0.7f); meshList[GEO_QUAD]->material.kShininess = 1.f; meshList[GEO_FLOOR] = MeshBuilder::GenerateRepeatQuad("floor", Color(1, 1, 1), 1.015, 1, 10); meshList[GEO_FLOOR]->textureID = LoadTGA("Image//Tile2.tga"); meshList[GEO_FLOOR]->material.kAmbient.Set(0.1f, 0.1f, 0.1f); meshList[GEO_FLOOR]->material.kDiffuse.Set(0.6f, 0.6f, 0.6f); meshList[GEO_FLOOR]->material.kSpecular.Set(0.7f, 0.7f, 0.7f); meshList[GEO_FLOOR]->material.kShininess = 1.f; meshList[GEO_PATH] = MeshBuilder::GenerateQuad("land", Color(1, 1, 1), 14, 13); meshList[GEO_PATH]->textureID = LoadTGA("Image//Menu.tga"); meshList[GEO_TEXT] = MeshBuilder::GenerateText("text", 16, 16); meshList[GEO_TEXT]->textureID = LoadTGA("Image//Text2.tga"); GLuint wood = LoadTGA("Image//book.tga"); GLuint textID = LoadTGA("Image//Chair.tga"); meshList[GEO_BENCH] = MeshBuilder::GenerateOBJ("bench", "OBJ//Bench.obj"); meshList[GEO_BENCH]->textureID = wood; GLuint santa = LoadTGA("Image//Santa.tga"); meshList[GEO_VENDING] = MeshBuilder::GenerateOBJ("VM", "OBJ//shelves.obj"); meshList[GEO_VENDING]->textureID = LoadTGA("Image//vending.tga"); meshList[GEO_BUILDING] = MeshBuilder::GenerateOBJ("Building", "OBJ//building.obj"); meshList[GEO_BUILDING]->textureID = LoadTGA("Image//b1.tga"); meshList[GEO_COKE] = MeshBuilder::GenerateOBJ("coke", "OBJ//coke.obj"); meshList[GEO_COKE]->textureID = LoadTGA("Image//coke.tga"); meshList[GEO_STAR] = MeshBuilder::GenerateOBJ("Star", "OBJ//Star.obj"); meshList[GEO_STAR]->textureID = LoadTGA("Image//sand_2.tga"); meshList[GEO_LIGHTBALL] = MeshBuilder::GenerateSpheres("Sph", Color(1, 1, 1), 18, 36); Mtx44 projection; projection.SetToPerspective(45.0f, 16.0f / 9.0f, 0.1f, 10000.0f); projectionStack.LoadMatrix(projection); }
static void MailResult(const ExecConfig *config, const char *file) { size_t line_size = CF_BUFSIZE; char *line = xmalloc(line_size); ssize_t read; #if defined __linux__ || defined __NetBSD__ || defined __FreeBSD__ || defined __OpenBSD__ time_t now = time(NULL); #endif Log(LOG_LEVEL_VERBOSE, "Mail report: sending result..."); { struct stat statbuf; if (stat(file, &statbuf) == -1) { return; } if (statbuf.st_size == 0) { unlink(file); Log(LOG_LEVEL_DEBUG, "Mail report: nothing to report in file '%s'", file); return; } } { char prev_file[CF_BUFSIZE]; snprintf(prev_file, CF_BUFSIZE, "%s/outputs/previous", GetWorkDir()); MapName(prev_file); if (CompareResultEqualOrFiltered(config, file, prev_file)) { Log(LOG_LEVEL_VERBOSE, "Mail report: previous output is the same as current so do not mail it"); return; } } if ((strlen(config->mail_server) == 0) || (strlen(config->mail_to_address) == 0)) { /* Syslog should have done this */ Log(LOG_LEVEL_VERBOSE, "Mail report: empty mail server or address - skipping"); return; } if (config->mail_max_lines == 0) { Log(LOG_LEVEL_DEBUG, "Mail report: not mailing because EmailMaxLines was zero"); return; } Log(LOG_LEVEL_DEBUG, "Mail report: mailing results of '%s' to '%s'", file, config->mail_to_address); FILE *fp = fopen(file, "r"); if (fp == NULL) { Log(LOG_LEVEL_ERR, "Mail report: couldn't open file '%s'. (fopen: %s)", file, GetErrorStr()); return; } int sd = ConnectToSmtpSocket(config); if (sd < 0) { fclose(fp); return; } /* read greeting */ if (!Dialogue(sd, NULL)) { goto mail_err; } char vbuff[CF_BUFSIZE]; snprintf(vbuff, sizeof(vbuff), "HELO %s\r\n", config->fq_name); Log(LOG_LEVEL_DEBUG, "Mail report: %s", vbuff); if (!Dialogue(sd, vbuff)) { goto mail_err; } if (strlen(config->mail_from_address) == 0) { snprintf(vbuff, sizeof(vbuff), "MAIL FROM: <cfengine@%s>\r\n", config->fq_name); Log(LOG_LEVEL_DEBUG, "Mail report: %s", vbuff); } else { snprintf(vbuff, sizeof(vbuff), "MAIL FROM: <%s>\r\n", config->mail_from_address); Log(LOG_LEVEL_DEBUG, "Mail report: %s", vbuff); } if (!Dialogue(sd, vbuff)) { goto mail_err; } snprintf(vbuff, sizeof(vbuff), "RCPT TO: <%s>\r\n", config->mail_to_address); Log(LOG_LEVEL_DEBUG, "Mail report: %s", vbuff); if (!Dialogue(sd, vbuff)) { goto mail_err; } if (!Dialogue(sd, "DATA\r\n")) { goto mail_err; } if (SafeStringLength(config->mail_subject) == 0) { snprintf(vbuff, sizeof(vbuff), "Subject: [%s/%s]\r\n", config->fq_name, config->ip_address); Log(LOG_LEVEL_DEBUG, "Mail report: %s", vbuff); } else { snprintf(vbuff, sizeof(vbuff), "Subject: %s\r\n", config->mail_subject); Log(LOG_LEVEL_DEBUG, "Mail report: %s", vbuff); } send(sd, vbuff, strlen(vbuff), 0); /* Send X-CFEngine SMTP header */ unsigned char digest[EVP_MAX_MD_SIZE + 1]; char buffer[CF_HOSTKEY_STRING_SIZE]; char *existing_policy_server = ReadPolicyServerFile(GetWorkDir()); if (!existing_policy_server) { existing_policy_server = xstrdup("(none)"); } HashPubKey(PUBKEY, digest, CF_DEFAULT_DIGEST); snprintf(vbuff, sizeof(vbuff), "X-CFEngine: vfqhost=\"%s\";ip-addresses=\"%s\";policyhub=\"%s\";pkhash=\"%s\"\r\n", VFQNAME, config->ip_addresses, existing_policy_server, HashPrintSafe(buffer, sizeof(buffer), digest, CF_DEFAULT_DIGEST, true)); send(sd, vbuff, strlen(vbuff), 0); free(existing_policy_server); #if defined __linux__ || defined __NetBSD__ || defined __FreeBSD__ || defined __OpenBSD__ strftime(vbuff, CF_BUFSIZE, "Date: %a, %d %b %Y %H:%M:%S %z\r\n", localtime(&now)); send(sd, vbuff, strlen(vbuff), 0); #endif if (strlen(config->mail_from_address) == 0) { snprintf(vbuff, sizeof(vbuff), "From: cfengine@%s\r\n", config->fq_name); Log(LOG_LEVEL_DEBUG, "Mail report: %s", vbuff); } else { snprintf(vbuff, sizeof(vbuff), "From: %s\r\n", config->mail_from_address); Log(LOG_LEVEL_DEBUG, "Mail report: %s", vbuff); } send(sd, vbuff, strlen(vbuff), 0); snprintf(vbuff, sizeof(vbuff), "To: %s\r\n\r\n", config->mail_to_address); Log(LOG_LEVEL_DEBUG, "Mail report: %s", vbuff); send(sd, vbuff, strlen(vbuff), 0); int count = 0; while ((read = CfReadLine(&line, &line_size, fp)) > 0) { if (LineIsFiltered(config, line)) { continue; } if (send(sd, line, read, 0) == -1 || send(sd, "\r\n", 2, 0) == -1) { Log(LOG_LEVEL_ERR, "Error while sending mail to mailserver " "'%s'. (send: '%s')", config->mail_server, GetErrorStr()); goto mail_err; } count++; if ((config->mail_max_lines != INF_LINES) && (count >= config->mail_max_lines)) { snprintf(line, line_size, "\r\n[Mail truncated by CFEngine. File is at %s on %s]\r\n", file, config->fq_name); if (send(sd, line, strlen(line), 0)) { Log(LOG_LEVEL_ERR, "Error while sending mail to mailserver " "'%s'. (send: '%s')", config->mail_server, GetErrorStr()); goto mail_err; } break; } } if (!Dialogue(sd, ".\r\n")) { Log(LOG_LEVEL_DEBUG, "Mail report: mail_err\n"); goto mail_err; } Dialogue(sd, "QUIT\r\n"); Log(LOG_LEVEL_DEBUG, "Mail report: done sending mail"); free(line); fclose(fp); cf_closesocket(sd); return; mail_err: free(line); fclose(fp); cf_closesocket(sd); Log(LOG_LEVEL_ERR, "Mail report: cannot mail to %s.", config->mail_to_address); }