merge
authorunc0rr
Mon, 28 May 2012 20:04:49 +0400
changeset 7137 5587ab553f32
parent 7135 4d863aadd0b2 (current diff)
parent 7134 beb16926ae5c (diff)
child 7139 a47fd17ba463
merge
--- a/hedgewars/pas2c.h	Mon May 28 01:51:50 2012 -0400
+++ b/hedgewars/pas2c.h	Mon May 28 20:04:49 2012 +0400
@@ -1,5 +1,6 @@
 #pragma once
 
+#include <stddef.h>
 #include <stdint.h>
 #include <stdbool.h>
 #include <wchar.h>
@@ -56,17 +57,6 @@
 typedef int PtrInt;
 typedef wchar_t widechar;
 
-#ifdef __GNUG__
-#define NULL __null
-#else   /* G++ */
-/* shield NULL definition for non-gnu parsers */
-#ifndef __cplusplus
-#define NULL ((void *)0)
-#else
-#define NULL 0
-#endif  /* __cplusplus */
-#endif  /* G++ */
-
 #define new(a) __new((void **)&a, sizeof(*(a)))
 void __new(void ** p, int size);
 #define dispose(a) __dispose(a, sizeof(*(a)))
@@ -85,6 +75,7 @@
 bool _strcomparec(string255 a, char b);
 bool _strncompare(string255 a, string255 b);
 char * _pchar(string255 s);
+string255 pchar2str(char * s);
 
 int Length(string255 a);
 string255 copy(string255 a, int s, int l);
@@ -113,6 +104,7 @@
 void close(int f);
 
 void write(string255 s);
+void writeLn(string255 s);
 
 bool DirectoryExists(string255 dir);
 bool FileExists(string255 filename);
--- a/hedgewars/pas2cSystem.pas	Mon May 28 01:51:50 2012 -0400
+++ b/hedgewars/pas2cSystem.pas	Mon May 28 20:04:49 2012 +0400
@@ -68,6 +68,7 @@
     Length, StrToInt : function : integer;
     SetLength, val : procedure;
     _pchar : function : PChar;
+    pchar2str : function : string;
     memcpy : procedure;
 
     assign, rewrite, reset, flush, BlockWrite, BlockRead, close : procedure;
--- a/hedgewars/uAI.pas	Mon May 28 01:51:50 2012 -0400
+++ b/hedgewars/uAI.pas	Mon May 28 20:04:49 2012 +0400
@@ -142,41 +142,41 @@
                     inc(BestActions.Score, Score);
                     BestActions.isWalkingToABetterPlace:= false;
 
-                if (ap.Angle > 0) then
-                    AddAction(BestActions, aia_LookRight, 0, 200, 0, 0)
-            else if (ap.Angle < 0) then
-                AddAction(BestActions, aia_LookLeft, 0, 200, 0, 0);
+                    AddAction(BestActions, aia_Weapon, Longword(a), 300 + random(400), 0, 0);
 
-                AddAction(BestActions, aia_Weapon, Longword(a), 300 + random(400), 0, 0);
-                
-                if (ap.Time <> 0) then
-                    AddAction(BestActions, aia_Timer, ap.Time div 1000, 400, 0, 0);
-                if (Ammoz[a].Ammo.Propz and ammoprop_NoCrosshair) = 0 then
-                    begin
-                    ap.Angle:= LongInt(Me^.Angle) - Abs(ap.Angle);
-                    if ap.Angle > 0 then
+                    if (ap.Angle > 0) then
+                        AddAction(BestActions, aia_LookRight, 0, 200, 0, 0)
+                    else if (ap.Angle < 0) then
+                        AddAction(BestActions, aia_LookLeft, 0, 200, 0, 0);
+                    
+                    if (ap.Time <> 0) then
+                        AddAction(BestActions, aia_Timer, ap.Time div 1000, 400, 0, 0);
+                    if (Ammoz[a].Ammo.Propz and ammoprop_NoCrosshair) = 0 then
                         begin
