+ /* reset moving state, so the game is not influenced before we want to */
+ mercenary_vr_move(0, NULL, NULL, 256, 256);
+ joystick_set_x = 0;
+ joystick_set_y = 0;
+ if (!mercenary_get_info_walking()) {
+ /* turn right and left when not walking */
+ if (stick_right_x > STICK_ROTATE_THRESHOLD) {
+ set_joystick(0, 1, -1, -1, -1);
+ joystick_set_x = 1;
+ } else
+ if (stick_right_x < -STICK_ROTATE_THRESHOLD) {
+ set_joystick(1, 0, -1, -1, -1);
+ joystick_set_x = 1;
+ }
+ /* pitch craft */
+ if (stick_right_y > STICK_WALK_THRESHOLD) {
+ set_joystick(-1, -1, 1, 0, -1);
+ joystick_set_y = 1;
+ } else
+ if (stick_right_y < -STICK_WALK_THRESHOLD) {
+ set_joystick(-1, -1, 0, 1, -1);
+ joystick_set_y = 1;
+ }
+ if (joystick_set_x || joystick_set_y) {
+ mercenary_vr_move(0, NULL, NULL,
+ 256,
+ (int)(256.0 * fabs(stick_right_y) - STICK_WALK_THRESHOLD / (1.0 - STICK_WALK_THRESHOLD) + 0.5));
+ }
+ } else {
+ double roll, pitch, yaw;
+ double tilt, dir, east, north;
+ int32_t move_east[4], move_north[4];
+
+ tilt = sqrt(stick_right_x * stick_right_x + stick_right_y * stick_right_y);
+
+ /* check if we rotate, walk or rest */
+ if (we_rotate == 0 && we_walk == 0) {
+ /* we rest */
+ if (fabs(stick_right_y) > fabs(stick_right_x) && tilt > STICK_WALK_THRESHOLD)
+ we_walk = 1;
+ if (fabs(stick_right_x) > fabs(stick_right_y) && tilt > STICK_ROTATE_THRESHOLD)
+ we_rotate = 1;
+ } else if (we_walk) {
+ /* we walk */
+ if (tilt < STICK_WALK_THRESHOLD) {
+ we_walk = 0;
+ /* we need to stop right here and not continue until next rendering */
+ set_joystick(-1, -1, 0, 0, -1); /* stop y */
+ reset_joystick(); /* commit stop */
+ }
+ } else {
+ /* we rotate */
+ if (tilt < STICK_ROTATE_THRESHOLD)
+ we_rotate = 0;
+ }
+
+ /* if we walk */
+ if (we_walk) {
+ /* get stick amplitude (dist) and direction (dir) */
+ if (tilt > 1.0)
+ tilt = 1.0;
+ tilt = (tilt - STICK_WALK_THRESHOLD) / (1.0 - STICK_WALK_THRESHOLD) * 40.0;
+ dir = atan2(stick_right_x, stick_right_y);
+#if 0
+ /* use hand direction to get actual stick direction */
+ dir = fmod(dir - hand_right_yaw, M_PI * 2.0);
+ if (dir < -M_PI)
+ dir += M_PI * 2.0;
+ if (dir > M_PI)
+ dir -= M_PI * 2.0;
+#endif
+ /* flip direction, if we walk backwards */
+ if (dir > M_PI / 2.0 || dir < -M_PI / 2.0) {
+ /* go backwards */
+ set_joystick(-1, -1, 0, 1, -1);
+ joystick_set_y = 1;
+ } else {
+ /* go forward */
+ set_joystick(-1, -1, 1, 0, -1);
+ joystick_set_y = 1;
+ }
+ mercenary_get_orientation(&roll, &pitch, &yaw);
+ east = sin(yaw - dir + M_PI) * tilt;
+ north = -cos(yaw - dir + M_PI) * tilt;
+ /* calculatate the integer positions of 4 steps */
+ move_east[0] = (int32_t)((east * 0.25) + 0.5);
+ move_north[0] = (int32_t)((north * 0.25) + 0.5);
+ move_east[1] = (int32_t)((east * 0.5) + 0.5);
+ move_north[1] = (int32_t)((north * 0.5) + 0.5);
+ move_east[2] = (int32_t)((east * 0.75) + 0.5);
+ move_north[2] = (int32_t)((north * 0.75) + 0.5);
+ move_east[3] = (int32_t)(east + 0.5);
+ move_north[3] = (int32_t)(north + 0.5);
+ /* calculate the delta between each of the 4 step */
+ move_east[3] -= move_east[2];
+ move_north[3] -= move_north[2];
+ move_east[2] -= move_east[1];
+ move_north[2] -= move_north[1];
+ move_east[1] -= move_east[0];
+ move_north[1] -= move_north[0];
+ /* the game takes 4 steps to move the player */
+ mercenary_vr_move(1, move_east, move_north, 256, 256);
+ }
+
+ /* snap orientation to steps of 45 degrees */
+ if (we_rotate == 1) {
+ mercenary_get_orientation(&roll, &pitch, &yaw);
+ yaw = round(yaw / degrees45) * degrees45;
+ /* if we rotate: change orientation */
+ if (stick_right_x > 0)
+ yaw -= degrees45;
+ else
+ yaw += degrees45;
+ /* rotate only once per stick movement */
+ we_rotate = 2;
+ mercenary_set_orientation(yaw);
+ }
+ }
+ if (joystick_set_x_last && !joystick_set_x)