Example #1
0
// @inc: >0, decrease loyalty; < 0, increase loyalty
int hero::increase_catalog(int inc, hero& leader)
{
	static int mid_catalog = HERO_MAX_LOYALTY / 2;
	static int max_catalog = HERO_MAX_LOYALTY;

	if (leader.base_catalog_ == base_catalog_) {
		// this is faithful hero, don't change.
		return loyalty(leader);
	}
	
	int diff = posix_abs((int)leader.base_catalog_ - base_catalog_);
	if (diff > mid_catalog) {
		diff = max_catalog - diff;
	}

	if (inc < 0 && -1 * inc >= diff) {
		// if increase loyalty, don't over HERO_MAX_LOYALTY.
		base_catalog_ = leader.base_catalog_;
	} else {
		int inc_catalog = inc;

		// enlarger distance base_catalog_ and base_catalog_ of leader
		uint16_t leader_base_catalog = leader.base_catalog_;
		bool up_base_catalog = false;
		if (base_catalog_ > leader_base_catalog) {
			if (base_catalog_ - leader_base_catalog < mid_catalog) {
				up_base_catalog = true;
			}
		} else if (leader_base_catalog - base_catalog_ > mid_catalog) {
			up_base_catalog = true;
		}
		if ((up_base_catalog && inc_catalog > 0) || (!up_base_catalog && inc_catalog < 0)) {
			if (inc_catalog < 0) {
				inc_catalog *= -1;
			}
			base_catalog_ += inc_catalog;
			if (base_catalog_ >= max_catalog) {
				base_catalog_ -= max_catalog;
			}
		} else if ((!up_base_catalog && inc_catalog > 0) || (up_base_catalog && inc_catalog < 0)) {
			if (inc_catalog < 0) {
				inc_catalog *= -1;
			}
			if (base_catalog_ >= inc_catalog) {
				base_catalog_ -= inc_catalog;
			} else {
				base_catalog_ = max_catalog - (inc_catalog - base_catalog_);
			}
		}
	}

	// make sure loyalty >= base loyalty
	int base_loyalty = posix_abs((int)leader.base_catalog_ - base_catalog_);
	if (base_loyalty < mid_catalog) {
		base_loyalty = max_catalog - base_loyalty;
	}
	set_loyalty(leader, base_loyalty);

	return base_catalog_;
}
Example #2
0
// @inc: >0, decrease loyalty; < 0, increase loyalty
int hero::increase_loyalty(int inc, hero& leader)
{
	static int mid_loyalty16 = ftofxp8(HERO_MAX_LOYALTY / 2);
	static int max_loyalty16 = ftofxp8(HERO_MAX_LOYALTY);

	if (leader.base_catalog_ == base_catalog_) {
		// this is faithful hero, don't change.
		return loyalty(leader);
	}
	
	// effect factor: amtition, heart
	double inc_catalog_float = 1.0 * ambition_ * INC_CATALOG_UNIT_AMBITION + 1.0 * (255 - heart_) / 64 * INC_CATALOG_UNIT_HEART;
	int inc_catalog = inc * std::max<int>((int)inc_catalog_float, INC_CATALOG_UNIT_AMBITION + INC_CATALOG_UNIT_HEART);
	// effect factor: base catalog
	int mid = posix_abs((int)leader.base_catalog_ - base_catalog_);
	if (mid > HERO_MAX_LOYALTY / 2) {
		mid = HERO_MAX_LOYALTY - mid;
	}
	inc_catalog *= (mid + 14) / 15;

	// enlarger distance float_catalog_ and base_catalog_ of leader
	uint16_t leader_base_catalog16 = ftofxp8(leader.base_catalog_);
	bool up_float_catalog = false;
	if (float_catalog_ > leader_base_catalog16) {
		if (float_catalog_ - leader_base_catalog16 < mid_loyalty16) {
			up_float_catalog = true;
		}
	} else if (leader_base_catalog16 - float_catalog_ > mid_loyalty16) {
		up_float_catalog = true;
	}
	if ((up_float_catalog && inc_catalog > 0) || (!up_float_catalog && inc_catalog < 0)) {
		if (inc_catalog < 0) {
			inc_catalog *= -1;
		}
		// float_catalog_ += inc_catalog
		float_catalog_ += inc_catalog;
		if (float_catalog_ >= max_loyalty16) {
			float_catalog_ -= max_loyalty16;
		}
	} else if ((!up_float_catalog && inc_catalog > 0) || (up_float_catalog && inc_catalog < 0)) {
		if (inc_catalog < 0) {
			inc_catalog *= -1;
		}
		// float_catalog_ -= inc_catalog
		if (float_catalog_ >= inc_catalog) {
			float_catalog_ -= inc_catalog;
		} else {
			float_catalog_ = max_loyalty16 - (inc_catalog - float_catalog_);
		}
	}

	// calculate loyalty. same as loyalty(...)
	mid = posix_abs((int)leader.float_catalog_ - float_catalog_);
	mid = fxptoi8(mid);
	if (mid < HERO_MAX_LOYALTY / 2) {
		mid = HERO_MAX_LOYALTY - mid;
	}
	return mid;
}
Example #3
0
int hero::base_loyalty(const hero& leader) const
{
	int mid = posix_abs((int)leader.base_catalog_ - base_catalog_);
	if (mid < HERO_MAX_LOYALTY / 2) {
		mid = HERO_MAX_LOYALTY - mid;
	}
	return mid;
}
Example #4
0
int hero::loyalty(const hero& leader) const
{
	int mid = posix_abs((int)leader.float_catalog_ - float_catalog_);
	mid = fxptoi8(mid);
	if (mid < HERO_MAX_LOYALTY / 2) {
		mid = HERO_MAX_LOYALTY - mid;
	}
	return mid;
}
Example #5
0
void hero::set_loyalty(hero& leader, int level, bool fixed)
{
	if (level > HERO_MAX_LOYALTY) {
		return;
	}
	int cur_level = loyalty(leader);
	if (cur_level == level) {
		return;
	}
	if (!fixed && cur_level >= level) {
		return;
	}

	bool up_float_catalog = false;
	int mid = posix_abs((int)leader.base_catalog_ - base_catalog_);
	if (mid < HERO_MAX_LOYALTY / 2) {
		if (leader.base_catalog_ < base_catalog_) {
			up_float_catalog = true;
		}
	} else {
		if (leader.base_catalog_ >= base_catalog_) {
			up_float_catalog = true;
		}
	}
	int to_catalog = leader.base_catalog_;
	if (up_float_catalog) {
		to_catalog = to_catalog + HERO_MAX_LOYALTY - level;
		if (to_catalog >= HERO_MAX_LOYALTY) {
			to_catalog -= HERO_MAX_LOYALTY;
		}
	} else {
		to_catalog = to_catalog - (HERO_MAX_LOYALTY - level);
		if (to_catalog < 0) {
			to_catalog = HERO_MAX_LOYALTY + to_catalog; 
		}
	}
	float_catalog_ = ftofxp8(to_catalog);
}
Example #6
0
void controller_base::handle_event(const SDL_Event& event)
{
	if (gui::in_dialog()) {
		return;
	}

#if (defined(__APPLE__) && TARGET_OS_IPHONE) || defined(ANDROID)
	if ((event.type == SDL_MOUSEBUTTONDOWN) || (event.type == SDL_FINGERDOWN)) {
		wait_bh_event_ = true;
		return;
	}
	if (event.type == SDL_MOUSEMOTION) {
		return;
	}
	if (event.type == SDL_MOUSEBUTTONUP) {
		return;
	}
#endif
	SDL_Event new_event;
	int abs_dx, abs_dy;
	// events::mouse_handler& mouse_handler = get_mouse_handler_base();

	switch(event.type) {
	case SDL_KEYDOWN:
		// Detect key press events, unless there something that has keyboard focus
		// in which case the key press events should go only to it.
		if(have_keyboard_focus()) {
			process_keydown_event(event);
			hotkey::key_event(get_display(),event.key,this);
		} else {
			process_focus_keydown_event(event);
			break;
		}
		// intentionally fall-through
	case SDL_KEYUP:
		process_keyup_event(event);
		break;

	case SDL_FINGERMOTION:
		abs_dx = posix_abs(event.tfinger.dx);
		abs_dy = posix_abs(event.tfinger.dy);
		if (abs_dx <= FINGER_HIT_THRESHOLD && abs_dy <= FINGER_HIT_THRESHOLD) {
			break;
		}
		if (abs_dx >= FINGER_MOTION_THRESHOLD && abs_dy >= FINGER_MOTION_THRESHOLD) {
			if (event.tfinger.dx > 0) {
				if (event.tfinger.dy > 0) {
					finger_motion_direction_ = SOUTH_EAST;
				} else {
					finger_motion_direction_ = NORTH_EAST;
				}
			} else if (event.tfinger.dy >= 0) {
				finger_motion_direction_ = SOUTH_WEST;
			} else {
				finger_motion_direction_ = NORTH_WEST;
			}
			finger_motion_scroll_ = true;
		} else if (abs_dx >= abs_dy && abs_dx >= FINGER_MOTION_THRESHOLD) {
			// x axis
			if (event.tfinger.dx > 0) {
				finger_motion_direction_ = RIGHT;
			} else {
				finger_motion_direction_ = LEFT;
			}
			finger_motion_scroll_ = true;
		} else if (abs_dx < abs_dy && abs_dy >= FINGER_MOTION_THRESHOLD) {
			// y axis
			if (event.tfinger.dy > 0) {
				finger_motion_direction_ = DOWN;
			} else {
				finger_motion_direction_ = UP;
			}
			finger_motion_scroll_ = true;
		}
		wait_bh_event_ = false;
		break;

	case SDL_MOUSEMOTION:
		// Ignore old mouse motion events in the event queue
		if (SDL_PeepEvents(&new_event,1,SDL_GETEVENT, SDL_MOUSEMOTIONMASK) > 0) {
			while(SDL_PeepEvents(&new_event,1,SDL_GETEVENT, SDL_MOUSEMOTIONMASK) > 0) {};
			get_mouse_handler_base().mouse_motion_event(new_event.motion, browse_);
		} else {
			get_mouse_handler_base().mouse_motion_event(event.motion, browse_);
		}
		break;
	case SDL_FINGERUP:
		if (!wait_bh_event_) {
			break;
		}
		// simulate SDL_MOUSEBUTTONDOWN
		new_event = event;
		new_event.button.button = SDL_BUTTON_LEFT;
		new_event.button.state = SDL_PRESSED;
		new_event.button.x = event.tfinger.x;
		new_event.button.y = event.tfinger.y;
		// SDL_SendMouseMotion(NULL, 0, event.tfinger.x, event.tfinger.y);
		do {
			get_mouse_handler_base().mouse_press(new_event.button, browse_);
			post_mouse_press(new_event);
			if (get_mouse_handler_base().get_show_menu()){
				get_display().goto_main_context_menu();
			}
			if (new_event.button.state == SDL_RELEASED) {
				break;
			}
			new_event.button.state = SDL_RELEASED;
		} while (true);
		wait_bh_event_ = false;
		break;

	case SDL_MOUSEBUTTONDOWN:
	case SDL_MOUSEBUTTONUP:
		get_mouse_handler_base().mouse_press(event.button, browse_);
		post_mouse_press(event);
		if (get_mouse_handler_base().get_show_menu()){
			get_display().goto_main_context_menu();
		}
		break;
	case SDL_ACTIVEEVENT:
		if (event.active.type == SDL_APPMOUSEFOCUS && event.active.gain == 0) {
			if (get_mouse_handler_base().is_dragging()) {
				//simulate mouse button up when the app has lost mouse focus
				//this should be a general fix for the issue when the mouse
				//is dragged out of the game window and then the button is released
				int x, y;
				Uint8 mouse_flags = SDL_GetMouseState(&x, &y);
				if ((mouse_flags & SDL_BUTTON_LEFT) == 0) {
					SDL_Event e;
					e.type = SDL_MOUSEBUTTONUP;
					e.button.state = SDL_RELEASED;
					e.button.button = SDL_BUTTON_LEFT;
					e.button.x = x;
					e.button.y = y;
					get_mouse_handler_base().mouse_press(event.button, browse_);
					post_mouse_press(event);
				}
			}
		}
		break;
	default:
		break;
	}
}
void thero_selection::fill_table(tlistbox& list, int catalog)
{
	std::vector<int> features;
	std::stringstream str;
	std::string color;

	hero& leader = *(*teams_)[side_ - 1].leader();
	int hero_index = 0;
	for (std::vector<std::pair<int, unit*> >::iterator itor = pairs_.begin(); itor != pairs_.end(); ++ itor, hero_index ++) {
		/*** Add list item ***/
		string_map table_item;
		std::map<std::string, string_map> table_item_item;

		hero& h = heros_[itor->first];
		unit& u = *itor->second;

		int catalog_diff = posix_abs((int)leader.base_catalog_ - h.base_catalog_);
		if (catalog_diff > HERO_MAX_LOYALTY / 2) {
			catalog_diff = HERO_MAX_LOYALTY - catalog_diff;
		}

		if (catalog == OWNERSHIP_PAGE) {
			table_item["label"] = h.name();
			table_item_item.insert(std::make_pair("name", table_item));

			if (h.side_ != HEROS_INVALID_SIDE) {
				table_item["label"] =  (*teams_)[h.side_].name();
			} else {
				table_item["label"] = "---";
			}
			table_item_item.insert(std::make_pair("side", table_item));

			artifical* city = units_? units_->city_from_cityno(h.city_): NULL;
			if (city) {
				table_item["label"] = city->name();
			} else {
				table_item["label"] = "---";
			}
			table_item_item.insert(std::make_pair("city", table_item));

			str.str("");
			if (h.side_ != HEROS_INVALID_SIDE) {
				str << h.loyalty(leader);
			} else {
				str << "--";
			}
			table_item["label"] = str.str();
			table_item_item.insert(std::make_pair("loyalty", table_item));

			str.str("");
			str << (int)h.base_catalog_;
			if (HERO_MAX_LOYALTY - catalog_diff >= game_config::move_loyalty_threshold) {
				color = "green";
			} else if (HERO_MAX_LOYALTY - catalog_diff >= game_config::wander_loyalty_threshold) {
				color = "yellow";
			} else {
				color = "red";
			}
			str << "(" << tintegrate::generate_format(catalog_diff, color) << ")";
			table_item["label"] = str.str();
			table_item_item.insert(std::make_pair("hero_catalog", table_item));

			str.str("");
			if (!u.is_artifical()) {
				str << u.name() << _("name^Troop");
			} else if (u.is_city()) {
				str << u.name();
			} else {
				str << u.name() << u.type_name();
			}
			table_item["label"] = str.str();
			table_item_item.insert(std::make_pair("position", table_item));

			table_item["label"] = lexical_cast<std::string>(h.meritorious_);
			table_item_item.insert(std::make_pair("meritorious", table_item));

		} else if (catalog == ABILITY_PAGE) {
			table_item["label"] = h.name();
			table_item_item.insert(std::make_pair("name", table_item));

			table_item["label"] = lexical_cast<std::string>(fxptoi9(h.leadership_));
			table_item_item.insert(std::make_pair("leadership", table_item));

			table_item["label"] = lexical_cast<std::string>(fxptoi9(h.force_));
			table_item_item.insert(std::make_pair("force", table_item));

			table_item["label"] = lexical_cast<std::string>(fxptoi9(h.intellect_));
			table_item_item.insert(std::make_pair("intellect", table_item));

			table_item["label"] = lexical_cast<std::string>(fxptoi9(h.spirit_));
			table_item_item.insert(std::make_pair("spirit", table_item));

			table_item["label"] = lexical_cast<std::string>(fxptoi9(h.charm_));
			table_item_item.insert(std::make_pair("charm", table_item));

			table_item["label"] = hero::status_str(h.status_);
			table_item_item.insert(std::make_pair("action", table_item));

		} else if (catalog == FEATURE_PAGE) {
			table_item["label"] = h.name();
			table_item_item.insert(std::make_pair("name", table_item));

			table_item["label"] = hero::feature_str(h.feature_);
			table_item_item.insert(std::make_pair("feature", table_item));

			table_item["label"] = hero::feature_desc_str(h.feature_);
			table_item_item.insert(std::make_pair("explain", table_item));

		} else if (catalog == ADAPTABILITY_PAGE) {
			table_item["label"] = h.name();
			table_item_item.insert(std::make_pair("name", table_item));

			table_item["label"] = hero::adaptability_str2(h.arms_[0]);
			table_item_item.insert(std::make_pair("arm0", table_item));

			table_item["label"] = hero::adaptability_str2(h.arms_[1]);
			table_item_item.insert(std::make_pair("arm1", table_item));

			table_item["label"] = hero::adaptability_str2(h.arms_[2]);
			table_item_item.insert(std::make_pair("arm2", table_item));

			table_item["label"] = hero::adaptability_str2(h.arms_[3]);
			table_item_item.insert(std::make_pair("arm3", table_item));

			table_item["label"] = hero::adaptability_str2(h.arms_[4]);
			table_item_item.insert(std::make_pair("arm4", table_item));

		} else if (catalog == COMMAND_PAGE) {
			table_item["label"] = h.name();
			table_item_item.insert(std::make_pair("name", table_item));

			table_item["label"] = "----";
			table_item_item.insert(std::make_pair("command", table_item));

		} else if (catalog == PERSONAL_PAGE) {
			table_item["label"] = h.name();
			table_item_item.insert(std::make_pair("name", table_item));

			table_item["label"] = hero::gender_str(h.gender_);
			table_item_item.insert(std::make_pair("gender", table_item));

		} else if (catalog == RELATION_PAGE) {
			table_item["label"] = h.name();
			table_item_item.insert(std::make_pair("name", table_item));

			if (h.parent_[0].hero_ != HEROS_INVALID_NUMBER) {
				table_item["label"] = heros_[h.parent_[0].hero_].name();
			} else {
				table_item["label"] = "";
			}
			table_item_item.insert(std::make_pair("father", table_item));

			if (h.parent_[1].hero_ != HEROS_INVALID_NUMBER) {
				table_item["label"] = heros_[h.parent_[1].hero_].name();
			} else {
				table_item["label"] = "";
			}
			table_item_item.insert(std::make_pair("mother", table_item));

			str.str("");
			for (uint32_t i = 0; i < HEROS_MAX_OATH; i ++) {
				if (h.oath_[i].hero_ != HEROS_INVALID_NUMBER) {
					if (i == 0) {
						str << heros_[h.oath_[i].hero_].name();
					} else {
						str << " " << heros_[h.oath_[i].hero_].name();
					}
				}
			}
			table_item["label"] = str.str();
			table_item_item.insert(std::make_pair("oath", table_item));

			str.str("");
			for (uint32_t i = 0; i < HEROS_MAX_CONSORT; i ++) {
				if (h.consort_[i].hero_ != HEROS_INVALID_NUMBER) {
					if (i == 0) {
						str << heros_[h.consort_[i].hero_].name();
					} else {
						str << " " << heros_[h.consort_[i].hero_].name();
					}
				}
			}
			table_item["label"] = str.str();
			table_item_item.insert(std::make_pair("consort", table_item));

			str.str("");
			for (uint32_t i = 0; i < HEROS_MAX_INTIMATE; i ++) {
				if (h.intimate_[i].hero_ != HEROS_INVALID_NUMBER) {
					if (i == 0) {
						str << heros_[h.intimate_[i].hero_].name();
					} else {
						str << " " << heros_[h.intimate_[i].hero_].name();
					}
				}
			}
			table_item["label"] = str.str();
			table_item_item.insert(std::make_pair("intimate", table_item));

			str.str("");
			for (uint32_t i = 0; i < HEROS_MAX_HATE; i ++) {
				if (h.hate_[i].hero_ != HEROS_INVALID_NUMBER) {
					if (i == 0) {
						str << heros_[h.hate_[i].hero_].name();
					} else {
						str << " " << heros_[h.hate_[i].hero_].name();
					}
				}
			}
			table_item["label"] = str.str();
			table_item_item.insert(std::make_pair("hate", table_item));

		}
		list.add_row(table_item_item);

		twidget* grid_ptr = list.get_row_panel(hero_index);
		ttoggle_button* toggle = dynamic_cast<ttoggle_button*>(grid_ptr->find("prefix", true));
		toggle->set_callback_state_change(boost::bind(&thero_selection::hero_toggled, this, _1));
		toggle->set_data(hero_index);
		if (checked_pairs_.find(hero_index) != checked_pairs_.end()) {
			toggle->set_value(true);
		}

		bool enable = true;
		for (std::vector<std::string>::const_iterator it2 = disables_.begin(); it2 != disables_.end() && enable; ++ it2) {
			const std::string& tag = *it2;
			if (tag == "loyalty") {
				if (h.side_ != HEROS_INVALID_SIDE) {
					hero& ownership_leader = *(*teams_)[h.side_].leader();
					if (ownership_leader.base_catalog_ == h.base_catalog_) {
						enable = false;
					}
				}
			} else if (tag == "hate") {
				if (leader.is_hate(h)) {
					enable = false;
				}
			}
		}
		if (enable && h.player_ == HEROS_INVALID_NUMBER && std::find(disables_.begin(), disables_.end(), "member") != disables_.end()) {
			if (runtime_groups::exist_member(h, leader)) {
				toggle->set_active(false);
			}
		}
		if (!enable) {
			// grid_ptr->set_active(false);
			toggle->set_active(false);
		}
	}
}