-                        AddAction(BestActions, aia_Up, aim_push, 300 + random(250), 0, 0);
-                        AddAction(BestActions, aia_Up, aim_release, ap.Angle, 0, 0)
-                        end
-                    else if ap.Angle < 0 then
+                        ap.Angle:= LongInt(Me^.Angle) - Abs(ap.Angle);
+                        if ap.Angle > 0 then
+                            begin
+                            AddAction(BestActions, aia_Up, aim_push, 300 + random(250), 0, 0);
+                            AddAction(BestActions, aia_Up, aim_release, ap.Angle, 0, 0)
+                            end
+                        else if ap.Angle < 0 then
+                            begin
+                            AddAction(BestActions, aia_Down, aim_push, 300 + random(250), 0, 0);
+                            AddAction(BestActions, aia_Down, aim_release, -ap.Angle, 0, 0)
+                            end
+                        end;
+                    if (Ammoz[a].Ammo.Propz and ammoprop_NeedTarget) <> 0 then
                         begin
-                        AddAction(BestActions, aia_Down, aim_push, 300 + random(250), 0, 0);
-                        AddAction(BestActions, aia_Down, aim_release, -ap.Angle, 0, 0)
-                        end
-                    end;
-                if (Ammoz[a].Ammo.Propz and ammoprop_NeedTarget) <> 0 then
-                    begin
-                    AddAction(BestActions, aia_Put, 0, 1, ap.AttackPutX, ap.AttackPutY)
-                    end;
-                if (Ammoz[a].Ammo.Propz and ammoprop_AttackingPut) = 0 then
-                    begin
-                    AddAction(BestActions, aia_attack, aim_push, 650 + random(300), 0, 0);
-                    AddAction(BestActions, aia_attack, aim_release, ap.Power, 0, 0);
-                    end;
-                if ap.ExplR > 0 then
-                    AddAction(BestActions, aia_AwareExpl, ap.ExplR, 10, ap.ExplX, ap.ExplY);
-                end
+                        AddAction(BestActions, aia_Put, 0, 1, ap.AttackPutX, ap.AttackPutY)
+                        end;
+                    if (Ammoz[a].Ammo.Propz and ammoprop_AttackingPut) = 0 then
+                        begin
+                        AddAction(BestActions, aia_attack, aim_push, 650 + random(300), 0, 0);
+                        AddAction(BestActions, aia_attack, aim_release, ap.Power, 0, 0);
+                        end;
+                    if ap.ExplR > 0 then
+                        AddAction(BestActions, aia_AwareExpl, ap.ExplR, 10, ap.ExplX, ap.ExplY);
+                    end
             end;
         if a = High(TAmmoType) then
             a:= Low(TAmmoType)
@@ -205,10 +205,6 @@
 
 BotLevel:= Me^.Hedgehog^.BotLevel;
 
-tmp:= random(2) + 1;
-Push(0, Actions, Me^, tmp);
-Push(0, Actions, Me^, tmp xor 3);
-
 if (Me^.State and gstAttacked) = 0 then
     maxticks:= Max(0, TurnTimeLeft - 5000 - LongWord(4000 * BotLevel))
 else
@@ -221,8 +217,12 @@
 BaseRate:= Max(BestRate, 0);
 
 if (Ammoz[Me^.Hedgehog^.CurAmmoType].Ammo.Propz and ammoprop_NeedTarget) <> 0 then
-    AddAction(Actions, aia_Weapon, Longword(amNothing), 100 + random(200), 0, 0);
+    AddAction(Actions, aia_Weapon, Longword(amSkip), 100 + random(200), 0, 0);
 
+tmp:= random(2) + 1;
+Push(0, Actions, Me^, tmp);
+Push(0, Actions, Me^, tmp xor 3);
+    
 while (Stack.Count > 0) and (not StopThinking) and (GameFlags and gfArtillery = 0) do
     begin
     Pop(ticks, Actions, Me^);
--- a/hedgewars/uAIActions.pas	Mon May 28 01:51:50 2012 -0400
+++ b/hedgewars/uAIActions.pas	Mon May 28 20:04:49 2012 +0400
@@ -176,7 +176,8 @@
                     exit
                     end
                 else
-                    begin CheckHang(Me);
+                    begin 
+                    CheckHang(Me);
                     exit
                     end;
                             
