Exemple #1
0
        void run_core(std::size_t core_id)
        {
            task_pack<TASK> tmp_task;

            while(cores_[core_id].running)
            {
                if(cores_[core_id].queue_size() > 0)
                { // Still has unfinished tasks.
                    cores_[core_id].execute(this);
                }
                else if(can_steal())
                { // Steal from other cores (at random).
                    try
                    {
                        tmp_task = steal_task();
                    }
                    catch(...)
                    {
                        // The queue got emptied before steal_task() could
                        // finnish.
                        cores_[core_id].running = false;
                        break;
                    }

                    cores_[core_id].add_task(std::move(tmp_task));
                    cores_[core_id].execute(this);
                }
                else
                { // No task in the entire schedulet that could be run
                  // by this core.
                    cores_[core_id].running = false;
                }
            }
        }
TSheet random_event(int n) {
  TSheet event_sheet;
  char str[100];
  int r=0, s=0, t[DRUG_COUNT];
  
  strcpy(event_sheet.title, "");
 
  switch (n) {
    case 0: // cops (1-3)
      r=((rand() % 2)+1);
      sprintf(str, "The cops are after you!\nYou get shot %d times.\nYou escape.", r);
      game.life -= r;
      
      break;
    case 1: // found (1-10)
      r=can_hold( (rand() % 9) +1);
      s=(rand() % DRUG_COUNT);

      sprintf(str, "You find %d units of\n%s inside a brown\npaper bag.", r, buy.menuitems[s].caption);
      game.coat += r;
      game.drugs[s] += r;
//      game.old_price[s] += game.price[s];
      
      break;
    case 2: // stolen (1-10)
      // only gets a random value between the drugs we have
      // first we get how many drugs have non-zero values, put them onto an array and choose a random from that
      for (s=0; s < DRUG_COUNT; s++) if (game.drugs[s]) t[r++] = s;
      if (! r) return (random_event(rand() % EVENT_COUNT));
      s=(rand() % r);
      r=can_steal(t[s], ((rand() % 9) + 1));

      sprintf(str, "A junkie steals %d units\nof %s from you.", r, buy.menuitems[t[s]].caption);
      game.coat -= r;
      game.drugs[t[s]] -= r;
      
      break;
    case 3: // steal (1-500)
      r=can_hold( (rand() % 499) + 1);
      sprintf(str, "You steal a tourist's\nwallet and find $%d.", r);
      game.money += r;
      
      break;
    case 4: // supply down (15%)
      r = (rand() % DRUG_COUNT);
      sprintf(str, "The supply of %s\nis short.\nPrices are down.", buy.menuitems[r].caption);
      game.price[r] = (game.price[r] * .15);
      
      break;
    case 5: // supply up (50%)
      r = (rand() % DRUG_COUNT);
      sprintf(str, "The supply of %s\nis high.\nPrices are up.", buy.menuitems[r].caption);
      game.price[r] += (game.price[r] * .15);
      
      break;
  }

  strcpy(event_sheet.text, str);
  strcpy(event_sheet.button, "Ok");
  event_sheet.ammount = -1;

  return (event_sheet);
}