/** * @brief Attempts to sell a commodity. * @param wid Window selling commodity from. * @param str Unused. */ static void commodity_sell( unsigned int wid, char* str ) { (void)str; char *comname; Commodity *com; unsigned int q; credits_t price; HookParam hparam[3]; /* Get parameters. */ q = commodity_getMod(); comname = toolkit_getList( wid, "lstGoods" ); com = commodity_get( comname ); price = planet_commodityPrice( land_planet, com ); /* Remove commodity. */ q = pilot_cargoRm( player.p, com, q ); price = price * (credits_t)q; player_modCredits( price ); land_checkAddRefuel(); commodity_update(wid, NULL); /* Run hooks. */ hparam[0].type = HOOK_PARAM_STRING; hparam[0].u.str = comname; hparam[1].type = HOOK_PARAM_NUMBER; hparam[1].u.num = q; hparam[2].type = HOOK_PARAM_SENTINEL; hooks_runParam( "comm_sell", hparam ); if (land_takeoff) takeoff(1); }
/** * @brief Modifies the player's standing without affecting others. * * Does not affect allies nor enemies and does not run through the Lua script. * * @param f Faction whose standing to modify. * @param mod Amount to modify standing by. * * @sa faction_modPlayer */ void faction_modPlayerRaw( int f, double mod ) { Faction *faction; HookParam hparam[3]; if (!faction_isFaction(f)) { WARN("%d is an invalid faction", f); return; } faction = &faction_stack[f]; faction->player += mod; /* Run hook if necessary. */ hparam[0].type = HOOK_PARAM_FACTION; hparam[0].u.lf.f = f; hparam[1].type = HOOK_PARAM_NUMBER; hparam[1].u.num = mod; hparam[2].type = HOOK_PARAM_SENTINEL; hooks_runParam( "standing", hparam ); /* Sanitize just in case. */ faction_sanitizePlayer( faction ); /* Tell space the faction changed. */ space_factionChange(); }
/** * @brief Buys the selected commodity. * @param wid Window buying from. * @param str Unused. */ static void commodity_buy( unsigned int wid, char* str ) { (void)str; char *comname; Commodity *com; unsigned int q; credits_t price; HookParam hparam[3]; /* Get selected. */ q = commodity_getMod(); comname = toolkit_getList( wid, "lstGoods" ); com = commodity_get( comname ); price = planet_commodityPrice( land_planet, com ); price *= q; /* Check stuff. */ if (!player_hasCredits( price )) { dialogue_alert( "Insufficient credits!" ); return; } else if (pilot_cargoFree(player.p) <= 0) { dialogue_alert( "Insufficient free space!" ); return; } /* Make the buy. */ q = pilot_cargoAdd( player.p, com, q ); player_modCredits( -price ); land_checkAddRefuel(); commodity_update(wid, NULL); /* Run hooks. */ hparam[0].type = HOOK_PARAM_STRING; hparam[0].u.str = comname; hparam[1].type = HOOK_PARAM_NUMBER; hparam[1].u.num = q; hparam[2].type = HOOK_PARAM_SENTINEL; hooks_runParam( "comm_buy", hparam ); if (land_takeoff) takeoff(1); }
/** * @brief Runs the input command. * * @param keynum The index of the keybind. * @param value The value of the keypress (defined above). * @param kabs The absolute value. */ static void input_key( int keynum, double value, double kabs, int repeat ) { unsigned int t; HookParam hparam[3]; /* Repetition stuff. */ if (conf.repeat_delay != 0) { if ((value == KEY_PRESS) && !repeat) { repeat_key = keynum; repeat_keyTimer = SDL_GetTicks(); repeat_keyCounter = 0; } else if (value == KEY_RELEASE) { repeat_key = -1; repeat_keyTimer = 0; repeat_keyCounter = 0; } } /* * movement */ /* accelerating */ if (KEY("accel") && !repeat) { if (kabs >= 0.) { player_restoreControl( PINPUT_MOVEMENT, NULL ); player_accel(kabs); input_accelButton = 1; } else { /* prevent it from getting stuck */ if (value==KEY_PRESS) { player_restoreControl( PINPUT_MOVEMENT, NULL ); player_setFlag(PLAYER_ACCEL); player_accel(1.); input_accelButton = 1; } else if (value==KEY_RELEASE) { player_accelOver(); player_rmFlag(PLAYER_ACCEL); input_accelButton = 0; } /* double tap accel = afterburn! */ t = SDL_GetTicks(); if ((conf.afterburn_sens != 0) && (value==KEY_PRESS) && INGAME() && NOHYP() && NODEAD() && (t-input_accelLast <= conf.afterburn_sens)) pilot_afterburn( player.p ); else if (value==KEY_RELEASE) pilot_afterburnOver( player.p ); if (value==KEY_PRESS) input_accelLast = t; } /* turning left */ } else if (KEY("left") && !repeat) { if (kabs >= 0.) { player_restoreControl( PINPUT_MOVEMENT, NULL ); player_setFlag(PLAYER_TURN_LEFT); player_left = kabs; } else { /* set flags for facing correction */ if (value==KEY_PRESS) { player_restoreControl( PINPUT_MOVEMENT, NULL ); player_setFlag(PLAYER_TURN_LEFT); player_left = 1.; } else if (value==KEY_RELEASE) { player_rmFlag(PLAYER_TURN_LEFT); player_left = 0.; } } /* turning right */ } else if (KEY("right") && !repeat) { if (kabs >= 0.) { player_restoreControl( PINPUT_MOVEMENT, NULL ); player_setFlag(PLAYER_TURN_RIGHT); player_right = kabs; } else { /* set flags for facing correction */ if (value==KEY_PRESS) { player_restoreControl( PINPUT_MOVEMENT, NULL ); player_setFlag(PLAYER_TURN_RIGHT); player_right = 1.; } else if (value==KEY_RELEASE) { player_rmFlag(PLAYER_TURN_RIGHT); player_right = 0.; } } /* turn around to face vel */ } else if (KEY("reverse") && !repeat) { if (value==KEY_PRESS) { player_restoreControl( PINPUT_MOVEMENT, NULL ); player_setFlag(PLAYER_REVERSE); } else if ((value==KEY_RELEASE) && player_isFlag(PLAYER_REVERSE)) { player_rmFlag(PLAYER_REVERSE); if (!player_isFlag(PLAYER_ACCEL)) player_accelOver(); } /* * combat */ /* shooting primary weapon */ } else if (KEY("primary") && NODEAD() && !repeat) { if (value==KEY_PRESS) { player_setFlag(PLAYER_PRIMARY); } else if (value==KEY_RELEASE) player_rmFlag(PLAYER_PRIMARY); /* targeting */ } else if (INGAME() && NODEAD() && KEY("target_next")) { if (value==KEY_PRESS) player_targetNext(0); } else if (INGAME() && NODEAD() && KEY("target_prev")) { if (value==KEY_PRESS) player_targetPrev(0); } else if (INGAME() && NODEAD() && KEY("target_nearest")) { if (value==KEY_PRESS) player_targetNearest(); } else if (INGAME() && NODEAD() && KEY("target_nextHostile")) { if (value==KEY_PRESS) player_targetNext(1); } else if (INGAME() && NODEAD() && KEY("target_prevHostile")) { if (value==KEY_PRESS) player_targetPrev(1); } else if (INGAME() && NODEAD() && KEY("target_hostile")) { if (value==KEY_PRESS) player_targetHostile(); } else if (INGAME() && NODEAD() && KEY("target_clear")) { if (value==KEY_PRESS) player_targetClear(); /* face the target */ } else if (INGAME() && NODEAD() && KEY("face") && !repeat) { if (value==KEY_PRESS) { player_restoreControl( PINPUT_MOVEMENT, NULL ); player_setFlag(PLAYER_FACE); } else if ((value==KEY_RELEASE) && player_isFlag(PLAYER_FACE)) player_rmFlag(PLAYER_FACE); /* board them ships */ } else if (KEY("board") && INGAME() && NOHYP() && NODEAD() && !repeat) { if (value==KEY_PRESS) { player_restoreControl( 0, NULL ); player_board(); } /* * Escorts. */ } else if (INGAME() && NODEAD() && KEY("e_targetNext") && !repeat) { if (value==KEY_PRESS) player_targetEscort(0); } else if (INGAME() && NODEAD() && KEY("e_targetPrev") && !repeat) { if (value==KEY_PRESS) player_targetEscort(1); } else if (INGAME() && NODEAD() && KEY("e_attack") && !repeat) { if (value==KEY_PRESS) escorts_attack(player.p); } else if (INGAME() && NODEAD() && KEY("e_hold") && !repeat) { if (value==KEY_PRESS) escorts_hold(player.p); } else if (INGAME() && NODEAD() && KEY("e_return") && !repeat) { if (value==KEY_PRESS) escorts_return(player.p); } else if (INGAME() && NODEAD() && KEY("e_clear") && !repeat) { if (value==KEY_PRESS) escorts_clear(player.p); /* * secondary weapons */ /* shooting secondary weapon */ } else if (KEY("secondary") && NOHYP() && NODEAD() && !repeat) { if (value==KEY_PRESS) { player_setFlag(PLAYER_SECONDARY); } else if (value==KEY_RELEASE) player_rmFlag(PLAYER_SECONDARY); /* Weapon sets. */ } else if (KEY("weapset1")) { player_weapSetPress( 0, value, repeat ); } else if (KEY("weapset2")) { player_weapSetPress( 1, value, repeat ); } else if (KEY("weapset3")) { player_weapSetPress( 2, value, repeat ); } else if (KEY("weapset4")) { player_weapSetPress( 3, value, repeat ); } else if (KEY("weapset5")) { player_weapSetPress( 4, value, repeat ); } else if (KEY("weapset6")) { player_weapSetPress( 5, value, repeat ); } else if (KEY("weapset7")) { player_weapSetPress( 6, value, repeat ); } else if (KEY("weapset8")) { player_weapSetPress( 7, value, repeat ); } else if (KEY("weapset9")) { player_weapSetPress( 8, value, repeat ); } else if (KEY("weapset0")) { player_weapSetPress( 9, value, repeat ); /* * space */ } else if (KEY("autonav") && INGAME() && NOHYP() && NODEAD()) { if (value==KEY_PRESS) player_autonavStart(); /* target planet (cycles like target) */ } else if (KEY("target_planet") && INGAME() && NOHYP() && NOLAND() && NODEAD()) { if (value==KEY_PRESS) player_targetPlanet(); /* target nearest planet or attempt to land */ } else if (KEY("land") && INGAME() && NOHYP() && NOLAND() && NODEAD()) { if (value==KEY_PRESS) player_land(); } else if (KEY("thyperspace") && NOHYP() && NOLAND() && NODEAD()) { if (value==KEY_PRESS) player_targetHyperspace(); } else if (KEY("starmap") && NOHYP() && NODEAD() && !repeat) { if (value==KEY_PRESS) map_open(); } else if (KEY("jump") && INGAME() && !repeat) { if (value==KEY_PRESS) { player_restoreControl( 0, NULL ); player_jump(); } } else if (KEY("overlay") && NODEAD() && INGAME() && !repeat) { ovr_key( value ); } else if (KEY("mousefly") && NODEAD() && !repeat) { if (value==KEY_PRESS) player_toggleMouseFly(); } else if (KEY("autobrake") && NOHYP() && NOLAND() && NODEAD() && !repeat) { if (value==KEY_PRESS) { player_restoreControl( PINPUT_BRAKING, NULL ); player_brake(); } /* * Communication. */ } else if (KEY("log_up") && INGAME() && NODEAD()) { if (value==KEY_PRESS) { gui_messageScrollUp(5); } } else if (KEY("log_down") && INGAME() && NODEAD()) { if (value==KEY_PRESS) { gui_messageScrollDown(5); } } else if (KEY("hail") && INGAME() && NOHYP() && NODEAD() && !repeat) { if (value==KEY_PRESS) { player_hail(); } } else if (KEY("autohail") && INGAME() && NOHYP() && NODEAD() && !repeat) { if (value==KEY_PRESS) { player_autohail(); } /* * misc */ /* zooming in */ } else if (KEY("mapzoomin") && INGAME() && NODEAD()) { if (value==KEY_PRESS) gui_setRadarRel(-1); /* zooming out */ } else if (KEY("mapzoomout") && INGAME() && NODEAD()) { if (value==KEY_PRESS) gui_setRadarRel(1); /* take a screenshot */ } else if (KEY("screenshot")) { if (value==KEY_PRESS) player_screenshot(); #if SDL_VERSION_ATLEAST(2,0,0) /* toggle fullscreen */ } else if (KEY("togglefullscreen") && !repeat) { if (value==KEY_PRESS) naev_toggleFullscreen(); #endif /* SDL_VERSION_ATLEAST(2,0,0) */ /* pause the games */ } else if (KEY("pause") && !repeat) { if (value==KEY_PRESS) { if (!toolkit_isOpen()) { if (paused) unpause_game(); else pause_player(); } } /* toggle speed mode */ } else if (KEY("speed") && !repeat) { if ((value==KEY_PRESS) && (!player_isFlag( PLAYER_CINEMATICS_2X ))) { if (player_isFlag(PLAYER_DOUBLESPEED)) { if (!player_isFlag(PLAYER_AUTONAV)) pause_setSpeed(1.); player_rmFlag(PLAYER_DOUBLESPEED); } else { if (!player_isFlag(PLAYER_AUTONAV)) pause_setSpeed(2.); player_setFlag(PLAYER_DOUBLESPEED); } } /* opens a small menu */ } else if (KEY("menu") && NODEAD() && !repeat) { if (value==KEY_PRESS) menu_small(); /* shows pilot information */ } else if (KEY("info") && NOHYP() && NODEAD() && !repeat) { if (value==KEY_PRESS) menu_info( INFO_MAIN ); /* Opens the Lua console. */ } else if (KEY("console") && NODEAD() && !repeat) { if (value==KEY_PRESS) cli_open(); } /* Key press not used. */ else { return; } /* Run the hook. */ hparam[0].type = HOOK_PARAM_STRING; hparam[0].u.str = input_keybinds[keynum].name; hparam[1].type = HOOK_PARAM_BOOL; hparam[1].u.b = (value > 0.); hparam[2].type = HOOK_PARAM_SENTINEL; hooks_runParam( "input", hparam ); }
/** * @brief Handles a click event. */ static void input_clickevent( SDL_Event* event ) { unsigned int pid; int mx, my, mxr, myr, pntid, jpid; int rx, ry, rh, rw, res; int autonav; double x, y, zoom, px, py; double ang, angp, mouseang; HookParam hparam[2]; /* Generate hook. */ hparam[0].type = HOOK_PARAM_NUMBER; hparam[0].u.num = event->button.button; hparam[1].type = HOOK_PARAM_SENTINEL; hooks_runParam( "mouse", hparam ); #if !SDL_VERSION_ATLEAST(2,0,0) /* Handle zoom. */ if (event->button.button == SDL_BUTTON_WHEELUP) { input_clickZoom( 1.1 ); return; } else if (event->button.button == SDL_BUTTON_WHEELDOWN) { input_clickZoom( 0.9 ); return; } #endif /* !SDL_VERSION_ATLEAST(2,0,0) */ /* Player must not be NULL. */ if ((player.p == NULL) || player_isFlag(PLAYER_DESTROYED)) return; /* Player must not be dead. */ if (pilot_isFlag(player.p, PILOT_DEAD)) return; /* Middle mouse enables mouse flying. */ if (event->button.button == SDL_BUTTON_MIDDLE) { player_toggleMouseFly(); return; } /* Mouse targeting only uses left and right buttons. */ if (event->button.button != SDL_BUTTON_LEFT && event->button.button != SDL_BUTTON_RIGHT) return; autonav = (event->button.button == SDL_BUTTON_RIGHT) ? 1 : 0; px = player.p->solid->pos.x; py = player.p->solid->pos.y; gl_windowToScreenPos( &mx, &my, event->button.x, event->button.y ); if ((mx <= 15 || my <= 15 ) || (my >= gl_screen.h - 15 || mx >= gl_screen.w - 15)) { /* Border targeting is handled as a special case, as it uses angles, * not coordinates. */ x = (mx - (gl_screen.w / 2.)) + px; y = (my - (gl_screen.h / 2.)) + py; mouseang = atan2(py - y, px - x); angp = pilot_getNearestAng( player.p, &pid, mouseang, 1 ); ang = system_getClosestAng( cur_system, &pntid, &jpid, x, y, mouseang ); if ((ABS(angle_diff(mouseang, angp)) > M_PI / 64) || ABS(angle_diff(mouseang, ang)) < ABS(angle_diff(mouseang, angp))) pid = PLAYER_ID; /* Pilot angle is too great, or planet/jump is closer. */ if (ABS(angle_diff(mouseang, ang)) > M_PI / 64 ) jpid = pntid = -1; /* Asset angle difference is too great. */ if (!autonav && pid != PLAYER_ID) { if (input_clickedPilot(pid)) return; } else if (pntid >= 0) { /* Planet is closest. */ if (input_clickedPlanet(pntid, autonav)) return; } else if (jpid >= 0) { /* Jump point is closest. */ if (input_clickedJump(jpid, autonav)) return; } /* Fall-through and handle as a normal click. */ } /* Radar targeting requires raw coordinates. */ mxr = event->button.x; myr = gl_screen.rh - event->button.y; gui_radarGetPos( &rx, &ry ); gui_radarGetDim( &rw, &rh ); if ((mxr > rx && mxr <= rx + rw ) && (myr > ry && myr <= ry + rh )) { /* Radar */ zoom = 1.; gui_radarGetRes( &res ); x = (mxr - (rx + rw / 2.)) * res + px; y = (myr - (ry + rh / 2.)) * res + py; if (input_clickPos( event, x, y, zoom, 10. * res, 15. * res )) return; } /* Visual (on-screen) */ gl_screenToGameCoords( &x, &y, (double)mx, (double)my ); zoom = res = 1. / cam_getZoom(); input_clickPos( event, x, y, zoom, 10. * res, 15. * res ); return; }
/** * @brief Handles a click event. */ static void input_clickevent( SDL_Event* event ) { unsigned int pid; Pilot *p; int mx, my, mxr, myr, pntid, jpid; int rx, ry, rh, rw, res; double x, y, m, r, rp, d, dp, px, py; double ang, angp, mouseang; Planet *pnt; JumpPoint *jp; HookParam hparam[2]; /* Generate hook. */ hparam[0].type = HOOK_PARAM_NUMBER; hparam[0].u.num = event->button.button; hparam[1].type = HOOK_PARAM_SENTINAL; hooks_runParam( "mouse", hparam ); /* Handle zoom. */ if (event->button.button == SDL_BUTTON_WHEELUP) { input_clickZoom( 1.1 ); return; } else if (event->button.button == SDL_BUTTON_WHEELDOWN) { input_clickZoom( 0.9 ); return; } /* Middle mouse enables mouse flying. */ if (event->button.button == SDL_BUTTON_MIDDLE) { player_toggleMouseFly(); return; } /* Mouse targetting is left only. */ if (event->button.button != SDL_BUTTON_LEFT) return; /* Player must not be NULL. */ if (player_isFlag(PLAYER_DESTROYED) || (player.p == NULL)) return; px = player.p->solid->pos.x; py = player.p->solid->pos.y; gl_windowToScreenPos( &mx, &my, event->button.x, event->button.y ); gl_screenToGameCoords( &x, &y, (double)mx, (double)my ); if ((mx <= 15 || my <= 15 ) || (my >= gl_screen.h - 15 || mx >= gl_screen.w - 15)) { /* Border */ x = (mx - (gl_screen.w / 2.)) + px; y = (my - (gl_screen.h / 2.)) + py; mouseang = atan2(py - y, px - x); angp = pilot_getNearestAng( player.p, &pid, mouseang, 1 ); ang = system_getClosestAng( cur_system, &pntid, &jpid, x, y, mouseang ); if ((ABS(angle_diff(mouseang, angp)) > M_PI / 64) || ABS(angle_diff(mouseang, ang)) < ABS(angle_diff(mouseang, angp))) pid = PLAYER_ID; /* Pilot angle is too great, or planet/jump is closer. */ if (ABS(angle_diff(mouseang, ang)) > M_PI / 64 ) jpid = pntid = -1; /* Asset angle difference is too great. */ } else { /* Radar targeting requires raw coordinates. */ mxr = event->button.x; myr = gl_screen.rh - event->button.y; gui_radarGetPos( &rx, &ry ); gui_radarGetDim( &rw, &rh ); if ((mxr > rx && mxr <= rx + rw ) && (myr > ry && myr <= ry + rh )) { /* Radar */ m = 1; gui_radarGetRes( &res ); x = (mxr - (rx + rw / 2.)) * res + px; y = (myr - (ry + rh / 2.)) * res + py; } else /* Visual (on-screen) */ m = res = 1. / cam_getZoom(); dp = pilot_getNearestPos( player.p, &pid, x, y, 1 ); d = system_getClosest( cur_system, &pntid, &jpid, x, y ); rp = MAX( 1.5 * PILOT_SIZE_APROX * pilot_get(pid)->ship->gfx_space->sw / 2 * m, 10. * res); if (pntid >=0) { /* Planet is closer. */ pnt = cur_system->planets[ pntid ]; r = MAX( 1.5 * pnt->radius, 100. ); } else if (jpid >= 0) { jp = &cur_system->jumps[ jpid ]; r = MAX( 1.5 * jp->radius, 100. ); } else { r = 0.; } /* Reject pilot if it's too far or a valid asset is closer. */ if (dp > pow2(rp) || (d < pow2(r) && dp < pow2(rp) && dp > d)) pid = PLAYER_ID; if (d > pow2(r)) /* Planet or jump point is too far. */ jpid = pntid = -1; } if (pid != PLAYER_ID) { /* Apply an action if already selected. */ if (!pilot_isFlag(player.p, PILOT_DEAD) && (pid == player.p->target)) { p = pilot_get(pid); if (pilot_isDisabled(p) || pilot_isFlag(p, PILOT_BOARDABLE)) player_board(); else player_hail(); } else player_targetSet( pid ); } else if (pntid >= 0) { /* Planet is closest. */ if (pntid == player.p->nav_planet) { pnt = cur_system->planets[ pntid ]; if (planet_hasService(pnt, PLANET_SERVICE_LAND) && (!areEnemies( player.p->faction, pnt->faction ) || pnt->bribed )) player_land(); else player_hailPlanet(); } else player_targetPlanetSet( pntid ); } else if (jpid >= 0) { /* Jump point is closest. */ jp = &cur_system->jumps[ jpid ]; if (jpid == player.p->nav_hyperspace) { if (space_canHyperspace(player.p)) { if (!paused) player_autonavAbort(NULL); player_jump(); } else player_autonavStart(); } else player_targetHyperspaceSet( jpid ); } }
/** * @brief Mods player using the power of Lua. */ static void faction_modPlayerLua( int f, double mod, const char *source, int secondary ) { Faction *faction; lua_State *L; int errf; double old, delta; HookParam hparam[3]; faction = &faction_stack[f]; /* Make sure it's not static. */ if (faction_isFlag(faction, FACTION_STATIC)) return; L = faction->state; old = faction->player; if (L == NULL) faction->player += mod; else { #if DEBUGGING lua_pushcfunction(L, nlua_errTrace); errf = -6; #else /* DEBUGGING */ errf = 0; #endif /* DEBUGGING */ /* Set up the function: * faction_hit( current, amount, source, secondary ) */ lua_getglobal( L, "faction_hit" ); lua_pushnumber( L, faction->player ); lua_pushnumber( L, mod ); lua_pushstring( L, source ); lua_pushboolean( L, secondary ); /* Call function. */ if (lua_pcall( L, 4, 1, errf )) { /* An error occurred. */ WARN("Faction '%s': %s", faction->name, lua_tostring(L,-1)); #if DEBUGGING lua_pop( L, 2 ); #else /* DEBUGGING */ lua_pop( L, 1 ); #endif /* DEBUGGING */ return; } /* Parse return. */ if (!lua_isnumber( L, -1 )) WARN( "Lua script for faction '%s' did not return a number from 'faction_hit(...)'.", faction->name ); else faction->player = lua_tonumber( L, -1 ); #if DEBUGGING lua_pop( L, 2 ); #else /* DEBUGGING */ lua_pop( L, 1 ); #endif /* DEBUGGING */ } /* Sanitize just in case. */ faction_sanitizePlayer( faction ); /* Run hook if necessary. */ delta = faction->player - old; if (fabs(delta) > 1e-10) { hparam[0].type = HOOK_PARAM_FACTION; hparam[0].u.lf.f = f; hparam[1].type = HOOK_PARAM_NUMBER; hparam[1].u.num = delta; hparam[2].type = HOOK_PARAM_SENTINEL; hooks_runParam( "standing", hparam ); /* Tell space the faction changed. */ space_factionChange(); } }