@@ -194,7 +195,8 @@
                     exit
                     end
                 else
-                    begin CheckHang(Me);
+                    begin 
+                    CheckHang(Me);
                     exit
                     end;
             aia_LookLeft:
--- a/hedgewars/uAIAmmoTests.pas	Mon May 28 01:51:50 2012 -0400
+++ b/hedgewars/uAIAmmoTests.pas	Mon May 28 20:04:49 2012 +0400
@@ -364,7 +364,7 @@
         Vx:= ((Targ.X+10) - meX) / (TestTime + tDelta)
     else
         Vx:= ((Targ.X-10) - meX) / (TestTime + tDelta);
-    Vy:= cGravityf * ((TestTime + tDelta) div 2) - ((Targ.Y-150) - meY) / (TestTime + tDelta);
+    Vy:= cGravityf * ((TestTime + tDelta) div 2) - ((Targ.Y-50) - meY) / (TestTime + tDelta);
     r:= sqr(Vx)+sqr(Vy);
     if not (r > 1) then
         begin
@@ -388,7 +388,7 @@
      if valueResult < Score then
         begin
         ap.Angle:= DxDy2AttackAnglef(Vx, Vy) + AIrndSign(random(Level));
-        ap.Power:= trunc(sqrt(r) * cMaxPower * 0.9) + AIrndSign(random(Level) * 15);
+        ap.Power:= trunc(sqrt(r) * cMaxPower) + AIrndSign(random(Level) * 15);
         ap.Time:= TestTime;
         ap.ExplR:= 90;
         ap.ExplX:= EX;
@@ -416,7 +416,7 @@
 repeat
     inc(TestTime, 1000);
     Vx:= (Targ.X - meX) / (TestTime + tDelta);
-    Vy:= cGravityf * ((TestTime + tDelta) div 2) - ((Targ.Y-200) - meY) / (TestTime + tDelta);
+    Vy:= cGravityf * ((TestTime + tDelta) div 2) - ((Targ.Y-50) - meY) / (TestTime + tDelta);
     r:= sqr(Vx)+sqr(Vy);
     if not (r > 1) then
         begin
@@ -424,30 +424,31 @@
         y:= meY;
         dY:= -Vy;
         t:= TestTime;
-    repeat
-        x:= x + Vx;
-        y:= y + dY;
-        dY:= dY + cGravityf;
-        dec(t)
-    until TestCollExcludingMe(Me, trunc(x), trunc(y), 5) or (t = 0);
-    EX:= trunc(x);
-    EY:= trunc(y);
-    if t < 50 then 
-        Score:= RateExplosion(Me, EX, EY, 381)
-    else 
-        Score:= BadTurn;
+        repeat
+            x:= x + Vx;
+            y:= y + dY;
+            dY:= dY + cGravityf;
+            dec(t)
+        until TestCollExcludingMe(Me, trunc(x), trunc(y), 7) or (t = 0);
         
-    if valueResult < Score then
-        begin
-        ap.Angle:= DxDy2AttackAnglef(Vx, Vy) + AIrndSign(random(Level));
-        ap.Power:= trunc(sqrt(r) * cMaxPower * 0.9) + AIrndSign(random(Level) * 15);
-        ap.Time:= TestTime;
-        ap.ExplR:= 300;
-        ap.ExplX:= EX;
-        ap.ExplY:= EY;
-        valueResult:= Score
-        end;
-    end
+        EX:= trunc(x);
+        EY:= trunc(y);
+        if t < 50 then 
+            Score:= RateExplosion(Me, EX, EY, 200) + RateExplosion(Me, EX, EY + 120, 200)
+        else 
+            Score:= BadTurn;
+            
+        if valueResult < Score then
+            begin
+            ap.Angle:= DxDy2AttackAnglef(Vx, Vy) + AIrndSign(random(Level));
+            ap.Power:= trunc(sqrt(r) * cMaxPower) + AIrndSign(random(Level) * 15);
+            ap.Time:= TestTime;
+            ap.ExplR:= 300;
+            ap.ExplX:= EX;
+            ap.ExplY:= EY;
+            valueResult:= Score
+            end;
+        end
 until (TestTime = 4000);
 TestWatermelon:= valueResult
 end;
