/* * pHatt - PalmOS(TM) Hattrick (http://www.hattrick.org) Manager Assistant * * position.c - list player's strength on various positions * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #include "phat.h" #define NUMPOS 18 static Char **positionValues; static Int8 calcExpLog(Int8 e) { switch ( e ) { case 0: return 0; case 1: return 30; case 2: return 48; case 3: return 60; case 4: return 70; case 5: return 78; case 6: return 85; case 7: return 90; case 8: return 95; case 9: return 100; case 10: return 104; case 11: return 108; case 12: return 111; case 13: return 115; case 14: return 118; case 15: return 120; case 16: return 123; case 17: return 126; case 18: return 128; case 19: return 130; } return 0; } static struct positions calcPositions(struct player *p) { Int8 i; Int16 wsum; Int16 round; Int32 calc; struct positions o; for ( i = 0; i < 20 ; i++) { calc = prefs.w[i].stamina * ( p->stamina + 1); calc += prefs.w[i].goalie * ( p->goalie + 1); calc += prefs.w[i].playmaking * ( p->playmaking + 1); calc += prefs.w[i].passing * ( p->passing + 1); calc += prefs.w[i].winging * ( p->winging + 1); calc += prefs.w[i].defending * ( p->defending + 1); calc += prefs.w[i].scoring * ( p->scoring + 1); calc += prefs.w[i].setting * ( p->setting + 1); calc += prefs.w[i].leading * ( p->leading + 1); wsum = prefs.w[i].stamina + prefs.w[i].goalie + prefs.w[i].playmaking + prefs.w[i].passing + prefs.w[i].winging + prefs.w[i].defending + prefs.w[i].scoring + prefs.w[i].setting + prefs.w[i].leading; /* TODO: We've got a numeric problem here */ calc *= 100; calc /= ( wsum ? wsum : 1 ); /* calc *= ( 100 + ( prefs.w[i].form * (p->form + 1 )) / 8 - prefs.w[i].form ); */ round = (prefs.w[i].form * ( p->form + 1) * 10 + 4) / 8; calc *= ( 1000 + round - prefs.w[i].form * 10 ); calc /= 1000; calc += calcExpLog(p->experience); /* Rounding */ calc += 5; o.v[i] = calc; } return o; } static void showStar(Int16 height) { WinDrawBitmap(starBitmap, 149, height + 2); } static void fillBest(Boolean *best, const struct positions *p) { Int8 i; Int16 bestvalue = 0; for ( i = 0; i < NUMPOS; i++ ) if ( p->v[i] > bestvalue ) bestvalue = p->v[i]; for ( i = 0; i < NUMPOS; i++ ) best[i] = ( p->v[i] == bestvalue ); } static void gotoBestPositionPage(const struct positions *p) { Boolean best[NUMPOS]; fillBest(best, p); if (best[posID(Pos_Keeper)] || best[posID(Pos_For)] || best[posID(Pos_ForDef)] ) prefs.selectedPositionPage = PositionScreen3; else if (best[posID(Pos_Def)] || best[posID(Pos_DefWing)] || best[posID(Pos_DefOff)] || best[posID(Pos_WBack)] || best[posID(Pos_WBackOff)] || best[posID(Pos_WBackDef)] || best[posID(Pos_WBackMid)] ) prefs.selectedPositionPage = PositionScreen; else prefs.selectedPositionPage = PositionScreen2; } static void showBestPositions(Int8 page, const struct positions *p) { Boolean best[NUMPOS]; RectangleType cArea; cArea.topLeft.x = 149; cArea.topLeft.y = 35; cArea.extent.x = 13; cArea.extent.y = 100; WinEraseRectangle(&cArea, 0); fillBest(best, p); switch ( page ) { case 0: if (best[posID(Pos_Def)]) { showStar(HeightDef); FldSetFont(getOPtr(Pos_Def), 1); } else FldSetFont(getOPtr(Pos_Def), 0); if (best[posID(Pos_DefWing)]) { showStar(HeightDefWing); FldSetFont(getOPtr(Pos_DefWing), 1); } else FldSetFont(getOPtr(Pos_DefWing), 0); if (best[posID(Pos_DefOff)]) { showStar(HeightDefOff); FldSetFont(getOPtr(Pos_DefOff), 1); } else FldSetFont(getOPtr(Pos_DefOff), 0); if (best[posID(Pos_WBack)]) { showStar(HeightWBack); FldSetFont(getOPtr(Pos_WBack), 1); } else FldSetFont(getOPtr(Pos_WBack), 0); if (best[posID(Pos_WBackOff)]) { showStar(HeightWBackOff); FldSetFont(getOPtr(Pos_WBackOff), 1); } else FldSetFont(getOPtr(Pos_WBackOff), 0); if (best[posID(Pos_WBackDef)]) { showStar(HeightWBackDef); FldSetFont(getOPtr(Pos_WBackDef), 1); } else FldSetFont(getOPtr(Pos_WBackDef), 0); if (best[posID(Pos_WBackMid)]) { showStar(HeightWBackMid); FldSetFont(getOPtr(Pos_WBackMid), 1); } else FldSetFont(getOPtr(Pos_WBackMid), 0); break; case 1: if (best[posID(Pos_Mid)]) { showStar(HeightMid); FldSetFont(getOPtr(Pos_Mid), 1); } else FldSetFont(getOPtr(Pos_Mid), 0); if (best[posID(Pos_MidWing)]) { showStar(HeightMidWing); FldSetFont(getOPtr(Pos_MidWing), 1); } else FldSetFont(getOPtr(Pos_MidWing), 0); if (best[posID(Pos_MidOff)]) { showStar(HeightMidOff); FldSetFont(getOPtr(Pos_MidOff), 1); } else FldSetFont(getOPtr(Pos_MidOff), 0); if (best[posID(Pos_MidDef)]) { showStar(HeightMidDef); FldSetFont(getOPtr(Pos_MidDef), 1); } else FldSetFont(getOPtr(Pos_MidDef), 0); if (best[posID(Pos_Wing)]) { showStar(HeightWing); FldSetFont(getOPtr(Pos_Wing), 1); } else FldSetFont(getOPtr(Pos_Wing), 0); if (best[posID(Pos_WingMid)]) { showStar(HeightWingMid); FldSetFont(getOPtr(Pos_WingMid), 1); } else FldSetFont(getOPtr(Pos_WingMid), 0); if (best[posID(Pos_WingOff)]) { showStar(HeightWingOff); FldSetFont(getOPtr(Pos_WingOff), 1); } else FldSetFont(getOPtr(Pos_WingOff), 0); if (best[posID(Pos_WingDef)]) { showStar(HeightWingDef); FldSetFont(getOPtr(Pos_WingDef), 1); } else FldSetFont(getOPtr(Pos_WingDef), 0); break; case 2: if (best[posID(Pos_Keeper)]) { showStar(HeightKeeper); FldSetFont(getOPtr(Pos_Keeper), 1); } else FldSetFont(getOPtr(Pos_Keeper), 0); if (best[posID(Pos_For)]) { showStar(HeightFor); FldSetFont(getOPtr(Pos_For), 1); } else FldSetFont(getOPtr(Pos_For), 0); if (best[posID(Pos_ForDef)]) { showStar(HeightForDef); FldSetFont(getOPtr(Pos_ForDef), 1); } else FldSetFont(getOPtr(Pos_ForDef), 0); break; default: break; } } static void showPositions(Int8 page, struct positions p) { Int8 i; if ( positionValues ) { for ( i = 0 ; i < 8; i++ ) MemPtrFree(positionValues[i]); MemPtrFree(positionValues); } /* We always take 8, it's easier to free like that */ positionValues = MemPtrNew(8 * sizeof(Char *)); for ( i = 0; i < 8 ; i++ ) positionValues[i] = MemPtrNew(maxStrIToALen); switch ( page ) { case 0: fillValue(positionValues[0], Pos_Def, p.v[posID(Pos_Def)]); fillValue(positionValues[1], Pos_DefWing, p.v[posID(Pos_DefWing)]); fillValue(positionValues[2], Pos_DefOff, p.v[posID(Pos_DefOff)]); fillValue(positionValues[3], Pos_WBack, p.v[posID(Pos_WBack)]); fillValue(positionValues[4], Pos_WBackMid, p.v[posID(Pos_WBackMid)]); fillValue(positionValues[5], Pos_WBackOff, p.v[posID(Pos_WBackOff)]); fillValue(positionValues[6], Pos_WBackDef, p.v[posID(Pos_WBackDef)]); break; case 1: fillValue(positionValues[0], Pos_Mid, p.v[posID(Pos_Mid)]); fillValue(positionValues[1], Pos_MidWing, p.v[posID(Pos_MidWing)]); fillValue(positionValues[2], Pos_MidOff, p.v[posID(Pos_MidOff)]); fillValue(positionValues[3], Pos_MidDef, p.v[posID(Pos_MidDef)]); fillValue(positionValues[4], Pos_Wing, p.v[posID(Pos_Wing)]); fillValue(positionValues[5], Pos_WingMid, p.v[posID(Pos_WingMid)]); fillValue(positionValues[6], Pos_WingOff, p.v[posID(Pos_WingOff)]); fillValue(positionValues[7], Pos_WingDef, p.v[posID(Pos_WingDef)]); break; case 2: fillValue(positionValues[0], Pos_Keeper, p.v[posID(Pos_Keeper)]); fillValue(positionValues[1], Pos_For, p.v[posID(Pos_For)]); fillValue(positionValues[2], Pos_ForDef, p.v[posID(Pos_ForDef)]); fillValue(positionValues[3], Pos_Captain, p.v[posID(Pos_Captain)]); fillValue(positionValues[4], Pos_Setter, p.v[posID(Pos_Setter)]); break; default: break; } } void setPositionWeight(UInt16 pos, struct positionWeight w) { prefs.w[posID(pos)].form = w.form; prefs.w[posID(pos)].experience = w.experience; prefs.w[posID(pos)].stamina = w.stamina; prefs.w[posID(pos)].goalie = w.goalie; prefs.w[posID(pos)].playmaking = w.playmaking; prefs.w[posID(pos)].passing = w.passing; prefs.w[posID(pos)].winging = w.winging; prefs.w[posID(pos)].defending = w.defending; prefs.w[posID(pos)].scoring = w.scoring; prefs.w[posID(pos)].setting = w.setting; } UInt16 whichPositionPage() { struct positions p; if ( ! prefs.positionPage ) { p = calcPositions(getPlayer(prefs.selectedPlayer)); gotoBestPositionPage(&p); } return prefs.selectedPositionPage; } Boolean PositionScreenEHandler(EventPtr event) { Boolean handled = false; Int8 i; struct positions p; switch (event->eType) { case frmOpenEvent: gpForm = FrmGetActiveForm(); if ( bgForm ) { FrmEraseForm(bgForm); FrmDeleteForm(bgForm); } bgForm = NULL; updatePlayerList(); positionValues = NULL; p = calcPositions(getPlayer(prefs.selectedPlayer)); /* Set correct position-switch to be lit */ switch ( FrmGetActiveFormID() ) { case PositionScreen: showPositions(0, p); CtlSetValue(getOPtr(PositionBackButton), 1); break; case PositionScreen2: showPositions(1, p); CtlSetValue(getOPtr(PositionMidButton), 1); break; case PositionScreen3: showPositions(2, p); CtlSetValue(getOPtr(PositionForwButton), 1); break; } FrmDrawForm(gpForm); switch ( FrmGetActiveFormID() ) { case PositionScreen: showBestPositions(0, &p); break; case PositionScreen2: showBestPositions(1, &p); break; case PositionScreen3: showBestPositions(2, &p); break; } handled = true; break; case frmCloseEvent: for ( i = 0 ; i < 8; i++ ) MemPtrFree(positionValues[i]); MemPtrFree(positionValues); positionValues = NULL; if ( ! bgForm ) { FrmEraseForm(gpForm); FrmDeleteForm(gpForm); } gpForm = 0; handled = true; break; case ctlSelectEvent: switch (event->data.ctlEnter.controlID) { case PositionPlayerButton: FrmGotoForm(PlayerScreen); handled = true; break; case PositionBackButton: prefs.selectedPositionPage = PositionScreen; FrmGotoForm(PositionScreen); handled = true; break; case PositionMidButton: prefs.selectedPositionPage = PositionScreen2; FrmGotoForm(PositionScreen2); handled = true; break; case PositionForwButton: prefs.selectedPositionPage = PositionScreen3; FrmGotoForm(PositionScreen3); handled = true; break; } break; case popSelectEvent: switch (event->data.popSelect.listID) { case PlayerSelection: prefs.selectedPlayer = LstGetSelection(getOPtr(PlayerSelection)); FrmGotoForm(whichPositionPage()); break; default: break; } break; case menuEvent: handled = switchScreen(event->data.menu.itemID); break; default: break; } return handled; }