int SevenSegment::displayNum(double value, int dp, boolean lzsuppress) { byte segments[NDIGITS]; long integerValue; long fractionValue; double iValue; double fValue; int digitIndex = 0; int intDigits = maxdigits; if (dp > maxdigits) return -1; if (value < 0.0) negative = 1; else negative = 0; if (negative) value = -value; fValue = modf(value,&iValue); fValue += .005; //This is added so that the truncation on line 59 will behave like //the rounding in the lcd and serial libraries. integerValue = (long) iValue; fractionValue = (long) (fValue * Pow10(dp)); if (negative) { --intDigits; ++digitIndex; } intDigits -= dp; // check here for overflow if (numDigits(value) > intDigits) { return -1; //FIX DINGER //Add some logic to remove the digits greater than we can display //And display only the digits that we can. } calcValue(integerValue,intDigits,digitIndex); if (dp != 0) { calcValue(fractionValue,dp,digitIndex+intDigits); } digitIndex = 0; intDigits = maxdigits; if (negative) { segments[0] = MinusSegments; ++digitIndex; } for (int i = digitIndex; i < maxdigits; ++i) { segments[i] = pgm_read_byte (& DigitSegments[digits[i]]); } if (lzsuppress) { for (int i = digitIndex; i < maxdigits; ++i) { if (digits[i] != 0) break; if (dp > 0 && i <= ((maxdigits-1)-dp)) { segments[i] = 0x00; } } } if (dp != 0) segments[(maxdigits-1)-dp] |= DecPoint; segOutput(segments); return 0; }
long double calcMaxError(Function f, Polynomial *polynomial , size_t numof_xs, size_t numof_tests, long double min_x, long double max_x) { long double const step = (max_x - min_x) / numof_xs; Table table; initTable(&table, numof_xs, f); long double curr_x = min_x; for (size_t i = 0; i < numof_xs; ++i) { tableAppend(&table, curr_x + frand() * step); curr_x += step; } makeLagrangePolynomial(polynomial, &table); disposeTable(&table); long double const test_step = (min_x - max_x) / numof_tests; long double max_error = 0.0; long double curr_test = min_x; for (size_t i = 0; i < numof_tests; ++i) { long double const x = curr_test + frand() * test_step; long double const y = calcValue(polynomial, x); long double const right_y = f(x); long double const error = y - right_y; max_error = fabsl(error) > fabsl(max_error) ? error : max_error; curr_test += test_step; } return max_error; }
void printReport(Table const *table, char const *name) { char const func_char = table->f == my_f ? f_charrep : g_charrep; char const *func_str = table->f == my_f ? f_strrep : g_strrep; printf("\n%s for %c(x) = %s on [%Lf, %Lf]\n", name, func_char, func_str, min_x, max_x); printTable(table); long double const error_coeff = (table->f == my_f ? error_coeff_f : error_coeff_g)(table->xs.size); long double const step = (max_x - min_x) / numof_checkpoints; long double curr_x = min_x; long double max_error = 0.0; long double max_upperbound = 0.0; Polynomial polynomial; makeLagrangePolynomial(&polynomial, table); printf("Ln(%c, x) = ", func_char); printPolynomial(&polynomial, stdout); printf("\n\nNo | x | %c(x) | Ln(%c, x) | error | A | error <= A\n", func_char, func_char); printf("---+--------------+--------------+------------------+------------------+------------------------+-----------\n"); for (size_t i = 1; i <= numof_checkpoints; ++i) { long double const x = curr_x + frand() * step; long double const y = table->f(x); long double const lagrange = lagrangeValue(table, x); assert(fabsl(calcValue(&polynomial, x) - lagrange) < 1.e-6); long double const error = fabsl(y - lagrange); long double const error_upperbound = errorUpperbound(table, x, error_coeff); printf("%2zu | %+.9Lf | %+.9Lf | %+16.9Lf | %+16.9Lf | %+22.9Lf | %s\n" , i, x, y, lagrange, error, error_upperbound, error <= error_upperbound ? "Yes" : "No"); curr_x += step; max_error = error > max_error ? error : max_error; max_upperbound = error_upperbound > max_upperbound ? error_upperbound : max_upperbound; } printf("\nmax error = %+.9Lf\nmax A = %+.9Lf\n\n", max_error, max_upperbound); disposePolynomial(&polynomial); }
void BlackJackHand::add(Card* newCard) { if (!canAdd()) { return; } numCards++; cards.push_back(newCard); calcValue(); }
Vector calcValues(Polynomial const *polynomial, Vector const *points) { Vector values; initVector(&values, points->size); values.size = values.capacity; for (size_t i = 0; i < values.size; ++i) { values.values[i] = calcValue(polynomial, points->values[i]); } return values; }
BlackJackHand::BlackJackHand(Card* first) { cards = *new vector<Card*>(); cards.push_back(first); numCards = 1; isSoft = NULL; is21 = false; isBust = false; totalValue = 0; calcValue(); }
void cTrade::buyaction(int s) { char clearmsg[8]; int clear, i, j; P_ITEM buyit[256]; int amount[512]; int layer[512]; int playergoldtotal; int goldtotal; int itemtotal; int soldout; int tmpvalue=0; // Fixed for adv trade system -- Magius(CHE) § // CHARACTER cc=currchar[s]; P_CHAR pc_currchar = currchar[s]; P_ITEM pi_pack = Packitem(pc_currchar); if (pi_pack == NULL) return; //LB no player-pack - no buy action possible - and no crash too ;-) P_CHAR npc = FindCharBySerial(calcserial(buffer[s][3], buffer[s][4], buffer[s][5], buffer[s][6])); if (npc <= 0) return; clear=0; goldtotal=0; soldout=0; itemtotal=(((256*(buffer[s][1]))+buffer[s][2])-8)/7; if (itemtotal>256) return; //LB for(i = 0; i < itemtotal; i++) { layer[i]=buffer[s][8+(7*i)]; buyit[i] = FindItemBySerial(calcserial(buffer[s][8+(7*i)+1], buffer[s][8+(7*i)+2], buffer[s][8+(7*i)+3], buffer[s][8+(7*i)+4])); amount[i]=(256*(buffer[s][8+(7*i)+5]))+buffer[s][8+(7*i)+6]; if (buyit[i] != NULL) { buyit[i]->rank=10; // Fixed for adv trade system -- Magius(CHE) § tmpvalue = buyit[i]->value; tmpvalue = calcValue(buyit[i], tmpvalue); if (SrvParams->trade_system()==1) tmpvalue = calcGoodValue(pc_currchar, buyit[i], tmpvalue,0); goldtotal += (amount[i]*tmpvalue); // End Fix for adv trade system -- Magius(CHE) § } } bool useBank; useBank = (goldtotal >= SrvParams->checkBank() ); if( useBank ) playergoldtotal = GetBankCount( pc_currchar, 0x0EED ); else playergoldtotal = pc_currchar->CountGold(); if ((playergoldtotal>=goldtotal)||(pc_currchar->isGM())) { for (i = 0; i < itemtotal; i++) { if (buyit[i] != NULL) { if (buyit[i]->amount < amount[i]) { soldout = 1; } } } if (soldout) { npctalk(s, npc, "Alas, I no longer have all those goods in stock. Let me know if there is something else thou wouldst buy.",0); clear = 1; } else { if (pc_currchar->isGM()) { sprintf((char*)temp, "Here you are, %s. Someone as special as thee will receive my wares for free of course.", pc_currchar->name.c_str()); } else { if(useBank) { sprintf((char*)temp, "Here you are, %s. %d gold coin%s will be deducted from your bank account. I thank thee for thy business.", pc_currchar->name.c_str(), goldtotal, (goldtotal==1) ? "" : "s"); goldsfx(s, goldtotal); } else { sprintf((char*)temp, "Here you are, %s. That will be %d gold coin%s. I thank thee for thy business.", pc_currchar->name.c_str(), goldtotal, (goldtotal==1) ? "" : "s"); goldsfx(s, goldtotal); // Dupois, SFX for gold movement. Added Oct 08, 1998 } } npctalkall(npc, (char*)temp, 0); npcaction(npc, 0x20); // bow (Duke, 17.3.2001) clear = 1; if( !(pc_currchar->isGM() ) ) { if( useBank ) DeleBankItem( pc_currchar, 0x0EED, 0, goldtotal ); else delequan( pc_currchar, 0x0EED, goldtotal, NULL ); } for (i=0;i<itemtotal;i++) { P_ITEM pi = buyit[i]; if (pi != NULL) { if (pi->amount>amount[i]) { if (pi->pileable) { Commands->DupeItem(s, buyit[i], amount[i]); } else { for (j=0;j<amount[i];j++) { Commands->DupeItem(s, buyit[i], 1); } } pi->amount-=amount[i]; pi->restock+=amount[i]; } else { switch(layer[i]) { case 0x1A: if (pi->pileable) { Commands->DupeItem(s, buyit[i], amount[i]); } else { for (j=0;j<amount[i];j++) { Commands->DupeItem(s, buyit[i], 1); } } pi->amount=pi->amount-amount[i]; pi->restock=pi->restock+amount[i]; break; case 0x1B: if (pi->pileable) { pi->SetContSerial(pi_pack->serial); RefreshItem(buyit[i]);//AntiChrist } else { for (j=0;j<amount[i]-1;j++) { Commands->DupeItem(s, buyit[i], 1); } pi->SetContSerial(pi_pack->serial); pi->amount = 1; RefreshItem(buyit[i]);//AntiChrist } break; default: clConsole.send("ERROR: Fallout of switch statement without default. wolfpack.cpp, buyaction()\n"); //Morrolan } } } } } } else { npctalkall(npc, "Alas, thou dost not possess sufficient gold for this purchase!",0); } if (clear) { clearmsg[0]=0x3B; clearmsg[1]=0x00; clearmsg[2]=0x08; clearmsg[3]=buffer[s][3]; clearmsg[4]=buffer[s][4]; clearmsg[5]=buffer[s][5]; clearmsg[6]=buffer[s][6]; clearmsg[7]=0x00; Xsend(s, clearmsg, 8); } Weight->NewCalc(pc_currchar); // Ison 2-20-99 statwindow(s, pc_currchar); }
void cTrade::sellaction(int s) { int i, amt, value=0, totgold=0; P_ITEM pRestock = NULL; P_ITEM pNoRestock = NULL; P_ITEM pSellCont = NULL; if (buffer[s][8]!=0) { P_CHAR pc_n = FindCharBySerial(calcserial(buffer[s][3], buffer[s][4], buffer[s][5], buffer[s][6])); if (pc_n == NULL) return; P_ITEM pi; unsigned int ci; vector<SERIAL> vecContainer = contsp.getData(pc_n->serial); for ( ci = 0; ci < vecContainer.size(); ci++) { pi = FindItemBySerial(vecContainer[ci]); if (pi->layer==0x1A) pRestock = pi; // Buy Restock container else if (pi->layer==0x1B) pNoRestock = pi; // Buy no restock container else if (pi->layer==0x1C) pSellCont = pi; // Sell container } // Pre Calculate Total Amount of selling items to STOPS if the items if greater than SELLMAXITEM - Magius(CHE) int maxsell=0; i=buffer[s][8]; if (i>256) return; for (i=0;i<buffer[s][8];i++) { amt=ShortFromCharPtr(buffer[s]+9+(6*i)+4); maxsell+=amt; } if (maxsell>SrvParams->sellmaxitem()) { char tmpmsg[256]; sprintf(tmpmsg,"Sorry %s but i can buy only %i items at time!",currchar[s]->name.c_str(),SrvParams->sellmaxitem()); npctalkall(pc_n, tmpmsg,0); return; } for (i=0;i<buffer[s][8];i++) { P_ITEM pSell=FindItemBySerPtr(buffer[s]+9+(6*i)); // the item to sell if (!pSell) continue; amt=ShortFromCharPtr(buffer[s]+9+(6*i)+4); // player may have taken items out of his bp while the sell menu was up ;-) if (pSell->amount<amt) { npctalkall(pc_n, "Cheating scum! Leave now, before I call the guards!",0); return; } // Search the buy restock Container P_ITEM join = NULL; ci=0; P_ITEM pi; vector<SERIAL> vecContainer = contsp.getData(pRestock->serial); for ( ci = 0; ci < vecContainer.size(); ci++) { pi = FindItemBySerial(vecContainer[ci]); if (items_match(pi,pSell)) join = pi; } // Search the sell Container to determine the price ci=0; vecContainer.clear(); vecContainer = contsp.getData(pSellCont->serial); for ( ci = 0; ci < vecContainer.size(); ci++) { pi = FindItemBySerial(vecContainer[ci]); if (items_match(pi,pSell)) { value=pi->value; value=calcValue(pSell, value); if (SrvParams->trade_system()==1) value=calcGoodValue(currchar[s], pSell, value, 1); // Fixed for adv trade --- by Magius(CHE) § break; // let's take the first match } } totgold+=(amt*value); // add to the bill if (join != NULL) // The item goes to the container with restockable items { join->amount+=amt; join->restock-=amt; pSell->ReduceAmount(amt); } else { pSell->SetContSerial(pNoRestock->serial); SndRemoveitem(pSell->serial); if (pSell->amount!=amt) Commands->DupeItem(s, pSell, pSell->amount-amt); } } addgold(s, totgold); goldsfx(s, totgold); // Dupois, SFX for gold movement // Added Oct 08, 1998 } char clearmsg[9]; clearmsg[0]=0x3B; clearmsg[1]=0x00; clearmsg[2]=0x08; clearmsg[3]=buffer[s][3]; clearmsg[4]=buffer[s][4]; clearmsg[5]=buffer[s][5]; clearmsg[6]=buffer[s][6]; clearmsg[7]=0x00; Xsend(s, clearmsg, 8); }
int make_job(struct jobstruct job) { DB_APP app; DB_WORKUNIT wu; char* wu_template; const char *infiles[3]; char *path; char additional_xml[512]; log_messages.printf(MSG_DEBUG, "Making files\n"); // write input file in the download directory // infiles[0] = job.nameligand; infiles[1] = job.namereceptor; infiles[2] = job.confname; char path_ligand[1024]; char path_receptor[1024]; char path_conf[1024]; int retval; config.download_path(job.nameligand, path_ligand); retval = createFile(path_ligand, job.pdbligand); if (retval) { fprintf(stderr, "error making input data\n"); exit(1); } config.download_path(job.namereceptor, path_receptor); retval = createFile(path_receptor, job.pdbreceptor); if (retval) { fprintf(stderr, "error making input data\n"); exit(1); } config.download_path(job.confname, path_conf); retval = createFile(path_conf, job.pdbconf); if (retval) { fprintf(stderr, "error making input data\n"); exit(1); } log_messages.printf(MSG_DEBUG, "Done making files\n"); wu.clear(); // zeroes all fields if (!strlen(wu.name)) { /*sprintf(wu.name, "%s_%d_%f-%s-%s-%s-%s-%s-", app_name, getpid(), dtime(), job.idreceptor, job.namereceptor, job.idligand, job.nameligand, job.experiment);*/ sprintf(wu.name, "%s_%s_%s_%s", app_name, job.idligand, job.idreceptor, job.experiment); } char buff[256]; sprintf(buff, "where name='%s'", app_name); retval = app.lookup(buff); if (retval) { fprintf(stderr, "create_work: app not found\n"); exit(1); } wu.appid = app.id; wu.id = 0; wu.min_quorum = 1; wu.target_nresults = 1; wu.max_error_results = 1; wu.max_total_results = 5; wu.max_success_results = 1; wu.rsc_disk_bound = 150000000; wu.rsc_bandwidth_bound = 0.0; double value = calcValue(job.pdbligand, job.pdbreceptor); std::cout << value << std::endl; double credit = CREDIT_CORRECTOR * value; double fpops_est = FPOPS_CORRECTOR * value; wu.rsc_fpops_est = fpops_est; wu.rsc_fpops_bound = fpops_est * fpops_est; if (credit > 500) { wu.delay_bound = 21 * 86400; } else if (credit > 150) { wu.delay_bound = 14 * 86400; } else { wu.delay_bound = 7 * 86400; } //sprintf(additional_xml, "<credit>%f</credit>\n", credit); sprintf(additional_xml, "<credit>%f</credit>\n<command_line>--ligand ligand --receptor receptor --config conf --rligand %s --rreceptor %s --cpu 1</command_line>", credit, job.nameligand, job.namereceptor); log_messages.printf(MSG_DEBUG, "Start create_work()\n"); create_work(wu, in_template, "templates/vina_result.xml", "../templates/vina_result.xml", infiles, 3, config, NULL, additional_xml); log_messages.printf(MSG_DEBUG, "Done create_work()\n"); return 0; }
//o---------------------------------------------------------------------------o //| Function : void sellItem(CSocket *mSock) //| Date : Unknown //| Programmer : UOX3 DevTeam //o---------------------------------------------------------------------------o //| Purpose : Player sells an item to the vendor //o---------------------------------------------------------------------------o bool CPISellItem::Handle(void) { if (tSock->GetByte(8) != 0) { CChar *mChar = tSock->CurrcharObj(); CChar *n = calcCharObjFromSer(tSock->GetDWord(3)); if (!ValidateObject(n) || !ValidateObject(mChar)) return true; CItem *buyPack = n->GetItemAtLayer(IL_BUYCONTAINER); CItem *boughtPack = n->GetItemAtLayer(IL_BOUGHTCONTAINER); CItem *sellPack = n->GetItemAtLayer(IL_SELLCONTAINER); if (!ValidateObject(buyPack) || !ValidateObject(sellPack) || !ValidateObject(boughtPack)) return true; CItem *j = NULL, *k = NULL, *l = NULL; UI16 amt = 0, maxsell = 0; UI08 i = 0; UI32 totgold = 0, value = 0; for (i = 0; i < tSock->GetByte(8); ++i) { j = calcItemObjFromSer(tSock->GetDWord(9 + (6*i))); amt = tSock->GetWord(13 + (6*i)); maxsell += amt; } if (maxsell > cwmWorldState->ServerData()->SellMaxItemsStatus()) { n->TextMessage(NULL, 1342, TALK, false, mChar->GetName().c_str(), cwmWorldState->ServerData()->SellMaxItemsStatus()); return true; } for (i = 0; i < tSock->GetByte(8); ++i) { j = calcItemObjFromSer(tSock->GetDWord(9 + (6*i))); amt = tSock->GetWord(13 + (6*i)); if (ValidateObject(j)) { if (j->GetAmount() < amt || FindItemOwner(j) != mChar) { n->TextMessage(NULL, 1343, TALK, false); return true; } // Check if onSellToVendor JS event is present for each item being sold // If true, and a value of "false" has been returned from the script, halt the sale UI16 targTrig = j->GetScriptTrigger(); cScript *toExecute = JSMapping->GetScript(targTrig); if (toExecute != NULL) if (toExecute->OnSellToVendor(tSock, n, j)) return true; CItem *join = NULL; CDataList<CItem *> *pCont = boughtPack->GetContainsList(); for (k = pCont->First(); !pCont->Finished(); k = pCont->Next()) if (ValidateObject(k)) if (k->GetID() == j->GetID() && j->GetType() == k->GetType()) join = k; pCont = buyPack->GetContainsList(); for (k = pCont->First(); !pCont->Finished(); k = pCont->Next()) if (ValidateObject(k)) if (k->GetID() == j->GetID() && j->GetType() == k->GetType()) value = calcValue(j, k->GetSellValue()); // If an object already exist in the boughtPack that this one can be joined to... if (ValidateObject(join)) { join->IncAmount(amt); join->SetRestock(join->GetRestock() - amt); l = join; totgold += (amt * value); if (j->GetAmount() == amt) j->Delete(); else j->IncAmount(-amt); } else { //Otherwise, move this item to the vendor's boughtPack totgold += (amt * value); if (j->GetAmount() != amt) { l = Items->DupeItem(tSock, j, amt); j->SetAmount(j->GetAmount() - amt); } else l = j; if (ValidateObject(l)) l->SetCont(boughtPack); } if (l) { cScript *toGrab = JSMapping->GetScript(l->GetScriptTrigger()); if (toGrab != NULL) toGrab->OnSoldToVendor(tSock, n, l); } } } Effects->goldSound(tSock, totgold); while (totgold > MAX_STACK) { Items->CreateScriptItem(tSock, mChar, "0x0EED", MAX_STACK, OT_ITEM, true); totgold -= MAX_STACK; } if (totgold > 0) Items->CreateScriptItem(tSock, mChar, "0x0EED", totgold, OT_ITEM, true); } CPBuyItem clrSend; clrSend.Serial(tSock->GetDWord(3)); tSock->Send(&clrSend); return true; }
void UAExp::addHook(double u) { currentValue = calcValue(u); }
double UAExp::predictUtility(double u) const { return calcValue(u); }
/* * We always try the maximize the board value */ int Board::search(int depth, int alpha, int beta) { int actValue=-16000, value; Move m; MoveList list; bool stop = false; /* We make a depth search for the following move types... */ int maxType = (depth < maxDepth/2) ? Move::maxMoveType() : (depth < maxDepth) ? Move::maxPushType() : Move::maxOutType(); generateMoves(list); #ifdef MYTRACE printf(">>>>>>>> Depth %d\n", depth); #endif /* check for a old best move in main combination */ if (inMainCombination) { m = mc[depth]; if (!list.isElement(m, 0)) m.type = Move::none; if (m.type == Move::none) inMainCombination = false; if (m.type > maxType) m.type = Move::none; } if (m.type == Move::none) stop = !list.getNext(m, maxType); /* depth first search */ while(!stop) { #ifdef MYTRACE indent(depth); m.print(); printf("\n"); #endif #ifdef SPION if (bUpdateSpy) emit update(depth, 0, m, false); #endif playMove(m); if (!isValid()) value = ((depth < maxDepth) ? 15999:14999) - depth; else { /* opponent searches for his maximum; but we won't the * minimum: so change sign (for alpha/beta window too!) */ value = - search(depth+1,-beta,-alpha); } takeBack(); /* For GUI response */ if (maxDepth - depth >2) emit searchBreak(); #ifdef MYTRACE indent(depth); printf("=> (%d - %d): Value %d [ %d ] for ", alpha, beta, value,actValue); m.print(); printf("\n"); #endif #ifdef SPION if (bUpdateSpy) { if (value > actValue) emit updateBest(depth, value, m, value >= beta); emit update(depth, value, m, true); } #endif if (value > actValue) { actValue = value; mc.update(depth, m); if (bUpdateSpy && depth == 0) emit updateBestMove(m, actValue); if (actValue >= beta) { #ifdef MYTRACE indent(depth); printf("CUTOFF\n"); #endif return actValue; } if (actValue > alpha) alpha = actValue; } stop = (!list.getNext(m, maxType)) || breakOut; } /* other moves: calculate rating */ while(list.getNext(m, Move::none)) { playMove(m); if (!isValid()) value = ((depth < maxDepth) ? 15999:14999) - depth; else value = calcValue(); takeBack(); #ifdef SPION if (bUpdateSpy) { if (value > actValue) emit updateBest(depth, value, m, value >= beta); emit update(depth, value, m, true); } #endif #ifdef MYTRACE indent(depth); printf("> (%d - %d): Value %d [ %d ] for ", alpha, beta, value, actValue); m.print(); printf("\n"); #endif if (value > actValue) { actValue = value; mc.update(depth, m); if (actValue >= beta) { #ifdef MYTRACE indent(depth); printf("CUTOFF\n"); #endif break; } if (actValue > alpha) alpha = actValue; } } return actValue; }