@@ -457,15 +458,15 @@
     var A, B, D, T: real;
         C: LongInt;
     begin
-        A:= sqr(cGravityf) * 0.25;
+        A:= sqr(cGravityf);
         B:= - cGravityf * (TY - MY) - 1;
         C:= sqr(TY - MY) + sqr(TX - MX);
-        D:= sqr(B) - (A * C * 4);
+        D:= sqr(B) - A * C;
         if D >= 0 then
             begin
-            D:= ( - B + sqrt(D)) * 0.5 / A;
+            D:= sqrt(D) - B;
             if D >= 0 then
-                T:= sqrt(D)
+                T:= sqrt(D * 2 / A)
             else
                 T:= 0;
             Solve:= trunc(T)
@@ -662,14 +663,15 @@
     x, y: real;
 begin
 Level:= Level; // avoid compiler hint
+TestFirePunch:= BadTurn;
 ap.ExplR:= 0;
 ap.Time:= 0;
 ap.Power:= 1;
 ap.Angle:= hwSign(Me^.dX);
 x:= hwFloat2Float(Me^.X);
 y:= hwFloat2Float(Me^.Y);
-if (Abs(trunc(x) - Targ.X) > 25)
-or (Abs(trunc(y) - 50 - Targ.Y) > 50) then
+if (Abs(trunc(x) - Targ.X) < 25)
+    and (Abs(trunc(y) - 50 - Targ.Y) < 50) then
     begin
 // TODO - find out WTH this works.
     if TestColl(trunc(x), trunc(y) - 16, 6) and 
@@ -772,6 +774,7 @@
 if (Level > 3) then
     exit(BadTurn);
 
+ap.Angle:= 0;
 ap.AttackPutX:= Targ.X;
 ap.AttackPutY:= Targ.Y;
 
--- a/hedgewars/uConsole.pas	Mon May 28 01:51:50 2012 -0400
+++ b/hedgewars/uConsole.pas	Mon May 28 20:04:49 2012 +0400
@@ -49,8 +49,10 @@
 end;
 
 procedure WriteToConsole(s: shortstring);
+{$IFNDEF NOCONSOLE}
 var Len: LongInt;
     done: boolean;
+{$ENDIF}
 begin
 {$IFNDEF NOCONSOLE}
 AddFileLog('[Con] ' + s);
--- a/hedgewars/uDebug.pas	Mon May 28 01:51:50 2012 -0400
+++ b/hedgewars/uDebug.pas	Mon May 28 20:04:49 2012 +0400
@@ -47,9 +47,13 @@
 end;
 
 procedure SDLTry(Assert: boolean; isFatal: boolean);
+var s: shortstring;
 begin
 if not Assert then
-    OutError(SDL_GetError, isFatal)
+    begin
+    s:= SDL_GetError();
+    OutError(s, isFatal)
+    end
 end;
 
 end.
--- a/hedgewars/uStore.pas	Mon May 28 01:51:50 2012 -0400
+++ b/hedgewars/uStore.pas	Mon May 28 20:04:49 2012 +0400
@@ -825,7 +825,7 @@
 if caption = '' then
     caption:= '???';
 if subcaption = '' then
-    subcaption:= ' ';
+    subcaption:= _S' ';
 
 font:= CheckCJKFont(caption,fnt16);
 font:= CheckCJKFont(subcaption,font);
@@ -909,9 +909,9 @@
         r:= WriteInRect(tmpsurf, cFontBorder + 2, r.y + r.h, $ff707070, font, tmpline);
 
         // render highlighted caption (if there is a ':')
-        tmpline2:= '';
+        tmpline2:= _S'';
         SplitByChar(tmpline, tmpline2, ':');
-        if tmpline2 <> '' then
+        if tmpline2 <> _S'' then
             WriteInRect(tmpsurf, cFontBorder + 2, r2.y + r2.h, $ffc7c7c7, font, tmpline + ':');
         end
     end;
@@ -954,7 +954,7 @@
 r.h:= 32;
 
 // default (no extra text)
