hedgewars/uMisc.pas
changeset 2699 249adefa9c1c
parent 2698 90585aba87ad
child 2705 2b5625c4ec16
equal deleted inserted replaced
2698:90585aba87ad 2699:249adefa9c1c
    19 {$INCLUDE "options.inc"}
    19 {$INCLUDE "options.inc"}
    20 
    20 
    21 unit uMisc;
    21 unit uMisc;
    22 interface
    22 interface
    23 
    23 
    24 uses uConsts, SDLh,
    24 uses	SDLh, uConsts, uFloat,
    25 {$IFDEF GLES11}
    25 {$IFDEF GLES11}
    26 	gles11,
    26 	gles11;
    27 {$ELSE}
    27 {$ELSE}
    28 	GL,
    28 	GL;
    29 {$ENDIF}
    29 {$ENDIF}
    30 	uFloat
       
    31 {$IFDEF IPHONEOS}
       
    32 	, PascalExports
       
    33 {$ENDIF}
       
    34 	;
       
    35 
       
    36 type HwColor4f = record
       
    37 	r, g, b, a: byte
       
    38 	end;
       
    39 
    30 
    40 var
    31 var
    41 	isCursorVisible : boolean = false;
    32 	isCursorVisible : boolean;
    42 	isTerminated    : boolean = false;
    33 	isTerminated    : boolean;
    43 	isInLag         : boolean = false;
    34 	isInLag         : boolean;
    44 	isPaused        : boolean = false;
    35 	isPaused        : boolean;
    45 	isSoundEnabled  : boolean = true;
    36 	isSoundEnabled  : boolean;
    46 	isMusicEnabled  : boolean = false;
    37 	isMusicEnabled  : boolean;
    47 	isSEBackup      : boolean = true;
    38 	isSEBackup      : boolean;
    48 	isInMultiShoot  : boolean = false;
    39 	isInMultiShoot  : boolean;
    49 	isSpeed         : boolean = false;
    40 	isSpeed         : boolean;
    50 
    41 
    51 	fastUntilLag    : boolean = false;
    42 	fastUntilLag    : boolean;
    52 
    43 
    53 	GameState     : TGameState = Low(TGameState);
    44 	GameState	: TGameState;
    54 	GameType      : TGameType = gmtLocal;
    45 	GameType	: TGameType;
    55 	GameFlags     : Longword = 0;
    46 	GameFlags	: Longword;
    56 	TrainingFlags : Longword = 0;
    47 	TrainingFlags	: Longword;
    57 	TurnTimeLeft  : Longword = 0;
    48 	TurnTimeLeft	: Longword;
    58 	cSuddenDTurns : LongInt = 15;
    49 	cSuddenDTurns	: LongInt;
    59 	cDamagePercent  : LongInt = 100;
    50 	cDamagePercent	: LongInt;
    60 	cTemplateFilter : LongInt = 0;
    51 	cTemplateFilter	: LongInt;
    61 
    52 
    62 	cHedgehogTurnTime: Longword = 45000;
    53 	cHedgehogTurnTime: Longword;
    63 	cMinesTime       : LongInt = 3000;
    54 	cMinesTime       : LongInt;
    64 	cMaxAIThinkTime  : Longword = 9000;
    55 	cMaxAIThinkTime  : Longword;
    65 
    56 
    66 	cCloudsNumber    : LongInt = 9;
    57 	cCloudsNumber    : LongInt;
    67 	cScreenWidth     : LongInt = 1024;
    58 	cScreenWidth     : LongInt;
    68 	cScreenHeight    : LongInt = 768;
    59 	cScreenHeight    : LongInt;
    69 	cInitWidth       : LongInt = 1024;
    60 	cInitWidth       : LongInt;
    70 	cInitHeight      : LongInt = 768;
    61 	cInitHeight      : LongInt;
    71 	cVSyncInUse		 : boolean = true;	
    62 	cVSyncInUse	 : boolean;	
    72 	cBits            : LongInt = 32;
    63 	cBits            : LongInt;
    73 	cBitsStr         : string[2] = '32';
    64 	cBitsStr         : string[2];
    74 	cTagsMaskIndex   : byte = Low(cTagsMasks);
    65 	cTagsMaskIndex   : byte;
    75 	zoom             : GLfloat = 2.0;
    66 	zoom             : GLfloat;
    76 	ZoomValue        : GLfloat = 2.0;
    67 	ZoomValue        : GLfloat;
    77 
    68 
    78 	cWaterLine       : LongInt = LAND_HEIGHT;
    69 	cWaterLine       : LongInt;
    79 	cVisibleWater    : LongInt = 128;
    70 	cGearScrEdgesDist: LongInt;
    80 	cGearScrEdgesDist: LongInt = 240;
    71 	cAltDamage       : boolean;
    81 	cCursorEdgesDist : LongInt = 100;
    72 
    82 	cTeamHealthWidth : LongInt = 128;
    73 	GameTicks	: LongWord;
    83 	cAltDamage       : boolean = true;
    74 	TrainingTimeInc	: Longword;
    84 
    75 	TrainingTimeInD	: Longword;
    85 	GameTicks      : LongWord = 0;
    76 	TrainingTimeInM	: Longword;
    86 	TrainingTimeInc: Longword = 10000;
    77 	TrainingTimeMax	: Longword;
    87 	TrainingTimeInD: Longword = 500;
    78 
    88 	TrainingTimeInM: Longword = 5000;
    79 	TimeTrialStartTime: Longword;
    89 	TrainingTimeMax: Longword = 60000;
    80 	TimeTrialStopTime : Longword;
    90 
       
    91 	TimeTrialStartTime: Longword = 0;
       
    92 	TimeTrialStopTime : Longword = 0;
       
    93 	
    81 	
    94 	cWhiteColorChannels	: TSDL_Color = (r:$FF; g:$FF; b:$FF; unused:$FF);
    82 	recordFileName	: shortstring;
    95 	cNearBlackColorChannels	: TSDL_Color = (r:$00; g:$00; b:$10; unused:$FF);
    83 	cShowFPS	: boolean;
    96 
    84 	cCaseFactor	: Longword;
    97 	cWhiteColor		: Longword = $FFFFFFFF;
    85 	cLandAdditions	: Longword;
    98 	cYellowColor		: Longword = $FFFFFF00;
    86 	cFullScreen	: boolean;
    99 	cExplosionBorderColor	: LongWord = $FF808080;
    87 	cReducedQuality	: boolean;
   100 
    88 	cLocaleFName	: shortstring;
   101 var recordFileName : shortstring = '';
    89 	cSeed		: shortstring;
   102 	cShowFPS      : boolean = false;
    90 	cInitVolume	: LongInt;
   103 	cCaseFactor   : Longword = 5;  {0..9}
    91 	cVolumeDelta	: LongInt;
   104 	cLandAdditions: Longword = 4;
    92 	cTimerInterval	: Longword;
   105 	cFullScreen   : boolean = false;
    93 	cHasFocus	: boolean;
   106 	cReducedQuality : boolean = false;
    94 	cInactDelay	: Longword;
   107 	cLocaleFName  : shortstring = 'en.txt';
    95 
   108 	cSeed         : shortstring = '';
    96 	bBetweenTurns	: boolean;
   109 	cInitVolume   : LongInt = 50;
    97 	cHealthDecrease	: LongWord;
   110 	cVolumeDelta  : LongInt = 0;
    98 	bWaterRising	: Boolean;
   111 	cTimerInterval: Longword = 8;
    99 
   112 	cHasFocus     : boolean = true;
   100 	ShowCrosshair	: boolean;
   113 	cInactDelay   : Longword = 1250;
   101 	CursorMovementX : Integer;
   114 
   102 	CursorMovementY : Integer;
   115 	bBetweenTurns: boolean = false;
   103 	cDrownSpeed	: hwFloat;
   116 	cHealthDecrease: LongWord = 0;
   104 	cMaxWindSpeed	: hwFloat;
   117 	bWaterRising   : Boolean = false;
   105 	cWindSpeed	: hwFloat;
   118 
   106 	cGravity	: hwFloat;
   119 {$WARNINGS OFF}
   107 	cDamageModifier	: hwFloat;
   120 	cAirPlaneSpeed: hwFloat = (isNegative: false; QWordValue:   3006477107); // 1.4
   108 	cLaserSighting	: boolean;
   121 	cBombsSpeed   : hwFloat = (isNegative: false; QWordValue:    429496729);
   109 	cVampiric	: boolean;
   122 {$WARNINGS ON}
   110 	cArtillery	: boolean;
   123 
   111 
   124 	cSendCursorPosTime   : LongWord = 50;
   112 	flagMakeCapture	: boolean;
   125 	ShowCrosshair : boolean;
   113 
   126 	CursorMovementX : Integer = 0;
   114 	InitStepsFlags	: Longword;
   127 	CursorMovementY : Integer = 0;
   115 	RealTicks	: Longword;
   128 	cDrownSpeed,
   116 	AttackBar	: LongInt;
   129 	cMaxWindSpeed,
   117 
   130 	cWindSpeed,
   118 	WaterColorArray	: array[0..3] of HwColor4f;
   131 	cGravity: hwFloat;
   119 
   132 	cDamageModifier: hwFloat;
   120 	CursorPoint	: TPoint;
   133 	cLaserSighting: boolean;
   121 	TargetPoint	: TPoint;
   134 	cVampiric: boolean;
   122 
   135 	cArtillery: boolean;
   123 
   136 
   124 procedure init_uMisc;
   137 	flagMakeCapture: boolean = false;
   125 procedure free_uMisc;
   138 
       
   139 	InitStepsFlags: Longword = 0;
       
   140 
       
   141 	RealTicks: Longword = 0;
       
   142 
       
   143 	AttackBar: LongInt = 0; // 0 - none, 1 - just bar at the right-down corner, 2 - like in WWP
       
   144 
       
   145 	i: LongInt;
       
   146 	
       
   147 	cWaterOpacity: byte = $80;
       
   148 	WaterColorArray: array[0..3] of HwColor4f;
       
   149 
       
   150 	CursorPoint: TPoint;
       
   151     TargetPoint: TPoint = (X: NoPointX; Y: 0);
       
   152 
       
   153 procedure movecursor(dx, dy: Integer);
   126 procedure movecursor(dx, dy: Integer);
   154 function hwSign(r: hwFloat): LongInt;
   127 function  hwSign(r: hwFloat): LongInt;
   155 function Min(a, b: LongInt): LongInt;
   128 function  Min(a, b: LongInt): LongInt;
   156 function Max(a, b: LongInt): LongInt;
   129 function  Max(a, b: LongInt): LongInt;
   157 procedure OutError(Msg: String; isFatalError: boolean);
   130 procedure OutError(Msg: String; isFatalError: boolean);
   158 procedure TryDo(Assert: boolean; Msg: string; isFatal: boolean);
   131 procedure TryDo(Assert: boolean; Msg: string; isFatal: boolean);
   159 procedure SDLTry(Assert: boolean; isFatal: boolean);
   132 procedure SDLTry(Assert: boolean; isFatal: boolean);
   160 function IntToStr(n: LongInt): shortstring;
   133 function  IntToStr(n: LongInt): shortstring;
   161 function FloatToStr(n: hwFloat): shortstring;
   134 function  FloatToStr(n: hwFloat): shortstring;
   162 function DxDy2Angle(const _dY, _dX: hwFloat): GLfloat;
   135 function  DxDy2Angle(const _dY, _dX: hwFloat): GLfloat;
   163 function DxDy2Angle32(const _dY, _dX: hwFloat): LongInt;
   136 function  DxDy2Angle32(const _dY, _dX: hwFloat): LongInt;
   164 function DxDy2AttackAngle(const _dY, _dX: hwFloat): LongInt;
   137 function  DxDy2AttackAngle(const _dY, _dX: hwFloat): LongInt;
   165 procedure AdjustColor(var Color: Longword);
   138 procedure AdjustColor(var Color: Longword);
   166 {$IFDEF DEBUGFILE}
       
   167 procedure AddFileLog(s: shortstring);
       
   168 function RectToStr(Rect: TSDL_Rect): shortstring;
       
   169 {$ENDIF}
       
   170 procedure SetKB(n: Longword);
   139 procedure SetKB(n: Longword);
   171 procedure SendKB;
   140 procedure SendKB;
   172 procedure SetLittle(var r: hwFloat);
   141 procedure SetLittle(var r: hwFloat);
   173 procedure SendStat(sit: TStatInfoType; s: shortstring);
   142 procedure SendStat(sit: TStatInfoType; s: shortstring);
   174 function  Str2PChar(const s: shortstring): PChar;
   143 function  Str2PChar(const s: shortstring): PChar;
   175 function NewTexture(width, height: Longword; buf: Pointer): PTexture;
   144 function  NewTexture(width, height: Longword; buf: Pointer): PTexture;
   176 function  Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture;
   145 function  Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture;
   177 procedure FreeTexture(tex: PTexture);
   146 procedure FreeTexture(tex: PTexture);
   178 function  toPowerOf2(i: Longword): Longword;
   147 function  toPowerOf2(i: Longword): Longword;
   179 function  DecodeBase64(s: shortstring): shortstring;
   148 function  DecodeBase64(s: shortstring): shortstring;
   180 function  doSurfaceConversion(tmpsurf: PSDL_Surface): PSDL_Surface;
   149 function  doSurfaceConversion(tmpsurf: PSDL_Surface): PSDL_Surface;
   181 function  endian(independent: LongWord): LongWord;
   150 function  endian(independent: LongWord): LongWord;
       
   151 function  modifyDamage(dmg: Longword): Longword;
       
   152 {$IFDEF DEBUGFILE}
       
   153 procedure AddFileLog(s: shortstring);
       
   154 function  RectToStr(Rect: TSDL_Rect): shortstring;
       
   155 {$ENDIF}
   182 {$IFNDEF IPHONEOS}
   156 {$IFNDEF IPHONEOS}
   183 procedure MakeScreenshot(s: shortstring);
   157 procedure MakeScreenshot(s: shortstring);
   184 {$ENDIF}
   158 {$ENDIF}
   185 function modifyDamage(dmg: Longword): Longword;
       
   186 
       
   187 
   159 
   188 implementation
   160 implementation
   189 uses uConsole, uStore, uIO, Math, uRandom, uSound;
   161 uses Math, uConsole, uStore, uIO, uRandom, uSound;
   190 var KBnum: Longword = 0;
   162 
       
   163 var KBnum: Longword;
   191 {$IFDEF DEBUGFILE}
   164 {$IFDEF DEBUGFILE}
   192 var f: textfile;
   165     f: textfile;
   193 {$ENDIF}
   166 {$ENDIF}
   194 
   167 
   195 procedure movecursor(dx, dy: Integer);
   168 procedure movecursor(dx, dy: Integer);
   196 var x, y: LongInt;
   169 var x, y: LongInt;
   197 begin
   170 begin
   572 	  ((independent and $0000FF00) shl 8) or
   545 	  ((independent and $0000FF00) shl 8) or
   573 	  ((independent and $000000FF) shl 24))
   546 	  ((independent and $000000FF) shl 24))
   574 {$ENDIF}
   547 {$ENDIF}
   575 end;
   548 end;
   576 
   549 
   577 initialization
   550 
   578 cDrownSpeed.QWordValue:= 257698038;// 0.06
   551 procedure init_uMisc;
   579 cMaxWindSpeed.QWordValue:= 2147484;// 0.0005
   552 var i: LongInt;
   580 cWindSpeed.QWordValue:= 429496;// 0.0001
   553 begin
   581 cGravity:= cMaxWindSpeed;
   554 	cDrownSpeed.QWordValue	:= 257698038;		// 0.06
   582 cDamageModifier:= _1;
   555 	cMaxWindSpeed.QWordValue:= 2147484;		// 0.0005
   583 cLaserSighting:= false;
   556 	cWindSpeed.QWordValue	:= 429496;		// 0.0001
   584 cVampiric:= false;
   557 	cGravity		:= cMaxWindSpeed;
   585 cArtillery:= false;
   558 	cDamageModifier		:= _1;
       
   559 	TargetPoint		:= cTargetPointRef;
       
   560 	
       
   561 	// int, longint longword and byte
       
   562 	CursorMovementX		:= 0;
       
   563 	CursorMovementY		:= 0;
       
   564 	GameTicks		:= 0;
       
   565 	TrainingTimeInc		:= 10000;
       
   566 	TrainingTimeInD		:= 500;
       
   567 	TrainingTimeInM		:= 5000;
       
   568 	TrainingTimeMax		:= 60000;
       
   569 	TimeTrialStartTime	:= 0;
       
   570 	TimeTrialStopTime	:= 0;
       
   571 	cWaterLine		:= LAND_HEIGHT;
       
   572 	cGearScrEdgesDist	:= 240;
       
   573 	cHealthDecrease		:= 0;
       
   574 
       
   575 	GameFlags		:= 0;
       
   576 	TrainingFlags		:= 0;
       
   577 	TurnTimeLeft		:= 0;
       
   578 	cSuddenDTurns		:= 15;
       
   579 	cDamagePercent		:= 100;
       
   580 	cTemplateFilter		:= 0;
       
   581 
       
   582 	cHedgehogTurnTime	:= 45000;
       
   583 	cMinesTime		:= 3000;
       
   584 	cMaxAIThinkTime		:= 9000;
       
   585 
       
   586 	cCloudsNumber		:= 9;
       
   587 	cScreenWidth		:= 1024;
       
   588 	cScreenHeight		:= 768;
       
   589 	cInitWidth		:= cScreenWidth;
       
   590 	cInitHeight		:= cScreenHeight;
       
   591 	cBits			:= 32;
       
   592 	cTagsMaskIndex		:= Low(cTagsMasks);
       
   593 	KBnum			:= 0;
       
   594 	InitStepsFlags		:= 0;
       
   595 	RealTicks		:= 0;
       
   596 	AttackBar		:= 0; // 0 - none, 1 - just bar at the right-down corner, 2 - like in WWP
       
   597 	
       
   598 	// tgametype and glfloat and string
       
   599 	GameState		:= Low(TGameState);
       
   600 	GameType		:= gmtLocal;
       
   601 	zoom			:= 2.0;
       
   602 	ZoomValue		:= 2.0;
       
   603 	cBitsStr		:= '32';
       
   604 
       
   605 	// booleans
       
   606 	cLaserSighting		:= false;
       
   607 	cVampiric		:= false;
       
   608 	cArtillery		:= false;
       
   609 	flagMakeCapture		:= false;
       
   610 	bBetweenTurns		:= false;
       
   611 	bWaterRising		:= false;
       
   612 	isCursorVisible		:= false;
       
   613 	isTerminated		:= false;
       
   614 	isInLag			:= false;
       
   615 	isPaused		:= false;
       
   616 	isMusicEnabled		:= false;
       
   617 	isInMultiShoot		:= false;
       
   618 	isSpeed			:= false;
       
   619 	fastUntilLag		:= false;
       
   620 	cVSyncInUse		:= true;	
       
   621 	isSoundEnabled		:= true;
       
   622 	isSEBackup		:= true;
       
   623 	
       
   624 	// init flags
       
   625 	recordFileName		:= '';
       
   626 	cShowFPS		:= false;
       
   627 	cCaseFactor		:= 5;  {0..9}
       
   628 	cLandAdditions		:= 4;
       
   629 	cFullScreen		:= false;
       
   630 	cReducedQuality		:= false;
       
   631 	cLocaleFName		:= 'en.txt';
       
   632 	cSeed			:= '';
       
   633 	cInitVolume		:= 50;
       
   634 	cVolumeDelta		:= 0;
       
   635 	cTimerInterval		:= 8;
       
   636 	cHasFocus		:= true;
       
   637 	cInactDelay		:= 1250;
       
   638 	cAltDamage		:= true;
   586 
   639 
   587 {$IFDEF DEBUGFILE}
   640 {$IFDEF DEBUGFILE}
   588 {$I-}
   641 {$I-}
   589 for i:= 0 to 7 do
   642 {$IFDEF IPHONEOS}
   590 begin
       
   591 {$IFDEF IPHONEDBG}
       
   592 	f:= stderr;
   643 	f:= stderr;
   593 {$ELSE}
   644 {$ELSE}
   594 	assign(f, 
   645 	for i:= 0 to 7 do
   595   {$IFDEF IPHONEOS}
       
   596 	  string(IPH_getDocumentsPath())
       
   597   {$ELSE}
       
   598 	  ParamStr(1)
       
   599   {$ENDIF}
       
   600 	  + '/debug' + inttostr(i) + '.txt');
       
   601 {$ENDIF}
       
   602 	rewrite(f);
       
   603 	if IOResult = 5 then
       
   604 	begin
   646 	begin
   605 		// prevent writing on a directory you do not have permissions on
   647 		assign(f, ParamStr(1) + '/debug' + inttostr(i) + '.txt');
   606 		// should be safe to assume the current directory is writable
       
   607 		assign(f, './debug' + inttostr(i) + '.txt');
       
   608 		rewrite(f);
   648 		rewrite(f);
       
   649 		if IOResult = 5 then
       
   650 		begin
       
   651 			// prevent writing on a directory you do not have permissions on
       
   652 			// should be safe to assume the current directory is writable
       
   653 			assign(f, './debug' + inttostr(i) + '.txt');
       
   654 			rewrite(f);
       
   655 		end;
       
   656 		if IOResult = 0 then break;
   609 	end;
   657 	end;
   610 	if IOResult = 0 then break
   658 {$ENDIF}
   611 end;
   659 
   612 {$I+}
   660 {$I+}
   613 
   661 {$ENDIF}
   614 finalization
   662 
       
   663 end;
       
   664 
       
   665 procedure free_uMisc;
       
   666 begin
   615 //uRandom.DumpBuffer;
   667 //uRandom.DumpBuffer;
   616 
   668 
       
   669 {$IFDEF DEBUGFILE}
   617 writeln(f, 'halt at ', GameTicks, ' ticks. TurnTimeLeft = ', TurnTimeLeft);
   670 writeln(f, 'halt at ', GameTicks, ' ticks. TurnTimeLeft = ', TurnTimeLeft);
   618 flush(f);
   671 flush(f);
   619 close(f)
   672 close(f);
   620 
   673 {$ENDIF}
   621 {$ENDIF}
   674 end;
   622 
   675 
   623 end.
   676 end.