-extra:= '';
+extra:= _S'';
 extracolor:= 0;
 
 if (CurrentTeam <> nil) and (Ammoz[atype].SkipTurns >= CurrentTeam^.Clan^.TurnNumber) then // weapon or utility is not yet available
@@ -969,7 +969,7 @@
     end
 else
     begin
-    extra:= '';
+    extra:= _S'';
     extracolor:= 0;
     end;
 
--- a/tools/pas2c.hs	Mon May 28 01:51:50 2012 -0400
+++ b/tools/pas2c.hs	Mon May 28 20:04:49 2012 +0400
@@ -39,10 +39,11 @@
         uniqCounter :: Int,
         toMangle :: Set.Set String,
         currentUnit :: String,
+        currentFunctionResult :: String,
         namespaces :: Map.Map String Records
     }
     
-emptyState = RenderState Map.empty "" BTUnknown [] 0 Set.empty ""
+emptyState = RenderState Map.empty "" BTUnknown [] 0 Set.empty "" ""
 
 getUniq :: State RenderState Int
 getUniq = do
@@ -381,13 +382,19 @@
     t <- type2C returnType
     t'<- gets lastType
     n <- id2C IOInsert $ setBaseType (BTFunction (numberOfDeclarations params) t') name
-    (p, ph) <- withState' (\st -> st{currentScope = Map.insertWith un (map toLower rv) [(render res, t')] $ currentScope st}) $ do
+    
+    let isVoid = case returnType of
+            VoidType -> True
+            _ -> False
+            
+    (p, ph) <- withState' (\st -> st{currentScope = Map.insertWith un (map toLower rv) [(render res, t')] $ currentScope st
+            , currentFunctionResult = if isVoid then [] else render res}) $ do
         p <- functionParams2C params
         ph <- liftM2 ($+$) (typesAndVars2C False tvars) (phrase2C' phrase)
         return (p, ph)
-    let phrasesBlock = case returnType of
-            VoidType -> ph
-            _ -> t empty <+> res <> semi $+$ ph $+$ text "return" <+> res <> semi
+        
+    let phrasesBlock = if isVoid then ph else t empty <+> res <> semi $+$ ph $+$ text "return" <+> res <> semi
+    
     return [ 
         t empty <+> n <> parens p
         $+$
@@ -615,6 +622,18 @@
         (BTFunction {}, (Reference r')) -> do
             e <- ref2C r'
             return $ r <+> text "=" <+> e <> semi
+        (BTString, _) -> do
+            e <- expr2C expr
+            lt <- gets lastType
+            case lt of
+                -- assume pointer to char for simplicity
+                BTPointerTo _ -> do
+                    e <- expr2C $ Reference $ FunCall [Reference $ RefExpression expr] (SimpleReference (Identifier "pchar2str" BTUnknown))
+                    return $ r <+> text "=" <+> e <> semi
+                BTString -> do
+                    e <- expr2C expr
+                    return $ r <+> text "=" <+> e <> semi
+                _ -> error $ "Assignment to string from " ++ show lt
         (BTArray (Range _) _ _, _) -> phrase2C $ 
             ProcCall (FunCall
                 [
@@ -671,7 +690,12 @@
     return $ text "do" <+> p <+> text "while" <> parens (text "!" <> parens e) <> semi
 phrase2C NOP = return $ text ";"
 
-phrase2C (BuiltInFunctionCall [] (SimpleReference (Identifier "exit" BTUnknown))) = return $ text "return" <> semi
+phrase2C (BuiltInFunctionCall [] (SimpleReference (Identifier "exit" BTUnknown))) = do
+    f <- gets currentFunctionResult
+    if null f then
+        return $ text "return" <> semi
+        else
+        return $ text "return" <+> text f <> semi
 phrase2C (BuiltInFunctionCall [] (SimpleReference (Identifier "break" BTUnknown))) = return $ text "break" <> semi
 phrase2C (BuiltInFunctionCall [] (SimpleReference (Identifier "continue" BTUnknown))) = return $ text "continue" <> semi
 phrase2C (BuiltInFunctionCall [e] (SimpleReference (Identifier "exit" BTUnknown))) = liftM (\e -> text "return" <+> e <> semi) $ expr2C e